blob: 35fc1da6c83d69f2607bf5d8b82b98f2eaf458a3 [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 Dryomov210c1042015-06-22 13:24:48 +0300798 Opt_err
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700799};
800
Alex Elder43ae4702012-07-03 16:01:18 -0500801static match_table_t rbd_opts_tokens = {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300802 {Opt_queue_depth, "queue_depth=%d"},
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700803 /* int args above */
804 /* string args above */
Alex Elderbe466c12012-10-22 11:31:26 -0500805 {Opt_read_only, "read_only"},
Alex Eldercc0538b2012-08-10 13:12:07 -0700806 {Opt_read_only, "ro"}, /* Alternate spelling */
807 {Opt_read_write, "read_write"},
808 {Opt_read_write, "rw"}, /* Alternate spelling */
Ilya Dryomov210c1042015-06-22 13:24:48 +0300809 {Opt_err, NULL}
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700810};
811
Alex Elder98571b52013-01-20 14:44:42 -0600812struct rbd_options {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300813 int queue_depth;
Alex Elder98571b52013-01-20 14:44:42 -0600814 bool read_only;
815};
816
Ilya Dryomovb5584182015-06-23 16:21:19 +0300817#define RBD_QUEUE_DEPTH_DEFAULT BLKDEV_MAX_RQ
Alex Elder98571b52013-01-20 14:44:42 -0600818#define RBD_READ_ONLY_DEFAULT false
819
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700820static int parse_rbd_opts_token(char *c, void *private)
821{
Alex Elder43ae4702012-07-03 16:01:18 -0500822 struct rbd_options *rbd_opts = private;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700823 substring_t argstr[MAX_OPT_ARGS];
824 int token, intval, ret;
825
Alex Elder43ae4702012-07-03 16:01:18 -0500826 token = match_token(c, rbd_opts_tokens, argstr);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700827 if (token < Opt_last_int) {
828 ret = match_int(&argstr[0], &intval);
829 if (ret < 0) {
Ilya Dryomov210c1042015-06-22 13:24:48 +0300830 pr_err("bad mount option arg (not int) at '%s'\n", c);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700831 return ret;
832 }
833 dout("got int token %d val %d\n", token, intval);
834 } else if (token > Opt_last_int && token < Opt_last_string) {
Ilya Dryomov210c1042015-06-22 13:24:48 +0300835 dout("got string token %d val %s\n", token, argstr[0].from);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700836 } else {
837 dout("got token %d\n", token);
838 }
839
840 switch (token) {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300841 case Opt_queue_depth:
842 if (intval < 1) {
843 pr_err("queue_depth out of range\n");
844 return -EINVAL;
845 }
846 rbd_opts->queue_depth = intval;
847 break;
Alex Eldercc0538b2012-08-10 13:12:07 -0700848 case Opt_read_only:
849 rbd_opts->read_only = true;
850 break;
851 case Opt_read_write:
852 rbd_opts->read_only = false;
853 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700854 default:
Ilya Dryomov210c1042015-06-22 13:24:48 +0300855 /* libceph prints "bad option" msg */
856 return -EINVAL;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700857 }
Ilya Dryomov210c1042015-06-22 13:24:48 +0300858
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700859 return 0;
860}
861
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800862static char* obj_op_name(enum obj_operation_type op_type)
863{
864 switch (op_type) {
865 case OBJ_OP_READ:
866 return "read";
867 case OBJ_OP_WRITE:
868 return "write";
Guangliang Zhao90e98c52014-04-01 22:22:16 +0800869 case OBJ_OP_DISCARD:
870 return "discard";
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800871 default:
872 return "???";
873 }
874}
875
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700876/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700877 * Get a ceph client with specific addr and configuration, if one does
Alex Elder7262cfc2013-05-16 15:04:20 -0500878 * not exist create it. Either way, ceph_opts is consumed by this
879 * function.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700880 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500881static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700882{
Alex Elderf8c38922012-08-10 13:12:07 -0700883 struct rbd_client *rbdc;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700884
Alex Eldercfbf6372013-05-31 17:40:45 -0500885 mutex_lock_nested(&client_mutex, SINGLE_DEPTH_NESTING);
Alex Elder1f7ba332012-08-10 13:12:07 -0700886 rbdc = rbd_client_find(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500887 if (rbdc) /* using an existing client */
Alex Elder43ae4702012-07-03 16:01:18 -0500888 ceph_destroy_options(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500889 else
Alex Elderf8c38922012-08-10 13:12:07 -0700890 rbdc = rbd_client_create(ceph_opts);
Alex Eldercfbf6372013-05-31 17:40:45 -0500891 mutex_unlock(&client_mutex);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700892
Alex Elder9d3997f2012-10-25 23:34:42 -0500893 return rbdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700894}
895
896/*
897 * Destroy ceph client
Alex Elderd23a4b32012-01-29 13:57:43 -0600898 *
Alex Elder432b8582012-01-29 13:57:44 -0600899 * Caller must hold rbd_client_list_lock.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700900 */
901static void rbd_client_release(struct kref *kref)
902{
903 struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
904
Alex Elder37206ee2013-02-20 17:32:08 -0600905 dout("%s: rbdc %p\n", __func__, rbdc);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500906 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700907 list_del(&rbdc->node);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500908 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700909
910 ceph_destroy_client(rbdc->client);
911 kfree(rbdc);
912}
913
914/*
915 * Drop reference to ceph client node. If it's not referenced anymore, release
916 * it.
917 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500918static void rbd_put_client(struct rbd_client *rbdc)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700919{
Alex Elderc53d5892012-10-25 23:34:42 -0500920 if (rbdc)
921 kref_put(&rbdc->kref, rbd_client_release);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700922}
923
Alex Eldera30b71b2012-07-10 20:30:11 -0500924static bool rbd_image_format_valid(u32 image_format)
925{
926 return image_format == 1 || image_format == 2;
927}
928
Alex Elder8e94af82012-07-25 09:32:40 -0500929static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
930{
Alex Elder103a1502012-08-02 11:29:45 -0500931 size_t size;
932 u32 snap_count;
933
934 /* The header has to start with the magic rbd header text */
935 if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT)))
936 return false;
937
Alex Elderdb2388b2012-10-20 22:17:27 -0500938 /* The bio layer requires at least sector-sized I/O */
939
940 if (ondisk->options.order < SECTOR_SHIFT)
941 return false;
942
943 /* If we use u64 in a few spots we may be able to loosen this */
944
945 if (ondisk->options.order > 8 * sizeof (int) - 1)
946 return false;
947
Alex Elder103a1502012-08-02 11:29:45 -0500948 /*
949 * The size of a snapshot header has to fit in a size_t, and
950 * that limits the number of snapshots.
951 */
952 snap_count = le32_to_cpu(ondisk->snap_count);
953 size = SIZE_MAX - sizeof (struct ceph_snap_context);
954 if (snap_count > size / sizeof (__le64))
955 return false;
956
957 /*
958 * Not only that, but the size of the entire the snapshot
959 * header must also be representable in a size_t.
960 */
961 size -= snap_count * sizeof (__le64);
962 if ((u64) size < le64_to_cpu(ondisk->snap_names_len))
963 return false;
964
965 return true;
Alex Elder8e94af82012-07-25 09:32:40 -0500966}
967
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700968/*
Alex Elderbb23e372013-05-06 09:51:29 -0500969 * Fill an rbd image header with information from the given format 1
970 * on-disk header.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700971 */
Alex Elder662518b2013-05-06 09:51:29 -0500972static int rbd_header_from_disk(struct rbd_device *rbd_dev,
Alex Elder4156d992012-08-02 11:29:46 -0500973 struct rbd_image_header_ondisk *ondisk)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700974{
Alex Elder662518b2013-05-06 09:51:29 -0500975 struct rbd_image_header *header = &rbd_dev->header;
Alex Elderbb23e372013-05-06 09:51:29 -0500976 bool first_time = header->object_prefix == NULL;
977 struct ceph_snap_context *snapc;
978 char *object_prefix = NULL;
979 char *snap_names = NULL;
980 u64 *snap_sizes = NULL;
Alex Elderccece232012-07-10 20:30:10 -0500981 u32 snap_count;
Alex Elderd2bb24e2012-07-26 23:37:14 -0500982 size_t size;
Alex Elderbb23e372013-05-06 09:51:29 -0500983 int ret = -ENOMEM;
Alex Elder621901d2012-08-23 23:22:06 -0500984 u32 i;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700985
Alex Elderbb23e372013-05-06 09:51:29 -0500986 /* Allocate this now to avoid having to handle failure below */
987
988 if (first_time) {
989 size_t len;
990
991 len = strnlen(ondisk->object_prefix,
992 sizeof (ondisk->object_prefix));
993 object_prefix = kmalloc(len + 1, GFP_KERNEL);
994 if (!object_prefix)
995 return -ENOMEM;
996 memcpy(object_prefix, ondisk->object_prefix, len);
997 object_prefix[len] = '\0';
998 }
999
1000 /* Allocate the snapshot context and fill it in */
Alex Elder6a523252012-07-19 17:12:59 -05001001
Alex Elder103a1502012-08-02 11:29:45 -05001002 snap_count = le32_to_cpu(ondisk->snap_count);
Alex Elderbb23e372013-05-06 09:51:29 -05001003 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
1004 if (!snapc)
1005 goto out_err;
1006 snapc->seq = le64_to_cpu(ondisk->snap_seq);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001007 if (snap_count) {
Alex Elderbb23e372013-05-06 09:51:29 -05001008 struct rbd_image_snap_ondisk *snaps;
Alex Elderf785cc12012-08-23 23:22:06 -05001009 u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len);
1010
Alex Elderbb23e372013-05-06 09:51:29 -05001011 /* We'll keep a copy of the snapshot names... */
Alex Elder621901d2012-08-23 23:22:06 -05001012
Alex Elderbb23e372013-05-06 09:51:29 -05001013 if (snap_names_len > (u64)SIZE_MAX)
1014 goto out_2big;
1015 snap_names = kmalloc(snap_names_len, GFP_KERNEL);
1016 if (!snap_names)
Alex Elder6a523252012-07-19 17:12:59 -05001017 goto out_err;
Alex Elderbb23e372013-05-06 09:51:29 -05001018
1019 /* ...as well as the array of their sizes. */
1020
1021 size = snap_count * sizeof (*header->snap_sizes);
1022 snap_sizes = kmalloc(size, GFP_KERNEL);
1023 if (!snap_sizes)
1024 goto out_err;
1025
Alex Elderf785cc12012-08-23 23:22:06 -05001026 /*
Alex Elderbb23e372013-05-06 09:51:29 -05001027 * Copy the names, and fill in each snapshot's id
1028 * and size.
1029 *
Alex Elder99a41eb2013-05-06 09:51:30 -05001030 * Note that rbd_dev_v1_header_info() guarantees the
Alex Elderbb23e372013-05-06 09:51:29 -05001031 * ondisk buffer we're working with has
Alex Elderf785cc12012-08-23 23:22:06 -05001032 * snap_names_len bytes beyond the end of the
1033 * snapshot id array, this memcpy() is safe.
1034 */
Alex Elderbb23e372013-05-06 09:51:29 -05001035 memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len);
1036 snaps = ondisk->snaps;
1037 for (i = 0; i < snap_count; i++) {
1038 snapc->snaps[i] = le64_to_cpu(snaps[i].id);
1039 snap_sizes[i] = le64_to_cpu(snaps[i].image_size);
1040 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001041 }
Alex Elder849b4262012-07-09 21:04:24 -05001042
Alex Elderbb23e372013-05-06 09:51:29 -05001043 /* We won't fail any more, fill in the header */
Alex Elder6a523252012-07-19 17:12:59 -05001044
Alex Elderbb23e372013-05-06 09:51:29 -05001045 if (first_time) {
1046 header->object_prefix = object_prefix;
1047 header->obj_order = ondisk->options.order;
1048 header->crypt_type = ondisk->options.crypt_type;
1049 header->comp_type = ondisk->options.comp_type;
1050 /* The rest aren't used for format 1 images */
1051 header->stripe_unit = 0;
1052 header->stripe_count = 0;
1053 header->features = 0;
Alex Elder662518b2013-05-06 09:51:29 -05001054 } else {
1055 ceph_put_snap_context(header->snapc);
1056 kfree(header->snap_names);
1057 kfree(header->snap_sizes);
Alex Elderbb23e372013-05-06 09:51:29 -05001058 }
1059
1060 /* The remaining fields always get updated (when we refresh) */
Alex Elder621901d2012-08-23 23:22:06 -05001061
Alex Elderf84344f2012-08-31 17:29:51 -05001062 header->image_size = le64_to_cpu(ondisk->image_size);
Alex Elderbb23e372013-05-06 09:51:29 -05001063 header->snapc = snapc;
1064 header->snap_names = snap_names;
1065 header->snap_sizes = snap_sizes;
Alex Elder468521c2013-04-26 09:43:47 -05001066
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001067 return 0;
Alex Elderbb23e372013-05-06 09:51:29 -05001068out_2big:
1069 ret = -EIO;
Alex Elder6a523252012-07-19 17:12:59 -05001070out_err:
Alex Elderbb23e372013-05-06 09:51:29 -05001071 kfree(snap_sizes);
1072 kfree(snap_names);
1073 ceph_put_snap_context(snapc);
1074 kfree(object_prefix);
Alex Elderccece232012-07-10 20:30:10 -05001075
Alex Elderbb23e372013-05-06 09:51:29 -05001076 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001077}
1078
Alex Elder9682fc62013-04-30 00:44:33 -05001079static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which)
1080{
1081 const char *snap_name;
1082
1083 rbd_assert(which < rbd_dev->header.snapc->num_snaps);
1084
1085 /* Skip over names until we find the one we are looking for */
1086
1087 snap_name = rbd_dev->header.snap_names;
1088 while (which--)
1089 snap_name += strlen(snap_name) + 1;
1090
1091 return kstrdup(snap_name, GFP_KERNEL);
1092}
1093
Alex Elder30d1cff2013-05-01 12:43:03 -05001094/*
1095 * Snapshot id comparison function for use with qsort()/bsearch().
1096 * Note that result is for snapshots in *descending* order.
1097 */
1098static int snapid_compare_reverse(const void *s1, const void *s2)
1099{
1100 u64 snap_id1 = *(u64 *)s1;
1101 u64 snap_id2 = *(u64 *)s2;
1102
1103 if (snap_id1 < snap_id2)
1104 return 1;
1105 return snap_id1 == snap_id2 ? 0 : -1;
1106}
1107
1108/*
1109 * Search a snapshot context to see if the given snapshot id is
1110 * present.
1111 *
1112 * Returns the position of the snapshot id in the array if it's found,
1113 * or BAD_SNAP_INDEX otherwise.
1114 *
1115 * Note: The snapshot array is in kept sorted (by the osd) in
1116 * reverse order, highest snapshot id first.
1117 */
Alex Elder9682fc62013-04-30 00:44:33 -05001118static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id)
1119{
1120 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
Alex Elder30d1cff2013-05-01 12:43:03 -05001121 u64 *found;
Alex Elder9682fc62013-04-30 00:44:33 -05001122
Alex Elder30d1cff2013-05-01 12:43:03 -05001123 found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps,
1124 sizeof (snap_id), snapid_compare_reverse);
Alex Elder9682fc62013-04-30 00:44:33 -05001125
Alex Elder30d1cff2013-05-01 12:43:03 -05001126 return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX;
Alex Elder9682fc62013-04-30 00:44:33 -05001127}
1128
Alex Elder2ad3d712013-04-30 00:44:33 -05001129static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev,
1130 u64 snap_id)
Alex Elder54cac612013-04-30 00:44:33 -05001131{
1132 u32 which;
Josh Durginda6a6b62013-09-04 17:57:31 -07001133 const char *snap_name;
Alex Elder54cac612013-04-30 00:44:33 -05001134
1135 which = rbd_dev_snap_index(rbd_dev, snap_id);
1136 if (which == BAD_SNAP_INDEX)
Josh Durginda6a6b62013-09-04 17:57:31 -07001137 return ERR_PTR(-ENOENT);
Alex Elder54cac612013-04-30 00:44:33 -05001138
Josh Durginda6a6b62013-09-04 17:57:31 -07001139 snap_name = _rbd_dev_v1_snap_name(rbd_dev, which);
1140 return snap_name ? snap_name : ERR_PTR(-ENOMEM);
Alex Elder54cac612013-04-30 00:44:33 -05001141}
1142
Alex Elder9e15b772012-10-30 19:40:33 -05001143static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id)
1144{
Alex Elder9e15b772012-10-30 19:40:33 -05001145 if (snap_id == CEPH_NOSNAP)
1146 return RBD_SNAP_HEAD_NAME;
1147
Alex Elder54cac612013-04-30 00:44:33 -05001148 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1149 if (rbd_dev->image_format == 1)
1150 return rbd_dev_v1_snap_name(rbd_dev, snap_id);
Alex Elder9e15b772012-10-30 19:40:33 -05001151
Alex Elder54cac612013-04-30 00:44:33 -05001152 return rbd_dev_v2_snap_name(rbd_dev, snap_id);
Alex Elder9e15b772012-10-30 19:40:33 -05001153}
1154
Alex Elder2ad3d712013-04-30 00:44:33 -05001155static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
1156 u64 *snap_size)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001157{
Alex Elder2ad3d712013-04-30 00:44:33 -05001158 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1159 if (snap_id == CEPH_NOSNAP) {
1160 *snap_size = rbd_dev->header.image_size;
1161 } else if (rbd_dev->image_format == 1) {
1162 u32 which;
Alex Elder00f1f362012-02-07 12:03:36 -06001163
Alex Elder2ad3d712013-04-30 00:44:33 -05001164 which = rbd_dev_snap_index(rbd_dev, snap_id);
1165 if (which == BAD_SNAP_INDEX)
1166 return -ENOENT;
Alex Elder00f1f362012-02-07 12:03:36 -06001167
Alex Elder2ad3d712013-04-30 00:44:33 -05001168 *snap_size = rbd_dev->header.snap_sizes[which];
1169 } else {
1170 u64 size = 0;
1171 int ret;
1172
1173 ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size);
1174 if (ret)
1175 return ret;
1176
1177 *snap_size = size;
1178 }
1179 return 0;
1180}
1181
1182static int rbd_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
1183 u64 *snap_features)
1184{
1185 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1186 if (snap_id == CEPH_NOSNAP) {
1187 *snap_features = rbd_dev->header.features;
1188 } else if (rbd_dev->image_format == 1) {
1189 *snap_features = 0; /* No features for format 1 */
1190 } else {
1191 u64 features = 0;
1192 int ret;
1193
1194 ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, &features);
1195 if (ret)
1196 return ret;
1197
1198 *snap_features = features;
1199 }
1200 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001201}
1202
Alex Elderd1cf5782013-04-27 09:59:30 -05001203static int rbd_dev_mapping_set(struct rbd_device *rbd_dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001204{
Alex Elder8f4b7d92013-05-06 07:40:30 -05001205 u64 snap_id = rbd_dev->spec->snap_id;
Alex Elder2ad3d712013-04-30 00:44:33 -05001206 u64 size = 0;
1207 u64 features = 0;
1208 int ret;
Alex Elder8b0241f2013-04-25 23:15:08 -05001209
Alex Elder2ad3d712013-04-30 00:44:33 -05001210 ret = rbd_snap_size(rbd_dev, snap_id, &size);
1211 if (ret)
1212 return ret;
1213 ret = rbd_snap_features(rbd_dev, snap_id, &features);
1214 if (ret)
1215 return ret;
1216
1217 rbd_dev->mapping.size = size;
1218 rbd_dev->mapping.features = features;
1219
Alex Elder8b0241f2013-04-25 23:15:08 -05001220 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001221}
1222
Alex Elderd1cf5782013-04-27 09:59:30 -05001223static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev)
1224{
1225 rbd_dev->mapping.size = 0;
1226 rbd_dev->mapping.features = 0;
Alex Elder200a6a82013-04-28 23:32:34 -05001227}
1228
Himangi Saraogi7d5079a2014-07-24 03:17:07 +05301229static void rbd_segment_name_free(const char *name)
1230{
1231 /* The explicit cast here is needed to drop the const qualifier */
1232
1233 kmem_cache_free(rbd_segment_name_cache, (void *)name);
1234}
1235
Alex Elder98571b52013-01-20 14:44:42 -06001236static const char *rbd_segment_name(struct rbd_device *rbd_dev, u64 offset)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001237{
Alex Elder65ccfe22012-08-09 10:33:26 -07001238 char *name;
1239 u64 segment;
1240 int ret;
Josh Durgin3a96d5c2013-06-12 19:15:06 -07001241 char *name_format;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001242
Alex Elder78c2a442013-05-01 12:43:04 -05001243 name = kmem_cache_alloc(rbd_segment_name_cache, GFP_NOIO);
Alex Elder65ccfe22012-08-09 10:33:26 -07001244 if (!name)
1245 return NULL;
1246 segment = offset >> rbd_dev->header.obj_order;
Josh Durgin3a96d5c2013-06-12 19:15:06 -07001247 name_format = "%s.%012llx";
1248 if (rbd_dev->image_format == 2)
1249 name_format = "%s.%016llx";
Ilya Dryomov2d0ebc52014-01-27 17:40:18 +02001250 ret = snprintf(name, CEPH_MAX_OID_NAME_LEN + 1, name_format,
Alex Elder65ccfe22012-08-09 10:33:26 -07001251 rbd_dev->header.object_prefix, segment);
Ilya Dryomov2d0ebc52014-01-27 17:40:18 +02001252 if (ret < 0 || ret > CEPH_MAX_OID_NAME_LEN) {
Alex Elder65ccfe22012-08-09 10:33:26 -07001253 pr_err("error formatting segment name for #%llu (%d)\n",
1254 segment, ret);
Himangi Saraogi7d5079a2014-07-24 03:17:07 +05301255 rbd_segment_name_free(name);
Alex Elder65ccfe22012-08-09 10:33:26 -07001256 name = NULL;
1257 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001258
Alex Elder65ccfe22012-08-09 10:33:26 -07001259 return name;
1260}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001261
Alex Elder65ccfe22012-08-09 10:33:26 -07001262static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset)
1263{
1264 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001265
Alex Elder65ccfe22012-08-09 10:33:26 -07001266 return offset & (segment_size - 1);
1267}
1268
1269static u64 rbd_segment_length(struct rbd_device *rbd_dev,
1270 u64 offset, u64 length)
1271{
1272 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
1273
1274 offset &= segment_size - 1;
1275
Alex Elderaafb2302012-09-06 16:00:54 -05001276 rbd_assert(length <= U64_MAX - offset);
Alex Elder65ccfe22012-08-09 10:33:26 -07001277 if (offset + length > segment_size)
1278 length = segment_size - offset;
1279
1280 return length;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001281}
1282
1283/*
Josh Durgin029bcbd2011-07-22 11:35:23 -07001284 * returns the size of an object in the image
1285 */
1286static u64 rbd_obj_bytes(struct rbd_image_header *header)
1287{
1288 return 1 << header->obj_order;
1289}
1290
1291/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001292 * bio helpers
1293 */
1294
1295static void bio_chain_put(struct bio *chain)
1296{
1297 struct bio *tmp;
1298
1299 while (chain) {
1300 tmp = chain;
1301 chain = chain->bi_next;
1302 bio_put(tmp);
1303 }
1304}
1305
1306/*
1307 * zeros a bio chain, starting at specific offset
1308 */
1309static void zero_bio_chain(struct bio *chain, int start_ofs)
1310{
Kent Overstreet79886132013-11-23 17:19:00 -08001311 struct bio_vec bv;
1312 struct bvec_iter iter;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001313 unsigned long flags;
1314 void *buf;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001315 int pos = 0;
1316
1317 while (chain) {
Kent Overstreet79886132013-11-23 17:19:00 -08001318 bio_for_each_segment(bv, chain, iter) {
1319 if (pos + bv.bv_len > start_ofs) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001320 int remainder = max(start_ofs - pos, 0);
Kent Overstreet79886132013-11-23 17:19:00 -08001321 buf = bvec_kmap_irq(&bv, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001322 memset(buf + remainder, 0,
Kent Overstreet79886132013-11-23 17:19:00 -08001323 bv.bv_len - remainder);
1324 flush_dcache_page(bv.bv_page);
Dan Carpenter85b5aaa2010-10-11 21:15:11 +02001325 bvec_kunmap_irq(buf, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001326 }
Kent Overstreet79886132013-11-23 17:19:00 -08001327 pos += bv.bv_len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001328 }
1329
1330 chain = chain->bi_next;
1331 }
1332}
1333
1334/*
Alex Elderb9434c52013-04-19 15:34:50 -05001335 * similar to zero_bio_chain(), zeros data defined by a page array,
1336 * starting at the given byte offset from the start of the array and
1337 * continuing up to the given end offset. The pages array is
1338 * assumed to be big enough to hold all bytes up to the end.
1339 */
1340static void zero_pages(struct page **pages, u64 offset, u64 end)
1341{
1342 struct page **page = &pages[offset >> PAGE_SHIFT];
1343
1344 rbd_assert(end > offset);
1345 rbd_assert(end - offset <= (u64)SIZE_MAX);
1346 while (offset < end) {
1347 size_t page_offset;
1348 size_t length;
1349 unsigned long flags;
1350 void *kaddr;
1351
Geert Uytterhoeven491205a2013-05-13 20:35:37 -05001352 page_offset = offset & ~PAGE_MASK;
1353 length = min_t(size_t, PAGE_SIZE - page_offset, end - offset);
Alex Elderb9434c52013-04-19 15:34:50 -05001354 local_irq_save(flags);
1355 kaddr = kmap_atomic(*page);
1356 memset(kaddr + page_offset, 0, length);
Alex Eldere2156052013-05-22 20:54:25 -05001357 flush_dcache_page(*page);
Alex Elderb9434c52013-04-19 15:34:50 -05001358 kunmap_atomic(kaddr);
1359 local_irq_restore(flags);
1360
1361 offset += length;
1362 page++;
1363 }
1364}
1365
1366/*
Alex Elderf7760da2012-10-20 22:17:27 -05001367 * Clone a portion of a bio, starting at the given byte offset
1368 * and continuing for the number of bytes indicated.
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001369 */
Alex Elderf7760da2012-10-20 22:17:27 -05001370static struct bio *bio_clone_range(struct bio *bio_src,
1371 unsigned int offset,
1372 unsigned int len,
1373 gfp_t gfpmask)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001374{
Alex Elderf7760da2012-10-20 22:17:27 -05001375 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001376
Kent Overstreet5341a622013-08-07 14:31:11 -07001377 bio = bio_clone(bio_src, gfpmask);
Alex Elderf7760da2012-10-20 22:17:27 -05001378 if (!bio)
1379 return NULL; /* ENOMEM */
1380
Kent Overstreet5341a622013-08-07 14:31:11 -07001381 bio_advance(bio, offset);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001382 bio->bi_iter.bi_size = len;
Alex Elder542582f2012-08-09 10:33:25 -07001383
Alex Elderf7760da2012-10-20 22:17:27 -05001384 return bio;
1385}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001386
Alex Elderf7760da2012-10-20 22:17:27 -05001387/*
1388 * Clone a portion of a bio chain, starting at the given byte offset
1389 * into the first bio in the source chain and continuing for the
1390 * number of bytes indicated. The result is another bio chain of
1391 * exactly the given length, or a null pointer on error.
1392 *
1393 * The bio_src and offset parameters are both in-out. On entry they
1394 * refer to the first source bio and the offset into that bio where
1395 * the start of data to be cloned is located.
1396 *
1397 * On return, bio_src is updated to refer to the bio in the source
1398 * chain that contains first un-cloned byte, and *offset will
1399 * contain the offset of that byte within that bio.
1400 */
1401static struct bio *bio_chain_clone_range(struct bio **bio_src,
1402 unsigned int *offset,
1403 unsigned int len,
1404 gfp_t gfpmask)
1405{
1406 struct bio *bi = *bio_src;
1407 unsigned int off = *offset;
1408 struct bio *chain = NULL;
1409 struct bio **end;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001410
Alex Elderf7760da2012-10-20 22:17:27 -05001411 /* Build up a chain of clone bios up to the limit */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001412
Kent Overstreet4f024f32013-10-11 15:44:27 -07001413 if (!bi || off >= bi->bi_iter.bi_size || !len)
Alex Elderf7760da2012-10-20 22:17:27 -05001414 return NULL; /* Nothing to clone */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001415
Alex Elderf7760da2012-10-20 22:17:27 -05001416 end = &chain;
1417 while (len) {
1418 unsigned int bi_size;
1419 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001420
Alex Elderf5400b72012-11-01 10:17:15 -05001421 if (!bi) {
1422 rbd_warn(NULL, "bio_chain exhausted with %u left", len);
Alex Elderf7760da2012-10-20 22:17:27 -05001423 goto out_err; /* EINVAL; ran out of bio's */
Alex Elderf5400b72012-11-01 10:17:15 -05001424 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07001425 bi_size = min_t(unsigned int, bi->bi_iter.bi_size - off, len);
Alex Elderf7760da2012-10-20 22:17:27 -05001426 bio = bio_clone_range(bi, off, bi_size, gfpmask);
1427 if (!bio)
1428 goto out_err; /* ENOMEM */
1429
1430 *end = bio;
1431 end = &bio->bi_next;
1432
1433 off += bi_size;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001434 if (off == bi->bi_iter.bi_size) {
Alex Elderf7760da2012-10-20 22:17:27 -05001435 bi = bi->bi_next;
1436 off = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001437 }
Alex Elderf7760da2012-10-20 22:17:27 -05001438 len -= bi_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001439 }
Alex Elderf7760da2012-10-20 22:17:27 -05001440 *bio_src = bi;
1441 *offset = off;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001442
Alex Elderf7760da2012-10-20 22:17:27 -05001443 return chain;
1444out_err:
1445 bio_chain_put(chain);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001446
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001447 return NULL;
1448}
1449
Alex Elder926f9b32013-02-11 12:33:24 -06001450/*
1451 * The default/initial value for all object request flags is 0. For
1452 * each flag, once its value is set to 1 it is never reset to 0
1453 * again.
1454 */
Alex Elder6365d332013-02-11 12:33:24 -06001455static void obj_request_img_data_set(struct rbd_obj_request *obj_request)
1456{
1457 if (test_and_set_bit(OBJ_REQ_IMG_DATA, &obj_request->flags)) {
Alex Elder6365d332013-02-11 12:33:24 -06001458 struct rbd_device *rbd_dev;
1459
Alex Elder57acbaa2013-02-11 12:33:24 -06001460 rbd_dev = obj_request->img_request->rbd_dev;
Ilya Dryomov9584d502014-07-11 12:11:20 +04001461 rbd_warn(rbd_dev, "obj_request %p already marked img_data",
Alex Elder6365d332013-02-11 12:33:24 -06001462 obj_request);
1463 }
1464}
1465
1466static bool obj_request_img_data_test(struct rbd_obj_request *obj_request)
1467{
1468 smp_mb();
1469 return test_bit(OBJ_REQ_IMG_DATA, &obj_request->flags) != 0;
1470}
1471
Alex Elder57acbaa2013-02-11 12:33:24 -06001472static void obj_request_done_set(struct rbd_obj_request *obj_request)
1473{
1474 if (test_and_set_bit(OBJ_REQ_DONE, &obj_request->flags)) {
1475 struct rbd_device *rbd_dev = NULL;
1476
1477 if (obj_request_img_data_test(obj_request))
1478 rbd_dev = obj_request->img_request->rbd_dev;
Ilya Dryomov9584d502014-07-11 12:11:20 +04001479 rbd_warn(rbd_dev, "obj_request %p already marked done",
Alex Elder57acbaa2013-02-11 12:33:24 -06001480 obj_request);
1481 }
1482}
1483
1484static bool obj_request_done_test(struct rbd_obj_request *obj_request)
1485{
1486 smp_mb();
1487 return test_bit(OBJ_REQ_DONE, &obj_request->flags) != 0;
1488}
1489
Alex Elder5679c592013-02-11 12:33:24 -06001490/*
1491 * This sets the KNOWN flag after (possibly) setting the EXISTS
1492 * flag. The latter is set based on the "exists" value provided.
1493 *
1494 * Note that for our purposes once an object exists it never goes
1495 * away again. It's possible that the response from two existence
1496 * checks are separated by the creation of the target object, and
1497 * the first ("doesn't exist") response arrives *after* the second
1498 * ("does exist"). In that case we ignore the second one.
1499 */
1500static void obj_request_existence_set(struct rbd_obj_request *obj_request,
1501 bool exists)
1502{
1503 if (exists)
1504 set_bit(OBJ_REQ_EXISTS, &obj_request->flags);
1505 set_bit(OBJ_REQ_KNOWN, &obj_request->flags);
1506 smp_mb();
1507}
1508
1509static bool obj_request_known_test(struct rbd_obj_request *obj_request)
1510{
1511 smp_mb();
1512 return test_bit(OBJ_REQ_KNOWN, &obj_request->flags) != 0;
1513}
1514
1515static bool obj_request_exists_test(struct rbd_obj_request *obj_request)
1516{
1517 smp_mb();
1518 return test_bit(OBJ_REQ_EXISTS, &obj_request->flags) != 0;
1519}
1520
Ilya Dryomov96385562014-06-10 13:53:29 +04001521static bool obj_request_overlaps_parent(struct rbd_obj_request *obj_request)
1522{
1523 struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev;
1524
1525 return obj_request->img_offset <
1526 round_up(rbd_dev->parent_overlap, rbd_obj_bytes(&rbd_dev->header));
1527}
1528
Alex Elderbf0d5f502012-11-22 00:00:08 -06001529static void rbd_obj_request_get(struct rbd_obj_request *obj_request)
1530{
Alex Elder37206ee2013-02-20 17:32:08 -06001531 dout("%s: obj %p (was %d)\n", __func__, obj_request,
1532 atomic_read(&obj_request->kref.refcount));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001533 kref_get(&obj_request->kref);
1534}
1535
1536static void rbd_obj_request_destroy(struct kref *kref);
1537static void rbd_obj_request_put(struct rbd_obj_request *obj_request)
1538{
1539 rbd_assert(obj_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001540 dout("%s: obj %p (was %d)\n", __func__, obj_request,
1541 atomic_read(&obj_request->kref.refcount));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001542 kref_put(&obj_request->kref, rbd_obj_request_destroy);
1543}
1544
Alex Elder0f2d5be2014-04-26 14:21:44 +04001545static void rbd_img_request_get(struct rbd_img_request *img_request)
1546{
1547 dout("%s: img %p (was %d)\n", __func__, img_request,
1548 atomic_read(&img_request->kref.refcount));
1549 kref_get(&img_request->kref);
1550}
1551
Alex Eldere93f3152013-05-08 22:50:04 -05001552static bool img_request_child_test(struct rbd_img_request *img_request);
1553static void rbd_parent_request_destroy(struct kref *kref);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001554static void rbd_img_request_destroy(struct kref *kref);
1555static void rbd_img_request_put(struct rbd_img_request *img_request)
1556{
1557 rbd_assert(img_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001558 dout("%s: img %p (was %d)\n", __func__, img_request,
1559 atomic_read(&img_request->kref.refcount));
Alex Eldere93f3152013-05-08 22:50:04 -05001560 if (img_request_child_test(img_request))
1561 kref_put(&img_request->kref, rbd_parent_request_destroy);
1562 else
1563 kref_put(&img_request->kref, rbd_img_request_destroy);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001564}
1565
1566static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request,
1567 struct rbd_obj_request *obj_request)
1568{
Alex Elder25dcf952013-01-25 17:08:55 -06001569 rbd_assert(obj_request->img_request == NULL);
1570
Alex Elderb155e862013-04-15 14:50:37 -05001571 /* Image request now owns object's original reference */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001572 obj_request->img_request = img_request;
Alex Elder25dcf952013-01-25 17:08:55 -06001573 obj_request->which = img_request->obj_request_count;
Alex Elder6365d332013-02-11 12:33:24 -06001574 rbd_assert(!obj_request_img_data_test(obj_request));
1575 obj_request_img_data_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001576 rbd_assert(obj_request->which != BAD_WHICH);
Alex Elder25dcf952013-01-25 17:08:55 -06001577 img_request->obj_request_count++;
1578 list_add_tail(&obj_request->links, &img_request->obj_requests);
Alex Elder37206ee2013-02-20 17:32:08 -06001579 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1580 obj_request->which);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001581}
1582
1583static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request,
1584 struct rbd_obj_request *obj_request)
1585{
1586 rbd_assert(obj_request->which != BAD_WHICH);
Alex Elder25dcf952013-01-25 17:08:55 -06001587
Alex Elder37206ee2013-02-20 17:32:08 -06001588 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1589 obj_request->which);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001590 list_del(&obj_request->links);
Alex Elder25dcf952013-01-25 17:08:55 -06001591 rbd_assert(img_request->obj_request_count > 0);
1592 img_request->obj_request_count--;
1593 rbd_assert(obj_request->which == img_request->obj_request_count);
1594 obj_request->which = BAD_WHICH;
Alex Elder6365d332013-02-11 12:33:24 -06001595 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001596 rbd_assert(obj_request->img_request == img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001597 obj_request->img_request = NULL;
Alex Elder25dcf952013-01-25 17:08:55 -06001598 obj_request->callback = NULL;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001599 rbd_obj_request_put(obj_request);
1600}
1601
1602static bool obj_request_type_valid(enum obj_request_type type)
1603{
1604 switch (type) {
Alex Elder9969ebc2013-01-18 12:31:10 -06001605 case OBJ_REQUEST_NODATA:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001606 case OBJ_REQUEST_BIO:
Alex Elder788e2df2013-01-17 12:25:27 -06001607 case OBJ_REQUEST_PAGES:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001608 return true;
1609 default:
1610 return false;
1611 }
1612}
1613
Alex Elderbf0d5f502012-11-22 00:00:08 -06001614static int rbd_obj_request_submit(struct ceph_osd_client *osdc,
1615 struct rbd_obj_request *obj_request)
1616{
Ilya Dryomov71c20a02014-06-19 11:38:14 +04001617 dout("%s %p\n", __func__, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001618 return ceph_osdc_start_request(osdc, obj_request->osd_req, false);
1619}
1620
Ilya Dryomov71c20a02014-06-19 11:38:14 +04001621static void rbd_obj_request_end(struct rbd_obj_request *obj_request)
1622{
1623 dout("%s %p\n", __func__, obj_request);
1624 ceph_osdc_cancel_request(obj_request->osd_req);
1625}
1626
1627/*
1628 * Wait for an object request to complete. If interrupted, cancel the
1629 * underlying osd request.
Ilya Dryomov2894e1d2015-05-12 19:53:24 +03001630 *
1631 * @timeout: in jiffies, 0 means "wait forever"
Ilya Dryomov71c20a02014-06-19 11:38:14 +04001632 */
Ilya Dryomov2894e1d2015-05-12 19:53:24 +03001633static int __rbd_obj_request_wait(struct rbd_obj_request *obj_request,
1634 unsigned long timeout)
Ilya Dryomov71c20a02014-06-19 11:38:14 +04001635{
Ilya Dryomov2894e1d2015-05-12 19:53:24 +03001636 long ret;
Ilya Dryomov71c20a02014-06-19 11:38:14 +04001637
1638 dout("%s %p\n", __func__, obj_request);
Ilya Dryomov2894e1d2015-05-12 19:53:24 +03001639 ret = wait_for_completion_interruptible_timeout(
1640 &obj_request->completion,
1641 ceph_timeout_jiffies(timeout));
1642 if (ret <= 0) {
1643 if (ret == 0)
1644 ret = -ETIMEDOUT;
Ilya Dryomov71c20a02014-06-19 11:38:14 +04001645 rbd_obj_request_end(obj_request);
Ilya Dryomov2894e1d2015-05-12 19:53:24 +03001646 } else {
1647 ret = 0;
Ilya Dryomov71c20a02014-06-19 11:38:14 +04001648 }
1649
Ilya Dryomov2894e1d2015-05-12 19:53:24 +03001650 dout("%s %p ret %d\n", __func__, obj_request, (int)ret);
1651 return ret;
1652}
1653
1654static int rbd_obj_request_wait(struct rbd_obj_request *obj_request)
1655{
1656 return __rbd_obj_request_wait(obj_request, 0);
1657}
1658
Alex Elderbf0d5f502012-11-22 00:00:08 -06001659static void rbd_img_request_complete(struct rbd_img_request *img_request)
1660{
Alex Elder55f27e02013-04-10 12:34:25 -05001661
Alex Elder37206ee2013-02-20 17:32:08 -06001662 dout("%s: img %p\n", __func__, img_request);
Alex Elder55f27e02013-04-10 12:34:25 -05001663
1664 /*
1665 * If no error occurred, compute the aggregate transfer
1666 * count for the image request. We could instead use
1667 * atomic64_cmpxchg() to update it as each object request
1668 * completes; not clear which way is better off hand.
1669 */
1670 if (!img_request->result) {
1671 struct rbd_obj_request *obj_request;
1672 u64 xferred = 0;
1673
1674 for_each_obj_request(img_request, obj_request)
1675 xferred += obj_request->xferred;
1676 img_request->xferred = xferred;
1677 }
1678
Alex Elderbf0d5f502012-11-22 00:00:08 -06001679 if (img_request->callback)
1680 img_request->callback(img_request);
1681 else
1682 rbd_img_request_put(img_request);
1683}
1684
Alex Elder0c425242013-02-08 09:55:49 -06001685/*
1686 * The default/initial value for all image request flags is 0. Each
1687 * is conditionally set to 1 at image request initialization time
1688 * and currently never change thereafter.
1689 */
1690static void img_request_write_set(struct rbd_img_request *img_request)
1691{
1692 set_bit(IMG_REQ_WRITE, &img_request->flags);
1693 smp_mb();
1694}
1695
1696static bool img_request_write_test(struct rbd_img_request *img_request)
1697{
1698 smp_mb();
1699 return test_bit(IMG_REQ_WRITE, &img_request->flags) != 0;
1700}
1701
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001702/*
1703 * Set the discard flag when the img_request is an discard request
1704 */
1705static void img_request_discard_set(struct rbd_img_request *img_request)
1706{
1707 set_bit(IMG_REQ_DISCARD, &img_request->flags);
1708 smp_mb();
1709}
1710
1711static bool img_request_discard_test(struct rbd_img_request *img_request)
1712{
1713 smp_mb();
1714 return test_bit(IMG_REQ_DISCARD, &img_request->flags) != 0;
1715}
1716
Alex Elder9849e982013-01-24 16:13:36 -06001717static void img_request_child_set(struct rbd_img_request *img_request)
1718{
1719 set_bit(IMG_REQ_CHILD, &img_request->flags);
1720 smp_mb();
1721}
1722
Alex Eldere93f3152013-05-08 22:50:04 -05001723static void img_request_child_clear(struct rbd_img_request *img_request)
1724{
1725 clear_bit(IMG_REQ_CHILD, &img_request->flags);
1726 smp_mb();
1727}
1728
Alex Elder9849e982013-01-24 16:13:36 -06001729static bool img_request_child_test(struct rbd_img_request *img_request)
1730{
1731 smp_mb();
1732 return test_bit(IMG_REQ_CHILD, &img_request->flags) != 0;
1733}
1734
Alex Elderd0b2e942013-01-24 16:13:36 -06001735static void img_request_layered_set(struct rbd_img_request *img_request)
1736{
1737 set_bit(IMG_REQ_LAYERED, &img_request->flags);
1738 smp_mb();
1739}
1740
Alex Eldera2acd002013-05-08 22:50:04 -05001741static void img_request_layered_clear(struct rbd_img_request *img_request)
1742{
1743 clear_bit(IMG_REQ_LAYERED, &img_request->flags);
1744 smp_mb();
1745}
1746
Alex Elderd0b2e942013-01-24 16:13:36 -06001747static bool img_request_layered_test(struct rbd_img_request *img_request)
1748{
1749 smp_mb();
1750 return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0;
1751}
1752
Josh Durgin3b434a2a2014-04-04 17:32:15 -07001753static enum obj_operation_type
1754rbd_img_request_op_type(struct rbd_img_request *img_request)
1755{
1756 if (img_request_write_test(img_request))
1757 return OBJ_OP_WRITE;
1758 else if (img_request_discard_test(img_request))
1759 return OBJ_OP_DISCARD;
1760 else
1761 return OBJ_OP_READ;
1762}
1763
Alex Elder6e2a4502013-03-27 09:16:30 -05001764static void
1765rbd_img_obj_request_read_callback(struct rbd_obj_request *obj_request)
1766{
Alex Elderb9434c52013-04-19 15:34:50 -05001767 u64 xferred = obj_request->xferred;
1768 u64 length = obj_request->length;
1769
Alex Elder6e2a4502013-03-27 09:16:30 -05001770 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1771 obj_request, obj_request->img_request, obj_request->result,
Alex Elderb9434c52013-04-19 15:34:50 -05001772 xferred, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001773 /*
Josh Durgin17c1cc12013-08-26 17:55:38 -07001774 * ENOENT means a hole in the image. We zero-fill the entire
1775 * length of the request. A short read also implies zero-fill
1776 * to the end of the request. An error requires the whole
1777 * length of the request to be reported finished with an error
1778 * to the block layer. In each case we update the xferred
1779 * count to indicate the whole request was satisfied.
Alex Elder6e2a4502013-03-27 09:16:30 -05001780 */
Alex Elderb9434c52013-04-19 15:34:50 -05001781 rbd_assert(obj_request->type != OBJ_REQUEST_NODATA);
Alex Elder6e2a4502013-03-27 09:16:30 -05001782 if (obj_request->result == -ENOENT) {
Alex Elderb9434c52013-04-19 15:34:50 -05001783 if (obj_request->type == OBJ_REQUEST_BIO)
1784 zero_bio_chain(obj_request->bio_list, 0);
1785 else
1786 zero_pages(obj_request->pages, 0, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001787 obj_request->result = 0;
Alex Elderb9434c52013-04-19 15:34:50 -05001788 } else if (xferred < length && !obj_request->result) {
1789 if (obj_request->type == OBJ_REQUEST_BIO)
1790 zero_bio_chain(obj_request->bio_list, xferred);
1791 else
1792 zero_pages(obj_request->pages, xferred, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001793 }
Josh Durgin17c1cc12013-08-26 17:55:38 -07001794 obj_request->xferred = length;
Alex Elder6e2a4502013-03-27 09:16:30 -05001795 obj_request_done_set(obj_request);
1796}
1797
Alex Elderbf0d5f502012-11-22 00:00:08 -06001798static void rbd_obj_request_complete(struct rbd_obj_request *obj_request)
1799{
Alex Elder37206ee2013-02-20 17:32:08 -06001800 dout("%s: obj %p cb %p\n", __func__, obj_request,
1801 obj_request->callback);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001802 if (obj_request->callback)
1803 obj_request->callback(obj_request);
Alex Elder788e2df2013-01-17 12:25:27 -06001804 else
1805 complete_all(&obj_request->completion);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001806}
1807
Alex Elderc47f9372013-02-26 14:23:07 -06001808static void rbd_osd_read_callback(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001809{
Alex Elder57acbaa2013-02-11 12:33:24 -06001810 struct rbd_img_request *img_request = NULL;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001811 struct rbd_device *rbd_dev = NULL;
Alex Elder57acbaa2013-02-11 12:33:24 -06001812 bool layered = false;
1813
1814 if (obj_request_img_data_test(obj_request)) {
1815 img_request = obj_request->img_request;
1816 layered = img_request && img_request_layered_test(img_request);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001817 rbd_dev = img_request->rbd_dev;
Alex Elder57acbaa2013-02-11 12:33:24 -06001818 }
Alex Elder8b3e1a52013-01-24 16:13:36 -06001819
1820 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1821 obj_request, img_request, obj_request->result,
1822 obj_request->xferred, obj_request->length);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001823 if (layered && obj_request->result == -ENOENT &&
1824 obj_request->img_offset < rbd_dev->parent_overlap)
Alex Elder8b3e1a52013-01-24 16:13:36 -06001825 rbd_img_parent_read(obj_request);
1826 else if (img_request)
Alex Elder6e2a4502013-03-27 09:16:30 -05001827 rbd_img_obj_request_read_callback(obj_request);
1828 else
1829 obj_request_done_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001830}
1831
Alex Elderc47f9372013-02-26 14:23:07 -06001832static void rbd_osd_write_callback(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001833{
Sage Weil1b83bef2013-02-25 16:11:12 -08001834 dout("%s: obj %p result %d %llu\n", __func__, obj_request,
1835 obj_request->result, obj_request->length);
1836 /*
Alex Elder8b3e1a52013-01-24 16:13:36 -06001837 * There is no such thing as a successful short write. Set
1838 * it to our originally-requested length.
Sage Weil1b83bef2013-02-25 16:11:12 -08001839 */
1840 obj_request->xferred = obj_request->length;
Alex Elder07741302013-02-05 23:41:50 -06001841 obj_request_done_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001842}
1843
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001844static void rbd_osd_discard_callback(struct rbd_obj_request *obj_request)
1845{
1846 dout("%s: obj %p result %d %llu\n", __func__, obj_request,
1847 obj_request->result, obj_request->length);
1848 /*
1849 * There is no such thing as a successful short discard. Set
1850 * it to our originally-requested length.
1851 */
1852 obj_request->xferred = obj_request->length;
Josh Durgind0265de2014-04-07 16:54:10 -07001853 /* discarding a non-existent object is not a problem */
1854 if (obj_request->result == -ENOENT)
1855 obj_request->result = 0;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001856 obj_request_done_set(obj_request);
1857}
1858
Alex Elderfbfab532013-02-08 09:55:48 -06001859/*
1860 * For a simple stat call there's nothing to do. We'll do more if
1861 * this is part of a write sequence for a layered image.
1862 */
Alex Elderc47f9372013-02-26 14:23:07 -06001863static void rbd_osd_stat_callback(struct rbd_obj_request *obj_request)
Alex Elderfbfab532013-02-08 09:55:48 -06001864{
Alex Elder37206ee2013-02-20 17:32:08 -06001865 dout("%s: obj %p\n", __func__, obj_request);
Alex Elderfbfab532013-02-08 09:55:48 -06001866 obj_request_done_set(obj_request);
1867}
1868
Ilya Dryomov27617132015-07-16 17:36:11 +03001869static void rbd_osd_call_callback(struct rbd_obj_request *obj_request)
1870{
1871 dout("%s: obj %p\n", __func__, obj_request);
1872
1873 if (obj_request_img_data_test(obj_request))
1874 rbd_osd_copyup_callback(obj_request);
1875 else
1876 obj_request_done_set(obj_request);
1877}
1878
Ilya Dryomov85e084f2016-04-28 16:07:24 +02001879static void rbd_osd_req_callback(struct ceph_osd_request *osd_req)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001880{
1881 struct rbd_obj_request *obj_request = osd_req->r_priv;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001882 u16 opcode;
1883
Ilya Dryomov85e084f2016-04-28 16:07:24 +02001884 dout("%s: osd_req %p\n", __func__, osd_req);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001885 rbd_assert(osd_req == obj_request->osd_req);
Alex Elder57acbaa2013-02-11 12:33:24 -06001886 if (obj_request_img_data_test(obj_request)) {
1887 rbd_assert(obj_request->img_request);
1888 rbd_assert(obj_request->which != BAD_WHICH);
1889 } else {
1890 rbd_assert(obj_request->which == BAD_WHICH);
1891 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06001892
Sage Weil1b83bef2013-02-25 16:11:12 -08001893 if (osd_req->r_result < 0)
1894 obj_request->result = osd_req->r_result;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001895
Alex Elderc47f9372013-02-26 14:23:07 -06001896 /*
1897 * We support a 64-bit length, but ultimately it has to be
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01001898 * passed to the block layer, which just supports a 32-bit
1899 * length field.
Alex Elderc47f9372013-02-26 14:23:07 -06001900 */
Yan, Zheng7665d852016-01-07 16:48:57 +08001901 obj_request->xferred = osd_req->r_ops[0].outdata_len;
Alex Elder8b3e1a52013-01-24 16:13:36 -06001902 rbd_assert(obj_request->xferred < (u64)UINT_MAX);
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001903
Alex Elder79528732013-04-03 21:32:51 -05001904 opcode = osd_req->r_ops[0].op;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001905 switch (opcode) {
1906 case CEPH_OSD_OP_READ:
Alex Elderc47f9372013-02-26 14:23:07 -06001907 rbd_osd_read_callback(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001908 break;
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001909 case CEPH_OSD_OP_SETALLOCHINT:
Ilya Dryomove30b7572015-10-07 17:27:17 +02001910 rbd_assert(osd_req->r_ops[1].op == CEPH_OSD_OP_WRITE ||
1911 osd_req->r_ops[1].op == CEPH_OSD_OP_WRITEFULL);
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001912 /* fall through */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001913 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +02001914 case CEPH_OSD_OP_WRITEFULL:
Alex Elderc47f9372013-02-26 14:23:07 -06001915 rbd_osd_write_callback(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001916 break;
Alex Elderfbfab532013-02-08 09:55:48 -06001917 case CEPH_OSD_OP_STAT:
Alex Elderc47f9372013-02-26 14:23:07 -06001918 rbd_osd_stat_callback(obj_request);
Alex Elderfbfab532013-02-08 09:55:48 -06001919 break;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001920 case CEPH_OSD_OP_DELETE:
1921 case CEPH_OSD_OP_TRUNCATE:
1922 case CEPH_OSD_OP_ZERO:
1923 rbd_osd_discard_callback(obj_request);
1924 break;
Alex Elder36be9a72013-01-19 00:30:28 -06001925 case CEPH_OSD_OP_CALL:
Ilya Dryomov27617132015-07-16 17:36:11 +03001926 rbd_osd_call_callback(obj_request);
1927 break;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001928 default:
Ilya Dryomov9584d502014-07-11 12:11:20 +04001929 rbd_warn(NULL, "%s: unsupported op %hu",
Alex Elderbf0d5f502012-11-22 00:00:08 -06001930 obj_request->object_name, (unsigned short) opcode);
1931 break;
1932 }
1933
Alex Elder07741302013-02-05 23:41:50 -06001934 if (obj_request_done_test(obj_request))
Alex Elderbf0d5f502012-11-22 00:00:08 -06001935 rbd_obj_request_complete(obj_request);
1936}
1937
Alex Elder9d4df012013-04-19 15:34:50 -05001938static void rbd_osd_req_format_read(struct rbd_obj_request *obj_request)
Alex Elder430c28c2013-04-03 21:32:51 -05001939{
1940 struct rbd_img_request *img_request = obj_request->img_request;
Alex Elder8c042b02013-04-03 01:28:58 -05001941 struct ceph_osd_request *osd_req = obj_request->osd_req;
Alex Elder430c28c2013-04-03 21:32:51 -05001942
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001943 if (img_request)
1944 osd_req->r_snapid = img_request->snap_id;
Alex Elder9d4df012013-04-19 15:34:50 -05001945}
1946
1947static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request)
1948{
Alex Elder9d4df012013-04-19 15:34:50 -05001949 struct ceph_osd_request *osd_req = obj_request->osd_req;
Alex Elder9d4df012013-04-19 15:34:50 -05001950
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001951 osd_req->r_mtime = CURRENT_TIME;
1952 osd_req->r_data_offset = obj_request->offset;
Alex Elder430c28c2013-04-03 21:32:51 -05001953}
1954
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001955/*
1956 * Create an osd request. A read request has one osd op (read).
1957 * A write request has either one (watch) or two (hint+write) osd ops.
1958 * (All rbd data writes are prefixed with an allocation hint op, but
1959 * technically osd watch is a write request, hence this distinction.)
1960 */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001961static struct ceph_osd_request *rbd_osd_req_create(
1962 struct rbd_device *rbd_dev,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08001963 enum obj_operation_type op_type,
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02001964 unsigned int num_ops,
Alex Elder430c28c2013-04-03 21:32:51 -05001965 struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001966{
Alex Elderbf0d5f502012-11-22 00:00:08 -06001967 struct ceph_snap_context *snapc = NULL;
1968 struct ceph_osd_client *osdc;
1969 struct ceph_osd_request *osd_req;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001970
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001971 if (obj_request_img_data_test(obj_request) &&
1972 (op_type == OBJ_OP_DISCARD || op_type == OBJ_OP_WRITE)) {
Alex Elder6365d332013-02-11 12:33:24 -06001973 struct rbd_img_request *img_request = obj_request->img_request;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001974 if (op_type == OBJ_OP_WRITE) {
1975 rbd_assert(img_request_write_test(img_request));
1976 } else {
1977 rbd_assert(img_request_discard_test(img_request));
1978 }
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08001979 snapc = img_request->snapc;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001980 }
1981
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08001982 rbd_assert(num_ops == 1 || ((op_type == OBJ_OP_WRITE) && num_ops == 2));
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02001983
1984 /* Allocate and initialize the request, for the num_ops ops */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001985
1986 osdc = &rbd_dev->rbd_client->client->osdc;
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02001987 osd_req = ceph_osdc_alloc_request(osdc, snapc, num_ops, false,
David Disseldorp2224d872016-04-05 11:13:39 +02001988 GFP_NOIO);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001989 if (!osd_req)
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02001990 goto fail;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001991
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001992 if (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001993 osd_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK;
Alex Elder430c28c2013-04-03 21:32:51 -05001994 else
Alex Elderbf0d5f502012-11-22 00:00:08 -06001995 osd_req->r_flags = CEPH_OSD_FLAG_READ;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001996
1997 osd_req->r_callback = rbd_osd_req_callback;
1998 osd_req->r_priv = obj_request;
1999
Yan, Zheng76271512016-02-03 21:24:49 +08002000 osd_req->r_base_oloc.pool = rbd_dev->layout.pool_id;
Ilya Dryomovd30291b2016-04-29 19:54:20 +02002001 if (ceph_oid_aprintf(&osd_req->r_base_oid, GFP_NOIO, "%s",
2002 obj_request->object_name))
2003 goto fail;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002004
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02002005 if (ceph_osdc_alloc_messages(osd_req, GFP_NOIO))
2006 goto fail;
2007
Alex Elderbf0d5f502012-11-22 00:00:08 -06002008 return osd_req;
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02002009
2010fail:
2011 ceph_osdc_put_request(osd_req);
2012 return NULL;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002013}
2014
Alex Elder0eefd472013-04-19 15:34:50 -05002015/*
Josh Durgind3246fb2014-04-07 16:49:21 -07002016 * Create a copyup osd request based on the information in the object
2017 * request supplied. A copyup request has two or three osd ops, a
2018 * copyup method call, potentially a hint op, and a write or truncate
2019 * or zero op.
Alex Elder0eefd472013-04-19 15:34:50 -05002020 */
2021static struct ceph_osd_request *
2022rbd_osd_req_create_copyup(struct rbd_obj_request *obj_request)
2023{
2024 struct rbd_img_request *img_request;
2025 struct ceph_snap_context *snapc;
2026 struct rbd_device *rbd_dev;
2027 struct ceph_osd_client *osdc;
2028 struct ceph_osd_request *osd_req;
Josh Durgind3246fb2014-04-07 16:49:21 -07002029 int num_osd_ops = 3;
Alex Elder0eefd472013-04-19 15:34:50 -05002030
2031 rbd_assert(obj_request_img_data_test(obj_request));
2032 img_request = obj_request->img_request;
2033 rbd_assert(img_request);
Josh Durgind3246fb2014-04-07 16:49:21 -07002034 rbd_assert(img_request_write_test(img_request) ||
2035 img_request_discard_test(img_request));
Alex Elder0eefd472013-04-19 15:34:50 -05002036
Josh Durgind3246fb2014-04-07 16:49:21 -07002037 if (img_request_discard_test(img_request))
2038 num_osd_ops = 2;
2039
2040 /* Allocate and initialize the request, for all the ops */
Alex Elder0eefd472013-04-19 15:34:50 -05002041
2042 snapc = img_request->snapc;
2043 rbd_dev = img_request->rbd_dev;
2044 osdc = &rbd_dev->rbd_client->client->osdc;
Josh Durgind3246fb2014-04-07 16:49:21 -07002045 osd_req = ceph_osdc_alloc_request(osdc, snapc, num_osd_ops,
David Disseldorp2224d872016-04-05 11:13:39 +02002046 false, GFP_NOIO);
Alex Elder0eefd472013-04-19 15:34:50 -05002047 if (!osd_req)
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02002048 goto fail;
Alex Elder0eefd472013-04-19 15:34:50 -05002049
2050 osd_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK;
2051 osd_req->r_callback = rbd_osd_req_callback;
2052 osd_req->r_priv = obj_request;
2053
Yan, Zheng76271512016-02-03 21:24:49 +08002054 osd_req->r_base_oloc.pool = rbd_dev->layout.pool_id;
Ilya Dryomovd30291b2016-04-29 19:54:20 +02002055 if (ceph_oid_aprintf(&osd_req->r_base_oid, GFP_NOIO, "%s",
2056 obj_request->object_name))
2057 goto fail;
Alex Elder0eefd472013-04-19 15:34:50 -05002058
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02002059 if (ceph_osdc_alloc_messages(osd_req, GFP_NOIO))
2060 goto fail;
2061
Alex Elder0eefd472013-04-19 15:34:50 -05002062 return osd_req;
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02002063
2064fail:
2065 ceph_osdc_put_request(osd_req);
2066 return NULL;
Alex Elder0eefd472013-04-19 15:34:50 -05002067}
2068
2069
Alex Elderbf0d5f502012-11-22 00:00:08 -06002070static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req)
2071{
2072 ceph_osdc_put_request(osd_req);
2073}
2074
2075/* object_name is assumed to be a non-null pointer and NUL-terminated */
2076
2077static struct rbd_obj_request *rbd_obj_request_create(const char *object_name,
2078 u64 offset, u64 length,
2079 enum obj_request_type type)
2080{
2081 struct rbd_obj_request *obj_request;
2082 size_t size;
2083 char *name;
2084
2085 rbd_assert(obj_request_type_valid(type));
2086
2087 size = strlen(object_name) + 1;
Ilya Dryomov5a60e872015-06-24 17:24:33 +03002088 name = kmalloc(size, GFP_NOIO);
Alex Elderf907ad52013-05-01 12:43:03 -05002089 if (!name)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002090 return NULL;
2091
Ilya Dryomov5a60e872015-06-24 17:24:33 +03002092 obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_NOIO);
Alex Elderf907ad52013-05-01 12:43:03 -05002093 if (!obj_request) {
2094 kfree(name);
2095 return NULL;
2096 }
2097
Alex Elderbf0d5f502012-11-22 00:00:08 -06002098 obj_request->object_name = memcpy(name, object_name, size);
2099 obj_request->offset = offset;
2100 obj_request->length = length;
Alex Elder926f9b32013-02-11 12:33:24 -06002101 obj_request->flags = 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002102 obj_request->which = BAD_WHICH;
2103 obj_request->type = type;
2104 INIT_LIST_HEAD(&obj_request->links);
Alex Elder788e2df2013-01-17 12:25:27 -06002105 init_completion(&obj_request->completion);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002106 kref_init(&obj_request->kref);
2107
Alex Elder37206ee2013-02-20 17:32:08 -06002108 dout("%s: \"%s\" %llu/%llu %d -> obj %p\n", __func__, object_name,
2109 offset, length, (int)type, obj_request);
2110
Alex Elderbf0d5f502012-11-22 00:00:08 -06002111 return obj_request;
2112}
2113
2114static void rbd_obj_request_destroy(struct kref *kref)
2115{
2116 struct rbd_obj_request *obj_request;
2117
2118 obj_request = container_of(kref, struct rbd_obj_request, kref);
2119
Alex Elder37206ee2013-02-20 17:32:08 -06002120 dout("%s: obj %p\n", __func__, obj_request);
2121
Alex Elderbf0d5f502012-11-22 00:00:08 -06002122 rbd_assert(obj_request->img_request == NULL);
2123 rbd_assert(obj_request->which == BAD_WHICH);
2124
2125 if (obj_request->osd_req)
2126 rbd_osd_req_destroy(obj_request->osd_req);
2127
2128 rbd_assert(obj_request_type_valid(obj_request->type));
2129 switch (obj_request->type) {
Alex Elder9969ebc2013-01-18 12:31:10 -06002130 case OBJ_REQUEST_NODATA:
2131 break; /* Nothing to do */
Alex Elderbf0d5f502012-11-22 00:00:08 -06002132 case OBJ_REQUEST_BIO:
2133 if (obj_request->bio_list)
2134 bio_chain_put(obj_request->bio_list);
2135 break;
Alex Elder788e2df2013-01-17 12:25:27 -06002136 case OBJ_REQUEST_PAGES:
2137 if (obj_request->pages)
2138 ceph_release_page_vector(obj_request->pages,
2139 obj_request->page_count);
2140 break;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002141 }
2142
Alex Elderf907ad52013-05-01 12:43:03 -05002143 kfree(obj_request->object_name);
Alex Elder868311b2013-05-01 12:43:03 -05002144 obj_request->object_name = NULL;
2145 kmem_cache_free(rbd_obj_request_cache, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002146}
2147
Alex Elderfb65d2282013-05-08 22:50:04 -05002148/* It's OK to call this for a device with no parent */
2149
2150static void rbd_spec_put(struct rbd_spec *spec);
2151static void rbd_dev_unparent(struct rbd_device *rbd_dev)
2152{
2153 rbd_dev_remove_parent(rbd_dev);
2154 rbd_spec_put(rbd_dev->parent_spec);
2155 rbd_dev->parent_spec = NULL;
2156 rbd_dev->parent_overlap = 0;
2157}
2158
Alex Elderbf0d5f502012-11-22 00:00:08 -06002159/*
Alex Eldera2acd002013-05-08 22:50:04 -05002160 * Parent image reference counting is used to determine when an
2161 * image's parent fields can be safely torn down--after there are no
2162 * more in-flight requests to the parent image. When the last
2163 * reference is dropped, cleaning them up is safe.
2164 */
2165static void rbd_dev_parent_put(struct rbd_device *rbd_dev)
2166{
2167 int counter;
2168
2169 if (!rbd_dev->parent_spec)
2170 return;
2171
2172 counter = atomic_dec_return_safe(&rbd_dev->parent_ref);
2173 if (counter > 0)
2174 return;
2175
2176 /* Last reference; clean up parent data structures */
2177
2178 if (!counter)
2179 rbd_dev_unparent(rbd_dev);
2180 else
Ilya Dryomov9584d502014-07-11 12:11:20 +04002181 rbd_warn(rbd_dev, "parent reference underflow");
Alex Eldera2acd002013-05-08 22:50:04 -05002182}
2183
2184/*
2185 * If an image has a non-zero parent overlap, get a reference to its
2186 * parent.
2187 *
2188 * Returns true if the rbd device has a parent with a non-zero
2189 * overlap and a reference for it was successfully taken, or
2190 * false otherwise.
2191 */
2192static bool rbd_dev_parent_get(struct rbd_device *rbd_dev)
2193{
Ilya Dryomovae43e9d2015-01-19 18:13:43 +03002194 int counter = 0;
Alex Eldera2acd002013-05-08 22:50:04 -05002195
2196 if (!rbd_dev->parent_spec)
2197 return false;
2198
Ilya Dryomovae43e9d2015-01-19 18:13:43 +03002199 down_read(&rbd_dev->header_rwsem);
2200 if (rbd_dev->parent_overlap)
2201 counter = atomic_inc_return_safe(&rbd_dev->parent_ref);
2202 up_read(&rbd_dev->header_rwsem);
Alex Eldera2acd002013-05-08 22:50:04 -05002203
2204 if (counter < 0)
Ilya Dryomov9584d502014-07-11 12:11:20 +04002205 rbd_warn(rbd_dev, "parent reference overflow");
Alex Eldera2acd002013-05-08 22:50:04 -05002206
Ilya Dryomovae43e9d2015-01-19 18:13:43 +03002207 return counter > 0;
Alex Eldera2acd002013-05-08 22:50:04 -05002208}
2209
Alex Elderbf0d5f502012-11-22 00:00:08 -06002210/*
2211 * Caller is responsible for filling in the list of object requests
2212 * that comprises the image request, and the Linux request pointer
2213 * (if there is one).
2214 */
Alex Eldercc344fa2013-02-19 12:25:56 -06002215static struct rbd_img_request *rbd_img_request_create(
2216 struct rbd_device *rbd_dev,
Alex Elderbf0d5f502012-11-22 00:00:08 -06002217 u64 offset, u64 length,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002218 enum obj_operation_type op_type,
Josh Durgin4e752f02014-04-08 11:12:11 -07002219 struct ceph_snap_context *snapc)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002220{
2221 struct rbd_img_request *img_request;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002222
Ilya Dryomov7a716aa2014-08-05 11:25:54 +04002223 img_request = kmem_cache_alloc(rbd_img_request_cache, GFP_NOIO);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002224 if (!img_request)
2225 return NULL;
2226
Alex Elderbf0d5f502012-11-22 00:00:08 -06002227 img_request->rq = NULL;
2228 img_request->rbd_dev = rbd_dev;
2229 img_request->offset = offset;
2230 img_request->length = length;
Alex Elder0c425242013-02-08 09:55:49 -06002231 img_request->flags = 0;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002232 if (op_type == OBJ_OP_DISCARD) {
2233 img_request_discard_set(img_request);
2234 img_request->snapc = snapc;
2235 } else if (op_type == OBJ_OP_WRITE) {
Alex Elder0c425242013-02-08 09:55:49 -06002236 img_request_write_set(img_request);
Josh Durgin4e752f02014-04-08 11:12:11 -07002237 img_request->snapc = snapc;
Alex Elder0c425242013-02-08 09:55:49 -06002238 } else {
Alex Elderbf0d5f502012-11-22 00:00:08 -06002239 img_request->snap_id = rbd_dev->spec->snap_id;
Alex Elder0c425242013-02-08 09:55:49 -06002240 }
Alex Eldera2acd002013-05-08 22:50:04 -05002241 if (rbd_dev_parent_get(rbd_dev))
Alex Elderd0b2e942013-01-24 16:13:36 -06002242 img_request_layered_set(img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002243 spin_lock_init(&img_request->completion_lock);
2244 img_request->next_completion = 0;
2245 img_request->callback = NULL;
Alex Eldera5a337d2013-01-24 16:13:36 -06002246 img_request->result = 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002247 img_request->obj_request_count = 0;
2248 INIT_LIST_HEAD(&img_request->obj_requests);
2249 kref_init(&img_request->kref);
2250
Alex Elder37206ee2013-02-20 17:32:08 -06002251 dout("%s: rbd_dev %p %s %llu/%llu -> img %p\n", __func__, rbd_dev,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002252 obj_op_name(op_type), offset, length, img_request);
Alex Elder37206ee2013-02-20 17:32:08 -06002253
Alex Elderbf0d5f502012-11-22 00:00:08 -06002254 return img_request;
2255}
2256
2257static void rbd_img_request_destroy(struct kref *kref)
2258{
2259 struct rbd_img_request *img_request;
2260 struct rbd_obj_request *obj_request;
2261 struct rbd_obj_request *next_obj_request;
2262
2263 img_request = container_of(kref, struct rbd_img_request, kref);
2264
Alex Elder37206ee2013-02-20 17:32:08 -06002265 dout("%s: img %p\n", __func__, img_request);
2266
Alex Elderbf0d5f502012-11-22 00:00:08 -06002267 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
2268 rbd_img_obj_request_del(img_request, obj_request);
Alex Elder25dcf952013-01-25 17:08:55 -06002269 rbd_assert(img_request->obj_request_count == 0);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002270
Alex Eldera2acd002013-05-08 22:50:04 -05002271 if (img_request_layered_test(img_request)) {
2272 img_request_layered_clear(img_request);
2273 rbd_dev_parent_put(img_request->rbd_dev);
2274 }
2275
Josh Durginbef95452014-04-04 17:47:52 -07002276 if (img_request_write_test(img_request) ||
2277 img_request_discard_test(img_request))
Alex Elder812164f82013-04-30 00:44:32 -05002278 ceph_put_snap_context(img_request->snapc);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002279
Alex Elder1c2a9df2013-05-01 12:43:03 -05002280 kmem_cache_free(rbd_img_request_cache, img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002281}
2282
Alex Eldere93f3152013-05-08 22:50:04 -05002283static struct rbd_img_request *rbd_parent_request_create(
2284 struct rbd_obj_request *obj_request,
2285 u64 img_offset, u64 length)
2286{
2287 struct rbd_img_request *parent_request;
2288 struct rbd_device *rbd_dev;
2289
2290 rbd_assert(obj_request->img_request);
2291 rbd_dev = obj_request->img_request->rbd_dev;
2292
Josh Durgin4e752f02014-04-08 11:12:11 -07002293 parent_request = rbd_img_request_create(rbd_dev->parent, img_offset,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002294 length, OBJ_OP_READ, NULL);
Alex Eldere93f3152013-05-08 22:50:04 -05002295 if (!parent_request)
2296 return NULL;
2297
2298 img_request_child_set(parent_request);
2299 rbd_obj_request_get(obj_request);
2300 parent_request->obj_request = obj_request;
2301
2302 return parent_request;
2303}
2304
2305static void rbd_parent_request_destroy(struct kref *kref)
2306{
2307 struct rbd_img_request *parent_request;
2308 struct rbd_obj_request *orig_request;
2309
2310 parent_request = container_of(kref, struct rbd_img_request, kref);
2311 orig_request = parent_request->obj_request;
2312
2313 parent_request->obj_request = NULL;
2314 rbd_obj_request_put(orig_request);
2315 img_request_child_clear(parent_request);
2316
2317 rbd_img_request_destroy(kref);
2318}
2319
Alex Elder12178572013-02-08 09:55:49 -06002320static bool rbd_img_obj_end_request(struct rbd_obj_request *obj_request)
2321{
Alex Elder6365d332013-02-11 12:33:24 -06002322 struct rbd_img_request *img_request;
Alex Elder12178572013-02-08 09:55:49 -06002323 unsigned int xferred;
2324 int result;
Alex Elder8b3e1a52013-01-24 16:13:36 -06002325 bool more;
Alex Elder12178572013-02-08 09:55:49 -06002326
Alex Elder6365d332013-02-11 12:33:24 -06002327 rbd_assert(obj_request_img_data_test(obj_request));
2328 img_request = obj_request->img_request;
2329
Alex Elder12178572013-02-08 09:55:49 -06002330 rbd_assert(obj_request->xferred <= (u64)UINT_MAX);
2331 xferred = (unsigned int)obj_request->xferred;
2332 result = obj_request->result;
2333 if (result) {
2334 struct rbd_device *rbd_dev = img_request->rbd_dev;
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002335 enum obj_operation_type op_type;
2336
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002337 if (img_request_discard_test(img_request))
2338 op_type = OBJ_OP_DISCARD;
2339 else if (img_request_write_test(img_request))
2340 op_type = OBJ_OP_WRITE;
2341 else
2342 op_type = OBJ_OP_READ;
Alex Elder12178572013-02-08 09:55:49 -06002343
Ilya Dryomov9584d502014-07-11 12:11:20 +04002344 rbd_warn(rbd_dev, "%s %llx at %llx (%llx)",
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002345 obj_op_name(op_type), obj_request->length,
2346 obj_request->img_offset, obj_request->offset);
Ilya Dryomov9584d502014-07-11 12:11:20 +04002347 rbd_warn(rbd_dev, " result %d xferred %x",
Alex Elder12178572013-02-08 09:55:49 -06002348 result, xferred);
2349 if (!img_request->result)
2350 img_request->result = result;
Ilya Dryomov082a75d2015-04-25 15:56:15 +03002351 /*
2352 * Need to end I/O on the entire obj_request worth of
2353 * bytes in case of error.
2354 */
2355 xferred = obj_request->length;
Alex Elder12178572013-02-08 09:55:49 -06002356 }
2357
Alex Elderf1a47392013-04-19 15:34:50 -05002358 /* Image object requests don't own their page array */
2359
2360 if (obj_request->type == OBJ_REQUEST_PAGES) {
2361 obj_request->pages = NULL;
2362 obj_request->page_count = 0;
2363 }
2364
Alex Elder8b3e1a52013-01-24 16:13:36 -06002365 if (img_request_child_test(img_request)) {
2366 rbd_assert(img_request->obj_request != NULL);
2367 more = obj_request->which < img_request->obj_request_count - 1;
2368 } else {
2369 rbd_assert(img_request->rq != NULL);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01002370
2371 more = blk_update_request(img_request->rq, result, xferred);
2372 if (!more)
2373 __blk_mq_end_request(img_request->rq, result);
Alex Elder8b3e1a52013-01-24 16:13:36 -06002374 }
2375
2376 return more;
Alex Elder12178572013-02-08 09:55:49 -06002377}
2378
Alex Elder21692382013-04-05 01:27:12 -05002379static void rbd_img_obj_callback(struct rbd_obj_request *obj_request)
2380{
2381 struct rbd_img_request *img_request;
2382 u32 which = obj_request->which;
2383 bool more = true;
2384
Alex Elder6365d332013-02-11 12:33:24 -06002385 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elder21692382013-04-05 01:27:12 -05002386 img_request = obj_request->img_request;
2387
2388 dout("%s: img %p obj %p\n", __func__, img_request, obj_request);
2389 rbd_assert(img_request != NULL);
Alex Elder21692382013-04-05 01:27:12 -05002390 rbd_assert(img_request->obj_request_count > 0);
2391 rbd_assert(which != BAD_WHICH);
2392 rbd_assert(which < img_request->obj_request_count);
Alex Elder21692382013-04-05 01:27:12 -05002393
2394 spin_lock_irq(&img_request->completion_lock);
2395 if (which != img_request->next_completion)
2396 goto out;
2397
2398 for_each_obj_request_from(img_request, obj_request) {
Alex Elder21692382013-04-05 01:27:12 -05002399 rbd_assert(more);
2400 rbd_assert(which < img_request->obj_request_count);
2401
2402 if (!obj_request_done_test(obj_request))
2403 break;
Alex Elder12178572013-02-08 09:55:49 -06002404 more = rbd_img_obj_end_request(obj_request);
Alex Elder21692382013-04-05 01:27:12 -05002405 which++;
2406 }
2407
2408 rbd_assert(more ^ (which == img_request->obj_request_count));
2409 img_request->next_completion = which;
2410out:
2411 spin_unlock_irq(&img_request->completion_lock);
Alex Elder0f2d5be2014-04-26 14:21:44 +04002412 rbd_img_request_put(img_request);
Alex Elder21692382013-04-05 01:27:12 -05002413
2414 if (!more)
2415 rbd_img_request_complete(img_request);
2416}
2417
Alex Elderf1a47392013-04-19 15:34:50 -05002418/*
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002419 * Add individual osd ops to the given ceph_osd_request and prepare
2420 * them for submission. num_ops is the current number of
2421 * osd operations already to the object request.
2422 */
2423static void rbd_img_obj_request_fill(struct rbd_obj_request *obj_request,
2424 struct ceph_osd_request *osd_request,
2425 enum obj_operation_type op_type,
2426 unsigned int num_ops)
2427{
2428 struct rbd_img_request *img_request = obj_request->img_request;
2429 struct rbd_device *rbd_dev = img_request->rbd_dev;
2430 u64 object_size = rbd_obj_bytes(&rbd_dev->header);
2431 u64 offset = obj_request->offset;
2432 u64 length = obj_request->length;
2433 u64 img_end;
2434 u16 opcode;
2435
2436 if (op_type == OBJ_OP_DISCARD) {
Josh Durgind3246fb2014-04-07 16:49:21 -07002437 if (!offset && length == object_size &&
2438 (!img_request_layered_test(img_request) ||
2439 !obj_request_overlaps_parent(obj_request))) {
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002440 opcode = CEPH_OSD_OP_DELETE;
2441 } else if ((offset + length == object_size)) {
2442 opcode = CEPH_OSD_OP_TRUNCATE;
2443 } else {
2444 down_read(&rbd_dev->header_rwsem);
2445 img_end = rbd_dev->header.image_size;
2446 up_read(&rbd_dev->header_rwsem);
2447
2448 if (obj_request->img_offset + length == img_end)
2449 opcode = CEPH_OSD_OP_TRUNCATE;
2450 else
2451 opcode = CEPH_OSD_OP_ZERO;
2452 }
2453 } else if (op_type == OBJ_OP_WRITE) {
Ilya Dryomove30b7572015-10-07 17:27:17 +02002454 if (!offset && length == object_size)
2455 opcode = CEPH_OSD_OP_WRITEFULL;
2456 else
2457 opcode = CEPH_OSD_OP_WRITE;
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002458 osd_req_op_alloc_hint_init(osd_request, num_ops,
2459 object_size, object_size);
2460 num_ops++;
2461 } else {
2462 opcode = CEPH_OSD_OP_READ;
2463 }
2464
Ilya Dryomov7e868b62014-11-21 22:16:43 +03002465 if (opcode == CEPH_OSD_OP_DELETE)
Yan, Zheng144cba12015-04-27 11:09:54 +08002466 osd_req_op_init(osd_request, num_ops, opcode, 0);
Ilya Dryomov7e868b62014-11-21 22:16:43 +03002467 else
2468 osd_req_op_extent_init(osd_request, num_ops, opcode,
2469 offset, length, 0, 0);
2470
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002471 if (obj_request->type == OBJ_REQUEST_BIO)
2472 osd_req_op_extent_osd_data_bio(osd_request, num_ops,
2473 obj_request->bio_list, length);
2474 else if (obj_request->type == OBJ_REQUEST_PAGES)
2475 osd_req_op_extent_osd_data_pages(osd_request, num_ops,
2476 obj_request->pages, length,
2477 offset & ~PAGE_MASK, false, false);
2478
2479 /* Discards are also writes */
2480 if (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD)
2481 rbd_osd_req_format_write(obj_request);
2482 else
2483 rbd_osd_req_format_read(obj_request);
2484}
2485
2486/*
Alex Elderf1a47392013-04-19 15:34:50 -05002487 * Split up an image request into one or more object requests, each
2488 * to a different object. The "type" parameter indicates whether
2489 * "data_desc" is the pointer to the head of a list of bio
2490 * structures, or the base of a page array. In either case this
2491 * function assumes data_desc describes memory sufficient to hold
2492 * all data described by the image request.
2493 */
2494static int rbd_img_request_fill(struct rbd_img_request *img_request,
2495 enum obj_request_type type,
2496 void *data_desc)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002497{
2498 struct rbd_device *rbd_dev = img_request->rbd_dev;
2499 struct rbd_obj_request *obj_request = NULL;
2500 struct rbd_obj_request *next_obj_request;
Jingoo Hana1580732013-08-09 13:04:35 +09002501 struct bio *bio_list = NULL;
Alex Elderf1a47392013-04-19 15:34:50 -05002502 unsigned int bio_offset = 0;
Jingoo Hana1580732013-08-09 13:04:35 +09002503 struct page **pages = NULL;
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002504 enum obj_operation_type op_type;
Alex Elder7da22d22013-01-24 16:13:36 -06002505 u64 img_offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002506 u64 resid;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002507
Alex Elderf1a47392013-04-19 15:34:50 -05002508 dout("%s: img %p type %d data_desc %p\n", __func__, img_request,
2509 (int)type, data_desc);
Alex Elder37206ee2013-02-20 17:32:08 -06002510
Alex Elder7da22d22013-01-24 16:13:36 -06002511 img_offset = img_request->offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002512 resid = img_request->length;
Alex Elder4dda41d2013-02-20 21:59:33 -06002513 rbd_assert(resid > 0);
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002514 op_type = rbd_img_request_op_type(img_request);
Alex Elderf1a47392013-04-19 15:34:50 -05002515
2516 if (type == OBJ_REQUEST_BIO) {
2517 bio_list = data_desc;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002518 rbd_assert(img_offset ==
2519 bio_list->bi_iter.bi_sector << SECTOR_SHIFT);
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002520 } else if (type == OBJ_REQUEST_PAGES) {
Alex Elderf1a47392013-04-19 15:34:50 -05002521 pages = data_desc;
2522 }
2523
Alex Elderbf0d5f502012-11-22 00:00:08 -06002524 while (resid) {
Alex Elder2fa12322013-04-05 01:27:12 -05002525 struct ceph_osd_request *osd_req;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002526 const char *object_name;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002527 u64 offset;
2528 u64 length;
2529
Alex Elder7da22d22013-01-24 16:13:36 -06002530 object_name = rbd_segment_name(rbd_dev, img_offset);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002531 if (!object_name)
2532 goto out_unwind;
Alex Elder7da22d22013-01-24 16:13:36 -06002533 offset = rbd_segment_offset(rbd_dev, img_offset);
2534 length = rbd_segment_length(rbd_dev, img_offset, resid);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002535 obj_request = rbd_obj_request_create(object_name,
Alex Elderf1a47392013-04-19 15:34:50 -05002536 offset, length, type);
Alex Elder78c2a442013-05-01 12:43:04 -05002537 /* object request has its own copy of the object name */
2538 rbd_segment_name_free(object_name);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002539 if (!obj_request)
2540 goto out_unwind;
Ilya Dryomov62054da2014-03-04 11:57:17 +02002541
Josh Durgin03507db2013-08-27 14:45:46 -07002542 /*
2543 * set obj_request->img_request before creating the
2544 * osd_request so that it gets the right snapc
2545 */
2546 rbd_img_obj_request_add(img_request, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002547
Alex Elderf1a47392013-04-19 15:34:50 -05002548 if (type == OBJ_REQUEST_BIO) {
2549 unsigned int clone_size;
2550
2551 rbd_assert(length <= (u64)UINT_MAX);
2552 clone_size = (unsigned int)length;
2553 obj_request->bio_list =
2554 bio_chain_clone_range(&bio_list,
2555 &bio_offset,
2556 clone_size,
David Disseldorp2224d872016-04-05 11:13:39 +02002557 GFP_NOIO);
Alex Elderf1a47392013-04-19 15:34:50 -05002558 if (!obj_request->bio_list)
Ilya Dryomov62054da2014-03-04 11:57:17 +02002559 goto out_unwind;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002560 } else if (type == OBJ_REQUEST_PAGES) {
Alex Elderf1a47392013-04-19 15:34:50 -05002561 unsigned int page_count;
2562
2563 obj_request->pages = pages;
2564 page_count = (u32)calc_pages_for(offset, length);
2565 obj_request->page_count = page_count;
2566 if ((offset + length) & ~PAGE_MASK)
2567 page_count--; /* more on last page */
2568 pages += page_count;
2569 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06002570
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002571 osd_req = rbd_osd_req_create(rbd_dev, op_type,
2572 (op_type == OBJ_OP_WRITE) ? 2 : 1,
2573 obj_request);
Alex Elder2fa12322013-04-05 01:27:12 -05002574 if (!osd_req)
Ilya Dryomov62054da2014-03-04 11:57:17 +02002575 goto out_unwind;
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002576
Alex Elder2fa12322013-04-05 01:27:12 -05002577 obj_request->osd_req = osd_req;
Alex Elder21692382013-04-05 01:27:12 -05002578 obj_request->callback = rbd_img_obj_callback;
Alex Elder7da22d22013-01-24 16:13:36 -06002579 obj_request->img_offset = img_offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002580
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002581 rbd_img_obj_request_fill(obj_request, osd_req, op_type, 0);
2582
2583 rbd_img_request_get(img_request);
2584
Alex Elder7da22d22013-01-24 16:13:36 -06002585 img_offset += length;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002586 resid -= length;
2587 }
2588
2589 return 0;
2590
Alex Elderbf0d5f502012-11-22 00:00:08 -06002591out_unwind:
2592 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
Ilya Dryomov42dd0372014-03-04 11:57:17 +02002593 rbd_img_obj_request_del(img_request, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002594
2595 return -ENOMEM;
2596}
2597
Alex Elder3d7efd12013-04-19 15:34:50 -05002598static void
Ilya Dryomov27617132015-07-16 17:36:11 +03002599rbd_osd_copyup_callback(struct rbd_obj_request *obj_request)
Alex Elder0eefd472013-04-19 15:34:50 -05002600{
2601 struct rbd_img_request *img_request;
2602 struct rbd_device *rbd_dev;
Alex Elderebda6402013-05-10 16:29:22 -05002603 struct page **pages;
Alex Elder0eefd472013-04-19 15:34:50 -05002604 u32 page_count;
2605
Ilya Dryomov27617132015-07-16 17:36:11 +03002606 dout("%s: obj %p\n", __func__, obj_request);
2607
Josh Durgind3246fb2014-04-07 16:49:21 -07002608 rbd_assert(obj_request->type == OBJ_REQUEST_BIO ||
2609 obj_request->type == OBJ_REQUEST_NODATA);
Alex Elder0eefd472013-04-19 15:34:50 -05002610 rbd_assert(obj_request_img_data_test(obj_request));
2611 img_request = obj_request->img_request;
2612 rbd_assert(img_request);
2613
2614 rbd_dev = img_request->rbd_dev;
2615 rbd_assert(rbd_dev);
Alex Elder0eefd472013-04-19 15:34:50 -05002616
Alex Elderebda6402013-05-10 16:29:22 -05002617 pages = obj_request->copyup_pages;
2618 rbd_assert(pages != NULL);
Alex Elder0eefd472013-04-19 15:34:50 -05002619 obj_request->copyup_pages = NULL;
Alex Elderebda6402013-05-10 16:29:22 -05002620 page_count = obj_request->copyup_page_count;
2621 rbd_assert(page_count);
2622 obj_request->copyup_page_count = 0;
2623 ceph_release_page_vector(pages, page_count);
Alex Elder0eefd472013-04-19 15:34:50 -05002624
2625 /*
2626 * We want the transfer count to reflect the size of the
2627 * original write request. There is no such thing as a
2628 * successful short write, so if the request was successful
2629 * we can just set it to the originally-requested length.
2630 */
2631 if (!obj_request->result)
2632 obj_request->xferred = obj_request->length;
2633
Ilya Dryomov27617132015-07-16 17:36:11 +03002634 obj_request_done_set(obj_request);
Alex Elder0eefd472013-04-19 15:34:50 -05002635}
2636
2637static void
Alex Elder3d7efd12013-04-19 15:34:50 -05002638rbd_img_obj_parent_read_full_callback(struct rbd_img_request *img_request)
2639{
2640 struct rbd_obj_request *orig_request;
Alex Elder0eefd472013-04-19 15:34:50 -05002641 struct ceph_osd_request *osd_req;
2642 struct ceph_osd_client *osdc;
2643 struct rbd_device *rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002644 struct page **pages;
Josh Durgind3246fb2014-04-07 16:49:21 -07002645 enum obj_operation_type op_type;
Alex Elderebda6402013-05-10 16:29:22 -05002646 u32 page_count;
Alex Elderbbea1c12013-05-06 17:40:33 -05002647 int img_result;
Alex Elderebda6402013-05-10 16:29:22 -05002648 u64 parent_length;
Alex Elder3d7efd12013-04-19 15:34:50 -05002649
2650 rbd_assert(img_request_child_test(img_request));
2651
2652 /* First get what we need from the image request */
2653
2654 pages = img_request->copyup_pages;
2655 rbd_assert(pages != NULL);
2656 img_request->copyup_pages = NULL;
Alex Elderebda6402013-05-10 16:29:22 -05002657 page_count = img_request->copyup_page_count;
2658 rbd_assert(page_count);
2659 img_request->copyup_page_count = 0;
Alex Elder3d7efd12013-04-19 15:34:50 -05002660
2661 orig_request = img_request->obj_request;
2662 rbd_assert(orig_request != NULL);
Alex Elderb91f09f2013-05-10 16:29:22 -05002663 rbd_assert(obj_request_type_valid(orig_request->type));
Alex Elderbbea1c12013-05-06 17:40:33 -05002664 img_result = img_request->result;
Alex Elderebda6402013-05-10 16:29:22 -05002665 parent_length = img_request->length;
2666 rbd_assert(parent_length == img_request->xferred);
Alex Elder3d7efd12013-04-19 15:34:50 -05002667 rbd_img_request_put(img_request);
2668
Alex Elder91c6feb2013-05-06 17:40:32 -05002669 rbd_assert(orig_request->img_request);
2670 rbd_dev = orig_request->img_request->rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002671 rbd_assert(rbd_dev);
Alex Elder3d7efd12013-04-19 15:34:50 -05002672
Alex Elderbbea1c12013-05-06 17:40:33 -05002673 /*
2674 * If the overlap has become 0 (most likely because the
2675 * image has been flattened) we need to free the pages
2676 * and re-submit the original write request.
2677 */
2678 if (!rbd_dev->parent_overlap) {
2679 struct ceph_osd_client *osdc;
2680
2681 ceph_release_page_vector(pages, page_count);
2682 osdc = &rbd_dev->rbd_client->client->osdc;
2683 img_result = rbd_obj_request_submit(osdc, orig_request);
2684 if (!img_result)
2685 return;
2686 }
2687
2688 if (img_result)
Alex Elder0eefd472013-04-19 15:34:50 -05002689 goto out_err;
Alex Elder3d7efd12013-04-19 15:34:50 -05002690
Alex Elder8785b1d2013-05-09 10:08:49 -05002691 /*
2692 * The original osd request is of no use to use any more.
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02002693 * We need a new one that can hold the three ops in a copyup
Alex Elder8785b1d2013-05-09 10:08:49 -05002694 * request. Allocate the new copyup osd request for the
2695 * original request, and release the old one.
2696 */
Alex Elderbbea1c12013-05-06 17:40:33 -05002697 img_result = -ENOMEM;
Alex Elder0eefd472013-04-19 15:34:50 -05002698 osd_req = rbd_osd_req_create_copyup(orig_request);
2699 if (!osd_req)
2700 goto out_err;
Alex Elder8785b1d2013-05-09 10:08:49 -05002701 rbd_osd_req_destroy(orig_request->osd_req);
Alex Elder0eefd472013-04-19 15:34:50 -05002702 orig_request->osd_req = osd_req;
2703 orig_request->copyup_pages = pages;
Alex Elderebda6402013-05-10 16:29:22 -05002704 orig_request->copyup_page_count = page_count;
Alex Elder3d7efd12013-04-19 15:34:50 -05002705
Alex Elder0eefd472013-04-19 15:34:50 -05002706 /* Initialize the copyup op */
2707
2708 osd_req_op_cls_init(osd_req, 0, CEPH_OSD_OP_CALL, "rbd", "copyup");
Alex Elderebda6402013-05-10 16:29:22 -05002709 osd_req_op_cls_request_data_pages(osd_req, 0, pages, parent_length, 0,
Alex Elder0eefd472013-04-19 15:34:50 -05002710 false, false);
2711
Josh Durgind3246fb2014-04-07 16:49:21 -07002712 /* Add the other op(s) */
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02002713
Josh Durgind3246fb2014-04-07 16:49:21 -07002714 op_type = rbd_img_request_op_type(orig_request->img_request);
2715 rbd_img_obj_request_fill(orig_request, osd_req, op_type, 1);
Alex Elder0eefd472013-04-19 15:34:50 -05002716
2717 /* All set, send it off. */
2718
Alex Elder0eefd472013-04-19 15:34:50 -05002719 osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elderbbea1c12013-05-06 17:40:33 -05002720 img_result = rbd_obj_request_submit(osdc, orig_request);
2721 if (!img_result)
Alex Elder0eefd472013-04-19 15:34:50 -05002722 return;
2723out_err:
2724 /* Record the error code and complete the request */
2725
Alex Elderbbea1c12013-05-06 17:40:33 -05002726 orig_request->result = img_result;
Alex Elder0eefd472013-04-19 15:34:50 -05002727 orig_request->xferred = 0;
2728 obj_request_done_set(orig_request);
2729 rbd_obj_request_complete(orig_request);
Alex Elder3d7efd12013-04-19 15:34:50 -05002730}
2731
2732/*
2733 * Read from the parent image the range of data that covers the
2734 * entire target of the given object request. This is used for
2735 * satisfying a layered image write request when the target of an
2736 * object request from the image request does not exist.
2737 *
2738 * A page array big enough to hold the returned data is allocated
2739 * and supplied to rbd_img_request_fill() as the "data descriptor."
2740 * When the read completes, this page array will be transferred to
2741 * the original object request for the copyup operation.
2742 *
2743 * If an error occurs, record it as the result of the original
2744 * object request and mark it done so it gets completed.
2745 */
2746static int rbd_img_obj_parent_read_full(struct rbd_obj_request *obj_request)
2747{
2748 struct rbd_img_request *img_request = NULL;
2749 struct rbd_img_request *parent_request = NULL;
2750 struct rbd_device *rbd_dev;
2751 u64 img_offset;
2752 u64 length;
2753 struct page **pages = NULL;
2754 u32 page_count;
2755 int result;
2756
2757 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elderb91f09f2013-05-10 16:29:22 -05002758 rbd_assert(obj_request_type_valid(obj_request->type));
Alex Elder3d7efd12013-04-19 15:34:50 -05002759
2760 img_request = obj_request->img_request;
2761 rbd_assert(img_request != NULL);
2762 rbd_dev = img_request->rbd_dev;
2763 rbd_assert(rbd_dev->parent != NULL);
2764
2765 /*
2766 * Determine the byte range covered by the object in the
2767 * child image to which the original request was to be sent.
2768 */
2769 img_offset = obj_request->img_offset - obj_request->offset;
2770 length = (u64)1 << rbd_dev->header.obj_order;
2771
2772 /*
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002773 * There is no defined parent data beyond the parent
2774 * overlap, so limit what we read at that boundary if
2775 * necessary.
2776 */
2777 if (img_offset + length > rbd_dev->parent_overlap) {
2778 rbd_assert(img_offset < rbd_dev->parent_overlap);
2779 length = rbd_dev->parent_overlap - img_offset;
2780 }
2781
2782 /*
Alex Elder3d7efd12013-04-19 15:34:50 -05002783 * Allocate a page array big enough to receive the data read
2784 * from the parent.
2785 */
2786 page_count = (u32)calc_pages_for(0, length);
2787 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2788 if (IS_ERR(pages)) {
2789 result = PTR_ERR(pages);
2790 pages = NULL;
2791 goto out_err;
2792 }
2793
2794 result = -ENOMEM;
Alex Eldere93f3152013-05-08 22:50:04 -05002795 parent_request = rbd_parent_request_create(obj_request,
2796 img_offset, length);
Alex Elder3d7efd12013-04-19 15:34:50 -05002797 if (!parent_request)
2798 goto out_err;
Alex Elder3d7efd12013-04-19 15:34:50 -05002799
2800 result = rbd_img_request_fill(parent_request, OBJ_REQUEST_PAGES, pages);
2801 if (result)
2802 goto out_err;
2803 parent_request->copyup_pages = pages;
Alex Elderebda6402013-05-10 16:29:22 -05002804 parent_request->copyup_page_count = page_count;
Alex Elder3d7efd12013-04-19 15:34:50 -05002805
2806 parent_request->callback = rbd_img_obj_parent_read_full_callback;
2807 result = rbd_img_request_submit(parent_request);
2808 if (!result)
2809 return 0;
2810
2811 parent_request->copyup_pages = NULL;
Alex Elderebda6402013-05-10 16:29:22 -05002812 parent_request->copyup_page_count = 0;
Alex Elder3d7efd12013-04-19 15:34:50 -05002813 parent_request->obj_request = NULL;
2814 rbd_obj_request_put(obj_request);
2815out_err:
2816 if (pages)
2817 ceph_release_page_vector(pages, page_count);
2818 if (parent_request)
2819 rbd_img_request_put(parent_request);
2820 obj_request->result = result;
2821 obj_request->xferred = 0;
2822 obj_request_done_set(obj_request);
2823
2824 return result;
2825}
2826
Alex Elderc5b5ef62013-02-11 12:33:24 -06002827static void rbd_img_obj_exists_callback(struct rbd_obj_request *obj_request)
2828{
Alex Elderc5b5ef62013-02-11 12:33:24 -06002829 struct rbd_obj_request *orig_request;
Alex Elder638f5ab2013-05-06 17:40:33 -05002830 struct rbd_device *rbd_dev;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002831 int result;
2832
2833 rbd_assert(!obj_request_img_data_test(obj_request));
2834
2835 /*
2836 * All we need from the object request is the original
2837 * request and the result of the STAT op. Grab those, then
2838 * we're done with the request.
2839 */
2840 orig_request = obj_request->obj_request;
2841 obj_request->obj_request = NULL;
Alex Elder912c3172013-05-13 20:35:38 -05002842 rbd_obj_request_put(orig_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002843 rbd_assert(orig_request);
2844 rbd_assert(orig_request->img_request);
2845
2846 result = obj_request->result;
2847 obj_request->result = 0;
2848
2849 dout("%s: obj %p for obj %p result %d %llu/%llu\n", __func__,
2850 obj_request, orig_request, result,
2851 obj_request->xferred, obj_request->length);
2852 rbd_obj_request_put(obj_request);
2853
Alex Elder638f5ab2013-05-06 17:40:33 -05002854 /*
2855 * If the overlap has become 0 (most likely because the
2856 * image has been flattened) we need to free the pages
2857 * and re-submit the original write request.
2858 */
2859 rbd_dev = orig_request->img_request->rbd_dev;
2860 if (!rbd_dev->parent_overlap) {
2861 struct ceph_osd_client *osdc;
2862
Alex Elder638f5ab2013-05-06 17:40:33 -05002863 osdc = &rbd_dev->rbd_client->client->osdc;
2864 result = rbd_obj_request_submit(osdc, orig_request);
2865 if (!result)
2866 return;
2867 }
Alex Elderc5b5ef62013-02-11 12:33:24 -06002868
2869 /*
2870 * Our only purpose here is to determine whether the object
2871 * exists, and we don't want to treat the non-existence as
2872 * an error. If something else comes back, transfer the
2873 * error to the original request and complete it now.
2874 */
2875 if (!result) {
2876 obj_request_existence_set(orig_request, true);
2877 } else if (result == -ENOENT) {
2878 obj_request_existence_set(orig_request, false);
2879 } else if (result) {
2880 orig_request->result = result;
Alex Elder3d7efd12013-04-19 15:34:50 -05002881 goto out;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002882 }
2883
2884 /*
2885 * Resubmit the original request now that we have recorded
2886 * whether the target object exists.
2887 */
Alex Elderb454e362013-04-19 15:34:50 -05002888 orig_request->result = rbd_img_obj_request_submit(orig_request);
Alex Elder3d7efd12013-04-19 15:34:50 -05002889out:
Alex Elderc5b5ef62013-02-11 12:33:24 -06002890 if (orig_request->result)
2891 rbd_obj_request_complete(orig_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002892}
2893
2894static int rbd_img_obj_exists_submit(struct rbd_obj_request *obj_request)
2895{
2896 struct rbd_obj_request *stat_request;
2897 struct rbd_device *rbd_dev;
2898 struct ceph_osd_client *osdc;
2899 struct page **pages = NULL;
2900 u32 page_count;
2901 size_t size;
2902 int ret;
2903
2904 /*
2905 * The response data for a STAT call consists of:
2906 * le64 length;
2907 * struct {
2908 * le32 tv_sec;
2909 * le32 tv_nsec;
2910 * } mtime;
2911 */
2912 size = sizeof (__le64) + sizeof (__le32) + sizeof (__le32);
2913 page_count = (u32)calc_pages_for(0, size);
2914 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2915 if (IS_ERR(pages))
2916 return PTR_ERR(pages);
2917
2918 ret = -ENOMEM;
2919 stat_request = rbd_obj_request_create(obj_request->object_name, 0, 0,
2920 OBJ_REQUEST_PAGES);
2921 if (!stat_request)
2922 goto out;
2923
2924 rbd_obj_request_get(obj_request);
2925 stat_request->obj_request = obj_request;
2926 stat_request->pages = pages;
2927 stat_request->page_count = page_count;
2928
2929 rbd_assert(obj_request->img_request);
2930 rbd_dev = obj_request->img_request->rbd_dev;
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002931 stat_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_READ, 1,
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02002932 stat_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002933 if (!stat_request->osd_req)
2934 goto out;
2935 stat_request->callback = rbd_img_obj_exists_callback;
2936
Yan, Zheng144cba12015-04-27 11:09:54 +08002937 osd_req_op_init(stat_request->osd_req, 0, CEPH_OSD_OP_STAT, 0);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002938 osd_req_op_raw_data_in_pages(stat_request->osd_req, 0, pages, size, 0,
2939 false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05002940 rbd_osd_req_format_read(stat_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002941
2942 osdc = &rbd_dev->rbd_client->client->osdc;
2943 ret = rbd_obj_request_submit(osdc, stat_request);
2944out:
2945 if (ret)
2946 rbd_obj_request_put(obj_request);
2947
2948 return ret;
2949}
2950
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002951static bool img_obj_request_simple(struct rbd_obj_request *obj_request)
Alex Elderb454e362013-04-19 15:34:50 -05002952{
2953 struct rbd_img_request *img_request;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002954 struct rbd_device *rbd_dev;
Alex Elderb454e362013-04-19 15:34:50 -05002955
2956 rbd_assert(obj_request_img_data_test(obj_request));
2957
2958 img_request = obj_request->img_request;
2959 rbd_assert(img_request);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002960 rbd_dev = img_request->rbd_dev;
Alex Elderb454e362013-04-19 15:34:50 -05002961
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002962 /* Reads */
Josh Durgin1c220882014-04-04 17:49:12 -07002963 if (!img_request_write_test(img_request) &&
2964 !img_request_discard_test(img_request))
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002965 return true;
Alex Elderb454e362013-04-19 15:34:50 -05002966
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002967 /* Non-layered writes */
2968 if (!img_request_layered_test(img_request))
2969 return true;
2970
2971 /*
2972 * Layered writes outside of the parent overlap range don't
2973 * share any data with the parent.
2974 */
2975 if (!obj_request_overlaps_parent(obj_request))
2976 return true;
2977
2978 /*
Guangliang Zhaoc622d222014-04-01 22:22:15 +08002979 * Entire-object layered writes - we will overwrite whatever
2980 * parent data there is anyway.
2981 */
2982 if (!obj_request->offset &&
2983 obj_request->length == rbd_obj_bytes(&rbd_dev->header))
2984 return true;
2985
2986 /*
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002987 * If the object is known to already exist, its parent data has
2988 * already been copied.
2989 */
2990 if (obj_request_known_test(obj_request) &&
2991 obj_request_exists_test(obj_request))
2992 return true;
2993
2994 return false;
2995}
2996
2997static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request)
2998{
2999 if (img_obj_request_simple(obj_request)) {
Alex Elderb454e362013-04-19 15:34:50 -05003000 struct rbd_device *rbd_dev;
3001 struct ceph_osd_client *osdc;
3002
3003 rbd_dev = obj_request->img_request->rbd_dev;
3004 osdc = &rbd_dev->rbd_client->client->osdc;
3005
3006 return rbd_obj_request_submit(osdc, obj_request);
3007 }
3008
3009 /*
Alex Elder3d7efd12013-04-19 15:34:50 -05003010 * It's a layered write. The target object might exist but
3011 * we may not know that yet. If we know it doesn't exist,
3012 * start by reading the data for the full target object from
3013 * the parent so we can use it for a copyup to the target.
Alex Elderb454e362013-04-19 15:34:50 -05003014 */
Ilya Dryomov70d045f2014-09-12 16:02:01 +04003015 if (obj_request_known_test(obj_request))
Alex Elder3d7efd12013-04-19 15:34:50 -05003016 return rbd_img_obj_parent_read_full(obj_request);
3017
3018 /* We don't know whether the target exists. Go find out. */
Alex Elderb454e362013-04-19 15:34:50 -05003019
3020 return rbd_img_obj_exists_submit(obj_request);
3021}
3022
Alex Elderbf0d5f502012-11-22 00:00:08 -06003023static int rbd_img_request_submit(struct rbd_img_request *img_request)
3024{
Alex Elderbf0d5f502012-11-22 00:00:08 -06003025 struct rbd_obj_request *obj_request;
Alex Elder46faeed2013-04-10 17:47:46 -05003026 struct rbd_obj_request *next_obj_request;
Ilya Dryomov663ae2c2016-05-16 13:18:57 +02003027 int ret = 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06003028
Alex Elder37206ee2013-02-20 17:32:08 -06003029 dout("%s: img %p\n", __func__, img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06003030
Ilya Dryomov663ae2c2016-05-16 13:18:57 +02003031 rbd_img_request_get(img_request);
3032 for_each_obj_request_safe(img_request, obj_request, next_obj_request) {
Alex Elderb454e362013-04-19 15:34:50 -05003033 ret = rbd_img_obj_request_submit(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06003034 if (ret)
Ilya Dryomov663ae2c2016-05-16 13:18:57 +02003035 goto out_put_ireq;
Alex Elderbf0d5f502012-11-22 00:00:08 -06003036 }
3037
Ilya Dryomov663ae2c2016-05-16 13:18:57 +02003038out_put_ireq:
3039 rbd_img_request_put(img_request);
3040 return ret;
Alex Elderbf0d5f502012-11-22 00:00:08 -06003041}
3042
Alex Elder8b3e1a52013-01-24 16:13:36 -06003043static void rbd_img_parent_read_callback(struct rbd_img_request *img_request)
3044{
3045 struct rbd_obj_request *obj_request;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05003046 struct rbd_device *rbd_dev;
3047 u64 obj_end;
Alex Elder02c74fb2013-05-06 17:40:33 -05003048 u64 img_xferred;
3049 int img_result;
Alex Elder8b3e1a52013-01-24 16:13:36 -06003050
3051 rbd_assert(img_request_child_test(img_request));
3052
Alex Elder02c74fb2013-05-06 17:40:33 -05003053 /* First get what we need from the image request and release it */
3054
Alex Elder8b3e1a52013-01-24 16:13:36 -06003055 obj_request = img_request->obj_request;
Alex Elder02c74fb2013-05-06 17:40:33 -05003056 img_xferred = img_request->xferred;
3057 img_result = img_request->result;
3058 rbd_img_request_put(img_request);
3059
3060 /*
3061 * If the overlap has become 0 (most likely because the
3062 * image has been flattened) we need to re-submit the
3063 * original request.
3064 */
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05003065 rbd_assert(obj_request);
3066 rbd_assert(obj_request->img_request);
Alex Elder02c74fb2013-05-06 17:40:33 -05003067 rbd_dev = obj_request->img_request->rbd_dev;
3068 if (!rbd_dev->parent_overlap) {
3069 struct ceph_osd_client *osdc;
Alex Elder8b3e1a52013-01-24 16:13:36 -06003070
Alex Elder02c74fb2013-05-06 17:40:33 -05003071 osdc = &rbd_dev->rbd_client->client->osdc;
3072 img_result = rbd_obj_request_submit(osdc, obj_request);
3073 if (!img_result)
3074 return;
3075 }
3076
3077 obj_request->result = img_result;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05003078 if (obj_request->result)
3079 goto out;
3080
3081 /*
3082 * We need to zero anything beyond the parent overlap
3083 * boundary. Since rbd_img_obj_request_read_callback()
3084 * will zero anything beyond the end of a short read, an
3085 * easy way to do this is to pretend the data from the
3086 * parent came up short--ending at the overlap boundary.
3087 */
3088 rbd_assert(obj_request->img_offset < U64_MAX - obj_request->length);
3089 obj_end = obj_request->img_offset + obj_request->length;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05003090 if (obj_end > rbd_dev->parent_overlap) {
3091 u64 xferred = 0;
3092
3093 if (obj_request->img_offset < rbd_dev->parent_overlap)
3094 xferred = rbd_dev->parent_overlap -
3095 obj_request->img_offset;
3096
Alex Elder02c74fb2013-05-06 17:40:33 -05003097 obj_request->xferred = min(img_xferred, xferred);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05003098 } else {
Alex Elder02c74fb2013-05-06 17:40:33 -05003099 obj_request->xferred = img_xferred;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05003100 }
3101out:
Alex Elder8b3e1a52013-01-24 16:13:36 -06003102 rbd_img_obj_request_read_callback(obj_request);
3103 rbd_obj_request_complete(obj_request);
3104}
3105
3106static void rbd_img_parent_read(struct rbd_obj_request *obj_request)
3107{
Alex Elder8b3e1a52013-01-24 16:13:36 -06003108 struct rbd_img_request *img_request;
3109 int result;
3110
3111 rbd_assert(obj_request_img_data_test(obj_request));
3112 rbd_assert(obj_request->img_request != NULL);
3113 rbd_assert(obj_request->result == (s32) -ENOENT);
Alex Elder5b2ab722013-05-06 17:40:33 -05003114 rbd_assert(obj_request_type_valid(obj_request->type));
Alex Elder8b3e1a52013-01-24 16:13:36 -06003115
Alex Elder8b3e1a52013-01-24 16:13:36 -06003116 /* rbd_read_finish(obj_request, obj_request->length); */
Alex Eldere93f3152013-05-08 22:50:04 -05003117 img_request = rbd_parent_request_create(obj_request,
Alex Elder8b3e1a52013-01-24 16:13:36 -06003118 obj_request->img_offset,
Alex Eldere93f3152013-05-08 22:50:04 -05003119 obj_request->length);
Alex Elder8b3e1a52013-01-24 16:13:36 -06003120 result = -ENOMEM;
3121 if (!img_request)
3122 goto out_err;
3123
Alex Elder5b2ab722013-05-06 17:40:33 -05003124 if (obj_request->type == OBJ_REQUEST_BIO)
3125 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
3126 obj_request->bio_list);
3127 else
3128 result = rbd_img_request_fill(img_request, OBJ_REQUEST_PAGES,
3129 obj_request->pages);
Alex Elder8b3e1a52013-01-24 16:13:36 -06003130 if (result)
3131 goto out_err;
3132
3133 img_request->callback = rbd_img_parent_read_callback;
3134 result = rbd_img_request_submit(img_request);
3135 if (result)
3136 goto out_err;
3137
3138 return;
3139out_err:
3140 if (img_request)
3141 rbd_img_request_put(img_request);
3142 obj_request->result = result;
3143 obj_request->xferred = 0;
3144 obj_request_done_set(obj_request);
3145}
3146
Ilya Dryomoved95b212016-08-12 16:40:02 +02003147static const struct rbd_client_id rbd_empty_cid;
3148
3149static bool rbd_cid_equal(const struct rbd_client_id *lhs,
3150 const struct rbd_client_id *rhs)
3151{
3152 return lhs->gid == rhs->gid && lhs->handle == rhs->handle;
3153}
3154
3155static struct rbd_client_id rbd_get_cid(struct rbd_device *rbd_dev)
3156{
3157 struct rbd_client_id cid;
3158
3159 mutex_lock(&rbd_dev->watch_mutex);
3160 cid.gid = ceph_client_gid(rbd_dev->rbd_client->client);
3161 cid.handle = rbd_dev->watch_cookie;
3162 mutex_unlock(&rbd_dev->watch_mutex);
3163 return cid;
3164}
3165
3166/*
3167 * lock_rwsem must be held for write
3168 */
3169static void rbd_set_owner_cid(struct rbd_device *rbd_dev,
3170 const struct rbd_client_id *cid)
3171{
3172 dout("%s rbd_dev %p %llu-%llu -> %llu-%llu\n", __func__, rbd_dev,
3173 rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle,
3174 cid->gid, cid->handle);
3175 rbd_dev->owner_cid = *cid; /* struct */
3176}
3177
3178static void format_lock_cookie(struct rbd_device *rbd_dev, char *buf)
3179{
3180 mutex_lock(&rbd_dev->watch_mutex);
3181 sprintf(buf, "%s %llu", RBD_LOCK_COOKIE_PREFIX, rbd_dev->watch_cookie);
3182 mutex_unlock(&rbd_dev->watch_mutex);
3183}
3184
3185/*
3186 * lock_rwsem must be held for write
3187 */
3188static int rbd_lock(struct rbd_device *rbd_dev)
3189{
3190 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3191 struct rbd_client_id cid = rbd_get_cid(rbd_dev);
3192 char cookie[32];
3193 int ret;
3194
3195 WARN_ON(__rbd_is_lock_owner(rbd_dev));
3196
3197 format_lock_cookie(rbd_dev, cookie);
3198 ret = ceph_cls_lock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc,
3199 RBD_LOCK_NAME, CEPH_CLS_LOCK_EXCLUSIVE, cookie,
3200 RBD_LOCK_TAG, "", 0);
3201 if (ret)
3202 return ret;
3203
3204 rbd_dev->lock_state = RBD_LOCK_STATE_LOCKED;
3205 rbd_set_owner_cid(rbd_dev, &cid);
3206 queue_work(rbd_dev->task_wq, &rbd_dev->acquired_lock_work);
3207 return 0;
3208}
3209
3210/*
3211 * lock_rwsem must be held for write
3212 */
3213static int rbd_unlock(struct rbd_device *rbd_dev)
3214{
3215 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3216 char cookie[32];
3217 int ret;
3218
3219 WARN_ON(!__rbd_is_lock_owner(rbd_dev));
3220
3221 rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED;
3222
3223 format_lock_cookie(rbd_dev, cookie);
3224 ret = ceph_cls_unlock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc,
3225 RBD_LOCK_NAME, cookie);
3226 if (ret && ret != -ENOENT) {
3227 rbd_warn(rbd_dev, "cls_unlock failed: %d", ret);
3228 return ret;
3229 }
3230
3231 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
3232 queue_work(rbd_dev->task_wq, &rbd_dev->released_lock_work);
3233 return 0;
3234}
3235
3236static int __rbd_notify_op_lock(struct rbd_device *rbd_dev,
3237 enum rbd_notify_op notify_op,
3238 struct page ***preply_pages,
3239 size_t *preply_len)
3240{
3241 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3242 struct rbd_client_id cid = rbd_get_cid(rbd_dev);
3243 int buf_size = 4 + 8 + 8 + CEPH_ENCODING_START_BLK_LEN;
3244 char buf[buf_size];
3245 void *p = buf;
3246
3247 dout("%s rbd_dev %p notify_op %d\n", __func__, rbd_dev, notify_op);
3248
3249 /* encode *LockPayload NotifyMessage (op + ClientId) */
3250 ceph_start_encoding(&p, 2, 1, buf_size - CEPH_ENCODING_START_BLK_LEN);
3251 ceph_encode_32(&p, notify_op);
3252 ceph_encode_64(&p, cid.gid);
3253 ceph_encode_64(&p, cid.handle);
3254
3255 return ceph_osdc_notify(osdc, &rbd_dev->header_oid,
3256 &rbd_dev->header_oloc, buf, buf_size,
3257 RBD_NOTIFY_TIMEOUT, preply_pages, preply_len);
3258}
3259
3260static void rbd_notify_op_lock(struct rbd_device *rbd_dev,
3261 enum rbd_notify_op notify_op)
3262{
3263 struct page **reply_pages;
3264 size_t reply_len;
3265
3266 __rbd_notify_op_lock(rbd_dev, notify_op, &reply_pages, &reply_len);
3267 ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len));
3268}
3269
3270static void rbd_notify_acquired_lock(struct work_struct *work)
3271{
3272 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
3273 acquired_lock_work);
3274
3275 rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_ACQUIRED_LOCK);
3276}
3277
3278static void rbd_notify_released_lock(struct work_struct *work)
3279{
3280 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
3281 released_lock_work);
3282
3283 rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_RELEASED_LOCK);
3284}
3285
3286static int rbd_request_lock(struct rbd_device *rbd_dev)
3287{
3288 struct page **reply_pages;
3289 size_t reply_len;
3290 bool lock_owner_responded = false;
3291 int ret;
3292
3293 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3294
3295 ret = __rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_REQUEST_LOCK,
3296 &reply_pages, &reply_len);
3297 if (ret && ret != -ETIMEDOUT) {
3298 rbd_warn(rbd_dev, "failed to request lock: %d", ret);
3299 goto out;
3300 }
3301
3302 if (reply_len > 0 && reply_len <= PAGE_SIZE) {
3303 void *p = page_address(reply_pages[0]);
3304 void *const end = p + reply_len;
3305 u32 n;
3306
3307 ceph_decode_32_safe(&p, end, n, e_inval); /* num_acks */
3308 while (n--) {
3309 u8 struct_v;
3310 u32 len;
3311
3312 ceph_decode_need(&p, end, 8 + 8, e_inval);
3313 p += 8 + 8; /* skip gid and cookie */
3314
3315 ceph_decode_32_safe(&p, end, len, e_inval);
3316 if (!len)
3317 continue;
3318
3319 if (lock_owner_responded) {
3320 rbd_warn(rbd_dev,
3321 "duplicate lock owners detected");
3322 ret = -EIO;
3323 goto out;
3324 }
3325
3326 lock_owner_responded = true;
3327 ret = ceph_start_decoding(&p, end, 1, "ResponseMessage",
3328 &struct_v, &len);
3329 if (ret) {
3330 rbd_warn(rbd_dev,
3331 "failed to decode ResponseMessage: %d",
3332 ret);
3333 goto e_inval;
3334 }
3335
3336 ret = ceph_decode_32(&p);
3337 }
3338 }
3339
3340 if (!lock_owner_responded) {
3341 rbd_warn(rbd_dev, "no lock owners detected");
3342 ret = -ETIMEDOUT;
3343 }
3344
3345out:
3346 ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len));
3347 return ret;
3348
3349e_inval:
3350 ret = -EINVAL;
3351 goto out;
3352}
3353
3354static void wake_requests(struct rbd_device *rbd_dev, bool wake_all)
3355{
3356 dout("%s rbd_dev %p wake_all %d\n", __func__, rbd_dev, wake_all);
3357
3358 cancel_delayed_work(&rbd_dev->lock_dwork);
3359 if (wake_all)
3360 wake_up_all(&rbd_dev->lock_waitq);
3361 else
3362 wake_up(&rbd_dev->lock_waitq);
3363}
3364
3365static int get_lock_owner_info(struct rbd_device *rbd_dev,
3366 struct ceph_locker **lockers, u32 *num_lockers)
3367{
3368 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3369 u8 lock_type;
3370 char *lock_tag;
3371 int ret;
3372
3373 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3374
3375 ret = ceph_cls_lock_info(osdc, &rbd_dev->header_oid,
3376 &rbd_dev->header_oloc, RBD_LOCK_NAME,
3377 &lock_type, &lock_tag, lockers, num_lockers);
3378 if (ret)
3379 return ret;
3380
3381 if (*num_lockers == 0) {
3382 dout("%s rbd_dev %p no lockers detected\n", __func__, rbd_dev);
3383 goto out;
3384 }
3385
3386 if (strcmp(lock_tag, RBD_LOCK_TAG)) {
3387 rbd_warn(rbd_dev, "locked by external mechanism, tag %s",
3388 lock_tag);
3389 ret = -EBUSY;
3390 goto out;
3391 }
3392
3393 if (lock_type == CEPH_CLS_LOCK_SHARED) {
3394 rbd_warn(rbd_dev, "shared lock type detected");
3395 ret = -EBUSY;
3396 goto out;
3397 }
3398
3399 if (strncmp((*lockers)[0].id.cookie, RBD_LOCK_COOKIE_PREFIX,
3400 strlen(RBD_LOCK_COOKIE_PREFIX))) {
3401 rbd_warn(rbd_dev, "locked by external mechanism, cookie %s",
3402 (*lockers)[0].id.cookie);
3403 ret = -EBUSY;
3404 goto out;
3405 }
3406
3407out:
3408 kfree(lock_tag);
3409 return ret;
3410}
3411
3412static int find_watcher(struct rbd_device *rbd_dev,
3413 const struct ceph_locker *locker)
3414{
3415 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3416 struct ceph_watch_item *watchers;
3417 u32 num_watchers;
3418 u64 cookie;
3419 int i;
3420 int ret;
3421
3422 ret = ceph_osdc_list_watchers(osdc, &rbd_dev->header_oid,
3423 &rbd_dev->header_oloc, &watchers,
3424 &num_watchers);
3425 if (ret)
3426 return ret;
3427
3428 sscanf(locker->id.cookie, RBD_LOCK_COOKIE_PREFIX " %llu", &cookie);
3429 for (i = 0; i < num_watchers; i++) {
3430 if (!memcmp(&watchers[i].addr, &locker->info.addr,
3431 sizeof(locker->info.addr)) &&
3432 watchers[i].cookie == cookie) {
3433 struct rbd_client_id cid = {
3434 .gid = le64_to_cpu(watchers[i].name.num),
3435 .handle = cookie,
3436 };
3437
3438 dout("%s rbd_dev %p found cid %llu-%llu\n", __func__,
3439 rbd_dev, cid.gid, cid.handle);
3440 rbd_set_owner_cid(rbd_dev, &cid);
3441 ret = 1;
3442 goto out;
3443 }
3444 }
3445
3446 dout("%s rbd_dev %p no watchers\n", __func__, rbd_dev);
3447 ret = 0;
3448out:
3449 kfree(watchers);
3450 return ret;
3451}
3452
3453/*
3454 * lock_rwsem must be held for write
3455 */
3456static int rbd_try_lock(struct rbd_device *rbd_dev)
3457{
3458 struct ceph_client *client = rbd_dev->rbd_client->client;
3459 struct ceph_locker *lockers;
3460 u32 num_lockers;
3461 int ret;
3462
3463 for (;;) {
3464 ret = rbd_lock(rbd_dev);
3465 if (ret != -EBUSY)
3466 return ret;
3467
3468 /* determine if the current lock holder is still alive */
3469 ret = get_lock_owner_info(rbd_dev, &lockers, &num_lockers);
3470 if (ret)
3471 return ret;
3472
3473 if (num_lockers == 0)
3474 goto again;
3475
3476 ret = find_watcher(rbd_dev, lockers);
3477 if (ret) {
3478 if (ret > 0)
3479 ret = 0; /* have to request lock */
3480 goto out;
3481 }
3482
3483 rbd_warn(rbd_dev, "%s%llu seems dead, breaking lock",
3484 ENTITY_NAME(lockers[0].id.name));
3485
3486 ret = ceph_monc_blacklist_add(&client->monc,
3487 &lockers[0].info.addr);
3488 if (ret) {
3489 rbd_warn(rbd_dev, "blacklist of %s%llu failed: %d",
3490 ENTITY_NAME(lockers[0].id.name), ret);
3491 goto out;
3492 }
3493
3494 ret = ceph_cls_break_lock(&client->osdc, &rbd_dev->header_oid,
3495 &rbd_dev->header_oloc, RBD_LOCK_NAME,
3496 lockers[0].id.cookie,
3497 &lockers[0].id.name);
3498 if (ret && ret != -ENOENT)
3499 goto out;
3500
3501again:
3502 ceph_free_lockers(lockers, num_lockers);
3503 }
3504
3505out:
3506 ceph_free_lockers(lockers, num_lockers);
3507 return ret;
3508}
3509
3510/*
3511 * ret is set only if lock_state is RBD_LOCK_STATE_UNLOCKED
3512 */
3513static enum rbd_lock_state rbd_try_acquire_lock(struct rbd_device *rbd_dev,
3514 int *pret)
3515{
3516 enum rbd_lock_state lock_state;
3517
3518 down_read(&rbd_dev->lock_rwsem);
3519 dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev,
3520 rbd_dev->lock_state);
3521 if (__rbd_is_lock_owner(rbd_dev)) {
3522 lock_state = rbd_dev->lock_state;
3523 up_read(&rbd_dev->lock_rwsem);
3524 return lock_state;
3525 }
3526
3527 up_read(&rbd_dev->lock_rwsem);
3528 down_write(&rbd_dev->lock_rwsem);
3529 dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev,
3530 rbd_dev->lock_state);
3531 if (!__rbd_is_lock_owner(rbd_dev)) {
3532 *pret = rbd_try_lock(rbd_dev);
3533 if (*pret)
3534 rbd_warn(rbd_dev, "failed to acquire lock: %d", *pret);
3535 }
3536
3537 lock_state = rbd_dev->lock_state;
3538 up_write(&rbd_dev->lock_rwsem);
3539 return lock_state;
3540}
3541
3542static void rbd_acquire_lock(struct work_struct *work)
3543{
3544 struct rbd_device *rbd_dev = container_of(to_delayed_work(work),
3545 struct rbd_device, lock_dwork);
3546 enum rbd_lock_state lock_state;
3547 int ret;
3548
3549 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3550again:
3551 lock_state = rbd_try_acquire_lock(rbd_dev, &ret);
3552 if (lock_state != RBD_LOCK_STATE_UNLOCKED || ret == -EBLACKLISTED) {
3553 if (lock_state == RBD_LOCK_STATE_LOCKED)
3554 wake_requests(rbd_dev, true);
3555 dout("%s rbd_dev %p lock_state %d ret %d - done\n", __func__,
3556 rbd_dev, lock_state, ret);
3557 return;
3558 }
3559
3560 ret = rbd_request_lock(rbd_dev);
3561 if (ret == -ETIMEDOUT) {
3562 goto again; /* treat this as a dead client */
3563 } else if (ret < 0) {
3564 rbd_warn(rbd_dev, "error requesting lock: %d", ret);
3565 mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork,
3566 RBD_RETRY_DELAY);
3567 } else {
3568 /*
3569 * lock owner acked, but resend if we don't see them
3570 * release the lock
3571 */
3572 dout("%s rbd_dev %p requeueing lock_dwork\n", __func__,
3573 rbd_dev);
3574 mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork,
3575 msecs_to_jiffies(2 * RBD_NOTIFY_TIMEOUT * MSEC_PER_SEC));
3576 }
3577}
3578
3579/*
3580 * lock_rwsem must be held for write
3581 */
3582static bool rbd_release_lock(struct rbd_device *rbd_dev)
3583{
3584 dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev,
3585 rbd_dev->lock_state);
3586 if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED)
3587 return false;
3588
3589 rbd_dev->lock_state = RBD_LOCK_STATE_RELEASING;
3590 downgrade_write(&rbd_dev->lock_rwsem);
3591 /*
3592 * Ensure that all in-flight IO is flushed.
3593 *
3594 * FIXME: ceph_osdc_sync() flushes the entire OSD client, which
3595 * may be shared with other devices.
3596 */
3597 ceph_osdc_sync(&rbd_dev->rbd_client->client->osdc);
3598 up_read(&rbd_dev->lock_rwsem);
3599
3600 down_write(&rbd_dev->lock_rwsem);
3601 dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev,
3602 rbd_dev->lock_state);
3603 if (rbd_dev->lock_state != RBD_LOCK_STATE_RELEASING)
3604 return false;
3605
3606 if (!rbd_unlock(rbd_dev))
3607 /*
3608 * Give others a chance to grab the lock - we would re-acquire
3609 * almost immediately if we got new IO during ceph_osdc_sync()
3610 * otherwise. We need to ack our own notifications, so this
3611 * lock_dwork will be requeued from rbd_wait_state_locked()
3612 * after wake_requests() in rbd_handle_released_lock().
3613 */
3614 cancel_delayed_work(&rbd_dev->lock_dwork);
3615
3616 return true;
3617}
3618
3619static void rbd_release_lock_work(struct work_struct *work)
3620{
3621 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
3622 unlock_work);
3623
3624 down_write(&rbd_dev->lock_rwsem);
3625 rbd_release_lock(rbd_dev);
3626 up_write(&rbd_dev->lock_rwsem);
3627}
3628
3629static void rbd_handle_acquired_lock(struct rbd_device *rbd_dev, u8 struct_v,
3630 void **p)
3631{
3632 struct rbd_client_id cid = { 0 };
3633
3634 if (struct_v >= 2) {
3635 cid.gid = ceph_decode_64(p);
3636 cid.handle = ceph_decode_64(p);
3637 }
3638
3639 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
3640 cid.handle);
3641 if (!rbd_cid_equal(&cid, &rbd_empty_cid)) {
3642 down_write(&rbd_dev->lock_rwsem);
3643 if (rbd_cid_equal(&cid, &rbd_dev->owner_cid)) {
3644 /*
3645 * we already know that the remote client is
3646 * the owner
3647 */
3648 up_write(&rbd_dev->lock_rwsem);
3649 return;
3650 }
3651
3652 rbd_set_owner_cid(rbd_dev, &cid);
3653 downgrade_write(&rbd_dev->lock_rwsem);
3654 } else {
3655 down_read(&rbd_dev->lock_rwsem);
3656 }
3657
3658 if (!__rbd_is_lock_owner(rbd_dev))
3659 wake_requests(rbd_dev, false);
3660 up_read(&rbd_dev->lock_rwsem);
3661}
3662
3663static void rbd_handle_released_lock(struct rbd_device *rbd_dev, u8 struct_v,
3664 void **p)
3665{
3666 struct rbd_client_id cid = { 0 };
3667
3668 if (struct_v >= 2) {
3669 cid.gid = ceph_decode_64(p);
3670 cid.handle = ceph_decode_64(p);
3671 }
3672
3673 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
3674 cid.handle);
3675 if (!rbd_cid_equal(&cid, &rbd_empty_cid)) {
3676 down_write(&rbd_dev->lock_rwsem);
3677 if (!rbd_cid_equal(&cid, &rbd_dev->owner_cid)) {
3678 dout("%s rbd_dev %p unexpected owner, cid %llu-%llu != owner_cid %llu-%llu\n",
3679 __func__, rbd_dev, cid.gid, cid.handle,
3680 rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle);
3681 up_write(&rbd_dev->lock_rwsem);
3682 return;
3683 }
3684
3685 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
3686 downgrade_write(&rbd_dev->lock_rwsem);
3687 } else {
3688 down_read(&rbd_dev->lock_rwsem);
3689 }
3690
3691 if (!__rbd_is_lock_owner(rbd_dev))
3692 wake_requests(rbd_dev, false);
3693 up_read(&rbd_dev->lock_rwsem);
3694}
3695
3696static bool rbd_handle_request_lock(struct rbd_device *rbd_dev, u8 struct_v,
3697 void **p)
3698{
3699 struct rbd_client_id my_cid = rbd_get_cid(rbd_dev);
3700 struct rbd_client_id cid = { 0 };
3701 bool need_to_send;
3702
3703 if (struct_v >= 2) {
3704 cid.gid = ceph_decode_64(p);
3705 cid.handle = ceph_decode_64(p);
3706 }
3707
3708 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
3709 cid.handle);
3710 if (rbd_cid_equal(&cid, &my_cid))
3711 return false;
3712
3713 down_read(&rbd_dev->lock_rwsem);
3714 need_to_send = __rbd_is_lock_owner(rbd_dev);
3715 if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) {
3716 if (!rbd_cid_equal(&rbd_dev->owner_cid, &rbd_empty_cid)) {
3717 dout("%s rbd_dev %p queueing unlock_work\n", __func__,
3718 rbd_dev);
3719 queue_work(rbd_dev->task_wq, &rbd_dev->unlock_work);
3720 }
3721 }
3722 up_read(&rbd_dev->lock_rwsem);
3723 return need_to_send;
3724}
3725
3726static void __rbd_acknowledge_notify(struct rbd_device *rbd_dev,
3727 u64 notify_id, u64 cookie, s32 *result)
3728{
3729 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3730 int buf_size = 4 + CEPH_ENCODING_START_BLK_LEN;
3731 char buf[buf_size];
3732 int ret;
3733
3734 if (result) {
3735 void *p = buf;
3736
3737 /* encode ResponseMessage */
3738 ceph_start_encoding(&p, 1, 1,
3739 buf_size - CEPH_ENCODING_START_BLK_LEN);
3740 ceph_encode_32(&p, *result);
3741 } else {
3742 buf_size = 0;
3743 }
3744
3745 ret = ceph_osdc_notify_ack(osdc, &rbd_dev->header_oid,
3746 &rbd_dev->header_oloc, notify_id, cookie,
3747 buf, buf_size);
3748 if (ret)
3749 rbd_warn(rbd_dev, "acknowledge_notify failed: %d", ret);
3750}
3751
3752static void rbd_acknowledge_notify(struct rbd_device *rbd_dev, u64 notify_id,
3753 u64 cookie)
3754{
3755 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3756 __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, NULL);
3757}
3758
3759static void rbd_acknowledge_notify_result(struct rbd_device *rbd_dev,
3760 u64 notify_id, u64 cookie, s32 result)
3761{
3762 dout("%s rbd_dev %p result %d\n", __func__, rbd_dev, result);
3763 __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, &result);
3764}
3765
Ilya Dryomov922dab62016-05-26 01:15:02 +02003766static void rbd_watch_cb(void *arg, u64 notify_id, u64 cookie,
3767 u64 notifier_id, void *data, size_t data_len)
Alex Elderb8d70032012-11-30 17:53:04 -06003768{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003769 struct rbd_device *rbd_dev = arg;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003770 void *p = data;
3771 void *const end = p + data_len;
3772 u8 struct_v;
3773 u32 len;
3774 u32 notify_op;
Alex Elderb8d70032012-11-30 17:53:04 -06003775 int ret;
3776
Ilya Dryomoved95b212016-08-12 16:40:02 +02003777 dout("%s rbd_dev %p cookie %llu notify_id %llu data_len %zu\n",
3778 __func__, rbd_dev, cookie, notify_id, data_len);
3779 if (data_len) {
3780 ret = ceph_start_decoding(&p, end, 1, "NotifyMessage",
3781 &struct_v, &len);
3782 if (ret) {
3783 rbd_warn(rbd_dev, "failed to decode NotifyMessage: %d",
3784 ret);
3785 return;
3786 }
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04003787
Ilya Dryomoved95b212016-08-12 16:40:02 +02003788 notify_op = ceph_decode_32(&p);
3789 } else {
3790 /* legacy notification for header updates */
3791 notify_op = RBD_NOTIFY_OP_HEADER_UPDATE;
3792 len = 0;
3793 }
Alex Elderb8d70032012-11-30 17:53:04 -06003794
Ilya Dryomoved95b212016-08-12 16:40:02 +02003795 dout("%s rbd_dev %p notify_op %u\n", __func__, rbd_dev, notify_op);
3796 switch (notify_op) {
3797 case RBD_NOTIFY_OP_ACQUIRED_LOCK:
3798 rbd_handle_acquired_lock(rbd_dev, struct_v, &p);
3799 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3800 break;
3801 case RBD_NOTIFY_OP_RELEASED_LOCK:
3802 rbd_handle_released_lock(rbd_dev, struct_v, &p);
3803 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3804 break;
3805 case RBD_NOTIFY_OP_REQUEST_LOCK:
3806 if (rbd_handle_request_lock(rbd_dev, struct_v, &p))
3807 /*
3808 * send ResponseMessage(0) back so the client
3809 * can detect a missing owner
3810 */
3811 rbd_acknowledge_notify_result(rbd_dev, notify_id,
3812 cookie, 0);
3813 else
3814 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3815 break;
3816 case RBD_NOTIFY_OP_HEADER_UPDATE:
3817 ret = rbd_dev_refresh(rbd_dev);
3818 if (ret)
3819 rbd_warn(rbd_dev, "refresh failed: %d", ret);
3820
3821 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3822 break;
3823 default:
3824 if (rbd_is_lock_owner(rbd_dev))
3825 rbd_acknowledge_notify_result(rbd_dev, notify_id,
3826 cookie, -EOPNOTSUPP);
3827 else
3828 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3829 break;
3830 }
Alex Elderb8d70032012-11-30 17:53:04 -06003831}
3832
Ilya Dryomov99d16942016-08-12 16:11:41 +02003833static void __rbd_unregister_watch(struct rbd_device *rbd_dev);
3834
Ilya Dryomov922dab62016-05-26 01:15:02 +02003835static void rbd_watch_errcb(void *arg, u64 cookie, int err)
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003836{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003837 struct rbd_device *rbd_dev = arg;
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003838
Ilya Dryomov922dab62016-05-26 01:15:02 +02003839 rbd_warn(rbd_dev, "encountered watch error: %d", err);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003840
Ilya Dryomoved95b212016-08-12 16:40:02 +02003841 down_write(&rbd_dev->lock_rwsem);
3842 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
3843 up_write(&rbd_dev->lock_rwsem);
3844
Ilya Dryomov99d16942016-08-12 16:11:41 +02003845 mutex_lock(&rbd_dev->watch_mutex);
3846 if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) {
3847 __rbd_unregister_watch(rbd_dev);
3848 rbd_dev->watch_state = RBD_WATCH_STATE_ERROR;
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003849
Ilya Dryomov99d16942016-08-12 16:11:41 +02003850 queue_delayed_work(rbd_dev->task_wq, &rbd_dev->watch_dwork, 0);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003851 }
Ilya Dryomov99d16942016-08-12 16:11:41 +02003852 mutex_unlock(&rbd_dev->watch_mutex);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003853}
3854
3855/*
Ilya Dryomov99d16942016-08-12 16:11:41 +02003856 * watch_mutex must be locked
Alex Elder9969ebc2013-01-18 12:31:10 -06003857 */
Ilya Dryomov99d16942016-08-12 16:11:41 +02003858static int __rbd_register_watch(struct rbd_device *rbd_dev)
Alex Elder9969ebc2013-01-18 12:31:10 -06003859{
3860 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Ilya Dryomov922dab62016-05-26 01:15:02 +02003861 struct ceph_osd_linger_request *handle;
Alex Elder9969ebc2013-01-18 12:31:10 -06003862
Ilya Dryomov922dab62016-05-26 01:15:02 +02003863 rbd_assert(!rbd_dev->watch_handle);
Ilya Dryomov99d16942016-08-12 16:11:41 +02003864 dout("%s rbd_dev %p\n", __func__, rbd_dev);
Alex Elder9969ebc2013-01-18 12:31:10 -06003865
Ilya Dryomov922dab62016-05-26 01:15:02 +02003866 handle = ceph_osdc_watch(osdc, &rbd_dev->header_oid,
3867 &rbd_dev->header_oloc, rbd_watch_cb,
3868 rbd_watch_errcb, rbd_dev);
3869 if (IS_ERR(handle))
3870 return PTR_ERR(handle);
Alex Elder9969ebc2013-01-18 12:31:10 -06003871
Ilya Dryomov922dab62016-05-26 01:15:02 +02003872 rbd_dev->watch_handle = handle;
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003873 return 0;
Alex Elder9969ebc2013-01-18 12:31:10 -06003874}
3875
Ilya Dryomov99d16942016-08-12 16:11:41 +02003876/*
3877 * watch_mutex must be locked
3878 */
3879static void __rbd_unregister_watch(struct rbd_device *rbd_dev)
Ilya Dryomovfca27062013-12-16 18:02:40 +02003880{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003881 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3882 int ret;
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003883
Ilya Dryomov99d16942016-08-12 16:11:41 +02003884 rbd_assert(rbd_dev->watch_handle);
3885 dout("%s rbd_dev %p\n", __func__, rbd_dev);
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003886
Ilya Dryomov922dab62016-05-26 01:15:02 +02003887 ret = ceph_osdc_unwatch(osdc, rbd_dev->watch_handle);
3888 if (ret)
3889 rbd_warn(rbd_dev, "failed to unwatch: %d", ret);
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003890
Ilya Dryomov922dab62016-05-26 01:15:02 +02003891 rbd_dev->watch_handle = NULL;
Ilya Dryomovc525f032016-04-28 16:07:26 +02003892}
3893
Ilya Dryomov99d16942016-08-12 16:11:41 +02003894static int rbd_register_watch(struct rbd_device *rbd_dev)
Ilya Dryomovc525f032016-04-28 16:07:26 +02003895{
Ilya Dryomov99d16942016-08-12 16:11:41 +02003896 int ret;
Ilya Dryomov811c6682016-04-15 16:22:16 +02003897
Ilya Dryomov99d16942016-08-12 16:11:41 +02003898 mutex_lock(&rbd_dev->watch_mutex);
3899 rbd_assert(rbd_dev->watch_state == RBD_WATCH_STATE_UNREGISTERED);
3900 ret = __rbd_register_watch(rbd_dev);
3901 if (ret)
3902 goto out;
3903
3904 rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED;
3905 rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id;
3906
3907out:
3908 mutex_unlock(&rbd_dev->watch_mutex);
3909 return ret;
3910}
3911
3912static void cancel_tasks_sync(struct rbd_device *rbd_dev)
3913{
3914 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3915
3916 cancel_delayed_work_sync(&rbd_dev->watch_dwork);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003917 cancel_work_sync(&rbd_dev->acquired_lock_work);
3918 cancel_work_sync(&rbd_dev->released_lock_work);
3919 cancel_delayed_work_sync(&rbd_dev->lock_dwork);
3920 cancel_work_sync(&rbd_dev->unlock_work);
Ilya Dryomov99d16942016-08-12 16:11:41 +02003921}
3922
3923static void rbd_unregister_watch(struct rbd_device *rbd_dev)
3924{
Ilya Dryomoved95b212016-08-12 16:40:02 +02003925 WARN_ON(waitqueue_active(&rbd_dev->lock_waitq));
Ilya Dryomov99d16942016-08-12 16:11:41 +02003926 cancel_tasks_sync(rbd_dev);
3927
3928 mutex_lock(&rbd_dev->watch_mutex);
3929 if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED)
3930 __rbd_unregister_watch(rbd_dev);
3931 rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED;
3932 mutex_unlock(&rbd_dev->watch_mutex);
3933
Ilya Dryomov811c6682016-04-15 16:22:16 +02003934 ceph_osdc_flush_notifies(&rbd_dev->rbd_client->client->osdc);
Ilya Dryomovfca27062013-12-16 18:02:40 +02003935}
3936
Ilya Dryomov99d16942016-08-12 16:11:41 +02003937static void rbd_reregister_watch(struct work_struct *work)
3938{
3939 struct rbd_device *rbd_dev = container_of(to_delayed_work(work),
3940 struct rbd_device, watch_dwork);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003941 bool was_lock_owner = false;
Ilya Dryomov99d16942016-08-12 16:11:41 +02003942 int ret;
3943
3944 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3945
Ilya Dryomoved95b212016-08-12 16:40:02 +02003946 down_write(&rbd_dev->lock_rwsem);
3947 if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED)
3948 was_lock_owner = rbd_release_lock(rbd_dev);
3949
Ilya Dryomov99d16942016-08-12 16:11:41 +02003950 mutex_lock(&rbd_dev->watch_mutex);
3951 if (rbd_dev->watch_state != RBD_WATCH_STATE_ERROR)
3952 goto fail_unlock;
3953
3954 ret = __rbd_register_watch(rbd_dev);
3955 if (ret) {
3956 rbd_warn(rbd_dev, "failed to reregister watch: %d", ret);
3957 if (ret != -EBLACKLISTED)
3958 queue_delayed_work(rbd_dev->task_wq,
3959 &rbd_dev->watch_dwork,
3960 RBD_RETRY_DELAY);
3961 goto fail_unlock;
3962 }
3963
3964 rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED;
3965 rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id;
3966 mutex_unlock(&rbd_dev->watch_mutex);
3967
3968 ret = rbd_dev_refresh(rbd_dev);
3969 if (ret)
3970 rbd_warn(rbd_dev, "reregisteration refresh failed: %d", ret);
3971
Ilya Dryomoved95b212016-08-12 16:40:02 +02003972 if (was_lock_owner) {
3973 ret = rbd_try_lock(rbd_dev);
3974 if (ret)
3975 rbd_warn(rbd_dev, "reregisteration lock failed: %d",
3976 ret);
3977 }
3978
3979 up_write(&rbd_dev->lock_rwsem);
3980 wake_requests(rbd_dev, true);
Ilya Dryomov99d16942016-08-12 16:11:41 +02003981 return;
3982
3983fail_unlock:
3984 mutex_unlock(&rbd_dev->watch_mutex);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003985 up_write(&rbd_dev->lock_rwsem);
Ilya Dryomov99d16942016-08-12 16:11:41 +02003986}
3987
Alex Elder36be9a72013-01-19 00:30:28 -06003988/*
Alex Elderf40eb342013-04-25 15:09:42 -05003989 * Synchronous osd object method call. Returns the number of bytes
3990 * returned in the outbound buffer, or a negative error code.
Alex Elder36be9a72013-01-19 00:30:28 -06003991 */
3992static int rbd_obj_method_sync(struct rbd_device *rbd_dev,
3993 const char *object_name,
3994 const char *class_name,
3995 const char *method_name,
Alex Elder41579762013-04-21 12:14:45 -05003996 const void *outbound,
Alex Elder36be9a72013-01-19 00:30:28 -06003997 size_t outbound_size,
Alex Elder41579762013-04-21 12:14:45 -05003998 void *inbound,
Alex Eldere2a58ee2013-04-30 00:44:33 -05003999 size_t inbound_size)
Alex Elder36be9a72013-01-19 00:30:28 -06004000{
Alex Elder21692382013-04-05 01:27:12 -05004001 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder36be9a72013-01-19 00:30:28 -06004002 struct rbd_obj_request *obj_request;
Alex Elder36be9a72013-01-19 00:30:28 -06004003 struct page **pages;
4004 u32 page_count;
4005 int ret;
4006
4007 /*
Alex Elder6010a452013-04-05 01:27:11 -05004008 * Method calls are ultimately read operations. The result
4009 * should placed into the inbound buffer provided. They
4010 * also supply outbound data--parameters for the object
4011 * method. Currently if this is present it will be a
4012 * snapshot id.
Alex Elder36be9a72013-01-19 00:30:28 -06004013 */
Alex Elder57385b52013-04-21 12:14:45 -05004014 page_count = (u32)calc_pages_for(0, inbound_size);
Alex Elder36be9a72013-01-19 00:30:28 -06004015 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
4016 if (IS_ERR(pages))
4017 return PTR_ERR(pages);
4018
4019 ret = -ENOMEM;
Alex Elder6010a452013-04-05 01:27:11 -05004020 obj_request = rbd_obj_request_create(object_name, 0, inbound_size,
Alex Elder36be9a72013-01-19 00:30:28 -06004021 OBJ_REQUEST_PAGES);
4022 if (!obj_request)
4023 goto out;
4024
4025 obj_request->pages = pages;
4026 obj_request->page_count = page_count;
4027
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004028 obj_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_READ, 1,
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02004029 obj_request);
Alex Elder36be9a72013-01-19 00:30:28 -06004030 if (!obj_request->osd_req)
4031 goto out;
4032
Alex Elderc99d2d42013-04-05 01:27:11 -05004033 osd_req_op_cls_init(obj_request->osd_req, 0, CEPH_OSD_OP_CALL,
Alex Elder04017e22013-04-05 14:46:02 -05004034 class_name, method_name);
4035 if (outbound_size) {
4036 struct ceph_pagelist *pagelist;
4037
4038 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
4039 if (!pagelist)
4040 goto out;
4041
4042 ceph_pagelist_init(pagelist);
4043 ceph_pagelist_append(pagelist, outbound, outbound_size);
4044 osd_req_op_cls_request_data_pagelist(obj_request->osd_req, 0,
4045 pagelist);
4046 }
Alex Eldera4ce40a2013-04-05 01:27:12 -05004047 osd_req_op_cls_response_data_pages(obj_request->osd_req, 0,
4048 obj_request->pages, inbound_size,
Alex Elder44cd1882013-04-05 01:27:12 -05004049 0, false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05004050 rbd_osd_req_format_read(obj_request);
Alex Elder430c28c2013-04-03 21:32:51 -05004051
Alex Elder36be9a72013-01-19 00:30:28 -06004052 ret = rbd_obj_request_submit(osdc, obj_request);
4053 if (ret)
4054 goto out;
4055 ret = rbd_obj_request_wait(obj_request);
4056 if (ret)
4057 goto out;
4058
4059 ret = obj_request->result;
4060 if (ret < 0)
4061 goto out;
Alex Elder57385b52013-04-21 12:14:45 -05004062
4063 rbd_assert(obj_request->xferred < (u64)INT_MAX);
4064 ret = (int)obj_request->xferred;
Alex Elder903bb322013-02-06 13:11:38 -06004065 ceph_copy_from_page_vector(pages, inbound, 0, obj_request->xferred);
Alex Elder36be9a72013-01-19 00:30:28 -06004066out:
4067 if (obj_request)
4068 rbd_obj_request_put(obj_request);
4069 else
4070 ceph_release_page_vector(pages, page_count);
4071
4072 return ret;
4073}
4074
Ilya Dryomoved95b212016-08-12 16:40:02 +02004075/*
4076 * lock_rwsem must be held for read
4077 */
4078static void rbd_wait_state_locked(struct rbd_device *rbd_dev)
4079{
4080 DEFINE_WAIT(wait);
4081
4082 do {
4083 /*
4084 * Note the use of mod_delayed_work() in rbd_acquire_lock()
4085 * and cancel_delayed_work() in wake_requests().
4086 */
4087 dout("%s rbd_dev %p queueing lock_dwork\n", __func__, rbd_dev);
4088 queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0);
4089 prepare_to_wait_exclusive(&rbd_dev->lock_waitq, &wait,
4090 TASK_UNINTERRUPTIBLE);
4091 up_read(&rbd_dev->lock_rwsem);
4092 schedule();
4093 down_read(&rbd_dev->lock_rwsem);
4094 } while (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED);
4095 finish_wait(&rbd_dev->lock_waitq, &wait);
4096}
4097
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004098static void rbd_queue_workfn(struct work_struct *work)
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004099{
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004100 struct request *rq = blk_mq_rq_from_pdu(work);
4101 struct rbd_device *rbd_dev = rq->q->queuedata;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004102 struct rbd_img_request *img_request;
Josh Durgin4e752f02014-04-08 11:12:11 -07004103 struct ceph_snap_context *snapc = NULL;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004104 u64 offset = (u64)blk_rq_pos(rq) << SECTOR_SHIFT;
4105 u64 length = blk_rq_bytes(rq);
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004106 enum obj_operation_type op_type;
Josh Durgin4e752f02014-04-08 11:12:11 -07004107 u64 mapping_size;
Ilya Dryomoved95b212016-08-12 16:40:02 +02004108 bool must_be_locked = false;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004109 int result;
4110
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004111 if (rq->cmd_type != REQ_TYPE_FS) {
4112 dout("%s: non-fs request type %d\n", __func__,
4113 (int) rq->cmd_type);
4114 result = -EIO;
4115 goto err;
4116 }
4117
Mike Christiec2df40d2016-06-05 14:32:17 -05004118 if (req_op(rq) == REQ_OP_DISCARD)
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004119 op_type = OBJ_OP_DISCARD;
Mike Christiec2df40d2016-06-05 14:32:17 -05004120 else if (req_op(rq) == REQ_OP_WRITE)
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004121 op_type = OBJ_OP_WRITE;
4122 else
4123 op_type = OBJ_OP_READ;
4124
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004125 /* Ignore/skip any zero-length requests */
4126
4127 if (!length) {
4128 dout("%s: zero-length request\n", __func__);
4129 result = 0;
4130 goto err_rq;
4131 }
4132
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004133 /* Only reads are allowed to a read-only device */
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004134
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004135 if (op_type != OBJ_OP_READ) {
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004136 if (rbd_dev->mapping.read_only) {
4137 result = -EROFS;
4138 goto err_rq;
4139 }
4140 rbd_assert(rbd_dev->spec->snap_id == CEPH_NOSNAP);
4141 }
4142
4143 /*
4144 * Quit early if the mapped snapshot no longer exists. It's
4145 * still possible the snapshot will have disappeared by the
4146 * time our request arrives at the osd, but there's no sense in
4147 * sending it if we already know.
4148 */
4149 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) {
4150 dout("request for non-existent snapshot");
4151 rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP);
4152 result = -ENXIO;
4153 goto err_rq;
4154 }
4155
4156 if (offset && length > U64_MAX - offset + 1) {
4157 rbd_warn(rbd_dev, "bad request range (%llu~%llu)", offset,
4158 length);
4159 result = -EINVAL;
4160 goto err_rq; /* Shouldn't happen */
4161 }
4162
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004163 blk_mq_start_request(rq);
4164
Josh Durgin4e752f02014-04-08 11:12:11 -07004165 down_read(&rbd_dev->header_rwsem);
4166 mapping_size = rbd_dev->mapping.size;
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004167 if (op_type != OBJ_OP_READ) {
Josh Durgin4e752f02014-04-08 11:12:11 -07004168 snapc = rbd_dev->header.snapc;
4169 ceph_get_snap_context(snapc);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004170 must_be_locked = rbd_is_lock_supported(rbd_dev);
Josh Durgin4e752f02014-04-08 11:12:11 -07004171 }
4172 up_read(&rbd_dev->header_rwsem);
4173
4174 if (offset + length > mapping_size) {
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004175 rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)", offset,
Josh Durgin4e752f02014-04-08 11:12:11 -07004176 length, mapping_size);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004177 result = -EIO;
4178 goto err_rq;
4179 }
4180
Ilya Dryomoved95b212016-08-12 16:40:02 +02004181 if (must_be_locked) {
4182 down_read(&rbd_dev->lock_rwsem);
4183 if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED)
4184 rbd_wait_state_locked(rbd_dev);
4185 }
4186
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004187 img_request = rbd_img_request_create(rbd_dev, offset, length, op_type,
Josh Durgin4e752f02014-04-08 11:12:11 -07004188 snapc);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004189 if (!img_request) {
4190 result = -ENOMEM;
Ilya Dryomoved95b212016-08-12 16:40:02 +02004191 goto err_unlock;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004192 }
4193 img_request->rq = rq;
Ilya Dryomov70b16db2015-11-27 19:23:24 +01004194 snapc = NULL; /* img_request consumes a ref */
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004195
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004196 if (op_type == OBJ_OP_DISCARD)
4197 result = rbd_img_request_fill(img_request, OBJ_REQUEST_NODATA,
4198 NULL);
4199 else
4200 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
4201 rq->bio);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004202 if (result)
4203 goto err_img_request;
4204
4205 result = rbd_img_request_submit(img_request);
4206 if (result)
4207 goto err_img_request;
4208
Ilya Dryomoved95b212016-08-12 16:40:02 +02004209 if (must_be_locked)
4210 up_read(&rbd_dev->lock_rwsem);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004211 return;
4212
4213err_img_request:
4214 rbd_img_request_put(img_request);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004215err_unlock:
4216 if (must_be_locked)
4217 up_read(&rbd_dev->lock_rwsem);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004218err_rq:
4219 if (result)
4220 rbd_warn(rbd_dev, "%s %llx at %llx result %d",
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004221 obj_op_name(op_type), length, offset, result);
SF Markus Elfringe96a6502014-11-02 15:20:59 +01004222 ceph_put_snap_context(snapc);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004223err:
4224 blk_mq_end_request(rq, result);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004225}
4226
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004227static int rbd_queue_rq(struct blk_mq_hw_ctx *hctx,
4228 const struct blk_mq_queue_data *bd)
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004229{
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004230 struct request *rq = bd->rq;
4231 struct work_struct *work = blk_mq_rq_to_pdu(rq);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004232
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004233 queue_work(rbd_wq, work);
4234 return BLK_MQ_RQ_QUEUE_OK;
Alex Elderbf0d5f502012-11-22 00:00:08 -06004235}
4236
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004237static void rbd_free_disk(struct rbd_device *rbd_dev)
4238{
4239 struct gendisk *disk = rbd_dev->disk;
4240
4241 if (!disk)
4242 return;
4243
Alex Eldera0cab922013-04-25 23:15:08 -05004244 rbd_dev->disk = NULL;
4245 if (disk->flags & GENHD_FL_UP) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004246 del_gendisk(disk);
Alex Eldera0cab922013-04-25 23:15:08 -05004247 if (disk->queue)
4248 blk_cleanup_queue(disk->queue);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004249 blk_mq_free_tag_set(&rbd_dev->tag_set);
Alex Eldera0cab922013-04-25 23:15:08 -05004250 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004251 put_disk(disk);
4252}
4253
Alex Elder788e2df2013-01-17 12:25:27 -06004254static int rbd_obj_read_sync(struct rbd_device *rbd_dev,
4255 const char *object_name,
Alex Elder7097f8d2013-04-30 00:44:33 -05004256 u64 offset, u64 length, void *buf)
Alex Elder788e2df2013-01-17 12:25:27 -06004257
4258{
Alex Elder21692382013-04-05 01:27:12 -05004259 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder788e2df2013-01-17 12:25:27 -06004260 struct rbd_obj_request *obj_request;
Alex Elder788e2df2013-01-17 12:25:27 -06004261 struct page **pages = NULL;
4262 u32 page_count;
Alex Elder1ceae7e2013-02-06 13:11:38 -06004263 size_t size;
Alex Elder788e2df2013-01-17 12:25:27 -06004264 int ret;
4265
4266 page_count = (u32) calc_pages_for(offset, length);
4267 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
4268 if (IS_ERR(pages))
Jan Karaa8d42052014-10-22 09:17:24 +02004269 return PTR_ERR(pages);
Alex Elder788e2df2013-01-17 12:25:27 -06004270
4271 ret = -ENOMEM;
4272 obj_request = rbd_obj_request_create(object_name, offset, length,
Alex Elder36be9a72013-01-19 00:30:28 -06004273 OBJ_REQUEST_PAGES);
Alex Elder788e2df2013-01-17 12:25:27 -06004274 if (!obj_request)
4275 goto out;
4276
4277 obj_request->pages = pages;
4278 obj_request->page_count = page_count;
4279
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004280 obj_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_READ, 1,
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02004281 obj_request);
Alex Elder788e2df2013-01-17 12:25:27 -06004282 if (!obj_request->osd_req)
4283 goto out;
4284
Alex Elderc99d2d42013-04-05 01:27:11 -05004285 osd_req_op_extent_init(obj_request->osd_req, 0, CEPH_OSD_OP_READ,
4286 offset, length, 0, 0);
Alex Elder406e2c92013-04-15 14:50:36 -05004287 osd_req_op_extent_osd_data_pages(obj_request->osd_req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05004288 obj_request->pages,
Alex Elder44cd1882013-04-05 01:27:12 -05004289 obj_request->length,
4290 obj_request->offset & ~PAGE_MASK,
4291 false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05004292 rbd_osd_req_format_read(obj_request);
Alex Elder430c28c2013-04-03 21:32:51 -05004293
Alex Elder788e2df2013-01-17 12:25:27 -06004294 ret = rbd_obj_request_submit(osdc, obj_request);
4295 if (ret)
4296 goto out;
4297 ret = rbd_obj_request_wait(obj_request);
4298 if (ret)
4299 goto out;
4300
4301 ret = obj_request->result;
4302 if (ret < 0)
4303 goto out;
Alex Elder1ceae7e2013-02-06 13:11:38 -06004304
4305 rbd_assert(obj_request->xferred <= (u64) SIZE_MAX);
4306 size = (size_t) obj_request->xferred;
Alex Elder903bb322013-02-06 13:11:38 -06004307 ceph_copy_from_page_vector(pages, buf, 0, size);
Alex Elder7097f8d2013-04-30 00:44:33 -05004308 rbd_assert(size <= (size_t)INT_MAX);
4309 ret = (int)size;
Alex Elder788e2df2013-01-17 12:25:27 -06004310out:
4311 if (obj_request)
4312 rbd_obj_request_put(obj_request);
4313 else
4314 ceph_release_page_vector(pages, page_count);
4315
4316 return ret;
4317}
4318
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004319/*
Alex Elder662518b2013-05-06 09:51:29 -05004320 * Read the complete header for the given rbd device. On successful
4321 * return, the rbd_dev->header field will contain up-to-date
4322 * information about the image.
Alex Elder4156d992012-08-02 11:29:46 -05004323 */
Alex Elder99a41eb2013-05-06 09:51:30 -05004324static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev)
Alex Elder4156d992012-08-02 11:29:46 -05004325{
4326 struct rbd_image_header_ondisk *ondisk = NULL;
4327 u32 snap_count = 0;
4328 u64 names_size = 0;
4329 u32 want_count;
4330 int ret;
4331
4332 /*
4333 * The complete header will include an array of its 64-bit
4334 * snapshot ids, followed by the names of those snapshots as
4335 * a contiguous block of NUL-terminated strings. Note that
4336 * the number of snapshots could change by the time we read
4337 * it in, in which case we re-read it.
4338 */
4339 do {
4340 size_t size;
4341
4342 kfree(ondisk);
4343
4344 size = sizeof (*ondisk);
4345 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
4346 size += names_size;
4347 ondisk = kmalloc(size, GFP_KERNEL);
4348 if (!ondisk)
Alex Elder662518b2013-05-06 09:51:29 -05004349 return -ENOMEM;
Alex Elder4156d992012-08-02 11:29:46 -05004350
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004351 ret = rbd_obj_read_sync(rbd_dev, rbd_dev->header_oid.name,
Alex Elder7097f8d2013-04-30 00:44:33 -05004352 0, size, ondisk);
Alex Elder4156d992012-08-02 11:29:46 -05004353 if (ret < 0)
Alex Elder662518b2013-05-06 09:51:29 -05004354 goto out;
Alex Elderc0cd10db2013-04-26 09:43:47 -05004355 if ((size_t)ret < size) {
Alex Elder4156d992012-08-02 11:29:46 -05004356 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05004357 rbd_warn(rbd_dev, "short header read (want %zd got %d)",
4358 size, ret);
Alex Elder662518b2013-05-06 09:51:29 -05004359 goto out;
Alex Elder4156d992012-08-02 11:29:46 -05004360 }
4361 if (!rbd_dev_ondisk_valid(ondisk)) {
4362 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05004363 rbd_warn(rbd_dev, "invalid header");
Alex Elder662518b2013-05-06 09:51:29 -05004364 goto out;
Alex Elder4156d992012-08-02 11:29:46 -05004365 }
4366
4367 names_size = le64_to_cpu(ondisk->snap_names_len);
4368 want_count = snap_count;
4369 snap_count = le32_to_cpu(ondisk->snap_count);
4370 } while (snap_count != want_count);
4371
Alex Elder662518b2013-05-06 09:51:29 -05004372 ret = rbd_header_from_disk(rbd_dev, ondisk);
4373out:
Alex Elder4156d992012-08-02 11:29:46 -05004374 kfree(ondisk);
4375
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004376 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004377}
4378
Alex Elder15228ed2013-05-01 12:43:03 -05004379/*
4380 * Clear the rbd device's EXISTS flag if the snapshot it's mapped to
4381 * has disappeared from the (just updated) snapshot context.
4382 */
4383static void rbd_exists_validate(struct rbd_device *rbd_dev)
4384{
4385 u64 snap_id;
4386
4387 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags))
4388 return;
4389
4390 snap_id = rbd_dev->spec->snap_id;
4391 if (snap_id == CEPH_NOSNAP)
4392 return;
4393
4394 if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX)
4395 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
4396}
4397
Josh Durgin98752012013-08-29 17:26:31 -07004398static void rbd_dev_update_size(struct rbd_device *rbd_dev)
4399{
4400 sector_t size;
Josh Durgin98752012013-08-29 17:26:31 -07004401
4402 /*
Ilya Dryomov811c6682016-04-15 16:22:16 +02004403 * If EXISTS is not set, rbd_dev->disk may be NULL, so don't
4404 * try to update its size. If REMOVING is set, updating size
4405 * is just useless work since the device can't be opened.
Josh Durgin98752012013-08-29 17:26:31 -07004406 */
Ilya Dryomov811c6682016-04-15 16:22:16 +02004407 if (test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags) &&
4408 !test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) {
Josh Durgin98752012013-08-29 17:26:31 -07004409 size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE;
4410 dout("setting size to %llu sectors", (unsigned long long)size);
4411 set_capacity(rbd_dev->disk, size);
4412 revalidate_disk(rbd_dev->disk);
4413 }
4414}
4415
Alex Eldercc4a38bd2013-04-30 00:44:33 -05004416static int rbd_dev_refresh(struct rbd_device *rbd_dev)
Alex Elder1fe5e992012-07-25 09:32:41 -05004417{
Alex Eldere627db02013-05-06 07:40:30 -05004418 u64 mapping_size;
Alex Elder1fe5e992012-07-25 09:32:41 -05004419 int ret;
4420
Alex Eldercfbf6372013-05-31 17:40:45 -05004421 down_write(&rbd_dev->header_rwsem);
Alex Elder3b5cf2a2013-05-29 11:18:59 -05004422 mapping_size = rbd_dev->mapping.size;
Ilya Dryomova720ae02014-07-23 17:11:19 +04004423
4424 ret = rbd_dev_header_info(rbd_dev);
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04004425 if (ret)
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004426 goto out;
Alex Elder15228ed2013-05-01 12:43:03 -05004427
Ilya Dryomove8f59b52014-07-24 10:42:13 +04004428 /*
4429 * If there is a parent, see if it has disappeared due to the
4430 * mapped image getting flattened.
4431 */
4432 if (rbd_dev->parent) {
4433 ret = rbd_dev_v2_parent_info(rbd_dev);
4434 if (ret)
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004435 goto out;
Ilya Dryomove8f59b52014-07-24 10:42:13 +04004436 }
4437
Ilya Dryomov5ff11082014-07-23 17:11:21 +04004438 if (rbd_dev->spec->snap_id == CEPH_NOSNAP) {
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004439 rbd_dev->mapping.size = rbd_dev->header.image_size;
Ilya Dryomov5ff11082014-07-23 17:11:21 +04004440 } else {
4441 /* validate mapped snapshot's EXISTS flag */
4442 rbd_exists_validate(rbd_dev);
4443 }
Alex Elder15228ed2013-05-01 12:43:03 -05004444
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004445out:
Alex Eldercfbf6372013-05-31 17:40:45 -05004446 up_write(&rbd_dev->header_rwsem);
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004447 if (!ret && mapping_size != rbd_dev->mapping.size)
Josh Durgin98752012013-08-29 17:26:31 -07004448 rbd_dev_update_size(rbd_dev);
Alex Elder1fe5e992012-07-25 09:32:41 -05004449
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004450 return ret;
Alex Elder1fe5e992012-07-25 09:32:41 -05004451}
4452
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004453static int rbd_init_request(void *data, struct request *rq,
4454 unsigned int hctx_idx, unsigned int request_idx,
4455 unsigned int numa_node)
4456{
4457 struct work_struct *work = blk_mq_rq_to_pdu(rq);
4458
4459 INIT_WORK(work, rbd_queue_workfn);
4460 return 0;
4461}
4462
4463static struct blk_mq_ops rbd_mq_ops = {
4464 .queue_rq = rbd_queue_rq,
4465 .map_queue = blk_mq_map_queue,
4466 .init_request = rbd_init_request,
4467};
4468
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004469static int rbd_init_disk(struct rbd_device *rbd_dev)
4470{
4471 struct gendisk *disk;
4472 struct request_queue *q;
Alex Elder593a9e72012-02-07 12:03:37 -06004473 u64 segment_size;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004474 int err;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004475
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004476 /* create gendisk info */
Ilya Dryomov7e513d42013-12-16 19:26:32 +02004477 disk = alloc_disk(single_major ?
4478 (1 << RBD_SINGLE_MAJOR_PART_SHIFT) :
4479 RBD_MINORS_PER_MAJOR);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004480 if (!disk)
Alex Elder1fcdb8a2012-08-29 17:11:06 -05004481 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004482
Alex Elderf0f8cef2012-01-29 13:57:44 -06004483 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
Alex Elderde71a292012-07-03 16:01:19 -05004484 rbd_dev->dev_id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004485 disk->major = rbd_dev->major;
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004486 disk->first_minor = rbd_dev->minor;
Ilya Dryomov7e513d42013-12-16 19:26:32 +02004487 if (single_major)
4488 disk->flags |= GENHD_FL_EXT_DEVT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004489 disk->fops = &rbd_bd_ops;
4490 disk->private_data = rbd_dev;
4491
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004492 memset(&rbd_dev->tag_set, 0, sizeof(rbd_dev->tag_set));
4493 rbd_dev->tag_set.ops = &rbd_mq_ops;
Ilya Dryomovb5584182015-06-23 16:21:19 +03004494 rbd_dev->tag_set.queue_depth = rbd_dev->opts->queue_depth;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004495 rbd_dev->tag_set.numa_node = NUMA_NO_NODE;
Ilya Dryomovb5584182015-06-23 16:21:19 +03004496 rbd_dev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004497 rbd_dev->tag_set.nr_hw_queues = 1;
4498 rbd_dev->tag_set.cmd_size = sizeof(struct work_struct);
4499
4500 err = blk_mq_alloc_tag_set(&rbd_dev->tag_set);
4501 if (err)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004502 goto out_disk;
Josh Durgin029bcbd2011-07-22 11:35:23 -07004503
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004504 q = blk_mq_init_queue(&rbd_dev->tag_set);
4505 if (IS_ERR(q)) {
4506 err = PTR_ERR(q);
4507 goto out_tag_set;
4508 }
4509
Ilya Dryomovd8a2c892015-03-24 16:15:17 +03004510 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
4511 /* QUEUE_FLAG_ADD_RANDOM is off by default for blk-mq */
Alex Elder593a9e72012-02-07 12:03:37 -06004512
Josh Durgin029bcbd2011-07-22 11:35:23 -07004513 /* set io sizes to object size */
Alex Elder593a9e72012-02-07 12:03:37 -06004514 segment_size = rbd_obj_bytes(&rbd_dev->header);
4515 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
Ilya Dryomov0d9fde42015-10-07 16:09:35 +02004516 q->limits.max_sectors = queue_max_hw_sectors(q);
Ilya Dryomovd3834fe2015-06-12 19:19:02 +03004517 blk_queue_max_segments(q, segment_size / SECTOR_SIZE);
Alex Elder593a9e72012-02-07 12:03:37 -06004518 blk_queue_max_segment_size(q, segment_size);
4519 blk_queue_io_min(q, segment_size);
4520 blk_queue_io_opt(q, segment_size);
Josh Durgin029bcbd2011-07-22 11:35:23 -07004521
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004522 /* enable the discard support */
4523 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
4524 q->limits.discard_granularity = segment_size;
4525 q->limits.discard_alignment = segment_size;
Jens Axboe2bb4cd52015-07-14 08:15:12 -06004526 blk_queue_max_discard_sectors(q, segment_size / SECTOR_SIZE);
Josh Durginb76f8232014-04-07 16:52:03 -07004527 q->limits.discard_zeroes_data = 1;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004528
Ronny Hegewaldbae818e2015-10-15 18:50:46 +00004529 if (!ceph_test_opt(rbd_dev->rbd_client->client, NOCRC))
4530 q->backing_dev_info.capabilities |= BDI_CAP_STABLE_WRITES;
4531
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004532 disk->queue = q;
4533
4534 q->queuedata = rbd_dev;
4535
4536 rbd_dev->disk = disk;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004537
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004538 return 0;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004539out_tag_set:
4540 blk_mq_free_tag_set(&rbd_dev->tag_set);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004541out_disk:
4542 put_disk(disk);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004543 return err;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004544}
4545
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004546/*
4547 sysfs
4548*/
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004549
Alex Elder593a9e72012-02-07 12:03:37 -06004550static struct rbd_device *dev_to_rbd_dev(struct device *dev)
4551{
4552 return container_of(dev, struct rbd_device, dev);
4553}
4554
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004555static ssize_t rbd_size_show(struct device *dev,
4556 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004557{
Alex Elder593a9e72012-02-07 12:03:37 -06004558 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004559
Alex Elderfc71d832013-04-26 15:44:36 -05004560 return sprintf(buf, "%llu\n",
4561 (unsigned long long)rbd_dev->mapping.size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004562}
4563
Alex Elder34b13182012-07-13 20:35:12 -05004564/*
4565 * Note this shows the features for whatever's mapped, which is not
4566 * necessarily the base image.
4567 */
4568static ssize_t rbd_features_show(struct device *dev,
4569 struct device_attribute *attr, char *buf)
4570{
4571 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4572
4573 return sprintf(buf, "0x%016llx\n",
Alex Elderfc71d832013-04-26 15:44:36 -05004574 (unsigned long long)rbd_dev->mapping.features);
Alex Elder34b13182012-07-13 20:35:12 -05004575}
4576
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004577static ssize_t rbd_major_show(struct device *dev,
4578 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004579{
Alex Elder593a9e72012-02-07 12:03:37 -06004580 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004581
Alex Elderfc71d832013-04-26 15:44:36 -05004582 if (rbd_dev->major)
4583 return sprintf(buf, "%d\n", rbd_dev->major);
4584
4585 return sprintf(buf, "(none)\n");
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004586}
Alex Elderfc71d832013-04-26 15:44:36 -05004587
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004588static ssize_t rbd_minor_show(struct device *dev,
4589 struct device_attribute *attr, char *buf)
4590{
4591 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4592
4593 return sprintf(buf, "%d\n", rbd_dev->minor);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004594}
4595
Ilya Dryomov005a07bf2016-08-18 18:38:43 +02004596static ssize_t rbd_client_addr_show(struct device *dev,
4597 struct device_attribute *attr, char *buf)
4598{
4599 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4600 struct ceph_entity_addr *client_addr =
4601 ceph_client_addr(rbd_dev->rbd_client->client);
4602
4603 return sprintf(buf, "%pISpc/%u\n", &client_addr->in_addr,
4604 le32_to_cpu(client_addr->nonce));
4605}
4606
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004607static ssize_t rbd_client_id_show(struct device *dev,
4608 struct device_attribute *attr, char *buf)
4609{
Alex Elder593a9e72012-02-07 12:03:37 -06004610 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004611
Alex Elder1dbb4392012-01-24 10:08:37 -06004612 return sprintf(buf, "client%lld\n",
Ilya Dryomov033268a2016-08-12 14:59:58 +02004613 ceph_client_gid(rbd_dev->rbd_client->client));
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004614}
4615
Mike Christie267fb902016-08-18 18:38:43 +02004616static ssize_t rbd_cluster_fsid_show(struct device *dev,
4617 struct device_attribute *attr, char *buf)
4618{
4619 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4620
4621 return sprintf(buf, "%pU\n", &rbd_dev->rbd_client->client->fsid);
4622}
4623
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02004624static ssize_t rbd_config_info_show(struct device *dev,
4625 struct device_attribute *attr, char *buf)
4626{
4627 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4628
4629 return sprintf(buf, "%s\n", rbd_dev->config_info);
4630}
4631
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004632static ssize_t rbd_pool_show(struct device *dev,
4633 struct device_attribute *attr, char *buf)
4634{
Alex Elder593a9e72012-02-07 12:03:37 -06004635 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004636
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004637 return sprintf(buf, "%s\n", rbd_dev->spec->pool_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004638}
4639
Alex Elder9bb2f332012-07-12 10:46:35 -05004640static ssize_t rbd_pool_id_show(struct device *dev,
4641 struct device_attribute *attr, char *buf)
4642{
4643 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4644
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004645 return sprintf(buf, "%llu\n",
Alex Elderfc71d832013-04-26 15:44:36 -05004646 (unsigned long long) rbd_dev->spec->pool_id);
Alex Elder9bb2f332012-07-12 10:46:35 -05004647}
4648
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004649static ssize_t rbd_name_show(struct device *dev,
4650 struct device_attribute *attr, char *buf)
4651{
Alex Elder593a9e72012-02-07 12:03:37 -06004652 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004653
Alex Eldera92ffdf2012-10-30 19:40:33 -05004654 if (rbd_dev->spec->image_name)
4655 return sprintf(buf, "%s\n", rbd_dev->spec->image_name);
4656
4657 return sprintf(buf, "(unknown)\n");
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004658}
4659
Alex Elder589d30e2012-07-10 20:30:11 -05004660static ssize_t rbd_image_id_show(struct device *dev,
4661 struct device_attribute *attr, char *buf)
4662{
4663 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4664
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004665 return sprintf(buf, "%s\n", rbd_dev->spec->image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05004666}
4667
Alex Elder34b13182012-07-13 20:35:12 -05004668/*
4669 * Shows the name of the currently-mapped snapshot (or
4670 * RBD_SNAP_HEAD_NAME for the base image).
4671 */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004672static ssize_t rbd_snap_show(struct device *dev,
4673 struct device_attribute *attr,
4674 char *buf)
4675{
Alex Elder593a9e72012-02-07 12:03:37 -06004676 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004677
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004678 return sprintf(buf, "%s\n", rbd_dev->spec->snap_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004679}
4680
Mike Christie92a58672016-08-18 18:38:44 +02004681static ssize_t rbd_snap_id_show(struct device *dev,
4682 struct device_attribute *attr, char *buf)
4683{
4684 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4685
4686 return sprintf(buf, "%llu\n", rbd_dev->spec->snap_id);
4687}
4688
Alex Elder86b00e02012-10-25 23:34:42 -05004689/*
Ilya Dryomovff961282014-07-22 21:53:07 +04004690 * For a v2 image, shows the chain of parent images, separated by empty
4691 * lines. For v1 images or if there is no parent, shows "(no parent
4692 * image)".
Alex Elder86b00e02012-10-25 23:34:42 -05004693 */
4694static ssize_t rbd_parent_show(struct device *dev,
Ilya Dryomovff961282014-07-22 21:53:07 +04004695 struct device_attribute *attr,
4696 char *buf)
Alex Elder86b00e02012-10-25 23:34:42 -05004697{
4698 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Ilya Dryomovff961282014-07-22 21:53:07 +04004699 ssize_t count = 0;
Alex Elder86b00e02012-10-25 23:34:42 -05004700
Ilya Dryomovff961282014-07-22 21:53:07 +04004701 if (!rbd_dev->parent)
Alex Elder86b00e02012-10-25 23:34:42 -05004702 return sprintf(buf, "(no parent image)\n");
4703
Ilya Dryomovff961282014-07-22 21:53:07 +04004704 for ( ; rbd_dev->parent; rbd_dev = rbd_dev->parent) {
4705 struct rbd_spec *spec = rbd_dev->parent_spec;
Alex Elder86b00e02012-10-25 23:34:42 -05004706
Ilya Dryomovff961282014-07-22 21:53:07 +04004707 count += sprintf(&buf[count], "%s"
4708 "pool_id %llu\npool_name %s\n"
4709 "image_id %s\nimage_name %s\n"
4710 "snap_id %llu\nsnap_name %s\n"
4711 "overlap %llu\n",
4712 !count ? "" : "\n", /* first? */
4713 spec->pool_id, spec->pool_name,
4714 spec->image_id, spec->image_name ?: "(unknown)",
4715 spec->snap_id, spec->snap_name,
4716 rbd_dev->parent_overlap);
4717 }
Alex Elder86b00e02012-10-25 23:34:42 -05004718
Ilya Dryomovff961282014-07-22 21:53:07 +04004719 return count;
Alex Elder86b00e02012-10-25 23:34:42 -05004720}
4721
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004722static ssize_t rbd_image_refresh(struct device *dev,
4723 struct device_attribute *attr,
4724 const char *buf,
4725 size_t size)
4726{
Alex Elder593a9e72012-02-07 12:03:37 -06004727 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Alex Elderb8136232012-07-25 09:32:41 -05004728 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004729
Alex Eldercc4a38bd2013-04-30 00:44:33 -05004730 ret = rbd_dev_refresh(rbd_dev);
Alex Eldere627db02013-05-06 07:40:30 -05004731 if (ret)
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04004732 return ret;
Alex Elderb8136232012-07-25 09:32:41 -05004733
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04004734 return size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004735}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004736
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004737static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
Alex Elder34b13182012-07-13 20:35:12 -05004738static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004739static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004740static DEVICE_ATTR(minor, S_IRUGO, rbd_minor_show, NULL);
Ilya Dryomov005a07bf2016-08-18 18:38:43 +02004741static DEVICE_ATTR(client_addr, S_IRUGO, rbd_client_addr_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004742static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
Mike Christie267fb902016-08-18 18:38:43 +02004743static DEVICE_ATTR(cluster_fsid, S_IRUGO, rbd_cluster_fsid_show, NULL);
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02004744static DEVICE_ATTR(config_info, S_IRUSR, rbd_config_info_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004745static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
Alex Elder9bb2f332012-07-12 10:46:35 -05004746static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004747static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
Alex Elder589d30e2012-07-10 20:30:11 -05004748static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004749static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
4750static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
Mike Christie92a58672016-08-18 18:38:44 +02004751static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL);
Alex Elder86b00e02012-10-25 23:34:42 -05004752static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004753
4754static struct attribute *rbd_attrs[] = {
4755 &dev_attr_size.attr,
Alex Elder34b13182012-07-13 20:35:12 -05004756 &dev_attr_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004757 &dev_attr_major.attr,
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004758 &dev_attr_minor.attr,
Ilya Dryomov005a07bf2016-08-18 18:38:43 +02004759 &dev_attr_client_addr.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004760 &dev_attr_client_id.attr,
Mike Christie267fb902016-08-18 18:38:43 +02004761 &dev_attr_cluster_fsid.attr,
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02004762 &dev_attr_config_info.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004763 &dev_attr_pool.attr,
Alex Elder9bb2f332012-07-12 10:46:35 -05004764 &dev_attr_pool_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004765 &dev_attr_name.attr,
Alex Elder589d30e2012-07-10 20:30:11 -05004766 &dev_attr_image_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004767 &dev_attr_current_snap.attr,
Mike Christie92a58672016-08-18 18:38:44 +02004768 &dev_attr_snap_id.attr,
Alex Elder86b00e02012-10-25 23:34:42 -05004769 &dev_attr_parent.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004770 &dev_attr_refresh.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004771 NULL
4772};
4773
4774static struct attribute_group rbd_attr_group = {
4775 .attrs = rbd_attrs,
4776};
4777
4778static const struct attribute_group *rbd_attr_groups[] = {
4779 &rbd_attr_group,
4780 NULL
4781};
4782
Ilya Dryomov6cac4692015-10-16 20:11:25 +02004783static void rbd_dev_release(struct device *dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004784
4785static struct device_type rbd_device_type = {
4786 .name = "rbd",
4787 .groups = rbd_attr_groups,
Ilya Dryomov6cac4692015-10-16 20:11:25 +02004788 .release = rbd_dev_release,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004789};
4790
Alex Elder8b8fb992012-10-26 17:25:24 -05004791static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)
4792{
4793 kref_get(&spec->kref);
4794
4795 return spec;
4796}
4797
4798static void rbd_spec_free(struct kref *kref);
4799static void rbd_spec_put(struct rbd_spec *spec)
4800{
4801 if (spec)
4802 kref_put(&spec->kref, rbd_spec_free);
4803}
4804
4805static struct rbd_spec *rbd_spec_alloc(void)
4806{
4807 struct rbd_spec *spec;
4808
4809 spec = kzalloc(sizeof (*spec), GFP_KERNEL);
4810 if (!spec)
4811 return NULL;
Ilya Dryomov04077592014-07-23 17:11:20 +04004812
4813 spec->pool_id = CEPH_NOPOOL;
4814 spec->snap_id = CEPH_NOSNAP;
Alex Elder8b8fb992012-10-26 17:25:24 -05004815 kref_init(&spec->kref);
4816
Alex Elder8b8fb992012-10-26 17:25:24 -05004817 return spec;
4818}
4819
4820static void rbd_spec_free(struct kref *kref)
4821{
4822 struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref);
4823
4824 kfree(spec->pool_name);
4825 kfree(spec->image_id);
4826 kfree(spec->image_name);
4827 kfree(spec->snap_name);
4828 kfree(spec);
4829}
4830
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004831static void rbd_dev_free(struct rbd_device *rbd_dev)
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004832{
Ilya Dryomov99d16942016-08-12 16:11:41 +02004833 WARN_ON(rbd_dev->watch_state != RBD_WATCH_STATE_UNREGISTERED);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004834 WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_UNLOCKED);
Ilya Dryomov99d16942016-08-12 16:11:41 +02004835
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004836 ceph_oid_destroy(&rbd_dev->header_oid);
Ilya Dryomov6b6dddb2016-08-05 16:15:38 +02004837 ceph_oloc_destroy(&rbd_dev->header_oloc);
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02004838 kfree(rbd_dev->config_info);
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004839
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004840 rbd_put_client(rbd_dev->rbd_client);
4841 rbd_spec_put(rbd_dev->spec);
4842 kfree(rbd_dev->opts);
4843 kfree(rbd_dev);
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004844}
4845
4846static void rbd_dev_release(struct device *dev)
4847{
4848 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4849 bool need_put = !!rbd_dev->opts;
4850
4851 if (need_put) {
4852 destroy_workqueue(rbd_dev->task_wq);
4853 ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id);
4854 }
4855
4856 rbd_dev_free(rbd_dev);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004857
4858 /*
4859 * This is racy, but way better than putting module outside of
4860 * the release callback. The race window is pretty small, so
4861 * doing something similar to dm (dm-builtin.c) is overkill.
4862 */
4863 if (need_put)
4864 module_put(THIS_MODULE);
4865}
4866
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004867static struct rbd_device *__rbd_dev_create(struct rbd_client *rbdc,
4868 struct rbd_spec *spec)
Alex Elderc53d5892012-10-25 23:34:42 -05004869{
4870 struct rbd_device *rbd_dev;
4871
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004872 rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL);
Alex Elderc53d5892012-10-25 23:34:42 -05004873 if (!rbd_dev)
4874 return NULL;
4875
4876 spin_lock_init(&rbd_dev->lock);
4877 INIT_LIST_HEAD(&rbd_dev->node);
Alex Elderc53d5892012-10-25 23:34:42 -05004878 init_rwsem(&rbd_dev->header_rwsem);
4879
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004880 ceph_oid_init(&rbd_dev->header_oid);
Ilya Dryomov922dab62016-05-26 01:15:02 +02004881 ceph_oloc_init(&rbd_dev->header_oloc);
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004882
Ilya Dryomov99d16942016-08-12 16:11:41 +02004883 mutex_init(&rbd_dev->watch_mutex);
4884 rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED;
4885 INIT_DELAYED_WORK(&rbd_dev->watch_dwork, rbd_reregister_watch);
4886
Ilya Dryomoved95b212016-08-12 16:40:02 +02004887 init_rwsem(&rbd_dev->lock_rwsem);
4888 rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED;
4889 INIT_WORK(&rbd_dev->acquired_lock_work, rbd_notify_acquired_lock);
4890 INIT_WORK(&rbd_dev->released_lock_work, rbd_notify_released_lock);
4891 INIT_DELAYED_WORK(&rbd_dev->lock_dwork, rbd_acquire_lock);
4892 INIT_WORK(&rbd_dev->unlock_work, rbd_release_lock_work);
4893 init_waitqueue_head(&rbd_dev->lock_waitq);
4894
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004895 rbd_dev->dev.bus = &rbd_bus_type;
4896 rbd_dev->dev.type = &rbd_device_type;
4897 rbd_dev->dev.parent = &rbd_root_dev;
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004898 device_initialize(&rbd_dev->dev);
4899
Alex Elderc53d5892012-10-25 23:34:42 -05004900 rbd_dev->rbd_client = rbdc;
Ilya Dryomovd1475432015-06-22 13:24:48 +03004901 rbd_dev->spec = spec;
Alex Elder0903e872012-11-14 12:25:19 -06004902
Yan, Zheng76271512016-02-03 21:24:49 +08004903 rbd_dev->layout.stripe_unit = 1 << RBD_MAX_OBJ_ORDER;
4904 rbd_dev->layout.stripe_count = 1;
4905 rbd_dev->layout.object_size = 1 << RBD_MAX_OBJ_ORDER;
4906 rbd_dev->layout.pool_id = spec->pool_id;
Yan, Zheng30c156d2016-02-14 11:24:31 +08004907 RCU_INIT_POINTER(rbd_dev->layout.pool_ns, NULL);
Alex Elder0903e872012-11-14 12:25:19 -06004908
Alex Elderc53d5892012-10-25 23:34:42 -05004909 return rbd_dev;
4910}
4911
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004912/*
4913 * Create a mapping rbd_dev.
4914 */
4915static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,
4916 struct rbd_spec *spec,
4917 struct rbd_options *opts)
4918{
4919 struct rbd_device *rbd_dev;
4920
4921 rbd_dev = __rbd_dev_create(rbdc, spec);
4922 if (!rbd_dev)
4923 return NULL;
4924
4925 rbd_dev->opts = opts;
4926
4927 /* get an id and fill in device name */
4928 rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0,
4929 minor_to_rbd_dev_id(1 << MINORBITS),
4930 GFP_KERNEL);
4931 if (rbd_dev->dev_id < 0)
4932 goto fail_rbd_dev;
4933
4934 sprintf(rbd_dev->name, RBD_DRV_NAME "%d", rbd_dev->dev_id);
4935 rbd_dev->task_wq = alloc_ordered_workqueue("%s-tasks", WQ_MEM_RECLAIM,
4936 rbd_dev->name);
4937 if (!rbd_dev->task_wq)
4938 goto fail_dev_id;
4939
4940 /* we have a ref from do_rbd_add() */
4941 __module_get(THIS_MODULE);
4942
4943 dout("%s rbd_dev %p dev_id %d\n", __func__, rbd_dev, rbd_dev->dev_id);
4944 return rbd_dev;
4945
4946fail_dev_id:
4947 ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id);
4948fail_rbd_dev:
4949 rbd_dev_free(rbd_dev);
4950 return NULL;
4951}
4952
Alex Elderc53d5892012-10-25 23:34:42 -05004953static void rbd_dev_destroy(struct rbd_device *rbd_dev)
4954{
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004955 if (rbd_dev)
4956 put_device(&rbd_dev->dev);
Alex Elderc53d5892012-10-25 23:34:42 -05004957}
4958
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004959/*
Alex Elder9d475de2012-07-03 16:01:19 -05004960 * Get the size and object order for an image snapshot, or if
4961 * snap_id is CEPH_NOSNAP, gets this information for the base
4962 * image.
4963 */
4964static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
4965 u8 *order, u64 *snap_size)
4966{
4967 __le64 snapid = cpu_to_le64(snap_id);
4968 int ret;
4969 struct {
4970 u8 order;
4971 __le64 size;
4972 } __attribute__ ((packed)) size_buf = { 0 };
4973
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004974 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name,
Alex Elder9d475de2012-07-03 16:01:19 -05004975 "rbd", "get_size",
Alex Elder41579762013-04-21 12:14:45 -05004976 &snapid, sizeof (snapid),
Alex Eldere2a58ee2013-04-30 00:44:33 -05004977 &size_buf, sizeof (size_buf));
Alex Elder36be9a72013-01-19 00:30:28 -06004978 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder9d475de2012-07-03 16:01:19 -05004979 if (ret < 0)
4980 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05004981 if (ret < sizeof (size_buf))
4982 return -ERANGE;
Alex Elder9d475de2012-07-03 16:01:19 -05004983
Josh Durginc3545572013-08-28 17:08:10 -07004984 if (order) {
Alex Elderc86f86e2013-04-25 15:09:41 -05004985 *order = size_buf.order;
Josh Durginc3545572013-08-28 17:08:10 -07004986 dout(" order %u", (unsigned int)*order);
4987 }
Alex Elder9d475de2012-07-03 16:01:19 -05004988 *snap_size = le64_to_cpu(size_buf.size);
4989
Josh Durginc3545572013-08-28 17:08:10 -07004990 dout(" snap_id 0x%016llx snap_size = %llu\n",
4991 (unsigned long long)snap_id,
Alex Elder57385b52013-04-21 12:14:45 -05004992 (unsigned long long)*snap_size);
Alex Elder9d475de2012-07-03 16:01:19 -05004993
4994 return 0;
4995}
4996
4997static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
4998{
4999 return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
5000 &rbd_dev->header.obj_order,
5001 &rbd_dev->header.image_size);
5002}
5003
Alex Elder1e130192012-07-03 16:01:19 -05005004static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
5005{
5006 void *reply_buf;
5007 int ret;
5008 void *p;
5009
5010 reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL);
5011 if (!reply_buf)
5012 return -ENOMEM;
5013
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005014 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name,
Alex Elder41579762013-04-21 12:14:45 -05005015 "rbd", "get_object_prefix", NULL, 0,
Alex Eldere2a58ee2013-04-30 00:44:33 -05005016 reply_buf, RBD_OBJ_PREFIX_LEN_MAX);
Alex Elder36be9a72013-01-19 00:30:28 -06005017 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder1e130192012-07-03 16:01:19 -05005018 if (ret < 0)
5019 goto out;
5020
5021 p = reply_buf;
5022 rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p,
Alex Elder57385b52013-04-21 12:14:45 -05005023 p + ret, NULL, GFP_NOIO);
5024 ret = 0;
Alex Elder1e130192012-07-03 16:01:19 -05005025
5026 if (IS_ERR(rbd_dev->header.object_prefix)) {
5027 ret = PTR_ERR(rbd_dev->header.object_prefix);
5028 rbd_dev->header.object_prefix = NULL;
5029 } else {
5030 dout(" object_prefix = %s\n", rbd_dev->header.object_prefix);
5031 }
Alex Elder1e130192012-07-03 16:01:19 -05005032out:
5033 kfree(reply_buf);
5034
5035 return ret;
5036}
5037
Alex Elderb1b54022012-07-03 16:01:19 -05005038static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
5039 u64 *snap_features)
5040{
5041 __le64 snapid = cpu_to_le64(snap_id);
5042 struct {
5043 __le64 features;
5044 __le64 incompat;
Alex Elder41579762013-04-21 12:14:45 -05005045 } __attribute__ ((packed)) features_buf = { 0 };
Ilya Dryomovd3767f02016-04-13 14:15:50 +02005046 u64 unsup;
Alex Elderb1b54022012-07-03 16:01:19 -05005047 int ret;
5048
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005049 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name,
Alex Elderb1b54022012-07-03 16:01:19 -05005050 "rbd", "get_features",
Alex Elder41579762013-04-21 12:14:45 -05005051 &snapid, sizeof (snapid),
Alex Eldere2a58ee2013-04-30 00:44:33 -05005052 &features_buf, sizeof (features_buf));
Alex Elder36be9a72013-01-19 00:30:28 -06005053 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderb1b54022012-07-03 16:01:19 -05005054 if (ret < 0)
5055 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05005056 if (ret < sizeof (features_buf))
5057 return -ERANGE;
Alex Elderd8891402012-10-09 13:50:17 -07005058
Ilya Dryomovd3767f02016-04-13 14:15:50 +02005059 unsup = le64_to_cpu(features_buf.incompat) & ~RBD_FEATURES_SUPPORTED;
5060 if (unsup) {
5061 rbd_warn(rbd_dev, "image uses unsupported features: 0x%llx",
5062 unsup);
Alex Elderb8f5c6e2012-11-01 08:39:26 -05005063 return -ENXIO;
Ilya Dryomovd3767f02016-04-13 14:15:50 +02005064 }
Alex Elderd8891402012-10-09 13:50:17 -07005065
Alex Elderb1b54022012-07-03 16:01:19 -05005066 *snap_features = le64_to_cpu(features_buf.features);
5067
5068 dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
Alex Elder57385b52013-04-21 12:14:45 -05005069 (unsigned long long)snap_id,
5070 (unsigned long long)*snap_features,
5071 (unsigned long long)le64_to_cpu(features_buf.incompat));
Alex Elderb1b54022012-07-03 16:01:19 -05005072
5073 return 0;
5074}
5075
5076static int rbd_dev_v2_features(struct rbd_device *rbd_dev)
5077{
5078 return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP,
5079 &rbd_dev->header.features);
5080}
5081
Alex Elder86b00e02012-10-25 23:34:42 -05005082static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev)
5083{
5084 struct rbd_spec *parent_spec;
5085 size_t size;
5086 void *reply_buf = NULL;
5087 __le64 snapid;
5088 void *p;
5089 void *end;
Alex Elder642a2532013-05-06 17:40:33 -05005090 u64 pool_id;
Alex Elder86b00e02012-10-25 23:34:42 -05005091 char *image_id;
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005092 u64 snap_id;
Alex Elder86b00e02012-10-25 23:34:42 -05005093 u64 overlap;
Alex Elder86b00e02012-10-25 23:34:42 -05005094 int ret;
5095
5096 parent_spec = rbd_spec_alloc();
5097 if (!parent_spec)
5098 return -ENOMEM;
5099
5100 size = sizeof (__le64) + /* pool_id */
5101 sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */
5102 sizeof (__le64) + /* snap_id */
5103 sizeof (__le64); /* overlap */
5104 reply_buf = kmalloc(size, GFP_KERNEL);
5105 if (!reply_buf) {
5106 ret = -ENOMEM;
5107 goto out_err;
5108 }
5109
Ilya Dryomov4d9b67c2014-07-24 10:42:13 +04005110 snapid = cpu_to_le64(rbd_dev->spec->snap_id);
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005111 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name,
Alex Elder86b00e02012-10-25 23:34:42 -05005112 "rbd", "get_parent",
Alex Elder41579762013-04-21 12:14:45 -05005113 &snapid, sizeof (snapid),
Alex Eldere2a58ee2013-04-30 00:44:33 -05005114 reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06005115 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder86b00e02012-10-25 23:34:42 -05005116 if (ret < 0)
5117 goto out_err;
5118
Alex Elder86b00e02012-10-25 23:34:42 -05005119 p = reply_buf;
Alex Elder57385b52013-04-21 12:14:45 -05005120 end = reply_buf + ret;
5121 ret = -ERANGE;
Alex Elder642a2532013-05-06 17:40:33 -05005122 ceph_decode_64_safe(&p, end, pool_id, out_err);
Alex Elder392a9da2013-05-06 17:40:33 -05005123 if (pool_id == CEPH_NOPOOL) {
5124 /*
5125 * Either the parent never existed, or we have
5126 * record of it but the image got flattened so it no
5127 * longer has a parent. When the parent of a
5128 * layered image disappears we immediately set the
5129 * overlap to 0. The effect of this is that all new
5130 * requests will be treated as if the image had no
5131 * parent.
5132 */
5133 if (rbd_dev->parent_overlap) {
5134 rbd_dev->parent_overlap = 0;
Alex Elder392a9da2013-05-06 17:40:33 -05005135 rbd_dev_parent_put(rbd_dev);
5136 pr_info("%s: clone image has been flattened\n",
5137 rbd_dev->disk->disk_name);
5138 }
5139
Alex Elder86b00e02012-10-25 23:34:42 -05005140 goto out; /* No parent? No problem. */
Alex Elder392a9da2013-05-06 17:40:33 -05005141 }
Alex Elder86b00e02012-10-25 23:34:42 -05005142
Alex Elder0903e872012-11-14 12:25:19 -06005143 /* The ceph file layout needs to fit pool id in 32 bits */
5144
5145 ret = -EIO;
Alex Elder642a2532013-05-06 17:40:33 -05005146 if (pool_id > (u64)U32_MAX) {
Ilya Dryomov9584d502014-07-11 12:11:20 +04005147 rbd_warn(NULL, "parent pool id too large (%llu > %u)",
Alex Elder642a2532013-05-06 17:40:33 -05005148 (unsigned long long)pool_id, U32_MAX);
Alex Elder57385b52013-04-21 12:14:45 -05005149 goto out_err;
Alex Elderc0cd10db2013-04-26 09:43:47 -05005150 }
Alex Elder0903e872012-11-14 12:25:19 -06005151
Alex Elder979ed482012-11-01 08:39:26 -05005152 image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elder86b00e02012-10-25 23:34:42 -05005153 if (IS_ERR(image_id)) {
5154 ret = PTR_ERR(image_id);
5155 goto out_err;
5156 }
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005157 ceph_decode_64_safe(&p, end, snap_id, out_err);
Alex Elder86b00e02012-10-25 23:34:42 -05005158 ceph_decode_64_safe(&p, end, overlap, out_err);
5159
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005160 /*
5161 * The parent won't change (except when the clone is
5162 * flattened, already handled that). So we only need to
5163 * record the parent spec we have not already done so.
5164 */
5165 if (!rbd_dev->parent_spec) {
5166 parent_spec->pool_id = pool_id;
5167 parent_spec->image_id = image_id;
5168 parent_spec->snap_id = snap_id;
Alex Elder70cf49c2013-05-06 17:40:33 -05005169 rbd_dev->parent_spec = parent_spec;
5170 parent_spec = NULL; /* rbd_dev now owns this */
Ilya Dryomovfbba11b2014-06-27 21:46:33 +04005171 } else {
5172 kfree(image_id);
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005173 }
5174
5175 /*
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005176 * We always update the parent overlap. If it's zero we issue
5177 * a warning, as we will proceed as if there was no parent.
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005178 */
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005179 if (!overlap) {
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005180 if (parent_spec) {
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005181 /* refresh, careful to warn just once */
5182 if (rbd_dev->parent_overlap)
5183 rbd_warn(rbd_dev,
5184 "clone now standalone (overlap became 0)");
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005185 } else {
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005186 /* initial probe */
5187 rbd_warn(rbd_dev, "clone is standalone (overlap 0)");
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005188 }
Alex Elder70cf49c2013-05-06 17:40:33 -05005189 }
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005190 rbd_dev->parent_overlap = overlap;
5191
Alex Elder86b00e02012-10-25 23:34:42 -05005192out:
5193 ret = 0;
5194out_err:
5195 kfree(reply_buf);
5196 rbd_spec_put(parent_spec);
5197
5198 return ret;
5199}
5200
Alex Eldercc070d52013-04-21 12:14:45 -05005201static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev)
5202{
5203 struct {
5204 __le64 stripe_unit;
5205 __le64 stripe_count;
5206 } __attribute__ ((packed)) striping_info_buf = { 0 };
5207 size_t size = sizeof (striping_info_buf);
5208 void *p;
5209 u64 obj_size;
5210 u64 stripe_unit;
5211 u64 stripe_count;
5212 int ret;
5213
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005214 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name,
Alex Eldercc070d52013-04-21 12:14:45 -05005215 "rbd", "get_stripe_unit_count", NULL, 0,
Alex Eldere2a58ee2013-04-30 00:44:33 -05005216 (char *)&striping_info_buf, size);
Alex Eldercc070d52013-04-21 12:14:45 -05005217 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
5218 if (ret < 0)
5219 return ret;
5220 if (ret < size)
5221 return -ERANGE;
5222
5223 /*
5224 * We don't actually support the "fancy striping" feature
5225 * (STRIPINGV2) yet, but if the striping sizes are the
5226 * defaults the behavior is the same as before. So find
5227 * out, and only fail if the image has non-default values.
5228 */
5229 ret = -EINVAL;
5230 obj_size = (u64)1 << rbd_dev->header.obj_order;
5231 p = &striping_info_buf;
5232 stripe_unit = ceph_decode_64(&p);
5233 if (stripe_unit != obj_size) {
5234 rbd_warn(rbd_dev, "unsupported stripe unit "
5235 "(got %llu want %llu)",
5236 stripe_unit, obj_size);
5237 return -EINVAL;
5238 }
5239 stripe_count = ceph_decode_64(&p);
5240 if (stripe_count != 1) {
5241 rbd_warn(rbd_dev, "unsupported stripe count "
5242 "(got %llu want 1)", stripe_count);
5243 return -EINVAL;
5244 }
Alex Elder500d0c02013-04-26 09:43:47 -05005245 rbd_dev->header.stripe_unit = stripe_unit;
5246 rbd_dev->header.stripe_count = stripe_count;
Alex Eldercc070d52013-04-21 12:14:45 -05005247
5248 return 0;
5249}
5250
Alex Elder9e15b772012-10-30 19:40:33 -05005251static char *rbd_dev_image_name(struct rbd_device *rbd_dev)
5252{
5253 size_t image_id_size;
5254 char *image_id;
5255 void *p;
5256 void *end;
5257 size_t size;
5258 void *reply_buf = NULL;
5259 size_t len = 0;
5260 char *image_name = NULL;
5261 int ret;
5262
5263 rbd_assert(!rbd_dev->spec->image_name);
5264
Alex Elder69e7a022012-11-01 08:39:26 -05005265 len = strlen(rbd_dev->spec->image_id);
5266 image_id_size = sizeof (__le32) + len;
Alex Elder9e15b772012-10-30 19:40:33 -05005267 image_id = kmalloc(image_id_size, GFP_KERNEL);
5268 if (!image_id)
5269 return NULL;
5270
5271 p = image_id;
Alex Elder41579762013-04-21 12:14:45 -05005272 end = image_id + image_id_size;
Alex Elder57385b52013-04-21 12:14:45 -05005273 ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len);
Alex Elder9e15b772012-10-30 19:40:33 -05005274
5275 size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX;
5276 reply_buf = kmalloc(size, GFP_KERNEL);
5277 if (!reply_buf)
5278 goto out;
5279
Alex Elder36be9a72013-01-19 00:30:28 -06005280 ret = rbd_obj_method_sync(rbd_dev, RBD_DIRECTORY,
Alex Elder9e15b772012-10-30 19:40:33 -05005281 "rbd", "dir_get_name",
5282 image_id, image_id_size,
Alex Eldere2a58ee2013-04-30 00:44:33 -05005283 reply_buf, size);
Alex Elder9e15b772012-10-30 19:40:33 -05005284 if (ret < 0)
5285 goto out;
5286 p = reply_buf;
Alex Elderf40eb342013-04-25 15:09:42 -05005287 end = reply_buf + ret;
5288
Alex Elder9e15b772012-10-30 19:40:33 -05005289 image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL);
5290 if (IS_ERR(image_name))
5291 image_name = NULL;
5292 else
5293 dout("%s: name is %s len is %zd\n", __func__, image_name, len);
5294out:
5295 kfree(reply_buf);
5296 kfree(image_id);
5297
5298 return image_name;
5299}
5300
Alex Elder2ad3d712013-04-30 00:44:33 -05005301static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
5302{
5303 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
5304 const char *snap_name;
5305 u32 which = 0;
5306
5307 /* Skip over names until we find the one we are looking for */
5308
5309 snap_name = rbd_dev->header.snap_names;
5310 while (which < snapc->num_snaps) {
5311 if (!strcmp(name, snap_name))
5312 return snapc->snaps[which];
5313 snap_name += strlen(snap_name) + 1;
5314 which++;
5315 }
5316 return CEPH_NOSNAP;
5317}
5318
5319static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
5320{
5321 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
5322 u32 which;
5323 bool found = false;
5324 u64 snap_id;
5325
5326 for (which = 0; !found && which < snapc->num_snaps; which++) {
5327 const char *snap_name;
5328
5329 snap_id = snapc->snaps[which];
5330 snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id);
Josh Durginefadc982013-08-29 19:16:42 -07005331 if (IS_ERR(snap_name)) {
5332 /* ignore no-longer existing snapshots */
5333 if (PTR_ERR(snap_name) == -ENOENT)
5334 continue;
5335 else
5336 break;
5337 }
Alex Elder2ad3d712013-04-30 00:44:33 -05005338 found = !strcmp(name, snap_name);
5339 kfree(snap_name);
5340 }
5341 return found ? snap_id : CEPH_NOSNAP;
5342}
5343
5344/*
5345 * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if
5346 * no snapshot by that name is found, or if an error occurs.
5347 */
5348static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
5349{
5350 if (rbd_dev->image_format == 1)
5351 return rbd_v1_snap_id_by_name(rbd_dev, name);
5352
5353 return rbd_v2_snap_id_by_name(rbd_dev, name);
5354}
5355
Alex Elder9e15b772012-10-30 19:40:33 -05005356/*
Ilya Dryomov04077592014-07-23 17:11:20 +04005357 * An image being mapped will have everything but the snap id.
Alex Elder9e15b772012-10-30 19:40:33 -05005358 */
Ilya Dryomov04077592014-07-23 17:11:20 +04005359static int rbd_spec_fill_snap_id(struct rbd_device *rbd_dev)
5360{
5361 struct rbd_spec *spec = rbd_dev->spec;
5362
5363 rbd_assert(spec->pool_id != CEPH_NOPOOL && spec->pool_name);
5364 rbd_assert(spec->image_id && spec->image_name);
5365 rbd_assert(spec->snap_name);
5366
5367 if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) {
5368 u64 snap_id;
5369
5370 snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name);
5371 if (snap_id == CEPH_NOSNAP)
5372 return -ENOENT;
5373
5374 spec->snap_id = snap_id;
5375 } else {
5376 spec->snap_id = CEPH_NOSNAP;
5377 }
5378
5379 return 0;
5380}
5381
5382/*
5383 * A parent image will have all ids but none of the names.
5384 *
5385 * All names in an rbd spec are dynamically allocated. It's OK if we
5386 * can't figure out the name for an image id.
5387 */
5388static int rbd_spec_fill_names(struct rbd_device *rbd_dev)
Alex Elder9e15b772012-10-30 19:40:33 -05005389{
Alex Elder2e9f7f12013-04-26 09:43:48 -05005390 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
5391 struct rbd_spec *spec = rbd_dev->spec;
5392 const char *pool_name;
5393 const char *image_name;
5394 const char *snap_name;
Alex Elder9e15b772012-10-30 19:40:33 -05005395 int ret;
5396
Ilya Dryomov04077592014-07-23 17:11:20 +04005397 rbd_assert(spec->pool_id != CEPH_NOPOOL);
5398 rbd_assert(spec->image_id);
5399 rbd_assert(spec->snap_id != CEPH_NOSNAP);
Alex Elder9e15b772012-10-30 19:40:33 -05005400
Alex Elder2e9f7f12013-04-26 09:43:48 -05005401 /* Get the pool name; we have to make our own copy of this */
Alex Elder9e15b772012-10-30 19:40:33 -05005402
Alex Elder2e9f7f12013-04-26 09:43:48 -05005403 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id);
5404 if (!pool_name) {
5405 rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id);
Alex Elder935dc892012-11-01 10:17:15 -05005406 return -EIO;
5407 }
Alex Elder2e9f7f12013-04-26 09:43:48 -05005408 pool_name = kstrdup(pool_name, GFP_KERNEL);
5409 if (!pool_name)
Alex Elder9e15b772012-10-30 19:40:33 -05005410 return -ENOMEM;
5411
5412 /* Fetch the image name; tolerate failure here */
5413
Alex Elder2e9f7f12013-04-26 09:43:48 -05005414 image_name = rbd_dev_image_name(rbd_dev);
5415 if (!image_name)
Alex Elder06ecc6c2012-11-01 10:17:15 -05005416 rbd_warn(rbd_dev, "unable to get image name");
Alex Elder9e15b772012-10-30 19:40:33 -05005417
Ilya Dryomov04077592014-07-23 17:11:20 +04005418 /* Fetch the snapshot name */
Alex Elder9e15b772012-10-30 19:40:33 -05005419
Alex Elder2e9f7f12013-04-26 09:43:48 -05005420 snap_name = rbd_snap_name(rbd_dev, spec->snap_id);
Josh Durginda6a6b62013-09-04 17:57:31 -07005421 if (IS_ERR(snap_name)) {
5422 ret = PTR_ERR(snap_name);
Alex Elder9e15b772012-10-30 19:40:33 -05005423 goto out_err;
Alex Elder2e9f7f12013-04-26 09:43:48 -05005424 }
5425
5426 spec->pool_name = pool_name;
5427 spec->image_name = image_name;
5428 spec->snap_name = snap_name;
Alex Elder9e15b772012-10-30 19:40:33 -05005429
5430 return 0;
Ilya Dryomov04077592014-07-23 17:11:20 +04005431
Alex Elder9e15b772012-10-30 19:40:33 -05005432out_err:
Alex Elder2e9f7f12013-04-26 09:43:48 -05005433 kfree(image_name);
5434 kfree(pool_name);
Alex Elder9e15b772012-10-30 19:40:33 -05005435 return ret;
5436}
5437
Alex Eldercc4a38bd2013-04-30 00:44:33 -05005438static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev)
Alex Elder35d489f2012-07-03 16:01:19 -05005439{
5440 size_t size;
5441 int ret;
5442 void *reply_buf;
5443 void *p;
5444 void *end;
5445 u64 seq;
5446 u32 snap_count;
5447 struct ceph_snap_context *snapc;
5448 u32 i;
5449
5450 /*
5451 * We'll need room for the seq value (maximum snapshot id),
5452 * snapshot count, and array of that many snapshot ids.
5453 * For now we have a fixed upper limit on the number we're
5454 * prepared to receive.
5455 */
5456 size = sizeof (__le64) + sizeof (__le32) +
5457 RBD_MAX_SNAP_COUNT * sizeof (__le64);
5458 reply_buf = kzalloc(size, GFP_KERNEL);
5459 if (!reply_buf)
5460 return -ENOMEM;
5461
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005462 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name,
Alex Elder41579762013-04-21 12:14:45 -05005463 "rbd", "get_snapcontext", NULL, 0,
Alex Eldere2a58ee2013-04-30 00:44:33 -05005464 reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06005465 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder35d489f2012-07-03 16:01:19 -05005466 if (ret < 0)
5467 goto out;
5468
Alex Elder35d489f2012-07-03 16:01:19 -05005469 p = reply_buf;
Alex Elder57385b52013-04-21 12:14:45 -05005470 end = reply_buf + ret;
5471 ret = -ERANGE;
Alex Elder35d489f2012-07-03 16:01:19 -05005472 ceph_decode_64_safe(&p, end, seq, out);
5473 ceph_decode_32_safe(&p, end, snap_count, out);
5474
5475 /*
5476 * Make sure the reported number of snapshot ids wouldn't go
5477 * beyond the end of our buffer. But before checking that,
5478 * make sure the computed size of the snapshot context we
5479 * allocate is representable in a size_t.
5480 */
5481 if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
5482 / sizeof (u64)) {
5483 ret = -EINVAL;
5484 goto out;
5485 }
5486 if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
5487 goto out;
Alex Elder468521c2013-04-26 09:43:47 -05005488 ret = 0;
Alex Elder35d489f2012-07-03 16:01:19 -05005489
Alex Elder812164f82013-04-30 00:44:32 -05005490 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
Alex Elder35d489f2012-07-03 16:01:19 -05005491 if (!snapc) {
5492 ret = -ENOMEM;
5493 goto out;
5494 }
Alex Elder35d489f2012-07-03 16:01:19 -05005495 snapc->seq = seq;
Alex Elder35d489f2012-07-03 16:01:19 -05005496 for (i = 0; i < snap_count; i++)
5497 snapc->snaps[i] = ceph_decode_64(&p);
5498
Alex Elder49ece552013-05-06 08:37:00 -05005499 ceph_put_snap_context(rbd_dev->header.snapc);
Alex Elder35d489f2012-07-03 16:01:19 -05005500 rbd_dev->header.snapc = snapc;
5501
5502 dout(" snap context seq = %llu, snap_count = %u\n",
Alex Elder57385b52013-04-21 12:14:45 -05005503 (unsigned long long)seq, (unsigned int)snap_count);
Alex Elder35d489f2012-07-03 16:01:19 -05005504out:
5505 kfree(reply_buf);
5506
Alex Elder57385b52013-04-21 12:14:45 -05005507 return ret;
Alex Elder35d489f2012-07-03 16:01:19 -05005508}
5509
Alex Elder54cac612013-04-30 00:44:33 -05005510static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
5511 u64 snap_id)
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005512{
5513 size_t size;
5514 void *reply_buf;
Alex Elder54cac612013-04-30 00:44:33 -05005515 __le64 snapid;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005516 int ret;
5517 void *p;
5518 void *end;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005519 char *snap_name;
5520
5521 size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN;
5522 reply_buf = kmalloc(size, GFP_KERNEL);
5523 if (!reply_buf)
5524 return ERR_PTR(-ENOMEM);
5525
Alex Elder54cac612013-04-30 00:44:33 -05005526 snapid = cpu_to_le64(snap_id);
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005527 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name,
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005528 "rbd", "get_snapshot_name",
Alex Elder54cac612013-04-30 00:44:33 -05005529 &snapid, sizeof (snapid),
Alex Eldere2a58ee2013-04-30 00:44:33 -05005530 reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06005531 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderf40eb342013-04-25 15:09:42 -05005532 if (ret < 0) {
5533 snap_name = ERR_PTR(ret);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005534 goto out;
Alex Elderf40eb342013-04-25 15:09:42 -05005535 }
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005536
5537 p = reply_buf;
Alex Elderf40eb342013-04-25 15:09:42 -05005538 end = reply_buf + ret;
Alex Eldere5c35532012-10-25 23:34:41 -05005539 snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elderf40eb342013-04-25 15:09:42 -05005540 if (IS_ERR(snap_name))
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005541 goto out;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005542
Alex Elderf40eb342013-04-25 15:09:42 -05005543 dout(" snap_id 0x%016llx snap_name = %s\n",
Alex Elder54cac612013-04-30 00:44:33 -05005544 (unsigned long long)snap_id, snap_name);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005545out:
5546 kfree(reply_buf);
5547
Alex Elderf40eb342013-04-25 15:09:42 -05005548 return snap_name;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005549}
5550
Alex Elder2df3fac2013-05-06 09:51:30 -05005551static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev)
Alex Elder117973f2012-08-31 17:29:55 -05005552{
Alex Elder2df3fac2013-05-06 09:51:30 -05005553 bool first_time = rbd_dev->header.object_prefix == NULL;
Alex Elder117973f2012-08-31 17:29:55 -05005554 int ret;
Alex Elder117973f2012-08-31 17:29:55 -05005555
Josh Durgin1617e402013-06-12 14:43:10 -07005556 ret = rbd_dev_v2_image_size(rbd_dev);
5557 if (ret)
Alex Eldercfbf6372013-05-31 17:40:45 -05005558 return ret;
Josh Durgin1617e402013-06-12 14:43:10 -07005559
Alex Elder2df3fac2013-05-06 09:51:30 -05005560 if (first_time) {
5561 ret = rbd_dev_v2_header_onetime(rbd_dev);
5562 if (ret)
Alex Eldercfbf6372013-05-31 17:40:45 -05005563 return ret;
Alex Elder2df3fac2013-05-06 09:51:30 -05005564 }
5565
Alex Eldercc4a38bd2013-04-30 00:44:33 -05005566 ret = rbd_dev_v2_snap_context(rbd_dev);
Ilya Dryomovd194cd12015-08-31 18:22:10 +03005567 if (ret && first_time) {
5568 kfree(rbd_dev->header.object_prefix);
5569 rbd_dev->header.object_prefix = NULL;
5570 }
Alex Elder117973f2012-08-31 17:29:55 -05005571
5572 return ret;
5573}
5574
Ilya Dryomova720ae02014-07-23 17:11:19 +04005575static int rbd_dev_header_info(struct rbd_device *rbd_dev)
5576{
5577 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
5578
5579 if (rbd_dev->image_format == 1)
5580 return rbd_dev_v1_header_info(rbd_dev);
5581
5582 return rbd_dev_v2_header_info(rbd_dev);
5583}
5584
Alex Elder1ddbe942012-01-29 13:57:44 -06005585/*
Alex Eldere28fff262012-02-02 08:13:30 -06005586 * Skips over white space at *buf, and updates *buf to point to the
5587 * first found non-space character (if any). Returns the length of
Alex Elder593a9e72012-02-07 12:03:37 -06005588 * the token (string of non-white space characters) found. Note
5589 * that *buf must be terminated with '\0'.
Alex Eldere28fff262012-02-02 08:13:30 -06005590 */
5591static inline size_t next_token(const char **buf)
5592{
5593 /*
5594 * These are the characters that produce nonzero for
5595 * isspace() in the "C" and "POSIX" locales.
5596 */
5597 const char *spaces = " \f\n\r\t\v";
5598
5599 *buf += strspn(*buf, spaces); /* Find start of token */
5600
5601 return strcspn(*buf, spaces); /* Return token length */
5602}
5603
5604/*
Alex Elderea3352f2012-07-09 21:04:23 -05005605 * Finds the next token in *buf, dynamically allocates a buffer big
5606 * enough to hold a copy of it, and copies the token into the new
5607 * buffer. The copy is guaranteed to be terminated with '\0'. Note
5608 * that a duplicate buffer is created even for a zero-length token.
5609 *
5610 * Returns a pointer to the newly-allocated duplicate, or a null
5611 * pointer if memory for the duplicate was not available. If
5612 * the lenp argument is a non-null pointer, the length of the token
5613 * (not including the '\0') is returned in *lenp.
5614 *
5615 * If successful, the *buf pointer will be updated to point beyond
5616 * the end of the found token.
5617 *
5618 * Note: uses GFP_KERNEL for allocation.
5619 */
5620static inline char *dup_token(const char **buf, size_t *lenp)
5621{
5622 char *dup;
5623 size_t len;
5624
5625 len = next_token(buf);
Alex Elder4caf35f2012-11-01 08:39:27 -05005626 dup = kmemdup(*buf, len + 1, GFP_KERNEL);
Alex Elderea3352f2012-07-09 21:04:23 -05005627 if (!dup)
5628 return NULL;
Alex Elderea3352f2012-07-09 21:04:23 -05005629 *(dup + len) = '\0';
5630 *buf += len;
5631
5632 if (lenp)
5633 *lenp = len;
5634
5635 return dup;
5636}
5637
5638/*
Alex Elder859c31d2012-10-25 23:34:42 -05005639 * Parse the options provided for an "rbd add" (i.e., rbd image
5640 * mapping) request. These arrive via a write to /sys/bus/rbd/add,
5641 * and the data written is passed here via a NUL-terminated buffer.
5642 * Returns 0 if successful or an error code otherwise.
Alex Elderd22f76e2012-07-12 10:46:35 -05005643 *
Alex Elder859c31d2012-10-25 23:34:42 -05005644 * The information extracted from these options is recorded in
5645 * the other parameters which return dynamically-allocated
5646 * structures:
5647 * ceph_opts
5648 * The address of a pointer that will refer to a ceph options
5649 * structure. Caller must release the returned pointer using
5650 * ceph_destroy_options() when it is no longer needed.
5651 * rbd_opts
5652 * Address of an rbd options pointer. Fully initialized by
5653 * this function; caller must release with kfree().
5654 * spec
5655 * Address of an rbd image specification pointer. Fully
5656 * initialized by this function based on parsed options.
5657 * Caller must release with rbd_spec_put().
5658 *
5659 * The options passed take this form:
5660 * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
5661 * where:
5662 * <mon_addrs>
5663 * A comma-separated list of one or more monitor addresses.
5664 * A monitor address is an ip address, optionally followed
5665 * by a port number (separated by a colon).
5666 * I.e.: ip1[:port1][,ip2[:port2]...]
5667 * <options>
5668 * A comma-separated list of ceph and/or rbd options.
5669 * <pool_name>
5670 * The name of the rados pool containing the rbd image.
5671 * <image_name>
5672 * The name of the image in that pool to map.
5673 * <snap_id>
5674 * An optional snapshot id. If provided, the mapping will
5675 * present data from the image at the time that snapshot was
5676 * created. The image head is used if no snapshot id is
5677 * provided. Snapshot mappings are always read-only.
Alex Eldera725f65e2012-02-02 08:13:30 -06005678 */
Alex Elder859c31d2012-10-25 23:34:42 -05005679static int rbd_add_parse_args(const char *buf,
Alex Elderdc79b112012-10-25 23:34:41 -05005680 struct ceph_options **ceph_opts,
Alex Elder859c31d2012-10-25 23:34:42 -05005681 struct rbd_options **opts,
5682 struct rbd_spec **rbd_spec)
Alex Eldera725f65e2012-02-02 08:13:30 -06005683{
Alex Elderd22f76e2012-07-12 10:46:35 -05005684 size_t len;
Alex Elder859c31d2012-10-25 23:34:42 -05005685 char *options;
Alex Elder0ddebc02012-10-25 23:34:41 -05005686 const char *mon_addrs;
Alex Elderecb4dc22013-04-26 09:43:47 -05005687 char *snap_name;
Alex Elder0ddebc02012-10-25 23:34:41 -05005688 size_t mon_addrs_size;
Alex Elder859c31d2012-10-25 23:34:42 -05005689 struct rbd_spec *spec = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05005690 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05005691 struct ceph_options *copts;
Alex Elderdc79b112012-10-25 23:34:41 -05005692 int ret;
Alex Eldere28fff262012-02-02 08:13:30 -06005693
5694 /* The first four tokens are required */
5695
Alex Elder7ef32142012-02-02 08:13:30 -06005696 len = next_token(&buf);
Alex Elder4fb5d6712012-11-01 10:17:15 -05005697 if (!len) {
5698 rbd_warn(NULL, "no monitor address(es) provided");
5699 return -EINVAL;
5700 }
Alex Elder0ddebc02012-10-25 23:34:41 -05005701 mon_addrs = buf;
Alex Elderf28e5652012-10-25 23:34:41 -05005702 mon_addrs_size = len + 1;
Alex Elder7ef32142012-02-02 08:13:30 -06005703 buf += len;
Alex Eldera725f65e2012-02-02 08:13:30 -06005704
Alex Elderdc79b112012-10-25 23:34:41 -05005705 ret = -EINVAL;
Alex Elderf28e5652012-10-25 23:34:41 -05005706 options = dup_token(&buf, NULL);
5707 if (!options)
Alex Elderdc79b112012-10-25 23:34:41 -05005708 return -ENOMEM;
Alex Elder4fb5d6712012-11-01 10:17:15 -05005709 if (!*options) {
5710 rbd_warn(NULL, "no options provided");
5711 goto out_err;
5712 }
Alex Eldera725f65e2012-02-02 08:13:30 -06005713
Alex Elder859c31d2012-10-25 23:34:42 -05005714 spec = rbd_spec_alloc();
5715 if (!spec)
Alex Elderf28e5652012-10-25 23:34:41 -05005716 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05005717
5718 spec->pool_name = dup_token(&buf, NULL);
5719 if (!spec->pool_name)
5720 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05005721 if (!*spec->pool_name) {
5722 rbd_warn(NULL, "no pool name provided");
5723 goto out_err;
5724 }
Alex Eldere28fff262012-02-02 08:13:30 -06005725
Alex Elder69e7a022012-11-01 08:39:26 -05005726 spec->image_name = dup_token(&buf, NULL);
Alex Elder859c31d2012-10-25 23:34:42 -05005727 if (!spec->image_name)
Alex Elderf28e5652012-10-25 23:34:41 -05005728 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05005729 if (!*spec->image_name) {
5730 rbd_warn(NULL, "no image name provided");
5731 goto out_err;
5732 }
Alex Eldere28fff262012-02-02 08:13:30 -06005733
Alex Elderf28e5652012-10-25 23:34:41 -05005734 /*
5735 * Snapshot name is optional; default is to use "-"
5736 * (indicating the head/no snapshot).
5737 */
Alex Elder3feeb8942012-08-31 17:29:52 -05005738 len = next_token(&buf);
Alex Elder820a5f32012-07-09 21:04:24 -05005739 if (!len) {
Alex Elder3feeb8942012-08-31 17:29:52 -05005740 buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
5741 len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
Alex Elderf28e5652012-10-25 23:34:41 -05005742 } else if (len > RBD_MAX_SNAP_NAME_LEN) {
Alex Elderdc79b112012-10-25 23:34:41 -05005743 ret = -ENAMETOOLONG;
Alex Elderf28e5652012-10-25 23:34:41 -05005744 goto out_err;
Alex Elder849b4262012-07-09 21:04:24 -05005745 }
Alex Elderecb4dc22013-04-26 09:43:47 -05005746 snap_name = kmemdup(buf, len + 1, GFP_KERNEL);
5747 if (!snap_name)
Alex Elderf28e5652012-10-25 23:34:41 -05005748 goto out_mem;
Alex Elderecb4dc22013-04-26 09:43:47 -05005749 *(snap_name + len) = '\0';
5750 spec->snap_name = snap_name;
Alex Eldere5c35532012-10-25 23:34:41 -05005751
Alex Elder0ddebc02012-10-25 23:34:41 -05005752 /* Initialize all rbd options to the defaults */
Alex Eldere28fff262012-02-02 08:13:30 -06005753
Alex Elder4e9afeb2012-10-25 23:34:41 -05005754 rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL);
5755 if (!rbd_opts)
5756 goto out_mem;
5757
5758 rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
Ilya Dryomovb5584182015-06-23 16:21:19 +03005759 rbd_opts->queue_depth = RBD_QUEUE_DEPTH_DEFAULT;
Alex Elderd22f76e2012-07-12 10:46:35 -05005760
Alex Elder859c31d2012-10-25 23:34:42 -05005761 copts = ceph_parse_options(options, mon_addrs,
Alex Elder0ddebc02012-10-25 23:34:41 -05005762 mon_addrs + mon_addrs_size - 1,
Alex Elder4e9afeb2012-10-25 23:34:41 -05005763 parse_rbd_opts_token, rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05005764 if (IS_ERR(copts)) {
5765 ret = PTR_ERR(copts);
Alex Elderdc79b112012-10-25 23:34:41 -05005766 goto out_err;
5767 }
Alex Elder859c31d2012-10-25 23:34:42 -05005768 kfree(options);
5769
5770 *ceph_opts = copts;
Alex Elder4e9afeb2012-10-25 23:34:41 -05005771 *opts = rbd_opts;
Alex Elder859c31d2012-10-25 23:34:42 -05005772 *rbd_spec = spec;
Alex Elder0ddebc02012-10-25 23:34:41 -05005773
Alex Elderdc79b112012-10-25 23:34:41 -05005774 return 0;
Alex Elderf28e5652012-10-25 23:34:41 -05005775out_mem:
Alex Elderdc79b112012-10-25 23:34:41 -05005776 ret = -ENOMEM;
Alex Elderd22f76e2012-07-12 10:46:35 -05005777out_err:
Alex Elder859c31d2012-10-25 23:34:42 -05005778 kfree(rbd_opts);
5779 rbd_spec_put(spec);
Alex Elderf28e5652012-10-25 23:34:41 -05005780 kfree(options);
Alex Elderd22f76e2012-07-12 10:46:35 -05005781
Alex Elderdc79b112012-10-25 23:34:41 -05005782 return ret;
Alex Eldera725f65e2012-02-02 08:13:30 -06005783}
5784
Alex Elder589d30e2012-07-10 20:30:11 -05005785/*
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005786 * Return pool id (>= 0) or a negative error code.
5787 */
5788static int rbd_add_get_pool_id(struct rbd_client *rbdc, const char *pool_name)
5789{
Ilya Dryomova319bf52015-05-15 12:02:17 +03005790 struct ceph_options *opts = rbdc->client->options;
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005791 u64 newest_epoch;
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005792 int tries = 0;
5793 int ret;
5794
5795again:
5796 ret = ceph_pg_poolid_by_name(rbdc->client->osdc.osdmap, pool_name);
5797 if (ret == -ENOENT && tries++ < 1) {
Ilya Dryomovd0b19702016-04-28 16:07:27 +02005798 ret = ceph_monc_get_version(&rbdc->client->monc, "osdmap",
5799 &newest_epoch);
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005800 if (ret < 0)
5801 return ret;
5802
5803 if (rbdc->client->osdc.osdmap->epoch < newest_epoch) {
Ilya Dryomov7cca78c2016-04-28 16:07:28 +02005804 ceph_osdc_maybe_request_map(&rbdc->client->osdc);
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005805 (void) ceph_monc_wait_osdmap(&rbdc->client->monc,
Ilya Dryomova319bf52015-05-15 12:02:17 +03005806 newest_epoch,
5807 opts->mount_timeout);
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005808 goto again;
5809 } else {
5810 /* the osdmap we have is new enough */
5811 return -ENOENT;
5812 }
5813 }
5814
5815 return ret;
5816}
5817
5818/*
Alex Elder589d30e2012-07-10 20:30:11 -05005819 * An rbd format 2 image has a unique identifier, distinct from the
5820 * name given to it by the user. Internally, that identifier is
5821 * what's used to specify the names of objects related to the image.
5822 *
5823 * A special "rbd id" object is used to map an rbd image name to its
5824 * id. If that object doesn't exist, then there is no v2 rbd image
5825 * with the supplied name.
5826 *
5827 * This function will record the given rbd_dev's image_id field if
5828 * it can be determined, and in that case will return 0. If any
5829 * errors occur a negative errno will be returned and the rbd_dev's
5830 * image_id field will be unchanged (and should be NULL).
5831 */
5832static int rbd_dev_image_id(struct rbd_device *rbd_dev)
5833{
5834 int ret;
5835 size_t size;
5836 char *object_name;
5837 void *response;
Alex Elderc0fba362013-04-25 23:15:08 -05005838 char *image_id;
Alex Elder2f82ee52012-10-30 19:40:33 -05005839
Alex Elder589d30e2012-07-10 20:30:11 -05005840 /*
Alex Elder2c0d0a12012-10-30 19:40:33 -05005841 * When probing a parent image, the image id is already
5842 * known (and the image name likely is not). There's no
Alex Elderc0fba362013-04-25 23:15:08 -05005843 * need to fetch the image id again in this case. We
5844 * do still need to set the image format though.
Alex Elder2c0d0a12012-10-30 19:40:33 -05005845 */
Alex Elderc0fba362013-04-25 23:15:08 -05005846 if (rbd_dev->spec->image_id) {
5847 rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1;
5848
Alex Elder2c0d0a12012-10-30 19:40:33 -05005849 return 0;
Alex Elderc0fba362013-04-25 23:15:08 -05005850 }
Alex Elder2c0d0a12012-10-30 19:40:33 -05005851
5852 /*
Alex Elder589d30e2012-07-10 20:30:11 -05005853 * First, see if the format 2 image id file exists, and if
5854 * so, get the image's persistent id from it.
5855 */
Alex Elder69e7a022012-11-01 08:39:26 -05005856 size = sizeof (RBD_ID_PREFIX) + strlen(rbd_dev->spec->image_name);
Alex Elder589d30e2012-07-10 20:30:11 -05005857 object_name = kmalloc(size, GFP_NOIO);
5858 if (!object_name)
5859 return -ENOMEM;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05005860 sprintf(object_name, "%s%s", RBD_ID_PREFIX, rbd_dev->spec->image_name);
Alex Elder589d30e2012-07-10 20:30:11 -05005861 dout("rbd id object name is %s\n", object_name);
5862
5863 /* Response will be an encoded string, which includes a length */
5864
5865 size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
5866 response = kzalloc(size, GFP_NOIO);
5867 if (!response) {
5868 ret = -ENOMEM;
5869 goto out;
5870 }
5871
Alex Elderc0fba362013-04-25 23:15:08 -05005872 /* If it doesn't exist we'll assume it's a format 1 image */
5873
Alex Elder36be9a72013-01-19 00:30:28 -06005874 ret = rbd_obj_method_sync(rbd_dev, object_name,
Alex Elder41579762013-04-21 12:14:45 -05005875 "rbd", "get_id", NULL, 0,
Alex Eldere2a58ee2013-04-30 00:44:33 -05005876 response, RBD_IMAGE_ID_LEN_MAX);
Alex Elder36be9a72013-01-19 00:30:28 -06005877 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderc0fba362013-04-25 23:15:08 -05005878 if (ret == -ENOENT) {
5879 image_id = kstrdup("", GFP_KERNEL);
5880 ret = image_id ? 0 : -ENOMEM;
5881 if (!ret)
5882 rbd_dev->image_format = 1;
Ilya Dryomov7dd440c2014-09-11 18:49:18 +04005883 } else if (ret >= 0) {
Alex Elderc0fba362013-04-25 23:15:08 -05005884 void *p = response;
Alex Elder589d30e2012-07-10 20:30:11 -05005885
Alex Elderc0fba362013-04-25 23:15:08 -05005886 image_id = ceph_extract_encoded_string(&p, p + ret,
Alex Elder979ed482012-11-01 08:39:26 -05005887 NULL, GFP_NOIO);
Duan Jiong461f7582014-04-11 16:38:12 +08005888 ret = PTR_ERR_OR_ZERO(image_id);
Alex Elderc0fba362013-04-25 23:15:08 -05005889 if (!ret)
5890 rbd_dev->image_format = 2;
Alex Elderc0fba362013-04-25 23:15:08 -05005891 }
5892
5893 if (!ret) {
5894 rbd_dev->spec->image_id = image_id;
5895 dout("image_id is %s\n", image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05005896 }
5897out:
5898 kfree(response);
5899 kfree(object_name);
5900
5901 return ret;
5902}
5903
Alex Elder3abef3b2013-05-13 20:35:37 -05005904/*
5905 * Undo whatever state changes are made by v1 or v2 header info
5906 * call.
5907 */
Alex Elder6fd48b32013-04-28 23:32:34 -05005908static void rbd_dev_unprobe(struct rbd_device *rbd_dev)
5909{
5910 struct rbd_image_header *header;
5911
Ilya Dryomove69b8d42015-01-19 12:06:14 +03005912 rbd_dev_parent_put(rbd_dev);
Alex Elder6fd48b32013-04-28 23:32:34 -05005913
5914 /* Free dynamic fields from the header, then zero it out */
5915
5916 header = &rbd_dev->header;
Alex Elder812164f82013-04-30 00:44:32 -05005917 ceph_put_snap_context(header->snapc);
Alex Elder6fd48b32013-04-28 23:32:34 -05005918 kfree(header->snap_sizes);
5919 kfree(header->snap_names);
5920 kfree(header->object_prefix);
5921 memset(header, 0, sizeof (*header));
5922}
5923
Alex Elder2df3fac2013-05-06 09:51:30 -05005924static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev)
Alex Eldera30b71b2012-07-10 20:30:11 -05005925{
5926 int ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05005927
Alex Elder1e130192012-07-03 16:01:19 -05005928 ret = rbd_dev_v2_object_prefix(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05005929 if (ret)
Alex Elder1e130192012-07-03 16:01:19 -05005930 goto out_err;
Alex Elderb1b54022012-07-03 16:01:19 -05005931
Alex Elder2df3fac2013-05-06 09:51:30 -05005932 /*
5933 * Get the and check features for the image. Currently the
5934 * features are assumed to never change.
5935 */
Alex Elderb1b54022012-07-03 16:01:19 -05005936 ret = rbd_dev_v2_features(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05005937 if (ret)
Alex Elderb1b54022012-07-03 16:01:19 -05005938 goto out_err;
Alex Elder35d489f2012-07-03 16:01:19 -05005939
Alex Eldercc070d52013-04-21 12:14:45 -05005940 /* If the image supports fancy striping, get its parameters */
5941
5942 if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) {
5943 ret = rbd_dev_v2_striping_info(rbd_dev);
5944 if (ret < 0)
5945 goto out_err;
5946 }
Alex Elder2df3fac2013-05-06 09:51:30 -05005947 /* No support for crypto and compression type format 2 images */
Alex Eldera30b71b2012-07-10 20:30:11 -05005948
Alex Elder35152972012-08-31 17:29:55 -05005949 return 0;
Alex Elder9d475de2012-07-03 16:01:19 -05005950out_err:
Alex Elder642a2532013-05-06 17:40:33 -05005951 rbd_dev->header.features = 0;
Alex Elder1e130192012-07-03 16:01:19 -05005952 kfree(rbd_dev->header.object_prefix);
5953 rbd_dev->header.object_prefix = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05005954
5955 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05005956}
5957
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005958/*
5959 * @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() ->
5960 * rbd_dev_image_probe() recursion depth, which means it's also the
5961 * length of the already discovered part of the parent chain.
5962 */
5963static int rbd_dev_probe_parent(struct rbd_device *rbd_dev, int depth)
Alex Elder83a06262012-10-30 15:47:17 -05005964{
Alex Elder2f82ee52012-10-30 19:40:33 -05005965 struct rbd_device *parent = NULL;
Alex Elder124afba2013-04-26 15:44:36 -05005966 int ret;
5967
5968 if (!rbd_dev->parent_spec)
5969 return 0;
Alex Elder124afba2013-04-26 15:44:36 -05005970
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005971 if (++depth > RBD_MAX_PARENT_CHAIN_LEN) {
5972 pr_info("parent chain is too long (%d)\n", depth);
5973 ret = -EINVAL;
5974 goto out_err;
5975 }
5976
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02005977 parent = __rbd_dev_create(rbd_dev->rbd_client, rbd_dev->parent_spec);
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005978 if (!parent) {
5979 ret = -ENOMEM;
Alex Elder124afba2013-04-26 15:44:36 -05005980 goto out_err;
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005981 }
5982
5983 /*
5984 * Images related by parent/child relationships always share
5985 * rbd_client and spec/parent_spec, so bump their refcounts.
5986 */
5987 __rbd_get_client(rbd_dev->rbd_client);
5988 rbd_spec_get(rbd_dev->parent_spec);
Alex Elder124afba2013-04-26 15:44:36 -05005989
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005990 ret = rbd_dev_image_probe(parent, depth);
Alex Elder124afba2013-04-26 15:44:36 -05005991 if (ret < 0)
5992 goto out_err;
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005993
Alex Elder124afba2013-04-26 15:44:36 -05005994 rbd_dev->parent = parent;
Alex Eldera2acd002013-05-08 22:50:04 -05005995 atomic_set(&rbd_dev->parent_ref, 1);
Alex Elder124afba2013-04-26 15:44:36 -05005996 return 0;
Alex Elder124afba2013-04-26 15:44:36 -05005997
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005998out_err:
5999 rbd_dev_unparent(rbd_dev);
Markus Elfring1761b222015-11-23 20:16:45 +01006000 rbd_dev_destroy(parent);
Alex Elder124afba2013-04-26 15:44:36 -05006001 return ret;
6002}
6003
Ilya Dryomov811c6682016-04-15 16:22:16 +02006004/*
6005 * rbd_dev->header_rwsem must be locked for write and will be unlocked
6006 * upon return.
6007 */
Alex Elder200a6a82013-04-28 23:32:34 -05006008static int rbd_dev_device_setup(struct rbd_device *rbd_dev)
Alex Elder124afba2013-04-26 15:44:36 -05006009{
Alex Elder83a06262012-10-30 15:47:17 -05006010 int ret;
Alex Elder83a06262012-10-30 15:47:17 -05006011
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006012 /* Record our major and minor device numbers. */
Alex Elder83a06262012-10-30 15:47:17 -05006013
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006014 if (!single_major) {
6015 ret = register_blkdev(0, rbd_dev->name);
6016 if (ret < 0)
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02006017 goto err_out_unlock;
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006018
6019 rbd_dev->major = ret;
6020 rbd_dev->minor = 0;
6021 } else {
6022 rbd_dev->major = rbd_major;
6023 rbd_dev->minor = rbd_dev_id_to_minor(rbd_dev->dev_id);
6024 }
Alex Elder83a06262012-10-30 15:47:17 -05006025
6026 /* Set up the blkdev mapping. */
6027
6028 ret = rbd_init_disk(rbd_dev);
6029 if (ret)
6030 goto err_out_blkdev;
6031
Alex Elderf35a4de2013-05-06 09:51:29 -05006032 ret = rbd_dev_mapping_set(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05006033 if (ret)
6034 goto err_out_disk;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04006035
Alex Elderf35a4de2013-05-06 09:51:29 -05006036 set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
Josh Durgin22001f62013-09-30 20:10:04 -07006037 set_disk_ro(rbd_dev->disk, rbd_dev->mapping.read_only);
Alex Elderf35a4de2013-05-06 09:51:29 -05006038
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006039 dev_set_name(&rbd_dev->dev, "%d", rbd_dev->dev_id);
6040 ret = device_add(&rbd_dev->dev);
Alex Elderf35a4de2013-05-06 09:51:29 -05006041 if (ret)
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006042 goto err_out_mapping;
Alex Elder83a06262012-10-30 15:47:17 -05006043
Alex Elder83a06262012-10-30 15:47:17 -05006044 /* Everything's ready. Announce the disk to the world. */
6045
Alex Elder129b79d2013-04-26 15:44:36 -05006046 set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
Ilya Dryomov811c6682016-04-15 16:22:16 +02006047 up_write(&rbd_dev->header_rwsem);
Alex Elder83a06262012-10-30 15:47:17 -05006048
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02006049 spin_lock(&rbd_dev_list_lock);
6050 list_add_tail(&rbd_dev->node, &rbd_dev_list);
6051 spin_unlock(&rbd_dev_list_lock);
6052
Ilya Dryomov811c6682016-04-15 16:22:16 +02006053 add_disk(rbd_dev->disk);
Ilya Dryomovca7909e2016-08-18 18:38:41 +02006054 pr_info("%s: capacity %llu features 0x%llx\n", rbd_dev->disk->disk_name,
6055 (unsigned long long)get_capacity(rbd_dev->disk) << SECTOR_SHIFT,
6056 rbd_dev->header.features);
Alex Elder83a06262012-10-30 15:47:17 -05006057
6058 return ret;
Alex Elder2f82ee52012-10-30 19:40:33 -05006059
Alex Elderf35a4de2013-05-06 09:51:29 -05006060err_out_mapping:
6061 rbd_dev_mapping_clear(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05006062err_out_disk:
6063 rbd_free_disk(rbd_dev);
6064err_out_blkdev:
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006065 if (!single_major)
6066 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Ilya Dryomov811c6682016-04-15 16:22:16 +02006067err_out_unlock:
6068 up_write(&rbd_dev->header_rwsem);
Alex Elder83a06262012-10-30 15:47:17 -05006069 return ret;
6070}
6071
Alex Elder332bb122013-04-27 09:59:30 -05006072static int rbd_dev_header_name(struct rbd_device *rbd_dev)
6073{
6074 struct rbd_spec *spec = rbd_dev->spec;
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006075 int ret;
Alex Elder332bb122013-04-27 09:59:30 -05006076
6077 /* Record the header object name for this rbd image. */
6078
6079 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
6080
Yan, Zheng76271512016-02-03 21:24:49 +08006081 rbd_dev->header_oloc.pool = rbd_dev->layout.pool_id;
Alex Elder332bb122013-04-27 09:59:30 -05006082 if (rbd_dev->image_format == 1)
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006083 ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s",
6084 spec->image_name, RBD_SUFFIX);
Alex Elder332bb122013-04-27 09:59:30 -05006085 else
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006086 ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s",
6087 RBD_HEADER_PREFIX, spec->image_id);
Alex Elder332bb122013-04-27 09:59:30 -05006088
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006089 return ret;
Alex Elder332bb122013-04-27 09:59:30 -05006090}
6091
Alex Elder200a6a82013-04-28 23:32:34 -05006092static void rbd_dev_image_release(struct rbd_device *rbd_dev)
6093{
Alex Elder6fd48b32013-04-28 23:32:34 -05006094 rbd_dev_unprobe(rbd_dev);
Alex Elder6fd48b32013-04-28 23:32:34 -05006095 rbd_dev->image_format = 0;
6096 kfree(rbd_dev->spec->image_id);
6097 rbd_dev->spec->image_id = NULL;
6098
Alex Elder200a6a82013-04-28 23:32:34 -05006099 rbd_dev_destroy(rbd_dev);
6100}
6101
Alex Eldera30b71b2012-07-10 20:30:11 -05006102/*
6103 * Probe for the existence of the header object for the given rbd
Alex Elder1f3ef782013-05-06 17:40:33 -05006104 * device. If this image is the one being mapped (i.e., not a
6105 * parent), initiate a watch on its header object before using that
6106 * object to get detailed information about the rbd image.
Alex Eldera30b71b2012-07-10 20:30:11 -05006107 */
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006108static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth)
Alex Eldera30b71b2012-07-10 20:30:11 -05006109{
6110 int ret;
6111
6112 /*
Alex Elder3abef3b2013-05-13 20:35:37 -05006113 * Get the id from the image id object. Unless there's an
6114 * error, rbd_dev->spec->image_id will be filled in with
6115 * a dynamically-allocated string, and rbd_dev->image_format
6116 * will be set to either 1 or 2.
Alex Eldera30b71b2012-07-10 20:30:11 -05006117 */
6118 ret = rbd_dev_image_id(rbd_dev);
6119 if (ret)
Alex Elderc0fba362013-04-25 23:15:08 -05006120 return ret;
Alex Elderc0fba362013-04-25 23:15:08 -05006121
Alex Elder332bb122013-04-27 09:59:30 -05006122 ret = rbd_dev_header_name(rbd_dev);
6123 if (ret)
6124 goto err_out_format;
6125
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006126 if (!depth) {
Ilya Dryomov99d16942016-08-12 16:11:41 +02006127 ret = rbd_register_watch(rbd_dev);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006128 if (ret) {
6129 if (ret == -ENOENT)
6130 pr_info("image %s/%s does not exist\n",
6131 rbd_dev->spec->pool_name,
6132 rbd_dev->spec->image_name);
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006133 goto err_out_format;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006134 }
Alex Elder1f3ef782013-05-06 17:40:33 -05006135 }
Alex Elderb644de22013-04-27 09:59:31 -05006136
Ilya Dryomova720ae02014-07-23 17:11:19 +04006137 ret = rbd_dev_header_info(rbd_dev);
Alex Elder5655c4d2013-04-25 23:15:08 -05006138 if (ret)
Alex Elderb644de22013-04-27 09:59:31 -05006139 goto err_out_watch;
Alex Elder83a06262012-10-30 15:47:17 -05006140
Ilya Dryomov04077592014-07-23 17:11:20 +04006141 /*
6142 * If this image is the one being mapped, we have pool name and
6143 * id, image name and id, and snap name - need to fill snap id.
6144 * Otherwise this is a parent image, identified by pool, image
6145 * and snap ids - need to fill in names for those ids.
6146 */
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006147 if (!depth)
Ilya Dryomov04077592014-07-23 17:11:20 +04006148 ret = rbd_spec_fill_snap_id(rbd_dev);
6149 else
6150 ret = rbd_spec_fill_names(rbd_dev);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006151 if (ret) {
6152 if (ret == -ENOENT)
6153 pr_info("snap %s/%s@%s does not exist\n",
6154 rbd_dev->spec->pool_name,
6155 rbd_dev->spec->image_name,
6156 rbd_dev->spec->snap_name);
Alex Elder33dca392013-04-30 00:44:33 -05006157 goto err_out_probe;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006158 }
Alex Elder9bb81c92013-04-27 09:59:30 -05006159
Ilya Dryomove8f59b52014-07-24 10:42:13 +04006160 if (rbd_dev->header.features & RBD_FEATURE_LAYERING) {
6161 ret = rbd_dev_v2_parent_info(rbd_dev);
6162 if (ret)
6163 goto err_out_probe;
6164
6165 /*
6166 * Need to warn users if this image is the one being
6167 * mapped and has a parent.
6168 */
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006169 if (!depth && rbd_dev->parent_spec)
Ilya Dryomove8f59b52014-07-24 10:42:13 +04006170 rbd_warn(rbd_dev,
6171 "WARNING: kernel layering is EXPERIMENTAL!");
6172 }
6173
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006174 ret = rbd_dev_probe_parent(rbd_dev, depth);
Alex Elder30d60ba2013-05-06 09:51:30 -05006175 if (ret)
6176 goto err_out_probe;
Alex Elder83a06262012-10-30 15:47:17 -05006177
Alex Elder30d60ba2013-05-06 09:51:30 -05006178 dout("discovered format %u image, header name is %s\n",
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006179 rbd_dev->image_format, rbd_dev->header_oid.name);
Alex Elder30d60ba2013-05-06 09:51:30 -05006180 return 0;
Ilya Dryomove8f59b52014-07-24 10:42:13 +04006181
Alex Elder6fd48b32013-04-28 23:32:34 -05006182err_out_probe:
6183 rbd_dev_unprobe(rbd_dev);
Alex Elderb644de22013-04-27 09:59:31 -05006184err_out_watch:
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006185 if (!depth)
Ilya Dryomov99d16942016-08-12 16:11:41 +02006186 rbd_unregister_watch(rbd_dev);
Alex Elder332bb122013-04-27 09:59:30 -05006187err_out_format:
6188 rbd_dev->image_format = 0;
Alex Elder5655c4d2013-04-25 23:15:08 -05006189 kfree(rbd_dev->spec->image_id);
6190 rbd_dev->spec->image_id = NULL;
Alex Elder5655c4d2013-04-25 23:15:08 -05006191 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05006192}
6193
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006194static ssize_t do_rbd_add(struct bus_type *bus,
6195 const char *buf,
6196 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006197{
Alex Eldercb8627c2012-07-09 21:04:23 -05006198 struct rbd_device *rbd_dev = NULL;
Alex Elderdc79b112012-10-25 23:34:41 -05006199 struct ceph_options *ceph_opts = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05006200 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05006201 struct rbd_spec *spec = NULL;
Alex Elder9d3997f2012-10-25 23:34:42 -05006202 struct rbd_client *rbdc;
Alex Elder51344a32013-05-06 07:40:30 -05006203 bool read_only;
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02006204 int rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006205
6206 if (!try_module_get(THIS_MODULE))
6207 return -ENODEV;
6208
Alex Eldera725f65e2012-02-02 08:13:30 -06006209 /* parse add command */
Alex Elder859c31d2012-10-25 23:34:42 -05006210 rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec);
Alex Elderdc79b112012-10-25 23:34:41 -05006211 if (rc < 0)
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006212 goto out;
Alex Eldera725f65e2012-02-02 08:13:30 -06006213
Alex Elder9d3997f2012-10-25 23:34:42 -05006214 rbdc = rbd_get_client(ceph_opts);
6215 if (IS_ERR(rbdc)) {
6216 rc = PTR_ERR(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05006217 goto err_out_args;
Alex Elder9d3997f2012-10-25 23:34:42 -05006218 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006219
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006220 /* pick the pool */
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04006221 rc = rbd_add_get_pool_id(rbdc, spec->pool_name);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006222 if (rc < 0) {
6223 if (rc == -ENOENT)
6224 pr_info("pool %s does not exist\n", spec->pool_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006225 goto err_out_client;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006226 }
Alex Elderc0cd10db2013-04-26 09:43:47 -05006227 spec->pool_id = (u64)rc;
Alex Elder859c31d2012-10-25 23:34:42 -05006228
Ilya Dryomovd1475432015-06-22 13:24:48 +03006229 rbd_dev = rbd_dev_create(rbdc, spec, rbd_opts);
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02006230 if (!rbd_dev) {
6231 rc = -ENOMEM;
Alex Elderbd4ba652012-10-25 23:34:42 -05006232 goto err_out_client;
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02006233 }
Alex Elderc53d5892012-10-25 23:34:42 -05006234 rbdc = NULL; /* rbd_dev now owns this */
6235 spec = NULL; /* rbd_dev now owns this */
Ilya Dryomovd1475432015-06-22 13:24:48 +03006236 rbd_opts = NULL; /* rbd_dev now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006237
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02006238 rbd_dev->config_info = kstrdup(buf, GFP_KERNEL);
6239 if (!rbd_dev->config_info) {
6240 rc = -ENOMEM;
6241 goto err_out_rbd_dev;
6242 }
6243
Ilya Dryomov811c6682016-04-15 16:22:16 +02006244 down_write(&rbd_dev->header_rwsem);
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006245 rc = rbd_dev_image_probe(rbd_dev, 0);
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02006246 if (rc < 0) {
6247 up_write(&rbd_dev->header_rwsem);
Alex Elderc53d5892012-10-25 23:34:42 -05006248 goto err_out_rbd_dev;
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02006249 }
Alex Elder05fd6f62012-08-29 17:11:07 -05006250
Alex Elder7ce4eef2013-05-06 17:40:33 -05006251 /* If we are mapping a snapshot it must be marked read-only */
6252
Ilya Dryomovd1475432015-06-22 13:24:48 +03006253 read_only = rbd_dev->opts->read_only;
Alex Elder7ce4eef2013-05-06 17:40:33 -05006254 if (rbd_dev->spec->snap_id != CEPH_NOSNAP)
6255 read_only = true;
6256 rbd_dev->mapping.read_only = read_only;
6257
Alex Elderb536f692013-04-28 23:32:34 -05006258 rc = rbd_dev_device_setup(rbd_dev);
Alex Elder3abef3b2013-05-13 20:35:37 -05006259 if (rc) {
Ilya Dryomove37180c2013-12-16 18:02:41 +02006260 /*
Ilya Dryomov99d16942016-08-12 16:11:41 +02006261 * rbd_unregister_watch() can't be moved into
Ilya Dryomove37180c2013-12-16 18:02:41 +02006262 * rbd_dev_image_release() without refactoring, see
6263 * commit 1f3ef78861ac.
6264 */
Ilya Dryomov99d16942016-08-12 16:11:41 +02006265 rbd_unregister_watch(rbd_dev);
Alex Elder3abef3b2013-05-13 20:35:37 -05006266 rbd_dev_image_release(rbd_dev);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006267 goto out;
Alex Elder3abef3b2013-05-13 20:35:37 -05006268 }
Alex Elderb536f692013-04-28 23:32:34 -05006269
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006270 rc = count;
6271out:
6272 module_put(THIS_MODULE);
6273 return rc;
Alex Elder3abef3b2013-05-13 20:35:37 -05006274
Alex Elderc53d5892012-10-25 23:34:42 -05006275err_out_rbd_dev:
6276 rbd_dev_destroy(rbd_dev);
Alex Elderbd4ba652012-10-25 23:34:42 -05006277err_out_client:
Alex Elder9d3997f2012-10-25 23:34:42 -05006278 rbd_put_client(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05006279err_out_args:
Alex Elder859c31d2012-10-25 23:34:42 -05006280 rbd_spec_put(spec);
Ilya Dryomovd1475432015-06-22 13:24:48 +03006281 kfree(rbd_opts);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006282 goto out;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006283}
6284
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006285static ssize_t rbd_add(struct bus_type *bus,
6286 const char *buf,
6287 size_t count)
6288{
6289 if (single_major)
6290 return -EINVAL;
6291
6292 return do_rbd_add(bus, buf, count);
6293}
6294
6295static ssize_t rbd_add_single_major(struct bus_type *bus,
6296 const char *buf,
6297 size_t count)
6298{
6299 return do_rbd_add(bus, buf, count);
6300}
6301
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006302static void rbd_dev_device_release(struct rbd_device *rbd_dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006303{
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006304 rbd_free_disk(rbd_dev);
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02006305
6306 spin_lock(&rbd_dev_list_lock);
6307 list_del_init(&rbd_dev->node);
6308 spin_unlock(&rbd_dev_list_lock);
6309
Alex Elder200a6a82013-04-28 23:32:34 -05006310 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006311 device_del(&rbd_dev->dev);
Alex Elder6d80b132013-05-06 07:40:30 -05006312 rbd_dev_mapping_clear(rbd_dev);
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006313 if (!single_major)
6314 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006315}
6316
Alex Elder05a46af2013-04-26 15:44:36 -05006317static void rbd_dev_remove_parent(struct rbd_device *rbd_dev)
6318{
Alex Elderad945fc2013-04-26 15:44:36 -05006319 while (rbd_dev->parent) {
Alex Elder05a46af2013-04-26 15:44:36 -05006320 struct rbd_device *first = rbd_dev;
6321 struct rbd_device *second = first->parent;
6322 struct rbd_device *third;
6323
6324 /*
6325 * Follow to the parent with no grandparent and
6326 * remove it.
6327 */
6328 while (second && (third = second->parent)) {
6329 first = second;
6330 second = third;
6331 }
Alex Elderad945fc2013-04-26 15:44:36 -05006332 rbd_assert(second);
Alex Elder8ad42cd2013-04-28 23:32:34 -05006333 rbd_dev_image_release(second);
Alex Elderad945fc2013-04-26 15:44:36 -05006334 first->parent = NULL;
6335 first->parent_overlap = 0;
6336
6337 rbd_assert(first->parent_spec);
Alex Elder05a46af2013-04-26 15:44:36 -05006338 rbd_spec_put(first->parent_spec);
6339 first->parent_spec = NULL;
Alex Elder05a46af2013-04-26 15:44:36 -05006340 }
6341}
6342
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006343static ssize_t do_rbd_remove(struct bus_type *bus,
6344 const char *buf,
6345 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006346{
6347 struct rbd_device *rbd_dev = NULL;
Alex Elder751cc0e2013-05-31 15:17:01 -05006348 struct list_head *tmp;
6349 int dev_id;
Mike Christie0276dca2016-08-18 18:38:45 +02006350 char opt_buf[6];
Alex Elder82a442d2013-05-31 17:40:44 -05006351 bool already = false;
Mike Christie0276dca2016-08-18 18:38:45 +02006352 bool force = false;
Alex Elder0d8189e2013-04-27 09:59:30 -05006353 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006354
Mike Christie0276dca2016-08-18 18:38:45 +02006355 dev_id = -1;
6356 opt_buf[0] = '\0';
6357 sscanf(buf, "%d %5s", &dev_id, opt_buf);
6358 if (dev_id < 0) {
6359 pr_err("dev_id out of range\n");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006360 return -EINVAL;
Mike Christie0276dca2016-08-18 18:38:45 +02006361 }
6362 if (opt_buf[0] != '\0') {
6363 if (!strcmp(opt_buf, "force")) {
6364 force = true;
6365 } else {
6366 pr_err("bad remove option at '%s'\n", opt_buf);
6367 return -EINVAL;
6368 }
6369 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006370
Alex Elder751cc0e2013-05-31 15:17:01 -05006371 ret = -ENOENT;
6372 spin_lock(&rbd_dev_list_lock);
6373 list_for_each(tmp, &rbd_dev_list) {
6374 rbd_dev = list_entry(tmp, struct rbd_device, node);
6375 if (rbd_dev->dev_id == dev_id) {
6376 ret = 0;
6377 break;
6378 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006379 }
Alex Elder751cc0e2013-05-31 15:17:01 -05006380 if (!ret) {
6381 spin_lock_irq(&rbd_dev->lock);
Mike Christie0276dca2016-08-18 18:38:45 +02006382 if (rbd_dev->open_count && !force)
Alex Elder751cc0e2013-05-31 15:17:01 -05006383 ret = -EBUSY;
6384 else
Alex Elder82a442d2013-05-31 17:40:44 -05006385 already = test_and_set_bit(RBD_DEV_FLAG_REMOVING,
6386 &rbd_dev->flags);
Alex Elder751cc0e2013-05-31 15:17:01 -05006387 spin_unlock_irq(&rbd_dev->lock);
6388 }
6389 spin_unlock(&rbd_dev_list_lock);
Alex Elder82a442d2013-05-31 17:40:44 -05006390 if (ret < 0 || already)
Alex Elder1ba0f1e2013-05-31 15:17:01 -05006391 return ret;
Alex Elder751cc0e2013-05-31 15:17:01 -05006392
Mike Christie0276dca2016-08-18 18:38:45 +02006393 if (force) {
6394 /*
6395 * Prevent new IO from being queued and wait for existing
6396 * IO to complete/fail.
6397 */
6398 blk_mq_freeze_queue(rbd_dev->disk->queue);
6399 blk_set_queue_dying(rbd_dev->disk->queue);
6400 }
6401
Ilya Dryomoved95b212016-08-12 16:40:02 +02006402 down_write(&rbd_dev->lock_rwsem);
6403 if (__rbd_is_lock_owner(rbd_dev))
6404 rbd_unlock(rbd_dev);
6405 up_write(&rbd_dev->lock_rwsem);
Ilya Dryomov99d16942016-08-12 16:11:41 +02006406 rbd_unregister_watch(rbd_dev);
Ilya Dryomovfca27062013-12-16 18:02:40 +02006407
Josh Durgin98752012013-08-29 17:26:31 -07006408 /*
6409 * Don't free anything from rbd_dev->disk until after all
6410 * notifies are completely processed. Otherwise
6411 * rbd_bus_del_dev() will race with rbd_watch_cb(), resulting
6412 * in a potential use after free of rbd_dev->disk or rbd_dev.
6413 */
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006414 rbd_dev_device_release(rbd_dev);
Alex Elder8ad42cd2013-04-28 23:32:34 -05006415 rbd_dev_image_release(rbd_dev);
Alex Elderaafb2302012-09-06 16:00:54 -05006416
Alex Elder1ba0f1e2013-05-31 15:17:01 -05006417 return count;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006418}
6419
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006420static ssize_t rbd_remove(struct bus_type *bus,
6421 const char *buf,
6422 size_t count)
6423{
6424 if (single_major)
6425 return -EINVAL;
6426
6427 return do_rbd_remove(bus, buf, count);
6428}
6429
6430static ssize_t rbd_remove_single_major(struct bus_type *bus,
6431 const char *buf,
6432 size_t count)
6433{
6434 return do_rbd_remove(bus, buf, count);
6435}
6436
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006437/*
6438 * create control files in sysfs
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006439 * /sys/bus/rbd/...
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006440 */
6441static int rbd_sysfs_init(void)
6442{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006443 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006444
Alex Elderfed4c142012-02-07 12:03:36 -06006445 ret = device_register(&rbd_root_dev);
Alex Elder21079782012-01-24 10:08:36 -06006446 if (ret < 0)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006447 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006448
Alex Elderfed4c142012-02-07 12:03:36 -06006449 ret = bus_register(&rbd_bus_type);
6450 if (ret < 0)
6451 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006452
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006453 return ret;
6454}
6455
6456static void rbd_sysfs_cleanup(void)
6457{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006458 bus_unregister(&rbd_bus_type);
Alex Elderfed4c142012-02-07 12:03:36 -06006459 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006460}
6461
Alex Elder1c2a9df2013-05-01 12:43:03 -05006462static int rbd_slab_init(void)
6463{
6464 rbd_assert(!rbd_img_request_cache);
Geliang Tang03d94402016-03-13 15:17:32 +08006465 rbd_img_request_cache = KMEM_CACHE(rbd_img_request, 0);
Alex Elder868311b2013-05-01 12:43:03 -05006466 if (!rbd_img_request_cache)
6467 return -ENOMEM;
6468
6469 rbd_assert(!rbd_obj_request_cache);
Geliang Tang03d94402016-03-13 15:17:32 +08006470 rbd_obj_request_cache = KMEM_CACHE(rbd_obj_request, 0);
Alex Elder78c2a442013-05-01 12:43:04 -05006471 if (!rbd_obj_request_cache)
6472 goto out_err;
6473
6474 rbd_assert(!rbd_segment_name_cache);
6475 rbd_segment_name_cache = kmem_cache_create("rbd_segment_name",
Ilya Dryomov2d0ebc52014-01-27 17:40:18 +02006476 CEPH_MAX_OID_NAME_LEN + 1, 1, 0, NULL);
Alex Elder78c2a442013-05-01 12:43:04 -05006477 if (rbd_segment_name_cache)
Alex Elder1c2a9df2013-05-01 12:43:03 -05006478 return 0;
Alex Elder78c2a442013-05-01 12:43:04 -05006479out_err:
Julia Lawall13bf2832015-09-13 14:15:26 +02006480 kmem_cache_destroy(rbd_obj_request_cache);
6481 rbd_obj_request_cache = NULL;
Alex Elder1c2a9df2013-05-01 12:43:03 -05006482
Alex Elder868311b2013-05-01 12:43:03 -05006483 kmem_cache_destroy(rbd_img_request_cache);
6484 rbd_img_request_cache = NULL;
6485
Alex Elder1c2a9df2013-05-01 12:43:03 -05006486 return -ENOMEM;
6487}
6488
6489static void rbd_slab_exit(void)
6490{
Alex Elder78c2a442013-05-01 12:43:04 -05006491 rbd_assert(rbd_segment_name_cache);
6492 kmem_cache_destroy(rbd_segment_name_cache);
6493 rbd_segment_name_cache = NULL;
6494
Alex Elder868311b2013-05-01 12:43:03 -05006495 rbd_assert(rbd_obj_request_cache);
6496 kmem_cache_destroy(rbd_obj_request_cache);
6497 rbd_obj_request_cache = NULL;
6498
Alex Elder1c2a9df2013-05-01 12:43:03 -05006499 rbd_assert(rbd_img_request_cache);
6500 kmem_cache_destroy(rbd_img_request_cache);
6501 rbd_img_request_cache = NULL;
6502}
6503
Alex Eldercc344fa2013-02-19 12:25:56 -06006504static int __init rbd_init(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006505{
6506 int rc;
6507
Alex Elder1e32d342013-01-30 11:13:33 -06006508 if (!libceph_compatible(NULL)) {
6509 rbd_warn(NULL, "libceph incompatibility (quitting)");
Alex Elder1e32d342013-01-30 11:13:33 -06006510 return -EINVAL;
6511 }
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006512
Alex Elder1c2a9df2013-05-01 12:43:03 -05006513 rc = rbd_slab_init();
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006514 if (rc)
6515 return rc;
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006516
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006517 /*
6518 * The number of active work items is limited by the number of
Ilya Dryomovf77303b2015-04-22 18:28:13 +03006519 * rbd devices * queue depth, so leave @max_active at default.
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006520 */
6521 rbd_wq = alloc_workqueue(RBD_DRV_NAME, WQ_MEM_RECLAIM, 0);
6522 if (!rbd_wq) {
6523 rc = -ENOMEM;
6524 goto err_out_slab;
6525 }
6526
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006527 if (single_major) {
6528 rbd_major = register_blkdev(0, RBD_DRV_NAME);
6529 if (rbd_major < 0) {
6530 rc = rbd_major;
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006531 goto err_out_wq;
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006532 }
6533 }
6534
Alex Elder1c2a9df2013-05-01 12:43:03 -05006535 rc = rbd_sysfs_init();
6536 if (rc)
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006537 goto err_out_blkdev;
Alex Elder1c2a9df2013-05-01 12:43:03 -05006538
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006539 if (single_major)
6540 pr_info("loaded (major %d)\n", rbd_major);
6541 else
6542 pr_info("loaded\n");
6543
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006544 return 0;
6545
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006546err_out_blkdev:
6547 if (single_major)
6548 unregister_blkdev(rbd_major, RBD_DRV_NAME);
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006549err_out_wq:
6550 destroy_workqueue(rbd_wq);
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006551err_out_slab:
6552 rbd_slab_exit();
Alex Elder1c2a9df2013-05-01 12:43:03 -05006553 return rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006554}
6555
Alex Eldercc344fa2013-02-19 12:25:56 -06006556static void __exit rbd_exit(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006557{
Ilya Dryomovffe312c2014-05-20 15:46:04 +04006558 ida_destroy(&rbd_dev_id_ida);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006559 rbd_sysfs_cleanup();
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006560 if (single_major)
6561 unregister_blkdev(rbd_major, RBD_DRV_NAME);
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006562 destroy_workqueue(rbd_wq);
Alex Elder1c2a9df2013-05-01 12:43:03 -05006563 rbd_slab_exit();
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006564}
6565
6566module_init(rbd_init);
6567module_exit(rbd_exit);
6568
Alex Elderd552c612013-05-31 20:13:09 -05006569MODULE_AUTHOR("Alex Elder <elder@inktank.com>");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006570MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
6571MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006572/* following authorship retained from original osdblk.c */
6573MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
6574
Ilya Dryomov90da2582013-12-13 15:28:56 +02006575MODULE_DESCRIPTION("RADOS Block Device (RBD) driver");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006576MODULE_LICENSE("GPL");