blob: 7f60ff28ac29c61fd4b804eccc5da3dfc440431e [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
44#define DRV_NAME "rbd"
45#define DRV_NAME_LONG "rbd (rados block device)"
46
47#define RBD_MINORS_PER_MAJOR 256 /* max minors per blkdev */
48
Alex Elder21079782012-01-24 10:08:36 -060049#define RBD_MAX_MD_NAME_LEN (RBD_MAX_OBJ_NAME_LEN + sizeof(RBD_SUFFIX))
Yehuda Sadeh602adf42010-08-12 16:11:25 -070050#define RBD_MAX_POOL_NAME_LEN 64
51#define RBD_MAX_SNAP_NAME_LEN 32
52#define RBD_MAX_OPT_LEN 1024
53
54#define RBD_SNAP_HEAD_NAME "-"
55
56#define DEV_NAME_LEN 32
57
Yehuda Sadeh59c2be12011-03-21 15:10:11 -070058#define RBD_NOTIFY_TIMEOUT_DEFAULT 10
59
Yehuda Sadeh602adf42010-08-12 16:11:25 -070060/*
61 * block device image metadata (in-memory version)
62 */
63struct rbd_image_header {
64 u64 image_size;
65 char block_name[32];
66 __u8 obj_order;
67 __u8 crypt_type;
68 __u8 comp_type;
69 struct rw_semaphore snap_rwsem;
70 struct ceph_snap_context *snapc;
71 size_t snap_names_len;
72 u64 snap_seq;
73 u32 total_snaps;
74
75 char *snap_names;
76 u64 *snap_sizes;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -070077
78 u64 obj_version;
79};
80
81struct rbd_options {
82 int notify_timeout;
Yehuda Sadeh602adf42010-08-12 16:11:25 -070083};
84
85/*
86 * an instance of the client. multiple devices may share a client.
87 */
88struct rbd_client {
89 struct ceph_client *client;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -070090 struct rbd_options *rbd_opts;
Yehuda Sadeh602adf42010-08-12 16:11:25 -070091 struct kref kref;
92 struct list_head node;
93};
94
Yehuda Sadeh1fec7092011-05-13 13:52:56 -070095struct rbd_req_coll;
96
Yehuda Sadeh602adf42010-08-12 16:11:25 -070097/*
98 * a single io request
99 */
100struct rbd_request {
101 struct request *rq; /* blk layer request */
102 struct bio *bio; /* cloned bio */
103 struct page **pages; /* list of used pages */
104 u64 len;
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700105 int coll_index;
106 struct rbd_req_coll *coll;
107};
108
109struct rbd_req_status {
110 int done;
111 int rc;
112 u64 bytes;
113};
114
115/*
116 * a collection of requests
117 */
118struct rbd_req_coll {
119 int total;
120 int num_done;
121 struct kref kref;
122 struct rbd_req_status status[0];
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700123};
124
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800125struct rbd_snap {
126 struct device dev;
127 const char *name;
128 size_t size;
129 struct list_head node;
130 u64 id;
131};
132
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700133/*
134 * a single device
135 */
136struct rbd_device {
137 int id; /* blkdev unique id */
138
139 int major; /* blkdev assigned major */
140 struct gendisk *disk; /* blkdev's gendisk and rq */
141 struct request_queue *q;
142
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700143 struct rbd_client *rbd_client;
144
145 char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */
146
147 spinlock_t lock; /* queue lock */
148
149 struct rbd_image_header header;
150 char obj[RBD_MAX_OBJ_NAME_LEN]; /* rbd image name */
151 int obj_len;
152 char obj_md_name[RBD_MAX_MD_NAME_LEN]; /* hdr nm. */
153 char pool_name[RBD_MAX_POOL_NAME_LEN];
154 int poolid;
155
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700156 struct ceph_osd_event *watch_event;
157 struct ceph_osd_request *watch_request;
158
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700159 char snap_name[RBD_MAX_SNAP_NAME_LEN];
160 u32 cur_snap; /* index+1 of current snapshot within snap context
161 0 - for the head */
162 int read_only;
163
164 struct list_head node;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800165
166 /* list of snapshots */
167 struct list_head snaps;
168
169 /* sysfs related */
170 struct device dev;
171};
172
173static struct bus_type rbd_bus_type = {
174 .name = "rbd",
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700175};
176
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700177static DEFINE_MUTEX(ctl_mutex); /* Serialize open/close/setup/teardown */
Alex Eldere124a82f2012-01-29 13:57:44 -0600178
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700179static LIST_HEAD(rbd_dev_list); /* devices */
Alex Eldere124a82f2012-01-29 13:57:44 -0600180static DEFINE_SPINLOCK(rbd_dev_list_lock);
181
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700182static LIST_HEAD(rbd_client_list); /* clients */
Alex Eldere124a82f2012-01-29 13:57:44 -0600183static DEFINE_SPINLOCK(node_lock); /* protects client get/put */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700184
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800185static int __rbd_init_snaps_header(struct rbd_device *rbd_dev);
186static void rbd_dev_release(struct device *dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800187static ssize_t rbd_snap_add(struct device *dev,
188 struct device_attribute *attr,
189 const char *buf,
190 size_t count);
191static void __rbd_remove_snap_dev(struct rbd_device *rbd_dev,
Justin P. Mattock69932482011-07-26 23:06:29 -0700192 struct rbd_snap *snap);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800193
194
195static struct rbd_device *dev_to_rbd(struct device *dev)
196{
197 return container_of(dev, struct rbd_device, dev);
198}
199
200static struct device *rbd_get_dev(struct rbd_device *rbd_dev)
201{
202 return get_device(&rbd_dev->dev);
203}
204
205static void rbd_put_dev(struct rbd_device *rbd_dev)
206{
207 put_device(&rbd_dev->dev);
208}
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700209
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700210static int __rbd_update_snaps(struct rbd_device *rbd_dev);
211
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700212static int rbd_open(struct block_device *bdev, fmode_t mode)
213{
214 struct gendisk *disk = bdev->bd_disk;
215 struct rbd_device *rbd_dev = disk->private_data;
216
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800217 rbd_get_dev(rbd_dev);
218
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700219 set_device_ro(bdev, rbd_dev->read_only);
220
221 if ((mode & FMODE_WRITE) && rbd_dev->read_only)
222 return -EROFS;
223
224 return 0;
225}
226
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800227static int rbd_release(struct gendisk *disk, fmode_t mode)
228{
229 struct rbd_device *rbd_dev = disk->private_data;
230
231 rbd_put_dev(rbd_dev);
232
233 return 0;
234}
235
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700236static const struct block_device_operations rbd_bd_ops = {
237 .owner = THIS_MODULE,
238 .open = rbd_open,
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800239 .release = rbd_release,
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700240};
241
242/*
243 * Initialize an rbd client instance.
244 * We own *opt.
245 */
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700246static struct rbd_client *rbd_client_create(struct ceph_options *opt,
247 struct rbd_options *rbd_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700248{
249 struct rbd_client *rbdc;
250 int ret = -ENOMEM;
251
252 dout("rbd_client_create\n");
253 rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL);
254 if (!rbdc)
255 goto out_opt;
256
257 kref_init(&rbdc->kref);
258 INIT_LIST_HEAD(&rbdc->node);
259
Sage Weil6ab00d42011-08-09 09:41:59 -0700260 rbdc->client = ceph_create_client(opt, rbdc, 0, 0);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700261 if (IS_ERR(rbdc->client))
262 goto out_rbdc;
Vasiliy Kulikov28f259b2010-09-26 12:59:37 +0400263 opt = NULL; /* Now rbdc->client is responsible for opt */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700264
265 ret = ceph_open_session(rbdc->client);
266 if (ret < 0)
267 goto out_err;
268
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700269 rbdc->rbd_opts = rbd_opts;
270
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700271 spin_lock(&node_lock);
272 list_add_tail(&rbdc->node, &rbd_client_list);
273 spin_unlock(&node_lock);
274
275 dout("rbd_client_create created %p\n", rbdc);
276 return rbdc;
277
278out_err:
279 ceph_destroy_client(rbdc->client);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700280out_rbdc:
281 kfree(rbdc);
282out_opt:
Vasiliy Kulikov28f259b2010-09-26 12:59:37 +0400283 if (opt)
284 ceph_destroy_options(opt);
285 return ERR_PTR(ret);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700286}
287
288/*
289 * Find a ceph client with specific addr and configuration.
290 */
291static struct rbd_client *__rbd_client_find(struct ceph_options *opt)
292{
293 struct rbd_client *client_node;
294
295 if (opt->flags & CEPH_OPT_NOSHARE)
296 return NULL;
297
298 list_for_each_entry(client_node, &rbd_client_list, node)
299 if (ceph_compare_options(opt, client_node->client) == 0)
300 return client_node;
301 return NULL;
302}
303
304/*
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700305 * mount options
306 */
307enum {
308 Opt_notify_timeout,
309 Opt_last_int,
310 /* int args above */
311 Opt_last_string,
312 /* string args above */
313};
314
315static match_table_t rbdopt_tokens = {
316 {Opt_notify_timeout, "notify_timeout=%d"},
317 /* int args above */
318 /* string args above */
319 {-1, NULL}
320};
321
322static int parse_rbd_opts_token(char *c, void *private)
323{
324 struct rbd_options *rbdopt = private;
325 substring_t argstr[MAX_OPT_ARGS];
326 int token, intval, ret;
327
Alex Elder21079782012-01-24 10:08:36 -0600328 token = match_token(c, rbdopt_tokens, argstr);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700329 if (token < 0)
330 return -EINVAL;
331
332 if (token < Opt_last_int) {
333 ret = match_int(&argstr[0], &intval);
334 if (ret < 0) {
335 pr_err("bad mount option arg (not int) "
336 "at '%s'\n", c);
337 return ret;
338 }
339 dout("got int token %d val %d\n", token, intval);
340 } else if (token > Opt_last_int && token < Opt_last_string) {
341 dout("got string token %d val %s\n", token,
342 argstr[0].from);
343 } else {
344 dout("got token %d\n", token);
345 }
346
347 switch (token) {
348 case Opt_notify_timeout:
349 rbdopt->notify_timeout = intval;
350 break;
351 default:
352 BUG_ON(token);
353 }
354 return 0;
355}
356
357/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700358 * Get a ceph client with specific addr and configuration, if one does
359 * not exist create it.
360 */
361static int rbd_get_client(struct rbd_device *rbd_dev, const char *mon_addr,
362 char *options)
363{
364 struct rbd_client *rbdc;
365 struct ceph_options *opt;
366 int ret;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700367 struct rbd_options *rbd_opts;
368
369 rbd_opts = kzalloc(sizeof(*rbd_opts), GFP_KERNEL);
370 if (!rbd_opts)
371 return -ENOMEM;
372
373 rbd_opts->notify_timeout = RBD_NOTIFY_TIMEOUT_DEFAULT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700374
Alex Elderee577412012-01-24 10:08:36 -0600375 opt = ceph_parse_options(options, mon_addr,
Alex Elder21079782012-01-24 10:08:36 -0600376 mon_addr + strlen(mon_addr),
377 parse_rbd_opts_token, rbd_opts);
Alex Elderee577412012-01-24 10:08:36 -0600378 if (IS_ERR(opt)) {
379 ret = PTR_ERR(opt);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700380 goto done_err;
Alex Elderee577412012-01-24 10:08:36 -0600381 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700382
383 spin_lock(&node_lock);
384 rbdc = __rbd_client_find(opt);
385 if (rbdc) {
Alex Eldere6994d3d2012-01-29 13:57:44 -0600386 /* using an existing client */
387 kref_get(&rbdc->kref);
388 spin_unlock(&node_lock);
389
390 rbd_dev->rbd_client = rbdc;
391
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700392 ceph_destroy_options(opt);
Alex Elder97bb59a2012-01-24 10:08:36 -0600393 kfree(rbd_opts);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700394
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700395 return 0;
396 }
397 spin_unlock(&node_lock);
398
Alex Elderd97081b2012-01-29 13:57:44 -0600399 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700400 rbdc = rbd_client_create(opt, rbd_opts);
Alex Elderd97081b2012-01-29 13:57:44 -0600401 mutex_unlock(&ctl_mutex);
402
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700403 if (IS_ERR(rbdc)) {
404 ret = PTR_ERR(rbdc);
405 goto done_err;
406 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700407
408 rbd_dev->rbd_client = rbdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700409 return 0;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700410done_err:
411 kfree(rbd_opts);
412 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700413}
414
415/*
416 * Destroy ceph client
Alex Elderd23a4b32012-01-29 13:57:43 -0600417 *
418 * Caller must hold node_lock.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700419 */
420static void rbd_client_release(struct kref *kref)
421{
422 struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
423
424 dout("rbd_release_client %p\n", rbdc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700425 list_del(&rbdc->node);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700426
427 ceph_destroy_client(rbdc->client);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700428 kfree(rbdc->rbd_opts);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700429 kfree(rbdc);
430}
431
432/*
433 * Drop reference to ceph client node. If it's not referenced anymore, release
434 * it.
435 */
436static void rbd_put_client(struct rbd_device *rbd_dev)
437{
Alex Elderd23a4b32012-01-29 13:57:43 -0600438 spin_lock(&node_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700439 kref_put(&rbd_dev->rbd_client->kref, rbd_client_release);
Alex Elderd23a4b32012-01-29 13:57:43 -0600440 spin_unlock(&node_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700441 rbd_dev->rbd_client = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700442}
443
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700444/*
445 * Destroy requests collection
446 */
447static void rbd_coll_release(struct kref *kref)
448{
449 struct rbd_req_coll *coll =
450 container_of(kref, struct rbd_req_coll, kref);
451
452 dout("rbd_coll_release %p\n", coll);
453 kfree(coll);
454}
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700455
456/*
457 * Create a new header structure, translate header format from the on-disk
458 * header.
459 */
460static int rbd_header_from_disk(struct rbd_image_header *header,
461 struct rbd_image_header_ondisk *ondisk,
462 int allocated_snaps,
463 gfp_t gfp_flags)
464{
465 int i;
466 u32 snap_count = le32_to_cpu(ondisk->snap_count);
467 int ret = -ENOMEM;
468
Alex Elder21079782012-01-24 10:08:36 -0600469 if (memcmp(ondisk, RBD_HEADER_TEXT, sizeof(RBD_HEADER_TEXT)))
Josh Durgin81e759f2011-11-15 14:49:53 -0800470 return -ENXIO;
Josh Durgin81e759f2011-11-15 14:49:53 -0800471
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700472 init_rwsem(&header->snap_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700473 header->snap_names_len = le64_to_cpu(ondisk->snap_names_len);
474 header->snapc = kmalloc(sizeof(struct ceph_snap_context) +
Alex Elder21079782012-01-24 10:08:36 -0600475 snap_count * sizeof (*ondisk),
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700476 gfp_flags);
477 if (!header->snapc)
478 return -ENOMEM;
479 if (snap_count) {
480 header->snap_names = kmalloc(header->snap_names_len,
481 GFP_KERNEL);
482 if (!header->snap_names)
483 goto err_snapc;
484 header->snap_sizes = kmalloc(snap_count * sizeof(u64),
485 GFP_KERNEL);
486 if (!header->snap_sizes)
487 goto err_names;
488 } else {
489 header->snap_names = NULL;
490 header->snap_sizes = NULL;
491 }
492 memcpy(header->block_name, ondisk->block_name,
493 sizeof(ondisk->block_name));
494
495 header->image_size = le64_to_cpu(ondisk->image_size);
496 header->obj_order = ondisk->options.order;
497 header->crypt_type = ondisk->options.crypt_type;
498 header->comp_type = ondisk->options.comp_type;
499
500 atomic_set(&header->snapc->nref, 1);
501 header->snap_seq = le64_to_cpu(ondisk->snap_seq);
502 header->snapc->num_snaps = snap_count;
503 header->total_snaps = snap_count;
504
Alex Elder21079782012-01-24 10:08:36 -0600505 if (snap_count && allocated_snaps == snap_count) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700506 for (i = 0; i < snap_count; i++) {
507 header->snapc->snaps[i] =
508 le64_to_cpu(ondisk->snaps[i].id);
509 header->snap_sizes[i] =
510 le64_to_cpu(ondisk->snaps[i].image_size);
511 }
512
513 /* copy snapshot names */
514 memcpy(header->snap_names, &ondisk->snaps[i],
515 header->snap_names_len);
516 }
517
518 return 0;
519
520err_names:
521 kfree(header->snap_names);
522err_snapc:
523 kfree(header->snapc);
524 return ret;
525}
526
527static int snap_index(struct rbd_image_header *header, int snap_num)
528{
529 return header->total_snaps - snap_num;
530}
531
532static u64 cur_snap_id(struct rbd_device *rbd_dev)
533{
534 struct rbd_image_header *header = &rbd_dev->header;
535
536 if (!rbd_dev->cur_snap)
537 return 0;
538
539 return header->snapc->snaps[snap_index(header, rbd_dev->cur_snap)];
540}
541
542static int snap_by_name(struct rbd_image_header *header, const char *snap_name,
543 u64 *seq, u64 *size)
544{
545 int i;
546 char *p = header->snap_names;
547
548 for (i = 0; i < header->total_snaps; i++, p += strlen(p) + 1) {
549 if (strcmp(snap_name, p) == 0)
550 break;
551 }
552 if (i == header->total_snaps)
553 return -ENOENT;
554 if (seq)
555 *seq = header->snapc->snaps[i];
556
557 if (size)
558 *size = header->snap_sizes[i];
559
560 return i;
561}
562
Josh Durgincc9d7342011-11-21 18:19:13 -0800563static int rbd_header_set_snap(struct rbd_device *dev, u64 *size)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700564{
565 struct rbd_image_header *header = &dev->header;
566 struct ceph_snap_context *snapc = header->snapc;
567 int ret = -ENOENT;
568
Josh Durgincc9d7342011-11-21 18:19:13 -0800569 BUILD_BUG_ON(sizeof (dev->snap_name) < sizeof (RBD_SNAP_HEAD_NAME));
570
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700571 down_write(&header->snap_rwsem);
572
Josh Durgincc9d7342011-11-21 18:19:13 -0800573 if (!memcmp(dev->snap_name, RBD_SNAP_HEAD_NAME,
574 sizeof (RBD_SNAP_HEAD_NAME))) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700575 if (header->total_snaps)
576 snapc->seq = header->snap_seq;
577 else
578 snapc->seq = 0;
579 dev->cur_snap = 0;
580 dev->read_only = 0;
581 if (size)
582 *size = header->image_size;
583 } else {
Josh Durgincc9d7342011-11-21 18:19:13 -0800584 ret = snap_by_name(header, dev->snap_name, &snapc->seq, size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700585 if (ret < 0)
586 goto done;
587
588 dev->cur_snap = header->total_snaps - ret;
589 dev->read_only = 1;
590 }
591
592 ret = 0;
593done:
594 up_write(&header->snap_rwsem);
595 return ret;
596}
597
598static void rbd_header_free(struct rbd_image_header *header)
599{
600 kfree(header->snapc);
601 kfree(header->snap_names);
602 kfree(header->snap_sizes);
603}
604
605/*
606 * get the actual striped segment name, offset and length
607 */
608static u64 rbd_get_segment(struct rbd_image_header *header,
609 const char *block_name,
610 u64 ofs, u64 len,
611 char *seg_name, u64 *segofs)
612{
613 u64 seg = ofs >> header->obj_order;
614
615 if (seg_name)
616 snprintf(seg_name, RBD_MAX_SEG_NAME_LEN,
617 "%s.%012llx", block_name, seg);
618
619 ofs = ofs & ((1 << header->obj_order) - 1);
620 len = min_t(u64, len, (1 << header->obj_order) - ofs);
621
622 if (segofs)
623 *segofs = ofs;
624
625 return len;
626}
627
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700628static int rbd_get_num_segments(struct rbd_image_header *header,
629 u64 ofs, u64 len)
630{
631 u64 start_seg = ofs >> header->obj_order;
632 u64 end_seg = (ofs + len - 1) >> header->obj_order;
633 return end_seg - start_seg + 1;
634}
635
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700636/*
Josh Durgin029bcbd2011-07-22 11:35:23 -0700637 * returns the size of an object in the image
638 */
639static u64 rbd_obj_bytes(struct rbd_image_header *header)
640{
641 return 1 << header->obj_order;
642}
643
644/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700645 * bio helpers
646 */
647
648static void bio_chain_put(struct bio *chain)
649{
650 struct bio *tmp;
651
652 while (chain) {
653 tmp = chain;
654 chain = chain->bi_next;
655 bio_put(tmp);
656 }
657}
658
659/*
660 * zeros a bio chain, starting at specific offset
661 */
662static void zero_bio_chain(struct bio *chain, int start_ofs)
663{
664 struct bio_vec *bv;
665 unsigned long flags;
666 void *buf;
667 int i;
668 int pos = 0;
669
670 while (chain) {
671 bio_for_each_segment(bv, chain, i) {
672 if (pos + bv->bv_len > start_ofs) {
673 int remainder = max(start_ofs - pos, 0);
674 buf = bvec_kmap_irq(bv, &flags);
675 memset(buf + remainder, 0,
676 bv->bv_len - remainder);
Dan Carpenter85b5aaa2010-10-11 21:15:11 +0200677 bvec_kunmap_irq(buf, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700678 }
679 pos += bv->bv_len;
680 }
681
682 chain = chain->bi_next;
683 }
684}
685
686/*
687 * bio_chain_clone - clone a chain of bios up to a certain length.
688 * might return a bio_pair that will need to be released.
689 */
690static struct bio *bio_chain_clone(struct bio **old, struct bio **next,
691 struct bio_pair **bp,
692 int len, gfp_t gfpmask)
693{
694 struct bio *tmp, *old_chain = *old, *new_chain = NULL, *tail = NULL;
695 int total = 0;
696
697 if (*bp) {
698 bio_pair_release(*bp);
699 *bp = NULL;
700 }
701
702 while (old_chain && (total < len)) {
703 tmp = bio_kmalloc(gfpmask, old_chain->bi_max_vecs);
704 if (!tmp)
705 goto err_out;
706
707 if (total + old_chain->bi_size > len) {
708 struct bio_pair *bp;
709
710 /*
711 * this split can only happen with a single paged bio,
712 * split_bio will BUG_ON if this is not the case
713 */
714 dout("bio_chain_clone split! total=%d remaining=%d"
715 "bi_size=%d\n",
716 (int)total, (int)len-total,
717 (int)old_chain->bi_size);
718
719 /* split the bio. We'll release it either in the next
720 call, or it will have to be released outside */
721 bp = bio_split(old_chain, (len - total) / 512ULL);
722 if (!bp)
723 goto err_out;
724
725 __bio_clone(tmp, &bp->bio1);
726
727 *next = &bp->bio2;
728 } else {
729 __bio_clone(tmp, old_chain);
730 *next = old_chain->bi_next;
731 }
732
733 tmp->bi_bdev = NULL;
734 gfpmask &= ~__GFP_WAIT;
735 tmp->bi_next = NULL;
736
737 if (!new_chain) {
738 new_chain = tail = tmp;
739 } else {
740 tail->bi_next = tmp;
741 tail = tmp;
742 }
743 old_chain = old_chain->bi_next;
744
745 total += tmp->bi_size;
746 }
747
748 BUG_ON(total < len);
749
750 if (tail)
751 tail->bi_next = NULL;
752
753 *old = old_chain;
754
755 return new_chain;
756
757err_out:
758 dout("bio_chain_clone with err\n");
759 bio_chain_put(new_chain);
760 return NULL;
761}
762
763/*
764 * helpers for osd request op vectors.
765 */
766static int rbd_create_rw_ops(struct ceph_osd_req_op **ops,
767 int num_ops,
768 int opcode,
769 u32 payload_len)
770{
771 *ops = kzalloc(sizeof(struct ceph_osd_req_op) * (num_ops + 1),
772 GFP_NOIO);
773 if (!*ops)
774 return -ENOMEM;
775 (*ops)[0].op = opcode;
776 /*
777 * op extent offset and length will be set later on
778 * in calc_raw_layout()
779 */
780 (*ops)[0].payload_len = payload_len;
781 return 0;
782}
783
784static void rbd_destroy_ops(struct ceph_osd_req_op *ops)
785{
786 kfree(ops);
787}
788
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700789static void rbd_coll_end_req_index(struct request *rq,
790 struct rbd_req_coll *coll,
791 int index,
792 int ret, u64 len)
793{
794 struct request_queue *q;
795 int min, max, i;
796
797 dout("rbd_coll_end_req_index %p index %d ret %d len %lld\n",
798 coll, index, ret, len);
799
800 if (!rq)
801 return;
802
803 if (!coll) {
804 blk_end_request(rq, ret, len);
805 return;
806 }
807
808 q = rq->q;
809
810 spin_lock_irq(q->queue_lock);
811 coll->status[index].done = 1;
812 coll->status[index].rc = ret;
813 coll->status[index].bytes = len;
814 max = min = coll->num_done;
815 while (max < coll->total && coll->status[max].done)
816 max++;
817
818 for (i = min; i<max; i++) {
819 __blk_end_request(rq, coll->status[i].rc,
820 coll->status[i].bytes);
821 coll->num_done++;
822 kref_put(&coll->kref, rbd_coll_release);
823 }
824 spin_unlock_irq(q->queue_lock);
825}
826
827static void rbd_coll_end_req(struct rbd_request *req,
828 int ret, u64 len)
829{
830 rbd_coll_end_req_index(req->rq, req->coll, req->coll_index, ret, len);
831}
832
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700833/*
834 * Send ceph osd request
835 */
836static int rbd_do_request(struct request *rq,
837 struct rbd_device *dev,
838 struct ceph_snap_context *snapc,
839 u64 snapid,
840 const char *obj, u64 ofs, u64 len,
841 struct bio *bio,
842 struct page **pages,
843 int num_pages,
844 int flags,
845 struct ceph_osd_req_op *ops,
846 int num_reply,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700847 struct rbd_req_coll *coll,
848 int coll_index,
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700849 void (*rbd_cb)(struct ceph_osd_request *req,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700850 struct ceph_msg *msg),
851 struct ceph_osd_request **linger_req,
852 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700853{
854 struct ceph_osd_request *req;
855 struct ceph_file_layout *layout;
856 int ret;
857 u64 bno;
858 struct timespec mtime = CURRENT_TIME;
859 struct rbd_request *req_data;
860 struct ceph_osd_request_head *reqhead;
861 struct rbd_image_header *header = &dev->header;
Alex Elder1dbb4392012-01-24 10:08:37 -0600862 struct ceph_osd_client *osdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700863
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700864 req_data = kzalloc(sizeof(*req_data), GFP_NOIO);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700865 if (!req_data) {
866 if (coll)
867 rbd_coll_end_req_index(rq, coll, coll_index,
868 -ENOMEM, len);
869 return -ENOMEM;
870 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700871
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700872 if (coll) {
873 req_data->coll = coll;
874 req_data->coll_index = coll_index;
875 }
876
877 dout("rbd_do_request obj=%s ofs=%lld len=%lld\n", obj, len, ofs);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700878
879 down_read(&header->snap_rwsem);
880
Alex Elder1dbb4392012-01-24 10:08:37 -0600881 osdc = &dev->rbd_client->client->osdc;
882 req = ceph_osdc_alloc_request(osdc, flags, snapc, ops,
883 false, GFP_NOIO, pages, bio);
Sage Weil4ad12622011-05-03 09:23:36 -0700884 if (!req) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700885 up_read(&header->snap_rwsem);
Sage Weil4ad12622011-05-03 09:23:36 -0700886 ret = -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700887 goto done_pages;
888 }
889
890 req->r_callback = rbd_cb;
891
892 req_data->rq = rq;
893 req_data->bio = bio;
894 req_data->pages = pages;
895 req_data->len = len;
896
897 req->r_priv = req_data;
898
899 reqhead = req->r_request->front.iov_base;
900 reqhead->snapid = cpu_to_le64(CEPH_NOSNAP);
901
902 strncpy(req->r_oid, obj, sizeof(req->r_oid));
903 req->r_oid_len = strlen(req->r_oid);
904
905 layout = &req->r_file_layout;
906 memset(layout, 0, sizeof(*layout));
907 layout->fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
908 layout->fl_stripe_count = cpu_to_le32(1);
909 layout->fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
910 layout->fl_pg_preferred = cpu_to_le32(-1);
911 layout->fl_pg_pool = cpu_to_le32(dev->poolid);
Alex Elder1dbb4392012-01-24 10:08:37 -0600912 ceph_calc_raw_layout(osdc, layout, snapid, ofs, &len, &bno,
913 req, ops);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700914
915 ceph_osdc_build_request(req, ofs, &len,
916 ops,
917 snapc,
918 &mtime,
919 req->r_oid, req->r_oid_len);
920 up_read(&header->snap_rwsem);
921
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700922 if (linger_req) {
Alex Elder1dbb4392012-01-24 10:08:37 -0600923 ceph_osdc_set_request_linger(osdc, req);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700924 *linger_req = req;
925 }
926
Alex Elder1dbb4392012-01-24 10:08:37 -0600927 ret = ceph_osdc_start_request(osdc, req, false);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700928 if (ret < 0)
929 goto done_err;
930
931 if (!rbd_cb) {
Alex Elder1dbb4392012-01-24 10:08:37 -0600932 ret = ceph_osdc_wait_request(osdc, req);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700933 if (ver)
934 *ver = le64_to_cpu(req->r_reassert_version.version);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700935 dout("reassert_ver=%lld\n",
936 le64_to_cpu(req->r_reassert_version.version));
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700937 ceph_osdc_put_request(req);
938 }
939 return ret;
940
941done_err:
942 bio_chain_put(req_data->bio);
943 ceph_osdc_put_request(req);
944done_pages:
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700945 rbd_coll_end_req(req_data, ret, len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700946 kfree(req_data);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700947 return ret;
948}
949
950/*
951 * Ceph osd op callback
952 */
953static void rbd_req_cb(struct ceph_osd_request *req, struct ceph_msg *msg)
954{
955 struct rbd_request *req_data = req->r_priv;
956 struct ceph_osd_reply_head *replyhead;
957 struct ceph_osd_op *op;
958 __s32 rc;
959 u64 bytes;
960 int read_op;
961
962 /* parse reply */
963 replyhead = msg->front.iov_base;
964 WARN_ON(le32_to_cpu(replyhead->num_ops) == 0);
965 op = (void *)(replyhead + 1);
966 rc = le32_to_cpu(replyhead->result);
967 bytes = le64_to_cpu(op->extent.length);
968 read_op = (le32_to_cpu(op->op) == CEPH_OSD_OP_READ);
969
970 dout("rbd_req_cb bytes=%lld readop=%d rc=%d\n", bytes, read_op, rc);
971
972 if (rc == -ENOENT && read_op) {
973 zero_bio_chain(req_data->bio, 0);
974 rc = 0;
975 } else if (rc == 0 && read_op && bytes < req_data->len) {
976 zero_bio_chain(req_data->bio, bytes);
977 bytes = req_data->len;
978 }
979
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700980 rbd_coll_end_req(req_data, rc, bytes);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700981
982 if (req_data->bio)
983 bio_chain_put(req_data->bio);
984
985 ceph_osdc_put_request(req);
986 kfree(req_data);
987}
988
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700989static void rbd_simple_req_cb(struct ceph_osd_request *req, struct ceph_msg *msg)
990{
991 ceph_osdc_put_request(req);
992}
993
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700994/*
995 * Do a synchronous ceph osd operation
996 */
997static int rbd_req_sync_op(struct rbd_device *dev,
998 struct ceph_snap_context *snapc,
999 u64 snapid,
1000 int opcode,
1001 int flags,
1002 struct ceph_osd_req_op *orig_ops,
1003 int num_reply,
1004 const char *obj,
1005 u64 ofs, u64 len,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001006 char *buf,
1007 struct ceph_osd_request **linger_req,
1008 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001009{
1010 int ret;
1011 struct page **pages;
1012 int num_pages;
1013 struct ceph_osd_req_op *ops = orig_ops;
1014 u32 payload_len;
1015
1016 num_pages = calc_pages_for(ofs , len);
1017 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
Dan Carpenterb8d06382010-10-11 21:14:23 +02001018 if (IS_ERR(pages))
1019 return PTR_ERR(pages);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001020
1021 if (!orig_ops) {
1022 payload_len = (flags & CEPH_OSD_FLAG_WRITE ? len : 0);
1023 ret = rbd_create_rw_ops(&ops, 1, opcode, payload_len);
1024 if (ret < 0)
1025 goto done;
1026
1027 if ((flags & CEPH_OSD_FLAG_WRITE) && buf) {
1028 ret = ceph_copy_to_page_vector(pages, buf, ofs, len);
1029 if (ret < 0)
1030 goto done_ops;
1031 }
1032 }
1033
1034 ret = rbd_do_request(NULL, dev, snapc, snapid,
1035 obj, ofs, len, NULL,
1036 pages, num_pages,
1037 flags,
1038 ops,
1039 2,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001040 NULL, 0,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001041 NULL,
1042 linger_req, ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001043 if (ret < 0)
1044 goto done_ops;
1045
1046 if ((flags & CEPH_OSD_FLAG_READ) && buf)
1047 ret = ceph_copy_from_page_vector(pages, buf, ofs, ret);
1048
1049done_ops:
1050 if (!orig_ops)
1051 rbd_destroy_ops(ops);
1052done:
1053 ceph_release_page_vector(pages, num_pages);
1054 return ret;
1055}
1056
1057/*
1058 * Do an asynchronous ceph osd operation
1059 */
1060static int rbd_do_op(struct request *rq,
1061 struct rbd_device *rbd_dev ,
1062 struct ceph_snap_context *snapc,
1063 u64 snapid,
1064 int opcode, int flags, int num_reply,
1065 u64 ofs, u64 len,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001066 struct bio *bio,
1067 struct rbd_req_coll *coll,
1068 int coll_index)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001069{
1070 char *seg_name;
1071 u64 seg_ofs;
1072 u64 seg_len;
1073 int ret;
1074 struct ceph_osd_req_op *ops;
1075 u32 payload_len;
1076
1077 seg_name = kmalloc(RBD_MAX_SEG_NAME_LEN + 1, GFP_NOIO);
1078 if (!seg_name)
1079 return -ENOMEM;
1080
1081 seg_len = rbd_get_segment(&rbd_dev->header,
1082 rbd_dev->header.block_name,
1083 ofs, len,
1084 seg_name, &seg_ofs);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001085
1086 payload_len = (flags & CEPH_OSD_FLAG_WRITE ? seg_len : 0);
1087
1088 ret = rbd_create_rw_ops(&ops, 1, opcode, payload_len);
1089 if (ret < 0)
1090 goto done;
1091
1092 /* we've taken care of segment sizes earlier when we
1093 cloned the bios. We should never have a segment
1094 truncated at this point */
1095 BUG_ON(seg_len < len);
1096
1097 ret = rbd_do_request(rq, rbd_dev, snapc, snapid,
1098 seg_name, seg_ofs, seg_len,
1099 bio,
1100 NULL, 0,
1101 flags,
1102 ops,
1103 num_reply,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001104 coll, coll_index,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001105 rbd_req_cb, 0, NULL);
Sage Weil11f77002011-05-12 16:13:54 -07001106
1107 rbd_destroy_ops(ops);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001108done:
1109 kfree(seg_name);
1110 return ret;
1111}
1112
1113/*
1114 * Request async osd write
1115 */
1116static int rbd_req_write(struct request *rq,
1117 struct rbd_device *rbd_dev,
1118 struct ceph_snap_context *snapc,
1119 u64 ofs, u64 len,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001120 struct bio *bio,
1121 struct rbd_req_coll *coll,
1122 int coll_index)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001123{
1124 return rbd_do_op(rq, rbd_dev, snapc, CEPH_NOSNAP,
1125 CEPH_OSD_OP_WRITE,
1126 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1127 2,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001128 ofs, len, bio, coll, coll_index);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001129}
1130
1131/*
1132 * Request async osd read
1133 */
1134static int rbd_req_read(struct request *rq,
1135 struct rbd_device *rbd_dev,
1136 u64 snapid,
1137 u64 ofs, u64 len,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001138 struct bio *bio,
1139 struct rbd_req_coll *coll,
1140 int coll_index)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001141{
1142 return rbd_do_op(rq, rbd_dev, NULL,
1143 (snapid ? snapid : CEPH_NOSNAP),
1144 CEPH_OSD_OP_READ,
1145 CEPH_OSD_FLAG_READ,
1146 2,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001147 ofs, len, bio, coll, coll_index);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001148}
1149
1150/*
1151 * Request sync osd read
1152 */
1153static int rbd_req_sync_read(struct rbd_device *dev,
1154 struct ceph_snap_context *snapc,
1155 u64 snapid,
1156 const char *obj,
1157 u64 ofs, u64 len,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001158 char *buf,
1159 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001160{
1161 return rbd_req_sync_op(dev, NULL,
1162 (snapid ? snapid : CEPH_NOSNAP),
1163 CEPH_OSD_OP_READ,
1164 CEPH_OSD_FLAG_READ,
1165 NULL,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001166 1, obj, ofs, len, buf, NULL, ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001167}
1168
1169/*
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001170 * Request sync osd watch
1171 */
1172static int rbd_req_sync_notify_ack(struct rbd_device *dev,
1173 u64 ver,
1174 u64 notify_id,
1175 const char *obj)
1176{
1177 struct ceph_osd_req_op *ops;
1178 struct page **pages = NULL;
Sage Weil11f77002011-05-12 16:13:54 -07001179 int ret;
1180
1181 ret = rbd_create_rw_ops(&ops, 1, CEPH_OSD_OP_NOTIFY_ACK, 0);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001182 if (ret < 0)
1183 return ret;
1184
1185 ops[0].watch.ver = cpu_to_le64(dev->header.obj_version);
1186 ops[0].watch.cookie = notify_id;
1187 ops[0].watch.flag = 0;
1188
1189 ret = rbd_do_request(NULL, dev, NULL, CEPH_NOSNAP,
1190 obj, 0, 0, NULL,
1191 pages, 0,
1192 CEPH_OSD_FLAG_READ,
1193 ops,
1194 1,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001195 NULL, 0,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001196 rbd_simple_req_cb, 0, NULL);
1197
1198 rbd_destroy_ops(ops);
1199 return ret;
1200}
1201
1202static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
1203{
1204 struct rbd_device *dev = (struct rbd_device *)data;
Sage Weil13143d22011-05-12 16:08:30 -07001205 int rc;
1206
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001207 if (!dev)
1208 return;
1209
1210 dout("rbd_watch_cb %s notify_id=%lld opcode=%d\n", dev->obj_md_name,
1211 notify_id, (int)opcode);
1212 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Sage Weil13143d22011-05-12 16:08:30 -07001213 rc = __rbd_update_snaps(dev);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001214 mutex_unlock(&ctl_mutex);
Sage Weil13143d22011-05-12 16:08:30 -07001215 if (rc)
1216 pr_warning(DRV_NAME "%d got notification but failed to update"
1217 " snaps: %d\n", dev->major, rc);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001218
1219 rbd_req_sync_notify_ack(dev, ver, notify_id, dev->obj_md_name);
1220}
1221
1222/*
1223 * Request sync osd watch
1224 */
1225static int rbd_req_sync_watch(struct rbd_device *dev,
1226 const char *obj,
1227 u64 ver)
1228{
1229 struct ceph_osd_req_op *ops;
Alex Elder1dbb4392012-01-24 10:08:37 -06001230 struct ceph_osd_client *osdc = &dev->rbd_client->client->osdc;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001231
1232 int ret = rbd_create_rw_ops(&ops, 1, CEPH_OSD_OP_WATCH, 0);
1233 if (ret < 0)
1234 return ret;
1235
1236 ret = ceph_osdc_create_event(osdc, rbd_watch_cb, 0,
1237 (void *)dev, &dev->watch_event);
1238 if (ret < 0)
1239 goto fail;
1240
1241 ops[0].watch.ver = cpu_to_le64(ver);
1242 ops[0].watch.cookie = cpu_to_le64(dev->watch_event->cookie);
1243 ops[0].watch.flag = 1;
1244
1245 ret = rbd_req_sync_op(dev, NULL,
1246 CEPH_NOSNAP,
1247 0,
1248 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1249 ops,
1250 1, obj, 0, 0, NULL,
1251 &dev->watch_request, NULL);
1252
1253 if (ret < 0)
1254 goto fail_event;
1255
1256 rbd_destroy_ops(ops);
1257 return 0;
1258
1259fail_event:
1260 ceph_osdc_cancel_event(dev->watch_event);
1261 dev->watch_event = NULL;
1262fail:
1263 rbd_destroy_ops(ops);
1264 return ret;
1265}
1266
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001267/*
1268 * Request sync osd unwatch
1269 */
1270static int rbd_req_sync_unwatch(struct rbd_device *dev,
1271 const char *obj)
1272{
1273 struct ceph_osd_req_op *ops;
1274
1275 int ret = rbd_create_rw_ops(&ops, 1, CEPH_OSD_OP_WATCH, 0);
1276 if (ret < 0)
1277 return ret;
1278
1279 ops[0].watch.ver = 0;
1280 ops[0].watch.cookie = cpu_to_le64(dev->watch_event->cookie);
1281 ops[0].watch.flag = 0;
1282
1283 ret = rbd_req_sync_op(dev, NULL,
1284 CEPH_NOSNAP,
1285 0,
1286 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1287 ops,
1288 1, obj, 0, 0, NULL, NULL, NULL);
1289
1290 rbd_destroy_ops(ops);
1291 ceph_osdc_cancel_event(dev->watch_event);
1292 dev->watch_event = NULL;
1293 return ret;
1294}
1295
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001296struct rbd_notify_info {
1297 struct rbd_device *dev;
1298};
1299
1300static void rbd_notify_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
1301{
1302 struct rbd_device *dev = (struct rbd_device *)data;
1303 if (!dev)
1304 return;
1305
1306 dout("rbd_notify_cb %s notify_id=%lld opcode=%d\n", dev->obj_md_name,
1307 notify_id, (int)opcode);
1308}
1309
1310/*
1311 * Request sync osd notify
1312 */
1313static int rbd_req_sync_notify(struct rbd_device *dev,
1314 const char *obj)
1315{
1316 struct ceph_osd_req_op *ops;
Alex Elder1dbb4392012-01-24 10:08:37 -06001317 struct ceph_osd_client *osdc = &dev->rbd_client->client->osdc;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001318 struct ceph_osd_event *event;
1319 struct rbd_notify_info info;
1320 int payload_len = sizeof(u32) + sizeof(u32);
1321 int ret;
1322
1323 ret = rbd_create_rw_ops(&ops, 1, CEPH_OSD_OP_NOTIFY, payload_len);
1324 if (ret < 0)
1325 return ret;
1326
1327 info.dev = dev;
1328
1329 ret = ceph_osdc_create_event(osdc, rbd_notify_cb, 1,
1330 (void *)&info, &event);
1331 if (ret < 0)
1332 goto fail;
1333
1334 ops[0].watch.ver = 1;
1335 ops[0].watch.flag = 1;
1336 ops[0].watch.cookie = event->cookie;
1337 ops[0].watch.prot_ver = RADOS_NOTIFY_VER;
1338 ops[0].watch.timeout = 12;
1339
1340 ret = rbd_req_sync_op(dev, NULL,
1341 CEPH_NOSNAP,
1342 0,
1343 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1344 ops,
1345 1, obj, 0, 0, NULL, NULL, NULL);
1346 if (ret < 0)
1347 goto fail_event;
1348
1349 ret = ceph_osdc_wait_event(event, CEPH_OSD_TIMEOUT_DEFAULT);
1350 dout("ceph_osdc_wait_event returned %d\n", ret);
1351 rbd_destroy_ops(ops);
1352 return 0;
1353
1354fail_event:
1355 ceph_osdc_cancel_event(event);
1356fail:
1357 rbd_destroy_ops(ops);
1358 return ret;
1359}
1360
1361/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001362 * Request sync osd read
1363 */
1364static int rbd_req_sync_exec(struct rbd_device *dev,
1365 const char *obj,
1366 const char *cls,
1367 const char *method,
1368 const char *data,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001369 int len,
1370 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001371{
1372 struct ceph_osd_req_op *ops;
1373 int cls_len = strlen(cls);
1374 int method_len = strlen(method);
1375 int ret = rbd_create_rw_ops(&ops, 1, CEPH_OSD_OP_CALL,
1376 cls_len + method_len + len);
1377 if (ret < 0)
1378 return ret;
1379
1380 ops[0].cls.class_name = cls;
1381 ops[0].cls.class_len = (__u8)cls_len;
1382 ops[0].cls.method_name = method;
1383 ops[0].cls.method_len = (__u8)method_len;
1384 ops[0].cls.argc = 0;
1385 ops[0].cls.indata = data;
1386 ops[0].cls.indata_len = len;
1387
1388 ret = rbd_req_sync_op(dev, NULL,
1389 CEPH_NOSNAP,
1390 0,
1391 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1392 ops,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001393 1, obj, 0, 0, NULL, NULL, ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001394
1395 rbd_destroy_ops(ops);
1396
1397 dout("cls_exec returned %d\n", ret);
1398 return ret;
1399}
1400
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001401static struct rbd_req_coll *rbd_alloc_coll(int num_reqs)
1402{
1403 struct rbd_req_coll *coll =
1404 kzalloc(sizeof(struct rbd_req_coll) +
1405 sizeof(struct rbd_req_status) * num_reqs,
1406 GFP_ATOMIC);
1407
1408 if (!coll)
1409 return NULL;
1410 coll->total = num_reqs;
1411 kref_init(&coll->kref);
1412 return coll;
1413}
1414
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001415/*
1416 * block device queue callback
1417 */
1418static void rbd_rq_fn(struct request_queue *q)
1419{
1420 struct rbd_device *rbd_dev = q->queuedata;
1421 struct request *rq;
1422 struct bio_pair *bp = NULL;
1423
1424 rq = blk_fetch_request(q);
1425
1426 while (1) {
1427 struct bio *bio;
1428 struct bio *rq_bio, *next_bio = NULL;
1429 bool do_write;
1430 int size, op_size = 0;
1431 u64 ofs;
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001432 int num_segs, cur_seg = 0;
1433 struct rbd_req_coll *coll;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001434
1435 /* peek at request from block layer */
1436 if (!rq)
1437 break;
1438
1439 dout("fetched request\n");
1440
1441 /* filter out block requests we don't understand */
1442 if ((rq->cmd_type != REQ_TYPE_FS)) {
1443 __blk_end_request_all(rq, 0);
1444 goto next;
1445 }
1446
1447 /* deduce our operation (read, write) */
1448 do_write = (rq_data_dir(rq) == WRITE);
1449
1450 size = blk_rq_bytes(rq);
1451 ofs = blk_rq_pos(rq) * 512ULL;
1452 rq_bio = rq->bio;
1453 if (do_write && rbd_dev->read_only) {
1454 __blk_end_request_all(rq, -EROFS);
1455 goto next;
1456 }
1457
1458 spin_unlock_irq(q->queue_lock);
1459
1460 dout("%s 0x%x bytes at 0x%llx\n",
1461 do_write ? "write" : "read",
1462 size, blk_rq_pos(rq) * 512ULL);
1463
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001464 num_segs = rbd_get_num_segments(&rbd_dev->header, ofs, size);
1465 coll = rbd_alloc_coll(num_segs);
1466 if (!coll) {
1467 spin_lock_irq(q->queue_lock);
1468 __blk_end_request_all(rq, -ENOMEM);
1469 goto next;
1470 }
1471
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001472 do {
1473 /* a bio clone to be passed down to OSD req */
1474 dout("rq->bio->bi_vcnt=%d\n", rq->bio->bi_vcnt);
1475 op_size = rbd_get_segment(&rbd_dev->header,
1476 rbd_dev->header.block_name,
1477 ofs, size,
1478 NULL, NULL);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001479 kref_get(&coll->kref);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001480 bio = bio_chain_clone(&rq_bio, &next_bio, &bp,
1481 op_size, GFP_ATOMIC);
1482 if (!bio) {
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001483 rbd_coll_end_req_index(rq, coll, cur_seg,
1484 -ENOMEM, op_size);
1485 goto next_seg;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001486 }
1487
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001488
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001489 /* init OSD command: write or read */
1490 if (do_write)
1491 rbd_req_write(rq, rbd_dev,
1492 rbd_dev->header.snapc,
1493 ofs,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001494 op_size, bio,
1495 coll, cur_seg);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001496 else
1497 rbd_req_read(rq, rbd_dev,
1498 cur_snap_id(rbd_dev),
1499 ofs,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001500 op_size, bio,
1501 coll, cur_seg);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001502
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001503next_seg:
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001504 size -= op_size;
1505 ofs += op_size;
1506
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001507 cur_seg++;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001508 rq_bio = next_bio;
1509 } while (size > 0);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001510 kref_put(&coll->kref, rbd_coll_release);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001511
1512 if (bp)
1513 bio_pair_release(bp);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001514 spin_lock_irq(q->queue_lock);
1515next:
1516 rq = blk_fetch_request(q);
1517 }
1518}
1519
1520/*
1521 * a queue callback. Makes sure that we don't create a bio that spans across
1522 * multiple osd objects. One exception would be with a single page bios,
1523 * which we handle later at bio_chain_clone
1524 */
1525static int rbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd,
1526 struct bio_vec *bvec)
1527{
1528 struct rbd_device *rbd_dev = q->queuedata;
1529 unsigned int chunk_sectors = 1 << (rbd_dev->header.obj_order - 9);
1530 sector_t sector = bmd->bi_sector + get_start_sect(bmd->bi_bdev);
1531 unsigned int bio_sectors = bmd->bi_size >> 9;
1532 int max;
1533
1534 max = (chunk_sectors - ((sector & (chunk_sectors - 1))
1535 + bio_sectors)) << 9;
1536 if (max < 0)
1537 max = 0; /* bio_add cannot handle a negative return */
1538 if (max <= bvec->bv_len && bio_sectors == 0)
1539 return bvec->bv_len;
1540 return max;
1541}
1542
1543static void rbd_free_disk(struct rbd_device *rbd_dev)
1544{
1545 struct gendisk *disk = rbd_dev->disk;
1546
1547 if (!disk)
1548 return;
1549
1550 rbd_header_free(&rbd_dev->header);
1551
1552 if (disk->flags & GENHD_FL_UP)
1553 del_gendisk(disk);
1554 if (disk->queue)
1555 blk_cleanup_queue(disk->queue);
1556 put_disk(disk);
1557}
1558
1559/*
1560 * reload the ondisk the header
1561 */
1562static int rbd_read_header(struct rbd_device *rbd_dev,
1563 struct rbd_image_header *header)
1564{
1565 ssize_t rc;
1566 struct rbd_image_header_ondisk *dh;
1567 int snap_count = 0;
1568 u64 snap_names_len = 0;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001569 u64 ver;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001570
1571 while (1) {
1572 int len = sizeof(*dh) +
1573 snap_count * sizeof(struct rbd_image_snap_ondisk) +
1574 snap_names_len;
1575
1576 rc = -ENOMEM;
1577 dh = kmalloc(len, GFP_KERNEL);
1578 if (!dh)
1579 return -ENOMEM;
1580
1581 rc = rbd_req_sync_read(rbd_dev,
1582 NULL, CEPH_NOSNAP,
1583 rbd_dev->obj_md_name,
1584 0, len,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001585 (char *)dh, &ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001586 if (rc < 0)
1587 goto out_dh;
1588
1589 rc = rbd_header_from_disk(header, dh, snap_count, GFP_KERNEL);
Josh Durgin81e759f2011-11-15 14:49:53 -08001590 if (rc < 0) {
1591 if (rc == -ENXIO) {
1592 pr_warning("unrecognized header format"
1593 " for image %s", rbd_dev->obj);
1594 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001595 goto out_dh;
Josh Durgin81e759f2011-11-15 14:49:53 -08001596 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001597
1598 if (snap_count != header->total_snaps) {
1599 snap_count = header->total_snaps;
1600 snap_names_len = header->snap_names_len;
1601 rbd_header_free(header);
1602 kfree(dh);
1603 continue;
1604 }
1605 break;
1606 }
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001607 header->obj_version = ver;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001608
1609out_dh:
1610 kfree(dh);
1611 return rc;
1612}
1613
1614/*
1615 * create a snapshot
1616 */
1617static int rbd_header_add_snap(struct rbd_device *dev,
1618 const char *snap_name,
1619 gfp_t gfp_flags)
1620{
1621 int name_len = strlen(snap_name);
1622 u64 new_snapid;
1623 int ret;
Sage Weil916d4d62011-05-12 16:10:50 -07001624 void *data, *p, *e;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001625 u64 ver;
Alex Elder1dbb4392012-01-24 10:08:37 -06001626 struct ceph_mon_client *monc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001627
1628 /* we should create a snapshot only if we're pointing at the head */
1629 if (dev->cur_snap)
1630 return -EINVAL;
1631
Alex Elder1dbb4392012-01-24 10:08:37 -06001632 monc = &dev->rbd_client->client->monc;
1633 ret = ceph_monc_create_snapid(monc, dev->poolid, &new_snapid);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001634 dout("created snapid=%lld\n", new_snapid);
1635 if (ret < 0)
1636 return ret;
1637
1638 data = kmalloc(name_len + 16, gfp_flags);
1639 if (!data)
1640 return -ENOMEM;
1641
Sage Weil916d4d62011-05-12 16:10:50 -07001642 p = data;
1643 e = data + name_len + 16;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001644
Sage Weil916d4d62011-05-12 16:10:50 -07001645 ceph_encode_string_safe(&p, e, snap_name, name_len, bad);
1646 ceph_encode_64_safe(&p, e, new_snapid, bad);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001647
1648 ret = rbd_req_sync_exec(dev, dev->obj_md_name, "rbd", "snap_add",
Sage Weil916d4d62011-05-12 16:10:50 -07001649 data, p - data, &ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001650
Sage Weil916d4d62011-05-12 16:10:50 -07001651 kfree(data);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001652
1653 if (ret < 0)
1654 return ret;
1655
1656 dev->header.snapc->seq = new_snapid;
1657
1658 return 0;
1659bad:
1660 return -ERANGE;
1661}
1662
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001663static void __rbd_remove_all_snaps(struct rbd_device *rbd_dev)
1664{
1665 struct rbd_snap *snap;
1666
1667 while (!list_empty(&rbd_dev->snaps)) {
1668 snap = list_first_entry(&rbd_dev->snaps, struct rbd_snap, node);
1669 __rbd_remove_snap_dev(rbd_dev, snap);
1670 }
1671}
1672
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001673/*
1674 * only read the first part of the ondisk header, without the snaps info
1675 */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001676static int __rbd_update_snaps(struct rbd_device *rbd_dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001677{
1678 int ret;
1679 struct rbd_image_header h;
1680 u64 snap_seq;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001681 int follow_seq = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001682
1683 ret = rbd_read_header(rbd_dev, &h);
1684 if (ret < 0)
1685 return ret;
1686
Sage Weil9db4b3e2011-04-19 22:49:06 -07001687 /* resized? */
1688 set_capacity(rbd_dev->disk, h.image_size / 512ULL);
1689
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001690 down_write(&rbd_dev->header.snap_rwsem);
1691
1692 snap_seq = rbd_dev->header.snapc->seq;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001693 if (rbd_dev->header.total_snaps &&
1694 rbd_dev->header.snapc->snaps[0] == snap_seq)
1695 /* pointing at the head, will need to follow that
1696 if head moves */
1697 follow_seq = 1;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001698
1699 kfree(rbd_dev->header.snapc);
1700 kfree(rbd_dev->header.snap_names);
1701 kfree(rbd_dev->header.snap_sizes);
1702
1703 rbd_dev->header.total_snaps = h.total_snaps;
1704 rbd_dev->header.snapc = h.snapc;
1705 rbd_dev->header.snap_names = h.snap_names;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001706 rbd_dev->header.snap_names_len = h.snap_names_len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001707 rbd_dev->header.snap_sizes = h.snap_sizes;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001708 if (follow_seq)
1709 rbd_dev->header.snapc->seq = rbd_dev->header.snapc->snaps[0];
1710 else
1711 rbd_dev->header.snapc->seq = snap_seq;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001712
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001713 ret = __rbd_init_snaps_header(rbd_dev);
1714
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001715 up_write(&rbd_dev->header.snap_rwsem);
1716
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001717 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001718}
1719
1720static int rbd_init_disk(struct rbd_device *rbd_dev)
1721{
1722 struct gendisk *disk;
1723 struct request_queue *q;
1724 int rc;
1725 u64 total_size = 0;
1726
1727 /* contact OSD, request size info about the object being mapped */
1728 rc = rbd_read_header(rbd_dev, &rbd_dev->header);
1729 if (rc)
1730 return rc;
1731
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001732 /* no need to lock here, as rbd_dev is not registered yet */
1733 rc = __rbd_init_snaps_header(rbd_dev);
1734 if (rc)
1735 return rc;
1736
Josh Durgincc9d7342011-11-21 18:19:13 -08001737 rc = rbd_header_set_snap(rbd_dev, &total_size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001738 if (rc)
1739 return rc;
1740
1741 /* create gendisk info */
1742 rc = -ENOMEM;
1743 disk = alloc_disk(RBD_MINORS_PER_MAJOR);
1744 if (!disk)
1745 goto out;
1746
Sage Weilaedfec52011-05-12 20:57:03 -07001747 snprintf(disk->disk_name, sizeof(disk->disk_name), DRV_NAME "%d",
1748 rbd_dev->id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001749 disk->major = rbd_dev->major;
1750 disk->first_minor = 0;
1751 disk->fops = &rbd_bd_ops;
1752 disk->private_data = rbd_dev;
1753
1754 /* init rq */
1755 rc = -ENOMEM;
1756 q = blk_init_queue(rbd_rq_fn, &rbd_dev->lock);
1757 if (!q)
1758 goto out_disk;
Josh Durgin029bcbd2011-07-22 11:35:23 -07001759
1760 /* set io sizes to object size */
1761 blk_queue_max_hw_sectors(q, rbd_obj_bytes(&rbd_dev->header) / 512ULL);
1762 blk_queue_max_segment_size(q, rbd_obj_bytes(&rbd_dev->header));
1763 blk_queue_io_min(q, rbd_obj_bytes(&rbd_dev->header));
1764 blk_queue_io_opt(q, rbd_obj_bytes(&rbd_dev->header));
1765
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001766 blk_queue_merge_bvec(q, rbd_merge_bvec);
1767 disk->queue = q;
1768
1769 q->queuedata = rbd_dev;
1770
1771 rbd_dev->disk = disk;
1772 rbd_dev->q = q;
1773
1774 /* finally, announce the disk to the world */
1775 set_capacity(disk, total_size / 512ULL);
1776 add_disk(disk);
1777
1778 pr_info("%s: added with size 0x%llx\n",
1779 disk->disk_name, (unsigned long long)total_size);
1780 return 0;
1781
1782out_disk:
1783 put_disk(disk);
1784out:
1785 return rc;
1786}
1787
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001788/*
1789 sysfs
1790*/
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001791
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001792static ssize_t rbd_size_show(struct device *dev,
1793 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001794{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001795 struct rbd_device *rbd_dev = dev_to_rbd(dev);
1796
1797 return sprintf(buf, "%llu\n", (unsigned long long)rbd_dev->header.image_size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001798}
1799
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001800static ssize_t rbd_major_show(struct device *dev,
1801 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001802{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001803 struct rbd_device *rbd_dev = dev_to_rbd(dev);
1804
1805 return sprintf(buf, "%d\n", rbd_dev->major);
1806}
1807
1808static ssize_t rbd_client_id_show(struct device *dev,
1809 struct device_attribute *attr, char *buf)
1810{
1811 struct rbd_device *rbd_dev = dev_to_rbd(dev);
1812
Alex Elder1dbb4392012-01-24 10:08:37 -06001813 return sprintf(buf, "client%lld\n",
1814 ceph_client_id(rbd_dev->rbd_client->client));
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001815}
1816
1817static ssize_t rbd_pool_show(struct device *dev,
1818 struct device_attribute *attr, char *buf)
1819{
1820 struct rbd_device *rbd_dev = dev_to_rbd(dev);
1821
1822 return sprintf(buf, "%s\n", rbd_dev->pool_name);
1823}
1824
1825static ssize_t rbd_name_show(struct device *dev,
1826 struct device_attribute *attr, char *buf)
1827{
1828 struct rbd_device *rbd_dev = dev_to_rbd(dev);
1829
1830 return sprintf(buf, "%s\n", rbd_dev->obj);
1831}
1832
1833static ssize_t rbd_snap_show(struct device *dev,
1834 struct device_attribute *attr,
1835 char *buf)
1836{
1837 struct rbd_device *rbd_dev = dev_to_rbd(dev);
1838
1839 return sprintf(buf, "%s\n", rbd_dev->snap_name);
1840}
1841
1842static ssize_t rbd_image_refresh(struct device *dev,
1843 struct device_attribute *attr,
1844 const char *buf,
1845 size_t size)
1846{
1847 struct rbd_device *rbd_dev = dev_to_rbd(dev);
1848 int rc;
1849 int ret = size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001850
1851 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
1852
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001853 rc = __rbd_update_snaps(rbd_dev);
1854 if (rc < 0)
1855 ret = rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001856
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001857 mutex_unlock(&ctl_mutex);
1858 return ret;
1859}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001860
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001861static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
1862static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
1863static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
1864static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
1865static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
1866static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
1867static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
1868static DEVICE_ATTR(create_snap, S_IWUSR, NULL, rbd_snap_add);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001869
1870static struct attribute *rbd_attrs[] = {
1871 &dev_attr_size.attr,
1872 &dev_attr_major.attr,
1873 &dev_attr_client_id.attr,
1874 &dev_attr_pool.attr,
1875 &dev_attr_name.attr,
1876 &dev_attr_current_snap.attr,
1877 &dev_attr_refresh.attr,
1878 &dev_attr_create_snap.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001879 NULL
1880};
1881
1882static struct attribute_group rbd_attr_group = {
1883 .attrs = rbd_attrs,
1884};
1885
1886static const struct attribute_group *rbd_attr_groups[] = {
1887 &rbd_attr_group,
1888 NULL
1889};
1890
1891static void rbd_sysfs_dev_release(struct device *dev)
1892{
1893}
1894
1895static struct device_type rbd_device_type = {
1896 .name = "rbd",
1897 .groups = rbd_attr_groups,
1898 .release = rbd_sysfs_dev_release,
1899};
1900
1901
1902/*
1903 sysfs - snapshots
1904*/
1905
1906static ssize_t rbd_snap_size_show(struct device *dev,
1907 struct device_attribute *attr,
1908 char *buf)
1909{
1910 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
1911
1912 return sprintf(buf, "%lld\n", (long long)snap->size);
1913}
1914
1915static ssize_t rbd_snap_id_show(struct device *dev,
1916 struct device_attribute *attr,
1917 char *buf)
1918{
1919 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
1920
1921 return sprintf(buf, "%lld\n", (long long)snap->id);
1922}
1923
1924static DEVICE_ATTR(snap_size, S_IRUGO, rbd_snap_size_show, NULL);
1925static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL);
1926
1927static struct attribute *rbd_snap_attrs[] = {
1928 &dev_attr_snap_size.attr,
1929 &dev_attr_snap_id.attr,
1930 NULL,
1931};
1932
1933static struct attribute_group rbd_snap_attr_group = {
1934 .attrs = rbd_snap_attrs,
1935};
1936
1937static void rbd_snap_dev_release(struct device *dev)
1938{
1939 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
1940 kfree(snap->name);
1941 kfree(snap);
1942}
1943
1944static const struct attribute_group *rbd_snap_attr_groups[] = {
1945 &rbd_snap_attr_group,
1946 NULL
1947};
1948
1949static struct device_type rbd_snap_device_type = {
1950 .groups = rbd_snap_attr_groups,
1951 .release = rbd_snap_dev_release,
1952};
1953
1954static void __rbd_remove_snap_dev(struct rbd_device *rbd_dev,
1955 struct rbd_snap *snap)
1956{
1957 list_del(&snap->node);
1958 device_unregister(&snap->dev);
1959}
1960
1961static int rbd_register_snap_dev(struct rbd_device *rbd_dev,
1962 struct rbd_snap *snap,
1963 struct device *parent)
1964{
1965 struct device *dev = &snap->dev;
1966 int ret;
1967
1968 dev->type = &rbd_snap_device_type;
1969 dev->parent = parent;
1970 dev->release = rbd_snap_dev_release;
1971 dev_set_name(dev, "snap_%s", snap->name);
1972 ret = device_register(dev);
1973
1974 return ret;
1975}
1976
1977static int __rbd_add_snap_dev(struct rbd_device *rbd_dev,
1978 int i, const char *name,
1979 struct rbd_snap **snapp)
1980{
1981 int ret;
1982 struct rbd_snap *snap = kzalloc(sizeof(*snap), GFP_KERNEL);
1983 if (!snap)
1984 return -ENOMEM;
1985 snap->name = kstrdup(name, GFP_KERNEL);
1986 snap->size = rbd_dev->header.snap_sizes[i];
1987 snap->id = rbd_dev->header.snapc->snaps[i];
1988 if (device_is_registered(&rbd_dev->dev)) {
1989 ret = rbd_register_snap_dev(rbd_dev, snap,
1990 &rbd_dev->dev);
1991 if (ret < 0)
1992 goto err;
1993 }
1994 *snapp = snap;
1995 return 0;
1996err:
1997 kfree(snap->name);
1998 kfree(snap);
1999 return ret;
2000}
2001
2002/*
2003 * search for the previous snap in a null delimited string list
2004 */
2005const char *rbd_prev_snap_name(const char *name, const char *start)
2006{
2007 if (name < start + 2)
2008 return NULL;
2009
2010 name -= 2;
2011 while (*name) {
2012 if (name == start)
2013 return start;
2014 name--;
2015 }
2016 return name + 1;
2017}
2018
2019/*
2020 * compare the old list of snapshots that we have to what's in the header
2021 * and update it accordingly. Note that the header holds the snapshots
2022 * in a reverse order (from newest to oldest) and we need to go from
2023 * older to new so that we don't get a duplicate snap name when
2024 * doing the process (e.g., removed snapshot and recreated a new
2025 * one with the same name.
2026 */
2027static int __rbd_init_snaps_header(struct rbd_device *rbd_dev)
2028{
2029 const char *name, *first_name;
2030 int i = rbd_dev->header.total_snaps;
2031 struct rbd_snap *snap, *old_snap = NULL;
2032 int ret;
2033 struct list_head *p, *n;
2034
2035 first_name = rbd_dev->header.snap_names;
2036 name = first_name + rbd_dev->header.snap_names_len;
2037
2038 list_for_each_prev_safe(p, n, &rbd_dev->snaps) {
2039 u64 cur_id;
2040
2041 old_snap = list_entry(p, struct rbd_snap, node);
2042
2043 if (i)
2044 cur_id = rbd_dev->header.snapc->snaps[i - 1];
2045
2046 if (!i || old_snap->id < cur_id) {
2047 /* old_snap->id was skipped, thus was removed */
2048 __rbd_remove_snap_dev(rbd_dev, old_snap);
2049 continue;
2050 }
2051 if (old_snap->id == cur_id) {
2052 /* we have this snapshot already */
2053 i--;
2054 name = rbd_prev_snap_name(name, first_name);
2055 continue;
2056 }
2057 for (; i > 0;
2058 i--, name = rbd_prev_snap_name(name, first_name)) {
2059 if (!name) {
2060 WARN_ON(1);
2061 return -EINVAL;
2062 }
2063 cur_id = rbd_dev->header.snapc->snaps[i];
2064 /* snapshot removal? handle it above */
2065 if (cur_id >= old_snap->id)
2066 break;
2067 /* a new snapshot */
2068 ret = __rbd_add_snap_dev(rbd_dev, i - 1, name, &snap);
2069 if (ret < 0)
2070 return ret;
2071
2072 /* note that we add it backward so using n and not p */
2073 list_add(&snap->node, n);
2074 p = &snap->node;
2075 }
2076 }
2077 /* we're done going over the old snap list, just add what's left */
2078 for (; i > 0; i--) {
2079 name = rbd_prev_snap_name(name, first_name);
2080 if (!name) {
2081 WARN_ON(1);
2082 return -EINVAL;
2083 }
2084 ret = __rbd_add_snap_dev(rbd_dev, i - 1, name, &snap);
2085 if (ret < 0)
2086 return ret;
2087 list_add(&snap->node, &rbd_dev->snaps);
2088 }
2089
2090 return 0;
2091}
2092
2093
2094static void rbd_root_dev_release(struct device *dev)
2095{
2096}
2097
2098static struct device rbd_root_dev = {
2099 .init_name = "rbd",
2100 .release = rbd_root_dev_release,
2101};
2102
2103static int rbd_bus_add_dev(struct rbd_device *rbd_dev)
2104{
2105 int ret = -ENOMEM;
2106 struct device *dev;
2107 struct rbd_snap *snap;
2108
2109 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2110 dev = &rbd_dev->dev;
2111
2112 dev->bus = &rbd_bus_type;
2113 dev->type = &rbd_device_type;
2114 dev->parent = &rbd_root_dev;
2115 dev->release = rbd_dev_release;
2116 dev_set_name(dev, "%d", rbd_dev->id);
2117 ret = device_register(dev);
2118 if (ret < 0)
2119 goto done_free;
2120
2121 list_for_each_entry(snap, &rbd_dev->snaps, node) {
2122 ret = rbd_register_snap_dev(rbd_dev, snap,
2123 &rbd_dev->dev);
2124 if (ret < 0)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002125 break;
2126 }
2127
2128 mutex_unlock(&ctl_mutex);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002129 return 0;
2130done_free:
2131 mutex_unlock(&ctl_mutex);
2132 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002133}
2134
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002135static void rbd_bus_del_dev(struct rbd_device *rbd_dev)
2136{
2137 device_unregister(&rbd_dev->dev);
2138}
2139
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002140static int rbd_init_watch_dev(struct rbd_device *rbd_dev)
2141{
2142 int ret, rc;
2143
2144 do {
2145 ret = rbd_req_sync_watch(rbd_dev, rbd_dev->obj_md_name,
2146 rbd_dev->header.obj_version);
2147 if (ret == -ERANGE) {
2148 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2149 rc = __rbd_update_snaps(rbd_dev);
2150 mutex_unlock(&ctl_mutex);
2151 if (rc < 0)
2152 return rc;
2153 }
2154 } while (ret == -ERANGE);
2155
2156 return ret;
2157}
2158
Alex Elder1ddbe942012-01-29 13:57:44 -06002159static atomic64_t rbd_id_max = ATOMIC64_INIT(0);
2160
2161/*
Alex Elder499afd52012-02-02 08:13:29 -06002162 * Get a unique rbd identifier for the given new rbd_dev, and add
2163 * the rbd_dev to the global list. The minimum rbd id is 1.
Alex Elder1ddbe942012-01-29 13:57:44 -06002164 */
Alex Elder499afd52012-02-02 08:13:29 -06002165static void rbd_id_get(struct rbd_device *rbd_dev)
Alex Elderb7f23c32012-01-29 13:57:43 -06002166{
Alex Elder499afd52012-02-02 08:13:29 -06002167 rbd_dev->id = atomic64_inc_return(&rbd_id_max);
2168
2169 spin_lock(&rbd_dev_list_lock);
2170 list_add_tail(&rbd_dev->node, &rbd_dev_list);
2171 spin_unlock(&rbd_dev_list_lock);
Alex Elder1ddbe942012-01-29 13:57:44 -06002172}
Alex Elderb7f23c32012-01-29 13:57:43 -06002173
Alex Elder1ddbe942012-01-29 13:57:44 -06002174/*
Alex Elder499afd52012-02-02 08:13:29 -06002175 * Remove an rbd_dev from the global list, and record that its
2176 * identifier is no longer in use.
Alex Elder1ddbe942012-01-29 13:57:44 -06002177 */
Alex Elder499afd52012-02-02 08:13:29 -06002178static void rbd_id_put(struct rbd_device *rbd_dev)
Alex Elder1ddbe942012-01-29 13:57:44 -06002179{
Alex Elderd184f6b2012-01-29 13:57:44 -06002180 struct list_head *tmp;
2181 int rbd_id = rbd_dev->id;
2182 int max_id;
2183
2184 BUG_ON(rbd_id < 1);
Alex Elder499afd52012-02-02 08:13:29 -06002185
2186 spin_lock(&rbd_dev_list_lock);
2187 list_del_init(&rbd_dev->node);
Alex Elderd184f6b2012-01-29 13:57:44 -06002188
2189 /*
2190 * If the id being "put" is not the current maximum, there
2191 * is nothing special we need to do.
2192 */
2193 if (rbd_id != atomic64_read(&rbd_id_max)) {
2194 spin_unlock(&rbd_dev_list_lock);
2195 return;
2196 }
2197
2198 /*
2199 * We need to update the current maximum id. Search the
2200 * list to find out what it is. We're more likely to find
2201 * the maximum at the end, so search the list backward.
2202 */
2203 max_id = 0;
2204 list_for_each_prev(tmp, &rbd_dev_list) {
2205 struct rbd_device *rbd_dev;
2206
2207 rbd_dev = list_entry(tmp, struct rbd_device, node);
2208 if (rbd_id > max_id)
2209 max_id = rbd_id;
2210 }
Alex Elder499afd52012-02-02 08:13:29 -06002211 spin_unlock(&rbd_dev_list_lock);
Alex Elderb7f23c32012-01-29 13:57:43 -06002212
Alex Elder1ddbe942012-01-29 13:57:44 -06002213 /*
Alex Elderd184f6b2012-01-29 13:57:44 -06002214 * The max id could have been updated by rbd_id_get(), in
2215 * which case it now accurately reflects the new maximum.
2216 * Be careful not to overwrite the maximum value in that
2217 * case.
Alex Elder1ddbe942012-01-29 13:57:44 -06002218 */
Alex Elderd184f6b2012-01-29 13:57:44 -06002219 atomic64_cmpxchg(&rbd_id_max, rbd_id, max_id);
Alex Elderb7f23c32012-01-29 13:57:43 -06002220}
2221
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002222static ssize_t rbd_add(struct bus_type *bus,
2223 const char *buf,
2224 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002225{
2226 struct ceph_osd_client *osdc;
2227 struct rbd_device *rbd_dev;
2228 ssize_t rc = -ENOMEM;
Alex Elderb7f23c32012-01-29 13:57:43 -06002229 int irc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002230 char *mon_dev_name;
2231 char *options;
2232
2233 if (!try_module_get(THIS_MODULE))
2234 return -ENODEV;
2235
2236 mon_dev_name = kmalloc(RBD_MAX_OPT_LEN, GFP_KERNEL);
2237 if (!mon_dev_name)
2238 goto err_out_mod;
2239
2240 options = kmalloc(RBD_MAX_OPT_LEN, GFP_KERNEL);
2241 if (!options)
2242 goto err_mon_dev;
2243
2244 /* new rbd_device object */
2245 rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL);
2246 if (!rbd_dev)
2247 goto err_out_opt;
2248
2249 /* static rbd_device initialization */
2250 spin_lock_init(&rbd_dev->lock);
2251 INIT_LIST_HEAD(&rbd_dev->node);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002252 INIT_LIST_HEAD(&rbd_dev->snaps);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002253
Alex Elder0e805a12012-01-11 19:42:15 -08002254 init_rwsem(&rbd_dev->header.snap_rwsem);
2255
Alex Elderd184f6b2012-01-29 13:57:44 -06002256 /* generate unique id: find highest unique id, add one */
Alex Elder499afd52012-02-02 08:13:29 -06002257 rbd_id_get(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002258
2259 /* parse add command */
2260 if (sscanf(buf, "%" __stringify(RBD_MAX_OPT_LEN) "s "
2261 "%" __stringify(RBD_MAX_OPT_LEN) "s "
2262 "%" __stringify(RBD_MAX_POOL_NAME_LEN) "s "
2263 "%" __stringify(RBD_MAX_OBJ_NAME_LEN) "s"
2264 "%" __stringify(RBD_MAX_SNAP_NAME_LEN) "s",
2265 mon_dev_name, options, rbd_dev->pool_name,
2266 rbd_dev->obj, rbd_dev->snap_name) < 4) {
2267 rc = -EINVAL;
2268 goto err_out_slot;
2269 }
2270
2271 if (rbd_dev->snap_name[0] == 0)
Josh Durgincc9d7342011-11-21 18:19:13 -08002272 memcpy(rbd_dev->snap_name, RBD_SNAP_HEAD_NAME,
2273 sizeof (RBD_SNAP_HEAD_NAME));
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002274
2275 rbd_dev->obj_len = strlen(rbd_dev->obj);
2276 snprintf(rbd_dev->obj_md_name, sizeof(rbd_dev->obj_md_name), "%s%s",
2277 rbd_dev->obj, RBD_SUFFIX);
2278
2279 /* initialize rest of new object */
2280 snprintf(rbd_dev->name, DEV_NAME_LEN, DRV_NAME "%d", rbd_dev->id);
Alex Eldere124a82f2012-01-29 13:57:44 -06002281
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002282 rc = rbd_get_client(rbd_dev, mon_dev_name, options);
2283 if (rc < 0)
2284 goto err_out_slot;
2285
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002286 /* pick the pool */
Alex Elder1dbb4392012-01-24 10:08:37 -06002287 osdc = &rbd_dev->rbd_client->client->osdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002288 rc = ceph_pg_poolid_by_name(osdc->osdmap, rbd_dev->pool_name);
2289 if (rc < 0)
2290 goto err_out_client;
2291 rbd_dev->poolid = rc;
2292
2293 /* register our block device */
2294 irc = register_blkdev(0, rbd_dev->name);
2295 if (irc < 0) {
2296 rc = irc;
2297 goto err_out_client;
2298 }
2299 rbd_dev->major = irc;
2300
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002301 rc = rbd_bus_add_dev(rbd_dev);
2302 if (rc)
Yehuda Sadeh766fc432011-01-07 14:58:42 -08002303 goto err_out_blkdev;
2304
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002305 /* set up and announce blkdev mapping */
2306 rc = rbd_init_disk(rbd_dev);
2307 if (rc)
Yehuda Sadeh766fc432011-01-07 14:58:42 -08002308 goto err_out_bus;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002309
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002310 rc = rbd_init_watch_dev(rbd_dev);
2311 if (rc)
2312 goto err_out_bus;
2313
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002314 return count;
2315
Yehuda Sadeh766fc432011-01-07 14:58:42 -08002316err_out_bus:
Alex Elder499afd52012-02-02 08:13:29 -06002317 rbd_id_put(rbd_dev);
Yehuda Sadeh766fc432011-01-07 14:58:42 -08002318
2319 /* this will also clean up rest of rbd_dev stuff */
2320
2321 rbd_bus_del_dev(rbd_dev);
2322 kfree(options);
2323 kfree(mon_dev_name);
2324 return rc;
2325
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002326err_out_blkdev:
2327 unregister_blkdev(rbd_dev->major, rbd_dev->name);
2328err_out_client:
2329 rbd_put_client(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002330err_out_slot:
Alex Elder499afd52012-02-02 08:13:29 -06002331 rbd_id_put(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002332
2333 kfree(rbd_dev);
2334err_out_opt:
2335 kfree(options);
2336err_mon_dev:
2337 kfree(mon_dev_name);
2338err_out_mod:
2339 dout("Error adding device %s\n", buf);
2340 module_put(THIS_MODULE);
2341 return rc;
2342}
2343
2344static struct rbd_device *__rbd_get_dev(unsigned long id)
2345{
2346 struct list_head *tmp;
2347 struct rbd_device *rbd_dev;
2348
Alex Eldere124a82f2012-01-29 13:57:44 -06002349 spin_lock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002350 list_for_each(tmp, &rbd_dev_list) {
2351 rbd_dev = list_entry(tmp, struct rbd_device, node);
Alex Eldere124a82f2012-01-29 13:57:44 -06002352 if (rbd_dev->id == id) {
2353 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002354 return rbd_dev;
Alex Eldere124a82f2012-01-29 13:57:44 -06002355 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002356 }
Alex Eldere124a82f2012-01-29 13:57:44 -06002357 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002358 return NULL;
2359}
2360
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002361static void rbd_dev_release(struct device *dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002362{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002363 struct rbd_device *rbd_dev =
2364 container_of(dev, struct rbd_device, dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002365
Alex Elder1dbb4392012-01-24 10:08:37 -06002366 if (rbd_dev->watch_request) {
2367 struct ceph_client *client = rbd_dev->rbd_client->client;
2368
2369 ceph_osdc_unregister_linger_request(&client->osdc,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002370 rbd_dev->watch_request);
Alex Elder1dbb4392012-01-24 10:08:37 -06002371 }
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002372 if (rbd_dev->watch_event)
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07002373 rbd_req_sync_unwatch(rbd_dev, rbd_dev->obj_md_name);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002374
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002375 rbd_put_client(rbd_dev);
2376
2377 /* clean up and free blkdev */
2378 rbd_free_disk(rbd_dev);
2379 unregister_blkdev(rbd_dev->major, rbd_dev->name);
2380 kfree(rbd_dev);
2381
2382 /* release module ref */
2383 module_put(THIS_MODULE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002384}
2385
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002386static ssize_t rbd_remove(struct bus_type *bus,
2387 const char *buf,
2388 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002389{
2390 struct rbd_device *rbd_dev = NULL;
2391 int target_id, rc;
2392 unsigned long ul;
2393 int ret = count;
2394
2395 rc = strict_strtoul(buf, 10, &ul);
2396 if (rc)
2397 return rc;
2398
2399 /* convert to int; abort if we lost anything in the conversion */
2400 target_id = (int) ul;
2401 if (target_id != ul)
2402 return -EINVAL;
2403
2404 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2405
2406 rbd_dev = __rbd_get_dev(target_id);
2407 if (!rbd_dev) {
2408 ret = -ENOENT;
2409 goto done;
2410 }
2411
Alex Elder499afd52012-02-02 08:13:29 -06002412 rbd_id_put(rbd_dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002413
2414 __rbd_remove_all_snaps(rbd_dev);
2415 rbd_bus_del_dev(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002416
2417done:
2418 mutex_unlock(&ctl_mutex);
2419 return ret;
2420}
2421
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002422static ssize_t rbd_snap_add(struct device *dev,
2423 struct device_attribute *attr,
2424 const char *buf,
2425 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002426{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002427 struct rbd_device *rbd_dev = dev_to_rbd(dev);
2428 int ret;
2429 char *name = kmalloc(count + 1, GFP_KERNEL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002430 if (!name)
2431 return -ENOMEM;
2432
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002433 snprintf(name, count, "%s", buf);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002434
2435 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
2436
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002437 ret = rbd_header_add_snap(rbd_dev,
2438 name, GFP_KERNEL);
2439 if (ret < 0)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002440 goto err_unlock;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002441
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002442 ret = __rbd_update_snaps(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002443 if (ret < 0)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002444 goto err_unlock;
2445
2446 /* shouldn't hold ctl_mutex when notifying.. notify might
2447 trigger a watch callback that would need to get that mutex */
2448 mutex_unlock(&ctl_mutex);
2449
2450 /* make a best effort, don't error if failed */
2451 rbd_req_sync_notify(rbd_dev, rbd_dev->obj_md_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002452
2453 ret = count;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07002454 kfree(name);
2455 return ret;
2456
2457err_unlock:
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002458 mutex_unlock(&ctl_mutex);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002459 kfree(name);
2460 return ret;
2461}
2462
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002463static struct bus_attribute rbd_bus_attrs[] = {
2464 __ATTR(add, S_IWUSR, NULL, rbd_add),
2465 __ATTR(remove, S_IWUSR, NULL, rbd_remove),
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002466 __ATTR_NULL
2467};
2468
2469/*
2470 * create control files in sysfs
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002471 * /sys/bus/rbd/...
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002472 */
2473static int rbd_sysfs_init(void)
2474{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002475 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002476
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002477 rbd_bus_type.bus_attrs = rbd_bus_attrs;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002478
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002479 ret = bus_register(&rbd_bus_type);
Alex Elder21079782012-01-24 10:08:36 -06002480 if (ret < 0)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002481 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002482
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002483 ret = device_register(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002484
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002485 return ret;
2486}
2487
2488static void rbd_sysfs_cleanup(void)
2489{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002490 device_unregister(&rbd_root_dev);
2491 bus_unregister(&rbd_bus_type);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002492}
2493
2494int __init rbd_init(void)
2495{
2496 int rc;
2497
2498 rc = rbd_sysfs_init();
2499 if (rc)
2500 return rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002501 pr_info("loaded " DRV_NAME_LONG "\n");
2502 return 0;
2503}
2504
2505void __exit rbd_exit(void)
2506{
2507 rbd_sysfs_cleanup();
2508}
2509
2510module_init(rbd_init);
2511module_exit(rbd_exit);
2512
2513MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
2514MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
2515MODULE_DESCRIPTION("rados block device");
2516
2517/* following authorship retained from original osdblk.c */
2518MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
2519
2520MODULE_LICENSE("GPL");