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