blob: 88de8ccb29bd2e68629832f74cd4e12bc15aad2a [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
Alex Elder0ec8ce82012-11-09 08:43:15 -060057#define U32_MAX ((u32) (~0U))
Alex Elderdf111be2012-08-09 10:33:26 -070058#define U64_MAX ((u64) (~0ULL))
59
Alex Elderf0f8cef2012-01-29 13:57:44 -060060#define RBD_DRV_NAME "rbd"
61#define RBD_DRV_NAME_LONG "rbd (rados block device)"
Yehuda Sadeh602adf42010-08-12 16:11:25 -070062
63#define RBD_MINORS_PER_MAJOR 256 /* max minors per blkdev */
64
Alex Elderd4b125e2012-07-03 16:01:19 -050065#define RBD_SNAP_DEV_NAME_PREFIX "snap_"
66#define RBD_MAX_SNAP_NAME_LEN \
67 (NAME_MAX - (sizeof (RBD_SNAP_DEV_NAME_PREFIX) - 1))
68
Alex Elder35d489f2012-07-03 16:01:19 -050069#define RBD_MAX_SNAP_COUNT 510 /* allows max snapc to fit in 4KB */
Yehuda Sadeh602adf42010-08-12 16:11:25 -070070#define RBD_MAX_OPT_LEN 1024
71
72#define RBD_SNAP_HEAD_NAME "-"
73
Alex Elder9e15b772012-10-30 19:40:33 -050074/* This allows a single page to hold an image name sent by OSD */
75#define RBD_IMAGE_NAME_LEN_MAX (PAGE_SIZE - sizeof (__le32) - 1)
Alex Elder1e130192012-07-03 16:01:19 -050076#define RBD_IMAGE_ID_LEN_MAX 64
Alex Elder9e15b772012-10-30 19:40:33 -050077
Alex Elder1e130192012-07-03 16:01:19 -050078#define RBD_OBJ_PREFIX_LEN_MAX 64
Alex Elder589d30e2012-07-10 20:30:11 -050079
Alex Elderd8891402012-10-09 13:50:17 -070080/* Feature bits */
81
82#define RBD_FEATURE_LAYERING 1
83
84/* Features supported by this (client software) implementation. */
85
86#define RBD_FEATURES_ALL (0)
87
Alex Elder81a89792012-02-02 08:13:30 -060088/*
89 * An RBD device name will be "rbd#", where the "rbd" comes from
90 * RBD_DRV_NAME above, and # is a unique integer identifier.
91 * MAX_INT_FORMAT_WIDTH is used in ensuring DEV_NAME_LEN is big
92 * enough to hold all possible device names.
93 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -070094#define DEV_NAME_LEN 32
Alex Elder81a89792012-02-02 08:13:30 -060095#define MAX_INT_FORMAT_WIDTH ((5 * sizeof (int)) / 2 + 1)
Yehuda Sadeh602adf42010-08-12 16:11:25 -070096
Alex Eldercc0538b2012-08-10 13:12:07 -070097#define RBD_READ_ONLY_DEFAULT false
Yehuda Sadeh59c2be12011-03-21 15:10:11 -070098
Yehuda Sadeh602adf42010-08-12 16:11:25 -070099/*
100 * block device image metadata (in-memory version)
101 */
102struct rbd_image_header {
Alex Elderf84344f2012-08-31 17:29:51 -0500103 /* These four fields never change for a given rbd image */
Alex Elder849b4262012-07-09 21:04:24 -0500104 char *object_prefix;
Alex Elder34b13182012-07-13 20:35:12 -0500105 u64 features;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700106 __u8 obj_order;
107 __u8 crypt_type;
108 __u8 comp_type;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700109
Alex Elderf84344f2012-08-31 17:29:51 -0500110 /* The remaining fields need to be updated occasionally */
111 u64 image_size;
112 struct ceph_snap_context *snapc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700113 char *snap_names;
114 u64 *snap_sizes;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700115
116 u64 obj_version;
117};
118
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500119/*
120 * An rbd image specification.
121 *
122 * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely
Alex Elderc66c6e02012-11-01 08:39:26 -0500123 * identify an image. Each rbd_dev structure includes a pointer to
124 * an rbd_spec structure that encapsulates this identity.
125 *
126 * Each of the id's in an rbd_spec has an associated name. For a
127 * user-mapped image, the names are supplied and the id's associated
128 * with them are looked up. For a layered image, a parent image is
129 * defined by the tuple, and the names are looked up.
130 *
131 * An rbd_dev structure contains a parent_spec pointer which is
132 * non-null if the image it represents is a child in a layered
133 * image. This pointer will refer to the rbd_spec structure used
134 * by the parent rbd_dev for its own identity (i.e., the structure
135 * is shared between the parent and child).
136 *
137 * Since these structures are populated once, during the discovery
138 * phase of image construction, they are effectively immutable so
139 * we make no effort to synchronize access to them.
140 *
141 * Note that code herein does not assume the image name is known (it
142 * could be a null pointer).
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500143 */
144struct rbd_spec {
145 u64 pool_id;
146 char *pool_name;
147
148 char *image_id;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500149 char *image_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500150
151 u64 snap_id;
152 char *snap_name;
153
154 struct kref kref;
155};
156
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700157struct rbd_options {
Alex Eldercc0538b2012-08-10 13:12:07 -0700158 bool read_only;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700159};
160
161/*
Alex Elderf0f8cef2012-01-29 13:57:44 -0600162 * an instance of the client. multiple devices may share an rbd client.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700163 */
164struct rbd_client {
165 struct ceph_client *client;
166 struct kref kref;
167 struct list_head node;
168};
169
170/*
Alex Elderf0f8cef2012-01-29 13:57:44 -0600171 * a request completion status
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700172 */
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700173struct rbd_req_status {
174 int done;
Alex Elder8986cb32012-11-08 08:01:39 -0600175 s32 rc;
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700176 u64 bytes;
177};
178
179/*
180 * a collection of requests
181 */
182struct rbd_req_coll {
183 int total;
184 int num_done;
185 struct kref kref;
186 struct rbd_req_status status[0];
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700187};
188
Alex Elderf0f8cef2012-01-29 13:57:44 -0600189/*
190 * a single io request
191 */
192struct rbd_request {
193 struct request *rq; /* blk layer request */
194 struct bio *bio; /* cloned bio */
195 struct page **pages; /* list of used pages */
196 u64 len;
197 int coll_index;
198 struct rbd_req_coll *coll;
199};
200
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800201struct rbd_snap {
202 struct device dev;
203 const char *name;
Josh Durgin3591538f2011-12-05 18:25:13 -0800204 u64 size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800205 struct list_head node;
206 u64 id;
Alex Elder34b13182012-07-13 20:35:12 -0500207 u64 features;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800208};
209
Alex Elderf84344f2012-08-31 17:29:51 -0500210struct rbd_mapping {
Alex Elder99c1f082012-08-30 14:42:15 -0500211 u64 size;
Alex Elder34b13182012-07-13 20:35:12 -0500212 u64 features;
Alex Elderf84344f2012-08-31 17:29:51 -0500213 bool read_only;
214};
215
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700216/*
217 * a single device
218 */
219struct rbd_device {
Alex Elderde71a292012-07-03 16:01:19 -0500220 int dev_id; /* blkdev unique id */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700221
222 int major; /* blkdev assigned major */
223 struct gendisk *disk; /* blkdev's gendisk and rq */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700224
Alex Eldera30b71b2012-07-10 20:30:11 -0500225 u32 image_format; /* Either 1 or 2 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700226 struct rbd_client *rbd_client;
227
228 char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */
229
230 spinlock_t lock; /* queue lock */
231
232 struct rbd_image_header header;
Alex Elderd78b6502012-11-09 08:43:15 -0600233 atomic_t exists;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500234 struct rbd_spec *spec;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700235
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500236 char *header_name;
Alex Elder971f8392012-10-25 23:34:41 -0500237
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700238 struct ceph_osd_event *watch_event;
239 struct ceph_osd_request *watch_request;
240
Alex Elder86b00e02012-10-25 23:34:42 -0500241 struct rbd_spec *parent_spec;
242 u64 parent_overlap;
243
Josh Durginc6666012011-11-21 17:11:12 -0800244 /* protects updating the header */
245 struct rw_semaphore header_rwsem;
Alex Elderf84344f2012-08-31 17:29:51 -0500246
247 struct rbd_mapping mapping;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700248
249 struct list_head node;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800250
251 /* list of snapshots */
252 struct list_head snaps;
253
254 /* sysfs related */
255 struct device dev;
Alex Elder42382b72012-11-16 09:29:16 -0600256 unsigned long open_count;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800257};
258
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700259static DEFINE_MUTEX(ctl_mutex); /* Serialize open/close/setup/teardown */
Alex Eldere124a82f2012-01-29 13:57:44 -0600260
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700261static LIST_HEAD(rbd_dev_list); /* devices */
Alex Eldere124a82f2012-01-29 13:57:44 -0600262static DEFINE_SPINLOCK(rbd_dev_list_lock);
263
Alex Elder432b8582012-01-29 13:57:44 -0600264static LIST_HEAD(rbd_client_list); /* clients */
265static DEFINE_SPINLOCK(rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700266
Alex Elder304f6802012-08-31 17:29:52 -0500267static int rbd_dev_snaps_update(struct rbd_device *rbd_dev);
268static int rbd_dev_snaps_register(struct rbd_device *rbd_dev);
269
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800270static void rbd_dev_release(struct device *dev);
Alex Elder41f38c22012-10-25 23:34:40 -0500271static void rbd_remove_snap_dev(struct rbd_snap *snap);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800272
Alex Elderf0f8cef2012-01-29 13:57:44 -0600273static ssize_t rbd_add(struct bus_type *bus, const char *buf,
274 size_t count);
275static ssize_t rbd_remove(struct bus_type *bus, const char *buf,
276 size_t count);
277
278static struct bus_attribute rbd_bus_attrs[] = {
279 __ATTR(add, S_IWUSR, NULL, rbd_add),
280 __ATTR(remove, S_IWUSR, NULL, rbd_remove),
281 __ATTR_NULL
282};
283
284static struct bus_type rbd_bus_type = {
285 .name = "rbd",
286 .bus_attrs = rbd_bus_attrs,
287};
288
289static void rbd_root_dev_release(struct device *dev)
290{
291}
292
293static struct device rbd_root_dev = {
294 .init_name = "rbd",
295 .release = rbd_root_dev_release,
296};
297
Alex Elder06ecc6c2012-11-01 10:17:15 -0500298static __printf(2, 3)
299void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...)
300{
301 struct va_format vaf;
302 va_list args;
303
304 va_start(args, fmt);
305 vaf.fmt = fmt;
306 vaf.va = &args;
307
308 if (!rbd_dev)
309 printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf);
310 else if (rbd_dev->disk)
311 printk(KERN_WARNING "%s: %s: %pV\n",
312 RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf);
313 else if (rbd_dev->spec && rbd_dev->spec->image_name)
314 printk(KERN_WARNING "%s: image %s: %pV\n",
315 RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf);
316 else if (rbd_dev->spec && rbd_dev->spec->image_id)
317 printk(KERN_WARNING "%s: id %s: %pV\n",
318 RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf);
319 else /* punt */
320 printk(KERN_WARNING "%s: rbd_dev %p: %pV\n",
321 RBD_DRV_NAME, rbd_dev, &vaf);
322 va_end(args);
323}
324
Alex Elderaafb2302012-09-06 16:00:54 -0500325#ifdef RBD_DEBUG
326#define rbd_assert(expr) \
327 if (unlikely(!(expr))) { \
328 printk(KERN_ERR "\nAssertion failure in %s() " \
329 "at line %d:\n\n" \
330 "\trbd_assert(%s);\n\n", \
331 __func__, __LINE__, #expr); \
332 BUG(); \
333 }
334#else /* !RBD_DEBUG */
335# define rbd_assert(expr) ((void) 0)
336#endif /* !RBD_DEBUG */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800337
Alex Elder117973f2012-08-31 17:29:55 -0500338static int rbd_dev_refresh(struct rbd_device *rbd_dev, u64 *hver);
339static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev, u64 *hver);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700340
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700341static int rbd_open(struct block_device *bdev, fmode_t mode)
342{
Alex Elderf0f8cef2012-01-29 13:57:44 -0600343 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700344
Alex Elderf84344f2012-08-31 17:29:51 -0500345 if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700346 return -EROFS;
347
Alex Elder42382b72012-11-16 09:29:16 -0600348 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Alex Elderc3e946c2012-11-16 09:29:16 -0600349 (void) get_device(&rbd_dev->dev);
Alex Elderf84344f2012-08-31 17:29:51 -0500350 set_device_ro(bdev, rbd_dev->mapping.read_only);
Alex Elder42382b72012-11-16 09:29:16 -0600351 rbd_dev->open_count++;
352 mutex_unlock(&ctl_mutex);
Alex Elder340c7a22012-08-10 13:12:07 -0700353
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700354 return 0;
355}
356
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800357static int rbd_release(struct gendisk *disk, fmode_t mode)
358{
359 struct rbd_device *rbd_dev = disk->private_data;
360
Alex Elder42382b72012-11-16 09:29:16 -0600361 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
362 rbd_assert(rbd_dev->open_count > 0);
363 rbd_dev->open_count--;
Alex Elderc3e946c2012-11-16 09:29:16 -0600364 put_device(&rbd_dev->dev);
Alex Elder42382b72012-11-16 09:29:16 -0600365 mutex_unlock(&ctl_mutex);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800366
367 return 0;
368}
369
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700370static const struct block_device_operations rbd_bd_ops = {
371 .owner = THIS_MODULE,
372 .open = rbd_open,
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800373 .release = rbd_release,
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700374};
375
376/*
377 * Initialize an rbd client instance.
Alex Elder43ae4702012-07-03 16:01:18 -0500378 * We own *ceph_opts.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700379 */
Alex Elderf8c38922012-08-10 13:12:07 -0700380static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700381{
382 struct rbd_client *rbdc;
383 int ret = -ENOMEM;
384
385 dout("rbd_client_create\n");
386 rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL);
387 if (!rbdc)
388 goto out_opt;
389
390 kref_init(&rbdc->kref);
391 INIT_LIST_HEAD(&rbdc->node);
392
Alex Elderbc534d82012-01-29 13:57:44 -0600393 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
394
Alex Elder43ae4702012-07-03 16:01:18 -0500395 rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700396 if (IS_ERR(rbdc->client))
Alex Elderbc534d82012-01-29 13:57:44 -0600397 goto out_mutex;
Alex Elder43ae4702012-07-03 16:01:18 -0500398 ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700399
400 ret = ceph_open_session(rbdc->client);
401 if (ret < 0)
402 goto out_err;
403
Alex Elder432b8582012-01-29 13:57:44 -0600404 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700405 list_add_tail(&rbdc->node, &rbd_client_list);
Alex Elder432b8582012-01-29 13:57:44 -0600406 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700407
Alex Elderbc534d82012-01-29 13:57:44 -0600408 mutex_unlock(&ctl_mutex);
409
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700410 dout("rbd_client_create created %p\n", rbdc);
411 return rbdc;
412
413out_err:
414 ceph_destroy_client(rbdc->client);
Alex Elderbc534d82012-01-29 13:57:44 -0600415out_mutex:
416 mutex_unlock(&ctl_mutex);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700417 kfree(rbdc);
418out_opt:
Alex Elder43ae4702012-07-03 16:01:18 -0500419 if (ceph_opts)
420 ceph_destroy_options(ceph_opts);
Vasiliy Kulikov28f259b2010-09-26 12:59:37 +0400421 return ERR_PTR(ret);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700422}
423
424/*
Alex Elder1f7ba332012-08-10 13:12:07 -0700425 * Find a ceph client with specific addr and configuration. If
426 * found, bump its reference count.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700427 */
Alex Elder1f7ba332012-08-10 13:12:07 -0700428static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700429{
430 struct rbd_client *client_node;
Alex Elder1f7ba332012-08-10 13:12:07 -0700431 bool found = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700432
Alex Elder43ae4702012-07-03 16:01:18 -0500433 if (ceph_opts->flags & CEPH_OPT_NOSHARE)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700434 return NULL;
435
Alex Elder1f7ba332012-08-10 13:12:07 -0700436 spin_lock(&rbd_client_list_lock);
437 list_for_each_entry(client_node, &rbd_client_list, node) {
438 if (!ceph_compare_options(ceph_opts, client_node->client)) {
439 kref_get(&client_node->kref);
440 found = true;
441 break;
442 }
443 }
444 spin_unlock(&rbd_client_list_lock);
445
446 return found ? client_node : NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700447}
448
449/*
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700450 * mount options
451 */
452enum {
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700453 Opt_last_int,
454 /* int args above */
455 Opt_last_string,
456 /* string args above */
Alex Eldercc0538b2012-08-10 13:12:07 -0700457 Opt_read_only,
458 Opt_read_write,
459 /* Boolean args above */
460 Opt_last_bool,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700461};
462
Alex Elder43ae4702012-07-03 16:01:18 -0500463static match_table_t rbd_opts_tokens = {
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700464 /* int args above */
465 /* string args above */
Alex Elderbe466c12012-10-22 11:31:26 -0500466 {Opt_read_only, "read_only"},
Alex Eldercc0538b2012-08-10 13:12:07 -0700467 {Opt_read_only, "ro"}, /* Alternate spelling */
468 {Opt_read_write, "read_write"},
469 {Opt_read_write, "rw"}, /* Alternate spelling */
470 /* Boolean args above */
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700471 {-1, NULL}
472};
473
474static int parse_rbd_opts_token(char *c, void *private)
475{
Alex Elder43ae4702012-07-03 16:01:18 -0500476 struct rbd_options *rbd_opts = private;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700477 substring_t argstr[MAX_OPT_ARGS];
478 int token, intval, ret;
479
Alex Elder43ae4702012-07-03 16:01:18 -0500480 token = match_token(c, rbd_opts_tokens, argstr);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700481 if (token < 0)
482 return -EINVAL;
483
484 if (token < Opt_last_int) {
485 ret = match_int(&argstr[0], &intval);
486 if (ret < 0) {
487 pr_err("bad mount option arg (not int) "
488 "at '%s'\n", c);
489 return ret;
490 }
491 dout("got int token %d val %d\n", token, intval);
492 } else if (token > Opt_last_int && token < Opt_last_string) {
493 dout("got string token %d val %s\n", token,
494 argstr[0].from);
Alex Eldercc0538b2012-08-10 13:12:07 -0700495 } else if (token > Opt_last_string && token < Opt_last_bool) {
496 dout("got Boolean token %d\n", token);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700497 } else {
498 dout("got token %d\n", token);
499 }
500
501 switch (token) {
Alex Eldercc0538b2012-08-10 13:12:07 -0700502 case Opt_read_only:
503 rbd_opts->read_only = true;
504 break;
505 case Opt_read_write:
506 rbd_opts->read_only = false;
507 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700508 default:
Alex Elderaafb2302012-09-06 16:00:54 -0500509 rbd_assert(false);
510 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700511 }
512 return 0;
513}
514
515/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700516 * Get a ceph client with specific addr and configuration, if one does
517 * not exist create it.
518 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500519static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700520{
Alex Elderf8c38922012-08-10 13:12:07 -0700521 struct rbd_client *rbdc;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700522
Alex Elder1f7ba332012-08-10 13:12:07 -0700523 rbdc = rbd_client_find(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500524 if (rbdc) /* using an existing client */
Alex Elder43ae4702012-07-03 16:01:18 -0500525 ceph_destroy_options(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500526 else
Alex Elderf8c38922012-08-10 13:12:07 -0700527 rbdc = rbd_client_create(ceph_opts);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700528
Alex Elder9d3997f2012-10-25 23:34:42 -0500529 return rbdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700530}
531
532/*
533 * Destroy ceph client
Alex Elderd23a4b32012-01-29 13:57:43 -0600534 *
Alex Elder432b8582012-01-29 13:57:44 -0600535 * Caller must hold rbd_client_list_lock.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700536 */
537static void rbd_client_release(struct kref *kref)
538{
539 struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
540
541 dout("rbd_release_client %p\n", rbdc);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500542 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700543 list_del(&rbdc->node);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500544 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700545
546 ceph_destroy_client(rbdc->client);
547 kfree(rbdc);
548}
549
550/*
551 * Drop reference to ceph client node. If it's not referenced anymore, release
552 * it.
553 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500554static void rbd_put_client(struct rbd_client *rbdc)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700555{
Alex Elderc53d5892012-10-25 23:34:42 -0500556 if (rbdc)
557 kref_put(&rbdc->kref, rbd_client_release);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700558}
559
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700560/*
561 * Destroy requests collection
562 */
563static void rbd_coll_release(struct kref *kref)
564{
565 struct rbd_req_coll *coll =
566 container_of(kref, struct rbd_req_coll, kref);
567
568 dout("rbd_coll_release %p\n", coll);
569 kfree(coll);
570}
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700571
Alex Eldera30b71b2012-07-10 20:30:11 -0500572static bool rbd_image_format_valid(u32 image_format)
573{
574 return image_format == 1 || image_format == 2;
575}
576
Alex Elder8e94af82012-07-25 09:32:40 -0500577static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
578{
Alex Elder103a1502012-08-02 11:29:45 -0500579 size_t size;
580 u32 snap_count;
581
582 /* The header has to start with the magic rbd header text */
583 if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT)))
584 return false;
585
Alex Elderdb2388b2012-10-20 22:17:27 -0500586 /* The bio layer requires at least sector-sized I/O */
587
588 if (ondisk->options.order < SECTOR_SHIFT)
589 return false;
590
591 /* If we use u64 in a few spots we may be able to loosen this */
592
593 if (ondisk->options.order > 8 * sizeof (int) - 1)
594 return false;
595
Alex Elder103a1502012-08-02 11:29:45 -0500596 /*
597 * The size of a snapshot header has to fit in a size_t, and
598 * that limits the number of snapshots.
599 */
600 snap_count = le32_to_cpu(ondisk->snap_count);
601 size = SIZE_MAX - sizeof (struct ceph_snap_context);
602 if (snap_count > size / sizeof (__le64))
603 return false;
604
605 /*
606 * Not only that, but the size of the entire the snapshot
607 * header must also be representable in a size_t.
608 */
609 size -= snap_count * sizeof (__le64);
610 if ((u64) size < le64_to_cpu(ondisk->snap_names_len))
611 return false;
612
613 return true;
Alex Elder8e94af82012-07-25 09:32:40 -0500614}
615
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700616/*
617 * Create a new header structure, translate header format from the on-disk
618 * header.
619 */
620static int rbd_header_from_disk(struct rbd_image_header *header,
Alex Elder4156d992012-08-02 11:29:46 -0500621 struct rbd_image_header_ondisk *ondisk)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700622{
Alex Elderccece232012-07-10 20:30:10 -0500623 u32 snap_count;
Alex Elder58c17b02012-08-23 23:22:06 -0500624 size_t len;
Alex Elderd2bb24e2012-07-26 23:37:14 -0500625 size_t size;
Alex Elder621901d2012-08-23 23:22:06 -0500626 u32 i;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700627
Alex Elder6a523252012-07-19 17:12:59 -0500628 memset(header, 0, sizeof (*header));
629
Alex Elder103a1502012-08-02 11:29:45 -0500630 snap_count = le32_to_cpu(ondisk->snap_count);
631
Alex Elder58c17b02012-08-23 23:22:06 -0500632 len = strnlen(ondisk->object_prefix, sizeof (ondisk->object_prefix));
633 header->object_prefix = kmalloc(len + 1, GFP_KERNEL);
Alex Elder6a523252012-07-19 17:12:59 -0500634 if (!header->object_prefix)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700635 return -ENOMEM;
Alex Elder58c17b02012-08-23 23:22:06 -0500636 memcpy(header->object_prefix, ondisk->object_prefix, len);
637 header->object_prefix[len] = '\0';
Alex Elder00f1f362012-02-07 12:03:36 -0600638
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700639 if (snap_count) {
Alex Elderf785cc12012-08-23 23:22:06 -0500640 u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len);
641
Alex Elder621901d2012-08-23 23:22:06 -0500642 /* Save a copy of the snapshot names */
643
Alex Elderf785cc12012-08-23 23:22:06 -0500644 if (snap_names_len > (u64) SIZE_MAX)
645 return -EIO;
646 header->snap_names = kmalloc(snap_names_len, GFP_KERNEL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700647 if (!header->snap_names)
Alex Elder6a523252012-07-19 17:12:59 -0500648 goto out_err;
Alex Elderf785cc12012-08-23 23:22:06 -0500649 /*
650 * Note that rbd_dev_v1_header_read() guarantees
651 * the ondisk buffer we're working with has
652 * snap_names_len bytes beyond the end of the
653 * snapshot id array, this memcpy() is safe.
654 */
655 memcpy(header->snap_names, &ondisk->snaps[snap_count],
656 snap_names_len);
Alex Elder6a523252012-07-19 17:12:59 -0500657
Alex Elder621901d2012-08-23 23:22:06 -0500658 /* Record each snapshot's size */
659
Alex Elderd2bb24e2012-07-26 23:37:14 -0500660 size = snap_count * sizeof (*header->snap_sizes);
661 header->snap_sizes = kmalloc(size, GFP_KERNEL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700662 if (!header->snap_sizes)
Alex Elder6a523252012-07-19 17:12:59 -0500663 goto out_err;
Alex Elder621901d2012-08-23 23:22:06 -0500664 for (i = 0; i < snap_count; i++)
665 header->snap_sizes[i] =
666 le64_to_cpu(ondisk->snaps[i].image_size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700667 } else {
Alex Elderccece232012-07-10 20:30:10 -0500668 WARN_ON(ondisk->snap_names_len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700669 header->snap_names = NULL;
670 header->snap_sizes = NULL;
671 }
Alex Elder849b4262012-07-09 21:04:24 -0500672
Alex Elder34b13182012-07-13 20:35:12 -0500673 header->features = 0; /* No features support in v1 images */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700674 header->obj_order = ondisk->options.order;
675 header->crypt_type = ondisk->options.crypt_type;
676 header->comp_type = ondisk->options.comp_type;
Alex Elder6a523252012-07-19 17:12:59 -0500677
Alex Elder621901d2012-08-23 23:22:06 -0500678 /* Allocate and fill in the snapshot context */
679
Alex Elderf84344f2012-08-31 17:29:51 -0500680 header->image_size = le64_to_cpu(ondisk->image_size);
Alex Elder6a523252012-07-19 17:12:59 -0500681 size = sizeof (struct ceph_snap_context);
682 size += snap_count * sizeof (header->snapc->snaps[0]);
683 header->snapc = kzalloc(size, GFP_KERNEL);
684 if (!header->snapc)
685 goto out_err;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700686
687 atomic_set(&header->snapc->nref, 1);
Alex Elder505cbb92012-07-19 08:49:18 -0500688 header->snapc->seq = le64_to_cpu(ondisk->snap_seq);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700689 header->snapc->num_snaps = snap_count;
Alex Elder621901d2012-08-23 23:22:06 -0500690 for (i = 0; i < snap_count; i++)
691 header->snapc->snaps[i] =
692 le64_to_cpu(ondisk->snaps[i].id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700693
694 return 0;
695
Alex Elder6a523252012-07-19 17:12:59 -0500696out_err:
Alex Elder849b4262012-07-09 21:04:24 -0500697 kfree(header->snap_sizes);
Alex Elderccece232012-07-10 20:30:10 -0500698 header->snap_sizes = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700699 kfree(header->snap_names);
Alex Elderccece232012-07-10 20:30:10 -0500700 header->snap_names = NULL;
Alex Elder6a523252012-07-19 17:12:59 -0500701 kfree(header->object_prefix);
702 header->object_prefix = NULL;
Alex Elderccece232012-07-10 20:30:10 -0500703
Alex Elder00f1f362012-02-07 12:03:36 -0600704 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700705}
706
Alex Elder9e15b772012-10-30 19:40:33 -0500707static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id)
708{
709 struct rbd_snap *snap;
710
711 if (snap_id == CEPH_NOSNAP)
712 return RBD_SNAP_HEAD_NAME;
713
714 list_for_each_entry(snap, &rbd_dev->snaps, node)
715 if (snap_id == snap->id)
716 return snap->name;
717
718 return NULL;
719}
720
Alex Elder8836b992012-08-30 14:42:15 -0500721static int snap_by_name(struct rbd_device *rbd_dev, const char *snap_name)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700722{
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700723
Alex Eldere86924a2012-07-10 20:30:11 -0500724 struct rbd_snap *snap;
Alex Elder00f1f362012-02-07 12:03:36 -0600725
Alex Eldere86924a2012-07-10 20:30:11 -0500726 list_for_each_entry(snap, &rbd_dev->snaps, node) {
727 if (!strcmp(snap_name, snap->name)) {
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500728 rbd_dev->spec->snap_id = snap->id;
Alex Eldere86924a2012-07-10 20:30:11 -0500729 rbd_dev->mapping.size = snap->size;
Alex Elder34b13182012-07-13 20:35:12 -0500730 rbd_dev->mapping.features = snap->features;
Alex Elder00f1f362012-02-07 12:03:36 -0600731
Alex Eldere86924a2012-07-10 20:30:11 -0500732 return 0;
Alex Elder00f1f362012-02-07 12:03:36 -0600733 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700734 }
Alex Eldere86924a2012-07-10 20:30:11 -0500735
Alex Elder00f1f362012-02-07 12:03:36 -0600736 return -ENOENT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700737}
738
Alex Elder819d52b2012-10-25 23:34:41 -0500739static int rbd_dev_set_mapping(struct rbd_device *rbd_dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700740{
Alex Elder78dc4472012-07-19 08:49:18 -0500741 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700742
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500743 if (!memcmp(rbd_dev->spec->snap_name, RBD_SNAP_HEAD_NAME,
Josh Durgincc9d7342011-11-21 18:19:13 -0800744 sizeof (RBD_SNAP_HEAD_NAME))) {
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500745 rbd_dev->spec->snap_id = CEPH_NOSNAP;
Alex Elder99c1f082012-08-30 14:42:15 -0500746 rbd_dev->mapping.size = rbd_dev->header.image_size;
Alex Elder34b13182012-07-13 20:35:12 -0500747 rbd_dev->mapping.features = rbd_dev->header.features;
Alex Eldere86924a2012-07-10 20:30:11 -0500748 ret = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700749 } else {
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500750 ret = snap_by_name(rbd_dev, rbd_dev->spec->snap_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700751 if (ret < 0)
752 goto done;
Alex Elderf84344f2012-08-31 17:29:51 -0500753 rbd_dev->mapping.read_only = true;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700754 }
Alex Elderd78b6502012-11-09 08:43:15 -0600755 atomic_set(&rbd_dev->exists, 1);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700756done:
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700757 return ret;
758}
759
760static void rbd_header_free(struct rbd_image_header *header)
761{
Alex Elder849b4262012-07-09 21:04:24 -0500762 kfree(header->object_prefix);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500763 header->object_prefix = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700764 kfree(header->snap_sizes);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500765 header->snap_sizes = NULL;
Alex Elder849b4262012-07-09 21:04:24 -0500766 kfree(header->snap_names);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500767 header->snap_names = NULL;
Josh Durgind1d25642011-12-05 14:03:05 -0800768 ceph_put_snap_context(header->snapc);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500769 header->snapc = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700770}
771
Alex Elder65ccfe22012-08-09 10:33:26 -0700772static char *rbd_segment_name(struct rbd_device *rbd_dev, u64 offset)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700773{
Alex Elder65ccfe22012-08-09 10:33:26 -0700774 char *name;
775 u64 segment;
776 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700777
Alex Elder2fd82b92012-11-09 15:05:54 -0600778 name = kmalloc(MAX_OBJ_NAME_SIZE + 1, GFP_NOIO);
Alex Elder65ccfe22012-08-09 10:33:26 -0700779 if (!name)
780 return NULL;
781 segment = offset >> rbd_dev->header.obj_order;
Alex Elder2fd82b92012-11-09 15:05:54 -0600782 ret = snprintf(name, MAX_OBJ_NAME_SIZE + 1, "%s.%012llx",
Alex Elder65ccfe22012-08-09 10:33:26 -0700783 rbd_dev->header.object_prefix, segment);
Alex Elder2fd82b92012-11-09 15:05:54 -0600784 if (ret < 0 || ret > MAX_OBJ_NAME_SIZE) {
Alex Elder65ccfe22012-08-09 10:33:26 -0700785 pr_err("error formatting segment name for #%llu (%d)\n",
786 segment, ret);
787 kfree(name);
788 name = NULL;
789 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700790
Alex Elder65ccfe22012-08-09 10:33:26 -0700791 return name;
792}
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700793
Alex Elder65ccfe22012-08-09 10:33:26 -0700794static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset)
795{
796 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700797
Alex Elder65ccfe22012-08-09 10:33:26 -0700798 return offset & (segment_size - 1);
799}
800
801static u64 rbd_segment_length(struct rbd_device *rbd_dev,
802 u64 offset, u64 length)
803{
804 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
805
806 offset &= segment_size - 1;
807
Alex Elderaafb2302012-09-06 16:00:54 -0500808 rbd_assert(length <= U64_MAX - offset);
Alex Elder65ccfe22012-08-09 10:33:26 -0700809 if (offset + length > segment_size)
810 length = segment_size - offset;
811
812 return length;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700813}
814
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700815static int rbd_get_num_segments(struct rbd_image_header *header,
816 u64 ofs, u64 len)
817{
Alex Elderdf111be2012-08-09 10:33:26 -0700818 u64 start_seg;
819 u64 end_seg;
820
821 if (!len)
822 return 0;
823 if (len - 1 > U64_MAX - ofs)
824 return -ERANGE;
825
826 start_seg = ofs >> header->obj_order;
827 end_seg = (ofs + len - 1) >> header->obj_order;
828
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700829 return end_seg - start_seg + 1;
830}
831
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700832/*
Josh Durgin029bcbd2011-07-22 11:35:23 -0700833 * returns the size of an object in the image
834 */
835static u64 rbd_obj_bytes(struct rbd_image_header *header)
836{
837 return 1 << header->obj_order;
838}
839
840/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700841 * bio helpers
842 */
843
844static void bio_chain_put(struct bio *chain)
845{
846 struct bio *tmp;
847
848 while (chain) {
849 tmp = chain;
850 chain = chain->bi_next;
851 bio_put(tmp);
852 }
853}
854
855/*
856 * zeros a bio chain, starting at specific offset
857 */
858static void zero_bio_chain(struct bio *chain, int start_ofs)
859{
860 struct bio_vec *bv;
861 unsigned long flags;
862 void *buf;
863 int i;
864 int pos = 0;
865
866 while (chain) {
867 bio_for_each_segment(bv, chain, i) {
868 if (pos + bv->bv_len > start_ofs) {
869 int remainder = max(start_ofs - pos, 0);
870 buf = bvec_kmap_irq(bv, &flags);
871 memset(buf + remainder, 0,
872 bv->bv_len - remainder);
Dan Carpenter85b5aaa2010-10-11 21:15:11 +0200873 bvec_kunmap_irq(buf, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700874 }
875 pos += bv->bv_len;
876 }
877
878 chain = chain->bi_next;
879 }
880}
881
882/*
Alex Elderf7760da2012-10-20 22:17:27 -0500883 * Clone a portion of a bio, starting at the given byte offset
884 * and continuing for the number of bytes indicated.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700885 */
Alex Elderf7760da2012-10-20 22:17:27 -0500886static struct bio *bio_clone_range(struct bio *bio_src,
887 unsigned int offset,
888 unsigned int len,
889 gfp_t gfpmask)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700890{
Alex Elderf7760da2012-10-20 22:17:27 -0500891 struct bio_vec *bv;
892 unsigned int resid;
893 unsigned short idx;
894 unsigned int voff;
895 unsigned short end_idx;
896 unsigned short vcnt;
897 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700898
Alex Elderf7760da2012-10-20 22:17:27 -0500899 /* Handle the easy case for the caller */
900
901 if (!offset && len == bio_src->bi_size)
902 return bio_clone(bio_src, gfpmask);
903
904 if (WARN_ON_ONCE(!len))
905 return NULL;
906 if (WARN_ON_ONCE(len > bio_src->bi_size))
907 return NULL;
908 if (WARN_ON_ONCE(offset > bio_src->bi_size - len))
909 return NULL;
910
911 /* Find first affected segment... */
912
913 resid = offset;
914 __bio_for_each_segment(bv, bio_src, idx, 0) {
915 if (resid < bv->bv_len)
916 break;
917 resid -= bv->bv_len;
918 }
919 voff = resid;
920
921 /* ...and the last affected segment */
922
923 resid += len;
924 __bio_for_each_segment(bv, bio_src, end_idx, idx) {
925 if (resid <= bv->bv_len)
926 break;
927 resid -= bv->bv_len;
928 }
929 vcnt = end_idx - idx + 1;
930
931 /* Build the clone */
932
933 bio = bio_alloc(gfpmask, (unsigned int) vcnt);
934 if (!bio)
935 return NULL; /* ENOMEM */
936
937 bio->bi_bdev = bio_src->bi_bdev;
938 bio->bi_sector = bio_src->bi_sector + (offset >> SECTOR_SHIFT);
939 bio->bi_rw = bio_src->bi_rw;
940 bio->bi_flags |= 1 << BIO_CLONED;
941
942 /*
943 * Copy over our part of the bio_vec, then update the first
944 * and last (or only) entries.
945 */
946 memcpy(&bio->bi_io_vec[0], &bio_src->bi_io_vec[idx],
947 vcnt * sizeof (struct bio_vec));
948 bio->bi_io_vec[0].bv_offset += voff;
949 if (vcnt > 1) {
950 bio->bi_io_vec[0].bv_len -= voff;
951 bio->bi_io_vec[vcnt - 1].bv_len = resid;
952 } else {
953 bio->bi_io_vec[0].bv_len = len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700954 }
955
Alex Elderf7760da2012-10-20 22:17:27 -0500956 bio->bi_vcnt = vcnt;
957 bio->bi_size = len;
958 bio->bi_idx = 0;
Alex Elder542582f2012-08-09 10:33:25 -0700959
Alex Elderf7760da2012-10-20 22:17:27 -0500960 return bio;
961}
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700962
Alex Elderf7760da2012-10-20 22:17:27 -0500963/*
964 * Clone a portion of a bio chain, starting at the given byte offset
965 * into the first bio in the source chain and continuing for the
966 * number of bytes indicated. The result is another bio chain of
967 * exactly the given length, or a null pointer on error.
968 *
969 * The bio_src and offset parameters are both in-out. On entry they
970 * refer to the first source bio and the offset into that bio where
971 * the start of data to be cloned is located.
972 *
973 * On return, bio_src is updated to refer to the bio in the source
974 * chain that contains first un-cloned byte, and *offset will
975 * contain the offset of that byte within that bio.
976 */
977static struct bio *bio_chain_clone_range(struct bio **bio_src,
978 unsigned int *offset,
979 unsigned int len,
980 gfp_t gfpmask)
981{
982 struct bio *bi = *bio_src;
983 unsigned int off = *offset;
984 struct bio *chain = NULL;
985 struct bio **end;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700986
Alex Elderf7760da2012-10-20 22:17:27 -0500987 /* Build up a chain of clone bios up to the limit */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700988
Alex Elderf7760da2012-10-20 22:17:27 -0500989 if (!bi || off >= bi->bi_size || !len)
990 return NULL; /* Nothing to clone */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700991
Alex Elderf7760da2012-10-20 22:17:27 -0500992 end = &chain;
993 while (len) {
994 unsigned int bi_size;
995 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700996
Alex Elderf5400b72012-11-01 10:17:15 -0500997 if (!bi) {
998 rbd_warn(NULL, "bio_chain exhausted with %u left", len);
Alex Elderf7760da2012-10-20 22:17:27 -0500999 goto out_err; /* EINVAL; ran out of bio's */
Alex Elderf5400b72012-11-01 10:17:15 -05001000 }
Alex Elderf7760da2012-10-20 22:17:27 -05001001 bi_size = min_t(unsigned int, bi->bi_size - off, len);
1002 bio = bio_clone_range(bi, off, bi_size, gfpmask);
1003 if (!bio)
1004 goto out_err; /* ENOMEM */
1005
1006 *end = bio;
1007 end = &bio->bi_next;
1008
1009 off += bi_size;
1010 if (off == bi->bi_size) {
1011 bi = bi->bi_next;
1012 off = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001013 }
Alex Elderf7760da2012-10-20 22:17:27 -05001014 len -= bi_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001015 }
Alex Elderf7760da2012-10-20 22:17:27 -05001016 *bio_src = bi;
1017 *offset = off;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001018
Alex Elderf7760da2012-10-20 22:17:27 -05001019 return chain;
1020out_err:
1021 bio_chain_put(chain);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001022
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001023 return NULL;
1024}
1025
1026/*
1027 * helpers for osd request op vectors.
1028 */
Alex Elderae7ca4a32012-11-13 21:11:15 -06001029static struct ceph_osd_req_op *rbd_create_rw_ops(int num_op,
Alex Elder57cfc102012-06-26 12:57:03 -07001030 int opcode, u32 payload_len)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001031{
Alex Elder57cfc102012-06-26 12:57:03 -07001032 struct ceph_osd_req_op *ops;
1033
Alex Elderae7ca4a32012-11-13 21:11:15 -06001034 ops = kzalloc(num_op * sizeof (*ops), GFP_NOIO);
Alex Elder57cfc102012-06-26 12:57:03 -07001035 if (!ops)
1036 return NULL;
1037
1038 ops[0].op = opcode;
1039
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001040 /*
1041 * op extent offset and length will be set later on
1042 * in calc_raw_layout()
1043 */
Alex Elder57cfc102012-06-26 12:57:03 -07001044 ops[0].payload_len = payload_len;
1045
1046 return ops;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001047}
1048
1049static void rbd_destroy_ops(struct ceph_osd_req_op *ops)
1050{
1051 kfree(ops);
1052}
1053
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001054static void rbd_coll_end_req_index(struct request *rq,
1055 struct rbd_req_coll *coll,
1056 int index,
Alex Elder8986cb32012-11-08 08:01:39 -06001057 s32 ret, u64 len)
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001058{
1059 struct request_queue *q;
1060 int min, max, i;
1061
Alex Elderbd919d42012-07-13 20:35:11 -05001062 dout("rbd_coll_end_req_index %p index %d ret %d len %llu\n",
Alex Elder8986cb32012-11-08 08:01:39 -06001063 coll, index, (int)ret, (unsigned long long)len);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001064
1065 if (!rq)
1066 return;
1067
1068 if (!coll) {
1069 blk_end_request(rq, ret, len);
1070 return;
1071 }
1072
1073 q = rq->q;
1074
1075 spin_lock_irq(q->queue_lock);
1076 coll->status[index].done = 1;
1077 coll->status[index].rc = ret;
1078 coll->status[index].bytes = len;
1079 max = min = coll->num_done;
1080 while (max < coll->total && coll->status[max].done)
1081 max++;
1082
1083 for (i = min; i<max; i++) {
Alex Elder8986cb32012-11-08 08:01:39 -06001084 __blk_end_request(rq, (int)coll->status[i].rc,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001085 coll->status[i].bytes);
1086 coll->num_done++;
1087 kref_put(&coll->kref, rbd_coll_release);
1088 }
1089 spin_unlock_irq(q->queue_lock);
1090}
1091
Alex Elder725afc92012-11-08 08:01:39 -06001092static void rbd_coll_end_req(struct rbd_request *rbd_req,
Alex Elder8986cb32012-11-08 08:01:39 -06001093 s32 ret, u64 len)
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001094{
Alex Elder725afc92012-11-08 08:01:39 -06001095 rbd_coll_end_req_index(rbd_req->rq,
1096 rbd_req->coll, rbd_req->coll_index,
1097 ret, len);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001098}
1099
Alex Elder0ec8ce82012-11-09 08:43:15 -06001100static void rbd_layout_init(struct ceph_file_layout *layout, u64 pool_id)
1101{
1102 memset(layout, 0, sizeof (*layout));
1103 layout->fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
1104 layout->fl_stripe_count = cpu_to_le32(1);
1105 layout->fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
1106 rbd_assert(pool_id <= (u64) U32_MAX);
1107 layout->fl_pg_pool = cpu_to_le32((u32) pool_id);
1108}
1109
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001110/*
1111 * Send ceph osd request
1112 */
1113static int rbd_do_request(struct request *rq,
Alex Elder0ce1a792012-07-03 16:01:18 -05001114 struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001115 struct ceph_snap_context *snapc,
1116 u64 snapid,
Alex Elderaded07e2012-07-03 16:01:18 -05001117 const char *object_name, u64 ofs, u64 len,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001118 struct bio *bio,
1119 struct page **pages,
1120 int num_pages,
1121 int flags,
Alex Elderd07c0952012-11-13 21:11:15 -06001122 unsigned int num_op,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001123 struct ceph_osd_req_op *ops,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001124 struct rbd_req_coll *coll,
1125 int coll_index,
Alex Elder5f29ddd2012-11-08 08:01:39 -06001126 void (*rbd_cb)(struct ceph_osd_request *,
1127 struct ceph_msg *),
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001128 struct ceph_osd_request **linger_req,
1129 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001130{
Alex Elder5f29ddd2012-11-08 08:01:39 -06001131 struct ceph_osd_request *osd_req;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001132 int ret;
1133 u64 bno;
1134 struct timespec mtime = CURRENT_TIME;
Alex Elder725afc92012-11-08 08:01:39 -06001135 struct rbd_request *rbd_req;
Alex Elder1dbb4392012-01-24 10:08:37 -06001136 struct ceph_osd_client *osdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001137
Alex Elder725afc92012-11-08 08:01:39 -06001138 rbd_req = kzalloc(sizeof(*rbd_req), GFP_NOIO);
Alex Eldercd323ac2012-11-08 08:01:39 -06001139 if (!rbd_req)
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001140 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001141
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001142 if (coll) {
Alex Elder725afc92012-11-08 08:01:39 -06001143 rbd_req->coll = coll;
1144 rbd_req->coll_index = coll_index;
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001145 }
1146
Alex Elderf7760da2012-10-20 22:17:27 -05001147 dout("rbd_do_request object_name=%s ofs=%llu len=%llu coll=%p[%d]\n",
1148 object_name, (unsigned long long) ofs,
1149 (unsigned long long) len, coll, coll_index);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001150
Alex Elder0ce1a792012-07-03 16:01:18 -05001151 osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elderae7ca4a32012-11-13 21:11:15 -06001152 osd_req = ceph_osdc_alloc_request(osdc, snapc, num_op, false, GFP_NOIO);
Alex Elder5f29ddd2012-11-08 08:01:39 -06001153 if (!osd_req) {
Sage Weil4ad12622011-05-03 09:23:36 -07001154 ret = -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001155 goto done_pages;
1156 }
1157
Alex Elderd178a9e2012-11-13 21:11:15 -06001158 osd_req->r_flags = flags;
Alex Elder54a54002012-11-13 21:11:15 -06001159 osd_req->r_pages = pages;
1160 if (bio) {
1161 osd_req->r_bio = bio;
1162 bio_get(osd_req->r_bio);
1163 }
Alex Elder5f29ddd2012-11-08 08:01:39 -06001164 osd_req->r_callback = rbd_cb;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001165
Alex Elder725afc92012-11-08 08:01:39 -06001166 rbd_req->rq = rq;
1167 rbd_req->bio = bio;
1168 rbd_req->pages = pages;
1169 rbd_req->len = len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001170
Alex Elder5f29ddd2012-11-08 08:01:39 -06001171 osd_req->r_priv = rbd_req;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001172
Alex Elder5f29ddd2012-11-08 08:01:39 -06001173 strncpy(osd_req->r_oid, object_name, sizeof(osd_req->r_oid));
1174 osd_req->r_oid_len = strlen(osd_req->r_oid);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001175
Alex Elder0ec8ce82012-11-09 08:43:15 -06001176 rbd_layout_init(&osd_req->r_file_layout, rbd_dev->spec->pool_id);
Alex Eldere75b45c2012-11-13 21:11:14 -06001177 ret = ceph_calc_raw_layout(&osd_req->r_file_layout,
Alex Elder4d6b2502012-11-13 21:11:15 -06001178 ofs, &len, &bno, osd_req, ops);
Sage Weil6cae3712012-09-24 21:02:47 -07001179 rbd_assert(ret == 0);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001180
Alex Elderae7ca4a32012-11-13 21:11:15 -06001181 ceph_osdc_build_request(osd_req, ofs, len, num_op, ops,
1182 snapc, snapid, &mtime);
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 kfree(rbd_req);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001210 return ret;
1211}
1212
1213/*
1214 * Ceph osd op callback
1215 */
Alex Elder5f29ddd2012-11-08 08:01:39 -06001216static void rbd_req_cb(struct ceph_osd_request *osd_req, struct ceph_msg *msg)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001217{
Alex Elder5f29ddd2012-11-08 08:01:39 -06001218 struct rbd_request *rbd_req = osd_req->r_priv;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001219 struct ceph_osd_reply_head *replyhead;
1220 struct ceph_osd_op *op;
Alex Elder8986cb32012-11-08 08:01:39 -06001221 s32 rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001222 u64 bytes;
1223 int read_op;
1224
1225 /* parse reply */
1226 replyhead = msg->front.iov_base;
1227 WARN_ON(le32_to_cpu(replyhead->num_ops) == 0);
1228 op = (void *)(replyhead + 1);
Alex Elder8986cb32012-11-08 08:01:39 -06001229 rc = (s32)le32_to_cpu(replyhead->result);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001230 bytes = le64_to_cpu(op->extent.length);
Dan Carpenter895cfcc2012-06-06 09:15:33 -05001231 read_op = (le16_to_cpu(op->op) == CEPH_OSD_OP_READ);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001232
Alex Elderbd919d42012-07-13 20:35:11 -05001233 dout("rbd_req_cb bytes=%llu readop=%d rc=%d\n",
1234 (unsigned long long) bytes, read_op, (int) rc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001235
Alex Elder8986cb32012-11-08 08:01:39 -06001236 if (rc == (s32)-ENOENT && read_op) {
Alex Elder725afc92012-11-08 08:01:39 -06001237 zero_bio_chain(rbd_req->bio, 0);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001238 rc = 0;
Alex Elder725afc92012-11-08 08:01:39 -06001239 } else if (rc == 0 && read_op && bytes < rbd_req->len) {
1240 zero_bio_chain(rbd_req->bio, bytes);
1241 bytes = rbd_req->len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001242 }
1243
Alex Elder725afc92012-11-08 08:01:39 -06001244 rbd_coll_end_req(rbd_req, rc, bytes);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001245
Alex Elder725afc92012-11-08 08:01:39 -06001246 if (rbd_req->bio)
1247 bio_chain_put(rbd_req->bio);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001248
Alex Elder5f29ddd2012-11-08 08:01:39 -06001249 ceph_osdc_put_request(osd_req);
Alex Elder725afc92012-11-08 08:01:39 -06001250 kfree(rbd_req);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001251}
1252
Alex Elder5f29ddd2012-11-08 08:01:39 -06001253static void rbd_simple_req_cb(struct ceph_osd_request *osd_req,
1254 struct ceph_msg *msg)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001255{
Alex Elder5f29ddd2012-11-08 08:01:39 -06001256 ceph_osdc_put_request(osd_req);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001257}
1258
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001259/*
1260 * Do a synchronous ceph osd operation
1261 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001262static int rbd_req_sync_op(struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001263 int flags,
Alex Elderd07c0952012-11-13 21:11:15 -06001264 unsigned int num_op,
Alex Elder913d2fd2012-06-26 12:57:03 -07001265 struct ceph_osd_req_op *ops,
Alex Elderaded07e2012-07-03 16:01:18 -05001266 const char *object_name,
Alex Elderf8d4de62012-07-03 16:01:19 -05001267 u64 ofs, u64 inbound_size,
1268 char *inbound,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001269 struct ceph_osd_request **linger_req,
1270 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001271{
1272 int ret;
1273 struct page **pages;
1274 int num_pages;
Alex Elder913d2fd2012-06-26 12:57:03 -07001275
Alex Elderaafb2302012-09-06 16:00:54 -05001276 rbd_assert(ops != NULL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001277
Alex Elderf8d4de62012-07-03 16:01:19 -05001278 num_pages = calc_pages_for(ofs, inbound_size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001279 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
Dan Carpenterb8d06382010-10-11 21:14:23 +02001280 if (IS_ERR(pages))
1281 return PTR_ERR(pages);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001282
Alex Elder25704ac2012-11-09 08:43:16 -06001283 ret = rbd_do_request(NULL, rbd_dev, NULL, CEPH_NOSNAP,
Alex Elderf8d4de62012-07-03 16:01:19 -05001284 object_name, ofs, inbound_size, NULL,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001285 pages, num_pages,
1286 flags,
Alex Elderd07c0952012-11-13 21:11:15 -06001287 num_op, ops,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001288 NULL, 0,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001289 NULL,
1290 linger_req, ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001291 if (ret < 0)
Alex Elder913d2fd2012-06-26 12:57:03 -07001292 goto done;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001293
Alex Elderf8d4de62012-07-03 16:01:19 -05001294 if ((flags & CEPH_OSD_FLAG_READ) && inbound)
1295 ret = ceph_copy_from_page_vector(pages, inbound, ofs, ret);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001296
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001297done:
1298 ceph_release_page_vector(pages, num_pages);
1299 return ret;
1300}
1301
1302/*
1303 * Do an asynchronous ceph osd operation
1304 */
1305static int rbd_do_op(struct request *rq,
Alex Elder0ce1a792012-07-03 16:01:18 -05001306 struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001307 struct ceph_snap_context *snapc,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001308 u64 ofs, u64 len,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001309 struct bio *bio,
1310 struct rbd_req_coll *coll,
1311 int coll_index)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001312{
1313 char *seg_name;
1314 u64 seg_ofs;
1315 u64 seg_len;
1316 int ret;
1317 struct ceph_osd_req_op *ops;
1318 u32 payload_len;
Alex Elderff2e4bb2012-10-10 18:59:29 -07001319 int opcode;
1320 int flags;
Alex Elder46342462012-10-10 18:59:29 -07001321 u64 snapid;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001322
Alex Elder65ccfe22012-08-09 10:33:26 -07001323 seg_name = rbd_segment_name(rbd_dev, ofs);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001324 if (!seg_name)
1325 return -ENOMEM;
Alex Elder65ccfe22012-08-09 10:33:26 -07001326 seg_len = rbd_segment_length(rbd_dev, ofs, len);
1327 seg_ofs = rbd_segment_offset(rbd_dev, ofs);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001328
Alex Elderff2e4bb2012-10-10 18:59:29 -07001329 if (rq_data_dir(rq) == WRITE) {
1330 opcode = CEPH_OSD_OP_WRITE;
1331 flags = CEPH_OSD_FLAG_WRITE|CEPH_OSD_FLAG_ONDISK;
Alex Elder46342462012-10-10 18:59:29 -07001332 snapid = CEPH_NOSNAP;
Alex Elderff2e4bb2012-10-10 18:59:29 -07001333 payload_len = seg_len;
1334 } else {
1335 opcode = CEPH_OSD_OP_READ;
1336 flags = CEPH_OSD_FLAG_READ;
Alex Eldera7b4c652012-11-09 08:43:15 -06001337 rbd_assert(!snapc);
Alex Elder0d7dbfc2012-10-25 23:34:41 -05001338 snapid = rbd_dev->spec->snap_id;
Alex Elderff2e4bb2012-10-10 18:59:29 -07001339 payload_len = 0;
1340 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001341
Alex Elder57cfc102012-06-26 12:57:03 -07001342 ret = -ENOMEM;
1343 ops = rbd_create_rw_ops(1, opcode, payload_len);
1344 if (!ops)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001345 goto done;
1346
1347 /* we've taken care of segment sizes earlier when we
1348 cloned the bios. We should never have a segment
1349 truncated at this point */
Alex Elderaafb2302012-09-06 16:00:54 -05001350 rbd_assert(seg_len == len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001351
1352 ret = rbd_do_request(rq, rbd_dev, snapc, snapid,
1353 seg_name, seg_ofs, seg_len,
1354 bio,
1355 NULL, 0,
1356 flags,
Alex Elderd07c0952012-11-13 21:11:15 -06001357 1, ops,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001358 coll, coll_index,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001359 rbd_req_cb, 0, NULL);
Alex Eldercd323ac2012-11-08 08:01:39 -06001360 if (ret < 0)
1361 rbd_coll_end_req_index(rq, coll, coll_index,
1362 (s32)ret, seg_len);
Sage Weil11f77002011-05-12 16:13:54 -07001363 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,
Alex Elderaded07e2012-07-03 16:01:18 -05001373 const char *object_name,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001374 u64 ofs, u64 len,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001375 char *buf,
1376 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001377{
Alex Elder913d2fd2012-06-26 12:57:03 -07001378 struct ceph_osd_req_op *ops;
1379 int ret;
1380
1381 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_READ, 0);
1382 if (!ops)
1383 return -ENOMEM;
1384
Alex Elder25704ac2012-11-09 08:43:16 -06001385 ret = rbd_req_sync_op(rbd_dev, CEPH_OSD_FLAG_READ,
Alex Elderd07c0952012-11-13 21:11:15 -06001386 1, ops, object_name, ofs, len, buf, NULL, ver);
Alex Elder913d2fd2012-06-26 12:57:03 -07001387 rbd_destroy_ops(ops);
1388
1389 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001390}
1391
1392/*
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001393 * Request sync osd watch
1394 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001395static int rbd_req_sync_notify_ack(struct rbd_device *rbd_dev,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001396 u64 ver,
Alex Elder7f0a24d2012-07-25 09:32:40 -05001397 u64 notify_id)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001398{
1399 struct ceph_osd_req_op *ops;
Sage Weil11f77002011-05-12 16:13:54 -07001400 int ret;
1401
Alex Elder57cfc102012-06-26 12:57:03 -07001402 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_NOTIFY_ACK, 0);
1403 if (!ops)
1404 return -ENOMEM;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001405
Josh Durgina71b8912011-12-05 18:10:44 -08001406 ops[0].watch.ver = cpu_to_le64(ver);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001407 ops[0].watch.cookie = notify_id;
1408 ops[0].watch.flag = 0;
1409
Alex Elder0ce1a792012-07-03 16:01:18 -05001410 ret = rbd_do_request(NULL, rbd_dev, NULL, CEPH_NOSNAP,
Alex Elder7f0a24d2012-07-25 09:32:40 -05001411 rbd_dev->header_name, 0, 0, NULL,
Alex Elderad4f2322012-07-03 16:01:19 -05001412 NULL, 0,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001413 CEPH_OSD_FLAG_READ,
Alex Elderd07c0952012-11-13 21:11:15 -06001414 1, ops,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001415 NULL, 0,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001416 rbd_simple_req_cb, 0, NULL);
1417
1418 rbd_destroy_ops(ops);
1419 return ret;
1420}
1421
1422static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
1423{
Alex Elder0ce1a792012-07-03 16:01:18 -05001424 struct rbd_device *rbd_dev = (struct rbd_device *)data;
Josh Durgina71b8912011-12-05 18:10:44 -08001425 u64 hver;
Sage Weil13143d22011-05-12 16:08:30 -07001426 int rc;
1427
Alex Elder0ce1a792012-07-03 16:01:18 -05001428 if (!rbd_dev)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001429 return;
1430
Alex Elderbd919d42012-07-13 20:35:11 -05001431 dout("rbd_watch_cb %s notify_id=%llu opcode=%u\n",
1432 rbd_dev->header_name, (unsigned long long) notify_id,
1433 (unsigned int) opcode);
Alex Elder117973f2012-08-31 17:29:55 -05001434 rc = rbd_dev_refresh(rbd_dev, &hver);
Sage Weil13143d22011-05-12 16:08:30 -07001435 if (rc)
Alex Elder06ecc6c2012-11-01 10:17:15 -05001436 rbd_warn(rbd_dev, "got notification but failed to "
1437 " update snaps: %d\n", rc);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001438
Alex Elder7f0a24d2012-07-25 09:32:40 -05001439 rbd_req_sync_notify_ack(rbd_dev, hver, notify_id);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001440}
1441
1442/*
1443 * Request sync osd watch
1444 */
Alex Elder0e6f3222012-07-25 09:32:40 -05001445static int rbd_req_sync_watch(struct rbd_device *rbd_dev)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001446{
1447 struct ceph_osd_req_op *ops;
Alex Elder0ce1a792012-07-03 16:01:18 -05001448 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder57cfc102012-06-26 12:57:03 -07001449 int ret;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001450
Alex Elder57cfc102012-06-26 12:57:03 -07001451 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_WATCH, 0);
1452 if (!ops)
1453 return -ENOMEM;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001454
1455 ret = ceph_osdc_create_event(osdc, rbd_watch_cb, 0,
Alex Elder0ce1a792012-07-03 16:01:18 -05001456 (void *)rbd_dev, &rbd_dev->watch_event);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001457 if (ret < 0)
1458 goto fail;
1459
Alex Elder0e6f3222012-07-25 09:32:40 -05001460 ops[0].watch.ver = cpu_to_le64(rbd_dev->header.obj_version);
Alex Elder0ce1a792012-07-03 16:01:18 -05001461 ops[0].watch.cookie = cpu_to_le64(rbd_dev->watch_event->cookie);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001462 ops[0].watch.flag = 1;
1463
Alex Elder25704ac2012-11-09 08:43:16 -06001464 ret = rbd_req_sync_op(rbd_dev,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001465 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
Alex Elderd07c0952012-11-13 21:11:15 -06001466 1, ops,
Alex Elder0e6f3222012-07-25 09:32:40 -05001467 rbd_dev->header_name,
1468 0, 0, NULL,
Alex Elder0ce1a792012-07-03 16:01:18 -05001469 &rbd_dev->watch_request, NULL);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001470
1471 if (ret < 0)
1472 goto fail_event;
1473
1474 rbd_destroy_ops(ops);
1475 return 0;
1476
1477fail_event:
Alex Elder0ce1a792012-07-03 16:01:18 -05001478 ceph_osdc_cancel_event(rbd_dev->watch_event);
1479 rbd_dev->watch_event = NULL;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001480fail:
1481 rbd_destroy_ops(ops);
1482 return ret;
1483}
1484
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001485/*
1486 * Request sync osd unwatch
1487 */
Alex Elder070c6332012-07-25 09:32:41 -05001488static int rbd_req_sync_unwatch(struct rbd_device *rbd_dev)
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001489{
1490 struct ceph_osd_req_op *ops;
Alex Elder57cfc102012-06-26 12:57:03 -07001491 int ret;
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001492
Alex Elder57cfc102012-06-26 12:57:03 -07001493 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_WATCH, 0);
1494 if (!ops)
1495 return -ENOMEM;
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001496
1497 ops[0].watch.ver = 0;
Alex Elder0ce1a792012-07-03 16:01:18 -05001498 ops[0].watch.cookie = cpu_to_le64(rbd_dev->watch_event->cookie);
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001499 ops[0].watch.flag = 0;
1500
Alex Elder25704ac2012-11-09 08:43:16 -06001501 ret = rbd_req_sync_op(rbd_dev,
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001502 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
Alex Elderd07c0952012-11-13 21:11:15 -06001503 1, ops,
Alex Elder070c6332012-07-25 09:32:41 -05001504 rbd_dev->header_name,
1505 0, 0, NULL, NULL, NULL);
1506
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001507
1508 rbd_destroy_ops(ops);
Alex Elder0ce1a792012-07-03 16:01:18 -05001509 ceph_osdc_cancel_event(rbd_dev->watch_event);
1510 rbd_dev->watch_event = NULL;
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001511 return ret;
1512}
1513
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001514/*
Alex Elder3cb4a682012-06-26 12:57:03 -07001515 * Synchronous osd object method call
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001516 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001517static int rbd_req_sync_exec(struct rbd_device *rbd_dev,
Alex Elderaded07e2012-07-03 16:01:18 -05001518 const char *object_name,
1519 const char *class_name,
1520 const char *method_name,
Alex Elder3cb4a682012-06-26 12:57:03 -07001521 const char *outbound,
1522 size_t outbound_size,
Alex Elderf8d4de62012-07-03 16:01:19 -05001523 char *inbound,
1524 size_t inbound_size,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001525 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001526{
1527 struct ceph_osd_req_op *ops;
Alex Elderaded07e2012-07-03 16:01:18 -05001528 int class_name_len = strlen(class_name);
1529 int method_name_len = strlen(method_name);
Alex Elder3cb4a682012-06-26 12:57:03 -07001530 int payload_size;
Alex Elder57cfc102012-06-26 12:57:03 -07001531 int ret;
1532
Alex Elder3cb4a682012-06-26 12:57:03 -07001533 /*
1534 * Any input parameters required by the method we're calling
1535 * will be sent along with the class and method names as
1536 * part of the message payload. That data and its size are
1537 * supplied via the indata and indata_len fields (named from
1538 * the perspective of the server side) in the OSD request
1539 * operation.
1540 */
1541 payload_size = class_name_len + method_name_len + outbound_size;
1542 ops = rbd_create_rw_ops(1, CEPH_OSD_OP_CALL, payload_size);
Alex Elder57cfc102012-06-26 12:57:03 -07001543 if (!ops)
1544 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001545
Alex Elderaded07e2012-07-03 16:01:18 -05001546 ops[0].cls.class_name = class_name;
1547 ops[0].cls.class_len = (__u8) class_name_len;
1548 ops[0].cls.method_name = method_name;
1549 ops[0].cls.method_len = (__u8) method_name_len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001550 ops[0].cls.argc = 0;
Alex Elder3cb4a682012-06-26 12:57:03 -07001551 ops[0].cls.indata = outbound;
1552 ops[0].cls.indata_len = outbound_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001553
Alex Elderd07c0952012-11-13 21:11:15 -06001554 ret = rbd_req_sync_op(rbd_dev, CEPH_OSD_FLAG_READ, 1, ops,
Alex Elderf8d4de62012-07-03 16:01:19 -05001555 object_name, 0, inbound_size, inbound,
1556 NULL, ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001557
1558 rbd_destroy_ops(ops);
1559
1560 dout("cls_exec returned %d\n", ret);
1561 return ret;
1562}
1563
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001564static struct rbd_req_coll *rbd_alloc_coll(int num_reqs)
1565{
1566 struct rbd_req_coll *coll =
1567 kzalloc(sizeof(struct rbd_req_coll) +
1568 sizeof(struct rbd_req_status) * num_reqs,
1569 GFP_ATOMIC);
1570
1571 if (!coll)
1572 return NULL;
1573 coll->total = num_reqs;
1574 kref_init(&coll->kref);
1575 return coll;
1576}
1577
Alex Elder8295cda2012-11-08 08:01:39 -06001578static int rbd_dev_do_request(struct request *rq,
1579 struct rbd_device *rbd_dev,
1580 struct ceph_snap_context *snapc,
1581 u64 ofs, unsigned int size,
1582 struct bio *bio_chain)
1583{
1584 int num_segs;
1585 struct rbd_req_coll *coll;
1586 unsigned int bio_offset;
1587 int cur_seg = 0;
1588
1589 dout("%s 0x%x bytes at 0x%llx\n",
1590 rq_data_dir(rq) == WRITE ? "write" : "read",
1591 size, (unsigned long long) blk_rq_pos(rq) * SECTOR_SIZE);
1592
1593 num_segs = rbd_get_num_segments(&rbd_dev->header, ofs, size);
1594 if (num_segs <= 0)
1595 return num_segs;
1596
1597 coll = rbd_alloc_coll(num_segs);
1598 if (!coll)
1599 return -ENOMEM;
1600
1601 bio_offset = 0;
1602 do {
1603 u64 limit = rbd_segment_length(rbd_dev, ofs, size);
1604 unsigned int clone_size;
1605 struct bio *bio_clone;
1606
1607 BUG_ON(limit > (u64)UINT_MAX);
1608 clone_size = (unsigned int)limit;
1609 dout("bio_chain->bi_vcnt=%hu\n", bio_chain->bi_vcnt);
1610
1611 kref_get(&coll->kref);
1612
1613 /* Pass a cloned bio chain via an osd request */
1614
1615 bio_clone = bio_chain_clone_range(&bio_chain,
1616 &bio_offset, clone_size,
1617 GFP_ATOMIC);
1618 if (bio_clone)
1619 (void)rbd_do_op(rq, rbd_dev, snapc,
1620 ofs, clone_size,
1621 bio_clone, coll, cur_seg);
1622 else
1623 rbd_coll_end_req_index(rq, coll, cur_seg,
1624 (s32)-ENOMEM,
1625 clone_size);
1626 size -= clone_size;
1627 ofs += clone_size;
1628
1629 cur_seg++;
1630 } while (size > 0);
1631 kref_put(&coll->kref, rbd_coll_release);
1632
1633 return 0;
1634}
1635
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001636/*
1637 * block device queue callback
1638 */
1639static void rbd_rq_fn(struct request_queue *q)
1640{
1641 struct rbd_device *rbd_dev = q->queuedata;
Alex Elderb395e8b2012-11-08 08:01:39 -06001642 bool read_only = rbd_dev->mapping.read_only;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001643 struct request *rq;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001644
Alex Elder00f1f362012-02-07 12:03:36 -06001645 while ((rq = blk_fetch_request(q))) {
Alex Elderb395e8b2012-11-08 08:01:39 -06001646 struct ceph_snap_context *snapc = NULL;
1647 unsigned int size = 0;
Alex Elder8295cda2012-11-08 08:01:39 -06001648 int result;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001649
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001650 dout("fetched request\n");
1651
Alex Elderb395e8b2012-11-08 08:01:39 -06001652 /* Filter out block requests we don't understand */
1653
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001654 if ((rq->cmd_type != REQ_TYPE_FS)) {
1655 __blk_end_request_all(rq, 0);
Alex Elder00f1f362012-02-07 12:03:36 -06001656 continue;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001657 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001658 spin_unlock_irq(q->queue_lock);
1659
Alex Eldera7b4c652012-11-09 08:43:15 -06001660 /* Write requests need a reference to the snapshot context */
Alex Elderb395e8b2012-11-08 08:01:39 -06001661
Alex Eldera7b4c652012-11-09 08:43:15 -06001662 if (rq_data_dir(rq) == WRITE) {
1663 result = -EROFS;
1664 if (read_only) /* Can't write to a read-only device */
1665 goto out_end_request;
Alex Elderb395e8b2012-11-08 08:01:39 -06001666
Alex Eldera7b4c652012-11-09 08:43:15 -06001667 /*
1668 * Note that each osd request will take its
1669 * own reference to the snapshot context
1670 * supplied. The reference we take here
1671 * just guarantees the one we provide stays
1672 * valid.
1673 */
1674 down_read(&rbd_dev->header_rwsem);
Alex Elderb395e8b2012-11-08 08:01:39 -06001675 snapc = ceph_get_snap_context(rbd_dev->header.snapc);
Alex Eldera7b4c652012-11-09 08:43:15 -06001676 up_read(&rbd_dev->header_rwsem);
Alex Elderb395e8b2012-11-08 08:01:39 -06001677 rbd_assert(snapc != NULL);
Alex Eldera7b4c652012-11-09 08:43:15 -06001678 } else if (!atomic_read(&rbd_dev->exists)) {
Alex Elderb395e8b2012-11-08 08:01:39 -06001679 rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP);
1680 dout("request for non-existent snapshot");
1681 result = -ENXIO;
1682 goto out_end_request;
1683 }
Alex Elderf7760da2012-10-20 22:17:27 -05001684
Alex Elderb395e8b2012-11-08 08:01:39 -06001685 size = blk_rq_bytes(rq);
1686 result = rbd_dev_do_request(rq, rbd_dev, snapc,
1687 blk_rq_pos(rq) * SECTOR_SIZE,
1688 size, rq->bio);
1689out_end_request:
Alex Eldera7b4c652012-11-09 08:43:15 -06001690 if (snapc)
1691 ceph_put_snap_context(snapc);
Alex Elder8295cda2012-11-08 08:01:39 -06001692 spin_lock_irq(q->queue_lock);
1693 if (!size || result < 0)
1694 __blk_end_request_all(rq, result);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001695 }
1696}
1697
1698/*
1699 * a queue callback. Makes sure that we don't create a bio that spans across
1700 * multiple osd objects. One exception would be with a single page bios,
Alex Elderf7760da2012-10-20 22:17:27 -05001701 * which we handle later at bio_chain_clone_range()
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001702 */
1703static int rbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd,
1704 struct bio_vec *bvec)
1705{
1706 struct rbd_device *rbd_dev = q->queuedata;
Alex Eldere5cfeed22012-10-20 22:17:27 -05001707 sector_t sector_offset;
1708 sector_t sectors_per_obj;
1709 sector_t obj_sector_offset;
1710 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001711
Alex Eldere5cfeed22012-10-20 22:17:27 -05001712 /*
1713 * Find how far into its rbd object the partition-relative
1714 * bio start sector is to offset relative to the enclosing
1715 * device.
1716 */
1717 sector_offset = get_start_sect(bmd->bi_bdev) + bmd->bi_sector;
1718 sectors_per_obj = 1 << (rbd_dev->header.obj_order - SECTOR_SHIFT);
1719 obj_sector_offset = sector_offset & (sectors_per_obj - 1);
Alex Elder593a9e72012-02-07 12:03:37 -06001720
Alex Eldere5cfeed22012-10-20 22:17:27 -05001721 /*
1722 * Compute the number of bytes from that offset to the end
1723 * of the object. Account for what's already used by the bio.
1724 */
1725 ret = (int) (sectors_per_obj - obj_sector_offset) << SECTOR_SHIFT;
1726 if (ret > bmd->bi_size)
1727 ret -= bmd->bi_size;
1728 else
1729 ret = 0;
1730
1731 /*
1732 * Don't send back more than was asked for. And if the bio
1733 * was empty, let the whole thing through because: "Note
1734 * that a block device *must* allow a single page to be
1735 * added to an empty bio."
1736 */
1737 rbd_assert(bvec->bv_len <= PAGE_SIZE);
1738 if (ret > (int) bvec->bv_len || !bmd->bi_size)
1739 ret = (int) bvec->bv_len;
1740
1741 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001742}
1743
1744static void rbd_free_disk(struct rbd_device *rbd_dev)
1745{
1746 struct gendisk *disk = rbd_dev->disk;
1747
1748 if (!disk)
1749 return;
1750
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001751 if (disk->flags & GENHD_FL_UP)
1752 del_gendisk(disk);
1753 if (disk->queue)
1754 blk_cleanup_queue(disk->queue);
1755 put_disk(disk);
1756}
1757
1758/*
Alex Elder4156d992012-08-02 11:29:46 -05001759 * Read the complete header for the given rbd device.
1760 *
1761 * Returns a pointer to a dynamically-allocated buffer containing
1762 * the complete and validated header. Caller can pass the address
1763 * of a variable that will be filled in with the version of the
1764 * header object at the time it was read.
1765 *
1766 * Returns a pointer-coded errno if a failure occurs.
1767 */
1768static struct rbd_image_header_ondisk *
1769rbd_dev_v1_header_read(struct rbd_device *rbd_dev, u64 *version)
1770{
1771 struct rbd_image_header_ondisk *ondisk = NULL;
1772 u32 snap_count = 0;
1773 u64 names_size = 0;
1774 u32 want_count;
1775 int ret;
1776
1777 /*
1778 * The complete header will include an array of its 64-bit
1779 * snapshot ids, followed by the names of those snapshots as
1780 * a contiguous block of NUL-terminated strings. Note that
1781 * the number of snapshots could change by the time we read
1782 * it in, in which case we re-read it.
1783 */
1784 do {
1785 size_t size;
1786
1787 kfree(ondisk);
1788
1789 size = sizeof (*ondisk);
1790 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
1791 size += names_size;
1792 ondisk = kmalloc(size, GFP_KERNEL);
1793 if (!ondisk)
1794 return ERR_PTR(-ENOMEM);
1795
Alex Elder47756182012-11-09 08:43:15 -06001796 ret = rbd_req_sync_read(rbd_dev, rbd_dev->header_name,
Alex Elder4156d992012-08-02 11:29:46 -05001797 0, size,
1798 (char *) ondisk, version);
1799
1800 if (ret < 0)
1801 goto out_err;
1802 if (WARN_ON((size_t) ret < size)) {
1803 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05001804 rbd_warn(rbd_dev, "short header read (want %zd got %d)",
1805 size, ret);
Alex Elder4156d992012-08-02 11:29:46 -05001806 goto out_err;
1807 }
1808 if (!rbd_dev_ondisk_valid(ondisk)) {
1809 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05001810 rbd_warn(rbd_dev, "invalid header");
Alex Elder4156d992012-08-02 11:29:46 -05001811 goto out_err;
1812 }
1813
1814 names_size = le64_to_cpu(ondisk->snap_names_len);
1815 want_count = snap_count;
1816 snap_count = le32_to_cpu(ondisk->snap_count);
1817 } while (snap_count != want_count);
1818
1819 return ondisk;
1820
1821out_err:
1822 kfree(ondisk);
1823
1824 return ERR_PTR(ret);
1825}
1826
1827/*
1828 * reload the ondisk the header
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001829 */
1830static int rbd_read_header(struct rbd_device *rbd_dev,
1831 struct rbd_image_header *header)
1832{
Alex Elder4156d992012-08-02 11:29:46 -05001833 struct rbd_image_header_ondisk *ondisk;
1834 u64 ver = 0;
1835 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001836
Alex Elder4156d992012-08-02 11:29:46 -05001837 ondisk = rbd_dev_v1_header_read(rbd_dev, &ver);
1838 if (IS_ERR(ondisk))
1839 return PTR_ERR(ondisk);
1840 ret = rbd_header_from_disk(header, ondisk);
1841 if (ret >= 0)
1842 header->obj_version = ver;
1843 kfree(ondisk);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001844
Alex Elder4156d992012-08-02 11:29:46 -05001845 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001846}
1847
Alex Elder41f38c22012-10-25 23:34:40 -05001848static void rbd_remove_all_snaps(struct rbd_device *rbd_dev)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001849{
1850 struct rbd_snap *snap;
Alex Eldera0593292012-07-19 09:09:27 -05001851 struct rbd_snap *next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001852
Alex Eldera0593292012-07-19 09:09:27 -05001853 list_for_each_entry_safe(snap, next, &rbd_dev->snaps, node)
Alex Elder41f38c22012-10-25 23:34:40 -05001854 rbd_remove_snap_dev(snap);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001855}
1856
Alex Elder94785542012-10-09 13:50:17 -07001857static void rbd_update_mapping_size(struct rbd_device *rbd_dev)
1858{
1859 sector_t size;
1860
Alex Elder0d7dbfc2012-10-25 23:34:41 -05001861 if (rbd_dev->spec->snap_id != CEPH_NOSNAP)
Alex Elder94785542012-10-09 13:50:17 -07001862 return;
1863
1864 size = (sector_t) rbd_dev->header.image_size / SECTOR_SIZE;
1865 dout("setting size to %llu sectors", (unsigned long long) size);
1866 rbd_dev->mapping.size = (u64) size;
1867 set_capacity(rbd_dev->disk, size);
1868}
1869
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001870/*
1871 * only read the first part of the ondisk header, without the snaps info
1872 */
Alex Elder117973f2012-08-31 17:29:55 -05001873static int rbd_dev_v1_refresh(struct rbd_device *rbd_dev, u64 *hver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001874{
1875 int ret;
1876 struct rbd_image_header h;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001877
1878 ret = rbd_read_header(rbd_dev, &h);
1879 if (ret < 0)
1880 return ret;
1881
Josh Durgina51aa0c2011-12-05 10:35:04 -08001882 down_write(&rbd_dev->header_rwsem);
1883
Alex Elder94785542012-10-09 13:50:17 -07001884 /* Update image size, and check for resize of mapped image */
1885 rbd_dev->header.image_size = h.image_size;
1886 rbd_update_mapping_size(rbd_dev);
Sage Weil9db4b3e2011-04-19 22:49:06 -07001887
Alex Elder849b4262012-07-09 21:04:24 -05001888 /* rbd_dev->header.object_prefix shouldn't change */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001889 kfree(rbd_dev->header.snap_sizes);
Alex Elder849b4262012-07-09 21:04:24 -05001890 kfree(rbd_dev->header.snap_names);
Josh Durgind1d25642011-12-05 14:03:05 -08001891 /* osd requests may still refer to snapc */
1892 ceph_put_snap_context(rbd_dev->header.snapc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001893
Alex Elderb8136232012-07-25 09:32:41 -05001894 if (hver)
1895 *hver = h.obj_version;
Josh Durgina71b8912011-12-05 18:10:44 -08001896 rbd_dev->header.obj_version = h.obj_version;
Josh Durgin93a24e02011-12-05 10:41:28 -08001897 rbd_dev->header.image_size = h.image_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001898 rbd_dev->header.snapc = h.snapc;
1899 rbd_dev->header.snap_names = h.snap_names;
1900 rbd_dev->header.snap_sizes = h.snap_sizes;
Alex Elder849b4262012-07-09 21:04:24 -05001901 /* Free the extra copy of the object prefix */
1902 WARN_ON(strcmp(rbd_dev->header.object_prefix, h.object_prefix));
1903 kfree(h.object_prefix);
1904
Alex Elder304f6802012-08-31 17:29:52 -05001905 ret = rbd_dev_snaps_update(rbd_dev);
1906 if (!ret)
1907 ret = rbd_dev_snaps_register(rbd_dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001908
Josh Durginc6666012011-11-21 17:11:12 -08001909 up_write(&rbd_dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001910
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001911 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001912}
1913
Alex Elder117973f2012-08-31 17:29:55 -05001914static int rbd_dev_refresh(struct rbd_device *rbd_dev, u64 *hver)
Alex Elder1fe5e992012-07-25 09:32:41 -05001915{
1916 int ret;
1917
Alex Elder117973f2012-08-31 17:29:55 -05001918 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
Alex Elder1fe5e992012-07-25 09:32:41 -05001919 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Alex Elder117973f2012-08-31 17:29:55 -05001920 if (rbd_dev->image_format == 1)
1921 ret = rbd_dev_v1_refresh(rbd_dev, hver);
1922 else
1923 ret = rbd_dev_v2_refresh(rbd_dev, hver);
Alex Elder1fe5e992012-07-25 09:32:41 -05001924 mutex_unlock(&ctl_mutex);
1925
1926 return ret;
1927}
1928
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001929static int rbd_init_disk(struct rbd_device *rbd_dev)
1930{
1931 struct gendisk *disk;
1932 struct request_queue *q;
Alex Elder593a9e72012-02-07 12:03:37 -06001933 u64 segment_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001934
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001935 /* create gendisk info */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001936 disk = alloc_disk(RBD_MINORS_PER_MAJOR);
1937 if (!disk)
Alex Elder1fcdb8a2012-08-29 17:11:06 -05001938 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001939
Alex Elderf0f8cef2012-01-29 13:57:44 -06001940 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
Alex Elderde71a292012-07-03 16:01:19 -05001941 rbd_dev->dev_id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001942 disk->major = rbd_dev->major;
1943 disk->first_minor = 0;
1944 disk->fops = &rbd_bd_ops;
1945 disk->private_data = rbd_dev;
1946
1947 /* init rq */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001948 q = blk_init_queue(rbd_rq_fn, &rbd_dev->lock);
1949 if (!q)
1950 goto out_disk;
Josh Durgin029bcbd2011-07-22 11:35:23 -07001951
Alex Elder593a9e72012-02-07 12:03:37 -06001952 /* We use the default size, but let's be explicit about it. */
1953 blk_queue_physical_block_size(q, SECTOR_SIZE);
1954
Josh Durgin029bcbd2011-07-22 11:35:23 -07001955 /* set io sizes to object size */
Alex Elder593a9e72012-02-07 12:03:37 -06001956 segment_size = rbd_obj_bytes(&rbd_dev->header);
1957 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
1958 blk_queue_max_segment_size(q, segment_size);
1959 blk_queue_io_min(q, segment_size);
1960 blk_queue_io_opt(q, segment_size);
Josh Durgin029bcbd2011-07-22 11:35:23 -07001961
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001962 blk_queue_merge_bvec(q, rbd_merge_bvec);
1963 disk->queue = q;
1964
1965 q->queuedata = rbd_dev;
1966
1967 rbd_dev->disk = disk;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001968
Alex Elder12f02942012-08-29 17:11:07 -05001969 set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
1970
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001971 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001972out_disk:
1973 put_disk(disk);
Alex Elder1fcdb8a2012-08-29 17:11:06 -05001974
1975 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001976}
1977
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001978/*
1979 sysfs
1980*/
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001981
Alex Elder593a9e72012-02-07 12:03:37 -06001982static struct rbd_device *dev_to_rbd_dev(struct device *dev)
1983{
1984 return container_of(dev, struct rbd_device, dev);
1985}
1986
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001987static ssize_t rbd_size_show(struct device *dev,
1988 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001989{
Alex Elder593a9e72012-02-07 12:03:37 -06001990 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Josh Durgina51aa0c2011-12-05 10:35:04 -08001991 sector_t size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001992
Josh Durgina51aa0c2011-12-05 10:35:04 -08001993 down_read(&rbd_dev->header_rwsem);
1994 size = get_capacity(rbd_dev->disk);
1995 up_read(&rbd_dev->header_rwsem);
1996
1997 return sprintf(buf, "%llu\n", (unsigned long long) size * SECTOR_SIZE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001998}
1999
Alex Elder34b13182012-07-13 20:35:12 -05002000/*
2001 * Note this shows the features for whatever's mapped, which is not
2002 * necessarily the base image.
2003 */
2004static ssize_t rbd_features_show(struct device *dev,
2005 struct device_attribute *attr, char *buf)
2006{
2007 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
2008
2009 return sprintf(buf, "0x%016llx\n",
2010 (unsigned long long) rbd_dev->mapping.features);
2011}
2012
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002013static ssize_t rbd_major_show(struct device *dev,
2014 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002015{
Alex Elder593a9e72012-02-07 12:03:37 -06002016 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002017
2018 return sprintf(buf, "%d\n", rbd_dev->major);
2019}
2020
2021static ssize_t rbd_client_id_show(struct device *dev,
2022 struct device_attribute *attr, char *buf)
2023{
Alex Elder593a9e72012-02-07 12:03:37 -06002024 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002025
Alex Elder1dbb4392012-01-24 10:08:37 -06002026 return sprintf(buf, "client%lld\n",
2027 ceph_client_id(rbd_dev->rbd_client->client));
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002028}
2029
2030static ssize_t rbd_pool_show(struct device *dev,
2031 struct device_attribute *attr, char *buf)
2032{
Alex Elder593a9e72012-02-07 12:03:37 -06002033 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002034
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002035 return sprintf(buf, "%s\n", rbd_dev->spec->pool_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002036}
2037
Alex Elder9bb2f332012-07-12 10:46:35 -05002038static ssize_t rbd_pool_id_show(struct device *dev,
2039 struct device_attribute *attr, char *buf)
2040{
2041 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
2042
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002043 return sprintf(buf, "%llu\n",
2044 (unsigned long long) rbd_dev->spec->pool_id);
Alex Elder9bb2f332012-07-12 10:46:35 -05002045}
2046
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002047static ssize_t rbd_name_show(struct device *dev,
2048 struct device_attribute *attr, char *buf)
2049{
Alex Elder593a9e72012-02-07 12:03:37 -06002050 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002051
Alex Eldera92ffdf2012-10-30 19:40:33 -05002052 if (rbd_dev->spec->image_name)
2053 return sprintf(buf, "%s\n", rbd_dev->spec->image_name);
2054
2055 return sprintf(buf, "(unknown)\n");
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002056}
2057
Alex Elder589d30e2012-07-10 20:30:11 -05002058static ssize_t rbd_image_id_show(struct device *dev,
2059 struct device_attribute *attr, char *buf)
2060{
2061 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
2062
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002063 return sprintf(buf, "%s\n", rbd_dev->spec->image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05002064}
2065
Alex Elder34b13182012-07-13 20:35:12 -05002066/*
2067 * Shows the name of the currently-mapped snapshot (or
2068 * RBD_SNAP_HEAD_NAME for the base image).
2069 */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002070static ssize_t rbd_snap_show(struct device *dev,
2071 struct device_attribute *attr,
2072 char *buf)
2073{
Alex Elder593a9e72012-02-07 12:03:37 -06002074 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002075
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002076 return sprintf(buf, "%s\n", rbd_dev->spec->snap_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002077}
2078
Alex Elder86b00e02012-10-25 23:34:42 -05002079/*
2080 * For an rbd v2 image, shows the pool id, image id, and snapshot id
2081 * for the parent image. If there is no parent, simply shows
2082 * "(no parent image)".
2083 */
2084static ssize_t rbd_parent_show(struct device *dev,
2085 struct device_attribute *attr,
2086 char *buf)
2087{
2088 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
2089 struct rbd_spec *spec = rbd_dev->parent_spec;
2090 int count;
2091 char *bufp = buf;
2092
2093 if (!spec)
2094 return sprintf(buf, "(no parent image)\n");
2095
2096 count = sprintf(bufp, "pool_id %llu\npool_name %s\n",
2097 (unsigned long long) spec->pool_id, spec->pool_name);
2098 if (count < 0)
2099 return count;
2100 bufp += count;
2101
2102 count = sprintf(bufp, "image_id %s\nimage_name %s\n", spec->image_id,
2103 spec->image_name ? spec->image_name : "(unknown)");
2104 if (count < 0)
2105 return count;
2106 bufp += count;
2107
2108 count = sprintf(bufp, "snap_id %llu\nsnap_name %s\n",
2109 (unsigned long long) spec->snap_id, spec->snap_name);
2110 if (count < 0)
2111 return count;
2112 bufp += count;
2113
2114 count = sprintf(bufp, "overlap %llu\n", rbd_dev->parent_overlap);
2115 if (count < 0)
2116 return count;
2117 bufp += count;
2118
2119 return (ssize_t) (bufp - buf);
2120}
2121
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002122static ssize_t rbd_image_refresh(struct device *dev,
2123 struct device_attribute *attr,
2124 const char *buf,
2125 size_t size)
2126{
Alex Elder593a9e72012-02-07 12:03:37 -06002127 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Alex Elderb8136232012-07-25 09:32:41 -05002128 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002129
Alex Elder117973f2012-08-31 17:29:55 -05002130 ret = rbd_dev_refresh(rbd_dev, NULL);
Alex Elderb8136232012-07-25 09:32:41 -05002131
2132 return ret < 0 ? ret : size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002133}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002134
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002135static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
Alex Elder34b13182012-07-13 20:35:12 -05002136static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002137static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
2138static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
2139static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
Alex Elder9bb2f332012-07-12 10:46:35 -05002140static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002141static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
Alex Elder589d30e2012-07-10 20:30:11 -05002142static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002143static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
2144static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
Alex Elder86b00e02012-10-25 23:34:42 -05002145static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002146
2147static struct attribute *rbd_attrs[] = {
2148 &dev_attr_size.attr,
Alex Elder34b13182012-07-13 20:35:12 -05002149 &dev_attr_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002150 &dev_attr_major.attr,
2151 &dev_attr_client_id.attr,
2152 &dev_attr_pool.attr,
Alex Elder9bb2f332012-07-12 10:46:35 -05002153 &dev_attr_pool_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002154 &dev_attr_name.attr,
Alex Elder589d30e2012-07-10 20:30:11 -05002155 &dev_attr_image_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002156 &dev_attr_current_snap.attr,
Alex Elder86b00e02012-10-25 23:34:42 -05002157 &dev_attr_parent.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002158 &dev_attr_refresh.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002159 NULL
2160};
2161
2162static struct attribute_group rbd_attr_group = {
2163 .attrs = rbd_attrs,
2164};
2165
2166static const struct attribute_group *rbd_attr_groups[] = {
2167 &rbd_attr_group,
2168 NULL
2169};
2170
2171static void rbd_sysfs_dev_release(struct device *dev)
2172{
2173}
2174
2175static struct device_type rbd_device_type = {
2176 .name = "rbd",
2177 .groups = rbd_attr_groups,
2178 .release = rbd_sysfs_dev_release,
2179};
2180
2181
2182/*
2183 sysfs - snapshots
2184*/
2185
2186static ssize_t rbd_snap_size_show(struct device *dev,
2187 struct device_attribute *attr,
2188 char *buf)
2189{
2190 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2191
Josh Durgin3591538f2011-12-05 18:25:13 -08002192 return sprintf(buf, "%llu\n", (unsigned long long)snap->size);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002193}
2194
2195static ssize_t rbd_snap_id_show(struct device *dev,
2196 struct device_attribute *attr,
2197 char *buf)
2198{
2199 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2200
Josh Durgin3591538f2011-12-05 18:25:13 -08002201 return sprintf(buf, "%llu\n", (unsigned long long)snap->id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002202}
2203
Alex Elder34b13182012-07-13 20:35:12 -05002204static ssize_t rbd_snap_features_show(struct device *dev,
2205 struct device_attribute *attr,
2206 char *buf)
2207{
2208 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2209
2210 return sprintf(buf, "0x%016llx\n",
2211 (unsigned long long) snap->features);
2212}
2213
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002214static DEVICE_ATTR(snap_size, S_IRUGO, rbd_snap_size_show, NULL);
2215static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL);
Alex Elder34b13182012-07-13 20:35:12 -05002216static DEVICE_ATTR(snap_features, S_IRUGO, rbd_snap_features_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002217
2218static struct attribute *rbd_snap_attrs[] = {
2219 &dev_attr_snap_size.attr,
2220 &dev_attr_snap_id.attr,
Alex Elder34b13182012-07-13 20:35:12 -05002221 &dev_attr_snap_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002222 NULL,
2223};
2224
2225static struct attribute_group rbd_snap_attr_group = {
2226 .attrs = rbd_snap_attrs,
2227};
2228
2229static void rbd_snap_dev_release(struct device *dev)
2230{
2231 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2232 kfree(snap->name);
2233 kfree(snap);
2234}
2235
2236static const struct attribute_group *rbd_snap_attr_groups[] = {
2237 &rbd_snap_attr_group,
2238 NULL
2239};
2240
2241static struct device_type rbd_snap_device_type = {
2242 .groups = rbd_snap_attr_groups,
2243 .release = rbd_snap_dev_release,
2244};
2245
Alex Elder8b8fb992012-10-26 17:25:24 -05002246static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)
2247{
2248 kref_get(&spec->kref);
2249
2250 return spec;
2251}
2252
2253static void rbd_spec_free(struct kref *kref);
2254static void rbd_spec_put(struct rbd_spec *spec)
2255{
2256 if (spec)
2257 kref_put(&spec->kref, rbd_spec_free);
2258}
2259
2260static struct rbd_spec *rbd_spec_alloc(void)
2261{
2262 struct rbd_spec *spec;
2263
2264 spec = kzalloc(sizeof (*spec), GFP_KERNEL);
2265 if (!spec)
2266 return NULL;
2267 kref_init(&spec->kref);
2268
2269 rbd_spec_put(rbd_spec_get(spec)); /* TEMPORARY */
2270
2271 return spec;
2272}
2273
2274static void rbd_spec_free(struct kref *kref)
2275{
2276 struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref);
2277
2278 kfree(spec->pool_name);
2279 kfree(spec->image_id);
2280 kfree(spec->image_name);
2281 kfree(spec->snap_name);
2282 kfree(spec);
2283}
2284
Alex Elderc53d5892012-10-25 23:34:42 -05002285struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,
2286 struct rbd_spec *spec)
2287{
2288 struct rbd_device *rbd_dev;
2289
2290 rbd_dev = kzalloc(sizeof (*rbd_dev), GFP_KERNEL);
2291 if (!rbd_dev)
2292 return NULL;
2293
2294 spin_lock_init(&rbd_dev->lock);
Alex Elderd78b6502012-11-09 08:43:15 -06002295 atomic_set(&rbd_dev->exists, 0);
Alex Elderc53d5892012-10-25 23:34:42 -05002296 INIT_LIST_HEAD(&rbd_dev->node);
2297 INIT_LIST_HEAD(&rbd_dev->snaps);
2298 init_rwsem(&rbd_dev->header_rwsem);
2299
2300 rbd_dev->spec = spec;
2301 rbd_dev->rbd_client = rbdc;
2302
2303 return rbd_dev;
2304}
2305
2306static void rbd_dev_destroy(struct rbd_device *rbd_dev)
2307{
Alex Elder86b00e02012-10-25 23:34:42 -05002308 rbd_spec_put(rbd_dev->parent_spec);
Alex Elderc53d5892012-10-25 23:34:42 -05002309 kfree(rbd_dev->header_name);
2310 rbd_put_client(rbd_dev->rbd_client);
2311 rbd_spec_put(rbd_dev->spec);
2312 kfree(rbd_dev);
2313}
2314
Alex Elder304f6802012-08-31 17:29:52 -05002315static bool rbd_snap_registered(struct rbd_snap *snap)
2316{
2317 bool ret = snap->dev.type == &rbd_snap_device_type;
2318 bool reg = device_is_registered(&snap->dev);
2319
2320 rbd_assert(!ret ^ reg);
2321
2322 return ret;
2323}
2324
Alex Elder41f38c22012-10-25 23:34:40 -05002325static void rbd_remove_snap_dev(struct rbd_snap *snap)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002326{
2327 list_del(&snap->node);
Alex Elder304f6802012-08-31 17:29:52 -05002328 if (device_is_registered(&snap->dev))
2329 device_unregister(&snap->dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002330}
2331
Alex Elder14e70852012-07-19 09:09:27 -05002332static int rbd_register_snap_dev(struct rbd_snap *snap,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002333 struct device *parent)
2334{
2335 struct device *dev = &snap->dev;
2336 int ret;
2337
2338 dev->type = &rbd_snap_device_type;
2339 dev->parent = parent;
2340 dev->release = rbd_snap_dev_release;
Alex Elderd4b125e2012-07-03 16:01:19 -05002341 dev_set_name(dev, "%s%s", RBD_SNAP_DEV_NAME_PREFIX, snap->name);
Alex Elder304f6802012-08-31 17:29:52 -05002342 dout("%s: registering device for snapshot %s\n", __func__, snap->name);
2343
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002344 ret = device_register(dev);
2345
2346 return ret;
2347}
2348
Alex Elder4e891e02012-07-10 20:30:10 -05002349static struct rbd_snap *__rbd_add_snap_dev(struct rbd_device *rbd_dev,
Alex Elderc8d18422012-07-10 20:30:11 -05002350 const char *snap_name,
Alex Elder34b13182012-07-13 20:35:12 -05002351 u64 snap_id, u64 snap_size,
2352 u64 snap_features)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002353{
Alex Elder4e891e02012-07-10 20:30:10 -05002354 struct rbd_snap *snap;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002355 int ret;
Alex Elder4e891e02012-07-10 20:30:10 -05002356
2357 snap = kzalloc(sizeof (*snap), GFP_KERNEL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002358 if (!snap)
Alex Elder4e891e02012-07-10 20:30:10 -05002359 return ERR_PTR(-ENOMEM);
2360
2361 ret = -ENOMEM;
Alex Elderc8d18422012-07-10 20:30:11 -05002362 snap->name = kstrdup(snap_name, GFP_KERNEL);
Alex Elder4e891e02012-07-10 20:30:10 -05002363 if (!snap->name)
2364 goto err;
2365
Alex Elderc8d18422012-07-10 20:30:11 -05002366 snap->id = snap_id;
2367 snap->size = snap_size;
Alex Elder34b13182012-07-13 20:35:12 -05002368 snap->features = snap_features;
Alex Elder4e891e02012-07-10 20:30:10 -05002369
2370 return snap;
2371
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002372err:
2373 kfree(snap->name);
2374 kfree(snap);
Alex Elder4e891e02012-07-10 20:30:10 -05002375
2376 return ERR_PTR(ret);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002377}
2378
Alex Eldercd892122012-07-03 16:01:19 -05002379static char *rbd_dev_v1_snap_info(struct rbd_device *rbd_dev, u32 which,
2380 u64 *snap_size, u64 *snap_features)
2381{
2382 char *snap_name;
2383
2384 rbd_assert(which < rbd_dev->header.snapc->num_snaps);
2385
2386 *snap_size = rbd_dev->header.snap_sizes[which];
2387 *snap_features = 0; /* No features for v1 */
2388
2389 /* Skip over names until we find the one we are looking for */
2390
2391 snap_name = rbd_dev->header.snap_names;
2392 while (which--)
2393 snap_name += strlen(snap_name) + 1;
2394
2395 return snap_name;
2396}
2397
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002398/*
Alex Elder9d475de2012-07-03 16:01:19 -05002399 * Get the size and object order for an image snapshot, or if
2400 * snap_id is CEPH_NOSNAP, gets this information for the base
2401 * image.
2402 */
2403static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
2404 u8 *order, u64 *snap_size)
2405{
2406 __le64 snapid = cpu_to_le64(snap_id);
2407 int ret;
2408 struct {
2409 u8 order;
2410 __le64 size;
2411 } __attribute__ ((packed)) size_buf = { 0 };
2412
2413 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2414 "rbd", "get_size",
2415 (char *) &snapid, sizeof (snapid),
Alex Elder07b23912012-11-09 08:43:16 -06002416 (char *) &size_buf, sizeof (size_buf), NULL);
Alex Elder9d475de2012-07-03 16:01:19 -05002417 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2418 if (ret < 0)
2419 return ret;
2420
2421 *order = size_buf.order;
2422 *snap_size = le64_to_cpu(size_buf.size);
2423
2424 dout(" snap_id 0x%016llx order = %u, snap_size = %llu\n",
2425 (unsigned long long) snap_id, (unsigned int) *order,
2426 (unsigned long long) *snap_size);
2427
2428 return 0;
2429}
2430
2431static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
2432{
2433 return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
2434 &rbd_dev->header.obj_order,
2435 &rbd_dev->header.image_size);
2436}
2437
Alex Elder1e130192012-07-03 16:01:19 -05002438static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
2439{
2440 void *reply_buf;
2441 int ret;
2442 void *p;
2443
2444 reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL);
2445 if (!reply_buf)
2446 return -ENOMEM;
2447
2448 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2449 "rbd", "get_object_prefix",
2450 NULL, 0,
Alex Elder07b23912012-11-09 08:43:16 -06002451 reply_buf, RBD_OBJ_PREFIX_LEN_MAX, NULL);
Alex Elder1e130192012-07-03 16:01:19 -05002452 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),
Alex Elder07b23912012-11-09 08:43:16 -06002490 NULL);
Alex Elderb1b54022012-07-03 16:01:19 -05002491 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),
Alex Elder07b23912012-11-09 08:43:16 -06002545 (char *) reply_buf, size, NULL);
Alex Elder86b00e02012-10-25 23:34:42 -05002546 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2547 if (ret < 0)
2548 goto out_err;
2549
2550 ret = -ERANGE;
2551 p = reply_buf;
2552 end = (char *) reply_buf + size;
2553 ceph_decode_64_safe(&p, end, parent_spec->pool_id, out_err);
2554 if (parent_spec->pool_id == CEPH_NOPOOL)
2555 goto out; /* No parent? No problem. */
2556
Alex Elder979ed482012-11-01 08:39:26 -05002557 image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elder86b00e02012-10-25 23:34:42 -05002558 if (IS_ERR(image_id)) {
2559 ret = PTR_ERR(image_id);
2560 goto out_err;
2561 }
2562 parent_spec->image_id = image_id;
2563 ceph_decode_64_safe(&p, end, parent_spec->snap_id, out_err);
2564 ceph_decode_64_safe(&p, end, overlap, out_err);
2565
2566 rbd_dev->parent_overlap = overlap;
2567 rbd_dev->parent_spec = parent_spec;
2568 parent_spec = NULL; /* rbd_dev now owns this */
2569out:
2570 ret = 0;
2571out_err:
2572 kfree(reply_buf);
2573 rbd_spec_put(parent_spec);
2574
2575 return ret;
2576}
2577
Alex Elder9e15b772012-10-30 19:40:33 -05002578static char *rbd_dev_image_name(struct rbd_device *rbd_dev)
2579{
2580 size_t image_id_size;
2581 char *image_id;
2582 void *p;
2583 void *end;
2584 size_t size;
2585 void *reply_buf = NULL;
2586 size_t len = 0;
2587 char *image_name = NULL;
2588 int ret;
2589
2590 rbd_assert(!rbd_dev->spec->image_name);
2591
Alex Elder69e7a022012-11-01 08:39:26 -05002592 len = strlen(rbd_dev->spec->image_id);
2593 image_id_size = sizeof (__le32) + len;
Alex Elder9e15b772012-10-30 19:40:33 -05002594 image_id = kmalloc(image_id_size, GFP_KERNEL);
2595 if (!image_id)
2596 return NULL;
2597
2598 p = image_id;
2599 end = (char *) image_id + image_id_size;
Alex Elder69e7a022012-11-01 08:39:26 -05002600 ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32) len);
Alex Elder9e15b772012-10-30 19:40:33 -05002601
2602 size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX;
2603 reply_buf = kmalloc(size, GFP_KERNEL);
2604 if (!reply_buf)
2605 goto out;
2606
2607 ret = rbd_req_sync_exec(rbd_dev, RBD_DIRECTORY,
2608 "rbd", "dir_get_name",
2609 image_id, image_id_size,
Alex Elder07b23912012-11-09 08:43:16 -06002610 (char *) reply_buf, size, NULL);
Alex Elder9e15b772012-10-30 19:40:33 -05002611 if (ret < 0)
2612 goto out;
2613 p = reply_buf;
2614 end = (char *) reply_buf + size;
2615 image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL);
2616 if (IS_ERR(image_name))
2617 image_name = NULL;
2618 else
2619 dout("%s: name is %s len is %zd\n", __func__, image_name, len);
2620out:
2621 kfree(reply_buf);
2622 kfree(image_id);
2623
2624 return image_name;
2625}
2626
2627/*
2628 * When a parent image gets probed, we only have the pool, image,
2629 * and snapshot ids but not the names of any of them. This call
2630 * is made later to fill in those names. It has to be done after
2631 * rbd_dev_snaps_update() has completed because some of the
2632 * information (in particular, snapshot name) is not available
2633 * until then.
2634 */
2635static int rbd_dev_probe_update_spec(struct rbd_device *rbd_dev)
2636{
2637 struct ceph_osd_client *osdc;
2638 const char *name;
2639 void *reply_buf = NULL;
2640 int ret;
2641
2642 if (rbd_dev->spec->pool_name)
2643 return 0; /* Already have the names */
2644
2645 /* Look up the pool name */
2646
2647 osdc = &rbd_dev->rbd_client->client->osdc;
2648 name = ceph_pg_pool_name_by_id(osdc->osdmap, rbd_dev->spec->pool_id);
Alex Elder935dc892012-11-01 10:17:15 -05002649 if (!name) {
2650 rbd_warn(rbd_dev, "there is no pool with id %llu",
2651 rbd_dev->spec->pool_id); /* Really a BUG() */
2652 return -EIO;
2653 }
Alex Elder9e15b772012-10-30 19:40:33 -05002654
2655 rbd_dev->spec->pool_name = kstrdup(name, GFP_KERNEL);
2656 if (!rbd_dev->spec->pool_name)
2657 return -ENOMEM;
2658
2659 /* Fetch the image name; tolerate failure here */
2660
2661 name = rbd_dev_image_name(rbd_dev);
Alex Elder69e7a022012-11-01 08:39:26 -05002662 if (name)
Alex Elder9e15b772012-10-30 19:40:33 -05002663 rbd_dev->spec->image_name = (char *) name;
Alex Elder69e7a022012-11-01 08:39:26 -05002664 else
Alex Elder06ecc6c2012-11-01 10:17:15 -05002665 rbd_warn(rbd_dev, "unable to get image name");
Alex Elder9e15b772012-10-30 19:40:33 -05002666
2667 /* Look up the snapshot name. */
2668
2669 name = rbd_snap_name(rbd_dev, rbd_dev->spec->snap_id);
2670 if (!name) {
Alex Elder935dc892012-11-01 10:17:15 -05002671 rbd_warn(rbd_dev, "no snapshot with id %llu",
2672 rbd_dev->spec->snap_id); /* Really a BUG() */
Alex Elder9e15b772012-10-30 19:40:33 -05002673 ret = -EIO;
2674 goto out_err;
2675 }
2676 rbd_dev->spec->snap_name = kstrdup(name, GFP_KERNEL);
2677 if(!rbd_dev->spec->snap_name)
2678 goto out_err;
2679
2680 return 0;
2681out_err:
2682 kfree(reply_buf);
2683 kfree(rbd_dev->spec->pool_name);
2684 rbd_dev->spec->pool_name = NULL;
2685
2686 return ret;
2687}
2688
Alex Elder6e14b1a2012-07-03 16:01:19 -05002689static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev, u64 *ver)
Alex Elder35d489f2012-07-03 16:01:19 -05002690{
2691 size_t size;
2692 int ret;
2693 void *reply_buf;
2694 void *p;
2695 void *end;
2696 u64 seq;
2697 u32 snap_count;
2698 struct ceph_snap_context *snapc;
2699 u32 i;
2700
2701 /*
2702 * We'll need room for the seq value (maximum snapshot id),
2703 * snapshot count, and array of that many snapshot ids.
2704 * For now we have a fixed upper limit on the number we're
2705 * prepared to receive.
2706 */
2707 size = sizeof (__le64) + sizeof (__le32) +
2708 RBD_MAX_SNAP_COUNT * sizeof (__le64);
2709 reply_buf = kzalloc(size, GFP_KERNEL);
2710 if (!reply_buf)
2711 return -ENOMEM;
2712
2713 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2714 "rbd", "get_snapcontext",
2715 NULL, 0,
Alex Elder07b23912012-11-09 08:43:16 -06002716 reply_buf, size, ver);
Alex Elder35d489f2012-07-03 16:01:19 -05002717 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2718 if (ret < 0)
2719 goto out;
2720
2721 ret = -ERANGE;
2722 p = reply_buf;
2723 end = (char *) reply_buf + size;
2724 ceph_decode_64_safe(&p, end, seq, out);
2725 ceph_decode_32_safe(&p, end, snap_count, out);
2726
2727 /*
2728 * Make sure the reported number of snapshot ids wouldn't go
2729 * beyond the end of our buffer. But before checking that,
2730 * make sure the computed size of the snapshot context we
2731 * allocate is representable in a size_t.
2732 */
2733 if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
2734 / sizeof (u64)) {
2735 ret = -EINVAL;
2736 goto out;
2737 }
2738 if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
2739 goto out;
2740
2741 size = sizeof (struct ceph_snap_context) +
2742 snap_count * sizeof (snapc->snaps[0]);
2743 snapc = kmalloc(size, GFP_KERNEL);
2744 if (!snapc) {
2745 ret = -ENOMEM;
2746 goto out;
2747 }
2748
2749 atomic_set(&snapc->nref, 1);
2750 snapc->seq = seq;
2751 snapc->num_snaps = snap_count;
2752 for (i = 0; i < snap_count; i++)
2753 snapc->snaps[i] = ceph_decode_64(&p);
2754
2755 rbd_dev->header.snapc = snapc;
2756
2757 dout(" snap context seq = %llu, snap_count = %u\n",
2758 (unsigned long long) seq, (unsigned int) snap_count);
2759
2760out:
2761 kfree(reply_buf);
2762
2763 return 0;
2764}
2765
Alex Elderb8b1e2d2012-07-03 16:01:19 -05002766static char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, u32 which)
2767{
2768 size_t size;
2769 void *reply_buf;
2770 __le64 snap_id;
2771 int ret;
2772 void *p;
2773 void *end;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05002774 char *snap_name;
2775
2776 size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN;
2777 reply_buf = kmalloc(size, GFP_KERNEL);
2778 if (!reply_buf)
2779 return ERR_PTR(-ENOMEM);
2780
2781 snap_id = cpu_to_le64(rbd_dev->header.snapc->snaps[which]);
2782 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2783 "rbd", "get_snapshot_name",
2784 (char *) &snap_id, sizeof (snap_id),
Alex Elder07b23912012-11-09 08:43:16 -06002785 reply_buf, size, NULL);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05002786 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2787 if (ret < 0)
2788 goto out;
2789
2790 p = reply_buf;
2791 end = (char *) reply_buf + size;
Alex Eldere5c35532012-10-25 23:34:41 -05002792 snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05002793 if (IS_ERR(snap_name)) {
2794 ret = PTR_ERR(snap_name);
2795 goto out;
2796 } else {
2797 dout(" snap_id 0x%016llx snap_name = %s\n",
2798 (unsigned long long) le64_to_cpu(snap_id), snap_name);
2799 }
2800 kfree(reply_buf);
2801
2802 return snap_name;
2803out:
2804 kfree(reply_buf);
2805
2806 return ERR_PTR(ret);
2807}
2808
2809static char *rbd_dev_v2_snap_info(struct rbd_device *rbd_dev, u32 which,
2810 u64 *snap_size, u64 *snap_features)
2811{
2812 __le64 snap_id;
2813 u8 order;
2814 int ret;
2815
2816 snap_id = rbd_dev->header.snapc->snaps[which];
2817 ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, &order, snap_size);
2818 if (ret)
2819 return ERR_PTR(ret);
2820 ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, snap_features);
2821 if (ret)
2822 return ERR_PTR(ret);
2823
2824 return rbd_dev_v2_snap_name(rbd_dev, which);
2825}
2826
2827static char *rbd_dev_snap_info(struct rbd_device *rbd_dev, u32 which,
2828 u64 *snap_size, u64 *snap_features)
2829{
2830 if (rbd_dev->image_format == 1)
2831 return rbd_dev_v1_snap_info(rbd_dev, which,
2832 snap_size, snap_features);
2833 if (rbd_dev->image_format == 2)
2834 return rbd_dev_v2_snap_info(rbd_dev, which,
2835 snap_size, snap_features);
2836 return ERR_PTR(-EINVAL);
2837}
2838
Alex Elder117973f2012-08-31 17:29:55 -05002839static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev, u64 *hver)
2840{
2841 int ret;
2842 __u8 obj_order;
2843
2844 down_write(&rbd_dev->header_rwsem);
2845
2846 /* Grab old order first, to see if it changes */
2847
2848 obj_order = rbd_dev->header.obj_order,
2849 ret = rbd_dev_v2_image_size(rbd_dev);
2850 if (ret)
2851 goto out;
2852 if (rbd_dev->header.obj_order != obj_order) {
2853 ret = -EIO;
2854 goto out;
2855 }
2856 rbd_update_mapping_size(rbd_dev);
2857
2858 ret = rbd_dev_v2_snap_context(rbd_dev, hver);
2859 dout("rbd_dev_v2_snap_context returned %d\n", ret);
2860 if (ret)
2861 goto out;
2862 ret = rbd_dev_snaps_update(rbd_dev);
2863 dout("rbd_dev_snaps_update returned %d\n", ret);
2864 if (ret)
2865 goto out;
2866 ret = rbd_dev_snaps_register(rbd_dev);
2867 dout("rbd_dev_snaps_register returned %d\n", ret);
2868out:
2869 up_write(&rbd_dev->header_rwsem);
2870
2871 return ret;
2872}
2873
Alex Elder9d475de2012-07-03 16:01:19 -05002874/*
Alex Elder35938152012-08-02 11:29:46 -05002875 * Scan the rbd device's current snapshot list and compare it to the
2876 * newly-received snapshot context. Remove any existing snapshots
2877 * not present in the new snapshot context. Add a new snapshot for
2878 * any snaphots in the snapshot context not in the current list.
2879 * And verify there are no changes to snapshots we already know
2880 * about.
2881 *
2882 * Assumes the snapshots in the snapshot context are sorted by
2883 * snapshot id, highest id first. (Snapshots in the rbd_dev's list
2884 * are also maintained in that order.)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002885 */
Alex Elder304f6802012-08-31 17:29:52 -05002886static int rbd_dev_snaps_update(struct rbd_device *rbd_dev)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002887{
Alex Elder35938152012-08-02 11:29:46 -05002888 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
2889 const u32 snap_count = snapc->num_snaps;
Alex Elder35938152012-08-02 11:29:46 -05002890 struct list_head *head = &rbd_dev->snaps;
2891 struct list_head *links = head->next;
2892 u32 index = 0;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002893
Alex Elder9fcbb802012-08-23 23:48:49 -05002894 dout("%s: snap count is %u\n", __func__, (unsigned int) snap_count);
Alex Elder35938152012-08-02 11:29:46 -05002895 while (index < snap_count || links != head) {
2896 u64 snap_id;
2897 struct rbd_snap *snap;
Alex Eldercd892122012-07-03 16:01:19 -05002898 char *snap_name;
2899 u64 snap_size = 0;
2900 u64 snap_features = 0;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002901
Alex Elder35938152012-08-02 11:29:46 -05002902 snap_id = index < snap_count ? snapc->snaps[index]
2903 : CEPH_NOSNAP;
2904 snap = links != head ? list_entry(links, struct rbd_snap, node)
2905 : NULL;
Alex Elderaafb2302012-09-06 16:00:54 -05002906 rbd_assert(!snap || snap->id != CEPH_NOSNAP);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002907
Alex Elder35938152012-08-02 11:29:46 -05002908 if (snap_id == CEPH_NOSNAP || (snap && snap->id > snap_id)) {
2909 struct list_head *next = links->next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002910
Alex Elder35938152012-08-02 11:29:46 -05002911 /* Existing snapshot not in the new snap context */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002912
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002913 if (rbd_dev->spec->snap_id == snap->id)
Alex Elderd78b6502012-11-09 08:43:15 -06002914 atomic_set(&rbd_dev->exists, 0);
Alex Elder41f38c22012-10-25 23:34:40 -05002915 rbd_remove_snap_dev(snap);
Alex Elder9fcbb802012-08-23 23:48:49 -05002916 dout("%ssnap id %llu has been removed\n",
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002917 rbd_dev->spec->snap_id == snap->id ?
2918 "mapped " : "",
Alex Elder9fcbb802012-08-23 23:48:49 -05002919 (unsigned long long) snap->id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002920
Alex Elder35938152012-08-02 11:29:46 -05002921 /* Done with this list entry; advance */
2922
2923 links = next;
2924 continue;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002925 }
Alex Elder35938152012-08-02 11:29:46 -05002926
Alex Elderb8b1e2d2012-07-03 16:01:19 -05002927 snap_name = rbd_dev_snap_info(rbd_dev, index,
2928 &snap_size, &snap_features);
Alex Eldercd892122012-07-03 16:01:19 -05002929 if (IS_ERR(snap_name))
2930 return PTR_ERR(snap_name);
2931
Alex Elder9fcbb802012-08-23 23:48:49 -05002932 dout("entry %u: snap_id = %llu\n", (unsigned int) snap_count,
2933 (unsigned long long) snap_id);
Alex Elder35938152012-08-02 11:29:46 -05002934 if (!snap || (snap_id != CEPH_NOSNAP && snap->id < snap_id)) {
2935 struct rbd_snap *new_snap;
2936
2937 /* We haven't seen this snapshot before */
2938
Alex Elderc8d18422012-07-10 20:30:11 -05002939 new_snap = __rbd_add_snap_dev(rbd_dev, snap_name,
Alex Eldercd892122012-07-03 16:01:19 -05002940 snap_id, snap_size, snap_features);
Alex Elder9fcbb802012-08-23 23:48:49 -05002941 if (IS_ERR(new_snap)) {
2942 int err = PTR_ERR(new_snap);
2943
2944 dout(" failed to add dev, error %d\n", err);
2945
2946 return err;
2947 }
Alex Elder35938152012-08-02 11:29:46 -05002948
2949 /* New goes before existing, or at end of list */
2950
Alex Elder9fcbb802012-08-23 23:48:49 -05002951 dout(" added dev%s\n", snap ? "" : " at end\n");
Alex Elder35938152012-08-02 11:29:46 -05002952 if (snap)
2953 list_add_tail(&new_snap->node, &snap->node);
2954 else
Alex Elder523f3252012-08-30 00:16:37 -05002955 list_add_tail(&new_snap->node, head);
Alex Elder35938152012-08-02 11:29:46 -05002956 } else {
2957 /* Already have this one */
2958
Alex Elder9fcbb802012-08-23 23:48:49 -05002959 dout(" already present\n");
2960
Alex Eldercd892122012-07-03 16:01:19 -05002961 rbd_assert(snap->size == snap_size);
Alex Elderaafb2302012-09-06 16:00:54 -05002962 rbd_assert(!strcmp(snap->name, snap_name));
Alex Eldercd892122012-07-03 16:01:19 -05002963 rbd_assert(snap->features == snap_features);
Alex Elder35938152012-08-02 11:29:46 -05002964
2965 /* Done with this list entry; advance */
2966
2967 links = links->next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002968 }
Alex Elder35938152012-08-02 11:29:46 -05002969
2970 /* Advance to the next entry in the snapshot context */
2971
2972 index++;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002973 }
Alex Elder9fcbb802012-08-23 23:48:49 -05002974 dout("%s: done\n", __func__);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002975
2976 return 0;
2977}
2978
Alex Elder304f6802012-08-31 17:29:52 -05002979/*
2980 * Scan the list of snapshots and register the devices for any that
2981 * have not already been registered.
2982 */
2983static int rbd_dev_snaps_register(struct rbd_device *rbd_dev)
2984{
2985 struct rbd_snap *snap;
2986 int ret = 0;
2987
2988 dout("%s called\n", __func__);
Alex Elder86ff77b2012-08-31 17:29:53 -05002989 if (WARN_ON(!device_is_registered(&rbd_dev->dev)))
2990 return -EIO;
Alex Elder304f6802012-08-31 17:29:52 -05002991
2992 list_for_each_entry(snap, &rbd_dev->snaps, node) {
2993 if (!rbd_snap_registered(snap)) {
2994 ret = rbd_register_snap_dev(snap, &rbd_dev->dev);
2995 if (ret < 0)
2996 break;
2997 }
2998 }
2999 dout("%s: returning %d\n", __func__, ret);
3000
3001 return ret;
3002}
3003
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003004static int rbd_bus_add_dev(struct rbd_device *rbd_dev)
3005{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003006 struct device *dev;
Alex Eldercd789ab2012-08-30 00:16:38 -05003007 int ret;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003008
3009 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003010
Alex Eldercd789ab2012-08-30 00:16:38 -05003011 dev = &rbd_dev->dev;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003012 dev->bus = &rbd_bus_type;
3013 dev->type = &rbd_device_type;
3014 dev->parent = &rbd_root_dev;
3015 dev->release = rbd_dev_release;
Alex Elderde71a292012-07-03 16:01:19 -05003016 dev_set_name(dev, "%d", rbd_dev->dev_id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003017 ret = device_register(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003018
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003019 mutex_unlock(&ctl_mutex);
Alex Eldercd789ab2012-08-30 00:16:38 -05003020
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003021 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003022}
3023
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003024static void rbd_bus_del_dev(struct rbd_device *rbd_dev)
3025{
3026 device_unregister(&rbd_dev->dev);
3027}
3028
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003029static int rbd_init_watch_dev(struct rbd_device *rbd_dev)
3030{
3031 int ret, rc;
3032
3033 do {
Alex Elder0e6f3222012-07-25 09:32:40 -05003034 ret = rbd_req_sync_watch(rbd_dev);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003035 if (ret == -ERANGE) {
Alex Elder117973f2012-08-31 17:29:55 -05003036 rc = rbd_dev_refresh(rbd_dev, NULL);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003037 if (rc < 0)
3038 return rc;
3039 }
3040 } while (ret == -ERANGE);
3041
3042 return ret;
3043}
3044
Alex Eldere2839302012-08-29 17:11:06 -05003045static atomic64_t rbd_dev_id_max = ATOMIC64_INIT(0);
Alex Elder1ddbe942012-01-29 13:57:44 -06003046
3047/*
Alex Elder499afd52012-02-02 08:13:29 -06003048 * Get a unique rbd identifier for the given new rbd_dev, and add
3049 * the rbd_dev to the global list. The minimum rbd id is 1.
Alex Elder1ddbe942012-01-29 13:57:44 -06003050 */
Alex Eldere2839302012-08-29 17:11:06 -05003051static void rbd_dev_id_get(struct rbd_device *rbd_dev)
Alex Elderb7f23c32012-01-29 13:57:43 -06003052{
Alex Eldere2839302012-08-29 17:11:06 -05003053 rbd_dev->dev_id = atomic64_inc_return(&rbd_dev_id_max);
Alex Elder499afd52012-02-02 08:13:29 -06003054
3055 spin_lock(&rbd_dev_list_lock);
3056 list_add_tail(&rbd_dev->node, &rbd_dev_list);
3057 spin_unlock(&rbd_dev_list_lock);
Alex Eldere2839302012-08-29 17:11:06 -05003058 dout("rbd_dev %p given dev id %llu\n", rbd_dev,
3059 (unsigned long long) rbd_dev->dev_id);
Alex Elder1ddbe942012-01-29 13:57:44 -06003060}
Alex Elderb7f23c32012-01-29 13:57:43 -06003061
Alex Elder1ddbe942012-01-29 13:57:44 -06003062/*
Alex Elder499afd52012-02-02 08:13:29 -06003063 * Remove an rbd_dev from the global list, and record that its
3064 * identifier is no longer in use.
Alex Elder1ddbe942012-01-29 13:57:44 -06003065 */
Alex Eldere2839302012-08-29 17:11:06 -05003066static void rbd_dev_id_put(struct rbd_device *rbd_dev)
Alex Elder1ddbe942012-01-29 13:57:44 -06003067{
Alex Elderd184f6b2012-01-29 13:57:44 -06003068 struct list_head *tmp;
Alex Elderde71a292012-07-03 16:01:19 -05003069 int rbd_id = rbd_dev->dev_id;
Alex Elderd184f6b2012-01-29 13:57:44 -06003070 int max_id;
3071
Alex Elderaafb2302012-09-06 16:00:54 -05003072 rbd_assert(rbd_id > 0);
Alex Elder499afd52012-02-02 08:13:29 -06003073
Alex Eldere2839302012-08-29 17:11:06 -05003074 dout("rbd_dev %p released dev id %llu\n", rbd_dev,
3075 (unsigned long long) rbd_dev->dev_id);
Alex Elder499afd52012-02-02 08:13:29 -06003076 spin_lock(&rbd_dev_list_lock);
3077 list_del_init(&rbd_dev->node);
Alex Elderd184f6b2012-01-29 13:57:44 -06003078
3079 /*
3080 * If the id being "put" is not the current maximum, there
3081 * is nothing special we need to do.
3082 */
Alex Eldere2839302012-08-29 17:11:06 -05003083 if (rbd_id != atomic64_read(&rbd_dev_id_max)) {
Alex Elderd184f6b2012-01-29 13:57:44 -06003084 spin_unlock(&rbd_dev_list_lock);
3085 return;
3086 }
3087
3088 /*
3089 * We need to update the current maximum id. Search the
3090 * list to find out what it is. We're more likely to find
3091 * the maximum at the end, so search the list backward.
3092 */
3093 max_id = 0;
3094 list_for_each_prev(tmp, &rbd_dev_list) {
3095 struct rbd_device *rbd_dev;
3096
3097 rbd_dev = list_entry(tmp, struct rbd_device, node);
Alex Elderb213e0b2012-10-10 21:19:13 -07003098 if (rbd_dev->dev_id > max_id)
3099 max_id = rbd_dev->dev_id;
Alex Elderd184f6b2012-01-29 13:57:44 -06003100 }
Alex Elder499afd52012-02-02 08:13:29 -06003101 spin_unlock(&rbd_dev_list_lock);
Alex Elderb7f23c32012-01-29 13:57:43 -06003102
Alex Elder1ddbe942012-01-29 13:57:44 -06003103 /*
Alex Eldere2839302012-08-29 17:11:06 -05003104 * The max id could have been updated by rbd_dev_id_get(), in
Alex Elderd184f6b2012-01-29 13:57:44 -06003105 * which case it now accurately reflects the new maximum.
3106 * Be careful not to overwrite the maximum value in that
3107 * case.
Alex Elder1ddbe942012-01-29 13:57:44 -06003108 */
Alex Eldere2839302012-08-29 17:11:06 -05003109 atomic64_cmpxchg(&rbd_dev_id_max, rbd_id, max_id);
3110 dout(" max dev id has been reset\n");
Alex Elderb7f23c32012-01-29 13:57:43 -06003111}
3112
Alex Eldera725f65e2012-02-02 08:13:30 -06003113/*
Alex Eldere28fff262012-02-02 08:13:30 -06003114 * Skips over white space at *buf, and updates *buf to point to the
3115 * first found non-space character (if any). Returns the length of
Alex Elder593a9e72012-02-07 12:03:37 -06003116 * the token (string of non-white space characters) found. Note
3117 * that *buf must be terminated with '\0'.
Alex Eldere28fff262012-02-02 08:13:30 -06003118 */
3119static inline size_t next_token(const char **buf)
3120{
3121 /*
3122 * These are the characters that produce nonzero for
3123 * isspace() in the "C" and "POSIX" locales.
3124 */
3125 const char *spaces = " \f\n\r\t\v";
3126
3127 *buf += strspn(*buf, spaces); /* Find start of token */
3128
3129 return strcspn(*buf, spaces); /* Return token length */
3130}
3131
3132/*
3133 * Finds the next token in *buf, and if the provided token buffer is
3134 * big enough, copies the found token into it. The result, if
Alex Elder593a9e72012-02-07 12:03:37 -06003135 * copied, is guaranteed to be terminated with '\0'. Note that *buf
3136 * must be terminated with '\0' on entry.
Alex Eldere28fff262012-02-02 08:13:30 -06003137 *
3138 * Returns the length of the token found (not including the '\0').
3139 * Return value will be 0 if no token is found, and it will be >=
3140 * token_size if the token would not fit.
3141 *
Alex Elder593a9e72012-02-07 12:03:37 -06003142 * The *buf pointer will be updated to point beyond the end of the
Alex Eldere28fff262012-02-02 08:13:30 -06003143 * found token. Note that this occurs even if the token buffer is
3144 * too small to hold it.
3145 */
3146static inline size_t copy_token(const char **buf,
3147 char *token,
3148 size_t token_size)
3149{
3150 size_t len;
3151
3152 len = next_token(buf);
3153 if (len < token_size) {
3154 memcpy(token, *buf, len);
3155 *(token + len) = '\0';
3156 }
3157 *buf += len;
3158
3159 return len;
3160}
3161
3162/*
Alex Elderea3352f2012-07-09 21:04:23 -05003163 * Finds the next token in *buf, dynamically allocates a buffer big
3164 * enough to hold a copy of it, and copies the token into the new
3165 * buffer. The copy is guaranteed to be terminated with '\0'. Note
3166 * that a duplicate buffer is created even for a zero-length token.
3167 *
3168 * Returns a pointer to the newly-allocated duplicate, or a null
3169 * pointer if memory for the duplicate was not available. If
3170 * the lenp argument is a non-null pointer, the length of the token
3171 * (not including the '\0') is returned in *lenp.
3172 *
3173 * If successful, the *buf pointer will be updated to point beyond
3174 * the end of the found token.
3175 *
3176 * Note: uses GFP_KERNEL for allocation.
3177 */
3178static inline char *dup_token(const char **buf, size_t *lenp)
3179{
3180 char *dup;
3181 size_t len;
3182
3183 len = next_token(buf);
Alex Elder4caf35f2012-11-01 08:39:27 -05003184 dup = kmemdup(*buf, len + 1, GFP_KERNEL);
Alex Elderea3352f2012-07-09 21:04:23 -05003185 if (!dup)
3186 return NULL;
Alex Elderea3352f2012-07-09 21:04:23 -05003187 *(dup + len) = '\0';
3188 *buf += len;
3189
3190 if (lenp)
3191 *lenp = len;
3192
3193 return dup;
3194}
3195
3196/*
Alex Elder859c31d2012-10-25 23:34:42 -05003197 * Parse the options provided for an "rbd add" (i.e., rbd image
3198 * mapping) request. These arrive via a write to /sys/bus/rbd/add,
3199 * and the data written is passed here via a NUL-terminated buffer.
3200 * Returns 0 if successful or an error code otherwise.
Alex Elderd22f76e2012-07-12 10:46:35 -05003201 *
Alex Elder859c31d2012-10-25 23:34:42 -05003202 * The information extracted from these options is recorded in
3203 * the other parameters which return dynamically-allocated
3204 * structures:
3205 * ceph_opts
3206 * The address of a pointer that will refer to a ceph options
3207 * structure. Caller must release the returned pointer using
3208 * ceph_destroy_options() when it is no longer needed.
3209 * rbd_opts
3210 * Address of an rbd options pointer. Fully initialized by
3211 * this function; caller must release with kfree().
3212 * spec
3213 * Address of an rbd image specification pointer. Fully
3214 * initialized by this function based on parsed options.
3215 * Caller must release with rbd_spec_put().
3216 *
3217 * The options passed take this form:
3218 * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
3219 * where:
3220 * <mon_addrs>
3221 * A comma-separated list of one or more monitor addresses.
3222 * A monitor address is an ip address, optionally followed
3223 * by a port number (separated by a colon).
3224 * I.e.: ip1[:port1][,ip2[:port2]...]
3225 * <options>
3226 * A comma-separated list of ceph and/or rbd options.
3227 * <pool_name>
3228 * The name of the rados pool containing the rbd image.
3229 * <image_name>
3230 * The name of the image in that pool to map.
3231 * <snap_id>
3232 * An optional snapshot id. If provided, the mapping will
3233 * present data from the image at the time that snapshot was
3234 * created. The image head is used if no snapshot id is
3235 * provided. Snapshot mappings are always read-only.
Alex Eldera725f65e2012-02-02 08:13:30 -06003236 */
Alex Elder859c31d2012-10-25 23:34:42 -05003237static int rbd_add_parse_args(const char *buf,
Alex Elderdc79b112012-10-25 23:34:41 -05003238 struct ceph_options **ceph_opts,
Alex Elder859c31d2012-10-25 23:34:42 -05003239 struct rbd_options **opts,
3240 struct rbd_spec **rbd_spec)
Alex Eldera725f65e2012-02-02 08:13:30 -06003241{
Alex Elderd22f76e2012-07-12 10:46:35 -05003242 size_t len;
Alex Elder859c31d2012-10-25 23:34:42 -05003243 char *options;
Alex Elder0ddebc02012-10-25 23:34:41 -05003244 const char *mon_addrs;
3245 size_t mon_addrs_size;
Alex Elder859c31d2012-10-25 23:34:42 -05003246 struct rbd_spec *spec = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05003247 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05003248 struct ceph_options *copts;
Alex Elderdc79b112012-10-25 23:34:41 -05003249 int ret;
Alex Eldere28fff262012-02-02 08:13:30 -06003250
3251 /* The first four tokens are required */
3252
Alex Elder7ef32142012-02-02 08:13:30 -06003253 len = next_token(&buf);
Alex Elder4fb5d6712012-11-01 10:17:15 -05003254 if (!len) {
3255 rbd_warn(NULL, "no monitor address(es) provided");
3256 return -EINVAL;
3257 }
Alex Elder0ddebc02012-10-25 23:34:41 -05003258 mon_addrs = buf;
Alex Elderf28e5652012-10-25 23:34:41 -05003259 mon_addrs_size = len + 1;
Alex Elder7ef32142012-02-02 08:13:30 -06003260 buf += len;
Alex Eldera725f65e2012-02-02 08:13:30 -06003261
Alex Elderdc79b112012-10-25 23:34:41 -05003262 ret = -EINVAL;
Alex Elderf28e5652012-10-25 23:34:41 -05003263 options = dup_token(&buf, NULL);
3264 if (!options)
Alex Elderdc79b112012-10-25 23:34:41 -05003265 return -ENOMEM;
Alex Elder4fb5d6712012-11-01 10:17:15 -05003266 if (!*options) {
3267 rbd_warn(NULL, "no options provided");
3268 goto out_err;
3269 }
Alex Eldera725f65e2012-02-02 08:13:30 -06003270
Alex Elder859c31d2012-10-25 23:34:42 -05003271 spec = rbd_spec_alloc();
3272 if (!spec)
Alex Elderf28e5652012-10-25 23:34:41 -05003273 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05003274
3275 spec->pool_name = dup_token(&buf, NULL);
3276 if (!spec->pool_name)
3277 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05003278 if (!*spec->pool_name) {
3279 rbd_warn(NULL, "no pool name provided");
3280 goto out_err;
3281 }
Alex Eldere28fff262012-02-02 08:13:30 -06003282
Alex Elder69e7a022012-11-01 08:39:26 -05003283 spec->image_name = dup_token(&buf, NULL);
Alex Elder859c31d2012-10-25 23:34:42 -05003284 if (!spec->image_name)
Alex Elderf28e5652012-10-25 23:34:41 -05003285 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05003286 if (!*spec->image_name) {
3287 rbd_warn(NULL, "no image name provided");
3288 goto out_err;
3289 }
Alex Eldere28fff262012-02-02 08:13:30 -06003290
Alex Elderf28e5652012-10-25 23:34:41 -05003291 /*
3292 * Snapshot name is optional; default is to use "-"
3293 * (indicating the head/no snapshot).
3294 */
Alex Elder3feeb8942012-08-31 17:29:52 -05003295 len = next_token(&buf);
Alex Elder820a5f32012-07-09 21:04:24 -05003296 if (!len) {
Alex Elder3feeb8942012-08-31 17:29:52 -05003297 buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
3298 len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
Alex Elderf28e5652012-10-25 23:34:41 -05003299 } else if (len > RBD_MAX_SNAP_NAME_LEN) {
Alex Elderdc79b112012-10-25 23:34:41 -05003300 ret = -ENAMETOOLONG;
Alex Elderf28e5652012-10-25 23:34:41 -05003301 goto out_err;
Alex Elder849b4262012-07-09 21:04:24 -05003302 }
Alex Elder4caf35f2012-11-01 08:39:27 -05003303 spec->snap_name = kmemdup(buf, len + 1, GFP_KERNEL);
Alex Elder859c31d2012-10-25 23:34:42 -05003304 if (!spec->snap_name)
Alex Elderf28e5652012-10-25 23:34:41 -05003305 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05003306 *(spec->snap_name + len) = '\0';
Alex Eldere5c35532012-10-25 23:34:41 -05003307
Alex Elder0ddebc02012-10-25 23:34:41 -05003308 /* Initialize all rbd options to the defaults */
Alex Eldere28fff262012-02-02 08:13:30 -06003309
Alex Elder4e9afeb2012-10-25 23:34:41 -05003310 rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL);
3311 if (!rbd_opts)
3312 goto out_mem;
3313
3314 rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
Alex Elderd22f76e2012-07-12 10:46:35 -05003315
Alex Elder859c31d2012-10-25 23:34:42 -05003316 copts = ceph_parse_options(options, mon_addrs,
Alex Elder0ddebc02012-10-25 23:34:41 -05003317 mon_addrs + mon_addrs_size - 1,
Alex Elder4e9afeb2012-10-25 23:34:41 -05003318 parse_rbd_opts_token, rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05003319 if (IS_ERR(copts)) {
3320 ret = PTR_ERR(copts);
Alex Elderdc79b112012-10-25 23:34:41 -05003321 goto out_err;
3322 }
Alex Elder859c31d2012-10-25 23:34:42 -05003323 kfree(options);
3324
3325 *ceph_opts = copts;
Alex Elder4e9afeb2012-10-25 23:34:41 -05003326 *opts = rbd_opts;
Alex Elder859c31d2012-10-25 23:34:42 -05003327 *rbd_spec = spec;
Alex Elder0ddebc02012-10-25 23:34:41 -05003328
Alex Elderdc79b112012-10-25 23:34:41 -05003329 return 0;
Alex Elderf28e5652012-10-25 23:34:41 -05003330out_mem:
Alex Elderdc79b112012-10-25 23:34:41 -05003331 ret = -ENOMEM;
Alex Elderd22f76e2012-07-12 10:46:35 -05003332out_err:
Alex Elder859c31d2012-10-25 23:34:42 -05003333 kfree(rbd_opts);
3334 rbd_spec_put(spec);
Alex Elderf28e5652012-10-25 23:34:41 -05003335 kfree(options);
Alex Elderd22f76e2012-07-12 10:46:35 -05003336
Alex Elderdc79b112012-10-25 23:34:41 -05003337 return ret;
Alex Eldera725f65e2012-02-02 08:13:30 -06003338}
3339
Alex Elder589d30e2012-07-10 20:30:11 -05003340/*
3341 * An rbd format 2 image has a unique identifier, distinct from the
3342 * name given to it by the user. Internally, that identifier is
3343 * what's used to specify the names of objects related to the image.
3344 *
3345 * A special "rbd id" object is used to map an rbd image name to its
3346 * id. If that object doesn't exist, then there is no v2 rbd image
3347 * with the supplied name.
3348 *
3349 * This function will record the given rbd_dev's image_id field if
3350 * it can be determined, and in that case will return 0. If any
3351 * errors occur a negative errno will be returned and the rbd_dev's
3352 * image_id field will be unchanged (and should be NULL).
3353 */
3354static int rbd_dev_image_id(struct rbd_device *rbd_dev)
3355{
3356 int ret;
3357 size_t size;
3358 char *object_name;
3359 void *response;
3360 void *p;
3361
3362 /*
Alex Elder2c0d0a12012-10-30 19:40:33 -05003363 * When probing a parent image, the image id is already
3364 * known (and the image name likely is not). There's no
3365 * need to fetch the image id again in this case.
3366 */
3367 if (rbd_dev->spec->image_id)
3368 return 0;
3369
3370 /*
Alex Elder589d30e2012-07-10 20:30:11 -05003371 * First, see if the format 2 image id file exists, and if
3372 * so, get the image's persistent id from it.
3373 */
Alex Elder69e7a022012-11-01 08:39:26 -05003374 size = sizeof (RBD_ID_PREFIX) + strlen(rbd_dev->spec->image_name);
Alex Elder589d30e2012-07-10 20:30:11 -05003375 object_name = kmalloc(size, GFP_NOIO);
3376 if (!object_name)
3377 return -ENOMEM;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003378 sprintf(object_name, "%s%s", RBD_ID_PREFIX, rbd_dev->spec->image_name);
Alex Elder589d30e2012-07-10 20:30:11 -05003379 dout("rbd id object name is %s\n", object_name);
3380
3381 /* Response will be an encoded string, which includes a length */
3382
3383 size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
3384 response = kzalloc(size, GFP_NOIO);
3385 if (!response) {
3386 ret = -ENOMEM;
3387 goto out;
3388 }
3389
3390 ret = rbd_req_sync_exec(rbd_dev, object_name,
3391 "rbd", "get_id",
3392 NULL, 0,
Alex Elder07b23912012-11-09 08:43:16 -06003393 response, RBD_IMAGE_ID_LEN_MAX, NULL);
Alex Elder589d30e2012-07-10 20:30:11 -05003394 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
3395 if (ret < 0)
3396 goto out;
Alex Eldera0ea3a42012-10-10 21:19:13 -07003397 ret = 0; /* rbd_req_sync_exec() can return positive */
Alex Elder589d30e2012-07-10 20:30:11 -05003398
3399 p = response;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003400 rbd_dev->spec->image_id = ceph_extract_encoded_string(&p,
Alex Elder589d30e2012-07-10 20:30:11 -05003401 p + RBD_IMAGE_ID_LEN_MAX,
Alex Elder979ed482012-11-01 08:39:26 -05003402 NULL, GFP_NOIO);
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003403 if (IS_ERR(rbd_dev->spec->image_id)) {
3404 ret = PTR_ERR(rbd_dev->spec->image_id);
3405 rbd_dev->spec->image_id = NULL;
Alex Elder589d30e2012-07-10 20:30:11 -05003406 } else {
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003407 dout("image_id is %s\n", rbd_dev->spec->image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05003408 }
3409out:
3410 kfree(response);
3411 kfree(object_name);
3412
3413 return ret;
3414}
3415
Alex Eldera30b71b2012-07-10 20:30:11 -05003416static int rbd_dev_v1_probe(struct rbd_device *rbd_dev)
3417{
3418 int ret;
3419 size_t size;
3420
3421 /* Version 1 images have no id; empty string is used */
3422
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003423 rbd_dev->spec->image_id = kstrdup("", GFP_KERNEL);
3424 if (!rbd_dev->spec->image_id)
Alex Eldera30b71b2012-07-10 20:30:11 -05003425 return -ENOMEM;
Alex Eldera30b71b2012-07-10 20:30:11 -05003426
3427 /* Record the header object name for this rbd image. */
3428
Alex Elder69e7a022012-11-01 08:39:26 -05003429 size = strlen(rbd_dev->spec->image_name) + sizeof (RBD_SUFFIX);
Alex Eldera30b71b2012-07-10 20:30:11 -05003430 rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
3431 if (!rbd_dev->header_name) {
3432 ret = -ENOMEM;
3433 goto out_err;
3434 }
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003435 sprintf(rbd_dev->header_name, "%s%s",
3436 rbd_dev->spec->image_name, RBD_SUFFIX);
Alex Eldera30b71b2012-07-10 20:30:11 -05003437
3438 /* Populate rbd image metadata */
3439
3440 ret = rbd_read_header(rbd_dev, &rbd_dev->header);
3441 if (ret < 0)
3442 goto out_err;
Alex Elder86b00e02012-10-25 23:34:42 -05003443
3444 /* Version 1 images have no parent (no layering) */
3445
3446 rbd_dev->parent_spec = NULL;
3447 rbd_dev->parent_overlap = 0;
3448
Alex Eldera30b71b2012-07-10 20:30:11 -05003449 rbd_dev->image_format = 1;
3450
3451 dout("discovered version 1 image, header name is %s\n",
3452 rbd_dev->header_name);
3453
3454 return 0;
3455
3456out_err:
3457 kfree(rbd_dev->header_name);
3458 rbd_dev->header_name = NULL;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003459 kfree(rbd_dev->spec->image_id);
3460 rbd_dev->spec->image_id = NULL;
Alex Eldera30b71b2012-07-10 20:30:11 -05003461
3462 return ret;
3463}
3464
3465static int rbd_dev_v2_probe(struct rbd_device *rbd_dev)
3466{
3467 size_t size;
Alex Elder9d475de2012-07-03 16:01:19 -05003468 int ret;
Alex Elder6e14b1a2012-07-03 16:01:19 -05003469 u64 ver = 0;
Alex Eldera30b71b2012-07-10 20:30:11 -05003470
3471 /*
3472 * Image id was filled in by the caller. Record the header
3473 * object name for this rbd image.
3474 */
Alex Elder979ed482012-11-01 08:39:26 -05003475 size = sizeof (RBD_HEADER_PREFIX) + strlen(rbd_dev->spec->image_id);
Alex Eldera30b71b2012-07-10 20:30:11 -05003476 rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
3477 if (!rbd_dev->header_name)
3478 return -ENOMEM;
3479 sprintf(rbd_dev->header_name, "%s%s",
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003480 RBD_HEADER_PREFIX, rbd_dev->spec->image_id);
Alex Elder9d475de2012-07-03 16:01:19 -05003481
3482 /* Get the size and object order for the image */
3483
3484 ret = rbd_dev_v2_image_size(rbd_dev);
3485 if (ret < 0)
3486 goto out_err;
Alex Elder1e130192012-07-03 16:01:19 -05003487
3488 /* Get the object prefix (a.k.a. block_name) for the image */
3489
3490 ret = rbd_dev_v2_object_prefix(rbd_dev);
3491 if (ret < 0)
3492 goto out_err;
Alex Elderb1b54022012-07-03 16:01:19 -05003493
Alex Elderd8891402012-10-09 13:50:17 -07003494 /* Get the and check features for the image */
Alex Elderb1b54022012-07-03 16:01:19 -05003495
3496 ret = rbd_dev_v2_features(rbd_dev);
3497 if (ret < 0)
3498 goto out_err;
Alex Elder35d489f2012-07-03 16:01:19 -05003499
Alex Elder86b00e02012-10-25 23:34:42 -05003500 /* If the image supports layering, get the parent info */
3501
3502 if (rbd_dev->header.features & RBD_FEATURE_LAYERING) {
3503 ret = rbd_dev_v2_parent_info(rbd_dev);
3504 if (ret < 0)
3505 goto out_err;
3506 }
3507
Alex Elder6e14b1a2012-07-03 16:01:19 -05003508 /* crypto and compression type aren't (yet) supported for v2 images */
Alex Elder35d489f2012-07-03 16:01:19 -05003509
Alex Elder6e14b1a2012-07-03 16:01:19 -05003510 rbd_dev->header.crypt_type = 0;
3511 rbd_dev->header.comp_type = 0;
3512
3513 /* Get the snapshot context, plus the header version */
3514
3515 ret = rbd_dev_v2_snap_context(rbd_dev, &ver);
Alex Elder35d489f2012-07-03 16:01:19 -05003516 if (ret)
3517 goto out_err;
Alex Elder6e14b1a2012-07-03 16:01:19 -05003518 rbd_dev->header.obj_version = ver;
3519
Alex Eldera30b71b2012-07-10 20:30:11 -05003520 rbd_dev->image_format = 2;
3521
3522 dout("discovered version 2 image, header name is %s\n",
3523 rbd_dev->header_name);
3524
Alex Elder35152972012-08-31 17:29:55 -05003525 return 0;
Alex Elder9d475de2012-07-03 16:01:19 -05003526out_err:
Alex Elder86b00e02012-10-25 23:34:42 -05003527 rbd_dev->parent_overlap = 0;
3528 rbd_spec_put(rbd_dev->parent_spec);
3529 rbd_dev->parent_spec = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05003530 kfree(rbd_dev->header_name);
3531 rbd_dev->header_name = NULL;
Alex Elder1e130192012-07-03 16:01:19 -05003532 kfree(rbd_dev->header.object_prefix);
3533 rbd_dev->header.object_prefix = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05003534
3535 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05003536}
3537
Alex Elder83a06262012-10-30 15:47:17 -05003538static int rbd_dev_probe_finish(struct rbd_device *rbd_dev)
3539{
3540 int ret;
3541
3542 /* no need to lock here, as rbd_dev is not registered yet */
3543 ret = rbd_dev_snaps_update(rbd_dev);
3544 if (ret)
3545 return ret;
3546
Alex Elder9e15b772012-10-30 19:40:33 -05003547 ret = rbd_dev_probe_update_spec(rbd_dev);
3548 if (ret)
3549 goto err_out_snaps;
3550
Alex Elder83a06262012-10-30 15:47:17 -05003551 ret = rbd_dev_set_mapping(rbd_dev);
3552 if (ret)
3553 goto err_out_snaps;
3554
3555 /* generate unique id: find highest unique id, add one */
3556 rbd_dev_id_get(rbd_dev);
3557
3558 /* Fill in the device name, now that we have its id. */
3559 BUILD_BUG_ON(DEV_NAME_LEN
3560 < sizeof (RBD_DRV_NAME) + MAX_INT_FORMAT_WIDTH);
3561 sprintf(rbd_dev->name, "%s%d", RBD_DRV_NAME, rbd_dev->dev_id);
3562
3563 /* Get our block major device number. */
3564
3565 ret = register_blkdev(0, rbd_dev->name);
3566 if (ret < 0)
3567 goto err_out_id;
3568 rbd_dev->major = ret;
3569
3570 /* Set up the blkdev mapping. */
3571
3572 ret = rbd_init_disk(rbd_dev);
3573 if (ret)
3574 goto err_out_blkdev;
3575
3576 ret = rbd_bus_add_dev(rbd_dev);
3577 if (ret)
3578 goto err_out_disk;
3579
3580 /*
3581 * At this point cleanup in the event of an error is the job
3582 * of the sysfs code (initiated by rbd_bus_del_dev()).
3583 */
3584 down_write(&rbd_dev->header_rwsem);
3585 ret = rbd_dev_snaps_register(rbd_dev);
3586 up_write(&rbd_dev->header_rwsem);
3587 if (ret)
3588 goto err_out_bus;
3589
3590 ret = rbd_init_watch_dev(rbd_dev);
3591 if (ret)
3592 goto err_out_bus;
3593
3594 /* Everything's ready. Announce the disk to the world. */
3595
3596 add_disk(rbd_dev->disk);
3597
3598 pr_info("%s: added with size 0x%llx\n", rbd_dev->disk->disk_name,
3599 (unsigned long long) rbd_dev->mapping.size);
3600
3601 return ret;
3602err_out_bus:
3603 /* this will also clean up rest of rbd_dev stuff */
3604
3605 rbd_bus_del_dev(rbd_dev);
3606
3607 return ret;
3608err_out_disk:
3609 rbd_free_disk(rbd_dev);
3610err_out_blkdev:
3611 unregister_blkdev(rbd_dev->major, rbd_dev->name);
3612err_out_id:
3613 rbd_dev_id_put(rbd_dev);
3614err_out_snaps:
3615 rbd_remove_all_snaps(rbd_dev);
3616
3617 return ret;
3618}
3619
Alex Eldera30b71b2012-07-10 20:30:11 -05003620/*
3621 * Probe for the existence of the header object for the given rbd
3622 * device. For format 2 images this includes determining the image
3623 * id.
3624 */
3625static int rbd_dev_probe(struct rbd_device *rbd_dev)
3626{
3627 int ret;
3628
3629 /*
3630 * Get the id from the image id object. If it's not a
3631 * format 2 image, we'll get ENOENT back, and we'll assume
3632 * it's a format 1 image.
3633 */
3634 ret = rbd_dev_image_id(rbd_dev);
3635 if (ret)
3636 ret = rbd_dev_v1_probe(rbd_dev);
3637 else
3638 ret = rbd_dev_v2_probe(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05003639 if (ret) {
Alex Eldera30b71b2012-07-10 20:30:11 -05003640 dout("probe failed, returning %d\n", ret);
3641
Alex Elder83a06262012-10-30 15:47:17 -05003642 return ret;
3643 }
3644
3645 ret = rbd_dev_probe_finish(rbd_dev);
3646 if (ret)
3647 rbd_header_free(&rbd_dev->header);
3648
Alex Eldera30b71b2012-07-10 20:30:11 -05003649 return ret;
3650}
3651
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003652static ssize_t rbd_add(struct bus_type *bus,
3653 const char *buf,
3654 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003655{
Alex Eldercb8627c2012-07-09 21:04:23 -05003656 struct rbd_device *rbd_dev = NULL;
Alex Elderdc79b112012-10-25 23:34:41 -05003657 struct ceph_options *ceph_opts = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05003658 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05003659 struct rbd_spec *spec = NULL;
Alex Elder9d3997f2012-10-25 23:34:42 -05003660 struct rbd_client *rbdc;
Alex Elder27cc2592012-02-02 08:13:30 -06003661 struct ceph_osd_client *osdc;
3662 int rc = -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003663
3664 if (!try_module_get(THIS_MODULE))
3665 return -ENODEV;
3666
Alex Eldera725f65e2012-02-02 08:13:30 -06003667 /* parse add command */
Alex Elder859c31d2012-10-25 23:34:42 -05003668 rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec);
Alex Elderdc79b112012-10-25 23:34:41 -05003669 if (rc < 0)
Alex Elderbd4ba652012-10-25 23:34:42 -05003670 goto err_out_module;
Alex Eldera725f65e2012-02-02 08:13:30 -06003671
Alex Elder9d3997f2012-10-25 23:34:42 -05003672 rbdc = rbd_get_client(ceph_opts);
3673 if (IS_ERR(rbdc)) {
3674 rc = PTR_ERR(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05003675 goto err_out_args;
Alex Elder9d3997f2012-10-25 23:34:42 -05003676 }
Alex Elderc53d5892012-10-25 23:34:42 -05003677 ceph_opts = NULL; /* rbd_dev client now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003678
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003679 /* pick the pool */
Alex Elder9d3997f2012-10-25 23:34:42 -05003680 osdc = &rbdc->client->osdc;
Alex Elder859c31d2012-10-25 23:34:42 -05003681 rc = ceph_pg_poolid_by_name(osdc->osdmap, spec->pool_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003682 if (rc < 0)
3683 goto err_out_client;
Alex Elder859c31d2012-10-25 23:34:42 -05003684 spec->pool_id = (u64) rc;
3685
Alex Elderc53d5892012-10-25 23:34:42 -05003686 rbd_dev = rbd_dev_create(rbdc, spec);
Alex Elderbd4ba652012-10-25 23:34:42 -05003687 if (!rbd_dev)
3688 goto err_out_client;
Alex Elderc53d5892012-10-25 23:34:42 -05003689 rbdc = NULL; /* rbd_dev now owns this */
3690 spec = NULL; /* rbd_dev now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003691
Alex Elderbd4ba652012-10-25 23:34:42 -05003692 rbd_dev->mapping.read_only = rbd_opts->read_only;
Alex Elderc53d5892012-10-25 23:34:42 -05003693 kfree(rbd_opts);
3694 rbd_opts = NULL; /* done with this */
Alex Elderbd4ba652012-10-25 23:34:42 -05003695
Alex Eldera30b71b2012-07-10 20:30:11 -05003696 rc = rbd_dev_probe(rbd_dev);
3697 if (rc < 0)
Alex Elderc53d5892012-10-25 23:34:42 -05003698 goto err_out_rbd_dev;
Alex Elder05fd6f62012-08-29 17:11:07 -05003699
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003700 return count;
Alex Elderc53d5892012-10-25 23:34:42 -05003701err_out_rbd_dev:
3702 rbd_dev_destroy(rbd_dev);
Alex Elderbd4ba652012-10-25 23:34:42 -05003703err_out_client:
Alex Elder9d3997f2012-10-25 23:34:42 -05003704 rbd_put_client(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05003705err_out_args:
Alex Elder78cea762012-10-25 23:34:41 -05003706 if (ceph_opts)
3707 ceph_destroy_options(ceph_opts);
Alex Elder4e9afeb2012-10-25 23:34:41 -05003708 kfree(rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05003709 rbd_spec_put(spec);
Alex Elderbd4ba652012-10-25 23:34:42 -05003710err_out_module:
3711 module_put(THIS_MODULE);
Alex Elder27cc2592012-02-02 08:13:30 -06003712
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003713 dout("Error adding device %s\n", buf);
Alex Elder27cc2592012-02-02 08:13:30 -06003714
3715 return (ssize_t) rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003716}
3717
Alex Elderde71a292012-07-03 16:01:19 -05003718static struct rbd_device *__rbd_get_dev(unsigned long dev_id)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003719{
3720 struct list_head *tmp;
3721 struct rbd_device *rbd_dev;
3722
Alex Eldere124a82f2012-01-29 13:57:44 -06003723 spin_lock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003724 list_for_each(tmp, &rbd_dev_list) {
3725 rbd_dev = list_entry(tmp, struct rbd_device, node);
Alex Elderde71a292012-07-03 16:01:19 -05003726 if (rbd_dev->dev_id == dev_id) {
Alex Eldere124a82f2012-01-29 13:57:44 -06003727 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003728 return rbd_dev;
Alex Eldere124a82f2012-01-29 13:57:44 -06003729 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003730 }
Alex Eldere124a82f2012-01-29 13:57:44 -06003731 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003732 return NULL;
3733}
3734
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003735static void rbd_dev_release(struct device *dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003736{
Alex Elder593a9e72012-02-07 12:03:37 -06003737 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003738
Alex Elder1dbb4392012-01-24 10:08:37 -06003739 if (rbd_dev->watch_request) {
3740 struct ceph_client *client = rbd_dev->rbd_client->client;
3741
3742 ceph_osdc_unregister_linger_request(&client->osdc,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003743 rbd_dev->watch_request);
Alex Elder1dbb4392012-01-24 10:08:37 -06003744 }
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003745 if (rbd_dev->watch_event)
Alex Elder070c6332012-07-25 09:32:41 -05003746 rbd_req_sync_unwatch(rbd_dev);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003747
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003748
3749 /* clean up and free blkdev */
3750 rbd_free_disk(rbd_dev);
3751 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Alex Elder32eec682012-02-08 16:11:14 -06003752
Alex Elder2ac4e752012-07-10 20:30:10 -05003753 /* release allocated disk header fields */
3754 rbd_header_free(&rbd_dev->header);
3755
Alex Elder32eec682012-02-08 16:11:14 -06003756 /* done with the id, and with the rbd_dev */
Alex Eldere2839302012-08-29 17:11:06 -05003757 rbd_dev_id_put(rbd_dev);
Alex Elderc53d5892012-10-25 23:34:42 -05003758 rbd_assert(rbd_dev->rbd_client != NULL);
3759 rbd_dev_destroy(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003760
3761 /* release module ref */
3762 module_put(THIS_MODULE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003763}
3764
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003765static ssize_t rbd_remove(struct bus_type *bus,
3766 const char *buf,
3767 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003768{
3769 struct rbd_device *rbd_dev = NULL;
3770 int target_id, rc;
3771 unsigned long ul;
3772 int ret = count;
3773
3774 rc = strict_strtoul(buf, 10, &ul);
3775 if (rc)
3776 return rc;
3777
3778 /* convert to int; abort if we lost anything in the conversion */
3779 target_id = (int) ul;
3780 if (target_id != ul)
3781 return -EINVAL;
3782
3783 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
3784
3785 rbd_dev = __rbd_get_dev(target_id);
3786 if (!rbd_dev) {
3787 ret = -ENOENT;
3788 goto done;
3789 }
3790
Alex Elder42382b72012-11-16 09:29:16 -06003791 if (rbd_dev->open_count) {
3792 ret = -EBUSY;
3793 goto done;
3794 }
3795
Alex Elder41f38c22012-10-25 23:34:40 -05003796 rbd_remove_all_snaps(rbd_dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003797 rbd_bus_del_dev(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003798
3799done:
3800 mutex_unlock(&ctl_mutex);
Alex Elderaafb2302012-09-06 16:00:54 -05003801
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003802 return ret;
3803}
3804
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003805/*
3806 * create control files in sysfs
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003807 * /sys/bus/rbd/...
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003808 */
3809static int rbd_sysfs_init(void)
3810{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003811 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003812
Alex Elderfed4c142012-02-07 12:03:36 -06003813 ret = device_register(&rbd_root_dev);
Alex Elder21079782012-01-24 10:08:36 -06003814 if (ret < 0)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003815 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003816
Alex Elderfed4c142012-02-07 12:03:36 -06003817 ret = bus_register(&rbd_bus_type);
3818 if (ret < 0)
3819 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003820
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003821 return ret;
3822}
3823
3824static void rbd_sysfs_cleanup(void)
3825{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003826 bus_unregister(&rbd_bus_type);
Alex Elderfed4c142012-02-07 12:03:36 -06003827 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003828}
3829
3830int __init rbd_init(void)
3831{
3832 int rc;
3833
3834 rc = rbd_sysfs_init();
3835 if (rc)
3836 return rc;
Alex Elderf0f8cef2012-01-29 13:57:44 -06003837 pr_info("loaded " RBD_DRV_NAME_LONG "\n");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003838 return 0;
3839}
3840
3841void __exit rbd_exit(void)
3842{
3843 rbd_sysfs_cleanup();
3844}
3845
3846module_init(rbd_init);
3847module_exit(rbd_exit);
3848
3849MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
3850MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
3851MODULE_DESCRIPTION("rados block device");
3852
3853/* following authorship retained from original osdblk.c */
3854MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
3855
3856MODULE_LICENSE("GPL");