blob: cc4c9f4c0d97adc8f20effac90fdd4a85298e0a9 [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
Alex Elder5cbf6f122013-04-11 09:29:48 -0500123#define RBD_FEATURE_LAYERING (1<<0)
124#define RBD_FEATURE_STRIPINGV2 (1<<1)
Ilya Dryomoved95b212016-08-12 16:40:02 +0200125#define RBD_FEATURE_EXCLUSIVE_LOCK (1<<2)
126#define RBD_FEATURES_ALL (RBD_FEATURE_LAYERING | \
127 RBD_FEATURE_STRIPINGV2 | \
128 RBD_FEATURE_EXCLUSIVE_LOCK)
Alex Elderd8891402012-10-09 13:50:17 -0700129
130/* Features supported by this (client software) implementation. */
131
Alex Elder770eba62012-10-25 23:34:40 -0500132#define RBD_FEATURES_SUPPORTED (RBD_FEATURES_ALL)
Alex Elderd8891402012-10-09 13:50:17 -0700133
Alex Elder81a89792012-02-02 08:13:30 -0600134/*
135 * An RBD device name will be "rbd#", where the "rbd" comes from
136 * RBD_DRV_NAME above, and # is a unique integer identifier.
Alex Elder81a89792012-02-02 08:13:30 -0600137 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700138#define DEV_NAME_LEN 32
139
140/*
141 * block device image metadata (in-memory version)
142 */
143struct rbd_image_header {
Alex Elderf35a4de2013-05-06 09:51:29 -0500144 /* These six fields never change for a given rbd image */
Alex Elder849b4262012-07-09 21:04:24 -0500145 char *object_prefix;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700146 __u8 obj_order;
147 __u8 crypt_type;
148 __u8 comp_type;
Alex Elderf35a4de2013-05-06 09:51:29 -0500149 u64 stripe_unit;
150 u64 stripe_count;
151 u64 features; /* Might be changeable someday? */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700152
Alex Elderf84344f2012-08-31 17:29:51 -0500153 /* The remaining fields need to be updated occasionally */
154 u64 image_size;
155 struct ceph_snap_context *snapc;
Alex Elderf35a4de2013-05-06 09:51:29 -0500156 char *snap_names; /* format 1 only */
157 u64 *snap_sizes; /* format 1 only */
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700158};
159
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500160/*
161 * An rbd image specification.
162 *
163 * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely
Alex Elderc66c6e02012-11-01 08:39:26 -0500164 * identify an image. Each rbd_dev structure includes a pointer to
165 * an rbd_spec structure that encapsulates this identity.
166 *
167 * Each of the id's in an rbd_spec has an associated name. For a
168 * user-mapped image, the names are supplied and the id's associated
169 * with them are looked up. For a layered image, a parent image is
170 * defined by the tuple, and the names are looked up.
171 *
172 * An rbd_dev structure contains a parent_spec pointer which is
173 * non-null if the image it represents is a child in a layered
174 * image. This pointer will refer to the rbd_spec structure used
175 * by the parent rbd_dev for its own identity (i.e., the structure
176 * is shared between the parent and child).
177 *
178 * Since these structures are populated once, during the discovery
179 * phase of image construction, they are effectively immutable so
180 * we make no effort to synchronize access to them.
181 *
182 * Note that code herein does not assume the image name is known (it
183 * could be a null pointer).
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500184 */
185struct rbd_spec {
186 u64 pool_id;
Alex Elderecb4dc22013-04-26 09:43:47 -0500187 const char *pool_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500188
Alex Elderecb4dc22013-04-26 09:43:47 -0500189 const char *image_id;
190 const char *image_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500191
192 u64 snap_id;
Alex Elderecb4dc22013-04-26 09:43:47 -0500193 const char *snap_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500194
195 struct kref kref;
196};
197
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700198/*
Alex Elderf0f8cef2012-01-29 13:57:44 -0600199 * an instance of the client. multiple devices may share an rbd client.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700200 */
201struct rbd_client {
202 struct ceph_client *client;
203 struct kref kref;
204 struct list_head node;
205};
206
Alex Elderbf0d5f502012-11-22 00:00:08 -0600207struct rbd_img_request;
208typedef void (*rbd_img_callback_t)(struct rbd_img_request *);
209
210#define BAD_WHICH U32_MAX /* Good which or bad which, which? */
211
212struct rbd_obj_request;
213typedef void (*rbd_obj_callback_t)(struct rbd_obj_request *);
214
Alex Elder9969ebc2013-01-18 12:31:10 -0600215enum obj_request_type {
216 OBJ_REQUEST_NODATA, OBJ_REQUEST_BIO, OBJ_REQUEST_PAGES
217};
Alex Elderbf0d5f502012-11-22 00:00:08 -0600218
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800219enum obj_operation_type {
220 OBJ_OP_WRITE,
221 OBJ_OP_READ,
Guangliang Zhao90e98c52014-04-01 22:22:16 +0800222 OBJ_OP_DISCARD,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800223};
224
Alex Elder926f9b32013-02-11 12:33:24 -0600225enum obj_req_flags {
226 OBJ_REQ_DONE, /* completion flag: not done = 0, done = 1 */
Alex Elder6365d332013-02-11 12:33:24 -0600227 OBJ_REQ_IMG_DATA, /* object usage: standalone = 0, image = 1 */
Alex Elder5679c592013-02-11 12:33:24 -0600228 OBJ_REQ_KNOWN, /* EXISTS flag valid: no = 0, yes = 1 */
229 OBJ_REQ_EXISTS, /* target exists: no = 0, yes = 1 */
Alex Elder926f9b32013-02-11 12:33:24 -0600230};
231
Alex Elderbf0d5f502012-11-22 00:00:08 -0600232struct rbd_obj_request {
233 const char *object_name;
234 u64 offset; /* object start byte */
235 u64 length; /* bytes from offset */
Alex Elder926f9b32013-02-11 12:33:24 -0600236 unsigned long flags;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600237
Alex Elderc5b5ef62013-02-11 12:33:24 -0600238 /*
239 * An object request associated with an image will have its
240 * img_data flag set; a standalone object request will not.
241 *
242 * A standalone object request will have which == BAD_WHICH
243 * and a null obj_request pointer.
244 *
245 * An object request initiated in support of a layered image
246 * object (to check for its existence before a write) will
247 * have which == BAD_WHICH and a non-null obj_request pointer.
248 *
249 * Finally, an object request for rbd image data will have
250 * which != BAD_WHICH, and will have a non-null img_request
251 * pointer. The value of which will be in the range
252 * 0..(img_request->obj_request_count-1).
253 */
254 union {
255 struct rbd_obj_request *obj_request; /* STAT op */
256 struct {
257 struct rbd_img_request *img_request;
258 u64 img_offset;
259 /* links for img_request->obj_requests list */
260 struct list_head links;
261 };
262 };
Alex Elderbf0d5f502012-11-22 00:00:08 -0600263 u32 which; /* posn image request list */
264
265 enum obj_request_type type;
Alex Elder788e2df2013-01-17 12:25:27 -0600266 union {
267 struct bio *bio_list;
268 struct {
269 struct page **pages;
270 u32 page_count;
271 };
272 };
Alex Elder0eefd472013-04-19 15:34:50 -0500273 struct page **copyup_pages;
Alex Elderebda6402013-05-10 16:29:22 -0500274 u32 copyup_page_count;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600275
276 struct ceph_osd_request *osd_req;
277
278 u64 xferred; /* bytes transferred */
Sage Weil1b83bef2013-02-25 16:11:12 -0800279 int result;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600280
281 rbd_obj_callback_t callback;
Alex Elder788e2df2013-01-17 12:25:27 -0600282 struct completion completion;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600283
284 struct kref kref;
285};
286
Alex Elder0c425242013-02-08 09:55:49 -0600287enum img_req_flags {
Alex Elder9849e982013-01-24 16:13:36 -0600288 IMG_REQ_WRITE, /* I/O direction: read = 0, write = 1 */
289 IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */
Alex Elderd0b2e942013-01-24 16:13:36 -0600290 IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */
Guangliang Zhao90e98c52014-04-01 22:22:16 +0800291 IMG_REQ_DISCARD, /* discard: normal = 0, discard request = 1 */
Alex Elder0c425242013-02-08 09:55:49 -0600292};
293
Alex Elderbf0d5f502012-11-22 00:00:08 -0600294struct rbd_img_request {
Alex Elderbf0d5f502012-11-22 00:00:08 -0600295 struct rbd_device *rbd_dev;
296 u64 offset; /* starting image byte offset */
297 u64 length; /* byte count from offset */
Alex Elder0c425242013-02-08 09:55:49 -0600298 unsigned long flags;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600299 union {
Alex Elder9849e982013-01-24 16:13:36 -0600300 u64 snap_id; /* for reads */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600301 struct ceph_snap_context *snapc; /* for writes */
Alex Elder9849e982013-01-24 16:13:36 -0600302 };
303 union {
304 struct request *rq; /* block request */
305 struct rbd_obj_request *obj_request; /* obj req initiator */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600306 };
Alex Elder3d7efd12013-04-19 15:34:50 -0500307 struct page **copyup_pages;
Alex Elderebda6402013-05-10 16:29:22 -0500308 u32 copyup_page_count;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600309 spinlock_t completion_lock;/* protects next_completion */
310 u32 next_completion;
311 rbd_img_callback_t callback;
Alex Elder55f27e02013-04-10 12:34:25 -0500312 u64 xferred;/* aggregate bytes transferred */
Alex Eldera5a337d2013-01-24 16:13:36 -0600313 int result; /* first nonzero obj_request result */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600314
315 u32 obj_request_count;
316 struct list_head obj_requests; /* rbd_obj_request structs */
317
318 struct kref kref;
319};
320
321#define for_each_obj_request(ireq, oreq) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600322 list_for_each_entry(oreq, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600323#define for_each_obj_request_from(ireq, oreq) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600324 list_for_each_entry_from(oreq, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600325#define for_each_obj_request_safe(ireq, oreq, n) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600326 list_for_each_entry_safe_reverse(oreq, n, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600327
Ilya Dryomov99d16942016-08-12 16:11:41 +0200328enum rbd_watch_state {
329 RBD_WATCH_STATE_UNREGISTERED,
330 RBD_WATCH_STATE_REGISTERED,
331 RBD_WATCH_STATE_ERROR,
332};
333
Ilya Dryomoved95b212016-08-12 16:40:02 +0200334enum rbd_lock_state {
335 RBD_LOCK_STATE_UNLOCKED,
336 RBD_LOCK_STATE_LOCKED,
337 RBD_LOCK_STATE_RELEASING,
338};
339
340/* WatchNotify::ClientId */
341struct rbd_client_id {
342 u64 gid;
343 u64 handle;
344};
345
Alex Elderf84344f2012-08-31 17:29:51 -0500346struct rbd_mapping {
Alex Elder99c1f082012-08-30 14:42:15 -0500347 u64 size;
Alex Elder34b13182012-07-13 20:35:12 -0500348 u64 features;
Alex Elderf84344f2012-08-31 17:29:51 -0500349 bool read_only;
350};
351
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700352/*
353 * a single device
354 */
355struct rbd_device {
Alex Elderde71a292012-07-03 16:01:19 -0500356 int dev_id; /* blkdev unique id */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700357
358 int major; /* blkdev assigned major */
Ilya Dryomovdd82fff2013-12-13 15:28:57 +0200359 int minor;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700360 struct gendisk *disk; /* blkdev's gendisk and rq */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700361
Alex Eldera30b71b2012-07-10 20:30:11 -0500362 u32 image_format; /* Either 1 or 2 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700363 struct rbd_client *rbd_client;
364
365 char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */
366
Alex Elderb82d1672013-01-14 12:43:31 -0600367 spinlock_t lock; /* queue, flags, open_count */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700368
369 struct rbd_image_header header;
Alex Elderb82d1672013-01-14 12:43:31 -0600370 unsigned long flags; /* possibly lock protected */
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500371 struct rbd_spec *spec;
Ilya Dryomovd1475432015-06-22 13:24:48 +0300372 struct rbd_options *opts;
Mike Christie0d6d1e9c2016-08-18 18:38:45 +0200373 char *config_info; /* add{,_single_major} string */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700374
Ilya Dryomovc41d13a2016-04-29 20:01:25 +0200375 struct ceph_object_id header_oid;
Ilya Dryomov922dab62016-05-26 01:15:02 +0200376 struct ceph_object_locator header_oloc;
Alex Elder971f8392012-10-25 23:34:41 -0500377
Ilya Dryomov1643dfa2016-08-12 15:45:52 +0200378 struct ceph_file_layout layout; /* used for all rbd requests */
Alex Elder0903e872012-11-14 12:25:19 -0600379
Ilya Dryomov99d16942016-08-12 16:11:41 +0200380 struct mutex watch_mutex;
381 enum rbd_watch_state watch_state;
Ilya Dryomov922dab62016-05-26 01:15:02 +0200382 struct ceph_osd_linger_request *watch_handle;
Ilya Dryomov99d16942016-08-12 16:11:41 +0200383 u64 watch_cookie;
384 struct delayed_work watch_dwork;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700385
Ilya Dryomoved95b212016-08-12 16:40:02 +0200386 struct rw_semaphore lock_rwsem;
387 enum rbd_lock_state lock_state;
388 struct rbd_client_id owner_cid;
389 struct work_struct acquired_lock_work;
390 struct work_struct released_lock_work;
391 struct delayed_work lock_dwork;
392 struct work_struct unlock_work;
393 wait_queue_head_t lock_waitq;
394
Ilya Dryomov1643dfa2016-08-12 15:45:52 +0200395 struct workqueue_struct *task_wq;
396
Alex Elder86b00e02012-10-25 23:34:42 -0500397 struct rbd_spec *parent_spec;
398 u64 parent_overlap;
Alex Eldera2acd002013-05-08 22:50:04 -0500399 atomic_t parent_ref;
Alex Elder2f82ee52012-10-30 19:40:33 -0500400 struct rbd_device *parent;
Alex Elder86b00e02012-10-25 23:34:42 -0500401
Christoph Hellwig7ad18af2015-01-13 17:20:04 +0100402 /* Block layer tags. */
403 struct blk_mq_tag_set tag_set;
404
Josh Durginc6666012011-11-21 17:11:12 -0800405 /* protects updating the header */
406 struct rw_semaphore header_rwsem;
Alex Elderf84344f2012-08-31 17:29:51 -0500407
408 struct rbd_mapping mapping;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700409
410 struct list_head node;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800411
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800412 /* sysfs related */
413 struct device dev;
Alex Elderb82d1672013-01-14 12:43:31 -0600414 unsigned long open_count; /* protected by lock */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800415};
416
Alex Elderb82d1672013-01-14 12:43:31 -0600417/*
418 * Flag bits for rbd_dev->flags. If atomicity is required,
419 * rbd_dev->lock is used to protect access.
420 *
421 * Currently, only the "removing" flag (which is coupled with the
422 * "open_count" field) requires atomic access.
423 */
Alex Elder6d292902013-01-14 12:43:31 -0600424enum rbd_dev_flags {
425 RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */
Alex Elderb82d1672013-01-14 12:43:31 -0600426 RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */
Alex Elder6d292902013-01-14 12:43:31 -0600427};
428
Alex Eldercfbf6372013-05-31 17:40:45 -0500429static DEFINE_MUTEX(client_mutex); /* Serialize client creation */
Alex Eldere124a82f2012-01-29 13:57:44 -0600430
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700431static LIST_HEAD(rbd_dev_list); /* devices */
Alex Eldere124a82f2012-01-29 13:57:44 -0600432static DEFINE_SPINLOCK(rbd_dev_list_lock);
433
Alex Elder432b8582012-01-29 13:57:44 -0600434static LIST_HEAD(rbd_client_list); /* clients */
435static DEFINE_SPINLOCK(rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700436
Alex Elder78c2a442013-05-01 12:43:04 -0500437/* Slab caches for frequently-allocated structures */
438
Alex Elder1c2a9df2013-05-01 12:43:03 -0500439static struct kmem_cache *rbd_img_request_cache;
Alex Elder868311b2013-05-01 12:43:03 -0500440static struct kmem_cache *rbd_obj_request_cache;
Alex Elder78c2a442013-05-01 12:43:04 -0500441static struct kmem_cache *rbd_segment_name_cache;
Alex Elder1c2a9df2013-05-01 12:43:03 -0500442
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200443static int rbd_major;
Ilya Dryomovf8a22fc2013-12-13 15:28:57 +0200444static DEFINE_IDA(rbd_dev_id_ida);
445
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +0400446static struct workqueue_struct *rbd_wq;
447
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200448/*
449 * Default to false for now, as single-major requires >= 0.75 version of
450 * userspace rbd utility.
451 */
452static bool single_major = false;
453module_param(single_major, bool, S_IRUGO);
454MODULE_PARM_DESC(single_major, "Use a single major number for all rbd devices (default: false)");
455
Alex Elder3d7efd12013-04-19 15:34:50 -0500456static int rbd_img_request_submit(struct rbd_img_request *img_request);
457
Alex Elderf0f8cef2012-01-29 13:57:44 -0600458static ssize_t rbd_add(struct bus_type *bus, const char *buf,
459 size_t count);
460static ssize_t rbd_remove(struct bus_type *bus, const char *buf,
461 size_t count);
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200462static ssize_t rbd_add_single_major(struct bus_type *bus, const char *buf,
463 size_t count);
464static ssize_t rbd_remove_single_major(struct bus_type *bus, const char *buf,
465 size_t count);
Ilya Dryomov6d69bb532015-10-11 19:38:00 +0200466static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth);
Alex Eldera2acd002013-05-08 22:50:04 -0500467static void rbd_spec_put(struct rbd_spec *spec);
Alex Elderf0f8cef2012-01-29 13:57:44 -0600468
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200469static int rbd_dev_id_to_minor(int dev_id)
470{
Ilya Dryomov7e513d42013-12-16 19:26:32 +0200471 return dev_id << RBD_SINGLE_MAJOR_PART_SHIFT;
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200472}
473
474static int minor_to_rbd_dev_id(int minor)
475{
Ilya Dryomov7e513d42013-12-16 19:26:32 +0200476 return minor >> RBD_SINGLE_MAJOR_PART_SHIFT;
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200477}
478
Ilya Dryomoved95b212016-08-12 16:40:02 +0200479static bool rbd_is_lock_supported(struct rbd_device *rbd_dev)
480{
481 return (rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK) &&
482 rbd_dev->spec->snap_id == CEPH_NOSNAP &&
483 !rbd_dev->mapping.read_only;
484}
485
486static bool __rbd_is_lock_owner(struct rbd_device *rbd_dev)
487{
488 return rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED ||
489 rbd_dev->lock_state == RBD_LOCK_STATE_RELEASING;
490}
491
492static bool rbd_is_lock_owner(struct rbd_device *rbd_dev)
493{
494 bool is_lock_owner;
495
496 down_read(&rbd_dev->lock_rwsem);
497 is_lock_owner = __rbd_is_lock_owner(rbd_dev);
498 up_read(&rbd_dev->lock_rwsem);
499 return is_lock_owner;
500}
501
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700502static BUS_ATTR(add, S_IWUSR, NULL, rbd_add);
503static BUS_ATTR(remove, S_IWUSR, NULL, rbd_remove);
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200504static BUS_ATTR(add_single_major, S_IWUSR, NULL, rbd_add_single_major);
505static BUS_ATTR(remove_single_major, S_IWUSR, NULL, rbd_remove_single_major);
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700506
507static struct attribute *rbd_bus_attrs[] = {
508 &bus_attr_add.attr,
509 &bus_attr_remove.attr,
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200510 &bus_attr_add_single_major.attr,
511 &bus_attr_remove_single_major.attr,
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700512 NULL,
Alex Elderf0f8cef2012-01-29 13:57:44 -0600513};
Ilya Dryomov92c76dc2013-12-13 15:28:57 +0200514
515static umode_t rbd_bus_is_visible(struct kobject *kobj,
516 struct attribute *attr, int index)
517{
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200518 if (!single_major &&
519 (attr == &bus_attr_add_single_major.attr ||
520 attr == &bus_attr_remove_single_major.attr))
521 return 0;
522
Ilya Dryomov92c76dc2013-12-13 15:28:57 +0200523 return attr->mode;
524}
525
526static const struct attribute_group rbd_bus_group = {
527 .attrs = rbd_bus_attrs,
528 .is_visible = rbd_bus_is_visible,
529};
530__ATTRIBUTE_GROUPS(rbd_bus);
Alex Elderf0f8cef2012-01-29 13:57:44 -0600531
532static struct bus_type rbd_bus_type = {
533 .name = "rbd",
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700534 .bus_groups = rbd_bus_groups,
Alex Elderf0f8cef2012-01-29 13:57:44 -0600535};
536
537static void rbd_root_dev_release(struct device *dev)
538{
539}
540
541static struct device rbd_root_dev = {
542 .init_name = "rbd",
543 .release = rbd_root_dev_release,
544};
545
Alex Elder06ecc6c2012-11-01 10:17:15 -0500546static __printf(2, 3)
547void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...)
548{
549 struct va_format vaf;
550 va_list args;
551
552 va_start(args, fmt);
553 vaf.fmt = fmt;
554 vaf.va = &args;
555
556 if (!rbd_dev)
557 printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf);
558 else if (rbd_dev->disk)
559 printk(KERN_WARNING "%s: %s: %pV\n",
560 RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf);
561 else if (rbd_dev->spec && rbd_dev->spec->image_name)
562 printk(KERN_WARNING "%s: image %s: %pV\n",
563 RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf);
564 else if (rbd_dev->spec && rbd_dev->spec->image_id)
565 printk(KERN_WARNING "%s: id %s: %pV\n",
566 RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf);
567 else /* punt */
568 printk(KERN_WARNING "%s: rbd_dev %p: %pV\n",
569 RBD_DRV_NAME, rbd_dev, &vaf);
570 va_end(args);
571}
572
Alex Elderaafb2302012-09-06 16:00:54 -0500573#ifdef RBD_DEBUG
574#define rbd_assert(expr) \
575 if (unlikely(!(expr))) { \
576 printk(KERN_ERR "\nAssertion failure in %s() " \
577 "at line %d:\n\n" \
578 "\trbd_assert(%s);\n\n", \
579 __func__, __LINE__, #expr); \
580 BUG(); \
581 }
582#else /* !RBD_DEBUG */
583# define rbd_assert(expr) ((void) 0)
584#endif /* !RBD_DEBUG */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800585
Ilya Dryomov27617132015-07-16 17:36:11 +0300586static void rbd_osd_copyup_callback(struct rbd_obj_request *obj_request);
Alex Elderb454e362013-04-19 15:34:50 -0500587static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request);
Alex Elder05a46af2013-04-26 15:44:36 -0500588static void rbd_img_parent_read(struct rbd_obj_request *obj_request);
589static void rbd_dev_remove_parent(struct rbd_device *rbd_dev);
Alex Elder8b3e1a52013-01-24 16:13:36 -0600590
Alex Eldercc4a38bd2013-04-30 00:44:33 -0500591static int rbd_dev_refresh(struct rbd_device *rbd_dev);
Alex Elder2df3fac2013-05-06 09:51:30 -0500592static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev);
Ilya Dryomova720ae02014-07-23 17:11:19 +0400593static int rbd_dev_header_info(struct rbd_device *rbd_dev);
Ilya Dryomove8f59b52014-07-24 10:42:13 +0400594static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev);
Alex Elder54cac612013-04-30 00:44:33 -0500595static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
596 u64 snap_id);
Alex Elder2ad3d712013-04-30 00:44:33 -0500597static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
598 u8 *order, u64 *snap_size);
599static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
600 u64 *snap_features);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700601
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700602static int rbd_open(struct block_device *bdev, fmode_t mode)
603{
Alex Elderf0f8cef2012-01-29 13:57:44 -0600604 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
Alex Elderb82d1672013-01-14 12:43:31 -0600605 bool removing = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700606
Alex Elderf84344f2012-08-31 17:29:51 -0500607 if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700608 return -EROFS;
609
Alex Eldera14ea262013-02-05 13:23:12 -0600610 spin_lock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600611 if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags))
612 removing = true;
613 else
614 rbd_dev->open_count++;
Alex Eldera14ea262013-02-05 13:23:12 -0600615 spin_unlock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600616 if (removing)
617 return -ENOENT;
618
Alex Elderc3e946c2012-11-16 09:29:16 -0600619 (void) get_device(&rbd_dev->dev);
Alex Elder340c7a22012-08-10 13:12:07 -0700620
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700621 return 0;
622}
623
Al Virodb2a1442013-05-05 21:52:57 -0400624static void rbd_release(struct gendisk *disk, fmode_t mode)
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800625{
626 struct rbd_device *rbd_dev = disk->private_data;
Alex Elderb82d1672013-01-14 12:43:31 -0600627 unsigned long open_count_before;
628
Alex Eldera14ea262013-02-05 13:23:12 -0600629 spin_lock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600630 open_count_before = rbd_dev->open_count--;
Alex Eldera14ea262013-02-05 13:23:12 -0600631 spin_unlock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600632 rbd_assert(open_count_before > 0);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800633
Alex Elderc3e946c2012-11-16 09:29:16 -0600634 put_device(&rbd_dev->dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800635}
636
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800637static int rbd_ioctl_set_ro(struct rbd_device *rbd_dev, unsigned long arg)
638{
Josh Durgin77f33c02013-09-30 17:09:54 -0700639 int ret = 0;
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800640 int val;
641 bool ro;
Josh Durgin77f33c02013-09-30 17:09:54 -0700642 bool ro_changed = false;
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800643
Josh Durgin77f33c02013-09-30 17:09:54 -0700644 /* get_user() may sleep, so call it before taking rbd_dev->lock */
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800645 if (get_user(val, (int __user *)(arg)))
646 return -EFAULT;
647
648 ro = val ? true : false;
649 /* Snapshot doesn't allow to write*/
650 if (rbd_dev->spec->snap_id != CEPH_NOSNAP && !ro)
651 return -EROFS;
652
Josh Durgin77f33c02013-09-30 17:09:54 -0700653 spin_lock_irq(&rbd_dev->lock);
654 /* prevent others open this device */
655 if (rbd_dev->open_count > 1) {
656 ret = -EBUSY;
657 goto out;
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800658 }
659
Josh Durgin77f33c02013-09-30 17:09:54 -0700660 if (rbd_dev->mapping.read_only != ro) {
661 rbd_dev->mapping.read_only = ro;
662 ro_changed = true;
663 }
664
665out:
666 spin_unlock_irq(&rbd_dev->lock);
667 /* set_disk_ro() may sleep, so call it after releasing rbd_dev->lock */
668 if (ret == 0 && ro_changed)
669 set_disk_ro(rbd_dev->disk, ro ? 1 : 0);
670
671 return ret;
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800672}
673
674static int rbd_ioctl(struct block_device *bdev, fmode_t mode,
675 unsigned int cmd, unsigned long arg)
676{
677 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
678 int ret = 0;
679
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800680 switch (cmd) {
681 case BLKROSET:
682 ret = rbd_ioctl_set_ro(rbd_dev, arg);
683 break;
684 default:
685 ret = -ENOTTY;
686 }
687
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800688 return ret;
689}
690
691#ifdef CONFIG_COMPAT
692static int rbd_compat_ioctl(struct block_device *bdev, fmode_t mode,
693 unsigned int cmd, unsigned long arg)
694{
695 return rbd_ioctl(bdev, mode, cmd, arg);
696}
697#endif /* CONFIG_COMPAT */
698
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700699static const struct block_device_operations rbd_bd_ops = {
700 .owner = THIS_MODULE,
701 .open = rbd_open,
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800702 .release = rbd_release,
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800703 .ioctl = rbd_ioctl,
704#ifdef CONFIG_COMPAT
705 .compat_ioctl = rbd_compat_ioctl,
706#endif
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700707};
708
709/*
Alex Elder7262cfc2013-05-16 15:04:20 -0500710 * Initialize an rbd client instance. Success or not, this function
Alex Eldercfbf6372013-05-31 17:40:45 -0500711 * consumes ceph_opts. Caller holds client_mutex.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700712 */
Alex Elderf8c38922012-08-10 13:12:07 -0700713static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700714{
715 struct rbd_client *rbdc;
716 int ret = -ENOMEM;
717
Alex Elder37206ee2013-02-20 17:32:08 -0600718 dout("%s:\n", __func__);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700719 rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL);
720 if (!rbdc)
721 goto out_opt;
722
723 kref_init(&rbdc->kref);
724 INIT_LIST_HEAD(&rbdc->node);
725
Alex Elder43ae4702012-07-03 16:01:18 -0500726 rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700727 if (IS_ERR(rbdc->client))
Alex Elder08f75462013-05-29 11:19:00 -0500728 goto out_rbdc;
Alex Elder43ae4702012-07-03 16:01:18 -0500729 ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700730
731 ret = ceph_open_session(rbdc->client);
732 if (ret < 0)
Alex Elder08f75462013-05-29 11:19:00 -0500733 goto out_client;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700734
Alex Elder432b8582012-01-29 13:57:44 -0600735 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700736 list_add_tail(&rbdc->node, &rbd_client_list);
Alex Elder432b8582012-01-29 13:57:44 -0600737 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700738
Alex Elder37206ee2013-02-20 17:32:08 -0600739 dout("%s: rbdc %p\n", __func__, rbdc);
Alex Elderbc534d82012-01-29 13:57:44 -0600740
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700741 return rbdc;
Alex Elder08f75462013-05-29 11:19:00 -0500742out_client:
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700743 ceph_destroy_client(rbdc->client);
Alex Elder08f75462013-05-29 11:19:00 -0500744out_rbdc:
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700745 kfree(rbdc);
746out_opt:
Alex Elder43ae4702012-07-03 16:01:18 -0500747 if (ceph_opts)
748 ceph_destroy_options(ceph_opts);
Alex Elder37206ee2013-02-20 17:32:08 -0600749 dout("%s: error %d\n", __func__, ret);
750
Vasiliy Kulikov28f259b2010-09-26 12:59:37 +0400751 return ERR_PTR(ret);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700752}
753
Alex Elder2f82ee52012-10-30 19:40:33 -0500754static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc)
755{
756 kref_get(&rbdc->kref);
757
758 return rbdc;
759}
760
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700761/*
Alex Elder1f7ba332012-08-10 13:12:07 -0700762 * Find a ceph client with specific addr and configuration. If
763 * found, bump its reference count.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700764 */
Alex Elder1f7ba332012-08-10 13:12:07 -0700765static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700766{
767 struct rbd_client *client_node;
Alex Elder1f7ba332012-08-10 13:12:07 -0700768 bool found = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700769
Alex Elder43ae4702012-07-03 16:01:18 -0500770 if (ceph_opts->flags & CEPH_OPT_NOSHARE)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700771 return NULL;
772
Alex Elder1f7ba332012-08-10 13:12:07 -0700773 spin_lock(&rbd_client_list_lock);
774 list_for_each_entry(client_node, &rbd_client_list, node) {
775 if (!ceph_compare_options(ceph_opts, client_node->client)) {
Alex Elder2f82ee52012-10-30 19:40:33 -0500776 __rbd_get_client(client_node);
777
Alex Elder1f7ba332012-08-10 13:12:07 -0700778 found = true;
779 break;
780 }
781 }
782 spin_unlock(&rbd_client_list_lock);
783
784 return found ? client_node : NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700785}
786
787/*
Ilya Dryomov210c1042015-06-22 13:24:48 +0300788 * (Per device) rbd map options
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700789 */
790enum {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300791 Opt_queue_depth,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700792 Opt_last_int,
793 /* int args above */
794 Opt_last_string,
795 /* string args above */
Alex Eldercc0538b2012-08-10 13:12:07 -0700796 Opt_read_only,
797 Opt_read_write,
Ilya Dryomov80de1912016-09-20 14:23:17 +0200798 Opt_lock_on_read,
Ilya Dryomov210c1042015-06-22 13:24:48 +0300799 Opt_err
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700800};
801
Alex Elder43ae4702012-07-03 16:01:18 -0500802static match_table_t rbd_opts_tokens = {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300803 {Opt_queue_depth, "queue_depth=%d"},
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700804 /* int args above */
805 /* string args above */
Alex Elderbe466c12012-10-22 11:31:26 -0500806 {Opt_read_only, "read_only"},
Alex Eldercc0538b2012-08-10 13:12:07 -0700807 {Opt_read_only, "ro"}, /* Alternate spelling */
808 {Opt_read_write, "read_write"},
809 {Opt_read_write, "rw"}, /* Alternate spelling */
Ilya Dryomov80de1912016-09-20 14:23:17 +0200810 {Opt_lock_on_read, "lock_on_read"},
Ilya Dryomov210c1042015-06-22 13:24:48 +0300811 {Opt_err, NULL}
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700812};
813
Alex Elder98571b52013-01-20 14:44:42 -0600814struct rbd_options {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300815 int queue_depth;
Alex Elder98571b52013-01-20 14:44:42 -0600816 bool read_only;
Ilya Dryomov80de1912016-09-20 14:23:17 +0200817 bool lock_on_read;
Alex Elder98571b52013-01-20 14:44:42 -0600818};
819
Ilya Dryomovb5584182015-06-23 16:21:19 +0300820#define RBD_QUEUE_DEPTH_DEFAULT BLKDEV_MAX_RQ
Alex Elder98571b52013-01-20 14:44:42 -0600821#define RBD_READ_ONLY_DEFAULT false
Ilya Dryomov80de1912016-09-20 14:23:17 +0200822#define RBD_LOCK_ON_READ_DEFAULT false
Alex Elder98571b52013-01-20 14:44:42 -0600823
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700824static int parse_rbd_opts_token(char *c, void *private)
825{
Alex Elder43ae4702012-07-03 16:01:18 -0500826 struct rbd_options *rbd_opts = private;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700827 substring_t argstr[MAX_OPT_ARGS];
828 int token, intval, ret;
829
Alex Elder43ae4702012-07-03 16:01:18 -0500830 token = match_token(c, rbd_opts_tokens, argstr);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700831 if (token < Opt_last_int) {
832 ret = match_int(&argstr[0], &intval);
833 if (ret < 0) {
Ilya Dryomov210c1042015-06-22 13:24:48 +0300834 pr_err("bad mount option arg (not int) at '%s'\n", c);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700835 return ret;
836 }
837 dout("got int token %d val %d\n", token, intval);
838 } else if (token > Opt_last_int && token < Opt_last_string) {
Ilya Dryomov210c1042015-06-22 13:24:48 +0300839 dout("got string token %d val %s\n", token, argstr[0].from);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700840 } else {
841 dout("got token %d\n", token);
842 }
843
844 switch (token) {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300845 case Opt_queue_depth:
846 if (intval < 1) {
847 pr_err("queue_depth out of range\n");
848 return -EINVAL;
849 }
850 rbd_opts->queue_depth = intval;
851 break;
Alex Eldercc0538b2012-08-10 13:12:07 -0700852 case Opt_read_only:
853 rbd_opts->read_only = true;
854 break;
855 case Opt_read_write:
856 rbd_opts->read_only = false;
857 break;
Ilya Dryomov80de1912016-09-20 14:23:17 +0200858 case Opt_lock_on_read:
859 rbd_opts->lock_on_read = true;
860 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700861 default:
Ilya Dryomov210c1042015-06-22 13:24:48 +0300862 /* libceph prints "bad option" msg */
863 return -EINVAL;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700864 }
Ilya Dryomov210c1042015-06-22 13:24:48 +0300865
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700866 return 0;
867}
868
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800869static char* obj_op_name(enum obj_operation_type op_type)
870{
871 switch (op_type) {
872 case OBJ_OP_READ:
873 return "read";
874 case OBJ_OP_WRITE:
875 return "write";
Guangliang Zhao90e98c52014-04-01 22:22:16 +0800876 case OBJ_OP_DISCARD:
877 return "discard";
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800878 default:
879 return "???";
880 }
881}
882
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700883/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700884 * Get a ceph client with specific addr and configuration, if one does
Alex Elder7262cfc2013-05-16 15:04:20 -0500885 * not exist create it. Either way, ceph_opts is consumed by this
886 * function.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700887 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500888static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700889{
Alex Elderf8c38922012-08-10 13:12:07 -0700890 struct rbd_client *rbdc;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700891
Alex Eldercfbf6372013-05-31 17:40:45 -0500892 mutex_lock_nested(&client_mutex, SINGLE_DEPTH_NESTING);
Alex Elder1f7ba332012-08-10 13:12:07 -0700893 rbdc = rbd_client_find(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500894 if (rbdc) /* using an existing client */
Alex Elder43ae4702012-07-03 16:01:18 -0500895 ceph_destroy_options(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500896 else
Alex Elderf8c38922012-08-10 13:12:07 -0700897 rbdc = rbd_client_create(ceph_opts);
Alex Eldercfbf6372013-05-31 17:40:45 -0500898 mutex_unlock(&client_mutex);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700899
Alex Elder9d3997f2012-10-25 23:34:42 -0500900 return rbdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700901}
902
903/*
904 * Destroy ceph client
Alex Elderd23a4b32012-01-29 13:57:43 -0600905 *
Alex Elder432b8582012-01-29 13:57:44 -0600906 * Caller must hold rbd_client_list_lock.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700907 */
908static void rbd_client_release(struct kref *kref)
909{
910 struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
911
Alex Elder37206ee2013-02-20 17:32:08 -0600912 dout("%s: rbdc %p\n", __func__, rbdc);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500913 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700914 list_del(&rbdc->node);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500915 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700916
917 ceph_destroy_client(rbdc->client);
918 kfree(rbdc);
919}
920
921/*
922 * Drop reference to ceph client node. If it's not referenced anymore, release
923 * it.
924 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500925static void rbd_put_client(struct rbd_client *rbdc)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700926{
Alex Elderc53d5892012-10-25 23:34:42 -0500927 if (rbdc)
928 kref_put(&rbdc->kref, rbd_client_release);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700929}
930
Alex Eldera30b71b2012-07-10 20:30:11 -0500931static bool rbd_image_format_valid(u32 image_format)
932{
933 return image_format == 1 || image_format == 2;
934}
935
Alex Elder8e94af82012-07-25 09:32:40 -0500936static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
937{
Alex Elder103a1502012-08-02 11:29:45 -0500938 size_t size;
939 u32 snap_count;
940
941 /* The header has to start with the magic rbd header text */
942 if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT)))
943 return false;
944
Alex Elderdb2388b2012-10-20 22:17:27 -0500945 /* The bio layer requires at least sector-sized I/O */
946
947 if (ondisk->options.order < SECTOR_SHIFT)
948 return false;
949
950 /* If we use u64 in a few spots we may be able to loosen this */
951
952 if (ondisk->options.order > 8 * sizeof (int) - 1)
953 return false;
954
Alex Elder103a1502012-08-02 11:29:45 -0500955 /*
956 * The size of a snapshot header has to fit in a size_t, and
957 * that limits the number of snapshots.
958 */
959 snap_count = le32_to_cpu(ondisk->snap_count);
960 size = SIZE_MAX - sizeof (struct ceph_snap_context);
961 if (snap_count > size / sizeof (__le64))
962 return false;
963
964 /*
965 * Not only that, but the size of the entire the snapshot
966 * header must also be representable in a size_t.
967 */
968 size -= snap_count * sizeof (__le64);
969 if ((u64) size < le64_to_cpu(ondisk->snap_names_len))
970 return false;
971
972 return true;
Alex Elder8e94af82012-07-25 09:32:40 -0500973}
974
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700975/*
Alex Elderbb23e372013-05-06 09:51:29 -0500976 * Fill an rbd image header with information from the given format 1
977 * on-disk header.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700978 */
Alex Elder662518b2013-05-06 09:51:29 -0500979static int rbd_header_from_disk(struct rbd_device *rbd_dev,
Alex Elder4156d992012-08-02 11:29:46 -0500980 struct rbd_image_header_ondisk *ondisk)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700981{
Alex Elder662518b2013-05-06 09:51:29 -0500982 struct rbd_image_header *header = &rbd_dev->header;
Alex Elderbb23e372013-05-06 09:51:29 -0500983 bool first_time = header->object_prefix == NULL;
984 struct ceph_snap_context *snapc;
985 char *object_prefix = NULL;
986 char *snap_names = NULL;
987 u64 *snap_sizes = NULL;
Alex Elderccece232012-07-10 20:30:10 -0500988 u32 snap_count;
Alex Elderd2bb24e2012-07-26 23:37:14 -0500989 size_t size;
Alex Elderbb23e372013-05-06 09:51:29 -0500990 int ret = -ENOMEM;
Alex Elder621901d2012-08-23 23:22:06 -0500991 u32 i;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700992
Alex Elderbb23e372013-05-06 09:51:29 -0500993 /* Allocate this now to avoid having to handle failure below */
994
995 if (first_time) {
996 size_t len;
997
998 len = strnlen(ondisk->object_prefix,
999 sizeof (ondisk->object_prefix));
1000 object_prefix = kmalloc(len + 1, GFP_KERNEL);
1001 if (!object_prefix)
1002 return -ENOMEM;
1003 memcpy(object_prefix, ondisk->object_prefix, len);
1004 object_prefix[len] = '\0';
1005 }
1006
1007 /* Allocate the snapshot context and fill it in */
Alex Elder6a523252012-07-19 17:12:59 -05001008
Alex Elder103a1502012-08-02 11:29:45 -05001009 snap_count = le32_to_cpu(ondisk->snap_count);
Alex Elderbb23e372013-05-06 09:51:29 -05001010 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
1011 if (!snapc)
1012 goto out_err;
1013 snapc->seq = le64_to_cpu(ondisk->snap_seq);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001014 if (snap_count) {
Alex Elderbb23e372013-05-06 09:51:29 -05001015 struct rbd_image_snap_ondisk *snaps;
Alex Elderf785cc12012-08-23 23:22:06 -05001016 u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len);
1017
Alex Elderbb23e372013-05-06 09:51:29 -05001018 /* We'll keep a copy of the snapshot names... */
Alex Elder621901d2012-08-23 23:22:06 -05001019
Alex Elderbb23e372013-05-06 09:51:29 -05001020 if (snap_names_len > (u64)SIZE_MAX)
1021 goto out_2big;
1022 snap_names = kmalloc(snap_names_len, GFP_KERNEL);
1023 if (!snap_names)
Alex Elder6a523252012-07-19 17:12:59 -05001024 goto out_err;
Alex Elderbb23e372013-05-06 09:51:29 -05001025
1026 /* ...as well as the array of their sizes. */
1027
1028 size = snap_count * sizeof (*header->snap_sizes);
1029 snap_sizes = kmalloc(size, GFP_KERNEL);
1030 if (!snap_sizes)
1031 goto out_err;
1032
Alex Elderf785cc12012-08-23 23:22:06 -05001033 /*
Alex Elderbb23e372013-05-06 09:51:29 -05001034 * Copy the names, and fill in each snapshot's id
1035 * and size.
1036 *
Alex Elder99a41eb2013-05-06 09:51:30 -05001037 * Note that rbd_dev_v1_header_info() guarantees the
Alex Elderbb23e372013-05-06 09:51:29 -05001038 * ondisk buffer we're working with has
Alex Elderf785cc12012-08-23 23:22:06 -05001039 * snap_names_len bytes beyond the end of the
1040 * snapshot id array, this memcpy() is safe.
1041 */
Alex Elderbb23e372013-05-06 09:51:29 -05001042 memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len);
1043 snaps = ondisk->snaps;
1044 for (i = 0; i < snap_count; i++) {
1045 snapc->snaps[i] = le64_to_cpu(snaps[i].id);
1046 snap_sizes[i] = le64_to_cpu(snaps[i].image_size);
1047 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001048 }
Alex Elder849b4262012-07-09 21:04:24 -05001049
Alex Elderbb23e372013-05-06 09:51:29 -05001050 /* We won't fail any more, fill in the header */
Alex Elder6a523252012-07-19 17:12:59 -05001051
Alex Elderbb23e372013-05-06 09:51:29 -05001052 if (first_time) {
1053 header->object_prefix = object_prefix;
1054 header->obj_order = ondisk->options.order;
1055 header->crypt_type = ondisk->options.crypt_type;
1056 header->comp_type = ondisk->options.comp_type;
1057 /* The rest aren't used for format 1 images */
1058 header->stripe_unit = 0;
1059 header->stripe_count = 0;
1060 header->features = 0;
Alex Elder662518b2013-05-06 09:51:29 -05001061 } else {
1062 ceph_put_snap_context(header->snapc);
1063 kfree(header->snap_names);
1064 kfree(header->snap_sizes);
Alex Elderbb23e372013-05-06 09:51:29 -05001065 }
1066
1067 /* The remaining fields always get updated (when we refresh) */
Alex Elder621901d2012-08-23 23:22:06 -05001068
Alex Elderf84344f2012-08-31 17:29:51 -05001069 header->image_size = le64_to_cpu(ondisk->image_size);
Alex Elderbb23e372013-05-06 09:51:29 -05001070 header->snapc = snapc;
1071 header->snap_names = snap_names;
1072 header->snap_sizes = snap_sizes;
Alex Elder468521c2013-04-26 09:43:47 -05001073
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001074 return 0;
Alex Elderbb23e372013-05-06 09:51:29 -05001075out_2big:
1076 ret = -EIO;
Alex Elder6a523252012-07-19 17:12:59 -05001077out_err:
Alex Elderbb23e372013-05-06 09:51:29 -05001078 kfree(snap_sizes);
1079 kfree(snap_names);
1080 ceph_put_snap_context(snapc);
1081 kfree(object_prefix);
Alex Elderccece232012-07-10 20:30:10 -05001082
Alex Elderbb23e372013-05-06 09:51:29 -05001083 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001084}
1085
Alex Elder9682fc62013-04-30 00:44:33 -05001086static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which)
1087{
1088 const char *snap_name;
1089
1090 rbd_assert(which < rbd_dev->header.snapc->num_snaps);
1091
1092 /* Skip over names until we find the one we are looking for */
1093
1094 snap_name = rbd_dev->header.snap_names;
1095 while (which--)
1096 snap_name += strlen(snap_name) + 1;
1097
1098 return kstrdup(snap_name, GFP_KERNEL);
1099}
1100
Alex Elder30d1cff2013-05-01 12:43:03 -05001101/*
1102 * Snapshot id comparison function for use with qsort()/bsearch().
1103 * Note that result is for snapshots in *descending* order.
1104 */
1105static int snapid_compare_reverse(const void *s1, const void *s2)
1106{
1107 u64 snap_id1 = *(u64 *)s1;
1108 u64 snap_id2 = *(u64 *)s2;
1109
1110 if (snap_id1 < snap_id2)
1111 return 1;
1112 return snap_id1 == snap_id2 ? 0 : -1;
1113}
1114
1115/*
1116 * Search a snapshot context to see if the given snapshot id is
1117 * present.
1118 *
1119 * Returns the position of the snapshot id in the array if it's found,
1120 * or BAD_SNAP_INDEX otherwise.
1121 *
1122 * Note: The snapshot array is in kept sorted (by the osd) in
1123 * reverse order, highest snapshot id first.
1124 */
Alex Elder9682fc62013-04-30 00:44:33 -05001125static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id)
1126{
1127 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
Alex Elder30d1cff2013-05-01 12:43:03 -05001128 u64 *found;
Alex Elder9682fc62013-04-30 00:44:33 -05001129
Alex Elder30d1cff2013-05-01 12:43:03 -05001130 found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps,
1131 sizeof (snap_id), snapid_compare_reverse);
Alex Elder9682fc62013-04-30 00:44:33 -05001132
Alex Elder30d1cff2013-05-01 12:43:03 -05001133 return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX;
Alex Elder9682fc62013-04-30 00:44:33 -05001134}
1135
Alex Elder2ad3d712013-04-30 00:44:33 -05001136static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev,
1137 u64 snap_id)
Alex Elder54cac612013-04-30 00:44:33 -05001138{
1139 u32 which;
Josh Durginda6a6b62013-09-04 17:57:31 -07001140 const char *snap_name;
Alex Elder54cac612013-04-30 00:44:33 -05001141
1142 which = rbd_dev_snap_index(rbd_dev, snap_id);
1143 if (which == BAD_SNAP_INDEX)
Josh Durginda6a6b62013-09-04 17:57:31 -07001144 return ERR_PTR(-ENOENT);
Alex Elder54cac612013-04-30 00:44:33 -05001145
Josh Durginda6a6b62013-09-04 17:57:31 -07001146 snap_name = _rbd_dev_v1_snap_name(rbd_dev, which);
1147 return snap_name ? snap_name : ERR_PTR(-ENOMEM);
Alex Elder54cac612013-04-30 00:44:33 -05001148}
1149
Alex Elder9e15b772012-10-30 19:40:33 -05001150static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id)
1151{
Alex Elder9e15b772012-10-30 19:40:33 -05001152 if (snap_id == CEPH_NOSNAP)
1153 return RBD_SNAP_HEAD_NAME;
1154
Alex Elder54cac612013-04-30 00:44:33 -05001155 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1156 if (rbd_dev->image_format == 1)
1157 return rbd_dev_v1_snap_name(rbd_dev, snap_id);
Alex Elder9e15b772012-10-30 19:40:33 -05001158
Alex Elder54cac612013-04-30 00:44:33 -05001159 return rbd_dev_v2_snap_name(rbd_dev, snap_id);
Alex Elder9e15b772012-10-30 19:40:33 -05001160}
1161
Alex Elder2ad3d712013-04-30 00:44:33 -05001162static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
1163 u64 *snap_size)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001164{
Alex Elder2ad3d712013-04-30 00:44:33 -05001165 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1166 if (snap_id == CEPH_NOSNAP) {
1167 *snap_size = rbd_dev->header.image_size;
1168 } else if (rbd_dev->image_format == 1) {
1169 u32 which;
Alex Elder00f1f362012-02-07 12:03:36 -06001170
Alex Elder2ad3d712013-04-30 00:44:33 -05001171 which = rbd_dev_snap_index(rbd_dev, snap_id);
1172 if (which == BAD_SNAP_INDEX)
1173 return -ENOENT;
Alex Elder00f1f362012-02-07 12:03:36 -06001174
Alex Elder2ad3d712013-04-30 00:44:33 -05001175 *snap_size = rbd_dev->header.snap_sizes[which];
1176 } else {
1177 u64 size = 0;
1178 int ret;
1179
1180 ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size);
1181 if (ret)
1182 return ret;
1183
1184 *snap_size = size;
1185 }
1186 return 0;
1187}
1188
1189static int rbd_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
1190 u64 *snap_features)
1191{
1192 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1193 if (snap_id == CEPH_NOSNAP) {
1194 *snap_features = rbd_dev->header.features;
1195 } else if (rbd_dev->image_format == 1) {
1196 *snap_features = 0; /* No features for format 1 */
1197 } else {
1198 u64 features = 0;
1199 int ret;
1200
1201 ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, &features);
1202 if (ret)
1203 return ret;
1204
1205 *snap_features = features;
1206 }
1207 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001208}
1209
Alex Elderd1cf5782013-04-27 09:59:30 -05001210static int rbd_dev_mapping_set(struct rbd_device *rbd_dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001211{
Alex Elder8f4b7d92013-05-06 07:40:30 -05001212 u64 snap_id = rbd_dev->spec->snap_id;
Alex Elder2ad3d712013-04-30 00:44:33 -05001213 u64 size = 0;
1214 u64 features = 0;
1215 int ret;
Alex Elder8b0241f2013-04-25 23:15:08 -05001216
Alex Elder2ad3d712013-04-30 00:44:33 -05001217 ret = rbd_snap_size(rbd_dev, snap_id, &size);
1218 if (ret)
1219 return ret;
1220 ret = rbd_snap_features(rbd_dev, snap_id, &features);
1221 if (ret)
1222 return ret;
1223
1224 rbd_dev->mapping.size = size;
1225 rbd_dev->mapping.features = features;
1226
Alex Elder8b0241f2013-04-25 23:15:08 -05001227 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001228}
1229
Alex Elderd1cf5782013-04-27 09:59:30 -05001230static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev)
1231{
1232 rbd_dev->mapping.size = 0;
1233 rbd_dev->mapping.features = 0;
Alex Elder200a6a82013-04-28 23:32:34 -05001234}
1235
Himangi Saraogi7d5079a2014-07-24 03:17:07 +05301236static void rbd_segment_name_free(const char *name)
1237{
1238 /* The explicit cast here is needed to drop the const qualifier */
1239
1240 kmem_cache_free(rbd_segment_name_cache, (void *)name);
1241}
1242
Alex Elder98571b52013-01-20 14:44:42 -06001243static const char *rbd_segment_name(struct rbd_device *rbd_dev, u64 offset)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001244{
Alex Elder65ccfe22012-08-09 10:33:26 -07001245 char *name;
1246 u64 segment;
1247 int ret;
Josh Durgin3a96d5c2013-06-12 19:15:06 -07001248 char *name_format;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001249
Alex Elder78c2a442013-05-01 12:43:04 -05001250 name = kmem_cache_alloc(rbd_segment_name_cache, GFP_NOIO);
Alex Elder65ccfe22012-08-09 10:33:26 -07001251 if (!name)
1252 return NULL;
1253 segment = offset >> rbd_dev->header.obj_order;
Josh Durgin3a96d5c2013-06-12 19:15:06 -07001254 name_format = "%s.%012llx";
1255 if (rbd_dev->image_format == 2)
1256 name_format = "%s.%016llx";
Ilya Dryomov2d0ebc52014-01-27 17:40:18 +02001257 ret = snprintf(name, CEPH_MAX_OID_NAME_LEN + 1, name_format,
Alex Elder65ccfe22012-08-09 10:33:26 -07001258 rbd_dev->header.object_prefix, segment);
Ilya Dryomov2d0ebc52014-01-27 17:40:18 +02001259 if (ret < 0 || ret > CEPH_MAX_OID_NAME_LEN) {
Alex Elder65ccfe22012-08-09 10:33:26 -07001260 pr_err("error formatting segment name for #%llu (%d)\n",
1261 segment, ret);
Himangi Saraogi7d5079a2014-07-24 03:17:07 +05301262 rbd_segment_name_free(name);
Alex Elder65ccfe22012-08-09 10:33:26 -07001263 name = NULL;
1264 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001265
Alex Elder65ccfe22012-08-09 10:33:26 -07001266 return name;
1267}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001268
Alex Elder65ccfe22012-08-09 10:33:26 -07001269static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset)
1270{
1271 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001272
Alex Elder65ccfe22012-08-09 10:33:26 -07001273 return offset & (segment_size - 1);
1274}
1275
1276static u64 rbd_segment_length(struct rbd_device *rbd_dev,
1277 u64 offset, u64 length)
1278{
1279 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
1280
1281 offset &= segment_size - 1;
1282
Alex Elderaafb2302012-09-06 16:00:54 -05001283 rbd_assert(length <= U64_MAX - offset);
Alex Elder65ccfe22012-08-09 10:33:26 -07001284 if (offset + length > segment_size)
1285 length = segment_size - offset;
1286
1287 return length;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001288}
1289
1290/*
Josh Durgin029bcbd2011-07-22 11:35:23 -07001291 * returns the size of an object in the image
1292 */
1293static u64 rbd_obj_bytes(struct rbd_image_header *header)
1294{
1295 return 1 << header->obj_order;
1296}
1297
1298/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001299 * bio helpers
1300 */
1301
1302static void bio_chain_put(struct bio *chain)
1303{
1304 struct bio *tmp;
1305
1306 while (chain) {
1307 tmp = chain;
1308 chain = chain->bi_next;
1309 bio_put(tmp);
1310 }
1311}
1312
1313/*
1314 * zeros a bio chain, starting at specific offset
1315 */
1316static void zero_bio_chain(struct bio *chain, int start_ofs)
1317{
Kent Overstreet79886132013-11-23 17:19:00 -08001318 struct bio_vec bv;
1319 struct bvec_iter iter;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001320 unsigned long flags;
1321 void *buf;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001322 int pos = 0;
1323
1324 while (chain) {
Kent Overstreet79886132013-11-23 17:19:00 -08001325 bio_for_each_segment(bv, chain, iter) {
1326 if (pos + bv.bv_len > start_ofs) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001327 int remainder = max(start_ofs - pos, 0);
Kent Overstreet79886132013-11-23 17:19:00 -08001328 buf = bvec_kmap_irq(&bv, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001329 memset(buf + remainder, 0,
Kent Overstreet79886132013-11-23 17:19:00 -08001330 bv.bv_len - remainder);
1331 flush_dcache_page(bv.bv_page);
Dan Carpenter85b5aaa2010-10-11 21:15:11 +02001332 bvec_kunmap_irq(buf, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001333 }
Kent Overstreet79886132013-11-23 17:19:00 -08001334 pos += bv.bv_len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001335 }
1336
1337 chain = chain->bi_next;
1338 }
1339}
1340
1341/*
Alex Elderb9434c52013-04-19 15:34:50 -05001342 * similar to zero_bio_chain(), zeros data defined by a page array,
1343 * starting at the given byte offset from the start of the array and
1344 * continuing up to the given end offset. The pages array is
1345 * assumed to be big enough to hold all bytes up to the end.
1346 */
1347static void zero_pages(struct page **pages, u64 offset, u64 end)
1348{
1349 struct page **page = &pages[offset >> PAGE_SHIFT];
1350
1351 rbd_assert(end > offset);
1352 rbd_assert(end - offset <= (u64)SIZE_MAX);
1353 while (offset < end) {
1354 size_t page_offset;
1355 size_t length;
1356 unsigned long flags;
1357 void *kaddr;
1358
Geert Uytterhoeven491205a2013-05-13 20:35:37 -05001359 page_offset = offset & ~PAGE_MASK;
1360 length = min_t(size_t, PAGE_SIZE - page_offset, end - offset);
Alex Elderb9434c52013-04-19 15:34:50 -05001361 local_irq_save(flags);
1362 kaddr = kmap_atomic(*page);
1363 memset(kaddr + page_offset, 0, length);
Alex Eldere2156052013-05-22 20:54:25 -05001364 flush_dcache_page(*page);
Alex Elderb9434c52013-04-19 15:34:50 -05001365 kunmap_atomic(kaddr);
1366 local_irq_restore(flags);
1367
1368 offset += length;
1369 page++;
1370 }
1371}
1372
1373/*
Alex Elderf7760da2012-10-20 22:17:27 -05001374 * Clone a portion of a bio, starting at the given byte offset
1375 * and continuing for the number of bytes indicated.
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001376 */
Alex Elderf7760da2012-10-20 22:17:27 -05001377static struct bio *bio_clone_range(struct bio *bio_src,
1378 unsigned int offset,
1379 unsigned int len,
1380 gfp_t gfpmask)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001381{
Alex Elderf7760da2012-10-20 22:17:27 -05001382 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001383
Kent Overstreet5341a6272013-08-07 14:31:11 -07001384 bio = bio_clone(bio_src, gfpmask);
Alex Elderf7760da2012-10-20 22:17:27 -05001385 if (!bio)
1386 return NULL; /* ENOMEM */
1387
Kent Overstreet5341a6272013-08-07 14:31:11 -07001388 bio_advance(bio, offset);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001389 bio->bi_iter.bi_size = len;
Alex Elder542582f2012-08-09 10:33:25 -07001390
Alex Elderf7760da2012-10-20 22:17:27 -05001391 return bio;
1392}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001393
Alex Elderf7760da2012-10-20 22:17:27 -05001394/*
1395 * Clone a portion of a bio chain, starting at the given byte offset
1396 * into the first bio in the source chain and continuing for the
1397 * number of bytes indicated. The result is another bio chain of
1398 * exactly the given length, or a null pointer on error.
1399 *
1400 * The bio_src and offset parameters are both in-out. On entry they
1401 * refer to the first source bio and the offset into that bio where
1402 * the start of data to be cloned is located.
1403 *
1404 * On return, bio_src is updated to refer to the bio in the source
1405 * chain that contains first un-cloned byte, and *offset will
1406 * contain the offset of that byte within that bio.
1407 */
1408static struct bio *bio_chain_clone_range(struct bio **bio_src,
1409 unsigned int *offset,
1410 unsigned int len,
1411 gfp_t gfpmask)
1412{
1413 struct bio *bi = *bio_src;
1414 unsigned int off = *offset;
1415 struct bio *chain = NULL;
1416 struct bio **end;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001417
Alex Elderf7760da2012-10-20 22:17:27 -05001418 /* Build up a chain of clone bios up to the limit */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001419
Kent Overstreet4f024f32013-10-11 15:44:27 -07001420 if (!bi || off >= bi->bi_iter.bi_size || !len)
Alex Elderf7760da2012-10-20 22:17:27 -05001421 return NULL; /* Nothing to clone */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001422
Alex Elderf7760da2012-10-20 22:17:27 -05001423 end = &chain;
1424 while (len) {
1425 unsigned int bi_size;
1426 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001427
Alex Elderf5400b72012-11-01 10:17:15 -05001428 if (!bi) {
1429 rbd_warn(NULL, "bio_chain exhausted with %u left", len);
Alex Elderf7760da2012-10-20 22:17:27 -05001430 goto out_err; /* EINVAL; ran out of bio's */
Alex Elderf5400b72012-11-01 10:17:15 -05001431 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07001432 bi_size = min_t(unsigned int, bi->bi_iter.bi_size - off, len);
Alex Elderf7760da2012-10-20 22:17:27 -05001433 bio = bio_clone_range(bi, off, bi_size, gfpmask);
1434 if (!bio)
1435 goto out_err; /* ENOMEM */
1436
1437 *end = bio;
1438 end = &bio->bi_next;
1439
1440 off += bi_size;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001441 if (off == bi->bi_iter.bi_size) {
Alex Elderf7760da2012-10-20 22:17:27 -05001442 bi = bi->bi_next;
1443 off = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001444 }
Alex Elderf7760da2012-10-20 22:17:27 -05001445 len -= bi_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001446 }
Alex Elderf7760da2012-10-20 22:17:27 -05001447 *bio_src = bi;
1448 *offset = off;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001449
Alex Elderf7760da2012-10-20 22:17:27 -05001450 return chain;
1451out_err:
1452 bio_chain_put(chain);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001453
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001454 return NULL;
1455}
1456
Alex Elder926f9b32013-02-11 12:33:24 -06001457/*
1458 * The default/initial value for all object request flags is 0. For
1459 * each flag, once its value is set to 1 it is never reset to 0
1460 * again.
1461 */
Alex Elder6365d332013-02-11 12:33:24 -06001462static void obj_request_img_data_set(struct rbd_obj_request *obj_request)
1463{
1464 if (test_and_set_bit(OBJ_REQ_IMG_DATA, &obj_request->flags)) {
Alex Elder6365d332013-02-11 12:33:24 -06001465 struct rbd_device *rbd_dev;
1466
Alex Elder57acbaa2013-02-11 12:33:24 -06001467 rbd_dev = obj_request->img_request->rbd_dev;
Ilya Dryomov9584d502014-07-11 12:11:20 +04001468 rbd_warn(rbd_dev, "obj_request %p already marked img_data",
Alex Elder6365d332013-02-11 12:33:24 -06001469 obj_request);
1470 }
1471}
1472
1473static bool obj_request_img_data_test(struct rbd_obj_request *obj_request)
1474{
1475 smp_mb();
1476 return test_bit(OBJ_REQ_IMG_DATA, &obj_request->flags) != 0;
1477}
1478
Alex Elder57acbaa2013-02-11 12:33:24 -06001479static void obj_request_done_set(struct rbd_obj_request *obj_request)
1480{
1481 if (test_and_set_bit(OBJ_REQ_DONE, &obj_request->flags)) {
1482 struct rbd_device *rbd_dev = NULL;
1483
1484 if (obj_request_img_data_test(obj_request))
1485 rbd_dev = obj_request->img_request->rbd_dev;
Ilya Dryomov9584d502014-07-11 12:11:20 +04001486 rbd_warn(rbd_dev, "obj_request %p already marked done",
Alex Elder57acbaa2013-02-11 12:33:24 -06001487 obj_request);
1488 }
1489}
1490
1491static bool obj_request_done_test(struct rbd_obj_request *obj_request)
1492{
1493 smp_mb();
1494 return test_bit(OBJ_REQ_DONE, &obj_request->flags) != 0;
1495}
1496
Alex Elder5679c592013-02-11 12:33:24 -06001497/*
1498 * This sets the KNOWN flag after (possibly) setting the EXISTS
1499 * flag. The latter is set based on the "exists" value provided.
1500 *
1501 * Note that for our purposes once an object exists it never goes
1502 * away again. It's possible that the response from two existence
1503 * checks are separated by the creation of the target object, and
1504 * the first ("doesn't exist") response arrives *after* the second
1505 * ("does exist"). In that case we ignore the second one.
1506 */
1507static void obj_request_existence_set(struct rbd_obj_request *obj_request,
1508 bool exists)
1509{
1510 if (exists)
1511 set_bit(OBJ_REQ_EXISTS, &obj_request->flags);
1512 set_bit(OBJ_REQ_KNOWN, &obj_request->flags);
1513 smp_mb();
1514}
1515
1516static bool obj_request_known_test(struct rbd_obj_request *obj_request)
1517{
1518 smp_mb();
1519 return test_bit(OBJ_REQ_KNOWN, &obj_request->flags) != 0;
1520}
1521
1522static bool obj_request_exists_test(struct rbd_obj_request *obj_request)
1523{
1524 smp_mb();
1525 return test_bit(OBJ_REQ_EXISTS, &obj_request->flags) != 0;
1526}
1527
Ilya Dryomov96385562014-06-10 13:53:29 +04001528static bool obj_request_overlaps_parent(struct rbd_obj_request *obj_request)
1529{
1530 struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev;
1531
1532 return obj_request->img_offset <
1533 round_up(rbd_dev->parent_overlap, rbd_obj_bytes(&rbd_dev->header));
1534}
1535
Alex Elderbf0d5f502012-11-22 00:00:08 -06001536static void rbd_obj_request_get(struct rbd_obj_request *obj_request)
1537{
Alex Elder37206ee2013-02-20 17:32:08 -06001538 dout("%s: obj %p (was %d)\n", __func__, obj_request,
1539 atomic_read(&obj_request->kref.refcount));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001540 kref_get(&obj_request->kref);
1541}
1542
1543static void rbd_obj_request_destroy(struct kref *kref);
1544static void rbd_obj_request_put(struct rbd_obj_request *obj_request)
1545{
1546 rbd_assert(obj_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001547 dout("%s: obj %p (was %d)\n", __func__, obj_request,
1548 atomic_read(&obj_request->kref.refcount));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001549 kref_put(&obj_request->kref, rbd_obj_request_destroy);
1550}
1551
Alex Elder0f2d5be2014-04-26 14:21:44 +04001552static void rbd_img_request_get(struct rbd_img_request *img_request)
1553{
1554 dout("%s: img %p (was %d)\n", __func__, img_request,
1555 atomic_read(&img_request->kref.refcount));
1556 kref_get(&img_request->kref);
1557}
1558
Alex Eldere93f3152013-05-08 22:50:04 -05001559static bool img_request_child_test(struct rbd_img_request *img_request);
1560static void rbd_parent_request_destroy(struct kref *kref);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001561static void rbd_img_request_destroy(struct kref *kref);
1562static void rbd_img_request_put(struct rbd_img_request *img_request)
1563{
1564 rbd_assert(img_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001565 dout("%s: img %p (was %d)\n", __func__, img_request,
1566 atomic_read(&img_request->kref.refcount));
Alex Eldere93f3152013-05-08 22:50:04 -05001567 if (img_request_child_test(img_request))
1568 kref_put(&img_request->kref, rbd_parent_request_destroy);
1569 else
1570 kref_put(&img_request->kref, rbd_img_request_destroy);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001571}
1572
1573static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request,
1574 struct rbd_obj_request *obj_request)
1575{
Alex Elder25dcf952013-01-25 17:08:55 -06001576 rbd_assert(obj_request->img_request == NULL);
1577
Alex Elderb155e862013-04-15 14:50:37 -05001578 /* Image request now owns object's original reference */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001579 obj_request->img_request = img_request;
Alex Elder25dcf952013-01-25 17:08:55 -06001580 obj_request->which = img_request->obj_request_count;
Alex Elder6365d332013-02-11 12:33:24 -06001581 rbd_assert(!obj_request_img_data_test(obj_request));
1582 obj_request_img_data_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001583 rbd_assert(obj_request->which != BAD_WHICH);
Alex Elder25dcf952013-01-25 17:08:55 -06001584 img_request->obj_request_count++;
1585 list_add_tail(&obj_request->links, &img_request->obj_requests);
Alex Elder37206ee2013-02-20 17:32:08 -06001586 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1587 obj_request->which);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001588}
1589
1590static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request,
1591 struct rbd_obj_request *obj_request)
1592{
1593 rbd_assert(obj_request->which != BAD_WHICH);
Alex Elder25dcf952013-01-25 17:08:55 -06001594
Alex Elder37206ee2013-02-20 17:32:08 -06001595 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1596 obj_request->which);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001597 list_del(&obj_request->links);
Alex Elder25dcf952013-01-25 17:08:55 -06001598 rbd_assert(img_request->obj_request_count > 0);
1599 img_request->obj_request_count--;
1600 rbd_assert(obj_request->which == img_request->obj_request_count);
1601 obj_request->which = BAD_WHICH;
Alex Elder6365d332013-02-11 12:33:24 -06001602 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001603 rbd_assert(obj_request->img_request == img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001604 obj_request->img_request = NULL;
Alex Elder25dcf952013-01-25 17:08:55 -06001605 obj_request->callback = NULL;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001606 rbd_obj_request_put(obj_request);
1607}
1608
1609static bool obj_request_type_valid(enum obj_request_type type)
1610{
1611 switch (type) {
Alex Elder9969ebc2013-01-18 12:31:10 -06001612 case OBJ_REQUEST_NODATA:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001613 case OBJ_REQUEST_BIO:
Alex Elder788e2df2013-01-17 12:25:27 -06001614 case OBJ_REQUEST_PAGES:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001615 return true;
1616 default:
1617 return false;
1618 }
1619}
1620
Alex Elderbf0d5f502012-11-22 00:00:08 -06001621static int rbd_obj_request_submit(struct ceph_osd_client *osdc,
1622 struct rbd_obj_request *obj_request)
1623{
Ilya Dryomov71c20a02014-06-19 11:38:14 +04001624 dout("%s %p\n", __func__, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001625 return ceph_osdc_start_request(osdc, obj_request->osd_req, false);
1626}
1627
Ilya Dryomov71c20a02014-06-19 11:38:14 +04001628static void rbd_obj_request_end(struct rbd_obj_request *obj_request)
1629{
1630 dout("%s %p\n", __func__, obj_request);
1631 ceph_osdc_cancel_request(obj_request->osd_req);
1632}
1633
1634/*
1635 * Wait for an object request to complete. If interrupted, cancel the
1636 * underlying osd request.
Ilya Dryomov2894e1d2015-05-12 19:53:24 +03001637 *
1638 * @timeout: in jiffies, 0 means "wait forever"
Ilya Dryomov71c20a02014-06-19 11:38:14 +04001639 */
Ilya Dryomov2894e1d2015-05-12 19:53:24 +03001640static int __rbd_obj_request_wait(struct rbd_obj_request *obj_request,
1641 unsigned long timeout)
Ilya Dryomov71c20a02014-06-19 11:38:14 +04001642{
Ilya Dryomov2894e1d2015-05-12 19:53:24 +03001643 long ret;
Ilya Dryomov71c20a02014-06-19 11:38:14 +04001644
1645 dout("%s %p\n", __func__, obj_request);
Ilya Dryomov2894e1d2015-05-12 19:53:24 +03001646 ret = wait_for_completion_interruptible_timeout(
1647 &obj_request->completion,
1648 ceph_timeout_jiffies(timeout));
1649 if (ret <= 0) {
1650 if (ret == 0)
1651 ret = -ETIMEDOUT;
Ilya Dryomov71c20a02014-06-19 11:38:14 +04001652 rbd_obj_request_end(obj_request);
Ilya Dryomov2894e1d2015-05-12 19:53:24 +03001653 } else {
1654 ret = 0;
Ilya Dryomov71c20a02014-06-19 11:38:14 +04001655 }
1656
Ilya Dryomov2894e1d2015-05-12 19:53:24 +03001657 dout("%s %p ret %d\n", __func__, obj_request, (int)ret);
1658 return ret;
1659}
1660
1661static int rbd_obj_request_wait(struct rbd_obj_request *obj_request)
1662{
1663 return __rbd_obj_request_wait(obj_request, 0);
1664}
1665
Alex Elderbf0d5f502012-11-22 00:00:08 -06001666static void rbd_img_request_complete(struct rbd_img_request *img_request)
1667{
Alex Elder55f27e02013-04-10 12:34:25 -05001668
Alex Elder37206ee2013-02-20 17:32:08 -06001669 dout("%s: img %p\n", __func__, img_request);
Alex Elder55f27e02013-04-10 12:34:25 -05001670
1671 /*
1672 * If no error occurred, compute the aggregate transfer
1673 * count for the image request. We could instead use
1674 * atomic64_cmpxchg() to update it as each object request
1675 * completes; not clear which way is better off hand.
1676 */
1677 if (!img_request->result) {
1678 struct rbd_obj_request *obj_request;
1679 u64 xferred = 0;
1680
1681 for_each_obj_request(img_request, obj_request)
1682 xferred += obj_request->xferred;
1683 img_request->xferred = xferred;
1684 }
1685
Alex Elderbf0d5f502012-11-22 00:00:08 -06001686 if (img_request->callback)
1687 img_request->callback(img_request);
1688 else
1689 rbd_img_request_put(img_request);
1690}
1691
Alex Elder0c425242013-02-08 09:55:49 -06001692/*
1693 * The default/initial value for all image request flags is 0. Each
1694 * is conditionally set to 1 at image request initialization time
1695 * and currently never change thereafter.
1696 */
1697static void img_request_write_set(struct rbd_img_request *img_request)
1698{
1699 set_bit(IMG_REQ_WRITE, &img_request->flags);
1700 smp_mb();
1701}
1702
1703static bool img_request_write_test(struct rbd_img_request *img_request)
1704{
1705 smp_mb();
1706 return test_bit(IMG_REQ_WRITE, &img_request->flags) != 0;
1707}
1708
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001709/*
1710 * Set the discard flag when the img_request is an discard request
1711 */
1712static void img_request_discard_set(struct rbd_img_request *img_request)
1713{
1714 set_bit(IMG_REQ_DISCARD, &img_request->flags);
1715 smp_mb();
1716}
1717
1718static bool img_request_discard_test(struct rbd_img_request *img_request)
1719{
1720 smp_mb();
1721 return test_bit(IMG_REQ_DISCARD, &img_request->flags) != 0;
1722}
1723
Alex Elder9849e982013-01-24 16:13:36 -06001724static void img_request_child_set(struct rbd_img_request *img_request)
1725{
1726 set_bit(IMG_REQ_CHILD, &img_request->flags);
1727 smp_mb();
1728}
1729
Alex Eldere93f3152013-05-08 22:50:04 -05001730static void img_request_child_clear(struct rbd_img_request *img_request)
1731{
1732 clear_bit(IMG_REQ_CHILD, &img_request->flags);
1733 smp_mb();
1734}
1735
Alex Elder9849e982013-01-24 16:13:36 -06001736static bool img_request_child_test(struct rbd_img_request *img_request)
1737{
1738 smp_mb();
1739 return test_bit(IMG_REQ_CHILD, &img_request->flags) != 0;
1740}
1741
Alex Elderd0b2e942013-01-24 16:13:36 -06001742static void img_request_layered_set(struct rbd_img_request *img_request)
1743{
1744 set_bit(IMG_REQ_LAYERED, &img_request->flags);
1745 smp_mb();
1746}
1747
Alex Eldera2acd002013-05-08 22:50:04 -05001748static void img_request_layered_clear(struct rbd_img_request *img_request)
1749{
1750 clear_bit(IMG_REQ_LAYERED, &img_request->flags);
1751 smp_mb();
1752}
1753
Alex Elderd0b2e942013-01-24 16:13:36 -06001754static bool img_request_layered_test(struct rbd_img_request *img_request)
1755{
1756 smp_mb();
1757 return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0;
1758}
1759
Josh Durgin3b434a2a2014-04-04 17:32:15 -07001760static enum obj_operation_type
1761rbd_img_request_op_type(struct rbd_img_request *img_request)
1762{
1763 if (img_request_write_test(img_request))
1764 return OBJ_OP_WRITE;
1765 else if (img_request_discard_test(img_request))
1766 return OBJ_OP_DISCARD;
1767 else
1768 return OBJ_OP_READ;
1769}
1770
Alex Elder6e2a4502013-03-27 09:16:30 -05001771static void
1772rbd_img_obj_request_read_callback(struct rbd_obj_request *obj_request)
1773{
Alex Elderb9434c52013-04-19 15:34:50 -05001774 u64 xferred = obj_request->xferred;
1775 u64 length = obj_request->length;
1776
Alex Elder6e2a4502013-03-27 09:16:30 -05001777 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1778 obj_request, obj_request->img_request, obj_request->result,
Alex Elderb9434c52013-04-19 15:34:50 -05001779 xferred, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001780 /*
Josh Durgin17c1cc12013-08-26 17:55:38 -07001781 * ENOENT means a hole in the image. We zero-fill the entire
1782 * length of the request. A short read also implies zero-fill
1783 * to the end of the request. An error requires the whole
1784 * length of the request to be reported finished with an error
1785 * to the block layer. In each case we update the xferred
1786 * count to indicate the whole request was satisfied.
Alex Elder6e2a4502013-03-27 09:16:30 -05001787 */
Alex Elderb9434c52013-04-19 15:34:50 -05001788 rbd_assert(obj_request->type != OBJ_REQUEST_NODATA);
Alex Elder6e2a4502013-03-27 09:16:30 -05001789 if (obj_request->result == -ENOENT) {
Alex Elderb9434c52013-04-19 15:34:50 -05001790 if (obj_request->type == OBJ_REQUEST_BIO)
1791 zero_bio_chain(obj_request->bio_list, 0);
1792 else
1793 zero_pages(obj_request->pages, 0, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001794 obj_request->result = 0;
Alex Elderb9434c52013-04-19 15:34:50 -05001795 } else if (xferred < length && !obj_request->result) {
1796 if (obj_request->type == OBJ_REQUEST_BIO)
1797 zero_bio_chain(obj_request->bio_list, xferred);
1798 else
1799 zero_pages(obj_request->pages, xferred, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001800 }
Josh Durgin17c1cc12013-08-26 17:55:38 -07001801 obj_request->xferred = length;
Alex Elder6e2a4502013-03-27 09:16:30 -05001802 obj_request_done_set(obj_request);
1803}
1804
Alex Elderbf0d5f502012-11-22 00:00:08 -06001805static void rbd_obj_request_complete(struct rbd_obj_request *obj_request)
1806{
Alex Elder37206ee2013-02-20 17:32:08 -06001807 dout("%s: obj %p cb %p\n", __func__, obj_request,
1808 obj_request->callback);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001809 if (obj_request->callback)
1810 obj_request->callback(obj_request);
Alex Elder788e2df2013-01-17 12:25:27 -06001811 else
1812 complete_all(&obj_request->completion);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001813}
1814
Alex Elderc47f9372013-02-26 14:23:07 -06001815static void rbd_osd_read_callback(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001816{
Alex Elder57acbaa2013-02-11 12:33:24 -06001817 struct rbd_img_request *img_request = NULL;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001818 struct rbd_device *rbd_dev = NULL;
Alex Elder57acbaa2013-02-11 12:33:24 -06001819 bool layered = false;
1820
1821 if (obj_request_img_data_test(obj_request)) {
1822 img_request = obj_request->img_request;
1823 layered = img_request && img_request_layered_test(img_request);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001824 rbd_dev = img_request->rbd_dev;
Alex Elder57acbaa2013-02-11 12:33:24 -06001825 }
Alex Elder8b3e1a52013-01-24 16:13:36 -06001826
1827 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1828 obj_request, img_request, obj_request->result,
1829 obj_request->xferred, obj_request->length);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001830 if (layered && obj_request->result == -ENOENT &&
1831 obj_request->img_offset < rbd_dev->parent_overlap)
Alex Elder8b3e1a52013-01-24 16:13:36 -06001832 rbd_img_parent_read(obj_request);
1833 else if (img_request)
Alex Elder6e2a4502013-03-27 09:16:30 -05001834 rbd_img_obj_request_read_callback(obj_request);
1835 else
1836 obj_request_done_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001837}
1838
Alex Elderc47f9372013-02-26 14:23:07 -06001839static void rbd_osd_write_callback(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001840{
Sage Weil1b83bef2013-02-25 16:11:12 -08001841 dout("%s: obj %p result %d %llu\n", __func__, obj_request,
1842 obj_request->result, obj_request->length);
1843 /*
Alex Elder8b3e1a52013-01-24 16:13:36 -06001844 * There is no such thing as a successful short write. Set
1845 * it to our originally-requested length.
Sage Weil1b83bef2013-02-25 16:11:12 -08001846 */
1847 obj_request->xferred = obj_request->length;
Alex Elder07741302013-02-05 23:41:50 -06001848 obj_request_done_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001849}
1850
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001851static void rbd_osd_discard_callback(struct rbd_obj_request *obj_request)
1852{
1853 dout("%s: obj %p result %d %llu\n", __func__, obj_request,
1854 obj_request->result, obj_request->length);
1855 /*
1856 * There is no such thing as a successful short discard. Set
1857 * it to our originally-requested length.
1858 */
1859 obj_request->xferred = obj_request->length;
Josh Durgind0265de2014-04-07 16:54:10 -07001860 /* discarding a non-existent object is not a problem */
1861 if (obj_request->result == -ENOENT)
1862 obj_request->result = 0;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001863 obj_request_done_set(obj_request);
1864}
1865
Alex Elderfbfab532013-02-08 09:55:48 -06001866/*
1867 * For a simple stat call there's nothing to do. We'll do more if
1868 * this is part of a write sequence for a layered image.
1869 */
Alex Elderc47f9372013-02-26 14:23:07 -06001870static void rbd_osd_stat_callback(struct rbd_obj_request *obj_request)
Alex Elderfbfab532013-02-08 09:55:48 -06001871{
Alex Elder37206ee2013-02-20 17:32:08 -06001872 dout("%s: obj %p\n", __func__, obj_request);
Alex Elderfbfab532013-02-08 09:55:48 -06001873 obj_request_done_set(obj_request);
1874}
1875
Ilya Dryomov27617132015-07-16 17:36:11 +03001876static void rbd_osd_call_callback(struct rbd_obj_request *obj_request)
1877{
1878 dout("%s: obj %p\n", __func__, obj_request);
1879
1880 if (obj_request_img_data_test(obj_request))
1881 rbd_osd_copyup_callback(obj_request);
1882 else
1883 obj_request_done_set(obj_request);
1884}
1885
Ilya Dryomov85e084f2016-04-28 16:07:24 +02001886static void rbd_osd_req_callback(struct ceph_osd_request *osd_req)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001887{
1888 struct rbd_obj_request *obj_request = osd_req->r_priv;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001889 u16 opcode;
1890
Ilya Dryomov85e084f2016-04-28 16:07:24 +02001891 dout("%s: osd_req %p\n", __func__, osd_req);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001892 rbd_assert(osd_req == obj_request->osd_req);
Alex Elder57acbaa2013-02-11 12:33:24 -06001893 if (obj_request_img_data_test(obj_request)) {
1894 rbd_assert(obj_request->img_request);
1895 rbd_assert(obj_request->which != BAD_WHICH);
1896 } else {
1897 rbd_assert(obj_request->which == BAD_WHICH);
1898 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06001899
Sage Weil1b83bef2013-02-25 16:11:12 -08001900 if (osd_req->r_result < 0)
1901 obj_request->result = osd_req->r_result;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001902
Alex Elderc47f9372013-02-26 14:23:07 -06001903 /*
1904 * We support a 64-bit length, but ultimately it has to be
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01001905 * passed to the block layer, which just supports a 32-bit
1906 * length field.
Alex Elderc47f9372013-02-26 14:23:07 -06001907 */
Yan, Zheng7665d852016-01-07 16:48:57 +08001908 obj_request->xferred = osd_req->r_ops[0].outdata_len;
Alex Elder8b3e1a52013-01-24 16:13:36 -06001909 rbd_assert(obj_request->xferred < (u64)UINT_MAX);
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001910
Alex Elder79528732013-04-03 21:32:51 -05001911 opcode = osd_req->r_ops[0].op;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001912 switch (opcode) {
1913 case CEPH_OSD_OP_READ:
Alex Elderc47f9372013-02-26 14:23:07 -06001914 rbd_osd_read_callback(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001915 break;
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001916 case CEPH_OSD_OP_SETALLOCHINT:
Ilya Dryomove30b7572015-10-07 17:27:17 +02001917 rbd_assert(osd_req->r_ops[1].op == CEPH_OSD_OP_WRITE ||
1918 osd_req->r_ops[1].op == CEPH_OSD_OP_WRITEFULL);
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001919 /* fall through */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001920 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +02001921 case CEPH_OSD_OP_WRITEFULL:
Alex Elderc47f9372013-02-26 14:23:07 -06001922 rbd_osd_write_callback(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001923 break;
Alex Elderfbfab532013-02-08 09:55:48 -06001924 case CEPH_OSD_OP_STAT:
Alex Elderc47f9372013-02-26 14:23:07 -06001925 rbd_osd_stat_callback(obj_request);
Alex Elderfbfab532013-02-08 09:55:48 -06001926 break;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001927 case CEPH_OSD_OP_DELETE:
1928 case CEPH_OSD_OP_TRUNCATE:
1929 case CEPH_OSD_OP_ZERO:
1930 rbd_osd_discard_callback(obj_request);
1931 break;
Alex Elder36be9a72013-01-19 00:30:28 -06001932 case CEPH_OSD_OP_CALL:
Ilya Dryomov27617132015-07-16 17:36:11 +03001933 rbd_osd_call_callback(obj_request);
1934 break;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001935 default:
Ilya Dryomov9584d502014-07-11 12:11:20 +04001936 rbd_warn(NULL, "%s: unsupported op %hu",
Alex Elderbf0d5f502012-11-22 00:00:08 -06001937 obj_request->object_name, (unsigned short) opcode);
1938 break;
1939 }
1940
Alex Elder07741302013-02-05 23:41:50 -06001941 if (obj_request_done_test(obj_request))
Alex Elderbf0d5f502012-11-22 00:00:08 -06001942 rbd_obj_request_complete(obj_request);
1943}
1944
Alex Elder9d4df012013-04-19 15:34:50 -05001945static void rbd_osd_req_format_read(struct rbd_obj_request *obj_request)
Alex Elder430c28c2013-04-03 21:32:51 -05001946{
1947 struct rbd_img_request *img_request = obj_request->img_request;
Alex Elder8c042b02013-04-03 01:28:58 -05001948 struct ceph_osd_request *osd_req = obj_request->osd_req;
Alex Elder430c28c2013-04-03 21:32:51 -05001949
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001950 if (img_request)
1951 osd_req->r_snapid = img_request->snap_id;
Alex Elder9d4df012013-04-19 15:34:50 -05001952}
1953
1954static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request)
1955{
Alex Elder9d4df012013-04-19 15:34:50 -05001956 struct ceph_osd_request *osd_req = obj_request->osd_req;
Alex Elder9d4df012013-04-19 15:34:50 -05001957
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001958 osd_req->r_mtime = CURRENT_TIME;
1959 osd_req->r_data_offset = obj_request->offset;
Alex Elder430c28c2013-04-03 21:32:51 -05001960}
1961
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001962/*
1963 * Create an osd request. A read request has one osd op (read).
1964 * A write request has either one (watch) or two (hint+write) osd ops.
1965 * (All rbd data writes are prefixed with an allocation hint op, but
1966 * technically osd watch is a write request, hence this distinction.)
1967 */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001968static struct ceph_osd_request *rbd_osd_req_create(
1969 struct rbd_device *rbd_dev,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08001970 enum obj_operation_type op_type,
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02001971 unsigned int num_ops,
Alex Elder430c28c2013-04-03 21:32:51 -05001972 struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001973{
Alex Elderbf0d5f502012-11-22 00:00:08 -06001974 struct ceph_snap_context *snapc = NULL;
1975 struct ceph_osd_client *osdc;
1976 struct ceph_osd_request *osd_req;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001977
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001978 if (obj_request_img_data_test(obj_request) &&
1979 (op_type == OBJ_OP_DISCARD || op_type == OBJ_OP_WRITE)) {
Alex Elder6365d332013-02-11 12:33:24 -06001980 struct rbd_img_request *img_request = obj_request->img_request;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001981 if (op_type == OBJ_OP_WRITE) {
1982 rbd_assert(img_request_write_test(img_request));
1983 } else {
1984 rbd_assert(img_request_discard_test(img_request));
1985 }
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08001986 snapc = img_request->snapc;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001987 }
1988
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08001989 rbd_assert(num_ops == 1 || ((op_type == OBJ_OP_WRITE) && num_ops == 2));
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02001990
1991 /* Allocate and initialize the request, for the num_ops ops */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001992
1993 osdc = &rbd_dev->rbd_client->client->osdc;
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02001994 osd_req = ceph_osdc_alloc_request(osdc, snapc, num_ops, false,
David Disseldorp2224d872016-04-05 11:13:39 +02001995 GFP_NOIO);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001996 if (!osd_req)
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02001997 goto fail;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001998
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001999 if (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002000 osd_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK;
Alex Elder430c28c2013-04-03 21:32:51 -05002001 else
Alex Elderbf0d5f502012-11-22 00:00:08 -06002002 osd_req->r_flags = CEPH_OSD_FLAG_READ;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002003
2004 osd_req->r_callback = rbd_osd_req_callback;
2005 osd_req->r_priv = obj_request;
2006
Yan, Zheng76271512016-02-03 21:24:49 +08002007 osd_req->r_base_oloc.pool = rbd_dev->layout.pool_id;
Ilya Dryomovd30291b2016-04-29 19:54:20 +02002008 if (ceph_oid_aprintf(&osd_req->r_base_oid, GFP_NOIO, "%s",
2009 obj_request->object_name))
2010 goto fail;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002011
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02002012 if (ceph_osdc_alloc_messages(osd_req, GFP_NOIO))
2013 goto fail;
2014
Alex Elderbf0d5f502012-11-22 00:00:08 -06002015 return osd_req;
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02002016
2017fail:
2018 ceph_osdc_put_request(osd_req);
2019 return NULL;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002020}
2021
Alex Elder0eefd472013-04-19 15:34:50 -05002022/*
Josh Durgind3246fb2014-04-07 16:49:21 -07002023 * Create a copyup osd request based on the information in the object
2024 * request supplied. A copyup request has two or three osd ops, a
2025 * copyup method call, potentially a hint op, and a write or truncate
2026 * or zero op.
Alex Elder0eefd472013-04-19 15:34:50 -05002027 */
2028static struct ceph_osd_request *
2029rbd_osd_req_create_copyup(struct rbd_obj_request *obj_request)
2030{
2031 struct rbd_img_request *img_request;
2032 struct ceph_snap_context *snapc;
2033 struct rbd_device *rbd_dev;
2034 struct ceph_osd_client *osdc;
2035 struct ceph_osd_request *osd_req;
Josh Durgind3246fb2014-04-07 16:49:21 -07002036 int num_osd_ops = 3;
Alex Elder0eefd472013-04-19 15:34:50 -05002037
2038 rbd_assert(obj_request_img_data_test(obj_request));
2039 img_request = obj_request->img_request;
2040 rbd_assert(img_request);
Josh Durgind3246fb2014-04-07 16:49:21 -07002041 rbd_assert(img_request_write_test(img_request) ||
2042 img_request_discard_test(img_request));
Alex Elder0eefd472013-04-19 15:34:50 -05002043
Josh Durgind3246fb2014-04-07 16:49:21 -07002044 if (img_request_discard_test(img_request))
2045 num_osd_ops = 2;
2046
2047 /* Allocate and initialize the request, for all the ops */
Alex Elder0eefd472013-04-19 15:34:50 -05002048
2049 snapc = img_request->snapc;
2050 rbd_dev = img_request->rbd_dev;
2051 osdc = &rbd_dev->rbd_client->client->osdc;
Josh Durgind3246fb2014-04-07 16:49:21 -07002052 osd_req = ceph_osdc_alloc_request(osdc, snapc, num_osd_ops,
David Disseldorp2224d872016-04-05 11:13:39 +02002053 false, GFP_NOIO);
Alex Elder0eefd472013-04-19 15:34:50 -05002054 if (!osd_req)
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02002055 goto fail;
Alex Elder0eefd472013-04-19 15:34:50 -05002056
2057 osd_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK;
2058 osd_req->r_callback = rbd_osd_req_callback;
2059 osd_req->r_priv = obj_request;
2060
Yan, Zheng76271512016-02-03 21:24:49 +08002061 osd_req->r_base_oloc.pool = rbd_dev->layout.pool_id;
Ilya Dryomovd30291b2016-04-29 19:54:20 +02002062 if (ceph_oid_aprintf(&osd_req->r_base_oid, GFP_NOIO, "%s",
2063 obj_request->object_name))
2064 goto fail;
Alex Elder0eefd472013-04-19 15:34:50 -05002065
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02002066 if (ceph_osdc_alloc_messages(osd_req, GFP_NOIO))
2067 goto fail;
2068
Alex Elder0eefd472013-04-19 15:34:50 -05002069 return osd_req;
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02002070
2071fail:
2072 ceph_osdc_put_request(osd_req);
2073 return NULL;
Alex Elder0eefd472013-04-19 15:34:50 -05002074}
2075
2076
Alex Elderbf0d5f502012-11-22 00:00:08 -06002077static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req)
2078{
2079 ceph_osdc_put_request(osd_req);
2080}
2081
2082/* object_name is assumed to be a non-null pointer and NUL-terminated */
2083
2084static struct rbd_obj_request *rbd_obj_request_create(const char *object_name,
2085 u64 offset, u64 length,
2086 enum obj_request_type type)
2087{
2088 struct rbd_obj_request *obj_request;
2089 size_t size;
2090 char *name;
2091
2092 rbd_assert(obj_request_type_valid(type));
2093
2094 size = strlen(object_name) + 1;
Ilya Dryomov5a60e872015-06-24 17:24:33 +03002095 name = kmalloc(size, GFP_NOIO);
Alex Elderf907ad52013-05-01 12:43:03 -05002096 if (!name)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002097 return NULL;
2098
Ilya Dryomov5a60e872015-06-24 17:24:33 +03002099 obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_NOIO);
Alex Elderf907ad52013-05-01 12:43:03 -05002100 if (!obj_request) {
2101 kfree(name);
2102 return NULL;
2103 }
2104
Alex Elderbf0d5f502012-11-22 00:00:08 -06002105 obj_request->object_name = memcpy(name, object_name, size);
2106 obj_request->offset = offset;
2107 obj_request->length = length;
Alex Elder926f9b32013-02-11 12:33:24 -06002108 obj_request->flags = 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002109 obj_request->which = BAD_WHICH;
2110 obj_request->type = type;
2111 INIT_LIST_HEAD(&obj_request->links);
Alex Elder788e2df2013-01-17 12:25:27 -06002112 init_completion(&obj_request->completion);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002113 kref_init(&obj_request->kref);
2114
Alex Elder37206ee2013-02-20 17:32:08 -06002115 dout("%s: \"%s\" %llu/%llu %d -> obj %p\n", __func__, object_name,
2116 offset, length, (int)type, obj_request);
2117
Alex Elderbf0d5f502012-11-22 00:00:08 -06002118 return obj_request;
2119}
2120
2121static void rbd_obj_request_destroy(struct kref *kref)
2122{
2123 struct rbd_obj_request *obj_request;
2124
2125 obj_request = container_of(kref, struct rbd_obj_request, kref);
2126
Alex Elder37206ee2013-02-20 17:32:08 -06002127 dout("%s: obj %p\n", __func__, obj_request);
2128
Alex Elderbf0d5f502012-11-22 00:00:08 -06002129 rbd_assert(obj_request->img_request == NULL);
2130 rbd_assert(obj_request->which == BAD_WHICH);
2131
2132 if (obj_request->osd_req)
2133 rbd_osd_req_destroy(obj_request->osd_req);
2134
2135 rbd_assert(obj_request_type_valid(obj_request->type));
2136 switch (obj_request->type) {
Alex Elder9969ebc2013-01-18 12:31:10 -06002137 case OBJ_REQUEST_NODATA:
2138 break; /* Nothing to do */
Alex Elderbf0d5f502012-11-22 00:00:08 -06002139 case OBJ_REQUEST_BIO:
2140 if (obj_request->bio_list)
2141 bio_chain_put(obj_request->bio_list);
2142 break;
Alex Elder788e2df2013-01-17 12:25:27 -06002143 case OBJ_REQUEST_PAGES:
2144 if (obj_request->pages)
2145 ceph_release_page_vector(obj_request->pages,
2146 obj_request->page_count);
2147 break;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002148 }
2149
Alex Elderf907ad52013-05-01 12:43:03 -05002150 kfree(obj_request->object_name);
Alex Elder868311b2013-05-01 12:43:03 -05002151 obj_request->object_name = NULL;
2152 kmem_cache_free(rbd_obj_request_cache, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002153}
2154
Alex Elderfb65d2282013-05-08 22:50:04 -05002155/* It's OK to call this for a device with no parent */
2156
2157static void rbd_spec_put(struct rbd_spec *spec);
2158static void rbd_dev_unparent(struct rbd_device *rbd_dev)
2159{
2160 rbd_dev_remove_parent(rbd_dev);
2161 rbd_spec_put(rbd_dev->parent_spec);
2162 rbd_dev->parent_spec = NULL;
2163 rbd_dev->parent_overlap = 0;
2164}
2165
Alex Elderbf0d5f502012-11-22 00:00:08 -06002166/*
Alex Eldera2acd002013-05-08 22:50:04 -05002167 * Parent image reference counting is used to determine when an
2168 * image's parent fields can be safely torn down--after there are no
2169 * more in-flight requests to the parent image. When the last
2170 * reference is dropped, cleaning them up is safe.
2171 */
2172static void rbd_dev_parent_put(struct rbd_device *rbd_dev)
2173{
2174 int counter;
2175
2176 if (!rbd_dev->parent_spec)
2177 return;
2178
2179 counter = atomic_dec_return_safe(&rbd_dev->parent_ref);
2180 if (counter > 0)
2181 return;
2182
2183 /* Last reference; clean up parent data structures */
2184
2185 if (!counter)
2186 rbd_dev_unparent(rbd_dev);
2187 else
Ilya Dryomov9584d502014-07-11 12:11:20 +04002188 rbd_warn(rbd_dev, "parent reference underflow");
Alex Eldera2acd002013-05-08 22:50:04 -05002189}
2190
2191/*
2192 * If an image has a non-zero parent overlap, get a reference to its
2193 * parent.
2194 *
2195 * Returns true if the rbd device has a parent with a non-zero
2196 * overlap and a reference for it was successfully taken, or
2197 * false otherwise.
2198 */
2199static bool rbd_dev_parent_get(struct rbd_device *rbd_dev)
2200{
Ilya Dryomovae43e9d2015-01-19 18:13:43 +03002201 int counter = 0;
Alex Eldera2acd002013-05-08 22:50:04 -05002202
2203 if (!rbd_dev->parent_spec)
2204 return false;
2205
Ilya Dryomovae43e9d2015-01-19 18:13:43 +03002206 down_read(&rbd_dev->header_rwsem);
2207 if (rbd_dev->parent_overlap)
2208 counter = atomic_inc_return_safe(&rbd_dev->parent_ref);
2209 up_read(&rbd_dev->header_rwsem);
Alex Eldera2acd002013-05-08 22:50:04 -05002210
2211 if (counter < 0)
Ilya Dryomov9584d502014-07-11 12:11:20 +04002212 rbd_warn(rbd_dev, "parent reference overflow");
Alex Eldera2acd002013-05-08 22:50:04 -05002213
Ilya Dryomovae43e9d2015-01-19 18:13:43 +03002214 return counter > 0;
Alex Eldera2acd002013-05-08 22:50:04 -05002215}
2216
Alex Elderbf0d5f502012-11-22 00:00:08 -06002217/*
2218 * Caller is responsible for filling in the list of object requests
2219 * that comprises the image request, and the Linux request pointer
2220 * (if there is one).
2221 */
Alex Eldercc344fa2013-02-19 12:25:56 -06002222static struct rbd_img_request *rbd_img_request_create(
2223 struct rbd_device *rbd_dev,
Alex Elderbf0d5f502012-11-22 00:00:08 -06002224 u64 offset, u64 length,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002225 enum obj_operation_type op_type,
Josh Durgin4e752f02014-04-08 11:12:11 -07002226 struct ceph_snap_context *snapc)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002227{
2228 struct rbd_img_request *img_request;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002229
Ilya Dryomov7a716aa2014-08-05 11:25:54 +04002230 img_request = kmem_cache_alloc(rbd_img_request_cache, GFP_NOIO);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002231 if (!img_request)
2232 return NULL;
2233
Alex Elderbf0d5f502012-11-22 00:00:08 -06002234 img_request->rq = NULL;
2235 img_request->rbd_dev = rbd_dev;
2236 img_request->offset = offset;
2237 img_request->length = length;
Alex Elder0c425242013-02-08 09:55:49 -06002238 img_request->flags = 0;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002239 if (op_type == OBJ_OP_DISCARD) {
2240 img_request_discard_set(img_request);
2241 img_request->snapc = snapc;
2242 } else if (op_type == OBJ_OP_WRITE) {
Alex Elder0c425242013-02-08 09:55:49 -06002243 img_request_write_set(img_request);
Josh Durgin4e752f02014-04-08 11:12:11 -07002244 img_request->snapc = snapc;
Alex Elder0c425242013-02-08 09:55:49 -06002245 } else {
Alex Elderbf0d5f502012-11-22 00:00:08 -06002246 img_request->snap_id = rbd_dev->spec->snap_id;
Alex Elder0c425242013-02-08 09:55:49 -06002247 }
Alex Eldera2acd002013-05-08 22:50:04 -05002248 if (rbd_dev_parent_get(rbd_dev))
Alex Elderd0b2e942013-01-24 16:13:36 -06002249 img_request_layered_set(img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002250 spin_lock_init(&img_request->completion_lock);
2251 img_request->next_completion = 0;
2252 img_request->callback = NULL;
Alex Eldera5a337d2013-01-24 16:13:36 -06002253 img_request->result = 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002254 img_request->obj_request_count = 0;
2255 INIT_LIST_HEAD(&img_request->obj_requests);
2256 kref_init(&img_request->kref);
2257
Alex Elder37206ee2013-02-20 17:32:08 -06002258 dout("%s: rbd_dev %p %s %llu/%llu -> img %p\n", __func__, rbd_dev,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002259 obj_op_name(op_type), offset, length, img_request);
Alex Elder37206ee2013-02-20 17:32:08 -06002260
Alex Elderbf0d5f502012-11-22 00:00:08 -06002261 return img_request;
2262}
2263
2264static void rbd_img_request_destroy(struct kref *kref)
2265{
2266 struct rbd_img_request *img_request;
2267 struct rbd_obj_request *obj_request;
2268 struct rbd_obj_request *next_obj_request;
2269
2270 img_request = container_of(kref, struct rbd_img_request, kref);
2271
Alex Elder37206ee2013-02-20 17:32:08 -06002272 dout("%s: img %p\n", __func__, img_request);
2273
Alex Elderbf0d5f502012-11-22 00:00:08 -06002274 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
2275 rbd_img_obj_request_del(img_request, obj_request);
Alex Elder25dcf952013-01-25 17:08:55 -06002276 rbd_assert(img_request->obj_request_count == 0);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002277
Alex Eldera2acd002013-05-08 22:50:04 -05002278 if (img_request_layered_test(img_request)) {
2279 img_request_layered_clear(img_request);
2280 rbd_dev_parent_put(img_request->rbd_dev);
2281 }
2282
Josh Durginbef95452014-04-04 17:47:52 -07002283 if (img_request_write_test(img_request) ||
2284 img_request_discard_test(img_request))
Alex Elder812164f82013-04-30 00:44:32 -05002285 ceph_put_snap_context(img_request->snapc);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002286
Alex Elder1c2a9df2013-05-01 12:43:03 -05002287 kmem_cache_free(rbd_img_request_cache, img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002288}
2289
Alex Eldere93f3152013-05-08 22:50:04 -05002290static struct rbd_img_request *rbd_parent_request_create(
2291 struct rbd_obj_request *obj_request,
2292 u64 img_offset, u64 length)
2293{
2294 struct rbd_img_request *parent_request;
2295 struct rbd_device *rbd_dev;
2296
2297 rbd_assert(obj_request->img_request);
2298 rbd_dev = obj_request->img_request->rbd_dev;
2299
Josh Durgin4e752f02014-04-08 11:12:11 -07002300 parent_request = rbd_img_request_create(rbd_dev->parent, img_offset,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002301 length, OBJ_OP_READ, NULL);
Alex Eldere93f3152013-05-08 22:50:04 -05002302 if (!parent_request)
2303 return NULL;
2304
2305 img_request_child_set(parent_request);
2306 rbd_obj_request_get(obj_request);
2307 parent_request->obj_request = obj_request;
2308
2309 return parent_request;
2310}
2311
2312static void rbd_parent_request_destroy(struct kref *kref)
2313{
2314 struct rbd_img_request *parent_request;
2315 struct rbd_obj_request *orig_request;
2316
2317 parent_request = container_of(kref, struct rbd_img_request, kref);
2318 orig_request = parent_request->obj_request;
2319
2320 parent_request->obj_request = NULL;
2321 rbd_obj_request_put(orig_request);
2322 img_request_child_clear(parent_request);
2323
2324 rbd_img_request_destroy(kref);
2325}
2326
Alex Elder12178572013-02-08 09:55:49 -06002327static bool rbd_img_obj_end_request(struct rbd_obj_request *obj_request)
2328{
Alex Elder6365d332013-02-11 12:33:24 -06002329 struct rbd_img_request *img_request;
Alex Elder12178572013-02-08 09:55:49 -06002330 unsigned int xferred;
2331 int result;
Alex Elder8b3e1a52013-01-24 16:13:36 -06002332 bool more;
Alex Elder12178572013-02-08 09:55:49 -06002333
Alex Elder6365d332013-02-11 12:33:24 -06002334 rbd_assert(obj_request_img_data_test(obj_request));
2335 img_request = obj_request->img_request;
2336
Alex Elder12178572013-02-08 09:55:49 -06002337 rbd_assert(obj_request->xferred <= (u64)UINT_MAX);
2338 xferred = (unsigned int)obj_request->xferred;
2339 result = obj_request->result;
2340 if (result) {
2341 struct rbd_device *rbd_dev = img_request->rbd_dev;
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002342 enum obj_operation_type op_type;
2343
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002344 if (img_request_discard_test(img_request))
2345 op_type = OBJ_OP_DISCARD;
2346 else if (img_request_write_test(img_request))
2347 op_type = OBJ_OP_WRITE;
2348 else
2349 op_type = OBJ_OP_READ;
Alex Elder12178572013-02-08 09:55:49 -06002350
Ilya Dryomov9584d502014-07-11 12:11:20 +04002351 rbd_warn(rbd_dev, "%s %llx at %llx (%llx)",
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002352 obj_op_name(op_type), obj_request->length,
2353 obj_request->img_offset, obj_request->offset);
Ilya Dryomov9584d502014-07-11 12:11:20 +04002354 rbd_warn(rbd_dev, " result %d xferred %x",
Alex Elder12178572013-02-08 09:55:49 -06002355 result, xferred);
2356 if (!img_request->result)
2357 img_request->result = result;
Ilya Dryomov082a75d2015-04-25 15:56:15 +03002358 /*
2359 * Need to end I/O on the entire obj_request worth of
2360 * bytes in case of error.
2361 */
2362 xferred = obj_request->length;
Alex Elder12178572013-02-08 09:55:49 -06002363 }
2364
Alex Elderf1a47392013-04-19 15:34:50 -05002365 /* Image object requests don't own their page array */
2366
2367 if (obj_request->type == OBJ_REQUEST_PAGES) {
2368 obj_request->pages = NULL;
2369 obj_request->page_count = 0;
2370 }
2371
Alex Elder8b3e1a52013-01-24 16:13:36 -06002372 if (img_request_child_test(img_request)) {
2373 rbd_assert(img_request->obj_request != NULL);
2374 more = obj_request->which < img_request->obj_request_count - 1;
2375 } else {
2376 rbd_assert(img_request->rq != NULL);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01002377
2378 more = blk_update_request(img_request->rq, result, xferred);
2379 if (!more)
2380 __blk_mq_end_request(img_request->rq, result);
Alex Elder8b3e1a52013-01-24 16:13:36 -06002381 }
2382
2383 return more;
Alex Elder12178572013-02-08 09:55:49 -06002384}
2385
Alex Elder21692382013-04-05 01:27:12 -05002386static void rbd_img_obj_callback(struct rbd_obj_request *obj_request)
2387{
2388 struct rbd_img_request *img_request;
2389 u32 which = obj_request->which;
2390 bool more = true;
2391
Alex Elder6365d332013-02-11 12:33:24 -06002392 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elder21692382013-04-05 01:27:12 -05002393 img_request = obj_request->img_request;
2394
2395 dout("%s: img %p obj %p\n", __func__, img_request, obj_request);
2396 rbd_assert(img_request != NULL);
Alex Elder21692382013-04-05 01:27:12 -05002397 rbd_assert(img_request->obj_request_count > 0);
2398 rbd_assert(which != BAD_WHICH);
2399 rbd_assert(which < img_request->obj_request_count);
Alex Elder21692382013-04-05 01:27:12 -05002400
2401 spin_lock_irq(&img_request->completion_lock);
2402 if (which != img_request->next_completion)
2403 goto out;
2404
2405 for_each_obj_request_from(img_request, obj_request) {
Alex Elder21692382013-04-05 01:27:12 -05002406 rbd_assert(more);
2407 rbd_assert(which < img_request->obj_request_count);
2408
2409 if (!obj_request_done_test(obj_request))
2410 break;
Alex Elder12178572013-02-08 09:55:49 -06002411 more = rbd_img_obj_end_request(obj_request);
Alex Elder21692382013-04-05 01:27:12 -05002412 which++;
2413 }
2414
2415 rbd_assert(more ^ (which == img_request->obj_request_count));
2416 img_request->next_completion = which;
2417out:
2418 spin_unlock_irq(&img_request->completion_lock);
Alex Elder0f2d5be2014-04-26 14:21:44 +04002419 rbd_img_request_put(img_request);
Alex Elder21692382013-04-05 01:27:12 -05002420
2421 if (!more)
2422 rbd_img_request_complete(img_request);
2423}
2424
Alex Elderf1a47392013-04-19 15:34:50 -05002425/*
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002426 * Add individual osd ops to the given ceph_osd_request and prepare
2427 * them for submission. num_ops is the current number of
2428 * osd operations already to the object request.
2429 */
2430static void rbd_img_obj_request_fill(struct rbd_obj_request *obj_request,
2431 struct ceph_osd_request *osd_request,
2432 enum obj_operation_type op_type,
2433 unsigned int num_ops)
2434{
2435 struct rbd_img_request *img_request = obj_request->img_request;
2436 struct rbd_device *rbd_dev = img_request->rbd_dev;
2437 u64 object_size = rbd_obj_bytes(&rbd_dev->header);
2438 u64 offset = obj_request->offset;
2439 u64 length = obj_request->length;
2440 u64 img_end;
2441 u16 opcode;
2442
2443 if (op_type == OBJ_OP_DISCARD) {
Josh Durgind3246fb2014-04-07 16:49:21 -07002444 if (!offset && length == object_size &&
2445 (!img_request_layered_test(img_request) ||
2446 !obj_request_overlaps_parent(obj_request))) {
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002447 opcode = CEPH_OSD_OP_DELETE;
2448 } else if ((offset + length == object_size)) {
2449 opcode = CEPH_OSD_OP_TRUNCATE;
2450 } else {
2451 down_read(&rbd_dev->header_rwsem);
2452 img_end = rbd_dev->header.image_size;
2453 up_read(&rbd_dev->header_rwsem);
2454
2455 if (obj_request->img_offset + length == img_end)
2456 opcode = CEPH_OSD_OP_TRUNCATE;
2457 else
2458 opcode = CEPH_OSD_OP_ZERO;
2459 }
2460 } else if (op_type == OBJ_OP_WRITE) {
Ilya Dryomove30b7572015-10-07 17:27:17 +02002461 if (!offset && length == object_size)
2462 opcode = CEPH_OSD_OP_WRITEFULL;
2463 else
2464 opcode = CEPH_OSD_OP_WRITE;
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002465 osd_req_op_alloc_hint_init(osd_request, num_ops,
2466 object_size, object_size);
2467 num_ops++;
2468 } else {
2469 opcode = CEPH_OSD_OP_READ;
2470 }
2471
Ilya Dryomov7e868b62014-11-21 22:16:43 +03002472 if (opcode == CEPH_OSD_OP_DELETE)
Yan, Zheng144cba12015-04-27 11:09:54 +08002473 osd_req_op_init(osd_request, num_ops, opcode, 0);
Ilya Dryomov7e868b62014-11-21 22:16:43 +03002474 else
2475 osd_req_op_extent_init(osd_request, num_ops, opcode,
2476 offset, length, 0, 0);
2477
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002478 if (obj_request->type == OBJ_REQUEST_BIO)
2479 osd_req_op_extent_osd_data_bio(osd_request, num_ops,
2480 obj_request->bio_list, length);
2481 else if (obj_request->type == OBJ_REQUEST_PAGES)
2482 osd_req_op_extent_osd_data_pages(osd_request, num_ops,
2483 obj_request->pages, length,
2484 offset & ~PAGE_MASK, false, false);
2485
2486 /* Discards are also writes */
2487 if (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD)
2488 rbd_osd_req_format_write(obj_request);
2489 else
2490 rbd_osd_req_format_read(obj_request);
2491}
2492
2493/*
Alex Elderf1a47392013-04-19 15:34:50 -05002494 * Split up an image request into one or more object requests, each
2495 * to a different object. The "type" parameter indicates whether
2496 * "data_desc" is the pointer to the head of a list of bio
2497 * structures, or the base of a page array. In either case this
2498 * function assumes data_desc describes memory sufficient to hold
2499 * all data described by the image request.
2500 */
2501static int rbd_img_request_fill(struct rbd_img_request *img_request,
2502 enum obj_request_type type,
2503 void *data_desc)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002504{
2505 struct rbd_device *rbd_dev = img_request->rbd_dev;
2506 struct rbd_obj_request *obj_request = NULL;
2507 struct rbd_obj_request *next_obj_request;
Jingoo Hana1580732013-08-09 13:04:35 +09002508 struct bio *bio_list = NULL;
Alex Elderf1a47392013-04-19 15:34:50 -05002509 unsigned int bio_offset = 0;
Jingoo Hana1580732013-08-09 13:04:35 +09002510 struct page **pages = NULL;
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002511 enum obj_operation_type op_type;
Alex Elder7da22d22013-01-24 16:13:36 -06002512 u64 img_offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002513 u64 resid;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002514
Alex Elderf1a47392013-04-19 15:34:50 -05002515 dout("%s: img %p type %d data_desc %p\n", __func__, img_request,
2516 (int)type, data_desc);
Alex Elder37206ee2013-02-20 17:32:08 -06002517
Alex Elder7da22d22013-01-24 16:13:36 -06002518 img_offset = img_request->offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002519 resid = img_request->length;
Alex Elder4dda41d2013-02-20 21:59:33 -06002520 rbd_assert(resid > 0);
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002521 op_type = rbd_img_request_op_type(img_request);
Alex Elderf1a47392013-04-19 15:34:50 -05002522
2523 if (type == OBJ_REQUEST_BIO) {
2524 bio_list = data_desc;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002525 rbd_assert(img_offset ==
2526 bio_list->bi_iter.bi_sector << SECTOR_SHIFT);
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002527 } else if (type == OBJ_REQUEST_PAGES) {
Alex Elderf1a47392013-04-19 15:34:50 -05002528 pages = data_desc;
2529 }
2530
Alex Elderbf0d5f502012-11-22 00:00:08 -06002531 while (resid) {
Alex Elder2fa12322013-04-05 01:27:12 -05002532 struct ceph_osd_request *osd_req;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002533 const char *object_name;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002534 u64 offset;
2535 u64 length;
2536
Alex Elder7da22d22013-01-24 16:13:36 -06002537 object_name = rbd_segment_name(rbd_dev, img_offset);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002538 if (!object_name)
2539 goto out_unwind;
Alex Elder7da22d22013-01-24 16:13:36 -06002540 offset = rbd_segment_offset(rbd_dev, img_offset);
2541 length = rbd_segment_length(rbd_dev, img_offset, resid);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002542 obj_request = rbd_obj_request_create(object_name,
Alex Elderf1a47392013-04-19 15:34:50 -05002543 offset, length, type);
Alex Elder78c2a442013-05-01 12:43:04 -05002544 /* object request has its own copy of the object name */
2545 rbd_segment_name_free(object_name);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002546 if (!obj_request)
2547 goto out_unwind;
Ilya Dryomov62054da2014-03-04 11:57:17 +02002548
Josh Durgin03507db2013-08-27 14:45:46 -07002549 /*
2550 * set obj_request->img_request before creating the
2551 * osd_request so that it gets the right snapc
2552 */
2553 rbd_img_obj_request_add(img_request, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002554
Alex Elderf1a47392013-04-19 15:34:50 -05002555 if (type == OBJ_REQUEST_BIO) {
2556 unsigned int clone_size;
2557
2558 rbd_assert(length <= (u64)UINT_MAX);
2559 clone_size = (unsigned int)length;
2560 obj_request->bio_list =
2561 bio_chain_clone_range(&bio_list,
2562 &bio_offset,
2563 clone_size,
David Disseldorp2224d872016-04-05 11:13:39 +02002564 GFP_NOIO);
Alex Elderf1a47392013-04-19 15:34:50 -05002565 if (!obj_request->bio_list)
Ilya Dryomov62054da2014-03-04 11:57:17 +02002566 goto out_unwind;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002567 } else if (type == OBJ_REQUEST_PAGES) {
Alex Elderf1a47392013-04-19 15:34:50 -05002568 unsigned int page_count;
2569
2570 obj_request->pages = pages;
2571 page_count = (u32)calc_pages_for(offset, length);
2572 obj_request->page_count = page_count;
2573 if ((offset + length) & ~PAGE_MASK)
2574 page_count--; /* more on last page */
2575 pages += page_count;
2576 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06002577
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002578 osd_req = rbd_osd_req_create(rbd_dev, op_type,
2579 (op_type == OBJ_OP_WRITE) ? 2 : 1,
2580 obj_request);
Alex Elder2fa12322013-04-05 01:27:12 -05002581 if (!osd_req)
Ilya Dryomov62054da2014-03-04 11:57:17 +02002582 goto out_unwind;
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002583
Alex Elder2fa12322013-04-05 01:27:12 -05002584 obj_request->osd_req = osd_req;
Alex Elder21692382013-04-05 01:27:12 -05002585 obj_request->callback = rbd_img_obj_callback;
Alex Elder7da22d22013-01-24 16:13:36 -06002586 obj_request->img_offset = img_offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002587
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002588 rbd_img_obj_request_fill(obj_request, osd_req, op_type, 0);
2589
2590 rbd_img_request_get(img_request);
2591
Alex Elder7da22d22013-01-24 16:13:36 -06002592 img_offset += length;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002593 resid -= length;
2594 }
2595
2596 return 0;
2597
Alex Elderbf0d5f502012-11-22 00:00:08 -06002598out_unwind:
2599 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
Ilya Dryomov42dd0372014-03-04 11:57:17 +02002600 rbd_img_obj_request_del(img_request, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002601
2602 return -ENOMEM;
2603}
2604
Alex Elder3d7efd12013-04-19 15:34:50 -05002605static void
Ilya Dryomov27617132015-07-16 17:36:11 +03002606rbd_osd_copyup_callback(struct rbd_obj_request *obj_request)
Alex Elder0eefd472013-04-19 15:34:50 -05002607{
2608 struct rbd_img_request *img_request;
2609 struct rbd_device *rbd_dev;
Alex Elderebda6402013-05-10 16:29:22 -05002610 struct page **pages;
Alex Elder0eefd472013-04-19 15:34:50 -05002611 u32 page_count;
2612
Ilya Dryomov27617132015-07-16 17:36:11 +03002613 dout("%s: obj %p\n", __func__, obj_request);
2614
Josh Durgind3246fb2014-04-07 16:49:21 -07002615 rbd_assert(obj_request->type == OBJ_REQUEST_BIO ||
2616 obj_request->type == OBJ_REQUEST_NODATA);
Alex Elder0eefd472013-04-19 15:34:50 -05002617 rbd_assert(obj_request_img_data_test(obj_request));
2618 img_request = obj_request->img_request;
2619 rbd_assert(img_request);
2620
2621 rbd_dev = img_request->rbd_dev;
2622 rbd_assert(rbd_dev);
Alex Elder0eefd472013-04-19 15:34:50 -05002623
Alex Elderebda6402013-05-10 16:29:22 -05002624 pages = obj_request->copyup_pages;
2625 rbd_assert(pages != NULL);
Alex Elder0eefd472013-04-19 15:34:50 -05002626 obj_request->copyup_pages = NULL;
Alex Elderebda6402013-05-10 16:29:22 -05002627 page_count = obj_request->copyup_page_count;
2628 rbd_assert(page_count);
2629 obj_request->copyup_page_count = 0;
2630 ceph_release_page_vector(pages, page_count);
Alex Elder0eefd472013-04-19 15:34:50 -05002631
2632 /*
2633 * We want the transfer count to reflect the size of the
2634 * original write request. There is no such thing as a
2635 * successful short write, so if the request was successful
2636 * we can just set it to the originally-requested length.
2637 */
2638 if (!obj_request->result)
2639 obj_request->xferred = obj_request->length;
2640
Ilya Dryomov27617132015-07-16 17:36:11 +03002641 obj_request_done_set(obj_request);
Alex Elder0eefd472013-04-19 15:34:50 -05002642}
2643
2644static void
Alex Elder3d7efd12013-04-19 15:34:50 -05002645rbd_img_obj_parent_read_full_callback(struct rbd_img_request *img_request)
2646{
2647 struct rbd_obj_request *orig_request;
Alex Elder0eefd472013-04-19 15:34:50 -05002648 struct ceph_osd_request *osd_req;
2649 struct ceph_osd_client *osdc;
2650 struct rbd_device *rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002651 struct page **pages;
Josh Durgind3246fb2014-04-07 16:49:21 -07002652 enum obj_operation_type op_type;
Alex Elderebda6402013-05-10 16:29:22 -05002653 u32 page_count;
Alex Elderbbea1c12013-05-06 17:40:33 -05002654 int img_result;
Alex Elderebda6402013-05-10 16:29:22 -05002655 u64 parent_length;
Alex Elder3d7efd12013-04-19 15:34:50 -05002656
2657 rbd_assert(img_request_child_test(img_request));
2658
2659 /* First get what we need from the image request */
2660
2661 pages = img_request->copyup_pages;
2662 rbd_assert(pages != NULL);
2663 img_request->copyup_pages = NULL;
Alex Elderebda6402013-05-10 16:29:22 -05002664 page_count = img_request->copyup_page_count;
2665 rbd_assert(page_count);
2666 img_request->copyup_page_count = 0;
Alex Elder3d7efd12013-04-19 15:34:50 -05002667
2668 orig_request = img_request->obj_request;
2669 rbd_assert(orig_request != NULL);
Alex Elderb91f09f2013-05-10 16:29:22 -05002670 rbd_assert(obj_request_type_valid(orig_request->type));
Alex Elderbbea1c12013-05-06 17:40:33 -05002671 img_result = img_request->result;
Alex Elderebda6402013-05-10 16:29:22 -05002672 parent_length = img_request->length;
2673 rbd_assert(parent_length == img_request->xferred);
Alex Elder3d7efd12013-04-19 15:34:50 -05002674 rbd_img_request_put(img_request);
2675
Alex Elder91c6feb2013-05-06 17:40:32 -05002676 rbd_assert(orig_request->img_request);
2677 rbd_dev = orig_request->img_request->rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002678 rbd_assert(rbd_dev);
Alex Elder3d7efd12013-04-19 15:34:50 -05002679
Alex Elderbbea1c12013-05-06 17:40:33 -05002680 /*
2681 * If the overlap has become 0 (most likely because the
2682 * image has been flattened) we need to free the pages
2683 * and re-submit the original write request.
2684 */
2685 if (!rbd_dev->parent_overlap) {
2686 struct ceph_osd_client *osdc;
2687
2688 ceph_release_page_vector(pages, page_count);
2689 osdc = &rbd_dev->rbd_client->client->osdc;
2690 img_result = rbd_obj_request_submit(osdc, orig_request);
2691 if (!img_result)
2692 return;
2693 }
2694
2695 if (img_result)
Alex Elder0eefd472013-04-19 15:34:50 -05002696 goto out_err;
Alex Elder3d7efd12013-04-19 15:34:50 -05002697
Alex Elder8785b1d2013-05-09 10:08:49 -05002698 /*
2699 * The original osd request is of no use to use any more.
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02002700 * We need a new one that can hold the three ops in a copyup
Alex Elder8785b1d2013-05-09 10:08:49 -05002701 * request. Allocate the new copyup osd request for the
2702 * original request, and release the old one.
2703 */
Alex Elderbbea1c12013-05-06 17:40:33 -05002704 img_result = -ENOMEM;
Alex Elder0eefd472013-04-19 15:34:50 -05002705 osd_req = rbd_osd_req_create_copyup(orig_request);
2706 if (!osd_req)
2707 goto out_err;
Alex Elder8785b1d2013-05-09 10:08:49 -05002708 rbd_osd_req_destroy(orig_request->osd_req);
Alex Elder0eefd472013-04-19 15:34:50 -05002709 orig_request->osd_req = osd_req;
2710 orig_request->copyup_pages = pages;
Alex Elderebda6402013-05-10 16:29:22 -05002711 orig_request->copyup_page_count = page_count;
Alex Elder3d7efd12013-04-19 15:34:50 -05002712
Alex Elder0eefd472013-04-19 15:34:50 -05002713 /* Initialize the copyup op */
2714
2715 osd_req_op_cls_init(osd_req, 0, CEPH_OSD_OP_CALL, "rbd", "copyup");
Alex Elderebda6402013-05-10 16:29:22 -05002716 osd_req_op_cls_request_data_pages(osd_req, 0, pages, parent_length, 0,
Alex Elder0eefd472013-04-19 15:34:50 -05002717 false, false);
2718
Josh Durgind3246fb2014-04-07 16:49:21 -07002719 /* Add the other op(s) */
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02002720
Josh Durgind3246fb2014-04-07 16:49:21 -07002721 op_type = rbd_img_request_op_type(orig_request->img_request);
2722 rbd_img_obj_request_fill(orig_request, osd_req, op_type, 1);
Alex Elder0eefd472013-04-19 15:34:50 -05002723
2724 /* All set, send it off. */
2725
Alex Elder0eefd472013-04-19 15:34:50 -05002726 osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elderbbea1c12013-05-06 17:40:33 -05002727 img_result = rbd_obj_request_submit(osdc, orig_request);
2728 if (!img_result)
Alex Elder0eefd472013-04-19 15:34:50 -05002729 return;
2730out_err:
2731 /* Record the error code and complete the request */
2732
Alex Elderbbea1c12013-05-06 17:40:33 -05002733 orig_request->result = img_result;
Alex Elder0eefd472013-04-19 15:34:50 -05002734 orig_request->xferred = 0;
2735 obj_request_done_set(orig_request);
2736 rbd_obj_request_complete(orig_request);
Alex Elder3d7efd12013-04-19 15:34:50 -05002737}
2738
2739/*
2740 * Read from the parent image the range of data that covers the
2741 * entire target of the given object request. This is used for
2742 * satisfying a layered image write request when the target of an
2743 * object request from the image request does not exist.
2744 *
2745 * A page array big enough to hold the returned data is allocated
2746 * and supplied to rbd_img_request_fill() as the "data descriptor."
2747 * When the read completes, this page array will be transferred to
2748 * the original object request for the copyup operation.
2749 *
2750 * If an error occurs, record it as the result of the original
2751 * object request and mark it done so it gets completed.
2752 */
2753static int rbd_img_obj_parent_read_full(struct rbd_obj_request *obj_request)
2754{
2755 struct rbd_img_request *img_request = NULL;
2756 struct rbd_img_request *parent_request = NULL;
2757 struct rbd_device *rbd_dev;
2758 u64 img_offset;
2759 u64 length;
2760 struct page **pages = NULL;
2761 u32 page_count;
2762 int result;
2763
2764 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elderb91f09f2013-05-10 16:29:22 -05002765 rbd_assert(obj_request_type_valid(obj_request->type));
Alex Elder3d7efd12013-04-19 15:34:50 -05002766
2767 img_request = obj_request->img_request;
2768 rbd_assert(img_request != NULL);
2769 rbd_dev = img_request->rbd_dev;
2770 rbd_assert(rbd_dev->parent != NULL);
2771
2772 /*
2773 * Determine the byte range covered by the object in the
2774 * child image to which the original request was to be sent.
2775 */
2776 img_offset = obj_request->img_offset - obj_request->offset;
2777 length = (u64)1 << rbd_dev->header.obj_order;
2778
2779 /*
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002780 * There is no defined parent data beyond the parent
2781 * overlap, so limit what we read at that boundary if
2782 * necessary.
2783 */
2784 if (img_offset + length > rbd_dev->parent_overlap) {
2785 rbd_assert(img_offset < rbd_dev->parent_overlap);
2786 length = rbd_dev->parent_overlap - img_offset;
2787 }
2788
2789 /*
Alex Elder3d7efd12013-04-19 15:34:50 -05002790 * Allocate a page array big enough to receive the data read
2791 * from the parent.
2792 */
2793 page_count = (u32)calc_pages_for(0, length);
2794 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2795 if (IS_ERR(pages)) {
2796 result = PTR_ERR(pages);
2797 pages = NULL;
2798 goto out_err;
2799 }
2800
2801 result = -ENOMEM;
Alex Eldere93f3152013-05-08 22:50:04 -05002802 parent_request = rbd_parent_request_create(obj_request,
2803 img_offset, length);
Alex Elder3d7efd12013-04-19 15:34:50 -05002804 if (!parent_request)
2805 goto out_err;
Alex Elder3d7efd12013-04-19 15:34:50 -05002806
2807 result = rbd_img_request_fill(parent_request, OBJ_REQUEST_PAGES, pages);
2808 if (result)
2809 goto out_err;
2810 parent_request->copyup_pages = pages;
Alex Elderebda6402013-05-10 16:29:22 -05002811 parent_request->copyup_page_count = page_count;
Alex Elder3d7efd12013-04-19 15:34:50 -05002812
2813 parent_request->callback = rbd_img_obj_parent_read_full_callback;
2814 result = rbd_img_request_submit(parent_request);
2815 if (!result)
2816 return 0;
2817
2818 parent_request->copyup_pages = NULL;
Alex Elderebda6402013-05-10 16:29:22 -05002819 parent_request->copyup_page_count = 0;
Alex Elder3d7efd12013-04-19 15:34:50 -05002820 parent_request->obj_request = NULL;
2821 rbd_obj_request_put(obj_request);
2822out_err:
2823 if (pages)
2824 ceph_release_page_vector(pages, page_count);
2825 if (parent_request)
2826 rbd_img_request_put(parent_request);
2827 obj_request->result = result;
2828 obj_request->xferred = 0;
2829 obj_request_done_set(obj_request);
2830
2831 return result;
2832}
2833
Alex Elderc5b5ef62013-02-11 12:33:24 -06002834static void rbd_img_obj_exists_callback(struct rbd_obj_request *obj_request)
2835{
Alex Elderc5b5ef62013-02-11 12:33:24 -06002836 struct rbd_obj_request *orig_request;
Alex Elder638f5ab2013-05-06 17:40:33 -05002837 struct rbd_device *rbd_dev;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002838 int result;
2839
2840 rbd_assert(!obj_request_img_data_test(obj_request));
2841
2842 /*
2843 * All we need from the object request is the original
2844 * request and the result of the STAT op. Grab those, then
2845 * we're done with the request.
2846 */
2847 orig_request = obj_request->obj_request;
2848 obj_request->obj_request = NULL;
Alex Elder912c3172013-05-13 20:35:38 -05002849 rbd_obj_request_put(orig_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002850 rbd_assert(orig_request);
2851 rbd_assert(orig_request->img_request);
2852
2853 result = obj_request->result;
2854 obj_request->result = 0;
2855
2856 dout("%s: obj %p for obj %p result %d %llu/%llu\n", __func__,
2857 obj_request, orig_request, result,
2858 obj_request->xferred, obj_request->length);
2859 rbd_obj_request_put(obj_request);
2860
Alex Elder638f5ab2013-05-06 17:40:33 -05002861 /*
2862 * If the overlap has become 0 (most likely because the
2863 * image has been flattened) we need to free the pages
2864 * and re-submit the original write request.
2865 */
2866 rbd_dev = orig_request->img_request->rbd_dev;
2867 if (!rbd_dev->parent_overlap) {
2868 struct ceph_osd_client *osdc;
2869
Alex Elder638f5ab2013-05-06 17:40:33 -05002870 osdc = &rbd_dev->rbd_client->client->osdc;
2871 result = rbd_obj_request_submit(osdc, orig_request);
2872 if (!result)
2873 return;
2874 }
Alex Elderc5b5ef62013-02-11 12:33:24 -06002875
2876 /*
2877 * Our only purpose here is to determine whether the object
2878 * exists, and we don't want to treat the non-existence as
2879 * an error. If something else comes back, transfer the
2880 * error to the original request and complete it now.
2881 */
2882 if (!result) {
2883 obj_request_existence_set(orig_request, true);
2884 } else if (result == -ENOENT) {
2885 obj_request_existence_set(orig_request, false);
2886 } else if (result) {
2887 orig_request->result = result;
Alex Elder3d7efd12013-04-19 15:34:50 -05002888 goto out;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002889 }
2890
2891 /*
2892 * Resubmit the original request now that we have recorded
2893 * whether the target object exists.
2894 */
Alex Elderb454e362013-04-19 15:34:50 -05002895 orig_request->result = rbd_img_obj_request_submit(orig_request);
Alex Elder3d7efd12013-04-19 15:34:50 -05002896out:
Alex Elderc5b5ef62013-02-11 12:33:24 -06002897 if (orig_request->result)
2898 rbd_obj_request_complete(orig_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002899}
2900
2901static int rbd_img_obj_exists_submit(struct rbd_obj_request *obj_request)
2902{
2903 struct rbd_obj_request *stat_request;
2904 struct rbd_device *rbd_dev;
2905 struct ceph_osd_client *osdc;
2906 struct page **pages = NULL;
2907 u32 page_count;
2908 size_t size;
2909 int ret;
2910
2911 /*
2912 * The response data for a STAT call consists of:
2913 * le64 length;
2914 * struct {
2915 * le32 tv_sec;
2916 * le32 tv_nsec;
2917 * } mtime;
2918 */
2919 size = sizeof (__le64) + sizeof (__le32) + sizeof (__le32);
2920 page_count = (u32)calc_pages_for(0, size);
2921 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2922 if (IS_ERR(pages))
2923 return PTR_ERR(pages);
2924
2925 ret = -ENOMEM;
2926 stat_request = rbd_obj_request_create(obj_request->object_name, 0, 0,
2927 OBJ_REQUEST_PAGES);
2928 if (!stat_request)
2929 goto out;
2930
2931 rbd_obj_request_get(obj_request);
2932 stat_request->obj_request = obj_request;
2933 stat_request->pages = pages;
2934 stat_request->page_count = page_count;
2935
2936 rbd_assert(obj_request->img_request);
2937 rbd_dev = obj_request->img_request->rbd_dev;
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002938 stat_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_READ, 1,
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02002939 stat_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002940 if (!stat_request->osd_req)
2941 goto out;
2942 stat_request->callback = rbd_img_obj_exists_callback;
2943
Yan, Zheng144cba12015-04-27 11:09:54 +08002944 osd_req_op_init(stat_request->osd_req, 0, CEPH_OSD_OP_STAT, 0);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002945 osd_req_op_raw_data_in_pages(stat_request->osd_req, 0, pages, size, 0,
2946 false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05002947 rbd_osd_req_format_read(stat_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002948
2949 osdc = &rbd_dev->rbd_client->client->osdc;
2950 ret = rbd_obj_request_submit(osdc, stat_request);
2951out:
2952 if (ret)
2953 rbd_obj_request_put(obj_request);
2954
2955 return ret;
2956}
2957
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002958static bool img_obj_request_simple(struct rbd_obj_request *obj_request)
Alex Elderb454e362013-04-19 15:34:50 -05002959{
2960 struct rbd_img_request *img_request;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002961 struct rbd_device *rbd_dev;
Alex Elderb454e362013-04-19 15:34:50 -05002962
2963 rbd_assert(obj_request_img_data_test(obj_request));
2964
2965 img_request = obj_request->img_request;
2966 rbd_assert(img_request);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002967 rbd_dev = img_request->rbd_dev;
Alex Elderb454e362013-04-19 15:34:50 -05002968
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002969 /* Reads */
Josh Durgin1c220882014-04-04 17:49:12 -07002970 if (!img_request_write_test(img_request) &&
2971 !img_request_discard_test(img_request))
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002972 return true;
Alex Elderb454e362013-04-19 15:34:50 -05002973
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002974 /* Non-layered writes */
2975 if (!img_request_layered_test(img_request))
2976 return true;
2977
2978 /*
2979 * Layered writes outside of the parent overlap range don't
2980 * share any data with the parent.
2981 */
2982 if (!obj_request_overlaps_parent(obj_request))
2983 return true;
2984
2985 /*
Guangliang Zhaoc622d222014-04-01 22:22:15 +08002986 * Entire-object layered writes - we will overwrite whatever
2987 * parent data there is anyway.
2988 */
2989 if (!obj_request->offset &&
2990 obj_request->length == rbd_obj_bytes(&rbd_dev->header))
2991 return true;
2992
2993 /*
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002994 * If the object is known to already exist, its parent data has
2995 * already been copied.
2996 */
2997 if (obj_request_known_test(obj_request) &&
2998 obj_request_exists_test(obj_request))
2999 return true;
3000
3001 return false;
3002}
3003
3004static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request)
3005{
3006 if (img_obj_request_simple(obj_request)) {
Alex Elderb454e362013-04-19 15:34:50 -05003007 struct rbd_device *rbd_dev;
3008 struct ceph_osd_client *osdc;
3009
3010 rbd_dev = obj_request->img_request->rbd_dev;
3011 osdc = &rbd_dev->rbd_client->client->osdc;
3012
3013 return rbd_obj_request_submit(osdc, obj_request);
3014 }
3015
3016 /*
Alex Elder3d7efd12013-04-19 15:34:50 -05003017 * It's a layered write. The target object might exist but
3018 * we may not know that yet. If we know it doesn't exist,
3019 * start by reading the data for the full target object from
3020 * the parent so we can use it for a copyup to the target.
Alex Elderb454e362013-04-19 15:34:50 -05003021 */
Ilya Dryomov70d045f2014-09-12 16:02:01 +04003022 if (obj_request_known_test(obj_request))
Alex Elder3d7efd12013-04-19 15:34:50 -05003023 return rbd_img_obj_parent_read_full(obj_request);
3024
3025 /* We don't know whether the target exists. Go find out. */
Alex Elderb454e362013-04-19 15:34:50 -05003026
3027 return rbd_img_obj_exists_submit(obj_request);
3028}
3029
Alex Elderbf0d5f502012-11-22 00:00:08 -06003030static int rbd_img_request_submit(struct rbd_img_request *img_request)
3031{
Alex Elderbf0d5f502012-11-22 00:00:08 -06003032 struct rbd_obj_request *obj_request;
Alex Elder46faeed2013-04-10 17:47:46 -05003033 struct rbd_obj_request *next_obj_request;
Ilya Dryomov663ae2c2016-05-16 13:18:57 +02003034 int ret = 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06003035
Alex Elder37206ee2013-02-20 17:32:08 -06003036 dout("%s: img %p\n", __func__, img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06003037
Ilya Dryomov663ae2c2016-05-16 13:18:57 +02003038 rbd_img_request_get(img_request);
3039 for_each_obj_request_safe(img_request, obj_request, next_obj_request) {
Alex Elderb454e362013-04-19 15:34:50 -05003040 ret = rbd_img_obj_request_submit(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06003041 if (ret)
Ilya Dryomov663ae2c2016-05-16 13:18:57 +02003042 goto out_put_ireq;
Alex Elderbf0d5f502012-11-22 00:00:08 -06003043 }
3044
Ilya Dryomov663ae2c2016-05-16 13:18:57 +02003045out_put_ireq:
3046 rbd_img_request_put(img_request);
3047 return ret;
Alex Elderbf0d5f502012-11-22 00:00:08 -06003048}
3049
Alex Elder8b3e1a52013-01-24 16:13:36 -06003050static void rbd_img_parent_read_callback(struct rbd_img_request *img_request)
3051{
3052 struct rbd_obj_request *obj_request;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05003053 struct rbd_device *rbd_dev;
3054 u64 obj_end;
Alex Elder02c74fb2013-05-06 17:40:33 -05003055 u64 img_xferred;
3056 int img_result;
Alex Elder8b3e1a52013-01-24 16:13:36 -06003057
3058 rbd_assert(img_request_child_test(img_request));
3059
Alex Elder02c74fb2013-05-06 17:40:33 -05003060 /* First get what we need from the image request and release it */
3061
Alex Elder8b3e1a52013-01-24 16:13:36 -06003062 obj_request = img_request->obj_request;
Alex Elder02c74fb2013-05-06 17:40:33 -05003063 img_xferred = img_request->xferred;
3064 img_result = img_request->result;
3065 rbd_img_request_put(img_request);
3066
3067 /*
3068 * If the overlap has become 0 (most likely because the
3069 * image has been flattened) we need to re-submit the
3070 * original request.
3071 */
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05003072 rbd_assert(obj_request);
3073 rbd_assert(obj_request->img_request);
Alex Elder02c74fb2013-05-06 17:40:33 -05003074 rbd_dev = obj_request->img_request->rbd_dev;
3075 if (!rbd_dev->parent_overlap) {
3076 struct ceph_osd_client *osdc;
Alex Elder8b3e1a52013-01-24 16:13:36 -06003077
Alex Elder02c74fb2013-05-06 17:40:33 -05003078 osdc = &rbd_dev->rbd_client->client->osdc;
3079 img_result = rbd_obj_request_submit(osdc, obj_request);
3080 if (!img_result)
3081 return;
3082 }
3083
3084 obj_request->result = img_result;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05003085 if (obj_request->result)
3086 goto out;
3087
3088 /*
3089 * We need to zero anything beyond the parent overlap
3090 * boundary. Since rbd_img_obj_request_read_callback()
3091 * will zero anything beyond the end of a short read, an
3092 * easy way to do this is to pretend the data from the
3093 * parent came up short--ending at the overlap boundary.
3094 */
3095 rbd_assert(obj_request->img_offset < U64_MAX - obj_request->length);
3096 obj_end = obj_request->img_offset + obj_request->length;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05003097 if (obj_end > rbd_dev->parent_overlap) {
3098 u64 xferred = 0;
3099
3100 if (obj_request->img_offset < rbd_dev->parent_overlap)
3101 xferred = rbd_dev->parent_overlap -
3102 obj_request->img_offset;
3103
Alex Elder02c74fb2013-05-06 17:40:33 -05003104 obj_request->xferred = min(img_xferred, xferred);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05003105 } else {
Alex Elder02c74fb2013-05-06 17:40:33 -05003106 obj_request->xferred = img_xferred;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05003107 }
3108out:
Alex Elder8b3e1a52013-01-24 16:13:36 -06003109 rbd_img_obj_request_read_callback(obj_request);
3110 rbd_obj_request_complete(obj_request);
3111}
3112
3113static void rbd_img_parent_read(struct rbd_obj_request *obj_request)
3114{
Alex Elder8b3e1a52013-01-24 16:13:36 -06003115 struct rbd_img_request *img_request;
3116 int result;
3117
3118 rbd_assert(obj_request_img_data_test(obj_request));
3119 rbd_assert(obj_request->img_request != NULL);
3120 rbd_assert(obj_request->result == (s32) -ENOENT);
Alex Elder5b2ab722013-05-06 17:40:33 -05003121 rbd_assert(obj_request_type_valid(obj_request->type));
Alex Elder8b3e1a52013-01-24 16:13:36 -06003122
Alex Elder8b3e1a52013-01-24 16:13:36 -06003123 /* rbd_read_finish(obj_request, obj_request->length); */
Alex Eldere93f3152013-05-08 22:50:04 -05003124 img_request = rbd_parent_request_create(obj_request,
Alex Elder8b3e1a52013-01-24 16:13:36 -06003125 obj_request->img_offset,
Alex Eldere93f3152013-05-08 22:50:04 -05003126 obj_request->length);
Alex Elder8b3e1a52013-01-24 16:13:36 -06003127 result = -ENOMEM;
3128 if (!img_request)
3129 goto out_err;
3130
Alex Elder5b2ab722013-05-06 17:40:33 -05003131 if (obj_request->type == OBJ_REQUEST_BIO)
3132 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
3133 obj_request->bio_list);
3134 else
3135 result = rbd_img_request_fill(img_request, OBJ_REQUEST_PAGES,
3136 obj_request->pages);
Alex Elder8b3e1a52013-01-24 16:13:36 -06003137 if (result)
3138 goto out_err;
3139
3140 img_request->callback = rbd_img_parent_read_callback;
3141 result = rbd_img_request_submit(img_request);
3142 if (result)
3143 goto out_err;
3144
3145 return;
3146out_err:
3147 if (img_request)
3148 rbd_img_request_put(img_request);
3149 obj_request->result = result;
3150 obj_request->xferred = 0;
3151 obj_request_done_set(obj_request);
3152}
3153
Ilya Dryomoved95b212016-08-12 16:40:02 +02003154static const struct rbd_client_id rbd_empty_cid;
3155
3156static bool rbd_cid_equal(const struct rbd_client_id *lhs,
3157 const struct rbd_client_id *rhs)
3158{
3159 return lhs->gid == rhs->gid && lhs->handle == rhs->handle;
3160}
3161
3162static struct rbd_client_id rbd_get_cid(struct rbd_device *rbd_dev)
3163{
3164 struct rbd_client_id cid;
3165
3166 mutex_lock(&rbd_dev->watch_mutex);
3167 cid.gid = ceph_client_gid(rbd_dev->rbd_client->client);
3168 cid.handle = rbd_dev->watch_cookie;
3169 mutex_unlock(&rbd_dev->watch_mutex);
3170 return cid;
3171}
3172
3173/*
3174 * lock_rwsem must be held for write
3175 */
3176static void rbd_set_owner_cid(struct rbd_device *rbd_dev,
3177 const struct rbd_client_id *cid)
3178{
3179 dout("%s rbd_dev %p %llu-%llu -> %llu-%llu\n", __func__, rbd_dev,
3180 rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle,
3181 cid->gid, cid->handle);
3182 rbd_dev->owner_cid = *cid; /* struct */
3183}
3184
3185static void format_lock_cookie(struct rbd_device *rbd_dev, char *buf)
3186{
3187 mutex_lock(&rbd_dev->watch_mutex);
3188 sprintf(buf, "%s %llu", RBD_LOCK_COOKIE_PREFIX, rbd_dev->watch_cookie);
3189 mutex_unlock(&rbd_dev->watch_mutex);
3190}
3191
3192/*
3193 * lock_rwsem must be held for write
3194 */
3195static int rbd_lock(struct rbd_device *rbd_dev)
3196{
3197 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3198 struct rbd_client_id cid = rbd_get_cid(rbd_dev);
3199 char cookie[32];
3200 int ret;
3201
3202 WARN_ON(__rbd_is_lock_owner(rbd_dev));
3203
3204 format_lock_cookie(rbd_dev, cookie);
3205 ret = ceph_cls_lock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc,
3206 RBD_LOCK_NAME, CEPH_CLS_LOCK_EXCLUSIVE, cookie,
3207 RBD_LOCK_TAG, "", 0);
3208 if (ret)
3209 return ret;
3210
3211 rbd_dev->lock_state = RBD_LOCK_STATE_LOCKED;
3212 rbd_set_owner_cid(rbd_dev, &cid);
3213 queue_work(rbd_dev->task_wq, &rbd_dev->acquired_lock_work);
3214 return 0;
3215}
3216
3217/*
3218 * lock_rwsem must be held for write
3219 */
3220static int rbd_unlock(struct rbd_device *rbd_dev)
3221{
3222 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3223 char cookie[32];
3224 int ret;
3225
3226 WARN_ON(!__rbd_is_lock_owner(rbd_dev));
3227
3228 rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED;
3229
3230 format_lock_cookie(rbd_dev, cookie);
3231 ret = ceph_cls_unlock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc,
3232 RBD_LOCK_NAME, cookie);
3233 if (ret && ret != -ENOENT) {
3234 rbd_warn(rbd_dev, "cls_unlock failed: %d", ret);
3235 return ret;
3236 }
3237
3238 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
3239 queue_work(rbd_dev->task_wq, &rbd_dev->released_lock_work);
3240 return 0;
3241}
3242
3243static int __rbd_notify_op_lock(struct rbd_device *rbd_dev,
3244 enum rbd_notify_op notify_op,
3245 struct page ***preply_pages,
3246 size_t *preply_len)
3247{
3248 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3249 struct rbd_client_id cid = rbd_get_cid(rbd_dev);
3250 int buf_size = 4 + 8 + 8 + CEPH_ENCODING_START_BLK_LEN;
3251 char buf[buf_size];
3252 void *p = buf;
3253
3254 dout("%s rbd_dev %p notify_op %d\n", __func__, rbd_dev, notify_op);
3255
3256 /* encode *LockPayload NotifyMessage (op + ClientId) */
3257 ceph_start_encoding(&p, 2, 1, buf_size - CEPH_ENCODING_START_BLK_LEN);
3258 ceph_encode_32(&p, notify_op);
3259 ceph_encode_64(&p, cid.gid);
3260 ceph_encode_64(&p, cid.handle);
3261
3262 return ceph_osdc_notify(osdc, &rbd_dev->header_oid,
3263 &rbd_dev->header_oloc, buf, buf_size,
3264 RBD_NOTIFY_TIMEOUT, preply_pages, preply_len);
3265}
3266
3267static void rbd_notify_op_lock(struct rbd_device *rbd_dev,
3268 enum rbd_notify_op notify_op)
3269{
3270 struct page **reply_pages;
3271 size_t reply_len;
3272
3273 __rbd_notify_op_lock(rbd_dev, notify_op, &reply_pages, &reply_len);
3274 ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len));
3275}
3276
3277static void rbd_notify_acquired_lock(struct work_struct *work)
3278{
3279 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
3280 acquired_lock_work);
3281
3282 rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_ACQUIRED_LOCK);
3283}
3284
3285static void rbd_notify_released_lock(struct work_struct *work)
3286{
3287 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
3288 released_lock_work);
3289
3290 rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_RELEASED_LOCK);
3291}
3292
3293static int rbd_request_lock(struct rbd_device *rbd_dev)
3294{
3295 struct page **reply_pages;
3296 size_t reply_len;
3297 bool lock_owner_responded = false;
3298 int ret;
3299
3300 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3301
3302 ret = __rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_REQUEST_LOCK,
3303 &reply_pages, &reply_len);
3304 if (ret && ret != -ETIMEDOUT) {
3305 rbd_warn(rbd_dev, "failed to request lock: %d", ret);
3306 goto out;
3307 }
3308
3309 if (reply_len > 0 && reply_len <= PAGE_SIZE) {
3310 void *p = page_address(reply_pages[0]);
3311 void *const end = p + reply_len;
3312 u32 n;
3313
3314 ceph_decode_32_safe(&p, end, n, e_inval); /* num_acks */
3315 while (n--) {
3316 u8 struct_v;
3317 u32 len;
3318
3319 ceph_decode_need(&p, end, 8 + 8, e_inval);
3320 p += 8 + 8; /* skip gid and cookie */
3321
3322 ceph_decode_32_safe(&p, end, len, e_inval);
3323 if (!len)
3324 continue;
3325
3326 if (lock_owner_responded) {
3327 rbd_warn(rbd_dev,
3328 "duplicate lock owners detected");
3329 ret = -EIO;
3330 goto out;
3331 }
3332
3333 lock_owner_responded = true;
3334 ret = ceph_start_decoding(&p, end, 1, "ResponseMessage",
3335 &struct_v, &len);
3336 if (ret) {
3337 rbd_warn(rbd_dev,
3338 "failed to decode ResponseMessage: %d",
3339 ret);
3340 goto e_inval;
3341 }
3342
3343 ret = ceph_decode_32(&p);
3344 }
3345 }
3346
3347 if (!lock_owner_responded) {
3348 rbd_warn(rbd_dev, "no lock owners detected");
3349 ret = -ETIMEDOUT;
3350 }
3351
3352out:
3353 ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len));
3354 return ret;
3355
3356e_inval:
3357 ret = -EINVAL;
3358 goto out;
3359}
3360
3361static void wake_requests(struct rbd_device *rbd_dev, bool wake_all)
3362{
3363 dout("%s rbd_dev %p wake_all %d\n", __func__, rbd_dev, wake_all);
3364
3365 cancel_delayed_work(&rbd_dev->lock_dwork);
3366 if (wake_all)
3367 wake_up_all(&rbd_dev->lock_waitq);
3368 else
3369 wake_up(&rbd_dev->lock_waitq);
3370}
3371
3372static int get_lock_owner_info(struct rbd_device *rbd_dev,
3373 struct ceph_locker **lockers, u32 *num_lockers)
3374{
3375 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3376 u8 lock_type;
3377 char *lock_tag;
3378 int ret;
3379
3380 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3381
3382 ret = ceph_cls_lock_info(osdc, &rbd_dev->header_oid,
3383 &rbd_dev->header_oloc, RBD_LOCK_NAME,
3384 &lock_type, &lock_tag, lockers, num_lockers);
3385 if (ret)
3386 return ret;
3387
3388 if (*num_lockers == 0) {
3389 dout("%s rbd_dev %p no lockers detected\n", __func__, rbd_dev);
3390 goto out;
3391 }
3392
3393 if (strcmp(lock_tag, RBD_LOCK_TAG)) {
3394 rbd_warn(rbd_dev, "locked by external mechanism, tag %s",
3395 lock_tag);
3396 ret = -EBUSY;
3397 goto out;
3398 }
3399
3400 if (lock_type == CEPH_CLS_LOCK_SHARED) {
3401 rbd_warn(rbd_dev, "shared lock type detected");
3402 ret = -EBUSY;
3403 goto out;
3404 }
3405
3406 if (strncmp((*lockers)[0].id.cookie, RBD_LOCK_COOKIE_PREFIX,
3407 strlen(RBD_LOCK_COOKIE_PREFIX))) {
3408 rbd_warn(rbd_dev, "locked by external mechanism, cookie %s",
3409 (*lockers)[0].id.cookie);
3410 ret = -EBUSY;
3411 goto out;
3412 }
3413
3414out:
3415 kfree(lock_tag);
3416 return ret;
3417}
3418
3419static int find_watcher(struct rbd_device *rbd_dev,
3420 const struct ceph_locker *locker)
3421{
3422 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3423 struct ceph_watch_item *watchers;
3424 u32 num_watchers;
3425 u64 cookie;
3426 int i;
3427 int ret;
3428
3429 ret = ceph_osdc_list_watchers(osdc, &rbd_dev->header_oid,
3430 &rbd_dev->header_oloc, &watchers,
3431 &num_watchers);
3432 if (ret)
3433 return ret;
3434
3435 sscanf(locker->id.cookie, RBD_LOCK_COOKIE_PREFIX " %llu", &cookie);
3436 for (i = 0; i < num_watchers; i++) {
3437 if (!memcmp(&watchers[i].addr, &locker->info.addr,
3438 sizeof(locker->info.addr)) &&
3439 watchers[i].cookie == cookie) {
3440 struct rbd_client_id cid = {
3441 .gid = le64_to_cpu(watchers[i].name.num),
3442 .handle = cookie,
3443 };
3444
3445 dout("%s rbd_dev %p found cid %llu-%llu\n", __func__,
3446 rbd_dev, cid.gid, cid.handle);
3447 rbd_set_owner_cid(rbd_dev, &cid);
3448 ret = 1;
3449 goto out;
3450 }
3451 }
3452
3453 dout("%s rbd_dev %p no watchers\n", __func__, rbd_dev);
3454 ret = 0;
3455out:
3456 kfree(watchers);
3457 return ret;
3458}
3459
3460/*
3461 * lock_rwsem must be held for write
3462 */
3463static int rbd_try_lock(struct rbd_device *rbd_dev)
3464{
3465 struct ceph_client *client = rbd_dev->rbd_client->client;
3466 struct ceph_locker *lockers;
3467 u32 num_lockers;
3468 int ret;
3469
3470 for (;;) {
3471 ret = rbd_lock(rbd_dev);
3472 if (ret != -EBUSY)
3473 return ret;
3474
3475 /* determine if the current lock holder is still alive */
3476 ret = get_lock_owner_info(rbd_dev, &lockers, &num_lockers);
3477 if (ret)
3478 return ret;
3479
3480 if (num_lockers == 0)
3481 goto again;
3482
3483 ret = find_watcher(rbd_dev, lockers);
3484 if (ret) {
3485 if (ret > 0)
3486 ret = 0; /* have to request lock */
3487 goto out;
3488 }
3489
3490 rbd_warn(rbd_dev, "%s%llu seems dead, breaking lock",
3491 ENTITY_NAME(lockers[0].id.name));
3492
3493 ret = ceph_monc_blacklist_add(&client->monc,
3494 &lockers[0].info.addr);
3495 if (ret) {
3496 rbd_warn(rbd_dev, "blacklist of %s%llu failed: %d",
3497 ENTITY_NAME(lockers[0].id.name), ret);
3498 goto out;
3499 }
3500
3501 ret = ceph_cls_break_lock(&client->osdc, &rbd_dev->header_oid,
3502 &rbd_dev->header_oloc, RBD_LOCK_NAME,
3503 lockers[0].id.cookie,
3504 &lockers[0].id.name);
3505 if (ret && ret != -ENOENT)
3506 goto out;
3507
3508again:
3509 ceph_free_lockers(lockers, num_lockers);
3510 }
3511
3512out:
3513 ceph_free_lockers(lockers, num_lockers);
3514 return ret;
3515}
3516
3517/*
3518 * ret is set only if lock_state is RBD_LOCK_STATE_UNLOCKED
3519 */
3520static enum rbd_lock_state rbd_try_acquire_lock(struct rbd_device *rbd_dev,
3521 int *pret)
3522{
3523 enum rbd_lock_state lock_state;
3524
3525 down_read(&rbd_dev->lock_rwsem);
3526 dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev,
3527 rbd_dev->lock_state);
3528 if (__rbd_is_lock_owner(rbd_dev)) {
3529 lock_state = rbd_dev->lock_state;
3530 up_read(&rbd_dev->lock_rwsem);
3531 return lock_state;
3532 }
3533
3534 up_read(&rbd_dev->lock_rwsem);
3535 down_write(&rbd_dev->lock_rwsem);
3536 dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev,
3537 rbd_dev->lock_state);
3538 if (!__rbd_is_lock_owner(rbd_dev)) {
3539 *pret = rbd_try_lock(rbd_dev);
3540 if (*pret)
3541 rbd_warn(rbd_dev, "failed to acquire lock: %d", *pret);
3542 }
3543
3544 lock_state = rbd_dev->lock_state;
3545 up_write(&rbd_dev->lock_rwsem);
3546 return lock_state;
3547}
3548
3549static void rbd_acquire_lock(struct work_struct *work)
3550{
3551 struct rbd_device *rbd_dev = container_of(to_delayed_work(work),
3552 struct rbd_device, lock_dwork);
3553 enum rbd_lock_state lock_state;
3554 int ret;
3555
3556 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3557again:
3558 lock_state = rbd_try_acquire_lock(rbd_dev, &ret);
3559 if (lock_state != RBD_LOCK_STATE_UNLOCKED || ret == -EBLACKLISTED) {
3560 if (lock_state == RBD_LOCK_STATE_LOCKED)
3561 wake_requests(rbd_dev, true);
3562 dout("%s rbd_dev %p lock_state %d ret %d - done\n", __func__,
3563 rbd_dev, lock_state, ret);
3564 return;
3565 }
3566
3567 ret = rbd_request_lock(rbd_dev);
3568 if (ret == -ETIMEDOUT) {
3569 goto again; /* treat this as a dead client */
3570 } else if (ret < 0) {
3571 rbd_warn(rbd_dev, "error requesting lock: %d", ret);
3572 mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork,
3573 RBD_RETRY_DELAY);
3574 } else {
3575 /*
3576 * lock owner acked, but resend if we don't see them
3577 * release the lock
3578 */
3579 dout("%s rbd_dev %p requeueing lock_dwork\n", __func__,
3580 rbd_dev);
3581 mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork,
3582 msecs_to_jiffies(2 * RBD_NOTIFY_TIMEOUT * MSEC_PER_SEC));
3583 }
3584}
3585
3586/*
3587 * lock_rwsem must be held for write
3588 */
3589static bool rbd_release_lock(struct rbd_device *rbd_dev)
3590{
3591 dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev,
3592 rbd_dev->lock_state);
3593 if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED)
3594 return false;
3595
3596 rbd_dev->lock_state = RBD_LOCK_STATE_RELEASING;
3597 downgrade_write(&rbd_dev->lock_rwsem);
3598 /*
3599 * Ensure that all in-flight IO is flushed.
3600 *
3601 * FIXME: ceph_osdc_sync() flushes the entire OSD client, which
3602 * may be shared with other devices.
3603 */
3604 ceph_osdc_sync(&rbd_dev->rbd_client->client->osdc);
3605 up_read(&rbd_dev->lock_rwsem);
3606
3607 down_write(&rbd_dev->lock_rwsem);
3608 dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev,
3609 rbd_dev->lock_state);
3610 if (rbd_dev->lock_state != RBD_LOCK_STATE_RELEASING)
3611 return false;
3612
3613 if (!rbd_unlock(rbd_dev))
3614 /*
3615 * Give others a chance to grab the lock - we would re-acquire
3616 * almost immediately if we got new IO during ceph_osdc_sync()
3617 * otherwise. We need to ack our own notifications, so this
3618 * lock_dwork will be requeued from rbd_wait_state_locked()
3619 * after wake_requests() in rbd_handle_released_lock().
3620 */
3621 cancel_delayed_work(&rbd_dev->lock_dwork);
3622
3623 return true;
3624}
3625
3626static void rbd_release_lock_work(struct work_struct *work)
3627{
3628 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
3629 unlock_work);
3630
3631 down_write(&rbd_dev->lock_rwsem);
3632 rbd_release_lock(rbd_dev);
3633 up_write(&rbd_dev->lock_rwsem);
3634}
3635
3636static void rbd_handle_acquired_lock(struct rbd_device *rbd_dev, u8 struct_v,
3637 void **p)
3638{
3639 struct rbd_client_id cid = { 0 };
3640
3641 if (struct_v >= 2) {
3642 cid.gid = ceph_decode_64(p);
3643 cid.handle = ceph_decode_64(p);
3644 }
3645
3646 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
3647 cid.handle);
3648 if (!rbd_cid_equal(&cid, &rbd_empty_cid)) {
3649 down_write(&rbd_dev->lock_rwsem);
3650 if (rbd_cid_equal(&cid, &rbd_dev->owner_cid)) {
3651 /*
3652 * we already know that the remote client is
3653 * the owner
3654 */
3655 up_write(&rbd_dev->lock_rwsem);
3656 return;
3657 }
3658
3659 rbd_set_owner_cid(rbd_dev, &cid);
3660 downgrade_write(&rbd_dev->lock_rwsem);
3661 } else {
3662 down_read(&rbd_dev->lock_rwsem);
3663 }
3664
3665 if (!__rbd_is_lock_owner(rbd_dev))
3666 wake_requests(rbd_dev, false);
3667 up_read(&rbd_dev->lock_rwsem);
3668}
3669
3670static void rbd_handle_released_lock(struct rbd_device *rbd_dev, u8 struct_v,
3671 void **p)
3672{
3673 struct rbd_client_id cid = { 0 };
3674
3675 if (struct_v >= 2) {
3676 cid.gid = ceph_decode_64(p);
3677 cid.handle = ceph_decode_64(p);
3678 }
3679
3680 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
3681 cid.handle);
3682 if (!rbd_cid_equal(&cid, &rbd_empty_cid)) {
3683 down_write(&rbd_dev->lock_rwsem);
3684 if (!rbd_cid_equal(&cid, &rbd_dev->owner_cid)) {
3685 dout("%s rbd_dev %p unexpected owner, cid %llu-%llu != owner_cid %llu-%llu\n",
3686 __func__, rbd_dev, cid.gid, cid.handle,
3687 rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle);
3688 up_write(&rbd_dev->lock_rwsem);
3689 return;
3690 }
3691
3692 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
3693 downgrade_write(&rbd_dev->lock_rwsem);
3694 } else {
3695 down_read(&rbd_dev->lock_rwsem);
3696 }
3697
3698 if (!__rbd_is_lock_owner(rbd_dev))
3699 wake_requests(rbd_dev, false);
3700 up_read(&rbd_dev->lock_rwsem);
3701}
3702
3703static bool rbd_handle_request_lock(struct rbd_device *rbd_dev, u8 struct_v,
3704 void **p)
3705{
3706 struct rbd_client_id my_cid = rbd_get_cid(rbd_dev);
3707 struct rbd_client_id cid = { 0 };
3708 bool need_to_send;
3709
3710 if (struct_v >= 2) {
3711 cid.gid = ceph_decode_64(p);
3712 cid.handle = ceph_decode_64(p);
3713 }
3714
3715 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
3716 cid.handle);
3717 if (rbd_cid_equal(&cid, &my_cid))
3718 return false;
3719
3720 down_read(&rbd_dev->lock_rwsem);
3721 need_to_send = __rbd_is_lock_owner(rbd_dev);
3722 if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) {
3723 if (!rbd_cid_equal(&rbd_dev->owner_cid, &rbd_empty_cid)) {
3724 dout("%s rbd_dev %p queueing unlock_work\n", __func__,
3725 rbd_dev);
3726 queue_work(rbd_dev->task_wq, &rbd_dev->unlock_work);
3727 }
3728 }
3729 up_read(&rbd_dev->lock_rwsem);
3730 return need_to_send;
3731}
3732
3733static void __rbd_acknowledge_notify(struct rbd_device *rbd_dev,
3734 u64 notify_id, u64 cookie, s32 *result)
3735{
3736 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3737 int buf_size = 4 + CEPH_ENCODING_START_BLK_LEN;
3738 char buf[buf_size];
3739 int ret;
3740
3741 if (result) {
3742 void *p = buf;
3743
3744 /* encode ResponseMessage */
3745 ceph_start_encoding(&p, 1, 1,
3746 buf_size - CEPH_ENCODING_START_BLK_LEN);
3747 ceph_encode_32(&p, *result);
3748 } else {
3749 buf_size = 0;
3750 }
3751
3752 ret = ceph_osdc_notify_ack(osdc, &rbd_dev->header_oid,
3753 &rbd_dev->header_oloc, notify_id, cookie,
3754 buf, buf_size);
3755 if (ret)
3756 rbd_warn(rbd_dev, "acknowledge_notify failed: %d", ret);
3757}
3758
3759static void rbd_acknowledge_notify(struct rbd_device *rbd_dev, u64 notify_id,
3760 u64 cookie)
3761{
3762 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3763 __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, NULL);
3764}
3765
3766static void rbd_acknowledge_notify_result(struct rbd_device *rbd_dev,
3767 u64 notify_id, u64 cookie, s32 result)
3768{
3769 dout("%s rbd_dev %p result %d\n", __func__, rbd_dev, result);
3770 __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, &result);
3771}
3772
Ilya Dryomov922dab62016-05-26 01:15:02 +02003773static void rbd_watch_cb(void *arg, u64 notify_id, u64 cookie,
3774 u64 notifier_id, void *data, size_t data_len)
Alex Elderb8d70032012-11-30 17:53:04 -06003775{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003776 struct rbd_device *rbd_dev = arg;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003777 void *p = data;
3778 void *const end = p + data_len;
3779 u8 struct_v;
3780 u32 len;
3781 u32 notify_op;
Alex Elderb8d70032012-11-30 17:53:04 -06003782 int ret;
3783
Ilya Dryomoved95b212016-08-12 16:40:02 +02003784 dout("%s rbd_dev %p cookie %llu notify_id %llu data_len %zu\n",
3785 __func__, rbd_dev, cookie, notify_id, data_len);
3786 if (data_len) {
3787 ret = ceph_start_decoding(&p, end, 1, "NotifyMessage",
3788 &struct_v, &len);
3789 if (ret) {
3790 rbd_warn(rbd_dev, "failed to decode NotifyMessage: %d",
3791 ret);
3792 return;
3793 }
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04003794
Ilya Dryomoved95b212016-08-12 16:40:02 +02003795 notify_op = ceph_decode_32(&p);
3796 } else {
3797 /* legacy notification for header updates */
3798 notify_op = RBD_NOTIFY_OP_HEADER_UPDATE;
3799 len = 0;
3800 }
Alex Elderb8d70032012-11-30 17:53:04 -06003801
Ilya Dryomoved95b212016-08-12 16:40:02 +02003802 dout("%s rbd_dev %p notify_op %u\n", __func__, rbd_dev, notify_op);
3803 switch (notify_op) {
3804 case RBD_NOTIFY_OP_ACQUIRED_LOCK:
3805 rbd_handle_acquired_lock(rbd_dev, struct_v, &p);
3806 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3807 break;
3808 case RBD_NOTIFY_OP_RELEASED_LOCK:
3809 rbd_handle_released_lock(rbd_dev, struct_v, &p);
3810 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3811 break;
3812 case RBD_NOTIFY_OP_REQUEST_LOCK:
3813 if (rbd_handle_request_lock(rbd_dev, struct_v, &p))
3814 /*
3815 * send ResponseMessage(0) back so the client
3816 * can detect a missing owner
3817 */
3818 rbd_acknowledge_notify_result(rbd_dev, notify_id,
3819 cookie, 0);
3820 else
3821 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3822 break;
3823 case RBD_NOTIFY_OP_HEADER_UPDATE:
3824 ret = rbd_dev_refresh(rbd_dev);
3825 if (ret)
3826 rbd_warn(rbd_dev, "refresh failed: %d", ret);
3827
3828 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3829 break;
3830 default:
3831 if (rbd_is_lock_owner(rbd_dev))
3832 rbd_acknowledge_notify_result(rbd_dev, notify_id,
3833 cookie, -EOPNOTSUPP);
3834 else
3835 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3836 break;
3837 }
Alex Elderb8d70032012-11-30 17:53:04 -06003838}
3839
Ilya Dryomov99d16942016-08-12 16:11:41 +02003840static void __rbd_unregister_watch(struct rbd_device *rbd_dev);
3841
Ilya Dryomov922dab62016-05-26 01:15:02 +02003842static void rbd_watch_errcb(void *arg, u64 cookie, int err)
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003843{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003844 struct rbd_device *rbd_dev = arg;
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003845
Ilya Dryomov922dab62016-05-26 01:15:02 +02003846 rbd_warn(rbd_dev, "encountered watch error: %d", err);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003847
Ilya Dryomoved95b212016-08-12 16:40:02 +02003848 down_write(&rbd_dev->lock_rwsem);
3849 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
3850 up_write(&rbd_dev->lock_rwsem);
3851
Ilya Dryomov99d16942016-08-12 16:11:41 +02003852 mutex_lock(&rbd_dev->watch_mutex);
3853 if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) {
3854 __rbd_unregister_watch(rbd_dev);
3855 rbd_dev->watch_state = RBD_WATCH_STATE_ERROR;
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003856
Ilya Dryomov99d16942016-08-12 16:11:41 +02003857 queue_delayed_work(rbd_dev->task_wq, &rbd_dev->watch_dwork, 0);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003858 }
Ilya Dryomov99d16942016-08-12 16:11:41 +02003859 mutex_unlock(&rbd_dev->watch_mutex);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003860}
3861
3862/*
Ilya Dryomov99d16942016-08-12 16:11:41 +02003863 * watch_mutex must be locked
Alex Elder9969ebc2013-01-18 12:31:10 -06003864 */
Ilya Dryomov99d16942016-08-12 16:11:41 +02003865static int __rbd_register_watch(struct rbd_device *rbd_dev)
Alex Elder9969ebc2013-01-18 12:31:10 -06003866{
3867 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Ilya Dryomov922dab62016-05-26 01:15:02 +02003868 struct ceph_osd_linger_request *handle;
Alex Elder9969ebc2013-01-18 12:31:10 -06003869
Ilya Dryomov922dab62016-05-26 01:15:02 +02003870 rbd_assert(!rbd_dev->watch_handle);
Ilya Dryomov99d16942016-08-12 16:11:41 +02003871 dout("%s rbd_dev %p\n", __func__, rbd_dev);
Alex Elder9969ebc2013-01-18 12:31:10 -06003872
Ilya Dryomov922dab62016-05-26 01:15:02 +02003873 handle = ceph_osdc_watch(osdc, &rbd_dev->header_oid,
3874 &rbd_dev->header_oloc, rbd_watch_cb,
3875 rbd_watch_errcb, rbd_dev);
3876 if (IS_ERR(handle))
3877 return PTR_ERR(handle);
Alex Elder9969ebc2013-01-18 12:31:10 -06003878
Ilya Dryomov922dab62016-05-26 01:15:02 +02003879 rbd_dev->watch_handle = handle;
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003880 return 0;
Alex Elder9969ebc2013-01-18 12:31:10 -06003881}
3882
Ilya Dryomov99d16942016-08-12 16:11:41 +02003883/*
3884 * watch_mutex must be locked
3885 */
3886static void __rbd_unregister_watch(struct rbd_device *rbd_dev)
Ilya Dryomovfca27062013-12-16 18:02:40 +02003887{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003888 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3889 int ret;
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003890
Ilya Dryomov99d16942016-08-12 16:11:41 +02003891 rbd_assert(rbd_dev->watch_handle);
3892 dout("%s rbd_dev %p\n", __func__, rbd_dev);
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003893
Ilya Dryomov922dab62016-05-26 01:15:02 +02003894 ret = ceph_osdc_unwatch(osdc, rbd_dev->watch_handle);
3895 if (ret)
3896 rbd_warn(rbd_dev, "failed to unwatch: %d", ret);
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003897
Ilya Dryomov922dab62016-05-26 01:15:02 +02003898 rbd_dev->watch_handle = NULL;
Ilya Dryomovc525f032016-04-28 16:07:26 +02003899}
3900
Ilya Dryomov99d16942016-08-12 16:11:41 +02003901static int rbd_register_watch(struct rbd_device *rbd_dev)
Ilya Dryomovc525f032016-04-28 16:07:26 +02003902{
Ilya Dryomov99d16942016-08-12 16:11:41 +02003903 int ret;
Ilya Dryomov811c6682016-04-15 16:22:16 +02003904
Ilya Dryomov99d16942016-08-12 16:11:41 +02003905 mutex_lock(&rbd_dev->watch_mutex);
3906 rbd_assert(rbd_dev->watch_state == RBD_WATCH_STATE_UNREGISTERED);
3907 ret = __rbd_register_watch(rbd_dev);
3908 if (ret)
3909 goto out;
3910
3911 rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED;
3912 rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id;
3913
3914out:
3915 mutex_unlock(&rbd_dev->watch_mutex);
3916 return ret;
3917}
3918
3919static void cancel_tasks_sync(struct rbd_device *rbd_dev)
3920{
3921 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3922
3923 cancel_delayed_work_sync(&rbd_dev->watch_dwork);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003924 cancel_work_sync(&rbd_dev->acquired_lock_work);
3925 cancel_work_sync(&rbd_dev->released_lock_work);
3926 cancel_delayed_work_sync(&rbd_dev->lock_dwork);
3927 cancel_work_sync(&rbd_dev->unlock_work);
Ilya Dryomov99d16942016-08-12 16:11:41 +02003928}
3929
3930static void rbd_unregister_watch(struct rbd_device *rbd_dev)
3931{
Ilya Dryomoved95b212016-08-12 16:40:02 +02003932 WARN_ON(waitqueue_active(&rbd_dev->lock_waitq));
Ilya Dryomov99d16942016-08-12 16:11:41 +02003933 cancel_tasks_sync(rbd_dev);
3934
3935 mutex_lock(&rbd_dev->watch_mutex);
3936 if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED)
3937 __rbd_unregister_watch(rbd_dev);
3938 rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED;
3939 mutex_unlock(&rbd_dev->watch_mutex);
3940
Ilya Dryomov811c6682016-04-15 16:22:16 +02003941 ceph_osdc_flush_notifies(&rbd_dev->rbd_client->client->osdc);
Ilya Dryomovfca27062013-12-16 18:02:40 +02003942}
3943
Ilya Dryomov99d16942016-08-12 16:11:41 +02003944static void rbd_reregister_watch(struct work_struct *work)
3945{
3946 struct rbd_device *rbd_dev = container_of(to_delayed_work(work),
3947 struct rbd_device, watch_dwork);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003948 bool was_lock_owner = false;
Ilya Dryomov99d16942016-08-12 16:11:41 +02003949 int ret;
3950
3951 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3952
Ilya Dryomoved95b212016-08-12 16:40:02 +02003953 down_write(&rbd_dev->lock_rwsem);
3954 if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED)
3955 was_lock_owner = rbd_release_lock(rbd_dev);
3956
Ilya Dryomov99d16942016-08-12 16:11:41 +02003957 mutex_lock(&rbd_dev->watch_mutex);
3958 if (rbd_dev->watch_state != RBD_WATCH_STATE_ERROR)
3959 goto fail_unlock;
3960
3961 ret = __rbd_register_watch(rbd_dev);
3962 if (ret) {
3963 rbd_warn(rbd_dev, "failed to reregister watch: %d", ret);
3964 if (ret != -EBLACKLISTED)
3965 queue_delayed_work(rbd_dev->task_wq,
3966 &rbd_dev->watch_dwork,
3967 RBD_RETRY_DELAY);
3968 goto fail_unlock;
3969 }
3970
3971 rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED;
3972 rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id;
3973 mutex_unlock(&rbd_dev->watch_mutex);
3974
3975 ret = rbd_dev_refresh(rbd_dev);
3976 if (ret)
3977 rbd_warn(rbd_dev, "reregisteration refresh failed: %d", ret);
3978
Ilya Dryomoved95b212016-08-12 16:40:02 +02003979 if (was_lock_owner) {
3980 ret = rbd_try_lock(rbd_dev);
3981 if (ret)
3982 rbd_warn(rbd_dev, "reregisteration lock failed: %d",
3983 ret);
3984 }
3985
3986 up_write(&rbd_dev->lock_rwsem);
3987 wake_requests(rbd_dev, true);
Ilya Dryomov99d16942016-08-12 16:11:41 +02003988 return;
3989
3990fail_unlock:
3991 mutex_unlock(&rbd_dev->watch_mutex);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003992 up_write(&rbd_dev->lock_rwsem);
Ilya Dryomov99d16942016-08-12 16:11:41 +02003993}
3994
Alex Elder36be9a72013-01-19 00:30:28 -06003995/*
Alex Elderf40eb342013-04-25 15:09:42 -05003996 * Synchronous osd object method call. Returns the number of bytes
3997 * returned in the outbound buffer, or a negative error code.
Alex Elder36be9a72013-01-19 00:30:28 -06003998 */
3999static int rbd_obj_method_sync(struct rbd_device *rbd_dev,
4000 const char *object_name,
4001 const char *class_name,
4002 const char *method_name,
Alex Elder41579762013-04-21 12:14:45 -05004003 const void *outbound,
Alex Elder36be9a72013-01-19 00:30:28 -06004004 size_t outbound_size,
Alex Elder41579762013-04-21 12:14:45 -05004005 void *inbound,
Alex Eldere2a58ee2013-04-30 00:44:33 -05004006 size_t inbound_size)
Alex Elder36be9a72013-01-19 00:30:28 -06004007{
Alex Elder21692382013-04-05 01:27:12 -05004008 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder36be9a72013-01-19 00:30:28 -06004009 struct rbd_obj_request *obj_request;
Alex Elder36be9a72013-01-19 00:30:28 -06004010 struct page **pages;
4011 u32 page_count;
4012 int ret;
4013
4014 /*
Alex Elder6010a452013-04-05 01:27:11 -05004015 * Method calls are ultimately read operations. The result
4016 * should placed into the inbound buffer provided. They
4017 * also supply outbound data--parameters for the object
4018 * method. Currently if this is present it will be a
4019 * snapshot id.
Alex Elder36be9a72013-01-19 00:30:28 -06004020 */
Alex Elder57385b52013-04-21 12:14:45 -05004021 page_count = (u32)calc_pages_for(0, inbound_size);
Alex Elder36be9a72013-01-19 00:30:28 -06004022 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
4023 if (IS_ERR(pages))
4024 return PTR_ERR(pages);
4025
4026 ret = -ENOMEM;
Alex Elder6010a452013-04-05 01:27:11 -05004027 obj_request = rbd_obj_request_create(object_name, 0, inbound_size,
Alex Elder36be9a72013-01-19 00:30:28 -06004028 OBJ_REQUEST_PAGES);
4029 if (!obj_request)
4030 goto out;
4031
4032 obj_request->pages = pages;
4033 obj_request->page_count = page_count;
4034
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004035 obj_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_READ, 1,
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02004036 obj_request);
Alex Elder36be9a72013-01-19 00:30:28 -06004037 if (!obj_request->osd_req)
4038 goto out;
4039
Alex Elderc99d2d42013-04-05 01:27:11 -05004040 osd_req_op_cls_init(obj_request->osd_req, 0, CEPH_OSD_OP_CALL,
Alex Elder04017e22013-04-05 14:46:02 -05004041 class_name, method_name);
4042 if (outbound_size) {
4043 struct ceph_pagelist *pagelist;
4044
4045 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
4046 if (!pagelist)
4047 goto out;
4048
4049 ceph_pagelist_init(pagelist);
4050 ceph_pagelist_append(pagelist, outbound, outbound_size);
4051 osd_req_op_cls_request_data_pagelist(obj_request->osd_req, 0,
4052 pagelist);
4053 }
Alex Eldera4ce40a2013-04-05 01:27:12 -05004054 osd_req_op_cls_response_data_pages(obj_request->osd_req, 0,
4055 obj_request->pages, inbound_size,
Alex Elder44cd1882013-04-05 01:27:12 -05004056 0, false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05004057 rbd_osd_req_format_read(obj_request);
Alex Elder430c28c2013-04-03 21:32:51 -05004058
Alex Elder36be9a72013-01-19 00:30:28 -06004059 ret = rbd_obj_request_submit(osdc, obj_request);
4060 if (ret)
4061 goto out;
4062 ret = rbd_obj_request_wait(obj_request);
4063 if (ret)
4064 goto out;
4065
4066 ret = obj_request->result;
4067 if (ret < 0)
4068 goto out;
Alex Elder57385b52013-04-21 12:14:45 -05004069
4070 rbd_assert(obj_request->xferred < (u64)INT_MAX);
4071 ret = (int)obj_request->xferred;
Alex Elder903bb322013-02-06 13:11:38 -06004072 ceph_copy_from_page_vector(pages, inbound, 0, obj_request->xferred);
Alex Elder36be9a72013-01-19 00:30:28 -06004073out:
4074 if (obj_request)
4075 rbd_obj_request_put(obj_request);
4076 else
4077 ceph_release_page_vector(pages, page_count);
4078
4079 return ret;
4080}
4081
Ilya Dryomoved95b212016-08-12 16:40:02 +02004082/*
4083 * lock_rwsem must be held for read
4084 */
4085static void rbd_wait_state_locked(struct rbd_device *rbd_dev)
4086{
4087 DEFINE_WAIT(wait);
4088
4089 do {
4090 /*
4091 * Note the use of mod_delayed_work() in rbd_acquire_lock()
4092 * and cancel_delayed_work() in wake_requests().
4093 */
4094 dout("%s rbd_dev %p queueing lock_dwork\n", __func__, rbd_dev);
4095 queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0);
4096 prepare_to_wait_exclusive(&rbd_dev->lock_waitq, &wait,
4097 TASK_UNINTERRUPTIBLE);
4098 up_read(&rbd_dev->lock_rwsem);
4099 schedule();
4100 down_read(&rbd_dev->lock_rwsem);
4101 } while (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED);
4102 finish_wait(&rbd_dev->lock_waitq, &wait);
4103}
4104
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004105static void rbd_queue_workfn(struct work_struct *work)
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004106{
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004107 struct request *rq = blk_mq_rq_from_pdu(work);
4108 struct rbd_device *rbd_dev = rq->q->queuedata;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004109 struct rbd_img_request *img_request;
Josh Durgin4e752f02014-04-08 11:12:11 -07004110 struct ceph_snap_context *snapc = NULL;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004111 u64 offset = (u64)blk_rq_pos(rq) << SECTOR_SHIFT;
4112 u64 length = blk_rq_bytes(rq);
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004113 enum obj_operation_type op_type;
Josh Durgin4e752f02014-04-08 11:12:11 -07004114 u64 mapping_size;
Ilya Dryomov80de1912016-09-20 14:23:17 +02004115 bool must_be_locked;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004116 int result;
4117
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004118 if (rq->cmd_type != REQ_TYPE_FS) {
4119 dout("%s: non-fs request type %d\n", __func__,
4120 (int) rq->cmd_type);
4121 result = -EIO;
4122 goto err;
4123 }
4124
Mike Christiec2df40d2016-06-05 14:32:17 -05004125 if (req_op(rq) == REQ_OP_DISCARD)
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004126 op_type = OBJ_OP_DISCARD;
Mike Christiec2df40d2016-06-05 14:32:17 -05004127 else if (req_op(rq) == REQ_OP_WRITE)
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004128 op_type = OBJ_OP_WRITE;
4129 else
4130 op_type = OBJ_OP_READ;
4131
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004132 /* Ignore/skip any zero-length requests */
4133
4134 if (!length) {
4135 dout("%s: zero-length request\n", __func__);
4136 result = 0;
4137 goto err_rq;
4138 }
4139
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004140 /* Only reads are allowed to a read-only device */
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004141
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004142 if (op_type != OBJ_OP_READ) {
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004143 if (rbd_dev->mapping.read_only) {
4144 result = -EROFS;
4145 goto err_rq;
4146 }
4147 rbd_assert(rbd_dev->spec->snap_id == CEPH_NOSNAP);
4148 }
4149
4150 /*
4151 * Quit early if the mapped snapshot no longer exists. It's
4152 * still possible the snapshot will have disappeared by the
4153 * time our request arrives at the osd, but there's no sense in
4154 * sending it if we already know.
4155 */
4156 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) {
4157 dout("request for non-existent snapshot");
4158 rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP);
4159 result = -ENXIO;
4160 goto err_rq;
4161 }
4162
4163 if (offset && length > U64_MAX - offset + 1) {
4164 rbd_warn(rbd_dev, "bad request range (%llu~%llu)", offset,
4165 length);
4166 result = -EINVAL;
4167 goto err_rq; /* Shouldn't happen */
4168 }
4169
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004170 blk_mq_start_request(rq);
4171
Josh Durgin4e752f02014-04-08 11:12:11 -07004172 down_read(&rbd_dev->header_rwsem);
4173 mapping_size = rbd_dev->mapping.size;
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004174 if (op_type != OBJ_OP_READ) {
Josh Durgin4e752f02014-04-08 11:12:11 -07004175 snapc = rbd_dev->header.snapc;
4176 ceph_get_snap_context(snapc);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004177 must_be_locked = rbd_is_lock_supported(rbd_dev);
Ilya Dryomov80de1912016-09-20 14:23:17 +02004178 } else {
4179 must_be_locked = rbd_dev->opts->lock_on_read &&
4180 rbd_is_lock_supported(rbd_dev);
Josh Durgin4e752f02014-04-08 11:12:11 -07004181 }
4182 up_read(&rbd_dev->header_rwsem);
4183
4184 if (offset + length > mapping_size) {
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004185 rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)", offset,
Josh Durgin4e752f02014-04-08 11:12:11 -07004186 length, mapping_size);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004187 result = -EIO;
4188 goto err_rq;
4189 }
4190
Ilya Dryomoved95b212016-08-12 16:40:02 +02004191 if (must_be_locked) {
4192 down_read(&rbd_dev->lock_rwsem);
4193 if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED)
4194 rbd_wait_state_locked(rbd_dev);
4195 }
4196
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004197 img_request = rbd_img_request_create(rbd_dev, offset, length, op_type,
Josh Durgin4e752f02014-04-08 11:12:11 -07004198 snapc);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004199 if (!img_request) {
4200 result = -ENOMEM;
Ilya Dryomoved95b212016-08-12 16:40:02 +02004201 goto err_unlock;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004202 }
4203 img_request->rq = rq;
Ilya Dryomov70b16db2015-11-27 19:23:24 +01004204 snapc = NULL; /* img_request consumes a ref */
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004205
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004206 if (op_type == OBJ_OP_DISCARD)
4207 result = rbd_img_request_fill(img_request, OBJ_REQUEST_NODATA,
4208 NULL);
4209 else
4210 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
4211 rq->bio);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004212 if (result)
4213 goto err_img_request;
4214
4215 result = rbd_img_request_submit(img_request);
4216 if (result)
4217 goto err_img_request;
4218
Ilya Dryomoved95b212016-08-12 16:40:02 +02004219 if (must_be_locked)
4220 up_read(&rbd_dev->lock_rwsem);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004221 return;
4222
4223err_img_request:
4224 rbd_img_request_put(img_request);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004225err_unlock:
4226 if (must_be_locked)
4227 up_read(&rbd_dev->lock_rwsem);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004228err_rq:
4229 if (result)
4230 rbd_warn(rbd_dev, "%s %llx at %llx result %d",
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004231 obj_op_name(op_type), length, offset, result);
SF Markus Elfringe96a6502014-11-02 15:20:59 +01004232 ceph_put_snap_context(snapc);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004233err:
4234 blk_mq_end_request(rq, result);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004235}
4236
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004237static int rbd_queue_rq(struct blk_mq_hw_ctx *hctx,
4238 const struct blk_mq_queue_data *bd)
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004239{
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004240 struct request *rq = bd->rq;
4241 struct work_struct *work = blk_mq_rq_to_pdu(rq);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004242
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004243 queue_work(rbd_wq, work);
4244 return BLK_MQ_RQ_QUEUE_OK;
Alex Elderbf0d5f502012-11-22 00:00:08 -06004245}
4246
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004247static void rbd_free_disk(struct rbd_device *rbd_dev)
4248{
4249 struct gendisk *disk = rbd_dev->disk;
4250
4251 if (!disk)
4252 return;
4253
Alex Eldera0cab922013-04-25 23:15:08 -05004254 rbd_dev->disk = NULL;
4255 if (disk->flags & GENHD_FL_UP) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004256 del_gendisk(disk);
Alex Eldera0cab922013-04-25 23:15:08 -05004257 if (disk->queue)
4258 blk_cleanup_queue(disk->queue);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004259 blk_mq_free_tag_set(&rbd_dev->tag_set);
Alex Eldera0cab922013-04-25 23:15:08 -05004260 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004261 put_disk(disk);
4262}
4263
Alex Elder788e2df2013-01-17 12:25:27 -06004264static int rbd_obj_read_sync(struct rbd_device *rbd_dev,
4265 const char *object_name,
Alex Elder7097f8d2013-04-30 00:44:33 -05004266 u64 offset, u64 length, void *buf)
Alex Elder788e2df2013-01-17 12:25:27 -06004267
4268{
Alex Elder21692382013-04-05 01:27:12 -05004269 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder788e2df2013-01-17 12:25:27 -06004270 struct rbd_obj_request *obj_request;
Alex Elder788e2df2013-01-17 12:25:27 -06004271 struct page **pages = NULL;
4272 u32 page_count;
Alex Elder1ceae7e2013-02-06 13:11:38 -06004273 size_t size;
Alex Elder788e2df2013-01-17 12:25:27 -06004274 int ret;
4275
4276 page_count = (u32) calc_pages_for(offset, length);
4277 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
4278 if (IS_ERR(pages))
Jan Karaa8d42052014-10-22 09:17:24 +02004279 return PTR_ERR(pages);
Alex Elder788e2df2013-01-17 12:25:27 -06004280
4281 ret = -ENOMEM;
4282 obj_request = rbd_obj_request_create(object_name, offset, length,
Alex Elder36be9a72013-01-19 00:30:28 -06004283 OBJ_REQUEST_PAGES);
Alex Elder788e2df2013-01-17 12:25:27 -06004284 if (!obj_request)
4285 goto out;
4286
4287 obj_request->pages = pages;
4288 obj_request->page_count = page_count;
4289
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004290 obj_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_READ, 1,
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02004291 obj_request);
Alex Elder788e2df2013-01-17 12:25:27 -06004292 if (!obj_request->osd_req)
4293 goto out;
4294
Alex Elderc99d2d42013-04-05 01:27:11 -05004295 osd_req_op_extent_init(obj_request->osd_req, 0, CEPH_OSD_OP_READ,
4296 offset, length, 0, 0);
Alex Elder406e2c92013-04-15 14:50:36 -05004297 osd_req_op_extent_osd_data_pages(obj_request->osd_req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05004298 obj_request->pages,
Alex Elder44cd1882013-04-05 01:27:12 -05004299 obj_request->length,
4300 obj_request->offset & ~PAGE_MASK,
4301 false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05004302 rbd_osd_req_format_read(obj_request);
Alex Elder430c28c2013-04-03 21:32:51 -05004303
Alex Elder788e2df2013-01-17 12:25:27 -06004304 ret = rbd_obj_request_submit(osdc, obj_request);
4305 if (ret)
4306 goto out;
4307 ret = rbd_obj_request_wait(obj_request);
4308 if (ret)
4309 goto out;
4310
4311 ret = obj_request->result;
4312 if (ret < 0)
4313 goto out;
Alex Elder1ceae7e2013-02-06 13:11:38 -06004314
4315 rbd_assert(obj_request->xferred <= (u64) SIZE_MAX);
4316 size = (size_t) obj_request->xferred;
Alex Elder903bb322013-02-06 13:11:38 -06004317 ceph_copy_from_page_vector(pages, buf, 0, size);
Alex Elder7097f8d2013-04-30 00:44:33 -05004318 rbd_assert(size <= (size_t)INT_MAX);
4319 ret = (int)size;
Alex Elder788e2df2013-01-17 12:25:27 -06004320out:
4321 if (obj_request)
4322 rbd_obj_request_put(obj_request);
4323 else
4324 ceph_release_page_vector(pages, page_count);
4325
4326 return ret;
4327}
4328
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004329/*
Alex Elder662518b2013-05-06 09:51:29 -05004330 * Read the complete header for the given rbd device. On successful
4331 * return, the rbd_dev->header field will contain up-to-date
4332 * information about the image.
Alex Elder4156d992012-08-02 11:29:46 -05004333 */
Alex Elder99a41eb2013-05-06 09:51:30 -05004334static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev)
Alex Elder4156d992012-08-02 11:29:46 -05004335{
4336 struct rbd_image_header_ondisk *ondisk = NULL;
4337 u32 snap_count = 0;
4338 u64 names_size = 0;
4339 u32 want_count;
4340 int ret;
4341
4342 /*
4343 * The complete header will include an array of its 64-bit
4344 * snapshot ids, followed by the names of those snapshots as
4345 * a contiguous block of NUL-terminated strings. Note that
4346 * the number of snapshots could change by the time we read
4347 * it in, in which case we re-read it.
4348 */
4349 do {
4350 size_t size;
4351
4352 kfree(ondisk);
4353
4354 size = sizeof (*ondisk);
4355 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
4356 size += names_size;
4357 ondisk = kmalloc(size, GFP_KERNEL);
4358 if (!ondisk)
Alex Elder662518b2013-05-06 09:51:29 -05004359 return -ENOMEM;
Alex Elder4156d992012-08-02 11:29:46 -05004360
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004361 ret = rbd_obj_read_sync(rbd_dev, rbd_dev->header_oid.name,
Alex Elder7097f8d2013-04-30 00:44:33 -05004362 0, size, ondisk);
Alex Elder4156d992012-08-02 11:29:46 -05004363 if (ret < 0)
Alex Elder662518b2013-05-06 09:51:29 -05004364 goto out;
Alex Elderc0cd10db2013-04-26 09:43:47 -05004365 if ((size_t)ret < size) {
Alex Elder4156d992012-08-02 11:29:46 -05004366 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05004367 rbd_warn(rbd_dev, "short header read (want %zd got %d)",
4368 size, ret);
Alex Elder662518b2013-05-06 09:51:29 -05004369 goto out;
Alex Elder4156d992012-08-02 11:29:46 -05004370 }
4371 if (!rbd_dev_ondisk_valid(ondisk)) {
4372 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05004373 rbd_warn(rbd_dev, "invalid header");
Alex Elder662518b2013-05-06 09:51:29 -05004374 goto out;
Alex Elder4156d992012-08-02 11:29:46 -05004375 }
4376
4377 names_size = le64_to_cpu(ondisk->snap_names_len);
4378 want_count = snap_count;
4379 snap_count = le32_to_cpu(ondisk->snap_count);
4380 } while (snap_count != want_count);
4381
Alex Elder662518b2013-05-06 09:51:29 -05004382 ret = rbd_header_from_disk(rbd_dev, ondisk);
4383out:
Alex Elder4156d992012-08-02 11:29:46 -05004384 kfree(ondisk);
4385
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004386 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004387}
4388
Alex Elder15228ed2013-05-01 12:43:03 -05004389/*
4390 * Clear the rbd device's EXISTS flag if the snapshot it's mapped to
4391 * has disappeared from the (just updated) snapshot context.
4392 */
4393static void rbd_exists_validate(struct rbd_device *rbd_dev)
4394{
4395 u64 snap_id;
4396
4397 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags))
4398 return;
4399
4400 snap_id = rbd_dev->spec->snap_id;
4401 if (snap_id == CEPH_NOSNAP)
4402 return;
4403
4404 if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX)
4405 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
4406}
4407
Josh Durgin98752012013-08-29 17:26:31 -07004408static void rbd_dev_update_size(struct rbd_device *rbd_dev)
4409{
4410 sector_t size;
Josh Durgin98752012013-08-29 17:26:31 -07004411
4412 /*
Ilya Dryomov811c6682016-04-15 16:22:16 +02004413 * If EXISTS is not set, rbd_dev->disk may be NULL, so don't
4414 * try to update its size. If REMOVING is set, updating size
4415 * is just useless work since the device can't be opened.
Josh Durgin98752012013-08-29 17:26:31 -07004416 */
Ilya Dryomov811c6682016-04-15 16:22:16 +02004417 if (test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags) &&
4418 !test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) {
Josh Durgin98752012013-08-29 17:26:31 -07004419 size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE;
4420 dout("setting size to %llu sectors", (unsigned long long)size);
4421 set_capacity(rbd_dev->disk, size);
4422 revalidate_disk(rbd_dev->disk);
4423 }
4424}
4425
Alex Eldercc4a38bd2013-04-30 00:44:33 -05004426static int rbd_dev_refresh(struct rbd_device *rbd_dev)
Alex Elder1fe5e992012-07-25 09:32:41 -05004427{
Alex Eldere627db02013-05-06 07:40:30 -05004428 u64 mapping_size;
Alex Elder1fe5e992012-07-25 09:32:41 -05004429 int ret;
4430
Alex Eldercfbf6372013-05-31 17:40:45 -05004431 down_write(&rbd_dev->header_rwsem);
Alex Elder3b5cf2a2013-05-29 11:18:59 -05004432 mapping_size = rbd_dev->mapping.size;
Ilya Dryomova720ae02014-07-23 17:11:19 +04004433
4434 ret = rbd_dev_header_info(rbd_dev);
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04004435 if (ret)
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004436 goto out;
Alex Elder15228ed2013-05-01 12:43:03 -05004437
Ilya Dryomove8f59b52014-07-24 10:42:13 +04004438 /*
4439 * If there is a parent, see if it has disappeared due to the
4440 * mapped image getting flattened.
4441 */
4442 if (rbd_dev->parent) {
4443 ret = rbd_dev_v2_parent_info(rbd_dev);
4444 if (ret)
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004445 goto out;
Ilya Dryomove8f59b52014-07-24 10:42:13 +04004446 }
4447
Ilya Dryomov5ff11082014-07-23 17:11:21 +04004448 if (rbd_dev->spec->snap_id == CEPH_NOSNAP) {
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004449 rbd_dev->mapping.size = rbd_dev->header.image_size;
Ilya Dryomov5ff11082014-07-23 17:11:21 +04004450 } else {
4451 /* validate mapped snapshot's EXISTS flag */
4452 rbd_exists_validate(rbd_dev);
4453 }
Alex Elder15228ed2013-05-01 12:43:03 -05004454
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004455out:
Alex Eldercfbf6372013-05-31 17:40:45 -05004456 up_write(&rbd_dev->header_rwsem);
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004457 if (!ret && mapping_size != rbd_dev->mapping.size)
Josh Durgin98752012013-08-29 17:26:31 -07004458 rbd_dev_update_size(rbd_dev);
Alex Elder1fe5e992012-07-25 09:32:41 -05004459
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004460 return ret;
Alex Elder1fe5e992012-07-25 09:32:41 -05004461}
4462
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004463static int rbd_init_request(void *data, struct request *rq,
4464 unsigned int hctx_idx, unsigned int request_idx,
4465 unsigned int numa_node)
4466{
4467 struct work_struct *work = blk_mq_rq_to_pdu(rq);
4468
4469 INIT_WORK(work, rbd_queue_workfn);
4470 return 0;
4471}
4472
4473static struct blk_mq_ops rbd_mq_ops = {
4474 .queue_rq = rbd_queue_rq,
4475 .map_queue = blk_mq_map_queue,
4476 .init_request = rbd_init_request,
4477};
4478
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004479static int rbd_init_disk(struct rbd_device *rbd_dev)
4480{
4481 struct gendisk *disk;
4482 struct request_queue *q;
Alex Elder593a9e72012-02-07 12:03:37 -06004483 u64 segment_size;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004484 int err;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004485
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004486 /* create gendisk info */
Ilya Dryomov7e513d42013-12-16 19:26:32 +02004487 disk = alloc_disk(single_major ?
4488 (1 << RBD_SINGLE_MAJOR_PART_SHIFT) :
4489 RBD_MINORS_PER_MAJOR);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004490 if (!disk)
Alex Elder1fcdb8a2012-08-29 17:11:06 -05004491 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004492
Alex Elderf0f8cef2012-01-29 13:57:44 -06004493 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
Alex Elderde71a292012-07-03 16:01:19 -05004494 rbd_dev->dev_id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004495 disk->major = rbd_dev->major;
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004496 disk->first_minor = rbd_dev->minor;
Ilya Dryomov7e513d42013-12-16 19:26:32 +02004497 if (single_major)
4498 disk->flags |= GENHD_FL_EXT_DEVT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004499 disk->fops = &rbd_bd_ops;
4500 disk->private_data = rbd_dev;
4501
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004502 memset(&rbd_dev->tag_set, 0, sizeof(rbd_dev->tag_set));
4503 rbd_dev->tag_set.ops = &rbd_mq_ops;
Ilya Dryomovb5584182015-06-23 16:21:19 +03004504 rbd_dev->tag_set.queue_depth = rbd_dev->opts->queue_depth;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004505 rbd_dev->tag_set.numa_node = NUMA_NO_NODE;
Ilya Dryomovb5584182015-06-23 16:21:19 +03004506 rbd_dev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004507 rbd_dev->tag_set.nr_hw_queues = 1;
4508 rbd_dev->tag_set.cmd_size = sizeof(struct work_struct);
4509
4510 err = blk_mq_alloc_tag_set(&rbd_dev->tag_set);
4511 if (err)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004512 goto out_disk;
Josh Durgin029bcbd2011-07-22 11:35:23 -07004513
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004514 q = blk_mq_init_queue(&rbd_dev->tag_set);
4515 if (IS_ERR(q)) {
4516 err = PTR_ERR(q);
4517 goto out_tag_set;
4518 }
4519
Ilya Dryomovd8a2c892015-03-24 16:15:17 +03004520 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
4521 /* QUEUE_FLAG_ADD_RANDOM is off by default for blk-mq */
Alex Elder593a9e72012-02-07 12:03:37 -06004522
Josh Durgin029bcbd2011-07-22 11:35:23 -07004523 /* set io sizes to object size */
Alex Elder593a9e72012-02-07 12:03:37 -06004524 segment_size = rbd_obj_bytes(&rbd_dev->header);
4525 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
Ilya Dryomov0d9fde42015-10-07 16:09:35 +02004526 q->limits.max_sectors = queue_max_hw_sectors(q);
Ilya Dryomovd3834fe2015-06-12 19:19:02 +03004527 blk_queue_max_segments(q, segment_size / SECTOR_SIZE);
Alex Elder593a9e72012-02-07 12:03:37 -06004528 blk_queue_max_segment_size(q, segment_size);
4529 blk_queue_io_min(q, segment_size);
4530 blk_queue_io_opt(q, segment_size);
Josh Durgin029bcbd2011-07-22 11:35:23 -07004531
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004532 /* enable the discard support */
4533 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
4534 q->limits.discard_granularity = segment_size;
4535 q->limits.discard_alignment = segment_size;
Jens Axboe2bb4cd52015-07-14 08:15:12 -06004536 blk_queue_max_discard_sectors(q, segment_size / SECTOR_SIZE);
Josh Durginb76f8232014-04-07 16:52:03 -07004537 q->limits.discard_zeroes_data = 1;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004538
Ronny Hegewaldbae818e2015-10-15 18:50:46 +00004539 if (!ceph_test_opt(rbd_dev->rbd_client->client, NOCRC))
4540 q->backing_dev_info.capabilities |= BDI_CAP_STABLE_WRITES;
4541
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004542 disk->queue = q;
4543
4544 q->queuedata = rbd_dev;
4545
4546 rbd_dev->disk = disk;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004547
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004548 return 0;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004549out_tag_set:
4550 blk_mq_free_tag_set(&rbd_dev->tag_set);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004551out_disk:
4552 put_disk(disk);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004553 return err;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004554}
4555
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004556/*
4557 sysfs
4558*/
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004559
Alex Elder593a9e72012-02-07 12:03:37 -06004560static struct rbd_device *dev_to_rbd_dev(struct device *dev)
4561{
4562 return container_of(dev, struct rbd_device, dev);
4563}
4564
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004565static ssize_t rbd_size_show(struct device *dev,
4566 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004567{
Alex Elder593a9e72012-02-07 12:03:37 -06004568 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004569
Alex Elderfc71d832013-04-26 15:44:36 -05004570 return sprintf(buf, "%llu\n",
4571 (unsigned long long)rbd_dev->mapping.size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004572}
4573
Alex Elder34b13182012-07-13 20:35:12 -05004574/*
4575 * Note this shows the features for whatever's mapped, which is not
4576 * necessarily the base image.
4577 */
4578static ssize_t rbd_features_show(struct device *dev,
4579 struct device_attribute *attr, char *buf)
4580{
4581 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4582
4583 return sprintf(buf, "0x%016llx\n",
Alex Elderfc71d832013-04-26 15:44:36 -05004584 (unsigned long long)rbd_dev->mapping.features);
Alex Elder34b13182012-07-13 20:35:12 -05004585}
4586
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004587static ssize_t rbd_major_show(struct device *dev,
4588 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004589{
Alex Elder593a9e72012-02-07 12:03:37 -06004590 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004591
Alex Elderfc71d832013-04-26 15:44:36 -05004592 if (rbd_dev->major)
4593 return sprintf(buf, "%d\n", rbd_dev->major);
4594
4595 return sprintf(buf, "(none)\n");
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004596}
Alex Elderfc71d832013-04-26 15:44:36 -05004597
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004598static ssize_t rbd_minor_show(struct device *dev,
4599 struct device_attribute *attr, char *buf)
4600{
4601 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4602
4603 return sprintf(buf, "%d\n", rbd_dev->minor);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004604}
4605
Ilya Dryomov005a07bf2016-08-18 18:38:43 +02004606static ssize_t rbd_client_addr_show(struct device *dev,
4607 struct device_attribute *attr, char *buf)
4608{
4609 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4610 struct ceph_entity_addr *client_addr =
4611 ceph_client_addr(rbd_dev->rbd_client->client);
4612
4613 return sprintf(buf, "%pISpc/%u\n", &client_addr->in_addr,
4614 le32_to_cpu(client_addr->nonce));
4615}
4616
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004617static ssize_t rbd_client_id_show(struct device *dev,
4618 struct device_attribute *attr, char *buf)
4619{
Alex Elder593a9e72012-02-07 12:03:37 -06004620 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004621
Alex Elder1dbb4392012-01-24 10:08:37 -06004622 return sprintf(buf, "client%lld\n",
Ilya Dryomov033268a2016-08-12 14:59:58 +02004623 ceph_client_gid(rbd_dev->rbd_client->client));
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004624}
4625
Mike Christie267fb902016-08-18 18:38:43 +02004626static ssize_t rbd_cluster_fsid_show(struct device *dev,
4627 struct device_attribute *attr, char *buf)
4628{
4629 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4630
4631 return sprintf(buf, "%pU\n", &rbd_dev->rbd_client->client->fsid);
4632}
4633
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02004634static ssize_t rbd_config_info_show(struct device *dev,
4635 struct device_attribute *attr, char *buf)
4636{
4637 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4638
4639 return sprintf(buf, "%s\n", rbd_dev->config_info);
4640}
4641
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004642static ssize_t rbd_pool_show(struct device *dev,
4643 struct device_attribute *attr, char *buf)
4644{
Alex Elder593a9e72012-02-07 12:03:37 -06004645 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004646
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004647 return sprintf(buf, "%s\n", rbd_dev->spec->pool_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004648}
4649
Alex Elder9bb2f332012-07-12 10:46:35 -05004650static ssize_t rbd_pool_id_show(struct device *dev,
4651 struct device_attribute *attr, char *buf)
4652{
4653 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4654
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004655 return sprintf(buf, "%llu\n",
Alex Elderfc71d832013-04-26 15:44:36 -05004656 (unsigned long long) rbd_dev->spec->pool_id);
Alex Elder9bb2f332012-07-12 10:46:35 -05004657}
4658
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004659static ssize_t rbd_name_show(struct device *dev,
4660 struct device_attribute *attr, char *buf)
4661{
Alex Elder593a9e72012-02-07 12:03:37 -06004662 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004663
Alex Eldera92ffdf2012-10-30 19:40:33 -05004664 if (rbd_dev->spec->image_name)
4665 return sprintf(buf, "%s\n", rbd_dev->spec->image_name);
4666
4667 return sprintf(buf, "(unknown)\n");
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004668}
4669
Alex Elder589d30e2012-07-10 20:30:11 -05004670static ssize_t rbd_image_id_show(struct device *dev,
4671 struct device_attribute *attr, char *buf)
4672{
4673 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4674
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004675 return sprintf(buf, "%s\n", rbd_dev->spec->image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05004676}
4677
Alex Elder34b13182012-07-13 20:35:12 -05004678/*
4679 * Shows the name of the currently-mapped snapshot (or
4680 * RBD_SNAP_HEAD_NAME for the base image).
4681 */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004682static ssize_t rbd_snap_show(struct device *dev,
4683 struct device_attribute *attr,
4684 char *buf)
4685{
Alex Elder593a9e72012-02-07 12:03:37 -06004686 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004687
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004688 return sprintf(buf, "%s\n", rbd_dev->spec->snap_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004689}
4690
Mike Christie92a58672016-08-18 18:38:44 +02004691static ssize_t rbd_snap_id_show(struct device *dev,
4692 struct device_attribute *attr, char *buf)
4693{
4694 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4695
4696 return sprintf(buf, "%llu\n", rbd_dev->spec->snap_id);
4697}
4698
Alex Elder86b00e02012-10-25 23:34:42 -05004699/*
Ilya Dryomovff961282014-07-22 21:53:07 +04004700 * For a v2 image, shows the chain of parent images, separated by empty
4701 * lines. For v1 images or if there is no parent, shows "(no parent
4702 * image)".
Alex Elder86b00e02012-10-25 23:34:42 -05004703 */
4704static ssize_t rbd_parent_show(struct device *dev,
Ilya Dryomovff961282014-07-22 21:53:07 +04004705 struct device_attribute *attr,
4706 char *buf)
Alex Elder86b00e02012-10-25 23:34:42 -05004707{
4708 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Ilya Dryomovff961282014-07-22 21:53:07 +04004709 ssize_t count = 0;
Alex Elder86b00e02012-10-25 23:34:42 -05004710
Ilya Dryomovff961282014-07-22 21:53:07 +04004711 if (!rbd_dev->parent)
Alex Elder86b00e02012-10-25 23:34:42 -05004712 return sprintf(buf, "(no parent image)\n");
4713
Ilya Dryomovff961282014-07-22 21:53:07 +04004714 for ( ; rbd_dev->parent; rbd_dev = rbd_dev->parent) {
4715 struct rbd_spec *spec = rbd_dev->parent_spec;
Alex Elder86b00e02012-10-25 23:34:42 -05004716
Ilya Dryomovff961282014-07-22 21:53:07 +04004717 count += sprintf(&buf[count], "%s"
4718 "pool_id %llu\npool_name %s\n"
4719 "image_id %s\nimage_name %s\n"
4720 "snap_id %llu\nsnap_name %s\n"
4721 "overlap %llu\n",
4722 !count ? "" : "\n", /* first? */
4723 spec->pool_id, spec->pool_name,
4724 spec->image_id, spec->image_name ?: "(unknown)",
4725 spec->snap_id, spec->snap_name,
4726 rbd_dev->parent_overlap);
4727 }
Alex Elder86b00e02012-10-25 23:34:42 -05004728
Ilya Dryomovff961282014-07-22 21:53:07 +04004729 return count;
Alex Elder86b00e02012-10-25 23:34:42 -05004730}
4731
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004732static ssize_t rbd_image_refresh(struct device *dev,
4733 struct device_attribute *attr,
4734 const char *buf,
4735 size_t size)
4736{
Alex Elder593a9e72012-02-07 12:03:37 -06004737 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Alex Elderb8136232012-07-25 09:32:41 -05004738 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004739
Alex Eldercc4a38bd2013-04-30 00:44:33 -05004740 ret = rbd_dev_refresh(rbd_dev);
Alex Eldere627db02013-05-06 07:40:30 -05004741 if (ret)
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04004742 return ret;
Alex Elderb8136232012-07-25 09:32:41 -05004743
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04004744 return size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004745}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004746
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004747static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
Alex Elder34b13182012-07-13 20:35:12 -05004748static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004749static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004750static DEVICE_ATTR(minor, S_IRUGO, rbd_minor_show, NULL);
Ilya Dryomov005a07bf2016-08-18 18:38:43 +02004751static DEVICE_ATTR(client_addr, S_IRUGO, rbd_client_addr_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004752static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
Mike Christie267fb902016-08-18 18:38:43 +02004753static DEVICE_ATTR(cluster_fsid, S_IRUGO, rbd_cluster_fsid_show, NULL);
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02004754static DEVICE_ATTR(config_info, S_IRUSR, rbd_config_info_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004755static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
Alex Elder9bb2f332012-07-12 10:46:35 -05004756static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004757static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
Alex Elder589d30e2012-07-10 20:30:11 -05004758static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004759static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
4760static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
Mike Christie92a58672016-08-18 18:38:44 +02004761static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL);
Alex Elder86b00e02012-10-25 23:34:42 -05004762static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004763
4764static struct attribute *rbd_attrs[] = {
4765 &dev_attr_size.attr,
Alex Elder34b13182012-07-13 20:35:12 -05004766 &dev_attr_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004767 &dev_attr_major.attr,
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004768 &dev_attr_minor.attr,
Ilya Dryomov005a07bf2016-08-18 18:38:43 +02004769 &dev_attr_client_addr.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004770 &dev_attr_client_id.attr,
Mike Christie267fb902016-08-18 18:38:43 +02004771 &dev_attr_cluster_fsid.attr,
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02004772 &dev_attr_config_info.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004773 &dev_attr_pool.attr,
Alex Elder9bb2f332012-07-12 10:46:35 -05004774 &dev_attr_pool_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004775 &dev_attr_name.attr,
Alex Elder589d30e2012-07-10 20:30:11 -05004776 &dev_attr_image_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004777 &dev_attr_current_snap.attr,
Mike Christie92a58672016-08-18 18:38:44 +02004778 &dev_attr_snap_id.attr,
Alex Elder86b00e02012-10-25 23:34:42 -05004779 &dev_attr_parent.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004780 &dev_attr_refresh.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004781 NULL
4782};
4783
4784static struct attribute_group rbd_attr_group = {
4785 .attrs = rbd_attrs,
4786};
4787
4788static const struct attribute_group *rbd_attr_groups[] = {
4789 &rbd_attr_group,
4790 NULL
4791};
4792
Ilya Dryomov6cac4692015-10-16 20:11:25 +02004793static void rbd_dev_release(struct device *dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004794
4795static struct device_type rbd_device_type = {
4796 .name = "rbd",
4797 .groups = rbd_attr_groups,
Ilya Dryomov6cac4692015-10-16 20:11:25 +02004798 .release = rbd_dev_release,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004799};
4800
Alex Elder8b8fb992012-10-26 17:25:24 -05004801static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)
4802{
4803 kref_get(&spec->kref);
4804
4805 return spec;
4806}
4807
4808static void rbd_spec_free(struct kref *kref);
4809static void rbd_spec_put(struct rbd_spec *spec)
4810{
4811 if (spec)
4812 kref_put(&spec->kref, rbd_spec_free);
4813}
4814
4815static struct rbd_spec *rbd_spec_alloc(void)
4816{
4817 struct rbd_spec *spec;
4818
4819 spec = kzalloc(sizeof (*spec), GFP_KERNEL);
4820 if (!spec)
4821 return NULL;
Ilya Dryomov04077592014-07-23 17:11:20 +04004822
4823 spec->pool_id = CEPH_NOPOOL;
4824 spec->snap_id = CEPH_NOSNAP;
Alex Elder8b8fb992012-10-26 17:25:24 -05004825 kref_init(&spec->kref);
4826
Alex Elder8b8fb992012-10-26 17:25:24 -05004827 return spec;
4828}
4829
4830static void rbd_spec_free(struct kref *kref)
4831{
4832 struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref);
4833
4834 kfree(spec->pool_name);
4835 kfree(spec->image_id);
4836 kfree(spec->image_name);
4837 kfree(spec->snap_name);
4838 kfree(spec);
4839}
4840
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004841static void rbd_dev_free(struct rbd_device *rbd_dev)
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004842{
Ilya Dryomov99d16942016-08-12 16:11:41 +02004843 WARN_ON(rbd_dev->watch_state != RBD_WATCH_STATE_UNREGISTERED);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004844 WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_UNLOCKED);
Ilya Dryomov99d16942016-08-12 16:11:41 +02004845
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004846 ceph_oid_destroy(&rbd_dev->header_oid);
Ilya Dryomov6b6dddb2016-08-05 16:15:38 +02004847 ceph_oloc_destroy(&rbd_dev->header_oloc);
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02004848 kfree(rbd_dev->config_info);
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004849
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004850 rbd_put_client(rbd_dev->rbd_client);
4851 rbd_spec_put(rbd_dev->spec);
4852 kfree(rbd_dev->opts);
4853 kfree(rbd_dev);
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004854}
4855
4856static void rbd_dev_release(struct device *dev)
4857{
4858 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4859 bool need_put = !!rbd_dev->opts;
4860
4861 if (need_put) {
4862 destroy_workqueue(rbd_dev->task_wq);
4863 ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id);
4864 }
4865
4866 rbd_dev_free(rbd_dev);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004867
4868 /*
4869 * This is racy, but way better than putting module outside of
4870 * the release callback. The race window is pretty small, so
4871 * doing something similar to dm (dm-builtin.c) is overkill.
4872 */
4873 if (need_put)
4874 module_put(THIS_MODULE);
4875}
4876
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004877static struct rbd_device *__rbd_dev_create(struct rbd_client *rbdc,
4878 struct rbd_spec *spec)
Alex Elderc53d5892012-10-25 23:34:42 -05004879{
4880 struct rbd_device *rbd_dev;
4881
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004882 rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL);
Alex Elderc53d5892012-10-25 23:34:42 -05004883 if (!rbd_dev)
4884 return NULL;
4885
4886 spin_lock_init(&rbd_dev->lock);
4887 INIT_LIST_HEAD(&rbd_dev->node);
Alex Elderc53d5892012-10-25 23:34:42 -05004888 init_rwsem(&rbd_dev->header_rwsem);
4889
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004890 ceph_oid_init(&rbd_dev->header_oid);
Ilya Dryomov922dab62016-05-26 01:15:02 +02004891 ceph_oloc_init(&rbd_dev->header_oloc);
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004892
Ilya Dryomov99d16942016-08-12 16:11:41 +02004893 mutex_init(&rbd_dev->watch_mutex);
4894 rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED;
4895 INIT_DELAYED_WORK(&rbd_dev->watch_dwork, rbd_reregister_watch);
4896
Ilya Dryomoved95b212016-08-12 16:40:02 +02004897 init_rwsem(&rbd_dev->lock_rwsem);
4898 rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED;
4899 INIT_WORK(&rbd_dev->acquired_lock_work, rbd_notify_acquired_lock);
4900 INIT_WORK(&rbd_dev->released_lock_work, rbd_notify_released_lock);
4901 INIT_DELAYED_WORK(&rbd_dev->lock_dwork, rbd_acquire_lock);
4902 INIT_WORK(&rbd_dev->unlock_work, rbd_release_lock_work);
4903 init_waitqueue_head(&rbd_dev->lock_waitq);
4904
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004905 rbd_dev->dev.bus = &rbd_bus_type;
4906 rbd_dev->dev.type = &rbd_device_type;
4907 rbd_dev->dev.parent = &rbd_root_dev;
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004908 device_initialize(&rbd_dev->dev);
4909
Alex Elderc53d5892012-10-25 23:34:42 -05004910 rbd_dev->rbd_client = rbdc;
Ilya Dryomovd1475432015-06-22 13:24:48 +03004911 rbd_dev->spec = spec;
Alex Elder0903e872012-11-14 12:25:19 -06004912
Yan, Zheng76271512016-02-03 21:24:49 +08004913 rbd_dev->layout.stripe_unit = 1 << RBD_MAX_OBJ_ORDER;
4914 rbd_dev->layout.stripe_count = 1;
4915 rbd_dev->layout.object_size = 1 << RBD_MAX_OBJ_ORDER;
4916 rbd_dev->layout.pool_id = spec->pool_id;
Yan, Zheng30c156d2016-02-14 11:24:31 +08004917 RCU_INIT_POINTER(rbd_dev->layout.pool_ns, NULL);
Alex Elder0903e872012-11-14 12:25:19 -06004918
Alex Elderc53d5892012-10-25 23:34:42 -05004919 return rbd_dev;
4920}
4921
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004922/*
4923 * Create a mapping rbd_dev.
4924 */
4925static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,
4926 struct rbd_spec *spec,
4927 struct rbd_options *opts)
4928{
4929 struct rbd_device *rbd_dev;
4930
4931 rbd_dev = __rbd_dev_create(rbdc, spec);
4932 if (!rbd_dev)
4933 return NULL;
4934
4935 rbd_dev->opts = opts;
4936
4937 /* get an id and fill in device name */
4938 rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0,
4939 minor_to_rbd_dev_id(1 << MINORBITS),
4940 GFP_KERNEL);
4941 if (rbd_dev->dev_id < 0)
4942 goto fail_rbd_dev;
4943
4944 sprintf(rbd_dev->name, RBD_DRV_NAME "%d", rbd_dev->dev_id);
4945 rbd_dev->task_wq = alloc_ordered_workqueue("%s-tasks", WQ_MEM_RECLAIM,
4946 rbd_dev->name);
4947 if (!rbd_dev->task_wq)
4948 goto fail_dev_id;
4949
4950 /* we have a ref from do_rbd_add() */
4951 __module_get(THIS_MODULE);
4952
4953 dout("%s rbd_dev %p dev_id %d\n", __func__, rbd_dev, rbd_dev->dev_id);
4954 return rbd_dev;
4955
4956fail_dev_id:
4957 ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id);
4958fail_rbd_dev:
4959 rbd_dev_free(rbd_dev);
4960 return NULL;
4961}
4962
Alex Elderc53d5892012-10-25 23:34:42 -05004963static void rbd_dev_destroy(struct rbd_device *rbd_dev)
4964{
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004965 if (rbd_dev)
4966 put_device(&rbd_dev->dev);
Alex Elderc53d5892012-10-25 23:34:42 -05004967}
4968
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004969/*
Alex Elder9d475de2012-07-03 16:01:19 -05004970 * Get the size and object order for an image snapshot, or if
4971 * snap_id is CEPH_NOSNAP, gets this information for the base
4972 * image.
4973 */
4974static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
4975 u8 *order, u64 *snap_size)
4976{
4977 __le64 snapid = cpu_to_le64(snap_id);
4978 int ret;
4979 struct {
4980 u8 order;
4981 __le64 size;
4982 } __attribute__ ((packed)) size_buf = { 0 };
4983
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004984 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name,
Alex Elder9d475de2012-07-03 16:01:19 -05004985 "rbd", "get_size",
Alex Elder41579762013-04-21 12:14:45 -05004986 &snapid, sizeof (snapid),
Alex Eldere2a58ee2013-04-30 00:44:33 -05004987 &size_buf, sizeof (size_buf));
Alex Elder36be9a72013-01-19 00:30:28 -06004988 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder9d475de2012-07-03 16:01:19 -05004989 if (ret < 0)
4990 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05004991 if (ret < sizeof (size_buf))
4992 return -ERANGE;
Alex Elder9d475de2012-07-03 16:01:19 -05004993
Josh Durginc3545572013-08-28 17:08:10 -07004994 if (order) {
Alex Elderc86f86e2013-04-25 15:09:41 -05004995 *order = size_buf.order;
Josh Durginc3545572013-08-28 17:08:10 -07004996 dout(" order %u", (unsigned int)*order);
4997 }
Alex Elder9d475de2012-07-03 16:01:19 -05004998 *snap_size = le64_to_cpu(size_buf.size);
4999
Josh Durginc3545572013-08-28 17:08:10 -07005000 dout(" snap_id 0x%016llx snap_size = %llu\n",
5001 (unsigned long long)snap_id,
Alex Elder57385b52013-04-21 12:14:45 -05005002 (unsigned long long)*snap_size);
Alex Elder9d475de2012-07-03 16:01:19 -05005003
5004 return 0;
5005}
5006
5007static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
5008{
5009 return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
5010 &rbd_dev->header.obj_order,
5011 &rbd_dev->header.image_size);
5012}
5013
Alex Elder1e130192012-07-03 16:01:19 -05005014static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
5015{
5016 void *reply_buf;
5017 int ret;
5018 void *p;
5019
5020 reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL);
5021 if (!reply_buf)
5022 return -ENOMEM;
5023
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005024 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name,
Alex Elder41579762013-04-21 12:14:45 -05005025 "rbd", "get_object_prefix", NULL, 0,
Alex Eldere2a58ee2013-04-30 00:44:33 -05005026 reply_buf, RBD_OBJ_PREFIX_LEN_MAX);
Alex Elder36be9a72013-01-19 00:30:28 -06005027 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder1e130192012-07-03 16:01:19 -05005028 if (ret < 0)
5029 goto out;
5030
5031 p = reply_buf;
5032 rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p,
Alex Elder57385b52013-04-21 12:14:45 -05005033 p + ret, NULL, GFP_NOIO);
5034 ret = 0;
Alex Elder1e130192012-07-03 16:01:19 -05005035
5036 if (IS_ERR(rbd_dev->header.object_prefix)) {
5037 ret = PTR_ERR(rbd_dev->header.object_prefix);
5038 rbd_dev->header.object_prefix = NULL;
5039 } else {
5040 dout(" object_prefix = %s\n", rbd_dev->header.object_prefix);
5041 }
Alex Elder1e130192012-07-03 16:01:19 -05005042out:
5043 kfree(reply_buf);
5044
5045 return ret;
5046}
5047
Alex Elderb1b54022012-07-03 16:01:19 -05005048static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
5049 u64 *snap_features)
5050{
5051 __le64 snapid = cpu_to_le64(snap_id);
5052 struct {
5053 __le64 features;
5054 __le64 incompat;
Alex Elder41579762013-04-21 12:14:45 -05005055 } __attribute__ ((packed)) features_buf = { 0 };
Ilya Dryomovd3767f02016-04-13 14:15:50 +02005056 u64 unsup;
Alex Elderb1b54022012-07-03 16:01:19 -05005057 int ret;
5058
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005059 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name,
Alex Elderb1b54022012-07-03 16:01:19 -05005060 "rbd", "get_features",
Alex Elder41579762013-04-21 12:14:45 -05005061 &snapid, sizeof (snapid),
Alex Eldere2a58ee2013-04-30 00:44:33 -05005062 &features_buf, sizeof (features_buf));
Alex Elder36be9a72013-01-19 00:30:28 -06005063 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderb1b54022012-07-03 16:01:19 -05005064 if (ret < 0)
5065 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05005066 if (ret < sizeof (features_buf))
5067 return -ERANGE;
Alex Elderd8891402012-10-09 13:50:17 -07005068
Ilya Dryomovd3767f02016-04-13 14:15:50 +02005069 unsup = le64_to_cpu(features_buf.incompat) & ~RBD_FEATURES_SUPPORTED;
5070 if (unsup) {
5071 rbd_warn(rbd_dev, "image uses unsupported features: 0x%llx",
5072 unsup);
Alex Elderb8f5c6e2012-11-01 08:39:26 -05005073 return -ENXIO;
Ilya Dryomovd3767f02016-04-13 14:15:50 +02005074 }
Alex Elderd8891402012-10-09 13:50:17 -07005075
Alex Elderb1b54022012-07-03 16:01:19 -05005076 *snap_features = le64_to_cpu(features_buf.features);
5077
5078 dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
Alex Elder57385b52013-04-21 12:14:45 -05005079 (unsigned long long)snap_id,
5080 (unsigned long long)*snap_features,
5081 (unsigned long long)le64_to_cpu(features_buf.incompat));
Alex Elderb1b54022012-07-03 16:01:19 -05005082
5083 return 0;
5084}
5085
5086static int rbd_dev_v2_features(struct rbd_device *rbd_dev)
5087{
5088 return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP,
5089 &rbd_dev->header.features);
5090}
5091
Alex Elder86b00e02012-10-25 23:34:42 -05005092static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev)
5093{
5094 struct rbd_spec *parent_spec;
5095 size_t size;
5096 void *reply_buf = NULL;
5097 __le64 snapid;
5098 void *p;
5099 void *end;
Alex Elder642a2532013-05-06 17:40:33 -05005100 u64 pool_id;
Alex Elder86b00e02012-10-25 23:34:42 -05005101 char *image_id;
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005102 u64 snap_id;
Alex Elder86b00e02012-10-25 23:34:42 -05005103 u64 overlap;
Alex Elder86b00e02012-10-25 23:34:42 -05005104 int ret;
5105
5106 parent_spec = rbd_spec_alloc();
5107 if (!parent_spec)
5108 return -ENOMEM;
5109
5110 size = sizeof (__le64) + /* pool_id */
5111 sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */
5112 sizeof (__le64) + /* snap_id */
5113 sizeof (__le64); /* overlap */
5114 reply_buf = kmalloc(size, GFP_KERNEL);
5115 if (!reply_buf) {
5116 ret = -ENOMEM;
5117 goto out_err;
5118 }
5119
Ilya Dryomov4d9b67c2014-07-24 10:42:13 +04005120 snapid = cpu_to_le64(rbd_dev->spec->snap_id);
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005121 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name,
Alex Elder86b00e02012-10-25 23:34:42 -05005122 "rbd", "get_parent",
Alex Elder41579762013-04-21 12:14:45 -05005123 &snapid, sizeof (snapid),
Alex Eldere2a58ee2013-04-30 00:44:33 -05005124 reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06005125 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder86b00e02012-10-25 23:34:42 -05005126 if (ret < 0)
5127 goto out_err;
5128
Alex Elder86b00e02012-10-25 23:34:42 -05005129 p = reply_buf;
Alex Elder57385b52013-04-21 12:14:45 -05005130 end = reply_buf + ret;
5131 ret = -ERANGE;
Alex Elder642a2532013-05-06 17:40:33 -05005132 ceph_decode_64_safe(&p, end, pool_id, out_err);
Alex Elder392a9da2013-05-06 17:40:33 -05005133 if (pool_id == CEPH_NOPOOL) {
5134 /*
5135 * Either the parent never existed, or we have
5136 * record of it but the image got flattened so it no
5137 * longer has a parent. When the parent of a
5138 * layered image disappears we immediately set the
5139 * overlap to 0. The effect of this is that all new
5140 * requests will be treated as if the image had no
5141 * parent.
5142 */
5143 if (rbd_dev->parent_overlap) {
5144 rbd_dev->parent_overlap = 0;
Alex Elder392a9da2013-05-06 17:40:33 -05005145 rbd_dev_parent_put(rbd_dev);
5146 pr_info("%s: clone image has been flattened\n",
5147 rbd_dev->disk->disk_name);
5148 }
5149
Alex Elder86b00e02012-10-25 23:34:42 -05005150 goto out; /* No parent? No problem. */
Alex Elder392a9da2013-05-06 17:40:33 -05005151 }
Alex Elder86b00e02012-10-25 23:34:42 -05005152
Alex Elder0903e872012-11-14 12:25:19 -06005153 /* The ceph file layout needs to fit pool id in 32 bits */
5154
5155 ret = -EIO;
Alex Elder642a2532013-05-06 17:40:33 -05005156 if (pool_id > (u64)U32_MAX) {
Ilya Dryomov9584d502014-07-11 12:11:20 +04005157 rbd_warn(NULL, "parent pool id too large (%llu > %u)",
Alex Elder642a2532013-05-06 17:40:33 -05005158 (unsigned long long)pool_id, U32_MAX);
Alex Elder57385b52013-04-21 12:14:45 -05005159 goto out_err;
Alex Elderc0cd10db2013-04-26 09:43:47 -05005160 }
Alex Elder0903e872012-11-14 12:25:19 -06005161
Alex Elder979ed482012-11-01 08:39:26 -05005162 image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elder86b00e02012-10-25 23:34:42 -05005163 if (IS_ERR(image_id)) {
5164 ret = PTR_ERR(image_id);
5165 goto out_err;
5166 }
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005167 ceph_decode_64_safe(&p, end, snap_id, out_err);
Alex Elder86b00e02012-10-25 23:34:42 -05005168 ceph_decode_64_safe(&p, end, overlap, out_err);
5169
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005170 /*
5171 * The parent won't change (except when the clone is
5172 * flattened, already handled that). So we only need to
5173 * record the parent spec we have not already done so.
5174 */
5175 if (!rbd_dev->parent_spec) {
5176 parent_spec->pool_id = pool_id;
5177 parent_spec->image_id = image_id;
5178 parent_spec->snap_id = snap_id;
Alex Elder70cf49c2013-05-06 17:40:33 -05005179 rbd_dev->parent_spec = parent_spec;
5180 parent_spec = NULL; /* rbd_dev now owns this */
Ilya Dryomovfbba11b2014-06-27 21:46:33 +04005181 } else {
5182 kfree(image_id);
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005183 }
5184
5185 /*
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005186 * We always update the parent overlap. If it's zero we issue
5187 * a warning, as we will proceed as if there was no parent.
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005188 */
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005189 if (!overlap) {
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005190 if (parent_spec) {
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005191 /* refresh, careful to warn just once */
5192 if (rbd_dev->parent_overlap)
5193 rbd_warn(rbd_dev,
5194 "clone now standalone (overlap became 0)");
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005195 } else {
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005196 /* initial probe */
5197 rbd_warn(rbd_dev, "clone is standalone (overlap 0)");
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005198 }
Alex Elder70cf49c2013-05-06 17:40:33 -05005199 }
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005200 rbd_dev->parent_overlap = overlap;
5201
Alex Elder86b00e02012-10-25 23:34:42 -05005202out:
5203 ret = 0;
5204out_err:
5205 kfree(reply_buf);
5206 rbd_spec_put(parent_spec);
5207
5208 return ret;
5209}
5210
Alex Eldercc070d52013-04-21 12:14:45 -05005211static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev)
5212{
5213 struct {
5214 __le64 stripe_unit;
5215 __le64 stripe_count;
5216 } __attribute__ ((packed)) striping_info_buf = { 0 };
5217 size_t size = sizeof (striping_info_buf);
5218 void *p;
5219 u64 obj_size;
5220 u64 stripe_unit;
5221 u64 stripe_count;
5222 int ret;
5223
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005224 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name,
Alex Eldercc070d52013-04-21 12:14:45 -05005225 "rbd", "get_stripe_unit_count", NULL, 0,
Alex Eldere2a58ee2013-04-30 00:44:33 -05005226 (char *)&striping_info_buf, size);
Alex Eldercc070d52013-04-21 12:14:45 -05005227 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
5228 if (ret < 0)
5229 return ret;
5230 if (ret < size)
5231 return -ERANGE;
5232
5233 /*
5234 * We don't actually support the "fancy striping" feature
5235 * (STRIPINGV2) yet, but if the striping sizes are the
5236 * defaults the behavior is the same as before. So find
5237 * out, and only fail if the image has non-default values.
5238 */
5239 ret = -EINVAL;
5240 obj_size = (u64)1 << rbd_dev->header.obj_order;
5241 p = &striping_info_buf;
5242 stripe_unit = ceph_decode_64(&p);
5243 if (stripe_unit != obj_size) {
5244 rbd_warn(rbd_dev, "unsupported stripe unit "
5245 "(got %llu want %llu)",
5246 stripe_unit, obj_size);
5247 return -EINVAL;
5248 }
5249 stripe_count = ceph_decode_64(&p);
5250 if (stripe_count != 1) {
5251 rbd_warn(rbd_dev, "unsupported stripe count "
5252 "(got %llu want 1)", stripe_count);
5253 return -EINVAL;
5254 }
Alex Elder500d0c02013-04-26 09:43:47 -05005255 rbd_dev->header.stripe_unit = stripe_unit;
5256 rbd_dev->header.stripe_count = stripe_count;
Alex Eldercc070d52013-04-21 12:14:45 -05005257
5258 return 0;
5259}
5260
Alex Elder9e15b772012-10-30 19:40:33 -05005261static char *rbd_dev_image_name(struct rbd_device *rbd_dev)
5262{
5263 size_t image_id_size;
5264 char *image_id;
5265 void *p;
5266 void *end;
5267 size_t size;
5268 void *reply_buf = NULL;
5269 size_t len = 0;
5270 char *image_name = NULL;
5271 int ret;
5272
5273 rbd_assert(!rbd_dev->spec->image_name);
5274
Alex Elder69e7a022012-11-01 08:39:26 -05005275 len = strlen(rbd_dev->spec->image_id);
5276 image_id_size = sizeof (__le32) + len;
Alex Elder9e15b772012-10-30 19:40:33 -05005277 image_id = kmalloc(image_id_size, GFP_KERNEL);
5278 if (!image_id)
5279 return NULL;
5280
5281 p = image_id;
Alex Elder41579762013-04-21 12:14:45 -05005282 end = image_id + image_id_size;
Alex Elder57385b52013-04-21 12:14:45 -05005283 ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len);
Alex Elder9e15b772012-10-30 19:40:33 -05005284
5285 size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX;
5286 reply_buf = kmalloc(size, GFP_KERNEL);
5287 if (!reply_buf)
5288 goto out;
5289
Alex Elder36be9a72013-01-19 00:30:28 -06005290 ret = rbd_obj_method_sync(rbd_dev, RBD_DIRECTORY,
Alex Elder9e15b772012-10-30 19:40:33 -05005291 "rbd", "dir_get_name",
5292 image_id, image_id_size,
Alex Eldere2a58ee2013-04-30 00:44:33 -05005293 reply_buf, size);
Alex Elder9e15b772012-10-30 19:40:33 -05005294 if (ret < 0)
5295 goto out;
5296 p = reply_buf;
Alex Elderf40eb342013-04-25 15:09:42 -05005297 end = reply_buf + ret;
5298
Alex Elder9e15b772012-10-30 19:40:33 -05005299 image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL);
5300 if (IS_ERR(image_name))
5301 image_name = NULL;
5302 else
5303 dout("%s: name is %s len is %zd\n", __func__, image_name, len);
5304out:
5305 kfree(reply_buf);
5306 kfree(image_id);
5307
5308 return image_name;
5309}
5310
Alex Elder2ad3d712013-04-30 00:44:33 -05005311static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
5312{
5313 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
5314 const char *snap_name;
5315 u32 which = 0;
5316
5317 /* Skip over names until we find the one we are looking for */
5318
5319 snap_name = rbd_dev->header.snap_names;
5320 while (which < snapc->num_snaps) {
5321 if (!strcmp(name, snap_name))
5322 return snapc->snaps[which];
5323 snap_name += strlen(snap_name) + 1;
5324 which++;
5325 }
5326 return CEPH_NOSNAP;
5327}
5328
5329static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
5330{
5331 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
5332 u32 which;
5333 bool found = false;
5334 u64 snap_id;
5335
5336 for (which = 0; !found && which < snapc->num_snaps; which++) {
5337 const char *snap_name;
5338
5339 snap_id = snapc->snaps[which];
5340 snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id);
Josh Durginefadc982013-08-29 19:16:42 -07005341 if (IS_ERR(snap_name)) {
5342 /* ignore no-longer existing snapshots */
5343 if (PTR_ERR(snap_name) == -ENOENT)
5344 continue;
5345 else
5346 break;
5347 }
Alex Elder2ad3d712013-04-30 00:44:33 -05005348 found = !strcmp(name, snap_name);
5349 kfree(snap_name);
5350 }
5351 return found ? snap_id : CEPH_NOSNAP;
5352}
5353
5354/*
5355 * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if
5356 * no snapshot by that name is found, or if an error occurs.
5357 */
5358static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
5359{
5360 if (rbd_dev->image_format == 1)
5361 return rbd_v1_snap_id_by_name(rbd_dev, name);
5362
5363 return rbd_v2_snap_id_by_name(rbd_dev, name);
5364}
5365
Alex Elder9e15b772012-10-30 19:40:33 -05005366/*
Ilya Dryomov04077592014-07-23 17:11:20 +04005367 * An image being mapped will have everything but the snap id.
Alex Elder9e15b772012-10-30 19:40:33 -05005368 */
Ilya Dryomov04077592014-07-23 17:11:20 +04005369static int rbd_spec_fill_snap_id(struct rbd_device *rbd_dev)
5370{
5371 struct rbd_spec *spec = rbd_dev->spec;
5372
5373 rbd_assert(spec->pool_id != CEPH_NOPOOL && spec->pool_name);
5374 rbd_assert(spec->image_id && spec->image_name);
5375 rbd_assert(spec->snap_name);
5376
5377 if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) {
5378 u64 snap_id;
5379
5380 snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name);
5381 if (snap_id == CEPH_NOSNAP)
5382 return -ENOENT;
5383
5384 spec->snap_id = snap_id;
5385 } else {
5386 spec->snap_id = CEPH_NOSNAP;
5387 }
5388
5389 return 0;
5390}
5391
5392/*
5393 * A parent image will have all ids but none of the names.
5394 *
5395 * All names in an rbd spec are dynamically allocated. It's OK if we
5396 * can't figure out the name for an image id.
5397 */
5398static int rbd_spec_fill_names(struct rbd_device *rbd_dev)
Alex Elder9e15b772012-10-30 19:40:33 -05005399{
Alex Elder2e9f7f12013-04-26 09:43:48 -05005400 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
5401 struct rbd_spec *spec = rbd_dev->spec;
5402 const char *pool_name;
5403 const char *image_name;
5404 const char *snap_name;
Alex Elder9e15b772012-10-30 19:40:33 -05005405 int ret;
5406
Ilya Dryomov04077592014-07-23 17:11:20 +04005407 rbd_assert(spec->pool_id != CEPH_NOPOOL);
5408 rbd_assert(spec->image_id);
5409 rbd_assert(spec->snap_id != CEPH_NOSNAP);
Alex Elder9e15b772012-10-30 19:40:33 -05005410
Alex Elder2e9f7f12013-04-26 09:43:48 -05005411 /* Get the pool name; we have to make our own copy of this */
Alex Elder9e15b772012-10-30 19:40:33 -05005412
Alex Elder2e9f7f12013-04-26 09:43:48 -05005413 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id);
5414 if (!pool_name) {
5415 rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id);
Alex Elder935dc892012-11-01 10:17:15 -05005416 return -EIO;
5417 }
Alex Elder2e9f7f12013-04-26 09:43:48 -05005418 pool_name = kstrdup(pool_name, GFP_KERNEL);
5419 if (!pool_name)
Alex Elder9e15b772012-10-30 19:40:33 -05005420 return -ENOMEM;
5421
5422 /* Fetch the image name; tolerate failure here */
5423
Alex Elder2e9f7f12013-04-26 09:43:48 -05005424 image_name = rbd_dev_image_name(rbd_dev);
5425 if (!image_name)
Alex Elder06ecc6c2012-11-01 10:17:15 -05005426 rbd_warn(rbd_dev, "unable to get image name");
Alex Elder9e15b772012-10-30 19:40:33 -05005427
Ilya Dryomov04077592014-07-23 17:11:20 +04005428 /* Fetch the snapshot name */
Alex Elder9e15b772012-10-30 19:40:33 -05005429
Alex Elder2e9f7f12013-04-26 09:43:48 -05005430 snap_name = rbd_snap_name(rbd_dev, spec->snap_id);
Josh Durginda6a6b62013-09-04 17:57:31 -07005431 if (IS_ERR(snap_name)) {
5432 ret = PTR_ERR(snap_name);
Alex Elder9e15b772012-10-30 19:40:33 -05005433 goto out_err;
Alex Elder2e9f7f12013-04-26 09:43:48 -05005434 }
5435
5436 spec->pool_name = pool_name;
5437 spec->image_name = image_name;
5438 spec->snap_name = snap_name;
Alex Elder9e15b772012-10-30 19:40:33 -05005439
5440 return 0;
Ilya Dryomov04077592014-07-23 17:11:20 +04005441
Alex Elder9e15b772012-10-30 19:40:33 -05005442out_err:
Alex Elder2e9f7f12013-04-26 09:43:48 -05005443 kfree(image_name);
5444 kfree(pool_name);
Alex Elder9e15b772012-10-30 19:40:33 -05005445 return ret;
5446}
5447
Alex Eldercc4a38bd2013-04-30 00:44:33 -05005448static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev)
Alex Elder35d489f2012-07-03 16:01:19 -05005449{
5450 size_t size;
5451 int ret;
5452 void *reply_buf;
5453 void *p;
5454 void *end;
5455 u64 seq;
5456 u32 snap_count;
5457 struct ceph_snap_context *snapc;
5458 u32 i;
5459
5460 /*
5461 * We'll need room for the seq value (maximum snapshot id),
5462 * snapshot count, and array of that many snapshot ids.
5463 * For now we have a fixed upper limit on the number we're
5464 * prepared to receive.
5465 */
5466 size = sizeof (__le64) + sizeof (__le32) +
5467 RBD_MAX_SNAP_COUNT * sizeof (__le64);
5468 reply_buf = kzalloc(size, GFP_KERNEL);
5469 if (!reply_buf)
5470 return -ENOMEM;
5471
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005472 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name,
Alex Elder41579762013-04-21 12:14:45 -05005473 "rbd", "get_snapcontext", NULL, 0,
Alex Eldere2a58ee2013-04-30 00:44:33 -05005474 reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06005475 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder35d489f2012-07-03 16:01:19 -05005476 if (ret < 0)
5477 goto out;
5478
Alex Elder35d489f2012-07-03 16:01:19 -05005479 p = reply_buf;
Alex Elder57385b52013-04-21 12:14:45 -05005480 end = reply_buf + ret;
5481 ret = -ERANGE;
Alex Elder35d489f2012-07-03 16:01:19 -05005482 ceph_decode_64_safe(&p, end, seq, out);
5483 ceph_decode_32_safe(&p, end, snap_count, out);
5484
5485 /*
5486 * Make sure the reported number of snapshot ids wouldn't go
5487 * beyond the end of our buffer. But before checking that,
5488 * make sure the computed size of the snapshot context we
5489 * allocate is representable in a size_t.
5490 */
5491 if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
5492 / sizeof (u64)) {
5493 ret = -EINVAL;
5494 goto out;
5495 }
5496 if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
5497 goto out;
Alex Elder468521c2013-04-26 09:43:47 -05005498 ret = 0;
Alex Elder35d489f2012-07-03 16:01:19 -05005499
Alex Elder812164f82013-04-30 00:44:32 -05005500 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
Alex Elder35d489f2012-07-03 16:01:19 -05005501 if (!snapc) {
5502 ret = -ENOMEM;
5503 goto out;
5504 }
Alex Elder35d489f2012-07-03 16:01:19 -05005505 snapc->seq = seq;
Alex Elder35d489f2012-07-03 16:01:19 -05005506 for (i = 0; i < snap_count; i++)
5507 snapc->snaps[i] = ceph_decode_64(&p);
5508
Alex Elder49ece552013-05-06 08:37:00 -05005509 ceph_put_snap_context(rbd_dev->header.snapc);
Alex Elder35d489f2012-07-03 16:01:19 -05005510 rbd_dev->header.snapc = snapc;
5511
5512 dout(" snap context seq = %llu, snap_count = %u\n",
Alex Elder57385b52013-04-21 12:14:45 -05005513 (unsigned long long)seq, (unsigned int)snap_count);
Alex Elder35d489f2012-07-03 16:01:19 -05005514out:
5515 kfree(reply_buf);
5516
Alex Elder57385b52013-04-21 12:14:45 -05005517 return ret;
Alex Elder35d489f2012-07-03 16:01:19 -05005518}
5519
Alex Elder54cac612013-04-30 00:44:33 -05005520static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
5521 u64 snap_id)
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005522{
5523 size_t size;
5524 void *reply_buf;
Alex Elder54cac612013-04-30 00:44:33 -05005525 __le64 snapid;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005526 int ret;
5527 void *p;
5528 void *end;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005529 char *snap_name;
5530
5531 size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN;
5532 reply_buf = kmalloc(size, GFP_KERNEL);
5533 if (!reply_buf)
5534 return ERR_PTR(-ENOMEM);
5535
Alex Elder54cac612013-04-30 00:44:33 -05005536 snapid = cpu_to_le64(snap_id);
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005537 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name,
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005538 "rbd", "get_snapshot_name",
Alex Elder54cac612013-04-30 00:44:33 -05005539 &snapid, sizeof (snapid),
Alex Eldere2a58ee2013-04-30 00:44:33 -05005540 reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06005541 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderf40eb342013-04-25 15:09:42 -05005542 if (ret < 0) {
5543 snap_name = ERR_PTR(ret);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005544 goto out;
Alex Elderf40eb342013-04-25 15:09:42 -05005545 }
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005546
5547 p = reply_buf;
Alex Elderf40eb342013-04-25 15:09:42 -05005548 end = reply_buf + ret;
Alex Eldere5c35532012-10-25 23:34:41 -05005549 snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elderf40eb342013-04-25 15:09:42 -05005550 if (IS_ERR(snap_name))
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005551 goto out;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005552
Alex Elderf40eb342013-04-25 15:09:42 -05005553 dout(" snap_id 0x%016llx snap_name = %s\n",
Alex Elder54cac612013-04-30 00:44:33 -05005554 (unsigned long long)snap_id, snap_name);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005555out:
5556 kfree(reply_buf);
5557
Alex Elderf40eb342013-04-25 15:09:42 -05005558 return snap_name;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005559}
5560
Alex Elder2df3fac2013-05-06 09:51:30 -05005561static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev)
Alex Elder117973f2012-08-31 17:29:55 -05005562{
Alex Elder2df3fac2013-05-06 09:51:30 -05005563 bool first_time = rbd_dev->header.object_prefix == NULL;
Alex Elder117973f2012-08-31 17:29:55 -05005564 int ret;
Alex Elder117973f2012-08-31 17:29:55 -05005565
Josh Durgin1617e402013-06-12 14:43:10 -07005566 ret = rbd_dev_v2_image_size(rbd_dev);
5567 if (ret)
Alex Eldercfbf6372013-05-31 17:40:45 -05005568 return ret;
Josh Durgin1617e402013-06-12 14:43:10 -07005569
Alex Elder2df3fac2013-05-06 09:51:30 -05005570 if (first_time) {
5571 ret = rbd_dev_v2_header_onetime(rbd_dev);
5572 if (ret)
Alex Eldercfbf6372013-05-31 17:40:45 -05005573 return ret;
Alex Elder2df3fac2013-05-06 09:51:30 -05005574 }
5575
Alex Eldercc4a38bd2013-04-30 00:44:33 -05005576 ret = rbd_dev_v2_snap_context(rbd_dev);
Ilya Dryomovd194cd12015-08-31 18:22:10 +03005577 if (ret && first_time) {
5578 kfree(rbd_dev->header.object_prefix);
5579 rbd_dev->header.object_prefix = NULL;
5580 }
Alex Elder117973f2012-08-31 17:29:55 -05005581
5582 return ret;
5583}
5584
Ilya Dryomova720ae02014-07-23 17:11:19 +04005585static int rbd_dev_header_info(struct rbd_device *rbd_dev)
5586{
5587 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
5588
5589 if (rbd_dev->image_format == 1)
5590 return rbd_dev_v1_header_info(rbd_dev);
5591
5592 return rbd_dev_v2_header_info(rbd_dev);
5593}
5594
Alex Elder1ddbe942012-01-29 13:57:44 -06005595/*
Alex Eldere28fff262012-02-02 08:13:30 -06005596 * Skips over white space at *buf, and updates *buf to point to the
5597 * first found non-space character (if any). Returns the length of
Alex Elder593a9e72012-02-07 12:03:37 -06005598 * the token (string of non-white space characters) found. Note
5599 * that *buf must be terminated with '\0'.
Alex Eldere28fff262012-02-02 08:13:30 -06005600 */
5601static inline size_t next_token(const char **buf)
5602{
5603 /*
5604 * These are the characters that produce nonzero for
5605 * isspace() in the "C" and "POSIX" locales.
5606 */
5607 const char *spaces = " \f\n\r\t\v";
5608
5609 *buf += strspn(*buf, spaces); /* Find start of token */
5610
5611 return strcspn(*buf, spaces); /* Return token length */
5612}
5613
5614/*
Alex Elderea3352f2012-07-09 21:04:23 -05005615 * Finds the next token in *buf, dynamically allocates a buffer big
5616 * enough to hold a copy of it, and copies the token into the new
5617 * buffer. The copy is guaranteed to be terminated with '\0'. Note
5618 * that a duplicate buffer is created even for a zero-length token.
5619 *
5620 * Returns a pointer to the newly-allocated duplicate, or a null
5621 * pointer if memory for the duplicate was not available. If
5622 * the lenp argument is a non-null pointer, the length of the token
5623 * (not including the '\0') is returned in *lenp.
5624 *
5625 * If successful, the *buf pointer will be updated to point beyond
5626 * the end of the found token.
5627 *
5628 * Note: uses GFP_KERNEL for allocation.
5629 */
5630static inline char *dup_token(const char **buf, size_t *lenp)
5631{
5632 char *dup;
5633 size_t len;
5634
5635 len = next_token(buf);
Alex Elder4caf35f2012-11-01 08:39:27 -05005636 dup = kmemdup(*buf, len + 1, GFP_KERNEL);
Alex Elderea3352f2012-07-09 21:04:23 -05005637 if (!dup)
5638 return NULL;
Alex Elderea3352f2012-07-09 21:04:23 -05005639 *(dup + len) = '\0';
5640 *buf += len;
5641
5642 if (lenp)
5643 *lenp = len;
5644
5645 return dup;
5646}
5647
5648/*
Alex Elder859c31d2012-10-25 23:34:42 -05005649 * Parse the options provided for an "rbd add" (i.e., rbd image
5650 * mapping) request. These arrive via a write to /sys/bus/rbd/add,
5651 * and the data written is passed here via a NUL-terminated buffer.
5652 * Returns 0 if successful or an error code otherwise.
Alex Elderd22f76e2012-07-12 10:46:35 -05005653 *
Alex Elder859c31d2012-10-25 23:34:42 -05005654 * The information extracted from these options is recorded in
5655 * the other parameters which return dynamically-allocated
5656 * structures:
5657 * ceph_opts
5658 * The address of a pointer that will refer to a ceph options
5659 * structure. Caller must release the returned pointer using
5660 * ceph_destroy_options() when it is no longer needed.
5661 * rbd_opts
5662 * Address of an rbd options pointer. Fully initialized by
5663 * this function; caller must release with kfree().
5664 * spec
5665 * Address of an rbd image specification pointer. Fully
5666 * initialized by this function based on parsed options.
5667 * Caller must release with rbd_spec_put().
5668 *
5669 * The options passed take this form:
5670 * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
5671 * where:
5672 * <mon_addrs>
5673 * A comma-separated list of one or more monitor addresses.
5674 * A monitor address is an ip address, optionally followed
5675 * by a port number (separated by a colon).
5676 * I.e.: ip1[:port1][,ip2[:port2]...]
5677 * <options>
5678 * A comma-separated list of ceph and/or rbd options.
5679 * <pool_name>
5680 * The name of the rados pool containing the rbd image.
5681 * <image_name>
5682 * The name of the image in that pool to map.
5683 * <snap_id>
5684 * An optional snapshot id. If provided, the mapping will
5685 * present data from the image at the time that snapshot was
5686 * created. The image head is used if no snapshot id is
5687 * provided. Snapshot mappings are always read-only.
Alex Eldera725f65e2012-02-02 08:13:30 -06005688 */
Alex Elder859c31d2012-10-25 23:34:42 -05005689static int rbd_add_parse_args(const char *buf,
Alex Elderdc79b112012-10-25 23:34:41 -05005690 struct ceph_options **ceph_opts,
Alex Elder859c31d2012-10-25 23:34:42 -05005691 struct rbd_options **opts,
5692 struct rbd_spec **rbd_spec)
Alex Eldera725f65e2012-02-02 08:13:30 -06005693{
Alex Elderd22f76e2012-07-12 10:46:35 -05005694 size_t len;
Alex Elder859c31d2012-10-25 23:34:42 -05005695 char *options;
Alex Elder0ddebc02012-10-25 23:34:41 -05005696 const char *mon_addrs;
Alex Elderecb4dc22013-04-26 09:43:47 -05005697 char *snap_name;
Alex Elder0ddebc02012-10-25 23:34:41 -05005698 size_t mon_addrs_size;
Alex Elder859c31d2012-10-25 23:34:42 -05005699 struct rbd_spec *spec = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05005700 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05005701 struct ceph_options *copts;
Alex Elderdc79b112012-10-25 23:34:41 -05005702 int ret;
Alex Eldere28fff262012-02-02 08:13:30 -06005703
5704 /* The first four tokens are required */
5705
Alex Elder7ef32142012-02-02 08:13:30 -06005706 len = next_token(&buf);
Alex Elder4fb5d6712012-11-01 10:17:15 -05005707 if (!len) {
5708 rbd_warn(NULL, "no monitor address(es) provided");
5709 return -EINVAL;
5710 }
Alex Elder0ddebc02012-10-25 23:34:41 -05005711 mon_addrs = buf;
Alex Elderf28e5652012-10-25 23:34:41 -05005712 mon_addrs_size = len + 1;
Alex Elder7ef32142012-02-02 08:13:30 -06005713 buf += len;
Alex Eldera725f65e2012-02-02 08:13:30 -06005714
Alex Elderdc79b112012-10-25 23:34:41 -05005715 ret = -EINVAL;
Alex Elderf28e5652012-10-25 23:34:41 -05005716 options = dup_token(&buf, NULL);
5717 if (!options)
Alex Elderdc79b112012-10-25 23:34:41 -05005718 return -ENOMEM;
Alex Elder4fb5d6712012-11-01 10:17:15 -05005719 if (!*options) {
5720 rbd_warn(NULL, "no options provided");
5721 goto out_err;
5722 }
Alex Eldera725f65e2012-02-02 08:13:30 -06005723
Alex Elder859c31d2012-10-25 23:34:42 -05005724 spec = rbd_spec_alloc();
5725 if (!spec)
Alex Elderf28e5652012-10-25 23:34:41 -05005726 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05005727
5728 spec->pool_name = dup_token(&buf, NULL);
5729 if (!spec->pool_name)
5730 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05005731 if (!*spec->pool_name) {
5732 rbd_warn(NULL, "no pool name provided");
5733 goto out_err;
5734 }
Alex Eldere28fff262012-02-02 08:13:30 -06005735
Alex Elder69e7a022012-11-01 08:39:26 -05005736 spec->image_name = dup_token(&buf, NULL);
Alex Elder859c31d2012-10-25 23:34:42 -05005737 if (!spec->image_name)
Alex Elderf28e5652012-10-25 23:34:41 -05005738 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05005739 if (!*spec->image_name) {
5740 rbd_warn(NULL, "no image name provided");
5741 goto out_err;
5742 }
Alex Eldere28fff262012-02-02 08:13:30 -06005743
Alex Elderf28e5652012-10-25 23:34:41 -05005744 /*
5745 * Snapshot name is optional; default is to use "-"
5746 * (indicating the head/no snapshot).
5747 */
Alex Elder3feeb8942012-08-31 17:29:52 -05005748 len = next_token(&buf);
Alex Elder820a5f32012-07-09 21:04:24 -05005749 if (!len) {
Alex Elder3feeb8942012-08-31 17:29:52 -05005750 buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
5751 len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
Alex Elderf28e5652012-10-25 23:34:41 -05005752 } else if (len > RBD_MAX_SNAP_NAME_LEN) {
Alex Elderdc79b112012-10-25 23:34:41 -05005753 ret = -ENAMETOOLONG;
Alex Elderf28e5652012-10-25 23:34:41 -05005754 goto out_err;
Alex Elder849b4262012-07-09 21:04:24 -05005755 }
Alex Elderecb4dc22013-04-26 09:43:47 -05005756 snap_name = kmemdup(buf, len + 1, GFP_KERNEL);
5757 if (!snap_name)
Alex Elderf28e5652012-10-25 23:34:41 -05005758 goto out_mem;
Alex Elderecb4dc22013-04-26 09:43:47 -05005759 *(snap_name + len) = '\0';
5760 spec->snap_name = snap_name;
Alex Eldere5c35532012-10-25 23:34:41 -05005761
Alex Elder0ddebc02012-10-25 23:34:41 -05005762 /* Initialize all rbd options to the defaults */
Alex Eldere28fff262012-02-02 08:13:30 -06005763
Alex Elder4e9afeb2012-10-25 23:34:41 -05005764 rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL);
5765 if (!rbd_opts)
5766 goto out_mem;
5767
5768 rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
Ilya Dryomovb5584182015-06-23 16:21:19 +03005769 rbd_opts->queue_depth = RBD_QUEUE_DEPTH_DEFAULT;
Ilya Dryomov80de1912016-09-20 14:23:17 +02005770 rbd_opts->lock_on_read = RBD_LOCK_ON_READ_DEFAULT;
Alex Elderd22f76e2012-07-12 10:46:35 -05005771
Alex Elder859c31d2012-10-25 23:34:42 -05005772 copts = ceph_parse_options(options, mon_addrs,
Alex Elder0ddebc02012-10-25 23:34:41 -05005773 mon_addrs + mon_addrs_size - 1,
Alex Elder4e9afeb2012-10-25 23:34:41 -05005774 parse_rbd_opts_token, rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05005775 if (IS_ERR(copts)) {
5776 ret = PTR_ERR(copts);
Alex Elderdc79b112012-10-25 23:34:41 -05005777 goto out_err;
5778 }
Alex Elder859c31d2012-10-25 23:34:42 -05005779 kfree(options);
5780
5781 *ceph_opts = copts;
Alex Elder4e9afeb2012-10-25 23:34:41 -05005782 *opts = rbd_opts;
Alex Elder859c31d2012-10-25 23:34:42 -05005783 *rbd_spec = spec;
Alex Elder0ddebc02012-10-25 23:34:41 -05005784
Alex Elderdc79b112012-10-25 23:34:41 -05005785 return 0;
Alex Elderf28e5652012-10-25 23:34:41 -05005786out_mem:
Alex Elderdc79b112012-10-25 23:34:41 -05005787 ret = -ENOMEM;
Alex Elderd22f76e2012-07-12 10:46:35 -05005788out_err:
Alex Elder859c31d2012-10-25 23:34:42 -05005789 kfree(rbd_opts);
5790 rbd_spec_put(spec);
Alex Elderf28e5652012-10-25 23:34:41 -05005791 kfree(options);
Alex Elderd22f76e2012-07-12 10:46:35 -05005792
Alex Elderdc79b112012-10-25 23:34:41 -05005793 return ret;
Alex Eldera725f65e2012-02-02 08:13:30 -06005794}
5795
Alex Elder589d30e2012-07-10 20:30:11 -05005796/*
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005797 * Return pool id (>= 0) or a negative error code.
5798 */
5799static int rbd_add_get_pool_id(struct rbd_client *rbdc, const char *pool_name)
5800{
Ilya Dryomova319bf52015-05-15 12:02:17 +03005801 struct ceph_options *opts = rbdc->client->options;
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005802 u64 newest_epoch;
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005803 int tries = 0;
5804 int ret;
5805
5806again:
5807 ret = ceph_pg_poolid_by_name(rbdc->client->osdc.osdmap, pool_name);
5808 if (ret == -ENOENT && tries++ < 1) {
Ilya Dryomovd0b19702016-04-28 16:07:27 +02005809 ret = ceph_monc_get_version(&rbdc->client->monc, "osdmap",
5810 &newest_epoch);
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005811 if (ret < 0)
5812 return ret;
5813
5814 if (rbdc->client->osdc.osdmap->epoch < newest_epoch) {
Ilya Dryomov7cca78c2016-04-28 16:07:28 +02005815 ceph_osdc_maybe_request_map(&rbdc->client->osdc);
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005816 (void) ceph_monc_wait_osdmap(&rbdc->client->monc,
Ilya Dryomova319bf52015-05-15 12:02:17 +03005817 newest_epoch,
5818 opts->mount_timeout);
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005819 goto again;
5820 } else {
5821 /* the osdmap we have is new enough */
5822 return -ENOENT;
5823 }
5824 }
5825
5826 return ret;
5827}
5828
5829/*
Alex Elder589d30e2012-07-10 20:30:11 -05005830 * An rbd format 2 image has a unique identifier, distinct from the
5831 * name given to it by the user. Internally, that identifier is
5832 * what's used to specify the names of objects related to the image.
5833 *
5834 * A special "rbd id" object is used to map an rbd image name to its
5835 * id. If that object doesn't exist, then there is no v2 rbd image
5836 * with the supplied name.
5837 *
5838 * This function will record the given rbd_dev's image_id field if
5839 * it can be determined, and in that case will return 0. If any
5840 * errors occur a negative errno will be returned and the rbd_dev's
5841 * image_id field will be unchanged (and should be NULL).
5842 */
5843static int rbd_dev_image_id(struct rbd_device *rbd_dev)
5844{
5845 int ret;
5846 size_t size;
5847 char *object_name;
5848 void *response;
Alex Elderc0fba362013-04-25 23:15:08 -05005849 char *image_id;
Alex Elder2f82ee52012-10-30 19:40:33 -05005850
Alex Elder589d30e2012-07-10 20:30:11 -05005851 /*
Alex Elder2c0d0a12012-10-30 19:40:33 -05005852 * When probing a parent image, the image id is already
5853 * known (and the image name likely is not). There's no
Alex Elderc0fba362013-04-25 23:15:08 -05005854 * need to fetch the image id again in this case. We
5855 * do still need to set the image format though.
Alex Elder2c0d0a12012-10-30 19:40:33 -05005856 */
Alex Elderc0fba362013-04-25 23:15:08 -05005857 if (rbd_dev->spec->image_id) {
5858 rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1;
5859
Alex Elder2c0d0a12012-10-30 19:40:33 -05005860 return 0;
Alex Elderc0fba362013-04-25 23:15:08 -05005861 }
Alex Elder2c0d0a12012-10-30 19:40:33 -05005862
5863 /*
Alex Elder589d30e2012-07-10 20:30:11 -05005864 * First, see if the format 2 image id file exists, and if
5865 * so, get the image's persistent id from it.
5866 */
Alex Elder69e7a022012-11-01 08:39:26 -05005867 size = sizeof (RBD_ID_PREFIX) + strlen(rbd_dev->spec->image_name);
Alex Elder589d30e2012-07-10 20:30:11 -05005868 object_name = kmalloc(size, GFP_NOIO);
5869 if (!object_name)
5870 return -ENOMEM;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05005871 sprintf(object_name, "%s%s", RBD_ID_PREFIX, rbd_dev->spec->image_name);
Alex Elder589d30e2012-07-10 20:30:11 -05005872 dout("rbd id object name is %s\n", object_name);
5873
5874 /* Response will be an encoded string, which includes a length */
5875
5876 size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
5877 response = kzalloc(size, GFP_NOIO);
5878 if (!response) {
5879 ret = -ENOMEM;
5880 goto out;
5881 }
5882
Alex Elderc0fba362013-04-25 23:15:08 -05005883 /* If it doesn't exist we'll assume it's a format 1 image */
5884
Alex Elder36be9a72013-01-19 00:30:28 -06005885 ret = rbd_obj_method_sync(rbd_dev, object_name,
Alex Elder41579762013-04-21 12:14:45 -05005886 "rbd", "get_id", NULL, 0,
Alex Eldere2a58ee2013-04-30 00:44:33 -05005887 response, RBD_IMAGE_ID_LEN_MAX);
Alex Elder36be9a72013-01-19 00:30:28 -06005888 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderc0fba362013-04-25 23:15:08 -05005889 if (ret == -ENOENT) {
5890 image_id = kstrdup("", GFP_KERNEL);
5891 ret = image_id ? 0 : -ENOMEM;
5892 if (!ret)
5893 rbd_dev->image_format = 1;
Ilya Dryomov7dd440c2014-09-11 18:49:18 +04005894 } else if (ret >= 0) {
Alex Elderc0fba362013-04-25 23:15:08 -05005895 void *p = response;
Alex Elder589d30e2012-07-10 20:30:11 -05005896
Alex Elderc0fba362013-04-25 23:15:08 -05005897 image_id = ceph_extract_encoded_string(&p, p + ret,
Alex Elder979ed482012-11-01 08:39:26 -05005898 NULL, GFP_NOIO);
Duan Jiong461f7582014-04-11 16:38:12 +08005899 ret = PTR_ERR_OR_ZERO(image_id);
Alex Elderc0fba362013-04-25 23:15:08 -05005900 if (!ret)
5901 rbd_dev->image_format = 2;
Alex Elderc0fba362013-04-25 23:15:08 -05005902 }
5903
5904 if (!ret) {
5905 rbd_dev->spec->image_id = image_id;
5906 dout("image_id is %s\n", image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05005907 }
5908out:
5909 kfree(response);
5910 kfree(object_name);
5911
5912 return ret;
5913}
5914
Alex Elder3abef3b2013-05-13 20:35:37 -05005915/*
5916 * Undo whatever state changes are made by v1 or v2 header info
5917 * call.
5918 */
Alex Elder6fd48b32013-04-28 23:32:34 -05005919static void rbd_dev_unprobe(struct rbd_device *rbd_dev)
5920{
5921 struct rbd_image_header *header;
5922
Ilya Dryomove69b8d42015-01-19 12:06:14 +03005923 rbd_dev_parent_put(rbd_dev);
Alex Elder6fd48b32013-04-28 23:32:34 -05005924
5925 /* Free dynamic fields from the header, then zero it out */
5926
5927 header = &rbd_dev->header;
Alex Elder812164f82013-04-30 00:44:32 -05005928 ceph_put_snap_context(header->snapc);
Alex Elder6fd48b32013-04-28 23:32:34 -05005929 kfree(header->snap_sizes);
5930 kfree(header->snap_names);
5931 kfree(header->object_prefix);
5932 memset(header, 0, sizeof (*header));
5933}
5934
Alex Elder2df3fac2013-05-06 09:51:30 -05005935static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev)
Alex Eldera30b71b2012-07-10 20:30:11 -05005936{
5937 int ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05005938
Alex Elder1e130192012-07-03 16:01:19 -05005939 ret = rbd_dev_v2_object_prefix(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05005940 if (ret)
Alex Elder1e130192012-07-03 16:01:19 -05005941 goto out_err;
Alex Elderb1b54022012-07-03 16:01:19 -05005942
Alex Elder2df3fac2013-05-06 09:51:30 -05005943 /*
5944 * Get the and check features for the image. Currently the
5945 * features are assumed to never change.
5946 */
Alex Elderb1b54022012-07-03 16:01:19 -05005947 ret = rbd_dev_v2_features(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05005948 if (ret)
Alex Elderb1b54022012-07-03 16:01:19 -05005949 goto out_err;
Alex Elder35d489f2012-07-03 16:01:19 -05005950
Alex Eldercc070d52013-04-21 12:14:45 -05005951 /* If the image supports fancy striping, get its parameters */
5952
5953 if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) {
5954 ret = rbd_dev_v2_striping_info(rbd_dev);
5955 if (ret < 0)
5956 goto out_err;
5957 }
Alex Elder2df3fac2013-05-06 09:51:30 -05005958 /* No support for crypto and compression type format 2 images */
Alex Eldera30b71b2012-07-10 20:30:11 -05005959
Alex Elder35152972012-08-31 17:29:55 -05005960 return 0;
Alex Elder9d475de2012-07-03 16:01:19 -05005961out_err:
Alex Elder642a2532013-05-06 17:40:33 -05005962 rbd_dev->header.features = 0;
Alex Elder1e130192012-07-03 16:01:19 -05005963 kfree(rbd_dev->header.object_prefix);
5964 rbd_dev->header.object_prefix = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05005965
5966 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05005967}
5968
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005969/*
5970 * @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() ->
5971 * rbd_dev_image_probe() recursion depth, which means it's also the
5972 * length of the already discovered part of the parent chain.
5973 */
5974static int rbd_dev_probe_parent(struct rbd_device *rbd_dev, int depth)
Alex Elder83a06262012-10-30 15:47:17 -05005975{
Alex Elder2f82ee52012-10-30 19:40:33 -05005976 struct rbd_device *parent = NULL;
Alex Elder124afba2013-04-26 15:44:36 -05005977 int ret;
5978
5979 if (!rbd_dev->parent_spec)
5980 return 0;
Alex Elder124afba2013-04-26 15:44:36 -05005981
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005982 if (++depth > RBD_MAX_PARENT_CHAIN_LEN) {
5983 pr_info("parent chain is too long (%d)\n", depth);
5984 ret = -EINVAL;
5985 goto out_err;
5986 }
5987
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02005988 parent = __rbd_dev_create(rbd_dev->rbd_client, rbd_dev->parent_spec);
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005989 if (!parent) {
5990 ret = -ENOMEM;
Alex Elder124afba2013-04-26 15:44:36 -05005991 goto out_err;
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005992 }
5993
5994 /*
5995 * Images related by parent/child relationships always share
5996 * rbd_client and spec/parent_spec, so bump their refcounts.
5997 */
5998 __rbd_get_client(rbd_dev->rbd_client);
5999 rbd_spec_get(rbd_dev->parent_spec);
Alex Elder124afba2013-04-26 15:44:36 -05006000
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006001 ret = rbd_dev_image_probe(parent, depth);
Alex Elder124afba2013-04-26 15:44:36 -05006002 if (ret < 0)
6003 goto out_err;
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02006004
Alex Elder124afba2013-04-26 15:44:36 -05006005 rbd_dev->parent = parent;
Alex Eldera2acd002013-05-08 22:50:04 -05006006 atomic_set(&rbd_dev->parent_ref, 1);
Alex Elder124afba2013-04-26 15:44:36 -05006007 return 0;
Alex Elder124afba2013-04-26 15:44:36 -05006008
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02006009out_err:
6010 rbd_dev_unparent(rbd_dev);
Markus Elfring1761b222015-11-23 20:16:45 +01006011 rbd_dev_destroy(parent);
Alex Elder124afba2013-04-26 15:44:36 -05006012 return ret;
6013}
6014
Ilya Dryomov811c6682016-04-15 16:22:16 +02006015/*
6016 * rbd_dev->header_rwsem must be locked for write and will be unlocked
6017 * upon return.
6018 */
Alex Elder200a6a82013-04-28 23:32:34 -05006019static int rbd_dev_device_setup(struct rbd_device *rbd_dev)
Alex Elder124afba2013-04-26 15:44:36 -05006020{
Alex Elder83a06262012-10-30 15:47:17 -05006021 int ret;
Alex Elder83a06262012-10-30 15:47:17 -05006022
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006023 /* Record our major and minor device numbers. */
Alex Elder83a06262012-10-30 15:47:17 -05006024
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006025 if (!single_major) {
6026 ret = register_blkdev(0, rbd_dev->name);
6027 if (ret < 0)
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02006028 goto err_out_unlock;
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006029
6030 rbd_dev->major = ret;
6031 rbd_dev->minor = 0;
6032 } else {
6033 rbd_dev->major = rbd_major;
6034 rbd_dev->minor = rbd_dev_id_to_minor(rbd_dev->dev_id);
6035 }
Alex Elder83a06262012-10-30 15:47:17 -05006036
6037 /* Set up the blkdev mapping. */
6038
6039 ret = rbd_init_disk(rbd_dev);
6040 if (ret)
6041 goto err_out_blkdev;
6042
Alex Elderf35a4de2013-05-06 09:51:29 -05006043 ret = rbd_dev_mapping_set(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05006044 if (ret)
6045 goto err_out_disk;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04006046
Alex Elderf35a4de2013-05-06 09:51:29 -05006047 set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
Josh Durgin22001f62013-09-30 20:10:04 -07006048 set_disk_ro(rbd_dev->disk, rbd_dev->mapping.read_only);
Alex Elderf35a4de2013-05-06 09:51:29 -05006049
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006050 dev_set_name(&rbd_dev->dev, "%d", rbd_dev->dev_id);
6051 ret = device_add(&rbd_dev->dev);
Alex Elderf35a4de2013-05-06 09:51:29 -05006052 if (ret)
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006053 goto err_out_mapping;
Alex Elder83a06262012-10-30 15:47:17 -05006054
Alex Elder83a06262012-10-30 15:47:17 -05006055 /* Everything's ready. Announce the disk to the world. */
6056
Alex Elder129b79d2013-04-26 15:44:36 -05006057 set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
Ilya Dryomov811c6682016-04-15 16:22:16 +02006058 up_write(&rbd_dev->header_rwsem);
Alex Elder83a06262012-10-30 15:47:17 -05006059
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02006060 spin_lock(&rbd_dev_list_lock);
6061 list_add_tail(&rbd_dev->node, &rbd_dev_list);
6062 spin_unlock(&rbd_dev_list_lock);
6063
Ilya Dryomov811c6682016-04-15 16:22:16 +02006064 add_disk(rbd_dev->disk);
Ilya Dryomovca7909e2016-08-18 18:38:41 +02006065 pr_info("%s: capacity %llu features 0x%llx\n", rbd_dev->disk->disk_name,
6066 (unsigned long long)get_capacity(rbd_dev->disk) << SECTOR_SHIFT,
6067 rbd_dev->header.features);
Alex Elder83a06262012-10-30 15:47:17 -05006068
6069 return ret;
Alex Elder2f82ee52012-10-30 19:40:33 -05006070
Alex Elderf35a4de2013-05-06 09:51:29 -05006071err_out_mapping:
6072 rbd_dev_mapping_clear(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05006073err_out_disk:
6074 rbd_free_disk(rbd_dev);
6075err_out_blkdev:
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006076 if (!single_major)
6077 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Ilya Dryomov811c6682016-04-15 16:22:16 +02006078err_out_unlock:
6079 up_write(&rbd_dev->header_rwsem);
Alex Elder83a06262012-10-30 15:47:17 -05006080 return ret;
6081}
6082
Alex Elder332bb122013-04-27 09:59:30 -05006083static int rbd_dev_header_name(struct rbd_device *rbd_dev)
6084{
6085 struct rbd_spec *spec = rbd_dev->spec;
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006086 int ret;
Alex Elder332bb122013-04-27 09:59:30 -05006087
6088 /* Record the header object name for this rbd image. */
6089
6090 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
6091
Yan, Zheng76271512016-02-03 21:24:49 +08006092 rbd_dev->header_oloc.pool = rbd_dev->layout.pool_id;
Alex Elder332bb122013-04-27 09:59:30 -05006093 if (rbd_dev->image_format == 1)
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006094 ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s",
6095 spec->image_name, RBD_SUFFIX);
Alex Elder332bb122013-04-27 09:59:30 -05006096 else
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006097 ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s",
6098 RBD_HEADER_PREFIX, spec->image_id);
Alex Elder332bb122013-04-27 09:59:30 -05006099
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006100 return ret;
Alex Elder332bb122013-04-27 09:59:30 -05006101}
6102
Alex Elder200a6a82013-04-28 23:32:34 -05006103static void rbd_dev_image_release(struct rbd_device *rbd_dev)
6104{
Alex Elder6fd48b32013-04-28 23:32:34 -05006105 rbd_dev_unprobe(rbd_dev);
Alex Elder6fd48b32013-04-28 23:32:34 -05006106 rbd_dev->image_format = 0;
6107 kfree(rbd_dev->spec->image_id);
6108 rbd_dev->spec->image_id = NULL;
6109
Alex Elder200a6a82013-04-28 23:32:34 -05006110 rbd_dev_destroy(rbd_dev);
6111}
6112
Alex Eldera30b71b2012-07-10 20:30:11 -05006113/*
6114 * Probe for the existence of the header object for the given rbd
Alex Elder1f3ef782013-05-06 17:40:33 -05006115 * device. If this image is the one being mapped (i.e., not a
6116 * parent), initiate a watch on its header object before using that
6117 * object to get detailed information about the rbd image.
Alex Eldera30b71b2012-07-10 20:30:11 -05006118 */
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006119static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth)
Alex Eldera30b71b2012-07-10 20:30:11 -05006120{
6121 int ret;
6122
6123 /*
Alex Elder3abef3b2013-05-13 20:35:37 -05006124 * Get the id from the image id object. Unless there's an
6125 * error, rbd_dev->spec->image_id will be filled in with
6126 * a dynamically-allocated string, and rbd_dev->image_format
6127 * will be set to either 1 or 2.
Alex Eldera30b71b2012-07-10 20:30:11 -05006128 */
6129 ret = rbd_dev_image_id(rbd_dev);
6130 if (ret)
Alex Elderc0fba362013-04-25 23:15:08 -05006131 return ret;
Alex Elderc0fba362013-04-25 23:15:08 -05006132
Alex Elder332bb122013-04-27 09:59:30 -05006133 ret = rbd_dev_header_name(rbd_dev);
6134 if (ret)
6135 goto err_out_format;
6136
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006137 if (!depth) {
Ilya Dryomov99d16942016-08-12 16:11:41 +02006138 ret = rbd_register_watch(rbd_dev);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006139 if (ret) {
6140 if (ret == -ENOENT)
6141 pr_info("image %s/%s does not exist\n",
6142 rbd_dev->spec->pool_name,
6143 rbd_dev->spec->image_name);
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006144 goto err_out_format;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006145 }
Alex Elder1f3ef782013-05-06 17:40:33 -05006146 }
Alex Elderb644de22013-04-27 09:59:31 -05006147
Ilya Dryomova720ae02014-07-23 17:11:19 +04006148 ret = rbd_dev_header_info(rbd_dev);
Alex Elder5655c4d2013-04-25 23:15:08 -05006149 if (ret)
Alex Elderb644de22013-04-27 09:59:31 -05006150 goto err_out_watch;
Alex Elder83a06262012-10-30 15:47:17 -05006151
Ilya Dryomov04077592014-07-23 17:11:20 +04006152 /*
6153 * If this image is the one being mapped, we have pool name and
6154 * id, image name and id, and snap name - need to fill snap id.
6155 * Otherwise this is a parent image, identified by pool, image
6156 * and snap ids - need to fill in names for those ids.
6157 */
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006158 if (!depth)
Ilya Dryomov04077592014-07-23 17:11:20 +04006159 ret = rbd_spec_fill_snap_id(rbd_dev);
6160 else
6161 ret = rbd_spec_fill_names(rbd_dev);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006162 if (ret) {
6163 if (ret == -ENOENT)
6164 pr_info("snap %s/%s@%s does not exist\n",
6165 rbd_dev->spec->pool_name,
6166 rbd_dev->spec->image_name,
6167 rbd_dev->spec->snap_name);
Alex Elder33dca392013-04-30 00:44:33 -05006168 goto err_out_probe;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006169 }
Alex Elder9bb81c92013-04-27 09:59:30 -05006170
Ilya Dryomove8f59b52014-07-24 10:42:13 +04006171 if (rbd_dev->header.features & RBD_FEATURE_LAYERING) {
6172 ret = rbd_dev_v2_parent_info(rbd_dev);
6173 if (ret)
6174 goto err_out_probe;
6175
6176 /*
6177 * Need to warn users if this image is the one being
6178 * mapped and has a parent.
6179 */
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006180 if (!depth && rbd_dev->parent_spec)
Ilya Dryomove8f59b52014-07-24 10:42:13 +04006181 rbd_warn(rbd_dev,
6182 "WARNING: kernel layering is EXPERIMENTAL!");
6183 }
6184
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006185 ret = rbd_dev_probe_parent(rbd_dev, depth);
Alex Elder30d60ba2013-05-06 09:51:30 -05006186 if (ret)
6187 goto err_out_probe;
Alex Elder83a06262012-10-30 15:47:17 -05006188
Alex Elder30d60ba2013-05-06 09:51:30 -05006189 dout("discovered format %u image, header name is %s\n",
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006190 rbd_dev->image_format, rbd_dev->header_oid.name);
Alex Elder30d60ba2013-05-06 09:51:30 -05006191 return 0;
Ilya Dryomove8f59b52014-07-24 10:42:13 +04006192
Alex Elder6fd48b32013-04-28 23:32:34 -05006193err_out_probe:
6194 rbd_dev_unprobe(rbd_dev);
Alex Elderb644de22013-04-27 09:59:31 -05006195err_out_watch:
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006196 if (!depth)
Ilya Dryomov99d16942016-08-12 16:11:41 +02006197 rbd_unregister_watch(rbd_dev);
Alex Elder332bb122013-04-27 09:59:30 -05006198err_out_format:
6199 rbd_dev->image_format = 0;
Alex Elder5655c4d2013-04-25 23:15:08 -05006200 kfree(rbd_dev->spec->image_id);
6201 rbd_dev->spec->image_id = NULL;
Alex Elder5655c4d2013-04-25 23:15:08 -05006202 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05006203}
6204
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006205static ssize_t do_rbd_add(struct bus_type *bus,
6206 const char *buf,
6207 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006208{
Alex Eldercb8627c2012-07-09 21:04:23 -05006209 struct rbd_device *rbd_dev = NULL;
Alex Elderdc79b112012-10-25 23:34:41 -05006210 struct ceph_options *ceph_opts = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05006211 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05006212 struct rbd_spec *spec = NULL;
Alex Elder9d3997f2012-10-25 23:34:42 -05006213 struct rbd_client *rbdc;
Alex Elder51344a32013-05-06 07:40:30 -05006214 bool read_only;
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02006215 int rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006216
6217 if (!try_module_get(THIS_MODULE))
6218 return -ENODEV;
6219
Alex Eldera725f65e2012-02-02 08:13:30 -06006220 /* parse add command */
Alex Elder859c31d2012-10-25 23:34:42 -05006221 rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec);
Alex Elderdc79b112012-10-25 23:34:41 -05006222 if (rc < 0)
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006223 goto out;
Alex Eldera725f65e2012-02-02 08:13:30 -06006224
Alex Elder9d3997f2012-10-25 23:34:42 -05006225 rbdc = rbd_get_client(ceph_opts);
6226 if (IS_ERR(rbdc)) {
6227 rc = PTR_ERR(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05006228 goto err_out_args;
Alex Elder9d3997f2012-10-25 23:34:42 -05006229 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006230
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006231 /* pick the pool */
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04006232 rc = rbd_add_get_pool_id(rbdc, spec->pool_name);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006233 if (rc < 0) {
6234 if (rc == -ENOENT)
6235 pr_info("pool %s does not exist\n", spec->pool_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006236 goto err_out_client;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006237 }
Alex Elderc0cd10db2013-04-26 09:43:47 -05006238 spec->pool_id = (u64)rc;
Alex Elder859c31d2012-10-25 23:34:42 -05006239
Ilya Dryomovd1475432015-06-22 13:24:48 +03006240 rbd_dev = rbd_dev_create(rbdc, spec, rbd_opts);
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02006241 if (!rbd_dev) {
6242 rc = -ENOMEM;
Alex Elderbd4ba652012-10-25 23:34:42 -05006243 goto err_out_client;
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02006244 }
Alex Elderc53d5892012-10-25 23:34:42 -05006245 rbdc = NULL; /* rbd_dev now owns this */
6246 spec = NULL; /* rbd_dev now owns this */
Ilya Dryomovd1475432015-06-22 13:24:48 +03006247 rbd_opts = NULL; /* rbd_dev now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006248
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02006249 rbd_dev->config_info = kstrdup(buf, GFP_KERNEL);
6250 if (!rbd_dev->config_info) {
6251 rc = -ENOMEM;
6252 goto err_out_rbd_dev;
6253 }
6254
Ilya Dryomov811c6682016-04-15 16:22:16 +02006255 down_write(&rbd_dev->header_rwsem);
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006256 rc = rbd_dev_image_probe(rbd_dev, 0);
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02006257 if (rc < 0) {
6258 up_write(&rbd_dev->header_rwsem);
Alex Elderc53d5892012-10-25 23:34:42 -05006259 goto err_out_rbd_dev;
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02006260 }
Alex Elder05fd6f62012-08-29 17:11:07 -05006261
Alex Elder7ce4eef2013-05-06 17:40:33 -05006262 /* If we are mapping a snapshot it must be marked read-only */
6263
Ilya Dryomovd1475432015-06-22 13:24:48 +03006264 read_only = rbd_dev->opts->read_only;
Alex Elder7ce4eef2013-05-06 17:40:33 -05006265 if (rbd_dev->spec->snap_id != CEPH_NOSNAP)
6266 read_only = true;
6267 rbd_dev->mapping.read_only = read_only;
6268
Alex Elderb536f692013-04-28 23:32:34 -05006269 rc = rbd_dev_device_setup(rbd_dev);
Alex Elder3abef3b2013-05-13 20:35:37 -05006270 if (rc) {
Ilya Dryomove37180c2013-12-16 18:02:41 +02006271 /*
Ilya Dryomov99d16942016-08-12 16:11:41 +02006272 * rbd_unregister_watch() can't be moved into
Ilya Dryomove37180c2013-12-16 18:02:41 +02006273 * rbd_dev_image_release() without refactoring, see
6274 * commit 1f3ef78861ac.
6275 */
Ilya Dryomov99d16942016-08-12 16:11:41 +02006276 rbd_unregister_watch(rbd_dev);
Alex Elder3abef3b2013-05-13 20:35:37 -05006277 rbd_dev_image_release(rbd_dev);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006278 goto out;
Alex Elder3abef3b2013-05-13 20:35:37 -05006279 }
Alex Elderb536f692013-04-28 23:32:34 -05006280
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006281 rc = count;
6282out:
6283 module_put(THIS_MODULE);
6284 return rc;
Alex Elder3abef3b2013-05-13 20:35:37 -05006285
Alex Elderc53d5892012-10-25 23:34:42 -05006286err_out_rbd_dev:
6287 rbd_dev_destroy(rbd_dev);
Alex Elderbd4ba652012-10-25 23:34:42 -05006288err_out_client:
Alex Elder9d3997f2012-10-25 23:34:42 -05006289 rbd_put_client(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05006290err_out_args:
Alex Elder859c31d2012-10-25 23:34:42 -05006291 rbd_spec_put(spec);
Ilya Dryomovd1475432015-06-22 13:24:48 +03006292 kfree(rbd_opts);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006293 goto out;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006294}
6295
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006296static ssize_t rbd_add(struct bus_type *bus,
6297 const char *buf,
6298 size_t count)
6299{
6300 if (single_major)
6301 return -EINVAL;
6302
6303 return do_rbd_add(bus, buf, count);
6304}
6305
6306static ssize_t rbd_add_single_major(struct bus_type *bus,
6307 const char *buf,
6308 size_t count)
6309{
6310 return do_rbd_add(bus, buf, count);
6311}
6312
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006313static void rbd_dev_device_release(struct rbd_device *rbd_dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006314{
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006315 rbd_free_disk(rbd_dev);
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02006316
6317 spin_lock(&rbd_dev_list_lock);
6318 list_del_init(&rbd_dev->node);
6319 spin_unlock(&rbd_dev_list_lock);
6320
Alex Elder200a6a82013-04-28 23:32:34 -05006321 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006322 device_del(&rbd_dev->dev);
Alex Elder6d80b132013-05-06 07:40:30 -05006323 rbd_dev_mapping_clear(rbd_dev);
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006324 if (!single_major)
6325 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006326}
6327
Alex Elder05a46af2013-04-26 15:44:36 -05006328static void rbd_dev_remove_parent(struct rbd_device *rbd_dev)
6329{
Alex Elderad945fc2013-04-26 15:44:36 -05006330 while (rbd_dev->parent) {
Alex Elder05a46af2013-04-26 15:44:36 -05006331 struct rbd_device *first = rbd_dev;
6332 struct rbd_device *second = first->parent;
6333 struct rbd_device *third;
6334
6335 /*
6336 * Follow to the parent with no grandparent and
6337 * remove it.
6338 */
6339 while (second && (third = second->parent)) {
6340 first = second;
6341 second = third;
6342 }
Alex Elderad945fc2013-04-26 15:44:36 -05006343 rbd_assert(second);
Alex Elder8ad42cd2013-04-28 23:32:34 -05006344 rbd_dev_image_release(second);
Alex Elderad945fc2013-04-26 15:44:36 -05006345 first->parent = NULL;
6346 first->parent_overlap = 0;
6347
6348 rbd_assert(first->parent_spec);
Alex Elder05a46af2013-04-26 15:44:36 -05006349 rbd_spec_put(first->parent_spec);
6350 first->parent_spec = NULL;
Alex Elder05a46af2013-04-26 15:44:36 -05006351 }
6352}
6353
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006354static ssize_t do_rbd_remove(struct bus_type *bus,
6355 const char *buf,
6356 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006357{
6358 struct rbd_device *rbd_dev = NULL;
Alex Elder751cc0e2013-05-31 15:17:01 -05006359 struct list_head *tmp;
6360 int dev_id;
Mike Christie0276dca2016-08-18 18:38:45 +02006361 char opt_buf[6];
Alex Elder82a442d2013-05-31 17:40:44 -05006362 bool already = false;
Mike Christie0276dca2016-08-18 18:38:45 +02006363 bool force = false;
Alex Elder0d8189e2013-04-27 09:59:30 -05006364 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006365
Mike Christie0276dca2016-08-18 18:38:45 +02006366 dev_id = -1;
6367 opt_buf[0] = '\0';
6368 sscanf(buf, "%d %5s", &dev_id, opt_buf);
6369 if (dev_id < 0) {
6370 pr_err("dev_id out of range\n");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006371 return -EINVAL;
Mike Christie0276dca2016-08-18 18:38:45 +02006372 }
6373 if (opt_buf[0] != '\0') {
6374 if (!strcmp(opt_buf, "force")) {
6375 force = true;
6376 } else {
6377 pr_err("bad remove option at '%s'\n", opt_buf);
6378 return -EINVAL;
6379 }
6380 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006381
Alex Elder751cc0e2013-05-31 15:17:01 -05006382 ret = -ENOENT;
6383 spin_lock(&rbd_dev_list_lock);
6384 list_for_each(tmp, &rbd_dev_list) {
6385 rbd_dev = list_entry(tmp, struct rbd_device, node);
6386 if (rbd_dev->dev_id == dev_id) {
6387 ret = 0;
6388 break;
6389 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006390 }
Alex Elder751cc0e2013-05-31 15:17:01 -05006391 if (!ret) {
6392 spin_lock_irq(&rbd_dev->lock);
Mike Christie0276dca2016-08-18 18:38:45 +02006393 if (rbd_dev->open_count && !force)
Alex Elder751cc0e2013-05-31 15:17:01 -05006394 ret = -EBUSY;
6395 else
Alex Elder82a442d2013-05-31 17:40:44 -05006396 already = test_and_set_bit(RBD_DEV_FLAG_REMOVING,
6397 &rbd_dev->flags);
Alex Elder751cc0e2013-05-31 15:17:01 -05006398 spin_unlock_irq(&rbd_dev->lock);
6399 }
6400 spin_unlock(&rbd_dev_list_lock);
Alex Elder82a442d2013-05-31 17:40:44 -05006401 if (ret < 0 || already)
Alex Elder1ba0f1e2013-05-31 15:17:01 -05006402 return ret;
Alex Elder751cc0e2013-05-31 15:17:01 -05006403
Mike Christie0276dca2016-08-18 18:38:45 +02006404 if (force) {
6405 /*
6406 * Prevent new IO from being queued and wait for existing
6407 * IO to complete/fail.
6408 */
6409 blk_mq_freeze_queue(rbd_dev->disk->queue);
6410 blk_set_queue_dying(rbd_dev->disk->queue);
6411 }
6412
Ilya Dryomoved95b212016-08-12 16:40:02 +02006413 down_write(&rbd_dev->lock_rwsem);
6414 if (__rbd_is_lock_owner(rbd_dev))
6415 rbd_unlock(rbd_dev);
6416 up_write(&rbd_dev->lock_rwsem);
Ilya Dryomov99d16942016-08-12 16:11:41 +02006417 rbd_unregister_watch(rbd_dev);
Ilya Dryomovfca27062013-12-16 18:02:40 +02006418
Josh Durgin98752012013-08-29 17:26:31 -07006419 /*
6420 * Don't free anything from rbd_dev->disk until after all
6421 * notifies are completely processed. Otherwise
6422 * rbd_bus_del_dev() will race with rbd_watch_cb(), resulting
6423 * in a potential use after free of rbd_dev->disk or rbd_dev.
6424 */
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006425 rbd_dev_device_release(rbd_dev);
Alex Elder8ad42cd2013-04-28 23:32:34 -05006426 rbd_dev_image_release(rbd_dev);
Alex Elderaafb2302012-09-06 16:00:54 -05006427
Alex Elder1ba0f1e2013-05-31 15:17:01 -05006428 return count;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006429}
6430
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006431static ssize_t rbd_remove(struct bus_type *bus,
6432 const char *buf,
6433 size_t count)
6434{
6435 if (single_major)
6436 return -EINVAL;
6437
6438 return do_rbd_remove(bus, buf, count);
6439}
6440
6441static ssize_t rbd_remove_single_major(struct bus_type *bus,
6442 const char *buf,
6443 size_t count)
6444{
6445 return do_rbd_remove(bus, buf, count);
6446}
6447
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006448/*
6449 * create control files in sysfs
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006450 * /sys/bus/rbd/...
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006451 */
6452static int rbd_sysfs_init(void)
6453{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006454 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006455
Alex Elderfed4c142012-02-07 12:03:36 -06006456 ret = device_register(&rbd_root_dev);
Alex Elder21079782012-01-24 10:08:36 -06006457 if (ret < 0)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006458 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006459
Alex Elderfed4c142012-02-07 12:03:36 -06006460 ret = bus_register(&rbd_bus_type);
6461 if (ret < 0)
6462 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006463
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006464 return ret;
6465}
6466
6467static void rbd_sysfs_cleanup(void)
6468{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006469 bus_unregister(&rbd_bus_type);
Alex Elderfed4c142012-02-07 12:03:36 -06006470 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006471}
6472
Alex Elder1c2a9df2013-05-01 12:43:03 -05006473static int rbd_slab_init(void)
6474{
6475 rbd_assert(!rbd_img_request_cache);
Geliang Tang03d94402016-03-13 15:17:32 +08006476 rbd_img_request_cache = KMEM_CACHE(rbd_img_request, 0);
Alex Elder868311b2013-05-01 12:43:03 -05006477 if (!rbd_img_request_cache)
6478 return -ENOMEM;
6479
6480 rbd_assert(!rbd_obj_request_cache);
Geliang Tang03d94402016-03-13 15:17:32 +08006481 rbd_obj_request_cache = KMEM_CACHE(rbd_obj_request, 0);
Alex Elder78c2a442013-05-01 12:43:04 -05006482 if (!rbd_obj_request_cache)
6483 goto out_err;
6484
6485 rbd_assert(!rbd_segment_name_cache);
6486 rbd_segment_name_cache = kmem_cache_create("rbd_segment_name",
Ilya Dryomov2d0ebc52014-01-27 17:40:18 +02006487 CEPH_MAX_OID_NAME_LEN + 1, 1, 0, NULL);
Alex Elder78c2a442013-05-01 12:43:04 -05006488 if (rbd_segment_name_cache)
Alex Elder1c2a9df2013-05-01 12:43:03 -05006489 return 0;
Alex Elder78c2a442013-05-01 12:43:04 -05006490out_err:
Julia Lawall13bf2832015-09-13 14:15:26 +02006491 kmem_cache_destroy(rbd_obj_request_cache);
6492 rbd_obj_request_cache = NULL;
Alex Elder1c2a9df2013-05-01 12:43:03 -05006493
Alex Elder868311b2013-05-01 12:43:03 -05006494 kmem_cache_destroy(rbd_img_request_cache);
6495 rbd_img_request_cache = NULL;
6496
Alex Elder1c2a9df2013-05-01 12:43:03 -05006497 return -ENOMEM;
6498}
6499
6500static void rbd_slab_exit(void)
6501{
Alex Elder78c2a442013-05-01 12:43:04 -05006502 rbd_assert(rbd_segment_name_cache);
6503 kmem_cache_destroy(rbd_segment_name_cache);
6504 rbd_segment_name_cache = NULL;
6505
Alex Elder868311b2013-05-01 12:43:03 -05006506 rbd_assert(rbd_obj_request_cache);
6507 kmem_cache_destroy(rbd_obj_request_cache);
6508 rbd_obj_request_cache = NULL;
6509
Alex Elder1c2a9df2013-05-01 12:43:03 -05006510 rbd_assert(rbd_img_request_cache);
6511 kmem_cache_destroy(rbd_img_request_cache);
6512 rbd_img_request_cache = NULL;
6513}
6514
Alex Eldercc344fa2013-02-19 12:25:56 -06006515static int __init rbd_init(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006516{
6517 int rc;
6518
Alex Elder1e32d342013-01-30 11:13:33 -06006519 if (!libceph_compatible(NULL)) {
6520 rbd_warn(NULL, "libceph incompatibility (quitting)");
Alex Elder1e32d342013-01-30 11:13:33 -06006521 return -EINVAL;
6522 }
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006523
Alex Elder1c2a9df2013-05-01 12:43:03 -05006524 rc = rbd_slab_init();
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006525 if (rc)
6526 return rc;
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006527
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006528 /*
6529 * The number of active work items is limited by the number of
Ilya Dryomovf77303b2015-04-22 18:28:13 +03006530 * rbd devices * queue depth, so leave @max_active at default.
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006531 */
6532 rbd_wq = alloc_workqueue(RBD_DRV_NAME, WQ_MEM_RECLAIM, 0);
6533 if (!rbd_wq) {
6534 rc = -ENOMEM;
6535 goto err_out_slab;
6536 }
6537
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006538 if (single_major) {
6539 rbd_major = register_blkdev(0, RBD_DRV_NAME);
6540 if (rbd_major < 0) {
6541 rc = rbd_major;
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006542 goto err_out_wq;
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006543 }
6544 }
6545
Alex Elder1c2a9df2013-05-01 12:43:03 -05006546 rc = rbd_sysfs_init();
6547 if (rc)
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006548 goto err_out_blkdev;
Alex Elder1c2a9df2013-05-01 12:43:03 -05006549
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006550 if (single_major)
6551 pr_info("loaded (major %d)\n", rbd_major);
6552 else
6553 pr_info("loaded\n");
6554
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006555 return 0;
6556
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006557err_out_blkdev:
6558 if (single_major)
6559 unregister_blkdev(rbd_major, RBD_DRV_NAME);
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006560err_out_wq:
6561 destroy_workqueue(rbd_wq);
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006562err_out_slab:
6563 rbd_slab_exit();
Alex Elder1c2a9df2013-05-01 12:43:03 -05006564 return rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006565}
6566
Alex Eldercc344fa2013-02-19 12:25:56 -06006567static void __exit rbd_exit(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006568{
Ilya Dryomovffe312c2014-05-20 15:46:04 +04006569 ida_destroy(&rbd_dev_id_ida);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006570 rbd_sysfs_cleanup();
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006571 if (single_major)
6572 unregister_blkdev(rbd_major, RBD_DRV_NAME);
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006573 destroy_workqueue(rbd_wq);
Alex Elder1c2a9df2013-05-01 12:43:03 -05006574 rbd_slab_exit();
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006575}
6576
6577module_init(rbd_init);
6578module_exit(rbd_exit);
6579
Alex Elderd552c612013-05-31 20:13:09 -05006580MODULE_AUTHOR("Alex Elder <elder@inktank.com>");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006581MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
6582MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006583/* following authorship retained from original osdblk.c */
6584MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
6585
Ilya Dryomov90da2582013-12-13 15:28:56 +02006586MODULE_DESCRIPTION("RADOS Block Device (RBD) driver");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006587MODULE_LICENSE("GPL");