blob: 7ba70c49bbdbf1321b8e2372db1426aebbf216bf [file] [log] [blame]
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001/*
2 rbd.c -- Export ceph rados objects as a Linux block device
3
4
5 based on drivers/block/osdblk.c:
6
7 Copyright 2009 Red Hat, Inc.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; see the file COPYING. If not, write to
20 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21
22
23
Yehuda Sadehdfc56062010-11-19 14:51:04 -080024 For usage instructions, please refer to:
Yehuda Sadeh602adf42010-08-12 16:11:25 -070025
Yehuda Sadehdfc56062010-11-19 14:51:04 -080026 Documentation/ABI/testing/sysfs-bus-rbd
Yehuda Sadeh602adf42010-08-12 16:11:25 -070027
28 */
29
30#include <linux/ceph/libceph.h>
31#include <linux/ceph/osd_client.h>
32#include <linux/ceph/mon_client.h>
33#include <linux/ceph/decode.h>
Yehuda Sadeh59c2be12011-03-21 15:10:11 -070034#include <linux/parser.h>
Yehuda Sadeh602adf42010-08-12 16:11:25 -070035
36#include <linux/kernel.h>
37#include <linux/device.h>
38#include <linux/module.h>
39#include <linux/fs.h>
40#include <linux/blkdev.h>
41
42#include "rbd_types.h"
43
Alex Elder593a9e72012-02-07 12:03:37 -060044/*
45 * The basic unit of block I/O is a sector. It is interpreted in a
46 * number of contexts in Linux (blk, bio, genhd), but the default is
47 * universally 512 bytes. These symbols are just slightly more
48 * meaningful than the bare numbers they represent.
49 */
50#define SECTOR_SHIFT 9
51#define SECTOR_SIZE (1ULL << SECTOR_SHIFT)
52
Alex Elderdf111be2012-08-09 10:33:26 -070053/* It might be useful to have this defined elsewhere too */
54
55#define U64_MAX ((u64) (~0ULL))
56
Alex Elderf0f8cef2012-01-29 13:57:44 -060057#define RBD_DRV_NAME "rbd"
58#define RBD_DRV_NAME_LONG "rbd (rados block device)"
Yehuda Sadeh602adf42010-08-12 16:11:25 -070059
60#define RBD_MINORS_PER_MAJOR 256 /* max minors per blkdev */
61
Yehuda Sadeh602adf42010-08-12 16:11:25 -070062#define RBD_MAX_SNAP_NAME_LEN 32
63#define RBD_MAX_OPT_LEN 1024
64
65#define RBD_SNAP_HEAD_NAME "-"
66
Alex Elder81a89792012-02-02 08:13:30 -060067/*
68 * An RBD device name will be "rbd#", where the "rbd" comes from
69 * RBD_DRV_NAME above, and # is a unique integer identifier.
70 * MAX_INT_FORMAT_WIDTH is used in ensuring DEV_NAME_LEN is big
71 * enough to hold all possible device names.
72 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -070073#define DEV_NAME_LEN 32
Alex Elder81a89792012-02-02 08:13:30 -060074#define MAX_INT_FORMAT_WIDTH ((5 * sizeof (int)) / 2 + 1)
Yehuda Sadeh602adf42010-08-12 16:11:25 -070075
Alex Eldercc0538b2012-08-10 13:12:07 -070076#define RBD_READ_ONLY_DEFAULT false
Yehuda Sadeh59c2be12011-03-21 15:10:11 -070077
Yehuda Sadeh602adf42010-08-12 16:11:25 -070078/*
79 * block device image metadata (in-memory version)
80 */
81struct rbd_image_header {
82 u64 image_size;
Alex Elder849b4262012-07-09 21:04:24 -050083 char *object_prefix;
Yehuda Sadeh602adf42010-08-12 16:11:25 -070084 __u8 obj_order;
85 __u8 crypt_type;
86 __u8 comp_type;
Yehuda Sadeh602adf42010-08-12 16:11:25 -070087 struct ceph_snap_context *snapc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -070088 u32 total_snaps;
89
90 char *snap_names;
91 u64 *snap_sizes;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -070092
93 u64 obj_version;
94};
95
96struct rbd_options {
Alex Eldercc0538b2012-08-10 13:12:07 -070097 bool read_only;
Yehuda Sadeh602adf42010-08-12 16:11:25 -070098};
99
100/*
Alex Elderf0f8cef2012-01-29 13:57:44 -0600101 * an instance of the client. multiple devices may share an rbd client.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700102 */
103struct rbd_client {
104 struct ceph_client *client;
105 struct kref kref;
106 struct list_head node;
107};
108
109/*
Alex Elderf0f8cef2012-01-29 13:57:44 -0600110 * a request completion status
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700111 */
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700112struct rbd_req_status {
113 int done;
114 int rc;
115 u64 bytes;
116};
117
118/*
119 * a collection of requests
120 */
121struct rbd_req_coll {
122 int total;
123 int num_done;
124 struct kref kref;
125 struct rbd_req_status status[0];
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700126};
127
Alex Elderf0f8cef2012-01-29 13:57:44 -0600128/*
129 * a single io request
130 */
131struct rbd_request {
132 struct request *rq; /* blk layer request */
133 struct bio *bio; /* cloned bio */
134 struct page **pages; /* list of used pages */
135 u64 len;
136 int coll_index;
137 struct rbd_req_coll *coll;
138};
139
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800140struct rbd_snap {
141 struct device dev;
142 const char *name;
Josh Durgin35915382011-12-05 18:25:13 -0800143 u64 size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800144 struct list_head node;
145 u64 id;
146};
147
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700148/*
149 * a single device
150 */
151struct rbd_device {
Alex Elderde71a292012-07-03 16:01:19 -0500152 int dev_id; /* blkdev unique id */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700153
154 int major; /* blkdev assigned major */
155 struct gendisk *disk; /* blkdev's gendisk and rq */
156 struct request_queue *q;
157
Alex Elderf8c38922012-08-10 13:12:07 -0700158 struct rbd_options rbd_opts;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700159 struct rbd_client *rbd_client;
160
161 char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */
162
163 spinlock_t lock; /* queue lock */
164
165 struct rbd_image_header header;
Alex Elder0bed54d2012-07-03 16:01:18 -0500166 char *image_name;
167 size_t image_name_len;
168 char *header_name;
Alex Elderd22f76e2012-07-12 10:46:35 -0500169 char *pool_name;
Alex Elder9bb2f332012-07-12 10:46:35 -0500170 int pool_id;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700171
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700172 struct ceph_osd_event *watch_event;
173 struct ceph_osd_request *watch_request;
174
Josh Durginc6666012011-11-21 17:11:12 -0800175 /* protects updating the header */
176 struct rw_semaphore header_rwsem;
Josh Durgine88a36e2011-11-21 18:14:25 -0800177 /* name of the snapshot this device reads from */
Alex Elder820a5f32012-07-09 21:04:24 -0500178 char *snap_name;
Josh Durgine88a36e2011-11-21 18:14:25 -0800179 /* id of the snapshot this device reads from */
Josh Durgin77dfe992011-11-21 13:04:42 -0800180 u64 snap_id; /* current snapshot id */
Josh Durgine88a36e2011-11-21 18:14:25 -0800181 /* whether the snap_id this device reads from still exists */
182 bool snap_exists;
Alex Eldercc0538b2012-08-10 13:12:07 -0700183 bool read_only;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700184
185 struct list_head node;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800186
187 /* list of snapshots */
188 struct list_head snaps;
189
190 /* sysfs related */
191 struct device dev;
192};
193
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700194static DEFINE_MUTEX(ctl_mutex); /* Serialize open/close/setup/teardown */
Alex Eldere124a82f2012-01-29 13:57:44 -0600195
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700196static LIST_HEAD(rbd_dev_list); /* devices */
Alex Eldere124a82f2012-01-29 13:57:44 -0600197static DEFINE_SPINLOCK(rbd_dev_list_lock);
198
Alex Elder432b8582012-01-29 13:57:44 -0600199static LIST_HEAD(rbd_client_list); /* clients */
200static DEFINE_SPINLOCK(rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700201
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800202static int __rbd_init_snaps_header(struct rbd_device *rbd_dev);
203static void rbd_dev_release(struct device *dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800204static ssize_t rbd_snap_add(struct device *dev,
205 struct device_attribute *attr,
206 const char *buf,
207 size_t count);
Alex Elder14e70852012-07-19 09:09:27 -0500208static void __rbd_remove_snap_dev(struct rbd_snap *snap);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800209
Alex Elderf0f8cef2012-01-29 13:57:44 -0600210static ssize_t rbd_add(struct bus_type *bus, const char *buf,
211 size_t count);
212static ssize_t rbd_remove(struct bus_type *bus, const char *buf,
213 size_t count);
214
215static struct bus_attribute rbd_bus_attrs[] = {
216 __ATTR(add, S_IWUSR, NULL, rbd_add),
217 __ATTR(remove, S_IWUSR, NULL, rbd_remove),
218 __ATTR_NULL
219};
220
221static struct bus_type rbd_bus_type = {
222 .name = "rbd",
223 .bus_attrs = rbd_bus_attrs,
224};
225
226static void rbd_root_dev_release(struct device *dev)
227{
228}
229
230static struct device rbd_root_dev = {
231 .init_name = "rbd",
232 .release = rbd_root_dev_release,
233};
234
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800235
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800236static struct device *rbd_get_dev(struct rbd_device *rbd_dev)
237{
238 return get_device(&rbd_dev->dev);
239}
240
241static void rbd_put_dev(struct rbd_device *rbd_dev)
242{
243 put_device(&rbd_dev->dev);
244}
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700245
Alex Elder1fe5e992012-07-25 09:32:41 -0500246static int rbd_refresh_header(struct rbd_device *rbd_dev, u64 *hver);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700247
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700248static int rbd_open(struct block_device *bdev, fmode_t mode)
249{
Alex Elderf0f8cef2012-01-29 13:57:44 -0600250 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700251
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700252 if ((mode & FMODE_WRITE) && rbd_dev->read_only)
253 return -EROFS;
254
Alex Elder340c7a22012-08-10 13:12:07 -0700255 rbd_get_dev(rbd_dev);
256 set_device_ro(bdev, rbd_dev->read_only);
257
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700258 return 0;
259}
260
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800261static int rbd_release(struct gendisk *disk, fmode_t mode)
262{
263 struct rbd_device *rbd_dev = disk->private_data;
264
265 rbd_put_dev(rbd_dev);
266
267 return 0;
268}
269
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700270static const struct block_device_operations rbd_bd_ops = {
271 .owner = THIS_MODULE,
272 .open = rbd_open,
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800273 .release = rbd_release,
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700274};
275
276/*
277 * Initialize an rbd client instance.
Alex Elder43ae4702012-07-03 16:01:18 -0500278 * We own *ceph_opts.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700279 */
Alex Elderf8c38922012-08-10 13:12:07 -0700280static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700281{
282 struct rbd_client *rbdc;
283 int ret = -ENOMEM;
284
285 dout("rbd_client_create\n");
286 rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL);
287 if (!rbdc)
288 goto out_opt;
289
290 kref_init(&rbdc->kref);
291 INIT_LIST_HEAD(&rbdc->node);
292
Alex Elderbc534d82012-01-29 13:57:44 -0600293 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
294
Alex Elder43ae4702012-07-03 16:01:18 -0500295 rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700296 if (IS_ERR(rbdc->client))
Alex Elderbc534d82012-01-29 13:57:44 -0600297 goto out_mutex;
Alex Elder43ae4702012-07-03 16:01:18 -0500298 ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700299
300 ret = ceph_open_session(rbdc->client);
301 if (ret < 0)
302 goto out_err;
303
Alex Elder432b8582012-01-29 13:57:44 -0600304 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700305 list_add_tail(&rbdc->node, &rbd_client_list);
Alex Elder432b8582012-01-29 13:57:44 -0600306 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700307
Alex Elderbc534d82012-01-29 13:57:44 -0600308 mutex_unlock(&ctl_mutex);
309
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700310 dout("rbd_client_create created %p\n", rbdc);
311 return rbdc;
312
313out_err:
314 ceph_destroy_client(rbdc->client);
Alex Elderbc534d82012-01-29 13:57:44 -0600315out_mutex:
316 mutex_unlock(&ctl_mutex);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700317 kfree(rbdc);
318out_opt:
Alex Elder43ae4702012-07-03 16:01:18 -0500319 if (ceph_opts)
320 ceph_destroy_options(ceph_opts);
Vasiliy Kulikov28f259b2010-09-26 12:59:37 +0400321 return ERR_PTR(ret);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700322}
323
324/*
Alex Elder1f7ba332012-08-10 13:12:07 -0700325 * Find a ceph client with specific addr and configuration. If
326 * found, bump its reference count.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700327 */
Alex Elder1f7ba332012-08-10 13:12:07 -0700328static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700329{
330 struct rbd_client *client_node;
Alex Elder1f7ba332012-08-10 13:12:07 -0700331 bool found = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700332
Alex Elder43ae4702012-07-03 16:01:18 -0500333 if (ceph_opts->flags & CEPH_OPT_NOSHARE)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700334 return NULL;
335
Alex Elder1f7ba332012-08-10 13:12:07 -0700336 spin_lock(&rbd_client_list_lock);
337 list_for_each_entry(client_node, &rbd_client_list, node) {
338 if (!ceph_compare_options(ceph_opts, client_node->client)) {
339 kref_get(&client_node->kref);
340 found = true;
341 break;
342 }
343 }
344 spin_unlock(&rbd_client_list_lock);
345
346 return found ? client_node : NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700347}
348
349/*
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700350 * mount options
351 */
352enum {
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700353 Opt_last_int,
354 /* int args above */
355 Opt_last_string,
356 /* string args above */
Alex Eldercc0538b2012-08-10 13:12:07 -0700357 Opt_read_only,
358 Opt_read_write,
359 /* Boolean args above */
360 Opt_last_bool,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700361};
362
Alex Elder43ae4702012-07-03 16:01:18 -0500363static match_table_t rbd_opts_tokens = {
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700364 /* int args above */
365 /* string args above */
Alex Eldercc0538b2012-08-10 13:12:07 -0700366 {Opt_read_only, "read_only"},
367 {Opt_read_only, "ro"}, /* Alternate spelling */
368 {Opt_read_write, "read_write"},
369 {Opt_read_write, "rw"}, /* Alternate spelling */
370 /* Boolean args above */
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700371 {-1, NULL}
372};
373
374static int parse_rbd_opts_token(char *c, void *private)
375{
Alex Elder43ae4702012-07-03 16:01:18 -0500376 struct rbd_options *rbd_opts = private;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700377 substring_t argstr[MAX_OPT_ARGS];
378 int token, intval, ret;
379
Alex Elder43ae4702012-07-03 16:01:18 -0500380 token = match_token(c, rbd_opts_tokens, argstr);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700381 if (token < 0)
382 return -EINVAL;
383
384 if (token < Opt_last_int) {
385 ret = match_int(&argstr[0], &intval);
386 if (ret < 0) {
387 pr_err("bad mount option arg (not int) "
388 "at '%s'\n", c);
389 return ret;
390 }
391 dout("got int token %d val %d\n", token, intval);
392 } else if (token > Opt_last_int && token < Opt_last_string) {
393 dout("got string token %d val %s\n", token,
394 argstr[0].from);
Alex Eldercc0538b2012-08-10 13:12:07 -0700395 } else if (token > Opt_last_string && token < Opt_last_bool) {
396 dout("got Boolean token %d\n", token);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700397 } else {
398 dout("got token %d\n", token);
399 }
400
401 switch (token) {
Alex Eldercc0538b2012-08-10 13:12:07 -0700402 case Opt_read_only:
403 rbd_opts->read_only = true;
404 break;
405 case Opt_read_write:
406 rbd_opts->read_only = false;
407 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700408 default:
409 BUG_ON(token);
410 }
411 return 0;
412}
413
414/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700415 * Get a ceph client with specific addr and configuration, if one does
416 * not exist create it.
417 */
Alex Elderf8c38922012-08-10 13:12:07 -0700418static int rbd_get_client(struct rbd_device *rbd_dev, const char *mon_addr,
419 size_t mon_addr_len, char *options)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700420{
Alex Elderf8c38922012-08-10 13:12:07 -0700421 struct rbd_options *rbd_opts = &rbd_dev->rbd_opts;
Alex Elder43ae4702012-07-03 16:01:18 -0500422 struct ceph_options *ceph_opts;
Alex Elderf8c38922012-08-10 13:12:07 -0700423 struct rbd_client *rbdc;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700424
Alex Eldercc0538b2012-08-10 13:12:07 -0700425 rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700426
Alex Elder43ae4702012-07-03 16:01:18 -0500427 ceph_opts = ceph_parse_options(options, mon_addr,
428 mon_addr + mon_addr_len,
429 parse_rbd_opts_token, rbd_opts);
Alex Elderf8c38922012-08-10 13:12:07 -0700430 if (IS_ERR(ceph_opts))
431 return PTR_ERR(ceph_opts);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700432
Alex Elder1f7ba332012-08-10 13:12:07 -0700433 rbdc = rbd_client_find(ceph_opts);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700434 if (rbdc) {
Alex Eldere6994d3d2012-01-29 13:57:44 -0600435 /* using an existing client */
Alex Elder43ae4702012-07-03 16:01:18 -0500436 ceph_destroy_options(ceph_opts);
Alex Elderf8c38922012-08-10 13:12:07 -0700437 } else {
438 rbdc = rbd_client_create(ceph_opts);
439 if (IS_ERR(rbdc))
440 return PTR_ERR(rbdc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700441 }
Alex Elderf8c38922012-08-10 13:12:07 -0700442 rbd_dev->rbd_client = rbdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700443
Alex Elderf8c38922012-08-10 13:12:07 -0700444 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700445}
446
447/*
448 * Destroy ceph client
Alex Elderd23a4b32012-01-29 13:57:43 -0600449 *
Alex Elder432b8582012-01-29 13:57:44 -0600450 * Caller must hold rbd_client_list_lock.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700451 */
452static void rbd_client_release(struct kref *kref)
453{
454 struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
455
456 dout("rbd_release_client %p\n", rbdc);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500457 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700458 list_del(&rbdc->node);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500459 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700460
461 ceph_destroy_client(rbdc->client);
462 kfree(rbdc);
463}
464
465/*
466 * Drop reference to ceph client node. If it's not referenced anymore, release
467 * it.
468 */
469static void rbd_put_client(struct rbd_device *rbd_dev)
470{
471 kref_put(&rbd_dev->rbd_client->kref, rbd_client_release);
472 rbd_dev->rbd_client = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700473}
474
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700475/*
476 * Destroy requests collection
477 */
478static void rbd_coll_release(struct kref *kref)
479{
480 struct rbd_req_coll *coll =
481 container_of(kref, struct rbd_req_coll, kref);
482
483 dout("rbd_coll_release %p\n", coll);
484 kfree(coll);
485}
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700486
Alex Elder8e94af82012-07-25 09:32:40 -0500487static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
488{
Alex Elder103a1502012-08-02 11:29:45 -0500489 size_t size;
490 u32 snap_count;
491
492 /* The header has to start with the magic rbd header text */
493 if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT)))
494 return false;
495
496 /*
497 * The size of a snapshot header has to fit in a size_t, and
498 * that limits the number of snapshots.
499 */
500 snap_count = le32_to_cpu(ondisk->snap_count);
501 size = SIZE_MAX - sizeof (struct ceph_snap_context);
502 if (snap_count > size / sizeof (__le64))
503 return false;
504
505 /*
506 * Not only that, but the size of the entire the snapshot
507 * header must also be representable in a size_t.
508 */
509 size -= snap_count * sizeof (__le64);
510 if ((u64) size < le64_to_cpu(ondisk->snap_names_len))
511 return false;
512
513 return true;
Alex Elder8e94af82012-07-25 09:32:40 -0500514}
515
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700516/*
517 * Create a new header structure, translate header format from the on-disk
518 * header.
519 */
520static int rbd_header_from_disk(struct rbd_image_header *header,
Alex Elder4156d992012-08-02 11:29:46 -0500521 struct rbd_image_header_ondisk *ondisk)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700522{
Alex Elderccece232012-07-10 20:30:10 -0500523 u32 snap_count;
Alex Elder58c17b02012-08-23 23:22:06 -0500524 size_t len;
Alex Elderd2bb24e2012-07-26 23:37:14 -0500525 size_t size;
Alex Elder621901d2012-08-23 23:22:06 -0500526 u32 i;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700527
Alex Elder6a523252012-07-19 17:12:59 -0500528 memset(header, 0, sizeof (*header));
529
Alex Elder103a1502012-08-02 11:29:45 -0500530 snap_count = le32_to_cpu(ondisk->snap_count);
531
Alex Elder58c17b02012-08-23 23:22:06 -0500532 len = strnlen(ondisk->object_prefix, sizeof (ondisk->object_prefix));
533 header->object_prefix = kmalloc(len + 1, GFP_KERNEL);
Alex Elder6a523252012-07-19 17:12:59 -0500534 if (!header->object_prefix)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700535 return -ENOMEM;
Alex Elder58c17b02012-08-23 23:22:06 -0500536 memcpy(header->object_prefix, ondisk->object_prefix, len);
537 header->object_prefix[len] = '\0';
Alex Elder00f1f362012-02-07 12:03:36 -0600538
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700539 if (snap_count) {
Alex Elderf785cc12012-08-23 23:22:06 -0500540 u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len);
541
Alex Elder621901d2012-08-23 23:22:06 -0500542 /* Save a copy of the snapshot names */
543
Alex Elderf785cc12012-08-23 23:22:06 -0500544 if (snap_names_len > (u64) SIZE_MAX)
545 return -EIO;
546 header->snap_names = kmalloc(snap_names_len, GFP_KERNEL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700547 if (!header->snap_names)
Alex Elder6a523252012-07-19 17:12:59 -0500548 goto out_err;
Alex Elderf785cc12012-08-23 23:22:06 -0500549 /*
550 * Note that rbd_dev_v1_header_read() guarantees
551 * the ondisk buffer we're working with has
552 * snap_names_len bytes beyond the end of the
553 * snapshot id array, this memcpy() is safe.
554 */
555 memcpy(header->snap_names, &ondisk->snaps[snap_count],
556 snap_names_len);
Alex Elder6a523252012-07-19 17:12:59 -0500557
Alex Elder621901d2012-08-23 23:22:06 -0500558 /* Record each snapshot's size */
559
Alex Elderd2bb24e2012-07-26 23:37:14 -0500560 size = snap_count * sizeof (*header->snap_sizes);
561 header->snap_sizes = kmalloc(size, GFP_KERNEL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700562 if (!header->snap_sizes)
Alex Elder6a523252012-07-19 17:12:59 -0500563 goto out_err;
Alex Elder621901d2012-08-23 23:22:06 -0500564 for (i = 0; i < snap_count; i++)
565 header->snap_sizes[i] =
566 le64_to_cpu(ondisk->snaps[i].image_size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700567 } else {
Alex Elderccece232012-07-10 20:30:10 -0500568 WARN_ON(ondisk->snap_names_len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700569 header->snap_names = NULL;
570 header->snap_sizes = NULL;
571 }
Alex Elder849b4262012-07-09 21:04:24 -0500572
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700573 header->image_size = le64_to_cpu(ondisk->image_size);
574 header->obj_order = ondisk->options.order;
575 header->crypt_type = ondisk->options.crypt_type;
576 header->comp_type = ondisk->options.comp_type;
Alex Elder6a523252012-07-19 17:12:59 -0500577 header->total_snaps = snap_count;
578
Alex Elder621901d2012-08-23 23:22:06 -0500579 /* Allocate and fill in the snapshot context */
580
Alex Elder6a523252012-07-19 17:12:59 -0500581 size = sizeof (struct ceph_snap_context);
582 size += snap_count * sizeof (header->snapc->snaps[0]);
583 header->snapc = kzalloc(size, GFP_KERNEL);
584 if (!header->snapc)
585 goto out_err;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700586
587 atomic_set(&header->snapc->nref, 1);
Alex Elder505cbb92012-07-19 08:49:18 -0500588 header->snapc->seq = le64_to_cpu(ondisk->snap_seq);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700589 header->snapc->num_snaps = snap_count;
Alex Elder621901d2012-08-23 23:22:06 -0500590 for (i = 0; i < snap_count; i++)
591 header->snapc->snaps[i] =
592 le64_to_cpu(ondisk->snaps[i].id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700593
594 return 0;
595
Alex Elder6a523252012-07-19 17:12:59 -0500596out_err:
Alex Elder849b4262012-07-09 21:04:24 -0500597 kfree(header->snap_sizes);
Alex Elderccece232012-07-10 20:30:10 -0500598 header->snap_sizes = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700599 kfree(header->snap_names);
Alex Elderccece232012-07-10 20:30:10 -0500600 header->snap_names = NULL;
Alex Elder6a523252012-07-19 17:12:59 -0500601 kfree(header->object_prefix);
602 header->object_prefix = NULL;
Alex Elderccece232012-07-10 20:30:10 -0500603
Alex Elder00f1f362012-02-07 12:03:36 -0600604 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700605}
606
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700607static int snap_by_name(struct rbd_image_header *header, const char *snap_name,
608 u64 *seq, u64 *size)
609{
610 int i;
611 char *p = header->snap_names;
612
Alex Elder00f1f362012-02-07 12:03:36 -0600613 for (i = 0; i < header->total_snaps; i++) {
614 if (!strcmp(snap_name, p)) {
615
616 /* Found it. Pass back its id and/or size */
617
618 if (seq)
619 *seq = header->snapc->snaps[i];
620 if (size)
621 *size = header->snap_sizes[i];
622 return i;
623 }
624 p += strlen(p) + 1; /* Skip ahead to the next name */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700625 }
Alex Elder00f1f362012-02-07 12:03:36 -0600626 return -ENOENT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700627}
628
Alex Elder0ce1a792012-07-03 16:01:18 -0500629static int rbd_header_set_snap(struct rbd_device *rbd_dev, u64 *size)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700630{
Alex Elder78dc4472012-07-19 08:49:18 -0500631 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700632
Alex Elder0ce1a792012-07-03 16:01:18 -0500633 down_write(&rbd_dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700634
Alex Elder0ce1a792012-07-03 16:01:18 -0500635 if (!memcmp(rbd_dev->snap_name, RBD_SNAP_HEAD_NAME,
Josh Durgincc9d7342011-11-21 18:19:13 -0800636 sizeof (RBD_SNAP_HEAD_NAME))) {
Alex Elder0ce1a792012-07-03 16:01:18 -0500637 rbd_dev->snap_id = CEPH_NOSNAP;
Josh Durgine88a36e2011-11-21 18:14:25 -0800638 rbd_dev->snap_exists = false;
Alex Eldercc0538b2012-08-10 13:12:07 -0700639 rbd_dev->read_only = rbd_dev->rbd_opts.read_only;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700640 if (size)
Alex Elder78dc4472012-07-19 08:49:18 -0500641 *size = rbd_dev->header.image_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700642 } else {
Alex Elder78dc4472012-07-19 08:49:18 -0500643 u64 snap_id = 0;
644
645 ret = snap_by_name(&rbd_dev->header, rbd_dev->snap_name,
646 &snap_id, size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700647 if (ret < 0)
648 goto done;
Alex Elder78dc4472012-07-19 08:49:18 -0500649 rbd_dev->snap_id = snap_id;
Josh Durgine88a36e2011-11-21 18:14:25 -0800650 rbd_dev->snap_exists = true;
Alex Eldercc0538b2012-08-10 13:12:07 -0700651 rbd_dev->read_only = true; /* No choice for snapshots */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700652 }
653
654 ret = 0;
655done:
Alex Elder0ce1a792012-07-03 16:01:18 -0500656 up_write(&rbd_dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700657 return ret;
658}
659
660static void rbd_header_free(struct rbd_image_header *header)
661{
Alex Elder849b4262012-07-09 21:04:24 -0500662 kfree(header->object_prefix);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500663 header->object_prefix = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700664 kfree(header->snap_sizes);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500665 header->snap_sizes = NULL;
Alex Elder849b4262012-07-09 21:04:24 -0500666 kfree(header->snap_names);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500667 header->snap_names = NULL;
Josh Durgind1d25642011-12-05 14:03:05 -0800668 ceph_put_snap_context(header->snapc);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500669 header->snapc = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700670}
671
Alex Elder65ccfe22012-08-09 10:33:26 -0700672static char *rbd_segment_name(struct rbd_device *rbd_dev, u64 offset)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700673{
Alex Elder65ccfe22012-08-09 10:33:26 -0700674 char *name;
675 u64 segment;
676 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700677
Alex Elder65ccfe22012-08-09 10:33:26 -0700678 name = kmalloc(RBD_MAX_SEG_NAME_LEN + 1, GFP_NOIO);
679 if (!name)
680 return NULL;
681 segment = offset >> rbd_dev->header.obj_order;
682 ret = snprintf(name, RBD_MAX_SEG_NAME_LEN, "%s.%012llx",
683 rbd_dev->header.object_prefix, segment);
684 if (ret < 0 || ret >= RBD_MAX_SEG_NAME_LEN) {
685 pr_err("error formatting segment name for #%llu (%d)\n",
686 segment, ret);
687 kfree(name);
688 name = NULL;
689 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700690
Alex Elder65ccfe22012-08-09 10:33:26 -0700691 return name;
692}
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700693
Alex Elder65ccfe22012-08-09 10:33:26 -0700694static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset)
695{
696 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700697
Alex Elder65ccfe22012-08-09 10:33:26 -0700698 return offset & (segment_size - 1);
699}
700
701static u64 rbd_segment_length(struct rbd_device *rbd_dev,
702 u64 offset, u64 length)
703{
704 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
705
706 offset &= segment_size - 1;
707
708 BUG_ON(length > U64_MAX - offset);
709 if (offset + length > segment_size)
710 length = segment_size - offset;
711
712 return length;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700713}
714
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700715static int rbd_get_num_segments(struct rbd_image_header *header,
716 u64 ofs, u64 len)
717{
Alex Elderdf111be2012-08-09 10:33:26 -0700718 u64 start_seg;
719 u64 end_seg;
720
721 if (!len)
722 return 0;
723 if (len - 1 > U64_MAX - ofs)
724 return -ERANGE;
725
726 start_seg = ofs >> header->obj_order;
727 end_seg = (ofs + len - 1) >> header->obj_order;
728
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700729 return end_seg - start_seg + 1;
730}
731
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700732/*
Josh Durgin029bcbd2011-07-22 11:35:23 -0700733 * returns the size of an object in the image
734 */
735static u64 rbd_obj_bytes(struct rbd_image_header *header)
736{
737 return 1 << header->obj_order;
738}
739
740/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700741 * bio helpers
742 */
743
744static void bio_chain_put(struct bio *chain)
745{
746 struct bio *tmp;
747
748 while (chain) {
749 tmp = chain;
750 chain = chain->bi_next;
751 bio_put(tmp);
752 }
753}
754
755/*
756 * zeros a bio chain, starting at specific offset
757 */
758static void zero_bio_chain(struct bio *chain, int start_ofs)
759{
760 struct bio_vec *bv;
761 unsigned long flags;
762 void *buf;
763 int i;
764 int pos = 0;
765
766 while (chain) {
767 bio_for_each_segment(bv, chain, i) {
768 if (pos + bv->bv_len > start_ofs) {
769 int remainder = max(start_ofs - pos, 0);
770 buf = bvec_kmap_irq(bv, &flags);
771 memset(buf + remainder, 0,
772 bv->bv_len - remainder);
Dan Carpenter85b5aaa2010-10-11 21:15:11 +0200773 bvec_kunmap_irq(buf, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700774 }
775 pos += bv->bv_len;
776 }
777
778 chain = chain->bi_next;
779 }
780}
781
782/*
783 * bio_chain_clone - clone a chain of bios up to a certain length.
784 * might return a bio_pair that will need to be released.
785 */
786static struct bio *bio_chain_clone(struct bio **old, struct bio **next,
787 struct bio_pair **bp,
788 int len, gfp_t gfpmask)
789{
Alex Elder542582f2012-08-09 10:33:25 -0700790 struct bio *old_chain = *old;
791 struct bio *new_chain = NULL;
792 struct bio *tail;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700793 int total = 0;
794
795 if (*bp) {
796 bio_pair_release(*bp);
797 *bp = NULL;
798 }
799
800 while (old_chain && (total < len)) {
Alex Elder542582f2012-08-09 10:33:25 -0700801 struct bio *tmp;
802
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700803 tmp = bio_kmalloc(gfpmask, old_chain->bi_max_vecs);
804 if (!tmp)
805 goto err_out;
Alex Elder542582f2012-08-09 10:33:25 -0700806 gfpmask &= ~__GFP_WAIT; /* can't wait after the first */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700807
808 if (total + old_chain->bi_size > len) {
809 struct bio_pair *bp;
810
811 /*
812 * this split can only happen with a single paged bio,
813 * split_bio will BUG_ON if this is not the case
814 */
815 dout("bio_chain_clone split! total=%d remaining=%d"
Alex Elderbd919d42012-07-13 20:35:11 -0500816 "bi_size=%u\n",
817 total, len - total, old_chain->bi_size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700818
819 /* split the bio. We'll release it either in the next
820 call, or it will have to be released outside */
Alex Elder593a9e72012-02-07 12:03:37 -0600821 bp = bio_split(old_chain, (len - total) / SECTOR_SIZE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700822 if (!bp)
823 goto err_out;
824
825 __bio_clone(tmp, &bp->bio1);
826
827 *next = &bp->bio2;
828 } else {
829 __bio_clone(tmp, old_chain);
830 *next = old_chain->bi_next;
831 }
832
833 tmp->bi_bdev = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700834 tmp->bi_next = NULL;
Alex Elder542582f2012-08-09 10:33:25 -0700835 if (new_chain)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700836 tail->bi_next = tmp;
Alex Elder542582f2012-08-09 10:33:25 -0700837 else
838 new_chain = tmp;
839 tail = tmp;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700840 old_chain = old_chain->bi_next;
841
842 total += tmp->bi_size;
843 }
844
845 BUG_ON(total < len);
846
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700847 *old = old_chain;
848
849 return new_chain;
850
851err_out:
852 dout("bio_chain_clone with err\n");
853 bio_chain_put(new_chain);
854 return NULL;
855}
856
857/*
858 * helpers for osd request op vectors.
859 */
Alex Elder57cfc102012-06-26 12:57:03 -0700860static struct ceph_osd_req_op *rbd_create_rw_ops(int num_ops,
861 int opcode, u32 payload_len)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700862{
Alex Elder57cfc102012-06-26 12:57:03 -0700863 struct ceph_osd_req_op *ops;
864
865 ops = kzalloc(sizeof (*ops) * (num_ops + 1), GFP_NOIO);
866 if (!ops)
867 return NULL;
868
869 ops[0].op = opcode;
870
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700871 /*
872 * op extent offset and length will be set later on
873 * in calc_raw_layout()
874 */
Alex Elder57cfc102012-06-26 12:57:03 -0700875 ops[0].payload_len = payload_len;
876
877 return ops;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700878}
879
880static void rbd_destroy_ops(struct ceph_osd_req_op *ops)
881{
882 kfree(ops);
883}
884
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700885static void rbd_coll_end_req_index(struct request *rq,
886 struct rbd_req_coll *coll,
887 int index,
888 int ret, u64 len)
889{
890 struct request_queue *q;
891 int min, max, i;
892
Alex Elderbd919d42012-07-13 20:35:11 -0500893 dout("rbd_coll_end_req_index %p index %d ret %d len %llu\n",
894 coll, index, ret, (unsigned long long) len);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700895
896 if (!rq)
897 return;
898
899 if (!coll) {
900 blk_end_request(rq, ret, len);
901 return;
902 }
903
904 q = rq->q;
905
906 spin_lock_irq(q->queue_lock);
907 coll->status[index].done = 1;
908 coll->status[index].rc = ret;
909 coll->status[index].bytes = len;
910 max = min = coll->num_done;
911 while (max < coll->total && coll->status[max].done)
912 max++;
913
914 for (i = min; i<max; i++) {
915 __blk_end_request(rq, coll->status[i].rc,
916 coll->status[i].bytes);
917 coll->num_done++;
918 kref_put(&coll->kref, rbd_coll_release);
919 }
920 spin_unlock_irq(q->queue_lock);
921}
922
923static void rbd_coll_end_req(struct rbd_request *req,
924 int ret, u64 len)
925{
926 rbd_coll_end_req_index(req->rq, req->coll, req->coll_index, ret, len);
927}
928
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700929/*
930 * Send ceph osd request
931 */
932static int rbd_do_request(struct request *rq,
Alex Elder0ce1a792012-07-03 16:01:18 -0500933 struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700934 struct ceph_snap_context *snapc,
935 u64 snapid,
Alex Elderaded07e2012-07-03 16:01:18 -0500936 const char *object_name, u64 ofs, u64 len,
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700937 struct bio *bio,
938 struct page **pages,
939 int num_pages,
940 int flags,
941 struct ceph_osd_req_op *ops,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700942 struct rbd_req_coll *coll,
943 int coll_index,
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700944 void (*rbd_cb)(struct ceph_osd_request *req,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700945 struct ceph_msg *msg),
946 struct ceph_osd_request **linger_req,
947 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700948{
949 struct ceph_osd_request *req;
950 struct ceph_file_layout *layout;
951 int ret;
952 u64 bno;
953 struct timespec mtime = CURRENT_TIME;
954 struct rbd_request *req_data;
955 struct ceph_osd_request_head *reqhead;
Alex Elder1dbb4392012-01-24 10:08:37 -0600956 struct ceph_osd_client *osdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700957
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700958 req_data = kzalloc(sizeof(*req_data), GFP_NOIO);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700959 if (!req_data) {
960 if (coll)
961 rbd_coll_end_req_index(rq, coll, coll_index,
962 -ENOMEM, len);
963 return -ENOMEM;
964 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700965
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700966 if (coll) {
967 req_data->coll = coll;
968 req_data->coll_index = coll_index;
969 }
970
Alex Elderbd919d42012-07-13 20:35:11 -0500971 dout("rbd_do_request object_name=%s ofs=%llu len=%llu\n", object_name,
972 (unsigned long long) ofs, (unsigned long long) len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700973
Alex Elder0ce1a792012-07-03 16:01:18 -0500974 osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder1dbb4392012-01-24 10:08:37 -0600975 req = ceph_osdc_alloc_request(osdc, flags, snapc, ops,
976 false, GFP_NOIO, pages, bio);
Sage Weil4ad12622011-05-03 09:23:36 -0700977 if (!req) {
Sage Weil4ad12622011-05-03 09:23:36 -0700978 ret = -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700979 goto done_pages;
980 }
981
982 req->r_callback = rbd_cb;
983
984 req_data->rq = rq;
985 req_data->bio = bio;
986 req_data->pages = pages;
987 req_data->len = len;
988
989 req->r_priv = req_data;
990
991 reqhead = req->r_request->front.iov_base;
992 reqhead->snapid = cpu_to_le64(CEPH_NOSNAP);
993
Alex Elderaded07e2012-07-03 16:01:18 -0500994 strncpy(req->r_oid, object_name, sizeof(req->r_oid));
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700995 req->r_oid_len = strlen(req->r_oid);
996
997 layout = &req->r_file_layout;
998 memset(layout, 0, sizeof(*layout));
999 layout->fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
1000 layout->fl_stripe_count = cpu_to_le32(1);
1001 layout->fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
Alex Elder0ce1a792012-07-03 16:01:18 -05001002 layout->fl_pg_pool = cpu_to_le32(rbd_dev->pool_id);
Alex Elder1dbb4392012-01-24 10:08:37 -06001003 ceph_calc_raw_layout(osdc, layout, snapid, ofs, &len, &bno,
1004 req, ops);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001005
1006 ceph_osdc_build_request(req, ofs, &len,
1007 ops,
1008 snapc,
1009 &mtime,
1010 req->r_oid, req->r_oid_len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001011
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001012 if (linger_req) {
Alex Elder1dbb4392012-01-24 10:08:37 -06001013 ceph_osdc_set_request_linger(osdc, req);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001014 *linger_req = req;
1015 }
1016
Alex Elder1dbb4392012-01-24 10:08:37 -06001017 ret = ceph_osdc_start_request(osdc, req, false);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001018 if (ret < 0)
1019 goto done_err;
1020
1021 if (!rbd_cb) {
Alex Elder1dbb4392012-01-24 10:08:37 -06001022 ret = ceph_osdc_wait_request(osdc, req);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001023 if (ver)
1024 *ver = le64_to_cpu(req->r_reassert_version.version);
Alex Elderbd919d42012-07-13 20:35:11 -05001025 dout("reassert_ver=%llu\n",
1026 (unsigned long long)
1027 le64_to_cpu(req->r_reassert_version.version));
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001028 ceph_osdc_put_request(req);
1029 }
1030 return ret;
1031
1032done_err:
1033 bio_chain_put(req_data->bio);
1034 ceph_osdc_put_request(req);
1035done_pages:
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001036 rbd_coll_end_req(req_data, ret, len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001037 kfree(req_data);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001038 return ret;
1039}
1040
1041/*
1042 * Ceph osd op callback
1043 */
1044static void rbd_req_cb(struct ceph_osd_request *req, struct ceph_msg *msg)
1045{
1046 struct rbd_request *req_data = req->r_priv;
1047 struct ceph_osd_reply_head *replyhead;
1048 struct ceph_osd_op *op;
1049 __s32 rc;
1050 u64 bytes;
1051 int read_op;
1052
1053 /* parse reply */
1054 replyhead = msg->front.iov_base;
1055 WARN_ON(le32_to_cpu(replyhead->num_ops) == 0);
1056 op = (void *)(replyhead + 1);
1057 rc = le32_to_cpu(replyhead->result);
1058 bytes = le64_to_cpu(op->extent.length);
Dan Carpenter895cfcc2012-06-06 09:15:33 -05001059 read_op = (le16_to_cpu(op->op) == CEPH_OSD_OP_READ);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001060
Alex Elderbd919d42012-07-13 20:35:11 -05001061 dout("rbd_req_cb bytes=%llu readop=%d rc=%d\n",
1062 (unsigned long long) bytes, read_op, (int) rc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001063
1064 if (rc == -ENOENT && read_op) {
1065 zero_bio_chain(req_data->bio, 0);
1066 rc = 0;
1067 } else if (rc == 0 && read_op && bytes < req_data->len) {
1068 zero_bio_chain(req_data->bio, bytes);
1069 bytes = req_data->len;
1070 }
1071
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001072 rbd_coll_end_req(req_data, rc, bytes);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001073
1074 if (req_data->bio)
1075 bio_chain_put(req_data->bio);
1076
1077 ceph_osdc_put_request(req);
1078 kfree(req_data);
1079}
1080
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001081static void rbd_simple_req_cb(struct ceph_osd_request *req, struct ceph_msg *msg)
1082{
1083 ceph_osdc_put_request(req);
1084}
1085
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001086/*
1087 * Do a synchronous ceph osd operation
1088 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001089static int rbd_req_sync_op(struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001090 struct ceph_snap_context *snapc,
1091 u64 snapid,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001092 int flags,
Alex Elder913d2fd2012-06-26 12:57:03 -07001093 struct ceph_osd_req_op *ops,
Alex Elderaded07e2012-07-03 16:01:18 -05001094 const char *object_name,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001095 u64 ofs, u64 len,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001096 char *buf,
1097 struct ceph_osd_request **linger_req,
1098 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001099{
1100 int ret;
1101 struct page **pages;
1102 int num_pages;
Alex Elder913d2fd2012-06-26 12:57:03 -07001103
1104 BUG_ON(ops == NULL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001105
1106 num_pages = calc_pages_for(ofs , len);
1107 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
Dan Carpenterb8d06382010-10-11 21:14:23 +02001108 if (IS_ERR(pages))
1109 return PTR_ERR(pages);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001110
Alex Elder0ce1a792012-07-03 16:01:18 -05001111 ret = rbd_do_request(NULL, rbd_dev, snapc, snapid,
Alex Elderaded07e2012-07-03 16:01:18 -05001112 object_name, ofs, len, NULL,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001113 pages, num_pages,
1114 flags,
1115 ops,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001116 NULL, 0,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001117 NULL,
1118 linger_req, ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001119 if (ret < 0)
Alex Elder913d2fd2012-06-26 12:57:03 -07001120 goto done;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001121
1122 if ((flags & CEPH_OSD_FLAG_READ) && buf)
1123 ret = ceph_copy_from_page_vector(pages, buf, ofs, ret);
1124
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001125done:
1126 ceph_release_page_vector(pages, num_pages);
1127 return ret;
1128}
1129
1130/*
1131 * Do an asynchronous ceph osd operation
1132 */
1133static int rbd_do_op(struct request *rq,
Alex Elder0ce1a792012-07-03 16:01:18 -05001134 struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001135 struct ceph_snap_context *snapc,
1136 u64 snapid,
Alex Elderd1f57ea2012-06-26 12:57:03 -07001137 int opcode, int flags,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001138 u64 ofs, u64 len,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001139 struct bio *bio,
1140 struct rbd_req_coll *coll,
1141 int coll_index)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001142{
1143 char *seg_name;
1144 u64 seg_ofs;
1145 u64 seg_len;
1146 int ret;
1147 struct ceph_osd_req_op *ops;
1148 u32 payload_len;
1149
Alex Elder65ccfe22012-08-09 10:33:26 -07001150 seg_name = rbd_segment_name(rbd_dev, ofs);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001151 if (!seg_name)
1152 return -ENOMEM;
Alex Elder65ccfe22012-08-09 10:33:26 -07001153 seg_len = rbd_segment_length(rbd_dev, ofs, len);
1154 seg_ofs = rbd_segment_offset(rbd_dev, ofs);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001155
1156 payload_len = (flags & CEPH_OSD_FLAG_WRITE ? seg_len : 0);
1157
Alex Elder57cfc102012-06-26 12:57:03 -07001158 ret = -ENOMEM;
1159 ops = rbd_create_rw_ops(1, opcode, payload_len);
1160 if (!ops)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001161 goto done;
1162
1163 /* we've taken care of segment sizes earlier when we
1164 cloned the bios. We should never have a segment
1165 truncated at this point */
1166 BUG_ON(seg_len < len);
1167
1168 ret = rbd_do_request(rq, rbd_dev, snapc, snapid,
1169 seg_name, seg_ofs, seg_len,
1170 bio,
1171 NULL, 0,
1172 flags,
1173 ops,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001174 coll, coll_index,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001175 rbd_req_cb, 0, NULL);
Sage Weil11f77002011-05-12 16:13:54 -07001176
1177 rbd_destroy_ops(ops);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001178done:
1179 kfree(seg_name);
1180 return ret;
1181}
1182
1183/*
1184 * Request async osd write
1185 */
1186static int rbd_req_write(struct request *rq,
1187 struct rbd_device *rbd_dev,
1188 struct ceph_snap_context *snapc,
1189 u64 ofs, u64 len,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001190 struct bio *bio,
1191 struct rbd_req_coll *coll,
1192 int coll_index)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001193{
1194 return rbd_do_op(rq, rbd_dev, snapc, CEPH_NOSNAP,
1195 CEPH_OSD_OP_WRITE,
1196 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001197 ofs, len, bio, coll, coll_index);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001198}
1199
1200/*
1201 * Request async osd read
1202 */
1203static int rbd_req_read(struct request *rq,
1204 struct rbd_device *rbd_dev,
1205 u64 snapid,
1206 u64 ofs, u64 len,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001207 struct bio *bio,
1208 struct rbd_req_coll *coll,
1209 int coll_index)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001210{
1211 return rbd_do_op(rq, rbd_dev, NULL,
Josh Durginb06e6a62011-11-21 18:16:52 -08001212 snapid,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001213 CEPH_OSD_OP_READ,
1214 CEPH_OSD_FLAG_READ,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001215 ofs, len, bio, coll, coll_index);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001216}
1217
1218/*
1219 * Request sync osd read
1220 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001221static int rbd_req_sync_read(struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001222 u64 snapid,
Alex Elderaded07e2012-07-03 16:01:18 -05001223 const char *object_name,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001224 u64 ofs, u64 len,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001225 char *buf,
1226 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001227{
Alex Elder913d2fd2012-06-26 12:57:03 -07001228 struct ceph_osd_req_op *ops;
1229 int ret;
1230
1231 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_READ, 0);
1232 if (!ops)
1233 return -ENOMEM;
1234
1235 ret = rbd_req_sync_op(rbd_dev, NULL,
Josh Durginb06e6a62011-11-21 18:16:52 -08001236 snapid,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001237 CEPH_OSD_FLAG_READ,
Alex Elder913d2fd2012-06-26 12:57:03 -07001238 ops, object_name, ofs, len, buf, NULL, ver);
1239 rbd_destroy_ops(ops);
1240
1241 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001242}
1243
1244/*
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001245 * Request sync osd watch
1246 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001247static int rbd_req_sync_notify_ack(struct rbd_device *rbd_dev,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001248 u64 ver,
Alex Elder7f0a24d2012-07-25 09:32:40 -05001249 u64 notify_id)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001250{
1251 struct ceph_osd_req_op *ops;
Sage Weil11f77002011-05-12 16:13:54 -07001252 int ret;
1253
Alex Elder57cfc102012-06-26 12:57:03 -07001254 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_NOTIFY_ACK, 0);
1255 if (!ops)
1256 return -ENOMEM;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001257
Josh Durgina71b8912011-12-05 18:10:44 -08001258 ops[0].watch.ver = cpu_to_le64(ver);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001259 ops[0].watch.cookie = notify_id;
1260 ops[0].watch.flag = 0;
1261
Alex Elder0ce1a792012-07-03 16:01:18 -05001262 ret = rbd_do_request(NULL, rbd_dev, NULL, CEPH_NOSNAP,
Alex Elder7f0a24d2012-07-25 09:32:40 -05001263 rbd_dev->header_name, 0, 0, NULL,
Alex Elderad4f2322012-07-03 16:01:19 -05001264 NULL, 0,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001265 CEPH_OSD_FLAG_READ,
1266 ops,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001267 NULL, 0,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001268 rbd_simple_req_cb, 0, NULL);
1269
1270 rbd_destroy_ops(ops);
1271 return ret;
1272}
1273
1274static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
1275{
Alex Elder0ce1a792012-07-03 16:01:18 -05001276 struct rbd_device *rbd_dev = (struct rbd_device *)data;
Josh Durgina71b8912011-12-05 18:10:44 -08001277 u64 hver;
Sage Weil13143d22011-05-12 16:08:30 -07001278 int rc;
1279
Alex Elder0ce1a792012-07-03 16:01:18 -05001280 if (!rbd_dev)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001281 return;
1282
Alex Elderbd919d42012-07-13 20:35:11 -05001283 dout("rbd_watch_cb %s notify_id=%llu opcode=%u\n",
1284 rbd_dev->header_name, (unsigned long long) notify_id,
1285 (unsigned int) opcode);
Alex Elder1fe5e992012-07-25 09:32:41 -05001286 rc = rbd_refresh_header(rbd_dev, &hver);
Sage Weil13143d22011-05-12 16:08:30 -07001287 if (rc)
Alex Elderf0f8cef2012-01-29 13:57:44 -06001288 pr_warning(RBD_DRV_NAME "%d got notification but failed to "
Alex Elder0ce1a792012-07-03 16:01:18 -05001289 " update snaps: %d\n", rbd_dev->major, rc);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001290
Alex Elder7f0a24d2012-07-25 09:32:40 -05001291 rbd_req_sync_notify_ack(rbd_dev, hver, notify_id);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001292}
1293
1294/*
1295 * Request sync osd watch
1296 */
Alex Elder0e6f3222012-07-25 09:32:40 -05001297static int rbd_req_sync_watch(struct rbd_device *rbd_dev)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001298{
1299 struct ceph_osd_req_op *ops;
Alex Elder0ce1a792012-07-03 16:01:18 -05001300 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder57cfc102012-06-26 12:57:03 -07001301 int ret;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001302
Alex Elder57cfc102012-06-26 12:57:03 -07001303 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_WATCH, 0);
1304 if (!ops)
1305 return -ENOMEM;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001306
1307 ret = ceph_osdc_create_event(osdc, rbd_watch_cb, 0,
Alex Elder0ce1a792012-07-03 16:01:18 -05001308 (void *)rbd_dev, &rbd_dev->watch_event);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001309 if (ret < 0)
1310 goto fail;
1311
Alex Elder0e6f3222012-07-25 09:32:40 -05001312 ops[0].watch.ver = cpu_to_le64(rbd_dev->header.obj_version);
Alex Elder0ce1a792012-07-03 16:01:18 -05001313 ops[0].watch.cookie = cpu_to_le64(rbd_dev->watch_event->cookie);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001314 ops[0].watch.flag = 1;
1315
Alex Elder0ce1a792012-07-03 16:01:18 -05001316 ret = rbd_req_sync_op(rbd_dev, NULL,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001317 CEPH_NOSNAP,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001318 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1319 ops,
Alex Elder0e6f3222012-07-25 09:32:40 -05001320 rbd_dev->header_name,
1321 0, 0, NULL,
Alex Elder0ce1a792012-07-03 16:01:18 -05001322 &rbd_dev->watch_request, NULL);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001323
1324 if (ret < 0)
1325 goto fail_event;
1326
1327 rbd_destroy_ops(ops);
1328 return 0;
1329
1330fail_event:
Alex Elder0ce1a792012-07-03 16:01:18 -05001331 ceph_osdc_cancel_event(rbd_dev->watch_event);
1332 rbd_dev->watch_event = NULL;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001333fail:
1334 rbd_destroy_ops(ops);
1335 return ret;
1336}
1337
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001338/*
1339 * Request sync osd unwatch
1340 */
Alex Elder070c6332012-07-25 09:32:41 -05001341static int rbd_req_sync_unwatch(struct rbd_device *rbd_dev)
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001342{
1343 struct ceph_osd_req_op *ops;
Alex Elder57cfc102012-06-26 12:57:03 -07001344 int ret;
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001345
Alex Elder57cfc102012-06-26 12:57:03 -07001346 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_WATCH, 0);
1347 if (!ops)
1348 return -ENOMEM;
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001349
1350 ops[0].watch.ver = 0;
Alex Elder0ce1a792012-07-03 16:01:18 -05001351 ops[0].watch.cookie = cpu_to_le64(rbd_dev->watch_event->cookie);
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001352 ops[0].watch.flag = 0;
1353
Alex Elder0ce1a792012-07-03 16:01:18 -05001354 ret = rbd_req_sync_op(rbd_dev, NULL,
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001355 CEPH_NOSNAP,
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001356 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1357 ops,
Alex Elder070c6332012-07-25 09:32:41 -05001358 rbd_dev->header_name,
1359 0, 0, NULL, NULL, NULL);
1360
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001361
1362 rbd_destroy_ops(ops);
Alex Elder0ce1a792012-07-03 16:01:18 -05001363 ceph_osdc_cancel_event(rbd_dev->watch_event);
1364 rbd_dev->watch_event = NULL;
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001365 return ret;
1366}
1367
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001368struct rbd_notify_info {
Alex Elder0ce1a792012-07-03 16:01:18 -05001369 struct rbd_device *rbd_dev;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001370};
1371
1372static void rbd_notify_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
1373{
Alex Elder0ce1a792012-07-03 16:01:18 -05001374 struct rbd_device *rbd_dev = (struct rbd_device *)data;
1375 if (!rbd_dev)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001376 return;
1377
Alex Elderbd919d42012-07-13 20:35:11 -05001378 dout("rbd_notify_cb %s notify_id=%llu opcode=%u\n",
1379 rbd_dev->header_name, (unsigned long long) notify_id,
1380 (unsigned int) opcode);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001381}
1382
1383/*
1384 * Request sync osd notify
1385 */
Alex Elder4cb16252012-07-25 09:32:40 -05001386static int rbd_req_sync_notify(struct rbd_device *rbd_dev)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001387{
1388 struct ceph_osd_req_op *ops;
Alex Elder0ce1a792012-07-03 16:01:18 -05001389 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001390 struct ceph_osd_event *event;
1391 struct rbd_notify_info info;
1392 int payload_len = sizeof(u32) + sizeof(u32);
1393 int ret;
1394
Alex Elder57cfc102012-06-26 12:57:03 -07001395 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_NOTIFY, payload_len);
1396 if (!ops)
1397 return -ENOMEM;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001398
Alex Elder0ce1a792012-07-03 16:01:18 -05001399 info.rbd_dev = rbd_dev;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001400
1401 ret = ceph_osdc_create_event(osdc, rbd_notify_cb, 1,
1402 (void *)&info, &event);
1403 if (ret < 0)
1404 goto fail;
1405
1406 ops[0].watch.ver = 1;
1407 ops[0].watch.flag = 1;
1408 ops[0].watch.cookie = event->cookie;
1409 ops[0].watch.prot_ver = RADOS_NOTIFY_VER;
1410 ops[0].watch.timeout = 12;
1411
Alex Elder0ce1a792012-07-03 16:01:18 -05001412 ret = rbd_req_sync_op(rbd_dev, NULL,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001413 CEPH_NOSNAP,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001414 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1415 ops,
Alex Elder4cb16252012-07-25 09:32:40 -05001416 rbd_dev->header_name,
1417 0, 0, NULL, NULL, NULL);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001418 if (ret < 0)
1419 goto fail_event;
1420
1421 ret = ceph_osdc_wait_event(event, CEPH_OSD_TIMEOUT_DEFAULT);
1422 dout("ceph_osdc_wait_event returned %d\n", ret);
1423 rbd_destroy_ops(ops);
1424 return 0;
1425
1426fail_event:
1427 ceph_osdc_cancel_event(event);
1428fail:
1429 rbd_destroy_ops(ops);
1430 return ret;
1431}
1432
1433/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001434 * Request sync osd read
1435 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001436static int rbd_req_sync_exec(struct rbd_device *rbd_dev,
Alex Elderaded07e2012-07-03 16:01:18 -05001437 const char *object_name,
1438 const char *class_name,
1439 const char *method_name,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001440 const char *data,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001441 int len,
1442 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001443{
1444 struct ceph_osd_req_op *ops;
Alex Elderaded07e2012-07-03 16:01:18 -05001445 int class_name_len = strlen(class_name);
1446 int method_name_len = strlen(method_name);
Alex Elder57cfc102012-06-26 12:57:03 -07001447 int ret;
1448
1449 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_CALL,
Alex Elderaded07e2012-07-03 16:01:18 -05001450 class_name_len + method_name_len + len);
Alex Elder57cfc102012-06-26 12:57:03 -07001451 if (!ops)
1452 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001453
Alex Elderaded07e2012-07-03 16:01:18 -05001454 ops[0].cls.class_name = class_name;
1455 ops[0].cls.class_len = (__u8) class_name_len;
1456 ops[0].cls.method_name = method_name;
1457 ops[0].cls.method_len = (__u8) method_name_len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001458 ops[0].cls.argc = 0;
1459 ops[0].cls.indata = data;
1460 ops[0].cls.indata_len = len;
1461
Alex Elder0ce1a792012-07-03 16:01:18 -05001462 ret = rbd_req_sync_op(rbd_dev, NULL,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001463 CEPH_NOSNAP,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001464 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1465 ops,
Alex Elderd1f57ea2012-06-26 12:57:03 -07001466 object_name, 0, 0, NULL, NULL, ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001467
1468 rbd_destroy_ops(ops);
1469
1470 dout("cls_exec returned %d\n", ret);
1471 return ret;
1472}
1473
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001474static struct rbd_req_coll *rbd_alloc_coll(int num_reqs)
1475{
1476 struct rbd_req_coll *coll =
1477 kzalloc(sizeof(struct rbd_req_coll) +
1478 sizeof(struct rbd_req_status) * num_reqs,
1479 GFP_ATOMIC);
1480
1481 if (!coll)
1482 return NULL;
1483 coll->total = num_reqs;
1484 kref_init(&coll->kref);
1485 return coll;
1486}
1487
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001488/*
1489 * block device queue callback
1490 */
1491static void rbd_rq_fn(struct request_queue *q)
1492{
1493 struct rbd_device *rbd_dev = q->queuedata;
1494 struct request *rq;
1495 struct bio_pair *bp = NULL;
1496
Alex Elder00f1f362012-02-07 12:03:36 -06001497 while ((rq = blk_fetch_request(q))) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001498 struct bio *bio;
1499 struct bio *rq_bio, *next_bio = NULL;
1500 bool do_write;
Alex Elderbd919d42012-07-13 20:35:11 -05001501 unsigned int size;
1502 u64 op_size = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001503 u64 ofs;
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001504 int num_segs, cur_seg = 0;
1505 struct rbd_req_coll *coll;
Josh Durgind1d25642011-12-05 14:03:05 -08001506 struct ceph_snap_context *snapc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001507
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001508 dout("fetched request\n");
1509
1510 /* filter out block requests we don't understand */
1511 if ((rq->cmd_type != REQ_TYPE_FS)) {
1512 __blk_end_request_all(rq, 0);
Alex Elder00f1f362012-02-07 12:03:36 -06001513 continue;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001514 }
1515
1516 /* deduce our operation (read, write) */
1517 do_write = (rq_data_dir(rq) == WRITE);
1518
1519 size = blk_rq_bytes(rq);
Alex Elder593a9e72012-02-07 12:03:37 -06001520 ofs = blk_rq_pos(rq) * SECTOR_SIZE;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001521 rq_bio = rq->bio;
1522 if (do_write && rbd_dev->read_only) {
1523 __blk_end_request_all(rq, -EROFS);
Alex Elder00f1f362012-02-07 12:03:36 -06001524 continue;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001525 }
1526
1527 spin_unlock_irq(q->queue_lock);
1528
Josh Durgind1d25642011-12-05 14:03:05 -08001529 down_read(&rbd_dev->header_rwsem);
Josh Durgine88a36e2011-11-21 18:14:25 -08001530
Josh Durgind1d25642011-12-05 14:03:05 -08001531 if (rbd_dev->snap_id != CEPH_NOSNAP && !rbd_dev->snap_exists) {
Josh Durgine88a36e2011-11-21 18:14:25 -08001532 up_read(&rbd_dev->header_rwsem);
Josh Durgind1d25642011-12-05 14:03:05 -08001533 dout("request for non-existent snapshot");
1534 spin_lock_irq(q->queue_lock);
1535 __blk_end_request_all(rq, -ENXIO);
1536 continue;
Josh Durgine88a36e2011-11-21 18:14:25 -08001537 }
1538
Josh Durgind1d25642011-12-05 14:03:05 -08001539 snapc = ceph_get_snap_context(rbd_dev->header.snapc);
1540
1541 up_read(&rbd_dev->header_rwsem);
1542
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001543 dout("%s 0x%x bytes at 0x%llx\n",
1544 do_write ? "write" : "read",
Alex Elderbd919d42012-07-13 20:35:11 -05001545 size, (unsigned long long) blk_rq_pos(rq) * SECTOR_SIZE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001546
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001547 num_segs = rbd_get_num_segments(&rbd_dev->header, ofs, size);
Alex Elderdf111be2012-08-09 10:33:26 -07001548 if (num_segs <= 0) {
1549 spin_lock_irq(q->queue_lock);
1550 __blk_end_request_all(rq, num_segs);
1551 ceph_put_snap_context(snapc);
1552 continue;
1553 }
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001554 coll = rbd_alloc_coll(num_segs);
1555 if (!coll) {
1556 spin_lock_irq(q->queue_lock);
1557 __blk_end_request_all(rq, -ENOMEM);
Josh Durgind1d25642011-12-05 14:03:05 -08001558 ceph_put_snap_context(snapc);
Alex Elder00f1f362012-02-07 12:03:36 -06001559 continue;
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001560 }
1561
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001562 do {
1563 /* a bio clone to be passed down to OSD req */
Alex Elderbd919d42012-07-13 20:35:11 -05001564 dout("rq->bio->bi_vcnt=%hu\n", rq->bio->bi_vcnt);
Alex Elder65ccfe22012-08-09 10:33:26 -07001565 op_size = rbd_segment_length(rbd_dev, ofs, size);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001566 kref_get(&coll->kref);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001567 bio = bio_chain_clone(&rq_bio, &next_bio, &bp,
1568 op_size, GFP_ATOMIC);
1569 if (!bio) {
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001570 rbd_coll_end_req_index(rq, coll, cur_seg,
1571 -ENOMEM, op_size);
1572 goto next_seg;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001573 }
1574
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001575
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001576 /* init OSD command: write or read */
1577 if (do_write)
1578 rbd_req_write(rq, rbd_dev,
Josh Durgind1d25642011-12-05 14:03:05 -08001579 snapc,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001580 ofs,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001581 op_size, bio,
1582 coll, cur_seg);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001583 else
1584 rbd_req_read(rq, rbd_dev,
Josh Durgin77dfe992011-11-21 13:04:42 -08001585 rbd_dev->snap_id,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001586 ofs,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001587 op_size, bio,
1588 coll, cur_seg);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001589
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001590next_seg:
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001591 size -= op_size;
1592 ofs += op_size;
1593
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001594 cur_seg++;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001595 rq_bio = next_bio;
1596 } while (size > 0);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001597 kref_put(&coll->kref, rbd_coll_release);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001598
1599 if (bp)
1600 bio_pair_release(bp);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001601 spin_lock_irq(q->queue_lock);
Josh Durgind1d25642011-12-05 14:03:05 -08001602
1603 ceph_put_snap_context(snapc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001604 }
1605}
1606
1607/*
1608 * a queue callback. Makes sure that we don't create a bio that spans across
1609 * multiple osd objects. One exception would be with a single page bios,
1610 * which we handle later at bio_chain_clone
1611 */
1612static int rbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd,
1613 struct bio_vec *bvec)
1614{
1615 struct rbd_device *rbd_dev = q->queuedata;
Alex Elder593a9e72012-02-07 12:03:37 -06001616 unsigned int chunk_sectors;
1617 sector_t sector;
1618 unsigned int bio_sectors;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001619 int max;
1620
Alex Elder593a9e72012-02-07 12:03:37 -06001621 chunk_sectors = 1 << (rbd_dev->header.obj_order - SECTOR_SHIFT);
1622 sector = bmd->bi_sector + get_start_sect(bmd->bi_bdev);
1623 bio_sectors = bmd->bi_size >> SECTOR_SHIFT;
1624
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001625 max = (chunk_sectors - ((sector & (chunk_sectors - 1))
Alex Elder593a9e72012-02-07 12:03:37 -06001626 + bio_sectors)) << SECTOR_SHIFT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001627 if (max < 0)
1628 max = 0; /* bio_add cannot handle a negative return */
1629 if (max <= bvec->bv_len && bio_sectors == 0)
1630 return bvec->bv_len;
1631 return max;
1632}
1633
1634static void rbd_free_disk(struct rbd_device *rbd_dev)
1635{
1636 struct gendisk *disk = rbd_dev->disk;
1637
1638 if (!disk)
1639 return;
1640
1641 rbd_header_free(&rbd_dev->header);
1642
1643 if (disk->flags & GENHD_FL_UP)
1644 del_gendisk(disk);
1645 if (disk->queue)
1646 blk_cleanup_queue(disk->queue);
1647 put_disk(disk);
1648}
1649
1650/*
Alex Elder4156d992012-08-02 11:29:46 -05001651 * Read the complete header for the given rbd device.
1652 *
1653 * Returns a pointer to a dynamically-allocated buffer containing
1654 * the complete and validated header. Caller can pass the address
1655 * of a variable that will be filled in with the version of the
1656 * header object at the time it was read.
1657 *
1658 * Returns a pointer-coded errno if a failure occurs.
1659 */
1660static struct rbd_image_header_ondisk *
1661rbd_dev_v1_header_read(struct rbd_device *rbd_dev, u64 *version)
1662{
1663 struct rbd_image_header_ondisk *ondisk = NULL;
1664 u32 snap_count = 0;
1665 u64 names_size = 0;
1666 u32 want_count;
1667 int ret;
1668
1669 /*
1670 * The complete header will include an array of its 64-bit
1671 * snapshot ids, followed by the names of those snapshots as
1672 * a contiguous block of NUL-terminated strings. Note that
1673 * the number of snapshots could change by the time we read
1674 * it in, in which case we re-read it.
1675 */
1676 do {
1677 size_t size;
1678
1679 kfree(ondisk);
1680
1681 size = sizeof (*ondisk);
1682 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
1683 size += names_size;
1684 ondisk = kmalloc(size, GFP_KERNEL);
1685 if (!ondisk)
1686 return ERR_PTR(-ENOMEM);
1687
1688 ret = rbd_req_sync_read(rbd_dev, CEPH_NOSNAP,
1689 rbd_dev->header_name,
1690 0, size,
1691 (char *) ondisk, version);
1692
1693 if (ret < 0)
1694 goto out_err;
1695 if (WARN_ON((size_t) ret < size)) {
1696 ret = -ENXIO;
1697 pr_warning("short header read for image %s"
1698 " (want %zd got %d)\n",
1699 rbd_dev->image_name, size, ret);
1700 goto out_err;
1701 }
1702 if (!rbd_dev_ondisk_valid(ondisk)) {
1703 ret = -ENXIO;
1704 pr_warning("invalid header for image %s\n",
1705 rbd_dev->image_name);
1706 goto out_err;
1707 }
1708
1709 names_size = le64_to_cpu(ondisk->snap_names_len);
1710 want_count = snap_count;
1711 snap_count = le32_to_cpu(ondisk->snap_count);
1712 } while (snap_count != want_count);
1713
1714 return ondisk;
1715
1716out_err:
1717 kfree(ondisk);
1718
1719 return ERR_PTR(ret);
1720}
1721
1722/*
1723 * reload the ondisk the header
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001724 */
1725static int rbd_read_header(struct rbd_device *rbd_dev,
1726 struct rbd_image_header *header)
1727{
Alex Elder4156d992012-08-02 11:29:46 -05001728 struct rbd_image_header_ondisk *ondisk;
1729 u64 ver = 0;
1730 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001731
Alex Elder4156d992012-08-02 11:29:46 -05001732 ondisk = rbd_dev_v1_header_read(rbd_dev, &ver);
1733 if (IS_ERR(ondisk))
1734 return PTR_ERR(ondisk);
1735 ret = rbd_header_from_disk(header, ondisk);
1736 if (ret >= 0)
1737 header->obj_version = ver;
1738 kfree(ondisk);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001739
Alex Elder4156d992012-08-02 11:29:46 -05001740 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001741}
1742
1743/*
1744 * create a snapshot
1745 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001746static int rbd_header_add_snap(struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001747 const char *snap_name,
1748 gfp_t gfp_flags)
1749{
1750 int name_len = strlen(snap_name);
1751 u64 new_snapid;
1752 int ret;
Sage Weil916d4d62011-05-12 16:10:50 -07001753 void *data, *p, *e;
Alex Elder1dbb4392012-01-24 10:08:37 -06001754 struct ceph_mon_client *monc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001755
1756 /* we should create a snapshot only if we're pointing at the head */
Alex Elder0ce1a792012-07-03 16:01:18 -05001757 if (rbd_dev->snap_id != CEPH_NOSNAP)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001758 return -EINVAL;
1759
Alex Elder0ce1a792012-07-03 16:01:18 -05001760 monc = &rbd_dev->rbd_client->client->monc;
1761 ret = ceph_monc_create_snapid(monc, rbd_dev->pool_id, &new_snapid);
Alex Elderbd919d42012-07-13 20:35:11 -05001762 dout("created snapid=%llu\n", (unsigned long long) new_snapid);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001763 if (ret < 0)
1764 return ret;
1765
1766 data = kmalloc(name_len + 16, gfp_flags);
1767 if (!data)
1768 return -ENOMEM;
1769
Sage Weil916d4d62011-05-12 16:10:50 -07001770 p = data;
1771 e = data + name_len + 16;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001772
Sage Weil916d4d62011-05-12 16:10:50 -07001773 ceph_encode_string_safe(&p, e, snap_name, name_len, bad);
1774 ceph_encode_64_safe(&p, e, new_snapid, bad);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001775
Alex Elder0bed54d2012-07-03 16:01:18 -05001776 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
Alex Elder0ce1a792012-07-03 16:01:18 -05001777 "rbd", "snap_add",
Alex Elderd67d4be2012-07-13 20:35:11 -05001778 data, p - data, NULL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001779
Sage Weil916d4d62011-05-12 16:10:50 -07001780 kfree(data);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001781
Alex Elder505cbb92012-07-19 08:49:18 -05001782 return ret < 0 ? ret : 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001783bad:
1784 return -ERANGE;
1785}
1786
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001787static void __rbd_remove_all_snaps(struct rbd_device *rbd_dev)
1788{
1789 struct rbd_snap *snap;
Alex Eldera0593292012-07-19 09:09:27 -05001790 struct rbd_snap *next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001791
Alex Eldera0593292012-07-19 09:09:27 -05001792 list_for_each_entry_safe(snap, next, &rbd_dev->snaps, node)
Alex Elder14e70852012-07-19 09:09:27 -05001793 __rbd_remove_snap_dev(snap);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001794}
1795
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001796/*
1797 * only read the first part of the ondisk header, without the snaps info
1798 */
Alex Elderb8136232012-07-25 09:32:41 -05001799static int __rbd_refresh_header(struct rbd_device *rbd_dev, u64 *hver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001800{
1801 int ret;
1802 struct rbd_image_header h;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001803
1804 ret = rbd_read_header(rbd_dev, &h);
1805 if (ret < 0)
1806 return ret;
1807
Josh Durgina51aa0c2011-12-05 10:35:04 -08001808 down_write(&rbd_dev->header_rwsem);
1809
Sage Weil9db4b3e2011-04-19 22:49:06 -07001810 /* resized? */
Josh Durgin474ef7c2011-11-21 17:13:54 -08001811 if (rbd_dev->snap_id == CEPH_NOSNAP) {
1812 sector_t size = (sector_t) h.image_size / SECTOR_SIZE;
1813
1814 dout("setting size to %llu sectors", (unsigned long long) size);
1815 set_capacity(rbd_dev->disk, size);
1816 }
Sage Weil9db4b3e2011-04-19 22:49:06 -07001817
Alex Elder849b4262012-07-09 21:04:24 -05001818 /* rbd_dev->header.object_prefix shouldn't change */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001819 kfree(rbd_dev->header.snap_sizes);
Alex Elder849b4262012-07-09 21:04:24 -05001820 kfree(rbd_dev->header.snap_names);
Josh Durgind1d25642011-12-05 14:03:05 -08001821 /* osd requests may still refer to snapc */
1822 ceph_put_snap_context(rbd_dev->header.snapc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001823
Alex Elderb8136232012-07-25 09:32:41 -05001824 if (hver)
1825 *hver = h.obj_version;
Josh Durgina71b8912011-12-05 18:10:44 -08001826 rbd_dev->header.obj_version = h.obj_version;
Josh Durgin93a24e02011-12-05 10:41:28 -08001827 rbd_dev->header.image_size = h.image_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001828 rbd_dev->header.total_snaps = h.total_snaps;
1829 rbd_dev->header.snapc = h.snapc;
1830 rbd_dev->header.snap_names = h.snap_names;
1831 rbd_dev->header.snap_sizes = h.snap_sizes;
Alex Elder849b4262012-07-09 21:04:24 -05001832 /* Free the extra copy of the object prefix */
1833 WARN_ON(strcmp(rbd_dev->header.object_prefix, h.object_prefix));
1834 kfree(h.object_prefix);
1835
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001836 ret = __rbd_init_snaps_header(rbd_dev);
1837
Josh Durginc6666012011-11-21 17:11:12 -08001838 up_write(&rbd_dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001839
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001840 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001841}
1842
Alex Elder1fe5e992012-07-25 09:32:41 -05001843static int rbd_refresh_header(struct rbd_device *rbd_dev, u64 *hver)
1844{
1845 int ret;
1846
1847 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
1848 ret = __rbd_refresh_header(rbd_dev, hver);
1849 mutex_unlock(&ctl_mutex);
1850
1851 return ret;
1852}
1853
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001854static int rbd_init_disk(struct rbd_device *rbd_dev)
1855{
1856 struct gendisk *disk;
1857 struct request_queue *q;
1858 int rc;
Alex Elder593a9e72012-02-07 12:03:37 -06001859 u64 segment_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001860 u64 total_size = 0;
1861
1862 /* contact OSD, request size info about the object being mapped */
1863 rc = rbd_read_header(rbd_dev, &rbd_dev->header);
1864 if (rc)
1865 return rc;
1866
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001867 /* no need to lock here, as rbd_dev is not registered yet */
1868 rc = __rbd_init_snaps_header(rbd_dev);
1869 if (rc)
1870 return rc;
1871
Josh Durgincc9d7342011-11-21 18:19:13 -08001872 rc = rbd_header_set_snap(rbd_dev, &total_size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001873 if (rc)
1874 return rc;
1875
1876 /* create gendisk info */
1877 rc = -ENOMEM;
1878 disk = alloc_disk(RBD_MINORS_PER_MAJOR);
1879 if (!disk)
1880 goto out;
1881
Alex Elderf0f8cef2012-01-29 13:57:44 -06001882 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
Alex Elderde71a292012-07-03 16:01:19 -05001883 rbd_dev->dev_id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001884 disk->major = rbd_dev->major;
1885 disk->first_minor = 0;
1886 disk->fops = &rbd_bd_ops;
1887 disk->private_data = rbd_dev;
1888
1889 /* init rq */
1890 rc = -ENOMEM;
1891 q = blk_init_queue(rbd_rq_fn, &rbd_dev->lock);
1892 if (!q)
1893 goto out_disk;
Josh Durgin029bcbd2011-07-22 11:35:23 -07001894
Alex Elder593a9e72012-02-07 12:03:37 -06001895 /* We use the default size, but let's be explicit about it. */
1896 blk_queue_physical_block_size(q, SECTOR_SIZE);
1897
Josh Durgin029bcbd2011-07-22 11:35:23 -07001898 /* set io sizes to object size */
Alex Elder593a9e72012-02-07 12:03:37 -06001899 segment_size = rbd_obj_bytes(&rbd_dev->header);
1900 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
1901 blk_queue_max_segment_size(q, segment_size);
1902 blk_queue_io_min(q, segment_size);
1903 blk_queue_io_opt(q, segment_size);
Josh Durgin029bcbd2011-07-22 11:35:23 -07001904
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001905 blk_queue_merge_bvec(q, rbd_merge_bvec);
1906 disk->queue = q;
1907
1908 q->queuedata = rbd_dev;
1909
1910 rbd_dev->disk = disk;
1911 rbd_dev->q = q;
1912
1913 /* finally, announce the disk to the world */
Alex Elder593a9e72012-02-07 12:03:37 -06001914 set_capacity(disk, total_size / SECTOR_SIZE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001915 add_disk(disk);
1916
1917 pr_info("%s: added with size 0x%llx\n",
1918 disk->disk_name, (unsigned long long)total_size);
1919 return 0;
1920
1921out_disk:
1922 put_disk(disk);
1923out:
1924 return rc;
1925}
1926
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001927/*
1928 sysfs
1929*/
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001930
Alex Elder593a9e72012-02-07 12:03:37 -06001931static struct rbd_device *dev_to_rbd_dev(struct device *dev)
1932{
1933 return container_of(dev, struct rbd_device, dev);
1934}
1935
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001936static ssize_t rbd_size_show(struct device *dev,
1937 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001938{
Alex Elder593a9e72012-02-07 12:03:37 -06001939 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Josh Durgina51aa0c2011-12-05 10:35:04 -08001940 sector_t size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001941
Josh Durgina51aa0c2011-12-05 10:35:04 -08001942 down_read(&rbd_dev->header_rwsem);
1943 size = get_capacity(rbd_dev->disk);
1944 up_read(&rbd_dev->header_rwsem);
1945
1946 return sprintf(buf, "%llu\n", (unsigned long long) size * SECTOR_SIZE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001947}
1948
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001949static ssize_t rbd_major_show(struct device *dev,
1950 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001951{
Alex Elder593a9e72012-02-07 12:03:37 -06001952 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001953
1954 return sprintf(buf, "%d\n", rbd_dev->major);
1955}
1956
1957static ssize_t rbd_client_id_show(struct device *dev,
1958 struct device_attribute *attr, char *buf)
1959{
Alex Elder593a9e72012-02-07 12:03:37 -06001960 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001961
Alex Elder1dbb4392012-01-24 10:08:37 -06001962 return sprintf(buf, "client%lld\n",
1963 ceph_client_id(rbd_dev->rbd_client->client));
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001964}
1965
1966static ssize_t rbd_pool_show(struct device *dev,
1967 struct device_attribute *attr, char *buf)
1968{
Alex Elder593a9e72012-02-07 12:03:37 -06001969 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001970
1971 return sprintf(buf, "%s\n", rbd_dev->pool_name);
1972}
1973
Alex Elder9bb2f332012-07-12 10:46:35 -05001974static ssize_t rbd_pool_id_show(struct device *dev,
1975 struct device_attribute *attr, char *buf)
1976{
1977 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
1978
1979 return sprintf(buf, "%d\n", rbd_dev->pool_id);
1980}
1981
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001982static ssize_t rbd_name_show(struct device *dev,
1983 struct device_attribute *attr, char *buf)
1984{
Alex Elder593a9e72012-02-07 12:03:37 -06001985 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001986
Alex Elder0bed54d2012-07-03 16:01:18 -05001987 return sprintf(buf, "%s\n", rbd_dev->image_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001988}
1989
1990static ssize_t rbd_snap_show(struct device *dev,
1991 struct device_attribute *attr,
1992 char *buf)
1993{
Alex Elder593a9e72012-02-07 12:03:37 -06001994 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001995
1996 return sprintf(buf, "%s\n", rbd_dev->snap_name);
1997}
1998
1999static ssize_t rbd_image_refresh(struct device *dev,
2000 struct device_attribute *attr,
2001 const char *buf,
2002 size_t size)
2003{
Alex Elder593a9e72012-02-07 12:03:37 -06002004 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Alex Elderb8136232012-07-25 09:32:41 -05002005 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002006
Alex Elder1fe5e992012-07-25 09:32:41 -05002007 ret = rbd_refresh_header(rbd_dev, NULL);
Alex Elderb8136232012-07-25 09:32:41 -05002008
2009 return ret < 0 ? ret : size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002010}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002011
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002012static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
2013static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
2014static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
2015static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
Alex Elder9bb2f332012-07-12 10:46:35 -05002016static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002017static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
2018static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
2019static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
2020static DEVICE_ATTR(create_snap, S_IWUSR, NULL, rbd_snap_add);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002021
2022static struct attribute *rbd_attrs[] = {
2023 &dev_attr_size.attr,
2024 &dev_attr_major.attr,
2025 &dev_attr_client_id.attr,
2026 &dev_attr_pool.attr,
Alex Elder9bb2f332012-07-12 10:46:35 -05002027 &dev_attr_pool_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002028 &dev_attr_name.attr,
2029 &dev_attr_current_snap.attr,
2030 &dev_attr_refresh.attr,
2031 &dev_attr_create_snap.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002032 NULL
2033};
2034
2035static struct attribute_group rbd_attr_group = {
2036 .attrs = rbd_attrs,
2037};
2038
2039static const struct attribute_group *rbd_attr_groups[] = {
2040 &rbd_attr_group,
2041 NULL
2042};
2043
2044static void rbd_sysfs_dev_release(struct device *dev)
2045{
2046}
2047
2048static struct device_type rbd_device_type = {
2049 .name = "rbd",
2050 .groups = rbd_attr_groups,
2051 .release = rbd_sysfs_dev_release,
2052};
2053
2054
2055/*
2056 sysfs - snapshots
2057*/
2058
2059static ssize_t rbd_snap_size_show(struct device *dev,
2060 struct device_attribute *attr,
2061 char *buf)
2062{
2063 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2064
Josh Durgin35915382011-12-05 18:25:13 -08002065 return sprintf(buf, "%llu\n", (unsigned long long)snap->size);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002066}
2067
2068static ssize_t rbd_snap_id_show(struct device *dev,
2069 struct device_attribute *attr,
2070 char *buf)
2071{
2072 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2073
Josh Durgin35915382011-12-05 18:25:13 -08002074 return sprintf(buf, "%llu\n", (unsigned long long)snap->id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002075}
2076
2077static DEVICE_ATTR(snap_size, S_IRUGO, rbd_snap_size_show, NULL);
2078static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL);
2079
2080static struct attribute *rbd_snap_attrs[] = {
2081 &dev_attr_snap_size.attr,
2082 &dev_attr_snap_id.attr,
2083 NULL,
2084};
2085
2086static struct attribute_group rbd_snap_attr_group = {
2087 .attrs = rbd_snap_attrs,
2088};
2089
2090static void rbd_snap_dev_release(struct device *dev)
2091{
2092 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2093 kfree(snap->name);
2094 kfree(snap);
2095}
2096
2097static const struct attribute_group *rbd_snap_attr_groups[] = {
2098 &rbd_snap_attr_group,
2099 NULL
2100};
2101
2102static struct device_type rbd_snap_device_type = {
2103 .groups = rbd_snap_attr_groups,
2104 .release = rbd_snap_dev_release,
2105};
2106
Alex Elder14e70852012-07-19 09:09:27 -05002107static void __rbd_remove_snap_dev(struct rbd_snap *snap)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002108{
2109 list_del(&snap->node);
2110 device_unregister(&snap->dev);
2111}
2112
Alex Elder14e70852012-07-19 09:09:27 -05002113static int rbd_register_snap_dev(struct rbd_snap *snap,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002114 struct device *parent)
2115{
2116 struct device *dev = &snap->dev;
2117 int ret;
2118
2119 dev->type = &rbd_snap_device_type;
2120 dev->parent = parent;
2121 dev->release = rbd_snap_dev_release;
2122 dev_set_name(dev, "snap_%s", snap->name);
2123 ret = device_register(dev);
2124
2125 return ret;
2126}
2127
Alex Elder4e891e02012-07-10 20:30:10 -05002128static struct rbd_snap *__rbd_add_snap_dev(struct rbd_device *rbd_dev,
2129 int i, const char *name)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002130{
Alex Elder4e891e02012-07-10 20:30:10 -05002131 struct rbd_snap *snap;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002132 int ret;
Alex Elder4e891e02012-07-10 20:30:10 -05002133
2134 snap = kzalloc(sizeof (*snap), GFP_KERNEL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002135 if (!snap)
Alex Elder4e891e02012-07-10 20:30:10 -05002136 return ERR_PTR(-ENOMEM);
2137
2138 ret = -ENOMEM;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002139 snap->name = kstrdup(name, GFP_KERNEL);
Alex Elder4e891e02012-07-10 20:30:10 -05002140 if (!snap->name)
2141 goto err;
2142
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002143 snap->size = rbd_dev->header.snap_sizes[i];
2144 snap->id = rbd_dev->header.snapc->snaps[i];
2145 if (device_is_registered(&rbd_dev->dev)) {
Alex Elder14e70852012-07-19 09:09:27 -05002146 ret = rbd_register_snap_dev(snap, &rbd_dev->dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002147 if (ret < 0)
2148 goto err;
2149 }
Alex Elder4e891e02012-07-10 20:30:10 -05002150
2151 return snap;
2152
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002153err:
2154 kfree(snap->name);
2155 kfree(snap);
Alex Elder4e891e02012-07-10 20:30:10 -05002156
2157 return ERR_PTR(ret);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002158}
2159
2160/*
Alex Elder35938152012-08-02 11:29:46 -05002161 * Scan the rbd device's current snapshot list and compare it to the
2162 * newly-received snapshot context. Remove any existing snapshots
2163 * not present in the new snapshot context. Add a new snapshot for
2164 * any snaphots in the snapshot context not in the current list.
2165 * And verify there are no changes to snapshots we already know
2166 * about.
2167 *
2168 * Assumes the snapshots in the snapshot context are sorted by
2169 * snapshot id, highest id first. (Snapshots in the rbd_dev's list
2170 * are also maintained in that order.)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002171 */
2172static int __rbd_init_snaps_header(struct rbd_device *rbd_dev)
2173{
Alex Elder35938152012-08-02 11:29:46 -05002174 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
2175 const u32 snap_count = snapc->num_snaps;
2176 char *snap_name = rbd_dev->header.snap_names;
2177 struct list_head *head = &rbd_dev->snaps;
2178 struct list_head *links = head->next;
2179 u32 index = 0;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002180
Alex Elder35938152012-08-02 11:29:46 -05002181 while (index < snap_count || links != head) {
2182 u64 snap_id;
2183 struct rbd_snap *snap;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002184
Alex Elder35938152012-08-02 11:29:46 -05002185 snap_id = index < snap_count ? snapc->snaps[index]
2186 : CEPH_NOSNAP;
2187 snap = links != head ? list_entry(links, struct rbd_snap, node)
2188 : NULL;
2189 BUG_ON(snap && snap->id == CEPH_NOSNAP);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002190
Alex Elder35938152012-08-02 11:29:46 -05002191 if (snap_id == CEPH_NOSNAP || (snap && snap->id > snap_id)) {
2192 struct list_head *next = links->next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002193
Alex Elder35938152012-08-02 11:29:46 -05002194 /* Existing snapshot not in the new snap context */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002195
Alex Elder35938152012-08-02 11:29:46 -05002196 if (rbd_dev->snap_id == snap->id)
Josh Durgine88a36e2011-11-21 18:14:25 -08002197 rbd_dev->snap_exists = false;
Alex Elder35938152012-08-02 11:29:46 -05002198 __rbd_remove_snap_dev(snap);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002199
Alex Elder35938152012-08-02 11:29:46 -05002200 /* Done with this list entry; advance */
2201
2202 links = next;
2203 continue;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002204 }
Alex Elder35938152012-08-02 11:29:46 -05002205
2206 if (!snap || (snap_id != CEPH_NOSNAP && snap->id < snap_id)) {
2207 struct rbd_snap *new_snap;
2208
2209 /* We haven't seen this snapshot before */
2210
2211 new_snap = __rbd_add_snap_dev(rbd_dev, index,
2212 snap_name);
2213 if (IS_ERR(new_snap))
2214 return PTR_ERR(new_snap);
2215
2216 /* New goes before existing, or at end of list */
2217
2218 if (snap)
2219 list_add_tail(&new_snap->node, &snap->node);
2220 else
Alex Elder523f3252012-08-30 00:16:37 -05002221 list_add_tail(&new_snap->node, head);
Alex Elder35938152012-08-02 11:29:46 -05002222 } else {
2223 /* Already have this one */
2224
2225 BUG_ON(snap->size != rbd_dev->header.snap_sizes[index]);
2226 BUG_ON(strcmp(snap->name, snap_name));
2227
2228 /* Done with this list entry; advance */
2229
2230 links = links->next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002231 }
Alex Elder35938152012-08-02 11:29:46 -05002232
2233 /* Advance to the next entry in the snapshot context */
2234
2235 index++;
2236 snap_name += strlen(snap_name) + 1;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002237 }
2238
2239 return 0;
2240}
2241
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002242static int rbd_bus_add_dev(struct rbd_device *rbd_dev)
2243{
Alex Elderf0f8cef2012-01-29 13:57:44 -06002244 int ret;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002245 struct device *dev;
2246 struct rbd_snap *snap;
2247
2248 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2249 dev = &rbd_dev->dev;
2250
2251 dev->bus = &rbd_bus_type;
2252 dev->type = &rbd_device_type;
2253 dev->parent = &rbd_root_dev;
2254 dev->release = rbd_dev_release;
Alex Elderde71a292012-07-03 16:01:19 -05002255 dev_set_name(dev, "%d", rbd_dev->dev_id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002256 ret = device_register(dev);
2257 if (ret < 0)
Alex Elderf0f8cef2012-01-29 13:57:44 -06002258 goto out;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002259
2260 list_for_each_entry(snap, &rbd_dev->snaps, node) {
Alex Elder14e70852012-07-19 09:09:27 -05002261 ret = rbd_register_snap_dev(snap, &rbd_dev->dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002262 if (ret < 0)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002263 break;
2264 }
Alex Elderf0f8cef2012-01-29 13:57:44 -06002265out:
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002266 mutex_unlock(&ctl_mutex);
2267 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002268}
2269
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002270static void rbd_bus_del_dev(struct rbd_device *rbd_dev)
2271{
2272 device_unregister(&rbd_dev->dev);
2273}
2274
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002275static int rbd_init_watch_dev(struct rbd_device *rbd_dev)
2276{
2277 int ret, rc;
2278
2279 do {
Alex Elder0e6f3222012-07-25 09:32:40 -05002280 ret = rbd_req_sync_watch(rbd_dev);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002281 if (ret == -ERANGE) {
Alex Elder1fe5e992012-07-25 09:32:41 -05002282 rc = rbd_refresh_header(rbd_dev, NULL);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002283 if (rc < 0)
2284 return rc;
2285 }
2286 } while (ret == -ERANGE);
2287
2288 return ret;
2289}
2290
Alex Elder1ddbe942012-01-29 13:57:44 -06002291static atomic64_t rbd_id_max = ATOMIC64_INIT(0);
2292
2293/*
Alex Elder499afd52012-02-02 08:13:29 -06002294 * Get a unique rbd identifier for the given new rbd_dev, and add
2295 * the rbd_dev to the global list. The minimum rbd id is 1.
Alex Elder1ddbe942012-01-29 13:57:44 -06002296 */
Alex Elder499afd52012-02-02 08:13:29 -06002297static void rbd_id_get(struct rbd_device *rbd_dev)
Alex Elderb7f23c32012-01-29 13:57:43 -06002298{
Alex Elderde71a292012-07-03 16:01:19 -05002299 rbd_dev->dev_id = atomic64_inc_return(&rbd_id_max);
Alex Elder499afd52012-02-02 08:13:29 -06002300
2301 spin_lock(&rbd_dev_list_lock);
2302 list_add_tail(&rbd_dev->node, &rbd_dev_list);
2303 spin_unlock(&rbd_dev_list_lock);
Alex Elder1ddbe942012-01-29 13:57:44 -06002304}
Alex Elderb7f23c32012-01-29 13:57:43 -06002305
Alex Elder1ddbe942012-01-29 13:57:44 -06002306/*
Alex Elder499afd52012-02-02 08:13:29 -06002307 * Remove an rbd_dev from the global list, and record that its
2308 * identifier is no longer in use.
Alex Elder1ddbe942012-01-29 13:57:44 -06002309 */
Alex Elder499afd52012-02-02 08:13:29 -06002310static void rbd_id_put(struct rbd_device *rbd_dev)
Alex Elder1ddbe942012-01-29 13:57:44 -06002311{
Alex Elderd184f6b2012-01-29 13:57:44 -06002312 struct list_head *tmp;
Alex Elderde71a292012-07-03 16:01:19 -05002313 int rbd_id = rbd_dev->dev_id;
Alex Elderd184f6b2012-01-29 13:57:44 -06002314 int max_id;
2315
2316 BUG_ON(rbd_id < 1);
Alex Elder499afd52012-02-02 08:13:29 -06002317
2318 spin_lock(&rbd_dev_list_lock);
2319 list_del_init(&rbd_dev->node);
Alex Elderd184f6b2012-01-29 13:57:44 -06002320
2321 /*
2322 * If the id being "put" is not the current maximum, there
2323 * is nothing special we need to do.
2324 */
2325 if (rbd_id != atomic64_read(&rbd_id_max)) {
2326 spin_unlock(&rbd_dev_list_lock);
2327 return;
2328 }
2329
2330 /*
2331 * We need to update the current maximum id. Search the
2332 * list to find out what it is. We're more likely to find
2333 * the maximum at the end, so search the list backward.
2334 */
2335 max_id = 0;
2336 list_for_each_prev(tmp, &rbd_dev_list) {
2337 struct rbd_device *rbd_dev;
2338
2339 rbd_dev = list_entry(tmp, struct rbd_device, node);
2340 if (rbd_id > max_id)
2341 max_id = rbd_id;
2342 }
Alex Elder499afd52012-02-02 08:13:29 -06002343 spin_unlock(&rbd_dev_list_lock);
Alex Elderb7f23c32012-01-29 13:57:43 -06002344
Alex Elder1ddbe942012-01-29 13:57:44 -06002345 /*
Alex Elderd184f6b2012-01-29 13:57:44 -06002346 * The max id could have been updated by rbd_id_get(), in
2347 * which case it now accurately reflects the new maximum.
2348 * Be careful not to overwrite the maximum value in that
2349 * case.
Alex Elder1ddbe942012-01-29 13:57:44 -06002350 */
Alex Elderd184f6b2012-01-29 13:57:44 -06002351 atomic64_cmpxchg(&rbd_id_max, rbd_id, max_id);
Alex Elderb7f23c32012-01-29 13:57:43 -06002352}
2353
Alex Eldera725f65e2012-02-02 08:13:30 -06002354/*
Alex Eldere28fff262012-02-02 08:13:30 -06002355 * Skips over white space at *buf, and updates *buf to point to the
2356 * first found non-space character (if any). Returns the length of
Alex Elder593a9e72012-02-07 12:03:37 -06002357 * the token (string of non-white space characters) found. Note
2358 * that *buf must be terminated with '\0'.
Alex Eldere28fff262012-02-02 08:13:30 -06002359 */
2360static inline size_t next_token(const char **buf)
2361{
2362 /*
2363 * These are the characters that produce nonzero for
2364 * isspace() in the "C" and "POSIX" locales.
2365 */
2366 const char *spaces = " \f\n\r\t\v";
2367
2368 *buf += strspn(*buf, spaces); /* Find start of token */
2369
2370 return strcspn(*buf, spaces); /* Return token length */
2371}
2372
2373/*
2374 * Finds the next token in *buf, and if the provided token buffer is
2375 * big enough, copies the found token into it. The result, if
Alex Elder593a9e72012-02-07 12:03:37 -06002376 * copied, is guaranteed to be terminated with '\0'. Note that *buf
2377 * must be terminated with '\0' on entry.
Alex Eldere28fff262012-02-02 08:13:30 -06002378 *
2379 * Returns the length of the token found (not including the '\0').
2380 * Return value will be 0 if no token is found, and it will be >=
2381 * token_size if the token would not fit.
2382 *
Alex Elder593a9e72012-02-07 12:03:37 -06002383 * The *buf pointer will be updated to point beyond the end of the
Alex Eldere28fff262012-02-02 08:13:30 -06002384 * found token. Note that this occurs even if the token buffer is
2385 * too small to hold it.
2386 */
2387static inline size_t copy_token(const char **buf,
2388 char *token,
2389 size_t token_size)
2390{
2391 size_t len;
2392
2393 len = next_token(buf);
2394 if (len < token_size) {
2395 memcpy(token, *buf, len);
2396 *(token + len) = '\0';
2397 }
2398 *buf += len;
2399
2400 return len;
2401}
2402
2403/*
Alex Elderea3352f2012-07-09 21:04:23 -05002404 * Finds the next token in *buf, dynamically allocates a buffer big
2405 * enough to hold a copy of it, and copies the token into the new
2406 * buffer. The copy is guaranteed to be terminated with '\0'. Note
2407 * that a duplicate buffer is created even for a zero-length token.
2408 *
2409 * Returns a pointer to the newly-allocated duplicate, or a null
2410 * pointer if memory for the duplicate was not available. If
2411 * the lenp argument is a non-null pointer, the length of the token
2412 * (not including the '\0') is returned in *lenp.
2413 *
2414 * If successful, the *buf pointer will be updated to point beyond
2415 * the end of the found token.
2416 *
2417 * Note: uses GFP_KERNEL for allocation.
2418 */
2419static inline char *dup_token(const char **buf, size_t *lenp)
2420{
2421 char *dup;
2422 size_t len;
2423
2424 len = next_token(buf);
2425 dup = kmalloc(len + 1, GFP_KERNEL);
2426 if (!dup)
2427 return NULL;
2428
2429 memcpy(dup, *buf, len);
2430 *(dup + len) = '\0';
2431 *buf += len;
2432
2433 if (lenp)
2434 *lenp = len;
2435
2436 return dup;
2437}
2438
2439/*
Alex Elder0bed54d2012-07-03 16:01:18 -05002440 * This fills in the pool_name, image_name, image_name_len, snap_name,
Alex Eldera725f65e2012-02-02 08:13:30 -06002441 * rbd_dev, rbd_md_name, and name fields of the given rbd_dev, based
2442 * on the list of monitor addresses and other options provided via
2443 * /sys/bus/rbd/add.
Alex Elderd22f76e2012-07-12 10:46:35 -05002444 *
2445 * Note: rbd_dev is assumed to have been initially zero-filled.
Alex Eldera725f65e2012-02-02 08:13:30 -06002446 */
2447static int rbd_add_parse_args(struct rbd_device *rbd_dev,
2448 const char *buf,
Alex Elder7ef32142012-02-02 08:13:30 -06002449 const char **mon_addrs,
Alex Elder5214ecc2012-02-02 08:13:30 -06002450 size_t *mon_addrs_size,
Alex Eldere28fff262012-02-02 08:13:30 -06002451 char *options,
Alex Elder0bed54d2012-07-03 16:01:18 -05002452 size_t options_size)
Alex Eldera725f65e2012-02-02 08:13:30 -06002453{
Alex Elderd22f76e2012-07-12 10:46:35 -05002454 size_t len;
2455 int ret;
Alex Eldere28fff262012-02-02 08:13:30 -06002456
2457 /* The first four tokens are required */
2458
Alex Elder7ef32142012-02-02 08:13:30 -06002459 len = next_token(&buf);
2460 if (!len)
Alex Eldera725f65e2012-02-02 08:13:30 -06002461 return -EINVAL;
Alex Elder5214ecc2012-02-02 08:13:30 -06002462 *mon_addrs_size = len + 1;
Alex Elder7ef32142012-02-02 08:13:30 -06002463 *mon_addrs = buf;
2464
2465 buf += len;
Alex Eldera725f65e2012-02-02 08:13:30 -06002466
Alex Eldere28fff262012-02-02 08:13:30 -06002467 len = copy_token(&buf, options, options_size);
2468 if (!len || len >= options_size)
2469 return -EINVAL;
Alex Eldera725f65e2012-02-02 08:13:30 -06002470
Alex Elderbf3e5ae2012-07-09 21:04:23 -05002471 ret = -ENOMEM;
Alex Elderd22f76e2012-07-12 10:46:35 -05002472 rbd_dev->pool_name = dup_token(&buf, NULL);
2473 if (!rbd_dev->pool_name)
Alex Elderd22f76e2012-07-12 10:46:35 -05002474 goto out_err;
Alex Eldere28fff262012-02-02 08:13:30 -06002475
Alex Elder0bed54d2012-07-03 16:01:18 -05002476 rbd_dev->image_name = dup_token(&buf, &rbd_dev->image_name_len);
2477 if (!rbd_dev->image_name)
Alex Elderbf3e5ae2012-07-09 21:04:23 -05002478 goto out_err;
Alex Eldere28fff262012-02-02 08:13:30 -06002479
Alex Eldercb8627c2012-07-09 21:04:23 -05002480 /* Create the name of the header object */
2481
Alex Elder0bed54d2012-07-03 16:01:18 -05002482 rbd_dev->header_name = kmalloc(rbd_dev->image_name_len
Alex Elderbf3e5ae2012-07-09 21:04:23 -05002483 + sizeof (RBD_SUFFIX),
2484 GFP_KERNEL);
Alex Elder0bed54d2012-07-03 16:01:18 -05002485 if (!rbd_dev->header_name)
Alex Eldercb8627c2012-07-09 21:04:23 -05002486 goto out_err;
Alex Elder0bed54d2012-07-03 16:01:18 -05002487 sprintf(rbd_dev->header_name, "%s%s", rbd_dev->image_name, RBD_SUFFIX);
Alex Eldera725f65e2012-02-02 08:13:30 -06002488
Alex Eldere28fff262012-02-02 08:13:30 -06002489 /*
Alex Elder820a5f32012-07-09 21:04:24 -05002490 * The snapshot name is optional. If none is is supplied,
2491 * we use the default value.
Alex Eldere28fff262012-02-02 08:13:30 -06002492 */
Alex Elder820a5f32012-07-09 21:04:24 -05002493 rbd_dev->snap_name = dup_token(&buf, &len);
2494 if (!rbd_dev->snap_name)
2495 goto out_err;
2496 if (!len) {
2497 /* Replace the empty name with the default */
2498 kfree(rbd_dev->snap_name);
2499 rbd_dev->snap_name
2500 = kmalloc(sizeof (RBD_SNAP_HEAD_NAME), GFP_KERNEL);
2501 if (!rbd_dev->snap_name)
2502 goto out_err;
2503
Alex Eldere28fff262012-02-02 08:13:30 -06002504 memcpy(rbd_dev->snap_name, RBD_SNAP_HEAD_NAME,
2505 sizeof (RBD_SNAP_HEAD_NAME));
Alex Elder849b4262012-07-09 21:04:24 -05002506 }
Alex Eldere28fff262012-02-02 08:13:30 -06002507
Alex Eldera725f65e2012-02-02 08:13:30 -06002508 return 0;
Alex Elderd22f76e2012-07-12 10:46:35 -05002509
2510out_err:
Alex Elder0bed54d2012-07-03 16:01:18 -05002511 kfree(rbd_dev->header_name);
Alex Elderd78fd7a2012-07-26 23:37:14 -05002512 rbd_dev->header_name = NULL;
Alex Elder0bed54d2012-07-03 16:01:18 -05002513 kfree(rbd_dev->image_name);
Alex Elderd78fd7a2012-07-26 23:37:14 -05002514 rbd_dev->image_name = NULL;
2515 rbd_dev->image_name_len = 0;
Alex Elderd22f76e2012-07-12 10:46:35 -05002516 kfree(rbd_dev->pool_name);
2517 rbd_dev->pool_name = NULL;
2518
2519 return ret;
Alex Eldera725f65e2012-02-02 08:13:30 -06002520}
2521
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002522static ssize_t rbd_add(struct bus_type *bus,
2523 const char *buf,
2524 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002525{
Alex Eldercb8627c2012-07-09 21:04:23 -05002526 char *options;
2527 struct rbd_device *rbd_dev = NULL;
Alex Elder7ef32142012-02-02 08:13:30 -06002528 const char *mon_addrs = NULL;
2529 size_t mon_addrs_size = 0;
Alex Elder27cc2592012-02-02 08:13:30 -06002530 struct ceph_osd_client *osdc;
2531 int rc = -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002532
2533 if (!try_module_get(THIS_MODULE))
2534 return -ENODEV;
2535
Alex Elder27cc2592012-02-02 08:13:30 -06002536 options = kmalloc(count, GFP_KERNEL);
2537 if (!options)
2538 goto err_nomem;
Alex Eldercb8627c2012-07-09 21:04:23 -05002539 rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL);
2540 if (!rbd_dev)
2541 goto err_nomem;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002542
2543 /* static rbd_device initialization */
2544 spin_lock_init(&rbd_dev->lock);
2545 INIT_LIST_HEAD(&rbd_dev->node);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002546 INIT_LIST_HEAD(&rbd_dev->snaps);
Josh Durginc6666012011-11-21 17:11:12 -08002547 init_rwsem(&rbd_dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002548
Alex Elderd184f6b2012-01-29 13:57:44 -06002549 /* generate unique id: find highest unique id, add one */
Alex Elder499afd52012-02-02 08:13:29 -06002550 rbd_id_get(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002551
Alex Eldera725f65e2012-02-02 08:13:30 -06002552 /* Fill in the device name, now that we have its id. */
Alex Elder81a89792012-02-02 08:13:30 -06002553 BUILD_BUG_ON(DEV_NAME_LEN
2554 < sizeof (RBD_DRV_NAME) + MAX_INT_FORMAT_WIDTH);
Alex Elderde71a292012-07-03 16:01:19 -05002555 sprintf(rbd_dev->name, "%s%d", RBD_DRV_NAME, rbd_dev->dev_id);
Alex Eldere124a82f2012-01-29 13:57:44 -06002556
Alex Eldera725f65e2012-02-02 08:13:30 -06002557 /* parse add command */
Alex Elder7ef32142012-02-02 08:13:30 -06002558 rc = rbd_add_parse_args(rbd_dev, buf, &mon_addrs, &mon_addrs_size,
Alex Eldere28fff262012-02-02 08:13:30 -06002559 options, count);
Alex Eldera725f65e2012-02-02 08:13:30 -06002560 if (rc)
2561 goto err_put_id;
2562
Alex Elderf8c38922012-08-10 13:12:07 -07002563 rc = rbd_get_client(rbd_dev, mon_addrs, mon_addrs_size - 1, options);
2564 if (rc < 0)
Alex Elderf0f8cef2012-01-29 13:57:44 -06002565 goto err_put_id;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002566
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002567 /* pick the pool */
Alex Elder1dbb4392012-01-24 10:08:37 -06002568 osdc = &rbd_dev->rbd_client->client->osdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002569 rc = ceph_pg_poolid_by_name(osdc->osdmap, rbd_dev->pool_name);
2570 if (rc < 0)
2571 goto err_out_client;
Alex Elder9bb2f332012-07-12 10:46:35 -05002572 rbd_dev->pool_id = rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002573
2574 /* register our block device */
Alex Elder27cc2592012-02-02 08:13:30 -06002575 rc = register_blkdev(0, rbd_dev->name);
2576 if (rc < 0)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002577 goto err_out_client;
Alex Elder27cc2592012-02-02 08:13:30 -06002578 rbd_dev->major = rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002579
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002580 rc = rbd_bus_add_dev(rbd_dev);
2581 if (rc)
Yehuda Sadeh766fc432011-01-07 14:58:42 -08002582 goto err_out_blkdev;
2583
Alex Elder32eec682012-02-08 16:11:14 -06002584 /*
2585 * At this point cleanup in the event of an error is the job
2586 * of the sysfs code (initiated by rbd_bus_del_dev()).
2587 *
2588 * Set up and announce blkdev mapping.
2589 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002590 rc = rbd_init_disk(rbd_dev);
2591 if (rc)
Yehuda Sadeh766fc432011-01-07 14:58:42 -08002592 goto err_out_bus;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002593
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002594 rc = rbd_init_watch_dev(rbd_dev);
2595 if (rc)
2596 goto err_out_bus;
2597
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002598 return count;
2599
Yehuda Sadeh766fc432011-01-07 14:58:42 -08002600err_out_bus:
Yehuda Sadeh766fc432011-01-07 14:58:42 -08002601 /* this will also clean up rest of rbd_dev stuff */
2602
2603 rbd_bus_del_dev(rbd_dev);
2604 kfree(options);
Yehuda Sadeh766fc432011-01-07 14:58:42 -08002605 return rc;
2606
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002607err_out_blkdev:
2608 unregister_blkdev(rbd_dev->major, rbd_dev->name);
2609err_out_client:
2610 rbd_put_client(rbd_dev);
Alex Elderf0f8cef2012-01-29 13:57:44 -06002611err_put_id:
Alex Eldercb8627c2012-07-09 21:04:23 -05002612 if (rbd_dev->pool_name) {
Alex Elder820a5f32012-07-09 21:04:24 -05002613 kfree(rbd_dev->snap_name);
Alex Elder0bed54d2012-07-03 16:01:18 -05002614 kfree(rbd_dev->header_name);
2615 kfree(rbd_dev->image_name);
Alex Eldercb8627c2012-07-09 21:04:23 -05002616 kfree(rbd_dev->pool_name);
2617 }
Alex Elder499afd52012-02-02 08:13:29 -06002618 rbd_id_put(rbd_dev);
Alex Elder27cc2592012-02-02 08:13:30 -06002619err_nomem:
Alex Elder27cc2592012-02-02 08:13:30 -06002620 kfree(rbd_dev);
Alex Eldercb8627c2012-07-09 21:04:23 -05002621 kfree(options);
Alex Elder27cc2592012-02-02 08:13:30 -06002622
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002623 dout("Error adding device %s\n", buf);
2624 module_put(THIS_MODULE);
Alex Elder27cc2592012-02-02 08:13:30 -06002625
2626 return (ssize_t) rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002627}
2628
Alex Elderde71a292012-07-03 16:01:19 -05002629static struct rbd_device *__rbd_get_dev(unsigned long dev_id)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002630{
2631 struct list_head *tmp;
2632 struct rbd_device *rbd_dev;
2633
Alex Eldere124a82f2012-01-29 13:57:44 -06002634 spin_lock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002635 list_for_each(tmp, &rbd_dev_list) {
2636 rbd_dev = list_entry(tmp, struct rbd_device, node);
Alex Elderde71a292012-07-03 16:01:19 -05002637 if (rbd_dev->dev_id == dev_id) {
Alex Eldere124a82f2012-01-29 13:57:44 -06002638 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002639 return rbd_dev;
Alex Eldere124a82f2012-01-29 13:57:44 -06002640 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002641 }
Alex Eldere124a82f2012-01-29 13:57:44 -06002642 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002643 return NULL;
2644}
2645
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002646static void rbd_dev_release(struct device *dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002647{
Alex Elder593a9e72012-02-07 12:03:37 -06002648 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002649
Alex Elder1dbb4392012-01-24 10:08:37 -06002650 if (rbd_dev->watch_request) {
2651 struct ceph_client *client = rbd_dev->rbd_client->client;
2652
2653 ceph_osdc_unregister_linger_request(&client->osdc,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002654 rbd_dev->watch_request);
Alex Elder1dbb4392012-01-24 10:08:37 -06002655 }
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002656 if (rbd_dev->watch_event)
Alex Elder070c6332012-07-25 09:32:41 -05002657 rbd_req_sync_unwatch(rbd_dev);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002658
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002659 rbd_put_client(rbd_dev);
2660
2661 /* clean up and free blkdev */
2662 rbd_free_disk(rbd_dev);
2663 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Alex Elder32eec682012-02-08 16:11:14 -06002664
2665 /* done with the id, and with the rbd_dev */
Alex Elder820a5f32012-07-09 21:04:24 -05002666 kfree(rbd_dev->snap_name);
Alex Elder0bed54d2012-07-03 16:01:18 -05002667 kfree(rbd_dev->header_name);
Alex Elderd22f76e2012-07-12 10:46:35 -05002668 kfree(rbd_dev->pool_name);
Alex Elder0bed54d2012-07-03 16:01:18 -05002669 kfree(rbd_dev->image_name);
Alex Elder32eec682012-02-08 16:11:14 -06002670 rbd_id_put(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002671 kfree(rbd_dev);
2672
2673 /* release module ref */
2674 module_put(THIS_MODULE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002675}
2676
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002677static ssize_t rbd_remove(struct bus_type *bus,
2678 const char *buf,
2679 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002680{
2681 struct rbd_device *rbd_dev = NULL;
2682 int target_id, rc;
2683 unsigned long ul;
2684 int ret = count;
2685
2686 rc = strict_strtoul(buf, 10, &ul);
2687 if (rc)
2688 return rc;
2689
2690 /* convert to int; abort if we lost anything in the conversion */
2691 target_id = (int) ul;
2692 if (target_id != ul)
2693 return -EINVAL;
2694
2695 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2696
2697 rbd_dev = __rbd_get_dev(target_id);
2698 if (!rbd_dev) {
2699 ret = -ENOENT;
2700 goto done;
2701 }
2702
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002703 __rbd_remove_all_snaps(rbd_dev);
2704 rbd_bus_del_dev(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002705
2706done:
2707 mutex_unlock(&ctl_mutex);
2708 return ret;
2709}
2710
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002711static ssize_t rbd_snap_add(struct device *dev,
2712 struct device_attribute *attr,
2713 const char *buf,
2714 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002715{
Alex Elder593a9e72012-02-07 12:03:37 -06002716 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002717 int ret;
2718 char *name = kmalloc(count + 1, GFP_KERNEL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002719 if (!name)
2720 return -ENOMEM;
2721
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002722 snprintf(name, count, "%s", buf);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002723
2724 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2725
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002726 ret = rbd_header_add_snap(rbd_dev,
2727 name, GFP_KERNEL);
2728 if (ret < 0)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002729 goto err_unlock;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002730
Alex Elderb8136232012-07-25 09:32:41 -05002731 ret = __rbd_refresh_header(rbd_dev, NULL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002732 if (ret < 0)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002733 goto err_unlock;
2734
2735 /* shouldn't hold ctl_mutex when notifying.. notify might
2736 trigger a watch callback that would need to get that mutex */
2737 mutex_unlock(&ctl_mutex);
2738
2739 /* make a best effort, don't error if failed */
Alex Elder4cb16252012-07-25 09:32:40 -05002740 rbd_req_sync_notify(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002741
2742 ret = count;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002743 kfree(name);
2744 return ret;
2745
2746err_unlock:
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002747 mutex_unlock(&ctl_mutex);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002748 kfree(name);
2749 return ret;
2750}
2751
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002752/*
2753 * create control files in sysfs
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002754 * /sys/bus/rbd/...
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002755 */
2756static int rbd_sysfs_init(void)
2757{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002758 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002759
Alex Elderfed4c142012-02-07 12:03:36 -06002760 ret = device_register(&rbd_root_dev);
Alex Elder21079782012-01-24 10:08:36 -06002761 if (ret < 0)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002762 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002763
Alex Elderfed4c142012-02-07 12:03:36 -06002764 ret = bus_register(&rbd_bus_type);
2765 if (ret < 0)
2766 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002767
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002768 return ret;
2769}
2770
2771static void rbd_sysfs_cleanup(void)
2772{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002773 bus_unregister(&rbd_bus_type);
Alex Elderfed4c142012-02-07 12:03:36 -06002774 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002775}
2776
2777int __init rbd_init(void)
2778{
2779 int rc;
2780
2781 rc = rbd_sysfs_init();
2782 if (rc)
2783 return rc;
Alex Elderf0f8cef2012-01-29 13:57:44 -06002784 pr_info("loaded " RBD_DRV_NAME_LONG "\n");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002785 return 0;
2786}
2787
2788void __exit rbd_exit(void)
2789{
2790 rbd_sysfs_cleanup();
2791}
2792
2793module_init(rbd_init);
2794module_exit(rbd_exit);
2795
2796MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
2797MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
2798MODULE_DESCRIPTION("rados block device");
2799
2800/* following authorship retained from original osdblk.c */
2801MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
2802
2803MODULE_LICENSE("GPL");