blob: 85131de900121b6cd738b19ef359ab6abf3706e2 [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 Elderaafb2302012-09-06 16:00:54 -050044#define RBD_DEBUG /* Activate rbd_assert() calls */
45
Alex Elder593a9e72012-02-07 12:03:37 -060046/*
47 * The basic unit of block I/O is a sector. It is interpreted in a
48 * number of contexts in Linux (blk, bio, genhd), but the default is
49 * universally 512 bytes. These symbols are just slightly more
50 * meaningful than the bare numbers they represent.
51 */
52#define SECTOR_SHIFT 9
53#define SECTOR_SIZE (1ULL << SECTOR_SHIFT)
54
Alex Elderdf111be2012-08-09 10:33:26 -070055/* It might be useful to have this defined elsewhere too */
56
57#define U64_MAX ((u64) (~0ULL))
58
Alex Elderf0f8cef2012-01-29 13:57:44 -060059#define RBD_DRV_NAME "rbd"
60#define RBD_DRV_NAME_LONG "rbd (rados block device)"
Yehuda Sadeh602adf42010-08-12 16:11:25 -070061
62#define RBD_MINORS_PER_MAJOR 256 /* max minors per blkdev */
63
Alex Elderd4b125e2012-07-03 16:01:19 -050064#define RBD_SNAP_DEV_NAME_PREFIX "snap_"
65#define RBD_MAX_SNAP_NAME_LEN \
66 (NAME_MAX - (sizeof (RBD_SNAP_DEV_NAME_PREFIX) - 1))
67
Alex Elder35d489f2012-07-03 16:01:19 -050068#define RBD_MAX_SNAP_COUNT 510 /* allows max snapc to fit in 4KB */
Yehuda Sadeh602adf42010-08-12 16:11:25 -070069#define RBD_MAX_OPT_LEN 1024
70
71#define RBD_SNAP_HEAD_NAME "-"
72
Alex Elder9e15b772012-10-30 19:40:33 -050073/* This allows a single page to hold an image name sent by OSD */
74#define RBD_IMAGE_NAME_LEN_MAX (PAGE_SIZE - sizeof (__le32) - 1)
Alex Elder1e130192012-07-03 16:01:19 -050075#define RBD_IMAGE_ID_LEN_MAX 64
Alex Elder9e15b772012-10-30 19:40:33 -050076
Alex Elder1e130192012-07-03 16:01:19 -050077#define RBD_OBJ_PREFIX_LEN_MAX 64
Alex Elder589d30e2012-07-10 20:30:11 -050078
Alex Elderd8891402012-10-09 13:50:17 -070079/* Feature bits */
80
81#define RBD_FEATURE_LAYERING 1
82
83/* Features supported by this (client software) implementation. */
84
85#define RBD_FEATURES_ALL (0)
86
Alex Elder81a89792012-02-02 08:13:30 -060087/*
88 * An RBD device name will be "rbd#", where the "rbd" comes from
89 * RBD_DRV_NAME above, and # is a unique integer identifier.
90 * MAX_INT_FORMAT_WIDTH is used in ensuring DEV_NAME_LEN is big
91 * enough to hold all possible device names.
92 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -070093#define DEV_NAME_LEN 32
Alex Elder81a89792012-02-02 08:13:30 -060094#define MAX_INT_FORMAT_WIDTH ((5 * sizeof (int)) / 2 + 1)
Yehuda Sadeh602adf42010-08-12 16:11:25 -070095
Alex Eldercc0538b2012-08-10 13:12:07 -070096#define RBD_READ_ONLY_DEFAULT false
Yehuda Sadeh59c2be12011-03-21 15:10:11 -070097
Yehuda Sadeh602adf42010-08-12 16:11:25 -070098/*
99 * block device image metadata (in-memory version)
100 */
101struct rbd_image_header {
Alex Elderf84344f2012-08-31 17:29:51 -0500102 /* These four fields never change for a given rbd image */
Alex Elder849b4262012-07-09 21:04:24 -0500103 char *object_prefix;
Alex Elder34b13182012-07-13 20:35:12 -0500104 u64 features;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700105 __u8 obj_order;
106 __u8 crypt_type;
107 __u8 comp_type;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700108
Alex Elderf84344f2012-08-31 17:29:51 -0500109 /* The remaining fields need to be updated occasionally */
110 u64 image_size;
111 struct ceph_snap_context *snapc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700112 char *snap_names;
113 u64 *snap_sizes;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700114
115 u64 obj_version;
116};
117
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500118/*
119 * An rbd image specification.
120 *
121 * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely
Alex Elderc66c6e02012-11-01 08:39:26 -0500122 * identify an image. Each rbd_dev structure includes a pointer to
123 * an rbd_spec structure that encapsulates this identity.
124 *
125 * Each of the id's in an rbd_spec has an associated name. For a
126 * user-mapped image, the names are supplied and the id's associated
127 * with them are looked up. For a layered image, a parent image is
128 * defined by the tuple, and the names are looked up.
129 *
130 * An rbd_dev structure contains a parent_spec pointer which is
131 * non-null if the image it represents is a child in a layered
132 * image. This pointer will refer to the rbd_spec structure used
133 * by the parent rbd_dev for its own identity (i.e., the structure
134 * is shared between the parent and child).
135 *
136 * Since these structures are populated once, during the discovery
137 * phase of image construction, they are effectively immutable so
138 * we make no effort to synchronize access to them.
139 *
140 * Note that code herein does not assume the image name is known (it
141 * could be a null pointer).
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500142 */
143struct rbd_spec {
144 u64 pool_id;
145 char *pool_name;
146
147 char *image_id;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500148 char *image_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500149
150 u64 snap_id;
151 char *snap_name;
152
153 struct kref kref;
154};
155
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700156struct rbd_options {
Alex Eldercc0538b2012-08-10 13:12:07 -0700157 bool read_only;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700158};
159
160/*
Alex Elderf0f8cef2012-01-29 13:57:44 -0600161 * an instance of the client. multiple devices may share an rbd client.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700162 */
163struct rbd_client {
164 struct ceph_client *client;
165 struct kref kref;
166 struct list_head node;
167};
168
169/*
Alex Elderf0f8cef2012-01-29 13:57:44 -0600170 * a request completion status
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700171 */
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700172struct rbd_req_status {
173 int done;
174 int rc;
175 u64 bytes;
176};
177
178/*
179 * a collection of requests
180 */
181struct rbd_req_coll {
182 int total;
183 int num_done;
184 struct kref kref;
185 struct rbd_req_status status[0];
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700186};
187
Alex Elderf0f8cef2012-01-29 13:57:44 -0600188/*
189 * a single io request
190 */
191struct rbd_request {
192 struct request *rq; /* blk layer request */
193 struct bio *bio; /* cloned bio */
194 struct page **pages; /* list of used pages */
195 u64 len;
196 int coll_index;
197 struct rbd_req_coll *coll;
198};
199
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800200struct rbd_snap {
201 struct device dev;
202 const char *name;
Josh Durgin3591538f2011-12-05 18:25:13 -0800203 u64 size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800204 struct list_head node;
205 u64 id;
Alex Elder34b13182012-07-13 20:35:12 -0500206 u64 features;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800207};
208
Alex Elderf84344f2012-08-31 17:29:51 -0500209struct rbd_mapping {
Alex Elder99c1f082012-08-30 14:42:15 -0500210 u64 size;
Alex Elder34b13182012-07-13 20:35:12 -0500211 u64 features;
Alex Elderf84344f2012-08-31 17:29:51 -0500212 bool read_only;
213};
214
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700215/*
216 * a single device
217 */
218struct rbd_device {
Alex Elderde71a292012-07-03 16:01:19 -0500219 int dev_id; /* blkdev unique id */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700220
221 int major; /* blkdev assigned major */
222 struct gendisk *disk; /* blkdev's gendisk and rq */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700223
Alex Eldera30b71b2012-07-10 20:30:11 -0500224 u32 image_format; /* Either 1 or 2 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700225 struct rbd_client *rbd_client;
226
227 char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */
228
229 spinlock_t lock; /* queue lock */
230
231 struct rbd_image_header header;
Alex Elderdaba5fd2012-10-26 17:25:23 -0500232 bool exists;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500233 struct rbd_spec *spec;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700234
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500235 char *header_name;
Alex Elder971f8392012-10-25 23:34:41 -0500236
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700237 struct ceph_osd_event *watch_event;
238 struct ceph_osd_request *watch_request;
239
Alex Elder86b00e02012-10-25 23:34:42 -0500240 struct rbd_spec *parent_spec;
241 u64 parent_overlap;
242
Josh Durginc6666012011-11-21 17:11:12 -0800243 /* protects updating the header */
244 struct rw_semaphore header_rwsem;
Alex Elderf84344f2012-08-31 17:29:51 -0500245
246 struct rbd_mapping mapping;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700247
248 struct list_head node;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800249
250 /* list of snapshots */
251 struct list_head snaps;
252
253 /* sysfs related */
254 struct device dev;
Alex Elder42382b72012-11-16 09:29:16 -0600255 unsigned long open_count;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800256};
257
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700258static DEFINE_MUTEX(ctl_mutex); /* Serialize open/close/setup/teardown */
Alex Eldere124a82f2012-01-29 13:57:44 -0600259
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700260static LIST_HEAD(rbd_dev_list); /* devices */
Alex Eldere124a82f2012-01-29 13:57:44 -0600261static DEFINE_SPINLOCK(rbd_dev_list_lock);
262
Alex Elder432b8582012-01-29 13:57:44 -0600263static LIST_HEAD(rbd_client_list); /* clients */
264static DEFINE_SPINLOCK(rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700265
Alex Elder304f6802012-08-31 17:29:52 -0500266static int rbd_dev_snaps_update(struct rbd_device *rbd_dev);
267static int rbd_dev_snaps_register(struct rbd_device *rbd_dev);
268
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800269static void rbd_dev_release(struct device *dev);
Alex Elder41f38c22012-10-25 23:34:40 -0500270static void rbd_remove_snap_dev(struct rbd_snap *snap);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800271
Alex Elderf0f8cef2012-01-29 13:57:44 -0600272static ssize_t rbd_add(struct bus_type *bus, const char *buf,
273 size_t count);
274static ssize_t rbd_remove(struct bus_type *bus, const char *buf,
275 size_t count);
276
277static struct bus_attribute rbd_bus_attrs[] = {
278 __ATTR(add, S_IWUSR, NULL, rbd_add),
279 __ATTR(remove, S_IWUSR, NULL, rbd_remove),
280 __ATTR_NULL
281};
282
283static struct bus_type rbd_bus_type = {
284 .name = "rbd",
285 .bus_attrs = rbd_bus_attrs,
286};
287
288static void rbd_root_dev_release(struct device *dev)
289{
290}
291
292static struct device rbd_root_dev = {
293 .init_name = "rbd",
294 .release = rbd_root_dev_release,
295};
296
Alex Elder06ecc6c2012-11-01 10:17:15 -0500297static __printf(2, 3)
298void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...)
299{
300 struct va_format vaf;
301 va_list args;
302
303 va_start(args, fmt);
304 vaf.fmt = fmt;
305 vaf.va = &args;
306
307 if (!rbd_dev)
308 printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf);
309 else if (rbd_dev->disk)
310 printk(KERN_WARNING "%s: %s: %pV\n",
311 RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf);
312 else if (rbd_dev->spec && rbd_dev->spec->image_name)
313 printk(KERN_WARNING "%s: image %s: %pV\n",
314 RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf);
315 else if (rbd_dev->spec && rbd_dev->spec->image_id)
316 printk(KERN_WARNING "%s: id %s: %pV\n",
317 RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf);
318 else /* punt */
319 printk(KERN_WARNING "%s: rbd_dev %p: %pV\n",
320 RBD_DRV_NAME, rbd_dev, &vaf);
321 va_end(args);
322}
323
Alex Elderaafb2302012-09-06 16:00:54 -0500324#ifdef RBD_DEBUG
325#define rbd_assert(expr) \
326 if (unlikely(!(expr))) { \
327 printk(KERN_ERR "\nAssertion failure in %s() " \
328 "at line %d:\n\n" \
329 "\trbd_assert(%s);\n\n", \
330 __func__, __LINE__, #expr); \
331 BUG(); \
332 }
333#else /* !RBD_DEBUG */
334# define rbd_assert(expr) ((void) 0)
335#endif /* !RBD_DEBUG */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800336
Alex Elder117973f2012-08-31 17:29:55 -0500337static int rbd_dev_refresh(struct rbd_device *rbd_dev, u64 *hver);
338static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev, u64 *hver);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700339
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700340static int rbd_open(struct block_device *bdev, fmode_t mode)
341{
Alex Elderf0f8cef2012-01-29 13:57:44 -0600342 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700343
Alex Elderf84344f2012-08-31 17:29:51 -0500344 if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700345 return -EROFS;
346
Alex Elder42382b72012-11-16 09:29:16 -0600347 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Alex Elderc3e946c2012-11-16 09:29:16 -0600348 (void) get_device(&rbd_dev->dev);
Alex Elderf84344f2012-08-31 17:29:51 -0500349 set_device_ro(bdev, rbd_dev->mapping.read_only);
Alex Elder42382b72012-11-16 09:29:16 -0600350 rbd_dev->open_count++;
351 mutex_unlock(&ctl_mutex);
Alex Elder340c7a22012-08-10 13:12:07 -0700352
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700353 return 0;
354}
355
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800356static int rbd_release(struct gendisk *disk, fmode_t mode)
357{
358 struct rbd_device *rbd_dev = disk->private_data;
359
Alex Elder42382b72012-11-16 09:29:16 -0600360 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
361 rbd_assert(rbd_dev->open_count > 0);
362 rbd_dev->open_count--;
Alex Elderc3e946c2012-11-16 09:29:16 -0600363 put_device(&rbd_dev->dev);
Alex Elder42382b72012-11-16 09:29:16 -0600364 mutex_unlock(&ctl_mutex);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800365
366 return 0;
367}
368
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700369static const struct block_device_operations rbd_bd_ops = {
370 .owner = THIS_MODULE,
371 .open = rbd_open,
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800372 .release = rbd_release,
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700373};
374
375/*
376 * Initialize an rbd client instance.
Alex Elder43ae4702012-07-03 16:01:18 -0500377 * We own *ceph_opts.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700378 */
Alex Elderf8c38922012-08-10 13:12:07 -0700379static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700380{
381 struct rbd_client *rbdc;
382 int ret = -ENOMEM;
383
384 dout("rbd_client_create\n");
385 rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL);
386 if (!rbdc)
387 goto out_opt;
388
389 kref_init(&rbdc->kref);
390 INIT_LIST_HEAD(&rbdc->node);
391
Alex Elderbc534d82012-01-29 13:57:44 -0600392 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
393
Alex Elder43ae4702012-07-03 16:01:18 -0500394 rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700395 if (IS_ERR(rbdc->client))
Alex Elderbc534d82012-01-29 13:57:44 -0600396 goto out_mutex;
Alex Elder43ae4702012-07-03 16:01:18 -0500397 ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700398
399 ret = ceph_open_session(rbdc->client);
400 if (ret < 0)
401 goto out_err;
402
Alex Elder432b8582012-01-29 13:57:44 -0600403 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700404 list_add_tail(&rbdc->node, &rbd_client_list);
Alex Elder432b8582012-01-29 13:57:44 -0600405 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700406
Alex Elderbc534d82012-01-29 13:57:44 -0600407 mutex_unlock(&ctl_mutex);
408
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700409 dout("rbd_client_create created %p\n", rbdc);
410 return rbdc;
411
412out_err:
413 ceph_destroy_client(rbdc->client);
Alex Elderbc534d82012-01-29 13:57:44 -0600414out_mutex:
415 mutex_unlock(&ctl_mutex);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700416 kfree(rbdc);
417out_opt:
Alex Elder43ae4702012-07-03 16:01:18 -0500418 if (ceph_opts)
419 ceph_destroy_options(ceph_opts);
Vasiliy Kulikov28f259b2010-09-26 12:59:37 +0400420 return ERR_PTR(ret);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700421}
422
423/*
Alex Elder1f7ba332012-08-10 13:12:07 -0700424 * Find a ceph client with specific addr and configuration. If
425 * found, bump its reference count.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700426 */
Alex Elder1f7ba332012-08-10 13:12:07 -0700427static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700428{
429 struct rbd_client *client_node;
Alex Elder1f7ba332012-08-10 13:12:07 -0700430 bool found = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700431
Alex Elder43ae4702012-07-03 16:01:18 -0500432 if (ceph_opts->flags & CEPH_OPT_NOSHARE)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700433 return NULL;
434
Alex Elder1f7ba332012-08-10 13:12:07 -0700435 spin_lock(&rbd_client_list_lock);
436 list_for_each_entry(client_node, &rbd_client_list, node) {
437 if (!ceph_compare_options(ceph_opts, client_node->client)) {
438 kref_get(&client_node->kref);
439 found = true;
440 break;
441 }
442 }
443 spin_unlock(&rbd_client_list_lock);
444
445 return found ? client_node : NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700446}
447
448/*
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700449 * mount options
450 */
451enum {
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700452 Opt_last_int,
453 /* int args above */
454 Opt_last_string,
455 /* string args above */
Alex Eldercc0538b2012-08-10 13:12:07 -0700456 Opt_read_only,
457 Opt_read_write,
458 /* Boolean args above */
459 Opt_last_bool,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700460};
461
Alex Elder43ae4702012-07-03 16:01:18 -0500462static match_table_t rbd_opts_tokens = {
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700463 /* int args above */
464 /* string args above */
Alex Elderbe466c12012-10-22 11:31:26 -0500465 {Opt_read_only, "read_only"},
Alex Eldercc0538b2012-08-10 13:12:07 -0700466 {Opt_read_only, "ro"}, /* Alternate spelling */
467 {Opt_read_write, "read_write"},
468 {Opt_read_write, "rw"}, /* Alternate spelling */
469 /* Boolean args above */
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700470 {-1, NULL}
471};
472
473static int parse_rbd_opts_token(char *c, void *private)
474{
Alex Elder43ae4702012-07-03 16:01:18 -0500475 struct rbd_options *rbd_opts = private;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700476 substring_t argstr[MAX_OPT_ARGS];
477 int token, intval, ret;
478
Alex Elder43ae4702012-07-03 16:01:18 -0500479 token = match_token(c, rbd_opts_tokens, argstr);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700480 if (token < 0)
481 return -EINVAL;
482
483 if (token < Opt_last_int) {
484 ret = match_int(&argstr[0], &intval);
485 if (ret < 0) {
486 pr_err("bad mount option arg (not int) "
487 "at '%s'\n", c);
488 return ret;
489 }
490 dout("got int token %d val %d\n", token, intval);
491 } else if (token > Opt_last_int && token < Opt_last_string) {
492 dout("got string token %d val %s\n", token,
493 argstr[0].from);
Alex Eldercc0538b2012-08-10 13:12:07 -0700494 } else if (token > Opt_last_string && token < Opt_last_bool) {
495 dout("got Boolean token %d\n", token);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700496 } else {
497 dout("got token %d\n", token);
498 }
499
500 switch (token) {
Alex Eldercc0538b2012-08-10 13:12:07 -0700501 case Opt_read_only:
502 rbd_opts->read_only = true;
503 break;
504 case Opt_read_write:
505 rbd_opts->read_only = false;
506 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700507 default:
Alex Elderaafb2302012-09-06 16:00:54 -0500508 rbd_assert(false);
509 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700510 }
511 return 0;
512}
513
514/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700515 * Get a ceph client with specific addr and configuration, if one does
516 * not exist create it.
517 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500518static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700519{
Alex Elderf8c38922012-08-10 13:12:07 -0700520 struct rbd_client *rbdc;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700521
Alex Elder1f7ba332012-08-10 13:12:07 -0700522 rbdc = rbd_client_find(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500523 if (rbdc) /* using an existing client */
Alex Elder43ae4702012-07-03 16:01:18 -0500524 ceph_destroy_options(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500525 else
Alex Elderf8c38922012-08-10 13:12:07 -0700526 rbdc = rbd_client_create(ceph_opts);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700527
Alex Elder9d3997f2012-10-25 23:34:42 -0500528 return rbdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700529}
530
531/*
532 * Destroy ceph client
Alex Elderd23a4b32012-01-29 13:57:43 -0600533 *
Alex Elder432b8582012-01-29 13:57:44 -0600534 * Caller must hold rbd_client_list_lock.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700535 */
536static void rbd_client_release(struct kref *kref)
537{
538 struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
539
540 dout("rbd_release_client %p\n", rbdc);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500541 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700542 list_del(&rbdc->node);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500543 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700544
545 ceph_destroy_client(rbdc->client);
546 kfree(rbdc);
547}
548
549/*
550 * Drop reference to ceph client node. If it's not referenced anymore, release
551 * it.
552 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500553static void rbd_put_client(struct rbd_client *rbdc)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700554{
Alex Elderc53d5892012-10-25 23:34:42 -0500555 if (rbdc)
556 kref_put(&rbdc->kref, rbd_client_release);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700557}
558
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700559/*
560 * Destroy requests collection
561 */
562static void rbd_coll_release(struct kref *kref)
563{
564 struct rbd_req_coll *coll =
565 container_of(kref, struct rbd_req_coll, kref);
566
567 dout("rbd_coll_release %p\n", coll);
568 kfree(coll);
569}
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700570
Alex Eldera30b71b2012-07-10 20:30:11 -0500571static bool rbd_image_format_valid(u32 image_format)
572{
573 return image_format == 1 || image_format == 2;
574}
575
Alex Elder8e94af82012-07-25 09:32:40 -0500576static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
577{
Alex Elder103a1502012-08-02 11:29:45 -0500578 size_t size;
579 u32 snap_count;
580
581 /* The header has to start with the magic rbd header text */
582 if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT)))
583 return false;
584
Alex Elderdb2388b2012-10-20 22:17:27 -0500585 /* The bio layer requires at least sector-sized I/O */
586
587 if (ondisk->options.order < SECTOR_SHIFT)
588 return false;
589
590 /* If we use u64 in a few spots we may be able to loosen this */
591
592 if (ondisk->options.order > 8 * sizeof (int) - 1)
593 return false;
594
Alex Elder103a1502012-08-02 11:29:45 -0500595 /*
596 * The size of a snapshot header has to fit in a size_t, and
597 * that limits the number of snapshots.
598 */
599 snap_count = le32_to_cpu(ondisk->snap_count);
600 size = SIZE_MAX - sizeof (struct ceph_snap_context);
601 if (snap_count > size / sizeof (__le64))
602 return false;
603
604 /*
605 * Not only that, but the size of the entire the snapshot
606 * header must also be representable in a size_t.
607 */
608 size -= snap_count * sizeof (__le64);
609 if ((u64) size < le64_to_cpu(ondisk->snap_names_len))
610 return false;
611
612 return true;
Alex Elder8e94af82012-07-25 09:32:40 -0500613}
614
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700615/*
616 * Create a new header structure, translate header format from the on-disk
617 * header.
618 */
619static int rbd_header_from_disk(struct rbd_image_header *header,
Alex Elder4156d992012-08-02 11:29:46 -0500620 struct rbd_image_header_ondisk *ondisk)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700621{
Alex Elderccece232012-07-10 20:30:10 -0500622 u32 snap_count;
Alex Elder58c17b02012-08-23 23:22:06 -0500623 size_t len;
Alex Elderd2bb24e2012-07-26 23:37:14 -0500624 size_t size;
Alex Elder621901d2012-08-23 23:22:06 -0500625 u32 i;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700626
Alex Elder6a523252012-07-19 17:12:59 -0500627 memset(header, 0, sizeof (*header));
628
Alex Elder103a1502012-08-02 11:29:45 -0500629 snap_count = le32_to_cpu(ondisk->snap_count);
630
Alex Elder58c17b02012-08-23 23:22:06 -0500631 len = strnlen(ondisk->object_prefix, sizeof (ondisk->object_prefix));
632 header->object_prefix = kmalloc(len + 1, GFP_KERNEL);
Alex Elder6a523252012-07-19 17:12:59 -0500633 if (!header->object_prefix)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700634 return -ENOMEM;
Alex Elder58c17b02012-08-23 23:22:06 -0500635 memcpy(header->object_prefix, ondisk->object_prefix, len);
636 header->object_prefix[len] = '\0';
Alex Elder00f1f362012-02-07 12:03:36 -0600637
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700638 if (snap_count) {
Alex Elderf785cc12012-08-23 23:22:06 -0500639 u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len);
640
Alex Elder621901d2012-08-23 23:22:06 -0500641 /* Save a copy of the snapshot names */
642
Alex Elderf785cc12012-08-23 23:22:06 -0500643 if (snap_names_len > (u64) SIZE_MAX)
644 return -EIO;
645 header->snap_names = kmalloc(snap_names_len, GFP_KERNEL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700646 if (!header->snap_names)
Alex Elder6a523252012-07-19 17:12:59 -0500647 goto out_err;
Alex Elderf785cc12012-08-23 23:22:06 -0500648 /*
649 * Note that rbd_dev_v1_header_read() guarantees
650 * the ondisk buffer we're working with has
651 * snap_names_len bytes beyond the end of the
652 * snapshot id array, this memcpy() is safe.
653 */
654 memcpy(header->snap_names, &ondisk->snaps[snap_count],
655 snap_names_len);
Alex Elder6a523252012-07-19 17:12:59 -0500656
Alex Elder621901d2012-08-23 23:22:06 -0500657 /* Record each snapshot's size */
658
Alex Elderd2bb24e2012-07-26 23:37:14 -0500659 size = snap_count * sizeof (*header->snap_sizes);
660 header->snap_sizes = kmalloc(size, GFP_KERNEL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700661 if (!header->snap_sizes)
Alex Elder6a523252012-07-19 17:12:59 -0500662 goto out_err;
Alex Elder621901d2012-08-23 23:22:06 -0500663 for (i = 0; i < snap_count; i++)
664 header->snap_sizes[i] =
665 le64_to_cpu(ondisk->snaps[i].image_size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700666 } else {
Alex Elderccece232012-07-10 20:30:10 -0500667 WARN_ON(ondisk->snap_names_len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700668 header->snap_names = NULL;
669 header->snap_sizes = NULL;
670 }
Alex Elder849b4262012-07-09 21:04:24 -0500671
Alex Elder34b13182012-07-13 20:35:12 -0500672 header->features = 0; /* No features support in v1 images */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700673 header->obj_order = ondisk->options.order;
674 header->crypt_type = ondisk->options.crypt_type;
675 header->comp_type = ondisk->options.comp_type;
Alex Elder6a523252012-07-19 17:12:59 -0500676
Alex Elder621901d2012-08-23 23:22:06 -0500677 /* Allocate and fill in the snapshot context */
678
Alex Elderf84344f2012-08-31 17:29:51 -0500679 header->image_size = le64_to_cpu(ondisk->image_size);
Alex Elder6a523252012-07-19 17:12:59 -0500680 size = sizeof (struct ceph_snap_context);
681 size += snap_count * sizeof (header->snapc->snaps[0]);
682 header->snapc = kzalloc(size, GFP_KERNEL);
683 if (!header->snapc)
684 goto out_err;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700685
686 atomic_set(&header->snapc->nref, 1);
Alex Elder505cbb92012-07-19 08:49:18 -0500687 header->snapc->seq = le64_to_cpu(ondisk->snap_seq);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700688 header->snapc->num_snaps = snap_count;
Alex Elder621901d2012-08-23 23:22:06 -0500689 for (i = 0; i < snap_count; i++)
690 header->snapc->snaps[i] =
691 le64_to_cpu(ondisk->snaps[i].id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700692
693 return 0;
694
Alex Elder6a523252012-07-19 17:12:59 -0500695out_err:
Alex Elder849b4262012-07-09 21:04:24 -0500696 kfree(header->snap_sizes);
Alex Elderccece232012-07-10 20:30:10 -0500697 header->snap_sizes = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700698 kfree(header->snap_names);
Alex Elderccece232012-07-10 20:30:10 -0500699 header->snap_names = NULL;
Alex Elder6a523252012-07-19 17:12:59 -0500700 kfree(header->object_prefix);
701 header->object_prefix = NULL;
Alex Elderccece232012-07-10 20:30:10 -0500702
Alex Elder00f1f362012-02-07 12:03:36 -0600703 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700704}
705
Alex Elder9e15b772012-10-30 19:40:33 -0500706static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id)
707{
708 struct rbd_snap *snap;
709
710 if (snap_id == CEPH_NOSNAP)
711 return RBD_SNAP_HEAD_NAME;
712
713 list_for_each_entry(snap, &rbd_dev->snaps, node)
714 if (snap_id == snap->id)
715 return snap->name;
716
717 return NULL;
718}
719
Alex Elder8836b992012-08-30 14:42:15 -0500720static int snap_by_name(struct rbd_device *rbd_dev, const char *snap_name)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700721{
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700722
Alex Eldere86924a2012-07-10 20:30:11 -0500723 struct rbd_snap *snap;
Alex Elder00f1f362012-02-07 12:03:36 -0600724
Alex Eldere86924a2012-07-10 20:30:11 -0500725 list_for_each_entry(snap, &rbd_dev->snaps, node) {
726 if (!strcmp(snap_name, snap->name)) {
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500727 rbd_dev->spec->snap_id = snap->id;
Alex Eldere86924a2012-07-10 20:30:11 -0500728 rbd_dev->mapping.size = snap->size;
Alex Elder34b13182012-07-13 20:35:12 -0500729 rbd_dev->mapping.features = snap->features;
Alex Elder00f1f362012-02-07 12:03:36 -0600730
Alex Eldere86924a2012-07-10 20:30:11 -0500731 return 0;
Alex Elder00f1f362012-02-07 12:03:36 -0600732 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700733 }
Alex Eldere86924a2012-07-10 20:30:11 -0500734
Alex Elder00f1f362012-02-07 12:03:36 -0600735 return -ENOENT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700736}
737
Alex Elder819d52b2012-10-25 23:34:41 -0500738static int rbd_dev_set_mapping(struct rbd_device *rbd_dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700739{
Alex Elder78dc4472012-07-19 08:49:18 -0500740 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700741
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500742 if (!memcmp(rbd_dev->spec->snap_name, RBD_SNAP_HEAD_NAME,
Josh Durgincc9d7342011-11-21 18:19:13 -0800743 sizeof (RBD_SNAP_HEAD_NAME))) {
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500744 rbd_dev->spec->snap_id = CEPH_NOSNAP;
Alex Elder99c1f082012-08-30 14:42:15 -0500745 rbd_dev->mapping.size = rbd_dev->header.image_size;
Alex Elder34b13182012-07-13 20:35:12 -0500746 rbd_dev->mapping.features = rbd_dev->header.features;
Alex Eldere86924a2012-07-10 20:30:11 -0500747 ret = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700748 } else {
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500749 ret = snap_by_name(rbd_dev, rbd_dev->spec->snap_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700750 if (ret < 0)
751 goto done;
Alex Elderf84344f2012-08-31 17:29:51 -0500752 rbd_dev->mapping.read_only = true;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700753 }
Alex Elderdaba5fd2012-10-26 17:25:23 -0500754 rbd_dev->exists = true;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700755done:
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700756 return ret;
757}
758
759static void rbd_header_free(struct rbd_image_header *header)
760{
Alex Elder849b4262012-07-09 21:04:24 -0500761 kfree(header->object_prefix);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500762 header->object_prefix = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700763 kfree(header->snap_sizes);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500764 header->snap_sizes = NULL;
Alex Elder849b4262012-07-09 21:04:24 -0500765 kfree(header->snap_names);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500766 header->snap_names = NULL;
Josh Durgind1d25642011-12-05 14:03:05 -0800767 ceph_put_snap_context(header->snapc);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500768 header->snapc = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700769}
770
Alex Elder65ccfe22012-08-09 10:33:26 -0700771static char *rbd_segment_name(struct rbd_device *rbd_dev, u64 offset)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700772{
Alex Elder65ccfe22012-08-09 10:33:26 -0700773 char *name;
774 u64 segment;
775 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700776
Alex Elder2fd82b92012-11-09 15:05:54 -0600777 name = kmalloc(MAX_OBJ_NAME_SIZE + 1, GFP_NOIO);
Alex Elder65ccfe22012-08-09 10:33:26 -0700778 if (!name)
779 return NULL;
780 segment = offset >> rbd_dev->header.obj_order;
Alex Elder2fd82b92012-11-09 15:05:54 -0600781 ret = snprintf(name, MAX_OBJ_NAME_SIZE + 1, "%s.%012llx",
Alex Elder65ccfe22012-08-09 10:33:26 -0700782 rbd_dev->header.object_prefix, segment);
Alex Elder2fd82b92012-11-09 15:05:54 -0600783 if (ret < 0 || ret > MAX_OBJ_NAME_SIZE) {
Alex Elder65ccfe22012-08-09 10:33:26 -0700784 pr_err("error formatting segment name for #%llu (%d)\n",
785 segment, ret);
786 kfree(name);
787 name = NULL;
788 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700789
Alex Elder65ccfe22012-08-09 10:33:26 -0700790 return name;
791}
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700792
Alex Elder65ccfe22012-08-09 10:33:26 -0700793static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset)
794{
795 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700796
Alex Elder65ccfe22012-08-09 10:33:26 -0700797 return offset & (segment_size - 1);
798}
799
800static u64 rbd_segment_length(struct rbd_device *rbd_dev,
801 u64 offset, u64 length)
802{
803 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
804
805 offset &= segment_size - 1;
806
Alex Elderaafb2302012-09-06 16:00:54 -0500807 rbd_assert(length <= U64_MAX - offset);
Alex Elder65ccfe22012-08-09 10:33:26 -0700808 if (offset + length > segment_size)
809 length = segment_size - offset;
810
811 return length;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700812}
813
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700814static int rbd_get_num_segments(struct rbd_image_header *header,
815 u64 ofs, u64 len)
816{
Alex Elderdf111be2012-08-09 10:33:26 -0700817 u64 start_seg;
818 u64 end_seg;
819
820 if (!len)
821 return 0;
822 if (len - 1 > U64_MAX - ofs)
823 return -ERANGE;
824
825 start_seg = ofs >> header->obj_order;
826 end_seg = (ofs + len - 1) >> header->obj_order;
827
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700828 return end_seg - start_seg + 1;
829}
830
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700831/*
Josh Durgin029bcbd2011-07-22 11:35:23 -0700832 * returns the size of an object in the image
833 */
834static u64 rbd_obj_bytes(struct rbd_image_header *header)
835{
836 return 1 << header->obj_order;
837}
838
839/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700840 * bio helpers
841 */
842
843static void bio_chain_put(struct bio *chain)
844{
845 struct bio *tmp;
846
847 while (chain) {
848 tmp = chain;
849 chain = chain->bi_next;
850 bio_put(tmp);
851 }
852}
853
854/*
855 * zeros a bio chain, starting at specific offset
856 */
857static void zero_bio_chain(struct bio *chain, int start_ofs)
858{
859 struct bio_vec *bv;
860 unsigned long flags;
861 void *buf;
862 int i;
863 int pos = 0;
864
865 while (chain) {
866 bio_for_each_segment(bv, chain, i) {
867 if (pos + bv->bv_len > start_ofs) {
868 int remainder = max(start_ofs - pos, 0);
869 buf = bvec_kmap_irq(bv, &flags);
870 memset(buf + remainder, 0,
871 bv->bv_len - remainder);
Dan Carpenter85b5aaa2010-10-11 21:15:11 +0200872 bvec_kunmap_irq(buf, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700873 }
874 pos += bv->bv_len;
875 }
876
877 chain = chain->bi_next;
878 }
879}
880
881/*
Alex Elderf7760da2012-10-20 22:17:27 -0500882 * Clone a portion of a bio, starting at the given byte offset
883 * and continuing for the number of bytes indicated.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700884 */
Alex Elderf7760da2012-10-20 22:17:27 -0500885static struct bio *bio_clone_range(struct bio *bio_src,
886 unsigned int offset,
887 unsigned int len,
888 gfp_t gfpmask)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700889{
Alex Elderf7760da2012-10-20 22:17:27 -0500890 struct bio_vec *bv;
891 unsigned int resid;
892 unsigned short idx;
893 unsigned int voff;
894 unsigned short end_idx;
895 unsigned short vcnt;
896 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700897
Alex Elderf7760da2012-10-20 22:17:27 -0500898 /* Handle the easy case for the caller */
899
900 if (!offset && len == bio_src->bi_size)
901 return bio_clone(bio_src, gfpmask);
902
903 if (WARN_ON_ONCE(!len))
904 return NULL;
905 if (WARN_ON_ONCE(len > bio_src->bi_size))
906 return NULL;
907 if (WARN_ON_ONCE(offset > bio_src->bi_size - len))
908 return NULL;
909
910 /* Find first affected segment... */
911
912 resid = offset;
913 __bio_for_each_segment(bv, bio_src, idx, 0) {
914 if (resid < bv->bv_len)
915 break;
916 resid -= bv->bv_len;
917 }
918 voff = resid;
919
920 /* ...and the last affected segment */
921
922 resid += len;
923 __bio_for_each_segment(bv, bio_src, end_idx, idx) {
924 if (resid <= bv->bv_len)
925 break;
926 resid -= bv->bv_len;
927 }
928 vcnt = end_idx - idx + 1;
929
930 /* Build the clone */
931
932 bio = bio_alloc(gfpmask, (unsigned int) vcnt);
933 if (!bio)
934 return NULL; /* ENOMEM */
935
936 bio->bi_bdev = bio_src->bi_bdev;
937 bio->bi_sector = bio_src->bi_sector + (offset >> SECTOR_SHIFT);
938 bio->bi_rw = bio_src->bi_rw;
939 bio->bi_flags |= 1 << BIO_CLONED;
940
941 /*
942 * Copy over our part of the bio_vec, then update the first
943 * and last (or only) entries.
944 */
945 memcpy(&bio->bi_io_vec[0], &bio_src->bi_io_vec[idx],
946 vcnt * sizeof (struct bio_vec));
947 bio->bi_io_vec[0].bv_offset += voff;
948 if (vcnt > 1) {
949 bio->bi_io_vec[0].bv_len -= voff;
950 bio->bi_io_vec[vcnt - 1].bv_len = resid;
951 } else {
952 bio->bi_io_vec[0].bv_len = len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700953 }
954
Alex Elderf7760da2012-10-20 22:17:27 -0500955 bio->bi_vcnt = vcnt;
956 bio->bi_size = len;
957 bio->bi_idx = 0;
Alex Elder542582f2012-08-09 10:33:25 -0700958
Alex Elderf7760da2012-10-20 22:17:27 -0500959 return bio;
960}
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700961
Alex Elderf7760da2012-10-20 22:17:27 -0500962/*
963 * Clone a portion of a bio chain, starting at the given byte offset
964 * into the first bio in the source chain and continuing for the
965 * number of bytes indicated. The result is another bio chain of
966 * exactly the given length, or a null pointer on error.
967 *
968 * The bio_src and offset parameters are both in-out. On entry they
969 * refer to the first source bio and the offset into that bio where
970 * the start of data to be cloned is located.
971 *
972 * On return, bio_src is updated to refer to the bio in the source
973 * chain that contains first un-cloned byte, and *offset will
974 * contain the offset of that byte within that bio.
975 */
976static struct bio *bio_chain_clone_range(struct bio **bio_src,
977 unsigned int *offset,
978 unsigned int len,
979 gfp_t gfpmask)
980{
981 struct bio *bi = *bio_src;
982 unsigned int off = *offset;
983 struct bio *chain = NULL;
984 struct bio **end;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700985
Alex Elderf7760da2012-10-20 22:17:27 -0500986 /* Build up a chain of clone bios up to the limit */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700987
Alex Elderf7760da2012-10-20 22:17:27 -0500988 if (!bi || off >= bi->bi_size || !len)
989 return NULL; /* Nothing to clone */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700990
Alex Elderf7760da2012-10-20 22:17:27 -0500991 end = &chain;
992 while (len) {
993 unsigned int bi_size;
994 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700995
Alex Elderf5400b72012-11-01 10:17:15 -0500996 if (!bi) {
997 rbd_warn(NULL, "bio_chain exhausted with %u left", len);
Alex Elderf7760da2012-10-20 22:17:27 -0500998 goto out_err; /* EINVAL; ran out of bio's */
Alex Elderf5400b72012-11-01 10:17:15 -0500999 }
Alex Elderf7760da2012-10-20 22:17:27 -05001000 bi_size = min_t(unsigned int, bi->bi_size - off, len);
1001 bio = bio_clone_range(bi, off, bi_size, gfpmask);
1002 if (!bio)
1003 goto out_err; /* ENOMEM */
1004
1005 *end = bio;
1006 end = &bio->bi_next;
1007
1008 off += bi_size;
1009 if (off == bi->bi_size) {
1010 bi = bi->bi_next;
1011 off = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001012 }
Alex Elderf7760da2012-10-20 22:17:27 -05001013 len -= bi_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001014 }
Alex Elderf7760da2012-10-20 22:17:27 -05001015 *bio_src = bi;
1016 *offset = off;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001017
Alex Elderf7760da2012-10-20 22:17:27 -05001018 return chain;
1019out_err:
1020 bio_chain_put(chain);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001021
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001022 return NULL;
1023}
1024
1025/*
1026 * helpers for osd request op vectors.
1027 */
Alex Elder57cfc102012-06-26 12:57:03 -07001028static struct ceph_osd_req_op *rbd_create_rw_ops(int num_ops,
1029 int opcode, u32 payload_len)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001030{
Alex Elder57cfc102012-06-26 12:57:03 -07001031 struct ceph_osd_req_op *ops;
1032
1033 ops = kzalloc(sizeof (*ops) * (num_ops + 1), GFP_NOIO);
1034 if (!ops)
1035 return NULL;
1036
1037 ops[0].op = opcode;
1038
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001039 /*
1040 * op extent offset and length will be set later on
1041 * in calc_raw_layout()
1042 */
Alex Elder57cfc102012-06-26 12:57:03 -07001043 ops[0].payload_len = payload_len;
1044
1045 return ops;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001046}
1047
1048static void rbd_destroy_ops(struct ceph_osd_req_op *ops)
1049{
1050 kfree(ops);
1051}
1052
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001053static void rbd_coll_end_req_index(struct request *rq,
1054 struct rbd_req_coll *coll,
1055 int index,
1056 int ret, u64 len)
1057{
1058 struct request_queue *q;
1059 int min, max, i;
1060
Alex Elderbd919d42012-07-13 20:35:11 -05001061 dout("rbd_coll_end_req_index %p index %d ret %d len %llu\n",
1062 coll, index, ret, (unsigned long long) len);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001063
1064 if (!rq)
1065 return;
1066
1067 if (!coll) {
1068 blk_end_request(rq, ret, len);
1069 return;
1070 }
1071
1072 q = rq->q;
1073
1074 spin_lock_irq(q->queue_lock);
1075 coll->status[index].done = 1;
1076 coll->status[index].rc = ret;
1077 coll->status[index].bytes = len;
1078 max = min = coll->num_done;
1079 while (max < coll->total && coll->status[max].done)
1080 max++;
1081
1082 for (i = min; i<max; i++) {
1083 __blk_end_request(rq, coll->status[i].rc,
1084 coll->status[i].bytes);
1085 coll->num_done++;
1086 kref_put(&coll->kref, rbd_coll_release);
1087 }
1088 spin_unlock_irq(q->queue_lock);
1089}
1090
Alex Elder725afc92012-11-08 08:01:39 -06001091static void rbd_coll_end_req(struct rbd_request *rbd_req,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001092 int ret, u64 len)
1093{
Alex Elder725afc92012-11-08 08:01:39 -06001094 rbd_coll_end_req_index(rbd_req->rq,
1095 rbd_req->coll, rbd_req->coll_index,
1096 ret, len);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001097}
1098
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001099/*
1100 * Send ceph osd request
1101 */
1102static int rbd_do_request(struct request *rq,
Alex Elder0ce1a792012-07-03 16:01:18 -05001103 struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001104 struct ceph_snap_context *snapc,
1105 u64 snapid,
Alex Elderaded07e2012-07-03 16:01:18 -05001106 const char *object_name, u64 ofs, u64 len,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001107 struct bio *bio,
1108 struct page **pages,
1109 int num_pages,
1110 int flags,
1111 struct ceph_osd_req_op *ops,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001112 struct rbd_req_coll *coll,
1113 int coll_index,
Alex Elder5f29ddd2012-11-08 08:01:39 -06001114 void (*rbd_cb)(struct ceph_osd_request *,
1115 struct ceph_msg *),
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001116 struct ceph_osd_request **linger_req,
1117 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001118{
Alex Elder5f29ddd2012-11-08 08:01:39 -06001119 struct ceph_osd_request *osd_req;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001120 struct ceph_file_layout *layout;
1121 int ret;
1122 u64 bno;
1123 struct timespec mtime = CURRENT_TIME;
Alex Elder725afc92012-11-08 08:01:39 -06001124 struct rbd_request *rbd_req;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001125 struct ceph_osd_request_head *reqhead;
Alex Elder1dbb4392012-01-24 10:08:37 -06001126 struct ceph_osd_client *osdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001127
Alex Elder725afc92012-11-08 08:01:39 -06001128 rbd_req = kzalloc(sizeof(*rbd_req), GFP_NOIO);
1129 if (!rbd_req) {
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001130 if (coll)
1131 rbd_coll_end_req_index(rq, coll, coll_index,
1132 -ENOMEM, len);
1133 return -ENOMEM;
1134 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001135
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001136 if (coll) {
Alex Elder725afc92012-11-08 08:01:39 -06001137 rbd_req->coll = coll;
1138 rbd_req->coll_index = coll_index;
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001139 }
1140
Alex Elderf7760da2012-10-20 22:17:27 -05001141 dout("rbd_do_request object_name=%s ofs=%llu len=%llu coll=%p[%d]\n",
1142 object_name, (unsigned long long) ofs,
1143 (unsigned long long) len, coll, coll_index);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001144
Alex Elder0ce1a792012-07-03 16:01:18 -05001145 osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder5f29ddd2012-11-08 08:01:39 -06001146 osd_req = ceph_osdc_alloc_request(osdc, flags, snapc, ops,
Alex Elder1dbb4392012-01-24 10:08:37 -06001147 false, GFP_NOIO, pages, bio);
Alex Elder5f29ddd2012-11-08 08:01:39 -06001148 if (!osd_req) {
Sage Weil4ad12622011-05-03 09:23:36 -07001149 ret = -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001150 goto done_pages;
1151 }
1152
Alex Elder5f29ddd2012-11-08 08:01:39 -06001153 osd_req->r_callback = rbd_cb;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001154
Alex Elder725afc92012-11-08 08:01:39 -06001155 rbd_req->rq = rq;
1156 rbd_req->bio = bio;
1157 rbd_req->pages = pages;
1158 rbd_req->len = len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001159
Alex Elder5f29ddd2012-11-08 08:01:39 -06001160 osd_req->r_priv = rbd_req;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001161
Alex Elder5f29ddd2012-11-08 08:01:39 -06001162 reqhead = osd_req->r_request->front.iov_base;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001163 reqhead->snapid = cpu_to_le64(CEPH_NOSNAP);
1164
Alex Elder5f29ddd2012-11-08 08:01:39 -06001165 strncpy(osd_req->r_oid, object_name, sizeof(osd_req->r_oid));
1166 osd_req->r_oid_len = strlen(osd_req->r_oid);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001167
Alex Elder5f29ddd2012-11-08 08:01:39 -06001168 layout = &osd_req->r_file_layout;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001169 memset(layout, 0, sizeof(*layout));
1170 layout->fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
1171 layout->fl_stripe_count = cpu_to_le32(1);
1172 layout->fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
Alex Elder0d7dbfc2012-10-25 23:34:41 -05001173 layout->fl_pg_pool = cpu_to_le32((int) rbd_dev->spec->pool_id);
Sage Weil6cae3712012-09-24 21:02:47 -07001174 ret = ceph_calc_raw_layout(osdc, layout, snapid, ofs, &len, &bno,
Alex Elder5f29ddd2012-11-08 08:01:39 -06001175 osd_req, ops);
Sage Weil6cae3712012-09-24 21:02:47 -07001176 rbd_assert(ret == 0);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001177
Alex Elder5f29ddd2012-11-08 08:01:39 -06001178 ceph_osdc_build_request(osd_req, ofs, &len,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001179 ops,
1180 snapc,
1181 &mtime,
Alex Elder5f29ddd2012-11-08 08:01:39 -06001182 osd_req->r_oid, osd_req->r_oid_len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001183
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001184 if (linger_req) {
Alex Elder5f29ddd2012-11-08 08:01:39 -06001185 ceph_osdc_set_request_linger(osdc, osd_req);
1186 *linger_req = osd_req;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001187 }
1188
Alex Elder5f29ddd2012-11-08 08:01:39 -06001189 ret = ceph_osdc_start_request(osdc, osd_req, false);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001190 if (ret < 0)
1191 goto done_err;
1192
1193 if (!rbd_cb) {
Alex Elder5f29ddd2012-11-08 08:01:39 -06001194 u64 version;
1195
1196 ret = ceph_osdc_wait_request(osdc, osd_req);
1197 version = le64_to_cpu(osd_req->r_reassert_version.version);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001198 if (ver)
Alex Elder5f29ddd2012-11-08 08:01:39 -06001199 *ver = version;
1200 dout("reassert_ver=%llu\n", (unsigned long long) version);
1201 ceph_osdc_put_request(osd_req);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001202 }
1203 return ret;
1204
1205done_err:
Alex Elder725afc92012-11-08 08:01:39 -06001206 bio_chain_put(rbd_req->bio);
Alex Elder5f29ddd2012-11-08 08:01:39 -06001207 ceph_osdc_put_request(osd_req);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001208done_pages:
Alex Elder725afc92012-11-08 08:01:39 -06001209 rbd_coll_end_req(rbd_req, ret, len);
1210 kfree(rbd_req);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001211 return ret;
1212}
1213
1214/*
1215 * Ceph osd op callback
1216 */
Alex Elder5f29ddd2012-11-08 08:01:39 -06001217static void rbd_req_cb(struct ceph_osd_request *osd_req, struct ceph_msg *msg)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001218{
Alex Elder5f29ddd2012-11-08 08:01:39 -06001219 struct rbd_request *rbd_req = osd_req->r_priv;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001220 struct ceph_osd_reply_head *replyhead;
1221 struct ceph_osd_op *op;
1222 __s32 rc;
1223 u64 bytes;
1224 int read_op;
1225
1226 /* parse reply */
1227 replyhead = msg->front.iov_base;
1228 WARN_ON(le32_to_cpu(replyhead->num_ops) == 0);
1229 op = (void *)(replyhead + 1);
1230 rc = le32_to_cpu(replyhead->result);
1231 bytes = le64_to_cpu(op->extent.length);
Dan Carpenter895cfcc2012-06-06 09:15:33 -05001232 read_op = (le16_to_cpu(op->op) == CEPH_OSD_OP_READ);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001233
Alex Elderbd919d42012-07-13 20:35:11 -05001234 dout("rbd_req_cb bytes=%llu readop=%d rc=%d\n",
1235 (unsigned long long) bytes, read_op, (int) rc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001236
1237 if (rc == -ENOENT && read_op) {
Alex Elder725afc92012-11-08 08:01:39 -06001238 zero_bio_chain(rbd_req->bio, 0);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001239 rc = 0;
Alex Elder725afc92012-11-08 08:01:39 -06001240 } else if (rc == 0 && read_op && bytes < rbd_req->len) {
1241 zero_bio_chain(rbd_req->bio, bytes);
1242 bytes = rbd_req->len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001243 }
1244
Alex Elder725afc92012-11-08 08:01:39 -06001245 rbd_coll_end_req(rbd_req, rc, bytes);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001246
Alex Elder725afc92012-11-08 08:01:39 -06001247 if (rbd_req->bio)
1248 bio_chain_put(rbd_req->bio);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001249
Alex Elder5f29ddd2012-11-08 08:01:39 -06001250 ceph_osdc_put_request(osd_req);
Alex Elder725afc92012-11-08 08:01:39 -06001251 kfree(rbd_req);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001252}
1253
Alex Elder5f29ddd2012-11-08 08:01:39 -06001254static void rbd_simple_req_cb(struct ceph_osd_request *osd_req,
1255 struct ceph_msg *msg)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001256{
Alex Elder5f29ddd2012-11-08 08:01:39 -06001257 ceph_osdc_put_request(osd_req);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001258}
1259
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001260/*
1261 * Do a synchronous ceph osd operation
1262 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001263static int rbd_req_sync_op(struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001264 struct ceph_snap_context *snapc,
1265 u64 snapid,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001266 int flags,
Alex Elder913d2fd2012-06-26 12:57:03 -07001267 struct ceph_osd_req_op *ops,
Alex Elderaded07e2012-07-03 16:01:18 -05001268 const char *object_name,
Alex Elderf8d4de62012-07-03 16:01:19 -05001269 u64 ofs, u64 inbound_size,
1270 char *inbound,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001271 struct ceph_osd_request **linger_req,
1272 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001273{
1274 int ret;
1275 struct page **pages;
1276 int num_pages;
Alex Elder913d2fd2012-06-26 12:57:03 -07001277
Alex Elderaafb2302012-09-06 16:00:54 -05001278 rbd_assert(ops != NULL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001279
Alex Elderf8d4de62012-07-03 16:01:19 -05001280 num_pages = calc_pages_for(ofs, inbound_size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001281 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
Dan Carpenterb8d06382010-10-11 21:14:23 +02001282 if (IS_ERR(pages))
1283 return PTR_ERR(pages);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001284
Alex Elder0ce1a792012-07-03 16:01:18 -05001285 ret = rbd_do_request(NULL, rbd_dev, snapc, snapid,
Alex Elderf8d4de62012-07-03 16:01:19 -05001286 object_name, ofs, inbound_size, NULL,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001287 pages, num_pages,
1288 flags,
1289 ops,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001290 NULL, 0,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001291 NULL,
1292 linger_req, ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001293 if (ret < 0)
Alex Elder913d2fd2012-06-26 12:57:03 -07001294 goto done;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001295
Alex Elderf8d4de62012-07-03 16:01:19 -05001296 if ((flags & CEPH_OSD_FLAG_READ) && inbound)
1297 ret = ceph_copy_from_page_vector(pages, inbound, ofs, ret);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001298
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001299done:
1300 ceph_release_page_vector(pages, num_pages);
1301 return ret;
1302}
1303
1304/*
1305 * Do an asynchronous ceph osd operation
1306 */
1307static int rbd_do_op(struct request *rq,
Alex Elder0ce1a792012-07-03 16:01:18 -05001308 struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001309 struct ceph_snap_context *snapc,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001310 u64 ofs, u64 len,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001311 struct bio *bio,
1312 struct rbd_req_coll *coll,
1313 int coll_index)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001314{
1315 char *seg_name;
1316 u64 seg_ofs;
1317 u64 seg_len;
1318 int ret;
1319 struct ceph_osd_req_op *ops;
1320 u32 payload_len;
Alex Elderff2e4bb2012-10-10 18:59:29 -07001321 int opcode;
1322 int flags;
Alex Elder46342462012-10-10 18:59:29 -07001323 u64 snapid;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001324
Alex Elder65ccfe22012-08-09 10:33:26 -07001325 seg_name = rbd_segment_name(rbd_dev, ofs);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001326 if (!seg_name)
1327 return -ENOMEM;
Alex Elder65ccfe22012-08-09 10:33:26 -07001328 seg_len = rbd_segment_length(rbd_dev, ofs, len);
1329 seg_ofs = rbd_segment_offset(rbd_dev, ofs);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001330
Alex Elderff2e4bb2012-10-10 18:59:29 -07001331 if (rq_data_dir(rq) == WRITE) {
1332 opcode = CEPH_OSD_OP_WRITE;
1333 flags = CEPH_OSD_FLAG_WRITE|CEPH_OSD_FLAG_ONDISK;
Alex Elder46342462012-10-10 18:59:29 -07001334 snapid = CEPH_NOSNAP;
Alex Elderff2e4bb2012-10-10 18:59:29 -07001335 payload_len = seg_len;
1336 } else {
1337 opcode = CEPH_OSD_OP_READ;
1338 flags = CEPH_OSD_FLAG_READ;
Alex Elder46342462012-10-10 18:59:29 -07001339 snapc = NULL;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05001340 snapid = rbd_dev->spec->snap_id;
Alex Elderff2e4bb2012-10-10 18:59:29 -07001341 payload_len = 0;
1342 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001343
Alex Elder57cfc102012-06-26 12:57:03 -07001344 ret = -ENOMEM;
1345 ops = rbd_create_rw_ops(1, opcode, payload_len);
1346 if (!ops)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001347 goto done;
1348
1349 /* we've taken care of segment sizes earlier when we
1350 cloned the bios. We should never have a segment
1351 truncated at this point */
Alex Elderaafb2302012-09-06 16:00:54 -05001352 rbd_assert(seg_len == len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001353
1354 ret = rbd_do_request(rq, rbd_dev, snapc, snapid,
1355 seg_name, seg_ofs, seg_len,
1356 bio,
1357 NULL, 0,
1358 flags,
1359 ops,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001360 coll, coll_index,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001361 rbd_req_cb, 0, NULL);
Sage Weil11f77002011-05-12 16:13:54 -07001362
1363 rbd_destroy_ops(ops);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001364done:
1365 kfree(seg_name);
1366 return ret;
1367}
1368
1369/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001370 * Request sync osd read
1371 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001372static int rbd_req_sync_read(struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001373 u64 snapid,
Alex Elderaded07e2012-07-03 16:01:18 -05001374 const char *object_name,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001375 u64 ofs, u64 len,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001376 char *buf,
1377 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001378{
Alex Elder913d2fd2012-06-26 12:57:03 -07001379 struct ceph_osd_req_op *ops;
1380 int ret;
1381
1382 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_READ, 0);
1383 if (!ops)
1384 return -ENOMEM;
1385
1386 ret = rbd_req_sync_op(rbd_dev, NULL,
Josh Durginb06e6a62011-11-21 18:16:52 -08001387 snapid,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001388 CEPH_OSD_FLAG_READ,
Alex Elder913d2fd2012-06-26 12:57:03 -07001389 ops, object_name, ofs, len, buf, NULL, ver);
1390 rbd_destroy_ops(ops);
1391
1392 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001393}
1394
1395/*
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001396 * Request sync osd watch
1397 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001398static int rbd_req_sync_notify_ack(struct rbd_device *rbd_dev,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001399 u64 ver,
Alex Elder7f0a24d2012-07-25 09:32:40 -05001400 u64 notify_id)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001401{
1402 struct ceph_osd_req_op *ops;
Sage Weil11f77002011-05-12 16:13:54 -07001403 int ret;
1404
Alex Elder57cfc102012-06-26 12:57:03 -07001405 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_NOTIFY_ACK, 0);
1406 if (!ops)
1407 return -ENOMEM;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001408
Josh Durgina71b8912011-12-05 18:10:44 -08001409 ops[0].watch.ver = cpu_to_le64(ver);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001410 ops[0].watch.cookie = notify_id;
1411 ops[0].watch.flag = 0;
1412
Alex Elder0ce1a792012-07-03 16:01:18 -05001413 ret = rbd_do_request(NULL, rbd_dev, NULL, CEPH_NOSNAP,
Alex Elder7f0a24d2012-07-25 09:32:40 -05001414 rbd_dev->header_name, 0, 0, NULL,
Alex Elderad4f2322012-07-03 16:01:19 -05001415 NULL, 0,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001416 CEPH_OSD_FLAG_READ,
1417 ops,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001418 NULL, 0,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001419 rbd_simple_req_cb, 0, NULL);
1420
1421 rbd_destroy_ops(ops);
1422 return ret;
1423}
1424
1425static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
1426{
Alex Elder0ce1a792012-07-03 16:01:18 -05001427 struct rbd_device *rbd_dev = (struct rbd_device *)data;
Josh Durgina71b8912011-12-05 18:10:44 -08001428 u64 hver;
Sage Weil13143d22011-05-12 16:08:30 -07001429 int rc;
1430
Alex Elder0ce1a792012-07-03 16:01:18 -05001431 if (!rbd_dev)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001432 return;
1433
Alex Elderbd919d42012-07-13 20:35:11 -05001434 dout("rbd_watch_cb %s notify_id=%llu opcode=%u\n",
1435 rbd_dev->header_name, (unsigned long long) notify_id,
1436 (unsigned int) opcode);
Alex Elder117973f2012-08-31 17:29:55 -05001437 rc = rbd_dev_refresh(rbd_dev, &hver);
Sage Weil13143d22011-05-12 16:08:30 -07001438 if (rc)
Alex Elder06ecc6c2012-11-01 10:17:15 -05001439 rbd_warn(rbd_dev, "got notification but failed to "
1440 " update snaps: %d\n", rc);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001441
Alex Elder7f0a24d2012-07-25 09:32:40 -05001442 rbd_req_sync_notify_ack(rbd_dev, hver, notify_id);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001443}
1444
1445/*
1446 * Request sync osd watch
1447 */
Alex Elder0e6f3222012-07-25 09:32:40 -05001448static int rbd_req_sync_watch(struct rbd_device *rbd_dev)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001449{
1450 struct ceph_osd_req_op *ops;
Alex Elder0ce1a792012-07-03 16:01:18 -05001451 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder57cfc102012-06-26 12:57:03 -07001452 int ret;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001453
Alex Elder57cfc102012-06-26 12:57:03 -07001454 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_WATCH, 0);
1455 if (!ops)
1456 return -ENOMEM;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001457
1458 ret = ceph_osdc_create_event(osdc, rbd_watch_cb, 0,
Alex Elder0ce1a792012-07-03 16:01:18 -05001459 (void *)rbd_dev, &rbd_dev->watch_event);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001460 if (ret < 0)
1461 goto fail;
1462
Alex Elder0e6f3222012-07-25 09:32:40 -05001463 ops[0].watch.ver = cpu_to_le64(rbd_dev->header.obj_version);
Alex Elder0ce1a792012-07-03 16:01:18 -05001464 ops[0].watch.cookie = cpu_to_le64(rbd_dev->watch_event->cookie);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001465 ops[0].watch.flag = 1;
1466
Alex Elder0ce1a792012-07-03 16:01:18 -05001467 ret = rbd_req_sync_op(rbd_dev, NULL,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001468 CEPH_NOSNAP,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001469 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1470 ops,
Alex Elder0e6f3222012-07-25 09:32:40 -05001471 rbd_dev->header_name,
1472 0, 0, NULL,
Alex Elder0ce1a792012-07-03 16:01:18 -05001473 &rbd_dev->watch_request, NULL);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001474
1475 if (ret < 0)
1476 goto fail_event;
1477
1478 rbd_destroy_ops(ops);
1479 return 0;
1480
1481fail_event:
Alex Elder0ce1a792012-07-03 16:01:18 -05001482 ceph_osdc_cancel_event(rbd_dev->watch_event);
1483 rbd_dev->watch_event = NULL;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001484fail:
1485 rbd_destroy_ops(ops);
1486 return ret;
1487}
1488
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001489/*
1490 * Request sync osd unwatch
1491 */
Alex Elder070c6332012-07-25 09:32:41 -05001492static int rbd_req_sync_unwatch(struct rbd_device *rbd_dev)
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001493{
1494 struct ceph_osd_req_op *ops;
Alex Elder57cfc102012-06-26 12:57:03 -07001495 int ret;
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001496
Alex Elder57cfc102012-06-26 12:57:03 -07001497 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_WATCH, 0);
1498 if (!ops)
1499 return -ENOMEM;
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001500
1501 ops[0].watch.ver = 0;
Alex Elder0ce1a792012-07-03 16:01:18 -05001502 ops[0].watch.cookie = cpu_to_le64(rbd_dev->watch_event->cookie);
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001503 ops[0].watch.flag = 0;
1504
Alex Elder0ce1a792012-07-03 16:01:18 -05001505 ret = rbd_req_sync_op(rbd_dev, NULL,
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001506 CEPH_NOSNAP,
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001507 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
1508 ops,
Alex Elder070c6332012-07-25 09:32:41 -05001509 rbd_dev->header_name,
1510 0, 0, NULL, NULL, NULL);
1511
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001512
1513 rbd_destroy_ops(ops);
Alex Elder0ce1a792012-07-03 16:01:18 -05001514 ceph_osdc_cancel_event(rbd_dev->watch_event);
1515 rbd_dev->watch_event = NULL;
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001516 return ret;
1517}
1518
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001519/*
Alex Elder3cb4a682012-06-26 12:57:03 -07001520 * Synchronous osd object method call
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001521 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001522static int rbd_req_sync_exec(struct rbd_device *rbd_dev,
Alex Elderaded07e2012-07-03 16:01:18 -05001523 const char *object_name,
1524 const char *class_name,
1525 const char *method_name,
Alex Elder3cb4a682012-06-26 12:57:03 -07001526 const char *outbound,
1527 size_t outbound_size,
Alex Elderf8d4de62012-07-03 16:01:19 -05001528 char *inbound,
1529 size_t inbound_size,
Alex Elder3cb4a682012-06-26 12:57:03 -07001530 int flags,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001531 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001532{
1533 struct ceph_osd_req_op *ops;
Alex Elderaded07e2012-07-03 16:01:18 -05001534 int class_name_len = strlen(class_name);
1535 int method_name_len = strlen(method_name);
Alex Elder3cb4a682012-06-26 12:57:03 -07001536 int payload_size;
Alex Elder57cfc102012-06-26 12:57:03 -07001537 int ret;
1538
Alex Elder3cb4a682012-06-26 12:57:03 -07001539 /*
1540 * Any input parameters required by the method we're calling
1541 * will be sent along with the class and method names as
1542 * part of the message payload. That data and its size are
1543 * supplied via the indata and indata_len fields (named from
1544 * the perspective of the server side) in the OSD request
1545 * operation.
1546 */
1547 payload_size = class_name_len + method_name_len + outbound_size;
1548 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_CALL, payload_size);
Alex Elder57cfc102012-06-26 12:57:03 -07001549 if (!ops)
1550 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001551
Alex Elderaded07e2012-07-03 16:01:18 -05001552 ops[0].cls.class_name = class_name;
1553 ops[0].cls.class_len = (__u8) class_name_len;
1554 ops[0].cls.method_name = method_name;
1555 ops[0].cls.method_len = (__u8) method_name_len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001556 ops[0].cls.argc = 0;
Alex Elder3cb4a682012-06-26 12:57:03 -07001557 ops[0].cls.indata = outbound;
1558 ops[0].cls.indata_len = outbound_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001559
Alex Elder0ce1a792012-07-03 16:01:18 -05001560 ret = rbd_req_sync_op(rbd_dev, NULL,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001561 CEPH_NOSNAP,
Alex Elder3cb4a682012-06-26 12:57:03 -07001562 flags, ops,
Alex Elderf8d4de62012-07-03 16:01:19 -05001563 object_name, 0, inbound_size, inbound,
1564 NULL, ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001565
1566 rbd_destroy_ops(ops);
1567
1568 dout("cls_exec returned %d\n", ret);
1569 return ret;
1570}
1571
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001572static struct rbd_req_coll *rbd_alloc_coll(int num_reqs)
1573{
1574 struct rbd_req_coll *coll =
1575 kzalloc(sizeof(struct rbd_req_coll) +
1576 sizeof(struct rbd_req_status) * num_reqs,
1577 GFP_ATOMIC);
1578
1579 if (!coll)
1580 return NULL;
1581 coll->total = num_reqs;
1582 kref_init(&coll->kref);
1583 return coll;
1584}
1585
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001586/*
1587 * block device queue callback
1588 */
1589static void rbd_rq_fn(struct request_queue *q)
1590{
1591 struct rbd_device *rbd_dev = q->queuedata;
1592 struct request *rq;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001593
Alex Elder00f1f362012-02-07 12:03:36 -06001594 while ((rq = blk_fetch_request(q))) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001595 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001596 bool do_write;
Alex Elderbd919d42012-07-13 20:35:11 -05001597 unsigned int size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001598 u64 ofs;
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001599 int num_segs, cur_seg = 0;
1600 struct rbd_req_coll *coll;
Josh Durgind1d25642011-12-05 14:03:05 -08001601 struct ceph_snap_context *snapc;
Alex Elderf7760da2012-10-20 22:17:27 -05001602 unsigned int bio_offset;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001603
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001604 dout("fetched request\n");
1605
1606 /* filter out block requests we don't understand */
1607 if ((rq->cmd_type != REQ_TYPE_FS)) {
1608 __blk_end_request_all(rq, 0);
Alex Elder00f1f362012-02-07 12:03:36 -06001609 continue;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001610 }
1611
1612 /* deduce our operation (read, write) */
1613 do_write = (rq_data_dir(rq) == WRITE);
Alex Elderf84344f2012-08-31 17:29:51 -05001614 if (do_write && rbd_dev->mapping.read_only) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001615 __blk_end_request_all(rq, -EROFS);
Alex Elder00f1f362012-02-07 12:03:36 -06001616 continue;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001617 }
1618
1619 spin_unlock_irq(q->queue_lock);
1620
Josh Durgind1d25642011-12-05 14:03:05 -08001621 down_read(&rbd_dev->header_rwsem);
Josh Durgine88a36e2011-11-21 18:14:25 -08001622
Alex Elderdaba5fd2012-10-26 17:25:23 -05001623 if (!rbd_dev->exists) {
Alex Elder0d7dbfc2012-10-25 23:34:41 -05001624 rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP);
Josh Durgine88a36e2011-11-21 18:14:25 -08001625 up_read(&rbd_dev->header_rwsem);
Josh Durgind1d25642011-12-05 14:03:05 -08001626 dout("request for non-existent snapshot");
1627 spin_lock_irq(q->queue_lock);
1628 __blk_end_request_all(rq, -ENXIO);
1629 continue;
Josh Durgine88a36e2011-11-21 18:14:25 -08001630 }
1631
Josh Durgind1d25642011-12-05 14:03:05 -08001632 snapc = ceph_get_snap_context(rbd_dev->header.snapc);
1633
1634 up_read(&rbd_dev->header_rwsem);
1635
Alex Elderf7760da2012-10-20 22:17:27 -05001636 size = blk_rq_bytes(rq);
1637 ofs = blk_rq_pos(rq) * SECTOR_SIZE;
1638 bio = rq->bio;
1639
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001640 dout("%s 0x%x bytes at 0x%llx\n",
1641 do_write ? "write" : "read",
Alex Elderbd919d42012-07-13 20:35:11 -05001642 size, (unsigned long long) blk_rq_pos(rq) * SECTOR_SIZE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001643
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001644 num_segs = rbd_get_num_segments(&rbd_dev->header, ofs, size);
Alex Elderdf111be2012-08-09 10:33:26 -07001645 if (num_segs <= 0) {
1646 spin_lock_irq(q->queue_lock);
1647 __blk_end_request_all(rq, num_segs);
1648 ceph_put_snap_context(snapc);
1649 continue;
1650 }
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001651 coll = rbd_alloc_coll(num_segs);
1652 if (!coll) {
1653 spin_lock_irq(q->queue_lock);
1654 __blk_end_request_all(rq, -ENOMEM);
Josh Durgind1d25642011-12-05 14:03:05 -08001655 ceph_put_snap_context(snapc);
Alex Elder00f1f362012-02-07 12:03:36 -06001656 continue;
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001657 }
1658
Alex Elderf7760da2012-10-20 22:17:27 -05001659 bio_offset = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001660 do {
Alex Elderf7760da2012-10-20 22:17:27 -05001661 u64 limit = rbd_segment_length(rbd_dev, ofs, size);
1662 unsigned int chain_size;
1663 struct bio *bio_chain;
1664
1665 BUG_ON(limit > (u64) UINT_MAX);
1666 chain_size = (unsigned int) limit;
Alex Elderbd919d42012-07-13 20:35:11 -05001667 dout("rq->bio->bi_vcnt=%hu\n", rq->bio->bi_vcnt);
Alex Elderf7760da2012-10-20 22:17:27 -05001668
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001669 kref_get(&coll->kref);
Alex Elderf7760da2012-10-20 22:17:27 -05001670
1671 /* Pass a cloned bio chain via an osd request */
1672
1673 bio_chain = bio_chain_clone_range(&bio,
1674 &bio_offset, chain_size,
1675 GFP_ATOMIC);
1676 if (bio_chain)
Alex Elder46342462012-10-10 18:59:29 -07001677 (void) rbd_do_op(rq, rbd_dev, snapc,
Alex Elderf7760da2012-10-20 22:17:27 -05001678 ofs, chain_size,
1679 bio_chain, coll, cur_seg);
Alex Elder46342462012-10-10 18:59:29 -07001680 else
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001681 rbd_coll_end_req_index(rq, coll, cur_seg,
Alex Elderf7760da2012-10-20 22:17:27 -05001682 -ENOMEM, chain_size);
1683 size -= chain_size;
1684 ofs += chain_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001685
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001686 cur_seg++;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001687 } while (size > 0);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001688 kref_put(&coll->kref, rbd_coll_release);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001689
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001690 spin_lock_irq(q->queue_lock);
Josh Durgind1d25642011-12-05 14:03:05 -08001691
1692 ceph_put_snap_context(snapc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001693 }
1694}
1695
1696/*
1697 * a queue callback. Makes sure that we don't create a bio that spans across
1698 * multiple osd objects. One exception would be with a single page bios,
Alex Elderf7760da2012-10-20 22:17:27 -05001699 * which we handle later at bio_chain_clone_range()
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001700 */
1701static int rbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd,
1702 struct bio_vec *bvec)
1703{
1704 struct rbd_device *rbd_dev = q->queuedata;
Alex Eldere5cfeed22012-10-20 22:17:27 -05001705 sector_t sector_offset;
1706 sector_t sectors_per_obj;
1707 sector_t obj_sector_offset;
1708 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001709
Alex Eldere5cfeed22012-10-20 22:17:27 -05001710 /*
1711 * Find how far into its rbd object the partition-relative
1712 * bio start sector is to offset relative to the enclosing
1713 * device.
1714 */
1715 sector_offset = get_start_sect(bmd->bi_bdev) + bmd->bi_sector;
1716 sectors_per_obj = 1 << (rbd_dev->header.obj_order - SECTOR_SHIFT);
1717 obj_sector_offset = sector_offset & (sectors_per_obj - 1);
Alex Elder593a9e72012-02-07 12:03:37 -06001718
Alex Eldere5cfeed22012-10-20 22:17:27 -05001719 /*
1720 * Compute the number of bytes from that offset to the end
1721 * of the object. Account for what's already used by the bio.
1722 */
1723 ret = (int) (sectors_per_obj - obj_sector_offset) << SECTOR_SHIFT;
1724 if (ret > bmd->bi_size)
1725 ret -= bmd->bi_size;
1726 else
1727 ret = 0;
1728
1729 /*
1730 * Don't send back more than was asked for. And if the bio
1731 * was empty, let the whole thing through because: "Note
1732 * that a block device *must* allow a single page to be
1733 * added to an empty bio."
1734 */
1735 rbd_assert(bvec->bv_len <= PAGE_SIZE);
1736 if (ret > (int) bvec->bv_len || !bmd->bi_size)
1737 ret = (int) bvec->bv_len;
1738
1739 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001740}
1741
1742static void rbd_free_disk(struct rbd_device *rbd_dev)
1743{
1744 struct gendisk *disk = rbd_dev->disk;
1745
1746 if (!disk)
1747 return;
1748
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001749 if (disk->flags & GENHD_FL_UP)
1750 del_gendisk(disk);
1751 if (disk->queue)
1752 blk_cleanup_queue(disk->queue);
1753 put_disk(disk);
1754}
1755
1756/*
Alex Elder4156d992012-08-02 11:29:46 -05001757 * Read the complete header for the given rbd device.
1758 *
1759 * Returns a pointer to a dynamically-allocated buffer containing
1760 * the complete and validated header. Caller can pass the address
1761 * of a variable that will be filled in with the version of the
1762 * header object at the time it was read.
1763 *
1764 * Returns a pointer-coded errno if a failure occurs.
1765 */
1766static struct rbd_image_header_ondisk *
1767rbd_dev_v1_header_read(struct rbd_device *rbd_dev, u64 *version)
1768{
1769 struct rbd_image_header_ondisk *ondisk = NULL;
1770 u32 snap_count = 0;
1771 u64 names_size = 0;
1772 u32 want_count;
1773 int ret;
1774
1775 /*
1776 * The complete header will include an array of its 64-bit
1777 * snapshot ids, followed by the names of those snapshots as
1778 * a contiguous block of NUL-terminated strings. Note that
1779 * the number of snapshots could change by the time we read
1780 * it in, in which case we re-read it.
1781 */
1782 do {
1783 size_t size;
1784
1785 kfree(ondisk);
1786
1787 size = sizeof (*ondisk);
1788 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
1789 size += names_size;
1790 ondisk = kmalloc(size, GFP_KERNEL);
1791 if (!ondisk)
1792 return ERR_PTR(-ENOMEM);
1793
1794 ret = rbd_req_sync_read(rbd_dev, CEPH_NOSNAP,
1795 rbd_dev->header_name,
1796 0, size,
1797 (char *) ondisk, version);
1798
1799 if (ret < 0)
1800 goto out_err;
1801 if (WARN_ON((size_t) ret < size)) {
1802 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05001803 rbd_warn(rbd_dev, "short header read (want %zd got %d)",
1804 size, ret);
Alex Elder4156d992012-08-02 11:29:46 -05001805 goto out_err;
1806 }
1807 if (!rbd_dev_ondisk_valid(ondisk)) {
1808 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05001809 rbd_warn(rbd_dev, "invalid header");
Alex Elder4156d992012-08-02 11:29:46 -05001810 goto out_err;
1811 }
1812
1813 names_size = le64_to_cpu(ondisk->snap_names_len);
1814 want_count = snap_count;
1815 snap_count = le32_to_cpu(ondisk->snap_count);
1816 } while (snap_count != want_count);
1817
1818 return ondisk;
1819
1820out_err:
1821 kfree(ondisk);
1822
1823 return ERR_PTR(ret);
1824}
1825
1826/*
1827 * reload the ondisk the header
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001828 */
1829static int rbd_read_header(struct rbd_device *rbd_dev,
1830 struct rbd_image_header *header)
1831{
Alex Elder4156d992012-08-02 11:29:46 -05001832 struct rbd_image_header_ondisk *ondisk;
1833 u64 ver = 0;
1834 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001835
Alex Elder4156d992012-08-02 11:29:46 -05001836 ondisk = rbd_dev_v1_header_read(rbd_dev, &ver);
1837 if (IS_ERR(ondisk))
1838 return PTR_ERR(ondisk);
1839 ret = rbd_header_from_disk(header, ondisk);
1840 if (ret >= 0)
1841 header->obj_version = ver;
1842 kfree(ondisk);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001843
Alex Elder4156d992012-08-02 11:29:46 -05001844 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001845}
1846
Alex Elder41f38c22012-10-25 23:34:40 -05001847static void rbd_remove_all_snaps(struct rbd_device *rbd_dev)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001848{
1849 struct rbd_snap *snap;
Alex Eldera0593292012-07-19 09:09:27 -05001850 struct rbd_snap *next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001851
Alex Eldera0593292012-07-19 09:09:27 -05001852 list_for_each_entry_safe(snap, next, &rbd_dev->snaps, node)
Alex Elder41f38c22012-10-25 23:34:40 -05001853 rbd_remove_snap_dev(snap);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001854}
1855
Alex Elder94785542012-10-09 13:50:17 -07001856static void rbd_update_mapping_size(struct rbd_device *rbd_dev)
1857{
1858 sector_t size;
1859
Alex Elder0d7dbfc2012-10-25 23:34:41 -05001860 if (rbd_dev->spec->snap_id != CEPH_NOSNAP)
Alex Elder94785542012-10-09 13:50:17 -07001861 return;
1862
1863 size = (sector_t) rbd_dev->header.image_size / SECTOR_SIZE;
1864 dout("setting size to %llu sectors", (unsigned long long) size);
1865 rbd_dev->mapping.size = (u64) size;
1866 set_capacity(rbd_dev->disk, size);
1867}
1868
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001869/*
1870 * only read the first part of the ondisk header, without the snaps info
1871 */
Alex Elder117973f2012-08-31 17:29:55 -05001872static int rbd_dev_v1_refresh(struct rbd_device *rbd_dev, u64 *hver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001873{
1874 int ret;
1875 struct rbd_image_header h;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001876
1877 ret = rbd_read_header(rbd_dev, &h);
1878 if (ret < 0)
1879 return ret;
1880
Josh Durgina51aa0c2011-12-05 10:35:04 -08001881 down_write(&rbd_dev->header_rwsem);
1882
Alex Elder94785542012-10-09 13:50:17 -07001883 /* Update image size, and check for resize of mapped image */
1884 rbd_dev->header.image_size = h.image_size;
1885 rbd_update_mapping_size(rbd_dev);
Sage Weil9db4b3e2011-04-19 22:49:06 -07001886
Alex Elder849b4262012-07-09 21:04:24 -05001887 /* rbd_dev->header.object_prefix shouldn't change */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001888 kfree(rbd_dev->header.snap_sizes);
Alex Elder849b4262012-07-09 21:04:24 -05001889 kfree(rbd_dev->header.snap_names);
Josh Durgind1d25642011-12-05 14:03:05 -08001890 /* osd requests may still refer to snapc */
1891 ceph_put_snap_context(rbd_dev->header.snapc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001892
Alex Elderb8136232012-07-25 09:32:41 -05001893 if (hver)
1894 *hver = h.obj_version;
Josh Durgina71b8912011-12-05 18:10:44 -08001895 rbd_dev->header.obj_version = h.obj_version;
Josh Durgin93a24e02011-12-05 10:41:28 -08001896 rbd_dev->header.image_size = h.image_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001897 rbd_dev->header.snapc = h.snapc;
1898 rbd_dev->header.snap_names = h.snap_names;
1899 rbd_dev->header.snap_sizes = h.snap_sizes;
Alex Elder849b4262012-07-09 21:04:24 -05001900 /* Free the extra copy of the object prefix */
1901 WARN_ON(strcmp(rbd_dev->header.object_prefix, h.object_prefix));
1902 kfree(h.object_prefix);
1903
Alex Elder304f6802012-08-31 17:29:52 -05001904 ret = rbd_dev_snaps_update(rbd_dev);
1905 if (!ret)
1906 ret = rbd_dev_snaps_register(rbd_dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001907
Josh Durginc6666012011-11-21 17:11:12 -08001908 up_write(&rbd_dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001909
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001910 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001911}
1912
Alex Elder117973f2012-08-31 17:29:55 -05001913static int rbd_dev_refresh(struct rbd_device *rbd_dev, u64 *hver)
Alex Elder1fe5e992012-07-25 09:32:41 -05001914{
1915 int ret;
1916
Alex Elder117973f2012-08-31 17:29:55 -05001917 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
Alex Elder1fe5e992012-07-25 09:32:41 -05001918 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Alex Elder117973f2012-08-31 17:29:55 -05001919 if (rbd_dev->image_format == 1)
1920 ret = rbd_dev_v1_refresh(rbd_dev, hver);
1921 else
1922 ret = rbd_dev_v2_refresh(rbd_dev, hver);
Alex Elder1fe5e992012-07-25 09:32:41 -05001923 mutex_unlock(&ctl_mutex);
1924
1925 return ret;
1926}
1927
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001928static int rbd_init_disk(struct rbd_device *rbd_dev)
1929{
1930 struct gendisk *disk;
1931 struct request_queue *q;
Alex Elder593a9e72012-02-07 12:03:37 -06001932 u64 segment_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001933
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001934 /* create gendisk info */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001935 disk = alloc_disk(RBD_MINORS_PER_MAJOR);
1936 if (!disk)
Alex Elder1fcdb8a2012-08-29 17:11:06 -05001937 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001938
Alex Elderf0f8cef2012-01-29 13:57:44 -06001939 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
Alex Elderde71a292012-07-03 16:01:19 -05001940 rbd_dev->dev_id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001941 disk->major = rbd_dev->major;
1942 disk->first_minor = 0;
1943 disk->fops = &rbd_bd_ops;
1944 disk->private_data = rbd_dev;
1945
1946 /* init rq */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001947 q = blk_init_queue(rbd_rq_fn, &rbd_dev->lock);
1948 if (!q)
1949 goto out_disk;
Josh Durgin029bcbd2011-07-22 11:35:23 -07001950
Alex Elder593a9e72012-02-07 12:03:37 -06001951 /* We use the default size, but let's be explicit about it. */
1952 blk_queue_physical_block_size(q, SECTOR_SIZE);
1953
Josh Durgin029bcbd2011-07-22 11:35:23 -07001954 /* set io sizes to object size */
Alex Elder593a9e72012-02-07 12:03:37 -06001955 segment_size = rbd_obj_bytes(&rbd_dev->header);
1956 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
1957 blk_queue_max_segment_size(q, segment_size);
1958 blk_queue_io_min(q, segment_size);
1959 blk_queue_io_opt(q, segment_size);
Josh Durgin029bcbd2011-07-22 11:35:23 -07001960
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001961 blk_queue_merge_bvec(q, rbd_merge_bvec);
1962 disk->queue = q;
1963
1964 q->queuedata = rbd_dev;
1965
1966 rbd_dev->disk = disk;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001967
Alex Elder12f02942012-08-29 17:11:07 -05001968 set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
1969
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001970 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001971out_disk:
1972 put_disk(disk);
Alex Elder1fcdb8a2012-08-29 17:11:06 -05001973
1974 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001975}
1976
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001977/*
1978 sysfs
1979*/
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001980
Alex Elder593a9e72012-02-07 12:03:37 -06001981static struct rbd_device *dev_to_rbd_dev(struct device *dev)
1982{
1983 return container_of(dev, struct rbd_device, dev);
1984}
1985
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001986static ssize_t rbd_size_show(struct device *dev,
1987 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001988{
Alex Elder593a9e72012-02-07 12:03:37 -06001989 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Josh Durgina51aa0c2011-12-05 10:35:04 -08001990 sector_t size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001991
Josh Durgina51aa0c2011-12-05 10:35:04 -08001992 down_read(&rbd_dev->header_rwsem);
1993 size = get_capacity(rbd_dev->disk);
1994 up_read(&rbd_dev->header_rwsem);
1995
1996 return sprintf(buf, "%llu\n", (unsigned long long) size * SECTOR_SIZE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001997}
1998
Alex Elder34b13182012-07-13 20:35:12 -05001999/*
2000 * Note this shows the features for whatever's mapped, which is not
2001 * necessarily the base image.
2002 */
2003static ssize_t rbd_features_show(struct device *dev,
2004 struct device_attribute *attr, char *buf)
2005{
2006 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
2007
2008 return sprintf(buf, "0x%016llx\n",
2009 (unsigned long long) rbd_dev->mapping.features);
2010}
2011
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002012static ssize_t rbd_major_show(struct device *dev,
2013 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002014{
Alex Elder593a9e72012-02-07 12:03:37 -06002015 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002016
2017 return sprintf(buf, "%d\n", rbd_dev->major);
2018}
2019
2020static ssize_t rbd_client_id_show(struct device *dev,
2021 struct device_attribute *attr, char *buf)
2022{
Alex Elder593a9e72012-02-07 12:03:37 -06002023 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002024
Alex Elder1dbb4392012-01-24 10:08:37 -06002025 return sprintf(buf, "client%lld\n",
2026 ceph_client_id(rbd_dev->rbd_client->client));
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002027}
2028
2029static ssize_t rbd_pool_show(struct device *dev,
2030 struct device_attribute *attr, char *buf)
2031{
Alex Elder593a9e72012-02-07 12:03:37 -06002032 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002033
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002034 return sprintf(buf, "%s\n", rbd_dev->spec->pool_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002035}
2036
Alex Elder9bb2f332012-07-12 10:46:35 -05002037static ssize_t rbd_pool_id_show(struct device *dev,
2038 struct device_attribute *attr, char *buf)
2039{
2040 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
2041
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002042 return sprintf(buf, "%llu\n",
2043 (unsigned long long) rbd_dev->spec->pool_id);
Alex Elder9bb2f332012-07-12 10:46:35 -05002044}
2045
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002046static ssize_t rbd_name_show(struct device *dev,
2047 struct device_attribute *attr, char *buf)
2048{
Alex Elder593a9e72012-02-07 12:03:37 -06002049 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002050
Alex Eldera92ffdf2012-10-30 19:40:33 -05002051 if (rbd_dev->spec->image_name)
2052 return sprintf(buf, "%s\n", rbd_dev->spec->image_name);
2053
2054 return sprintf(buf, "(unknown)\n");
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002055}
2056
Alex Elder589d30e2012-07-10 20:30:11 -05002057static ssize_t rbd_image_id_show(struct device *dev,
2058 struct device_attribute *attr, char *buf)
2059{
2060 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
2061
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002062 return sprintf(buf, "%s\n", rbd_dev->spec->image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05002063}
2064
Alex Elder34b13182012-07-13 20:35:12 -05002065/*
2066 * Shows the name of the currently-mapped snapshot (or
2067 * RBD_SNAP_HEAD_NAME for the base image).
2068 */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002069static ssize_t rbd_snap_show(struct device *dev,
2070 struct device_attribute *attr,
2071 char *buf)
2072{
Alex Elder593a9e72012-02-07 12:03:37 -06002073 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002074
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002075 return sprintf(buf, "%s\n", rbd_dev->spec->snap_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002076}
2077
Alex Elder86b00e02012-10-25 23:34:42 -05002078/*
2079 * For an rbd v2 image, shows the pool id, image id, and snapshot id
2080 * for the parent image. If there is no parent, simply shows
2081 * "(no parent image)".
2082 */
2083static ssize_t rbd_parent_show(struct device *dev,
2084 struct device_attribute *attr,
2085 char *buf)
2086{
2087 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
2088 struct rbd_spec *spec = rbd_dev->parent_spec;
2089 int count;
2090 char *bufp = buf;
2091
2092 if (!spec)
2093 return sprintf(buf, "(no parent image)\n");
2094
2095 count = sprintf(bufp, "pool_id %llu\npool_name %s\n",
2096 (unsigned long long) spec->pool_id, spec->pool_name);
2097 if (count < 0)
2098 return count;
2099 bufp += count;
2100
2101 count = sprintf(bufp, "image_id %s\nimage_name %s\n", spec->image_id,
2102 spec->image_name ? spec->image_name : "(unknown)");
2103 if (count < 0)
2104 return count;
2105 bufp += count;
2106
2107 count = sprintf(bufp, "snap_id %llu\nsnap_name %s\n",
2108 (unsigned long long) spec->snap_id, spec->snap_name);
2109 if (count < 0)
2110 return count;
2111 bufp += count;
2112
2113 count = sprintf(bufp, "overlap %llu\n", rbd_dev->parent_overlap);
2114 if (count < 0)
2115 return count;
2116 bufp += count;
2117
2118 return (ssize_t) (bufp - buf);
2119}
2120
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002121static ssize_t rbd_image_refresh(struct device *dev,
2122 struct device_attribute *attr,
2123 const char *buf,
2124 size_t size)
2125{
Alex Elder593a9e72012-02-07 12:03:37 -06002126 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Alex Elderb8136232012-07-25 09:32:41 -05002127 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002128
Alex Elder117973f2012-08-31 17:29:55 -05002129 ret = rbd_dev_refresh(rbd_dev, NULL);
Alex Elderb8136232012-07-25 09:32:41 -05002130
2131 return ret < 0 ? ret : size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002132}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002133
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002134static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
Alex Elder34b13182012-07-13 20:35:12 -05002135static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002136static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
2137static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
2138static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
Alex Elder9bb2f332012-07-12 10:46:35 -05002139static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002140static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
Alex Elder589d30e2012-07-10 20:30:11 -05002141static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002142static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
2143static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
Alex Elder86b00e02012-10-25 23:34:42 -05002144static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002145
2146static struct attribute *rbd_attrs[] = {
2147 &dev_attr_size.attr,
Alex Elder34b13182012-07-13 20:35:12 -05002148 &dev_attr_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002149 &dev_attr_major.attr,
2150 &dev_attr_client_id.attr,
2151 &dev_attr_pool.attr,
Alex Elder9bb2f332012-07-12 10:46:35 -05002152 &dev_attr_pool_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002153 &dev_attr_name.attr,
Alex Elder589d30e2012-07-10 20:30:11 -05002154 &dev_attr_image_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002155 &dev_attr_current_snap.attr,
Alex Elder86b00e02012-10-25 23:34:42 -05002156 &dev_attr_parent.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002157 &dev_attr_refresh.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002158 NULL
2159};
2160
2161static struct attribute_group rbd_attr_group = {
2162 .attrs = rbd_attrs,
2163};
2164
2165static const struct attribute_group *rbd_attr_groups[] = {
2166 &rbd_attr_group,
2167 NULL
2168};
2169
2170static void rbd_sysfs_dev_release(struct device *dev)
2171{
2172}
2173
2174static struct device_type rbd_device_type = {
2175 .name = "rbd",
2176 .groups = rbd_attr_groups,
2177 .release = rbd_sysfs_dev_release,
2178};
2179
2180
2181/*
2182 sysfs - snapshots
2183*/
2184
2185static ssize_t rbd_snap_size_show(struct device *dev,
2186 struct device_attribute *attr,
2187 char *buf)
2188{
2189 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2190
Josh Durgin3591538f2011-12-05 18:25:13 -08002191 return sprintf(buf, "%llu\n", (unsigned long long)snap->size);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002192}
2193
2194static ssize_t rbd_snap_id_show(struct device *dev,
2195 struct device_attribute *attr,
2196 char *buf)
2197{
2198 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2199
Josh Durgin3591538f2011-12-05 18:25:13 -08002200 return sprintf(buf, "%llu\n", (unsigned long long)snap->id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002201}
2202
Alex Elder34b13182012-07-13 20:35:12 -05002203static ssize_t rbd_snap_features_show(struct device *dev,
2204 struct device_attribute *attr,
2205 char *buf)
2206{
2207 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2208
2209 return sprintf(buf, "0x%016llx\n",
2210 (unsigned long long) snap->features);
2211}
2212
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002213static DEVICE_ATTR(snap_size, S_IRUGO, rbd_snap_size_show, NULL);
2214static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL);
Alex Elder34b13182012-07-13 20:35:12 -05002215static DEVICE_ATTR(snap_features, S_IRUGO, rbd_snap_features_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002216
2217static struct attribute *rbd_snap_attrs[] = {
2218 &dev_attr_snap_size.attr,
2219 &dev_attr_snap_id.attr,
Alex Elder34b13182012-07-13 20:35:12 -05002220 &dev_attr_snap_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002221 NULL,
2222};
2223
2224static struct attribute_group rbd_snap_attr_group = {
2225 .attrs = rbd_snap_attrs,
2226};
2227
2228static void rbd_snap_dev_release(struct device *dev)
2229{
2230 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2231 kfree(snap->name);
2232 kfree(snap);
2233}
2234
2235static const struct attribute_group *rbd_snap_attr_groups[] = {
2236 &rbd_snap_attr_group,
2237 NULL
2238};
2239
2240static struct device_type rbd_snap_device_type = {
2241 .groups = rbd_snap_attr_groups,
2242 .release = rbd_snap_dev_release,
2243};
2244
Alex Elder8b8fb992012-10-26 17:25:24 -05002245static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)
2246{
2247 kref_get(&spec->kref);
2248
2249 return spec;
2250}
2251
2252static void rbd_spec_free(struct kref *kref);
2253static void rbd_spec_put(struct rbd_spec *spec)
2254{
2255 if (spec)
2256 kref_put(&spec->kref, rbd_spec_free);
2257}
2258
2259static struct rbd_spec *rbd_spec_alloc(void)
2260{
2261 struct rbd_spec *spec;
2262
2263 spec = kzalloc(sizeof (*spec), GFP_KERNEL);
2264 if (!spec)
2265 return NULL;
2266 kref_init(&spec->kref);
2267
2268 rbd_spec_put(rbd_spec_get(spec)); /* TEMPORARY */
2269
2270 return spec;
2271}
2272
2273static void rbd_spec_free(struct kref *kref)
2274{
2275 struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref);
2276
2277 kfree(spec->pool_name);
2278 kfree(spec->image_id);
2279 kfree(spec->image_name);
2280 kfree(spec->snap_name);
2281 kfree(spec);
2282}
2283
Alex Elderc53d5892012-10-25 23:34:42 -05002284struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,
2285 struct rbd_spec *spec)
2286{
2287 struct rbd_device *rbd_dev;
2288
2289 rbd_dev = kzalloc(sizeof (*rbd_dev), GFP_KERNEL);
2290 if (!rbd_dev)
2291 return NULL;
2292
2293 spin_lock_init(&rbd_dev->lock);
2294 INIT_LIST_HEAD(&rbd_dev->node);
2295 INIT_LIST_HEAD(&rbd_dev->snaps);
2296 init_rwsem(&rbd_dev->header_rwsem);
2297
2298 rbd_dev->spec = spec;
2299 rbd_dev->rbd_client = rbdc;
2300
2301 return rbd_dev;
2302}
2303
2304static void rbd_dev_destroy(struct rbd_device *rbd_dev)
2305{
Alex Elder86b00e02012-10-25 23:34:42 -05002306 rbd_spec_put(rbd_dev->parent_spec);
Alex Elderc53d5892012-10-25 23:34:42 -05002307 kfree(rbd_dev->header_name);
2308 rbd_put_client(rbd_dev->rbd_client);
2309 rbd_spec_put(rbd_dev->spec);
2310 kfree(rbd_dev);
2311}
2312
Alex Elder304f6802012-08-31 17:29:52 -05002313static bool rbd_snap_registered(struct rbd_snap *snap)
2314{
2315 bool ret = snap->dev.type == &rbd_snap_device_type;
2316 bool reg = device_is_registered(&snap->dev);
2317
2318 rbd_assert(!ret ^ reg);
2319
2320 return ret;
2321}
2322
Alex Elder41f38c22012-10-25 23:34:40 -05002323static void rbd_remove_snap_dev(struct rbd_snap *snap)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002324{
2325 list_del(&snap->node);
Alex Elder304f6802012-08-31 17:29:52 -05002326 if (device_is_registered(&snap->dev))
2327 device_unregister(&snap->dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002328}
2329
Alex Elder14e70852012-07-19 09:09:27 -05002330static int rbd_register_snap_dev(struct rbd_snap *snap,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002331 struct device *parent)
2332{
2333 struct device *dev = &snap->dev;
2334 int ret;
2335
2336 dev->type = &rbd_snap_device_type;
2337 dev->parent = parent;
2338 dev->release = rbd_snap_dev_release;
Alex Elderd4b125e2012-07-03 16:01:19 -05002339 dev_set_name(dev, "%s%s", RBD_SNAP_DEV_NAME_PREFIX, snap->name);
Alex Elder304f6802012-08-31 17:29:52 -05002340 dout("%s: registering device for snapshot %s\n", __func__, snap->name);
2341
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002342 ret = device_register(dev);
2343
2344 return ret;
2345}
2346
Alex Elder4e891e02012-07-10 20:30:10 -05002347static struct rbd_snap *__rbd_add_snap_dev(struct rbd_device *rbd_dev,
Alex Elderc8d18422012-07-10 20:30:11 -05002348 const char *snap_name,
Alex Elder34b13182012-07-13 20:35:12 -05002349 u64 snap_id, u64 snap_size,
2350 u64 snap_features)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002351{
Alex Elder4e891e02012-07-10 20:30:10 -05002352 struct rbd_snap *snap;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002353 int ret;
Alex Elder4e891e02012-07-10 20:30:10 -05002354
2355 snap = kzalloc(sizeof (*snap), GFP_KERNEL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002356 if (!snap)
Alex Elder4e891e02012-07-10 20:30:10 -05002357 return ERR_PTR(-ENOMEM);
2358
2359 ret = -ENOMEM;
Alex Elderc8d18422012-07-10 20:30:11 -05002360 snap->name = kstrdup(snap_name, GFP_KERNEL);
Alex Elder4e891e02012-07-10 20:30:10 -05002361 if (!snap->name)
2362 goto err;
2363
Alex Elderc8d18422012-07-10 20:30:11 -05002364 snap->id = snap_id;
2365 snap->size = snap_size;
Alex Elder34b13182012-07-13 20:35:12 -05002366 snap->features = snap_features;
Alex Elder4e891e02012-07-10 20:30:10 -05002367
2368 return snap;
2369
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002370err:
2371 kfree(snap->name);
2372 kfree(snap);
Alex Elder4e891e02012-07-10 20:30:10 -05002373
2374 return ERR_PTR(ret);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002375}
2376
Alex Eldercd892122012-07-03 16:01:19 -05002377static char *rbd_dev_v1_snap_info(struct rbd_device *rbd_dev, u32 which,
2378 u64 *snap_size, u64 *snap_features)
2379{
2380 char *snap_name;
2381
2382 rbd_assert(which < rbd_dev->header.snapc->num_snaps);
2383
2384 *snap_size = rbd_dev->header.snap_sizes[which];
2385 *snap_features = 0; /* No features for v1 */
2386
2387 /* Skip over names until we find the one we are looking for */
2388
2389 snap_name = rbd_dev->header.snap_names;
2390 while (which--)
2391 snap_name += strlen(snap_name) + 1;
2392
2393 return snap_name;
2394}
2395
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002396/*
Alex Elder9d475de2012-07-03 16:01:19 -05002397 * Get the size and object order for an image snapshot, or if
2398 * snap_id is CEPH_NOSNAP, gets this information for the base
2399 * image.
2400 */
2401static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
2402 u8 *order, u64 *snap_size)
2403{
2404 __le64 snapid = cpu_to_le64(snap_id);
2405 int ret;
2406 struct {
2407 u8 order;
2408 __le64 size;
2409 } __attribute__ ((packed)) size_buf = { 0 };
2410
2411 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2412 "rbd", "get_size",
2413 (char *) &snapid, sizeof (snapid),
2414 (char *) &size_buf, sizeof (size_buf),
2415 CEPH_OSD_FLAG_READ, NULL);
2416 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2417 if (ret < 0)
2418 return ret;
2419
2420 *order = size_buf.order;
2421 *snap_size = le64_to_cpu(size_buf.size);
2422
2423 dout(" snap_id 0x%016llx order = %u, snap_size = %llu\n",
2424 (unsigned long long) snap_id, (unsigned int) *order,
2425 (unsigned long long) *snap_size);
2426
2427 return 0;
2428}
2429
2430static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
2431{
2432 return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
2433 &rbd_dev->header.obj_order,
2434 &rbd_dev->header.image_size);
2435}
2436
Alex Elder1e130192012-07-03 16:01:19 -05002437static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
2438{
2439 void *reply_buf;
2440 int ret;
2441 void *p;
2442
2443 reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL);
2444 if (!reply_buf)
2445 return -ENOMEM;
2446
2447 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2448 "rbd", "get_object_prefix",
2449 NULL, 0,
2450 reply_buf, RBD_OBJ_PREFIX_LEN_MAX,
2451 CEPH_OSD_FLAG_READ, NULL);
2452 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2453 if (ret < 0)
2454 goto out;
Alex Eldera0ea3a42012-10-10 21:19:13 -07002455 ret = 0; /* rbd_req_sync_exec() can return positive */
Alex Elder1e130192012-07-03 16:01:19 -05002456
2457 p = reply_buf;
2458 rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p,
2459 p + RBD_OBJ_PREFIX_LEN_MAX,
2460 NULL, GFP_NOIO);
2461
2462 if (IS_ERR(rbd_dev->header.object_prefix)) {
2463 ret = PTR_ERR(rbd_dev->header.object_prefix);
2464 rbd_dev->header.object_prefix = NULL;
2465 } else {
2466 dout(" object_prefix = %s\n", rbd_dev->header.object_prefix);
2467 }
2468
2469out:
2470 kfree(reply_buf);
2471
2472 return ret;
2473}
2474
Alex Elderb1b54022012-07-03 16:01:19 -05002475static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
2476 u64 *snap_features)
2477{
2478 __le64 snapid = cpu_to_le64(snap_id);
2479 struct {
2480 __le64 features;
2481 __le64 incompat;
2482 } features_buf = { 0 };
Alex Elderd8891402012-10-09 13:50:17 -07002483 u64 incompat;
Alex Elderb1b54022012-07-03 16:01:19 -05002484 int ret;
2485
2486 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2487 "rbd", "get_features",
2488 (char *) &snapid, sizeof (snapid),
2489 (char *) &features_buf, sizeof (features_buf),
2490 CEPH_OSD_FLAG_READ, NULL);
2491 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2492 if (ret < 0)
2493 return ret;
Alex Elderd8891402012-10-09 13:50:17 -07002494
2495 incompat = le64_to_cpu(features_buf.incompat);
2496 if (incompat & ~RBD_FEATURES_ALL)
Alex Elderb8f5c6e2012-11-01 08:39:26 -05002497 return -ENXIO;
Alex Elderd8891402012-10-09 13:50:17 -07002498
Alex Elderb1b54022012-07-03 16:01:19 -05002499 *snap_features = le64_to_cpu(features_buf.features);
2500
2501 dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
2502 (unsigned long long) snap_id,
2503 (unsigned long long) *snap_features,
2504 (unsigned long long) le64_to_cpu(features_buf.incompat));
2505
2506 return 0;
2507}
2508
2509static int rbd_dev_v2_features(struct rbd_device *rbd_dev)
2510{
2511 return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP,
2512 &rbd_dev->header.features);
2513}
2514
Alex Elder86b00e02012-10-25 23:34:42 -05002515static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev)
2516{
2517 struct rbd_spec *parent_spec;
2518 size_t size;
2519 void *reply_buf = NULL;
2520 __le64 snapid;
2521 void *p;
2522 void *end;
2523 char *image_id;
2524 u64 overlap;
Alex Elder86b00e02012-10-25 23:34:42 -05002525 int ret;
2526
2527 parent_spec = rbd_spec_alloc();
2528 if (!parent_spec)
2529 return -ENOMEM;
2530
2531 size = sizeof (__le64) + /* pool_id */
2532 sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */
2533 sizeof (__le64) + /* snap_id */
2534 sizeof (__le64); /* overlap */
2535 reply_buf = kmalloc(size, GFP_KERNEL);
2536 if (!reply_buf) {
2537 ret = -ENOMEM;
2538 goto out_err;
2539 }
2540
2541 snapid = cpu_to_le64(CEPH_NOSNAP);
2542 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2543 "rbd", "get_parent",
2544 (char *) &snapid, sizeof (snapid),
2545 (char *) reply_buf, size,
2546 CEPH_OSD_FLAG_READ, NULL);
2547 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2548 if (ret < 0)
2549 goto out_err;
2550
2551 ret = -ERANGE;
2552 p = reply_buf;
2553 end = (char *) reply_buf + size;
2554 ceph_decode_64_safe(&p, end, parent_spec->pool_id, out_err);
2555 if (parent_spec->pool_id == CEPH_NOPOOL)
2556 goto out; /* No parent? No problem. */
2557
Alex Elder979ed482012-11-01 08:39:26 -05002558 image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elder86b00e02012-10-25 23:34:42 -05002559 if (IS_ERR(image_id)) {
2560 ret = PTR_ERR(image_id);
2561 goto out_err;
2562 }
2563 parent_spec->image_id = image_id;
2564 ceph_decode_64_safe(&p, end, parent_spec->snap_id, out_err);
2565 ceph_decode_64_safe(&p, end, overlap, out_err);
2566
2567 rbd_dev->parent_overlap = overlap;
2568 rbd_dev->parent_spec = parent_spec;
2569 parent_spec = NULL; /* rbd_dev now owns this */
2570out:
2571 ret = 0;
2572out_err:
2573 kfree(reply_buf);
2574 rbd_spec_put(parent_spec);
2575
2576 return ret;
2577}
2578
Alex Elder9e15b772012-10-30 19:40:33 -05002579static char *rbd_dev_image_name(struct rbd_device *rbd_dev)
2580{
2581 size_t image_id_size;
2582 char *image_id;
2583 void *p;
2584 void *end;
2585 size_t size;
2586 void *reply_buf = NULL;
2587 size_t len = 0;
2588 char *image_name = NULL;
2589 int ret;
2590
2591 rbd_assert(!rbd_dev->spec->image_name);
2592
Alex Elder69e7a022012-11-01 08:39:26 -05002593 len = strlen(rbd_dev->spec->image_id);
2594 image_id_size = sizeof (__le32) + len;
Alex Elder9e15b772012-10-30 19:40:33 -05002595 image_id = kmalloc(image_id_size, GFP_KERNEL);
2596 if (!image_id)
2597 return NULL;
2598
2599 p = image_id;
2600 end = (char *) image_id + image_id_size;
Alex Elder69e7a022012-11-01 08:39:26 -05002601 ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32) len);
Alex Elder9e15b772012-10-30 19:40:33 -05002602
2603 size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX;
2604 reply_buf = kmalloc(size, GFP_KERNEL);
2605 if (!reply_buf)
2606 goto out;
2607
2608 ret = rbd_req_sync_exec(rbd_dev, RBD_DIRECTORY,
2609 "rbd", "dir_get_name",
2610 image_id, image_id_size,
2611 (char *) reply_buf, size,
2612 CEPH_OSD_FLAG_READ, NULL);
2613 if (ret < 0)
2614 goto out;
2615 p = reply_buf;
2616 end = (char *) reply_buf + size;
2617 image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL);
2618 if (IS_ERR(image_name))
2619 image_name = NULL;
2620 else
2621 dout("%s: name is %s len is %zd\n", __func__, image_name, len);
2622out:
2623 kfree(reply_buf);
2624 kfree(image_id);
2625
2626 return image_name;
2627}
2628
2629/*
2630 * When a parent image gets probed, we only have the pool, image,
2631 * and snapshot ids but not the names of any of them. This call
2632 * is made later to fill in those names. It has to be done after
2633 * rbd_dev_snaps_update() has completed because some of the
2634 * information (in particular, snapshot name) is not available
2635 * until then.
2636 */
2637static int rbd_dev_probe_update_spec(struct rbd_device *rbd_dev)
2638{
2639 struct ceph_osd_client *osdc;
2640 const char *name;
2641 void *reply_buf = NULL;
2642 int ret;
2643
2644 if (rbd_dev->spec->pool_name)
2645 return 0; /* Already have the names */
2646
2647 /* Look up the pool name */
2648
2649 osdc = &rbd_dev->rbd_client->client->osdc;
2650 name = ceph_pg_pool_name_by_id(osdc->osdmap, rbd_dev->spec->pool_id);
Alex Elder935dc892012-11-01 10:17:15 -05002651 if (!name) {
2652 rbd_warn(rbd_dev, "there is no pool with id %llu",
2653 rbd_dev->spec->pool_id); /* Really a BUG() */
2654 return -EIO;
2655 }
Alex Elder9e15b772012-10-30 19:40:33 -05002656
2657 rbd_dev->spec->pool_name = kstrdup(name, GFP_KERNEL);
2658 if (!rbd_dev->spec->pool_name)
2659 return -ENOMEM;
2660
2661 /* Fetch the image name; tolerate failure here */
2662
2663 name = rbd_dev_image_name(rbd_dev);
Alex Elder69e7a022012-11-01 08:39:26 -05002664 if (name)
Alex Elder9e15b772012-10-30 19:40:33 -05002665 rbd_dev->spec->image_name = (char *) name;
Alex Elder69e7a022012-11-01 08:39:26 -05002666 else
Alex Elder06ecc6c2012-11-01 10:17:15 -05002667 rbd_warn(rbd_dev, "unable to get image name");
Alex Elder9e15b772012-10-30 19:40:33 -05002668
2669 /* Look up the snapshot name. */
2670
2671 name = rbd_snap_name(rbd_dev, rbd_dev->spec->snap_id);
2672 if (!name) {
Alex Elder935dc892012-11-01 10:17:15 -05002673 rbd_warn(rbd_dev, "no snapshot with id %llu",
2674 rbd_dev->spec->snap_id); /* Really a BUG() */
Alex Elder9e15b772012-10-30 19:40:33 -05002675 ret = -EIO;
2676 goto out_err;
2677 }
2678 rbd_dev->spec->snap_name = kstrdup(name, GFP_KERNEL);
2679 if(!rbd_dev->spec->snap_name)
2680 goto out_err;
2681
2682 return 0;
2683out_err:
2684 kfree(reply_buf);
2685 kfree(rbd_dev->spec->pool_name);
2686 rbd_dev->spec->pool_name = NULL;
2687
2688 return ret;
2689}
2690
Alex Elder6e14b1a2012-07-03 16:01:19 -05002691static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev, u64 *ver)
Alex Elder35d489f2012-07-03 16:01:19 -05002692{
2693 size_t size;
2694 int ret;
2695 void *reply_buf;
2696 void *p;
2697 void *end;
2698 u64 seq;
2699 u32 snap_count;
2700 struct ceph_snap_context *snapc;
2701 u32 i;
2702
2703 /*
2704 * We'll need room for the seq value (maximum snapshot id),
2705 * snapshot count, and array of that many snapshot ids.
2706 * For now we have a fixed upper limit on the number we're
2707 * prepared to receive.
2708 */
2709 size = sizeof (__le64) + sizeof (__le32) +
2710 RBD_MAX_SNAP_COUNT * sizeof (__le64);
2711 reply_buf = kzalloc(size, GFP_KERNEL);
2712 if (!reply_buf)
2713 return -ENOMEM;
2714
2715 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2716 "rbd", "get_snapcontext",
2717 NULL, 0,
2718 reply_buf, size,
Alex Elder6e14b1a2012-07-03 16:01:19 -05002719 CEPH_OSD_FLAG_READ, ver);
Alex Elder35d489f2012-07-03 16:01:19 -05002720 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2721 if (ret < 0)
2722 goto out;
2723
2724 ret = -ERANGE;
2725 p = reply_buf;
2726 end = (char *) reply_buf + size;
2727 ceph_decode_64_safe(&p, end, seq, out);
2728 ceph_decode_32_safe(&p, end, snap_count, out);
2729
2730 /*
2731 * Make sure the reported number of snapshot ids wouldn't go
2732 * beyond the end of our buffer. But before checking that,
2733 * make sure the computed size of the snapshot context we
2734 * allocate is representable in a size_t.
2735 */
2736 if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
2737 / sizeof (u64)) {
2738 ret = -EINVAL;
2739 goto out;
2740 }
2741 if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
2742 goto out;
2743
2744 size = sizeof (struct ceph_snap_context) +
2745 snap_count * sizeof (snapc->snaps[0]);
2746 snapc = kmalloc(size, GFP_KERNEL);
2747 if (!snapc) {
2748 ret = -ENOMEM;
2749 goto out;
2750 }
2751
2752 atomic_set(&snapc->nref, 1);
2753 snapc->seq = seq;
2754 snapc->num_snaps = snap_count;
2755 for (i = 0; i < snap_count; i++)
2756 snapc->snaps[i] = ceph_decode_64(&p);
2757
2758 rbd_dev->header.snapc = snapc;
2759
2760 dout(" snap context seq = %llu, snap_count = %u\n",
2761 (unsigned long long) seq, (unsigned int) snap_count);
2762
2763out:
2764 kfree(reply_buf);
2765
2766 return 0;
2767}
2768
Alex Elderb8b1e2d2012-07-03 16:01:19 -05002769static char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, u32 which)
2770{
2771 size_t size;
2772 void *reply_buf;
2773 __le64 snap_id;
2774 int ret;
2775 void *p;
2776 void *end;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05002777 char *snap_name;
2778
2779 size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN;
2780 reply_buf = kmalloc(size, GFP_KERNEL);
2781 if (!reply_buf)
2782 return ERR_PTR(-ENOMEM);
2783
2784 snap_id = cpu_to_le64(rbd_dev->header.snapc->snaps[which]);
2785 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2786 "rbd", "get_snapshot_name",
2787 (char *) &snap_id, sizeof (snap_id),
2788 reply_buf, size,
2789 CEPH_OSD_FLAG_READ, NULL);
2790 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2791 if (ret < 0)
2792 goto out;
2793
2794 p = reply_buf;
2795 end = (char *) reply_buf + size;
Alex Eldere5c35532012-10-25 23:34:41 -05002796 snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05002797 if (IS_ERR(snap_name)) {
2798 ret = PTR_ERR(snap_name);
2799 goto out;
2800 } else {
2801 dout(" snap_id 0x%016llx snap_name = %s\n",
2802 (unsigned long long) le64_to_cpu(snap_id), snap_name);
2803 }
2804 kfree(reply_buf);
2805
2806 return snap_name;
2807out:
2808 kfree(reply_buf);
2809
2810 return ERR_PTR(ret);
2811}
2812
2813static char *rbd_dev_v2_snap_info(struct rbd_device *rbd_dev, u32 which,
2814 u64 *snap_size, u64 *snap_features)
2815{
2816 __le64 snap_id;
2817 u8 order;
2818 int ret;
2819
2820 snap_id = rbd_dev->header.snapc->snaps[which];
2821 ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, &order, snap_size);
2822 if (ret)
2823 return ERR_PTR(ret);
2824 ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, snap_features);
2825 if (ret)
2826 return ERR_PTR(ret);
2827
2828 return rbd_dev_v2_snap_name(rbd_dev, which);
2829}
2830
2831static char *rbd_dev_snap_info(struct rbd_device *rbd_dev, u32 which,
2832 u64 *snap_size, u64 *snap_features)
2833{
2834 if (rbd_dev->image_format == 1)
2835 return rbd_dev_v1_snap_info(rbd_dev, which,
2836 snap_size, snap_features);
2837 if (rbd_dev->image_format == 2)
2838 return rbd_dev_v2_snap_info(rbd_dev, which,
2839 snap_size, snap_features);
2840 return ERR_PTR(-EINVAL);
2841}
2842
Alex Elder117973f2012-08-31 17:29:55 -05002843static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev, u64 *hver)
2844{
2845 int ret;
2846 __u8 obj_order;
2847
2848 down_write(&rbd_dev->header_rwsem);
2849
2850 /* Grab old order first, to see if it changes */
2851
2852 obj_order = rbd_dev->header.obj_order,
2853 ret = rbd_dev_v2_image_size(rbd_dev);
2854 if (ret)
2855 goto out;
2856 if (rbd_dev->header.obj_order != obj_order) {
2857 ret = -EIO;
2858 goto out;
2859 }
2860 rbd_update_mapping_size(rbd_dev);
2861
2862 ret = rbd_dev_v2_snap_context(rbd_dev, hver);
2863 dout("rbd_dev_v2_snap_context returned %d\n", ret);
2864 if (ret)
2865 goto out;
2866 ret = rbd_dev_snaps_update(rbd_dev);
2867 dout("rbd_dev_snaps_update returned %d\n", ret);
2868 if (ret)
2869 goto out;
2870 ret = rbd_dev_snaps_register(rbd_dev);
2871 dout("rbd_dev_snaps_register returned %d\n", ret);
2872out:
2873 up_write(&rbd_dev->header_rwsem);
2874
2875 return ret;
2876}
2877
Alex Elder9d475de2012-07-03 16:01:19 -05002878/*
Alex Elder35938152012-08-02 11:29:46 -05002879 * Scan the rbd device's current snapshot list and compare it to the
2880 * newly-received snapshot context. Remove any existing snapshots
2881 * not present in the new snapshot context. Add a new snapshot for
2882 * any snaphots in the snapshot context not in the current list.
2883 * And verify there are no changes to snapshots we already know
2884 * about.
2885 *
2886 * Assumes the snapshots in the snapshot context are sorted by
2887 * snapshot id, highest id first. (Snapshots in the rbd_dev's list
2888 * are also maintained in that order.)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002889 */
Alex Elder304f6802012-08-31 17:29:52 -05002890static int rbd_dev_snaps_update(struct rbd_device *rbd_dev)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002891{
Alex Elder35938152012-08-02 11:29:46 -05002892 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
2893 const u32 snap_count = snapc->num_snaps;
Alex Elder35938152012-08-02 11:29:46 -05002894 struct list_head *head = &rbd_dev->snaps;
2895 struct list_head *links = head->next;
2896 u32 index = 0;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002897
Alex Elder9fcbb802012-08-23 23:48:49 -05002898 dout("%s: snap count is %u\n", __func__, (unsigned int) snap_count);
Alex Elder35938152012-08-02 11:29:46 -05002899 while (index < snap_count || links != head) {
2900 u64 snap_id;
2901 struct rbd_snap *snap;
Alex Eldercd892122012-07-03 16:01:19 -05002902 char *snap_name;
2903 u64 snap_size = 0;
2904 u64 snap_features = 0;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002905
Alex Elder35938152012-08-02 11:29:46 -05002906 snap_id = index < snap_count ? snapc->snaps[index]
2907 : CEPH_NOSNAP;
2908 snap = links != head ? list_entry(links, struct rbd_snap, node)
2909 : NULL;
Alex Elderaafb2302012-09-06 16:00:54 -05002910 rbd_assert(!snap || snap->id != CEPH_NOSNAP);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002911
Alex Elder35938152012-08-02 11:29:46 -05002912 if (snap_id == CEPH_NOSNAP || (snap && snap->id > snap_id)) {
2913 struct list_head *next = links->next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002914
Alex Elder35938152012-08-02 11:29:46 -05002915 /* Existing snapshot not in the new snap context */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002916
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002917 if (rbd_dev->spec->snap_id == snap->id)
Alex Elderdaba5fd2012-10-26 17:25:23 -05002918 rbd_dev->exists = false;
Alex Elder41f38c22012-10-25 23:34:40 -05002919 rbd_remove_snap_dev(snap);
Alex Elder9fcbb802012-08-23 23:48:49 -05002920 dout("%ssnap id %llu has been removed\n",
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002921 rbd_dev->spec->snap_id == snap->id ?
2922 "mapped " : "",
Alex Elder9fcbb802012-08-23 23:48:49 -05002923 (unsigned long long) snap->id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002924
Alex Elder35938152012-08-02 11:29:46 -05002925 /* Done with this list entry; advance */
2926
2927 links = next;
2928 continue;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002929 }
Alex Elder35938152012-08-02 11:29:46 -05002930
Alex Elderb8b1e2d2012-07-03 16:01:19 -05002931 snap_name = rbd_dev_snap_info(rbd_dev, index,
2932 &snap_size, &snap_features);
Alex Eldercd892122012-07-03 16:01:19 -05002933 if (IS_ERR(snap_name))
2934 return PTR_ERR(snap_name);
2935
Alex Elder9fcbb802012-08-23 23:48:49 -05002936 dout("entry %u: snap_id = %llu\n", (unsigned int) snap_count,
2937 (unsigned long long) snap_id);
Alex Elder35938152012-08-02 11:29:46 -05002938 if (!snap || (snap_id != CEPH_NOSNAP && snap->id < snap_id)) {
2939 struct rbd_snap *new_snap;
2940
2941 /* We haven't seen this snapshot before */
2942
Alex Elderc8d18422012-07-10 20:30:11 -05002943 new_snap = __rbd_add_snap_dev(rbd_dev, snap_name,
Alex Eldercd892122012-07-03 16:01:19 -05002944 snap_id, snap_size, snap_features);
Alex Elder9fcbb802012-08-23 23:48:49 -05002945 if (IS_ERR(new_snap)) {
2946 int err = PTR_ERR(new_snap);
2947
2948 dout(" failed to add dev, error %d\n", err);
2949
2950 return err;
2951 }
Alex Elder35938152012-08-02 11:29:46 -05002952
2953 /* New goes before existing, or at end of list */
2954
Alex Elder9fcbb802012-08-23 23:48:49 -05002955 dout(" added dev%s\n", snap ? "" : " at end\n");
Alex Elder35938152012-08-02 11:29:46 -05002956 if (snap)
2957 list_add_tail(&new_snap->node, &snap->node);
2958 else
Alex Elder523f3252012-08-30 00:16:37 -05002959 list_add_tail(&new_snap->node, head);
Alex Elder35938152012-08-02 11:29:46 -05002960 } else {
2961 /* Already have this one */
2962
Alex Elder9fcbb802012-08-23 23:48:49 -05002963 dout(" already present\n");
2964
Alex Eldercd892122012-07-03 16:01:19 -05002965 rbd_assert(snap->size == snap_size);
Alex Elderaafb2302012-09-06 16:00:54 -05002966 rbd_assert(!strcmp(snap->name, snap_name));
Alex Eldercd892122012-07-03 16:01:19 -05002967 rbd_assert(snap->features == snap_features);
Alex Elder35938152012-08-02 11:29:46 -05002968
2969 /* Done with this list entry; advance */
2970
2971 links = links->next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002972 }
Alex Elder35938152012-08-02 11:29:46 -05002973
2974 /* Advance to the next entry in the snapshot context */
2975
2976 index++;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002977 }
Alex Elder9fcbb802012-08-23 23:48:49 -05002978 dout("%s: done\n", __func__);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002979
2980 return 0;
2981}
2982
Alex Elder304f6802012-08-31 17:29:52 -05002983/*
2984 * Scan the list of snapshots and register the devices for any that
2985 * have not already been registered.
2986 */
2987static int rbd_dev_snaps_register(struct rbd_device *rbd_dev)
2988{
2989 struct rbd_snap *snap;
2990 int ret = 0;
2991
2992 dout("%s called\n", __func__);
Alex Elder86ff77b2012-08-31 17:29:53 -05002993 if (WARN_ON(!device_is_registered(&rbd_dev->dev)))
2994 return -EIO;
Alex Elder304f6802012-08-31 17:29:52 -05002995
2996 list_for_each_entry(snap, &rbd_dev->snaps, node) {
2997 if (!rbd_snap_registered(snap)) {
2998 ret = rbd_register_snap_dev(snap, &rbd_dev->dev);
2999 if (ret < 0)
3000 break;
3001 }
3002 }
3003 dout("%s: returning %d\n", __func__, ret);
3004
3005 return ret;
3006}
3007
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003008static int rbd_bus_add_dev(struct rbd_device *rbd_dev)
3009{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003010 struct device *dev;
Alex Eldercd789ab2012-08-30 00:16:38 -05003011 int ret;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003012
3013 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003014
Alex Eldercd789ab2012-08-30 00:16:38 -05003015 dev = &rbd_dev->dev;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003016 dev->bus = &rbd_bus_type;
3017 dev->type = &rbd_device_type;
3018 dev->parent = &rbd_root_dev;
3019 dev->release = rbd_dev_release;
Alex Elderde71a292012-07-03 16:01:19 -05003020 dev_set_name(dev, "%d", rbd_dev->dev_id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003021 ret = device_register(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003022
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003023 mutex_unlock(&ctl_mutex);
Alex Eldercd789ab2012-08-30 00:16:38 -05003024
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003025 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003026}
3027
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003028static void rbd_bus_del_dev(struct rbd_device *rbd_dev)
3029{
3030 device_unregister(&rbd_dev->dev);
3031}
3032
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003033static int rbd_init_watch_dev(struct rbd_device *rbd_dev)
3034{
3035 int ret, rc;
3036
3037 do {
Alex Elder0e6f3222012-07-25 09:32:40 -05003038 ret = rbd_req_sync_watch(rbd_dev);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003039 if (ret == -ERANGE) {
Alex Elder117973f2012-08-31 17:29:55 -05003040 rc = rbd_dev_refresh(rbd_dev, NULL);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003041 if (rc < 0)
3042 return rc;
3043 }
3044 } while (ret == -ERANGE);
3045
3046 return ret;
3047}
3048
Alex Eldere2839302012-08-29 17:11:06 -05003049static atomic64_t rbd_dev_id_max = ATOMIC64_INIT(0);
Alex Elder1ddbe942012-01-29 13:57:44 -06003050
3051/*
Alex Elder499afd52012-02-02 08:13:29 -06003052 * Get a unique rbd identifier for the given new rbd_dev, and add
3053 * the rbd_dev to the global list. The minimum rbd id is 1.
Alex Elder1ddbe942012-01-29 13:57:44 -06003054 */
Alex Eldere2839302012-08-29 17:11:06 -05003055static void rbd_dev_id_get(struct rbd_device *rbd_dev)
Alex Elderb7f23c32012-01-29 13:57:43 -06003056{
Alex Eldere2839302012-08-29 17:11:06 -05003057 rbd_dev->dev_id = atomic64_inc_return(&rbd_dev_id_max);
Alex Elder499afd52012-02-02 08:13:29 -06003058
3059 spin_lock(&rbd_dev_list_lock);
3060 list_add_tail(&rbd_dev->node, &rbd_dev_list);
3061 spin_unlock(&rbd_dev_list_lock);
Alex Eldere2839302012-08-29 17:11:06 -05003062 dout("rbd_dev %p given dev id %llu\n", rbd_dev,
3063 (unsigned long long) rbd_dev->dev_id);
Alex Elder1ddbe942012-01-29 13:57:44 -06003064}
Alex Elderb7f23c32012-01-29 13:57:43 -06003065
Alex Elder1ddbe942012-01-29 13:57:44 -06003066/*
Alex Elder499afd52012-02-02 08:13:29 -06003067 * Remove an rbd_dev from the global list, and record that its
3068 * identifier is no longer in use.
Alex Elder1ddbe942012-01-29 13:57:44 -06003069 */
Alex Eldere2839302012-08-29 17:11:06 -05003070static void rbd_dev_id_put(struct rbd_device *rbd_dev)
Alex Elder1ddbe942012-01-29 13:57:44 -06003071{
Alex Elderd184f6b2012-01-29 13:57:44 -06003072 struct list_head *tmp;
Alex Elderde71a292012-07-03 16:01:19 -05003073 int rbd_id = rbd_dev->dev_id;
Alex Elderd184f6b2012-01-29 13:57:44 -06003074 int max_id;
3075
Alex Elderaafb2302012-09-06 16:00:54 -05003076 rbd_assert(rbd_id > 0);
Alex Elder499afd52012-02-02 08:13:29 -06003077
Alex Eldere2839302012-08-29 17:11:06 -05003078 dout("rbd_dev %p released dev id %llu\n", rbd_dev,
3079 (unsigned long long) rbd_dev->dev_id);
Alex Elder499afd52012-02-02 08:13:29 -06003080 spin_lock(&rbd_dev_list_lock);
3081 list_del_init(&rbd_dev->node);
Alex Elderd184f6b2012-01-29 13:57:44 -06003082
3083 /*
3084 * If the id being "put" is not the current maximum, there
3085 * is nothing special we need to do.
3086 */
Alex Eldere2839302012-08-29 17:11:06 -05003087 if (rbd_id != atomic64_read(&rbd_dev_id_max)) {
Alex Elderd184f6b2012-01-29 13:57:44 -06003088 spin_unlock(&rbd_dev_list_lock);
3089 return;
3090 }
3091
3092 /*
3093 * We need to update the current maximum id. Search the
3094 * list to find out what it is. We're more likely to find
3095 * the maximum at the end, so search the list backward.
3096 */
3097 max_id = 0;
3098 list_for_each_prev(tmp, &rbd_dev_list) {
3099 struct rbd_device *rbd_dev;
3100
3101 rbd_dev = list_entry(tmp, struct rbd_device, node);
Alex Elderb213e0b2012-10-10 21:19:13 -07003102 if (rbd_dev->dev_id > max_id)
3103 max_id = rbd_dev->dev_id;
Alex Elderd184f6b2012-01-29 13:57:44 -06003104 }
Alex Elder499afd52012-02-02 08:13:29 -06003105 spin_unlock(&rbd_dev_list_lock);
Alex Elderb7f23c32012-01-29 13:57:43 -06003106
Alex Elder1ddbe942012-01-29 13:57:44 -06003107 /*
Alex Eldere2839302012-08-29 17:11:06 -05003108 * The max id could have been updated by rbd_dev_id_get(), in
Alex Elderd184f6b2012-01-29 13:57:44 -06003109 * which case it now accurately reflects the new maximum.
3110 * Be careful not to overwrite the maximum value in that
3111 * case.
Alex Elder1ddbe942012-01-29 13:57:44 -06003112 */
Alex Eldere2839302012-08-29 17:11:06 -05003113 atomic64_cmpxchg(&rbd_dev_id_max, rbd_id, max_id);
3114 dout(" max dev id has been reset\n");
Alex Elderb7f23c32012-01-29 13:57:43 -06003115}
3116
Alex Eldera725f65e2012-02-02 08:13:30 -06003117/*
Alex Eldere28fff262012-02-02 08:13:30 -06003118 * Skips over white space at *buf, and updates *buf to point to the
3119 * first found non-space character (if any). Returns the length of
Alex Elder593a9e72012-02-07 12:03:37 -06003120 * the token (string of non-white space characters) found. Note
3121 * that *buf must be terminated with '\0'.
Alex Eldere28fff262012-02-02 08:13:30 -06003122 */
3123static inline size_t next_token(const char **buf)
3124{
3125 /*
3126 * These are the characters that produce nonzero for
3127 * isspace() in the "C" and "POSIX" locales.
3128 */
3129 const char *spaces = " \f\n\r\t\v";
3130
3131 *buf += strspn(*buf, spaces); /* Find start of token */
3132
3133 return strcspn(*buf, spaces); /* Return token length */
3134}
3135
3136/*
3137 * Finds the next token in *buf, and if the provided token buffer is
3138 * big enough, copies the found token into it. The result, if
Alex Elder593a9e72012-02-07 12:03:37 -06003139 * copied, is guaranteed to be terminated with '\0'. Note that *buf
3140 * must be terminated with '\0' on entry.
Alex Eldere28fff262012-02-02 08:13:30 -06003141 *
3142 * Returns the length of the token found (not including the '\0').
3143 * Return value will be 0 if no token is found, and it will be >=
3144 * token_size if the token would not fit.
3145 *
Alex Elder593a9e72012-02-07 12:03:37 -06003146 * The *buf pointer will be updated to point beyond the end of the
Alex Eldere28fff262012-02-02 08:13:30 -06003147 * found token. Note that this occurs even if the token buffer is
3148 * too small to hold it.
3149 */
3150static inline size_t copy_token(const char **buf,
3151 char *token,
3152 size_t token_size)
3153{
3154 size_t len;
3155
3156 len = next_token(buf);
3157 if (len < token_size) {
3158 memcpy(token, *buf, len);
3159 *(token + len) = '\0';
3160 }
3161 *buf += len;
3162
3163 return len;
3164}
3165
3166/*
Alex Elderea3352f2012-07-09 21:04:23 -05003167 * Finds the next token in *buf, dynamically allocates a buffer big
3168 * enough to hold a copy of it, and copies the token into the new
3169 * buffer. The copy is guaranteed to be terminated with '\0'. Note
3170 * that a duplicate buffer is created even for a zero-length token.
3171 *
3172 * Returns a pointer to the newly-allocated duplicate, or a null
3173 * pointer if memory for the duplicate was not available. If
3174 * the lenp argument is a non-null pointer, the length of the token
3175 * (not including the '\0') is returned in *lenp.
3176 *
3177 * If successful, the *buf pointer will be updated to point beyond
3178 * the end of the found token.
3179 *
3180 * Note: uses GFP_KERNEL for allocation.
3181 */
3182static inline char *dup_token(const char **buf, size_t *lenp)
3183{
3184 char *dup;
3185 size_t len;
3186
3187 len = next_token(buf);
Alex Elder4caf35f2012-11-01 08:39:27 -05003188 dup = kmemdup(*buf, len + 1, GFP_KERNEL);
Alex Elderea3352f2012-07-09 21:04:23 -05003189 if (!dup)
3190 return NULL;
Alex Elderea3352f2012-07-09 21:04:23 -05003191 *(dup + len) = '\0';
3192 *buf += len;
3193
3194 if (lenp)
3195 *lenp = len;
3196
3197 return dup;
3198}
3199
3200/*
Alex Elder859c31d2012-10-25 23:34:42 -05003201 * Parse the options provided for an "rbd add" (i.e., rbd image
3202 * mapping) request. These arrive via a write to /sys/bus/rbd/add,
3203 * and the data written is passed here via a NUL-terminated buffer.
3204 * Returns 0 if successful or an error code otherwise.
Alex Elderd22f76e2012-07-12 10:46:35 -05003205 *
Alex Elder859c31d2012-10-25 23:34:42 -05003206 * The information extracted from these options is recorded in
3207 * the other parameters which return dynamically-allocated
3208 * structures:
3209 * ceph_opts
3210 * The address of a pointer that will refer to a ceph options
3211 * structure. Caller must release the returned pointer using
3212 * ceph_destroy_options() when it is no longer needed.
3213 * rbd_opts
3214 * Address of an rbd options pointer. Fully initialized by
3215 * this function; caller must release with kfree().
3216 * spec
3217 * Address of an rbd image specification pointer. Fully
3218 * initialized by this function based on parsed options.
3219 * Caller must release with rbd_spec_put().
3220 *
3221 * The options passed take this form:
3222 * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
3223 * where:
3224 * <mon_addrs>
3225 * A comma-separated list of one or more monitor addresses.
3226 * A monitor address is an ip address, optionally followed
3227 * by a port number (separated by a colon).
3228 * I.e.: ip1[:port1][,ip2[:port2]...]
3229 * <options>
3230 * A comma-separated list of ceph and/or rbd options.
3231 * <pool_name>
3232 * The name of the rados pool containing the rbd image.
3233 * <image_name>
3234 * The name of the image in that pool to map.
3235 * <snap_id>
3236 * An optional snapshot id. If provided, the mapping will
3237 * present data from the image at the time that snapshot was
3238 * created. The image head is used if no snapshot id is
3239 * provided. Snapshot mappings are always read-only.
Alex Eldera725f65e2012-02-02 08:13:30 -06003240 */
Alex Elder859c31d2012-10-25 23:34:42 -05003241static int rbd_add_parse_args(const char *buf,
Alex Elderdc79b112012-10-25 23:34:41 -05003242 struct ceph_options **ceph_opts,
Alex Elder859c31d2012-10-25 23:34:42 -05003243 struct rbd_options **opts,
3244 struct rbd_spec **rbd_spec)
Alex Eldera725f65e2012-02-02 08:13:30 -06003245{
Alex Elderd22f76e2012-07-12 10:46:35 -05003246 size_t len;
Alex Elder859c31d2012-10-25 23:34:42 -05003247 char *options;
Alex Elder0ddebc02012-10-25 23:34:41 -05003248 const char *mon_addrs;
3249 size_t mon_addrs_size;
Alex Elder859c31d2012-10-25 23:34:42 -05003250 struct rbd_spec *spec = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05003251 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05003252 struct ceph_options *copts;
Alex Elderdc79b112012-10-25 23:34:41 -05003253 int ret;
Alex Eldere28fff262012-02-02 08:13:30 -06003254
3255 /* The first four tokens are required */
3256
Alex Elder7ef32142012-02-02 08:13:30 -06003257 len = next_token(&buf);
Alex Elder4fb5d6712012-11-01 10:17:15 -05003258 if (!len) {
3259 rbd_warn(NULL, "no monitor address(es) provided");
3260 return -EINVAL;
3261 }
Alex Elder0ddebc02012-10-25 23:34:41 -05003262 mon_addrs = buf;
Alex Elderf28e5652012-10-25 23:34:41 -05003263 mon_addrs_size = len + 1;
Alex Elder7ef32142012-02-02 08:13:30 -06003264 buf += len;
Alex Eldera725f65e2012-02-02 08:13:30 -06003265
Alex Elderdc79b112012-10-25 23:34:41 -05003266 ret = -EINVAL;
Alex Elderf28e5652012-10-25 23:34:41 -05003267 options = dup_token(&buf, NULL);
3268 if (!options)
Alex Elderdc79b112012-10-25 23:34:41 -05003269 return -ENOMEM;
Alex Elder4fb5d6712012-11-01 10:17:15 -05003270 if (!*options) {
3271 rbd_warn(NULL, "no options provided");
3272 goto out_err;
3273 }
Alex Eldera725f65e2012-02-02 08:13:30 -06003274
Alex Elder859c31d2012-10-25 23:34:42 -05003275 spec = rbd_spec_alloc();
3276 if (!spec)
Alex Elderf28e5652012-10-25 23:34:41 -05003277 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05003278
3279 spec->pool_name = dup_token(&buf, NULL);
3280 if (!spec->pool_name)
3281 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05003282 if (!*spec->pool_name) {
3283 rbd_warn(NULL, "no pool name provided");
3284 goto out_err;
3285 }
Alex Eldere28fff262012-02-02 08:13:30 -06003286
Alex Elder69e7a022012-11-01 08:39:26 -05003287 spec->image_name = dup_token(&buf, NULL);
Alex Elder859c31d2012-10-25 23:34:42 -05003288 if (!spec->image_name)
Alex Elderf28e5652012-10-25 23:34:41 -05003289 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05003290 if (!*spec->image_name) {
3291 rbd_warn(NULL, "no image name provided");
3292 goto out_err;
3293 }
Alex Eldere28fff262012-02-02 08:13:30 -06003294
Alex Elderf28e5652012-10-25 23:34:41 -05003295 /*
3296 * Snapshot name is optional; default is to use "-"
3297 * (indicating the head/no snapshot).
3298 */
Alex Elder3feeb8942012-08-31 17:29:52 -05003299 len = next_token(&buf);
Alex Elder820a5f32012-07-09 21:04:24 -05003300 if (!len) {
Alex Elder3feeb8942012-08-31 17:29:52 -05003301 buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
3302 len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
Alex Elderf28e5652012-10-25 23:34:41 -05003303 } else if (len > RBD_MAX_SNAP_NAME_LEN) {
Alex Elderdc79b112012-10-25 23:34:41 -05003304 ret = -ENAMETOOLONG;
Alex Elderf28e5652012-10-25 23:34:41 -05003305 goto out_err;
Alex Elder849b4262012-07-09 21:04:24 -05003306 }
Alex Elder4caf35f2012-11-01 08:39:27 -05003307 spec->snap_name = kmemdup(buf, len + 1, GFP_KERNEL);
Alex Elder859c31d2012-10-25 23:34:42 -05003308 if (!spec->snap_name)
Alex Elderf28e5652012-10-25 23:34:41 -05003309 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05003310 *(spec->snap_name + len) = '\0';
Alex Eldere5c35532012-10-25 23:34:41 -05003311
Alex Elder0ddebc02012-10-25 23:34:41 -05003312 /* Initialize all rbd options to the defaults */
Alex Eldere28fff262012-02-02 08:13:30 -06003313
Alex Elder4e9afeb2012-10-25 23:34:41 -05003314 rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL);
3315 if (!rbd_opts)
3316 goto out_mem;
3317
3318 rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
Alex Elderd22f76e2012-07-12 10:46:35 -05003319
Alex Elder859c31d2012-10-25 23:34:42 -05003320 copts = ceph_parse_options(options, mon_addrs,
Alex Elder0ddebc02012-10-25 23:34:41 -05003321 mon_addrs + mon_addrs_size - 1,
Alex Elder4e9afeb2012-10-25 23:34:41 -05003322 parse_rbd_opts_token, rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05003323 if (IS_ERR(copts)) {
3324 ret = PTR_ERR(copts);
Alex Elderdc79b112012-10-25 23:34:41 -05003325 goto out_err;
3326 }
Alex Elder859c31d2012-10-25 23:34:42 -05003327 kfree(options);
3328
3329 *ceph_opts = copts;
Alex Elder4e9afeb2012-10-25 23:34:41 -05003330 *opts = rbd_opts;
Alex Elder859c31d2012-10-25 23:34:42 -05003331 *rbd_spec = spec;
Alex Elder0ddebc02012-10-25 23:34:41 -05003332
Alex Elderdc79b112012-10-25 23:34:41 -05003333 return 0;
Alex Elderf28e5652012-10-25 23:34:41 -05003334out_mem:
Alex Elderdc79b112012-10-25 23:34:41 -05003335 ret = -ENOMEM;
Alex Elderd22f76e2012-07-12 10:46:35 -05003336out_err:
Alex Elder859c31d2012-10-25 23:34:42 -05003337 kfree(rbd_opts);
3338 rbd_spec_put(spec);
Alex Elderf28e5652012-10-25 23:34:41 -05003339 kfree(options);
Alex Elderd22f76e2012-07-12 10:46:35 -05003340
Alex Elderdc79b112012-10-25 23:34:41 -05003341 return ret;
Alex Eldera725f65e2012-02-02 08:13:30 -06003342}
3343
Alex Elder589d30e2012-07-10 20:30:11 -05003344/*
3345 * An rbd format 2 image has a unique identifier, distinct from the
3346 * name given to it by the user. Internally, that identifier is
3347 * what's used to specify the names of objects related to the image.
3348 *
3349 * A special "rbd id" object is used to map an rbd image name to its
3350 * id. If that object doesn't exist, then there is no v2 rbd image
3351 * with the supplied name.
3352 *
3353 * This function will record the given rbd_dev's image_id field if
3354 * it can be determined, and in that case will return 0. If any
3355 * errors occur a negative errno will be returned and the rbd_dev's
3356 * image_id field will be unchanged (and should be NULL).
3357 */
3358static int rbd_dev_image_id(struct rbd_device *rbd_dev)
3359{
3360 int ret;
3361 size_t size;
3362 char *object_name;
3363 void *response;
3364 void *p;
3365
3366 /*
Alex Elder2c0d0a12012-10-30 19:40:33 -05003367 * When probing a parent image, the image id is already
3368 * known (and the image name likely is not). There's no
3369 * need to fetch the image id again in this case.
3370 */
3371 if (rbd_dev->spec->image_id)
3372 return 0;
3373
3374 /*
Alex Elder589d30e2012-07-10 20:30:11 -05003375 * First, see if the format 2 image id file exists, and if
3376 * so, get the image's persistent id from it.
3377 */
Alex Elder69e7a022012-11-01 08:39:26 -05003378 size = sizeof (RBD_ID_PREFIX) + strlen(rbd_dev->spec->image_name);
Alex Elder589d30e2012-07-10 20:30:11 -05003379 object_name = kmalloc(size, GFP_NOIO);
3380 if (!object_name)
3381 return -ENOMEM;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003382 sprintf(object_name, "%s%s", RBD_ID_PREFIX, rbd_dev->spec->image_name);
Alex Elder589d30e2012-07-10 20:30:11 -05003383 dout("rbd id object name is %s\n", object_name);
3384
3385 /* Response will be an encoded string, which includes a length */
3386
3387 size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
3388 response = kzalloc(size, GFP_NOIO);
3389 if (!response) {
3390 ret = -ENOMEM;
3391 goto out;
3392 }
3393
3394 ret = rbd_req_sync_exec(rbd_dev, object_name,
3395 "rbd", "get_id",
3396 NULL, 0,
3397 response, RBD_IMAGE_ID_LEN_MAX,
3398 CEPH_OSD_FLAG_READ, NULL);
3399 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
3400 if (ret < 0)
3401 goto out;
Alex Eldera0ea3a42012-10-10 21:19:13 -07003402 ret = 0; /* rbd_req_sync_exec() can return positive */
Alex Elder589d30e2012-07-10 20:30:11 -05003403
3404 p = response;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003405 rbd_dev->spec->image_id = ceph_extract_encoded_string(&p,
Alex Elder589d30e2012-07-10 20:30:11 -05003406 p + RBD_IMAGE_ID_LEN_MAX,
Alex Elder979ed482012-11-01 08:39:26 -05003407 NULL, GFP_NOIO);
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003408 if (IS_ERR(rbd_dev->spec->image_id)) {
3409 ret = PTR_ERR(rbd_dev->spec->image_id);
3410 rbd_dev->spec->image_id = NULL;
Alex Elder589d30e2012-07-10 20:30:11 -05003411 } else {
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003412 dout("image_id is %s\n", rbd_dev->spec->image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05003413 }
3414out:
3415 kfree(response);
3416 kfree(object_name);
3417
3418 return ret;
3419}
3420
Alex Eldera30b71b2012-07-10 20:30:11 -05003421static int rbd_dev_v1_probe(struct rbd_device *rbd_dev)
3422{
3423 int ret;
3424 size_t size;
3425
3426 /* Version 1 images have no id; empty string is used */
3427
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003428 rbd_dev->spec->image_id = kstrdup("", GFP_KERNEL);
3429 if (!rbd_dev->spec->image_id)
Alex Eldera30b71b2012-07-10 20:30:11 -05003430 return -ENOMEM;
Alex Eldera30b71b2012-07-10 20:30:11 -05003431
3432 /* Record the header object name for this rbd image. */
3433
Alex Elder69e7a022012-11-01 08:39:26 -05003434 size = strlen(rbd_dev->spec->image_name) + sizeof (RBD_SUFFIX);
Alex Eldera30b71b2012-07-10 20:30:11 -05003435 rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
3436 if (!rbd_dev->header_name) {
3437 ret = -ENOMEM;
3438 goto out_err;
3439 }
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003440 sprintf(rbd_dev->header_name, "%s%s",
3441 rbd_dev->spec->image_name, RBD_SUFFIX);
Alex Eldera30b71b2012-07-10 20:30:11 -05003442
3443 /* Populate rbd image metadata */
3444
3445 ret = rbd_read_header(rbd_dev, &rbd_dev->header);
3446 if (ret < 0)
3447 goto out_err;
Alex Elder86b00e02012-10-25 23:34:42 -05003448
3449 /* Version 1 images have no parent (no layering) */
3450
3451 rbd_dev->parent_spec = NULL;
3452 rbd_dev->parent_overlap = 0;
3453
Alex Eldera30b71b2012-07-10 20:30:11 -05003454 rbd_dev->image_format = 1;
3455
3456 dout("discovered version 1 image, header name is %s\n",
3457 rbd_dev->header_name);
3458
3459 return 0;
3460
3461out_err:
3462 kfree(rbd_dev->header_name);
3463 rbd_dev->header_name = NULL;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003464 kfree(rbd_dev->spec->image_id);
3465 rbd_dev->spec->image_id = NULL;
Alex Eldera30b71b2012-07-10 20:30:11 -05003466
3467 return ret;
3468}
3469
3470static int rbd_dev_v2_probe(struct rbd_device *rbd_dev)
3471{
3472 size_t size;
Alex Elder9d475de2012-07-03 16:01:19 -05003473 int ret;
Alex Elder6e14b1a2012-07-03 16:01:19 -05003474 u64 ver = 0;
Alex Eldera30b71b2012-07-10 20:30:11 -05003475
3476 /*
3477 * Image id was filled in by the caller. Record the header
3478 * object name for this rbd image.
3479 */
Alex Elder979ed482012-11-01 08:39:26 -05003480 size = sizeof (RBD_HEADER_PREFIX) + strlen(rbd_dev->spec->image_id);
Alex Eldera30b71b2012-07-10 20:30:11 -05003481 rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
3482 if (!rbd_dev->header_name)
3483 return -ENOMEM;
3484 sprintf(rbd_dev->header_name, "%s%s",
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003485 RBD_HEADER_PREFIX, rbd_dev->spec->image_id);
Alex Elder9d475de2012-07-03 16:01:19 -05003486
3487 /* Get the size and object order for the image */
3488
3489 ret = rbd_dev_v2_image_size(rbd_dev);
3490 if (ret < 0)
3491 goto out_err;
Alex Elder1e130192012-07-03 16:01:19 -05003492
3493 /* Get the object prefix (a.k.a. block_name) for the image */
3494
3495 ret = rbd_dev_v2_object_prefix(rbd_dev);
3496 if (ret < 0)
3497 goto out_err;
Alex Elderb1b54022012-07-03 16:01:19 -05003498
Alex Elderd8891402012-10-09 13:50:17 -07003499 /* Get the and check features for the image */
Alex Elderb1b54022012-07-03 16:01:19 -05003500
3501 ret = rbd_dev_v2_features(rbd_dev);
3502 if (ret < 0)
3503 goto out_err;
Alex Elder35d489f2012-07-03 16:01:19 -05003504
Alex Elder86b00e02012-10-25 23:34:42 -05003505 /* If the image supports layering, get the parent info */
3506
3507 if (rbd_dev->header.features & RBD_FEATURE_LAYERING) {
3508 ret = rbd_dev_v2_parent_info(rbd_dev);
3509 if (ret < 0)
3510 goto out_err;
3511 }
3512
Alex Elder6e14b1a2012-07-03 16:01:19 -05003513 /* crypto and compression type aren't (yet) supported for v2 images */
Alex Elder35d489f2012-07-03 16:01:19 -05003514
Alex Elder6e14b1a2012-07-03 16:01:19 -05003515 rbd_dev->header.crypt_type = 0;
3516 rbd_dev->header.comp_type = 0;
3517
3518 /* Get the snapshot context, plus the header version */
3519
3520 ret = rbd_dev_v2_snap_context(rbd_dev, &ver);
Alex Elder35d489f2012-07-03 16:01:19 -05003521 if (ret)
3522 goto out_err;
Alex Elder6e14b1a2012-07-03 16:01:19 -05003523 rbd_dev->header.obj_version = ver;
3524
Alex Eldera30b71b2012-07-10 20:30:11 -05003525 rbd_dev->image_format = 2;
3526
3527 dout("discovered version 2 image, header name is %s\n",
3528 rbd_dev->header_name);
3529
Alex Elder35152972012-08-31 17:29:55 -05003530 return 0;
Alex Elder9d475de2012-07-03 16:01:19 -05003531out_err:
Alex Elder86b00e02012-10-25 23:34:42 -05003532 rbd_dev->parent_overlap = 0;
3533 rbd_spec_put(rbd_dev->parent_spec);
3534 rbd_dev->parent_spec = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05003535 kfree(rbd_dev->header_name);
3536 rbd_dev->header_name = NULL;
Alex Elder1e130192012-07-03 16:01:19 -05003537 kfree(rbd_dev->header.object_prefix);
3538 rbd_dev->header.object_prefix = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05003539
3540 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05003541}
3542
Alex Elder83a06262012-10-30 15:47:17 -05003543static int rbd_dev_probe_finish(struct rbd_device *rbd_dev)
3544{
3545 int ret;
3546
3547 /* no need to lock here, as rbd_dev is not registered yet */
3548 ret = rbd_dev_snaps_update(rbd_dev);
3549 if (ret)
3550 return ret;
3551
Alex Elder9e15b772012-10-30 19:40:33 -05003552 ret = rbd_dev_probe_update_spec(rbd_dev);
3553 if (ret)
3554 goto err_out_snaps;
3555
Alex Elder83a06262012-10-30 15:47:17 -05003556 ret = rbd_dev_set_mapping(rbd_dev);
3557 if (ret)
3558 goto err_out_snaps;
3559
3560 /* generate unique id: find highest unique id, add one */
3561 rbd_dev_id_get(rbd_dev);
3562
3563 /* Fill in the device name, now that we have its id. */
3564 BUILD_BUG_ON(DEV_NAME_LEN
3565 < sizeof (RBD_DRV_NAME) + MAX_INT_FORMAT_WIDTH);
3566 sprintf(rbd_dev->name, "%s%d", RBD_DRV_NAME, rbd_dev->dev_id);
3567
3568 /* Get our block major device number. */
3569
3570 ret = register_blkdev(0, rbd_dev->name);
3571 if (ret < 0)
3572 goto err_out_id;
3573 rbd_dev->major = ret;
3574
3575 /* Set up the blkdev mapping. */
3576
3577 ret = rbd_init_disk(rbd_dev);
3578 if (ret)
3579 goto err_out_blkdev;
3580
3581 ret = rbd_bus_add_dev(rbd_dev);
3582 if (ret)
3583 goto err_out_disk;
3584
3585 /*
3586 * At this point cleanup in the event of an error is the job
3587 * of the sysfs code (initiated by rbd_bus_del_dev()).
3588 */
3589 down_write(&rbd_dev->header_rwsem);
3590 ret = rbd_dev_snaps_register(rbd_dev);
3591 up_write(&rbd_dev->header_rwsem);
3592 if (ret)
3593 goto err_out_bus;
3594
3595 ret = rbd_init_watch_dev(rbd_dev);
3596 if (ret)
3597 goto err_out_bus;
3598
3599 /* Everything's ready. Announce the disk to the world. */
3600
3601 add_disk(rbd_dev->disk);
3602
3603 pr_info("%s: added with size 0x%llx\n", rbd_dev->disk->disk_name,
3604 (unsigned long long) rbd_dev->mapping.size);
3605
3606 return ret;
3607err_out_bus:
3608 /* this will also clean up rest of rbd_dev stuff */
3609
3610 rbd_bus_del_dev(rbd_dev);
3611
3612 return ret;
3613err_out_disk:
3614 rbd_free_disk(rbd_dev);
3615err_out_blkdev:
3616 unregister_blkdev(rbd_dev->major, rbd_dev->name);
3617err_out_id:
3618 rbd_dev_id_put(rbd_dev);
3619err_out_snaps:
3620 rbd_remove_all_snaps(rbd_dev);
3621
3622 return ret;
3623}
3624
Alex Eldera30b71b2012-07-10 20:30:11 -05003625/*
3626 * Probe for the existence of the header object for the given rbd
3627 * device. For format 2 images this includes determining the image
3628 * id.
3629 */
3630static int rbd_dev_probe(struct rbd_device *rbd_dev)
3631{
3632 int ret;
3633
3634 /*
3635 * Get the id from the image id object. If it's not a
3636 * format 2 image, we'll get ENOENT back, and we'll assume
3637 * it's a format 1 image.
3638 */
3639 ret = rbd_dev_image_id(rbd_dev);
3640 if (ret)
3641 ret = rbd_dev_v1_probe(rbd_dev);
3642 else
3643 ret = rbd_dev_v2_probe(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05003644 if (ret) {
Alex Eldera30b71b2012-07-10 20:30:11 -05003645 dout("probe failed, returning %d\n", ret);
3646
Alex Elder83a06262012-10-30 15:47:17 -05003647 return ret;
3648 }
3649
3650 ret = rbd_dev_probe_finish(rbd_dev);
3651 if (ret)
3652 rbd_header_free(&rbd_dev->header);
3653
Alex Eldera30b71b2012-07-10 20:30:11 -05003654 return ret;
3655}
3656
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003657static ssize_t rbd_add(struct bus_type *bus,
3658 const char *buf,
3659 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003660{
Alex Eldercb8627c2012-07-09 21:04:23 -05003661 struct rbd_device *rbd_dev = NULL;
Alex Elderdc79b112012-10-25 23:34:41 -05003662 struct ceph_options *ceph_opts = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05003663 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05003664 struct rbd_spec *spec = NULL;
Alex Elder9d3997f2012-10-25 23:34:42 -05003665 struct rbd_client *rbdc;
Alex Elder27cc2592012-02-02 08:13:30 -06003666 struct ceph_osd_client *osdc;
3667 int rc = -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003668
3669 if (!try_module_get(THIS_MODULE))
3670 return -ENODEV;
3671
Alex Eldera725f65e2012-02-02 08:13:30 -06003672 /* parse add command */
Alex Elder859c31d2012-10-25 23:34:42 -05003673 rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec);
Alex Elderdc79b112012-10-25 23:34:41 -05003674 if (rc < 0)
Alex Elderbd4ba652012-10-25 23:34:42 -05003675 goto err_out_module;
Alex Eldera725f65e2012-02-02 08:13:30 -06003676
Alex Elder9d3997f2012-10-25 23:34:42 -05003677 rbdc = rbd_get_client(ceph_opts);
3678 if (IS_ERR(rbdc)) {
3679 rc = PTR_ERR(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05003680 goto err_out_args;
Alex Elder9d3997f2012-10-25 23:34:42 -05003681 }
Alex Elderc53d5892012-10-25 23:34:42 -05003682 ceph_opts = NULL; /* rbd_dev client now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003683
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003684 /* pick the pool */
Alex Elder9d3997f2012-10-25 23:34:42 -05003685 osdc = &rbdc->client->osdc;
Alex Elder859c31d2012-10-25 23:34:42 -05003686 rc = ceph_pg_poolid_by_name(osdc->osdmap, spec->pool_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003687 if (rc < 0)
3688 goto err_out_client;
Alex Elder859c31d2012-10-25 23:34:42 -05003689 spec->pool_id = (u64) rc;
3690
Alex Elderc53d5892012-10-25 23:34:42 -05003691 rbd_dev = rbd_dev_create(rbdc, spec);
Alex Elderbd4ba652012-10-25 23:34:42 -05003692 if (!rbd_dev)
3693 goto err_out_client;
Alex Elderc53d5892012-10-25 23:34:42 -05003694 rbdc = NULL; /* rbd_dev now owns this */
3695 spec = NULL; /* rbd_dev now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003696
Alex Elderbd4ba652012-10-25 23:34:42 -05003697 rbd_dev->mapping.read_only = rbd_opts->read_only;
Alex Elderc53d5892012-10-25 23:34:42 -05003698 kfree(rbd_opts);
3699 rbd_opts = NULL; /* done with this */
Alex Elderbd4ba652012-10-25 23:34:42 -05003700
Alex Eldera30b71b2012-07-10 20:30:11 -05003701 rc = rbd_dev_probe(rbd_dev);
3702 if (rc < 0)
Alex Elderc53d5892012-10-25 23:34:42 -05003703 goto err_out_rbd_dev;
Alex Elder05fd6f62012-08-29 17:11:07 -05003704
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003705 return count;
Alex Elderc53d5892012-10-25 23:34:42 -05003706err_out_rbd_dev:
3707 rbd_dev_destroy(rbd_dev);
Alex Elderbd4ba652012-10-25 23:34:42 -05003708err_out_client:
Alex Elder9d3997f2012-10-25 23:34:42 -05003709 rbd_put_client(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05003710err_out_args:
Alex Elder78cea762012-10-25 23:34:41 -05003711 if (ceph_opts)
3712 ceph_destroy_options(ceph_opts);
Alex Elder4e9afeb2012-10-25 23:34:41 -05003713 kfree(rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05003714 rbd_spec_put(spec);
Alex Elderbd4ba652012-10-25 23:34:42 -05003715err_out_module:
3716 module_put(THIS_MODULE);
Alex Elder27cc2592012-02-02 08:13:30 -06003717
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003718 dout("Error adding device %s\n", buf);
Alex Elder27cc2592012-02-02 08:13:30 -06003719
3720 return (ssize_t) rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003721}
3722
Alex Elderde71a292012-07-03 16:01:19 -05003723static struct rbd_device *__rbd_get_dev(unsigned long dev_id)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003724{
3725 struct list_head *tmp;
3726 struct rbd_device *rbd_dev;
3727
Alex Eldere124a82f2012-01-29 13:57:44 -06003728 spin_lock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003729 list_for_each(tmp, &rbd_dev_list) {
3730 rbd_dev = list_entry(tmp, struct rbd_device, node);
Alex Elderde71a292012-07-03 16:01:19 -05003731 if (rbd_dev->dev_id == dev_id) {
Alex Eldere124a82f2012-01-29 13:57:44 -06003732 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003733 return rbd_dev;
Alex Eldere124a82f2012-01-29 13:57:44 -06003734 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003735 }
Alex Eldere124a82f2012-01-29 13:57:44 -06003736 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003737 return NULL;
3738}
3739
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003740static void rbd_dev_release(struct device *dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003741{
Alex Elder593a9e72012-02-07 12:03:37 -06003742 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003743
Alex Elder1dbb4392012-01-24 10:08:37 -06003744 if (rbd_dev->watch_request) {
3745 struct ceph_client *client = rbd_dev->rbd_client->client;
3746
3747 ceph_osdc_unregister_linger_request(&client->osdc,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003748 rbd_dev->watch_request);
Alex Elder1dbb4392012-01-24 10:08:37 -06003749 }
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003750 if (rbd_dev->watch_event)
Alex Elder070c6332012-07-25 09:32:41 -05003751 rbd_req_sync_unwatch(rbd_dev);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003752
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003753
3754 /* clean up and free blkdev */
3755 rbd_free_disk(rbd_dev);
3756 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Alex Elder32eec682012-02-08 16:11:14 -06003757
Alex Elder2ac4e752012-07-10 20:30:10 -05003758 /* release allocated disk header fields */
3759 rbd_header_free(&rbd_dev->header);
3760
Alex Elder32eec682012-02-08 16:11:14 -06003761 /* done with the id, and with the rbd_dev */
Alex Eldere2839302012-08-29 17:11:06 -05003762 rbd_dev_id_put(rbd_dev);
Alex Elderc53d5892012-10-25 23:34:42 -05003763 rbd_assert(rbd_dev->rbd_client != NULL);
3764 rbd_dev_destroy(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003765
3766 /* release module ref */
3767 module_put(THIS_MODULE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003768}
3769
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003770static ssize_t rbd_remove(struct bus_type *bus,
3771 const char *buf,
3772 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003773{
3774 struct rbd_device *rbd_dev = NULL;
3775 int target_id, rc;
3776 unsigned long ul;
3777 int ret = count;
3778
3779 rc = strict_strtoul(buf, 10, &ul);
3780 if (rc)
3781 return rc;
3782
3783 /* convert to int; abort if we lost anything in the conversion */
3784 target_id = (int) ul;
3785 if (target_id != ul)
3786 return -EINVAL;
3787
3788 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
3789
3790 rbd_dev = __rbd_get_dev(target_id);
3791 if (!rbd_dev) {
3792 ret = -ENOENT;
3793 goto done;
3794 }
3795
Alex Elder42382b72012-11-16 09:29:16 -06003796 if (rbd_dev->open_count) {
3797 ret = -EBUSY;
3798 goto done;
3799 }
3800
Alex Elder41f38c22012-10-25 23:34:40 -05003801 rbd_remove_all_snaps(rbd_dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003802 rbd_bus_del_dev(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003803
3804done:
3805 mutex_unlock(&ctl_mutex);
Alex Elderaafb2302012-09-06 16:00:54 -05003806
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003807 return ret;
3808}
3809
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003810/*
3811 * create control files in sysfs
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003812 * /sys/bus/rbd/...
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003813 */
3814static int rbd_sysfs_init(void)
3815{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003816 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003817
Alex Elderfed4c142012-02-07 12:03:36 -06003818 ret = device_register(&rbd_root_dev);
Alex Elder21079782012-01-24 10:08:36 -06003819 if (ret < 0)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003820 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003821
Alex Elderfed4c142012-02-07 12:03:36 -06003822 ret = bus_register(&rbd_bus_type);
3823 if (ret < 0)
3824 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003825
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003826 return ret;
3827}
3828
3829static void rbd_sysfs_cleanup(void)
3830{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003831 bus_unregister(&rbd_bus_type);
Alex Elderfed4c142012-02-07 12:03:36 -06003832 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003833}
3834
3835int __init rbd_init(void)
3836{
3837 int rc;
3838
3839 rc = rbd_sysfs_init();
3840 if (rc)
3841 return rc;
Alex Elderf0f8cef2012-01-29 13:57:44 -06003842 pr_info("loaded " RBD_DRV_NAME_LONG "\n");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003843 return 0;
3844}
3845
3846void __exit rbd_exit(void)
3847{
3848 rbd_sysfs_cleanup();
3849}
3850
3851module_init(rbd_init);
3852module_exit(rbd_exit);
3853
3854MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
3855MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
3856MODULE_DESCRIPTION("rados block device");
3857
3858/* following authorship retained from original osdblk.c */
3859MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
3860
3861MODULE_LICENSE("GPL");