block: Improvements to bounce-buffer handling
[linux-2.6-block.git] / drivers / block / rbd.c
CommitLineData
e2a58ee5 1
602adf40
YS
2/*
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
dfc5606d 25 For usage instructions, please refer to:
602adf40 26
dfc5606d 27 Documentation/ABI/testing/sysfs-bus-rbd
602adf40
YS
28
29 */
30
31#include <linux/ceph/libceph.h>
32#include <linux/ceph/osd_client.h>
33#include <linux/ceph/mon_client.h>
ed95b21a 34#include <linux/ceph/cls_lock_client.h>
602adf40 35#include <linux/ceph/decode.h>
59c2be1e 36#include <linux/parser.h>
30d1cff8 37#include <linux/bsearch.h>
602adf40
YS
38
39#include <linux/kernel.h>
40#include <linux/device.h>
41#include <linux/module.h>
7ad18afa 42#include <linux/blk-mq.h>
602adf40
YS
43#include <linux/fs.h>
44#include <linux/blkdev.h>
1c2a9dfe 45#include <linux/slab.h>
f8a22fc2 46#include <linux/idr.h>
bc1ecc65 47#include <linux/workqueue.h>
602adf40
YS
48
49#include "rbd_types.h"
50
aafb230e
AE
51#define RBD_DEBUG /* Activate rbd_assert() calls */
52
593a9e7b
AE
53/*
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
a2acd00e
AE
62/*
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
f0f8cef5 95#define RBD_DRV_NAME "rbd"
602adf40 96
7e513d43
ID
97#define RBD_MINORS_PER_MAJOR 256
98#define RBD_SINGLE_MAJOR_PART_SHIFT 4
602adf40 99
6d69bb53
ID
100#define RBD_MAX_PARENT_CHAIN_LEN 16
101
d4b125e9
AE
102#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
35d489f9 106#define RBD_MAX_SNAP_COUNT 510 /* allows max snapc to fit in 4KB */
602adf40
YS
107
108#define RBD_SNAP_HEAD_NAME "-"
109
9682fc6d
AE
110#define BAD_SNAP_INDEX U32_MAX /* invalid index into snap array */
111
9e15b77d
AE
112/* 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)
1e130199 114#define RBD_IMAGE_ID_LEN_MAX 64
9e15b77d 115
1e130199 116#define RBD_OBJ_PREFIX_LEN_MAX 64
589d30e0 117
ed95b21a 118#define RBD_NOTIFY_TIMEOUT 5 /* seconds */
99d16943
ID
119#define RBD_RETRY_DELAY msecs_to_jiffies(1000)
120
d889140c
AE
121/* Feature bits */
122
8767b293
ID
123#define RBD_FEATURE_LAYERING (1ULL<<0)
124#define RBD_FEATURE_STRIPINGV2 (1ULL<<1)
125#define RBD_FEATURE_EXCLUSIVE_LOCK (1ULL<<2)
126#define RBD_FEATURE_DATA_POOL (1ULL<<7)
127
ed95b21a
ID
128#define RBD_FEATURES_ALL (RBD_FEATURE_LAYERING | \
129 RBD_FEATURE_STRIPINGV2 | \
7e97332e
ID
130 RBD_FEATURE_EXCLUSIVE_LOCK | \
131 RBD_FEATURE_DATA_POOL)
d889140c
AE
132
133/* Features supported by this (client software) implementation. */
134
770eba6e 135#define RBD_FEATURES_SUPPORTED (RBD_FEATURES_ALL)
d889140c 136
81a89793
AE
137/*
138 * An RBD device name will be "rbd#", where the "rbd" comes from
139 * RBD_DRV_NAME above, and # is a unique integer identifier.
81a89793 140 */
602adf40
YS
141#define DEV_NAME_LEN 32
142
143/*
144 * block device image metadata (in-memory version)
145 */
146struct rbd_image_header {
f35a4dee 147 /* These six fields never change for a given rbd image */
849b4260 148 char *object_prefix;
602adf40 149 __u8 obj_order;
f35a4dee
AE
150 u64 stripe_unit;
151 u64 stripe_count;
7e97332e 152 s64 data_pool_id;
f35a4dee 153 u64 features; /* Might be changeable someday? */
602adf40 154
f84344f3
AE
155 /* The remaining fields need to be updated occasionally */
156 u64 image_size;
157 struct ceph_snap_context *snapc;
f35a4dee
AE
158 char *snap_names; /* format 1 only */
159 u64 *snap_sizes; /* format 1 only */
59c2be1e
YS
160};
161
0d7dbfce
AE
162/*
163 * An rbd image specification.
164 *
165 * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely
c66c6e0c
AE
166 * identify an image. Each rbd_dev structure includes a pointer to
167 * an rbd_spec structure that encapsulates this identity.
168 *
169 * Each of the id's in an rbd_spec has an associated name. For a
170 * user-mapped image, the names are supplied and the id's associated
171 * with them are looked up. For a layered image, a parent image is
172 * defined by the tuple, and the names are looked up.
173 *
174 * An rbd_dev structure contains a parent_spec pointer which is
175 * non-null if the image it represents is a child in a layered
176 * image. This pointer will refer to the rbd_spec structure used
177 * by the parent rbd_dev for its own identity (i.e., the structure
178 * is shared between the parent and child).
179 *
180 * Since these structures are populated once, during the discovery
181 * phase of image construction, they are effectively immutable so
182 * we make no effort to synchronize access to them.
183 *
184 * Note that code herein does not assume the image name is known (it
185 * could be a null pointer).
0d7dbfce
AE
186 */
187struct rbd_spec {
188 u64 pool_id;
ecb4dc22 189 const char *pool_name;
0d7dbfce 190
ecb4dc22
AE
191 const char *image_id;
192 const char *image_name;
0d7dbfce
AE
193
194 u64 snap_id;
ecb4dc22 195 const char *snap_name;
0d7dbfce
AE
196
197 struct kref kref;
198};
199
602adf40 200/*
f0f8cef5 201 * an instance of the client. multiple devices may share an rbd client.
602adf40
YS
202 */
203struct rbd_client {
204 struct ceph_client *client;
205 struct kref kref;
206 struct list_head node;
207};
208
bf0d5f50
AE
209struct rbd_img_request;
210typedef void (*rbd_img_callback_t)(struct rbd_img_request *);
211
212#define BAD_WHICH U32_MAX /* Good which or bad which, which? */
213
214struct rbd_obj_request;
215typedef void (*rbd_obj_callback_t)(struct rbd_obj_request *);
216
9969ebc5
AE
217enum obj_request_type {
218 OBJ_REQUEST_NODATA, OBJ_REQUEST_BIO, OBJ_REQUEST_PAGES
219};
bf0d5f50 220
6d2940c8
GZ
221enum obj_operation_type {
222 OBJ_OP_WRITE,
223 OBJ_OP_READ,
90e98c52 224 OBJ_OP_DISCARD,
6d2940c8
GZ
225};
226
926f9b3f
AE
227enum obj_req_flags {
228 OBJ_REQ_DONE, /* completion flag: not done = 0, done = 1 */
6365d33a 229 OBJ_REQ_IMG_DATA, /* object usage: standalone = 0, image = 1 */
5679c59f
AE
230 OBJ_REQ_KNOWN, /* EXISTS flag valid: no = 0, yes = 1 */
231 OBJ_REQ_EXISTS, /* target exists: no = 0, yes = 1 */
926f9b3f
AE
232};
233
bf0d5f50 234struct rbd_obj_request {
a90bb0c1 235 u64 object_no;
bf0d5f50
AE
236 u64 offset; /* object start byte */
237 u64 length; /* bytes from offset */
926f9b3f 238 unsigned long flags;
bf0d5f50 239
c5b5ef6c
AE
240 /*
241 * An object request associated with an image will have its
242 * img_data flag set; a standalone object request will not.
243 *
244 * A standalone object request will have which == BAD_WHICH
245 * and a null obj_request pointer.
246 *
247 * An object request initiated in support of a layered image
248 * object (to check for its existence before a write) will
249 * have which == BAD_WHICH and a non-null obj_request pointer.
250 *
251 * Finally, an object request for rbd image data will have
252 * which != BAD_WHICH, and will have a non-null img_request
253 * pointer. The value of which will be in the range
254 * 0..(img_request->obj_request_count-1).
255 */
256 union {
257 struct rbd_obj_request *obj_request; /* STAT op */
258 struct {
259 struct rbd_img_request *img_request;
260 u64 img_offset;
261 /* links for img_request->obj_requests list */
262 struct list_head links;
263 };
264 };
bf0d5f50
AE
265 u32 which; /* posn image request list */
266
267 enum obj_request_type type;
788e2df3
AE
268 union {
269 struct bio *bio_list;
270 struct {
271 struct page **pages;
272 u32 page_count;
273 };
274 };
0eefd470 275 struct page **copyup_pages;
ebda6408 276 u32 copyup_page_count;
bf0d5f50
AE
277
278 struct ceph_osd_request *osd_req;
279
280 u64 xferred; /* bytes transferred */
1b83bef2 281 int result;
bf0d5f50
AE
282
283 rbd_obj_callback_t callback;
788e2df3 284 struct completion completion;
bf0d5f50
AE
285
286 struct kref kref;
287};
288
0c425248 289enum img_req_flags {
9849e986
AE
290 IMG_REQ_WRITE, /* I/O direction: read = 0, write = 1 */
291 IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */
d0b2e944 292 IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */
90e98c52 293 IMG_REQ_DISCARD, /* discard: normal = 0, discard request = 1 */
0c425248
AE
294};
295
bf0d5f50 296struct rbd_img_request {
bf0d5f50
AE
297 struct rbd_device *rbd_dev;
298 u64 offset; /* starting image byte offset */
299 u64 length; /* byte count from offset */
0c425248 300 unsigned long flags;
bf0d5f50 301 union {
9849e986 302 u64 snap_id; /* for reads */
bf0d5f50 303 struct ceph_snap_context *snapc; /* for writes */
9849e986
AE
304 };
305 union {
306 struct request *rq; /* block request */
307 struct rbd_obj_request *obj_request; /* obj req initiator */
bf0d5f50 308 };
3d7efd18 309 struct page **copyup_pages;
ebda6408 310 u32 copyup_page_count;
bf0d5f50
AE
311 spinlock_t completion_lock;/* protects next_completion */
312 u32 next_completion;
313 rbd_img_callback_t callback;
55f27e09 314 u64 xferred;/* aggregate bytes transferred */
a5a337d4 315 int result; /* first nonzero obj_request result */
bf0d5f50
AE
316
317 u32 obj_request_count;
318 struct list_head obj_requests; /* rbd_obj_request structs */
319
320 struct kref kref;
321};
322
323#define for_each_obj_request(ireq, oreq) \
ef06f4d3 324 list_for_each_entry(oreq, &(ireq)->obj_requests, links)
bf0d5f50 325#define for_each_obj_request_from(ireq, oreq) \
ef06f4d3 326 list_for_each_entry_from(oreq, &(ireq)->obj_requests, links)
bf0d5f50 327#define for_each_obj_request_safe(ireq, oreq, n) \
ef06f4d3 328 list_for_each_entry_safe_reverse(oreq, n, &(ireq)->obj_requests, links)
bf0d5f50 329
99d16943
ID
330enum rbd_watch_state {
331 RBD_WATCH_STATE_UNREGISTERED,
332 RBD_WATCH_STATE_REGISTERED,
333 RBD_WATCH_STATE_ERROR,
334};
335
ed95b21a
ID
336enum rbd_lock_state {
337 RBD_LOCK_STATE_UNLOCKED,
338 RBD_LOCK_STATE_LOCKED,
339 RBD_LOCK_STATE_RELEASING,
340};
341
342/* WatchNotify::ClientId */
343struct rbd_client_id {
344 u64 gid;
345 u64 handle;
346};
347
f84344f3 348struct rbd_mapping {
99c1f08f 349 u64 size;
34b13184 350 u64 features;
f84344f3
AE
351 bool read_only;
352};
353
602adf40
YS
354/*
355 * a single device
356 */
357struct rbd_device {
de71a297 358 int dev_id; /* blkdev unique id */
602adf40
YS
359
360 int major; /* blkdev assigned major */
dd82fff1 361 int minor;
602adf40 362 struct gendisk *disk; /* blkdev's gendisk and rq */
602adf40 363
a30b71b9 364 u32 image_format; /* Either 1 or 2 */
602adf40
YS
365 struct rbd_client *rbd_client;
366
367 char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */
368
b82d167b 369 spinlock_t lock; /* queue, flags, open_count */
602adf40
YS
370
371 struct rbd_image_header header;
b82d167b 372 unsigned long flags; /* possibly lock protected */
0d7dbfce 373 struct rbd_spec *spec;
d147543d 374 struct rbd_options *opts;
0d6d1e9c 375 char *config_info; /* add{,_single_major} string */
602adf40 376
c41d13a3 377 struct ceph_object_id header_oid;
922dab61 378 struct ceph_object_locator header_oloc;
971f839a 379
1643dfa4 380 struct ceph_file_layout layout; /* used for all rbd requests */
0903e875 381
99d16943
ID
382 struct mutex watch_mutex;
383 enum rbd_watch_state watch_state;
922dab61 384 struct ceph_osd_linger_request *watch_handle;
99d16943
ID
385 u64 watch_cookie;
386 struct delayed_work watch_dwork;
59c2be1e 387
ed95b21a
ID
388 struct rw_semaphore lock_rwsem;
389 enum rbd_lock_state lock_state;
cbbfb0ff 390 char lock_cookie[32];
ed95b21a
ID
391 struct rbd_client_id owner_cid;
392 struct work_struct acquired_lock_work;
393 struct work_struct released_lock_work;
394 struct delayed_work lock_dwork;
395 struct work_struct unlock_work;
396 wait_queue_head_t lock_waitq;
397
1643dfa4 398 struct workqueue_struct *task_wq;
59c2be1e 399
86b00e0d
AE
400 struct rbd_spec *parent_spec;
401 u64 parent_overlap;
a2acd00e 402 atomic_t parent_ref;
2f82ee54 403 struct rbd_device *parent;
86b00e0d 404
7ad18afa
CH
405 /* Block layer tags. */
406 struct blk_mq_tag_set tag_set;
407
c666601a
JD
408 /* protects updating the header */
409 struct rw_semaphore header_rwsem;
f84344f3
AE
410
411 struct rbd_mapping mapping;
602adf40
YS
412
413 struct list_head node;
dfc5606d 414
dfc5606d
YS
415 /* sysfs related */
416 struct device dev;
b82d167b 417 unsigned long open_count; /* protected by lock */
dfc5606d
YS
418};
419
b82d167b 420/*
87c0fded
ID
421 * Flag bits for rbd_dev->flags:
422 * - REMOVING (which is coupled with rbd_dev->open_count) is protected
423 * by rbd_dev->lock
424 * - BLACKLISTED is protected by rbd_dev->lock_rwsem
b82d167b 425 */
6d292906
AE
426enum rbd_dev_flags {
427 RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */
b82d167b 428 RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */
87c0fded 429 RBD_DEV_FLAG_BLACKLISTED, /* our ceph_client is blacklisted */
6d292906
AE
430};
431
cfbf6377 432static DEFINE_MUTEX(client_mutex); /* Serialize client creation */
e124a82f 433
602adf40 434static LIST_HEAD(rbd_dev_list); /* devices */
e124a82f
AE
435static DEFINE_SPINLOCK(rbd_dev_list_lock);
436
432b8587
AE
437static LIST_HEAD(rbd_client_list); /* clients */
438static DEFINE_SPINLOCK(rbd_client_list_lock);
602adf40 439
78c2a44a
AE
440/* Slab caches for frequently-allocated structures */
441
1c2a9dfe 442static struct kmem_cache *rbd_img_request_cache;
868311b1 443static struct kmem_cache *rbd_obj_request_cache;
1c2a9dfe 444
9b60e70b 445static int rbd_major;
f8a22fc2
ID
446static DEFINE_IDA(rbd_dev_id_ida);
447
f5ee37bd
ID
448static struct workqueue_struct *rbd_wq;
449
9b60e70b
ID
450/*
451 * Default to false for now, as single-major requires >= 0.75 version of
452 * userspace rbd utility.
453 */
454static bool single_major = false;
455module_param(single_major, bool, S_IRUGO);
456MODULE_PARM_DESC(single_major, "Use a single major number for all rbd devices (default: false)");
457
3d7efd18
AE
458static int rbd_img_request_submit(struct rbd_img_request *img_request);
459
f0f8cef5
AE
460static ssize_t rbd_add(struct bus_type *bus, const char *buf,
461 size_t count);
462static ssize_t rbd_remove(struct bus_type *bus, const char *buf,
463 size_t count);
9b60e70b
ID
464static ssize_t rbd_add_single_major(struct bus_type *bus, const char *buf,
465 size_t count);
466static ssize_t rbd_remove_single_major(struct bus_type *bus, const char *buf,
467 size_t count);
6d69bb53 468static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth);
a2acd00e 469static void rbd_spec_put(struct rbd_spec *spec);
f0f8cef5 470
9b60e70b
ID
471static int rbd_dev_id_to_minor(int dev_id)
472{
7e513d43 473 return dev_id << RBD_SINGLE_MAJOR_PART_SHIFT;
9b60e70b
ID
474}
475
476static int minor_to_rbd_dev_id(int minor)
477{
7e513d43 478 return minor >> RBD_SINGLE_MAJOR_PART_SHIFT;
9b60e70b
ID
479}
480
ed95b21a
ID
481static bool __rbd_is_lock_owner(struct rbd_device *rbd_dev)
482{
483 return rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED ||
484 rbd_dev->lock_state == RBD_LOCK_STATE_RELEASING;
485}
486
487static bool rbd_is_lock_owner(struct rbd_device *rbd_dev)
488{
489 bool is_lock_owner;
490
491 down_read(&rbd_dev->lock_rwsem);
492 is_lock_owner = __rbd_is_lock_owner(rbd_dev);
493 up_read(&rbd_dev->lock_rwsem);
494 return is_lock_owner;
495}
496
8767b293
ID
497static ssize_t rbd_supported_features_show(struct bus_type *bus, char *buf)
498{
499 return sprintf(buf, "0x%llx\n", RBD_FEATURES_SUPPORTED);
500}
501
b15a21dd
GKH
502static BUS_ATTR(add, S_IWUSR, NULL, rbd_add);
503static BUS_ATTR(remove, S_IWUSR, NULL, rbd_remove);
9b60e70b
ID
504static BUS_ATTR(add_single_major, S_IWUSR, NULL, rbd_add_single_major);
505static BUS_ATTR(remove_single_major, S_IWUSR, NULL, rbd_remove_single_major);
8767b293 506static BUS_ATTR(supported_features, S_IRUGO, rbd_supported_features_show, NULL);
b15a21dd
GKH
507
508static struct attribute *rbd_bus_attrs[] = {
509 &bus_attr_add.attr,
510 &bus_attr_remove.attr,
9b60e70b
ID
511 &bus_attr_add_single_major.attr,
512 &bus_attr_remove_single_major.attr,
8767b293 513 &bus_attr_supported_features.attr,
b15a21dd 514 NULL,
f0f8cef5 515};
92c76dc0
ID
516
517static umode_t rbd_bus_is_visible(struct kobject *kobj,
518 struct attribute *attr, int index)
519{
9b60e70b
ID
520 if (!single_major &&
521 (attr == &bus_attr_add_single_major.attr ||
522 attr == &bus_attr_remove_single_major.attr))
523 return 0;
524
92c76dc0
ID
525 return attr->mode;
526}
527
528static const struct attribute_group rbd_bus_group = {
529 .attrs = rbd_bus_attrs,
530 .is_visible = rbd_bus_is_visible,
531};
532__ATTRIBUTE_GROUPS(rbd_bus);
f0f8cef5
AE
533
534static struct bus_type rbd_bus_type = {
535 .name = "rbd",
b15a21dd 536 .bus_groups = rbd_bus_groups,
f0f8cef5
AE
537};
538
539static void rbd_root_dev_release(struct device *dev)
540{
541}
542
543static struct device rbd_root_dev = {
544 .init_name = "rbd",
545 .release = rbd_root_dev_release,
546};
547
06ecc6cb
AE
548static __printf(2, 3)
549void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...)
550{
551 struct va_format vaf;
552 va_list args;
553
554 va_start(args, fmt);
555 vaf.fmt = fmt;
556 vaf.va = &args;
557
558 if (!rbd_dev)
559 printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf);
560 else if (rbd_dev->disk)
561 printk(KERN_WARNING "%s: %s: %pV\n",
562 RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf);
563 else if (rbd_dev->spec && rbd_dev->spec->image_name)
564 printk(KERN_WARNING "%s: image %s: %pV\n",
565 RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf);
566 else if (rbd_dev->spec && rbd_dev->spec->image_id)
567 printk(KERN_WARNING "%s: id %s: %pV\n",
568 RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf);
569 else /* punt */
570 printk(KERN_WARNING "%s: rbd_dev %p: %pV\n",
571 RBD_DRV_NAME, rbd_dev, &vaf);
572 va_end(args);
573}
574
aafb230e
AE
575#ifdef RBD_DEBUG
576#define rbd_assert(expr) \
577 if (unlikely(!(expr))) { \
578 printk(KERN_ERR "\nAssertion failure in %s() " \
579 "at line %d:\n\n" \
580 "\trbd_assert(%s);\n\n", \
581 __func__, __LINE__, #expr); \
582 BUG(); \
583 }
584#else /* !RBD_DEBUG */
585# define rbd_assert(expr) ((void) 0)
586#endif /* !RBD_DEBUG */
dfc5606d 587
2761713d 588static void rbd_osd_copyup_callback(struct rbd_obj_request *obj_request);
b454e36d 589static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request);
05a46afd
AE
590static void rbd_img_parent_read(struct rbd_obj_request *obj_request);
591static void rbd_dev_remove_parent(struct rbd_device *rbd_dev);
8b3e1a56 592
cc4a38bd 593static int rbd_dev_refresh(struct rbd_device *rbd_dev);
2df3fac7 594static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev);
a720ae09 595static int rbd_dev_header_info(struct rbd_device *rbd_dev);
e8f59b59 596static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev);
54cac61f
AE
597static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
598 u64 snap_id);
2ad3d716
AE
599static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
600 u8 *order, u64 *snap_size);
601static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
602 u64 *snap_features);
59c2be1e 603
602adf40
YS
604static int rbd_open(struct block_device *bdev, fmode_t mode)
605{
f0f8cef5 606 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
b82d167b 607 bool removing = false;
602adf40 608
f84344f3 609 if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only)
602adf40
YS
610 return -EROFS;
611
a14ea269 612 spin_lock_irq(&rbd_dev->lock);
b82d167b
AE
613 if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags))
614 removing = true;
615 else
616 rbd_dev->open_count++;
a14ea269 617 spin_unlock_irq(&rbd_dev->lock);
b82d167b
AE
618 if (removing)
619 return -ENOENT;
620
c3e946ce 621 (void) get_device(&rbd_dev->dev);
340c7a2b 622
602adf40
YS
623 return 0;
624}
625
db2a144b 626static void rbd_release(struct gendisk *disk, fmode_t mode)
dfc5606d
YS
627{
628 struct rbd_device *rbd_dev = disk->private_data;
b82d167b
AE
629 unsigned long open_count_before;
630
a14ea269 631 spin_lock_irq(&rbd_dev->lock);
b82d167b 632 open_count_before = rbd_dev->open_count--;
a14ea269 633 spin_unlock_irq(&rbd_dev->lock);
b82d167b 634 rbd_assert(open_count_before > 0);
dfc5606d 635
c3e946ce 636 put_device(&rbd_dev->dev);
dfc5606d
YS
637}
638
131fd9f6
GZ
639static int rbd_ioctl_set_ro(struct rbd_device *rbd_dev, unsigned long arg)
640{
77f33c03 641 int ret = 0;
131fd9f6
GZ
642 int val;
643 bool ro;
77f33c03 644 bool ro_changed = false;
131fd9f6 645
77f33c03 646 /* get_user() may sleep, so call it before taking rbd_dev->lock */
131fd9f6
GZ
647 if (get_user(val, (int __user *)(arg)))
648 return -EFAULT;
649
650 ro = val ? true : false;
651 /* Snapshot doesn't allow to write*/
652 if (rbd_dev->spec->snap_id != CEPH_NOSNAP && !ro)
653 return -EROFS;
654
77f33c03
JD
655 spin_lock_irq(&rbd_dev->lock);
656 /* prevent others open this device */
657 if (rbd_dev->open_count > 1) {
658 ret = -EBUSY;
659 goto out;
660 }
661
131fd9f6
GZ
662 if (rbd_dev->mapping.read_only != ro) {
663 rbd_dev->mapping.read_only = ro;
77f33c03 664 ro_changed = true;
131fd9f6
GZ
665 }
666
77f33c03
JD
667out:
668 spin_unlock_irq(&rbd_dev->lock);
669 /* set_disk_ro() may sleep, so call it after releasing rbd_dev->lock */
670 if (ret == 0 && ro_changed)
671 set_disk_ro(rbd_dev->disk, ro ? 1 : 0);
672
673 return ret;
131fd9f6
GZ
674}
675
676static int rbd_ioctl(struct block_device *bdev, fmode_t mode,
677 unsigned int cmd, unsigned long arg)
678{
679 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
680 int ret = 0;
681
131fd9f6
GZ
682 switch (cmd) {
683 case BLKROSET:
684 ret = rbd_ioctl_set_ro(rbd_dev, arg);
685 break;
686 default:
687 ret = -ENOTTY;
688 }
689
131fd9f6
GZ
690 return ret;
691}
692
693#ifdef CONFIG_COMPAT
694static int rbd_compat_ioctl(struct block_device *bdev, fmode_t mode,
695 unsigned int cmd, unsigned long arg)
696{
697 return rbd_ioctl(bdev, mode, cmd, arg);
698}
699#endif /* CONFIG_COMPAT */
700
602adf40
YS
701static const struct block_device_operations rbd_bd_ops = {
702 .owner = THIS_MODULE,
703 .open = rbd_open,
dfc5606d 704 .release = rbd_release,
131fd9f6
GZ
705 .ioctl = rbd_ioctl,
706#ifdef CONFIG_COMPAT
707 .compat_ioctl = rbd_compat_ioctl,
708#endif
602adf40
YS
709};
710
711/*
7262cfca 712 * Initialize an rbd client instance. Success or not, this function
cfbf6377 713 * consumes ceph_opts. Caller holds client_mutex.
602adf40 714 */
f8c38929 715static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts)
602adf40
YS
716{
717 struct rbd_client *rbdc;
718 int ret = -ENOMEM;
719
37206ee5 720 dout("%s:\n", __func__);
602adf40
YS
721 rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL);
722 if (!rbdc)
723 goto out_opt;
724
725 kref_init(&rbdc->kref);
726 INIT_LIST_HEAD(&rbdc->node);
727
74da4a0f 728 rbdc->client = ceph_create_client(ceph_opts, rbdc);
602adf40 729 if (IS_ERR(rbdc->client))
08f75463 730 goto out_rbdc;
43ae4701 731 ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */
602adf40
YS
732
733 ret = ceph_open_session(rbdc->client);
734 if (ret < 0)
08f75463 735 goto out_client;
602adf40 736
432b8587 737 spin_lock(&rbd_client_list_lock);
602adf40 738 list_add_tail(&rbdc->node, &rbd_client_list);
432b8587 739 spin_unlock(&rbd_client_list_lock);
602adf40 740
37206ee5 741 dout("%s: rbdc %p\n", __func__, rbdc);
bc534d86 742
602adf40 743 return rbdc;
08f75463 744out_client:
602adf40 745 ceph_destroy_client(rbdc->client);
08f75463 746out_rbdc:
602adf40
YS
747 kfree(rbdc);
748out_opt:
43ae4701
AE
749 if (ceph_opts)
750 ceph_destroy_options(ceph_opts);
37206ee5
AE
751 dout("%s: error %d\n", __func__, ret);
752
28f259b7 753 return ERR_PTR(ret);
602adf40
YS
754}
755
2f82ee54
AE
756static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc)
757{
758 kref_get(&rbdc->kref);
759
760 return rbdc;
761}
762
602adf40 763/*
1f7ba331
AE
764 * Find a ceph client with specific addr and configuration. If
765 * found, bump its reference count.
602adf40 766 */
1f7ba331 767static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts)
602adf40
YS
768{
769 struct rbd_client *client_node;
1f7ba331 770 bool found = false;
602adf40 771
43ae4701 772 if (ceph_opts->flags & CEPH_OPT_NOSHARE)
602adf40
YS
773 return NULL;
774
1f7ba331
AE
775 spin_lock(&rbd_client_list_lock);
776 list_for_each_entry(client_node, &rbd_client_list, node) {
777 if (!ceph_compare_options(ceph_opts, client_node->client)) {
2f82ee54
AE
778 __rbd_get_client(client_node);
779
1f7ba331
AE
780 found = true;
781 break;
782 }
783 }
784 spin_unlock(&rbd_client_list_lock);
785
786 return found ? client_node : NULL;
602adf40
YS
787}
788
59c2be1e 789/*
210c104c 790 * (Per device) rbd map options
59c2be1e
YS
791 */
792enum {
b5584180 793 Opt_queue_depth,
59c2be1e
YS
794 Opt_last_int,
795 /* int args above */
796 Opt_last_string,
797 /* string args above */
cc0538b6
AE
798 Opt_read_only,
799 Opt_read_write,
80de1912 800 Opt_lock_on_read,
e010dd0a 801 Opt_exclusive,
210c104c 802 Opt_err
59c2be1e
YS
803};
804
43ae4701 805static match_table_t rbd_opts_tokens = {
b5584180 806 {Opt_queue_depth, "queue_depth=%d"},
59c2be1e
YS
807 /* int args above */
808 /* string args above */
be466c1c 809 {Opt_read_only, "read_only"},
cc0538b6
AE
810 {Opt_read_only, "ro"}, /* Alternate spelling */
811 {Opt_read_write, "read_write"},
812 {Opt_read_write, "rw"}, /* Alternate spelling */
80de1912 813 {Opt_lock_on_read, "lock_on_read"},
e010dd0a 814 {Opt_exclusive, "exclusive"},
210c104c 815 {Opt_err, NULL}
59c2be1e
YS
816};
817
98571b5a 818struct rbd_options {
b5584180 819 int queue_depth;
98571b5a 820 bool read_only;
80de1912 821 bool lock_on_read;
e010dd0a 822 bool exclusive;
98571b5a
AE
823};
824
b5584180 825#define RBD_QUEUE_DEPTH_DEFAULT BLKDEV_MAX_RQ
98571b5a 826#define RBD_READ_ONLY_DEFAULT false
80de1912 827#define RBD_LOCK_ON_READ_DEFAULT false
e010dd0a 828#define RBD_EXCLUSIVE_DEFAULT false
98571b5a 829
59c2be1e
YS
830static int parse_rbd_opts_token(char *c, void *private)
831{
43ae4701 832 struct rbd_options *rbd_opts = private;
59c2be1e
YS
833 substring_t argstr[MAX_OPT_ARGS];
834 int token, intval, ret;
835
43ae4701 836 token = match_token(c, rbd_opts_tokens, argstr);
59c2be1e
YS
837 if (token < Opt_last_int) {
838 ret = match_int(&argstr[0], &intval);
839 if (ret < 0) {
210c104c 840 pr_err("bad mount option arg (not int) at '%s'\n", c);
59c2be1e
YS
841 return ret;
842 }
843 dout("got int token %d val %d\n", token, intval);
844 } else if (token > Opt_last_int && token < Opt_last_string) {
210c104c 845 dout("got string token %d val %s\n", token, argstr[0].from);
59c2be1e
YS
846 } else {
847 dout("got token %d\n", token);
848 }
849
850 switch (token) {
b5584180
ID
851 case Opt_queue_depth:
852 if (intval < 1) {
853 pr_err("queue_depth out of range\n");
854 return -EINVAL;
855 }
856 rbd_opts->queue_depth = intval;
857 break;
cc0538b6
AE
858 case Opt_read_only:
859 rbd_opts->read_only = true;
860 break;
861 case Opt_read_write:
862 rbd_opts->read_only = false;
863 break;
80de1912
ID
864 case Opt_lock_on_read:
865 rbd_opts->lock_on_read = true;
866 break;
e010dd0a
ID
867 case Opt_exclusive:
868 rbd_opts->exclusive = true;
869 break;
59c2be1e 870 default:
210c104c
ID
871 /* libceph prints "bad option" msg */
872 return -EINVAL;
59c2be1e 873 }
210c104c 874
59c2be1e
YS
875 return 0;
876}
877
6d2940c8
GZ
878static char* obj_op_name(enum obj_operation_type op_type)
879{
880 switch (op_type) {
881 case OBJ_OP_READ:
882 return "read";
883 case OBJ_OP_WRITE:
884 return "write";
90e98c52
GZ
885 case OBJ_OP_DISCARD:
886 return "discard";
6d2940c8
GZ
887 default:
888 return "???";
889 }
890}
891
602adf40
YS
892/*
893 * Get a ceph client with specific addr and configuration, if one does
7262cfca
AE
894 * not exist create it. Either way, ceph_opts is consumed by this
895 * function.
602adf40 896 */
9d3997fd 897static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts)
602adf40 898{
f8c38929 899 struct rbd_client *rbdc;
59c2be1e 900
cfbf6377 901 mutex_lock_nested(&client_mutex, SINGLE_DEPTH_NESTING);
1f7ba331 902 rbdc = rbd_client_find(ceph_opts);
9d3997fd 903 if (rbdc) /* using an existing client */
43ae4701 904 ceph_destroy_options(ceph_opts);
9d3997fd 905 else
f8c38929 906 rbdc = rbd_client_create(ceph_opts);
cfbf6377 907 mutex_unlock(&client_mutex);
602adf40 908
9d3997fd 909 return rbdc;
602adf40
YS
910}
911
912/*
913 * Destroy ceph client
d23a4b3f 914 *
432b8587 915 * Caller must hold rbd_client_list_lock.
602adf40
YS
916 */
917static void rbd_client_release(struct kref *kref)
918{
919 struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
920
37206ee5 921 dout("%s: rbdc %p\n", __func__, rbdc);
cd9d9f5d 922 spin_lock(&rbd_client_list_lock);
602adf40 923 list_del(&rbdc->node);
cd9d9f5d 924 spin_unlock(&rbd_client_list_lock);
602adf40
YS
925
926 ceph_destroy_client(rbdc->client);
927 kfree(rbdc);
928}
929
930/*
931 * Drop reference to ceph client node. If it's not referenced anymore, release
932 * it.
933 */
9d3997fd 934static void rbd_put_client(struct rbd_client *rbdc)
602adf40 935{
c53d5893
AE
936 if (rbdc)
937 kref_put(&rbdc->kref, rbd_client_release);
602adf40
YS
938}
939
a30b71b9
AE
940static bool rbd_image_format_valid(u32 image_format)
941{
942 return image_format == 1 || image_format == 2;
943}
944
8e94af8e
AE
945static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
946{
103a150f
AE
947 size_t size;
948 u32 snap_count;
949
950 /* The header has to start with the magic rbd header text */
951 if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT)))
952 return false;
953
db2388b6
AE
954 /* The bio layer requires at least sector-sized I/O */
955
956 if (ondisk->options.order < SECTOR_SHIFT)
957 return false;
958
959 /* If we use u64 in a few spots we may be able to loosen this */
960
961 if (ondisk->options.order > 8 * sizeof (int) - 1)
962 return false;
963
103a150f
AE
964 /*
965 * The size of a snapshot header has to fit in a size_t, and
966 * that limits the number of snapshots.
967 */
968 snap_count = le32_to_cpu(ondisk->snap_count);
969 size = SIZE_MAX - sizeof (struct ceph_snap_context);
970 if (snap_count > size / sizeof (__le64))
971 return false;
972
973 /*
974 * Not only that, but the size of the entire the snapshot
975 * header must also be representable in a size_t.
976 */
977 size -= snap_count * sizeof (__le64);
978 if ((u64) size < le64_to_cpu(ondisk->snap_names_len))
979 return false;
980
981 return true;
8e94af8e
AE
982}
983
5bc3fb17
ID
984/*
985 * returns the size of an object in the image
986 */
987static u32 rbd_obj_bytes(struct rbd_image_header *header)
988{
989 return 1U << header->obj_order;
990}
991
263423f8
ID
992static void rbd_init_layout(struct rbd_device *rbd_dev)
993{
994 if (rbd_dev->header.stripe_unit == 0 ||
995 rbd_dev->header.stripe_count == 0) {
996 rbd_dev->header.stripe_unit = rbd_obj_bytes(&rbd_dev->header);
997 rbd_dev->header.stripe_count = 1;
998 }
999
1000 rbd_dev->layout.stripe_unit = rbd_dev->header.stripe_unit;
1001 rbd_dev->layout.stripe_count = rbd_dev->header.stripe_count;
1002 rbd_dev->layout.object_size = rbd_obj_bytes(&rbd_dev->header);
7e97332e
ID
1003 rbd_dev->layout.pool_id = rbd_dev->header.data_pool_id == CEPH_NOPOOL ?
1004 rbd_dev->spec->pool_id : rbd_dev->header.data_pool_id;
263423f8
ID
1005 RCU_INIT_POINTER(rbd_dev->layout.pool_ns, NULL);
1006}
1007
602adf40 1008/*
bb23e37a
AE
1009 * Fill an rbd image header with information from the given format 1
1010 * on-disk header.
602adf40 1011 */
662518b1 1012static int rbd_header_from_disk(struct rbd_device *rbd_dev,
4156d998 1013 struct rbd_image_header_ondisk *ondisk)
602adf40 1014{
662518b1 1015 struct rbd_image_header *header = &rbd_dev->header;
bb23e37a
AE
1016 bool first_time = header->object_prefix == NULL;
1017 struct ceph_snap_context *snapc;
1018 char *object_prefix = NULL;
1019 char *snap_names = NULL;
1020 u64 *snap_sizes = NULL;
ccece235 1021 u32 snap_count;
bb23e37a 1022 int ret = -ENOMEM;
621901d6 1023 u32 i;
602adf40 1024
bb23e37a 1025 /* Allocate this now to avoid having to handle failure below */
6a52325f 1026
bb23e37a 1027 if (first_time) {
848d796c
ID
1028 object_prefix = kstrndup(ondisk->object_prefix,
1029 sizeof(ondisk->object_prefix),
1030 GFP_KERNEL);
bb23e37a
AE
1031 if (!object_prefix)
1032 return -ENOMEM;
bb23e37a 1033 }
00f1f36f 1034
bb23e37a 1035 /* Allocate the snapshot context and fill it in */
00f1f36f 1036
bb23e37a
AE
1037 snap_count = le32_to_cpu(ondisk->snap_count);
1038 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
1039 if (!snapc)
1040 goto out_err;
1041 snapc->seq = le64_to_cpu(ondisk->snap_seq);
602adf40 1042 if (snap_count) {
bb23e37a 1043 struct rbd_image_snap_ondisk *snaps;
f785cc1d
AE
1044 u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len);
1045
bb23e37a 1046 /* We'll keep a copy of the snapshot names... */
621901d6 1047
bb23e37a
AE
1048 if (snap_names_len > (u64)SIZE_MAX)
1049 goto out_2big;
1050 snap_names = kmalloc(snap_names_len, GFP_KERNEL);
1051 if (!snap_names)
6a52325f
AE
1052 goto out_err;
1053
bb23e37a 1054 /* ...as well as the array of their sizes. */
88a25a5f
ME
1055 snap_sizes = kmalloc_array(snap_count,
1056 sizeof(*header->snap_sizes),
1057 GFP_KERNEL);
bb23e37a 1058 if (!snap_sizes)
6a52325f 1059 goto out_err;
bb23e37a 1060
f785cc1d 1061 /*
bb23e37a
AE
1062 * Copy the names, and fill in each snapshot's id
1063 * and size.
1064 *
99a41ebc 1065 * Note that rbd_dev_v1_header_info() guarantees the
bb23e37a 1066 * ondisk buffer we're working with has
f785cc1d
AE
1067 * snap_names_len bytes beyond the end of the
1068 * snapshot id array, this memcpy() is safe.
1069 */
bb23e37a
AE
1070 memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len);
1071 snaps = ondisk->snaps;
1072 for (i = 0; i < snap_count; i++) {
1073 snapc->snaps[i] = le64_to_cpu(snaps[i].id);
1074 snap_sizes[i] = le64_to_cpu(snaps[i].image_size);
1075 }
602adf40 1076 }
6a52325f 1077
bb23e37a 1078 /* We won't fail any more, fill in the header */
621901d6 1079
bb23e37a
AE
1080 if (first_time) {
1081 header->object_prefix = object_prefix;
1082 header->obj_order = ondisk->options.order;
263423f8 1083 rbd_init_layout(rbd_dev);
602adf40 1084 } else {
662518b1
AE
1085 ceph_put_snap_context(header->snapc);
1086 kfree(header->snap_names);
1087 kfree(header->snap_sizes);
602adf40 1088 }
849b4260 1089
bb23e37a 1090 /* The remaining fields always get updated (when we refresh) */
621901d6 1091
f84344f3 1092 header->image_size = le64_to_cpu(ondisk->image_size);
bb23e37a
AE
1093 header->snapc = snapc;
1094 header->snap_names = snap_names;
1095 header->snap_sizes = snap_sizes;
468521c1 1096
602adf40 1097 return 0;
bb23e37a
AE
1098out_2big:
1099 ret = -EIO;
6a52325f 1100out_err:
bb23e37a
AE
1101 kfree(snap_sizes);
1102 kfree(snap_names);
1103 ceph_put_snap_context(snapc);
1104 kfree(object_prefix);
ccece235 1105
bb23e37a 1106 return ret;
602adf40
YS
1107}
1108
9682fc6d
AE
1109static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which)
1110{
1111 const char *snap_name;
1112
1113 rbd_assert(which < rbd_dev->header.snapc->num_snaps);
1114
1115 /* Skip over names until we find the one we are looking for */
1116
1117 snap_name = rbd_dev->header.snap_names;
1118 while (which--)
1119 snap_name += strlen(snap_name) + 1;
1120
1121 return kstrdup(snap_name, GFP_KERNEL);
1122}
1123
30d1cff8
AE
1124/*
1125 * Snapshot id comparison function for use with qsort()/bsearch().
1126 * Note that result is for snapshots in *descending* order.
1127 */
1128static int snapid_compare_reverse(const void *s1, const void *s2)
1129{
1130 u64 snap_id1 = *(u64 *)s1;
1131 u64 snap_id2 = *(u64 *)s2;
1132
1133 if (snap_id1 < snap_id2)
1134 return 1;
1135 return snap_id1 == snap_id2 ? 0 : -1;
1136}
1137
1138/*
1139 * Search a snapshot context to see if the given snapshot id is
1140 * present.
1141 *
1142 * Returns the position of the snapshot id in the array if it's found,
1143 * or BAD_SNAP_INDEX otherwise.
1144 *
1145 * Note: The snapshot array is in kept sorted (by the osd) in
1146 * reverse order, highest snapshot id first.
1147 */
9682fc6d
AE
1148static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id)
1149{
1150 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
30d1cff8 1151 u64 *found;
9682fc6d 1152
30d1cff8
AE
1153 found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps,
1154 sizeof (snap_id), snapid_compare_reverse);
9682fc6d 1155
30d1cff8 1156 return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX;
9682fc6d
AE
1157}
1158
2ad3d716
AE
1159static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev,
1160 u64 snap_id)
9e15b77d 1161{
54cac61f 1162 u32 which;
da6a6b63 1163 const char *snap_name;
9e15b77d 1164
54cac61f
AE
1165 which = rbd_dev_snap_index(rbd_dev, snap_id);
1166 if (which == BAD_SNAP_INDEX)
da6a6b63 1167 return ERR_PTR(-ENOENT);
54cac61f 1168
da6a6b63
JD
1169 snap_name = _rbd_dev_v1_snap_name(rbd_dev, which);
1170 return snap_name ? snap_name : ERR_PTR(-ENOMEM);
54cac61f
AE
1171}
1172
1173static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id)
1174{
9e15b77d
AE
1175 if (snap_id == CEPH_NOSNAP)
1176 return RBD_SNAP_HEAD_NAME;
1177
54cac61f
AE
1178 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1179 if (rbd_dev->image_format == 1)
1180 return rbd_dev_v1_snap_name(rbd_dev, snap_id);
9e15b77d 1181
54cac61f 1182 return rbd_dev_v2_snap_name(rbd_dev, snap_id);
9e15b77d
AE
1183}
1184
2ad3d716
AE
1185static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
1186 u64 *snap_size)
602adf40 1187{
2ad3d716
AE
1188 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1189 if (snap_id == CEPH_NOSNAP) {
1190 *snap_size = rbd_dev->header.image_size;
1191 } else if (rbd_dev->image_format == 1) {
1192 u32 which;
602adf40 1193
2ad3d716
AE
1194 which = rbd_dev_snap_index(rbd_dev, snap_id);
1195 if (which == BAD_SNAP_INDEX)
1196 return -ENOENT;
e86924a8 1197
2ad3d716
AE
1198 *snap_size = rbd_dev->header.snap_sizes[which];
1199 } else {
1200 u64 size = 0;
1201 int ret;
1202
1203 ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size);
1204 if (ret)
1205 return ret;
1206
1207 *snap_size = size;
1208 }
1209 return 0;
602adf40
YS
1210}
1211
2ad3d716
AE
1212static int rbd_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
1213 u64 *snap_features)
602adf40 1214{
2ad3d716
AE
1215 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1216 if (snap_id == CEPH_NOSNAP) {
1217 *snap_features = rbd_dev->header.features;
1218 } else if (rbd_dev->image_format == 1) {
1219 *snap_features = 0; /* No features for format 1 */
602adf40 1220 } else {
2ad3d716
AE
1221 u64 features = 0;
1222 int ret;
8b0241f8 1223
2ad3d716
AE
1224 ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, &features);
1225 if (ret)
1226 return ret;
1227
1228 *snap_features = features;
1229 }
1230 return 0;
1231}
1232
1233static int rbd_dev_mapping_set(struct rbd_device *rbd_dev)
1234{
8f4b7d98 1235 u64 snap_id = rbd_dev->spec->snap_id;
2ad3d716
AE
1236 u64 size = 0;
1237 u64 features = 0;
1238 int ret;
1239
2ad3d716
AE
1240 ret = rbd_snap_size(rbd_dev, snap_id, &size);
1241 if (ret)
1242 return ret;
1243 ret = rbd_snap_features(rbd_dev, snap_id, &features);
1244 if (ret)
1245 return ret;
1246
1247 rbd_dev->mapping.size = size;
1248 rbd_dev->mapping.features = features;
1249
8b0241f8 1250 return 0;
602adf40
YS
1251}
1252
d1cf5788
AE
1253static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev)
1254{
1255 rbd_dev->mapping.size = 0;
1256 rbd_dev->mapping.features = 0;
200a6a8b
AE
1257}
1258
65ccfe21
AE
1259static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset)
1260{
5bc3fb17 1261 u64 segment_size = rbd_obj_bytes(&rbd_dev->header);
602adf40 1262
65ccfe21
AE
1263 return offset & (segment_size - 1);
1264}
1265
1266static u64 rbd_segment_length(struct rbd_device *rbd_dev,
1267 u64 offset, u64 length)
1268{
5bc3fb17 1269 u64 segment_size = rbd_obj_bytes(&rbd_dev->header);
65ccfe21
AE
1270
1271 offset &= segment_size - 1;
1272
aafb230e 1273 rbd_assert(length <= U64_MAX - offset);
65ccfe21
AE
1274 if (offset + length > segment_size)
1275 length = segment_size - offset;
1276
1277 return length;
602adf40
YS
1278}
1279
1280/*
1281 * bio helpers
1282 */
1283
1284static void bio_chain_put(struct bio *chain)
1285{
1286 struct bio *tmp;
1287
1288 while (chain) {
1289 tmp = chain;
1290 chain = chain->bi_next;
1291 bio_put(tmp);
1292 }
1293}
1294
1295/*
1296 * zeros a bio chain, starting at specific offset
1297 */
1298static void zero_bio_chain(struct bio *chain, int start_ofs)
1299{
7988613b
KO
1300 struct bio_vec bv;
1301 struct bvec_iter iter;
602adf40
YS
1302 unsigned long flags;
1303 void *buf;
602adf40
YS
1304 int pos = 0;
1305
1306 while (chain) {
7988613b
KO
1307 bio_for_each_segment(bv, chain, iter) {
1308 if (pos + bv.bv_len > start_ofs) {
602adf40 1309 int remainder = max(start_ofs - pos, 0);
7988613b 1310 buf = bvec_kmap_irq(&bv, &flags);
602adf40 1311 memset(buf + remainder, 0,
7988613b
KO
1312 bv.bv_len - remainder);
1313 flush_dcache_page(bv.bv_page);
85b5aaa6 1314 bvec_kunmap_irq(buf, &flags);
602adf40 1315 }
7988613b 1316 pos += bv.bv_len;
602adf40
YS
1317 }
1318
1319 chain = chain->bi_next;
1320 }
1321}
1322
b9434c5b
AE
1323/*
1324 * similar to zero_bio_chain(), zeros data defined by a page array,
1325 * starting at the given byte offset from the start of the array and
1326 * continuing up to the given end offset. The pages array is
1327 * assumed to be big enough to hold all bytes up to the end.
1328 */
1329static void zero_pages(struct page **pages, u64 offset, u64 end)
1330{
1331 struct page **page = &pages[offset >> PAGE_SHIFT];
1332
1333 rbd_assert(end > offset);
1334 rbd_assert(end - offset <= (u64)SIZE_MAX);
1335 while (offset < end) {
1336 size_t page_offset;
1337 size_t length;
1338 unsigned long flags;
1339 void *kaddr;
1340
491205a8
GU
1341 page_offset = offset & ~PAGE_MASK;
1342 length = min_t(size_t, PAGE_SIZE - page_offset, end - offset);
b9434c5b
AE
1343 local_irq_save(flags);
1344 kaddr = kmap_atomic(*page);
1345 memset(kaddr + page_offset, 0, length);
e2156054 1346 flush_dcache_page(*page);
b9434c5b
AE
1347 kunmap_atomic(kaddr);
1348 local_irq_restore(flags);
1349
1350 offset += length;
1351 page++;
1352 }
1353}
1354
602adf40 1355/*
f7760dad
AE
1356 * Clone a portion of a bio, starting at the given byte offset
1357 * and continuing for the number of bytes indicated.
602adf40 1358 */
f7760dad
AE
1359static struct bio *bio_clone_range(struct bio *bio_src,
1360 unsigned int offset,
1361 unsigned int len,
1362 gfp_t gfpmask)
602adf40 1363{
f7760dad
AE
1364 struct bio *bio;
1365
5341a627 1366 bio = bio_clone(bio_src, gfpmask);
f7760dad
AE
1367 if (!bio)
1368 return NULL; /* ENOMEM */
602adf40 1369
5341a627 1370 bio_advance(bio, offset);
4f024f37 1371 bio->bi_iter.bi_size = len;
f7760dad
AE
1372
1373 return bio;
1374}
1375
1376/*
1377 * Clone a portion of a bio chain, starting at the given byte offset
1378 * into the first bio in the source chain and continuing for the
1379 * number of bytes indicated. The result is another bio chain of
1380 * exactly the given length, or a null pointer on error.
1381 *
1382 * The bio_src and offset parameters are both in-out. On entry they
1383 * refer to the first source bio and the offset into that bio where
1384 * the start of data to be cloned is located.
1385 *
1386 * On return, bio_src is updated to refer to the bio in the source
1387 * chain that contains first un-cloned byte, and *offset will
1388 * contain the offset of that byte within that bio.
1389 */
1390static struct bio *bio_chain_clone_range(struct bio **bio_src,
1391 unsigned int *offset,
1392 unsigned int len,
1393 gfp_t gfpmask)
1394{
1395 struct bio *bi = *bio_src;
1396 unsigned int off = *offset;
1397 struct bio *chain = NULL;
1398 struct bio **end;
1399
1400 /* Build up a chain of clone bios up to the limit */
1401
4f024f37 1402 if (!bi || off >= bi->bi_iter.bi_size || !len)
f7760dad 1403 return NULL; /* Nothing to clone */
602adf40 1404
f7760dad
AE
1405 end = &chain;
1406 while (len) {
1407 unsigned int bi_size;
1408 struct bio *bio;
1409
f5400b7a
AE
1410 if (!bi) {
1411 rbd_warn(NULL, "bio_chain exhausted with %u left", len);
f7760dad 1412 goto out_err; /* EINVAL; ran out of bio's */
f5400b7a 1413 }
4f024f37 1414 bi_size = min_t(unsigned int, bi->bi_iter.bi_size - off, len);
f7760dad
AE
1415 bio = bio_clone_range(bi, off, bi_size, gfpmask);
1416 if (!bio)
1417 goto out_err; /* ENOMEM */
1418
1419 *end = bio;
1420 end = &bio->bi_next;
602adf40 1421
f7760dad 1422 off += bi_size;
4f024f37 1423 if (off == bi->bi_iter.bi_size) {
f7760dad
AE
1424 bi = bi->bi_next;
1425 off = 0;
1426 }
1427 len -= bi_size;
1428 }
1429 *bio_src = bi;
1430 *offset = off;
1431
1432 return chain;
1433out_err:
1434 bio_chain_put(chain);
602adf40 1435
602adf40
YS
1436 return NULL;
1437}
1438
926f9b3f
AE
1439/*
1440 * The default/initial value for all object request flags is 0. For
1441 * each flag, once its value is set to 1 it is never reset to 0
1442 * again.
1443 */
57acbaa7 1444static void obj_request_img_data_set(struct rbd_obj_request *obj_request)
926f9b3f 1445{
57acbaa7 1446 if (test_and_set_bit(OBJ_REQ_IMG_DATA, &obj_request->flags)) {
926f9b3f
AE
1447 struct rbd_device *rbd_dev;
1448
57acbaa7 1449 rbd_dev = obj_request->img_request->rbd_dev;
9584d508 1450 rbd_warn(rbd_dev, "obj_request %p already marked img_data",
926f9b3f
AE
1451 obj_request);
1452 }
1453}
1454
57acbaa7 1455static bool obj_request_img_data_test(struct rbd_obj_request *obj_request)
926f9b3f
AE
1456{
1457 smp_mb();
57acbaa7 1458 return test_bit(OBJ_REQ_IMG_DATA, &obj_request->flags) != 0;
926f9b3f
AE
1459}
1460
57acbaa7 1461static void obj_request_done_set(struct rbd_obj_request *obj_request)
6365d33a 1462{
57acbaa7
AE
1463 if (test_and_set_bit(OBJ_REQ_DONE, &obj_request->flags)) {
1464 struct rbd_device *rbd_dev = NULL;
6365d33a 1465
57acbaa7
AE
1466 if (obj_request_img_data_test(obj_request))
1467 rbd_dev = obj_request->img_request->rbd_dev;
9584d508 1468 rbd_warn(rbd_dev, "obj_request %p already marked done",
6365d33a
AE
1469 obj_request);
1470 }
1471}
1472
57acbaa7 1473static bool obj_request_done_test(struct rbd_obj_request *obj_request)
6365d33a
AE
1474{
1475 smp_mb();
57acbaa7 1476 return test_bit(OBJ_REQ_DONE, &obj_request->flags) != 0;
6365d33a
AE
1477}
1478
5679c59f
AE
1479/*
1480 * This sets the KNOWN flag after (possibly) setting the EXISTS
1481 * flag. The latter is set based on the "exists" value provided.
1482 *
1483 * Note that for our purposes once an object exists it never goes
1484 * away again. It's possible that the response from two existence
1485 * checks are separated by the creation of the target object, and
1486 * the first ("doesn't exist") response arrives *after* the second
1487 * ("does exist"). In that case we ignore the second one.
1488 */
1489static void obj_request_existence_set(struct rbd_obj_request *obj_request,
1490 bool exists)
1491{
1492 if (exists)
1493 set_bit(OBJ_REQ_EXISTS, &obj_request->flags);
1494 set_bit(OBJ_REQ_KNOWN, &obj_request->flags);
1495 smp_mb();
1496}
1497
1498static bool obj_request_known_test(struct rbd_obj_request *obj_request)
1499{
1500 smp_mb();
1501 return test_bit(OBJ_REQ_KNOWN, &obj_request->flags) != 0;
1502}
1503
1504static bool obj_request_exists_test(struct rbd_obj_request *obj_request)
1505{
1506 smp_mb();
1507 return test_bit(OBJ_REQ_EXISTS, &obj_request->flags) != 0;
1508}
1509
9638556a
ID
1510static bool obj_request_overlaps_parent(struct rbd_obj_request *obj_request)
1511{
1512 struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev;
1513
1514 return obj_request->img_offset <
1515 round_up(rbd_dev->parent_overlap, rbd_obj_bytes(&rbd_dev->header));
1516}
1517
bf0d5f50
AE
1518static void rbd_obj_request_get(struct rbd_obj_request *obj_request)
1519{
37206ee5 1520 dout("%s: obj %p (was %d)\n", __func__, obj_request,
2c935bc5 1521 kref_read(&obj_request->kref));
bf0d5f50
AE
1522 kref_get(&obj_request->kref);
1523}
1524
1525static void rbd_obj_request_destroy(struct kref *kref);
1526static void rbd_obj_request_put(struct rbd_obj_request *obj_request)
1527{
1528 rbd_assert(obj_request != NULL);
37206ee5 1529 dout("%s: obj %p (was %d)\n", __func__, obj_request,
2c935bc5 1530 kref_read(&obj_request->kref));
bf0d5f50
AE
1531 kref_put(&obj_request->kref, rbd_obj_request_destroy);
1532}
1533
0f2d5be7
AE
1534static void rbd_img_request_get(struct rbd_img_request *img_request)
1535{
1536 dout("%s: img %p (was %d)\n", __func__, img_request,
2c935bc5 1537 kref_read(&img_request->kref));
0f2d5be7
AE
1538 kref_get(&img_request->kref);
1539}
1540
e93f3152
AE
1541static bool img_request_child_test(struct rbd_img_request *img_request);
1542static void rbd_parent_request_destroy(struct kref *kref);
bf0d5f50
AE
1543static void rbd_img_request_destroy(struct kref *kref);
1544static void rbd_img_request_put(struct rbd_img_request *img_request)
1545{
1546 rbd_assert(img_request != NULL);
37206ee5 1547 dout("%s: img %p (was %d)\n", __func__, img_request,
2c935bc5 1548 kref_read(&img_request->kref));
e93f3152
AE
1549 if (img_request_child_test(img_request))
1550 kref_put(&img_request->kref, rbd_parent_request_destroy);
1551 else
1552 kref_put(&img_request->kref, rbd_img_request_destroy);
bf0d5f50
AE
1553}
1554
1555static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request,
1556 struct rbd_obj_request *obj_request)
1557{
25dcf954
AE
1558 rbd_assert(obj_request->img_request == NULL);
1559
b155e86c 1560 /* Image request now owns object's original reference */
bf0d5f50 1561 obj_request->img_request = img_request;
25dcf954 1562 obj_request->which = img_request->obj_request_count;
6365d33a
AE
1563 rbd_assert(!obj_request_img_data_test(obj_request));
1564 obj_request_img_data_set(obj_request);
bf0d5f50 1565 rbd_assert(obj_request->which != BAD_WHICH);
25dcf954
AE
1566 img_request->obj_request_count++;
1567 list_add_tail(&obj_request->links, &img_request->obj_requests);
37206ee5
AE
1568 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1569 obj_request->which);
bf0d5f50
AE
1570}
1571
1572static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request,
1573 struct rbd_obj_request *obj_request)
1574{
1575 rbd_assert(obj_request->which != BAD_WHICH);
25dcf954 1576
37206ee5
AE
1577 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1578 obj_request->which);
bf0d5f50 1579 list_del(&obj_request->links);
25dcf954
AE
1580 rbd_assert(img_request->obj_request_count > 0);
1581 img_request->obj_request_count--;
1582 rbd_assert(obj_request->which == img_request->obj_request_count);
1583 obj_request->which = BAD_WHICH;
6365d33a 1584 rbd_assert(obj_request_img_data_test(obj_request));
bf0d5f50 1585 rbd_assert(obj_request->img_request == img_request);
bf0d5f50 1586 obj_request->img_request = NULL;
25dcf954 1587 obj_request->callback = NULL;
bf0d5f50
AE
1588 rbd_obj_request_put(obj_request);
1589}
1590
1591static bool obj_request_type_valid(enum obj_request_type type)
1592{
1593 switch (type) {
9969ebc5 1594 case OBJ_REQUEST_NODATA:
bf0d5f50 1595 case OBJ_REQUEST_BIO:
788e2df3 1596 case OBJ_REQUEST_PAGES:
bf0d5f50
AE
1597 return true;
1598 default:
1599 return false;
1600 }
1601}
1602
4a17dadc
ID
1603static void rbd_img_obj_callback(struct rbd_obj_request *obj_request);
1604
980917fc 1605static void rbd_obj_request_submit(struct rbd_obj_request *obj_request)
bf0d5f50 1606{
980917fc
ID
1607 struct ceph_osd_request *osd_req = obj_request->osd_req;
1608
a90bb0c1
ID
1609 dout("%s %p object_no %016llx %llu~%llu osd_req %p\n", __func__,
1610 obj_request, obj_request->object_no, obj_request->offset,
67e2b652 1611 obj_request->length, osd_req);
4a17dadc
ID
1612 if (obj_request_img_data_test(obj_request)) {
1613 WARN_ON(obj_request->callback != rbd_img_obj_callback);
1614 rbd_img_request_get(obj_request->img_request);
1615 }
980917fc 1616 ceph_osdc_start_request(osd_req->r_osdc, osd_req, false);
bf0d5f50
AE
1617}
1618
1619static void rbd_img_request_complete(struct rbd_img_request *img_request)
1620{
55f27e09 1621
37206ee5 1622 dout("%s: img %p\n", __func__, img_request);
55f27e09
AE
1623
1624 /*
1625 * If no error occurred, compute the aggregate transfer
1626 * count for the image request. We could instead use
1627 * atomic64_cmpxchg() to update it as each object request
1628 * completes; not clear which way is better off hand.
1629 */
1630 if (!img_request->result) {
1631 struct rbd_obj_request *obj_request;
1632 u64 xferred = 0;
1633
1634 for_each_obj_request(img_request, obj_request)
1635 xferred += obj_request->xferred;
1636 img_request->xferred = xferred;
1637 }
1638
bf0d5f50
AE
1639 if (img_request->callback)
1640 img_request->callback(img_request);
1641 else
1642 rbd_img_request_put(img_request);
1643}
1644
0c425248
AE
1645/*
1646 * The default/initial value for all image request flags is 0. Each
1647 * is conditionally set to 1 at image request initialization time
1648 * and currently never change thereafter.
1649 */
1650static void img_request_write_set(struct rbd_img_request *img_request)
1651{
1652 set_bit(IMG_REQ_WRITE, &img_request->flags);
1653 smp_mb();
1654}
1655
1656static bool img_request_write_test(struct rbd_img_request *img_request)
1657{
1658 smp_mb();
1659 return test_bit(IMG_REQ_WRITE, &img_request->flags) != 0;
1660}
1661
90e98c52
GZ
1662/*
1663 * Set the discard flag when the img_request is an discard request
1664 */
1665static void img_request_discard_set(struct rbd_img_request *img_request)
1666{
1667 set_bit(IMG_REQ_DISCARD, &img_request->flags);
1668 smp_mb();
1669}
1670
1671static bool img_request_discard_test(struct rbd_img_request *img_request)
1672{
1673 smp_mb();
1674 return test_bit(IMG_REQ_DISCARD, &img_request->flags) != 0;
1675}
1676
9849e986
AE
1677static void img_request_child_set(struct rbd_img_request *img_request)
1678{
1679 set_bit(IMG_REQ_CHILD, &img_request->flags);
1680 smp_mb();
1681}
1682
e93f3152
AE
1683static void img_request_child_clear(struct rbd_img_request *img_request)
1684{
1685 clear_bit(IMG_REQ_CHILD, &img_request->flags);
1686 smp_mb();
1687}
1688
9849e986
AE
1689static bool img_request_child_test(struct rbd_img_request *img_request)
1690{
1691 smp_mb();
1692 return test_bit(IMG_REQ_CHILD, &img_request->flags) != 0;
1693}
1694
d0b2e944
AE
1695static void img_request_layered_set(struct rbd_img_request *img_request)
1696{
1697 set_bit(IMG_REQ_LAYERED, &img_request->flags);
1698 smp_mb();
1699}
1700
a2acd00e
AE
1701static void img_request_layered_clear(struct rbd_img_request *img_request)
1702{
1703 clear_bit(IMG_REQ_LAYERED, &img_request->flags);
1704 smp_mb();
1705}
1706
d0b2e944
AE
1707static bool img_request_layered_test(struct rbd_img_request *img_request)
1708{
1709 smp_mb();
1710 return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0;
1711}
1712
3b434a2a
JD
1713static enum obj_operation_type
1714rbd_img_request_op_type(struct rbd_img_request *img_request)
1715{
1716 if (img_request_write_test(img_request))
1717 return OBJ_OP_WRITE;
1718 else if (img_request_discard_test(img_request))
1719 return OBJ_OP_DISCARD;
1720 else
1721 return OBJ_OP_READ;
1722}
1723
6e2a4505
AE
1724static void
1725rbd_img_obj_request_read_callback(struct rbd_obj_request *obj_request)
1726{
b9434c5b
AE
1727 u64 xferred = obj_request->xferred;
1728 u64 length = obj_request->length;
1729
6e2a4505
AE
1730 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1731 obj_request, obj_request->img_request, obj_request->result,
b9434c5b 1732 xferred, length);
6e2a4505 1733 /*
17c1cc1d
JD
1734 * ENOENT means a hole in the image. We zero-fill the entire
1735 * length of the request. A short read also implies zero-fill
1736 * to the end of the request. An error requires the whole
1737 * length of the request to be reported finished with an error
1738 * to the block layer. In each case we update the xferred
1739 * count to indicate the whole request was satisfied.
6e2a4505 1740 */
b9434c5b 1741 rbd_assert(obj_request->type != OBJ_REQUEST_NODATA);
6e2a4505 1742 if (obj_request->result == -ENOENT) {
b9434c5b
AE
1743 if (obj_request->type == OBJ_REQUEST_BIO)
1744 zero_bio_chain(obj_request->bio_list, 0);
1745 else
1746 zero_pages(obj_request->pages, 0, length);
6e2a4505 1747 obj_request->result = 0;
b9434c5b
AE
1748 } else if (xferred < length && !obj_request->result) {
1749 if (obj_request->type == OBJ_REQUEST_BIO)
1750 zero_bio_chain(obj_request->bio_list, xferred);
1751 else
1752 zero_pages(obj_request->pages, xferred, length);
6e2a4505 1753 }
17c1cc1d 1754 obj_request->xferred = length;
6e2a4505
AE
1755 obj_request_done_set(obj_request);
1756}
1757
bf0d5f50
AE
1758static void rbd_obj_request_complete(struct rbd_obj_request *obj_request)
1759{
37206ee5
AE
1760 dout("%s: obj %p cb %p\n", __func__, obj_request,
1761 obj_request->callback);
bf0d5f50
AE
1762 if (obj_request->callback)
1763 obj_request->callback(obj_request);
788e2df3
AE
1764 else
1765 complete_all(&obj_request->completion);
bf0d5f50
AE
1766}
1767
0dcc685e
ID
1768static void rbd_obj_request_error(struct rbd_obj_request *obj_request, int err)
1769{
1770 obj_request->result = err;
1771 obj_request->xferred = 0;
1772 /*
1773 * kludge - mirror rbd_obj_request_submit() to match a put in
1774 * rbd_img_obj_callback()
1775 */
1776 if (obj_request_img_data_test(obj_request)) {
1777 WARN_ON(obj_request->callback != rbd_img_obj_callback);
1778 rbd_img_request_get(obj_request->img_request);
1779 }
1780 obj_request_done_set(obj_request);
1781 rbd_obj_request_complete(obj_request);
1782}
1783
c47f9371 1784static void rbd_osd_read_callback(struct rbd_obj_request *obj_request)
bf0d5f50 1785{
57acbaa7 1786 struct rbd_img_request *img_request = NULL;
a9e8ba2c 1787 struct rbd_device *rbd_dev = NULL;
57acbaa7
AE
1788 bool layered = false;
1789
1790 if (obj_request_img_data_test(obj_request)) {
1791 img_request = obj_request->img_request;
1792 layered = img_request && img_request_layered_test(img_request);
a9e8ba2c 1793 rbd_dev = img_request->rbd_dev;
57acbaa7 1794 }
8b3e1a56
AE
1795
1796 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1797 obj_request, img_request, obj_request->result,
1798 obj_request->xferred, obj_request->length);
a9e8ba2c
AE
1799 if (layered && obj_request->result == -ENOENT &&
1800 obj_request->img_offset < rbd_dev->parent_overlap)
8b3e1a56
AE
1801 rbd_img_parent_read(obj_request);
1802 else if (img_request)
6e2a4505
AE
1803 rbd_img_obj_request_read_callback(obj_request);
1804 else
1805 obj_request_done_set(obj_request);
bf0d5f50
AE
1806}
1807
c47f9371 1808static void rbd_osd_write_callback(struct rbd_obj_request *obj_request)
bf0d5f50 1809{
1b83bef2
SW
1810 dout("%s: obj %p result %d %llu\n", __func__, obj_request,
1811 obj_request->result, obj_request->length);
1812 /*
8b3e1a56
AE
1813 * There is no such thing as a successful short write. Set
1814 * it to our originally-requested length.
1b83bef2
SW
1815 */
1816 obj_request->xferred = obj_request->length;
07741308 1817 obj_request_done_set(obj_request);
bf0d5f50
AE
1818}
1819
90e98c52
GZ
1820static void rbd_osd_discard_callback(struct rbd_obj_request *obj_request)
1821{
1822 dout("%s: obj %p result %d %llu\n", __func__, obj_request,
1823 obj_request->result, obj_request->length);
1824 /*
1825 * There is no such thing as a successful short discard. Set
1826 * it to our originally-requested length.
1827 */
1828 obj_request->xferred = obj_request->length;
d0265de7
JD
1829 /* discarding a non-existent object is not a problem */
1830 if (obj_request->result == -ENOENT)
1831 obj_request->result = 0;
90e98c52
GZ
1832 obj_request_done_set(obj_request);
1833}
1834
fbfab539
AE
1835/*
1836 * For a simple stat call there's nothing to do. We'll do more if
1837 * this is part of a write sequence for a layered image.
1838 */
c47f9371 1839static void rbd_osd_stat_callback(struct rbd_obj_request *obj_request)
fbfab539 1840{
37206ee5 1841 dout("%s: obj %p\n", __func__, obj_request);
fbfab539
AE
1842 obj_request_done_set(obj_request);
1843}
1844
2761713d
ID
1845static void rbd_osd_call_callback(struct rbd_obj_request *obj_request)
1846{
1847 dout("%s: obj %p\n", __func__, obj_request);
1848
1849 if (obj_request_img_data_test(obj_request))
1850 rbd_osd_copyup_callback(obj_request);
1851 else
1852 obj_request_done_set(obj_request);
1853}
1854
85e084fe 1855static void rbd_osd_req_callback(struct ceph_osd_request *osd_req)
bf0d5f50
AE
1856{
1857 struct rbd_obj_request *obj_request = osd_req->r_priv;
bf0d5f50
AE
1858 u16 opcode;
1859
85e084fe 1860 dout("%s: osd_req %p\n", __func__, osd_req);
bf0d5f50 1861 rbd_assert(osd_req == obj_request->osd_req);
57acbaa7
AE
1862 if (obj_request_img_data_test(obj_request)) {
1863 rbd_assert(obj_request->img_request);
1864 rbd_assert(obj_request->which != BAD_WHICH);
1865 } else {
1866 rbd_assert(obj_request->which == BAD_WHICH);
1867 }
bf0d5f50 1868
1b83bef2
SW
1869 if (osd_req->r_result < 0)
1870 obj_request->result = osd_req->r_result;
bf0d5f50 1871
c47f9371
AE
1872 /*
1873 * We support a 64-bit length, but ultimately it has to be
7ad18afa
CH
1874 * passed to the block layer, which just supports a 32-bit
1875 * length field.
c47f9371 1876 */
7665d85b 1877 obj_request->xferred = osd_req->r_ops[0].outdata_len;
8b3e1a56 1878 rbd_assert(obj_request->xferred < (u64)UINT_MAX);
0ccd5926 1879
79528734 1880 opcode = osd_req->r_ops[0].op;
bf0d5f50
AE
1881 switch (opcode) {
1882 case CEPH_OSD_OP_READ:
c47f9371 1883 rbd_osd_read_callback(obj_request);
bf0d5f50 1884 break;
0ccd5926 1885 case CEPH_OSD_OP_SETALLOCHINT:
e30b7577
ID
1886 rbd_assert(osd_req->r_ops[1].op == CEPH_OSD_OP_WRITE ||
1887 osd_req->r_ops[1].op == CEPH_OSD_OP_WRITEFULL);
0ccd5926 1888 /* fall through */
bf0d5f50 1889 case CEPH_OSD_OP_WRITE:
e30b7577 1890 case CEPH_OSD_OP_WRITEFULL:
c47f9371 1891 rbd_osd_write_callback(obj_request);
bf0d5f50 1892 break;
fbfab539 1893 case CEPH_OSD_OP_STAT:
c47f9371 1894 rbd_osd_stat_callback(obj_request);
fbfab539 1895 break;
90e98c52
GZ
1896 case CEPH_OSD_OP_DELETE:
1897 case CEPH_OSD_OP_TRUNCATE:
1898 case CEPH_OSD_OP_ZERO:
1899 rbd_osd_discard_callback(obj_request);
1900 break;
36be9a76 1901 case CEPH_OSD_OP_CALL:
2761713d
ID
1902 rbd_osd_call_callback(obj_request);
1903 break;
bf0d5f50 1904 default:
a90bb0c1
ID
1905 rbd_warn(NULL, "unexpected OSD op: object_no %016llx opcode %d",
1906 obj_request->object_no, opcode);
bf0d5f50
AE
1907 break;
1908 }
1909
07741308 1910 if (obj_request_done_test(obj_request))
bf0d5f50
AE
1911 rbd_obj_request_complete(obj_request);
1912}
1913
9d4df01f 1914static void rbd_osd_req_format_read(struct rbd_obj_request *obj_request)
430c28c3 1915{
8c042b0d 1916 struct ceph_osd_request *osd_req = obj_request->osd_req;
430c28c3 1917
7c84883a
ID
1918 rbd_assert(obj_request_img_data_test(obj_request));
1919 osd_req->r_snapid = obj_request->img_request->snap_id;
9d4df01f
AE
1920}
1921
1922static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request)
1923{
9d4df01f 1924 struct ceph_osd_request *osd_req = obj_request->osd_req;
9d4df01f 1925
1134e091 1926 ktime_get_real_ts(&osd_req->r_mtime);
bb873b53 1927 osd_req->r_data_offset = obj_request->offset;
430c28c3
AE
1928}
1929
bc81207e
ID
1930static struct ceph_osd_request *
1931__rbd_osd_req_create(struct rbd_device *rbd_dev,
1932 struct ceph_snap_context *snapc,
1933 int num_ops, unsigned int flags,
1934 struct rbd_obj_request *obj_request)
1935{
1936 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
1937 struct ceph_osd_request *req;
a90bb0c1
ID
1938 const char *name_format = rbd_dev->image_format == 1 ?
1939 RBD_V1_DATA_FORMAT : RBD_V2_DATA_FORMAT;
bc81207e
ID
1940
1941 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, false, GFP_NOIO);
1942 if (!req)
1943 return NULL;
1944
1945 req->r_flags = flags;
1946 req->r_callback = rbd_osd_req_callback;
1947 req->r_priv = obj_request;
1948
1949 req->r_base_oloc.pool = rbd_dev->layout.pool_id;
a90bb0c1
ID
1950 if (ceph_oid_aprintf(&req->r_base_oid, GFP_NOIO, name_format,
1951 rbd_dev->header.object_prefix, obj_request->object_no))
bc81207e
ID
1952 goto err_req;
1953
1954 if (ceph_osdc_alloc_messages(req, GFP_NOIO))
1955 goto err_req;
1956
1957 return req;
1958
1959err_req:
1960 ceph_osdc_put_request(req);
1961 return NULL;
1962}
1963
0ccd5926
ID
1964/*
1965 * Create an osd request. A read request has one osd op (read).
1966 * A write request has either one (watch) or two (hint+write) osd ops.
1967 * (All rbd data writes are prefixed with an allocation hint op, but
1968 * technically osd watch is a write request, hence this distinction.)
1969 */
bf0d5f50
AE
1970static struct ceph_osd_request *rbd_osd_req_create(
1971 struct rbd_device *rbd_dev,
6d2940c8 1972 enum obj_operation_type op_type,
deb236b3 1973 unsigned int num_ops,
430c28c3 1974 struct rbd_obj_request *obj_request)
bf0d5f50 1975{
bf0d5f50 1976 struct ceph_snap_context *snapc = NULL;
bf0d5f50 1977
90e98c52
GZ
1978 if (obj_request_img_data_test(obj_request) &&
1979 (op_type == OBJ_OP_DISCARD || op_type == OBJ_OP_WRITE)) {
6365d33a 1980 struct rbd_img_request *img_request = obj_request->img_request;
90e98c52
GZ
1981 if (op_type == OBJ_OP_WRITE) {
1982 rbd_assert(img_request_write_test(img_request));
1983 } else {
1984 rbd_assert(img_request_discard_test(img_request));
1985 }
6d2940c8 1986 snapc = img_request->snapc;
bf0d5f50
AE
1987 }
1988
6d2940c8 1989 rbd_assert(num_ops == 1 || ((op_type == OBJ_OP_WRITE) && num_ops == 2));
deb236b3 1990
bc81207e
ID
1991 return __rbd_osd_req_create(rbd_dev, snapc, num_ops,
1992 (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD) ?
54ea0046 1993 CEPH_OSD_FLAG_WRITE : CEPH_OSD_FLAG_READ, obj_request);
bf0d5f50
AE
1994}
1995
0eefd470 1996/*
d3246fb0
JD
1997 * Create a copyup osd request based on the information in the object
1998 * request supplied. A copyup request has two or three osd ops, a
1999 * copyup method call, potentially a hint op, and a write or truncate
2000 * or zero op.
0eefd470
AE
2001 */
2002static struct ceph_osd_request *
2003rbd_osd_req_create_copyup(struct rbd_obj_request *obj_request)
2004{
2005 struct rbd_img_request *img_request;
d3246fb0 2006 int num_osd_ops = 3;
0eefd470
AE
2007
2008 rbd_assert(obj_request_img_data_test(obj_request));
2009 img_request = obj_request->img_request;
2010 rbd_assert(img_request);
d3246fb0
JD
2011 rbd_assert(img_request_write_test(img_request) ||
2012 img_request_discard_test(img_request));
0eefd470 2013
d3246fb0
JD
2014 if (img_request_discard_test(img_request))
2015 num_osd_ops = 2;
2016
bc81207e
ID
2017 return __rbd_osd_req_create(img_request->rbd_dev,
2018 img_request->snapc, num_osd_ops,
54ea0046 2019 CEPH_OSD_FLAG_WRITE, obj_request);
0eefd470
AE
2020}
2021
bf0d5f50
AE
2022static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req)
2023{
2024 ceph_osdc_put_request(osd_req);
2025}
2026
6c696d85
ID
2027static struct rbd_obj_request *
2028rbd_obj_request_create(enum obj_request_type type)
bf0d5f50
AE
2029{
2030 struct rbd_obj_request *obj_request;
bf0d5f50
AE
2031
2032 rbd_assert(obj_request_type_valid(type));
2033
5a60e876 2034 obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_NOIO);
6c696d85 2035 if (!obj_request)
f907ad55 2036 return NULL;
f907ad55 2037
bf0d5f50
AE
2038 obj_request->which = BAD_WHICH;
2039 obj_request->type = type;
2040 INIT_LIST_HEAD(&obj_request->links);
788e2df3 2041 init_completion(&obj_request->completion);
bf0d5f50
AE
2042 kref_init(&obj_request->kref);
2043
67e2b652 2044 dout("%s %p\n", __func__, obj_request);
bf0d5f50
AE
2045 return obj_request;
2046}
2047
2048static void rbd_obj_request_destroy(struct kref *kref)
2049{
2050 struct rbd_obj_request *obj_request;
2051
2052 obj_request = container_of(kref, struct rbd_obj_request, kref);
2053
37206ee5
AE
2054 dout("%s: obj %p\n", __func__, obj_request);
2055
bf0d5f50
AE
2056 rbd_assert(obj_request->img_request == NULL);
2057 rbd_assert(obj_request->which == BAD_WHICH);
2058
2059 if (obj_request->osd_req)
2060 rbd_osd_req_destroy(obj_request->osd_req);
2061
2062 rbd_assert(obj_request_type_valid(obj_request->type));
2063 switch (obj_request->type) {
9969ebc5
AE
2064 case OBJ_REQUEST_NODATA:
2065 break; /* Nothing to do */
bf0d5f50
AE
2066 case OBJ_REQUEST_BIO:
2067 if (obj_request->bio_list)
2068 bio_chain_put(obj_request->bio_list);
2069 break;
788e2df3 2070 case OBJ_REQUEST_PAGES:
04dc923c
ID
2071 /* img_data requests don't own their page array */
2072 if (obj_request->pages &&
2073 !obj_request_img_data_test(obj_request))
788e2df3
AE
2074 ceph_release_page_vector(obj_request->pages,
2075 obj_request->page_count);
2076 break;
bf0d5f50
AE
2077 }
2078
868311b1 2079 kmem_cache_free(rbd_obj_request_cache, obj_request);
bf0d5f50
AE
2080}
2081
fb65d228
AE
2082/* It's OK to call this for a device with no parent */
2083
2084static void rbd_spec_put(struct rbd_spec *spec);
2085static void rbd_dev_unparent(struct rbd_device *rbd_dev)
2086{
2087 rbd_dev_remove_parent(rbd_dev);
2088 rbd_spec_put(rbd_dev->parent_spec);
2089 rbd_dev->parent_spec = NULL;
2090 rbd_dev->parent_overlap = 0;
2091}
2092
a2acd00e
AE
2093/*
2094 * Parent image reference counting is used to determine when an
2095 * image's parent fields can be safely torn down--after there are no
2096 * more in-flight requests to the parent image. When the last
2097 * reference is dropped, cleaning them up is safe.
2098 */
2099static void rbd_dev_parent_put(struct rbd_device *rbd_dev)
2100{
2101 int counter;
2102
2103 if (!rbd_dev->parent_spec)
2104 return;
2105
2106 counter = atomic_dec_return_safe(&rbd_dev->parent_ref);
2107 if (counter > 0)
2108 return;
2109
2110 /* Last reference; clean up parent data structures */
2111
2112 if (!counter)
2113 rbd_dev_unparent(rbd_dev);
2114 else
9584d508 2115 rbd_warn(rbd_dev, "parent reference underflow");
a2acd00e
AE
2116}
2117
2118/*
2119 * If an image has a non-zero parent overlap, get a reference to its
2120 * parent.
2121 *
2122 * Returns true if the rbd device has a parent with a non-zero
2123 * overlap and a reference for it was successfully taken, or
2124 * false otherwise.
2125 */
2126static bool rbd_dev_parent_get(struct rbd_device *rbd_dev)
2127{
ae43e9d0 2128 int counter = 0;
a2acd00e
AE
2129
2130 if (!rbd_dev->parent_spec)
2131 return false;
2132
ae43e9d0
ID
2133 down_read(&rbd_dev->header_rwsem);
2134 if (rbd_dev->parent_overlap)
2135 counter = atomic_inc_return_safe(&rbd_dev->parent_ref);
2136 up_read(&rbd_dev->header_rwsem);
a2acd00e
AE
2137
2138 if (counter < 0)
9584d508 2139 rbd_warn(rbd_dev, "parent reference overflow");
a2acd00e 2140
ae43e9d0 2141 return counter > 0;
a2acd00e
AE
2142}
2143
bf0d5f50
AE
2144/*
2145 * Caller is responsible for filling in the list of object requests
2146 * that comprises the image request, and the Linux request pointer
2147 * (if there is one).
2148 */
cc344fa1
AE
2149static struct rbd_img_request *rbd_img_request_create(
2150 struct rbd_device *rbd_dev,
bf0d5f50 2151 u64 offset, u64 length,
6d2940c8 2152 enum obj_operation_type op_type,
4e752f0a 2153 struct ceph_snap_context *snapc)
bf0d5f50
AE
2154{
2155 struct rbd_img_request *img_request;
bf0d5f50 2156
7a716aac 2157 img_request = kmem_cache_alloc(rbd_img_request_cache, GFP_NOIO);
bf0d5f50
AE
2158 if (!img_request)
2159 return NULL;
2160
bf0d5f50
AE
2161 img_request->rq = NULL;
2162 img_request->rbd_dev = rbd_dev;
2163 img_request->offset = offset;
2164 img_request->length = length;
0c425248 2165 img_request->flags = 0;
90e98c52
GZ
2166 if (op_type == OBJ_OP_DISCARD) {
2167 img_request_discard_set(img_request);
2168 img_request->snapc = snapc;
2169 } else if (op_type == OBJ_OP_WRITE) {
0c425248 2170 img_request_write_set(img_request);
4e752f0a 2171 img_request->snapc = snapc;
0c425248 2172 } else {
bf0d5f50 2173 img_request->snap_id = rbd_dev->spec->snap_id;
0c425248 2174 }
a2acd00e 2175 if (rbd_dev_parent_get(rbd_dev))
d0b2e944 2176 img_request_layered_set(img_request);
bf0d5f50
AE
2177 spin_lock_init(&img_request->completion_lock);
2178 img_request->next_completion = 0;
2179 img_request->callback = NULL;
a5a337d4 2180 img_request->result = 0;
bf0d5f50
AE
2181 img_request->obj_request_count = 0;
2182 INIT_LIST_HEAD(&img_request->obj_requests);
2183 kref_init(&img_request->kref);
2184
37206ee5 2185 dout("%s: rbd_dev %p %s %llu/%llu -> img %p\n", __func__, rbd_dev,
6d2940c8 2186 obj_op_name(op_type), offset, length, img_request);
37206ee5 2187
bf0d5f50
AE
2188 return img_request;
2189}
2190
2191static void rbd_img_request_destroy(struct kref *kref)
2192{
2193 struct rbd_img_request *img_request;
2194 struct rbd_obj_request *obj_request;
2195 struct rbd_obj_request *next_obj_request;
2196
2197 img_request = container_of(kref, struct rbd_img_request, kref);
2198
37206ee5
AE
2199 dout("%s: img %p\n", __func__, img_request);
2200
bf0d5f50
AE
2201 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
2202 rbd_img_obj_request_del(img_request, obj_request);
25dcf954 2203 rbd_assert(img_request->obj_request_count == 0);
bf0d5f50 2204
a2acd00e
AE
2205 if (img_request_layered_test(img_request)) {
2206 img_request_layered_clear(img_request);
2207 rbd_dev_parent_put(img_request->rbd_dev);
2208 }
2209
bef95455
JD
2210 if (img_request_write_test(img_request) ||
2211 img_request_discard_test(img_request))
812164f8 2212 ceph_put_snap_context(img_request->snapc);
bf0d5f50 2213
1c2a9dfe 2214 kmem_cache_free(rbd_img_request_cache, img_request);
bf0d5f50
AE
2215}
2216
e93f3152
AE
2217static struct rbd_img_request *rbd_parent_request_create(
2218 struct rbd_obj_request *obj_request,
2219 u64 img_offset, u64 length)
2220{
2221 struct rbd_img_request *parent_request;
2222 struct rbd_device *rbd_dev;
2223
2224 rbd_assert(obj_request->img_request);
2225 rbd_dev = obj_request->img_request->rbd_dev;
2226
4e752f0a 2227 parent_request = rbd_img_request_create(rbd_dev->parent, img_offset,
6d2940c8 2228 length, OBJ_OP_READ, NULL);
e93f3152
AE
2229 if (!parent_request)
2230 return NULL;
2231
2232 img_request_child_set(parent_request);
2233 rbd_obj_request_get(obj_request);
2234 parent_request->obj_request = obj_request;
2235
2236 return parent_request;
2237}
2238
2239static void rbd_parent_request_destroy(struct kref *kref)
2240{
2241 struct rbd_img_request *parent_request;
2242 struct rbd_obj_request *orig_request;
2243
2244 parent_request = container_of(kref, struct rbd_img_request, kref);
2245 orig_request = parent_request->obj_request;
2246
2247 parent_request->obj_request = NULL;
2248 rbd_obj_request_put(orig_request);
2249 img_request_child_clear(parent_request);
2250
2251 rbd_img_request_destroy(kref);
2252}
2253
1217857f
AE
2254static bool rbd_img_obj_end_request(struct rbd_obj_request *obj_request)
2255{
6365d33a 2256 struct rbd_img_request *img_request;
1217857f
AE
2257 unsigned int xferred;
2258 int result;
8b3e1a56 2259 bool more;
1217857f 2260
6365d33a
AE
2261 rbd_assert(obj_request_img_data_test(obj_request));
2262 img_request = obj_request->img_request;
2263
1217857f
AE
2264 rbd_assert(obj_request->xferred <= (u64)UINT_MAX);
2265 xferred = (unsigned int)obj_request->xferred;
2266 result = obj_request->result;
2267 if (result) {
2268 struct rbd_device *rbd_dev = img_request->rbd_dev;
6d2940c8
GZ
2269 enum obj_operation_type op_type;
2270
90e98c52
GZ
2271 if (img_request_discard_test(img_request))
2272 op_type = OBJ_OP_DISCARD;
2273 else if (img_request_write_test(img_request))
2274 op_type = OBJ_OP_WRITE;
2275 else
2276 op_type = OBJ_OP_READ;
1217857f 2277
9584d508 2278 rbd_warn(rbd_dev, "%s %llx at %llx (%llx)",
6d2940c8
GZ
2279 obj_op_name(op_type), obj_request->length,
2280 obj_request->img_offset, obj_request->offset);
9584d508 2281 rbd_warn(rbd_dev, " result %d xferred %x",
1217857f
AE
2282 result, xferred);
2283 if (!img_request->result)
2284 img_request->result = result;
082a75da
ID
2285 /*
2286 * Need to end I/O on the entire obj_request worth of
2287 * bytes in case of error.
2288 */
2289 xferred = obj_request->length;
1217857f
AE
2290 }
2291
8b3e1a56
AE
2292 if (img_request_child_test(img_request)) {
2293 rbd_assert(img_request->obj_request != NULL);
2294 more = obj_request->which < img_request->obj_request_count - 1;
2295 } else {
2a842aca
CH
2296 blk_status_t status = errno_to_blk_status(result);
2297
8b3e1a56 2298 rbd_assert(img_request->rq != NULL);
7ad18afa 2299
2a842aca 2300 more = blk_update_request(img_request->rq, status, xferred);
7ad18afa 2301 if (!more)
2a842aca 2302 __blk_mq_end_request(img_request->rq, status);
8b3e1a56
AE
2303 }
2304
2305 return more;
1217857f
AE
2306}
2307
2169238d
AE
2308static void rbd_img_obj_callback(struct rbd_obj_request *obj_request)
2309{
2310 struct rbd_img_request *img_request;
2311 u32 which = obj_request->which;
2312 bool more = true;
2313
6365d33a 2314 rbd_assert(obj_request_img_data_test(obj_request));
2169238d
AE
2315 img_request = obj_request->img_request;
2316
2317 dout("%s: img %p obj %p\n", __func__, img_request, obj_request);
2318 rbd_assert(img_request != NULL);
2169238d
AE
2319 rbd_assert(img_request->obj_request_count > 0);
2320 rbd_assert(which != BAD_WHICH);
2321 rbd_assert(which < img_request->obj_request_count);
2169238d
AE
2322
2323 spin_lock_irq(&img_request->completion_lock);
2324 if (which != img_request->next_completion)
2325 goto out;
2326
2327 for_each_obj_request_from(img_request, obj_request) {
2169238d
AE
2328 rbd_assert(more);
2329 rbd_assert(which < img_request->obj_request_count);
2330
2331 if (!obj_request_done_test(obj_request))
2332 break;
1217857f 2333 more = rbd_img_obj_end_request(obj_request);
2169238d
AE
2334 which++;
2335 }
2336
2337 rbd_assert(more ^ (which == img_request->obj_request_count));
2338 img_request->next_completion = which;
2339out:
2340 spin_unlock_irq(&img_request->completion_lock);
0f2d5be7 2341 rbd_img_request_put(img_request);
2169238d
AE
2342
2343 if (!more)
2344 rbd_img_request_complete(img_request);
2345}
2346
3b434a2a
JD
2347/*
2348 * Add individual osd ops to the given ceph_osd_request and prepare
2349 * them for submission. num_ops is the current number of
2350 * osd operations already to the object request.
2351 */
2352static void rbd_img_obj_request_fill(struct rbd_obj_request *obj_request,
2353 struct ceph_osd_request *osd_request,
2354 enum obj_operation_type op_type,
2355 unsigned int num_ops)
2356{
2357 struct rbd_img_request *img_request = obj_request->img_request;
2358 struct rbd_device *rbd_dev = img_request->rbd_dev;
2359 u64 object_size = rbd_obj_bytes(&rbd_dev->header);
2360 u64 offset = obj_request->offset;
2361 u64 length = obj_request->length;
2362 u64 img_end;
2363 u16 opcode;
2364
2365 if (op_type == OBJ_OP_DISCARD) {
d3246fb0
JD
2366 if (!offset && length == object_size &&
2367 (!img_request_layered_test(img_request) ||
2368 !obj_request_overlaps_parent(obj_request))) {
3b434a2a
JD
2369 opcode = CEPH_OSD_OP_DELETE;
2370 } else if ((offset + length == object_size)) {
2371 opcode = CEPH_OSD_OP_TRUNCATE;
2372 } else {
2373 down_read(&rbd_dev->header_rwsem);
2374 img_end = rbd_dev->header.image_size;
2375 up_read(&rbd_dev->header_rwsem);
2376
2377 if (obj_request->img_offset + length == img_end)
2378 opcode = CEPH_OSD_OP_TRUNCATE;
2379 else
2380 opcode = CEPH_OSD_OP_ZERO;
2381 }
2382 } else if (op_type == OBJ_OP_WRITE) {
e30b7577
ID
2383 if (!offset && length == object_size)
2384 opcode = CEPH_OSD_OP_WRITEFULL;
2385 else
2386 opcode = CEPH_OSD_OP_WRITE;
3b434a2a
JD
2387 osd_req_op_alloc_hint_init(osd_request, num_ops,
2388 object_size, object_size);
2389 num_ops++;
2390 } else {
2391 opcode = CEPH_OSD_OP_READ;
2392 }
2393
7e868b6e 2394 if (opcode == CEPH_OSD_OP_DELETE)
144cba14 2395 osd_req_op_init(osd_request, num_ops, opcode, 0);
7e868b6e
ID
2396 else
2397 osd_req_op_extent_init(osd_request, num_ops, opcode,
2398 offset, length, 0, 0);
2399
3b434a2a
JD
2400 if (obj_request->type == OBJ_REQUEST_BIO)
2401 osd_req_op_extent_osd_data_bio(osd_request, num_ops,
2402 obj_request->bio_list, length);
2403 else if (obj_request->type == OBJ_REQUEST_PAGES)
2404 osd_req_op_extent_osd_data_pages(osd_request, num_ops,
2405 obj_request->pages, length,
2406 offset & ~PAGE_MASK, false, false);
2407
2408 /* Discards are also writes */
2409 if (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD)
2410 rbd_osd_req_format_write(obj_request);
2411 else
2412 rbd_osd_req_format_read(obj_request);
2413}
2414
f1a4739f
AE
2415/*
2416 * Split up an image request into one or more object requests, each
2417 * to a different object. The "type" parameter indicates whether
2418 * "data_desc" is the pointer to the head of a list of bio
2419 * structures, or the base of a page array. In either case this
2420 * function assumes data_desc describes memory sufficient to hold
2421 * all data described by the image request.
2422 */
2423static int rbd_img_request_fill(struct rbd_img_request *img_request,
2424 enum obj_request_type type,
2425 void *data_desc)
bf0d5f50
AE
2426{
2427 struct rbd_device *rbd_dev = img_request->rbd_dev;
2428 struct rbd_obj_request *obj_request = NULL;
2429 struct rbd_obj_request *next_obj_request;
a158073c 2430 struct bio *bio_list = NULL;
f1a4739f 2431 unsigned int bio_offset = 0;
a158073c 2432 struct page **pages = NULL;
6d2940c8 2433 enum obj_operation_type op_type;
7da22d29 2434 u64 img_offset;
bf0d5f50 2435 u64 resid;
bf0d5f50 2436
f1a4739f
AE
2437 dout("%s: img %p type %d data_desc %p\n", __func__, img_request,
2438 (int)type, data_desc);
37206ee5 2439
7da22d29 2440 img_offset = img_request->offset;
bf0d5f50 2441 resid = img_request->length;
4dda41d3 2442 rbd_assert(resid > 0);
3b434a2a 2443 op_type = rbd_img_request_op_type(img_request);
f1a4739f
AE
2444
2445 if (type == OBJ_REQUEST_BIO) {
2446 bio_list = data_desc;
4f024f37
KO
2447 rbd_assert(img_offset ==
2448 bio_list->bi_iter.bi_sector << SECTOR_SHIFT);
90e98c52 2449 } else if (type == OBJ_REQUEST_PAGES) {
f1a4739f
AE
2450 pages = data_desc;
2451 }
2452
bf0d5f50 2453 while (resid) {
2fa12320 2454 struct ceph_osd_request *osd_req;
a90bb0c1 2455 u64 object_no = img_offset >> rbd_dev->header.obj_order;
67e2b652
ID
2456 u64 offset = rbd_segment_offset(rbd_dev, img_offset);
2457 u64 length = rbd_segment_length(rbd_dev, img_offset, resid);
bf0d5f50 2458
6c696d85 2459 obj_request = rbd_obj_request_create(type);
bf0d5f50
AE
2460 if (!obj_request)
2461 goto out_unwind;
62054da6 2462
a90bb0c1 2463 obj_request->object_no = object_no;
67e2b652
ID
2464 obj_request->offset = offset;
2465 obj_request->length = length;
2466
03507db6
JD
2467 /*
2468 * set obj_request->img_request before creating the
2469 * osd_request so that it gets the right snapc
2470 */
2471 rbd_img_obj_request_add(img_request, obj_request);
bf0d5f50 2472
f1a4739f
AE
2473 if (type == OBJ_REQUEST_BIO) {
2474 unsigned int clone_size;
2475
2476 rbd_assert(length <= (u64)UINT_MAX);
2477 clone_size = (unsigned int)length;
2478 obj_request->bio_list =
2479 bio_chain_clone_range(&bio_list,
2480 &bio_offset,
2481 clone_size,
2224d879 2482 GFP_NOIO);
f1a4739f 2483 if (!obj_request->bio_list)
62054da6 2484 goto out_unwind;
90e98c52 2485 } else if (type == OBJ_REQUEST_PAGES) {
f1a4739f
AE
2486 unsigned int page_count;
2487
2488 obj_request->pages = pages;
2489 page_count = (u32)calc_pages_for(offset, length);
2490 obj_request->page_count = page_count;
2491 if ((offset + length) & ~PAGE_MASK)
2492 page_count--; /* more on last page */
2493 pages += page_count;
2494 }
bf0d5f50 2495
6d2940c8
GZ
2496 osd_req = rbd_osd_req_create(rbd_dev, op_type,
2497 (op_type == OBJ_OP_WRITE) ? 2 : 1,
2498 obj_request);
2fa12320 2499 if (!osd_req)
62054da6 2500 goto out_unwind;
3b434a2a 2501
2fa12320 2502 obj_request->osd_req = osd_req;
2169238d 2503 obj_request->callback = rbd_img_obj_callback;
3b434a2a 2504 obj_request->img_offset = img_offset;
9d4df01f 2505
3b434a2a 2506 rbd_img_obj_request_fill(obj_request, osd_req, op_type, 0);
430c28c3 2507
7da22d29 2508 img_offset += length;
bf0d5f50
AE
2509 resid -= length;
2510 }
2511
2512 return 0;
2513
bf0d5f50
AE
2514out_unwind:
2515 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
42dd037c 2516 rbd_img_obj_request_del(img_request, obj_request);
bf0d5f50
AE
2517
2518 return -ENOMEM;
2519}
2520
0eefd470 2521static void
2761713d 2522rbd_osd_copyup_callback(struct rbd_obj_request *obj_request)
0eefd470
AE
2523{
2524 struct rbd_img_request *img_request;
2525 struct rbd_device *rbd_dev;
ebda6408 2526 struct page **pages;
0eefd470
AE
2527 u32 page_count;
2528
2761713d
ID
2529 dout("%s: obj %p\n", __func__, obj_request);
2530
d3246fb0
JD
2531 rbd_assert(obj_request->type == OBJ_REQUEST_BIO ||
2532 obj_request->type == OBJ_REQUEST_NODATA);
0eefd470
AE
2533 rbd_assert(obj_request_img_data_test(obj_request));
2534 img_request = obj_request->img_request;
2535 rbd_assert(img_request);
2536
2537 rbd_dev = img_request->rbd_dev;
2538 rbd_assert(rbd_dev);
0eefd470 2539
ebda6408
AE
2540 pages = obj_request->copyup_pages;
2541 rbd_assert(pages != NULL);
0eefd470 2542 obj_request->copyup_pages = NULL;
ebda6408
AE
2543 page_count = obj_request->copyup_page_count;
2544 rbd_assert(page_count);
2545 obj_request->copyup_page_count = 0;
2546 ceph_release_page_vector(pages, page_count);
0eefd470
AE
2547
2548 /*
2549 * We want the transfer count to reflect the size of the
2550 * original write request. There is no such thing as a
2551 * successful short write, so if the request was successful
2552 * we can just set it to the originally-requested length.
2553 */
2554 if (!obj_request->result)
2555 obj_request->xferred = obj_request->length;
2556
2761713d 2557 obj_request_done_set(obj_request);
0eefd470
AE
2558}
2559
3d7efd18
AE
2560static void
2561rbd_img_obj_parent_read_full_callback(struct rbd_img_request *img_request)
2562{
2563 struct rbd_obj_request *orig_request;
0eefd470 2564 struct ceph_osd_request *osd_req;
0eefd470 2565 struct rbd_device *rbd_dev;
3d7efd18 2566 struct page **pages;
d3246fb0 2567 enum obj_operation_type op_type;
ebda6408 2568 u32 page_count;
bbea1c1a 2569 int img_result;
ebda6408 2570 u64 parent_length;
3d7efd18
AE
2571
2572 rbd_assert(img_request_child_test(img_request));
2573
2574 /* First get what we need from the image request */
2575
2576 pages = img_request->copyup_pages;
2577 rbd_assert(pages != NULL);
2578 img_request->copyup_pages = NULL;
ebda6408
AE
2579 page_count = img_request->copyup_page_count;
2580 rbd_assert(page_count);
2581 img_request->copyup_page_count = 0;
3d7efd18
AE
2582
2583 orig_request = img_request->obj_request;
2584 rbd_assert(orig_request != NULL);
b91f09f1 2585 rbd_assert(obj_request_type_valid(orig_request->type));
bbea1c1a 2586 img_result = img_request->result;
ebda6408 2587 parent_length = img_request->length;
fa355112 2588 rbd_assert(img_result || parent_length == img_request->xferred);
91c6febb 2589 rbd_img_request_put(img_request);
3d7efd18 2590
91c6febb
AE
2591 rbd_assert(orig_request->img_request);
2592 rbd_dev = orig_request->img_request->rbd_dev;
0eefd470 2593 rbd_assert(rbd_dev);
0eefd470 2594
bbea1c1a
AE
2595 /*
2596 * If the overlap has become 0 (most likely because the
2597 * image has been flattened) we need to free the pages
2598 * and re-submit the original write request.
2599 */
2600 if (!rbd_dev->parent_overlap) {
bbea1c1a 2601 ceph_release_page_vector(pages, page_count);
980917fc
ID
2602 rbd_obj_request_submit(orig_request);
2603 return;
bbea1c1a 2604 }
0eefd470 2605
bbea1c1a 2606 if (img_result)
0eefd470 2607 goto out_err;
0eefd470 2608
8785b1d4
AE
2609 /*
2610 * The original osd request is of no use to use any more.
0ccd5926 2611 * We need a new one that can hold the three ops in a copyup
8785b1d4
AE
2612 * request. Allocate the new copyup osd request for the
2613 * original request, and release the old one.
2614 */
bbea1c1a 2615 img_result = -ENOMEM;
0eefd470
AE
2616 osd_req = rbd_osd_req_create_copyup(orig_request);
2617 if (!osd_req)
2618 goto out_err;
8785b1d4 2619 rbd_osd_req_destroy(orig_request->osd_req);
0eefd470
AE
2620 orig_request->osd_req = osd_req;
2621 orig_request->copyup_pages = pages;
ebda6408 2622 orig_request->copyup_page_count = page_count;
3d7efd18 2623
0eefd470 2624 /* Initialize the copyup op */
3d7efd18 2625
0eefd470 2626 osd_req_op_cls_init(osd_req, 0, CEPH_OSD_OP_CALL, "rbd", "copyup");
ebda6408 2627 osd_req_op_cls_request_data_pages(osd_req, 0, pages, parent_length, 0,
0eefd470 2628 false, false);
3d7efd18 2629
d3246fb0 2630 /* Add the other op(s) */
0eefd470 2631
d3246fb0
JD
2632 op_type = rbd_img_request_op_type(orig_request->img_request);
2633 rbd_img_obj_request_fill(orig_request, osd_req, op_type, 1);
0eefd470
AE
2634
2635 /* All set, send it off. */
2636
980917fc
ID
2637 rbd_obj_request_submit(orig_request);
2638 return;
0eefd470 2639
0eefd470 2640out_err:
fa355112 2641 ceph_release_page_vector(pages, page_count);
0dcc685e 2642 rbd_obj_request_error(orig_request, img_result);
3d7efd18
AE
2643}
2644
2645/*
2646 * Read from the parent image the range of data that covers the
2647 * entire target of the given object request. This is used for
2648 * satisfying a layered image write request when the target of an
2649 * object request from the image request does not exist.
2650 *
2651 * A page array big enough to hold the returned data is allocated
2652 * and supplied to rbd_img_request_fill() as the "data descriptor."
2653 * When the read completes, this page array will be transferred to
2654 * the original object request for the copyup operation.
2655 *
c2e82414
ID
2656 * If an error occurs, it is recorded as the result of the original
2657 * object request in rbd_img_obj_exists_callback().
3d7efd18
AE
2658 */
2659static int rbd_img_obj_parent_read_full(struct rbd_obj_request *obj_request)
2660{
058aa991 2661 struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev;
3d7efd18 2662 struct rbd_img_request *parent_request = NULL;
3d7efd18
AE
2663 u64 img_offset;
2664 u64 length;
2665 struct page **pages = NULL;
2666 u32 page_count;
2667 int result;
2668
3d7efd18
AE
2669 rbd_assert(rbd_dev->parent != NULL);
2670
2671 /*
2672 * Determine the byte range covered by the object in the
2673 * child image to which the original request was to be sent.
2674 */
2675 img_offset = obj_request->img_offset - obj_request->offset;
5bc3fb17 2676 length = rbd_obj_bytes(&rbd_dev->header);
3d7efd18 2677
a9e8ba2c
AE
2678 /*
2679 * There is no defined parent data beyond the parent
2680 * overlap, so limit what we read at that boundary if
2681 * necessary.
2682 */
2683 if (img_offset + length > rbd_dev->parent_overlap) {
2684 rbd_assert(img_offset < rbd_dev->parent_overlap);
2685 length = rbd_dev->parent_overlap - img_offset;
2686 }
2687
3d7efd18
AE
2688 /*
2689 * Allocate a page array big enough to receive the data read
2690 * from the parent.
2691 */
2692 page_count = (u32)calc_pages_for(0, length);
2693 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2694 if (IS_ERR(pages)) {
2695 result = PTR_ERR(pages);
2696 pages = NULL;
2697 goto out_err;
2698 }
2699
2700 result = -ENOMEM;
e93f3152
AE
2701 parent_request = rbd_parent_request_create(obj_request,
2702 img_offset, length);
3d7efd18
AE
2703 if (!parent_request)
2704 goto out_err;
3d7efd18
AE
2705
2706 result = rbd_img_request_fill(parent_request, OBJ_REQUEST_PAGES, pages);
2707 if (result)
2708 goto out_err;
058aa991 2709
3d7efd18 2710 parent_request->copyup_pages = pages;
ebda6408 2711 parent_request->copyup_page_count = page_count;
3d7efd18 2712 parent_request->callback = rbd_img_obj_parent_read_full_callback;
058aa991 2713
3d7efd18
AE
2714 result = rbd_img_request_submit(parent_request);
2715 if (!result)
2716 return 0;
2717
2718 parent_request->copyup_pages = NULL;
ebda6408 2719 parent_request->copyup_page_count = 0;
3d7efd18
AE
2720 parent_request->obj_request = NULL;
2721 rbd_obj_request_put(obj_request);
2722out_err:
2723 if (pages)
2724 ceph_release_page_vector(pages, page_count);
2725 if (parent_request)
2726 rbd_img_request_put(parent_request);
3d7efd18
AE
2727 return result;
2728}
2729
c5b5ef6c
AE
2730static void rbd_img_obj_exists_callback(struct rbd_obj_request *obj_request)
2731{
c5b5ef6c 2732 struct rbd_obj_request *orig_request;
638f5abe 2733 struct rbd_device *rbd_dev;
c5b5ef6c
AE
2734 int result;
2735
2736 rbd_assert(!obj_request_img_data_test(obj_request));
2737
2738 /*
2739 * All we need from the object request is the original
2740 * request and the result of the STAT op. Grab those, then
2741 * we're done with the request.
2742 */
2743 orig_request = obj_request->obj_request;
2744 obj_request->obj_request = NULL;
912c317d 2745 rbd_obj_request_put(orig_request);
c5b5ef6c
AE
2746 rbd_assert(orig_request);
2747 rbd_assert(orig_request->img_request);
2748
2749 result = obj_request->result;
2750 obj_request->result = 0;
2751
2752 dout("%s: obj %p for obj %p result %d %llu/%llu\n", __func__,
2753 obj_request, orig_request, result,
2754 obj_request->xferred, obj_request->length);
2755 rbd_obj_request_put(obj_request);
2756
638f5abe
AE
2757 /*
2758 * If the overlap has become 0 (most likely because the
980917fc
ID
2759 * image has been flattened) we need to re-submit the
2760 * original request.
638f5abe
AE
2761 */
2762 rbd_dev = orig_request->img_request->rbd_dev;
2763 if (!rbd_dev->parent_overlap) {
980917fc
ID
2764 rbd_obj_request_submit(orig_request);
2765 return;
638f5abe 2766 }
c5b5ef6c
AE
2767
2768 /*
2769 * Our only purpose here is to determine whether the object
2770 * exists, and we don't want to treat the non-existence as
2771 * an error. If something else comes back, transfer the
2772 * error to the original request and complete it now.
2773 */
2774 if (!result) {
2775 obj_request_existence_set(orig_request, true);
2776 } else if (result == -ENOENT) {
2777 obj_request_existence_set(orig_request, false);
c2e82414
ID
2778 } else {
2779 goto fail_orig_request;
c5b5ef6c
AE
2780 }
2781
2782 /*
2783 * Resubmit the original request now that we have recorded
2784 * whether the target object exists.
2785 */
c2e82414
ID
2786 result = rbd_img_obj_request_submit(orig_request);
2787 if (result)
2788 goto fail_orig_request;
2789
2790 return;
2791
2792fail_orig_request:
0dcc685e 2793 rbd_obj_request_error(orig_request, result);
c5b5ef6c
AE
2794}
2795
2796static int rbd_img_obj_exists_submit(struct rbd_obj_request *obj_request)
2797{
058aa991 2798 struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev;
c5b5ef6c 2799 struct rbd_obj_request *stat_request;
710214e3 2800 struct page **pages;
c5b5ef6c
AE
2801 u32 page_count;
2802 size_t size;
2803 int ret;
2804
6c696d85 2805 stat_request = rbd_obj_request_create(OBJ_REQUEST_PAGES);
710214e3
ID
2806 if (!stat_request)
2807 return -ENOMEM;
2808
a90bb0c1
ID
2809 stat_request->object_no = obj_request->object_no;
2810
710214e3
ID
2811 stat_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_READ, 1,
2812 stat_request);
2813 if (!stat_request->osd_req) {
2814 ret = -ENOMEM;
2815 goto fail_stat_request;
2816 }
2817
c5b5ef6c
AE
2818 /*
2819 * The response data for a STAT call consists of:
2820 * le64 length;
2821 * struct {
2822 * le32 tv_sec;
2823 * le32 tv_nsec;
2824 * } mtime;
2825 */
2826 size = sizeof (__le64) + sizeof (__le32) + sizeof (__le32);
2827 page_count = (u32)calc_pages_for(0, size);
2828 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
710214e3
ID
2829 if (IS_ERR(pages)) {
2830 ret = PTR_ERR(pages);
2831 goto fail_stat_request;
2832 }
c5b5ef6c 2833
710214e3
ID
2834 osd_req_op_init(stat_request->osd_req, 0, CEPH_OSD_OP_STAT, 0);
2835 osd_req_op_raw_data_in_pages(stat_request->osd_req, 0, pages, size, 0,
2836 false, false);
c5b5ef6c
AE
2837
2838 rbd_obj_request_get(obj_request);
2839 stat_request->obj_request = obj_request;
2840 stat_request->pages = pages;
2841 stat_request->page_count = page_count;
c5b5ef6c
AE
2842 stat_request->callback = rbd_img_obj_exists_callback;
2843
980917fc
ID
2844 rbd_obj_request_submit(stat_request);
2845 return 0;
c5b5ef6c 2846
710214e3
ID
2847fail_stat_request:
2848 rbd_obj_request_put(stat_request);
c5b5ef6c
AE
2849 return ret;
2850}
2851
70d045f6 2852static bool img_obj_request_simple(struct rbd_obj_request *obj_request)
b454e36d 2853{
058aa991
ID
2854 struct rbd_img_request *img_request = obj_request->img_request;
2855 struct rbd_device *rbd_dev = img_request->rbd_dev;
b454e36d 2856
70d045f6 2857 /* Reads */
1c220881
JD
2858 if (!img_request_write_test(img_request) &&
2859 !img_request_discard_test(img_request))
70d045f6
ID
2860 return true;
2861
2862 /* Non-layered writes */
2863 if (!img_request_layered_test(img_request))
2864 return true;
2865
b454e36d 2866 /*
70d045f6
ID
2867 * Layered writes outside of the parent overlap range don't
2868 * share any data with the parent.
b454e36d 2869 */
70d045f6
ID
2870 if (!obj_request_overlaps_parent(obj_request))
2871 return true;
b454e36d 2872
c622d226
GZ
2873 /*
2874 * Entire-object layered writes - we will overwrite whatever
2875 * parent data there is anyway.
2876 */
2877 if (!obj_request->offset &&
2878 obj_request->length == rbd_obj_bytes(&rbd_dev->header))
2879 return true;
2880
70d045f6
ID
2881 /*
2882 * If the object is known to already exist, its parent data has
2883 * already been copied.
2884 */
2885 if (obj_request_known_test(obj_request) &&
2886 obj_request_exists_test(obj_request))
2887 return true;
2888
2889 return false;
2890}
2891
2892static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request)
2893{
058aa991
ID
2894 rbd_assert(obj_request_img_data_test(obj_request));
2895 rbd_assert(obj_request_type_valid(obj_request->type));
2896 rbd_assert(obj_request->img_request);
b454e36d 2897
70d045f6 2898 if (img_obj_request_simple(obj_request)) {
980917fc
ID
2899 rbd_obj_request_submit(obj_request);
2900 return 0;
b454e36d
AE
2901 }
2902
2903 /*
3d7efd18
AE
2904 * It's a layered write. The target object might exist but
2905 * we may not know that yet. If we know it doesn't exist,
2906 * start by reading the data for the full target object from
2907 * the parent so we can use it for a copyup to the target.
b454e36d 2908 */
70d045f6 2909 if (obj_request_known_test(obj_request))
3d7efd18
AE
2910 return rbd_img_obj_parent_read_full(obj_request);
2911
2912 /* We don't know whether the target exists. Go find out. */
b454e36d
AE
2913
2914 return rbd_img_obj_exists_submit(obj_request);
2915}
2916
bf0d5f50
AE
2917static int rbd_img_request_submit(struct rbd_img_request *img_request)
2918{
bf0d5f50 2919 struct rbd_obj_request *obj_request;
46faeed4 2920 struct rbd_obj_request *next_obj_request;
663ae2cc 2921 int ret = 0;
bf0d5f50 2922
37206ee5 2923 dout("%s: img %p\n", __func__, img_request);
bf0d5f50 2924
663ae2cc
ID
2925 rbd_img_request_get(img_request);
2926 for_each_obj_request_safe(img_request, obj_request, next_obj_request) {
b454e36d 2927 ret = rbd_img_obj_request_submit(obj_request);
bf0d5f50 2928 if (ret)
663ae2cc 2929 goto out_put_ireq;
bf0d5f50
AE
2930 }
2931
663ae2cc
ID
2932out_put_ireq:
2933 rbd_img_request_put(img_request);
2934 return ret;
bf0d5f50 2935}
8b3e1a56
AE
2936
2937static void rbd_img_parent_read_callback(struct rbd_img_request *img_request)
2938{
2939 struct rbd_obj_request *obj_request;
a9e8ba2c
AE
2940 struct rbd_device *rbd_dev;
2941 u64 obj_end;
02c74fba
AE
2942 u64 img_xferred;
2943 int img_result;
8b3e1a56
AE
2944
2945 rbd_assert(img_request_child_test(img_request));
2946
02c74fba
AE
2947 /* First get what we need from the image request and release it */
2948
8b3e1a56 2949 obj_request = img_request->obj_request;
02c74fba
AE
2950 img_xferred = img_request->xferred;
2951 img_result = img_request->result;
2952 rbd_img_request_put(img_request);
2953
2954 /*
2955 * If the overlap has become 0 (most likely because the
2956 * image has been flattened) we need to re-submit the
2957 * original request.
2958 */
a9e8ba2c
AE
2959 rbd_assert(obj_request);
2960 rbd_assert(obj_request->img_request);
02c74fba
AE
2961 rbd_dev = obj_request->img_request->rbd_dev;
2962 if (!rbd_dev->parent_overlap) {
980917fc
ID
2963 rbd_obj_request_submit(obj_request);
2964 return;
02c74fba 2965 }
a9e8ba2c 2966
02c74fba 2967 obj_request->result = img_result;
a9e8ba2c
AE
2968 if (obj_request->result)
2969 goto out;
2970
2971 /*
2972 * We need to zero anything beyond the parent overlap
2973 * boundary. Since rbd_img_obj_request_read_callback()
2974 * will zero anything beyond the end of a short read, an
2975 * easy way to do this is to pretend the data from the
2976 * parent came up short--ending at the overlap boundary.
2977 */
2978 rbd_assert(obj_request->img_offset < U64_MAX - obj_request->length);
2979 obj_end = obj_request->img_offset + obj_request->length;
a9e8ba2c
AE
2980 if (obj_end > rbd_dev->parent_overlap) {
2981 u64 xferred = 0;
2982
2983 if (obj_request->img_offset < rbd_dev->parent_overlap)
2984 xferred = rbd_dev->parent_overlap -
2985 obj_request->img_offset;
8b3e1a56 2986
02c74fba 2987 obj_request->xferred = min(img_xferred, xferred);
a9e8ba2c 2988 } else {
02c74fba 2989 obj_request->xferred = img_xferred;
a9e8ba2c
AE
2990 }
2991out:
8b3e1a56
AE
2992 rbd_img_obj_request_read_callback(obj_request);
2993 rbd_obj_request_complete(obj_request);
2994}
2995
2996static void rbd_img_parent_read(struct rbd_obj_request *obj_request)
2997{
8b3e1a56
AE
2998 struct rbd_img_request *img_request;
2999 int result;
3000
3001 rbd_assert(obj_request_img_data_test(obj_request));
3002 rbd_assert(obj_request->img_request != NULL);
3003 rbd_assert(obj_request->result == (s32) -ENOENT);
5b2ab72d 3004 rbd_assert(obj_request_type_valid(obj_request->type));
8b3e1a56 3005
8b3e1a56 3006 /* rbd_read_finish(obj_request, obj_request->length); */
e93f3152 3007 img_request = rbd_parent_request_create(obj_request,
8b3e1a56 3008 obj_request->img_offset,
e93f3152 3009 obj_request->length);
8b3e1a56
AE
3010 result = -ENOMEM;
3011 if (!img_request)
3012 goto out_err;
3013
5b2ab72d
AE
3014 if (obj_request->type == OBJ_REQUEST_BIO)
3015 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
3016 obj_request->bio_list);
3017 else
3018 result = rbd_img_request_fill(img_request, OBJ_REQUEST_PAGES,
3019 obj_request->pages);
8b3e1a56
AE
3020 if (result)
3021 goto out_err;
3022
3023 img_request->callback = rbd_img_parent_read_callback;
3024 result = rbd_img_request_submit(img_request);
3025 if (result)
3026 goto out_err;
3027
3028 return;
3029out_err:
3030 if (img_request)
3031 rbd_img_request_put(img_request);
3032 obj_request->result = result;
3033 obj_request->xferred = 0;
3034 obj_request_done_set(obj_request);
3035}
bf0d5f50 3036
ed95b21a 3037static const struct rbd_client_id rbd_empty_cid;
b8d70035 3038
ed95b21a
ID
3039static bool rbd_cid_equal(const struct rbd_client_id *lhs,
3040 const struct rbd_client_id *rhs)
3041{
3042 return lhs->gid == rhs->gid && lhs->handle == rhs->handle;
3043}
3044
3045static struct rbd_client_id rbd_get_cid(struct rbd_device *rbd_dev)
3046{
3047 struct rbd_client_id cid;
3048
3049 mutex_lock(&rbd_dev->watch_mutex);
3050 cid.gid = ceph_client_gid(rbd_dev->rbd_client->client);
3051 cid.handle = rbd_dev->watch_cookie;
3052 mutex_unlock(&rbd_dev->watch_mutex);
3053 return cid;
3054}
3055
3056/*
3057 * lock_rwsem must be held for write
3058 */
3059static void rbd_set_owner_cid(struct rbd_device *rbd_dev,
3060 const struct rbd_client_id *cid)
3061{
3062 dout("%s rbd_dev %p %llu-%llu -> %llu-%llu\n", __func__, rbd_dev,
3063 rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle,
3064 cid->gid, cid->handle);
3065 rbd_dev->owner_cid = *cid; /* struct */
3066}
3067
3068static void format_lock_cookie(struct rbd_device *rbd_dev, char *buf)
3069{
3070 mutex_lock(&rbd_dev->watch_mutex);
3071 sprintf(buf, "%s %llu", RBD_LOCK_COOKIE_PREFIX, rbd_dev->watch_cookie);
3072 mutex_unlock(&rbd_dev->watch_mutex);
3073}
3074
3075/*
3076 * lock_rwsem must be held for write
3077 */
3078static int rbd_lock(struct rbd_device *rbd_dev)
b8d70035 3079{
922dab61 3080 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
ed95b21a
ID
3081 struct rbd_client_id cid = rbd_get_cid(rbd_dev);
3082 char cookie[32];
e627db08 3083 int ret;
b8d70035 3084
cbbfb0ff
ID
3085 WARN_ON(__rbd_is_lock_owner(rbd_dev) ||
3086 rbd_dev->lock_cookie[0] != '\0');
52bb1f9b 3087
ed95b21a
ID
3088 format_lock_cookie(rbd_dev, cookie);
3089 ret = ceph_cls_lock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc,
3090 RBD_LOCK_NAME, CEPH_CLS_LOCK_EXCLUSIVE, cookie,
3091 RBD_LOCK_TAG, "", 0);
e627db08 3092 if (ret)
ed95b21a 3093 return ret;
b8d70035 3094
ed95b21a 3095 rbd_dev->lock_state = RBD_LOCK_STATE_LOCKED;
cbbfb0ff 3096 strcpy(rbd_dev->lock_cookie, cookie);
ed95b21a
ID
3097 rbd_set_owner_cid(rbd_dev, &cid);
3098 queue_work(rbd_dev->task_wq, &rbd_dev->acquired_lock_work);
3099 return 0;
b8d70035
AE
3100}
3101
ed95b21a
ID
3102/*
3103 * lock_rwsem must be held for write
3104 */
bbead745 3105static void rbd_unlock(struct rbd_device *rbd_dev)
bb040aa0 3106{
922dab61 3107 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
bb040aa0
ID
3108 int ret;
3109
cbbfb0ff
ID
3110 WARN_ON(!__rbd_is_lock_owner(rbd_dev) ||
3111 rbd_dev->lock_cookie[0] == '\0');
bb040aa0 3112
ed95b21a 3113 ret = ceph_cls_unlock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc,
cbbfb0ff 3114 RBD_LOCK_NAME, rbd_dev->lock_cookie);
bbead745
ID
3115 if (ret && ret != -ENOENT)
3116 rbd_warn(rbd_dev, "failed to unlock: %d", ret);
bb040aa0 3117
bbead745
ID
3118 /* treat errors as the image is unlocked */
3119 rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED;
cbbfb0ff 3120 rbd_dev->lock_cookie[0] = '\0';
ed95b21a
ID
3121 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
3122 queue_work(rbd_dev->task_wq, &rbd_dev->released_lock_work);
bb040aa0
ID
3123}
3124
ed95b21a
ID
3125static int __rbd_notify_op_lock(struct rbd_device *rbd_dev,
3126 enum rbd_notify_op notify_op,
3127 struct page ***preply_pages,
3128 size_t *preply_len)
9969ebc5
AE
3129{
3130 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
ed95b21a
ID
3131 struct rbd_client_id cid = rbd_get_cid(rbd_dev);
3132 int buf_size = 4 + 8 + 8 + CEPH_ENCODING_START_BLK_LEN;
3133 char buf[buf_size];
3134 void *p = buf;
9969ebc5 3135
ed95b21a 3136 dout("%s rbd_dev %p notify_op %d\n", __func__, rbd_dev, notify_op);
9969ebc5 3137
ed95b21a
ID
3138 /* encode *LockPayload NotifyMessage (op + ClientId) */
3139 ceph_start_encoding(&p, 2, 1, buf_size - CEPH_ENCODING_START_BLK_LEN);
3140 ceph_encode_32(&p, notify_op);
3141 ceph_encode_64(&p, cid.gid);
3142 ceph_encode_64(&p, cid.handle);
8eb87565 3143
ed95b21a
ID
3144 return ceph_osdc_notify(osdc, &rbd_dev->header_oid,
3145 &rbd_dev->header_oloc, buf, buf_size,
3146 RBD_NOTIFY_TIMEOUT, preply_pages, preply_len);
b30a01f2
ID
3147}
3148
ed95b21a
ID
3149static void rbd_notify_op_lock(struct rbd_device *rbd_dev,
3150 enum rbd_notify_op notify_op)
b30a01f2 3151{
ed95b21a
ID
3152 struct page **reply_pages;
3153 size_t reply_len;
b30a01f2 3154
ed95b21a
ID
3155 __rbd_notify_op_lock(rbd_dev, notify_op, &reply_pages, &reply_len);
3156 ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len));
3157}
b30a01f2 3158
ed95b21a
ID
3159static void rbd_notify_acquired_lock(struct work_struct *work)
3160{
3161 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
3162 acquired_lock_work);
76756a51 3163
ed95b21a 3164 rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_ACQUIRED_LOCK);
c525f036
ID
3165}
3166
ed95b21a 3167static void rbd_notify_released_lock(struct work_struct *work)
c525f036 3168{
ed95b21a
ID
3169 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
3170 released_lock_work);
811c6688 3171
ed95b21a 3172 rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_RELEASED_LOCK);
fca27065
ID
3173}
3174
ed95b21a 3175static int rbd_request_lock(struct rbd_device *rbd_dev)
36be9a76 3176{
ed95b21a
ID
3177 struct page **reply_pages;
3178 size_t reply_len;
3179 bool lock_owner_responded = false;
36be9a76
AE
3180 int ret;
3181
ed95b21a 3182 dout("%s rbd_dev %p\n", __func__, rbd_dev);
36be9a76 3183
ed95b21a
ID
3184 ret = __rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_REQUEST_LOCK,
3185 &reply_pages, &reply_len);
3186 if (ret && ret != -ETIMEDOUT) {
3187 rbd_warn(rbd_dev, "failed to request lock: %d", ret);
36be9a76 3188 goto out;
ed95b21a 3189 }
36be9a76 3190
ed95b21a
ID
3191 if (reply_len > 0 && reply_len <= PAGE_SIZE) {
3192 void *p = page_address(reply_pages[0]);
3193 void *const end = p + reply_len;
3194 u32 n;
36be9a76 3195
ed95b21a
ID
3196 ceph_decode_32_safe(&p, end, n, e_inval); /* num_acks */
3197 while (n--) {
3198 u8 struct_v;
3199 u32 len;
36be9a76 3200
ed95b21a
ID
3201 ceph_decode_need(&p, end, 8 + 8, e_inval);
3202 p += 8 + 8; /* skip gid and cookie */
04017e29 3203
ed95b21a
ID
3204 ceph_decode_32_safe(&p, end, len, e_inval);
3205 if (!len)
3206 continue;
3207
3208 if (lock_owner_responded) {
3209 rbd_warn(rbd_dev,
3210 "duplicate lock owners detected");
3211 ret = -EIO;
3212 goto out;
3213 }
3214
3215 lock_owner_responded = true;
3216 ret = ceph_start_decoding(&p, end, 1, "ResponseMessage",
3217 &struct_v, &len);
3218 if (ret) {
3219 rbd_warn(rbd_dev,
3220 "failed to decode ResponseMessage: %d",
3221 ret);
3222 goto e_inval;
3223 }
3224
3225 ret = ceph_decode_32(&p);
3226 }
3227 }
3228
3229 if (!lock_owner_responded) {
3230 rbd_warn(rbd_dev, "no lock owners detected");
3231 ret = -ETIMEDOUT;
3232 }
3233
3234out:
3235 ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len));
3236 return ret;
3237
3238e_inval:
3239 ret = -EINVAL;
3240 goto out;
3241}
3242
3243static void wake_requests(struct rbd_device *rbd_dev, bool wake_all)
3244{
3245 dout("%s rbd_dev %p wake_all %d\n", __func__, rbd_dev, wake_all);
3246
3247 cancel_delayed_work(&rbd_dev->lock_dwork);
3248 if (wake_all)
3249 wake_up_all(&rbd_dev->lock_waitq);
3250 else
3251 wake_up(&rbd_dev->lock_waitq);
3252}
3253
3254static int get_lock_owner_info(struct rbd_device *rbd_dev,
3255 struct ceph_locker **lockers, u32 *num_lockers)
3256{
3257 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3258 u8 lock_type;
3259 char *lock_tag;
3260 int ret;
3261
3262 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3263
3264 ret = ceph_cls_lock_info(osdc, &rbd_dev->header_oid,
3265 &rbd_dev->header_oloc, RBD_LOCK_NAME,
3266 &lock_type, &lock_tag, lockers, num_lockers);
3267 if (ret)
3268 return ret;
3269
3270 if (*num_lockers == 0) {
3271 dout("%s rbd_dev %p no lockers detected\n", __func__, rbd_dev);
3272 goto out;
3273 }
3274
3275 if (strcmp(lock_tag, RBD_LOCK_TAG)) {
3276 rbd_warn(rbd_dev, "locked by external mechanism, tag %s",
3277 lock_tag);
3278 ret = -EBUSY;
3279 goto out;
3280 }
3281
3282 if (lock_type == CEPH_CLS_LOCK_SHARED) {
3283 rbd_warn(rbd_dev, "shared lock type detected");
3284 ret = -EBUSY;
3285 goto out;
3286 }
3287
3288 if (strncmp((*lockers)[0].id.cookie, RBD_LOCK_COOKIE_PREFIX,
3289 strlen(RBD_LOCK_COOKIE_PREFIX))) {
3290 rbd_warn(rbd_dev, "locked by external mechanism, cookie %s",
3291 (*lockers)[0].id.cookie);
3292 ret = -EBUSY;
3293 goto out;
3294 }
3295
3296out:
3297 kfree(lock_tag);
3298 return ret;
3299}
3300
3301static int find_watcher(struct rbd_device *rbd_dev,
3302 const struct ceph_locker *locker)
3303{
3304 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3305 struct ceph_watch_item *watchers;
3306 u32 num_watchers;
3307 u64 cookie;
3308 int i;
3309 int ret;
3310
3311 ret = ceph_osdc_list_watchers(osdc, &rbd_dev->header_oid,
3312 &rbd_dev->header_oloc, &watchers,
3313 &num_watchers);
3314 if (ret)
3315 return ret;
3316
3317 sscanf(locker->id.cookie, RBD_LOCK_COOKIE_PREFIX " %llu", &cookie);
3318 for (i = 0; i < num_watchers; i++) {
3319 if (!memcmp(&watchers[i].addr, &locker->info.addr,
3320 sizeof(locker->info.addr)) &&
3321 watchers[i].cookie == cookie) {
3322 struct rbd_client_id cid = {
3323 .gid = le64_to_cpu(watchers[i].name.num),
3324 .handle = cookie,
3325 };
3326
3327 dout("%s rbd_dev %p found cid %llu-%llu\n", __func__,
3328 rbd_dev, cid.gid, cid.handle);
3329 rbd_set_owner_cid(rbd_dev, &cid);
3330 ret = 1;
3331 goto out;
3332 }
3333 }
3334
3335 dout("%s rbd_dev %p no watchers\n", __func__, rbd_dev);
3336 ret = 0;
3337out:
3338 kfree(watchers);
3339 return ret;
3340}
3341
3342/*
3343 * lock_rwsem must be held for write
3344 */
3345static int rbd_try_lock(struct rbd_device *rbd_dev)
3346{
3347 struct ceph_client *client = rbd_dev->rbd_client->client;
3348 struct ceph_locker *lockers;
3349 u32 num_lockers;
3350 int ret;
3351
3352 for (;;) {
3353 ret = rbd_lock(rbd_dev);
3354 if (ret != -EBUSY)
3355 return ret;
3356
3357 /* determine if the current lock holder is still alive */
3358 ret = get_lock_owner_info(rbd_dev, &lockers, &num_lockers);
3359 if (ret)
3360 return ret;
3361
3362 if (num_lockers == 0)
3363 goto again;
3364
3365 ret = find_watcher(rbd_dev, lockers);
3366 if (ret) {
3367 if (ret > 0)
3368 ret = 0; /* have to request lock */
3369 goto out;
3370 }
3371
3372 rbd_warn(rbd_dev, "%s%llu seems dead, breaking lock",
3373 ENTITY_NAME(lockers[0].id.name));
3374
3375 ret = ceph_monc_blacklist_add(&client->monc,
3376 &lockers[0].info.addr);
3377 if (ret) {
3378 rbd_warn(rbd_dev, "blacklist of %s%llu failed: %d",
3379 ENTITY_NAME(lockers[0].id.name), ret);
3380 goto out;
3381 }
3382
3383 ret = ceph_cls_break_lock(&client->osdc, &rbd_dev->header_oid,
3384 &rbd_dev->header_oloc, RBD_LOCK_NAME,
3385 lockers[0].id.cookie,
3386 &lockers[0].id.name);
3387 if (ret && ret != -ENOENT)
3388 goto out;
3389
3390again:
3391 ceph_free_lockers(lockers, num_lockers);
3392 }
3393
3394out:
3395 ceph_free_lockers(lockers, num_lockers);
3396 return ret;
3397}
3398
3399/*
3400 * ret is set only if lock_state is RBD_LOCK_STATE_UNLOCKED
3401 */
3402static enum rbd_lock_state rbd_try_acquire_lock(struct rbd_device *rbd_dev,
3403 int *pret)
3404{
3405 enum rbd_lock_state lock_state;
3406
3407 down_read(&rbd_dev->lock_rwsem);
3408 dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev,
3409 rbd_dev->lock_state);
3410 if (__rbd_is_lock_owner(rbd_dev)) {
3411 lock_state = rbd_dev->lock_state;
3412 up_read(&rbd_dev->lock_rwsem);
3413 return lock_state;
3414 }
3415
3416 up_read(&rbd_dev->lock_rwsem);
3417 down_write(&rbd_dev->lock_rwsem);
3418 dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev,
3419 rbd_dev->lock_state);
3420 if (!__rbd_is_lock_owner(rbd_dev)) {
3421 *pret = rbd_try_lock(rbd_dev);
3422 if (*pret)
3423 rbd_warn(rbd_dev, "failed to acquire lock: %d", *pret);
3424 }
3425
3426 lock_state = rbd_dev->lock_state;
3427 up_write(&rbd_dev->lock_rwsem);
3428 return lock_state;
3429}
3430
3431static void rbd_acquire_lock(struct work_struct *work)
3432{
3433 struct rbd_device *rbd_dev = container_of(to_delayed_work(work),
3434 struct rbd_device, lock_dwork);
3435 enum rbd_lock_state lock_state;
3436 int ret;
3437
3438 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3439again:
3440 lock_state = rbd_try_acquire_lock(rbd_dev, &ret);
3441 if (lock_state != RBD_LOCK_STATE_UNLOCKED || ret == -EBLACKLISTED) {
3442 if (lock_state == RBD_LOCK_STATE_LOCKED)
3443 wake_requests(rbd_dev, true);
3444 dout("%s rbd_dev %p lock_state %d ret %d - done\n", __func__,
3445 rbd_dev, lock_state, ret);
3446 return;
3447 }
3448
3449 ret = rbd_request_lock(rbd_dev);
3450 if (ret == -ETIMEDOUT) {
3451 goto again; /* treat this as a dead client */
e010dd0a
ID
3452 } else if (ret == -EROFS) {
3453 rbd_warn(rbd_dev, "peer will not release lock");
3454 /*
3455 * If this is rbd_add_acquire_lock(), we want to fail
3456 * immediately -- reuse BLACKLISTED flag. Otherwise we
3457 * want to block.
3458 */
3459 if (!(rbd_dev->disk->flags & GENHD_FL_UP)) {
3460 set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags);
3461 /* wake "rbd map --exclusive" process */
3462 wake_requests(rbd_dev, false);
3463 }
ed95b21a
ID
3464 } else if (ret < 0) {
3465 rbd_warn(rbd_dev, "error requesting lock: %d", ret);
3466 mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork,
3467 RBD_RETRY_DELAY);
3468 } else {
3469 /*
3470 * lock owner acked, but resend if we don't see them
3471 * release the lock
3472 */
3473 dout("%s rbd_dev %p requeueing lock_dwork\n", __func__,
3474 rbd_dev);
3475 mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork,
3476 msecs_to_jiffies(2 * RBD_NOTIFY_TIMEOUT * MSEC_PER_SEC));
3477 }
3478}
3479
3480/*
3481 * lock_rwsem must be held for write
3482 */
3483static bool rbd_release_lock(struct rbd_device *rbd_dev)
3484{
3485 dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev,
3486 rbd_dev->lock_state);
3487 if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED)
3488 return false;
3489
3490 rbd_dev->lock_state = RBD_LOCK_STATE_RELEASING;
3491 downgrade_write(&rbd_dev->lock_rwsem);
52bb1f9b 3492 /*
ed95b21a 3493 * Ensure that all in-flight IO is flushed.
52bb1f9b 3494 *
ed95b21a
ID
3495 * FIXME: ceph_osdc_sync() flushes the entire OSD client, which
3496 * may be shared with other devices.
52bb1f9b 3497 */
ed95b21a
ID
3498 ceph_osdc_sync(&rbd_dev->rbd_client->client->osdc);
3499 up_read(&rbd_dev->lock_rwsem);
3500
3501 down_write(&rbd_dev->lock_rwsem);
3502 dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev,
3503 rbd_dev->lock_state);
3504 if (rbd_dev->lock_state != RBD_LOCK_STATE_RELEASING)
3505 return false;
3506
bbead745
ID
3507 rbd_unlock(rbd_dev);
3508 /*
3509 * Give others a chance to grab the lock - we would re-acquire
3510 * almost immediately if we got new IO during ceph_osdc_sync()
3511 * otherwise. We need to ack our own notifications, so this
3512 * lock_dwork will be requeued from rbd_wait_state_locked()
3513 * after wake_requests() in rbd_handle_released_lock().
3514 */
3515 cancel_delayed_work(&rbd_dev->lock_dwork);
ed95b21a
ID
3516 return true;
3517}
3518
3519static void rbd_release_lock_work(struct work_struct *work)
3520{
3521 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
3522 unlock_work);
3523
3524 down_write(&rbd_dev->lock_rwsem);
3525 rbd_release_lock(rbd_dev);
3526 up_write(&rbd_dev->lock_rwsem);
3527}
3528
3529static void rbd_handle_acquired_lock(struct rbd_device *rbd_dev, u8 struct_v,
3530 void **p)
3531{
3532 struct rbd_client_id cid = { 0 };
3533
3534 if (struct_v >= 2) {
3535 cid.gid = ceph_decode_64(p);
3536 cid.handle = ceph_decode_64(p);
3537 }
3538
3539 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
3540 cid.handle);
3541 if (!rbd_cid_equal(&cid, &rbd_empty_cid)) {
3542 down_write(&rbd_dev->lock_rwsem);
3543 if (rbd_cid_equal(&cid, &rbd_dev->owner_cid)) {
3544 /*
3545 * we already know that the remote client is
3546 * the owner
3547 */
3548 up_write(&rbd_dev->lock_rwsem);
3549 return;
3550 }
3551
3552 rbd_set_owner_cid(rbd_dev, &cid);
3553 downgrade_write(&rbd_dev->lock_rwsem);
3554 } else {
3555 down_read(&rbd_dev->lock_rwsem);
3556 }
3557
3558 if (!__rbd_is_lock_owner(rbd_dev))
3559 wake_requests(rbd_dev, false);
3560 up_read(&rbd_dev->lock_rwsem);
3561}
3562
3563static void rbd_handle_released_lock(struct rbd_device *rbd_dev, u8 struct_v,
3564 void **p)
3565{
3566 struct rbd_client_id cid = { 0 };
3567
3568 if (struct_v >= 2) {
3569 cid.gid = ceph_decode_64(p);
3570 cid.handle = ceph_decode_64(p);
3571 }
3572
3573 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
3574 cid.handle);
3575 if (!rbd_cid_equal(&cid, &rbd_empty_cid)) {
3576 down_write(&rbd_dev->lock_rwsem);
3577 if (!rbd_cid_equal(&cid, &rbd_dev->owner_cid)) {
3578 dout("%s rbd_dev %p unexpected owner, cid %llu-%llu != owner_cid %llu-%llu\n",
3579 __func__, rbd_dev, cid.gid, cid.handle,
3580 rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle);
3581 up_write(&rbd_dev->lock_rwsem);
3582 return;
3583 }
3584
3585 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
3586 downgrade_write(&rbd_dev->lock_rwsem);
3587 } else {
3588 down_read(&rbd_dev->lock_rwsem);
3589 }
3590
3591 if (!__rbd_is_lock_owner(rbd_dev))
3592 wake_requests(rbd_dev, false);
3593 up_read(&rbd_dev->lock_rwsem);
3594}
3595
3b77faa0
ID
3596/*
3597 * Returns result for ResponseMessage to be encoded (<= 0), or 1 if no
3598 * ResponseMessage is needed.
3599 */
3600static int rbd_handle_request_lock(struct rbd_device *rbd_dev, u8 struct_v,
3601 void **p)
ed95b21a
ID
3602{
3603 struct rbd_client_id my_cid = rbd_get_cid(rbd_dev);
3604 struct rbd_client_id cid = { 0 };
3b77faa0 3605 int result = 1;
ed95b21a
ID
3606
3607 if (struct_v >= 2) {
3608 cid.gid = ceph_decode_64(p);
3609 cid.handle = ceph_decode_64(p);
3610 }
3611
3612 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
3613 cid.handle);
3614 if (rbd_cid_equal(&cid, &my_cid))
3b77faa0 3615 return result;
ed95b21a
ID
3616
3617 down_read(&rbd_dev->lock_rwsem);
3b77faa0
ID
3618 if (__rbd_is_lock_owner(rbd_dev)) {
3619 if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED &&
3620 rbd_cid_equal(&rbd_dev->owner_cid, &rbd_empty_cid))
3621 goto out_unlock;
3622
3623 /*
3624 * encode ResponseMessage(0) so the peer can detect
3625 * a missing owner
3626 */
3627 result = 0;
3628
3629 if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) {
e010dd0a
ID
3630 if (!rbd_dev->opts->exclusive) {
3631 dout("%s rbd_dev %p queueing unlock_work\n",
3632 __func__, rbd_dev);
3633 queue_work(rbd_dev->task_wq,
3634 &rbd_dev->unlock_work);
3635 } else {
3636 /* refuse to release the lock */
3637 result = -EROFS;
3638 }
ed95b21a
ID
3639 }
3640 }
3b77faa0
ID
3641
3642out_unlock:
ed95b21a 3643 up_read(&rbd_dev->lock_rwsem);
3b77faa0 3644 return result;
ed95b21a
ID
3645}
3646
3647static void __rbd_acknowledge_notify(struct rbd_device *rbd_dev,
3648 u64 notify_id, u64 cookie, s32 *result)
3649{
3650 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3651 int buf_size = 4 + CEPH_ENCODING_START_BLK_LEN;
3652 char buf[buf_size];
3653 int ret;
3654
3655 if (result) {
3656 void *p = buf;
3657
3658 /* encode ResponseMessage */
3659 ceph_start_encoding(&p, 1, 1,
3660 buf_size - CEPH_ENCODING_START_BLK_LEN);
3661 ceph_encode_32(&p, *result);
3662 } else {
3663 buf_size = 0;
3664 }
b8d70035 3665
922dab61
ID
3666 ret = ceph_osdc_notify_ack(osdc, &rbd_dev->header_oid,
3667 &rbd_dev->header_oloc, notify_id, cookie,
ed95b21a 3668 buf, buf_size);
52bb1f9b 3669 if (ret)
ed95b21a
ID
3670 rbd_warn(rbd_dev, "acknowledge_notify failed: %d", ret);
3671}
3672
3673static void rbd_acknowledge_notify(struct rbd_device *rbd_dev, u64 notify_id,
3674 u64 cookie)
3675{
3676 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3677 __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, NULL);
3678}
3679
3680static void rbd_acknowledge_notify_result(struct rbd_device *rbd_dev,
3681 u64 notify_id, u64 cookie, s32 result)
3682{
3683 dout("%s rbd_dev %p result %d\n", __func__, rbd_dev, result);
3684 __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, &result);
3685}
3686
3687static void rbd_watch_cb(void *arg, u64 notify_id, u64 cookie,
3688 u64 notifier_id, void *data, size_t data_len)
3689{
3690 struct rbd_device *rbd_dev = arg;
3691 void *p = data;
3692 void *const end = p + data_len;
d4c2269b 3693 u8 struct_v = 0;
ed95b21a
ID
3694 u32 len;
3695 u32 notify_op;
3696 int ret;
3697
3698 dout("%s rbd_dev %p cookie %llu notify_id %llu data_len %zu\n",
3699 __func__, rbd_dev, cookie, notify_id, data_len);
3700 if (data_len) {
3701 ret = ceph_start_decoding(&p, end, 1, "NotifyMessage",
3702 &struct_v, &len);
3703 if (ret) {
3704 rbd_warn(rbd_dev, "failed to decode NotifyMessage: %d",
3705 ret);
3706 return;
3707 }
3708
3709 notify_op = ceph_decode_32(&p);
3710 } else {
3711 /* legacy notification for header updates */
3712 notify_op = RBD_NOTIFY_OP_HEADER_UPDATE;
3713 len = 0;
3714 }
3715
3716 dout("%s rbd_dev %p notify_op %u\n", __func__, rbd_dev, notify_op);
3717 switch (notify_op) {
3718 case RBD_NOTIFY_OP_ACQUIRED_LOCK:
3719 rbd_handle_acquired_lock(rbd_dev, struct_v, &p);
3720 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3721 break;
3722 case RBD_NOTIFY_OP_RELEASED_LOCK:
3723 rbd_handle_released_lock(rbd_dev, struct_v, &p);
3724 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3725 break;
3726 case RBD_NOTIFY_OP_REQUEST_LOCK:
3b77faa0
ID
3727 ret = rbd_handle_request_lock(rbd_dev, struct_v, &p);
3728 if (ret <= 0)
ed95b21a 3729 rbd_acknowledge_notify_result(rbd_dev, notify_id,
3b77faa0 3730 cookie, ret);
ed95b21a
ID
3731 else
3732 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3733 break;
3734 case RBD_NOTIFY_OP_HEADER_UPDATE:
3735 ret = rbd_dev_refresh(rbd_dev);
3736 if (ret)
3737 rbd_warn(rbd_dev, "refresh failed: %d", ret);
3738
3739 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3740 break;
3741 default:
3742 if (rbd_is_lock_owner(rbd_dev))
3743 rbd_acknowledge_notify_result(rbd_dev, notify_id,
3744 cookie, -EOPNOTSUPP);
3745 else
3746 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3747 break;
3748 }
b8d70035
AE
3749}
3750
99d16943
ID
3751static void __rbd_unregister_watch(struct rbd_device *rbd_dev);
3752
922dab61 3753static void rbd_watch_errcb(void *arg, u64 cookie, int err)
bb040aa0 3754{
922dab61 3755 struct rbd_device *rbd_dev = arg;
bb040aa0 3756
922dab61 3757 rbd_warn(rbd_dev, "encountered watch error: %d", err);
bb040aa0 3758
ed95b21a
ID
3759 down_write(&rbd_dev->lock_rwsem);
3760 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
3761 up_write(&rbd_dev->lock_rwsem);
3762
99d16943
ID
3763 mutex_lock(&rbd_dev->watch_mutex);
3764 if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) {
3765 __rbd_unregister_watch(rbd_dev);
3766 rbd_dev->watch_state = RBD_WATCH_STATE_ERROR;
bb040aa0 3767
99d16943 3768 queue_delayed_work(rbd_dev->task_wq, &rbd_dev->watch_dwork, 0);
bb040aa0 3769 }
99d16943 3770 mutex_unlock(&rbd_dev->watch_mutex);
bb040aa0
ID
3771}
3772
9969ebc5 3773/*
99d16943 3774 * watch_mutex must be locked
9969ebc5 3775 */
99d16943 3776static int __rbd_register_watch(struct rbd_device *rbd_dev)
9969ebc5
AE
3777{
3778 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
922dab61 3779 struct ceph_osd_linger_request *handle;
9969ebc5 3780
922dab61 3781 rbd_assert(!rbd_dev->watch_handle);
99d16943 3782 dout("%s rbd_dev %p\n", __func__, rbd_dev);
9969ebc5 3783
922dab61
ID
3784 handle = ceph_osdc_watch(osdc, &rbd_dev->header_oid,
3785 &rbd_dev->header_oloc, rbd_watch_cb,
3786 rbd_watch_errcb, rbd_dev);
3787 if (IS_ERR(handle))
3788 return PTR_ERR(handle);
8eb87565 3789
922dab61 3790 rbd_dev->watch_handle = handle;
b30a01f2 3791 return 0;
b30a01f2
ID
3792}
3793
99d16943
ID
3794/*
3795 * watch_mutex must be locked
3796 */
3797static void __rbd_unregister_watch(struct rbd_device *rbd_dev)
b30a01f2 3798{
922dab61
ID
3799 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3800 int ret;
b30a01f2 3801
99d16943
ID
3802 rbd_assert(rbd_dev->watch_handle);
3803 dout("%s rbd_dev %p\n", __func__, rbd_dev);
b30a01f2 3804
922dab61
ID
3805 ret = ceph_osdc_unwatch(osdc, rbd_dev->watch_handle);
3806 if (ret)
3807 rbd_warn(rbd_dev, "failed to unwatch: %d", ret);
76756a51 3808
922dab61 3809 rbd_dev->watch_handle = NULL;
c525f036
ID
3810}
3811
99d16943
ID
3812static int rbd_register_watch(struct rbd_device *rbd_dev)
3813{
3814 int ret;
3815
3816 mutex_lock(&rbd_dev->watch_mutex);
3817 rbd_assert(rbd_dev->watch_state == RBD_WATCH_STATE_UNREGISTERED);
3818 ret = __rbd_register_watch(rbd_dev);
3819 if (ret)
3820 goto out;
3821
3822 rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED;
3823 rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id;
3824
3825out:
3826 mutex_unlock(&rbd_dev->watch_mutex);
3827 return ret;
3828}
3829
3830static void cancel_tasks_sync(struct rbd_device *rbd_dev)
c525f036 3831{
99d16943
ID
3832 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3833
3834 cancel_delayed_work_sync(&rbd_dev->watch_dwork);
ed95b21a
ID
3835 cancel_work_sync(&rbd_dev->acquired_lock_work);
3836 cancel_work_sync(&rbd_dev->released_lock_work);
3837 cancel_delayed_work_sync(&rbd_dev->lock_dwork);
3838 cancel_work_sync(&rbd_dev->unlock_work);
99d16943
ID
3839}
3840
3841static void rbd_unregister_watch(struct rbd_device *rbd_dev)
3842{
ed95b21a 3843 WARN_ON(waitqueue_active(&rbd_dev->lock_waitq));
99d16943
ID
3844 cancel_tasks_sync(rbd_dev);
3845
3846 mutex_lock(&rbd_dev->watch_mutex);
3847 if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED)
3848 __rbd_unregister_watch(rbd_dev);
3849 rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED;
3850 mutex_unlock(&rbd_dev->watch_mutex);
811c6688 3851
811c6688 3852 ceph_osdc_flush_notifies(&rbd_dev->rbd_client->client->osdc);
fca27065
ID
3853}
3854
14bb211d
ID
3855/*
3856 * lock_rwsem must be held for write
3857 */
3858static void rbd_reacquire_lock(struct rbd_device *rbd_dev)
3859{
3860 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3861 char cookie[32];
3862 int ret;
3863
3864 WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED);
3865
3866 format_lock_cookie(rbd_dev, cookie);
3867 ret = ceph_cls_set_cookie(osdc, &rbd_dev->header_oid,
3868 &rbd_dev->header_oloc, RBD_LOCK_NAME,
3869 CEPH_CLS_LOCK_EXCLUSIVE, rbd_dev->lock_cookie,
3870 RBD_LOCK_TAG, cookie);
3871 if (ret) {
3872 if (ret != -EOPNOTSUPP)
3873 rbd_warn(rbd_dev, "failed to update lock cookie: %d",
3874 ret);
3875
3876 /*
3877 * Lock cookie cannot be updated on older OSDs, so do
3878 * a manual release and queue an acquire.
3879 */
3880 if (rbd_release_lock(rbd_dev))
3881 queue_delayed_work(rbd_dev->task_wq,
3882 &rbd_dev->lock_dwork, 0);
3883 } else {
3884 strcpy(rbd_dev->lock_cookie, cookie);
3885 }
3886}
3887
99d16943
ID
3888static void rbd_reregister_watch(struct work_struct *work)
3889{
3890 struct rbd_device *rbd_dev = container_of(to_delayed_work(work),
3891 struct rbd_device, watch_dwork);
3892 int ret;
3893
3894 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3895
3896 mutex_lock(&rbd_dev->watch_mutex);
87c0fded
ID
3897 if (rbd_dev->watch_state != RBD_WATCH_STATE_ERROR) {
3898 mutex_unlock(&rbd_dev->watch_mutex);
14bb211d 3899 return;
87c0fded 3900 }
99d16943
ID
3901
3902 ret = __rbd_register_watch(rbd_dev);
3903 if (ret) {
3904 rbd_warn(rbd_dev, "failed to reregister watch: %d", ret);
4d73644b 3905 if (ret == -EBLACKLISTED || ret == -ENOENT) {
87c0fded 3906 set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags);
14bb211d 3907 wake_requests(rbd_dev, true);
87c0fded 3908 } else {
99d16943
ID
3909 queue_delayed_work(rbd_dev->task_wq,
3910 &rbd_dev->watch_dwork,
3911 RBD_RETRY_DELAY);
87c0fded
ID
3912 }
3913 mutex_unlock(&rbd_dev->watch_mutex);
14bb211d 3914 return;
99d16943
ID
3915 }
3916
3917 rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED;
3918 rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id;
3919 mutex_unlock(&rbd_dev->watch_mutex);
3920
14bb211d
ID
3921 down_write(&rbd_dev->lock_rwsem);
3922 if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED)
3923 rbd_reacquire_lock(rbd_dev);
3924 up_write(&rbd_dev->lock_rwsem);
3925
99d16943
ID
3926 ret = rbd_dev_refresh(rbd_dev);
3927 if (ret)
3928 rbd_warn(rbd_dev, "reregisteration refresh failed: %d", ret);
99d16943
ID
3929}
3930
36be9a76 3931/*
f40eb349
AE
3932 * Synchronous osd object method call. Returns the number of bytes
3933 * returned in the outbound buffer, or a negative error code.
36be9a76
AE
3934 */
3935static int rbd_obj_method_sync(struct rbd_device *rbd_dev,
ecd4a68a
ID
3936 struct ceph_object_id *oid,
3937 struct ceph_object_locator *oloc,
36be9a76 3938 const char *method_name,
4157976b 3939 const void *outbound,
36be9a76 3940 size_t outbound_size,
4157976b 3941 void *inbound,
e2a58ee5 3942 size_t inbound_size)
36be9a76 3943{
ecd4a68a
ID
3944 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3945 struct page *req_page = NULL;
3946 struct page *reply_page;
36be9a76
AE
3947 int ret;
3948
3949 /*
6010a451
AE
3950 * Method calls are ultimately read operations. The result
3951 * should placed into the inbound buffer provided. They
3952 * also supply outbound data--parameters for the object
3953 * method. Currently if this is present it will be a
3954 * snapshot id.
36be9a76 3955 */
ecd4a68a
ID
3956 if (outbound) {
3957 if (outbound_size > PAGE_SIZE)
3958 return -E2BIG;
36be9a76 3959
ecd4a68a
ID
3960 req_page = alloc_page(GFP_KERNEL);
3961 if (!req_page)
3962 return -ENOMEM;
04017e29 3963
ecd4a68a 3964 memcpy(page_address(req_page), outbound, outbound_size);
04017e29 3965 }
36be9a76 3966
ecd4a68a
ID
3967 reply_page = alloc_page(GFP_KERNEL);
3968 if (!reply_page) {
3969 if (req_page)
3970 __free_page(req_page);
3971 return -ENOMEM;
3972 }
57385b51 3973
ecd4a68a
ID
3974 ret = ceph_osdc_call(osdc, oid, oloc, RBD_DRV_NAME, method_name,
3975 CEPH_OSD_FLAG_READ, req_page, outbound_size,
3976 reply_page, &inbound_size);
3977 if (!ret) {
3978 memcpy(inbound, page_address(reply_page), inbound_size);
3979 ret = inbound_size;
3980 }
36be9a76 3981
ecd4a68a
ID
3982 if (req_page)
3983 __free_page(req_page);
3984 __free_page(reply_page);
36be9a76
AE
3985 return ret;
3986}
3987
ed95b21a
ID
3988/*
3989 * lock_rwsem must be held for read
3990 */
3991static void rbd_wait_state_locked(struct rbd_device *rbd_dev)
3992{
3993 DEFINE_WAIT(wait);
3994
3995 do {
3996 /*
3997 * Note the use of mod_delayed_work() in rbd_acquire_lock()
3998 * and cancel_delayed_work() in wake_requests().
3999 */
4000 dout("%s rbd_dev %p queueing lock_dwork\n", __func__, rbd_dev);
4001 queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0);
4002 prepare_to_wait_exclusive(&rbd_dev->lock_waitq, &wait,
4003 TASK_UNINTERRUPTIBLE);
4004 up_read(&rbd_dev->lock_rwsem);
4005 schedule();
4006 down_read(&rbd_dev->lock_rwsem);
87c0fded
ID
4007 } while (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED &&
4008 !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags));
4009
ed95b21a
ID
4010 finish_wait(&rbd_dev->lock_waitq, &wait);
4011}
4012
7ad18afa 4013static void rbd_queue_workfn(struct work_struct *work)
bf0d5f50 4014{
7ad18afa
CH
4015 struct request *rq = blk_mq_rq_from_pdu(work);
4016 struct rbd_device *rbd_dev = rq->q->queuedata;
bc1ecc65 4017 struct rbd_img_request *img_request;
4e752f0a 4018 struct ceph_snap_context *snapc = NULL;
bc1ecc65
ID
4019 u64 offset = (u64)blk_rq_pos(rq) << SECTOR_SHIFT;
4020 u64 length = blk_rq_bytes(rq);
6d2940c8 4021 enum obj_operation_type op_type;
4e752f0a 4022 u64 mapping_size;
80de1912 4023 bool must_be_locked;
bf0d5f50
AE
4024 int result;
4025
aebf526b
CH
4026 switch (req_op(rq)) {
4027 case REQ_OP_DISCARD:
6ac56951 4028 case REQ_OP_WRITE_ZEROES:
90e98c52 4029 op_type = OBJ_OP_DISCARD;
aebf526b
CH
4030 break;
4031 case REQ_OP_WRITE:
6d2940c8 4032 op_type = OBJ_OP_WRITE;
aebf526b
CH
4033 break;
4034 case REQ_OP_READ:
6d2940c8 4035 op_type = OBJ_OP_READ;
aebf526b
CH
4036 break;
4037 default:
4038 dout("%s: non-fs request type %d\n", __func__, req_op(rq));
4039 result = -EIO;
4040 goto err;
4041 }
6d2940c8 4042
bc1ecc65 4043 /* Ignore/skip any zero-length requests */
bf0d5f50 4044
bc1ecc65
ID
4045 if (!length) {
4046 dout("%s: zero-length request\n", __func__);
4047 result = 0;
4048 goto err_rq;
4049 }
bf0d5f50 4050
6d2940c8 4051 /* Only reads are allowed to a read-only device */
bc1ecc65 4052
6d2940c8 4053 if (op_type != OBJ_OP_READ) {
bc1ecc65
ID
4054 if (rbd_dev->mapping.read_only) {
4055 result = -EROFS;
4056 goto err_rq;
4dda41d3 4057 }
bc1ecc65
ID
4058 rbd_assert(rbd_dev->spec->snap_id == CEPH_NOSNAP);
4059 }
4dda41d3 4060
bc1ecc65
ID
4061 /*
4062 * Quit early if the mapped snapshot no longer exists. It's
4063 * still possible the snapshot will have disappeared by the
4064 * time our request arrives at the osd, but there's no sense in
4065 * sending it if we already know.
4066 */
4067 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) {
4068 dout("request for non-existent snapshot");
4069 rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP);
4070 result = -ENXIO;
4071 goto err_rq;
4072 }
4dda41d3 4073
bc1ecc65
ID
4074 if (offset && length > U64_MAX - offset + 1) {
4075 rbd_warn(rbd_dev, "bad request range (%llu~%llu)", offset,
4076 length);
4077 result = -EINVAL;
4078 goto err_rq; /* Shouldn't happen */
4079 }
4dda41d3 4080
7ad18afa
CH
4081 blk_mq_start_request(rq);
4082
4e752f0a
JD
4083 down_read(&rbd_dev->header_rwsem);
4084 mapping_size = rbd_dev->mapping.size;
6d2940c8 4085 if (op_type != OBJ_OP_READ) {
4e752f0a
JD
4086 snapc = rbd_dev->header.snapc;
4087 ceph_get_snap_context(snapc);
4088 }
4089 up_read(&rbd_dev->header_rwsem);
4090
4091 if (offset + length > mapping_size) {
bc1ecc65 4092 rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)", offset,
4e752f0a 4093 length, mapping_size);
bc1ecc65
ID
4094 result = -EIO;
4095 goto err_rq;
4096 }
bf0d5f50 4097
f9bebd58
ID
4098 must_be_locked =
4099 (rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK) &&
4100 (op_type != OBJ_OP_READ || rbd_dev->opts->lock_on_read);
ed95b21a
ID
4101 if (must_be_locked) {
4102 down_read(&rbd_dev->lock_rwsem);
87c0fded 4103 if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED &&
e010dd0a
ID
4104 !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) {
4105 if (rbd_dev->opts->exclusive) {
4106 rbd_warn(rbd_dev, "exclusive lock required");
4107 result = -EROFS;
4108 goto err_unlock;
4109 }
ed95b21a 4110 rbd_wait_state_locked(rbd_dev);
e010dd0a 4111 }
87c0fded
ID
4112 if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) {
4113 result = -EBLACKLISTED;
4114 goto err_unlock;
4115 }
ed95b21a
ID
4116 }
4117
6d2940c8 4118 img_request = rbd_img_request_create(rbd_dev, offset, length, op_type,
4e752f0a 4119 snapc);
bc1ecc65
ID
4120 if (!img_request) {
4121 result = -ENOMEM;
ed95b21a 4122 goto err_unlock;
bc1ecc65
ID
4123 }
4124 img_request->rq = rq;
70b16db8 4125 snapc = NULL; /* img_request consumes a ref */
bf0d5f50 4126
90e98c52
GZ
4127 if (op_type == OBJ_OP_DISCARD)
4128 result = rbd_img_request_fill(img_request, OBJ_REQUEST_NODATA,
4129 NULL);
4130 else
4131 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
4132 rq->bio);
bc1ecc65
ID
4133 if (result)
4134 goto err_img_request;
bf0d5f50 4135
bc1ecc65
ID
4136 result = rbd_img_request_submit(img_request);
4137 if (result)
4138 goto err_img_request;
bf0d5f50 4139
ed95b21a
ID
4140 if (must_be_locked)
4141 up_read(&rbd_dev->lock_rwsem);
bc1ecc65 4142 return;
bf0d5f50 4143
bc1ecc65
ID
4144err_img_request:
4145 rbd_img_request_put(img_request);
ed95b21a
ID
4146err_unlock:
4147 if (must_be_locked)
4148 up_read(&rbd_dev->lock_rwsem);
bc1ecc65
ID
4149err_rq:
4150 if (result)
4151 rbd_warn(rbd_dev, "%s %llx at %llx result %d",
6d2940c8 4152 obj_op_name(op_type), length, offset, result);
e96a650a 4153 ceph_put_snap_context(snapc);
7ad18afa 4154err:
2a842aca 4155 blk_mq_end_request(rq, errno_to_blk_status(result));
bc1ecc65 4156}
bf0d5f50 4157
fc17b653 4158static blk_status_t rbd_queue_rq(struct blk_mq_hw_ctx *hctx,
7ad18afa 4159 const struct blk_mq_queue_data *bd)
bc1ecc65 4160{
7ad18afa
CH
4161 struct request *rq = bd->rq;
4162 struct work_struct *work = blk_mq_rq_to_pdu(rq);
bf0d5f50 4163
7ad18afa 4164 queue_work(rbd_wq, work);
fc17b653 4165 return BLK_STS_OK;
bf0d5f50
AE
4166}
4167
602adf40
YS
4168static void rbd_free_disk(struct rbd_device *rbd_dev)
4169{
5769ed0c
ID
4170 blk_cleanup_queue(rbd_dev->disk->queue);
4171 blk_mq_free_tag_set(&rbd_dev->tag_set);
4172 put_disk(rbd_dev->disk);
a0cab924 4173 rbd_dev->disk = NULL;
602adf40
YS
4174}
4175
788e2df3 4176static int rbd_obj_read_sync(struct rbd_device *rbd_dev,
fe5478e0
ID
4177 struct ceph_object_id *oid,
4178 struct ceph_object_locator *oloc,
4179 void *buf, int buf_len)
788e2df3
AE
4180
4181{
fe5478e0
ID
4182 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
4183 struct ceph_osd_request *req;
4184 struct page **pages;
4185 int num_pages = calc_pages_for(0, buf_len);
788e2df3
AE
4186 int ret;
4187
fe5478e0
ID
4188 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL);
4189 if (!req)
4190 return -ENOMEM;
788e2df3 4191
fe5478e0
ID
4192 ceph_oid_copy(&req->r_base_oid, oid);
4193 ceph_oloc_copy(&req->r_base_oloc, oloc);
4194 req->r_flags = CEPH_OSD_FLAG_READ;
430c28c3 4195
fe5478e0 4196 ret = ceph_osdc_alloc_messages(req, GFP_KERNEL);
788e2df3 4197 if (ret)
fe5478e0 4198 goto out_req;
788e2df3 4199
fe5478e0
ID
4200 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
4201 if (IS_ERR(pages)) {
4202 ret = PTR_ERR(pages);
4203 goto out_req;
4204 }
1ceae7ef 4205
fe5478e0
ID
4206 osd_req_op_extent_init(req, 0, CEPH_OSD_OP_READ, 0, buf_len, 0, 0);
4207 osd_req_op_extent_osd_data_pages(req, 0, pages, buf_len, 0, false,
4208 true);
4209
4210 ceph_osdc_start_request(osdc, req, false);
4211 ret = ceph_osdc_wait_request(osdc, req);
4212 if (ret >= 0)
4213 ceph_copy_from_page_vector(pages, buf, 0, ret);
788e2df3 4214
fe5478e0
ID
4215out_req:
4216 ceph_osdc_put_request(req);
788e2df3
AE
4217 return ret;
4218}
4219
602adf40 4220/*
662518b1
AE
4221 * Read the complete header for the given rbd device. On successful
4222 * return, the rbd_dev->header field will contain up-to-date
4223 * information about the image.
602adf40 4224 */
99a41ebc 4225static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev)
602adf40 4226{
4156d998 4227 struct rbd_image_header_ondisk *ondisk = NULL;
50f7c4c9 4228 u32 snap_count = 0;
4156d998
AE
4229 u64 names_size = 0;
4230 u32 want_count;
4231 int ret;
602adf40 4232
00f1f36f 4233 /*
4156d998
AE
4234 * The complete header will include an array of its 64-bit
4235 * snapshot ids, followed by the names of those snapshots as
4236 * a contiguous block of NUL-terminated strings. Note that
4237 * the number of snapshots could change by the time we read
4238 * it in, in which case we re-read it.
00f1f36f 4239 */
4156d998
AE
4240 do {
4241 size_t size;
4242
4243 kfree(ondisk);
4244
4245 size = sizeof (*ondisk);
4246 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
4247 size += names_size;
4248 ondisk = kmalloc(size, GFP_KERNEL);
4249 if (!ondisk)
662518b1 4250 return -ENOMEM;
4156d998 4251
fe5478e0
ID
4252 ret = rbd_obj_read_sync(rbd_dev, &rbd_dev->header_oid,
4253 &rbd_dev->header_oloc, ondisk, size);
4156d998 4254 if (ret < 0)
662518b1 4255 goto out;
c0cd10db 4256 if ((size_t)ret < size) {
4156d998 4257 ret = -ENXIO;
06ecc6cb
AE
4258 rbd_warn(rbd_dev, "short header read (want %zd got %d)",
4259 size, ret);
662518b1 4260 goto out;
4156d998
AE
4261 }
4262 if (!rbd_dev_ondisk_valid(ondisk)) {
4263 ret = -ENXIO;
06ecc6cb 4264 rbd_warn(rbd_dev, "invalid header");
662518b1 4265 goto out;
81e759fb 4266 }
602adf40 4267
4156d998
AE
4268 names_size = le64_to_cpu(ondisk->snap_names_len);
4269 want_count = snap_count;
4270 snap_count = le32_to_cpu(ondisk->snap_count);
4271 } while (snap_count != want_count);
00f1f36f 4272
662518b1
AE
4273 ret = rbd_header_from_disk(rbd_dev, ondisk);
4274out:
4156d998
AE
4275 kfree(ondisk);
4276
4277 return ret;
602adf40
YS
4278}
4279
15228ede
AE
4280/*
4281 * Clear the rbd device's EXISTS flag if the snapshot it's mapped to
4282 * has disappeared from the (just updated) snapshot context.
4283 */
4284static void rbd_exists_validate(struct rbd_device *rbd_dev)
4285{
4286 u64 snap_id;
4287
4288 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags))
4289 return;
4290
4291 snap_id = rbd_dev->spec->snap_id;
4292 if (snap_id == CEPH_NOSNAP)
4293 return;
4294
4295 if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX)
4296 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
4297}
4298
9875201e
JD
4299static void rbd_dev_update_size(struct rbd_device *rbd_dev)
4300{
4301 sector_t size;
9875201e
JD
4302
4303 /*
811c6688
ID
4304 * If EXISTS is not set, rbd_dev->disk may be NULL, so don't
4305 * try to update its size. If REMOVING is set, updating size
4306 * is just useless work since the device can't be opened.
9875201e 4307 */
811c6688
ID
4308 if (test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags) &&
4309 !test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) {
9875201e
JD
4310 size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE;
4311 dout("setting size to %llu sectors", (unsigned long long)size);
4312 set_capacity(rbd_dev->disk, size);
4313 revalidate_disk(rbd_dev->disk);
4314 }
4315}
4316
cc4a38bd 4317static int rbd_dev_refresh(struct rbd_device *rbd_dev)
1fe5e993 4318{
e627db08 4319 u64 mapping_size;
1fe5e993
AE
4320 int ret;
4321
cfbf6377 4322 down_write(&rbd_dev->header_rwsem);
3b5cf2a2 4323 mapping_size = rbd_dev->mapping.size;
a720ae09
ID
4324
4325 ret = rbd_dev_header_info(rbd_dev);
52bb1f9b 4326 if (ret)
73e39e4d 4327 goto out;
15228ede 4328
e8f59b59
ID
4329 /*
4330 * If there is a parent, see if it has disappeared due to the
4331 * mapped image getting flattened.
4332 */
4333 if (rbd_dev->parent) {
4334 ret = rbd_dev_v2_parent_info(rbd_dev);
4335 if (ret)
73e39e4d 4336 goto out;
e8f59b59
ID
4337 }
4338
5ff1108c 4339 if (rbd_dev->spec->snap_id == CEPH_NOSNAP) {
73e39e4d 4340 rbd_dev->mapping.size = rbd_dev->header.image_size;
5ff1108c
ID
4341 } else {
4342 /* validate mapped snapshot's EXISTS flag */
4343 rbd_exists_validate(rbd_dev);
4344 }
15228ede 4345
73e39e4d 4346out:
cfbf6377 4347 up_write(&rbd_dev->header_rwsem);
73e39e4d 4348 if (!ret && mapping_size != rbd_dev->mapping.size)
9875201e 4349 rbd_dev_update_size(rbd_dev);
1fe5e993 4350
73e39e4d 4351 return ret;
1fe5e993
AE
4352}
4353
d6296d39
CH
4354static int rbd_init_request(struct blk_mq_tag_set *set, struct request *rq,
4355 unsigned int hctx_idx, unsigned int numa_node)
7ad18afa
CH
4356{
4357 struct work_struct *work = blk_mq_rq_to_pdu(rq);
4358
4359 INIT_WORK(work, rbd_queue_workfn);
4360 return 0;
4361}
4362
f363b089 4363static const struct blk_mq_ops rbd_mq_ops = {
7ad18afa 4364 .queue_rq = rbd_queue_rq,
7ad18afa
CH
4365 .init_request = rbd_init_request,
4366};
4367
602adf40
YS
4368static int rbd_init_disk(struct rbd_device *rbd_dev)
4369{
4370 struct gendisk *disk;
4371 struct request_queue *q;
593a9e7b 4372 u64 segment_size;
7ad18afa 4373 int err;
602adf40 4374
602adf40 4375 /* create gendisk info */
7e513d43
ID
4376 disk = alloc_disk(single_major ?
4377 (1 << RBD_SINGLE_MAJOR_PART_SHIFT) :
4378 RBD_MINORS_PER_MAJOR);
602adf40 4379 if (!disk)
1fcdb8aa 4380 return -ENOMEM;
602adf40 4381
f0f8cef5 4382 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
de71a297 4383 rbd_dev->dev_id);
602adf40 4384 disk->major = rbd_dev->major;
dd82fff1 4385 disk->first_minor = rbd_dev->minor;
7e513d43
ID
4386 if (single_major)
4387 disk->flags |= GENHD_FL_EXT_DEVT;
602adf40
YS
4388 disk->fops = &rbd_bd_ops;
4389 disk->private_data = rbd_dev;
4390
7ad18afa
CH
4391 memset(&rbd_dev->tag_set, 0, sizeof(rbd_dev->tag_set));
4392 rbd_dev->tag_set.ops = &rbd_mq_ops;
b5584180 4393 rbd_dev->tag_set.queue_depth = rbd_dev->opts->queue_depth;
7ad18afa 4394 rbd_dev->tag_set.numa_node = NUMA_NO_NODE;
b5584180 4395 rbd_dev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
7ad18afa
CH
4396 rbd_dev->tag_set.nr_hw_queues = 1;
4397 rbd_dev->tag_set.cmd_size = sizeof(struct work_struct);
4398
4399 err = blk_mq_alloc_tag_set(&rbd_dev->tag_set);
4400 if (err)
602adf40 4401 goto out_disk;
029bcbd8 4402
7ad18afa
CH
4403 q = blk_mq_init_queue(&rbd_dev->tag_set);
4404 if (IS_ERR(q)) {
4405 err = PTR_ERR(q);
4406 goto out_tag_set;
4407 }
4408
d8a2c89c
ID
4409 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
4410 /* QUEUE_FLAG_ADD_RANDOM is off by default for blk-mq */
593a9e7b 4411
029bcbd8 4412 /* set io sizes to object size */
593a9e7b
AE
4413 segment_size = rbd_obj_bytes(&rbd_dev->header);
4414 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
0d9fde4f 4415 q->limits.max_sectors = queue_max_hw_sectors(q);
d3834fef 4416 blk_queue_max_segments(q, segment_size / SECTOR_SIZE);
593a9e7b
AE
4417 blk_queue_max_segment_size(q, segment_size);
4418 blk_queue_io_min(q, segment_size);
4419 blk_queue_io_opt(q, segment_size);
029bcbd8 4420
90e98c52
GZ
4421 /* enable the discard support */
4422 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
4423 q->limits.discard_granularity = segment_size;
4424 q->limits.discard_alignment = segment_size;
2bb4cd5c 4425 blk_queue_max_discard_sectors(q, segment_size / SECTOR_SIZE);
6ac56951 4426 blk_queue_max_write_zeroes_sectors(q, segment_size / SECTOR_SIZE);
90e98c52 4427
bae818ee 4428 if (!ceph_test_opt(rbd_dev->rbd_client->client, NOCRC))
dc3b17cc 4429 q->backing_dev_info->capabilities |= BDI_CAP_STABLE_WRITES;
bae818ee 4430
5769ed0c
ID
4431 /*
4432 * disk_release() expects a queue ref from add_disk() and will
4433 * put it. Hold an extra ref until add_disk() is called.
4434 */
4435 WARN_ON(!blk_get_queue(q));
602adf40 4436 disk->queue = q;
602adf40
YS
4437 q->queuedata = rbd_dev;
4438
4439 rbd_dev->disk = disk;
602adf40 4440
602adf40 4441 return 0;
7ad18afa
CH
4442out_tag_set:
4443 blk_mq_free_tag_set(&rbd_dev->tag_set);
602adf40
YS
4444out_disk:
4445 put_disk(disk);
7ad18afa 4446 return err;
602adf40
YS
4447}
4448
dfc5606d
YS
4449/*
4450 sysfs
4451*/
4452
593a9e7b
AE
4453static struct rbd_device *dev_to_rbd_dev(struct device *dev)
4454{
4455 return container_of(dev, struct rbd_device, dev);
4456}
4457
dfc5606d
YS
4458static ssize_t rbd_size_show(struct device *dev,
4459 struct device_attribute *attr, char *buf)
4460{
593a9e7b 4461 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
a51aa0c0 4462
fc71d833
AE
4463 return sprintf(buf, "%llu\n",
4464 (unsigned long long)rbd_dev->mapping.size);
dfc5606d
YS
4465}
4466
34b13184
AE
4467/*
4468 * Note this shows the features for whatever's mapped, which is not
4469 * necessarily the base image.
4470 */
4471static ssize_t rbd_features_show(struct device *dev,
4472 struct device_attribute *attr, char *buf)
4473{
4474 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4475
4476 return sprintf(buf, "0x%016llx\n",
fc71d833 4477 (unsigned long long)rbd_dev->mapping.features);
34b13184
AE
4478}
4479
dfc5606d
YS
4480static ssize_t rbd_major_show(struct device *dev,
4481 struct device_attribute *attr, char *buf)
4482{
593a9e7b 4483 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
602adf40 4484
fc71d833
AE
4485 if (rbd_dev->major)
4486 return sprintf(buf, "%d\n", rbd_dev->major);
4487
4488 return sprintf(buf, "(none)\n");
dd82fff1
ID
4489}
4490
4491static ssize_t rbd_minor_show(struct device *dev,
4492 struct device_attribute *attr, char *buf)
4493{
4494 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
fc71d833 4495
dd82fff1 4496 return sprintf(buf, "%d\n", rbd_dev->minor);
dfc5606d
YS
4497}
4498
005a07bf
ID
4499static ssize_t rbd_client_addr_show(struct device *dev,
4500 struct device_attribute *attr, char *buf)
4501{
4502 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4503 struct ceph_entity_addr *client_addr =
4504 ceph_client_addr(rbd_dev->rbd_client->client);
4505
4506 return sprintf(buf, "%pISpc/%u\n", &client_addr->in_addr,
4507 le32_to_cpu(client_addr->nonce));
4508}
4509
dfc5606d
YS
4510static ssize_t rbd_client_id_show(struct device *dev,
4511 struct device_attribute *attr, char *buf)
602adf40 4512{
593a9e7b 4513 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
dfc5606d 4514
1dbb4399 4515 return sprintf(buf, "client%lld\n",
033268a5 4516 ceph_client_gid(rbd_dev->rbd_client->client));
602adf40
YS
4517}
4518
267fb90b
MC
4519static ssize_t rbd_cluster_fsid_show(struct device *dev,
4520 struct device_attribute *attr, char *buf)
4521{
4522 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4523
4524 return sprintf(buf, "%pU\n", &rbd_dev->rbd_client->client->fsid);
4525}
4526
0d6d1e9c
MC
4527static ssize_t rbd_config_info_show(struct device *dev,
4528 struct device_attribute *attr, char *buf)
4529{
4530 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4531
4532 return sprintf(buf, "%s\n", rbd_dev->config_info);
602adf40
YS
4533}
4534
dfc5606d
YS
4535static ssize_t rbd_pool_show(struct device *dev,
4536 struct device_attribute *attr, char *buf)
602adf40 4537{
593a9e7b 4538 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
dfc5606d 4539
0d7dbfce 4540 return sprintf(buf, "%s\n", rbd_dev->spec->pool_name);
dfc5606d
YS
4541}
4542
9bb2f334
AE
4543static ssize_t rbd_pool_id_show(struct device *dev,
4544 struct device_attribute *attr, char *buf)
4545{
4546 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4547
0d7dbfce 4548 return sprintf(buf, "%llu\n",
fc71d833 4549 (unsigned long long) rbd_dev->spec->pool_id);
9bb2f334
AE
4550}
4551
dfc5606d
YS
4552static ssize_t rbd_name_show(struct device *dev,
4553 struct device_attribute *attr, char *buf)
4554{
593a9e7b 4555 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
dfc5606d 4556
a92ffdf8
AE
4557 if (rbd_dev->spec->image_name)
4558 return sprintf(buf, "%s\n", rbd_dev->spec->image_name);
4559
4560 return sprintf(buf, "(unknown)\n");
dfc5606d
YS
4561}
4562
589d30e0
AE
4563static ssize_t rbd_image_id_show(struct device *dev,
4564 struct device_attribute *attr, char *buf)
4565{
4566 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4567
0d7dbfce 4568 return sprintf(buf, "%s\n", rbd_dev->spec->image_id);
589d30e0
AE
4569}
4570
34b13184
AE
4571/*
4572 * Shows the name of the currently-mapped snapshot (or
4573 * RBD_SNAP_HEAD_NAME for the base image).
4574 */
dfc5606d
YS
4575static ssize_t rbd_snap_show(struct device *dev,
4576 struct device_attribute *attr,
4577 char *buf)
4578{
593a9e7b 4579 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
dfc5606d 4580
0d7dbfce 4581 return sprintf(buf, "%s\n", rbd_dev->spec->snap_name);
dfc5606d
YS
4582}
4583
92a58671
MC
4584static ssize_t rbd_snap_id_show(struct device *dev,
4585 struct device_attribute *attr, char *buf)
4586{
4587 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4588
4589 return sprintf(buf, "%llu\n", rbd_dev->spec->snap_id);
4590}
4591
86b00e0d 4592/*
ff96128f
ID
4593 * For a v2 image, shows the chain of parent images, separated by empty
4594 * lines. For v1 images or if there is no parent, shows "(no parent
4595 * image)".
86b00e0d
AE
4596 */
4597static ssize_t rbd_parent_show(struct device *dev,
ff96128f
ID
4598 struct device_attribute *attr,
4599 char *buf)
86b00e0d
AE
4600{
4601 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
ff96128f 4602 ssize_t count = 0;
86b00e0d 4603
ff96128f 4604 if (!rbd_dev->parent)
86b00e0d
AE
4605 return sprintf(buf, "(no parent image)\n");
4606
ff96128f
ID
4607 for ( ; rbd_dev->parent; rbd_dev = rbd_dev->parent) {
4608 struct rbd_spec *spec = rbd_dev->parent_spec;
4609
4610 count += sprintf(&buf[count], "%s"
4611 "pool_id %llu\npool_name %s\n"
4612 "image_id %s\nimage_name %s\n"
4613 "snap_id %llu\nsnap_name %s\n"
4614 "overlap %llu\n",
4615 !count ? "" : "\n", /* first? */
4616 spec->pool_id, spec->pool_name,
4617 spec->image_id, spec->image_name ?: "(unknown)",
4618 spec->snap_id, spec->snap_name,
4619 rbd_dev->parent_overlap);
4620 }
4621
4622 return count;
86b00e0d
AE
4623}
4624
dfc5606d
YS
4625static ssize_t rbd_image_refresh(struct device *dev,
4626 struct device_attribute *attr,
4627 const char *buf,
4628 size_t size)
4629{
593a9e7b 4630 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
b813623a 4631 int ret;
602adf40 4632
cc4a38bd 4633 ret = rbd_dev_refresh(rbd_dev);
e627db08 4634 if (ret)
52bb1f9b 4635 return ret;
b813623a 4636
52bb1f9b 4637 return size;
dfc5606d 4638}
602adf40 4639
dfc5606d 4640static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
34b13184 4641static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL);
dfc5606d 4642static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
dd82fff1 4643static DEVICE_ATTR(minor, S_IRUGO, rbd_minor_show, NULL);
005a07bf 4644static DEVICE_ATTR(client_addr, S_IRUGO, rbd_client_addr_show, NULL);
dfc5606d 4645static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
267fb90b 4646static DEVICE_ATTR(cluster_fsid, S_IRUGO, rbd_cluster_fsid_show, NULL);
0d6d1e9c 4647static DEVICE_ATTR(config_info, S_IRUSR, rbd_config_info_show, NULL);
dfc5606d 4648static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
9bb2f334 4649static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
dfc5606d 4650static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
589d30e0 4651static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL);
dfc5606d
YS
4652static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
4653static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
92a58671 4654static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL);
86b00e0d 4655static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL);
dfc5606d
YS
4656
4657static struct attribute *rbd_attrs[] = {
4658 &dev_attr_size.attr,
34b13184 4659 &dev_attr_features.attr,
dfc5606d 4660 &dev_attr_major.attr,
dd82fff1 4661 &dev_attr_minor.attr,
005a07bf 4662 &dev_attr_client_addr.attr,
dfc5606d 4663 &dev_attr_client_id.attr,
267fb90b 4664 &dev_attr_cluster_fsid.attr,
0d6d1e9c 4665 &dev_attr_config_info.attr,
dfc5606d 4666 &dev_attr_pool.attr,
9bb2f334 4667 &dev_attr_pool_id.attr,
dfc5606d 4668 &dev_attr_name.attr,
589d30e0 4669 &dev_attr_image_id.attr,
dfc5606d 4670 &dev_attr_current_snap.attr,
92a58671 4671 &dev_attr_snap_id.attr,
86b00e0d 4672 &dev_attr_parent.attr,
dfc5606d 4673 &dev_attr_refresh.attr,
dfc5606d
YS
4674 NULL
4675};
4676
4677static struct attribute_group rbd_attr_group = {
4678 .attrs = rbd_attrs,
4679};
4680
4681static const struct attribute_group *rbd_attr_groups[] = {
4682 &rbd_attr_group,
4683 NULL
4684};
4685
6cac4695 4686static void rbd_dev_release(struct device *dev);
dfc5606d 4687
b9942bc9 4688static const struct device_type rbd_device_type = {
dfc5606d
YS
4689 .name = "rbd",
4690 .groups = rbd_attr_groups,
6cac4695 4691 .release = rbd_dev_release,
dfc5606d
YS
4692};
4693
8b8fb99c
AE
4694static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)
4695{
4696 kref_get(&spec->kref);
4697
4698 return spec;
4699}
4700
4701static void rbd_spec_free(struct kref *kref);
4702static void rbd_spec_put(struct rbd_spec *spec)
4703{
4704 if (spec)
4705 kref_put(&spec->kref, rbd_spec_free);
4706}
4707
4708static struct rbd_spec *rbd_spec_alloc(void)
4709{
4710 struct rbd_spec *spec;
4711
4712 spec = kzalloc(sizeof (*spec), GFP_KERNEL);
4713 if (!spec)
4714 return NULL;
04077599
ID
4715
4716 spec->pool_id = CEPH_NOPOOL;
4717 spec->snap_id = CEPH_NOSNAP;
8b8fb99c
AE
4718 kref_init(&spec->kref);
4719
8b8fb99c
AE
4720 return spec;
4721}
4722
4723static void rbd_spec_free(struct kref *kref)
4724{
4725 struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref);
4726
4727 kfree(spec->pool_name);
4728 kfree(spec->image_id);
4729 kfree(spec->image_name);
4730 kfree(spec->snap_name);
4731 kfree(spec);
4732}
4733
1643dfa4 4734static void rbd_dev_free(struct rbd_device *rbd_dev)
dd5ac32d 4735{
99d16943 4736 WARN_ON(rbd_dev->watch_state != RBD_WATCH_STATE_UNREGISTERED);
ed95b21a 4737 WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_UNLOCKED);
dd5ac32d 4738
c41d13a3 4739 ceph_oid_destroy(&rbd_dev->header_oid);
6b6dddbe 4740 ceph_oloc_destroy(&rbd_dev->header_oloc);
0d6d1e9c 4741 kfree(rbd_dev->config_info);
c41d13a3 4742
dd5ac32d
ID
4743 rbd_put_client(rbd_dev->rbd_client);
4744 rbd_spec_put(rbd_dev->spec);
4745 kfree(rbd_dev->opts);
4746 kfree(rbd_dev);
1643dfa4
ID
4747}
4748
4749static void rbd_dev_release(struct device *dev)
4750{
4751 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4752 bool need_put = !!rbd_dev->opts;
4753
4754 if (need_put) {
4755 destroy_workqueue(rbd_dev->task_wq);
4756 ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id);
4757 }
4758
4759 rbd_dev_free(rbd_dev);
dd5ac32d
ID
4760
4761 /*
4762 * This is racy, but way better than putting module outside of
4763 * the release callback. The race window is pretty small, so
4764 * doing something similar to dm (dm-builtin.c) is overkill.
4765 */
4766 if (need_put)
4767 module_put(THIS_MODULE);
4768}
4769
1643dfa4
ID
4770static struct rbd_device *__rbd_dev_create(struct rbd_client *rbdc,
4771 struct rbd_spec *spec)
c53d5893
AE
4772{
4773 struct rbd_device *rbd_dev;
4774
1643dfa4 4775 rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL);
c53d5893
AE
4776 if (!rbd_dev)
4777 return NULL;
4778
4779 spin_lock_init(&rbd_dev->lock);
4780 INIT_LIST_HEAD(&rbd_dev->node);
c53d5893
AE
4781 init_rwsem(&rbd_dev->header_rwsem);
4782
7e97332e 4783 rbd_dev->header.data_pool_id = CEPH_NOPOOL;
c41d13a3 4784 ceph_oid_init(&rbd_dev->header_oid);
431a02cd 4785 rbd_dev->header_oloc.pool = spec->pool_id;
c41d13a3 4786
99d16943
ID
4787 mutex_init(&rbd_dev->watch_mutex);
4788 rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED;
4789 INIT_DELAYED_WORK(&rbd_dev->watch_dwork, rbd_reregister_watch);
4790
ed95b21a
ID
4791 init_rwsem(&rbd_dev->lock_rwsem);
4792 rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED;
4793 INIT_WORK(&rbd_dev->acquired_lock_work, rbd_notify_acquired_lock);
4794 INIT_WORK(&rbd_dev->released_lock_work, rbd_notify_released_lock);
4795 INIT_DELAYED_WORK(&rbd_dev->lock_dwork, rbd_acquire_lock);
4796 INIT_WORK(&rbd_dev->unlock_work, rbd_release_lock_work);
4797 init_waitqueue_head(&rbd_dev->lock_waitq);
4798
dd5ac32d
ID
4799 rbd_dev->dev.bus = &rbd_bus_type;
4800 rbd_dev->dev.type = &rbd_device_type;
4801 rbd_dev->dev.parent = &rbd_root_dev;
dd5ac32d
ID
4802 device_initialize(&rbd_dev->dev);
4803
c53d5893 4804 rbd_dev->rbd_client = rbdc;
d147543d 4805 rbd_dev->spec = spec;
0903e875 4806
1643dfa4
ID
4807 return rbd_dev;
4808}
4809
4810/*
4811 * Create a mapping rbd_dev.
4812 */
4813static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,
4814 struct rbd_spec *spec,
4815 struct rbd_options *opts)
4816{
4817 struct rbd_device *rbd_dev;
4818
4819 rbd_dev = __rbd_dev_create(rbdc, spec);
4820 if (!rbd_dev)
4821 return NULL;
4822
4823 rbd_dev->opts = opts;
4824
4825 /* get an id and fill in device name */
4826 rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0,
4827 minor_to_rbd_dev_id(1 << MINORBITS),
4828 GFP_KERNEL);
4829 if (rbd_dev->dev_id < 0)
4830 goto fail_rbd_dev;
4831
4832 sprintf(rbd_dev->name, RBD_DRV_NAME "%d", rbd_dev->dev_id);
4833 rbd_dev->task_wq = alloc_ordered_workqueue("%s-tasks", WQ_MEM_RECLAIM,
4834 rbd_dev->name);
4835 if (!rbd_dev->task_wq)
4836 goto fail_dev_id;
dd5ac32d 4837
1643dfa4
ID
4838 /* we have a ref from do_rbd_add() */
4839 __module_get(THIS_MODULE);
dd5ac32d 4840
1643dfa4 4841 dout("%s rbd_dev %p dev_id %d\n", __func__, rbd_dev, rbd_dev->dev_id);
c53d5893 4842 return rbd_dev;
1643dfa4
ID
4843
4844fail_dev_id:
4845 ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id);
4846fail_rbd_dev:
4847 rbd_dev_free(rbd_dev);
4848 return NULL;
c53d5893
AE
4849}
4850
4851static void rbd_dev_destroy(struct rbd_device *rbd_dev)
4852{
dd5ac32d
ID
4853 if (rbd_dev)
4854 put_device(&rbd_dev->dev);
c53d5893
AE
4855}
4856
9d475de5
AE
4857/*
4858 * Get the size and object order for an image snapshot, or if
4859 * snap_id is CEPH_NOSNAP, gets this information for the base
4860 * image.
4861 */
4862static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
4863 u8 *order, u64 *snap_size)
4864{
4865 __le64 snapid = cpu_to_le64(snap_id);
4866 int ret;
4867 struct {
4868 u8 order;
4869 __le64 size;
4870 } __attribute__ ((packed)) size_buf = { 0 };
4871
ecd4a68a
ID
4872 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
4873 &rbd_dev->header_oloc, "get_size",
4874 &snapid, sizeof(snapid),
4875 &size_buf, sizeof(size_buf));
36be9a76 4876 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
9d475de5
AE
4877 if (ret < 0)
4878 return ret;
57385b51
AE
4879 if (ret < sizeof (size_buf))
4880 return -ERANGE;
9d475de5 4881
c3545579 4882 if (order) {
c86f86e9 4883 *order = size_buf.order;
c3545579
JD
4884 dout(" order %u", (unsigned int)*order);
4885 }
9d475de5
AE
4886 *snap_size = le64_to_cpu(size_buf.size);
4887
c3545579
JD
4888 dout(" snap_id 0x%016llx snap_size = %llu\n",
4889 (unsigned long long)snap_id,
57385b51 4890 (unsigned long long)*snap_size);
9d475de5
AE
4891
4892 return 0;
4893}
4894
4895static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
4896{
4897 return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
4898 &rbd_dev->header.obj_order,
4899 &rbd_dev->header.image_size);
4900}
4901
1e130199
AE
4902static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
4903{
4904 void *reply_buf;
4905 int ret;
4906 void *p;
4907
4908 reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL);
4909 if (!reply_buf)
4910 return -ENOMEM;
4911
ecd4a68a
ID
4912 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
4913 &rbd_dev->header_oloc, "get_object_prefix",
4914 NULL, 0, reply_buf, RBD_OBJ_PREFIX_LEN_MAX);
36be9a76 4915 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
1e130199
AE
4916 if (ret < 0)
4917 goto out;
4918
4919 p = reply_buf;
4920 rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p,
57385b51
AE
4921 p + ret, NULL, GFP_NOIO);
4922 ret = 0;
1e130199
AE
4923
4924 if (IS_ERR(rbd_dev->header.object_prefix)) {
4925 ret = PTR_ERR(rbd_dev->header.object_prefix);
4926 rbd_dev->header.object_prefix = NULL;
4927 } else {
4928 dout(" object_prefix = %s\n", rbd_dev->header.object_prefix);
4929 }
1e130199
AE
4930out:
4931 kfree(reply_buf);
4932
4933 return ret;
4934}
4935
b1b5402a
AE
4936static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
4937 u64 *snap_features)
4938{
4939 __le64 snapid = cpu_to_le64(snap_id);
4940 struct {
4941 __le64 features;
4942 __le64 incompat;
4157976b 4943 } __attribute__ ((packed)) features_buf = { 0 };
d3767f0f 4944 u64 unsup;
b1b5402a
AE
4945 int ret;
4946
ecd4a68a
ID
4947 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
4948 &rbd_dev->header_oloc, "get_features",
4949 &snapid, sizeof(snapid),
4950 &features_buf, sizeof(features_buf));
36be9a76 4951 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
b1b5402a
AE
4952 if (ret < 0)
4953 return ret;
57385b51
AE
4954 if (ret < sizeof (features_buf))
4955 return -ERANGE;
d889140c 4956
d3767f0f
ID
4957 unsup = le64_to_cpu(features_buf.incompat) & ~RBD_FEATURES_SUPPORTED;
4958 if (unsup) {
4959 rbd_warn(rbd_dev, "image uses unsupported features: 0x%llx",
4960 unsup);
b8f5c6ed 4961 return -ENXIO;
d3767f0f 4962 }
d889140c 4963
b1b5402a
AE
4964 *snap_features = le64_to_cpu(features_buf.features);
4965
4966 dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
57385b51
AE
4967 (unsigned long long)snap_id,
4968 (unsigned long long)*snap_features,
4969 (unsigned long long)le64_to_cpu(features_buf.incompat));
b1b5402a
AE
4970
4971 return 0;
4972}
4973
4974static int rbd_dev_v2_features(struct rbd_device *rbd_dev)
4975{
4976 return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP,
4977 &rbd_dev->header.features);
4978}
4979
86b00e0d
AE
4980static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev)
4981{
4982 struct rbd_spec *parent_spec;
4983 size_t size;
4984 void *reply_buf = NULL;
4985 __le64 snapid;
4986 void *p;
4987 void *end;
642a2537 4988 u64 pool_id;
86b00e0d 4989 char *image_id;
3b5cf2a2 4990 u64 snap_id;
86b00e0d 4991 u64 overlap;
86b00e0d
AE
4992 int ret;
4993
4994 parent_spec = rbd_spec_alloc();
4995 if (!parent_spec)
4996 return -ENOMEM;
4997
4998 size = sizeof (__le64) + /* pool_id */
4999 sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */
5000 sizeof (__le64) + /* snap_id */
5001 sizeof (__le64); /* overlap */
5002 reply_buf = kmalloc(size, GFP_KERNEL);
5003 if (!reply_buf) {
5004 ret = -ENOMEM;
5005 goto out_err;
5006 }
5007
4d9b67cd 5008 snapid = cpu_to_le64(rbd_dev->spec->snap_id);
ecd4a68a
ID
5009 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5010 &rbd_dev->header_oloc, "get_parent",
5011 &snapid, sizeof(snapid), reply_buf, size);
36be9a76 5012 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
86b00e0d
AE
5013 if (ret < 0)
5014 goto out_err;
5015
86b00e0d 5016 p = reply_buf;
57385b51
AE
5017 end = reply_buf + ret;
5018 ret = -ERANGE;
642a2537 5019 ceph_decode_64_safe(&p, end, pool_id, out_err);
392a9dad
AE
5020 if (pool_id == CEPH_NOPOOL) {
5021 /*
5022 * Either the parent never existed, or we have
5023 * record of it but the image got flattened so it no
5024 * longer has a parent. When the parent of a
5025 * layered image disappears we immediately set the
5026 * overlap to 0. The effect of this is that all new
5027 * requests will be treated as if the image had no
5028 * parent.
5029 */
5030 if (rbd_dev->parent_overlap) {
5031 rbd_dev->parent_overlap = 0;
392a9dad
AE
5032 rbd_dev_parent_put(rbd_dev);
5033 pr_info("%s: clone image has been flattened\n",
5034 rbd_dev->disk->disk_name);
5035 }
5036
86b00e0d 5037 goto out; /* No parent? No problem. */
392a9dad 5038 }
86b00e0d 5039
0903e875
AE
5040 /* The ceph file layout needs to fit pool id in 32 bits */
5041
5042 ret = -EIO;
642a2537 5043 if (pool_id > (u64)U32_MAX) {
9584d508 5044 rbd_warn(NULL, "parent pool id too large (%llu > %u)",
642a2537 5045 (unsigned long long)pool_id, U32_MAX);
57385b51 5046 goto out_err;
c0cd10db 5047 }
0903e875 5048
979ed480 5049 image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
86b00e0d
AE
5050 if (IS_ERR(image_id)) {
5051 ret = PTR_ERR(image_id);
5052 goto out_err;
5053 }
3b5cf2a2 5054 ceph_decode_64_safe(&p, end, snap_id, out_err);
86b00e0d
AE
5055 ceph_decode_64_safe(&p, end, overlap, out_err);
5056
3b5cf2a2
AE
5057 /*
5058 * The parent won't change (except when the clone is
5059 * flattened, already handled that). So we only need to
5060 * record the parent spec we have not already done so.
5061 */
5062 if (!rbd_dev->parent_spec) {
5063 parent_spec->pool_id = pool_id;
5064 parent_spec->image_id = image_id;
5065 parent_spec->snap_id = snap_id;
70cf49cf
AE
5066 rbd_dev->parent_spec = parent_spec;
5067 parent_spec = NULL; /* rbd_dev now owns this */
fbba11b3
ID
5068 } else {
5069 kfree(image_id);
3b5cf2a2
AE
5070 }
5071
5072 /*
cf32bd9c
ID
5073 * We always update the parent overlap. If it's zero we issue
5074 * a warning, as we will proceed as if there was no parent.
3b5cf2a2 5075 */
3b5cf2a2 5076 if (!overlap) {
3b5cf2a2 5077 if (parent_spec) {
cf32bd9c
ID
5078 /* refresh, careful to warn just once */
5079 if (rbd_dev->parent_overlap)
5080 rbd_warn(rbd_dev,
5081 "clone now standalone (overlap became 0)");
3b5cf2a2 5082 } else {
cf32bd9c
ID
5083 /* initial probe */
5084 rbd_warn(rbd_dev, "clone is standalone (overlap 0)");
3b5cf2a2 5085 }
70cf49cf 5086 }
cf32bd9c
ID
5087 rbd_dev->parent_overlap = overlap;
5088
86b00e0d
AE
5089out:
5090 ret = 0;
5091out_err:
5092 kfree(reply_buf);
5093 rbd_spec_put(parent_spec);
5094
5095 return ret;
5096}
5097
cc070d59
AE
5098static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev)
5099{
5100 struct {
5101 __le64 stripe_unit;
5102 __le64 stripe_count;
5103 } __attribute__ ((packed)) striping_info_buf = { 0 };
5104 size_t size = sizeof (striping_info_buf);
5105 void *p;
5106 u64 obj_size;
5107 u64 stripe_unit;
5108 u64 stripe_count;
5109 int ret;
5110
ecd4a68a
ID
5111 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5112 &rbd_dev->header_oloc, "get_stripe_unit_count",
5113 NULL, 0, &striping_info_buf, size);
cc070d59
AE
5114 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
5115 if (ret < 0)
5116 return ret;
5117 if (ret < size)
5118 return -ERANGE;
5119
5120 /*
5121 * We don't actually support the "fancy striping" feature
5122 * (STRIPINGV2) yet, but if the striping sizes are the
5123 * defaults the behavior is the same as before. So find
5124 * out, and only fail if the image has non-default values.
5125 */
5126 ret = -EINVAL;
5bc3fb17 5127 obj_size = rbd_obj_bytes(&rbd_dev->header);
cc070d59
AE
5128 p = &striping_info_buf;
5129 stripe_unit = ceph_decode_64(&p);
5130 if (stripe_unit != obj_size) {
5131 rbd_warn(rbd_dev, "unsupported stripe unit "
5132 "(got %llu want %llu)",
5133 stripe_unit, obj_size);
5134 return -EINVAL;
5135 }
5136 stripe_count = ceph_decode_64(&p);
5137 if (stripe_count != 1) {
5138 rbd_warn(rbd_dev, "unsupported stripe count "
5139 "(got %llu want 1)", stripe_count);
5140 return -EINVAL;
5141 }
500d0c0f
AE
5142 rbd_dev->header.stripe_unit = stripe_unit;
5143 rbd_dev->header.stripe_count = stripe_count;
cc070d59
AE
5144
5145 return 0;
5146}
5147
7e97332e
ID
5148static int rbd_dev_v2_data_pool(struct rbd_device *rbd_dev)
5149{
5150 __le64 data_pool_id;
5151 int ret;
5152
5153 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5154 &rbd_dev->header_oloc, "get_data_pool",
5155 NULL, 0, &data_pool_id, sizeof(data_pool_id));
5156 if (ret < 0)
5157 return ret;
5158 if (ret < sizeof(data_pool_id))
5159 return -EBADMSG;
5160
5161 rbd_dev->header.data_pool_id = le64_to_cpu(data_pool_id);
5162 WARN_ON(rbd_dev->header.data_pool_id == CEPH_NOPOOL);
5163 return 0;
5164}
5165
9e15b77d
AE
5166static char *rbd_dev_image_name(struct rbd_device *rbd_dev)
5167{
ecd4a68a 5168 CEPH_DEFINE_OID_ONSTACK(oid);
9e15b77d
AE
5169 size_t image_id_size;
5170 char *image_id;
5171 void *p;
5172 void *end;
5173 size_t size;
5174 void *reply_buf = NULL;
5175 size_t len = 0;
5176 char *image_name = NULL;
5177 int ret;
5178
5179 rbd_assert(!rbd_dev->spec->image_name);
5180
69e7a02f
AE
5181 len = strlen(rbd_dev->spec->image_id);
5182 image_id_size = sizeof (__le32) + len;
9e15b77d
AE
5183 image_id = kmalloc(image_id_size, GFP_KERNEL);
5184 if (!image_id)
5185 return NULL;
5186
5187 p = image_id;
4157976b 5188 end = image_id + image_id_size;
57385b51 5189 ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len);
9e15b77d
AE
5190
5191 size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX;
5192 reply_buf = kmalloc(size, GFP_KERNEL);
5193 if (!reply_buf)
5194 goto out;
5195
ecd4a68a
ID
5196 ceph_oid_printf(&oid, "%s", RBD_DIRECTORY);
5197 ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc,
5198 "dir_get_name", image_id, image_id_size,
5199 reply_buf, size);
9e15b77d
AE
5200 if (ret < 0)
5201 goto out;
5202 p = reply_buf;
f40eb349
AE
5203 end = reply_buf + ret;
5204
9e15b77d
AE
5205 image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL);
5206 if (IS_ERR(image_name))
5207 image_name = NULL;
5208 else
5209 dout("%s: name is %s len is %zd\n", __func__, image_name, len);
5210out:
5211 kfree(reply_buf);
5212 kfree(image_id);
5213
5214 return image_name;
5215}
5216
2ad3d716
AE
5217static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
5218{
5219 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
5220 const char *snap_name;
5221 u32 which = 0;
5222
5223 /* Skip over names until we find the one we are looking for */
5224
5225 snap_name = rbd_dev->header.snap_names;
5226 while (which < snapc->num_snaps) {
5227 if (!strcmp(name, snap_name))
5228 return snapc->snaps[which];
5229 snap_name += strlen(snap_name) + 1;
5230 which++;
5231 }
5232 return CEPH_NOSNAP;
5233}
5234
5235static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
5236{
5237 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
5238 u32 which;
5239 bool found = false;
5240 u64 snap_id;
5241
5242 for (which = 0; !found && which < snapc->num_snaps; which++) {
5243 const char *snap_name;
5244
5245 snap_id = snapc->snaps[which];
5246 snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id);
efadc98a
JD
5247 if (IS_ERR(snap_name)) {
5248 /* ignore no-longer existing snapshots */
5249 if (PTR_ERR(snap_name) == -ENOENT)
5250 continue;
5251 else
5252 break;
5253 }
2ad3d716
AE
5254 found = !strcmp(name, snap_name);
5255 kfree(snap_name);
5256 }
5257 return found ? snap_id : CEPH_NOSNAP;
5258}
5259
5260/*
5261 * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if
5262 * no snapshot by that name is found, or if an error occurs.
5263 */
5264static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
5265{
5266 if (rbd_dev->image_format == 1)
5267 return rbd_v1_snap_id_by_name(rbd_dev, name);
5268
5269 return rbd_v2_snap_id_by_name(rbd_dev, name);
5270}
5271
9e15b77d 5272/*
04077599
ID
5273 * An image being mapped will have everything but the snap id.
5274 */
5275static int rbd_spec_fill_snap_id(struct rbd_device *rbd_dev)
5276{
5277 struct rbd_spec *spec = rbd_dev->spec;
5278
5279 rbd_assert(spec->pool_id != CEPH_NOPOOL && spec->pool_name);
5280 rbd_assert(spec->image_id && spec->image_name);
5281 rbd_assert(spec->snap_name);
5282
5283 if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) {
5284 u64 snap_id;
5285
5286 snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name);
5287 if (snap_id == CEPH_NOSNAP)
5288 return -ENOENT;
5289
5290 spec->snap_id = snap_id;
5291 } else {
5292 spec->snap_id = CEPH_NOSNAP;
5293 }
5294
5295 return 0;
5296}
5297
5298/*
5299 * A parent image will have all ids but none of the names.
e1d4213f 5300 *
04077599
ID
5301 * All names in an rbd spec are dynamically allocated. It's OK if we
5302 * can't figure out the name for an image id.
9e15b77d 5303 */
04077599 5304static int rbd_spec_fill_names(struct rbd_device *rbd_dev)
9e15b77d 5305{
2e9f7f1c
AE
5306 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
5307 struct rbd_spec *spec = rbd_dev->spec;
5308 const char *pool_name;
5309 const char *image_name;
5310 const char *snap_name;
9e15b77d
AE
5311 int ret;
5312
04077599
ID
5313 rbd_assert(spec->pool_id != CEPH_NOPOOL);
5314 rbd_assert(spec->image_id);
5315 rbd_assert(spec->snap_id != CEPH_NOSNAP);
9e15b77d 5316
2e9f7f1c 5317 /* Get the pool name; we have to make our own copy of this */
9e15b77d 5318
2e9f7f1c
AE
5319 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id);
5320 if (!pool_name) {
5321 rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id);
935dc89f
AE
5322 return -EIO;
5323 }
2e9f7f1c
AE
5324 pool_name = kstrdup(pool_name, GFP_KERNEL);
5325 if (!pool_name)
9e15b77d
AE
5326 return -ENOMEM;
5327
5328 /* Fetch the image name; tolerate failure here */
5329
2e9f7f1c
AE
5330 image_name = rbd_dev_image_name(rbd_dev);
5331 if (!image_name)
06ecc6cb 5332 rbd_warn(rbd_dev, "unable to get image name");
9e15b77d 5333
04077599 5334 /* Fetch the snapshot name */
9e15b77d 5335
2e9f7f1c 5336 snap_name = rbd_snap_name(rbd_dev, spec->snap_id);
da6a6b63
JD
5337 if (IS_ERR(snap_name)) {
5338 ret = PTR_ERR(snap_name);
9e15b77d 5339 goto out_err;
2e9f7f1c
AE
5340 }
5341
5342 spec->pool_name = pool_name;
5343 spec->image_name = image_name;
5344 spec->snap_name = snap_name;
9e15b77d
AE
5345
5346 return 0;
04077599 5347
9e15b77d 5348out_err:
2e9f7f1c
AE
5349 kfree(image_name);
5350 kfree(pool_name);
9e15b77d
AE
5351 return ret;
5352}
5353
cc4a38bd 5354static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev)
35d489f9
AE
5355{
5356 size_t size;
5357 int ret;
5358 void *reply_buf;
5359 void *p;
5360 void *end;
5361 u64 seq;
5362 u32 snap_count;
5363 struct ceph_snap_context *snapc;
5364 u32 i;
5365
5366 /*
5367 * We'll need room for the seq value (maximum snapshot id),
5368 * snapshot count, and array of that many snapshot ids.
5369 * For now we have a fixed upper limit on the number we're
5370 * prepared to receive.
5371 */
5372 size = sizeof (__le64) + sizeof (__le32) +
5373 RBD_MAX_SNAP_COUNT * sizeof (__le64);
5374 reply_buf = kzalloc(size, GFP_KERNEL);
5375 if (!reply_buf)
5376 return -ENOMEM;
5377
ecd4a68a
ID
5378 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5379 &rbd_dev->header_oloc, "get_snapcontext",
5380 NULL, 0, reply_buf, size);
36be9a76 5381 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
35d489f9
AE
5382 if (ret < 0)
5383 goto out;
5384
35d489f9 5385 p = reply_buf;
57385b51
AE
5386 end = reply_buf + ret;
5387 ret = -ERANGE;
35d489f9
AE
5388 ceph_decode_64_safe(&p, end, seq, out);
5389 ceph_decode_32_safe(&p, end, snap_count, out);
5390
5391 /*
5392 * Make sure the reported number of snapshot ids wouldn't go
5393 * beyond the end of our buffer. But before checking that,
5394 * make sure the computed size of the snapshot context we
5395 * allocate is representable in a size_t.
5396 */
5397 if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
5398 / sizeof (u64)) {
5399 ret = -EINVAL;
5400 goto out;
5401 }
5402 if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
5403 goto out;
468521c1 5404 ret = 0;
35d489f9 5405
812164f8 5406 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
35d489f9
AE
5407 if (!snapc) {
5408 ret = -ENOMEM;
5409 goto out;
5410 }
35d489f9 5411 snapc->seq = seq;
35d489f9
AE
5412 for (i = 0; i < snap_count; i++)
5413 snapc->snaps[i] = ceph_decode_64(&p);
5414
49ece554 5415 ceph_put_snap_context(rbd_dev->header.snapc);
35d489f9
AE
5416 rbd_dev->header.snapc = snapc;
5417
5418 dout(" snap context seq = %llu, snap_count = %u\n",
57385b51 5419 (unsigned long long)seq, (unsigned int)snap_count);
35d489f9
AE
5420out:
5421 kfree(reply_buf);
5422
57385b51 5423 return ret;
35d489f9
AE
5424}
5425
54cac61f
AE
5426static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
5427 u64 snap_id)
b8b1e2db
AE
5428{
5429 size_t size;
5430 void *reply_buf;
54cac61f 5431 __le64 snapid;
b8b1e2db
AE
5432 int ret;
5433 void *p;
5434 void *end;
b8b1e2db
AE
5435 char *snap_name;
5436
5437 size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN;
5438 reply_buf = kmalloc(size, GFP_KERNEL);
5439 if (!reply_buf)
5440 return ERR_PTR(-ENOMEM);
5441
54cac61f 5442 snapid = cpu_to_le64(snap_id);
ecd4a68a
ID
5443 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5444 &rbd_dev->header_oloc, "get_snapshot_name",
5445 &snapid, sizeof(snapid), reply_buf, size);
36be9a76 5446 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
f40eb349
AE
5447 if (ret < 0) {
5448 snap_name = ERR_PTR(ret);
b8b1e2db 5449 goto out;
f40eb349 5450 }
b8b1e2db
AE
5451
5452 p = reply_buf;
f40eb349 5453 end = reply_buf + ret;
e5c35534 5454 snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
f40eb349 5455 if (IS_ERR(snap_name))
b8b1e2db 5456 goto out;
b8b1e2db 5457
f40eb349 5458 dout(" snap_id 0x%016llx snap_name = %s\n",
54cac61f 5459 (unsigned long long)snap_id, snap_name);
b8b1e2db
AE
5460out:
5461 kfree(reply_buf);
5462
f40eb349 5463 return snap_name;
b8b1e2db
AE
5464}
5465
2df3fac7 5466static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev)
117973fb 5467{
2df3fac7 5468 bool first_time = rbd_dev->header.object_prefix == NULL;
117973fb 5469 int ret;
117973fb 5470
1617e40c
JD
5471 ret = rbd_dev_v2_image_size(rbd_dev);
5472 if (ret)
cfbf6377 5473 return ret;
1617e40c 5474
2df3fac7
AE
5475 if (first_time) {
5476 ret = rbd_dev_v2_header_onetime(rbd_dev);
5477 if (ret)
cfbf6377 5478 return ret;
2df3fac7
AE
5479 }
5480
cc4a38bd 5481 ret = rbd_dev_v2_snap_context(rbd_dev);
d194cd1d
ID
5482 if (ret && first_time) {
5483 kfree(rbd_dev->header.object_prefix);
5484 rbd_dev->header.object_prefix = NULL;
5485 }
117973fb
AE
5486
5487 return ret;
5488}
5489
a720ae09
ID
5490static int rbd_dev_header_info(struct rbd_device *rbd_dev)
5491{
5492 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
5493
5494 if (rbd_dev->image_format == 1)
5495 return rbd_dev_v1_header_info(rbd_dev);
5496
5497 return rbd_dev_v2_header_info(rbd_dev);
5498}
5499
e28fff26
AE
5500/*
5501 * Skips over white space at *buf, and updates *buf to point to the
5502 * first found non-space character (if any). Returns the length of
593a9e7b
AE
5503 * the token (string of non-white space characters) found. Note
5504 * that *buf must be terminated with '\0'.
e28fff26
AE
5505 */
5506static inline size_t next_token(const char **buf)
5507{
5508 /*
5509 * These are the characters that produce nonzero for
5510 * isspace() in the "C" and "POSIX" locales.
5511 */
5512 const char *spaces = " \f\n\r\t\v";
5513
5514 *buf += strspn(*buf, spaces); /* Find start of token */
5515
5516 return strcspn(*buf, spaces); /* Return token length */
5517}
5518
ea3352f4
AE
5519/*
5520 * Finds the next token in *buf, dynamically allocates a buffer big
5521 * enough to hold a copy of it, and copies the token into the new
5522 * buffer. The copy is guaranteed to be terminated with '\0'. Note
5523 * that a duplicate buffer is created even for a zero-length token.
5524 *
5525 * Returns a pointer to the newly-allocated duplicate, or a null
5526 * pointer if memory for the duplicate was not available. If
5527 * the lenp argument is a non-null pointer, the length of the token
5528 * (not including the '\0') is returned in *lenp.
5529 *
5530 * If successful, the *buf pointer will be updated to point beyond
5531 * the end of the found token.
5532 *
5533 * Note: uses GFP_KERNEL for allocation.
5534 */
5535static inline char *dup_token(const char **buf, size_t *lenp)
5536{
5537 char *dup;
5538 size_t len;
5539
5540 len = next_token(buf);
4caf35f9 5541 dup = kmemdup(*buf, len + 1, GFP_KERNEL);
ea3352f4
AE
5542 if (!dup)
5543 return NULL;
ea3352f4
AE
5544 *(dup + len) = '\0';
5545 *buf += len;
5546
5547 if (lenp)
5548 *lenp = len;
5549
5550 return dup;
5551}
5552
a725f65e 5553/*
859c31df
AE
5554 * Parse the options provided for an "rbd add" (i.e., rbd image
5555 * mapping) request. These arrive via a write to /sys/bus/rbd/add,
5556 * and the data written is passed here via a NUL-terminated buffer.
5557 * Returns 0 if successful or an error code otherwise.
d22f76e7 5558 *
859c31df
AE
5559 * The information extracted from these options is recorded in
5560 * the other parameters which return dynamically-allocated
5561 * structures:
5562 * ceph_opts
5563 * The address of a pointer that will refer to a ceph options
5564 * structure. Caller must release the returned pointer using
5565 * ceph_destroy_options() when it is no longer needed.
5566 * rbd_opts
5567 * Address of an rbd options pointer. Fully initialized by
5568 * this function; caller must release with kfree().
5569 * spec
5570 * Address of an rbd image specification pointer. Fully
5571 * initialized by this function based on parsed options.
5572 * Caller must release with rbd_spec_put().
5573 *
5574 * The options passed take this form:
5575 * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
5576 * where:
5577 * <mon_addrs>
5578 * A comma-separated list of one or more monitor addresses.
5579 * A monitor address is an ip address, optionally followed
5580 * by a port number (separated by a colon).
5581 * I.e.: ip1[:port1][,ip2[:port2]...]
5582 * <options>
5583 * A comma-separated list of ceph and/or rbd options.
5584 * <pool_name>
5585 * The name of the rados pool containing the rbd image.
5586 * <image_name>
5587 * The name of the image in that pool to map.
5588 * <snap_id>
5589 * An optional snapshot id. If provided, the mapping will
5590 * present data from the image at the time that snapshot was
5591 * created. The image head is used if no snapshot id is
5592 * provided. Snapshot mappings are always read-only.
a725f65e 5593 */
859c31df 5594static int rbd_add_parse_args(const char *buf,
dc79b113 5595 struct ceph_options **ceph_opts,
859c31df
AE
5596 struct rbd_options **opts,
5597 struct rbd_spec **rbd_spec)
e28fff26 5598{
d22f76e7 5599 size_t len;
859c31df 5600 char *options;
0ddebc0c 5601 const char *mon_addrs;
ecb4dc22 5602 char *snap_name;
0ddebc0c 5603 size_t mon_addrs_size;
859c31df 5604 struct rbd_spec *spec = NULL;
4e9afeba 5605 struct rbd_options *rbd_opts = NULL;
859c31df 5606 struct ceph_options *copts;
dc79b113 5607 int ret;
e28fff26
AE
5608
5609 /* The first four tokens are required */
5610
7ef3214a 5611 len = next_token(&buf);
4fb5d671
AE
5612 if (!len) {
5613 rbd_warn(NULL, "no monitor address(es) provided");
5614 return -EINVAL;
5615 }
0ddebc0c 5616 mon_addrs = buf;
f28e565a 5617 mon_addrs_size = len + 1;
7ef3214a 5618 buf += len;
a725f65e 5619
dc79b113 5620 ret = -EINVAL;
f28e565a
AE
5621 options = dup_token(&buf, NULL);
5622 if (!options)
dc79b113 5623 return -ENOMEM;
4fb5d671
AE
5624 if (!*options) {
5625 rbd_warn(NULL, "no options provided");
5626 goto out_err;
5627 }
e28fff26 5628
859c31df
AE
5629 spec = rbd_spec_alloc();
5630 if (!spec)
f28e565a 5631 goto out_mem;
859c31df
AE
5632
5633 spec->pool_name = dup_token(&buf, NULL);
5634 if (!spec->pool_name)
5635 goto out_mem;
4fb5d671
AE
5636 if (!*spec->pool_name) {
5637 rbd_warn(NULL, "no pool name provided");
5638 goto out_err;
5639 }
e28fff26 5640
69e7a02f 5641 spec->image_name = dup_token(&buf, NULL);
859c31df 5642 if (!spec->image_name)
f28e565a 5643 goto out_mem;
4fb5d671
AE
5644 if (!*spec->image_name) {
5645 rbd_warn(NULL, "no image name provided");
5646 goto out_err;
5647 }
d4b125e9 5648
f28e565a
AE
5649 /*
5650 * Snapshot name is optional; default is to use "-"
5651 * (indicating the head/no snapshot).
5652 */
3feeb894 5653 len = next_token(&buf);
820a5f3e 5654 if (!len) {
3feeb894
AE
5655 buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
5656 len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
f28e565a 5657 } else if (len > RBD_MAX_SNAP_NAME_LEN) {
dc79b113 5658 ret = -ENAMETOOLONG;
f28e565a 5659 goto out_err;
849b4260 5660 }
ecb4dc22
AE
5661 snap_name = kmemdup(buf, len + 1, GFP_KERNEL);
5662 if (!snap_name)
f28e565a 5663 goto out_mem;
ecb4dc22
AE
5664 *(snap_name + len) = '\0';
5665 spec->snap_name = snap_name;
e5c35534 5666
0ddebc0c 5667 /* Initialize all rbd options to the defaults */
e28fff26 5668
4e9afeba
AE
5669 rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL);
5670 if (!rbd_opts)
5671 goto out_mem;
5672
5673 rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
b5584180 5674 rbd_opts->queue_depth = RBD_QUEUE_DEPTH_DEFAULT;
80de1912 5675 rbd_opts->lock_on_read = RBD_LOCK_ON_READ_DEFAULT;
e010dd0a 5676 rbd_opts->exclusive = RBD_EXCLUSIVE_DEFAULT;
d22f76e7 5677
859c31df 5678 copts = ceph_parse_options(options, mon_addrs,
0ddebc0c 5679 mon_addrs + mon_addrs_size - 1,
4e9afeba 5680 parse_rbd_opts_token, rbd_opts);
859c31df
AE
5681 if (IS_ERR(copts)) {
5682 ret = PTR_ERR(copts);
dc79b113
AE
5683 goto out_err;
5684 }
859c31df
AE
5685 kfree(options);
5686
5687 *ceph_opts = copts;
4e9afeba 5688 *opts = rbd_opts;
859c31df 5689 *rbd_spec = spec;
0ddebc0c 5690
dc79b113 5691 return 0;
f28e565a 5692out_mem:
dc79b113 5693 ret = -ENOMEM;
d22f76e7 5694out_err:
859c31df
AE
5695 kfree(rbd_opts);
5696 rbd_spec_put(spec);
f28e565a 5697 kfree(options);
d22f76e7 5698
dc79b113 5699 return ret;
a725f65e
AE
5700}
5701
30ba1f02
ID
5702/*
5703 * Return pool id (>= 0) or a negative error code.
5704 */
5705static int rbd_add_get_pool_id(struct rbd_client *rbdc, const char *pool_name)
5706{
a319bf56 5707 struct ceph_options *opts = rbdc->client->options;
30ba1f02 5708 u64 newest_epoch;
30ba1f02
ID
5709 int tries = 0;
5710 int ret;
5711
5712again:
5713 ret = ceph_pg_poolid_by_name(rbdc->client->osdc.osdmap, pool_name);
5714 if (ret == -ENOENT && tries++ < 1) {
d0b19705
ID
5715 ret = ceph_monc_get_version(&rbdc->client->monc, "osdmap",
5716 &newest_epoch);
30ba1f02
ID
5717 if (ret < 0)
5718 return ret;
5719
5720 if (rbdc->client->osdc.osdmap->epoch < newest_epoch) {
7cca78c9 5721 ceph_osdc_maybe_request_map(&rbdc->client->osdc);
30ba1f02 5722 (void) ceph_monc_wait_osdmap(&rbdc->client->monc,
a319bf56
ID
5723 newest_epoch,
5724 opts->mount_timeout);
30ba1f02
ID
5725 goto again;
5726 } else {
5727 /* the osdmap we have is new enough */
5728 return -ENOENT;
5729 }
5730 }
5731
5732 return ret;
5733}
5734
e010dd0a
ID
5735static void rbd_dev_image_unlock(struct rbd_device *rbd_dev)
5736{
5737 down_write(&rbd_dev->lock_rwsem);
5738 if (__rbd_is_lock_owner(rbd_dev))
5739 rbd_unlock(rbd_dev);
5740 up_write(&rbd_dev->lock_rwsem);
5741}
5742
5743static int rbd_add_acquire_lock(struct rbd_device *rbd_dev)
5744{
5745 if (!(rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK)) {
5746 rbd_warn(rbd_dev, "exclusive-lock feature is not enabled");
5747 return -EINVAL;
5748 }
5749
5750 /* FIXME: "rbd map --exclusive" should be in interruptible */
5751 down_read(&rbd_dev->lock_rwsem);
5752 rbd_wait_state_locked(rbd_dev);
5753 up_read(&rbd_dev->lock_rwsem);
5754 if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) {
5755 rbd_warn(rbd_dev, "failed to acquire exclusive lock");
5756 return -EROFS;
5757 }
5758
5759 return 0;
5760}
5761
589d30e0
AE
5762/*
5763 * An rbd format 2 image has a unique identifier, distinct from the
5764 * name given to it by the user. Internally, that identifier is
5765 * what's used to specify the names of objects related to the image.
5766 *
5767 * A special "rbd id" object is used to map an rbd image name to its
5768 * id. If that object doesn't exist, then there is no v2 rbd image
5769 * with the supplied name.
5770 *
5771 * This function will record the given rbd_dev's image_id field if
5772 * it can be determined, and in that case will return 0. If any
5773 * errors occur a negative errno will be returned and the rbd_dev's
5774 * image_id field will be unchanged (and should be NULL).
5775 */
5776static int rbd_dev_image_id(struct rbd_device *rbd_dev)
5777{
5778 int ret;
5779 size_t size;
ecd4a68a 5780 CEPH_DEFINE_OID_ONSTACK(oid);
589d30e0 5781 void *response;
c0fba368 5782 char *image_id;
2f82ee54 5783
2c0d0a10
AE
5784 /*
5785 * When probing a parent image, the image id is already
5786 * known (and the image name likely is not). There's no
c0fba368
AE
5787 * need to fetch the image id again in this case. We
5788 * do still need to set the image format though.
2c0d0a10 5789 */
c0fba368
AE
5790 if (rbd_dev->spec->image_id) {
5791 rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1;
5792
2c0d0a10 5793 return 0;
c0fba368 5794 }
2c0d0a10 5795
589d30e0
AE
5796 /*
5797 * First, see if the format 2 image id file exists, and if
5798 * so, get the image's persistent id from it.
5799 */
ecd4a68a
ID
5800 ret = ceph_oid_aprintf(&oid, GFP_KERNEL, "%s%s", RBD_ID_PREFIX,
5801 rbd_dev->spec->image_name);
5802 if (ret)
5803 return ret;
5804
5805 dout("rbd id object name is %s\n", oid.name);
589d30e0
AE
5806
5807 /* Response will be an encoded string, which includes a length */
5808
5809 size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
5810 response = kzalloc(size, GFP_NOIO);
5811 if (!response) {
5812 ret = -ENOMEM;
5813 goto out;
5814 }
5815
c0fba368
AE
5816 /* If it doesn't exist we'll assume it's a format 1 image */
5817
ecd4a68a
ID
5818 ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc,
5819 "get_id", NULL, 0,
5820 response, RBD_IMAGE_ID_LEN_MAX);
36be9a76 5821 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
c0fba368
AE
5822 if (ret == -ENOENT) {
5823 image_id = kstrdup("", GFP_KERNEL);
5824 ret = image_id ? 0 : -ENOMEM;
5825 if (!ret)
5826 rbd_dev->image_format = 1;
7dd440c9 5827 } else if (ret >= 0) {
c0fba368
AE
5828 void *p = response;
5829
5830 image_id = ceph_extract_encoded_string(&p, p + ret,
979ed480 5831 NULL, GFP_NOIO);
461f758a 5832 ret = PTR_ERR_OR_ZERO(image_id);
c0fba368
AE
5833 if (!ret)
5834 rbd_dev->image_format = 2;
c0fba368
AE
5835 }
5836
5837 if (!ret) {
5838 rbd_dev->spec->image_id = image_id;
5839 dout("image_id is %s\n", image_id);
589d30e0
AE
5840 }
5841out:
5842 kfree(response);
ecd4a68a 5843 ceph_oid_destroy(&oid);
589d30e0
AE
5844 return ret;
5845}
5846
3abef3b3
AE
5847/*
5848 * Undo whatever state changes are made by v1 or v2 header info
5849 * call.
5850 */
6fd48b3b
AE
5851static void rbd_dev_unprobe(struct rbd_device *rbd_dev)
5852{
5853 struct rbd_image_header *header;
5854
e69b8d41 5855 rbd_dev_parent_put(rbd_dev);
6fd48b3b
AE
5856
5857 /* Free dynamic fields from the header, then zero it out */
5858
5859 header = &rbd_dev->header;
812164f8 5860 ceph_put_snap_context(header->snapc);
6fd48b3b
AE
5861 kfree(header->snap_sizes);
5862 kfree(header->snap_names);
5863 kfree(header->object_prefix);
5864 memset(header, 0, sizeof (*header));
5865}
5866
2df3fac7 5867static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev)
a30b71b9
AE
5868{
5869 int ret;
a30b71b9 5870
1e130199 5871 ret = rbd_dev_v2_object_prefix(rbd_dev);
57385b51 5872 if (ret)
b1b5402a
AE
5873 goto out_err;
5874
2df3fac7
AE
5875 /*
5876 * Get the and check features for the image. Currently the
5877 * features are assumed to never change.
5878 */
b1b5402a 5879 ret = rbd_dev_v2_features(rbd_dev);
57385b51 5880 if (ret)
9d475de5 5881 goto out_err;
35d489f9 5882
cc070d59
AE
5883 /* If the image supports fancy striping, get its parameters */
5884
5885 if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) {
5886 ret = rbd_dev_v2_striping_info(rbd_dev);
5887 if (ret < 0)
5888 goto out_err;
5889 }
a30b71b9 5890
7e97332e
ID
5891 if (rbd_dev->header.features & RBD_FEATURE_DATA_POOL) {
5892 ret = rbd_dev_v2_data_pool(rbd_dev);
5893 if (ret)
5894 goto out_err;
5895 }
5896
263423f8 5897 rbd_init_layout(rbd_dev);
35152979 5898 return 0;
263423f8 5899
9d475de5 5900out_err:
642a2537 5901 rbd_dev->header.features = 0;
1e130199
AE
5902 kfree(rbd_dev->header.object_prefix);
5903 rbd_dev->header.object_prefix = NULL;
9d475de5 5904 return ret;
a30b71b9
AE
5905}
5906
6d69bb53
ID
5907/*
5908 * @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() ->
5909 * rbd_dev_image_probe() recursion depth, which means it's also the
5910 * length of the already discovered part of the parent chain.
5911 */
5912static int rbd_dev_probe_parent(struct rbd_device *rbd_dev, int depth)
83a06263 5913{
2f82ee54 5914 struct rbd_device *parent = NULL;
124afba2
AE
5915 int ret;
5916
5917 if (!rbd_dev->parent_spec)
5918 return 0;
124afba2 5919
6d69bb53
ID
5920 if (++depth > RBD_MAX_PARENT_CHAIN_LEN) {
5921 pr_info("parent chain is too long (%d)\n", depth);
5922 ret = -EINVAL;
5923 goto out_err;
5924 }
5925
1643dfa4 5926 parent = __rbd_dev_create(rbd_dev->rbd_client, rbd_dev->parent_spec);
1f2c6651
ID
5927 if (!parent) {
5928 ret = -ENOMEM;
124afba2 5929 goto out_err;
1f2c6651
ID
5930 }
5931
5932 /*
5933 * Images related by parent/child relationships always share
5934 * rbd_client and spec/parent_spec, so bump their refcounts.
5935 */
5936 __rbd_get_client(rbd_dev->rbd_client);
5937 rbd_spec_get(rbd_dev->parent_spec);
124afba2 5938
6d69bb53 5939 ret = rbd_dev_image_probe(parent, depth);
124afba2
AE
5940 if (ret < 0)
5941 goto out_err;
1f2c6651 5942
124afba2 5943 rbd_dev->parent = parent;
a2acd00e 5944 atomic_set(&rbd_dev->parent_ref, 1);
124afba2 5945 return 0;
1f2c6651 5946
124afba2 5947out_err:
1f2c6651 5948 rbd_dev_unparent(rbd_dev);
1761b229 5949 rbd_dev_destroy(parent);
124afba2
AE
5950 return ret;
5951}
5952
5769ed0c
ID
5953static void rbd_dev_device_release(struct rbd_device *rbd_dev)
5954{
5955 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
5956 rbd_dev_mapping_clear(rbd_dev);
5957 rbd_free_disk(rbd_dev);
5958 if (!single_major)
5959 unregister_blkdev(rbd_dev->major, rbd_dev->name);
5960}
5961
811c6688
ID
5962/*
5963 * rbd_dev->header_rwsem must be locked for write and will be unlocked
5964 * upon return.
5965 */
200a6a8b 5966static int rbd_dev_device_setup(struct rbd_device *rbd_dev)
124afba2 5967{
83a06263 5968 int ret;
d1cf5788 5969
9b60e70b 5970 /* Record our major and minor device numbers. */
83a06263 5971
9b60e70b
ID
5972 if (!single_major) {
5973 ret = register_blkdev(0, rbd_dev->name);
5974 if (ret < 0)
1643dfa4 5975 goto err_out_unlock;
9b60e70b
ID
5976
5977 rbd_dev->major = ret;
5978 rbd_dev->minor = 0;
5979 } else {
5980 rbd_dev->major = rbd_major;
5981 rbd_dev->minor = rbd_dev_id_to_minor(rbd_dev->dev_id);
5982 }
83a06263
AE
5983
5984 /* Set up the blkdev mapping. */
5985
5986 ret = rbd_init_disk(rbd_dev);
5987 if (ret)
5988 goto err_out_blkdev;
5989
f35a4dee 5990 ret = rbd_dev_mapping_set(rbd_dev);
83a06263
AE
5991 if (ret)
5992 goto err_out_disk;
bc1ecc65 5993
f35a4dee 5994 set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
22001f61 5995 set_disk_ro(rbd_dev->disk, rbd_dev->mapping.read_only);
f35a4dee 5996
5769ed0c 5997 ret = dev_set_name(&rbd_dev->dev, "%d", rbd_dev->dev_id);
f35a4dee 5998 if (ret)
f5ee37bd 5999 goto err_out_mapping;
83a06263 6000
129b79d4 6001 set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
811c6688 6002 up_write(&rbd_dev->header_rwsem);
5769ed0c 6003 return 0;
2f82ee54 6004
f35a4dee
AE
6005err_out_mapping:
6006 rbd_dev_mapping_clear(rbd_dev);
83a06263
AE
6007err_out_disk:
6008 rbd_free_disk(rbd_dev);
6009err_out_blkdev:
9b60e70b
ID
6010 if (!single_major)
6011 unregister_blkdev(rbd_dev->major, rbd_dev->name);
811c6688
ID
6012err_out_unlock:
6013 up_write(&rbd_dev->header_rwsem);
83a06263
AE
6014 return ret;
6015}
6016
332bb12d
AE
6017static int rbd_dev_header_name(struct rbd_device *rbd_dev)
6018{
6019 struct rbd_spec *spec = rbd_dev->spec;
c41d13a3 6020 int ret;
332bb12d
AE
6021
6022 /* Record the header object name for this rbd image. */
6023
6024 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
332bb12d 6025 if (rbd_dev->image_format == 1)
c41d13a3
ID
6026 ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s",
6027 spec->image_name, RBD_SUFFIX);
332bb12d 6028 else
c41d13a3
ID
6029 ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s",
6030 RBD_HEADER_PREFIX, spec->image_id);
332bb12d 6031
c41d13a3 6032 return ret;
332bb12d
AE
6033}
6034
200a6a8b
AE
6035static void rbd_dev_image_release(struct rbd_device *rbd_dev)
6036{
6fd48b3b 6037 rbd_dev_unprobe(rbd_dev);
fd22aef8
ID
6038 if (rbd_dev->opts)
6039 rbd_unregister_watch(rbd_dev);
6fd48b3b
AE
6040 rbd_dev->image_format = 0;
6041 kfree(rbd_dev->spec->image_id);
6042 rbd_dev->spec->image_id = NULL;
200a6a8b
AE
6043}
6044
a30b71b9
AE
6045/*
6046 * Probe for the existence of the header object for the given rbd
1f3ef788
AE
6047 * device. If this image is the one being mapped (i.e., not a
6048 * parent), initiate a watch on its header object before using that
6049 * object to get detailed information about the rbd image.
a30b71b9 6050 */
6d69bb53 6051static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth)
a30b71b9
AE
6052{
6053 int ret;
6054
6055 /*
3abef3b3
AE
6056 * Get the id from the image id object. Unless there's an
6057 * error, rbd_dev->spec->image_id will be filled in with
6058 * a dynamically-allocated string, and rbd_dev->image_format
6059 * will be set to either 1 or 2.
a30b71b9
AE
6060 */
6061 ret = rbd_dev_image_id(rbd_dev);
6062 if (ret)
c0fba368 6063 return ret;
c0fba368 6064
332bb12d
AE
6065 ret = rbd_dev_header_name(rbd_dev);
6066 if (ret)
6067 goto err_out_format;
6068
6d69bb53 6069 if (!depth) {
99d16943 6070 ret = rbd_register_watch(rbd_dev);
1fe48023
ID
6071 if (ret) {
6072 if (ret == -ENOENT)
6073 pr_info("image %s/%s does not exist\n",
6074 rbd_dev->spec->pool_name,
6075 rbd_dev->spec->image_name);
c41d13a3 6076 goto err_out_format;
1fe48023 6077 }
1f3ef788 6078 }
b644de2b 6079
a720ae09 6080 ret = rbd_dev_header_info(rbd_dev);
5655c4d9 6081 if (ret)
b644de2b 6082 goto err_out_watch;
83a06263 6083
04077599
ID
6084 /*
6085 * If this image is the one being mapped, we have pool name and
6086 * id, image name and id, and snap name - need to fill snap id.
6087 * Otherwise this is a parent image, identified by pool, image
6088 * and snap ids - need to fill in names for those ids.
6089 */
6d69bb53 6090 if (!depth)
04077599
ID
6091 ret = rbd_spec_fill_snap_id(rbd_dev);
6092 else
6093 ret = rbd_spec_fill_names(rbd_dev);
1fe48023
ID
6094 if (ret) {
6095 if (ret == -ENOENT)
6096 pr_info("snap %s/%s@%s does not exist\n",
6097 rbd_dev->spec->pool_name,
6098 rbd_dev->spec->image_name,
6099 rbd_dev->spec->snap_name);
33dca39f 6100 goto err_out_probe;
1fe48023 6101 }
9bb81c9b 6102
e8f59b59
ID
6103 if (rbd_dev->header.features & RBD_FEATURE_LAYERING) {
6104 ret = rbd_dev_v2_parent_info(rbd_dev);
6105 if (ret)
6106 goto err_out_probe;
6107
6108 /*
6109 * Need to warn users if this image is the one being
6110 * mapped and has a parent.
6111 */
6d69bb53 6112 if (!depth && rbd_dev->parent_spec)
e8f59b59
ID
6113 rbd_warn(rbd_dev,
6114 "WARNING: kernel layering is EXPERIMENTAL!");
6115 }
6116
6d69bb53 6117 ret = rbd_dev_probe_parent(rbd_dev, depth);
30d60ba2
AE
6118 if (ret)
6119 goto err_out_probe;
6120
6121 dout("discovered format %u image, header name is %s\n",
c41d13a3 6122 rbd_dev->image_format, rbd_dev->header_oid.name);
30d60ba2 6123 return 0;
e8f59b59 6124
6fd48b3b
AE
6125err_out_probe:
6126 rbd_dev_unprobe(rbd_dev);
b644de2b 6127err_out_watch:
6d69bb53 6128 if (!depth)
99d16943 6129 rbd_unregister_watch(rbd_dev);
332bb12d
AE
6130err_out_format:
6131 rbd_dev->image_format = 0;
5655c4d9
AE
6132 kfree(rbd_dev->spec->image_id);
6133 rbd_dev->spec->image_id = NULL;
a30b71b9
AE
6134 return ret;
6135}
6136
9b60e70b
ID
6137static ssize_t do_rbd_add(struct bus_type *bus,
6138 const char *buf,
6139 size_t count)
602adf40 6140{
cb8627c7 6141 struct rbd_device *rbd_dev = NULL;
dc79b113 6142 struct ceph_options *ceph_opts = NULL;
4e9afeba 6143 struct rbd_options *rbd_opts = NULL;
859c31df 6144 struct rbd_spec *spec = NULL;
9d3997fd 6145 struct rbd_client *rbdc;
51344a38 6146 bool read_only;
b51c83c2 6147 int rc;
602adf40
YS
6148
6149 if (!try_module_get(THIS_MODULE))
6150 return -ENODEV;
6151
602adf40 6152 /* parse add command */
859c31df 6153 rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec);
dc79b113 6154 if (rc < 0)
dd5ac32d 6155 goto out;
78cea76e 6156
9d3997fd
AE
6157 rbdc = rbd_get_client(ceph_opts);
6158 if (IS_ERR(rbdc)) {
6159 rc = PTR_ERR(rbdc);
0ddebc0c 6160 goto err_out_args;
9d3997fd 6161 }
602adf40 6162
602adf40 6163 /* pick the pool */
30ba1f02 6164 rc = rbd_add_get_pool_id(rbdc, spec->pool_name);
1fe48023
ID
6165 if (rc < 0) {
6166 if (rc == -ENOENT)
6167 pr_info("pool %s does not exist\n", spec->pool_name);
602adf40 6168 goto err_out_client;
1fe48023 6169 }
c0cd10db 6170 spec->pool_id = (u64)rc;
859c31df 6171
d147543d 6172 rbd_dev = rbd_dev_create(rbdc, spec, rbd_opts);
b51c83c2
ID
6173 if (!rbd_dev) {
6174 rc = -ENOMEM;
bd4ba655 6175 goto err_out_client;
b51c83c2 6176 }
c53d5893
AE
6177 rbdc = NULL; /* rbd_dev now owns this */
6178 spec = NULL; /* rbd_dev now owns this */
d147543d 6179 rbd_opts = NULL; /* rbd_dev now owns this */
602adf40 6180
0d6d1e9c
MC
6181 rbd_dev->config_info = kstrdup(buf, GFP_KERNEL);
6182 if (!rbd_dev->config_info) {
6183 rc = -ENOMEM;
6184 goto err_out_rbd_dev;
6185 }
6186
811c6688 6187 down_write(&rbd_dev->header_rwsem);
6d69bb53 6188 rc = rbd_dev_image_probe(rbd_dev, 0);
0d6d1e9c
MC
6189 if (rc < 0) {
6190 up_write(&rbd_dev->header_rwsem);
c53d5893 6191 goto err_out_rbd_dev;
0d6d1e9c 6192 }
05fd6f6f 6193
7ce4eef7
AE
6194 /* If we are mapping a snapshot it must be marked read-only */
6195
d147543d 6196 read_only = rbd_dev->opts->read_only;
7ce4eef7
AE
6197 if (rbd_dev->spec->snap_id != CEPH_NOSNAP)
6198 read_only = true;
6199 rbd_dev->mapping.read_only = read_only;
6200
b536f69a 6201 rc = rbd_dev_device_setup(rbd_dev);
fd22aef8 6202 if (rc)
8b679ec5 6203 goto err_out_image_probe;
3abef3b3 6204
e010dd0a
ID
6205 if (rbd_dev->opts->exclusive) {
6206 rc = rbd_add_acquire_lock(rbd_dev);
6207 if (rc)
6208 goto err_out_device_setup;
3abef3b3
AE
6209 }
6210
5769ed0c
ID
6211 /* Everything's ready. Announce the disk to the world. */
6212
6213 rc = device_add(&rbd_dev->dev);
6214 if (rc)
e010dd0a 6215 goto err_out_image_lock;
5769ed0c
ID
6216
6217 add_disk(rbd_dev->disk);
6218 /* see rbd_init_disk() */
6219 blk_put_queue(rbd_dev->disk->queue);
6220
6221 spin_lock(&rbd_dev_list_lock);
6222 list_add_tail(&rbd_dev->node, &rbd_dev_list);
6223 spin_unlock(&rbd_dev_list_lock);
6224
6225 pr_info("%s: capacity %llu features 0x%llx\n", rbd_dev->disk->disk_name,
6226 (unsigned long long)get_capacity(rbd_dev->disk) << SECTOR_SHIFT,
6227 rbd_dev->header.features);
dd5ac32d
ID
6228 rc = count;
6229out:
6230 module_put(THIS_MODULE);
6231 return rc;
b536f69a 6232
e010dd0a
ID
6233err_out_image_lock:
6234 rbd_dev_image_unlock(rbd_dev);
5769ed0c
ID
6235err_out_device_setup:
6236 rbd_dev_device_release(rbd_dev);
8b679ec5
ID
6237err_out_image_probe:
6238 rbd_dev_image_release(rbd_dev);
c53d5893
AE
6239err_out_rbd_dev:
6240 rbd_dev_destroy(rbd_dev);
bd4ba655 6241err_out_client:
9d3997fd 6242 rbd_put_client(rbdc);
0ddebc0c 6243err_out_args:
859c31df 6244 rbd_spec_put(spec);
d147543d 6245 kfree(rbd_opts);
dd5ac32d 6246 goto out;
602adf40
YS
6247}
6248
9b60e70b
ID
6249static ssize_t rbd_add(struct bus_type *bus,
6250 const char *buf,
6251 size_t count)
6252{
6253 if (single_major)
6254 return -EINVAL;
6255
6256 return do_rbd_add(bus, buf, count);
6257}
6258
6259static ssize_t rbd_add_single_major(struct bus_type *bus,
6260 const char *buf,
6261 size_t count)
6262{
6263 return do_rbd_add(bus, buf, count);
6264}
6265
05a46afd
AE
6266static void rbd_dev_remove_parent(struct rbd_device *rbd_dev)
6267{
ad945fc1 6268 while (rbd_dev->parent) {
05a46afd
AE
6269 struct rbd_device *first = rbd_dev;
6270 struct rbd_device *second = first->parent;
6271 struct rbd_device *third;
6272
6273 /*
6274 * Follow to the parent with no grandparent and
6275 * remove it.
6276 */
6277 while (second && (third = second->parent)) {
6278 first = second;
6279 second = third;
6280 }
ad945fc1 6281 rbd_assert(second);
8ad42cd0 6282 rbd_dev_image_release(second);
8b679ec5 6283 rbd_dev_destroy(second);
ad945fc1
AE
6284 first->parent = NULL;
6285 first->parent_overlap = 0;
6286
6287 rbd_assert(first->parent_spec);
05a46afd
AE
6288 rbd_spec_put(first->parent_spec);
6289 first->parent_spec = NULL;
05a46afd
AE
6290 }
6291}
6292
9b60e70b
ID
6293static ssize_t do_rbd_remove(struct bus_type *bus,
6294 const char *buf,
6295 size_t count)
602adf40
YS
6296{
6297 struct rbd_device *rbd_dev = NULL;
751cc0e3
AE
6298 struct list_head *tmp;
6299 int dev_id;
0276dca6 6300 char opt_buf[6];
82a442d2 6301 bool already = false;
0276dca6 6302 bool force = false;
0d8189e1 6303 int ret;
602adf40 6304
0276dca6
MC
6305 dev_id = -1;
6306 opt_buf[0] = '\0';
6307 sscanf(buf, "%d %5s", &dev_id, opt_buf);
6308 if (dev_id < 0) {
6309 pr_err("dev_id out of range\n");
602adf40 6310 return -EINVAL;
0276dca6
MC
6311 }
6312 if (opt_buf[0] != '\0') {
6313 if (!strcmp(opt_buf, "force")) {
6314 force = true;
6315 } else {
6316 pr_err("bad remove option at '%s'\n", opt_buf);
6317 return -EINVAL;
6318 }
6319 }
602adf40 6320
751cc0e3
AE
6321 ret = -ENOENT;
6322 spin_lock(&rbd_dev_list_lock);
6323 list_for_each(tmp, &rbd_dev_list) {
6324 rbd_dev = list_entry(tmp, struct rbd_device, node);
6325 if (rbd_dev->dev_id == dev_id) {
6326 ret = 0;
6327 break;
6328 }
42382b70 6329 }
751cc0e3
AE
6330 if (!ret) {
6331 spin_lock_irq(&rbd_dev->lock);
0276dca6 6332 if (rbd_dev->open_count && !force)
751cc0e3
AE
6333 ret = -EBUSY;
6334 else
82a442d2
AE
6335 already = test_and_set_bit(RBD_DEV_FLAG_REMOVING,
6336 &rbd_dev->flags);
751cc0e3
AE
6337 spin_unlock_irq(&rbd_dev->lock);
6338 }
6339 spin_unlock(&rbd_dev_list_lock);
82a442d2 6340 if (ret < 0 || already)
1ba0f1e7 6341 return ret;
751cc0e3 6342
0276dca6
MC
6343 if (force) {
6344 /*
6345 * Prevent new IO from being queued and wait for existing
6346 * IO to complete/fail.
6347 */
6348 blk_mq_freeze_queue(rbd_dev->disk->queue);
6349 blk_set_queue_dying(rbd_dev->disk->queue);
6350 }
6351
5769ed0c
ID
6352 del_gendisk(rbd_dev->disk);
6353 spin_lock(&rbd_dev_list_lock);
6354 list_del_init(&rbd_dev->node);
6355 spin_unlock(&rbd_dev_list_lock);
6356 device_del(&rbd_dev->dev);
fca27065 6357
e010dd0a 6358 rbd_dev_image_unlock(rbd_dev);
dd5ac32d 6359 rbd_dev_device_release(rbd_dev);
8ad42cd0 6360 rbd_dev_image_release(rbd_dev);
8b679ec5 6361 rbd_dev_destroy(rbd_dev);
1ba0f1e7 6362 return count;
602adf40
YS
6363}
6364
9b60e70b
ID
6365static ssize_t rbd_remove(struct bus_type *bus,
6366 const char *buf,
6367 size_t count)
6368{
6369 if (single_major)
6370 return -EINVAL;
6371
6372 return do_rbd_remove(bus, buf, count);
6373}
6374
6375static ssize_t rbd_remove_single_major(struct bus_type *bus,
6376 const char *buf,
6377 size_t count)
6378{
6379 return do_rbd_remove(bus, buf, count);
6380}
6381
602adf40
YS
6382/*
6383 * create control files in sysfs
dfc5606d 6384 * /sys/bus/rbd/...
602adf40
YS
6385 */
6386static int rbd_sysfs_init(void)
6387{
dfc5606d 6388 int ret;
602adf40 6389
fed4c143 6390 ret = device_register(&rbd_root_dev);
21079786 6391 if (ret < 0)
dfc5606d 6392 return ret;
602adf40 6393
fed4c143
AE
6394 ret = bus_register(&rbd_bus_type);
6395 if (ret < 0)
6396 device_unregister(&rbd_root_dev);
602adf40 6397
602adf40
YS
6398 return ret;
6399}
6400
6401static void rbd_sysfs_cleanup(void)
6402{
dfc5606d 6403 bus_unregister(&rbd_bus_type);
fed4c143 6404 device_unregister(&rbd_root_dev);
602adf40
YS
6405}
6406
1c2a9dfe
AE
6407static int rbd_slab_init(void)
6408{
6409 rbd_assert(!rbd_img_request_cache);
03d94406 6410 rbd_img_request_cache = KMEM_CACHE(rbd_img_request, 0);
868311b1
AE
6411 if (!rbd_img_request_cache)
6412 return -ENOMEM;
6413
6414 rbd_assert(!rbd_obj_request_cache);
03d94406 6415 rbd_obj_request_cache = KMEM_CACHE(rbd_obj_request, 0);
78c2a44a
AE
6416 if (!rbd_obj_request_cache)
6417 goto out_err;
6418
6c696d85 6419 return 0;
1c2a9dfe 6420
6c696d85 6421out_err:
868311b1
AE
6422 kmem_cache_destroy(rbd_img_request_cache);
6423 rbd_img_request_cache = NULL;
1c2a9dfe
AE
6424 return -ENOMEM;
6425}
6426
6427static void rbd_slab_exit(void)
6428{
868311b1
AE
6429 rbd_assert(rbd_obj_request_cache);
6430 kmem_cache_destroy(rbd_obj_request_cache);
6431 rbd_obj_request_cache = NULL;
6432
1c2a9dfe
AE
6433 rbd_assert(rbd_img_request_cache);
6434 kmem_cache_destroy(rbd_img_request_cache);
6435 rbd_img_request_cache = NULL;
6436}
6437
cc344fa1 6438static int __init rbd_init(void)
602adf40
YS
6439{
6440 int rc;
6441
1e32d34c
AE
6442 if (!libceph_compatible(NULL)) {
6443 rbd_warn(NULL, "libceph incompatibility (quitting)");
1e32d34c
AE
6444 return -EINVAL;
6445 }
e1b4d96d 6446
1c2a9dfe 6447 rc = rbd_slab_init();
602adf40
YS
6448 if (rc)
6449 return rc;
e1b4d96d 6450
f5ee37bd
ID
6451 /*
6452 * The number of active work items is limited by the number of
f77303bd 6453 * rbd devices * queue depth, so leave @max_active at default.
f5ee37bd
ID
6454 */
6455 rbd_wq = alloc_workqueue(RBD_DRV_NAME, WQ_MEM_RECLAIM, 0);
6456 if (!rbd_wq) {
6457 rc = -ENOMEM;
6458 goto err_out_slab;
6459 }
6460
9b60e70b
ID
6461 if (single_major) {
6462 rbd_major = register_blkdev(0, RBD_DRV_NAME);
6463 if (rbd_major < 0) {
6464 rc = rbd_major;
f5ee37bd 6465 goto err_out_wq;
9b60e70b
ID
6466 }
6467 }
6468
1c2a9dfe
AE
6469 rc = rbd_sysfs_init();
6470 if (rc)
9b60e70b
ID
6471 goto err_out_blkdev;
6472
6473 if (single_major)
6474 pr_info("loaded (major %d)\n", rbd_major);
6475 else
6476 pr_info("loaded\n");
1c2a9dfe 6477
e1b4d96d
ID
6478 return 0;
6479
9b60e70b
ID
6480err_out_blkdev:
6481 if (single_major)
6482 unregister_blkdev(rbd_major, RBD_DRV_NAME);
f5ee37bd
ID
6483err_out_wq:
6484 destroy_workqueue(rbd_wq);
e1b4d96d
ID
6485err_out_slab:
6486 rbd_slab_exit();
1c2a9dfe 6487 return rc;
602adf40
YS
6488}
6489
cc344fa1 6490static void __exit rbd_exit(void)
602adf40 6491{
ffe312cf 6492 ida_destroy(&rbd_dev_id_ida);
602adf40 6493 rbd_sysfs_cleanup();
9b60e70b
ID
6494 if (single_major)
6495 unregister_blkdev(rbd_major, RBD_DRV_NAME);
f5ee37bd 6496 destroy_workqueue(rbd_wq);
1c2a9dfe 6497 rbd_slab_exit();
602adf40
YS
6498}
6499
6500module_init(rbd_init);
6501module_exit(rbd_exit);
6502
d552c619 6503MODULE_AUTHOR("Alex Elder <elder@inktank.com>");
602adf40
YS
6504MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
6505MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
602adf40
YS
6506/* following authorship retained from original osdblk.c */
6507MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
6508
90da258b 6509MODULE_DESCRIPTION("RADOS Block Device (RBD) driver");
602adf40 6510MODULE_LICENSE("GPL");