rbd: end request on error in rbd_do_request() caller
[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;
8986cb37 174 s32 rc;
1fec7093
YS
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,
8986cb37 1056 s32 ret, u64 len)
1fec7093
YS
1057{
1058 struct request_queue *q;
1059 int min, max, i;
1060
bd919d45 1061 dout("rbd_coll_end_req_index %p index %d ret %d len %llu\n",
8986cb37 1062 coll, index, (int)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++) {
8986cb37 1083 __blk_end_request(rq, (int)coll->status[i].rc,
1fec7093
YS
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
725afc97 1091static void rbd_coll_end_req(struct rbd_request *rbd_req,
8986cb37 1092 s32 ret, u64 len)
1fec7093 1093{
725afc97
AE
1094 rbd_coll_end_req_index(rbd_req->rq,
1095 rbd_req->coll, rbd_req->coll_index,
1096 ret, len);
1fec7093
YS
1097}
1098
602adf40
YS
1099/*
1100 * Send ceph osd request
1101 */
1102static int rbd_do_request(struct request *rq,
0ce1a794 1103 struct rbd_device *rbd_dev,
602adf40
YS
1104 struct ceph_snap_context *snapc,
1105 u64 snapid,
aded07ea 1106 const char *object_name, u64 ofs, u64 len,
602adf40
YS
1107 struct bio *bio,
1108 struct page **pages,
1109 int num_pages,
1110 int flags,
1111 struct ceph_osd_req_op *ops,
1fec7093
YS
1112 struct rbd_req_coll *coll,
1113 int coll_index,
5f29ddd4
AE
1114 void (*rbd_cb)(struct ceph_osd_request *,
1115 struct ceph_msg *),
59c2be1e
YS
1116 struct ceph_osd_request **linger_req,
1117 u64 *ver)
602adf40 1118{
5f29ddd4 1119 struct ceph_osd_request *osd_req;
602adf40
YS
1120 struct ceph_file_layout *layout;
1121 int ret;
1122 u64 bno;
1123 struct timespec mtime = CURRENT_TIME;
725afc97 1124 struct rbd_request *rbd_req;
602adf40 1125 struct ceph_osd_request_head *reqhead;
1dbb4399 1126 struct ceph_osd_client *osdc;
602adf40 1127
725afc97 1128 rbd_req = kzalloc(sizeof(*rbd_req), GFP_NOIO);
cd323ac0 1129 if (!rbd_req)
1fec7093 1130 return -ENOMEM;
1fec7093
YS
1131
1132 if (coll) {
725afc97
AE
1133 rbd_req->coll = coll;
1134 rbd_req->coll_index = coll_index;
1fec7093 1135 }
602adf40 1136
f7760dad
AE
1137 dout("rbd_do_request object_name=%s ofs=%llu len=%llu coll=%p[%d]\n",
1138 object_name, (unsigned long long) ofs,
1139 (unsigned long long) len, coll, coll_index);
602adf40 1140
0ce1a794 1141 osdc = &rbd_dev->rbd_client->client->osdc;
5f29ddd4 1142 osd_req = ceph_osdc_alloc_request(osdc, flags, snapc, ops,
1dbb4399 1143 false, GFP_NOIO, pages, bio);
5f29ddd4 1144 if (!osd_req) {
4ad12621 1145 ret = -ENOMEM;
602adf40
YS
1146 goto done_pages;
1147 }
1148
5f29ddd4 1149 osd_req->r_callback = rbd_cb;
602adf40 1150
725afc97
AE
1151 rbd_req->rq = rq;
1152 rbd_req->bio = bio;
1153 rbd_req->pages = pages;
1154 rbd_req->len = len;
602adf40 1155
5f29ddd4 1156 osd_req->r_priv = rbd_req;
602adf40 1157
5f29ddd4 1158 reqhead = osd_req->r_request->front.iov_base;
602adf40
YS
1159 reqhead->snapid = cpu_to_le64(CEPH_NOSNAP);
1160
5f29ddd4
AE
1161 strncpy(osd_req->r_oid, object_name, sizeof(osd_req->r_oid));
1162 osd_req->r_oid_len = strlen(osd_req->r_oid);
602adf40 1163
5f29ddd4 1164 layout = &osd_req->r_file_layout;
602adf40
YS
1165 memset(layout, 0, sizeof(*layout));
1166 layout->fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
1167 layout->fl_stripe_count = cpu_to_le32(1);
1168 layout->fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
0d7dbfce 1169 layout->fl_pg_pool = cpu_to_le32((int) rbd_dev->spec->pool_id);
6cae3717 1170 ret = ceph_calc_raw_layout(osdc, layout, snapid, ofs, &len, &bno,
5f29ddd4 1171 osd_req, ops);
6cae3717 1172 rbd_assert(ret == 0);
602adf40 1173
5f29ddd4 1174 ceph_osdc_build_request(osd_req, ofs, &len,
602adf40
YS
1175 ops,
1176 snapc,
1177 &mtime,
5f29ddd4 1178 osd_req->r_oid, osd_req->r_oid_len);
602adf40 1179
59c2be1e 1180 if (linger_req) {
5f29ddd4
AE
1181 ceph_osdc_set_request_linger(osdc, osd_req);
1182 *linger_req = osd_req;
59c2be1e
YS
1183 }
1184
5f29ddd4 1185 ret = ceph_osdc_start_request(osdc, osd_req, false);
602adf40
YS
1186 if (ret < 0)
1187 goto done_err;
1188
1189 if (!rbd_cb) {
5f29ddd4
AE
1190 u64 version;
1191
1192 ret = ceph_osdc_wait_request(osdc, osd_req);
1193 version = le64_to_cpu(osd_req->r_reassert_version.version);
59c2be1e 1194 if (ver)
5f29ddd4
AE
1195 *ver = version;
1196 dout("reassert_ver=%llu\n", (unsigned long long) version);
1197 ceph_osdc_put_request(osd_req);
602adf40
YS
1198 }
1199 return ret;
1200
1201done_err:
725afc97 1202 bio_chain_put(rbd_req->bio);
5f29ddd4 1203 ceph_osdc_put_request(osd_req);
602adf40 1204done_pages:
725afc97 1205 kfree(rbd_req);
602adf40
YS
1206 return ret;
1207}
1208
1209/*
1210 * Ceph osd op callback
1211 */
5f29ddd4 1212static void rbd_req_cb(struct ceph_osd_request *osd_req, struct ceph_msg *msg)
602adf40 1213{
5f29ddd4 1214 struct rbd_request *rbd_req = osd_req->r_priv;
602adf40
YS
1215 struct ceph_osd_reply_head *replyhead;
1216 struct ceph_osd_op *op;
8986cb37 1217 s32 rc;
602adf40
YS
1218 u64 bytes;
1219 int read_op;
1220
1221 /* parse reply */
1222 replyhead = msg->front.iov_base;
1223 WARN_ON(le32_to_cpu(replyhead->num_ops) == 0);
1224 op = (void *)(replyhead + 1);
8986cb37 1225 rc = (s32)le32_to_cpu(replyhead->result);
602adf40 1226 bytes = le64_to_cpu(op->extent.length);
895cfcc8 1227 read_op = (le16_to_cpu(op->op) == CEPH_OSD_OP_READ);
602adf40 1228
bd919d45
AE
1229 dout("rbd_req_cb bytes=%llu readop=%d rc=%d\n",
1230 (unsigned long long) bytes, read_op, (int) rc);
602adf40 1231
8986cb37 1232 if (rc == (s32)-ENOENT && read_op) {
725afc97 1233 zero_bio_chain(rbd_req->bio, 0);
602adf40 1234 rc = 0;
725afc97
AE
1235 } else if (rc == 0 && read_op && bytes < rbd_req->len) {
1236 zero_bio_chain(rbd_req->bio, bytes);
1237 bytes = rbd_req->len;
602adf40
YS
1238 }
1239
725afc97 1240 rbd_coll_end_req(rbd_req, rc, bytes);
602adf40 1241
725afc97
AE
1242 if (rbd_req->bio)
1243 bio_chain_put(rbd_req->bio);
602adf40 1244
5f29ddd4 1245 ceph_osdc_put_request(osd_req);
725afc97 1246 kfree(rbd_req);
602adf40
YS
1247}
1248
5f29ddd4
AE
1249static void rbd_simple_req_cb(struct ceph_osd_request *osd_req,
1250 struct ceph_msg *msg)
59c2be1e 1251{
5f29ddd4 1252 ceph_osdc_put_request(osd_req);
59c2be1e
YS
1253}
1254
602adf40
YS
1255/*
1256 * Do a synchronous ceph osd operation
1257 */
0ce1a794 1258static int rbd_req_sync_op(struct rbd_device *rbd_dev,
602adf40
YS
1259 struct ceph_snap_context *snapc,
1260 u64 snapid,
602adf40 1261 int flags,
913d2fdc 1262 struct ceph_osd_req_op *ops,
aded07ea 1263 const char *object_name,
f8d4de6e
AE
1264 u64 ofs, u64 inbound_size,
1265 char *inbound,
59c2be1e
YS
1266 struct ceph_osd_request **linger_req,
1267 u64 *ver)
602adf40
YS
1268{
1269 int ret;
1270 struct page **pages;
1271 int num_pages;
913d2fdc 1272
aafb230e 1273 rbd_assert(ops != NULL);
602adf40 1274
f8d4de6e 1275 num_pages = calc_pages_for(ofs, inbound_size);
602adf40 1276 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
b8d0638a
DC
1277 if (IS_ERR(pages))
1278 return PTR_ERR(pages);
602adf40 1279
0ce1a794 1280 ret = rbd_do_request(NULL, rbd_dev, snapc, snapid,
f8d4de6e 1281 object_name, ofs, inbound_size, NULL,
602adf40
YS
1282 pages, num_pages,
1283 flags,
1284 ops,
1fec7093 1285 NULL, 0,
59c2be1e
YS
1286 NULL,
1287 linger_req, ver);
602adf40 1288 if (ret < 0)
913d2fdc 1289 goto done;
602adf40 1290
f8d4de6e
AE
1291 if ((flags & CEPH_OSD_FLAG_READ) && inbound)
1292 ret = ceph_copy_from_page_vector(pages, inbound, ofs, ret);
602adf40 1293
602adf40
YS
1294done:
1295 ceph_release_page_vector(pages, num_pages);
1296 return ret;
1297}
1298
1299/*
1300 * Do an asynchronous ceph osd operation
1301 */
1302static int rbd_do_op(struct request *rq,
0ce1a794 1303 struct rbd_device *rbd_dev,
602adf40 1304 struct ceph_snap_context *snapc,
602adf40 1305 u64 ofs, u64 len,
1fec7093
YS
1306 struct bio *bio,
1307 struct rbd_req_coll *coll,
1308 int coll_index)
602adf40
YS
1309{
1310 char *seg_name;
1311 u64 seg_ofs;
1312 u64 seg_len;
1313 int ret;
1314 struct ceph_osd_req_op *ops;
1315 u32 payload_len;
ff2e4bb5
AE
1316 int opcode;
1317 int flags;
4634246d 1318 u64 snapid;
602adf40 1319
65ccfe21 1320 seg_name = rbd_segment_name(rbd_dev, ofs);
602adf40
YS
1321 if (!seg_name)
1322 return -ENOMEM;
65ccfe21
AE
1323 seg_len = rbd_segment_length(rbd_dev, ofs, len);
1324 seg_ofs = rbd_segment_offset(rbd_dev, ofs);
602adf40 1325
ff2e4bb5
AE
1326 if (rq_data_dir(rq) == WRITE) {
1327 opcode = CEPH_OSD_OP_WRITE;
1328 flags = CEPH_OSD_FLAG_WRITE|CEPH_OSD_FLAG_ONDISK;
4634246d 1329 snapid = CEPH_NOSNAP;
ff2e4bb5
AE
1330 payload_len = seg_len;
1331 } else {
1332 opcode = CEPH_OSD_OP_READ;
1333 flags = CEPH_OSD_FLAG_READ;
4634246d 1334 snapc = NULL;
0d7dbfce 1335 snapid = rbd_dev->spec->snap_id;
ff2e4bb5
AE
1336 payload_len = 0;
1337 }
602adf40 1338
57cfc106
AE
1339 ret = -ENOMEM;
1340 ops = rbd_create_rw_ops(1, opcode, payload_len);
1341 if (!ops)
602adf40
YS
1342 goto done;
1343
1344 /* we've taken care of segment sizes earlier when we
1345 cloned the bios. We should never have a segment
1346 truncated at this point */
aafb230e 1347 rbd_assert(seg_len == len);
602adf40
YS
1348
1349 ret = rbd_do_request(rq, rbd_dev, snapc, snapid,
1350 seg_name, seg_ofs, seg_len,
1351 bio,
1352 NULL, 0,
1353 flags,
1354 ops,
1fec7093 1355 coll, coll_index,
59c2be1e 1356 rbd_req_cb, 0, NULL);
cd323ac0
AE
1357 if (ret < 0)
1358 rbd_coll_end_req_index(rq, coll, coll_index,
1359 (s32)ret, seg_len);
11f77002 1360 rbd_destroy_ops(ops);
602adf40
YS
1361done:
1362 kfree(seg_name);
1363 return ret;
1364}
1365
602adf40
YS
1366/*
1367 * Request sync osd read
1368 */
0ce1a794 1369static int rbd_req_sync_read(struct rbd_device *rbd_dev,
602adf40 1370 u64 snapid,
aded07ea 1371 const char *object_name,
602adf40 1372 u64 ofs, u64 len,
59c2be1e
YS
1373 char *buf,
1374 u64 *ver)
602adf40 1375{
913d2fdc
AE
1376 struct ceph_osd_req_op *ops;
1377 int ret;
1378
1379 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_READ, 0);
1380 if (!ops)
1381 return -ENOMEM;
1382
1383 ret = rbd_req_sync_op(rbd_dev, NULL,
b06e6a6b 1384 snapid,
602adf40 1385 CEPH_OSD_FLAG_READ,
913d2fdc
AE
1386 ops, object_name, ofs, len, buf, NULL, ver);
1387 rbd_destroy_ops(ops);
1388
1389 return ret;
602adf40
YS
1390}
1391
1392/*
59c2be1e
YS
1393 * Request sync osd watch
1394 */
0ce1a794 1395static int rbd_req_sync_notify_ack(struct rbd_device *rbd_dev,
59c2be1e 1396 u64 ver,
7f0a24d8 1397 u64 notify_id)
59c2be1e
YS
1398{
1399 struct ceph_osd_req_op *ops;
11f77002
SW
1400 int ret;
1401
57cfc106
AE
1402 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_NOTIFY_ACK, 0);
1403 if (!ops)
1404 return -ENOMEM;
59c2be1e 1405
a71b891b 1406 ops[0].watch.ver = cpu_to_le64(ver);
59c2be1e
YS
1407 ops[0].watch.cookie = notify_id;
1408 ops[0].watch.flag = 0;
1409
0ce1a794 1410 ret = rbd_do_request(NULL, rbd_dev, NULL, CEPH_NOSNAP,
7f0a24d8 1411 rbd_dev->header_name, 0, 0, NULL,
ad4f232f 1412 NULL, 0,
59c2be1e
YS
1413 CEPH_OSD_FLAG_READ,
1414 ops,
1fec7093 1415 NULL, 0,
59c2be1e
YS
1416 rbd_simple_req_cb, 0, NULL);
1417
1418 rbd_destroy_ops(ops);
1419 return ret;
1420}
1421
1422static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
1423{
0ce1a794 1424 struct rbd_device *rbd_dev = (struct rbd_device *)data;
a71b891b 1425 u64 hver;
13143d2d
SW
1426 int rc;
1427
0ce1a794 1428 if (!rbd_dev)
59c2be1e
YS
1429 return;
1430
bd919d45
AE
1431 dout("rbd_watch_cb %s notify_id=%llu opcode=%u\n",
1432 rbd_dev->header_name, (unsigned long long) notify_id,
1433 (unsigned int) opcode);
117973fb 1434 rc = rbd_dev_refresh(rbd_dev, &hver);
13143d2d 1435 if (rc)
06ecc6cb
AE
1436 rbd_warn(rbd_dev, "got notification but failed to "
1437 " update snaps: %d\n", rc);
59c2be1e 1438
7f0a24d8 1439 rbd_req_sync_notify_ack(rbd_dev, hver, notify_id);
59c2be1e
YS
1440}
1441
1442/*
1443 * Request sync osd watch
1444 */
0e6f322d 1445static int rbd_req_sync_watch(struct rbd_device *rbd_dev)
59c2be1e
YS
1446{
1447 struct ceph_osd_req_op *ops;
0ce1a794 1448 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
57cfc106 1449 int ret;
59c2be1e 1450
57cfc106
AE
1451 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_WATCH, 0);
1452 if (!ops)
1453 return -ENOMEM;
59c2be1e
YS
1454
1455 ret = ceph_osdc_create_event(osdc, rbd_watch_cb, 0,
0ce1a794 1456 (void *)rbd_dev, &rbd_dev->watch_event);
59c2be1e
YS
1457 if (ret < 0)
1458 goto fail;
1459
0e6f322d 1460 ops[0].watch.ver = cpu_to_le64(rbd_dev->header.obj_version);
0ce1a794 1461 ops[0].watch.cookie = cpu_to_le64(rbd_dev->watch_event->cookie);
59c2be1e
YS
1462 ops[0].watch.flag = 1;
1463
0ce1a794 1464 ret = rbd_req_sync_op(rbd_dev, NULL,
59c2be1e 1465 CEPH_NOSNAP,
59c2be1e
YS
1466 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1467 ops,
0e6f322d
AE
1468 rbd_dev->header_name,
1469 0, 0, NULL,
0ce1a794 1470 &rbd_dev->watch_request, NULL);
59c2be1e
YS
1471
1472 if (ret < 0)
1473 goto fail_event;
1474
1475 rbd_destroy_ops(ops);
1476 return 0;
1477
1478fail_event:
0ce1a794
AE
1479 ceph_osdc_cancel_event(rbd_dev->watch_event);
1480 rbd_dev->watch_event = NULL;
59c2be1e
YS
1481fail:
1482 rbd_destroy_ops(ops);
1483 return ret;
1484}
1485
79e3057c
YS
1486/*
1487 * Request sync osd unwatch
1488 */
070c633f 1489static int rbd_req_sync_unwatch(struct rbd_device *rbd_dev)
79e3057c
YS
1490{
1491 struct ceph_osd_req_op *ops;
57cfc106 1492 int ret;
79e3057c 1493
57cfc106
AE
1494 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_WATCH, 0);
1495 if (!ops)
1496 return -ENOMEM;
79e3057c
YS
1497
1498 ops[0].watch.ver = 0;
0ce1a794 1499 ops[0].watch.cookie = cpu_to_le64(rbd_dev->watch_event->cookie);
79e3057c
YS
1500 ops[0].watch.flag = 0;
1501
0ce1a794 1502 ret = rbd_req_sync_op(rbd_dev, NULL,
79e3057c 1503 CEPH_NOSNAP,
79e3057c
YS
1504 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1505 ops,
070c633f
AE
1506 rbd_dev->header_name,
1507 0, 0, NULL, NULL, NULL);
1508
79e3057c
YS
1509
1510 rbd_destroy_ops(ops);
0ce1a794
AE
1511 ceph_osdc_cancel_event(rbd_dev->watch_event);
1512 rbd_dev->watch_event = NULL;
79e3057c
YS
1513 return ret;
1514}
1515
602adf40 1516/*
3cb4a687 1517 * Synchronous osd object method call
602adf40 1518 */
0ce1a794 1519static int rbd_req_sync_exec(struct rbd_device *rbd_dev,
aded07ea
AE
1520 const char *object_name,
1521 const char *class_name,
1522 const char *method_name,
3cb4a687
AE
1523 const char *outbound,
1524 size_t outbound_size,
f8d4de6e
AE
1525 char *inbound,
1526 size_t inbound_size,
3cb4a687 1527 int flags,
59c2be1e 1528 u64 *ver)
602adf40
YS
1529{
1530 struct ceph_osd_req_op *ops;
aded07ea
AE
1531 int class_name_len = strlen(class_name);
1532 int method_name_len = strlen(method_name);
3cb4a687 1533 int payload_size;
57cfc106
AE
1534 int ret;
1535
3cb4a687
AE
1536 /*
1537 * Any input parameters required by the method we're calling
1538 * will be sent along with the class and method names as
1539 * part of the message payload. That data and its size are
1540 * supplied via the indata and indata_len fields (named from
1541 * the perspective of the server side) in the OSD request
1542 * operation.
1543 */
1544 payload_size = class_name_len + method_name_len + outbound_size;
1545 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_CALL, payload_size);
57cfc106
AE
1546 if (!ops)
1547 return -ENOMEM;
602adf40 1548
aded07ea
AE
1549 ops[0].cls.class_name = class_name;
1550 ops[0].cls.class_len = (__u8) class_name_len;
1551 ops[0].cls.method_name = method_name;
1552 ops[0].cls.method_len = (__u8) method_name_len;
602adf40 1553 ops[0].cls.argc = 0;
3cb4a687
AE
1554 ops[0].cls.indata = outbound;
1555 ops[0].cls.indata_len = outbound_size;
602adf40 1556
0ce1a794 1557 ret = rbd_req_sync_op(rbd_dev, NULL,
602adf40 1558 CEPH_NOSNAP,
3cb4a687 1559 flags, ops,
f8d4de6e
AE
1560 object_name, 0, inbound_size, inbound,
1561 NULL, ver);
602adf40
YS
1562
1563 rbd_destroy_ops(ops);
1564
1565 dout("cls_exec returned %d\n", ret);
1566 return ret;
1567}
1568
1fec7093
YS
1569static struct rbd_req_coll *rbd_alloc_coll(int num_reqs)
1570{
1571 struct rbd_req_coll *coll =
1572 kzalloc(sizeof(struct rbd_req_coll) +
1573 sizeof(struct rbd_req_status) * num_reqs,
1574 GFP_ATOMIC);
1575
1576 if (!coll)
1577 return NULL;
1578 coll->total = num_reqs;
1579 kref_init(&coll->kref);
1580 return coll;
1581}
1582
8295cda7
AE
1583static int rbd_dev_do_request(struct request *rq,
1584 struct rbd_device *rbd_dev,
1585 struct ceph_snap_context *snapc,
1586 u64 ofs, unsigned int size,
1587 struct bio *bio_chain)
1588{
1589 int num_segs;
1590 struct rbd_req_coll *coll;
1591 unsigned int bio_offset;
1592 int cur_seg = 0;
1593
1594 dout("%s 0x%x bytes at 0x%llx\n",
1595 rq_data_dir(rq) == WRITE ? "write" : "read",
1596 size, (unsigned long long) blk_rq_pos(rq) * SECTOR_SIZE);
1597
1598 num_segs = rbd_get_num_segments(&rbd_dev->header, ofs, size);
1599 if (num_segs <= 0)
1600 return num_segs;
1601
1602 coll = rbd_alloc_coll(num_segs);
1603 if (!coll)
1604 return -ENOMEM;
1605
1606 bio_offset = 0;
1607 do {
1608 u64 limit = rbd_segment_length(rbd_dev, ofs, size);
1609 unsigned int clone_size;
1610 struct bio *bio_clone;
1611
1612 BUG_ON(limit > (u64)UINT_MAX);
1613 clone_size = (unsigned int)limit;
1614 dout("bio_chain->bi_vcnt=%hu\n", bio_chain->bi_vcnt);
1615
1616 kref_get(&coll->kref);
1617
1618 /* Pass a cloned bio chain via an osd request */
1619
1620 bio_clone = bio_chain_clone_range(&bio_chain,
1621 &bio_offset, clone_size,
1622 GFP_ATOMIC);
1623 if (bio_clone)
1624 (void)rbd_do_op(rq, rbd_dev, snapc,
1625 ofs, clone_size,
1626 bio_clone, coll, cur_seg);
1627 else
1628 rbd_coll_end_req_index(rq, coll, cur_seg,
1629 (s32)-ENOMEM,
1630 clone_size);
1631 size -= clone_size;
1632 ofs += clone_size;
1633
1634 cur_seg++;
1635 } while (size > 0);
1636 kref_put(&coll->kref, rbd_coll_release);
1637
1638 return 0;
1639}
1640
602adf40
YS
1641/*
1642 * block device queue callback
1643 */
1644static void rbd_rq_fn(struct request_queue *q)
1645{
1646 struct rbd_device *rbd_dev = q->queuedata;
1647 struct request *rq;
602adf40 1648
00f1f36f 1649 while ((rq = blk_fetch_request(q))) {
602adf40 1650 struct bio *bio;
602adf40 1651 bool do_write;
bd919d45 1652 unsigned int size;
602adf40 1653 u64 ofs;
d1d25646 1654 struct ceph_snap_context *snapc;
8295cda7 1655 int result;
602adf40 1656
602adf40
YS
1657 dout("fetched request\n");
1658
1659 /* filter out block requests we don't understand */
1660 if ((rq->cmd_type != REQ_TYPE_FS)) {
1661 __blk_end_request_all(rq, 0);
00f1f36f 1662 continue;
602adf40
YS
1663 }
1664
1665 /* deduce our operation (read, write) */
1666 do_write = (rq_data_dir(rq) == WRITE);
f84344f3 1667 if (do_write && rbd_dev->mapping.read_only) {
602adf40 1668 __blk_end_request_all(rq, -EROFS);
00f1f36f 1669 continue;
602adf40
YS
1670 }
1671
1672 spin_unlock_irq(q->queue_lock);
1673
d1d25646 1674 down_read(&rbd_dev->header_rwsem);
e88a36ec 1675
daba5fdb 1676 if (!rbd_dev->exists) {
0d7dbfce 1677 rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP);
e88a36ec 1678 up_read(&rbd_dev->header_rwsem);
d1d25646
JD
1679 dout("request for non-existent snapshot");
1680 spin_lock_irq(q->queue_lock);
1681 __blk_end_request_all(rq, -ENXIO);
1682 continue;
e88a36ec
JD
1683 }
1684
d1d25646
JD
1685 snapc = ceph_get_snap_context(rbd_dev->header.snapc);
1686
1687 up_read(&rbd_dev->header_rwsem);
1688
f7760dad
AE
1689 size = blk_rq_bytes(rq);
1690 ofs = blk_rq_pos(rq) * SECTOR_SIZE;
1691 bio = rq->bio;
1692
8295cda7 1693 result = rbd_dev_do_request(rq, rbd_dev, snapc, ofs, size, bio);
d1d25646 1694 ceph_put_snap_context(snapc);
8295cda7
AE
1695 spin_lock_irq(q->queue_lock);
1696 if (!size || result < 0)
1697 __blk_end_request_all(rq, result);
602adf40
YS
1698 }
1699}
1700
1701/*
1702 * a queue callback. Makes sure that we don't create a bio that spans across
1703 * multiple osd objects. One exception would be with a single page bios,
f7760dad 1704 * which we handle later at bio_chain_clone_range()
602adf40
YS
1705 */
1706static int rbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd,
1707 struct bio_vec *bvec)
1708{
1709 struct rbd_device *rbd_dev = q->queuedata;
e5cfeed2
AE
1710 sector_t sector_offset;
1711 sector_t sectors_per_obj;
1712 sector_t obj_sector_offset;
1713 int ret;
1714
1715 /*
1716 * Find how far into its rbd object the partition-relative
1717 * bio start sector is to offset relative to the enclosing
1718 * device.
1719 */
1720 sector_offset = get_start_sect(bmd->bi_bdev) + bmd->bi_sector;
1721 sectors_per_obj = 1 << (rbd_dev->header.obj_order - SECTOR_SHIFT);
1722 obj_sector_offset = sector_offset & (sectors_per_obj - 1);
1723
1724 /*
1725 * Compute the number of bytes from that offset to the end
1726 * of the object. Account for what's already used by the bio.
1727 */
1728 ret = (int) (sectors_per_obj - obj_sector_offset) << SECTOR_SHIFT;
1729 if (ret > bmd->bi_size)
1730 ret -= bmd->bi_size;
1731 else
1732 ret = 0;
1733
1734 /*
1735 * Don't send back more than was asked for. And if the bio
1736 * was empty, let the whole thing through because: "Note
1737 * that a block device *must* allow a single page to be
1738 * added to an empty bio."
1739 */
1740 rbd_assert(bvec->bv_len <= PAGE_SIZE);
1741 if (ret > (int) bvec->bv_len || !bmd->bi_size)
1742 ret = (int) bvec->bv_len;
1743
1744 return ret;
602adf40
YS
1745}
1746
1747static void rbd_free_disk(struct rbd_device *rbd_dev)
1748{
1749 struct gendisk *disk = rbd_dev->disk;
1750
1751 if (!disk)
1752 return;
1753
602adf40
YS
1754 if (disk->flags & GENHD_FL_UP)
1755 del_gendisk(disk);
1756 if (disk->queue)
1757 blk_cleanup_queue(disk->queue);
1758 put_disk(disk);
1759}
1760
1761/*
4156d998
AE
1762 * Read the complete header for the given rbd device.
1763 *
1764 * Returns a pointer to a dynamically-allocated buffer containing
1765 * the complete and validated header. Caller can pass the address
1766 * of a variable that will be filled in with the version of the
1767 * header object at the time it was read.
1768 *
1769 * Returns a pointer-coded errno if a failure occurs.
602adf40 1770 */
4156d998
AE
1771static struct rbd_image_header_ondisk *
1772rbd_dev_v1_header_read(struct rbd_device *rbd_dev, u64 *version)
602adf40 1773{
4156d998 1774 struct rbd_image_header_ondisk *ondisk = NULL;
50f7c4c9 1775 u32 snap_count = 0;
4156d998
AE
1776 u64 names_size = 0;
1777 u32 want_count;
1778 int ret;
602adf40 1779
00f1f36f 1780 /*
4156d998
AE
1781 * The complete header will include an array of its 64-bit
1782 * snapshot ids, followed by the names of those snapshots as
1783 * a contiguous block of NUL-terminated strings. Note that
1784 * the number of snapshots could change by the time we read
1785 * it in, in which case we re-read it.
00f1f36f 1786 */
4156d998
AE
1787 do {
1788 size_t size;
1789
1790 kfree(ondisk);
1791
1792 size = sizeof (*ondisk);
1793 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
1794 size += names_size;
1795 ondisk = kmalloc(size, GFP_KERNEL);
1796 if (!ondisk)
1797 return ERR_PTR(-ENOMEM);
1798
1799 ret = rbd_req_sync_read(rbd_dev, CEPH_NOSNAP,
0bed54dc 1800 rbd_dev->header_name,
4156d998
AE
1801 0, size,
1802 (char *) ondisk, version);
1803
1804 if (ret < 0)
1805 goto out_err;
1806 if (WARN_ON((size_t) ret < size)) {
1807 ret = -ENXIO;
06ecc6cb
AE
1808 rbd_warn(rbd_dev, "short header read (want %zd got %d)",
1809 size, ret);
4156d998
AE
1810 goto out_err;
1811 }
1812 if (!rbd_dev_ondisk_valid(ondisk)) {
1813 ret = -ENXIO;
06ecc6cb 1814 rbd_warn(rbd_dev, "invalid header");
4156d998 1815 goto out_err;
81e759fb 1816 }
602adf40 1817
4156d998
AE
1818 names_size = le64_to_cpu(ondisk->snap_names_len);
1819 want_count = snap_count;
1820 snap_count = le32_to_cpu(ondisk->snap_count);
1821 } while (snap_count != want_count);
00f1f36f 1822
4156d998 1823 return ondisk;
00f1f36f 1824
4156d998
AE
1825out_err:
1826 kfree(ondisk);
1827
1828 return ERR_PTR(ret);
1829}
1830
1831/*
1832 * reload the ondisk the header
1833 */
1834static int rbd_read_header(struct rbd_device *rbd_dev,
1835 struct rbd_image_header *header)
1836{
1837 struct rbd_image_header_ondisk *ondisk;
1838 u64 ver = 0;
1839 int ret;
602adf40 1840
4156d998
AE
1841 ondisk = rbd_dev_v1_header_read(rbd_dev, &ver);
1842 if (IS_ERR(ondisk))
1843 return PTR_ERR(ondisk);
1844 ret = rbd_header_from_disk(header, ondisk);
1845 if (ret >= 0)
1846 header->obj_version = ver;
1847 kfree(ondisk);
1848
1849 return ret;
602adf40
YS
1850}
1851
41f38c2b 1852static void rbd_remove_all_snaps(struct rbd_device *rbd_dev)
dfc5606d
YS
1853{
1854 struct rbd_snap *snap;
a0593290 1855 struct rbd_snap *next;
dfc5606d 1856
a0593290 1857 list_for_each_entry_safe(snap, next, &rbd_dev->snaps, node)
41f38c2b 1858 rbd_remove_snap_dev(snap);
dfc5606d
YS
1859}
1860
9478554a
AE
1861static void rbd_update_mapping_size(struct rbd_device *rbd_dev)
1862{
1863 sector_t size;
1864
0d7dbfce 1865 if (rbd_dev->spec->snap_id != CEPH_NOSNAP)
9478554a
AE
1866 return;
1867
1868 size = (sector_t) rbd_dev->header.image_size / SECTOR_SIZE;
1869 dout("setting size to %llu sectors", (unsigned long long) size);
1870 rbd_dev->mapping.size = (u64) size;
1871 set_capacity(rbd_dev->disk, size);
1872}
1873
602adf40
YS
1874/*
1875 * only read the first part of the ondisk header, without the snaps info
1876 */
117973fb 1877static int rbd_dev_v1_refresh(struct rbd_device *rbd_dev, u64 *hver)
602adf40
YS
1878{
1879 int ret;
1880 struct rbd_image_header h;
602adf40
YS
1881
1882 ret = rbd_read_header(rbd_dev, &h);
1883 if (ret < 0)
1884 return ret;
1885
a51aa0c0
JD
1886 down_write(&rbd_dev->header_rwsem);
1887
9478554a
AE
1888 /* Update image size, and check for resize of mapped image */
1889 rbd_dev->header.image_size = h.image_size;
1890 rbd_update_mapping_size(rbd_dev);
9db4b3e3 1891
849b4260 1892 /* rbd_dev->header.object_prefix shouldn't change */
602adf40 1893 kfree(rbd_dev->header.snap_sizes);
849b4260 1894 kfree(rbd_dev->header.snap_names);
d1d25646
JD
1895 /* osd requests may still refer to snapc */
1896 ceph_put_snap_context(rbd_dev->header.snapc);
602adf40 1897
b813623a
AE
1898 if (hver)
1899 *hver = h.obj_version;
a71b891b 1900 rbd_dev->header.obj_version = h.obj_version;
93a24e08 1901 rbd_dev->header.image_size = h.image_size;
602adf40
YS
1902 rbd_dev->header.snapc = h.snapc;
1903 rbd_dev->header.snap_names = h.snap_names;
1904 rbd_dev->header.snap_sizes = h.snap_sizes;
849b4260
AE
1905 /* Free the extra copy of the object prefix */
1906 WARN_ON(strcmp(rbd_dev->header.object_prefix, h.object_prefix));
1907 kfree(h.object_prefix);
1908
304f6808
AE
1909 ret = rbd_dev_snaps_update(rbd_dev);
1910 if (!ret)
1911 ret = rbd_dev_snaps_register(rbd_dev);
dfc5606d 1912
c666601a 1913 up_write(&rbd_dev->header_rwsem);
602adf40 1914
dfc5606d 1915 return ret;
602adf40
YS
1916}
1917
117973fb 1918static int rbd_dev_refresh(struct rbd_device *rbd_dev, u64 *hver)
1fe5e993
AE
1919{
1920 int ret;
1921
117973fb 1922 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1fe5e993 1923 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
117973fb
AE
1924 if (rbd_dev->image_format == 1)
1925 ret = rbd_dev_v1_refresh(rbd_dev, hver);
1926 else
1927 ret = rbd_dev_v2_refresh(rbd_dev, hver);
1fe5e993
AE
1928 mutex_unlock(&ctl_mutex);
1929
1930 return ret;
1931}
1932
602adf40
YS
1933static int rbd_init_disk(struct rbd_device *rbd_dev)
1934{
1935 struct gendisk *disk;
1936 struct request_queue *q;
593a9e7b 1937 u64 segment_size;
602adf40 1938
602adf40 1939 /* create gendisk info */
602adf40
YS
1940 disk = alloc_disk(RBD_MINORS_PER_MAJOR);
1941 if (!disk)
1fcdb8aa 1942 return -ENOMEM;
602adf40 1943
f0f8cef5 1944 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
de71a297 1945 rbd_dev->dev_id);
602adf40
YS
1946 disk->major = rbd_dev->major;
1947 disk->first_minor = 0;
1948 disk->fops = &rbd_bd_ops;
1949 disk->private_data = rbd_dev;
1950
1951 /* init rq */
602adf40
YS
1952 q = blk_init_queue(rbd_rq_fn, &rbd_dev->lock);
1953 if (!q)
1954 goto out_disk;
029bcbd8 1955
593a9e7b
AE
1956 /* We use the default size, but let's be explicit about it. */
1957 blk_queue_physical_block_size(q, SECTOR_SIZE);
1958
029bcbd8 1959 /* set io sizes to object size */
593a9e7b
AE
1960 segment_size = rbd_obj_bytes(&rbd_dev->header);
1961 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
1962 blk_queue_max_segment_size(q, segment_size);
1963 blk_queue_io_min(q, segment_size);
1964 blk_queue_io_opt(q, segment_size);
029bcbd8 1965
602adf40
YS
1966 blk_queue_merge_bvec(q, rbd_merge_bvec);
1967 disk->queue = q;
1968
1969 q->queuedata = rbd_dev;
1970
1971 rbd_dev->disk = disk;
602adf40 1972
12f02944
AE
1973 set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
1974
602adf40 1975 return 0;
602adf40
YS
1976out_disk:
1977 put_disk(disk);
1fcdb8aa
AE
1978
1979 return -ENOMEM;
602adf40
YS
1980}
1981
dfc5606d
YS
1982/*
1983 sysfs
1984*/
1985
593a9e7b
AE
1986static struct rbd_device *dev_to_rbd_dev(struct device *dev)
1987{
1988 return container_of(dev, struct rbd_device, dev);
1989}
1990
dfc5606d
YS
1991static ssize_t rbd_size_show(struct device *dev,
1992 struct device_attribute *attr, char *buf)
1993{
593a9e7b 1994 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
a51aa0c0
JD
1995 sector_t size;
1996
1997 down_read(&rbd_dev->header_rwsem);
1998 size = get_capacity(rbd_dev->disk);
1999 up_read(&rbd_dev->header_rwsem);
dfc5606d 2000
a51aa0c0 2001 return sprintf(buf, "%llu\n", (unsigned long long) size * SECTOR_SIZE);
dfc5606d
YS
2002}
2003
34b13184
AE
2004/*
2005 * Note this shows the features for whatever's mapped, which is not
2006 * necessarily the base image.
2007 */
2008static ssize_t rbd_features_show(struct device *dev,
2009 struct device_attribute *attr, char *buf)
2010{
2011 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
2012
2013 return sprintf(buf, "0x%016llx\n",
2014 (unsigned long long) rbd_dev->mapping.features);
2015}
2016
dfc5606d
YS
2017static ssize_t rbd_major_show(struct device *dev,
2018 struct device_attribute *attr, char *buf)
2019{
593a9e7b 2020 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
602adf40 2021
dfc5606d
YS
2022 return sprintf(buf, "%d\n", rbd_dev->major);
2023}
2024
2025static ssize_t rbd_client_id_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
1dbb4399
AE
2030 return sprintf(buf, "client%lld\n",
2031 ceph_client_id(rbd_dev->rbd_client->client));
602adf40
YS
2032}
2033
dfc5606d
YS
2034static ssize_t rbd_pool_show(struct device *dev,
2035 struct device_attribute *attr, char *buf)
602adf40 2036{
593a9e7b 2037 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
dfc5606d 2038
0d7dbfce 2039 return sprintf(buf, "%s\n", rbd_dev->spec->pool_name);
dfc5606d
YS
2040}
2041
9bb2f334
AE
2042static ssize_t rbd_pool_id_show(struct device *dev,
2043 struct device_attribute *attr, char *buf)
2044{
2045 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
2046
0d7dbfce
AE
2047 return sprintf(buf, "%llu\n",
2048 (unsigned long long) rbd_dev->spec->pool_id);
9bb2f334
AE
2049}
2050
dfc5606d
YS
2051static ssize_t rbd_name_show(struct device *dev,
2052 struct device_attribute *attr, char *buf)
2053{
593a9e7b 2054 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
dfc5606d 2055
a92ffdf8
AE
2056 if (rbd_dev->spec->image_name)
2057 return sprintf(buf, "%s\n", rbd_dev->spec->image_name);
2058
2059 return sprintf(buf, "(unknown)\n");
dfc5606d
YS
2060}
2061
589d30e0
AE
2062static ssize_t rbd_image_id_show(struct device *dev,
2063 struct device_attribute *attr, char *buf)
2064{
2065 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
2066
0d7dbfce 2067 return sprintf(buf, "%s\n", rbd_dev->spec->image_id);
589d30e0
AE
2068}
2069
34b13184
AE
2070/*
2071 * Shows the name of the currently-mapped snapshot (or
2072 * RBD_SNAP_HEAD_NAME for the base image).
2073 */
dfc5606d
YS
2074static ssize_t rbd_snap_show(struct device *dev,
2075 struct device_attribute *attr,
2076 char *buf)
2077{
593a9e7b 2078 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
dfc5606d 2079
0d7dbfce 2080 return sprintf(buf, "%s\n", rbd_dev->spec->snap_name);
dfc5606d
YS
2081}
2082
86b00e0d
AE
2083/*
2084 * For an rbd v2 image, shows the pool id, image id, and snapshot id
2085 * for the parent image. If there is no parent, simply shows
2086 * "(no parent image)".
2087 */
2088static ssize_t rbd_parent_show(struct device *dev,
2089 struct device_attribute *attr,
2090 char *buf)
2091{
2092 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
2093 struct rbd_spec *spec = rbd_dev->parent_spec;
2094 int count;
2095 char *bufp = buf;
2096
2097 if (!spec)
2098 return sprintf(buf, "(no parent image)\n");
2099
2100 count = sprintf(bufp, "pool_id %llu\npool_name %s\n",
2101 (unsigned long long) spec->pool_id, spec->pool_name);
2102 if (count < 0)
2103 return count;
2104 bufp += count;
2105
2106 count = sprintf(bufp, "image_id %s\nimage_name %s\n", spec->image_id,
2107 spec->image_name ? spec->image_name : "(unknown)");
2108 if (count < 0)
2109 return count;
2110 bufp += count;
2111
2112 count = sprintf(bufp, "snap_id %llu\nsnap_name %s\n",
2113 (unsigned long long) spec->snap_id, spec->snap_name);
2114 if (count < 0)
2115 return count;
2116 bufp += count;
2117
2118 count = sprintf(bufp, "overlap %llu\n", rbd_dev->parent_overlap);
2119 if (count < 0)
2120 return count;
2121 bufp += count;
2122
2123 return (ssize_t) (bufp - buf);
2124}
2125
dfc5606d
YS
2126static ssize_t rbd_image_refresh(struct device *dev,
2127 struct device_attribute *attr,
2128 const char *buf,
2129 size_t size)
2130{
593a9e7b 2131 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
b813623a 2132 int ret;
602adf40 2133
117973fb 2134 ret = rbd_dev_refresh(rbd_dev, NULL);
b813623a
AE
2135
2136 return ret < 0 ? ret : size;
dfc5606d 2137}
602adf40 2138
dfc5606d 2139static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
34b13184 2140static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL);
dfc5606d
YS
2141static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
2142static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
2143static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
9bb2f334 2144static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
dfc5606d 2145static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
589d30e0 2146static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL);
dfc5606d
YS
2147static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
2148static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
86b00e0d 2149static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL);
dfc5606d
YS
2150
2151static struct attribute *rbd_attrs[] = {
2152 &dev_attr_size.attr,
34b13184 2153 &dev_attr_features.attr,
dfc5606d
YS
2154 &dev_attr_major.attr,
2155 &dev_attr_client_id.attr,
2156 &dev_attr_pool.attr,
9bb2f334 2157 &dev_attr_pool_id.attr,
dfc5606d 2158 &dev_attr_name.attr,
589d30e0 2159 &dev_attr_image_id.attr,
dfc5606d 2160 &dev_attr_current_snap.attr,
86b00e0d 2161 &dev_attr_parent.attr,
dfc5606d 2162 &dev_attr_refresh.attr,
dfc5606d
YS
2163 NULL
2164};
2165
2166static struct attribute_group rbd_attr_group = {
2167 .attrs = rbd_attrs,
2168};
2169
2170static const struct attribute_group *rbd_attr_groups[] = {
2171 &rbd_attr_group,
2172 NULL
2173};
2174
2175static void rbd_sysfs_dev_release(struct device *dev)
2176{
2177}
2178
2179static struct device_type rbd_device_type = {
2180 .name = "rbd",
2181 .groups = rbd_attr_groups,
2182 .release = rbd_sysfs_dev_release,
2183};
2184
2185
2186/*
2187 sysfs - snapshots
2188*/
2189
2190static ssize_t rbd_snap_size_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->size);
dfc5606d
YS
2197}
2198
2199static ssize_t rbd_snap_id_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
3591538f 2205 return sprintf(buf, "%llu\n", (unsigned long long)snap->id);
dfc5606d
YS
2206}
2207
34b13184
AE
2208static ssize_t rbd_snap_features_show(struct device *dev,
2209 struct device_attribute *attr,
2210 char *buf)
2211{
2212 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2213
2214 return sprintf(buf, "0x%016llx\n",
2215 (unsigned long long) snap->features);
2216}
2217
dfc5606d
YS
2218static DEVICE_ATTR(snap_size, S_IRUGO, rbd_snap_size_show, NULL);
2219static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL);
34b13184 2220static DEVICE_ATTR(snap_features, S_IRUGO, rbd_snap_features_show, NULL);
dfc5606d
YS
2221
2222static struct attribute *rbd_snap_attrs[] = {
2223 &dev_attr_snap_size.attr,
2224 &dev_attr_snap_id.attr,
34b13184 2225 &dev_attr_snap_features.attr,
dfc5606d
YS
2226 NULL,
2227};
2228
2229static struct attribute_group rbd_snap_attr_group = {
2230 .attrs = rbd_snap_attrs,
2231};
2232
2233static void rbd_snap_dev_release(struct device *dev)
2234{
2235 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2236 kfree(snap->name);
2237 kfree(snap);
2238}
2239
2240static const struct attribute_group *rbd_snap_attr_groups[] = {
2241 &rbd_snap_attr_group,
2242 NULL
2243};
2244
2245static struct device_type rbd_snap_device_type = {
2246 .groups = rbd_snap_attr_groups,
2247 .release = rbd_snap_dev_release,
2248};
2249
8b8fb99c
AE
2250static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)
2251{
2252 kref_get(&spec->kref);
2253
2254 return spec;
2255}
2256
2257static void rbd_spec_free(struct kref *kref);
2258static void rbd_spec_put(struct rbd_spec *spec)
2259{
2260 if (spec)
2261 kref_put(&spec->kref, rbd_spec_free);
2262}
2263
2264static struct rbd_spec *rbd_spec_alloc(void)
2265{
2266 struct rbd_spec *spec;
2267
2268 spec = kzalloc(sizeof (*spec), GFP_KERNEL);
2269 if (!spec)
2270 return NULL;
2271 kref_init(&spec->kref);
2272
2273 rbd_spec_put(rbd_spec_get(spec)); /* TEMPORARY */
2274
2275 return spec;
2276}
2277
2278static void rbd_spec_free(struct kref *kref)
2279{
2280 struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref);
2281
2282 kfree(spec->pool_name);
2283 kfree(spec->image_id);
2284 kfree(spec->image_name);
2285 kfree(spec->snap_name);
2286 kfree(spec);
2287}
2288
c53d5893
AE
2289struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,
2290 struct rbd_spec *spec)
2291{
2292 struct rbd_device *rbd_dev;
2293
2294 rbd_dev = kzalloc(sizeof (*rbd_dev), GFP_KERNEL);
2295 if (!rbd_dev)
2296 return NULL;
2297
2298 spin_lock_init(&rbd_dev->lock);
2299 INIT_LIST_HEAD(&rbd_dev->node);
2300 INIT_LIST_HEAD(&rbd_dev->snaps);
2301 init_rwsem(&rbd_dev->header_rwsem);
2302
2303 rbd_dev->spec = spec;
2304 rbd_dev->rbd_client = rbdc;
2305
2306 return rbd_dev;
2307}
2308
2309static void rbd_dev_destroy(struct rbd_device *rbd_dev)
2310{
86b00e0d 2311 rbd_spec_put(rbd_dev->parent_spec);
c53d5893
AE
2312 kfree(rbd_dev->header_name);
2313 rbd_put_client(rbd_dev->rbd_client);
2314 rbd_spec_put(rbd_dev->spec);
2315 kfree(rbd_dev);
2316}
2317
304f6808
AE
2318static bool rbd_snap_registered(struct rbd_snap *snap)
2319{
2320 bool ret = snap->dev.type == &rbd_snap_device_type;
2321 bool reg = device_is_registered(&snap->dev);
2322
2323 rbd_assert(!ret ^ reg);
2324
2325 return ret;
2326}
2327
41f38c2b 2328static void rbd_remove_snap_dev(struct rbd_snap *snap)
dfc5606d
YS
2329{
2330 list_del(&snap->node);
304f6808
AE
2331 if (device_is_registered(&snap->dev))
2332 device_unregister(&snap->dev);
dfc5606d
YS
2333}
2334
14e7085d 2335static int rbd_register_snap_dev(struct rbd_snap *snap,
dfc5606d
YS
2336 struct device *parent)
2337{
2338 struct device *dev = &snap->dev;
2339 int ret;
2340
2341 dev->type = &rbd_snap_device_type;
2342 dev->parent = parent;
2343 dev->release = rbd_snap_dev_release;
d4b125e9 2344 dev_set_name(dev, "%s%s", RBD_SNAP_DEV_NAME_PREFIX, snap->name);
304f6808
AE
2345 dout("%s: registering device for snapshot %s\n", __func__, snap->name);
2346
dfc5606d
YS
2347 ret = device_register(dev);
2348
2349 return ret;
2350}
2351
4e891e0a 2352static struct rbd_snap *__rbd_add_snap_dev(struct rbd_device *rbd_dev,
c8d18425 2353 const char *snap_name,
34b13184
AE
2354 u64 snap_id, u64 snap_size,
2355 u64 snap_features)
dfc5606d 2356{
4e891e0a 2357 struct rbd_snap *snap;
dfc5606d 2358 int ret;
4e891e0a
AE
2359
2360 snap = kzalloc(sizeof (*snap), GFP_KERNEL);
dfc5606d 2361 if (!snap)
4e891e0a
AE
2362 return ERR_PTR(-ENOMEM);
2363
2364 ret = -ENOMEM;
c8d18425 2365 snap->name = kstrdup(snap_name, GFP_KERNEL);
4e891e0a
AE
2366 if (!snap->name)
2367 goto err;
2368
c8d18425
AE
2369 snap->id = snap_id;
2370 snap->size = snap_size;
34b13184 2371 snap->features = snap_features;
4e891e0a
AE
2372
2373 return snap;
2374
dfc5606d
YS
2375err:
2376 kfree(snap->name);
2377 kfree(snap);
4e891e0a
AE
2378
2379 return ERR_PTR(ret);
dfc5606d
YS
2380}
2381
cd892126
AE
2382static char *rbd_dev_v1_snap_info(struct rbd_device *rbd_dev, u32 which,
2383 u64 *snap_size, u64 *snap_features)
2384{
2385 char *snap_name;
2386
2387 rbd_assert(which < rbd_dev->header.snapc->num_snaps);
2388
2389 *snap_size = rbd_dev->header.snap_sizes[which];
2390 *snap_features = 0; /* No features for v1 */
2391
2392 /* Skip over names until we find the one we are looking for */
2393
2394 snap_name = rbd_dev->header.snap_names;
2395 while (which--)
2396 snap_name += strlen(snap_name) + 1;
2397
2398 return snap_name;
2399}
2400
9d475de5
AE
2401/*
2402 * Get the size and object order for an image snapshot, or if
2403 * snap_id is CEPH_NOSNAP, gets this information for the base
2404 * image.
2405 */
2406static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
2407 u8 *order, u64 *snap_size)
2408{
2409 __le64 snapid = cpu_to_le64(snap_id);
2410 int ret;
2411 struct {
2412 u8 order;
2413 __le64 size;
2414 } __attribute__ ((packed)) size_buf = { 0 };
2415
2416 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2417 "rbd", "get_size",
2418 (char *) &snapid, sizeof (snapid),
2419 (char *) &size_buf, sizeof (size_buf),
2420 CEPH_OSD_FLAG_READ, NULL);
2421 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2422 if (ret < 0)
2423 return ret;
2424
2425 *order = size_buf.order;
2426 *snap_size = le64_to_cpu(size_buf.size);
2427
2428 dout(" snap_id 0x%016llx order = %u, snap_size = %llu\n",
2429 (unsigned long long) snap_id, (unsigned int) *order,
2430 (unsigned long long) *snap_size);
2431
2432 return 0;
2433}
2434
2435static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
2436{
2437 return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
2438 &rbd_dev->header.obj_order,
2439 &rbd_dev->header.image_size);
2440}
2441
1e130199
AE
2442static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
2443{
2444 void *reply_buf;
2445 int ret;
2446 void *p;
2447
2448 reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL);
2449 if (!reply_buf)
2450 return -ENOMEM;
2451
2452 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2453 "rbd", "get_object_prefix",
2454 NULL, 0,
2455 reply_buf, RBD_OBJ_PREFIX_LEN_MAX,
2456 CEPH_OSD_FLAG_READ, NULL);
2457 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2458 if (ret < 0)
2459 goto out;
a0ea3a40 2460 ret = 0; /* rbd_req_sync_exec() can return positive */
1e130199
AE
2461
2462 p = reply_buf;
2463 rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p,
2464 p + RBD_OBJ_PREFIX_LEN_MAX,
2465 NULL, GFP_NOIO);
2466
2467 if (IS_ERR(rbd_dev->header.object_prefix)) {
2468 ret = PTR_ERR(rbd_dev->header.object_prefix);
2469 rbd_dev->header.object_prefix = NULL;
2470 } else {
2471 dout(" object_prefix = %s\n", rbd_dev->header.object_prefix);
2472 }
2473
2474out:
2475 kfree(reply_buf);
2476
2477 return ret;
2478}
2479
b1b5402a
AE
2480static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
2481 u64 *snap_features)
2482{
2483 __le64 snapid = cpu_to_le64(snap_id);
2484 struct {
2485 __le64 features;
2486 __le64 incompat;
2487 } features_buf = { 0 };
d889140c 2488 u64 incompat;
b1b5402a
AE
2489 int ret;
2490
2491 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2492 "rbd", "get_features",
2493 (char *) &snapid, sizeof (snapid),
2494 (char *) &features_buf, sizeof (features_buf),
2495 CEPH_OSD_FLAG_READ, NULL);
2496 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2497 if (ret < 0)
2498 return ret;
d889140c
AE
2499
2500 incompat = le64_to_cpu(features_buf.incompat);
2501 if (incompat & ~RBD_FEATURES_ALL)
b8f5c6ed 2502 return -ENXIO;
d889140c 2503
b1b5402a
AE
2504 *snap_features = le64_to_cpu(features_buf.features);
2505
2506 dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
2507 (unsigned long long) snap_id,
2508 (unsigned long long) *snap_features,
2509 (unsigned long long) le64_to_cpu(features_buf.incompat));
2510
2511 return 0;
2512}
2513
2514static int rbd_dev_v2_features(struct rbd_device *rbd_dev)
2515{
2516 return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP,
2517 &rbd_dev->header.features);
2518}
2519
86b00e0d
AE
2520static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev)
2521{
2522 struct rbd_spec *parent_spec;
2523 size_t size;
2524 void *reply_buf = NULL;
2525 __le64 snapid;
2526 void *p;
2527 void *end;
2528 char *image_id;
2529 u64 overlap;
86b00e0d
AE
2530 int ret;
2531
2532 parent_spec = rbd_spec_alloc();
2533 if (!parent_spec)
2534 return -ENOMEM;
2535
2536 size = sizeof (__le64) + /* pool_id */
2537 sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */
2538 sizeof (__le64) + /* snap_id */
2539 sizeof (__le64); /* overlap */
2540 reply_buf = kmalloc(size, GFP_KERNEL);
2541 if (!reply_buf) {
2542 ret = -ENOMEM;
2543 goto out_err;
2544 }
2545
2546 snapid = cpu_to_le64(CEPH_NOSNAP);
2547 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2548 "rbd", "get_parent",
2549 (char *) &snapid, sizeof (snapid),
2550 (char *) reply_buf, size,
2551 CEPH_OSD_FLAG_READ, NULL);
2552 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2553 if (ret < 0)
2554 goto out_err;
2555
2556 ret = -ERANGE;
2557 p = reply_buf;
2558 end = (char *) reply_buf + size;
2559 ceph_decode_64_safe(&p, end, parent_spec->pool_id, out_err);
2560 if (parent_spec->pool_id == CEPH_NOPOOL)
2561 goto out; /* No parent? No problem. */
2562
979ed480 2563 image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
86b00e0d
AE
2564 if (IS_ERR(image_id)) {
2565 ret = PTR_ERR(image_id);
2566 goto out_err;
2567 }
2568 parent_spec->image_id = image_id;
2569 ceph_decode_64_safe(&p, end, parent_spec->snap_id, out_err);
2570 ceph_decode_64_safe(&p, end, overlap, out_err);
2571
2572 rbd_dev->parent_overlap = overlap;
2573 rbd_dev->parent_spec = parent_spec;
2574 parent_spec = NULL; /* rbd_dev now owns this */
2575out:
2576 ret = 0;
2577out_err:
2578 kfree(reply_buf);
2579 rbd_spec_put(parent_spec);
2580
2581 return ret;
2582}
2583
9e15b77d
AE
2584static char *rbd_dev_image_name(struct rbd_device *rbd_dev)
2585{
2586 size_t image_id_size;
2587 char *image_id;
2588 void *p;
2589 void *end;
2590 size_t size;
2591 void *reply_buf = NULL;
2592 size_t len = 0;
2593 char *image_name = NULL;
2594 int ret;
2595
2596 rbd_assert(!rbd_dev->spec->image_name);
2597
69e7a02f
AE
2598 len = strlen(rbd_dev->spec->image_id);
2599 image_id_size = sizeof (__le32) + len;
9e15b77d
AE
2600 image_id = kmalloc(image_id_size, GFP_KERNEL);
2601 if (!image_id)
2602 return NULL;
2603
2604 p = image_id;
2605 end = (char *) image_id + image_id_size;
69e7a02f 2606 ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32) len);
9e15b77d
AE
2607
2608 size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX;
2609 reply_buf = kmalloc(size, GFP_KERNEL);
2610 if (!reply_buf)
2611 goto out;
2612
2613 ret = rbd_req_sync_exec(rbd_dev, RBD_DIRECTORY,
2614 "rbd", "dir_get_name",
2615 image_id, image_id_size,
2616 (char *) reply_buf, size,
2617 CEPH_OSD_FLAG_READ, NULL);
2618 if (ret < 0)
2619 goto out;
2620 p = reply_buf;
2621 end = (char *) reply_buf + size;
2622 image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL);
2623 if (IS_ERR(image_name))
2624 image_name = NULL;
2625 else
2626 dout("%s: name is %s len is %zd\n", __func__, image_name, len);
2627out:
2628 kfree(reply_buf);
2629 kfree(image_id);
2630
2631 return image_name;
2632}
2633
2634/*
2635 * When a parent image gets probed, we only have the pool, image,
2636 * and snapshot ids but not the names of any of them. This call
2637 * is made later to fill in those names. It has to be done after
2638 * rbd_dev_snaps_update() has completed because some of the
2639 * information (in particular, snapshot name) is not available
2640 * until then.
2641 */
2642static int rbd_dev_probe_update_spec(struct rbd_device *rbd_dev)
2643{
2644 struct ceph_osd_client *osdc;
2645 const char *name;
2646 void *reply_buf = NULL;
2647 int ret;
2648
2649 if (rbd_dev->spec->pool_name)
2650 return 0; /* Already have the names */
2651
2652 /* Look up the pool name */
2653
2654 osdc = &rbd_dev->rbd_client->client->osdc;
2655 name = ceph_pg_pool_name_by_id(osdc->osdmap, rbd_dev->spec->pool_id);
935dc89f
AE
2656 if (!name) {
2657 rbd_warn(rbd_dev, "there is no pool with id %llu",
2658 rbd_dev->spec->pool_id); /* Really a BUG() */
2659 return -EIO;
2660 }
9e15b77d
AE
2661
2662 rbd_dev->spec->pool_name = kstrdup(name, GFP_KERNEL);
2663 if (!rbd_dev->spec->pool_name)
2664 return -ENOMEM;
2665
2666 /* Fetch the image name; tolerate failure here */
2667
2668 name = rbd_dev_image_name(rbd_dev);
69e7a02f 2669 if (name)
9e15b77d 2670 rbd_dev->spec->image_name = (char *) name;
69e7a02f 2671 else
06ecc6cb 2672 rbd_warn(rbd_dev, "unable to get image name");
9e15b77d
AE
2673
2674 /* Look up the snapshot name. */
2675
2676 name = rbd_snap_name(rbd_dev, rbd_dev->spec->snap_id);
2677 if (!name) {
935dc89f
AE
2678 rbd_warn(rbd_dev, "no snapshot with id %llu",
2679 rbd_dev->spec->snap_id); /* Really a BUG() */
9e15b77d
AE
2680 ret = -EIO;
2681 goto out_err;
2682 }
2683 rbd_dev->spec->snap_name = kstrdup(name, GFP_KERNEL);
2684 if(!rbd_dev->spec->snap_name)
2685 goto out_err;
2686
2687 return 0;
2688out_err:
2689 kfree(reply_buf);
2690 kfree(rbd_dev->spec->pool_name);
2691 rbd_dev->spec->pool_name = NULL;
2692
2693 return ret;
2694}
2695
6e14b1a6 2696static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev, u64 *ver)
35d489f9
AE
2697{
2698 size_t size;
2699 int ret;
2700 void *reply_buf;
2701 void *p;
2702 void *end;
2703 u64 seq;
2704 u32 snap_count;
2705 struct ceph_snap_context *snapc;
2706 u32 i;
2707
2708 /*
2709 * We'll need room for the seq value (maximum snapshot id),
2710 * snapshot count, and array of that many snapshot ids.
2711 * For now we have a fixed upper limit on the number we're
2712 * prepared to receive.
2713 */
2714 size = sizeof (__le64) + sizeof (__le32) +
2715 RBD_MAX_SNAP_COUNT * sizeof (__le64);
2716 reply_buf = kzalloc(size, GFP_KERNEL);
2717 if (!reply_buf)
2718 return -ENOMEM;
2719
2720 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2721 "rbd", "get_snapcontext",
2722 NULL, 0,
2723 reply_buf, size,
6e14b1a6 2724 CEPH_OSD_FLAG_READ, ver);
35d489f9
AE
2725 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2726 if (ret < 0)
2727 goto out;
2728
2729 ret = -ERANGE;
2730 p = reply_buf;
2731 end = (char *) reply_buf + size;
2732 ceph_decode_64_safe(&p, end, seq, out);
2733 ceph_decode_32_safe(&p, end, snap_count, out);
2734
2735 /*
2736 * Make sure the reported number of snapshot ids wouldn't go
2737 * beyond the end of our buffer. But before checking that,
2738 * make sure the computed size of the snapshot context we
2739 * allocate is representable in a size_t.
2740 */
2741 if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
2742 / sizeof (u64)) {
2743 ret = -EINVAL;
2744 goto out;
2745 }
2746 if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
2747 goto out;
2748
2749 size = sizeof (struct ceph_snap_context) +
2750 snap_count * sizeof (snapc->snaps[0]);
2751 snapc = kmalloc(size, GFP_KERNEL);
2752 if (!snapc) {
2753 ret = -ENOMEM;
2754 goto out;
2755 }
2756
2757 atomic_set(&snapc->nref, 1);
2758 snapc->seq = seq;
2759 snapc->num_snaps = snap_count;
2760 for (i = 0; i < snap_count; i++)
2761 snapc->snaps[i] = ceph_decode_64(&p);
2762
2763 rbd_dev->header.snapc = snapc;
2764
2765 dout(" snap context seq = %llu, snap_count = %u\n",
2766 (unsigned long long) seq, (unsigned int) snap_count);
2767
2768out:
2769 kfree(reply_buf);
2770
2771 return 0;
2772}
2773
b8b1e2db
AE
2774static char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, u32 which)
2775{
2776 size_t size;
2777 void *reply_buf;
2778 __le64 snap_id;
2779 int ret;
2780 void *p;
2781 void *end;
b8b1e2db
AE
2782 char *snap_name;
2783
2784 size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN;
2785 reply_buf = kmalloc(size, GFP_KERNEL);
2786 if (!reply_buf)
2787 return ERR_PTR(-ENOMEM);
2788
2789 snap_id = cpu_to_le64(rbd_dev->header.snapc->snaps[which]);
2790 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2791 "rbd", "get_snapshot_name",
2792 (char *) &snap_id, sizeof (snap_id),
2793 reply_buf, size,
2794 CEPH_OSD_FLAG_READ, NULL);
2795 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2796 if (ret < 0)
2797 goto out;
2798
2799 p = reply_buf;
2800 end = (char *) reply_buf + size;
e5c35534 2801 snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
b8b1e2db
AE
2802 if (IS_ERR(snap_name)) {
2803 ret = PTR_ERR(snap_name);
2804 goto out;
2805 } else {
2806 dout(" snap_id 0x%016llx snap_name = %s\n",
2807 (unsigned long long) le64_to_cpu(snap_id), snap_name);
2808 }
2809 kfree(reply_buf);
2810
2811 return snap_name;
2812out:
2813 kfree(reply_buf);
2814
2815 return ERR_PTR(ret);
2816}
2817
2818static char *rbd_dev_v2_snap_info(struct rbd_device *rbd_dev, u32 which,
2819 u64 *snap_size, u64 *snap_features)
2820{
2821 __le64 snap_id;
2822 u8 order;
2823 int ret;
2824
2825 snap_id = rbd_dev->header.snapc->snaps[which];
2826 ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, &order, snap_size);
2827 if (ret)
2828 return ERR_PTR(ret);
2829 ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, snap_features);
2830 if (ret)
2831 return ERR_PTR(ret);
2832
2833 return rbd_dev_v2_snap_name(rbd_dev, which);
2834}
2835
2836static char *rbd_dev_snap_info(struct rbd_device *rbd_dev, u32 which,
2837 u64 *snap_size, u64 *snap_features)
2838{
2839 if (rbd_dev->image_format == 1)
2840 return rbd_dev_v1_snap_info(rbd_dev, which,
2841 snap_size, snap_features);
2842 if (rbd_dev->image_format == 2)
2843 return rbd_dev_v2_snap_info(rbd_dev, which,
2844 snap_size, snap_features);
2845 return ERR_PTR(-EINVAL);
2846}
2847
117973fb
AE
2848static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev, u64 *hver)
2849{
2850 int ret;
2851 __u8 obj_order;
2852
2853 down_write(&rbd_dev->header_rwsem);
2854
2855 /* Grab old order first, to see if it changes */
2856
2857 obj_order = rbd_dev->header.obj_order,
2858 ret = rbd_dev_v2_image_size(rbd_dev);
2859 if (ret)
2860 goto out;
2861 if (rbd_dev->header.obj_order != obj_order) {
2862 ret = -EIO;
2863 goto out;
2864 }
2865 rbd_update_mapping_size(rbd_dev);
2866
2867 ret = rbd_dev_v2_snap_context(rbd_dev, hver);
2868 dout("rbd_dev_v2_snap_context returned %d\n", ret);
2869 if (ret)
2870 goto out;
2871 ret = rbd_dev_snaps_update(rbd_dev);
2872 dout("rbd_dev_snaps_update returned %d\n", ret);
2873 if (ret)
2874 goto out;
2875 ret = rbd_dev_snaps_register(rbd_dev);
2876 dout("rbd_dev_snaps_register returned %d\n", ret);
2877out:
2878 up_write(&rbd_dev->header_rwsem);
2879
2880 return ret;
2881}
2882
dfc5606d 2883/*
35938150
AE
2884 * Scan the rbd device's current snapshot list and compare it to the
2885 * newly-received snapshot context. Remove any existing snapshots
2886 * not present in the new snapshot context. Add a new snapshot for
2887 * any snaphots in the snapshot context not in the current list.
2888 * And verify there are no changes to snapshots we already know
2889 * about.
2890 *
2891 * Assumes the snapshots in the snapshot context are sorted by
2892 * snapshot id, highest id first. (Snapshots in the rbd_dev's list
2893 * are also maintained in that order.)
dfc5606d 2894 */
304f6808 2895static int rbd_dev_snaps_update(struct rbd_device *rbd_dev)
dfc5606d 2896{
35938150
AE
2897 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
2898 const u32 snap_count = snapc->num_snaps;
35938150
AE
2899 struct list_head *head = &rbd_dev->snaps;
2900 struct list_head *links = head->next;
2901 u32 index = 0;
dfc5606d 2902
9fcbb800 2903 dout("%s: snap count is %u\n", __func__, (unsigned int) snap_count);
35938150
AE
2904 while (index < snap_count || links != head) {
2905 u64 snap_id;
2906 struct rbd_snap *snap;
cd892126
AE
2907 char *snap_name;
2908 u64 snap_size = 0;
2909 u64 snap_features = 0;
dfc5606d 2910
35938150
AE
2911 snap_id = index < snap_count ? snapc->snaps[index]
2912 : CEPH_NOSNAP;
2913 snap = links != head ? list_entry(links, struct rbd_snap, node)
2914 : NULL;
aafb230e 2915 rbd_assert(!snap || snap->id != CEPH_NOSNAP);
dfc5606d 2916
35938150
AE
2917 if (snap_id == CEPH_NOSNAP || (snap && snap->id > snap_id)) {
2918 struct list_head *next = links->next;
dfc5606d 2919
35938150 2920 /* Existing snapshot not in the new snap context */
dfc5606d 2921
0d7dbfce 2922 if (rbd_dev->spec->snap_id == snap->id)
daba5fdb 2923 rbd_dev->exists = false;
41f38c2b 2924 rbd_remove_snap_dev(snap);
9fcbb800 2925 dout("%ssnap id %llu has been removed\n",
0d7dbfce
AE
2926 rbd_dev->spec->snap_id == snap->id ?
2927 "mapped " : "",
9fcbb800 2928 (unsigned long long) snap->id);
35938150
AE
2929
2930 /* Done with this list entry; advance */
2931
2932 links = next;
dfc5606d
YS
2933 continue;
2934 }
35938150 2935
b8b1e2db
AE
2936 snap_name = rbd_dev_snap_info(rbd_dev, index,
2937 &snap_size, &snap_features);
cd892126
AE
2938 if (IS_ERR(snap_name))
2939 return PTR_ERR(snap_name);
2940
9fcbb800
AE
2941 dout("entry %u: snap_id = %llu\n", (unsigned int) snap_count,
2942 (unsigned long long) snap_id);
35938150
AE
2943 if (!snap || (snap_id != CEPH_NOSNAP && snap->id < snap_id)) {
2944 struct rbd_snap *new_snap;
2945
2946 /* We haven't seen this snapshot before */
2947
c8d18425 2948 new_snap = __rbd_add_snap_dev(rbd_dev, snap_name,
cd892126 2949 snap_id, snap_size, snap_features);
9fcbb800
AE
2950 if (IS_ERR(new_snap)) {
2951 int err = PTR_ERR(new_snap);
2952
2953 dout(" failed to add dev, error %d\n", err);
2954
2955 return err;
2956 }
35938150
AE
2957
2958 /* New goes before existing, or at end of list */
2959
9fcbb800 2960 dout(" added dev%s\n", snap ? "" : " at end\n");
35938150
AE
2961 if (snap)
2962 list_add_tail(&new_snap->node, &snap->node);
2963 else
523f3258 2964 list_add_tail(&new_snap->node, head);
35938150
AE
2965 } else {
2966 /* Already have this one */
2967
9fcbb800
AE
2968 dout(" already present\n");
2969
cd892126 2970 rbd_assert(snap->size == snap_size);
aafb230e 2971 rbd_assert(!strcmp(snap->name, snap_name));
cd892126 2972 rbd_assert(snap->features == snap_features);
35938150
AE
2973
2974 /* Done with this list entry; advance */
2975
2976 links = links->next;
dfc5606d 2977 }
35938150
AE
2978
2979 /* Advance to the next entry in the snapshot context */
2980
2981 index++;
dfc5606d 2982 }
9fcbb800 2983 dout("%s: done\n", __func__);
dfc5606d
YS
2984
2985 return 0;
2986}
2987
304f6808
AE
2988/*
2989 * Scan the list of snapshots and register the devices for any that
2990 * have not already been registered.
2991 */
2992static int rbd_dev_snaps_register(struct rbd_device *rbd_dev)
2993{
2994 struct rbd_snap *snap;
2995 int ret = 0;
2996
2997 dout("%s called\n", __func__);
86ff77bb
AE
2998 if (WARN_ON(!device_is_registered(&rbd_dev->dev)))
2999 return -EIO;
304f6808
AE
3000
3001 list_for_each_entry(snap, &rbd_dev->snaps, node) {
3002 if (!rbd_snap_registered(snap)) {
3003 ret = rbd_register_snap_dev(snap, &rbd_dev->dev);
3004 if (ret < 0)
3005 break;
3006 }
3007 }
3008 dout("%s: returning %d\n", __func__, ret);
3009
3010 return ret;
3011}
3012
dfc5606d
YS
3013static int rbd_bus_add_dev(struct rbd_device *rbd_dev)
3014{
dfc5606d 3015 struct device *dev;
cd789ab9 3016 int ret;
dfc5606d
YS
3017
3018 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
dfc5606d 3019
cd789ab9 3020 dev = &rbd_dev->dev;
dfc5606d
YS
3021 dev->bus = &rbd_bus_type;
3022 dev->type = &rbd_device_type;
3023 dev->parent = &rbd_root_dev;
3024 dev->release = rbd_dev_release;
de71a297 3025 dev_set_name(dev, "%d", rbd_dev->dev_id);
dfc5606d 3026 ret = device_register(dev);
dfc5606d 3027
dfc5606d 3028 mutex_unlock(&ctl_mutex);
cd789ab9 3029
dfc5606d 3030 return ret;
602adf40
YS
3031}
3032
dfc5606d
YS
3033static void rbd_bus_del_dev(struct rbd_device *rbd_dev)
3034{
3035 device_unregister(&rbd_dev->dev);
3036}
3037
59c2be1e
YS
3038static int rbd_init_watch_dev(struct rbd_device *rbd_dev)
3039{
3040 int ret, rc;
3041
3042 do {
0e6f322d 3043 ret = rbd_req_sync_watch(rbd_dev);
59c2be1e 3044 if (ret == -ERANGE) {
117973fb 3045 rc = rbd_dev_refresh(rbd_dev, NULL);
59c2be1e
YS
3046 if (rc < 0)
3047 return rc;
3048 }
3049 } while (ret == -ERANGE);
3050
3051 return ret;
3052}
3053
e2839308 3054static atomic64_t rbd_dev_id_max = ATOMIC64_INIT(0);
1ddbe94e
AE
3055
3056/*
499afd5b
AE
3057 * Get a unique rbd identifier for the given new rbd_dev, and add
3058 * the rbd_dev to the global list. The minimum rbd id is 1.
1ddbe94e 3059 */
e2839308 3060static void rbd_dev_id_get(struct rbd_device *rbd_dev)
b7f23c36 3061{
e2839308 3062 rbd_dev->dev_id = atomic64_inc_return(&rbd_dev_id_max);
499afd5b
AE
3063
3064 spin_lock(&rbd_dev_list_lock);
3065 list_add_tail(&rbd_dev->node, &rbd_dev_list);
3066 spin_unlock(&rbd_dev_list_lock);
e2839308
AE
3067 dout("rbd_dev %p given dev id %llu\n", rbd_dev,
3068 (unsigned long long) rbd_dev->dev_id);
1ddbe94e 3069}
b7f23c36 3070
1ddbe94e 3071/*
499afd5b
AE
3072 * Remove an rbd_dev from the global list, and record that its
3073 * identifier is no longer in use.
1ddbe94e 3074 */
e2839308 3075static void rbd_dev_id_put(struct rbd_device *rbd_dev)
1ddbe94e 3076{
d184f6bf 3077 struct list_head *tmp;
de71a297 3078 int rbd_id = rbd_dev->dev_id;
d184f6bf
AE
3079 int max_id;
3080
aafb230e 3081 rbd_assert(rbd_id > 0);
499afd5b 3082
e2839308
AE
3083 dout("rbd_dev %p released dev id %llu\n", rbd_dev,
3084 (unsigned long long) rbd_dev->dev_id);
499afd5b
AE
3085 spin_lock(&rbd_dev_list_lock);
3086 list_del_init(&rbd_dev->node);
d184f6bf
AE
3087
3088 /*
3089 * If the id being "put" is not the current maximum, there
3090 * is nothing special we need to do.
3091 */
e2839308 3092 if (rbd_id != atomic64_read(&rbd_dev_id_max)) {
d184f6bf
AE
3093 spin_unlock(&rbd_dev_list_lock);
3094 return;
3095 }
3096
3097 /*
3098 * We need to update the current maximum id. Search the
3099 * list to find out what it is. We're more likely to find
3100 * the maximum at the end, so search the list backward.
3101 */
3102 max_id = 0;
3103 list_for_each_prev(tmp, &rbd_dev_list) {
3104 struct rbd_device *rbd_dev;
3105
3106 rbd_dev = list_entry(tmp, struct rbd_device, node);
b213e0b1
AE
3107 if (rbd_dev->dev_id > max_id)
3108 max_id = rbd_dev->dev_id;
d184f6bf 3109 }
499afd5b 3110 spin_unlock(&rbd_dev_list_lock);
b7f23c36 3111
1ddbe94e 3112 /*
e2839308 3113 * The max id could have been updated by rbd_dev_id_get(), in
d184f6bf
AE
3114 * which case it now accurately reflects the new maximum.
3115 * Be careful not to overwrite the maximum value in that
3116 * case.
1ddbe94e 3117 */
e2839308
AE
3118 atomic64_cmpxchg(&rbd_dev_id_max, rbd_id, max_id);
3119 dout(" max dev id has been reset\n");
b7f23c36
AE
3120}
3121
e28fff26
AE
3122/*
3123 * Skips over white space at *buf, and updates *buf to point to the
3124 * first found non-space character (if any). Returns the length of
593a9e7b
AE
3125 * the token (string of non-white space characters) found. Note
3126 * that *buf must be terminated with '\0'.
e28fff26
AE
3127 */
3128static inline size_t next_token(const char **buf)
3129{
3130 /*
3131 * These are the characters that produce nonzero for
3132 * isspace() in the "C" and "POSIX" locales.
3133 */
3134 const char *spaces = " \f\n\r\t\v";
3135
3136 *buf += strspn(*buf, spaces); /* Find start of token */
3137
3138 return strcspn(*buf, spaces); /* Return token length */
3139}
3140
3141/*
3142 * Finds the next token in *buf, and if the provided token buffer is
3143 * big enough, copies the found token into it. The result, if
593a9e7b
AE
3144 * copied, is guaranteed to be terminated with '\0'. Note that *buf
3145 * must be terminated with '\0' on entry.
e28fff26
AE
3146 *
3147 * Returns the length of the token found (not including the '\0').
3148 * Return value will be 0 if no token is found, and it will be >=
3149 * token_size if the token would not fit.
3150 *
593a9e7b 3151 * The *buf pointer will be updated to point beyond the end of the
e28fff26
AE
3152 * found token. Note that this occurs even if the token buffer is
3153 * too small to hold it.
3154 */
3155static inline size_t copy_token(const char **buf,
3156 char *token,
3157 size_t token_size)
3158{
3159 size_t len;
3160
3161 len = next_token(buf);
3162 if (len < token_size) {
3163 memcpy(token, *buf, len);
3164 *(token + len) = '\0';
3165 }
3166 *buf += len;
3167
3168 return len;
3169}
3170
ea3352f4
AE
3171/*
3172 * Finds the next token in *buf, dynamically allocates a buffer big
3173 * enough to hold a copy of it, and copies the token into the new
3174 * buffer. The copy is guaranteed to be terminated with '\0'. Note
3175 * that a duplicate buffer is created even for a zero-length token.
3176 *
3177 * Returns a pointer to the newly-allocated duplicate, or a null
3178 * pointer if memory for the duplicate was not available. If
3179 * the lenp argument is a non-null pointer, the length of the token
3180 * (not including the '\0') is returned in *lenp.
3181 *
3182 * If successful, the *buf pointer will be updated to point beyond
3183 * the end of the found token.
3184 *
3185 * Note: uses GFP_KERNEL for allocation.
3186 */
3187static inline char *dup_token(const char **buf, size_t *lenp)
3188{
3189 char *dup;
3190 size_t len;
3191
3192 len = next_token(buf);
4caf35f9 3193 dup = kmemdup(*buf, len + 1, GFP_KERNEL);
ea3352f4
AE
3194 if (!dup)
3195 return NULL;
ea3352f4
AE
3196 *(dup + len) = '\0';
3197 *buf += len;
3198
3199 if (lenp)
3200 *lenp = len;
3201
3202 return dup;
3203}
3204
a725f65e 3205/*
859c31df
AE
3206 * Parse the options provided for an "rbd add" (i.e., rbd image
3207 * mapping) request. These arrive via a write to /sys/bus/rbd/add,
3208 * and the data written is passed here via a NUL-terminated buffer.
3209 * Returns 0 if successful or an error code otherwise.
d22f76e7 3210 *
859c31df
AE
3211 * The information extracted from these options is recorded in
3212 * the other parameters which return dynamically-allocated
3213 * structures:
3214 * ceph_opts
3215 * The address of a pointer that will refer to a ceph options
3216 * structure. Caller must release the returned pointer using
3217 * ceph_destroy_options() when it is no longer needed.
3218 * rbd_opts
3219 * Address of an rbd options pointer. Fully initialized by
3220 * this function; caller must release with kfree().
3221 * spec
3222 * Address of an rbd image specification pointer. Fully
3223 * initialized by this function based on parsed options.
3224 * Caller must release with rbd_spec_put().
3225 *
3226 * The options passed take this form:
3227 * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
3228 * where:
3229 * <mon_addrs>
3230 * A comma-separated list of one or more monitor addresses.
3231 * A monitor address is an ip address, optionally followed
3232 * by a port number (separated by a colon).
3233 * I.e.: ip1[:port1][,ip2[:port2]...]
3234 * <options>
3235 * A comma-separated list of ceph and/or rbd options.
3236 * <pool_name>
3237 * The name of the rados pool containing the rbd image.
3238 * <image_name>
3239 * The name of the image in that pool to map.
3240 * <snap_id>
3241 * An optional snapshot id. If provided, the mapping will
3242 * present data from the image at the time that snapshot was
3243 * created. The image head is used if no snapshot id is
3244 * provided. Snapshot mappings are always read-only.
a725f65e 3245 */
859c31df 3246static int rbd_add_parse_args(const char *buf,
dc79b113 3247 struct ceph_options **ceph_opts,
859c31df
AE
3248 struct rbd_options **opts,
3249 struct rbd_spec **rbd_spec)
e28fff26 3250{
d22f76e7 3251 size_t len;
859c31df 3252 char *options;
0ddebc0c
AE
3253 const char *mon_addrs;
3254 size_t mon_addrs_size;
859c31df 3255 struct rbd_spec *spec = NULL;
4e9afeba 3256 struct rbd_options *rbd_opts = NULL;
859c31df 3257 struct ceph_options *copts;
dc79b113 3258 int ret;
e28fff26
AE
3259
3260 /* The first four tokens are required */
3261
7ef3214a 3262 len = next_token(&buf);
4fb5d671
AE
3263 if (!len) {
3264 rbd_warn(NULL, "no monitor address(es) provided");
3265 return -EINVAL;
3266 }
0ddebc0c 3267 mon_addrs = buf;
f28e565a 3268 mon_addrs_size = len + 1;
7ef3214a 3269 buf += len;
a725f65e 3270
dc79b113 3271 ret = -EINVAL;
f28e565a
AE
3272 options = dup_token(&buf, NULL);
3273 if (!options)
dc79b113 3274 return -ENOMEM;
4fb5d671
AE
3275 if (!*options) {
3276 rbd_warn(NULL, "no options provided");
3277 goto out_err;
3278 }
e28fff26 3279
859c31df
AE
3280 spec = rbd_spec_alloc();
3281 if (!spec)
f28e565a 3282 goto out_mem;
859c31df
AE
3283
3284 spec->pool_name = dup_token(&buf, NULL);
3285 if (!spec->pool_name)
3286 goto out_mem;
4fb5d671
AE
3287 if (!*spec->pool_name) {
3288 rbd_warn(NULL, "no pool name provided");
3289 goto out_err;
3290 }
e28fff26 3291
69e7a02f 3292 spec->image_name = dup_token(&buf, NULL);
859c31df 3293 if (!spec->image_name)
f28e565a 3294 goto out_mem;
4fb5d671
AE
3295 if (!*spec->image_name) {
3296 rbd_warn(NULL, "no image name provided");
3297 goto out_err;
3298 }
d4b125e9 3299
f28e565a
AE
3300 /*
3301 * Snapshot name is optional; default is to use "-"
3302 * (indicating the head/no snapshot).
3303 */
3feeb894 3304 len = next_token(&buf);
820a5f3e 3305 if (!len) {
3feeb894
AE
3306 buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
3307 len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
f28e565a 3308 } else if (len > RBD_MAX_SNAP_NAME_LEN) {
dc79b113 3309 ret = -ENAMETOOLONG;
f28e565a 3310 goto out_err;
849b4260 3311 }
4caf35f9 3312 spec->snap_name = kmemdup(buf, len + 1, GFP_KERNEL);
859c31df 3313 if (!spec->snap_name)
f28e565a 3314 goto out_mem;
859c31df 3315 *(spec->snap_name + len) = '\0';
e5c35534 3316
0ddebc0c 3317 /* Initialize all rbd options to the defaults */
e28fff26 3318
4e9afeba
AE
3319 rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL);
3320 if (!rbd_opts)
3321 goto out_mem;
3322
3323 rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
d22f76e7 3324
859c31df 3325 copts = ceph_parse_options(options, mon_addrs,
0ddebc0c 3326 mon_addrs + mon_addrs_size - 1,
4e9afeba 3327 parse_rbd_opts_token, rbd_opts);
859c31df
AE
3328 if (IS_ERR(copts)) {
3329 ret = PTR_ERR(copts);
dc79b113
AE
3330 goto out_err;
3331 }
859c31df
AE
3332 kfree(options);
3333
3334 *ceph_opts = copts;
4e9afeba 3335 *opts = rbd_opts;
859c31df 3336 *rbd_spec = spec;
0ddebc0c 3337
dc79b113 3338 return 0;
f28e565a 3339out_mem:
dc79b113 3340 ret = -ENOMEM;
d22f76e7 3341out_err:
859c31df
AE
3342 kfree(rbd_opts);
3343 rbd_spec_put(spec);
f28e565a 3344 kfree(options);
d22f76e7 3345
dc79b113 3346 return ret;
a725f65e
AE
3347}
3348
589d30e0
AE
3349/*
3350 * An rbd format 2 image has a unique identifier, distinct from the
3351 * name given to it by the user. Internally, that identifier is
3352 * what's used to specify the names of objects related to the image.
3353 *
3354 * A special "rbd id" object is used to map an rbd image name to its
3355 * id. If that object doesn't exist, then there is no v2 rbd image
3356 * with the supplied name.
3357 *
3358 * This function will record the given rbd_dev's image_id field if
3359 * it can be determined, and in that case will return 0. If any
3360 * errors occur a negative errno will be returned and the rbd_dev's
3361 * image_id field will be unchanged (and should be NULL).
3362 */
3363static int rbd_dev_image_id(struct rbd_device *rbd_dev)
3364{
3365 int ret;
3366 size_t size;
3367 char *object_name;
3368 void *response;
3369 void *p;
3370
2c0d0a10
AE
3371 /*
3372 * When probing a parent image, the image id is already
3373 * known (and the image name likely is not). There's no
3374 * need to fetch the image id again in this case.
3375 */
3376 if (rbd_dev->spec->image_id)
3377 return 0;
3378
589d30e0
AE
3379 /*
3380 * First, see if the format 2 image id file exists, and if
3381 * so, get the image's persistent id from it.
3382 */
69e7a02f 3383 size = sizeof (RBD_ID_PREFIX) + strlen(rbd_dev->spec->image_name);
589d30e0
AE
3384 object_name = kmalloc(size, GFP_NOIO);
3385 if (!object_name)
3386 return -ENOMEM;
0d7dbfce 3387 sprintf(object_name, "%s%s", RBD_ID_PREFIX, rbd_dev->spec->image_name);
589d30e0
AE
3388 dout("rbd id object name is %s\n", object_name);
3389
3390 /* Response will be an encoded string, which includes a length */
3391
3392 size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
3393 response = kzalloc(size, GFP_NOIO);
3394 if (!response) {
3395 ret = -ENOMEM;
3396 goto out;
3397 }
3398
3399 ret = rbd_req_sync_exec(rbd_dev, object_name,
3400 "rbd", "get_id",
3401 NULL, 0,
3402 response, RBD_IMAGE_ID_LEN_MAX,
3403 CEPH_OSD_FLAG_READ, NULL);
3404 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
3405 if (ret < 0)
3406 goto out;
a0ea3a40 3407 ret = 0; /* rbd_req_sync_exec() can return positive */
589d30e0
AE
3408
3409 p = response;
0d7dbfce 3410 rbd_dev->spec->image_id = ceph_extract_encoded_string(&p,
589d30e0 3411 p + RBD_IMAGE_ID_LEN_MAX,
979ed480 3412 NULL, GFP_NOIO);
0d7dbfce
AE
3413 if (IS_ERR(rbd_dev->spec->image_id)) {
3414 ret = PTR_ERR(rbd_dev->spec->image_id);
3415 rbd_dev->spec->image_id = NULL;
589d30e0 3416 } else {
0d7dbfce 3417 dout("image_id is %s\n", rbd_dev->spec->image_id);
589d30e0
AE
3418 }
3419out:
3420 kfree(response);
3421 kfree(object_name);
3422
3423 return ret;
3424}
3425
a30b71b9
AE
3426static int rbd_dev_v1_probe(struct rbd_device *rbd_dev)
3427{
3428 int ret;
3429 size_t size;
3430
3431 /* Version 1 images have no id; empty string is used */
3432
0d7dbfce
AE
3433 rbd_dev->spec->image_id = kstrdup("", GFP_KERNEL);
3434 if (!rbd_dev->spec->image_id)
a30b71b9 3435 return -ENOMEM;
a30b71b9
AE
3436
3437 /* Record the header object name for this rbd image. */
3438
69e7a02f 3439 size = strlen(rbd_dev->spec->image_name) + sizeof (RBD_SUFFIX);
a30b71b9
AE
3440 rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
3441 if (!rbd_dev->header_name) {
3442 ret = -ENOMEM;
3443 goto out_err;
3444 }
0d7dbfce
AE
3445 sprintf(rbd_dev->header_name, "%s%s",
3446 rbd_dev->spec->image_name, RBD_SUFFIX);
a30b71b9
AE
3447
3448 /* Populate rbd image metadata */
3449
3450 ret = rbd_read_header(rbd_dev, &rbd_dev->header);
3451 if (ret < 0)
3452 goto out_err;
86b00e0d
AE
3453
3454 /* Version 1 images have no parent (no layering) */
3455
3456 rbd_dev->parent_spec = NULL;
3457 rbd_dev->parent_overlap = 0;
3458
a30b71b9
AE
3459 rbd_dev->image_format = 1;
3460
3461 dout("discovered version 1 image, header name is %s\n",
3462 rbd_dev->header_name);
3463
3464 return 0;
3465
3466out_err:
3467 kfree(rbd_dev->header_name);
3468 rbd_dev->header_name = NULL;
0d7dbfce
AE
3469 kfree(rbd_dev->spec->image_id);
3470 rbd_dev->spec->image_id = NULL;
a30b71b9
AE
3471
3472 return ret;
3473}
3474
3475static int rbd_dev_v2_probe(struct rbd_device *rbd_dev)
3476{
3477 size_t size;
9d475de5 3478 int ret;
6e14b1a6 3479 u64 ver = 0;
a30b71b9
AE
3480
3481 /*
3482 * Image id was filled in by the caller. Record the header
3483 * object name for this rbd image.
3484 */
979ed480 3485 size = sizeof (RBD_HEADER_PREFIX) + strlen(rbd_dev->spec->image_id);
a30b71b9
AE
3486 rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
3487 if (!rbd_dev->header_name)
3488 return -ENOMEM;
3489 sprintf(rbd_dev->header_name, "%s%s",
0d7dbfce 3490 RBD_HEADER_PREFIX, rbd_dev->spec->image_id);
9d475de5
AE
3491
3492 /* Get the size and object order for the image */
3493
3494 ret = rbd_dev_v2_image_size(rbd_dev);
1e130199
AE
3495 if (ret < 0)
3496 goto out_err;
3497
3498 /* Get the object prefix (a.k.a. block_name) for the image */
3499
3500 ret = rbd_dev_v2_object_prefix(rbd_dev);
b1b5402a
AE
3501 if (ret < 0)
3502 goto out_err;
3503
d889140c 3504 /* Get the and check features for the image */
b1b5402a
AE
3505
3506 ret = rbd_dev_v2_features(rbd_dev);
9d475de5
AE
3507 if (ret < 0)
3508 goto out_err;
35d489f9 3509
86b00e0d
AE
3510 /* If the image supports layering, get the parent info */
3511
3512 if (rbd_dev->header.features & RBD_FEATURE_LAYERING) {
3513 ret = rbd_dev_v2_parent_info(rbd_dev);
3514 if (ret < 0)
3515 goto out_err;
3516 }
3517
6e14b1a6
AE
3518 /* crypto and compression type aren't (yet) supported for v2 images */
3519
3520 rbd_dev->header.crypt_type = 0;
3521 rbd_dev->header.comp_type = 0;
35d489f9 3522
6e14b1a6
AE
3523 /* Get the snapshot context, plus the header version */
3524
3525 ret = rbd_dev_v2_snap_context(rbd_dev, &ver);
35d489f9
AE
3526 if (ret)
3527 goto out_err;
6e14b1a6
AE
3528 rbd_dev->header.obj_version = ver;
3529
a30b71b9
AE
3530 rbd_dev->image_format = 2;
3531
3532 dout("discovered version 2 image, header name is %s\n",
3533 rbd_dev->header_name);
3534
35152979 3535 return 0;
9d475de5 3536out_err:
86b00e0d
AE
3537 rbd_dev->parent_overlap = 0;
3538 rbd_spec_put(rbd_dev->parent_spec);
3539 rbd_dev->parent_spec = NULL;
9d475de5
AE
3540 kfree(rbd_dev->header_name);
3541 rbd_dev->header_name = NULL;
1e130199
AE
3542 kfree(rbd_dev->header.object_prefix);
3543 rbd_dev->header.object_prefix = NULL;
9d475de5
AE
3544
3545 return ret;
a30b71b9
AE
3546}
3547
83a06263
AE
3548static int rbd_dev_probe_finish(struct rbd_device *rbd_dev)
3549{
3550 int ret;
3551
3552 /* no need to lock here, as rbd_dev is not registered yet */
3553 ret = rbd_dev_snaps_update(rbd_dev);
3554 if (ret)
3555 return ret;
3556
9e15b77d
AE
3557 ret = rbd_dev_probe_update_spec(rbd_dev);
3558 if (ret)
3559 goto err_out_snaps;
3560
83a06263
AE
3561 ret = rbd_dev_set_mapping(rbd_dev);
3562 if (ret)
3563 goto err_out_snaps;
3564
3565 /* generate unique id: find highest unique id, add one */
3566 rbd_dev_id_get(rbd_dev);
3567
3568 /* Fill in the device name, now that we have its id. */
3569 BUILD_BUG_ON(DEV_NAME_LEN
3570 < sizeof (RBD_DRV_NAME) + MAX_INT_FORMAT_WIDTH);
3571 sprintf(rbd_dev->name, "%s%d", RBD_DRV_NAME, rbd_dev->dev_id);
3572
3573 /* Get our block major device number. */
3574
3575 ret = register_blkdev(0, rbd_dev->name);
3576 if (ret < 0)
3577 goto err_out_id;
3578 rbd_dev->major = ret;
3579
3580 /* Set up the blkdev mapping. */
3581
3582 ret = rbd_init_disk(rbd_dev);
3583 if (ret)
3584 goto err_out_blkdev;
3585
3586 ret = rbd_bus_add_dev(rbd_dev);
3587 if (ret)
3588 goto err_out_disk;
3589
3590 /*
3591 * At this point cleanup in the event of an error is the job
3592 * of the sysfs code (initiated by rbd_bus_del_dev()).
3593 */
3594 down_write(&rbd_dev->header_rwsem);
3595 ret = rbd_dev_snaps_register(rbd_dev);
3596 up_write(&rbd_dev->header_rwsem);
3597 if (ret)
3598 goto err_out_bus;
3599
3600 ret = rbd_init_watch_dev(rbd_dev);
3601 if (ret)
3602 goto err_out_bus;
3603
3604 /* Everything's ready. Announce the disk to the world. */
3605
3606 add_disk(rbd_dev->disk);
3607
3608 pr_info("%s: added with size 0x%llx\n", rbd_dev->disk->disk_name,
3609 (unsigned long long) rbd_dev->mapping.size);
3610
3611 return ret;
3612err_out_bus:
3613 /* this will also clean up rest of rbd_dev stuff */
3614
3615 rbd_bus_del_dev(rbd_dev);
3616
3617 return ret;
3618err_out_disk:
3619 rbd_free_disk(rbd_dev);
3620err_out_blkdev:
3621 unregister_blkdev(rbd_dev->major, rbd_dev->name);
3622err_out_id:
3623 rbd_dev_id_put(rbd_dev);
3624err_out_snaps:
3625 rbd_remove_all_snaps(rbd_dev);
3626
3627 return ret;
3628}
3629
a30b71b9
AE
3630/*
3631 * Probe for the existence of the header object for the given rbd
3632 * device. For format 2 images this includes determining the image
3633 * id.
3634 */
3635static int rbd_dev_probe(struct rbd_device *rbd_dev)
3636{
3637 int ret;
3638
3639 /*
3640 * Get the id from the image id object. If it's not a
3641 * format 2 image, we'll get ENOENT back, and we'll assume
3642 * it's a format 1 image.
3643 */
3644 ret = rbd_dev_image_id(rbd_dev);
3645 if (ret)
3646 ret = rbd_dev_v1_probe(rbd_dev);
3647 else
3648 ret = rbd_dev_v2_probe(rbd_dev);
83a06263 3649 if (ret) {
a30b71b9
AE
3650 dout("probe failed, returning %d\n", ret);
3651
83a06263
AE
3652 return ret;
3653 }
3654
3655 ret = rbd_dev_probe_finish(rbd_dev);
3656 if (ret)
3657 rbd_header_free(&rbd_dev->header);
3658
a30b71b9
AE
3659 return ret;
3660}
3661
59c2be1e
YS
3662static ssize_t rbd_add(struct bus_type *bus,
3663 const char *buf,
3664 size_t count)
602adf40 3665{
cb8627c7 3666 struct rbd_device *rbd_dev = NULL;
dc79b113 3667 struct ceph_options *ceph_opts = NULL;
4e9afeba 3668 struct rbd_options *rbd_opts = NULL;
859c31df 3669 struct rbd_spec *spec = NULL;
9d3997fd 3670 struct rbd_client *rbdc;
27cc2594
AE
3671 struct ceph_osd_client *osdc;
3672 int rc = -ENOMEM;
602adf40
YS
3673
3674 if (!try_module_get(THIS_MODULE))
3675 return -ENODEV;
3676
602adf40 3677 /* parse add command */
859c31df 3678 rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec);
dc79b113 3679 if (rc < 0)
bd4ba655 3680 goto err_out_module;
78cea76e 3681
9d3997fd
AE
3682 rbdc = rbd_get_client(ceph_opts);
3683 if (IS_ERR(rbdc)) {
3684 rc = PTR_ERR(rbdc);
0ddebc0c 3685 goto err_out_args;
9d3997fd 3686 }
c53d5893 3687 ceph_opts = NULL; /* rbd_dev client now owns this */
602adf40 3688
602adf40 3689 /* pick the pool */
9d3997fd 3690 osdc = &rbdc->client->osdc;
859c31df 3691 rc = ceph_pg_poolid_by_name(osdc->osdmap, spec->pool_name);
602adf40
YS
3692 if (rc < 0)
3693 goto err_out_client;
859c31df
AE
3694 spec->pool_id = (u64) rc;
3695
c53d5893 3696 rbd_dev = rbd_dev_create(rbdc, spec);
bd4ba655
AE
3697 if (!rbd_dev)
3698 goto err_out_client;
c53d5893
AE
3699 rbdc = NULL; /* rbd_dev now owns this */
3700 spec = NULL; /* rbd_dev now owns this */
602adf40 3701
bd4ba655 3702 rbd_dev->mapping.read_only = rbd_opts->read_only;
c53d5893
AE
3703 kfree(rbd_opts);
3704 rbd_opts = NULL; /* done with this */
bd4ba655 3705
a30b71b9
AE
3706 rc = rbd_dev_probe(rbd_dev);
3707 if (rc < 0)
c53d5893 3708 goto err_out_rbd_dev;
05fd6f6f 3709
602adf40 3710 return count;
c53d5893
AE
3711err_out_rbd_dev:
3712 rbd_dev_destroy(rbd_dev);
bd4ba655 3713err_out_client:
9d3997fd 3714 rbd_put_client(rbdc);
0ddebc0c 3715err_out_args:
78cea76e
AE
3716 if (ceph_opts)
3717 ceph_destroy_options(ceph_opts);
4e9afeba 3718 kfree(rbd_opts);
859c31df 3719 rbd_spec_put(spec);
bd4ba655
AE
3720err_out_module:
3721 module_put(THIS_MODULE);
27cc2594 3722
602adf40 3723 dout("Error adding device %s\n", buf);
27cc2594
AE
3724
3725 return (ssize_t) rc;
602adf40
YS
3726}
3727
de71a297 3728static struct rbd_device *__rbd_get_dev(unsigned long dev_id)
602adf40
YS
3729{
3730 struct list_head *tmp;
3731 struct rbd_device *rbd_dev;
3732
e124a82f 3733 spin_lock(&rbd_dev_list_lock);
602adf40
YS
3734 list_for_each(tmp, &rbd_dev_list) {
3735 rbd_dev = list_entry(tmp, struct rbd_device, node);
de71a297 3736 if (rbd_dev->dev_id == dev_id) {
e124a82f 3737 spin_unlock(&rbd_dev_list_lock);
602adf40 3738 return rbd_dev;
e124a82f 3739 }
602adf40 3740 }
e124a82f 3741 spin_unlock(&rbd_dev_list_lock);
602adf40
YS
3742 return NULL;
3743}
3744
dfc5606d 3745static void rbd_dev_release(struct device *dev)
602adf40 3746{
593a9e7b 3747 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
602adf40 3748
1dbb4399
AE
3749 if (rbd_dev->watch_request) {
3750 struct ceph_client *client = rbd_dev->rbd_client->client;
3751
3752 ceph_osdc_unregister_linger_request(&client->osdc,
59c2be1e 3753 rbd_dev->watch_request);
1dbb4399 3754 }
59c2be1e 3755 if (rbd_dev->watch_event)
070c633f 3756 rbd_req_sync_unwatch(rbd_dev);
59c2be1e 3757
602adf40
YS
3758
3759 /* clean up and free blkdev */
3760 rbd_free_disk(rbd_dev);
3761 unregister_blkdev(rbd_dev->major, rbd_dev->name);
32eec68d 3762
2ac4e75d
AE
3763 /* release allocated disk header fields */
3764 rbd_header_free(&rbd_dev->header);
3765
32eec68d 3766 /* done with the id, and with the rbd_dev */
e2839308 3767 rbd_dev_id_put(rbd_dev);
c53d5893
AE
3768 rbd_assert(rbd_dev->rbd_client != NULL);
3769 rbd_dev_destroy(rbd_dev);
602adf40
YS
3770
3771 /* release module ref */
3772 module_put(THIS_MODULE);
602adf40
YS
3773}
3774
dfc5606d
YS
3775static ssize_t rbd_remove(struct bus_type *bus,
3776 const char *buf,
3777 size_t count)
602adf40
YS
3778{
3779 struct rbd_device *rbd_dev = NULL;
3780 int target_id, rc;
3781 unsigned long ul;
3782 int ret = count;
3783
3784 rc = strict_strtoul(buf, 10, &ul);
3785 if (rc)
3786 return rc;
3787
3788 /* convert to int; abort if we lost anything in the conversion */
3789 target_id = (int) ul;
3790 if (target_id != ul)
3791 return -EINVAL;
3792
3793 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
3794
3795 rbd_dev = __rbd_get_dev(target_id);
3796 if (!rbd_dev) {
3797 ret = -ENOENT;
3798 goto done;
42382b70
AE
3799 }
3800
3801 if (rbd_dev->open_count) {
3802 ret = -EBUSY;
3803 goto done;
602adf40
YS
3804 }
3805
41f38c2b 3806 rbd_remove_all_snaps(rbd_dev);
dfc5606d 3807 rbd_bus_del_dev(rbd_dev);
602adf40
YS
3808
3809done:
3810 mutex_unlock(&ctl_mutex);
aafb230e 3811
602adf40
YS
3812 return ret;
3813}
3814
602adf40
YS
3815/*
3816 * create control files in sysfs
dfc5606d 3817 * /sys/bus/rbd/...
602adf40
YS
3818 */
3819static int rbd_sysfs_init(void)
3820{
dfc5606d 3821 int ret;
602adf40 3822
fed4c143 3823 ret = device_register(&rbd_root_dev);
21079786 3824 if (ret < 0)
dfc5606d 3825 return ret;
602adf40 3826
fed4c143
AE
3827 ret = bus_register(&rbd_bus_type);
3828 if (ret < 0)
3829 device_unregister(&rbd_root_dev);
602adf40 3830
602adf40
YS
3831 return ret;
3832}
3833
3834static void rbd_sysfs_cleanup(void)
3835{
dfc5606d 3836 bus_unregister(&rbd_bus_type);
fed4c143 3837 device_unregister(&rbd_root_dev);
602adf40
YS
3838}
3839
3840int __init rbd_init(void)
3841{
3842 int rc;
3843
3844 rc = rbd_sysfs_init();
3845 if (rc)
3846 return rc;
f0f8cef5 3847 pr_info("loaded " RBD_DRV_NAME_LONG "\n");
602adf40
YS
3848 return 0;
3849}
3850
3851void __exit rbd_exit(void)
3852{
3853 rbd_sysfs_cleanup();
3854}
3855
3856module_init(rbd_init);
3857module_exit(rbd_exit);
3858
3859MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
3860MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
3861MODULE_DESCRIPTION("rados block device");
3862
3863/* following authorship retained from original osdblk.c */
3864MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
3865
3866MODULE_LICENSE("GPL");
This page took 0.385334 seconds and 5 git commands to generate.