blob: a002984891d77189dc6ee6022356390beebf64b0 [file] [log] [blame]
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001/*
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
Yehuda Sadehdfc56062010-11-19 14:51:04 -080024 For usage instructions, please refer to:
Yehuda Sadeh602adf42010-08-12 16:11:25 -070025
Yehuda Sadehdfc56062010-11-19 14:51:04 -080026 Documentation/ABI/testing/sysfs-bus-rbd
Yehuda Sadeh602adf42010-08-12 16:11:25 -070027
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>
Yehuda Sadeh59c2be12011-03-21 15:10:11 -070034#include <linux/parser.h>
Yehuda Sadeh602adf42010-08-12 16:11:25 -070035
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
Alex Elderaafb2302012-09-06 16:00:54 -050044#define RBD_DEBUG /* Activate rbd_assert() calls */
45
Alex Elder593a9e72012-02-07 12:03:37 -060046/*
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
Alex Elderdf111be2012-08-09 10:33:26 -070055/* It might be useful to have this defined elsewhere too */
56
57#define U64_MAX ((u64) (~0ULL))
58
Alex Elderf0f8cef2012-01-29 13:57:44 -060059#define RBD_DRV_NAME "rbd"
60#define RBD_DRV_NAME_LONG "rbd (rados block device)"
Yehuda Sadeh602adf42010-08-12 16:11:25 -070061
62#define RBD_MINORS_PER_MAJOR 256 /* max minors per blkdev */
63
Alex Elderd4b125e2012-07-03 16:01:19 -050064#define RBD_SNAP_DEV_NAME_PREFIX "snap_"
65#define RBD_MAX_SNAP_NAME_LEN \
66 (NAME_MAX - (sizeof (RBD_SNAP_DEV_NAME_PREFIX) - 1))
67
Alex Elder35d489f2012-07-03 16:01:19 -050068#define RBD_MAX_SNAP_COUNT 510 /* allows max snapc to fit in 4KB */
Yehuda Sadeh602adf42010-08-12 16:11:25 -070069#define RBD_MAX_OPT_LEN 1024
70
71#define RBD_SNAP_HEAD_NAME "-"
72
Alex Elder9e15b772012-10-30 19:40:33 -050073/* This allows a single page to hold an image name sent by OSD */
74#define RBD_IMAGE_NAME_LEN_MAX (PAGE_SIZE - sizeof (__le32) - 1)
Alex Elder1e130192012-07-03 16:01:19 -050075#define RBD_IMAGE_ID_LEN_MAX 64
Alex Elder9e15b772012-10-30 19:40:33 -050076
Alex Elder1e130192012-07-03 16:01:19 -050077#define RBD_OBJ_PREFIX_LEN_MAX 64
Alex Elder589d30e2012-07-10 20:30:11 -050078
Alex Elderd8891402012-10-09 13:50:17 -070079/* Feature bits */
80
81#define RBD_FEATURE_LAYERING 1
82
83/* Features supported by this (client software) implementation. */
84
85#define RBD_FEATURES_ALL (0)
86
Alex Elder81a89792012-02-02 08:13:30 -060087/*
88 * An RBD device name will be "rbd#", where the "rbd" comes from
89 * RBD_DRV_NAME above, and # is a unique integer identifier.
90 * MAX_INT_FORMAT_WIDTH is used in ensuring DEV_NAME_LEN is big
91 * enough to hold all possible device names.
92 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -070093#define DEV_NAME_LEN 32
Alex Elder81a89792012-02-02 08:13:30 -060094#define MAX_INT_FORMAT_WIDTH ((5 * sizeof (int)) / 2 + 1)
Yehuda Sadeh602adf42010-08-12 16:11:25 -070095
Alex Eldercc0538b2012-08-10 13:12:07 -070096#define RBD_READ_ONLY_DEFAULT false
Yehuda Sadeh59c2be12011-03-21 15:10:11 -070097
Yehuda Sadeh602adf42010-08-12 16:11:25 -070098/*
99 * block device image metadata (in-memory version)
100 */
101struct rbd_image_header {
Alex Elderf84344f2012-08-31 17:29:51 -0500102 /* These four fields never change for a given rbd image */
Alex Elder849b4262012-07-09 21:04:24 -0500103 char *object_prefix;
Alex Elder34b13182012-07-13 20:35:12 -0500104 u64 features;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700105 __u8 obj_order;
106 __u8 crypt_type;
107 __u8 comp_type;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700108
Alex Elderf84344f2012-08-31 17:29:51 -0500109 /* The remaining fields need to be updated occasionally */
110 u64 image_size;
111 struct ceph_snap_context *snapc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700112 char *snap_names;
113 u64 *snap_sizes;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700114
115 u64 obj_version;
116};
117
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500118/*
119 * An rbd image specification.
120 *
121 * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely
Alex Elderc66c6e02012-11-01 08:39:26 -0500122 * 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).
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500142 */
143struct rbd_spec {
144 u64 pool_id;
145 char *pool_name;
146
147 char *image_id;
148 size_t image_id_len;
149 char *image_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500150
151 u64 snap_id;
152 char *snap_name;
153
154 struct kref kref;
155};
156
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700157struct rbd_options {
Alex Eldercc0538b2012-08-10 13:12:07 -0700158 bool read_only;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700159};
160
161/*
Alex Elderf0f8cef2012-01-29 13:57:44 -0600162 * an instance of the client. multiple devices may share an rbd client.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700163 */
164struct rbd_client {
165 struct ceph_client *client;
166 struct kref kref;
167 struct list_head node;
168};
169
170/*
Alex Elderf0f8cef2012-01-29 13:57:44 -0600171 * a request completion status
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700172 */
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700173struct rbd_req_status {
174 int done;
175 int rc;
176 u64 bytes;
177};
178
179/*
180 * a collection of requests
181 */
182struct rbd_req_coll {
183 int total;
184 int num_done;
185 struct kref kref;
186 struct rbd_req_status status[0];
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700187};
188
Alex Elderf0f8cef2012-01-29 13:57:44 -0600189/*
190 * a single io request
191 */
192struct rbd_request {
193 struct request *rq; /* blk layer request */
194 struct bio *bio; /* cloned bio */
195 struct page **pages; /* list of used pages */
196 u64 len;
197 int coll_index;
198 struct rbd_req_coll *coll;
199};
200
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800201struct rbd_snap {
202 struct device dev;
203 const char *name;
Josh Durgin3591538f2011-12-05 18:25:13 -0800204 u64 size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800205 struct list_head node;
206 u64 id;
Alex Elder34b13182012-07-13 20:35:12 -0500207 u64 features;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800208};
209
Alex Elderf84344f2012-08-31 17:29:51 -0500210struct rbd_mapping {
Alex Elder99c1f082012-08-30 14:42:15 -0500211 u64 size;
Alex Elder34b13182012-07-13 20:35:12 -0500212 u64 features;
Alex Elderf84344f2012-08-31 17:29:51 -0500213 bool read_only;
214};
215
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700216/*
217 * a single device
218 */
219struct rbd_device {
Alex Elderde71a292012-07-03 16:01:19 -0500220 int dev_id; /* blkdev unique id */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700221
222 int major; /* blkdev assigned major */
223 struct gendisk *disk; /* blkdev's gendisk and rq */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700224
Alex Eldera30b71b2012-07-10 20:30:11 -0500225 u32 image_format; /* Either 1 or 2 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700226 struct rbd_client *rbd_client;
227
228 char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */
229
230 spinlock_t lock; /* queue lock */
231
232 struct rbd_image_header header;
Alex Elderdaba5fd2012-10-26 17:25:23 -0500233 bool exists;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500234 struct rbd_spec *spec;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700235
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500236 char *header_name;
Alex Elder971f8392012-10-25 23:34:41 -0500237
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700238 struct ceph_osd_event *watch_event;
239 struct ceph_osd_request *watch_request;
240
Alex Elder86b00e02012-10-25 23:34:42 -0500241 struct rbd_spec *parent_spec;
242 u64 parent_overlap;
243
Josh Durginc6666012011-11-21 17:11:12 -0800244 /* protects updating the header */
245 struct rw_semaphore header_rwsem;
Alex Elderf84344f2012-08-31 17:29:51 -0500246
247 struct rbd_mapping mapping;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700248
249 struct list_head node;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800250
251 /* list of snapshots */
252 struct list_head snaps;
253
254 /* sysfs related */
255 struct device dev;
Alex Elder42382b72012-11-16 09:29:16 -0600256 unsigned long open_count;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800257};
258
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700259static DEFINE_MUTEX(ctl_mutex); /* Serialize open/close/setup/teardown */
Alex Eldere124a82f2012-01-29 13:57:44 -0600260
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700261static LIST_HEAD(rbd_dev_list); /* devices */
Alex Eldere124a82f2012-01-29 13:57:44 -0600262static DEFINE_SPINLOCK(rbd_dev_list_lock);
263
Alex Elder432b8582012-01-29 13:57:44 -0600264static LIST_HEAD(rbd_client_list); /* clients */
265static DEFINE_SPINLOCK(rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700266
Alex Elder304f6802012-08-31 17:29:52 -0500267static int rbd_dev_snaps_update(struct rbd_device *rbd_dev);
268static int rbd_dev_snaps_register(struct rbd_device *rbd_dev);
269
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800270static void rbd_dev_release(struct device *dev);
Alex Elder41f38c22012-10-25 23:34:40 -0500271static void rbd_remove_snap_dev(struct rbd_snap *snap);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800272
Alex Elderf0f8cef2012-01-29 13:57:44 -0600273static ssize_t rbd_add(struct bus_type *bus, const char *buf,
274 size_t count);
275static ssize_t rbd_remove(struct bus_type *bus, const char *buf,
276 size_t count);
277
278static struct bus_attribute rbd_bus_attrs[] = {
279 __ATTR(add, S_IWUSR, NULL, rbd_add),
280 __ATTR(remove, S_IWUSR, NULL, rbd_remove),
281 __ATTR_NULL
282};
283
284static struct bus_type rbd_bus_type = {
285 .name = "rbd",
286 .bus_attrs = rbd_bus_attrs,
287};
288
289static void rbd_root_dev_release(struct device *dev)
290{
291}
292
293static struct device rbd_root_dev = {
294 .init_name = "rbd",
295 .release = rbd_root_dev_release,
296};
297
Alex Elderaafb2302012-09-06 16:00:54 -0500298#ifdef RBD_DEBUG
299#define rbd_assert(expr) \
300 if (unlikely(!(expr))) { \
301 printk(KERN_ERR "\nAssertion failure in %s() " \
302 "at line %d:\n\n" \
303 "\trbd_assert(%s);\n\n", \
304 __func__, __LINE__, #expr); \
305 BUG(); \
306 }
307#else /* !RBD_DEBUG */
308# define rbd_assert(expr) ((void) 0)
309#endif /* !RBD_DEBUG */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800310
Alex Elder117973f2012-08-31 17:29:55 -0500311static int rbd_dev_refresh(struct rbd_device *rbd_dev, u64 *hver);
312static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev, u64 *hver);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700313
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700314static int rbd_open(struct block_device *bdev, fmode_t mode)
315{
Alex Elderf0f8cef2012-01-29 13:57:44 -0600316 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700317
Alex Elderf84344f2012-08-31 17:29:51 -0500318 if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700319 return -EROFS;
320
Alex Elder42382b72012-11-16 09:29:16 -0600321 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Alex Elderc3e946c2012-11-16 09:29:16 -0600322 (void) get_device(&rbd_dev->dev);
Alex Elderf84344f2012-08-31 17:29:51 -0500323 set_device_ro(bdev, rbd_dev->mapping.read_only);
Alex Elder42382b72012-11-16 09:29:16 -0600324 rbd_dev->open_count++;
325 mutex_unlock(&ctl_mutex);
Alex Elder340c7a22012-08-10 13:12:07 -0700326
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700327 return 0;
328}
329
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800330static int rbd_release(struct gendisk *disk, fmode_t mode)
331{
332 struct rbd_device *rbd_dev = disk->private_data;
333
Alex Elder42382b72012-11-16 09:29:16 -0600334 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
335 rbd_assert(rbd_dev->open_count > 0);
336 rbd_dev->open_count--;
Alex Elderc3e946c2012-11-16 09:29:16 -0600337 put_device(&rbd_dev->dev);
Alex Elder42382b72012-11-16 09:29:16 -0600338 mutex_unlock(&ctl_mutex);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800339
340 return 0;
341}
342
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700343static const struct block_device_operations rbd_bd_ops = {
344 .owner = THIS_MODULE,
345 .open = rbd_open,
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800346 .release = rbd_release,
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700347};
348
349/*
350 * Initialize an rbd client instance.
Alex Elder43ae4702012-07-03 16:01:18 -0500351 * We own *ceph_opts.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700352 */
Alex Elderf8c38922012-08-10 13:12:07 -0700353static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700354{
355 struct rbd_client *rbdc;
356 int ret = -ENOMEM;
357
358 dout("rbd_client_create\n");
359 rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL);
360 if (!rbdc)
361 goto out_opt;
362
363 kref_init(&rbdc->kref);
364 INIT_LIST_HEAD(&rbdc->node);
365
Alex Elderbc534d82012-01-29 13:57:44 -0600366 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
367
Alex Elder43ae4702012-07-03 16:01:18 -0500368 rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700369 if (IS_ERR(rbdc->client))
Alex Elderbc534d82012-01-29 13:57:44 -0600370 goto out_mutex;
Alex Elder43ae4702012-07-03 16:01:18 -0500371 ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700372
373 ret = ceph_open_session(rbdc->client);
374 if (ret < 0)
375 goto out_err;
376
Alex Elder432b8582012-01-29 13:57:44 -0600377 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700378 list_add_tail(&rbdc->node, &rbd_client_list);
Alex Elder432b8582012-01-29 13:57:44 -0600379 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700380
Alex Elderbc534d82012-01-29 13:57:44 -0600381 mutex_unlock(&ctl_mutex);
382
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700383 dout("rbd_client_create created %p\n", rbdc);
384 return rbdc;
385
386out_err:
387 ceph_destroy_client(rbdc->client);
Alex Elderbc534d82012-01-29 13:57:44 -0600388out_mutex:
389 mutex_unlock(&ctl_mutex);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700390 kfree(rbdc);
391out_opt:
Alex Elder43ae4702012-07-03 16:01:18 -0500392 if (ceph_opts)
393 ceph_destroy_options(ceph_opts);
Vasiliy Kulikov28f259b2010-09-26 12:59:37 +0400394 return ERR_PTR(ret);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700395}
396
397/*
Alex Elder1f7ba332012-08-10 13:12:07 -0700398 * Find a ceph client with specific addr and configuration. If
399 * found, bump its reference count.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700400 */
Alex Elder1f7ba332012-08-10 13:12:07 -0700401static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700402{
403 struct rbd_client *client_node;
Alex Elder1f7ba332012-08-10 13:12:07 -0700404 bool found = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700405
Alex Elder43ae4702012-07-03 16:01:18 -0500406 if (ceph_opts->flags & CEPH_OPT_NOSHARE)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700407 return NULL;
408
Alex Elder1f7ba332012-08-10 13:12:07 -0700409 spin_lock(&rbd_client_list_lock);
410 list_for_each_entry(client_node, &rbd_client_list, node) {
411 if (!ceph_compare_options(ceph_opts, client_node->client)) {
412 kref_get(&client_node->kref);
413 found = true;
414 break;
415 }
416 }
417 spin_unlock(&rbd_client_list_lock);
418
419 return found ? client_node : NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700420}
421
422/*
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700423 * mount options
424 */
425enum {
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700426 Opt_last_int,
427 /* int args above */
428 Opt_last_string,
429 /* string args above */
Alex Eldercc0538b2012-08-10 13:12:07 -0700430 Opt_read_only,
431 Opt_read_write,
432 /* Boolean args above */
433 Opt_last_bool,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700434};
435
Alex Elder43ae4702012-07-03 16:01:18 -0500436static match_table_t rbd_opts_tokens = {
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700437 /* int args above */
438 /* string args above */
Alex Elderbe466c12012-10-22 11:31:26 -0500439 {Opt_read_only, "read_only"},
Alex Eldercc0538b2012-08-10 13:12:07 -0700440 {Opt_read_only, "ro"}, /* Alternate spelling */
441 {Opt_read_write, "read_write"},
442 {Opt_read_write, "rw"}, /* Alternate spelling */
443 /* Boolean args above */
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700444 {-1, NULL}
445};
446
447static int parse_rbd_opts_token(char *c, void *private)
448{
Alex Elder43ae4702012-07-03 16:01:18 -0500449 struct rbd_options *rbd_opts = private;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700450 substring_t argstr[MAX_OPT_ARGS];
451 int token, intval, ret;
452
Alex Elder43ae4702012-07-03 16:01:18 -0500453 token = match_token(c, rbd_opts_tokens, argstr);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700454 if (token < 0)
455 return -EINVAL;
456
457 if (token < Opt_last_int) {
458 ret = match_int(&argstr[0], &intval);
459 if (ret < 0) {
460 pr_err("bad mount option arg (not int) "
461 "at '%s'\n", c);
462 return ret;
463 }
464 dout("got int token %d val %d\n", token, intval);
465 } else if (token > Opt_last_int && token < Opt_last_string) {
466 dout("got string token %d val %s\n", token,
467 argstr[0].from);
Alex Eldercc0538b2012-08-10 13:12:07 -0700468 } else if (token > Opt_last_string && token < Opt_last_bool) {
469 dout("got Boolean token %d\n", token);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700470 } else {
471 dout("got token %d\n", token);
472 }
473
474 switch (token) {
Alex Eldercc0538b2012-08-10 13:12:07 -0700475 case Opt_read_only:
476 rbd_opts->read_only = true;
477 break;
478 case Opt_read_write:
479 rbd_opts->read_only = false;
480 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700481 default:
Alex Elderaafb2302012-09-06 16:00:54 -0500482 rbd_assert(false);
483 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700484 }
485 return 0;
486}
487
488/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700489 * Get a ceph client with specific addr and configuration, if one does
490 * not exist create it.
491 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500492static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700493{
Alex Elderf8c38922012-08-10 13:12:07 -0700494 struct rbd_client *rbdc;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700495
Alex Elder1f7ba332012-08-10 13:12:07 -0700496 rbdc = rbd_client_find(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500497 if (rbdc) /* using an existing client */
Alex Elder43ae4702012-07-03 16:01:18 -0500498 ceph_destroy_options(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500499 else
Alex Elderf8c38922012-08-10 13:12:07 -0700500 rbdc = rbd_client_create(ceph_opts);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700501
Alex Elder9d3997f2012-10-25 23:34:42 -0500502 return rbdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700503}
504
505/*
506 * Destroy ceph client
Alex Elderd23a4b32012-01-29 13:57:43 -0600507 *
Alex Elder432b8582012-01-29 13:57:44 -0600508 * Caller must hold rbd_client_list_lock.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700509 */
510static void rbd_client_release(struct kref *kref)
511{
512 struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
513
514 dout("rbd_release_client %p\n", rbdc);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500515 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700516 list_del(&rbdc->node);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500517 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700518
519 ceph_destroy_client(rbdc->client);
520 kfree(rbdc);
521}
522
523/*
524 * Drop reference to ceph client node. If it's not referenced anymore, release
525 * it.
526 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500527static void rbd_put_client(struct rbd_client *rbdc)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700528{
Alex Elderc53d5892012-10-25 23:34:42 -0500529 if (rbdc)
530 kref_put(&rbdc->kref, rbd_client_release);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700531}
532
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700533/*
534 * Destroy requests collection
535 */
536static void rbd_coll_release(struct kref *kref)
537{
538 struct rbd_req_coll *coll =
539 container_of(kref, struct rbd_req_coll, kref);
540
541 dout("rbd_coll_release %p\n", coll);
542 kfree(coll);
543}
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700544
Alex Eldera30b71b2012-07-10 20:30:11 -0500545static bool rbd_image_format_valid(u32 image_format)
546{
547 return image_format == 1 || image_format == 2;
548}
549
Alex Elder8e94af82012-07-25 09:32:40 -0500550static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
551{
Alex Elder103a1502012-08-02 11:29:45 -0500552 size_t size;
553 u32 snap_count;
554
555 /* The header has to start with the magic rbd header text */
556 if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT)))
557 return false;
558
Alex Elderdb2388b2012-10-20 22:17:27 -0500559 /* The bio layer requires at least sector-sized I/O */
560
561 if (ondisk->options.order < SECTOR_SHIFT)
562 return false;
563
564 /* If we use u64 in a few spots we may be able to loosen this */
565
566 if (ondisk->options.order > 8 * sizeof (int) - 1)
567 return false;
568
Alex Elder103a1502012-08-02 11:29:45 -0500569 /*
570 * The size of a snapshot header has to fit in a size_t, and
571 * that limits the number of snapshots.
572 */
573 snap_count = le32_to_cpu(ondisk->snap_count);
574 size = SIZE_MAX - sizeof (struct ceph_snap_context);
575 if (snap_count > size / sizeof (__le64))
576 return false;
577
578 /*
579 * Not only that, but the size of the entire the snapshot
580 * header must also be representable in a size_t.
581 */
582 size -= snap_count * sizeof (__le64);
583 if ((u64) size < le64_to_cpu(ondisk->snap_names_len))
584 return false;
585
586 return true;
Alex Elder8e94af82012-07-25 09:32:40 -0500587}
588
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700589/*
590 * Create a new header structure, translate header format from the on-disk
591 * header.
592 */
593static int rbd_header_from_disk(struct rbd_image_header *header,
Alex Elder4156d992012-08-02 11:29:46 -0500594 struct rbd_image_header_ondisk *ondisk)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700595{
Alex Elderccece232012-07-10 20:30:10 -0500596 u32 snap_count;
Alex Elder58c17b02012-08-23 23:22:06 -0500597 size_t len;
Alex Elderd2bb24e2012-07-26 23:37:14 -0500598 size_t size;
Alex Elder621901d2012-08-23 23:22:06 -0500599 u32 i;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700600
Alex Elder6a523252012-07-19 17:12:59 -0500601 memset(header, 0, sizeof (*header));
602
Alex Elder103a1502012-08-02 11:29:45 -0500603 snap_count = le32_to_cpu(ondisk->snap_count);
604
Alex Elder58c17b02012-08-23 23:22:06 -0500605 len = strnlen(ondisk->object_prefix, sizeof (ondisk->object_prefix));
606 header->object_prefix = kmalloc(len + 1, GFP_KERNEL);
Alex Elder6a523252012-07-19 17:12:59 -0500607 if (!header->object_prefix)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700608 return -ENOMEM;
Alex Elder58c17b02012-08-23 23:22:06 -0500609 memcpy(header->object_prefix, ondisk->object_prefix, len);
610 header->object_prefix[len] = '\0';
Alex Elder00f1f362012-02-07 12:03:36 -0600611
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700612 if (snap_count) {
Alex Elderf785cc12012-08-23 23:22:06 -0500613 u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len);
614
Alex Elder621901d2012-08-23 23:22:06 -0500615 /* Save a copy of the snapshot names */
616
Alex Elderf785cc12012-08-23 23:22:06 -0500617 if (snap_names_len > (u64) SIZE_MAX)
618 return -EIO;
619 header->snap_names = kmalloc(snap_names_len, GFP_KERNEL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700620 if (!header->snap_names)
Alex Elder6a523252012-07-19 17:12:59 -0500621 goto out_err;
Alex Elderf785cc12012-08-23 23:22:06 -0500622 /*
623 * Note that rbd_dev_v1_header_read() guarantees
624 * the ondisk buffer we're working with has
625 * snap_names_len bytes beyond the end of the
626 * snapshot id array, this memcpy() is safe.
627 */
628 memcpy(header->snap_names, &ondisk->snaps[snap_count],
629 snap_names_len);
Alex Elder6a523252012-07-19 17:12:59 -0500630
Alex Elder621901d2012-08-23 23:22:06 -0500631 /* Record each snapshot's size */
632
Alex Elderd2bb24e2012-07-26 23:37:14 -0500633 size = snap_count * sizeof (*header->snap_sizes);
634 header->snap_sizes = kmalloc(size, GFP_KERNEL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700635 if (!header->snap_sizes)
Alex Elder6a523252012-07-19 17:12:59 -0500636 goto out_err;
Alex Elder621901d2012-08-23 23:22:06 -0500637 for (i = 0; i < snap_count; i++)
638 header->snap_sizes[i] =
639 le64_to_cpu(ondisk->snaps[i].image_size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700640 } else {
Alex Elderccece232012-07-10 20:30:10 -0500641 WARN_ON(ondisk->snap_names_len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700642 header->snap_names = NULL;
643 header->snap_sizes = NULL;
644 }
Alex Elder849b4262012-07-09 21:04:24 -0500645
Alex Elder34b13182012-07-13 20:35:12 -0500646 header->features = 0; /* No features support in v1 images */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700647 header->obj_order = ondisk->options.order;
648 header->crypt_type = ondisk->options.crypt_type;
649 header->comp_type = ondisk->options.comp_type;
Alex Elder6a523252012-07-19 17:12:59 -0500650
Alex Elder621901d2012-08-23 23:22:06 -0500651 /* Allocate and fill in the snapshot context */
652
Alex Elderf84344f2012-08-31 17:29:51 -0500653 header->image_size = le64_to_cpu(ondisk->image_size);
Alex Elder6a523252012-07-19 17:12:59 -0500654 size = sizeof (struct ceph_snap_context);
655 size += snap_count * sizeof (header->snapc->snaps[0]);
656 header->snapc = kzalloc(size, GFP_KERNEL);
657 if (!header->snapc)
658 goto out_err;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700659
660 atomic_set(&header->snapc->nref, 1);
Alex Elder505cbb92012-07-19 08:49:18 -0500661 header->snapc->seq = le64_to_cpu(ondisk->snap_seq);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700662 header->snapc->num_snaps = snap_count;
Alex Elder621901d2012-08-23 23:22:06 -0500663 for (i = 0; i < snap_count; i++)
664 header->snapc->snaps[i] =
665 le64_to_cpu(ondisk->snaps[i].id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700666
667 return 0;
668
Alex Elder6a523252012-07-19 17:12:59 -0500669out_err:
Alex Elder849b4262012-07-09 21:04:24 -0500670 kfree(header->snap_sizes);
Alex Elderccece232012-07-10 20:30:10 -0500671 header->snap_sizes = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700672 kfree(header->snap_names);
Alex Elderccece232012-07-10 20:30:10 -0500673 header->snap_names = NULL;
Alex Elder6a523252012-07-19 17:12:59 -0500674 kfree(header->object_prefix);
675 header->object_prefix = NULL;
Alex Elderccece232012-07-10 20:30:10 -0500676
Alex Elder00f1f362012-02-07 12:03:36 -0600677 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700678}
679
Alex Elder9e15b772012-10-30 19:40:33 -0500680static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id)
681{
682 struct rbd_snap *snap;
683
684 if (snap_id == CEPH_NOSNAP)
685 return RBD_SNAP_HEAD_NAME;
686
687 list_for_each_entry(snap, &rbd_dev->snaps, node)
688 if (snap_id == snap->id)
689 return snap->name;
690
691 return NULL;
692}
693
Alex Elder8836b992012-08-30 14:42:15 -0500694static int snap_by_name(struct rbd_device *rbd_dev, const char *snap_name)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700695{
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700696
Alex Eldere86924a2012-07-10 20:30:11 -0500697 struct rbd_snap *snap;
Alex Elder00f1f362012-02-07 12:03:36 -0600698
Alex Eldere86924a2012-07-10 20:30:11 -0500699 list_for_each_entry(snap, &rbd_dev->snaps, node) {
700 if (!strcmp(snap_name, snap->name)) {
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500701 rbd_dev->spec->snap_id = snap->id;
Alex Eldere86924a2012-07-10 20:30:11 -0500702 rbd_dev->mapping.size = snap->size;
Alex Elder34b13182012-07-13 20:35:12 -0500703 rbd_dev->mapping.features = snap->features;
Alex Elder00f1f362012-02-07 12:03:36 -0600704
Alex Eldere86924a2012-07-10 20:30:11 -0500705 return 0;
Alex Elder00f1f362012-02-07 12:03:36 -0600706 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700707 }
Alex Eldere86924a2012-07-10 20:30:11 -0500708
Alex Elder00f1f362012-02-07 12:03:36 -0600709 return -ENOENT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700710}
711
Alex Elder819d52b2012-10-25 23:34:41 -0500712static int rbd_dev_set_mapping(struct rbd_device *rbd_dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700713{
Alex Elder78dc4472012-07-19 08:49:18 -0500714 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700715
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500716 if (!memcmp(rbd_dev->spec->snap_name, RBD_SNAP_HEAD_NAME,
Josh Durgincc9d7342011-11-21 18:19:13 -0800717 sizeof (RBD_SNAP_HEAD_NAME))) {
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500718 rbd_dev->spec->snap_id = CEPH_NOSNAP;
Alex Elder99c1f082012-08-30 14:42:15 -0500719 rbd_dev->mapping.size = rbd_dev->header.image_size;
Alex Elder34b13182012-07-13 20:35:12 -0500720 rbd_dev->mapping.features = rbd_dev->header.features;
Alex Eldere86924a2012-07-10 20:30:11 -0500721 ret = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700722 } else {
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500723 ret = snap_by_name(rbd_dev, rbd_dev->spec->snap_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700724 if (ret < 0)
725 goto done;
Alex Elderf84344f2012-08-31 17:29:51 -0500726 rbd_dev->mapping.read_only = true;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700727 }
Alex Elderdaba5fd2012-10-26 17:25:23 -0500728 rbd_dev->exists = true;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700729done:
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700730 return ret;
731}
732
733static void rbd_header_free(struct rbd_image_header *header)
734{
Alex Elder849b4262012-07-09 21:04:24 -0500735 kfree(header->object_prefix);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500736 header->object_prefix = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700737 kfree(header->snap_sizes);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500738 header->snap_sizes = NULL;
Alex Elder849b4262012-07-09 21:04:24 -0500739 kfree(header->snap_names);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500740 header->snap_names = NULL;
Josh Durgind1d25642011-12-05 14:03:05 -0800741 ceph_put_snap_context(header->snapc);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500742 header->snapc = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700743}
744
Alex Elder65ccfe22012-08-09 10:33:26 -0700745static char *rbd_segment_name(struct rbd_device *rbd_dev, u64 offset)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700746{
Alex Elder65ccfe22012-08-09 10:33:26 -0700747 char *name;
748 u64 segment;
749 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700750
Alex Elder2fd82b92012-11-09 15:05:54 -0600751 name = kmalloc(MAX_OBJ_NAME_SIZE + 1, GFP_NOIO);
Alex Elder65ccfe22012-08-09 10:33:26 -0700752 if (!name)
753 return NULL;
754 segment = offset >> rbd_dev->header.obj_order;
Alex Elder2fd82b92012-11-09 15:05:54 -0600755 ret = snprintf(name, MAX_OBJ_NAME_SIZE + 1, "%s.%012llx",
Alex Elder65ccfe22012-08-09 10:33:26 -0700756 rbd_dev->header.object_prefix, segment);
Alex Elder2fd82b92012-11-09 15:05:54 -0600757 if (ret < 0 || ret > MAX_OBJ_NAME_SIZE) {
Alex Elder65ccfe22012-08-09 10:33:26 -0700758 pr_err("error formatting segment name for #%llu (%d)\n",
759 segment, ret);
760 kfree(name);
761 name = NULL;
762 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700763
Alex Elder65ccfe22012-08-09 10:33:26 -0700764 return name;
765}
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700766
Alex Elder65ccfe22012-08-09 10:33:26 -0700767static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset)
768{
769 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700770
Alex Elder65ccfe22012-08-09 10:33:26 -0700771 return offset & (segment_size - 1);
772}
773
774static u64 rbd_segment_length(struct rbd_device *rbd_dev,
775 u64 offset, u64 length)
776{
777 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
778
779 offset &= segment_size - 1;
780
Alex Elderaafb2302012-09-06 16:00:54 -0500781 rbd_assert(length <= U64_MAX - offset);
Alex Elder65ccfe22012-08-09 10:33:26 -0700782 if (offset + length > segment_size)
783 length = segment_size - offset;
784
785 return length;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700786}
787
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700788static int rbd_get_num_segments(struct rbd_image_header *header,
789 u64 ofs, u64 len)
790{
Alex Elderdf111be2012-08-09 10:33:26 -0700791 u64 start_seg;
792 u64 end_seg;
793
794 if (!len)
795 return 0;
796 if (len - 1 > U64_MAX - ofs)
797 return -ERANGE;
798
799 start_seg = ofs >> header->obj_order;
800 end_seg = (ofs + len - 1) >> header->obj_order;
801
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700802 return end_seg - start_seg + 1;
803}
804
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700805/*
Josh Durgin029bcbd2011-07-22 11:35:23 -0700806 * returns the size of an object in the image
807 */
808static u64 rbd_obj_bytes(struct rbd_image_header *header)
809{
810 return 1 << header->obj_order;
811}
812
813/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700814 * bio helpers
815 */
816
817static void bio_chain_put(struct bio *chain)
818{
819 struct bio *tmp;
820
821 while (chain) {
822 tmp = chain;
823 chain = chain->bi_next;
824 bio_put(tmp);
825 }
826}
827
828/*
829 * zeros a bio chain, starting at specific offset
830 */
831static void zero_bio_chain(struct bio *chain, int start_ofs)
832{
833 struct bio_vec *bv;
834 unsigned long flags;
835 void *buf;
836 int i;
837 int pos = 0;
838
839 while (chain) {
840 bio_for_each_segment(bv, chain, i) {
841 if (pos + bv->bv_len > start_ofs) {
842 int remainder = max(start_ofs - pos, 0);
843 buf = bvec_kmap_irq(bv, &flags);
844 memset(buf + remainder, 0,
845 bv->bv_len - remainder);
Dan Carpenter85b5aaa2010-10-11 21:15:11 +0200846 bvec_kunmap_irq(buf, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700847 }
848 pos += bv->bv_len;
849 }
850
851 chain = chain->bi_next;
852 }
853}
854
855/*
Alex Elderf7760da2012-10-20 22:17:27 -0500856 * Clone a portion of a bio, starting at the given byte offset
857 * and continuing for the number of bytes indicated.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700858 */
Alex Elderf7760da2012-10-20 22:17:27 -0500859static struct bio *bio_clone_range(struct bio *bio_src,
860 unsigned int offset,
861 unsigned int len,
862 gfp_t gfpmask)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700863{
Alex Elderf7760da2012-10-20 22:17:27 -0500864 struct bio_vec *bv;
865 unsigned int resid;
866 unsigned short idx;
867 unsigned int voff;
868 unsigned short end_idx;
869 unsigned short vcnt;
870 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700871
Alex Elderf7760da2012-10-20 22:17:27 -0500872 /* Handle the easy case for the caller */
873
874 if (!offset && len == bio_src->bi_size)
875 return bio_clone(bio_src, gfpmask);
876
877 if (WARN_ON_ONCE(!len))
878 return NULL;
879 if (WARN_ON_ONCE(len > bio_src->bi_size))
880 return NULL;
881 if (WARN_ON_ONCE(offset > bio_src->bi_size - len))
882 return NULL;
883
884 /* Find first affected segment... */
885
886 resid = offset;
887 __bio_for_each_segment(bv, bio_src, idx, 0) {
888 if (resid < bv->bv_len)
889 break;
890 resid -= bv->bv_len;
891 }
892 voff = resid;
893
894 /* ...and the last affected segment */
895
896 resid += len;
897 __bio_for_each_segment(bv, bio_src, end_idx, idx) {
898 if (resid <= bv->bv_len)
899 break;
900 resid -= bv->bv_len;
901 }
902 vcnt = end_idx - idx + 1;
903
904 /* Build the clone */
905
906 bio = bio_alloc(gfpmask, (unsigned int) vcnt);
907 if (!bio)
908 return NULL; /* ENOMEM */
909
910 bio->bi_bdev = bio_src->bi_bdev;
911 bio->bi_sector = bio_src->bi_sector + (offset >> SECTOR_SHIFT);
912 bio->bi_rw = bio_src->bi_rw;
913 bio->bi_flags |= 1 << BIO_CLONED;
914
915 /*
916 * Copy over our part of the bio_vec, then update the first
917 * and last (or only) entries.
918 */
919 memcpy(&bio->bi_io_vec[0], &bio_src->bi_io_vec[idx],
920 vcnt * sizeof (struct bio_vec));
921 bio->bi_io_vec[0].bv_offset += voff;
922 if (vcnt > 1) {
923 bio->bi_io_vec[0].bv_len -= voff;
924 bio->bi_io_vec[vcnt - 1].bv_len = resid;
925 } else {
926 bio->bi_io_vec[0].bv_len = len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700927 }
928
Alex Elderf7760da2012-10-20 22:17:27 -0500929 bio->bi_vcnt = vcnt;
930 bio->bi_size = len;
931 bio->bi_idx = 0;
Alex Elder542582f2012-08-09 10:33:25 -0700932
Alex Elderf7760da2012-10-20 22:17:27 -0500933 return bio;
934}
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700935
Alex Elderf7760da2012-10-20 22:17:27 -0500936/*
937 * Clone a portion of a bio chain, starting at the given byte offset
938 * into the first bio in the source chain and continuing for the
939 * number of bytes indicated. The result is another bio chain of
940 * exactly the given length, or a null pointer on error.
941 *
942 * The bio_src and offset parameters are both in-out. On entry they
943 * refer to the first source bio and the offset into that bio where
944 * the start of data to be cloned is located.
945 *
946 * On return, bio_src is updated to refer to the bio in the source
947 * chain that contains first un-cloned byte, and *offset will
948 * contain the offset of that byte within that bio.
949 */
950static struct bio *bio_chain_clone_range(struct bio **bio_src,
951 unsigned int *offset,
952 unsigned int len,
953 gfp_t gfpmask)
954{
955 struct bio *bi = *bio_src;
956 unsigned int off = *offset;
957 struct bio *chain = NULL;
958 struct bio **end;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700959
Alex Elderf7760da2012-10-20 22:17:27 -0500960 /* Build up a chain of clone bios up to the limit */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700961
Alex Elderf7760da2012-10-20 22:17:27 -0500962 if (!bi || off >= bi->bi_size || !len)
963 return NULL; /* Nothing to clone */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700964
Alex Elderf7760da2012-10-20 22:17:27 -0500965 end = &chain;
966 while (len) {
967 unsigned int bi_size;
968 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700969
Alex Elderf7760da2012-10-20 22:17:27 -0500970 if (!bi)
971 goto out_err; /* EINVAL; ran out of bio's */
972 bi_size = min_t(unsigned int, bi->bi_size - off, len);
973 bio = bio_clone_range(bi, off, bi_size, gfpmask);
974 if (!bio)
975 goto out_err; /* ENOMEM */
976
977 *end = bio;
978 end = &bio->bi_next;
979
980 off += bi_size;
981 if (off == bi->bi_size) {
982 bi = bi->bi_next;
983 off = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700984 }
Alex Elderf7760da2012-10-20 22:17:27 -0500985 len -= bi_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700986 }
Alex Elderf7760da2012-10-20 22:17:27 -0500987 *bio_src = bi;
988 *offset = off;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700989
Alex Elderf7760da2012-10-20 22:17:27 -0500990 return chain;
991out_err:
992 bio_chain_put(chain);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700993
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700994 return NULL;
995}
996
997/*
998 * helpers for osd request op vectors.
999 */
Alex Elder57cfc102012-06-26 12:57:03 -07001000static struct ceph_osd_req_op *rbd_create_rw_ops(int num_ops,
1001 int opcode, u32 payload_len)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001002{
Alex Elder57cfc102012-06-26 12:57:03 -07001003 struct ceph_osd_req_op *ops;
1004
1005 ops = kzalloc(sizeof (*ops) * (num_ops + 1), GFP_NOIO);
1006 if (!ops)
1007 return NULL;
1008
1009 ops[0].op = opcode;
1010
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001011 /*
1012 * op extent offset and length will be set later on
1013 * in calc_raw_layout()
1014 */
Alex Elder57cfc102012-06-26 12:57:03 -07001015 ops[0].payload_len = payload_len;
1016
1017 return ops;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001018}
1019
1020static void rbd_destroy_ops(struct ceph_osd_req_op *ops)
1021{
1022 kfree(ops);
1023}
1024
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001025static void rbd_coll_end_req_index(struct request *rq,
1026 struct rbd_req_coll *coll,
1027 int index,
1028 int ret, u64 len)
1029{
1030 struct request_queue *q;
1031 int min, max, i;
1032
Alex Elderbd919d42012-07-13 20:35:11 -05001033 dout("rbd_coll_end_req_index %p index %d ret %d len %llu\n",
1034 coll, index, ret, (unsigned long long) len);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001035
1036 if (!rq)
1037 return;
1038
1039 if (!coll) {
1040 blk_end_request(rq, ret, len);
1041 return;
1042 }
1043
1044 q = rq->q;
1045
1046 spin_lock_irq(q->queue_lock);
1047 coll->status[index].done = 1;
1048 coll->status[index].rc = ret;
1049 coll->status[index].bytes = len;
1050 max = min = coll->num_done;
1051 while (max < coll->total && coll->status[max].done)
1052 max++;
1053
1054 for (i = min; i<max; i++) {
1055 __blk_end_request(rq, coll->status[i].rc,
1056 coll->status[i].bytes);
1057 coll->num_done++;
1058 kref_put(&coll->kref, rbd_coll_release);
1059 }
1060 spin_unlock_irq(q->queue_lock);
1061}
1062
1063static void rbd_coll_end_req(struct rbd_request *req,
1064 int ret, u64 len)
1065{
1066 rbd_coll_end_req_index(req->rq, req->coll, req->coll_index, ret, len);
1067}
1068
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001069/*
1070 * Send ceph osd request
1071 */
1072static int rbd_do_request(struct request *rq,
Alex Elder0ce1a792012-07-03 16:01:18 -05001073 struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001074 struct ceph_snap_context *snapc,
1075 u64 snapid,
Alex Elderaded07e2012-07-03 16:01:18 -05001076 const char *object_name, u64 ofs, u64 len,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001077 struct bio *bio,
1078 struct page **pages,
1079 int num_pages,
1080 int flags,
1081 struct ceph_osd_req_op *ops,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001082 struct rbd_req_coll *coll,
1083 int coll_index,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001084 void (*rbd_cb)(struct ceph_osd_request *req,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001085 struct ceph_msg *msg),
1086 struct ceph_osd_request **linger_req,
1087 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001088{
1089 struct ceph_osd_request *req;
1090 struct ceph_file_layout *layout;
1091 int ret;
1092 u64 bno;
1093 struct timespec mtime = CURRENT_TIME;
1094 struct rbd_request *req_data;
1095 struct ceph_osd_request_head *reqhead;
Alex Elder1dbb4392012-01-24 10:08:37 -06001096 struct ceph_osd_client *osdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001097
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001098 req_data = kzalloc(sizeof(*req_data), GFP_NOIO);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001099 if (!req_data) {
1100 if (coll)
1101 rbd_coll_end_req_index(rq, coll, coll_index,
1102 -ENOMEM, len);
1103 return -ENOMEM;
1104 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001105
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001106 if (coll) {
1107 req_data->coll = coll;
1108 req_data->coll_index = coll_index;
1109 }
1110
Alex Elderf7760da2012-10-20 22:17:27 -05001111 dout("rbd_do_request object_name=%s ofs=%llu len=%llu coll=%p[%d]\n",
1112 object_name, (unsigned long long) ofs,
1113 (unsigned long long) len, coll, coll_index);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001114
Alex Elder0ce1a792012-07-03 16:01:18 -05001115 osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder1dbb4392012-01-24 10:08:37 -06001116 req = ceph_osdc_alloc_request(osdc, flags, snapc, ops,
1117 false, GFP_NOIO, pages, bio);
Sage Weil4ad12622011-05-03 09:23:36 -07001118 if (!req) {
Sage Weil4ad12622011-05-03 09:23:36 -07001119 ret = -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001120 goto done_pages;
1121 }
1122
1123 req->r_callback = rbd_cb;
1124
1125 req_data->rq = rq;
1126 req_data->bio = bio;
1127 req_data->pages = pages;
1128 req_data->len = len;
1129
1130 req->r_priv = req_data;
1131
1132 reqhead = req->r_request->front.iov_base;
1133 reqhead->snapid = cpu_to_le64(CEPH_NOSNAP);
1134
Alex Elderaded07e2012-07-03 16:01:18 -05001135 strncpy(req->r_oid, object_name, sizeof(req->r_oid));
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001136 req->r_oid_len = strlen(req->r_oid);
1137
1138 layout = &req->r_file_layout;
1139 memset(layout, 0, sizeof(*layout));
1140 layout->fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
1141 layout->fl_stripe_count = cpu_to_le32(1);
1142 layout->fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
Alex Elder0d7dbfc2012-10-25 23:34:41 -05001143 layout->fl_pg_pool = cpu_to_le32((int) rbd_dev->spec->pool_id);
Sage Weil6cae3712012-09-24 21:02:47 -07001144 ret = ceph_calc_raw_layout(osdc, layout, snapid, ofs, &len, &bno,
1145 req, ops);
1146 rbd_assert(ret == 0);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001147
1148 ceph_osdc_build_request(req, ofs, &len,
1149 ops,
1150 snapc,
1151 &mtime,
1152 req->r_oid, req->r_oid_len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001153
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001154 if (linger_req) {
Alex Elder1dbb4392012-01-24 10:08:37 -06001155 ceph_osdc_set_request_linger(osdc, req);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001156 *linger_req = req;
1157 }
1158
Alex Elder1dbb4392012-01-24 10:08:37 -06001159 ret = ceph_osdc_start_request(osdc, req, false);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001160 if (ret < 0)
1161 goto done_err;
1162
1163 if (!rbd_cb) {
Alex Elder1dbb4392012-01-24 10:08:37 -06001164 ret = ceph_osdc_wait_request(osdc, req);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001165 if (ver)
1166 *ver = le64_to_cpu(req->r_reassert_version.version);
Alex Elderbd919d42012-07-13 20:35:11 -05001167 dout("reassert_ver=%llu\n",
1168 (unsigned long long)
1169 le64_to_cpu(req->r_reassert_version.version));
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001170 ceph_osdc_put_request(req);
1171 }
1172 return ret;
1173
1174done_err:
1175 bio_chain_put(req_data->bio);
1176 ceph_osdc_put_request(req);
1177done_pages:
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001178 rbd_coll_end_req(req_data, ret, len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001179 kfree(req_data);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001180 return ret;
1181}
1182
1183/*
1184 * Ceph osd op callback
1185 */
1186static void rbd_req_cb(struct ceph_osd_request *req, struct ceph_msg *msg)
1187{
1188 struct rbd_request *req_data = req->r_priv;
1189 struct ceph_osd_reply_head *replyhead;
1190 struct ceph_osd_op *op;
1191 __s32 rc;
1192 u64 bytes;
1193 int read_op;
1194
1195 /* parse reply */
1196 replyhead = msg->front.iov_base;
1197 WARN_ON(le32_to_cpu(replyhead->num_ops) == 0);
1198 op = (void *)(replyhead + 1);
1199 rc = le32_to_cpu(replyhead->result);
1200 bytes = le64_to_cpu(op->extent.length);
Dan Carpenter895cfcc2012-06-06 09:15:33 -05001201 read_op = (le16_to_cpu(op->op) == CEPH_OSD_OP_READ);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001202
Alex Elderbd919d42012-07-13 20:35:11 -05001203 dout("rbd_req_cb bytes=%llu readop=%d rc=%d\n",
1204 (unsigned long long) bytes, read_op, (int) rc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001205
1206 if (rc == -ENOENT && read_op) {
1207 zero_bio_chain(req_data->bio, 0);
1208 rc = 0;
1209 } else if (rc == 0 && read_op && bytes < req_data->len) {
1210 zero_bio_chain(req_data->bio, bytes);
1211 bytes = req_data->len;
1212 }
1213
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001214 rbd_coll_end_req(req_data, rc, bytes);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001215
1216 if (req_data->bio)
1217 bio_chain_put(req_data->bio);
1218
1219 ceph_osdc_put_request(req);
1220 kfree(req_data);
1221}
1222
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001223static void rbd_simple_req_cb(struct ceph_osd_request *req, struct ceph_msg *msg)
1224{
1225 ceph_osdc_put_request(req);
1226}
1227
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001228/*
1229 * Do a synchronous ceph osd operation
1230 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001231static int rbd_req_sync_op(struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001232 struct ceph_snap_context *snapc,
1233 u64 snapid,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001234 int flags,
Alex Elder913d2fd2012-06-26 12:57:03 -07001235 struct ceph_osd_req_op *ops,
Alex Elderaded07e2012-07-03 16:01:18 -05001236 const char *object_name,
Alex Elderf8d4de62012-07-03 16:01:19 -05001237 u64 ofs, u64 inbound_size,
1238 char *inbound,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001239 struct ceph_osd_request **linger_req,
1240 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001241{
1242 int ret;
1243 struct page **pages;
1244 int num_pages;
Alex Elder913d2fd2012-06-26 12:57:03 -07001245
Alex Elderaafb2302012-09-06 16:00:54 -05001246 rbd_assert(ops != NULL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001247
Alex Elderf8d4de62012-07-03 16:01:19 -05001248 num_pages = calc_pages_for(ofs, inbound_size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001249 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
Dan Carpenterb8d06382010-10-11 21:14:23 +02001250 if (IS_ERR(pages))
1251 return PTR_ERR(pages);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001252
Alex Elder0ce1a792012-07-03 16:01:18 -05001253 ret = rbd_do_request(NULL, rbd_dev, snapc, snapid,
Alex Elderf8d4de62012-07-03 16:01:19 -05001254 object_name, ofs, inbound_size, NULL,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001255 pages, num_pages,
1256 flags,
1257 ops,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001258 NULL, 0,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001259 NULL,
1260 linger_req, ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001261 if (ret < 0)
Alex Elder913d2fd2012-06-26 12:57:03 -07001262 goto done;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001263
Alex Elderf8d4de62012-07-03 16:01:19 -05001264 if ((flags & CEPH_OSD_FLAG_READ) && inbound)
1265 ret = ceph_copy_from_page_vector(pages, inbound, ofs, ret);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001266
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001267done:
1268 ceph_release_page_vector(pages, num_pages);
1269 return ret;
1270}
1271
1272/*
1273 * Do an asynchronous ceph osd operation
1274 */
1275static int rbd_do_op(struct request *rq,
Alex Elder0ce1a792012-07-03 16:01:18 -05001276 struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001277 struct ceph_snap_context *snapc,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001278 u64 ofs, u64 len,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001279 struct bio *bio,
1280 struct rbd_req_coll *coll,
1281 int coll_index)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001282{
1283 char *seg_name;
1284 u64 seg_ofs;
1285 u64 seg_len;
1286 int ret;
1287 struct ceph_osd_req_op *ops;
1288 u32 payload_len;
Alex Elderff2e4bb2012-10-10 18:59:29 -07001289 int opcode;
1290 int flags;
Alex Elder46342462012-10-10 18:59:29 -07001291 u64 snapid;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001292
Alex Elder65ccfe22012-08-09 10:33:26 -07001293 seg_name = rbd_segment_name(rbd_dev, ofs);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001294 if (!seg_name)
1295 return -ENOMEM;
Alex Elder65ccfe22012-08-09 10:33:26 -07001296 seg_len = rbd_segment_length(rbd_dev, ofs, len);
1297 seg_ofs = rbd_segment_offset(rbd_dev, ofs);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001298
Alex Elderff2e4bb2012-10-10 18:59:29 -07001299 if (rq_data_dir(rq) == WRITE) {
1300 opcode = CEPH_OSD_OP_WRITE;
1301 flags = CEPH_OSD_FLAG_WRITE|CEPH_OSD_FLAG_ONDISK;
Alex Elder46342462012-10-10 18:59:29 -07001302 snapid = CEPH_NOSNAP;
Alex Elderff2e4bb2012-10-10 18:59:29 -07001303 payload_len = seg_len;
1304 } else {
1305 opcode = CEPH_OSD_OP_READ;
1306 flags = CEPH_OSD_FLAG_READ;
Alex Elder46342462012-10-10 18:59:29 -07001307 snapc = NULL;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05001308 snapid = rbd_dev->spec->snap_id;
Alex Elderff2e4bb2012-10-10 18:59:29 -07001309 payload_len = 0;
1310 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001311
Alex Elder57cfc102012-06-26 12:57:03 -07001312 ret = -ENOMEM;
1313 ops = rbd_create_rw_ops(1, opcode, payload_len);
1314 if (!ops)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001315 goto done;
1316
1317 /* we've taken care of segment sizes earlier when we
1318 cloned the bios. We should never have a segment
1319 truncated at this point */
Alex Elderaafb2302012-09-06 16:00:54 -05001320 rbd_assert(seg_len == len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001321
1322 ret = rbd_do_request(rq, rbd_dev, snapc, snapid,
1323 seg_name, seg_ofs, seg_len,
1324 bio,
1325 NULL, 0,
1326 flags,
1327 ops,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001328 coll, coll_index,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001329 rbd_req_cb, 0, NULL);
Sage Weil11f77002011-05-12 16:13:54 -07001330
1331 rbd_destroy_ops(ops);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001332done:
1333 kfree(seg_name);
1334 return ret;
1335}
1336
1337/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001338 * Request sync osd read
1339 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001340static int rbd_req_sync_read(struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001341 u64 snapid,
Alex Elderaded07e2012-07-03 16:01:18 -05001342 const char *object_name,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001343 u64 ofs, u64 len,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001344 char *buf,
1345 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001346{
Alex Elder913d2fd2012-06-26 12:57:03 -07001347 struct ceph_osd_req_op *ops;
1348 int ret;
1349
1350 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_READ, 0);
1351 if (!ops)
1352 return -ENOMEM;
1353
1354 ret = rbd_req_sync_op(rbd_dev, NULL,
Josh Durginb06e6a62011-11-21 18:16:52 -08001355 snapid,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001356 CEPH_OSD_FLAG_READ,
Alex Elder913d2fd2012-06-26 12:57:03 -07001357 ops, object_name, ofs, len, buf, NULL, ver);
1358 rbd_destroy_ops(ops);
1359
1360 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001361}
1362
1363/*
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001364 * Request sync osd watch
1365 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001366static int rbd_req_sync_notify_ack(struct rbd_device *rbd_dev,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001367 u64 ver,
Alex Elder7f0a24d2012-07-25 09:32:40 -05001368 u64 notify_id)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001369{
1370 struct ceph_osd_req_op *ops;
Sage Weil11f77002011-05-12 16:13:54 -07001371 int ret;
1372
Alex Elder57cfc102012-06-26 12:57:03 -07001373 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_NOTIFY_ACK, 0);
1374 if (!ops)
1375 return -ENOMEM;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001376
Josh Durgina71b8912011-12-05 18:10:44 -08001377 ops[0].watch.ver = cpu_to_le64(ver);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001378 ops[0].watch.cookie = notify_id;
1379 ops[0].watch.flag = 0;
1380
Alex Elder0ce1a792012-07-03 16:01:18 -05001381 ret = rbd_do_request(NULL, rbd_dev, NULL, CEPH_NOSNAP,
Alex Elder7f0a24d2012-07-25 09:32:40 -05001382 rbd_dev->header_name, 0, 0, NULL,
Alex Elderad4f2322012-07-03 16:01:19 -05001383 NULL, 0,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001384 CEPH_OSD_FLAG_READ,
1385 ops,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001386 NULL, 0,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001387 rbd_simple_req_cb, 0, NULL);
1388
1389 rbd_destroy_ops(ops);
1390 return ret;
1391}
1392
1393static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
1394{
Alex Elder0ce1a792012-07-03 16:01:18 -05001395 struct rbd_device *rbd_dev = (struct rbd_device *)data;
Josh Durgina71b8912011-12-05 18:10:44 -08001396 u64 hver;
Sage Weil13143d22011-05-12 16:08:30 -07001397 int rc;
1398
Alex Elder0ce1a792012-07-03 16:01:18 -05001399 if (!rbd_dev)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001400 return;
1401
Alex Elderbd919d42012-07-13 20:35:11 -05001402 dout("rbd_watch_cb %s notify_id=%llu opcode=%u\n",
1403 rbd_dev->header_name, (unsigned long long) notify_id,
1404 (unsigned int) opcode);
Alex Elder117973f2012-08-31 17:29:55 -05001405 rc = rbd_dev_refresh(rbd_dev, &hver);
Sage Weil13143d22011-05-12 16:08:30 -07001406 if (rc)
Alex Elderf0f8cef2012-01-29 13:57:44 -06001407 pr_warning(RBD_DRV_NAME "%d got notification but failed to "
Alex Elder0ce1a792012-07-03 16:01:18 -05001408 " update snaps: %d\n", rbd_dev->major, rc);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001409
Alex Elder7f0a24d2012-07-25 09:32:40 -05001410 rbd_req_sync_notify_ack(rbd_dev, hver, notify_id);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001411}
1412
1413/*
1414 * Request sync osd watch
1415 */
Alex Elder0e6f3222012-07-25 09:32:40 -05001416static int rbd_req_sync_watch(struct rbd_device *rbd_dev)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001417{
1418 struct ceph_osd_req_op *ops;
Alex Elder0ce1a792012-07-03 16:01:18 -05001419 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder57cfc102012-06-26 12:57:03 -07001420 int ret;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001421
Alex Elder57cfc102012-06-26 12:57:03 -07001422 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_WATCH, 0);
1423 if (!ops)
1424 return -ENOMEM;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001425
1426 ret = ceph_osdc_create_event(osdc, rbd_watch_cb, 0,
Alex Elder0ce1a792012-07-03 16:01:18 -05001427 (void *)rbd_dev, &rbd_dev->watch_event);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001428 if (ret < 0)
1429 goto fail;
1430
Alex Elder0e6f3222012-07-25 09:32:40 -05001431 ops[0].watch.ver = cpu_to_le64(rbd_dev->header.obj_version);
Alex Elder0ce1a792012-07-03 16:01:18 -05001432 ops[0].watch.cookie = cpu_to_le64(rbd_dev->watch_event->cookie);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001433 ops[0].watch.flag = 1;
1434
Alex Elder0ce1a792012-07-03 16:01:18 -05001435 ret = rbd_req_sync_op(rbd_dev, NULL,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001436 CEPH_NOSNAP,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001437 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1438 ops,
Alex Elder0e6f3222012-07-25 09:32:40 -05001439 rbd_dev->header_name,
1440 0, 0, NULL,
Alex Elder0ce1a792012-07-03 16:01:18 -05001441 &rbd_dev->watch_request, NULL);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001442
1443 if (ret < 0)
1444 goto fail_event;
1445
1446 rbd_destroy_ops(ops);
1447 return 0;
1448
1449fail_event:
Alex Elder0ce1a792012-07-03 16:01:18 -05001450 ceph_osdc_cancel_event(rbd_dev->watch_event);
1451 rbd_dev->watch_event = NULL;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001452fail:
1453 rbd_destroy_ops(ops);
1454 return ret;
1455}
1456
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001457/*
1458 * Request sync osd unwatch
1459 */
Alex Elder070c6332012-07-25 09:32:41 -05001460static int rbd_req_sync_unwatch(struct rbd_device *rbd_dev)
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001461{
1462 struct ceph_osd_req_op *ops;
Alex Elder57cfc102012-06-26 12:57:03 -07001463 int ret;
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001464
Alex Elder57cfc102012-06-26 12:57:03 -07001465 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_WATCH, 0);
1466 if (!ops)
1467 return -ENOMEM;
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001468
1469 ops[0].watch.ver = 0;
Alex Elder0ce1a792012-07-03 16:01:18 -05001470 ops[0].watch.cookie = cpu_to_le64(rbd_dev->watch_event->cookie);
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001471 ops[0].watch.flag = 0;
1472
Alex Elder0ce1a792012-07-03 16:01:18 -05001473 ret = rbd_req_sync_op(rbd_dev, NULL,
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001474 CEPH_NOSNAP,
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001475 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1476 ops,
Alex Elder070c6332012-07-25 09:32:41 -05001477 rbd_dev->header_name,
1478 0, 0, NULL, NULL, NULL);
1479
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001480
1481 rbd_destroy_ops(ops);
Alex Elder0ce1a792012-07-03 16:01:18 -05001482 ceph_osdc_cancel_event(rbd_dev->watch_event);
1483 rbd_dev->watch_event = NULL;
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001484 return ret;
1485}
1486
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001487/*
Alex Elder3cb4a682012-06-26 12:57:03 -07001488 * Synchronous osd object method call
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001489 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001490static int rbd_req_sync_exec(struct rbd_device *rbd_dev,
Alex Elderaded07e2012-07-03 16:01:18 -05001491 const char *object_name,
1492 const char *class_name,
1493 const char *method_name,
Alex Elder3cb4a682012-06-26 12:57:03 -07001494 const char *outbound,
1495 size_t outbound_size,
Alex Elderf8d4de62012-07-03 16:01:19 -05001496 char *inbound,
1497 size_t inbound_size,
Alex Elder3cb4a682012-06-26 12:57:03 -07001498 int flags,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001499 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001500{
1501 struct ceph_osd_req_op *ops;
Alex Elderaded07e2012-07-03 16:01:18 -05001502 int class_name_len = strlen(class_name);
1503 int method_name_len = strlen(method_name);
Alex Elder3cb4a682012-06-26 12:57:03 -07001504 int payload_size;
Alex Elder57cfc102012-06-26 12:57:03 -07001505 int ret;
1506
Alex Elder3cb4a682012-06-26 12:57:03 -07001507 /*
1508 * Any input parameters required by the method we're calling
1509 * will be sent along with the class and method names as
1510 * part of the message payload. That data and its size are
1511 * supplied via the indata and indata_len fields (named from
1512 * the perspective of the server side) in the OSD request
1513 * operation.
1514 */
1515 payload_size = class_name_len + method_name_len + outbound_size;
1516 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_CALL, payload_size);
Alex Elder57cfc102012-06-26 12:57:03 -07001517 if (!ops)
1518 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001519
Alex Elderaded07e2012-07-03 16:01:18 -05001520 ops[0].cls.class_name = class_name;
1521 ops[0].cls.class_len = (__u8) class_name_len;
1522 ops[0].cls.method_name = method_name;
1523 ops[0].cls.method_len = (__u8) method_name_len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001524 ops[0].cls.argc = 0;
Alex Elder3cb4a682012-06-26 12:57:03 -07001525 ops[0].cls.indata = outbound;
1526 ops[0].cls.indata_len = outbound_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001527
Alex Elder0ce1a792012-07-03 16:01:18 -05001528 ret = rbd_req_sync_op(rbd_dev, NULL,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001529 CEPH_NOSNAP,
Alex Elder3cb4a682012-06-26 12:57:03 -07001530 flags, ops,
Alex Elderf8d4de62012-07-03 16:01:19 -05001531 object_name, 0, inbound_size, inbound,
1532 NULL, ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001533
1534 rbd_destroy_ops(ops);
1535
1536 dout("cls_exec returned %d\n", ret);
1537 return ret;
1538}
1539
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001540static struct rbd_req_coll *rbd_alloc_coll(int num_reqs)
1541{
1542 struct rbd_req_coll *coll =
1543 kzalloc(sizeof(struct rbd_req_coll) +
1544 sizeof(struct rbd_req_status) * num_reqs,
1545 GFP_ATOMIC);
1546
1547 if (!coll)
1548 return NULL;
1549 coll->total = num_reqs;
1550 kref_init(&coll->kref);
1551 return coll;
1552}
1553
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001554/*
1555 * block device queue callback
1556 */
1557static void rbd_rq_fn(struct request_queue *q)
1558{
1559 struct rbd_device *rbd_dev = q->queuedata;
1560 struct request *rq;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001561
Alex Elder00f1f362012-02-07 12:03:36 -06001562 while ((rq = blk_fetch_request(q))) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001563 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001564 bool do_write;
Alex Elderbd919d42012-07-13 20:35:11 -05001565 unsigned int size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001566 u64 ofs;
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001567 int num_segs, cur_seg = 0;
1568 struct rbd_req_coll *coll;
Josh Durgind1d25642011-12-05 14:03:05 -08001569 struct ceph_snap_context *snapc;
Alex Elderf7760da2012-10-20 22:17:27 -05001570 unsigned int bio_offset;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001571
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001572 dout("fetched request\n");
1573
1574 /* filter out block requests we don't understand */
1575 if ((rq->cmd_type != REQ_TYPE_FS)) {
1576 __blk_end_request_all(rq, 0);
Alex Elder00f1f362012-02-07 12:03:36 -06001577 continue;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001578 }
1579
1580 /* deduce our operation (read, write) */
1581 do_write = (rq_data_dir(rq) == WRITE);
Alex Elderf84344f2012-08-31 17:29:51 -05001582 if (do_write && rbd_dev->mapping.read_only) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001583 __blk_end_request_all(rq, -EROFS);
Alex Elder00f1f362012-02-07 12:03:36 -06001584 continue;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001585 }
1586
1587 spin_unlock_irq(q->queue_lock);
1588
Josh Durgind1d25642011-12-05 14:03:05 -08001589 down_read(&rbd_dev->header_rwsem);
Josh Durgine88a36e2011-11-21 18:14:25 -08001590
Alex Elderdaba5fd2012-10-26 17:25:23 -05001591 if (!rbd_dev->exists) {
Alex Elder0d7dbfc2012-10-25 23:34:41 -05001592 rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP);
Josh Durgine88a36e2011-11-21 18:14:25 -08001593 up_read(&rbd_dev->header_rwsem);
Josh Durgind1d25642011-12-05 14:03:05 -08001594 dout("request for non-existent snapshot");
1595 spin_lock_irq(q->queue_lock);
1596 __blk_end_request_all(rq, -ENXIO);
1597 continue;
Josh Durgine88a36e2011-11-21 18:14:25 -08001598 }
1599
Josh Durgind1d25642011-12-05 14:03:05 -08001600 snapc = ceph_get_snap_context(rbd_dev->header.snapc);
1601
1602 up_read(&rbd_dev->header_rwsem);
1603
Alex Elderf7760da2012-10-20 22:17:27 -05001604 size = blk_rq_bytes(rq);
1605 ofs = blk_rq_pos(rq) * SECTOR_SIZE;
1606 bio = rq->bio;
1607
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001608 dout("%s 0x%x bytes at 0x%llx\n",
1609 do_write ? "write" : "read",
Alex Elderbd919d42012-07-13 20:35:11 -05001610 size, (unsigned long long) blk_rq_pos(rq) * SECTOR_SIZE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001611
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001612 num_segs = rbd_get_num_segments(&rbd_dev->header, ofs, size);
Alex Elderdf111be2012-08-09 10:33:26 -07001613 if (num_segs <= 0) {
1614 spin_lock_irq(q->queue_lock);
1615 __blk_end_request_all(rq, num_segs);
1616 ceph_put_snap_context(snapc);
1617 continue;
1618 }
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001619 coll = rbd_alloc_coll(num_segs);
1620 if (!coll) {
1621 spin_lock_irq(q->queue_lock);
1622 __blk_end_request_all(rq, -ENOMEM);
Josh Durgind1d25642011-12-05 14:03:05 -08001623 ceph_put_snap_context(snapc);
Alex Elder00f1f362012-02-07 12:03:36 -06001624 continue;
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001625 }
1626
Alex Elderf7760da2012-10-20 22:17:27 -05001627 bio_offset = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001628 do {
Alex Elderf7760da2012-10-20 22:17:27 -05001629 u64 limit = rbd_segment_length(rbd_dev, ofs, size);
1630 unsigned int chain_size;
1631 struct bio *bio_chain;
1632
1633 BUG_ON(limit > (u64) UINT_MAX);
1634 chain_size = (unsigned int) limit;
Alex Elderbd919d42012-07-13 20:35:11 -05001635 dout("rq->bio->bi_vcnt=%hu\n", rq->bio->bi_vcnt);
Alex Elderf7760da2012-10-20 22:17:27 -05001636
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001637 kref_get(&coll->kref);
Alex Elderf7760da2012-10-20 22:17:27 -05001638
1639 /* Pass a cloned bio chain via an osd request */
1640
1641 bio_chain = bio_chain_clone_range(&bio,
1642 &bio_offset, chain_size,
1643 GFP_ATOMIC);
1644 if (bio_chain)
Alex Elder46342462012-10-10 18:59:29 -07001645 (void) rbd_do_op(rq, rbd_dev, snapc,
Alex Elderf7760da2012-10-20 22:17:27 -05001646 ofs, chain_size,
1647 bio_chain, coll, cur_seg);
Alex Elder46342462012-10-10 18:59:29 -07001648 else
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001649 rbd_coll_end_req_index(rq, coll, cur_seg,
Alex Elderf7760da2012-10-20 22:17:27 -05001650 -ENOMEM, chain_size);
1651 size -= chain_size;
1652 ofs += chain_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001653
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001654 cur_seg++;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001655 } while (size > 0);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001656 kref_put(&coll->kref, rbd_coll_release);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001657
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001658 spin_lock_irq(q->queue_lock);
Josh Durgind1d25642011-12-05 14:03:05 -08001659
1660 ceph_put_snap_context(snapc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001661 }
1662}
1663
1664/*
1665 * a queue callback. Makes sure that we don't create a bio that spans across
1666 * multiple osd objects. One exception would be with a single page bios,
Alex Elderf7760da2012-10-20 22:17:27 -05001667 * which we handle later at bio_chain_clone_range()
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001668 */
1669static int rbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd,
1670 struct bio_vec *bvec)
1671{
1672 struct rbd_device *rbd_dev = q->queuedata;
Alex Eldere5cfeed22012-10-20 22:17:27 -05001673 sector_t sector_offset;
1674 sector_t sectors_per_obj;
1675 sector_t obj_sector_offset;
1676 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001677
Alex Eldere5cfeed22012-10-20 22:17:27 -05001678 /*
1679 * Find how far into its rbd object the partition-relative
1680 * bio start sector is to offset relative to the enclosing
1681 * device.
1682 */
1683 sector_offset = get_start_sect(bmd->bi_bdev) + bmd->bi_sector;
1684 sectors_per_obj = 1 << (rbd_dev->header.obj_order - SECTOR_SHIFT);
1685 obj_sector_offset = sector_offset & (sectors_per_obj - 1);
Alex Elder593a9e72012-02-07 12:03:37 -06001686
Alex Eldere5cfeed22012-10-20 22:17:27 -05001687 /*
1688 * Compute the number of bytes from that offset to the end
1689 * of the object. Account for what's already used by the bio.
1690 */
1691 ret = (int) (sectors_per_obj - obj_sector_offset) << SECTOR_SHIFT;
1692 if (ret > bmd->bi_size)
1693 ret -= bmd->bi_size;
1694 else
1695 ret = 0;
1696
1697 /*
1698 * Don't send back more than was asked for. And if the bio
1699 * was empty, let the whole thing through because: "Note
1700 * that a block device *must* allow a single page to be
1701 * added to an empty bio."
1702 */
1703 rbd_assert(bvec->bv_len <= PAGE_SIZE);
1704 if (ret > (int) bvec->bv_len || !bmd->bi_size)
1705 ret = (int) bvec->bv_len;
1706
1707 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001708}
1709
1710static void rbd_free_disk(struct rbd_device *rbd_dev)
1711{
1712 struct gendisk *disk = rbd_dev->disk;
1713
1714 if (!disk)
1715 return;
1716
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001717 if (disk->flags & GENHD_FL_UP)
1718 del_gendisk(disk);
1719 if (disk->queue)
1720 blk_cleanup_queue(disk->queue);
1721 put_disk(disk);
1722}
1723
1724/*
Alex Elder4156d992012-08-02 11:29:46 -05001725 * Read the complete header for the given rbd device.
1726 *
1727 * Returns a pointer to a dynamically-allocated buffer containing
1728 * the complete and validated header. Caller can pass the address
1729 * of a variable that will be filled in with the version of the
1730 * header object at the time it was read.
1731 *
1732 * Returns a pointer-coded errno if a failure occurs.
1733 */
1734static struct rbd_image_header_ondisk *
1735rbd_dev_v1_header_read(struct rbd_device *rbd_dev, u64 *version)
1736{
1737 struct rbd_image_header_ondisk *ondisk = NULL;
1738 u32 snap_count = 0;
1739 u64 names_size = 0;
1740 u32 want_count;
1741 int ret;
1742
1743 /*
1744 * The complete header will include an array of its 64-bit
1745 * snapshot ids, followed by the names of those snapshots as
1746 * a contiguous block of NUL-terminated strings. Note that
1747 * the number of snapshots could change by the time we read
1748 * it in, in which case we re-read it.
1749 */
1750 do {
1751 size_t size;
1752
1753 kfree(ondisk);
1754
1755 size = sizeof (*ondisk);
1756 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
1757 size += names_size;
1758 ondisk = kmalloc(size, GFP_KERNEL);
1759 if (!ondisk)
1760 return ERR_PTR(-ENOMEM);
1761
1762 ret = rbd_req_sync_read(rbd_dev, CEPH_NOSNAP,
1763 rbd_dev->header_name,
1764 0, size,
1765 (char *) ondisk, version);
1766
1767 if (ret < 0)
1768 goto out_err;
1769 if (WARN_ON((size_t) ret < size)) {
1770 ret = -ENXIO;
1771 pr_warning("short header read for image %s"
1772 " (want %zd got %d)\n",
Alex Elder0d7dbfc2012-10-25 23:34:41 -05001773 rbd_dev->spec->image_name, size, ret);
Alex Elder4156d992012-08-02 11:29:46 -05001774 goto out_err;
1775 }
1776 if (!rbd_dev_ondisk_valid(ondisk)) {
1777 ret = -ENXIO;
1778 pr_warning("invalid header for image %s\n",
Alex Elder0d7dbfc2012-10-25 23:34:41 -05001779 rbd_dev->spec->image_name);
Alex Elder4156d992012-08-02 11:29:46 -05001780 goto out_err;
1781 }
1782
1783 names_size = le64_to_cpu(ondisk->snap_names_len);
1784 want_count = snap_count;
1785 snap_count = le32_to_cpu(ondisk->snap_count);
1786 } while (snap_count != want_count);
1787
1788 return ondisk;
1789
1790out_err:
1791 kfree(ondisk);
1792
1793 return ERR_PTR(ret);
1794}
1795
1796/*
1797 * reload the ondisk the header
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001798 */
1799static int rbd_read_header(struct rbd_device *rbd_dev,
1800 struct rbd_image_header *header)
1801{
Alex Elder4156d992012-08-02 11:29:46 -05001802 struct rbd_image_header_ondisk *ondisk;
1803 u64 ver = 0;
1804 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001805
Alex Elder4156d992012-08-02 11:29:46 -05001806 ondisk = rbd_dev_v1_header_read(rbd_dev, &ver);
1807 if (IS_ERR(ondisk))
1808 return PTR_ERR(ondisk);
1809 ret = rbd_header_from_disk(header, ondisk);
1810 if (ret >= 0)
1811 header->obj_version = ver;
1812 kfree(ondisk);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001813
Alex Elder4156d992012-08-02 11:29:46 -05001814 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001815}
1816
Alex Elder41f38c22012-10-25 23:34:40 -05001817static void rbd_remove_all_snaps(struct rbd_device *rbd_dev)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001818{
1819 struct rbd_snap *snap;
Alex Eldera0593292012-07-19 09:09:27 -05001820 struct rbd_snap *next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001821
Alex Eldera0593292012-07-19 09:09:27 -05001822 list_for_each_entry_safe(snap, next, &rbd_dev->snaps, node)
Alex Elder41f38c22012-10-25 23:34:40 -05001823 rbd_remove_snap_dev(snap);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001824}
1825
Alex Elder94785542012-10-09 13:50:17 -07001826static void rbd_update_mapping_size(struct rbd_device *rbd_dev)
1827{
1828 sector_t size;
1829
Alex Elder0d7dbfc2012-10-25 23:34:41 -05001830 if (rbd_dev->spec->snap_id != CEPH_NOSNAP)
Alex Elder94785542012-10-09 13:50:17 -07001831 return;
1832
1833 size = (sector_t) rbd_dev->header.image_size / SECTOR_SIZE;
1834 dout("setting size to %llu sectors", (unsigned long long) size);
1835 rbd_dev->mapping.size = (u64) size;
1836 set_capacity(rbd_dev->disk, size);
1837}
1838
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001839/*
1840 * only read the first part of the ondisk header, without the snaps info
1841 */
Alex Elder117973f2012-08-31 17:29:55 -05001842static int rbd_dev_v1_refresh(struct rbd_device *rbd_dev, u64 *hver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001843{
1844 int ret;
1845 struct rbd_image_header h;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001846
1847 ret = rbd_read_header(rbd_dev, &h);
1848 if (ret < 0)
1849 return ret;
1850
Josh Durgina51aa0c2011-12-05 10:35:04 -08001851 down_write(&rbd_dev->header_rwsem);
1852
Alex Elder94785542012-10-09 13:50:17 -07001853 /* Update image size, and check for resize of mapped image */
1854 rbd_dev->header.image_size = h.image_size;
1855 rbd_update_mapping_size(rbd_dev);
Sage Weil9db4b3e2011-04-19 22:49:06 -07001856
Alex Elder849b4262012-07-09 21:04:24 -05001857 /* rbd_dev->header.object_prefix shouldn't change */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001858 kfree(rbd_dev->header.snap_sizes);
Alex Elder849b4262012-07-09 21:04:24 -05001859 kfree(rbd_dev->header.snap_names);
Josh Durgind1d25642011-12-05 14:03:05 -08001860 /* osd requests may still refer to snapc */
1861 ceph_put_snap_context(rbd_dev->header.snapc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001862
Alex Elderb8136232012-07-25 09:32:41 -05001863 if (hver)
1864 *hver = h.obj_version;
Josh Durgina71b8912011-12-05 18:10:44 -08001865 rbd_dev->header.obj_version = h.obj_version;
Josh Durgin93a24e02011-12-05 10:41:28 -08001866 rbd_dev->header.image_size = h.image_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001867 rbd_dev->header.snapc = h.snapc;
1868 rbd_dev->header.snap_names = h.snap_names;
1869 rbd_dev->header.snap_sizes = h.snap_sizes;
Alex Elder849b4262012-07-09 21:04:24 -05001870 /* Free the extra copy of the object prefix */
1871 WARN_ON(strcmp(rbd_dev->header.object_prefix, h.object_prefix));
1872 kfree(h.object_prefix);
1873
Alex Elder304f6802012-08-31 17:29:52 -05001874 ret = rbd_dev_snaps_update(rbd_dev);
1875 if (!ret)
1876 ret = rbd_dev_snaps_register(rbd_dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001877
Josh Durginc6666012011-11-21 17:11:12 -08001878 up_write(&rbd_dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001879
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001880 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001881}
1882
Alex Elder117973f2012-08-31 17:29:55 -05001883static int rbd_dev_refresh(struct rbd_device *rbd_dev, u64 *hver)
Alex Elder1fe5e992012-07-25 09:32:41 -05001884{
1885 int ret;
1886
Alex Elder117973f2012-08-31 17:29:55 -05001887 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
Alex Elder1fe5e992012-07-25 09:32:41 -05001888 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Alex Elder117973f2012-08-31 17:29:55 -05001889 if (rbd_dev->image_format == 1)
1890 ret = rbd_dev_v1_refresh(rbd_dev, hver);
1891 else
1892 ret = rbd_dev_v2_refresh(rbd_dev, hver);
Alex Elder1fe5e992012-07-25 09:32:41 -05001893 mutex_unlock(&ctl_mutex);
1894
1895 return ret;
1896}
1897
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001898static int rbd_init_disk(struct rbd_device *rbd_dev)
1899{
1900 struct gendisk *disk;
1901 struct request_queue *q;
Alex Elder593a9e72012-02-07 12:03:37 -06001902 u64 segment_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001903
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001904 /* create gendisk info */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001905 disk = alloc_disk(RBD_MINORS_PER_MAJOR);
1906 if (!disk)
Alex Elder1fcdb8a2012-08-29 17:11:06 -05001907 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001908
Alex Elderf0f8cef2012-01-29 13:57:44 -06001909 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
Alex Elderde71a292012-07-03 16:01:19 -05001910 rbd_dev->dev_id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001911 disk->major = rbd_dev->major;
1912 disk->first_minor = 0;
1913 disk->fops = &rbd_bd_ops;
1914 disk->private_data = rbd_dev;
1915
1916 /* init rq */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001917 q = blk_init_queue(rbd_rq_fn, &rbd_dev->lock);
1918 if (!q)
1919 goto out_disk;
Josh Durgin029bcbd2011-07-22 11:35:23 -07001920
Alex Elder593a9e72012-02-07 12:03:37 -06001921 /* We use the default size, but let's be explicit about it. */
1922 blk_queue_physical_block_size(q, SECTOR_SIZE);
1923
Josh Durgin029bcbd2011-07-22 11:35:23 -07001924 /* set io sizes to object size */
Alex Elder593a9e72012-02-07 12:03:37 -06001925 segment_size = rbd_obj_bytes(&rbd_dev->header);
1926 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
1927 blk_queue_max_segment_size(q, segment_size);
1928 blk_queue_io_min(q, segment_size);
1929 blk_queue_io_opt(q, segment_size);
Josh Durgin029bcbd2011-07-22 11:35:23 -07001930
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001931 blk_queue_merge_bvec(q, rbd_merge_bvec);
1932 disk->queue = q;
1933
1934 q->queuedata = rbd_dev;
1935
1936 rbd_dev->disk = disk;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001937
Alex Elder12f02942012-08-29 17:11:07 -05001938 set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
1939
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001940 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001941out_disk:
1942 put_disk(disk);
Alex Elder1fcdb8a2012-08-29 17:11:06 -05001943
1944 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001945}
1946
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001947/*
1948 sysfs
1949*/
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001950
Alex Elder593a9e72012-02-07 12:03:37 -06001951static struct rbd_device *dev_to_rbd_dev(struct device *dev)
1952{
1953 return container_of(dev, struct rbd_device, dev);
1954}
1955
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001956static ssize_t rbd_size_show(struct device *dev,
1957 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001958{
Alex Elder593a9e72012-02-07 12:03:37 -06001959 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Josh Durgina51aa0c2011-12-05 10:35:04 -08001960 sector_t size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001961
Josh Durgina51aa0c2011-12-05 10:35:04 -08001962 down_read(&rbd_dev->header_rwsem);
1963 size = get_capacity(rbd_dev->disk);
1964 up_read(&rbd_dev->header_rwsem);
1965
1966 return sprintf(buf, "%llu\n", (unsigned long long) size * SECTOR_SIZE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001967}
1968
Alex Elder34b13182012-07-13 20:35:12 -05001969/*
1970 * Note this shows the features for whatever's mapped, which is not
1971 * necessarily the base image.
1972 */
1973static ssize_t rbd_features_show(struct device *dev,
1974 struct device_attribute *attr, char *buf)
1975{
1976 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
1977
1978 return sprintf(buf, "0x%016llx\n",
1979 (unsigned long long) rbd_dev->mapping.features);
1980}
1981
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001982static ssize_t rbd_major_show(struct device *dev,
1983 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001984{
Alex Elder593a9e72012-02-07 12:03:37 -06001985 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001986
1987 return sprintf(buf, "%d\n", rbd_dev->major);
1988}
1989
1990static ssize_t rbd_client_id_show(struct device *dev,
1991 struct device_attribute *attr, char *buf)
1992{
Alex Elder593a9e72012-02-07 12:03:37 -06001993 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001994
Alex Elder1dbb4392012-01-24 10:08:37 -06001995 return sprintf(buf, "client%lld\n",
1996 ceph_client_id(rbd_dev->rbd_client->client));
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001997}
1998
1999static ssize_t rbd_pool_show(struct device *dev,
2000 struct device_attribute *attr, char *buf)
2001{
Alex Elder593a9e72012-02-07 12:03:37 -06002002 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002003
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002004 return sprintf(buf, "%s\n", rbd_dev->spec->pool_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002005}
2006
Alex Elder9bb2f332012-07-12 10:46:35 -05002007static ssize_t rbd_pool_id_show(struct device *dev,
2008 struct device_attribute *attr, char *buf)
2009{
2010 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
2011
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002012 return sprintf(buf, "%llu\n",
2013 (unsigned long long) rbd_dev->spec->pool_id);
Alex Elder9bb2f332012-07-12 10:46:35 -05002014}
2015
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002016static ssize_t rbd_name_show(struct device *dev,
2017 struct device_attribute *attr, char *buf)
2018{
Alex Elder593a9e72012-02-07 12:03:37 -06002019 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002020
Alex Eldera92ffdf2012-10-30 19:40:33 -05002021 if (rbd_dev->spec->image_name)
2022 return sprintf(buf, "%s\n", rbd_dev->spec->image_name);
2023
2024 return sprintf(buf, "(unknown)\n");
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002025}
2026
Alex Elder589d30e2012-07-10 20:30:11 -05002027static ssize_t rbd_image_id_show(struct device *dev,
2028 struct device_attribute *attr, char *buf)
2029{
2030 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
2031
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002032 return sprintf(buf, "%s\n", rbd_dev->spec->image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05002033}
2034
Alex Elder34b13182012-07-13 20:35:12 -05002035/*
2036 * Shows the name of the currently-mapped snapshot (or
2037 * RBD_SNAP_HEAD_NAME for the base image).
2038 */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002039static ssize_t rbd_snap_show(struct device *dev,
2040 struct device_attribute *attr,
2041 char *buf)
2042{
Alex Elder593a9e72012-02-07 12:03:37 -06002043 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002044
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002045 return sprintf(buf, "%s\n", rbd_dev->spec->snap_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002046}
2047
Alex Elder86b00e02012-10-25 23:34:42 -05002048/*
2049 * For an rbd v2 image, shows the pool id, image id, and snapshot id
2050 * for the parent image. If there is no parent, simply shows
2051 * "(no parent image)".
2052 */
2053static ssize_t rbd_parent_show(struct device *dev,
2054 struct device_attribute *attr,
2055 char *buf)
2056{
2057 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
2058 struct rbd_spec *spec = rbd_dev->parent_spec;
2059 int count;
2060 char *bufp = buf;
2061
2062 if (!spec)
2063 return sprintf(buf, "(no parent image)\n");
2064
2065 count = sprintf(bufp, "pool_id %llu\npool_name %s\n",
2066 (unsigned long long) spec->pool_id, spec->pool_name);
2067 if (count < 0)
2068 return count;
2069 bufp += count;
2070
2071 count = sprintf(bufp, "image_id %s\nimage_name %s\n", spec->image_id,
2072 spec->image_name ? spec->image_name : "(unknown)");
2073 if (count < 0)
2074 return count;
2075 bufp += count;
2076
2077 count = sprintf(bufp, "snap_id %llu\nsnap_name %s\n",
2078 (unsigned long long) spec->snap_id, spec->snap_name);
2079 if (count < 0)
2080 return count;
2081 bufp += count;
2082
2083 count = sprintf(bufp, "overlap %llu\n", rbd_dev->parent_overlap);
2084 if (count < 0)
2085 return count;
2086 bufp += count;
2087
2088 return (ssize_t) (bufp - buf);
2089}
2090
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002091static ssize_t rbd_image_refresh(struct device *dev,
2092 struct device_attribute *attr,
2093 const char *buf,
2094 size_t size)
2095{
Alex Elder593a9e72012-02-07 12:03:37 -06002096 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Alex Elderb8136232012-07-25 09:32:41 -05002097 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002098
Alex Elder117973f2012-08-31 17:29:55 -05002099 ret = rbd_dev_refresh(rbd_dev, NULL);
Alex Elderb8136232012-07-25 09:32:41 -05002100
2101 return ret < 0 ? ret : size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002102}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002103
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002104static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
Alex Elder34b13182012-07-13 20:35:12 -05002105static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002106static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
2107static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
2108static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
Alex Elder9bb2f332012-07-12 10:46:35 -05002109static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002110static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
Alex Elder589d30e2012-07-10 20:30:11 -05002111static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002112static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
2113static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
Alex Elder86b00e02012-10-25 23:34:42 -05002114static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002115
2116static struct attribute *rbd_attrs[] = {
2117 &dev_attr_size.attr,
Alex Elder34b13182012-07-13 20:35:12 -05002118 &dev_attr_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002119 &dev_attr_major.attr,
2120 &dev_attr_client_id.attr,
2121 &dev_attr_pool.attr,
Alex Elder9bb2f332012-07-12 10:46:35 -05002122 &dev_attr_pool_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002123 &dev_attr_name.attr,
Alex Elder589d30e2012-07-10 20:30:11 -05002124 &dev_attr_image_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002125 &dev_attr_current_snap.attr,
Alex Elder86b00e02012-10-25 23:34:42 -05002126 &dev_attr_parent.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002127 &dev_attr_refresh.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002128 NULL
2129};
2130
2131static struct attribute_group rbd_attr_group = {
2132 .attrs = rbd_attrs,
2133};
2134
2135static const struct attribute_group *rbd_attr_groups[] = {
2136 &rbd_attr_group,
2137 NULL
2138};
2139
2140static void rbd_sysfs_dev_release(struct device *dev)
2141{
2142}
2143
2144static struct device_type rbd_device_type = {
2145 .name = "rbd",
2146 .groups = rbd_attr_groups,
2147 .release = rbd_sysfs_dev_release,
2148};
2149
2150
2151/*
2152 sysfs - snapshots
2153*/
2154
2155static ssize_t rbd_snap_size_show(struct device *dev,
2156 struct device_attribute *attr,
2157 char *buf)
2158{
2159 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2160
Josh Durgin3591538f2011-12-05 18:25:13 -08002161 return sprintf(buf, "%llu\n", (unsigned long long)snap->size);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002162}
2163
2164static ssize_t rbd_snap_id_show(struct device *dev,
2165 struct device_attribute *attr,
2166 char *buf)
2167{
2168 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2169
Josh Durgin3591538f2011-12-05 18:25:13 -08002170 return sprintf(buf, "%llu\n", (unsigned long long)snap->id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002171}
2172
Alex Elder34b13182012-07-13 20:35:12 -05002173static ssize_t rbd_snap_features_show(struct device *dev,
2174 struct device_attribute *attr,
2175 char *buf)
2176{
2177 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2178
2179 return sprintf(buf, "0x%016llx\n",
2180 (unsigned long long) snap->features);
2181}
2182
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002183static DEVICE_ATTR(snap_size, S_IRUGO, rbd_snap_size_show, NULL);
2184static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL);
Alex Elder34b13182012-07-13 20:35:12 -05002185static DEVICE_ATTR(snap_features, S_IRUGO, rbd_snap_features_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002186
2187static struct attribute *rbd_snap_attrs[] = {
2188 &dev_attr_snap_size.attr,
2189 &dev_attr_snap_id.attr,
Alex Elder34b13182012-07-13 20:35:12 -05002190 &dev_attr_snap_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002191 NULL,
2192};
2193
2194static struct attribute_group rbd_snap_attr_group = {
2195 .attrs = rbd_snap_attrs,
2196};
2197
2198static void rbd_snap_dev_release(struct device *dev)
2199{
2200 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2201 kfree(snap->name);
2202 kfree(snap);
2203}
2204
2205static const struct attribute_group *rbd_snap_attr_groups[] = {
2206 &rbd_snap_attr_group,
2207 NULL
2208};
2209
2210static struct device_type rbd_snap_device_type = {
2211 .groups = rbd_snap_attr_groups,
2212 .release = rbd_snap_dev_release,
2213};
2214
Alex Elder8b8fb992012-10-26 17:25:24 -05002215static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)
2216{
2217 kref_get(&spec->kref);
2218
2219 return spec;
2220}
2221
2222static void rbd_spec_free(struct kref *kref);
2223static void rbd_spec_put(struct rbd_spec *spec)
2224{
2225 if (spec)
2226 kref_put(&spec->kref, rbd_spec_free);
2227}
2228
2229static struct rbd_spec *rbd_spec_alloc(void)
2230{
2231 struct rbd_spec *spec;
2232
2233 spec = kzalloc(sizeof (*spec), GFP_KERNEL);
2234 if (!spec)
2235 return NULL;
2236 kref_init(&spec->kref);
2237
2238 rbd_spec_put(rbd_spec_get(spec)); /* TEMPORARY */
2239
2240 return spec;
2241}
2242
2243static void rbd_spec_free(struct kref *kref)
2244{
2245 struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref);
2246
2247 kfree(spec->pool_name);
2248 kfree(spec->image_id);
2249 kfree(spec->image_name);
2250 kfree(spec->snap_name);
2251 kfree(spec);
2252}
2253
Alex Elderc53d5892012-10-25 23:34:42 -05002254struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,
2255 struct rbd_spec *spec)
2256{
2257 struct rbd_device *rbd_dev;
2258
2259 rbd_dev = kzalloc(sizeof (*rbd_dev), GFP_KERNEL);
2260 if (!rbd_dev)
2261 return NULL;
2262
2263 spin_lock_init(&rbd_dev->lock);
2264 INIT_LIST_HEAD(&rbd_dev->node);
2265 INIT_LIST_HEAD(&rbd_dev->snaps);
2266 init_rwsem(&rbd_dev->header_rwsem);
2267
2268 rbd_dev->spec = spec;
2269 rbd_dev->rbd_client = rbdc;
2270
2271 return rbd_dev;
2272}
2273
2274static void rbd_dev_destroy(struct rbd_device *rbd_dev)
2275{
Alex Elder86b00e02012-10-25 23:34:42 -05002276 rbd_spec_put(rbd_dev->parent_spec);
Alex Elderc53d5892012-10-25 23:34:42 -05002277 kfree(rbd_dev->header_name);
2278 rbd_put_client(rbd_dev->rbd_client);
2279 rbd_spec_put(rbd_dev->spec);
2280 kfree(rbd_dev);
2281}
2282
Alex Elder304f6802012-08-31 17:29:52 -05002283static bool rbd_snap_registered(struct rbd_snap *snap)
2284{
2285 bool ret = snap->dev.type == &rbd_snap_device_type;
2286 bool reg = device_is_registered(&snap->dev);
2287
2288 rbd_assert(!ret ^ reg);
2289
2290 return ret;
2291}
2292
Alex Elder41f38c22012-10-25 23:34:40 -05002293static void rbd_remove_snap_dev(struct rbd_snap *snap)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002294{
2295 list_del(&snap->node);
Alex Elder304f6802012-08-31 17:29:52 -05002296 if (device_is_registered(&snap->dev))
2297 device_unregister(&snap->dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002298}
2299
Alex Elder14e70852012-07-19 09:09:27 -05002300static int rbd_register_snap_dev(struct rbd_snap *snap,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002301 struct device *parent)
2302{
2303 struct device *dev = &snap->dev;
2304 int ret;
2305
2306 dev->type = &rbd_snap_device_type;
2307 dev->parent = parent;
2308 dev->release = rbd_snap_dev_release;
Alex Elderd4b125e2012-07-03 16:01:19 -05002309 dev_set_name(dev, "%s%s", RBD_SNAP_DEV_NAME_PREFIX, snap->name);
Alex Elder304f6802012-08-31 17:29:52 -05002310 dout("%s: registering device for snapshot %s\n", __func__, snap->name);
2311
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002312 ret = device_register(dev);
2313
2314 return ret;
2315}
2316
Alex Elder4e891e02012-07-10 20:30:10 -05002317static struct rbd_snap *__rbd_add_snap_dev(struct rbd_device *rbd_dev,
Alex Elderc8d18422012-07-10 20:30:11 -05002318 const char *snap_name,
Alex Elder34b13182012-07-13 20:35:12 -05002319 u64 snap_id, u64 snap_size,
2320 u64 snap_features)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002321{
Alex Elder4e891e02012-07-10 20:30:10 -05002322 struct rbd_snap *snap;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002323 int ret;
Alex Elder4e891e02012-07-10 20:30:10 -05002324
2325 snap = kzalloc(sizeof (*snap), GFP_KERNEL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002326 if (!snap)
Alex Elder4e891e02012-07-10 20:30:10 -05002327 return ERR_PTR(-ENOMEM);
2328
2329 ret = -ENOMEM;
Alex Elderc8d18422012-07-10 20:30:11 -05002330 snap->name = kstrdup(snap_name, GFP_KERNEL);
Alex Elder4e891e02012-07-10 20:30:10 -05002331 if (!snap->name)
2332 goto err;
2333
Alex Elderc8d18422012-07-10 20:30:11 -05002334 snap->id = snap_id;
2335 snap->size = snap_size;
Alex Elder34b13182012-07-13 20:35:12 -05002336 snap->features = snap_features;
Alex Elder4e891e02012-07-10 20:30:10 -05002337
2338 return snap;
2339
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002340err:
2341 kfree(snap->name);
2342 kfree(snap);
Alex Elder4e891e02012-07-10 20:30:10 -05002343
2344 return ERR_PTR(ret);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002345}
2346
Alex Eldercd892122012-07-03 16:01:19 -05002347static char *rbd_dev_v1_snap_info(struct rbd_device *rbd_dev, u32 which,
2348 u64 *snap_size, u64 *snap_features)
2349{
2350 char *snap_name;
2351
2352 rbd_assert(which < rbd_dev->header.snapc->num_snaps);
2353
2354 *snap_size = rbd_dev->header.snap_sizes[which];
2355 *snap_features = 0; /* No features for v1 */
2356
2357 /* Skip over names until we find the one we are looking for */
2358
2359 snap_name = rbd_dev->header.snap_names;
2360 while (which--)
2361 snap_name += strlen(snap_name) + 1;
2362
2363 return snap_name;
2364}
2365
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002366/*
Alex Elder9d475de2012-07-03 16:01:19 -05002367 * Get the size and object order for an image snapshot, or if
2368 * snap_id is CEPH_NOSNAP, gets this information for the base
2369 * image.
2370 */
2371static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
2372 u8 *order, u64 *snap_size)
2373{
2374 __le64 snapid = cpu_to_le64(snap_id);
2375 int ret;
2376 struct {
2377 u8 order;
2378 __le64 size;
2379 } __attribute__ ((packed)) size_buf = { 0 };
2380
2381 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2382 "rbd", "get_size",
2383 (char *) &snapid, sizeof (snapid),
2384 (char *) &size_buf, sizeof (size_buf),
2385 CEPH_OSD_FLAG_READ, NULL);
2386 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2387 if (ret < 0)
2388 return ret;
2389
2390 *order = size_buf.order;
2391 *snap_size = le64_to_cpu(size_buf.size);
2392
2393 dout(" snap_id 0x%016llx order = %u, snap_size = %llu\n",
2394 (unsigned long long) snap_id, (unsigned int) *order,
2395 (unsigned long long) *snap_size);
2396
2397 return 0;
2398}
2399
2400static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
2401{
2402 return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
2403 &rbd_dev->header.obj_order,
2404 &rbd_dev->header.image_size);
2405}
2406
Alex Elder1e130192012-07-03 16:01:19 -05002407static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
2408{
2409 void *reply_buf;
2410 int ret;
2411 void *p;
2412
2413 reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL);
2414 if (!reply_buf)
2415 return -ENOMEM;
2416
2417 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2418 "rbd", "get_object_prefix",
2419 NULL, 0,
2420 reply_buf, RBD_OBJ_PREFIX_LEN_MAX,
2421 CEPH_OSD_FLAG_READ, NULL);
2422 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2423 if (ret < 0)
2424 goto out;
Alex Eldera0ea3a42012-10-10 21:19:13 -07002425 ret = 0; /* rbd_req_sync_exec() can return positive */
Alex Elder1e130192012-07-03 16:01:19 -05002426
2427 p = reply_buf;
2428 rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p,
2429 p + RBD_OBJ_PREFIX_LEN_MAX,
2430 NULL, GFP_NOIO);
2431
2432 if (IS_ERR(rbd_dev->header.object_prefix)) {
2433 ret = PTR_ERR(rbd_dev->header.object_prefix);
2434 rbd_dev->header.object_prefix = NULL;
2435 } else {
2436 dout(" object_prefix = %s\n", rbd_dev->header.object_prefix);
2437 }
2438
2439out:
2440 kfree(reply_buf);
2441
2442 return ret;
2443}
2444
Alex Elderb1b54022012-07-03 16:01:19 -05002445static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
2446 u64 *snap_features)
2447{
2448 __le64 snapid = cpu_to_le64(snap_id);
2449 struct {
2450 __le64 features;
2451 __le64 incompat;
2452 } features_buf = { 0 };
Alex Elderd8891402012-10-09 13:50:17 -07002453 u64 incompat;
Alex Elderb1b54022012-07-03 16:01:19 -05002454 int ret;
2455
2456 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2457 "rbd", "get_features",
2458 (char *) &snapid, sizeof (snapid),
2459 (char *) &features_buf, sizeof (features_buf),
2460 CEPH_OSD_FLAG_READ, NULL);
2461 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2462 if (ret < 0)
2463 return ret;
Alex Elderd8891402012-10-09 13:50:17 -07002464
2465 incompat = le64_to_cpu(features_buf.incompat);
2466 if (incompat & ~RBD_FEATURES_ALL)
Alex Elderb8f5c6e2012-11-01 08:39:26 -05002467 return -ENXIO;
Alex Elderd8891402012-10-09 13:50:17 -07002468
Alex Elderb1b54022012-07-03 16:01:19 -05002469 *snap_features = le64_to_cpu(features_buf.features);
2470
2471 dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
2472 (unsigned long long) snap_id,
2473 (unsigned long long) *snap_features,
2474 (unsigned long long) le64_to_cpu(features_buf.incompat));
2475
2476 return 0;
2477}
2478
2479static int rbd_dev_v2_features(struct rbd_device *rbd_dev)
2480{
2481 return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP,
2482 &rbd_dev->header.features);
2483}
2484
Alex Elder86b00e02012-10-25 23:34:42 -05002485static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev)
2486{
2487 struct rbd_spec *parent_spec;
2488 size_t size;
2489 void *reply_buf = NULL;
2490 __le64 snapid;
2491 void *p;
2492 void *end;
2493 char *image_id;
2494 u64 overlap;
2495 size_t len = 0;
2496 int ret;
2497
2498 parent_spec = rbd_spec_alloc();
2499 if (!parent_spec)
2500 return -ENOMEM;
2501
2502 size = sizeof (__le64) + /* pool_id */
2503 sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */
2504 sizeof (__le64) + /* snap_id */
2505 sizeof (__le64); /* overlap */
2506 reply_buf = kmalloc(size, GFP_KERNEL);
2507 if (!reply_buf) {
2508 ret = -ENOMEM;
2509 goto out_err;
2510 }
2511
2512 snapid = cpu_to_le64(CEPH_NOSNAP);
2513 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2514 "rbd", "get_parent",
2515 (char *) &snapid, sizeof (snapid),
2516 (char *) reply_buf, size,
2517 CEPH_OSD_FLAG_READ, NULL);
2518 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2519 if (ret < 0)
2520 goto out_err;
2521
2522 ret = -ERANGE;
2523 p = reply_buf;
2524 end = (char *) reply_buf + size;
2525 ceph_decode_64_safe(&p, end, parent_spec->pool_id, out_err);
2526 if (parent_spec->pool_id == CEPH_NOPOOL)
2527 goto out; /* No parent? No problem. */
2528
2529 image_id = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL);
2530 if (IS_ERR(image_id)) {
2531 ret = PTR_ERR(image_id);
2532 goto out_err;
2533 }
2534 parent_spec->image_id = image_id;
Alex Elder9e15b772012-10-30 19:40:33 -05002535 parent_spec->image_id_len = len;
Alex Elder86b00e02012-10-25 23:34:42 -05002536 ceph_decode_64_safe(&p, end, parent_spec->snap_id, out_err);
2537 ceph_decode_64_safe(&p, end, overlap, out_err);
2538
2539 rbd_dev->parent_overlap = overlap;
2540 rbd_dev->parent_spec = parent_spec;
2541 parent_spec = NULL; /* rbd_dev now owns this */
2542out:
2543 ret = 0;
2544out_err:
2545 kfree(reply_buf);
2546 rbd_spec_put(parent_spec);
2547
2548 return ret;
2549}
2550
Alex Elder9e15b772012-10-30 19:40:33 -05002551static char *rbd_dev_image_name(struct rbd_device *rbd_dev)
2552{
2553 size_t image_id_size;
2554 char *image_id;
2555 void *p;
2556 void *end;
2557 size_t size;
2558 void *reply_buf = NULL;
2559 size_t len = 0;
2560 char *image_name = NULL;
2561 int ret;
2562
2563 rbd_assert(!rbd_dev->spec->image_name);
2564
Alex Elder69e7a022012-11-01 08:39:26 -05002565 len = strlen(rbd_dev->spec->image_id);
2566 image_id_size = sizeof (__le32) + len;
Alex Elder9e15b772012-10-30 19:40:33 -05002567 image_id = kmalloc(image_id_size, GFP_KERNEL);
2568 if (!image_id)
2569 return NULL;
2570
2571 p = image_id;
2572 end = (char *) image_id + image_id_size;
Alex Elder69e7a022012-11-01 08:39:26 -05002573 ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32) len);
Alex Elder9e15b772012-10-30 19:40:33 -05002574
2575 size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX;
2576 reply_buf = kmalloc(size, GFP_KERNEL);
2577 if (!reply_buf)
2578 goto out;
2579
2580 ret = rbd_req_sync_exec(rbd_dev, RBD_DIRECTORY,
2581 "rbd", "dir_get_name",
2582 image_id, image_id_size,
2583 (char *) reply_buf, size,
2584 CEPH_OSD_FLAG_READ, NULL);
2585 if (ret < 0)
2586 goto out;
2587 p = reply_buf;
2588 end = (char *) reply_buf + size;
2589 image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL);
2590 if (IS_ERR(image_name))
2591 image_name = NULL;
2592 else
2593 dout("%s: name is %s len is %zd\n", __func__, image_name, len);
2594out:
2595 kfree(reply_buf);
2596 kfree(image_id);
2597
2598 return image_name;
2599}
2600
2601/*
2602 * When a parent image gets probed, we only have the pool, image,
2603 * and snapshot ids but not the names of any of them. This call
2604 * is made later to fill in those names. It has to be done after
2605 * rbd_dev_snaps_update() has completed because some of the
2606 * information (in particular, snapshot name) is not available
2607 * until then.
2608 */
2609static int rbd_dev_probe_update_spec(struct rbd_device *rbd_dev)
2610{
2611 struct ceph_osd_client *osdc;
2612 const char *name;
2613 void *reply_buf = NULL;
2614 int ret;
2615
2616 if (rbd_dev->spec->pool_name)
2617 return 0; /* Already have the names */
2618
2619 /* Look up the pool name */
2620
2621 osdc = &rbd_dev->rbd_client->client->osdc;
2622 name = ceph_pg_pool_name_by_id(osdc->osdmap, rbd_dev->spec->pool_id);
2623 if (!name)
2624 return -EIO; /* pool id too large (>= 2^31) */
2625
2626 rbd_dev->spec->pool_name = kstrdup(name, GFP_KERNEL);
2627 if (!rbd_dev->spec->pool_name)
2628 return -ENOMEM;
2629
2630 /* Fetch the image name; tolerate failure here */
2631
2632 name = rbd_dev_image_name(rbd_dev);
Alex Elder69e7a022012-11-01 08:39:26 -05002633 if (name)
Alex Elder9e15b772012-10-30 19:40:33 -05002634 rbd_dev->spec->image_name = (char *) name;
Alex Elder69e7a022012-11-01 08:39:26 -05002635 else
Alex Elder9e15b772012-10-30 19:40:33 -05002636 pr_warning(RBD_DRV_NAME "%d "
2637 "unable to get image name for image id %s\n",
2638 rbd_dev->major, rbd_dev->spec->image_id);
Alex Elder9e15b772012-10-30 19:40:33 -05002639
2640 /* Look up the snapshot name. */
2641
2642 name = rbd_snap_name(rbd_dev, rbd_dev->spec->snap_id);
2643 if (!name) {
2644 ret = -EIO;
2645 goto out_err;
2646 }
2647 rbd_dev->spec->snap_name = kstrdup(name, GFP_KERNEL);
2648 if(!rbd_dev->spec->snap_name)
2649 goto out_err;
2650
2651 return 0;
2652out_err:
2653 kfree(reply_buf);
2654 kfree(rbd_dev->spec->pool_name);
2655 rbd_dev->spec->pool_name = NULL;
2656
2657 return ret;
2658}
2659
Alex Elder6e14b1a2012-07-03 16:01:19 -05002660static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev, u64 *ver)
Alex Elder35d489f2012-07-03 16:01:19 -05002661{
2662 size_t size;
2663 int ret;
2664 void *reply_buf;
2665 void *p;
2666 void *end;
2667 u64 seq;
2668 u32 snap_count;
2669 struct ceph_snap_context *snapc;
2670 u32 i;
2671
2672 /*
2673 * We'll need room for the seq value (maximum snapshot id),
2674 * snapshot count, and array of that many snapshot ids.
2675 * For now we have a fixed upper limit on the number we're
2676 * prepared to receive.
2677 */
2678 size = sizeof (__le64) + sizeof (__le32) +
2679 RBD_MAX_SNAP_COUNT * sizeof (__le64);
2680 reply_buf = kzalloc(size, GFP_KERNEL);
2681 if (!reply_buf)
2682 return -ENOMEM;
2683
2684 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2685 "rbd", "get_snapcontext",
2686 NULL, 0,
2687 reply_buf, size,
Alex Elder6e14b1a2012-07-03 16:01:19 -05002688 CEPH_OSD_FLAG_READ, ver);
Alex Elder35d489f2012-07-03 16:01:19 -05002689 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2690 if (ret < 0)
2691 goto out;
2692
2693 ret = -ERANGE;
2694 p = reply_buf;
2695 end = (char *) reply_buf + size;
2696 ceph_decode_64_safe(&p, end, seq, out);
2697 ceph_decode_32_safe(&p, end, snap_count, out);
2698
2699 /*
2700 * Make sure the reported number of snapshot ids wouldn't go
2701 * beyond the end of our buffer. But before checking that,
2702 * make sure the computed size of the snapshot context we
2703 * allocate is representable in a size_t.
2704 */
2705 if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
2706 / sizeof (u64)) {
2707 ret = -EINVAL;
2708 goto out;
2709 }
2710 if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
2711 goto out;
2712
2713 size = sizeof (struct ceph_snap_context) +
2714 snap_count * sizeof (snapc->snaps[0]);
2715 snapc = kmalloc(size, GFP_KERNEL);
2716 if (!snapc) {
2717 ret = -ENOMEM;
2718 goto out;
2719 }
2720
2721 atomic_set(&snapc->nref, 1);
2722 snapc->seq = seq;
2723 snapc->num_snaps = snap_count;
2724 for (i = 0; i < snap_count; i++)
2725 snapc->snaps[i] = ceph_decode_64(&p);
2726
2727 rbd_dev->header.snapc = snapc;
2728
2729 dout(" snap context seq = %llu, snap_count = %u\n",
2730 (unsigned long long) seq, (unsigned int) snap_count);
2731
2732out:
2733 kfree(reply_buf);
2734
2735 return 0;
2736}
2737
Alex Elderb8b1e2d2012-07-03 16:01:19 -05002738static char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, u32 which)
2739{
2740 size_t size;
2741 void *reply_buf;
2742 __le64 snap_id;
2743 int ret;
2744 void *p;
2745 void *end;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05002746 char *snap_name;
2747
2748 size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN;
2749 reply_buf = kmalloc(size, GFP_KERNEL);
2750 if (!reply_buf)
2751 return ERR_PTR(-ENOMEM);
2752
2753 snap_id = cpu_to_le64(rbd_dev->header.snapc->snaps[which]);
2754 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2755 "rbd", "get_snapshot_name",
2756 (char *) &snap_id, sizeof (snap_id),
2757 reply_buf, size,
2758 CEPH_OSD_FLAG_READ, NULL);
2759 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2760 if (ret < 0)
2761 goto out;
2762
2763 p = reply_buf;
2764 end = (char *) reply_buf + size;
Alex Eldere5c35532012-10-25 23:34:41 -05002765 snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05002766 if (IS_ERR(snap_name)) {
2767 ret = PTR_ERR(snap_name);
2768 goto out;
2769 } else {
2770 dout(" snap_id 0x%016llx snap_name = %s\n",
2771 (unsigned long long) le64_to_cpu(snap_id), snap_name);
2772 }
2773 kfree(reply_buf);
2774
2775 return snap_name;
2776out:
2777 kfree(reply_buf);
2778
2779 return ERR_PTR(ret);
2780}
2781
2782static char *rbd_dev_v2_snap_info(struct rbd_device *rbd_dev, u32 which,
2783 u64 *snap_size, u64 *snap_features)
2784{
2785 __le64 snap_id;
2786 u8 order;
2787 int ret;
2788
2789 snap_id = rbd_dev->header.snapc->snaps[which];
2790 ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, &order, snap_size);
2791 if (ret)
2792 return ERR_PTR(ret);
2793 ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, snap_features);
2794 if (ret)
2795 return ERR_PTR(ret);
2796
2797 return rbd_dev_v2_snap_name(rbd_dev, which);
2798}
2799
2800static char *rbd_dev_snap_info(struct rbd_device *rbd_dev, u32 which,
2801 u64 *snap_size, u64 *snap_features)
2802{
2803 if (rbd_dev->image_format == 1)
2804 return rbd_dev_v1_snap_info(rbd_dev, which,
2805 snap_size, snap_features);
2806 if (rbd_dev->image_format == 2)
2807 return rbd_dev_v2_snap_info(rbd_dev, which,
2808 snap_size, snap_features);
2809 return ERR_PTR(-EINVAL);
2810}
2811
Alex Elder117973f2012-08-31 17:29:55 -05002812static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev, u64 *hver)
2813{
2814 int ret;
2815 __u8 obj_order;
2816
2817 down_write(&rbd_dev->header_rwsem);
2818
2819 /* Grab old order first, to see if it changes */
2820
2821 obj_order = rbd_dev->header.obj_order,
2822 ret = rbd_dev_v2_image_size(rbd_dev);
2823 if (ret)
2824 goto out;
2825 if (rbd_dev->header.obj_order != obj_order) {
2826 ret = -EIO;
2827 goto out;
2828 }
2829 rbd_update_mapping_size(rbd_dev);
2830
2831 ret = rbd_dev_v2_snap_context(rbd_dev, hver);
2832 dout("rbd_dev_v2_snap_context returned %d\n", ret);
2833 if (ret)
2834 goto out;
2835 ret = rbd_dev_snaps_update(rbd_dev);
2836 dout("rbd_dev_snaps_update returned %d\n", ret);
2837 if (ret)
2838 goto out;
2839 ret = rbd_dev_snaps_register(rbd_dev);
2840 dout("rbd_dev_snaps_register returned %d\n", ret);
2841out:
2842 up_write(&rbd_dev->header_rwsem);
2843
2844 return ret;
2845}
2846
Alex Elder9d475de2012-07-03 16:01:19 -05002847/*
Alex Elder35938152012-08-02 11:29:46 -05002848 * Scan the rbd device's current snapshot list and compare it to the
2849 * newly-received snapshot context. Remove any existing snapshots
2850 * not present in the new snapshot context. Add a new snapshot for
2851 * any snaphots in the snapshot context not in the current list.
2852 * And verify there are no changes to snapshots we already know
2853 * about.
2854 *
2855 * Assumes the snapshots in the snapshot context are sorted by
2856 * snapshot id, highest id first. (Snapshots in the rbd_dev's list
2857 * are also maintained in that order.)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002858 */
Alex Elder304f6802012-08-31 17:29:52 -05002859static int rbd_dev_snaps_update(struct rbd_device *rbd_dev)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002860{
Alex Elder35938152012-08-02 11:29:46 -05002861 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
2862 const u32 snap_count = snapc->num_snaps;
Alex Elder35938152012-08-02 11:29:46 -05002863 struct list_head *head = &rbd_dev->snaps;
2864 struct list_head *links = head->next;
2865 u32 index = 0;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002866
Alex Elder9fcbb802012-08-23 23:48:49 -05002867 dout("%s: snap count is %u\n", __func__, (unsigned int) snap_count);
Alex Elder35938152012-08-02 11:29:46 -05002868 while (index < snap_count || links != head) {
2869 u64 snap_id;
2870 struct rbd_snap *snap;
Alex Eldercd892122012-07-03 16:01:19 -05002871 char *snap_name;
2872 u64 snap_size = 0;
2873 u64 snap_features = 0;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002874
Alex Elder35938152012-08-02 11:29:46 -05002875 snap_id = index < snap_count ? snapc->snaps[index]
2876 : CEPH_NOSNAP;
2877 snap = links != head ? list_entry(links, struct rbd_snap, node)
2878 : NULL;
Alex Elderaafb2302012-09-06 16:00:54 -05002879 rbd_assert(!snap || snap->id != CEPH_NOSNAP);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002880
Alex Elder35938152012-08-02 11:29:46 -05002881 if (snap_id == CEPH_NOSNAP || (snap && snap->id > snap_id)) {
2882 struct list_head *next = links->next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002883
Alex Elder35938152012-08-02 11:29:46 -05002884 /* Existing snapshot not in the new snap context */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002885
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002886 if (rbd_dev->spec->snap_id == snap->id)
Alex Elderdaba5fd2012-10-26 17:25:23 -05002887 rbd_dev->exists = false;
Alex Elder41f38c22012-10-25 23:34:40 -05002888 rbd_remove_snap_dev(snap);
Alex Elder9fcbb802012-08-23 23:48:49 -05002889 dout("%ssnap id %llu has been removed\n",
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002890 rbd_dev->spec->snap_id == snap->id ?
2891 "mapped " : "",
Alex Elder9fcbb802012-08-23 23:48:49 -05002892 (unsigned long long) snap->id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002893
Alex Elder35938152012-08-02 11:29:46 -05002894 /* Done with this list entry; advance */
2895
2896 links = next;
2897 continue;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002898 }
Alex Elder35938152012-08-02 11:29:46 -05002899
Alex Elderb8b1e2d2012-07-03 16:01:19 -05002900 snap_name = rbd_dev_snap_info(rbd_dev, index,
2901 &snap_size, &snap_features);
Alex Eldercd892122012-07-03 16:01:19 -05002902 if (IS_ERR(snap_name))
2903 return PTR_ERR(snap_name);
2904
Alex Elder9fcbb802012-08-23 23:48:49 -05002905 dout("entry %u: snap_id = %llu\n", (unsigned int) snap_count,
2906 (unsigned long long) snap_id);
Alex Elder35938152012-08-02 11:29:46 -05002907 if (!snap || (snap_id != CEPH_NOSNAP && snap->id < snap_id)) {
2908 struct rbd_snap *new_snap;
2909
2910 /* We haven't seen this snapshot before */
2911
Alex Elderc8d18422012-07-10 20:30:11 -05002912 new_snap = __rbd_add_snap_dev(rbd_dev, snap_name,
Alex Eldercd892122012-07-03 16:01:19 -05002913 snap_id, snap_size, snap_features);
Alex Elder9fcbb802012-08-23 23:48:49 -05002914 if (IS_ERR(new_snap)) {
2915 int err = PTR_ERR(new_snap);
2916
2917 dout(" failed to add dev, error %d\n", err);
2918
2919 return err;
2920 }
Alex Elder35938152012-08-02 11:29:46 -05002921
2922 /* New goes before existing, or at end of list */
2923
Alex Elder9fcbb802012-08-23 23:48:49 -05002924 dout(" added dev%s\n", snap ? "" : " at end\n");
Alex Elder35938152012-08-02 11:29:46 -05002925 if (snap)
2926 list_add_tail(&new_snap->node, &snap->node);
2927 else
Alex Elder523f3252012-08-30 00:16:37 -05002928 list_add_tail(&new_snap->node, head);
Alex Elder35938152012-08-02 11:29:46 -05002929 } else {
2930 /* Already have this one */
2931
Alex Elder9fcbb802012-08-23 23:48:49 -05002932 dout(" already present\n");
2933
Alex Eldercd892122012-07-03 16:01:19 -05002934 rbd_assert(snap->size == snap_size);
Alex Elderaafb2302012-09-06 16:00:54 -05002935 rbd_assert(!strcmp(snap->name, snap_name));
Alex Eldercd892122012-07-03 16:01:19 -05002936 rbd_assert(snap->features == snap_features);
Alex Elder35938152012-08-02 11:29:46 -05002937
2938 /* Done with this list entry; advance */
2939
2940 links = links->next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002941 }
Alex Elder35938152012-08-02 11:29:46 -05002942
2943 /* Advance to the next entry in the snapshot context */
2944
2945 index++;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002946 }
Alex Elder9fcbb802012-08-23 23:48:49 -05002947 dout("%s: done\n", __func__);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002948
2949 return 0;
2950}
2951
Alex Elder304f6802012-08-31 17:29:52 -05002952/*
2953 * Scan the list of snapshots and register the devices for any that
2954 * have not already been registered.
2955 */
2956static int rbd_dev_snaps_register(struct rbd_device *rbd_dev)
2957{
2958 struct rbd_snap *snap;
2959 int ret = 0;
2960
2961 dout("%s called\n", __func__);
Alex Elder86ff77b2012-08-31 17:29:53 -05002962 if (WARN_ON(!device_is_registered(&rbd_dev->dev)))
2963 return -EIO;
Alex Elder304f6802012-08-31 17:29:52 -05002964
2965 list_for_each_entry(snap, &rbd_dev->snaps, node) {
2966 if (!rbd_snap_registered(snap)) {
2967 ret = rbd_register_snap_dev(snap, &rbd_dev->dev);
2968 if (ret < 0)
2969 break;
2970 }
2971 }
2972 dout("%s: returning %d\n", __func__, ret);
2973
2974 return ret;
2975}
2976
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002977static int rbd_bus_add_dev(struct rbd_device *rbd_dev)
2978{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002979 struct device *dev;
Alex Eldercd789ab2012-08-30 00:16:38 -05002980 int ret;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002981
2982 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002983
Alex Eldercd789ab2012-08-30 00:16:38 -05002984 dev = &rbd_dev->dev;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002985 dev->bus = &rbd_bus_type;
2986 dev->type = &rbd_device_type;
2987 dev->parent = &rbd_root_dev;
2988 dev->release = rbd_dev_release;
Alex Elderde71a292012-07-03 16:01:19 -05002989 dev_set_name(dev, "%d", rbd_dev->dev_id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002990 ret = device_register(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002991
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002992 mutex_unlock(&ctl_mutex);
Alex Eldercd789ab2012-08-30 00:16:38 -05002993
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002994 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002995}
2996
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002997static void rbd_bus_del_dev(struct rbd_device *rbd_dev)
2998{
2999 device_unregister(&rbd_dev->dev);
3000}
3001
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003002static int rbd_init_watch_dev(struct rbd_device *rbd_dev)
3003{
3004 int ret, rc;
3005
3006 do {
Alex Elder0e6f3222012-07-25 09:32:40 -05003007 ret = rbd_req_sync_watch(rbd_dev);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003008 if (ret == -ERANGE) {
Alex Elder117973f2012-08-31 17:29:55 -05003009 rc = rbd_dev_refresh(rbd_dev, NULL);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003010 if (rc < 0)
3011 return rc;
3012 }
3013 } while (ret == -ERANGE);
3014
3015 return ret;
3016}
3017
Alex Eldere2839302012-08-29 17:11:06 -05003018static atomic64_t rbd_dev_id_max = ATOMIC64_INIT(0);
Alex Elder1ddbe942012-01-29 13:57:44 -06003019
3020/*
Alex Elder499afd52012-02-02 08:13:29 -06003021 * Get a unique rbd identifier for the given new rbd_dev, and add
3022 * the rbd_dev to the global list. The minimum rbd id is 1.
Alex Elder1ddbe942012-01-29 13:57:44 -06003023 */
Alex Eldere2839302012-08-29 17:11:06 -05003024static void rbd_dev_id_get(struct rbd_device *rbd_dev)
Alex Elderb7f23c32012-01-29 13:57:43 -06003025{
Alex Eldere2839302012-08-29 17:11:06 -05003026 rbd_dev->dev_id = atomic64_inc_return(&rbd_dev_id_max);
Alex Elder499afd52012-02-02 08:13:29 -06003027
3028 spin_lock(&rbd_dev_list_lock);
3029 list_add_tail(&rbd_dev->node, &rbd_dev_list);
3030 spin_unlock(&rbd_dev_list_lock);
Alex Eldere2839302012-08-29 17:11:06 -05003031 dout("rbd_dev %p given dev id %llu\n", rbd_dev,
3032 (unsigned long long) rbd_dev->dev_id);
Alex Elder1ddbe942012-01-29 13:57:44 -06003033}
Alex Elderb7f23c32012-01-29 13:57:43 -06003034
Alex Elder1ddbe942012-01-29 13:57:44 -06003035/*
Alex Elder499afd52012-02-02 08:13:29 -06003036 * Remove an rbd_dev from the global list, and record that its
3037 * identifier is no longer in use.
Alex Elder1ddbe942012-01-29 13:57:44 -06003038 */
Alex Eldere2839302012-08-29 17:11:06 -05003039static void rbd_dev_id_put(struct rbd_device *rbd_dev)
Alex Elder1ddbe942012-01-29 13:57:44 -06003040{
Alex Elderd184f6b2012-01-29 13:57:44 -06003041 struct list_head *tmp;
Alex Elderde71a292012-07-03 16:01:19 -05003042 int rbd_id = rbd_dev->dev_id;
Alex Elderd184f6b2012-01-29 13:57:44 -06003043 int max_id;
3044
Alex Elderaafb2302012-09-06 16:00:54 -05003045 rbd_assert(rbd_id > 0);
Alex Elder499afd52012-02-02 08:13:29 -06003046
Alex Eldere2839302012-08-29 17:11:06 -05003047 dout("rbd_dev %p released dev id %llu\n", rbd_dev,
3048 (unsigned long long) rbd_dev->dev_id);
Alex Elder499afd52012-02-02 08:13:29 -06003049 spin_lock(&rbd_dev_list_lock);
3050 list_del_init(&rbd_dev->node);
Alex Elderd184f6b2012-01-29 13:57:44 -06003051
3052 /*
3053 * If the id being "put" is not the current maximum, there
3054 * is nothing special we need to do.
3055 */
Alex Eldere2839302012-08-29 17:11:06 -05003056 if (rbd_id != atomic64_read(&rbd_dev_id_max)) {
Alex Elderd184f6b2012-01-29 13:57:44 -06003057 spin_unlock(&rbd_dev_list_lock);
3058 return;
3059 }
3060
3061 /*
3062 * We need to update the current maximum id. Search the
3063 * list to find out what it is. We're more likely to find
3064 * the maximum at the end, so search the list backward.
3065 */
3066 max_id = 0;
3067 list_for_each_prev(tmp, &rbd_dev_list) {
3068 struct rbd_device *rbd_dev;
3069
3070 rbd_dev = list_entry(tmp, struct rbd_device, node);
Alex Elderb213e0b2012-10-10 21:19:13 -07003071 if (rbd_dev->dev_id > max_id)
3072 max_id = rbd_dev->dev_id;
Alex Elderd184f6b2012-01-29 13:57:44 -06003073 }
Alex Elder499afd52012-02-02 08:13:29 -06003074 spin_unlock(&rbd_dev_list_lock);
Alex Elderb7f23c32012-01-29 13:57:43 -06003075
Alex Elder1ddbe942012-01-29 13:57:44 -06003076 /*
Alex Eldere2839302012-08-29 17:11:06 -05003077 * The max id could have been updated by rbd_dev_id_get(), in
Alex Elderd184f6b2012-01-29 13:57:44 -06003078 * which case it now accurately reflects the new maximum.
3079 * Be careful not to overwrite the maximum value in that
3080 * case.
Alex Elder1ddbe942012-01-29 13:57:44 -06003081 */
Alex Eldere2839302012-08-29 17:11:06 -05003082 atomic64_cmpxchg(&rbd_dev_id_max, rbd_id, max_id);
3083 dout(" max dev id has been reset\n");
Alex Elderb7f23c32012-01-29 13:57:43 -06003084}
3085
Alex Eldera725f65e2012-02-02 08:13:30 -06003086/*
Alex Eldere28fff262012-02-02 08:13:30 -06003087 * Skips over white space at *buf, and updates *buf to point to the
3088 * first found non-space character (if any). Returns the length of
Alex Elder593a9e72012-02-07 12:03:37 -06003089 * the token (string of non-white space characters) found. Note
3090 * that *buf must be terminated with '\0'.
Alex Eldere28fff262012-02-02 08:13:30 -06003091 */
3092static inline size_t next_token(const char **buf)
3093{
3094 /*
3095 * These are the characters that produce nonzero for
3096 * isspace() in the "C" and "POSIX" locales.
3097 */
3098 const char *spaces = " \f\n\r\t\v";
3099
3100 *buf += strspn(*buf, spaces); /* Find start of token */
3101
3102 return strcspn(*buf, spaces); /* Return token length */
3103}
3104
3105/*
3106 * Finds the next token in *buf, and if the provided token buffer is
3107 * big enough, copies the found token into it. The result, if
Alex Elder593a9e72012-02-07 12:03:37 -06003108 * copied, is guaranteed to be terminated with '\0'. Note that *buf
3109 * must be terminated with '\0' on entry.
Alex Eldere28fff262012-02-02 08:13:30 -06003110 *
3111 * Returns the length of the token found (not including the '\0').
3112 * Return value will be 0 if no token is found, and it will be >=
3113 * token_size if the token would not fit.
3114 *
Alex Elder593a9e72012-02-07 12:03:37 -06003115 * The *buf pointer will be updated to point beyond the end of the
Alex Eldere28fff262012-02-02 08:13:30 -06003116 * found token. Note that this occurs even if the token buffer is
3117 * too small to hold it.
3118 */
3119static inline size_t copy_token(const char **buf,
3120 char *token,
3121 size_t token_size)
3122{
3123 size_t len;
3124
3125 len = next_token(buf);
3126 if (len < token_size) {
3127 memcpy(token, *buf, len);
3128 *(token + len) = '\0';
3129 }
3130 *buf += len;
3131
3132 return len;
3133}
3134
3135/*
Alex Elderea3352f2012-07-09 21:04:23 -05003136 * Finds the next token in *buf, dynamically allocates a buffer big
3137 * enough to hold a copy of it, and copies the token into the new
3138 * buffer. The copy is guaranteed to be terminated with '\0'. Note
3139 * that a duplicate buffer is created even for a zero-length token.
3140 *
3141 * Returns a pointer to the newly-allocated duplicate, or a null
3142 * pointer if memory for the duplicate was not available. If
3143 * the lenp argument is a non-null pointer, the length of the token
3144 * (not including the '\0') is returned in *lenp.
3145 *
3146 * If successful, the *buf pointer will be updated to point beyond
3147 * the end of the found token.
3148 *
3149 * Note: uses GFP_KERNEL for allocation.
3150 */
3151static inline char *dup_token(const char **buf, size_t *lenp)
3152{
3153 char *dup;
3154 size_t len;
3155
3156 len = next_token(buf);
3157 dup = kmalloc(len + 1, GFP_KERNEL);
3158 if (!dup)
3159 return NULL;
3160
3161 memcpy(dup, *buf, len);
3162 *(dup + len) = '\0';
3163 *buf += len;
3164
3165 if (lenp)
3166 *lenp = len;
3167
3168 return dup;
3169}
3170
3171/*
Alex Elder859c31d2012-10-25 23:34:42 -05003172 * Parse the options provided for an "rbd add" (i.e., rbd image
3173 * mapping) request. These arrive via a write to /sys/bus/rbd/add,
3174 * and the data written is passed here via a NUL-terminated buffer.
3175 * Returns 0 if successful or an error code otherwise.
Alex Elderd22f76e2012-07-12 10:46:35 -05003176 *
Alex Elder859c31d2012-10-25 23:34:42 -05003177 * The information extracted from these options is recorded in
3178 * the other parameters which return dynamically-allocated
3179 * structures:
3180 * ceph_opts
3181 * The address of a pointer that will refer to a ceph options
3182 * structure. Caller must release the returned pointer using
3183 * ceph_destroy_options() when it is no longer needed.
3184 * rbd_opts
3185 * Address of an rbd options pointer. Fully initialized by
3186 * this function; caller must release with kfree().
3187 * spec
3188 * Address of an rbd image specification pointer. Fully
3189 * initialized by this function based on parsed options.
3190 * Caller must release with rbd_spec_put().
3191 *
3192 * The options passed take this form:
3193 * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
3194 * where:
3195 * <mon_addrs>
3196 * A comma-separated list of one or more monitor addresses.
3197 * A monitor address is an ip address, optionally followed
3198 * by a port number (separated by a colon).
3199 * I.e.: ip1[:port1][,ip2[:port2]...]
3200 * <options>
3201 * A comma-separated list of ceph and/or rbd options.
3202 * <pool_name>
3203 * The name of the rados pool containing the rbd image.
3204 * <image_name>
3205 * The name of the image in that pool to map.
3206 * <snap_id>
3207 * An optional snapshot id. If provided, the mapping will
3208 * present data from the image at the time that snapshot was
3209 * created. The image head is used if no snapshot id is
3210 * provided. Snapshot mappings are always read-only.
Alex Eldera725f65e2012-02-02 08:13:30 -06003211 */
Alex Elder859c31d2012-10-25 23:34:42 -05003212static int rbd_add_parse_args(const char *buf,
Alex Elderdc79b112012-10-25 23:34:41 -05003213 struct ceph_options **ceph_opts,
Alex Elder859c31d2012-10-25 23:34:42 -05003214 struct rbd_options **opts,
3215 struct rbd_spec **rbd_spec)
Alex Eldera725f65e2012-02-02 08:13:30 -06003216{
Alex Elderd22f76e2012-07-12 10:46:35 -05003217 size_t len;
Alex Elder859c31d2012-10-25 23:34:42 -05003218 char *options;
Alex Elder0ddebc02012-10-25 23:34:41 -05003219 const char *mon_addrs;
3220 size_t mon_addrs_size;
Alex Elder859c31d2012-10-25 23:34:42 -05003221 struct rbd_spec *spec = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05003222 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05003223 struct ceph_options *copts;
Alex Elderdc79b112012-10-25 23:34:41 -05003224 int ret;
Alex Eldere28fff262012-02-02 08:13:30 -06003225
3226 /* The first four tokens are required */
3227
Alex Elder7ef32142012-02-02 08:13:30 -06003228 len = next_token(&buf);
3229 if (!len)
Alex Elderdc79b112012-10-25 23:34:41 -05003230 return -EINVAL; /* Missing monitor address(es) */
Alex Elder0ddebc02012-10-25 23:34:41 -05003231 mon_addrs = buf;
Alex Elderf28e5652012-10-25 23:34:41 -05003232 mon_addrs_size = len + 1;
Alex Elder7ef32142012-02-02 08:13:30 -06003233 buf += len;
Alex Eldera725f65e2012-02-02 08:13:30 -06003234
Alex Elderdc79b112012-10-25 23:34:41 -05003235 ret = -EINVAL;
Alex Elderf28e5652012-10-25 23:34:41 -05003236 options = dup_token(&buf, NULL);
3237 if (!options)
Alex Elderdc79b112012-10-25 23:34:41 -05003238 return -ENOMEM;
Alex Elderf28e5652012-10-25 23:34:41 -05003239 if (!*options)
3240 goto out_err; /* Missing options */
Alex Eldera725f65e2012-02-02 08:13:30 -06003241
Alex Elder859c31d2012-10-25 23:34:42 -05003242 spec = rbd_spec_alloc();
3243 if (!spec)
Alex Elderf28e5652012-10-25 23:34:41 -05003244 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05003245
3246 spec->pool_name = dup_token(&buf, NULL);
3247 if (!spec->pool_name)
3248 goto out_mem;
3249 if (!*spec->pool_name)
Alex Elderf28e5652012-10-25 23:34:41 -05003250 goto out_err; /* Missing pool name */
Alex Eldere28fff262012-02-02 08:13:30 -06003251
Alex Elder69e7a022012-11-01 08:39:26 -05003252 spec->image_name = dup_token(&buf, NULL);
Alex Elder859c31d2012-10-25 23:34:42 -05003253 if (!spec->image_name)
Alex Elderf28e5652012-10-25 23:34:41 -05003254 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05003255 if (!*spec->image_name)
Alex Elderf28e5652012-10-25 23:34:41 -05003256 goto out_err; /* Missing image name */
Alex Eldere28fff262012-02-02 08:13:30 -06003257
Alex Elderf28e5652012-10-25 23:34:41 -05003258 /*
3259 * Snapshot name is optional; default is to use "-"
3260 * (indicating the head/no snapshot).
3261 */
Alex Elder3feeb8942012-08-31 17:29:52 -05003262 len = next_token(&buf);
Alex Elder820a5f32012-07-09 21:04:24 -05003263 if (!len) {
Alex Elder3feeb8942012-08-31 17:29:52 -05003264 buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
3265 len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
Alex Elderf28e5652012-10-25 23:34:41 -05003266 } else if (len > RBD_MAX_SNAP_NAME_LEN) {
Alex Elderdc79b112012-10-25 23:34:41 -05003267 ret = -ENAMETOOLONG;
Alex Elderf28e5652012-10-25 23:34:41 -05003268 goto out_err;
Alex Elder849b4262012-07-09 21:04:24 -05003269 }
Alex Elder859c31d2012-10-25 23:34:42 -05003270 spec->snap_name = kmalloc(len + 1, GFP_KERNEL);
3271 if (!spec->snap_name)
Alex Elderf28e5652012-10-25 23:34:41 -05003272 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05003273 memcpy(spec->snap_name, buf, len);
3274 *(spec->snap_name + len) = '\0';
Alex Eldere5c35532012-10-25 23:34:41 -05003275
Alex Elder0ddebc02012-10-25 23:34:41 -05003276 /* Initialize all rbd options to the defaults */
Alex Eldere28fff262012-02-02 08:13:30 -06003277
Alex Elder4e9afeb2012-10-25 23:34:41 -05003278 rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL);
3279 if (!rbd_opts)
3280 goto out_mem;
3281
3282 rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
Alex Elderd22f76e2012-07-12 10:46:35 -05003283
Alex Elder859c31d2012-10-25 23:34:42 -05003284 copts = ceph_parse_options(options, mon_addrs,
Alex Elder0ddebc02012-10-25 23:34:41 -05003285 mon_addrs + mon_addrs_size - 1,
Alex Elder4e9afeb2012-10-25 23:34:41 -05003286 parse_rbd_opts_token, rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05003287 if (IS_ERR(copts)) {
3288 ret = PTR_ERR(copts);
Alex Elderdc79b112012-10-25 23:34:41 -05003289 goto out_err;
3290 }
Alex Elder859c31d2012-10-25 23:34:42 -05003291 kfree(options);
3292
3293 *ceph_opts = copts;
Alex Elder4e9afeb2012-10-25 23:34:41 -05003294 *opts = rbd_opts;
Alex Elder859c31d2012-10-25 23:34:42 -05003295 *rbd_spec = spec;
Alex Elder0ddebc02012-10-25 23:34:41 -05003296
Alex Elderdc79b112012-10-25 23:34:41 -05003297 return 0;
Alex Elderf28e5652012-10-25 23:34:41 -05003298out_mem:
Alex Elderdc79b112012-10-25 23:34:41 -05003299 ret = -ENOMEM;
Alex Elderd22f76e2012-07-12 10:46:35 -05003300out_err:
Alex Elder859c31d2012-10-25 23:34:42 -05003301 kfree(rbd_opts);
3302 rbd_spec_put(spec);
Alex Elderf28e5652012-10-25 23:34:41 -05003303 kfree(options);
Alex Elderd22f76e2012-07-12 10:46:35 -05003304
Alex Elderdc79b112012-10-25 23:34:41 -05003305 return ret;
Alex Eldera725f65e2012-02-02 08:13:30 -06003306}
3307
Alex Elder589d30e2012-07-10 20:30:11 -05003308/*
3309 * An rbd format 2 image has a unique identifier, distinct from the
3310 * name given to it by the user. Internally, that identifier is
3311 * what's used to specify the names of objects related to the image.
3312 *
3313 * A special "rbd id" object is used to map an rbd image name to its
3314 * id. If that object doesn't exist, then there is no v2 rbd image
3315 * with the supplied name.
3316 *
3317 * This function will record the given rbd_dev's image_id field if
3318 * it can be determined, and in that case will return 0. If any
3319 * errors occur a negative errno will be returned and the rbd_dev's
3320 * image_id field will be unchanged (and should be NULL).
3321 */
3322static int rbd_dev_image_id(struct rbd_device *rbd_dev)
3323{
3324 int ret;
3325 size_t size;
3326 char *object_name;
3327 void *response;
3328 void *p;
3329
3330 /*
Alex Elder2c0d0a12012-10-30 19:40:33 -05003331 * When probing a parent image, the image id is already
3332 * known (and the image name likely is not). There's no
3333 * need to fetch the image id again in this case.
3334 */
3335 if (rbd_dev->spec->image_id)
3336 return 0;
3337
3338 /*
Alex Elder589d30e2012-07-10 20:30:11 -05003339 * First, see if the format 2 image id file exists, and if
3340 * so, get the image's persistent id from it.
3341 */
Alex Elder69e7a022012-11-01 08:39:26 -05003342 size = sizeof (RBD_ID_PREFIX) + strlen(rbd_dev->spec->image_name);
Alex Elder589d30e2012-07-10 20:30:11 -05003343 object_name = kmalloc(size, GFP_NOIO);
3344 if (!object_name)
3345 return -ENOMEM;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003346 sprintf(object_name, "%s%s", RBD_ID_PREFIX, rbd_dev->spec->image_name);
Alex Elder589d30e2012-07-10 20:30:11 -05003347 dout("rbd id object name is %s\n", object_name);
3348
3349 /* Response will be an encoded string, which includes a length */
3350
3351 size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
3352 response = kzalloc(size, GFP_NOIO);
3353 if (!response) {
3354 ret = -ENOMEM;
3355 goto out;
3356 }
3357
3358 ret = rbd_req_sync_exec(rbd_dev, object_name,
3359 "rbd", "get_id",
3360 NULL, 0,
3361 response, RBD_IMAGE_ID_LEN_MAX,
3362 CEPH_OSD_FLAG_READ, NULL);
3363 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
3364 if (ret < 0)
3365 goto out;
Alex Eldera0ea3a42012-10-10 21:19:13 -07003366 ret = 0; /* rbd_req_sync_exec() can return positive */
Alex Elder589d30e2012-07-10 20:30:11 -05003367
3368 p = response;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003369 rbd_dev->spec->image_id = ceph_extract_encoded_string(&p,
Alex Elder589d30e2012-07-10 20:30:11 -05003370 p + RBD_IMAGE_ID_LEN_MAX,
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003371 &rbd_dev->spec->image_id_len,
Alex Elder589d30e2012-07-10 20:30:11 -05003372 GFP_NOIO);
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003373 if (IS_ERR(rbd_dev->spec->image_id)) {
3374 ret = PTR_ERR(rbd_dev->spec->image_id);
3375 rbd_dev->spec->image_id = NULL;
Alex Elder589d30e2012-07-10 20:30:11 -05003376 } else {
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003377 dout("image_id is %s\n", rbd_dev->spec->image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05003378 }
3379out:
3380 kfree(response);
3381 kfree(object_name);
3382
3383 return ret;
3384}
3385
Alex Eldera30b71b2012-07-10 20:30:11 -05003386static int rbd_dev_v1_probe(struct rbd_device *rbd_dev)
3387{
3388 int ret;
3389 size_t size;
3390
3391 /* Version 1 images have no id; empty string is used */
3392
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003393 rbd_dev->spec->image_id = kstrdup("", GFP_KERNEL);
3394 if (!rbd_dev->spec->image_id)
Alex Eldera30b71b2012-07-10 20:30:11 -05003395 return -ENOMEM;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003396 rbd_dev->spec->image_id_len = 0;
Alex Eldera30b71b2012-07-10 20:30:11 -05003397
3398 /* Record the header object name for this rbd image. */
3399
Alex Elder69e7a022012-11-01 08:39:26 -05003400 size = strlen(rbd_dev->spec->image_name) + sizeof (RBD_SUFFIX);
Alex Eldera30b71b2012-07-10 20:30:11 -05003401 rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
3402 if (!rbd_dev->header_name) {
3403 ret = -ENOMEM;
3404 goto out_err;
3405 }
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003406 sprintf(rbd_dev->header_name, "%s%s",
3407 rbd_dev->spec->image_name, RBD_SUFFIX);
Alex Eldera30b71b2012-07-10 20:30:11 -05003408
3409 /* Populate rbd image metadata */
3410
3411 ret = rbd_read_header(rbd_dev, &rbd_dev->header);
3412 if (ret < 0)
3413 goto out_err;
Alex Elder86b00e02012-10-25 23:34:42 -05003414
3415 /* Version 1 images have no parent (no layering) */
3416
3417 rbd_dev->parent_spec = NULL;
3418 rbd_dev->parent_overlap = 0;
3419
Alex Eldera30b71b2012-07-10 20:30:11 -05003420 rbd_dev->image_format = 1;
3421
3422 dout("discovered version 1 image, header name is %s\n",
3423 rbd_dev->header_name);
3424
3425 return 0;
3426
3427out_err:
3428 kfree(rbd_dev->header_name);
3429 rbd_dev->header_name = NULL;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003430 kfree(rbd_dev->spec->image_id);
3431 rbd_dev->spec->image_id = NULL;
Alex Eldera30b71b2012-07-10 20:30:11 -05003432
3433 return ret;
3434}
3435
3436static int rbd_dev_v2_probe(struct rbd_device *rbd_dev)
3437{
3438 size_t size;
Alex Elder9d475de2012-07-03 16:01:19 -05003439 int ret;
Alex Elder6e14b1a2012-07-03 16:01:19 -05003440 u64 ver = 0;
Alex Eldera30b71b2012-07-10 20:30:11 -05003441
3442 /*
3443 * Image id was filled in by the caller. Record the header
3444 * object name for this rbd image.
3445 */
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003446 size = sizeof (RBD_HEADER_PREFIX) + rbd_dev->spec->image_id_len;
Alex Eldera30b71b2012-07-10 20:30:11 -05003447 rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
3448 if (!rbd_dev->header_name)
3449 return -ENOMEM;
3450 sprintf(rbd_dev->header_name, "%s%s",
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003451 RBD_HEADER_PREFIX, rbd_dev->spec->image_id);
Alex Elder9d475de2012-07-03 16:01:19 -05003452
3453 /* Get the size and object order for the image */
3454
3455 ret = rbd_dev_v2_image_size(rbd_dev);
3456 if (ret < 0)
3457 goto out_err;
Alex Elder1e130192012-07-03 16:01:19 -05003458
3459 /* Get the object prefix (a.k.a. block_name) for the image */
3460
3461 ret = rbd_dev_v2_object_prefix(rbd_dev);
3462 if (ret < 0)
3463 goto out_err;
Alex Elderb1b54022012-07-03 16:01:19 -05003464
Alex Elderd8891402012-10-09 13:50:17 -07003465 /* Get the and check features for the image */
Alex Elderb1b54022012-07-03 16:01:19 -05003466
3467 ret = rbd_dev_v2_features(rbd_dev);
3468 if (ret < 0)
3469 goto out_err;
Alex Elder35d489f2012-07-03 16:01:19 -05003470
Alex Elder86b00e02012-10-25 23:34:42 -05003471 /* If the image supports layering, get the parent info */
3472
3473 if (rbd_dev->header.features & RBD_FEATURE_LAYERING) {
3474 ret = rbd_dev_v2_parent_info(rbd_dev);
3475 if (ret < 0)
3476 goto out_err;
3477 }
3478
Alex Elder6e14b1a2012-07-03 16:01:19 -05003479 /* crypto and compression type aren't (yet) supported for v2 images */
Alex Elder35d489f2012-07-03 16:01:19 -05003480
Alex Elder6e14b1a2012-07-03 16:01:19 -05003481 rbd_dev->header.crypt_type = 0;
3482 rbd_dev->header.comp_type = 0;
3483
3484 /* Get the snapshot context, plus the header version */
3485
3486 ret = rbd_dev_v2_snap_context(rbd_dev, &ver);
Alex Elder35d489f2012-07-03 16:01:19 -05003487 if (ret)
3488 goto out_err;
Alex Elder6e14b1a2012-07-03 16:01:19 -05003489 rbd_dev->header.obj_version = ver;
3490
Alex Eldera30b71b2012-07-10 20:30:11 -05003491 rbd_dev->image_format = 2;
3492
3493 dout("discovered version 2 image, header name is %s\n",
3494 rbd_dev->header_name);
3495
Alex Elder35152972012-08-31 17:29:55 -05003496 return 0;
Alex Elder9d475de2012-07-03 16:01:19 -05003497out_err:
Alex Elder86b00e02012-10-25 23:34:42 -05003498 rbd_dev->parent_overlap = 0;
3499 rbd_spec_put(rbd_dev->parent_spec);
3500 rbd_dev->parent_spec = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05003501 kfree(rbd_dev->header_name);
3502 rbd_dev->header_name = NULL;
Alex Elder1e130192012-07-03 16:01:19 -05003503 kfree(rbd_dev->header.object_prefix);
3504 rbd_dev->header.object_prefix = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05003505
3506 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05003507}
3508
Alex Elder83a06262012-10-30 15:47:17 -05003509static int rbd_dev_probe_finish(struct rbd_device *rbd_dev)
3510{
3511 int ret;
3512
3513 /* no need to lock here, as rbd_dev is not registered yet */
3514 ret = rbd_dev_snaps_update(rbd_dev);
3515 if (ret)
3516 return ret;
3517
Alex Elder9e15b772012-10-30 19:40:33 -05003518 ret = rbd_dev_probe_update_spec(rbd_dev);
3519 if (ret)
3520 goto err_out_snaps;
3521
Alex Elder83a06262012-10-30 15:47:17 -05003522 ret = rbd_dev_set_mapping(rbd_dev);
3523 if (ret)
3524 goto err_out_snaps;
3525
3526 /* generate unique id: find highest unique id, add one */
3527 rbd_dev_id_get(rbd_dev);
3528
3529 /* Fill in the device name, now that we have its id. */
3530 BUILD_BUG_ON(DEV_NAME_LEN
3531 < sizeof (RBD_DRV_NAME) + MAX_INT_FORMAT_WIDTH);
3532 sprintf(rbd_dev->name, "%s%d", RBD_DRV_NAME, rbd_dev->dev_id);
3533
3534 /* Get our block major device number. */
3535
3536 ret = register_blkdev(0, rbd_dev->name);
3537 if (ret < 0)
3538 goto err_out_id;
3539 rbd_dev->major = ret;
3540
3541 /* Set up the blkdev mapping. */
3542
3543 ret = rbd_init_disk(rbd_dev);
3544 if (ret)
3545 goto err_out_blkdev;
3546
3547 ret = rbd_bus_add_dev(rbd_dev);
3548 if (ret)
3549 goto err_out_disk;
3550
3551 /*
3552 * At this point cleanup in the event of an error is the job
3553 * of the sysfs code (initiated by rbd_bus_del_dev()).
3554 */
3555 down_write(&rbd_dev->header_rwsem);
3556 ret = rbd_dev_snaps_register(rbd_dev);
3557 up_write(&rbd_dev->header_rwsem);
3558 if (ret)
3559 goto err_out_bus;
3560
3561 ret = rbd_init_watch_dev(rbd_dev);
3562 if (ret)
3563 goto err_out_bus;
3564
3565 /* Everything's ready. Announce the disk to the world. */
3566
3567 add_disk(rbd_dev->disk);
3568
3569 pr_info("%s: added with size 0x%llx\n", rbd_dev->disk->disk_name,
3570 (unsigned long long) rbd_dev->mapping.size);
3571
3572 return ret;
3573err_out_bus:
3574 /* this will also clean up rest of rbd_dev stuff */
3575
3576 rbd_bus_del_dev(rbd_dev);
3577
3578 return ret;
3579err_out_disk:
3580 rbd_free_disk(rbd_dev);
3581err_out_blkdev:
3582 unregister_blkdev(rbd_dev->major, rbd_dev->name);
3583err_out_id:
3584 rbd_dev_id_put(rbd_dev);
3585err_out_snaps:
3586 rbd_remove_all_snaps(rbd_dev);
3587
3588 return ret;
3589}
3590
Alex Eldera30b71b2012-07-10 20:30:11 -05003591/*
3592 * Probe for the existence of the header object for the given rbd
3593 * device. For format 2 images this includes determining the image
3594 * id.
3595 */
3596static int rbd_dev_probe(struct rbd_device *rbd_dev)
3597{
3598 int ret;
3599
3600 /*
3601 * Get the id from the image id object. If it's not a
3602 * format 2 image, we'll get ENOENT back, and we'll assume
3603 * it's a format 1 image.
3604 */
3605 ret = rbd_dev_image_id(rbd_dev);
3606 if (ret)
3607 ret = rbd_dev_v1_probe(rbd_dev);
3608 else
3609 ret = rbd_dev_v2_probe(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05003610 if (ret) {
Alex Eldera30b71b2012-07-10 20:30:11 -05003611 dout("probe failed, returning %d\n", ret);
3612
Alex Elder83a06262012-10-30 15:47:17 -05003613 return ret;
3614 }
3615
3616 ret = rbd_dev_probe_finish(rbd_dev);
3617 if (ret)
3618 rbd_header_free(&rbd_dev->header);
3619
Alex Eldera30b71b2012-07-10 20:30:11 -05003620 return ret;
3621}
3622
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003623static ssize_t rbd_add(struct bus_type *bus,
3624 const char *buf,
3625 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003626{
Alex Eldercb8627c2012-07-09 21:04:23 -05003627 struct rbd_device *rbd_dev = NULL;
Alex Elderdc79b112012-10-25 23:34:41 -05003628 struct ceph_options *ceph_opts = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05003629 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05003630 struct rbd_spec *spec = NULL;
Alex Elder9d3997f2012-10-25 23:34:42 -05003631 struct rbd_client *rbdc;
Alex Elder27cc2592012-02-02 08:13:30 -06003632 struct ceph_osd_client *osdc;
3633 int rc = -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003634
3635 if (!try_module_get(THIS_MODULE))
3636 return -ENODEV;
3637
Alex Eldera725f65e2012-02-02 08:13:30 -06003638 /* parse add command */
Alex Elder859c31d2012-10-25 23:34:42 -05003639 rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec);
Alex Elderdc79b112012-10-25 23:34:41 -05003640 if (rc < 0)
Alex Elderbd4ba652012-10-25 23:34:42 -05003641 goto err_out_module;
Alex Eldera725f65e2012-02-02 08:13:30 -06003642
Alex Elder9d3997f2012-10-25 23:34:42 -05003643 rbdc = rbd_get_client(ceph_opts);
3644 if (IS_ERR(rbdc)) {
3645 rc = PTR_ERR(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05003646 goto err_out_args;
Alex Elder9d3997f2012-10-25 23:34:42 -05003647 }
Alex Elderc53d5892012-10-25 23:34:42 -05003648 ceph_opts = NULL; /* rbd_dev client now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003649
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003650 /* pick the pool */
Alex Elder9d3997f2012-10-25 23:34:42 -05003651 osdc = &rbdc->client->osdc;
Alex Elder859c31d2012-10-25 23:34:42 -05003652 rc = ceph_pg_poolid_by_name(osdc->osdmap, spec->pool_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003653 if (rc < 0)
3654 goto err_out_client;
Alex Elder859c31d2012-10-25 23:34:42 -05003655 spec->pool_id = (u64) rc;
3656
Alex Elderc53d5892012-10-25 23:34:42 -05003657 rbd_dev = rbd_dev_create(rbdc, spec);
Alex Elderbd4ba652012-10-25 23:34:42 -05003658 if (!rbd_dev)
3659 goto err_out_client;
Alex Elderc53d5892012-10-25 23:34:42 -05003660 rbdc = NULL; /* rbd_dev now owns this */
3661 spec = NULL; /* rbd_dev now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003662
Alex Elderbd4ba652012-10-25 23:34:42 -05003663 rbd_dev->mapping.read_only = rbd_opts->read_only;
Alex Elderc53d5892012-10-25 23:34:42 -05003664 kfree(rbd_opts);
3665 rbd_opts = NULL; /* done with this */
Alex Elderbd4ba652012-10-25 23:34:42 -05003666
Alex Eldera30b71b2012-07-10 20:30:11 -05003667 rc = rbd_dev_probe(rbd_dev);
3668 if (rc < 0)
Alex Elderc53d5892012-10-25 23:34:42 -05003669 goto err_out_rbd_dev;
Alex Elder05fd6f62012-08-29 17:11:07 -05003670
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003671 return count;
Alex Elderc53d5892012-10-25 23:34:42 -05003672err_out_rbd_dev:
3673 rbd_dev_destroy(rbd_dev);
Alex Elderbd4ba652012-10-25 23:34:42 -05003674err_out_client:
Alex Elder9d3997f2012-10-25 23:34:42 -05003675 rbd_put_client(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05003676err_out_args:
Alex Elder78cea762012-10-25 23:34:41 -05003677 if (ceph_opts)
3678 ceph_destroy_options(ceph_opts);
Alex Elder4e9afeb2012-10-25 23:34:41 -05003679 kfree(rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05003680 rbd_spec_put(spec);
Alex Elderbd4ba652012-10-25 23:34:42 -05003681err_out_module:
3682 module_put(THIS_MODULE);
Alex Elder27cc2592012-02-02 08:13:30 -06003683
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003684 dout("Error adding device %s\n", buf);
Alex Elder27cc2592012-02-02 08:13:30 -06003685
3686 return (ssize_t) rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003687}
3688
Alex Elderde71a292012-07-03 16:01:19 -05003689static struct rbd_device *__rbd_get_dev(unsigned long dev_id)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003690{
3691 struct list_head *tmp;
3692 struct rbd_device *rbd_dev;
3693
Alex Eldere124a82f2012-01-29 13:57:44 -06003694 spin_lock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003695 list_for_each(tmp, &rbd_dev_list) {
3696 rbd_dev = list_entry(tmp, struct rbd_device, node);
Alex Elderde71a292012-07-03 16:01:19 -05003697 if (rbd_dev->dev_id == dev_id) {
Alex Eldere124a82f2012-01-29 13:57:44 -06003698 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003699 return rbd_dev;
Alex Eldere124a82f2012-01-29 13:57:44 -06003700 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003701 }
Alex Eldere124a82f2012-01-29 13:57:44 -06003702 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003703 return NULL;
3704}
3705
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003706static void rbd_dev_release(struct device *dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003707{
Alex Elder593a9e72012-02-07 12:03:37 -06003708 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003709
Alex Elder1dbb4392012-01-24 10:08:37 -06003710 if (rbd_dev->watch_request) {
3711 struct ceph_client *client = rbd_dev->rbd_client->client;
3712
3713 ceph_osdc_unregister_linger_request(&client->osdc,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003714 rbd_dev->watch_request);
Alex Elder1dbb4392012-01-24 10:08:37 -06003715 }
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003716 if (rbd_dev->watch_event)
Alex Elder070c6332012-07-25 09:32:41 -05003717 rbd_req_sync_unwatch(rbd_dev);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003718
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003719
3720 /* clean up and free blkdev */
3721 rbd_free_disk(rbd_dev);
3722 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Alex Elder32eec682012-02-08 16:11:14 -06003723
Alex Elder2ac4e752012-07-10 20:30:10 -05003724 /* release allocated disk header fields */
3725 rbd_header_free(&rbd_dev->header);
3726
Alex Elder32eec682012-02-08 16:11:14 -06003727 /* done with the id, and with the rbd_dev */
Alex Eldere2839302012-08-29 17:11:06 -05003728 rbd_dev_id_put(rbd_dev);
Alex Elderc53d5892012-10-25 23:34:42 -05003729 rbd_assert(rbd_dev->rbd_client != NULL);
3730 rbd_dev_destroy(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003731
3732 /* release module ref */
3733 module_put(THIS_MODULE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003734}
3735
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003736static ssize_t rbd_remove(struct bus_type *bus,
3737 const char *buf,
3738 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003739{
3740 struct rbd_device *rbd_dev = NULL;
3741 int target_id, rc;
3742 unsigned long ul;
3743 int ret = count;
3744
3745 rc = strict_strtoul(buf, 10, &ul);
3746 if (rc)
3747 return rc;
3748
3749 /* convert to int; abort if we lost anything in the conversion */
3750 target_id = (int) ul;
3751 if (target_id != ul)
3752 return -EINVAL;
3753
3754 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
3755
3756 rbd_dev = __rbd_get_dev(target_id);
3757 if (!rbd_dev) {
3758 ret = -ENOENT;
3759 goto done;
3760 }
3761
Alex Elder42382b72012-11-16 09:29:16 -06003762 if (rbd_dev->open_count) {
3763 ret = -EBUSY;
3764 goto done;
3765 }
3766
Alex Elder41f38c22012-10-25 23:34:40 -05003767 rbd_remove_all_snaps(rbd_dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003768 rbd_bus_del_dev(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003769
3770done:
3771 mutex_unlock(&ctl_mutex);
Alex Elderaafb2302012-09-06 16:00:54 -05003772
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003773 return ret;
3774}
3775
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003776/*
3777 * create control files in sysfs
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003778 * /sys/bus/rbd/...
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003779 */
3780static int rbd_sysfs_init(void)
3781{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003782 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003783
Alex Elderfed4c142012-02-07 12:03:36 -06003784 ret = device_register(&rbd_root_dev);
Alex Elder21079782012-01-24 10:08:36 -06003785 if (ret < 0)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003786 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003787
Alex Elderfed4c142012-02-07 12:03:36 -06003788 ret = bus_register(&rbd_bus_type);
3789 if (ret < 0)
3790 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003791
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003792 return ret;
3793}
3794
3795static void rbd_sysfs_cleanup(void)
3796{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003797 bus_unregister(&rbd_bus_type);
Alex Elderfed4c142012-02-07 12:03:36 -06003798 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003799}
3800
3801int __init rbd_init(void)
3802{
3803 int rc;
3804
3805 rc = rbd_sysfs_init();
3806 if (rc)
3807 return rc;
Alex Elderf0f8cef2012-01-29 13:57:44 -06003808 pr_info("loaded " RBD_DRV_NAME_LONG "\n");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003809 return 0;
3810}
3811
3812void __exit rbd_exit(void)
3813{
3814 rbd_sysfs_cleanup();
3815}
3816
3817module_init(rbd_init);
3818module_exit(rbd_exit);
3819
3820MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
3821MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
3822MODULE_DESCRIPTION("rados block device");
3823
3824/* following authorship retained from original osdblk.c */
3825MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
3826
3827MODULE_LICENSE("GPL");