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