blob: 5ab9f55d3e0cb08ea4323fdc74159e5d9899dccb [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,
Xi Wang50f7c4c2012-04-20 15:49:44 -0500490 u32 allocated_snaps,
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700491 gfp_t gfp_flags)
492{
Xi Wang50f7c4c2012-04-20 15:49:44 -0500493 u32 i, snap_count;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700494
Alex Elder21079782012-01-24 10:08:36 -0600495 if (memcmp(ondisk, RBD_HEADER_TEXT, sizeof(RBD_HEADER_TEXT)))
Josh Durgin81e759f2011-11-15 14:49:53 -0800496 return -ENXIO;
Josh Durgin81e759f2011-11-15 14:49:53 -0800497
Alex Elder00f1f362012-02-07 12:03:36 -0600498 snap_count = le32_to_cpu(ondisk->snap_count);
Xi Wang50f7c4c2012-04-20 15:49:44 -0500499 if (snap_count > (UINT_MAX - sizeof(struct ceph_snap_context))
500 / sizeof (*ondisk))
501 return -EINVAL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700502 header->snapc = kmalloc(sizeof(struct ceph_snap_context) +
Alex Elder21079782012-01-24 10:08:36 -0600503 snap_count * sizeof (*ondisk),
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700504 gfp_flags);
505 if (!header->snapc)
506 return -ENOMEM;
Alex Elder00f1f362012-02-07 12:03:36 -0600507
Alex Elder00f1f362012-02-07 12:03:36 -0600508 header->snap_names_len = le64_to_cpu(ondisk->snap_names_len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700509 if (snap_count) {
510 header->snap_names = kmalloc(header->snap_names_len,
Dan Carpenterf8ad4952012-04-20 15:49:44 -0500511 gfp_flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700512 if (!header->snap_names)
513 goto err_snapc;
514 header->snap_sizes = kmalloc(snap_count * sizeof(u64),
Dan Carpenterf8ad4952012-04-20 15:49:44 -0500515 gfp_flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700516 if (!header->snap_sizes)
517 goto err_names;
518 } else {
519 header->snap_names = NULL;
520 header->snap_sizes = NULL;
521 }
522 memcpy(header->block_name, ondisk->block_name,
523 sizeof(ondisk->block_name));
524
525 header->image_size = le64_to_cpu(ondisk->image_size);
526 header->obj_order = ondisk->options.order;
527 header->crypt_type = ondisk->options.crypt_type;
528 header->comp_type = ondisk->options.comp_type;
529
530 atomic_set(&header->snapc->nref, 1);
531 header->snap_seq = le64_to_cpu(ondisk->snap_seq);
532 header->snapc->num_snaps = snap_count;
533 header->total_snaps = snap_count;
534
Alex Elder21079782012-01-24 10:08:36 -0600535 if (snap_count && allocated_snaps == snap_count) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700536 for (i = 0; i < snap_count; i++) {
537 header->snapc->snaps[i] =
538 le64_to_cpu(ondisk->snaps[i].id);
539 header->snap_sizes[i] =
540 le64_to_cpu(ondisk->snaps[i].image_size);
541 }
542
543 /* copy snapshot names */
544 memcpy(header->snap_names, &ondisk->snaps[i],
545 header->snap_names_len);
546 }
547
548 return 0;
549
550err_names:
551 kfree(header->snap_names);
552err_snapc:
553 kfree(header->snapc);
Alex Elder00f1f362012-02-07 12:03:36 -0600554 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700555}
556
557static int snap_index(struct rbd_image_header *header, int snap_num)
558{
559 return header->total_snaps - snap_num;
560}
561
562static u64 cur_snap_id(struct rbd_device *rbd_dev)
563{
564 struct rbd_image_header *header = &rbd_dev->header;
565
566 if (!rbd_dev->cur_snap)
567 return 0;
568
569 return header->snapc->snaps[snap_index(header, rbd_dev->cur_snap)];
570}
571
572static int snap_by_name(struct rbd_image_header *header, const char *snap_name,
573 u64 *seq, u64 *size)
574{
575 int i;
576 char *p = header->snap_names;
577
Alex Elder00f1f362012-02-07 12:03:36 -0600578 for (i = 0; i < header->total_snaps; i++) {
579 if (!strcmp(snap_name, p)) {
580
581 /* Found it. Pass back its id and/or size */
582
583 if (seq)
584 *seq = header->snapc->snaps[i];
585 if (size)
586 *size = header->snap_sizes[i];
587 return i;
588 }
589 p += strlen(p) + 1; /* Skip ahead to the next name */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700590 }
Alex Elder00f1f362012-02-07 12:03:36 -0600591 return -ENOENT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700592}
593
Josh Durgincc9d7342011-11-21 18:19:13 -0800594static int rbd_header_set_snap(struct rbd_device *dev, u64 *size)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700595{
596 struct rbd_image_header *header = &dev->header;
597 struct ceph_snap_context *snapc = header->snapc;
598 int ret = -ENOENT;
599
Josh Durgincc9d7342011-11-21 18:19:13 -0800600 BUILD_BUG_ON(sizeof (dev->snap_name) < sizeof (RBD_SNAP_HEAD_NAME));
601
Josh Durginc6666012011-11-21 17:11:12 -0800602 down_write(&dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700603
Josh Durgincc9d7342011-11-21 18:19:13 -0800604 if (!memcmp(dev->snap_name, RBD_SNAP_HEAD_NAME,
605 sizeof (RBD_SNAP_HEAD_NAME))) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700606 if (header->total_snaps)
607 snapc->seq = header->snap_seq;
608 else
609 snapc->seq = 0;
610 dev->cur_snap = 0;
611 dev->read_only = 0;
612 if (size)
613 *size = header->image_size;
614 } else {
Josh Durgincc9d7342011-11-21 18:19:13 -0800615 ret = snap_by_name(header, dev->snap_name, &snapc->seq, size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700616 if (ret < 0)
617 goto done;
618
619 dev->cur_snap = header->total_snaps - ret;
620 dev->read_only = 1;
621 }
622
623 ret = 0;
624done:
Josh Durginc6666012011-11-21 17:11:12 -0800625 up_write(&dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700626 return ret;
627}
628
629static void rbd_header_free(struct rbd_image_header *header)
630{
631 kfree(header->snapc);
632 kfree(header->snap_names);
633 kfree(header->snap_sizes);
634}
635
636/*
637 * get the actual striped segment name, offset and length
638 */
639static u64 rbd_get_segment(struct rbd_image_header *header,
640 const char *block_name,
641 u64 ofs, u64 len,
642 char *seg_name, u64 *segofs)
643{
644 u64 seg = ofs >> header->obj_order;
645
646 if (seg_name)
647 snprintf(seg_name, RBD_MAX_SEG_NAME_LEN,
648 "%s.%012llx", block_name, seg);
649
650 ofs = ofs & ((1 << header->obj_order) - 1);
651 len = min_t(u64, len, (1 << header->obj_order) - ofs);
652
653 if (segofs)
654 *segofs = ofs;
655
656 return len;
657}
658
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700659static int rbd_get_num_segments(struct rbd_image_header *header,
660 u64 ofs, u64 len)
661{
662 u64 start_seg = ofs >> header->obj_order;
663 u64 end_seg = (ofs + len - 1) >> header->obj_order;
664 return end_seg - start_seg + 1;
665}
666
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700667/*
Josh Durgin029bcbd2011-07-22 11:35:23 -0700668 * returns the size of an object in the image
669 */
670static u64 rbd_obj_bytes(struct rbd_image_header *header)
671{
672 return 1 << header->obj_order;
673}
674
675/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700676 * bio helpers
677 */
678
679static void bio_chain_put(struct bio *chain)
680{
681 struct bio *tmp;
682
683 while (chain) {
684 tmp = chain;
685 chain = chain->bi_next;
686 bio_put(tmp);
687 }
688}
689
690/*
691 * zeros a bio chain, starting at specific offset
692 */
693static void zero_bio_chain(struct bio *chain, int start_ofs)
694{
695 struct bio_vec *bv;
696 unsigned long flags;
697 void *buf;
698 int i;
699 int pos = 0;
700
701 while (chain) {
702 bio_for_each_segment(bv, chain, i) {
703 if (pos + bv->bv_len > start_ofs) {
704 int remainder = max(start_ofs - pos, 0);
705 buf = bvec_kmap_irq(bv, &flags);
706 memset(buf + remainder, 0,
707 bv->bv_len - remainder);
Dan Carpenter85b5aaa2010-10-11 21:15:11 +0200708 bvec_kunmap_irq(buf, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700709 }
710 pos += bv->bv_len;
711 }
712
713 chain = chain->bi_next;
714 }
715}
716
717/*
718 * bio_chain_clone - clone a chain of bios up to a certain length.
719 * might return a bio_pair that will need to be released.
720 */
721static struct bio *bio_chain_clone(struct bio **old, struct bio **next,
722 struct bio_pair **bp,
723 int len, gfp_t gfpmask)
724{
725 struct bio *tmp, *old_chain = *old, *new_chain = NULL, *tail = NULL;
726 int total = 0;
727
728 if (*bp) {
729 bio_pair_release(*bp);
730 *bp = NULL;
731 }
732
733 while (old_chain && (total < len)) {
734 tmp = bio_kmalloc(gfpmask, old_chain->bi_max_vecs);
735 if (!tmp)
736 goto err_out;
737
738 if (total + old_chain->bi_size > len) {
739 struct bio_pair *bp;
740
741 /*
742 * this split can only happen with a single paged bio,
743 * split_bio will BUG_ON if this is not the case
744 */
745 dout("bio_chain_clone split! total=%d remaining=%d"
746 "bi_size=%d\n",
747 (int)total, (int)len-total,
748 (int)old_chain->bi_size);
749
750 /* split the bio. We'll release it either in the next
751 call, or it will have to be released outside */
Alex Elder593a9e72012-02-07 12:03:37 -0600752 bp = bio_split(old_chain, (len - total) / SECTOR_SIZE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700753 if (!bp)
754 goto err_out;
755
756 __bio_clone(tmp, &bp->bio1);
757
758 *next = &bp->bio2;
759 } else {
760 __bio_clone(tmp, old_chain);
761 *next = old_chain->bi_next;
762 }
763
764 tmp->bi_bdev = NULL;
765 gfpmask &= ~__GFP_WAIT;
766 tmp->bi_next = NULL;
767
768 if (!new_chain) {
769 new_chain = tail = tmp;
770 } else {
771 tail->bi_next = tmp;
772 tail = tmp;
773 }
774 old_chain = old_chain->bi_next;
775
776 total += tmp->bi_size;
777 }
778
779 BUG_ON(total < len);
780
781 if (tail)
782 tail->bi_next = NULL;
783
784 *old = old_chain;
785
786 return new_chain;
787
788err_out:
789 dout("bio_chain_clone with err\n");
790 bio_chain_put(new_chain);
791 return NULL;
792}
793
794/*
795 * helpers for osd request op vectors.
796 */
797static int rbd_create_rw_ops(struct ceph_osd_req_op **ops,
798 int num_ops,
799 int opcode,
800 u32 payload_len)
801{
802 *ops = kzalloc(sizeof(struct ceph_osd_req_op) * (num_ops + 1),
803 GFP_NOIO);
804 if (!*ops)
805 return -ENOMEM;
806 (*ops)[0].op = opcode;
807 /*
808 * op extent offset and length will be set later on
809 * in calc_raw_layout()
810 */
811 (*ops)[0].payload_len = payload_len;
812 return 0;
813}
814
815static void rbd_destroy_ops(struct ceph_osd_req_op *ops)
816{
817 kfree(ops);
818}
819
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700820static void rbd_coll_end_req_index(struct request *rq,
821 struct rbd_req_coll *coll,
822 int index,
823 int ret, u64 len)
824{
825 struct request_queue *q;
826 int min, max, i;
827
828 dout("rbd_coll_end_req_index %p index %d ret %d len %lld\n",
829 coll, index, ret, len);
830
831 if (!rq)
832 return;
833
834 if (!coll) {
835 blk_end_request(rq, ret, len);
836 return;
837 }
838
839 q = rq->q;
840
841 spin_lock_irq(q->queue_lock);
842 coll->status[index].done = 1;
843 coll->status[index].rc = ret;
844 coll->status[index].bytes = len;
845 max = min = coll->num_done;
846 while (max < coll->total && coll->status[max].done)
847 max++;
848
849 for (i = min; i<max; i++) {
850 __blk_end_request(rq, coll->status[i].rc,
851 coll->status[i].bytes);
852 coll->num_done++;
853 kref_put(&coll->kref, rbd_coll_release);
854 }
855 spin_unlock_irq(q->queue_lock);
856}
857
858static void rbd_coll_end_req(struct rbd_request *req,
859 int ret, u64 len)
860{
861 rbd_coll_end_req_index(req->rq, req->coll, req->coll_index, ret, len);
862}
863
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700864/*
865 * Send ceph osd request
866 */
867static int rbd_do_request(struct request *rq,
868 struct rbd_device *dev,
869 struct ceph_snap_context *snapc,
870 u64 snapid,
871 const char *obj, u64 ofs, u64 len,
872 struct bio *bio,
873 struct page **pages,
874 int num_pages,
875 int flags,
876 struct ceph_osd_req_op *ops,
877 int num_reply,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700878 struct rbd_req_coll *coll,
879 int coll_index,
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700880 void (*rbd_cb)(struct ceph_osd_request *req,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700881 struct ceph_msg *msg),
882 struct ceph_osd_request **linger_req,
883 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700884{
885 struct ceph_osd_request *req;
886 struct ceph_file_layout *layout;
887 int ret;
888 u64 bno;
889 struct timespec mtime = CURRENT_TIME;
890 struct rbd_request *req_data;
891 struct ceph_osd_request_head *reqhead;
Alex Elder1dbb4392012-01-24 10:08:37 -0600892 struct ceph_osd_client *osdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700893
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700894 req_data = kzalloc(sizeof(*req_data), GFP_NOIO);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700895 if (!req_data) {
896 if (coll)
897 rbd_coll_end_req_index(rq, coll, coll_index,
898 -ENOMEM, len);
899 return -ENOMEM;
900 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700901
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700902 if (coll) {
903 req_data->coll = coll;
904 req_data->coll_index = coll_index;
905 }
906
907 dout("rbd_do_request obj=%s ofs=%lld len=%lld\n", obj, len, ofs);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700908
Josh Durginc6666012011-11-21 17:11:12 -0800909 down_read(&dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700910
Alex Elder1dbb4392012-01-24 10:08:37 -0600911 osdc = &dev->rbd_client->client->osdc;
912 req = ceph_osdc_alloc_request(osdc, flags, snapc, ops,
913 false, GFP_NOIO, pages, bio);
Sage Weil4ad12622011-05-03 09:23:36 -0700914 if (!req) {
Josh Durginc6666012011-11-21 17:11:12 -0800915 up_read(&dev->header_rwsem);
Sage Weil4ad12622011-05-03 09:23:36 -0700916 ret = -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700917 goto done_pages;
918 }
919
920 req->r_callback = rbd_cb;
921
922 req_data->rq = rq;
923 req_data->bio = bio;
924 req_data->pages = pages;
925 req_data->len = len;
926
927 req->r_priv = req_data;
928
929 reqhead = req->r_request->front.iov_base;
930 reqhead->snapid = cpu_to_le64(CEPH_NOSNAP);
931
932 strncpy(req->r_oid, obj, sizeof(req->r_oid));
933 req->r_oid_len = strlen(req->r_oid);
934
935 layout = &req->r_file_layout;
936 memset(layout, 0, sizeof(*layout));
937 layout->fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
938 layout->fl_stripe_count = cpu_to_le32(1);
939 layout->fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700940 layout->fl_pg_pool = cpu_to_le32(dev->poolid);
Alex Elder1dbb4392012-01-24 10:08:37 -0600941 ceph_calc_raw_layout(osdc, layout, snapid, ofs, &len, &bno,
942 req, ops);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700943
944 ceph_osdc_build_request(req, ofs, &len,
945 ops,
946 snapc,
947 &mtime,
948 req->r_oid, req->r_oid_len);
Josh Durginc6666012011-11-21 17:11:12 -0800949 up_read(&dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700950
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700951 if (linger_req) {
Alex Elder1dbb4392012-01-24 10:08:37 -0600952 ceph_osdc_set_request_linger(osdc, req);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700953 *linger_req = req;
954 }
955
Alex Elder1dbb4392012-01-24 10:08:37 -0600956 ret = ceph_osdc_start_request(osdc, req, false);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700957 if (ret < 0)
958 goto done_err;
959
960 if (!rbd_cb) {
Alex Elder1dbb4392012-01-24 10:08:37 -0600961 ret = ceph_osdc_wait_request(osdc, req);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700962 if (ver)
963 *ver = le64_to_cpu(req->r_reassert_version.version);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700964 dout("reassert_ver=%lld\n",
965 le64_to_cpu(req->r_reassert_version.version));
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700966 ceph_osdc_put_request(req);
967 }
968 return ret;
969
970done_err:
971 bio_chain_put(req_data->bio);
972 ceph_osdc_put_request(req);
973done_pages:
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700974 rbd_coll_end_req(req_data, ret, len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700975 kfree(req_data);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700976 return ret;
977}
978
979/*
980 * Ceph osd op callback
981 */
982static void rbd_req_cb(struct ceph_osd_request *req, struct ceph_msg *msg)
983{
984 struct rbd_request *req_data = req->r_priv;
985 struct ceph_osd_reply_head *replyhead;
986 struct ceph_osd_op *op;
987 __s32 rc;
988 u64 bytes;
989 int read_op;
990
991 /* parse reply */
992 replyhead = msg->front.iov_base;
993 WARN_ON(le32_to_cpu(replyhead->num_ops) == 0);
994 op = (void *)(replyhead + 1);
995 rc = le32_to_cpu(replyhead->result);
996 bytes = le64_to_cpu(op->extent.length);
997 read_op = (le32_to_cpu(op->op) == CEPH_OSD_OP_READ);
998
999 dout("rbd_req_cb bytes=%lld readop=%d rc=%d\n", bytes, read_op, rc);
1000
1001 if (rc == -ENOENT && read_op) {
1002 zero_bio_chain(req_data->bio, 0);
1003 rc = 0;
1004 } else if (rc == 0 && read_op && bytes < req_data->len) {
1005 zero_bio_chain(req_data->bio, bytes);
1006 bytes = req_data->len;
1007 }
1008
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001009 rbd_coll_end_req(req_data, rc, bytes);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001010
1011 if (req_data->bio)
1012 bio_chain_put(req_data->bio);
1013
1014 ceph_osdc_put_request(req);
1015 kfree(req_data);
1016}
1017
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001018static void rbd_simple_req_cb(struct ceph_osd_request *req, struct ceph_msg *msg)
1019{
1020 ceph_osdc_put_request(req);
1021}
1022
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001023/*
1024 * Do a synchronous ceph osd operation
1025 */
1026static int rbd_req_sync_op(struct rbd_device *dev,
1027 struct ceph_snap_context *snapc,
1028 u64 snapid,
1029 int opcode,
1030 int flags,
1031 struct ceph_osd_req_op *orig_ops,
1032 int num_reply,
1033 const char *obj,
1034 u64 ofs, u64 len,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001035 char *buf,
1036 struct ceph_osd_request **linger_req,
1037 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001038{
1039 int ret;
1040 struct page **pages;
1041 int num_pages;
1042 struct ceph_osd_req_op *ops = orig_ops;
1043 u32 payload_len;
1044
1045 num_pages = calc_pages_for(ofs , len);
1046 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
Dan Carpenterb8d06382010-10-11 21:14:23 +02001047 if (IS_ERR(pages))
1048 return PTR_ERR(pages);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001049
1050 if (!orig_ops) {
1051 payload_len = (flags & CEPH_OSD_FLAG_WRITE ? len : 0);
1052 ret = rbd_create_rw_ops(&ops, 1, opcode, payload_len);
1053 if (ret < 0)
1054 goto done;
1055
1056 if ((flags & CEPH_OSD_FLAG_WRITE) && buf) {
1057 ret = ceph_copy_to_page_vector(pages, buf, ofs, len);
1058 if (ret < 0)
1059 goto done_ops;
1060 }
1061 }
1062
1063 ret = rbd_do_request(NULL, dev, snapc, snapid,
1064 obj, ofs, len, NULL,
1065 pages, num_pages,
1066 flags,
1067 ops,
1068 2,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001069 NULL, 0,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001070 NULL,
1071 linger_req, ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001072 if (ret < 0)
1073 goto done_ops;
1074
1075 if ((flags & CEPH_OSD_FLAG_READ) && buf)
1076 ret = ceph_copy_from_page_vector(pages, buf, ofs, ret);
1077
1078done_ops:
1079 if (!orig_ops)
1080 rbd_destroy_ops(ops);
1081done:
1082 ceph_release_page_vector(pages, num_pages);
1083 return ret;
1084}
1085
1086/*
1087 * Do an asynchronous ceph osd operation
1088 */
1089static int rbd_do_op(struct request *rq,
1090 struct rbd_device *rbd_dev ,
1091 struct ceph_snap_context *snapc,
1092 u64 snapid,
1093 int opcode, int flags, int num_reply,
1094 u64 ofs, u64 len,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001095 struct bio *bio,
1096 struct rbd_req_coll *coll,
1097 int coll_index)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001098{
1099 char *seg_name;
1100 u64 seg_ofs;
1101 u64 seg_len;
1102 int ret;
1103 struct ceph_osd_req_op *ops;
1104 u32 payload_len;
1105
1106 seg_name = kmalloc(RBD_MAX_SEG_NAME_LEN + 1, GFP_NOIO);
1107 if (!seg_name)
1108 return -ENOMEM;
1109
1110 seg_len = rbd_get_segment(&rbd_dev->header,
1111 rbd_dev->header.block_name,
1112 ofs, len,
1113 seg_name, &seg_ofs);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001114
1115 payload_len = (flags & CEPH_OSD_FLAG_WRITE ? seg_len : 0);
1116
1117 ret = rbd_create_rw_ops(&ops, 1, opcode, payload_len);
1118 if (ret < 0)
1119 goto done;
1120
1121 /* we've taken care of segment sizes earlier when we
1122 cloned the bios. We should never have a segment
1123 truncated at this point */
1124 BUG_ON(seg_len < len);
1125
1126 ret = rbd_do_request(rq, rbd_dev, snapc, snapid,
1127 seg_name, seg_ofs, seg_len,
1128 bio,
1129 NULL, 0,
1130 flags,
1131 ops,
1132 num_reply,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001133 coll, coll_index,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001134 rbd_req_cb, 0, NULL);
Sage Weil11f77002011-05-12 16:13:54 -07001135
1136 rbd_destroy_ops(ops);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001137done:
1138 kfree(seg_name);
1139 return ret;
1140}
1141
1142/*
1143 * Request async osd write
1144 */
1145static int rbd_req_write(struct request *rq,
1146 struct rbd_device *rbd_dev,
1147 struct ceph_snap_context *snapc,
1148 u64 ofs, u64 len,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001149 struct bio *bio,
1150 struct rbd_req_coll *coll,
1151 int coll_index)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001152{
1153 return rbd_do_op(rq, rbd_dev, snapc, CEPH_NOSNAP,
1154 CEPH_OSD_OP_WRITE,
1155 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1156 2,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001157 ofs, len, bio, coll, coll_index);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001158}
1159
1160/*
1161 * Request async osd read
1162 */
1163static int rbd_req_read(struct request *rq,
1164 struct rbd_device *rbd_dev,
1165 u64 snapid,
1166 u64 ofs, u64 len,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001167 struct bio *bio,
1168 struct rbd_req_coll *coll,
1169 int coll_index)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001170{
1171 return rbd_do_op(rq, rbd_dev, NULL,
1172 (snapid ? snapid : CEPH_NOSNAP),
1173 CEPH_OSD_OP_READ,
1174 CEPH_OSD_FLAG_READ,
1175 2,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001176 ofs, len, bio, coll, coll_index);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001177}
1178
1179/*
1180 * Request sync osd read
1181 */
1182static int rbd_req_sync_read(struct rbd_device *dev,
1183 struct ceph_snap_context *snapc,
1184 u64 snapid,
1185 const char *obj,
1186 u64 ofs, u64 len,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001187 char *buf,
1188 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001189{
1190 return rbd_req_sync_op(dev, NULL,
1191 (snapid ? snapid : CEPH_NOSNAP),
1192 CEPH_OSD_OP_READ,
1193 CEPH_OSD_FLAG_READ,
1194 NULL,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001195 1, obj, ofs, len, buf, NULL, ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001196}
1197
1198/*
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001199 * Request sync osd watch
1200 */
1201static int rbd_req_sync_notify_ack(struct rbd_device *dev,
1202 u64 ver,
1203 u64 notify_id,
1204 const char *obj)
1205{
1206 struct ceph_osd_req_op *ops;
1207 struct page **pages = NULL;
Sage Weil11f77002011-05-12 16:13:54 -07001208 int ret;
1209
1210 ret = rbd_create_rw_ops(&ops, 1, CEPH_OSD_OP_NOTIFY_ACK, 0);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001211 if (ret < 0)
1212 return ret;
1213
1214 ops[0].watch.ver = cpu_to_le64(dev->header.obj_version);
1215 ops[0].watch.cookie = notify_id;
1216 ops[0].watch.flag = 0;
1217
1218 ret = rbd_do_request(NULL, dev, NULL, CEPH_NOSNAP,
1219 obj, 0, 0, NULL,
1220 pages, 0,
1221 CEPH_OSD_FLAG_READ,
1222 ops,
1223 1,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001224 NULL, 0,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001225 rbd_simple_req_cb, 0, NULL);
1226
1227 rbd_destroy_ops(ops);
1228 return ret;
1229}
1230
1231static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
1232{
1233 struct rbd_device *dev = (struct rbd_device *)data;
Sage Weil13143d22011-05-12 16:08:30 -07001234 int rc;
1235
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001236 if (!dev)
1237 return;
1238
1239 dout("rbd_watch_cb %s notify_id=%lld opcode=%d\n", dev->obj_md_name,
1240 notify_id, (int)opcode);
1241 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Sage Weil13143d22011-05-12 16:08:30 -07001242 rc = __rbd_update_snaps(dev);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001243 mutex_unlock(&ctl_mutex);
Sage Weil13143d22011-05-12 16:08:30 -07001244 if (rc)
Alex Elderf0f8cef2012-01-29 13:57:44 -06001245 pr_warning(RBD_DRV_NAME "%d got notification but failed to "
1246 " update snaps: %d\n", dev->major, rc);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001247
1248 rbd_req_sync_notify_ack(dev, ver, notify_id, dev->obj_md_name);
1249}
1250
1251/*
1252 * Request sync osd watch
1253 */
1254static int rbd_req_sync_watch(struct rbd_device *dev,
1255 const char *obj,
1256 u64 ver)
1257{
1258 struct ceph_osd_req_op *ops;
Alex Elder1dbb4392012-01-24 10:08:37 -06001259 struct ceph_osd_client *osdc = &dev->rbd_client->client->osdc;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001260
1261 int ret = rbd_create_rw_ops(&ops, 1, CEPH_OSD_OP_WATCH, 0);
1262 if (ret < 0)
1263 return ret;
1264
1265 ret = ceph_osdc_create_event(osdc, rbd_watch_cb, 0,
1266 (void *)dev, &dev->watch_event);
1267 if (ret < 0)
1268 goto fail;
1269
1270 ops[0].watch.ver = cpu_to_le64(ver);
1271 ops[0].watch.cookie = cpu_to_le64(dev->watch_event->cookie);
1272 ops[0].watch.flag = 1;
1273
1274 ret = rbd_req_sync_op(dev, NULL,
1275 CEPH_NOSNAP,
1276 0,
1277 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1278 ops,
1279 1, obj, 0, 0, NULL,
1280 &dev->watch_request, NULL);
1281
1282 if (ret < 0)
1283 goto fail_event;
1284
1285 rbd_destroy_ops(ops);
1286 return 0;
1287
1288fail_event:
1289 ceph_osdc_cancel_event(dev->watch_event);
1290 dev->watch_event = NULL;
1291fail:
1292 rbd_destroy_ops(ops);
1293 return ret;
1294}
1295
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001296/*
1297 * Request sync osd unwatch
1298 */
1299static int rbd_req_sync_unwatch(struct rbd_device *dev,
1300 const char *obj)
1301{
1302 struct ceph_osd_req_op *ops;
1303
1304 int ret = rbd_create_rw_ops(&ops, 1, CEPH_OSD_OP_WATCH, 0);
1305 if (ret < 0)
1306 return ret;
1307
1308 ops[0].watch.ver = 0;
1309 ops[0].watch.cookie = cpu_to_le64(dev->watch_event->cookie);
1310 ops[0].watch.flag = 0;
1311
1312 ret = rbd_req_sync_op(dev, NULL,
1313 CEPH_NOSNAP,
1314 0,
1315 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1316 ops,
1317 1, obj, 0, 0, NULL, NULL, NULL);
1318
1319 rbd_destroy_ops(ops);
1320 ceph_osdc_cancel_event(dev->watch_event);
1321 dev->watch_event = NULL;
1322 return ret;
1323}
1324
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001325struct rbd_notify_info {
1326 struct rbd_device *dev;
1327};
1328
1329static void rbd_notify_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
1330{
1331 struct rbd_device *dev = (struct rbd_device *)data;
1332 if (!dev)
1333 return;
1334
1335 dout("rbd_notify_cb %s notify_id=%lld opcode=%d\n", dev->obj_md_name,
1336 notify_id, (int)opcode);
1337}
1338
1339/*
1340 * Request sync osd notify
1341 */
1342static int rbd_req_sync_notify(struct rbd_device *dev,
1343 const char *obj)
1344{
1345 struct ceph_osd_req_op *ops;
Alex Elder1dbb4392012-01-24 10:08:37 -06001346 struct ceph_osd_client *osdc = &dev->rbd_client->client->osdc;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001347 struct ceph_osd_event *event;
1348 struct rbd_notify_info info;
1349 int payload_len = sizeof(u32) + sizeof(u32);
1350 int ret;
1351
1352 ret = rbd_create_rw_ops(&ops, 1, CEPH_OSD_OP_NOTIFY, payload_len);
1353 if (ret < 0)
1354 return ret;
1355
1356 info.dev = dev;
1357
1358 ret = ceph_osdc_create_event(osdc, rbd_notify_cb, 1,
1359 (void *)&info, &event);
1360 if (ret < 0)
1361 goto fail;
1362
1363 ops[0].watch.ver = 1;
1364 ops[0].watch.flag = 1;
1365 ops[0].watch.cookie = event->cookie;
1366 ops[0].watch.prot_ver = RADOS_NOTIFY_VER;
1367 ops[0].watch.timeout = 12;
1368
1369 ret = rbd_req_sync_op(dev, NULL,
1370 CEPH_NOSNAP,
1371 0,
1372 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1373 ops,
1374 1, obj, 0, 0, NULL, NULL, NULL);
1375 if (ret < 0)
1376 goto fail_event;
1377
1378 ret = ceph_osdc_wait_event(event, CEPH_OSD_TIMEOUT_DEFAULT);
1379 dout("ceph_osdc_wait_event returned %d\n", ret);
1380 rbd_destroy_ops(ops);
1381 return 0;
1382
1383fail_event:
1384 ceph_osdc_cancel_event(event);
1385fail:
1386 rbd_destroy_ops(ops);
1387 return ret;
1388}
1389
1390/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001391 * Request sync osd read
1392 */
1393static int rbd_req_sync_exec(struct rbd_device *dev,
1394 const char *obj,
1395 const char *cls,
1396 const char *method,
1397 const char *data,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001398 int len,
1399 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001400{
1401 struct ceph_osd_req_op *ops;
1402 int cls_len = strlen(cls);
1403 int method_len = strlen(method);
1404 int ret = rbd_create_rw_ops(&ops, 1, CEPH_OSD_OP_CALL,
1405 cls_len + method_len + len);
1406 if (ret < 0)
1407 return ret;
1408
1409 ops[0].cls.class_name = cls;
1410 ops[0].cls.class_len = (__u8)cls_len;
1411 ops[0].cls.method_name = method;
1412 ops[0].cls.method_len = (__u8)method_len;
1413 ops[0].cls.argc = 0;
1414 ops[0].cls.indata = data;
1415 ops[0].cls.indata_len = len;
1416
1417 ret = rbd_req_sync_op(dev, NULL,
1418 CEPH_NOSNAP,
1419 0,
1420 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1421 ops,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001422 1, obj, 0, 0, NULL, NULL, ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001423
1424 rbd_destroy_ops(ops);
1425
1426 dout("cls_exec returned %d\n", ret);
1427 return ret;
1428}
1429
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001430static struct rbd_req_coll *rbd_alloc_coll(int num_reqs)
1431{
1432 struct rbd_req_coll *coll =
1433 kzalloc(sizeof(struct rbd_req_coll) +
1434 sizeof(struct rbd_req_status) * num_reqs,
1435 GFP_ATOMIC);
1436
1437 if (!coll)
1438 return NULL;
1439 coll->total = num_reqs;
1440 kref_init(&coll->kref);
1441 return coll;
1442}
1443
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001444/*
1445 * block device queue callback
1446 */
1447static void rbd_rq_fn(struct request_queue *q)
1448{
1449 struct rbd_device *rbd_dev = q->queuedata;
1450 struct request *rq;
1451 struct bio_pair *bp = NULL;
1452
Alex Elder00f1f362012-02-07 12:03:36 -06001453 while ((rq = blk_fetch_request(q))) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001454 struct bio *bio;
1455 struct bio *rq_bio, *next_bio = NULL;
1456 bool do_write;
1457 int size, op_size = 0;
1458 u64 ofs;
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001459 int num_segs, cur_seg = 0;
1460 struct rbd_req_coll *coll;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001461
1462 /* peek at request from block layer */
1463 if (!rq)
1464 break;
1465
1466 dout("fetched request\n");
1467
1468 /* filter out block requests we don't understand */
1469 if ((rq->cmd_type != REQ_TYPE_FS)) {
1470 __blk_end_request_all(rq, 0);
Alex Elder00f1f362012-02-07 12:03:36 -06001471 continue;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001472 }
1473
1474 /* deduce our operation (read, write) */
1475 do_write = (rq_data_dir(rq) == WRITE);
1476
1477 size = blk_rq_bytes(rq);
Alex Elder593a9e72012-02-07 12:03:37 -06001478 ofs = blk_rq_pos(rq) * SECTOR_SIZE;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001479 rq_bio = rq->bio;
1480 if (do_write && rbd_dev->read_only) {
1481 __blk_end_request_all(rq, -EROFS);
Alex Elder00f1f362012-02-07 12:03:36 -06001482 continue;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001483 }
1484
1485 spin_unlock_irq(q->queue_lock);
1486
1487 dout("%s 0x%x bytes at 0x%llx\n",
1488 do_write ? "write" : "read",
Alex Elder593a9e72012-02-07 12:03:37 -06001489 size, blk_rq_pos(rq) * SECTOR_SIZE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001490
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001491 num_segs = rbd_get_num_segments(&rbd_dev->header, ofs, size);
1492 coll = rbd_alloc_coll(num_segs);
1493 if (!coll) {
1494 spin_lock_irq(q->queue_lock);
1495 __blk_end_request_all(rq, -ENOMEM);
Alex Elder00f1f362012-02-07 12:03:36 -06001496 continue;
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001497 }
1498
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001499 do {
1500 /* a bio clone to be passed down to OSD req */
1501 dout("rq->bio->bi_vcnt=%d\n", rq->bio->bi_vcnt);
1502 op_size = rbd_get_segment(&rbd_dev->header,
1503 rbd_dev->header.block_name,
1504 ofs, size,
1505 NULL, NULL);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001506 kref_get(&coll->kref);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001507 bio = bio_chain_clone(&rq_bio, &next_bio, &bp,
1508 op_size, GFP_ATOMIC);
1509 if (!bio) {
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001510 rbd_coll_end_req_index(rq, coll, cur_seg,
1511 -ENOMEM, op_size);
1512 goto next_seg;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001513 }
1514
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001515
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001516 /* init OSD command: write or read */
1517 if (do_write)
1518 rbd_req_write(rq, rbd_dev,
1519 rbd_dev->header.snapc,
1520 ofs,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001521 op_size, bio,
1522 coll, cur_seg);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001523 else
1524 rbd_req_read(rq, rbd_dev,
1525 cur_snap_id(rbd_dev),
1526 ofs,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001527 op_size, bio,
1528 coll, cur_seg);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001529
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001530next_seg:
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001531 size -= op_size;
1532 ofs += op_size;
1533
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001534 cur_seg++;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001535 rq_bio = next_bio;
1536 } while (size > 0);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001537 kref_put(&coll->kref, rbd_coll_release);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001538
1539 if (bp)
1540 bio_pair_release(bp);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001541 spin_lock_irq(q->queue_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001542 }
1543}
1544
1545/*
1546 * a queue callback. Makes sure that we don't create a bio that spans across
1547 * multiple osd objects. One exception would be with a single page bios,
1548 * which we handle later at bio_chain_clone
1549 */
1550static int rbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd,
1551 struct bio_vec *bvec)
1552{
1553 struct rbd_device *rbd_dev = q->queuedata;
Alex Elder593a9e72012-02-07 12:03:37 -06001554 unsigned int chunk_sectors;
1555 sector_t sector;
1556 unsigned int bio_sectors;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001557 int max;
1558
Alex Elder593a9e72012-02-07 12:03:37 -06001559 chunk_sectors = 1 << (rbd_dev->header.obj_order - SECTOR_SHIFT);
1560 sector = bmd->bi_sector + get_start_sect(bmd->bi_bdev);
1561 bio_sectors = bmd->bi_size >> SECTOR_SHIFT;
1562
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001563 max = (chunk_sectors - ((sector & (chunk_sectors - 1))
Alex Elder593a9e72012-02-07 12:03:37 -06001564 + bio_sectors)) << SECTOR_SHIFT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001565 if (max < 0)
1566 max = 0; /* bio_add cannot handle a negative return */
1567 if (max <= bvec->bv_len && bio_sectors == 0)
1568 return bvec->bv_len;
1569 return max;
1570}
1571
1572static void rbd_free_disk(struct rbd_device *rbd_dev)
1573{
1574 struct gendisk *disk = rbd_dev->disk;
1575
1576 if (!disk)
1577 return;
1578
1579 rbd_header_free(&rbd_dev->header);
1580
1581 if (disk->flags & GENHD_FL_UP)
1582 del_gendisk(disk);
1583 if (disk->queue)
1584 blk_cleanup_queue(disk->queue);
1585 put_disk(disk);
1586}
1587
1588/*
1589 * reload the ondisk the header
1590 */
1591static int rbd_read_header(struct rbd_device *rbd_dev,
1592 struct rbd_image_header *header)
1593{
1594 ssize_t rc;
1595 struct rbd_image_header_ondisk *dh;
Xi Wang50f7c4c2012-04-20 15:49:44 -05001596 u32 snap_count = 0;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001597 u64 ver;
Alex Elder00f1f362012-02-07 12:03:36 -06001598 size_t len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001599
Alex Elder00f1f362012-02-07 12:03:36 -06001600 /*
1601 * First reads the fixed-size header to determine the number
1602 * of snapshots, then re-reads it, along with all snapshot
1603 * records as well as their stored names.
1604 */
1605 len = sizeof (*dh);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001606 while (1) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001607 dh = kmalloc(len, GFP_KERNEL);
1608 if (!dh)
1609 return -ENOMEM;
1610
1611 rc = rbd_req_sync_read(rbd_dev,
1612 NULL, CEPH_NOSNAP,
1613 rbd_dev->obj_md_name,
1614 0, len,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001615 (char *)dh, &ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001616 if (rc < 0)
1617 goto out_dh;
1618
1619 rc = rbd_header_from_disk(header, dh, snap_count, GFP_KERNEL);
Josh Durgin81e759f2011-11-15 14:49:53 -08001620 if (rc < 0) {
Alex Elder00f1f362012-02-07 12:03:36 -06001621 if (rc == -ENXIO)
Josh Durgin81e759f2011-11-15 14:49:53 -08001622 pr_warning("unrecognized header format"
1623 " for image %s", rbd_dev->obj);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001624 goto out_dh;
Josh Durgin81e759f2011-11-15 14:49:53 -08001625 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001626
Alex Elder00f1f362012-02-07 12:03:36 -06001627 if (snap_count == header->total_snaps)
1628 break;
1629
1630 snap_count = header->total_snaps;
1631 len = sizeof (*dh) +
1632 snap_count * sizeof(struct rbd_image_snap_ondisk) +
1633 header->snap_names_len;
1634
1635 rbd_header_free(header);
1636 kfree(dh);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001637 }
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001638 header->obj_version = ver;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001639
1640out_dh:
1641 kfree(dh);
1642 return rc;
1643}
1644
1645/*
1646 * create a snapshot
1647 */
1648static int rbd_header_add_snap(struct rbd_device *dev,
1649 const char *snap_name,
1650 gfp_t gfp_flags)
1651{
1652 int name_len = strlen(snap_name);
1653 u64 new_snapid;
1654 int ret;
Sage Weil916d4d62011-05-12 16:10:50 -07001655 void *data, *p, *e;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001656 u64 ver;
Alex Elder1dbb4392012-01-24 10:08:37 -06001657 struct ceph_mon_client *monc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001658
1659 /* we should create a snapshot only if we're pointing at the head */
1660 if (dev->cur_snap)
1661 return -EINVAL;
1662
Alex Elder1dbb4392012-01-24 10:08:37 -06001663 monc = &dev->rbd_client->client->monc;
1664 ret = ceph_monc_create_snapid(monc, dev->poolid, &new_snapid);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001665 dout("created snapid=%lld\n", new_snapid);
1666 if (ret < 0)
1667 return ret;
1668
1669 data = kmalloc(name_len + 16, gfp_flags);
1670 if (!data)
1671 return -ENOMEM;
1672
Sage Weil916d4d62011-05-12 16:10:50 -07001673 p = data;
1674 e = data + name_len + 16;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001675
Sage Weil916d4d62011-05-12 16:10:50 -07001676 ceph_encode_string_safe(&p, e, snap_name, name_len, bad);
1677 ceph_encode_64_safe(&p, e, new_snapid, bad);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001678
1679 ret = rbd_req_sync_exec(dev, dev->obj_md_name, "rbd", "snap_add",
Sage Weil916d4d62011-05-12 16:10:50 -07001680 data, p - data, &ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001681
Sage Weil916d4d62011-05-12 16:10:50 -07001682 kfree(data);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001683
1684 if (ret < 0)
1685 return ret;
1686
Josh Durgin403f24d2011-12-05 10:47:13 -08001687 down_write(&dev->header_rwsem);
1688 dev->header.snapc->seq = new_snapid;
1689 up_write(&dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001690
1691 return 0;
1692bad:
1693 return -ERANGE;
1694}
1695
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001696static void __rbd_remove_all_snaps(struct rbd_device *rbd_dev)
1697{
1698 struct rbd_snap *snap;
1699
1700 while (!list_empty(&rbd_dev->snaps)) {
1701 snap = list_first_entry(&rbd_dev->snaps, struct rbd_snap, node);
1702 __rbd_remove_snap_dev(rbd_dev, snap);
1703 }
1704}
1705
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001706/*
1707 * only read the first part of the ondisk header, without the snaps info
1708 */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001709static int __rbd_update_snaps(struct rbd_device *rbd_dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001710{
1711 int ret;
1712 struct rbd_image_header h;
1713 u64 snap_seq;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001714 int follow_seq = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001715
1716 ret = rbd_read_header(rbd_dev, &h);
1717 if (ret < 0)
1718 return ret;
1719
Sage Weil9db4b3e2011-04-19 22:49:06 -07001720 /* resized? */
Alex Elder593a9e72012-02-07 12:03:37 -06001721 set_capacity(rbd_dev->disk, h.image_size / SECTOR_SIZE);
Sage Weil9db4b3e2011-04-19 22:49:06 -07001722
Josh Durginc6666012011-11-21 17:11:12 -08001723 down_write(&rbd_dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001724
1725 snap_seq = rbd_dev->header.snapc->seq;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001726 if (rbd_dev->header.total_snaps &&
1727 rbd_dev->header.snapc->snaps[0] == snap_seq)
1728 /* pointing at the head, will need to follow that
1729 if head moves */
1730 follow_seq = 1;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001731
1732 kfree(rbd_dev->header.snapc);
1733 kfree(rbd_dev->header.snap_names);
1734 kfree(rbd_dev->header.snap_sizes);
1735
1736 rbd_dev->header.total_snaps = h.total_snaps;
1737 rbd_dev->header.snapc = h.snapc;
1738 rbd_dev->header.snap_names = h.snap_names;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001739 rbd_dev->header.snap_names_len = h.snap_names_len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001740 rbd_dev->header.snap_sizes = h.snap_sizes;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001741 if (follow_seq)
1742 rbd_dev->header.snapc->seq = rbd_dev->header.snapc->snaps[0];
1743 else
1744 rbd_dev->header.snapc->seq = snap_seq;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001745
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001746 ret = __rbd_init_snaps_header(rbd_dev);
1747
Josh Durginc6666012011-11-21 17:11:12 -08001748 up_write(&rbd_dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001749
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001750 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001751}
1752
1753static int rbd_init_disk(struct rbd_device *rbd_dev)
1754{
1755 struct gendisk *disk;
1756 struct request_queue *q;
1757 int rc;
Alex Elder593a9e72012-02-07 12:03:37 -06001758 u64 segment_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001759 u64 total_size = 0;
1760
1761 /* contact OSD, request size info about the object being mapped */
1762 rc = rbd_read_header(rbd_dev, &rbd_dev->header);
1763 if (rc)
1764 return rc;
1765
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001766 /* no need to lock here, as rbd_dev is not registered yet */
1767 rc = __rbd_init_snaps_header(rbd_dev);
1768 if (rc)
1769 return rc;
1770
Josh Durgincc9d7342011-11-21 18:19:13 -08001771 rc = rbd_header_set_snap(rbd_dev, &total_size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001772 if (rc)
1773 return rc;
1774
1775 /* create gendisk info */
1776 rc = -ENOMEM;
1777 disk = alloc_disk(RBD_MINORS_PER_MAJOR);
1778 if (!disk)
1779 goto out;
1780
Alex Elderf0f8cef2012-01-29 13:57:44 -06001781 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
Sage Weilaedfec52011-05-12 20:57:03 -07001782 rbd_dev->id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001783 disk->major = rbd_dev->major;
1784 disk->first_minor = 0;
1785 disk->fops = &rbd_bd_ops;
1786 disk->private_data = rbd_dev;
1787
1788 /* init rq */
1789 rc = -ENOMEM;
1790 q = blk_init_queue(rbd_rq_fn, &rbd_dev->lock);
1791 if (!q)
1792 goto out_disk;
Josh Durgin029bcbd2011-07-22 11:35:23 -07001793
Alex Elder593a9e72012-02-07 12:03:37 -06001794 /* We use the default size, but let's be explicit about it. */
1795 blk_queue_physical_block_size(q, SECTOR_SIZE);
1796
Josh Durgin029bcbd2011-07-22 11:35:23 -07001797 /* set io sizes to object size */
Alex Elder593a9e72012-02-07 12:03:37 -06001798 segment_size = rbd_obj_bytes(&rbd_dev->header);
1799 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
1800 blk_queue_max_segment_size(q, segment_size);
1801 blk_queue_io_min(q, segment_size);
1802 blk_queue_io_opt(q, segment_size);
Josh Durgin029bcbd2011-07-22 11:35:23 -07001803
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001804 blk_queue_merge_bvec(q, rbd_merge_bvec);
1805 disk->queue = q;
1806
1807 q->queuedata = rbd_dev;
1808
1809 rbd_dev->disk = disk;
1810 rbd_dev->q = q;
1811
1812 /* finally, announce the disk to the world */
Alex Elder593a9e72012-02-07 12:03:37 -06001813 set_capacity(disk, total_size / SECTOR_SIZE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001814 add_disk(disk);
1815
1816 pr_info("%s: added with size 0x%llx\n",
1817 disk->disk_name, (unsigned long long)total_size);
1818 return 0;
1819
1820out_disk:
1821 put_disk(disk);
1822out:
1823 return rc;
1824}
1825
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001826/*
1827 sysfs
1828*/
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001829
Alex Elder593a9e72012-02-07 12:03:37 -06001830static struct rbd_device *dev_to_rbd_dev(struct device *dev)
1831{
1832 return container_of(dev, struct rbd_device, dev);
1833}
1834
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001835static ssize_t rbd_size_show(struct device *dev,
1836 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001837{
Alex Elder593a9e72012-02-07 12:03:37 -06001838 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001839
1840 return sprintf(buf, "%llu\n", (unsigned long long)rbd_dev->header.image_size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001841}
1842
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001843static ssize_t rbd_major_show(struct device *dev,
1844 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001845{
Alex Elder593a9e72012-02-07 12:03:37 -06001846 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001847
1848 return sprintf(buf, "%d\n", rbd_dev->major);
1849}
1850
1851static ssize_t rbd_client_id_show(struct device *dev,
1852 struct device_attribute *attr, char *buf)
1853{
Alex Elder593a9e72012-02-07 12:03:37 -06001854 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001855
Alex Elder1dbb4392012-01-24 10:08:37 -06001856 return sprintf(buf, "client%lld\n",
1857 ceph_client_id(rbd_dev->rbd_client->client));
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001858}
1859
1860static ssize_t rbd_pool_show(struct device *dev,
1861 struct device_attribute *attr, char *buf)
1862{
Alex Elder593a9e72012-02-07 12:03:37 -06001863 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001864
1865 return sprintf(buf, "%s\n", rbd_dev->pool_name);
1866}
1867
1868static ssize_t rbd_name_show(struct device *dev,
1869 struct device_attribute *attr, char *buf)
1870{
Alex Elder593a9e72012-02-07 12:03:37 -06001871 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001872
1873 return sprintf(buf, "%s\n", rbd_dev->obj);
1874}
1875
1876static ssize_t rbd_snap_show(struct device *dev,
1877 struct device_attribute *attr,
1878 char *buf)
1879{
Alex Elder593a9e72012-02-07 12:03:37 -06001880 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001881
1882 return sprintf(buf, "%s\n", rbd_dev->snap_name);
1883}
1884
1885static ssize_t rbd_image_refresh(struct device *dev,
1886 struct device_attribute *attr,
1887 const char *buf,
1888 size_t size)
1889{
Alex Elder593a9e72012-02-07 12:03:37 -06001890 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001891 int rc;
1892 int ret = size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001893
1894 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
1895
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001896 rc = __rbd_update_snaps(rbd_dev);
1897 if (rc < 0)
1898 ret = rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001899
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001900 mutex_unlock(&ctl_mutex);
1901 return ret;
1902}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001903
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001904static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
1905static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
1906static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
1907static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
1908static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
1909static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
1910static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
1911static DEVICE_ATTR(create_snap, S_IWUSR, NULL, rbd_snap_add);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001912
1913static struct attribute *rbd_attrs[] = {
1914 &dev_attr_size.attr,
1915 &dev_attr_major.attr,
1916 &dev_attr_client_id.attr,
1917 &dev_attr_pool.attr,
1918 &dev_attr_name.attr,
1919 &dev_attr_current_snap.attr,
1920 &dev_attr_refresh.attr,
1921 &dev_attr_create_snap.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001922 NULL
1923};
1924
1925static struct attribute_group rbd_attr_group = {
1926 .attrs = rbd_attrs,
1927};
1928
1929static const struct attribute_group *rbd_attr_groups[] = {
1930 &rbd_attr_group,
1931 NULL
1932};
1933
1934static void rbd_sysfs_dev_release(struct device *dev)
1935{
1936}
1937
1938static struct device_type rbd_device_type = {
1939 .name = "rbd",
1940 .groups = rbd_attr_groups,
1941 .release = rbd_sysfs_dev_release,
1942};
1943
1944
1945/*
1946 sysfs - snapshots
1947*/
1948
1949static ssize_t rbd_snap_size_show(struct device *dev,
1950 struct device_attribute *attr,
1951 char *buf)
1952{
1953 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
1954
Alex Elder593a9e72012-02-07 12:03:37 -06001955 return sprintf(buf, "%zd\n", snap->size);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001956}
1957
1958static ssize_t rbd_snap_id_show(struct device *dev,
1959 struct device_attribute *attr,
1960 char *buf)
1961{
1962 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
1963
Alex Elder593a9e72012-02-07 12:03:37 -06001964 return sprintf(buf, "%llu\n", (unsigned long long) snap->id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001965}
1966
1967static DEVICE_ATTR(snap_size, S_IRUGO, rbd_snap_size_show, NULL);
1968static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL);
1969
1970static struct attribute *rbd_snap_attrs[] = {
1971 &dev_attr_snap_size.attr,
1972 &dev_attr_snap_id.attr,
1973 NULL,
1974};
1975
1976static struct attribute_group rbd_snap_attr_group = {
1977 .attrs = rbd_snap_attrs,
1978};
1979
1980static void rbd_snap_dev_release(struct device *dev)
1981{
1982 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
1983 kfree(snap->name);
1984 kfree(snap);
1985}
1986
1987static const struct attribute_group *rbd_snap_attr_groups[] = {
1988 &rbd_snap_attr_group,
1989 NULL
1990};
1991
1992static struct device_type rbd_snap_device_type = {
1993 .groups = rbd_snap_attr_groups,
1994 .release = rbd_snap_dev_release,
1995};
1996
1997static void __rbd_remove_snap_dev(struct rbd_device *rbd_dev,
1998 struct rbd_snap *snap)
1999{
2000 list_del(&snap->node);
2001 device_unregister(&snap->dev);
2002}
2003
2004static int rbd_register_snap_dev(struct rbd_device *rbd_dev,
2005 struct rbd_snap *snap,
2006 struct device *parent)
2007{
2008 struct device *dev = &snap->dev;
2009 int ret;
2010
2011 dev->type = &rbd_snap_device_type;
2012 dev->parent = parent;
2013 dev->release = rbd_snap_dev_release;
2014 dev_set_name(dev, "snap_%s", snap->name);
2015 ret = device_register(dev);
2016
2017 return ret;
2018}
2019
2020static int __rbd_add_snap_dev(struct rbd_device *rbd_dev,
2021 int i, const char *name,
2022 struct rbd_snap **snapp)
2023{
2024 int ret;
2025 struct rbd_snap *snap = kzalloc(sizeof(*snap), GFP_KERNEL);
2026 if (!snap)
2027 return -ENOMEM;
2028 snap->name = kstrdup(name, GFP_KERNEL);
2029 snap->size = rbd_dev->header.snap_sizes[i];
2030 snap->id = rbd_dev->header.snapc->snaps[i];
2031 if (device_is_registered(&rbd_dev->dev)) {
2032 ret = rbd_register_snap_dev(rbd_dev, snap,
2033 &rbd_dev->dev);
2034 if (ret < 0)
2035 goto err;
2036 }
2037 *snapp = snap;
2038 return 0;
2039err:
2040 kfree(snap->name);
2041 kfree(snap);
2042 return ret;
2043}
2044
2045/*
2046 * search for the previous snap in a null delimited string list
2047 */
2048const char *rbd_prev_snap_name(const char *name, const char *start)
2049{
2050 if (name < start + 2)
2051 return NULL;
2052
2053 name -= 2;
2054 while (*name) {
2055 if (name == start)
2056 return start;
2057 name--;
2058 }
2059 return name + 1;
2060}
2061
2062/*
2063 * compare the old list of snapshots that we have to what's in the header
2064 * and update it accordingly. Note that the header holds the snapshots
2065 * in a reverse order (from newest to oldest) and we need to go from
2066 * older to new so that we don't get a duplicate snap name when
2067 * doing the process (e.g., removed snapshot and recreated a new
2068 * one with the same name.
2069 */
2070static int __rbd_init_snaps_header(struct rbd_device *rbd_dev)
2071{
2072 const char *name, *first_name;
2073 int i = rbd_dev->header.total_snaps;
2074 struct rbd_snap *snap, *old_snap = NULL;
2075 int ret;
2076 struct list_head *p, *n;
2077
2078 first_name = rbd_dev->header.snap_names;
2079 name = first_name + rbd_dev->header.snap_names_len;
2080
2081 list_for_each_prev_safe(p, n, &rbd_dev->snaps) {
2082 u64 cur_id;
2083
2084 old_snap = list_entry(p, struct rbd_snap, node);
2085
2086 if (i)
2087 cur_id = rbd_dev->header.snapc->snaps[i - 1];
2088
2089 if (!i || old_snap->id < cur_id) {
2090 /* old_snap->id was skipped, thus was removed */
2091 __rbd_remove_snap_dev(rbd_dev, old_snap);
2092 continue;
2093 }
2094 if (old_snap->id == cur_id) {
2095 /* we have this snapshot already */
2096 i--;
2097 name = rbd_prev_snap_name(name, first_name);
2098 continue;
2099 }
2100 for (; i > 0;
2101 i--, name = rbd_prev_snap_name(name, first_name)) {
2102 if (!name) {
2103 WARN_ON(1);
2104 return -EINVAL;
2105 }
2106 cur_id = rbd_dev->header.snapc->snaps[i];
2107 /* snapshot removal? handle it above */
2108 if (cur_id >= old_snap->id)
2109 break;
2110 /* a new snapshot */
2111 ret = __rbd_add_snap_dev(rbd_dev, i - 1, name, &snap);
2112 if (ret < 0)
2113 return ret;
2114
2115 /* note that we add it backward so using n and not p */
2116 list_add(&snap->node, n);
2117 p = &snap->node;
2118 }
2119 }
2120 /* we're done going over the old snap list, just add what's left */
2121 for (; i > 0; i--) {
2122 name = rbd_prev_snap_name(name, first_name);
2123 if (!name) {
2124 WARN_ON(1);
2125 return -EINVAL;
2126 }
2127 ret = __rbd_add_snap_dev(rbd_dev, i - 1, name, &snap);
2128 if (ret < 0)
2129 return ret;
2130 list_add(&snap->node, &rbd_dev->snaps);
2131 }
2132
2133 return 0;
2134}
2135
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002136static int rbd_bus_add_dev(struct rbd_device *rbd_dev)
2137{
Alex Elderf0f8cef2012-01-29 13:57:44 -06002138 int ret;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002139 struct device *dev;
2140 struct rbd_snap *snap;
2141
2142 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2143 dev = &rbd_dev->dev;
2144
2145 dev->bus = &rbd_bus_type;
2146 dev->type = &rbd_device_type;
2147 dev->parent = &rbd_root_dev;
2148 dev->release = rbd_dev_release;
2149 dev_set_name(dev, "%d", rbd_dev->id);
2150 ret = device_register(dev);
2151 if (ret < 0)
Alex Elderf0f8cef2012-01-29 13:57:44 -06002152 goto out;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002153
2154 list_for_each_entry(snap, &rbd_dev->snaps, node) {
2155 ret = rbd_register_snap_dev(rbd_dev, snap,
2156 &rbd_dev->dev);
2157 if (ret < 0)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002158 break;
2159 }
Alex Elderf0f8cef2012-01-29 13:57:44 -06002160out:
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002161 mutex_unlock(&ctl_mutex);
2162 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002163}
2164
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002165static void rbd_bus_del_dev(struct rbd_device *rbd_dev)
2166{
2167 device_unregister(&rbd_dev->dev);
2168}
2169
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002170static int rbd_init_watch_dev(struct rbd_device *rbd_dev)
2171{
2172 int ret, rc;
2173
2174 do {
2175 ret = rbd_req_sync_watch(rbd_dev, rbd_dev->obj_md_name,
2176 rbd_dev->header.obj_version);
2177 if (ret == -ERANGE) {
2178 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2179 rc = __rbd_update_snaps(rbd_dev);
2180 mutex_unlock(&ctl_mutex);
2181 if (rc < 0)
2182 return rc;
2183 }
2184 } while (ret == -ERANGE);
2185
2186 return ret;
2187}
2188
Alex Elder1ddbe942012-01-29 13:57:44 -06002189static atomic64_t rbd_id_max = ATOMIC64_INIT(0);
2190
2191/*
Alex Elder499afd52012-02-02 08:13:29 -06002192 * Get a unique rbd identifier for the given new rbd_dev, and add
2193 * the rbd_dev to the global list. The minimum rbd id is 1.
Alex Elder1ddbe942012-01-29 13:57:44 -06002194 */
Alex Elder499afd52012-02-02 08:13:29 -06002195static void rbd_id_get(struct rbd_device *rbd_dev)
Alex Elderb7f23c32012-01-29 13:57:43 -06002196{
Alex Elder499afd52012-02-02 08:13:29 -06002197 rbd_dev->id = atomic64_inc_return(&rbd_id_max);
2198
2199 spin_lock(&rbd_dev_list_lock);
2200 list_add_tail(&rbd_dev->node, &rbd_dev_list);
2201 spin_unlock(&rbd_dev_list_lock);
Alex Elder1ddbe942012-01-29 13:57:44 -06002202}
Alex Elderb7f23c32012-01-29 13:57:43 -06002203
Alex Elder1ddbe942012-01-29 13:57:44 -06002204/*
Alex Elder499afd52012-02-02 08:13:29 -06002205 * Remove an rbd_dev from the global list, and record that its
2206 * identifier is no longer in use.
Alex Elder1ddbe942012-01-29 13:57:44 -06002207 */
Alex Elder499afd52012-02-02 08:13:29 -06002208static void rbd_id_put(struct rbd_device *rbd_dev)
Alex Elder1ddbe942012-01-29 13:57:44 -06002209{
Alex Elderd184f6b2012-01-29 13:57:44 -06002210 struct list_head *tmp;
2211 int rbd_id = rbd_dev->id;
2212 int max_id;
2213
2214 BUG_ON(rbd_id < 1);
Alex Elder499afd52012-02-02 08:13:29 -06002215
2216 spin_lock(&rbd_dev_list_lock);
2217 list_del_init(&rbd_dev->node);
Alex Elderd184f6b2012-01-29 13:57:44 -06002218
2219 /*
2220 * If the id being "put" is not the current maximum, there
2221 * is nothing special we need to do.
2222 */
2223 if (rbd_id != atomic64_read(&rbd_id_max)) {
2224 spin_unlock(&rbd_dev_list_lock);
2225 return;
2226 }
2227
2228 /*
2229 * We need to update the current maximum id. Search the
2230 * list to find out what it is. We're more likely to find
2231 * the maximum at the end, so search the list backward.
2232 */
2233 max_id = 0;
2234 list_for_each_prev(tmp, &rbd_dev_list) {
2235 struct rbd_device *rbd_dev;
2236
2237 rbd_dev = list_entry(tmp, struct rbd_device, node);
2238 if (rbd_id > max_id)
2239 max_id = rbd_id;
2240 }
Alex Elder499afd52012-02-02 08:13:29 -06002241 spin_unlock(&rbd_dev_list_lock);
Alex Elderb7f23c32012-01-29 13:57:43 -06002242
Alex Elder1ddbe942012-01-29 13:57:44 -06002243 /*
Alex Elderd184f6b2012-01-29 13:57:44 -06002244 * The max id could have been updated by rbd_id_get(), in
2245 * which case it now accurately reflects the new maximum.
2246 * Be careful not to overwrite the maximum value in that
2247 * case.
Alex Elder1ddbe942012-01-29 13:57:44 -06002248 */
Alex Elderd184f6b2012-01-29 13:57:44 -06002249 atomic64_cmpxchg(&rbd_id_max, rbd_id, max_id);
Alex Elderb7f23c32012-01-29 13:57:43 -06002250}
2251
Alex Eldera725f65e2012-02-02 08:13:30 -06002252/*
Alex Eldere28fff262012-02-02 08:13:30 -06002253 * Skips over white space at *buf, and updates *buf to point to the
2254 * first found non-space character (if any). Returns the length of
Alex Elder593a9e72012-02-07 12:03:37 -06002255 * the token (string of non-white space characters) found. Note
2256 * that *buf must be terminated with '\0'.
Alex Eldere28fff262012-02-02 08:13:30 -06002257 */
2258static inline size_t next_token(const char **buf)
2259{
2260 /*
2261 * These are the characters that produce nonzero for
2262 * isspace() in the "C" and "POSIX" locales.
2263 */
2264 const char *spaces = " \f\n\r\t\v";
2265
2266 *buf += strspn(*buf, spaces); /* Find start of token */
2267
2268 return strcspn(*buf, spaces); /* Return token length */
2269}
2270
2271/*
2272 * Finds the next token in *buf, and if the provided token buffer is
2273 * big enough, copies the found token into it. The result, if
Alex Elder593a9e72012-02-07 12:03:37 -06002274 * copied, is guaranteed to be terminated with '\0'. Note that *buf
2275 * must be terminated with '\0' on entry.
Alex Eldere28fff262012-02-02 08:13:30 -06002276 *
2277 * Returns the length of the token found (not including the '\0').
2278 * Return value will be 0 if no token is found, and it will be >=
2279 * token_size if the token would not fit.
2280 *
Alex Elder593a9e72012-02-07 12:03:37 -06002281 * The *buf pointer will be updated to point beyond the end of the
Alex Eldere28fff262012-02-02 08:13:30 -06002282 * found token. Note that this occurs even if the token buffer is
2283 * too small to hold it.
2284 */
2285static inline size_t copy_token(const char **buf,
2286 char *token,
2287 size_t token_size)
2288{
2289 size_t len;
2290
2291 len = next_token(buf);
2292 if (len < token_size) {
2293 memcpy(token, *buf, len);
2294 *(token + len) = '\0';
2295 }
2296 *buf += len;
2297
2298 return len;
2299}
2300
2301/*
Alex Eldera725f65e2012-02-02 08:13:30 -06002302 * This fills in the pool_name, obj, obj_len, snap_name, obj_len,
2303 * rbd_dev, rbd_md_name, and name fields of the given rbd_dev, based
2304 * on the list of monitor addresses and other options provided via
2305 * /sys/bus/rbd/add.
2306 */
2307static int rbd_add_parse_args(struct rbd_device *rbd_dev,
2308 const char *buf,
Alex Elder7ef32142012-02-02 08:13:30 -06002309 const char **mon_addrs,
Alex Elder5214ecc2012-02-02 08:13:30 -06002310 size_t *mon_addrs_size,
Alex Eldere28fff262012-02-02 08:13:30 -06002311 char *options,
2312 size_t options_size)
Alex Eldera725f65e2012-02-02 08:13:30 -06002313{
Alex Eldere28fff262012-02-02 08:13:30 -06002314 size_t len;
2315
2316 /* The first four tokens are required */
2317
Alex Elder7ef32142012-02-02 08:13:30 -06002318 len = next_token(&buf);
2319 if (!len)
Alex Eldera725f65e2012-02-02 08:13:30 -06002320 return -EINVAL;
Alex Elder5214ecc2012-02-02 08:13:30 -06002321 *mon_addrs_size = len + 1;
Alex Elder7ef32142012-02-02 08:13:30 -06002322 *mon_addrs = buf;
2323
2324 buf += len;
Alex Eldera725f65e2012-02-02 08:13:30 -06002325
Alex Eldere28fff262012-02-02 08:13:30 -06002326 len = copy_token(&buf, options, options_size);
2327 if (!len || len >= options_size)
2328 return -EINVAL;
Alex Eldera725f65e2012-02-02 08:13:30 -06002329
Alex Eldere28fff262012-02-02 08:13:30 -06002330 len = copy_token(&buf, rbd_dev->pool_name, sizeof (rbd_dev->pool_name));
2331 if (!len || len >= sizeof (rbd_dev->pool_name))
2332 return -EINVAL;
2333
2334 len = copy_token(&buf, rbd_dev->obj, sizeof (rbd_dev->obj));
2335 if (!len || len >= sizeof (rbd_dev->obj))
2336 return -EINVAL;
2337
2338 /* We have the object length in hand, save it. */
2339
2340 rbd_dev->obj_len = len;
2341
Alex Elder81a89792012-02-02 08:13:30 -06002342 BUILD_BUG_ON(RBD_MAX_MD_NAME_LEN
2343 < RBD_MAX_OBJ_NAME_LEN + sizeof (RBD_SUFFIX));
2344 sprintf(rbd_dev->obj_md_name, "%s%s", rbd_dev->obj, RBD_SUFFIX);
Alex Eldera725f65e2012-02-02 08:13:30 -06002345
Alex Eldere28fff262012-02-02 08:13:30 -06002346 /*
2347 * The snapshot name is optional, but it's an error if it's
2348 * too long. If no snapshot is supplied, fill in the default.
2349 */
2350 len = copy_token(&buf, rbd_dev->snap_name, sizeof (rbd_dev->snap_name));
2351 if (!len)
2352 memcpy(rbd_dev->snap_name, RBD_SNAP_HEAD_NAME,
2353 sizeof (RBD_SNAP_HEAD_NAME));
2354 else if (len >= sizeof (rbd_dev->snap_name))
2355 return -EINVAL;
2356
Alex Eldera725f65e2012-02-02 08:13:30 -06002357 return 0;
2358}
2359
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002360static ssize_t rbd_add(struct bus_type *bus,
2361 const char *buf,
2362 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002363{
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002364 struct rbd_device *rbd_dev;
Alex Elder7ef32142012-02-02 08:13:30 -06002365 const char *mon_addrs = NULL;
2366 size_t mon_addrs_size = 0;
Alex Elder27cc2592012-02-02 08:13:30 -06002367 char *options = NULL;
2368 struct ceph_osd_client *osdc;
2369 int rc = -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002370
2371 if (!try_module_get(THIS_MODULE))
2372 return -ENODEV;
2373
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002374 rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL);
2375 if (!rbd_dev)
Alex Elder27cc2592012-02-02 08:13:30 -06002376 goto err_nomem;
Alex Elder27cc2592012-02-02 08:13:30 -06002377 options = kmalloc(count, GFP_KERNEL);
2378 if (!options)
2379 goto err_nomem;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002380
2381 /* static rbd_device initialization */
2382 spin_lock_init(&rbd_dev->lock);
2383 INIT_LIST_HEAD(&rbd_dev->node);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002384 INIT_LIST_HEAD(&rbd_dev->snaps);
Josh Durginc6666012011-11-21 17:11:12 -08002385 init_rwsem(&rbd_dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002386
Josh Durginc6666012011-11-21 17:11:12 -08002387 init_rwsem(&rbd_dev->header_rwsem);
Alex Elder0e805a12012-01-11 19:42:15 -08002388
Alex Elderd184f6b2012-01-29 13:57:44 -06002389 /* generate unique id: find highest unique id, add one */
Alex Elder499afd52012-02-02 08:13:29 -06002390 rbd_id_get(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002391
Alex Eldera725f65e2012-02-02 08:13:30 -06002392 /* Fill in the device name, now that we have its id. */
Alex Elder81a89792012-02-02 08:13:30 -06002393 BUILD_BUG_ON(DEV_NAME_LEN
2394 < sizeof (RBD_DRV_NAME) + MAX_INT_FORMAT_WIDTH);
2395 sprintf(rbd_dev->name, "%s%d", RBD_DRV_NAME, rbd_dev->id);
Alex Eldere124a822012-01-29 13:57:44 -06002396
Alex Eldera725f65e2012-02-02 08:13:30 -06002397 /* parse add command */
Alex Elder7ef32142012-02-02 08:13:30 -06002398 rc = rbd_add_parse_args(rbd_dev, buf, &mon_addrs, &mon_addrs_size,
Alex Eldere28fff262012-02-02 08:13:30 -06002399 options, count);
Alex Eldera725f65e2012-02-02 08:13:30 -06002400 if (rc)
2401 goto err_put_id;
2402
Alex Elder5214ecc2012-02-02 08:13:30 -06002403 rbd_dev->rbd_client = rbd_get_client(mon_addrs, mon_addrs_size - 1,
2404 options);
Alex Elderd720bcb2012-02-02 08:13:30 -06002405 if (IS_ERR(rbd_dev->rbd_client)) {
2406 rc = PTR_ERR(rbd_dev->rbd_client);
Alex Elderf0f8cef2012-01-29 13:57:44 -06002407 goto err_put_id;
Alex Elderd720bcb2012-02-02 08:13:30 -06002408 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002409
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002410 /* pick the pool */
Alex Elder1dbb4392012-01-24 10:08:37 -06002411 osdc = &rbd_dev->rbd_client->client->osdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002412 rc = ceph_pg_poolid_by_name(osdc->osdmap, rbd_dev->pool_name);
2413 if (rc < 0)
2414 goto err_out_client;
2415 rbd_dev->poolid = rc;
2416
2417 /* register our block device */
Alex Elder27cc2592012-02-02 08:13:30 -06002418 rc = register_blkdev(0, rbd_dev->name);
2419 if (rc < 0)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002420 goto err_out_client;
Alex Elder27cc2592012-02-02 08:13:30 -06002421 rbd_dev->major = rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002422
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002423 rc = rbd_bus_add_dev(rbd_dev);
2424 if (rc)
Yehuda Sadeh766fc432011-01-07 14:58:42 -08002425 goto err_out_blkdev;
2426
Alex Elder32eec682012-02-08 16:11:14 -06002427 /*
2428 * At this point cleanup in the event of an error is the job
2429 * of the sysfs code (initiated by rbd_bus_del_dev()).
2430 *
2431 * Set up and announce blkdev mapping.
2432 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002433 rc = rbd_init_disk(rbd_dev);
2434 if (rc)
Yehuda Sadeh766fc432011-01-07 14:58:42 -08002435 goto err_out_bus;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002436
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002437 rc = rbd_init_watch_dev(rbd_dev);
2438 if (rc)
2439 goto err_out_bus;
2440
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002441 return count;
2442
Yehuda Sadeh766fc432011-01-07 14:58:42 -08002443err_out_bus:
Yehuda Sadeh766fc432011-01-07 14:58:42 -08002444 /* this will also clean up rest of rbd_dev stuff */
2445
2446 rbd_bus_del_dev(rbd_dev);
2447 kfree(options);
Yehuda Sadeh766fc432011-01-07 14:58:42 -08002448 return rc;
2449
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002450err_out_blkdev:
2451 unregister_blkdev(rbd_dev->major, rbd_dev->name);
2452err_out_client:
2453 rbd_put_client(rbd_dev);
Alex Elderf0f8cef2012-01-29 13:57:44 -06002454err_put_id:
Alex Elder499afd52012-02-02 08:13:29 -06002455 rbd_id_put(rbd_dev);
Alex Elder27cc2592012-02-02 08:13:30 -06002456err_nomem:
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002457 kfree(options);
Alex Elder27cc2592012-02-02 08:13:30 -06002458 kfree(rbd_dev);
2459
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002460 dout("Error adding device %s\n", buf);
2461 module_put(THIS_MODULE);
Alex Elder27cc2592012-02-02 08:13:30 -06002462
2463 return (ssize_t) rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002464}
2465
2466static struct rbd_device *__rbd_get_dev(unsigned long id)
2467{
2468 struct list_head *tmp;
2469 struct rbd_device *rbd_dev;
2470
Alex Eldere124a822012-01-29 13:57:44 -06002471 spin_lock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002472 list_for_each(tmp, &rbd_dev_list) {
2473 rbd_dev = list_entry(tmp, struct rbd_device, node);
Alex Eldere124a822012-01-29 13:57:44 -06002474 if (rbd_dev->id == id) {
2475 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002476 return rbd_dev;
Alex Eldere124a822012-01-29 13:57:44 -06002477 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002478 }
Alex Eldere124a822012-01-29 13:57:44 -06002479 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002480 return NULL;
2481}
2482
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002483static void rbd_dev_release(struct device *dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002484{
Alex Elder593a9e72012-02-07 12:03:37 -06002485 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002486
Alex Elder1dbb4392012-01-24 10:08:37 -06002487 if (rbd_dev->watch_request) {
2488 struct ceph_client *client = rbd_dev->rbd_client->client;
2489
2490 ceph_osdc_unregister_linger_request(&client->osdc,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002491 rbd_dev->watch_request);
Alex Elder1dbb4392012-01-24 10:08:37 -06002492 }
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002493 if (rbd_dev->watch_event)
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07002494 rbd_req_sync_unwatch(rbd_dev, rbd_dev->obj_md_name);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002495
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002496 rbd_put_client(rbd_dev);
2497
2498 /* clean up and free blkdev */
2499 rbd_free_disk(rbd_dev);
2500 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Alex Elder32eec682012-02-08 16:11:14 -06002501
2502 /* done with the id, and with the rbd_dev */
2503 rbd_id_put(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002504 kfree(rbd_dev);
2505
2506 /* release module ref */
2507 module_put(THIS_MODULE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002508}
2509
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002510static ssize_t rbd_remove(struct bus_type *bus,
2511 const char *buf,
2512 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002513{
2514 struct rbd_device *rbd_dev = NULL;
2515 int target_id, rc;
2516 unsigned long ul;
2517 int ret = count;
2518
2519 rc = strict_strtoul(buf, 10, &ul);
2520 if (rc)
2521 return rc;
2522
2523 /* convert to int; abort if we lost anything in the conversion */
2524 target_id = (int) ul;
2525 if (target_id != ul)
2526 return -EINVAL;
2527
2528 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2529
2530 rbd_dev = __rbd_get_dev(target_id);
2531 if (!rbd_dev) {
2532 ret = -ENOENT;
2533 goto done;
2534 }
2535
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002536 __rbd_remove_all_snaps(rbd_dev);
2537 rbd_bus_del_dev(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002538
2539done:
2540 mutex_unlock(&ctl_mutex);
2541 return ret;
2542}
2543
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002544static ssize_t rbd_snap_add(struct device *dev,
2545 struct device_attribute *attr,
2546 const char *buf,
2547 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002548{
Alex Elder593a9e72012-02-07 12:03:37 -06002549 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002550 int ret;
2551 char *name = kmalloc(count + 1, GFP_KERNEL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002552 if (!name)
2553 return -ENOMEM;
2554
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002555 snprintf(name, count, "%s", buf);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002556
2557 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2558
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002559 ret = rbd_header_add_snap(rbd_dev,
2560 name, GFP_KERNEL);
2561 if (ret < 0)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002562 goto err_unlock;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002563
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002564 ret = __rbd_update_snaps(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002565 if (ret < 0)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002566 goto err_unlock;
2567
2568 /* shouldn't hold ctl_mutex when notifying.. notify might
2569 trigger a watch callback that would need to get that mutex */
2570 mutex_unlock(&ctl_mutex);
2571
2572 /* make a best effort, don't error if failed */
2573 rbd_req_sync_notify(rbd_dev, rbd_dev->obj_md_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002574
2575 ret = count;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002576 kfree(name);
2577 return ret;
2578
2579err_unlock:
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002580 mutex_unlock(&ctl_mutex);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002581 kfree(name);
2582 return ret;
2583}
2584
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002585/*
2586 * create control files in sysfs
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002587 * /sys/bus/rbd/...
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002588 */
2589static int rbd_sysfs_init(void)
2590{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002591 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002592
Alex Elderfed4c142012-02-07 12:03:36 -06002593 ret = device_register(&rbd_root_dev);
Alex Elder21079782012-01-24 10:08:36 -06002594 if (ret < 0)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002595 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002596
Alex Elderfed4c142012-02-07 12:03:36 -06002597 ret = bus_register(&rbd_bus_type);
2598 if (ret < 0)
2599 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002600
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002601 return ret;
2602}
2603
2604static void rbd_sysfs_cleanup(void)
2605{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002606 bus_unregister(&rbd_bus_type);
Alex Elderfed4c142012-02-07 12:03:36 -06002607 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002608}
2609
2610int __init rbd_init(void)
2611{
2612 int rc;
2613
2614 rc = rbd_sysfs_init();
2615 if (rc)
2616 return rc;
Alex Elderf0f8cef2012-01-29 13:57:44 -06002617 pr_info("loaded " RBD_DRV_NAME_LONG "\n");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002618 return 0;
2619}
2620
2621void __exit rbd_exit(void)
2622{
2623 rbd_sysfs_cleanup();
2624}
2625
2626module_init(rbd_init);
2627module_exit(rbd_exit);
2628
2629MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
2630MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
2631MODULE_DESCRIPTION("rados block device");
2632
2633/* following authorship retained from original osdblk.c */
2634MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
2635
2636MODULE_LICENSE("GPL");