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