blob: ca59d4d9471e9bd2b445943ce475cba76fee18c9 [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 Elder593a9e72012-02-07 12:03:37 -060044/*
45 * The basic unit of block I/O is a sector. It is interpreted in a
46 * number of contexts in Linux (blk, bio, genhd), but the default is
47 * universally 512 bytes. These symbols are just slightly more
48 * meaningful than the bare numbers they represent.
49 */
50#define SECTOR_SHIFT 9
51#define SECTOR_SIZE (1ULL << SECTOR_SHIFT)
52
Alex Elderf0f8cef2012-01-29 13:57:44 -060053#define RBD_DRV_NAME "rbd"
54#define RBD_DRV_NAME_LONG "rbd (rados block device)"
Yehuda Sadeh602adf42010-08-12 16:11:25 -070055
56#define RBD_MINORS_PER_MAJOR 256 /* max minors per blkdev */
57
Alex Elder21079782012-01-24 10:08:36 -060058#define RBD_MAX_MD_NAME_LEN (RBD_MAX_OBJ_NAME_LEN + sizeof(RBD_SUFFIX))
Yehuda Sadeh602adf42010-08-12 16:11:25 -070059#define RBD_MAX_POOL_NAME_LEN 64
60#define RBD_MAX_SNAP_NAME_LEN 32
61#define RBD_MAX_OPT_LEN 1024
62
63#define RBD_SNAP_HEAD_NAME "-"
64
Alex Elder81a89792012-02-02 08:13:30 -060065/*
66 * An RBD device name will be "rbd#", where the "rbd" comes from
67 * RBD_DRV_NAME above, and # is a unique integer identifier.
68 * MAX_INT_FORMAT_WIDTH is used in ensuring DEV_NAME_LEN is big
69 * enough to hold all possible device names.
70 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -070071#define DEV_NAME_LEN 32
Alex Elder81a89792012-02-02 08:13:30 -060072#define MAX_INT_FORMAT_WIDTH ((5 * sizeof (int)) / 2 + 1)
Yehuda Sadeh602adf42010-08-12 16:11:25 -070073
Yehuda Sadeh59c2be12011-03-21 15:10:11 -070074#define RBD_NOTIFY_TIMEOUT_DEFAULT 10
75
Yehuda Sadeh602adf42010-08-12 16:11:25 -070076/*
77 * block device image metadata (in-memory version)
78 */
79struct rbd_image_header {
80 u64 image_size;
81 char block_name[32];
82 __u8 obj_order;
83 __u8 crypt_type;
84 __u8 comp_type;
Yehuda Sadeh602adf42010-08-12 16:11:25 -070085 struct ceph_snap_context *snapc;
86 size_t snap_names_len;
87 u64 snap_seq;
88 u32 total_snaps;
89
90 char *snap_names;
91 u64 *snap_sizes;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -070092
93 u64 obj_version;
94};
95
96struct rbd_options {
97 int notify_timeout;
Yehuda Sadeh602adf42010-08-12 16:11:25 -070098};
99
100/*
Alex Elderf0f8cef2012-01-29 13:57:44 -0600101 * an instance of the client. multiple devices may share an rbd client.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700102 */
103struct rbd_client {
104 struct ceph_client *client;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700105 struct rbd_options *rbd_opts;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700106 struct kref kref;
107 struct list_head node;
108};
109
110/*
Alex Elderf0f8cef2012-01-29 13:57:44 -0600111 * a request completion status
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700112 */
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700113struct rbd_req_status {
114 int done;
115 int rc;
116 u64 bytes;
117};
118
119/*
120 * a collection of requests
121 */
122struct rbd_req_coll {
123 int total;
124 int num_done;
125 struct kref kref;
126 struct rbd_req_status status[0];
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700127};
128
Alex Elderf0f8cef2012-01-29 13:57:44 -0600129/*
130 * a single io request
131 */
132struct rbd_request {
133 struct request *rq; /* blk layer request */
134 struct bio *bio; /* cloned bio */
135 struct page **pages; /* list of used pages */
136 u64 len;
137 int coll_index;
138 struct rbd_req_coll *coll;
139};
140
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800141struct rbd_snap {
142 struct device dev;
143 const char *name;
144 size_t size;
145 struct list_head node;
146 u64 id;
147};
148
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700149/*
150 * a single device
151 */
152struct rbd_device {
153 int id; /* blkdev unique id */
154
155 int major; /* blkdev assigned major */
156 struct gendisk *disk; /* blkdev's gendisk and rq */
157 struct request_queue *q;
158
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700159 struct rbd_client *rbd_client;
160
161 char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */
162
163 spinlock_t lock; /* queue lock */
164
165 struct rbd_image_header header;
166 char obj[RBD_MAX_OBJ_NAME_LEN]; /* rbd image name */
167 int obj_len;
168 char obj_md_name[RBD_MAX_MD_NAME_LEN]; /* hdr nm. */
169 char pool_name[RBD_MAX_POOL_NAME_LEN];
170 int poolid;
171
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700172 struct ceph_osd_event *watch_event;
173 struct ceph_osd_request *watch_request;
174
Josh Durginc6666012011-11-21 17:11:12 -0800175 /* protects updating the header */
176 struct rw_semaphore header_rwsem;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700177 char snap_name[RBD_MAX_SNAP_NAME_LEN];
178 u32 cur_snap; /* index+1 of current snapshot within snap context
179 0 - for the head */
180 int read_only;
181
182 struct list_head node;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800183
184 /* list of snapshots */
185 struct list_head snaps;
186
187 /* sysfs related */
188 struct device dev;
189};
190
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700191static DEFINE_MUTEX(ctl_mutex); /* Serialize open/close/setup/teardown */
Alex Eldere124a822012-01-29 13:57:44 -0600192
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700193static LIST_HEAD(rbd_dev_list); /* devices */
Alex Eldere124a822012-01-29 13:57:44 -0600194static DEFINE_SPINLOCK(rbd_dev_list_lock);
195
Alex Elder432b8582012-01-29 13:57:44 -0600196static LIST_HEAD(rbd_client_list); /* clients */
197static DEFINE_SPINLOCK(rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700198
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800199static int __rbd_init_snaps_header(struct rbd_device *rbd_dev);
200static void rbd_dev_release(struct device *dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800201static ssize_t rbd_snap_add(struct device *dev,
202 struct device_attribute *attr,
203 const char *buf,
204 size_t count);
205static void __rbd_remove_snap_dev(struct rbd_device *rbd_dev,
Justin P. Mattock69932482011-07-26 23:06:29 -0700206 struct rbd_snap *snap);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800207
Alex Elderf0f8cef2012-01-29 13:57:44 -0600208static ssize_t rbd_add(struct bus_type *bus, const char *buf,
209 size_t count);
210static ssize_t rbd_remove(struct bus_type *bus, const char *buf,
211 size_t count);
212
213static struct bus_attribute rbd_bus_attrs[] = {
214 __ATTR(add, S_IWUSR, NULL, rbd_add),
215 __ATTR(remove, S_IWUSR, NULL, rbd_remove),
216 __ATTR_NULL
217};
218
219static struct bus_type rbd_bus_type = {
220 .name = "rbd",
221 .bus_attrs = rbd_bus_attrs,
222};
223
224static void rbd_root_dev_release(struct device *dev)
225{
226}
227
228static struct device rbd_root_dev = {
229 .init_name = "rbd",
230 .release = rbd_root_dev_release,
231};
232
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800233
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800234static struct device *rbd_get_dev(struct rbd_device *rbd_dev)
235{
236 return get_device(&rbd_dev->dev);
237}
238
239static void rbd_put_dev(struct rbd_device *rbd_dev)
240{
241 put_device(&rbd_dev->dev);
242}
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700243
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700244static int __rbd_update_snaps(struct rbd_device *rbd_dev);
245
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700246static int rbd_open(struct block_device *bdev, fmode_t mode)
247{
Alex Elderf0f8cef2012-01-29 13:57:44 -0600248 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700249
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800250 rbd_get_dev(rbd_dev);
251
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700252 set_device_ro(bdev, rbd_dev->read_only);
253
254 if ((mode & FMODE_WRITE) && rbd_dev->read_only)
255 return -EROFS;
256
257 return 0;
258}
259
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800260static int rbd_release(struct gendisk *disk, fmode_t mode)
261{
262 struct rbd_device *rbd_dev = disk->private_data;
263
264 rbd_put_dev(rbd_dev);
265
266 return 0;
267}
268
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700269static const struct block_device_operations rbd_bd_ops = {
270 .owner = THIS_MODULE,
271 .open = rbd_open,
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800272 .release = rbd_release,
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700273};
274
275/*
276 * Initialize an rbd client instance.
277 * We own *opt.
278 */
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700279static struct rbd_client *rbd_client_create(struct ceph_options *opt,
280 struct rbd_options *rbd_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700281{
282 struct rbd_client *rbdc;
283 int ret = -ENOMEM;
284
285 dout("rbd_client_create\n");
286 rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL);
287 if (!rbdc)
288 goto out_opt;
289
290 kref_init(&rbdc->kref);
291 INIT_LIST_HEAD(&rbdc->node);
292
Alex Elderbc534d862012-01-29 13:57:44 -0600293 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
294
Sage Weil6ab00d42011-08-09 09:41:59 -0700295 rbdc->client = ceph_create_client(opt, rbdc, 0, 0);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700296 if (IS_ERR(rbdc->client))
Alex Elderbc534d862012-01-29 13:57:44 -0600297 goto out_mutex;
Vasiliy Kulikov28f259b2010-09-26 12:59:37 +0400298 opt = NULL; /* Now rbdc->client is responsible for opt */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700299
300 ret = ceph_open_session(rbdc->client);
301 if (ret < 0)
302 goto out_err;
303
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700304 rbdc->rbd_opts = rbd_opts;
305
Alex Elder432b8582012-01-29 13:57:44 -0600306 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700307 list_add_tail(&rbdc->node, &rbd_client_list);
Alex Elder432b8582012-01-29 13:57:44 -0600308 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700309
Alex Elderbc534d862012-01-29 13:57:44 -0600310 mutex_unlock(&ctl_mutex);
311
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700312 dout("rbd_client_create created %p\n", rbdc);
313 return rbdc;
314
315out_err:
316 ceph_destroy_client(rbdc->client);
Alex Elderbc534d862012-01-29 13:57:44 -0600317out_mutex:
318 mutex_unlock(&ctl_mutex);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700319 kfree(rbdc);
320out_opt:
Vasiliy Kulikov28f259b2010-09-26 12:59:37 +0400321 if (opt)
322 ceph_destroy_options(opt);
323 return ERR_PTR(ret);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700324}
325
326/*
327 * Find a ceph client with specific addr and configuration.
328 */
329static struct rbd_client *__rbd_client_find(struct ceph_options *opt)
330{
331 struct rbd_client *client_node;
332
333 if (opt->flags & CEPH_OPT_NOSHARE)
334 return NULL;
335
336 list_for_each_entry(client_node, &rbd_client_list, node)
337 if (ceph_compare_options(opt, client_node->client) == 0)
338 return client_node;
339 return NULL;
340}
341
342/*
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700343 * mount options
344 */
345enum {
346 Opt_notify_timeout,
347 Opt_last_int,
348 /* int args above */
349 Opt_last_string,
350 /* string args above */
351};
352
353static match_table_t rbdopt_tokens = {
354 {Opt_notify_timeout, "notify_timeout=%d"},
355 /* int args above */
356 /* string args above */
357 {-1, NULL}
358};
359
360static int parse_rbd_opts_token(char *c, void *private)
361{
362 struct rbd_options *rbdopt = private;
363 substring_t argstr[MAX_OPT_ARGS];
364 int token, intval, ret;
365
Alex Elder21079782012-01-24 10:08:36 -0600366 token = match_token(c, rbdopt_tokens, argstr);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700367 if (token < 0)
368 return -EINVAL;
369
370 if (token < Opt_last_int) {
371 ret = match_int(&argstr[0], &intval);
372 if (ret < 0) {
373 pr_err("bad mount option arg (not int) "
374 "at '%s'\n", c);
375 return ret;
376 }
377 dout("got int token %d val %d\n", token, intval);
378 } else if (token > Opt_last_int && token < Opt_last_string) {
379 dout("got string token %d val %s\n", token,
380 argstr[0].from);
381 } else {
382 dout("got token %d\n", token);
383 }
384
385 switch (token) {
386 case Opt_notify_timeout:
387 rbdopt->notify_timeout = intval;
388 break;
389 default:
390 BUG_ON(token);
391 }
392 return 0;
393}
394
395/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700396 * Get a ceph client with specific addr and configuration, if one does
397 * not exist create it.
398 */
Alex Elder5214ecc2012-02-02 08:13:30 -0600399static struct rbd_client *rbd_get_client(const char *mon_addr,
400 size_t mon_addr_len,
401 char *options)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700402{
403 struct rbd_client *rbdc;
404 struct ceph_options *opt;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700405 struct rbd_options *rbd_opts;
406
407 rbd_opts = kzalloc(sizeof(*rbd_opts), GFP_KERNEL);
408 if (!rbd_opts)
Alex Elderd720bcb2012-02-02 08:13:30 -0600409 return ERR_PTR(-ENOMEM);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700410
411 rbd_opts->notify_timeout = RBD_NOTIFY_TIMEOUT_DEFAULT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700412
Alex Elderee577412012-01-24 10:08:36 -0600413 opt = ceph_parse_options(options, mon_addr,
Alex Elder5214ecc2012-02-02 08:13:30 -0600414 mon_addr + mon_addr_len,
Alex Elder21079782012-01-24 10:08:36 -0600415 parse_rbd_opts_token, rbd_opts);
Alex Elderee577412012-01-24 10:08:36 -0600416 if (IS_ERR(opt)) {
Alex Elderd720bcb2012-02-02 08:13:30 -0600417 kfree(rbd_opts);
418 return ERR_CAST(opt);
Alex Elderee577412012-01-24 10:08:36 -0600419 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700420
Alex Elder432b8582012-01-29 13:57:44 -0600421 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700422 rbdc = __rbd_client_find(opt);
423 if (rbdc) {
Alex Eldere6994d32012-01-29 13:57:44 -0600424 /* using an existing client */
425 kref_get(&rbdc->kref);
Alex Elder432b8582012-01-29 13:57:44 -0600426 spin_unlock(&rbd_client_list_lock);
Alex Eldere6994d32012-01-29 13:57:44 -0600427
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700428 ceph_destroy_options(opt);
Alex Elder97bb59a2012-01-24 10:08:36 -0600429 kfree(rbd_opts);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700430
Alex Elderd720bcb2012-02-02 08:13:30 -0600431 return rbdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700432 }
Alex Elder432b8582012-01-29 13:57:44 -0600433 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700434
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700435 rbdc = rbd_client_create(opt, rbd_opts);
Alex Elderd97081b2012-01-29 13:57:44 -0600436
Alex Elderd720bcb2012-02-02 08:13:30 -0600437 if (IS_ERR(rbdc))
438 kfree(rbd_opts);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700439
Alex Elderd720bcb2012-02-02 08:13:30 -0600440 return rbdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700441}
442
443/*
444 * Destroy ceph client
Alex Elderd23a4b32012-01-29 13:57:43 -0600445 *
Alex Elder432b8582012-01-29 13:57:44 -0600446 * Caller must hold rbd_client_list_lock.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700447 */
448static void rbd_client_release(struct kref *kref)
449{
450 struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
451
452 dout("rbd_release_client %p\n", rbdc);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500453 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700454 list_del(&rbdc->node);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500455 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700456
457 ceph_destroy_client(rbdc->client);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700458 kfree(rbdc->rbd_opts);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700459 kfree(rbdc);
460}
461
462/*
463 * Drop reference to ceph client node. If it's not referenced anymore, release
464 * it.
465 */
466static void rbd_put_client(struct rbd_device *rbd_dev)
467{
468 kref_put(&rbd_dev->rbd_client->kref, rbd_client_release);
469 rbd_dev->rbd_client = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700470}
471
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700472/*
473 * Destroy requests collection
474 */
475static void rbd_coll_release(struct kref *kref)
476{
477 struct rbd_req_coll *coll =
478 container_of(kref, struct rbd_req_coll, kref);
479
480 dout("rbd_coll_release %p\n", coll);
481 kfree(coll);
482}
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700483
484/*
485 * Create a new header structure, translate header format from the on-disk
486 * header.
487 */
488static int rbd_header_from_disk(struct rbd_image_header *header,
489 struct rbd_image_header_ondisk *ondisk,
490 int allocated_snaps,
491 gfp_t gfp_flags)
492{
493 int i;
Alex Elder00f1f362012-02-07 12:03:36 -0600494 u32 snap_count;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700495
Alex Elder21079782012-01-24 10:08:36 -0600496 if (memcmp(ondisk, RBD_HEADER_TEXT, sizeof(RBD_HEADER_TEXT)))
Josh Durgin81e759f2011-11-15 14:49:53 -0800497 return -ENXIO;
Josh Durgin81e759f2011-11-15 14:49:53 -0800498
Alex Elder00f1f362012-02-07 12:03:36 -0600499 snap_count = le32_to_cpu(ondisk->snap_count);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700500 header->snapc = kmalloc(sizeof(struct ceph_snap_context) +
Alex Elder21079782012-01-24 10:08:36 -0600501 snap_count * sizeof (*ondisk),
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700502 gfp_flags);
503 if (!header->snapc)
504 return -ENOMEM;
Alex Elder00f1f362012-02-07 12:03:36 -0600505
Alex Elder00f1f362012-02-07 12:03:36 -0600506 header->snap_names_len = le64_to_cpu(ondisk->snap_names_len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700507 if (snap_count) {
508 header->snap_names = kmalloc(header->snap_names_len,
Dan Carpenterf8ad4952012-04-20 15:49:44 -0500509 gfp_flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700510 if (!header->snap_names)
511 goto err_snapc;
512 header->snap_sizes = kmalloc(snap_count * sizeof(u64),
Dan Carpenterf8ad4952012-04-20 15:49:44 -0500513 gfp_flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700514 if (!header->snap_sizes)
515 goto err_names;
516 } else {
517 header->snap_names = NULL;
518 header->snap_sizes = NULL;
519 }
520 memcpy(header->block_name, ondisk->block_name,
521 sizeof(ondisk->block_name));
522
523 header->image_size = le64_to_cpu(ondisk->image_size);
524 header->obj_order = ondisk->options.order;
525 header->crypt_type = ondisk->options.crypt_type;
526 header->comp_type = ondisk->options.comp_type;
527
528 atomic_set(&header->snapc->nref, 1);
529 header->snap_seq = le64_to_cpu(ondisk->snap_seq);
530 header->snapc->num_snaps = snap_count;
531 header->total_snaps = snap_count;
532
Alex Elder21079782012-01-24 10:08:36 -0600533 if (snap_count && allocated_snaps == snap_count) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700534 for (i = 0; i < snap_count; i++) {
535 header->snapc->snaps[i] =
536 le64_to_cpu(ondisk->snaps[i].id);
537 header->snap_sizes[i] =
538 le64_to_cpu(ondisk->snaps[i].image_size);
539 }
540
541 /* copy snapshot names */
542 memcpy(header->snap_names, &ondisk->snaps[i],
543 header->snap_names_len);
544 }
545
546 return 0;
547
548err_names:
549 kfree(header->snap_names);
550err_snapc:
551 kfree(header->snapc);
Alex Elder00f1f362012-02-07 12:03:36 -0600552 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700553}
554
555static int snap_index(struct rbd_image_header *header, int snap_num)
556{
557 return header->total_snaps - snap_num;
558}
559
560static u64 cur_snap_id(struct rbd_device *rbd_dev)
561{
562 struct rbd_image_header *header = &rbd_dev->header;
563
564 if (!rbd_dev->cur_snap)
565 return 0;
566
567 return header->snapc->snaps[snap_index(header, rbd_dev->cur_snap)];
568}
569
570static int snap_by_name(struct rbd_image_header *header, const char *snap_name,
571 u64 *seq, u64 *size)
572{
573 int i;
574 char *p = header->snap_names;
575
Alex Elder00f1f362012-02-07 12:03:36 -0600576 for (i = 0; i < header->total_snaps; i++) {
577 if (!strcmp(snap_name, p)) {
578
579 /* Found it. Pass back its id and/or size */
580
581 if (seq)
582 *seq = header->snapc->snaps[i];
583 if (size)
584 *size = header->snap_sizes[i];
585 return i;
586 }
587 p += strlen(p) + 1; /* Skip ahead to the next name */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700588 }
Alex Elder00f1f362012-02-07 12:03:36 -0600589 return -ENOENT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700590}
591
Josh Durgincc9d7342011-11-21 18:19:13 -0800592static int rbd_header_set_snap(struct rbd_device *dev, u64 *size)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700593{
594 struct rbd_image_header *header = &dev->header;
595 struct ceph_snap_context *snapc = header->snapc;
596 int ret = -ENOENT;
597
Josh Durgincc9d7342011-11-21 18:19:13 -0800598 BUILD_BUG_ON(sizeof (dev->snap_name) < sizeof (RBD_SNAP_HEAD_NAME));
599
Josh Durginc6666012011-11-21 17:11:12 -0800600 down_write(&dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700601
Josh Durgincc9d7342011-11-21 18:19:13 -0800602 if (!memcmp(dev->snap_name, RBD_SNAP_HEAD_NAME,
603 sizeof (RBD_SNAP_HEAD_NAME))) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700604 if (header->total_snaps)
605 snapc->seq = header->snap_seq;
606 else
607 snapc->seq = 0;
608 dev->cur_snap = 0;
609 dev->read_only = 0;
610 if (size)
611 *size = header->image_size;
612 } else {
Josh Durgincc9d7342011-11-21 18:19:13 -0800613 ret = snap_by_name(header, dev->snap_name, &snapc->seq, size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700614 if (ret < 0)
615 goto done;
616
617 dev->cur_snap = header->total_snaps - ret;
618 dev->read_only = 1;
619 }
620
621 ret = 0;
622done:
Josh Durginc6666012011-11-21 17:11:12 -0800623 up_write(&dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700624 return ret;
625}
626
627static void rbd_header_free(struct rbd_image_header *header)
628{
629 kfree(header->snapc);
630 kfree(header->snap_names);
631 kfree(header->snap_sizes);
632}
633
634/*
635 * get the actual striped segment name, offset and length
636 */
637static u64 rbd_get_segment(struct rbd_image_header *header,
638 const char *block_name,
639 u64 ofs, u64 len,
640 char *seg_name, u64 *segofs)
641{
642 u64 seg = ofs >> header->obj_order;
643
644 if (seg_name)
645 snprintf(seg_name, RBD_MAX_SEG_NAME_LEN,
646 "%s.%012llx", block_name, seg);
647
648 ofs = ofs & ((1 << header->obj_order) - 1);
649 len = min_t(u64, len, (1 << header->obj_order) - ofs);
650
651 if (segofs)
652 *segofs = ofs;
653
654 return len;
655}
656
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700657static int rbd_get_num_segments(struct rbd_image_header *header,
658 u64 ofs, u64 len)
659{
660 u64 start_seg = ofs >> header->obj_order;
661 u64 end_seg = (ofs + len - 1) >> header->obj_order;
662 return end_seg - start_seg + 1;
663}
664
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700665/*
Josh Durgin029bcbd2011-07-22 11:35:23 -0700666 * returns the size of an object in the image
667 */
668static u64 rbd_obj_bytes(struct rbd_image_header *header)
669{
670 return 1 << header->obj_order;
671}
672
673/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700674 * bio helpers
675 */
676
677static void bio_chain_put(struct bio *chain)
678{
679 struct bio *tmp;
680
681 while (chain) {
682 tmp = chain;
683 chain = chain->bi_next;
684 bio_put(tmp);
685 }
686}
687
688/*
689 * zeros a bio chain, starting at specific offset
690 */
691static void zero_bio_chain(struct bio *chain, int start_ofs)
692{
693 struct bio_vec *bv;
694 unsigned long flags;
695 void *buf;
696 int i;
697 int pos = 0;
698
699 while (chain) {
700 bio_for_each_segment(bv, chain, i) {
701 if (pos + bv->bv_len > start_ofs) {
702 int remainder = max(start_ofs - pos, 0);
703 buf = bvec_kmap_irq(bv, &flags);
704 memset(buf + remainder, 0,
705 bv->bv_len - remainder);
Dan Carpenter85b5aaa2010-10-11 21:15:11 +0200706 bvec_kunmap_irq(buf, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700707 }
708 pos += bv->bv_len;
709 }
710
711 chain = chain->bi_next;
712 }
713}
714
715/*
716 * bio_chain_clone - clone a chain of bios up to a certain length.
717 * might return a bio_pair that will need to be released.
718 */
719static struct bio *bio_chain_clone(struct bio **old, struct bio **next,
720 struct bio_pair **bp,
721 int len, gfp_t gfpmask)
722{
723 struct bio *tmp, *old_chain = *old, *new_chain = NULL, *tail = NULL;
724 int total = 0;
725
726 if (*bp) {
727 bio_pair_release(*bp);
728 *bp = NULL;
729 }
730
731 while (old_chain && (total < len)) {
732 tmp = bio_kmalloc(gfpmask, old_chain->bi_max_vecs);
733 if (!tmp)
734 goto err_out;
735
736 if (total + old_chain->bi_size > len) {
737 struct bio_pair *bp;
738
739 /*
740 * this split can only happen with a single paged bio,
741 * split_bio will BUG_ON if this is not the case
742 */
743 dout("bio_chain_clone split! total=%d remaining=%d"
744 "bi_size=%d\n",
745 (int)total, (int)len-total,
746 (int)old_chain->bi_size);
747
748 /* split the bio. We'll release it either in the next
749 call, or it will have to be released outside */
Alex Elder593a9e72012-02-07 12:03:37 -0600750 bp = bio_split(old_chain, (len - total) / SECTOR_SIZE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700751 if (!bp)
752 goto err_out;
753
754 __bio_clone(tmp, &bp->bio1);
755
756 *next = &bp->bio2;
757 } else {
758 __bio_clone(tmp, old_chain);
759 *next = old_chain->bi_next;
760 }
761
762 tmp->bi_bdev = NULL;
763 gfpmask &= ~__GFP_WAIT;
764 tmp->bi_next = NULL;
765
766 if (!new_chain) {
767 new_chain = tail = tmp;
768 } else {
769 tail->bi_next = tmp;
770 tail = tmp;
771 }
772 old_chain = old_chain->bi_next;
773
774 total += tmp->bi_size;
775 }
776
777 BUG_ON(total < len);
778
779 if (tail)
780 tail->bi_next = NULL;
781
782 *old = old_chain;
783
784 return new_chain;
785
786err_out:
787 dout("bio_chain_clone with err\n");
788 bio_chain_put(new_chain);
789 return NULL;
790}
791
792/*
793 * helpers for osd request op vectors.
794 */
795static int rbd_create_rw_ops(struct ceph_osd_req_op **ops,
796 int num_ops,
797 int opcode,
798 u32 payload_len)
799{
800 *ops = kzalloc(sizeof(struct ceph_osd_req_op) * (num_ops + 1),
801 GFP_NOIO);
802 if (!*ops)
803 return -ENOMEM;
804 (*ops)[0].op = opcode;
805 /*
806 * op extent offset and length will be set later on
807 * in calc_raw_layout()
808 */
809 (*ops)[0].payload_len = payload_len;
810 return 0;
811}
812
813static void rbd_destroy_ops(struct ceph_osd_req_op *ops)
814{
815 kfree(ops);
816}
817
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700818static void rbd_coll_end_req_index(struct request *rq,
819 struct rbd_req_coll *coll,
820 int index,
821 int ret, u64 len)
822{
823 struct request_queue *q;
824 int min, max, i;
825
826 dout("rbd_coll_end_req_index %p index %d ret %d len %lld\n",
827 coll, index, ret, len);
828
829 if (!rq)
830 return;
831
832 if (!coll) {
833 blk_end_request(rq, ret, len);
834 return;
835 }
836
837 q = rq->q;
838
839 spin_lock_irq(q->queue_lock);
840 coll->status[index].done = 1;
841 coll->status[index].rc = ret;
842 coll->status[index].bytes = len;
843 max = min = coll->num_done;
844 while (max < coll->total && coll->status[max].done)
845 max++;
846
847 for (i = min; i<max; i++) {
848 __blk_end_request(rq, coll->status[i].rc,
849 coll->status[i].bytes);
850 coll->num_done++;
851 kref_put(&coll->kref, rbd_coll_release);
852 }
853 spin_unlock_irq(q->queue_lock);
854}
855
856static void rbd_coll_end_req(struct rbd_request *req,
857 int ret, u64 len)
858{
859 rbd_coll_end_req_index(req->rq, req->coll, req->coll_index, ret, len);
860}
861
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700862/*
863 * Send ceph osd request
864 */
865static int rbd_do_request(struct request *rq,
866 struct rbd_device *dev,
867 struct ceph_snap_context *snapc,
868 u64 snapid,
869 const char *obj, u64 ofs, u64 len,
870 struct bio *bio,
871 struct page **pages,
872 int num_pages,
873 int flags,
874 struct ceph_osd_req_op *ops,
875 int num_reply,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700876 struct rbd_req_coll *coll,
877 int coll_index,
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700878 void (*rbd_cb)(struct ceph_osd_request *req,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700879 struct ceph_msg *msg),
880 struct ceph_osd_request **linger_req,
881 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700882{
883 struct ceph_osd_request *req;
884 struct ceph_file_layout *layout;
885 int ret;
886 u64 bno;
887 struct timespec mtime = CURRENT_TIME;
888 struct rbd_request *req_data;
889 struct ceph_osd_request_head *reqhead;
Alex Elder1dbb4392012-01-24 10:08:37 -0600890 struct ceph_osd_client *osdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700891
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700892 req_data = kzalloc(sizeof(*req_data), GFP_NOIO);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700893 if (!req_data) {
894 if (coll)
895 rbd_coll_end_req_index(rq, coll, coll_index,
896 -ENOMEM, len);
897 return -ENOMEM;
898 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700899
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700900 if (coll) {
901 req_data->coll = coll;
902 req_data->coll_index = coll_index;
903 }
904
905 dout("rbd_do_request obj=%s ofs=%lld len=%lld\n", obj, len, ofs);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700906
Josh Durginc6666012011-11-21 17:11:12 -0800907 down_read(&dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700908
Alex Elder1dbb4392012-01-24 10:08:37 -0600909 osdc = &dev->rbd_client->client->osdc;
910 req = ceph_osdc_alloc_request(osdc, flags, snapc, ops,
911 false, GFP_NOIO, pages, bio);
Sage Weil4ad12622011-05-03 09:23:36 -0700912 if (!req) {
Josh Durginc6666012011-11-21 17:11:12 -0800913 up_read(&dev->header_rwsem);
Sage Weil4ad12622011-05-03 09:23:36 -0700914 ret = -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700915 goto done_pages;
916 }
917
918 req->r_callback = rbd_cb;
919
920 req_data->rq = rq;
921 req_data->bio = bio;
922 req_data->pages = pages;
923 req_data->len = len;
924
925 req->r_priv = req_data;
926
927 reqhead = req->r_request->front.iov_base;
928 reqhead->snapid = cpu_to_le64(CEPH_NOSNAP);
929
930 strncpy(req->r_oid, obj, sizeof(req->r_oid));
931 req->r_oid_len = strlen(req->r_oid);
932
933 layout = &req->r_file_layout;
934 memset(layout, 0, sizeof(*layout));
935 layout->fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
936 layout->fl_stripe_count = cpu_to_le32(1);
937 layout->fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700938 layout->fl_pg_pool = cpu_to_le32(dev->poolid);
Alex Elder1dbb4392012-01-24 10:08:37 -0600939 ceph_calc_raw_layout(osdc, layout, snapid, ofs, &len, &bno,
940 req, ops);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700941
942 ceph_osdc_build_request(req, ofs, &len,
943 ops,
944 snapc,
945 &mtime,
946 req->r_oid, req->r_oid_len);
Josh Durginc6666012011-11-21 17:11:12 -0800947 up_read(&dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700948
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700949 if (linger_req) {
Alex Elder1dbb4392012-01-24 10:08:37 -0600950 ceph_osdc_set_request_linger(osdc, req);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700951 *linger_req = req;
952 }
953
Alex Elder1dbb4392012-01-24 10:08:37 -0600954 ret = ceph_osdc_start_request(osdc, req, false);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700955 if (ret < 0)
956 goto done_err;
957
958 if (!rbd_cb) {
Alex Elder1dbb4392012-01-24 10:08:37 -0600959 ret = ceph_osdc_wait_request(osdc, req);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700960 if (ver)
961 *ver = le64_to_cpu(req->r_reassert_version.version);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700962 dout("reassert_ver=%lld\n",
963 le64_to_cpu(req->r_reassert_version.version));
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700964 ceph_osdc_put_request(req);
965 }
966 return ret;
967
968done_err:
969 bio_chain_put(req_data->bio);
970 ceph_osdc_put_request(req);
971done_pages:
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700972 rbd_coll_end_req(req_data, ret, len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700973 kfree(req_data);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700974 return ret;
975}
976
977/*
978 * Ceph osd op callback
979 */
980static void rbd_req_cb(struct ceph_osd_request *req, struct ceph_msg *msg)
981{
982 struct rbd_request *req_data = req->r_priv;
983 struct ceph_osd_reply_head *replyhead;
984 struct ceph_osd_op *op;
985 __s32 rc;
986 u64 bytes;
987 int read_op;
988
989 /* parse reply */
990 replyhead = msg->front.iov_base;
991 WARN_ON(le32_to_cpu(replyhead->num_ops) == 0);
992 op = (void *)(replyhead + 1);
993 rc = le32_to_cpu(replyhead->result);
994 bytes = le64_to_cpu(op->extent.length);
995 read_op = (le32_to_cpu(op->op) == CEPH_OSD_OP_READ);
996
997 dout("rbd_req_cb bytes=%lld readop=%d rc=%d\n", bytes, read_op, rc);
998
999 if (rc == -ENOENT && read_op) {
1000 zero_bio_chain(req_data->bio, 0);
1001 rc = 0;
1002 } else if (rc == 0 && read_op && bytes < req_data->len) {
1003 zero_bio_chain(req_data->bio, bytes);
1004 bytes = req_data->len;
1005 }
1006
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001007 rbd_coll_end_req(req_data, rc, bytes);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001008
1009 if (req_data->bio)
1010 bio_chain_put(req_data->bio);
1011
1012 ceph_osdc_put_request(req);
1013 kfree(req_data);
1014}
1015
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001016static void rbd_simple_req_cb(struct ceph_osd_request *req, struct ceph_msg *msg)
1017{
1018 ceph_osdc_put_request(req);
1019}
1020
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001021/*
1022 * Do a synchronous ceph osd operation
1023 */
1024static int rbd_req_sync_op(struct rbd_device *dev,
1025 struct ceph_snap_context *snapc,
1026 u64 snapid,
1027 int opcode,
1028 int flags,
1029 struct ceph_osd_req_op *orig_ops,
1030 int num_reply,
1031 const char *obj,
1032 u64 ofs, u64 len,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001033 char *buf,
1034 struct ceph_osd_request **linger_req,
1035 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001036{
1037 int ret;
1038 struct page **pages;
1039 int num_pages;
1040 struct ceph_osd_req_op *ops = orig_ops;
1041 u32 payload_len;
1042
1043 num_pages = calc_pages_for(ofs , len);
1044 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
Dan Carpenterb8d06382010-10-11 21:14:23 +02001045 if (IS_ERR(pages))
1046 return PTR_ERR(pages);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001047
1048 if (!orig_ops) {
1049 payload_len = (flags & CEPH_OSD_FLAG_WRITE ? len : 0);
1050 ret = rbd_create_rw_ops(&ops, 1, opcode, payload_len);
1051 if (ret < 0)
1052 goto done;
1053
1054 if ((flags & CEPH_OSD_FLAG_WRITE) && buf) {
1055 ret = ceph_copy_to_page_vector(pages, buf, ofs, len);
1056 if (ret < 0)
1057 goto done_ops;
1058 }
1059 }
1060
1061 ret = rbd_do_request(NULL, dev, snapc, snapid,
1062 obj, ofs, len, NULL,
1063 pages, num_pages,
1064 flags,
1065 ops,
1066 2,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001067 NULL, 0,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001068 NULL,
1069 linger_req, ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001070 if (ret < 0)
1071 goto done_ops;
1072
1073 if ((flags & CEPH_OSD_FLAG_READ) && buf)
1074 ret = ceph_copy_from_page_vector(pages, buf, ofs, ret);
1075
1076done_ops:
1077 if (!orig_ops)
1078 rbd_destroy_ops(ops);
1079done:
1080 ceph_release_page_vector(pages, num_pages);
1081 return ret;
1082}
1083
1084/*
1085 * Do an asynchronous ceph osd operation
1086 */
1087static int rbd_do_op(struct request *rq,
1088 struct rbd_device *rbd_dev ,
1089 struct ceph_snap_context *snapc,
1090 u64 snapid,
1091 int opcode, int flags, int num_reply,
1092 u64 ofs, u64 len,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001093 struct bio *bio,
1094 struct rbd_req_coll *coll,
1095 int coll_index)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001096{
1097 char *seg_name;
1098 u64 seg_ofs;
1099 u64 seg_len;
1100 int ret;
1101 struct ceph_osd_req_op *ops;
1102 u32 payload_len;
1103
1104 seg_name = kmalloc(RBD_MAX_SEG_NAME_LEN + 1, GFP_NOIO);
1105 if (!seg_name)
1106 return -ENOMEM;
1107
1108 seg_len = rbd_get_segment(&rbd_dev->header,
1109 rbd_dev->header.block_name,
1110 ofs, len,
1111 seg_name, &seg_ofs);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001112
1113 payload_len = (flags & CEPH_OSD_FLAG_WRITE ? seg_len : 0);
1114
1115 ret = rbd_create_rw_ops(&ops, 1, opcode, payload_len);
1116 if (ret < 0)
1117 goto done;
1118
1119 /* we've taken care of segment sizes earlier when we
1120 cloned the bios. We should never have a segment
1121 truncated at this point */
1122 BUG_ON(seg_len < len);
1123
1124 ret = rbd_do_request(rq, rbd_dev, snapc, snapid,
1125 seg_name, seg_ofs, seg_len,
1126 bio,
1127 NULL, 0,
1128 flags,
1129 ops,
1130 num_reply,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001131 coll, coll_index,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001132 rbd_req_cb, 0, NULL);
Sage Weil11f77002011-05-12 16:13:54 -07001133
1134 rbd_destroy_ops(ops);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001135done:
1136 kfree(seg_name);
1137 return ret;
1138}
1139
1140/*
1141 * Request async osd write
1142 */
1143static int rbd_req_write(struct request *rq,
1144 struct rbd_device *rbd_dev,
1145 struct ceph_snap_context *snapc,
1146 u64 ofs, u64 len,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001147 struct bio *bio,
1148 struct rbd_req_coll *coll,
1149 int coll_index)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001150{
1151 return rbd_do_op(rq, rbd_dev, snapc, CEPH_NOSNAP,
1152 CEPH_OSD_OP_WRITE,
1153 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1154 2,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001155 ofs, len, bio, coll, coll_index);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001156}
1157
1158/*
1159 * Request async osd read
1160 */
1161static int rbd_req_read(struct request *rq,
1162 struct rbd_device *rbd_dev,
1163 u64 snapid,
1164 u64 ofs, u64 len,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001165 struct bio *bio,
1166 struct rbd_req_coll *coll,
1167 int coll_index)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001168{
1169 return rbd_do_op(rq, rbd_dev, NULL,
1170 (snapid ? snapid : CEPH_NOSNAP),
1171 CEPH_OSD_OP_READ,
1172 CEPH_OSD_FLAG_READ,
1173 2,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001174 ofs, len, bio, coll, coll_index);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001175}
1176
1177/*
1178 * Request sync osd read
1179 */
1180static int rbd_req_sync_read(struct rbd_device *dev,
1181 struct ceph_snap_context *snapc,
1182 u64 snapid,
1183 const char *obj,
1184 u64 ofs, u64 len,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001185 char *buf,
1186 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001187{
1188 return rbd_req_sync_op(dev, NULL,
1189 (snapid ? snapid : CEPH_NOSNAP),
1190 CEPH_OSD_OP_READ,
1191 CEPH_OSD_FLAG_READ,
1192 NULL,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001193 1, obj, ofs, len, buf, NULL, ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001194}
1195
1196/*
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001197 * Request sync osd watch
1198 */
1199static int rbd_req_sync_notify_ack(struct rbd_device *dev,
1200 u64 ver,
1201 u64 notify_id,
1202 const char *obj)
1203{
1204 struct ceph_osd_req_op *ops;
1205 struct page **pages = NULL;
Sage Weil11f77002011-05-12 16:13:54 -07001206 int ret;
1207
1208 ret = rbd_create_rw_ops(&ops, 1, CEPH_OSD_OP_NOTIFY_ACK, 0);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001209 if (ret < 0)
1210 return ret;
1211
1212 ops[0].watch.ver = cpu_to_le64(dev->header.obj_version);
1213 ops[0].watch.cookie = notify_id;
1214 ops[0].watch.flag = 0;
1215
1216 ret = rbd_do_request(NULL, dev, NULL, CEPH_NOSNAP,
1217 obj, 0, 0, NULL,
1218 pages, 0,
1219 CEPH_OSD_FLAG_READ,
1220 ops,
1221 1,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001222 NULL, 0,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001223 rbd_simple_req_cb, 0, NULL);
1224
1225 rbd_destroy_ops(ops);
1226 return ret;
1227}
1228
1229static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
1230{
1231 struct rbd_device *dev = (struct rbd_device *)data;
Sage Weil13143d22011-05-12 16:08:30 -07001232 int rc;
1233
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001234 if (!dev)
1235 return;
1236
1237 dout("rbd_watch_cb %s notify_id=%lld opcode=%d\n", dev->obj_md_name,
1238 notify_id, (int)opcode);
1239 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Sage Weil13143d22011-05-12 16:08:30 -07001240 rc = __rbd_update_snaps(dev);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001241 mutex_unlock(&ctl_mutex);
Sage Weil13143d22011-05-12 16:08:30 -07001242 if (rc)
Alex Elderf0f8cef2012-01-29 13:57:44 -06001243 pr_warning(RBD_DRV_NAME "%d got notification but failed to "
1244 " update snaps: %d\n", dev->major, rc);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001245
1246 rbd_req_sync_notify_ack(dev, ver, notify_id, dev->obj_md_name);
1247}
1248
1249/*
1250 * Request sync osd watch
1251 */
1252static int rbd_req_sync_watch(struct rbd_device *dev,
1253 const char *obj,
1254 u64 ver)
1255{
1256 struct ceph_osd_req_op *ops;
Alex Elder1dbb4392012-01-24 10:08:37 -06001257 struct ceph_osd_client *osdc = &dev->rbd_client->client->osdc;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001258
1259 int ret = rbd_create_rw_ops(&ops, 1, CEPH_OSD_OP_WATCH, 0);
1260 if (ret < 0)
1261 return ret;
1262
1263 ret = ceph_osdc_create_event(osdc, rbd_watch_cb, 0,
1264 (void *)dev, &dev->watch_event);
1265 if (ret < 0)
1266 goto fail;
1267
1268 ops[0].watch.ver = cpu_to_le64(ver);
1269 ops[0].watch.cookie = cpu_to_le64(dev->watch_event->cookie);
1270 ops[0].watch.flag = 1;
1271
1272 ret = rbd_req_sync_op(dev, NULL,
1273 CEPH_NOSNAP,
1274 0,
1275 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1276 ops,
1277 1, obj, 0, 0, NULL,
1278 &dev->watch_request, NULL);
1279
1280 if (ret < 0)
1281 goto fail_event;
1282
1283 rbd_destroy_ops(ops);
1284 return 0;
1285
1286fail_event:
1287 ceph_osdc_cancel_event(dev->watch_event);
1288 dev->watch_event = NULL;
1289fail:
1290 rbd_destroy_ops(ops);
1291 return ret;
1292}
1293
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001294/*
1295 * Request sync osd unwatch
1296 */
1297static int rbd_req_sync_unwatch(struct rbd_device *dev,
1298 const char *obj)
1299{
1300 struct ceph_osd_req_op *ops;
1301
1302 int ret = rbd_create_rw_ops(&ops, 1, CEPH_OSD_OP_WATCH, 0);
1303 if (ret < 0)
1304 return ret;
1305
1306 ops[0].watch.ver = 0;
1307 ops[0].watch.cookie = cpu_to_le64(dev->watch_event->cookie);
1308 ops[0].watch.flag = 0;
1309
1310 ret = rbd_req_sync_op(dev, NULL,
1311 CEPH_NOSNAP,
1312 0,
1313 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1314 ops,
1315 1, obj, 0, 0, NULL, NULL, NULL);
1316
1317 rbd_destroy_ops(ops);
1318 ceph_osdc_cancel_event(dev->watch_event);
1319 dev->watch_event = NULL;
1320 return ret;
1321}
1322
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001323struct rbd_notify_info {
1324 struct rbd_device *dev;
1325};
1326
1327static void rbd_notify_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
1328{
1329 struct rbd_device *dev = (struct rbd_device *)data;
1330 if (!dev)
1331 return;
1332
1333 dout("rbd_notify_cb %s notify_id=%lld opcode=%d\n", dev->obj_md_name,
1334 notify_id, (int)opcode);
1335}
1336
1337/*
1338 * Request sync osd notify
1339 */
1340static int rbd_req_sync_notify(struct rbd_device *dev,
1341 const char *obj)
1342{
1343 struct ceph_osd_req_op *ops;
Alex Elder1dbb4392012-01-24 10:08:37 -06001344 struct ceph_osd_client *osdc = &dev->rbd_client->client->osdc;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001345 struct ceph_osd_event *event;
1346 struct rbd_notify_info info;
1347 int payload_len = sizeof(u32) + sizeof(u32);
1348 int ret;
1349
1350 ret = rbd_create_rw_ops(&ops, 1, CEPH_OSD_OP_NOTIFY, payload_len);
1351 if (ret < 0)
1352 return ret;
1353
1354 info.dev = dev;
1355
1356 ret = ceph_osdc_create_event(osdc, rbd_notify_cb, 1,
1357 (void *)&info, &event);
1358 if (ret < 0)
1359 goto fail;
1360
1361 ops[0].watch.ver = 1;
1362 ops[0].watch.flag = 1;
1363 ops[0].watch.cookie = event->cookie;
1364 ops[0].watch.prot_ver = RADOS_NOTIFY_VER;
1365 ops[0].watch.timeout = 12;
1366
1367 ret = rbd_req_sync_op(dev, NULL,
1368 CEPH_NOSNAP,
1369 0,
1370 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1371 ops,
1372 1, obj, 0, 0, NULL, NULL, NULL);
1373 if (ret < 0)
1374 goto fail_event;
1375
1376 ret = ceph_osdc_wait_event(event, CEPH_OSD_TIMEOUT_DEFAULT);
1377 dout("ceph_osdc_wait_event returned %d\n", ret);
1378 rbd_destroy_ops(ops);
1379 return 0;
1380
1381fail_event:
1382 ceph_osdc_cancel_event(event);
1383fail:
1384 rbd_destroy_ops(ops);
1385 return ret;
1386}
1387
1388/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001389 * Request sync osd read
1390 */
1391static int rbd_req_sync_exec(struct rbd_device *dev,
1392 const char *obj,
1393 const char *cls,
1394 const char *method,
1395 const char *data,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001396 int len,
1397 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001398{
1399 struct ceph_osd_req_op *ops;
1400 int cls_len = strlen(cls);
1401 int method_len = strlen(method);
1402 int ret = rbd_create_rw_ops(&ops, 1, CEPH_OSD_OP_CALL,
1403 cls_len + method_len + len);
1404 if (ret < 0)
1405 return ret;
1406
1407 ops[0].cls.class_name = cls;
1408 ops[0].cls.class_len = (__u8)cls_len;
1409 ops[0].cls.method_name = method;
1410 ops[0].cls.method_len = (__u8)method_len;
1411 ops[0].cls.argc = 0;
1412 ops[0].cls.indata = data;
1413 ops[0].cls.indata_len = len;
1414
1415 ret = rbd_req_sync_op(dev, NULL,
1416 CEPH_NOSNAP,
1417 0,
1418 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1419 ops,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001420 1, obj, 0, 0, NULL, NULL, ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001421
1422 rbd_destroy_ops(ops);
1423
1424 dout("cls_exec returned %d\n", ret);
1425 return ret;
1426}
1427
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001428static struct rbd_req_coll *rbd_alloc_coll(int num_reqs)
1429{
1430 struct rbd_req_coll *coll =
1431 kzalloc(sizeof(struct rbd_req_coll) +
1432 sizeof(struct rbd_req_status) * num_reqs,
1433 GFP_ATOMIC);
1434
1435 if (!coll)
1436 return NULL;
1437 coll->total = num_reqs;
1438 kref_init(&coll->kref);
1439 return coll;
1440}
1441
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001442/*
1443 * block device queue callback
1444 */
1445static void rbd_rq_fn(struct request_queue *q)
1446{
1447 struct rbd_device *rbd_dev = q->queuedata;
1448 struct request *rq;
1449 struct bio_pair *bp = NULL;
1450
Alex Elder00f1f362012-02-07 12:03:36 -06001451 while ((rq = blk_fetch_request(q))) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001452 struct bio *bio;
1453 struct bio *rq_bio, *next_bio = NULL;
1454 bool do_write;
1455 int size, op_size = 0;
1456 u64 ofs;
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001457 int num_segs, cur_seg = 0;
1458 struct rbd_req_coll *coll;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001459
1460 /* peek at request from block layer */
1461 if (!rq)
1462 break;
1463
1464 dout("fetched request\n");
1465
1466 /* filter out block requests we don't understand */
1467 if ((rq->cmd_type != REQ_TYPE_FS)) {
1468 __blk_end_request_all(rq, 0);
Alex Elder00f1f362012-02-07 12:03:36 -06001469 continue;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001470 }
1471
1472 /* deduce our operation (read, write) */
1473 do_write = (rq_data_dir(rq) == WRITE);
1474
1475 size = blk_rq_bytes(rq);
Alex Elder593a9e72012-02-07 12:03:37 -06001476 ofs = blk_rq_pos(rq) * SECTOR_SIZE;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001477 rq_bio = rq->bio;
1478 if (do_write && rbd_dev->read_only) {
1479 __blk_end_request_all(rq, -EROFS);
Alex Elder00f1f362012-02-07 12:03:36 -06001480 continue;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001481 }
1482
1483 spin_unlock_irq(q->queue_lock);
1484
1485 dout("%s 0x%x bytes at 0x%llx\n",
1486 do_write ? "write" : "read",
Alex Elder593a9e72012-02-07 12:03:37 -06001487 size, blk_rq_pos(rq) * SECTOR_SIZE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001488
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001489 num_segs = rbd_get_num_segments(&rbd_dev->header, ofs, size);
1490 coll = rbd_alloc_coll(num_segs);
1491 if (!coll) {
1492 spin_lock_irq(q->queue_lock);
1493 __blk_end_request_all(rq, -ENOMEM);
Alex Elder00f1f362012-02-07 12:03:36 -06001494 continue;
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001495 }
1496
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001497 do {
1498 /* a bio clone to be passed down to OSD req */
1499 dout("rq->bio->bi_vcnt=%d\n", rq->bio->bi_vcnt);
1500 op_size = rbd_get_segment(&rbd_dev->header,
1501 rbd_dev->header.block_name,
1502 ofs, size,
1503 NULL, NULL);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001504 kref_get(&coll->kref);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001505 bio = bio_chain_clone(&rq_bio, &next_bio, &bp,
1506 op_size, GFP_ATOMIC);
1507 if (!bio) {
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001508 rbd_coll_end_req_index(rq, coll, cur_seg,
1509 -ENOMEM, op_size);
1510 goto next_seg;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001511 }
1512
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001513
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001514 /* init OSD command: write or read */
1515 if (do_write)
1516 rbd_req_write(rq, rbd_dev,
1517 rbd_dev->header.snapc,
1518 ofs,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001519 op_size, bio,
1520 coll, cur_seg);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001521 else
1522 rbd_req_read(rq, rbd_dev,
1523 cur_snap_id(rbd_dev),
1524 ofs,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001525 op_size, bio,
1526 coll, cur_seg);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001527
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001528next_seg:
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001529 size -= op_size;
1530 ofs += op_size;
1531
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001532 cur_seg++;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001533 rq_bio = next_bio;
1534 } while (size > 0);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001535 kref_put(&coll->kref, rbd_coll_release);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001536
1537 if (bp)
1538 bio_pair_release(bp);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001539 spin_lock_irq(q->queue_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001540 }
1541}
1542
1543/*
1544 * a queue callback. Makes sure that we don't create a bio that spans across
1545 * multiple osd objects. One exception would be with a single page bios,
1546 * which we handle later at bio_chain_clone
1547 */
1548static int rbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd,
1549 struct bio_vec *bvec)
1550{
1551 struct rbd_device *rbd_dev = q->queuedata;
Alex Elder593a9e72012-02-07 12:03:37 -06001552 unsigned int chunk_sectors;
1553 sector_t sector;
1554 unsigned int bio_sectors;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001555 int max;
1556
Alex Elder593a9e72012-02-07 12:03:37 -06001557 chunk_sectors = 1 << (rbd_dev->header.obj_order - SECTOR_SHIFT);
1558 sector = bmd->bi_sector + get_start_sect(bmd->bi_bdev);
1559 bio_sectors = bmd->bi_size >> SECTOR_SHIFT;
1560
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001561 max = (chunk_sectors - ((sector & (chunk_sectors - 1))
Alex Elder593a9e72012-02-07 12:03:37 -06001562 + bio_sectors)) << SECTOR_SHIFT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001563 if (max < 0)
1564 max = 0; /* bio_add cannot handle a negative return */
1565 if (max <= bvec->bv_len && bio_sectors == 0)
1566 return bvec->bv_len;
1567 return max;
1568}
1569
1570static void rbd_free_disk(struct rbd_device *rbd_dev)
1571{
1572 struct gendisk *disk = rbd_dev->disk;
1573
1574 if (!disk)
1575 return;
1576
1577 rbd_header_free(&rbd_dev->header);
1578
1579 if (disk->flags & GENHD_FL_UP)
1580 del_gendisk(disk);
1581 if (disk->queue)
1582 blk_cleanup_queue(disk->queue);
1583 put_disk(disk);
1584}
1585
1586/*
1587 * reload the ondisk the header
1588 */
1589static int rbd_read_header(struct rbd_device *rbd_dev,
1590 struct rbd_image_header *header)
1591{
1592 ssize_t rc;
1593 struct rbd_image_header_ondisk *dh;
1594 int snap_count = 0;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001595 u64 ver;
Alex Elder00f1f362012-02-07 12:03:36 -06001596 size_t len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001597
Alex Elder00f1f362012-02-07 12:03:36 -06001598 /*
1599 * First reads the fixed-size header to determine the number
1600 * of snapshots, then re-reads it, along with all snapshot
1601 * records as well as their stored names.
1602 */
1603 len = sizeof (*dh);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001604 while (1) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001605 dh = kmalloc(len, GFP_KERNEL);
1606 if (!dh)
1607 return -ENOMEM;
1608
1609 rc = rbd_req_sync_read(rbd_dev,
1610 NULL, CEPH_NOSNAP,
1611 rbd_dev->obj_md_name,
1612 0, len,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001613 (char *)dh, &ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001614 if (rc < 0)
1615 goto out_dh;
1616
1617 rc = rbd_header_from_disk(header, dh, snap_count, GFP_KERNEL);
Josh Durgin81e759f2011-11-15 14:49:53 -08001618 if (rc < 0) {
Alex Elder00f1f362012-02-07 12:03:36 -06001619 if (rc == -ENXIO)
Josh Durgin81e759f2011-11-15 14:49:53 -08001620 pr_warning("unrecognized header format"
1621 " for image %s", rbd_dev->obj);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001622 goto out_dh;
Josh Durgin81e759f2011-11-15 14:49:53 -08001623 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001624
Alex Elder00f1f362012-02-07 12:03:36 -06001625 if (snap_count == header->total_snaps)
1626 break;
1627
1628 snap_count = header->total_snaps;
1629 len = sizeof (*dh) +
1630 snap_count * sizeof(struct rbd_image_snap_ondisk) +
1631 header->snap_names_len;
1632
1633 rbd_header_free(header);
1634 kfree(dh);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001635 }
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001636 header->obj_version = ver;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001637
1638out_dh:
1639 kfree(dh);
1640 return rc;
1641}
1642
1643/*
1644 * create a snapshot
1645 */
1646static int rbd_header_add_snap(struct rbd_device *dev,
1647 const char *snap_name,
1648 gfp_t gfp_flags)
1649{
1650 int name_len = strlen(snap_name);
1651 u64 new_snapid;
1652 int ret;
Sage Weil916d4d62011-05-12 16:10:50 -07001653 void *data, *p, *e;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001654 u64 ver;
Alex Elder1dbb4392012-01-24 10:08:37 -06001655 struct ceph_mon_client *monc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001656
1657 /* we should create a snapshot only if we're pointing at the head */
1658 if (dev->cur_snap)
1659 return -EINVAL;
1660
Alex Elder1dbb4392012-01-24 10:08:37 -06001661 monc = &dev->rbd_client->client->monc;
1662 ret = ceph_monc_create_snapid(monc, dev->poolid, &new_snapid);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001663 dout("created snapid=%lld\n", new_snapid);
1664 if (ret < 0)
1665 return ret;
1666
1667 data = kmalloc(name_len + 16, gfp_flags);
1668 if (!data)
1669 return -ENOMEM;
1670
Sage Weil916d4d62011-05-12 16:10:50 -07001671 p = data;
1672 e = data + name_len + 16;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001673
Sage Weil916d4d62011-05-12 16:10:50 -07001674 ceph_encode_string_safe(&p, e, snap_name, name_len, bad);
1675 ceph_encode_64_safe(&p, e, new_snapid, bad);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001676
1677 ret = rbd_req_sync_exec(dev, dev->obj_md_name, "rbd", "snap_add",
Sage Weil916d4d62011-05-12 16:10:50 -07001678 data, p - data, &ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001679
Sage Weil916d4d62011-05-12 16:10:50 -07001680 kfree(data);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001681
1682 if (ret < 0)
1683 return ret;
1684
1685 dev->header.snapc->seq = new_snapid;
1686
1687 return 0;
1688bad:
1689 return -ERANGE;
1690}
1691
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001692static void __rbd_remove_all_snaps(struct rbd_device *rbd_dev)
1693{
1694 struct rbd_snap *snap;
1695
1696 while (!list_empty(&rbd_dev->snaps)) {
1697 snap = list_first_entry(&rbd_dev->snaps, struct rbd_snap, node);
1698 __rbd_remove_snap_dev(rbd_dev, snap);
1699 }
1700}
1701
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001702/*
1703 * only read the first part of the ondisk header, without the snaps info
1704 */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001705static int __rbd_update_snaps(struct rbd_device *rbd_dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001706{
1707 int ret;
1708 struct rbd_image_header h;
1709 u64 snap_seq;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001710 int follow_seq = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001711
1712 ret = rbd_read_header(rbd_dev, &h);
1713 if (ret < 0)
1714 return ret;
1715
Sage Weil9db4b3e2011-04-19 22:49:06 -07001716 /* resized? */
Alex Elder593a9e72012-02-07 12:03:37 -06001717 set_capacity(rbd_dev->disk, h.image_size / SECTOR_SIZE);
Sage Weil9db4b3e2011-04-19 22:49:06 -07001718
Josh Durginc6666012011-11-21 17:11:12 -08001719 down_write(&rbd_dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001720
1721 snap_seq = rbd_dev->header.snapc->seq;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001722 if (rbd_dev->header.total_snaps &&
1723 rbd_dev->header.snapc->snaps[0] == snap_seq)
1724 /* pointing at the head, will need to follow that
1725 if head moves */
1726 follow_seq = 1;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001727
1728 kfree(rbd_dev->header.snapc);
1729 kfree(rbd_dev->header.snap_names);
1730 kfree(rbd_dev->header.snap_sizes);
1731
1732 rbd_dev->header.total_snaps = h.total_snaps;
1733 rbd_dev->header.snapc = h.snapc;
1734 rbd_dev->header.snap_names = h.snap_names;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001735 rbd_dev->header.snap_names_len = h.snap_names_len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001736 rbd_dev->header.snap_sizes = h.snap_sizes;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001737 if (follow_seq)
1738 rbd_dev->header.snapc->seq = rbd_dev->header.snapc->snaps[0];
1739 else
1740 rbd_dev->header.snapc->seq = snap_seq;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001741
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001742 ret = __rbd_init_snaps_header(rbd_dev);
1743
Josh Durginc6666012011-11-21 17:11:12 -08001744 up_write(&rbd_dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001745
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001746 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001747}
1748
1749static int rbd_init_disk(struct rbd_device *rbd_dev)
1750{
1751 struct gendisk *disk;
1752 struct request_queue *q;
1753 int rc;
Alex Elder593a9e72012-02-07 12:03:37 -06001754 u64 segment_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001755 u64 total_size = 0;
1756
1757 /* contact OSD, request size info about the object being mapped */
1758 rc = rbd_read_header(rbd_dev, &rbd_dev->header);
1759 if (rc)
1760 return rc;
1761
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001762 /* no need to lock here, as rbd_dev is not registered yet */
1763 rc = __rbd_init_snaps_header(rbd_dev);
1764 if (rc)
1765 return rc;
1766
Josh Durgincc9d7342011-11-21 18:19:13 -08001767 rc = rbd_header_set_snap(rbd_dev, &total_size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001768 if (rc)
1769 return rc;
1770
1771 /* create gendisk info */
1772 rc = -ENOMEM;
1773 disk = alloc_disk(RBD_MINORS_PER_MAJOR);
1774 if (!disk)
1775 goto out;
1776
Alex Elderf0f8cef2012-01-29 13:57:44 -06001777 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
Sage Weilaedfec52011-05-12 20:57:03 -07001778 rbd_dev->id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001779 disk->major = rbd_dev->major;
1780 disk->first_minor = 0;
1781 disk->fops = &rbd_bd_ops;
1782 disk->private_data = rbd_dev;
1783
1784 /* init rq */
1785 rc = -ENOMEM;
1786 q = blk_init_queue(rbd_rq_fn, &rbd_dev->lock);
1787 if (!q)
1788 goto out_disk;
Josh Durgin029bcbd2011-07-22 11:35:23 -07001789
Alex Elder593a9e72012-02-07 12:03:37 -06001790 /* We use the default size, but let's be explicit about it. */
1791 blk_queue_physical_block_size(q, SECTOR_SIZE);
1792
Josh Durgin029bcbd2011-07-22 11:35:23 -07001793 /* set io sizes to object size */
Alex Elder593a9e72012-02-07 12:03:37 -06001794 segment_size = rbd_obj_bytes(&rbd_dev->header);
1795 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
1796 blk_queue_max_segment_size(q, segment_size);
1797 blk_queue_io_min(q, segment_size);
1798 blk_queue_io_opt(q, segment_size);
Josh Durgin029bcbd2011-07-22 11:35:23 -07001799
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001800 blk_queue_merge_bvec(q, rbd_merge_bvec);
1801 disk->queue = q;
1802
1803 q->queuedata = rbd_dev;
1804
1805 rbd_dev->disk = disk;
1806 rbd_dev->q = q;
1807
1808 /* finally, announce the disk to the world */
Alex Elder593a9e72012-02-07 12:03:37 -06001809 set_capacity(disk, total_size / SECTOR_SIZE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001810 add_disk(disk);
1811
1812 pr_info("%s: added with size 0x%llx\n",
1813 disk->disk_name, (unsigned long long)total_size);
1814 return 0;
1815
1816out_disk:
1817 put_disk(disk);
1818out:
1819 return rc;
1820}
1821
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001822/*
1823 sysfs
1824*/
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001825
Alex Elder593a9e72012-02-07 12:03:37 -06001826static struct rbd_device *dev_to_rbd_dev(struct device *dev)
1827{
1828 return container_of(dev, struct rbd_device, dev);
1829}
1830
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001831static ssize_t rbd_size_show(struct device *dev,
1832 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001833{
Alex Elder593a9e72012-02-07 12:03:37 -06001834 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001835
1836 return sprintf(buf, "%llu\n", (unsigned long long)rbd_dev->header.image_size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001837}
1838
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001839static ssize_t rbd_major_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);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001843
1844 return sprintf(buf, "%d\n", rbd_dev->major);
1845}
1846
1847static ssize_t rbd_client_id_show(struct device *dev,
1848 struct device_attribute *attr, char *buf)
1849{
Alex Elder593a9e72012-02-07 12:03:37 -06001850 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001851
Alex Elder1dbb4392012-01-24 10:08:37 -06001852 return sprintf(buf, "client%lld\n",
1853 ceph_client_id(rbd_dev->rbd_client->client));
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001854}
1855
1856static ssize_t rbd_pool_show(struct device *dev,
1857 struct device_attribute *attr, char *buf)
1858{
Alex Elder593a9e72012-02-07 12:03:37 -06001859 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001860
1861 return sprintf(buf, "%s\n", rbd_dev->pool_name);
1862}
1863
1864static ssize_t rbd_name_show(struct device *dev,
1865 struct device_attribute *attr, char *buf)
1866{
Alex Elder593a9e72012-02-07 12:03:37 -06001867 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001868
1869 return sprintf(buf, "%s\n", rbd_dev->obj);
1870}
1871
1872static ssize_t rbd_snap_show(struct device *dev,
1873 struct device_attribute *attr,
1874 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
1878 return sprintf(buf, "%s\n", rbd_dev->snap_name);
1879}
1880
1881static ssize_t rbd_image_refresh(struct device *dev,
1882 struct device_attribute *attr,
1883 const char *buf,
1884 size_t size)
1885{
Alex Elder593a9e72012-02-07 12:03:37 -06001886 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001887 int rc;
1888 int ret = size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001889
1890 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
1891
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001892 rc = __rbd_update_snaps(rbd_dev);
1893 if (rc < 0)
1894 ret = rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001895
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001896 mutex_unlock(&ctl_mutex);
1897 return ret;
1898}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001899
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001900static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
1901static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
1902static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
1903static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
1904static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
1905static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
1906static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
1907static DEVICE_ATTR(create_snap, S_IWUSR, NULL, rbd_snap_add);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001908
1909static struct attribute *rbd_attrs[] = {
1910 &dev_attr_size.attr,
1911 &dev_attr_major.attr,
1912 &dev_attr_client_id.attr,
1913 &dev_attr_pool.attr,
1914 &dev_attr_name.attr,
1915 &dev_attr_current_snap.attr,
1916 &dev_attr_refresh.attr,
1917 &dev_attr_create_snap.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001918 NULL
1919};
1920
1921static struct attribute_group rbd_attr_group = {
1922 .attrs = rbd_attrs,
1923};
1924
1925static const struct attribute_group *rbd_attr_groups[] = {
1926 &rbd_attr_group,
1927 NULL
1928};
1929
1930static void rbd_sysfs_dev_release(struct device *dev)
1931{
1932}
1933
1934static struct device_type rbd_device_type = {
1935 .name = "rbd",
1936 .groups = rbd_attr_groups,
1937 .release = rbd_sysfs_dev_release,
1938};
1939
1940
1941/*
1942 sysfs - snapshots
1943*/
1944
1945static ssize_t rbd_snap_size_show(struct device *dev,
1946 struct device_attribute *attr,
1947 char *buf)
1948{
1949 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
1950
Alex Elder593a9e72012-02-07 12:03:37 -06001951 return sprintf(buf, "%zd\n", snap->size);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001952}
1953
1954static ssize_t rbd_snap_id_show(struct device *dev,
1955 struct device_attribute *attr,
1956 char *buf)
1957{
1958 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
1959
Alex Elder593a9e72012-02-07 12:03:37 -06001960 return sprintf(buf, "%llu\n", (unsigned long long) snap->id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001961}
1962
1963static DEVICE_ATTR(snap_size, S_IRUGO, rbd_snap_size_show, NULL);
1964static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL);
1965
1966static struct attribute *rbd_snap_attrs[] = {
1967 &dev_attr_snap_size.attr,
1968 &dev_attr_snap_id.attr,
1969 NULL,
1970};
1971
1972static struct attribute_group rbd_snap_attr_group = {
1973 .attrs = rbd_snap_attrs,
1974};
1975
1976static void rbd_snap_dev_release(struct device *dev)
1977{
1978 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
1979 kfree(snap->name);
1980 kfree(snap);
1981}
1982
1983static const struct attribute_group *rbd_snap_attr_groups[] = {
1984 &rbd_snap_attr_group,
1985 NULL
1986};
1987
1988static struct device_type rbd_snap_device_type = {
1989 .groups = rbd_snap_attr_groups,
1990 .release = rbd_snap_dev_release,
1991};
1992
1993static void __rbd_remove_snap_dev(struct rbd_device *rbd_dev,
1994 struct rbd_snap *snap)
1995{
1996 list_del(&snap->node);
1997 device_unregister(&snap->dev);
1998}
1999
2000static int rbd_register_snap_dev(struct rbd_device *rbd_dev,
2001 struct rbd_snap *snap,
2002 struct device *parent)
2003{
2004 struct device *dev = &snap->dev;
2005 int ret;
2006
2007 dev->type = &rbd_snap_device_type;
2008 dev->parent = parent;
2009 dev->release = rbd_snap_dev_release;
2010 dev_set_name(dev, "snap_%s", snap->name);
2011 ret = device_register(dev);
2012
2013 return ret;
2014}
2015
2016static int __rbd_add_snap_dev(struct rbd_device *rbd_dev,
2017 int i, const char *name,
2018 struct rbd_snap **snapp)
2019{
2020 int ret;
2021 struct rbd_snap *snap = kzalloc(sizeof(*snap), GFP_KERNEL);
2022 if (!snap)
2023 return -ENOMEM;
2024 snap->name = kstrdup(name, GFP_KERNEL);
2025 snap->size = rbd_dev->header.snap_sizes[i];
2026 snap->id = rbd_dev->header.snapc->snaps[i];
2027 if (device_is_registered(&rbd_dev->dev)) {
2028 ret = rbd_register_snap_dev(rbd_dev, snap,
2029 &rbd_dev->dev);
2030 if (ret < 0)
2031 goto err;
2032 }
2033 *snapp = snap;
2034 return 0;
2035err:
2036 kfree(snap->name);
2037 kfree(snap);
2038 return ret;
2039}
2040
2041/*
2042 * search for the previous snap in a null delimited string list
2043 */
2044const char *rbd_prev_snap_name(const char *name, const char *start)
2045{
2046 if (name < start + 2)
2047 return NULL;
2048
2049 name -= 2;
2050 while (*name) {
2051 if (name == start)
2052 return start;
2053 name--;
2054 }
2055 return name + 1;
2056}
2057
2058/*
2059 * compare the old list of snapshots that we have to what's in the header
2060 * and update it accordingly. Note that the header holds the snapshots
2061 * in a reverse order (from newest to oldest) and we need to go from
2062 * older to new so that we don't get a duplicate snap name when
2063 * doing the process (e.g., removed snapshot and recreated a new
2064 * one with the same name.
2065 */
2066static int __rbd_init_snaps_header(struct rbd_device *rbd_dev)
2067{
2068 const char *name, *first_name;
2069 int i = rbd_dev->header.total_snaps;
2070 struct rbd_snap *snap, *old_snap = NULL;
2071 int ret;
2072 struct list_head *p, *n;
2073
2074 first_name = rbd_dev->header.snap_names;
2075 name = first_name + rbd_dev->header.snap_names_len;
2076
2077 list_for_each_prev_safe(p, n, &rbd_dev->snaps) {
2078 u64 cur_id;
2079
2080 old_snap = list_entry(p, struct rbd_snap, node);
2081
2082 if (i)
2083 cur_id = rbd_dev->header.snapc->snaps[i - 1];
2084
2085 if (!i || old_snap->id < cur_id) {
2086 /* old_snap->id was skipped, thus was removed */
2087 __rbd_remove_snap_dev(rbd_dev, old_snap);
2088 continue;
2089 }
2090 if (old_snap->id == cur_id) {
2091 /* we have this snapshot already */
2092 i--;
2093 name = rbd_prev_snap_name(name, first_name);
2094 continue;
2095 }
2096 for (; i > 0;
2097 i--, name = rbd_prev_snap_name(name, first_name)) {
2098 if (!name) {
2099 WARN_ON(1);
2100 return -EINVAL;
2101 }
2102 cur_id = rbd_dev->header.snapc->snaps[i];
2103 /* snapshot removal? handle it above */
2104 if (cur_id >= old_snap->id)
2105 break;
2106 /* a new snapshot */
2107 ret = __rbd_add_snap_dev(rbd_dev, i - 1, name, &snap);
2108 if (ret < 0)
2109 return ret;
2110
2111 /* note that we add it backward so using n and not p */
2112 list_add(&snap->node, n);
2113 p = &snap->node;
2114 }
2115 }
2116 /* we're done going over the old snap list, just add what's left */
2117 for (; i > 0; i--) {
2118 name = rbd_prev_snap_name(name, first_name);
2119 if (!name) {
2120 WARN_ON(1);
2121 return -EINVAL;
2122 }
2123 ret = __rbd_add_snap_dev(rbd_dev, i - 1, name, &snap);
2124 if (ret < 0)
2125 return ret;
2126 list_add(&snap->node, &rbd_dev->snaps);
2127 }
2128
2129 return 0;
2130}
2131
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002132static int rbd_bus_add_dev(struct rbd_device *rbd_dev)
2133{
Alex Elderf0f8cef2012-01-29 13:57:44 -06002134 int ret;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002135 struct device *dev;
2136 struct rbd_snap *snap;
2137
2138 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2139 dev = &rbd_dev->dev;
2140
2141 dev->bus = &rbd_bus_type;
2142 dev->type = &rbd_device_type;
2143 dev->parent = &rbd_root_dev;
2144 dev->release = rbd_dev_release;
2145 dev_set_name(dev, "%d", rbd_dev->id);
2146 ret = device_register(dev);
2147 if (ret < 0)
Alex Elderf0f8cef2012-01-29 13:57:44 -06002148 goto out;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002149
2150 list_for_each_entry(snap, &rbd_dev->snaps, node) {
2151 ret = rbd_register_snap_dev(rbd_dev, snap,
2152 &rbd_dev->dev);
2153 if (ret < 0)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002154 break;
2155 }
Alex Elderf0f8cef2012-01-29 13:57:44 -06002156out:
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002157 mutex_unlock(&ctl_mutex);
2158 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002159}
2160
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002161static void rbd_bus_del_dev(struct rbd_device *rbd_dev)
2162{
2163 device_unregister(&rbd_dev->dev);
2164}
2165
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002166static int rbd_init_watch_dev(struct rbd_device *rbd_dev)
2167{
2168 int ret, rc;
2169
2170 do {
2171 ret = rbd_req_sync_watch(rbd_dev, rbd_dev->obj_md_name,
2172 rbd_dev->header.obj_version);
2173 if (ret == -ERANGE) {
2174 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2175 rc = __rbd_update_snaps(rbd_dev);
2176 mutex_unlock(&ctl_mutex);
2177 if (rc < 0)
2178 return rc;
2179 }
2180 } while (ret == -ERANGE);
2181
2182 return ret;
2183}
2184
Alex Elder1ddbe942012-01-29 13:57:44 -06002185static atomic64_t rbd_id_max = ATOMIC64_INIT(0);
2186
2187/*
Alex Elder499afd52012-02-02 08:13:29 -06002188 * Get a unique rbd identifier for the given new rbd_dev, and add
2189 * the rbd_dev to the global list. The minimum rbd id is 1.
Alex Elder1ddbe942012-01-29 13:57:44 -06002190 */
Alex Elder499afd52012-02-02 08:13:29 -06002191static void rbd_id_get(struct rbd_device *rbd_dev)
Alex Elderb7f23c32012-01-29 13:57:43 -06002192{
Alex Elder499afd52012-02-02 08:13:29 -06002193 rbd_dev->id = atomic64_inc_return(&rbd_id_max);
2194
2195 spin_lock(&rbd_dev_list_lock);
2196 list_add_tail(&rbd_dev->node, &rbd_dev_list);
2197 spin_unlock(&rbd_dev_list_lock);
Alex Elder1ddbe942012-01-29 13:57:44 -06002198}
Alex Elderb7f23c32012-01-29 13:57:43 -06002199
Alex Elder1ddbe942012-01-29 13:57:44 -06002200/*
Alex Elder499afd52012-02-02 08:13:29 -06002201 * Remove an rbd_dev from the global list, and record that its
2202 * identifier is no longer in use.
Alex Elder1ddbe942012-01-29 13:57:44 -06002203 */
Alex Elder499afd52012-02-02 08:13:29 -06002204static void rbd_id_put(struct rbd_device *rbd_dev)
Alex Elder1ddbe942012-01-29 13:57:44 -06002205{
Alex Elderd184f6b2012-01-29 13:57:44 -06002206 struct list_head *tmp;
2207 int rbd_id = rbd_dev->id;
2208 int max_id;
2209
2210 BUG_ON(rbd_id < 1);
Alex Elder499afd52012-02-02 08:13:29 -06002211
2212 spin_lock(&rbd_dev_list_lock);
2213 list_del_init(&rbd_dev->node);
Alex Elderd184f6b2012-01-29 13:57:44 -06002214
2215 /*
2216 * If the id being "put" is not the current maximum, there
2217 * is nothing special we need to do.
2218 */
2219 if (rbd_id != atomic64_read(&rbd_id_max)) {
2220 spin_unlock(&rbd_dev_list_lock);
2221 return;
2222 }
2223
2224 /*
2225 * We need to update the current maximum id. Search the
2226 * list to find out what it is. We're more likely to find
2227 * the maximum at the end, so search the list backward.
2228 */
2229 max_id = 0;
2230 list_for_each_prev(tmp, &rbd_dev_list) {
2231 struct rbd_device *rbd_dev;
2232
2233 rbd_dev = list_entry(tmp, struct rbd_device, node);
2234 if (rbd_id > max_id)
2235 max_id = rbd_id;
2236 }
Alex Elder499afd52012-02-02 08:13:29 -06002237 spin_unlock(&rbd_dev_list_lock);
Alex Elderb7f23c32012-01-29 13:57:43 -06002238
Alex Elder1ddbe942012-01-29 13:57:44 -06002239 /*
Alex Elderd184f6b2012-01-29 13:57:44 -06002240 * The max id could have been updated by rbd_id_get(), in
2241 * which case it now accurately reflects the new maximum.
2242 * Be careful not to overwrite the maximum value in that
2243 * case.
Alex Elder1ddbe942012-01-29 13:57:44 -06002244 */
Alex Elderd184f6b2012-01-29 13:57:44 -06002245 atomic64_cmpxchg(&rbd_id_max, rbd_id, max_id);
Alex Elderb7f23c32012-01-29 13:57:43 -06002246}
2247
Alex Eldera725f65e2012-02-02 08:13:30 -06002248/*
Alex Eldere28fff262012-02-02 08:13:30 -06002249 * Skips over white space at *buf, and updates *buf to point to the
2250 * first found non-space character (if any). Returns the length of
Alex Elder593a9e72012-02-07 12:03:37 -06002251 * the token (string of non-white space characters) found. Note
2252 * that *buf must be terminated with '\0'.
Alex Eldere28fff262012-02-02 08:13:30 -06002253 */
2254static inline size_t next_token(const char **buf)
2255{
2256 /*
2257 * These are the characters that produce nonzero for
2258 * isspace() in the "C" and "POSIX" locales.
2259 */
2260 const char *spaces = " \f\n\r\t\v";
2261
2262 *buf += strspn(*buf, spaces); /* Find start of token */
2263
2264 return strcspn(*buf, spaces); /* Return token length */
2265}
2266
2267/*
2268 * Finds the next token in *buf, and if the provided token buffer is
2269 * big enough, copies the found token into it. The result, if
Alex Elder593a9e72012-02-07 12:03:37 -06002270 * copied, is guaranteed to be terminated with '\0'. Note that *buf
2271 * must be terminated with '\0' on entry.
Alex Eldere28fff262012-02-02 08:13:30 -06002272 *
2273 * Returns the length of the token found (not including the '\0').
2274 * Return value will be 0 if no token is found, and it will be >=
2275 * token_size if the token would not fit.
2276 *
Alex Elder593a9e72012-02-07 12:03:37 -06002277 * The *buf pointer will be updated to point beyond the end of the
Alex Eldere28fff262012-02-02 08:13:30 -06002278 * found token. Note that this occurs even if the token buffer is
2279 * too small to hold it.
2280 */
2281static inline size_t copy_token(const char **buf,
2282 char *token,
2283 size_t token_size)
2284{
2285 size_t len;
2286
2287 len = next_token(buf);
2288 if (len < token_size) {
2289 memcpy(token, *buf, len);
2290 *(token + len) = '\0';
2291 }
2292 *buf += len;
2293
2294 return len;
2295}
2296
2297/*
Alex Eldera725f65e2012-02-02 08:13:30 -06002298 * This fills in the pool_name, obj, obj_len, snap_name, obj_len,
2299 * rbd_dev, rbd_md_name, and name fields of the given rbd_dev, based
2300 * on the list of monitor addresses and other options provided via
2301 * /sys/bus/rbd/add.
2302 */
2303static int rbd_add_parse_args(struct rbd_device *rbd_dev,
2304 const char *buf,
Alex Elder7ef32142012-02-02 08:13:30 -06002305 const char **mon_addrs,
Alex Elder5214ecc2012-02-02 08:13:30 -06002306 size_t *mon_addrs_size,
Alex Eldere28fff262012-02-02 08:13:30 -06002307 char *options,
2308 size_t options_size)
Alex Eldera725f65e2012-02-02 08:13:30 -06002309{
Alex Eldere28fff262012-02-02 08:13:30 -06002310 size_t len;
2311
2312 /* The first four tokens are required */
2313
Alex Elder7ef32142012-02-02 08:13:30 -06002314 len = next_token(&buf);
2315 if (!len)
Alex Eldera725f65e2012-02-02 08:13:30 -06002316 return -EINVAL;
Alex Elder5214ecc2012-02-02 08:13:30 -06002317 *mon_addrs_size = len + 1;
Alex Elder7ef32142012-02-02 08:13:30 -06002318 *mon_addrs = buf;
2319
2320 buf += len;
Alex Eldera725f65e2012-02-02 08:13:30 -06002321
Alex Eldere28fff262012-02-02 08:13:30 -06002322 len = copy_token(&buf, options, options_size);
2323 if (!len || len >= options_size)
2324 return -EINVAL;
Alex Eldera725f65e2012-02-02 08:13:30 -06002325
Alex Eldere28fff262012-02-02 08:13:30 -06002326 len = copy_token(&buf, rbd_dev->pool_name, sizeof (rbd_dev->pool_name));
2327 if (!len || len >= sizeof (rbd_dev->pool_name))
2328 return -EINVAL;
2329
2330 len = copy_token(&buf, rbd_dev->obj, sizeof (rbd_dev->obj));
2331 if (!len || len >= sizeof (rbd_dev->obj))
2332 return -EINVAL;
2333
2334 /* We have the object length in hand, save it. */
2335
2336 rbd_dev->obj_len = len;
2337
Alex Elder81a89792012-02-02 08:13:30 -06002338 BUILD_BUG_ON(RBD_MAX_MD_NAME_LEN
2339 < RBD_MAX_OBJ_NAME_LEN + sizeof (RBD_SUFFIX));
2340 sprintf(rbd_dev->obj_md_name, "%s%s", rbd_dev->obj, RBD_SUFFIX);
Alex Eldera725f65e2012-02-02 08:13:30 -06002341
Alex Eldere28fff262012-02-02 08:13:30 -06002342 /*
2343 * The snapshot name is optional, but it's an error if it's
2344 * too long. If no snapshot is supplied, fill in the default.
2345 */
2346 len = copy_token(&buf, rbd_dev->snap_name, sizeof (rbd_dev->snap_name));
2347 if (!len)
2348 memcpy(rbd_dev->snap_name, RBD_SNAP_HEAD_NAME,
2349 sizeof (RBD_SNAP_HEAD_NAME));
2350 else if (len >= sizeof (rbd_dev->snap_name))
2351 return -EINVAL;
2352
Alex Eldera725f65e2012-02-02 08:13:30 -06002353 return 0;
2354}
2355
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002356static ssize_t rbd_add(struct bus_type *bus,
2357 const char *buf,
2358 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002359{
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002360 struct rbd_device *rbd_dev;
Alex Elder7ef32142012-02-02 08:13:30 -06002361 const char *mon_addrs = NULL;
2362 size_t mon_addrs_size = 0;
Alex Elder27cc2592012-02-02 08:13:30 -06002363 char *options = NULL;
2364 struct ceph_osd_client *osdc;
2365 int rc = -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002366
2367 if (!try_module_get(THIS_MODULE))
2368 return -ENODEV;
2369
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002370 rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL);
2371 if (!rbd_dev)
Alex Elder27cc2592012-02-02 08:13:30 -06002372 goto err_nomem;
Alex Elder27cc2592012-02-02 08:13:30 -06002373 options = kmalloc(count, GFP_KERNEL);
2374 if (!options)
2375 goto err_nomem;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002376
2377 /* static rbd_device initialization */
2378 spin_lock_init(&rbd_dev->lock);
2379 INIT_LIST_HEAD(&rbd_dev->node);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002380 INIT_LIST_HEAD(&rbd_dev->snaps);
Josh Durginc6666012011-11-21 17:11:12 -08002381 init_rwsem(&rbd_dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002382
Josh Durginc6666012011-11-21 17:11:12 -08002383 init_rwsem(&rbd_dev->header_rwsem);
Alex Elder0e805a12012-01-11 19:42:15 -08002384
Alex Elderd184f6b2012-01-29 13:57:44 -06002385 /* generate unique id: find highest unique id, add one */
Alex Elder499afd52012-02-02 08:13:29 -06002386 rbd_id_get(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002387
Alex Eldera725f65e2012-02-02 08:13:30 -06002388 /* Fill in the device name, now that we have its id. */
Alex Elder81a89792012-02-02 08:13:30 -06002389 BUILD_BUG_ON(DEV_NAME_LEN
2390 < sizeof (RBD_DRV_NAME) + MAX_INT_FORMAT_WIDTH);
2391 sprintf(rbd_dev->name, "%s%d", RBD_DRV_NAME, rbd_dev->id);
Alex Eldere124a822012-01-29 13:57:44 -06002392
Alex Eldera725f65e2012-02-02 08:13:30 -06002393 /* parse add command */
Alex Elder7ef32142012-02-02 08:13:30 -06002394 rc = rbd_add_parse_args(rbd_dev, buf, &mon_addrs, &mon_addrs_size,
Alex Eldere28fff262012-02-02 08:13:30 -06002395 options, count);
Alex Eldera725f65e2012-02-02 08:13:30 -06002396 if (rc)
2397 goto err_put_id;
2398
Alex Elder5214ecc2012-02-02 08:13:30 -06002399 rbd_dev->rbd_client = rbd_get_client(mon_addrs, mon_addrs_size - 1,
2400 options);
Alex Elderd720bcb2012-02-02 08:13:30 -06002401 if (IS_ERR(rbd_dev->rbd_client)) {
2402 rc = PTR_ERR(rbd_dev->rbd_client);
Alex Elderf0f8cef2012-01-29 13:57:44 -06002403 goto err_put_id;
Alex Elderd720bcb2012-02-02 08:13:30 -06002404 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002405
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002406 /* pick the pool */
Alex Elder1dbb4392012-01-24 10:08:37 -06002407 osdc = &rbd_dev->rbd_client->client->osdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002408 rc = ceph_pg_poolid_by_name(osdc->osdmap, rbd_dev->pool_name);
2409 if (rc < 0)
2410 goto err_out_client;
2411 rbd_dev->poolid = rc;
2412
2413 /* register our block device */
Alex Elder27cc2592012-02-02 08:13:30 -06002414 rc = register_blkdev(0, rbd_dev->name);
2415 if (rc < 0)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002416 goto err_out_client;
Alex Elder27cc2592012-02-02 08:13:30 -06002417 rbd_dev->major = rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002418
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002419 rc = rbd_bus_add_dev(rbd_dev);
2420 if (rc)
Yehuda Sadeh766fc432011-01-07 14:58:42 -08002421 goto err_out_blkdev;
2422
Alex Elder32eec682012-02-08 16:11:14 -06002423 /*
2424 * At this point cleanup in the event of an error is the job
2425 * of the sysfs code (initiated by rbd_bus_del_dev()).
2426 *
2427 * Set up and announce blkdev mapping.
2428 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002429 rc = rbd_init_disk(rbd_dev);
2430 if (rc)
Yehuda Sadeh766fc432011-01-07 14:58:42 -08002431 goto err_out_bus;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002432
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002433 rc = rbd_init_watch_dev(rbd_dev);
2434 if (rc)
2435 goto err_out_bus;
2436
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002437 return count;
2438
Yehuda Sadeh766fc432011-01-07 14:58:42 -08002439err_out_bus:
Yehuda Sadeh766fc432011-01-07 14:58:42 -08002440 /* this will also clean up rest of rbd_dev stuff */
2441
2442 rbd_bus_del_dev(rbd_dev);
2443 kfree(options);
Yehuda Sadeh766fc432011-01-07 14:58:42 -08002444 return rc;
2445
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002446err_out_blkdev:
2447 unregister_blkdev(rbd_dev->major, rbd_dev->name);
2448err_out_client:
2449 rbd_put_client(rbd_dev);
Alex Elderf0f8cef2012-01-29 13:57:44 -06002450err_put_id:
Alex Elder499afd52012-02-02 08:13:29 -06002451 rbd_id_put(rbd_dev);
Alex Elder27cc2592012-02-02 08:13:30 -06002452err_nomem:
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002453 kfree(options);
Alex Elder27cc2592012-02-02 08:13:30 -06002454 kfree(rbd_dev);
2455
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002456 dout("Error adding device %s\n", buf);
2457 module_put(THIS_MODULE);
Alex Elder27cc2592012-02-02 08:13:30 -06002458
2459 return (ssize_t) rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002460}
2461
2462static struct rbd_device *__rbd_get_dev(unsigned long id)
2463{
2464 struct list_head *tmp;
2465 struct rbd_device *rbd_dev;
2466
Alex Eldere124a822012-01-29 13:57:44 -06002467 spin_lock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002468 list_for_each(tmp, &rbd_dev_list) {
2469 rbd_dev = list_entry(tmp, struct rbd_device, node);
Alex Eldere124a822012-01-29 13:57:44 -06002470 if (rbd_dev->id == id) {
2471 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002472 return rbd_dev;
Alex Eldere124a822012-01-29 13:57:44 -06002473 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002474 }
Alex Eldere124a822012-01-29 13:57:44 -06002475 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002476 return NULL;
2477}
2478
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002479static void rbd_dev_release(struct device *dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002480{
Alex Elder593a9e72012-02-07 12:03:37 -06002481 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002482
Alex Elder1dbb4392012-01-24 10:08:37 -06002483 if (rbd_dev->watch_request) {
2484 struct ceph_client *client = rbd_dev->rbd_client->client;
2485
2486 ceph_osdc_unregister_linger_request(&client->osdc,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002487 rbd_dev->watch_request);
Alex Elder1dbb4392012-01-24 10:08:37 -06002488 }
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002489 if (rbd_dev->watch_event)
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07002490 rbd_req_sync_unwatch(rbd_dev, rbd_dev->obj_md_name);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002491
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002492 rbd_put_client(rbd_dev);
2493
2494 /* clean up and free blkdev */
2495 rbd_free_disk(rbd_dev);
2496 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Alex Elder32eec682012-02-08 16:11:14 -06002497
2498 /* done with the id, and with the rbd_dev */
2499 rbd_id_put(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002500 kfree(rbd_dev);
2501
2502 /* release module ref */
2503 module_put(THIS_MODULE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002504}
2505
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002506static ssize_t rbd_remove(struct bus_type *bus,
2507 const char *buf,
2508 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002509{
2510 struct rbd_device *rbd_dev = NULL;
2511 int target_id, rc;
2512 unsigned long ul;
2513 int ret = count;
2514
2515 rc = strict_strtoul(buf, 10, &ul);
2516 if (rc)
2517 return rc;
2518
2519 /* convert to int; abort if we lost anything in the conversion */
2520 target_id = (int) ul;
2521 if (target_id != ul)
2522 return -EINVAL;
2523
2524 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2525
2526 rbd_dev = __rbd_get_dev(target_id);
2527 if (!rbd_dev) {
2528 ret = -ENOENT;
2529 goto done;
2530 }
2531
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002532 __rbd_remove_all_snaps(rbd_dev);
2533 rbd_bus_del_dev(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002534
2535done:
2536 mutex_unlock(&ctl_mutex);
2537 return ret;
2538}
2539
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002540static ssize_t rbd_snap_add(struct device *dev,
2541 struct device_attribute *attr,
2542 const char *buf,
2543 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002544{
Alex Elder593a9e72012-02-07 12:03:37 -06002545 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002546 int ret;
2547 char *name = kmalloc(count + 1, GFP_KERNEL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002548 if (!name)
2549 return -ENOMEM;
2550
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002551 snprintf(name, count, "%s", buf);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002552
2553 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2554
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002555 ret = rbd_header_add_snap(rbd_dev,
2556 name, GFP_KERNEL);
2557 if (ret < 0)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002558 goto err_unlock;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002559
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002560 ret = __rbd_update_snaps(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002561 if (ret < 0)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002562 goto err_unlock;
2563
2564 /* shouldn't hold ctl_mutex when notifying.. notify might
2565 trigger a watch callback that would need to get that mutex */
2566 mutex_unlock(&ctl_mutex);
2567
2568 /* make a best effort, don't error if failed */
2569 rbd_req_sync_notify(rbd_dev, rbd_dev->obj_md_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002570
2571 ret = count;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002572 kfree(name);
2573 return ret;
2574
2575err_unlock:
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002576 mutex_unlock(&ctl_mutex);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002577 kfree(name);
2578 return ret;
2579}
2580
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002581/*
2582 * create control files in sysfs
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002583 * /sys/bus/rbd/...
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002584 */
2585static int rbd_sysfs_init(void)
2586{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002587 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002588
Alex Elderfed4c142012-02-07 12:03:36 -06002589 ret = device_register(&rbd_root_dev);
Alex Elder21079782012-01-24 10:08:36 -06002590 if (ret < 0)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002591 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002592
Alex Elderfed4c142012-02-07 12:03:36 -06002593 ret = bus_register(&rbd_bus_type);
2594 if (ret < 0)
2595 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002596
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002597 return ret;
2598}
2599
2600static void rbd_sysfs_cleanup(void)
2601{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002602 bus_unregister(&rbd_bus_type);
Alex Elderfed4c142012-02-07 12:03:36 -06002603 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002604}
2605
2606int __init rbd_init(void)
2607{
2608 int rc;
2609
2610 rc = rbd_sysfs_init();
2611 if (rc)
2612 return rc;
Alex Elderf0f8cef2012-01-29 13:57:44 -06002613 pr_info("loaded " RBD_DRV_NAME_LONG "\n");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002614 return 0;
2615}
2616
2617void __exit rbd_exit(void)
2618{
2619 rbd_sysfs_cleanup();
2620}
2621
2622module_init(rbd_init);
2623module_exit(rbd_exit);
2624
2625MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
2626MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
2627MODULE_DESCRIPTION("rados block device");
2628
2629/* following authorship retained from original osdblk.c */
2630MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
2631
2632MODULE_LICENSE("GPL");