blob: fb7cb38a6d83bf409602285bcadb7c0ce135905f [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)
127
Ilya Dryomoved95b212016-08-12 16:40:02 +0200128#define RBD_FEATURES_ALL (RBD_FEATURE_LAYERING | \
129 RBD_FEATURE_STRIPINGV2 | \
Ilya Dryomov7e973322017-01-25 18:16:22 +0100130 RBD_FEATURE_EXCLUSIVE_LOCK | \
131 RBD_FEATURE_DATA_POOL)
Alex Elderd8891402012-10-09 13:50:17 -0700132
133/* Features supported by this (client software) implementation. */
134
Alex Elder770eba62012-10-25 23:34:40 -0500135#define RBD_FEATURES_SUPPORTED (RBD_FEATURES_ALL)
Alex Elderd8891402012-10-09 13:50:17 -0700136
Alex Elder81a89792012-02-02 08:13:30 -0600137/*
138 * An RBD device name will be "rbd#", where the "rbd" comes from
139 * RBD_DRV_NAME above, and # is a unique integer identifier.
Alex Elder81a89792012-02-02 08:13:30 -0600140 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700141#define DEV_NAME_LEN 32
142
143/*
144 * block device image metadata (in-memory version)
145 */
146struct rbd_image_header {
Alex Elderf35a4de2013-05-06 09:51:29 -0500147 /* These six fields never change for a given rbd image */
Alex Elder849b4262012-07-09 21:04:24 -0500148 char *object_prefix;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700149 __u8 obj_order;
Alex Elderf35a4de2013-05-06 09:51:29 -0500150 u64 stripe_unit;
151 u64 stripe_count;
Ilya Dryomov7e973322017-01-25 18:16:22 +0100152 s64 data_pool_id;
Alex Elderf35a4de2013-05-06 09:51:29 -0500153 u64 features; /* Might be changeable someday? */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700154
Alex Elderf84344f2012-08-31 17:29:51 -0500155 /* The remaining fields need to be updated occasionally */
156 u64 image_size;
157 struct ceph_snap_context *snapc;
Alex Elderf35a4de2013-05-06 09:51:29 -0500158 char *snap_names; /* format 1 only */
159 u64 *snap_sizes; /* format 1 only */
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700160};
161
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500162/*
163 * An rbd image specification.
164 *
165 * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely
Alex Elderc66c6e02012-11-01 08:39:26 -0500166 * identify an image. Each rbd_dev structure includes a pointer to
167 * an rbd_spec structure that encapsulates this identity.
168 *
169 * Each of the id's in an rbd_spec has an associated name. For a
170 * user-mapped image, the names are supplied and the id's associated
171 * with them are looked up. For a layered image, a parent image is
172 * defined by the tuple, and the names are looked up.
173 *
174 * An rbd_dev structure contains a parent_spec pointer which is
175 * non-null if the image it represents is a child in a layered
176 * image. This pointer will refer to the rbd_spec structure used
177 * by the parent rbd_dev for its own identity (i.e., the structure
178 * is shared between the parent and child).
179 *
180 * Since these structures are populated once, during the discovery
181 * phase of image construction, they are effectively immutable so
182 * we make no effort to synchronize access to them.
183 *
184 * Note that code herein does not assume the image name is known (it
185 * could be a null pointer).
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500186 */
187struct rbd_spec {
188 u64 pool_id;
Alex Elderecb4dc22013-04-26 09:43:47 -0500189 const char *pool_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500190
Alex Elderecb4dc22013-04-26 09:43:47 -0500191 const char *image_id;
192 const char *image_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500193
194 u64 snap_id;
Alex Elderecb4dc22013-04-26 09:43:47 -0500195 const char *snap_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500196
197 struct kref kref;
198};
199
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700200/*
Alex Elderf0f8cef2012-01-29 13:57:44 -0600201 * an instance of the client. multiple devices may share an rbd client.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700202 */
203struct rbd_client {
204 struct ceph_client *client;
205 struct kref kref;
206 struct list_head node;
207};
208
Alex Elderbf0d5f502012-11-22 00:00:08 -0600209struct rbd_img_request;
210typedef void (*rbd_img_callback_t)(struct rbd_img_request *);
211
212#define BAD_WHICH U32_MAX /* Good which or bad which, which? */
213
214struct rbd_obj_request;
215typedef void (*rbd_obj_callback_t)(struct rbd_obj_request *);
216
Alex Elder9969ebc2013-01-18 12:31:10 -0600217enum obj_request_type {
218 OBJ_REQUEST_NODATA, OBJ_REQUEST_BIO, OBJ_REQUEST_PAGES
219};
Alex Elderbf0d5f502012-11-22 00:00:08 -0600220
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800221enum obj_operation_type {
222 OBJ_OP_WRITE,
223 OBJ_OP_READ,
Guangliang Zhao90e98c52014-04-01 22:22:16 +0800224 OBJ_OP_DISCARD,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800225};
226
Alex Elder926f9b32013-02-11 12:33:24 -0600227enum obj_req_flags {
228 OBJ_REQ_DONE, /* completion flag: not done = 0, done = 1 */
Alex Elder6365d332013-02-11 12:33:24 -0600229 OBJ_REQ_IMG_DATA, /* object usage: standalone = 0, image = 1 */
Alex Elder5679c592013-02-11 12:33:24 -0600230 OBJ_REQ_KNOWN, /* EXISTS flag valid: no = 0, yes = 1 */
231 OBJ_REQ_EXISTS, /* target exists: no = 0, yes = 1 */
Alex Elder926f9b32013-02-11 12:33:24 -0600232};
233
Alex Elderbf0d5f502012-11-22 00:00:08 -0600234struct rbd_obj_request {
Ilya Dryomova90bb0c2017-01-25 18:16:23 +0100235 u64 object_no;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600236 u64 offset; /* object start byte */
237 u64 length; /* bytes from offset */
Alex Elder926f9b32013-02-11 12:33:24 -0600238 unsigned long flags;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600239
Alex Elderc5b5ef62013-02-11 12:33:24 -0600240 /*
241 * An object request associated with an image will have its
242 * img_data flag set; a standalone object request will not.
243 *
244 * A standalone object request will have which == BAD_WHICH
245 * and a null obj_request pointer.
246 *
247 * An object request initiated in support of a layered image
248 * object (to check for its existence before a write) will
249 * have which == BAD_WHICH and a non-null obj_request pointer.
250 *
251 * Finally, an object request for rbd image data will have
252 * which != BAD_WHICH, and will have a non-null img_request
253 * pointer. The value of which will be in the range
254 * 0..(img_request->obj_request_count-1).
255 */
256 union {
257 struct rbd_obj_request *obj_request; /* STAT op */
258 struct {
259 struct rbd_img_request *img_request;
260 u64 img_offset;
261 /* links for img_request->obj_requests list */
262 struct list_head links;
263 };
264 };
Alex Elderbf0d5f502012-11-22 00:00:08 -0600265 u32 which; /* posn image request list */
266
267 enum obj_request_type type;
Alex Elder788e2df2013-01-17 12:25:27 -0600268 union {
269 struct bio *bio_list;
270 struct {
271 struct page **pages;
272 u32 page_count;
273 };
274 };
Alex Elder0eefd472013-04-19 15:34:50 -0500275 struct page **copyup_pages;
Alex Elderebda6402013-05-10 16:29:22 -0500276 u32 copyup_page_count;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600277
278 struct ceph_osd_request *osd_req;
279
280 u64 xferred; /* bytes transferred */
Sage Weil1b83bef2013-02-25 16:11:12 -0800281 int result;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600282
283 rbd_obj_callback_t callback;
Alex Elder788e2df2013-01-17 12:25:27 -0600284 struct completion completion;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600285
286 struct kref kref;
287};
288
Alex Elder0c425242013-02-08 09:55:49 -0600289enum img_req_flags {
Alex Elder9849e982013-01-24 16:13:36 -0600290 IMG_REQ_WRITE, /* I/O direction: read = 0, write = 1 */
291 IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */
Alex Elderd0b2e942013-01-24 16:13:36 -0600292 IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */
Guangliang Zhao90e98c52014-04-01 22:22:16 +0800293 IMG_REQ_DISCARD, /* discard: normal = 0, discard request = 1 */
Alex Elder0c425242013-02-08 09:55:49 -0600294};
295
Alex Elderbf0d5f502012-11-22 00:00:08 -0600296struct rbd_img_request {
Alex Elderbf0d5f502012-11-22 00:00:08 -0600297 struct rbd_device *rbd_dev;
298 u64 offset; /* starting image byte offset */
299 u64 length; /* byte count from offset */
Alex Elder0c425242013-02-08 09:55:49 -0600300 unsigned long flags;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600301 union {
Alex Elder9849e982013-01-24 16:13:36 -0600302 u64 snap_id; /* for reads */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600303 struct ceph_snap_context *snapc; /* for writes */
Alex Elder9849e982013-01-24 16:13:36 -0600304 };
305 union {
306 struct request *rq; /* block request */
307 struct rbd_obj_request *obj_request; /* obj req initiator */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600308 };
Alex Elder3d7efd12013-04-19 15:34:50 -0500309 struct page **copyup_pages;
Alex Elderebda6402013-05-10 16:29:22 -0500310 u32 copyup_page_count;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600311 spinlock_t completion_lock;/* protects next_completion */
312 u32 next_completion;
313 rbd_img_callback_t callback;
Alex Elder55f27e02013-04-10 12:34:25 -0500314 u64 xferred;/* aggregate bytes transferred */
Alex Eldera5a337d2013-01-24 16:13:36 -0600315 int result; /* first nonzero obj_request result */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600316
317 u32 obj_request_count;
318 struct list_head obj_requests; /* rbd_obj_request structs */
319
320 struct kref kref;
321};
322
323#define for_each_obj_request(ireq, oreq) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600324 list_for_each_entry(oreq, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600325#define for_each_obj_request_from(ireq, oreq) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600326 list_for_each_entry_from(oreq, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600327#define for_each_obj_request_safe(ireq, oreq, n) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600328 list_for_each_entry_safe_reverse(oreq, n, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600329
Ilya Dryomov99d16942016-08-12 16:11:41 +0200330enum rbd_watch_state {
331 RBD_WATCH_STATE_UNREGISTERED,
332 RBD_WATCH_STATE_REGISTERED,
333 RBD_WATCH_STATE_ERROR,
334};
335
Ilya Dryomoved95b212016-08-12 16:40:02 +0200336enum rbd_lock_state {
337 RBD_LOCK_STATE_UNLOCKED,
338 RBD_LOCK_STATE_LOCKED,
339 RBD_LOCK_STATE_RELEASING,
340};
341
342/* WatchNotify::ClientId */
343struct rbd_client_id {
344 u64 gid;
345 u64 handle;
346};
347
Alex Elderf84344f2012-08-31 17:29:51 -0500348struct rbd_mapping {
Alex Elder99c1f082012-08-30 14:42:15 -0500349 u64 size;
Alex Elder34b13182012-07-13 20:35:12 -0500350 u64 features;
Alex Elderf84344f2012-08-31 17:29:51 -0500351 bool read_only;
352};
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
NeilBrownf856dc32017-06-18 14:38:58 +1000445static struct bio_set *rbd_bio_clone;
446
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200447static int rbd_major;
Ilya Dryomovf8a22fc2013-12-13 15:28:57 +0200448static DEFINE_IDA(rbd_dev_id_ida);
449
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +0400450static struct workqueue_struct *rbd_wq;
451
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200452/*
453 * Default to false for now, as single-major requires >= 0.75 version of
454 * userspace rbd utility.
455 */
456static bool single_major = false;
457module_param(single_major, bool, S_IRUGO);
458MODULE_PARM_DESC(single_major, "Use a single major number for all rbd devices (default: false)");
459
Alex Elder3d7efd12013-04-19 15:34:50 -0500460static int rbd_img_request_submit(struct rbd_img_request *img_request);
461
Alex Elderf0f8cef2012-01-29 13:57:44 -0600462static ssize_t rbd_add(struct bus_type *bus, const char *buf,
463 size_t count);
464static ssize_t rbd_remove(struct bus_type *bus, const char *buf,
465 size_t count);
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200466static ssize_t rbd_add_single_major(struct bus_type *bus, const char *buf,
467 size_t count);
468static ssize_t rbd_remove_single_major(struct bus_type *bus, const char *buf,
469 size_t count);
Ilya Dryomov6d69bb532015-10-11 19:38:00 +0200470static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth);
Alex Eldera2acd002013-05-08 22:50:04 -0500471static void rbd_spec_put(struct rbd_spec *spec);
Alex Elderf0f8cef2012-01-29 13:57:44 -0600472
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200473static int rbd_dev_id_to_minor(int dev_id)
474{
Ilya Dryomov7e513d42013-12-16 19:26:32 +0200475 return dev_id << RBD_SINGLE_MAJOR_PART_SHIFT;
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200476}
477
478static int minor_to_rbd_dev_id(int minor)
479{
Ilya Dryomov7e513d42013-12-16 19:26:32 +0200480 return minor >> RBD_SINGLE_MAJOR_PART_SHIFT;
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200481}
482
Ilya Dryomoved95b212016-08-12 16:40:02 +0200483static bool __rbd_is_lock_owner(struct rbd_device *rbd_dev)
484{
485 return rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED ||
486 rbd_dev->lock_state == RBD_LOCK_STATE_RELEASING;
487}
488
489static bool rbd_is_lock_owner(struct rbd_device *rbd_dev)
490{
491 bool is_lock_owner;
492
493 down_read(&rbd_dev->lock_rwsem);
494 is_lock_owner = __rbd_is_lock_owner(rbd_dev);
495 up_read(&rbd_dev->lock_rwsem);
496 return is_lock_owner;
497}
498
Ilya Dryomov8767b292017-03-02 19:56:57 +0100499static ssize_t rbd_supported_features_show(struct bus_type *bus, char *buf)
500{
501 return sprintf(buf, "0x%llx\n", RBD_FEATURES_SUPPORTED);
502}
503
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700504static BUS_ATTR(add, S_IWUSR, NULL, rbd_add);
505static BUS_ATTR(remove, S_IWUSR, NULL, rbd_remove);
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200506static BUS_ATTR(add_single_major, S_IWUSR, NULL, rbd_add_single_major);
507static BUS_ATTR(remove_single_major, S_IWUSR, NULL, rbd_remove_single_major);
Ilya Dryomov8767b292017-03-02 19:56:57 +0100508static BUS_ATTR(supported_features, S_IRUGO, rbd_supported_features_show, NULL);
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700509
510static struct attribute *rbd_bus_attrs[] = {
511 &bus_attr_add.attr,
512 &bus_attr_remove.attr,
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200513 &bus_attr_add_single_major.attr,
514 &bus_attr_remove_single_major.attr,
Ilya Dryomov8767b292017-03-02 19:56:57 +0100515 &bus_attr_supported_features.attr,
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700516 NULL,
Alex Elderf0f8cef2012-01-29 13:57:44 -0600517};
Ilya Dryomov92c76dc2013-12-13 15:28:57 +0200518
519static umode_t rbd_bus_is_visible(struct kobject *kobj,
520 struct attribute *attr, int index)
521{
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200522 if (!single_major &&
523 (attr == &bus_attr_add_single_major.attr ||
524 attr == &bus_attr_remove_single_major.attr))
525 return 0;
526
Ilya Dryomov92c76dc2013-12-13 15:28:57 +0200527 return attr->mode;
528}
529
530static const struct attribute_group rbd_bus_group = {
531 .attrs = rbd_bus_attrs,
532 .is_visible = rbd_bus_is_visible,
533};
534__ATTRIBUTE_GROUPS(rbd_bus);
Alex Elderf0f8cef2012-01-29 13:57:44 -0600535
536static struct bus_type rbd_bus_type = {
537 .name = "rbd",
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700538 .bus_groups = rbd_bus_groups,
Alex Elderf0f8cef2012-01-29 13:57:44 -0600539};
540
541static void rbd_root_dev_release(struct device *dev)
542{
543}
544
545static struct device rbd_root_dev = {
546 .init_name = "rbd",
547 .release = rbd_root_dev_release,
548};
549
Alex Elder06ecc6c2012-11-01 10:17:15 -0500550static __printf(2, 3)
551void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...)
552{
553 struct va_format vaf;
554 va_list args;
555
556 va_start(args, fmt);
557 vaf.fmt = fmt;
558 vaf.va = &args;
559
560 if (!rbd_dev)
561 printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf);
562 else if (rbd_dev->disk)
563 printk(KERN_WARNING "%s: %s: %pV\n",
564 RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf);
565 else if (rbd_dev->spec && rbd_dev->spec->image_name)
566 printk(KERN_WARNING "%s: image %s: %pV\n",
567 RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf);
568 else if (rbd_dev->spec && rbd_dev->spec->image_id)
569 printk(KERN_WARNING "%s: id %s: %pV\n",
570 RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf);
571 else /* punt */
572 printk(KERN_WARNING "%s: rbd_dev %p: %pV\n",
573 RBD_DRV_NAME, rbd_dev, &vaf);
574 va_end(args);
575}
576
Alex Elderaafb2302012-09-06 16:00:54 -0500577#ifdef RBD_DEBUG
578#define rbd_assert(expr) \
579 if (unlikely(!(expr))) { \
580 printk(KERN_ERR "\nAssertion failure in %s() " \
581 "at line %d:\n\n" \
582 "\trbd_assert(%s);\n\n", \
583 __func__, __LINE__, #expr); \
584 BUG(); \
585 }
586#else /* !RBD_DEBUG */
587# define rbd_assert(expr) ((void) 0)
588#endif /* !RBD_DEBUG */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800589
Ilya Dryomov27617132015-07-16 17:36:11 +0300590static void rbd_osd_copyup_callback(struct rbd_obj_request *obj_request);
Alex Elderb454e362013-04-19 15:34:50 -0500591static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request);
Alex Elder05a46af2013-04-26 15:44:36 -0500592static void rbd_img_parent_read(struct rbd_obj_request *obj_request);
593static void rbd_dev_remove_parent(struct rbd_device *rbd_dev);
Alex Elder8b3e1a52013-01-24 16:13:36 -0600594
Alex Eldercc4a38bd2013-04-30 00:44:33 -0500595static int rbd_dev_refresh(struct rbd_device *rbd_dev);
Alex Elder2df3fac2013-05-06 09:51:30 -0500596static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev);
Ilya Dryomova720ae02014-07-23 17:11:19 +0400597static int rbd_dev_header_info(struct rbd_device *rbd_dev);
Ilya Dryomove8f59b52014-07-24 10:42:13 +0400598static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev);
Alex Elder54cac612013-04-30 00:44:33 -0500599static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
600 u64 snap_id);
Alex Elder2ad3d712013-04-30 00:44:33 -0500601static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
602 u8 *order, u64 *snap_size);
603static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
604 u64 *snap_features);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700605
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700606static int rbd_open(struct block_device *bdev, fmode_t mode)
607{
Alex Elderf0f8cef2012-01-29 13:57:44 -0600608 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
Alex Elderb82d1672013-01-14 12:43:31 -0600609 bool removing = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700610
Alex Elderf84344f2012-08-31 17:29:51 -0500611 if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700612 return -EROFS;
613
Alex Eldera14ea262013-02-05 13:23:12 -0600614 spin_lock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600615 if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags))
616 removing = true;
617 else
618 rbd_dev->open_count++;
Alex Eldera14ea262013-02-05 13:23:12 -0600619 spin_unlock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600620 if (removing)
621 return -ENOENT;
622
Alex Elderc3e946c2012-11-16 09:29:16 -0600623 (void) get_device(&rbd_dev->dev);
Alex Elder340c7a22012-08-10 13:12:07 -0700624
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700625 return 0;
626}
627
Al Virodb2a1442013-05-05 21:52:57 -0400628static void rbd_release(struct gendisk *disk, fmode_t mode)
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800629{
630 struct rbd_device *rbd_dev = disk->private_data;
Alex Elderb82d1672013-01-14 12:43:31 -0600631 unsigned long open_count_before;
632
Alex Eldera14ea262013-02-05 13:23:12 -0600633 spin_lock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600634 open_count_before = rbd_dev->open_count--;
Alex Eldera14ea262013-02-05 13:23:12 -0600635 spin_unlock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600636 rbd_assert(open_count_before > 0);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800637
Alex Elderc3e946c2012-11-16 09:29:16 -0600638 put_device(&rbd_dev->dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800639}
640
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800641static int rbd_ioctl_set_ro(struct rbd_device *rbd_dev, unsigned long arg)
642{
Ilya Dryomov1de797b2017-10-12 12:35:19 +0200643 int ro;
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800644
Ilya Dryomov1de797b2017-10-12 12:35:19 +0200645 if (get_user(ro, (int __user *)arg))
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800646 return -EFAULT;
647
Ilya Dryomov1de797b2017-10-12 12:35:19 +0200648 /* Snapshots can't be marked read-write */
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800649 if (rbd_dev->spec->snap_id != CEPH_NOSNAP && !ro)
650 return -EROFS;
651
Ilya Dryomov1de797b2017-10-12 12:35:19 +0200652 /* Let blkdev_roset() handle it */
653 return -ENOTTY;
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800654}
655
656static int rbd_ioctl(struct block_device *bdev, fmode_t mode,
657 unsigned int cmd, unsigned long arg)
658{
659 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
Ilya Dryomov1de797b2017-10-12 12:35:19 +0200660 int ret;
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800661
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800662 switch (cmd) {
663 case BLKROSET:
664 ret = rbd_ioctl_set_ro(rbd_dev, arg);
665 break;
666 default:
667 ret = -ENOTTY;
668 }
669
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800670 return ret;
671}
672
673#ifdef CONFIG_COMPAT
674static int rbd_compat_ioctl(struct block_device *bdev, fmode_t mode,
675 unsigned int cmd, unsigned long arg)
676{
677 return rbd_ioctl(bdev, mode, cmd, arg);
678}
679#endif /* CONFIG_COMPAT */
680
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700681static const struct block_device_operations rbd_bd_ops = {
682 .owner = THIS_MODULE,
683 .open = rbd_open,
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800684 .release = rbd_release,
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800685 .ioctl = rbd_ioctl,
686#ifdef CONFIG_COMPAT
687 .compat_ioctl = rbd_compat_ioctl,
688#endif
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700689};
690
691/*
Alex Elder7262cfc2013-05-16 15:04:20 -0500692 * Initialize an rbd client instance. Success or not, this function
Alex Eldercfbf6372013-05-31 17:40:45 -0500693 * consumes ceph_opts. Caller holds client_mutex.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700694 */
Alex Elderf8c38922012-08-10 13:12:07 -0700695static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700696{
697 struct rbd_client *rbdc;
698 int ret = -ENOMEM;
699
Alex Elder37206ee2013-02-20 17:32:08 -0600700 dout("%s:\n", __func__);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700701 rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL);
702 if (!rbdc)
703 goto out_opt;
704
705 kref_init(&rbdc->kref);
706 INIT_LIST_HEAD(&rbdc->node);
707
Ilya Dryomov74da4a0f2017-03-03 18:16:07 +0100708 rbdc->client = ceph_create_client(ceph_opts, rbdc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700709 if (IS_ERR(rbdc->client))
Alex Elder08f75462013-05-29 11:19:00 -0500710 goto out_rbdc;
Alex Elder43ae4702012-07-03 16:01:18 -0500711 ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700712
713 ret = ceph_open_session(rbdc->client);
714 if (ret < 0)
Alex Elder08f75462013-05-29 11:19:00 -0500715 goto out_client;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700716
Alex Elder432b8582012-01-29 13:57:44 -0600717 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700718 list_add_tail(&rbdc->node, &rbd_client_list);
Alex Elder432b8582012-01-29 13:57:44 -0600719 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700720
Alex Elder37206ee2013-02-20 17:32:08 -0600721 dout("%s: rbdc %p\n", __func__, rbdc);
Alex Elderbc534d82012-01-29 13:57:44 -0600722
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700723 return rbdc;
Alex Elder08f75462013-05-29 11:19:00 -0500724out_client:
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700725 ceph_destroy_client(rbdc->client);
Alex Elder08f75462013-05-29 11:19:00 -0500726out_rbdc:
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700727 kfree(rbdc);
728out_opt:
Alex Elder43ae4702012-07-03 16:01:18 -0500729 if (ceph_opts)
730 ceph_destroy_options(ceph_opts);
Alex Elder37206ee2013-02-20 17:32:08 -0600731 dout("%s: error %d\n", __func__, ret);
732
Vasiliy Kulikov28f259b2010-09-26 12:59:37 +0400733 return ERR_PTR(ret);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700734}
735
Alex Elder2f82ee52012-10-30 19:40:33 -0500736static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc)
737{
738 kref_get(&rbdc->kref);
739
740 return rbdc;
741}
742
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700743/*
Alex Elder1f7ba332012-08-10 13:12:07 -0700744 * Find a ceph client with specific addr and configuration. If
745 * found, bump its reference count.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700746 */
Alex Elder1f7ba332012-08-10 13:12:07 -0700747static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700748{
749 struct rbd_client *client_node;
Alex Elder1f7ba332012-08-10 13:12:07 -0700750 bool found = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700751
Alex Elder43ae4702012-07-03 16:01:18 -0500752 if (ceph_opts->flags & CEPH_OPT_NOSHARE)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700753 return NULL;
754
Alex Elder1f7ba332012-08-10 13:12:07 -0700755 spin_lock(&rbd_client_list_lock);
756 list_for_each_entry(client_node, &rbd_client_list, node) {
757 if (!ceph_compare_options(ceph_opts, client_node->client)) {
Alex Elder2f82ee52012-10-30 19:40:33 -0500758 __rbd_get_client(client_node);
759
Alex Elder1f7ba332012-08-10 13:12:07 -0700760 found = true;
761 break;
762 }
763 }
764 spin_unlock(&rbd_client_list_lock);
765
766 return found ? client_node : NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700767}
768
769/*
Ilya Dryomov210c1042015-06-22 13:24:48 +0300770 * (Per device) rbd map options
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700771 */
772enum {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300773 Opt_queue_depth,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700774 Opt_last_int,
775 /* int args above */
776 Opt_last_string,
777 /* string args above */
Alex Eldercc0538b2012-08-10 13:12:07 -0700778 Opt_read_only,
779 Opt_read_write,
Ilya Dryomov80de1912016-09-20 14:23:17 +0200780 Opt_lock_on_read,
Ilya Dryomove010dd02017-04-13 12:17:39 +0200781 Opt_exclusive,
Ilya Dryomov210c1042015-06-22 13:24:48 +0300782 Opt_err
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700783};
784
Alex Elder43ae4702012-07-03 16:01:18 -0500785static match_table_t rbd_opts_tokens = {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300786 {Opt_queue_depth, "queue_depth=%d"},
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700787 /* int args above */
788 /* string args above */
Alex Elderbe466c12012-10-22 11:31:26 -0500789 {Opt_read_only, "read_only"},
Alex Eldercc0538b2012-08-10 13:12:07 -0700790 {Opt_read_only, "ro"}, /* Alternate spelling */
791 {Opt_read_write, "read_write"},
792 {Opt_read_write, "rw"}, /* Alternate spelling */
Ilya Dryomov80de1912016-09-20 14:23:17 +0200793 {Opt_lock_on_read, "lock_on_read"},
Ilya Dryomove010dd02017-04-13 12:17:39 +0200794 {Opt_exclusive, "exclusive"},
Ilya Dryomov210c1042015-06-22 13:24:48 +0300795 {Opt_err, NULL}
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700796};
797
Alex Elder98571b52013-01-20 14:44:42 -0600798struct rbd_options {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300799 int queue_depth;
Alex Elder98571b52013-01-20 14:44:42 -0600800 bool read_only;
Ilya Dryomov80de1912016-09-20 14:23:17 +0200801 bool lock_on_read;
Ilya Dryomove010dd02017-04-13 12:17:39 +0200802 bool exclusive;
Alex Elder98571b52013-01-20 14:44:42 -0600803};
804
Ilya Dryomovb5584182015-06-23 16:21:19 +0300805#define RBD_QUEUE_DEPTH_DEFAULT BLKDEV_MAX_RQ
Alex Elder98571b52013-01-20 14:44:42 -0600806#define RBD_READ_ONLY_DEFAULT false
Ilya Dryomov80de1912016-09-20 14:23:17 +0200807#define RBD_LOCK_ON_READ_DEFAULT false
Ilya Dryomove010dd02017-04-13 12:17:39 +0200808#define RBD_EXCLUSIVE_DEFAULT false
Alex Elder98571b52013-01-20 14:44:42 -0600809
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700810static int parse_rbd_opts_token(char *c, void *private)
811{
Alex Elder43ae4702012-07-03 16:01:18 -0500812 struct rbd_options *rbd_opts = private;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700813 substring_t argstr[MAX_OPT_ARGS];
814 int token, intval, ret;
815
Alex Elder43ae4702012-07-03 16:01:18 -0500816 token = match_token(c, rbd_opts_tokens, argstr);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700817 if (token < Opt_last_int) {
818 ret = match_int(&argstr[0], &intval);
819 if (ret < 0) {
Ilya Dryomov210c1042015-06-22 13:24:48 +0300820 pr_err("bad mount option arg (not int) at '%s'\n", c);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700821 return ret;
822 }
823 dout("got int token %d val %d\n", token, intval);
824 } else if (token > Opt_last_int && token < Opt_last_string) {
Ilya Dryomov210c1042015-06-22 13:24:48 +0300825 dout("got string token %d val %s\n", token, argstr[0].from);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700826 } else {
827 dout("got token %d\n", token);
828 }
829
830 switch (token) {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300831 case Opt_queue_depth:
832 if (intval < 1) {
833 pr_err("queue_depth out of range\n");
834 return -EINVAL;
835 }
836 rbd_opts->queue_depth = intval;
837 break;
Alex Eldercc0538b2012-08-10 13:12:07 -0700838 case Opt_read_only:
839 rbd_opts->read_only = true;
840 break;
841 case Opt_read_write:
842 rbd_opts->read_only = false;
843 break;
Ilya Dryomov80de1912016-09-20 14:23:17 +0200844 case Opt_lock_on_read:
845 rbd_opts->lock_on_read = true;
846 break;
Ilya Dryomove010dd02017-04-13 12:17:39 +0200847 case Opt_exclusive:
848 rbd_opts->exclusive = true;
849 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700850 default:
Ilya Dryomov210c1042015-06-22 13:24:48 +0300851 /* libceph prints "bad option" msg */
852 return -EINVAL;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700853 }
Ilya Dryomov210c1042015-06-22 13:24:48 +0300854
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700855 return 0;
856}
857
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800858static char* obj_op_name(enum obj_operation_type op_type)
859{
860 switch (op_type) {
861 case OBJ_OP_READ:
862 return "read";
863 case OBJ_OP_WRITE:
864 return "write";
Guangliang Zhao90e98c52014-04-01 22:22:16 +0800865 case OBJ_OP_DISCARD:
866 return "discard";
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800867 default:
868 return "???";
869 }
870}
871
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700872/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700873 * Get a ceph client with specific addr and configuration, if one does
Alex Elder7262cfc2013-05-16 15:04:20 -0500874 * not exist create it. Either way, ceph_opts is consumed by this
875 * function.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700876 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500877static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700878{
Alex Elderf8c38922012-08-10 13:12:07 -0700879 struct rbd_client *rbdc;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700880
Alex Eldercfbf6372013-05-31 17:40:45 -0500881 mutex_lock_nested(&client_mutex, SINGLE_DEPTH_NESTING);
Alex Elder1f7ba332012-08-10 13:12:07 -0700882 rbdc = rbd_client_find(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500883 if (rbdc) /* using an existing client */
Alex Elder43ae4702012-07-03 16:01:18 -0500884 ceph_destroy_options(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500885 else
Alex Elderf8c38922012-08-10 13:12:07 -0700886 rbdc = rbd_client_create(ceph_opts);
Alex Eldercfbf6372013-05-31 17:40:45 -0500887 mutex_unlock(&client_mutex);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700888
Alex Elder9d3997f2012-10-25 23:34:42 -0500889 return rbdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700890}
891
892/*
893 * Destroy ceph client
Alex Elderd23a4b32012-01-29 13:57:43 -0600894 *
Alex Elder432b8582012-01-29 13:57:44 -0600895 * Caller must hold rbd_client_list_lock.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700896 */
897static void rbd_client_release(struct kref *kref)
898{
899 struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
900
Alex Elder37206ee2013-02-20 17:32:08 -0600901 dout("%s: rbdc %p\n", __func__, rbdc);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500902 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700903 list_del(&rbdc->node);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500904 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700905
906 ceph_destroy_client(rbdc->client);
907 kfree(rbdc);
908}
909
910/*
911 * Drop reference to ceph client node. If it's not referenced anymore, release
912 * it.
913 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500914static void rbd_put_client(struct rbd_client *rbdc)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700915{
Alex Elderc53d5892012-10-25 23:34:42 -0500916 if (rbdc)
917 kref_put(&rbdc->kref, rbd_client_release);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700918}
919
Alex Eldera30b71b2012-07-10 20:30:11 -0500920static bool rbd_image_format_valid(u32 image_format)
921{
922 return image_format == 1 || image_format == 2;
923}
924
Alex Elder8e94af82012-07-25 09:32:40 -0500925static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
926{
Alex Elder103a1502012-08-02 11:29:45 -0500927 size_t size;
928 u32 snap_count;
929
930 /* The header has to start with the magic rbd header text */
931 if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT)))
932 return false;
933
Alex Elderdb2388b2012-10-20 22:17:27 -0500934 /* The bio layer requires at least sector-sized I/O */
935
936 if (ondisk->options.order < SECTOR_SHIFT)
937 return false;
938
939 /* If we use u64 in a few spots we may be able to loosen this */
940
941 if (ondisk->options.order > 8 * sizeof (int) - 1)
942 return false;
943
Alex Elder103a1502012-08-02 11:29:45 -0500944 /*
945 * The size of a snapshot header has to fit in a size_t, and
946 * that limits the number of snapshots.
947 */
948 snap_count = le32_to_cpu(ondisk->snap_count);
949 size = SIZE_MAX - sizeof (struct ceph_snap_context);
950 if (snap_count > size / sizeof (__le64))
951 return false;
952
953 /*
954 * Not only that, but the size of the entire the snapshot
955 * header must also be representable in a size_t.
956 */
957 size -= snap_count * sizeof (__le64);
958 if ((u64) size < le64_to_cpu(ondisk->snap_names_len))
959 return false;
960
961 return true;
Alex Elder8e94af82012-07-25 09:32:40 -0500962}
963
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700964/*
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +0100965 * returns the size of an object in the image
966 */
967static u32 rbd_obj_bytes(struct rbd_image_header *header)
968{
969 return 1U << header->obj_order;
970}
971
Ilya Dryomov263423f2017-01-25 18:16:22 +0100972static void rbd_init_layout(struct rbd_device *rbd_dev)
973{
974 if (rbd_dev->header.stripe_unit == 0 ||
975 rbd_dev->header.stripe_count == 0) {
976 rbd_dev->header.stripe_unit = rbd_obj_bytes(&rbd_dev->header);
977 rbd_dev->header.stripe_count = 1;
978 }
979
980 rbd_dev->layout.stripe_unit = rbd_dev->header.stripe_unit;
981 rbd_dev->layout.stripe_count = rbd_dev->header.stripe_count;
982 rbd_dev->layout.object_size = rbd_obj_bytes(&rbd_dev->header);
Ilya Dryomov7e973322017-01-25 18:16:22 +0100983 rbd_dev->layout.pool_id = rbd_dev->header.data_pool_id == CEPH_NOPOOL ?
984 rbd_dev->spec->pool_id : rbd_dev->header.data_pool_id;
Ilya Dryomov263423f2017-01-25 18:16:22 +0100985 RCU_INIT_POINTER(rbd_dev->layout.pool_ns, NULL);
986}
987
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +0100988/*
Alex Elderbb23e372013-05-06 09:51:29 -0500989 * Fill an rbd image header with information from the given format 1
990 * on-disk header.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700991 */
Alex Elder662518b2013-05-06 09:51:29 -0500992static int rbd_header_from_disk(struct rbd_device *rbd_dev,
Alex Elder4156d992012-08-02 11:29:46 -0500993 struct rbd_image_header_ondisk *ondisk)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700994{
Alex Elder662518b2013-05-06 09:51:29 -0500995 struct rbd_image_header *header = &rbd_dev->header;
Alex Elderbb23e372013-05-06 09:51:29 -0500996 bool first_time = header->object_prefix == NULL;
997 struct ceph_snap_context *snapc;
998 char *object_prefix = NULL;
999 char *snap_names = NULL;
1000 u64 *snap_sizes = NULL;
Alex Elderccece232012-07-10 20:30:10 -05001001 u32 snap_count;
Alex Elderbb23e372013-05-06 09:51:29 -05001002 int ret = -ENOMEM;
Alex Elder621901d2012-08-23 23:22:06 -05001003 u32 i;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001004
Alex Elderbb23e372013-05-06 09:51:29 -05001005 /* Allocate this now to avoid having to handle failure below */
1006
1007 if (first_time) {
Ilya Dryomov848d7962017-01-25 18:16:21 +01001008 object_prefix = kstrndup(ondisk->object_prefix,
1009 sizeof(ondisk->object_prefix),
1010 GFP_KERNEL);
Alex Elderbb23e372013-05-06 09:51:29 -05001011 if (!object_prefix)
1012 return -ENOMEM;
Alex Elderbb23e372013-05-06 09:51:29 -05001013 }
1014
1015 /* Allocate the snapshot context and fill it in */
Alex Elder6a523252012-07-19 17:12:59 -05001016
Alex Elder103a1502012-08-02 11:29:45 -05001017 snap_count = le32_to_cpu(ondisk->snap_count);
Alex Elderbb23e372013-05-06 09:51:29 -05001018 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
1019 if (!snapc)
1020 goto out_err;
1021 snapc->seq = le64_to_cpu(ondisk->snap_seq);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001022 if (snap_count) {
Alex Elderbb23e372013-05-06 09:51:29 -05001023 struct rbd_image_snap_ondisk *snaps;
Alex Elderf785cc12012-08-23 23:22:06 -05001024 u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len);
1025
Alex Elderbb23e372013-05-06 09:51:29 -05001026 /* We'll keep a copy of the snapshot names... */
Alex Elder621901d2012-08-23 23:22:06 -05001027
Alex Elderbb23e372013-05-06 09:51:29 -05001028 if (snap_names_len > (u64)SIZE_MAX)
1029 goto out_2big;
1030 snap_names = kmalloc(snap_names_len, GFP_KERNEL);
1031 if (!snap_names)
Alex Elder6a523252012-07-19 17:12:59 -05001032 goto out_err;
Alex Elderbb23e372013-05-06 09:51:29 -05001033
1034 /* ...as well as the array of their sizes. */
Markus Elfring88a25a52016-09-11 12:21:25 +02001035 snap_sizes = kmalloc_array(snap_count,
1036 sizeof(*header->snap_sizes),
1037 GFP_KERNEL);
Alex Elderbb23e372013-05-06 09:51:29 -05001038 if (!snap_sizes)
1039 goto out_err;
1040
Alex Elderf785cc12012-08-23 23:22:06 -05001041 /*
Alex Elderbb23e372013-05-06 09:51:29 -05001042 * Copy the names, and fill in each snapshot's id
1043 * and size.
1044 *
Alex Elder99a41eb2013-05-06 09:51:30 -05001045 * Note that rbd_dev_v1_header_info() guarantees the
Alex Elderbb23e372013-05-06 09:51:29 -05001046 * ondisk buffer we're working with has
Alex Elderf785cc12012-08-23 23:22:06 -05001047 * snap_names_len bytes beyond the end of the
1048 * snapshot id array, this memcpy() is safe.
1049 */
Alex Elderbb23e372013-05-06 09:51:29 -05001050 memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len);
1051 snaps = ondisk->snaps;
1052 for (i = 0; i < snap_count; i++) {
1053 snapc->snaps[i] = le64_to_cpu(snaps[i].id);
1054 snap_sizes[i] = le64_to_cpu(snaps[i].image_size);
1055 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001056 }
Alex Elder849b4262012-07-09 21:04:24 -05001057
Alex Elderbb23e372013-05-06 09:51:29 -05001058 /* We won't fail any more, fill in the header */
Alex Elder6a523252012-07-19 17:12:59 -05001059
Alex Elderbb23e372013-05-06 09:51:29 -05001060 if (first_time) {
1061 header->object_prefix = object_prefix;
1062 header->obj_order = ondisk->options.order;
Ilya Dryomov263423f2017-01-25 18:16:22 +01001063 rbd_init_layout(rbd_dev);
Alex Elder662518b2013-05-06 09:51:29 -05001064 } else {
1065 ceph_put_snap_context(header->snapc);
1066 kfree(header->snap_names);
1067 kfree(header->snap_sizes);
Alex Elderbb23e372013-05-06 09:51:29 -05001068 }
1069
1070 /* The remaining fields always get updated (when we refresh) */
Alex Elder621901d2012-08-23 23:22:06 -05001071
Alex Elderf84344f2012-08-31 17:29:51 -05001072 header->image_size = le64_to_cpu(ondisk->image_size);
Alex Elderbb23e372013-05-06 09:51:29 -05001073 header->snapc = snapc;
1074 header->snap_names = snap_names;
1075 header->snap_sizes = snap_sizes;
Alex Elder468521c2013-04-26 09:43:47 -05001076
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001077 return 0;
Alex Elderbb23e372013-05-06 09:51:29 -05001078out_2big:
1079 ret = -EIO;
Alex Elder6a523252012-07-19 17:12:59 -05001080out_err:
Alex Elderbb23e372013-05-06 09:51:29 -05001081 kfree(snap_sizes);
1082 kfree(snap_names);
1083 ceph_put_snap_context(snapc);
1084 kfree(object_prefix);
Alex Elderccece232012-07-10 20:30:10 -05001085
Alex Elderbb23e372013-05-06 09:51:29 -05001086 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001087}
1088
Alex Elder9682fc62013-04-30 00:44:33 -05001089static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which)
1090{
1091 const char *snap_name;
1092
1093 rbd_assert(which < rbd_dev->header.snapc->num_snaps);
1094
1095 /* Skip over names until we find the one we are looking for */
1096
1097 snap_name = rbd_dev->header.snap_names;
1098 while (which--)
1099 snap_name += strlen(snap_name) + 1;
1100
1101 return kstrdup(snap_name, GFP_KERNEL);
1102}
1103
Alex Elder30d1cff2013-05-01 12:43:03 -05001104/*
1105 * Snapshot id comparison function for use with qsort()/bsearch().
1106 * Note that result is for snapshots in *descending* order.
1107 */
1108static int snapid_compare_reverse(const void *s1, const void *s2)
1109{
1110 u64 snap_id1 = *(u64 *)s1;
1111 u64 snap_id2 = *(u64 *)s2;
1112
1113 if (snap_id1 < snap_id2)
1114 return 1;
1115 return snap_id1 == snap_id2 ? 0 : -1;
1116}
1117
1118/*
1119 * Search a snapshot context to see if the given snapshot id is
1120 * present.
1121 *
1122 * Returns the position of the snapshot id in the array if it's found,
1123 * or BAD_SNAP_INDEX otherwise.
1124 *
1125 * Note: The snapshot array is in kept sorted (by the osd) in
1126 * reverse order, highest snapshot id first.
1127 */
Alex Elder9682fc62013-04-30 00:44:33 -05001128static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id)
1129{
1130 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
Alex Elder30d1cff2013-05-01 12:43:03 -05001131 u64 *found;
Alex Elder9682fc62013-04-30 00:44:33 -05001132
Alex Elder30d1cff2013-05-01 12:43:03 -05001133 found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps,
1134 sizeof (snap_id), snapid_compare_reverse);
Alex Elder9682fc62013-04-30 00:44:33 -05001135
Alex Elder30d1cff2013-05-01 12:43:03 -05001136 return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX;
Alex Elder9682fc62013-04-30 00:44:33 -05001137}
1138
Alex Elder2ad3d712013-04-30 00:44:33 -05001139static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev,
1140 u64 snap_id)
Alex Elder54cac612013-04-30 00:44:33 -05001141{
1142 u32 which;
Josh Durginda6a6b62013-09-04 17:57:31 -07001143 const char *snap_name;
Alex Elder54cac612013-04-30 00:44:33 -05001144
1145 which = rbd_dev_snap_index(rbd_dev, snap_id);
1146 if (which == BAD_SNAP_INDEX)
Josh Durginda6a6b62013-09-04 17:57:31 -07001147 return ERR_PTR(-ENOENT);
Alex Elder54cac612013-04-30 00:44:33 -05001148
Josh Durginda6a6b62013-09-04 17:57:31 -07001149 snap_name = _rbd_dev_v1_snap_name(rbd_dev, which);
1150 return snap_name ? snap_name : ERR_PTR(-ENOMEM);
Alex Elder54cac612013-04-30 00:44:33 -05001151}
1152
Alex Elder9e15b772012-10-30 19:40:33 -05001153static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id)
1154{
Alex Elder9e15b772012-10-30 19:40:33 -05001155 if (snap_id == CEPH_NOSNAP)
1156 return RBD_SNAP_HEAD_NAME;
1157
Alex Elder54cac612013-04-30 00:44:33 -05001158 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1159 if (rbd_dev->image_format == 1)
1160 return rbd_dev_v1_snap_name(rbd_dev, snap_id);
Alex Elder9e15b772012-10-30 19:40:33 -05001161
Alex Elder54cac612013-04-30 00:44:33 -05001162 return rbd_dev_v2_snap_name(rbd_dev, snap_id);
Alex Elder9e15b772012-10-30 19:40:33 -05001163}
1164
Alex Elder2ad3d712013-04-30 00:44:33 -05001165static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
1166 u64 *snap_size)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001167{
Alex Elder2ad3d712013-04-30 00:44:33 -05001168 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1169 if (snap_id == CEPH_NOSNAP) {
1170 *snap_size = rbd_dev->header.image_size;
1171 } else if (rbd_dev->image_format == 1) {
1172 u32 which;
Alex Elder00f1f362012-02-07 12:03:36 -06001173
Alex Elder2ad3d712013-04-30 00:44:33 -05001174 which = rbd_dev_snap_index(rbd_dev, snap_id);
1175 if (which == BAD_SNAP_INDEX)
1176 return -ENOENT;
Alex Elder00f1f362012-02-07 12:03:36 -06001177
Alex Elder2ad3d712013-04-30 00:44:33 -05001178 *snap_size = rbd_dev->header.snap_sizes[which];
1179 } else {
1180 u64 size = 0;
1181 int ret;
1182
1183 ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size);
1184 if (ret)
1185 return ret;
1186
1187 *snap_size = size;
1188 }
1189 return 0;
1190}
1191
1192static int rbd_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
1193 u64 *snap_features)
1194{
1195 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1196 if (snap_id == CEPH_NOSNAP) {
1197 *snap_features = rbd_dev->header.features;
1198 } else if (rbd_dev->image_format == 1) {
1199 *snap_features = 0; /* No features for format 1 */
1200 } else {
1201 u64 features = 0;
1202 int ret;
1203
1204 ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, &features);
1205 if (ret)
1206 return ret;
1207
1208 *snap_features = features;
1209 }
1210 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001211}
1212
Alex Elderd1cf5782013-04-27 09:59:30 -05001213static int rbd_dev_mapping_set(struct rbd_device *rbd_dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001214{
Alex Elder8f4b7d92013-05-06 07:40:30 -05001215 u64 snap_id = rbd_dev->spec->snap_id;
Alex Elder2ad3d712013-04-30 00:44:33 -05001216 u64 size = 0;
1217 u64 features = 0;
1218 int ret;
Alex Elder8b0241f2013-04-25 23:15:08 -05001219
Alex Elder2ad3d712013-04-30 00:44:33 -05001220 ret = rbd_snap_size(rbd_dev, snap_id, &size);
1221 if (ret)
1222 return ret;
1223 ret = rbd_snap_features(rbd_dev, snap_id, &features);
1224 if (ret)
1225 return ret;
1226
1227 rbd_dev->mapping.size = size;
1228 rbd_dev->mapping.features = features;
1229
Alex Elder8b0241f2013-04-25 23:15:08 -05001230 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001231}
1232
Alex Elderd1cf5782013-04-27 09:59:30 -05001233static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev)
1234{
1235 rbd_dev->mapping.size = 0;
1236 rbd_dev->mapping.features = 0;
Alex Elder200a6a82013-04-28 23:32:34 -05001237}
1238
Alex Elder65ccfe22012-08-09 10:33:26 -07001239static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset)
1240{
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +01001241 u64 segment_size = rbd_obj_bytes(&rbd_dev->header);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001242
Alex Elder65ccfe22012-08-09 10:33:26 -07001243 return offset & (segment_size - 1);
1244}
1245
1246static u64 rbd_segment_length(struct rbd_device *rbd_dev,
1247 u64 offset, u64 length)
1248{
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +01001249 u64 segment_size = rbd_obj_bytes(&rbd_dev->header);
Alex Elder65ccfe22012-08-09 10:33:26 -07001250
1251 offset &= segment_size - 1;
1252
Alex Elderaafb2302012-09-06 16:00:54 -05001253 rbd_assert(length <= U64_MAX - offset);
Alex Elder65ccfe22012-08-09 10:33:26 -07001254 if (offset + length > segment_size)
1255 length = segment_size - offset;
1256
1257 return length;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001258}
1259
1260/*
1261 * bio helpers
1262 */
1263
1264static void bio_chain_put(struct bio *chain)
1265{
1266 struct bio *tmp;
1267
1268 while (chain) {
1269 tmp = chain;
1270 chain = chain->bi_next;
1271 bio_put(tmp);
1272 }
1273}
1274
1275/*
1276 * zeros a bio chain, starting at specific offset
1277 */
1278static void zero_bio_chain(struct bio *chain, int start_ofs)
1279{
Kent Overstreet79886132013-11-23 17:19:00 -08001280 struct bio_vec bv;
1281 struct bvec_iter iter;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001282 unsigned long flags;
1283 void *buf;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001284 int pos = 0;
1285
1286 while (chain) {
Kent Overstreet79886132013-11-23 17:19:00 -08001287 bio_for_each_segment(bv, chain, iter) {
1288 if (pos + bv.bv_len > start_ofs) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001289 int remainder = max(start_ofs - pos, 0);
Kent Overstreet79886132013-11-23 17:19:00 -08001290 buf = bvec_kmap_irq(&bv, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001291 memset(buf + remainder, 0,
Kent Overstreet79886132013-11-23 17:19:00 -08001292 bv.bv_len - remainder);
1293 flush_dcache_page(bv.bv_page);
Dan Carpenter85b5aaa2010-10-11 21:15:11 +02001294 bvec_kunmap_irq(buf, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001295 }
Kent Overstreet79886132013-11-23 17:19:00 -08001296 pos += bv.bv_len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001297 }
1298
1299 chain = chain->bi_next;
1300 }
1301}
1302
1303/*
Alex Elderb9434c52013-04-19 15:34:50 -05001304 * similar to zero_bio_chain(), zeros data defined by a page array,
1305 * starting at the given byte offset from the start of the array and
1306 * continuing up to the given end offset. The pages array is
1307 * assumed to be big enough to hold all bytes up to the end.
1308 */
1309static void zero_pages(struct page **pages, u64 offset, u64 end)
1310{
1311 struct page **page = &pages[offset >> PAGE_SHIFT];
1312
1313 rbd_assert(end > offset);
1314 rbd_assert(end - offset <= (u64)SIZE_MAX);
1315 while (offset < end) {
1316 size_t page_offset;
1317 size_t length;
1318 unsigned long flags;
1319 void *kaddr;
1320
Geert Uytterhoeven491205a2013-05-13 20:35:37 -05001321 page_offset = offset & ~PAGE_MASK;
1322 length = min_t(size_t, PAGE_SIZE - page_offset, end - offset);
Alex Elderb9434c52013-04-19 15:34:50 -05001323 local_irq_save(flags);
1324 kaddr = kmap_atomic(*page);
1325 memset(kaddr + page_offset, 0, length);
Alex Eldere2156052013-05-22 20:54:25 -05001326 flush_dcache_page(*page);
Alex Elderb9434c52013-04-19 15:34:50 -05001327 kunmap_atomic(kaddr);
1328 local_irq_restore(flags);
1329
1330 offset += length;
1331 page++;
1332 }
1333}
1334
1335/*
Alex Elderf7760da2012-10-20 22:17:27 -05001336 * Clone a portion of a bio, starting at the given byte offset
1337 * and continuing for the number of bytes indicated.
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001338 */
Alex Elderf7760da2012-10-20 22:17:27 -05001339static struct bio *bio_clone_range(struct bio *bio_src,
1340 unsigned int offset,
1341 unsigned int len,
1342 gfp_t gfpmask)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001343{
Alex Elderf7760da2012-10-20 22:17:27 -05001344 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001345
NeilBrownf856dc32017-06-18 14:38:58 +10001346 bio = bio_clone_fast(bio_src, gfpmask, rbd_bio_clone);
Alex Elderf7760da2012-10-20 22:17:27 -05001347 if (!bio)
1348 return NULL; /* ENOMEM */
1349
Kent Overstreet5341a6272013-08-07 14:31:11 -07001350 bio_advance(bio, offset);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001351 bio->bi_iter.bi_size = len;
Alex Elder542582f2012-08-09 10:33:25 -07001352
Alex Elderf7760da2012-10-20 22:17:27 -05001353 return bio;
1354}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001355
Alex Elderf7760da2012-10-20 22:17:27 -05001356/*
1357 * Clone a portion of a bio chain, starting at the given byte offset
1358 * into the first bio in the source chain and continuing for the
1359 * number of bytes indicated. The result is another bio chain of
1360 * exactly the given length, or a null pointer on error.
1361 *
1362 * The bio_src and offset parameters are both in-out. On entry they
1363 * refer to the first source bio and the offset into that bio where
1364 * the start of data to be cloned is located.
1365 *
1366 * On return, bio_src is updated to refer to the bio in the source
1367 * chain that contains first un-cloned byte, and *offset will
1368 * contain the offset of that byte within that bio.
1369 */
1370static struct bio *bio_chain_clone_range(struct bio **bio_src,
1371 unsigned int *offset,
1372 unsigned int len,
1373 gfp_t gfpmask)
1374{
1375 struct bio *bi = *bio_src;
1376 unsigned int off = *offset;
1377 struct bio *chain = NULL;
1378 struct bio **end;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001379
Alex Elderf7760da2012-10-20 22:17:27 -05001380 /* Build up a chain of clone bios up to the limit */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001381
Kent Overstreet4f024f32013-10-11 15:44:27 -07001382 if (!bi || off >= bi->bi_iter.bi_size || !len)
Alex Elderf7760da2012-10-20 22:17:27 -05001383 return NULL; /* Nothing to clone */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001384
Alex Elderf7760da2012-10-20 22:17:27 -05001385 end = &chain;
1386 while (len) {
1387 unsigned int bi_size;
1388 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001389
Alex Elderf5400b72012-11-01 10:17:15 -05001390 if (!bi) {
1391 rbd_warn(NULL, "bio_chain exhausted with %u left", len);
Alex Elderf7760da2012-10-20 22:17:27 -05001392 goto out_err; /* EINVAL; ran out of bio's */
Alex Elderf5400b72012-11-01 10:17:15 -05001393 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07001394 bi_size = min_t(unsigned int, bi->bi_iter.bi_size - off, len);
Alex Elderf7760da2012-10-20 22:17:27 -05001395 bio = bio_clone_range(bi, off, bi_size, gfpmask);
1396 if (!bio)
1397 goto out_err; /* ENOMEM */
1398
1399 *end = bio;
1400 end = &bio->bi_next;
1401
1402 off += bi_size;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001403 if (off == bi->bi_iter.bi_size) {
Alex Elderf7760da2012-10-20 22:17:27 -05001404 bi = bi->bi_next;
1405 off = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001406 }
Alex Elderf7760da2012-10-20 22:17:27 -05001407 len -= bi_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001408 }
Alex Elderf7760da2012-10-20 22:17:27 -05001409 *bio_src = bi;
1410 *offset = off;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001411
Alex Elderf7760da2012-10-20 22:17:27 -05001412 return chain;
1413out_err:
1414 bio_chain_put(chain);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001415
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001416 return NULL;
1417}
1418
Alex Elder926f9b32013-02-11 12:33:24 -06001419/*
1420 * The default/initial value for all object request flags is 0. For
1421 * each flag, once its value is set to 1 it is never reset to 0
1422 * again.
1423 */
Alex Elder6365d332013-02-11 12:33:24 -06001424static void obj_request_img_data_set(struct rbd_obj_request *obj_request)
1425{
1426 if (test_and_set_bit(OBJ_REQ_IMG_DATA, &obj_request->flags)) {
Alex Elder6365d332013-02-11 12:33:24 -06001427 struct rbd_device *rbd_dev;
1428
Alex Elder57acbaa2013-02-11 12:33:24 -06001429 rbd_dev = obj_request->img_request->rbd_dev;
Ilya Dryomov9584d502014-07-11 12:11:20 +04001430 rbd_warn(rbd_dev, "obj_request %p already marked img_data",
Alex Elder6365d332013-02-11 12:33:24 -06001431 obj_request);
1432 }
1433}
1434
1435static bool obj_request_img_data_test(struct rbd_obj_request *obj_request)
1436{
1437 smp_mb();
1438 return test_bit(OBJ_REQ_IMG_DATA, &obj_request->flags) != 0;
1439}
1440
Alex Elder57acbaa2013-02-11 12:33:24 -06001441static void obj_request_done_set(struct rbd_obj_request *obj_request)
1442{
1443 if (test_and_set_bit(OBJ_REQ_DONE, &obj_request->flags)) {
1444 struct rbd_device *rbd_dev = NULL;
1445
1446 if (obj_request_img_data_test(obj_request))
1447 rbd_dev = obj_request->img_request->rbd_dev;
Ilya Dryomov9584d502014-07-11 12:11:20 +04001448 rbd_warn(rbd_dev, "obj_request %p already marked done",
Alex Elder57acbaa2013-02-11 12:33:24 -06001449 obj_request);
1450 }
1451}
1452
1453static bool obj_request_done_test(struct rbd_obj_request *obj_request)
1454{
1455 smp_mb();
1456 return test_bit(OBJ_REQ_DONE, &obj_request->flags) != 0;
1457}
1458
Alex Elder5679c592013-02-11 12:33:24 -06001459/*
1460 * This sets the KNOWN flag after (possibly) setting the EXISTS
1461 * flag. The latter is set based on the "exists" value provided.
1462 *
1463 * Note that for our purposes once an object exists it never goes
1464 * away again. It's possible that the response from two existence
1465 * checks are separated by the creation of the target object, and
1466 * the first ("doesn't exist") response arrives *after* the second
1467 * ("does exist"). In that case we ignore the second one.
1468 */
1469static void obj_request_existence_set(struct rbd_obj_request *obj_request,
1470 bool exists)
1471{
1472 if (exists)
1473 set_bit(OBJ_REQ_EXISTS, &obj_request->flags);
1474 set_bit(OBJ_REQ_KNOWN, &obj_request->flags);
1475 smp_mb();
1476}
1477
1478static bool obj_request_known_test(struct rbd_obj_request *obj_request)
1479{
1480 smp_mb();
1481 return test_bit(OBJ_REQ_KNOWN, &obj_request->flags) != 0;
1482}
1483
1484static bool obj_request_exists_test(struct rbd_obj_request *obj_request)
1485{
1486 smp_mb();
1487 return test_bit(OBJ_REQ_EXISTS, &obj_request->flags) != 0;
1488}
1489
Ilya Dryomov96385562014-06-10 13:53:29 +04001490static bool obj_request_overlaps_parent(struct rbd_obj_request *obj_request)
1491{
1492 struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev;
1493
1494 return obj_request->img_offset <
1495 round_up(rbd_dev->parent_overlap, rbd_obj_bytes(&rbd_dev->header));
1496}
1497
Alex Elderbf0d5f502012-11-22 00:00:08 -06001498static void rbd_obj_request_get(struct rbd_obj_request *obj_request)
1499{
Alex Elder37206ee2013-02-20 17:32:08 -06001500 dout("%s: obj %p (was %d)\n", __func__, obj_request,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01001501 kref_read(&obj_request->kref));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001502 kref_get(&obj_request->kref);
1503}
1504
1505static void rbd_obj_request_destroy(struct kref *kref);
1506static void rbd_obj_request_put(struct rbd_obj_request *obj_request)
1507{
1508 rbd_assert(obj_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001509 dout("%s: obj %p (was %d)\n", __func__, obj_request,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01001510 kref_read(&obj_request->kref));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001511 kref_put(&obj_request->kref, rbd_obj_request_destroy);
1512}
1513
Alex Elder0f2d5be2014-04-26 14:21:44 +04001514static void rbd_img_request_get(struct rbd_img_request *img_request)
1515{
1516 dout("%s: img %p (was %d)\n", __func__, img_request,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01001517 kref_read(&img_request->kref));
Alex Elder0f2d5be2014-04-26 14:21:44 +04001518 kref_get(&img_request->kref);
1519}
1520
Alex Eldere93f3152013-05-08 22:50:04 -05001521static bool img_request_child_test(struct rbd_img_request *img_request);
1522static void rbd_parent_request_destroy(struct kref *kref);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001523static void rbd_img_request_destroy(struct kref *kref);
1524static void rbd_img_request_put(struct rbd_img_request *img_request)
1525{
1526 rbd_assert(img_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001527 dout("%s: img %p (was %d)\n", __func__, img_request,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01001528 kref_read(&img_request->kref));
Alex Eldere93f3152013-05-08 22:50:04 -05001529 if (img_request_child_test(img_request))
1530 kref_put(&img_request->kref, rbd_parent_request_destroy);
1531 else
1532 kref_put(&img_request->kref, rbd_img_request_destroy);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001533}
1534
1535static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request,
1536 struct rbd_obj_request *obj_request)
1537{
Alex Elder25dcf952013-01-25 17:08:55 -06001538 rbd_assert(obj_request->img_request == NULL);
1539
Alex Elderb155e862013-04-15 14:50:37 -05001540 /* Image request now owns object's original reference */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001541 obj_request->img_request = img_request;
Alex Elder25dcf952013-01-25 17:08:55 -06001542 obj_request->which = img_request->obj_request_count;
Alex Elder6365d332013-02-11 12:33:24 -06001543 rbd_assert(!obj_request_img_data_test(obj_request));
1544 obj_request_img_data_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001545 rbd_assert(obj_request->which != BAD_WHICH);
Alex Elder25dcf952013-01-25 17:08:55 -06001546 img_request->obj_request_count++;
1547 list_add_tail(&obj_request->links, &img_request->obj_requests);
Alex Elder37206ee2013-02-20 17:32:08 -06001548 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1549 obj_request->which);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001550}
1551
1552static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request,
1553 struct rbd_obj_request *obj_request)
1554{
1555 rbd_assert(obj_request->which != BAD_WHICH);
Alex Elder25dcf952013-01-25 17:08:55 -06001556
Alex Elder37206ee2013-02-20 17:32:08 -06001557 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1558 obj_request->which);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001559 list_del(&obj_request->links);
Alex Elder25dcf952013-01-25 17:08:55 -06001560 rbd_assert(img_request->obj_request_count > 0);
1561 img_request->obj_request_count--;
1562 rbd_assert(obj_request->which == img_request->obj_request_count);
1563 obj_request->which = BAD_WHICH;
Alex Elder6365d332013-02-11 12:33:24 -06001564 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001565 rbd_assert(obj_request->img_request == img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001566 obj_request->img_request = NULL;
Alex Elder25dcf952013-01-25 17:08:55 -06001567 obj_request->callback = NULL;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001568 rbd_obj_request_put(obj_request);
1569}
1570
1571static bool obj_request_type_valid(enum obj_request_type type)
1572{
1573 switch (type) {
Alex Elder9969ebc2013-01-18 12:31:10 -06001574 case OBJ_REQUEST_NODATA:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001575 case OBJ_REQUEST_BIO:
Alex Elder788e2df2013-01-17 12:25:27 -06001576 case OBJ_REQUEST_PAGES:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001577 return true;
1578 default:
1579 return false;
1580 }
1581}
1582
Ilya Dryomov4a17dad2016-09-13 21:08:10 +02001583static void rbd_img_obj_callback(struct rbd_obj_request *obj_request);
1584
Ilya Dryomov980917f2016-09-12 18:59:42 +02001585static void rbd_obj_request_submit(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001586{
Ilya Dryomov980917f2016-09-12 18:59:42 +02001587 struct ceph_osd_request *osd_req = obj_request->osd_req;
1588
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01001589 dout("%s %p object_no %016llx %llu~%llu osd_req %p\n", __func__,
1590 obj_request, obj_request->object_no, obj_request->offset,
Ilya Dryomov67e2b652017-01-25 18:16:22 +01001591 obj_request->length, osd_req);
Ilya Dryomov4a17dad2016-09-13 21:08:10 +02001592 if (obj_request_img_data_test(obj_request)) {
1593 WARN_ON(obj_request->callback != rbd_img_obj_callback);
1594 rbd_img_request_get(obj_request->img_request);
1595 }
Ilya Dryomov980917f2016-09-12 18:59:42 +02001596 ceph_osdc_start_request(osd_req->r_osdc, osd_req, false);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001597}
1598
1599static void rbd_img_request_complete(struct rbd_img_request *img_request)
1600{
Alex Elder55f27e02013-04-10 12:34:25 -05001601
Alex Elder37206ee2013-02-20 17:32:08 -06001602 dout("%s: img %p\n", __func__, img_request);
Alex Elder55f27e02013-04-10 12:34:25 -05001603
1604 /*
1605 * If no error occurred, compute the aggregate transfer
1606 * count for the image request. We could instead use
1607 * atomic64_cmpxchg() to update it as each object request
1608 * completes; not clear which way is better off hand.
1609 */
1610 if (!img_request->result) {
1611 struct rbd_obj_request *obj_request;
1612 u64 xferred = 0;
1613
1614 for_each_obj_request(img_request, obj_request)
1615 xferred += obj_request->xferred;
1616 img_request->xferred = xferred;
1617 }
1618
Alex Elderbf0d5f502012-11-22 00:00:08 -06001619 if (img_request->callback)
1620 img_request->callback(img_request);
1621 else
1622 rbd_img_request_put(img_request);
1623}
1624
Alex Elder0c425242013-02-08 09:55:49 -06001625/*
1626 * The default/initial value for all image request flags is 0. Each
1627 * is conditionally set to 1 at image request initialization time
1628 * and currently never change thereafter.
1629 */
1630static void img_request_write_set(struct rbd_img_request *img_request)
1631{
1632 set_bit(IMG_REQ_WRITE, &img_request->flags);
1633 smp_mb();
1634}
1635
1636static bool img_request_write_test(struct rbd_img_request *img_request)
1637{
1638 smp_mb();
1639 return test_bit(IMG_REQ_WRITE, &img_request->flags) != 0;
1640}
1641
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001642/*
1643 * Set the discard flag when the img_request is an discard request
1644 */
1645static void img_request_discard_set(struct rbd_img_request *img_request)
1646{
1647 set_bit(IMG_REQ_DISCARD, &img_request->flags);
1648 smp_mb();
1649}
1650
1651static bool img_request_discard_test(struct rbd_img_request *img_request)
1652{
1653 smp_mb();
1654 return test_bit(IMG_REQ_DISCARD, &img_request->flags) != 0;
1655}
1656
Alex Elder9849e982013-01-24 16:13:36 -06001657static void img_request_child_set(struct rbd_img_request *img_request)
1658{
1659 set_bit(IMG_REQ_CHILD, &img_request->flags);
1660 smp_mb();
1661}
1662
Alex Eldere93f3152013-05-08 22:50:04 -05001663static void img_request_child_clear(struct rbd_img_request *img_request)
1664{
1665 clear_bit(IMG_REQ_CHILD, &img_request->flags);
1666 smp_mb();
1667}
1668
Alex Elder9849e982013-01-24 16:13:36 -06001669static bool img_request_child_test(struct rbd_img_request *img_request)
1670{
1671 smp_mb();
1672 return test_bit(IMG_REQ_CHILD, &img_request->flags) != 0;
1673}
1674
Alex Elderd0b2e942013-01-24 16:13:36 -06001675static void img_request_layered_set(struct rbd_img_request *img_request)
1676{
1677 set_bit(IMG_REQ_LAYERED, &img_request->flags);
1678 smp_mb();
1679}
1680
Alex Eldera2acd002013-05-08 22:50:04 -05001681static void img_request_layered_clear(struct rbd_img_request *img_request)
1682{
1683 clear_bit(IMG_REQ_LAYERED, &img_request->flags);
1684 smp_mb();
1685}
1686
Alex Elderd0b2e942013-01-24 16:13:36 -06001687static bool img_request_layered_test(struct rbd_img_request *img_request)
1688{
1689 smp_mb();
1690 return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0;
1691}
1692
Josh Durgin3b434a2a2014-04-04 17:32:15 -07001693static enum obj_operation_type
1694rbd_img_request_op_type(struct rbd_img_request *img_request)
1695{
1696 if (img_request_write_test(img_request))
1697 return OBJ_OP_WRITE;
1698 else if (img_request_discard_test(img_request))
1699 return OBJ_OP_DISCARD;
1700 else
1701 return OBJ_OP_READ;
1702}
1703
Alex Elder6e2a4502013-03-27 09:16:30 -05001704static void
1705rbd_img_obj_request_read_callback(struct rbd_obj_request *obj_request)
1706{
Alex Elderb9434c52013-04-19 15:34:50 -05001707 u64 xferred = obj_request->xferred;
1708 u64 length = obj_request->length;
1709
Alex Elder6e2a4502013-03-27 09:16:30 -05001710 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1711 obj_request, obj_request->img_request, obj_request->result,
Alex Elderb9434c52013-04-19 15:34:50 -05001712 xferred, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001713 /*
Josh Durgin17c1cc12013-08-26 17:55:38 -07001714 * ENOENT means a hole in the image. We zero-fill the entire
1715 * length of the request. A short read also implies zero-fill
1716 * to the end of the request. An error requires the whole
1717 * length of the request to be reported finished with an error
1718 * to the block layer. In each case we update the xferred
1719 * count to indicate the whole request was satisfied.
Alex Elder6e2a4502013-03-27 09:16:30 -05001720 */
Alex Elderb9434c52013-04-19 15:34:50 -05001721 rbd_assert(obj_request->type != OBJ_REQUEST_NODATA);
Alex Elder6e2a4502013-03-27 09:16:30 -05001722 if (obj_request->result == -ENOENT) {
Alex Elderb9434c52013-04-19 15:34:50 -05001723 if (obj_request->type == OBJ_REQUEST_BIO)
1724 zero_bio_chain(obj_request->bio_list, 0);
1725 else
1726 zero_pages(obj_request->pages, 0, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001727 obj_request->result = 0;
Alex Elderb9434c52013-04-19 15:34:50 -05001728 } else if (xferred < length && !obj_request->result) {
1729 if (obj_request->type == OBJ_REQUEST_BIO)
1730 zero_bio_chain(obj_request->bio_list, xferred);
1731 else
1732 zero_pages(obj_request->pages, xferred, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001733 }
Josh Durgin17c1cc12013-08-26 17:55:38 -07001734 obj_request->xferred = length;
Alex Elder6e2a4502013-03-27 09:16:30 -05001735 obj_request_done_set(obj_request);
1736}
1737
Alex Elderbf0d5f502012-11-22 00:00:08 -06001738static void rbd_obj_request_complete(struct rbd_obj_request *obj_request)
1739{
Alex Elder37206ee2013-02-20 17:32:08 -06001740 dout("%s: obj %p cb %p\n", __func__, obj_request,
1741 obj_request->callback);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001742 if (obj_request->callback)
1743 obj_request->callback(obj_request);
Alex Elder788e2df2013-01-17 12:25:27 -06001744 else
1745 complete_all(&obj_request->completion);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001746}
1747
Ilya Dryomov0dcc6852016-09-26 15:43:52 +02001748static void rbd_obj_request_error(struct rbd_obj_request *obj_request, int err)
1749{
1750 obj_request->result = err;
1751 obj_request->xferred = 0;
1752 /*
1753 * kludge - mirror rbd_obj_request_submit() to match a put in
1754 * rbd_img_obj_callback()
1755 */
1756 if (obj_request_img_data_test(obj_request)) {
1757 WARN_ON(obj_request->callback != rbd_img_obj_callback);
1758 rbd_img_request_get(obj_request->img_request);
1759 }
1760 obj_request_done_set(obj_request);
1761 rbd_obj_request_complete(obj_request);
1762}
1763
Alex Elderc47f9372013-02-26 14:23:07 -06001764static void rbd_osd_read_callback(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001765{
Alex Elder57acbaa2013-02-11 12:33:24 -06001766 struct rbd_img_request *img_request = NULL;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001767 struct rbd_device *rbd_dev = NULL;
Alex Elder57acbaa2013-02-11 12:33:24 -06001768 bool layered = false;
1769
1770 if (obj_request_img_data_test(obj_request)) {
1771 img_request = obj_request->img_request;
1772 layered = img_request && img_request_layered_test(img_request);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001773 rbd_dev = img_request->rbd_dev;
Alex Elder57acbaa2013-02-11 12:33:24 -06001774 }
Alex Elder8b3e1a52013-01-24 16:13:36 -06001775
1776 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1777 obj_request, img_request, obj_request->result,
1778 obj_request->xferred, obj_request->length);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001779 if (layered && obj_request->result == -ENOENT &&
1780 obj_request->img_offset < rbd_dev->parent_overlap)
Alex Elder8b3e1a52013-01-24 16:13:36 -06001781 rbd_img_parent_read(obj_request);
1782 else if (img_request)
Alex Elder6e2a4502013-03-27 09:16:30 -05001783 rbd_img_obj_request_read_callback(obj_request);
1784 else
1785 obj_request_done_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001786}
1787
Alex Elderc47f9372013-02-26 14:23:07 -06001788static void rbd_osd_write_callback(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001789{
Sage Weil1b83bef2013-02-25 16:11:12 -08001790 dout("%s: obj %p result %d %llu\n", __func__, obj_request,
1791 obj_request->result, obj_request->length);
1792 /*
Alex Elder8b3e1a52013-01-24 16:13:36 -06001793 * There is no such thing as a successful short write. Set
1794 * it to our originally-requested length.
Sage Weil1b83bef2013-02-25 16:11:12 -08001795 */
1796 obj_request->xferred = obj_request->length;
Alex Elder07741302013-02-05 23:41:50 -06001797 obj_request_done_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001798}
1799
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001800static void rbd_osd_discard_callback(struct rbd_obj_request *obj_request)
1801{
1802 dout("%s: obj %p result %d %llu\n", __func__, obj_request,
1803 obj_request->result, obj_request->length);
1804 /*
1805 * There is no such thing as a successful short discard. Set
1806 * it to our originally-requested length.
1807 */
1808 obj_request->xferred = obj_request->length;
Josh Durgind0265de2014-04-07 16:54:10 -07001809 /* discarding a non-existent object is not a problem */
1810 if (obj_request->result == -ENOENT)
1811 obj_request->result = 0;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001812 obj_request_done_set(obj_request);
1813}
1814
Alex Elderfbfab532013-02-08 09:55:48 -06001815/*
1816 * For a simple stat call there's nothing to do. We'll do more if
1817 * this is part of a write sequence for a layered image.
1818 */
Alex Elderc47f9372013-02-26 14:23:07 -06001819static void rbd_osd_stat_callback(struct rbd_obj_request *obj_request)
Alex Elderfbfab532013-02-08 09:55:48 -06001820{
Alex Elder37206ee2013-02-20 17:32:08 -06001821 dout("%s: obj %p\n", __func__, obj_request);
Alex Elderfbfab532013-02-08 09:55:48 -06001822 obj_request_done_set(obj_request);
1823}
1824
Ilya Dryomov27617132015-07-16 17:36:11 +03001825static void rbd_osd_call_callback(struct rbd_obj_request *obj_request)
1826{
1827 dout("%s: obj %p\n", __func__, obj_request);
1828
1829 if (obj_request_img_data_test(obj_request))
1830 rbd_osd_copyup_callback(obj_request);
1831 else
1832 obj_request_done_set(obj_request);
1833}
1834
Ilya Dryomov85e084f2016-04-28 16:07:24 +02001835static void rbd_osd_req_callback(struct ceph_osd_request *osd_req)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001836{
1837 struct rbd_obj_request *obj_request = osd_req->r_priv;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001838 u16 opcode;
1839
Ilya Dryomov85e084f2016-04-28 16:07:24 +02001840 dout("%s: osd_req %p\n", __func__, osd_req);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001841 rbd_assert(osd_req == obj_request->osd_req);
Alex Elder57acbaa2013-02-11 12:33:24 -06001842 if (obj_request_img_data_test(obj_request)) {
1843 rbd_assert(obj_request->img_request);
1844 rbd_assert(obj_request->which != BAD_WHICH);
1845 } else {
1846 rbd_assert(obj_request->which == BAD_WHICH);
1847 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06001848
Sage Weil1b83bef2013-02-25 16:11:12 -08001849 if (osd_req->r_result < 0)
1850 obj_request->result = osd_req->r_result;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001851
Alex Elderc47f9372013-02-26 14:23:07 -06001852 /*
1853 * We support a 64-bit length, but ultimately it has to be
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01001854 * passed to the block layer, which just supports a 32-bit
1855 * length field.
Alex Elderc47f9372013-02-26 14:23:07 -06001856 */
Yan, Zheng7665d852016-01-07 16:48:57 +08001857 obj_request->xferred = osd_req->r_ops[0].outdata_len;
Alex Elder8b3e1a52013-01-24 16:13:36 -06001858 rbd_assert(obj_request->xferred < (u64)UINT_MAX);
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001859
Alex Elder79528732013-04-03 21:32:51 -05001860 opcode = osd_req->r_ops[0].op;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001861 switch (opcode) {
1862 case CEPH_OSD_OP_READ:
Alex Elderc47f9372013-02-26 14:23:07 -06001863 rbd_osd_read_callback(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001864 break;
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001865 case CEPH_OSD_OP_SETALLOCHINT:
Ilya Dryomove30b7572015-10-07 17:27:17 +02001866 rbd_assert(osd_req->r_ops[1].op == CEPH_OSD_OP_WRITE ||
1867 osd_req->r_ops[1].op == CEPH_OSD_OP_WRITEFULL);
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001868 /* fall through */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001869 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +02001870 case CEPH_OSD_OP_WRITEFULL:
Alex Elderc47f9372013-02-26 14:23:07 -06001871 rbd_osd_write_callback(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001872 break;
Alex Elderfbfab532013-02-08 09:55:48 -06001873 case CEPH_OSD_OP_STAT:
Alex Elderc47f9372013-02-26 14:23:07 -06001874 rbd_osd_stat_callback(obj_request);
Alex Elderfbfab532013-02-08 09:55:48 -06001875 break;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001876 case CEPH_OSD_OP_DELETE:
1877 case CEPH_OSD_OP_TRUNCATE:
1878 case CEPH_OSD_OP_ZERO:
1879 rbd_osd_discard_callback(obj_request);
1880 break;
Alex Elder36be9a72013-01-19 00:30:28 -06001881 case CEPH_OSD_OP_CALL:
Ilya Dryomov27617132015-07-16 17:36:11 +03001882 rbd_osd_call_callback(obj_request);
1883 break;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001884 default:
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01001885 rbd_warn(NULL, "unexpected OSD op: object_no %016llx opcode %d",
1886 obj_request->object_no, opcode);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001887 break;
1888 }
1889
Alex Elder07741302013-02-05 23:41:50 -06001890 if (obj_request_done_test(obj_request))
Alex Elderbf0d5f502012-11-22 00:00:08 -06001891 rbd_obj_request_complete(obj_request);
1892}
1893
Alex Elder9d4df012013-04-19 15:34:50 -05001894static void rbd_osd_req_format_read(struct rbd_obj_request *obj_request)
Alex Elder430c28c2013-04-03 21:32:51 -05001895{
Alex Elder8c042b02013-04-03 01:28:58 -05001896 struct ceph_osd_request *osd_req = obj_request->osd_req;
Alex Elder430c28c2013-04-03 21:32:51 -05001897
Ilya Dryomov7c848832016-09-15 17:56:39 +02001898 rbd_assert(obj_request_img_data_test(obj_request));
1899 osd_req->r_snapid = obj_request->img_request->snap_id;
Alex Elder9d4df012013-04-19 15:34:50 -05001900}
1901
1902static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request)
1903{
Alex Elder9d4df012013-04-19 15:34:50 -05001904 struct ceph_osd_request *osd_req = obj_request->osd_req;
Alex Elder9d4df012013-04-19 15:34:50 -05001905
Deepa Dinamani1134e092017-05-08 15:59:19 -07001906 ktime_get_real_ts(&osd_req->r_mtime);
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001907 osd_req->r_data_offset = obj_request->offset;
Alex Elder430c28c2013-04-03 21:32:51 -05001908}
1909
Ilya Dryomovbc812072017-01-25 18:16:23 +01001910static struct ceph_osd_request *
1911__rbd_osd_req_create(struct rbd_device *rbd_dev,
1912 struct ceph_snap_context *snapc,
1913 int num_ops, unsigned int flags,
1914 struct rbd_obj_request *obj_request)
1915{
1916 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
1917 struct ceph_osd_request *req;
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01001918 const char *name_format = rbd_dev->image_format == 1 ?
1919 RBD_V1_DATA_FORMAT : RBD_V2_DATA_FORMAT;
Ilya Dryomovbc812072017-01-25 18:16:23 +01001920
1921 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, false, GFP_NOIO);
1922 if (!req)
1923 return NULL;
1924
1925 req->r_flags = flags;
1926 req->r_callback = rbd_osd_req_callback;
1927 req->r_priv = obj_request;
1928
1929 req->r_base_oloc.pool = rbd_dev->layout.pool_id;
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01001930 if (ceph_oid_aprintf(&req->r_base_oid, GFP_NOIO, name_format,
1931 rbd_dev->header.object_prefix, obj_request->object_no))
Ilya Dryomovbc812072017-01-25 18:16:23 +01001932 goto err_req;
1933
1934 if (ceph_osdc_alloc_messages(req, GFP_NOIO))
1935 goto err_req;
1936
1937 return req;
1938
1939err_req:
1940 ceph_osdc_put_request(req);
1941 return NULL;
1942}
1943
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001944/*
1945 * Create an osd request. A read request has one osd op (read).
1946 * A write request has either one (watch) or two (hint+write) osd ops.
1947 * (All rbd data writes are prefixed with an allocation hint op, but
1948 * technically osd watch is a write request, hence this distinction.)
1949 */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001950static struct ceph_osd_request *rbd_osd_req_create(
1951 struct rbd_device *rbd_dev,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08001952 enum obj_operation_type op_type,
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02001953 unsigned int num_ops,
Alex Elder430c28c2013-04-03 21:32:51 -05001954 struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001955{
Alex Elderbf0d5f502012-11-22 00:00:08 -06001956 struct ceph_snap_context *snapc = NULL;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001957
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001958 if (obj_request_img_data_test(obj_request) &&
1959 (op_type == OBJ_OP_DISCARD || op_type == OBJ_OP_WRITE)) {
Alex Elder6365d332013-02-11 12:33:24 -06001960 struct rbd_img_request *img_request = obj_request->img_request;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001961 if (op_type == OBJ_OP_WRITE) {
1962 rbd_assert(img_request_write_test(img_request));
1963 } else {
1964 rbd_assert(img_request_discard_test(img_request));
1965 }
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08001966 snapc = img_request->snapc;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001967 }
1968
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08001969 rbd_assert(num_ops == 1 || ((op_type == OBJ_OP_WRITE) && num_ops == 2));
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02001970
Ilya Dryomovbc812072017-01-25 18:16:23 +01001971 return __rbd_osd_req_create(rbd_dev, snapc, num_ops,
1972 (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD) ?
Ilya Dryomov54ea0042017-02-11 18:48:41 +01001973 CEPH_OSD_FLAG_WRITE : CEPH_OSD_FLAG_READ, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001974}
1975
Alex Elder0eefd472013-04-19 15:34:50 -05001976/*
Josh Durgind3246fb2014-04-07 16:49:21 -07001977 * Create a copyup osd request based on the information in the object
1978 * request supplied. A copyup request has two or three osd ops, a
1979 * copyup method call, potentially a hint op, and a write or truncate
1980 * or zero op.
Alex Elder0eefd472013-04-19 15:34:50 -05001981 */
1982static struct ceph_osd_request *
1983rbd_osd_req_create_copyup(struct rbd_obj_request *obj_request)
1984{
1985 struct rbd_img_request *img_request;
Josh Durgind3246fb2014-04-07 16:49:21 -07001986 int num_osd_ops = 3;
Alex Elder0eefd472013-04-19 15:34:50 -05001987
1988 rbd_assert(obj_request_img_data_test(obj_request));
1989 img_request = obj_request->img_request;
1990 rbd_assert(img_request);
Josh Durgind3246fb2014-04-07 16:49:21 -07001991 rbd_assert(img_request_write_test(img_request) ||
1992 img_request_discard_test(img_request));
Alex Elder0eefd472013-04-19 15:34:50 -05001993
Josh Durgind3246fb2014-04-07 16:49:21 -07001994 if (img_request_discard_test(img_request))
1995 num_osd_ops = 2;
1996
Ilya Dryomovbc812072017-01-25 18:16:23 +01001997 return __rbd_osd_req_create(img_request->rbd_dev,
1998 img_request->snapc, num_osd_ops,
Ilya Dryomov54ea0042017-02-11 18:48:41 +01001999 CEPH_OSD_FLAG_WRITE, obj_request);
Alex Elder0eefd472013-04-19 15:34:50 -05002000}
2001
Alex Elderbf0d5f502012-11-22 00:00:08 -06002002static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req)
2003{
2004 ceph_osdc_put_request(osd_req);
2005}
2006
Ilya Dryomov6c696d82017-01-25 18:16:23 +01002007static struct rbd_obj_request *
2008rbd_obj_request_create(enum obj_request_type type)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002009{
2010 struct rbd_obj_request *obj_request;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002011
2012 rbd_assert(obj_request_type_valid(type));
2013
Ilya Dryomov5a60e872015-06-24 17:24:33 +03002014 obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_NOIO);
Ilya Dryomov6c696d82017-01-25 18:16:23 +01002015 if (!obj_request)
Alex Elderf907ad52013-05-01 12:43:03 -05002016 return NULL;
Alex Elderf907ad52013-05-01 12:43:03 -05002017
Alex Elderbf0d5f502012-11-22 00:00:08 -06002018 obj_request->which = BAD_WHICH;
2019 obj_request->type = type;
2020 INIT_LIST_HEAD(&obj_request->links);
Alex Elder788e2df2013-01-17 12:25:27 -06002021 init_completion(&obj_request->completion);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002022 kref_init(&obj_request->kref);
2023
Ilya Dryomov67e2b652017-01-25 18:16:22 +01002024 dout("%s %p\n", __func__, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002025 return obj_request;
2026}
2027
2028static void rbd_obj_request_destroy(struct kref *kref)
2029{
2030 struct rbd_obj_request *obj_request;
2031
2032 obj_request = container_of(kref, struct rbd_obj_request, kref);
2033
Alex Elder37206ee2013-02-20 17:32:08 -06002034 dout("%s: obj %p\n", __func__, obj_request);
2035
Alex Elderbf0d5f502012-11-22 00:00:08 -06002036 rbd_assert(obj_request->img_request == NULL);
2037 rbd_assert(obj_request->which == BAD_WHICH);
2038
2039 if (obj_request->osd_req)
2040 rbd_osd_req_destroy(obj_request->osd_req);
2041
2042 rbd_assert(obj_request_type_valid(obj_request->type));
2043 switch (obj_request->type) {
Alex Elder9969ebc2013-01-18 12:31:10 -06002044 case OBJ_REQUEST_NODATA:
2045 break; /* Nothing to do */
Alex Elderbf0d5f502012-11-22 00:00:08 -06002046 case OBJ_REQUEST_BIO:
2047 if (obj_request->bio_list)
2048 bio_chain_put(obj_request->bio_list);
2049 break;
Alex Elder788e2df2013-01-17 12:25:27 -06002050 case OBJ_REQUEST_PAGES:
Ilya Dryomov04dc9232016-09-15 18:05:16 +02002051 /* img_data requests don't own their page array */
2052 if (obj_request->pages &&
2053 !obj_request_img_data_test(obj_request))
Alex Elder788e2df2013-01-17 12:25:27 -06002054 ceph_release_page_vector(obj_request->pages,
2055 obj_request->page_count);
2056 break;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002057 }
2058
Alex Elder868311b2013-05-01 12:43:03 -05002059 kmem_cache_free(rbd_obj_request_cache, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002060}
2061
Alex Elderfb65d2282013-05-08 22:50:04 -05002062/* It's OK to call this for a device with no parent */
2063
2064static void rbd_spec_put(struct rbd_spec *spec);
2065static void rbd_dev_unparent(struct rbd_device *rbd_dev)
2066{
2067 rbd_dev_remove_parent(rbd_dev);
2068 rbd_spec_put(rbd_dev->parent_spec);
2069 rbd_dev->parent_spec = NULL;
2070 rbd_dev->parent_overlap = 0;
2071}
2072
Alex Elderbf0d5f502012-11-22 00:00:08 -06002073/*
Alex Eldera2acd002013-05-08 22:50:04 -05002074 * Parent image reference counting is used to determine when an
2075 * image's parent fields can be safely torn down--after there are no
2076 * more in-flight requests to the parent image. When the last
2077 * reference is dropped, cleaning them up is safe.
2078 */
2079static void rbd_dev_parent_put(struct rbd_device *rbd_dev)
2080{
2081 int counter;
2082
2083 if (!rbd_dev->parent_spec)
2084 return;
2085
2086 counter = atomic_dec_return_safe(&rbd_dev->parent_ref);
2087 if (counter > 0)
2088 return;
2089
2090 /* Last reference; clean up parent data structures */
2091
2092 if (!counter)
2093 rbd_dev_unparent(rbd_dev);
2094 else
Ilya Dryomov9584d502014-07-11 12:11:20 +04002095 rbd_warn(rbd_dev, "parent reference underflow");
Alex Eldera2acd002013-05-08 22:50:04 -05002096}
2097
2098/*
2099 * If an image has a non-zero parent overlap, get a reference to its
2100 * parent.
2101 *
2102 * Returns true if the rbd device has a parent with a non-zero
2103 * overlap and a reference for it was successfully taken, or
2104 * false otherwise.
2105 */
2106static bool rbd_dev_parent_get(struct rbd_device *rbd_dev)
2107{
Ilya Dryomovae43e9d2015-01-19 18:13:43 +03002108 int counter = 0;
Alex Eldera2acd002013-05-08 22:50:04 -05002109
2110 if (!rbd_dev->parent_spec)
2111 return false;
2112
Ilya Dryomovae43e9d2015-01-19 18:13:43 +03002113 down_read(&rbd_dev->header_rwsem);
2114 if (rbd_dev->parent_overlap)
2115 counter = atomic_inc_return_safe(&rbd_dev->parent_ref);
2116 up_read(&rbd_dev->header_rwsem);
Alex Eldera2acd002013-05-08 22:50:04 -05002117
2118 if (counter < 0)
Ilya Dryomov9584d502014-07-11 12:11:20 +04002119 rbd_warn(rbd_dev, "parent reference overflow");
Alex Eldera2acd002013-05-08 22:50:04 -05002120
Ilya Dryomovae43e9d2015-01-19 18:13:43 +03002121 return counter > 0;
Alex Eldera2acd002013-05-08 22:50:04 -05002122}
2123
Alex Elderbf0d5f502012-11-22 00:00:08 -06002124/*
2125 * Caller is responsible for filling in the list of object requests
2126 * that comprises the image request, and the Linux request pointer
2127 * (if there is one).
2128 */
Alex Eldercc344fa2013-02-19 12:25:56 -06002129static struct rbd_img_request *rbd_img_request_create(
2130 struct rbd_device *rbd_dev,
Alex Elderbf0d5f502012-11-22 00:00:08 -06002131 u64 offset, u64 length,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002132 enum obj_operation_type op_type,
Josh Durgin4e752f02014-04-08 11:12:11 -07002133 struct ceph_snap_context *snapc)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002134{
2135 struct rbd_img_request *img_request;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002136
Ilya Dryomov7a716aa2014-08-05 11:25:54 +04002137 img_request = kmem_cache_alloc(rbd_img_request_cache, GFP_NOIO);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002138 if (!img_request)
2139 return NULL;
2140
Alex Elderbf0d5f502012-11-22 00:00:08 -06002141 img_request->rq = NULL;
2142 img_request->rbd_dev = rbd_dev;
2143 img_request->offset = offset;
2144 img_request->length = length;
Alex Elder0c425242013-02-08 09:55:49 -06002145 img_request->flags = 0;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002146 if (op_type == OBJ_OP_DISCARD) {
2147 img_request_discard_set(img_request);
2148 img_request->snapc = snapc;
2149 } else if (op_type == OBJ_OP_WRITE) {
Alex Elder0c425242013-02-08 09:55:49 -06002150 img_request_write_set(img_request);
Josh Durgin4e752f02014-04-08 11:12:11 -07002151 img_request->snapc = snapc;
Alex Elder0c425242013-02-08 09:55:49 -06002152 } else {
Alex Elderbf0d5f502012-11-22 00:00:08 -06002153 img_request->snap_id = rbd_dev->spec->snap_id;
Alex Elder0c425242013-02-08 09:55:49 -06002154 }
Alex Eldera2acd002013-05-08 22:50:04 -05002155 if (rbd_dev_parent_get(rbd_dev))
Alex Elderd0b2e942013-01-24 16:13:36 -06002156 img_request_layered_set(img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002157 spin_lock_init(&img_request->completion_lock);
2158 img_request->next_completion = 0;
2159 img_request->callback = NULL;
Alex Eldera5a337d2013-01-24 16:13:36 -06002160 img_request->result = 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002161 img_request->obj_request_count = 0;
2162 INIT_LIST_HEAD(&img_request->obj_requests);
2163 kref_init(&img_request->kref);
2164
Alex Elder37206ee2013-02-20 17:32:08 -06002165 dout("%s: rbd_dev %p %s %llu/%llu -> img %p\n", __func__, rbd_dev,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002166 obj_op_name(op_type), offset, length, img_request);
Alex Elder37206ee2013-02-20 17:32:08 -06002167
Alex Elderbf0d5f502012-11-22 00:00:08 -06002168 return img_request;
2169}
2170
2171static void rbd_img_request_destroy(struct kref *kref)
2172{
2173 struct rbd_img_request *img_request;
2174 struct rbd_obj_request *obj_request;
2175 struct rbd_obj_request *next_obj_request;
2176
2177 img_request = container_of(kref, struct rbd_img_request, kref);
2178
Alex Elder37206ee2013-02-20 17:32:08 -06002179 dout("%s: img %p\n", __func__, img_request);
2180
Alex Elderbf0d5f502012-11-22 00:00:08 -06002181 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
2182 rbd_img_obj_request_del(img_request, obj_request);
Alex Elder25dcf952013-01-25 17:08:55 -06002183 rbd_assert(img_request->obj_request_count == 0);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002184
Alex Eldera2acd002013-05-08 22:50:04 -05002185 if (img_request_layered_test(img_request)) {
2186 img_request_layered_clear(img_request);
2187 rbd_dev_parent_put(img_request->rbd_dev);
2188 }
2189
Josh Durginbef95452014-04-04 17:47:52 -07002190 if (img_request_write_test(img_request) ||
2191 img_request_discard_test(img_request))
Alex Elder812164f82013-04-30 00:44:32 -05002192 ceph_put_snap_context(img_request->snapc);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002193
Alex Elder1c2a9df2013-05-01 12:43:03 -05002194 kmem_cache_free(rbd_img_request_cache, img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002195}
2196
Alex Eldere93f3152013-05-08 22:50:04 -05002197static struct rbd_img_request *rbd_parent_request_create(
2198 struct rbd_obj_request *obj_request,
2199 u64 img_offset, u64 length)
2200{
2201 struct rbd_img_request *parent_request;
2202 struct rbd_device *rbd_dev;
2203
2204 rbd_assert(obj_request->img_request);
2205 rbd_dev = obj_request->img_request->rbd_dev;
2206
Josh Durgin4e752f02014-04-08 11:12:11 -07002207 parent_request = rbd_img_request_create(rbd_dev->parent, img_offset,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002208 length, OBJ_OP_READ, NULL);
Alex Eldere93f3152013-05-08 22:50:04 -05002209 if (!parent_request)
2210 return NULL;
2211
2212 img_request_child_set(parent_request);
2213 rbd_obj_request_get(obj_request);
2214 parent_request->obj_request = obj_request;
2215
2216 return parent_request;
2217}
2218
2219static void rbd_parent_request_destroy(struct kref *kref)
2220{
2221 struct rbd_img_request *parent_request;
2222 struct rbd_obj_request *orig_request;
2223
2224 parent_request = container_of(kref, struct rbd_img_request, kref);
2225 orig_request = parent_request->obj_request;
2226
2227 parent_request->obj_request = NULL;
2228 rbd_obj_request_put(orig_request);
2229 img_request_child_clear(parent_request);
2230
2231 rbd_img_request_destroy(kref);
2232}
2233
Alex Elder12178572013-02-08 09:55:49 -06002234static bool rbd_img_obj_end_request(struct rbd_obj_request *obj_request)
2235{
Alex Elder6365d332013-02-11 12:33:24 -06002236 struct rbd_img_request *img_request;
Alex Elder12178572013-02-08 09:55:49 -06002237 unsigned int xferred;
2238 int result;
Alex Elder8b3e1a52013-01-24 16:13:36 -06002239 bool more;
Alex Elder12178572013-02-08 09:55:49 -06002240
Alex Elder6365d332013-02-11 12:33:24 -06002241 rbd_assert(obj_request_img_data_test(obj_request));
2242 img_request = obj_request->img_request;
2243
Alex Elder12178572013-02-08 09:55:49 -06002244 rbd_assert(obj_request->xferred <= (u64)UINT_MAX);
2245 xferred = (unsigned int)obj_request->xferred;
2246 result = obj_request->result;
2247 if (result) {
2248 struct rbd_device *rbd_dev = img_request->rbd_dev;
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002249 enum obj_operation_type op_type;
2250
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002251 if (img_request_discard_test(img_request))
2252 op_type = OBJ_OP_DISCARD;
2253 else if (img_request_write_test(img_request))
2254 op_type = OBJ_OP_WRITE;
2255 else
2256 op_type = OBJ_OP_READ;
Alex Elder12178572013-02-08 09:55:49 -06002257
Ilya Dryomov9584d502014-07-11 12:11:20 +04002258 rbd_warn(rbd_dev, "%s %llx at %llx (%llx)",
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002259 obj_op_name(op_type), obj_request->length,
2260 obj_request->img_offset, obj_request->offset);
Ilya Dryomov9584d502014-07-11 12:11:20 +04002261 rbd_warn(rbd_dev, " result %d xferred %x",
Alex Elder12178572013-02-08 09:55:49 -06002262 result, xferred);
2263 if (!img_request->result)
2264 img_request->result = result;
Ilya Dryomov082a75d2015-04-25 15:56:15 +03002265 /*
2266 * Need to end I/O on the entire obj_request worth of
2267 * bytes in case of error.
2268 */
2269 xferred = obj_request->length;
Alex Elder12178572013-02-08 09:55:49 -06002270 }
2271
Alex Elder8b3e1a52013-01-24 16:13:36 -06002272 if (img_request_child_test(img_request)) {
2273 rbd_assert(img_request->obj_request != NULL);
2274 more = obj_request->which < img_request->obj_request_count - 1;
2275 } else {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002276 blk_status_t status = errno_to_blk_status(result);
2277
Alex Elder8b3e1a52013-01-24 16:13:36 -06002278 rbd_assert(img_request->rq != NULL);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01002279
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002280 more = blk_update_request(img_request->rq, status, xferred);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01002281 if (!more)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002282 __blk_mq_end_request(img_request->rq, status);
Alex Elder8b3e1a52013-01-24 16:13:36 -06002283 }
2284
2285 return more;
Alex Elder12178572013-02-08 09:55:49 -06002286}
2287
Alex Elder21692382013-04-05 01:27:12 -05002288static void rbd_img_obj_callback(struct rbd_obj_request *obj_request)
2289{
2290 struct rbd_img_request *img_request;
2291 u32 which = obj_request->which;
2292 bool more = true;
2293
Alex Elder6365d332013-02-11 12:33:24 -06002294 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elder21692382013-04-05 01:27:12 -05002295 img_request = obj_request->img_request;
2296
2297 dout("%s: img %p obj %p\n", __func__, img_request, obj_request);
2298 rbd_assert(img_request != NULL);
Alex Elder21692382013-04-05 01:27:12 -05002299 rbd_assert(img_request->obj_request_count > 0);
2300 rbd_assert(which != BAD_WHICH);
2301 rbd_assert(which < img_request->obj_request_count);
Alex Elder21692382013-04-05 01:27:12 -05002302
2303 spin_lock_irq(&img_request->completion_lock);
2304 if (which != img_request->next_completion)
2305 goto out;
2306
2307 for_each_obj_request_from(img_request, obj_request) {
Alex Elder21692382013-04-05 01:27:12 -05002308 rbd_assert(more);
2309 rbd_assert(which < img_request->obj_request_count);
2310
2311 if (!obj_request_done_test(obj_request))
2312 break;
Alex Elder12178572013-02-08 09:55:49 -06002313 more = rbd_img_obj_end_request(obj_request);
Alex Elder21692382013-04-05 01:27:12 -05002314 which++;
2315 }
2316
2317 rbd_assert(more ^ (which == img_request->obj_request_count));
2318 img_request->next_completion = which;
2319out:
2320 spin_unlock_irq(&img_request->completion_lock);
Alex Elder0f2d5be2014-04-26 14:21:44 +04002321 rbd_img_request_put(img_request);
Alex Elder21692382013-04-05 01:27:12 -05002322
2323 if (!more)
2324 rbd_img_request_complete(img_request);
2325}
2326
Alex Elderf1a47392013-04-19 15:34:50 -05002327/*
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002328 * Add individual osd ops to the given ceph_osd_request and prepare
2329 * them for submission. num_ops is the current number of
2330 * osd operations already to the object request.
2331 */
2332static void rbd_img_obj_request_fill(struct rbd_obj_request *obj_request,
2333 struct ceph_osd_request *osd_request,
2334 enum obj_operation_type op_type,
2335 unsigned int num_ops)
2336{
2337 struct rbd_img_request *img_request = obj_request->img_request;
2338 struct rbd_device *rbd_dev = img_request->rbd_dev;
2339 u64 object_size = rbd_obj_bytes(&rbd_dev->header);
2340 u64 offset = obj_request->offset;
2341 u64 length = obj_request->length;
2342 u64 img_end;
2343 u16 opcode;
2344
2345 if (op_type == OBJ_OP_DISCARD) {
Josh Durgind3246fb2014-04-07 16:49:21 -07002346 if (!offset && length == object_size &&
2347 (!img_request_layered_test(img_request) ||
2348 !obj_request_overlaps_parent(obj_request))) {
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002349 opcode = CEPH_OSD_OP_DELETE;
2350 } else if ((offset + length == object_size)) {
2351 opcode = CEPH_OSD_OP_TRUNCATE;
2352 } else {
2353 down_read(&rbd_dev->header_rwsem);
2354 img_end = rbd_dev->header.image_size;
2355 up_read(&rbd_dev->header_rwsem);
2356
2357 if (obj_request->img_offset + length == img_end)
2358 opcode = CEPH_OSD_OP_TRUNCATE;
2359 else
2360 opcode = CEPH_OSD_OP_ZERO;
2361 }
2362 } else if (op_type == OBJ_OP_WRITE) {
Ilya Dryomove30b7572015-10-07 17:27:17 +02002363 if (!offset && length == object_size)
2364 opcode = CEPH_OSD_OP_WRITEFULL;
2365 else
2366 opcode = CEPH_OSD_OP_WRITE;
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002367 osd_req_op_alloc_hint_init(osd_request, num_ops,
2368 object_size, object_size);
2369 num_ops++;
2370 } else {
2371 opcode = CEPH_OSD_OP_READ;
2372 }
2373
Ilya Dryomov7e868b62014-11-21 22:16:43 +03002374 if (opcode == CEPH_OSD_OP_DELETE)
Yan, Zheng144cba12015-04-27 11:09:54 +08002375 osd_req_op_init(osd_request, num_ops, opcode, 0);
Ilya Dryomov7e868b62014-11-21 22:16:43 +03002376 else
2377 osd_req_op_extent_init(osd_request, num_ops, opcode,
2378 offset, length, 0, 0);
2379
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002380 if (obj_request->type == OBJ_REQUEST_BIO)
2381 osd_req_op_extent_osd_data_bio(osd_request, num_ops,
2382 obj_request->bio_list, length);
2383 else if (obj_request->type == OBJ_REQUEST_PAGES)
2384 osd_req_op_extent_osd_data_pages(osd_request, num_ops,
2385 obj_request->pages, length,
2386 offset & ~PAGE_MASK, false, false);
2387
2388 /* Discards are also writes */
2389 if (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD)
2390 rbd_osd_req_format_write(obj_request);
2391 else
2392 rbd_osd_req_format_read(obj_request);
2393}
2394
2395/*
Alex Elderf1a47392013-04-19 15:34:50 -05002396 * Split up an image request into one or more object requests, each
2397 * to a different object. The "type" parameter indicates whether
2398 * "data_desc" is the pointer to the head of a list of bio
2399 * structures, or the base of a page array. In either case this
2400 * function assumes data_desc describes memory sufficient to hold
2401 * all data described by the image request.
2402 */
2403static int rbd_img_request_fill(struct rbd_img_request *img_request,
2404 enum obj_request_type type,
2405 void *data_desc)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002406{
2407 struct rbd_device *rbd_dev = img_request->rbd_dev;
2408 struct rbd_obj_request *obj_request = NULL;
2409 struct rbd_obj_request *next_obj_request;
Jingoo Hana1580732013-08-09 13:04:35 +09002410 struct bio *bio_list = NULL;
Alex Elderf1a47392013-04-19 15:34:50 -05002411 unsigned int bio_offset = 0;
Jingoo Hana1580732013-08-09 13:04:35 +09002412 struct page **pages = NULL;
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002413 enum obj_operation_type op_type;
Alex Elder7da22d22013-01-24 16:13:36 -06002414 u64 img_offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002415 u64 resid;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002416
Alex Elderf1a47392013-04-19 15:34:50 -05002417 dout("%s: img %p type %d data_desc %p\n", __func__, img_request,
2418 (int)type, data_desc);
Alex Elder37206ee2013-02-20 17:32:08 -06002419
Alex Elder7da22d22013-01-24 16:13:36 -06002420 img_offset = img_request->offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002421 resid = img_request->length;
Alex Elder4dda41d2013-02-20 21:59:33 -06002422 rbd_assert(resid > 0);
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002423 op_type = rbd_img_request_op_type(img_request);
Alex Elderf1a47392013-04-19 15:34:50 -05002424
2425 if (type == OBJ_REQUEST_BIO) {
2426 bio_list = data_desc;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002427 rbd_assert(img_offset ==
2428 bio_list->bi_iter.bi_sector << SECTOR_SHIFT);
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002429 } else if (type == OBJ_REQUEST_PAGES) {
Alex Elderf1a47392013-04-19 15:34:50 -05002430 pages = data_desc;
2431 }
2432
Alex Elderbf0d5f502012-11-22 00:00:08 -06002433 while (resid) {
Alex Elder2fa12322013-04-05 01:27:12 -05002434 struct ceph_osd_request *osd_req;
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01002435 u64 object_no = img_offset >> rbd_dev->header.obj_order;
Ilya Dryomov67e2b652017-01-25 18:16:22 +01002436 u64 offset = rbd_segment_offset(rbd_dev, img_offset);
2437 u64 length = rbd_segment_length(rbd_dev, img_offset, resid);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002438
Ilya Dryomov6c696d82017-01-25 18:16:23 +01002439 obj_request = rbd_obj_request_create(type);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002440 if (!obj_request)
2441 goto out_unwind;
Ilya Dryomov62054da2014-03-04 11:57:17 +02002442
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01002443 obj_request->object_no = object_no;
Ilya Dryomov67e2b652017-01-25 18:16:22 +01002444 obj_request->offset = offset;
2445 obj_request->length = length;
2446
Josh Durgin03507db2013-08-27 14:45:46 -07002447 /*
2448 * set obj_request->img_request before creating the
2449 * osd_request so that it gets the right snapc
2450 */
2451 rbd_img_obj_request_add(img_request, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002452
Alex Elderf1a47392013-04-19 15:34:50 -05002453 if (type == OBJ_REQUEST_BIO) {
2454 unsigned int clone_size;
2455
2456 rbd_assert(length <= (u64)UINT_MAX);
2457 clone_size = (unsigned int)length;
2458 obj_request->bio_list =
2459 bio_chain_clone_range(&bio_list,
2460 &bio_offset,
2461 clone_size,
David Disseldorp2224d872016-04-05 11:13:39 +02002462 GFP_NOIO);
Alex Elderf1a47392013-04-19 15:34:50 -05002463 if (!obj_request->bio_list)
Ilya Dryomov62054da2014-03-04 11:57:17 +02002464 goto out_unwind;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002465 } else if (type == OBJ_REQUEST_PAGES) {
Alex Elderf1a47392013-04-19 15:34:50 -05002466 unsigned int page_count;
2467
2468 obj_request->pages = pages;
2469 page_count = (u32)calc_pages_for(offset, length);
2470 obj_request->page_count = page_count;
2471 if ((offset + length) & ~PAGE_MASK)
2472 page_count--; /* more on last page */
2473 pages += page_count;
2474 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06002475
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002476 osd_req = rbd_osd_req_create(rbd_dev, op_type,
2477 (op_type == OBJ_OP_WRITE) ? 2 : 1,
2478 obj_request);
Alex Elder2fa12322013-04-05 01:27:12 -05002479 if (!osd_req)
Ilya Dryomov62054da2014-03-04 11:57:17 +02002480 goto out_unwind;
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002481
Alex Elder2fa12322013-04-05 01:27:12 -05002482 obj_request->osd_req = osd_req;
Alex Elder21692382013-04-05 01:27:12 -05002483 obj_request->callback = rbd_img_obj_callback;
Alex Elder7da22d22013-01-24 16:13:36 -06002484 obj_request->img_offset = img_offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002485
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002486 rbd_img_obj_request_fill(obj_request, osd_req, op_type, 0);
2487
Alex Elder7da22d22013-01-24 16:13:36 -06002488 img_offset += length;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002489 resid -= length;
2490 }
2491
2492 return 0;
2493
Alex Elderbf0d5f502012-11-22 00:00:08 -06002494out_unwind:
2495 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
Ilya Dryomov42dd0372014-03-04 11:57:17 +02002496 rbd_img_obj_request_del(img_request, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002497
2498 return -ENOMEM;
2499}
2500
Alex Elder3d7efd12013-04-19 15:34:50 -05002501static void
Ilya Dryomov27617132015-07-16 17:36:11 +03002502rbd_osd_copyup_callback(struct rbd_obj_request *obj_request)
Alex Elder0eefd472013-04-19 15:34:50 -05002503{
2504 struct rbd_img_request *img_request;
2505 struct rbd_device *rbd_dev;
Alex Elderebda6402013-05-10 16:29:22 -05002506 struct page **pages;
Alex Elder0eefd472013-04-19 15:34:50 -05002507 u32 page_count;
2508
Ilya Dryomov27617132015-07-16 17:36:11 +03002509 dout("%s: obj %p\n", __func__, obj_request);
2510
Josh Durgind3246fb2014-04-07 16:49:21 -07002511 rbd_assert(obj_request->type == OBJ_REQUEST_BIO ||
2512 obj_request->type == OBJ_REQUEST_NODATA);
Alex Elder0eefd472013-04-19 15:34:50 -05002513 rbd_assert(obj_request_img_data_test(obj_request));
2514 img_request = obj_request->img_request;
2515 rbd_assert(img_request);
2516
2517 rbd_dev = img_request->rbd_dev;
2518 rbd_assert(rbd_dev);
Alex Elder0eefd472013-04-19 15:34:50 -05002519
Alex Elderebda6402013-05-10 16:29:22 -05002520 pages = obj_request->copyup_pages;
2521 rbd_assert(pages != NULL);
Alex Elder0eefd472013-04-19 15:34:50 -05002522 obj_request->copyup_pages = NULL;
Alex Elderebda6402013-05-10 16:29:22 -05002523 page_count = obj_request->copyup_page_count;
2524 rbd_assert(page_count);
2525 obj_request->copyup_page_count = 0;
2526 ceph_release_page_vector(pages, page_count);
Alex Elder0eefd472013-04-19 15:34:50 -05002527
2528 /*
2529 * We want the transfer count to reflect the size of the
2530 * original write request. There is no such thing as a
2531 * successful short write, so if the request was successful
2532 * we can just set it to the originally-requested length.
2533 */
2534 if (!obj_request->result)
2535 obj_request->xferred = obj_request->length;
2536
Ilya Dryomov27617132015-07-16 17:36:11 +03002537 obj_request_done_set(obj_request);
Alex Elder0eefd472013-04-19 15:34:50 -05002538}
2539
2540static void
Alex Elder3d7efd12013-04-19 15:34:50 -05002541rbd_img_obj_parent_read_full_callback(struct rbd_img_request *img_request)
2542{
2543 struct rbd_obj_request *orig_request;
Alex Elder0eefd472013-04-19 15:34:50 -05002544 struct ceph_osd_request *osd_req;
Alex Elder0eefd472013-04-19 15:34:50 -05002545 struct rbd_device *rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002546 struct page **pages;
Josh Durgind3246fb2014-04-07 16:49:21 -07002547 enum obj_operation_type op_type;
Alex Elderebda6402013-05-10 16:29:22 -05002548 u32 page_count;
Alex Elderbbea1c12013-05-06 17:40:33 -05002549 int img_result;
Alex Elderebda6402013-05-10 16:29:22 -05002550 u64 parent_length;
Alex Elder3d7efd12013-04-19 15:34:50 -05002551
2552 rbd_assert(img_request_child_test(img_request));
2553
2554 /* First get what we need from the image request */
2555
2556 pages = img_request->copyup_pages;
2557 rbd_assert(pages != NULL);
2558 img_request->copyup_pages = NULL;
Alex Elderebda6402013-05-10 16:29:22 -05002559 page_count = img_request->copyup_page_count;
2560 rbd_assert(page_count);
2561 img_request->copyup_page_count = 0;
Alex Elder3d7efd12013-04-19 15:34:50 -05002562
2563 orig_request = img_request->obj_request;
2564 rbd_assert(orig_request != NULL);
Alex Elderb91f09f2013-05-10 16:29:22 -05002565 rbd_assert(obj_request_type_valid(orig_request->type));
Alex Elderbbea1c12013-05-06 17:40:33 -05002566 img_result = img_request->result;
Alex Elderebda6402013-05-10 16:29:22 -05002567 parent_length = img_request->length;
Ilya Dryomovfa355112016-09-16 15:20:42 +02002568 rbd_assert(img_result || parent_length == img_request->xferred);
Alex Elder3d7efd12013-04-19 15:34:50 -05002569 rbd_img_request_put(img_request);
2570
Alex Elder91c6feb2013-05-06 17:40:32 -05002571 rbd_assert(orig_request->img_request);
2572 rbd_dev = orig_request->img_request->rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002573 rbd_assert(rbd_dev);
Alex Elder3d7efd12013-04-19 15:34:50 -05002574
Alex Elderbbea1c12013-05-06 17:40:33 -05002575 /*
2576 * If the overlap has become 0 (most likely because the
2577 * image has been flattened) we need to free the pages
2578 * and re-submit the original write request.
2579 */
2580 if (!rbd_dev->parent_overlap) {
Alex Elderbbea1c12013-05-06 17:40:33 -05002581 ceph_release_page_vector(pages, page_count);
Ilya Dryomov980917f2016-09-12 18:59:42 +02002582 rbd_obj_request_submit(orig_request);
2583 return;
Alex Elderbbea1c12013-05-06 17:40:33 -05002584 }
2585
2586 if (img_result)
Alex Elder0eefd472013-04-19 15:34:50 -05002587 goto out_err;
Alex Elder3d7efd12013-04-19 15:34:50 -05002588
Alex Elder8785b1d2013-05-09 10:08:49 -05002589 /*
2590 * The original osd request is of no use to use any more.
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02002591 * We need a new one that can hold the three ops in a copyup
Alex Elder8785b1d2013-05-09 10:08:49 -05002592 * request. Allocate the new copyup osd request for the
2593 * original request, and release the old one.
2594 */
Alex Elderbbea1c12013-05-06 17:40:33 -05002595 img_result = -ENOMEM;
Alex Elder0eefd472013-04-19 15:34:50 -05002596 osd_req = rbd_osd_req_create_copyup(orig_request);
2597 if (!osd_req)
2598 goto out_err;
Alex Elder8785b1d2013-05-09 10:08:49 -05002599 rbd_osd_req_destroy(orig_request->osd_req);
Alex Elder0eefd472013-04-19 15:34:50 -05002600 orig_request->osd_req = osd_req;
2601 orig_request->copyup_pages = pages;
Alex Elderebda6402013-05-10 16:29:22 -05002602 orig_request->copyup_page_count = page_count;
Alex Elder3d7efd12013-04-19 15:34:50 -05002603
Alex Elder0eefd472013-04-19 15:34:50 -05002604 /* Initialize the copyup op */
2605
2606 osd_req_op_cls_init(osd_req, 0, CEPH_OSD_OP_CALL, "rbd", "copyup");
Alex Elderebda6402013-05-10 16:29:22 -05002607 osd_req_op_cls_request_data_pages(osd_req, 0, pages, parent_length, 0,
Alex Elder0eefd472013-04-19 15:34:50 -05002608 false, false);
2609
Josh Durgind3246fb2014-04-07 16:49:21 -07002610 /* Add the other op(s) */
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02002611
Josh Durgind3246fb2014-04-07 16:49:21 -07002612 op_type = rbd_img_request_op_type(orig_request->img_request);
2613 rbd_img_obj_request_fill(orig_request, osd_req, op_type, 1);
Alex Elder0eefd472013-04-19 15:34:50 -05002614
2615 /* All set, send it off. */
2616
Ilya Dryomov980917f2016-09-12 18:59:42 +02002617 rbd_obj_request_submit(orig_request);
2618 return;
Alex Elder0eefd472013-04-19 15:34:50 -05002619
Alex Elder0eefd472013-04-19 15:34:50 -05002620out_err:
Ilya Dryomovfa355112016-09-16 15:20:42 +02002621 ceph_release_page_vector(pages, page_count);
Ilya Dryomov0dcc6852016-09-26 15:43:52 +02002622 rbd_obj_request_error(orig_request, img_result);
Alex Elder3d7efd12013-04-19 15:34:50 -05002623}
2624
2625/*
2626 * Read from the parent image the range of data that covers the
2627 * entire target of the given object request. This is used for
2628 * satisfying a layered image write request when the target of an
2629 * object request from the image request does not exist.
2630 *
2631 * A page array big enough to hold the returned data is allocated
2632 * and supplied to rbd_img_request_fill() as the "data descriptor."
2633 * When the read completes, this page array will be transferred to
2634 * the original object request for the copyup operation.
2635 *
Ilya Dryomovc2e82412016-09-13 20:18:01 +02002636 * If an error occurs, it is recorded as the result of the original
2637 * object request in rbd_img_obj_exists_callback().
Alex Elder3d7efd12013-04-19 15:34:50 -05002638 */
2639static int rbd_img_obj_parent_read_full(struct rbd_obj_request *obj_request)
2640{
Ilya Dryomov058aa992016-09-12 14:44:45 +02002641 struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002642 struct rbd_img_request *parent_request = NULL;
Alex Elder3d7efd12013-04-19 15:34:50 -05002643 u64 img_offset;
2644 u64 length;
2645 struct page **pages = NULL;
2646 u32 page_count;
2647 int result;
2648
Alex Elder3d7efd12013-04-19 15:34:50 -05002649 rbd_assert(rbd_dev->parent != NULL);
2650
2651 /*
2652 * Determine the byte range covered by the object in the
2653 * child image to which the original request was to be sent.
2654 */
2655 img_offset = obj_request->img_offset - obj_request->offset;
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +01002656 length = rbd_obj_bytes(&rbd_dev->header);
Alex Elder3d7efd12013-04-19 15:34:50 -05002657
2658 /*
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002659 * There is no defined parent data beyond the parent
2660 * overlap, so limit what we read at that boundary if
2661 * necessary.
2662 */
2663 if (img_offset + length > rbd_dev->parent_overlap) {
2664 rbd_assert(img_offset < rbd_dev->parent_overlap);
2665 length = rbd_dev->parent_overlap - img_offset;
2666 }
2667
2668 /*
Alex Elder3d7efd12013-04-19 15:34:50 -05002669 * Allocate a page array big enough to receive the data read
2670 * from the parent.
2671 */
2672 page_count = (u32)calc_pages_for(0, length);
Ilya Dryomov1e37f2f2017-11-06 11:33:36 +01002673 pages = ceph_alloc_page_vector(page_count, GFP_NOIO);
Alex Elder3d7efd12013-04-19 15:34:50 -05002674 if (IS_ERR(pages)) {
2675 result = PTR_ERR(pages);
2676 pages = NULL;
2677 goto out_err;
2678 }
2679
2680 result = -ENOMEM;
Alex Eldere93f3152013-05-08 22:50:04 -05002681 parent_request = rbd_parent_request_create(obj_request,
2682 img_offset, length);
Alex Elder3d7efd12013-04-19 15:34:50 -05002683 if (!parent_request)
2684 goto out_err;
Alex Elder3d7efd12013-04-19 15:34:50 -05002685
2686 result = rbd_img_request_fill(parent_request, OBJ_REQUEST_PAGES, pages);
2687 if (result)
2688 goto out_err;
Ilya Dryomov058aa992016-09-12 14:44:45 +02002689
Alex Elder3d7efd12013-04-19 15:34:50 -05002690 parent_request->copyup_pages = pages;
Alex Elderebda6402013-05-10 16:29:22 -05002691 parent_request->copyup_page_count = page_count;
Alex Elder3d7efd12013-04-19 15:34:50 -05002692 parent_request->callback = rbd_img_obj_parent_read_full_callback;
Ilya Dryomov058aa992016-09-12 14:44:45 +02002693
Alex Elder3d7efd12013-04-19 15:34:50 -05002694 result = rbd_img_request_submit(parent_request);
2695 if (!result)
2696 return 0;
2697
2698 parent_request->copyup_pages = NULL;
Alex Elderebda6402013-05-10 16:29:22 -05002699 parent_request->copyup_page_count = 0;
Alex Elder3d7efd12013-04-19 15:34:50 -05002700 parent_request->obj_request = NULL;
2701 rbd_obj_request_put(obj_request);
2702out_err:
2703 if (pages)
2704 ceph_release_page_vector(pages, page_count);
2705 if (parent_request)
2706 rbd_img_request_put(parent_request);
Alex Elder3d7efd12013-04-19 15:34:50 -05002707 return result;
2708}
2709
Alex Elderc5b5ef62013-02-11 12:33:24 -06002710static void rbd_img_obj_exists_callback(struct rbd_obj_request *obj_request)
2711{
Alex Elderc5b5ef62013-02-11 12:33:24 -06002712 struct rbd_obj_request *orig_request;
Alex Elder638f5ab2013-05-06 17:40:33 -05002713 struct rbd_device *rbd_dev;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002714 int result;
2715
2716 rbd_assert(!obj_request_img_data_test(obj_request));
2717
2718 /*
2719 * All we need from the object request is the original
2720 * request and the result of the STAT op. Grab those, then
2721 * we're done with the request.
2722 */
2723 orig_request = obj_request->obj_request;
2724 obj_request->obj_request = NULL;
Alex Elder912c3172013-05-13 20:35:38 -05002725 rbd_obj_request_put(orig_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002726 rbd_assert(orig_request);
2727 rbd_assert(orig_request->img_request);
2728
2729 result = obj_request->result;
2730 obj_request->result = 0;
2731
2732 dout("%s: obj %p for obj %p result %d %llu/%llu\n", __func__,
2733 obj_request, orig_request, result,
2734 obj_request->xferred, obj_request->length);
2735 rbd_obj_request_put(obj_request);
2736
Alex Elder638f5ab2013-05-06 17:40:33 -05002737 /*
2738 * If the overlap has become 0 (most likely because the
Ilya Dryomov980917f2016-09-12 18:59:42 +02002739 * image has been flattened) we need to re-submit the
2740 * original request.
Alex Elder638f5ab2013-05-06 17:40:33 -05002741 */
2742 rbd_dev = orig_request->img_request->rbd_dev;
2743 if (!rbd_dev->parent_overlap) {
Ilya Dryomov980917f2016-09-12 18:59:42 +02002744 rbd_obj_request_submit(orig_request);
2745 return;
Alex Elder638f5ab2013-05-06 17:40:33 -05002746 }
Alex Elderc5b5ef62013-02-11 12:33:24 -06002747
2748 /*
2749 * Our only purpose here is to determine whether the object
2750 * exists, and we don't want to treat the non-existence as
2751 * an error. If something else comes back, transfer the
2752 * error to the original request and complete it now.
2753 */
2754 if (!result) {
2755 obj_request_existence_set(orig_request, true);
2756 } else if (result == -ENOENT) {
2757 obj_request_existence_set(orig_request, false);
Ilya Dryomovc2e82412016-09-13 20:18:01 +02002758 } else {
2759 goto fail_orig_request;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002760 }
2761
2762 /*
2763 * Resubmit the original request now that we have recorded
2764 * whether the target object exists.
2765 */
Ilya Dryomovc2e82412016-09-13 20:18:01 +02002766 result = rbd_img_obj_request_submit(orig_request);
2767 if (result)
2768 goto fail_orig_request;
2769
2770 return;
2771
2772fail_orig_request:
Ilya Dryomov0dcc6852016-09-26 15:43:52 +02002773 rbd_obj_request_error(orig_request, result);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002774}
2775
2776static int rbd_img_obj_exists_submit(struct rbd_obj_request *obj_request)
2777{
Ilya Dryomov058aa992016-09-12 14:44:45 +02002778 struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002779 struct rbd_obj_request *stat_request;
Ilya Dryomov710214e2016-09-15 17:53:32 +02002780 struct page **pages;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002781 u32 page_count;
2782 size_t size;
2783 int ret;
2784
Ilya Dryomov6c696d82017-01-25 18:16:23 +01002785 stat_request = rbd_obj_request_create(OBJ_REQUEST_PAGES);
Ilya Dryomov710214e2016-09-15 17:53:32 +02002786 if (!stat_request)
2787 return -ENOMEM;
2788
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01002789 stat_request->object_no = obj_request->object_no;
2790
Ilya Dryomov710214e2016-09-15 17:53:32 +02002791 stat_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_READ, 1,
2792 stat_request);
2793 if (!stat_request->osd_req) {
2794 ret = -ENOMEM;
2795 goto fail_stat_request;
2796 }
2797
Alex Elderc5b5ef62013-02-11 12:33:24 -06002798 /*
2799 * The response data for a STAT call consists of:
2800 * le64 length;
2801 * struct {
2802 * le32 tv_sec;
2803 * le32 tv_nsec;
2804 * } mtime;
2805 */
2806 size = sizeof (__le64) + sizeof (__le32) + sizeof (__le32);
2807 page_count = (u32)calc_pages_for(0, size);
Ilya Dryomov1e37f2f2017-11-06 11:33:36 +01002808 pages = ceph_alloc_page_vector(page_count, GFP_NOIO);
Ilya Dryomov710214e2016-09-15 17:53:32 +02002809 if (IS_ERR(pages)) {
2810 ret = PTR_ERR(pages);
2811 goto fail_stat_request;
2812 }
Alex Elderc5b5ef62013-02-11 12:33:24 -06002813
Ilya Dryomov710214e2016-09-15 17:53:32 +02002814 osd_req_op_init(stat_request->osd_req, 0, CEPH_OSD_OP_STAT, 0);
2815 osd_req_op_raw_data_in_pages(stat_request->osd_req, 0, pages, size, 0,
2816 false, false);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002817
2818 rbd_obj_request_get(obj_request);
2819 stat_request->obj_request = obj_request;
2820 stat_request->pages = pages;
2821 stat_request->page_count = page_count;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002822 stat_request->callback = rbd_img_obj_exists_callback;
2823
Ilya Dryomov980917f2016-09-12 18:59:42 +02002824 rbd_obj_request_submit(stat_request);
2825 return 0;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002826
Ilya Dryomov710214e2016-09-15 17:53:32 +02002827fail_stat_request:
2828 rbd_obj_request_put(stat_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002829 return ret;
2830}
2831
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002832static bool img_obj_request_simple(struct rbd_obj_request *obj_request)
Alex Elderb454e362013-04-19 15:34:50 -05002833{
Ilya Dryomov058aa992016-09-12 14:44:45 +02002834 struct rbd_img_request *img_request = obj_request->img_request;
2835 struct rbd_device *rbd_dev = img_request->rbd_dev;
Alex Elderb454e362013-04-19 15:34:50 -05002836
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002837 /* Reads */
Josh Durgin1c220882014-04-04 17:49:12 -07002838 if (!img_request_write_test(img_request) &&
2839 !img_request_discard_test(img_request))
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002840 return true;
Alex Elderb454e362013-04-19 15:34:50 -05002841
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002842 /* Non-layered writes */
2843 if (!img_request_layered_test(img_request))
2844 return true;
2845
2846 /*
2847 * Layered writes outside of the parent overlap range don't
2848 * share any data with the parent.
2849 */
2850 if (!obj_request_overlaps_parent(obj_request))
2851 return true;
2852
2853 /*
Guangliang Zhaoc622d222014-04-01 22:22:15 +08002854 * Entire-object layered writes - we will overwrite whatever
2855 * parent data there is anyway.
2856 */
2857 if (!obj_request->offset &&
2858 obj_request->length == rbd_obj_bytes(&rbd_dev->header))
2859 return true;
2860
2861 /*
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002862 * If the object is known to already exist, its parent data has
2863 * already been copied.
2864 */
2865 if (obj_request_known_test(obj_request) &&
2866 obj_request_exists_test(obj_request))
2867 return true;
2868
2869 return false;
2870}
2871
2872static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request)
2873{
Ilya Dryomov058aa992016-09-12 14:44:45 +02002874 rbd_assert(obj_request_img_data_test(obj_request));
2875 rbd_assert(obj_request_type_valid(obj_request->type));
2876 rbd_assert(obj_request->img_request);
2877
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002878 if (img_obj_request_simple(obj_request)) {
Ilya Dryomov980917f2016-09-12 18:59:42 +02002879 rbd_obj_request_submit(obj_request);
2880 return 0;
Alex Elderb454e362013-04-19 15:34:50 -05002881 }
2882
2883 /*
Alex Elder3d7efd12013-04-19 15:34:50 -05002884 * It's a layered write. The target object might exist but
2885 * we may not know that yet. If we know it doesn't exist,
2886 * start by reading the data for the full target object from
2887 * the parent so we can use it for a copyup to the target.
Alex Elderb454e362013-04-19 15:34:50 -05002888 */
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002889 if (obj_request_known_test(obj_request))
Alex Elder3d7efd12013-04-19 15:34:50 -05002890 return rbd_img_obj_parent_read_full(obj_request);
2891
2892 /* We don't know whether the target exists. Go find out. */
Alex Elderb454e362013-04-19 15:34:50 -05002893
2894 return rbd_img_obj_exists_submit(obj_request);
2895}
2896
Alex Elderbf0d5f502012-11-22 00:00:08 -06002897static int rbd_img_request_submit(struct rbd_img_request *img_request)
2898{
Alex Elderbf0d5f502012-11-22 00:00:08 -06002899 struct rbd_obj_request *obj_request;
Alex Elder46faeed2013-04-10 17:47:46 -05002900 struct rbd_obj_request *next_obj_request;
Ilya Dryomov663ae2c2016-05-16 13:18:57 +02002901 int ret = 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002902
Alex Elder37206ee2013-02-20 17:32:08 -06002903 dout("%s: img %p\n", __func__, img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002904
Ilya Dryomov663ae2c2016-05-16 13:18:57 +02002905 rbd_img_request_get(img_request);
2906 for_each_obj_request_safe(img_request, obj_request, next_obj_request) {
Alex Elderb454e362013-04-19 15:34:50 -05002907 ret = rbd_img_obj_request_submit(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002908 if (ret)
Ilya Dryomov663ae2c2016-05-16 13:18:57 +02002909 goto out_put_ireq;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002910 }
2911
Ilya Dryomov663ae2c2016-05-16 13:18:57 +02002912out_put_ireq:
2913 rbd_img_request_put(img_request);
2914 return ret;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002915}
2916
Alex Elder8b3e1a52013-01-24 16:13:36 -06002917static void rbd_img_parent_read_callback(struct rbd_img_request *img_request)
2918{
2919 struct rbd_obj_request *obj_request;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002920 struct rbd_device *rbd_dev;
2921 u64 obj_end;
Alex Elder02c74fb2013-05-06 17:40:33 -05002922 u64 img_xferred;
2923 int img_result;
Alex Elder8b3e1a52013-01-24 16:13:36 -06002924
2925 rbd_assert(img_request_child_test(img_request));
2926
Alex Elder02c74fb2013-05-06 17:40:33 -05002927 /* First get what we need from the image request and release it */
2928
Alex Elder8b3e1a52013-01-24 16:13:36 -06002929 obj_request = img_request->obj_request;
Alex Elder02c74fb2013-05-06 17:40:33 -05002930 img_xferred = img_request->xferred;
2931 img_result = img_request->result;
2932 rbd_img_request_put(img_request);
2933
2934 /*
2935 * If the overlap has become 0 (most likely because the
2936 * image has been flattened) we need to re-submit the
2937 * original request.
2938 */
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002939 rbd_assert(obj_request);
2940 rbd_assert(obj_request->img_request);
Alex Elder02c74fb2013-05-06 17:40:33 -05002941 rbd_dev = obj_request->img_request->rbd_dev;
2942 if (!rbd_dev->parent_overlap) {
Ilya Dryomov980917f2016-09-12 18:59:42 +02002943 rbd_obj_request_submit(obj_request);
2944 return;
Alex Elder02c74fb2013-05-06 17:40:33 -05002945 }
2946
2947 obj_request->result = img_result;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002948 if (obj_request->result)
2949 goto out;
2950
2951 /*
2952 * We need to zero anything beyond the parent overlap
2953 * boundary. Since rbd_img_obj_request_read_callback()
2954 * will zero anything beyond the end of a short read, an
2955 * easy way to do this is to pretend the data from the
2956 * parent came up short--ending at the overlap boundary.
2957 */
2958 rbd_assert(obj_request->img_offset < U64_MAX - obj_request->length);
2959 obj_end = obj_request->img_offset + obj_request->length;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002960 if (obj_end > rbd_dev->parent_overlap) {
2961 u64 xferred = 0;
2962
2963 if (obj_request->img_offset < rbd_dev->parent_overlap)
2964 xferred = rbd_dev->parent_overlap -
2965 obj_request->img_offset;
2966
Alex Elder02c74fb2013-05-06 17:40:33 -05002967 obj_request->xferred = min(img_xferred, xferred);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002968 } else {
Alex Elder02c74fb2013-05-06 17:40:33 -05002969 obj_request->xferred = img_xferred;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002970 }
2971out:
Alex Elder8b3e1a52013-01-24 16:13:36 -06002972 rbd_img_obj_request_read_callback(obj_request);
2973 rbd_obj_request_complete(obj_request);
2974}
2975
2976static void rbd_img_parent_read(struct rbd_obj_request *obj_request)
2977{
Alex Elder8b3e1a52013-01-24 16:13:36 -06002978 struct rbd_img_request *img_request;
2979 int result;
2980
2981 rbd_assert(obj_request_img_data_test(obj_request));
2982 rbd_assert(obj_request->img_request != NULL);
2983 rbd_assert(obj_request->result == (s32) -ENOENT);
Alex Elder5b2ab722013-05-06 17:40:33 -05002984 rbd_assert(obj_request_type_valid(obj_request->type));
Alex Elder8b3e1a52013-01-24 16:13:36 -06002985
Alex Elder8b3e1a52013-01-24 16:13:36 -06002986 /* rbd_read_finish(obj_request, obj_request->length); */
Alex Eldere93f3152013-05-08 22:50:04 -05002987 img_request = rbd_parent_request_create(obj_request,
Alex Elder8b3e1a52013-01-24 16:13:36 -06002988 obj_request->img_offset,
Alex Eldere93f3152013-05-08 22:50:04 -05002989 obj_request->length);
Alex Elder8b3e1a52013-01-24 16:13:36 -06002990 result = -ENOMEM;
2991 if (!img_request)
2992 goto out_err;
2993
Alex Elder5b2ab722013-05-06 17:40:33 -05002994 if (obj_request->type == OBJ_REQUEST_BIO)
2995 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
2996 obj_request->bio_list);
2997 else
2998 result = rbd_img_request_fill(img_request, OBJ_REQUEST_PAGES,
2999 obj_request->pages);
Alex Elder8b3e1a52013-01-24 16:13:36 -06003000 if (result)
3001 goto out_err;
3002
3003 img_request->callback = rbd_img_parent_read_callback;
3004 result = rbd_img_request_submit(img_request);
3005 if (result)
3006 goto out_err;
3007
3008 return;
3009out_err:
3010 if (img_request)
3011 rbd_img_request_put(img_request);
3012 obj_request->result = result;
3013 obj_request->xferred = 0;
3014 obj_request_done_set(obj_request);
3015}
3016
Ilya Dryomoved95b212016-08-12 16:40:02 +02003017static const struct rbd_client_id rbd_empty_cid;
3018
3019static bool rbd_cid_equal(const struct rbd_client_id *lhs,
3020 const struct rbd_client_id *rhs)
3021{
3022 return lhs->gid == rhs->gid && lhs->handle == rhs->handle;
3023}
3024
3025static struct rbd_client_id rbd_get_cid(struct rbd_device *rbd_dev)
3026{
3027 struct rbd_client_id cid;
3028
3029 mutex_lock(&rbd_dev->watch_mutex);
3030 cid.gid = ceph_client_gid(rbd_dev->rbd_client->client);
3031 cid.handle = rbd_dev->watch_cookie;
3032 mutex_unlock(&rbd_dev->watch_mutex);
3033 return cid;
3034}
3035
3036/*
3037 * lock_rwsem must be held for write
3038 */
3039static void rbd_set_owner_cid(struct rbd_device *rbd_dev,
3040 const struct rbd_client_id *cid)
3041{
3042 dout("%s rbd_dev %p %llu-%llu -> %llu-%llu\n", __func__, rbd_dev,
3043 rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle,
3044 cid->gid, cid->handle);
3045 rbd_dev->owner_cid = *cid; /* struct */
3046}
3047
3048static void format_lock_cookie(struct rbd_device *rbd_dev, char *buf)
3049{
3050 mutex_lock(&rbd_dev->watch_mutex);
3051 sprintf(buf, "%s %llu", RBD_LOCK_COOKIE_PREFIX, rbd_dev->watch_cookie);
3052 mutex_unlock(&rbd_dev->watch_mutex);
3053}
3054
3055/*
3056 * lock_rwsem must be held for write
3057 */
3058static int rbd_lock(struct rbd_device *rbd_dev)
3059{
3060 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3061 struct rbd_client_id cid = rbd_get_cid(rbd_dev);
3062 char cookie[32];
3063 int ret;
3064
Ilya Dryomovcbbfb0f2017-04-13 12:17:38 +02003065 WARN_ON(__rbd_is_lock_owner(rbd_dev) ||
3066 rbd_dev->lock_cookie[0] != '\0');
Ilya Dryomoved95b212016-08-12 16:40:02 +02003067
3068 format_lock_cookie(rbd_dev, cookie);
3069 ret = ceph_cls_lock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc,
3070 RBD_LOCK_NAME, CEPH_CLS_LOCK_EXCLUSIVE, cookie,
3071 RBD_LOCK_TAG, "", 0);
3072 if (ret)
3073 return ret;
3074
3075 rbd_dev->lock_state = RBD_LOCK_STATE_LOCKED;
Ilya Dryomovcbbfb0f2017-04-13 12:17:38 +02003076 strcpy(rbd_dev->lock_cookie, cookie);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003077 rbd_set_owner_cid(rbd_dev, &cid);
3078 queue_work(rbd_dev->task_wq, &rbd_dev->acquired_lock_work);
3079 return 0;
3080}
3081
3082/*
3083 * lock_rwsem must be held for write
3084 */
Ilya Dryomovbbead742017-04-13 12:17:38 +02003085static void rbd_unlock(struct rbd_device *rbd_dev)
Ilya Dryomoved95b212016-08-12 16:40:02 +02003086{
3087 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003088 int ret;
3089
Ilya Dryomovcbbfb0f2017-04-13 12:17:38 +02003090 WARN_ON(!__rbd_is_lock_owner(rbd_dev) ||
3091 rbd_dev->lock_cookie[0] == '\0');
Ilya Dryomoved95b212016-08-12 16:40:02 +02003092
Ilya Dryomoved95b212016-08-12 16:40:02 +02003093 ret = ceph_cls_unlock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc,
Ilya Dryomovcbbfb0f2017-04-13 12:17:38 +02003094 RBD_LOCK_NAME, rbd_dev->lock_cookie);
Ilya Dryomovbbead742017-04-13 12:17:38 +02003095 if (ret && ret != -ENOENT)
3096 rbd_warn(rbd_dev, "failed to unlock: %d", ret);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003097
Ilya Dryomovbbead742017-04-13 12:17:38 +02003098 /* treat errors as the image is unlocked */
3099 rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED;
Ilya Dryomovcbbfb0f2017-04-13 12:17:38 +02003100 rbd_dev->lock_cookie[0] = '\0';
Ilya Dryomoved95b212016-08-12 16:40:02 +02003101 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
3102 queue_work(rbd_dev->task_wq, &rbd_dev->released_lock_work);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003103}
3104
3105static int __rbd_notify_op_lock(struct rbd_device *rbd_dev,
3106 enum rbd_notify_op notify_op,
3107 struct page ***preply_pages,
3108 size_t *preply_len)
3109{
3110 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3111 struct rbd_client_id cid = rbd_get_cid(rbd_dev);
3112 int buf_size = 4 + 8 + 8 + CEPH_ENCODING_START_BLK_LEN;
3113 char buf[buf_size];
3114 void *p = buf;
3115
3116 dout("%s rbd_dev %p notify_op %d\n", __func__, rbd_dev, notify_op);
3117
3118 /* encode *LockPayload NotifyMessage (op + ClientId) */
3119 ceph_start_encoding(&p, 2, 1, buf_size - CEPH_ENCODING_START_BLK_LEN);
3120 ceph_encode_32(&p, notify_op);
3121 ceph_encode_64(&p, cid.gid);
3122 ceph_encode_64(&p, cid.handle);
3123
3124 return ceph_osdc_notify(osdc, &rbd_dev->header_oid,
3125 &rbd_dev->header_oloc, buf, buf_size,
3126 RBD_NOTIFY_TIMEOUT, preply_pages, preply_len);
3127}
3128
3129static void rbd_notify_op_lock(struct rbd_device *rbd_dev,
3130 enum rbd_notify_op notify_op)
3131{
3132 struct page **reply_pages;
3133 size_t reply_len;
3134
3135 __rbd_notify_op_lock(rbd_dev, notify_op, &reply_pages, &reply_len);
3136 ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len));
3137}
3138
3139static void rbd_notify_acquired_lock(struct work_struct *work)
3140{
3141 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
3142 acquired_lock_work);
3143
3144 rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_ACQUIRED_LOCK);
3145}
3146
3147static void rbd_notify_released_lock(struct work_struct *work)
3148{
3149 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
3150 released_lock_work);
3151
3152 rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_RELEASED_LOCK);
3153}
3154
3155static int rbd_request_lock(struct rbd_device *rbd_dev)
3156{
3157 struct page **reply_pages;
3158 size_t reply_len;
3159 bool lock_owner_responded = false;
3160 int ret;
3161
3162 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3163
3164 ret = __rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_REQUEST_LOCK,
3165 &reply_pages, &reply_len);
3166 if (ret && ret != -ETIMEDOUT) {
3167 rbd_warn(rbd_dev, "failed to request lock: %d", ret);
3168 goto out;
3169 }
3170
3171 if (reply_len > 0 && reply_len <= PAGE_SIZE) {
3172 void *p = page_address(reply_pages[0]);
3173 void *const end = p + reply_len;
3174 u32 n;
3175
3176 ceph_decode_32_safe(&p, end, n, e_inval); /* num_acks */
3177 while (n--) {
3178 u8 struct_v;
3179 u32 len;
3180
3181 ceph_decode_need(&p, end, 8 + 8, e_inval);
3182 p += 8 + 8; /* skip gid and cookie */
3183
3184 ceph_decode_32_safe(&p, end, len, e_inval);
3185 if (!len)
3186 continue;
3187
3188 if (lock_owner_responded) {
3189 rbd_warn(rbd_dev,
3190 "duplicate lock owners detected");
3191 ret = -EIO;
3192 goto out;
3193 }
3194
3195 lock_owner_responded = true;
3196 ret = ceph_start_decoding(&p, end, 1, "ResponseMessage",
3197 &struct_v, &len);
3198 if (ret) {
3199 rbd_warn(rbd_dev,
3200 "failed to decode ResponseMessage: %d",
3201 ret);
3202 goto e_inval;
3203 }
3204
3205 ret = ceph_decode_32(&p);
3206 }
3207 }
3208
3209 if (!lock_owner_responded) {
3210 rbd_warn(rbd_dev, "no lock owners detected");
3211 ret = -ETIMEDOUT;
3212 }
3213
3214out:
3215 ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len));
3216 return ret;
3217
3218e_inval:
3219 ret = -EINVAL;
3220 goto out;
3221}
3222
3223static void wake_requests(struct rbd_device *rbd_dev, bool wake_all)
3224{
3225 dout("%s rbd_dev %p wake_all %d\n", __func__, rbd_dev, wake_all);
3226
3227 cancel_delayed_work(&rbd_dev->lock_dwork);
3228 if (wake_all)
3229 wake_up_all(&rbd_dev->lock_waitq);
3230 else
3231 wake_up(&rbd_dev->lock_waitq);
3232}
3233
3234static int get_lock_owner_info(struct rbd_device *rbd_dev,
3235 struct ceph_locker **lockers, u32 *num_lockers)
3236{
3237 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3238 u8 lock_type;
3239 char *lock_tag;
3240 int ret;
3241
3242 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3243
3244 ret = ceph_cls_lock_info(osdc, &rbd_dev->header_oid,
3245 &rbd_dev->header_oloc, RBD_LOCK_NAME,
3246 &lock_type, &lock_tag, lockers, num_lockers);
3247 if (ret)
3248 return ret;
3249
3250 if (*num_lockers == 0) {
3251 dout("%s rbd_dev %p no lockers detected\n", __func__, rbd_dev);
3252 goto out;
3253 }
3254
3255 if (strcmp(lock_tag, RBD_LOCK_TAG)) {
3256 rbd_warn(rbd_dev, "locked by external mechanism, tag %s",
3257 lock_tag);
3258 ret = -EBUSY;
3259 goto out;
3260 }
3261
3262 if (lock_type == CEPH_CLS_LOCK_SHARED) {
3263 rbd_warn(rbd_dev, "shared lock type detected");
3264 ret = -EBUSY;
3265 goto out;
3266 }
3267
3268 if (strncmp((*lockers)[0].id.cookie, RBD_LOCK_COOKIE_PREFIX,
3269 strlen(RBD_LOCK_COOKIE_PREFIX))) {
3270 rbd_warn(rbd_dev, "locked by external mechanism, cookie %s",
3271 (*lockers)[0].id.cookie);
3272 ret = -EBUSY;
3273 goto out;
3274 }
3275
3276out:
3277 kfree(lock_tag);
3278 return ret;
3279}
3280
3281static int find_watcher(struct rbd_device *rbd_dev,
3282 const struct ceph_locker *locker)
3283{
3284 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3285 struct ceph_watch_item *watchers;
3286 u32 num_watchers;
3287 u64 cookie;
3288 int i;
3289 int ret;
3290
3291 ret = ceph_osdc_list_watchers(osdc, &rbd_dev->header_oid,
3292 &rbd_dev->header_oloc, &watchers,
3293 &num_watchers);
3294 if (ret)
3295 return ret;
3296
3297 sscanf(locker->id.cookie, RBD_LOCK_COOKIE_PREFIX " %llu", &cookie);
3298 for (i = 0; i < num_watchers; i++) {
3299 if (!memcmp(&watchers[i].addr, &locker->info.addr,
3300 sizeof(locker->info.addr)) &&
3301 watchers[i].cookie == cookie) {
3302 struct rbd_client_id cid = {
3303 .gid = le64_to_cpu(watchers[i].name.num),
3304 .handle = cookie,
3305 };
3306
3307 dout("%s rbd_dev %p found cid %llu-%llu\n", __func__,
3308 rbd_dev, cid.gid, cid.handle);
3309 rbd_set_owner_cid(rbd_dev, &cid);
3310 ret = 1;
3311 goto out;
3312 }
3313 }
3314
3315 dout("%s rbd_dev %p no watchers\n", __func__, rbd_dev);
3316 ret = 0;
3317out:
3318 kfree(watchers);
3319 return ret;
3320}
3321
3322/*
3323 * lock_rwsem must be held for write
3324 */
3325static int rbd_try_lock(struct rbd_device *rbd_dev)
3326{
3327 struct ceph_client *client = rbd_dev->rbd_client->client;
3328 struct ceph_locker *lockers;
3329 u32 num_lockers;
3330 int ret;
3331
3332 for (;;) {
3333 ret = rbd_lock(rbd_dev);
3334 if (ret != -EBUSY)
3335 return ret;
3336
3337 /* determine if the current lock holder is still alive */
3338 ret = get_lock_owner_info(rbd_dev, &lockers, &num_lockers);
3339 if (ret)
3340 return ret;
3341
3342 if (num_lockers == 0)
3343 goto again;
3344
3345 ret = find_watcher(rbd_dev, lockers);
3346 if (ret) {
3347 if (ret > 0)
3348 ret = 0; /* have to request lock */
3349 goto out;
3350 }
3351
3352 rbd_warn(rbd_dev, "%s%llu seems dead, breaking lock",
3353 ENTITY_NAME(lockers[0].id.name));
3354
3355 ret = ceph_monc_blacklist_add(&client->monc,
3356 &lockers[0].info.addr);
3357 if (ret) {
3358 rbd_warn(rbd_dev, "blacklist of %s%llu failed: %d",
3359 ENTITY_NAME(lockers[0].id.name), ret);
3360 goto out;
3361 }
3362
3363 ret = ceph_cls_break_lock(&client->osdc, &rbd_dev->header_oid,
3364 &rbd_dev->header_oloc, RBD_LOCK_NAME,
3365 lockers[0].id.cookie,
3366 &lockers[0].id.name);
3367 if (ret && ret != -ENOENT)
3368 goto out;
3369
3370again:
3371 ceph_free_lockers(lockers, num_lockers);
3372 }
3373
3374out:
3375 ceph_free_lockers(lockers, num_lockers);
3376 return ret;
3377}
3378
3379/*
3380 * ret is set only if lock_state is RBD_LOCK_STATE_UNLOCKED
3381 */
3382static enum rbd_lock_state rbd_try_acquire_lock(struct rbd_device *rbd_dev,
3383 int *pret)
3384{
3385 enum rbd_lock_state lock_state;
3386
3387 down_read(&rbd_dev->lock_rwsem);
3388 dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev,
3389 rbd_dev->lock_state);
3390 if (__rbd_is_lock_owner(rbd_dev)) {
3391 lock_state = rbd_dev->lock_state;
3392 up_read(&rbd_dev->lock_rwsem);
3393 return lock_state;
3394 }
3395
3396 up_read(&rbd_dev->lock_rwsem);
3397 down_write(&rbd_dev->lock_rwsem);
3398 dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev,
3399 rbd_dev->lock_state);
3400 if (!__rbd_is_lock_owner(rbd_dev)) {
3401 *pret = rbd_try_lock(rbd_dev);
3402 if (*pret)
3403 rbd_warn(rbd_dev, "failed to acquire lock: %d", *pret);
3404 }
3405
3406 lock_state = rbd_dev->lock_state;
3407 up_write(&rbd_dev->lock_rwsem);
3408 return lock_state;
3409}
3410
3411static void rbd_acquire_lock(struct work_struct *work)
3412{
3413 struct rbd_device *rbd_dev = container_of(to_delayed_work(work),
3414 struct rbd_device, lock_dwork);
3415 enum rbd_lock_state lock_state;
Kefeng Wang37f13252017-07-13 15:46:35 +08003416 int ret = 0;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003417
3418 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3419again:
3420 lock_state = rbd_try_acquire_lock(rbd_dev, &ret);
3421 if (lock_state != RBD_LOCK_STATE_UNLOCKED || ret == -EBLACKLISTED) {
3422 if (lock_state == RBD_LOCK_STATE_LOCKED)
3423 wake_requests(rbd_dev, true);
3424 dout("%s rbd_dev %p lock_state %d ret %d - done\n", __func__,
3425 rbd_dev, lock_state, ret);
3426 return;
3427 }
3428
3429 ret = rbd_request_lock(rbd_dev);
3430 if (ret == -ETIMEDOUT) {
3431 goto again; /* treat this as a dead client */
Ilya Dryomove010dd02017-04-13 12:17:39 +02003432 } else if (ret == -EROFS) {
3433 rbd_warn(rbd_dev, "peer will not release lock");
3434 /*
3435 * If this is rbd_add_acquire_lock(), we want to fail
3436 * immediately -- reuse BLACKLISTED flag. Otherwise we
3437 * want to block.
3438 */
3439 if (!(rbd_dev->disk->flags & GENHD_FL_UP)) {
3440 set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags);
3441 /* wake "rbd map --exclusive" process */
3442 wake_requests(rbd_dev, false);
3443 }
Ilya Dryomoved95b212016-08-12 16:40:02 +02003444 } else if (ret < 0) {
3445 rbd_warn(rbd_dev, "error requesting lock: %d", ret);
3446 mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork,
3447 RBD_RETRY_DELAY);
3448 } else {
3449 /*
3450 * lock owner acked, but resend if we don't see them
3451 * release the lock
3452 */
3453 dout("%s rbd_dev %p requeueing lock_dwork\n", __func__,
3454 rbd_dev);
3455 mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork,
3456 msecs_to_jiffies(2 * RBD_NOTIFY_TIMEOUT * MSEC_PER_SEC));
3457 }
3458}
3459
3460/*
3461 * lock_rwsem must be held for write
3462 */
3463static bool rbd_release_lock(struct rbd_device *rbd_dev)
3464{
3465 dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev,
3466 rbd_dev->lock_state);
3467 if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED)
3468 return false;
3469
3470 rbd_dev->lock_state = RBD_LOCK_STATE_RELEASING;
3471 downgrade_write(&rbd_dev->lock_rwsem);
3472 /*
3473 * Ensure that all in-flight IO is flushed.
3474 *
3475 * FIXME: ceph_osdc_sync() flushes the entire OSD client, which
3476 * may be shared with other devices.
3477 */
3478 ceph_osdc_sync(&rbd_dev->rbd_client->client->osdc);
3479 up_read(&rbd_dev->lock_rwsem);
3480
3481 down_write(&rbd_dev->lock_rwsem);
3482 dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev,
3483 rbd_dev->lock_state);
3484 if (rbd_dev->lock_state != RBD_LOCK_STATE_RELEASING)
3485 return false;
3486
Ilya Dryomovbbead742017-04-13 12:17:38 +02003487 rbd_unlock(rbd_dev);
3488 /*
3489 * Give others a chance to grab the lock - we would re-acquire
3490 * almost immediately if we got new IO during ceph_osdc_sync()
3491 * otherwise. We need to ack our own notifications, so this
3492 * lock_dwork will be requeued from rbd_wait_state_locked()
3493 * after wake_requests() in rbd_handle_released_lock().
3494 */
3495 cancel_delayed_work(&rbd_dev->lock_dwork);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003496 return true;
3497}
3498
3499static void rbd_release_lock_work(struct work_struct *work)
3500{
3501 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
3502 unlock_work);
3503
3504 down_write(&rbd_dev->lock_rwsem);
3505 rbd_release_lock(rbd_dev);
3506 up_write(&rbd_dev->lock_rwsem);
3507}
3508
3509static void rbd_handle_acquired_lock(struct rbd_device *rbd_dev, u8 struct_v,
3510 void **p)
3511{
3512 struct rbd_client_id cid = { 0 };
3513
3514 if (struct_v >= 2) {
3515 cid.gid = ceph_decode_64(p);
3516 cid.handle = ceph_decode_64(p);
3517 }
3518
3519 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
3520 cid.handle);
3521 if (!rbd_cid_equal(&cid, &rbd_empty_cid)) {
3522 down_write(&rbd_dev->lock_rwsem);
3523 if (rbd_cid_equal(&cid, &rbd_dev->owner_cid)) {
3524 /*
3525 * we already know that the remote client is
3526 * the owner
3527 */
3528 up_write(&rbd_dev->lock_rwsem);
3529 return;
3530 }
3531
3532 rbd_set_owner_cid(rbd_dev, &cid);
3533 downgrade_write(&rbd_dev->lock_rwsem);
3534 } else {
3535 down_read(&rbd_dev->lock_rwsem);
3536 }
3537
3538 if (!__rbd_is_lock_owner(rbd_dev))
3539 wake_requests(rbd_dev, false);
3540 up_read(&rbd_dev->lock_rwsem);
3541}
3542
3543static void rbd_handle_released_lock(struct rbd_device *rbd_dev, u8 struct_v,
3544 void **p)
3545{
3546 struct rbd_client_id cid = { 0 };
3547
3548 if (struct_v >= 2) {
3549 cid.gid = ceph_decode_64(p);
3550 cid.handle = ceph_decode_64(p);
3551 }
3552
3553 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
3554 cid.handle);
3555 if (!rbd_cid_equal(&cid, &rbd_empty_cid)) {
3556 down_write(&rbd_dev->lock_rwsem);
3557 if (!rbd_cid_equal(&cid, &rbd_dev->owner_cid)) {
3558 dout("%s rbd_dev %p unexpected owner, cid %llu-%llu != owner_cid %llu-%llu\n",
3559 __func__, rbd_dev, cid.gid, cid.handle,
3560 rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle);
3561 up_write(&rbd_dev->lock_rwsem);
3562 return;
3563 }
3564
3565 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
3566 downgrade_write(&rbd_dev->lock_rwsem);
3567 } else {
3568 down_read(&rbd_dev->lock_rwsem);
3569 }
3570
3571 if (!__rbd_is_lock_owner(rbd_dev))
3572 wake_requests(rbd_dev, false);
3573 up_read(&rbd_dev->lock_rwsem);
3574}
3575
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02003576/*
3577 * Returns result for ResponseMessage to be encoded (<= 0), or 1 if no
3578 * ResponseMessage is needed.
3579 */
3580static int rbd_handle_request_lock(struct rbd_device *rbd_dev, u8 struct_v,
3581 void **p)
Ilya Dryomoved95b212016-08-12 16:40:02 +02003582{
3583 struct rbd_client_id my_cid = rbd_get_cid(rbd_dev);
3584 struct rbd_client_id cid = { 0 };
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02003585 int result = 1;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003586
3587 if (struct_v >= 2) {
3588 cid.gid = ceph_decode_64(p);
3589 cid.handle = ceph_decode_64(p);
3590 }
3591
3592 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
3593 cid.handle);
3594 if (rbd_cid_equal(&cid, &my_cid))
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02003595 return result;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003596
3597 down_read(&rbd_dev->lock_rwsem);
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02003598 if (__rbd_is_lock_owner(rbd_dev)) {
3599 if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED &&
3600 rbd_cid_equal(&rbd_dev->owner_cid, &rbd_empty_cid))
3601 goto out_unlock;
3602
3603 /*
3604 * encode ResponseMessage(0) so the peer can detect
3605 * a missing owner
3606 */
3607 result = 0;
3608
3609 if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) {
Ilya Dryomove010dd02017-04-13 12:17:39 +02003610 if (!rbd_dev->opts->exclusive) {
3611 dout("%s rbd_dev %p queueing unlock_work\n",
3612 __func__, rbd_dev);
3613 queue_work(rbd_dev->task_wq,
3614 &rbd_dev->unlock_work);
3615 } else {
3616 /* refuse to release the lock */
3617 result = -EROFS;
3618 }
Ilya Dryomoved95b212016-08-12 16:40:02 +02003619 }
3620 }
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02003621
3622out_unlock:
Ilya Dryomoved95b212016-08-12 16:40:02 +02003623 up_read(&rbd_dev->lock_rwsem);
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02003624 return result;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003625}
3626
3627static void __rbd_acknowledge_notify(struct rbd_device *rbd_dev,
3628 u64 notify_id, u64 cookie, s32 *result)
3629{
3630 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3631 int buf_size = 4 + CEPH_ENCODING_START_BLK_LEN;
3632 char buf[buf_size];
3633 int ret;
3634
3635 if (result) {
3636 void *p = buf;
3637
3638 /* encode ResponseMessage */
3639 ceph_start_encoding(&p, 1, 1,
3640 buf_size - CEPH_ENCODING_START_BLK_LEN);
3641 ceph_encode_32(&p, *result);
3642 } else {
3643 buf_size = 0;
3644 }
3645
3646 ret = ceph_osdc_notify_ack(osdc, &rbd_dev->header_oid,
3647 &rbd_dev->header_oloc, notify_id, cookie,
3648 buf, buf_size);
3649 if (ret)
3650 rbd_warn(rbd_dev, "acknowledge_notify failed: %d", ret);
3651}
3652
3653static void rbd_acknowledge_notify(struct rbd_device *rbd_dev, u64 notify_id,
3654 u64 cookie)
3655{
3656 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3657 __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, NULL);
3658}
3659
3660static void rbd_acknowledge_notify_result(struct rbd_device *rbd_dev,
3661 u64 notify_id, u64 cookie, s32 result)
3662{
3663 dout("%s rbd_dev %p result %d\n", __func__, rbd_dev, result);
3664 __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, &result);
3665}
Ilya Dryomov922dab62016-05-26 01:15:02 +02003666
3667static void rbd_watch_cb(void *arg, u64 notify_id, u64 cookie,
3668 u64 notifier_id, void *data, size_t data_len)
Alex Elderb8d70032012-11-30 17:53:04 -06003669{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003670 struct rbd_device *rbd_dev = arg;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003671 void *p = data;
3672 void *const end = p + data_len;
Ilya Dryomovd4c22692016-09-06 11:15:48 +02003673 u8 struct_v = 0;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003674 u32 len;
3675 u32 notify_op;
Alex Elderb8d70032012-11-30 17:53:04 -06003676 int ret;
3677
Ilya Dryomoved95b212016-08-12 16:40:02 +02003678 dout("%s rbd_dev %p cookie %llu notify_id %llu data_len %zu\n",
3679 __func__, rbd_dev, cookie, notify_id, data_len);
3680 if (data_len) {
3681 ret = ceph_start_decoding(&p, end, 1, "NotifyMessage",
3682 &struct_v, &len);
3683 if (ret) {
3684 rbd_warn(rbd_dev, "failed to decode NotifyMessage: %d",
3685 ret);
3686 return;
3687 }
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04003688
Ilya Dryomoved95b212016-08-12 16:40:02 +02003689 notify_op = ceph_decode_32(&p);
3690 } else {
3691 /* legacy notification for header updates */
3692 notify_op = RBD_NOTIFY_OP_HEADER_UPDATE;
3693 len = 0;
3694 }
Alex Elderb8d70032012-11-30 17:53:04 -06003695
Ilya Dryomoved95b212016-08-12 16:40:02 +02003696 dout("%s rbd_dev %p notify_op %u\n", __func__, rbd_dev, notify_op);
3697 switch (notify_op) {
3698 case RBD_NOTIFY_OP_ACQUIRED_LOCK:
3699 rbd_handle_acquired_lock(rbd_dev, struct_v, &p);
3700 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3701 break;
3702 case RBD_NOTIFY_OP_RELEASED_LOCK:
3703 rbd_handle_released_lock(rbd_dev, struct_v, &p);
3704 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3705 break;
3706 case RBD_NOTIFY_OP_REQUEST_LOCK:
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02003707 ret = rbd_handle_request_lock(rbd_dev, struct_v, &p);
3708 if (ret <= 0)
Ilya Dryomoved95b212016-08-12 16:40:02 +02003709 rbd_acknowledge_notify_result(rbd_dev, notify_id,
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02003710 cookie, ret);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003711 else
3712 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3713 break;
3714 case RBD_NOTIFY_OP_HEADER_UPDATE:
3715 ret = rbd_dev_refresh(rbd_dev);
3716 if (ret)
3717 rbd_warn(rbd_dev, "refresh failed: %d", ret);
3718
3719 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3720 break;
3721 default:
3722 if (rbd_is_lock_owner(rbd_dev))
3723 rbd_acknowledge_notify_result(rbd_dev, notify_id,
3724 cookie, -EOPNOTSUPP);
3725 else
3726 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3727 break;
3728 }
Alex Elderb8d70032012-11-30 17:53:04 -06003729}
3730
Ilya Dryomov99d16942016-08-12 16:11:41 +02003731static void __rbd_unregister_watch(struct rbd_device *rbd_dev);
3732
Ilya Dryomov922dab62016-05-26 01:15:02 +02003733static void rbd_watch_errcb(void *arg, u64 cookie, int err)
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003734{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003735 struct rbd_device *rbd_dev = arg;
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003736
Ilya Dryomov922dab62016-05-26 01:15:02 +02003737 rbd_warn(rbd_dev, "encountered watch error: %d", err);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003738
Ilya Dryomoved95b212016-08-12 16:40:02 +02003739 down_write(&rbd_dev->lock_rwsem);
3740 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
3741 up_write(&rbd_dev->lock_rwsem);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003742
Ilya Dryomov99d16942016-08-12 16:11:41 +02003743 mutex_lock(&rbd_dev->watch_mutex);
3744 if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) {
3745 __rbd_unregister_watch(rbd_dev);
3746 rbd_dev->watch_state = RBD_WATCH_STATE_ERROR;
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003747
Ilya Dryomov99d16942016-08-12 16:11:41 +02003748 queue_delayed_work(rbd_dev->task_wq, &rbd_dev->watch_dwork, 0);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003749 }
Ilya Dryomov99d16942016-08-12 16:11:41 +02003750 mutex_unlock(&rbd_dev->watch_mutex);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003751}
3752
3753/*
Ilya Dryomov99d16942016-08-12 16:11:41 +02003754 * watch_mutex must be locked
Alex Elder9969ebc2013-01-18 12:31:10 -06003755 */
Ilya Dryomov99d16942016-08-12 16:11:41 +02003756static int __rbd_register_watch(struct rbd_device *rbd_dev)
Alex Elder9969ebc2013-01-18 12:31:10 -06003757{
3758 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Ilya Dryomov922dab62016-05-26 01:15:02 +02003759 struct ceph_osd_linger_request *handle;
Alex Elder9969ebc2013-01-18 12:31:10 -06003760
Ilya Dryomov922dab62016-05-26 01:15:02 +02003761 rbd_assert(!rbd_dev->watch_handle);
Ilya Dryomov99d16942016-08-12 16:11:41 +02003762 dout("%s rbd_dev %p\n", __func__, rbd_dev);
Alex Elder9969ebc2013-01-18 12:31:10 -06003763
Ilya Dryomov922dab62016-05-26 01:15:02 +02003764 handle = ceph_osdc_watch(osdc, &rbd_dev->header_oid,
3765 &rbd_dev->header_oloc, rbd_watch_cb,
3766 rbd_watch_errcb, rbd_dev);
3767 if (IS_ERR(handle))
3768 return PTR_ERR(handle);
Alex Elder9969ebc2013-01-18 12:31:10 -06003769
Ilya Dryomov922dab62016-05-26 01:15:02 +02003770 rbd_dev->watch_handle = handle;
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003771 return 0;
Alex Elder9969ebc2013-01-18 12:31:10 -06003772}
3773
Ilya Dryomov99d16942016-08-12 16:11:41 +02003774/*
3775 * watch_mutex must be locked
3776 */
3777static void __rbd_unregister_watch(struct rbd_device *rbd_dev)
Ilya Dryomovfca27062013-12-16 18:02:40 +02003778{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003779 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3780 int ret;
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003781
Ilya Dryomov99d16942016-08-12 16:11:41 +02003782 rbd_assert(rbd_dev->watch_handle);
3783 dout("%s rbd_dev %p\n", __func__, rbd_dev);
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003784
Ilya Dryomov922dab62016-05-26 01:15:02 +02003785 ret = ceph_osdc_unwatch(osdc, rbd_dev->watch_handle);
3786 if (ret)
3787 rbd_warn(rbd_dev, "failed to unwatch: %d", ret);
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003788
Ilya Dryomov922dab62016-05-26 01:15:02 +02003789 rbd_dev->watch_handle = NULL;
Ilya Dryomovc525f032016-04-28 16:07:26 +02003790}
3791
Ilya Dryomov99d16942016-08-12 16:11:41 +02003792static int rbd_register_watch(struct rbd_device *rbd_dev)
Ilya Dryomovc525f032016-04-28 16:07:26 +02003793{
Ilya Dryomov99d16942016-08-12 16:11:41 +02003794 int ret;
Ilya Dryomov811c6682016-04-15 16:22:16 +02003795
Ilya Dryomov99d16942016-08-12 16:11:41 +02003796 mutex_lock(&rbd_dev->watch_mutex);
3797 rbd_assert(rbd_dev->watch_state == RBD_WATCH_STATE_UNREGISTERED);
3798 ret = __rbd_register_watch(rbd_dev);
3799 if (ret)
3800 goto out;
3801
3802 rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED;
3803 rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id;
3804
3805out:
3806 mutex_unlock(&rbd_dev->watch_mutex);
3807 return ret;
3808}
3809
3810static void cancel_tasks_sync(struct rbd_device *rbd_dev)
3811{
3812 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3813
3814 cancel_delayed_work_sync(&rbd_dev->watch_dwork);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003815 cancel_work_sync(&rbd_dev->acquired_lock_work);
3816 cancel_work_sync(&rbd_dev->released_lock_work);
3817 cancel_delayed_work_sync(&rbd_dev->lock_dwork);
3818 cancel_work_sync(&rbd_dev->unlock_work);
Ilya Dryomov99d16942016-08-12 16:11:41 +02003819}
3820
3821static void rbd_unregister_watch(struct rbd_device *rbd_dev)
3822{
Ilya Dryomoved95b212016-08-12 16:40:02 +02003823 WARN_ON(waitqueue_active(&rbd_dev->lock_waitq));
Ilya Dryomov99d16942016-08-12 16:11:41 +02003824 cancel_tasks_sync(rbd_dev);
3825
3826 mutex_lock(&rbd_dev->watch_mutex);
3827 if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED)
3828 __rbd_unregister_watch(rbd_dev);
3829 rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED;
3830 mutex_unlock(&rbd_dev->watch_mutex);
3831
Ilya Dryomov811c6682016-04-15 16:22:16 +02003832 ceph_osdc_flush_notifies(&rbd_dev->rbd_client->client->osdc);
Ilya Dryomovfca27062013-12-16 18:02:40 +02003833}
3834
Ilya Dryomov14bb2112017-04-13 12:17:38 +02003835/*
3836 * lock_rwsem must be held for write
3837 */
3838static void rbd_reacquire_lock(struct rbd_device *rbd_dev)
3839{
3840 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3841 char cookie[32];
3842 int ret;
3843
3844 WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED);
3845
3846 format_lock_cookie(rbd_dev, cookie);
3847 ret = ceph_cls_set_cookie(osdc, &rbd_dev->header_oid,
3848 &rbd_dev->header_oloc, RBD_LOCK_NAME,
3849 CEPH_CLS_LOCK_EXCLUSIVE, rbd_dev->lock_cookie,
3850 RBD_LOCK_TAG, cookie);
3851 if (ret) {
3852 if (ret != -EOPNOTSUPP)
3853 rbd_warn(rbd_dev, "failed to update lock cookie: %d",
3854 ret);
3855
3856 /*
3857 * Lock cookie cannot be updated on older OSDs, so do
3858 * a manual release and queue an acquire.
3859 */
3860 if (rbd_release_lock(rbd_dev))
3861 queue_delayed_work(rbd_dev->task_wq,
3862 &rbd_dev->lock_dwork, 0);
3863 } else {
3864 strcpy(rbd_dev->lock_cookie, cookie);
3865 }
3866}
3867
Ilya Dryomov99d16942016-08-12 16:11:41 +02003868static void rbd_reregister_watch(struct work_struct *work)
3869{
3870 struct rbd_device *rbd_dev = container_of(to_delayed_work(work),
3871 struct rbd_device, watch_dwork);
3872 int ret;
3873
3874 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3875
3876 mutex_lock(&rbd_dev->watch_mutex);
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003877 if (rbd_dev->watch_state != RBD_WATCH_STATE_ERROR) {
3878 mutex_unlock(&rbd_dev->watch_mutex);
Ilya Dryomov14bb2112017-04-13 12:17:38 +02003879 return;
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003880 }
Ilya Dryomov99d16942016-08-12 16:11:41 +02003881
3882 ret = __rbd_register_watch(rbd_dev);
3883 if (ret) {
3884 rbd_warn(rbd_dev, "failed to reregister watch: %d", ret);
Ilya Dryomov4d736442016-09-29 14:23:12 +02003885 if (ret == -EBLACKLISTED || ret == -ENOENT) {
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003886 set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags);
Ilya Dryomov14bb2112017-04-13 12:17:38 +02003887 wake_requests(rbd_dev, true);
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003888 } else {
Ilya Dryomov99d16942016-08-12 16:11:41 +02003889 queue_delayed_work(rbd_dev->task_wq,
3890 &rbd_dev->watch_dwork,
3891 RBD_RETRY_DELAY);
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003892 }
3893 mutex_unlock(&rbd_dev->watch_mutex);
Ilya Dryomov14bb2112017-04-13 12:17:38 +02003894 return;
Ilya Dryomov99d16942016-08-12 16:11:41 +02003895 }
3896
3897 rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED;
3898 rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id;
3899 mutex_unlock(&rbd_dev->watch_mutex);
3900
Ilya Dryomov14bb2112017-04-13 12:17:38 +02003901 down_write(&rbd_dev->lock_rwsem);
3902 if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED)
3903 rbd_reacquire_lock(rbd_dev);
3904 up_write(&rbd_dev->lock_rwsem);
3905
Ilya Dryomov99d16942016-08-12 16:11:41 +02003906 ret = rbd_dev_refresh(rbd_dev);
3907 if (ret)
3908 rbd_warn(rbd_dev, "reregisteration refresh failed: %d", ret);
Ilya Dryomov99d16942016-08-12 16:11:41 +02003909}
3910
Alex Elder36be9a72013-01-19 00:30:28 -06003911/*
Alex Elderf40eb342013-04-25 15:09:42 -05003912 * Synchronous osd object method call. Returns the number of bytes
3913 * returned in the outbound buffer, or a negative error code.
Alex Elder36be9a72013-01-19 00:30:28 -06003914 */
3915static int rbd_obj_method_sync(struct rbd_device *rbd_dev,
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003916 struct ceph_object_id *oid,
3917 struct ceph_object_locator *oloc,
Alex Elder36be9a72013-01-19 00:30:28 -06003918 const char *method_name,
Alex Elder41579762013-04-21 12:14:45 -05003919 const void *outbound,
Alex Elder36be9a72013-01-19 00:30:28 -06003920 size_t outbound_size,
Alex Elder41579762013-04-21 12:14:45 -05003921 void *inbound,
Alex Eldere2a58ee2013-04-30 00:44:33 -05003922 size_t inbound_size)
Alex Elder36be9a72013-01-19 00:30:28 -06003923{
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003924 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3925 struct page *req_page = NULL;
3926 struct page *reply_page;
Alex Elder36be9a72013-01-19 00:30:28 -06003927 int ret;
3928
3929 /*
Alex Elder6010a452013-04-05 01:27:11 -05003930 * Method calls are ultimately read operations. The result
3931 * should placed into the inbound buffer provided. They
3932 * also supply outbound data--parameters for the object
3933 * method. Currently if this is present it will be a
3934 * snapshot id.
Alex Elder36be9a72013-01-19 00:30:28 -06003935 */
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003936 if (outbound) {
3937 if (outbound_size > PAGE_SIZE)
3938 return -E2BIG;
Alex Elder36be9a72013-01-19 00:30:28 -06003939
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003940 req_page = alloc_page(GFP_KERNEL);
3941 if (!req_page)
3942 return -ENOMEM;
Alex Elder36be9a72013-01-19 00:30:28 -06003943
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003944 memcpy(page_address(req_page), outbound, outbound_size);
Alex Elder04017e22013-04-05 14:46:02 -05003945 }
Alex Elder430c28c2013-04-03 21:32:51 -05003946
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003947 reply_page = alloc_page(GFP_KERNEL);
3948 if (!reply_page) {
3949 if (req_page)
3950 __free_page(req_page);
3951 return -ENOMEM;
3952 }
Alex Elder36be9a72013-01-19 00:30:28 -06003953
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003954 ret = ceph_osdc_call(osdc, oid, oloc, RBD_DRV_NAME, method_name,
3955 CEPH_OSD_FLAG_READ, req_page, outbound_size,
3956 reply_page, &inbound_size);
3957 if (!ret) {
3958 memcpy(inbound, page_address(reply_page), inbound_size);
3959 ret = inbound_size;
3960 }
Alex Elder57385b52013-04-21 12:14:45 -05003961
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003962 if (req_page)
3963 __free_page(req_page);
3964 __free_page(reply_page);
Alex Elder36be9a72013-01-19 00:30:28 -06003965 return ret;
3966}
3967
Ilya Dryomoved95b212016-08-12 16:40:02 +02003968/*
3969 * lock_rwsem must be held for read
3970 */
3971static void rbd_wait_state_locked(struct rbd_device *rbd_dev)
3972{
3973 DEFINE_WAIT(wait);
3974
3975 do {
3976 /*
3977 * Note the use of mod_delayed_work() in rbd_acquire_lock()
3978 * and cancel_delayed_work() in wake_requests().
3979 */
3980 dout("%s rbd_dev %p queueing lock_dwork\n", __func__, rbd_dev);
3981 queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0);
3982 prepare_to_wait_exclusive(&rbd_dev->lock_waitq, &wait,
3983 TASK_UNINTERRUPTIBLE);
3984 up_read(&rbd_dev->lock_rwsem);
3985 schedule();
3986 down_read(&rbd_dev->lock_rwsem);
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003987 } while (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED &&
3988 !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags));
3989
Ilya Dryomoved95b212016-08-12 16:40:02 +02003990 finish_wait(&rbd_dev->lock_waitq, &wait);
3991}
3992
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01003993static void rbd_queue_workfn(struct work_struct *work)
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04003994{
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01003995 struct request *rq = blk_mq_rq_from_pdu(work);
3996 struct rbd_device *rbd_dev = rq->q->queuedata;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04003997 struct rbd_img_request *img_request;
Josh Durgin4e752f02014-04-08 11:12:11 -07003998 struct ceph_snap_context *snapc = NULL;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04003999 u64 offset = (u64)blk_rq_pos(rq) << SECTOR_SHIFT;
4000 u64 length = blk_rq_bytes(rq);
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004001 enum obj_operation_type op_type;
Josh Durgin4e752f02014-04-08 11:12:11 -07004002 u64 mapping_size;
Ilya Dryomov80de1912016-09-20 14:23:17 +02004003 bool must_be_locked;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004004 int result;
4005
Christoph Hellwigaebf5262017-01-31 16:57:31 +01004006 switch (req_op(rq)) {
4007 case REQ_OP_DISCARD:
Ilya Dryomov6ac56952017-05-22 19:59:24 +02004008 case REQ_OP_WRITE_ZEROES:
Christoph Hellwigaebf5262017-01-31 16:57:31 +01004009 op_type = OBJ_OP_DISCARD;
4010 break;
4011 case REQ_OP_WRITE:
4012 op_type = OBJ_OP_WRITE;
4013 break;
4014 case REQ_OP_READ:
4015 op_type = OBJ_OP_READ;
4016 break;
4017 default:
4018 dout("%s: non-fs request type %d\n", __func__, req_op(rq));
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004019 result = -EIO;
4020 goto err;
4021 }
4022
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004023 /* Ignore/skip any zero-length requests */
4024
4025 if (!length) {
4026 dout("%s: zero-length request\n", __func__);
4027 result = 0;
4028 goto err_rq;
4029 }
4030
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004031 /* Only reads are allowed to a read-only device */
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004032
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004033 if (op_type != OBJ_OP_READ) {
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004034 if (rbd_dev->mapping.read_only) {
4035 result = -EROFS;
4036 goto err_rq;
4037 }
4038 rbd_assert(rbd_dev->spec->snap_id == CEPH_NOSNAP);
4039 }
4040
4041 /*
4042 * Quit early if the mapped snapshot no longer exists. It's
4043 * still possible the snapshot will have disappeared by the
4044 * time our request arrives at the osd, but there's no sense in
4045 * sending it if we already know.
4046 */
4047 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) {
4048 dout("request for non-existent snapshot");
4049 rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP);
4050 result = -ENXIO;
4051 goto err_rq;
4052 }
4053
4054 if (offset && length > U64_MAX - offset + 1) {
4055 rbd_warn(rbd_dev, "bad request range (%llu~%llu)", offset,
4056 length);
4057 result = -EINVAL;
4058 goto err_rq; /* Shouldn't happen */
4059 }
4060
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004061 blk_mq_start_request(rq);
4062
Josh Durgin4e752f02014-04-08 11:12:11 -07004063 down_read(&rbd_dev->header_rwsem);
4064 mapping_size = rbd_dev->mapping.size;
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004065 if (op_type != OBJ_OP_READ) {
Josh Durgin4e752f02014-04-08 11:12:11 -07004066 snapc = rbd_dev->header.snapc;
4067 ceph_get_snap_context(snapc);
4068 }
4069 up_read(&rbd_dev->header_rwsem);
4070
4071 if (offset + length > mapping_size) {
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004072 rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)", offset,
Josh Durgin4e752f02014-04-08 11:12:11 -07004073 length, mapping_size);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004074 result = -EIO;
4075 goto err_rq;
4076 }
4077
Ilya Dryomovf9bebd52017-04-13 12:17:39 +02004078 must_be_locked =
4079 (rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK) &&
4080 (op_type != OBJ_OP_READ || rbd_dev->opts->lock_on_read);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004081 if (must_be_locked) {
4082 down_read(&rbd_dev->lock_rwsem);
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02004083 if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED &&
Ilya Dryomove010dd02017-04-13 12:17:39 +02004084 !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) {
4085 if (rbd_dev->opts->exclusive) {
4086 rbd_warn(rbd_dev, "exclusive lock required");
4087 result = -EROFS;
4088 goto err_unlock;
4089 }
Ilya Dryomoved95b212016-08-12 16:40:02 +02004090 rbd_wait_state_locked(rbd_dev);
Ilya Dryomove010dd02017-04-13 12:17:39 +02004091 }
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02004092 if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) {
4093 result = -EBLACKLISTED;
4094 goto err_unlock;
4095 }
Ilya Dryomoved95b212016-08-12 16:40:02 +02004096 }
4097
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004098 img_request = rbd_img_request_create(rbd_dev, offset, length, op_type,
Josh Durgin4e752f02014-04-08 11:12:11 -07004099 snapc);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004100 if (!img_request) {
4101 result = -ENOMEM;
Ilya Dryomoved95b212016-08-12 16:40:02 +02004102 goto err_unlock;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004103 }
4104 img_request->rq = rq;
Ilya Dryomov70b16db2015-11-27 19:23:24 +01004105 snapc = NULL; /* img_request consumes a ref */
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004106
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004107 if (op_type == OBJ_OP_DISCARD)
4108 result = rbd_img_request_fill(img_request, OBJ_REQUEST_NODATA,
4109 NULL);
4110 else
4111 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
4112 rq->bio);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004113 if (result)
4114 goto err_img_request;
4115
4116 result = rbd_img_request_submit(img_request);
4117 if (result)
4118 goto err_img_request;
4119
Ilya Dryomoved95b212016-08-12 16:40:02 +02004120 if (must_be_locked)
4121 up_read(&rbd_dev->lock_rwsem);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004122 return;
4123
4124err_img_request:
4125 rbd_img_request_put(img_request);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004126err_unlock:
4127 if (must_be_locked)
4128 up_read(&rbd_dev->lock_rwsem);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004129err_rq:
4130 if (result)
4131 rbd_warn(rbd_dev, "%s %llx at %llx result %d",
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004132 obj_op_name(op_type), length, offset, result);
SF Markus Elfringe96a6502014-11-02 15:20:59 +01004133 ceph_put_snap_context(snapc);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004134err:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02004135 blk_mq_end_request(rq, errno_to_blk_status(result));
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004136}
4137
Christoph Hellwigfc17b652017-06-03 09:38:05 +02004138static blk_status_t rbd_queue_rq(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004139 const struct blk_mq_queue_data *bd)
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004140{
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004141 struct request *rq = bd->rq;
4142 struct work_struct *work = blk_mq_rq_to_pdu(rq);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004143
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004144 queue_work(rbd_wq, work);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02004145 return BLK_STS_OK;
Alex Elderbf0d5f502012-11-22 00:00:08 -06004146}
4147
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004148static void rbd_free_disk(struct rbd_device *rbd_dev)
4149{
Ilya Dryomov5769ed02017-04-13 12:17:38 +02004150 blk_cleanup_queue(rbd_dev->disk->queue);
4151 blk_mq_free_tag_set(&rbd_dev->tag_set);
4152 put_disk(rbd_dev->disk);
Alex Eldera0cab922013-04-25 23:15:08 -05004153 rbd_dev->disk = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004154}
4155
Alex Elder788e2df2013-01-17 12:25:27 -06004156static int rbd_obj_read_sync(struct rbd_device *rbd_dev,
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004157 struct ceph_object_id *oid,
4158 struct ceph_object_locator *oloc,
4159 void *buf, int buf_len)
Alex Elder788e2df2013-01-17 12:25:27 -06004160
4161{
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004162 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
4163 struct ceph_osd_request *req;
4164 struct page **pages;
4165 int num_pages = calc_pages_for(0, buf_len);
Alex Elder788e2df2013-01-17 12:25:27 -06004166 int ret;
4167
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004168 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL);
4169 if (!req)
4170 return -ENOMEM;
Alex Elder788e2df2013-01-17 12:25:27 -06004171
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004172 ceph_oid_copy(&req->r_base_oid, oid);
4173 ceph_oloc_copy(&req->r_base_oloc, oloc);
4174 req->r_flags = CEPH_OSD_FLAG_READ;
Alex Elder788e2df2013-01-17 12:25:27 -06004175
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004176 ret = ceph_osdc_alloc_messages(req, GFP_KERNEL);
Alex Elder788e2df2013-01-17 12:25:27 -06004177 if (ret)
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004178 goto out_req;
Alex Elder788e2df2013-01-17 12:25:27 -06004179
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004180 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
4181 if (IS_ERR(pages)) {
4182 ret = PTR_ERR(pages);
4183 goto out_req;
4184 }
Alex Elder1ceae7e2013-02-06 13:11:38 -06004185
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004186 osd_req_op_extent_init(req, 0, CEPH_OSD_OP_READ, 0, buf_len, 0, 0);
4187 osd_req_op_extent_osd_data_pages(req, 0, pages, buf_len, 0, false,
4188 true);
Alex Elder788e2df2013-01-17 12:25:27 -06004189
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004190 ceph_osdc_start_request(osdc, req, false);
4191 ret = ceph_osdc_wait_request(osdc, req);
4192 if (ret >= 0)
4193 ceph_copy_from_page_vector(pages, buf, 0, ret);
4194
4195out_req:
4196 ceph_osdc_put_request(req);
Alex Elder788e2df2013-01-17 12:25:27 -06004197 return ret;
4198}
4199
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004200/*
Alex Elder662518b2013-05-06 09:51:29 -05004201 * Read the complete header for the given rbd device. On successful
4202 * return, the rbd_dev->header field will contain up-to-date
4203 * information about the image.
Alex Elder4156d992012-08-02 11:29:46 -05004204 */
Alex Elder99a41eb2013-05-06 09:51:30 -05004205static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev)
Alex Elder4156d992012-08-02 11:29:46 -05004206{
4207 struct rbd_image_header_ondisk *ondisk = NULL;
4208 u32 snap_count = 0;
4209 u64 names_size = 0;
4210 u32 want_count;
4211 int ret;
4212
4213 /*
4214 * The complete header will include an array of its 64-bit
4215 * snapshot ids, followed by the names of those snapshots as
4216 * a contiguous block of NUL-terminated strings. Note that
4217 * the number of snapshots could change by the time we read
4218 * it in, in which case we re-read it.
4219 */
4220 do {
4221 size_t size;
4222
4223 kfree(ondisk);
4224
4225 size = sizeof (*ondisk);
4226 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
4227 size += names_size;
4228 ondisk = kmalloc(size, GFP_KERNEL);
4229 if (!ondisk)
Alex Elder662518b2013-05-06 09:51:29 -05004230 return -ENOMEM;
Alex Elder4156d992012-08-02 11:29:46 -05004231
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004232 ret = rbd_obj_read_sync(rbd_dev, &rbd_dev->header_oid,
4233 &rbd_dev->header_oloc, ondisk, size);
Alex Elder4156d992012-08-02 11:29:46 -05004234 if (ret < 0)
Alex Elder662518b2013-05-06 09:51:29 -05004235 goto out;
Alex Elderc0cd10db2013-04-26 09:43:47 -05004236 if ((size_t)ret < size) {
Alex Elder4156d992012-08-02 11:29:46 -05004237 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05004238 rbd_warn(rbd_dev, "short header read (want %zd got %d)",
4239 size, ret);
Alex Elder662518b2013-05-06 09:51:29 -05004240 goto out;
Alex Elder4156d992012-08-02 11:29:46 -05004241 }
4242 if (!rbd_dev_ondisk_valid(ondisk)) {
4243 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05004244 rbd_warn(rbd_dev, "invalid header");
Alex Elder662518b2013-05-06 09:51:29 -05004245 goto out;
Alex Elder4156d992012-08-02 11:29:46 -05004246 }
4247
4248 names_size = le64_to_cpu(ondisk->snap_names_len);
4249 want_count = snap_count;
4250 snap_count = le32_to_cpu(ondisk->snap_count);
4251 } while (snap_count != want_count);
4252
Alex Elder662518b2013-05-06 09:51:29 -05004253 ret = rbd_header_from_disk(rbd_dev, ondisk);
4254out:
Alex Elder4156d992012-08-02 11:29:46 -05004255 kfree(ondisk);
4256
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004257 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004258}
4259
Alex Elder15228ed2013-05-01 12:43:03 -05004260/*
4261 * Clear the rbd device's EXISTS flag if the snapshot it's mapped to
4262 * has disappeared from the (just updated) snapshot context.
4263 */
4264static void rbd_exists_validate(struct rbd_device *rbd_dev)
4265{
4266 u64 snap_id;
4267
4268 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags))
4269 return;
4270
4271 snap_id = rbd_dev->spec->snap_id;
4272 if (snap_id == CEPH_NOSNAP)
4273 return;
4274
4275 if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX)
4276 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
4277}
4278
Josh Durgin98752012013-08-29 17:26:31 -07004279static void rbd_dev_update_size(struct rbd_device *rbd_dev)
4280{
4281 sector_t size;
Josh Durgin98752012013-08-29 17:26:31 -07004282
4283 /*
Ilya Dryomov811c6682016-04-15 16:22:16 +02004284 * If EXISTS is not set, rbd_dev->disk may be NULL, so don't
4285 * try to update its size. If REMOVING is set, updating size
4286 * is just useless work since the device can't be opened.
Josh Durgin98752012013-08-29 17:26:31 -07004287 */
Ilya Dryomov811c6682016-04-15 16:22:16 +02004288 if (test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags) &&
4289 !test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) {
Josh Durgin98752012013-08-29 17:26:31 -07004290 size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE;
4291 dout("setting size to %llu sectors", (unsigned long long)size);
4292 set_capacity(rbd_dev->disk, size);
4293 revalidate_disk(rbd_dev->disk);
4294 }
4295}
4296
Alex Eldercc4a38bd2013-04-30 00:44:33 -05004297static int rbd_dev_refresh(struct rbd_device *rbd_dev)
Alex Elder1fe5e992012-07-25 09:32:41 -05004298{
Alex Eldere627db02013-05-06 07:40:30 -05004299 u64 mapping_size;
Alex Elder1fe5e992012-07-25 09:32:41 -05004300 int ret;
4301
Alex Eldercfbf6372013-05-31 17:40:45 -05004302 down_write(&rbd_dev->header_rwsem);
Alex Elder3b5cf2a2013-05-29 11:18:59 -05004303 mapping_size = rbd_dev->mapping.size;
Ilya Dryomova720ae02014-07-23 17:11:19 +04004304
4305 ret = rbd_dev_header_info(rbd_dev);
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04004306 if (ret)
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004307 goto out;
Alex Elder15228ed2013-05-01 12:43:03 -05004308
Ilya Dryomove8f59b52014-07-24 10:42:13 +04004309 /*
4310 * If there is a parent, see if it has disappeared due to the
4311 * mapped image getting flattened.
4312 */
4313 if (rbd_dev->parent) {
4314 ret = rbd_dev_v2_parent_info(rbd_dev);
4315 if (ret)
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004316 goto out;
Ilya Dryomove8f59b52014-07-24 10:42:13 +04004317 }
4318
Ilya Dryomov5ff11082014-07-23 17:11:21 +04004319 if (rbd_dev->spec->snap_id == CEPH_NOSNAP) {
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004320 rbd_dev->mapping.size = rbd_dev->header.image_size;
Ilya Dryomov5ff11082014-07-23 17:11:21 +04004321 } else {
4322 /* validate mapped snapshot's EXISTS flag */
4323 rbd_exists_validate(rbd_dev);
4324 }
Alex Elder15228ed2013-05-01 12:43:03 -05004325
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004326out:
Alex Eldercfbf6372013-05-31 17:40:45 -05004327 up_write(&rbd_dev->header_rwsem);
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004328 if (!ret && mapping_size != rbd_dev->mapping.size)
Josh Durgin98752012013-08-29 17:26:31 -07004329 rbd_dev_update_size(rbd_dev);
Alex Elder1fe5e992012-07-25 09:32:41 -05004330
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004331 return ret;
Alex Elder1fe5e992012-07-25 09:32:41 -05004332}
4333
Christoph Hellwigd6296d32017-05-01 10:19:08 -06004334static int rbd_init_request(struct blk_mq_tag_set *set, struct request *rq,
4335 unsigned int hctx_idx, unsigned int numa_node)
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004336{
4337 struct work_struct *work = blk_mq_rq_to_pdu(rq);
4338
4339 INIT_WORK(work, rbd_queue_workfn);
4340 return 0;
4341}
4342
Eric Biggersf363b082017-03-30 13:39:16 -07004343static const struct blk_mq_ops rbd_mq_ops = {
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004344 .queue_rq = rbd_queue_rq,
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004345 .init_request = rbd_init_request,
4346};
4347
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004348static int rbd_init_disk(struct rbd_device *rbd_dev)
4349{
4350 struct gendisk *disk;
4351 struct request_queue *q;
Alex Elder593a9e72012-02-07 12:03:37 -06004352 u64 segment_size;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004353 int err;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004354
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004355 /* create gendisk info */
Ilya Dryomov7e513d42013-12-16 19:26:32 +02004356 disk = alloc_disk(single_major ?
4357 (1 << RBD_SINGLE_MAJOR_PART_SHIFT) :
4358 RBD_MINORS_PER_MAJOR);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004359 if (!disk)
Alex Elder1fcdb8a2012-08-29 17:11:06 -05004360 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004361
Alex Elderf0f8cef2012-01-29 13:57:44 -06004362 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
Alex Elderde71a292012-07-03 16:01:19 -05004363 rbd_dev->dev_id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004364 disk->major = rbd_dev->major;
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004365 disk->first_minor = rbd_dev->minor;
Ilya Dryomov7e513d42013-12-16 19:26:32 +02004366 if (single_major)
4367 disk->flags |= GENHD_FL_EXT_DEVT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004368 disk->fops = &rbd_bd_ops;
4369 disk->private_data = rbd_dev;
4370
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004371 memset(&rbd_dev->tag_set, 0, sizeof(rbd_dev->tag_set));
4372 rbd_dev->tag_set.ops = &rbd_mq_ops;
Ilya Dryomovb5584182015-06-23 16:21:19 +03004373 rbd_dev->tag_set.queue_depth = rbd_dev->opts->queue_depth;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004374 rbd_dev->tag_set.numa_node = NUMA_NO_NODE;
Ilya Dryomovb5584182015-06-23 16:21:19 +03004375 rbd_dev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004376 rbd_dev->tag_set.nr_hw_queues = 1;
4377 rbd_dev->tag_set.cmd_size = sizeof(struct work_struct);
4378
4379 err = blk_mq_alloc_tag_set(&rbd_dev->tag_set);
4380 if (err)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004381 goto out_disk;
Josh Durgin029bcbd2011-07-22 11:35:23 -07004382
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004383 q = blk_mq_init_queue(&rbd_dev->tag_set);
4384 if (IS_ERR(q)) {
4385 err = PTR_ERR(q);
4386 goto out_tag_set;
4387 }
4388
Ilya Dryomovd8a2c892015-03-24 16:15:17 +03004389 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
4390 /* QUEUE_FLAG_ADD_RANDOM is off by default for blk-mq */
Alex Elder593a9e72012-02-07 12:03:37 -06004391
Josh Durgin029bcbd2011-07-22 11:35:23 -07004392 /* set io sizes to object size */
Alex Elder593a9e72012-02-07 12:03:37 -06004393 segment_size = rbd_obj_bytes(&rbd_dev->header);
4394 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
Ilya Dryomov0d9fde42015-10-07 16:09:35 +02004395 q->limits.max_sectors = queue_max_hw_sectors(q);
Ilya Dryomovd3834fe2015-06-12 19:19:02 +03004396 blk_queue_max_segments(q, segment_size / SECTOR_SIZE);
Alex Elder593a9e72012-02-07 12:03:37 -06004397 blk_queue_max_segment_size(q, segment_size);
4398 blk_queue_io_min(q, segment_size);
4399 blk_queue_io_opt(q, segment_size);
Josh Durgin029bcbd2011-07-22 11:35:23 -07004400
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004401 /* enable the discard support */
4402 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
4403 q->limits.discard_granularity = segment_size;
4404 q->limits.discard_alignment = segment_size;
Jens Axboe2bb4cd52015-07-14 08:15:12 -06004405 blk_queue_max_discard_sectors(q, segment_size / SECTOR_SIZE);
Ilya Dryomov6ac56952017-05-22 19:59:24 +02004406 blk_queue_max_write_zeroes_sectors(q, segment_size / SECTOR_SIZE);
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004407
Ronny Hegewaldbae818e2015-10-15 18:50:46 +00004408 if (!ceph_test_opt(rbd_dev->rbd_client->client, NOCRC))
Jan Karadc3b17c2017-02-02 15:56:50 +01004409 q->backing_dev_info->capabilities |= BDI_CAP_STABLE_WRITES;
Ronny Hegewaldbae818e2015-10-15 18:50:46 +00004410
Ilya Dryomov5769ed02017-04-13 12:17:38 +02004411 /*
4412 * disk_release() expects a queue ref from add_disk() and will
4413 * put it. Hold an extra ref until add_disk() is called.
4414 */
4415 WARN_ON(!blk_get_queue(q));
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004416 disk->queue = q;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004417 q->queuedata = rbd_dev;
4418
4419 rbd_dev->disk = disk;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004420
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004421 return 0;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004422out_tag_set:
4423 blk_mq_free_tag_set(&rbd_dev->tag_set);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004424out_disk:
4425 put_disk(disk);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004426 return err;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004427}
4428
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004429/*
4430 sysfs
4431*/
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004432
Alex Elder593a9e72012-02-07 12:03:37 -06004433static struct rbd_device *dev_to_rbd_dev(struct device *dev)
4434{
4435 return container_of(dev, struct rbd_device, dev);
4436}
4437
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004438static ssize_t rbd_size_show(struct device *dev,
4439 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004440{
Alex Elder593a9e72012-02-07 12:03:37 -06004441 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004442
Alex Elderfc71d832013-04-26 15:44:36 -05004443 return sprintf(buf, "%llu\n",
4444 (unsigned long long)rbd_dev->mapping.size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004445}
4446
Alex Elder34b13182012-07-13 20:35:12 -05004447/*
4448 * Note this shows the features for whatever's mapped, which is not
4449 * necessarily the base image.
4450 */
4451static ssize_t rbd_features_show(struct device *dev,
4452 struct device_attribute *attr, char *buf)
4453{
4454 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4455
4456 return sprintf(buf, "0x%016llx\n",
Alex Elderfc71d832013-04-26 15:44:36 -05004457 (unsigned long long)rbd_dev->mapping.features);
Alex Elder34b13182012-07-13 20:35:12 -05004458}
4459
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004460static ssize_t rbd_major_show(struct device *dev,
4461 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004462{
Alex Elder593a9e72012-02-07 12:03:37 -06004463 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004464
Alex Elderfc71d832013-04-26 15:44:36 -05004465 if (rbd_dev->major)
4466 return sprintf(buf, "%d\n", rbd_dev->major);
4467
4468 return sprintf(buf, "(none)\n");
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004469}
Alex Elderfc71d832013-04-26 15:44:36 -05004470
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004471static ssize_t rbd_minor_show(struct device *dev,
4472 struct device_attribute *attr, char *buf)
4473{
4474 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4475
4476 return sprintf(buf, "%d\n", rbd_dev->minor);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004477}
4478
Ilya Dryomov005a07bf2016-08-18 18:38:43 +02004479static ssize_t rbd_client_addr_show(struct device *dev,
4480 struct device_attribute *attr, char *buf)
4481{
4482 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4483 struct ceph_entity_addr *client_addr =
4484 ceph_client_addr(rbd_dev->rbd_client->client);
4485
4486 return sprintf(buf, "%pISpc/%u\n", &client_addr->in_addr,
4487 le32_to_cpu(client_addr->nonce));
4488}
4489
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004490static ssize_t rbd_client_id_show(struct device *dev,
4491 struct device_attribute *attr, char *buf)
4492{
Alex Elder593a9e72012-02-07 12:03:37 -06004493 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004494
Alex Elder1dbb4392012-01-24 10:08:37 -06004495 return sprintf(buf, "client%lld\n",
Ilya Dryomov033268a2016-08-12 14:59:58 +02004496 ceph_client_gid(rbd_dev->rbd_client->client));
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004497}
4498
Mike Christie267fb902016-08-18 18:38:43 +02004499static ssize_t rbd_cluster_fsid_show(struct device *dev,
4500 struct device_attribute *attr, char *buf)
4501{
4502 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4503
4504 return sprintf(buf, "%pU\n", &rbd_dev->rbd_client->client->fsid);
4505}
4506
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02004507static ssize_t rbd_config_info_show(struct device *dev,
4508 struct device_attribute *attr, char *buf)
4509{
4510 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4511
4512 return sprintf(buf, "%s\n", rbd_dev->config_info);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004513}
4514
4515static ssize_t rbd_pool_show(struct device *dev,
4516 struct device_attribute *attr, char *buf)
4517{
Alex Elder593a9e72012-02-07 12:03:37 -06004518 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004519
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004520 return sprintf(buf, "%s\n", rbd_dev->spec->pool_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004521}
4522
Alex Elder9bb2f332012-07-12 10:46:35 -05004523static ssize_t rbd_pool_id_show(struct device *dev,
4524 struct device_attribute *attr, char *buf)
4525{
4526 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4527
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004528 return sprintf(buf, "%llu\n",
Alex Elderfc71d832013-04-26 15:44:36 -05004529 (unsigned long long) rbd_dev->spec->pool_id);
Alex Elder9bb2f332012-07-12 10:46:35 -05004530}
4531
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004532static ssize_t rbd_name_show(struct device *dev,
4533 struct device_attribute *attr, char *buf)
4534{
Alex Elder593a9e72012-02-07 12:03:37 -06004535 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004536
Alex Eldera92ffdf2012-10-30 19:40:33 -05004537 if (rbd_dev->spec->image_name)
4538 return sprintf(buf, "%s\n", rbd_dev->spec->image_name);
4539
4540 return sprintf(buf, "(unknown)\n");
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004541}
4542
Alex Elder589d30e2012-07-10 20:30:11 -05004543static ssize_t rbd_image_id_show(struct device *dev,
4544 struct device_attribute *attr, char *buf)
4545{
4546 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4547
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004548 return sprintf(buf, "%s\n", rbd_dev->spec->image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05004549}
4550
Alex Elder34b13182012-07-13 20:35:12 -05004551/*
4552 * Shows the name of the currently-mapped snapshot (or
4553 * RBD_SNAP_HEAD_NAME for the base image).
4554 */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004555static ssize_t rbd_snap_show(struct device *dev,
4556 struct device_attribute *attr,
4557 char *buf)
4558{
Alex Elder593a9e72012-02-07 12:03:37 -06004559 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004560
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004561 return sprintf(buf, "%s\n", rbd_dev->spec->snap_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004562}
4563
Mike Christie92a58672016-08-18 18:38:44 +02004564static ssize_t rbd_snap_id_show(struct device *dev,
4565 struct device_attribute *attr, char *buf)
4566{
4567 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4568
4569 return sprintf(buf, "%llu\n", rbd_dev->spec->snap_id);
4570}
4571
Alex Elder86b00e02012-10-25 23:34:42 -05004572/*
Ilya Dryomovff961282014-07-22 21:53:07 +04004573 * For a v2 image, shows the chain of parent images, separated by empty
4574 * lines. For v1 images or if there is no parent, shows "(no parent
4575 * image)".
Alex Elder86b00e02012-10-25 23:34:42 -05004576 */
4577static ssize_t rbd_parent_show(struct device *dev,
Ilya Dryomovff961282014-07-22 21:53:07 +04004578 struct device_attribute *attr,
4579 char *buf)
Alex Elder86b00e02012-10-25 23:34:42 -05004580{
4581 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Ilya Dryomovff961282014-07-22 21:53:07 +04004582 ssize_t count = 0;
Alex Elder86b00e02012-10-25 23:34:42 -05004583
Ilya Dryomovff961282014-07-22 21:53:07 +04004584 if (!rbd_dev->parent)
Alex Elder86b00e02012-10-25 23:34:42 -05004585 return sprintf(buf, "(no parent image)\n");
4586
Ilya Dryomovff961282014-07-22 21:53:07 +04004587 for ( ; rbd_dev->parent; rbd_dev = rbd_dev->parent) {
4588 struct rbd_spec *spec = rbd_dev->parent_spec;
Alex Elder86b00e02012-10-25 23:34:42 -05004589
Ilya Dryomovff961282014-07-22 21:53:07 +04004590 count += sprintf(&buf[count], "%s"
4591 "pool_id %llu\npool_name %s\n"
4592 "image_id %s\nimage_name %s\n"
4593 "snap_id %llu\nsnap_name %s\n"
4594 "overlap %llu\n",
4595 !count ? "" : "\n", /* first? */
4596 spec->pool_id, spec->pool_name,
4597 spec->image_id, spec->image_name ?: "(unknown)",
4598 spec->snap_id, spec->snap_name,
4599 rbd_dev->parent_overlap);
4600 }
Alex Elder86b00e02012-10-25 23:34:42 -05004601
Ilya Dryomovff961282014-07-22 21:53:07 +04004602 return count;
Alex Elder86b00e02012-10-25 23:34:42 -05004603}
4604
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004605static ssize_t rbd_image_refresh(struct device *dev,
4606 struct device_attribute *attr,
4607 const char *buf,
4608 size_t size)
4609{
Alex Elder593a9e72012-02-07 12:03:37 -06004610 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Alex Elderb8136232012-07-25 09:32:41 -05004611 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004612
Alex Eldercc4a38bd2013-04-30 00:44:33 -05004613 ret = rbd_dev_refresh(rbd_dev);
Alex Eldere627db02013-05-06 07:40:30 -05004614 if (ret)
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04004615 return ret;
Alex Elderb8136232012-07-25 09:32:41 -05004616
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04004617 return size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004618}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004619
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004620static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
Alex Elder34b13182012-07-13 20:35:12 -05004621static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004622static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004623static DEVICE_ATTR(minor, S_IRUGO, rbd_minor_show, NULL);
Ilya Dryomov005a07bf2016-08-18 18:38:43 +02004624static DEVICE_ATTR(client_addr, S_IRUGO, rbd_client_addr_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004625static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
Mike Christie267fb902016-08-18 18:38:43 +02004626static DEVICE_ATTR(cluster_fsid, S_IRUGO, rbd_cluster_fsid_show, NULL);
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02004627static DEVICE_ATTR(config_info, S_IRUSR, rbd_config_info_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004628static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
Alex Elder9bb2f332012-07-12 10:46:35 -05004629static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004630static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
Alex Elder589d30e2012-07-10 20:30:11 -05004631static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004632static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
4633static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
Mike Christie92a58672016-08-18 18:38:44 +02004634static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL);
Alex Elder86b00e02012-10-25 23:34:42 -05004635static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004636
4637static struct attribute *rbd_attrs[] = {
4638 &dev_attr_size.attr,
Alex Elder34b13182012-07-13 20:35:12 -05004639 &dev_attr_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004640 &dev_attr_major.attr,
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004641 &dev_attr_minor.attr,
Ilya Dryomov005a07bf2016-08-18 18:38:43 +02004642 &dev_attr_client_addr.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004643 &dev_attr_client_id.attr,
Mike Christie267fb902016-08-18 18:38:43 +02004644 &dev_attr_cluster_fsid.attr,
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02004645 &dev_attr_config_info.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004646 &dev_attr_pool.attr,
Alex Elder9bb2f332012-07-12 10:46:35 -05004647 &dev_attr_pool_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004648 &dev_attr_name.attr,
Alex Elder589d30e2012-07-10 20:30:11 -05004649 &dev_attr_image_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004650 &dev_attr_current_snap.attr,
Mike Christie92a58672016-08-18 18:38:44 +02004651 &dev_attr_snap_id.attr,
Alex Elder86b00e02012-10-25 23:34:42 -05004652 &dev_attr_parent.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004653 &dev_attr_refresh.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004654 NULL
4655};
4656
4657static struct attribute_group rbd_attr_group = {
4658 .attrs = rbd_attrs,
4659};
4660
4661static const struct attribute_group *rbd_attr_groups[] = {
4662 &rbd_attr_group,
4663 NULL
4664};
4665
Ilya Dryomov6cac4692015-10-16 20:11:25 +02004666static void rbd_dev_release(struct device *dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004667
Bhumika Goyalb9942bc2017-02-11 12:14:38 +05304668static const struct device_type rbd_device_type = {
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004669 .name = "rbd",
4670 .groups = rbd_attr_groups,
Ilya Dryomov6cac4692015-10-16 20:11:25 +02004671 .release = rbd_dev_release,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004672};
4673
Alex Elder8b8fb992012-10-26 17:25:24 -05004674static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)
4675{
4676 kref_get(&spec->kref);
4677
4678 return spec;
4679}
4680
4681static void rbd_spec_free(struct kref *kref);
4682static void rbd_spec_put(struct rbd_spec *spec)
4683{
4684 if (spec)
4685 kref_put(&spec->kref, rbd_spec_free);
4686}
4687
4688static struct rbd_spec *rbd_spec_alloc(void)
4689{
4690 struct rbd_spec *spec;
4691
4692 spec = kzalloc(sizeof (*spec), GFP_KERNEL);
4693 if (!spec)
4694 return NULL;
Ilya Dryomov04077592014-07-23 17:11:20 +04004695
4696 spec->pool_id = CEPH_NOPOOL;
4697 spec->snap_id = CEPH_NOSNAP;
Alex Elder8b8fb992012-10-26 17:25:24 -05004698 kref_init(&spec->kref);
4699
Alex Elder8b8fb992012-10-26 17:25:24 -05004700 return spec;
4701}
4702
4703static void rbd_spec_free(struct kref *kref)
4704{
4705 struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref);
4706
4707 kfree(spec->pool_name);
4708 kfree(spec->image_id);
4709 kfree(spec->image_name);
4710 kfree(spec->snap_name);
4711 kfree(spec);
4712}
4713
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004714static void rbd_dev_free(struct rbd_device *rbd_dev)
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004715{
Ilya Dryomov99d16942016-08-12 16:11:41 +02004716 WARN_ON(rbd_dev->watch_state != RBD_WATCH_STATE_UNREGISTERED);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004717 WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_UNLOCKED);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004718
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004719 ceph_oid_destroy(&rbd_dev->header_oid);
Ilya Dryomov6b6dddb2016-08-05 16:15:38 +02004720 ceph_oloc_destroy(&rbd_dev->header_oloc);
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02004721 kfree(rbd_dev->config_info);
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004722
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004723 rbd_put_client(rbd_dev->rbd_client);
4724 rbd_spec_put(rbd_dev->spec);
4725 kfree(rbd_dev->opts);
4726 kfree(rbd_dev);
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004727}
4728
4729static void rbd_dev_release(struct device *dev)
4730{
4731 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4732 bool need_put = !!rbd_dev->opts;
4733
4734 if (need_put) {
4735 destroy_workqueue(rbd_dev->task_wq);
4736 ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id);
4737 }
4738
4739 rbd_dev_free(rbd_dev);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004740
4741 /*
4742 * This is racy, but way better than putting module outside of
4743 * the release callback. The race window is pretty small, so
4744 * doing something similar to dm (dm-builtin.c) is overkill.
4745 */
4746 if (need_put)
4747 module_put(THIS_MODULE);
4748}
4749
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004750static struct rbd_device *__rbd_dev_create(struct rbd_client *rbdc,
4751 struct rbd_spec *spec)
Alex Elderc53d5892012-10-25 23:34:42 -05004752{
4753 struct rbd_device *rbd_dev;
4754
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004755 rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL);
Alex Elderc53d5892012-10-25 23:34:42 -05004756 if (!rbd_dev)
4757 return NULL;
4758
4759 spin_lock_init(&rbd_dev->lock);
4760 INIT_LIST_HEAD(&rbd_dev->node);
Alex Elderc53d5892012-10-25 23:34:42 -05004761 init_rwsem(&rbd_dev->header_rwsem);
4762
Ilya Dryomov7e973322017-01-25 18:16:22 +01004763 rbd_dev->header.data_pool_id = CEPH_NOPOOL;
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004764 ceph_oid_init(&rbd_dev->header_oid);
Ilya Dryomov431a02c2017-01-25 18:16:21 +01004765 rbd_dev->header_oloc.pool = spec->pool_id;
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004766
Ilya Dryomov99d16942016-08-12 16:11:41 +02004767 mutex_init(&rbd_dev->watch_mutex);
4768 rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED;
4769 INIT_DELAYED_WORK(&rbd_dev->watch_dwork, rbd_reregister_watch);
4770
Ilya Dryomoved95b212016-08-12 16:40:02 +02004771 init_rwsem(&rbd_dev->lock_rwsem);
4772 rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED;
4773 INIT_WORK(&rbd_dev->acquired_lock_work, rbd_notify_acquired_lock);
4774 INIT_WORK(&rbd_dev->released_lock_work, rbd_notify_released_lock);
4775 INIT_DELAYED_WORK(&rbd_dev->lock_dwork, rbd_acquire_lock);
4776 INIT_WORK(&rbd_dev->unlock_work, rbd_release_lock_work);
4777 init_waitqueue_head(&rbd_dev->lock_waitq);
4778
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004779 rbd_dev->dev.bus = &rbd_bus_type;
4780 rbd_dev->dev.type = &rbd_device_type;
4781 rbd_dev->dev.parent = &rbd_root_dev;
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004782 device_initialize(&rbd_dev->dev);
4783
Alex Elderc53d5892012-10-25 23:34:42 -05004784 rbd_dev->rbd_client = rbdc;
Ilya Dryomovd1475432015-06-22 13:24:48 +03004785 rbd_dev->spec = spec;
Alex Elder0903e872012-11-14 12:25:19 -06004786
Alex Elderc53d5892012-10-25 23:34:42 -05004787 return rbd_dev;
4788}
4789
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004790/*
4791 * Create a mapping rbd_dev.
4792 */
4793static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,
4794 struct rbd_spec *spec,
4795 struct rbd_options *opts)
4796{
4797 struct rbd_device *rbd_dev;
4798
4799 rbd_dev = __rbd_dev_create(rbdc, spec);
4800 if (!rbd_dev)
4801 return NULL;
4802
4803 rbd_dev->opts = opts;
4804
4805 /* get an id and fill in device name */
4806 rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0,
4807 minor_to_rbd_dev_id(1 << MINORBITS),
4808 GFP_KERNEL);
4809 if (rbd_dev->dev_id < 0)
4810 goto fail_rbd_dev;
4811
4812 sprintf(rbd_dev->name, RBD_DRV_NAME "%d", rbd_dev->dev_id);
4813 rbd_dev->task_wq = alloc_ordered_workqueue("%s-tasks", WQ_MEM_RECLAIM,
4814 rbd_dev->name);
4815 if (!rbd_dev->task_wq)
4816 goto fail_dev_id;
4817
4818 /* we have a ref from do_rbd_add() */
4819 __module_get(THIS_MODULE);
4820
4821 dout("%s rbd_dev %p dev_id %d\n", __func__, rbd_dev, rbd_dev->dev_id);
4822 return rbd_dev;
4823
4824fail_dev_id:
4825 ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id);
4826fail_rbd_dev:
4827 rbd_dev_free(rbd_dev);
4828 return NULL;
4829}
4830
Alex Elderc53d5892012-10-25 23:34:42 -05004831static void rbd_dev_destroy(struct rbd_device *rbd_dev)
4832{
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004833 if (rbd_dev)
4834 put_device(&rbd_dev->dev);
Alex Elderc53d5892012-10-25 23:34:42 -05004835}
4836
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004837/*
Alex Elder9d475de2012-07-03 16:01:19 -05004838 * Get the size and object order for an image snapshot, or if
4839 * snap_id is CEPH_NOSNAP, gets this information for the base
4840 * image.
4841 */
4842static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
4843 u8 *order, u64 *snap_size)
4844{
4845 __le64 snapid = cpu_to_le64(snap_id);
4846 int ret;
4847 struct {
4848 u8 order;
4849 __le64 size;
4850 } __attribute__ ((packed)) size_buf = { 0 };
4851
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004852 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
4853 &rbd_dev->header_oloc, "get_size",
4854 &snapid, sizeof(snapid),
4855 &size_buf, sizeof(size_buf));
Alex Elder36be9a72013-01-19 00:30:28 -06004856 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder9d475de2012-07-03 16:01:19 -05004857 if (ret < 0)
4858 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05004859 if (ret < sizeof (size_buf))
4860 return -ERANGE;
Alex Elder9d475de2012-07-03 16:01:19 -05004861
Josh Durginc3545572013-08-28 17:08:10 -07004862 if (order) {
Alex Elderc86f86e2013-04-25 15:09:41 -05004863 *order = size_buf.order;
Josh Durginc3545572013-08-28 17:08:10 -07004864 dout(" order %u", (unsigned int)*order);
4865 }
Alex Elder9d475de2012-07-03 16:01:19 -05004866 *snap_size = le64_to_cpu(size_buf.size);
4867
Josh Durginc3545572013-08-28 17:08:10 -07004868 dout(" snap_id 0x%016llx snap_size = %llu\n",
4869 (unsigned long long)snap_id,
Alex Elder57385b52013-04-21 12:14:45 -05004870 (unsigned long long)*snap_size);
Alex Elder9d475de2012-07-03 16:01:19 -05004871
4872 return 0;
4873}
4874
4875static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
4876{
4877 return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
4878 &rbd_dev->header.obj_order,
4879 &rbd_dev->header.image_size);
4880}
4881
Alex Elder1e130192012-07-03 16:01:19 -05004882static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
4883{
4884 void *reply_buf;
4885 int ret;
4886 void *p;
4887
4888 reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL);
4889 if (!reply_buf)
4890 return -ENOMEM;
4891
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004892 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
4893 &rbd_dev->header_oloc, "get_object_prefix",
4894 NULL, 0, reply_buf, RBD_OBJ_PREFIX_LEN_MAX);
Alex Elder36be9a72013-01-19 00:30:28 -06004895 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder1e130192012-07-03 16:01:19 -05004896 if (ret < 0)
4897 goto out;
4898
4899 p = reply_buf;
4900 rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p,
Alex Elder57385b52013-04-21 12:14:45 -05004901 p + ret, NULL, GFP_NOIO);
4902 ret = 0;
Alex Elder1e130192012-07-03 16:01:19 -05004903
4904 if (IS_ERR(rbd_dev->header.object_prefix)) {
4905 ret = PTR_ERR(rbd_dev->header.object_prefix);
4906 rbd_dev->header.object_prefix = NULL;
4907 } else {
4908 dout(" object_prefix = %s\n", rbd_dev->header.object_prefix);
4909 }
Alex Elder1e130192012-07-03 16:01:19 -05004910out:
4911 kfree(reply_buf);
4912
4913 return ret;
4914}
4915
Alex Elderb1b54022012-07-03 16:01:19 -05004916static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
4917 u64 *snap_features)
4918{
4919 __le64 snapid = cpu_to_le64(snap_id);
4920 struct {
4921 __le64 features;
4922 __le64 incompat;
Alex Elder41579762013-04-21 12:14:45 -05004923 } __attribute__ ((packed)) features_buf = { 0 };
Ilya Dryomovd3767f02016-04-13 14:15:50 +02004924 u64 unsup;
Alex Elderb1b54022012-07-03 16:01:19 -05004925 int ret;
4926
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004927 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
4928 &rbd_dev->header_oloc, "get_features",
4929 &snapid, sizeof(snapid),
4930 &features_buf, sizeof(features_buf));
Alex Elder36be9a72013-01-19 00:30:28 -06004931 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderb1b54022012-07-03 16:01:19 -05004932 if (ret < 0)
4933 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05004934 if (ret < sizeof (features_buf))
4935 return -ERANGE;
Alex Elderd8891402012-10-09 13:50:17 -07004936
Ilya Dryomovd3767f02016-04-13 14:15:50 +02004937 unsup = le64_to_cpu(features_buf.incompat) & ~RBD_FEATURES_SUPPORTED;
4938 if (unsup) {
4939 rbd_warn(rbd_dev, "image uses unsupported features: 0x%llx",
4940 unsup);
Alex Elderb8f5c6e2012-11-01 08:39:26 -05004941 return -ENXIO;
Ilya Dryomovd3767f02016-04-13 14:15:50 +02004942 }
Alex Elderd8891402012-10-09 13:50:17 -07004943
Alex Elderb1b54022012-07-03 16:01:19 -05004944 *snap_features = le64_to_cpu(features_buf.features);
4945
4946 dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
Alex Elder57385b52013-04-21 12:14:45 -05004947 (unsigned long long)snap_id,
4948 (unsigned long long)*snap_features,
4949 (unsigned long long)le64_to_cpu(features_buf.incompat));
Alex Elderb1b54022012-07-03 16:01:19 -05004950
4951 return 0;
4952}
4953
4954static int rbd_dev_v2_features(struct rbd_device *rbd_dev)
4955{
4956 return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP,
4957 &rbd_dev->header.features);
4958}
4959
Alex Elder86b00e02012-10-25 23:34:42 -05004960static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev)
4961{
4962 struct rbd_spec *parent_spec;
4963 size_t size;
4964 void *reply_buf = NULL;
4965 __le64 snapid;
4966 void *p;
4967 void *end;
Alex Elder642a2532013-05-06 17:40:33 -05004968 u64 pool_id;
Alex Elder86b00e02012-10-25 23:34:42 -05004969 char *image_id;
Alex Elder3b5cf2a2013-05-29 11:18:59 -05004970 u64 snap_id;
Alex Elder86b00e02012-10-25 23:34:42 -05004971 u64 overlap;
Alex Elder86b00e02012-10-25 23:34:42 -05004972 int ret;
4973
4974 parent_spec = rbd_spec_alloc();
4975 if (!parent_spec)
4976 return -ENOMEM;
4977
4978 size = sizeof (__le64) + /* pool_id */
4979 sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */
4980 sizeof (__le64) + /* snap_id */
4981 sizeof (__le64); /* overlap */
4982 reply_buf = kmalloc(size, GFP_KERNEL);
4983 if (!reply_buf) {
4984 ret = -ENOMEM;
4985 goto out_err;
4986 }
4987
Ilya Dryomov4d9b67c2014-07-24 10:42:13 +04004988 snapid = cpu_to_le64(rbd_dev->spec->snap_id);
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004989 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
4990 &rbd_dev->header_oloc, "get_parent",
4991 &snapid, sizeof(snapid), reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06004992 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder86b00e02012-10-25 23:34:42 -05004993 if (ret < 0)
4994 goto out_err;
4995
Alex Elder86b00e02012-10-25 23:34:42 -05004996 p = reply_buf;
Alex Elder57385b52013-04-21 12:14:45 -05004997 end = reply_buf + ret;
4998 ret = -ERANGE;
Alex Elder642a2532013-05-06 17:40:33 -05004999 ceph_decode_64_safe(&p, end, pool_id, out_err);
Alex Elder392a9da2013-05-06 17:40:33 -05005000 if (pool_id == CEPH_NOPOOL) {
5001 /*
5002 * Either the parent never existed, or we have
5003 * record of it but the image got flattened so it no
5004 * longer has a parent. When the parent of a
5005 * layered image disappears we immediately set the
5006 * overlap to 0. The effect of this is that all new
5007 * requests will be treated as if the image had no
5008 * parent.
5009 */
5010 if (rbd_dev->parent_overlap) {
5011 rbd_dev->parent_overlap = 0;
Alex Elder392a9da2013-05-06 17:40:33 -05005012 rbd_dev_parent_put(rbd_dev);
5013 pr_info("%s: clone image has been flattened\n",
5014 rbd_dev->disk->disk_name);
5015 }
5016
Alex Elder86b00e02012-10-25 23:34:42 -05005017 goto out; /* No parent? No problem. */
Alex Elder392a9da2013-05-06 17:40:33 -05005018 }
Alex Elder86b00e02012-10-25 23:34:42 -05005019
Alex Elder0903e872012-11-14 12:25:19 -06005020 /* The ceph file layout needs to fit pool id in 32 bits */
5021
5022 ret = -EIO;
Alex Elder642a2532013-05-06 17:40:33 -05005023 if (pool_id > (u64)U32_MAX) {
Ilya Dryomov9584d502014-07-11 12:11:20 +04005024 rbd_warn(NULL, "parent pool id too large (%llu > %u)",
Alex Elder642a2532013-05-06 17:40:33 -05005025 (unsigned long long)pool_id, U32_MAX);
Alex Elder57385b52013-04-21 12:14:45 -05005026 goto out_err;
Alex Elderc0cd10db2013-04-26 09:43:47 -05005027 }
Alex Elder0903e872012-11-14 12:25:19 -06005028
Alex Elder979ed482012-11-01 08:39:26 -05005029 image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elder86b00e02012-10-25 23:34:42 -05005030 if (IS_ERR(image_id)) {
5031 ret = PTR_ERR(image_id);
5032 goto out_err;
5033 }
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005034 ceph_decode_64_safe(&p, end, snap_id, out_err);
Alex Elder86b00e02012-10-25 23:34:42 -05005035 ceph_decode_64_safe(&p, end, overlap, out_err);
5036
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005037 /*
5038 * The parent won't change (except when the clone is
5039 * flattened, already handled that). So we only need to
5040 * record the parent spec we have not already done so.
5041 */
5042 if (!rbd_dev->parent_spec) {
5043 parent_spec->pool_id = pool_id;
5044 parent_spec->image_id = image_id;
5045 parent_spec->snap_id = snap_id;
Alex Elder70cf49c2013-05-06 17:40:33 -05005046 rbd_dev->parent_spec = parent_spec;
5047 parent_spec = NULL; /* rbd_dev now owns this */
Ilya Dryomovfbba11b2014-06-27 21:46:33 +04005048 } else {
5049 kfree(image_id);
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005050 }
5051
5052 /*
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005053 * We always update the parent overlap. If it's zero we issue
5054 * a warning, as we will proceed as if there was no parent.
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005055 */
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005056 if (!overlap) {
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005057 if (parent_spec) {
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005058 /* refresh, careful to warn just once */
5059 if (rbd_dev->parent_overlap)
5060 rbd_warn(rbd_dev,
5061 "clone now standalone (overlap became 0)");
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005062 } else {
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005063 /* initial probe */
5064 rbd_warn(rbd_dev, "clone is standalone (overlap 0)");
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005065 }
Alex Elder70cf49c2013-05-06 17:40:33 -05005066 }
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005067 rbd_dev->parent_overlap = overlap;
5068
Alex Elder86b00e02012-10-25 23:34:42 -05005069out:
5070 ret = 0;
5071out_err:
5072 kfree(reply_buf);
5073 rbd_spec_put(parent_spec);
5074
5075 return ret;
5076}
5077
Alex Eldercc070d52013-04-21 12:14:45 -05005078static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev)
5079{
5080 struct {
5081 __le64 stripe_unit;
5082 __le64 stripe_count;
5083 } __attribute__ ((packed)) striping_info_buf = { 0 };
5084 size_t size = sizeof (striping_info_buf);
5085 void *p;
5086 u64 obj_size;
5087 u64 stripe_unit;
5088 u64 stripe_count;
5089 int ret;
5090
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005091 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5092 &rbd_dev->header_oloc, "get_stripe_unit_count",
5093 NULL, 0, &striping_info_buf, size);
Alex Eldercc070d52013-04-21 12:14:45 -05005094 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
5095 if (ret < 0)
5096 return ret;
5097 if (ret < size)
5098 return -ERANGE;
5099
5100 /*
5101 * We don't actually support the "fancy striping" feature
5102 * (STRIPINGV2) yet, but if the striping sizes are the
5103 * defaults the behavior is the same as before. So find
5104 * out, and only fail if the image has non-default values.
5105 */
5106 ret = -EINVAL;
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +01005107 obj_size = rbd_obj_bytes(&rbd_dev->header);
Alex Eldercc070d52013-04-21 12:14:45 -05005108 p = &striping_info_buf;
5109 stripe_unit = ceph_decode_64(&p);
5110 if (stripe_unit != obj_size) {
5111 rbd_warn(rbd_dev, "unsupported stripe unit "
5112 "(got %llu want %llu)",
5113 stripe_unit, obj_size);
5114 return -EINVAL;
5115 }
5116 stripe_count = ceph_decode_64(&p);
5117 if (stripe_count != 1) {
5118 rbd_warn(rbd_dev, "unsupported stripe count "
5119 "(got %llu want 1)", stripe_count);
5120 return -EINVAL;
5121 }
Alex Elder500d0c02013-04-26 09:43:47 -05005122 rbd_dev->header.stripe_unit = stripe_unit;
5123 rbd_dev->header.stripe_count = stripe_count;
Alex Eldercc070d52013-04-21 12:14:45 -05005124
5125 return 0;
5126}
5127
Ilya Dryomov7e973322017-01-25 18:16:22 +01005128static int rbd_dev_v2_data_pool(struct rbd_device *rbd_dev)
5129{
5130 __le64 data_pool_id;
5131 int ret;
5132
5133 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5134 &rbd_dev->header_oloc, "get_data_pool",
5135 NULL, 0, &data_pool_id, sizeof(data_pool_id));
5136 if (ret < 0)
5137 return ret;
5138 if (ret < sizeof(data_pool_id))
5139 return -EBADMSG;
5140
5141 rbd_dev->header.data_pool_id = le64_to_cpu(data_pool_id);
5142 WARN_ON(rbd_dev->header.data_pool_id == CEPH_NOPOOL);
5143 return 0;
5144}
5145
Alex Elder9e15b772012-10-30 19:40:33 -05005146static char *rbd_dev_image_name(struct rbd_device *rbd_dev)
5147{
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005148 CEPH_DEFINE_OID_ONSTACK(oid);
Alex Elder9e15b772012-10-30 19:40:33 -05005149 size_t image_id_size;
5150 char *image_id;
5151 void *p;
5152 void *end;
5153 size_t size;
5154 void *reply_buf = NULL;
5155 size_t len = 0;
5156 char *image_name = NULL;
5157 int ret;
5158
5159 rbd_assert(!rbd_dev->spec->image_name);
5160
Alex Elder69e7a022012-11-01 08:39:26 -05005161 len = strlen(rbd_dev->spec->image_id);
5162 image_id_size = sizeof (__le32) + len;
Alex Elder9e15b772012-10-30 19:40:33 -05005163 image_id = kmalloc(image_id_size, GFP_KERNEL);
5164 if (!image_id)
5165 return NULL;
5166
5167 p = image_id;
Alex Elder41579762013-04-21 12:14:45 -05005168 end = image_id + image_id_size;
Alex Elder57385b52013-04-21 12:14:45 -05005169 ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len);
Alex Elder9e15b772012-10-30 19:40:33 -05005170
5171 size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX;
5172 reply_buf = kmalloc(size, GFP_KERNEL);
5173 if (!reply_buf)
5174 goto out;
5175
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005176 ceph_oid_printf(&oid, "%s", RBD_DIRECTORY);
5177 ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc,
5178 "dir_get_name", image_id, image_id_size,
5179 reply_buf, size);
Alex Elder9e15b772012-10-30 19:40:33 -05005180 if (ret < 0)
5181 goto out;
5182 p = reply_buf;
Alex Elderf40eb342013-04-25 15:09:42 -05005183 end = reply_buf + ret;
5184
Alex Elder9e15b772012-10-30 19:40:33 -05005185 image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL);
5186 if (IS_ERR(image_name))
5187 image_name = NULL;
5188 else
5189 dout("%s: name is %s len is %zd\n", __func__, image_name, len);
5190out:
5191 kfree(reply_buf);
5192 kfree(image_id);
5193
5194 return image_name;
5195}
5196
Alex Elder2ad3d712013-04-30 00:44:33 -05005197static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
5198{
5199 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
5200 const char *snap_name;
5201 u32 which = 0;
5202
5203 /* Skip over names until we find the one we are looking for */
5204
5205 snap_name = rbd_dev->header.snap_names;
5206 while (which < snapc->num_snaps) {
5207 if (!strcmp(name, snap_name))
5208 return snapc->snaps[which];
5209 snap_name += strlen(snap_name) + 1;
5210 which++;
5211 }
5212 return CEPH_NOSNAP;
5213}
5214
5215static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
5216{
5217 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
5218 u32 which;
5219 bool found = false;
5220 u64 snap_id;
5221
5222 for (which = 0; !found && which < snapc->num_snaps; which++) {
5223 const char *snap_name;
5224
5225 snap_id = snapc->snaps[which];
5226 snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id);
Josh Durginefadc982013-08-29 19:16:42 -07005227 if (IS_ERR(snap_name)) {
5228 /* ignore no-longer existing snapshots */
5229 if (PTR_ERR(snap_name) == -ENOENT)
5230 continue;
5231 else
5232 break;
5233 }
Alex Elder2ad3d712013-04-30 00:44:33 -05005234 found = !strcmp(name, snap_name);
5235 kfree(snap_name);
5236 }
5237 return found ? snap_id : CEPH_NOSNAP;
5238}
5239
5240/*
5241 * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if
5242 * no snapshot by that name is found, or if an error occurs.
5243 */
5244static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
5245{
5246 if (rbd_dev->image_format == 1)
5247 return rbd_v1_snap_id_by_name(rbd_dev, name);
5248
5249 return rbd_v2_snap_id_by_name(rbd_dev, name);
5250}
5251
Alex Elder9e15b772012-10-30 19:40:33 -05005252/*
Ilya Dryomov04077592014-07-23 17:11:20 +04005253 * An image being mapped will have everything but the snap id.
Alex Elder9e15b772012-10-30 19:40:33 -05005254 */
Ilya Dryomov04077592014-07-23 17:11:20 +04005255static int rbd_spec_fill_snap_id(struct rbd_device *rbd_dev)
5256{
5257 struct rbd_spec *spec = rbd_dev->spec;
5258
5259 rbd_assert(spec->pool_id != CEPH_NOPOOL && spec->pool_name);
5260 rbd_assert(spec->image_id && spec->image_name);
5261 rbd_assert(spec->snap_name);
5262
5263 if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) {
5264 u64 snap_id;
5265
5266 snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name);
5267 if (snap_id == CEPH_NOSNAP)
5268 return -ENOENT;
5269
5270 spec->snap_id = snap_id;
5271 } else {
5272 spec->snap_id = CEPH_NOSNAP;
5273 }
5274
5275 return 0;
5276}
5277
5278/*
5279 * A parent image will have all ids but none of the names.
5280 *
5281 * All names in an rbd spec are dynamically allocated. It's OK if we
5282 * can't figure out the name for an image id.
5283 */
5284static int rbd_spec_fill_names(struct rbd_device *rbd_dev)
Alex Elder9e15b772012-10-30 19:40:33 -05005285{
Alex Elder2e9f7f12013-04-26 09:43:48 -05005286 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
5287 struct rbd_spec *spec = rbd_dev->spec;
5288 const char *pool_name;
5289 const char *image_name;
5290 const char *snap_name;
Alex Elder9e15b772012-10-30 19:40:33 -05005291 int ret;
5292
Ilya Dryomov04077592014-07-23 17:11:20 +04005293 rbd_assert(spec->pool_id != CEPH_NOPOOL);
5294 rbd_assert(spec->image_id);
5295 rbd_assert(spec->snap_id != CEPH_NOSNAP);
Alex Elder9e15b772012-10-30 19:40:33 -05005296
Alex Elder2e9f7f12013-04-26 09:43:48 -05005297 /* Get the pool name; we have to make our own copy of this */
Alex Elder9e15b772012-10-30 19:40:33 -05005298
Alex Elder2e9f7f12013-04-26 09:43:48 -05005299 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id);
5300 if (!pool_name) {
5301 rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id);
Alex Elder935dc892012-11-01 10:17:15 -05005302 return -EIO;
5303 }
Alex Elder2e9f7f12013-04-26 09:43:48 -05005304 pool_name = kstrdup(pool_name, GFP_KERNEL);
5305 if (!pool_name)
Alex Elder9e15b772012-10-30 19:40:33 -05005306 return -ENOMEM;
5307
5308 /* Fetch the image name; tolerate failure here */
5309
Alex Elder2e9f7f12013-04-26 09:43:48 -05005310 image_name = rbd_dev_image_name(rbd_dev);
5311 if (!image_name)
Alex Elder06ecc6c2012-11-01 10:17:15 -05005312 rbd_warn(rbd_dev, "unable to get image name");
Alex Elder9e15b772012-10-30 19:40:33 -05005313
Ilya Dryomov04077592014-07-23 17:11:20 +04005314 /* Fetch the snapshot name */
Alex Elder9e15b772012-10-30 19:40:33 -05005315
Alex Elder2e9f7f12013-04-26 09:43:48 -05005316 snap_name = rbd_snap_name(rbd_dev, spec->snap_id);
Josh Durginda6a6b62013-09-04 17:57:31 -07005317 if (IS_ERR(snap_name)) {
5318 ret = PTR_ERR(snap_name);
Alex Elder9e15b772012-10-30 19:40:33 -05005319 goto out_err;
Alex Elder2e9f7f12013-04-26 09:43:48 -05005320 }
5321
5322 spec->pool_name = pool_name;
5323 spec->image_name = image_name;
5324 spec->snap_name = snap_name;
Alex Elder9e15b772012-10-30 19:40:33 -05005325
5326 return 0;
Ilya Dryomov04077592014-07-23 17:11:20 +04005327
Alex Elder9e15b772012-10-30 19:40:33 -05005328out_err:
Alex Elder2e9f7f12013-04-26 09:43:48 -05005329 kfree(image_name);
5330 kfree(pool_name);
Alex Elder9e15b772012-10-30 19:40:33 -05005331 return ret;
5332}
5333
Alex Eldercc4a38bd2013-04-30 00:44:33 -05005334static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev)
Alex Elder35d489f2012-07-03 16:01:19 -05005335{
5336 size_t size;
5337 int ret;
5338 void *reply_buf;
5339 void *p;
5340 void *end;
5341 u64 seq;
5342 u32 snap_count;
5343 struct ceph_snap_context *snapc;
5344 u32 i;
5345
5346 /*
5347 * We'll need room for the seq value (maximum snapshot id),
5348 * snapshot count, and array of that many snapshot ids.
5349 * For now we have a fixed upper limit on the number we're
5350 * prepared to receive.
5351 */
5352 size = sizeof (__le64) + sizeof (__le32) +
5353 RBD_MAX_SNAP_COUNT * sizeof (__le64);
5354 reply_buf = kzalloc(size, GFP_KERNEL);
5355 if (!reply_buf)
5356 return -ENOMEM;
5357
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005358 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5359 &rbd_dev->header_oloc, "get_snapcontext",
5360 NULL, 0, reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06005361 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder35d489f2012-07-03 16:01:19 -05005362 if (ret < 0)
5363 goto out;
5364
Alex Elder35d489f2012-07-03 16:01:19 -05005365 p = reply_buf;
Alex Elder57385b52013-04-21 12:14:45 -05005366 end = reply_buf + ret;
5367 ret = -ERANGE;
Alex Elder35d489f2012-07-03 16:01:19 -05005368 ceph_decode_64_safe(&p, end, seq, out);
5369 ceph_decode_32_safe(&p, end, snap_count, out);
5370
5371 /*
5372 * Make sure the reported number of snapshot ids wouldn't go
5373 * beyond the end of our buffer. But before checking that,
5374 * make sure the computed size of the snapshot context we
5375 * allocate is representable in a size_t.
5376 */
5377 if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
5378 / sizeof (u64)) {
5379 ret = -EINVAL;
5380 goto out;
5381 }
5382 if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
5383 goto out;
Alex Elder468521c2013-04-26 09:43:47 -05005384 ret = 0;
Alex Elder35d489f2012-07-03 16:01:19 -05005385
Alex Elder812164f82013-04-30 00:44:32 -05005386 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
Alex Elder35d489f2012-07-03 16:01:19 -05005387 if (!snapc) {
5388 ret = -ENOMEM;
5389 goto out;
5390 }
Alex Elder35d489f2012-07-03 16:01:19 -05005391 snapc->seq = seq;
Alex Elder35d489f2012-07-03 16:01:19 -05005392 for (i = 0; i < snap_count; i++)
5393 snapc->snaps[i] = ceph_decode_64(&p);
5394
Alex Elder49ece552013-05-06 08:37:00 -05005395 ceph_put_snap_context(rbd_dev->header.snapc);
Alex Elder35d489f2012-07-03 16:01:19 -05005396 rbd_dev->header.snapc = snapc;
5397
5398 dout(" snap context seq = %llu, snap_count = %u\n",
Alex Elder57385b52013-04-21 12:14:45 -05005399 (unsigned long long)seq, (unsigned int)snap_count);
Alex Elder35d489f2012-07-03 16:01:19 -05005400out:
5401 kfree(reply_buf);
5402
Alex Elder57385b52013-04-21 12:14:45 -05005403 return ret;
Alex Elder35d489f2012-07-03 16:01:19 -05005404}
5405
Alex Elder54cac612013-04-30 00:44:33 -05005406static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
5407 u64 snap_id)
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005408{
5409 size_t size;
5410 void *reply_buf;
Alex Elder54cac612013-04-30 00:44:33 -05005411 __le64 snapid;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005412 int ret;
5413 void *p;
5414 void *end;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005415 char *snap_name;
5416
5417 size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN;
5418 reply_buf = kmalloc(size, GFP_KERNEL);
5419 if (!reply_buf)
5420 return ERR_PTR(-ENOMEM);
5421
Alex Elder54cac612013-04-30 00:44:33 -05005422 snapid = cpu_to_le64(snap_id);
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005423 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5424 &rbd_dev->header_oloc, "get_snapshot_name",
5425 &snapid, sizeof(snapid), reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06005426 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderf40eb342013-04-25 15:09:42 -05005427 if (ret < 0) {
5428 snap_name = ERR_PTR(ret);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005429 goto out;
Alex Elderf40eb342013-04-25 15:09:42 -05005430 }
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005431
5432 p = reply_buf;
Alex Elderf40eb342013-04-25 15:09:42 -05005433 end = reply_buf + ret;
Alex Eldere5c35532012-10-25 23:34:41 -05005434 snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elderf40eb342013-04-25 15:09:42 -05005435 if (IS_ERR(snap_name))
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005436 goto out;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005437
Alex Elderf40eb342013-04-25 15:09:42 -05005438 dout(" snap_id 0x%016llx snap_name = %s\n",
Alex Elder54cac612013-04-30 00:44:33 -05005439 (unsigned long long)snap_id, snap_name);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005440out:
5441 kfree(reply_buf);
5442
Alex Elderf40eb342013-04-25 15:09:42 -05005443 return snap_name;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005444}
5445
Alex Elder2df3fac2013-05-06 09:51:30 -05005446static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev)
Alex Elder117973f2012-08-31 17:29:55 -05005447{
Alex Elder2df3fac2013-05-06 09:51:30 -05005448 bool first_time = rbd_dev->header.object_prefix == NULL;
Alex Elder117973f2012-08-31 17:29:55 -05005449 int ret;
Alex Elder117973f2012-08-31 17:29:55 -05005450
Josh Durgin1617e402013-06-12 14:43:10 -07005451 ret = rbd_dev_v2_image_size(rbd_dev);
5452 if (ret)
Alex Eldercfbf6372013-05-31 17:40:45 -05005453 return ret;
Josh Durgin1617e402013-06-12 14:43:10 -07005454
Alex Elder2df3fac2013-05-06 09:51:30 -05005455 if (first_time) {
5456 ret = rbd_dev_v2_header_onetime(rbd_dev);
5457 if (ret)
Alex Eldercfbf6372013-05-31 17:40:45 -05005458 return ret;
Alex Elder2df3fac2013-05-06 09:51:30 -05005459 }
5460
Alex Eldercc4a38bd2013-04-30 00:44:33 -05005461 ret = rbd_dev_v2_snap_context(rbd_dev);
Ilya Dryomovd194cd12015-08-31 18:22:10 +03005462 if (ret && first_time) {
5463 kfree(rbd_dev->header.object_prefix);
5464 rbd_dev->header.object_prefix = NULL;
5465 }
Alex Elder117973f2012-08-31 17:29:55 -05005466
5467 return ret;
5468}
5469
Ilya Dryomova720ae02014-07-23 17:11:19 +04005470static int rbd_dev_header_info(struct rbd_device *rbd_dev)
5471{
5472 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
5473
5474 if (rbd_dev->image_format == 1)
5475 return rbd_dev_v1_header_info(rbd_dev);
5476
5477 return rbd_dev_v2_header_info(rbd_dev);
5478}
5479
Alex Elder1ddbe942012-01-29 13:57:44 -06005480/*
Alex Eldere28fff262012-02-02 08:13:30 -06005481 * Skips over white space at *buf, and updates *buf to point to the
5482 * first found non-space character (if any). Returns the length of
Alex Elder593a9e72012-02-07 12:03:37 -06005483 * the token (string of non-white space characters) found. Note
5484 * that *buf must be terminated with '\0'.
Alex Eldere28fff262012-02-02 08:13:30 -06005485 */
5486static inline size_t next_token(const char **buf)
5487{
5488 /*
5489 * These are the characters that produce nonzero for
5490 * isspace() in the "C" and "POSIX" locales.
5491 */
5492 const char *spaces = " \f\n\r\t\v";
5493
5494 *buf += strspn(*buf, spaces); /* Find start of token */
5495
5496 return strcspn(*buf, spaces); /* Return token length */
5497}
5498
5499/*
Alex Elderea3352f2012-07-09 21:04:23 -05005500 * Finds the next token in *buf, dynamically allocates a buffer big
5501 * enough to hold a copy of it, and copies the token into the new
5502 * buffer. The copy is guaranteed to be terminated with '\0'. Note
5503 * that a duplicate buffer is created even for a zero-length token.
5504 *
5505 * Returns a pointer to the newly-allocated duplicate, or a null
5506 * pointer if memory for the duplicate was not available. If
5507 * the lenp argument is a non-null pointer, the length of the token
5508 * (not including the '\0') is returned in *lenp.
5509 *
5510 * If successful, the *buf pointer will be updated to point beyond
5511 * the end of the found token.
5512 *
5513 * Note: uses GFP_KERNEL for allocation.
5514 */
5515static inline char *dup_token(const char **buf, size_t *lenp)
5516{
5517 char *dup;
5518 size_t len;
5519
5520 len = next_token(buf);
Alex Elder4caf35f2012-11-01 08:39:27 -05005521 dup = kmemdup(*buf, len + 1, GFP_KERNEL);
Alex Elderea3352f2012-07-09 21:04:23 -05005522 if (!dup)
5523 return NULL;
Alex Elderea3352f2012-07-09 21:04:23 -05005524 *(dup + len) = '\0';
5525 *buf += len;
5526
5527 if (lenp)
5528 *lenp = len;
5529
5530 return dup;
5531}
5532
5533/*
Alex Elder859c31d2012-10-25 23:34:42 -05005534 * Parse the options provided for an "rbd add" (i.e., rbd image
5535 * mapping) request. These arrive via a write to /sys/bus/rbd/add,
5536 * and the data written is passed here via a NUL-terminated buffer.
5537 * Returns 0 if successful or an error code otherwise.
Alex Elderd22f76e2012-07-12 10:46:35 -05005538 *
Alex Elder859c31d2012-10-25 23:34:42 -05005539 * The information extracted from these options is recorded in
5540 * the other parameters which return dynamically-allocated
5541 * structures:
5542 * ceph_opts
5543 * The address of a pointer that will refer to a ceph options
5544 * structure. Caller must release the returned pointer using
5545 * ceph_destroy_options() when it is no longer needed.
5546 * rbd_opts
5547 * Address of an rbd options pointer. Fully initialized by
5548 * this function; caller must release with kfree().
5549 * spec
5550 * Address of an rbd image specification pointer. Fully
5551 * initialized by this function based on parsed options.
5552 * Caller must release with rbd_spec_put().
5553 *
5554 * The options passed take this form:
5555 * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
5556 * where:
5557 * <mon_addrs>
5558 * A comma-separated list of one or more monitor addresses.
5559 * A monitor address is an ip address, optionally followed
5560 * by a port number (separated by a colon).
5561 * I.e.: ip1[:port1][,ip2[:port2]...]
5562 * <options>
5563 * A comma-separated list of ceph and/or rbd options.
5564 * <pool_name>
5565 * The name of the rados pool containing the rbd image.
5566 * <image_name>
5567 * The name of the image in that pool to map.
5568 * <snap_id>
5569 * An optional snapshot id. If provided, the mapping will
5570 * present data from the image at the time that snapshot was
5571 * created. The image head is used if no snapshot id is
5572 * provided. Snapshot mappings are always read-only.
Alex Eldera725f65e2012-02-02 08:13:30 -06005573 */
Alex Elder859c31d2012-10-25 23:34:42 -05005574static int rbd_add_parse_args(const char *buf,
Alex Elderdc79b112012-10-25 23:34:41 -05005575 struct ceph_options **ceph_opts,
Alex Elder859c31d2012-10-25 23:34:42 -05005576 struct rbd_options **opts,
5577 struct rbd_spec **rbd_spec)
Alex Eldera725f65e2012-02-02 08:13:30 -06005578{
Alex Elderd22f76e2012-07-12 10:46:35 -05005579 size_t len;
Alex Elder859c31d2012-10-25 23:34:42 -05005580 char *options;
Alex Elder0ddebc02012-10-25 23:34:41 -05005581 const char *mon_addrs;
Alex Elderecb4dc22013-04-26 09:43:47 -05005582 char *snap_name;
Alex Elder0ddebc02012-10-25 23:34:41 -05005583 size_t mon_addrs_size;
Alex Elder859c31d2012-10-25 23:34:42 -05005584 struct rbd_spec *spec = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05005585 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05005586 struct ceph_options *copts;
Alex Elderdc79b112012-10-25 23:34:41 -05005587 int ret;
Alex Eldere28fff262012-02-02 08:13:30 -06005588
5589 /* The first four tokens are required */
5590
Alex Elder7ef32142012-02-02 08:13:30 -06005591 len = next_token(&buf);
Alex Elder4fb5d6712012-11-01 10:17:15 -05005592 if (!len) {
5593 rbd_warn(NULL, "no monitor address(es) provided");
5594 return -EINVAL;
5595 }
Alex Elder0ddebc02012-10-25 23:34:41 -05005596 mon_addrs = buf;
Alex Elderf28e5652012-10-25 23:34:41 -05005597 mon_addrs_size = len + 1;
Alex Elder7ef32142012-02-02 08:13:30 -06005598 buf += len;
Alex Eldera725f65e2012-02-02 08:13:30 -06005599
Alex Elderdc79b112012-10-25 23:34:41 -05005600 ret = -EINVAL;
Alex Elderf28e5652012-10-25 23:34:41 -05005601 options = dup_token(&buf, NULL);
5602 if (!options)
Alex Elderdc79b112012-10-25 23:34:41 -05005603 return -ENOMEM;
Alex Elder4fb5d6712012-11-01 10:17:15 -05005604 if (!*options) {
5605 rbd_warn(NULL, "no options provided");
5606 goto out_err;
5607 }
Alex Eldera725f65e2012-02-02 08:13:30 -06005608
Alex Elder859c31d2012-10-25 23:34:42 -05005609 spec = rbd_spec_alloc();
5610 if (!spec)
Alex Elderf28e5652012-10-25 23:34:41 -05005611 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05005612
5613 spec->pool_name = dup_token(&buf, NULL);
5614 if (!spec->pool_name)
5615 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05005616 if (!*spec->pool_name) {
5617 rbd_warn(NULL, "no pool name provided");
5618 goto out_err;
5619 }
Alex Eldere28fff262012-02-02 08:13:30 -06005620
Alex Elder69e7a022012-11-01 08:39:26 -05005621 spec->image_name = dup_token(&buf, NULL);
Alex Elder859c31d2012-10-25 23:34:42 -05005622 if (!spec->image_name)
Alex Elderf28e5652012-10-25 23:34:41 -05005623 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05005624 if (!*spec->image_name) {
5625 rbd_warn(NULL, "no image name provided");
5626 goto out_err;
5627 }
Alex Eldere28fff262012-02-02 08:13:30 -06005628
Alex Elderf28e5652012-10-25 23:34:41 -05005629 /*
5630 * Snapshot name is optional; default is to use "-"
5631 * (indicating the head/no snapshot).
5632 */
Alex Elder3feeb8942012-08-31 17:29:52 -05005633 len = next_token(&buf);
Alex Elder820a5f32012-07-09 21:04:24 -05005634 if (!len) {
Alex Elder3feeb8942012-08-31 17:29:52 -05005635 buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
5636 len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
Alex Elderf28e5652012-10-25 23:34:41 -05005637 } else if (len > RBD_MAX_SNAP_NAME_LEN) {
Alex Elderdc79b112012-10-25 23:34:41 -05005638 ret = -ENAMETOOLONG;
Alex Elderf28e5652012-10-25 23:34:41 -05005639 goto out_err;
Alex Elder849b4262012-07-09 21:04:24 -05005640 }
Alex Elderecb4dc22013-04-26 09:43:47 -05005641 snap_name = kmemdup(buf, len + 1, GFP_KERNEL);
5642 if (!snap_name)
Alex Elderf28e5652012-10-25 23:34:41 -05005643 goto out_mem;
Alex Elderecb4dc22013-04-26 09:43:47 -05005644 *(snap_name + len) = '\0';
5645 spec->snap_name = snap_name;
Alex Eldere5c35532012-10-25 23:34:41 -05005646
Alex Elder0ddebc02012-10-25 23:34:41 -05005647 /* Initialize all rbd options to the defaults */
Alex Eldere28fff262012-02-02 08:13:30 -06005648
Alex Elder4e9afeb2012-10-25 23:34:41 -05005649 rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL);
5650 if (!rbd_opts)
5651 goto out_mem;
5652
5653 rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
Ilya Dryomovb5584182015-06-23 16:21:19 +03005654 rbd_opts->queue_depth = RBD_QUEUE_DEPTH_DEFAULT;
Ilya Dryomov80de1912016-09-20 14:23:17 +02005655 rbd_opts->lock_on_read = RBD_LOCK_ON_READ_DEFAULT;
Ilya Dryomove010dd02017-04-13 12:17:39 +02005656 rbd_opts->exclusive = RBD_EXCLUSIVE_DEFAULT;
Alex Elderd22f76e2012-07-12 10:46:35 -05005657
Alex Elder859c31d2012-10-25 23:34:42 -05005658 copts = ceph_parse_options(options, mon_addrs,
Alex Elder0ddebc02012-10-25 23:34:41 -05005659 mon_addrs + mon_addrs_size - 1,
Alex Elder4e9afeb2012-10-25 23:34:41 -05005660 parse_rbd_opts_token, rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05005661 if (IS_ERR(copts)) {
5662 ret = PTR_ERR(copts);
Alex Elderdc79b112012-10-25 23:34:41 -05005663 goto out_err;
5664 }
Alex Elder859c31d2012-10-25 23:34:42 -05005665 kfree(options);
5666
5667 *ceph_opts = copts;
Alex Elder4e9afeb2012-10-25 23:34:41 -05005668 *opts = rbd_opts;
Alex Elder859c31d2012-10-25 23:34:42 -05005669 *rbd_spec = spec;
Alex Elder0ddebc02012-10-25 23:34:41 -05005670
Alex Elderdc79b112012-10-25 23:34:41 -05005671 return 0;
Alex Elderf28e5652012-10-25 23:34:41 -05005672out_mem:
Alex Elderdc79b112012-10-25 23:34:41 -05005673 ret = -ENOMEM;
Alex Elderd22f76e2012-07-12 10:46:35 -05005674out_err:
Alex Elder859c31d2012-10-25 23:34:42 -05005675 kfree(rbd_opts);
5676 rbd_spec_put(spec);
Alex Elderf28e5652012-10-25 23:34:41 -05005677 kfree(options);
Alex Elderd22f76e2012-07-12 10:46:35 -05005678
Alex Elderdc79b112012-10-25 23:34:41 -05005679 return ret;
Alex Eldera725f65e2012-02-02 08:13:30 -06005680}
5681
Alex Elder589d30e2012-07-10 20:30:11 -05005682/*
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005683 * Return pool id (>= 0) or a negative error code.
5684 */
5685static int rbd_add_get_pool_id(struct rbd_client *rbdc, const char *pool_name)
5686{
Ilya Dryomova319bf52015-05-15 12:02:17 +03005687 struct ceph_options *opts = rbdc->client->options;
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005688 u64 newest_epoch;
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005689 int tries = 0;
5690 int ret;
5691
5692again:
5693 ret = ceph_pg_poolid_by_name(rbdc->client->osdc.osdmap, pool_name);
5694 if (ret == -ENOENT && tries++ < 1) {
Ilya Dryomovd0b19702016-04-28 16:07:27 +02005695 ret = ceph_monc_get_version(&rbdc->client->monc, "osdmap",
5696 &newest_epoch);
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005697 if (ret < 0)
5698 return ret;
5699
5700 if (rbdc->client->osdc.osdmap->epoch < newest_epoch) {
Ilya Dryomov7cca78c2016-04-28 16:07:28 +02005701 ceph_osdc_maybe_request_map(&rbdc->client->osdc);
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005702 (void) ceph_monc_wait_osdmap(&rbdc->client->monc,
Ilya Dryomova319bf52015-05-15 12:02:17 +03005703 newest_epoch,
5704 opts->mount_timeout);
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005705 goto again;
5706 } else {
5707 /* the osdmap we have is new enough */
5708 return -ENOENT;
5709 }
5710 }
5711
5712 return ret;
5713}
5714
Ilya Dryomove010dd02017-04-13 12:17:39 +02005715static void rbd_dev_image_unlock(struct rbd_device *rbd_dev)
5716{
5717 down_write(&rbd_dev->lock_rwsem);
5718 if (__rbd_is_lock_owner(rbd_dev))
5719 rbd_unlock(rbd_dev);
5720 up_write(&rbd_dev->lock_rwsem);
5721}
5722
5723static int rbd_add_acquire_lock(struct rbd_device *rbd_dev)
5724{
5725 if (!(rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK)) {
5726 rbd_warn(rbd_dev, "exclusive-lock feature is not enabled");
5727 return -EINVAL;
5728 }
5729
5730 /* FIXME: "rbd map --exclusive" should be in interruptible */
5731 down_read(&rbd_dev->lock_rwsem);
5732 rbd_wait_state_locked(rbd_dev);
5733 up_read(&rbd_dev->lock_rwsem);
5734 if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) {
5735 rbd_warn(rbd_dev, "failed to acquire exclusive lock");
5736 return -EROFS;
5737 }
5738
5739 return 0;
5740}
5741
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005742/*
Alex Elder589d30e2012-07-10 20:30:11 -05005743 * An rbd format 2 image has a unique identifier, distinct from the
5744 * name given to it by the user. Internally, that identifier is
5745 * what's used to specify the names of objects related to the image.
5746 *
5747 * A special "rbd id" object is used to map an rbd image name to its
5748 * id. If that object doesn't exist, then there is no v2 rbd image
5749 * with the supplied name.
5750 *
5751 * This function will record the given rbd_dev's image_id field if
5752 * it can be determined, and in that case will return 0. If any
5753 * errors occur a negative errno will be returned and the rbd_dev's
5754 * image_id field will be unchanged (and should be NULL).
5755 */
5756static int rbd_dev_image_id(struct rbd_device *rbd_dev)
5757{
5758 int ret;
5759 size_t size;
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005760 CEPH_DEFINE_OID_ONSTACK(oid);
Alex Elder589d30e2012-07-10 20:30:11 -05005761 void *response;
Alex Elderc0fba362013-04-25 23:15:08 -05005762 char *image_id;
Alex Elder2f82ee52012-10-30 19:40:33 -05005763
Alex Elder589d30e2012-07-10 20:30:11 -05005764 /*
Alex Elder2c0d0a12012-10-30 19:40:33 -05005765 * When probing a parent image, the image id is already
5766 * known (and the image name likely is not). There's no
Alex Elderc0fba362013-04-25 23:15:08 -05005767 * need to fetch the image id again in this case. We
5768 * do still need to set the image format though.
Alex Elder2c0d0a12012-10-30 19:40:33 -05005769 */
Alex Elderc0fba362013-04-25 23:15:08 -05005770 if (rbd_dev->spec->image_id) {
5771 rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1;
5772
Alex Elder2c0d0a12012-10-30 19:40:33 -05005773 return 0;
Alex Elderc0fba362013-04-25 23:15:08 -05005774 }
Alex Elder2c0d0a12012-10-30 19:40:33 -05005775
5776 /*
Alex Elder589d30e2012-07-10 20:30:11 -05005777 * First, see if the format 2 image id file exists, and if
5778 * so, get the image's persistent id from it.
5779 */
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005780 ret = ceph_oid_aprintf(&oid, GFP_KERNEL, "%s%s", RBD_ID_PREFIX,
5781 rbd_dev->spec->image_name);
5782 if (ret)
5783 return ret;
5784
5785 dout("rbd id object name is %s\n", oid.name);
Alex Elder589d30e2012-07-10 20:30:11 -05005786
5787 /* Response will be an encoded string, which includes a length */
5788
5789 size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
5790 response = kzalloc(size, GFP_NOIO);
5791 if (!response) {
5792 ret = -ENOMEM;
5793 goto out;
5794 }
5795
Alex Elderc0fba362013-04-25 23:15:08 -05005796 /* If it doesn't exist we'll assume it's a format 1 image */
5797
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005798 ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc,
5799 "get_id", NULL, 0,
5800 response, RBD_IMAGE_ID_LEN_MAX);
Alex Elder36be9a72013-01-19 00:30:28 -06005801 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderc0fba362013-04-25 23:15:08 -05005802 if (ret == -ENOENT) {
5803 image_id = kstrdup("", GFP_KERNEL);
5804 ret = image_id ? 0 : -ENOMEM;
5805 if (!ret)
5806 rbd_dev->image_format = 1;
Ilya Dryomov7dd440c2014-09-11 18:49:18 +04005807 } else if (ret >= 0) {
Alex Elderc0fba362013-04-25 23:15:08 -05005808 void *p = response;
Alex Elder589d30e2012-07-10 20:30:11 -05005809
Alex Elderc0fba362013-04-25 23:15:08 -05005810 image_id = ceph_extract_encoded_string(&p, p + ret,
Alex Elder979ed482012-11-01 08:39:26 -05005811 NULL, GFP_NOIO);
Duan Jiong461f7582014-04-11 16:38:12 +08005812 ret = PTR_ERR_OR_ZERO(image_id);
Alex Elderc0fba362013-04-25 23:15:08 -05005813 if (!ret)
5814 rbd_dev->image_format = 2;
Alex Elderc0fba362013-04-25 23:15:08 -05005815 }
5816
5817 if (!ret) {
5818 rbd_dev->spec->image_id = image_id;
5819 dout("image_id is %s\n", image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05005820 }
5821out:
5822 kfree(response);
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005823 ceph_oid_destroy(&oid);
Alex Elder589d30e2012-07-10 20:30:11 -05005824 return ret;
5825}
5826
Alex Elder3abef3b2013-05-13 20:35:37 -05005827/*
5828 * Undo whatever state changes are made by v1 or v2 header info
5829 * call.
5830 */
Alex Elder6fd48b32013-04-28 23:32:34 -05005831static void rbd_dev_unprobe(struct rbd_device *rbd_dev)
5832{
5833 struct rbd_image_header *header;
5834
Ilya Dryomove69b8d42015-01-19 12:06:14 +03005835 rbd_dev_parent_put(rbd_dev);
Alex Elder6fd48b32013-04-28 23:32:34 -05005836
5837 /* Free dynamic fields from the header, then zero it out */
5838
5839 header = &rbd_dev->header;
Alex Elder812164f82013-04-30 00:44:32 -05005840 ceph_put_snap_context(header->snapc);
Alex Elder6fd48b32013-04-28 23:32:34 -05005841 kfree(header->snap_sizes);
5842 kfree(header->snap_names);
5843 kfree(header->object_prefix);
5844 memset(header, 0, sizeof (*header));
5845}
5846
Alex Elder2df3fac2013-05-06 09:51:30 -05005847static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev)
Alex Eldera30b71b2012-07-10 20:30:11 -05005848{
5849 int ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05005850
Alex Elder1e130192012-07-03 16:01:19 -05005851 ret = rbd_dev_v2_object_prefix(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05005852 if (ret)
Alex Elder1e130192012-07-03 16:01:19 -05005853 goto out_err;
Alex Elderb1b54022012-07-03 16:01:19 -05005854
Alex Elder2df3fac2013-05-06 09:51:30 -05005855 /*
5856 * Get the and check features for the image. Currently the
5857 * features are assumed to never change.
5858 */
Alex Elderb1b54022012-07-03 16:01:19 -05005859 ret = rbd_dev_v2_features(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05005860 if (ret)
Alex Elderb1b54022012-07-03 16:01:19 -05005861 goto out_err;
Alex Elder35d489f2012-07-03 16:01:19 -05005862
Alex Eldercc070d52013-04-21 12:14:45 -05005863 /* If the image supports fancy striping, get its parameters */
5864
5865 if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) {
5866 ret = rbd_dev_v2_striping_info(rbd_dev);
5867 if (ret < 0)
5868 goto out_err;
5869 }
Alex Eldera30b71b2012-07-10 20:30:11 -05005870
Ilya Dryomov7e973322017-01-25 18:16:22 +01005871 if (rbd_dev->header.features & RBD_FEATURE_DATA_POOL) {
5872 ret = rbd_dev_v2_data_pool(rbd_dev);
5873 if (ret)
5874 goto out_err;
5875 }
5876
Ilya Dryomov263423f2017-01-25 18:16:22 +01005877 rbd_init_layout(rbd_dev);
Alex Elder35152972012-08-31 17:29:55 -05005878 return 0;
Ilya Dryomov263423f2017-01-25 18:16:22 +01005879
Alex Elder9d475de2012-07-03 16:01:19 -05005880out_err:
Alex Elder642a2532013-05-06 17:40:33 -05005881 rbd_dev->header.features = 0;
Alex Elder1e130192012-07-03 16:01:19 -05005882 kfree(rbd_dev->header.object_prefix);
5883 rbd_dev->header.object_prefix = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05005884 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05005885}
5886
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005887/*
5888 * @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() ->
5889 * rbd_dev_image_probe() recursion depth, which means it's also the
5890 * length of the already discovered part of the parent chain.
5891 */
5892static int rbd_dev_probe_parent(struct rbd_device *rbd_dev, int depth)
Alex Elder83a06262012-10-30 15:47:17 -05005893{
Alex Elder2f82ee52012-10-30 19:40:33 -05005894 struct rbd_device *parent = NULL;
Alex Elder124afba2013-04-26 15:44:36 -05005895 int ret;
5896
5897 if (!rbd_dev->parent_spec)
5898 return 0;
Alex Elder124afba2013-04-26 15:44:36 -05005899
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005900 if (++depth > RBD_MAX_PARENT_CHAIN_LEN) {
5901 pr_info("parent chain is too long (%d)\n", depth);
5902 ret = -EINVAL;
5903 goto out_err;
5904 }
5905
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02005906 parent = __rbd_dev_create(rbd_dev->rbd_client, rbd_dev->parent_spec);
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005907 if (!parent) {
5908 ret = -ENOMEM;
Alex Elder124afba2013-04-26 15:44:36 -05005909 goto out_err;
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005910 }
5911
5912 /*
5913 * Images related by parent/child relationships always share
5914 * rbd_client and spec/parent_spec, so bump their refcounts.
5915 */
5916 __rbd_get_client(rbd_dev->rbd_client);
5917 rbd_spec_get(rbd_dev->parent_spec);
Alex Elder124afba2013-04-26 15:44:36 -05005918
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005919 ret = rbd_dev_image_probe(parent, depth);
Alex Elder124afba2013-04-26 15:44:36 -05005920 if (ret < 0)
5921 goto out_err;
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005922
Alex Elder124afba2013-04-26 15:44:36 -05005923 rbd_dev->parent = parent;
Alex Eldera2acd002013-05-08 22:50:04 -05005924 atomic_set(&rbd_dev->parent_ref, 1);
Alex Elder124afba2013-04-26 15:44:36 -05005925 return 0;
Alex Elder124afba2013-04-26 15:44:36 -05005926
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005927out_err:
5928 rbd_dev_unparent(rbd_dev);
Markus Elfring1761b222015-11-23 20:16:45 +01005929 rbd_dev_destroy(parent);
Alex Elder124afba2013-04-26 15:44:36 -05005930 return ret;
5931}
5932
Ilya Dryomov5769ed02017-04-13 12:17:38 +02005933static void rbd_dev_device_release(struct rbd_device *rbd_dev)
5934{
5935 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
5936 rbd_dev_mapping_clear(rbd_dev);
5937 rbd_free_disk(rbd_dev);
5938 if (!single_major)
5939 unregister_blkdev(rbd_dev->major, rbd_dev->name);
5940}
5941
Ilya Dryomov811c6682016-04-15 16:22:16 +02005942/*
5943 * rbd_dev->header_rwsem must be locked for write and will be unlocked
5944 * upon return.
5945 */
Alex Elder200a6a82013-04-28 23:32:34 -05005946static int rbd_dev_device_setup(struct rbd_device *rbd_dev)
Alex Elder124afba2013-04-26 15:44:36 -05005947{
Alex Elder83a06262012-10-30 15:47:17 -05005948 int ret;
Alex Elder83a06262012-10-30 15:47:17 -05005949
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005950 /* Record our major and minor device numbers. */
Alex Elder83a06262012-10-30 15:47:17 -05005951
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005952 if (!single_major) {
5953 ret = register_blkdev(0, rbd_dev->name);
5954 if (ret < 0)
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02005955 goto err_out_unlock;
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005956
5957 rbd_dev->major = ret;
5958 rbd_dev->minor = 0;
5959 } else {
5960 rbd_dev->major = rbd_major;
5961 rbd_dev->minor = rbd_dev_id_to_minor(rbd_dev->dev_id);
5962 }
Alex Elder83a06262012-10-30 15:47:17 -05005963
5964 /* Set up the blkdev mapping. */
5965
5966 ret = rbd_init_disk(rbd_dev);
5967 if (ret)
5968 goto err_out_blkdev;
5969
Alex Elderf35a4de2013-05-06 09:51:29 -05005970 ret = rbd_dev_mapping_set(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05005971 if (ret)
5972 goto err_out_disk;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04005973
Alex Elderf35a4de2013-05-06 09:51:29 -05005974 set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
Josh Durgin22001f62013-09-30 20:10:04 -07005975 set_disk_ro(rbd_dev->disk, rbd_dev->mapping.read_only);
Alex Elderf35a4de2013-05-06 09:51:29 -05005976
Ilya Dryomov5769ed02017-04-13 12:17:38 +02005977 ret = dev_set_name(&rbd_dev->dev, "%d", rbd_dev->dev_id);
Alex Elderf35a4de2013-05-06 09:51:29 -05005978 if (ret)
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04005979 goto err_out_mapping;
Alex Elder83a06262012-10-30 15:47:17 -05005980
Alex Elder129b79d2013-04-26 15:44:36 -05005981 set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
Ilya Dryomov811c6682016-04-15 16:22:16 +02005982 up_write(&rbd_dev->header_rwsem);
Ilya Dryomov5769ed02017-04-13 12:17:38 +02005983 return 0;
Alex Elder2f82ee52012-10-30 19:40:33 -05005984
Alex Elderf35a4de2013-05-06 09:51:29 -05005985err_out_mapping:
5986 rbd_dev_mapping_clear(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05005987err_out_disk:
5988 rbd_free_disk(rbd_dev);
5989err_out_blkdev:
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005990 if (!single_major)
5991 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Ilya Dryomov811c6682016-04-15 16:22:16 +02005992err_out_unlock:
5993 up_write(&rbd_dev->header_rwsem);
Alex Elder83a06262012-10-30 15:47:17 -05005994 return ret;
5995}
5996
Alex Elder332bb122013-04-27 09:59:30 -05005997static int rbd_dev_header_name(struct rbd_device *rbd_dev)
5998{
5999 struct rbd_spec *spec = rbd_dev->spec;
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006000 int ret;
Alex Elder332bb122013-04-27 09:59:30 -05006001
6002 /* Record the header object name for this rbd image. */
6003
6004 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
Alex Elder332bb122013-04-27 09:59:30 -05006005 if (rbd_dev->image_format == 1)
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006006 ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s",
6007 spec->image_name, RBD_SUFFIX);
Alex Elder332bb122013-04-27 09:59:30 -05006008 else
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006009 ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s",
6010 RBD_HEADER_PREFIX, spec->image_id);
Alex Elder332bb122013-04-27 09:59:30 -05006011
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006012 return ret;
Alex Elder332bb122013-04-27 09:59:30 -05006013}
6014
Alex Elder200a6a82013-04-28 23:32:34 -05006015static void rbd_dev_image_release(struct rbd_device *rbd_dev)
6016{
Alex Elder6fd48b32013-04-28 23:32:34 -05006017 rbd_dev_unprobe(rbd_dev);
Ilya Dryomovfd22aef2017-04-13 12:17:37 +02006018 if (rbd_dev->opts)
6019 rbd_unregister_watch(rbd_dev);
Alex Elder6fd48b32013-04-28 23:32:34 -05006020 rbd_dev->image_format = 0;
6021 kfree(rbd_dev->spec->image_id);
6022 rbd_dev->spec->image_id = NULL;
Alex Elder200a6a82013-04-28 23:32:34 -05006023}
6024
Alex Eldera30b71b2012-07-10 20:30:11 -05006025/*
6026 * Probe for the existence of the header object for the given rbd
Alex Elder1f3ef782013-05-06 17:40:33 -05006027 * device. If this image is the one being mapped (i.e., not a
6028 * parent), initiate a watch on its header object before using that
6029 * object to get detailed information about the rbd image.
Alex Eldera30b71b2012-07-10 20:30:11 -05006030 */
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006031static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth)
Alex Eldera30b71b2012-07-10 20:30:11 -05006032{
6033 int ret;
6034
6035 /*
Alex Elder3abef3b2013-05-13 20:35:37 -05006036 * Get the id from the image id object. Unless there's an
6037 * error, rbd_dev->spec->image_id will be filled in with
6038 * a dynamically-allocated string, and rbd_dev->image_format
6039 * will be set to either 1 or 2.
Alex Eldera30b71b2012-07-10 20:30:11 -05006040 */
6041 ret = rbd_dev_image_id(rbd_dev);
6042 if (ret)
Alex Elderc0fba362013-04-25 23:15:08 -05006043 return ret;
Alex Elderc0fba362013-04-25 23:15:08 -05006044
Alex Elder332bb122013-04-27 09:59:30 -05006045 ret = rbd_dev_header_name(rbd_dev);
6046 if (ret)
6047 goto err_out_format;
6048
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006049 if (!depth) {
Ilya Dryomov99d16942016-08-12 16:11:41 +02006050 ret = rbd_register_watch(rbd_dev);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006051 if (ret) {
6052 if (ret == -ENOENT)
6053 pr_info("image %s/%s does not exist\n",
6054 rbd_dev->spec->pool_name,
6055 rbd_dev->spec->image_name);
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006056 goto err_out_format;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006057 }
Alex Elder1f3ef782013-05-06 17:40:33 -05006058 }
Alex Elderb644de22013-04-27 09:59:31 -05006059
Ilya Dryomova720ae02014-07-23 17:11:19 +04006060 ret = rbd_dev_header_info(rbd_dev);
Alex Elder5655c4d2013-04-25 23:15:08 -05006061 if (ret)
Alex Elderb644de22013-04-27 09:59:31 -05006062 goto err_out_watch;
Alex Elder83a06262012-10-30 15:47:17 -05006063
Ilya Dryomov04077592014-07-23 17:11:20 +04006064 /*
6065 * If this image is the one being mapped, we have pool name and
6066 * id, image name and id, and snap name - need to fill snap id.
6067 * Otherwise this is a parent image, identified by pool, image
6068 * and snap ids - need to fill in names for those ids.
6069 */
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006070 if (!depth)
Ilya Dryomov04077592014-07-23 17:11:20 +04006071 ret = rbd_spec_fill_snap_id(rbd_dev);
6072 else
6073 ret = rbd_spec_fill_names(rbd_dev);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006074 if (ret) {
6075 if (ret == -ENOENT)
6076 pr_info("snap %s/%s@%s does not exist\n",
6077 rbd_dev->spec->pool_name,
6078 rbd_dev->spec->image_name,
6079 rbd_dev->spec->snap_name);
Alex Elder33dca392013-04-30 00:44:33 -05006080 goto err_out_probe;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006081 }
Alex Elder9bb81c92013-04-27 09:59:30 -05006082
Ilya Dryomove8f59b52014-07-24 10:42:13 +04006083 if (rbd_dev->header.features & RBD_FEATURE_LAYERING) {
6084 ret = rbd_dev_v2_parent_info(rbd_dev);
6085 if (ret)
6086 goto err_out_probe;
6087
6088 /*
6089 * Need to warn users if this image is the one being
6090 * mapped and has a parent.
6091 */
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006092 if (!depth && rbd_dev->parent_spec)
Ilya Dryomove8f59b52014-07-24 10:42:13 +04006093 rbd_warn(rbd_dev,
6094 "WARNING: kernel layering is EXPERIMENTAL!");
6095 }
6096
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006097 ret = rbd_dev_probe_parent(rbd_dev, depth);
Alex Elder30d60ba2013-05-06 09:51:30 -05006098 if (ret)
6099 goto err_out_probe;
Alex Elder83a06262012-10-30 15:47:17 -05006100
Alex Elder30d60ba2013-05-06 09:51:30 -05006101 dout("discovered format %u image, header name is %s\n",
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006102 rbd_dev->image_format, rbd_dev->header_oid.name);
Alex Elder30d60ba2013-05-06 09:51:30 -05006103 return 0;
Ilya Dryomove8f59b52014-07-24 10:42:13 +04006104
Alex Elder6fd48b32013-04-28 23:32:34 -05006105err_out_probe:
6106 rbd_dev_unprobe(rbd_dev);
Alex Elderb644de22013-04-27 09:59:31 -05006107err_out_watch:
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006108 if (!depth)
Ilya Dryomov99d16942016-08-12 16:11:41 +02006109 rbd_unregister_watch(rbd_dev);
Alex Elder332bb122013-04-27 09:59:30 -05006110err_out_format:
6111 rbd_dev->image_format = 0;
Alex Elder5655c4d2013-04-25 23:15:08 -05006112 kfree(rbd_dev->spec->image_id);
6113 rbd_dev->spec->image_id = NULL;
Alex Elder5655c4d2013-04-25 23:15:08 -05006114 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05006115}
6116
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006117static ssize_t do_rbd_add(struct bus_type *bus,
6118 const char *buf,
6119 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006120{
Alex Eldercb8627c2012-07-09 21:04:23 -05006121 struct rbd_device *rbd_dev = NULL;
Alex Elderdc79b112012-10-25 23:34:41 -05006122 struct ceph_options *ceph_opts = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05006123 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05006124 struct rbd_spec *spec = NULL;
Alex Elder9d3997f2012-10-25 23:34:42 -05006125 struct rbd_client *rbdc;
Alex Elder51344a32013-05-06 07:40:30 -05006126 bool read_only;
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02006127 int rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006128
6129 if (!try_module_get(THIS_MODULE))
6130 return -ENODEV;
6131
Alex Eldera725f65e2012-02-02 08:13:30 -06006132 /* parse add command */
Alex Elder859c31d2012-10-25 23:34:42 -05006133 rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec);
Alex Elderdc79b112012-10-25 23:34:41 -05006134 if (rc < 0)
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006135 goto out;
Alex Eldera725f65e2012-02-02 08:13:30 -06006136
Alex Elder9d3997f2012-10-25 23:34:42 -05006137 rbdc = rbd_get_client(ceph_opts);
6138 if (IS_ERR(rbdc)) {
6139 rc = PTR_ERR(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05006140 goto err_out_args;
Alex Elder9d3997f2012-10-25 23:34:42 -05006141 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006142
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006143 /* pick the pool */
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04006144 rc = rbd_add_get_pool_id(rbdc, spec->pool_name);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006145 if (rc < 0) {
6146 if (rc == -ENOENT)
6147 pr_info("pool %s does not exist\n", spec->pool_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006148 goto err_out_client;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006149 }
Alex Elderc0cd10db2013-04-26 09:43:47 -05006150 spec->pool_id = (u64)rc;
Alex Elder859c31d2012-10-25 23:34:42 -05006151
Ilya Dryomovd1475432015-06-22 13:24:48 +03006152 rbd_dev = rbd_dev_create(rbdc, spec, rbd_opts);
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02006153 if (!rbd_dev) {
6154 rc = -ENOMEM;
Alex Elderbd4ba652012-10-25 23:34:42 -05006155 goto err_out_client;
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02006156 }
Alex Elderc53d5892012-10-25 23:34:42 -05006157 rbdc = NULL; /* rbd_dev now owns this */
6158 spec = NULL; /* rbd_dev now owns this */
Ilya Dryomovd1475432015-06-22 13:24:48 +03006159 rbd_opts = NULL; /* rbd_dev now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006160
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02006161 rbd_dev->config_info = kstrdup(buf, GFP_KERNEL);
6162 if (!rbd_dev->config_info) {
6163 rc = -ENOMEM;
6164 goto err_out_rbd_dev;
6165 }
6166
Ilya Dryomov811c6682016-04-15 16:22:16 +02006167 down_write(&rbd_dev->header_rwsem);
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006168 rc = rbd_dev_image_probe(rbd_dev, 0);
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02006169 if (rc < 0) {
6170 up_write(&rbd_dev->header_rwsem);
Alex Elderc53d5892012-10-25 23:34:42 -05006171 goto err_out_rbd_dev;
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02006172 }
Alex Elder05fd6f62012-08-29 17:11:07 -05006173
Alex Elder7ce4eef2013-05-06 17:40:33 -05006174 /* If we are mapping a snapshot it must be marked read-only */
6175
Ilya Dryomovd1475432015-06-22 13:24:48 +03006176 read_only = rbd_dev->opts->read_only;
Alex Elder7ce4eef2013-05-06 17:40:33 -05006177 if (rbd_dev->spec->snap_id != CEPH_NOSNAP)
6178 read_only = true;
6179 rbd_dev->mapping.read_only = read_only;
6180
Alex Elderb536f692013-04-28 23:32:34 -05006181 rc = rbd_dev_device_setup(rbd_dev);
Ilya Dryomovfd22aef2017-04-13 12:17:37 +02006182 if (rc)
Ilya Dryomov8b679ec2017-04-13 12:17:37 +02006183 goto err_out_image_probe;
Alex Elderb536f692013-04-28 23:32:34 -05006184
Ilya Dryomove010dd02017-04-13 12:17:39 +02006185 if (rbd_dev->opts->exclusive) {
6186 rc = rbd_add_acquire_lock(rbd_dev);
6187 if (rc)
6188 goto err_out_device_setup;
Alex Elderb536f692013-04-28 23:32:34 -05006189 }
6190
Ilya Dryomov5769ed02017-04-13 12:17:38 +02006191 /* Everything's ready. Announce the disk to the world. */
6192
6193 rc = device_add(&rbd_dev->dev);
6194 if (rc)
Ilya Dryomove010dd02017-04-13 12:17:39 +02006195 goto err_out_image_lock;
Ilya Dryomov5769ed02017-04-13 12:17:38 +02006196
6197 add_disk(rbd_dev->disk);
6198 /* see rbd_init_disk() */
6199 blk_put_queue(rbd_dev->disk->queue);
6200
6201 spin_lock(&rbd_dev_list_lock);
6202 list_add_tail(&rbd_dev->node, &rbd_dev_list);
6203 spin_unlock(&rbd_dev_list_lock);
6204
6205 pr_info("%s: capacity %llu features 0x%llx\n", rbd_dev->disk->disk_name,
6206 (unsigned long long)get_capacity(rbd_dev->disk) << SECTOR_SHIFT,
6207 rbd_dev->header.features);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006208 rc = count;
6209out:
6210 module_put(THIS_MODULE);
6211 return rc;
Alex Elder3abef3b2013-05-13 20:35:37 -05006212
Ilya Dryomove010dd02017-04-13 12:17:39 +02006213err_out_image_lock:
6214 rbd_dev_image_unlock(rbd_dev);
Ilya Dryomov5769ed02017-04-13 12:17:38 +02006215err_out_device_setup:
6216 rbd_dev_device_release(rbd_dev);
Ilya Dryomov8b679ec2017-04-13 12:17:37 +02006217err_out_image_probe:
6218 rbd_dev_image_release(rbd_dev);
Alex Elderc53d5892012-10-25 23:34:42 -05006219err_out_rbd_dev:
6220 rbd_dev_destroy(rbd_dev);
Alex Elderbd4ba652012-10-25 23:34:42 -05006221err_out_client:
Alex Elder9d3997f2012-10-25 23:34:42 -05006222 rbd_put_client(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05006223err_out_args:
Alex Elder859c31d2012-10-25 23:34:42 -05006224 rbd_spec_put(spec);
Ilya Dryomovd1475432015-06-22 13:24:48 +03006225 kfree(rbd_opts);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006226 goto out;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006227}
6228
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006229static ssize_t rbd_add(struct bus_type *bus,
6230 const char *buf,
6231 size_t count)
6232{
6233 if (single_major)
6234 return -EINVAL;
6235
6236 return do_rbd_add(bus, buf, count);
6237}
6238
6239static ssize_t rbd_add_single_major(struct bus_type *bus,
6240 const char *buf,
6241 size_t count)
6242{
6243 return do_rbd_add(bus, buf, count);
6244}
6245
Alex Elder05a46af2013-04-26 15:44:36 -05006246static void rbd_dev_remove_parent(struct rbd_device *rbd_dev)
6247{
Alex Elderad945fc2013-04-26 15:44:36 -05006248 while (rbd_dev->parent) {
Alex Elder05a46af2013-04-26 15:44:36 -05006249 struct rbd_device *first = rbd_dev;
6250 struct rbd_device *second = first->parent;
6251 struct rbd_device *third;
6252
6253 /*
6254 * Follow to the parent with no grandparent and
6255 * remove it.
6256 */
6257 while (second && (third = second->parent)) {
6258 first = second;
6259 second = third;
6260 }
Alex Elderad945fc2013-04-26 15:44:36 -05006261 rbd_assert(second);
Alex Elder8ad42cd2013-04-28 23:32:34 -05006262 rbd_dev_image_release(second);
Ilya Dryomov8b679ec2017-04-13 12:17:37 +02006263 rbd_dev_destroy(second);
Alex Elderad945fc2013-04-26 15:44:36 -05006264 first->parent = NULL;
6265 first->parent_overlap = 0;
6266
6267 rbd_assert(first->parent_spec);
Alex Elder05a46af2013-04-26 15:44:36 -05006268 rbd_spec_put(first->parent_spec);
6269 first->parent_spec = NULL;
Alex Elder05a46af2013-04-26 15:44:36 -05006270 }
6271}
6272
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006273static ssize_t do_rbd_remove(struct bus_type *bus,
6274 const char *buf,
6275 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006276{
6277 struct rbd_device *rbd_dev = NULL;
Alex Elder751cc0e2013-05-31 15:17:01 -05006278 struct list_head *tmp;
6279 int dev_id;
Mike Christie0276dca2016-08-18 18:38:45 +02006280 char opt_buf[6];
Alex Elder82a442d2013-05-31 17:40:44 -05006281 bool already = false;
Mike Christie0276dca2016-08-18 18:38:45 +02006282 bool force = false;
Alex Elder0d8189e2013-04-27 09:59:30 -05006283 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006284
Mike Christie0276dca2016-08-18 18:38:45 +02006285 dev_id = -1;
6286 opt_buf[0] = '\0';
6287 sscanf(buf, "%d %5s", &dev_id, opt_buf);
6288 if (dev_id < 0) {
6289 pr_err("dev_id out of range\n");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006290 return -EINVAL;
Mike Christie0276dca2016-08-18 18:38:45 +02006291 }
6292 if (opt_buf[0] != '\0') {
6293 if (!strcmp(opt_buf, "force")) {
6294 force = true;
6295 } else {
6296 pr_err("bad remove option at '%s'\n", opt_buf);
6297 return -EINVAL;
6298 }
6299 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006300
Alex Elder751cc0e2013-05-31 15:17:01 -05006301 ret = -ENOENT;
6302 spin_lock(&rbd_dev_list_lock);
6303 list_for_each(tmp, &rbd_dev_list) {
6304 rbd_dev = list_entry(tmp, struct rbd_device, node);
6305 if (rbd_dev->dev_id == dev_id) {
6306 ret = 0;
6307 break;
6308 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006309 }
Alex Elder751cc0e2013-05-31 15:17:01 -05006310 if (!ret) {
6311 spin_lock_irq(&rbd_dev->lock);
Mike Christie0276dca2016-08-18 18:38:45 +02006312 if (rbd_dev->open_count && !force)
Alex Elder751cc0e2013-05-31 15:17:01 -05006313 ret = -EBUSY;
6314 else
Alex Elder82a442d2013-05-31 17:40:44 -05006315 already = test_and_set_bit(RBD_DEV_FLAG_REMOVING,
6316 &rbd_dev->flags);
Alex Elder751cc0e2013-05-31 15:17:01 -05006317 spin_unlock_irq(&rbd_dev->lock);
6318 }
6319 spin_unlock(&rbd_dev_list_lock);
Alex Elder82a442d2013-05-31 17:40:44 -05006320 if (ret < 0 || already)
Alex Elder1ba0f1e2013-05-31 15:17:01 -05006321 return ret;
Alex Elder751cc0e2013-05-31 15:17:01 -05006322
Mike Christie0276dca2016-08-18 18:38:45 +02006323 if (force) {
6324 /*
6325 * Prevent new IO from being queued and wait for existing
6326 * IO to complete/fail.
6327 */
6328 blk_mq_freeze_queue(rbd_dev->disk->queue);
6329 blk_set_queue_dying(rbd_dev->disk->queue);
6330 }
6331
Ilya Dryomov5769ed02017-04-13 12:17:38 +02006332 del_gendisk(rbd_dev->disk);
6333 spin_lock(&rbd_dev_list_lock);
6334 list_del_init(&rbd_dev->node);
6335 spin_unlock(&rbd_dev_list_lock);
6336 device_del(&rbd_dev->dev);
Ilya Dryomovfca27062013-12-16 18:02:40 +02006337
Ilya Dryomove010dd02017-04-13 12:17:39 +02006338 rbd_dev_image_unlock(rbd_dev);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006339 rbd_dev_device_release(rbd_dev);
Alex Elder8ad42cd2013-04-28 23:32:34 -05006340 rbd_dev_image_release(rbd_dev);
Ilya Dryomov8b679ec2017-04-13 12:17:37 +02006341 rbd_dev_destroy(rbd_dev);
Alex Elder1ba0f1e2013-05-31 15:17:01 -05006342 return count;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006343}
6344
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006345static ssize_t rbd_remove(struct bus_type *bus,
6346 const char *buf,
6347 size_t count)
6348{
6349 if (single_major)
6350 return -EINVAL;
6351
6352 return do_rbd_remove(bus, buf, count);
6353}
6354
6355static ssize_t rbd_remove_single_major(struct bus_type *bus,
6356 const char *buf,
6357 size_t count)
6358{
6359 return do_rbd_remove(bus, buf, count);
6360}
6361
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006362/*
6363 * create control files in sysfs
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006364 * /sys/bus/rbd/...
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006365 */
6366static int rbd_sysfs_init(void)
6367{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006368 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006369
Alex Elderfed4c142012-02-07 12:03:36 -06006370 ret = device_register(&rbd_root_dev);
Alex Elder21079782012-01-24 10:08:36 -06006371 if (ret < 0)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006372 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006373
Alex Elderfed4c142012-02-07 12:03:36 -06006374 ret = bus_register(&rbd_bus_type);
6375 if (ret < 0)
6376 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006377
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006378 return ret;
6379}
6380
6381static void rbd_sysfs_cleanup(void)
6382{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006383 bus_unregister(&rbd_bus_type);
Alex Elderfed4c142012-02-07 12:03:36 -06006384 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006385}
6386
Alex Elder1c2a9df2013-05-01 12:43:03 -05006387static int rbd_slab_init(void)
6388{
6389 rbd_assert(!rbd_img_request_cache);
Geliang Tang03d94402016-03-13 15:17:32 +08006390 rbd_img_request_cache = KMEM_CACHE(rbd_img_request, 0);
Alex Elder868311b2013-05-01 12:43:03 -05006391 if (!rbd_img_request_cache)
6392 return -ENOMEM;
6393
6394 rbd_assert(!rbd_obj_request_cache);
Geliang Tang03d94402016-03-13 15:17:32 +08006395 rbd_obj_request_cache = KMEM_CACHE(rbd_obj_request, 0);
Alex Elder78c2a442013-05-01 12:43:04 -05006396 if (!rbd_obj_request_cache)
6397 goto out_err;
6398
NeilBrownf856dc32017-06-18 14:38:58 +10006399 rbd_assert(!rbd_bio_clone);
6400 rbd_bio_clone = bioset_create(BIO_POOL_SIZE, 0, 0);
6401 if (!rbd_bio_clone)
6402 goto out_err_clone;
6403
Ilya Dryomov6c696d82017-01-25 18:16:23 +01006404 return 0;
Alex Elder1c2a9df2013-05-01 12:43:03 -05006405
NeilBrownf856dc32017-06-18 14:38:58 +10006406out_err_clone:
6407 kmem_cache_destroy(rbd_obj_request_cache);
6408 rbd_obj_request_cache = NULL;
Ilya Dryomov6c696d82017-01-25 18:16:23 +01006409out_err:
Alex Elder868311b2013-05-01 12:43:03 -05006410 kmem_cache_destroy(rbd_img_request_cache);
6411 rbd_img_request_cache = NULL;
Alex Elder1c2a9df2013-05-01 12:43:03 -05006412 return -ENOMEM;
6413}
6414
6415static void rbd_slab_exit(void)
6416{
Alex Elder868311b2013-05-01 12:43:03 -05006417 rbd_assert(rbd_obj_request_cache);
6418 kmem_cache_destroy(rbd_obj_request_cache);
6419 rbd_obj_request_cache = NULL;
6420
Alex Elder1c2a9df2013-05-01 12:43:03 -05006421 rbd_assert(rbd_img_request_cache);
6422 kmem_cache_destroy(rbd_img_request_cache);
6423 rbd_img_request_cache = NULL;
NeilBrownf856dc32017-06-18 14:38:58 +10006424
6425 rbd_assert(rbd_bio_clone);
6426 bioset_free(rbd_bio_clone);
6427 rbd_bio_clone = NULL;
Alex Elder1c2a9df2013-05-01 12:43:03 -05006428}
6429
Alex Eldercc344fa2013-02-19 12:25:56 -06006430static int __init rbd_init(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006431{
6432 int rc;
6433
Alex Elder1e32d342013-01-30 11:13:33 -06006434 if (!libceph_compatible(NULL)) {
6435 rbd_warn(NULL, "libceph incompatibility (quitting)");
Alex Elder1e32d342013-01-30 11:13:33 -06006436 return -EINVAL;
6437 }
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006438
Alex Elder1c2a9df2013-05-01 12:43:03 -05006439 rc = rbd_slab_init();
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006440 if (rc)
6441 return rc;
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006442
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006443 /*
6444 * The number of active work items is limited by the number of
Ilya Dryomovf77303b2015-04-22 18:28:13 +03006445 * rbd devices * queue depth, so leave @max_active at default.
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006446 */
6447 rbd_wq = alloc_workqueue(RBD_DRV_NAME, WQ_MEM_RECLAIM, 0);
6448 if (!rbd_wq) {
6449 rc = -ENOMEM;
6450 goto err_out_slab;
6451 }
6452
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006453 if (single_major) {
6454 rbd_major = register_blkdev(0, RBD_DRV_NAME);
6455 if (rbd_major < 0) {
6456 rc = rbd_major;
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006457 goto err_out_wq;
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006458 }
6459 }
6460
Alex Elder1c2a9df2013-05-01 12:43:03 -05006461 rc = rbd_sysfs_init();
6462 if (rc)
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006463 goto err_out_blkdev;
Alex Elder1c2a9df2013-05-01 12:43:03 -05006464
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006465 if (single_major)
6466 pr_info("loaded (major %d)\n", rbd_major);
6467 else
6468 pr_info("loaded\n");
6469
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006470 return 0;
6471
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006472err_out_blkdev:
6473 if (single_major)
6474 unregister_blkdev(rbd_major, RBD_DRV_NAME);
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006475err_out_wq:
6476 destroy_workqueue(rbd_wq);
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006477err_out_slab:
6478 rbd_slab_exit();
Alex Elder1c2a9df2013-05-01 12:43:03 -05006479 return rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006480}
6481
Alex Eldercc344fa2013-02-19 12:25:56 -06006482static void __exit rbd_exit(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006483{
Ilya Dryomovffe312c2014-05-20 15:46:04 +04006484 ida_destroy(&rbd_dev_id_ida);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006485 rbd_sysfs_cleanup();
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006486 if (single_major)
6487 unregister_blkdev(rbd_major, RBD_DRV_NAME);
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006488 destroy_workqueue(rbd_wq);
Alex Elder1c2a9df2013-05-01 12:43:03 -05006489 rbd_slab_exit();
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006490}
6491
6492module_init(rbd_init);
6493module_exit(rbd_exit);
6494
Alex Elderd552c612013-05-31 20:13:09 -05006495MODULE_AUTHOR("Alex Elder <elder@inktank.com>");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006496MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
6497MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006498/* following authorship retained from original osdblk.c */
6499MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
6500
Ilya Dryomov90da2582013-12-13 15:28:56 +02006501MODULE_DESCRIPTION("RADOS Block Device (RBD) driver");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006502MODULE_LICENSE("GPL");