rbd: get parent spec for version 2 images
[deliverable/linux.git] / drivers / block / rbd.c
CommitLineData
602adf40
YS
1/*
2 rbd.c -- Export ceph rados objects as a Linux block device
3
4
5 based on drivers/block/osdblk.c:
6
7 Copyright 2009 Red Hat, Inc.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; see the file COPYING. If not, write to
20 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21
22
23
dfc5606d 24 For usage instructions, please refer to:
602adf40 25
dfc5606d 26 Documentation/ABI/testing/sysfs-bus-rbd
602adf40
YS
27
28 */
29
30#include <linux/ceph/libceph.h>
31#include <linux/ceph/osd_client.h>
32#include <linux/ceph/mon_client.h>
33#include <linux/ceph/decode.h>
59c2be1e 34#include <linux/parser.h>
602adf40
YS
35
36#include <linux/kernel.h>
37#include <linux/device.h>
38#include <linux/module.h>
39#include <linux/fs.h>
40#include <linux/blkdev.h>
41
42#include "rbd_types.h"
43
aafb230e
AE
44#define RBD_DEBUG /* Activate rbd_assert() calls */
45
593a9e7b
AE
46/*
47 * The basic unit of block I/O is a sector. It is interpreted in a
48 * number of contexts in Linux (blk, bio, genhd), but the default is
49 * universally 512 bytes. These symbols are just slightly more
50 * meaningful than the bare numbers they represent.
51 */
52#define SECTOR_SHIFT 9
53#define SECTOR_SIZE (1ULL << SECTOR_SHIFT)
54
df111be6
AE
55/* It might be useful to have this defined elsewhere too */
56
57#define U64_MAX ((u64) (~0ULL))
58
f0f8cef5
AE
59#define RBD_DRV_NAME "rbd"
60#define RBD_DRV_NAME_LONG "rbd (rados block device)"
602adf40
YS
61
62#define RBD_MINORS_PER_MAJOR 256 /* max minors per blkdev */
63
d4b125e9
AE
64#define RBD_SNAP_DEV_NAME_PREFIX "snap_"
65#define RBD_MAX_SNAP_NAME_LEN \
66 (NAME_MAX - (sizeof (RBD_SNAP_DEV_NAME_PREFIX) - 1))
67
35d489f9 68#define RBD_MAX_SNAP_COUNT 510 /* allows max snapc to fit in 4KB */
602adf40
YS
69#define RBD_MAX_OPT_LEN 1024
70
71#define RBD_SNAP_HEAD_NAME "-"
72
1e130199
AE
73#define RBD_IMAGE_ID_LEN_MAX 64
74#define RBD_OBJ_PREFIX_LEN_MAX 64
589d30e0 75
d889140c
AE
76/* Feature bits */
77
78#define RBD_FEATURE_LAYERING 1
79
80/* Features supported by this (client software) implementation. */
81
82#define RBD_FEATURES_ALL (0)
83
81a89793
AE
84/*
85 * An RBD device name will be "rbd#", where the "rbd" comes from
86 * RBD_DRV_NAME above, and # is a unique integer identifier.
87 * MAX_INT_FORMAT_WIDTH is used in ensuring DEV_NAME_LEN is big
88 * enough to hold all possible device names.
89 */
602adf40 90#define DEV_NAME_LEN 32
81a89793 91#define MAX_INT_FORMAT_WIDTH ((5 * sizeof (int)) / 2 + 1)
602adf40 92
cc0538b6 93#define RBD_READ_ONLY_DEFAULT false
59c2be1e 94
602adf40
YS
95/*
96 * block device image metadata (in-memory version)
97 */
98struct rbd_image_header {
f84344f3 99 /* These four fields never change for a given rbd image */
849b4260 100 char *object_prefix;
34b13184 101 u64 features;
602adf40
YS
102 __u8 obj_order;
103 __u8 crypt_type;
104 __u8 comp_type;
602adf40 105
f84344f3
AE
106 /* The remaining fields need to be updated occasionally */
107 u64 image_size;
108 struct ceph_snap_context *snapc;
602adf40
YS
109 char *snap_names;
110 u64 *snap_sizes;
59c2be1e
YS
111
112 u64 obj_version;
113};
114
0d7dbfce
AE
115/*
116 * An rbd image specification.
117 *
118 * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely
119 * identify an image.
120 */
121struct rbd_spec {
122 u64 pool_id;
123 char *pool_name;
124
125 char *image_id;
126 size_t image_id_len;
127 char *image_name;
128 size_t image_name_len;
129
130 u64 snap_id;
131 char *snap_name;
132
133 struct kref kref;
134};
135
59c2be1e 136struct rbd_options {
cc0538b6 137 bool read_only;
602adf40
YS
138};
139
140/*
f0f8cef5 141 * an instance of the client. multiple devices may share an rbd client.
602adf40
YS
142 */
143struct rbd_client {
144 struct ceph_client *client;
145 struct kref kref;
146 struct list_head node;
147};
148
149/*
f0f8cef5 150 * a request completion status
602adf40 151 */
1fec7093
YS
152struct rbd_req_status {
153 int done;
154 int rc;
155 u64 bytes;
156};
157
158/*
159 * a collection of requests
160 */
161struct rbd_req_coll {
162 int total;
163 int num_done;
164 struct kref kref;
165 struct rbd_req_status status[0];
602adf40
YS
166};
167
f0f8cef5
AE
168/*
169 * a single io request
170 */
171struct rbd_request {
172 struct request *rq; /* blk layer request */
173 struct bio *bio; /* cloned bio */
174 struct page **pages; /* list of used pages */
175 u64 len;
176 int coll_index;
177 struct rbd_req_coll *coll;
178};
179
dfc5606d
YS
180struct rbd_snap {
181 struct device dev;
182 const char *name;
3591538f 183 u64 size;
dfc5606d
YS
184 struct list_head node;
185 u64 id;
34b13184 186 u64 features;
dfc5606d
YS
187};
188
f84344f3 189struct rbd_mapping {
99c1f08f 190 u64 size;
34b13184 191 u64 features;
f84344f3
AE
192 bool read_only;
193};
194
602adf40
YS
195/*
196 * a single device
197 */
198struct rbd_device {
de71a297 199 int dev_id; /* blkdev unique id */
602adf40
YS
200
201 int major; /* blkdev assigned major */
202 struct gendisk *disk; /* blkdev's gendisk and rq */
602adf40 203
a30b71b9 204 u32 image_format; /* Either 1 or 2 */
602adf40
YS
205 struct rbd_client *rbd_client;
206
207 char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */
208
209 spinlock_t lock; /* queue lock */
210
211 struct rbd_image_header header;
daba5fdb 212 bool exists;
0d7dbfce 213 struct rbd_spec *spec;
602adf40 214
0d7dbfce 215 char *header_name;
971f839a 216
59c2be1e
YS
217 struct ceph_osd_event *watch_event;
218 struct ceph_osd_request *watch_request;
219
86b00e0d
AE
220 struct rbd_spec *parent_spec;
221 u64 parent_overlap;
222
c666601a
JD
223 /* protects updating the header */
224 struct rw_semaphore header_rwsem;
f84344f3
AE
225
226 struct rbd_mapping mapping;
602adf40
YS
227
228 struct list_head node;
dfc5606d
YS
229
230 /* list of snapshots */
231 struct list_head snaps;
232
233 /* sysfs related */
234 struct device dev;
235};
236
602adf40 237static DEFINE_MUTEX(ctl_mutex); /* Serialize open/close/setup/teardown */
e124a82f 238
602adf40 239static LIST_HEAD(rbd_dev_list); /* devices */
e124a82f
AE
240static DEFINE_SPINLOCK(rbd_dev_list_lock);
241
432b8587
AE
242static LIST_HEAD(rbd_client_list); /* clients */
243static DEFINE_SPINLOCK(rbd_client_list_lock);
602adf40 244
304f6808
AE
245static int rbd_dev_snaps_update(struct rbd_device *rbd_dev);
246static int rbd_dev_snaps_register(struct rbd_device *rbd_dev);
247
dfc5606d 248static void rbd_dev_release(struct device *dev);
41f38c2b 249static void rbd_remove_snap_dev(struct rbd_snap *snap);
dfc5606d 250
f0f8cef5
AE
251static ssize_t rbd_add(struct bus_type *bus, const char *buf,
252 size_t count);
253static ssize_t rbd_remove(struct bus_type *bus, const char *buf,
254 size_t count);
255
256static struct bus_attribute rbd_bus_attrs[] = {
257 __ATTR(add, S_IWUSR, NULL, rbd_add),
258 __ATTR(remove, S_IWUSR, NULL, rbd_remove),
259 __ATTR_NULL
260};
261
262static struct bus_type rbd_bus_type = {
263 .name = "rbd",
264 .bus_attrs = rbd_bus_attrs,
265};
266
267static void rbd_root_dev_release(struct device *dev)
268{
269}
270
271static struct device rbd_root_dev = {
272 .init_name = "rbd",
273 .release = rbd_root_dev_release,
274};
275
aafb230e
AE
276#ifdef RBD_DEBUG
277#define rbd_assert(expr) \
278 if (unlikely(!(expr))) { \
279 printk(KERN_ERR "\nAssertion failure in %s() " \
280 "at line %d:\n\n" \
281 "\trbd_assert(%s);\n\n", \
282 __func__, __LINE__, #expr); \
283 BUG(); \
284 }
285#else /* !RBD_DEBUG */
286# define rbd_assert(expr) ((void) 0)
287#endif /* !RBD_DEBUG */
dfc5606d 288
dfc5606d
YS
289static struct device *rbd_get_dev(struct rbd_device *rbd_dev)
290{
291 return get_device(&rbd_dev->dev);
292}
293
294static void rbd_put_dev(struct rbd_device *rbd_dev)
295{
296 put_device(&rbd_dev->dev);
297}
602adf40 298
117973fb
AE
299static int rbd_dev_refresh(struct rbd_device *rbd_dev, u64 *hver);
300static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev, u64 *hver);
59c2be1e 301
602adf40
YS
302static int rbd_open(struct block_device *bdev, fmode_t mode)
303{
f0f8cef5 304 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
602adf40 305
f84344f3 306 if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only)
602adf40
YS
307 return -EROFS;
308
340c7a2b 309 rbd_get_dev(rbd_dev);
f84344f3 310 set_device_ro(bdev, rbd_dev->mapping.read_only);
340c7a2b 311
602adf40
YS
312 return 0;
313}
314
dfc5606d
YS
315static int rbd_release(struct gendisk *disk, fmode_t mode)
316{
317 struct rbd_device *rbd_dev = disk->private_data;
318
319 rbd_put_dev(rbd_dev);
320
321 return 0;
322}
323
602adf40
YS
324static const struct block_device_operations rbd_bd_ops = {
325 .owner = THIS_MODULE,
326 .open = rbd_open,
dfc5606d 327 .release = rbd_release,
602adf40
YS
328};
329
330/*
331 * Initialize an rbd client instance.
43ae4701 332 * We own *ceph_opts.
602adf40 333 */
f8c38929 334static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts)
602adf40
YS
335{
336 struct rbd_client *rbdc;
337 int ret = -ENOMEM;
338
339 dout("rbd_client_create\n");
340 rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL);
341 if (!rbdc)
342 goto out_opt;
343
344 kref_init(&rbdc->kref);
345 INIT_LIST_HEAD(&rbdc->node);
346
bc534d86
AE
347 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
348
43ae4701 349 rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0);
602adf40 350 if (IS_ERR(rbdc->client))
bc534d86 351 goto out_mutex;
43ae4701 352 ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */
602adf40
YS
353
354 ret = ceph_open_session(rbdc->client);
355 if (ret < 0)
356 goto out_err;
357
432b8587 358 spin_lock(&rbd_client_list_lock);
602adf40 359 list_add_tail(&rbdc->node, &rbd_client_list);
432b8587 360 spin_unlock(&rbd_client_list_lock);
602adf40 361
bc534d86
AE
362 mutex_unlock(&ctl_mutex);
363
602adf40
YS
364 dout("rbd_client_create created %p\n", rbdc);
365 return rbdc;
366
367out_err:
368 ceph_destroy_client(rbdc->client);
bc534d86
AE
369out_mutex:
370 mutex_unlock(&ctl_mutex);
602adf40
YS
371 kfree(rbdc);
372out_opt:
43ae4701
AE
373 if (ceph_opts)
374 ceph_destroy_options(ceph_opts);
28f259b7 375 return ERR_PTR(ret);
602adf40
YS
376}
377
378/*
1f7ba331
AE
379 * Find a ceph client with specific addr and configuration. If
380 * found, bump its reference count.
602adf40 381 */
1f7ba331 382static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts)
602adf40
YS
383{
384 struct rbd_client *client_node;
1f7ba331 385 bool found = false;
602adf40 386
43ae4701 387 if (ceph_opts->flags & CEPH_OPT_NOSHARE)
602adf40
YS
388 return NULL;
389
1f7ba331
AE
390 spin_lock(&rbd_client_list_lock);
391 list_for_each_entry(client_node, &rbd_client_list, node) {
392 if (!ceph_compare_options(ceph_opts, client_node->client)) {
393 kref_get(&client_node->kref);
394 found = true;
395 break;
396 }
397 }
398 spin_unlock(&rbd_client_list_lock);
399
400 return found ? client_node : NULL;
602adf40
YS
401}
402
59c2be1e
YS
403/*
404 * mount options
405 */
406enum {
59c2be1e
YS
407 Opt_last_int,
408 /* int args above */
409 Opt_last_string,
410 /* string args above */
cc0538b6
AE
411 Opt_read_only,
412 Opt_read_write,
413 /* Boolean args above */
414 Opt_last_bool,
59c2be1e
YS
415};
416
43ae4701 417static match_table_t rbd_opts_tokens = {
59c2be1e
YS
418 /* int args above */
419 /* string args above */
be466c1c 420 {Opt_read_only, "read_only"},
cc0538b6
AE
421 {Opt_read_only, "ro"}, /* Alternate spelling */
422 {Opt_read_write, "read_write"},
423 {Opt_read_write, "rw"}, /* Alternate spelling */
424 /* Boolean args above */
59c2be1e
YS
425 {-1, NULL}
426};
427
428static int parse_rbd_opts_token(char *c, void *private)
429{
43ae4701 430 struct rbd_options *rbd_opts = private;
59c2be1e
YS
431 substring_t argstr[MAX_OPT_ARGS];
432 int token, intval, ret;
433
43ae4701 434 token = match_token(c, rbd_opts_tokens, argstr);
59c2be1e
YS
435 if (token < 0)
436 return -EINVAL;
437
438 if (token < Opt_last_int) {
439 ret = match_int(&argstr[0], &intval);
440 if (ret < 0) {
441 pr_err("bad mount option arg (not int) "
442 "at '%s'\n", c);
443 return ret;
444 }
445 dout("got int token %d val %d\n", token, intval);
446 } else if (token > Opt_last_int && token < Opt_last_string) {
447 dout("got string token %d val %s\n", token,
448 argstr[0].from);
cc0538b6
AE
449 } else if (token > Opt_last_string && token < Opt_last_bool) {
450 dout("got Boolean token %d\n", token);
59c2be1e
YS
451 } else {
452 dout("got token %d\n", token);
453 }
454
455 switch (token) {
cc0538b6
AE
456 case Opt_read_only:
457 rbd_opts->read_only = true;
458 break;
459 case Opt_read_write:
460 rbd_opts->read_only = false;
461 break;
59c2be1e 462 default:
aafb230e
AE
463 rbd_assert(false);
464 break;
59c2be1e
YS
465 }
466 return 0;
467}
468
602adf40
YS
469/*
470 * Get a ceph client with specific addr and configuration, if one does
471 * not exist create it.
472 */
9d3997fd 473static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts)
602adf40 474{
f8c38929 475 struct rbd_client *rbdc;
59c2be1e 476
1f7ba331 477 rbdc = rbd_client_find(ceph_opts);
9d3997fd 478 if (rbdc) /* using an existing client */
43ae4701 479 ceph_destroy_options(ceph_opts);
9d3997fd 480 else
f8c38929 481 rbdc = rbd_client_create(ceph_opts);
602adf40 482
9d3997fd 483 return rbdc;
602adf40
YS
484}
485
486/*
487 * Destroy ceph client
d23a4b3f 488 *
432b8587 489 * Caller must hold rbd_client_list_lock.
602adf40
YS
490 */
491static void rbd_client_release(struct kref *kref)
492{
493 struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
494
495 dout("rbd_release_client %p\n", rbdc);
cd9d9f5d 496 spin_lock(&rbd_client_list_lock);
602adf40 497 list_del(&rbdc->node);
cd9d9f5d 498 spin_unlock(&rbd_client_list_lock);
602adf40
YS
499
500 ceph_destroy_client(rbdc->client);
501 kfree(rbdc);
502}
503
504/*
505 * Drop reference to ceph client node. If it's not referenced anymore, release
506 * it.
507 */
9d3997fd 508static void rbd_put_client(struct rbd_client *rbdc)
602adf40 509{
c53d5893
AE
510 if (rbdc)
511 kref_put(&rbdc->kref, rbd_client_release);
602adf40
YS
512}
513
1fec7093
YS
514/*
515 * Destroy requests collection
516 */
517static void rbd_coll_release(struct kref *kref)
518{
519 struct rbd_req_coll *coll =
520 container_of(kref, struct rbd_req_coll, kref);
521
522 dout("rbd_coll_release %p\n", coll);
523 kfree(coll);
524}
602adf40 525
a30b71b9
AE
526static bool rbd_image_format_valid(u32 image_format)
527{
528 return image_format == 1 || image_format == 2;
529}
530
8e94af8e
AE
531static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
532{
103a150f
AE
533 size_t size;
534 u32 snap_count;
535
536 /* The header has to start with the magic rbd header text */
537 if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT)))
538 return false;
539
db2388b6
AE
540 /* The bio layer requires at least sector-sized I/O */
541
542 if (ondisk->options.order < SECTOR_SHIFT)
543 return false;
544
545 /* If we use u64 in a few spots we may be able to loosen this */
546
547 if (ondisk->options.order > 8 * sizeof (int) - 1)
548 return false;
549
103a150f
AE
550 /*
551 * The size of a snapshot header has to fit in a size_t, and
552 * that limits the number of snapshots.
553 */
554 snap_count = le32_to_cpu(ondisk->snap_count);
555 size = SIZE_MAX - sizeof (struct ceph_snap_context);
556 if (snap_count > size / sizeof (__le64))
557 return false;
558
559 /*
560 * Not only that, but the size of the entire the snapshot
561 * header must also be representable in a size_t.
562 */
563 size -= snap_count * sizeof (__le64);
564 if ((u64) size < le64_to_cpu(ondisk->snap_names_len))
565 return false;
566
567 return true;
8e94af8e
AE
568}
569
602adf40
YS
570/*
571 * Create a new header structure, translate header format from the on-disk
572 * header.
573 */
574static int rbd_header_from_disk(struct rbd_image_header *header,
4156d998 575 struct rbd_image_header_ondisk *ondisk)
602adf40 576{
ccece235 577 u32 snap_count;
58c17b0e 578 size_t len;
d2bb24e5 579 size_t size;
621901d6 580 u32 i;
602adf40 581
6a52325f
AE
582 memset(header, 0, sizeof (*header));
583
103a150f
AE
584 snap_count = le32_to_cpu(ondisk->snap_count);
585
58c17b0e
AE
586 len = strnlen(ondisk->object_prefix, sizeof (ondisk->object_prefix));
587 header->object_prefix = kmalloc(len + 1, GFP_KERNEL);
6a52325f 588 if (!header->object_prefix)
602adf40 589 return -ENOMEM;
58c17b0e
AE
590 memcpy(header->object_prefix, ondisk->object_prefix, len);
591 header->object_prefix[len] = '\0';
00f1f36f 592
602adf40 593 if (snap_count) {
f785cc1d
AE
594 u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len);
595
621901d6
AE
596 /* Save a copy of the snapshot names */
597
f785cc1d
AE
598 if (snap_names_len > (u64) SIZE_MAX)
599 return -EIO;
600 header->snap_names = kmalloc(snap_names_len, GFP_KERNEL);
602adf40 601 if (!header->snap_names)
6a52325f 602 goto out_err;
f785cc1d
AE
603 /*
604 * Note that rbd_dev_v1_header_read() guarantees
605 * the ondisk buffer we're working with has
606 * snap_names_len bytes beyond the end of the
607 * snapshot id array, this memcpy() is safe.
608 */
609 memcpy(header->snap_names, &ondisk->snaps[snap_count],
610 snap_names_len);
6a52325f 611
621901d6
AE
612 /* Record each snapshot's size */
613
d2bb24e5
AE
614 size = snap_count * sizeof (*header->snap_sizes);
615 header->snap_sizes = kmalloc(size, GFP_KERNEL);
602adf40 616 if (!header->snap_sizes)
6a52325f 617 goto out_err;
621901d6
AE
618 for (i = 0; i < snap_count; i++)
619 header->snap_sizes[i] =
620 le64_to_cpu(ondisk->snaps[i].image_size);
602adf40 621 } else {
ccece235 622 WARN_ON(ondisk->snap_names_len);
602adf40
YS
623 header->snap_names = NULL;
624 header->snap_sizes = NULL;
625 }
849b4260 626
34b13184 627 header->features = 0; /* No features support in v1 images */
602adf40
YS
628 header->obj_order = ondisk->options.order;
629 header->crypt_type = ondisk->options.crypt_type;
630 header->comp_type = ondisk->options.comp_type;
6a52325f 631
621901d6
AE
632 /* Allocate and fill in the snapshot context */
633
f84344f3 634 header->image_size = le64_to_cpu(ondisk->image_size);
6a52325f
AE
635 size = sizeof (struct ceph_snap_context);
636 size += snap_count * sizeof (header->snapc->snaps[0]);
637 header->snapc = kzalloc(size, GFP_KERNEL);
638 if (!header->snapc)
639 goto out_err;
602adf40
YS
640
641 atomic_set(&header->snapc->nref, 1);
505cbb9b 642 header->snapc->seq = le64_to_cpu(ondisk->snap_seq);
602adf40 643 header->snapc->num_snaps = snap_count;
621901d6
AE
644 for (i = 0; i < snap_count; i++)
645 header->snapc->snaps[i] =
646 le64_to_cpu(ondisk->snaps[i].id);
602adf40
YS
647
648 return 0;
649
6a52325f 650out_err:
849b4260 651 kfree(header->snap_sizes);
ccece235 652 header->snap_sizes = NULL;
602adf40 653 kfree(header->snap_names);
ccece235 654 header->snap_names = NULL;
6a52325f
AE
655 kfree(header->object_prefix);
656 header->object_prefix = NULL;
ccece235 657
00f1f36f 658 return -ENOMEM;
602adf40
YS
659}
660
8836b995 661static int snap_by_name(struct rbd_device *rbd_dev, const char *snap_name)
602adf40 662{
602adf40 663
e86924a8 664 struct rbd_snap *snap;
602adf40 665
e86924a8
AE
666 list_for_each_entry(snap, &rbd_dev->snaps, node) {
667 if (!strcmp(snap_name, snap->name)) {
0d7dbfce 668 rbd_dev->spec->snap_id = snap->id;
e86924a8 669 rbd_dev->mapping.size = snap->size;
34b13184 670 rbd_dev->mapping.features = snap->features;
602adf40 671
e86924a8 672 return 0;
00f1f36f 673 }
00f1f36f 674 }
e86924a8 675
00f1f36f 676 return -ENOENT;
602adf40
YS
677}
678
819d52bf 679static int rbd_dev_set_mapping(struct rbd_device *rbd_dev)
602adf40 680{
78dc447d 681 int ret;
602adf40 682
0d7dbfce 683 if (!memcmp(rbd_dev->spec->snap_name, RBD_SNAP_HEAD_NAME,
cc9d734c 684 sizeof (RBD_SNAP_HEAD_NAME))) {
0d7dbfce 685 rbd_dev->spec->snap_id = CEPH_NOSNAP;
99c1f08f 686 rbd_dev->mapping.size = rbd_dev->header.image_size;
34b13184 687 rbd_dev->mapping.features = rbd_dev->header.features;
e86924a8 688 ret = 0;
602adf40 689 } else {
0d7dbfce 690 ret = snap_by_name(rbd_dev, rbd_dev->spec->snap_name);
602adf40
YS
691 if (ret < 0)
692 goto done;
f84344f3 693 rbd_dev->mapping.read_only = true;
602adf40 694 }
daba5fdb 695 rbd_dev->exists = true;
602adf40 696done:
602adf40
YS
697 return ret;
698}
699
700static void rbd_header_free(struct rbd_image_header *header)
701{
849b4260 702 kfree(header->object_prefix);
d78fd7ae 703 header->object_prefix = NULL;
602adf40 704 kfree(header->snap_sizes);
d78fd7ae 705 header->snap_sizes = NULL;
849b4260 706 kfree(header->snap_names);
d78fd7ae 707 header->snap_names = NULL;
d1d25646 708 ceph_put_snap_context(header->snapc);
d78fd7ae 709 header->snapc = NULL;
602adf40
YS
710}
711
65ccfe21 712static char *rbd_segment_name(struct rbd_device *rbd_dev, u64 offset)
602adf40 713{
65ccfe21
AE
714 char *name;
715 u64 segment;
716 int ret;
602adf40 717
65ccfe21
AE
718 name = kmalloc(RBD_MAX_SEG_NAME_LEN + 1, GFP_NOIO);
719 if (!name)
720 return NULL;
721 segment = offset >> rbd_dev->header.obj_order;
722 ret = snprintf(name, RBD_MAX_SEG_NAME_LEN, "%s.%012llx",
723 rbd_dev->header.object_prefix, segment);
724 if (ret < 0 || ret >= RBD_MAX_SEG_NAME_LEN) {
725 pr_err("error formatting segment name for #%llu (%d)\n",
726 segment, ret);
727 kfree(name);
728 name = NULL;
729 }
602adf40 730
65ccfe21
AE
731 return name;
732}
602adf40 733
65ccfe21
AE
734static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset)
735{
736 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
602adf40 737
65ccfe21
AE
738 return offset & (segment_size - 1);
739}
740
741static u64 rbd_segment_length(struct rbd_device *rbd_dev,
742 u64 offset, u64 length)
743{
744 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
745
746 offset &= segment_size - 1;
747
aafb230e 748 rbd_assert(length <= U64_MAX - offset);
65ccfe21
AE
749 if (offset + length > segment_size)
750 length = segment_size - offset;
751
752 return length;
602adf40
YS
753}
754
1fec7093
YS
755static int rbd_get_num_segments(struct rbd_image_header *header,
756 u64 ofs, u64 len)
757{
df111be6
AE
758 u64 start_seg;
759 u64 end_seg;
760
761 if (!len)
762 return 0;
763 if (len - 1 > U64_MAX - ofs)
764 return -ERANGE;
765
766 start_seg = ofs >> header->obj_order;
767 end_seg = (ofs + len - 1) >> header->obj_order;
768
1fec7093
YS
769 return end_seg - start_seg + 1;
770}
771
029bcbd8
JD
772/*
773 * returns the size of an object in the image
774 */
775static u64 rbd_obj_bytes(struct rbd_image_header *header)
776{
777 return 1 << header->obj_order;
778}
779
602adf40
YS
780/*
781 * bio helpers
782 */
783
784static void bio_chain_put(struct bio *chain)
785{
786 struct bio *tmp;
787
788 while (chain) {
789 tmp = chain;
790 chain = chain->bi_next;
791 bio_put(tmp);
792 }
793}
794
795/*
796 * zeros a bio chain, starting at specific offset
797 */
798static void zero_bio_chain(struct bio *chain, int start_ofs)
799{
800 struct bio_vec *bv;
801 unsigned long flags;
802 void *buf;
803 int i;
804 int pos = 0;
805
806 while (chain) {
807 bio_for_each_segment(bv, chain, i) {
808 if (pos + bv->bv_len > start_ofs) {
809 int remainder = max(start_ofs - pos, 0);
810 buf = bvec_kmap_irq(bv, &flags);
811 memset(buf + remainder, 0,
812 bv->bv_len - remainder);
85b5aaa6 813 bvec_kunmap_irq(buf, &flags);
602adf40
YS
814 }
815 pos += bv->bv_len;
816 }
817
818 chain = chain->bi_next;
819 }
820}
821
822/*
f7760dad
AE
823 * Clone a portion of a bio, starting at the given byte offset
824 * and continuing for the number of bytes indicated.
602adf40 825 */
f7760dad
AE
826static struct bio *bio_clone_range(struct bio *bio_src,
827 unsigned int offset,
828 unsigned int len,
829 gfp_t gfpmask)
602adf40 830{
f7760dad
AE
831 struct bio_vec *bv;
832 unsigned int resid;
833 unsigned short idx;
834 unsigned int voff;
835 unsigned short end_idx;
836 unsigned short vcnt;
837 struct bio *bio;
838
839 /* Handle the easy case for the caller */
840
841 if (!offset && len == bio_src->bi_size)
842 return bio_clone(bio_src, gfpmask);
843
844 if (WARN_ON_ONCE(!len))
845 return NULL;
846 if (WARN_ON_ONCE(len > bio_src->bi_size))
847 return NULL;
848 if (WARN_ON_ONCE(offset > bio_src->bi_size - len))
849 return NULL;
850
851 /* Find first affected segment... */
852
853 resid = offset;
854 __bio_for_each_segment(bv, bio_src, idx, 0) {
855 if (resid < bv->bv_len)
856 break;
857 resid -= bv->bv_len;
602adf40 858 }
f7760dad 859 voff = resid;
602adf40 860
f7760dad 861 /* ...and the last affected segment */
602adf40 862
f7760dad
AE
863 resid += len;
864 __bio_for_each_segment(bv, bio_src, end_idx, idx) {
865 if (resid <= bv->bv_len)
866 break;
867 resid -= bv->bv_len;
868 }
869 vcnt = end_idx - idx + 1;
870
871 /* Build the clone */
872
873 bio = bio_alloc(gfpmask, (unsigned int) vcnt);
874 if (!bio)
875 return NULL; /* ENOMEM */
602adf40 876
f7760dad
AE
877 bio->bi_bdev = bio_src->bi_bdev;
878 bio->bi_sector = bio_src->bi_sector + (offset >> SECTOR_SHIFT);
879 bio->bi_rw = bio_src->bi_rw;
880 bio->bi_flags |= 1 << BIO_CLONED;
881
882 /*
883 * Copy over our part of the bio_vec, then update the first
884 * and last (or only) entries.
885 */
886 memcpy(&bio->bi_io_vec[0], &bio_src->bi_io_vec[idx],
887 vcnt * sizeof (struct bio_vec));
888 bio->bi_io_vec[0].bv_offset += voff;
889 if (vcnt > 1) {
890 bio->bi_io_vec[0].bv_len -= voff;
891 bio->bi_io_vec[vcnt - 1].bv_len = resid;
892 } else {
893 bio->bi_io_vec[0].bv_len = len;
602adf40
YS
894 }
895
f7760dad
AE
896 bio->bi_vcnt = vcnt;
897 bio->bi_size = len;
898 bio->bi_idx = 0;
899
900 return bio;
901}
902
903/*
904 * Clone a portion of a bio chain, starting at the given byte offset
905 * into the first bio in the source chain and continuing for the
906 * number of bytes indicated. The result is another bio chain of
907 * exactly the given length, or a null pointer on error.
908 *
909 * The bio_src and offset parameters are both in-out. On entry they
910 * refer to the first source bio and the offset into that bio where
911 * the start of data to be cloned is located.
912 *
913 * On return, bio_src is updated to refer to the bio in the source
914 * chain that contains first un-cloned byte, and *offset will
915 * contain the offset of that byte within that bio.
916 */
917static struct bio *bio_chain_clone_range(struct bio **bio_src,
918 unsigned int *offset,
919 unsigned int len,
920 gfp_t gfpmask)
921{
922 struct bio *bi = *bio_src;
923 unsigned int off = *offset;
924 struct bio *chain = NULL;
925 struct bio **end;
926
927 /* Build up a chain of clone bios up to the limit */
928
929 if (!bi || off >= bi->bi_size || !len)
930 return NULL; /* Nothing to clone */
602adf40 931
f7760dad
AE
932 end = &chain;
933 while (len) {
934 unsigned int bi_size;
935 struct bio *bio;
936
937 if (!bi)
938 goto out_err; /* EINVAL; ran out of bio's */
939 bi_size = min_t(unsigned int, bi->bi_size - off, len);
940 bio = bio_clone_range(bi, off, bi_size, gfpmask);
941 if (!bio)
942 goto out_err; /* ENOMEM */
943
944 *end = bio;
945 end = &bio->bi_next;
602adf40 946
f7760dad
AE
947 off += bi_size;
948 if (off == bi->bi_size) {
949 bi = bi->bi_next;
950 off = 0;
951 }
952 len -= bi_size;
953 }
954 *bio_src = bi;
955 *offset = off;
956
957 return chain;
958out_err:
959 bio_chain_put(chain);
602adf40 960
602adf40
YS
961 return NULL;
962}
963
964/*
965 * helpers for osd request op vectors.
966 */
57cfc106
AE
967static struct ceph_osd_req_op *rbd_create_rw_ops(int num_ops,
968 int opcode, u32 payload_len)
602adf40 969{
57cfc106
AE
970 struct ceph_osd_req_op *ops;
971
972 ops = kzalloc(sizeof (*ops) * (num_ops + 1), GFP_NOIO);
973 if (!ops)
974 return NULL;
975
976 ops[0].op = opcode;
977
602adf40
YS
978 /*
979 * op extent offset and length will be set later on
980 * in calc_raw_layout()
981 */
57cfc106
AE
982 ops[0].payload_len = payload_len;
983
984 return ops;
602adf40
YS
985}
986
987static void rbd_destroy_ops(struct ceph_osd_req_op *ops)
988{
989 kfree(ops);
990}
991
1fec7093
YS
992static void rbd_coll_end_req_index(struct request *rq,
993 struct rbd_req_coll *coll,
994 int index,
995 int ret, u64 len)
996{
997 struct request_queue *q;
998 int min, max, i;
999
bd919d45
AE
1000 dout("rbd_coll_end_req_index %p index %d ret %d len %llu\n",
1001 coll, index, ret, (unsigned long long) len);
1fec7093
YS
1002
1003 if (!rq)
1004 return;
1005
1006 if (!coll) {
1007 blk_end_request(rq, ret, len);
1008 return;
1009 }
1010
1011 q = rq->q;
1012
1013 spin_lock_irq(q->queue_lock);
1014 coll->status[index].done = 1;
1015 coll->status[index].rc = ret;
1016 coll->status[index].bytes = len;
1017 max = min = coll->num_done;
1018 while (max < coll->total && coll->status[max].done)
1019 max++;
1020
1021 for (i = min; i<max; i++) {
1022 __blk_end_request(rq, coll->status[i].rc,
1023 coll->status[i].bytes);
1024 coll->num_done++;
1025 kref_put(&coll->kref, rbd_coll_release);
1026 }
1027 spin_unlock_irq(q->queue_lock);
1028}
1029
1030static void rbd_coll_end_req(struct rbd_request *req,
1031 int ret, u64 len)
1032{
1033 rbd_coll_end_req_index(req->rq, req->coll, req->coll_index, ret, len);
1034}
1035
602adf40
YS
1036/*
1037 * Send ceph osd request
1038 */
1039static int rbd_do_request(struct request *rq,
0ce1a794 1040 struct rbd_device *rbd_dev,
602adf40
YS
1041 struct ceph_snap_context *snapc,
1042 u64 snapid,
aded07ea 1043 const char *object_name, u64 ofs, u64 len,
602adf40
YS
1044 struct bio *bio,
1045 struct page **pages,
1046 int num_pages,
1047 int flags,
1048 struct ceph_osd_req_op *ops,
1fec7093
YS
1049 struct rbd_req_coll *coll,
1050 int coll_index,
602adf40 1051 void (*rbd_cb)(struct ceph_osd_request *req,
59c2be1e
YS
1052 struct ceph_msg *msg),
1053 struct ceph_osd_request **linger_req,
1054 u64 *ver)
602adf40
YS
1055{
1056 struct ceph_osd_request *req;
1057 struct ceph_file_layout *layout;
1058 int ret;
1059 u64 bno;
1060 struct timespec mtime = CURRENT_TIME;
1061 struct rbd_request *req_data;
1062 struct ceph_osd_request_head *reqhead;
1dbb4399 1063 struct ceph_osd_client *osdc;
602adf40 1064
602adf40 1065 req_data = kzalloc(sizeof(*req_data), GFP_NOIO);
1fec7093
YS
1066 if (!req_data) {
1067 if (coll)
1068 rbd_coll_end_req_index(rq, coll, coll_index,
1069 -ENOMEM, len);
1070 return -ENOMEM;
1071 }
1072
1073 if (coll) {
1074 req_data->coll = coll;
1075 req_data->coll_index = coll_index;
1076 }
602adf40 1077
f7760dad
AE
1078 dout("rbd_do_request object_name=%s ofs=%llu len=%llu coll=%p[%d]\n",
1079 object_name, (unsigned long long) ofs,
1080 (unsigned long long) len, coll, coll_index);
602adf40 1081
0ce1a794 1082 osdc = &rbd_dev->rbd_client->client->osdc;
1dbb4399
AE
1083 req = ceph_osdc_alloc_request(osdc, flags, snapc, ops,
1084 false, GFP_NOIO, pages, bio);
4ad12621 1085 if (!req) {
4ad12621 1086 ret = -ENOMEM;
602adf40
YS
1087 goto done_pages;
1088 }
1089
1090 req->r_callback = rbd_cb;
1091
1092 req_data->rq = rq;
1093 req_data->bio = bio;
1094 req_data->pages = pages;
1095 req_data->len = len;
1096
1097 req->r_priv = req_data;
1098
1099 reqhead = req->r_request->front.iov_base;
1100 reqhead->snapid = cpu_to_le64(CEPH_NOSNAP);
1101
aded07ea 1102 strncpy(req->r_oid, object_name, sizeof(req->r_oid));
602adf40
YS
1103 req->r_oid_len = strlen(req->r_oid);
1104
1105 layout = &req->r_file_layout;
1106 memset(layout, 0, sizeof(*layout));
1107 layout->fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
1108 layout->fl_stripe_count = cpu_to_le32(1);
1109 layout->fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
0d7dbfce 1110 layout->fl_pg_pool = cpu_to_le32((int) rbd_dev->spec->pool_id);
6cae3717
SW
1111 ret = ceph_calc_raw_layout(osdc, layout, snapid, ofs, &len, &bno,
1112 req, ops);
1113 rbd_assert(ret == 0);
602adf40
YS
1114
1115 ceph_osdc_build_request(req, ofs, &len,
1116 ops,
1117 snapc,
1118 &mtime,
1119 req->r_oid, req->r_oid_len);
602adf40 1120
59c2be1e 1121 if (linger_req) {
1dbb4399 1122 ceph_osdc_set_request_linger(osdc, req);
59c2be1e
YS
1123 *linger_req = req;
1124 }
1125
1dbb4399 1126 ret = ceph_osdc_start_request(osdc, req, false);
602adf40
YS
1127 if (ret < 0)
1128 goto done_err;
1129
1130 if (!rbd_cb) {
1dbb4399 1131 ret = ceph_osdc_wait_request(osdc, req);
59c2be1e
YS
1132 if (ver)
1133 *ver = le64_to_cpu(req->r_reassert_version.version);
bd919d45
AE
1134 dout("reassert_ver=%llu\n",
1135 (unsigned long long)
1136 le64_to_cpu(req->r_reassert_version.version));
602adf40
YS
1137 ceph_osdc_put_request(req);
1138 }
1139 return ret;
1140
1141done_err:
1142 bio_chain_put(req_data->bio);
1143 ceph_osdc_put_request(req);
1144done_pages:
1fec7093 1145 rbd_coll_end_req(req_data, ret, len);
602adf40 1146 kfree(req_data);
602adf40
YS
1147 return ret;
1148}
1149
1150/*
1151 * Ceph osd op callback
1152 */
1153static void rbd_req_cb(struct ceph_osd_request *req, struct ceph_msg *msg)
1154{
1155 struct rbd_request *req_data = req->r_priv;
1156 struct ceph_osd_reply_head *replyhead;
1157 struct ceph_osd_op *op;
1158 __s32 rc;
1159 u64 bytes;
1160 int read_op;
1161
1162 /* parse reply */
1163 replyhead = msg->front.iov_base;
1164 WARN_ON(le32_to_cpu(replyhead->num_ops) == 0);
1165 op = (void *)(replyhead + 1);
1166 rc = le32_to_cpu(replyhead->result);
1167 bytes = le64_to_cpu(op->extent.length);
895cfcc8 1168 read_op = (le16_to_cpu(op->op) == CEPH_OSD_OP_READ);
602adf40 1169
bd919d45
AE
1170 dout("rbd_req_cb bytes=%llu readop=%d rc=%d\n",
1171 (unsigned long long) bytes, read_op, (int) rc);
602adf40
YS
1172
1173 if (rc == -ENOENT && read_op) {
1174 zero_bio_chain(req_data->bio, 0);
1175 rc = 0;
1176 } else if (rc == 0 && read_op && bytes < req_data->len) {
1177 zero_bio_chain(req_data->bio, bytes);
1178 bytes = req_data->len;
1179 }
1180
1fec7093 1181 rbd_coll_end_req(req_data, rc, bytes);
602adf40
YS
1182
1183 if (req_data->bio)
1184 bio_chain_put(req_data->bio);
1185
1186 ceph_osdc_put_request(req);
1187 kfree(req_data);
1188}
1189
59c2be1e
YS
1190static void rbd_simple_req_cb(struct ceph_osd_request *req, struct ceph_msg *msg)
1191{
1192 ceph_osdc_put_request(req);
1193}
1194
602adf40
YS
1195/*
1196 * Do a synchronous ceph osd operation
1197 */
0ce1a794 1198static int rbd_req_sync_op(struct rbd_device *rbd_dev,
602adf40
YS
1199 struct ceph_snap_context *snapc,
1200 u64 snapid,
602adf40 1201 int flags,
913d2fdc 1202 struct ceph_osd_req_op *ops,
aded07ea 1203 const char *object_name,
f8d4de6e
AE
1204 u64 ofs, u64 inbound_size,
1205 char *inbound,
59c2be1e
YS
1206 struct ceph_osd_request **linger_req,
1207 u64 *ver)
602adf40
YS
1208{
1209 int ret;
1210 struct page **pages;
1211 int num_pages;
913d2fdc 1212
aafb230e 1213 rbd_assert(ops != NULL);
602adf40 1214
f8d4de6e 1215 num_pages = calc_pages_for(ofs, inbound_size);
602adf40 1216 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
b8d0638a
DC
1217 if (IS_ERR(pages))
1218 return PTR_ERR(pages);
602adf40 1219
0ce1a794 1220 ret = rbd_do_request(NULL, rbd_dev, snapc, snapid,
f8d4de6e 1221 object_name, ofs, inbound_size, NULL,
602adf40
YS
1222 pages, num_pages,
1223 flags,
1224 ops,
1fec7093 1225 NULL, 0,
59c2be1e
YS
1226 NULL,
1227 linger_req, ver);
602adf40 1228 if (ret < 0)
913d2fdc 1229 goto done;
602adf40 1230
f8d4de6e
AE
1231 if ((flags & CEPH_OSD_FLAG_READ) && inbound)
1232 ret = ceph_copy_from_page_vector(pages, inbound, ofs, ret);
602adf40 1233
602adf40
YS
1234done:
1235 ceph_release_page_vector(pages, num_pages);
1236 return ret;
1237}
1238
1239/*
1240 * Do an asynchronous ceph osd operation
1241 */
1242static int rbd_do_op(struct request *rq,
0ce1a794 1243 struct rbd_device *rbd_dev,
602adf40 1244 struct ceph_snap_context *snapc,
602adf40 1245 u64 ofs, u64 len,
1fec7093
YS
1246 struct bio *bio,
1247 struct rbd_req_coll *coll,
1248 int coll_index)
602adf40
YS
1249{
1250 char *seg_name;
1251 u64 seg_ofs;
1252 u64 seg_len;
1253 int ret;
1254 struct ceph_osd_req_op *ops;
1255 u32 payload_len;
ff2e4bb5
AE
1256 int opcode;
1257 int flags;
4634246d 1258 u64 snapid;
602adf40 1259
65ccfe21 1260 seg_name = rbd_segment_name(rbd_dev, ofs);
602adf40
YS
1261 if (!seg_name)
1262 return -ENOMEM;
65ccfe21
AE
1263 seg_len = rbd_segment_length(rbd_dev, ofs, len);
1264 seg_ofs = rbd_segment_offset(rbd_dev, ofs);
602adf40 1265
ff2e4bb5
AE
1266 if (rq_data_dir(rq) == WRITE) {
1267 opcode = CEPH_OSD_OP_WRITE;
1268 flags = CEPH_OSD_FLAG_WRITE|CEPH_OSD_FLAG_ONDISK;
4634246d 1269 snapid = CEPH_NOSNAP;
ff2e4bb5
AE
1270 payload_len = seg_len;
1271 } else {
1272 opcode = CEPH_OSD_OP_READ;
1273 flags = CEPH_OSD_FLAG_READ;
4634246d 1274 snapc = NULL;
0d7dbfce 1275 snapid = rbd_dev->spec->snap_id;
ff2e4bb5
AE
1276 payload_len = 0;
1277 }
602adf40 1278
57cfc106
AE
1279 ret = -ENOMEM;
1280 ops = rbd_create_rw_ops(1, opcode, payload_len);
1281 if (!ops)
602adf40
YS
1282 goto done;
1283
1284 /* we've taken care of segment sizes earlier when we
1285 cloned the bios. We should never have a segment
1286 truncated at this point */
aafb230e 1287 rbd_assert(seg_len == len);
602adf40
YS
1288
1289 ret = rbd_do_request(rq, rbd_dev, snapc, snapid,
1290 seg_name, seg_ofs, seg_len,
1291 bio,
1292 NULL, 0,
1293 flags,
1294 ops,
1fec7093 1295 coll, coll_index,
59c2be1e 1296 rbd_req_cb, 0, NULL);
11f77002
SW
1297
1298 rbd_destroy_ops(ops);
602adf40
YS
1299done:
1300 kfree(seg_name);
1301 return ret;
1302}
1303
602adf40
YS
1304/*
1305 * Request sync osd read
1306 */
0ce1a794 1307static int rbd_req_sync_read(struct rbd_device *rbd_dev,
602adf40 1308 u64 snapid,
aded07ea 1309 const char *object_name,
602adf40 1310 u64 ofs, u64 len,
59c2be1e
YS
1311 char *buf,
1312 u64 *ver)
602adf40 1313{
913d2fdc
AE
1314 struct ceph_osd_req_op *ops;
1315 int ret;
1316
1317 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_READ, 0);
1318 if (!ops)
1319 return -ENOMEM;
1320
1321 ret = rbd_req_sync_op(rbd_dev, NULL,
b06e6a6b 1322 snapid,
602adf40 1323 CEPH_OSD_FLAG_READ,
913d2fdc
AE
1324 ops, object_name, ofs, len, buf, NULL, ver);
1325 rbd_destroy_ops(ops);
1326
1327 return ret;
602adf40
YS
1328}
1329
1330/*
59c2be1e
YS
1331 * Request sync osd watch
1332 */
0ce1a794 1333static int rbd_req_sync_notify_ack(struct rbd_device *rbd_dev,
59c2be1e 1334 u64 ver,
7f0a24d8 1335 u64 notify_id)
59c2be1e
YS
1336{
1337 struct ceph_osd_req_op *ops;
11f77002
SW
1338 int ret;
1339
57cfc106
AE
1340 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_NOTIFY_ACK, 0);
1341 if (!ops)
1342 return -ENOMEM;
59c2be1e 1343
a71b891b 1344 ops[0].watch.ver = cpu_to_le64(ver);
59c2be1e
YS
1345 ops[0].watch.cookie = notify_id;
1346 ops[0].watch.flag = 0;
1347
0ce1a794 1348 ret = rbd_do_request(NULL, rbd_dev, NULL, CEPH_NOSNAP,
7f0a24d8 1349 rbd_dev->header_name, 0, 0, NULL,
ad4f232f 1350 NULL, 0,
59c2be1e
YS
1351 CEPH_OSD_FLAG_READ,
1352 ops,
1fec7093 1353 NULL, 0,
59c2be1e
YS
1354 rbd_simple_req_cb, 0, NULL);
1355
1356 rbd_destroy_ops(ops);
1357 return ret;
1358}
1359
1360static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
1361{
0ce1a794 1362 struct rbd_device *rbd_dev = (struct rbd_device *)data;
a71b891b 1363 u64 hver;
13143d2d
SW
1364 int rc;
1365
0ce1a794 1366 if (!rbd_dev)
59c2be1e
YS
1367 return;
1368
bd919d45
AE
1369 dout("rbd_watch_cb %s notify_id=%llu opcode=%u\n",
1370 rbd_dev->header_name, (unsigned long long) notify_id,
1371 (unsigned int) opcode);
117973fb 1372 rc = rbd_dev_refresh(rbd_dev, &hver);
13143d2d 1373 if (rc)
f0f8cef5 1374 pr_warning(RBD_DRV_NAME "%d got notification but failed to "
0ce1a794 1375 " update snaps: %d\n", rbd_dev->major, rc);
59c2be1e 1376
7f0a24d8 1377 rbd_req_sync_notify_ack(rbd_dev, hver, notify_id);
59c2be1e
YS
1378}
1379
1380/*
1381 * Request sync osd watch
1382 */
0e6f322d 1383static int rbd_req_sync_watch(struct rbd_device *rbd_dev)
59c2be1e
YS
1384{
1385 struct ceph_osd_req_op *ops;
0ce1a794 1386 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
57cfc106 1387 int ret;
59c2be1e 1388
57cfc106
AE
1389 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_WATCH, 0);
1390 if (!ops)
1391 return -ENOMEM;
59c2be1e
YS
1392
1393 ret = ceph_osdc_create_event(osdc, rbd_watch_cb, 0,
0ce1a794 1394 (void *)rbd_dev, &rbd_dev->watch_event);
59c2be1e
YS
1395 if (ret < 0)
1396 goto fail;
1397
0e6f322d 1398 ops[0].watch.ver = cpu_to_le64(rbd_dev->header.obj_version);
0ce1a794 1399 ops[0].watch.cookie = cpu_to_le64(rbd_dev->watch_event->cookie);
59c2be1e
YS
1400 ops[0].watch.flag = 1;
1401
0ce1a794 1402 ret = rbd_req_sync_op(rbd_dev, NULL,
59c2be1e 1403 CEPH_NOSNAP,
59c2be1e
YS
1404 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1405 ops,
0e6f322d
AE
1406 rbd_dev->header_name,
1407 0, 0, NULL,
0ce1a794 1408 &rbd_dev->watch_request, NULL);
59c2be1e
YS
1409
1410 if (ret < 0)
1411 goto fail_event;
1412
1413 rbd_destroy_ops(ops);
1414 return 0;
1415
1416fail_event:
0ce1a794
AE
1417 ceph_osdc_cancel_event(rbd_dev->watch_event);
1418 rbd_dev->watch_event = NULL;
59c2be1e
YS
1419fail:
1420 rbd_destroy_ops(ops);
1421 return ret;
1422}
1423
79e3057c
YS
1424/*
1425 * Request sync osd unwatch
1426 */
070c633f 1427static int rbd_req_sync_unwatch(struct rbd_device *rbd_dev)
79e3057c
YS
1428{
1429 struct ceph_osd_req_op *ops;
57cfc106 1430 int ret;
79e3057c 1431
57cfc106
AE
1432 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_WATCH, 0);
1433 if (!ops)
1434 return -ENOMEM;
79e3057c
YS
1435
1436 ops[0].watch.ver = 0;
0ce1a794 1437 ops[0].watch.cookie = cpu_to_le64(rbd_dev->watch_event->cookie);
79e3057c
YS
1438 ops[0].watch.flag = 0;
1439
0ce1a794 1440 ret = rbd_req_sync_op(rbd_dev, NULL,
79e3057c 1441 CEPH_NOSNAP,
79e3057c
YS
1442 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1443 ops,
070c633f
AE
1444 rbd_dev->header_name,
1445 0, 0, NULL, NULL, NULL);
1446
79e3057c
YS
1447
1448 rbd_destroy_ops(ops);
0ce1a794
AE
1449 ceph_osdc_cancel_event(rbd_dev->watch_event);
1450 rbd_dev->watch_event = NULL;
79e3057c
YS
1451 return ret;
1452}
1453
602adf40 1454/*
3cb4a687 1455 * Synchronous osd object method call
602adf40 1456 */
0ce1a794 1457static int rbd_req_sync_exec(struct rbd_device *rbd_dev,
aded07ea
AE
1458 const char *object_name,
1459 const char *class_name,
1460 const char *method_name,
3cb4a687
AE
1461 const char *outbound,
1462 size_t outbound_size,
f8d4de6e
AE
1463 char *inbound,
1464 size_t inbound_size,
3cb4a687 1465 int flags,
59c2be1e 1466 u64 *ver)
602adf40
YS
1467{
1468 struct ceph_osd_req_op *ops;
aded07ea
AE
1469 int class_name_len = strlen(class_name);
1470 int method_name_len = strlen(method_name);
3cb4a687 1471 int payload_size;
57cfc106
AE
1472 int ret;
1473
3cb4a687
AE
1474 /*
1475 * Any input parameters required by the method we're calling
1476 * will be sent along with the class and method names as
1477 * part of the message payload. That data and its size are
1478 * supplied via the indata and indata_len fields (named from
1479 * the perspective of the server side) in the OSD request
1480 * operation.
1481 */
1482 payload_size = class_name_len + method_name_len + outbound_size;
1483 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_CALL, payload_size);
57cfc106
AE
1484 if (!ops)
1485 return -ENOMEM;
602adf40 1486
aded07ea
AE
1487 ops[0].cls.class_name = class_name;
1488 ops[0].cls.class_len = (__u8) class_name_len;
1489 ops[0].cls.method_name = method_name;
1490 ops[0].cls.method_len = (__u8) method_name_len;
602adf40 1491 ops[0].cls.argc = 0;
3cb4a687
AE
1492 ops[0].cls.indata = outbound;
1493 ops[0].cls.indata_len = outbound_size;
602adf40 1494
0ce1a794 1495 ret = rbd_req_sync_op(rbd_dev, NULL,
602adf40 1496 CEPH_NOSNAP,
3cb4a687 1497 flags, ops,
f8d4de6e
AE
1498 object_name, 0, inbound_size, inbound,
1499 NULL, ver);
602adf40
YS
1500
1501 rbd_destroy_ops(ops);
1502
1503 dout("cls_exec returned %d\n", ret);
1504 return ret;
1505}
1506
1fec7093
YS
1507static struct rbd_req_coll *rbd_alloc_coll(int num_reqs)
1508{
1509 struct rbd_req_coll *coll =
1510 kzalloc(sizeof(struct rbd_req_coll) +
1511 sizeof(struct rbd_req_status) * num_reqs,
1512 GFP_ATOMIC);
1513
1514 if (!coll)
1515 return NULL;
1516 coll->total = num_reqs;
1517 kref_init(&coll->kref);
1518 return coll;
1519}
1520
602adf40
YS
1521/*
1522 * block device queue callback
1523 */
1524static void rbd_rq_fn(struct request_queue *q)
1525{
1526 struct rbd_device *rbd_dev = q->queuedata;
1527 struct request *rq;
602adf40 1528
00f1f36f 1529 while ((rq = blk_fetch_request(q))) {
602adf40 1530 struct bio *bio;
602adf40 1531 bool do_write;
bd919d45 1532 unsigned int size;
602adf40 1533 u64 ofs;
1fec7093
YS
1534 int num_segs, cur_seg = 0;
1535 struct rbd_req_coll *coll;
d1d25646 1536 struct ceph_snap_context *snapc;
f7760dad 1537 unsigned int bio_offset;
602adf40 1538
602adf40
YS
1539 dout("fetched request\n");
1540
1541 /* filter out block requests we don't understand */
1542 if ((rq->cmd_type != REQ_TYPE_FS)) {
1543 __blk_end_request_all(rq, 0);
00f1f36f 1544 continue;
602adf40
YS
1545 }
1546
1547 /* deduce our operation (read, write) */
1548 do_write = (rq_data_dir(rq) == WRITE);
f84344f3 1549 if (do_write && rbd_dev->mapping.read_only) {
602adf40 1550 __blk_end_request_all(rq, -EROFS);
00f1f36f 1551 continue;
602adf40
YS
1552 }
1553
1554 spin_unlock_irq(q->queue_lock);
1555
d1d25646 1556 down_read(&rbd_dev->header_rwsem);
e88a36ec 1557
daba5fdb 1558 if (!rbd_dev->exists) {
0d7dbfce 1559 rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP);
e88a36ec 1560 up_read(&rbd_dev->header_rwsem);
d1d25646
JD
1561 dout("request for non-existent snapshot");
1562 spin_lock_irq(q->queue_lock);
1563 __blk_end_request_all(rq, -ENXIO);
1564 continue;
e88a36ec
JD
1565 }
1566
d1d25646
JD
1567 snapc = ceph_get_snap_context(rbd_dev->header.snapc);
1568
1569 up_read(&rbd_dev->header_rwsem);
1570
f7760dad
AE
1571 size = blk_rq_bytes(rq);
1572 ofs = blk_rq_pos(rq) * SECTOR_SIZE;
1573 bio = rq->bio;
1574
602adf40
YS
1575 dout("%s 0x%x bytes at 0x%llx\n",
1576 do_write ? "write" : "read",
bd919d45 1577 size, (unsigned long long) blk_rq_pos(rq) * SECTOR_SIZE);
602adf40 1578
1fec7093 1579 num_segs = rbd_get_num_segments(&rbd_dev->header, ofs, size);
df111be6
AE
1580 if (num_segs <= 0) {
1581 spin_lock_irq(q->queue_lock);
1582 __blk_end_request_all(rq, num_segs);
1583 ceph_put_snap_context(snapc);
1584 continue;
1585 }
1fec7093
YS
1586 coll = rbd_alloc_coll(num_segs);
1587 if (!coll) {
1588 spin_lock_irq(q->queue_lock);
1589 __blk_end_request_all(rq, -ENOMEM);
d1d25646 1590 ceph_put_snap_context(snapc);
00f1f36f 1591 continue;
1fec7093
YS
1592 }
1593
f7760dad 1594 bio_offset = 0;
602adf40 1595 do {
f7760dad
AE
1596 u64 limit = rbd_segment_length(rbd_dev, ofs, size);
1597 unsigned int chain_size;
1598 struct bio *bio_chain;
1599
1600 BUG_ON(limit > (u64) UINT_MAX);
1601 chain_size = (unsigned int) limit;
bd919d45 1602 dout("rq->bio->bi_vcnt=%hu\n", rq->bio->bi_vcnt);
f7760dad 1603
1fec7093 1604 kref_get(&coll->kref);
f7760dad
AE
1605
1606 /* Pass a cloned bio chain via an osd request */
1607
1608 bio_chain = bio_chain_clone_range(&bio,
1609 &bio_offset, chain_size,
1610 GFP_ATOMIC);
1611 if (bio_chain)
4634246d 1612 (void) rbd_do_op(rq, rbd_dev, snapc,
f7760dad
AE
1613 ofs, chain_size,
1614 bio_chain, coll, cur_seg);
4634246d 1615 else
1fec7093 1616 rbd_coll_end_req_index(rq, coll, cur_seg,
f7760dad
AE
1617 -ENOMEM, chain_size);
1618 size -= chain_size;
1619 ofs += chain_size;
602adf40 1620
1fec7093 1621 cur_seg++;
602adf40 1622 } while (size > 0);
1fec7093 1623 kref_put(&coll->kref, rbd_coll_release);
602adf40 1624
602adf40 1625 spin_lock_irq(q->queue_lock);
d1d25646
JD
1626
1627 ceph_put_snap_context(snapc);
602adf40
YS
1628 }
1629}
1630
1631/*
1632 * a queue callback. Makes sure that we don't create a bio that spans across
1633 * multiple osd objects. One exception would be with a single page bios,
f7760dad 1634 * which we handle later at bio_chain_clone_range()
602adf40
YS
1635 */
1636static int rbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd,
1637 struct bio_vec *bvec)
1638{
1639 struct rbd_device *rbd_dev = q->queuedata;
e5cfeed2
AE
1640 sector_t sector_offset;
1641 sector_t sectors_per_obj;
1642 sector_t obj_sector_offset;
1643 int ret;
1644
1645 /*
1646 * Find how far into its rbd object the partition-relative
1647 * bio start sector is to offset relative to the enclosing
1648 * device.
1649 */
1650 sector_offset = get_start_sect(bmd->bi_bdev) + bmd->bi_sector;
1651 sectors_per_obj = 1 << (rbd_dev->header.obj_order - SECTOR_SHIFT);
1652 obj_sector_offset = sector_offset & (sectors_per_obj - 1);
1653
1654 /*
1655 * Compute the number of bytes from that offset to the end
1656 * of the object. Account for what's already used by the bio.
1657 */
1658 ret = (int) (sectors_per_obj - obj_sector_offset) << SECTOR_SHIFT;
1659 if (ret > bmd->bi_size)
1660 ret -= bmd->bi_size;
1661 else
1662 ret = 0;
1663
1664 /*
1665 * Don't send back more than was asked for. And if the bio
1666 * was empty, let the whole thing through because: "Note
1667 * that a block device *must* allow a single page to be
1668 * added to an empty bio."
1669 */
1670 rbd_assert(bvec->bv_len <= PAGE_SIZE);
1671 if (ret > (int) bvec->bv_len || !bmd->bi_size)
1672 ret = (int) bvec->bv_len;
1673
1674 return ret;
602adf40
YS
1675}
1676
1677static void rbd_free_disk(struct rbd_device *rbd_dev)
1678{
1679 struct gendisk *disk = rbd_dev->disk;
1680
1681 if (!disk)
1682 return;
1683
602adf40
YS
1684 if (disk->flags & GENHD_FL_UP)
1685 del_gendisk(disk);
1686 if (disk->queue)
1687 blk_cleanup_queue(disk->queue);
1688 put_disk(disk);
1689}
1690
1691/*
4156d998
AE
1692 * Read the complete header for the given rbd device.
1693 *
1694 * Returns a pointer to a dynamically-allocated buffer containing
1695 * the complete and validated header. Caller can pass the address
1696 * of a variable that will be filled in with the version of the
1697 * header object at the time it was read.
1698 *
1699 * Returns a pointer-coded errno if a failure occurs.
602adf40 1700 */
4156d998
AE
1701static struct rbd_image_header_ondisk *
1702rbd_dev_v1_header_read(struct rbd_device *rbd_dev, u64 *version)
602adf40 1703{
4156d998 1704 struct rbd_image_header_ondisk *ondisk = NULL;
50f7c4c9 1705 u32 snap_count = 0;
4156d998
AE
1706 u64 names_size = 0;
1707 u32 want_count;
1708 int ret;
602adf40 1709
00f1f36f 1710 /*
4156d998
AE
1711 * The complete header will include an array of its 64-bit
1712 * snapshot ids, followed by the names of those snapshots as
1713 * a contiguous block of NUL-terminated strings. Note that
1714 * the number of snapshots could change by the time we read
1715 * it in, in which case we re-read it.
00f1f36f 1716 */
4156d998
AE
1717 do {
1718 size_t size;
1719
1720 kfree(ondisk);
1721
1722 size = sizeof (*ondisk);
1723 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
1724 size += names_size;
1725 ondisk = kmalloc(size, GFP_KERNEL);
1726 if (!ondisk)
1727 return ERR_PTR(-ENOMEM);
1728
1729 ret = rbd_req_sync_read(rbd_dev, CEPH_NOSNAP,
0bed54dc 1730 rbd_dev->header_name,
4156d998
AE
1731 0, size,
1732 (char *) ondisk, version);
1733
1734 if (ret < 0)
1735 goto out_err;
1736 if (WARN_ON((size_t) ret < size)) {
1737 ret = -ENXIO;
1738 pr_warning("short header read for image %s"
1739 " (want %zd got %d)\n",
0d7dbfce 1740 rbd_dev->spec->image_name, size, ret);
4156d998
AE
1741 goto out_err;
1742 }
1743 if (!rbd_dev_ondisk_valid(ondisk)) {
1744 ret = -ENXIO;
1745 pr_warning("invalid header for image %s\n",
0d7dbfce 1746 rbd_dev->spec->image_name);
4156d998 1747 goto out_err;
81e759fb 1748 }
602adf40 1749
4156d998
AE
1750 names_size = le64_to_cpu(ondisk->snap_names_len);
1751 want_count = snap_count;
1752 snap_count = le32_to_cpu(ondisk->snap_count);
1753 } while (snap_count != want_count);
00f1f36f 1754
4156d998 1755 return ondisk;
00f1f36f 1756
4156d998
AE
1757out_err:
1758 kfree(ondisk);
1759
1760 return ERR_PTR(ret);
1761}
1762
1763/*
1764 * reload the ondisk the header
1765 */
1766static int rbd_read_header(struct rbd_device *rbd_dev,
1767 struct rbd_image_header *header)
1768{
1769 struct rbd_image_header_ondisk *ondisk;
1770 u64 ver = 0;
1771 int ret;
602adf40 1772
4156d998
AE
1773 ondisk = rbd_dev_v1_header_read(rbd_dev, &ver);
1774 if (IS_ERR(ondisk))
1775 return PTR_ERR(ondisk);
1776 ret = rbd_header_from_disk(header, ondisk);
1777 if (ret >= 0)
1778 header->obj_version = ver;
1779 kfree(ondisk);
1780
1781 return ret;
602adf40
YS
1782}
1783
41f38c2b 1784static void rbd_remove_all_snaps(struct rbd_device *rbd_dev)
dfc5606d
YS
1785{
1786 struct rbd_snap *snap;
a0593290 1787 struct rbd_snap *next;
dfc5606d 1788
a0593290 1789 list_for_each_entry_safe(snap, next, &rbd_dev->snaps, node)
41f38c2b 1790 rbd_remove_snap_dev(snap);
dfc5606d
YS
1791}
1792
9478554a
AE
1793static void rbd_update_mapping_size(struct rbd_device *rbd_dev)
1794{
1795 sector_t size;
1796
0d7dbfce 1797 if (rbd_dev->spec->snap_id != CEPH_NOSNAP)
9478554a
AE
1798 return;
1799
1800 size = (sector_t) rbd_dev->header.image_size / SECTOR_SIZE;
1801 dout("setting size to %llu sectors", (unsigned long long) size);
1802 rbd_dev->mapping.size = (u64) size;
1803 set_capacity(rbd_dev->disk, size);
1804}
1805
602adf40
YS
1806/*
1807 * only read the first part of the ondisk header, without the snaps info
1808 */
117973fb 1809static int rbd_dev_v1_refresh(struct rbd_device *rbd_dev, u64 *hver)
602adf40
YS
1810{
1811 int ret;
1812 struct rbd_image_header h;
602adf40
YS
1813
1814 ret = rbd_read_header(rbd_dev, &h);
1815 if (ret < 0)
1816 return ret;
1817
a51aa0c0
JD
1818 down_write(&rbd_dev->header_rwsem);
1819
9478554a
AE
1820 /* Update image size, and check for resize of mapped image */
1821 rbd_dev->header.image_size = h.image_size;
1822 rbd_update_mapping_size(rbd_dev);
9db4b3e3 1823
849b4260 1824 /* rbd_dev->header.object_prefix shouldn't change */
602adf40 1825 kfree(rbd_dev->header.snap_sizes);
849b4260 1826 kfree(rbd_dev->header.snap_names);
d1d25646
JD
1827 /* osd requests may still refer to snapc */
1828 ceph_put_snap_context(rbd_dev->header.snapc);
602adf40 1829
b813623a
AE
1830 if (hver)
1831 *hver = h.obj_version;
a71b891b 1832 rbd_dev->header.obj_version = h.obj_version;
93a24e08 1833 rbd_dev->header.image_size = h.image_size;
602adf40
YS
1834 rbd_dev->header.snapc = h.snapc;
1835 rbd_dev->header.snap_names = h.snap_names;
1836 rbd_dev->header.snap_sizes = h.snap_sizes;
849b4260
AE
1837 /* Free the extra copy of the object prefix */
1838 WARN_ON(strcmp(rbd_dev->header.object_prefix, h.object_prefix));
1839 kfree(h.object_prefix);
1840
304f6808
AE
1841 ret = rbd_dev_snaps_update(rbd_dev);
1842 if (!ret)
1843 ret = rbd_dev_snaps_register(rbd_dev);
dfc5606d 1844
c666601a 1845 up_write(&rbd_dev->header_rwsem);
602adf40 1846
dfc5606d 1847 return ret;
602adf40
YS
1848}
1849
117973fb 1850static int rbd_dev_refresh(struct rbd_device *rbd_dev, u64 *hver)
1fe5e993
AE
1851{
1852 int ret;
1853
117973fb 1854 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1fe5e993 1855 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
117973fb
AE
1856 if (rbd_dev->image_format == 1)
1857 ret = rbd_dev_v1_refresh(rbd_dev, hver);
1858 else
1859 ret = rbd_dev_v2_refresh(rbd_dev, hver);
1fe5e993
AE
1860 mutex_unlock(&ctl_mutex);
1861
1862 return ret;
1863}
1864
602adf40
YS
1865static int rbd_init_disk(struct rbd_device *rbd_dev)
1866{
1867 struct gendisk *disk;
1868 struct request_queue *q;
593a9e7b 1869 u64 segment_size;
602adf40 1870
602adf40 1871 /* create gendisk info */
602adf40
YS
1872 disk = alloc_disk(RBD_MINORS_PER_MAJOR);
1873 if (!disk)
1fcdb8aa 1874 return -ENOMEM;
602adf40 1875
f0f8cef5 1876 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
de71a297 1877 rbd_dev->dev_id);
602adf40
YS
1878 disk->major = rbd_dev->major;
1879 disk->first_minor = 0;
1880 disk->fops = &rbd_bd_ops;
1881 disk->private_data = rbd_dev;
1882
1883 /* init rq */
602adf40
YS
1884 q = blk_init_queue(rbd_rq_fn, &rbd_dev->lock);
1885 if (!q)
1886 goto out_disk;
029bcbd8 1887
593a9e7b
AE
1888 /* We use the default size, but let's be explicit about it. */
1889 blk_queue_physical_block_size(q, SECTOR_SIZE);
1890
029bcbd8 1891 /* set io sizes to object size */
593a9e7b
AE
1892 segment_size = rbd_obj_bytes(&rbd_dev->header);
1893 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
1894 blk_queue_max_segment_size(q, segment_size);
1895 blk_queue_io_min(q, segment_size);
1896 blk_queue_io_opt(q, segment_size);
029bcbd8 1897
602adf40
YS
1898 blk_queue_merge_bvec(q, rbd_merge_bvec);
1899 disk->queue = q;
1900
1901 q->queuedata = rbd_dev;
1902
1903 rbd_dev->disk = disk;
602adf40 1904
12f02944
AE
1905 set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
1906
602adf40 1907 return 0;
602adf40
YS
1908out_disk:
1909 put_disk(disk);
1fcdb8aa
AE
1910
1911 return -ENOMEM;
602adf40
YS
1912}
1913
dfc5606d
YS
1914/*
1915 sysfs
1916*/
1917
593a9e7b
AE
1918static struct rbd_device *dev_to_rbd_dev(struct device *dev)
1919{
1920 return container_of(dev, struct rbd_device, dev);
1921}
1922
dfc5606d
YS
1923static ssize_t rbd_size_show(struct device *dev,
1924 struct device_attribute *attr, char *buf)
1925{
593a9e7b 1926 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
a51aa0c0
JD
1927 sector_t size;
1928
1929 down_read(&rbd_dev->header_rwsem);
1930 size = get_capacity(rbd_dev->disk);
1931 up_read(&rbd_dev->header_rwsem);
dfc5606d 1932
a51aa0c0 1933 return sprintf(buf, "%llu\n", (unsigned long long) size * SECTOR_SIZE);
dfc5606d
YS
1934}
1935
34b13184
AE
1936/*
1937 * Note this shows the features for whatever's mapped, which is not
1938 * necessarily the base image.
1939 */
1940static ssize_t rbd_features_show(struct device *dev,
1941 struct device_attribute *attr, char *buf)
1942{
1943 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
1944
1945 return sprintf(buf, "0x%016llx\n",
1946 (unsigned long long) rbd_dev->mapping.features);
1947}
1948
dfc5606d
YS
1949static ssize_t rbd_major_show(struct device *dev,
1950 struct device_attribute *attr, char *buf)
1951{
593a9e7b 1952 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
602adf40 1953
dfc5606d
YS
1954 return sprintf(buf, "%d\n", rbd_dev->major);
1955}
1956
1957static ssize_t rbd_client_id_show(struct device *dev,
1958 struct device_attribute *attr, char *buf)
602adf40 1959{
593a9e7b 1960 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
dfc5606d 1961
1dbb4399
AE
1962 return sprintf(buf, "client%lld\n",
1963 ceph_client_id(rbd_dev->rbd_client->client));
602adf40
YS
1964}
1965
dfc5606d
YS
1966static ssize_t rbd_pool_show(struct device *dev,
1967 struct device_attribute *attr, char *buf)
602adf40 1968{
593a9e7b 1969 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
dfc5606d 1970
0d7dbfce 1971 return sprintf(buf, "%s\n", rbd_dev->spec->pool_name);
dfc5606d
YS
1972}
1973
9bb2f334
AE
1974static ssize_t rbd_pool_id_show(struct device *dev,
1975 struct device_attribute *attr, char *buf)
1976{
1977 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
1978
0d7dbfce
AE
1979 return sprintf(buf, "%llu\n",
1980 (unsigned long long) rbd_dev->spec->pool_id);
9bb2f334
AE
1981}
1982
dfc5606d
YS
1983static ssize_t rbd_name_show(struct device *dev,
1984 struct device_attribute *attr, char *buf)
1985{
593a9e7b 1986 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
dfc5606d 1987
a92ffdf8
AE
1988 if (rbd_dev->spec->image_name)
1989 return sprintf(buf, "%s\n", rbd_dev->spec->image_name);
1990
1991 return sprintf(buf, "(unknown)\n");
dfc5606d
YS
1992}
1993
589d30e0
AE
1994static ssize_t rbd_image_id_show(struct device *dev,
1995 struct device_attribute *attr, char *buf)
1996{
1997 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
1998
0d7dbfce 1999 return sprintf(buf, "%s\n", rbd_dev->spec->image_id);
589d30e0
AE
2000}
2001
34b13184
AE
2002/*
2003 * Shows the name of the currently-mapped snapshot (or
2004 * RBD_SNAP_HEAD_NAME for the base image).
2005 */
dfc5606d
YS
2006static ssize_t rbd_snap_show(struct device *dev,
2007 struct device_attribute *attr,
2008 char *buf)
2009{
593a9e7b 2010 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
dfc5606d 2011
0d7dbfce 2012 return sprintf(buf, "%s\n", rbd_dev->spec->snap_name);
dfc5606d
YS
2013}
2014
86b00e0d
AE
2015/*
2016 * For an rbd v2 image, shows the pool id, image id, and snapshot id
2017 * for the parent image. If there is no parent, simply shows
2018 * "(no parent image)".
2019 */
2020static ssize_t rbd_parent_show(struct device *dev,
2021 struct device_attribute *attr,
2022 char *buf)
2023{
2024 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
2025 struct rbd_spec *spec = rbd_dev->parent_spec;
2026 int count;
2027 char *bufp = buf;
2028
2029 if (!spec)
2030 return sprintf(buf, "(no parent image)\n");
2031
2032 count = sprintf(bufp, "pool_id %llu\npool_name %s\n",
2033 (unsigned long long) spec->pool_id, spec->pool_name);
2034 if (count < 0)
2035 return count;
2036 bufp += count;
2037
2038 count = sprintf(bufp, "image_id %s\nimage_name %s\n", spec->image_id,
2039 spec->image_name ? spec->image_name : "(unknown)");
2040 if (count < 0)
2041 return count;
2042 bufp += count;
2043
2044 count = sprintf(bufp, "snap_id %llu\nsnap_name %s\n",
2045 (unsigned long long) spec->snap_id, spec->snap_name);
2046 if (count < 0)
2047 return count;
2048 bufp += count;
2049
2050 count = sprintf(bufp, "overlap %llu\n", rbd_dev->parent_overlap);
2051 if (count < 0)
2052 return count;
2053 bufp += count;
2054
2055 return (ssize_t) (bufp - buf);
2056}
2057
dfc5606d
YS
2058static ssize_t rbd_image_refresh(struct device *dev,
2059 struct device_attribute *attr,
2060 const char *buf,
2061 size_t size)
2062{
593a9e7b 2063 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
b813623a 2064 int ret;
602adf40 2065
117973fb 2066 ret = rbd_dev_refresh(rbd_dev, NULL);
b813623a
AE
2067
2068 return ret < 0 ? ret : size;
dfc5606d 2069}
602adf40 2070
dfc5606d 2071static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
34b13184 2072static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL);
dfc5606d
YS
2073static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
2074static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
2075static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
9bb2f334 2076static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
dfc5606d 2077static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
589d30e0 2078static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL);
dfc5606d
YS
2079static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
2080static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
86b00e0d 2081static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL);
dfc5606d
YS
2082
2083static struct attribute *rbd_attrs[] = {
2084 &dev_attr_size.attr,
34b13184 2085 &dev_attr_features.attr,
dfc5606d
YS
2086 &dev_attr_major.attr,
2087 &dev_attr_client_id.attr,
2088 &dev_attr_pool.attr,
9bb2f334 2089 &dev_attr_pool_id.attr,
dfc5606d 2090 &dev_attr_name.attr,
589d30e0 2091 &dev_attr_image_id.attr,
dfc5606d 2092 &dev_attr_current_snap.attr,
86b00e0d 2093 &dev_attr_parent.attr,
dfc5606d 2094 &dev_attr_refresh.attr,
dfc5606d
YS
2095 NULL
2096};
2097
2098static struct attribute_group rbd_attr_group = {
2099 .attrs = rbd_attrs,
2100};
2101
2102static const struct attribute_group *rbd_attr_groups[] = {
2103 &rbd_attr_group,
2104 NULL
2105};
2106
2107static void rbd_sysfs_dev_release(struct device *dev)
2108{
2109}
2110
2111static struct device_type rbd_device_type = {
2112 .name = "rbd",
2113 .groups = rbd_attr_groups,
2114 .release = rbd_sysfs_dev_release,
2115};
2116
2117
2118/*
2119 sysfs - snapshots
2120*/
2121
2122static ssize_t rbd_snap_size_show(struct device *dev,
2123 struct device_attribute *attr,
2124 char *buf)
2125{
2126 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2127
3591538f 2128 return sprintf(buf, "%llu\n", (unsigned long long)snap->size);
dfc5606d
YS
2129}
2130
2131static ssize_t rbd_snap_id_show(struct device *dev,
2132 struct device_attribute *attr,
2133 char *buf)
2134{
2135 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2136
3591538f 2137 return sprintf(buf, "%llu\n", (unsigned long long)snap->id);
dfc5606d
YS
2138}
2139
34b13184
AE
2140static ssize_t rbd_snap_features_show(struct device *dev,
2141 struct device_attribute *attr,
2142 char *buf)
2143{
2144 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2145
2146 return sprintf(buf, "0x%016llx\n",
2147 (unsigned long long) snap->features);
2148}
2149
dfc5606d
YS
2150static DEVICE_ATTR(snap_size, S_IRUGO, rbd_snap_size_show, NULL);
2151static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL);
34b13184 2152static DEVICE_ATTR(snap_features, S_IRUGO, rbd_snap_features_show, NULL);
dfc5606d
YS
2153
2154static struct attribute *rbd_snap_attrs[] = {
2155 &dev_attr_snap_size.attr,
2156 &dev_attr_snap_id.attr,
34b13184 2157 &dev_attr_snap_features.attr,
dfc5606d
YS
2158 NULL,
2159};
2160
2161static struct attribute_group rbd_snap_attr_group = {
2162 .attrs = rbd_snap_attrs,
2163};
2164
2165static void rbd_snap_dev_release(struct device *dev)
2166{
2167 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2168 kfree(snap->name);
2169 kfree(snap);
2170}
2171
2172static const struct attribute_group *rbd_snap_attr_groups[] = {
2173 &rbd_snap_attr_group,
2174 NULL
2175};
2176
2177static struct device_type rbd_snap_device_type = {
2178 .groups = rbd_snap_attr_groups,
2179 .release = rbd_snap_dev_release,
2180};
2181
8b8fb99c
AE
2182static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)
2183{
2184 kref_get(&spec->kref);
2185
2186 return spec;
2187}
2188
2189static void rbd_spec_free(struct kref *kref);
2190static void rbd_spec_put(struct rbd_spec *spec)
2191{
2192 if (spec)
2193 kref_put(&spec->kref, rbd_spec_free);
2194}
2195
2196static struct rbd_spec *rbd_spec_alloc(void)
2197{
2198 struct rbd_spec *spec;
2199
2200 spec = kzalloc(sizeof (*spec), GFP_KERNEL);
2201 if (!spec)
2202 return NULL;
2203 kref_init(&spec->kref);
2204
2205 rbd_spec_put(rbd_spec_get(spec)); /* TEMPORARY */
2206
2207 return spec;
2208}
2209
2210static void rbd_spec_free(struct kref *kref)
2211{
2212 struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref);
2213
2214 kfree(spec->pool_name);
2215 kfree(spec->image_id);
2216 kfree(spec->image_name);
2217 kfree(spec->snap_name);
2218 kfree(spec);
2219}
2220
c53d5893
AE
2221struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,
2222 struct rbd_spec *spec)
2223{
2224 struct rbd_device *rbd_dev;
2225
2226 rbd_dev = kzalloc(sizeof (*rbd_dev), GFP_KERNEL);
2227 if (!rbd_dev)
2228 return NULL;
2229
2230 spin_lock_init(&rbd_dev->lock);
2231 INIT_LIST_HEAD(&rbd_dev->node);
2232 INIT_LIST_HEAD(&rbd_dev->snaps);
2233 init_rwsem(&rbd_dev->header_rwsem);
2234
2235 rbd_dev->spec = spec;
2236 rbd_dev->rbd_client = rbdc;
2237
2238 return rbd_dev;
2239}
2240
2241static void rbd_dev_destroy(struct rbd_device *rbd_dev)
2242{
86b00e0d 2243 rbd_spec_put(rbd_dev->parent_spec);
c53d5893
AE
2244 kfree(rbd_dev->header_name);
2245 rbd_put_client(rbd_dev->rbd_client);
2246 rbd_spec_put(rbd_dev->spec);
2247 kfree(rbd_dev);
2248}
2249
304f6808
AE
2250static bool rbd_snap_registered(struct rbd_snap *snap)
2251{
2252 bool ret = snap->dev.type == &rbd_snap_device_type;
2253 bool reg = device_is_registered(&snap->dev);
2254
2255 rbd_assert(!ret ^ reg);
2256
2257 return ret;
2258}
2259
41f38c2b 2260static void rbd_remove_snap_dev(struct rbd_snap *snap)
dfc5606d
YS
2261{
2262 list_del(&snap->node);
304f6808
AE
2263 if (device_is_registered(&snap->dev))
2264 device_unregister(&snap->dev);
dfc5606d
YS
2265}
2266
14e7085d 2267static int rbd_register_snap_dev(struct rbd_snap *snap,
dfc5606d
YS
2268 struct device *parent)
2269{
2270 struct device *dev = &snap->dev;
2271 int ret;
2272
2273 dev->type = &rbd_snap_device_type;
2274 dev->parent = parent;
2275 dev->release = rbd_snap_dev_release;
d4b125e9 2276 dev_set_name(dev, "%s%s", RBD_SNAP_DEV_NAME_PREFIX, snap->name);
304f6808
AE
2277 dout("%s: registering device for snapshot %s\n", __func__, snap->name);
2278
dfc5606d
YS
2279 ret = device_register(dev);
2280
2281 return ret;
2282}
2283
4e891e0a 2284static struct rbd_snap *__rbd_add_snap_dev(struct rbd_device *rbd_dev,
c8d18425 2285 const char *snap_name,
34b13184
AE
2286 u64 snap_id, u64 snap_size,
2287 u64 snap_features)
dfc5606d 2288{
4e891e0a 2289 struct rbd_snap *snap;
dfc5606d 2290 int ret;
4e891e0a
AE
2291
2292 snap = kzalloc(sizeof (*snap), GFP_KERNEL);
dfc5606d 2293 if (!snap)
4e891e0a
AE
2294 return ERR_PTR(-ENOMEM);
2295
2296 ret = -ENOMEM;
c8d18425 2297 snap->name = kstrdup(snap_name, GFP_KERNEL);
4e891e0a
AE
2298 if (!snap->name)
2299 goto err;
2300
c8d18425
AE
2301 snap->id = snap_id;
2302 snap->size = snap_size;
34b13184 2303 snap->features = snap_features;
4e891e0a
AE
2304
2305 return snap;
2306
dfc5606d
YS
2307err:
2308 kfree(snap->name);
2309 kfree(snap);
4e891e0a
AE
2310
2311 return ERR_PTR(ret);
dfc5606d
YS
2312}
2313
cd892126
AE
2314static char *rbd_dev_v1_snap_info(struct rbd_device *rbd_dev, u32 which,
2315 u64 *snap_size, u64 *snap_features)
2316{
2317 char *snap_name;
2318
2319 rbd_assert(which < rbd_dev->header.snapc->num_snaps);
2320
2321 *snap_size = rbd_dev->header.snap_sizes[which];
2322 *snap_features = 0; /* No features for v1 */
2323
2324 /* Skip over names until we find the one we are looking for */
2325
2326 snap_name = rbd_dev->header.snap_names;
2327 while (which--)
2328 snap_name += strlen(snap_name) + 1;
2329
2330 return snap_name;
2331}
2332
9d475de5
AE
2333/*
2334 * Get the size and object order for an image snapshot, or if
2335 * snap_id is CEPH_NOSNAP, gets this information for the base
2336 * image.
2337 */
2338static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
2339 u8 *order, u64 *snap_size)
2340{
2341 __le64 snapid = cpu_to_le64(snap_id);
2342 int ret;
2343 struct {
2344 u8 order;
2345 __le64 size;
2346 } __attribute__ ((packed)) size_buf = { 0 };
2347
2348 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2349 "rbd", "get_size",
2350 (char *) &snapid, sizeof (snapid),
2351 (char *) &size_buf, sizeof (size_buf),
2352 CEPH_OSD_FLAG_READ, NULL);
2353 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2354 if (ret < 0)
2355 return ret;
2356
2357 *order = size_buf.order;
2358 *snap_size = le64_to_cpu(size_buf.size);
2359
2360 dout(" snap_id 0x%016llx order = %u, snap_size = %llu\n",
2361 (unsigned long long) snap_id, (unsigned int) *order,
2362 (unsigned long long) *snap_size);
2363
2364 return 0;
2365}
2366
2367static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
2368{
2369 return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
2370 &rbd_dev->header.obj_order,
2371 &rbd_dev->header.image_size);
2372}
2373
1e130199
AE
2374static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
2375{
2376 void *reply_buf;
2377 int ret;
2378 void *p;
2379
2380 reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL);
2381 if (!reply_buf)
2382 return -ENOMEM;
2383
2384 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2385 "rbd", "get_object_prefix",
2386 NULL, 0,
2387 reply_buf, RBD_OBJ_PREFIX_LEN_MAX,
2388 CEPH_OSD_FLAG_READ, NULL);
2389 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2390 if (ret < 0)
2391 goto out;
a0ea3a40 2392 ret = 0; /* rbd_req_sync_exec() can return positive */
1e130199
AE
2393
2394 p = reply_buf;
2395 rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p,
2396 p + RBD_OBJ_PREFIX_LEN_MAX,
2397 NULL, GFP_NOIO);
2398
2399 if (IS_ERR(rbd_dev->header.object_prefix)) {
2400 ret = PTR_ERR(rbd_dev->header.object_prefix);
2401 rbd_dev->header.object_prefix = NULL;
2402 } else {
2403 dout(" object_prefix = %s\n", rbd_dev->header.object_prefix);
2404 }
2405
2406out:
2407 kfree(reply_buf);
2408
2409 return ret;
2410}
2411
b1b5402a
AE
2412static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
2413 u64 *snap_features)
2414{
2415 __le64 snapid = cpu_to_le64(snap_id);
2416 struct {
2417 __le64 features;
2418 __le64 incompat;
2419 } features_buf = { 0 };
d889140c 2420 u64 incompat;
b1b5402a
AE
2421 int ret;
2422
2423 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2424 "rbd", "get_features",
2425 (char *) &snapid, sizeof (snapid),
2426 (char *) &features_buf, sizeof (features_buf),
2427 CEPH_OSD_FLAG_READ, NULL);
2428 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2429 if (ret < 0)
2430 return ret;
d889140c
AE
2431
2432 incompat = le64_to_cpu(features_buf.incompat);
2433 if (incompat & ~RBD_FEATURES_ALL)
2434 return -ENOTSUPP;
2435
b1b5402a
AE
2436 *snap_features = le64_to_cpu(features_buf.features);
2437
2438 dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
2439 (unsigned long long) snap_id,
2440 (unsigned long long) *snap_features,
2441 (unsigned long long) le64_to_cpu(features_buf.incompat));
2442
2443 return 0;
2444}
2445
2446static int rbd_dev_v2_features(struct rbd_device *rbd_dev)
2447{
2448 return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP,
2449 &rbd_dev->header.features);
2450}
2451
86b00e0d
AE
2452static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev)
2453{
2454 struct rbd_spec *parent_spec;
2455 size_t size;
2456 void *reply_buf = NULL;
2457 __le64 snapid;
2458 void *p;
2459 void *end;
2460 char *image_id;
2461 u64 overlap;
2462 size_t len = 0;
2463 int ret;
2464
2465 parent_spec = rbd_spec_alloc();
2466 if (!parent_spec)
2467 return -ENOMEM;
2468
2469 size = sizeof (__le64) + /* pool_id */
2470 sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */
2471 sizeof (__le64) + /* snap_id */
2472 sizeof (__le64); /* overlap */
2473 reply_buf = kmalloc(size, GFP_KERNEL);
2474 if (!reply_buf) {
2475 ret = -ENOMEM;
2476 goto out_err;
2477 }
2478
2479 snapid = cpu_to_le64(CEPH_NOSNAP);
2480 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2481 "rbd", "get_parent",
2482 (char *) &snapid, sizeof (snapid),
2483 (char *) reply_buf, size,
2484 CEPH_OSD_FLAG_READ, NULL);
2485 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2486 if (ret < 0)
2487 goto out_err;
2488
2489 ret = -ERANGE;
2490 p = reply_buf;
2491 end = (char *) reply_buf + size;
2492 ceph_decode_64_safe(&p, end, parent_spec->pool_id, out_err);
2493 if (parent_spec->pool_id == CEPH_NOPOOL)
2494 goto out; /* No parent? No problem. */
2495
2496 image_id = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL);
2497 if (IS_ERR(image_id)) {
2498 ret = PTR_ERR(image_id);
2499 goto out_err;
2500 }
2501 parent_spec->image_id = image_id;
2502 ceph_decode_64_safe(&p, end, parent_spec->snap_id, out_err);
2503 ceph_decode_64_safe(&p, end, overlap, out_err);
2504
2505 rbd_dev->parent_overlap = overlap;
2506 rbd_dev->parent_spec = parent_spec;
2507 parent_spec = NULL; /* rbd_dev now owns this */
2508out:
2509 ret = 0;
2510out_err:
2511 kfree(reply_buf);
2512 rbd_spec_put(parent_spec);
2513
2514 return ret;
2515}
2516
6e14b1a6 2517static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev, u64 *ver)
35d489f9
AE
2518{
2519 size_t size;
2520 int ret;
2521 void *reply_buf;
2522 void *p;
2523 void *end;
2524 u64 seq;
2525 u32 snap_count;
2526 struct ceph_snap_context *snapc;
2527 u32 i;
2528
2529 /*
2530 * We'll need room for the seq value (maximum snapshot id),
2531 * snapshot count, and array of that many snapshot ids.
2532 * For now we have a fixed upper limit on the number we're
2533 * prepared to receive.
2534 */
2535 size = sizeof (__le64) + sizeof (__le32) +
2536 RBD_MAX_SNAP_COUNT * sizeof (__le64);
2537 reply_buf = kzalloc(size, GFP_KERNEL);
2538 if (!reply_buf)
2539 return -ENOMEM;
2540
2541 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2542 "rbd", "get_snapcontext",
2543 NULL, 0,
2544 reply_buf, size,
6e14b1a6 2545 CEPH_OSD_FLAG_READ, ver);
35d489f9
AE
2546 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2547 if (ret < 0)
2548 goto out;
2549
2550 ret = -ERANGE;
2551 p = reply_buf;
2552 end = (char *) reply_buf + size;
2553 ceph_decode_64_safe(&p, end, seq, out);
2554 ceph_decode_32_safe(&p, end, snap_count, out);
2555
2556 /*
2557 * Make sure the reported number of snapshot ids wouldn't go
2558 * beyond the end of our buffer. But before checking that,
2559 * make sure the computed size of the snapshot context we
2560 * allocate is representable in a size_t.
2561 */
2562 if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
2563 / sizeof (u64)) {
2564 ret = -EINVAL;
2565 goto out;
2566 }
2567 if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
2568 goto out;
2569
2570 size = sizeof (struct ceph_snap_context) +
2571 snap_count * sizeof (snapc->snaps[0]);
2572 snapc = kmalloc(size, GFP_KERNEL);
2573 if (!snapc) {
2574 ret = -ENOMEM;
2575 goto out;
2576 }
2577
2578 atomic_set(&snapc->nref, 1);
2579 snapc->seq = seq;
2580 snapc->num_snaps = snap_count;
2581 for (i = 0; i < snap_count; i++)
2582 snapc->snaps[i] = ceph_decode_64(&p);
2583
2584 rbd_dev->header.snapc = snapc;
2585
2586 dout(" snap context seq = %llu, snap_count = %u\n",
2587 (unsigned long long) seq, (unsigned int) snap_count);
2588
2589out:
2590 kfree(reply_buf);
2591
2592 return 0;
2593}
2594
b8b1e2db
AE
2595static char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, u32 which)
2596{
2597 size_t size;
2598 void *reply_buf;
2599 __le64 snap_id;
2600 int ret;
2601 void *p;
2602 void *end;
b8b1e2db
AE
2603 char *snap_name;
2604
2605 size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN;
2606 reply_buf = kmalloc(size, GFP_KERNEL);
2607 if (!reply_buf)
2608 return ERR_PTR(-ENOMEM);
2609
2610 snap_id = cpu_to_le64(rbd_dev->header.snapc->snaps[which]);
2611 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2612 "rbd", "get_snapshot_name",
2613 (char *) &snap_id, sizeof (snap_id),
2614 reply_buf, size,
2615 CEPH_OSD_FLAG_READ, NULL);
2616 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2617 if (ret < 0)
2618 goto out;
2619
2620 p = reply_buf;
2621 end = (char *) reply_buf + size;
e5c35534 2622 snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
b8b1e2db
AE
2623 if (IS_ERR(snap_name)) {
2624 ret = PTR_ERR(snap_name);
2625 goto out;
2626 } else {
2627 dout(" snap_id 0x%016llx snap_name = %s\n",
2628 (unsigned long long) le64_to_cpu(snap_id), snap_name);
2629 }
2630 kfree(reply_buf);
2631
2632 return snap_name;
2633out:
2634 kfree(reply_buf);
2635
2636 return ERR_PTR(ret);
2637}
2638
2639static char *rbd_dev_v2_snap_info(struct rbd_device *rbd_dev, u32 which,
2640 u64 *snap_size, u64 *snap_features)
2641{
2642 __le64 snap_id;
2643 u8 order;
2644 int ret;
2645
2646 snap_id = rbd_dev->header.snapc->snaps[which];
2647 ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, &order, snap_size);
2648 if (ret)
2649 return ERR_PTR(ret);
2650 ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, snap_features);
2651 if (ret)
2652 return ERR_PTR(ret);
2653
2654 return rbd_dev_v2_snap_name(rbd_dev, which);
2655}
2656
2657static char *rbd_dev_snap_info(struct rbd_device *rbd_dev, u32 which,
2658 u64 *snap_size, u64 *snap_features)
2659{
2660 if (rbd_dev->image_format == 1)
2661 return rbd_dev_v1_snap_info(rbd_dev, which,
2662 snap_size, snap_features);
2663 if (rbd_dev->image_format == 2)
2664 return rbd_dev_v2_snap_info(rbd_dev, which,
2665 snap_size, snap_features);
2666 return ERR_PTR(-EINVAL);
2667}
2668
117973fb
AE
2669static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev, u64 *hver)
2670{
2671 int ret;
2672 __u8 obj_order;
2673
2674 down_write(&rbd_dev->header_rwsem);
2675
2676 /* Grab old order first, to see if it changes */
2677
2678 obj_order = rbd_dev->header.obj_order,
2679 ret = rbd_dev_v2_image_size(rbd_dev);
2680 if (ret)
2681 goto out;
2682 if (rbd_dev->header.obj_order != obj_order) {
2683 ret = -EIO;
2684 goto out;
2685 }
2686 rbd_update_mapping_size(rbd_dev);
2687
2688 ret = rbd_dev_v2_snap_context(rbd_dev, hver);
2689 dout("rbd_dev_v2_snap_context returned %d\n", ret);
2690 if (ret)
2691 goto out;
2692 ret = rbd_dev_snaps_update(rbd_dev);
2693 dout("rbd_dev_snaps_update returned %d\n", ret);
2694 if (ret)
2695 goto out;
2696 ret = rbd_dev_snaps_register(rbd_dev);
2697 dout("rbd_dev_snaps_register returned %d\n", ret);
2698out:
2699 up_write(&rbd_dev->header_rwsem);
2700
2701 return ret;
2702}
2703
dfc5606d 2704/*
35938150
AE
2705 * Scan the rbd device's current snapshot list and compare it to the
2706 * newly-received snapshot context. Remove any existing snapshots
2707 * not present in the new snapshot context. Add a new snapshot for
2708 * any snaphots in the snapshot context not in the current list.
2709 * And verify there are no changes to snapshots we already know
2710 * about.
2711 *
2712 * Assumes the snapshots in the snapshot context are sorted by
2713 * snapshot id, highest id first. (Snapshots in the rbd_dev's list
2714 * are also maintained in that order.)
dfc5606d 2715 */
304f6808 2716static int rbd_dev_snaps_update(struct rbd_device *rbd_dev)
dfc5606d 2717{
35938150
AE
2718 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
2719 const u32 snap_count = snapc->num_snaps;
35938150
AE
2720 struct list_head *head = &rbd_dev->snaps;
2721 struct list_head *links = head->next;
2722 u32 index = 0;
dfc5606d 2723
9fcbb800 2724 dout("%s: snap count is %u\n", __func__, (unsigned int) snap_count);
35938150
AE
2725 while (index < snap_count || links != head) {
2726 u64 snap_id;
2727 struct rbd_snap *snap;
cd892126
AE
2728 char *snap_name;
2729 u64 snap_size = 0;
2730 u64 snap_features = 0;
dfc5606d 2731
35938150
AE
2732 snap_id = index < snap_count ? snapc->snaps[index]
2733 : CEPH_NOSNAP;
2734 snap = links != head ? list_entry(links, struct rbd_snap, node)
2735 : NULL;
aafb230e 2736 rbd_assert(!snap || snap->id != CEPH_NOSNAP);
dfc5606d 2737
35938150
AE
2738 if (snap_id == CEPH_NOSNAP || (snap && snap->id > snap_id)) {
2739 struct list_head *next = links->next;
dfc5606d 2740
35938150 2741 /* Existing snapshot not in the new snap context */
dfc5606d 2742
0d7dbfce 2743 if (rbd_dev->spec->snap_id == snap->id)
daba5fdb 2744 rbd_dev->exists = false;
41f38c2b 2745 rbd_remove_snap_dev(snap);
9fcbb800 2746 dout("%ssnap id %llu has been removed\n",
0d7dbfce
AE
2747 rbd_dev->spec->snap_id == snap->id ?
2748 "mapped " : "",
9fcbb800 2749 (unsigned long long) snap->id);
35938150
AE
2750
2751 /* Done with this list entry; advance */
2752
2753 links = next;
dfc5606d
YS
2754 continue;
2755 }
35938150 2756
b8b1e2db
AE
2757 snap_name = rbd_dev_snap_info(rbd_dev, index,
2758 &snap_size, &snap_features);
cd892126
AE
2759 if (IS_ERR(snap_name))
2760 return PTR_ERR(snap_name);
2761
9fcbb800
AE
2762 dout("entry %u: snap_id = %llu\n", (unsigned int) snap_count,
2763 (unsigned long long) snap_id);
35938150
AE
2764 if (!snap || (snap_id != CEPH_NOSNAP && snap->id < snap_id)) {
2765 struct rbd_snap *new_snap;
2766
2767 /* We haven't seen this snapshot before */
2768
c8d18425 2769 new_snap = __rbd_add_snap_dev(rbd_dev, snap_name,
cd892126 2770 snap_id, snap_size, snap_features);
9fcbb800
AE
2771 if (IS_ERR(new_snap)) {
2772 int err = PTR_ERR(new_snap);
2773
2774 dout(" failed to add dev, error %d\n", err);
2775
2776 return err;
2777 }
35938150
AE
2778
2779 /* New goes before existing, or at end of list */
2780
9fcbb800 2781 dout(" added dev%s\n", snap ? "" : " at end\n");
35938150
AE
2782 if (snap)
2783 list_add_tail(&new_snap->node, &snap->node);
2784 else
523f3258 2785 list_add_tail(&new_snap->node, head);
35938150
AE
2786 } else {
2787 /* Already have this one */
2788
9fcbb800
AE
2789 dout(" already present\n");
2790
cd892126 2791 rbd_assert(snap->size == snap_size);
aafb230e 2792 rbd_assert(!strcmp(snap->name, snap_name));
cd892126 2793 rbd_assert(snap->features == snap_features);
35938150
AE
2794
2795 /* Done with this list entry; advance */
2796
2797 links = links->next;
dfc5606d 2798 }
35938150
AE
2799
2800 /* Advance to the next entry in the snapshot context */
2801
2802 index++;
dfc5606d 2803 }
9fcbb800 2804 dout("%s: done\n", __func__);
dfc5606d
YS
2805
2806 return 0;
2807}
2808
304f6808
AE
2809/*
2810 * Scan the list of snapshots and register the devices for any that
2811 * have not already been registered.
2812 */
2813static int rbd_dev_snaps_register(struct rbd_device *rbd_dev)
2814{
2815 struct rbd_snap *snap;
2816 int ret = 0;
2817
2818 dout("%s called\n", __func__);
86ff77bb
AE
2819 if (WARN_ON(!device_is_registered(&rbd_dev->dev)))
2820 return -EIO;
304f6808
AE
2821
2822 list_for_each_entry(snap, &rbd_dev->snaps, node) {
2823 if (!rbd_snap_registered(snap)) {
2824 ret = rbd_register_snap_dev(snap, &rbd_dev->dev);
2825 if (ret < 0)
2826 break;
2827 }
2828 }
2829 dout("%s: returning %d\n", __func__, ret);
2830
2831 return ret;
2832}
2833
dfc5606d
YS
2834static int rbd_bus_add_dev(struct rbd_device *rbd_dev)
2835{
dfc5606d 2836 struct device *dev;
cd789ab9 2837 int ret;
dfc5606d
YS
2838
2839 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
dfc5606d 2840
cd789ab9 2841 dev = &rbd_dev->dev;
dfc5606d
YS
2842 dev->bus = &rbd_bus_type;
2843 dev->type = &rbd_device_type;
2844 dev->parent = &rbd_root_dev;
2845 dev->release = rbd_dev_release;
de71a297 2846 dev_set_name(dev, "%d", rbd_dev->dev_id);
dfc5606d 2847 ret = device_register(dev);
dfc5606d 2848
dfc5606d 2849 mutex_unlock(&ctl_mutex);
cd789ab9 2850
dfc5606d 2851 return ret;
602adf40
YS
2852}
2853
dfc5606d
YS
2854static void rbd_bus_del_dev(struct rbd_device *rbd_dev)
2855{
2856 device_unregister(&rbd_dev->dev);
2857}
2858
59c2be1e
YS
2859static int rbd_init_watch_dev(struct rbd_device *rbd_dev)
2860{
2861 int ret, rc;
2862
2863 do {
0e6f322d 2864 ret = rbd_req_sync_watch(rbd_dev);
59c2be1e 2865 if (ret == -ERANGE) {
117973fb 2866 rc = rbd_dev_refresh(rbd_dev, NULL);
59c2be1e
YS
2867 if (rc < 0)
2868 return rc;
2869 }
2870 } while (ret == -ERANGE);
2871
2872 return ret;
2873}
2874
e2839308 2875static atomic64_t rbd_dev_id_max = ATOMIC64_INIT(0);
1ddbe94e
AE
2876
2877/*
499afd5b
AE
2878 * Get a unique rbd identifier for the given new rbd_dev, and add
2879 * the rbd_dev to the global list. The minimum rbd id is 1.
1ddbe94e 2880 */
e2839308 2881static void rbd_dev_id_get(struct rbd_device *rbd_dev)
b7f23c36 2882{
e2839308 2883 rbd_dev->dev_id = atomic64_inc_return(&rbd_dev_id_max);
499afd5b
AE
2884
2885 spin_lock(&rbd_dev_list_lock);
2886 list_add_tail(&rbd_dev->node, &rbd_dev_list);
2887 spin_unlock(&rbd_dev_list_lock);
e2839308
AE
2888 dout("rbd_dev %p given dev id %llu\n", rbd_dev,
2889 (unsigned long long) rbd_dev->dev_id);
1ddbe94e 2890}
b7f23c36 2891
1ddbe94e 2892/*
499afd5b
AE
2893 * Remove an rbd_dev from the global list, and record that its
2894 * identifier is no longer in use.
1ddbe94e 2895 */
e2839308 2896static void rbd_dev_id_put(struct rbd_device *rbd_dev)
1ddbe94e 2897{
d184f6bf 2898 struct list_head *tmp;
de71a297 2899 int rbd_id = rbd_dev->dev_id;
d184f6bf
AE
2900 int max_id;
2901
aafb230e 2902 rbd_assert(rbd_id > 0);
499afd5b 2903
e2839308
AE
2904 dout("rbd_dev %p released dev id %llu\n", rbd_dev,
2905 (unsigned long long) rbd_dev->dev_id);
499afd5b
AE
2906 spin_lock(&rbd_dev_list_lock);
2907 list_del_init(&rbd_dev->node);
d184f6bf
AE
2908
2909 /*
2910 * If the id being "put" is not the current maximum, there
2911 * is nothing special we need to do.
2912 */
e2839308 2913 if (rbd_id != atomic64_read(&rbd_dev_id_max)) {
d184f6bf
AE
2914 spin_unlock(&rbd_dev_list_lock);
2915 return;
2916 }
2917
2918 /*
2919 * We need to update the current maximum id. Search the
2920 * list to find out what it is. We're more likely to find
2921 * the maximum at the end, so search the list backward.
2922 */
2923 max_id = 0;
2924 list_for_each_prev(tmp, &rbd_dev_list) {
2925 struct rbd_device *rbd_dev;
2926
2927 rbd_dev = list_entry(tmp, struct rbd_device, node);
b213e0b1
AE
2928 if (rbd_dev->dev_id > max_id)
2929 max_id = rbd_dev->dev_id;
d184f6bf 2930 }
499afd5b 2931 spin_unlock(&rbd_dev_list_lock);
b7f23c36 2932
1ddbe94e 2933 /*
e2839308 2934 * The max id could have been updated by rbd_dev_id_get(), in
d184f6bf
AE
2935 * which case it now accurately reflects the new maximum.
2936 * Be careful not to overwrite the maximum value in that
2937 * case.
1ddbe94e 2938 */
e2839308
AE
2939 atomic64_cmpxchg(&rbd_dev_id_max, rbd_id, max_id);
2940 dout(" max dev id has been reset\n");
b7f23c36
AE
2941}
2942
e28fff26
AE
2943/*
2944 * Skips over white space at *buf, and updates *buf to point to the
2945 * first found non-space character (if any). Returns the length of
593a9e7b
AE
2946 * the token (string of non-white space characters) found. Note
2947 * that *buf must be terminated with '\0'.
e28fff26
AE
2948 */
2949static inline size_t next_token(const char **buf)
2950{
2951 /*
2952 * These are the characters that produce nonzero for
2953 * isspace() in the "C" and "POSIX" locales.
2954 */
2955 const char *spaces = " \f\n\r\t\v";
2956
2957 *buf += strspn(*buf, spaces); /* Find start of token */
2958
2959 return strcspn(*buf, spaces); /* Return token length */
2960}
2961
2962/*
2963 * Finds the next token in *buf, and if the provided token buffer is
2964 * big enough, copies the found token into it. The result, if
593a9e7b
AE
2965 * copied, is guaranteed to be terminated with '\0'. Note that *buf
2966 * must be terminated with '\0' on entry.
e28fff26
AE
2967 *
2968 * Returns the length of the token found (not including the '\0').
2969 * Return value will be 0 if no token is found, and it will be >=
2970 * token_size if the token would not fit.
2971 *
593a9e7b 2972 * The *buf pointer will be updated to point beyond the end of the
e28fff26
AE
2973 * found token. Note that this occurs even if the token buffer is
2974 * too small to hold it.
2975 */
2976static inline size_t copy_token(const char **buf,
2977 char *token,
2978 size_t token_size)
2979{
2980 size_t len;
2981
2982 len = next_token(buf);
2983 if (len < token_size) {
2984 memcpy(token, *buf, len);
2985 *(token + len) = '\0';
2986 }
2987 *buf += len;
2988
2989 return len;
2990}
2991
ea3352f4
AE
2992/*
2993 * Finds the next token in *buf, dynamically allocates a buffer big
2994 * enough to hold a copy of it, and copies the token into the new
2995 * buffer. The copy is guaranteed to be terminated with '\0'. Note
2996 * that a duplicate buffer is created even for a zero-length token.
2997 *
2998 * Returns a pointer to the newly-allocated duplicate, or a null
2999 * pointer if memory for the duplicate was not available. If
3000 * the lenp argument is a non-null pointer, the length of the token
3001 * (not including the '\0') is returned in *lenp.
3002 *
3003 * If successful, the *buf pointer will be updated to point beyond
3004 * the end of the found token.
3005 *
3006 * Note: uses GFP_KERNEL for allocation.
3007 */
3008static inline char *dup_token(const char **buf, size_t *lenp)
3009{
3010 char *dup;
3011 size_t len;
3012
3013 len = next_token(buf);
3014 dup = kmalloc(len + 1, GFP_KERNEL);
3015 if (!dup)
3016 return NULL;
3017
3018 memcpy(dup, *buf, len);
3019 *(dup + len) = '\0';
3020 *buf += len;
3021
3022 if (lenp)
3023 *lenp = len;
3024
3025 return dup;
3026}
3027
a725f65e 3028/*
859c31df
AE
3029 * Parse the options provided for an "rbd add" (i.e., rbd image
3030 * mapping) request. These arrive via a write to /sys/bus/rbd/add,
3031 * and the data written is passed here via a NUL-terminated buffer.
3032 * Returns 0 if successful or an error code otherwise.
d22f76e7 3033 *
859c31df
AE
3034 * The information extracted from these options is recorded in
3035 * the other parameters which return dynamically-allocated
3036 * structures:
3037 * ceph_opts
3038 * The address of a pointer that will refer to a ceph options
3039 * structure. Caller must release the returned pointer using
3040 * ceph_destroy_options() when it is no longer needed.
3041 * rbd_opts
3042 * Address of an rbd options pointer. Fully initialized by
3043 * this function; caller must release with kfree().
3044 * spec
3045 * Address of an rbd image specification pointer. Fully
3046 * initialized by this function based on parsed options.
3047 * Caller must release with rbd_spec_put().
3048 *
3049 * The options passed take this form:
3050 * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
3051 * where:
3052 * <mon_addrs>
3053 * A comma-separated list of one or more monitor addresses.
3054 * A monitor address is an ip address, optionally followed
3055 * by a port number (separated by a colon).
3056 * I.e.: ip1[:port1][,ip2[:port2]...]
3057 * <options>
3058 * A comma-separated list of ceph and/or rbd options.
3059 * <pool_name>
3060 * The name of the rados pool containing the rbd image.
3061 * <image_name>
3062 * The name of the image in that pool to map.
3063 * <snap_id>
3064 * An optional snapshot id. If provided, the mapping will
3065 * present data from the image at the time that snapshot was
3066 * created. The image head is used if no snapshot id is
3067 * provided. Snapshot mappings are always read-only.
a725f65e 3068 */
859c31df 3069static int rbd_add_parse_args(const char *buf,
dc79b113 3070 struct ceph_options **ceph_opts,
859c31df
AE
3071 struct rbd_options **opts,
3072 struct rbd_spec **rbd_spec)
e28fff26 3073{
d22f76e7 3074 size_t len;
859c31df 3075 char *options;
0ddebc0c
AE
3076 const char *mon_addrs;
3077 size_t mon_addrs_size;
859c31df 3078 struct rbd_spec *spec = NULL;
4e9afeba 3079 struct rbd_options *rbd_opts = NULL;
859c31df 3080 struct ceph_options *copts;
dc79b113 3081 int ret;
e28fff26
AE
3082
3083 /* The first four tokens are required */
3084
7ef3214a
AE
3085 len = next_token(&buf);
3086 if (!len)
dc79b113 3087 return -EINVAL; /* Missing monitor address(es) */
0ddebc0c 3088 mon_addrs = buf;
f28e565a 3089 mon_addrs_size = len + 1;
7ef3214a 3090 buf += len;
a725f65e 3091
dc79b113 3092 ret = -EINVAL;
f28e565a
AE
3093 options = dup_token(&buf, NULL);
3094 if (!options)
dc79b113 3095 return -ENOMEM;
f28e565a
AE
3096 if (!*options)
3097 goto out_err; /* Missing options */
e28fff26 3098
859c31df
AE
3099 spec = rbd_spec_alloc();
3100 if (!spec)
f28e565a 3101 goto out_mem;
859c31df
AE
3102
3103 spec->pool_name = dup_token(&buf, NULL);
3104 if (!spec->pool_name)
3105 goto out_mem;
3106 if (!*spec->pool_name)
f28e565a 3107 goto out_err; /* Missing pool name */
e28fff26 3108
859c31df
AE
3109 spec->image_name = dup_token(&buf, &spec->image_name_len);
3110 if (!spec->image_name)
f28e565a 3111 goto out_mem;
859c31df 3112 if (!*spec->image_name)
f28e565a 3113 goto out_err; /* Missing image name */
d4b125e9 3114
f28e565a
AE
3115 /*
3116 * Snapshot name is optional; default is to use "-"
3117 * (indicating the head/no snapshot).
3118 */
3feeb894 3119 len = next_token(&buf);
820a5f3e 3120 if (!len) {
3feeb894
AE
3121 buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
3122 len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
f28e565a 3123 } else if (len > RBD_MAX_SNAP_NAME_LEN) {
dc79b113 3124 ret = -ENAMETOOLONG;
f28e565a 3125 goto out_err;
849b4260 3126 }
859c31df
AE
3127 spec->snap_name = kmalloc(len + 1, GFP_KERNEL);
3128 if (!spec->snap_name)
f28e565a 3129 goto out_mem;
859c31df
AE
3130 memcpy(spec->snap_name, buf, len);
3131 *(spec->snap_name + len) = '\0';
e5c35534 3132
0ddebc0c 3133 /* Initialize all rbd options to the defaults */
e28fff26 3134
4e9afeba
AE
3135 rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL);
3136 if (!rbd_opts)
3137 goto out_mem;
3138
3139 rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
d22f76e7 3140
859c31df 3141 copts = ceph_parse_options(options, mon_addrs,
0ddebc0c 3142 mon_addrs + mon_addrs_size - 1,
4e9afeba 3143 parse_rbd_opts_token, rbd_opts);
859c31df
AE
3144 if (IS_ERR(copts)) {
3145 ret = PTR_ERR(copts);
dc79b113
AE
3146 goto out_err;
3147 }
859c31df
AE
3148 kfree(options);
3149
3150 *ceph_opts = copts;
4e9afeba 3151 *opts = rbd_opts;
859c31df 3152 *rbd_spec = spec;
0ddebc0c 3153
dc79b113 3154 return 0;
f28e565a 3155out_mem:
dc79b113 3156 ret = -ENOMEM;
d22f76e7 3157out_err:
859c31df
AE
3158 kfree(rbd_opts);
3159 rbd_spec_put(spec);
f28e565a 3160 kfree(options);
d22f76e7 3161
dc79b113 3162 return ret;
a725f65e
AE
3163}
3164
589d30e0
AE
3165/*
3166 * An rbd format 2 image has a unique identifier, distinct from the
3167 * name given to it by the user. Internally, that identifier is
3168 * what's used to specify the names of objects related to the image.
3169 *
3170 * A special "rbd id" object is used to map an rbd image name to its
3171 * id. If that object doesn't exist, then there is no v2 rbd image
3172 * with the supplied name.
3173 *
3174 * This function will record the given rbd_dev's image_id field if
3175 * it can be determined, and in that case will return 0. If any
3176 * errors occur a negative errno will be returned and the rbd_dev's
3177 * image_id field will be unchanged (and should be NULL).
3178 */
3179static int rbd_dev_image_id(struct rbd_device *rbd_dev)
3180{
3181 int ret;
3182 size_t size;
3183 char *object_name;
3184 void *response;
3185 void *p;
3186
2c0d0a10
AE
3187 /*
3188 * When probing a parent image, the image id is already
3189 * known (and the image name likely is not). There's no
3190 * need to fetch the image id again in this case.
3191 */
3192 if (rbd_dev->spec->image_id)
3193 return 0;
3194
589d30e0
AE
3195 /*
3196 * First, see if the format 2 image id file exists, and if
3197 * so, get the image's persistent id from it.
3198 */
0d7dbfce 3199 size = sizeof (RBD_ID_PREFIX) + rbd_dev->spec->image_name_len;
589d30e0
AE
3200 object_name = kmalloc(size, GFP_NOIO);
3201 if (!object_name)
3202 return -ENOMEM;
0d7dbfce 3203 sprintf(object_name, "%s%s", RBD_ID_PREFIX, rbd_dev->spec->image_name);
589d30e0
AE
3204 dout("rbd id object name is %s\n", object_name);
3205
3206 /* Response will be an encoded string, which includes a length */
3207
3208 size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
3209 response = kzalloc(size, GFP_NOIO);
3210 if (!response) {
3211 ret = -ENOMEM;
3212 goto out;
3213 }
3214
3215 ret = rbd_req_sync_exec(rbd_dev, object_name,
3216 "rbd", "get_id",
3217 NULL, 0,
3218 response, RBD_IMAGE_ID_LEN_MAX,
3219 CEPH_OSD_FLAG_READ, NULL);
3220 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
3221 if (ret < 0)
3222 goto out;
a0ea3a40 3223 ret = 0; /* rbd_req_sync_exec() can return positive */
589d30e0
AE
3224
3225 p = response;
0d7dbfce 3226 rbd_dev->spec->image_id = ceph_extract_encoded_string(&p,
589d30e0 3227 p + RBD_IMAGE_ID_LEN_MAX,
0d7dbfce 3228 &rbd_dev->spec->image_id_len,
589d30e0 3229 GFP_NOIO);
0d7dbfce
AE
3230 if (IS_ERR(rbd_dev->spec->image_id)) {
3231 ret = PTR_ERR(rbd_dev->spec->image_id);
3232 rbd_dev->spec->image_id = NULL;
589d30e0 3233 } else {
0d7dbfce 3234 dout("image_id is %s\n", rbd_dev->spec->image_id);
589d30e0
AE
3235 }
3236out:
3237 kfree(response);
3238 kfree(object_name);
3239
3240 return ret;
3241}
3242
a30b71b9
AE
3243static int rbd_dev_v1_probe(struct rbd_device *rbd_dev)
3244{
3245 int ret;
3246 size_t size;
3247
3248 /* Version 1 images have no id; empty string is used */
3249
0d7dbfce
AE
3250 rbd_dev->spec->image_id = kstrdup("", GFP_KERNEL);
3251 if (!rbd_dev->spec->image_id)
a30b71b9 3252 return -ENOMEM;
0d7dbfce 3253 rbd_dev->spec->image_id_len = 0;
a30b71b9
AE
3254
3255 /* Record the header object name for this rbd image. */
3256
0d7dbfce 3257 size = rbd_dev->spec->image_name_len + sizeof (RBD_SUFFIX);
a30b71b9
AE
3258 rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
3259 if (!rbd_dev->header_name) {
3260 ret = -ENOMEM;
3261 goto out_err;
3262 }
0d7dbfce
AE
3263 sprintf(rbd_dev->header_name, "%s%s",
3264 rbd_dev->spec->image_name, RBD_SUFFIX);
a30b71b9
AE
3265
3266 /* Populate rbd image metadata */
3267
3268 ret = rbd_read_header(rbd_dev, &rbd_dev->header);
3269 if (ret < 0)
3270 goto out_err;
86b00e0d
AE
3271
3272 /* Version 1 images have no parent (no layering) */
3273
3274 rbd_dev->parent_spec = NULL;
3275 rbd_dev->parent_overlap = 0;
3276
a30b71b9
AE
3277 rbd_dev->image_format = 1;
3278
3279 dout("discovered version 1 image, header name is %s\n",
3280 rbd_dev->header_name);
3281
3282 return 0;
3283
3284out_err:
3285 kfree(rbd_dev->header_name);
3286 rbd_dev->header_name = NULL;
0d7dbfce
AE
3287 kfree(rbd_dev->spec->image_id);
3288 rbd_dev->spec->image_id = NULL;
a30b71b9
AE
3289
3290 return ret;
3291}
3292
3293static int rbd_dev_v2_probe(struct rbd_device *rbd_dev)
3294{
3295 size_t size;
9d475de5 3296 int ret;
6e14b1a6 3297 u64 ver = 0;
a30b71b9
AE
3298
3299 /*
3300 * Image id was filled in by the caller. Record the header
3301 * object name for this rbd image.
3302 */
0d7dbfce 3303 size = sizeof (RBD_HEADER_PREFIX) + rbd_dev->spec->image_id_len;
a30b71b9
AE
3304 rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
3305 if (!rbd_dev->header_name)
3306 return -ENOMEM;
3307 sprintf(rbd_dev->header_name, "%s%s",
0d7dbfce 3308 RBD_HEADER_PREFIX, rbd_dev->spec->image_id);
9d475de5
AE
3309
3310 /* Get the size and object order for the image */
3311
3312 ret = rbd_dev_v2_image_size(rbd_dev);
1e130199
AE
3313 if (ret < 0)
3314 goto out_err;
3315
3316 /* Get the object prefix (a.k.a. block_name) for the image */
3317
3318 ret = rbd_dev_v2_object_prefix(rbd_dev);
b1b5402a
AE
3319 if (ret < 0)
3320 goto out_err;
3321
d889140c 3322 /* Get the and check features for the image */
b1b5402a
AE
3323
3324 ret = rbd_dev_v2_features(rbd_dev);
9d475de5
AE
3325 if (ret < 0)
3326 goto out_err;
35d489f9 3327
86b00e0d
AE
3328 /* If the image supports layering, get the parent info */
3329
3330 if (rbd_dev->header.features & RBD_FEATURE_LAYERING) {
3331 ret = rbd_dev_v2_parent_info(rbd_dev);
3332 if (ret < 0)
3333 goto out_err;
3334 }
3335
6e14b1a6
AE
3336 /* crypto and compression type aren't (yet) supported for v2 images */
3337
3338 rbd_dev->header.crypt_type = 0;
3339 rbd_dev->header.comp_type = 0;
35d489f9 3340
6e14b1a6
AE
3341 /* Get the snapshot context, plus the header version */
3342
3343 ret = rbd_dev_v2_snap_context(rbd_dev, &ver);
35d489f9
AE
3344 if (ret)
3345 goto out_err;
6e14b1a6
AE
3346 rbd_dev->header.obj_version = ver;
3347
a30b71b9
AE
3348 rbd_dev->image_format = 2;
3349
3350 dout("discovered version 2 image, header name is %s\n",
3351 rbd_dev->header_name);
3352
35152979 3353 return 0;
9d475de5 3354out_err:
86b00e0d
AE
3355 rbd_dev->parent_overlap = 0;
3356 rbd_spec_put(rbd_dev->parent_spec);
3357 rbd_dev->parent_spec = NULL;
9d475de5
AE
3358 kfree(rbd_dev->header_name);
3359 rbd_dev->header_name = NULL;
1e130199
AE
3360 kfree(rbd_dev->header.object_prefix);
3361 rbd_dev->header.object_prefix = NULL;
9d475de5
AE
3362
3363 return ret;
a30b71b9
AE
3364}
3365
83a06263
AE
3366static int rbd_dev_probe_finish(struct rbd_device *rbd_dev)
3367{
3368 int ret;
3369
3370 /* no need to lock here, as rbd_dev is not registered yet */
3371 ret = rbd_dev_snaps_update(rbd_dev);
3372 if (ret)
3373 return ret;
3374
3375 ret = rbd_dev_set_mapping(rbd_dev);
3376 if (ret)
3377 goto err_out_snaps;
3378
3379 /* generate unique id: find highest unique id, add one */
3380 rbd_dev_id_get(rbd_dev);
3381
3382 /* Fill in the device name, now that we have its id. */
3383 BUILD_BUG_ON(DEV_NAME_LEN
3384 < sizeof (RBD_DRV_NAME) + MAX_INT_FORMAT_WIDTH);
3385 sprintf(rbd_dev->name, "%s%d", RBD_DRV_NAME, rbd_dev->dev_id);
3386
3387 /* Get our block major device number. */
3388
3389 ret = register_blkdev(0, rbd_dev->name);
3390 if (ret < 0)
3391 goto err_out_id;
3392 rbd_dev->major = ret;
3393
3394 /* Set up the blkdev mapping. */
3395
3396 ret = rbd_init_disk(rbd_dev);
3397 if (ret)
3398 goto err_out_blkdev;
3399
3400 ret = rbd_bus_add_dev(rbd_dev);
3401 if (ret)
3402 goto err_out_disk;
3403
3404 /*
3405 * At this point cleanup in the event of an error is the job
3406 * of the sysfs code (initiated by rbd_bus_del_dev()).
3407 */
3408 down_write(&rbd_dev->header_rwsem);
3409 ret = rbd_dev_snaps_register(rbd_dev);
3410 up_write(&rbd_dev->header_rwsem);
3411 if (ret)
3412 goto err_out_bus;
3413
3414 ret = rbd_init_watch_dev(rbd_dev);
3415 if (ret)
3416 goto err_out_bus;
3417
3418 /* Everything's ready. Announce the disk to the world. */
3419
3420 add_disk(rbd_dev->disk);
3421
3422 pr_info("%s: added with size 0x%llx\n", rbd_dev->disk->disk_name,
3423 (unsigned long long) rbd_dev->mapping.size);
3424
3425 return ret;
3426err_out_bus:
3427 /* this will also clean up rest of rbd_dev stuff */
3428
3429 rbd_bus_del_dev(rbd_dev);
3430
3431 return ret;
3432err_out_disk:
3433 rbd_free_disk(rbd_dev);
3434err_out_blkdev:
3435 unregister_blkdev(rbd_dev->major, rbd_dev->name);
3436err_out_id:
3437 rbd_dev_id_put(rbd_dev);
3438err_out_snaps:
3439 rbd_remove_all_snaps(rbd_dev);
3440
3441 return ret;
3442}
3443
a30b71b9
AE
3444/*
3445 * Probe for the existence of the header object for the given rbd
3446 * device. For format 2 images this includes determining the image
3447 * id.
3448 */
3449static int rbd_dev_probe(struct rbd_device *rbd_dev)
3450{
3451 int ret;
3452
3453 /*
3454 * Get the id from the image id object. If it's not a
3455 * format 2 image, we'll get ENOENT back, and we'll assume
3456 * it's a format 1 image.
3457 */
3458 ret = rbd_dev_image_id(rbd_dev);
3459 if (ret)
3460 ret = rbd_dev_v1_probe(rbd_dev);
3461 else
3462 ret = rbd_dev_v2_probe(rbd_dev);
83a06263 3463 if (ret) {
a30b71b9
AE
3464 dout("probe failed, returning %d\n", ret);
3465
83a06263
AE
3466 return ret;
3467 }
3468
3469 ret = rbd_dev_probe_finish(rbd_dev);
3470 if (ret)
3471 rbd_header_free(&rbd_dev->header);
3472
a30b71b9
AE
3473 return ret;
3474}
3475
59c2be1e
YS
3476static ssize_t rbd_add(struct bus_type *bus,
3477 const char *buf,
3478 size_t count)
602adf40 3479{
cb8627c7 3480 struct rbd_device *rbd_dev = NULL;
dc79b113 3481 struct ceph_options *ceph_opts = NULL;
4e9afeba 3482 struct rbd_options *rbd_opts = NULL;
859c31df 3483 struct rbd_spec *spec = NULL;
9d3997fd 3484 struct rbd_client *rbdc;
27cc2594
AE
3485 struct ceph_osd_client *osdc;
3486 int rc = -ENOMEM;
602adf40
YS
3487
3488 if (!try_module_get(THIS_MODULE))
3489 return -ENODEV;
3490
602adf40 3491 /* parse add command */
859c31df 3492 rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec);
dc79b113 3493 if (rc < 0)
bd4ba655 3494 goto err_out_module;
78cea76e 3495
9d3997fd
AE
3496 rbdc = rbd_get_client(ceph_opts);
3497 if (IS_ERR(rbdc)) {
3498 rc = PTR_ERR(rbdc);
0ddebc0c 3499 goto err_out_args;
9d3997fd 3500 }
c53d5893 3501 ceph_opts = NULL; /* rbd_dev client now owns this */
602adf40 3502
602adf40 3503 /* pick the pool */
9d3997fd 3504 osdc = &rbdc->client->osdc;
859c31df 3505 rc = ceph_pg_poolid_by_name(osdc->osdmap, spec->pool_name);
602adf40
YS
3506 if (rc < 0)
3507 goto err_out_client;
859c31df
AE
3508 spec->pool_id = (u64) rc;
3509
c53d5893 3510 rbd_dev = rbd_dev_create(rbdc, spec);
bd4ba655
AE
3511 if (!rbd_dev)
3512 goto err_out_client;
c53d5893
AE
3513 rbdc = NULL; /* rbd_dev now owns this */
3514 spec = NULL; /* rbd_dev now owns this */
602adf40 3515
bd4ba655 3516 rbd_dev->mapping.read_only = rbd_opts->read_only;
c53d5893
AE
3517 kfree(rbd_opts);
3518 rbd_opts = NULL; /* done with this */
bd4ba655 3519
a30b71b9
AE
3520 rc = rbd_dev_probe(rbd_dev);
3521 if (rc < 0)
c53d5893 3522 goto err_out_rbd_dev;
05fd6f6f 3523
602adf40 3524 return count;
c53d5893
AE
3525err_out_rbd_dev:
3526 rbd_dev_destroy(rbd_dev);
bd4ba655 3527err_out_client:
9d3997fd 3528 rbd_put_client(rbdc);
0ddebc0c 3529err_out_args:
78cea76e
AE
3530 if (ceph_opts)
3531 ceph_destroy_options(ceph_opts);
4e9afeba 3532 kfree(rbd_opts);
859c31df 3533 rbd_spec_put(spec);
bd4ba655
AE
3534err_out_module:
3535 module_put(THIS_MODULE);
27cc2594 3536
602adf40 3537 dout("Error adding device %s\n", buf);
27cc2594
AE
3538
3539 return (ssize_t) rc;
602adf40
YS
3540}
3541
de71a297 3542static struct rbd_device *__rbd_get_dev(unsigned long dev_id)
602adf40
YS
3543{
3544 struct list_head *tmp;
3545 struct rbd_device *rbd_dev;
3546
e124a82f 3547 spin_lock(&rbd_dev_list_lock);
602adf40
YS
3548 list_for_each(tmp, &rbd_dev_list) {
3549 rbd_dev = list_entry(tmp, struct rbd_device, node);
de71a297 3550 if (rbd_dev->dev_id == dev_id) {
e124a82f 3551 spin_unlock(&rbd_dev_list_lock);
602adf40 3552 return rbd_dev;
e124a82f 3553 }
602adf40 3554 }
e124a82f 3555 spin_unlock(&rbd_dev_list_lock);
602adf40
YS
3556 return NULL;
3557}
3558
dfc5606d 3559static void rbd_dev_release(struct device *dev)
602adf40 3560{
593a9e7b 3561 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
602adf40 3562
1dbb4399
AE
3563 if (rbd_dev->watch_request) {
3564 struct ceph_client *client = rbd_dev->rbd_client->client;
3565
3566 ceph_osdc_unregister_linger_request(&client->osdc,
59c2be1e 3567 rbd_dev->watch_request);
1dbb4399 3568 }
59c2be1e 3569 if (rbd_dev->watch_event)
070c633f 3570 rbd_req_sync_unwatch(rbd_dev);
59c2be1e 3571
602adf40
YS
3572
3573 /* clean up and free blkdev */
3574 rbd_free_disk(rbd_dev);
3575 unregister_blkdev(rbd_dev->major, rbd_dev->name);
32eec68d 3576
2ac4e75d
AE
3577 /* release allocated disk header fields */
3578 rbd_header_free(&rbd_dev->header);
3579
32eec68d 3580 /* done with the id, and with the rbd_dev */
e2839308 3581 rbd_dev_id_put(rbd_dev);
c53d5893
AE
3582 rbd_assert(rbd_dev->rbd_client != NULL);
3583 rbd_dev_destroy(rbd_dev);
602adf40
YS
3584
3585 /* release module ref */
3586 module_put(THIS_MODULE);
602adf40
YS
3587}
3588
dfc5606d
YS
3589static ssize_t rbd_remove(struct bus_type *bus,
3590 const char *buf,
3591 size_t count)
602adf40
YS
3592{
3593 struct rbd_device *rbd_dev = NULL;
3594 int target_id, rc;
3595 unsigned long ul;
3596 int ret = count;
3597
3598 rc = strict_strtoul(buf, 10, &ul);
3599 if (rc)
3600 return rc;
3601
3602 /* convert to int; abort if we lost anything in the conversion */
3603 target_id = (int) ul;
3604 if (target_id != ul)
3605 return -EINVAL;
3606
3607 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
3608
3609 rbd_dev = __rbd_get_dev(target_id);
3610 if (!rbd_dev) {
3611 ret = -ENOENT;
3612 goto done;
3613 }
3614
41f38c2b 3615 rbd_remove_all_snaps(rbd_dev);
dfc5606d 3616 rbd_bus_del_dev(rbd_dev);
602adf40
YS
3617
3618done:
3619 mutex_unlock(&ctl_mutex);
aafb230e 3620
602adf40
YS
3621 return ret;
3622}
3623
602adf40
YS
3624/*
3625 * create control files in sysfs
dfc5606d 3626 * /sys/bus/rbd/...
602adf40
YS
3627 */
3628static int rbd_sysfs_init(void)
3629{
dfc5606d 3630 int ret;
602adf40 3631
fed4c143 3632 ret = device_register(&rbd_root_dev);
21079786 3633 if (ret < 0)
dfc5606d 3634 return ret;
602adf40 3635
fed4c143
AE
3636 ret = bus_register(&rbd_bus_type);
3637 if (ret < 0)
3638 device_unregister(&rbd_root_dev);
602adf40 3639
602adf40
YS
3640 return ret;
3641}
3642
3643static void rbd_sysfs_cleanup(void)
3644{
dfc5606d 3645 bus_unregister(&rbd_bus_type);
fed4c143 3646 device_unregister(&rbd_root_dev);
602adf40
YS
3647}
3648
3649int __init rbd_init(void)
3650{
3651 int rc;
3652
3653 rc = rbd_sysfs_init();
3654 if (rc)
3655 return rc;
f0f8cef5 3656 pr_info("loaded " RBD_DRV_NAME_LONG "\n");
602adf40
YS
3657 return 0;
3658}
3659
3660void __exit rbd_exit(void)
3661{
3662 rbd_sysfs_cleanup();
3663}
3664
3665module_init(rbd_init);
3666module_exit(rbd_exit);
3667
3668MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
3669MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
3670MODULE_DESCRIPTION("rados block device");
3671
3672/* following authorship retained from original osdblk.c */
3673MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
3674
3675MODULE_LICENSE("GPL");
This page took 0.377214 seconds and 5 git commands to generate.