blob: d2a6e9589fabcc142e862f6fca6072fb891d9181 [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
Alex Elder0903e872012-11-14 12:25:19 -0600238 struct ceph_file_layout layout;
239
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700240 struct ceph_osd_event *watch_event;
241 struct ceph_osd_request *watch_request;
242
Alex Elder86b00e02012-10-25 23:34:42 -0500243 struct rbd_spec *parent_spec;
244 u64 parent_overlap;
245
Josh Durginc6666012011-11-21 17:11:12 -0800246 /* protects updating the header */
247 struct rw_semaphore header_rwsem;
Alex Elderf84344f2012-08-31 17:29:51 -0500248
249 struct rbd_mapping mapping;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700250
251 struct list_head node;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800252
253 /* list of snapshots */
254 struct list_head snaps;
255
256 /* sysfs related */
257 struct device dev;
Alex Elder42382b72012-11-16 09:29:16 -0600258 unsigned long open_count;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800259};
260
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700261static DEFINE_MUTEX(ctl_mutex); /* Serialize open/close/setup/teardown */
Alex Eldere124a82f2012-01-29 13:57:44 -0600262
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700263static LIST_HEAD(rbd_dev_list); /* devices */
Alex Eldere124a82f2012-01-29 13:57:44 -0600264static DEFINE_SPINLOCK(rbd_dev_list_lock);
265
Alex Elder432b8582012-01-29 13:57:44 -0600266static LIST_HEAD(rbd_client_list); /* clients */
267static DEFINE_SPINLOCK(rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700268
Alex Elder304f6802012-08-31 17:29:52 -0500269static int rbd_dev_snaps_update(struct rbd_device *rbd_dev);
270static int rbd_dev_snaps_register(struct rbd_device *rbd_dev);
271
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800272static void rbd_dev_release(struct device *dev);
Alex Elder41f38c22012-10-25 23:34:40 -0500273static void rbd_remove_snap_dev(struct rbd_snap *snap);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800274
Alex Elderf0f8cef2012-01-29 13:57:44 -0600275static ssize_t rbd_add(struct bus_type *bus, const char *buf,
276 size_t count);
277static ssize_t rbd_remove(struct bus_type *bus, const char *buf,
278 size_t count);
279
280static struct bus_attribute rbd_bus_attrs[] = {
281 __ATTR(add, S_IWUSR, NULL, rbd_add),
282 __ATTR(remove, S_IWUSR, NULL, rbd_remove),
283 __ATTR_NULL
284};
285
286static struct bus_type rbd_bus_type = {
287 .name = "rbd",
288 .bus_attrs = rbd_bus_attrs,
289};
290
291static void rbd_root_dev_release(struct device *dev)
292{
293}
294
295static struct device rbd_root_dev = {
296 .init_name = "rbd",
297 .release = rbd_root_dev_release,
298};
299
Alex Elder06ecc6c2012-11-01 10:17:15 -0500300static __printf(2, 3)
301void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...)
302{
303 struct va_format vaf;
304 va_list args;
305
306 va_start(args, fmt);
307 vaf.fmt = fmt;
308 vaf.va = &args;
309
310 if (!rbd_dev)
311 printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf);
312 else if (rbd_dev->disk)
313 printk(KERN_WARNING "%s: %s: %pV\n",
314 RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf);
315 else if (rbd_dev->spec && rbd_dev->spec->image_name)
316 printk(KERN_WARNING "%s: image %s: %pV\n",
317 RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf);
318 else if (rbd_dev->spec && rbd_dev->spec->image_id)
319 printk(KERN_WARNING "%s: id %s: %pV\n",
320 RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf);
321 else /* punt */
322 printk(KERN_WARNING "%s: rbd_dev %p: %pV\n",
323 RBD_DRV_NAME, rbd_dev, &vaf);
324 va_end(args);
325}
326
Alex Elderaafb2302012-09-06 16:00:54 -0500327#ifdef RBD_DEBUG
328#define rbd_assert(expr) \
329 if (unlikely(!(expr))) { \
330 printk(KERN_ERR "\nAssertion failure in %s() " \
331 "at line %d:\n\n" \
332 "\trbd_assert(%s);\n\n", \
333 __func__, __LINE__, #expr); \
334 BUG(); \
335 }
336#else /* !RBD_DEBUG */
337# define rbd_assert(expr) ((void) 0)
338#endif /* !RBD_DEBUG */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800339
Alex Elder117973f2012-08-31 17:29:55 -0500340static int rbd_dev_refresh(struct rbd_device *rbd_dev, u64 *hver);
341static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev, u64 *hver);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700342
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700343static int rbd_open(struct block_device *bdev, fmode_t mode)
344{
Alex Elderf0f8cef2012-01-29 13:57:44 -0600345 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700346
Alex Elderf84344f2012-08-31 17:29:51 -0500347 if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700348 return -EROFS;
349
Alex Elder42382b72012-11-16 09:29:16 -0600350 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Alex Elderc3e946c2012-11-16 09:29:16 -0600351 (void) get_device(&rbd_dev->dev);
Alex Elderf84344f2012-08-31 17:29:51 -0500352 set_device_ro(bdev, rbd_dev->mapping.read_only);
Alex Elder42382b72012-11-16 09:29:16 -0600353 rbd_dev->open_count++;
354 mutex_unlock(&ctl_mutex);
Alex Elder340c7a22012-08-10 13:12:07 -0700355
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700356 return 0;
357}
358
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800359static int rbd_release(struct gendisk *disk, fmode_t mode)
360{
361 struct rbd_device *rbd_dev = disk->private_data;
362
Alex Elder42382b72012-11-16 09:29:16 -0600363 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
364 rbd_assert(rbd_dev->open_count > 0);
365 rbd_dev->open_count--;
Alex Elderc3e946c2012-11-16 09:29:16 -0600366 put_device(&rbd_dev->dev);
Alex Elder42382b72012-11-16 09:29:16 -0600367 mutex_unlock(&ctl_mutex);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800368
369 return 0;
370}
371
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700372static const struct block_device_operations rbd_bd_ops = {
373 .owner = THIS_MODULE,
374 .open = rbd_open,
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800375 .release = rbd_release,
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700376};
377
378/*
379 * Initialize an rbd client instance.
Alex Elder43ae4702012-07-03 16:01:18 -0500380 * We own *ceph_opts.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700381 */
Alex Elderf8c38922012-08-10 13:12:07 -0700382static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700383{
384 struct rbd_client *rbdc;
385 int ret = -ENOMEM;
386
387 dout("rbd_client_create\n");
388 rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL);
389 if (!rbdc)
390 goto out_opt;
391
392 kref_init(&rbdc->kref);
393 INIT_LIST_HEAD(&rbdc->node);
394
Alex Elderbc534d82012-01-29 13:57:44 -0600395 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
396
Alex Elder43ae4702012-07-03 16:01:18 -0500397 rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700398 if (IS_ERR(rbdc->client))
Alex Elderbc534d82012-01-29 13:57:44 -0600399 goto out_mutex;
Alex Elder43ae4702012-07-03 16:01:18 -0500400 ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700401
402 ret = ceph_open_session(rbdc->client);
403 if (ret < 0)
404 goto out_err;
405
Alex Elder432b8582012-01-29 13:57:44 -0600406 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700407 list_add_tail(&rbdc->node, &rbd_client_list);
Alex Elder432b8582012-01-29 13:57:44 -0600408 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700409
Alex Elderbc534d82012-01-29 13:57:44 -0600410 mutex_unlock(&ctl_mutex);
411
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700412 dout("rbd_client_create created %p\n", rbdc);
413 return rbdc;
414
415out_err:
416 ceph_destroy_client(rbdc->client);
Alex Elderbc534d82012-01-29 13:57:44 -0600417out_mutex:
418 mutex_unlock(&ctl_mutex);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700419 kfree(rbdc);
420out_opt:
Alex Elder43ae4702012-07-03 16:01:18 -0500421 if (ceph_opts)
422 ceph_destroy_options(ceph_opts);
Vasiliy Kulikov28f259b2010-09-26 12:59:37 +0400423 return ERR_PTR(ret);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700424}
425
426/*
Alex Elder1f7ba332012-08-10 13:12:07 -0700427 * Find a ceph client with specific addr and configuration. If
428 * found, bump its reference count.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700429 */
Alex Elder1f7ba332012-08-10 13:12:07 -0700430static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700431{
432 struct rbd_client *client_node;
Alex Elder1f7ba332012-08-10 13:12:07 -0700433 bool found = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700434
Alex Elder43ae4702012-07-03 16:01:18 -0500435 if (ceph_opts->flags & CEPH_OPT_NOSHARE)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700436 return NULL;
437
Alex Elder1f7ba332012-08-10 13:12:07 -0700438 spin_lock(&rbd_client_list_lock);
439 list_for_each_entry(client_node, &rbd_client_list, node) {
440 if (!ceph_compare_options(ceph_opts, client_node->client)) {
441 kref_get(&client_node->kref);
442 found = true;
443 break;
444 }
445 }
446 spin_unlock(&rbd_client_list_lock);
447
448 return found ? client_node : NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700449}
450
451/*
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700452 * mount options
453 */
454enum {
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700455 Opt_last_int,
456 /* int args above */
457 Opt_last_string,
458 /* string args above */
Alex Eldercc0538b2012-08-10 13:12:07 -0700459 Opt_read_only,
460 Opt_read_write,
461 /* Boolean args above */
462 Opt_last_bool,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700463};
464
Alex Elder43ae4702012-07-03 16:01:18 -0500465static match_table_t rbd_opts_tokens = {
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700466 /* int args above */
467 /* string args above */
Alex Elderbe466c12012-10-22 11:31:26 -0500468 {Opt_read_only, "read_only"},
Alex Eldercc0538b2012-08-10 13:12:07 -0700469 {Opt_read_only, "ro"}, /* Alternate spelling */
470 {Opt_read_write, "read_write"},
471 {Opt_read_write, "rw"}, /* Alternate spelling */
472 /* Boolean args above */
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700473 {-1, NULL}
474};
475
476static int parse_rbd_opts_token(char *c, void *private)
477{
Alex Elder43ae4702012-07-03 16:01:18 -0500478 struct rbd_options *rbd_opts = private;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700479 substring_t argstr[MAX_OPT_ARGS];
480 int token, intval, ret;
481
Alex Elder43ae4702012-07-03 16:01:18 -0500482 token = match_token(c, rbd_opts_tokens, argstr);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700483 if (token < 0)
484 return -EINVAL;
485
486 if (token < Opt_last_int) {
487 ret = match_int(&argstr[0], &intval);
488 if (ret < 0) {
489 pr_err("bad mount option arg (not int) "
490 "at '%s'\n", c);
491 return ret;
492 }
493 dout("got int token %d val %d\n", token, intval);
494 } else if (token > Opt_last_int && token < Opt_last_string) {
495 dout("got string token %d val %s\n", token,
496 argstr[0].from);
Alex Eldercc0538b2012-08-10 13:12:07 -0700497 } else if (token > Opt_last_string && token < Opt_last_bool) {
498 dout("got Boolean token %d\n", token);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700499 } else {
500 dout("got token %d\n", token);
501 }
502
503 switch (token) {
Alex Eldercc0538b2012-08-10 13:12:07 -0700504 case Opt_read_only:
505 rbd_opts->read_only = true;
506 break;
507 case Opt_read_write:
508 rbd_opts->read_only = false;
509 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700510 default:
Alex Elderaafb2302012-09-06 16:00:54 -0500511 rbd_assert(false);
512 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700513 }
514 return 0;
515}
516
517/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700518 * Get a ceph client with specific addr and configuration, if one does
519 * not exist create it.
520 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500521static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700522{
Alex Elderf8c38922012-08-10 13:12:07 -0700523 struct rbd_client *rbdc;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700524
Alex Elder1f7ba332012-08-10 13:12:07 -0700525 rbdc = rbd_client_find(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500526 if (rbdc) /* using an existing client */
Alex Elder43ae4702012-07-03 16:01:18 -0500527 ceph_destroy_options(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500528 else
Alex Elderf8c38922012-08-10 13:12:07 -0700529 rbdc = rbd_client_create(ceph_opts);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700530
Alex Elder9d3997f2012-10-25 23:34:42 -0500531 return rbdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700532}
533
534/*
535 * Destroy ceph client
Alex Elderd23a4b32012-01-29 13:57:43 -0600536 *
Alex Elder432b8582012-01-29 13:57:44 -0600537 * Caller must hold rbd_client_list_lock.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700538 */
539static void rbd_client_release(struct kref *kref)
540{
541 struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
542
543 dout("rbd_release_client %p\n", rbdc);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500544 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700545 list_del(&rbdc->node);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500546 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700547
548 ceph_destroy_client(rbdc->client);
549 kfree(rbdc);
550}
551
552/*
553 * Drop reference to ceph client node. If it's not referenced anymore, release
554 * it.
555 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500556static void rbd_put_client(struct rbd_client *rbdc)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700557{
Alex Elderc53d5892012-10-25 23:34:42 -0500558 if (rbdc)
559 kref_put(&rbdc->kref, rbd_client_release);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700560}
561
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700562/*
563 * Destroy requests collection
564 */
565static void rbd_coll_release(struct kref *kref)
566{
567 struct rbd_req_coll *coll =
568 container_of(kref, struct rbd_req_coll, kref);
569
570 dout("rbd_coll_release %p\n", coll);
571 kfree(coll);
572}
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700573
Alex Eldera30b71b2012-07-10 20:30:11 -0500574static bool rbd_image_format_valid(u32 image_format)
575{
576 return image_format == 1 || image_format == 2;
577}
578
Alex Elder8e94af82012-07-25 09:32:40 -0500579static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
580{
Alex Elder103a1502012-08-02 11:29:45 -0500581 size_t size;
582 u32 snap_count;
583
584 /* The header has to start with the magic rbd header text */
585 if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT)))
586 return false;
587
Alex Elderdb2388b2012-10-20 22:17:27 -0500588 /* The bio layer requires at least sector-sized I/O */
589
590 if (ondisk->options.order < SECTOR_SHIFT)
591 return false;
592
593 /* If we use u64 in a few spots we may be able to loosen this */
594
595 if (ondisk->options.order > 8 * sizeof (int) - 1)
596 return false;
597
Alex Elder103a1502012-08-02 11:29:45 -0500598 /*
599 * The size of a snapshot header has to fit in a size_t, and
600 * that limits the number of snapshots.
601 */
602 snap_count = le32_to_cpu(ondisk->snap_count);
603 size = SIZE_MAX - sizeof (struct ceph_snap_context);
604 if (snap_count > size / sizeof (__le64))
605 return false;
606
607 /*
608 * Not only that, but the size of the entire the snapshot
609 * header must also be representable in a size_t.
610 */
611 size -= snap_count * sizeof (__le64);
612 if ((u64) size < le64_to_cpu(ondisk->snap_names_len))
613 return false;
614
615 return true;
Alex Elder8e94af82012-07-25 09:32:40 -0500616}
617
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700618/*
619 * Create a new header structure, translate header format from the on-disk
620 * header.
621 */
622static int rbd_header_from_disk(struct rbd_image_header *header,
Alex Elder4156d992012-08-02 11:29:46 -0500623 struct rbd_image_header_ondisk *ondisk)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700624{
Alex Elderccece232012-07-10 20:30:10 -0500625 u32 snap_count;
Alex Elder58c17b02012-08-23 23:22:06 -0500626 size_t len;
Alex Elderd2bb24e2012-07-26 23:37:14 -0500627 size_t size;
Alex Elder621901d2012-08-23 23:22:06 -0500628 u32 i;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700629
Alex Elder6a523252012-07-19 17:12:59 -0500630 memset(header, 0, sizeof (*header));
631
Alex Elder103a1502012-08-02 11:29:45 -0500632 snap_count = le32_to_cpu(ondisk->snap_count);
633
Alex Elder58c17b02012-08-23 23:22:06 -0500634 len = strnlen(ondisk->object_prefix, sizeof (ondisk->object_prefix));
635 header->object_prefix = kmalloc(len + 1, GFP_KERNEL);
Alex Elder6a523252012-07-19 17:12:59 -0500636 if (!header->object_prefix)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700637 return -ENOMEM;
Alex Elder58c17b02012-08-23 23:22:06 -0500638 memcpy(header->object_prefix, ondisk->object_prefix, len);
639 header->object_prefix[len] = '\0';
Alex Elder00f1f362012-02-07 12:03:36 -0600640
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700641 if (snap_count) {
Alex Elderf785cc12012-08-23 23:22:06 -0500642 u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len);
643
Alex Elder621901d2012-08-23 23:22:06 -0500644 /* Save a copy of the snapshot names */
645
Alex Elderf785cc12012-08-23 23:22:06 -0500646 if (snap_names_len > (u64) SIZE_MAX)
647 return -EIO;
648 header->snap_names = kmalloc(snap_names_len, GFP_KERNEL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700649 if (!header->snap_names)
Alex Elder6a523252012-07-19 17:12:59 -0500650 goto out_err;
Alex Elderf785cc12012-08-23 23:22:06 -0500651 /*
652 * Note that rbd_dev_v1_header_read() guarantees
653 * the ondisk buffer we're working with has
654 * snap_names_len bytes beyond the end of the
655 * snapshot id array, this memcpy() is safe.
656 */
657 memcpy(header->snap_names, &ondisk->snaps[snap_count],
658 snap_names_len);
Alex Elder6a523252012-07-19 17:12:59 -0500659
Alex Elder621901d2012-08-23 23:22:06 -0500660 /* Record each snapshot's size */
661
Alex Elderd2bb24e2012-07-26 23:37:14 -0500662 size = snap_count * sizeof (*header->snap_sizes);
663 header->snap_sizes = kmalloc(size, GFP_KERNEL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700664 if (!header->snap_sizes)
Alex Elder6a523252012-07-19 17:12:59 -0500665 goto out_err;
Alex Elder621901d2012-08-23 23:22:06 -0500666 for (i = 0; i < snap_count; i++)
667 header->snap_sizes[i] =
668 le64_to_cpu(ondisk->snaps[i].image_size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700669 } else {
Alex Elderccece232012-07-10 20:30:10 -0500670 WARN_ON(ondisk->snap_names_len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700671 header->snap_names = NULL;
672 header->snap_sizes = NULL;
673 }
Alex Elder849b4262012-07-09 21:04:24 -0500674
Alex Elder34b13182012-07-13 20:35:12 -0500675 header->features = 0; /* No features support in v1 images */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700676 header->obj_order = ondisk->options.order;
677 header->crypt_type = ondisk->options.crypt_type;
678 header->comp_type = ondisk->options.comp_type;
Alex Elder6a523252012-07-19 17:12:59 -0500679
Alex Elder621901d2012-08-23 23:22:06 -0500680 /* Allocate and fill in the snapshot context */
681
Alex Elderf84344f2012-08-31 17:29:51 -0500682 header->image_size = le64_to_cpu(ondisk->image_size);
Alex Elder6a523252012-07-19 17:12:59 -0500683 size = sizeof (struct ceph_snap_context);
684 size += snap_count * sizeof (header->snapc->snaps[0]);
685 header->snapc = kzalloc(size, GFP_KERNEL);
686 if (!header->snapc)
687 goto out_err;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700688
689 atomic_set(&header->snapc->nref, 1);
Alex Elder505cbb92012-07-19 08:49:18 -0500690 header->snapc->seq = le64_to_cpu(ondisk->snap_seq);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700691 header->snapc->num_snaps = snap_count;
Alex Elder621901d2012-08-23 23:22:06 -0500692 for (i = 0; i < snap_count; i++)
693 header->snapc->snaps[i] =
694 le64_to_cpu(ondisk->snaps[i].id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700695
696 return 0;
697
Alex Elder6a523252012-07-19 17:12:59 -0500698out_err:
Alex Elder849b4262012-07-09 21:04:24 -0500699 kfree(header->snap_sizes);
Alex Elderccece232012-07-10 20:30:10 -0500700 header->snap_sizes = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700701 kfree(header->snap_names);
Alex Elderccece232012-07-10 20:30:10 -0500702 header->snap_names = NULL;
Alex Elder6a523252012-07-19 17:12:59 -0500703 kfree(header->object_prefix);
704 header->object_prefix = NULL;
Alex Elderccece232012-07-10 20:30:10 -0500705
Alex Elder00f1f362012-02-07 12:03:36 -0600706 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700707}
708
Alex Elder9e15b772012-10-30 19:40:33 -0500709static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id)
710{
711 struct rbd_snap *snap;
712
713 if (snap_id == CEPH_NOSNAP)
714 return RBD_SNAP_HEAD_NAME;
715
716 list_for_each_entry(snap, &rbd_dev->snaps, node)
717 if (snap_id == snap->id)
718 return snap->name;
719
720 return NULL;
721}
722
Alex Elder8836b992012-08-30 14:42:15 -0500723static int snap_by_name(struct rbd_device *rbd_dev, const char *snap_name)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700724{
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700725
Alex Eldere86924a2012-07-10 20:30:11 -0500726 struct rbd_snap *snap;
Alex Elder00f1f362012-02-07 12:03:36 -0600727
Alex Eldere86924a2012-07-10 20:30:11 -0500728 list_for_each_entry(snap, &rbd_dev->snaps, node) {
729 if (!strcmp(snap_name, snap->name)) {
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500730 rbd_dev->spec->snap_id = snap->id;
Alex Eldere86924a2012-07-10 20:30:11 -0500731 rbd_dev->mapping.size = snap->size;
Alex Elder34b13182012-07-13 20:35:12 -0500732 rbd_dev->mapping.features = snap->features;
Alex Elder00f1f362012-02-07 12:03:36 -0600733
Alex Eldere86924a2012-07-10 20:30:11 -0500734 return 0;
Alex Elder00f1f362012-02-07 12:03:36 -0600735 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700736 }
Alex Eldere86924a2012-07-10 20:30:11 -0500737
Alex Elder00f1f362012-02-07 12:03:36 -0600738 return -ENOENT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700739}
740
Alex Elder819d52b2012-10-25 23:34:41 -0500741static int rbd_dev_set_mapping(struct rbd_device *rbd_dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700742{
Alex Elder78dc4472012-07-19 08:49:18 -0500743 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700744
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500745 if (!memcmp(rbd_dev->spec->snap_name, RBD_SNAP_HEAD_NAME,
Josh Durgincc9d7342011-11-21 18:19:13 -0800746 sizeof (RBD_SNAP_HEAD_NAME))) {
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500747 rbd_dev->spec->snap_id = CEPH_NOSNAP;
Alex Elder99c1f082012-08-30 14:42:15 -0500748 rbd_dev->mapping.size = rbd_dev->header.image_size;
Alex Elder34b13182012-07-13 20:35:12 -0500749 rbd_dev->mapping.features = rbd_dev->header.features;
Alex Eldere86924a2012-07-10 20:30:11 -0500750 ret = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700751 } else {
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500752 ret = snap_by_name(rbd_dev, rbd_dev->spec->snap_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700753 if (ret < 0)
754 goto done;
Alex Elderf84344f2012-08-31 17:29:51 -0500755 rbd_dev->mapping.read_only = true;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700756 }
Alex Elderd78b6502012-11-09 08:43:15 -0600757 atomic_set(&rbd_dev->exists, 1);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700758done:
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700759 return ret;
760}
761
762static void rbd_header_free(struct rbd_image_header *header)
763{
Alex Elder849b4262012-07-09 21:04:24 -0500764 kfree(header->object_prefix);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500765 header->object_prefix = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700766 kfree(header->snap_sizes);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500767 header->snap_sizes = NULL;
Alex Elder849b4262012-07-09 21:04:24 -0500768 kfree(header->snap_names);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500769 header->snap_names = NULL;
Josh Durgind1d25642011-12-05 14:03:05 -0800770 ceph_put_snap_context(header->snapc);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500771 header->snapc = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700772}
773
Alex Elder65ccfe22012-08-09 10:33:26 -0700774static char *rbd_segment_name(struct rbd_device *rbd_dev, u64 offset)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700775{
Alex Elder65ccfe22012-08-09 10:33:26 -0700776 char *name;
777 u64 segment;
778 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700779
Alex Elder2fd82b92012-11-09 15:05:54 -0600780 name = kmalloc(MAX_OBJ_NAME_SIZE + 1, GFP_NOIO);
Alex Elder65ccfe22012-08-09 10:33:26 -0700781 if (!name)
782 return NULL;
783 segment = offset >> rbd_dev->header.obj_order;
Alex Elder2fd82b92012-11-09 15:05:54 -0600784 ret = snprintf(name, MAX_OBJ_NAME_SIZE + 1, "%s.%012llx",
Alex Elder65ccfe22012-08-09 10:33:26 -0700785 rbd_dev->header.object_prefix, segment);
Alex Elder2fd82b92012-11-09 15:05:54 -0600786 if (ret < 0 || ret > MAX_OBJ_NAME_SIZE) {
Alex Elder65ccfe22012-08-09 10:33:26 -0700787 pr_err("error formatting segment name for #%llu (%d)\n",
788 segment, ret);
789 kfree(name);
790 name = NULL;
791 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700792
Alex Elder65ccfe22012-08-09 10:33:26 -0700793 return name;
794}
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700795
Alex Elder65ccfe22012-08-09 10:33:26 -0700796static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset)
797{
798 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700799
Alex Elder65ccfe22012-08-09 10:33:26 -0700800 return offset & (segment_size - 1);
801}
802
803static u64 rbd_segment_length(struct rbd_device *rbd_dev,
804 u64 offset, u64 length)
805{
806 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
807
808 offset &= segment_size - 1;
809
Alex Elderaafb2302012-09-06 16:00:54 -0500810 rbd_assert(length <= U64_MAX - offset);
Alex Elder65ccfe22012-08-09 10:33:26 -0700811 if (offset + length > segment_size)
812 length = segment_size - offset;
813
814 return length;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700815}
816
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700817static int rbd_get_num_segments(struct rbd_image_header *header,
818 u64 ofs, u64 len)
819{
Alex Elderdf111be2012-08-09 10:33:26 -0700820 u64 start_seg;
821 u64 end_seg;
822
823 if (!len)
824 return 0;
825 if (len - 1 > U64_MAX - ofs)
826 return -ERANGE;
827
828 start_seg = ofs >> header->obj_order;
829 end_seg = (ofs + len - 1) >> header->obj_order;
830
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700831 return end_seg - start_seg + 1;
832}
833
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700834/*
Josh Durgin029bcbd2011-07-22 11:35:23 -0700835 * returns the size of an object in the image
836 */
837static u64 rbd_obj_bytes(struct rbd_image_header *header)
838{
839 return 1 << header->obj_order;
840}
841
842/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700843 * bio helpers
844 */
845
846static void bio_chain_put(struct bio *chain)
847{
848 struct bio *tmp;
849
850 while (chain) {
851 tmp = chain;
852 chain = chain->bi_next;
853 bio_put(tmp);
854 }
855}
856
857/*
858 * zeros a bio chain, starting at specific offset
859 */
860static void zero_bio_chain(struct bio *chain, int start_ofs)
861{
862 struct bio_vec *bv;
863 unsigned long flags;
864 void *buf;
865 int i;
866 int pos = 0;
867
868 while (chain) {
869 bio_for_each_segment(bv, chain, i) {
870 if (pos + bv->bv_len > start_ofs) {
871 int remainder = max(start_ofs - pos, 0);
872 buf = bvec_kmap_irq(bv, &flags);
873 memset(buf + remainder, 0,
874 bv->bv_len - remainder);
Dan Carpenter85b5aaa2010-10-11 21:15:11 +0200875 bvec_kunmap_irq(buf, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700876 }
877 pos += bv->bv_len;
878 }
879
880 chain = chain->bi_next;
881 }
882}
883
884/*
Alex Elderf7760da2012-10-20 22:17:27 -0500885 * Clone a portion of a bio, starting at the given byte offset
886 * and continuing for the number of bytes indicated.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700887 */
Alex Elderf7760da2012-10-20 22:17:27 -0500888static struct bio *bio_clone_range(struct bio *bio_src,
889 unsigned int offset,
890 unsigned int len,
891 gfp_t gfpmask)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700892{
Alex Elderf7760da2012-10-20 22:17:27 -0500893 struct bio_vec *bv;
894 unsigned int resid;
895 unsigned short idx;
896 unsigned int voff;
897 unsigned short end_idx;
898 unsigned short vcnt;
899 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700900
Alex Elderf7760da2012-10-20 22:17:27 -0500901 /* Handle the easy case for the caller */
902
903 if (!offset && len == bio_src->bi_size)
904 return bio_clone(bio_src, gfpmask);
905
906 if (WARN_ON_ONCE(!len))
907 return NULL;
908 if (WARN_ON_ONCE(len > bio_src->bi_size))
909 return NULL;
910 if (WARN_ON_ONCE(offset > bio_src->bi_size - len))
911 return NULL;
912
913 /* Find first affected segment... */
914
915 resid = offset;
916 __bio_for_each_segment(bv, bio_src, idx, 0) {
917 if (resid < bv->bv_len)
918 break;
919 resid -= bv->bv_len;
920 }
921 voff = resid;
922
923 /* ...and the last affected segment */
924
925 resid += len;
926 __bio_for_each_segment(bv, bio_src, end_idx, idx) {
927 if (resid <= bv->bv_len)
928 break;
929 resid -= bv->bv_len;
930 }
931 vcnt = end_idx - idx + 1;
932
933 /* Build the clone */
934
935 bio = bio_alloc(gfpmask, (unsigned int) vcnt);
936 if (!bio)
937 return NULL; /* ENOMEM */
938
939 bio->bi_bdev = bio_src->bi_bdev;
940 bio->bi_sector = bio_src->bi_sector + (offset >> SECTOR_SHIFT);
941 bio->bi_rw = bio_src->bi_rw;
942 bio->bi_flags |= 1 << BIO_CLONED;
943
944 /*
945 * Copy over our part of the bio_vec, then update the first
946 * and last (or only) entries.
947 */
948 memcpy(&bio->bi_io_vec[0], &bio_src->bi_io_vec[idx],
949 vcnt * sizeof (struct bio_vec));
950 bio->bi_io_vec[0].bv_offset += voff;
951 if (vcnt > 1) {
952 bio->bi_io_vec[0].bv_len -= voff;
953 bio->bi_io_vec[vcnt - 1].bv_len = resid;
954 } else {
955 bio->bi_io_vec[0].bv_len = len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700956 }
957
Alex Elderf7760da2012-10-20 22:17:27 -0500958 bio->bi_vcnt = vcnt;
959 bio->bi_size = len;
960 bio->bi_idx = 0;
Alex Elder542582f2012-08-09 10:33:25 -0700961
Alex Elderf7760da2012-10-20 22:17:27 -0500962 return bio;
963}
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700964
Alex Elderf7760da2012-10-20 22:17:27 -0500965/*
966 * Clone a portion of a bio chain, starting at the given byte offset
967 * into the first bio in the source chain and continuing for the
968 * number of bytes indicated. The result is another bio chain of
969 * exactly the given length, or a null pointer on error.
970 *
971 * The bio_src and offset parameters are both in-out. On entry they
972 * refer to the first source bio and the offset into that bio where
973 * the start of data to be cloned is located.
974 *
975 * On return, bio_src is updated to refer to the bio in the source
976 * chain that contains first un-cloned byte, and *offset will
977 * contain the offset of that byte within that bio.
978 */
979static struct bio *bio_chain_clone_range(struct bio **bio_src,
980 unsigned int *offset,
981 unsigned int len,
982 gfp_t gfpmask)
983{
984 struct bio *bi = *bio_src;
985 unsigned int off = *offset;
986 struct bio *chain = NULL;
987 struct bio **end;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700988
Alex Elderf7760da2012-10-20 22:17:27 -0500989 /* Build up a chain of clone bios up to the limit */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700990
Alex Elderf7760da2012-10-20 22:17:27 -0500991 if (!bi || off >= bi->bi_size || !len)
992 return NULL; /* Nothing to clone */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700993
Alex Elderf7760da2012-10-20 22:17:27 -0500994 end = &chain;
995 while (len) {
996 unsigned int bi_size;
997 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700998
Alex Elderf5400b72012-11-01 10:17:15 -0500999 if (!bi) {
1000 rbd_warn(NULL, "bio_chain exhausted with %u left", len);
Alex Elderf7760da2012-10-20 22:17:27 -05001001 goto out_err; /* EINVAL; ran out of bio's */
Alex Elderf5400b72012-11-01 10:17:15 -05001002 }
Alex Elderf7760da2012-10-20 22:17:27 -05001003 bi_size = min_t(unsigned int, bi->bi_size - off, len);
1004 bio = bio_clone_range(bi, off, bi_size, gfpmask);
1005 if (!bio)
1006 goto out_err; /* ENOMEM */
1007
1008 *end = bio;
1009 end = &bio->bi_next;
1010
1011 off += bi_size;
1012 if (off == bi->bi_size) {
1013 bi = bi->bi_next;
1014 off = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001015 }
Alex Elderf7760da2012-10-20 22:17:27 -05001016 len -= bi_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001017 }
Alex Elderf7760da2012-10-20 22:17:27 -05001018 *bio_src = bi;
1019 *offset = off;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001020
Alex Elderf7760da2012-10-20 22:17:27 -05001021 return chain;
1022out_err:
1023 bio_chain_put(chain);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001024
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001025 return NULL;
1026}
1027
Alex Elder139b4312012-11-13 21:11:15 -06001028static struct ceph_osd_req_op *rbd_create_rw_op(int opcode, u32 payload_len)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001029{
Alex Elder139b4312012-11-13 21:11:15 -06001030 struct ceph_osd_req_op *op;
Alex Elder57cfc102012-06-26 12:57:03 -07001031
Alex Elder139b4312012-11-13 21:11:15 -06001032 op = kzalloc(sizeof (*op), GFP_NOIO);
1033 if (!op)
Alex Elder57cfc102012-06-26 12:57:03 -07001034 return NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001035 /*
1036 * op extent offset and length will be set later on
Alex Eldere01e7922012-11-14 12:25:18 -06001037 * after ceph_calc_file_object_mapping().
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001038 */
Alex Elder139b4312012-11-13 21:11:15 -06001039 op->op = opcode;
1040 op->payload_len = payload_len;
Alex Elder57cfc102012-06-26 12:57:03 -07001041
Alex Elder139b4312012-11-13 21:11:15 -06001042 return op;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001043}
1044
Alex Elder139b4312012-11-13 21:11:15 -06001045static void rbd_destroy_op(struct ceph_osd_req_op *op)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001046{
Alex Elder139b4312012-11-13 21:11:15 -06001047 kfree(op);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001048}
1049
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001050static void rbd_coll_end_req_index(struct request *rq,
1051 struct rbd_req_coll *coll,
1052 int index,
Alex Elder8986cb32012-11-08 08:01:39 -06001053 s32 ret, u64 len)
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001054{
1055 struct request_queue *q;
1056 int min, max, i;
1057
Alex Elderbd919d42012-07-13 20:35:11 -05001058 dout("rbd_coll_end_req_index %p index %d ret %d len %llu\n",
Alex Elder8986cb32012-11-08 08:01:39 -06001059 coll, index, (int)ret, (unsigned long long)len);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001060
1061 if (!rq)
1062 return;
1063
1064 if (!coll) {
1065 blk_end_request(rq, ret, len);
1066 return;
1067 }
1068
1069 q = rq->q;
1070
1071 spin_lock_irq(q->queue_lock);
1072 coll->status[index].done = 1;
1073 coll->status[index].rc = ret;
1074 coll->status[index].bytes = len;
1075 max = min = coll->num_done;
1076 while (max < coll->total && coll->status[max].done)
1077 max++;
1078
1079 for (i = min; i<max; i++) {
Alex Elder8986cb32012-11-08 08:01:39 -06001080 __blk_end_request(rq, (int)coll->status[i].rc,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001081 coll->status[i].bytes);
1082 coll->num_done++;
1083 kref_put(&coll->kref, rbd_coll_release);
1084 }
1085 spin_unlock_irq(q->queue_lock);
1086}
1087
Alex Elder725afc92012-11-08 08:01:39 -06001088static void rbd_coll_end_req(struct rbd_request *rbd_req,
Alex Elder8986cb32012-11-08 08:01:39 -06001089 s32 ret, u64 len)
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001090{
Alex Elder725afc92012-11-08 08:01:39 -06001091 rbd_coll_end_req_index(rbd_req->rq,
1092 rbd_req->coll, rbd_req->coll_index,
1093 ret, len);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001094}
1095
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001096/*
1097 * Send ceph osd request
1098 */
1099static int rbd_do_request(struct request *rq,
Alex Elder0ce1a792012-07-03 16:01:18 -05001100 struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001101 struct ceph_snap_context *snapc,
1102 u64 snapid,
Alex Elderaded07e2012-07-03 16:01:18 -05001103 const char *object_name, u64 ofs, u64 len,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001104 struct bio *bio,
1105 struct page **pages,
1106 int num_pages,
1107 int flags,
Alex Elder30573d62012-11-13 21:11:15 -06001108 struct ceph_osd_req_op *op,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001109 struct rbd_req_coll *coll,
1110 int coll_index,
Alex Elder5f29ddd2012-11-08 08:01:39 -06001111 void (*rbd_cb)(struct ceph_osd_request *,
1112 struct ceph_msg *),
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001113 struct ceph_osd_request **linger_req,
1114 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001115{
Alex Elder5f29ddd2012-11-08 08:01:39 -06001116 struct ceph_osd_request *osd_req;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001117 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001118 struct timespec mtime = CURRENT_TIME;
Alex Elder725afc92012-11-08 08:01:39 -06001119 struct rbd_request *rbd_req;
Alex Elder1dbb4392012-01-24 10:08:37 -06001120 struct ceph_osd_client *osdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001121
Alex Elder725afc92012-11-08 08:01:39 -06001122 rbd_req = kzalloc(sizeof(*rbd_req), GFP_NOIO);
Alex Eldercd323ac2012-11-08 08:01:39 -06001123 if (!rbd_req)
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001124 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001125
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001126 if (coll) {
Alex Elder725afc92012-11-08 08:01:39 -06001127 rbd_req->coll = coll;
1128 rbd_req->coll_index = coll_index;
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001129 }
1130
Alex Elderf7760da2012-10-20 22:17:27 -05001131 dout("rbd_do_request object_name=%s ofs=%llu len=%llu coll=%p[%d]\n",
1132 object_name, (unsigned long long) ofs,
1133 (unsigned long long) len, coll, coll_index);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001134
Alex Elder0ce1a792012-07-03 16:01:18 -05001135 osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder30573d62012-11-13 21:11:15 -06001136 osd_req = ceph_osdc_alloc_request(osdc, snapc, 1, false, GFP_NOIO);
Alex Elder5f29ddd2012-11-08 08:01:39 -06001137 if (!osd_req) {
Sage Weil4ad12622011-05-03 09:23:36 -07001138 ret = -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001139 goto done_pages;
1140 }
1141
Alex Elderd178a9e2012-11-13 21:11:15 -06001142 osd_req->r_flags = flags;
Alex Elder54a54002012-11-13 21:11:15 -06001143 osd_req->r_pages = pages;
1144 if (bio) {
1145 osd_req->r_bio = bio;
1146 bio_get(osd_req->r_bio);
1147 }
Alex Elder5f29ddd2012-11-08 08:01:39 -06001148 osd_req->r_callback = rbd_cb;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001149
Alex Elder725afc92012-11-08 08:01:39 -06001150 rbd_req->rq = rq;
1151 rbd_req->bio = bio;
1152 rbd_req->pages = pages;
1153 rbd_req->len = len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001154
Alex Elder5f29ddd2012-11-08 08:01:39 -06001155 osd_req->r_priv = rbd_req;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001156
Alex Elder5f29ddd2012-11-08 08:01:39 -06001157 strncpy(osd_req->r_oid, object_name, sizeof(osd_req->r_oid));
1158 osd_req->r_oid_len = strlen(osd_req->r_oid);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001159
Alex Elder0903e872012-11-14 12:25:19 -06001160 osd_req->r_file_layout = rbd_dev->layout; /* struct */
Alex Elder47dba7b2012-11-14 12:25:19 -06001161
Alex Eldere01e7922012-11-14 12:25:18 -06001162 if (op->op == CEPH_OSD_OP_READ || op->op == CEPH_OSD_OP_WRITE) {
Alex Elder47dba7b2012-11-14 12:25:19 -06001163 op->extent.offset = ofs;
1164 op->extent.length = len;
Alex Eldere01e7922012-11-14 12:25:18 -06001165 if (op->op == CEPH_OSD_OP_WRITE)
Alex Elder47dba7b2012-11-14 12:25:19 -06001166 op->payload_len = len;
Alex Eldere01e7922012-11-14 12:25:18 -06001167 }
1168 osd_req->r_num_pages = calc_pages_for(ofs, len);
1169 osd_req->r_page_alignment = ofs & ~PAGE_MASK;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001170
Alex Elder30573d62012-11-13 21:11:15 -06001171 ceph_osdc_build_request(osd_req, ofs, len, 1, op,
Alex Elderae7ca4a32012-11-13 21:11:15 -06001172 snapc, snapid, &mtime);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001173
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001174 if (linger_req) {
Alex Elder5f29ddd2012-11-08 08:01:39 -06001175 ceph_osdc_set_request_linger(osdc, osd_req);
1176 *linger_req = osd_req;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001177 }
1178
Alex Elder5f29ddd2012-11-08 08:01:39 -06001179 ret = ceph_osdc_start_request(osdc, osd_req, false);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001180 if (ret < 0)
1181 goto done_err;
1182
1183 if (!rbd_cb) {
Alex Elder5f29ddd2012-11-08 08:01:39 -06001184 u64 version;
1185
1186 ret = ceph_osdc_wait_request(osdc, osd_req);
1187 version = le64_to_cpu(osd_req->r_reassert_version.version);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001188 if (ver)
Alex Elder5f29ddd2012-11-08 08:01:39 -06001189 *ver = version;
1190 dout("reassert_ver=%llu\n", (unsigned long long) version);
1191 ceph_osdc_put_request(osd_req);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001192 }
1193 return ret;
1194
1195done_err:
Alex Elder725afc92012-11-08 08:01:39 -06001196 bio_chain_put(rbd_req->bio);
Alex Elder5f29ddd2012-11-08 08:01:39 -06001197 ceph_osdc_put_request(osd_req);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001198done_pages:
Alex Elder725afc92012-11-08 08:01:39 -06001199 kfree(rbd_req);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001200 return ret;
1201}
1202
1203/*
1204 * Ceph osd op callback
1205 */
Alex Elder5f29ddd2012-11-08 08:01:39 -06001206static void rbd_req_cb(struct ceph_osd_request *osd_req, struct ceph_msg *msg)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001207{
Alex Elder5f29ddd2012-11-08 08:01:39 -06001208 struct rbd_request *rbd_req = osd_req->r_priv;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001209 struct ceph_osd_reply_head *replyhead;
1210 struct ceph_osd_op *op;
Alex Elder8986cb32012-11-08 08:01:39 -06001211 s32 rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001212 u64 bytes;
1213 int read_op;
1214
1215 /* parse reply */
1216 replyhead = msg->front.iov_base;
1217 WARN_ON(le32_to_cpu(replyhead->num_ops) == 0);
1218 op = (void *)(replyhead + 1);
Alex Elder8986cb32012-11-08 08:01:39 -06001219 rc = (s32)le32_to_cpu(replyhead->result);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001220 bytes = le64_to_cpu(op->extent.length);
Dan Carpenter895cfcc2012-06-06 09:15:33 -05001221 read_op = (le16_to_cpu(op->op) == CEPH_OSD_OP_READ);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001222
Alex Elderbd919d42012-07-13 20:35:11 -05001223 dout("rbd_req_cb bytes=%llu readop=%d rc=%d\n",
1224 (unsigned long long) bytes, read_op, (int) rc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001225
Alex Elder8986cb32012-11-08 08:01:39 -06001226 if (rc == (s32)-ENOENT && read_op) {
Alex Elder725afc92012-11-08 08:01:39 -06001227 zero_bio_chain(rbd_req->bio, 0);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001228 rc = 0;
Alex Elder725afc92012-11-08 08:01:39 -06001229 } else if (rc == 0 && read_op && bytes < rbd_req->len) {
1230 zero_bio_chain(rbd_req->bio, bytes);
1231 bytes = rbd_req->len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001232 }
1233
Alex Elder725afc92012-11-08 08:01:39 -06001234 rbd_coll_end_req(rbd_req, rc, bytes);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001235
Alex Elder725afc92012-11-08 08:01:39 -06001236 if (rbd_req->bio)
1237 bio_chain_put(rbd_req->bio);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001238
Alex Elder5f29ddd2012-11-08 08:01:39 -06001239 ceph_osdc_put_request(osd_req);
Alex Elder725afc92012-11-08 08:01:39 -06001240 kfree(rbd_req);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001241}
1242
Alex Elder5f29ddd2012-11-08 08:01:39 -06001243static void rbd_simple_req_cb(struct ceph_osd_request *osd_req,
1244 struct ceph_msg *msg)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001245{
Alex Elder5f29ddd2012-11-08 08:01:39 -06001246 ceph_osdc_put_request(osd_req);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001247}
1248
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001249/*
1250 * Do a synchronous ceph osd operation
1251 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001252static int rbd_req_sync_op(struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001253 int flags,
Alex Elder30573d62012-11-13 21:11:15 -06001254 struct ceph_osd_req_op *op,
Alex Elderaded07e2012-07-03 16:01:18 -05001255 const char *object_name,
Alex Elderf8d4de62012-07-03 16:01:19 -05001256 u64 ofs, u64 inbound_size,
1257 char *inbound,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001258 struct ceph_osd_request **linger_req,
1259 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001260{
1261 int ret;
1262 struct page **pages;
1263 int num_pages;
Alex Elder913d2fd2012-06-26 12:57:03 -07001264
Alex Elder30573d62012-11-13 21:11:15 -06001265 rbd_assert(op != NULL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001266
Alex Elderf8d4de62012-07-03 16:01:19 -05001267 num_pages = calc_pages_for(ofs, inbound_size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001268 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
Dan Carpenterb8d06382010-10-11 21:14:23 +02001269 if (IS_ERR(pages))
1270 return PTR_ERR(pages);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001271
Alex Elder25704ac2012-11-09 08:43:16 -06001272 ret = rbd_do_request(NULL, rbd_dev, NULL, CEPH_NOSNAP,
Alex Elderf8d4de62012-07-03 16:01:19 -05001273 object_name, ofs, inbound_size, NULL,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001274 pages, num_pages,
1275 flags,
Alex Elder30573d62012-11-13 21:11:15 -06001276 op,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001277 NULL, 0,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001278 NULL,
1279 linger_req, ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001280 if (ret < 0)
Alex Elder913d2fd2012-06-26 12:57:03 -07001281 goto done;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001282
Alex Elderf8d4de62012-07-03 16:01:19 -05001283 if ((flags & CEPH_OSD_FLAG_READ) && inbound)
1284 ret = ceph_copy_from_page_vector(pages, inbound, ofs, ret);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001285
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001286done:
1287 ceph_release_page_vector(pages, num_pages);
1288 return ret;
1289}
1290
1291/*
1292 * Do an asynchronous ceph osd operation
1293 */
1294static int rbd_do_op(struct request *rq,
Alex Elder0ce1a792012-07-03 16:01:18 -05001295 struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001296 struct ceph_snap_context *snapc,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001297 u64 ofs, u64 len,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001298 struct bio *bio,
1299 struct rbd_req_coll *coll,
1300 int coll_index)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001301{
1302 char *seg_name;
1303 u64 seg_ofs;
1304 u64 seg_len;
1305 int ret;
Alex Elder139b4312012-11-13 21:11:15 -06001306 struct ceph_osd_req_op *op;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001307 u32 payload_len;
Alex Elderff2e4bb2012-10-10 18:59:29 -07001308 int opcode;
1309 int flags;
Alex Elder46342462012-10-10 18:59:29 -07001310 u64 snapid;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001311
Alex Elder65ccfe22012-08-09 10:33:26 -07001312 seg_name = rbd_segment_name(rbd_dev, ofs);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001313 if (!seg_name)
1314 return -ENOMEM;
Alex Elder65ccfe22012-08-09 10:33:26 -07001315 seg_len = rbd_segment_length(rbd_dev, ofs, len);
1316 seg_ofs = rbd_segment_offset(rbd_dev, ofs);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001317
Alex Elderff2e4bb2012-10-10 18:59:29 -07001318 if (rq_data_dir(rq) == WRITE) {
1319 opcode = CEPH_OSD_OP_WRITE;
1320 flags = CEPH_OSD_FLAG_WRITE|CEPH_OSD_FLAG_ONDISK;
Alex Elder46342462012-10-10 18:59:29 -07001321 snapid = CEPH_NOSNAP;
Alex Elderff2e4bb2012-10-10 18:59:29 -07001322 payload_len = seg_len;
1323 } else {
1324 opcode = CEPH_OSD_OP_READ;
1325 flags = CEPH_OSD_FLAG_READ;
Alex Eldera7b4c652012-11-09 08:43:15 -06001326 rbd_assert(!snapc);
Alex Elder0d7dbfc2012-10-25 23:34:41 -05001327 snapid = rbd_dev->spec->snap_id;
Alex Elderff2e4bb2012-10-10 18:59:29 -07001328 payload_len = 0;
1329 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001330
Alex Elder57cfc102012-06-26 12:57:03 -07001331 ret = -ENOMEM;
Alex Elder139b4312012-11-13 21:11:15 -06001332 op = rbd_create_rw_op(opcode, payload_len);
1333 if (!op)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001334 goto done;
1335
1336 /* we've taken care of segment sizes earlier when we
1337 cloned the bios. We should never have a segment
1338 truncated at this point */
Alex Elderaafb2302012-09-06 16:00:54 -05001339 rbd_assert(seg_len == len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001340
1341 ret = rbd_do_request(rq, rbd_dev, snapc, snapid,
1342 seg_name, seg_ofs, seg_len,
1343 bio,
1344 NULL, 0,
1345 flags,
Alex Elder30573d62012-11-13 21:11:15 -06001346 op,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001347 coll, coll_index,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001348 rbd_req_cb, 0, NULL);
Alex Eldercd323ac2012-11-08 08:01:39 -06001349 if (ret < 0)
1350 rbd_coll_end_req_index(rq, coll, coll_index,
1351 (s32)ret, seg_len);
Alex Elder139b4312012-11-13 21:11:15 -06001352 rbd_destroy_op(op);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001353done:
1354 kfree(seg_name);
1355 return ret;
1356}
1357
1358/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001359 * Request sync osd read
1360 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001361static int rbd_req_sync_read(struct rbd_device *rbd_dev,
Alex Elderaded07e2012-07-03 16:01:18 -05001362 const char *object_name,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001363 u64 ofs, u64 len,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001364 char *buf,
1365 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001366{
Alex Elder139b4312012-11-13 21:11:15 -06001367 struct ceph_osd_req_op *op;
Alex Elder913d2fd2012-06-26 12:57:03 -07001368 int ret;
1369
Alex Elder139b4312012-11-13 21:11:15 -06001370 op = rbd_create_rw_op(CEPH_OSD_OP_READ, 0);
1371 if (!op)
Alex Elder913d2fd2012-06-26 12:57:03 -07001372 return -ENOMEM;
1373
Alex Elder25704ac2012-11-09 08:43:16 -06001374 ret = rbd_req_sync_op(rbd_dev, CEPH_OSD_FLAG_READ,
Alex Elder30573d62012-11-13 21:11:15 -06001375 op, object_name, ofs, len, buf, NULL, ver);
Alex Elder139b4312012-11-13 21:11:15 -06001376 rbd_destroy_op(op);
Alex Elder913d2fd2012-06-26 12:57:03 -07001377
1378 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001379}
1380
1381/*
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001382 * Request sync osd watch
1383 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001384static int rbd_req_sync_notify_ack(struct rbd_device *rbd_dev,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001385 u64 ver,
Alex Elder7f0a24d2012-07-25 09:32:40 -05001386 u64 notify_id)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001387{
Alex Elder139b4312012-11-13 21:11:15 -06001388 struct ceph_osd_req_op *op;
Sage Weil11f77002011-05-12 16:13:54 -07001389 int ret;
1390
Alex Elder139b4312012-11-13 21:11:15 -06001391 op = rbd_create_rw_op(CEPH_OSD_OP_NOTIFY_ACK, 0);
1392 if (!op)
Alex Elder57cfc102012-06-26 12:57:03 -07001393 return -ENOMEM;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001394
Alex Elder139b4312012-11-13 21:11:15 -06001395 op->watch.ver = cpu_to_le64(ver);
1396 op->watch.cookie = notify_id;
1397 op->watch.flag = 0;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001398
Alex Elder0ce1a792012-07-03 16:01:18 -05001399 ret = rbd_do_request(NULL, rbd_dev, NULL, CEPH_NOSNAP,
Alex Elder7f0a24d2012-07-25 09:32:40 -05001400 rbd_dev->header_name, 0, 0, NULL,
Alex Elderad4f2322012-07-03 16:01:19 -05001401 NULL, 0,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001402 CEPH_OSD_FLAG_READ,
Alex Elder30573d62012-11-13 21:11:15 -06001403 op,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001404 NULL, 0,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001405 rbd_simple_req_cb, 0, NULL);
1406
Alex Elder139b4312012-11-13 21:11:15 -06001407 rbd_destroy_op(op);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001408 return ret;
1409}
1410
1411static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
1412{
Alex Elder0ce1a792012-07-03 16:01:18 -05001413 struct rbd_device *rbd_dev = (struct rbd_device *)data;
Josh Durgina71b8912011-12-05 18:10:44 -08001414 u64 hver;
Sage Weil13143d22011-05-12 16:08:30 -07001415 int rc;
1416
Alex Elder0ce1a792012-07-03 16:01:18 -05001417 if (!rbd_dev)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001418 return;
1419
Alex Elderbd919d42012-07-13 20:35:11 -05001420 dout("rbd_watch_cb %s notify_id=%llu opcode=%u\n",
1421 rbd_dev->header_name, (unsigned long long) notify_id,
1422 (unsigned int) opcode);
Alex Elder117973f2012-08-31 17:29:55 -05001423 rc = rbd_dev_refresh(rbd_dev, &hver);
Sage Weil13143d22011-05-12 16:08:30 -07001424 if (rc)
Alex Elder06ecc6c2012-11-01 10:17:15 -05001425 rbd_warn(rbd_dev, "got notification but failed to "
1426 " update snaps: %d\n", rc);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001427
Alex Elder7f0a24d2012-07-25 09:32:40 -05001428 rbd_req_sync_notify_ack(rbd_dev, hver, notify_id);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001429}
1430
1431/*
1432 * Request sync osd watch
1433 */
Alex Elder0e6f3222012-07-25 09:32:40 -05001434static int rbd_req_sync_watch(struct rbd_device *rbd_dev)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001435{
Alex Elder139b4312012-11-13 21:11:15 -06001436 struct ceph_osd_req_op *op;
Alex Elder0ce1a792012-07-03 16:01:18 -05001437 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder57cfc102012-06-26 12:57:03 -07001438 int ret;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001439
Alex Elder139b4312012-11-13 21:11:15 -06001440 op = rbd_create_rw_op(CEPH_OSD_OP_WATCH, 0);
1441 if (!op)
Alex Elder57cfc102012-06-26 12:57:03 -07001442 return -ENOMEM;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001443
1444 ret = ceph_osdc_create_event(osdc, rbd_watch_cb, 0,
Alex Elder0ce1a792012-07-03 16:01:18 -05001445 (void *)rbd_dev, &rbd_dev->watch_event);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001446 if (ret < 0)
1447 goto fail;
1448
Alex Elder139b4312012-11-13 21:11:15 -06001449 op->watch.ver = cpu_to_le64(rbd_dev->header.obj_version);
1450 op->watch.cookie = cpu_to_le64(rbd_dev->watch_event->cookie);
1451 op->watch.flag = 1;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001452
Alex Elder25704ac2012-11-09 08:43:16 -06001453 ret = rbd_req_sync_op(rbd_dev,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001454 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
Alex Elder30573d62012-11-13 21:11:15 -06001455 op,
Alex Elder0e6f3222012-07-25 09:32:40 -05001456 rbd_dev->header_name,
1457 0, 0, NULL,
Alex Elder0ce1a792012-07-03 16:01:18 -05001458 &rbd_dev->watch_request, NULL);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001459
1460 if (ret < 0)
1461 goto fail_event;
1462
Alex Elder139b4312012-11-13 21:11:15 -06001463 rbd_destroy_op(op);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001464 return 0;
1465
1466fail_event:
Alex Elder0ce1a792012-07-03 16:01:18 -05001467 ceph_osdc_cancel_event(rbd_dev->watch_event);
1468 rbd_dev->watch_event = NULL;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001469fail:
Alex Elder139b4312012-11-13 21:11:15 -06001470 rbd_destroy_op(op);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001471 return ret;
1472}
1473
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001474/*
1475 * Request sync osd unwatch
1476 */
Alex Elder070c6332012-07-25 09:32:41 -05001477static int rbd_req_sync_unwatch(struct rbd_device *rbd_dev)
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001478{
Alex Elder139b4312012-11-13 21:11:15 -06001479 struct ceph_osd_req_op *op;
Alex Elder57cfc102012-06-26 12:57:03 -07001480 int ret;
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001481
Alex Elder139b4312012-11-13 21:11:15 -06001482 op = rbd_create_rw_op(CEPH_OSD_OP_WATCH, 0);
1483 if (!op)
Alex Elder57cfc102012-06-26 12:57:03 -07001484 return -ENOMEM;
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001485
Alex Elder139b4312012-11-13 21:11:15 -06001486 op->watch.ver = 0;
1487 op->watch.cookie = cpu_to_le64(rbd_dev->watch_event->cookie);
1488 op->watch.flag = 0;
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001489
Alex Elder25704ac2012-11-09 08:43:16 -06001490 ret = rbd_req_sync_op(rbd_dev,
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001491 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
Alex Elder30573d62012-11-13 21:11:15 -06001492 op,
Alex Elder070c6332012-07-25 09:32:41 -05001493 rbd_dev->header_name,
1494 0, 0, NULL, NULL, NULL);
1495
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001496
Alex Elder139b4312012-11-13 21:11:15 -06001497 rbd_destroy_op(op);
Alex Elder0ce1a792012-07-03 16:01:18 -05001498 ceph_osdc_cancel_event(rbd_dev->watch_event);
1499 rbd_dev->watch_event = NULL;
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001500 return ret;
1501}
1502
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001503/*
Alex Elder3cb4a682012-06-26 12:57:03 -07001504 * Synchronous osd object method call
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001505 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001506static int rbd_req_sync_exec(struct rbd_device *rbd_dev,
Alex Elderaded07e2012-07-03 16:01:18 -05001507 const char *object_name,
1508 const char *class_name,
1509 const char *method_name,
Alex Elder3cb4a682012-06-26 12:57:03 -07001510 const char *outbound,
1511 size_t outbound_size,
Alex Elderf8d4de62012-07-03 16:01:19 -05001512 char *inbound,
1513 size_t inbound_size,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001514 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001515{
Alex Elder139b4312012-11-13 21:11:15 -06001516 struct ceph_osd_req_op *op;
Alex Elderaded07e2012-07-03 16:01:18 -05001517 int class_name_len = strlen(class_name);
1518 int method_name_len = strlen(method_name);
Alex Elder3cb4a682012-06-26 12:57:03 -07001519 int payload_size;
Alex Elder57cfc102012-06-26 12:57:03 -07001520 int ret;
1521
Alex Elder3cb4a682012-06-26 12:57:03 -07001522 /*
1523 * Any input parameters required by the method we're calling
1524 * will be sent along with the class and method names as
1525 * part of the message payload. That data and its size are
1526 * supplied via the indata and indata_len fields (named from
1527 * the perspective of the server side) in the OSD request
1528 * operation.
1529 */
1530 payload_size = class_name_len + method_name_len + outbound_size;
Alex Elder139b4312012-11-13 21:11:15 -06001531 op = rbd_create_rw_op(CEPH_OSD_OP_CALL, payload_size);
1532 if (!op)
Alex Elder57cfc102012-06-26 12:57:03 -07001533 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001534
Alex Elder139b4312012-11-13 21:11:15 -06001535 op->cls.class_name = class_name;
1536 op->cls.class_len = (__u8) class_name_len;
1537 op->cls.method_name = method_name;
1538 op->cls.method_len = (__u8) method_name_len;
1539 op->cls.argc = 0;
1540 op->cls.indata = outbound;
1541 op->cls.indata_len = outbound_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001542
Alex Elder30573d62012-11-13 21:11:15 -06001543 ret = rbd_req_sync_op(rbd_dev, CEPH_OSD_FLAG_READ, op,
Alex Elderf8d4de62012-07-03 16:01:19 -05001544 object_name, 0, inbound_size, inbound,
1545 NULL, ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001546
Alex Elder139b4312012-11-13 21:11:15 -06001547 rbd_destroy_op(op);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001548
1549 dout("cls_exec returned %d\n", ret);
1550 return ret;
1551}
1552
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001553static struct rbd_req_coll *rbd_alloc_coll(int num_reqs)
1554{
1555 struct rbd_req_coll *coll =
1556 kzalloc(sizeof(struct rbd_req_coll) +
1557 sizeof(struct rbd_req_status) * num_reqs,
1558 GFP_ATOMIC);
1559
1560 if (!coll)
1561 return NULL;
1562 coll->total = num_reqs;
1563 kref_init(&coll->kref);
1564 return coll;
1565}
1566
Alex Elder8295cda2012-11-08 08:01:39 -06001567static int rbd_dev_do_request(struct request *rq,
1568 struct rbd_device *rbd_dev,
1569 struct ceph_snap_context *snapc,
1570 u64 ofs, unsigned int size,
1571 struct bio *bio_chain)
1572{
1573 int num_segs;
1574 struct rbd_req_coll *coll;
1575 unsigned int bio_offset;
1576 int cur_seg = 0;
1577
1578 dout("%s 0x%x bytes at 0x%llx\n",
1579 rq_data_dir(rq) == WRITE ? "write" : "read",
1580 size, (unsigned long long) blk_rq_pos(rq) * SECTOR_SIZE);
1581
1582 num_segs = rbd_get_num_segments(&rbd_dev->header, ofs, size);
1583 if (num_segs <= 0)
1584 return num_segs;
1585
1586 coll = rbd_alloc_coll(num_segs);
1587 if (!coll)
1588 return -ENOMEM;
1589
1590 bio_offset = 0;
1591 do {
1592 u64 limit = rbd_segment_length(rbd_dev, ofs, size);
1593 unsigned int clone_size;
1594 struct bio *bio_clone;
1595
1596 BUG_ON(limit > (u64)UINT_MAX);
1597 clone_size = (unsigned int)limit;
1598 dout("bio_chain->bi_vcnt=%hu\n", bio_chain->bi_vcnt);
1599
1600 kref_get(&coll->kref);
1601
1602 /* Pass a cloned bio chain via an osd request */
1603
1604 bio_clone = bio_chain_clone_range(&bio_chain,
1605 &bio_offset, clone_size,
1606 GFP_ATOMIC);
1607 if (bio_clone)
1608 (void)rbd_do_op(rq, rbd_dev, snapc,
1609 ofs, clone_size,
1610 bio_clone, coll, cur_seg);
1611 else
1612 rbd_coll_end_req_index(rq, coll, cur_seg,
1613 (s32)-ENOMEM,
1614 clone_size);
1615 size -= clone_size;
1616 ofs += clone_size;
1617
1618 cur_seg++;
1619 } while (size > 0);
1620 kref_put(&coll->kref, rbd_coll_release);
1621
1622 return 0;
1623}
1624
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001625/*
1626 * block device queue callback
1627 */
1628static void rbd_rq_fn(struct request_queue *q)
1629{
1630 struct rbd_device *rbd_dev = q->queuedata;
Alex Elderb395e8b2012-11-08 08:01:39 -06001631 bool read_only = rbd_dev->mapping.read_only;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001632 struct request *rq;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001633
Alex Elder00f1f362012-02-07 12:03:36 -06001634 while ((rq = blk_fetch_request(q))) {
Alex Elderb395e8b2012-11-08 08:01:39 -06001635 struct ceph_snap_context *snapc = NULL;
1636 unsigned int size = 0;
Alex Elder8295cda2012-11-08 08:01:39 -06001637 int result;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001638
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001639 dout("fetched request\n");
1640
Alex Elderb395e8b2012-11-08 08:01:39 -06001641 /* Filter out block requests we don't understand */
1642
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001643 if ((rq->cmd_type != REQ_TYPE_FS)) {
1644 __blk_end_request_all(rq, 0);
Alex Elder00f1f362012-02-07 12:03:36 -06001645 continue;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001646 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001647 spin_unlock_irq(q->queue_lock);
1648
Alex Eldera7b4c652012-11-09 08:43:15 -06001649 /* Write requests need a reference to the snapshot context */
Alex Elderb395e8b2012-11-08 08:01:39 -06001650
Alex Eldera7b4c652012-11-09 08:43:15 -06001651 if (rq_data_dir(rq) == WRITE) {
1652 result = -EROFS;
1653 if (read_only) /* Can't write to a read-only device */
1654 goto out_end_request;
Alex Elderb395e8b2012-11-08 08:01:39 -06001655
Alex Eldera7b4c652012-11-09 08:43:15 -06001656 /*
1657 * Note that each osd request will take its
1658 * own reference to the snapshot context
1659 * supplied. The reference we take here
1660 * just guarantees the one we provide stays
1661 * valid.
1662 */
1663 down_read(&rbd_dev->header_rwsem);
Alex Elderb395e8b2012-11-08 08:01:39 -06001664 snapc = ceph_get_snap_context(rbd_dev->header.snapc);
Alex Eldera7b4c652012-11-09 08:43:15 -06001665 up_read(&rbd_dev->header_rwsem);
Alex Elderb395e8b2012-11-08 08:01:39 -06001666 rbd_assert(snapc != NULL);
Alex Eldera7b4c652012-11-09 08:43:15 -06001667 } else if (!atomic_read(&rbd_dev->exists)) {
Alex Elderb395e8b2012-11-08 08:01:39 -06001668 rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP);
1669 dout("request for non-existent snapshot");
1670 result = -ENXIO;
1671 goto out_end_request;
1672 }
Alex Elderf7760da2012-10-20 22:17:27 -05001673
Alex Elderb395e8b2012-11-08 08:01:39 -06001674 size = blk_rq_bytes(rq);
1675 result = rbd_dev_do_request(rq, rbd_dev, snapc,
1676 blk_rq_pos(rq) * SECTOR_SIZE,
1677 size, rq->bio);
1678out_end_request:
Alex Eldera7b4c652012-11-09 08:43:15 -06001679 if (snapc)
1680 ceph_put_snap_context(snapc);
Alex Elder8295cda2012-11-08 08:01:39 -06001681 spin_lock_irq(q->queue_lock);
1682 if (!size || result < 0)
1683 __blk_end_request_all(rq, result);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001684 }
1685}
1686
1687/*
1688 * a queue callback. Makes sure that we don't create a bio that spans across
1689 * multiple osd objects. One exception would be with a single page bios,
Alex Elderf7760da2012-10-20 22:17:27 -05001690 * which we handle later at bio_chain_clone_range()
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001691 */
1692static int rbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd,
1693 struct bio_vec *bvec)
1694{
1695 struct rbd_device *rbd_dev = q->queuedata;
Alex Eldere5cfeed22012-10-20 22:17:27 -05001696 sector_t sector_offset;
1697 sector_t sectors_per_obj;
1698 sector_t obj_sector_offset;
1699 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001700
Alex Eldere5cfeed22012-10-20 22:17:27 -05001701 /*
1702 * Find how far into its rbd object the partition-relative
1703 * bio start sector is to offset relative to the enclosing
1704 * device.
1705 */
1706 sector_offset = get_start_sect(bmd->bi_bdev) + bmd->bi_sector;
1707 sectors_per_obj = 1 << (rbd_dev->header.obj_order - SECTOR_SHIFT);
1708 obj_sector_offset = sector_offset & (sectors_per_obj - 1);
Alex Elder593a9e72012-02-07 12:03:37 -06001709
Alex Eldere5cfeed22012-10-20 22:17:27 -05001710 /*
1711 * Compute the number of bytes from that offset to the end
1712 * of the object. Account for what's already used by the bio.
1713 */
1714 ret = (int) (sectors_per_obj - obj_sector_offset) << SECTOR_SHIFT;
1715 if (ret > bmd->bi_size)
1716 ret -= bmd->bi_size;
1717 else
1718 ret = 0;
1719
1720 /*
1721 * Don't send back more than was asked for. And if the bio
1722 * was empty, let the whole thing through because: "Note
1723 * that a block device *must* allow a single page to be
1724 * added to an empty bio."
1725 */
1726 rbd_assert(bvec->bv_len <= PAGE_SIZE);
1727 if (ret > (int) bvec->bv_len || !bmd->bi_size)
1728 ret = (int) bvec->bv_len;
1729
1730 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001731}
1732
1733static void rbd_free_disk(struct rbd_device *rbd_dev)
1734{
1735 struct gendisk *disk = rbd_dev->disk;
1736
1737 if (!disk)
1738 return;
1739
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001740 if (disk->flags & GENHD_FL_UP)
1741 del_gendisk(disk);
1742 if (disk->queue)
1743 blk_cleanup_queue(disk->queue);
1744 put_disk(disk);
1745}
1746
1747/*
Alex Elder4156d992012-08-02 11:29:46 -05001748 * Read the complete header for the given rbd device.
1749 *
1750 * Returns a pointer to a dynamically-allocated buffer containing
1751 * the complete and validated header. Caller can pass the address
1752 * of a variable that will be filled in with the version of the
1753 * header object at the time it was read.
1754 *
1755 * Returns a pointer-coded errno if a failure occurs.
1756 */
1757static struct rbd_image_header_ondisk *
1758rbd_dev_v1_header_read(struct rbd_device *rbd_dev, u64 *version)
1759{
1760 struct rbd_image_header_ondisk *ondisk = NULL;
1761 u32 snap_count = 0;
1762 u64 names_size = 0;
1763 u32 want_count;
1764 int ret;
1765
1766 /*
1767 * The complete header will include an array of its 64-bit
1768 * snapshot ids, followed by the names of those snapshots as
1769 * a contiguous block of NUL-terminated strings. Note that
1770 * the number of snapshots could change by the time we read
1771 * it in, in which case we re-read it.
1772 */
1773 do {
1774 size_t size;
1775
1776 kfree(ondisk);
1777
1778 size = sizeof (*ondisk);
1779 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
1780 size += names_size;
1781 ondisk = kmalloc(size, GFP_KERNEL);
1782 if (!ondisk)
1783 return ERR_PTR(-ENOMEM);
1784
Alex Elder47756182012-11-09 08:43:15 -06001785 ret = rbd_req_sync_read(rbd_dev, rbd_dev->header_name,
Alex Elder4156d992012-08-02 11:29:46 -05001786 0, size,
1787 (char *) ondisk, version);
1788
1789 if (ret < 0)
1790 goto out_err;
1791 if (WARN_ON((size_t) ret < size)) {
1792 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05001793 rbd_warn(rbd_dev, "short header read (want %zd got %d)",
1794 size, ret);
Alex Elder4156d992012-08-02 11:29:46 -05001795 goto out_err;
1796 }
1797 if (!rbd_dev_ondisk_valid(ondisk)) {
1798 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05001799 rbd_warn(rbd_dev, "invalid header");
Alex Elder4156d992012-08-02 11:29:46 -05001800 goto out_err;
1801 }
1802
1803 names_size = le64_to_cpu(ondisk->snap_names_len);
1804 want_count = snap_count;
1805 snap_count = le32_to_cpu(ondisk->snap_count);
1806 } while (snap_count != want_count);
1807
1808 return ondisk;
1809
1810out_err:
1811 kfree(ondisk);
1812
1813 return ERR_PTR(ret);
1814}
1815
1816/*
1817 * reload the ondisk the header
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001818 */
1819static int rbd_read_header(struct rbd_device *rbd_dev,
1820 struct rbd_image_header *header)
1821{
Alex Elder4156d992012-08-02 11:29:46 -05001822 struct rbd_image_header_ondisk *ondisk;
1823 u64 ver = 0;
1824 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001825
Alex Elder4156d992012-08-02 11:29:46 -05001826 ondisk = rbd_dev_v1_header_read(rbd_dev, &ver);
1827 if (IS_ERR(ondisk))
1828 return PTR_ERR(ondisk);
1829 ret = rbd_header_from_disk(header, ondisk);
1830 if (ret >= 0)
1831 header->obj_version = ver;
1832 kfree(ondisk);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001833
Alex Elder4156d992012-08-02 11:29:46 -05001834 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001835}
1836
Alex Elder41f38c22012-10-25 23:34:40 -05001837static void rbd_remove_all_snaps(struct rbd_device *rbd_dev)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001838{
1839 struct rbd_snap *snap;
Alex Eldera0593292012-07-19 09:09:27 -05001840 struct rbd_snap *next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001841
Alex Eldera0593292012-07-19 09:09:27 -05001842 list_for_each_entry_safe(snap, next, &rbd_dev->snaps, node)
Alex Elder41f38c22012-10-25 23:34:40 -05001843 rbd_remove_snap_dev(snap);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001844}
1845
Alex Elder94785542012-10-09 13:50:17 -07001846static void rbd_update_mapping_size(struct rbd_device *rbd_dev)
1847{
1848 sector_t size;
1849
Alex Elder0d7dbfc2012-10-25 23:34:41 -05001850 if (rbd_dev->spec->snap_id != CEPH_NOSNAP)
Alex Elder94785542012-10-09 13:50:17 -07001851 return;
1852
1853 size = (sector_t) rbd_dev->header.image_size / SECTOR_SIZE;
1854 dout("setting size to %llu sectors", (unsigned long long) size);
1855 rbd_dev->mapping.size = (u64) size;
1856 set_capacity(rbd_dev->disk, size);
1857}
1858
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001859/*
1860 * only read the first part of the ondisk header, without the snaps info
1861 */
Alex Elder117973f2012-08-31 17:29:55 -05001862static int rbd_dev_v1_refresh(struct rbd_device *rbd_dev, u64 *hver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001863{
1864 int ret;
1865 struct rbd_image_header h;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001866
1867 ret = rbd_read_header(rbd_dev, &h);
1868 if (ret < 0)
1869 return ret;
1870
Josh Durgina51aa0c2011-12-05 10:35:04 -08001871 down_write(&rbd_dev->header_rwsem);
1872
Alex Elder94785542012-10-09 13:50:17 -07001873 /* Update image size, and check for resize of mapped image */
1874 rbd_dev->header.image_size = h.image_size;
1875 rbd_update_mapping_size(rbd_dev);
Sage Weil9db4b3e2011-04-19 22:49:06 -07001876
Alex Elder849b4262012-07-09 21:04:24 -05001877 /* rbd_dev->header.object_prefix shouldn't change */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001878 kfree(rbd_dev->header.snap_sizes);
Alex Elder849b4262012-07-09 21:04:24 -05001879 kfree(rbd_dev->header.snap_names);
Josh Durgind1d25642011-12-05 14:03:05 -08001880 /* osd requests may still refer to snapc */
1881 ceph_put_snap_context(rbd_dev->header.snapc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001882
Alex Elderb8136232012-07-25 09:32:41 -05001883 if (hver)
1884 *hver = h.obj_version;
Josh Durgina71b8912011-12-05 18:10:44 -08001885 rbd_dev->header.obj_version = h.obj_version;
Josh Durgin93a24e02011-12-05 10:41:28 -08001886 rbd_dev->header.image_size = h.image_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001887 rbd_dev->header.snapc = h.snapc;
1888 rbd_dev->header.snap_names = h.snap_names;
1889 rbd_dev->header.snap_sizes = h.snap_sizes;
Alex Elder849b4262012-07-09 21:04:24 -05001890 /* Free the extra copy of the object prefix */
1891 WARN_ON(strcmp(rbd_dev->header.object_prefix, h.object_prefix));
1892 kfree(h.object_prefix);
1893
Alex Elder304f6802012-08-31 17:29:52 -05001894 ret = rbd_dev_snaps_update(rbd_dev);
1895 if (!ret)
1896 ret = rbd_dev_snaps_register(rbd_dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001897
Josh Durginc6666012011-11-21 17:11:12 -08001898 up_write(&rbd_dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001899
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001900 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001901}
1902
Alex Elder117973f2012-08-31 17:29:55 -05001903static int rbd_dev_refresh(struct rbd_device *rbd_dev, u64 *hver)
Alex Elder1fe5e992012-07-25 09:32:41 -05001904{
1905 int ret;
1906
Alex Elder117973f2012-08-31 17:29:55 -05001907 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
Alex Elder1fe5e992012-07-25 09:32:41 -05001908 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Alex Elder117973f2012-08-31 17:29:55 -05001909 if (rbd_dev->image_format == 1)
1910 ret = rbd_dev_v1_refresh(rbd_dev, hver);
1911 else
1912 ret = rbd_dev_v2_refresh(rbd_dev, hver);
Alex Elder1fe5e992012-07-25 09:32:41 -05001913 mutex_unlock(&ctl_mutex);
1914
1915 return ret;
1916}
1917
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001918static int rbd_init_disk(struct rbd_device *rbd_dev)
1919{
1920 struct gendisk *disk;
1921 struct request_queue *q;
Alex Elder593a9e72012-02-07 12:03:37 -06001922 u64 segment_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001923
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001924 /* create gendisk info */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001925 disk = alloc_disk(RBD_MINORS_PER_MAJOR);
1926 if (!disk)
Alex Elder1fcdb8a2012-08-29 17:11:06 -05001927 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001928
Alex Elderf0f8cef2012-01-29 13:57:44 -06001929 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
Alex Elderde71a292012-07-03 16:01:19 -05001930 rbd_dev->dev_id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001931 disk->major = rbd_dev->major;
1932 disk->first_minor = 0;
1933 disk->fops = &rbd_bd_ops;
1934 disk->private_data = rbd_dev;
1935
1936 /* init rq */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001937 q = blk_init_queue(rbd_rq_fn, &rbd_dev->lock);
1938 if (!q)
1939 goto out_disk;
Josh Durgin029bcbd2011-07-22 11:35:23 -07001940
Alex Elder593a9e72012-02-07 12:03:37 -06001941 /* We use the default size, but let's be explicit about it. */
1942 blk_queue_physical_block_size(q, SECTOR_SIZE);
1943
Josh Durgin029bcbd2011-07-22 11:35:23 -07001944 /* set io sizes to object size */
Alex Elder593a9e72012-02-07 12:03:37 -06001945 segment_size = rbd_obj_bytes(&rbd_dev->header);
1946 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
1947 blk_queue_max_segment_size(q, segment_size);
1948 blk_queue_io_min(q, segment_size);
1949 blk_queue_io_opt(q, segment_size);
Josh Durgin029bcbd2011-07-22 11:35:23 -07001950
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001951 blk_queue_merge_bvec(q, rbd_merge_bvec);
1952 disk->queue = q;
1953
1954 q->queuedata = rbd_dev;
1955
1956 rbd_dev->disk = disk;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001957
Alex Elder12f02942012-08-29 17:11:07 -05001958 set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
1959
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001960 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001961out_disk:
1962 put_disk(disk);
Alex Elder1fcdb8a2012-08-29 17:11:06 -05001963
1964 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001965}
1966
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001967/*
1968 sysfs
1969*/
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001970
Alex Elder593a9e72012-02-07 12:03:37 -06001971static struct rbd_device *dev_to_rbd_dev(struct device *dev)
1972{
1973 return container_of(dev, struct rbd_device, dev);
1974}
1975
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001976static ssize_t rbd_size_show(struct device *dev,
1977 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001978{
Alex Elder593a9e72012-02-07 12:03:37 -06001979 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Josh Durgina51aa0c2011-12-05 10:35:04 -08001980 sector_t size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001981
Josh Durgina51aa0c2011-12-05 10:35:04 -08001982 down_read(&rbd_dev->header_rwsem);
1983 size = get_capacity(rbd_dev->disk);
1984 up_read(&rbd_dev->header_rwsem);
1985
1986 return sprintf(buf, "%llu\n", (unsigned long long) size * SECTOR_SIZE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001987}
1988
Alex Elder34b13182012-07-13 20:35:12 -05001989/*
1990 * Note this shows the features for whatever's mapped, which is not
1991 * necessarily the base image.
1992 */
1993static ssize_t rbd_features_show(struct device *dev,
1994 struct device_attribute *attr, char *buf)
1995{
1996 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
1997
1998 return sprintf(buf, "0x%016llx\n",
1999 (unsigned long long) rbd_dev->mapping.features);
2000}
2001
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002002static ssize_t rbd_major_show(struct device *dev,
2003 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002004{
Alex Elder593a9e72012-02-07 12:03:37 -06002005 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002006
2007 return sprintf(buf, "%d\n", rbd_dev->major);
2008}
2009
2010static ssize_t rbd_client_id_show(struct device *dev,
2011 struct device_attribute *attr, char *buf)
2012{
Alex Elder593a9e72012-02-07 12:03:37 -06002013 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002014
Alex Elder1dbb4392012-01-24 10:08:37 -06002015 return sprintf(buf, "client%lld\n",
2016 ceph_client_id(rbd_dev->rbd_client->client));
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002017}
2018
2019static ssize_t rbd_pool_show(struct device *dev,
2020 struct device_attribute *attr, char *buf)
2021{
Alex Elder593a9e72012-02-07 12:03:37 -06002022 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002023
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002024 return sprintf(buf, "%s\n", rbd_dev->spec->pool_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002025}
2026
Alex Elder9bb2f332012-07-12 10:46:35 -05002027static ssize_t rbd_pool_id_show(struct device *dev,
2028 struct device_attribute *attr, char *buf)
2029{
2030 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
2031
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002032 return sprintf(buf, "%llu\n",
2033 (unsigned long long) rbd_dev->spec->pool_id);
Alex Elder9bb2f332012-07-12 10:46:35 -05002034}
2035
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002036static ssize_t rbd_name_show(struct device *dev,
2037 struct device_attribute *attr, char *buf)
2038{
Alex Elder593a9e72012-02-07 12:03:37 -06002039 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002040
Alex Eldera92ffdf2012-10-30 19:40:33 -05002041 if (rbd_dev->spec->image_name)
2042 return sprintf(buf, "%s\n", rbd_dev->spec->image_name);
2043
2044 return sprintf(buf, "(unknown)\n");
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002045}
2046
Alex Elder589d30e2012-07-10 20:30:11 -05002047static ssize_t rbd_image_id_show(struct device *dev,
2048 struct device_attribute *attr, char *buf)
2049{
2050 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
2051
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002052 return sprintf(buf, "%s\n", rbd_dev->spec->image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05002053}
2054
Alex Elder34b13182012-07-13 20:35:12 -05002055/*
2056 * Shows the name of the currently-mapped snapshot (or
2057 * RBD_SNAP_HEAD_NAME for the base image).
2058 */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002059static ssize_t rbd_snap_show(struct device *dev,
2060 struct device_attribute *attr,
2061 char *buf)
2062{
Alex Elder593a9e72012-02-07 12:03:37 -06002063 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002064
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002065 return sprintf(buf, "%s\n", rbd_dev->spec->snap_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002066}
2067
Alex Elder86b00e02012-10-25 23:34:42 -05002068/*
2069 * For an rbd v2 image, shows the pool id, image id, and snapshot id
2070 * for the parent image. If there is no parent, simply shows
2071 * "(no parent image)".
2072 */
2073static ssize_t rbd_parent_show(struct device *dev,
2074 struct device_attribute *attr,
2075 char *buf)
2076{
2077 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
2078 struct rbd_spec *spec = rbd_dev->parent_spec;
2079 int count;
2080 char *bufp = buf;
2081
2082 if (!spec)
2083 return sprintf(buf, "(no parent image)\n");
2084
2085 count = sprintf(bufp, "pool_id %llu\npool_name %s\n",
2086 (unsigned long long) spec->pool_id, spec->pool_name);
2087 if (count < 0)
2088 return count;
2089 bufp += count;
2090
2091 count = sprintf(bufp, "image_id %s\nimage_name %s\n", spec->image_id,
2092 spec->image_name ? spec->image_name : "(unknown)");
2093 if (count < 0)
2094 return count;
2095 bufp += count;
2096
2097 count = sprintf(bufp, "snap_id %llu\nsnap_name %s\n",
2098 (unsigned long long) spec->snap_id, spec->snap_name);
2099 if (count < 0)
2100 return count;
2101 bufp += count;
2102
2103 count = sprintf(bufp, "overlap %llu\n", rbd_dev->parent_overlap);
2104 if (count < 0)
2105 return count;
2106 bufp += count;
2107
2108 return (ssize_t) (bufp - buf);
2109}
2110
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002111static ssize_t rbd_image_refresh(struct device *dev,
2112 struct device_attribute *attr,
2113 const char *buf,
2114 size_t size)
2115{
Alex Elder593a9e72012-02-07 12:03:37 -06002116 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Alex Elderb8136232012-07-25 09:32:41 -05002117 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002118
Alex Elder117973f2012-08-31 17:29:55 -05002119 ret = rbd_dev_refresh(rbd_dev, NULL);
Alex Elderb8136232012-07-25 09:32:41 -05002120
2121 return ret < 0 ? ret : size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002122}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002123
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002124static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
Alex Elder34b13182012-07-13 20:35:12 -05002125static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002126static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
2127static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
2128static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
Alex Elder9bb2f332012-07-12 10:46:35 -05002129static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002130static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
Alex Elder589d30e2012-07-10 20:30:11 -05002131static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002132static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
2133static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
Alex Elder86b00e02012-10-25 23:34:42 -05002134static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002135
2136static struct attribute *rbd_attrs[] = {
2137 &dev_attr_size.attr,
Alex Elder34b13182012-07-13 20:35:12 -05002138 &dev_attr_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002139 &dev_attr_major.attr,
2140 &dev_attr_client_id.attr,
2141 &dev_attr_pool.attr,
Alex Elder9bb2f332012-07-12 10:46:35 -05002142 &dev_attr_pool_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002143 &dev_attr_name.attr,
Alex Elder589d30e2012-07-10 20:30:11 -05002144 &dev_attr_image_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002145 &dev_attr_current_snap.attr,
Alex Elder86b00e02012-10-25 23:34:42 -05002146 &dev_attr_parent.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002147 &dev_attr_refresh.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002148 NULL
2149};
2150
2151static struct attribute_group rbd_attr_group = {
2152 .attrs = rbd_attrs,
2153};
2154
2155static const struct attribute_group *rbd_attr_groups[] = {
2156 &rbd_attr_group,
2157 NULL
2158};
2159
2160static void rbd_sysfs_dev_release(struct device *dev)
2161{
2162}
2163
2164static struct device_type rbd_device_type = {
2165 .name = "rbd",
2166 .groups = rbd_attr_groups,
2167 .release = rbd_sysfs_dev_release,
2168};
2169
2170
2171/*
2172 sysfs - snapshots
2173*/
2174
2175static ssize_t rbd_snap_size_show(struct device *dev,
2176 struct device_attribute *attr,
2177 char *buf)
2178{
2179 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2180
Josh Durgin3591538f2011-12-05 18:25:13 -08002181 return sprintf(buf, "%llu\n", (unsigned long long)snap->size);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002182}
2183
2184static ssize_t rbd_snap_id_show(struct device *dev,
2185 struct device_attribute *attr,
2186 char *buf)
2187{
2188 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2189
Josh Durgin3591538f2011-12-05 18:25:13 -08002190 return sprintf(buf, "%llu\n", (unsigned long long)snap->id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002191}
2192
Alex Elder34b13182012-07-13 20:35:12 -05002193static ssize_t rbd_snap_features_show(struct device *dev,
2194 struct device_attribute *attr,
2195 char *buf)
2196{
2197 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2198
2199 return sprintf(buf, "0x%016llx\n",
2200 (unsigned long long) snap->features);
2201}
2202
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002203static DEVICE_ATTR(snap_size, S_IRUGO, rbd_snap_size_show, NULL);
2204static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL);
Alex Elder34b13182012-07-13 20:35:12 -05002205static DEVICE_ATTR(snap_features, S_IRUGO, rbd_snap_features_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002206
2207static struct attribute *rbd_snap_attrs[] = {
2208 &dev_attr_snap_size.attr,
2209 &dev_attr_snap_id.attr,
Alex Elder34b13182012-07-13 20:35:12 -05002210 &dev_attr_snap_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002211 NULL,
2212};
2213
2214static struct attribute_group rbd_snap_attr_group = {
2215 .attrs = rbd_snap_attrs,
2216};
2217
2218static void rbd_snap_dev_release(struct device *dev)
2219{
2220 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2221 kfree(snap->name);
2222 kfree(snap);
2223}
2224
2225static const struct attribute_group *rbd_snap_attr_groups[] = {
2226 &rbd_snap_attr_group,
2227 NULL
2228};
2229
2230static struct device_type rbd_snap_device_type = {
2231 .groups = rbd_snap_attr_groups,
2232 .release = rbd_snap_dev_release,
2233};
2234
Alex Elder8b8fb992012-10-26 17:25:24 -05002235static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)
2236{
2237 kref_get(&spec->kref);
2238
2239 return spec;
2240}
2241
2242static void rbd_spec_free(struct kref *kref);
2243static void rbd_spec_put(struct rbd_spec *spec)
2244{
2245 if (spec)
2246 kref_put(&spec->kref, rbd_spec_free);
2247}
2248
2249static struct rbd_spec *rbd_spec_alloc(void)
2250{
2251 struct rbd_spec *spec;
2252
2253 spec = kzalloc(sizeof (*spec), GFP_KERNEL);
2254 if (!spec)
2255 return NULL;
2256 kref_init(&spec->kref);
2257
2258 rbd_spec_put(rbd_spec_get(spec)); /* TEMPORARY */
2259
2260 return spec;
2261}
2262
2263static void rbd_spec_free(struct kref *kref)
2264{
2265 struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref);
2266
2267 kfree(spec->pool_name);
2268 kfree(spec->image_id);
2269 kfree(spec->image_name);
2270 kfree(spec->snap_name);
2271 kfree(spec);
2272}
2273
Alex Elderc53d5892012-10-25 23:34:42 -05002274struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,
2275 struct rbd_spec *spec)
2276{
2277 struct rbd_device *rbd_dev;
2278
2279 rbd_dev = kzalloc(sizeof (*rbd_dev), GFP_KERNEL);
2280 if (!rbd_dev)
2281 return NULL;
2282
2283 spin_lock_init(&rbd_dev->lock);
Alex Elderd78b6502012-11-09 08:43:15 -06002284 atomic_set(&rbd_dev->exists, 0);
Alex Elderc53d5892012-10-25 23:34:42 -05002285 INIT_LIST_HEAD(&rbd_dev->node);
2286 INIT_LIST_HEAD(&rbd_dev->snaps);
2287 init_rwsem(&rbd_dev->header_rwsem);
2288
2289 rbd_dev->spec = spec;
2290 rbd_dev->rbd_client = rbdc;
2291
Alex Elder0903e872012-11-14 12:25:19 -06002292 /* Initialize the layout used for all rbd requests */
2293
2294 rbd_dev->layout.fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
2295 rbd_dev->layout.fl_stripe_count = cpu_to_le32(1);
2296 rbd_dev->layout.fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
2297 rbd_dev->layout.fl_pg_pool = cpu_to_le32((u32) spec->pool_id);
2298
Alex Elderc53d5892012-10-25 23:34:42 -05002299 return rbd_dev;
2300}
2301
2302static void rbd_dev_destroy(struct rbd_device *rbd_dev)
2303{
Alex Elder86b00e02012-10-25 23:34:42 -05002304 rbd_spec_put(rbd_dev->parent_spec);
Alex Elderc53d5892012-10-25 23:34:42 -05002305 kfree(rbd_dev->header_name);
2306 rbd_put_client(rbd_dev->rbd_client);
2307 rbd_spec_put(rbd_dev->spec);
2308 kfree(rbd_dev);
2309}
2310
Alex Elder304f6802012-08-31 17:29:52 -05002311static bool rbd_snap_registered(struct rbd_snap *snap)
2312{
2313 bool ret = snap->dev.type == &rbd_snap_device_type;
2314 bool reg = device_is_registered(&snap->dev);
2315
2316 rbd_assert(!ret ^ reg);
2317
2318 return ret;
2319}
2320
Alex Elder41f38c22012-10-25 23:34:40 -05002321static void rbd_remove_snap_dev(struct rbd_snap *snap)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002322{
2323 list_del(&snap->node);
Alex Elder304f6802012-08-31 17:29:52 -05002324 if (device_is_registered(&snap->dev))
2325 device_unregister(&snap->dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002326}
2327
Alex Elder14e70852012-07-19 09:09:27 -05002328static int rbd_register_snap_dev(struct rbd_snap *snap,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002329 struct device *parent)
2330{
2331 struct device *dev = &snap->dev;
2332 int ret;
2333
2334 dev->type = &rbd_snap_device_type;
2335 dev->parent = parent;
2336 dev->release = rbd_snap_dev_release;
Alex Elderd4b125e2012-07-03 16:01:19 -05002337 dev_set_name(dev, "%s%s", RBD_SNAP_DEV_NAME_PREFIX, snap->name);
Alex Elder304f6802012-08-31 17:29:52 -05002338 dout("%s: registering device for snapshot %s\n", __func__, snap->name);
2339
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002340 ret = device_register(dev);
2341
2342 return ret;
2343}
2344
Alex Elder4e891e02012-07-10 20:30:10 -05002345static struct rbd_snap *__rbd_add_snap_dev(struct rbd_device *rbd_dev,
Alex Elderc8d18422012-07-10 20:30:11 -05002346 const char *snap_name,
Alex Elder34b13182012-07-13 20:35:12 -05002347 u64 snap_id, u64 snap_size,
2348 u64 snap_features)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002349{
Alex Elder4e891e02012-07-10 20:30:10 -05002350 struct rbd_snap *snap;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002351 int ret;
Alex Elder4e891e02012-07-10 20:30:10 -05002352
2353 snap = kzalloc(sizeof (*snap), GFP_KERNEL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002354 if (!snap)
Alex Elder4e891e02012-07-10 20:30:10 -05002355 return ERR_PTR(-ENOMEM);
2356
2357 ret = -ENOMEM;
Alex Elderc8d18422012-07-10 20:30:11 -05002358 snap->name = kstrdup(snap_name, GFP_KERNEL);
Alex Elder4e891e02012-07-10 20:30:10 -05002359 if (!snap->name)
2360 goto err;
2361
Alex Elderc8d18422012-07-10 20:30:11 -05002362 snap->id = snap_id;
2363 snap->size = snap_size;
Alex Elder34b13182012-07-13 20:35:12 -05002364 snap->features = snap_features;
Alex Elder4e891e02012-07-10 20:30:10 -05002365
2366 return snap;
2367
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002368err:
2369 kfree(snap->name);
2370 kfree(snap);
Alex Elder4e891e02012-07-10 20:30:10 -05002371
2372 return ERR_PTR(ret);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002373}
2374
Alex Eldercd892122012-07-03 16:01:19 -05002375static char *rbd_dev_v1_snap_info(struct rbd_device *rbd_dev, u32 which,
2376 u64 *snap_size, u64 *snap_features)
2377{
2378 char *snap_name;
2379
2380 rbd_assert(which < rbd_dev->header.snapc->num_snaps);
2381
2382 *snap_size = rbd_dev->header.snap_sizes[which];
2383 *snap_features = 0; /* No features for v1 */
2384
2385 /* Skip over names until we find the one we are looking for */
2386
2387 snap_name = rbd_dev->header.snap_names;
2388 while (which--)
2389 snap_name += strlen(snap_name) + 1;
2390
2391 return snap_name;
2392}
2393
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002394/*
Alex Elder9d475de2012-07-03 16:01:19 -05002395 * Get the size and object order for an image snapshot, or if
2396 * snap_id is CEPH_NOSNAP, gets this information for the base
2397 * image.
2398 */
2399static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
2400 u8 *order, u64 *snap_size)
2401{
2402 __le64 snapid = cpu_to_le64(snap_id);
2403 int ret;
2404 struct {
2405 u8 order;
2406 __le64 size;
2407 } __attribute__ ((packed)) size_buf = { 0 };
2408
2409 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2410 "rbd", "get_size",
2411 (char *) &snapid, sizeof (snapid),
Alex Elder07b23912012-11-09 08:43:16 -06002412 (char *) &size_buf, sizeof (size_buf), NULL);
Alex Elder9d475de2012-07-03 16:01:19 -05002413 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2414 if (ret < 0)
2415 return ret;
2416
2417 *order = size_buf.order;
2418 *snap_size = le64_to_cpu(size_buf.size);
2419
2420 dout(" snap_id 0x%016llx order = %u, snap_size = %llu\n",
2421 (unsigned long long) snap_id, (unsigned int) *order,
2422 (unsigned long long) *snap_size);
2423
2424 return 0;
2425}
2426
2427static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
2428{
2429 return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
2430 &rbd_dev->header.obj_order,
2431 &rbd_dev->header.image_size);
2432}
2433
Alex Elder1e130192012-07-03 16:01:19 -05002434static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
2435{
2436 void *reply_buf;
2437 int ret;
2438 void *p;
2439
2440 reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL);
2441 if (!reply_buf)
2442 return -ENOMEM;
2443
2444 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2445 "rbd", "get_object_prefix",
2446 NULL, 0,
Alex Elder07b23912012-11-09 08:43:16 -06002447 reply_buf, RBD_OBJ_PREFIX_LEN_MAX, NULL);
Alex Elder1e130192012-07-03 16:01:19 -05002448 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2449 if (ret < 0)
2450 goto out;
Alex Eldera0ea3a42012-10-10 21:19:13 -07002451 ret = 0; /* rbd_req_sync_exec() can return positive */
Alex Elder1e130192012-07-03 16:01:19 -05002452
2453 p = reply_buf;
2454 rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p,
2455 p + RBD_OBJ_PREFIX_LEN_MAX,
2456 NULL, GFP_NOIO);
2457
2458 if (IS_ERR(rbd_dev->header.object_prefix)) {
2459 ret = PTR_ERR(rbd_dev->header.object_prefix);
2460 rbd_dev->header.object_prefix = NULL;
2461 } else {
2462 dout(" object_prefix = %s\n", rbd_dev->header.object_prefix);
2463 }
2464
2465out:
2466 kfree(reply_buf);
2467
2468 return ret;
2469}
2470
Alex Elderb1b54022012-07-03 16:01:19 -05002471static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
2472 u64 *snap_features)
2473{
2474 __le64 snapid = cpu_to_le64(snap_id);
2475 struct {
2476 __le64 features;
2477 __le64 incompat;
2478 } features_buf = { 0 };
Alex Elderd8891402012-10-09 13:50:17 -07002479 u64 incompat;
Alex Elderb1b54022012-07-03 16:01:19 -05002480 int ret;
2481
2482 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2483 "rbd", "get_features",
2484 (char *) &snapid, sizeof (snapid),
2485 (char *) &features_buf, sizeof (features_buf),
Alex Elder07b23912012-11-09 08:43:16 -06002486 NULL);
Alex Elderb1b54022012-07-03 16:01:19 -05002487 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2488 if (ret < 0)
2489 return ret;
Alex Elderd8891402012-10-09 13:50:17 -07002490
2491 incompat = le64_to_cpu(features_buf.incompat);
2492 if (incompat & ~RBD_FEATURES_ALL)
Alex Elderb8f5c6e2012-11-01 08:39:26 -05002493 return -ENXIO;
Alex Elderd8891402012-10-09 13:50:17 -07002494
Alex Elderb1b54022012-07-03 16:01:19 -05002495 *snap_features = le64_to_cpu(features_buf.features);
2496
2497 dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
2498 (unsigned long long) snap_id,
2499 (unsigned long long) *snap_features,
2500 (unsigned long long) le64_to_cpu(features_buf.incompat));
2501
2502 return 0;
2503}
2504
2505static int rbd_dev_v2_features(struct rbd_device *rbd_dev)
2506{
2507 return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP,
2508 &rbd_dev->header.features);
2509}
2510
Alex Elder86b00e02012-10-25 23:34:42 -05002511static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev)
2512{
2513 struct rbd_spec *parent_spec;
2514 size_t size;
2515 void *reply_buf = NULL;
2516 __le64 snapid;
2517 void *p;
2518 void *end;
2519 char *image_id;
2520 u64 overlap;
Alex Elder86b00e02012-10-25 23:34:42 -05002521 int ret;
2522
2523 parent_spec = rbd_spec_alloc();
2524 if (!parent_spec)
2525 return -ENOMEM;
2526
2527 size = sizeof (__le64) + /* pool_id */
2528 sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */
2529 sizeof (__le64) + /* snap_id */
2530 sizeof (__le64); /* overlap */
2531 reply_buf = kmalloc(size, GFP_KERNEL);
2532 if (!reply_buf) {
2533 ret = -ENOMEM;
2534 goto out_err;
2535 }
2536
2537 snapid = cpu_to_le64(CEPH_NOSNAP);
2538 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2539 "rbd", "get_parent",
2540 (char *) &snapid, sizeof (snapid),
Alex Elder07b23912012-11-09 08:43:16 -06002541 (char *) reply_buf, size, NULL);
Alex Elder86b00e02012-10-25 23:34:42 -05002542 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2543 if (ret < 0)
2544 goto out_err;
2545
2546 ret = -ERANGE;
2547 p = reply_buf;
2548 end = (char *) reply_buf + size;
2549 ceph_decode_64_safe(&p, end, parent_spec->pool_id, out_err);
2550 if (parent_spec->pool_id == CEPH_NOPOOL)
2551 goto out; /* No parent? No problem. */
2552
Alex Elder0903e872012-11-14 12:25:19 -06002553 /* The ceph file layout needs to fit pool id in 32 bits */
2554
2555 ret = -EIO;
2556 if (WARN_ON(parent_spec->pool_id > (u64) U32_MAX))
2557 goto out;
2558
Alex Elder979ed482012-11-01 08:39:26 -05002559 image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elder86b00e02012-10-25 23:34:42 -05002560 if (IS_ERR(image_id)) {
2561 ret = PTR_ERR(image_id);
2562 goto out_err;
2563 }
2564 parent_spec->image_id = image_id;
2565 ceph_decode_64_safe(&p, end, parent_spec->snap_id, out_err);
2566 ceph_decode_64_safe(&p, end, overlap, out_err);
2567
2568 rbd_dev->parent_overlap = overlap;
2569 rbd_dev->parent_spec = parent_spec;
2570 parent_spec = NULL; /* rbd_dev now owns this */
2571out:
2572 ret = 0;
2573out_err:
2574 kfree(reply_buf);
2575 rbd_spec_put(parent_spec);
2576
2577 return ret;
2578}
2579
Alex Elder9e15b772012-10-30 19:40:33 -05002580static char *rbd_dev_image_name(struct rbd_device *rbd_dev)
2581{
2582 size_t image_id_size;
2583 char *image_id;
2584 void *p;
2585 void *end;
2586 size_t size;
2587 void *reply_buf = NULL;
2588 size_t len = 0;
2589 char *image_name = NULL;
2590 int ret;
2591
2592 rbd_assert(!rbd_dev->spec->image_name);
2593
Alex Elder69e7a022012-11-01 08:39:26 -05002594 len = strlen(rbd_dev->spec->image_id);
2595 image_id_size = sizeof (__le32) + len;
Alex Elder9e15b772012-10-30 19:40:33 -05002596 image_id = kmalloc(image_id_size, GFP_KERNEL);
2597 if (!image_id)
2598 return NULL;
2599
2600 p = image_id;
2601 end = (char *) image_id + image_id_size;
Alex Elder69e7a022012-11-01 08:39:26 -05002602 ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32) len);
Alex Elder9e15b772012-10-30 19:40:33 -05002603
2604 size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX;
2605 reply_buf = kmalloc(size, GFP_KERNEL);
2606 if (!reply_buf)
2607 goto out;
2608
2609 ret = rbd_req_sync_exec(rbd_dev, RBD_DIRECTORY,
2610 "rbd", "dir_get_name",
2611 image_id, image_id_size,
Alex Elder07b23912012-11-09 08:43:16 -06002612 (char *) reply_buf, size, NULL);
Alex Elder9e15b772012-10-30 19:40:33 -05002613 if (ret < 0)
2614 goto out;
2615 p = reply_buf;
2616 end = (char *) reply_buf + size;
2617 image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL);
2618 if (IS_ERR(image_name))
2619 image_name = NULL;
2620 else
2621 dout("%s: name is %s len is %zd\n", __func__, image_name, len);
2622out:
2623 kfree(reply_buf);
2624 kfree(image_id);
2625
2626 return image_name;
2627}
2628
2629/*
2630 * When a parent image gets probed, we only have the pool, image,
2631 * and snapshot ids but not the names of any of them. This call
2632 * is made later to fill in those names. It has to be done after
2633 * rbd_dev_snaps_update() has completed because some of the
2634 * information (in particular, snapshot name) is not available
2635 * until then.
2636 */
2637static int rbd_dev_probe_update_spec(struct rbd_device *rbd_dev)
2638{
2639 struct ceph_osd_client *osdc;
2640 const char *name;
2641 void *reply_buf = NULL;
2642 int ret;
2643
2644 if (rbd_dev->spec->pool_name)
2645 return 0; /* Already have the names */
2646
2647 /* Look up the pool name */
2648
2649 osdc = &rbd_dev->rbd_client->client->osdc;
2650 name = ceph_pg_pool_name_by_id(osdc->osdmap, rbd_dev->spec->pool_id);
Alex Elder935dc892012-11-01 10:17:15 -05002651 if (!name) {
2652 rbd_warn(rbd_dev, "there is no pool with id %llu",
2653 rbd_dev->spec->pool_id); /* Really a BUG() */
2654 return -EIO;
2655 }
Alex Elder9e15b772012-10-30 19:40:33 -05002656
2657 rbd_dev->spec->pool_name = kstrdup(name, GFP_KERNEL);
2658 if (!rbd_dev->spec->pool_name)
2659 return -ENOMEM;
2660
2661 /* Fetch the image name; tolerate failure here */
2662
2663 name = rbd_dev_image_name(rbd_dev);
Alex Elder69e7a022012-11-01 08:39:26 -05002664 if (name)
Alex Elder9e15b772012-10-30 19:40:33 -05002665 rbd_dev->spec->image_name = (char *) name;
Alex Elder69e7a022012-11-01 08:39:26 -05002666 else
Alex Elder06ecc6c2012-11-01 10:17:15 -05002667 rbd_warn(rbd_dev, "unable to get image name");
Alex Elder9e15b772012-10-30 19:40:33 -05002668
2669 /* Look up the snapshot name. */
2670
2671 name = rbd_snap_name(rbd_dev, rbd_dev->spec->snap_id);
2672 if (!name) {
Alex Elder935dc892012-11-01 10:17:15 -05002673 rbd_warn(rbd_dev, "no snapshot with id %llu",
2674 rbd_dev->spec->snap_id); /* Really a BUG() */
Alex Elder9e15b772012-10-30 19:40:33 -05002675 ret = -EIO;
2676 goto out_err;
2677 }
2678 rbd_dev->spec->snap_name = kstrdup(name, GFP_KERNEL);
2679 if(!rbd_dev->spec->snap_name)
2680 goto out_err;
2681
2682 return 0;
2683out_err:
2684 kfree(reply_buf);
2685 kfree(rbd_dev->spec->pool_name);
2686 rbd_dev->spec->pool_name = NULL;
2687
2688 return ret;
2689}
2690
Alex Elder6e14b1a2012-07-03 16:01:19 -05002691static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev, u64 *ver)
Alex Elder35d489f2012-07-03 16:01:19 -05002692{
2693 size_t size;
2694 int ret;
2695 void *reply_buf;
2696 void *p;
2697 void *end;
2698 u64 seq;
2699 u32 snap_count;
2700 struct ceph_snap_context *snapc;
2701 u32 i;
2702
2703 /*
2704 * We'll need room for the seq value (maximum snapshot id),
2705 * snapshot count, and array of that many snapshot ids.
2706 * For now we have a fixed upper limit on the number we're
2707 * prepared to receive.
2708 */
2709 size = sizeof (__le64) + sizeof (__le32) +
2710 RBD_MAX_SNAP_COUNT * sizeof (__le64);
2711 reply_buf = kzalloc(size, GFP_KERNEL);
2712 if (!reply_buf)
2713 return -ENOMEM;
2714
2715 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2716 "rbd", "get_snapcontext",
2717 NULL, 0,
Alex Elder07b23912012-11-09 08:43:16 -06002718 reply_buf, size, ver);
Alex Elder35d489f2012-07-03 16:01:19 -05002719 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2720 if (ret < 0)
2721 goto out;
2722
2723 ret = -ERANGE;
2724 p = reply_buf;
2725 end = (char *) reply_buf + size;
2726 ceph_decode_64_safe(&p, end, seq, out);
2727 ceph_decode_32_safe(&p, end, snap_count, out);
2728
2729 /*
2730 * Make sure the reported number of snapshot ids wouldn't go
2731 * beyond the end of our buffer. But before checking that,
2732 * make sure the computed size of the snapshot context we
2733 * allocate is representable in a size_t.
2734 */
2735 if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
2736 / sizeof (u64)) {
2737 ret = -EINVAL;
2738 goto out;
2739 }
2740 if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
2741 goto out;
2742
2743 size = sizeof (struct ceph_snap_context) +
2744 snap_count * sizeof (snapc->snaps[0]);
2745 snapc = kmalloc(size, GFP_KERNEL);
2746 if (!snapc) {
2747 ret = -ENOMEM;
2748 goto out;
2749 }
2750
2751 atomic_set(&snapc->nref, 1);
2752 snapc->seq = seq;
2753 snapc->num_snaps = snap_count;
2754 for (i = 0; i < snap_count; i++)
2755 snapc->snaps[i] = ceph_decode_64(&p);
2756
2757 rbd_dev->header.snapc = snapc;
2758
2759 dout(" snap context seq = %llu, snap_count = %u\n",
2760 (unsigned long long) seq, (unsigned int) snap_count);
2761
2762out:
2763 kfree(reply_buf);
2764
2765 return 0;
2766}
2767
Alex Elderb8b1e2d2012-07-03 16:01:19 -05002768static char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, u32 which)
2769{
2770 size_t size;
2771 void *reply_buf;
2772 __le64 snap_id;
2773 int ret;
2774 void *p;
2775 void *end;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05002776 char *snap_name;
2777
2778 size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN;
2779 reply_buf = kmalloc(size, GFP_KERNEL);
2780 if (!reply_buf)
2781 return ERR_PTR(-ENOMEM);
2782
2783 snap_id = cpu_to_le64(rbd_dev->header.snapc->snaps[which]);
2784 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2785 "rbd", "get_snapshot_name",
2786 (char *) &snap_id, sizeof (snap_id),
Alex Elder07b23912012-11-09 08:43:16 -06002787 reply_buf, size, NULL);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05002788 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2789 if (ret < 0)
2790 goto out;
2791
2792 p = reply_buf;
2793 end = (char *) reply_buf + size;
Alex Eldere5c35532012-10-25 23:34:41 -05002794 snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05002795 if (IS_ERR(snap_name)) {
2796 ret = PTR_ERR(snap_name);
2797 goto out;
2798 } else {
2799 dout(" snap_id 0x%016llx snap_name = %s\n",
2800 (unsigned long long) le64_to_cpu(snap_id), snap_name);
2801 }
2802 kfree(reply_buf);
2803
2804 return snap_name;
2805out:
2806 kfree(reply_buf);
2807
2808 return ERR_PTR(ret);
2809}
2810
2811static char *rbd_dev_v2_snap_info(struct rbd_device *rbd_dev, u32 which,
2812 u64 *snap_size, u64 *snap_features)
2813{
2814 __le64 snap_id;
2815 u8 order;
2816 int ret;
2817
2818 snap_id = rbd_dev->header.snapc->snaps[which];
2819 ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, &order, snap_size);
2820 if (ret)
2821 return ERR_PTR(ret);
2822 ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, snap_features);
2823 if (ret)
2824 return ERR_PTR(ret);
2825
2826 return rbd_dev_v2_snap_name(rbd_dev, which);
2827}
2828
2829static char *rbd_dev_snap_info(struct rbd_device *rbd_dev, u32 which,
2830 u64 *snap_size, u64 *snap_features)
2831{
2832 if (rbd_dev->image_format == 1)
2833 return rbd_dev_v1_snap_info(rbd_dev, which,
2834 snap_size, snap_features);
2835 if (rbd_dev->image_format == 2)
2836 return rbd_dev_v2_snap_info(rbd_dev, which,
2837 snap_size, snap_features);
2838 return ERR_PTR(-EINVAL);
2839}
2840
Alex Elder117973f2012-08-31 17:29:55 -05002841static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev, u64 *hver)
2842{
2843 int ret;
2844 __u8 obj_order;
2845
2846 down_write(&rbd_dev->header_rwsem);
2847
2848 /* Grab old order first, to see if it changes */
2849
2850 obj_order = rbd_dev->header.obj_order,
2851 ret = rbd_dev_v2_image_size(rbd_dev);
2852 if (ret)
2853 goto out;
2854 if (rbd_dev->header.obj_order != obj_order) {
2855 ret = -EIO;
2856 goto out;
2857 }
2858 rbd_update_mapping_size(rbd_dev);
2859
2860 ret = rbd_dev_v2_snap_context(rbd_dev, hver);
2861 dout("rbd_dev_v2_snap_context returned %d\n", ret);
2862 if (ret)
2863 goto out;
2864 ret = rbd_dev_snaps_update(rbd_dev);
2865 dout("rbd_dev_snaps_update returned %d\n", ret);
2866 if (ret)
2867 goto out;
2868 ret = rbd_dev_snaps_register(rbd_dev);
2869 dout("rbd_dev_snaps_register returned %d\n", ret);
2870out:
2871 up_write(&rbd_dev->header_rwsem);
2872
2873 return ret;
2874}
2875
Alex Elder9d475de2012-07-03 16:01:19 -05002876/*
Alex Elder35938152012-08-02 11:29:46 -05002877 * Scan the rbd device's current snapshot list and compare it to the
2878 * newly-received snapshot context. Remove any existing snapshots
2879 * not present in the new snapshot context. Add a new snapshot for
2880 * any snaphots in the snapshot context not in the current list.
2881 * And verify there are no changes to snapshots we already know
2882 * about.
2883 *
2884 * Assumes the snapshots in the snapshot context are sorted by
2885 * snapshot id, highest id first. (Snapshots in the rbd_dev's list
2886 * are also maintained in that order.)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002887 */
Alex Elder304f6802012-08-31 17:29:52 -05002888static int rbd_dev_snaps_update(struct rbd_device *rbd_dev)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002889{
Alex Elder35938152012-08-02 11:29:46 -05002890 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
2891 const u32 snap_count = snapc->num_snaps;
Alex Elder35938152012-08-02 11:29:46 -05002892 struct list_head *head = &rbd_dev->snaps;
2893 struct list_head *links = head->next;
2894 u32 index = 0;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002895
Alex Elder9fcbb802012-08-23 23:48:49 -05002896 dout("%s: snap count is %u\n", __func__, (unsigned int) snap_count);
Alex Elder35938152012-08-02 11:29:46 -05002897 while (index < snap_count || links != head) {
2898 u64 snap_id;
2899 struct rbd_snap *snap;
Alex Eldercd892122012-07-03 16:01:19 -05002900 char *snap_name;
2901 u64 snap_size = 0;
2902 u64 snap_features = 0;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002903
Alex Elder35938152012-08-02 11:29:46 -05002904 snap_id = index < snap_count ? snapc->snaps[index]
2905 : CEPH_NOSNAP;
2906 snap = links != head ? list_entry(links, struct rbd_snap, node)
2907 : NULL;
Alex Elderaafb2302012-09-06 16:00:54 -05002908 rbd_assert(!snap || snap->id != CEPH_NOSNAP);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002909
Alex Elder35938152012-08-02 11:29:46 -05002910 if (snap_id == CEPH_NOSNAP || (snap && snap->id > snap_id)) {
2911 struct list_head *next = links->next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002912
Alex Elder35938152012-08-02 11:29:46 -05002913 /* Existing snapshot not in the new snap context */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002914
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002915 if (rbd_dev->spec->snap_id == snap->id)
Alex Elderd78b6502012-11-09 08:43:15 -06002916 atomic_set(&rbd_dev->exists, 0);
Alex Elder41f38c22012-10-25 23:34:40 -05002917 rbd_remove_snap_dev(snap);
Alex Elder9fcbb802012-08-23 23:48:49 -05002918 dout("%ssnap id %llu has been removed\n",
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002919 rbd_dev->spec->snap_id == snap->id ?
2920 "mapped " : "",
Alex Elder9fcbb802012-08-23 23:48:49 -05002921 (unsigned long long) snap->id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002922
Alex Elder35938152012-08-02 11:29:46 -05002923 /* Done with this list entry; advance */
2924
2925 links = next;
2926 continue;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002927 }
Alex Elder35938152012-08-02 11:29:46 -05002928
Alex Elderb8b1e2d2012-07-03 16:01:19 -05002929 snap_name = rbd_dev_snap_info(rbd_dev, index,
2930 &snap_size, &snap_features);
Alex Eldercd892122012-07-03 16:01:19 -05002931 if (IS_ERR(snap_name))
2932 return PTR_ERR(snap_name);
2933
Alex Elder9fcbb802012-08-23 23:48:49 -05002934 dout("entry %u: snap_id = %llu\n", (unsigned int) snap_count,
2935 (unsigned long long) snap_id);
Alex Elder35938152012-08-02 11:29:46 -05002936 if (!snap || (snap_id != CEPH_NOSNAP && snap->id < snap_id)) {
2937 struct rbd_snap *new_snap;
2938
2939 /* We haven't seen this snapshot before */
2940
Alex Elderc8d18422012-07-10 20:30:11 -05002941 new_snap = __rbd_add_snap_dev(rbd_dev, snap_name,
Alex Eldercd892122012-07-03 16:01:19 -05002942 snap_id, snap_size, snap_features);
Alex Elder9fcbb802012-08-23 23:48:49 -05002943 if (IS_ERR(new_snap)) {
2944 int err = PTR_ERR(new_snap);
2945
2946 dout(" failed to add dev, error %d\n", err);
2947
2948 return err;
2949 }
Alex Elder35938152012-08-02 11:29:46 -05002950
2951 /* New goes before existing, or at end of list */
2952
Alex Elder9fcbb802012-08-23 23:48:49 -05002953 dout(" added dev%s\n", snap ? "" : " at end\n");
Alex Elder35938152012-08-02 11:29:46 -05002954 if (snap)
2955 list_add_tail(&new_snap->node, &snap->node);
2956 else
Alex Elder523f3252012-08-30 00:16:37 -05002957 list_add_tail(&new_snap->node, head);
Alex Elder35938152012-08-02 11:29:46 -05002958 } else {
2959 /* Already have this one */
2960
Alex Elder9fcbb802012-08-23 23:48:49 -05002961 dout(" already present\n");
2962
Alex Eldercd892122012-07-03 16:01:19 -05002963 rbd_assert(snap->size == snap_size);
Alex Elderaafb2302012-09-06 16:00:54 -05002964 rbd_assert(!strcmp(snap->name, snap_name));
Alex Eldercd892122012-07-03 16:01:19 -05002965 rbd_assert(snap->features == snap_features);
Alex Elder35938152012-08-02 11:29:46 -05002966
2967 /* Done with this list entry; advance */
2968
2969 links = links->next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002970 }
Alex Elder35938152012-08-02 11:29:46 -05002971
2972 /* Advance to the next entry in the snapshot context */
2973
2974 index++;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002975 }
Alex Elder9fcbb802012-08-23 23:48:49 -05002976 dout("%s: done\n", __func__);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002977
2978 return 0;
2979}
2980
Alex Elder304f6802012-08-31 17:29:52 -05002981/*
2982 * Scan the list of snapshots and register the devices for any that
2983 * have not already been registered.
2984 */
2985static int rbd_dev_snaps_register(struct rbd_device *rbd_dev)
2986{
2987 struct rbd_snap *snap;
2988 int ret = 0;
2989
2990 dout("%s called\n", __func__);
Alex Elder86ff77b2012-08-31 17:29:53 -05002991 if (WARN_ON(!device_is_registered(&rbd_dev->dev)))
2992 return -EIO;
Alex Elder304f6802012-08-31 17:29:52 -05002993
2994 list_for_each_entry(snap, &rbd_dev->snaps, node) {
2995 if (!rbd_snap_registered(snap)) {
2996 ret = rbd_register_snap_dev(snap, &rbd_dev->dev);
2997 if (ret < 0)
2998 break;
2999 }
3000 }
3001 dout("%s: returning %d\n", __func__, ret);
3002
3003 return ret;
3004}
3005
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003006static int rbd_bus_add_dev(struct rbd_device *rbd_dev)
3007{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003008 struct device *dev;
Alex Eldercd789ab2012-08-30 00:16:38 -05003009 int ret;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003010
3011 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003012
Alex Eldercd789ab2012-08-30 00:16:38 -05003013 dev = &rbd_dev->dev;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003014 dev->bus = &rbd_bus_type;
3015 dev->type = &rbd_device_type;
3016 dev->parent = &rbd_root_dev;
3017 dev->release = rbd_dev_release;
Alex Elderde71a292012-07-03 16:01:19 -05003018 dev_set_name(dev, "%d", rbd_dev->dev_id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003019 ret = device_register(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003020
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003021 mutex_unlock(&ctl_mutex);
Alex Eldercd789ab2012-08-30 00:16:38 -05003022
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003023 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003024}
3025
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003026static void rbd_bus_del_dev(struct rbd_device *rbd_dev)
3027{
3028 device_unregister(&rbd_dev->dev);
3029}
3030
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003031static int rbd_init_watch_dev(struct rbd_device *rbd_dev)
3032{
3033 int ret, rc;
3034
3035 do {
Alex Elder0e6f3222012-07-25 09:32:40 -05003036 ret = rbd_req_sync_watch(rbd_dev);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003037 if (ret == -ERANGE) {
Alex Elder117973f2012-08-31 17:29:55 -05003038 rc = rbd_dev_refresh(rbd_dev, NULL);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003039 if (rc < 0)
3040 return rc;
3041 }
3042 } while (ret == -ERANGE);
3043
3044 return ret;
3045}
3046
Alex Eldere2839302012-08-29 17:11:06 -05003047static atomic64_t rbd_dev_id_max = ATOMIC64_INIT(0);
Alex Elder1ddbe942012-01-29 13:57:44 -06003048
3049/*
Alex Elder499afd52012-02-02 08:13:29 -06003050 * Get a unique rbd identifier for the given new rbd_dev, and add
3051 * the rbd_dev to the global list. The minimum rbd id is 1.
Alex Elder1ddbe942012-01-29 13:57:44 -06003052 */
Alex Eldere2839302012-08-29 17:11:06 -05003053static void rbd_dev_id_get(struct rbd_device *rbd_dev)
Alex Elderb7f23c32012-01-29 13:57:43 -06003054{
Alex Eldere2839302012-08-29 17:11:06 -05003055 rbd_dev->dev_id = atomic64_inc_return(&rbd_dev_id_max);
Alex Elder499afd52012-02-02 08:13:29 -06003056
3057 spin_lock(&rbd_dev_list_lock);
3058 list_add_tail(&rbd_dev->node, &rbd_dev_list);
3059 spin_unlock(&rbd_dev_list_lock);
Alex Eldere2839302012-08-29 17:11:06 -05003060 dout("rbd_dev %p given dev id %llu\n", rbd_dev,
3061 (unsigned long long) rbd_dev->dev_id);
Alex Elder1ddbe942012-01-29 13:57:44 -06003062}
Alex Elderb7f23c32012-01-29 13:57:43 -06003063
Alex Elder1ddbe942012-01-29 13:57:44 -06003064/*
Alex Elder499afd52012-02-02 08:13:29 -06003065 * Remove an rbd_dev from the global list, and record that its
3066 * identifier is no longer in use.
Alex Elder1ddbe942012-01-29 13:57:44 -06003067 */
Alex Eldere2839302012-08-29 17:11:06 -05003068static void rbd_dev_id_put(struct rbd_device *rbd_dev)
Alex Elder1ddbe942012-01-29 13:57:44 -06003069{
Alex Elderd184f6b2012-01-29 13:57:44 -06003070 struct list_head *tmp;
Alex Elderde71a292012-07-03 16:01:19 -05003071 int rbd_id = rbd_dev->dev_id;
Alex Elderd184f6b2012-01-29 13:57:44 -06003072 int max_id;
3073
Alex Elderaafb2302012-09-06 16:00:54 -05003074 rbd_assert(rbd_id > 0);
Alex Elder499afd52012-02-02 08:13:29 -06003075
Alex Eldere2839302012-08-29 17:11:06 -05003076 dout("rbd_dev %p released dev id %llu\n", rbd_dev,
3077 (unsigned long long) rbd_dev->dev_id);
Alex Elder499afd52012-02-02 08:13:29 -06003078 spin_lock(&rbd_dev_list_lock);
3079 list_del_init(&rbd_dev->node);
Alex Elderd184f6b2012-01-29 13:57:44 -06003080
3081 /*
3082 * If the id being "put" is not the current maximum, there
3083 * is nothing special we need to do.
3084 */
Alex Eldere2839302012-08-29 17:11:06 -05003085 if (rbd_id != atomic64_read(&rbd_dev_id_max)) {
Alex Elderd184f6b2012-01-29 13:57:44 -06003086 spin_unlock(&rbd_dev_list_lock);
3087 return;
3088 }
3089
3090 /*
3091 * We need to update the current maximum id. Search the
3092 * list to find out what it is. We're more likely to find
3093 * the maximum at the end, so search the list backward.
3094 */
3095 max_id = 0;
3096 list_for_each_prev(tmp, &rbd_dev_list) {
3097 struct rbd_device *rbd_dev;
3098
3099 rbd_dev = list_entry(tmp, struct rbd_device, node);
Alex Elderb213e0b2012-10-10 21:19:13 -07003100 if (rbd_dev->dev_id > max_id)
3101 max_id = rbd_dev->dev_id;
Alex Elderd184f6b2012-01-29 13:57:44 -06003102 }
Alex Elder499afd52012-02-02 08:13:29 -06003103 spin_unlock(&rbd_dev_list_lock);
Alex Elderb7f23c32012-01-29 13:57:43 -06003104
Alex Elder1ddbe942012-01-29 13:57:44 -06003105 /*
Alex Eldere2839302012-08-29 17:11:06 -05003106 * The max id could have been updated by rbd_dev_id_get(), in
Alex Elderd184f6b2012-01-29 13:57:44 -06003107 * which case it now accurately reflects the new maximum.
3108 * Be careful not to overwrite the maximum value in that
3109 * case.
Alex Elder1ddbe942012-01-29 13:57:44 -06003110 */
Alex Eldere2839302012-08-29 17:11:06 -05003111 atomic64_cmpxchg(&rbd_dev_id_max, rbd_id, max_id);
3112 dout(" max dev id has been reset\n");
Alex Elderb7f23c32012-01-29 13:57:43 -06003113}
3114
Alex Eldera725f65e2012-02-02 08:13:30 -06003115/*
Alex Eldere28fff262012-02-02 08:13:30 -06003116 * Skips over white space at *buf, and updates *buf to point to the
3117 * first found non-space character (if any). Returns the length of
Alex Elder593a9e72012-02-07 12:03:37 -06003118 * the token (string of non-white space characters) found. Note
3119 * that *buf must be terminated with '\0'.
Alex Eldere28fff262012-02-02 08:13:30 -06003120 */
3121static inline size_t next_token(const char **buf)
3122{
3123 /*
3124 * These are the characters that produce nonzero for
3125 * isspace() in the "C" and "POSIX" locales.
3126 */
3127 const char *spaces = " \f\n\r\t\v";
3128
3129 *buf += strspn(*buf, spaces); /* Find start of token */
3130
3131 return strcspn(*buf, spaces); /* Return token length */
3132}
3133
3134/*
3135 * Finds the next token in *buf, and if the provided token buffer is
3136 * big enough, copies the found token into it. The result, if
Alex Elder593a9e72012-02-07 12:03:37 -06003137 * copied, is guaranteed to be terminated with '\0'. Note that *buf
3138 * must be terminated with '\0' on entry.
Alex Eldere28fff262012-02-02 08:13:30 -06003139 *
3140 * Returns the length of the token found (not including the '\0').
3141 * Return value will be 0 if no token is found, and it will be >=
3142 * token_size if the token would not fit.
3143 *
Alex Elder593a9e72012-02-07 12:03:37 -06003144 * The *buf pointer will be updated to point beyond the end of the
Alex Eldere28fff262012-02-02 08:13:30 -06003145 * found token. Note that this occurs even if the token buffer is
3146 * too small to hold it.
3147 */
3148static inline size_t copy_token(const char **buf,
3149 char *token,
3150 size_t token_size)
3151{
3152 size_t len;
3153
3154 len = next_token(buf);
3155 if (len < token_size) {
3156 memcpy(token, *buf, len);
3157 *(token + len) = '\0';
3158 }
3159 *buf += len;
3160
3161 return len;
3162}
3163
3164/*
Alex Elderea3352f2012-07-09 21:04:23 -05003165 * Finds the next token in *buf, dynamically allocates a buffer big
3166 * enough to hold a copy of it, and copies the token into the new
3167 * buffer. The copy is guaranteed to be terminated with '\0'. Note
3168 * that a duplicate buffer is created even for a zero-length token.
3169 *
3170 * Returns a pointer to the newly-allocated duplicate, or a null
3171 * pointer if memory for the duplicate was not available. If
3172 * the lenp argument is a non-null pointer, the length of the token
3173 * (not including the '\0') is returned in *lenp.
3174 *
3175 * If successful, the *buf pointer will be updated to point beyond
3176 * the end of the found token.
3177 *
3178 * Note: uses GFP_KERNEL for allocation.
3179 */
3180static inline char *dup_token(const char **buf, size_t *lenp)
3181{
3182 char *dup;
3183 size_t len;
3184
3185 len = next_token(buf);
Alex Elder4caf35f2012-11-01 08:39:27 -05003186 dup = kmemdup(*buf, len + 1, GFP_KERNEL);
Alex Elderea3352f2012-07-09 21:04:23 -05003187 if (!dup)
3188 return NULL;
Alex Elderea3352f2012-07-09 21:04:23 -05003189 *(dup + len) = '\0';
3190 *buf += len;
3191
3192 if (lenp)
3193 *lenp = len;
3194
3195 return dup;
3196}
3197
3198/*
Alex Elder859c31d2012-10-25 23:34:42 -05003199 * Parse the options provided for an "rbd add" (i.e., rbd image
3200 * mapping) request. These arrive via a write to /sys/bus/rbd/add,
3201 * and the data written is passed here via a NUL-terminated buffer.
3202 * Returns 0 if successful or an error code otherwise.
Alex Elderd22f76e2012-07-12 10:46:35 -05003203 *
Alex Elder859c31d2012-10-25 23:34:42 -05003204 * The information extracted from these options is recorded in
3205 * the other parameters which return dynamically-allocated
3206 * structures:
3207 * ceph_opts
3208 * The address of a pointer that will refer to a ceph options
3209 * structure. Caller must release the returned pointer using
3210 * ceph_destroy_options() when it is no longer needed.
3211 * rbd_opts
3212 * Address of an rbd options pointer. Fully initialized by
3213 * this function; caller must release with kfree().
3214 * spec
3215 * Address of an rbd image specification pointer. Fully
3216 * initialized by this function based on parsed options.
3217 * Caller must release with rbd_spec_put().
3218 *
3219 * The options passed take this form:
3220 * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
3221 * where:
3222 * <mon_addrs>
3223 * A comma-separated list of one or more monitor addresses.
3224 * A monitor address is an ip address, optionally followed
3225 * by a port number (separated by a colon).
3226 * I.e.: ip1[:port1][,ip2[:port2]...]
3227 * <options>
3228 * A comma-separated list of ceph and/or rbd options.
3229 * <pool_name>
3230 * The name of the rados pool containing the rbd image.
3231 * <image_name>
3232 * The name of the image in that pool to map.
3233 * <snap_id>
3234 * An optional snapshot id. If provided, the mapping will
3235 * present data from the image at the time that snapshot was
3236 * created. The image head is used if no snapshot id is
3237 * provided. Snapshot mappings are always read-only.
Alex Eldera725f65e2012-02-02 08:13:30 -06003238 */
Alex Elder859c31d2012-10-25 23:34:42 -05003239static int rbd_add_parse_args(const char *buf,
Alex Elderdc79b112012-10-25 23:34:41 -05003240 struct ceph_options **ceph_opts,
Alex Elder859c31d2012-10-25 23:34:42 -05003241 struct rbd_options **opts,
3242 struct rbd_spec **rbd_spec)
Alex Eldera725f65e2012-02-02 08:13:30 -06003243{
Alex Elderd22f76e2012-07-12 10:46:35 -05003244 size_t len;
Alex Elder859c31d2012-10-25 23:34:42 -05003245 char *options;
Alex Elder0ddebc02012-10-25 23:34:41 -05003246 const char *mon_addrs;
3247 size_t mon_addrs_size;
Alex Elder859c31d2012-10-25 23:34:42 -05003248 struct rbd_spec *spec = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05003249 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05003250 struct ceph_options *copts;
Alex Elderdc79b112012-10-25 23:34:41 -05003251 int ret;
Alex Eldere28fff262012-02-02 08:13:30 -06003252
3253 /* The first four tokens are required */
3254
Alex Elder7ef32142012-02-02 08:13:30 -06003255 len = next_token(&buf);
Alex Elder4fb5d6712012-11-01 10:17:15 -05003256 if (!len) {
3257 rbd_warn(NULL, "no monitor address(es) provided");
3258 return -EINVAL;
3259 }
Alex Elder0ddebc02012-10-25 23:34:41 -05003260 mon_addrs = buf;
Alex Elderf28e5652012-10-25 23:34:41 -05003261 mon_addrs_size = len + 1;
Alex Elder7ef32142012-02-02 08:13:30 -06003262 buf += len;
Alex Eldera725f65e2012-02-02 08:13:30 -06003263
Alex Elderdc79b112012-10-25 23:34:41 -05003264 ret = -EINVAL;
Alex Elderf28e5652012-10-25 23:34:41 -05003265 options = dup_token(&buf, NULL);
3266 if (!options)
Alex Elderdc79b112012-10-25 23:34:41 -05003267 return -ENOMEM;
Alex Elder4fb5d6712012-11-01 10:17:15 -05003268 if (!*options) {
3269 rbd_warn(NULL, "no options provided");
3270 goto out_err;
3271 }
Alex Eldera725f65e2012-02-02 08:13:30 -06003272
Alex Elder859c31d2012-10-25 23:34:42 -05003273 spec = rbd_spec_alloc();
3274 if (!spec)
Alex Elderf28e5652012-10-25 23:34:41 -05003275 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05003276
3277 spec->pool_name = dup_token(&buf, NULL);
3278 if (!spec->pool_name)
3279 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05003280 if (!*spec->pool_name) {
3281 rbd_warn(NULL, "no pool name provided");
3282 goto out_err;
3283 }
Alex Eldere28fff262012-02-02 08:13:30 -06003284
Alex Elder69e7a022012-11-01 08:39:26 -05003285 spec->image_name = dup_token(&buf, NULL);
Alex Elder859c31d2012-10-25 23:34:42 -05003286 if (!spec->image_name)
Alex Elderf28e5652012-10-25 23:34:41 -05003287 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05003288 if (!*spec->image_name) {
3289 rbd_warn(NULL, "no image name provided");
3290 goto out_err;
3291 }
Alex Eldere28fff262012-02-02 08:13:30 -06003292
Alex Elderf28e5652012-10-25 23:34:41 -05003293 /*
3294 * Snapshot name is optional; default is to use "-"
3295 * (indicating the head/no snapshot).
3296 */
Alex Elder3feeb8942012-08-31 17:29:52 -05003297 len = next_token(&buf);
Alex Elder820a5f32012-07-09 21:04:24 -05003298 if (!len) {
Alex Elder3feeb8942012-08-31 17:29:52 -05003299 buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
3300 len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
Alex Elderf28e5652012-10-25 23:34:41 -05003301 } else if (len > RBD_MAX_SNAP_NAME_LEN) {
Alex Elderdc79b112012-10-25 23:34:41 -05003302 ret = -ENAMETOOLONG;
Alex Elderf28e5652012-10-25 23:34:41 -05003303 goto out_err;
Alex Elder849b4262012-07-09 21:04:24 -05003304 }
Alex Elder4caf35f2012-11-01 08:39:27 -05003305 spec->snap_name = kmemdup(buf, len + 1, GFP_KERNEL);
Alex Elder859c31d2012-10-25 23:34:42 -05003306 if (!spec->snap_name)
Alex Elderf28e5652012-10-25 23:34:41 -05003307 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05003308 *(spec->snap_name + len) = '\0';
Alex Eldere5c35532012-10-25 23:34:41 -05003309
Alex Elder0ddebc02012-10-25 23:34:41 -05003310 /* Initialize all rbd options to the defaults */
Alex Eldere28fff262012-02-02 08:13:30 -06003311
Alex Elder4e9afeb2012-10-25 23:34:41 -05003312 rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL);
3313 if (!rbd_opts)
3314 goto out_mem;
3315
3316 rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
Alex Elderd22f76e2012-07-12 10:46:35 -05003317
Alex Elder859c31d2012-10-25 23:34:42 -05003318 copts = ceph_parse_options(options, mon_addrs,
Alex Elder0ddebc02012-10-25 23:34:41 -05003319 mon_addrs + mon_addrs_size - 1,
Alex Elder4e9afeb2012-10-25 23:34:41 -05003320 parse_rbd_opts_token, rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05003321 if (IS_ERR(copts)) {
3322 ret = PTR_ERR(copts);
Alex Elderdc79b112012-10-25 23:34:41 -05003323 goto out_err;
3324 }
Alex Elder859c31d2012-10-25 23:34:42 -05003325 kfree(options);
3326
3327 *ceph_opts = copts;
Alex Elder4e9afeb2012-10-25 23:34:41 -05003328 *opts = rbd_opts;
Alex Elder859c31d2012-10-25 23:34:42 -05003329 *rbd_spec = spec;
Alex Elder0ddebc02012-10-25 23:34:41 -05003330
Alex Elderdc79b112012-10-25 23:34:41 -05003331 return 0;
Alex Elderf28e5652012-10-25 23:34:41 -05003332out_mem:
Alex Elderdc79b112012-10-25 23:34:41 -05003333 ret = -ENOMEM;
Alex Elderd22f76e2012-07-12 10:46:35 -05003334out_err:
Alex Elder859c31d2012-10-25 23:34:42 -05003335 kfree(rbd_opts);
3336 rbd_spec_put(spec);
Alex Elderf28e5652012-10-25 23:34:41 -05003337 kfree(options);
Alex Elderd22f76e2012-07-12 10:46:35 -05003338
Alex Elderdc79b112012-10-25 23:34:41 -05003339 return ret;
Alex Eldera725f65e2012-02-02 08:13:30 -06003340}
3341
Alex Elder589d30e2012-07-10 20:30:11 -05003342/*
3343 * An rbd format 2 image has a unique identifier, distinct from the
3344 * name given to it by the user. Internally, that identifier is
3345 * what's used to specify the names of objects related to the image.
3346 *
3347 * A special "rbd id" object is used to map an rbd image name to its
3348 * id. If that object doesn't exist, then there is no v2 rbd image
3349 * with the supplied name.
3350 *
3351 * This function will record the given rbd_dev's image_id field if
3352 * it can be determined, and in that case will return 0. If any
3353 * errors occur a negative errno will be returned and the rbd_dev's
3354 * image_id field will be unchanged (and should be NULL).
3355 */
3356static int rbd_dev_image_id(struct rbd_device *rbd_dev)
3357{
3358 int ret;
3359 size_t size;
3360 char *object_name;
3361 void *response;
3362 void *p;
3363
3364 /*
Alex Elder2c0d0a12012-10-30 19:40:33 -05003365 * When probing a parent image, the image id is already
3366 * known (and the image name likely is not). There's no
3367 * need to fetch the image id again in this case.
3368 */
3369 if (rbd_dev->spec->image_id)
3370 return 0;
3371
3372 /*
Alex Elder589d30e2012-07-10 20:30:11 -05003373 * First, see if the format 2 image id file exists, and if
3374 * so, get the image's persistent id from it.
3375 */
Alex Elder69e7a022012-11-01 08:39:26 -05003376 size = sizeof (RBD_ID_PREFIX) + strlen(rbd_dev->spec->image_name);
Alex Elder589d30e2012-07-10 20:30:11 -05003377 object_name = kmalloc(size, GFP_NOIO);
3378 if (!object_name)
3379 return -ENOMEM;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003380 sprintf(object_name, "%s%s", RBD_ID_PREFIX, rbd_dev->spec->image_name);
Alex Elder589d30e2012-07-10 20:30:11 -05003381 dout("rbd id object name is %s\n", object_name);
3382
3383 /* Response will be an encoded string, which includes a length */
3384
3385 size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
3386 response = kzalloc(size, GFP_NOIO);
3387 if (!response) {
3388 ret = -ENOMEM;
3389 goto out;
3390 }
3391
3392 ret = rbd_req_sync_exec(rbd_dev, object_name,
3393 "rbd", "get_id",
3394 NULL, 0,
Alex Elder07b23912012-11-09 08:43:16 -06003395 response, RBD_IMAGE_ID_LEN_MAX, NULL);
Alex Elder589d30e2012-07-10 20:30:11 -05003396 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
3397 if (ret < 0)
3398 goto out;
Alex Eldera0ea3a42012-10-10 21:19:13 -07003399 ret = 0; /* rbd_req_sync_exec() can return positive */
Alex Elder589d30e2012-07-10 20:30:11 -05003400
3401 p = response;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003402 rbd_dev->spec->image_id = ceph_extract_encoded_string(&p,
Alex Elder589d30e2012-07-10 20:30:11 -05003403 p + RBD_IMAGE_ID_LEN_MAX,
Alex Elder979ed482012-11-01 08:39:26 -05003404 NULL, GFP_NOIO);
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003405 if (IS_ERR(rbd_dev->spec->image_id)) {
3406 ret = PTR_ERR(rbd_dev->spec->image_id);
3407 rbd_dev->spec->image_id = NULL;
Alex Elder589d30e2012-07-10 20:30:11 -05003408 } else {
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003409 dout("image_id is %s\n", rbd_dev->spec->image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05003410 }
3411out:
3412 kfree(response);
3413 kfree(object_name);
3414
3415 return ret;
3416}
3417
Alex Eldera30b71b2012-07-10 20:30:11 -05003418static int rbd_dev_v1_probe(struct rbd_device *rbd_dev)
3419{
3420 int ret;
3421 size_t size;
3422
3423 /* Version 1 images have no id; empty string is used */
3424
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003425 rbd_dev->spec->image_id = kstrdup("", GFP_KERNEL);
3426 if (!rbd_dev->spec->image_id)
Alex Eldera30b71b2012-07-10 20:30:11 -05003427 return -ENOMEM;
Alex Eldera30b71b2012-07-10 20:30:11 -05003428
3429 /* Record the header object name for this rbd image. */
3430
Alex Elder69e7a022012-11-01 08:39:26 -05003431 size = strlen(rbd_dev->spec->image_name) + sizeof (RBD_SUFFIX);
Alex Eldera30b71b2012-07-10 20:30:11 -05003432 rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
3433 if (!rbd_dev->header_name) {
3434 ret = -ENOMEM;
3435 goto out_err;
3436 }
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003437 sprintf(rbd_dev->header_name, "%s%s",
3438 rbd_dev->spec->image_name, RBD_SUFFIX);
Alex Eldera30b71b2012-07-10 20:30:11 -05003439
3440 /* Populate rbd image metadata */
3441
3442 ret = rbd_read_header(rbd_dev, &rbd_dev->header);
3443 if (ret < 0)
3444 goto out_err;
Alex Elder86b00e02012-10-25 23:34:42 -05003445
3446 /* Version 1 images have no parent (no layering) */
3447
3448 rbd_dev->parent_spec = NULL;
3449 rbd_dev->parent_overlap = 0;
3450
Alex Eldera30b71b2012-07-10 20:30:11 -05003451 rbd_dev->image_format = 1;
3452
3453 dout("discovered version 1 image, header name is %s\n",
3454 rbd_dev->header_name);
3455
3456 return 0;
3457
3458out_err:
3459 kfree(rbd_dev->header_name);
3460 rbd_dev->header_name = NULL;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003461 kfree(rbd_dev->spec->image_id);
3462 rbd_dev->spec->image_id = NULL;
Alex Eldera30b71b2012-07-10 20:30:11 -05003463
3464 return ret;
3465}
3466
3467static int rbd_dev_v2_probe(struct rbd_device *rbd_dev)
3468{
3469 size_t size;
Alex Elder9d475de2012-07-03 16:01:19 -05003470 int ret;
Alex Elder6e14b1a2012-07-03 16:01:19 -05003471 u64 ver = 0;
Alex Eldera30b71b2012-07-10 20:30:11 -05003472
3473 /*
3474 * Image id was filled in by the caller. Record the header
3475 * object name for this rbd image.
3476 */
Alex Elder979ed482012-11-01 08:39:26 -05003477 size = sizeof (RBD_HEADER_PREFIX) + strlen(rbd_dev->spec->image_id);
Alex Eldera30b71b2012-07-10 20:30:11 -05003478 rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
3479 if (!rbd_dev->header_name)
3480 return -ENOMEM;
3481 sprintf(rbd_dev->header_name, "%s%s",
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003482 RBD_HEADER_PREFIX, rbd_dev->spec->image_id);
Alex Elder9d475de2012-07-03 16:01:19 -05003483
3484 /* Get the size and object order for the image */
3485
3486 ret = rbd_dev_v2_image_size(rbd_dev);
3487 if (ret < 0)
3488 goto out_err;
Alex Elder1e130192012-07-03 16:01:19 -05003489
3490 /* Get the object prefix (a.k.a. block_name) for the image */
3491
3492 ret = rbd_dev_v2_object_prefix(rbd_dev);
3493 if (ret < 0)
3494 goto out_err;
Alex Elderb1b54022012-07-03 16:01:19 -05003495
Alex Elderd8891402012-10-09 13:50:17 -07003496 /* Get the and check features for the image */
Alex Elderb1b54022012-07-03 16:01:19 -05003497
3498 ret = rbd_dev_v2_features(rbd_dev);
3499 if (ret < 0)
3500 goto out_err;
Alex Elder35d489f2012-07-03 16:01:19 -05003501
Alex Elder86b00e02012-10-25 23:34:42 -05003502 /* If the image supports layering, get the parent info */
3503
3504 if (rbd_dev->header.features & RBD_FEATURE_LAYERING) {
3505 ret = rbd_dev_v2_parent_info(rbd_dev);
3506 if (ret < 0)
3507 goto out_err;
3508 }
3509
Alex Elder6e14b1a2012-07-03 16:01:19 -05003510 /* crypto and compression type aren't (yet) supported for v2 images */
Alex Elder35d489f2012-07-03 16:01:19 -05003511
Alex Elder6e14b1a2012-07-03 16:01:19 -05003512 rbd_dev->header.crypt_type = 0;
3513 rbd_dev->header.comp_type = 0;
3514
3515 /* Get the snapshot context, plus the header version */
3516
3517 ret = rbd_dev_v2_snap_context(rbd_dev, &ver);
Alex Elder35d489f2012-07-03 16:01:19 -05003518 if (ret)
3519 goto out_err;
Alex Elder6e14b1a2012-07-03 16:01:19 -05003520 rbd_dev->header.obj_version = ver;
3521
Alex Eldera30b71b2012-07-10 20:30:11 -05003522 rbd_dev->image_format = 2;
3523
3524 dout("discovered version 2 image, header name is %s\n",
3525 rbd_dev->header_name);
3526
Alex Elder35152972012-08-31 17:29:55 -05003527 return 0;
Alex Elder9d475de2012-07-03 16:01:19 -05003528out_err:
Alex Elder86b00e02012-10-25 23:34:42 -05003529 rbd_dev->parent_overlap = 0;
3530 rbd_spec_put(rbd_dev->parent_spec);
3531 rbd_dev->parent_spec = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05003532 kfree(rbd_dev->header_name);
3533 rbd_dev->header_name = NULL;
Alex Elder1e130192012-07-03 16:01:19 -05003534 kfree(rbd_dev->header.object_prefix);
3535 rbd_dev->header.object_prefix = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05003536
3537 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05003538}
3539
Alex Elder83a06262012-10-30 15:47:17 -05003540static int rbd_dev_probe_finish(struct rbd_device *rbd_dev)
3541{
3542 int ret;
3543
3544 /* no need to lock here, as rbd_dev is not registered yet */
3545 ret = rbd_dev_snaps_update(rbd_dev);
3546 if (ret)
3547 return ret;
3548
Alex Elder9e15b772012-10-30 19:40:33 -05003549 ret = rbd_dev_probe_update_spec(rbd_dev);
3550 if (ret)
3551 goto err_out_snaps;
3552
Alex Elder83a06262012-10-30 15:47:17 -05003553 ret = rbd_dev_set_mapping(rbd_dev);
3554 if (ret)
3555 goto err_out_snaps;
3556
3557 /* generate unique id: find highest unique id, add one */
3558 rbd_dev_id_get(rbd_dev);
3559
3560 /* Fill in the device name, now that we have its id. */
3561 BUILD_BUG_ON(DEV_NAME_LEN
3562 < sizeof (RBD_DRV_NAME) + MAX_INT_FORMAT_WIDTH);
3563 sprintf(rbd_dev->name, "%s%d", RBD_DRV_NAME, rbd_dev->dev_id);
3564
3565 /* Get our block major device number. */
3566
3567 ret = register_blkdev(0, rbd_dev->name);
3568 if (ret < 0)
3569 goto err_out_id;
3570 rbd_dev->major = ret;
3571
3572 /* Set up the blkdev mapping. */
3573
3574 ret = rbd_init_disk(rbd_dev);
3575 if (ret)
3576 goto err_out_blkdev;
3577
3578 ret = rbd_bus_add_dev(rbd_dev);
3579 if (ret)
3580 goto err_out_disk;
3581
3582 /*
3583 * At this point cleanup in the event of an error is the job
3584 * of the sysfs code (initiated by rbd_bus_del_dev()).
3585 */
3586 down_write(&rbd_dev->header_rwsem);
3587 ret = rbd_dev_snaps_register(rbd_dev);
3588 up_write(&rbd_dev->header_rwsem);
3589 if (ret)
3590 goto err_out_bus;
3591
3592 ret = rbd_init_watch_dev(rbd_dev);
3593 if (ret)
3594 goto err_out_bus;
3595
3596 /* Everything's ready. Announce the disk to the world. */
3597
3598 add_disk(rbd_dev->disk);
3599
3600 pr_info("%s: added with size 0x%llx\n", rbd_dev->disk->disk_name,
3601 (unsigned long long) rbd_dev->mapping.size);
3602
3603 return ret;
3604err_out_bus:
3605 /* this will also clean up rest of rbd_dev stuff */
3606
3607 rbd_bus_del_dev(rbd_dev);
3608
3609 return ret;
3610err_out_disk:
3611 rbd_free_disk(rbd_dev);
3612err_out_blkdev:
3613 unregister_blkdev(rbd_dev->major, rbd_dev->name);
3614err_out_id:
3615 rbd_dev_id_put(rbd_dev);
3616err_out_snaps:
3617 rbd_remove_all_snaps(rbd_dev);
3618
3619 return ret;
3620}
3621
Alex Eldera30b71b2012-07-10 20:30:11 -05003622/*
3623 * Probe for the existence of the header object for the given rbd
3624 * device. For format 2 images this includes determining the image
3625 * id.
3626 */
3627static int rbd_dev_probe(struct rbd_device *rbd_dev)
3628{
3629 int ret;
3630
3631 /*
3632 * Get the id from the image id object. If it's not a
3633 * format 2 image, we'll get ENOENT back, and we'll assume
3634 * it's a format 1 image.
3635 */
3636 ret = rbd_dev_image_id(rbd_dev);
3637 if (ret)
3638 ret = rbd_dev_v1_probe(rbd_dev);
3639 else
3640 ret = rbd_dev_v2_probe(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05003641 if (ret) {
Alex Eldera30b71b2012-07-10 20:30:11 -05003642 dout("probe failed, returning %d\n", ret);
3643
Alex Elder83a06262012-10-30 15:47:17 -05003644 return ret;
3645 }
3646
3647 ret = rbd_dev_probe_finish(rbd_dev);
3648 if (ret)
3649 rbd_header_free(&rbd_dev->header);
3650
Alex Eldera30b71b2012-07-10 20:30:11 -05003651 return ret;
3652}
3653
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003654static ssize_t rbd_add(struct bus_type *bus,
3655 const char *buf,
3656 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003657{
Alex Eldercb8627c2012-07-09 21:04:23 -05003658 struct rbd_device *rbd_dev = NULL;
Alex Elderdc79b112012-10-25 23:34:41 -05003659 struct ceph_options *ceph_opts = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05003660 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05003661 struct rbd_spec *spec = NULL;
Alex Elder9d3997f2012-10-25 23:34:42 -05003662 struct rbd_client *rbdc;
Alex Elder27cc2592012-02-02 08:13:30 -06003663 struct ceph_osd_client *osdc;
3664 int rc = -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003665
3666 if (!try_module_get(THIS_MODULE))
3667 return -ENODEV;
3668
Alex Eldera725f65e2012-02-02 08:13:30 -06003669 /* parse add command */
Alex Elder859c31d2012-10-25 23:34:42 -05003670 rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec);
Alex Elderdc79b112012-10-25 23:34:41 -05003671 if (rc < 0)
Alex Elderbd4ba652012-10-25 23:34:42 -05003672 goto err_out_module;
Alex Eldera725f65e2012-02-02 08:13:30 -06003673
Alex Elder9d3997f2012-10-25 23:34:42 -05003674 rbdc = rbd_get_client(ceph_opts);
3675 if (IS_ERR(rbdc)) {
3676 rc = PTR_ERR(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05003677 goto err_out_args;
Alex Elder9d3997f2012-10-25 23:34:42 -05003678 }
Alex Elderc53d5892012-10-25 23:34:42 -05003679 ceph_opts = NULL; /* rbd_dev client now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003680
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003681 /* pick the pool */
Alex Elder9d3997f2012-10-25 23:34:42 -05003682 osdc = &rbdc->client->osdc;
Alex Elder859c31d2012-10-25 23:34:42 -05003683 rc = ceph_pg_poolid_by_name(osdc->osdmap, spec->pool_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003684 if (rc < 0)
3685 goto err_out_client;
Alex Elder859c31d2012-10-25 23:34:42 -05003686 spec->pool_id = (u64) rc;
3687
Alex Elder0903e872012-11-14 12:25:19 -06003688 /* The ceph file layout needs to fit pool id in 32 bits */
3689
3690 if (WARN_ON(spec->pool_id > (u64) U32_MAX)) {
3691 rc = -EIO;
3692 goto err_out_client;
3693 }
3694
Alex Elderc53d5892012-10-25 23:34:42 -05003695 rbd_dev = rbd_dev_create(rbdc, spec);
Alex Elderbd4ba652012-10-25 23:34:42 -05003696 if (!rbd_dev)
3697 goto err_out_client;
Alex Elderc53d5892012-10-25 23:34:42 -05003698 rbdc = NULL; /* rbd_dev now owns this */
3699 spec = NULL; /* rbd_dev now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003700
Alex Elderbd4ba652012-10-25 23:34:42 -05003701 rbd_dev->mapping.read_only = rbd_opts->read_only;
Alex Elderc53d5892012-10-25 23:34:42 -05003702 kfree(rbd_opts);
3703 rbd_opts = NULL; /* done with this */
Alex Elderbd4ba652012-10-25 23:34:42 -05003704
Alex Eldera30b71b2012-07-10 20:30:11 -05003705 rc = rbd_dev_probe(rbd_dev);
3706 if (rc < 0)
Alex Elderc53d5892012-10-25 23:34:42 -05003707 goto err_out_rbd_dev;
Alex Elder05fd6f62012-08-29 17:11:07 -05003708
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003709 return count;
Alex Elderc53d5892012-10-25 23:34:42 -05003710err_out_rbd_dev:
3711 rbd_dev_destroy(rbd_dev);
Alex Elderbd4ba652012-10-25 23:34:42 -05003712err_out_client:
Alex Elder9d3997f2012-10-25 23:34:42 -05003713 rbd_put_client(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05003714err_out_args:
Alex Elder78cea762012-10-25 23:34:41 -05003715 if (ceph_opts)
3716 ceph_destroy_options(ceph_opts);
Alex Elder4e9afeb2012-10-25 23:34:41 -05003717 kfree(rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05003718 rbd_spec_put(spec);
Alex Elderbd4ba652012-10-25 23:34:42 -05003719err_out_module:
3720 module_put(THIS_MODULE);
Alex Elder27cc2592012-02-02 08:13:30 -06003721
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003722 dout("Error adding device %s\n", buf);
Alex Elder27cc2592012-02-02 08:13:30 -06003723
3724 return (ssize_t) rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003725}
3726
Alex Elderde71a292012-07-03 16:01:19 -05003727static struct rbd_device *__rbd_get_dev(unsigned long dev_id)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003728{
3729 struct list_head *tmp;
3730 struct rbd_device *rbd_dev;
3731
Alex Eldere124a82f2012-01-29 13:57:44 -06003732 spin_lock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003733 list_for_each(tmp, &rbd_dev_list) {
3734 rbd_dev = list_entry(tmp, struct rbd_device, node);
Alex Elderde71a292012-07-03 16:01:19 -05003735 if (rbd_dev->dev_id == dev_id) {
Alex Eldere124a82f2012-01-29 13:57:44 -06003736 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003737 return rbd_dev;
Alex Eldere124a82f2012-01-29 13:57:44 -06003738 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003739 }
Alex Eldere124a82f2012-01-29 13:57:44 -06003740 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003741 return NULL;
3742}
3743
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003744static void rbd_dev_release(struct device *dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003745{
Alex Elder593a9e72012-02-07 12:03:37 -06003746 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003747
Alex Elder1dbb4392012-01-24 10:08:37 -06003748 if (rbd_dev->watch_request) {
3749 struct ceph_client *client = rbd_dev->rbd_client->client;
3750
3751 ceph_osdc_unregister_linger_request(&client->osdc,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003752 rbd_dev->watch_request);
Alex Elder1dbb4392012-01-24 10:08:37 -06003753 }
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003754 if (rbd_dev->watch_event)
Alex Elder070c6332012-07-25 09:32:41 -05003755 rbd_req_sync_unwatch(rbd_dev);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003756
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003757
3758 /* clean up and free blkdev */
3759 rbd_free_disk(rbd_dev);
3760 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Alex Elder32eec682012-02-08 16:11:14 -06003761
Alex Elder2ac4e752012-07-10 20:30:10 -05003762 /* release allocated disk header fields */
3763 rbd_header_free(&rbd_dev->header);
3764
Alex Elder32eec682012-02-08 16:11:14 -06003765 /* done with the id, and with the rbd_dev */
Alex Eldere2839302012-08-29 17:11:06 -05003766 rbd_dev_id_put(rbd_dev);
Alex Elderc53d5892012-10-25 23:34:42 -05003767 rbd_assert(rbd_dev->rbd_client != NULL);
3768 rbd_dev_destroy(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003769
3770 /* release module ref */
3771 module_put(THIS_MODULE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003772}
3773
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003774static ssize_t rbd_remove(struct bus_type *bus,
3775 const char *buf,
3776 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003777{
3778 struct rbd_device *rbd_dev = NULL;
3779 int target_id, rc;
3780 unsigned long ul;
3781 int ret = count;
3782
3783 rc = strict_strtoul(buf, 10, &ul);
3784 if (rc)
3785 return rc;
3786
3787 /* convert to int; abort if we lost anything in the conversion */
3788 target_id = (int) ul;
3789 if (target_id != ul)
3790 return -EINVAL;
3791
3792 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
3793
3794 rbd_dev = __rbd_get_dev(target_id);
3795 if (!rbd_dev) {
3796 ret = -ENOENT;
3797 goto done;
3798 }
3799
Alex Elder42382b72012-11-16 09:29:16 -06003800 if (rbd_dev->open_count) {
3801 ret = -EBUSY;
3802 goto done;
3803 }
3804
Alex Elder41f38c22012-10-25 23:34:40 -05003805 rbd_remove_all_snaps(rbd_dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003806 rbd_bus_del_dev(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003807
3808done:
3809 mutex_unlock(&ctl_mutex);
Alex Elderaafb2302012-09-06 16:00:54 -05003810
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003811 return ret;
3812}
3813
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003814/*
3815 * create control files in sysfs
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003816 * /sys/bus/rbd/...
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003817 */
3818static int rbd_sysfs_init(void)
3819{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003820 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003821
Alex Elderfed4c142012-02-07 12:03:36 -06003822 ret = device_register(&rbd_root_dev);
Alex Elder21079782012-01-24 10:08:36 -06003823 if (ret < 0)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003824 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003825
Alex Elderfed4c142012-02-07 12:03:36 -06003826 ret = bus_register(&rbd_bus_type);
3827 if (ret < 0)
3828 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003829
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003830 return ret;
3831}
3832
3833static void rbd_sysfs_cleanup(void)
3834{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003835 bus_unregister(&rbd_bus_type);
Alex Elderfed4c142012-02-07 12:03:36 -06003836 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003837}
3838
3839int __init rbd_init(void)
3840{
3841 int rc;
3842
3843 rc = rbd_sysfs_init();
3844 if (rc)
3845 return rc;
Alex Elderf0f8cef2012-01-29 13:57:44 -06003846 pr_info("loaded " RBD_DRV_NAME_LONG "\n");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003847 return 0;
3848}
3849
3850void __exit rbd_exit(void)
3851{
3852 rbd_sysfs_cleanup();
3853}
3854
3855module_init(rbd_init);
3856module_exit(rbd_exit);
3857
3858MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
3859MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
3860MODULE_DESCRIPTION("rados block device");
3861
3862/* following authorship retained from original osdblk.c */
3863MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
3864
3865MODULE_LICENSE("GPL");