blob: b673a8dc161d019f914012c7c262eb29d6e11baa [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
Yehuda Sadeh602adf42010-08-12 16:11:25 -070064#define RBD_MAX_SNAP_NAME_LEN 32
Alex Elder35d489f2012-07-03 16:01:19 -050065#define RBD_MAX_SNAP_COUNT 510 /* allows max snapc to fit in 4KB */
Yehuda Sadeh602adf42010-08-12 16:11:25 -070066#define RBD_MAX_OPT_LEN 1024
67
68#define RBD_SNAP_HEAD_NAME "-"
69
Alex Elder1e130192012-07-03 16:01:19 -050070#define RBD_IMAGE_ID_LEN_MAX 64
71#define RBD_OBJ_PREFIX_LEN_MAX 64
Alex Elder589d30e2012-07-10 20:30:11 -050072
Alex Elder81a89792012-02-02 08:13:30 -060073/*
74 * An RBD device name will be "rbd#", where the "rbd" comes from
75 * RBD_DRV_NAME above, and # is a unique integer identifier.
76 * MAX_INT_FORMAT_WIDTH is used in ensuring DEV_NAME_LEN is big
77 * enough to hold all possible device names.
78 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -070079#define DEV_NAME_LEN 32
Alex Elder81a89792012-02-02 08:13:30 -060080#define MAX_INT_FORMAT_WIDTH ((5 * sizeof (int)) / 2 + 1)
Yehuda Sadeh602adf42010-08-12 16:11:25 -070081
Alex Eldercc0538b2012-08-10 13:12:07 -070082#define RBD_READ_ONLY_DEFAULT false
Yehuda Sadeh59c2be12011-03-21 15:10:11 -070083
Yehuda Sadeh602adf42010-08-12 16:11:25 -070084/*
85 * block device image metadata (in-memory version)
86 */
87struct rbd_image_header {
Alex Elderf84344f2012-08-31 17:29:51 -050088 /* These four fields never change for a given rbd image */
Alex Elder849b4262012-07-09 21:04:24 -050089 char *object_prefix;
Alex Elder34b13182012-07-13 20:35:12 -050090 u64 features;
Yehuda Sadeh602adf42010-08-12 16:11:25 -070091 __u8 obj_order;
92 __u8 crypt_type;
93 __u8 comp_type;
Yehuda Sadeh602adf42010-08-12 16:11:25 -070094
Alex Elderf84344f2012-08-31 17:29:51 -050095 /* The remaining fields need to be updated occasionally */
96 u64 image_size;
97 struct ceph_snap_context *snapc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -070098 char *snap_names;
99 u64 *snap_sizes;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700100
101 u64 obj_version;
102};
103
104struct rbd_options {
Alex Eldercc0538b2012-08-10 13:12:07 -0700105 bool read_only;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700106};
107
108/*
Alex Elderf0f8cef2012-01-29 13:57:44 -0600109 * an instance of the client. multiple devices may share an rbd client.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700110 */
111struct rbd_client {
112 struct ceph_client *client;
113 struct kref kref;
114 struct list_head node;
115};
116
117/*
Alex Elderf0f8cef2012-01-29 13:57:44 -0600118 * a request completion status
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700119 */
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700120struct rbd_req_status {
121 int done;
122 int rc;
123 u64 bytes;
124};
125
126/*
127 * a collection of requests
128 */
129struct rbd_req_coll {
130 int total;
131 int num_done;
132 struct kref kref;
133 struct rbd_req_status status[0];
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700134};
135
Alex Elderf0f8cef2012-01-29 13:57:44 -0600136/*
137 * a single io request
138 */
139struct rbd_request {
140 struct request *rq; /* blk layer request */
141 struct bio *bio; /* cloned bio */
142 struct page **pages; /* list of used pages */
143 u64 len;
144 int coll_index;
145 struct rbd_req_coll *coll;
146};
147
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800148struct rbd_snap {
149 struct device dev;
150 const char *name;
Josh Durgin3591538f2011-12-05 18:25:13 -0800151 u64 size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800152 struct list_head node;
153 u64 id;
Alex Elder34b13182012-07-13 20:35:12 -0500154 u64 features;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800155};
156
Alex Elderf84344f2012-08-31 17:29:51 -0500157struct rbd_mapping {
158 char *snap_name;
159 u64 snap_id;
Alex Elder99c1f082012-08-30 14:42:15 -0500160 u64 size;
Alex Elder34b13182012-07-13 20:35:12 -0500161 u64 features;
Alex Elderf84344f2012-08-31 17:29:51 -0500162 bool snap_exists;
163 bool read_only;
164};
165
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700166/*
167 * a single device
168 */
169struct rbd_device {
Alex Elderde71a292012-07-03 16:01:19 -0500170 int dev_id; /* blkdev unique id */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700171
172 int major; /* blkdev assigned major */
173 struct gendisk *disk; /* blkdev's gendisk and rq */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700174
Alex Eldera30b71b2012-07-10 20:30:11 -0500175 u32 image_format; /* Either 1 or 2 */
Alex Elderf8c38922012-08-10 13:12:07 -0700176 struct rbd_options rbd_opts;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700177 struct rbd_client *rbd_client;
178
179 char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */
180
181 spinlock_t lock; /* queue lock */
182
183 struct rbd_image_header header;
Alex Elder589d30e2012-07-10 20:30:11 -0500184 char *image_id;
185 size_t image_id_len;
Alex Elder0bed54d2012-07-03 16:01:18 -0500186 char *image_name;
187 size_t image_name_len;
188 char *header_name;
Alex Elderd22f76e2012-07-12 10:46:35 -0500189 char *pool_name;
Alex Elder9bb2f332012-07-12 10:46:35 -0500190 int pool_id;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700191
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700192 struct ceph_osd_event *watch_event;
193 struct ceph_osd_request *watch_request;
194
Josh Durginc6666012011-11-21 17:11:12 -0800195 /* protects updating the header */
196 struct rw_semaphore header_rwsem;
Alex Elderf84344f2012-08-31 17:29:51 -0500197
198 struct rbd_mapping mapping;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700199
200 struct list_head node;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800201
202 /* list of snapshots */
203 struct list_head snaps;
204
205 /* sysfs related */
206 struct device dev;
207};
208
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700209static DEFINE_MUTEX(ctl_mutex); /* Serialize open/close/setup/teardown */
Alex Eldere124a82f2012-01-29 13:57:44 -0600210
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700211static LIST_HEAD(rbd_dev_list); /* devices */
Alex Eldere124a82f2012-01-29 13:57:44 -0600212static DEFINE_SPINLOCK(rbd_dev_list_lock);
213
Alex Elder432b8582012-01-29 13:57:44 -0600214static LIST_HEAD(rbd_client_list); /* clients */
215static DEFINE_SPINLOCK(rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700216
Alex Elder304f6802012-08-31 17:29:52 -0500217static int rbd_dev_snaps_update(struct rbd_device *rbd_dev);
218static int rbd_dev_snaps_register(struct rbd_device *rbd_dev);
219
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800220static void rbd_dev_release(struct device *dev);
Alex Elder14e70852012-07-19 09:09:27 -0500221static void __rbd_remove_snap_dev(struct rbd_snap *snap);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800222
Alex Elderf0f8cef2012-01-29 13:57:44 -0600223static ssize_t rbd_add(struct bus_type *bus, const char *buf,
224 size_t count);
225static ssize_t rbd_remove(struct bus_type *bus, const char *buf,
226 size_t count);
227
228static struct bus_attribute rbd_bus_attrs[] = {
229 __ATTR(add, S_IWUSR, NULL, rbd_add),
230 __ATTR(remove, S_IWUSR, NULL, rbd_remove),
231 __ATTR_NULL
232};
233
234static struct bus_type rbd_bus_type = {
235 .name = "rbd",
236 .bus_attrs = rbd_bus_attrs,
237};
238
239static void rbd_root_dev_release(struct device *dev)
240{
241}
242
243static struct device rbd_root_dev = {
244 .init_name = "rbd",
245 .release = rbd_root_dev_release,
246};
247
Alex Elderaafb2302012-09-06 16:00:54 -0500248#ifdef RBD_DEBUG
249#define rbd_assert(expr) \
250 if (unlikely(!(expr))) { \
251 printk(KERN_ERR "\nAssertion failure in %s() " \
252 "at line %d:\n\n" \
253 "\trbd_assert(%s);\n\n", \
254 __func__, __LINE__, #expr); \
255 BUG(); \
256 }
257#else /* !RBD_DEBUG */
258# define rbd_assert(expr) ((void) 0)
259#endif /* !RBD_DEBUG */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800260
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800261static struct device *rbd_get_dev(struct rbd_device *rbd_dev)
262{
263 return get_device(&rbd_dev->dev);
264}
265
266static void rbd_put_dev(struct rbd_device *rbd_dev)
267{
268 put_device(&rbd_dev->dev);
269}
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700270
Alex Elder1fe5e992012-07-25 09:32:41 -0500271static int rbd_refresh_header(struct rbd_device *rbd_dev, u64 *hver);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700272
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700273static int rbd_open(struct block_device *bdev, fmode_t mode)
274{
Alex Elderf0f8cef2012-01-29 13:57:44 -0600275 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700276
Alex Elderf84344f2012-08-31 17:29:51 -0500277 if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700278 return -EROFS;
279
Alex Elder340c7a22012-08-10 13:12:07 -0700280 rbd_get_dev(rbd_dev);
Alex Elderf84344f2012-08-31 17:29:51 -0500281 set_device_ro(bdev, rbd_dev->mapping.read_only);
Alex Elder340c7a22012-08-10 13:12:07 -0700282
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700283 return 0;
284}
285
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800286static int rbd_release(struct gendisk *disk, fmode_t mode)
287{
288 struct rbd_device *rbd_dev = disk->private_data;
289
290 rbd_put_dev(rbd_dev);
291
292 return 0;
293}
294
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700295static const struct block_device_operations rbd_bd_ops = {
296 .owner = THIS_MODULE,
297 .open = rbd_open,
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800298 .release = rbd_release,
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700299};
300
301/*
302 * Initialize an rbd client instance.
Alex Elder43ae4702012-07-03 16:01:18 -0500303 * We own *ceph_opts.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700304 */
Alex Elderf8c38922012-08-10 13:12:07 -0700305static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700306{
307 struct rbd_client *rbdc;
308 int ret = -ENOMEM;
309
310 dout("rbd_client_create\n");
311 rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL);
312 if (!rbdc)
313 goto out_opt;
314
315 kref_init(&rbdc->kref);
316 INIT_LIST_HEAD(&rbdc->node);
317
Alex Elderbc534d82012-01-29 13:57:44 -0600318 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
319
Alex Elder43ae4702012-07-03 16:01:18 -0500320 rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700321 if (IS_ERR(rbdc->client))
Alex Elderbc534d82012-01-29 13:57:44 -0600322 goto out_mutex;
Alex Elder43ae4702012-07-03 16:01:18 -0500323 ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700324
325 ret = ceph_open_session(rbdc->client);
326 if (ret < 0)
327 goto out_err;
328
Alex Elder432b8582012-01-29 13:57:44 -0600329 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700330 list_add_tail(&rbdc->node, &rbd_client_list);
Alex Elder432b8582012-01-29 13:57:44 -0600331 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700332
Alex Elderbc534d82012-01-29 13:57:44 -0600333 mutex_unlock(&ctl_mutex);
334
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700335 dout("rbd_client_create created %p\n", rbdc);
336 return rbdc;
337
338out_err:
339 ceph_destroy_client(rbdc->client);
Alex Elderbc534d82012-01-29 13:57:44 -0600340out_mutex:
341 mutex_unlock(&ctl_mutex);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700342 kfree(rbdc);
343out_opt:
Alex Elder43ae4702012-07-03 16:01:18 -0500344 if (ceph_opts)
345 ceph_destroy_options(ceph_opts);
Vasiliy Kulikov28f259b2010-09-26 12:59:37 +0400346 return ERR_PTR(ret);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700347}
348
349/*
Alex Elder1f7ba332012-08-10 13:12:07 -0700350 * Find a ceph client with specific addr and configuration. If
351 * found, bump its reference count.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700352 */
Alex Elder1f7ba332012-08-10 13:12:07 -0700353static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700354{
355 struct rbd_client *client_node;
Alex Elder1f7ba332012-08-10 13:12:07 -0700356 bool found = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700357
Alex Elder43ae4702012-07-03 16:01:18 -0500358 if (ceph_opts->flags & CEPH_OPT_NOSHARE)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700359 return NULL;
360
Alex Elder1f7ba332012-08-10 13:12:07 -0700361 spin_lock(&rbd_client_list_lock);
362 list_for_each_entry(client_node, &rbd_client_list, node) {
363 if (!ceph_compare_options(ceph_opts, client_node->client)) {
364 kref_get(&client_node->kref);
365 found = true;
366 break;
367 }
368 }
369 spin_unlock(&rbd_client_list_lock);
370
371 return found ? client_node : NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700372}
373
374/*
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700375 * mount options
376 */
377enum {
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700378 Opt_last_int,
379 /* int args above */
380 Opt_last_string,
381 /* string args above */
Alex Eldercc0538b2012-08-10 13:12:07 -0700382 Opt_read_only,
383 Opt_read_write,
384 /* Boolean args above */
385 Opt_last_bool,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700386};
387
Alex Elder43ae4702012-07-03 16:01:18 -0500388static match_table_t rbd_opts_tokens = {
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700389 /* int args above */
390 /* string args above */
Alex Elderf84344f2012-08-31 17:29:51 -0500391 {Opt_read_only, "mapping.read_only"},
Alex Eldercc0538b2012-08-10 13:12:07 -0700392 {Opt_read_only, "ro"}, /* Alternate spelling */
393 {Opt_read_write, "read_write"},
394 {Opt_read_write, "rw"}, /* Alternate spelling */
395 /* Boolean args above */
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700396 {-1, NULL}
397};
398
399static int parse_rbd_opts_token(char *c, void *private)
400{
Alex Elder43ae4702012-07-03 16:01:18 -0500401 struct rbd_options *rbd_opts = private;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700402 substring_t argstr[MAX_OPT_ARGS];
403 int token, intval, ret;
404
Alex Elder43ae4702012-07-03 16:01:18 -0500405 token = match_token(c, rbd_opts_tokens, argstr);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700406 if (token < 0)
407 return -EINVAL;
408
409 if (token < Opt_last_int) {
410 ret = match_int(&argstr[0], &intval);
411 if (ret < 0) {
412 pr_err("bad mount option arg (not int) "
413 "at '%s'\n", c);
414 return ret;
415 }
416 dout("got int token %d val %d\n", token, intval);
417 } else if (token > Opt_last_int && token < Opt_last_string) {
418 dout("got string token %d val %s\n", token,
419 argstr[0].from);
Alex Eldercc0538b2012-08-10 13:12:07 -0700420 } else if (token > Opt_last_string && token < Opt_last_bool) {
421 dout("got Boolean token %d\n", token);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700422 } else {
423 dout("got token %d\n", token);
424 }
425
426 switch (token) {
Alex Eldercc0538b2012-08-10 13:12:07 -0700427 case Opt_read_only:
428 rbd_opts->read_only = true;
429 break;
430 case Opt_read_write:
431 rbd_opts->read_only = false;
432 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700433 default:
Alex Elderaafb2302012-09-06 16:00:54 -0500434 rbd_assert(false);
435 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700436 }
437 return 0;
438}
439
440/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700441 * Get a ceph client with specific addr and configuration, if one does
442 * not exist create it.
443 */
Alex Elderf8c38922012-08-10 13:12:07 -0700444static int rbd_get_client(struct rbd_device *rbd_dev, const char *mon_addr,
445 size_t mon_addr_len, char *options)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700446{
Alex Elderf8c38922012-08-10 13:12:07 -0700447 struct rbd_options *rbd_opts = &rbd_dev->rbd_opts;
Alex Elder43ae4702012-07-03 16:01:18 -0500448 struct ceph_options *ceph_opts;
Alex Elderf8c38922012-08-10 13:12:07 -0700449 struct rbd_client *rbdc;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700450
Alex Eldercc0538b2012-08-10 13:12:07 -0700451 rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700452
Alex Elder43ae4702012-07-03 16:01:18 -0500453 ceph_opts = ceph_parse_options(options, mon_addr,
454 mon_addr + mon_addr_len,
455 parse_rbd_opts_token, rbd_opts);
Alex Elderf8c38922012-08-10 13:12:07 -0700456 if (IS_ERR(ceph_opts))
457 return PTR_ERR(ceph_opts);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700458
Alex Elder1f7ba332012-08-10 13:12:07 -0700459 rbdc = rbd_client_find(ceph_opts);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700460 if (rbdc) {
Alex Eldere6994d3d2012-01-29 13:57:44 -0600461 /* using an existing client */
Alex Elder43ae4702012-07-03 16:01:18 -0500462 ceph_destroy_options(ceph_opts);
Alex Elderf8c38922012-08-10 13:12:07 -0700463 } else {
464 rbdc = rbd_client_create(ceph_opts);
465 if (IS_ERR(rbdc))
466 return PTR_ERR(rbdc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700467 }
Alex Elderf8c38922012-08-10 13:12:07 -0700468 rbd_dev->rbd_client = rbdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700469
Alex Elderf8c38922012-08-10 13:12:07 -0700470 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700471}
472
473/*
474 * Destroy ceph client
Alex Elderd23a4b32012-01-29 13:57:43 -0600475 *
Alex Elder432b8582012-01-29 13:57:44 -0600476 * Caller must hold rbd_client_list_lock.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700477 */
478static void rbd_client_release(struct kref *kref)
479{
480 struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
481
482 dout("rbd_release_client %p\n", rbdc);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500483 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700484 list_del(&rbdc->node);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500485 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700486
487 ceph_destroy_client(rbdc->client);
488 kfree(rbdc);
489}
490
491/*
492 * Drop reference to ceph client node. If it's not referenced anymore, release
493 * it.
494 */
495static void rbd_put_client(struct rbd_device *rbd_dev)
496{
497 kref_put(&rbd_dev->rbd_client->kref, rbd_client_release);
498 rbd_dev->rbd_client = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700499}
500
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700501/*
502 * Destroy requests collection
503 */
504static void rbd_coll_release(struct kref *kref)
505{
506 struct rbd_req_coll *coll =
507 container_of(kref, struct rbd_req_coll, kref);
508
509 dout("rbd_coll_release %p\n", coll);
510 kfree(coll);
511}
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700512
Alex Eldera30b71b2012-07-10 20:30:11 -0500513static bool rbd_image_format_valid(u32 image_format)
514{
515 return image_format == 1 || image_format == 2;
516}
517
Alex Elder8e94af82012-07-25 09:32:40 -0500518static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
519{
Alex Elder103a1502012-08-02 11:29:45 -0500520 size_t size;
521 u32 snap_count;
522
523 /* The header has to start with the magic rbd header text */
524 if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT)))
525 return false;
526
527 /*
528 * The size of a snapshot header has to fit in a size_t, and
529 * that limits the number of snapshots.
530 */
531 snap_count = le32_to_cpu(ondisk->snap_count);
532 size = SIZE_MAX - sizeof (struct ceph_snap_context);
533 if (snap_count > size / sizeof (__le64))
534 return false;
535
536 /*
537 * Not only that, but the size of the entire the snapshot
538 * header must also be representable in a size_t.
539 */
540 size -= snap_count * sizeof (__le64);
541 if ((u64) size < le64_to_cpu(ondisk->snap_names_len))
542 return false;
543
544 return true;
Alex Elder8e94af82012-07-25 09:32:40 -0500545}
546
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700547/*
548 * Create a new header structure, translate header format from the on-disk
549 * header.
550 */
551static int rbd_header_from_disk(struct rbd_image_header *header,
Alex Elder4156d992012-08-02 11:29:46 -0500552 struct rbd_image_header_ondisk *ondisk)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700553{
Alex Elderccece232012-07-10 20:30:10 -0500554 u32 snap_count;
Alex Elder58c17b02012-08-23 23:22:06 -0500555 size_t len;
Alex Elderd2bb24e2012-07-26 23:37:14 -0500556 size_t size;
Alex Elder621901d2012-08-23 23:22:06 -0500557 u32 i;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700558
Alex Elder6a523252012-07-19 17:12:59 -0500559 memset(header, 0, sizeof (*header));
560
Alex Elder103a1502012-08-02 11:29:45 -0500561 snap_count = le32_to_cpu(ondisk->snap_count);
562
Alex Elder58c17b02012-08-23 23:22:06 -0500563 len = strnlen(ondisk->object_prefix, sizeof (ondisk->object_prefix));
564 header->object_prefix = kmalloc(len + 1, GFP_KERNEL);
Alex Elder6a523252012-07-19 17:12:59 -0500565 if (!header->object_prefix)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700566 return -ENOMEM;
Alex Elder58c17b02012-08-23 23:22:06 -0500567 memcpy(header->object_prefix, ondisk->object_prefix, len);
568 header->object_prefix[len] = '\0';
Alex Elder00f1f362012-02-07 12:03:36 -0600569
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700570 if (snap_count) {
Alex Elderf785cc12012-08-23 23:22:06 -0500571 u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len);
572
Alex Elder621901d2012-08-23 23:22:06 -0500573 /* Save a copy of the snapshot names */
574
Alex Elderf785cc12012-08-23 23:22:06 -0500575 if (snap_names_len > (u64) SIZE_MAX)
576 return -EIO;
577 header->snap_names = kmalloc(snap_names_len, GFP_KERNEL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700578 if (!header->snap_names)
Alex Elder6a523252012-07-19 17:12:59 -0500579 goto out_err;
Alex Elderf785cc12012-08-23 23:22:06 -0500580 /*
581 * Note that rbd_dev_v1_header_read() guarantees
582 * the ondisk buffer we're working with has
583 * snap_names_len bytes beyond the end of the
584 * snapshot id array, this memcpy() is safe.
585 */
586 memcpy(header->snap_names, &ondisk->snaps[snap_count],
587 snap_names_len);
Alex Elder6a523252012-07-19 17:12:59 -0500588
Alex Elder621901d2012-08-23 23:22:06 -0500589 /* Record each snapshot's size */
590
Alex Elderd2bb24e2012-07-26 23:37:14 -0500591 size = snap_count * sizeof (*header->snap_sizes);
592 header->snap_sizes = kmalloc(size, GFP_KERNEL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700593 if (!header->snap_sizes)
Alex Elder6a523252012-07-19 17:12:59 -0500594 goto out_err;
Alex Elder621901d2012-08-23 23:22:06 -0500595 for (i = 0; i < snap_count; i++)
596 header->snap_sizes[i] =
597 le64_to_cpu(ondisk->snaps[i].image_size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700598 } else {
Alex Elderccece232012-07-10 20:30:10 -0500599 WARN_ON(ondisk->snap_names_len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700600 header->snap_names = NULL;
601 header->snap_sizes = NULL;
602 }
Alex Elder849b4262012-07-09 21:04:24 -0500603
Alex Elder34b13182012-07-13 20:35:12 -0500604 header->features = 0; /* No features support in v1 images */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700605 header->obj_order = ondisk->options.order;
606 header->crypt_type = ondisk->options.crypt_type;
607 header->comp_type = ondisk->options.comp_type;
Alex Elder6a523252012-07-19 17:12:59 -0500608
Alex Elder621901d2012-08-23 23:22:06 -0500609 /* Allocate and fill in the snapshot context */
610
Alex Elderf84344f2012-08-31 17:29:51 -0500611 header->image_size = le64_to_cpu(ondisk->image_size);
Alex Elder6a523252012-07-19 17:12:59 -0500612 size = sizeof (struct ceph_snap_context);
613 size += snap_count * sizeof (header->snapc->snaps[0]);
614 header->snapc = kzalloc(size, GFP_KERNEL);
615 if (!header->snapc)
616 goto out_err;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700617
618 atomic_set(&header->snapc->nref, 1);
Alex Elder505cbb92012-07-19 08:49:18 -0500619 header->snapc->seq = le64_to_cpu(ondisk->snap_seq);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700620 header->snapc->num_snaps = snap_count;
Alex Elder621901d2012-08-23 23:22:06 -0500621 for (i = 0; i < snap_count; i++)
622 header->snapc->snaps[i] =
623 le64_to_cpu(ondisk->snaps[i].id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700624
625 return 0;
626
Alex Elder6a523252012-07-19 17:12:59 -0500627out_err:
Alex Elder849b4262012-07-09 21:04:24 -0500628 kfree(header->snap_sizes);
Alex Elderccece232012-07-10 20:30:10 -0500629 header->snap_sizes = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700630 kfree(header->snap_names);
Alex Elderccece232012-07-10 20:30:10 -0500631 header->snap_names = NULL;
Alex Elder6a523252012-07-19 17:12:59 -0500632 kfree(header->object_prefix);
633 header->object_prefix = NULL;
Alex Elderccece232012-07-10 20:30:10 -0500634
Alex Elder00f1f362012-02-07 12:03:36 -0600635 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700636}
637
Alex Elder8836b992012-08-30 14:42:15 -0500638static int snap_by_name(struct rbd_device *rbd_dev, const char *snap_name)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700639{
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700640
Alex Eldere86924a2012-07-10 20:30:11 -0500641 struct rbd_snap *snap;
Alex Elder00f1f362012-02-07 12:03:36 -0600642
Alex Eldere86924a2012-07-10 20:30:11 -0500643 list_for_each_entry(snap, &rbd_dev->snaps, node) {
644 if (!strcmp(snap_name, snap->name)) {
645 rbd_dev->mapping.snap_id = snap->id;
646 rbd_dev->mapping.size = snap->size;
Alex Elder34b13182012-07-13 20:35:12 -0500647 rbd_dev->mapping.features = snap->features;
Alex Elder00f1f362012-02-07 12:03:36 -0600648
Alex Eldere86924a2012-07-10 20:30:11 -0500649 return 0;
Alex Elder00f1f362012-02-07 12:03:36 -0600650 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700651 }
Alex Eldere86924a2012-07-10 20:30:11 -0500652
Alex Elder00f1f362012-02-07 12:03:36 -0600653 return -ENOENT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700654}
655
Alex Elder5ed16172012-08-29 17:11:07 -0500656static int rbd_dev_set_mapping(struct rbd_device *rbd_dev, char *snap_name)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700657{
Alex Elder78dc4472012-07-19 08:49:18 -0500658 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700659
Alex Elder4e1105a2012-08-31 17:29:52 -0500660 if (!memcmp(snap_name, RBD_SNAP_HEAD_NAME,
Josh Durgincc9d7342011-11-21 18:19:13 -0800661 sizeof (RBD_SNAP_HEAD_NAME))) {
Alex Elderf84344f2012-08-31 17:29:51 -0500662 rbd_dev->mapping.snap_id = CEPH_NOSNAP;
Alex Elder99c1f082012-08-30 14:42:15 -0500663 rbd_dev->mapping.size = rbd_dev->header.image_size;
Alex Elder34b13182012-07-13 20:35:12 -0500664 rbd_dev->mapping.features = rbd_dev->header.features;
Alex Elderf84344f2012-08-31 17:29:51 -0500665 rbd_dev->mapping.snap_exists = false;
666 rbd_dev->mapping.read_only = rbd_dev->rbd_opts.read_only;
Alex Eldere86924a2012-07-10 20:30:11 -0500667 ret = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700668 } else {
Alex Elder8836b992012-08-30 14:42:15 -0500669 ret = snap_by_name(rbd_dev, snap_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700670 if (ret < 0)
671 goto done;
Alex Elderf84344f2012-08-31 17:29:51 -0500672 rbd_dev->mapping.snap_exists = true;
673 rbd_dev->mapping.read_only = true;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700674 }
Alex Elder4e1105a2012-08-31 17:29:52 -0500675 rbd_dev->mapping.snap_name = snap_name;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700676done:
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700677 return ret;
678}
679
680static void rbd_header_free(struct rbd_image_header *header)
681{
Alex Elder849b4262012-07-09 21:04:24 -0500682 kfree(header->object_prefix);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500683 header->object_prefix = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700684 kfree(header->snap_sizes);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500685 header->snap_sizes = NULL;
Alex Elder849b4262012-07-09 21:04:24 -0500686 kfree(header->snap_names);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500687 header->snap_names = NULL;
Josh Durgind1d25642011-12-05 14:03:05 -0800688 ceph_put_snap_context(header->snapc);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500689 header->snapc = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700690}
691
Alex Elder65ccfe22012-08-09 10:33:26 -0700692static char *rbd_segment_name(struct rbd_device *rbd_dev, u64 offset)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700693{
Alex Elder65ccfe22012-08-09 10:33:26 -0700694 char *name;
695 u64 segment;
696 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700697
Alex Elder65ccfe22012-08-09 10:33:26 -0700698 name = kmalloc(RBD_MAX_SEG_NAME_LEN + 1, GFP_NOIO);
699 if (!name)
700 return NULL;
701 segment = offset >> rbd_dev->header.obj_order;
702 ret = snprintf(name, RBD_MAX_SEG_NAME_LEN, "%s.%012llx",
703 rbd_dev->header.object_prefix, segment);
704 if (ret < 0 || ret >= RBD_MAX_SEG_NAME_LEN) {
705 pr_err("error formatting segment name for #%llu (%d)\n",
706 segment, ret);
707 kfree(name);
708 name = NULL;
709 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700710
Alex Elder65ccfe22012-08-09 10:33:26 -0700711 return name;
712}
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700713
Alex Elder65ccfe22012-08-09 10:33:26 -0700714static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset)
715{
716 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700717
Alex Elder65ccfe22012-08-09 10:33:26 -0700718 return offset & (segment_size - 1);
719}
720
721static u64 rbd_segment_length(struct rbd_device *rbd_dev,
722 u64 offset, u64 length)
723{
724 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
725
726 offset &= segment_size - 1;
727
Alex Elderaafb2302012-09-06 16:00:54 -0500728 rbd_assert(length <= U64_MAX - offset);
Alex Elder65ccfe22012-08-09 10:33:26 -0700729 if (offset + length > segment_size)
730 length = segment_size - offset;
731
732 return length;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700733}
734
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700735static int rbd_get_num_segments(struct rbd_image_header *header,
736 u64 ofs, u64 len)
737{
Alex Elderdf111be2012-08-09 10:33:26 -0700738 u64 start_seg;
739 u64 end_seg;
740
741 if (!len)
742 return 0;
743 if (len - 1 > U64_MAX - ofs)
744 return -ERANGE;
745
746 start_seg = ofs >> header->obj_order;
747 end_seg = (ofs + len - 1) >> header->obj_order;
748
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700749 return end_seg - start_seg + 1;
750}
751
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700752/*
Josh Durgin029bcbd2011-07-22 11:35:23 -0700753 * returns the size of an object in the image
754 */
755static u64 rbd_obj_bytes(struct rbd_image_header *header)
756{
757 return 1 << header->obj_order;
758}
759
760/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700761 * bio helpers
762 */
763
764static void bio_chain_put(struct bio *chain)
765{
766 struct bio *tmp;
767
768 while (chain) {
769 tmp = chain;
770 chain = chain->bi_next;
771 bio_put(tmp);
772 }
773}
774
775/*
776 * zeros a bio chain, starting at specific offset
777 */
778static void zero_bio_chain(struct bio *chain, int start_ofs)
779{
780 struct bio_vec *bv;
781 unsigned long flags;
782 void *buf;
783 int i;
784 int pos = 0;
785
786 while (chain) {
787 bio_for_each_segment(bv, chain, i) {
788 if (pos + bv->bv_len > start_ofs) {
789 int remainder = max(start_ofs - pos, 0);
790 buf = bvec_kmap_irq(bv, &flags);
791 memset(buf + remainder, 0,
792 bv->bv_len - remainder);
Dan Carpenter85b5aaa2010-10-11 21:15:11 +0200793 bvec_kunmap_irq(buf, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700794 }
795 pos += bv->bv_len;
796 }
797
798 chain = chain->bi_next;
799 }
800}
801
802/*
803 * bio_chain_clone - clone a chain of bios up to a certain length.
804 * might return a bio_pair that will need to be released.
805 */
806static struct bio *bio_chain_clone(struct bio **old, struct bio **next,
807 struct bio_pair **bp,
808 int len, gfp_t gfpmask)
809{
Alex Elder542582f2012-08-09 10:33:25 -0700810 struct bio *old_chain = *old;
811 struct bio *new_chain = NULL;
812 struct bio *tail;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700813 int total = 0;
814
815 if (*bp) {
816 bio_pair_release(*bp);
817 *bp = NULL;
818 }
819
820 while (old_chain && (total < len)) {
Alex Elder542582f2012-08-09 10:33:25 -0700821 struct bio *tmp;
822
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700823 tmp = bio_kmalloc(gfpmask, old_chain->bi_max_vecs);
824 if (!tmp)
825 goto err_out;
Alex Elder542582f2012-08-09 10:33:25 -0700826 gfpmask &= ~__GFP_WAIT; /* can't wait after the first */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700827
828 if (total + old_chain->bi_size > len) {
829 struct bio_pair *bp;
830
831 /*
832 * this split can only happen with a single paged bio,
833 * split_bio will BUG_ON if this is not the case
834 */
835 dout("bio_chain_clone split! total=%d remaining=%d"
Alex Elderbd919d42012-07-13 20:35:11 -0500836 "bi_size=%u\n",
837 total, len - total, old_chain->bi_size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700838
839 /* split the bio. We'll release it either in the next
840 call, or it will have to be released outside */
Alex Elder593a9e72012-02-07 12:03:37 -0600841 bp = bio_split(old_chain, (len - total) / SECTOR_SIZE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700842 if (!bp)
843 goto err_out;
844
845 __bio_clone(tmp, &bp->bio1);
846
847 *next = &bp->bio2;
848 } else {
849 __bio_clone(tmp, old_chain);
850 *next = old_chain->bi_next;
851 }
852
853 tmp->bi_bdev = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700854 tmp->bi_next = NULL;
Alex Elder542582f2012-08-09 10:33:25 -0700855 if (new_chain)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700856 tail->bi_next = tmp;
Alex Elder542582f2012-08-09 10:33:25 -0700857 else
858 new_chain = tmp;
859 tail = tmp;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700860 old_chain = old_chain->bi_next;
861
862 total += tmp->bi_size;
863 }
864
Alex Elderaafb2302012-09-06 16:00:54 -0500865 rbd_assert(total == len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700866
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700867 *old = old_chain;
868
869 return new_chain;
870
871err_out:
872 dout("bio_chain_clone with err\n");
873 bio_chain_put(new_chain);
874 return NULL;
875}
876
877/*
878 * helpers for osd request op vectors.
879 */
Alex Elder57cfc102012-06-26 12:57:03 -0700880static struct ceph_osd_req_op *rbd_create_rw_ops(int num_ops,
881 int opcode, u32 payload_len)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700882{
Alex Elder57cfc102012-06-26 12:57:03 -0700883 struct ceph_osd_req_op *ops;
884
885 ops = kzalloc(sizeof (*ops) * (num_ops + 1), GFP_NOIO);
886 if (!ops)
887 return NULL;
888
889 ops[0].op = opcode;
890
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700891 /*
892 * op extent offset and length will be set later on
893 * in calc_raw_layout()
894 */
Alex Elder57cfc102012-06-26 12:57:03 -0700895 ops[0].payload_len = payload_len;
896
897 return ops;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700898}
899
900static void rbd_destroy_ops(struct ceph_osd_req_op *ops)
901{
902 kfree(ops);
903}
904
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700905static void rbd_coll_end_req_index(struct request *rq,
906 struct rbd_req_coll *coll,
907 int index,
908 int ret, u64 len)
909{
910 struct request_queue *q;
911 int min, max, i;
912
Alex Elderbd919d42012-07-13 20:35:11 -0500913 dout("rbd_coll_end_req_index %p index %d ret %d len %llu\n",
914 coll, index, ret, (unsigned long long) len);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700915
916 if (!rq)
917 return;
918
919 if (!coll) {
920 blk_end_request(rq, ret, len);
921 return;
922 }
923
924 q = rq->q;
925
926 spin_lock_irq(q->queue_lock);
927 coll->status[index].done = 1;
928 coll->status[index].rc = ret;
929 coll->status[index].bytes = len;
930 max = min = coll->num_done;
931 while (max < coll->total && coll->status[max].done)
932 max++;
933
934 for (i = min; i<max; i++) {
935 __blk_end_request(rq, coll->status[i].rc,
936 coll->status[i].bytes);
937 coll->num_done++;
938 kref_put(&coll->kref, rbd_coll_release);
939 }
940 spin_unlock_irq(q->queue_lock);
941}
942
943static void rbd_coll_end_req(struct rbd_request *req,
944 int ret, u64 len)
945{
946 rbd_coll_end_req_index(req->rq, req->coll, req->coll_index, ret, len);
947}
948
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700949/*
950 * Send ceph osd request
951 */
952static int rbd_do_request(struct request *rq,
Alex Elder0ce1a792012-07-03 16:01:18 -0500953 struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700954 struct ceph_snap_context *snapc,
955 u64 snapid,
Alex Elderaded07e2012-07-03 16:01:18 -0500956 const char *object_name, u64 ofs, u64 len,
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700957 struct bio *bio,
958 struct page **pages,
959 int num_pages,
960 int flags,
961 struct ceph_osd_req_op *ops,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700962 struct rbd_req_coll *coll,
963 int coll_index,
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700964 void (*rbd_cb)(struct ceph_osd_request *req,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700965 struct ceph_msg *msg),
966 struct ceph_osd_request **linger_req,
967 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700968{
969 struct ceph_osd_request *req;
970 struct ceph_file_layout *layout;
971 int ret;
972 u64 bno;
973 struct timespec mtime = CURRENT_TIME;
974 struct rbd_request *req_data;
975 struct ceph_osd_request_head *reqhead;
Alex Elder1dbb4392012-01-24 10:08:37 -0600976 struct ceph_osd_client *osdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700977
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700978 req_data = kzalloc(sizeof(*req_data), GFP_NOIO);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700979 if (!req_data) {
980 if (coll)
981 rbd_coll_end_req_index(rq, coll, coll_index,
982 -ENOMEM, len);
983 return -ENOMEM;
984 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700985
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700986 if (coll) {
987 req_data->coll = coll;
988 req_data->coll_index = coll_index;
989 }
990
Alex Elderbd919d42012-07-13 20:35:11 -0500991 dout("rbd_do_request object_name=%s ofs=%llu len=%llu\n", object_name,
992 (unsigned long long) ofs, (unsigned long long) len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700993
Alex Elder0ce1a792012-07-03 16:01:18 -0500994 osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder1dbb4392012-01-24 10:08:37 -0600995 req = ceph_osdc_alloc_request(osdc, flags, snapc, ops,
996 false, GFP_NOIO, pages, bio);
Sage Weil4ad12622011-05-03 09:23:36 -0700997 if (!req) {
Sage Weil4ad12622011-05-03 09:23:36 -0700998 ret = -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700999 goto done_pages;
1000 }
1001
1002 req->r_callback = rbd_cb;
1003
1004 req_data->rq = rq;
1005 req_data->bio = bio;
1006 req_data->pages = pages;
1007 req_data->len = len;
1008
1009 req->r_priv = req_data;
1010
1011 reqhead = req->r_request->front.iov_base;
1012 reqhead->snapid = cpu_to_le64(CEPH_NOSNAP);
1013
Alex Elderaded07e2012-07-03 16:01:18 -05001014 strncpy(req->r_oid, object_name, sizeof(req->r_oid));
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001015 req->r_oid_len = strlen(req->r_oid);
1016
1017 layout = &req->r_file_layout;
1018 memset(layout, 0, sizeof(*layout));
1019 layout->fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
1020 layout->fl_stripe_count = cpu_to_le32(1);
1021 layout->fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
Alex Elder0ce1a792012-07-03 16:01:18 -05001022 layout->fl_pg_pool = cpu_to_le32(rbd_dev->pool_id);
Alex Elder1dbb4392012-01-24 10:08:37 -06001023 ceph_calc_raw_layout(osdc, layout, snapid, ofs, &len, &bno,
1024 req, ops);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001025
1026 ceph_osdc_build_request(req, ofs, &len,
1027 ops,
1028 snapc,
1029 &mtime,
1030 req->r_oid, req->r_oid_len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001031
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001032 if (linger_req) {
Alex Elder1dbb4392012-01-24 10:08:37 -06001033 ceph_osdc_set_request_linger(osdc, req);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001034 *linger_req = req;
1035 }
1036
Alex Elder1dbb4392012-01-24 10:08:37 -06001037 ret = ceph_osdc_start_request(osdc, req, false);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001038 if (ret < 0)
1039 goto done_err;
1040
1041 if (!rbd_cb) {
Alex Elder1dbb4392012-01-24 10:08:37 -06001042 ret = ceph_osdc_wait_request(osdc, req);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001043 if (ver)
1044 *ver = le64_to_cpu(req->r_reassert_version.version);
Alex Elderbd919d42012-07-13 20:35:11 -05001045 dout("reassert_ver=%llu\n",
1046 (unsigned long long)
1047 le64_to_cpu(req->r_reassert_version.version));
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001048 ceph_osdc_put_request(req);
1049 }
1050 return ret;
1051
1052done_err:
1053 bio_chain_put(req_data->bio);
1054 ceph_osdc_put_request(req);
1055done_pages:
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001056 rbd_coll_end_req(req_data, ret, len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001057 kfree(req_data);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001058 return ret;
1059}
1060
1061/*
1062 * Ceph osd op callback
1063 */
1064static void rbd_req_cb(struct ceph_osd_request *req, struct ceph_msg *msg)
1065{
1066 struct rbd_request *req_data = req->r_priv;
1067 struct ceph_osd_reply_head *replyhead;
1068 struct ceph_osd_op *op;
1069 __s32 rc;
1070 u64 bytes;
1071 int read_op;
1072
1073 /* parse reply */
1074 replyhead = msg->front.iov_base;
1075 WARN_ON(le32_to_cpu(replyhead->num_ops) == 0);
1076 op = (void *)(replyhead + 1);
1077 rc = le32_to_cpu(replyhead->result);
1078 bytes = le64_to_cpu(op->extent.length);
Dan Carpenter895cfcc2012-06-06 09:15:33 -05001079 read_op = (le16_to_cpu(op->op) == CEPH_OSD_OP_READ);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001080
Alex Elderbd919d42012-07-13 20:35:11 -05001081 dout("rbd_req_cb bytes=%llu readop=%d rc=%d\n",
1082 (unsigned long long) bytes, read_op, (int) rc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001083
1084 if (rc == -ENOENT && read_op) {
1085 zero_bio_chain(req_data->bio, 0);
1086 rc = 0;
1087 } else if (rc == 0 && read_op && bytes < req_data->len) {
1088 zero_bio_chain(req_data->bio, bytes);
1089 bytes = req_data->len;
1090 }
1091
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001092 rbd_coll_end_req(req_data, rc, bytes);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001093
1094 if (req_data->bio)
1095 bio_chain_put(req_data->bio);
1096
1097 ceph_osdc_put_request(req);
1098 kfree(req_data);
1099}
1100
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001101static void rbd_simple_req_cb(struct ceph_osd_request *req, struct ceph_msg *msg)
1102{
1103 ceph_osdc_put_request(req);
1104}
1105
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001106/*
1107 * Do a synchronous ceph osd operation
1108 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001109static int rbd_req_sync_op(struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001110 struct ceph_snap_context *snapc,
1111 u64 snapid,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001112 int flags,
Alex Elder913d2fd2012-06-26 12:57:03 -07001113 struct ceph_osd_req_op *ops,
Alex Elderaded07e2012-07-03 16:01:18 -05001114 const char *object_name,
Alex Elderf8d4de62012-07-03 16:01:19 -05001115 u64 ofs, u64 inbound_size,
1116 char *inbound,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001117 struct ceph_osd_request **linger_req,
1118 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001119{
1120 int ret;
1121 struct page **pages;
1122 int num_pages;
Alex Elder913d2fd2012-06-26 12:57:03 -07001123
Alex Elderaafb2302012-09-06 16:00:54 -05001124 rbd_assert(ops != NULL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001125
Alex Elderf8d4de62012-07-03 16:01:19 -05001126 num_pages = calc_pages_for(ofs, inbound_size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001127 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
Dan Carpenterb8d06382010-10-11 21:14:23 +02001128 if (IS_ERR(pages))
1129 return PTR_ERR(pages);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001130
Alex Elder0ce1a792012-07-03 16:01:18 -05001131 ret = rbd_do_request(NULL, rbd_dev, snapc, snapid,
Alex Elderf8d4de62012-07-03 16:01:19 -05001132 object_name, ofs, inbound_size, NULL,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001133 pages, num_pages,
1134 flags,
1135 ops,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001136 NULL, 0,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001137 NULL,
1138 linger_req, ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001139 if (ret < 0)
Alex Elder913d2fd2012-06-26 12:57:03 -07001140 goto done;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001141
Alex Elderf8d4de62012-07-03 16:01:19 -05001142 if ((flags & CEPH_OSD_FLAG_READ) && inbound)
1143 ret = ceph_copy_from_page_vector(pages, inbound, ofs, ret);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001144
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001145done:
1146 ceph_release_page_vector(pages, num_pages);
1147 return ret;
1148}
1149
1150/*
1151 * Do an asynchronous ceph osd operation
1152 */
1153static int rbd_do_op(struct request *rq,
Alex Elder0ce1a792012-07-03 16:01:18 -05001154 struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001155 struct ceph_snap_context *snapc,
1156 u64 snapid,
Alex Elderd1f57ea2012-06-26 12:57:03 -07001157 int opcode, int flags,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001158 u64 ofs, u64 len,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001159 struct bio *bio,
1160 struct rbd_req_coll *coll,
1161 int coll_index)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001162{
1163 char *seg_name;
1164 u64 seg_ofs;
1165 u64 seg_len;
1166 int ret;
1167 struct ceph_osd_req_op *ops;
1168 u32 payload_len;
1169
Alex Elder65ccfe22012-08-09 10:33:26 -07001170 seg_name = rbd_segment_name(rbd_dev, ofs);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001171 if (!seg_name)
1172 return -ENOMEM;
Alex Elder65ccfe22012-08-09 10:33:26 -07001173 seg_len = rbd_segment_length(rbd_dev, ofs, len);
1174 seg_ofs = rbd_segment_offset(rbd_dev, ofs);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001175
1176 payload_len = (flags & CEPH_OSD_FLAG_WRITE ? seg_len : 0);
1177
Alex Elder57cfc102012-06-26 12:57:03 -07001178 ret = -ENOMEM;
1179 ops = rbd_create_rw_ops(1, opcode, payload_len);
1180 if (!ops)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001181 goto done;
1182
1183 /* we've taken care of segment sizes earlier when we
1184 cloned the bios. We should never have a segment
1185 truncated at this point */
Alex Elderaafb2302012-09-06 16:00:54 -05001186 rbd_assert(seg_len == len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001187
1188 ret = rbd_do_request(rq, rbd_dev, snapc, snapid,
1189 seg_name, seg_ofs, seg_len,
1190 bio,
1191 NULL, 0,
1192 flags,
1193 ops,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001194 coll, coll_index,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001195 rbd_req_cb, 0, NULL);
Sage Weil11f77002011-05-12 16:13:54 -07001196
1197 rbd_destroy_ops(ops);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001198done:
1199 kfree(seg_name);
1200 return ret;
1201}
1202
1203/*
1204 * Request async osd write
1205 */
1206static int rbd_req_write(struct request *rq,
1207 struct rbd_device *rbd_dev,
1208 struct ceph_snap_context *snapc,
1209 u64 ofs, u64 len,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001210 struct bio *bio,
1211 struct rbd_req_coll *coll,
1212 int coll_index)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001213{
1214 return rbd_do_op(rq, rbd_dev, snapc, CEPH_NOSNAP,
1215 CEPH_OSD_OP_WRITE,
1216 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001217 ofs, len, bio, coll, coll_index);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001218}
1219
1220/*
1221 * Request async osd read
1222 */
1223static int rbd_req_read(struct request *rq,
1224 struct rbd_device *rbd_dev,
1225 u64 snapid,
1226 u64 ofs, u64 len,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001227 struct bio *bio,
1228 struct rbd_req_coll *coll,
1229 int coll_index)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001230{
1231 return rbd_do_op(rq, rbd_dev, NULL,
Josh Durginb06e6a62011-11-21 18:16:52 -08001232 snapid,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001233 CEPH_OSD_OP_READ,
1234 CEPH_OSD_FLAG_READ,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001235 ofs, len, bio, coll, coll_index);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001236}
1237
1238/*
1239 * Request sync osd read
1240 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001241static int rbd_req_sync_read(struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001242 u64 snapid,
Alex Elderaded07e2012-07-03 16:01:18 -05001243 const char *object_name,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001244 u64 ofs, u64 len,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001245 char *buf,
1246 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001247{
Alex Elder913d2fd2012-06-26 12:57:03 -07001248 struct ceph_osd_req_op *ops;
1249 int ret;
1250
1251 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_READ, 0);
1252 if (!ops)
1253 return -ENOMEM;
1254
1255 ret = rbd_req_sync_op(rbd_dev, NULL,
Josh Durginb06e6a62011-11-21 18:16:52 -08001256 snapid,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001257 CEPH_OSD_FLAG_READ,
Alex Elder913d2fd2012-06-26 12:57:03 -07001258 ops, object_name, ofs, len, buf, NULL, ver);
1259 rbd_destroy_ops(ops);
1260
1261 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001262}
1263
1264/*
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001265 * Request sync osd watch
1266 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001267static int rbd_req_sync_notify_ack(struct rbd_device *rbd_dev,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001268 u64 ver,
Alex Elder7f0a24d2012-07-25 09:32:40 -05001269 u64 notify_id)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001270{
1271 struct ceph_osd_req_op *ops;
Sage Weil11f77002011-05-12 16:13:54 -07001272 int ret;
1273
Alex Elder57cfc102012-06-26 12:57:03 -07001274 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_NOTIFY_ACK, 0);
1275 if (!ops)
1276 return -ENOMEM;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001277
Josh Durgina71b8912011-12-05 18:10:44 -08001278 ops[0].watch.ver = cpu_to_le64(ver);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001279 ops[0].watch.cookie = notify_id;
1280 ops[0].watch.flag = 0;
1281
Alex Elder0ce1a792012-07-03 16:01:18 -05001282 ret = rbd_do_request(NULL, rbd_dev, NULL, CEPH_NOSNAP,
Alex Elder7f0a24d2012-07-25 09:32:40 -05001283 rbd_dev->header_name, 0, 0, NULL,
Alex Elderad4f2322012-07-03 16:01:19 -05001284 NULL, 0,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001285 CEPH_OSD_FLAG_READ,
1286 ops,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001287 NULL, 0,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001288 rbd_simple_req_cb, 0, NULL);
1289
1290 rbd_destroy_ops(ops);
1291 return ret;
1292}
1293
1294static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
1295{
Alex Elder0ce1a792012-07-03 16:01:18 -05001296 struct rbd_device *rbd_dev = (struct rbd_device *)data;
Josh Durgina71b8912011-12-05 18:10:44 -08001297 u64 hver;
Sage Weil13143d22011-05-12 16:08:30 -07001298 int rc;
1299
Alex Elder0ce1a792012-07-03 16:01:18 -05001300 if (!rbd_dev)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001301 return;
1302
Alex Elderbd919d42012-07-13 20:35:11 -05001303 dout("rbd_watch_cb %s notify_id=%llu opcode=%u\n",
1304 rbd_dev->header_name, (unsigned long long) notify_id,
1305 (unsigned int) opcode);
Alex Elder1fe5e992012-07-25 09:32:41 -05001306 rc = rbd_refresh_header(rbd_dev, &hver);
Sage Weil13143d22011-05-12 16:08:30 -07001307 if (rc)
Alex Elderf0f8cef2012-01-29 13:57:44 -06001308 pr_warning(RBD_DRV_NAME "%d got notification but failed to "
Alex Elder0ce1a792012-07-03 16:01:18 -05001309 " update snaps: %d\n", rbd_dev->major, rc);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001310
Alex Elder7f0a24d2012-07-25 09:32:40 -05001311 rbd_req_sync_notify_ack(rbd_dev, hver, notify_id);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001312}
1313
1314/*
1315 * Request sync osd watch
1316 */
Alex Elder0e6f3222012-07-25 09:32:40 -05001317static int rbd_req_sync_watch(struct rbd_device *rbd_dev)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001318{
1319 struct ceph_osd_req_op *ops;
Alex Elder0ce1a792012-07-03 16:01:18 -05001320 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder57cfc102012-06-26 12:57:03 -07001321 int ret;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001322
Alex Elder57cfc102012-06-26 12:57:03 -07001323 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_WATCH, 0);
1324 if (!ops)
1325 return -ENOMEM;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001326
1327 ret = ceph_osdc_create_event(osdc, rbd_watch_cb, 0,
Alex Elder0ce1a792012-07-03 16:01:18 -05001328 (void *)rbd_dev, &rbd_dev->watch_event);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001329 if (ret < 0)
1330 goto fail;
1331
Alex Elder0e6f3222012-07-25 09:32:40 -05001332 ops[0].watch.ver = cpu_to_le64(rbd_dev->header.obj_version);
Alex Elder0ce1a792012-07-03 16:01:18 -05001333 ops[0].watch.cookie = cpu_to_le64(rbd_dev->watch_event->cookie);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001334 ops[0].watch.flag = 1;
1335
Alex Elder0ce1a792012-07-03 16:01:18 -05001336 ret = rbd_req_sync_op(rbd_dev, NULL,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001337 CEPH_NOSNAP,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001338 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1339 ops,
Alex Elder0e6f3222012-07-25 09:32:40 -05001340 rbd_dev->header_name,
1341 0, 0, NULL,
Alex Elder0ce1a792012-07-03 16:01:18 -05001342 &rbd_dev->watch_request, NULL);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001343
1344 if (ret < 0)
1345 goto fail_event;
1346
1347 rbd_destroy_ops(ops);
1348 return 0;
1349
1350fail_event:
Alex Elder0ce1a792012-07-03 16:01:18 -05001351 ceph_osdc_cancel_event(rbd_dev->watch_event);
1352 rbd_dev->watch_event = NULL;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001353fail:
1354 rbd_destroy_ops(ops);
1355 return ret;
1356}
1357
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001358/*
1359 * Request sync osd unwatch
1360 */
Alex Elder070c6332012-07-25 09:32:41 -05001361static int rbd_req_sync_unwatch(struct rbd_device *rbd_dev)
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001362{
1363 struct ceph_osd_req_op *ops;
Alex Elder57cfc102012-06-26 12:57:03 -07001364 int ret;
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001365
Alex Elder57cfc102012-06-26 12:57:03 -07001366 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_WATCH, 0);
1367 if (!ops)
1368 return -ENOMEM;
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001369
1370 ops[0].watch.ver = 0;
Alex Elder0ce1a792012-07-03 16:01:18 -05001371 ops[0].watch.cookie = cpu_to_le64(rbd_dev->watch_event->cookie);
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001372 ops[0].watch.flag = 0;
1373
Alex Elder0ce1a792012-07-03 16:01:18 -05001374 ret = rbd_req_sync_op(rbd_dev, NULL,
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001375 CEPH_NOSNAP,
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001376 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1377 ops,
Alex Elder070c6332012-07-25 09:32:41 -05001378 rbd_dev->header_name,
1379 0, 0, NULL, NULL, NULL);
1380
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001381
1382 rbd_destroy_ops(ops);
Alex Elder0ce1a792012-07-03 16:01:18 -05001383 ceph_osdc_cancel_event(rbd_dev->watch_event);
1384 rbd_dev->watch_event = NULL;
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001385 return ret;
1386}
1387
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001388/*
Alex Elder3cb4a682012-06-26 12:57:03 -07001389 * Synchronous osd object method call
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001390 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001391static int rbd_req_sync_exec(struct rbd_device *rbd_dev,
Alex Elderaded07e2012-07-03 16:01:18 -05001392 const char *object_name,
1393 const char *class_name,
1394 const char *method_name,
Alex Elder3cb4a682012-06-26 12:57:03 -07001395 const char *outbound,
1396 size_t outbound_size,
Alex Elderf8d4de62012-07-03 16:01:19 -05001397 char *inbound,
1398 size_t inbound_size,
Alex Elder3cb4a682012-06-26 12:57:03 -07001399 int flags,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001400 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001401{
1402 struct ceph_osd_req_op *ops;
Alex Elderaded07e2012-07-03 16:01:18 -05001403 int class_name_len = strlen(class_name);
1404 int method_name_len = strlen(method_name);
Alex Elder3cb4a682012-06-26 12:57:03 -07001405 int payload_size;
Alex Elder57cfc102012-06-26 12:57:03 -07001406 int ret;
1407
Alex Elder3cb4a682012-06-26 12:57:03 -07001408 /*
1409 * Any input parameters required by the method we're calling
1410 * will be sent along with the class and method names as
1411 * part of the message payload. That data and its size are
1412 * supplied via the indata and indata_len fields (named from
1413 * the perspective of the server side) in the OSD request
1414 * operation.
1415 */
1416 payload_size = class_name_len + method_name_len + outbound_size;
1417 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_CALL, payload_size);
Alex Elder57cfc102012-06-26 12:57:03 -07001418 if (!ops)
1419 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001420
Alex Elderaded07e2012-07-03 16:01:18 -05001421 ops[0].cls.class_name = class_name;
1422 ops[0].cls.class_len = (__u8) class_name_len;
1423 ops[0].cls.method_name = method_name;
1424 ops[0].cls.method_len = (__u8) method_name_len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001425 ops[0].cls.argc = 0;
Alex Elder3cb4a682012-06-26 12:57:03 -07001426 ops[0].cls.indata = outbound;
1427 ops[0].cls.indata_len = outbound_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001428
Alex Elder0ce1a792012-07-03 16:01:18 -05001429 ret = rbd_req_sync_op(rbd_dev, NULL,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001430 CEPH_NOSNAP,
Alex Elder3cb4a682012-06-26 12:57:03 -07001431 flags, ops,
Alex Elderf8d4de62012-07-03 16:01:19 -05001432 object_name, 0, inbound_size, inbound,
1433 NULL, ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001434
1435 rbd_destroy_ops(ops);
1436
1437 dout("cls_exec returned %d\n", ret);
1438 return ret;
1439}
1440
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001441static struct rbd_req_coll *rbd_alloc_coll(int num_reqs)
1442{
1443 struct rbd_req_coll *coll =
1444 kzalloc(sizeof(struct rbd_req_coll) +
1445 sizeof(struct rbd_req_status) * num_reqs,
1446 GFP_ATOMIC);
1447
1448 if (!coll)
1449 return NULL;
1450 coll->total = num_reqs;
1451 kref_init(&coll->kref);
1452 return coll;
1453}
1454
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001455/*
1456 * block device queue callback
1457 */
1458static void rbd_rq_fn(struct request_queue *q)
1459{
1460 struct rbd_device *rbd_dev = q->queuedata;
1461 struct request *rq;
1462 struct bio_pair *bp = NULL;
1463
Alex Elder00f1f362012-02-07 12:03:36 -06001464 while ((rq = blk_fetch_request(q))) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001465 struct bio *bio;
1466 struct bio *rq_bio, *next_bio = NULL;
1467 bool do_write;
Alex Elderbd919d42012-07-13 20:35:11 -05001468 unsigned int size;
1469 u64 op_size = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001470 u64 ofs;
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001471 int num_segs, cur_seg = 0;
1472 struct rbd_req_coll *coll;
Josh Durgind1d25642011-12-05 14:03:05 -08001473 struct ceph_snap_context *snapc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001474
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001475 dout("fetched request\n");
1476
1477 /* filter out block requests we don't understand */
1478 if ((rq->cmd_type != REQ_TYPE_FS)) {
1479 __blk_end_request_all(rq, 0);
Alex Elder00f1f362012-02-07 12:03:36 -06001480 continue;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001481 }
1482
1483 /* deduce our operation (read, write) */
1484 do_write = (rq_data_dir(rq) == WRITE);
1485
1486 size = blk_rq_bytes(rq);
Alex Elder593a9e72012-02-07 12:03:37 -06001487 ofs = blk_rq_pos(rq) * SECTOR_SIZE;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001488 rq_bio = rq->bio;
Alex Elderf84344f2012-08-31 17:29:51 -05001489 if (do_write && rbd_dev->mapping.read_only) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001490 __blk_end_request_all(rq, -EROFS);
Alex Elder00f1f362012-02-07 12:03:36 -06001491 continue;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001492 }
1493
1494 spin_unlock_irq(q->queue_lock);
1495
Josh Durgind1d25642011-12-05 14:03:05 -08001496 down_read(&rbd_dev->header_rwsem);
Josh Durgine88a36e2011-11-21 18:14:25 -08001497
Alex Elderf84344f2012-08-31 17:29:51 -05001498 if (rbd_dev->mapping.snap_id != CEPH_NOSNAP &&
1499 !rbd_dev->mapping.snap_exists) {
Josh Durgine88a36e2011-11-21 18:14:25 -08001500 up_read(&rbd_dev->header_rwsem);
Josh Durgind1d25642011-12-05 14:03:05 -08001501 dout("request for non-existent snapshot");
1502 spin_lock_irq(q->queue_lock);
1503 __blk_end_request_all(rq, -ENXIO);
1504 continue;
Josh Durgine88a36e2011-11-21 18:14:25 -08001505 }
1506
Josh Durgind1d25642011-12-05 14:03:05 -08001507 snapc = ceph_get_snap_context(rbd_dev->header.snapc);
1508
1509 up_read(&rbd_dev->header_rwsem);
1510
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001511 dout("%s 0x%x bytes at 0x%llx\n",
1512 do_write ? "write" : "read",
Alex Elderbd919d42012-07-13 20:35:11 -05001513 size, (unsigned long long) blk_rq_pos(rq) * SECTOR_SIZE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001514
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001515 num_segs = rbd_get_num_segments(&rbd_dev->header, ofs, size);
Alex Elderdf111be2012-08-09 10:33:26 -07001516 if (num_segs <= 0) {
1517 spin_lock_irq(q->queue_lock);
1518 __blk_end_request_all(rq, num_segs);
1519 ceph_put_snap_context(snapc);
1520 continue;
1521 }
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001522 coll = rbd_alloc_coll(num_segs);
1523 if (!coll) {
1524 spin_lock_irq(q->queue_lock);
1525 __blk_end_request_all(rq, -ENOMEM);
Josh Durgind1d25642011-12-05 14:03:05 -08001526 ceph_put_snap_context(snapc);
Alex Elder00f1f362012-02-07 12:03:36 -06001527 continue;
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001528 }
1529
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001530 do {
1531 /* a bio clone to be passed down to OSD req */
Alex Elderbd919d42012-07-13 20:35:11 -05001532 dout("rq->bio->bi_vcnt=%hu\n", rq->bio->bi_vcnt);
Alex Elder65ccfe22012-08-09 10:33:26 -07001533 op_size = rbd_segment_length(rbd_dev, ofs, size);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001534 kref_get(&coll->kref);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001535 bio = bio_chain_clone(&rq_bio, &next_bio, &bp,
1536 op_size, GFP_ATOMIC);
1537 if (!bio) {
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001538 rbd_coll_end_req_index(rq, coll, cur_seg,
1539 -ENOMEM, op_size);
1540 goto next_seg;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001541 }
1542
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001543
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001544 /* init OSD command: write or read */
1545 if (do_write)
1546 rbd_req_write(rq, rbd_dev,
Josh Durgind1d25642011-12-05 14:03:05 -08001547 snapc,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001548 ofs,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001549 op_size, bio,
1550 coll, cur_seg);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001551 else
1552 rbd_req_read(rq, rbd_dev,
Alex Elderf84344f2012-08-31 17:29:51 -05001553 rbd_dev->mapping.snap_id,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001554 ofs,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001555 op_size, bio,
1556 coll, cur_seg);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001557
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001558next_seg:
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001559 size -= op_size;
1560 ofs += op_size;
1561
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001562 cur_seg++;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001563 rq_bio = next_bio;
1564 } while (size > 0);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001565 kref_put(&coll->kref, rbd_coll_release);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001566
1567 if (bp)
1568 bio_pair_release(bp);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001569 spin_lock_irq(q->queue_lock);
Josh Durgind1d25642011-12-05 14:03:05 -08001570
1571 ceph_put_snap_context(snapc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001572 }
1573}
1574
1575/*
1576 * a queue callback. Makes sure that we don't create a bio that spans across
1577 * multiple osd objects. One exception would be with a single page bios,
1578 * which we handle later at bio_chain_clone
1579 */
1580static int rbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd,
1581 struct bio_vec *bvec)
1582{
1583 struct rbd_device *rbd_dev = q->queuedata;
Alex Elder593a9e72012-02-07 12:03:37 -06001584 unsigned int chunk_sectors;
1585 sector_t sector;
1586 unsigned int bio_sectors;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001587 int max;
1588
Alex Elder593a9e72012-02-07 12:03:37 -06001589 chunk_sectors = 1 << (rbd_dev->header.obj_order - SECTOR_SHIFT);
1590 sector = bmd->bi_sector + get_start_sect(bmd->bi_bdev);
1591 bio_sectors = bmd->bi_size >> SECTOR_SHIFT;
1592
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001593 max = (chunk_sectors - ((sector & (chunk_sectors - 1))
Alex Elder593a9e72012-02-07 12:03:37 -06001594 + bio_sectors)) << SECTOR_SHIFT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001595 if (max < 0)
1596 max = 0; /* bio_add cannot handle a negative return */
1597 if (max <= bvec->bv_len && bio_sectors == 0)
1598 return bvec->bv_len;
1599 return max;
1600}
1601
1602static void rbd_free_disk(struct rbd_device *rbd_dev)
1603{
1604 struct gendisk *disk = rbd_dev->disk;
1605
1606 if (!disk)
1607 return;
1608
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001609 if (disk->flags & GENHD_FL_UP)
1610 del_gendisk(disk);
1611 if (disk->queue)
1612 blk_cleanup_queue(disk->queue);
1613 put_disk(disk);
1614}
1615
1616/*
Alex Elder4156d992012-08-02 11:29:46 -05001617 * Read the complete header for the given rbd device.
1618 *
1619 * Returns a pointer to a dynamically-allocated buffer containing
1620 * the complete and validated header. Caller can pass the address
1621 * of a variable that will be filled in with the version of the
1622 * header object at the time it was read.
1623 *
1624 * Returns a pointer-coded errno if a failure occurs.
1625 */
1626static struct rbd_image_header_ondisk *
1627rbd_dev_v1_header_read(struct rbd_device *rbd_dev, u64 *version)
1628{
1629 struct rbd_image_header_ondisk *ondisk = NULL;
1630 u32 snap_count = 0;
1631 u64 names_size = 0;
1632 u32 want_count;
1633 int ret;
1634
1635 /*
1636 * The complete header will include an array of its 64-bit
1637 * snapshot ids, followed by the names of those snapshots as
1638 * a contiguous block of NUL-terminated strings. Note that
1639 * the number of snapshots could change by the time we read
1640 * it in, in which case we re-read it.
1641 */
1642 do {
1643 size_t size;
1644
1645 kfree(ondisk);
1646
1647 size = sizeof (*ondisk);
1648 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
1649 size += names_size;
1650 ondisk = kmalloc(size, GFP_KERNEL);
1651 if (!ondisk)
1652 return ERR_PTR(-ENOMEM);
1653
1654 ret = rbd_req_sync_read(rbd_dev, CEPH_NOSNAP,
1655 rbd_dev->header_name,
1656 0, size,
1657 (char *) ondisk, version);
1658
1659 if (ret < 0)
1660 goto out_err;
1661 if (WARN_ON((size_t) ret < size)) {
1662 ret = -ENXIO;
1663 pr_warning("short header read for image %s"
1664 " (want %zd got %d)\n",
1665 rbd_dev->image_name, size, ret);
1666 goto out_err;
1667 }
1668 if (!rbd_dev_ondisk_valid(ondisk)) {
1669 ret = -ENXIO;
1670 pr_warning("invalid header for image %s\n",
1671 rbd_dev->image_name);
1672 goto out_err;
1673 }
1674
1675 names_size = le64_to_cpu(ondisk->snap_names_len);
1676 want_count = snap_count;
1677 snap_count = le32_to_cpu(ondisk->snap_count);
1678 } while (snap_count != want_count);
1679
1680 return ondisk;
1681
1682out_err:
1683 kfree(ondisk);
1684
1685 return ERR_PTR(ret);
1686}
1687
1688/*
1689 * reload the ondisk the header
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001690 */
1691static int rbd_read_header(struct rbd_device *rbd_dev,
1692 struct rbd_image_header *header)
1693{
Alex Elder4156d992012-08-02 11:29:46 -05001694 struct rbd_image_header_ondisk *ondisk;
1695 u64 ver = 0;
1696 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001697
Alex Elder4156d992012-08-02 11:29:46 -05001698 ondisk = rbd_dev_v1_header_read(rbd_dev, &ver);
1699 if (IS_ERR(ondisk))
1700 return PTR_ERR(ondisk);
1701 ret = rbd_header_from_disk(header, ondisk);
1702 if (ret >= 0)
1703 header->obj_version = ver;
1704 kfree(ondisk);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001705
Alex Elder4156d992012-08-02 11:29:46 -05001706 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001707}
1708
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001709static void __rbd_remove_all_snaps(struct rbd_device *rbd_dev)
1710{
1711 struct rbd_snap *snap;
Alex Eldera0593292012-07-19 09:09:27 -05001712 struct rbd_snap *next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001713
Alex Eldera0593292012-07-19 09:09:27 -05001714 list_for_each_entry_safe(snap, next, &rbd_dev->snaps, node)
Alex Elder14e70852012-07-19 09:09:27 -05001715 __rbd_remove_snap_dev(snap);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001716}
1717
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001718/*
1719 * only read the first part of the ondisk header, without the snaps info
1720 */
Alex Elderb8136232012-07-25 09:32:41 -05001721static int __rbd_refresh_header(struct rbd_device *rbd_dev, u64 *hver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001722{
1723 int ret;
1724 struct rbd_image_header h;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001725
1726 ret = rbd_read_header(rbd_dev, &h);
1727 if (ret < 0)
1728 return ret;
1729
Josh Durgina51aa0c2011-12-05 10:35:04 -08001730 down_write(&rbd_dev->header_rwsem);
1731
Sage Weil9db4b3e2011-04-19 22:49:06 -07001732 /* resized? */
Alex Elderf84344f2012-08-31 17:29:51 -05001733 if (rbd_dev->mapping.snap_id == CEPH_NOSNAP) {
Josh Durgin474ef7c2011-11-21 17:13:54 -08001734 sector_t size = (sector_t) h.image_size / SECTOR_SIZE;
1735
Alex Elder99c1f082012-08-30 14:42:15 -05001736 if (size != (sector_t) rbd_dev->mapping.size) {
1737 dout("setting size to %llu sectors",
1738 (unsigned long long) size);
1739 rbd_dev->mapping.size = (u64) size;
1740 set_capacity(rbd_dev->disk, size);
1741 }
Josh Durgin474ef7c2011-11-21 17:13:54 -08001742 }
Sage Weil9db4b3e2011-04-19 22:49:06 -07001743
Alex Elder849b4262012-07-09 21:04:24 -05001744 /* rbd_dev->header.object_prefix shouldn't change */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001745 kfree(rbd_dev->header.snap_sizes);
Alex Elder849b4262012-07-09 21:04:24 -05001746 kfree(rbd_dev->header.snap_names);
Josh Durgind1d25642011-12-05 14:03:05 -08001747 /* osd requests may still refer to snapc */
1748 ceph_put_snap_context(rbd_dev->header.snapc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001749
Alex Elderb8136232012-07-25 09:32:41 -05001750 if (hver)
1751 *hver = h.obj_version;
Josh Durgina71b8912011-12-05 18:10:44 -08001752 rbd_dev->header.obj_version = h.obj_version;
Josh Durgin93a24e02011-12-05 10:41:28 -08001753 rbd_dev->header.image_size = h.image_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001754 rbd_dev->header.snapc = h.snapc;
1755 rbd_dev->header.snap_names = h.snap_names;
1756 rbd_dev->header.snap_sizes = h.snap_sizes;
Alex Elder849b4262012-07-09 21:04:24 -05001757 /* Free the extra copy of the object prefix */
1758 WARN_ON(strcmp(rbd_dev->header.object_prefix, h.object_prefix));
1759 kfree(h.object_prefix);
1760
Alex Elder304f6802012-08-31 17:29:52 -05001761 ret = rbd_dev_snaps_update(rbd_dev);
1762 if (!ret)
1763 ret = rbd_dev_snaps_register(rbd_dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001764
Josh Durginc6666012011-11-21 17:11:12 -08001765 up_write(&rbd_dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001766
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001767 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001768}
1769
Alex Elder1fe5e992012-07-25 09:32:41 -05001770static int rbd_refresh_header(struct rbd_device *rbd_dev, u64 *hver)
1771{
1772 int ret;
1773
1774 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
1775 ret = __rbd_refresh_header(rbd_dev, hver);
1776 mutex_unlock(&ctl_mutex);
1777
1778 return ret;
1779}
1780
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001781static int rbd_init_disk(struct rbd_device *rbd_dev)
1782{
1783 struct gendisk *disk;
1784 struct request_queue *q;
Alex Elder593a9e72012-02-07 12:03:37 -06001785 u64 segment_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001786
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001787 /* create gendisk info */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001788 disk = alloc_disk(RBD_MINORS_PER_MAJOR);
1789 if (!disk)
Alex Elder1fcdb8a2012-08-29 17:11:06 -05001790 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001791
Alex Elderf0f8cef2012-01-29 13:57:44 -06001792 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
Alex Elderde71a292012-07-03 16:01:19 -05001793 rbd_dev->dev_id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001794 disk->major = rbd_dev->major;
1795 disk->first_minor = 0;
1796 disk->fops = &rbd_bd_ops;
1797 disk->private_data = rbd_dev;
1798
1799 /* init rq */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001800 q = blk_init_queue(rbd_rq_fn, &rbd_dev->lock);
1801 if (!q)
1802 goto out_disk;
Josh Durgin029bcbd2011-07-22 11:35:23 -07001803
Alex Elder593a9e72012-02-07 12:03:37 -06001804 /* We use the default size, but let's be explicit about it. */
1805 blk_queue_physical_block_size(q, SECTOR_SIZE);
1806
Josh Durgin029bcbd2011-07-22 11:35:23 -07001807 /* set io sizes to object size */
Alex Elder593a9e72012-02-07 12:03:37 -06001808 segment_size = rbd_obj_bytes(&rbd_dev->header);
1809 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
1810 blk_queue_max_segment_size(q, segment_size);
1811 blk_queue_io_min(q, segment_size);
1812 blk_queue_io_opt(q, segment_size);
Josh Durgin029bcbd2011-07-22 11:35:23 -07001813
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001814 blk_queue_merge_bvec(q, rbd_merge_bvec);
1815 disk->queue = q;
1816
1817 q->queuedata = rbd_dev;
1818
1819 rbd_dev->disk = disk;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001820
Alex Elder12f02942012-08-29 17:11:07 -05001821 set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
1822
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001823 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001824out_disk:
1825 put_disk(disk);
Alex Elder1fcdb8a2012-08-29 17:11:06 -05001826
1827 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001828}
1829
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001830/*
1831 sysfs
1832*/
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001833
Alex Elder593a9e72012-02-07 12:03:37 -06001834static struct rbd_device *dev_to_rbd_dev(struct device *dev)
1835{
1836 return container_of(dev, struct rbd_device, dev);
1837}
1838
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001839static ssize_t rbd_size_show(struct device *dev,
1840 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001841{
Alex Elder593a9e72012-02-07 12:03:37 -06001842 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Josh Durgina51aa0c2011-12-05 10:35:04 -08001843 sector_t size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001844
Josh Durgina51aa0c2011-12-05 10:35:04 -08001845 down_read(&rbd_dev->header_rwsem);
1846 size = get_capacity(rbd_dev->disk);
1847 up_read(&rbd_dev->header_rwsem);
1848
1849 return sprintf(buf, "%llu\n", (unsigned long long) size * SECTOR_SIZE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001850}
1851
Alex Elder34b13182012-07-13 20:35:12 -05001852/*
1853 * Note this shows the features for whatever's mapped, which is not
1854 * necessarily the base image.
1855 */
1856static ssize_t rbd_features_show(struct device *dev,
1857 struct device_attribute *attr, char *buf)
1858{
1859 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
1860
1861 return sprintf(buf, "0x%016llx\n",
1862 (unsigned long long) rbd_dev->mapping.features);
1863}
1864
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001865static ssize_t rbd_major_show(struct device *dev,
1866 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001867{
Alex Elder593a9e72012-02-07 12:03:37 -06001868 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001869
1870 return sprintf(buf, "%d\n", rbd_dev->major);
1871}
1872
1873static ssize_t rbd_client_id_show(struct device *dev,
1874 struct device_attribute *attr, char *buf)
1875{
Alex Elder593a9e72012-02-07 12:03:37 -06001876 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001877
Alex Elder1dbb4392012-01-24 10:08:37 -06001878 return sprintf(buf, "client%lld\n",
1879 ceph_client_id(rbd_dev->rbd_client->client));
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001880}
1881
1882static ssize_t rbd_pool_show(struct device *dev,
1883 struct device_attribute *attr, char *buf)
1884{
Alex Elder593a9e72012-02-07 12:03:37 -06001885 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001886
1887 return sprintf(buf, "%s\n", rbd_dev->pool_name);
1888}
1889
Alex Elder9bb2f332012-07-12 10:46:35 -05001890static ssize_t rbd_pool_id_show(struct device *dev,
1891 struct device_attribute *attr, char *buf)
1892{
1893 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
1894
1895 return sprintf(buf, "%d\n", rbd_dev->pool_id);
1896}
1897
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001898static ssize_t rbd_name_show(struct device *dev,
1899 struct device_attribute *attr, char *buf)
1900{
Alex Elder593a9e72012-02-07 12:03:37 -06001901 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001902
Alex Elder0bed54d2012-07-03 16:01:18 -05001903 return sprintf(buf, "%s\n", rbd_dev->image_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001904}
1905
Alex Elder589d30e2012-07-10 20:30:11 -05001906static ssize_t rbd_image_id_show(struct device *dev,
1907 struct device_attribute *attr, char *buf)
1908{
1909 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
1910
1911 return sprintf(buf, "%s\n", rbd_dev->image_id);
1912}
1913
Alex Elder34b13182012-07-13 20:35:12 -05001914/*
1915 * Shows the name of the currently-mapped snapshot (or
1916 * RBD_SNAP_HEAD_NAME for the base image).
1917 */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001918static ssize_t rbd_snap_show(struct device *dev,
1919 struct device_attribute *attr,
1920 char *buf)
1921{
Alex Elder593a9e72012-02-07 12:03:37 -06001922 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001923
Alex Elderf84344f2012-08-31 17:29:51 -05001924 return sprintf(buf, "%s\n", rbd_dev->mapping.snap_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001925}
1926
1927static ssize_t rbd_image_refresh(struct device *dev,
1928 struct device_attribute *attr,
1929 const char *buf,
1930 size_t size)
1931{
Alex Elder593a9e72012-02-07 12:03:37 -06001932 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Alex Elderb8136232012-07-25 09:32:41 -05001933 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001934
Alex Elder1fe5e992012-07-25 09:32:41 -05001935 ret = rbd_refresh_header(rbd_dev, NULL);
Alex Elderb8136232012-07-25 09:32:41 -05001936
1937 return ret < 0 ? ret : size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001938}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001939
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001940static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
Alex Elder34b13182012-07-13 20:35:12 -05001941static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001942static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
1943static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
1944static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
Alex Elder9bb2f332012-07-12 10:46:35 -05001945static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001946static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
Alex Elder589d30e2012-07-10 20:30:11 -05001947static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001948static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
1949static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001950
1951static struct attribute *rbd_attrs[] = {
1952 &dev_attr_size.attr,
Alex Elder34b13182012-07-13 20:35:12 -05001953 &dev_attr_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001954 &dev_attr_major.attr,
1955 &dev_attr_client_id.attr,
1956 &dev_attr_pool.attr,
Alex Elder9bb2f332012-07-12 10:46:35 -05001957 &dev_attr_pool_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001958 &dev_attr_name.attr,
Alex Elder589d30e2012-07-10 20:30:11 -05001959 &dev_attr_image_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001960 &dev_attr_current_snap.attr,
1961 &dev_attr_refresh.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001962 NULL
1963};
1964
1965static struct attribute_group rbd_attr_group = {
1966 .attrs = rbd_attrs,
1967};
1968
1969static const struct attribute_group *rbd_attr_groups[] = {
1970 &rbd_attr_group,
1971 NULL
1972};
1973
1974static void rbd_sysfs_dev_release(struct device *dev)
1975{
1976}
1977
1978static struct device_type rbd_device_type = {
1979 .name = "rbd",
1980 .groups = rbd_attr_groups,
1981 .release = rbd_sysfs_dev_release,
1982};
1983
1984
1985/*
1986 sysfs - snapshots
1987*/
1988
1989static ssize_t rbd_snap_size_show(struct device *dev,
1990 struct device_attribute *attr,
1991 char *buf)
1992{
1993 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
1994
Josh Durgin3591538f2011-12-05 18:25:13 -08001995 return sprintf(buf, "%llu\n", (unsigned long long)snap->size);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001996}
1997
1998static ssize_t rbd_snap_id_show(struct device *dev,
1999 struct device_attribute *attr,
2000 char *buf)
2001{
2002 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2003
Josh Durgin3591538f2011-12-05 18:25:13 -08002004 return sprintf(buf, "%llu\n", (unsigned long long)snap->id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002005}
2006
Alex Elder34b13182012-07-13 20:35:12 -05002007static ssize_t rbd_snap_features_show(struct device *dev,
2008 struct device_attribute *attr,
2009 char *buf)
2010{
2011 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2012
2013 return sprintf(buf, "0x%016llx\n",
2014 (unsigned long long) snap->features);
2015}
2016
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002017static DEVICE_ATTR(snap_size, S_IRUGO, rbd_snap_size_show, NULL);
2018static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL);
Alex Elder34b13182012-07-13 20:35:12 -05002019static DEVICE_ATTR(snap_features, S_IRUGO, rbd_snap_features_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002020
2021static struct attribute *rbd_snap_attrs[] = {
2022 &dev_attr_snap_size.attr,
2023 &dev_attr_snap_id.attr,
Alex Elder34b13182012-07-13 20:35:12 -05002024 &dev_attr_snap_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002025 NULL,
2026};
2027
2028static struct attribute_group rbd_snap_attr_group = {
2029 .attrs = rbd_snap_attrs,
2030};
2031
2032static void rbd_snap_dev_release(struct device *dev)
2033{
2034 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2035 kfree(snap->name);
2036 kfree(snap);
2037}
2038
2039static const struct attribute_group *rbd_snap_attr_groups[] = {
2040 &rbd_snap_attr_group,
2041 NULL
2042};
2043
2044static struct device_type rbd_snap_device_type = {
2045 .groups = rbd_snap_attr_groups,
2046 .release = rbd_snap_dev_release,
2047};
2048
Alex Elder304f6802012-08-31 17:29:52 -05002049static bool rbd_snap_registered(struct rbd_snap *snap)
2050{
2051 bool ret = snap->dev.type == &rbd_snap_device_type;
2052 bool reg = device_is_registered(&snap->dev);
2053
2054 rbd_assert(!ret ^ reg);
2055
2056 return ret;
2057}
2058
Alex Elder14e70852012-07-19 09:09:27 -05002059static void __rbd_remove_snap_dev(struct rbd_snap *snap)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002060{
2061 list_del(&snap->node);
Alex Elder304f6802012-08-31 17:29:52 -05002062 if (device_is_registered(&snap->dev))
2063 device_unregister(&snap->dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002064}
2065
Alex Elder14e70852012-07-19 09:09:27 -05002066static int rbd_register_snap_dev(struct rbd_snap *snap,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002067 struct device *parent)
2068{
2069 struct device *dev = &snap->dev;
2070 int ret;
2071
2072 dev->type = &rbd_snap_device_type;
2073 dev->parent = parent;
2074 dev->release = rbd_snap_dev_release;
2075 dev_set_name(dev, "snap_%s", snap->name);
Alex Elder304f6802012-08-31 17:29:52 -05002076 dout("%s: registering device for snapshot %s\n", __func__, snap->name);
2077
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002078 ret = device_register(dev);
2079
2080 return ret;
2081}
2082
Alex Elder4e891e02012-07-10 20:30:10 -05002083static struct rbd_snap *__rbd_add_snap_dev(struct rbd_device *rbd_dev,
Alex Elderc8d18422012-07-10 20:30:11 -05002084 const char *snap_name,
Alex Elder34b13182012-07-13 20:35:12 -05002085 u64 snap_id, u64 snap_size,
2086 u64 snap_features)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002087{
Alex Elder4e891e02012-07-10 20:30:10 -05002088 struct rbd_snap *snap;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002089 int ret;
Alex Elder4e891e02012-07-10 20:30:10 -05002090
2091 snap = kzalloc(sizeof (*snap), GFP_KERNEL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002092 if (!snap)
Alex Elder4e891e02012-07-10 20:30:10 -05002093 return ERR_PTR(-ENOMEM);
2094
2095 ret = -ENOMEM;
Alex Elderc8d18422012-07-10 20:30:11 -05002096 snap->name = kstrdup(snap_name, GFP_KERNEL);
Alex Elder4e891e02012-07-10 20:30:10 -05002097 if (!snap->name)
2098 goto err;
2099
Alex Elderc8d18422012-07-10 20:30:11 -05002100 snap->id = snap_id;
2101 snap->size = snap_size;
Alex Elder34b13182012-07-13 20:35:12 -05002102 snap->features = snap_features;
Alex Elder4e891e02012-07-10 20:30:10 -05002103
2104 return snap;
2105
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002106err:
2107 kfree(snap->name);
2108 kfree(snap);
Alex Elder4e891e02012-07-10 20:30:10 -05002109
2110 return ERR_PTR(ret);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002111}
2112
Alex Eldercd892122012-07-03 16:01:19 -05002113static char *rbd_dev_v1_snap_info(struct rbd_device *rbd_dev, u32 which,
2114 u64 *snap_size, u64 *snap_features)
2115{
2116 char *snap_name;
2117
2118 rbd_assert(which < rbd_dev->header.snapc->num_snaps);
2119
2120 *snap_size = rbd_dev->header.snap_sizes[which];
2121 *snap_features = 0; /* No features for v1 */
2122
2123 /* Skip over names until we find the one we are looking for */
2124
2125 snap_name = rbd_dev->header.snap_names;
2126 while (which--)
2127 snap_name += strlen(snap_name) + 1;
2128
2129 return snap_name;
2130}
2131
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002132/*
Alex Elder9d475de2012-07-03 16:01:19 -05002133 * Get the size and object order for an image snapshot, or if
2134 * snap_id is CEPH_NOSNAP, gets this information for the base
2135 * image.
2136 */
2137static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
2138 u8 *order, u64 *snap_size)
2139{
2140 __le64 snapid = cpu_to_le64(snap_id);
2141 int ret;
2142 struct {
2143 u8 order;
2144 __le64 size;
2145 } __attribute__ ((packed)) size_buf = { 0 };
2146
2147 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2148 "rbd", "get_size",
2149 (char *) &snapid, sizeof (snapid),
2150 (char *) &size_buf, sizeof (size_buf),
2151 CEPH_OSD_FLAG_READ, NULL);
2152 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2153 if (ret < 0)
2154 return ret;
2155
2156 *order = size_buf.order;
2157 *snap_size = le64_to_cpu(size_buf.size);
2158
2159 dout(" snap_id 0x%016llx order = %u, snap_size = %llu\n",
2160 (unsigned long long) snap_id, (unsigned int) *order,
2161 (unsigned long long) *snap_size);
2162
2163 return 0;
2164}
2165
2166static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
2167{
2168 return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
2169 &rbd_dev->header.obj_order,
2170 &rbd_dev->header.image_size);
2171}
2172
Alex Elder1e130192012-07-03 16:01:19 -05002173static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
2174{
2175 void *reply_buf;
2176 int ret;
2177 void *p;
2178
2179 reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL);
2180 if (!reply_buf)
2181 return -ENOMEM;
2182
2183 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2184 "rbd", "get_object_prefix",
2185 NULL, 0,
2186 reply_buf, RBD_OBJ_PREFIX_LEN_MAX,
2187 CEPH_OSD_FLAG_READ, NULL);
2188 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2189 if (ret < 0)
2190 goto out;
2191
2192 p = reply_buf;
2193 rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p,
2194 p + RBD_OBJ_PREFIX_LEN_MAX,
2195 NULL, GFP_NOIO);
2196
2197 if (IS_ERR(rbd_dev->header.object_prefix)) {
2198 ret = PTR_ERR(rbd_dev->header.object_prefix);
2199 rbd_dev->header.object_prefix = NULL;
2200 } else {
2201 dout(" object_prefix = %s\n", rbd_dev->header.object_prefix);
2202 }
2203
2204out:
2205 kfree(reply_buf);
2206
2207 return ret;
2208}
2209
Alex Elderb1b54022012-07-03 16:01:19 -05002210static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
2211 u64 *snap_features)
2212{
2213 __le64 snapid = cpu_to_le64(snap_id);
2214 struct {
2215 __le64 features;
2216 __le64 incompat;
2217 } features_buf = { 0 };
2218 int ret;
2219
2220 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2221 "rbd", "get_features",
2222 (char *) &snapid, sizeof (snapid),
2223 (char *) &features_buf, sizeof (features_buf),
2224 CEPH_OSD_FLAG_READ, NULL);
2225 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2226 if (ret < 0)
2227 return ret;
2228 *snap_features = le64_to_cpu(features_buf.features);
2229
2230 dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
2231 (unsigned long long) snap_id,
2232 (unsigned long long) *snap_features,
2233 (unsigned long long) le64_to_cpu(features_buf.incompat));
2234
2235 return 0;
2236}
2237
2238static int rbd_dev_v2_features(struct rbd_device *rbd_dev)
2239{
2240 return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP,
2241 &rbd_dev->header.features);
2242}
2243
Alex Elder35d489f2012-07-03 16:01:19 -05002244static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev)
2245{
2246 size_t size;
2247 int ret;
2248 void *reply_buf;
2249 void *p;
2250 void *end;
2251 u64 seq;
2252 u32 snap_count;
2253 struct ceph_snap_context *snapc;
2254 u32 i;
2255
2256 /*
2257 * We'll need room for the seq value (maximum snapshot id),
2258 * snapshot count, and array of that many snapshot ids.
2259 * For now we have a fixed upper limit on the number we're
2260 * prepared to receive.
2261 */
2262 size = sizeof (__le64) + sizeof (__le32) +
2263 RBD_MAX_SNAP_COUNT * sizeof (__le64);
2264 reply_buf = kzalloc(size, GFP_KERNEL);
2265 if (!reply_buf)
2266 return -ENOMEM;
2267
2268 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2269 "rbd", "get_snapcontext",
2270 NULL, 0,
2271 reply_buf, size,
2272 CEPH_OSD_FLAG_READ, NULL);
2273 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2274 if (ret < 0)
2275 goto out;
2276
2277 ret = -ERANGE;
2278 p = reply_buf;
2279 end = (char *) reply_buf + size;
2280 ceph_decode_64_safe(&p, end, seq, out);
2281 ceph_decode_32_safe(&p, end, snap_count, out);
2282
2283 /*
2284 * Make sure the reported number of snapshot ids wouldn't go
2285 * beyond the end of our buffer. But before checking that,
2286 * make sure the computed size of the snapshot context we
2287 * allocate is representable in a size_t.
2288 */
2289 if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
2290 / sizeof (u64)) {
2291 ret = -EINVAL;
2292 goto out;
2293 }
2294 if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
2295 goto out;
2296
2297 size = sizeof (struct ceph_snap_context) +
2298 snap_count * sizeof (snapc->snaps[0]);
2299 snapc = kmalloc(size, GFP_KERNEL);
2300 if (!snapc) {
2301 ret = -ENOMEM;
2302 goto out;
2303 }
2304
2305 atomic_set(&snapc->nref, 1);
2306 snapc->seq = seq;
2307 snapc->num_snaps = snap_count;
2308 for (i = 0; i < snap_count; i++)
2309 snapc->snaps[i] = ceph_decode_64(&p);
2310
2311 rbd_dev->header.snapc = snapc;
2312
2313 dout(" snap context seq = %llu, snap_count = %u\n",
2314 (unsigned long long) seq, (unsigned int) snap_count);
2315
2316out:
2317 kfree(reply_buf);
2318
2319 return 0;
2320}
2321
Alex Elder9d475de2012-07-03 16:01:19 -05002322/*
Alex Elder35938152012-08-02 11:29:46 -05002323 * Scan the rbd device's current snapshot list and compare it to the
2324 * newly-received snapshot context. Remove any existing snapshots
2325 * not present in the new snapshot context. Add a new snapshot for
2326 * any snaphots in the snapshot context not in the current list.
2327 * And verify there are no changes to snapshots we already know
2328 * about.
2329 *
2330 * Assumes the snapshots in the snapshot context are sorted by
2331 * snapshot id, highest id first. (Snapshots in the rbd_dev's list
2332 * are also maintained in that order.)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002333 */
Alex Elder304f6802012-08-31 17:29:52 -05002334static int rbd_dev_snaps_update(struct rbd_device *rbd_dev)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002335{
Alex Elder35938152012-08-02 11:29:46 -05002336 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
2337 const u32 snap_count = snapc->num_snaps;
Alex Elder35938152012-08-02 11:29:46 -05002338 struct list_head *head = &rbd_dev->snaps;
2339 struct list_head *links = head->next;
2340 u32 index = 0;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002341
Alex Elder9fcbb802012-08-23 23:48:49 -05002342 dout("%s: snap count is %u\n", __func__, (unsigned int) snap_count);
Alex Elder35938152012-08-02 11:29:46 -05002343 while (index < snap_count || links != head) {
2344 u64 snap_id;
2345 struct rbd_snap *snap;
Alex Eldercd892122012-07-03 16:01:19 -05002346 char *snap_name;
2347 u64 snap_size = 0;
2348 u64 snap_features = 0;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002349
Alex Elder35938152012-08-02 11:29:46 -05002350 snap_id = index < snap_count ? snapc->snaps[index]
2351 : CEPH_NOSNAP;
2352 snap = links != head ? list_entry(links, struct rbd_snap, node)
2353 : NULL;
Alex Elderaafb2302012-09-06 16:00:54 -05002354 rbd_assert(!snap || snap->id != CEPH_NOSNAP);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002355
Alex Elder35938152012-08-02 11:29:46 -05002356 if (snap_id == CEPH_NOSNAP || (snap && snap->id > snap_id)) {
2357 struct list_head *next = links->next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002358
Alex Elder35938152012-08-02 11:29:46 -05002359 /* Existing snapshot not in the new snap context */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002360
Alex Elderf84344f2012-08-31 17:29:51 -05002361 if (rbd_dev->mapping.snap_id == snap->id)
2362 rbd_dev->mapping.snap_exists = false;
Alex Elder35938152012-08-02 11:29:46 -05002363 __rbd_remove_snap_dev(snap);
Alex Elder9fcbb802012-08-23 23:48:49 -05002364 dout("%ssnap id %llu has been removed\n",
Alex Elderf84344f2012-08-31 17:29:51 -05002365 rbd_dev->mapping.snap_id == snap->id ?
2366 "mapped " : "",
Alex Elder9fcbb802012-08-23 23:48:49 -05002367 (unsigned long long) snap->id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002368
Alex Elder35938152012-08-02 11:29:46 -05002369 /* Done with this list entry; advance */
2370
2371 links = next;
2372 continue;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002373 }
Alex Elder35938152012-08-02 11:29:46 -05002374
Alex Eldercd892122012-07-03 16:01:19 -05002375 snap_name = rbd_dev_v1_snap_info(rbd_dev, index,
2376 &snap_size, &snap_features);
2377 if (IS_ERR(snap_name))
2378 return PTR_ERR(snap_name);
2379
Alex Elder9fcbb802012-08-23 23:48:49 -05002380 dout("entry %u: snap_id = %llu\n", (unsigned int) snap_count,
2381 (unsigned long long) snap_id);
Alex Elder35938152012-08-02 11:29:46 -05002382 if (!snap || (snap_id != CEPH_NOSNAP && snap->id < snap_id)) {
2383 struct rbd_snap *new_snap;
2384
2385 /* We haven't seen this snapshot before */
2386
Alex Elderc8d18422012-07-10 20:30:11 -05002387 new_snap = __rbd_add_snap_dev(rbd_dev, snap_name,
Alex Eldercd892122012-07-03 16:01:19 -05002388 snap_id, snap_size, snap_features);
Alex Elder9fcbb802012-08-23 23:48:49 -05002389 if (IS_ERR(new_snap)) {
2390 int err = PTR_ERR(new_snap);
2391
2392 dout(" failed to add dev, error %d\n", err);
2393
2394 return err;
2395 }
Alex Elder35938152012-08-02 11:29:46 -05002396
2397 /* New goes before existing, or at end of list */
2398
Alex Elder9fcbb802012-08-23 23:48:49 -05002399 dout(" added dev%s\n", snap ? "" : " at end\n");
Alex Elder35938152012-08-02 11:29:46 -05002400 if (snap)
2401 list_add_tail(&new_snap->node, &snap->node);
2402 else
Alex Elder523f3252012-08-30 00:16:37 -05002403 list_add_tail(&new_snap->node, head);
Alex Elder35938152012-08-02 11:29:46 -05002404 } else {
2405 /* Already have this one */
2406
Alex Elder9fcbb802012-08-23 23:48:49 -05002407 dout(" already present\n");
2408
Alex Eldercd892122012-07-03 16:01:19 -05002409 rbd_assert(snap->size == snap_size);
Alex Elderaafb2302012-09-06 16:00:54 -05002410 rbd_assert(!strcmp(snap->name, snap_name));
Alex Eldercd892122012-07-03 16:01:19 -05002411 rbd_assert(snap->features == snap_features);
Alex Elder35938152012-08-02 11:29:46 -05002412
2413 /* Done with this list entry; advance */
2414
2415 links = links->next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002416 }
Alex Elder35938152012-08-02 11:29:46 -05002417
2418 /* Advance to the next entry in the snapshot context */
2419
2420 index++;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002421 }
Alex Elder9fcbb802012-08-23 23:48:49 -05002422 dout("%s: done\n", __func__);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002423
2424 return 0;
2425}
2426
Alex Elder304f6802012-08-31 17:29:52 -05002427/*
2428 * Scan the list of snapshots and register the devices for any that
2429 * have not already been registered.
2430 */
2431static int rbd_dev_snaps_register(struct rbd_device *rbd_dev)
2432{
2433 struct rbd_snap *snap;
2434 int ret = 0;
2435
2436 dout("%s called\n", __func__);
Alex Elder86ff77b2012-08-31 17:29:53 -05002437 if (WARN_ON(!device_is_registered(&rbd_dev->dev)))
2438 return -EIO;
Alex Elder304f6802012-08-31 17:29:52 -05002439
2440 list_for_each_entry(snap, &rbd_dev->snaps, node) {
2441 if (!rbd_snap_registered(snap)) {
2442 ret = rbd_register_snap_dev(snap, &rbd_dev->dev);
2443 if (ret < 0)
2444 break;
2445 }
2446 }
2447 dout("%s: returning %d\n", __func__, ret);
2448
2449 return ret;
2450}
2451
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002452static int rbd_bus_add_dev(struct rbd_device *rbd_dev)
2453{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002454 struct device *dev;
Alex Eldercd789ab2012-08-30 00:16:38 -05002455 int ret;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002456
2457 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002458
Alex Eldercd789ab2012-08-30 00:16:38 -05002459 dev = &rbd_dev->dev;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002460 dev->bus = &rbd_bus_type;
2461 dev->type = &rbd_device_type;
2462 dev->parent = &rbd_root_dev;
2463 dev->release = rbd_dev_release;
Alex Elderde71a292012-07-03 16:01:19 -05002464 dev_set_name(dev, "%d", rbd_dev->dev_id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002465 ret = device_register(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002466
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002467 mutex_unlock(&ctl_mutex);
Alex Eldercd789ab2012-08-30 00:16:38 -05002468
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002469 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002470}
2471
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002472static void rbd_bus_del_dev(struct rbd_device *rbd_dev)
2473{
2474 device_unregister(&rbd_dev->dev);
2475}
2476
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002477static int rbd_init_watch_dev(struct rbd_device *rbd_dev)
2478{
2479 int ret, rc;
2480
2481 do {
Alex Elder0e6f3222012-07-25 09:32:40 -05002482 ret = rbd_req_sync_watch(rbd_dev);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002483 if (ret == -ERANGE) {
Alex Elder1fe5e992012-07-25 09:32:41 -05002484 rc = rbd_refresh_header(rbd_dev, NULL);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002485 if (rc < 0)
2486 return rc;
2487 }
2488 } while (ret == -ERANGE);
2489
2490 return ret;
2491}
2492
Alex Eldere2839302012-08-29 17:11:06 -05002493static atomic64_t rbd_dev_id_max = ATOMIC64_INIT(0);
Alex Elder1ddbe942012-01-29 13:57:44 -06002494
2495/*
Alex Elder499afd52012-02-02 08:13:29 -06002496 * Get a unique rbd identifier for the given new rbd_dev, and add
2497 * the rbd_dev to the global list. The minimum rbd id is 1.
Alex Elder1ddbe942012-01-29 13:57:44 -06002498 */
Alex Eldere2839302012-08-29 17:11:06 -05002499static void rbd_dev_id_get(struct rbd_device *rbd_dev)
Alex Elderb7f23c32012-01-29 13:57:43 -06002500{
Alex Eldere2839302012-08-29 17:11:06 -05002501 rbd_dev->dev_id = atomic64_inc_return(&rbd_dev_id_max);
Alex Elder499afd52012-02-02 08:13:29 -06002502
2503 spin_lock(&rbd_dev_list_lock);
2504 list_add_tail(&rbd_dev->node, &rbd_dev_list);
2505 spin_unlock(&rbd_dev_list_lock);
Alex Eldere2839302012-08-29 17:11:06 -05002506 dout("rbd_dev %p given dev id %llu\n", rbd_dev,
2507 (unsigned long long) rbd_dev->dev_id);
Alex Elder1ddbe942012-01-29 13:57:44 -06002508}
Alex Elderb7f23c32012-01-29 13:57:43 -06002509
Alex Elder1ddbe942012-01-29 13:57:44 -06002510/*
Alex Elder499afd52012-02-02 08:13:29 -06002511 * Remove an rbd_dev from the global list, and record that its
2512 * identifier is no longer in use.
Alex Elder1ddbe942012-01-29 13:57:44 -06002513 */
Alex Eldere2839302012-08-29 17:11:06 -05002514static void rbd_dev_id_put(struct rbd_device *rbd_dev)
Alex Elder1ddbe942012-01-29 13:57:44 -06002515{
Alex Elderd184f6b2012-01-29 13:57:44 -06002516 struct list_head *tmp;
Alex Elderde71a292012-07-03 16:01:19 -05002517 int rbd_id = rbd_dev->dev_id;
Alex Elderd184f6b2012-01-29 13:57:44 -06002518 int max_id;
2519
Alex Elderaafb2302012-09-06 16:00:54 -05002520 rbd_assert(rbd_id > 0);
Alex Elder499afd52012-02-02 08:13:29 -06002521
Alex Eldere2839302012-08-29 17:11:06 -05002522 dout("rbd_dev %p released dev id %llu\n", rbd_dev,
2523 (unsigned long long) rbd_dev->dev_id);
Alex Elder499afd52012-02-02 08:13:29 -06002524 spin_lock(&rbd_dev_list_lock);
2525 list_del_init(&rbd_dev->node);
Alex Elderd184f6b2012-01-29 13:57:44 -06002526
2527 /*
2528 * If the id being "put" is not the current maximum, there
2529 * is nothing special we need to do.
2530 */
Alex Eldere2839302012-08-29 17:11:06 -05002531 if (rbd_id != atomic64_read(&rbd_dev_id_max)) {
Alex Elderd184f6b2012-01-29 13:57:44 -06002532 spin_unlock(&rbd_dev_list_lock);
2533 return;
2534 }
2535
2536 /*
2537 * We need to update the current maximum id. Search the
2538 * list to find out what it is. We're more likely to find
2539 * the maximum at the end, so search the list backward.
2540 */
2541 max_id = 0;
2542 list_for_each_prev(tmp, &rbd_dev_list) {
2543 struct rbd_device *rbd_dev;
2544
2545 rbd_dev = list_entry(tmp, struct rbd_device, node);
2546 if (rbd_id > max_id)
2547 max_id = rbd_id;
2548 }
Alex Elder499afd52012-02-02 08:13:29 -06002549 spin_unlock(&rbd_dev_list_lock);
Alex Elderb7f23c32012-01-29 13:57:43 -06002550
Alex Elder1ddbe942012-01-29 13:57:44 -06002551 /*
Alex Eldere2839302012-08-29 17:11:06 -05002552 * The max id could have been updated by rbd_dev_id_get(), in
Alex Elderd184f6b2012-01-29 13:57:44 -06002553 * which case it now accurately reflects the new maximum.
2554 * Be careful not to overwrite the maximum value in that
2555 * case.
Alex Elder1ddbe942012-01-29 13:57:44 -06002556 */
Alex Eldere2839302012-08-29 17:11:06 -05002557 atomic64_cmpxchg(&rbd_dev_id_max, rbd_id, max_id);
2558 dout(" max dev id has been reset\n");
Alex Elderb7f23c32012-01-29 13:57:43 -06002559}
2560
Alex Eldera725f65e2012-02-02 08:13:30 -06002561/*
Alex Eldere28fff262012-02-02 08:13:30 -06002562 * Skips over white space at *buf, and updates *buf to point to the
2563 * first found non-space character (if any). Returns the length of
Alex Elder593a9e72012-02-07 12:03:37 -06002564 * the token (string of non-white space characters) found. Note
2565 * that *buf must be terminated with '\0'.
Alex Eldere28fff262012-02-02 08:13:30 -06002566 */
2567static inline size_t next_token(const char **buf)
2568{
2569 /*
2570 * These are the characters that produce nonzero for
2571 * isspace() in the "C" and "POSIX" locales.
2572 */
2573 const char *spaces = " \f\n\r\t\v";
2574
2575 *buf += strspn(*buf, spaces); /* Find start of token */
2576
2577 return strcspn(*buf, spaces); /* Return token length */
2578}
2579
2580/*
2581 * Finds the next token in *buf, and if the provided token buffer is
2582 * big enough, copies the found token into it. The result, if
Alex Elder593a9e72012-02-07 12:03:37 -06002583 * copied, is guaranteed to be terminated with '\0'. Note that *buf
2584 * must be terminated with '\0' on entry.
Alex Eldere28fff262012-02-02 08:13:30 -06002585 *
2586 * Returns the length of the token found (not including the '\0').
2587 * Return value will be 0 if no token is found, and it will be >=
2588 * token_size if the token would not fit.
2589 *
Alex Elder593a9e72012-02-07 12:03:37 -06002590 * The *buf pointer will be updated to point beyond the end of the
Alex Eldere28fff262012-02-02 08:13:30 -06002591 * found token. Note that this occurs even if the token buffer is
2592 * too small to hold it.
2593 */
2594static inline size_t copy_token(const char **buf,
2595 char *token,
2596 size_t token_size)
2597{
2598 size_t len;
2599
2600 len = next_token(buf);
2601 if (len < token_size) {
2602 memcpy(token, *buf, len);
2603 *(token + len) = '\0';
2604 }
2605 *buf += len;
2606
2607 return len;
2608}
2609
2610/*
Alex Elderea3352f2012-07-09 21:04:23 -05002611 * Finds the next token in *buf, dynamically allocates a buffer big
2612 * enough to hold a copy of it, and copies the token into the new
2613 * buffer. The copy is guaranteed to be terminated with '\0'. Note
2614 * that a duplicate buffer is created even for a zero-length token.
2615 *
2616 * Returns a pointer to the newly-allocated duplicate, or a null
2617 * pointer if memory for the duplicate was not available. If
2618 * the lenp argument is a non-null pointer, the length of the token
2619 * (not including the '\0') is returned in *lenp.
2620 *
2621 * If successful, the *buf pointer will be updated to point beyond
2622 * the end of the found token.
2623 *
2624 * Note: uses GFP_KERNEL for allocation.
2625 */
2626static inline char *dup_token(const char **buf, size_t *lenp)
2627{
2628 char *dup;
2629 size_t len;
2630
2631 len = next_token(buf);
2632 dup = kmalloc(len + 1, GFP_KERNEL);
2633 if (!dup)
2634 return NULL;
2635
2636 memcpy(dup, *buf, len);
2637 *(dup + len) = '\0';
2638 *buf += len;
2639
2640 if (lenp)
2641 *lenp = len;
2642
2643 return dup;
2644}
2645
2646/*
Alex Elder3feeb8942012-08-31 17:29:52 -05002647 * This fills in the pool_name, image_name, image_name_len, rbd_dev,
2648 * rbd_md_name, and name fields of the given rbd_dev, based on the
2649 * list of monitor addresses and other options provided via
2650 * /sys/bus/rbd/add. Returns a pointer to a dynamically-allocated
2651 * copy of the snapshot name to map if successful, or a
2652 * pointer-coded error otherwise.
Alex Elderd22f76e2012-07-12 10:46:35 -05002653 *
2654 * Note: rbd_dev is assumed to have been initially zero-filled.
Alex Eldera725f65e2012-02-02 08:13:30 -06002655 */
Alex Elder3feeb8942012-08-31 17:29:52 -05002656static char *rbd_add_parse_args(struct rbd_device *rbd_dev,
2657 const char *buf,
2658 const char **mon_addrs,
2659 size_t *mon_addrs_size,
2660 char *options,
2661 size_t options_size)
Alex Eldera725f65e2012-02-02 08:13:30 -06002662{
Alex Elderd22f76e2012-07-12 10:46:35 -05002663 size_t len;
Alex Elder3feeb8942012-08-31 17:29:52 -05002664 char *err_ptr = ERR_PTR(-EINVAL);
2665 char *snap_name;
Alex Eldere28fff262012-02-02 08:13:30 -06002666
2667 /* The first four tokens are required */
2668
Alex Elder7ef32142012-02-02 08:13:30 -06002669 len = next_token(&buf);
2670 if (!len)
Alex Elder3feeb8942012-08-31 17:29:52 -05002671 return err_ptr;
Alex Elder5214ecc2012-02-02 08:13:30 -06002672 *mon_addrs_size = len + 1;
Alex Elder7ef32142012-02-02 08:13:30 -06002673 *mon_addrs = buf;
2674
2675 buf += len;
Alex Eldera725f65e2012-02-02 08:13:30 -06002676
Alex Eldere28fff262012-02-02 08:13:30 -06002677 len = copy_token(&buf, options, options_size);
2678 if (!len || len >= options_size)
Alex Elder3feeb8942012-08-31 17:29:52 -05002679 return err_ptr;
Alex Eldera725f65e2012-02-02 08:13:30 -06002680
Alex Elder3feeb8942012-08-31 17:29:52 -05002681 err_ptr = ERR_PTR(-ENOMEM);
Alex Elderd22f76e2012-07-12 10:46:35 -05002682 rbd_dev->pool_name = dup_token(&buf, NULL);
2683 if (!rbd_dev->pool_name)
Alex Elderd22f76e2012-07-12 10:46:35 -05002684 goto out_err;
Alex Eldere28fff262012-02-02 08:13:30 -06002685
Alex Elder0bed54d2012-07-03 16:01:18 -05002686 rbd_dev->image_name = dup_token(&buf, &rbd_dev->image_name_len);
2687 if (!rbd_dev->image_name)
Alex Elderbf3e5ae2012-07-09 21:04:23 -05002688 goto out_err;
Alex Eldere28fff262012-02-02 08:13:30 -06002689
Alex Elder3feeb8942012-08-31 17:29:52 -05002690 /* Snapshot name is optional */
2691 len = next_token(&buf);
Alex Elder820a5f32012-07-09 21:04:24 -05002692 if (!len) {
Alex Elder3feeb8942012-08-31 17:29:52 -05002693 buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
2694 len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
Alex Elder849b4262012-07-09 21:04:24 -05002695 }
Alex Elder3feeb8942012-08-31 17:29:52 -05002696 snap_name = kmalloc(len + 1, GFP_KERNEL);
2697 if (!snap_name)
2698 goto out_err;
2699 memcpy(snap_name, buf, len);
2700 *(snap_name + len) = '\0';
Alex Eldere28fff262012-02-02 08:13:30 -06002701
Alex Elder3feeb8942012-08-31 17:29:52 -05002702dout(" SNAP_NAME is <%s>, len is %zd\n", snap_name, len);
2703
2704 return snap_name;
Alex Elderd22f76e2012-07-12 10:46:35 -05002705
2706out_err:
Alex Elder0bed54d2012-07-03 16:01:18 -05002707 kfree(rbd_dev->image_name);
Alex Elderd78fd7a2012-07-26 23:37:14 -05002708 rbd_dev->image_name = NULL;
2709 rbd_dev->image_name_len = 0;
Alex Elderd22f76e2012-07-12 10:46:35 -05002710 kfree(rbd_dev->pool_name);
2711 rbd_dev->pool_name = NULL;
2712
Alex Elder3feeb8942012-08-31 17:29:52 -05002713 return err_ptr;
Alex Eldera725f65e2012-02-02 08:13:30 -06002714}
2715
Alex Elder589d30e2012-07-10 20:30:11 -05002716/*
2717 * An rbd format 2 image has a unique identifier, distinct from the
2718 * name given to it by the user. Internally, that identifier is
2719 * what's used to specify the names of objects related to the image.
2720 *
2721 * A special "rbd id" object is used to map an rbd image name to its
2722 * id. If that object doesn't exist, then there is no v2 rbd image
2723 * with the supplied name.
2724 *
2725 * This function will record the given rbd_dev's image_id field if
2726 * it can be determined, and in that case will return 0. If any
2727 * errors occur a negative errno will be returned and the rbd_dev's
2728 * image_id field will be unchanged (and should be NULL).
2729 */
2730static int rbd_dev_image_id(struct rbd_device *rbd_dev)
2731{
2732 int ret;
2733 size_t size;
2734 char *object_name;
2735 void *response;
2736 void *p;
2737
2738 /*
2739 * First, see if the format 2 image id file exists, and if
2740 * so, get the image's persistent id from it.
2741 */
2742 size = sizeof (RBD_ID_PREFIX) + rbd_dev->image_name_len;
2743 object_name = kmalloc(size, GFP_NOIO);
2744 if (!object_name)
2745 return -ENOMEM;
2746 sprintf(object_name, "%s%s", RBD_ID_PREFIX, rbd_dev->image_name);
2747 dout("rbd id object name is %s\n", object_name);
2748
2749 /* Response will be an encoded string, which includes a length */
2750
2751 size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
2752 response = kzalloc(size, GFP_NOIO);
2753 if (!response) {
2754 ret = -ENOMEM;
2755 goto out;
2756 }
2757
2758 ret = rbd_req_sync_exec(rbd_dev, object_name,
2759 "rbd", "get_id",
2760 NULL, 0,
2761 response, RBD_IMAGE_ID_LEN_MAX,
2762 CEPH_OSD_FLAG_READ, NULL);
2763 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2764 if (ret < 0)
2765 goto out;
2766
2767 p = response;
2768 rbd_dev->image_id = ceph_extract_encoded_string(&p,
2769 p + RBD_IMAGE_ID_LEN_MAX,
2770 &rbd_dev->image_id_len,
2771 GFP_NOIO);
2772 if (IS_ERR(rbd_dev->image_id)) {
2773 ret = PTR_ERR(rbd_dev->image_id);
2774 rbd_dev->image_id = NULL;
2775 } else {
2776 dout("image_id is %s\n", rbd_dev->image_id);
2777 }
2778out:
2779 kfree(response);
2780 kfree(object_name);
2781
2782 return ret;
2783}
2784
Alex Eldera30b71b2012-07-10 20:30:11 -05002785static int rbd_dev_v1_probe(struct rbd_device *rbd_dev)
2786{
2787 int ret;
2788 size_t size;
2789
2790 /* Version 1 images have no id; empty string is used */
2791
2792 rbd_dev->image_id = kstrdup("", GFP_KERNEL);
2793 if (!rbd_dev->image_id)
2794 return -ENOMEM;
2795 rbd_dev->image_id_len = 0;
2796
2797 /* Record the header object name for this rbd image. */
2798
2799 size = rbd_dev->image_name_len + sizeof (RBD_SUFFIX);
2800 rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
2801 if (!rbd_dev->header_name) {
2802 ret = -ENOMEM;
2803 goto out_err;
2804 }
2805 sprintf(rbd_dev->header_name, "%s%s", rbd_dev->image_name, RBD_SUFFIX);
2806
2807 /* Populate rbd image metadata */
2808
2809 ret = rbd_read_header(rbd_dev, &rbd_dev->header);
2810 if (ret < 0)
2811 goto out_err;
2812 rbd_dev->image_format = 1;
2813
2814 dout("discovered version 1 image, header name is %s\n",
2815 rbd_dev->header_name);
2816
2817 return 0;
2818
2819out_err:
2820 kfree(rbd_dev->header_name);
2821 rbd_dev->header_name = NULL;
2822 kfree(rbd_dev->image_id);
2823 rbd_dev->image_id = NULL;
2824
2825 return ret;
2826}
2827
2828static int rbd_dev_v2_probe(struct rbd_device *rbd_dev)
2829{
2830 size_t size;
Alex Elder9d475de2012-07-03 16:01:19 -05002831 int ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05002832
2833 /*
2834 * Image id was filled in by the caller. Record the header
2835 * object name for this rbd image.
2836 */
2837 size = sizeof (RBD_HEADER_PREFIX) + rbd_dev->image_id_len;
2838 rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
2839 if (!rbd_dev->header_name)
2840 return -ENOMEM;
2841 sprintf(rbd_dev->header_name, "%s%s",
2842 RBD_HEADER_PREFIX, rbd_dev->image_id);
Alex Elder9d475de2012-07-03 16:01:19 -05002843
2844 /* Get the size and object order for the image */
2845
2846 ret = rbd_dev_v2_image_size(rbd_dev);
2847 if (ret < 0)
2848 goto out_err;
Alex Elder1e130192012-07-03 16:01:19 -05002849
2850 /* Get the object prefix (a.k.a. block_name) for the image */
2851
2852 ret = rbd_dev_v2_object_prefix(rbd_dev);
2853 if (ret < 0)
2854 goto out_err;
Alex Elderb1b54022012-07-03 16:01:19 -05002855
2856 /* Get the features for the image */
2857
2858 ret = rbd_dev_v2_features(rbd_dev);
2859 if (ret < 0)
2860 goto out_err;
Alex Elder35d489f2012-07-03 16:01:19 -05002861
2862 /* Get the snapshot context */
2863
2864 ret = rbd_dev_v2_snap_context(rbd_dev);
2865 if (ret)
2866 goto out_err;
Alex Eldera30b71b2012-07-10 20:30:11 -05002867 rbd_dev->image_format = 2;
2868
2869 dout("discovered version 2 image, header name is %s\n",
2870 rbd_dev->header_name);
2871
2872 return -ENOTSUPP;
Alex Elder9d475de2012-07-03 16:01:19 -05002873out_err:
2874 kfree(rbd_dev->header_name);
2875 rbd_dev->header_name = NULL;
Alex Elder1e130192012-07-03 16:01:19 -05002876 kfree(rbd_dev->header.object_prefix);
2877 rbd_dev->header.object_prefix = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05002878
2879 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05002880}
2881
2882/*
2883 * Probe for the existence of the header object for the given rbd
2884 * device. For format 2 images this includes determining the image
2885 * id.
2886 */
2887static int rbd_dev_probe(struct rbd_device *rbd_dev)
2888{
2889 int ret;
2890
2891 /*
2892 * Get the id from the image id object. If it's not a
2893 * format 2 image, we'll get ENOENT back, and we'll assume
2894 * it's a format 1 image.
2895 */
2896 ret = rbd_dev_image_id(rbd_dev);
2897 if (ret)
2898 ret = rbd_dev_v1_probe(rbd_dev);
2899 else
2900 ret = rbd_dev_v2_probe(rbd_dev);
2901 if (ret)
2902 dout("probe failed, returning %d\n", ret);
2903
2904 return ret;
2905}
2906
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002907static ssize_t rbd_add(struct bus_type *bus,
2908 const char *buf,
2909 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002910{
Alex Eldercb8627c2012-07-09 21:04:23 -05002911 char *options;
2912 struct rbd_device *rbd_dev = NULL;
Alex Elder7ef32142012-02-02 08:13:30 -06002913 const char *mon_addrs = NULL;
2914 size_t mon_addrs_size = 0;
Alex Elder27cc2592012-02-02 08:13:30 -06002915 struct ceph_osd_client *osdc;
2916 int rc = -ENOMEM;
Alex Elder3feeb8942012-08-31 17:29:52 -05002917 char *snap_name;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002918
2919 if (!try_module_get(THIS_MODULE))
2920 return -ENODEV;
2921
Alex Elder27cc2592012-02-02 08:13:30 -06002922 options = kmalloc(count, GFP_KERNEL);
2923 if (!options)
Alex Elder85ae8922012-07-26 23:37:14 -05002924 goto err_out_mem;
Alex Eldercb8627c2012-07-09 21:04:23 -05002925 rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL);
2926 if (!rbd_dev)
Alex Elder85ae8922012-07-26 23:37:14 -05002927 goto err_out_mem;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002928
2929 /* static rbd_device initialization */
2930 spin_lock_init(&rbd_dev->lock);
2931 INIT_LIST_HEAD(&rbd_dev->node);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002932 INIT_LIST_HEAD(&rbd_dev->snaps);
Josh Durginc6666012011-11-21 17:11:12 -08002933 init_rwsem(&rbd_dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002934
Alex Eldera725f65e2012-02-02 08:13:30 -06002935 /* parse add command */
Alex Elder3feeb8942012-08-31 17:29:52 -05002936 snap_name = rbd_add_parse_args(rbd_dev, buf,
2937 &mon_addrs, &mon_addrs_size, options, count);
2938 if (IS_ERR(snap_name)) {
2939 rc = PTR_ERR(snap_name);
Alex Elder85ae8922012-07-26 23:37:14 -05002940 goto err_out_mem;
Alex Elder3feeb8942012-08-31 17:29:52 -05002941 }
Alex Eldera725f65e2012-02-02 08:13:30 -06002942
Alex Elderf8c38922012-08-10 13:12:07 -07002943 rc = rbd_get_client(rbd_dev, mon_addrs, mon_addrs_size - 1, options);
2944 if (rc < 0)
Alex Elder85ae8922012-07-26 23:37:14 -05002945 goto err_out_args;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002946
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002947 /* pick the pool */
Alex Elder1dbb4392012-01-24 10:08:37 -06002948 osdc = &rbd_dev->rbd_client->client->osdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002949 rc = ceph_pg_poolid_by_name(osdc->osdmap, rbd_dev->pool_name);
2950 if (rc < 0)
2951 goto err_out_client;
Alex Elder9bb2f332012-07-12 10:46:35 -05002952 rbd_dev->pool_id = rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002953
Alex Eldera30b71b2012-07-10 20:30:11 -05002954 rc = rbd_dev_probe(rbd_dev);
2955 if (rc < 0)
Alex Elder589d30e2012-07-10 20:30:11 -05002956 goto err_out_client;
Alex Eldera30b71b2012-07-10 20:30:11 -05002957 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
Alex Elder05fd6f62012-08-29 17:11:07 -05002958
2959 /* no need to lock here, as rbd_dev is not registered yet */
2960 rc = rbd_dev_snaps_update(rbd_dev);
2961 if (rc)
2962 goto err_out_header;
2963
2964 rc = rbd_dev_set_mapping(rbd_dev, snap_name);
2965 if (rc)
2966 goto err_out_header;
2967
Alex Elder85ae8922012-07-26 23:37:14 -05002968 /* generate unique id: find highest unique id, add one */
2969 rbd_dev_id_get(rbd_dev);
2970
2971 /* Fill in the device name, now that we have its id. */
2972 BUILD_BUG_ON(DEV_NAME_LEN
2973 < sizeof (RBD_DRV_NAME) + MAX_INT_FORMAT_WIDTH);
2974 sprintf(rbd_dev->name, "%s%d", RBD_DRV_NAME, rbd_dev->dev_id);
2975
2976 /* Get our block major device number. */
2977
Alex Elder27cc2592012-02-02 08:13:30 -06002978 rc = register_blkdev(0, rbd_dev->name);
2979 if (rc < 0)
Alex Elder85ae8922012-07-26 23:37:14 -05002980 goto err_out_id;
Alex Elder27cc2592012-02-02 08:13:30 -06002981 rbd_dev->major = rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002982
Alex Elder0f308a32012-08-29 17:11:07 -05002983 /* Set up the blkdev mapping. */
2984
2985 rc = rbd_init_disk(rbd_dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002986 if (rc)
Yehuda Sadeh766fc432011-01-07 14:58:42 -08002987 goto err_out_blkdev;
2988
Alex Elder0f308a32012-08-29 17:11:07 -05002989 rc = rbd_bus_add_dev(rbd_dev);
2990 if (rc)
2991 goto err_out_disk;
2992
Alex Elder32eec682012-02-08 16:11:14 -06002993 /*
2994 * At this point cleanup in the event of an error is the job
2995 * of the sysfs code (initiated by rbd_bus_del_dev()).
Alex Elder32eec682012-02-08 16:11:14 -06002996 */
Alex Elder2ac4e752012-07-10 20:30:10 -05002997
Alex Elder4bb1f1e2012-08-23 23:48:49 -05002998 down_write(&rbd_dev->header_rwsem);
Alex Elder5ed16172012-08-29 17:11:07 -05002999 rc = rbd_dev_snaps_register(rbd_dev);
Alex Elder4bb1f1e2012-08-23 23:48:49 -05003000 up_write(&rbd_dev->header_rwsem);
Alex Elder2ac4e752012-07-10 20:30:10 -05003001 if (rc)
3002 goto err_out_bus;
3003
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003004 rc = rbd_init_watch_dev(rbd_dev);
3005 if (rc)
3006 goto err_out_bus;
3007
Alex Elder3ee40012012-08-29 17:11:07 -05003008 /* Everything's ready. Announce the disk to the world. */
3009
3010 add_disk(rbd_dev->disk);
3011
3012 pr_info("%s: added with size 0x%llx\n", rbd_dev->disk->disk_name,
3013 (unsigned long long) rbd_dev->mapping.size);
3014
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003015 return count;
3016
Yehuda Sadeh766fc432011-01-07 14:58:42 -08003017err_out_bus:
Yehuda Sadeh766fc432011-01-07 14:58:42 -08003018 /* this will also clean up rest of rbd_dev stuff */
3019
3020 rbd_bus_del_dev(rbd_dev);
3021 kfree(options);
Yehuda Sadeh766fc432011-01-07 14:58:42 -08003022 return rc;
3023
Alex Elder0f308a32012-08-29 17:11:07 -05003024err_out_disk:
3025 rbd_free_disk(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003026err_out_blkdev:
3027 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Alex Elder85ae8922012-07-26 23:37:14 -05003028err_out_id:
3029 rbd_dev_id_put(rbd_dev);
Alex Elder05fd6f62012-08-29 17:11:07 -05003030err_out_header:
3031 rbd_header_free(&rbd_dev->header);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003032err_out_client:
Alex Elder3fcf2582012-07-03 16:01:19 -05003033 kfree(rbd_dev->header_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003034 rbd_put_client(rbd_dev);
Alex Elder589d30e2012-07-10 20:30:11 -05003035 kfree(rbd_dev->image_id);
Alex Elder85ae8922012-07-26 23:37:14 -05003036err_out_args:
3037 kfree(rbd_dev->mapping.snap_name);
3038 kfree(rbd_dev->image_name);
3039 kfree(rbd_dev->pool_name);
3040err_out_mem:
Alex Elder27cc2592012-02-02 08:13:30 -06003041 kfree(rbd_dev);
Alex Eldercb8627c2012-07-09 21:04:23 -05003042 kfree(options);
Alex Elder27cc2592012-02-02 08:13:30 -06003043
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003044 dout("Error adding device %s\n", buf);
3045 module_put(THIS_MODULE);
Alex Elder27cc2592012-02-02 08:13:30 -06003046
3047 return (ssize_t) rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003048}
3049
Alex Elderde71a292012-07-03 16:01:19 -05003050static struct rbd_device *__rbd_get_dev(unsigned long dev_id)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003051{
3052 struct list_head *tmp;
3053 struct rbd_device *rbd_dev;
3054
Alex Eldere124a82f2012-01-29 13:57:44 -06003055 spin_lock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003056 list_for_each(tmp, &rbd_dev_list) {
3057 rbd_dev = list_entry(tmp, struct rbd_device, node);
Alex Elderde71a292012-07-03 16:01:19 -05003058 if (rbd_dev->dev_id == dev_id) {
Alex Eldere124a82f2012-01-29 13:57:44 -06003059 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003060 return rbd_dev;
Alex Eldere124a82f2012-01-29 13:57:44 -06003061 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003062 }
Alex Eldere124a82f2012-01-29 13:57:44 -06003063 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003064 return NULL;
3065}
3066
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003067static void rbd_dev_release(struct device *dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003068{
Alex Elder593a9e72012-02-07 12:03:37 -06003069 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003070
Alex Elder1dbb4392012-01-24 10:08:37 -06003071 if (rbd_dev->watch_request) {
3072 struct ceph_client *client = rbd_dev->rbd_client->client;
3073
3074 ceph_osdc_unregister_linger_request(&client->osdc,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003075 rbd_dev->watch_request);
Alex Elder1dbb4392012-01-24 10:08:37 -06003076 }
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003077 if (rbd_dev->watch_event)
Alex Elder070c6332012-07-25 09:32:41 -05003078 rbd_req_sync_unwatch(rbd_dev);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003079
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003080 rbd_put_client(rbd_dev);
3081
3082 /* clean up and free blkdev */
3083 rbd_free_disk(rbd_dev);
3084 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Alex Elder32eec682012-02-08 16:11:14 -06003085
Alex Elder2ac4e752012-07-10 20:30:10 -05003086 /* release allocated disk header fields */
3087 rbd_header_free(&rbd_dev->header);
3088
Alex Elder32eec682012-02-08 16:11:14 -06003089 /* done with the id, and with the rbd_dev */
Alex Elderf84344f2012-08-31 17:29:51 -05003090 kfree(rbd_dev->mapping.snap_name);
Alex Elder589d30e2012-07-10 20:30:11 -05003091 kfree(rbd_dev->image_id);
Alex Elder0bed54d2012-07-03 16:01:18 -05003092 kfree(rbd_dev->header_name);
Alex Elderd22f76e2012-07-12 10:46:35 -05003093 kfree(rbd_dev->pool_name);
Alex Elder0bed54d2012-07-03 16:01:18 -05003094 kfree(rbd_dev->image_name);
Alex Eldere2839302012-08-29 17:11:06 -05003095 rbd_dev_id_put(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003096 kfree(rbd_dev);
3097
3098 /* release module ref */
3099 module_put(THIS_MODULE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003100}
3101
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003102static ssize_t rbd_remove(struct bus_type *bus,
3103 const char *buf,
3104 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003105{
3106 struct rbd_device *rbd_dev = NULL;
3107 int target_id, rc;
3108 unsigned long ul;
3109 int ret = count;
3110
3111 rc = strict_strtoul(buf, 10, &ul);
3112 if (rc)
3113 return rc;
3114
3115 /* convert to int; abort if we lost anything in the conversion */
3116 target_id = (int) ul;
3117 if (target_id != ul)
3118 return -EINVAL;
3119
3120 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
3121
3122 rbd_dev = __rbd_get_dev(target_id);
3123 if (!rbd_dev) {
3124 ret = -ENOENT;
3125 goto done;
3126 }
3127
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003128 __rbd_remove_all_snaps(rbd_dev);
3129 rbd_bus_del_dev(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003130
3131done:
3132 mutex_unlock(&ctl_mutex);
Alex Elderaafb2302012-09-06 16:00:54 -05003133
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003134 return ret;
3135}
3136
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003137/*
3138 * create control files in sysfs
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003139 * /sys/bus/rbd/...
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003140 */
3141static int rbd_sysfs_init(void)
3142{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003143 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003144
Alex Elderfed4c142012-02-07 12:03:36 -06003145 ret = device_register(&rbd_root_dev);
Alex Elder21079782012-01-24 10:08:36 -06003146 if (ret < 0)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003147 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003148
Alex Elderfed4c142012-02-07 12:03:36 -06003149 ret = bus_register(&rbd_bus_type);
3150 if (ret < 0)
3151 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003152
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003153 return ret;
3154}
3155
3156static void rbd_sysfs_cleanup(void)
3157{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003158 bus_unregister(&rbd_bus_type);
Alex Elderfed4c142012-02-07 12:03:36 -06003159 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003160}
3161
3162int __init rbd_init(void)
3163{
3164 int rc;
3165
3166 rc = rbd_sysfs_init();
3167 if (rc)
3168 return rc;
Alex Elderf0f8cef2012-01-29 13:57:44 -06003169 pr_info("loaded " RBD_DRV_NAME_LONG "\n");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003170 return 0;
3171}
3172
3173void __exit rbd_exit(void)
3174{
3175 rbd_sysfs_cleanup();
3176}
3177
3178module_init(rbd_init);
3179module_exit(rbd_exit);
3180
3181MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
3182MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
3183MODULE_DESCRIPTION("rados block device");
3184
3185/* following authorship retained from original osdblk.c */
3186MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
3187
3188MODULE_LICENSE("GPL");