blob: 46b8f8e536be9d8d4bb4cd8428dd5fac5a1a2f2d [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 Elderaafb230e2012-09-06 16:00:54 -050044#define RBD_DEBUG /* Activate rbd_assert() calls */
45
Alex Elder593a9e72012-02-07 12:03:37 -060046/*
47 * The basic unit of block I/O is a sector. It is interpreted in a
48 * number of contexts in Linux (blk, bio, genhd), but the default is
49 * universally 512 bytes. These symbols are just slightly more
50 * meaningful than the bare numbers they represent.
51 */
52#define SECTOR_SHIFT 9
53#define SECTOR_SIZE (1ULL << SECTOR_SHIFT)
54
Alex Elderdf111be2012-08-09 10:33:26 -070055/* It might be useful to have this defined elsewhere too */
56
57#define U64_MAX ((u64) (~0ULL))
58
Alex Elderf0f8cef2012-01-29 13:57:44 -060059#define RBD_DRV_NAME "rbd"
60#define RBD_DRV_NAME_LONG "rbd (rados block device)"
Yehuda Sadeh602adf42010-08-12 16:11:25 -070061
62#define RBD_MINORS_PER_MAJOR 256 /* max minors per blkdev */
63
Yehuda Sadeh602adf42010-08-12 16:11:25 -070064#define RBD_MAX_SNAP_NAME_LEN 32
65#define RBD_MAX_OPT_LEN 1024
66
67#define RBD_SNAP_HEAD_NAME "-"
68
Alex Elder81a89792012-02-02 08:13:30 -060069/*
70 * An RBD device name will be "rbd#", where the "rbd" comes from
71 * RBD_DRV_NAME above, and # is a unique integer identifier.
72 * MAX_INT_FORMAT_WIDTH is used in ensuring DEV_NAME_LEN is big
73 * enough to hold all possible device names.
74 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -070075#define DEV_NAME_LEN 32
Alex Elder81a89792012-02-02 08:13:30 -060076#define MAX_INT_FORMAT_WIDTH ((5 * sizeof (int)) / 2 + 1)
Yehuda Sadeh602adf42010-08-12 16:11:25 -070077
Alex Eldercc0538b2012-08-10 13:12:07 -070078#define RBD_READ_ONLY_DEFAULT false
Yehuda Sadeh59c2be12011-03-21 15:10:11 -070079
Yehuda Sadeh602adf42010-08-12 16:11:25 -070080/*
81 * block device image metadata (in-memory version)
82 */
83struct rbd_image_header {
Alex Elderf84344f2012-08-31 17:29:51 -050084 /* These four fields never change for a given rbd image */
Alex Elder849b4262012-07-09 21:04:24 -050085 char *object_prefix;
Yehuda Sadeh602adf42010-08-12 16:11:25 -070086 __u8 obj_order;
87 __u8 crypt_type;
88 __u8 comp_type;
Yehuda Sadeh602adf42010-08-12 16:11:25 -070089
Alex Elderf84344f2012-08-31 17:29:51 -050090 /* The remaining fields need to be updated occasionally */
91 u64 image_size;
92 struct ceph_snap_context *snapc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -070093 char *snap_names;
94 u64 *snap_sizes;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -070095
96 u64 obj_version;
97};
98
99struct rbd_options {
Alex Eldercc0538b2012-08-10 13:12:07 -0700100 bool read_only;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700101};
102
103/*
Alex Elderf0f8cef2012-01-29 13:57:44 -0600104 * an instance of the client. multiple devices may share an rbd client.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700105 */
106struct rbd_client {
107 struct ceph_client *client;
108 struct kref kref;
109 struct list_head node;
110};
111
112/*
Alex Elderf0f8cef2012-01-29 13:57:44 -0600113 * a request completion status
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700114 */
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700115struct rbd_req_status {
116 int done;
117 int rc;
118 u64 bytes;
119};
120
121/*
122 * a collection of requests
123 */
124struct rbd_req_coll {
125 int total;
126 int num_done;
127 struct kref kref;
128 struct rbd_req_status status[0];
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700129};
130
Alex Elderf0f8cef2012-01-29 13:57:44 -0600131/*
132 * a single io request
133 */
134struct rbd_request {
135 struct request *rq; /* blk layer request */
136 struct bio *bio; /* cloned bio */
137 struct page **pages; /* list of used pages */
138 u64 len;
139 int coll_index;
140 struct rbd_req_coll *coll;
141};
142
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800143struct rbd_snap {
144 struct device dev;
145 const char *name;
Josh Durgin35915382011-12-05 18:25:13 -0800146 u64 size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800147 struct list_head node;
148 u64 id;
149};
150
Alex Elderf84344f2012-08-31 17:29:51 -0500151struct rbd_mapping {
152 char *snap_name;
153 u64 snap_id;
Alex Elder99c1f082012-08-30 14:42:15 -0500154 u64 size;
Alex Elderf84344f2012-08-31 17:29:51 -0500155 bool snap_exists;
156 bool read_only;
157};
158
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700159/*
160 * a single device
161 */
162struct rbd_device {
Alex Elderde71a292012-07-03 16:01:19 -0500163 int dev_id; /* blkdev unique id */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700164
165 int major; /* blkdev assigned major */
166 struct gendisk *disk; /* blkdev's gendisk and rq */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700167
Alex Elderf8c38922012-08-10 13:12:07 -0700168 struct rbd_options rbd_opts;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700169 struct rbd_client *rbd_client;
170
171 char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */
172
173 spinlock_t lock; /* queue lock */
174
175 struct rbd_image_header header;
Alex Elder0bed54d2012-07-03 16:01:18 -0500176 char *image_name;
177 size_t image_name_len;
178 char *header_name;
Alex Elderd22f76e2012-07-12 10:46:35 -0500179 char *pool_name;
Alex Elder9bb2f332012-07-12 10:46:35 -0500180 int pool_id;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700181
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700182 struct ceph_osd_event *watch_event;
183 struct ceph_osd_request *watch_request;
184
Josh Durginc6666012011-11-21 17:11:12 -0800185 /* protects updating the header */
186 struct rw_semaphore header_rwsem;
Alex Elderf84344f2012-08-31 17:29:51 -0500187
188 struct rbd_mapping mapping;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700189
190 struct list_head node;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800191
192 /* list of snapshots */
193 struct list_head snaps;
194
195 /* sysfs related */
196 struct device dev;
197};
198
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700199static DEFINE_MUTEX(ctl_mutex); /* Serialize open/close/setup/teardown */
Alex Eldere124a822012-01-29 13:57:44 -0600200
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700201static LIST_HEAD(rbd_dev_list); /* devices */
Alex Eldere124a822012-01-29 13:57:44 -0600202static DEFINE_SPINLOCK(rbd_dev_list_lock);
203
Alex Elder432b8582012-01-29 13:57:44 -0600204static LIST_HEAD(rbd_client_list); /* clients */
205static DEFINE_SPINLOCK(rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700206
Alex Elder9fcbb802012-08-23 23:48:49 -0500207static int rbd_dev_snap_devs_update(struct rbd_device *rbd_dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800208static void rbd_dev_release(struct device *dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800209static ssize_t rbd_snap_add(struct device *dev,
210 struct device_attribute *attr,
211 const char *buf,
212 size_t count);
Alex Elder14e70852012-07-19 09:09:27 -0500213static void __rbd_remove_snap_dev(struct rbd_snap *snap);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800214
Alex Elderf0f8cef2012-01-29 13:57:44 -0600215static ssize_t rbd_add(struct bus_type *bus, const char *buf,
216 size_t count);
217static ssize_t rbd_remove(struct bus_type *bus, const char *buf,
218 size_t count);
219
220static struct bus_attribute rbd_bus_attrs[] = {
221 __ATTR(add, S_IWUSR, NULL, rbd_add),
222 __ATTR(remove, S_IWUSR, NULL, rbd_remove),
223 __ATTR_NULL
224};
225
226static struct bus_type rbd_bus_type = {
227 .name = "rbd",
228 .bus_attrs = rbd_bus_attrs,
229};
230
231static void rbd_root_dev_release(struct device *dev)
232{
233}
234
235static struct device rbd_root_dev = {
236 .init_name = "rbd",
237 .release = rbd_root_dev_release,
238};
239
Alex Elderaafb230e2012-09-06 16:00:54 -0500240#ifdef RBD_DEBUG
241#define rbd_assert(expr) \
242 if (unlikely(!(expr))) { \
243 printk(KERN_ERR "\nAssertion failure in %s() " \
244 "at line %d:\n\n" \
245 "\trbd_assert(%s);\n\n", \
246 __func__, __LINE__, #expr); \
247 BUG(); \
248 }
249#else /* !RBD_DEBUG */
250# define rbd_assert(expr) ((void) 0)
251#endif /* !RBD_DEBUG */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800252
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800253static struct device *rbd_get_dev(struct rbd_device *rbd_dev)
254{
255 return get_device(&rbd_dev->dev);
256}
257
258static void rbd_put_dev(struct rbd_device *rbd_dev)
259{
260 put_device(&rbd_dev->dev);
261}
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700262
Alex Elder1fe5e992012-07-25 09:32:41 -0500263static int rbd_refresh_header(struct rbd_device *rbd_dev, u64 *hver);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700264
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700265static int rbd_open(struct block_device *bdev, fmode_t mode)
266{
Alex Elderf0f8cef2012-01-29 13:57:44 -0600267 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700268
Alex Elderf84344f2012-08-31 17:29:51 -0500269 if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700270 return -EROFS;
271
Alex Elder340c7a22012-08-10 13:12:07 -0700272 rbd_get_dev(rbd_dev);
Alex Elderf84344f2012-08-31 17:29:51 -0500273 set_device_ro(bdev, rbd_dev->mapping.read_only);
Alex Elder340c7a22012-08-10 13:12:07 -0700274
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700275 return 0;
276}
277
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800278static int rbd_release(struct gendisk *disk, fmode_t mode)
279{
280 struct rbd_device *rbd_dev = disk->private_data;
281
282 rbd_put_dev(rbd_dev);
283
284 return 0;
285}
286
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700287static const struct block_device_operations rbd_bd_ops = {
288 .owner = THIS_MODULE,
289 .open = rbd_open,
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800290 .release = rbd_release,
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700291};
292
293/*
294 * Initialize an rbd client instance.
Alex Elder43ae4702012-07-03 16:01:18 -0500295 * We own *ceph_opts.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700296 */
Alex Elderf8c38922012-08-10 13:12:07 -0700297static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700298{
299 struct rbd_client *rbdc;
300 int ret = -ENOMEM;
301
302 dout("rbd_client_create\n");
303 rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL);
304 if (!rbdc)
305 goto out_opt;
306
307 kref_init(&rbdc->kref);
308 INIT_LIST_HEAD(&rbdc->node);
309
Alex Elderbc534d82012-01-29 13:57:44 -0600310 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
311
Alex Elder43ae4702012-07-03 16:01:18 -0500312 rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700313 if (IS_ERR(rbdc->client))
Alex Elderbc534d82012-01-29 13:57:44 -0600314 goto out_mutex;
Alex Elder43ae4702012-07-03 16:01:18 -0500315 ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700316
317 ret = ceph_open_session(rbdc->client);
318 if (ret < 0)
319 goto out_err;
320
Alex Elder432b8582012-01-29 13:57:44 -0600321 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700322 list_add_tail(&rbdc->node, &rbd_client_list);
Alex Elder432b8582012-01-29 13:57:44 -0600323 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700324
Alex Elderbc534d82012-01-29 13:57:44 -0600325 mutex_unlock(&ctl_mutex);
326
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700327 dout("rbd_client_create created %p\n", rbdc);
328 return rbdc;
329
330out_err:
331 ceph_destroy_client(rbdc->client);
Alex Elderbc534d82012-01-29 13:57:44 -0600332out_mutex:
333 mutex_unlock(&ctl_mutex);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700334 kfree(rbdc);
335out_opt:
Alex Elder43ae4702012-07-03 16:01:18 -0500336 if (ceph_opts)
337 ceph_destroy_options(ceph_opts);
Vasiliy Kulikov28f259b2010-09-26 12:59:37 +0400338 return ERR_PTR(ret);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700339}
340
341/*
Alex Elder1f7ba332012-08-10 13:12:07 -0700342 * Find a ceph client with specific addr and configuration. If
343 * found, bump its reference count.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700344 */
Alex Elder1f7ba332012-08-10 13:12:07 -0700345static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700346{
347 struct rbd_client *client_node;
Alex Elder1f7ba332012-08-10 13:12:07 -0700348 bool found = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700349
Alex Elder43ae4702012-07-03 16:01:18 -0500350 if (ceph_opts->flags & CEPH_OPT_NOSHARE)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700351 return NULL;
352
Alex Elder1f7ba332012-08-10 13:12:07 -0700353 spin_lock(&rbd_client_list_lock);
354 list_for_each_entry(client_node, &rbd_client_list, node) {
355 if (!ceph_compare_options(ceph_opts, client_node->client)) {
356 kref_get(&client_node->kref);
357 found = true;
358 break;
359 }
360 }
361 spin_unlock(&rbd_client_list_lock);
362
363 return found ? client_node : NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700364}
365
366/*
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700367 * mount options
368 */
369enum {
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700370 Opt_last_int,
371 /* int args above */
372 Opt_last_string,
373 /* string args above */
Alex Eldercc0538b2012-08-10 13:12:07 -0700374 Opt_read_only,
375 Opt_read_write,
376 /* Boolean args above */
377 Opt_last_bool,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700378};
379
Alex Elder43ae4702012-07-03 16:01:18 -0500380static match_table_t rbd_opts_tokens = {
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700381 /* int args above */
382 /* string args above */
Alex Elderf84344f2012-08-31 17:29:51 -0500383 {Opt_read_only, "mapping.read_only"},
Alex Eldercc0538b2012-08-10 13:12:07 -0700384 {Opt_read_only, "ro"}, /* Alternate spelling */
385 {Opt_read_write, "read_write"},
386 {Opt_read_write, "rw"}, /* Alternate spelling */
387 /* Boolean args above */
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700388 {-1, NULL}
389};
390
391static int parse_rbd_opts_token(char *c, void *private)
392{
Alex Elder43ae4702012-07-03 16:01:18 -0500393 struct rbd_options *rbd_opts = private;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700394 substring_t argstr[MAX_OPT_ARGS];
395 int token, intval, ret;
396
Alex Elder43ae4702012-07-03 16:01:18 -0500397 token = match_token(c, rbd_opts_tokens, argstr);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700398 if (token < 0)
399 return -EINVAL;
400
401 if (token < Opt_last_int) {
402 ret = match_int(&argstr[0], &intval);
403 if (ret < 0) {
404 pr_err("bad mount option arg (not int) "
405 "at '%s'\n", c);
406 return ret;
407 }
408 dout("got int token %d val %d\n", token, intval);
409 } else if (token > Opt_last_int && token < Opt_last_string) {
410 dout("got string token %d val %s\n", token,
411 argstr[0].from);
Alex Eldercc0538b2012-08-10 13:12:07 -0700412 } else if (token > Opt_last_string && token < Opt_last_bool) {
413 dout("got Boolean token %d\n", token);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700414 } else {
415 dout("got token %d\n", token);
416 }
417
418 switch (token) {
Alex Eldercc0538b2012-08-10 13:12:07 -0700419 case Opt_read_only:
420 rbd_opts->read_only = true;
421 break;
422 case Opt_read_write:
423 rbd_opts->read_only = false;
424 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700425 default:
Alex Elderaafb230e2012-09-06 16:00:54 -0500426 rbd_assert(false);
427 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700428 }
429 return 0;
430}
431
432/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700433 * Get a ceph client with specific addr and configuration, if one does
434 * not exist create it.
435 */
Alex Elderf8c38922012-08-10 13:12:07 -0700436static int rbd_get_client(struct rbd_device *rbd_dev, const char *mon_addr,
437 size_t mon_addr_len, char *options)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700438{
Alex Elderf8c38922012-08-10 13:12:07 -0700439 struct rbd_options *rbd_opts = &rbd_dev->rbd_opts;
Alex Elder43ae4702012-07-03 16:01:18 -0500440 struct ceph_options *ceph_opts;
Alex Elderf8c38922012-08-10 13:12:07 -0700441 struct rbd_client *rbdc;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700442
Alex Eldercc0538b2012-08-10 13:12:07 -0700443 rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700444
Alex Elder43ae4702012-07-03 16:01:18 -0500445 ceph_opts = ceph_parse_options(options, mon_addr,
446 mon_addr + mon_addr_len,
447 parse_rbd_opts_token, rbd_opts);
Alex Elderf8c38922012-08-10 13:12:07 -0700448 if (IS_ERR(ceph_opts))
449 return PTR_ERR(ceph_opts);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700450
Alex Elder1f7ba332012-08-10 13:12:07 -0700451 rbdc = rbd_client_find(ceph_opts);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700452 if (rbdc) {
Alex Eldere6994d32012-01-29 13:57:44 -0600453 /* using an existing client */
Alex Elder43ae4702012-07-03 16:01:18 -0500454 ceph_destroy_options(ceph_opts);
Alex Elderf8c38922012-08-10 13:12:07 -0700455 } else {
456 rbdc = rbd_client_create(ceph_opts);
457 if (IS_ERR(rbdc))
458 return PTR_ERR(rbdc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700459 }
Alex Elderf8c38922012-08-10 13:12:07 -0700460 rbd_dev->rbd_client = rbdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700461
Alex Elderf8c38922012-08-10 13:12:07 -0700462 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700463}
464
465/*
466 * Destroy ceph client
Alex Elderd23a4b32012-01-29 13:57:43 -0600467 *
Alex Elder432b8582012-01-29 13:57:44 -0600468 * Caller must hold rbd_client_list_lock.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700469 */
470static void rbd_client_release(struct kref *kref)
471{
472 struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
473
474 dout("rbd_release_client %p\n", rbdc);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500475 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700476 list_del(&rbdc->node);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500477 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700478
479 ceph_destroy_client(rbdc->client);
480 kfree(rbdc);
481}
482
483/*
484 * Drop reference to ceph client node. If it's not referenced anymore, release
485 * it.
486 */
487static void rbd_put_client(struct rbd_device *rbd_dev)
488{
489 kref_put(&rbd_dev->rbd_client->kref, rbd_client_release);
490 rbd_dev->rbd_client = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700491}
492
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700493/*
494 * Destroy requests collection
495 */
496static void rbd_coll_release(struct kref *kref)
497{
498 struct rbd_req_coll *coll =
499 container_of(kref, struct rbd_req_coll, kref);
500
501 dout("rbd_coll_release %p\n", coll);
502 kfree(coll);
503}
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700504
Alex Elder8e94af82012-07-25 09:32:40 -0500505static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
506{
Alex Elder103a1502012-08-02 11:29:45 -0500507 size_t size;
508 u32 snap_count;
509
510 /* The header has to start with the magic rbd header text */
511 if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT)))
512 return false;
513
514 /*
515 * The size of a snapshot header has to fit in a size_t, and
516 * that limits the number of snapshots.
517 */
518 snap_count = le32_to_cpu(ondisk->snap_count);
519 size = SIZE_MAX - sizeof (struct ceph_snap_context);
520 if (snap_count > size / sizeof (__le64))
521 return false;
522
523 /*
524 * Not only that, but the size of the entire the snapshot
525 * header must also be representable in a size_t.
526 */
527 size -= snap_count * sizeof (__le64);
528 if ((u64) size < le64_to_cpu(ondisk->snap_names_len))
529 return false;
530
531 return true;
Alex Elder8e94af82012-07-25 09:32:40 -0500532}
533
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700534/*
535 * Create a new header structure, translate header format from the on-disk
536 * header.
537 */
538static int rbd_header_from_disk(struct rbd_image_header *header,
Alex Elder4156d992012-08-02 11:29:46 -0500539 struct rbd_image_header_ondisk *ondisk)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700540{
Alex Elderccece232012-07-10 20:30:10 -0500541 u32 snap_count;
Alex Elder58c17b02012-08-23 23:22:06 -0500542 size_t len;
Alex Elderd2bb24e2012-07-26 23:37:14 -0500543 size_t size;
Alex Elder621901d2012-08-23 23:22:06 -0500544 u32 i;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700545
Alex Elder6a523252012-07-19 17:12:59 -0500546 memset(header, 0, sizeof (*header));
547
Alex Elder103a1502012-08-02 11:29:45 -0500548 snap_count = le32_to_cpu(ondisk->snap_count);
549
Alex Elder58c17b02012-08-23 23:22:06 -0500550 len = strnlen(ondisk->object_prefix, sizeof (ondisk->object_prefix));
551 header->object_prefix = kmalloc(len + 1, GFP_KERNEL);
Alex Elder6a523252012-07-19 17:12:59 -0500552 if (!header->object_prefix)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700553 return -ENOMEM;
Alex Elder58c17b02012-08-23 23:22:06 -0500554 memcpy(header->object_prefix, ondisk->object_prefix, len);
555 header->object_prefix[len] = '\0';
Alex Elder00f1f362012-02-07 12:03:36 -0600556
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700557 if (snap_count) {
Alex Elderf785cc12012-08-23 23:22:06 -0500558 u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len);
559
Alex Elder621901d2012-08-23 23:22:06 -0500560 /* Save a copy of the snapshot names */
561
Alex Elderf785cc12012-08-23 23:22:06 -0500562 if (snap_names_len > (u64) SIZE_MAX)
563 return -EIO;
564 header->snap_names = kmalloc(snap_names_len, GFP_KERNEL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700565 if (!header->snap_names)
Alex Elder6a523252012-07-19 17:12:59 -0500566 goto out_err;
Alex Elderf785cc12012-08-23 23:22:06 -0500567 /*
568 * Note that rbd_dev_v1_header_read() guarantees
569 * the ondisk buffer we're working with has
570 * snap_names_len bytes beyond the end of the
571 * snapshot id array, this memcpy() is safe.
572 */
573 memcpy(header->snap_names, &ondisk->snaps[snap_count],
574 snap_names_len);
Alex Elder6a523252012-07-19 17:12:59 -0500575
Alex Elder621901d2012-08-23 23:22:06 -0500576 /* Record each snapshot's size */
577
Alex Elderd2bb24e2012-07-26 23:37:14 -0500578 size = snap_count * sizeof (*header->snap_sizes);
579 header->snap_sizes = kmalloc(size, GFP_KERNEL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700580 if (!header->snap_sizes)
Alex Elder6a523252012-07-19 17:12:59 -0500581 goto out_err;
Alex Elder621901d2012-08-23 23:22:06 -0500582 for (i = 0; i < snap_count; i++)
583 header->snap_sizes[i] =
584 le64_to_cpu(ondisk->snaps[i].image_size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700585 } else {
Alex Elderccece232012-07-10 20:30:10 -0500586 WARN_ON(ondisk->snap_names_len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700587 header->snap_names = NULL;
588 header->snap_sizes = NULL;
589 }
Alex Elder849b4262012-07-09 21:04:24 -0500590
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700591 header->obj_order = ondisk->options.order;
592 header->crypt_type = ondisk->options.crypt_type;
593 header->comp_type = ondisk->options.comp_type;
Alex Elder6a523252012-07-19 17:12:59 -0500594
Alex Elder621901d2012-08-23 23:22:06 -0500595 /* Allocate and fill in the snapshot context */
596
Alex Elderf84344f2012-08-31 17:29:51 -0500597 header->image_size = le64_to_cpu(ondisk->image_size);
Alex Elder6a523252012-07-19 17:12:59 -0500598 size = sizeof (struct ceph_snap_context);
599 size += snap_count * sizeof (header->snapc->snaps[0]);
600 header->snapc = kzalloc(size, GFP_KERNEL);
601 if (!header->snapc)
602 goto out_err;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700603
604 atomic_set(&header->snapc->nref, 1);
Alex Elder505cbb92012-07-19 08:49:18 -0500605 header->snapc->seq = le64_to_cpu(ondisk->snap_seq);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700606 header->snapc->num_snaps = snap_count;
Alex Elder621901d2012-08-23 23:22:06 -0500607 for (i = 0; i < snap_count; i++)
608 header->snapc->snaps[i] =
609 le64_to_cpu(ondisk->snaps[i].id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700610
611 return 0;
612
Alex Elder6a523252012-07-19 17:12:59 -0500613out_err:
Alex Elder849b4262012-07-09 21:04:24 -0500614 kfree(header->snap_sizes);
Alex Elderccece232012-07-10 20:30:10 -0500615 header->snap_sizes = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700616 kfree(header->snap_names);
Alex Elderccece232012-07-10 20:30:10 -0500617 header->snap_names = NULL;
Alex Elder6a523252012-07-19 17:12:59 -0500618 kfree(header->object_prefix);
619 header->object_prefix = NULL;
Alex Elderccece232012-07-10 20:30:10 -0500620
Alex Elder00f1f362012-02-07 12:03:36 -0600621 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700622}
623
Alex Elder8836b992012-08-30 14:42:15 -0500624static int snap_by_name(struct rbd_device *rbd_dev, const char *snap_name)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700625{
626 int i;
Alex Elder8836b992012-08-30 14:42:15 -0500627 struct rbd_image_header *header = &rbd_dev->header;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700628 char *p = header->snap_names;
629
Alex Elderc9aadfe2012-08-30 14:42:15 -0500630 rbd_assert(header->snapc != NULL);
631 for (i = 0; i < header->snapc->num_snaps; i++) {
Alex Elder00f1f362012-02-07 12:03:36 -0600632 if (!strcmp(snap_name, p)) {
633
634 /* Found it. Pass back its id and/or size */
635
Alex Elder8836b992012-08-30 14:42:15 -0500636 rbd_dev->mapping.snap_id = header->snapc->snaps[i];
637 rbd_dev->mapping.size = header->snap_sizes[i];
638
Alex Elder00f1f362012-02-07 12:03:36 -0600639 return i;
640 }
641 p += strlen(p) + 1; /* Skip ahead to the next name */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700642 }
Alex Elder00f1f362012-02-07 12:03:36 -0600643 return -ENOENT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700644}
645
Alex Elder4e1105a2012-08-31 17:29:52 -0500646static int rbd_header_set_snap(struct rbd_device *rbd_dev, char *snap_name)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700647{
Alex Elder78dc4472012-07-19 08:49:18 -0500648 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700649
Alex Elder0ce1a792012-07-03 16:01:18 -0500650 down_write(&rbd_dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700651
Alex Elder4e1105a2012-08-31 17:29:52 -0500652 if (!memcmp(snap_name, RBD_SNAP_HEAD_NAME,
Josh Durgincc9d7342011-11-21 18:19:13 -0800653 sizeof (RBD_SNAP_HEAD_NAME))) {
Alex Elderf84344f2012-08-31 17:29:51 -0500654 rbd_dev->mapping.snap_id = CEPH_NOSNAP;
Alex Elder99c1f082012-08-30 14:42:15 -0500655 rbd_dev->mapping.size = rbd_dev->header.image_size;
Alex Elderf84344f2012-08-31 17:29:51 -0500656 rbd_dev->mapping.snap_exists = false;
657 rbd_dev->mapping.read_only = rbd_dev->rbd_opts.read_only;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700658 } else {
Alex Elder8836b992012-08-30 14:42:15 -0500659 ret = snap_by_name(rbd_dev, snap_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700660 if (ret < 0)
661 goto done;
Alex Elderf84344f2012-08-31 17:29:51 -0500662 rbd_dev->mapping.snap_exists = true;
663 rbd_dev->mapping.read_only = true;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700664 }
Alex Elder4e1105a2012-08-31 17:29:52 -0500665 rbd_dev->mapping.snap_name = snap_name;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700666
667 ret = 0;
668done:
Alex Elder0ce1a792012-07-03 16:01:18 -0500669 up_write(&rbd_dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700670 return ret;
671}
672
673static void rbd_header_free(struct rbd_image_header *header)
674{
Alex Elder849b4262012-07-09 21:04:24 -0500675 kfree(header->object_prefix);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500676 header->object_prefix = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700677 kfree(header->snap_sizes);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500678 header->snap_sizes = NULL;
Alex Elder849b4262012-07-09 21:04:24 -0500679 kfree(header->snap_names);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500680 header->snap_names = NULL;
Josh Durgind1d25642011-12-05 14:03:05 -0800681 ceph_put_snap_context(header->snapc);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500682 header->snapc = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700683}
684
Alex Elder65ccfe22012-08-09 10:33:26 -0700685static char *rbd_segment_name(struct rbd_device *rbd_dev, u64 offset)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700686{
Alex Elder65ccfe22012-08-09 10:33:26 -0700687 char *name;
688 u64 segment;
689 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700690
Alex Elder65ccfe22012-08-09 10:33:26 -0700691 name = kmalloc(RBD_MAX_SEG_NAME_LEN + 1, GFP_NOIO);
692 if (!name)
693 return NULL;
694 segment = offset >> rbd_dev->header.obj_order;
695 ret = snprintf(name, RBD_MAX_SEG_NAME_LEN, "%s.%012llx",
696 rbd_dev->header.object_prefix, segment);
697 if (ret < 0 || ret >= RBD_MAX_SEG_NAME_LEN) {
698 pr_err("error formatting segment name for #%llu (%d)\n",
699 segment, ret);
700 kfree(name);
701 name = NULL;
702 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700703
Alex Elder65ccfe22012-08-09 10:33:26 -0700704 return name;
705}
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700706
Alex Elder65ccfe22012-08-09 10:33:26 -0700707static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset)
708{
709 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700710
Alex Elder65ccfe22012-08-09 10:33:26 -0700711 return offset & (segment_size - 1);
712}
713
714static u64 rbd_segment_length(struct rbd_device *rbd_dev,
715 u64 offset, u64 length)
716{
717 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
718
719 offset &= segment_size - 1;
720
Alex Elderaafb230e2012-09-06 16:00:54 -0500721 rbd_assert(length <= U64_MAX - offset);
Alex Elder65ccfe22012-08-09 10:33:26 -0700722 if (offset + length > segment_size)
723 length = segment_size - offset;
724
725 return length;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700726}
727
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700728static int rbd_get_num_segments(struct rbd_image_header *header,
729 u64 ofs, u64 len)
730{
Alex Elderdf111be2012-08-09 10:33:26 -0700731 u64 start_seg;
732 u64 end_seg;
733
734 if (!len)
735 return 0;
736 if (len - 1 > U64_MAX - ofs)
737 return -ERANGE;
738
739 start_seg = ofs >> header->obj_order;
740 end_seg = (ofs + len - 1) >> header->obj_order;
741
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700742 return end_seg - start_seg + 1;
743}
744
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700745/*
Josh Durgin029bcbd2011-07-22 11:35:23 -0700746 * returns the size of an object in the image
747 */
748static u64 rbd_obj_bytes(struct rbd_image_header *header)
749{
750 return 1 << header->obj_order;
751}
752
753/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700754 * bio helpers
755 */
756
757static void bio_chain_put(struct bio *chain)
758{
759 struct bio *tmp;
760
761 while (chain) {
762 tmp = chain;
763 chain = chain->bi_next;
764 bio_put(tmp);
765 }
766}
767
768/*
769 * zeros a bio chain, starting at specific offset
770 */
771static void zero_bio_chain(struct bio *chain, int start_ofs)
772{
773 struct bio_vec *bv;
774 unsigned long flags;
775 void *buf;
776 int i;
777 int pos = 0;
778
779 while (chain) {
780 bio_for_each_segment(bv, chain, i) {
781 if (pos + bv->bv_len > start_ofs) {
782 int remainder = max(start_ofs - pos, 0);
783 buf = bvec_kmap_irq(bv, &flags);
784 memset(buf + remainder, 0,
785 bv->bv_len - remainder);
Dan Carpenter85b5aaa2010-10-11 21:15:11 +0200786 bvec_kunmap_irq(buf, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700787 }
788 pos += bv->bv_len;
789 }
790
791 chain = chain->bi_next;
792 }
793}
794
795/*
796 * bio_chain_clone - clone a chain of bios up to a certain length.
797 * might return a bio_pair that will need to be released.
798 */
799static struct bio *bio_chain_clone(struct bio **old, struct bio **next,
800 struct bio_pair **bp,
801 int len, gfp_t gfpmask)
802{
Alex Elder542582f2012-08-09 10:33:25 -0700803 struct bio *old_chain = *old;
804 struct bio *new_chain = NULL;
805 struct bio *tail;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700806 int total = 0;
807
808 if (*bp) {
809 bio_pair_release(*bp);
810 *bp = NULL;
811 }
812
813 while (old_chain && (total < len)) {
Alex Elder542582f2012-08-09 10:33:25 -0700814 struct bio *tmp;
815
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700816 tmp = bio_kmalloc(gfpmask, old_chain->bi_max_vecs);
817 if (!tmp)
818 goto err_out;
Alex Elder542582f2012-08-09 10:33:25 -0700819 gfpmask &= ~__GFP_WAIT; /* can't wait after the first */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700820
821 if (total + old_chain->bi_size > len) {
822 struct bio_pair *bp;
823
824 /*
825 * this split can only happen with a single paged bio,
826 * split_bio will BUG_ON if this is not the case
827 */
828 dout("bio_chain_clone split! total=%d remaining=%d"
Alex Elderbd919d42012-07-13 20:35:11 -0500829 "bi_size=%u\n",
830 total, len - total, old_chain->bi_size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700831
832 /* split the bio. We'll release it either in the next
833 call, or it will have to be released outside */
Alex Elder593a9e72012-02-07 12:03:37 -0600834 bp = bio_split(old_chain, (len - total) / SECTOR_SIZE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700835 if (!bp)
836 goto err_out;
837
838 __bio_clone(tmp, &bp->bio1);
839
840 *next = &bp->bio2;
841 } else {
842 __bio_clone(tmp, old_chain);
843 *next = old_chain->bi_next;
844 }
845
846 tmp->bi_bdev = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700847 tmp->bi_next = NULL;
Alex Elder542582f2012-08-09 10:33:25 -0700848 if (new_chain)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700849 tail->bi_next = tmp;
Alex Elder542582f2012-08-09 10:33:25 -0700850 else
851 new_chain = tmp;
852 tail = tmp;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700853 old_chain = old_chain->bi_next;
854
855 total += tmp->bi_size;
856 }
857
Alex Elderaafb230e2012-09-06 16:00:54 -0500858 rbd_assert(total == len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700859
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700860 *old = old_chain;
861
862 return new_chain;
863
864err_out:
865 dout("bio_chain_clone with err\n");
866 bio_chain_put(new_chain);
867 return NULL;
868}
869
870/*
871 * helpers for osd request op vectors.
872 */
Alex Elder57cfc102012-06-26 12:57:03 -0700873static struct ceph_osd_req_op *rbd_create_rw_ops(int num_ops,
874 int opcode, u32 payload_len)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700875{
Alex Elder57cfc102012-06-26 12:57:03 -0700876 struct ceph_osd_req_op *ops;
877
878 ops = kzalloc(sizeof (*ops) * (num_ops + 1), GFP_NOIO);
879 if (!ops)
880 return NULL;
881
882 ops[0].op = opcode;
883
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700884 /*
885 * op extent offset and length will be set later on
886 * in calc_raw_layout()
887 */
Alex Elder57cfc102012-06-26 12:57:03 -0700888 ops[0].payload_len = payload_len;
889
890 return ops;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700891}
892
893static void rbd_destroy_ops(struct ceph_osd_req_op *ops)
894{
895 kfree(ops);
896}
897
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700898static void rbd_coll_end_req_index(struct request *rq,
899 struct rbd_req_coll *coll,
900 int index,
901 int ret, u64 len)
902{
903 struct request_queue *q;
904 int min, max, i;
905
Alex Elderbd919d42012-07-13 20:35:11 -0500906 dout("rbd_coll_end_req_index %p index %d ret %d len %llu\n",
907 coll, index, ret, (unsigned long long) len);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700908
909 if (!rq)
910 return;
911
912 if (!coll) {
913 blk_end_request(rq, ret, len);
914 return;
915 }
916
917 q = rq->q;
918
919 spin_lock_irq(q->queue_lock);
920 coll->status[index].done = 1;
921 coll->status[index].rc = ret;
922 coll->status[index].bytes = len;
923 max = min = coll->num_done;
924 while (max < coll->total && coll->status[max].done)
925 max++;
926
927 for (i = min; i<max; i++) {
928 __blk_end_request(rq, coll->status[i].rc,
929 coll->status[i].bytes);
930 coll->num_done++;
931 kref_put(&coll->kref, rbd_coll_release);
932 }
933 spin_unlock_irq(q->queue_lock);
934}
935
936static void rbd_coll_end_req(struct rbd_request *req,
937 int ret, u64 len)
938{
939 rbd_coll_end_req_index(req->rq, req->coll, req->coll_index, ret, len);
940}
941
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700942/*
943 * Send ceph osd request
944 */
945static int rbd_do_request(struct request *rq,
Alex Elder0ce1a792012-07-03 16:01:18 -0500946 struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700947 struct ceph_snap_context *snapc,
948 u64 snapid,
Alex Elderaded07e2012-07-03 16:01:18 -0500949 const char *object_name, u64 ofs, u64 len,
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700950 struct bio *bio,
951 struct page **pages,
952 int num_pages,
953 int flags,
954 struct ceph_osd_req_op *ops,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700955 struct rbd_req_coll *coll,
956 int coll_index,
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700957 void (*rbd_cb)(struct ceph_osd_request *req,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700958 struct ceph_msg *msg),
959 struct ceph_osd_request **linger_req,
960 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700961{
962 struct ceph_osd_request *req;
963 struct ceph_file_layout *layout;
964 int ret;
965 u64 bno;
966 struct timespec mtime = CURRENT_TIME;
967 struct rbd_request *req_data;
968 struct ceph_osd_request_head *reqhead;
Alex Elder1dbb4392012-01-24 10:08:37 -0600969 struct ceph_osd_client *osdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700970
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700971 req_data = kzalloc(sizeof(*req_data), GFP_NOIO);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700972 if (!req_data) {
973 if (coll)
974 rbd_coll_end_req_index(rq, coll, coll_index,
975 -ENOMEM, len);
976 return -ENOMEM;
977 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700978
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700979 if (coll) {
980 req_data->coll = coll;
981 req_data->coll_index = coll_index;
982 }
983
Alex Elderbd919d42012-07-13 20:35:11 -0500984 dout("rbd_do_request object_name=%s ofs=%llu len=%llu\n", object_name,
985 (unsigned long long) ofs, (unsigned long long) len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700986
Alex Elder0ce1a792012-07-03 16:01:18 -0500987 osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder1dbb4392012-01-24 10:08:37 -0600988 req = ceph_osdc_alloc_request(osdc, flags, snapc, ops,
989 false, GFP_NOIO, pages, bio);
Sage Weil4ad12622011-05-03 09:23:36 -0700990 if (!req) {
Sage Weil4ad12622011-05-03 09:23:36 -0700991 ret = -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700992 goto done_pages;
993 }
994
995 req->r_callback = rbd_cb;
996
997 req_data->rq = rq;
998 req_data->bio = bio;
999 req_data->pages = pages;
1000 req_data->len = len;
1001
1002 req->r_priv = req_data;
1003
1004 reqhead = req->r_request->front.iov_base;
1005 reqhead->snapid = cpu_to_le64(CEPH_NOSNAP);
1006
Alex Elderaded07e2012-07-03 16:01:18 -05001007 strncpy(req->r_oid, object_name, sizeof(req->r_oid));
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001008 req->r_oid_len = strlen(req->r_oid);
1009
1010 layout = &req->r_file_layout;
1011 memset(layout, 0, sizeof(*layout));
1012 layout->fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
1013 layout->fl_stripe_count = cpu_to_le32(1);
1014 layout->fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
Alex Elder0ce1a792012-07-03 16:01:18 -05001015 layout->fl_pg_pool = cpu_to_le32(rbd_dev->pool_id);
Alex Elder1dbb4392012-01-24 10:08:37 -06001016 ceph_calc_raw_layout(osdc, layout, snapid, ofs, &len, &bno,
1017 req, ops);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001018
1019 ceph_osdc_build_request(req, ofs, &len,
1020 ops,
1021 snapc,
1022 &mtime,
1023 req->r_oid, req->r_oid_len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001024
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001025 if (linger_req) {
Alex Elder1dbb4392012-01-24 10:08:37 -06001026 ceph_osdc_set_request_linger(osdc, req);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001027 *linger_req = req;
1028 }
1029
Alex Elder1dbb4392012-01-24 10:08:37 -06001030 ret = ceph_osdc_start_request(osdc, req, false);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001031 if (ret < 0)
1032 goto done_err;
1033
1034 if (!rbd_cb) {
Alex Elder1dbb4392012-01-24 10:08:37 -06001035 ret = ceph_osdc_wait_request(osdc, req);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001036 if (ver)
1037 *ver = le64_to_cpu(req->r_reassert_version.version);
Alex Elderbd919d42012-07-13 20:35:11 -05001038 dout("reassert_ver=%llu\n",
1039 (unsigned long long)
1040 le64_to_cpu(req->r_reassert_version.version));
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001041 ceph_osdc_put_request(req);
1042 }
1043 return ret;
1044
1045done_err:
1046 bio_chain_put(req_data->bio);
1047 ceph_osdc_put_request(req);
1048done_pages:
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001049 rbd_coll_end_req(req_data, ret, len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001050 kfree(req_data);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001051 return ret;
1052}
1053
1054/*
1055 * Ceph osd op callback
1056 */
1057static void rbd_req_cb(struct ceph_osd_request *req, struct ceph_msg *msg)
1058{
1059 struct rbd_request *req_data = req->r_priv;
1060 struct ceph_osd_reply_head *replyhead;
1061 struct ceph_osd_op *op;
1062 __s32 rc;
1063 u64 bytes;
1064 int read_op;
1065
1066 /* parse reply */
1067 replyhead = msg->front.iov_base;
1068 WARN_ON(le32_to_cpu(replyhead->num_ops) == 0);
1069 op = (void *)(replyhead + 1);
1070 rc = le32_to_cpu(replyhead->result);
1071 bytes = le64_to_cpu(op->extent.length);
Dan Carpenter895cfcc2012-06-06 09:15:33 -05001072 read_op = (le16_to_cpu(op->op) == CEPH_OSD_OP_READ);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001073
Alex Elderbd919d42012-07-13 20:35:11 -05001074 dout("rbd_req_cb bytes=%llu readop=%d rc=%d\n",
1075 (unsigned long long) bytes, read_op, (int) rc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001076
1077 if (rc == -ENOENT && read_op) {
1078 zero_bio_chain(req_data->bio, 0);
1079 rc = 0;
1080 } else if (rc == 0 && read_op && bytes < req_data->len) {
1081 zero_bio_chain(req_data->bio, bytes);
1082 bytes = req_data->len;
1083 }
1084
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001085 rbd_coll_end_req(req_data, rc, bytes);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001086
1087 if (req_data->bio)
1088 bio_chain_put(req_data->bio);
1089
1090 ceph_osdc_put_request(req);
1091 kfree(req_data);
1092}
1093
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001094static void rbd_simple_req_cb(struct ceph_osd_request *req, struct ceph_msg *msg)
1095{
1096 ceph_osdc_put_request(req);
1097}
1098
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001099/*
1100 * Do a synchronous ceph osd operation
1101 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001102static int rbd_req_sync_op(struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001103 struct ceph_snap_context *snapc,
1104 u64 snapid,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001105 int flags,
Alex Elder913d2fd2012-06-26 12:57:03 -07001106 struct ceph_osd_req_op *ops,
Alex Elderaded07e2012-07-03 16:01:18 -05001107 const char *object_name,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001108 u64 ofs, u64 len,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001109 char *buf,
1110 struct ceph_osd_request **linger_req,
1111 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001112{
1113 int ret;
1114 struct page **pages;
1115 int num_pages;
Alex Elder913d2fd2012-06-26 12:57:03 -07001116
Alex Elderaafb230e2012-09-06 16:00:54 -05001117 rbd_assert(ops != NULL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001118
1119 num_pages = calc_pages_for(ofs , len);
1120 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
Dan Carpenterb8d06382010-10-11 21:14:23 +02001121 if (IS_ERR(pages))
1122 return PTR_ERR(pages);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001123
Alex Elder0ce1a792012-07-03 16:01:18 -05001124 ret = rbd_do_request(NULL, rbd_dev, snapc, snapid,
Alex Elderaded07e2012-07-03 16:01:18 -05001125 object_name, ofs, len, NULL,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001126 pages, num_pages,
1127 flags,
1128 ops,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001129 NULL, 0,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001130 NULL,
1131 linger_req, ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001132 if (ret < 0)
Alex Elder913d2fd2012-06-26 12:57:03 -07001133 goto done;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001134
1135 if ((flags & CEPH_OSD_FLAG_READ) && buf)
1136 ret = ceph_copy_from_page_vector(pages, buf, ofs, ret);
1137
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001138done:
1139 ceph_release_page_vector(pages, num_pages);
1140 return ret;
1141}
1142
1143/*
1144 * Do an asynchronous ceph osd operation
1145 */
1146static int rbd_do_op(struct request *rq,
Alex Elder0ce1a792012-07-03 16:01:18 -05001147 struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001148 struct ceph_snap_context *snapc,
1149 u64 snapid,
Alex Elderd1f57ea2012-06-26 12:57:03 -07001150 int opcode, int flags,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001151 u64 ofs, u64 len,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001152 struct bio *bio,
1153 struct rbd_req_coll *coll,
1154 int coll_index)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001155{
1156 char *seg_name;
1157 u64 seg_ofs;
1158 u64 seg_len;
1159 int ret;
1160 struct ceph_osd_req_op *ops;
1161 u32 payload_len;
1162
Alex Elder65ccfe22012-08-09 10:33:26 -07001163 seg_name = rbd_segment_name(rbd_dev, ofs);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001164 if (!seg_name)
1165 return -ENOMEM;
Alex Elder65ccfe22012-08-09 10:33:26 -07001166 seg_len = rbd_segment_length(rbd_dev, ofs, len);
1167 seg_ofs = rbd_segment_offset(rbd_dev, ofs);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001168
1169 payload_len = (flags & CEPH_OSD_FLAG_WRITE ? seg_len : 0);
1170
Alex Elder57cfc102012-06-26 12:57:03 -07001171 ret = -ENOMEM;
1172 ops = rbd_create_rw_ops(1, opcode, payload_len);
1173 if (!ops)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001174 goto done;
1175
1176 /* we've taken care of segment sizes earlier when we
1177 cloned the bios. We should never have a segment
1178 truncated at this point */
Alex Elderaafb230e2012-09-06 16:00:54 -05001179 rbd_assert(seg_len == len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001180
1181 ret = rbd_do_request(rq, rbd_dev, snapc, snapid,
1182 seg_name, seg_ofs, seg_len,
1183 bio,
1184 NULL, 0,
1185 flags,
1186 ops,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001187 coll, coll_index,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001188 rbd_req_cb, 0, NULL);
Sage Weil11f77002011-05-12 16:13:54 -07001189
1190 rbd_destroy_ops(ops);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001191done:
1192 kfree(seg_name);
1193 return ret;
1194}
1195
1196/*
1197 * Request async osd write
1198 */
1199static int rbd_req_write(struct request *rq,
1200 struct rbd_device *rbd_dev,
1201 struct ceph_snap_context *snapc,
1202 u64 ofs, u64 len,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001203 struct bio *bio,
1204 struct rbd_req_coll *coll,
1205 int coll_index)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001206{
1207 return rbd_do_op(rq, rbd_dev, snapc, CEPH_NOSNAP,
1208 CEPH_OSD_OP_WRITE,
1209 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001210 ofs, len, bio, coll, coll_index);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001211}
1212
1213/*
1214 * Request async osd read
1215 */
1216static int rbd_req_read(struct request *rq,
1217 struct rbd_device *rbd_dev,
1218 u64 snapid,
1219 u64 ofs, u64 len,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001220 struct bio *bio,
1221 struct rbd_req_coll *coll,
1222 int coll_index)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001223{
1224 return rbd_do_op(rq, rbd_dev, NULL,
Josh Durginb06e6a62011-11-21 18:16:52 -08001225 snapid,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001226 CEPH_OSD_OP_READ,
1227 CEPH_OSD_FLAG_READ,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001228 ofs, len, bio, coll, coll_index);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001229}
1230
1231/*
1232 * Request sync osd read
1233 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001234static int rbd_req_sync_read(struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001235 u64 snapid,
Alex Elderaded07e2012-07-03 16:01:18 -05001236 const char *object_name,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001237 u64 ofs, u64 len,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001238 char *buf,
1239 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001240{
Alex Elder913d2fd2012-06-26 12:57:03 -07001241 struct ceph_osd_req_op *ops;
1242 int ret;
1243
1244 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_READ, 0);
1245 if (!ops)
1246 return -ENOMEM;
1247
1248 ret = rbd_req_sync_op(rbd_dev, NULL,
Josh Durginb06e6a62011-11-21 18:16:52 -08001249 snapid,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001250 CEPH_OSD_FLAG_READ,
Alex Elder913d2fd2012-06-26 12:57:03 -07001251 ops, object_name, ofs, len, buf, NULL, ver);
1252 rbd_destroy_ops(ops);
1253
1254 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001255}
1256
1257/*
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001258 * Request sync osd watch
1259 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001260static int rbd_req_sync_notify_ack(struct rbd_device *rbd_dev,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001261 u64 ver,
Alex Elder7f0a24d2012-07-25 09:32:40 -05001262 u64 notify_id)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001263{
1264 struct ceph_osd_req_op *ops;
Sage Weil11f77002011-05-12 16:13:54 -07001265 int ret;
1266
Alex Elder57cfc102012-06-26 12:57:03 -07001267 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_NOTIFY_ACK, 0);
1268 if (!ops)
1269 return -ENOMEM;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001270
Josh Durgina71b8912011-12-05 18:10:44 -08001271 ops[0].watch.ver = cpu_to_le64(ver);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001272 ops[0].watch.cookie = notify_id;
1273 ops[0].watch.flag = 0;
1274
Alex Elder0ce1a792012-07-03 16:01:18 -05001275 ret = rbd_do_request(NULL, rbd_dev, NULL, CEPH_NOSNAP,
Alex Elder7f0a24d2012-07-25 09:32:40 -05001276 rbd_dev->header_name, 0, 0, NULL,
Alex Elderad4f2322012-07-03 16:01:19 -05001277 NULL, 0,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001278 CEPH_OSD_FLAG_READ,
1279 ops,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001280 NULL, 0,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001281 rbd_simple_req_cb, 0, NULL);
1282
1283 rbd_destroy_ops(ops);
1284 return ret;
1285}
1286
1287static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
1288{
Alex Elder0ce1a792012-07-03 16:01:18 -05001289 struct rbd_device *rbd_dev = (struct rbd_device *)data;
Josh Durgina71b8912011-12-05 18:10:44 -08001290 u64 hver;
Sage Weil13143d22011-05-12 16:08:30 -07001291 int rc;
1292
Alex Elder0ce1a792012-07-03 16:01:18 -05001293 if (!rbd_dev)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001294 return;
1295
Alex Elderbd919d42012-07-13 20:35:11 -05001296 dout("rbd_watch_cb %s notify_id=%llu opcode=%u\n",
1297 rbd_dev->header_name, (unsigned long long) notify_id,
1298 (unsigned int) opcode);
Alex Elder1fe5e992012-07-25 09:32:41 -05001299 rc = rbd_refresh_header(rbd_dev, &hver);
Sage Weil13143d22011-05-12 16:08:30 -07001300 if (rc)
Alex Elderf0f8cef2012-01-29 13:57:44 -06001301 pr_warning(RBD_DRV_NAME "%d got notification but failed to "
Alex Elder0ce1a792012-07-03 16:01:18 -05001302 " update snaps: %d\n", rbd_dev->major, rc);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001303
Alex Elder7f0a24d2012-07-25 09:32:40 -05001304 rbd_req_sync_notify_ack(rbd_dev, hver, notify_id);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001305}
1306
1307/*
1308 * Request sync osd watch
1309 */
Alex Elder0e6f3222012-07-25 09:32:40 -05001310static int rbd_req_sync_watch(struct rbd_device *rbd_dev)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001311{
1312 struct ceph_osd_req_op *ops;
Alex Elder0ce1a792012-07-03 16:01:18 -05001313 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder57cfc102012-06-26 12:57:03 -07001314 int ret;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001315
Alex Elder57cfc102012-06-26 12:57:03 -07001316 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_WATCH, 0);
1317 if (!ops)
1318 return -ENOMEM;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001319
1320 ret = ceph_osdc_create_event(osdc, rbd_watch_cb, 0,
Alex Elder0ce1a792012-07-03 16:01:18 -05001321 (void *)rbd_dev, &rbd_dev->watch_event);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001322 if (ret < 0)
1323 goto fail;
1324
Alex Elder0e6f3222012-07-25 09:32:40 -05001325 ops[0].watch.ver = cpu_to_le64(rbd_dev->header.obj_version);
Alex Elder0ce1a792012-07-03 16:01:18 -05001326 ops[0].watch.cookie = cpu_to_le64(rbd_dev->watch_event->cookie);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001327 ops[0].watch.flag = 1;
1328
Alex Elder0ce1a792012-07-03 16:01:18 -05001329 ret = rbd_req_sync_op(rbd_dev, NULL,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001330 CEPH_NOSNAP,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001331 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1332 ops,
Alex Elder0e6f3222012-07-25 09:32:40 -05001333 rbd_dev->header_name,
1334 0, 0, NULL,
Alex Elder0ce1a792012-07-03 16:01:18 -05001335 &rbd_dev->watch_request, NULL);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001336
1337 if (ret < 0)
1338 goto fail_event;
1339
1340 rbd_destroy_ops(ops);
1341 return 0;
1342
1343fail_event:
Alex Elder0ce1a792012-07-03 16:01:18 -05001344 ceph_osdc_cancel_event(rbd_dev->watch_event);
1345 rbd_dev->watch_event = NULL;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001346fail:
1347 rbd_destroy_ops(ops);
1348 return ret;
1349}
1350
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001351/*
1352 * Request sync osd unwatch
1353 */
Alex Elder070c6332012-07-25 09:32:41 -05001354static int rbd_req_sync_unwatch(struct rbd_device *rbd_dev)
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001355{
1356 struct ceph_osd_req_op *ops;
Alex Elder57cfc102012-06-26 12:57:03 -07001357 int ret;
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001358
Alex Elder57cfc102012-06-26 12:57:03 -07001359 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_WATCH, 0);
1360 if (!ops)
1361 return -ENOMEM;
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001362
1363 ops[0].watch.ver = 0;
Alex Elder0ce1a792012-07-03 16:01:18 -05001364 ops[0].watch.cookie = cpu_to_le64(rbd_dev->watch_event->cookie);
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001365 ops[0].watch.flag = 0;
1366
Alex Elder0ce1a792012-07-03 16:01:18 -05001367 ret = rbd_req_sync_op(rbd_dev, NULL,
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001368 CEPH_NOSNAP,
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001369 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1370 ops,
Alex Elder070c6332012-07-25 09:32:41 -05001371 rbd_dev->header_name,
1372 0, 0, NULL, NULL, NULL);
1373
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001374
1375 rbd_destroy_ops(ops);
Alex Elder0ce1a792012-07-03 16:01:18 -05001376 ceph_osdc_cancel_event(rbd_dev->watch_event);
1377 rbd_dev->watch_event = NULL;
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001378 return ret;
1379}
1380
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001381struct rbd_notify_info {
Alex Elder0ce1a792012-07-03 16:01:18 -05001382 struct rbd_device *rbd_dev;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001383};
1384
1385static void rbd_notify_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
1386{
Alex Elder0ce1a792012-07-03 16:01:18 -05001387 struct rbd_device *rbd_dev = (struct rbd_device *)data;
1388 if (!rbd_dev)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001389 return;
1390
Alex Elderbd919d42012-07-13 20:35:11 -05001391 dout("rbd_notify_cb %s notify_id=%llu opcode=%u\n",
1392 rbd_dev->header_name, (unsigned long long) notify_id,
1393 (unsigned int) opcode);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001394}
1395
1396/*
1397 * Request sync osd notify
1398 */
Alex Elder4cb16252012-07-25 09:32:40 -05001399static int rbd_req_sync_notify(struct rbd_device *rbd_dev)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001400{
1401 struct ceph_osd_req_op *ops;
Alex Elder0ce1a792012-07-03 16:01:18 -05001402 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001403 struct ceph_osd_event *event;
1404 struct rbd_notify_info info;
1405 int payload_len = sizeof(u32) + sizeof(u32);
1406 int ret;
1407
Alex Elder57cfc102012-06-26 12:57:03 -07001408 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_NOTIFY, payload_len);
1409 if (!ops)
1410 return -ENOMEM;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001411
Alex Elder0ce1a792012-07-03 16:01:18 -05001412 info.rbd_dev = rbd_dev;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001413
1414 ret = ceph_osdc_create_event(osdc, rbd_notify_cb, 1,
1415 (void *)&info, &event);
1416 if (ret < 0)
1417 goto fail;
1418
1419 ops[0].watch.ver = 1;
1420 ops[0].watch.flag = 1;
1421 ops[0].watch.cookie = event->cookie;
1422 ops[0].watch.prot_ver = RADOS_NOTIFY_VER;
1423 ops[0].watch.timeout = 12;
1424
Alex Elder0ce1a792012-07-03 16:01:18 -05001425 ret = rbd_req_sync_op(rbd_dev, NULL,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001426 CEPH_NOSNAP,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001427 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1428 ops,
Alex Elder4cb16252012-07-25 09:32:40 -05001429 rbd_dev->header_name,
1430 0, 0, NULL, NULL, NULL);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001431 if (ret < 0)
1432 goto fail_event;
1433
1434 ret = ceph_osdc_wait_event(event, CEPH_OSD_TIMEOUT_DEFAULT);
1435 dout("ceph_osdc_wait_event returned %d\n", ret);
1436 rbd_destroy_ops(ops);
1437 return 0;
1438
1439fail_event:
1440 ceph_osdc_cancel_event(event);
1441fail:
1442 rbd_destroy_ops(ops);
1443 return ret;
1444}
1445
1446/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001447 * Request sync osd read
1448 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001449static int rbd_req_sync_exec(struct rbd_device *rbd_dev,
Alex Elderaded07e2012-07-03 16:01:18 -05001450 const char *object_name,
1451 const char *class_name,
1452 const char *method_name,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001453 const char *data,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001454 int len,
1455 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001456{
1457 struct ceph_osd_req_op *ops;
Alex Elderaded07e2012-07-03 16:01:18 -05001458 int class_name_len = strlen(class_name);
1459 int method_name_len = strlen(method_name);
Alex Elder57cfc102012-06-26 12:57:03 -07001460 int ret;
1461
1462 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_CALL,
Alex Elderaded07e2012-07-03 16:01:18 -05001463 class_name_len + method_name_len + len);
Alex Elder57cfc102012-06-26 12:57:03 -07001464 if (!ops)
1465 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001466
Alex Elderaded07e2012-07-03 16:01:18 -05001467 ops[0].cls.class_name = class_name;
1468 ops[0].cls.class_len = (__u8) class_name_len;
1469 ops[0].cls.method_name = method_name;
1470 ops[0].cls.method_len = (__u8) method_name_len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001471 ops[0].cls.argc = 0;
1472 ops[0].cls.indata = data;
1473 ops[0].cls.indata_len = len;
1474
Alex Elder0ce1a792012-07-03 16:01:18 -05001475 ret = rbd_req_sync_op(rbd_dev, NULL,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001476 CEPH_NOSNAP,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001477 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1478 ops,
Alex Elderd1f57ea2012-06-26 12:57:03 -07001479 object_name, 0, 0, NULL, NULL, ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001480
1481 rbd_destroy_ops(ops);
1482
1483 dout("cls_exec returned %d\n", ret);
1484 return ret;
1485}
1486
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001487static struct rbd_req_coll *rbd_alloc_coll(int num_reqs)
1488{
1489 struct rbd_req_coll *coll =
1490 kzalloc(sizeof(struct rbd_req_coll) +
1491 sizeof(struct rbd_req_status) * num_reqs,
1492 GFP_ATOMIC);
1493
1494 if (!coll)
1495 return NULL;
1496 coll->total = num_reqs;
1497 kref_init(&coll->kref);
1498 return coll;
1499}
1500
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001501/*
1502 * block device queue callback
1503 */
1504static void rbd_rq_fn(struct request_queue *q)
1505{
1506 struct rbd_device *rbd_dev = q->queuedata;
1507 struct request *rq;
1508 struct bio_pair *bp = NULL;
1509
Alex Elder00f1f362012-02-07 12:03:36 -06001510 while ((rq = blk_fetch_request(q))) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001511 struct bio *bio;
1512 struct bio *rq_bio, *next_bio = NULL;
1513 bool do_write;
Alex Elderbd919d42012-07-13 20:35:11 -05001514 unsigned int size;
1515 u64 op_size = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001516 u64 ofs;
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001517 int num_segs, cur_seg = 0;
1518 struct rbd_req_coll *coll;
Josh Durgind1d25642011-12-05 14:03:05 -08001519 struct ceph_snap_context *snapc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001520
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001521 dout("fetched request\n");
1522
1523 /* filter out block requests we don't understand */
1524 if ((rq->cmd_type != REQ_TYPE_FS)) {
1525 __blk_end_request_all(rq, 0);
Alex Elder00f1f362012-02-07 12:03:36 -06001526 continue;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001527 }
1528
1529 /* deduce our operation (read, write) */
1530 do_write = (rq_data_dir(rq) == WRITE);
1531
1532 size = blk_rq_bytes(rq);
Alex Elder593a9e72012-02-07 12:03:37 -06001533 ofs = blk_rq_pos(rq) * SECTOR_SIZE;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001534 rq_bio = rq->bio;
Alex Elderf84344f2012-08-31 17:29:51 -05001535 if (do_write && rbd_dev->mapping.read_only) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001536 __blk_end_request_all(rq, -EROFS);
Alex Elder00f1f362012-02-07 12:03:36 -06001537 continue;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001538 }
1539
1540 spin_unlock_irq(q->queue_lock);
1541
Josh Durgind1d25642011-12-05 14:03:05 -08001542 down_read(&rbd_dev->header_rwsem);
Josh Durgine88a36e2011-11-21 18:14:25 -08001543
Alex Elderf84344f2012-08-31 17:29:51 -05001544 if (rbd_dev->mapping.snap_id != CEPH_NOSNAP &&
1545 !rbd_dev->mapping.snap_exists) {
Josh Durgine88a36e2011-11-21 18:14:25 -08001546 up_read(&rbd_dev->header_rwsem);
Josh Durgind1d25642011-12-05 14:03:05 -08001547 dout("request for non-existent snapshot");
1548 spin_lock_irq(q->queue_lock);
1549 __blk_end_request_all(rq, -ENXIO);
1550 continue;
Josh Durgine88a36e2011-11-21 18:14:25 -08001551 }
1552
Josh Durgind1d25642011-12-05 14:03:05 -08001553 snapc = ceph_get_snap_context(rbd_dev->header.snapc);
1554
1555 up_read(&rbd_dev->header_rwsem);
1556
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001557 dout("%s 0x%x bytes at 0x%llx\n",
1558 do_write ? "write" : "read",
Alex Elderbd919d42012-07-13 20:35:11 -05001559 size, (unsigned long long) blk_rq_pos(rq) * SECTOR_SIZE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001560
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001561 num_segs = rbd_get_num_segments(&rbd_dev->header, ofs, size);
Alex Elderdf111be2012-08-09 10:33:26 -07001562 if (num_segs <= 0) {
1563 spin_lock_irq(q->queue_lock);
1564 __blk_end_request_all(rq, num_segs);
1565 ceph_put_snap_context(snapc);
1566 continue;
1567 }
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001568 coll = rbd_alloc_coll(num_segs);
1569 if (!coll) {
1570 spin_lock_irq(q->queue_lock);
1571 __blk_end_request_all(rq, -ENOMEM);
Josh Durgind1d25642011-12-05 14:03:05 -08001572 ceph_put_snap_context(snapc);
Alex Elder00f1f362012-02-07 12:03:36 -06001573 continue;
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001574 }
1575
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001576 do {
1577 /* a bio clone to be passed down to OSD req */
Alex Elderbd919d42012-07-13 20:35:11 -05001578 dout("rq->bio->bi_vcnt=%hu\n", rq->bio->bi_vcnt);
Alex Elder65ccfe22012-08-09 10:33:26 -07001579 op_size = rbd_segment_length(rbd_dev, ofs, size);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001580 kref_get(&coll->kref);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001581 bio = bio_chain_clone(&rq_bio, &next_bio, &bp,
1582 op_size, GFP_ATOMIC);
1583 if (!bio) {
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001584 rbd_coll_end_req_index(rq, coll, cur_seg,
1585 -ENOMEM, op_size);
1586 goto next_seg;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001587 }
1588
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001589
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001590 /* init OSD command: write or read */
1591 if (do_write)
1592 rbd_req_write(rq, rbd_dev,
Josh Durgind1d25642011-12-05 14:03:05 -08001593 snapc,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001594 ofs,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001595 op_size, bio,
1596 coll, cur_seg);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001597 else
1598 rbd_req_read(rq, rbd_dev,
Alex Elderf84344f2012-08-31 17:29:51 -05001599 rbd_dev->mapping.snap_id,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001600 ofs,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001601 op_size, bio,
1602 coll, cur_seg);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001603
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001604next_seg:
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001605 size -= op_size;
1606 ofs += op_size;
1607
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001608 cur_seg++;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001609 rq_bio = next_bio;
1610 } while (size > 0);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001611 kref_put(&coll->kref, rbd_coll_release);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001612
1613 if (bp)
1614 bio_pair_release(bp);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001615 spin_lock_irq(q->queue_lock);
Josh Durgind1d25642011-12-05 14:03:05 -08001616
1617 ceph_put_snap_context(snapc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001618 }
1619}
1620
1621/*
1622 * a queue callback. Makes sure that we don't create a bio that spans across
1623 * multiple osd objects. One exception would be with a single page bios,
1624 * which we handle later at bio_chain_clone
1625 */
1626static int rbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd,
1627 struct bio_vec *bvec)
1628{
1629 struct rbd_device *rbd_dev = q->queuedata;
Alex Elder593a9e72012-02-07 12:03:37 -06001630 unsigned int chunk_sectors;
1631 sector_t sector;
1632 unsigned int bio_sectors;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001633 int max;
1634
Alex Elder593a9e72012-02-07 12:03:37 -06001635 chunk_sectors = 1 << (rbd_dev->header.obj_order - SECTOR_SHIFT);
1636 sector = bmd->bi_sector + get_start_sect(bmd->bi_bdev);
1637 bio_sectors = bmd->bi_size >> SECTOR_SHIFT;
1638
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001639 max = (chunk_sectors - ((sector & (chunk_sectors - 1))
Alex Elder593a9e72012-02-07 12:03:37 -06001640 + bio_sectors)) << SECTOR_SHIFT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001641 if (max < 0)
1642 max = 0; /* bio_add cannot handle a negative return */
1643 if (max <= bvec->bv_len && bio_sectors == 0)
1644 return bvec->bv_len;
1645 return max;
1646}
1647
1648static void rbd_free_disk(struct rbd_device *rbd_dev)
1649{
1650 struct gendisk *disk = rbd_dev->disk;
1651
1652 if (!disk)
1653 return;
1654
1655 rbd_header_free(&rbd_dev->header);
1656
1657 if (disk->flags & GENHD_FL_UP)
1658 del_gendisk(disk);
1659 if (disk->queue)
1660 blk_cleanup_queue(disk->queue);
1661 put_disk(disk);
1662}
1663
1664/*
Alex Elder4156d992012-08-02 11:29:46 -05001665 * Read the complete header for the given rbd device.
1666 *
1667 * Returns a pointer to a dynamically-allocated buffer containing
1668 * the complete and validated header. Caller can pass the address
1669 * of a variable that will be filled in with the version of the
1670 * header object at the time it was read.
1671 *
1672 * Returns a pointer-coded errno if a failure occurs.
1673 */
1674static struct rbd_image_header_ondisk *
1675rbd_dev_v1_header_read(struct rbd_device *rbd_dev, u64 *version)
1676{
1677 struct rbd_image_header_ondisk *ondisk = NULL;
1678 u32 snap_count = 0;
1679 u64 names_size = 0;
1680 u32 want_count;
1681 int ret;
1682
1683 /*
1684 * The complete header will include an array of its 64-bit
1685 * snapshot ids, followed by the names of those snapshots as
1686 * a contiguous block of NUL-terminated strings. Note that
1687 * the number of snapshots could change by the time we read
1688 * it in, in which case we re-read it.
1689 */
1690 do {
1691 size_t size;
1692
1693 kfree(ondisk);
1694
1695 size = sizeof (*ondisk);
1696 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
1697 size += names_size;
1698 ondisk = kmalloc(size, GFP_KERNEL);
1699 if (!ondisk)
1700 return ERR_PTR(-ENOMEM);
1701
1702 ret = rbd_req_sync_read(rbd_dev, CEPH_NOSNAP,
1703 rbd_dev->header_name,
1704 0, size,
1705 (char *) ondisk, version);
1706
1707 if (ret < 0)
1708 goto out_err;
1709 if (WARN_ON((size_t) ret < size)) {
1710 ret = -ENXIO;
1711 pr_warning("short header read for image %s"
1712 " (want %zd got %d)\n",
1713 rbd_dev->image_name, size, ret);
1714 goto out_err;
1715 }
1716 if (!rbd_dev_ondisk_valid(ondisk)) {
1717 ret = -ENXIO;
1718 pr_warning("invalid header for image %s\n",
1719 rbd_dev->image_name);
1720 goto out_err;
1721 }
1722
1723 names_size = le64_to_cpu(ondisk->snap_names_len);
1724 want_count = snap_count;
1725 snap_count = le32_to_cpu(ondisk->snap_count);
1726 } while (snap_count != want_count);
1727
1728 return ondisk;
1729
1730out_err:
1731 kfree(ondisk);
1732
1733 return ERR_PTR(ret);
1734}
1735
1736/*
1737 * reload the ondisk the header
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001738 */
1739static int rbd_read_header(struct rbd_device *rbd_dev,
1740 struct rbd_image_header *header)
1741{
Alex Elder4156d992012-08-02 11:29:46 -05001742 struct rbd_image_header_ondisk *ondisk;
1743 u64 ver = 0;
1744 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001745
Alex Elder4156d992012-08-02 11:29:46 -05001746 ondisk = rbd_dev_v1_header_read(rbd_dev, &ver);
1747 if (IS_ERR(ondisk))
1748 return PTR_ERR(ondisk);
1749 ret = rbd_header_from_disk(header, ondisk);
1750 if (ret >= 0)
1751 header->obj_version = ver;
1752 kfree(ondisk);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001753
Alex Elder4156d992012-08-02 11:29:46 -05001754 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001755}
1756
1757/*
1758 * create a snapshot
1759 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001760static int rbd_header_add_snap(struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001761 const char *snap_name,
1762 gfp_t gfp_flags)
1763{
1764 int name_len = strlen(snap_name);
1765 u64 new_snapid;
1766 int ret;
Sage Weil916d4d62011-05-12 16:10:50 -07001767 void *data, *p, *e;
Alex Elder1dbb4392012-01-24 10:08:37 -06001768 struct ceph_mon_client *monc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001769
1770 /* we should create a snapshot only if we're pointing at the head */
Alex Elderf84344f2012-08-31 17:29:51 -05001771 if (rbd_dev->mapping.snap_id != CEPH_NOSNAP)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001772 return -EINVAL;
1773
Alex Elder0ce1a792012-07-03 16:01:18 -05001774 monc = &rbd_dev->rbd_client->client->monc;
1775 ret = ceph_monc_create_snapid(monc, rbd_dev->pool_id, &new_snapid);
Alex Elderbd919d42012-07-13 20:35:11 -05001776 dout("created snapid=%llu\n", (unsigned long long) new_snapid);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001777 if (ret < 0)
1778 return ret;
1779
1780 data = kmalloc(name_len + 16, gfp_flags);
1781 if (!data)
1782 return -ENOMEM;
1783
Sage Weil916d4d62011-05-12 16:10:50 -07001784 p = data;
1785 e = data + name_len + 16;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001786
Sage Weil916d4d62011-05-12 16:10:50 -07001787 ceph_encode_string_safe(&p, e, snap_name, name_len, bad);
1788 ceph_encode_64_safe(&p, e, new_snapid, bad);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001789
Alex Elder0bed54d2012-07-03 16:01:18 -05001790 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
Alex Elder0ce1a792012-07-03 16:01:18 -05001791 "rbd", "snap_add",
Alex Elderd67d4be2012-07-13 20:35:11 -05001792 data, p - data, NULL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001793
Sage Weil916d4d62011-05-12 16:10:50 -07001794 kfree(data);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001795
Alex Elder505cbb92012-07-19 08:49:18 -05001796 return ret < 0 ? ret : 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001797bad:
1798 return -ERANGE;
1799}
1800
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001801static void __rbd_remove_all_snaps(struct rbd_device *rbd_dev)
1802{
1803 struct rbd_snap *snap;
Alex Eldera0593292012-07-19 09:09:27 -05001804 struct rbd_snap *next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001805
Alex Eldera0593292012-07-19 09:09:27 -05001806 list_for_each_entry_safe(snap, next, &rbd_dev->snaps, node)
Alex Elder14e70852012-07-19 09:09:27 -05001807 __rbd_remove_snap_dev(snap);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001808}
1809
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001810/*
1811 * only read the first part of the ondisk header, without the snaps info
1812 */
Alex Elderb8136232012-07-25 09:32:41 -05001813static int __rbd_refresh_header(struct rbd_device *rbd_dev, u64 *hver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001814{
1815 int ret;
1816 struct rbd_image_header h;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001817
1818 ret = rbd_read_header(rbd_dev, &h);
1819 if (ret < 0)
1820 return ret;
1821
Josh Durgina51aa0c2011-12-05 10:35:04 -08001822 down_write(&rbd_dev->header_rwsem);
1823
Sage Weil9db4b3e2011-04-19 22:49:06 -07001824 /* resized? */
Alex Elderf84344f2012-08-31 17:29:51 -05001825 if (rbd_dev->mapping.snap_id == CEPH_NOSNAP) {
Josh Durgin474ef7c2011-11-21 17:13:54 -08001826 sector_t size = (sector_t) h.image_size / SECTOR_SIZE;
1827
Alex Elder99c1f082012-08-30 14:42:15 -05001828 if (size != (sector_t) rbd_dev->mapping.size) {
1829 dout("setting size to %llu sectors",
1830 (unsigned long long) size);
1831 rbd_dev->mapping.size = (u64) size;
1832 set_capacity(rbd_dev->disk, size);
1833 }
Josh Durgin474ef7c2011-11-21 17:13:54 -08001834 }
Sage Weil9db4b3e2011-04-19 22:49:06 -07001835
Alex Elder849b4262012-07-09 21:04:24 -05001836 /* rbd_dev->header.object_prefix shouldn't change */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001837 kfree(rbd_dev->header.snap_sizes);
Alex Elder849b4262012-07-09 21:04:24 -05001838 kfree(rbd_dev->header.snap_names);
Josh Durgind1d25642011-12-05 14:03:05 -08001839 /* osd requests may still refer to snapc */
1840 ceph_put_snap_context(rbd_dev->header.snapc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001841
Alex Elderb8136232012-07-25 09:32:41 -05001842 if (hver)
1843 *hver = h.obj_version;
Josh Durgina71b8912011-12-05 18:10:44 -08001844 rbd_dev->header.obj_version = h.obj_version;
Josh Durgin93a24e02011-12-05 10:41:28 -08001845 rbd_dev->header.image_size = h.image_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001846 rbd_dev->header.snapc = h.snapc;
1847 rbd_dev->header.snap_names = h.snap_names;
1848 rbd_dev->header.snap_sizes = h.snap_sizes;
Alex Elder849b4262012-07-09 21:04:24 -05001849 /* Free the extra copy of the object prefix */
1850 WARN_ON(strcmp(rbd_dev->header.object_prefix, h.object_prefix));
1851 kfree(h.object_prefix);
1852
Alex Elder9fcbb802012-08-23 23:48:49 -05001853 ret = rbd_dev_snap_devs_update(rbd_dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001854
Josh Durginc6666012011-11-21 17:11:12 -08001855 up_write(&rbd_dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001856
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001857 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001858}
1859
Alex Elder1fe5e992012-07-25 09:32:41 -05001860static int rbd_refresh_header(struct rbd_device *rbd_dev, u64 *hver)
1861{
1862 int ret;
1863
1864 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
1865 ret = __rbd_refresh_header(rbd_dev, hver);
1866 mutex_unlock(&ctl_mutex);
1867
1868 return ret;
1869}
1870
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001871static int rbd_init_disk(struct rbd_device *rbd_dev)
1872{
1873 struct gendisk *disk;
1874 struct request_queue *q;
1875 int rc;
Alex Elder593a9e72012-02-07 12:03:37 -06001876 u64 segment_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001877
1878 /* contact OSD, request size info about the object being mapped */
1879 rc = rbd_read_header(rbd_dev, &rbd_dev->header);
1880 if (rc)
1881 return rc;
1882
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001883 /* no need to lock here, as rbd_dev is not registered yet */
Alex Elder9fcbb802012-08-23 23:48:49 -05001884 rc = rbd_dev_snap_devs_update(rbd_dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001885 if (rc)
1886 return rc;
1887
Alex Elder4e1105a2012-08-31 17:29:52 -05001888 rc = rbd_header_set_snap(rbd_dev, snap_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001889 if (rc)
1890 return rc;
1891
1892 /* create gendisk info */
1893 rc = -ENOMEM;
1894 disk = alloc_disk(RBD_MINORS_PER_MAJOR);
1895 if (!disk)
1896 goto out;
1897
Alex Elderf0f8cef2012-01-29 13:57:44 -06001898 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
Alex Elderde71a292012-07-03 16:01:19 -05001899 rbd_dev->dev_id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001900 disk->major = rbd_dev->major;
1901 disk->first_minor = 0;
1902 disk->fops = &rbd_bd_ops;
1903 disk->private_data = rbd_dev;
1904
1905 /* init rq */
1906 rc = -ENOMEM;
1907 q = blk_init_queue(rbd_rq_fn, &rbd_dev->lock);
1908 if (!q)
1909 goto out_disk;
Josh Durgin029bcbd2011-07-22 11:35:23 -07001910
Alex Elder593a9e72012-02-07 12:03:37 -06001911 /* We use the default size, but let's be explicit about it. */
1912 blk_queue_physical_block_size(q, SECTOR_SIZE);
1913
Josh Durgin029bcbd2011-07-22 11:35:23 -07001914 /* set io sizes to object size */
Alex Elder593a9e72012-02-07 12:03:37 -06001915 segment_size = rbd_obj_bytes(&rbd_dev->header);
1916 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
1917 blk_queue_max_segment_size(q, segment_size);
1918 blk_queue_io_min(q, segment_size);
1919 blk_queue_io_opt(q, segment_size);
Josh Durgin029bcbd2011-07-22 11:35:23 -07001920
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001921 blk_queue_merge_bvec(q, rbd_merge_bvec);
1922 disk->queue = q;
1923
1924 q->queuedata = rbd_dev;
1925
1926 rbd_dev->disk = disk;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001927
1928 /* finally, announce the disk to the world */
Alex Elder99c1f082012-08-30 14:42:15 -05001929 set_capacity(disk, (sector_t) rbd_dev->mapping.size / SECTOR_SIZE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001930 add_disk(disk);
1931
1932 pr_info("%s: added with size 0x%llx\n",
Alex Elder99c1f082012-08-30 14:42:15 -05001933 disk->disk_name, (unsigned long long) rbd_dev->mapping.size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001934 return 0;
1935
1936out_disk:
1937 put_disk(disk);
1938out:
1939 return rc;
1940}
1941
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001942/*
1943 sysfs
1944*/
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001945
Alex Elder593a9e72012-02-07 12:03:37 -06001946static struct rbd_device *dev_to_rbd_dev(struct device *dev)
1947{
1948 return container_of(dev, struct rbd_device, dev);
1949}
1950
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001951static ssize_t rbd_size_show(struct device *dev,
1952 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001953{
Alex Elder593a9e72012-02-07 12:03:37 -06001954 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Josh Durgina51aa0c2011-12-05 10:35:04 -08001955 sector_t size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001956
Josh Durgina51aa0c2011-12-05 10:35:04 -08001957 down_read(&rbd_dev->header_rwsem);
1958 size = get_capacity(rbd_dev->disk);
1959 up_read(&rbd_dev->header_rwsem);
1960
1961 return sprintf(buf, "%llu\n", (unsigned long long) size * SECTOR_SIZE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001962}
1963
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001964static ssize_t rbd_major_show(struct device *dev,
1965 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001966{
Alex Elder593a9e72012-02-07 12:03:37 -06001967 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001968
1969 return sprintf(buf, "%d\n", rbd_dev->major);
1970}
1971
1972static ssize_t rbd_client_id_show(struct device *dev,
1973 struct device_attribute *attr, char *buf)
1974{
Alex Elder593a9e72012-02-07 12:03:37 -06001975 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001976
Alex Elder1dbb4392012-01-24 10:08:37 -06001977 return sprintf(buf, "client%lld\n",
1978 ceph_client_id(rbd_dev->rbd_client->client));
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001979}
1980
1981static ssize_t rbd_pool_show(struct device *dev,
1982 struct device_attribute *attr, char *buf)
1983{
Alex Elder593a9e72012-02-07 12:03:37 -06001984 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001985
1986 return sprintf(buf, "%s\n", rbd_dev->pool_name);
1987}
1988
Alex Elder9bb2f332012-07-12 10:46:35 -05001989static ssize_t rbd_pool_id_show(struct device *dev,
1990 struct device_attribute *attr, char *buf)
1991{
1992 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
1993
1994 return sprintf(buf, "%d\n", rbd_dev->pool_id);
1995}
1996
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001997static ssize_t rbd_name_show(struct device *dev,
1998 struct device_attribute *attr, char *buf)
1999{
Alex Elder593a9e72012-02-07 12:03:37 -06002000 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002001
Alex Elder0bed54d2012-07-03 16:01:18 -05002002 return sprintf(buf, "%s\n", rbd_dev->image_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002003}
2004
2005static ssize_t rbd_snap_show(struct device *dev,
2006 struct device_attribute *attr,
2007 char *buf)
2008{
Alex Elder593a9e72012-02-07 12:03:37 -06002009 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002010
Alex Elderf84344f2012-08-31 17:29:51 -05002011 return sprintf(buf, "%s\n", rbd_dev->mapping.snap_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002012}
2013
2014static ssize_t rbd_image_refresh(struct device *dev,
2015 struct device_attribute *attr,
2016 const char *buf,
2017 size_t size)
2018{
Alex Elder593a9e72012-02-07 12:03:37 -06002019 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Alex Elderb8136232012-07-25 09:32:41 -05002020 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002021
Alex Elder1fe5e992012-07-25 09:32:41 -05002022 ret = rbd_refresh_header(rbd_dev, NULL);
Alex Elderb8136232012-07-25 09:32:41 -05002023
2024 return ret < 0 ? ret : size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002025}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002026
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002027static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
2028static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
2029static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
2030static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
Alex Elder9bb2f332012-07-12 10:46:35 -05002031static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002032static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
2033static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
2034static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
2035static DEVICE_ATTR(create_snap, S_IWUSR, NULL, rbd_snap_add);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002036
2037static struct attribute *rbd_attrs[] = {
2038 &dev_attr_size.attr,
2039 &dev_attr_major.attr,
2040 &dev_attr_client_id.attr,
2041 &dev_attr_pool.attr,
Alex Elder9bb2f332012-07-12 10:46:35 -05002042 &dev_attr_pool_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002043 &dev_attr_name.attr,
2044 &dev_attr_current_snap.attr,
2045 &dev_attr_refresh.attr,
2046 &dev_attr_create_snap.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002047 NULL
2048};
2049
2050static struct attribute_group rbd_attr_group = {
2051 .attrs = rbd_attrs,
2052};
2053
2054static const struct attribute_group *rbd_attr_groups[] = {
2055 &rbd_attr_group,
2056 NULL
2057};
2058
2059static void rbd_sysfs_dev_release(struct device *dev)
2060{
2061}
2062
2063static struct device_type rbd_device_type = {
2064 .name = "rbd",
2065 .groups = rbd_attr_groups,
2066 .release = rbd_sysfs_dev_release,
2067};
2068
2069
2070/*
2071 sysfs - snapshots
2072*/
2073
2074static ssize_t rbd_snap_size_show(struct device *dev,
2075 struct device_attribute *attr,
2076 char *buf)
2077{
2078 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2079
Josh Durgin35915382011-12-05 18:25:13 -08002080 return sprintf(buf, "%llu\n", (unsigned long long)snap->size);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002081}
2082
2083static ssize_t rbd_snap_id_show(struct device *dev,
2084 struct device_attribute *attr,
2085 char *buf)
2086{
2087 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2088
Josh Durgin35915382011-12-05 18:25:13 -08002089 return sprintf(buf, "%llu\n", (unsigned long long)snap->id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002090}
2091
2092static DEVICE_ATTR(snap_size, S_IRUGO, rbd_snap_size_show, NULL);
2093static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL);
2094
2095static struct attribute *rbd_snap_attrs[] = {
2096 &dev_attr_snap_size.attr,
2097 &dev_attr_snap_id.attr,
2098 NULL,
2099};
2100
2101static struct attribute_group rbd_snap_attr_group = {
2102 .attrs = rbd_snap_attrs,
2103};
2104
2105static void rbd_snap_dev_release(struct device *dev)
2106{
2107 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2108 kfree(snap->name);
2109 kfree(snap);
2110}
2111
2112static const struct attribute_group *rbd_snap_attr_groups[] = {
2113 &rbd_snap_attr_group,
2114 NULL
2115};
2116
2117static struct device_type rbd_snap_device_type = {
2118 .groups = rbd_snap_attr_groups,
2119 .release = rbd_snap_dev_release,
2120};
2121
Alex Elder14e70852012-07-19 09:09:27 -05002122static void __rbd_remove_snap_dev(struct rbd_snap *snap)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002123{
2124 list_del(&snap->node);
2125 device_unregister(&snap->dev);
2126}
2127
Alex Elder14e70852012-07-19 09:09:27 -05002128static int rbd_register_snap_dev(struct rbd_snap *snap,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002129 struct device *parent)
2130{
2131 struct device *dev = &snap->dev;
2132 int ret;
2133
2134 dev->type = &rbd_snap_device_type;
2135 dev->parent = parent;
2136 dev->release = rbd_snap_dev_release;
2137 dev_set_name(dev, "snap_%s", snap->name);
2138 ret = device_register(dev);
2139
2140 return ret;
2141}
2142
Alex Elder4e891e02012-07-10 20:30:10 -05002143static struct rbd_snap *__rbd_add_snap_dev(struct rbd_device *rbd_dev,
2144 int i, const char *name)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002145{
Alex Elder4e891e02012-07-10 20:30:10 -05002146 struct rbd_snap *snap;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002147 int ret;
Alex Elder4e891e02012-07-10 20:30:10 -05002148
2149 snap = kzalloc(sizeof (*snap), GFP_KERNEL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002150 if (!snap)
Alex Elder4e891e02012-07-10 20:30:10 -05002151 return ERR_PTR(-ENOMEM);
2152
2153 ret = -ENOMEM;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002154 snap->name = kstrdup(name, GFP_KERNEL);
Alex Elder4e891e02012-07-10 20:30:10 -05002155 if (!snap->name)
2156 goto err;
2157
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002158 snap->size = rbd_dev->header.snap_sizes[i];
2159 snap->id = rbd_dev->header.snapc->snaps[i];
2160 if (device_is_registered(&rbd_dev->dev)) {
Alex Elder14e70852012-07-19 09:09:27 -05002161 ret = rbd_register_snap_dev(snap, &rbd_dev->dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002162 if (ret < 0)
2163 goto err;
2164 }
Alex Elder4e891e02012-07-10 20:30:10 -05002165
2166 return snap;
2167
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002168err:
2169 kfree(snap->name);
2170 kfree(snap);
Alex Elder4e891e02012-07-10 20:30:10 -05002171
2172 return ERR_PTR(ret);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002173}
2174
2175/*
Alex Elder35938152012-08-02 11:29:46 -05002176 * Scan the rbd device's current snapshot list and compare it to the
2177 * newly-received snapshot context. Remove any existing snapshots
2178 * not present in the new snapshot context. Add a new snapshot for
2179 * any snaphots in the snapshot context not in the current list.
2180 * And verify there are no changes to snapshots we already know
2181 * about.
2182 *
2183 * Assumes the snapshots in the snapshot context are sorted by
2184 * snapshot id, highest id first. (Snapshots in the rbd_dev's list
2185 * are also maintained in that order.)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002186 */
Alex Elder9fcbb802012-08-23 23:48:49 -05002187static int rbd_dev_snap_devs_update(struct rbd_device *rbd_dev)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002188{
Alex Elder35938152012-08-02 11:29:46 -05002189 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
2190 const u32 snap_count = snapc->num_snaps;
2191 char *snap_name = rbd_dev->header.snap_names;
2192 struct list_head *head = &rbd_dev->snaps;
2193 struct list_head *links = head->next;
2194 u32 index = 0;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002195
Alex Elder9fcbb802012-08-23 23:48:49 -05002196 dout("%s: snap count is %u\n", __func__, (unsigned int) snap_count);
Alex Elder35938152012-08-02 11:29:46 -05002197 while (index < snap_count || links != head) {
2198 u64 snap_id;
2199 struct rbd_snap *snap;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002200
Alex Elder35938152012-08-02 11:29:46 -05002201 snap_id = index < snap_count ? snapc->snaps[index]
2202 : CEPH_NOSNAP;
2203 snap = links != head ? list_entry(links, struct rbd_snap, node)
2204 : NULL;
Alex Elderaafb230e2012-09-06 16:00:54 -05002205 rbd_assert(!snap || snap->id != CEPH_NOSNAP);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002206
Alex Elder35938152012-08-02 11:29:46 -05002207 if (snap_id == CEPH_NOSNAP || (snap && snap->id > snap_id)) {
2208 struct list_head *next = links->next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002209
Alex Elder35938152012-08-02 11:29:46 -05002210 /* Existing snapshot not in the new snap context */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002211
Alex Elderf84344f2012-08-31 17:29:51 -05002212 if (rbd_dev->mapping.snap_id == snap->id)
2213 rbd_dev->mapping.snap_exists = false;
Alex Elder35938152012-08-02 11:29:46 -05002214 __rbd_remove_snap_dev(snap);
Alex Elder9fcbb802012-08-23 23:48:49 -05002215 dout("%ssnap id %llu has been removed\n",
Alex Elderf84344f2012-08-31 17:29:51 -05002216 rbd_dev->mapping.snap_id == snap->id ?
2217 "mapped " : "",
Alex Elder9fcbb802012-08-23 23:48:49 -05002218 (unsigned long long) snap->id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002219
Alex Elder35938152012-08-02 11:29:46 -05002220 /* Done with this list entry; advance */
2221
2222 links = next;
2223 continue;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002224 }
Alex Elder35938152012-08-02 11:29:46 -05002225
Alex Elder9fcbb802012-08-23 23:48:49 -05002226 dout("entry %u: snap_id = %llu\n", (unsigned int) snap_count,
2227 (unsigned long long) snap_id);
Alex Elder35938152012-08-02 11:29:46 -05002228 if (!snap || (snap_id != CEPH_NOSNAP && snap->id < snap_id)) {
2229 struct rbd_snap *new_snap;
2230
2231 /* We haven't seen this snapshot before */
2232
2233 new_snap = __rbd_add_snap_dev(rbd_dev, index,
2234 snap_name);
Alex Elder9fcbb802012-08-23 23:48:49 -05002235 if (IS_ERR(new_snap)) {
2236 int err = PTR_ERR(new_snap);
2237
2238 dout(" failed to add dev, error %d\n", err);
2239
2240 return err;
2241 }
Alex Elder35938152012-08-02 11:29:46 -05002242
2243 /* New goes before existing, or at end of list */
2244
Alex Elder9fcbb802012-08-23 23:48:49 -05002245 dout(" added dev%s\n", snap ? "" : " at end\n");
Alex Elder35938152012-08-02 11:29:46 -05002246 if (snap)
2247 list_add_tail(&new_snap->node, &snap->node);
2248 else
Alex Elder523f3252012-08-30 00:16:37 -05002249 list_add_tail(&new_snap->node, head);
Alex Elder35938152012-08-02 11:29:46 -05002250 } else {
2251 /* Already have this one */
2252
Alex Elder9fcbb802012-08-23 23:48:49 -05002253 dout(" already present\n");
2254
Alex Elderaafb230e2012-09-06 16:00:54 -05002255 rbd_assert(snap->size ==
2256 rbd_dev->header.snap_sizes[index]);
2257 rbd_assert(!strcmp(snap->name, snap_name));
Alex Elder35938152012-08-02 11:29:46 -05002258
2259 /* Done with this list entry; advance */
2260
2261 links = links->next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002262 }
Alex Elder35938152012-08-02 11:29:46 -05002263
2264 /* Advance to the next entry in the snapshot context */
2265
2266 index++;
2267 snap_name += strlen(snap_name) + 1;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002268 }
Alex Elder9fcbb802012-08-23 23:48:49 -05002269 dout("%s: done\n", __func__);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002270
2271 return 0;
2272}
2273
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002274static int rbd_bus_add_dev(struct rbd_device *rbd_dev)
2275{
Alex Elderf0f8cef2012-01-29 13:57:44 -06002276 int ret;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002277 struct device *dev;
2278 struct rbd_snap *snap;
2279
2280 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2281 dev = &rbd_dev->dev;
2282
2283 dev->bus = &rbd_bus_type;
2284 dev->type = &rbd_device_type;
2285 dev->parent = &rbd_root_dev;
2286 dev->release = rbd_dev_release;
Alex Elderde71a292012-07-03 16:01:19 -05002287 dev_set_name(dev, "%d", rbd_dev->dev_id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002288 ret = device_register(dev);
2289 if (ret < 0)
Alex Elderf0f8cef2012-01-29 13:57:44 -06002290 goto out;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002291
2292 list_for_each_entry(snap, &rbd_dev->snaps, node) {
Alex Elder14e70852012-07-19 09:09:27 -05002293 ret = rbd_register_snap_dev(snap, &rbd_dev->dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002294 if (ret < 0)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002295 break;
2296 }
Alex Elderf0f8cef2012-01-29 13:57:44 -06002297out:
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002298 mutex_unlock(&ctl_mutex);
2299 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002300}
2301
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002302static void rbd_bus_del_dev(struct rbd_device *rbd_dev)
2303{
2304 device_unregister(&rbd_dev->dev);
2305}
2306
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002307static int rbd_init_watch_dev(struct rbd_device *rbd_dev)
2308{
2309 int ret, rc;
2310
2311 do {
Alex Elder0e6f3222012-07-25 09:32:40 -05002312 ret = rbd_req_sync_watch(rbd_dev);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002313 if (ret == -ERANGE) {
Alex Elder1fe5e992012-07-25 09:32:41 -05002314 rc = rbd_refresh_header(rbd_dev, NULL);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002315 if (rc < 0)
2316 return rc;
2317 }
2318 } while (ret == -ERANGE);
2319
2320 return ret;
2321}
2322
Alex Eldere2839302012-08-29 17:11:06 -05002323static atomic64_t rbd_dev_id_max = ATOMIC64_INIT(0);
Alex Elder1ddbe942012-01-29 13:57:44 -06002324
2325/*
Alex Elder499afd52012-02-02 08:13:29 -06002326 * Get a unique rbd identifier for the given new rbd_dev, and add
2327 * the rbd_dev to the global list. The minimum rbd id is 1.
Alex Elder1ddbe942012-01-29 13:57:44 -06002328 */
Alex Eldere2839302012-08-29 17:11:06 -05002329static void rbd_dev_id_get(struct rbd_device *rbd_dev)
Alex Elderb7f23c32012-01-29 13:57:43 -06002330{
Alex Eldere2839302012-08-29 17:11:06 -05002331 rbd_dev->dev_id = atomic64_inc_return(&rbd_dev_id_max);
Alex Elder499afd52012-02-02 08:13:29 -06002332
2333 spin_lock(&rbd_dev_list_lock);
2334 list_add_tail(&rbd_dev->node, &rbd_dev_list);
2335 spin_unlock(&rbd_dev_list_lock);
Alex Eldere2839302012-08-29 17:11:06 -05002336 dout("rbd_dev %p given dev id %llu\n", rbd_dev,
2337 (unsigned long long) rbd_dev->dev_id);
Alex Elder1ddbe942012-01-29 13:57:44 -06002338}
Alex Elderb7f23c32012-01-29 13:57:43 -06002339
Alex Elder1ddbe942012-01-29 13:57:44 -06002340/*
Alex Elder499afd52012-02-02 08:13:29 -06002341 * Remove an rbd_dev from the global list, and record that its
2342 * identifier is no longer in use.
Alex Elder1ddbe942012-01-29 13:57:44 -06002343 */
Alex Eldere2839302012-08-29 17:11:06 -05002344static void rbd_dev_id_put(struct rbd_device *rbd_dev)
Alex Elder1ddbe942012-01-29 13:57:44 -06002345{
Alex Elderd184f6b2012-01-29 13:57:44 -06002346 struct list_head *tmp;
Alex Elderde71a292012-07-03 16:01:19 -05002347 int rbd_id = rbd_dev->dev_id;
Alex Elderd184f6b2012-01-29 13:57:44 -06002348 int max_id;
2349
Alex Elderaafb230e2012-09-06 16:00:54 -05002350 rbd_assert(rbd_id > 0);
Alex Elder499afd52012-02-02 08:13:29 -06002351
Alex Eldere2839302012-08-29 17:11:06 -05002352 dout("rbd_dev %p released dev id %llu\n", rbd_dev,
2353 (unsigned long long) rbd_dev->dev_id);
Alex Elder499afd52012-02-02 08:13:29 -06002354 spin_lock(&rbd_dev_list_lock);
2355 list_del_init(&rbd_dev->node);
Alex Elderd184f6b2012-01-29 13:57:44 -06002356
2357 /*
2358 * If the id being "put" is not the current maximum, there
2359 * is nothing special we need to do.
2360 */
Alex Eldere2839302012-08-29 17:11:06 -05002361 if (rbd_id != atomic64_read(&rbd_dev_id_max)) {
Alex Elderd184f6b2012-01-29 13:57:44 -06002362 spin_unlock(&rbd_dev_list_lock);
2363 return;
2364 }
2365
2366 /*
2367 * We need to update the current maximum id. Search the
2368 * list to find out what it is. We're more likely to find
2369 * the maximum at the end, so search the list backward.
2370 */
2371 max_id = 0;
2372 list_for_each_prev(tmp, &rbd_dev_list) {
2373 struct rbd_device *rbd_dev;
2374
2375 rbd_dev = list_entry(tmp, struct rbd_device, node);
2376 if (rbd_id > max_id)
2377 max_id = rbd_id;
2378 }
Alex Elder499afd52012-02-02 08:13:29 -06002379 spin_unlock(&rbd_dev_list_lock);
Alex Elderb7f23c32012-01-29 13:57:43 -06002380
Alex Elder1ddbe942012-01-29 13:57:44 -06002381 /*
Alex Eldere2839302012-08-29 17:11:06 -05002382 * The max id could have been updated by rbd_dev_id_get(), in
Alex Elderd184f6b2012-01-29 13:57:44 -06002383 * which case it now accurately reflects the new maximum.
2384 * Be careful not to overwrite the maximum value in that
2385 * case.
Alex Elder1ddbe942012-01-29 13:57:44 -06002386 */
Alex Eldere2839302012-08-29 17:11:06 -05002387 atomic64_cmpxchg(&rbd_dev_id_max, rbd_id, max_id);
2388 dout(" max dev id has been reset\n");
Alex Elderb7f23c32012-01-29 13:57:43 -06002389}
2390
Alex Eldera725f65e2012-02-02 08:13:30 -06002391/*
Alex Eldere28fff262012-02-02 08:13:30 -06002392 * Skips over white space at *buf, and updates *buf to point to the
2393 * first found non-space character (if any). Returns the length of
Alex Elder593a9e72012-02-07 12:03:37 -06002394 * the token (string of non-white space characters) found. Note
2395 * that *buf must be terminated with '\0'.
Alex Eldere28fff262012-02-02 08:13:30 -06002396 */
2397static inline size_t next_token(const char **buf)
2398{
2399 /*
2400 * These are the characters that produce nonzero for
2401 * isspace() in the "C" and "POSIX" locales.
2402 */
2403 const char *spaces = " \f\n\r\t\v";
2404
2405 *buf += strspn(*buf, spaces); /* Find start of token */
2406
2407 return strcspn(*buf, spaces); /* Return token length */
2408}
2409
2410/*
2411 * Finds the next token in *buf, and if the provided token buffer is
2412 * big enough, copies the found token into it. The result, if
Alex Elder593a9e72012-02-07 12:03:37 -06002413 * copied, is guaranteed to be terminated with '\0'. Note that *buf
2414 * must be terminated with '\0' on entry.
Alex Eldere28fff262012-02-02 08:13:30 -06002415 *
2416 * Returns the length of the token found (not including the '\0').
2417 * Return value will be 0 if no token is found, and it will be >=
2418 * token_size if the token would not fit.
2419 *
Alex Elder593a9e72012-02-07 12:03:37 -06002420 * The *buf pointer will be updated to point beyond the end of the
Alex Eldere28fff262012-02-02 08:13:30 -06002421 * found token. Note that this occurs even if the token buffer is
2422 * too small to hold it.
2423 */
2424static inline size_t copy_token(const char **buf,
2425 char *token,
2426 size_t token_size)
2427{
2428 size_t len;
2429
2430 len = next_token(buf);
2431 if (len < token_size) {
2432 memcpy(token, *buf, len);
2433 *(token + len) = '\0';
2434 }
2435 *buf += len;
2436
2437 return len;
2438}
2439
2440/*
Alex Elderea3352f2012-07-09 21:04:23 -05002441 * Finds the next token in *buf, dynamically allocates a buffer big
2442 * enough to hold a copy of it, and copies the token into the new
2443 * buffer. The copy is guaranteed to be terminated with '\0'. Note
2444 * that a duplicate buffer is created even for a zero-length token.
2445 *
2446 * Returns a pointer to the newly-allocated duplicate, or a null
2447 * pointer if memory for the duplicate was not available. If
2448 * the lenp argument is a non-null pointer, the length of the token
2449 * (not including the '\0') is returned in *lenp.
2450 *
2451 * If successful, the *buf pointer will be updated to point beyond
2452 * the end of the found token.
2453 *
2454 * Note: uses GFP_KERNEL for allocation.
2455 */
2456static inline char *dup_token(const char **buf, size_t *lenp)
2457{
2458 char *dup;
2459 size_t len;
2460
2461 len = next_token(buf);
2462 dup = kmalloc(len + 1, GFP_KERNEL);
2463 if (!dup)
2464 return NULL;
2465
2466 memcpy(dup, *buf, len);
2467 *(dup + len) = '\0';
2468 *buf += len;
2469
2470 if (lenp)
2471 *lenp = len;
2472
2473 return dup;
2474}
2475
2476/*
Alex Elder3feeb8942012-08-31 17:29:52 -05002477 * This fills in the pool_name, image_name, image_name_len, rbd_dev,
2478 * rbd_md_name, and name fields of the given rbd_dev, based on the
2479 * list of monitor addresses and other options provided via
2480 * /sys/bus/rbd/add. Returns a pointer to a dynamically-allocated
2481 * copy of the snapshot name to map if successful, or a
2482 * pointer-coded error otherwise.
Alex Elderd22f76e2012-07-12 10:46:35 -05002483 *
2484 * Note: rbd_dev is assumed to have been initially zero-filled.
Alex Eldera725f65e2012-02-02 08:13:30 -06002485 */
Alex Elder3feeb8942012-08-31 17:29:52 -05002486static char *rbd_add_parse_args(struct rbd_device *rbd_dev,
2487 const char *buf,
2488 const char **mon_addrs,
2489 size_t *mon_addrs_size,
2490 char *options,
2491 size_t options_size)
Alex Eldera725f65e2012-02-02 08:13:30 -06002492{
Alex Elderd22f76e2012-07-12 10:46:35 -05002493 size_t len;
Alex Elder3feeb8942012-08-31 17:29:52 -05002494 char *err_ptr = ERR_PTR(-EINVAL);
2495 char *snap_name;
Alex Eldere28fff262012-02-02 08:13:30 -06002496
2497 /* The first four tokens are required */
2498
Alex Elder7ef32142012-02-02 08:13:30 -06002499 len = next_token(&buf);
2500 if (!len)
Alex Elder3feeb8942012-08-31 17:29:52 -05002501 return err_ptr;
Alex Elder5214ecc2012-02-02 08:13:30 -06002502 *mon_addrs_size = len + 1;
Alex Elder7ef32142012-02-02 08:13:30 -06002503 *mon_addrs = buf;
2504
2505 buf += len;
Alex Eldera725f65e2012-02-02 08:13:30 -06002506
Alex Eldere28fff262012-02-02 08:13:30 -06002507 len = copy_token(&buf, options, options_size);
2508 if (!len || len >= options_size)
Alex Elder3feeb8942012-08-31 17:29:52 -05002509 return err_ptr;
Alex Eldera725f65e2012-02-02 08:13:30 -06002510
Alex Elder3feeb8942012-08-31 17:29:52 -05002511 err_ptr = ERR_PTR(-ENOMEM);
Alex Elderd22f76e2012-07-12 10:46:35 -05002512 rbd_dev->pool_name = dup_token(&buf, NULL);
2513 if (!rbd_dev->pool_name)
Alex Elderd22f76e2012-07-12 10:46:35 -05002514 goto out_err;
Alex Eldere28fff262012-02-02 08:13:30 -06002515
Alex Elder0bed54d2012-07-03 16:01:18 -05002516 rbd_dev->image_name = dup_token(&buf, &rbd_dev->image_name_len);
2517 if (!rbd_dev->image_name)
Alex Elderbf3e5ae2012-07-09 21:04:23 -05002518 goto out_err;
Alex Eldere28fff262012-02-02 08:13:30 -06002519
Alex Eldercb8627c2012-07-09 21:04:23 -05002520 /* Create the name of the header object */
2521
Alex Elder0bed54d2012-07-03 16:01:18 -05002522 rbd_dev->header_name = kmalloc(rbd_dev->image_name_len
Alex Elderbf3e5ae2012-07-09 21:04:23 -05002523 + sizeof (RBD_SUFFIX),
2524 GFP_KERNEL);
Alex Elder0bed54d2012-07-03 16:01:18 -05002525 if (!rbd_dev->header_name)
Alex Eldercb8627c2012-07-09 21:04:23 -05002526 goto out_err;
Alex Elder0bed54d2012-07-03 16:01:18 -05002527 sprintf(rbd_dev->header_name, "%s%s", rbd_dev->image_name, RBD_SUFFIX);
Alex Eldera725f65e2012-02-02 08:13:30 -06002528
Alex Elder3feeb8942012-08-31 17:29:52 -05002529 /* Snapshot name is optional */
2530 len = next_token(&buf);
Alex Elder820a5f32012-07-09 21:04:24 -05002531 if (!len) {
Alex Elder3feeb8942012-08-31 17:29:52 -05002532 buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
2533 len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
Alex Elder849b4262012-07-09 21:04:24 -05002534 }
Alex Elder3feeb8942012-08-31 17:29:52 -05002535 snap_name = kmalloc(len + 1, GFP_KERNEL);
2536 if (!snap_name)
2537 goto out_err;
2538 memcpy(snap_name, buf, len);
2539 *(snap_name + len) = '\0';
Alex Eldere28fff262012-02-02 08:13:30 -06002540
Alex Elder3feeb8942012-08-31 17:29:52 -05002541dout(" SNAP_NAME is <%s>, len is %zd\n", snap_name, len);
2542
2543 return snap_name;
Alex Elderd22f76e2012-07-12 10:46:35 -05002544
2545out_err:
Alex Elder0bed54d2012-07-03 16:01:18 -05002546 kfree(rbd_dev->header_name);
Alex Elderd78fd7a2012-07-26 23:37:14 -05002547 rbd_dev->header_name = NULL;
Alex Elder0bed54d2012-07-03 16:01:18 -05002548 kfree(rbd_dev->image_name);
Alex Elderd78fd7a2012-07-26 23:37:14 -05002549 rbd_dev->image_name = NULL;
2550 rbd_dev->image_name_len = 0;
Alex Elderd22f76e2012-07-12 10:46:35 -05002551 kfree(rbd_dev->pool_name);
2552 rbd_dev->pool_name = NULL;
2553
Alex Elder3feeb8942012-08-31 17:29:52 -05002554 return err_ptr;
Alex Eldera725f65e2012-02-02 08:13:30 -06002555}
2556
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002557static ssize_t rbd_add(struct bus_type *bus,
2558 const char *buf,
2559 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002560{
Alex Eldercb8627c2012-07-09 21:04:23 -05002561 char *options;
2562 struct rbd_device *rbd_dev = NULL;
Alex Elder7ef32142012-02-02 08:13:30 -06002563 const char *mon_addrs = NULL;
2564 size_t mon_addrs_size = 0;
Alex Elder27cc2592012-02-02 08:13:30 -06002565 struct ceph_osd_client *osdc;
2566 int rc = -ENOMEM;
Alex Elder3feeb8942012-08-31 17:29:52 -05002567 char *snap_name;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002568
2569 if (!try_module_get(THIS_MODULE))
2570 return -ENODEV;
2571
Alex Elder27cc2592012-02-02 08:13:30 -06002572 options = kmalloc(count, GFP_KERNEL);
2573 if (!options)
2574 goto err_nomem;
Alex Eldercb8627c2012-07-09 21:04:23 -05002575 rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL);
2576 if (!rbd_dev)
2577 goto err_nomem;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002578
2579 /* static rbd_device initialization */
2580 spin_lock_init(&rbd_dev->lock);
2581 INIT_LIST_HEAD(&rbd_dev->node);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002582 INIT_LIST_HEAD(&rbd_dev->snaps);
Josh Durginc6666012011-11-21 17:11:12 -08002583 init_rwsem(&rbd_dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002584
Alex Elderd184f6b2012-01-29 13:57:44 -06002585 /* generate unique id: find highest unique id, add one */
Alex Eldere2839302012-08-29 17:11:06 -05002586 rbd_dev_id_get(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002587
Alex Eldera725f65e2012-02-02 08:13:30 -06002588 /* Fill in the device name, now that we have its id. */
Alex Elder81a89792012-02-02 08:13:30 -06002589 BUILD_BUG_ON(DEV_NAME_LEN
2590 < sizeof (RBD_DRV_NAME) + MAX_INT_FORMAT_WIDTH);
Alex Elderde71a292012-07-03 16:01:19 -05002591 sprintf(rbd_dev->name, "%s%d", RBD_DRV_NAME, rbd_dev->dev_id);
Alex Eldere124a822012-01-29 13:57:44 -06002592
Alex Eldera725f65e2012-02-02 08:13:30 -06002593 /* parse add command */
Alex Elder3feeb8942012-08-31 17:29:52 -05002594 snap_name = rbd_add_parse_args(rbd_dev, buf,
2595 &mon_addrs, &mon_addrs_size, options, count);
2596 if (IS_ERR(snap_name)) {
2597 rc = PTR_ERR(snap_name);
Alex Eldera725f65e2012-02-02 08:13:30 -06002598 goto err_put_id;
Alex Elder3feeb8942012-08-31 17:29:52 -05002599 }
Alex Eldera725f65e2012-02-02 08:13:30 -06002600
Alex Elderf8c38922012-08-10 13:12:07 -07002601 rc = rbd_get_client(rbd_dev, mon_addrs, mon_addrs_size - 1, options);
2602 if (rc < 0)
Alex Elderf0f8cef2012-01-29 13:57:44 -06002603 goto err_put_id;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002604
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002605 /* pick the pool */
Alex Elder1dbb4392012-01-24 10:08:37 -06002606 osdc = &rbd_dev->rbd_client->client->osdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002607 rc = ceph_pg_poolid_by_name(osdc->osdmap, rbd_dev->pool_name);
2608 if (rc < 0)
2609 goto err_out_client;
Alex Elder9bb2f332012-07-12 10:46:35 -05002610 rbd_dev->pool_id = rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002611
2612 /* register our block device */
Alex Elder27cc2592012-02-02 08:13:30 -06002613 rc = register_blkdev(0, rbd_dev->name);
2614 if (rc < 0)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002615 goto err_out_client;
Alex Elder27cc2592012-02-02 08:13:30 -06002616 rbd_dev->major = rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002617
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002618 rc = rbd_bus_add_dev(rbd_dev);
2619 if (rc)
Yehuda Sadeh766fc432011-01-07 14:58:42 -08002620 goto err_out_blkdev;
2621
Alex Elder32eec682012-02-08 16:11:14 -06002622 /*
2623 * At this point cleanup in the event of an error is the job
2624 * of the sysfs code (initiated by rbd_bus_del_dev()).
2625 *
2626 * Set up and announce blkdev mapping.
2627 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002628 rc = rbd_init_disk(rbd_dev);
2629 if (rc)
Yehuda Sadeh766fc432011-01-07 14:58:42 -08002630 goto err_out_bus;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002631
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002632 rc = rbd_init_watch_dev(rbd_dev);
2633 if (rc)
2634 goto err_out_bus;
2635
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002636 return count;
2637
Yehuda Sadeh766fc432011-01-07 14:58:42 -08002638err_out_bus:
Yehuda Sadeh766fc432011-01-07 14:58:42 -08002639 /* this will also clean up rest of rbd_dev stuff */
2640
2641 rbd_bus_del_dev(rbd_dev);
2642 kfree(options);
Yehuda Sadeh766fc432011-01-07 14:58:42 -08002643 return rc;
2644
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002645err_out_blkdev:
2646 unregister_blkdev(rbd_dev->major, rbd_dev->name);
2647err_out_client:
2648 rbd_put_client(rbd_dev);
Alex Elderf0f8cef2012-01-29 13:57:44 -06002649err_put_id:
Alex Eldercb8627c2012-07-09 21:04:23 -05002650 if (rbd_dev->pool_name) {
Alex Elderf84344f2012-08-31 17:29:51 -05002651 kfree(rbd_dev->mapping.snap_name);
Alex Elder0bed54d2012-07-03 16:01:18 -05002652 kfree(rbd_dev->header_name);
2653 kfree(rbd_dev->image_name);
Alex Eldercb8627c2012-07-09 21:04:23 -05002654 kfree(rbd_dev->pool_name);
2655 }
Alex Eldere2839302012-08-29 17:11:06 -05002656 rbd_dev_id_put(rbd_dev);
Alex Elder27cc2592012-02-02 08:13:30 -06002657err_nomem:
Alex Elder27cc2592012-02-02 08:13:30 -06002658 kfree(rbd_dev);
Alex Eldercb8627c2012-07-09 21:04:23 -05002659 kfree(options);
Alex Elder27cc2592012-02-02 08:13:30 -06002660
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002661 dout("Error adding device %s\n", buf);
2662 module_put(THIS_MODULE);
Alex Elder27cc2592012-02-02 08:13:30 -06002663
2664 return (ssize_t) rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002665}
2666
Alex Elderde71a292012-07-03 16:01:19 -05002667static struct rbd_device *__rbd_get_dev(unsigned long dev_id)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002668{
2669 struct list_head *tmp;
2670 struct rbd_device *rbd_dev;
2671
Alex Eldere124a822012-01-29 13:57:44 -06002672 spin_lock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002673 list_for_each(tmp, &rbd_dev_list) {
2674 rbd_dev = list_entry(tmp, struct rbd_device, node);
Alex Elderde71a292012-07-03 16:01:19 -05002675 if (rbd_dev->dev_id == dev_id) {
Alex Eldere124a822012-01-29 13:57:44 -06002676 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002677 return rbd_dev;
Alex Eldere124a822012-01-29 13:57:44 -06002678 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002679 }
Alex Eldere124a822012-01-29 13:57:44 -06002680 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002681 return NULL;
2682}
2683
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002684static void rbd_dev_release(struct device *dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002685{
Alex Elder593a9e72012-02-07 12:03:37 -06002686 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002687
Alex Elder1dbb4392012-01-24 10:08:37 -06002688 if (rbd_dev->watch_request) {
2689 struct ceph_client *client = rbd_dev->rbd_client->client;
2690
2691 ceph_osdc_unregister_linger_request(&client->osdc,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002692 rbd_dev->watch_request);
Alex Elder1dbb4392012-01-24 10:08:37 -06002693 }
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002694 if (rbd_dev->watch_event)
Alex Elder070c6332012-07-25 09:32:41 -05002695 rbd_req_sync_unwatch(rbd_dev);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002696
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002697 rbd_put_client(rbd_dev);
2698
2699 /* clean up and free blkdev */
2700 rbd_free_disk(rbd_dev);
2701 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Alex Elder32eec682012-02-08 16:11:14 -06002702
2703 /* done with the id, and with the rbd_dev */
Alex Elderf84344f2012-08-31 17:29:51 -05002704 kfree(rbd_dev->mapping.snap_name);
Alex Elder0bed54d2012-07-03 16:01:18 -05002705 kfree(rbd_dev->header_name);
Alex Elderd22f76e2012-07-12 10:46:35 -05002706 kfree(rbd_dev->pool_name);
Alex Elder0bed54d2012-07-03 16:01:18 -05002707 kfree(rbd_dev->image_name);
Alex Eldere2839302012-08-29 17:11:06 -05002708 rbd_dev_id_put(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002709 kfree(rbd_dev);
2710
2711 /* release module ref */
2712 module_put(THIS_MODULE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002713}
2714
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002715static ssize_t rbd_remove(struct bus_type *bus,
2716 const char *buf,
2717 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002718{
2719 struct rbd_device *rbd_dev = NULL;
2720 int target_id, rc;
2721 unsigned long ul;
2722 int ret = count;
2723
2724 rc = strict_strtoul(buf, 10, &ul);
2725 if (rc)
2726 return rc;
2727
2728 /* convert to int; abort if we lost anything in the conversion */
2729 target_id = (int) ul;
2730 if (target_id != ul)
2731 return -EINVAL;
2732
2733 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2734
2735 rbd_dev = __rbd_get_dev(target_id);
2736 if (!rbd_dev) {
2737 ret = -ENOENT;
2738 goto done;
2739 }
2740
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002741 __rbd_remove_all_snaps(rbd_dev);
2742 rbd_bus_del_dev(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002743
2744done:
2745 mutex_unlock(&ctl_mutex);
Alex Elderaafb230e2012-09-06 16:00:54 -05002746
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002747 return ret;
2748}
2749
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002750static ssize_t rbd_snap_add(struct device *dev,
2751 struct device_attribute *attr,
2752 const char *buf,
2753 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002754{
Alex Elder593a9e72012-02-07 12:03:37 -06002755 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002756 int ret;
2757 char *name = kmalloc(count + 1, GFP_KERNEL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002758 if (!name)
2759 return -ENOMEM;
2760
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002761 snprintf(name, count, "%s", buf);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002762
2763 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2764
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002765 ret = rbd_header_add_snap(rbd_dev,
2766 name, GFP_KERNEL);
2767 if (ret < 0)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002768 goto err_unlock;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002769
Alex Elderb8136232012-07-25 09:32:41 -05002770 ret = __rbd_refresh_header(rbd_dev, NULL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002771 if (ret < 0)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002772 goto err_unlock;
2773
2774 /* shouldn't hold ctl_mutex when notifying.. notify might
2775 trigger a watch callback that would need to get that mutex */
2776 mutex_unlock(&ctl_mutex);
2777
2778 /* make a best effort, don't error if failed */
Alex Elder4cb16252012-07-25 09:32:40 -05002779 rbd_req_sync_notify(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002780
2781 ret = count;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002782 kfree(name);
2783 return ret;
2784
2785err_unlock:
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002786 mutex_unlock(&ctl_mutex);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002787 kfree(name);
2788 return ret;
2789}
2790
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002791/*
2792 * create control files in sysfs
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002793 * /sys/bus/rbd/...
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002794 */
2795static int rbd_sysfs_init(void)
2796{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002797 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002798
Alex Elderfed4c142012-02-07 12:03:36 -06002799 ret = device_register(&rbd_root_dev);
Alex Elder21079782012-01-24 10:08:36 -06002800 if (ret < 0)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002801 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002802
Alex Elderfed4c142012-02-07 12:03:36 -06002803 ret = bus_register(&rbd_bus_type);
2804 if (ret < 0)
2805 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002806
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002807 return ret;
2808}
2809
2810static void rbd_sysfs_cleanup(void)
2811{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002812 bus_unregister(&rbd_bus_type);
Alex Elderfed4c142012-02-07 12:03:36 -06002813 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002814}
2815
2816int __init rbd_init(void)
2817{
2818 int rc;
2819
2820 rc = rbd_sysfs_init();
2821 if (rc)
2822 return rc;
Alex Elderf0f8cef2012-01-29 13:57:44 -06002823 pr_info("loaded " RBD_DRV_NAME_LONG "\n");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002824 return 0;
2825}
2826
2827void __exit rbd_exit(void)
2828{
2829 rbd_sysfs_cleanup();
2830}
2831
2832module_init(rbd_init);
2833module_exit(rbd_exit);
2834
2835MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
2836MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
2837MODULE_DESCRIPTION("rados block device");
2838
2839/* following authorship retained from original osdblk.c */
2840MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
2841
2842MODULE_LICENSE("GPL");