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