blob: fd1a9891b348f6f3e48461f8df5214a9a5473fad [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 Elderecb4dc222013-04-26 09:43:47 -0500187 const char *pool_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500188
Alex Elderecb4dc222013-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 Elderecb4dc222013-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;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700373
Ilya Dryomovc41d13a2016-04-29 20:01:25 +0200374 struct ceph_object_id header_oid;
Ilya Dryomov922dab62016-05-26 01:15:02 +0200375 struct ceph_object_locator header_oloc;
Alex Elder971f8392012-10-25 23:34:41 -0500376
Ilya Dryomov1643dfa2016-08-12 15:45:52 +0200377 struct ceph_file_layout layout; /* used for all rbd requests */
Alex Elder0903e872012-11-14 12:25:19 -0600378
Ilya Dryomov99d16942016-08-12 16:11:41 +0200379 struct mutex watch_mutex;
380 enum rbd_watch_state watch_state;
Ilya Dryomov922dab62016-05-26 01:15:02 +0200381 struct ceph_osd_linger_request *watch_handle;
Ilya Dryomov99d16942016-08-12 16:11:41 +0200382 u64 watch_cookie;
383 struct delayed_work watch_dwork;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700384
Ilya Dryomoved95b212016-08-12 16:40:02 +0200385 struct rw_semaphore lock_rwsem;
386 enum rbd_lock_state lock_state;
387 struct rbd_client_id owner_cid;
388 struct work_struct acquired_lock_work;
389 struct work_struct released_lock_work;
390 struct delayed_work lock_dwork;
391 struct work_struct unlock_work;
392 wait_queue_head_t lock_waitq;
393
Ilya Dryomov1643dfa2016-08-12 15:45:52 +0200394 struct workqueue_struct *task_wq;
395
Alex Elder86b00e02012-10-25 23:34:42 -0500396 struct rbd_spec *parent_spec;
397 u64 parent_overlap;
Alex Eldera2acd002013-05-08 22:50:04 -0500398 atomic_t parent_ref;
Alex Elder2f82ee52012-10-30 19:40:33 -0500399 struct rbd_device *parent;
Alex Elder86b00e02012-10-25 23:34:42 -0500400
Christoph Hellwig7ad18af2015-01-13 17:20:04 +0100401 /* Block layer tags. */
402 struct blk_mq_tag_set tag_set;
403
Josh Durginc6666012011-11-21 17:11:12 -0800404 /* protects updating the header */
405 struct rw_semaphore header_rwsem;
Alex Elderf84344f2012-08-31 17:29:51 -0500406
407 struct rbd_mapping mapping;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700408
409 struct list_head node;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800410
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800411 /* sysfs related */
412 struct device dev;
Alex Elderb82d1672013-01-14 12:43:31 -0600413 unsigned long open_count; /* protected by lock */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800414};
415
Alex Elderb82d1672013-01-14 12:43:31 -0600416/*
417 * Flag bits for rbd_dev->flags. If atomicity is required,
418 * rbd_dev->lock is used to protect access.
419 *
420 * Currently, only the "removing" flag (which is coupled with the
421 * "open_count" field) requires atomic access.
422 */
Alex Elder6d292902013-01-14 12:43:31 -0600423enum rbd_dev_flags {
424 RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */
Alex Elderb82d1672013-01-14 12:43:31 -0600425 RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */
Alex Elder6d292902013-01-14 12:43:31 -0600426};
427
Alex Eldercfbf6372013-05-31 17:40:45 -0500428static DEFINE_MUTEX(client_mutex); /* Serialize client creation */
Alex Eldere124a82f2012-01-29 13:57:44 -0600429
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700430static LIST_HEAD(rbd_dev_list); /* devices */
Alex Eldere124a82f2012-01-29 13:57:44 -0600431static DEFINE_SPINLOCK(rbd_dev_list_lock);
432
Alex Elder432b8582012-01-29 13:57:44 -0600433static LIST_HEAD(rbd_client_list); /* clients */
434static DEFINE_SPINLOCK(rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700435
Alex Elder78c2a442013-05-01 12:43:04 -0500436/* Slab caches for frequently-allocated structures */
437
Alex Elder1c2a9df2013-05-01 12:43:03 -0500438static struct kmem_cache *rbd_img_request_cache;
Alex Elder868311b2013-05-01 12:43:03 -0500439static struct kmem_cache *rbd_obj_request_cache;
Alex Elder78c2a442013-05-01 12:43:04 -0500440static struct kmem_cache *rbd_segment_name_cache;
Alex Elder1c2a9df2013-05-01 12:43:03 -0500441
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200442static int rbd_major;
Ilya Dryomovf8a22fc2013-12-13 15:28:57 +0200443static DEFINE_IDA(rbd_dev_id_ida);
444
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +0400445static struct workqueue_struct *rbd_wq;
446
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200447/*
448 * Default to false for now, as single-major requires >= 0.75 version of
449 * userspace rbd utility.
450 */
451static bool single_major = false;
452module_param(single_major, bool, S_IRUGO);
453MODULE_PARM_DESC(single_major, "Use a single major number for all rbd devices (default: false)");
454
Alex Elder3d7efd12013-04-19 15:34:50 -0500455static int rbd_img_request_submit(struct rbd_img_request *img_request);
456
Alex Elderf0f8cef2012-01-29 13:57:44 -0600457static ssize_t rbd_add(struct bus_type *bus, const char *buf,
458 size_t count);
459static ssize_t rbd_remove(struct bus_type *bus, const char *buf,
460 size_t count);
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200461static ssize_t rbd_add_single_major(struct bus_type *bus, const char *buf,
462 size_t count);
463static ssize_t rbd_remove_single_major(struct bus_type *bus, const char *buf,
464 size_t count);
Ilya Dryomov6d69bb532015-10-11 19:38:00 +0200465static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth);
Alex Eldera2acd002013-05-08 22:50:04 -0500466static void rbd_spec_put(struct rbd_spec *spec);
Alex Elderf0f8cef2012-01-29 13:57:44 -0600467
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200468static int rbd_dev_id_to_minor(int dev_id)
469{
Ilya Dryomov7e513d42013-12-16 19:26:32 +0200470 return dev_id << RBD_SINGLE_MAJOR_PART_SHIFT;
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200471}
472
473static int minor_to_rbd_dev_id(int minor)
474{
Ilya Dryomov7e513d42013-12-16 19:26:32 +0200475 return minor >> RBD_SINGLE_MAJOR_PART_SHIFT;
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200476}
477
Ilya Dryomoved95b212016-08-12 16:40:02 +0200478static bool rbd_is_lock_supported(struct rbd_device *rbd_dev)
479{
480 return (rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK) &&
481 rbd_dev->spec->snap_id == CEPH_NOSNAP &&
482 !rbd_dev->mapping.read_only;
483}
484
485static bool __rbd_is_lock_owner(struct rbd_device *rbd_dev)
486{
487 return rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED ||
488 rbd_dev->lock_state == RBD_LOCK_STATE_RELEASING;
489}
490
491static bool rbd_is_lock_owner(struct rbd_device *rbd_dev)
492{
493 bool is_lock_owner;
494
495 down_read(&rbd_dev->lock_rwsem);
496 is_lock_owner = __rbd_is_lock_owner(rbd_dev);
497 up_read(&rbd_dev->lock_rwsem);
498 return is_lock_owner;
499}
500
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700501static BUS_ATTR(add, S_IWUSR, NULL, rbd_add);
502static BUS_ATTR(remove, S_IWUSR, NULL, rbd_remove);
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200503static BUS_ATTR(add_single_major, S_IWUSR, NULL, rbd_add_single_major);
504static BUS_ATTR(remove_single_major, S_IWUSR, NULL, rbd_remove_single_major);
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700505
506static struct attribute *rbd_bus_attrs[] = {
507 &bus_attr_add.attr,
508 &bus_attr_remove.attr,
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200509 &bus_attr_add_single_major.attr,
510 &bus_attr_remove_single_major.attr,
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700511 NULL,
Alex Elderf0f8cef2012-01-29 13:57:44 -0600512};
Ilya Dryomov92c76dc2013-12-13 15:28:57 +0200513
514static umode_t rbd_bus_is_visible(struct kobject *kobj,
515 struct attribute *attr, int index)
516{
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200517 if (!single_major &&
518 (attr == &bus_attr_add_single_major.attr ||
519 attr == &bus_attr_remove_single_major.attr))
520 return 0;
521
Ilya Dryomov92c76dc2013-12-13 15:28:57 +0200522 return attr->mode;
523}
524
525static const struct attribute_group rbd_bus_group = {
526 .attrs = rbd_bus_attrs,
527 .is_visible = rbd_bus_is_visible,
528};
529__ATTRIBUTE_GROUPS(rbd_bus);
Alex Elderf0f8cef2012-01-29 13:57:44 -0600530
531static struct bus_type rbd_bus_type = {
532 .name = "rbd",
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700533 .bus_groups = rbd_bus_groups,
Alex Elderf0f8cef2012-01-29 13:57:44 -0600534};
535
536static void rbd_root_dev_release(struct device *dev)
537{
538}
539
540static struct device rbd_root_dev = {
541 .init_name = "rbd",
542 .release = rbd_root_dev_release,
543};
544
Alex Elder06ecc6c2012-11-01 10:17:15 -0500545static __printf(2, 3)
546void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...)
547{
548 struct va_format vaf;
549 va_list args;
550
551 va_start(args, fmt);
552 vaf.fmt = fmt;
553 vaf.va = &args;
554
555 if (!rbd_dev)
556 printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf);
557 else if (rbd_dev->disk)
558 printk(KERN_WARNING "%s: %s: %pV\n",
559 RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf);
560 else if (rbd_dev->spec && rbd_dev->spec->image_name)
561 printk(KERN_WARNING "%s: image %s: %pV\n",
562 RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf);
563 else if (rbd_dev->spec && rbd_dev->spec->image_id)
564 printk(KERN_WARNING "%s: id %s: %pV\n",
565 RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf);
566 else /* punt */
567 printk(KERN_WARNING "%s: rbd_dev %p: %pV\n",
568 RBD_DRV_NAME, rbd_dev, &vaf);
569 va_end(args);
570}
571
Alex Elderaafb2302012-09-06 16:00:54 -0500572#ifdef RBD_DEBUG
573#define rbd_assert(expr) \
574 if (unlikely(!(expr))) { \
575 printk(KERN_ERR "\nAssertion failure in %s() " \
576 "at line %d:\n\n" \
577 "\trbd_assert(%s);\n\n", \
578 __func__, __LINE__, #expr); \
579 BUG(); \
580 }
581#else /* !RBD_DEBUG */
582# define rbd_assert(expr) ((void) 0)
583#endif /* !RBD_DEBUG */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800584
Ilya Dryomov27617132015-07-16 17:36:11 +0300585static void rbd_osd_copyup_callback(struct rbd_obj_request *obj_request);
Alex Elderb454e362013-04-19 15:34:50 -0500586static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request);
Alex Elder05a46af2013-04-26 15:44:36 -0500587static void rbd_img_parent_read(struct rbd_obj_request *obj_request);
588static void rbd_dev_remove_parent(struct rbd_device *rbd_dev);
Alex Elder8b3e1a52013-01-24 16:13:36 -0600589
Alex Eldercc4a38bd2013-04-30 00:44:33 -0500590static int rbd_dev_refresh(struct rbd_device *rbd_dev);
Alex Elder2df3fac2013-05-06 09:51:30 -0500591static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev);
Ilya Dryomova720ae02014-07-23 17:11:19 +0400592static int rbd_dev_header_info(struct rbd_device *rbd_dev);
Ilya Dryomove8f59b52014-07-24 10:42:13 +0400593static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev);
Alex Elder54cac612013-04-30 00:44:33 -0500594static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
595 u64 snap_id);
Alex Elder2ad3d712013-04-30 00:44:33 -0500596static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
597 u8 *order, u64 *snap_size);
598static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
599 u64 *snap_features);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700600
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700601static int rbd_open(struct block_device *bdev, fmode_t mode)
602{
Alex Elderf0f8cef2012-01-29 13:57:44 -0600603 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
Alex Elderb82d1672013-01-14 12:43:31 -0600604 bool removing = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700605
Alex Elderf84344f2012-08-31 17:29:51 -0500606 if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700607 return -EROFS;
608
Alex Eldera14ea262013-02-05 13:23:12 -0600609 spin_lock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600610 if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags))
611 removing = true;
612 else
613 rbd_dev->open_count++;
Alex Eldera14ea262013-02-05 13:23:12 -0600614 spin_unlock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600615 if (removing)
616 return -ENOENT;
617
Alex Elderc3e946c2012-11-16 09:29:16 -0600618 (void) get_device(&rbd_dev->dev);
Alex Elder340c7a22012-08-10 13:12:07 -0700619
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700620 return 0;
621}
622
Al Virodb2a1442013-05-05 21:52:57 -0400623static void rbd_release(struct gendisk *disk, fmode_t mode)
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800624{
625 struct rbd_device *rbd_dev = disk->private_data;
Alex Elderb82d1672013-01-14 12:43:31 -0600626 unsigned long open_count_before;
627
Alex Eldera14ea262013-02-05 13:23:12 -0600628 spin_lock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600629 open_count_before = rbd_dev->open_count--;
Alex Eldera14ea262013-02-05 13:23:12 -0600630 spin_unlock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600631 rbd_assert(open_count_before > 0);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800632
Alex Elderc3e946c2012-11-16 09:29:16 -0600633 put_device(&rbd_dev->dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800634}
635
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800636static int rbd_ioctl_set_ro(struct rbd_device *rbd_dev, unsigned long arg)
637{
Josh Durgin77f33c02013-09-30 17:09:54 -0700638 int ret = 0;
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800639 int val;
640 bool ro;
Josh Durgin77f33c02013-09-30 17:09:54 -0700641 bool ro_changed = false;
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800642
Josh Durgin77f33c02013-09-30 17:09:54 -0700643 /* get_user() may sleep, so call it before taking rbd_dev->lock */
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800644 if (get_user(val, (int __user *)(arg)))
645 return -EFAULT;
646
647 ro = val ? true : false;
648 /* Snapshot doesn't allow to write*/
649 if (rbd_dev->spec->snap_id != CEPH_NOSNAP && !ro)
650 return -EROFS;
651
Josh Durgin77f33c02013-09-30 17:09:54 -0700652 spin_lock_irq(&rbd_dev->lock);
653 /* prevent others open this device */
654 if (rbd_dev->open_count > 1) {
655 ret = -EBUSY;
656 goto out;
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800657 }
658
Josh Durgin77f33c02013-09-30 17:09:54 -0700659 if (rbd_dev->mapping.read_only != ro) {
660 rbd_dev->mapping.read_only = ro;
661 ro_changed = true;
662 }
663
664out:
665 spin_unlock_irq(&rbd_dev->lock);
666 /* set_disk_ro() may sleep, so call it after releasing rbd_dev->lock */
667 if (ret == 0 && ro_changed)
668 set_disk_ro(rbd_dev->disk, ro ? 1 : 0);
669
670 return ret;
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800671}
672
673static int rbd_ioctl(struct block_device *bdev, fmode_t mode,
674 unsigned int cmd, unsigned long arg)
675{
676 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
677 int ret = 0;
678
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800679 switch (cmd) {
680 case BLKROSET:
681 ret = rbd_ioctl_set_ro(rbd_dev, arg);
682 break;
683 default:
684 ret = -ENOTTY;
685 }
686
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800687 return ret;
688}
689
690#ifdef CONFIG_COMPAT
691static int rbd_compat_ioctl(struct block_device *bdev, fmode_t mode,
692 unsigned int cmd, unsigned long arg)
693{
694 return rbd_ioctl(bdev, mode, cmd, arg);
695}
696#endif /* CONFIG_COMPAT */
697
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700698static const struct block_device_operations rbd_bd_ops = {
699 .owner = THIS_MODULE,
700 .open = rbd_open,
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800701 .release = rbd_release,
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800702 .ioctl = rbd_ioctl,
703#ifdef CONFIG_COMPAT
704 .compat_ioctl = rbd_compat_ioctl,
705#endif
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700706};
707
708/*
Alex Elder7262cfc2013-05-16 15:04:20 -0500709 * Initialize an rbd client instance. Success or not, this function
Alex Eldercfbf6372013-05-31 17:40:45 -0500710 * consumes ceph_opts. Caller holds client_mutex.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700711 */
Alex Elderf8c38922012-08-10 13:12:07 -0700712static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700713{
714 struct rbd_client *rbdc;
715 int ret = -ENOMEM;
716
Alex Elder37206ee2013-02-20 17:32:08 -0600717 dout("%s:\n", __func__);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700718 rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL);
719 if (!rbdc)
720 goto out_opt;
721
722 kref_init(&rbdc->kref);
723 INIT_LIST_HEAD(&rbdc->node);
724
Alex Elder43ae4702012-07-03 16:01:18 -0500725 rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700726 if (IS_ERR(rbdc->client))
Alex Elder08f75462013-05-29 11:19:00 -0500727 goto out_rbdc;
Alex Elder43ae4702012-07-03 16:01:18 -0500728 ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700729
730 ret = ceph_open_session(rbdc->client);
731 if (ret < 0)
Alex Elder08f75462013-05-29 11:19:00 -0500732 goto out_client;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700733
Alex Elder432b8582012-01-29 13:57:44 -0600734 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700735 list_add_tail(&rbdc->node, &rbd_client_list);
Alex Elder432b8582012-01-29 13:57:44 -0600736 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700737
Alex Elder37206ee2013-02-20 17:32:08 -0600738 dout("%s: rbdc %p\n", __func__, rbdc);
Alex Elderbc534d82012-01-29 13:57:44 -0600739
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700740 return rbdc;
Alex Elder08f75462013-05-29 11:19:00 -0500741out_client:
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700742 ceph_destroy_client(rbdc->client);
Alex Elder08f75462013-05-29 11:19:00 -0500743out_rbdc:
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700744 kfree(rbdc);
745out_opt:
Alex Elder43ae4702012-07-03 16:01:18 -0500746 if (ceph_opts)
747 ceph_destroy_options(ceph_opts);
Alex Elder37206ee2013-02-20 17:32:08 -0600748 dout("%s: error %d\n", __func__, ret);
749
Vasiliy Kulikov28f259b2010-09-26 12:59:37 +0400750 return ERR_PTR(ret);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700751}
752
Alex Elder2f82ee52012-10-30 19:40:33 -0500753static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc)
754{
755 kref_get(&rbdc->kref);
756
757 return rbdc;
758}
759
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700760/*
Alex Elder1f7ba332012-08-10 13:12:07 -0700761 * Find a ceph client with specific addr and configuration. If
762 * found, bump its reference count.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700763 */
Alex Elder1f7ba332012-08-10 13:12:07 -0700764static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700765{
766 struct rbd_client *client_node;
Alex Elder1f7ba332012-08-10 13:12:07 -0700767 bool found = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700768
Alex Elder43ae4702012-07-03 16:01:18 -0500769 if (ceph_opts->flags & CEPH_OPT_NOSHARE)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700770 return NULL;
771
Alex Elder1f7ba332012-08-10 13:12:07 -0700772 spin_lock(&rbd_client_list_lock);
773 list_for_each_entry(client_node, &rbd_client_list, node) {
774 if (!ceph_compare_options(ceph_opts, client_node->client)) {
Alex Elder2f82ee52012-10-30 19:40:33 -0500775 __rbd_get_client(client_node);
776
Alex Elder1f7ba332012-08-10 13:12:07 -0700777 found = true;
778 break;
779 }
780 }
781 spin_unlock(&rbd_client_list_lock);
782
783 return found ? client_node : NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700784}
785
786/*
Ilya Dryomov210c1042015-06-22 13:24:48 +0300787 * (Per device) rbd map options
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700788 */
789enum {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300790 Opt_queue_depth,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700791 Opt_last_int,
792 /* int args above */
793 Opt_last_string,
794 /* string args above */
Alex Eldercc0538b2012-08-10 13:12:07 -0700795 Opt_read_only,
796 Opt_read_write,
Ilya Dryomov210c1042015-06-22 13:24:48 +0300797 Opt_err
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700798};
799
Alex Elder43ae4702012-07-03 16:01:18 -0500800static match_table_t rbd_opts_tokens = {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300801 {Opt_queue_depth, "queue_depth=%d"},
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700802 /* int args above */
803 /* string args above */
Alex Elderbe466c12012-10-22 11:31:26 -0500804 {Opt_read_only, "read_only"},
Alex Eldercc0538b2012-08-10 13:12:07 -0700805 {Opt_read_only, "ro"}, /* Alternate spelling */
806 {Opt_read_write, "read_write"},
807 {Opt_read_write, "rw"}, /* Alternate spelling */
Ilya Dryomov210c1042015-06-22 13:24:48 +0300808 {Opt_err, NULL}
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700809};
810
Alex Elder98571b52013-01-20 14:44:42 -0600811struct rbd_options {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300812 int queue_depth;
Alex Elder98571b52013-01-20 14:44:42 -0600813 bool read_only;
814};
815
Ilya Dryomovb5584182015-06-23 16:21:19 +0300816#define RBD_QUEUE_DEPTH_DEFAULT BLKDEV_MAX_RQ
Alex Elder98571b52013-01-20 14:44:42 -0600817#define RBD_READ_ONLY_DEFAULT false
818
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700819static int parse_rbd_opts_token(char *c, void *private)
820{
Alex Elder43ae4702012-07-03 16:01:18 -0500821 struct rbd_options *rbd_opts = private;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700822 substring_t argstr[MAX_OPT_ARGS];
823 int token, intval, ret;
824
Alex Elder43ae4702012-07-03 16:01:18 -0500825 token = match_token(c, rbd_opts_tokens, argstr);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700826 if (token < Opt_last_int) {
827 ret = match_int(&argstr[0], &intval);
828 if (ret < 0) {
Ilya Dryomov210c1042015-06-22 13:24:48 +0300829 pr_err("bad mount option arg (not int) at '%s'\n", c);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700830 return ret;
831 }
832 dout("got int token %d val %d\n", token, intval);
833 } else if (token > Opt_last_int && token < Opt_last_string) {
Ilya Dryomov210c1042015-06-22 13:24:48 +0300834 dout("got string token %d val %s\n", token, argstr[0].from);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700835 } else {
836 dout("got token %d\n", token);
837 }
838
839 switch (token) {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300840 case Opt_queue_depth:
841 if (intval < 1) {
842 pr_err("queue_depth out of range\n");
843 return -EINVAL;
844 }
845 rbd_opts->queue_depth = intval;
846 break;
Alex Eldercc0538b2012-08-10 13:12:07 -0700847 case Opt_read_only:
848 rbd_opts->read_only = true;
849 break;
850 case Opt_read_write:
851 rbd_opts->read_only = false;
852 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700853 default:
Ilya Dryomov210c1042015-06-22 13:24:48 +0300854 /* libceph prints "bad option" msg */
855 return -EINVAL;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700856 }
Ilya Dryomov210c1042015-06-22 13:24:48 +0300857
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700858 return 0;
859}
860
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800861static char* obj_op_name(enum obj_operation_type op_type)
862{
863 switch (op_type) {
864 case OBJ_OP_READ:
865 return "read";
866 case OBJ_OP_WRITE:
867 return "write";
Guangliang Zhao90e98c52014-04-01 22:22:16 +0800868 case OBJ_OP_DISCARD:
869 return "discard";
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800870 default:
871 return "???";
872 }
873}
874
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700875/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700876 * Get a ceph client with specific addr and configuration, if one does
Alex Elder7262cfc2013-05-16 15:04:20 -0500877 * not exist create it. Either way, ceph_opts is consumed by this
878 * function.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700879 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500880static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700881{
Alex Elderf8c38922012-08-10 13:12:07 -0700882 struct rbd_client *rbdc;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700883
Alex Eldercfbf6372013-05-31 17:40:45 -0500884 mutex_lock_nested(&client_mutex, SINGLE_DEPTH_NESTING);
Alex Elder1f7ba332012-08-10 13:12:07 -0700885 rbdc = rbd_client_find(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500886 if (rbdc) /* using an existing client */
Alex Elder43ae4702012-07-03 16:01:18 -0500887 ceph_destroy_options(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500888 else
Alex Elderf8c38922012-08-10 13:12:07 -0700889 rbdc = rbd_client_create(ceph_opts);
Alex Eldercfbf6372013-05-31 17:40:45 -0500890 mutex_unlock(&client_mutex);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700891
Alex Elder9d3997f2012-10-25 23:34:42 -0500892 return rbdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700893}
894
895/*
896 * Destroy ceph client
Alex Elderd23a4b32012-01-29 13:57:43 -0600897 *
Alex Elder432b8582012-01-29 13:57:44 -0600898 * Caller must hold rbd_client_list_lock.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700899 */
900static void rbd_client_release(struct kref *kref)
901{
902 struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
903
Alex Elder37206ee2013-02-20 17:32:08 -0600904 dout("%s: rbdc %p\n", __func__, rbdc);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500905 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700906 list_del(&rbdc->node);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500907 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700908
909 ceph_destroy_client(rbdc->client);
910 kfree(rbdc);
911}
912
913/*
914 * Drop reference to ceph client node. If it's not referenced anymore, release
915 * it.
916 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500917static void rbd_put_client(struct rbd_client *rbdc)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700918{
Alex Elderc53d5892012-10-25 23:34:42 -0500919 if (rbdc)
920 kref_put(&rbdc->kref, rbd_client_release);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700921}
922
Alex Eldera30b71b2012-07-10 20:30:11 -0500923static bool rbd_image_format_valid(u32 image_format)
924{
925 return image_format == 1 || image_format == 2;
926}
927
Alex Elder8e94af82012-07-25 09:32:40 -0500928static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
929{
Alex Elder103a1502012-08-02 11:29:45 -0500930 size_t size;
931 u32 snap_count;
932
933 /* The header has to start with the magic rbd header text */
934 if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT)))
935 return false;
936
Alex Elderdb2388b2012-10-20 22:17:27 -0500937 /* The bio layer requires at least sector-sized I/O */
938
939 if (ondisk->options.order < SECTOR_SHIFT)
940 return false;
941
942 /* If we use u64 in a few spots we may be able to loosen this */
943
944 if (ondisk->options.order > 8 * sizeof (int) - 1)
945 return false;
946
Alex Elder103a1502012-08-02 11:29:45 -0500947 /*
948 * The size of a snapshot header has to fit in a size_t, and
949 * that limits the number of snapshots.
950 */
951 snap_count = le32_to_cpu(ondisk->snap_count);
952 size = SIZE_MAX - sizeof (struct ceph_snap_context);
953 if (snap_count > size / sizeof (__le64))
954 return false;
955
956 /*
957 * Not only that, but the size of the entire the snapshot
958 * header must also be representable in a size_t.
959 */
960 size -= snap_count * sizeof (__le64);
961 if ((u64) size < le64_to_cpu(ondisk->snap_names_len))
962 return false;
963
964 return true;
Alex Elder8e94af82012-07-25 09:32:40 -0500965}
966
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700967/*
Alex Elderbb23e372013-05-06 09:51:29 -0500968 * Fill an rbd image header with information from the given format 1
969 * on-disk header.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700970 */
Alex Elder662518b2013-05-06 09:51:29 -0500971static int rbd_header_from_disk(struct rbd_device *rbd_dev,
Alex Elder4156d992012-08-02 11:29:46 -0500972 struct rbd_image_header_ondisk *ondisk)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700973{
Alex Elder662518b2013-05-06 09:51:29 -0500974 struct rbd_image_header *header = &rbd_dev->header;
Alex Elderbb23e372013-05-06 09:51:29 -0500975 bool first_time = header->object_prefix == NULL;
976 struct ceph_snap_context *snapc;
977 char *object_prefix = NULL;
978 char *snap_names = NULL;
979 u64 *snap_sizes = NULL;
Alex Elderccece232012-07-10 20:30:10 -0500980 u32 snap_count;
Alex Elderd2bb24e2012-07-26 23:37:14 -0500981 size_t size;
Alex Elderbb23e372013-05-06 09:51:29 -0500982 int ret = -ENOMEM;
Alex Elder621901d2012-08-23 23:22:06 -0500983 u32 i;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700984
Alex Elderbb23e372013-05-06 09:51:29 -0500985 /* Allocate this now to avoid having to handle failure below */
986
987 if (first_time) {
988 size_t len;
989
990 len = strnlen(ondisk->object_prefix,
991 sizeof (ondisk->object_prefix));
992 object_prefix = kmalloc(len + 1, GFP_KERNEL);
993 if (!object_prefix)
994 return -ENOMEM;
995 memcpy(object_prefix, ondisk->object_prefix, len);
996 object_prefix[len] = '\0';
997 }
998
999 /* Allocate the snapshot context and fill it in */
Alex Elder6a523252012-07-19 17:12:59 -05001000
Alex Elder103a1502012-08-02 11:29:45 -05001001 snap_count = le32_to_cpu(ondisk->snap_count);
Alex Elderbb23e372013-05-06 09:51:29 -05001002 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
1003 if (!snapc)
1004 goto out_err;
1005 snapc->seq = le64_to_cpu(ondisk->snap_seq);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001006 if (snap_count) {
Alex Elderbb23e372013-05-06 09:51:29 -05001007 struct rbd_image_snap_ondisk *snaps;
Alex Elderf785cc12012-08-23 23:22:06 -05001008 u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len);
1009
Alex Elderbb23e372013-05-06 09:51:29 -05001010 /* We'll keep a copy of the snapshot names... */
Alex Elder621901d2012-08-23 23:22:06 -05001011
Alex Elderbb23e372013-05-06 09:51:29 -05001012 if (snap_names_len > (u64)SIZE_MAX)
1013 goto out_2big;
1014 snap_names = kmalloc(snap_names_len, GFP_KERNEL);
1015 if (!snap_names)
Alex Elder6a523252012-07-19 17:12:59 -05001016 goto out_err;
Alex Elderbb23e372013-05-06 09:51:29 -05001017
1018 /* ...as well as the array of their sizes. */
1019
1020 size = snap_count * sizeof (*header->snap_sizes);
1021 snap_sizes = kmalloc(size, GFP_KERNEL);
1022 if (!snap_sizes)
1023 goto out_err;
1024
Alex Elderf785cc12012-08-23 23:22:06 -05001025 /*
Alex Elderbb23e372013-05-06 09:51:29 -05001026 * Copy the names, and fill in each snapshot's id
1027 * and size.
1028 *
Alex Elder99a41eb2013-05-06 09:51:30 -05001029 * Note that rbd_dev_v1_header_info() guarantees the
Alex Elderbb23e372013-05-06 09:51:29 -05001030 * ondisk buffer we're working with has
Alex Elderf785cc12012-08-23 23:22:06 -05001031 * snap_names_len bytes beyond the end of the
1032 * snapshot id array, this memcpy() is safe.
1033 */
Alex Elderbb23e372013-05-06 09:51:29 -05001034 memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len);
1035 snaps = ondisk->snaps;
1036 for (i = 0; i < snap_count; i++) {
1037 snapc->snaps[i] = le64_to_cpu(snaps[i].id);
1038 snap_sizes[i] = le64_to_cpu(snaps[i].image_size);
1039 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001040 }
Alex Elder849b4262012-07-09 21:04:24 -05001041
Alex Elderbb23e372013-05-06 09:51:29 -05001042 /* We won't fail any more, fill in the header */
Alex Elder6a523252012-07-19 17:12:59 -05001043
Alex Elderbb23e372013-05-06 09:51:29 -05001044 if (first_time) {
1045 header->object_prefix = object_prefix;
1046 header->obj_order = ondisk->options.order;
1047 header->crypt_type = ondisk->options.crypt_type;
1048 header->comp_type = ondisk->options.comp_type;
1049 /* The rest aren't used for format 1 images */
1050 header->stripe_unit = 0;
1051 header->stripe_count = 0;
1052 header->features = 0;
Alex Elder662518b2013-05-06 09:51:29 -05001053 } else {
1054 ceph_put_snap_context(header->snapc);
1055 kfree(header->snap_names);
1056 kfree(header->snap_sizes);
Alex Elderbb23e372013-05-06 09:51:29 -05001057 }
1058
1059 /* The remaining fields always get updated (when we refresh) */
Alex Elder621901d2012-08-23 23:22:06 -05001060
Alex Elderf84344f2012-08-31 17:29:51 -05001061 header->image_size = le64_to_cpu(ondisk->image_size);
Alex Elderbb23e372013-05-06 09:51:29 -05001062 header->snapc = snapc;
1063 header->snap_names = snap_names;
1064 header->snap_sizes = snap_sizes;
Alex Elder468521c2013-04-26 09:43:47 -05001065
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001066 return 0;
Alex Elderbb23e372013-05-06 09:51:29 -05001067out_2big:
1068 ret = -EIO;
Alex Elder6a523252012-07-19 17:12:59 -05001069out_err:
Alex Elderbb23e372013-05-06 09:51:29 -05001070 kfree(snap_sizes);
1071 kfree(snap_names);
1072 ceph_put_snap_context(snapc);
1073 kfree(object_prefix);
Alex Elderccece232012-07-10 20:30:10 -05001074
Alex Elderbb23e372013-05-06 09:51:29 -05001075 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001076}
1077
Alex Elder9682fc62013-04-30 00:44:33 -05001078static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which)
1079{
1080 const char *snap_name;
1081
1082 rbd_assert(which < rbd_dev->header.snapc->num_snaps);
1083
1084 /* Skip over names until we find the one we are looking for */
1085
1086 snap_name = rbd_dev->header.snap_names;
1087 while (which--)
1088 snap_name += strlen(snap_name) + 1;
1089
1090 return kstrdup(snap_name, GFP_KERNEL);
1091}
1092
Alex Elder30d1cff2013-05-01 12:43:03 -05001093/*
1094 * Snapshot id comparison function for use with qsort()/bsearch().
1095 * Note that result is for snapshots in *descending* order.
1096 */
1097static int snapid_compare_reverse(const void *s1, const void *s2)
1098{
1099 u64 snap_id1 = *(u64 *)s1;
1100 u64 snap_id2 = *(u64 *)s2;
1101
1102 if (snap_id1 < snap_id2)
1103 return 1;
1104 return snap_id1 == snap_id2 ? 0 : -1;
1105}
1106
1107/*
1108 * Search a snapshot context to see if the given snapshot id is
1109 * present.
1110 *
1111 * Returns the position of the snapshot id in the array if it's found,
1112 * or BAD_SNAP_INDEX otherwise.
1113 *
1114 * Note: The snapshot array is in kept sorted (by the osd) in
1115 * reverse order, highest snapshot id first.
1116 */
Alex Elder9682fc62013-04-30 00:44:33 -05001117static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id)
1118{
1119 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
Alex Elder30d1cff2013-05-01 12:43:03 -05001120 u64 *found;
Alex Elder9682fc62013-04-30 00:44:33 -05001121
Alex Elder30d1cff2013-05-01 12:43:03 -05001122 found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps,
1123 sizeof (snap_id), snapid_compare_reverse);
Alex Elder9682fc62013-04-30 00:44:33 -05001124
Alex Elder30d1cff2013-05-01 12:43:03 -05001125 return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX;
Alex Elder9682fc62013-04-30 00:44:33 -05001126}
1127
Alex Elder2ad3d712013-04-30 00:44:33 -05001128static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev,
1129 u64 snap_id)
Alex Elder54cac612013-04-30 00:44:33 -05001130{
1131 u32 which;
Josh Durginda6a6b62013-09-04 17:57:31 -07001132 const char *snap_name;
Alex Elder54cac612013-04-30 00:44:33 -05001133
1134 which = rbd_dev_snap_index(rbd_dev, snap_id);
1135 if (which == BAD_SNAP_INDEX)
Josh Durginda6a6b62013-09-04 17:57:31 -07001136 return ERR_PTR(-ENOENT);
Alex Elder54cac612013-04-30 00:44:33 -05001137
Josh Durginda6a6b62013-09-04 17:57:31 -07001138 snap_name = _rbd_dev_v1_snap_name(rbd_dev, which);
1139 return snap_name ? snap_name : ERR_PTR(-ENOMEM);
Alex Elder54cac612013-04-30 00:44:33 -05001140}
1141
Alex Elder9e15b772012-10-30 19:40:33 -05001142static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id)
1143{
Alex Elder9e15b772012-10-30 19:40:33 -05001144 if (snap_id == CEPH_NOSNAP)
1145 return RBD_SNAP_HEAD_NAME;
1146
Alex Elder54cac612013-04-30 00:44:33 -05001147 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1148 if (rbd_dev->image_format == 1)
1149 return rbd_dev_v1_snap_name(rbd_dev, snap_id);
Alex Elder9e15b772012-10-30 19:40:33 -05001150
Alex Elder54cac612013-04-30 00:44:33 -05001151 return rbd_dev_v2_snap_name(rbd_dev, snap_id);
Alex Elder9e15b772012-10-30 19:40:33 -05001152}
1153
Alex Elder2ad3d712013-04-30 00:44:33 -05001154static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
1155 u64 *snap_size)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001156{
Alex Elder2ad3d712013-04-30 00:44:33 -05001157 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1158 if (snap_id == CEPH_NOSNAP) {
1159 *snap_size = rbd_dev->header.image_size;
1160 } else if (rbd_dev->image_format == 1) {
1161 u32 which;
Alex Elder00f1f362012-02-07 12:03:36 -06001162
Alex Elder2ad3d712013-04-30 00:44:33 -05001163 which = rbd_dev_snap_index(rbd_dev, snap_id);
1164 if (which == BAD_SNAP_INDEX)
1165 return -ENOENT;
Alex Elder00f1f362012-02-07 12:03:36 -06001166
Alex Elder2ad3d712013-04-30 00:44:33 -05001167 *snap_size = rbd_dev->header.snap_sizes[which];
1168 } else {
1169 u64 size = 0;
1170 int ret;
1171
1172 ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size);
1173 if (ret)
1174 return ret;
1175
1176 *snap_size = size;
1177 }
1178 return 0;
1179}
1180
1181static int rbd_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
1182 u64 *snap_features)
1183{
1184 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1185 if (snap_id == CEPH_NOSNAP) {
1186 *snap_features = rbd_dev->header.features;
1187 } else if (rbd_dev->image_format == 1) {
1188 *snap_features = 0; /* No features for format 1 */
1189 } else {
1190 u64 features = 0;
1191 int ret;
1192
1193 ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, &features);
1194 if (ret)
1195 return ret;
1196
1197 *snap_features = features;
1198 }
1199 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001200}
1201
Alex Elderd1cf5782013-04-27 09:59:30 -05001202static int rbd_dev_mapping_set(struct rbd_device *rbd_dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001203{
Alex Elder8f4b7d92013-05-06 07:40:30 -05001204 u64 snap_id = rbd_dev->spec->snap_id;
Alex Elder2ad3d712013-04-30 00:44:33 -05001205 u64 size = 0;
1206 u64 features = 0;
1207 int ret;
Alex Elder8b0241f2013-04-25 23:15:08 -05001208
Alex Elder2ad3d712013-04-30 00:44:33 -05001209 ret = rbd_snap_size(rbd_dev, snap_id, &size);
1210 if (ret)
1211 return ret;
1212 ret = rbd_snap_features(rbd_dev, snap_id, &features);
1213 if (ret)
1214 return ret;
1215
1216 rbd_dev->mapping.size = size;
1217 rbd_dev->mapping.features = features;
1218
Alex Elder8b0241f2013-04-25 23:15:08 -05001219 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001220}
1221
Alex Elderd1cf5782013-04-27 09:59:30 -05001222static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev)
1223{
1224 rbd_dev->mapping.size = 0;
1225 rbd_dev->mapping.features = 0;
Alex Elder200a6a82013-04-28 23:32:34 -05001226}
1227
Himangi Saraogi7d5079a2014-07-24 03:17:07 +05301228static void rbd_segment_name_free(const char *name)
1229{
1230 /* The explicit cast here is needed to drop the const qualifier */
1231
1232 kmem_cache_free(rbd_segment_name_cache, (void *)name);
1233}
1234
Alex Elder98571b52013-01-20 14:44:42 -06001235static const char *rbd_segment_name(struct rbd_device *rbd_dev, u64 offset)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001236{
Alex Elder65ccfe22012-08-09 10:33:26 -07001237 char *name;
1238 u64 segment;
1239 int ret;
Josh Durgin3a96d5c2013-06-12 19:15:06 -07001240 char *name_format;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001241
Alex Elder78c2a442013-05-01 12:43:04 -05001242 name = kmem_cache_alloc(rbd_segment_name_cache, GFP_NOIO);
Alex Elder65ccfe22012-08-09 10:33:26 -07001243 if (!name)
1244 return NULL;
1245 segment = offset >> rbd_dev->header.obj_order;
Josh Durgin3a96d5c2013-06-12 19:15:06 -07001246 name_format = "%s.%012llx";
1247 if (rbd_dev->image_format == 2)
1248 name_format = "%s.%016llx";
Ilya Dryomov2d0ebc52014-01-27 17:40:18 +02001249 ret = snprintf(name, CEPH_MAX_OID_NAME_LEN + 1, name_format,
Alex Elder65ccfe22012-08-09 10:33:26 -07001250 rbd_dev->header.object_prefix, segment);
Ilya Dryomov2d0ebc52014-01-27 17:40:18 +02001251 if (ret < 0 || ret > CEPH_MAX_OID_NAME_LEN) {
Alex Elder65ccfe22012-08-09 10:33:26 -07001252 pr_err("error formatting segment name for #%llu (%d)\n",
1253 segment, ret);
Himangi Saraogi7d5079a2014-07-24 03:17:07 +05301254 rbd_segment_name_free(name);
Alex Elder65ccfe22012-08-09 10:33:26 -07001255 name = NULL;
1256 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001257
Alex Elder65ccfe22012-08-09 10:33:26 -07001258 return name;
1259}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001260
Alex Elder65ccfe22012-08-09 10:33:26 -07001261static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset)
1262{
1263 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001264
Alex Elder65ccfe22012-08-09 10:33:26 -07001265 return offset & (segment_size - 1);
1266}
1267
1268static u64 rbd_segment_length(struct rbd_device *rbd_dev,
1269 u64 offset, u64 length)
1270{
1271 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
1272
1273 offset &= segment_size - 1;
1274
Alex Elderaafb2302012-09-06 16:00:54 -05001275 rbd_assert(length <= U64_MAX - offset);
Alex Elder65ccfe22012-08-09 10:33:26 -07001276 if (offset + length > segment_size)
1277 length = segment_size - offset;
1278
1279 return length;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001280}
1281
1282/*
Josh Durgin029bcbd2011-07-22 11:35:23 -07001283 * returns the size of an object in the image
1284 */
1285static u64 rbd_obj_bytes(struct rbd_image_header *header)
1286{
1287 return 1 << header->obj_order;
1288}
1289
1290/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001291 * bio helpers
1292 */
1293
1294static void bio_chain_put(struct bio *chain)
1295{
1296 struct bio *tmp;
1297
1298 while (chain) {
1299 tmp = chain;
1300 chain = chain->bi_next;
1301 bio_put(tmp);
1302 }
1303}
1304
1305/*
1306 * zeros a bio chain, starting at specific offset
1307 */
1308static void zero_bio_chain(struct bio *chain, int start_ofs)
1309{
Kent Overstreet79886132013-11-23 17:19:00 -08001310 struct bio_vec bv;
1311 struct bvec_iter iter;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001312 unsigned long flags;
1313 void *buf;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001314 int pos = 0;
1315
1316 while (chain) {
Kent Overstreet79886132013-11-23 17:19:00 -08001317 bio_for_each_segment(bv, chain, iter) {
1318 if (pos + bv.bv_len > start_ofs) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001319 int remainder = max(start_ofs - pos, 0);
Kent Overstreet79886132013-11-23 17:19:00 -08001320 buf = bvec_kmap_irq(&bv, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001321 memset(buf + remainder, 0,
Kent Overstreet79886132013-11-23 17:19:00 -08001322 bv.bv_len - remainder);
1323 flush_dcache_page(bv.bv_page);
Dan Carpenter85b5aaa2010-10-11 21:15:11 +02001324 bvec_kunmap_irq(buf, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001325 }
Kent Overstreet79886132013-11-23 17:19:00 -08001326 pos += bv.bv_len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001327 }
1328
1329 chain = chain->bi_next;
1330 }
1331}
1332
1333/*
Alex Elderb9434c52013-04-19 15:34:50 -05001334 * similar to zero_bio_chain(), zeros data defined by a page array,
1335 * starting at the given byte offset from the start of the array and
1336 * continuing up to the given end offset. The pages array is
1337 * assumed to be big enough to hold all bytes up to the end.
1338 */
1339static void zero_pages(struct page **pages, u64 offset, u64 end)
1340{
1341 struct page **page = &pages[offset >> PAGE_SHIFT];
1342
1343 rbd_assert(end > offset);
1344 rbd_assert(end - offset <= (u64)SIZE_MAX);
1345 while (offset < end) {
1346 size_t page_offset;
1347 size_t length;
1348 unsigned long flags;
1349 void *kaddr;
1350
Geert Uytterhoeven491205a2013-05-13 20:35:37 -05001351 page_offset = offset & ~PAGE_MASK;
1352 length = min_t(size_t, PAGE_SIZE - page_offset, end - offset);
Alex Elderb9434c52013-04-19 15:34:50 -05001353 local_irq_save(flags);
1354 kaddr = kmap_atomic(*page);
1355 memset(kaddr + page_offset, 0, length);
Alex Eldere2156052013-05-22 20:54:25 -05001356 flush_dcache_page(*page);
Alex Elderb9434c52013-04-19 15:34:50 -05001357 kunmap_atomic(kaddr);
1358 local_irq_restore(flags);
1359
1360 offset += length;
1361 page++;
1362 }
1363}
1364
1365/*
Alex Elderf7760da2012-10-20 22:17:27 -05001366 * Clone a portion of a bio, starting at the given byte offset
1367 * and continuing for the number of bytes indicated.
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001368 */
Alex Elderf7760da2012-10-20 22:17:27 -05001369static struct bio *bio_clone_range(struct bio *bio_src,
1370 unsigned int offset,
1371 unsigned int len,
1372 gfp_t gfpmask)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001373{
Alex Elderf7760da2012-10-20 22:17:27 -05001374 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001375
Kent Overstreet5341a622013-08-07 14:31:11 -07001376 bio = bio_clone(bio_src, gfpmask);
Alex Elderf7760da2012-10-20 22:17:27 -05001377 if (!bio)
1378 return NULL; /* ENOMEM */
1379
Kent Overstreet5341a622013-08-07 14:31:11 -07001380 bio_advance(bio, offset);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001381 bio->bi_iter.bi_size = len;
Alex Elder542582f2012-08-09 10:33:25 -07001382
Alex Elderf7760da2012-10-20 22:17:27 -05001383 return bio;
1384}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001385
Alex Elderf7760da2012-10-20 22:17:27 -05001386/*
1387 * Clone a portion of a bio chain, starting at the given byte offset
1388 * into the first bio in the source chain and continuing for the
1389 * number of bytes indicated. The result is another bio chain of
1390 * exactly the given length, or a null pointer on error.
1391 *
1392 * The bio_src and offset parameters are both in-out. On entry they
1393 * refer to the first source bio and the offset into that bio where
1394 * the start of data to be cloned is located.
1395 *
1396 * On return, bio_src is updated to refer to the bio in the source
1397 * chain that contains first un-cloned byte, and *offset will
1398 * contain the offset of that byte within that bio.
1399 */
1400static struct bio *bio_chain_clone_range(struct bio **bio_src,
1401 unsigned int *offset,
1402 unsigned int len,
1403 gfp_t gfpmask)
1404{
1405 struct bio *bi = *bio_src;
1406 unsigned int off = *offset;
1407 struct bio *chain = NULL;
1408 struct bio **end;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001409
Alex Elderf7760da2012-10-20 22:17:27 -05001410 /* Build up a chain of clone bios up to the limit */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001411
Kent Overstreet4f024f32013-10-11 15:44:27 -07001412 if (!bi || off >= bi->bi_iter.bi_size || !len)
Alex Elderf7760da2012-10-20 22:17:27 -05001413 return NULL; /* Nothing to clone */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001414
Alex Elderf7760da2012-10-20 22:17:27 -05001415 end = &chain;
1416 while (len) {
1417 unsigned int bi_size;
1418 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001419
Alex Elderf5400b72012-11-01 10:17:15 -05001420 if (!bi) {
1421 rbd_warn(NULL, "bio_chain exhausted with %u left", len);
Alex Elderf7760da2012-10-20 22:17:27 -05001422 goto out_err; /* EINVAL; ran out of bio's */
Alex Elderf5400b72012-11-01 10:17:15 -05001423 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07001424 bi_size = min_t(unsigned int, bi->bi_iter.bi_size - off, len);
Alex Elderf7760da2012-10-20 22:17:27 -05001425 bio = bio_clone_range(bi, off, bi_size, gfpmask);
1426 if (!bio)
1427 goto out_err; /* ENOMEM */
1428
1429 *end = bio;
1430 end = &bio->bi_next;
1431
1432 off += bi_size;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001433 if (off == bi->bi_iter.bi_size) {
Alex Elderf7760da2012-10-20 22:17:27 -05001434 bi = bi->bi_next;
1435 off = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001436 }
Alex Elderf7760da2012-10-20 22:17:27 -05001437 len -= bi_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001438 }
Alex Elderf7760da2012-10-20 22:17:27 -05001439 *bio_src = bi;
1440 *offset = off;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001441
Alex Elderf7760da2012-10-20 22:17:27 -05001442 return chain;
1443out_err:
1444 bio_chain_put(chain);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001445
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001446 return NULL;
1447}
1448
Alex Elder926f9b32013-02-11 12:33:24 -06001449/*
1450 * The default/initial value for all object request flags is 0. For
1451 * each flag, once its value is set to 1 it is never reset to 0
1452 * again.
1453 */
Alex Elder6365d332013-02-11 12:33:24 -06001454static void obj_request_img_data_set(struct rbd_obj_request *obj_request)
1455{
1456 if (test_and_set_bit(OBJ_REQ_IMG_DATA, &obj_request->flags)) {
Alex Elder6365d332013-02-11 12:33:24 -06001457 struct rbd_device *rbd_dev;
1458
Alex Elder57acbaa2013-02-11 12:33:24 -06001459 rbd_dev = obj_request->img_request->rbd_dev;
Ilya Dryomov9584d502014-07-11 12:11:20 +04001460 rbd_warn(rbd_dev, "obj_request %p already marked img_data",
Alex Elder6365d332013-02-11 12:33:24 -06001461 obj_request);
1462 }
1463}
1464
1465static bool obj_request_img_data_test(struct rbd_obj_request *obj_request)
1466{
1467 smp_mb();
1468 return test_bit(OBJ_REQ_IMG_DATA, &obj_request->flags) != 0;
1469}
1470
Alex Elder57acbaa2013-02-11 12:33:24 -06001471static void obj_request_done_set(struct rbd_obj_request *obj_request)
1472{
1473 if (test_and_set_bit(OBJ_REQ_DONE, &obj_request->flags)) {
1474 struct rbd_device *rbd_dev = NULL;
1475
1476 if (obj_request_img_data_test(obj_request))
1477 rbd_dev = obj_request->img_request->rbd_dev;
Ilya Dryomov9584d502014-07-11 12:11:20 +04001478 rbd_warn(rbd_dev, "obj_request %p already marked done",
Alex Elder57acbaa2013-02-11 12:33:24 -06001479 obj_request);
1480 }
1481}
1482
1483static bool obj_request_done_test(struct rbd_obj_request *obj_request)
1484{
1485 smp_mb();
1486 return test_bit(OBJ_REQ_DONE, &obj_request->flags) != 0;
1487}
1488
Alex Elder5679c592013-02-11 12:33:24 -06001489/*
1490 * This sets the KNOWN flag after (possibly) setting the EXISTS
1491 * flag. The latter is set based on the "exists" value provided.
1492 *
1493 * Note that for our purposes once an object exists it never goes
1494 * away again. It's possible that the response from two existence
1495 * checks are separated by the creation of the target object, and
1496 * the first ("doesn't exist") response arrives *after* the second
1497 * ("does exist"). In that case we ignore the second one.
1498 */
1499static void obj_request_existence_set(struct rbd_obj_request *obj_request,
1500 bool exists)
1501{
1502 if (exists)
1503 set_bit(OBJ_REQ_EXISTS, &obj_request->flags);
1504 set_bit(OBJ_REQ_KNOWN, &obj_request->flags);
1505 smp_mb();
1506}
1507
1508static bool obj_request_known_test(struct rbd_obj_request *obj_request)
1509{
1510 smp_mb();
1511 return test_bit(OBJ_REQ_KNOWN, &obj_request->flags) != 0;
1512}
1513
1514static bool obj_request_exists_test(struct rbd_obj_request *obj_request)
1515{
1516 smp_mb();
1517 return test_bit(OBJ_REQ_EXISTS, &obj_request->flags) != 0;
1518}
1519
Ilya Dryomov96385562014-06-10 13:53:29 +04001520static bool obj_request_overlaps_parent(struct rbd_obj_request *obj_request)
1521{
1522 struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev;
1523
1524 return obj_request->img_offset <
1525 round_up(rbd_dev->parent_overlap, rbd_obj_bytes(&rbd_dev->header));
1526}
1527
Alex Elderbf0d5f502012-11-22 00:00:08 -06001528static void rbd_obj_request_get(struct rbd_obj_request *obj_request)
1529{
Alex Elder37206ee2013-02-20 17:32:08 -06001530 dout("%s: obj %p (was %d)\n", __func__, obj_request,
1531 atomic_read(&obj_request->kref.refcount));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001532 kref_get(&obj_request->kref);
1533}
1534
1535static void rbd_obj_request_destroy(struct kref *kref);
1536static void rbd_obj_request_put(struct rbd_obj_request *obj_request)
1537{
1538 rbd_assert(obj_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001539 dout("%s: obj %p (was %d)\n", __func__, obj_request,
1540 atomic_read(&obj_request->kref.refcount));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001541 kref_put(&obj_request->kref, rbd_obj_request_destroy);
1542}
1543
Alex Elder0f2d5be2014-04-26 14:21:44 +04001544static void rbd_img_request_get(struct rbd_img_request *img_request)
1545{
1546 dout("%s: img %p (was %d)\n", __func__, img_request,
1547 atomic_read(&img_request->kref.refcount));
1548 kref_get(&img_request->kref);
1549}
1550
Alex Eldere93f3152013-05-08 22:50:04 -05001551static bool img_request_child_test(struct rbd_img_request *img_request);
1552static void rbd_parent_request_destroy(struct kref *kref);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001553static void rbd_img_request_destroy(struct kref *kref);
1554static void rbd_img_request_put(struct rbd_img_request *img_request)
1555{
1556 rbd_assert(img_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001557 dout("%s: img %p (was %d)\n", __func__, img_request,
1558 atomic_read(&img_request->kref.refcount));
Alex Eldere93f3152013-05-08 22:50:04 -05001559 if (img_request_child_test(img_request))
1560 kref_put(&img_request->kref, rbd_parent_request_destroy);
1561 else
1562 kref_put(&img_request->kref, rbd_img_request_destroy);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001563}
1564
1565static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request,
1566 struct rbd_obj_request *obj_request)
1567{
Alex Elder25dcf952013-01-25 17:08:55 -06001568 rbd_assert(obj_request->img_request == NULL);
1569
Alex Elderb155e862013-04-15 14:50:37 -05001570 /* Image request now owns object's original reference */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001571 obj_request->img_request = img_request;
Alex Elder25dcf952013-01-25 17:08:55 -06001572 obj_request->which = img_request->obj_request_count;
Alex Elder6365d332013-02-11 12:33:24 -06001573 rbd_assert(!obj_request_img_data_test(obj_request));
1574 obj_request_img_data_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001575 rbd_assert(obj_request->which != BAD_WHICH);
Alex Elder25dcf952013-01-25 17:08:55 -06001576 img_request->obj_request_count++;
1577 list_add_tail(&obj_request->links, &img_request->obj_requests);
Alex Elder37206ee2013-02-20 17:32:08 -06001578 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1579 obj_request->which);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001580}
1581
1582static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request,
1583 struct rbd_obj_request *obj_request)
1584{
1585 rbd_assert(obj_request->which != BAD_WHICH);
Alex Elder25dcf952013-01-25 17:08:55 -06001586
Alex Elder37206ee2013-02-20 17:32:08 -06001587 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1588 obj_request->which);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001589 list_del(&obj_request->links);
Alex Elder25dcf952013-01-25 17:08:55 -06001590 rbd_assert(img_request->obj_request_count > 0);
1591 img_request->obj_request_count--;
1592 rbd_assert(obj_request->which == img_request->obj_request_count);
1593 obj_request->which = BAD_WHICH;
Alex Elder6365d332013-02-11 12:33:24 -06001594 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001595 rbd_assert(obj_request->img_request == img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001596 obj_request->img_request = NULL;
Alex Elder25dcf952013-01-25 17:08:55 -06001597 obj_request->callback = NULL;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001598 rbd_obj_request_put(obj_request);
1599}
1600
1601static bool obj_request_type_valid(enum obj_request_type type)
1602{
1603 switch (type) {
Alex Elder9969ebc2013-01-18 12:31:10 -06001604 case OBJ_REQUEST_NODATA:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001605 case OBJ_REQUEST_BIO:
Alex Elder788e2df2013-01-17 12:25:27 -06001606 case OBJ_REQUEST_PAGES:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001607 return true;
1608 default:
1609 return false;
1610 }
1611}
1612
Alex Elderbf0d5f502012-11-22 00:00:08 -06001613static int rbd_obj_request_submit(struct ceph_osd_client *osdc,
1614 struct rbd_obj_request *obj_request)
1615{
Ilya Dryomov71c20a02014-06-19 11:38:14 +04001616 dout("%s %p\n", __func__, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001617 return ceph_osdc_start_request(osdc, obj_request->osd_req, false);
1618}
1619
Ilya Dryomov71c20a02014-06-19 11:38:14 +04001620static void rbd_obj_request_end(struct rbd_obj_request *obj_request)
1621{
1622 dout("%s %p\n", __func__, obj_request);
1623 ceph_osdc_cancel_request(obj_request->osd_req);
1624}
1625
1626/*
1627 * Wait for an object request to complete. If interrupted, cancel the
1628 * underlying osd request.
Ilya Dryomov2894e1d2015-05-12 19:53:24 +03001629 *
1630 * @timeout: in jiffies, 0 means "wait forever"
Ilya Dryomov71c20a02014-06-19 11:38:14 +04001631 */
Ilya Dryomov2894e1d2015-05-12 19:53:24 +03001632static int __rbd_obj_request_wait(struct rbd_obj_request *obj_request,
1633 unsigned long timeout)
Ilya Dryomov71c20a02014-06-19 11:38:14 +04001634{
Ilya Dryomov2894e1d2015-05-12 19:53:24 +03001635 long ret;
Ilya Dryomov71c20a02014-06-19 11:38:14 +04001636
1637 dout("%s %p\n", __func__, obj_request);
Ilya Dryomov2894e1d2015-05-12 19:53:24 +03001638 ret = wait_for_completion_interruptible_timeout(
1639 &obj_request->completion,
1640 ceph_timeout_jiffies(timeout));
1641 if (ret <= 0) {
1642 if (ret == 0)
1643 ret = -ETIMEDOUT;
Ilya Dryomov71c20a02014-06-19 11:38:14 +04001644 rbd_obj_request_end(obj_request);
Ilya Dryomov2894e1d2015-05-12 19:53:24 +03001645 } else {
1646 ret = 0;
Ilya Dryomov71c20a02014-06-19 11:38:14 +04001647 }
1648
Ilya Dryomov2894e1d2015-05-12 19:53:24 +03001649 dout("%s %p ret %d\n", __func__, obj_request, (int)ret);
1650 return ret;
1651}
1652
1653static int rbd_obj_request_wait(struct rbd_obj_request *obj_request)
1654{
1655 return __rbd_obj_request_wait(obj_request, 0);
1656}
1657
Alex Elderbf0d5f502012-11-22 00:00:08 -06001658static void rbd_img_request_complete(struct rbd_img_request *img_request)
1659{
Alex Elder55f27e02013-04-10 12:34:25 -05001660
Alex Elder37206ee2013-02-20 17:32:08 -06001661 dout("%s: img %p\n", __func__, img_request);
Alex Elder55f27e02013-04-10 12:34:25 -05001662
1663 /*
1664 * If no error occurred, compute the aggregate transfer
1665 * count for the image request. We could instead use
1666 * atomic64_cmpxchg() to update it as each object request
1667 * completes; not clear which way is better off hand.
1668 */
1669 if (!img_request->result) {
1670 struct rbd_obj_request *obj_request;
1671 u64 xferred = 0;
1672
1673 for_each_obj_request(img_request, obj_request)
1674 xferred += obj_request->xferred;
1675 img_request->xferred = xferred;
1676 }
1677
Alex Elderbf0d5f502012-11-22 00:00:08 -06001678 if (img_request->callback)
1679 img_request->callback(img_request);
1680 else
1681 rbd_img_request_put(img_request);
1682}
1683
Alex Elder0c425242013-02-08 09:55:49 -06001684/*
1685 * The default/initial value for all image request flags is 0. Each
1686 * is conditionally set to 1 at image request initialization time
1687 * and currently never change thereafter.
1688 */
1689static void img_request_write_set(struct rbd_img_request *img_request)
1690{
1691 set_bit(IMG_REQ_WRITE, &img_request->flags);
1692 smp_mb();
1693}
1694
1695static bool img_request_write_test(struct rbd_img_request *img_request)
1696{
1697 smp_mb();
1698 return test_bit(IMG_REQ_WRITE, &img_request->flags) != 0;
1699}
1700
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001701/*
1702 * Set the discard flag when the img_request is an discard request
1703 */
1704static void img_request_discard_set(struct rbd_img_request *img_request)
1705{
1706 set_bit(IMG_REQ_DISCARD, &img_request->flags);
1707 smp_mb();
1708}
1709
1710static bool img_request_discard_test(struct rbd_img_request *img_request)
1711{
1712 smp_mb();
1713 return test_bit(IMG_REQ_DISCARD, &img_request->flags) != 0;
1714}
1715
Alex Elder9849e982013-01-24 16:13:36 -06001716static void img_request_child_set(struct rbd_img_request *img_request)
1717{
1718 set_bit(IMG_REQ_CHILD, &img_request->flags);
1719 smp_mb();
1720}
1721
Alex Eldere93f3152013-05-08 22:50:04 -05001722static void img_request_child_clear(struct rbd_img_request *img_request)
1723{
1724 clear_bit(IMG_REQ_CHILD, &img_request->flags);
1725 smp_mb();
1726}
1727
Alex Elder9849e982013-01-24 16:13:36 -06001728static bool img_request_child_test(struct rbd_img_request *img_request)
1729{
1730 smp_mb();
1731 return test_bit(IMG_REQ_CHILD, &img_request->flags) != 0;
1732}
1733
Alex Elderd0b2e942013-01-24 16:13:36 -06001734static void img_request_layered_set(struct rbd_img_request *img_request)
1735{
1736 set_bit(IMG_REQ_LAYERED, &img_request->flags);
1737 smp_mb();
1738}
1739
Alex Eldera2acd002013-05-08 22:50:04 -05001740static void img_request_layered_clear(struct rbd_img_request *img_request)
1741{
1742 clear_bit(IMG_REQ_LAYERED, &img_request->flags);
1743 smp_mb();
1744}
1745
Alex Elderd0b2e942013-01-24 16:13:36 -06001746static bool img_request_layered_test(struct rbd_img_request *img_request)
1747{
1748 smp_mb();
1749 return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0;
1750}
1751
Josh Durgin3b434a2a2014-04-04 17:32:15 -07001752static enum obj_operation_type
1753rbd_img_request_op_type(struct rbd_img_request *img_request)
1754{
1755 if (img_request_write_test(img_request))
1756 return OBJ_OP_WRITE;
1757 else if (img_request_discard_test(img_request))
1758 return OBJ_OP_DISCARD;
1759 else
1760 return OBJ_OP_READ;
1761}
1762
Alex Elder6e2a4502013-03-27 09:16:30 -05001763static void
1764rbd_img_obj_request_read_callback(struct rbd_obj_request *obj_request)
1765{
Alex Elderb9434c52013-04-19 15:34:50 -05001766 u64 xferred = obj_request->xferred;
1767 u64 length = obj_request->length;
1768
Alex Elder6e2a4502013-03-27 09:16:30 -05001769 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1770 obj_request, obj_request->img_request, obj_request->result,
Alex Elderb9434c52013-04-19 15:34:50 -05001771 xferred, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001772 /*
Josh Durgin17c1cc12013-08-26 17:55:38 -07001773 * ENOENT means a hole in the image. We zero-fill the entire
1774 * length of the request. A short read also implies zero-fill
1775 * to the end of the request. An error requires the whole
1776 * length of the request to be reported finished with an error
1777 * to the block layer. In each case we update the xferred
1778 * count to indicate the whole request was satisfied.
Alex Elder6e2a4502013-03-27 09:16:30 -05001779 */
Alex Elderb9434c52013-04-19 15:34:50 -05001780 rbd_assert(obj_request->type != OBJ_REQUEST_NODATA);
Alex Elder6e2a4502013-03-27 09:16:30 -05001781 if (obj_request->result == -ENOENT) {
Alex Elderb9434c52013-04-19 15:34:50 -05001782 if (obj_request->type == OBJ_REQUEST_BIO)
1783 zero_bio_chain(obj_request->bio_list, 0);
1784 else
1785 zero_pages(obj_request->pages, 0, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001786 obj_request->result = 0;
Alex Elderb9434c52013-04-19 15:34:50 -05001787 } else if (xferred < length && !obj_request->result) {
1788 if (obj_request->type == OBJ_REQUEST_BIO)
1789 zero_bio_chain(obj_request->bio_list, xferred);
1790 else
1791 zero_pages(obj_request->pages, xferred, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001792 }
Josh Durgin17c1cc12013-08-26 17:55:38 -07001793 obj_request->xferred = length;
Alex Elder6e2a4502013-03-27 09:16:30 -05001794 obj_request_done_set(obj_request);
1795}
1796
Alex Elderbf0d5f502012-11-22 00:00:08 -06001797static void rbd_obj_request_complete(struct rbd_obj_request *obj_request)
1798{
Alex Elder37206ee2013-02-20 17:32:08 -06001799 dout("%s: obj %p cb %p\n", __func__, obj_request,
1800 obj_request->callback);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001801 if (obj_request->callback)
1802 obj_request->callback(obj_request);
Alex Elder788e2df2013-01-17 12:25:27 -06001803 else
1804 complete_all(&obj_request->completion);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001805}
1806
Alex Elderc47f9372013-02-26 14:23:07 -06001807static void rbd_osd_read_callback(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001808{
Alex Elder57acbaa2013-02-11 12:33:24 -06001809 struct rbd_img_request *img_request = NULL;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001810 struct rbd_device *rbd_dev = NULL;
Alex Elder57acbaa2013-02-11 12:33:24 -06001811 bool layered = false;
1812
1813 if (obj_request_img_data_test(obj_request)) {
1814 img_request = obj_request->img_request;
1815 layered = img_request && img_request_layered_test(img_request);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001816 rbd_dev = img_request->rbd_dev;
Alex Elder57acbaa2013-02-11 12:33:24 -06001817 }
Alex Elder8b3e1a52013-01-24 16:13:36 -06001818
1819 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1820 obj_request, img_request, obj_request->result,
1821 obj_request->xferred, obj_request->length);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001822 if (layered && obj_request->result == -ENOENT &&
1823 obj_request->img_offset < rbd_dev->parent_overlap)
Alex Elder8b3e1a52013-01-24 16:13:36 -06001824 rbd_img_parent_read(obj_request);
1825 else if (img_request)
Alex Elder6e2a4502013-03-27 09:16:30 -05001826 rbd_img_obj_request_read_callback(obj_request);
1827 else
1828 obj_request_done_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001829}
1830
Alex Elderc47f9372013-02-26 14:23:07 -06001831static void rbd_osd_write_callback(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001832{
Sage Weil1b83bef2013-02-25 16:11:12 -08001833 dout("%s: obj %p result %d %llu\n", __func__, obj_request,
1834 obj_request->result, obj_request->length);
1835 /*
Alex Elder8b3e1a52013-01-24 16:13:36 -06001836 * There is no such thing as a successful short write. Set
1837 * it to our originally-requested length.
Sage Weil1b83bef2013-02-25 16:11:12 -08001838 */
1839 obj_request->xferred = obj_request->length;
Alex Elder07741302013-02-05 23:41:50 -06001840 obj_request_done_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001841}
1842
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001843static void rbd_osd_discard_callback(struct rbd_obj_request *obj_request)
1844{
1845 dout("%s: obj %p result %d %llu\n", __func__, obj_request,
1846 obj_request->result, obj_request->length);
1847 /*
1848 * There is no such thing as a successful short discard. Set
1849 * it to our originally-requested length.
1850 */
1851 obj_request->xferred = obj_request->length;
Josh Durgind0265de2014-04-07 16:54:10 -07001852 /* discarding a non-existent object is not a problem */
1853 if (obj_request->result == -ENOENT)
1854 obj_request->result = 0;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001855 obj_request_done_set(obj_request);
1856}
1857
Alex Elderfbfab532013-02-08 09:55:48 -06001858/*
1859 * For a simple stat call there's nothing to do. We'll do more if
1860 * this is part of a write sequence for a layered image.
1861 */
Alex Elderc47f9372013-02-26 14:23:07 -06001862static void rbd_osd_stat_callback(struct rbd_obj_request *obj_request)
Alex Elderfbfab532013-02-08 09:55:48 -06001863{
Alex Elder37206ee2013-02-20 17:32:08 -06001864 dout("%s: obj %p\n", __func__, obj_request);
Alex Elderfbfab532013-02-08 09:55:48 -06001865 obj_request_done_set(obj_request);
1866}
1867
Ilya Dryomov27617132015-07-16 17:36:11 +03001868static void rbd_osd_call_callback(struct rbd_obj_request *obj_request)
1869{
1870 dout("%s: obj %p\n", __func__, obj_request);
1871
1872 if (obj_request_img_data_test(obj_request))
1873 rbd_osd_copyup_callback(obj_request);
1874 else
1875 obj_request_done_set(obj_request);
1876}
1877
Ilya Dryomov85e084f2016-04-28 16:07:24 +02001878static void rbd_osd_req_callback(struct ceph_osd_request *osd_req)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001879{
1880 struct rbd_obj_request *obj_request = osd_req->r_priv;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001881 u16 opcode;
1882
Ilya Dryomov85e084f2016-04-28 16:07:24 +02001883 dout("%s: osd_req %p\n", __func__, osd_req);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001884 rbd_assert(osd_req == obj_request->osd_req);
Alex Elder57acbaa2013-02-11 12:33:24 -06001885 if (obj_request_img_data_test(obj_request)) {
1886 rbd_assert(obj_request->img_request);
1887 rbd_assert(obj_request->which != BAD_WHICH);
1888 } else {
1889 rbd_assert(obj_request->which == BAD_WHICH);
1890 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06001891
Sage Weil1b83bef2013-02-25 16:11:12 -08001892 if (osd_req->r_result < 0)
1893 obj_request->result = osd_req->r_result;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001894
Alex Elderc47f9372013-02-26 14:23:07 -06001895 /*
1896 * We support a 64-bit length, but ultimately it has to be
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01001897 * passed to the block layer, which just supports a 32-bit
1898 * length field.
Alex Elderc47f9372013-02-26 14:23:07 -06001899 */
Yan, Zheng7665d852016-01-07 16:48:57 +08001900 obj_request->xferred = osd_req->r_ops[0].outdata_len;
Alex Elder8b3e1a52013-01-24 16:13:36 -06001901 rbd_assert(obj_request->xferred < (u64)UINT_MAX);
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001902
Alex Elder79528732013-04-03 21:32:51 -05001903 opcode = osd_req->r_ops[0].op;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001904 switch (opcode) {
1905 case CEPH_OSD_OP_READ:
Alex Elderc47f9372013-02-26 14:23:07 -06001906 rbd_osd_read_callback(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001907 break;
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001908 case CEPH_OSD_OP_SETALLOCHINT:
Ilya Dryomove30b7572015-10-07 17:27:17 +02001909 rbd_assert(osd_req->r_ops[1].op == CEPH_OSD_OP_WRITE ||
1910 osd_req->r_ops[1].op == CEPH_OSD_OP_WRITEFULL);
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001911 /* fall through */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001912 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +02001913 case CEPH_OSD_OP_WRITEFULL:
Alex Elderc47f9372013-02-26 14:23:07 -06001914 rbd_osd_write_callback(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001915 break;
Alex Elderfbfab532013-02-08 09:55:48 -06001916 case CEPH_OSD_OP_STAT:
Alex Elderc47f9372013-02-26 14:23:07 -06001917 rbd_osd_stat_callback(obj_request);
Alex Elderfbfab532013-02-08 09:55:48 -06001918 break;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001919 case CEPH_OSD_OP_DELETE:
1920 case CEPH_OSD_OP_TRUNCATE:
1921 case CEPH_OSD_OP_ZERO:
1922 rbd_osd_discard_callback(obj_request);
1923 break;
Alex Elder36be9a72013-01-19 00:30:28 -06001924 case CEPH_OSD_OP_CALL:
Ilya Dryomov27617132015-07-16 17:36:11 +03001925 rbd_osd_call_callback(obj_request);
1926 break;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001927 default:
Ilya Dryomov9584d502014-07-11 12:11:20 +04001928 rbd_warn(NULL, "%s: unsupported op %hu",
Alex Elderbf0d5f502012-11-22 00:00:08 -06001929 obj_request->object_name, (unsigned short) opcode);
1930 break;
1931 }
1932
Alex Elder07741302013-02-05 23:41:50 -06001933 if (obj_request_done_test(obj_request))
Alex Elderbf0d5f502012-11-22 00:00:08 -06001934 rbd_obj_request_complete(obj_request);
1935}
1936
Alex Elder9d4df012013-04-19 15:34:50 -05001937static void rbd_osd_req_format_read(struct rbd_obj_request *obj_request)
Alex Elder430c28c2013-04-03 21:32:51 -05001938{
1939 struct rbd_img_request *img_request = obj_request->img_request;
Alex Elder8c042b02013-04-03 01:28:58 -05001940 struct ceph_osd_request *osd_req = obj_request->osd_req;
Alex Elder430c28c2013-04-03 21:32:51 -05001941
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001942 if (img_request)
1943 osd_req->r_snapid = img_request->snap_id;
Alex Elder9d4df012013-04-19 15:34:50 -05001944}
1945
1946static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request)
1947{
Alex Elder9d4df012013-04-19 15:34:50 -05001948 struct ceph_osd_request *osd_req = obj_request->osd_req;
Alex Elder9d4df012013-04-19 15:34:50 -05001949
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001950 osd_req->r_mtime = CURRENT_TIME;
1951 osd_req->r_data_offset = obj_request->offset;
Alex Elder430c28c2013-04-03 21:32:51 -05001952}
1953
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001954/*
1955 * Create an osd request. A read request has one osd op (read).
1956 * A write request has either one (watch) or two (hint+write) osd ops.
1957 * (All rbd data writes are prefixed with an allocation hint op, but
1958 * technically osd watch is a write request, hence this distinction.)
1959 */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001960static struct ceph_osd_request *rbd_osd_req_create(
1961 struct rbd_device *rbd_dev,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08001962 enum obj_operation_type op_type,
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02001963 unsigned int num_ops,
Alex Elder430c28c2013-04-03 21:32:51 -05001964 struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001965{
Alex Elderbf0d5f502012-11-22 00:00:08 -06001966 struct ceph_snap_context *snapc = NULL;
1967 struct ceph_osd_client *osdc;
1968 struct ceph_osd_request *osd_req;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001969
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001970 if (obj_request_img_data_test(obj_request) &&
1971 (op_type == OBJ_OP_DISCARD || op_type == OBJ_OP_WRITE)) {
Alex Elder6365d332013-02-11 12:33:24 -06001972 struct rbd_img_request *img_request = obj_request->img_request;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001973 if (op_type == OBJ_OP_WRITE) {
1974 rbd_assert(img_request_write_test(img_request));
1975 } else {
1976 rbd_assert(img_request_discard_test(img_request));
1977 }
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08001978 snapc = img_request->snapc;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001979 }
1980
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08001981 rbd_assert(num_ops == 1 || ((op_type == OBJ_OP_WRITE) && num_ops == 2));
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02001982
1983 /* Allocate and initialize the request, for the num_ops ops */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001984
1985 osdc = &rbd_dev->rbd_client->client->osdc;
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02001986 osd_req = ceph_osdc_alloc_request(osdc, snapc, num_ops, false,
David Disseldorp2224d872016-04-05 11:13:39 +02001987 GFP_NOIO);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001988 if (!osd_req)
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02001989 goto fail;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001990
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001991 if (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001992 osd_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK;
Alex Elder430c28c2013-04-03 21:32:51 -05001993 else
Alex Elderbf0d5f502012-11-22 00:00:08 -06001994 osd_req->r_flags = CEPH_OSD_FLAG_READ;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001995
1996 osd_req->r_callback = rbd_osd_req_callback;
1997 osd_req->r_priv = obj_request;
1998
Yan, Zheng76271512016-02-03 21:24:49 +08001999 osd_req->r_base_oloc.pool = rbd_dev->layout.pool_id;
Ilya Dryomovd30291b2016-04-29 19:54:20 +02002000 if (ceph_oid_aprintf(&osd_req->r_base_oid, GFP_NOIO, "%s",
2001 obj_request->object_name))
2002 goto fail;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002003
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02002004 if (ceph_osdc_alloc_messages(osd_req, GFP_NOIO))
2005 goto fail;
2006
Alex Elderbf0d5f502012-11-22 00:00:08 -06002007 return osd_req;
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02002008
2009fail:
2010 ceph_osdc_put_request(osd_req);
2011 return NULL;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002012}
2013
Alex Elder0eefd472013-04-19 15:34:50 -05002014/*
Josh Durgind3246fb2014-04-07 16:49:21 -07002015 * Create a copyup osd request based on the information in the object
2016 * request supplied. A copyup request has two or three osd ops, a
2017 * copyup method call, potentially a hint op, and a write or truncate
2018 * or zero op.
Alex Elder0eefd472013-04-19 15:34:50 -05002019 */
2020static struct ceph_osd_request *
2021rbd_osd_req_create_copyup(struct rbd_obj_request *obj_request)
2022{
2023 struct rbd_img_request *img_request;
2024 struct ceph_snap_context *snapc;
2025 struct rbd_device *rbd_dev;
2026 struct ceph_osd_client *osdc;
2027 struct ceph_osd_request *osd_req;
Josh Durgind3246fb2014-04-07 16:49:21 -07002028 int num_osd_ops = 3;
Alex Elder0eefd472013-04-19 15:34:50 -05002029
2030 rbd_assert(obj_request_img_data_test(obj_request));
2031 img_request = obj_request->img_request;
2032 rbd_assert(img_request);
Josh Durgind3246fb2014-04-07 16:49:21 -07002033 rbd_assert(img_request_write_test(img_request) ||
2034 img_request_discard_test(img_request));
Alex Elder0eefd472013-04-19 15:34:50 -05002035
Josh Durgind3246fb2014-04-07 16:49:21 -07002036 if (img_request_discard_test(img_request))
2037 num_osd_ops = 2;
2038
2039 /* Allocate and initialize the request, for all the ops */
Alex Elder0eefd472013-04-19 15:34:50 -05002040
2041 snapc = img_request->snapc;
2042 rbd_dev = img_request->rbd_dev;
2043 osdc = &rbd_dev->rbd_client->client->osdc;
Josh Durgind3246fb2014-04-07 16:49:21 -07002044 osd_req = ceph_osdc_alloc_request(osdc, snapc, num_osd_ops,
David Disseldorp2224d872016-04-05 11:13:39 +02002045 false, GFP_NOIO);
Alex Elder0eefd472013-04-19 15:34:50 -05002046 if (!osd_req)
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02002047 goto fail;
Alex Elder0eefd472013-04-19 15:34:50 -05002048
2049 osd_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK;
2050 osd_req->r_callback = rbd_osd_req_callback;
2051 osd_req->r_priv = obj_request;
2052
Yan, Zheng76271512016-02-03 21:24:49 +08002053 osd_req->r_base_oloc.pool = rbd_dev->layout.pool_id;
Ilya Dryomovd30291b2016-04-29 19:54:20 +02002054 if (ceph_oid_aprintf(&osd_req->r_base_oid, GFP_NOIO, "%s",
2055 obj_request->object_name))
2056 goto fail;
Alex Elder0eefd472013-04-19 15:34:50 -05002057
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02002058 if (ceph_osdc_alloc_messages(osd_req, GFP_NOIO))
2059 goto fail;
2060
Alex Elder0eefd472013-04-19 15:34:50 -05002061 return osd_req;
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02002062
2063fail:
2064 ceph_osdc_put_request(osd_req);
2065 return NULL;
Alex Elder0eefd472013-04-19 15:34:50 -05002066}
2067
2068
Alex Elderbf0d5f502012-11-22 00:00:08 -06002069static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req)
2070{
2071 ceph_osdc_put_request(osd_req);
2072}
2073
2074/* object_name is assumed to be a non-null pointer and NUL-terminated */
2075
2076static struct rbd_obj_request *rbd_obj_request_create(const char *object_name,
2077 u64 offset, u64 length,
2078 enum obj_request_type type)
2079{
2080 struct rbd_obj_request *obj_request;
2081 size_t size;
2082 char *name;
2083
2084 rbd_assert(obj_request_type_valid(type));
2085
2086 size = strlen(object_name) + 1;
Ilya Dryomov5a60e872015-06-24 17:24:33 +03002087 name = kmalloc(size, GFP_NOIO);
Alex Elderf907ad52013-05-01 12:43:03 -05002088 if (!name)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002089 return NULL;
2090
Ilya Dryomov5a60e872015-06-24 17:24:33 +03002091 obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_NOIO);
Alex Elderf907ad52013-05-01 12:43:03 -05002092 if (!obj_request) {
2093 kfree(name);
2094 return NULL;
2095 }
2096
Alex Elderbf0d5f502012-11-22 00:00:08 -06002097 obj_request->object_name = memcpy(name, object_name, size);
2098 obj_request->offset = offset;
2099 obj_request->length = length;
Alex Elder926f9b32013-02-11 12:33:24 -06002100 obj_request->flags = 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002101 obj_request->which = BAD_WHICH;
2102 obj_request->type = type;
2103 INIT_LIST_HEAD(&obj_request->links);
Alex Elder788e2df2013-01-17 12:25:27 -06002104 init_completion(&obj_request->completion);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002105 kref_init(&obj_request->kref);
2106
Alex Elder37206ee2013-02-20 17:32:08 -06002107 dout("%s: \"%s\" %llu/%llu %d -> obj %p\n", __func__, object_name,
2108 offset, length, (int)type, obj_request);
2109
Alex Elderbf0d5f502012-11-22 00:00:08 -06002110 return obj_request;
2111}
2112
2113static void rbd_obj_request_destroy(struct kref *kref)
2114{
2115 struct rbd_obj_request *obj_request;
2116
2117 obj_request = container_of(kref, struct rbd_obj_request, kref);
2118
Alex Elder37206ee2013-02-20 17:32:08 -06002119 dout("%s: obj %p\n", __func__, obj_request);
2120
Alex Elderbf0d5f502012-11-22 00:00:08 -06002121 rbd_assert(obj_request->img_request == NULL);
2122 rbd_assert(obj_request->which == BAD_WHICH);
2123
2124 if (obj_request->osd_req)
2125 rbd_osd_req_destroy(obj_request->osd_req);
2126
2127 rbd_assert(obj_request_type_valid(obj_request->type));
2128 switch (obj_request->type) {
Alex Elder9969ebc2013-01-18 12:31:10 -06002129 case OBJ_REQUEST_NODATA:
2130 break; /* Nothing to do */
Alex Elderbf0d5f502012-11-22 00:00:08 -06002131 case OBJ_REQUEST_BIO:
2132 if (obj_request->bio_list)
2133 bio_chain_put(obj_request->bio_list);
2134 break;
Alex Elder788e2df2013-01-17 12:25:27 -06002135 case OBJ_REQUEST_PAGES:
2136 if (obj_request->pages)
2137 ceph_release_page_vector(obj_request->pages,
2138 obj_request->page_count);
2139 break;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002140 }
2141
Alex Elderf907ad52013-05-01 12:43:03 -05002142 kfree(obj_request->object_name);
Alex Elder868311b2013-05-01 12:43:03 -05002143 obj_request->object_name = NULL;
2144 kmem_cache_free(rbd_obj_request_cache, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002145}
2146
Alex Elderfb65d2282013-05-08 22:50:04 -05002147/* It's OK to call this for a device with no parent */
2148
2149static void rbd_spec_put(struct rbd_spec *spec);
2150static void rbd_dev_unparent(struct rbd_device *rbd_dev)
2151{
2152 rbd_dev_remove_parent(rbd_dev);
2153 rbd_spec_put(rbd_dev->parent_spec);
2154 rbd_dev->parent_spec = NULL;
2155 rbd_dev->parent_overlap = 0;
2156}
2157
Alex Elderbf0d5f502012-11-22 00:00:08 -06002158/*
Alex Eldera2acd002013-05-08 22:50:04 -05002159 * Parent image reference counting is used to determine when an
2160 * image's parent fields can be safely torn down--after there are no
2161 * more in-flight requests to the parent image. When the last
2162 * reference is dropped, cleaning them up is safe.
2163 */
2164static void rbd_dev_parent_put(struct rbd_device *rbd_dev)
2165{
2166 int counter;
2167
2168 if (!rbd_dev->parent_spec)
2169 return;
2170
2171 counter = atomic_dec_return_safe(&rbd_dev->parent_ref);
2172 if (counter > 0)
2173 return;
2174
2175 /* Last reference; clean up parent data structures */
2176
2177 if (!counter)
2178 rbd_dev_unparent(rbd_dev);
2179 else
Ilya Dryomov9584d502014-07-11 12:11:20 +04002180 rbd_warn(rbd_dev, "parent reference underflow");
Alex Eldera2acd002013-05-08 22:50:04 -05002181}
2182
2183/*
2184 * If an image has a non-zero parent overlap, get a reference to its
2185 * parent.
2186 *
2187 * Returns true if the rbd device has a parent with a non-zero
2188 * overlap and a reference for it was successfully taken, or
2189 * false otherwise.
2190 */
2191static bool rbd_dev_parent_get(struct rbd_device *rbd_dev)
2192{
Ilya Dryomovae43e9d2015-01-19 18:13:43 +03002193 int counter = 0;
Alex Eldera2acd002013-05-08 22:50:04 -05002194
2195 if (!rbd_dev->parent_spec)
2196 return false;
2197
Ilya Dryomovae43e9d2015-01-19 18:13:43 +03002198 down_read(&rbd_dev->header_rwsem);
2199 if (rbd_dev->parent_overlap)
2200 counter = atomic_inc_return_safe(&rbd_dev->parent_ref);
2201 up_read(&rbd_dev->header_rwsem);
Alex Eldera2acd002013-05-08 22:50:04 -05002202
2203 if (counter < 0)
Ilya Dryomov9584d502014-07-11 12:11:20 +04002204 rbd_warn(rbd_dev, "parent reference overflow");
Alex Eldera2acd002013-05-08 22:50:04 -05002205
Ilya Dryomovae43e9d2015-01-19 18:13:43 +03002206 return counter > 0;
Alex Eldera2acd002013-05-08 22:50:04 -05002207}
2208
Alex Elderbf0d5f502012-11-22 00:00:08 -06002209/*
2210 * Caller is responsible for filling in the list of object requests
2211 * that comprises the image request, and the Linux request pointer
2212 * (if there is one).
2213 */
Alex Eldercc344fa2013-02-19 12:25:56 -06002214static struct rbd_img_request *rbd_img_request_create(
2215 struct rbd_device *rbd_dev,
Alex Elderbf0d5f502012-11-22 00:00:08 -06002216 u64 offset, u64 length,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002217 enum obj_operation_type op_type,
Josh Durgin4e752f02014-04-08 11:12:11 -07002218 struct ceph_snap_context *snapc)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002219{
2220 struct rbd_img_request *img_request;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002221
Ilya Dryomov7a716aa2014-08-05 11:25:54 +04002222 img_request = kmem_cache_alloc(rbd_img_request_cache, GFP_NOIO);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002223 if (!img_request)
2224 return NULL;
2225
Alex Elderbf0d5f502012-11-22 00:00:08 -06002226 img_request->rq = NULL;
2227 img_request->rbd_dev = rbd_dev;
2228 img_request->offset = offset;
2229 img_request->length = length;
Alex Elder0c425242013-02-08 09:55:49 -06002230 img_request->flags = 0;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002231 if (op_type == OBJ_OP_DISCARD) {
2232 img_request_discard_set(img_request);
2233 img_request->snapc = snapc;
2234 } else if (op_type == OBJ_OP_WRITE) {
Alex Elder0c425242013-02-08 09:55:49 -06002235 img_request_write_set(img_request);
Josh Durgin4e752f02014-04-08 11:12:11 -07002236 img_request->snapc = snapc;
Alex Elder0c425242013-02-08 09:55:49 -06002237 } else {
Alex Elderbf0d5f502012-11-22 00:00:08 -06002238 img_request->snap_id = rbd_dev->spec->snap_id;
Alex Elder0c425242013-02-08 09:55:49 -06002239 }
Alex Eldera2acd002013-05-08 22:50:04 -05002240 if (rbd_dev_parent_get(rbd_dev))
Alex Elderd0b2e942013-01-24 16:13:36 -06002241 img_request_layered_set(img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002242 spin_lock_init(&img_request->completion_lock);
2243 img_request->next_completion = 0;
2244 img_request->callback = NULL;
Alex Eldera5a337d2013-01-24 16:13:36 -06002245 img_request->result = 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002246 img_request->obj_request_count = 0;
2247 INIT_LIST_HEAD(&img_request->obj_requests);
2248 kref_init(&img_request->kref);
2249
Alex Elder37206ee2013-02-20 17:32:08 -06002250 dout("%s: rbd_dev %p %s %llu/%llu -> img %p\n", __func__, rbd_dev,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002251 obj_op_name(op_type), offset, length, img_request);
Alex Elder37206ee2013-02-20 17:32:08 -06002252
Alex Elderbf0d5f502012-11-22 00:00:08 -06002253 return img_request;
2254}
2255
2256static void rbd_img_request_destroy(struct kref *kref)
2257{
2258 struct rbd_img_request *img_request;
2259 struct rbd_obj_request *obj_request;
2260 struct rbd_obj_request *next_obj_request;
2261
2262 img_request = container_of(kref, struct rbd_img_request, kref);
2263
Alex Elder37206ee2013-02-20 17:32:08 -06002264 dout("%s: img %p\n", __func__, img_request);
2265
Alex Elderbf0d5f502012-11-22 00:00:08 -06002266 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
2267 rbd_img_obj_request_del(img_request, obj_request);
Alex Elder25dcf952013-01-25 17:08:55 -06002268 rbd_assert(img_request->obj_request_count == 0);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002269
Alex Eldera2acd002013-05-08 22:50:04 -05002270 if (img_request_layered_test(img_request)) {
2271 img_request_layered_clear(img_request);
2272 rbd_dev_parent_put(img_request->rbd_dev);
2273 }
2274
Josh Durginbef95452014-04-04 17:47:52 -07002275 if (img_request_write_test(img_request) ||
2276 img_request_discard_test(img_request))
Alex Elder812164f82013-04-30 00:44:32 -05002277 ceph_put_snap_context(img_request->snapc);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002278
Alex Elder1c2a9df2013-05-01 12:43:03 -05002279 kmem_cache_free(rbd_img_request_cache, img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002280}
2281
Alex Eldere93f3152013-05-08 22:50:04 -05002282static struct rbd_img_request *rbd_parent_request_create(
2283 struct rbd_obj_request *obj_request,
2284 u64 img_offset, u64 length)
2285{
2286 struct rbd_img_request *parent_request;
2287 struct rbd_device *rbd_dev;
2288
2289 rbd_assert(obj_request->img_request);
2290 rbd_dev = obj_request->img_request->rbd_dev;
2291
Josh Durgin4e752f02014-04-08 11:12:11 -07002292 parent_request = rbd_img_request_create(rbd_dev->parent, img_offset,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002293 length, OBJ_OP_READ, NULL);
Alex Eldere93f3152013-05-08 22:50:04 -05002294 if (!parent_request)
2295 return NULL;
2296
2297 img_request_child_set(parent_request);
2298 rbd_obj_request_get(obj_request);
2299 parent_request->obj_request = obj_request;
2300
2301 return parent_request;
2302}
2303
2304static void rbd_parent_request_destroy(struct kref *kref)
2305{
2306 struct rbd_img_request *parent_request;
2307 struct rbd_obj_request *orig_request;
2308
2309 parent_request = container_of(kref, struct rbd_img_request, kref);
2310 orig_request = parent_request->obj_request;
2311
2312 parent_request->obj_request = NULL;
2313 rbd_obj_request_put(orig_request);
2314 img_request_child_clear(parent_request);
2315
2316 rbd_img_request_destroy(kref);
2317}
2318
Alex Elder12178572013-02-08 09:55:49 -06002319static bool rbd_img_obj_end_request(struct rbd_obj_request *obj_request)
2320{
Alex Elder6365d332013-02-11 12:33:24 -06002321 struct rbd_img_request *img_request;
Alex Elder12178572013-02-08 09:55:49 -06002322 unsigned int xferred;
2323 int result;
Alex Elder8b3e1a52013-01-24 16:13:36 -06002324 bool more;
Alex Elder12178572013-02-08 09:55:49 -06002325
Alex Elder6365d332013-02-11 12:33:24 -06002326 rbd_assert(obj_request_img_data_test(obj_request));
2327 img_request = obj_request->img_request;
2328
Alex Elder12178572013-02-08 09:55:49 -06002329 rbd_assert(obj_request->xferred <= (u64)UINT_MAX);
2330 xferred = (unsigned int)obj_request->xferred;
2331 result = obj_request->result;
2332 if (result) {
2333 struct rbd_device *rbd_dev = img_request->rbd_dev;
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002334 enum obj_operation_type op_type;
2335
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002336 if (img_request_discard_test(img_request))
2337 op_type = OBJ_OP_DISCARD;
2338 else if (img_request_write_test(img_request))
2339 op_type = OBJ_OP_WRITE;
2340 else
2341 op_type = OBJ_OP_READ;
Alex Elder12178572013-02-08 09:55:49 -06002342
Ilya Dryomov9584d502014-07-11 12:11:20 +04002343 rbd_warn(rbd_dev, "%s %llx at %llx (%llx)",
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002344 obj_op_name(op_type), obj_request->length,
2345 obj_request->img_offset, obj_request->offset);
Ilya Dryomov9584d502014-07-11 12:11:20 +04002346 rbd_warn(rbd_dev, " result %d xferred %x",
Alex Elder12178572013-02-08 09:55:49 -06002347 result, xferred);
2348 if (!img_request->result)
2349 img_request->result = result;
Ilya Dryomov082a75d2015-04-25 15:56:15 +03002350 /*
2351 * Need to end I/O on the entire obj_request worth of
2352 * bytes in case of error.
2353 */
2354 xferred = obj_request->length;
Alex Elder12178572013-02-08 09:55:49 -06002355 }
2356
Alex Elderf1a47392013-04-19 15:34:50 -05002357 /* Image object requests don't own their page array */
2358
2359 if (obj_request->type == OBJ_REQUEST_PAGES) {
2360 obj_request->pages = NULL;
2361 obj_request->page_count = 0;
2362 }
2363
Alex Elder8b3e1a52013-01-24 16:13:36 -06002364 if (img_request_child_test(img_request)) {
2365 rbd_assert(img_request->obj_request != NULL);
2366 more = obj_request->which < img_request->obj_request_count - 1;
2367 } else {
2368 rbd_assert(img_request->rq != NULL);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01002369
2370 more = blk_update_request(img_request->rq, result, xferred);
2371 if (!more)
2372 __blk_mq_end_request(img_request->rq, result);
Alex Elder8b3e1a52013-01-24 16:13:36 -06002373 }
2374
2375 return more;
Alex Elder12178572013-02-08 09:55:49 -06002376}
2377
Alex Elder21692382013-04-05 01:27:12 -05002378static void rbd_img_obj_callback(struct rbd_obj_request *obj_request)
2379{
2380 struct rbd_img_request *img_request;
2381 u32 which = obj_request->which;
2382 bool more = true;
2383
Alex Elder6365d332013-02-11 12:33:24 -06002384 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elder21692382013-04-05 01:27:12 -05002385 img_request = obj_request->img_request;
2386
2387 dout("%s: img %p obj %p\n", __func__, img_request, obj_request);
2388 rbd_assert(img_request != NULL);
Alex Elder21692382013-04-05 01:27:12 -05002389 rbd_assert(img_request->obj_request_count > 0);
2390 rbd_assert(which != BAD_WHICH);
2391 rbd_assert(which < img_request->obj_request_count);
Alex Elder21692382013-04-05 01:27:12 -05002392
2393 spin_lock_irq(&img_request->completion_lock);
2394 if (which != img_request->next_completion)
2395 goto out;
2396
2397 for_each_obj_request_from(img_request, obj_request) {
Alex Elder21692382013-04-05 01:27:12 -05002398 rbd_assert(more);
2399 rbd_assert(which < img_request->obj_request_count);
2400
2401 if (!obj_request_done_test(obj_request))
2402 break;
Alex Elder12178572013-02-08 09:55:49 -06002403 more = rbd_img_obj_end_request(obj_request);
Alex Elder21692382013-04-05 01:27:12 -05002404 which++;
2405 }
2406
2407 rbd_assert(more ^ (which == img_request->obj_request_count));
2408 img_request->next_completion = which;
2409out:
2410 spin_unlock_irq(&img_request->completion_lock);
Alex Elder0f2d5be2014-04-26 14:21:44 +04002411 rbd_img_request_put(img_request);
Alex Elder21692382013-04-05 01:27:12 -05002412
2413 if (!more)
2414 rbd_img_request_complete(img_request);
2415}
2416
Alex Elderf1a47392013-04-19 15:34:50 -05002417/*
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002418 * Add individual osd ops to the given ceph_osd_request and prepare
2419 * them for submission. num_ops is the current number of
2420 * osd operations already to the object request.
2421 */
2422static void rbd_img_obj_request_fill(struct rbd_obj_request *obj_request,
2423 struct ceph_osd_request *osd_request,
2424 enum obj_operation_type op_type,
2425 unsigned int num_ops)
2426{
2427 struct rbd_img_request *img_request = obj_request->img_request;
2428 struct rbd_device *rbd_dev = img_request->rbd_dev;
2429 u64 object_size = rbd_obj_bytes(&rbd_dev->header);
2430 u64 offset = obj_request->offset;
2431 u64 length = obj_request->length;
2432 u64 img_end;
2433 u16 opcode;
2434
2435 if (op_type == OBJ_OP_DISCARD) {
Josh Durgind3246fb2014-04-07 16:49:21 -07002436 if (!offset && length == object_size &&
2437 (!img_request_layered_test(img_request) ||
2438 !obj_request_overlaps_parent(obj_request))) {
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002439 opcode = CEPH_OSD_OP_DELETE;
2440 } else if ((offset + length == object_size)) {
2441 opcode = CEPH_OSD_OP_TRUNCATE;
2442 } else {
2443 down_read(&rbd_dev->header_rwsem);
2444 img_end = rbd_dev->header.image_size;
2445 up_read(&rbd_dev->header_rwsem);
2446
2447 if (obj_request->img_offset + length == img_end)
2448 opcode = CEPH_OSD_OP_TRUNCATE;
2449 else
2450 opcode = CEPH_OSD_OP_ZERO;
2451 }
2452 } else if (op_type == OBJ_OP_WRITE) {
Ilya Dryomove30b7572015-10-07 17:27:17 +02002453 if (!offset && length == object_size)
2454 opcode = CEPH_OSD_OP_WRITEFULL;
2455 else
2456 opcode = CEPH_OSD_OP_WRITE;
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002457 osd_req_op_alloc_hint_init(osd_request, num_ops,
2458 object_size, object_size);
2459 num_ops++;
2460 } else {
2461 opcode = CEPH_OSD_OP_READ;
2462 }
2463
Ilya Dryomov7e868b62014-11-21 22:16:43 +03002464 if (opcode == CEPH_OSD_OP_DELETE)
Yan, Zheng144cba12015-04-27 11:09:54 +08002465 osd_req_op_init(osd_request, num_ops, opcode, 0);
Ilya Dryomov7e868b62014-11-21 22:16:43 +03002466 else
2467 osd_req_op_extent_init(osd_request, num_ops, opcode,
2468 offset, length, 0, 0);
2469
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002470 if (obj_request->type == OBJ_REQUEST_BIO)
2471 osd_req_op_extent_osd_data_bio(osd_request, num_ops,
2472 obj_request->bio_list, length);
2473 else if (obj_request->type == OBJ_REQUEST_PAGES)
2474 osd_req_op_extent_osd_data_pages(osd_request, num_ops,
2475 obj_request->pages, length,
2476 offset & ~PAGE_MASK, false, false);
2477
2478 /* Discards are also writes */
2479 if (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD)
2480 rbd_osd_req_format_write(obj_request);
2481 else
2482 rbd_osd_req_format_read(obj_request);
2483}
2484
2485/*
Alex Elderf1a47392013-04-19 15:34:50 -05002486 * Split up an image request into one or more object requests, each
2487 * to a different object. The "type" parameter indicates whether
2488 * "data_desc" is the pointer to the head of a list of bio
2489 * structures, or the base of a page array. In either case this
2490 * function assumes data_desc describes memory sufficient to hold
2491 * all data described by the image request.
2492 */
2493static int rbd_img_request_fill(struct rbd_img_request *img_request,
2494 enum obj_request_type type,
2495 void *data_desc)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002496{
2497 struct rbd_device *rbd_dev = img_request->rbd_dev;
2498 struct rbd_obj_request *obj_request = NULL;
2499 struct rbd_obj_request *next_obj_request;
Jingoo Hana1580732013-08-09 13:04:35 +09002500 struct bio *bio_list = NULL;
Alex Elderf1a47392013-04-19 15:34:50 -05002501 unsigned int bio_offset = 0;
Jingoo Hana1580732013-08-09 13:04:35 +09002502 struct page **pages = NULL;
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002503 enum obj_operation_type op_type;
Alex Elder7da22d22013-01-24 16:13:36 -06002504 u64 img_offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002505 u64 resid;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002506
Alex Elderf1a47392013-04-19 15:34:50 -05002507 dout("%s: img %p type %d data_desc %p\n", __func__, img_request,
2508 (int)type, data_desc);
Alex Elder37206ee2013-02-20 17:32:08 -06002509
Alex Elder7da22d22013-01-24 16:13:36 -06002510 img_offset = img_request->offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002511 resid = img_request->length;
Alex Elder4dda41d2013-02-20 21:59:33 -06002512 rbd_assert(resid > 0);
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002513 op_type = rbd_img_request_op_type(img_request);
Alex Elderf1a47392013-04-19 15:34:50 -05002514
2515 if (type == OBJ_REQUEST_BIO) {
2516 bio_list = data_desc;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002517 rbd_assert(img_offset ==
2518 bio_list->bi_iter.bi_sector << SECTOR_SHIFT);
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002519 } else if (type == OBJ_REQUEST_PAGES) {
Alex Elderf1a47392013-04-19 15:34:50 -05002520 pages = data_desc;
2521 }
2522
Alex Elderbf0d5f502012-11-22 00:00:08 -06002523 while (resid) {
Alex Elder2fa12322013-04-05 01:27:12 -05002524 struct ceph_osd_request *osd_req;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002525 const char *object_name;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002526 u64 offset;
2527 u64 length;
2528
Alex Elder7da22d22013-01-24 16:13:36 -06002529 object_name = rbd_segment_name(rbd_dev, img_offset);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002530 if (!object_name)
2531 goto out_unwind;
Alex Elder7da22d22013-01-24 16:13:36 -06002532 offset = rbd_segment_offset(rbd_dev, img_offset);
2533 length = rbd_segment_length(rbd_dev, img_offset, resid);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002534 obj_request = rbd_obj_request_create(object_name,
Alex Elderf1a47392013-04-19 15:34:50 -05002535 offset, length, type);
Alex Elder78c2a442013-05-01 12:43:04 -05002536 /* object request has its own copy of the object name */
2537 rbd_segment_name_free(object_name);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002538 if (!obj_request)
2539 goto out_unwind;
Ilya Dryomov62054da2014-03-04 11:57:17 +02002540
Josh Durgin03507db2013-08-27 14:45:46 -07002541 /*
2542 * set obj_request->img_request before creating the
2543 * osd_request so that it gets the right snapc
2544 */
2545 rbd_img_obj_request_add(img_request, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002546
Alex Elderf1a47392013-04-19 15:34:50 -05002547 if (type == OBJ_REQUEST_BIO) {
2548 unsigned int clone_size;
2549
2550 rbd_assert(length <= (u64)UINT_MAX);
2551 clone_size = (unsigned int)length;
2552 obj_request->bio_list =
2553 bio_chain_clone_range(&bio_list,
2554 &bio_offset,
2555 clone_size,
David Disseldorp2224d872016-04-05 11:13:39 +02002556 GFP_NOIO);
Alex Elderf1a47392013-04-19 15:34:50 -05002557 if (!obj_request->bio_list)
Ilya Dryomov62054da2014-03-04 11:57:17 +02002558 goto out_unwind;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002559 } else if (type == OBJ_REQUEST_PAGES) {
Alex Elderf1a47392013-04-19 15:34:50 -05002560 unsigned int page_count;
2561
2562 obj_request->pages = pages;
2563 page_count = (u32)calc_pages_for(offset, length);
2564 obj_request->page_count = page_count;
2565 if ((offset + length) & ~PAGE_MASK)
2566 page_count--; /* more on last page */
2567 pages += page_count;
2568 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06002569
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002570 osd_req = rbd_osd_req_create(rbd_dev, op_type,
2571 (op_type == OBJ_OP_WRITE) ? 2 : 1,
2572 obj_request);
Alex Elder2fa12322013-04-05 01:27:12 -05002573 if (!osd_req)
Ilya Dryomov62054da2014-03-04 11:57:17 +02002574 goto out_unwind;
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002575
Alex Elder2fa12322013-04-05 01:27:12 -05002576 obj_request->osd_req = osd_req;
Alex Elder21692382013-04-05 01:27:12 -05002577 obj_request->callback = rbd_img_obj_callback;
Alex Elder7da22d22013-01-24 16:13:36 -06002578 obj_request->img_offset = img_offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002579
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002580 rbd_img_obj_request_fill(obj_request, osd_req, op_type, 0);
2581
2582 rbd_img_request_get(img_request);
2583
Alex Elder7da22d22013-01-24 16:13:36 -06002584 img_offset += length;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002585 resid -= length;
2586 }
2587
2588 return 0;
2589
Alex Elderbf0d5f502012-11-22 00:00:08 -06002590out_unwind:
2591 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
Ilya Dryomov42dd0372014-03-04 11:57:17 +02002592 rbd_img_obj_request_del(img_request, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002593
2594 return -ENOMEM;
2595}
2596
Alex Elder3d7efd12013-04-19 15:34:50 -05002597static void
Ilya Dryomov27617132015-07-16 17:36:11 +03002598rbd_osd_copyup_callback(struct rbd_obj_request *obj_request)
Alex Elder0eefd472013-04-19 15:34:50 -05002599{
2600 struct rbd_img_request *img_request;
2601 struct rbd_device *rbd_dev;
Alex Elderebda6402013-05-10 16:29:22 -05002602 struct page **pages;
Alex Elder0eefd472013-04-19 15:34:50 -05002603 u32 page_count;
2604
Ilya Dryomov27617132015-07-16 17:36:11 +03002605 dout("%s: obj %p\n", __func__, obj_request);
2606
Josh Durgind3246fb2014-04-07 16:49:21 -07002607 rbd_assert(obj_request->type == OBJ_REQUEST_BIO ||
2608 obj_request->type == OBJ_REQUEST_NODATA);
Alex Elder0eefd472013-04-19 15:34:50 -05002609 rbd_assert(obj_request_img_data_test(obj_request));
2610 img_request = obj_request->img_request;
2611 rbd_assert(img_request);
2612
2613 rbd_dev = img_request->rbd_dev;
2614 rbd_assert(rbd_dev);
Alex Elder0eefd472013-04-19 15:34:50 -05002615
Alex Elderebda6402013-05-10 16:29:22 -05002616 pages = obj_request->copyup_pages;
2617 rbd_assert(pages != NULL);
Alex Elder0eefd472013-04-19 15:34:50 -05002618 obj_request->copyup_pages = NULL;
Alex Elderebda6402013-05-10 16:29:22 -05002619 page_count = obj_request->copyup_page_count;
2620 rbd_assert(page_count);
2621 obj_request->copyup_page_count = 0;
2622 ceph_release_page_vector(pages, page_count);
Alex Elder0eefd472013-04-19 15:34:50 -05002623
2624 /*
2625 * We want the transfer count to reflect the size of the
2626 * original write request. There is no such thing as a
2627 * successful short write, so if the request was successful
2628 * we can just set it to the originally-requested length.
2629 */
2630 if (!obj_request->result)
2631 obj_request->xferred = obj_request->length;
2632
Ilya Dryomov27617132015-07-16 17:36:11 +03002633 obj_request_done_set(obj_request);
Alex Elder0eefd472013-04-19 15:34:50 -05002634}
2635
2636static void
Alex Elder3d7efd12013-04-19 15:34:50 -05002637rbd_img_obj_parent_read_full_callback(struct rbd_img_request *img_request)
2638{
2639 struct rbd_obj_request *orig_request;
Alex Elder0eefd472013-04-19 15:34:50 -05002640 struct ceph_osd_request *osd_req;
2641 struct ceph_osd_client *osdc;
2642 struct rbd_device *rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002643 struct page **pages;
Josh Durgind3246fb2014-04-07 16:49:21 -07002644 enum obj_operation_type op_type;
Alex Elderebda6402013-05-10 16:29:22 -05002645 u32 page_count;
Alex Elderbbea1c12013-05-06 17:40:33 -05002646 int img_result;
Alex Elderebda6402013-05-10 16:29:22 -05002647 u64 parent_length;
Alex Elder3d7efd12013-04-19 15:34:50 -05002648
2649 rbd_assert(img_request_child_test(img_request));
2650
2651 /* First get what we need from the image request */
2652
2653 pages = img_request->copyup_pages;
2654 rbd_assert(pages != NULL);
2655 img_request->copyup_pages = NULL;
Alex Elderebda6402013-05-10 16:29:22 -05002656 page_count = img_request->copyup_page_count;
2657 rbd_assert(page_count);
2658 img_request->copyup_page_count = 0;
Alex Elder3d7efd12013-04-19 15:34:50 -05002659
2660 orig_request = img_request->obj_request;
2661 rbd_assert(orig_request != NULL);
Alex Elderb91f09f2013-05-10 16:29:22 -05002662 rbd_assert(obj_request_type_valid(orig_request->type));
Alex Elderbbea1c12013-05-06 17:40:33 -05002663 img_result = img_request->result;
Alex Elderebda6402013-05-10 16:29:22 -05002664 parent_length = img_request->length;
2665 rbd_assert(parent_length == img_request->xferred);
Alex Elder3d7efd12013-04-19 15:34:50 -05002666 rbd_img_request_put(img_request);
2667
Alex Elder91c6feb2013-05-06 17:40:32 -05002668 rbd_assert(orig_request->img_request);
2669 rbd_dev = orig_request->img_request->rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002670 rbd_assert(rbd_dev);
Alex Elder3d7efd12013-04-19 15:34:50 -05002671
Alex Elderbbea1c12013-05-06 17:40:33 -05002672 /*
2673 * If the overlap has become 0 (most likely because the
2674 * image has been flattened) we need to free the pages
2675 * and re-submit the original write request.
2676 */
2677 if (!rbd_dev->parent_overlap) {
2678 struct ceph_osd_client *osdc;
2679
2680 ceph_release_page_vector(pages, page_count);
2681 osdc = &rbd_dev->rbd_client->client->osdc;
2682 img_result = rbd_obj_request_submit(osdc, orig_request);
2683 if (!img_result)
2684 return;
2685 }
2686
2687 if (img_result)
Alex Elder0eefd472013-04-19 15:34:50 -05002688 goto out_err;
Alex Elder3d7efd12013-04-19 15:34:50 -05002689
Alex Elder8785b1d2013-05-09 10:08:49 -05002690 /*
2691 * The original osd request is of no use to use any more.
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02002692 * We need a new one that can hold the three ops in a copyup
Alex Elder8785b1d2013-05-09 10:08:49 -05002693 * request. Allocate the new copyup osd request for the
2694 * original request, and release the old one.
2695 */
Alex Elderbbea1c12013-05-06 17:40:33 -05002696 img_result = -ENOMEM;
Alex Elder0eefd472013-04-19 15:34:50 -05002697 osd_req = rbd_osd_req_create_copyup(orig_request);
2698 if (!osd_req)
2699 goto out_err;
Alex Elder8785b1d2013-05-09 10:08:49 -05002700 rbd_osd_req_destroy(orig_request->osd_req);
Alex Elder0eefd472013-04-19 15:34:50 -05002701 orig_request->osd_req = osd_req;
2702 orig_request->copyup_pages = pages;
Alex Elderebda6402013-05-10 16:29:22 -05002703 orig_request->copyup_page_count = page_count;
Alex Elder3d7efd12013-04-19 15:34:50 -05002704
Alex Elder0eefd472013-04-19 15:34:50 -05002705 /* Initialize the copyup op */
2706
2707 osd_req_op_cls_init(osd_req, 0, CEPH_OSD_OP_CALL, "rbd", "copyup");
Alex Elderebda6402013-05-10 16:29:22 -05002708 osd_req_op_cls_request_data_pages(osd_req, 0, pages, parent_length, 0,
Alex Elder0eefd472013-04-19 15:34:50 -05002709 false, false);
2710
Josh Durgind3246fb2014-04-07 16:49:21 -07002711 /* Add the other op(s) */
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02002712
Josh Durgind3246fb2014-04-07 16:49:21 -07002713 op_type = rbd_img_request_op_type(orig_request->img_request);
2714 rbd_img_obj_request_fill(orig_request, osd_req, op_type, 1);
Alex Elder0eefd472013-04-19 15:34:50 -05002715
2716 /* All set, send it off. */
2717
Alex Elder0eefd472013-04-19 15:34:50 -05002718 osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elderbbea1c12013-05-06 17:40:33 -05002719 img_result = rbd_obj_request_submit(osdc, orig_request);
2720 if (!img_result)
Alex Elder0eefd472013-04-19 15:34:50 -05002721 return;
2722out_err:
2723 /* Record the error code and complete the request */
2724
Alex Elderbbea1c12013-05-06 17:40:33 -05002725 orig_request->result = img_result;
Alex Elder0eefd472013-04-19 15:34:50 -05002726 orig_request->xferred = 0;
2727 obj_request_done_set(orig_request);
2728 rbd_obj_request_complete(orig_request);
Alex Elder3d7efd12013-04-19 15:34:50 -05002729}
2730
2731/*
2732 * Read from the parent image the range of data that covers the
2733 * entire target of the given object request. This is used for
2734 * satisfying a layered image write request when the target of an
2735 * object request from the image request does not exist.
2736 *
2737 * A page array big enough to hold the returned data is allocated
2738 * and supplied to rbd_img_request_fill() as the "data descriptor."
2739 * When the read completes, this page array will be transferred to
2740 * the original object request for the copyup operation.
2741 *
2742 * If an error occurs, record it as the result of the original
2743 * object request and mark it done so it gets completed.
2744 */
2745static int rbd_img_obj_parent_read_full(struct rbd_obj_request *obj_request)
2746{
2747 struct rbd_img_request *img_request = NULL;
2748 struct rbd_img_request *parent_request = NULL;
2749 struct rbd_device *rbd_dev;
2750 u64 img_offset;
2751 u64 length;
2752 struct page **pages = NULL;
2753 u32 page_count;
2754 int result;
2755
2756 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elderb91f09f2013-05-10 16:29:22 -05002757 rbd_assert(obj_request_type_valid(obj_request->type));
Alex Elder3d7efd12013-04-19 15:34:50 -05002758
2759 img_request = obj_request->img_request;
2760 rbd_assert(img_request != NULL);
2761 rbd_dev = img_request->rbd_dev;
2762 rbd_assert(rbd_dev->parent != NULL);
2763
2764 /*
2765 * Determine the byte range covered by the object in the
2766 * child image to which the original request was to be sent.
2767 */
2768 img_offset = obj_request->img_offset - obj_request->offset;
2769 length = (u64)1 << rbd_dev->header.obj_order;
2770
2771 /*
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002772 * There is no defined parent data beyond the parent
2773 * overlap, so limit what we read at that boundary if
2774 * necessary.
2775 */
2776 if (img_offset + length > rbd_dev->parent_overlap) {
2777 rbd_assert(img_offset < rbd_dev->parent_overlap);
2778 length = rbd_dev->parent_overlap - img_offset;
2779 }
2780
2781 /*
Alex Elder3d7efd12013-04-19 15:34:50 -05002782 * Allocate a page array big enough to receive the data read
2783 * from the parent.
2784 */
2785 page_count = (u32)calc_pages_for(0, length);
2786 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2787 if (IS_ERR(pages)) {
2788 result = PTR_ERR(pages);
2789 pages = NULL;
2790 goto out_err;
2791 }
2792
2793 result = -ENOMEM;
Alex Eldere93f3152013-05-08 22:50:04 -05002794 parent_request = rbd_parent_request_create(obj_request,
2795 img_offset, length);
Alex Elder3d7efd12013-04-19 15:34:50 -05002796 if (!parent_request)
2797 goto out_err;
Alex Elder3d7efd12013-04-19 15:34:50 -05002798
2799 result = rbd_img_request_fill(parent_request, OBJ_REQUEST_PAGES, pages);
2800 if (result)
2801 goto out_err;
2802 parent_request->copyup_pages = pages;
Alex Elderebda6402013-05-10 16:29:22 -05002803 parent_request->copyup_page_count = page_count;
Alex Elder3d7efd12013-04-19 15:34:50 -05002804
2805 parent_request->callback = rbd_img_obj_parent_read_full_callback;
2806 result = rbd_img_request_submit(parent_request);
2807 if (!result)
2808 return 0;
2809
2810 parent_request->copyup_pages = NULL;
Alex Elderebda6402013-05-10 16:29:22 -05002811 parent_request->copyup_page_count = 0;
Alex Elder3d7efd12013-04-19 15:34:50 -05002812 parent_request->obj_request = NULL;
2813 rbd_obj_request_put(obj_request);
2814out_err:
2815 if (pages)
2816 ceph_release_page_vector(pages, page_count);
2817 if (parent_request)
2818 rbd_img_request_put(parent_request);
2819 obj_request->result = result;
2820 obj_request->xferred = 0;
2821 obj_request_done_set(obj_request);
2822
2823 return result;
2824}
2825
Alex Elderc5b5ef62013-02-11 12:33:24 -06002826static void rbd_img_obj_exists_callback(struct rbd_obj_request *obj_request)
2827{
Alex Elderc5b5ef62013-02-11 12:33:24 -06002828 struct rbd_obj_request *orig_request;
Alex Elder638f5ab2013-05-06 17:40:33 -05002829 struct rbd_device *rbd_dev;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002830 int result;
2831
2832 rbd_assert(!obj_request_img_data_test(obj_request));
2833
2834 /*
2835 * All we need from the object request is the original
2836 * request and the result of the STAT op. Grab those, then
2837 * we're done with the request.
2838 */
2839 orig_request = obj_request->obj_request;
2840 obj_request->obj_request = NULL;
Alex Elder912c3172013-05-13 20:35:38 -05002841 rbd_obj_request_put(orig_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002842 rbd_assert(orig_request);
2843 rbd_assert(orig_request->img_request);
2844
2845 result = obj_request->result;
2846 obj_request->result = 0;
2847
2848 dout("%s: obj %p for obj %p result %d %llu/%llu\n", __func__,
2849 obj_request, orig_request, result,
2850 obj_request->xferred, obj_request->length);
2851 rbd_obj_request_put(obj_request);
2852
Alex Elder638f5ab2013-05-06 17:40:33 -05002853 /*
2854 * If the overlap has become 0 (most likely because the
2855 * image has been flattened) we need to free the pages
2856 * and re-submit the original write request.
2857 */
2858 rbd_dev = orig_request->img_request->rbd_dev;
2859 if (!rbd_dev->parent_overlap) {
2860 struct ceph_osd_client *osdc;
2861
Alex Elder638f5ab2013-05-06 17:40:33 -05002862 osdc = &rbd_dev->rbd_client->client->osdc;
2863 result = rbd_obj_request_submit(osdc, orig_request);
2864 if (!result)
2865 return;
2866 }
Alex Elderc5b5ef62013-02-11 12:33:24 -06002867
2868 /*
2869 * Our only purpose here is to determine whether the object
2870 * exists, and we don't want to treat the non-existence as
2871 * an error. If something else comes back, transfer the
2872 * error to the original request and complete it now.
2873 */
2874 if (!result) {
2875 obj_request_existence_set(orig_request, true);
2876 } else if (result == -ENOENT) {
2877 obj_request_existence_set(orig_request, false);
2878 } else if (result) {
2879 orig_request->result = result;
Alex Elder3d7efd12013-04-19 15:34:50 -05002880 goto out;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002881 }
2882
2883 /*
2884 * Resubmit the original request now that we have recorded
2885 * whether the target object exists.
2886 */
Alex Elderb454e362013-04-19 15:34:50 -05002887 orig_request->result = rbd_img_obj_request_submit(orig_request);
Alex Elder3d7efd12013-04-19 15:34:50 -05002888out:
Alex Elderc5b5ef62013-02-11 12:33:24 -06002889 if (orig_request->result)
2890 rbd_obj_request_complete(orig_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002891}
2892
2893static int rbd_img_obj_exists_submit(struct rbd_obj_request *obj_request)
2894{
2895 struct rbd_obj_request *stat_request;
2896 struct rbd_device *rbd_dev;
2897 struct ceph_osd_client *osdc;
2898 struct page **pages = NULL;
2899 u32 page_count;
2900 size_t size;
2901 int ret;
2902
2903 /*
2904 * The response data for a STAT call consists of:
2905 * le64 length;
2906 * struct {
2907 * le32 tv_sec;
2908 * le32 tv_nsec;
2909 * } mtime;
2910 */
2911 size = sizeof (__le64) + sizeof (__le32) + sizeof (__le32);
2912 page_count = (u32)calc_pages_for(0, size);
2913 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2914 if (IS_ERR(pages))
2915 return PTR_ERR(pages);
2916
2917 ret = -ENOMEM;
2918 stat_request = rbd_obj_request_create(obj_request->object_name, 0, 0,
2919 OBJ_REQUEST_PAGES);
2920 if (!stat_request)
2921 goto out;
2922
2923 rbd_obj_request_get(obj_request);
2924 stat_request->obj_request = obj_request;
2925 stat_request->pages = pages;
2926 stat_request->page_count = page_count;
2927
2928 rbd_assert(obj_request->img_request);
2929 rbd_dev = obj_request->img_request->rbd_dev;
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002930 stat_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_READ, 1,
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02002931 stat_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002932 if (!stat_request->osd_req)
2933 goto out;
2934 stat_request->callback = rbd_img_obj_exists_callback;
2935
Yan, Zheng144cba12015-04-27 11:09:54 +08002936 osd_req_op_init(stat_request->osd_req, 0, CEPH_OSD_OP_STAT, 0);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002937 osd_req_op_raw_data_in_pages(stat_request->osd_req, 0, pages, size, 0,
2938 false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05002939 rbd_osd_req_format_read(stat_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002940
2941 osdc = &rbd_dev->rbd_client->client->osdc;
2942 ret = rbd_obj_request_submit(osdc, stat_request);
2943out:
2944 if (ret)
2945 rbd_obj_request_put(obj_request);
2946
2947 return ret;
2948}
2949
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002950static bool img_obj_request_simple(struct rbd_obj_request *obj_request)
Alex Elderb454e362013-04-19 15:34:50 -05002951{
2952 struct rbd_img_request *img_request;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002953 struct rbd_device *rbd_dev;
Alex Elderb454e362013-04-19 15:34:50 -05002954
2955 rbd_assert(obj_request_img_data_test(obj_request));
2956
2957 img_request = obj_request->img_request;
2958 rbd_assert(img_request);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002959 rbd_dev = img_request->rbd_dev;
Alex Elderb454e362013-04-19 15:34:50 -05002960
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002961 /* Reads */
Josh Durgin1c220882014-04-04 17:49:12 -07002962 if (!img_request_write_test(img_request) &&
2963 !img_request_discard_test(img_request))
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002964 return true;
Alex Elderb454e362013-04-19 15:34:50 -05002965
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002966 /* Non-layered writes */
2967 if (!img_request_layered_test(img_request))
2968 return true;
2969
2970 /*
2971 * Layered writes outside of the parent overlap range don't
2972 * share any data with the parent.
2973 */
2974 if (!obj_request_overlaps_parent(obj_request))
2975 return true;
2976
2977 /*
Guangliang Zhaoc622d222014-04-01 22:22:15 +08002978 * Entire-object layered writes - we will overwrite whatever
2979 * parent data there is anyway.
2980 */
2981 if (!obj_request->offset &&
2982 obj_request->length == rbd_obj_bytes(&rbd_dev->header))
2983 return true;
2984
2985 /*
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002986 * If the object is known to already exist, its parent data has
2987 * already been copied.
2988 */
2989 if (obj_request_known_test(obj_request) &&
2990 obj_request_exists_test(obj_request))
2991 return true;
2992
2993 return false;
2994}
2995
2996static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request)
2997{
2998 if (img_obj_request_simple(obj_request)) {
Alex Elderb454e362013-04-19 15:34:50 -05002999 struct rbd_device *rbd_dev;
3000 struct ceph_osd_client *osdc;
3001
3002 rbd_dev = obj_request->img_request->rbd_dev;
3003 osdc = &rbd_dev->rbd_client->client->osdc;
3004
3005 return rbd_obj_request_submit(osdc, obj_request);
3006 }
3007
3008 /*
Alex Elder3d7efd12013-04-19 15:34:50 -05003009 * It's a layered write. The target object might exist but
3010 * we may not know that yet. If we know it doesn't exist,
3011 * start by reading the data for the full target object from
3012 * the parent so we can use it for a copyup to the target.
Alex Elderb454e362013-04-19 15:34:50 -05003013 */
Ilya Dryomov70d045f2014-09-12 16:02:01 +04003014 if (obj_request_known_test(obj_request))
Alex Elder3d7efd12013-04-19 15:34:50 -05003015 return rbd_img_obj_parent_read_full(obj_request);
3016
3017 /* We don't know whether the target exists. Go find out. */
Alex Elderb454e362013-04-19 15:34:50 -05003018
3019 return rbd_img_obj_exists_submit(obj_request);
3020}
3021
Alex Elderbf0d5f502012-11-22 00:00:08 -06003022static int rbd_img_request_submit(struct rbd_img_request *img_request)
3023{
Alex Elderbf0d5f502012-11-22 00:00:08 -06003024 struct rbd_obj_request *obj_request;
Alex Elder46faeed2013-04-10 17:47:46 -05003025 struct rbd_obj_request *next_obj_request;
Ilya Dryomov663ae2c2016-05-16 13:18:57 +02003026 int ret = 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06003027
Alex Elder37206ee2013-02-20 17:32:08 -06003028 dout("%s: img %p\n", __func__, img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06003029
Ilya Dryomov663ae2c2016-05-16 13:18:57 +02003030 rbd_img_request_get(img_request);
3031 for_each_obj_request_safe(img_request, obj_request, next_obj_request) {
Alex Elderb454e362013-04-19 15:34:50 -05003032 ret = rbd_img_obj_request_submit(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06003033 if (ret)
Ilya Dryomov663ae2c2016-05-16 13:18:57 +02003034 goto out_put_ireq;
Alex Elderbf0d5f502012-11-22 00:00:08 -06003035 }
3036
Ilya Dryomov663ae2c2016-05-16 13:18:57 +02003037out_put_ireq:
3038 rbd_img_request_put(img_request);
3039 return ret;
Alex Elderbf0d5f502012-11-22 00:00:08 -06003040}
3041
Alex Elder8b3e1a52013-01-24 16:13:36 -06003042static void rbd_img_parent_read_callback(struct rbd_img_request *img_request)
3043{
3044 struct rbd_obj_request *obj_request;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05003045 struct rbd_device *rbd_dev;
3046 u64 obj_end;
Alex Elder02c74fb2013-05-06 17:40:33 -05003047 u64 img_xferred;
3048 int img_result;
Alex Elder8b3e1a52013-01-24 16:13:36 -06003049
3050 rbd_assert(img_request_child_test(img_request));
3051
Alex Elder02c74fb2013-05-06 17:40:33 -05003052 /* First get what we need from the image request and release it */
3053
Alex Elder8b3e1a52013-01-24 16:13:36 -06003054 obj_request = img_request->obj_request;
Alex Elder02c74fb2013-05-06 17:40:33 -05003055 img_xferred = img_request->xferred;
3056 img_result = img_request->result;
3057 rbd_img_request_put(img_request);
3058
3059 /*
3060 * If the overlap has become 0 (most likely because the
3061 * image has been flattened) we need to re-submit the
3062 * original request.
3063 */
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05003064 rbd_assert(obj_request);
3065 rbd_assert(obj_request->img_request);
Alex Elder02c74fb2013-05-06 17:40:33 -05003066 rbd_dev = obj_request->img_request->rbd_dev;
3067 if (!rbd_dev->parent_overlap) {
3068 struct ceph_osd_client *osdc;
Alex Elder8b3e1a52013-01-24 16:13:36 -06003069
Alex Elder02c74fb2013-05-06 17:40:33 -05003070 osdc = &rbd_dev->rbd_client->client->osdc;
3071 img_result = rbd_obj_request_submit(osdc, obj_request);
3072 if (!img_result)
3073 return;
3074 }
3075
3076 obj_request->result = img_result;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05003077 if (obj_request->result)
3078 goto out;
3079
3080 /*
3081 * We need to zero anything beyond the parent overlap
3082 * boundary. Since rbd_img_obj_request_read_callback()
3083 * will zero anything beyond the end of a short read, an
3084 * easy way to do this is to pretend the data from the
3085 * parent came up short--ending at the overlap boundary.
3086 */
3087 rbd_assert(obj_request->img_offset < U64_MAX - obj_request->length);
3088 obj_end = obj_request->img_offset + obj_request->length;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05003089 if (obj_end > rbd_dev->parent_overlap) {
3090 u64 xferred = 0;
3091
3092 if (obj_request->img_offset < rbd_dev->parent_overlap)
3093 xferred = rbd_dev->parent_overlap -
3094 obj_request->img_offset;
3095
Alex Elder02c74fb2013-05-06 17:40:33 -05003096 obj_request->xferred = min(img_xferred, xferred);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05003097 } else {
Alex Elder02c74fb2013-05-06 17:40:33 -05003098 obj_request->xferred = img_xferred;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05003099 }
3100out:
Alex Elder8b3e1a52013-01-24 16:13:36 -06003101 rbd_img_obj_request_read_callback(obj_request);
3102 rbd_obj_request_complete(obj_request);
3103}
3104
3105static void rbd_img_parent_read(struct rbd_obj_request *obj_request)
3106{
Alex Elder8b3e1a52013-01-24 16:13:36 -06003107 struct rbd_img_request *img_request;
3108 int result;
3109
3110 rbd_assert(obj_request_img_data_test(obj_request));
3111 rbd_assert(obj_request->img_request != NULL);
3112 rbd_assert(obj_request->result == (s32) -ENOENT);
Alex Elder5b2ab722013-05-06 17:40:33 -05003113 rbd_assert(obj_request_type_valid(obj_request->type));
Alex Elder8b3e1a52013-01-24 16:13:36 -06003114
Alex Elder8b3e1a52013-01-24 16:13:36 -06003115 /* rbd_read_finish(obj_request, obj_request->length); */
Alex Eldere93f3152013-05-08 22:50:04 -05003116 img_request = rbd_parent_request_create(obj_request,
Alex Elder8b3e1a52013-01-24 16:13:36 -06003117 obj_request->img_offset,
Alex Eldere93f3152013-05-08 22:50:04 -05003118 obj_request->length);
Alex Elder8b3e1a52013-01-24 16:13:36 -06003119 result = -ENOMEM;
3120 if (!img_request)
3121 goto out_err;
3122
Alex Elder5b2ab722013-05-06 17:40:33 -05003123 if (obj_request->type == OBJ_REQUEST_BIO)
3124 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
3125 obj_request->bio_list);
3126 else
3127 result = rbd_img_request_fill(img_request, OBJ_REQUEST_PAGES,
3128 obj_request->pages);
Alex Elder8b3e1a52013-01-24 16:13:36 -06003129 if (result)
3130 goto out_err;
3131
3132 img_request->callback = rbd_img_parent_read_callback;
3133 result = rbd_img_request_submit(img_request);
3134 if (result)
3135 goto out_err;
3136
3137 return;
3138out_err:
3139 if (img_request)
3140 rbd_img_request_put(img_request);
3141 obj_request->result = result;
3142 obj_request->xferred = 0;
3143 obj_request_done_set(obj_request);
3144}
3145
Ilya Dryomoved95b212016-08-12 16:40:02 +02003146static const struct rbd_client_id rbd_empty_cid;
3147
3148static bool rbd_cid_equal(const struct rbd_client_id *lhs,
3149 const struct rbd_client_id *rhs)
3150{
3151 return lhs->gid == rhs->gid && lhs->handle == rhs->handle;
3152}
3153
3154static struct rbd_client_id rbd_get_cid(struct rbd_device *rbd_dev)
3155{
3156 struct rbd_client_id cid;
3157
3158 mutex_lock(&rbd_dev->watch_mutex);
3159 cid.gid = ceph_client_gid(rbd_dev->rbd_client->client);
3160 cid.handle = rbd_dev->watch_cookie;
3161 mutex_unlock(&rbd_dev->watch_mutex);
3162 return cid;
3163}
3164
3165/*
3166 * lock_rwsem must be held for write
3167 */
3168static void rbd_set_owner_cid(struct rbd_device *rbd_dev,
3169 const struct rbd_client_id *cid)
3170{
3171 dout("%s rbd_dev %p %llu-%llu -> %llu-%llu\n", __func__, rbd_dev,
3172 rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle,
3173 cid->gid, cid->handle);
3174 rbd_dev->owner_cid = *cid; /* struct */
3175}
3176
3177static void format_lock_cookie(struct rbd_device *rbd_dev, char *buf)
3178{
3179 mutex_lock(&rbd_dev->watch_mutex);
3180 sprintf(buf, "%s %llu", RBD_LOCK_COOKIE_PREFIX, rbd_dev->watch_cookie);
3181 mutex_unlock(&rbd_dev->watch_mutex);
3182}
3183
3184/*
3185 * lock_rwsem must be held for write
3186 */
3187static int rbd_lock(struct rbd_device *rbd_dev)
3188{
3189 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3190 struct rbd_client_id cid = rbd_get_cid(rbd_dev);
3191 char cookie[32];
3192 int ret;
3193
3194 WARN_ON(__rbd_is_lock_owner(rbd_dev));
3195
3196 format_lock_cookie(rbd_dev, cookie);
3197 ret = ceph_cls_lock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc,
3198 RBD_LOCK_NAME, CEPH_CLS_LOCK_EXCLUSIVE, cookie,
3199 RBD_LOCK_TAG, "", 0);
3200 if (ret)
3201 return ret;
3202
3203 rbd_dev->lock_state = RBD_LOCK_STATE_LOCKED;
3204 rbd_set_owner_cid(rbd_dev, &cid);
3205 queue_work(rbd_dev->task_wq, &rbd_dev->acquired_lock_work);
3206 return 0;
3207}
3208
3209/*
3210 * lock_rwsem must be held for write
3211 */
3212static int rbd_unlock(struct rbd_device *rbd_dev)
3213{
3214 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3215 char cookie[32];
3216 int ret;
3217
3218 WARN_ON(!__rbd_is_lock_owner(rbd_dev));
3219
3220 rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED;
3221
3222 format_lock_cookie(rbd_dev, cookie);
3223 ret = ceph_cls_unlock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc,
3224 RBD_LOCK_NAME, cookie);
3225 if (ret && ret != -ENOENT) {
3226 rbd_warn(rbd_dev, "cls_unlock failed: %d", ret);
3227 return ret;
3228 }
3229
3230 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
3231 queue_work(rbd_dev->task_wq, &rbd_dev->released_lock_work);
3232 return 0;
3233}
3234
3235static int __rbd_notify_op_lock(struct rbd_device *rbd_dev,
3236 enum rbd_notify_op notify_op,
3237 struct page ***preply_pages,
3238 size_t *preply_len)
3239{
3240 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3241 struct rbd_client_id cid = rbd_get_cid(rbd_dev);
3242 int buf_size = 4 + 8 + 8 + CEPH_ENCODING_START_BLK_LEN;
3243 char buf[buf_size];
3244 void *p = buf;
3245
3246 dout("%s rbd_dev %p notify_op %d\n", __func__, rbd_dev, notify_op);
3247
3248 /* encode *LockPayload NotifyMessage (op + ClientId) */
3249 ceph_start_encoding(&p, 2, 1, buf_size - CEPH_ENCODING_START_BLK_LEN);
3250 ceph_encode_32(&p, notify_op);
3251 ceph_encode_64(&p, cid.gid);
3252 ceph_encode_64(&p, cid.handle);
3253
3254 return ceph_osdc_notify(osdc, &rbd_dev->header_oid,
3255 &rbd_dev->header_oloc, buf, buf_size,
3256 RBD_NOTIFY_TIMEOUT, preply_pages, preply_len);
3257}
3258
3259static void rbd_notify_op_lock(struct rbd_device *rbd_dev,
3260 enum rbd_notify_op notify_op)
3261{
3262 struct page **reply_pages;
3263 size_t reply_len;
3264
3265 __rbd_notify_op_lock(rbd_dev, notify_op, &reply_pages, &reply_len);
3266 ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len));
3267}
3268
3269static void rbd_notify_acquired_lock(struct work_struct *work)
3270{
3271 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
3272 acquired_lock_work);
3273
3274 rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_ACQUIRED_LOCK);
3275}
3276
3277static void rbd_notify_released_lock(struct work_struct *work)
3278{
3279 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
3280 released_lock_work);
3281
3282 rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_RELEASED_LOCK);
3283}
3284
3285static int rbd_request_lock(struct rbd_device *rbd_dev)
3286{
3287 struct page **reply_pages;
3288 size_t reply_len;
3289 bool lock_owner_responded = false;
3290 int ret;
3291
3292 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3293
3294 ret = __rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_REQUEST_LOCK,
3295 &reply_pages, &reply_len);
3296 if (ret && ret != -ETIMEDOUT) {
3297 rbd_warn(rbd_dev, "failed to request lock: %d", ret);
3298 goto out;
3299 }
3300
3301 if (reply_len > 0 && reply_len <= PAGE_SIZE) {
3302 void *p = page_address(reply_pages[0]);
3303 void *const end = p + reply_len;
3304 u32 n;
3305
3306 ceph_decode_32_safe(&p, end, n, e_inval); /* num_acks */
3307 while (n--) {
3308 u8 struct_v;
3309 u32 len;
3310
3311 ceph_decode_need(&p, end, 8 + 8, e_inval);
3312 p += 8 + 8; /* skip gid and cookie */
3313
3314 ceph_decode_32_safe(&p, end, len, e_inval);
3315 if (!len)
3316 continue;
3317
3318 if (lock_owner_responded) {
3319 rbd_warn(rbd_dev,
3320 "duplicate lock owners detected");
3321 ret = -EIO;
3322 goto out;
3323 }
3324
3325 lock_owner_responded = true;
3326 ret = ceph_start_decoding(&p, end, 1, "ResponseMessage",
3327 &struct_v, &len);
3328 if (ret) {
3329 rbd_warn(rbd_dev,
3330 "failed to decode ResponseMessage: %d",
3331 ret);
3332 goto e_inval;
3333 }
3334
3335 ret = ceph_decode_32(&p);
3336 }
3337 }
3338
3339 if (!lock_owner_responded) {
3340 rbd_warn(rbd_dev, "no lock owners detected");
3341 ret = -ETIMEDOUT;
3342 }
3343
3344out:
3345 ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len));
3346 return ret;
3347
3348e_inval:
3349 ret = -EINVAL;
3350 goto out;
3351}
3352
3353static void wake_requests(struct rbd_device *rbd_dev, bool wake_all)
3354{
3355 dout("%s rbd_dev %p wake_all %d\n", __func__, rbd_dev, wake_all);
3356
3357 cancel_delayed_work(&rbd_dev->lock_dwork);
3358 if (wake_all)
3359 wake_up_all(&rbd_dev->lock_waitq);
3360 else
3361 wake_up(&rbd_dev->lock_waitq);
3362}
3363
3364static int get_lock_owner_info(struct rbd_device *rbd_dev,
3365 struct ceph_locker **lockers, u32 *num_lockers)
3366{
3367 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3368 u8 lock_type;
3369 char *lock_tag;
3370 int ret;
3371
3372 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3373
3374 ret = ceph_cls_lock_info(osdc, &rbd_dev->header_oid,
3375 &rbd_dev->header_oloc, RBD_LOCK_NAME,
3376 &lock_type, &lock_tag, lockers, num_lockers);
3377 if (ret)
3378 return ret;
3379
3380 if (*num_lockers == 0) {
3381 dout("%s rbd_dev %p no lockers detected\n", __func__, rbd_dev);
3382 goto out;
3383 }
3384
3385 if (strcmp(lock_tag, RBD_LOCK_TAG)) {
3386 rbd_warn(rbd_dev, "locked by external mechanism, tag %s",
3387 lock_tag);
3388 ret = -EBUSY;
3389 goto out;
3390 }
3391
3392 if (lock_type == CEPH_CLS_LOCK_SHARED) {
3393 rbd_warn(rbd_dev, "shared lock type detected");
3394 ret = -EBUSY;
3395 goto out;
3396 }
3397
3398 if (strncmp((*lockers)[0].id.cookie, RBD_LOCK_COOKIE_PREFIX,
3399 strlen(RBD_LOCK_COOKIE_PREFIX))) {
3400 rbd_warn(rbd_dev, "locked by external mechanism, cookie %s",
3401 (*lockers)[0].id.cookie);
3402 ret = -EBUSY;
3403 goto out;
3404 }
3405
3406out:
3407 kfree(lock_tag);
3408 return ret;
3409}
3410
3411static int find_watcher(struct rbd_device *rbd_dev,
3412 const struct ceph_locker *locker)
3413{
3414 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3415 struct ceph_watch_item *watchers;
3416 u32 num_watchers;
3417 u64 cookie;
3418 int i;
3419 int ret;
3420
3421 ret = ceph_osdc_list_watchers(osdc, &rbd_dev->header_oid,
3422 &rbd_dev->header_oloc, &watchers,
3423 &num_watchers);
3424 if (ret)
3425 return ret;
3426
3427 sscanf(locker->id.cookie, RBD_LOCK_COOKIE_PREFIX " %llu", &cookie);
3428 for (i = 0; i < num_watchers; i++) {
3429 if (!memcmp(&watchers[i].addr, &locker->info.addr,
3430 sizeof(locker->info.addr)) &&
3431 watchers[i].cookie == cookie) {
3432 struct rbd_client_id cid = {
3433 .gid = le64_to_cpu(watchers[i].name.num),
3434 .handle = cookie,
3435 };
3436
3437 dout("%s rbd_dev %p found cid %llu-%llu\n", __func__,
3438 rbd_dev, cid.gid, cid.handle);
3439 rbd_set_owner_cid(rbd_dev, &cid);
3440 ret = 1;
3441 goto out;
3442 }
3443 }
3444
3445 dout("%s rbd_dev %p no watchers\n", __func__, rbd_dev);
3446 ret = 0;
3447out:
3448 kfree(watchers);
3449 return ret;
3450}
3451
3452/*
3453 * lock_rwsem must be held for write
3454 */
3455static int rbd_try_lock(struct rbd_device *rbd_dev)
3456{
3457 struct ceph_client *client = rbd_dev->rbd_client->client;
3458 struct ceph_locker *lockers;
3459 u32 num_lockers;
3460 int ret;
3461
3462 for (;;) {
3463 ret = rbd_lock(rbd_dev);
3464 if (ret != -EBUSY)
3465 return ret;
3466
3467 /* determine if the current lock holder is still alive */
3468 ret = get_lock_owner_info(rbd_dev, &lockers, &num_lockers);
3469 if (ret)
3470 return ret;
3471
3472 if (num_lockers == 0)
3473 goto again;
3474
3475 ret = find_watcher(rbd_dev, lockers);
3476 if (ret) {
3477 if (ret > 0)
3478 ret = 0; /* have to request lock */
3479 goto out;
3480 }
3481
3482 rbd_warn(rbd_dev, "%s%llu seems dead, breaking lock",
3483 ENTITY_NAME(lockers[0].id.name));
3484
3485 ret = ceph_monc_blacklist_add(&client->monc,
3486 &lockers[0].info.addr);
3487 if (ret) {
3488 rbd_warn(rbd_dev, "blacklist of %s%llu failed: %d",
3489 ENTITY_NAME(lockers[0].id.name), ret);
3490 goto out;
3491 }
3492
3493 ret = ceph_cls_break_lock(&client->osdc, &rbd_dev->header_oid,
3494 &rbd_dev->header_oloc, RBD_LOCK_NAME,
3495 lockers[0].id.cookie,
3496 &lockers[0].id.name);
3497 if (ret && ret != -ENOENT)
3498 goto out;
3499
3500again:
3501 ceph_free_lockers(lockers, num_lockers);
3502 }
3503
3504out:
3505 ceph_free_lockers(lockers, num_lockers);
3506 return ret;
3507}
3508
3509/*
3510 * ret is set only if lock_state is RBD_LOCK_STATE_UNLOCKED
3511 */
3512static enum rbd_lock_state rbd_try_acquire_lock(struct rbd_device *rbd_dev,
3513 int *pret)
3514{
3515 enum rbd_lock_state lock_state;
3516
3517 down_read(&rbd_dev->lock_rwsem);
3518 dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev,
3519 rbd_dev->lock_state);
3520 if (__rbd_is_lock_owner(rbd_dev)) {
3521 lock_state = rbd_dev->lock_state;
3522 up_read(&rbd_dev->lock_rwsem);
3523 return lock_state;
3524 }
3525
3526 up_read(&rbd_dev->lock_rwsem);
3527 down_write(&rbd_dev->lock_rwsem);
3528 dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev,
3529 rbd_dev->lock_state);
3530 if (!__rbd_is_lock_owner(rbd_dev)) {
3531 *pret = rbd_try_lock(rbd_dev);
3532 if (*pret)
3533 rbd_warn(rbd_dev, "failed to acquire lock: %d", *pret);
3534 }
3535
3536 lock_state = rbd_dev->lock_state;
3537 up_write(&rbd_dev->lock_rwsem);
3538 return lock_state;
3539}
3540
3541static void rbd_acquire_lock(struct work_struct *work)
3542{
3543 struct rbd_device *rbd_dev = container_of(to_delayed_work(work),
3544 struct rbd_device, lock_dwork);
3545 enum rbd_lock_state lock_state;
3546 int ret;
3547
3548 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3549again:
3550 lock_state = rbd_try_acquire_lock(rbd_dev, &ret);
3551 if (lock_state != RBD_LOCK_STATE_UNLOCKED || ret == -EBLACKLISTED) {
3552 if (lock_state == RBD_LOCK_STATE_LOCKED)
3553 wake_requests(rbd_dev, true);
3554 dout("%s rbd_dev %p lock_state %d ret %d - done\n", __func__,
3555 rbd_dev, lock_state, ret);
3556 return;
3557 }
3558
3559 ret = rbd_request_lock(rbd_dev);
3560 if (ret == -ETIMEDOUT) {
3561 goto again; /* treat this as a dead client */
3562 } else if (ret < 0) {
3563 rbd_warn(rbd_dev, "error requesting lock: %d", ret);
3564 mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork,
3565 RBD_RETRY_DELAY);
3566 } else {
3567 /*
3568 * lock owner acked, but resend if we don't see them
3569 * release the lock
3570 */
3571 dout("%s rbd_dev %p requeueing lock_dwork\n", __func__,
3572 rbd_dev);
3573 mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork,
3574 msecs_to_jiffies(2 * RBD_NOTIFY_TIMEOUT * MSEC_PER_SEC));
3575 }
3576}
3577
3578/*
3579 * lock_rwsem must be held for write
3580 */
3581static bool rbd_release_lock(struct rbd_device *rbd_dev)
3582{
3583 dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev,
3584 rbd_dev->lock_state);
3585 if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED)
3586 return false;
3587
3588 rbd_dev->lock_state = RBD_LOCK_STATE_RELEASING;
3589 downgrade_write(&rbd_dev->lock_rwsem);
3590 /*
3591 * Ensure that all in-flight IO is flushed.
3592 *
3593 * FIXME: ceph_osdc_sync() flushes the entire OSD client, which
3594 * may be shared with other devices.
3595 */
3596 ceph_osdc_sync(&rbd_dev->rbd_client->client->osdc);
3597 up_read(&rbd_dev->lock_rwsem);
3598
3599 down_write(&rbd_dev->lock_rwsem);
3600 dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev,
3601 rbd_dev->lock_state);
3602 if (rbd_dev->lock_state != RBD_LOCK_STATE_RELEASING)
3603 return false;
3604
3605 if (!rbd_unlock(rbd_dev))
3606 /*
3607 * Give others a chance to grab the lock - we would re-acquire
3608 * almost immediately if we got new IO during ceph_osdc_sync()
3609 * otherwise. We need to ack our own notifications, so this
3610 * lock_dwork will be requeued from rbd_wait_state_locked()
3611 * after wake_requests() in rbd_handle_released_lock().
3612 */
3613 cancel_delayed_work(&rbd_dev->lock_dwork);
3614
3615 return true;
3616}
3617
3618static void rbd_release_lock_work(struct work_struct *work)
3619{
3620 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
3621 unlock_work);
3622
3623 down_write(&rbd_dev->lock_rwsem);
3624 rbd_release_lock(rbd_dev);
3625 up_write(&rbd_dev->lock_rwsem);
3626}
3627
3628static void rbd_handle_acquired_lock(struct rbd_device *rbd_dev, u8 struct_v,
3629 void **p)
3630{
3631 struct rbd_client_id cid = { 0 };
3632
3633 if (struct_v >= 2) {
3634 cid.gid = ceph_decode_64(p);
3635 cid.handle = ceph_decode_64(p);
3636 }
3637
3638 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
3639 cid.handle);
3640 if (!rbd_cid_equal(&cid, &rbd_empty_cid)) {
3641 down_write(&rbd_dev->lock_rwsem);
3642 if (rbd_cid_equal(&cid, &rbd_dev->owner_cid)) {
3643 /*
3644 * we already know that the remote client is
3645 * the owner
3646 */
3647 up_write(&rbd_dev->lock_rwsem);
3648 return;
3649 }
3650
3651 rbd_set_owner_cid(rbd_dev, &cid);
3652 downgrade_write(&rbd_dev->lock_rwsem);
3653 } else {
3654 down_read(&rbd_dev->lock_rwsem);
3655 }
3656
3657 if (!__rbd_is_lock_owner(rbd_dev))
3658 wake_requests(rbd_dev, false);
3659 up_read(&rbd_dev->lock_rwsem);
3660}
3661
3662static void rbd_handle_released_lock(struct rbd_device *rbd_dev, u8 struct_v,
3663 void **p)
3664{
3665 struct rbd_client_id cid = { 0 };
3666
3667 if (struct_v >= 2) {
3668 cid.gid = ceph_decode_64(p);
3669 cid.handle = ceph_decode_64(p);
3670 }
3671
3672 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
3673 cid.handle);
3674 if (!rbd_cid_equal(&cid, &rbd_empty_cid)) {
3675 down_write(&rbd_dev->lock_rwsem);
3676 if (!rbd_cid_equal(&cid, &rbd_dev->owner_cid)) {
3677 dout("%s rbd_dev %p unexpected owner, cid %llu-%llu != owner_cid %llu-%llu\n",
3678 __func__, rbd_dev, cid.gid, cid.handle,
3679 rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle);
3680 up_write(&rbd_dev->lock_rwsem);
3681 return;
3682 }
3683
3684 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
3685 downgrade_write(&rbd_dev->lock_rwsem);
3686 } else {
3687 down_read(&rbd_dev->lock_rwsem);
3688 }
3689
3690 if (!__rbd_is_lock_owner(rbd_dev))
3691 wake_requests(rbd_dev, false);
3692 up_read(&rbd_dev->lock_rwsem);
3693}
3694
3695static bool rbd_handle_request_lock(struct rbd_device *rbd_dev, u8 struct_v,
3696 void **p)
3697{
3698 struct rbd_client_id my_cid = rbd_get_cid(rbd_dev);
3699 struct rbd_client_id cid = { 0 };
3700 bool need_to_send;
3701
3702 if (struct_v >= 2) {
3703 cid.gid = ceph_decode_64(p);
3704 cid.handle = ceph_decode_64(p);
3705 }
3706
3707 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
3708 cid.handle);
3709 if (rbd_cid_equal(&cid, &my_cid))
3710 return false;
3711
3712 down_read(&rbd_dev->lock_rwsem);
3713 need_to_send = __rbd_is_lock_owner(rbd_dev);
3714 if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) {
3715 if (!rbd_cid_equal(&rbd_dev->owner_cid, &rbd_empty_cid)) {
3716 dout("%s rbd_dev %p queueing unlock_work\n", __func__,
3717 rbd_dev);
3718 queue_work(rbd_dev->task_wq, &rbd_dev->unlock_work);
3719 }
3720 }
3721 up_read(&rbd_dev->lock_rwsem);
3722 return need_to_send;
3723}
3724
3725static void __rbd_acknowledge_notify(struct rbd_device *rbd_dev,
3726 u64 notify_id, u64 cookie, s32 *result)
3727{
3728 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3729 int buf_size = 4 + CEPH_ENCODING_START_BLK_LEN;
3730 char buf[buf_size];
3731 int ret;
3732
3733 if (result) {
3734 void *p = buf;
3735
3736 /* encode ResponseMessage */
3737 ceph_start_encoding(&p, 1, 1,
3738 buf_size - CEPH_ENCODING_START_BLK_LEN);
3739 ceph_encode_32(&p, *result);
3740 } else {
3741 buf_size = 0;
3742 }
3743
3744 ret = ceph_osdc_notify_ack(osdc, &rbd_dev->header_oid,
3745 &rbd_dev->header_oloc, notify_id, cookie,
3746 buf, buf_size);
3747 if (ret)
3748 rbd_warn(rbd_dev, "acknowledge_notify failed: %d", ret);
3749}
3750
3751static void rbd_acknowledge_notify(struct rbd_device *rbd_dev, u64 notify_id,
3752 u64 cookie)
3753{
3754 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3755 __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, NULL);
3756}
3757
3758static void rbd_acknowledge_notify_result(struct rbd_device *rbd_dev,
3759 u64 notify_id, u64 cookie, s32 result)
3760{
3761 dout("%s rbd_dev %p result %d\n", __func__, rbd_dev, result);
3762 __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, &result);
3763}
3764
Ilya Dryomov922dab62016-05-26 01:15:02 +02003765static void rbd_watch_cb(void *arg, u64 notify_id, u64 cookie,
3766 u64 notifier_id, void *data, size_t data_len)
Alex Elderb8d70032012-11-30 17:53:04 -06003767{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003768 struct rbd_device *rbd_dev = arg;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003769 void *p = data;
3770 void *const end = p + data_len;
3771 u8 struct_v;
3772 u32 len;
3773 u32 notify_op;
Alex Elderb8d70032012-11-30 17:53:04 -06003774 int ret;
3775
Ilya Dryomoved95b212016-08-12 16:40:02 +02003776 dout("%s rbd_dev %p cookie %llu notify_id %llu data_len %zu\n",
3777 __func__, rbd_dev, cookie, notify_id, data_len);
3778 if (data_len) {
3779 ret = ceph_start_decoding(&p, end, 1, "NotifyMessage",
3780 &struct_v, &len);
3781 if (ret) {
3782 rbd_warn(rbd_dev, "failed to decode NotifyMessage: %d",
3783 ret);
3784 return;
3785 }
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04003786
Ilya Dryomoved95b212016-08-12 16:40:02 +02003787 notify_op = ceph_decode_32(&p);
3788 } else {
3789 /* legacy notification for header updates */
3790 notify_op = RBD_NOTIFY_OP_HEADER_UPDATE;
3791 len = 0;
3792 }
Alex Elderb8d70032012-11-30 17:53:04 -06003793
Ilya Dryomoved95b212016-08-12 16:40:02 +02003794 dout("%s rbd_dev %p notify_op %u\n", __func__, rbd_dev, notify_op);
3795 switch (notify_op) {
3796 case RBD_NOTIFY_OP_ACQUIRED_LOCK:
3797 rbd_handle_acquired_lock(rbd_dev, struct_v, &p);
3798 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3799 break;
3800 case RBD_NOTIFY_OP_RELEASED_LOCK:
3801 rbd_handle_released_lock(rbd_dev, struct_v, &p);
3802 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3803 break;
3804 case RBD_NOTIFY_OP_REQUEST_LOCK:
3805 if (rbd_handle_request_lock(rbd_dev, struct_v, &p))
3806 /*
3807 * send ResponseMessage(0) back so the client
3808 * can detect a missing owner
3809 */
3810 rbd_acknowledge_notify_result(rbd_dev, notify_id,
3811 cookie, 0);
3812 else
3813 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3814 break;
3815 case RBD_NOTIFY_OP_HEADER_UPDATE:
3816 ret = rbd_dev_refresh(rbd_dev);
3817 if (ret)
3818 rbd_warn(rbd_dev, "refresh failed: %d", ret);
3819
3820 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3821 break;
3822 default:
3823 if (rbd_is_lock_owner(rbd_dev))
3824 rbd_acknowledge_notify_result(rbd_dev, notify_id,
3825 cookie, -EOPNOTSUPP);
3826 else
3827 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3828 break;
3829 }
Alex Elderb8d70032012-11-30 17:53:04 -06003830}
3831
Ilya Dryomov99d16942016-08-12 16:11:41 +02003832static void __rbd_unregister_watch(struct rbd_device *rbd_dev);
3833
Ilya Dryomov922dab62016-05-26 01:15:02 +02003834static void rbd_watch_errcb(void *arg, u64 cookie, int err)
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003835{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003836 struct rbd_device *rbd_dev = arg;
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003837
Ilya Dryomov922dab62016-05-26 01:15:02 +02003838 rbd_warn(rbd_dev, "encountered watch error: %d", err);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003839
Ilya Dryomoved95b212016-08-12 16:40:02 +02003840 down_write(&rbd_dev->lock_rwsem);
3841 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
3842 up_write(&rbd_dev->lock_rwsem);
3843
Ilya Dryomov99d16942016-08-12 16:11:41 +02003844 mutex_lock(&rbd_dev->watch_mutex);
3845 if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) {
3846 __rbd_unregister_watch(rbd_dev);
3847 rbd_dev->watch_state = RBD_WATCH_STATE_ERROR;
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003848
Ilya Dryomov99d16942016-08-12 16:11:41 +02003849 queue_delayed_work(rbd_dev->task_wq, &rbd_dev->watch_dwork, 0);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003850 }
Ilya Dryomov99d16942016-08-12 16:11:41 +02003851 mutex_unlock(&rbd_dev->watch_mutex);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003852}
3853
3854/*
Ilya Dryomov99d16942016-08-12 16:11:41 +02003855 * watch_mutex must be locked
Alex Elder9969ebc2013-01-18 12:31:10 -06003856 */
Ilya Dryomov99d16942016-08-12 16:11:41 +02003857static int __rbd_register_watch(struct rbd_device *rbd_dev)
Alex Elder9969ebc2013-01-18 12:31:10 -06003858{
3859 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Ilya Dryomov922dab62016-05-26 01:15:02 +02003860 struct ceph_osd_linger_request *handle;
Alex Elder9969ebc2013-01-18 12:31:10 -06003861
Ilya Dryomov922dab62016-05-26 01:15:02 +02003862 rbd_assert(!rbd_dev->watch_handle);
Ilya Dryomov99d16942016-08-12 16:11:41 +02003863 dout("%s rbd_dev %p\n", __func__, rbd_dev);
Alex Elder9969ebc2013-01-18 12:31:10 -06003864
Ilya Dryomov922dab62016-05-26 01:15:02 +02003865 handle = ceph_osdc_watch(osdc, &rbd_dev->header_oid,
3866 &rbd_dev->header_oloc, rbd_watch_cb,
3867 rbd_watch_errcb, rbd_dev);
3868 if (IS_ERR(handle))
3869 return PTR_ERR(handle);
Alex Elder9969ebc2013-01-18 12:31:10 -06003870
Ilya Dryomov922dab62016-05-26 01:15:02 +02003871 rbd_dev->watch_handle = handle;
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003872 return 0;
Alex Elder9969ebc2013-01-18 12:31:10 -06003873}
3874
Ilya Dryomov99d16942016-08-12 16:11:41 +02003875/*
3876 * watch_mutex must be locked
3877 */
3878static void __rbd_unregister_watch(struct rbd_device *rbd_dev)
Ilya Dryomovfca27062013-12-16 18:02:40 +02003879{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003880 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3881 int ret;
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003882
Ilya Dryomov99d16942016-08-12 16:11:41 +02003883 rbd_assert(rbd_dev->watch_handle);
3884 dout("%s rbd_dev %p\n", __func__, rbd_dev);
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003885
Ilya Dryomov922dab62016-05-26 01:15:02 +02003886 ret = ceph_osdc_unwatch(osdc, rbd_dev->watch_handle);
3887 if (ret)
3888 rbd_warn(rbd_dev, "failed to unwatch: %d", ret);
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003889
Ilya Dryomov922dab62016-05-26 01:15:02 +02003890 rbd_dev->watch_handle = NULL;
Ilya Dryomovc525f032016-04-28 16:07:26 +02003891}
3892
Ilya Dryomov99d16942016-08-12 16:11:41 +02003893static int rbd_register_watch(struct rbd_device *rbd_dev)
Ilya Dryomovc525f032016-04-28 16:07:26 +02003894{
Ilya Dryomov99d16942016-08-12 16:11:41 +02003895 int ret;
Ilya Dryomov811c6682016-04-15 16:22:16 +02003896
Ilya Dryomov99d16942016-08-12 16:11:41 +02003897 mutex_lock(&rbd_dev->watch_mutex);
3898 rbd_assert(rbd_dev->watch_state == RBD_WATCH_STATE_UNREGISTERED);
3899 ret = __rbd_register_watch(rbd_dev);
3900 if (ret)
3901 goto out;
3902
3903 rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED;
3904 rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id;
3905
3906out:
3907 mutex_unlock(&rbd_dev->watch_mutex);
3908 return ret;
3909}
3910
3911static void cancel_tasks_sync(struct rbd_device *rbd_dev)
3912{
3913 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3914
3915 cancel_delayed_work_sync(&rbd_dev->watch_dwork);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003916 cancel_work_sync(&rbd_dev->acquired_lock_work);
3917 cancel_work_sync(&rbd_dev->released_lock_work);
3918 cancel_delayed_work_sync(&rbd_dev->lock_dwork);
3919 cancel_work_sync(&rbd_dev->unlock_work);
Ilya Dryomov99d16942016-08-12 16:11:41 +02003920}
3921
3922static void rbd_unregister_watch(struct rbd_device *rbd_dev)
3923{
Ilya Dryomoved95b212016-08-12 16:40:02 +02003924 WARN_ON(waitqueue_active(&rbd_dev->lock_waitq));
Ilya Dryomov99d16942016-08-12 16:11:41 +02003925 cancel_tasks_sync(rbd_dev);
3926
3927 mutex_lock(&rbd_dev->watch_mutex);
3928 if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED)
3929 __rbd_unregister_watch(rbd_dev);
3930 rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED;
3931 mutex_unlock(&rbd_dev->watch_mutex);
3932
Ilya Dryomov811c6682016-04-15 16:22:16 +02003933 ceph_osdc_flush_notifies(&rbd_dev->rbd_client->client->osdc);
Ilya Dryomovfca27062013-12-16 18:02:40 +02003934}
3935
Ilya Dryomov99d16942016-08-12 16:11:41 +02003936static void rbd_reregister_watch(struct work_struct *work)
3937{
3938 struct rbd_device *rbd_dev = container_of(to_delayed_work(work),
3939 struct rbd_device, watch_dwork);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003940 bool was_lock_owner = false;
Ilya Dryomov99d16942016-08-12 16:11:41 +02003941 int ret;
3942
3943 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3944
Ilya Dryomoved95b212016-08-12 16:40:02 +02003945 down_write(&rbd_dev->lock_rwsem);
3946 if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED)
3947 was_lock_owner = rbd_release_lock(rbd_dev);
3948
Ilya Dryomov99d16942016-08-12 16:11:41 +02003949 mutex_lock(&rbd_dev->watch_mutex);
3950 if (rbd_dev->watch_state != RBD_WATCH_STATE_ERROR)
3951 goto fail_unlock;
3952
3953 ret = __rbd_register_watch(rbd_dev);
3954 if (ret) {
3955 rbd_warn(rbd_dev, "failed to reregister watch: %d", ret);
3956 if (ret != -EBLACKLISTED)
3957 queue_delayed_work(rbd_dev->task_wq,
3958 &rbd_dev->watch_dwork,
3959 RBD_RETRY_DELAY);
3960 goto fail_unlock;
3961 }
3962
3963 rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED;
3964 rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id;
3965 mutex_unlock(&rbd_dev->watch_mutex);
3966
3967 ret = rbd_dev_refresh(rbd_dev);
3968 if (ret)
3969 rbd_warn(rbd_dev, "reregisteration refresh failed: %d", ret);
3970
Ilya Dryomoved95b212016-08-12 16:40:02 +02003971 if (was_lock_owner) {
3972 ret = rbd_try_lock(rbd_dev);
3973 if (ret)
3974 rbd_warn(rbd_dev, "reregisteration lock failed: %d",
3975 ret);
3976 }
3977
3978 up_write(&rbd_dev->lock_rwsem);
3979 wake_requests(rbd_dev, true);
Ilya Dryomov99d16942016-08-12 16:11:41 +02003980 return;
3981
3982fail_unlock:
3983 mutex_unlock(&rbd_dev->watch_mutex);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003984 up_write(&rbd_dev->lock_rwsem);
Ilya Dryomov99d16942016-08-12 16:11:41 +02003985}
3986
Alex Elder36be9a72013-01-19 00:30:28 -06003987/*
Alex Elderf40eb342013-04-25 15:09:42 -05003988 * Synchronous osd object method call. Returns the number of bytes
3989 * returned in the outbound buffer, or a negative error code.
Alex Elder36be9a72013-01-19 00:30:28 -06003990 */
3991static int rbd_obj_method_sync(struct rbd_device *rbd_dev,
3992 const char *object_name,
3993 const char *class_name,
3994 const char *method_name,
Alex Elder41579762013-04-21 12:14:45 -05003995 const void *outbound,
Alex Elder36be9a72013-01-19 00:30:28 -06003996 size_t outbound_size,
Alex Elder41579762013-04-21 12:14:45 -05003997 void *inbound,
Alex Eldere2a58ee2013-04-30 00:44:33 -05003998 size_t inbound_size)
Alex Elder36be9a72013-01-19 00:30:28 -06003999{
Alex Elder21692382013-04-05 01:27:12 -05004000 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder36be9a72013-01-19 00:30:28 -06004001 struct rbd_obj_request *obj_request;
Alex Elder36be9a72013-01-19 00:30:28 -06004002 struct page **pages;
4003 u32 page_count;
4004 int ret;
4005
4006 /*
Alex Elder6010a452013-04-05 01:27:11 -05004007 * Method calls are ultimately read operations. The result
4008 * should placed into the inbound buffer provided. They
4009 * also supply outbound data--parameters for the object
4010 * method. Currently if this is present it will be a
4011 * snapshot id.
Alex Elder36be9a72013-01-19 00:30:28 -06004012 */
Alex Elder57385b52013-04-21 12:14:45 -05004013 page_count = (u32)calc_pages_for(0, inbound_size);
Alex Elder36be9a72013-01-19 00:30:28 -06004014 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
4015 if (IS_ERR(pages))
4016 return PTR_ERR(pages);
4017
4018 ret = -ENOMEM;
Alex Elder6010a452013-04-05 01:27:11 -05004019 obj_request = rbd_obj_request_create(object_name, 0, inbound_size,
Alex Elder36be9a72013-01-19 00:30:28 -06004020 OBJ_REQUEST_PAGES);
4021 if (!obj_request)
4022 goto out;
4023
4024 obj_request->pages = pages;
4025 obj_request->page_count = page_count;
4026
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004027 obj_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_READ, 1,
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02004028 obj_request);
Alex Elder36be9a72013-01-19 00:30:28 -06004029 if (!obj_request->osd_req)
4030 goto out;
4031
Alex Elderc99d2d42013-04-05 01:27:11 -05004032 osd_req_op_cls_init(obj_request->osd_req, 0, CEPH_OSD_OP_CALL,
Alex Elder04017e22013-04-05 14:46:02 -05004033 class_name, method_name);
4034 if (outbound_size) {
4035 struct ceph_pagelist *pagelist;
4036
4037 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
4038 if (!pagelist)
4039 goto out;
4040
4041 ceph_pagelist_init(pagelist);
4042 ceph_pagelist_append(pagelist, outbound, outbound_size);
4043 osd_req_op_cls_request_data_pagelist(obj_request->osd_req, 0,
4044 pagelist);
4045 }
Alex Eldera4ce40a2013-04-05 01:27:12 -05004046 osd_req_op_cls_response_data_pages(obj_request->osd_req, 0,
4047 obj_request->pages, inbound_size,
Alex Elder44cd1882013-04-05 01:27:12 -05004048 0, false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05004049 rbd_osd_req_format_read(obj_request);
Alex Elder430c28c2013-04-03 21:32:51 -05004050
Alex Elder36be9a72013-01-19 00:30:28 -06004051 ret = rbd_obj_request_submit(osdc, obj_request);
4052 if (ret)
4053 goto out;
4054 ret = rbd_obj_request_wait(obj_request);
4055 if (ret)
4056 goto out;
4057
4058 ret = obj_request->result;
4059 if (ret < 0)
4060 goto out;
Alex Elder57385b52013-04-21 12:14:45 -05004061
4062 rbd_assert(obj_request->xferred < (u64)INT_MAX);
4063 ret = (int)obj_request->xferred;
Alex Elder903bb322013-02-06 13:11:38 -06004064 ceph_copy_from_page_vector(pages, inbound, 0, obj_request->xferred);
Alex Elder36be9a72013-01-19 00:30:28 -06004065out:
4066 if (obj_request)
4067 rbd_obj_request_put(obj_request);
4068 else
4069 ceph_release_page_vector(pages, page_count);
4070
4071 return ret;
4072}
4073
Ilya Dryomoved95b212016-08-12 16:40:02 +02004074/*
4075 * lock_rwsem must be held for read
4076 */
4077static void rbd_wait_state_locked(struct rbd_device *rbd_dev)
4078{
4079 DEFINE_WAIT(wait);
4080
4081 do {
4082 /*
4083 * Note the use of mod_delayed_work() in rbd_acquire_lock()
4084 * and cancel_delayed_work() in wake_requests().
4085 */
4086 dout("%s rbd_dev %p queueing lock_dwork\n", __func__, rbd_dev);
4087 queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0);
4088 prepare_to_wait_exclusive(&rbd_dev->lock_waitq, &wait,
4089 TASK_UNINTERRUPTIBLE);
4090 up_read(&rbd_dev->lock_rwsem);
4091 schedule();
4092 down_read(&rbd_dev->lock_rwsem);
4093 } while (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED);
4094 finish_wait(&rbd_dev->lock_waitq, &wait);
4095}
4096
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004097static void rbd_queue_workfn(struct work_struct *work)
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004098{
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004099 struct request *rq = blk_mq_rq_from_pdu(work);
4100 struct rbd_device *rbd_dev = rq->q->queuedata;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004101 struct rbd_img_request *img_request;
Josh Durgin4e752f02014-04-08 11:12:11 -07004102 struct ceph_snap_context *snapc = NULL;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004103 u64 offset = (u64)blk_rq_pos(rq) << SECTOR_SHIFT;
4104 u64 length = blk_rq_bytes(rq);
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004105 enum obj_operation_type op_type;
Josh Durgin4e752f02014-04-08 11:12:11 -07004106 u64 mapping_size;
Ilya Dryomoved95b212016-08-12 16:40:02 +02004107 bool must_be_locked = false;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004108 int result;
4109
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004110 if (rq->cmd_type != REQ_TYPE_FS) {
4111 dout("%s: non-fs request type %d\n", __func__,
4112 (int) rq->cmd_type);
4113 result = -EIO;
4114 goto err;
4115 }
4116
Mike Christiec2df40d2016-06-05 14:32:17 -05004117 if (req_op(rq) == REQ_OP_DISCARD)
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004118 op_type = OBJ_OP_DISCARD;
Mike Christiec2df40d2016-06-05 14:32:17 -05004119 else if (req_op(rq) == REQ_OP_WRITE)
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004120 op_type = OBJ_OP_WRITE;
4121 else
4122 op_type = OBJ_OP_READ;
4123
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004124 /* Ignore/skip any zero-length requests */
4125
4126 if (!length) {
4127 dout("%s: zero-length request\n", __func__);
4128 result = 0;
4129 goto err_rq;
4130 }
4131
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004132 /* Only reads are allowed to a read-only device */
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004133
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004134 if (op_type != OBJ_OP_READ) {
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004135 if (rbd_dev->mapping.read_only) {
4136 result = -EROFS;
4137 goto err_rq;
4138 }
4139 rbd_assert(rbd_dev->spec->snap_id == CEPH_NOSNAP);
4140 }
4141
4142 /*
4143 * Quit early if the mapped snapshot no longer exists. It's
4144 * still possible the snapshot will have disappeared by the
4145 * time our request arrives at the osd, but there's no sense in
4146 * sending it if we already know.
4147 */
4148 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) {
4149 dout("request for non-existent snapshot");
4150 rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP);
4151 result = -ENXIO;
4152 goto err_rq;
4153 }
4154
4155 if (offset && length > U64_MAX - offset + 1) {
4156 rbd_warn(rbd_dev, "bad request range (%llu~%llu)", offset,
4157 length);
4158 result = -EINVAL;
4159 goto err_rq; /* Shouldn't happen */
4160 }
4161
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004162 blk_mq_start_request(rq);
4163
Josh Durgin4e752f02014-04-08 11:12:11 -07004164 down_read(&rbd_dev->header_rwsem);
4165 mapping_size = rbd_dev->mapping.size;
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004166 if (op_type != OBJ_OP_READ) {
Josh Durgin4e752f02014-04-08 11:12:11 -07004167 snapc = rbd_dev->header.snapc;
4168 ceph_get_snap_context(snapc);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004169 must_be_locked = rbd_is_lock_supported(rbd_dev);
Josh Durgin4e752f02014-04-08 11:12:11 -07004170 }
4171 up_read(&rbd_dev->header_rwsem);
4172
4173 if (offset + length > mapping_size) {
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004174 rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)", offset,
Josh Durgin4e752f02014-04-08 11:12:11 -07004175 length, mapping_size);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004176 result = -EIO;
4177 goto err_rq;
4178 }
4179
Ilya Dryomoved95b212016-08-12 16:40:02 +02004180 if (must_be_locked) {
4181 down_read(&rbd_dev->lock_rwsem);
4182 if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED)
4183 rbd_wait_state_locked(rbd_dev);
4184 }
4185
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004186 img_request = rbd_img_request_create(rbd_dev, offset, length, op_type,
Josh Durgin4e752f02014-04-08 11:12:11 -07004187 snapc);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004188 if (!img_request) {
4189 result = -ENOMEM;
Ilya Dryomoved95b212016-08-12 16:40:02 +02004190 goto err_unlock;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004191 }
4192 img_request->rq = rq;
Ilya Dryomov70b16db2015-11-27 19:23:24 +01004193 snapc = NULL; /* img_request consumes a ref */
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004194
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004195 if (op_type == OBJ_OP_DISCARD)
4196 result = rbd_img_request_fill(img_request, OBJ_REQUEST_NODATA,
4197 NULL);
4198 else
4199 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
4200 rq->bio);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004201 if (result)
4202 goto err_img_request;
4203
4204 result = rbd_img_request_submit(img_request);
4205 if (result)
4206 goto err_img_request;
4207
Ilya Dryomoved95b212016-08-12 16:40:02 +02004208 if (must_be_locked)
4209 up_read(&rbd_dev->lock_rwsem);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004210 return;
4211
4212err_img_request:
4213 rbd_img_request_put(img_request);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004214err_unlock:
4215 if (must_be_locked)
4216 up_read(&rbd_dev->lock_rwsem);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004217err_rq:
4218 if (result)
4219 rbd_warn(rbd_dev, "%s %llx at %llx result %d",
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004220 obj_op_name(op_type), length, offset, result);
SF Markus Elfringe96a6502014-11-02 15:20:59 +01004221 ceph_put_snap_context(snapc);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004222err:
4223 blk_mq_end_request(rq, result);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004224}
4225
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004226static int rbd_queue_rq(struct blk_mq_hw_ctx *hctx,
4227 const struct blk_mq_queue_data *bd)
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004228{
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004229 struct request *rq = bd->rq;
4230 struct work_struct *work = blk_mq_rq_to_pdu(rq);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004231
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004232 queue_work(rbd_wq, work);
4233 return BLK_MQ_RQ_QUEUE_OK;
Alex Elderbf0d5f502012-11-22 00:00:08 -06004234}
4235
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004236static void rbd_free_disk(struct rbd_device *rbd_dev)
4237{
4238 struct gendisk *disk = rbd_dev->disk;
4239
4240 if (!disk)
4241 return;
4242
Alex Eldera0cab922013-04-25 23:15:08 -05004243 rbd_dev->disk = NULL;
4244 if (disk->flags & GENHD_FL_UP) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004245 del_gendisk(disk);
Alex Eldera0cab922013-04-25 23:15:08 -05004246 if (disk->queue)
4247 blk_cleanup_queue(disk->queue);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004248 blk_mq_free_tag_set(&rbd_dev->tag_set);
Alex Eldera0cab922013-04-25 23:15:08 -05004249 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004250 put_disk(disk);
4251}
4252
Alex Elder788e2df2013-01-17 12:25:27 -06004253static int rbd_obj_read_sync(struct rbd_device *rbd_dev,
4254 const char *object_name,
Alex Elder7097f8d2013-04-30 00:44:33 -05004255 u64 offset, u64 length, void *buf)
Alex Elder788e2df2013-01-17 12:25:27 -06004256
4257{
Alex Elder21692382013-04-05 01:27:12 -05004258 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder788e2df2013-01-17 12:25:27 -06004259 struct rbd_obj_request *obj_request;
Alex Elder788e2df2013-01-17 12:25:27 -06004260 struct page **pages = NULL;
4261 u32 page_count;
Alex Elder1ceae7e2013-02-06 13:11:38 -06004262 size_t size;
Alex Elder788e2df2013-01-17 12:25:27 -06004263 int ret;
4264
4265 page_count = (u32) calc_pages_for(offset, length);
4266 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
4267 if (IS_ERR(pages))
Jan Karaa8d42052014-10-22 09:17:24 +02004268 return PTR_ERR(pages);
Alex Elder788e2df2013-01-17 12:25:27 -06004269
4270 ret = -ENOMEM;
4271 obj_request = rbd_obj_request_create(object_name, offset, length,
Alex Elder36be9a72013-01-19 00:30:28 -06004272 OBJ_REQUEST_PAGES);
Alex Elder788e2df2013-01-17 12:25:27 -06004273 if (!obj_request)
4274 goto out;
4275
4276 obj_request->pages = pages;
4277 obj_request->page_count = page_count;
4278
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004279 obj_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_READ, 1,
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02004280 obj_request);
Alex Elder788e2df2013-01-17 12:25:27 -06004281 if (!obj_request->osd_req)
4282 goto out;
4283
Alex Elderc99d2d42013-04-05 01:27:11 -05004284 osd_req_op_extent_init(obj_request->osd_req, 0, CEPH_OSD_OP_READ,
4285 offset, length, 0, 0);
Alex Elder406e2c92013-04-15 14:50:36 -05004286 osd_req_op_extent_osd_data_pages(obj_request->osd_req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05004287 obj_request->pages,
Alex Elder44cd1882013-04-05 01:27:12 -05004288 obj_request->length,
4289 obj_request->offset & ~PAGE_MASK,
4290 false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05004291 rbd_osd_req_format_read(obj_request);
Alex Elder430c28c2013-04-03 21:32:51 -05004292
Alex Elder788e2df2013-01-17 12:25:27 -06004293 ret = rbd_obj_request_submit(osdc, obj_request);
4294 if (ret)
4295 goto out;
4296 ret = rbd_obj_request_wait(obj_request);
4297 if (ret)
4298 goto out;
4299
4300 ret = obj_request->result;
4301 if (ret < 0)
4302 goto out;
Alex Elder1ceae7e2013-02-06 13:11:38 -06004303
4304 rbd_assert(obj_request->xferred <= (u64) SIZE_MAX);
4305 size = (size_t) obj_request->xferred;
Alex Elder903bb322013-02-06 13:11:38 -06004306 ceph_copy_from_page_vector(pages, buf, 0, size);
Alex Elder7097f8d2013-04-30 00:44:33 -05004307 rbd_assert(size <= (size_t)INT_MAX);
4308 ret = (int)size;
Alex Elder788e2df2013-01-17 12:25:27 -06004309out:
4310 if (obj_request)
4311 rbd_obj_request_put(obj_request);
4312 else
4313 ceph_release_page_vector(pages, page_count);
4314
4315 return ret;
4316}
4317
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004318/*
Alex Elder662518b2013-05-06 09:51:29 -05004319 * Read the complete header for the given rbd device. On successful
4320 * return, the rbd_dev->header field will contain up-to-date
4321 * information about the image.
Alex Elder4156d992012-08-02 11:29:46 -05004322 */
Alex Elder99a41eb2013-05-06 09:51:30 -05004323static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev)
Alex Elder4156d992012-08-02 11:29:46 -05004324{
4325 struct rbd_image_header_ondisk *ondisk = NULL;
4326 u32 snap_count = 0;
4327 u64 names_size = 0;
4328 u32 want_count;
4329 int ret;
4330
4331 /*
4332 * The complete header will include an array of its 64-bit
4333 * snapshot ids, followed by the names of those snapshots as
4334 * a contiguous block of NUL-terminated strings. Note that
4335 * the number of snapshots could change by the time we read
4336 * it in, in which case we re-read it.
4337 */
4338 do {
4339 size_t size;
4340
4341 kfree(ondisk);
4342
4343 size = sizeof (*ondisk);
4344 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
4345 size += names_size;
4346 ondisk = kmalloc(size, GFP_KERNEL);
4347 if (!ondisk)
Alex Elder662518b2013-05-06 09:51:29 -05004348 return -ENOMEM;
Alex Elder4156d992012-08-02 11:29:46 -05004349
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004350 ret = rbd_obj_read_sync(rbd_dev, rbd_dev->header_oid.name,
Alex Elder7097f8d2013-04-30 00:44:33 -05004351 0, size, ondisk);
Alex Elder4156d992012-08-02 11:29:46 -05004352 if (ret < 0)
Alex Elder662518b2013-05-06 09:51:29 -05004353 goto out;
Alex Elderc0cd10db2013-04-26 09:43:47 -05004354 if ((size_t)ret < size) {
Alex Elder4156d992012-08-02 11:29:46 -05004355 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05004356 rbd_warn(rbd_dev, "short header read (want %zd got %d)",
4357 size, ret);
Alex Elder662518b2013-05-06 09:51:29 -05004358 goto out;
Alex Elder4156d992012-08-02 11:29:46 -05004359 }
4360 if (!rbd_dev_ondisk_valid(ondisk)) {
4361 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05004362 rbd_warn(rbd_dev, "invalid header");
Alex Elder662518b2013-05-06 09:51:29 -05004363 goto out;
Alex Elder4156d992012-08-02 11:29:46 -05004364 }
4365
4366 names_size = le64_to_cpu(ondisk->snap_names_len);
4367 want_count = snap_count;
4368 snap_count = le32_to_cpu(ondisk->snap_count);
4369 } while (snap_count != want_count);
4370
Alex Elder662518b2013-05-06 09:51:29 -05004371 ret = rbd_header_from_disk(rbd_dev, ondisk);
4372out:
Alex Elder4156d992012-08-02 11:29:46 -05004373 kfree(ondisk);
4374
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004375 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004376}
4377
Alex Elder15228ed2013-05-01 12:43:03 -05004378/*
4379 * Clear the rbd device's EXISTS flag if the snapshot it's mapped to
4380 * has disappeared from the (just updated) snapshot context.
4381 */
4382static void rbd_exists_validate(struct rbd_device *rbd_dev)
4383{
4384 u64 snap_id;
4385
4386 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags))
4387 return;
4388
4389 snap_id = rbd_dev->spec->snap_id;
4390 if (snap_id == CEPH_NOSNAP)
4391 return;
4392
4393 if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX)
4394 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
4395}
4396
Josh Durgin98752012013-08-29 17:26:31 -07004397static void rbd_dev_update_size(struct rbd_device *rbd_dev)
4398{
4399 sector_t size;
Josh Durgin98752012013-08-29 17:26:31 -07004400
4401 /*
Ilya Dryomov811c6682016-04-15 16:22:16 +02004402 * If EXISTS is not set, rbd_dev->disk may be NULL, so don't
4403 * try to update its size. If REMOVING is set, updating size
4404 * is just useless work since the device can't be opened.
Josh Durgin98752012013-08-29 17:26:31 -07004405 */
Ilya Dryomov811c6682016-04-15 16:22:16 +02004406 if (test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags) &&
4407 !test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) {
Josh Durgin98752012013-08-29 17:26:31 -07004408 size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE;
4409 dout("setting size to %llu sectors", (unsigned long long)size);
4410 set_capacity(rbd_dev->disk, size);
4411 revalidate_disk(rbd_dev->disk);
4412 }
4413}
4414
Alex Eldercc4a38bd2013-04-30 00:44:33 -05004415static int rbd_dev_refresh(struct rbd_device *rbd_dev)
Alex Elder1fe5e992012-07-25 09:32:41 -05004416{
Alex Eldere627db02013-05-06 07:40:30 -05004417 u64 mapping_size;
Alex Elder1fe5e992012-07-25 09:32:41 -05004418 int ret;
4419
Alex Eldercfbf6372013-05-31 17:40:45 -05004420 down_write(&rbd_dev->header_rwsem);
Alex Elder3b5cf2a2013-05-29 11:18:59 -05004421 mapping_size = rbd_dev->mapping.size;
Ilya Dryomova720ae02014-07-23 17:11:19 +04004422
4423 ret = rbd_dev_header_info(rbd_dev);
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04004424 if (ret)
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004425 goto out;
Alex Elder15228ed2013-05-01 12:43:03 -05004426
Ilya Dryomove8f59b52014-07-24 10:42:13 +04004427 /*
4428 * If there is a parent, see if it has disappeared due to the
4429 * mapped image getting flattened.
4430 */
4431 if (rbd_dev->parent) {
4432 ret = rbd_dev_v2_parent_info(rbd_dev);
4433 if (ret)
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004434 goto out;
Ilya Dryomove8f59b52014-07-24 10:42:13 +04004435 }
4436
Ilya Dryomov5ff11082014-07-23 17:11:21 +04004437 if (rbd_dev->spec->snap_id == CEPH_NOSNAP) {
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004438 rbd_dev->mapping.size = rbd_dev->header.image_size;
Ilya Dryomov5ff11082014-07-23 17:11:21 +04004439 } else {
4440 /* validate mapped snapshot's EXISTS flag */
4441 rbd_exists_validate(rbd_dev);
4442 }
Alex Elder15228ed2013-05-01 12:43:03 -05004443
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004444out:
Alex Eldercfbf6372013-05-31 17:40:45 -05004445 up_write(&rbd_dev->header_rwsem);
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004446 if (!ret && mapping_size != rbd_dev->mapping.size)
Josh Durgin98752012013-08-29 17:26:31 -07004447 rbd_dev_update_size(rbd_dev);
Alex Elder1fe5e992012-07-25 09:32:41 -05004448
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004449 return ret;
Alex Elder1fe5e992012-07-25 09:32:41 -05004450}
4451
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004452static int rbd_init_request(void *data, struct request *rq,
4453 unsigned int hctx_idx, unsigned int request_idx,
4454 unsigned int numa_node)
4455{
4456 struct work_struct *work = blk_mq_rq_to_pdu(rq);
4457
4458 INIT_WORK(work, rbd_queue_workfn);
4459 return 0;
4460}
4461
4462static struct blk_mq_ops rbd_mq_ops = {
4463 .queue_rq = rbd_queue_rq,
4464 .map_queue = blk_mq_map_queue,
4465 .init_request = rbd_init_request,
4466};
4467
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004468static int rbd_init_disk(struct rbd_device *rbd_dev)
4469{
4470 struct gendisk *disk;
4471 struct request_queue *q;
Alex Elder593a9e72012-02-07 12:03:37 -06004472 u64 segment_size;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004473 int err;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004474
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004475 /* create gendisk info */
Ilya Dryomov7e513d42013-12-16 19:26:32 +02004476 disk = alloc_disk(single_major ?
4477 (1 << RBD_SINGLE_MAJOR_PART_SHIFT) :
4478 RBD_MINORS_PER_MAJOR);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004479 if (!disk)
Alex Elder1fcdb8a2012-08-29 17:11:06 -05004480 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004481
Alex Elderf0f8cef2012-01-29 13:57:44 -06004482 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
Alex Elderde71a292012-07-03 16:01:19 -05004483 rbd_dev->dev_id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004484 disk->major = rbd_dev->major;
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004485 disk->first_minor = rbd_dev->minor;
Ilya Dryomov7e513d42013-12-16 19:26:32 +02004486 if (single_major)
4487 disk->flags |= GENHD_FL_EXT_DEVT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004488 disk->fops = &rbd_bd_ops;
4489 disk->private_data = rbd_dev;
4490
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004491 memset(&rbd_dev->tag_set, 0, sizeof(rbd_dev->tag_set));
4492 rbd_dev->tag_set.ops = &rbd_mq_ops;
Ilya Dryomovb5584182015-06-23 16:21:19 +03004493 rbd_dev->tag_set.queue_depth = rbd_dev->opts->queue_depth;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004494 rbd_dev->tag_set.numa_node = NUMA_NO_NODE;
Ilya Dryomovb5584182015-06-23 16:21:19 +03004495 rbd_dev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004496 rbd_dev->tag_set.nr_hw_queues = 1;
4497 rbd_dev->tag_set.cmd_size = sizeof(struct work_struct);
4498
4499 err = blk_mq_alloc_tag_set(&rbd_dev->tag_set);
4500 if (err)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004501 goto out_disk;
Josh Durgin029bcbd2011-07-22 11:35:23 -07004502
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004503 q = blk_mq_init_queue(&rbd_dev->tag_set);
4504 if (IS_ERR(q)) {
4505 err = PTR_ERR(q);
4506 goto out_tag_set;
4507 }
4508
Ilya Dryomovd8a2c892015-03-24 16:15:17 +03004509 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
4510 /* QUEUE_FLAG_ADD_RANDOM is off by default for blk-mq */
Alex Elder593a9e72012-02-07 12:03:37 -06004511
Josh Durgin029bcbd2011-07-22 11:35:23 -07004512 /* set io sizes to object size */
Alex Elder593a9e72012-02-07 12:03:37 -06004513 segment_size = rbd_obj_bytes(&rbd_dev->header);
4514 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
Ilya Dryomov0d9fde42015-10-07 16:09:35 +02004515 q->limits.max_sectors = queue_max_hw_sectors(q);
Ilya Dryomovd3834fe2015-06-12 19:19:02 +03004516 blk_queue_max_segments(q, segment_size / SECTOR_SIZE);
Alex Elder593a9e72012-02-07 12:03:37 -06004517 blk_queue_max_segment_size(q, segment_size);
4518 blk_queue_io_min(q, segment_size);
4519 blk_queue_io_opt(q, segment_size);
Josh Durgin029bcbd2011-07-22 11:35:23 -07004520
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004521 /* enable the discard support */
4522 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
4523 q->limits.discard_granularity = segment_size;
4524 q->limits.discard_alignment = segment_size;
Jens Axboe2bb4cd52015-07-14 08:15:12 -06004525 blk_queue_max_discard_sectors(q, segment_size / SECTOR_SIZE);
Josh Durginb76f8232014-04-07 16:52:03 -07004526 q->limits.discard_zeroes_data = 1;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004527
Ronny Hegewaldbae818e2015-10-15 18:50:46 +00004528 if (!ceph_test_opt(rbd_dev->rbd_client->client, NOCRC))
4529 q->backing_dev_info.capabilities |= BDI_CAP_STABLE_WRITES;
4530
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004531 disk->queue = q;
4532
4533 q->queuedata = rbd_dev;
4534
4535 rbd_dev->disk = disk;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004536
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004537 return 0;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004538out_tag_set:
4539 blk_mq_free_tag_set(&rbd_dev->tag_set);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004540out_disk:
4541 put_disk(disk);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004542 return err;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004543}
4544
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004545/*
4546 sysfs
4547*/
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004548
Alex Elder593a9e72012-02-07 12:03:37 -06004549static struct rbd_device *dev_to_rbd_dev(struct device *dev)
4550{
4551 return container_of(dev, struct rbd_device, dev);
4552}
4553
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004554static ssize_t rbd_size_show(struct device *dev,
4555 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004556{
Alex Elder593a9e72012-02-07 12:03:37 -06004557 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004558
Alex Elderfc71d832013-04-26 15:44:36 -05004559 return sprintf(buf, "%llu\n",
4560 (unsigned long long)rbd_dev->mapping.size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004561}
4562
Alex Elder34b13182012-07-13 20:35:12 -05004563/*
4564 * Note this shows the features for whatever's mapped, which is not
4565 * necessarily the base image.
4566 */
4567static ssize_t rbd_features_show(struct device *dev,
4568 struct device_attribute *attr, char *buf)
4569{
4570 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4571
4572 return sprintf(buf, "0x%016llx\n",
Alex Elderfc71d832013-04-26 15:44:36 -05004573 (unsigned long long)rbd_dev->mapping.features);
Alex Elder34b13182012-07-13 20:35:12 -05004574}
4575
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004576static ssize_t rbd_major_show(struct device *dev,
4577 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004578{
Alex Elder593a9e72012-02-07 12:03:37 -06004579 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004580
Alex Elderfc71d832013-04-26 15:44:36 -05004581 if (rbd_dev->major)
4582 return sprintf(buf, "%d\n", rbd_dev->major);
4583
4584 return sprintf(buf, "(none)\n");
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004585}
Alex Elderfc71d832013-04-26 15:44:36 -05004586
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004587static ssize_t rbd_minor_show(struct device *dev,
4588 struct device_attribute *attr, char *buf)
4589{
4590 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4591
4592 return sprintf(buf, "%d\n", rbd_dev->minor);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004593}
4594
4595static ssize_t rbd_client_id_show(struct device *dev,
4596 struct device_attribute *attr, char *buf)
4597{
Alex Elder593a9e72012-02-07 12:03:37 -06004598 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004599
Alex Elder1dbb4392012-01-24 10:08:37 -06004600 return sprintf(buf, "client%lld\n",
Ilya Dryomov033268a2016-08-12 14:59:58 +02004601 ceph_client_gid(rbd_dev->rbd_client->client));
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004602}
4603
4604static ssize_t rbd_pool_show(struct device *dev,
4605 struct device_attribute *attr, char *buf)
4606{
Alex Elder593a9e72012-02-07 12:03:37 -06004607 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004608
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004609 return sprintf(buf, "%s\n", rbd_dev->spec->pool_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004610}
4611
Alex Elder9bb2f332012-07-12 10:46:35 -05004612static ssize_t rbd_pool_id_show(struct device *dev,
4613 struct device_attribute *attr, char *buf)
4614{
4615 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4616
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004617 return sprintf(buf, "%llu\n",
Alex Elderfc71d832013-04-26 15:44:36 -05004618 (unsigned long long) rbd_dev->spec->pool_id);
Alex Elder9bb2f332012-07-12 10:46:35 -05004619}
4620
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004621static ssize_t rbd_name_show(struct device *dev,
4622 struct device_attribute *attr, char *buf)
4623{
Alex Elder593a9e72012-02-07 12:03:37 -06004624 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004625
Alex Eldera92ffdf2012-10-30 19:40:33 -05004626 if (rbd_dev->spec->image_name)
4627 return sprintf(buf, "%s\n", rbd_dev->spec->image_name);
4628
4629 return sprintf(buf, "(unknown)\n");
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004630}
4631
Alex Elder589d30e2012-07-10 20:30:11 -05004632static ssize_t rbd_image_id_show(struct device *dev,
4633 struct device_attribute *attr, char *buf)
4634{
4635 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4636
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004637 return sprintf(buf, "%s\n", rbd_dev->spec->image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05004638}
4639
Alex Elder34b13182012-07-13 20:35:12 -05004640/*
4641 * Shows the name of the currently-mapped snapshot (or
4642 * RBD_SNAP_HEAD_NAME for the base image).
4643 */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004644static ssize_t rbd_snap_show(struct device *dev,
4645 struct device_attribute *attr,
4646 char *buf)
4647{
Alex Elder593a9e72012-02-07 12:03:37 -06004648 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004649
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004650 return sprintf(buf, "%s\n", rbd_dev->spec->snap_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004651}
4652
Alex Elder86b00e02012-10-25 23:34:42 -05004653/*
Ilya Dryomovff961282014-07-22 21:53:07 +04004654 * For a v2 image, shows the chain of parent images, separated by empty
4655 * lines. For v1 images or if there is no parent, shows "(no parent
4656 * image)".
Alex Elder86b00e02012-10-25 23:34:42 -05004657 */
4658static ssize_t rbd_parent_show(struct device *dev,
Ilya Dryomovff961282014-07-22 21:53:07 +04004659 struct device_attribute *attr,
4660 char *buf)
Alex Elder86b00e02012-10-25 23:34:42 -05004661{
4662 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Ilya Dryomovff961282014-07-22 21:53:07 +04004663 ssize_t count = 0;
Alex Elder86b00e02012-10-25 23:34:42 -05004664
Ilya Dryomovff961282014-07-22 21:53:07 +04004665 if (!rbd_dev->parent)
Alex Elder86b00e02012-10-25 23:34:42 -05004666 return sprintf(buf, "(no parent image)\n");
4667
Ilya Dryomovff961282014-07-22 21:53:07 +04004668 for ( ; rbd_dev->parent; rbd_dev = rbd_dev->parent) {
4669 struct rbd_spec *spec = rbd_dev->parent_spec;
Alex Elder86b00e02012-10-25 23:34:42 -05004670
Ilya Dryomovff961282014-07-22 21:53:07 +04004671 count += sprintf(&buf[count], "%s"
4672 "pool_id %llu\npool_name %s\n"
4673 "image_id %s\nimage_name %s\n"
4674 "snap_id %llu\nsnap_name %s\n"
4675 "overlap %llu\n",
4676 !count ? "" : "\n", /* first? */
4677 spec->pool_id, spec->pool_name,
4678 spec->image_id, spec->image_name ?: "(unknown)",
4679 spec->snap_id, spec->snap_name,
4680 rbd_dev->parent_overlap);
4681 }
Alex Elder86b00e02012-10-25 23:34:42 -05004682
Ilya Dryomovff961282014-07-22 21:53:07 +04004683 return count;
Alex Elder86b00e02012-10-25 23:34:42 -05004684}
4685
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004686static ssize_t rbd_image_refresh(struct device *dev,
4687 struct device_attribute *attr,
4688 const char *buf,
4689 size_t size)
4690{
Alex Elder593a9e72012-02-07 12:03:37 -06004691 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Alex Elderb8136232012-07-25 09:32:41 -05004692 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004693
Alex Eldercc4a38bd2013-04-30 00:44:33 -05004694 ret = rbd_dev_refresh(rbd_dev);
Alex Eldere627db02013-05-06 07:40:30 -05004695 if (ret)
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04004696 return ret;
Alex Elderb8136232012-07-25 09:32:41 -05004697
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04004698 return size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004699}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004700
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004701static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
Alex Elder34b13182012-07-13 20:35:12 -05004702static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004703static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004704static DEVICE_ATTR(minor, S_IRUGO, rbd_minor_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004705static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
4706static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
Alex Elder9bb2f332012-07-12 10:46:35 -05004707static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004708static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
Alex Elder589d30e2012-07-10 20:30:11 -05004709static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004710static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
4711static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
Alex Elder86b00e02012-10-25 23:34:42 -05004712static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004713
4714static struct attribute *rbd_attrs[] = {
4715 &dev_attr_size.attr,
Alex Elder34b13182012-07-13 20:35:12 -05004716 &dev_attr_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004717 &dev_attr_major.attr,
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004718 &dev_attr_minor.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004719 &dev_attr_client_id.attr,
4720 &dev_attr_pool.attr,
Alex Elder9bb2f332012-07-12 10:46:35 -05004721 &dev_attr_pool_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004722 &dev_attr_name.attr,
Alex Elder589d30e2012-07-10 20:30:11 -05004723 &dev_attr_image_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004724 &dev_attr_current_snap.attr,
Alex Elder86b00e02012-10-25 23:34:42 -05004725 &dev_attr_parent.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004726 &dev_attr_refresh.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004727 NULL
4728};
4729
4730static struct attribute_group rbd_attr_group = {
4731 .attrs = rbd_attrs,
4732};
4733
4734static const struct attribute_group *rbd_attr_groups[] = {
4735 &rbd_attr_group,
4736 NULL
4737};
4738
Ilya Dryomov6cac4692015-10-16 20:11:25 +02004739static void rbd_dev_release(struct device *dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004740
4741static struct device_type rbd_device_type = {
4742 .name = "rbd",
4743 .groups = rbd_attr_groups,
Ilya Dryomov6cac4692015-10-16 20:11:25 +02004744 .release = rbd_dev_release,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004745};
4746
Alex Elder8b8fb992012-10-26 17:25:24 -05004747static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)
4748{
4749 kref_get(&spec->kref);
4750
4751 return spec;
4752}
4753
4754static void rbd_spec_free(struct kref *kref);
4755static void rbd_spec_put(struct rbd_spec *spec)
4756{
4757 if (spec)
4758 kref_put(&spec->kref, rbd_spec_free);
4759}
4760
4761static struct rbd_spec *rbd_spec_alloc(void)
4762{
4763 struct rbd_spec *spec;
4764
4765 spec = kzalloc(sizeof (*spec), GFP_KERNEL);
4766 if (!spec)
4767 return NULL;
Ilya Dryomov04077592014-07-23 17:11:20 +04004768
4769 spec->pool_id = CEPH_NOPOOL;
4770 spec->snap_id = CEPH_NOSNAP;
Alex Elder8b8fb992012-10-26 17:25:24 -05004771 kref_init(&spec->kref);
4772
Alex Elder8b8fb992012-10-26 17:25:24 -05004773 return spec;
4774}
4775
4776static void rbd_spec_free(struct kref *kref)
4777{
4778 struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref);
4779
4780 kfree(spec->pool_name);
4781 kfree(spec->image_id);
4782 kfree(spec->image_name);
4783 kfree(spec->snap_name);
4784 kfree(spec);
4785}
4786
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004787static void rbd_dev_free(struct rbd_device *rbd_dev)
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004788{
Ilya Dryomov99d16942016-08-12 16:11:41 +02004789 WARN_ON(rbd_dev->watch_state != RBD_WATCH_STATE_UNREGISTERED);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004790 WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_UNLOCKED);
Ilya Dryomov99d16942016-08-12 16:11:41 +02004791
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004792 ceph_oid_destroy(&rbd_dev->header_oid);
Ilya Dryomov6b6dddb2016-08-05 16:15:38 +02004793 ceph_oloc_destroy(&rbd_dev->header_oloc);
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004794
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004795 rbd_put_client(rbd_dev->rbd_client);
4796 rbd_spec_put(rbd_dev->spec);
4797 kfree(rbd_dev->opts);
4798 kfree(rbd_dev);
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004799}
4800
4801static void rbd_dev_release(struct device *dev)
4802{
4803 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4804 bool need_put = !!rbd_dev->opts;
4805
4806 if (need_put) {
4807 destroy_workqueue(rbd_dev->task_wq);
4808 ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id);
4809 }
4810
4811 rbd_dev_free(rbd_dev);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004812
4813 /*
4814 * This is racy, but way better than putting module outside of
4815 * the release callback. The race window is pretty small, so
4816 * doing something similar to dm (dm-builtin.c) is overkill.
4817 */
4818 if (need_put)
4819 module_put(THIS_MODULE);
4820}
4821
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004822static struct rbd_device *__rbd_dev_create(struct rbd_client *rbdc,
4823 struct rbd_spec *spec)
Alex Elderc53d5892012-10-25 23:34:42 -05004824{
4825 struct rbd_device *rbd_dev;
4826
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004827 rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL);
Alex Elderc53d5892012-10-25 23:34:42 -05004828 if (!rbd_dev)
4829 return NULL;
4830
4831 spin_lock_init(&rbd_dev->lock);
4832 INIT_LIST_HEAD(&rbd_dev->node);
Alex Elderc53d5892012-10-25 23:34:42 -05004833 init_rwsem(&rbd_dev->header_rwsem);
4834
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004835 ceph_oid_init(&rbd_dev->header_oid);
Ilya Dryomov922dab62016-05-26 01:15:02 +02004836 ceph_oloc_init(&rbd_dev->header_oloc);
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004837
Ilya Dryomov99d16942016-08-12 16:11:41 +02004838 mutex_init(&rbd_dev->watch_mutex);
4839 rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED;
4840 INIT_DELAYED_WORK(&rbd_dev->watch_dwork, rbd_reregister_watch);
4841
Ilya Dryomoved95b212016-08-12 16:40:02 +02004842 init_rwsem(&rbd_dev->lock_rwsem);
4843 rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED;
4844 INIT_WORK(&rbd_dev->acquired_lock_work, rbd_notify_acquired_lock);
4845 INIT_WORK(&rbd_dev->released_lock_work, rbd_notify_released_lock);
4846 INIT_DELAYED_WORK(&rbd_dev->lock_dwork, rbd_acquire_lock);
4847 INIT_WORK(&rbd_dev->unlock_work, rbd_release_lock_work);
4848 init_waitqueue_head(&rbd_dev->lock_waitq);
4849
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004850 rbd_dev->dev.bus = &rbd_bus_type;
4851 rbd_dev->dev.type = &rbd_device_type;
4852 rbd_dev->dev.parent = &rbd_root_dev;
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004853 device_initialize(&rbd_dev->dev);
4854
Alex Elderc53d5892012-10-25 23:34:42 -05004855 rbd_dev->rbd_client = rbdc;
Ilya Dryomovd1475432015-06-22 13:24:48 +03004856 rbd_dev->spec = spec;
Alex Elder0903e872012-11-14 12:25:19 -06004857
Yan, Zheng76271512016-02-03 21:24:49 +08004858 rbd_dev->layout.stripe_unit = 1 << RBD_MAX_OBJ_ORDER;
4859 rbd_dev->layout.stripe_count = 1;
4860 rbd_dev->layout.object_size = 1 << RBD_MAX_OBJ_ORDER;
4861 rbd_dev->layout.pool_id = spec->pool_id;
Yan, Zheng30c156d2016-02-14 11:24:31 +08004862 RCU_INIT_POINTER(rbd_dev->layout.pool_ns, NULL);
Alex Elder0903e872012-11-14 12:25:19 -06004863
Alex Elderc53d5892012-10-25 23:34:42 -05004864 return rbd_dev;
4865}
4866
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004867/*
4868 * Create a mapping rbd_dev.
4869 */
4870static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,
4871 struct rbd_spec *spec,
4872 struct rbd_options *opts)
4873{
4874 struct rbd_device *rbd_dev;
4875
4876 rbd_dev = __rbd_dev_create(rbdc, spec);
4877 if (!rbd_dev)
4878 return NULL;
4879
4880 rbd_dev->opts = opts;
4881
4882 /* get an id and fill in device name */
4883 rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0,
4884 minor_to_rbd_dev_id(1 << MINORBITS),
4885 GFP_KERNEL);
4886 if (rbd_dev->dev_id < 0)
4887 goto fail_rbd_dev;
4888
4889 sprintf(rbd_dev->name, RBD_DRV_NAME "%d", rbd_dev->dev_id);
4890 rbd_dev->task_wq = alloc_ordered_workqueue("%s-tasks", WQ_MEM_RECLAIM,
4891 rbd_dev->name);
4892 if (!rbd_dev->task_wq)
4893 goto fail_dev_id;
4894
4895 /* we have a ref from do_rbd_add() */
4896 __module_get(THIS_MODULE);
4897
4898 dout("%s rbd_dev %p dev_id %d\n", __func__, rbd_dev, rbd_dev->dev_id);
4899 return rbd_dev;
4900
4901fail_dev_id:
4902 ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id);
4903fail_rbd_dev:
4904 rbd_dev_free(rbd_dev);
4905 return NULL;
4906}
4907
Alex Elderc53d5892012-10-25 23:34:42 -05004908static void rbd_dev_destroy(struct rbd_device *rbd_dev)
4909{
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004910 if (rbd_dev)
4911 put_device(&rbd_dev->dev);
Alex Elderc53d5892012-10-25 23:34:42 -05004912}
4913
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004914/*
Alex Elder9d475de2012-07-03 16:01:19 -05004915 * Get the size and object order for an image snapshot, or if
4916 * snap_id is CEPH_NOSNAP, gets this information for the base
4917 * image.
4918 */
4919static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
4920 u8 *order, u64 *snap_size)
4921{
4922 __le64 snapid = cpu_to_le64(snap_id);
4923 int ret;
4924 struct {
4925 u8 order;
4926 __le64 size;
4927 } __attribute__ ((packed)) size_buf = { 0 };
4928
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004929 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name,
Alex Elder9d475de2012-07-03 16:01:19 -05004930 "rbd", "get_size",
Alex Elder41579762013-04-21 12:14:45 -05004931 &snapid, sizeof (snapid),
Alex Eldere2a58ee2013-04-30 00:44:33 -05004932 &size_buf, sizeof (size_buf));
Alex Elder36be9a72013-01-19 00:30:28 -06004933 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder9d475de2012-07-03 16:01:19 -05004934 if (ret < 0)
4935 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05004936 if (ret < sizeof (size_buf))
4937 return -ERANGE;
Alex Elder9d475de2012-07-03 16:01:19 -05004938
Josh Durginc3545572013-08-28 17:08:10 -07004939 if (order) {
Alex Elderc86f86e2013-04-25 15:09:41 -05004940 *order = size_buf.order;
Josh Durginc3545572013-08-28 17:08:10 -07004941 dout(" order %u", (unsigned int)*order);
4942 }
Alex Elder9d475de2012-07-03 16:01:19 -05004943 *snap_size = le64_to_cpu(size_buf.size);
4944
Josh Durginc3545572013-08-28 17:08:10 -07004945 dout(" snap_id 0x%016llx snap_size = %llu\n",
4946 (unsigned long long)snap_id,
Alex Elder57385b52013-04-21 12:14:45 -05004947 (unsigned long long)*snap_size);
Alex Elder9d475de2012-07-03 16:01:19 -05004948
4949 return 0;
4950}
4951
4952static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
4953{
4954 return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
4955 &rbd_dev->header.obj_order,
4956 &rbd_dev->header.image_size);
4957}
4958
Alex Elder1e130192012-07-03 16:01:19 -05004959static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
4960{
4961 void *reply_buf;
4962 int ret;
4963 void *p;
4964
4965 reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL);
4966 if (!reply_buf)
4967 return -ENOMEM;
4968
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004969 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name,
Alex Elder41579762013-04-21 12:14:45 -05004970 "rbd", "get_object_prefix", NULL, 0,
Alex Eldere2a58ee2013-04-30 00:44:33 -05004971 reply_buf, RBD_OBJ_PREFIX_LEN_MAX);
Alex Elder36be9a72013-01-19 00:30:28 -06004972 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder1e130192012-07-03 16:01:19 -05004973 if (ret < 0)
4974 goto out;
4975
4976 p = reply_buf;
4977 rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p,
Alex Elder57385b52013-04-21 12:14:45 -05004978 p + ret, NULL, GFP_NOIO);
4979 ret = 0;
Alex Elder1e130192012-07-03 16:01:19 -05004980
4981 if (IS_ERR(rbd_dev->header.object_prefix)) {
4982 ret = PTR_ERR(rbd_dev->header.object_prefix);
4983 rbd_dev->header.object_prefix = NULL;
4984 } else {
4985 dout(" object_prefix = %s\n", rbd_dev->header.object_prefix);
4986 }
Alex Elder1e130192012-07-03 16:01:19 -05004987out:
4988 kfree(reply_buf);
4989
4990 return ret;
4991}
4992
Alex Elderb1b54022012-07-03 16:01:19 -05004993static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
4994 u64 *snap_features)
4995{
4996 __le64 snapid = cpu_to_le64(snap_id);
4997 struct {
4998 __le64 features;
4999 __le64 incompat;
Alex Elder41579762013-04-21 12:14:45 -05005000 } __attribute__ ((packed)) features_buf = { 0 };
Ilya Dryomovd3767f02016-04-13 14:15:50 +02005001 u64 unsup;
Alex Elderb1b54022012-07-03 16:01:19 -05005002 int ret;
5003
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005004 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name,
Alex Elderb1b54022012-07-03 16:01:19 -05005005 "rbd", "get_features",
Alex Elder41579762013-04-21 12:14:45 -05005006 &snapid, sizeof (snapid),
Alex Eldere2a58ee2013-04-30 00:44:33 -05005007 &features_buf, sizeof (features_buf));
Alex Elder36be9a72013-01-19 00:30:28 -06005008 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderb1b54022012-07-03 16:01:19 -05005009 if (ret < 0)
5010 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05005011 if (ret < sizeof (features_buf))
5012 return -ERANGE;
Alex Elderd8891402012-10-09 13:50:17 -07005013
Ilya Dryomovd3767f02016-04-13 14:15:50 +02005014 unsup = le64_to_cpu(features_buf.incompat) & ~RBD_FEATURES_SUPPORTED;
5015 if (unsup) {
5016 rbd_warn(rbd_dev, "image uses unsupported features: 0x%llx",
5017 unsup);
Alex Elderb8f5c6e2012-11-01 08:39:26 -05005018 return -ENXIO;
Ilya Dryomovd3767f02016-04-13 14:15:50 +02005019 }
Alex Elderd8891402012-10-09 13:50:17 -07005020
Alex Elderb1b54022012-07-03 16:01:19 -05005021 *snap_features = le64_to_cpu(features_buf.features);
5022
5023 dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
Alex Elder57385b52013-04-21 12:14:45 -05005024 (unsigned long long)snap_id,
5025 (unsigned long long)*snap_features,
5026 (unsigned long long)le64_to_cpu(features_buf.incompat));
Alex Elderb1b54022012-07-03 16:01:19 -05005027
5028 return 0;
5029}
5030
5031static int rbd_dev_v2_features(struct rbd_device *rbd_dev)
5032{
5033 return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP,
5034 &rbd_dev->header.features);
5035}
5036
Alex Elder86b00e02012-10-25 23:34:42 -05005037static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev)
5038{
5039 struct rbd_spec *parent_spec;
5040 size_t size;
5041 void *reply_buf = NULL;
5042 __le64 snapid;
5043 void *p;
5044 void *end;
Alex Elder642a2532013-05-06 17:40:33 -05005045 u64 pool_id;
Alex Elder86b00e02012-10-25 23:34:42 -05005046 char *image_id;
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005047 u64 snap_id;
Alex Elder86b00e02012-10-25 23:34:42 -05005048 u64 overlap;
Alex Elder86b00e02012-10-25 23:34:42 -05005049 int ret;
5050
5051 parent_spec = rbd_spec_alloc();
5052 if (!parent_spec)
5053 return -ENOMEM;
5054
5055 size = sizeof (__le64) + /* pool_id */
5056 sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */
5057 sizeof (__le64) + /* snap_id */
5058 sizeof (__le64); /* overlap */
5059 reply_buf = kmalloc(size, GFP_KERNEL);
5060 if (!reply_buf) {
5061 ret = -ENOMEM;
5062 goto out_err;
5063 }
5064
Ilya Dryomov4d9b67c2014-07-24 10:42:13 +04005065 snapid = cpu_to_le64(rbd_dev->spec->snap_id);
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005066 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name,
Alex Elder86b00e02012-10-25 23:34:42 -05005067 "rbd", "get_parent",
Alex Elder41579762013-04-21 12:14:45 -05005068 &snapid, sizeof (snapid),
Alex Eldere2a58ee2013-04-30 00:44:33 -05005069 reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06005070 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder86b00e02012-10-25 23:34:42 -05005071 if (ret < 0)
5072 goto out_err;
5073
Alex Elder86b00e02012-10-25 23:34:42 -05005074 p = reply_buf;
Alex Elder57385b52013-04-21 12:14:45 -05005075 end = reply_buf + ret;
5076 ret = -ERANGE;
Alex Elder642a2532013-05-06 17:40:33 -05005077 ceph_decode_64_safe(&p, end, pool_id, out_err);
Alex Elder392a9da2013-05-06 17:40:33 -05005078 if (pool_id == CEPH_NOPOOL) {
5079 /*
5080 * Either the parent never existed, or we have
5081 * record of it but the image got flattened so it no
5082 * longer has a parent. When the parent of a
5083 * layered image disappears we immediately set the
5084 * overlap to 0. The effect of this is that all new
5085 * requests will be treated as if the image had no
5086 * parent.
5087 */
5088 if (rbd_dev->parent_overlap) {
5089 rbd_dev->parent_overlap = 0;
Alex Elder392a9da2013-05-06 17:40:33 -05005090 rbd_dev_parent_put(rbd_dev);
5091 pr_info("%s: clone image has been flattened\n",
5092 rbd_dev->disk->disk_name);
5093 }
5094
Alex Elder86b00e02012-10-25 23:34:42 -05005095 goto out; /* No parent? No problem. */
Alex Elder392a9da2013-05-06 17:40:33 -05005096 }
Alex Elder86b00e02012-10-25 23:34:42 -05005097
Alex Elder0903e872012-11-14 12:25:19 -06005098 /* The ceph file layout needs to fit pool id in 32 bits */
5099
5100 ret = -EIO;
Alex Elder642a2532013-05-06 17:40:33 -05005101 if (pool_id > (u64)U32_MAX) {
Ilya Dryomov9584d502014-07-11 12:11:20 +04005102 rbd_warn(NULL, "parent pool id too large (%llu > %u)",
Alex Elder642a2532013-05-06 17:40:33 -05005103 (unsigned long long)pool_id, U32_MAX);
Alex Elder57385b52013-04-21 12:14:45 -05005104 goto out_err;
Alex Elderc0cd10db2013-04-26 09:43:47 -05005105 }
Alex Elder0903e872012-11-14 12:25:19 -06005106
Alex Elder979ed482012-11-01 08:39:26 -05005107 image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elder86b00e02012-10-25 23:34:42 -05005108 if (IS_ERR(image_id)) {
5109 ret = PTR_ERR(image_id);
5110 goto out_err;
5111 }
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005112 ceph_decode_64_safe(&p, end, snap_id, out_err);
Alex Elder86b00e02012-10-25 23:34:42 -05005113 ceph_decode_64_safe(&p, end, overlap, out_err);
5114
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005115 /*
5116 * The parent won't change (except when the clone is
5117 * flattened, already handled that). So we only need to
5118 * record the parent spec we have not already done so.
5119 */
5120 if (!rbd_dev->parent_spec) {
5121 parent_spec->pool_id = pool_id;
5122 parent_spec->image_id = image_id;
5123 parent_spec->snap_id = snap_id;
Alex Elder70cf49c2013-05-06 17:40:33 -05005124 rbd_dev->parent_spec = parent_spec;
5125 parent_spec = NULL; /* rbd_dev now owns this */
Ilya Dryomovfbba11b2014-06-27 21:46:33 +04005126 } else {
5127 kfree(image_id);
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005128 }
5129
5130 /*
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005131 * We always update the parent overlap. If it's zero we issue
5132 * a warning, as we will proceed as if there was no parent.
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005133 */
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005134 if (!overlap) {
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005135 if (parent_spec) {
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005136 /* refresh, careful to warn just once */
5137 if (rbd_dev->parent_overlap)
5138 rbd_warn(rbd_dev,
5139 "clone now standalone (overlap became 0)");
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005140 } else {
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005141 /* initial probe */
5142 rbd_warn(rbd_dev, "clone is standalone (overlap 0)");
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005143 }
Alex Elder70cf49c2013-05-06 17:40:33 -05005144 }
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005145 rbd_dev->parent_overlap = overlap;
5146
Alex Elder86b00e02012-10-25 23:34:42 -05005147out:
5148 ret = 0;
5149out_err:
5150 kfree(reply_buf);
5151 rbd_spec_put(parent_spec);
5152
5153 return ret;
5154}
5155
Alex Eldercc070d52013-04-21 12:14:45 -05005156static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev)
5157{
5158 struct {
5159 __le64 stripe_unit;
5160 __le64 stripe_count;
5161 } __attribute__ ((packed)) striping_info_buf = { 0 };
5162 size_t size = sizeof (striping_info_buf);
5163 void *p;
5164 u64 obj_size;
5165 u64 stripe_unit;
5166 u64 stripe_count;
5167 int ret;
5168
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005169 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name,
Alex Eldercc070d52013-04-21 12:14:45 -05005170 "rbd", "get_stripe_unit_count", NULL, 0,
Alex Eldere2a58ee2013-04-30 00:44:33 -05005171 (char *)&striping_info_buf, size);
Alex Eldercc070d52013-04-21 12:14:45 -05005172 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
5173 if (ret < 0)
5174 return ret;
5175 if (ret < size)
5176 return -ERANGE;
5177
5178 /*
5179 * We don't actually support the "fancy striping" feature
5180 * (STRIPINGV2) yet, but if the striping sizes are the
5181 * defaults the behavior is the same as before. So find
5182 * out, and only fail if the image has non-default values.
5183 */
5184 ret = -EINVAL;
5185 obj_size = (u64)1 << rbd_dev->header.obj_order;
5186 p = &striping_info_buf;
5187 stripe_unit = ceph_decode_64(&p);
5188 if (stripe_unit != obj_size) {
5189 rbd_warn(rbd_dev, "unsupported stripe unit "
5190 "(got %llu want %llu)",
5191 stripe_unit, obj_size);
5192 return -EINVAL;
5193 }
5194 stripe_count = ceph_decode_64(&p);
5195 if (stripe_count != 1) {
5196 rbd_warn(rbd_dev, "unsupported stripe count "
5197 "(got %llu want 1)", stripe_count);
5198 return -EINVAL;
5199 }
Alex Elder500d0c02013-04-26 09:43:47 -05005200 rbd_dev->header.stripe_unit = stripe_unit;
5201 rbd_dev->header.stripe_count = stripe_count;
Alex Eldercc070d52013-04-21 12:14:45 -05005202
5203 return 0;
5204}
5205
Alex Elder9e15b772012-10-30 19:40:33 -05005206static char *rbd_dev_image_name(struct rbd_device *rbd_dev)
5207{
5208 size_t image_id_size;
5209 char *image_id;
5210 void *p;
5211 void *end;
5212 size_t size;
5213 void *reply_buf = NULL;
5214 size_t len = 0;
5215 char *image_name = NULL;
5216 int ret;
5217
5218 rbd_assert(!rbd_dev->spec->image_name);
5219
Alex Elder69e7a022012-11-01 08:39:26 -05005220 len = strlen(rbd_dev->spec->image_id);
5221 image_id_size = sizeof (__le32) + len;
Alex Elder9e15b772012-10-30 19:40:33 -05005222 image_id = kmalloc(image_id_size, GFP_KERNEL);
5223 if (!image_id)
5224 return NULL;
5225
5226 p = image_id;
Alex Elder41579762013-04-21 12:14:45 -05005227 end = image_id + image_id_size;
Alex Elder57385b52013-04-21 12:14:45 -05005228 ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len);
Alex Elder9e15b772012-10-30 19:40:33 -05005229
5230 size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX;
5231 reply_buf = kmalloc(size, GFP_KERNEL);
5232 if (!reply_buf)
5233 goto out;
5234
Alex Elder36be9a72013-01-19 00:30:28 -06005235 ret = rbd_obj_method_sync(rbd_dev, RBD_DIRECTORY,
Alex Elder9e15b772012-10-30 19:40:33 -05005236 "rbd", "dir_get_name",
5237 image_id, image_id_size,
Alex Eldere2a58ee2013-04-30 00:44:33 -05005238 reply_buf, size);
Alex Elder9e15b772012-10-30 19:40:33 -05005239 if (ret < 0)
5240 goto out;
5241 p = reply_buf;
Alex Elderf40eb342013-04-25 15:09:42 -05005242 end = reply_buf + ret;
5243
Alex Elder9e15b772012-10-30 19:40:33 -05005244 image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL);
5245 if (IS_ERR(image_name))
5246 image_name = NULL;
5247 else
5248 dout("%s: name is %s len is %zd\n", __func__, image_name, len);
5249out:
5250 kfree(reply_buf);
5251 kfree(image_id);
5252
5253 return image_name;
5254}
5255
Alex Elder2ad3d712013-04-30 00:44:33 -05005256static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
5257{
5258 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
5259 const char *snap_name;
5260 u32 which = 0;
5261
5262 /* Skip over names until we find the one we are looking for */
5263
5264 snap_name = rbd_dev->header.snap_names;
5265 while (which < snapc->num_snaps) {
5266 if (!strcmp(name, snap_name))
5267 return snapc->snaps[which];
5268 snap_name += strlen(snap_name) + 1;
5269 which++;
5270 }
5271 return CEPH_NOSNAP;
5272}
5273
5274static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
5275{
5276 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
5277 u32 which;
5278 bool found = false;
5279 u64 snap_id;
5280
5281 for (which = 0; !found && which < snapc->num_snaps; which++) {
5282 const char *snap_name;
5283
5284 snap_id = snapc->snaps[which];
5285 snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id);
Josh Durginefadc982013-08-29 19:16:42 -07005286 if (IS_ERR(snap_name)) {
5287 /* ignore no-longer existing snapshots */
5288 if (PTR_ERR(snap_name) == -ENOENT)
5289 continue;
5290 else
5291 break;
5292 }
Alex Elder2ad3d712013-04-30 00:44:33 -05005293 found = !strcmp(name, snap_name);
5294 kfree(snap_name);
5295 }
5296 return found ? snap_id : CEPH_NOSNAP;
5297}
5298
5299/*
5300 * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if
5301 * no snapshot by that name is found, or if an error occurs.
5302 */
5303static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
5304{
5305 if (rbd_dev->image_format == 1)
5306 return rbd_v1_snap_id_by_name(rbd_dev, name);
5307
5308 return rbd_v2_snap_id_by_name(rbd_dev, name);
5309}
5310
Alex Elder9e15b772012-10-30 19:40:33 -05005311/*
Ilya Dryomov04077592014-07-23 17:11:20 +04005312 * An image being mapped will have everything but the snap id.
Alex Elder9e15b772012-10-30 19:40:33 -05005313 */
Ilya Dryomov04077592014-07-23 17:11:20 +04005314static int rbd_spec_fill_snap_id(struct rbd_device *rbd_dev)
5315{
5316 struct rbd_spec *spec = rbd_dev->spec;
5317
5318 rbd_assert(spec->pool_id != CEPH_NOPOOL && spec->pool_name);
5319 rbd_assert(spec->image_id && spec->image_name);
5320 rbd_assert(spec->snap_name);
5321
5322 if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) {
5323 u64 snap_id;
5324
5325 snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name);
5326 if (snap_id == CEPH_NOSNAP)
5327 return -ENOENT;
5328
5329 spec->snap_id = snap_id;
5330 } else {
5331 spec->snap_id = CEPH_NOSNAP;
5332 }
5333
5334 return 0;
5335}
5336
5337/*
5338 * A parent image will have all ids but none of the names.
5339 *
5340 * All names in an rbd spec are dynamically allocated. It's OK if we
5341 * can't figure out the name for an image id.
5342 */
5343static int rbd_spec_fill_names(struct rbd_device *rbd_dev)
Alex Elder9e15b772012-10-30 19:40:33 -05005344{
Alex Elder2e9f7f12013-04-26 09:43:48 -05005345 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
5346 struct rbd_spec *spec = rbd_dev->spec;
5347 const char *pool_name;
5348 const char *image_name;
5349 const char *snap_name;
Alex Elder9e15b772012-10-30 19:40:33 -05005350 int ret;
5351
Ilya Dryomov04077592014-07-23 17:11:20 +04005352 rbd_assert(spec->pool_id != CEPH_NOPOOL);
5353 rbd_assert(spec->image_id);
5354 rbd_assert(spec->snap_id != CEPH_NOSNAP);
Alex Elder9e15b772012-10-30 19:40:33 -05005355
Alex Elder2e9f7f12013-04-26 09:43:48 -05005356 /* Get the pool name; we have to make our own copy of this */
Alex Elder9e15b772012-10-30 19:40:33 -05005357
Alex Elder2e9f7f12013-04-26 09:43:48 -05005358 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id);
5359 if (!pool_name) {
5360 rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id);
Alex Elder935dc892012-11-01 10:17:15 -05005361 return -EIO;
5362 }
Alex Elder2e9f7f12013-04-26 09:43:48 -05005363 pool_name = kstrdup(pool_name, GFP_KERNEL);
5364 if (!pool_name)
Alex Elder9e15b772012-10-30 19:40:33 -05005365 return -ENOMEM;
5366
5367 /* Fetch the image name; tolerate failure here */
5368
Alex Elder2e9f7f12013-04-26 09:43:48 -05005369 image_name = rbd_dev_image_name(rbd_dev);
5370 if (!image_name)
Alex Elder06ecc6c2012-11-01 10:17:15 -05005371 rbd_warn(rbd_dev, "unable to get image name");
Alex Elder9e15b772012-10-30 19:40:33 -05005372
Ilya Dryomov04077592014-07-23 17:11:20 +04005373 /* Fetch the snapshot name */
Alex Elder9e15b772012-10-30 19:40:33 -05005374
Alex Elder2e9f7f12013-04-26 09:43:48 -05005375 snap_name = rbd_snap_name(rbd_dev, spec->snap_id);
Josh Durginda6a6b62013-09-04 17:57:31 -07005376 if (IS_ERR(snap_name)) {
5377 ret = PTR_ERR(snap_name);
Alex Elder9e15b772012-10-30 19:40:33 -05005378 goto out_err;
Alex Elder2e9f7f12013-04-26 09:43:48 -05005379 }
5380
5381 spec->pool_name = pool_name;
5382 spec->image_name = image_name;
5383 spec->snap_name = snap_name;
Alex Elder9e15b772012-10-30 19:40:33 -05005384
5385 return 0;
Ilya Dryomov04077592014-07-23 17:11:20 +04005386
Alex Elder9e15b772012-10-30 19:40:33 -05005387out_err:
Alex Elder2e9f7f12013-04-26 09:43:48 -05005388 kfree(image_name);
5389 kfree(pool_name);
Alex Elder9e15b772012-10-30 19:40:33 -05005390 return ret;
5391}
5392
Alex Eldercc4a38bd2013-04-30 00:44:33 -05005393static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev)
Alex Elder35d489f2012-07-03 16:01:19 -05005394{
5395 size_t size;
5396 int ret;
5397 void *reply_buf;
5398 void *p;
5399 void *end;
5400 u64 seq;
5401 u32 snap_count;
5402 struct ceph_snap_context *snapc;
5403 u32 i;
5404
5405 /*
5406 * We'll need room for the seq value (maximum snapshot id),
5407 * snapshot count, and array of that many snapshot ids.
5408 * For now we have a fixed upper limit on the number we're
5409 * prepared to receive.
5410 */
5411 size = sizeof (__le64) + sizeof (__le32) +
5412 RBD_MAX_SNAP_COUNT * sizeof (__le64);
5413 reply_buf = kzalloc(size, GFP_KERNEL);
5414 if (!reply_buf)
5415 return -ENOMEM;
5416
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005417 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name,
Alex Elder41579762013-04-21 12:14:45 -05005418 "rbd", "get_snapcontext", NULL, 0,
Alex Eldere2a58ee2013-04-30 00:44:33 -05005419 reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06005420 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder35d489f2012-07-03 16:01:19 -05005421 if (ret < 0)
5422 goto out;
5423
Alex Elder35d489f2012-07-03 16:01:19 -05005424 p = reply_buf;
Alex Elder57385b52013-04-21 12:14:45 -05005425 end = reply_buf + ret;
5426 ret = -ERANGE;
Alex Elder35d489f2012-07-03 16:01:19 -05005427 ceph_decode_64_safe(&p, end, seq, out);
5428 ceph_decode_32_safe(&p, end, snap_count, out);
5429
5430 /*
5431 * Make sure the reported number of snapshot ids wouldn't go
5432 * beyond the end of our buffer. But before checking that,
5433 * make sure the computed size of the snapshot context we
5434 * allocate is representable in a size_t.
5435 */
5436 if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
5437 / sizeof (u64)) {
5438 ret = -EINVAL;
5439 goto out;
5440 }
5441 if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
5442 goto out;
Alex Elder468521c2013-04-26 09:43:47 -05005443 ret = 0;
Alex Elder35d489f2012-07-03 16:01:19 -05005444
Alex Elder812164f82013-04-30 00:44:32 -05005445 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
Alex Elder35d489f2012-07-03 16:01:19 -05005446 if (!snapc) {
5447 ret = -ENOMEM;
5448 goto out;
5449 }
Alex Elder35d489f2012-07-03 16:01:19 -05005450 snapc->seq = seq;
Alex Elder35d489f2012-07-03 16:01:19 -05005451 for (i = 0; i < snap_count; i++)
5452 snapc->snaps[i] = ceph_decode_64(&p);
5453
Alex Elder49ece552013-05-06 08:37:00 -05005454 ceph_put_snap_context(rbd_dev->header.snapc);
Alex Elder35d489f2012-07-03 16:01:19 -05005455 rbd_dev->header.snapc = snapc;
5456
5457 dout(" snap context seq = %llu, snap_count = %u\n",
Alex Elder57385b52013-04-21 12:14:45 -05005458 (unsigned long long)seq, (unsigned int)snap_count);
Alex Elder35d489f2012-07-03 16:01:19 -05005459out:
5460 kfree(reply_buf);
5461
Alex Elder57385b52013-04-21 12:14:45 -05005462 return ret;
Alex Elder35d489f2012-07-03 16:01:19 -05005463}
5464
Alex Elder54cac612013-04-30 00:44:33 -05005465static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
5466 u64 snap_id)
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005467{
5468 size_t size;
5469 void *reply_buf;
Alex Elder54cac612013-04-30 00:44:33 -05005470 __le64 snapid;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005471 int ret;
5472 void *p;
5473 void *end;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005474 char *snap_name;
5475
5476 size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN;
5477 reply_buf = kmalloc(size, GFP_KERNEL);
5478 if (!reply_buf)
5479 return ERR_PTR(-ENOMEM);
5480
Alex Elder54cac612013-04-30 00:44:33 -05005481 snapid = cpu_to_le64(snap_id);
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005482 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name,
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005483 "rbd", "get_snapshot_name",
Alex Elder54cac612013-04-30 00:44:33 -05005484 &snapid, sizeof (snapid),
Alex Eldere2a58ee2013-04-30 00:44:33 -05005485 reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06005486 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderf40eb342013-04-25 15:09:42 -05005487 if (ret < 0) {
5488 snap_name = ERR_PTR(ret);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005489 goto out;
Alex Elderf40eb342013-04-25 15:09:42 -05005490 }
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005491
5492 p = reply_buf;
Alex Elderf40eb342013-04-25 15:09:42 -05005493 end = reply_buf + ret;
Alex Eldere5c35532012-10-25 23:34:41 -05005494 snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elderf40eb342013-04-25 15:09:42 -05005495 if (IS_ERR(snap_name))
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005496 goto out;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005497
Alex Elderf40eb342013-04-25 15:09:42 -05005498 dout(" snap_id 0x%016llx snap_name = %s\n",
Alex Elder54cac612013-04-30 00:44:33 -05005499 (unsigned long long)snap_id, snap_name);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005500out:
5501 kfree(reply_buf);
5502
Alex Elderf40eb342013-04-25 15:09:42 -05005503 return snap_name;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005504}
5505
Alex Elder2df3fac2013-05-06 09:51:30 -05005506static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev)
Alex Elder117973f2012-08-31 17:29:55 -05005507{
Alex Elder2df3fac2013-05-06 09:51:30 -05005508 bool first_time = rbd_dev->header.object_prefix == NULL;
Alex Elder117973f2012-08-31 17:29:55 -05005509 int ret;
Alex Elder117973f2012-08-31 17:29:55 -05005510
Josh Durgin1617e402013-06-12 14:43:10 -07005511 ret = rbd_dev_v2_image_size(rbd_dev);
5512 if (ret)
Alex Eldercfbf6372013-05-31 17:40:45 -05005513 return ret;
Josh Durgin1617e402013-06-12 14:43:10 -07005514
Alex Elder2df3fac2013-05-06 09:51:30 -05005515 if (first_time) {
5516 ret = rbd_dev_v2_header_onetime(rbd_dev);
5517 if (ret)
Alex Eldercfbf6372013-05-31 17:40:45 -05005518 return ret;
Alex Elder2df3fac2013-05-06 09:51:30 -05005519 }
5520
Alex Eldercc4a38bd2013-04-30 00:44:33 -05005521 ret = rbd_dev_v2_snap_context(rbd_dev);
Ilya Dryomovd194cd12015-08-31 18:22:10 +03005522 if (ret && first_time) {
5523 kfree(rbd_dev->header.object_prefix);
5524 rbd_dev->header.object_prefix = NULL;
5525 }
Alex Elder117973f2012-08-31 17:29:55 -05005526
5527 return ret;
5528}
5529
Ilya Dryomova720ae02014-07-23 17:11:19 +04005530static int rbd_dev_header_info(struct rbd_device *rbd_dev)
5531{
5532 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
5533
5534 if (rbd_dev->image_format == 1)
5535 return rbd_dev_v1_header_info(rbd_dev);
5536
5537 return rbd_dev_v2_header_info(rbd_dev);
5538}
5539
Alex Elder1ddbe942012-01-29 13:57:44 -06005540/*
Alex Eldere28fff262012-02-02 08:13:30 -06005541 * Skips over white space at *buf, and updates *buf to point to the
5542 * first found non-space character (if any). Returns the length of
Alex Elder593a9e72012-02-07 12:03:37 -06005543 * the token (string of non-white space characters) found. Note
5544 * that *buf must be terminated with '\0'.
Alex Eldere28fff262012-02-02 08:13:30 -06005545 */
5546static inline size_t next_token(const char **buf)
5547{
5548 /*
5549 * These are the characters that produce nonzero for
5550 * isspace() in the "C" and "POSIX" locales.
5551 */
5552 const char *spaces = " \f\n\r\t\v";
5553
5554 *buf += strspn(*buf, spaces); /* Find start of token */
5555
5556 return strcspn(*buf, spaces); /* Return token length */
5557}
5558
5559/*
Alex Elderea3352f2012-07-09 21:04:23 -05005560 * Finds the next token in *buf, dynamically allocates a buffer big
5561 * enough to hold a copy of it, and copies the token into the new
5562 * buffer. The copy is guaranteed to be terminated with '\0'. Note
5563 * that a duplicate buffer is created even for a zero-length token.
5564 *
5565 * Returns a pointer to the newly-allocated duplicate, or a null
5566 * pointer if memory for the duplicate was not available. If
5567 * the lenp argument is a non-null pointer, the length of the token
5568 * (not including the '\0') is returned in *lenp.
5569 *
5570 * If successful, the *buf pointer will be updated to point beyond
5571 * the end of the found token.
5572 *
5573 * Note: uses GFP_KERNEL for allocation.
5574 */
5575static inline char *dup_token(const char **buf, size_t *lenp)
5576{
5577 char *dup;
5578 size_t len;
5579
5580 len = next_token(buf);
Alex Elder4caf35f2012-11-01 08:39:27 -05005581 dup = kmemdup(*buf, len + 1, GFP_KERNEL);
Alex Elderea3352f2012-07-09 21:04:23 -05005582 if (!dup)
5583 return NULL;
Alex Elderea3352f2012-07-09 21:04:23 -05005584 *(dup + len) = '\0';
5585 *buf += len;
5586
5587 if (lenp)
5588 *lenp = len;
5589
5590 return dup;
5591}
5592
5593/*
Alex Elder859c31d2012-10-25 23:34:42 -05005594 * Parse the options provided for an "rbd add" (i.e., rbd image
5595 * mapping) request. These arrive via a write to /sys/bus/rbd/add,
5596 * and the data written is passed here via a NUL-terminated buffer.
5597 * Returns 0 if successful or an error code otherwise.
Alex Elderd22f76e2012-07-12 10:46:35 -05005598 *
Alex Elder859c31d2012-10-25 23:34:42 -05005599 * The information extracted from these options is recorded in
5600 * the other parameters which return dynamically-allocated
5601 * structures:
5602 * ceph_opts
5603 * The address of a pointer that will refer to a ceph options
5604 * structure. Caller must release the returned pointer using
5605 * ceph_destroy_options() when it is no longer needed.
5606 * rbd_opts
5607 * Address of an rbd options pointer. Fully initialized by
5608 * this function; caller must release with kfree().
5609 * spec
5610 * Address of an rbd image specification pointer. Fully
5611 * initialized by this function based on parsed options.
5612 * Caller must release with rbd_spec_put().
5613 *
5614 * The options passed take this form:
5615 * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
5616 * where:
5617 * <mon_addrs>
5618 * A comma-separated list of one or more monitor addresses.
5619 * A monitor address is an ip address, optionally followed
5620 * by a port number (separated by a colon).
5621 * I.e.: ip1[:port1][,ip2[:port2]...]
5622 * <options>
5623 * A comma-separated list of ceph and/or rbd options.
5624 * <pool_name>
5625 * The name of the rados pool containing the rbd image.
5626 * <image_name>
5627 * The name of the image in that pool to map.
5628 * <snap_id>
5629 * An optional snapshot id. If provided, the mapping will
5630 * present data from the image at the time that snapshot was
5631 * created. The image head is used if no snapshot id is
5632 * provided. Snapshot mappings are always read-only.
Alex Eldera725f65e2012-02-02 08:13:30 -06005633 */
Alex Elder859c31d2012-10-25 23:34:42 -05005634static int rbd_add_parse_args(const char *buf,
Alex Elderdc79b112012-10-25 23:34:41 -05005635 struct ceph_options **ceph_opts,
Alex Elder859c31d2012-10-25 23:34:42 -05005636 struct rbd_options **opts,
5637 struct rbd_spec **rbd_spec)
Alex Eldera725f65e2012-02-02 08:13:30 -06005638{
Alex Elderd22f76e2012-07-12 10:46:35 -05005639 size_t len;
Alex Elder859c31d2012-10-25 23:34:42 -05005640 char *options;
Alex Elder0ddebc02012-10-25 23:34:41 -05005641 const char *mon_addrs;
Alex Elderecb4dc222013-04-26 09:43:47 -05005642 char *snap_name;
Alex Elder0ddebc02012-10-25 23:34:41 -05005643 size_t mon_addrs_size;
Alex Elder859c31d2012-10-25 23:34:42 -05005644 struct rbd_spec *spec = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05005645 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05005646 struct ceph_options *copts;
Alex Elderdc79b112012-10-25 23:34:41 -05005647 int ret;
Alex Eldere28fff262012-02-02 08:13:30 -06005648
5649 /* The first four tokens are required */
5650
Alex Elder7ef32142012-02-02 08:13:30 -06005651 len = next_token(&buf);
Alex Elder4fb5d6712012-11-01 10:17:15 -05005652 if (!len) {
5653 rbd_warn(NULL, "no monitor address(es) provided");
5654 return -EINVAL;
5655 }
Alex Elder0ddebc02012-10-25 23:34:41 -05005656 mon_addrs = buf;
Alex Elderf28e5652012-10-25 23:34:41 -05005657 mon_addrs_size = len + 1;
Alex Elder7ef32142012-02-02 08:13:30 -06005658 buf += len;
Alex Eldera725f65e2012-02-02 08:13:30 -06005659
Alex Elderdc79b112012-10-25 23:34:41 -05005660 ret = -EINVAL;
Alex Elderf28e5652012-10-25 23:34:41 -05005661 options = dup_token(&buf, NULL);
5662 if (!options)
Alex Elderdc79b112012-10-25 23:34:41 -05005663 return -ENOMEM;
Alex Elder4fb5d6712012-11-01 10:17:15 -05005664 if (!*options) {
5665 rbd_warn(NULL, "no options provided");
5666 goto out_err;
5667 }
Alex Eldera725f65e2012-02-02 08:13:30 -06005668
Alex Elder859c31d2012-10-25 23:34:42 -05005669 spec = rbd_spec_alloc();
5670 if (!spec)
Alex Elderf28e5652012-10-25 23:34:41 -05005671 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05005672
5673 spec->pool_name = dup_token(&buf, NULL);
5674 if (!spec->pool_name)
5675 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05005676 if (!*spec->pool_name) {
5677 rbd_warn(NULL, "no pool name provided");
5678 goto out_err;
5679 }
Alex Eldere28fff262012-02-02 08:13:30 -06005680
Alex Elder69e7a022012-11-01 08:39:26 -05005681 spec->image_name = dup_token(&buf, NULL);
Alex Elder859c31d2012-10-25 23:34:42 -05005682 if (!spec->image_name)
Alex Elderf28e5652012-10-25 23:34:41 -05005683 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05005684 if (!*spec->image_name) {
5685 rbd_warn(NULL, "no image name provided");
5686 goto out_err;
5687 }
Alex Eldere28fff262012-02-02 08:13:30 -06005688
Alex Elderf28e5652012-10-25 23:34:41 -05005689 /*
5690 * Snapshot name is optional; default is to use "-"
5691 * (indicating the head/no snapshot).
5692 */
Alex Elder3feeb8942012-08-31 17:29:52 -05005693 len = next_token(&buf);
Alex Elder820a5f32012-07-09 21:04:24 -05005694 if (!len) {
Alex Elder3feeb8942012-08-31 17:29:52 -05005695 buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
5696 len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
Alex Elderf28e5652012-10-25 23:34:41 -05005697 } else if (len > RBD_MAX_SNAP_NAME_LEN) {
Alex Elderdc79b112012-10-25 23:34:41 -05005698 ret = -ENAMETOOLONG;
Alex Elderf28e5652012-10-25 23:34:41 -05005699 goto out_err;
Alex Elder849b4262012-07-09 21:04:24 -05005700 }
Alex Elderecb4dc222013-04-26 09:43:47 -05005701 snap_name = kmemdup(buf, len + 1, GFP_KERNEL);
5702 if (!snap_name)
Alex Elderf28e5652012-10-25 23:34:41 -05005703 goto out_mem;
Alex Elderecb4dc222013-04-26 09:43:47 -05005704 *(snap_name + len) = '\0';
5705 spec->snap_name = snap_name;
Alex Eldere5c35532012-10-25 23:34:41 -05005706
Alex Elder0ddebc02012-10-25 23:34:41 -05005707 /* Initialize all rbd options to the defaults */
Alex Eldere28fff262012-02-02 08:13:30 -06005708
Alex Elder4e9afeb2012-10-25 23:34:41 -05005709 rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL);
5710 if (!rbd_opts)
5711 goto out_mem;
5712
5713 rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
Ilya Dryomovb5584182015-06-23 16:21:19 +03005714 rbd_opts->queue_depth = RBD_QUEUE_DEPTH_DEFAULT;
Alex Elderd22f76e2012-07-12 10:46:35 -05005715
Alex Elder859c31d2012-10-25 23:34:42 -05005716 copts = ceph_parse_options(options, mon_addrs,
Alex Elder0ddebc02012-10-25 23:34:41 -05005717 mon_addrs + mon_addrs_size - 1,
Alex Elder4e9afeb2012-10-25 23:34:41 -05005718 parse_rbd_opts_token, rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05005719 if (IS_ERR(copts)) {
5720 ret = PTR_ERR(copts);
Alex Elderdc79b112012-10-25 23:34:41 -05005721 goto out_err;
5722 }
Alex Elder859c31d2012-10-25 23:34:42 -05005723 kfree(options);
5724
5725 *ceph_opts = copts;
Alex Elder4e9afeb2012-10-25 23:34:41 -05005726 *opts = rbd_opts;
Alex Elder859c31d2012-10-25 23:34:42 -05005727 *rbd_spec = spec;
Alex Elder0ddebc02012-10-25 23:34:41 -05005728
Alex Elderdc79b112012-10-25 23:34:41 -05005729 return 0;
Alex Elderf28e5652012-10-25 23:34:41 -05005730out_mem:
Alex Elderdc79b112012-10-25 23:34:41 -05005731 ret = -ENOMEM;
Alex Elderd22f76e2012-07-12 10:46:35 -05005732out_err:
Alex Elder859c31d2012-10-25 23:34:42 -05005733 kfree(rbd_opts);
5734 rbd_spec_put(spec);
Alex Elderf28e5652012-10-25 23:34:41 -05005735 kfree(options);
Alex Elderd22f76e2012-07-12 10:46:35 -05005736
Alex Elderdc79b112012-10-25 23:34:41 -05005737 return ret;
Alex Eldera725f65e2012-02-02 08:13:30 -06005738}
5739
Alex Elder589d30e2012-07-10 20:30:11 -05005740/*
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005741 * Return pool id (>= 0) or a negative error code.
5742 */
5743static int rbd_add_get_pool_id(struct rbd_client *rbdc, const char *pool_name)
5744{
Ilya Dryomova319bf52015-05-15 12:02:17 +03005745 struct ceph_options *opts = rbdc->client->options;
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005746 u64 newest_epoch;
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005747 int tries = 0;
5748 int ret;
5749
5750again:
5751 ret = ceph_pg_poolid_by_name(rbdc->client->osdc.osdmap, pool_name);
5752 if (ret == -ENOENT && tries++ < 1) {
Ilya Dryomovd0b19702016-04-28 16:07:27 +02005753 ret = ceph_monc_get_version(&rbdc->client->monc, "osdmap",
5754 &newest_epoch);
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005755 if (ret < 0)
5756 return ret;
5757
5758 if (rbdc->client->osdc.osdmap->epoch < newest_epoch) {
Ilya Dryomov7cca78c2016-04-28 16:07:28 +02005759 ceph_osdc_maybe_request_map(&rbdc->client->osdc);
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005760 (void) ceph_monc_wait_osdmap(&rbdc->client->monc,
Ilya Dryomova319bf52015-05-15 12:02:17 +03005761 newest_epoch,
5762 opts->mount_timeout);
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005763 goto again;
5764 } else {
5765 /* the osdmap we have is new enough */
5766 return -ENOENT;
5767 }
5768 }
5769
5770 return ret;
5771}
5772
5773/*
Alex Elder589d30e2012-07-10 20:30:11 -05005774 * An rbd format 2 image has a unique identifier, distinct from the
5775 * name given to it by the user. Internally, that identifier is
5776 * what's used to specify the names of objects related to the image.
5777 *
5778 * A special "rbd id" object is used to map an rbd image name to its
5779 * id. If that object doesn't exist, then there is no v2 rbd image
5780 * with the supplied name.
5781 *
5782 * This function will record the given rbd_dev's image_id field if
5783 * it can be determined, and in that case will return 0. If any
5784 * errors occur a negative errno will be returned and the rbd_dev's
5785 * image_id field will be unchanged (and should be NULL).
5786 */
5787static int rbd_dev_image_id(struct rbd_device *rbd_dev)
5788{
5789 int ret;
5790 size_t size;
5791 char *object_name;
5792 void *response;
Alex Elderc0fba362013-04-25 23:15:08 -05005793 char *image_id;
Alex Elder2f82ee52012-10-30 19:40:33 -05005794
Alex Elder589d30e2012-07-10 20:30:11 -05005795 /*
Alex Elder2c0d0a12012-10-30 19:40:33 -05005796 * When probing a parent image, the image id is already
5797 * known (and the image name likely is not). There's no
Alex Elderc0fba362013-04-25 23:15:08 -05005798 * need to fetch the image id again in this case. We
5799 * do still need to set the image format though.
Alex Elder2c0d0a12012-10-30 19:40:33 -05005800 */
Alex Elderc0fba362013-04-25 23:15:08 -05005801 if (rbd_dev->spec->image_id) {
5802 rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1;
5803
Alex Elder2c0d0a12012-10-30 19:40:33 -05005804 return 0;
Alex Elderc0fba362013-04-25 23:15:08 -05005805 }
Alex Elder2c0d0a12012-10-30 19:40:33 -05005806
5807 /*
Alex Elder589d30e2012-07-10 20:30:11 -05005808 * First, see if the format 2 image id file exists, and if
5809 * so, get the image's persistent id from it.
5810 */
Alex Elder69e7a022012-11-01 08:39:26 -05005811 size = sizeof (RBD_ID_PREFIX) + strlen(rbd_dev->spec->image_name);
Alex Elder589d30e2012-07-10 20:30:11 -05005812 object_name = kmalloc(size, GFP_NOIO);
5813 if (!object_name)
5814 return -ENOMEM;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05005815 sprintf(object_name, "%s%s", RBD_ID_PREFIX, rbd_dev->spec->image_name);
Alex Elder589d30e2012-07-10 20:30:11 -05005816 dout("rbd id object name is %s\n", object_name);
5817
5818 /* Response will be an encoded string, which includes a length */
5819
5820 size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
5821 response = kzalloc(size, GFP_NOIO);
5822 if (!response) {
5823 ret = -ENOMEM;
5824 goto out;
5825 }
5826
Alex Elderc0fba362013-04-25 23:15:08 -05005827 /* If it doesn't exist we'll assume it's a format 1 image */
5828
Alex Elder36be9a72013-01-19 00:30:28 -06005829 ret = rbd_obj_method_sync(rbd_dev, object_name,
Alex Elder41579762013-04-21 12:14:45 -05005830 "rbd", "get_id", NULL, 0,
Alex Eldere2a58ee2013-04-30 00:44:33 -05005831 response, RBD_IMAGE_ID_LEN_MAX);
Alex Elder36be9a72013-01-19 00:30:28 -06005832 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderc0fba362013-04-25 23:15:08 -05005833 if (ret == -ENOENT) {
5834 image_id = kstrdup("", GFP_KERNEL);
5835 ret = image_id ? 0 : -ENOMEM;
5836 if (!ret)
5837 rbd_dev->image_format = 1;
Ilya Dryomov7dd440c2014-09-11 18:49:18 +04005838 } else if (ret >= 0) {
Alex Elderc0fba362013-04-25 23:15:08 -05005839 void *p = response;
Alex Elder589d30e2012-07-10 20:30:11 -05005840
Alex Elderc0fba362013-04-25 23:15:08 -05005841 image_id = ceph_extract_encoded_string(&p, p + ret,
Alex Elder979ed482012-11-01 08:39:26 -05005842 NULL, GFP_NOIO);
Duan Jiong461f7582014-04-11 16:38:12 +08005843 ret = PTR_ERR_OR_ZERO(image_id);
Alex Elderc0fba362013-04-25 23:15:08 -05005844 if (!ret)
5845 rbd_dev->image_format = 2;
Alex Elderc0fba362013-04-25 23:15:08 -05005846 }
5847
5848 if (!ret) {
5849 rbd_dev->spec->image_id = image_id;
5850 dout("image_id is %s\n", image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05005851 }
5852out:
5853 kfree(response);
5854 kfree(object_name);
5855
5856 return ret;
5857}
5858
Alex Elder3abef3b2013-05-13 20:35:37 -05005859/*
5860 * Undo whatever state changes are made by v1 or v2 header info
5861 * call.
5862 */
Alex Elder6fd48b32013-04-28 23:32:34 -05005863static void rbd_dev_unprobe(struct rbd_device *rbd_dev)
5864{
5865 struct rbd_image_header *header;
5866
Ilya Dryomove69b8d42015-01-19 12:06:14 +03005867 rbd_dev_parent_put(rbd_dev);
Alex Elder6fd48b32013-04-28 23:32:34 -05005868
5869 /* Free dynamic fields from the header, then zero it out */
5870
5871 header = &rbd_dev->header;
Alex Elder812164f82013-04-30 00:44:32 -05005872 ceph_put_snap_context(header->snapc);
Alex Elder6fd48b32013-04-28 23:32:34 -05005873 kfree(header->snap_sizes);
5874 kfree(header->snap_names);
5875 kfree(header->object_prefix);
5876 memset(header, 0, sizeof (*header));
5877}
5878
Alex Elder2df3fac2013-05-06 09:51:30 -05005879static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev)
Alex Eldera30b71b2012-07-10 20:30:11 -05005880{
5881 int ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05005882
Alex Elder1e130192012-07-03 16:01:19 -05005883 ret = rbd_dev_v2_object_prefix(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05005884 if (ret)
Alex Elder1e130192012-07-03 16:01:19 -05005885 goto out_err;
Alex Elderb1b54022012-07-03 16:01:19 -05005886
Alex Elder2df3fac2013-05-06 09:51:30 -05005887 /*
5888 * Get the and check features for the image. Currently the
5889 * features are assumed to never change.
5890 */
Alex Elderb1b54022012-07-03 16:01:19 -05005891 ret = rbd_dev_v2_features(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05005892 if (ret)
Alex Elderb1b54022012-07-03 16:01:19 -05005893 goto out_err;
Alex Elder35d489f2012-07-03 16:01:19 -05005894
Alex Eldercc070d52013-04-21 12:14:45 -05005895 /* If the image supports fancy striping, get its parameters */
5896
5897 if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) {
5898 ret = rbd_dev_v2_striping_info(rbd_dev);
5899 if (ret < 0)
5900 goto out_err;
5901 }
Alex Elder2df3fac2013-05-06 09:51:30 -05005902 /* No support for crypto and compression type format 2 images */
Alex Eldera30b71b2012-07-10 20:30:11 -05005903
Alex Elder35152972012-08-31 17:29:55 -05005904 return 0;
Alex Elder9d475de2012-07-03 16:01:19 -05005905out_err:
Alex Elder642a2532013-05-06 17:40:33 -05005906 rbd_dev->header.features = 0;
Alex Elder1e130192012-07-03 16:01:19 -05005907 kfree(rbd_dev->header.object_prefix);
5908 rbd_dev->header.object_prefix = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05005909
5910 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05005911}
5912
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005913/*
5914 * @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() ->
5915 * rbd_dev_image_probe() recursion depth, which means it's also the
5916 * length of the already discovered part of the parent chain.
5917 */
5918static int rbd_dev_probe_parent(struct rbd_device *rbd_dev, int depth)
Alex Elder83a06262012-10-30 15:47:17 -05005919{
Alex Elder2f82ee52012-10-30 19:40:33 -05005920 struct rbd_device *parent = NULL;
Alex Elder124afba2013-04-26 15:44:36 -05005921 int ret;
5922
5923 if (!rbd_dev->parent_spec)
5924 return 0;
Alex Elder124afba2013-04-26 15:44:36 -05005925
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005926 if (++depth > RBD_MAX_PARENT_CHAIN_LEN) {
5927 pr_info("parent chain is too long (%d)\n", depth);
5928 ret = -EINVAL;
5929 goto out_err;
5930 }
5931
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02005932 parent = __rbd_dev_create(rbd_dev->rbd_client, rbd_dev->parent_spec);
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005933 if (!parent) {
5934 ret = -ENOMEM;
Alex Elder124afba2013-04-26 15:44:36 -05005935 goto out_err;
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005936 }
5937
5938 /*
5939 * Images related by parent/child relationships always share
5940 * rbd_client and spec/parent_spec, so bump their refcounts.
5941 */
5942 __rbd_get_client(rbd_dev->rbd_client);
5943 rbd_spec_get(rbd_dev->parent_spec);
Alex Elder124afba2013-04-26 15:44:36 -05005944
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005945 ret = rbd_dev_image_probe(parent, depth);
Alex Elder124afba2013-04-26 15:44:36 -05005946 if (ret < 0)
5947 goto out_err;
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005948
Alex Elder124afba2013-04-26 15:44:36 -05005949 rbd_dev->parent = parent;
Alex Eldera2acd002013-05-08 22:50:04 -05005950 atomic_set(&rbd_dev->parent_ref, 1);
Alex Elder124afba2013-04-26 15:44:36 -05005951 return 0;
Alex Elder124afba2013-04-26 15:44:36 -05005952
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005953out_err:
5954 rbd_dev_unparent(rbd_dev);
Markus Elfring1761b222015-11-23 20:16:45 +01005955 rbd_dev_destroy(parent);
Alex Elder124afba2013-04-26 15:44:36 -05005956 return ret;
5957}
5958
Ilya Dryomov811c6682016-04-15 16:22:16 +02005959/*
5960 * rbd_dev->header_rwsem must be locked for write and will be unlocked
5961 * upon return.
5962 */
Alex Elder200a6a82013-04-28 23:32:34 -05005963static int rbd_dev_device_setup(struct rbd_device *rbd_dev)
Alex Elder124afba2013-04-26 15:44:36 -05005964{
Alex Elder83a06262012-10-30 15:47:17 -05005965 int ret;
Alex Elder83a06262012-10-30 15:47:17 -05005966
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005967 /* Record our major and minor device numbers. */
Alex Elder83a06262012-10-30 15:47:17 -05005968
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005969 if (!single_major) {
5970 ret = register_blkdev(0, rbd_dev->name);
5971 if (ret < 0)
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02005972 goto err_out_unlock;
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005973
5974 rbd_dev->major = ret;
5975 rbd_dev->minor = 0;
5976 } else {
5977 rbd_dev->major = rbd_major;
5978 rbd_dev->minor = rbd_dev_id_to_minor(rbd_dev->dev_id);
5979 }
Alex Elder83a06262012-10-30 15:47:17 -05005980
5981 /* Set up the blkdev mapping. */
5982
5983 ret = rbd_init_disk(rbd_dev);
5984 if (ret)
5985 goto err_out_blkdev;
5986
Alex Elderf35a4de2013-05-06 09:51:29 -05005987 ret = rbd_dev_mapping_set(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05005988 if (ret)
5989 goto err_out_disk;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04005990
Alex Elderf35a4de2013-05-06 09:51:29 -05005991 set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
Josh Durgin22001f62013-09-30 20:10:04 -07005992 set_disk_ro(rbd_dev->disk, rbd_dev->mapping.read_only);
Alex Elderf35a4de2013-05-06 09:51:29 -05005993
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02005994 dev_set_name(&rbd_dev->dev, "%d", rbd_dev->dev_id);
5995 ret = device_add(&rbd_dev->dev);
Alex Elderf35a4de2013-05-06 09:51:29 -05005996 if (ret)
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04005997 goto err_out_mapping;
Alex Elder83a06262012-10-30 15:47:17 -05005998
Alex Elder83a06262012-10-30 15:47:17 -05005999 /* Everything's ready. Announce the disk to the world. */
6000
Alex Elder129b79d2013-04-26 15:44:36 -05006001 set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
Ilya Dryomov811c6682016-04-15 16:22:16 +02006002 up_write(&rbd_dev->header_rwsem);
Alex Elder83a06262012-10-30 15:47:17 -05006003
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02006004 spin_lock(&rbd_dev_list_lock);
6005 list_add_tail(&rbd_dev->node, &rbd_dev_list);
6006 spin_unlock(&rbd_dev_list_lock);
6007
Ilya Dryomov811c6682016-04-15 16:22:16 +02006008 add_disk(rbd_dev->disk);
Ilya Dryomovca7909e2016-08-18 18:38:41 +02006009 pr_info("%s: capacity %llu features 0x%llx\n", rbd_dev->disk->disk_name,
6010 (unsigned long long)get_capacity(rbd_dev->disk) << SECTOR_SHIFT,
6011 rbd_dev->header.features);
Alex Elder83a06262012-10-30 15:47:17 -05006012
6013 return ret;
Alex Elder2f82ee52012-10-30 19:40:33 -05006014
Alex Elderf35a4de2013-05-06 09:51:29 -05006015err_out_mapping:
6016 rbd_dev_mapping_clear(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05006017err_out_disk:
6018 rbd_free_disk(rbd_dev);
6019err_out_blkdev:
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006020 if (!single_major)
6021 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Ilya Dryomov811c6682016-04-15 16:22:16 +02006022err_out_unlock:
6023 up_write(&rbd_dev->header_rwsem);
Alex Elder83a06262012-10-30 15:47:17 -05006024 return ret;
6025}
6026
Alex Elder332bb122013-04-27 09:59:30 -05006027static int rbd_dev_header_name(struct rbd_device *rbd_dev)
6028{
6029 struct rbd_spec *spec = rbd_dev->spec;
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006030 int ret;
Alex Elder332bb122013-04-27 09:59:30 -05006031
6032 /* Record the header object name for this rbd image. */
6033
6034 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
6035
Yan, Zheng76271512016-02-03 21:24:49 +08006036 rbd_dev->header_oloc.pool = rbd_dev->layout.pool_id;
Alex Elder332bb122013-04-27 09:59:30 -05006037 if (rbd_dev->image_format == 1)
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006038 ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s",
6039 spec->image_name, RBD_SUFFIX);
Alex Elder332bb122013-04-27 09:59:30 -05006040 else
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006041 ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s",
6042 RBD_HEADER_PREFIX, spec->image_id);
Alex Elder332bb122013-04-27 09:59:30 -05006043
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006044 return ret;
Alex Elder332bb122013-04-27 09:59:30 -05006045}
6046
Alex Elder200a6a82013-04-28 23:32:34 -05006047static void rbd_dev_image_release(struct rbd_device *rbd_dev)
6048{
Alex Elder6fd48b32013-04-28 23:32:34 -05006049 rbd_dev_unprobe(rbd_dev);
Alex Elder6fd48b32013-04-28 23:32:34 -05006050 rbd_dev->image_format = 0;
6051 kfree(rbd_dev->spec->image_id);
6052 rbd_dev->spec->image_id = NULL;
6053
Alex Elder200a6a82013-04-28 23:32:34 -05006054 rbd_dev_destroy(rbd_dev);
6055}
6056
Alex Eldera30b71b2012-07-10 20:30:11 -05006057/*
6058 * Probe for the existence of the header object for the given rbd
Alex Elder1f3ef782013-05-06 17:40:33 -05006059 * device. If this image is the one being mapped (i.e., not a
6060 * parent), initiate a watch on its header object before using that
6061 * object to get detailed information about the rbd image.
Alex Eldera30b71b2012-07-10 20:30:11 -05006062 */
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006063static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth)
Alex Eldera30b71b2012-07-10 20:30:11 -05006064{
6065 int ret;
6066
6067 /*
Alex Elder3abef3b2013-05-13 20:35:37 -05006068 * Get the id from the image id object. Unless there's an
6069 * error, rbd_dev->spec->image_id will be filled in with
6070 * a dynamically-allocated string, and rbd_dev->image_format
6071 * will be set to either 1 or 2.
Alex Eldera30b71b2012-07-10 20:30:11 -05006072 */
6073 ret = rbd_dev_image_id(rbd_dev);
6074 if (ret)
Alex Elderc0fba362013-04-25 23:15:08 -05006075 return ret;
Alex Elderc0fba362013-04-25 23:15:08 -05006076
Alex Elder332bb122013-04-27 09:59:30 -05006077 ret = rbd_dev_header_name(rbd_dev);
6078 if (ret)
6079 goto err_out_format;
6080
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006081 if (!depth) {
Ilya Dryomov99d16942016-08-12 16:11:41 +02006082 ret = rbd_register_watch(rbd_dev);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006083 if (ret) {
6084 if (ret == -ENOENT)
6085 pr_info("image %s/%s does not exist\n",
6086 rbd_dev->spec->pool_name,
6087 rbd_dev->spec->image_name);
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006088 goto err_out_format;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006089 }
Alex Elder1f3ef782013-05-06 17:40:33 -05006090 }
Alex Elderb644de22013-04-27 09:59:31 -05006091
Ilya Dryomova720ae02014-07-23 17:11:19 +04006092 ret = rbd_dev_header_info(rbd_dev);
Alex Elder5655c4d2013-04-25 23:15:08 -05006093 if (ret)
Alex Elderb644de22013-04-27 09:59:31 -05006094 goto err_out_watch;
Alex Elder83a06262012-10-30 15:47:17 -05006095
Ilya Dryomov04077592014-07-23 17:11:20 +04006096 /*
6097 * If this image is the one being mapped, we have pool name and
6098 * id, image name and id, and snap name - need to fill snap id.
6099 * Otherwise this is a parent image, identified by pool, image
6100 * and snap ids - need to fill in names for those ids.
6101 */
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006102 if (!depth)
Ilya Dryomov04077592014-07-23 17:11:20 +04006103 ret = rbd_spec_fill_snap_id(rbd_dev);
6104 else
6105 ret = rbd_spec_fill_names(rbd_dev);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006106 if (ret) {
6107 if (ret == -ENOENT)
6108 pr_info("snap %s/%s@%s does not exist\n",
6109 rbd_dev->spec->pool_name,
6110 rbd_dev->spec->image_name,
6111 rbd_dev->spec->snap_name);
Alex Elder33dca392013-04-30 00:44:33 -05006112 goto err_out_probe;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006113 }
Alex Elder9bb81c92013-04-27 09:59:30 -05006114
Ilya Dryomove8f59b52014-07-24 10:42:13 +04006115 if (rbd_dev->header.features & RBD_FEATURE_LAYERING) {
6116 ret = rbd_dev_v2_parent_info(rbd_dev);
6117 if (ret)
6118 goto err_out_probe;
6119
6120 /*
6121 * Need to warn users if this image is the one being
6122 * mapped and has a parent.
6123 */
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006124 if (!depth && rbd_dev->parent_spec)
Ilya Dryomove8f59b52014-07-24 10:42:13 +04006125 rbd_warn(rbd_dev,
6126 "WARNING: kernel layering is EXPERIMENTAL!");
6127 }
6128
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006129 ret = rbd_dev_probe_parent(rbd_dev, depth);
Alex Elder30d60ba2013-05-06 09:51:30 -05006130 if (ret)
6131 goto err_out_probe;
Alex Elder83a06262012-10-30 15:47:17 -05006132
Alex Elder30d60ba2013-05-06 09:51:30 -05006133 dout("discovered format %u image, header name is %s\n",
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006134 rbd_dev->image_format, rbd_dev->header_oid.name);
Alex Elder30d60ba2013-05-06 09:51:30 -05006135 return 0;
Ilya Dryomove8f59b52014-07-24 10:42:13 +04006136
Alex Elder6fd48b32013-04-28 23:32:34 -05006137err_out_probe:
6138 rbd_dev_unprobe(rbd_dev);
Alex Elderb644de22013-04-27 09:59:31 -05006139err_out_watch:
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006140 if (!depth)
Ilya Dryomov99d16942016-08-12 16:11:41 +02006141 rbd_unregister_watch(rbd_dev);
Alex Elder332bb122013-04-27 09:59:30 -05006142err_out_format:
6143 rbd_dev->image_format = 0;
Alex Elder5655c4d2013-04-25 23:15:08 -05006144 kfree(rbd_dev->spec->image_id);
6145 rbd_dev->spec->image_id = NULL;
Alex Elder5655c4d2013-04-25 23:15:08 -05006146 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05006147}
6148
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006149static ssize_t do_rbd_add(struct bus_type *bus,
6150 const char *buf,
6151 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006152{
Alex Eldercb8627c2012-07-09 21:04:23 -05006153 struct rbd_device *rbd_dev = NULL;
Alex Elderdc79b112012-10-25 23:34:41 -05006154 struct ceph_options *ceph_opts = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05006155 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05006156 struct rbd_spec *spec = NULL;
Alex Elder9d3997f2012-10-25 23:34:42 -05006157 struct rbd_client *rbdc;
Alex Elder51344a32013-05-06 07:40:30 -05006158 bool read_only;
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02006159 int rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006160
6161 if (!try_module_get(THIS_MODULE))
6162 return -ENODEV;
6163
Alex Eldera725f65e2012-02-02 08:13:30 -06006164 /* parse add command */
Alex Elder859c31d2012-10-25 23:34:42 -05006165 rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec);
Alex Elderdc79b112012-10-25 23:34:41 -05006166 if (rc < 0)
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006167 goto out;
Alex Eldera725f65e2012-02-02 08:13:30 -06006168
Alex Elder9d3997f2012-10-25 23:34:42 -05006169 rbdc = rbd_get_client(ceph_opts);
6170 if (IS_ERR(rbdc)) {
6171 rc = PTR_ERR(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05006172 goto err_out_args;
Alex Elder9d3997f2012-10-25 23:34:42 -05006173 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006174
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006175 /* pick the pool */
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04006176 rc = rbd_add_get_pool_id(rbdc, spec->pool_name);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006177 if (rc < 0) {
6178 if (rc == -ENOENT)
6179 pr_info("pool %s does not exist\n", spec->pool_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006180 goto err_out_client;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006181 }
Alex Elderc0cd10db2013-04-26 09:43:47 -05006182 spec->pool_id = (u64)rc;
Alex Elder859c31d2012-10-25 23:34:42 -05006183
Ilya Dryomovd1475432015-06-22 13:24:48 +03006184 rbd_dev = rbd_dev_create(rbdc, spec, rbd_opts);
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02006185 if (!rbd_dev) {
6186 rc = -ENOMEM;
Alex Elderbd4ba652012-10-25 23:34:42 -05006187 goto err_out_client;
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02006188 }
Alex Elderc53d5892012-10-25 23:34:42 -05006189 rbdc = NULL; /* rbd_dev now owns this */
6190 spec = NULL; /* rbd_dev now owns this */
Ilya Dryomovd1475432015-06-22 13:24:48 +03006191 rbd_opts = NULL; /* rbd_dev now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006192
Ilya Dryomov811c6682016-04-15 16:22:16 +02006193 down_write(&rbd_dev->header_rwsem);
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006194 rc = rbd_dev_image_probe(rbd_dev, 0);
Alex Eldera30b71b2012-07-10 20:30:11 -05006195 if (rc < 0)
Alex Elderc53d5892012-10-25 23:34:42 -05006196 goto err_out_rbd_dev;
Alex Elder05fd6f62012-08-29 17:11:07 -05006197
Alex Elder7ce4eef2013-05-06 17:40:33 -05006198 /* If we are mapping a snapshot it must be marked read-only */
6199
Ilya Dryomovd1475432015-06-22 13:24:48 +03006200 read_only = rbd_dev->opts->read_only;
Alex Elder7ce4eef2013-05-06 17:40:33 -05006201 if (rbd_dev->spec->snap_id != CEPH_NOSNAP)
6202 read_only = true;
6203 rbd_dev->mapping.read_only = read_only;
6204
Alex Elderb536f692013-04-28 23:32:34 -05006205 rc = rbd_dev_device_setup(rbd_dev);
Alex Elder3abef3b2013-05-13 20:35:37 -05006206 if (rc) {
Ilya Dryomove37180c2013-12-16 18:02:41 +02006207 /*
Ilya Dryomov99d16942016-08-12 16:11:41 +02006208 * rbd_unregister_watch() can't be moved into
Ilya Dryomove37180c2013-12-16 18:02:41 +02006209 * rbd_dev_image_release() without refactoring, see
6210 * commit 1f3ef78861ac.
6211 */
Ilya Dryomov99d16942016-08-12 16:11:41 +02006212 rbd_unregister_watch(rbd_dev);
Alex Elder3abef3b2013-05-13 20:35:37 -05006213 rbd_dev_image_release(rbd_dev);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006214 goto out;
Alex Elder3abef3b2013-05-13 20:35:37 -05006215 }
Alex Elderb536f692013-04-28 23:32:34 -05006216
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006217 rc = count;
6218out:
6219 module_put(THIS_MODULE);
6220 return rc;
Alex Elder3abef3b2013-05-13 20:35:37 -05006221
Alex Elderc53d5892012-10-25 23:34:42 -05006222err_out_rbd_dev:
Ilya Dryomov811c6682016-04-15 16:22:16 +02006223 up_write(&rbd_dev->header_rwsem);
Alex Elderc53d5892012-10-25 23:34:42 -05006224 rbd_dev_destroy(rbd_dev);
Alex Elderbd4ba652012-10-25 23:34:42 -05006225err_out_client:
Alex Elder9d3997f2012-10-25 23:34:42 -05006226 rbd_put_client(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05006227err_out_args:
Alex Elder859c31d2012-10-25 23:34:42 -05006228 rbd_spec_put(spec);
Ilya Dryomovd1475432015-06-22 13:24:48 +03006229 kfree(rbd_opts);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006230 goto out;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006231}
6232
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006233static ssize_t rbd_add(struct bus_type *bus,
6234 const char *buf,
6235 size_t count)
6236{
6237 if (single_major)
6238 return -EINVAL;
6239
6240 return do_rbd_add(bus, buf, count);
6241}
6242
6243static ssize_t rbd_add_single_major(struct bus_type *bus,
6244 const char *buf,
6245 size_t count)
6246{
6247 return do_rbd_add(bus, buf, count);
6248}
6249
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006250static void rbd_dev_device_release(struct rbd_device *rbd_dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006251{
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006252 rbd_free_disk(rbd_dev);
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02006253
6254 spin_lock(&rbd_dev_list_lock);
6255 list_del_init(&rbd_dev->node);
6256 spin_unlock(&rbd_dev_list_lock);
6257
Alex Elder200a6a82013-04-28 23:32:34 -05006258 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006259 device_del(&rbd_dev->dev);
Alex Elder6d80b132013-05-06 07:40:30 -05006260 rbd_dev_mapping_clear(rbd_dev);
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006261 if (!single_major)
6262 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006263}
6264
Alex Elder05a46af2013-04-26 15:44:36 -05006265static void rbd_dev_remove_parent(struct rbd_device *rbd_dev)
6266{
Alex Elderad945fc2013-04-26 15:44:36 -05006267 while (rbd_dev->parent) {
Alex Elder05a46af2013-04-26 15:44:36 -05006268 struct rbd_device *first = rbd_dev;
6269 struct rbd_device *second = first->parent;
6270 struct rbd_device *third;
6271
6272 /*
6273 * Follow to the parent with no grandparent and
6274 * remove it.
6275 */
6276 while (second && (third = second->parent)) {
6277 first = second;
6278 second = third;
6279 }
Alex Elderad945fc2013-04-26 15:44:36 -05006280 rbd_assert(second);
Alex Elder8ad42cd2013-04-28 23:32:34 -05006281 rbd_dev_image_release(second);
Alex Elderad945fc2013-04-26 15:44:36 -05006282 first->parent = NULL;
6283 first->parent_overlap = 0;
6284
6285 rbd_assert(first->parent_spec);
Alex Elder05a46af2013-04-26 15:44:36 -05006286 rbd_spec_put(first->parent_spec);
6287 first->parent_spec = NULL;
Alex Elder05a46af2013-04-26 15:44:36 -05006288 }
6289}
6290
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006291static ssize_t do_rbd_remove(struct bus_type *bus,
6292 const char *buf,
6293 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006294{
6295 struct rbd_device *rbd_dev = NULL;
Alex Elder751cc0e2013-05-31 15:17:01 -05006296 struct list_head *tmp;
6297 int dev_id;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006298 unsigned long ul;
Alex Elder82a442d2013-05-31 17:40:44 -05006299 bool already = false;
Alex Elder0d8189e2013-04-27 09:59:30 -05006300 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006301
Jingoo Hanbb8e0e82013-09-11 14:20:07 -07006302 ret = kstrtoul(buf, 10, &ul);
Alex Elder0d8189e2013-04-27 09:59:30 -05006303 if (ret)
6304 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006305
6306 /* convert to int; abort if we lost anything in the conversion */
Alex Elder751cc0e2013-05-31 15:17:01 -05006307 dev_id = (int)ul;
6308 if (dev_id != ul)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006309 return -EINVAL;
6310
Alex Elder751cc0e2013-05-31 15:17:01 -05006311 ret = -ENOENT;
6312 spin_lock(&rbd_dev_list_lock);
6313 list_for_each(tmp, &rbd_dev_list) {
6314 rbd_dev = list_entry(tmp, struct rbd_device, node);
6315 if (rbd_dev->dev_id == dev_id) {
6316 ret = 0;
6317 break;
6318 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006319 }
Alex Elder751cc0e2013-05-31 15:17:01 -05006320 if (!ret) {
6321 spin_lock_irq(&rbd_dev->lock);
6322 if (rbd_dev->open_count)
6323 ret = -EBUSY;
6324 else
Alex Elder82a442d2013-05-31 17:40:44 -05006325 already = test_and_set_bit(RBD_DEV_FLAG_REMOVING,
6326 &rbd_dev->flags);
Alex Elder751cc0e2013-05-31 15:17:01 -05006327 spin_unlock_irq(&rbd_dev->lock);
6328 }
6329 spin_unlock(&rbd_dev_list_lock);
Alex Elder82a442d2013-05-31 17:40:44 -05006330 if (ret < 0 || already)
Alex Elder1ba0f1e2013-05-31 15:17:01 -05006331 return ret;
Alex Elder751cc0e2013-05-31 15:17:01 -05006332
Ilya Dryomoved95b212016-08-12 16:40:02 +02006333 down_write(&rbd_dev->lock_rwsem);
6334 if (__rbd_is_lock_owner(rbd_dev))
6335 rbd_unlock(rbd_dev);
6336 up_write(&rbd_dev->lock_rwsem);
Ilya Dryomov99d16942016-08-12 16:11:41 +02006337 rbd_unregister_watch(rbd_dev);
Ilya Dryomovfca27062013-12-16 18:02:40 +02006338
Josh Durgin98752012013-08-29 17:26:31 -07006339 /*
6340 * Don't free anything from rbd_dev->disk until after all
6341 * notifies are completely processed. Otherwise
6342 * rbd_bus_del_dev() will race with rbd_watch_cb(), resulting
6343 * in a potential use after free of rbd_dev->disk or rbd_dev.
6344 */
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006345 rbd_dev_device_release(rbd_dev);
Alex Elder8ad42cd2013-04-28 23:32:34 -05006346 rbd_dev_image_release(rbd_dev);
Alex Elderaafb2302012-09-06 16:00:54 -05006347
Alex Elder1ba0f1e2013-05-31 15:17:01 -05006348 return count;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006349}
6350
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006351static ssize_t rbd_remove(struct bus_type *bus,
6352 const char *buf,
6353 size_t count)
6354{
6355 if (single_major)
6356 return -EINVAL;
6357
6358 return do_rbd_remove(bus, buf, count);
6359}
6360
6361static ssize_t rbd_remove_single_major(struct bus_type *bus,
6362 const char *buf,
6363 size_t count)
6364{
6365 return do_rbd_remove(bus, buf, count);
6366}
6367
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006368/*
6369 * create control files in sysfs
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006370 * /sys/bus/rbd/...
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006371 */
6372static int rbd_sysfs_init(void)
6373{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006374 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006375
Alex Elderfed4c142012-02-07 12:03:36 -06006376 ret = device_register(&rbd_root_dev);
Alex Elder21079782012-01-24 10:08:36 -06006377 if (ret < 0)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006378 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006379
Alex Elderfed4c142012-02-07 12:03:36 -06006380 ret = bus_register(&rbd_bus_type);
6381 if (ret < 0)
6382 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006383
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006384 return ret;
6385}
6386
6387static void rbd_sysfs_cleanup(void)
6388{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006389 bus_unregister(&rbd_bus_type);
Alex Elderfed4c142012-02-07 12:03:36 -06006390 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006391}
6392
Alex Elder1c2a9df2013-05-01 12:43:03 -05006393static int rbd_slab_init(void)
6394{
6395 rbd_assert(!rbd_img_request_cache);
Geliang Tang03d94402016-03-13 15:17:32 +08006396 rbd_img_request_cache = KMEM_CACHE(rbd_img_request, 0);
Alex Elder868311b2013-05-01 12:43:03 -05006397 if (!rbd_img_request_cache)
6398 return -ENOMEM;
6399
6400 rbd_assert(!rbd_obj_request_cache);
Geliang Tang03d94402016-03-13 15:17:32 +08006401 rbd_obj_request_cache = KMEM_CACHE(rbd_obj_request, 0);
Alex Elder78c2a442013-05-01 12:43:04 -05006402 if (!rbd_obj_request_cache)
6403 goto out_err;
6404
6405 rbd_assert(!rbd_segment_name_cache);
6406 rbd_segment_name_cache = kmem_cache_create("rbd_segment_name",
Ilya Dryomov2d0ebc52014-01-27 17:40:18 +02006407 CEPH_MAX_OID_NAME_LEN + 1, 1, 0, NULL);
Alex Elder78c2a442013-05-01 12:43:04 -05006408 if (rbd_segment_name_cache)
Alex Elder1c2a9df2013-05-01 12:43:03 -05006409 return 0;
Alex Elder78c2a442013-05-01 12:43:04 -05006410out_err:
Julia Lawall13bf2832015-09-13 14:15:26 +02006411 kmem_cache_destroy(rbd_obj_request_cache);
6412 rbd_obj_request_cache = NULL;
Alex Elder1c2a9df2013-05-01 12:43:03 -05006413
Alex Elder868311b2013-05-01 12:43:03 -05006414 kmem_cache_destroy(rbd_img_request_cache);
6415 rbd_img_request_cache = NULL;
6416
Alex Elder1c2a9df2013-05-01 12:43:03 -05006417 return -ENOMEM;
6418}
6419
6420static void rbd_slab_exit(void)
6421{
Alex Elder78c2a442013-05-01 12:43:04 -05006422 rbd_assert(rbd_segment_name_cache);
6423 kmem_cache_destroy(rbd_segment_name_cache);
6424 rbd_segment_name_cache = NULL;
6425
Alex Elder868311b2013-05-01 12:43:03 -05006426 rbd_assert(rbd_obj_request_cache);
6427 kmem_cache_destroy(rbd_obj_request_cache);
6428 rbd_obj_request_cache = NULL;
6429
Alex Elder1c2a9df2013-05-01 12:43:03 -05006430 rbd_assert(rbd_img_request_cache);
6431 kmem_cache_destroy(rbd_img_request_cache);
6432 rbd_img_request_cache = NULL;
6433}
6434
Alex Eldercc344fa2013-02-19 12:25:56 -06006435static int __init rbd_init(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006436{
6437 int rc;
6438
Alex Elder1e32d342013-01-30 11:13:33 -06006439 if (!libceph_compatible(NULL)) {
6440 rbd_warn(NULL, "libceph incompatibility (quitting)");
Alex Elder1e32d342013-01-30 11:13:33 -06006441 return -EINVAL;
6442 }
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006443
Alex Elder1c2a9df2013-05-01 12:43:03 -05006444 rc = rbd_slab_init();
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006445 if (rc)
6446 return rc;
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006447
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006448 /*
6449 * The number of active work items is limited by the number of
Ilya Dryomovf77303b2015-04-22 18:28:13 +03006450 * rbd devices * queue depth, so leave @max_active at default.
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006451 */
6452 rbd_wq = alloc_workqueue(RBD_DRV_NAME, WQ_MEM_RECLAIM, 0);
6453 if (!rbd_wq) {
6454 rc = -ENOMEM;
6455 goto err_out_slab;
6456 }
6457
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006458 if (single_major) {
6459 rbd_major = register_blkdev(0, RBD_DRV_NAME);
6460 if (rbd_major < 0) {
6461 rc = rbd_major;
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006462 goto err_out_wq;
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006463 }
6464 }
6465
Alex Elder1c2a9df2013-05-01 12:43:03 -05006466 rc = rbd_sysfs_init();
6467 if (rc)
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006468 goto err_out_blkdev;
Alex Elder1c2a9df2013-05-01 12:43:03 -05006469
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006470 if (single_major)
6471 pr_info("loaded (major %d)\n", rbd_major);
6472 else
6473 pr_info("loaded\n");
6474
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006475 return 0;
6476
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006477err_out_blkdev:
6478 if (single_major)
6479 unregister_blkdev(rbd_major, RBD_DRV_NAME);
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006480err_out_wq:
6481 destroy_workqueue(rbd_wq);
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006482err_out_slab:
6483 rbd_slab_exit();
Alex Elder1c2a9df2013-05-01 12:43:03 -05006484 return rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006485}
6486
Alex Eldercc344fa2013-02-19 12:25:56 -06006487static void __exit rbd_exit(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006488{
Ilya Dryomovffe312c2014-05-20 15:46:04 +04006489 ida_destroy(&rbd_dev_id_ida);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006490 rbd_sysfs_cleanup();
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006491 if (single_major)
6492 unregister_blkdev(rbd_major, RBD_DRV_NAME);
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006493 destroy_workqueue(rbd_wq);
Alex Elder1c2a9df2013-05-01 12:43:03 -05006494 rbd_slab_exit();
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006495}
6496
6497module_init(rbd_init);
6498module_exit(rbd_exit);
6499
Alex Elderd552c612013-05-31 20:13:09 -05006500MODULE_AUTHOR("Alex Elder <elder@inktank.com>");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006501MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
6502MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006503/* following authorship retained from original osdblk.c */
6504MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
6505
Ilya Dryomov90da2582013-12-13 15:28:56 +02006506MODULE_DESCRIPTION("RADOS Block Device (RBD) driver");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006507MODULE_LICENSE("GPL");