blob: 007b726ea0eb550e2d735242471618c2af6f51f2 [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 Elder2647ba32012-11-19 22:55:21 -060055/* It might be useful to have these defined elsewhere */
Alex Elderdf111be2012-08-09 10:33:26 -070056
Alex Elder2647ba32012-11-19 22:55:21 -060057#define U8_MAX ((u8) (~0U))
58#define U16_MAX ((u16) (~0U))
59#define U32_MAX ((u32) (~0U))
60#define U64_MAX ((u64) (~0ULL))
Alex Elderdf111be2012-08-09 10:33:26 -070061
Alex Elderf0f8cef2012-01-29 13:57:44 -060062#define RBD_DRV_NAME "rbd"
63#define RBD_DRV_NAME_LONG "rbd (rados block device)"
Yehuda Sadeh602adf42010-08-12 16:11:25 -070064
65#define RBD_MINORS_PER_MAJOR 256 /* max minors per blkdev */
66
Alex Elderd4b125e2012-07-03 16:01:19 -050067#define RBD_SNAP_DEV_NAME_PREFIX "snap_"
68#define RBD_MAX_SNAP_NAME_LEN \
69 (NAME_MAX - (sizeof (RBD_SNAP_DEV_NAME_PREFIX) - 1))
70
Alex Elder35d489f2012-07-03 16:01:19 -050071#define RBD_MAX_SNAP_COUNT 510 /* allows max snapc to fit in 4KB */
Yehuda Sadeh602adf42010-08-12 16:11:25 -070072#define RBD_MAX_OPT_LEN 1024
73
74#define RBD_SNAP_HEAD_NAME "-"
75
Alex Elder9e15b772012-10-30 19:40:33 -050076/* This allows a single page to hold an image name sent by OSD */
77#define RBD_IMAGE_NAME_LEN_MAX (PAGE_SIZE - sizeof (__le32) - 1)
Alex Elder1e130192012-07-03 16:01:19 -050078#define RBD_IMAGE_ID_LEN_MAX 64
Alex Elder9e15b772012-10-30 19:40:33 -050079
Alex Elder1e130192012-07-03 16:01:19 -050080#define RBD_OBJ_PREFIX_LEN_MAX 64
Alex Elder589d30e2012-07-10 20:30:11 -050081
Alex Elderd8891402012-10-09 13:50:17 -070082/* Feature bits */
83
84#define RBD_FEATURE_LAYERING 1
85
86/* Features supported by this (client software) implementation. */
87
88#define RBD_FEATURES_ALL (0)
89
Alex Elder81a89792012-02-02 08:13:30 -060090/*
91 * An RBD device name will be "rbd#", where the "rbd" comes from
92 * RBD_DRV_NAME above, and # is a unique integer identifier.
93 * MAX_INT_FORMAT_WIDTH is used in ensuring DEV_NAME_LEN is big
94 * enough to hold all possible device names.
95 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -070096#define DEV_NAME_LEN 32
Alex Elder81a89792012-02-02 08:13:30 -060097#define MAX_INT_FORMAT_WIDTH ((5 * sizeof (int)) / 2 + 1)
Yehuda Sadeh602adf42010-08-12 16:11:25 -070098
Alex Eldercc0538b2012-08-10 13:12:07 -070099#define RBD_READ_ONLY_DEFAULT false
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700100
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700101/*
102 * block device image metadata (in-memory version)
103 */
104struct rbd_image_header {
Alex Elderf84344f2012-08-31 17:29:51 -0500105 /* These four fields never change for a given rbd image */
Alex Elder849b4262012-07-09 21:04:24 -0500106 char *object_prefix;
Alex Elder34b13182012-07-13 20:35:12 -0500107 u64 features;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700108 __u8 obj_order;
109 __u8 crypt_type;
110 __u8 comp_type;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700111
Alex Elderf84344f2012-08-31 17:29:51 -0500112 /* The remaining fields need to be updated occasionally */
113 u64 image_size;
114 struct ceph_snap_context *snapc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700115 char *snap_names;
116 u64 *snap_sizes;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700117
118 u64 obj_version;
119};
120
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500121/*
122 * An rbd image specification.
123 *
124 * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely
Alex Elderc66c6e02012-11-01 08:39:26 -0500125 * identify an image. Each rbd_dev structure includes a pointer to
126 * an rbd_spec structure that encapsulates this identity.
127 *
128 * Each of the id's in an rbd_spec has an associated name. For a
129 * user-mapped image, the names are supplied and the id's associated
130 * with them are looked up. For a layered image, a parent image is
131 * defined by the tuple, and the names are looked up.
132 *
133 * An rbd_dev structure contains a parent_spec pointer which is
134 * non-null if the image it represents is a child in a layered
135 * image. This pointer will refer to the rbd_spec structure used
136 * by the parent rbd_dev for its own identity (i.e., the structure
137 * is shared between the parent and child).
138 *
139 * Since these structures are populated once, during the discovery
140 * phase of image construction, they are effectively immutable so
141 * we make no effort to synchronize access to them.
142 *
143 * Note that code herein does not assume the image name is known (it
144 * could be a null pointer).
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500145 */
146struct rbd_spec {
147 u64 pool_id;
148 char *pool_name;
149
150 char *image_id;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500151 char *image_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500152
153 u64 snap_id;
154 char *snap_name;
155
156 struct kref kref;
157};
158
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700159struct rbd_options {
Alex Eldercc0538b2012-08-10 13:12:07 -0700160 bool read_only;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700161};
162
163/*
Alex Elderf0f8cef2012-01-29 13:57:44 -0600164 * an instance of the client. multiple devices may share an rbd client.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700165 */
166struct rbd_client {
167 struct ceph_client *client;
168 struct kref kref;
169 struct list_head node;
170};
171
172/*
Alex Elderf0f8cef2012-01-29 13:57:44 -0600173 * a request completion status
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700174 */
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700175struct rbd_req_status {
176 int done;
Alex Elder8986cb32012-11-08 08:01:39 -0600177 s32 rc;
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700178 u64 bytes;
179};
180
181/*
182 * a collection of requests
183 */
184struct rbd_req_coll {
185 int total;
186 int num_done;
187 struct kref kref;
188 struct rbd_req_status status[0];
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700189};
190
Alex Elderf0f8cef2012-01-29 13:57:44 -0600191/*
192 * a single io request
193 */
194struct rbd_request {
195 struct request *rq; /* blk layer request */
196 struct bio *bio; /* cloned bio */
197 struct page **pages; /* list of used pages */
198 u64 len;
199 int coll_index;
200 struct rbd_req_coll *coll;
201};
202
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800203struct rbd_snap {
204 struct device dev;
205 const char *name;
Josh Durgin35915382011-12-05 18:25:13 -0800206 u64 size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800207 struct list_head node;
208 u64 id;
Alex Elder34b13182012-07-13 20:35:12 -0500209 u64 features;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800210};
211
Alex Elderf84344f2012-08-31 17:29:51 -0500212struct rbd_mapping {
Alex Elder99c1f082012-08-30 14:42:15 -0500213 u64 size;
Alex Elder34b13182012-07-13 20:35:12 -0500214 u64 features;
Alex Elderf84344f2012-08-31 17:29:51 -0500215 bool read_only;
216};
217
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700218/*
219 * a single device
220 */
221struct rbd_device {
Alex Elderde71a292012-07-03 16:01:19 -0500222 int dev_id; /* blkdev unique id */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700223
224 int major; /* blkdev assigned major */
225 struct gendisk *disk; /* blkdev's gendisk and rq */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700226
Alex Eldera30b71b2012-07-10 20:30:11 -0500227 u32 image_format; /* Either 1 or 2 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700228 struct rbd_client *rbd_client;
229
230 char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */
231
232 spinlock_t lock; /* queue lock */
233
234 struct rbd_image_header header;
Alex Elderd78b6502012-11-09 08:43:15 -0600235 atomic_t exists;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500236 struct rbd_spec *spec;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700237
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500238 char *header_name;
Alex Elder971f8392012-10-25 23:34:41 -0500239
Alex Elder0903e872012-11-14 12:25:19 -0600240 struct ceph_file_layout layout;
241
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700242 struct ceph_osd_event *watch_event;
243 struct ceph_osd_request *watch_request;
244
Alex Elder86b00e02012-10-25 23:34:42 -0500245 struct rbd_spec *parent_spec;
246 u64 parent_overlap;
247
Josh Durginc6666012011-11-21 17:11:12 -0800248 /* protects updating the header */
249 struct rw_semaphore header_rwsem;
Alex Elderf84344f2012-08-31 17:29:51 -0500250
251 struct rbd_mapping mapping;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700252
253 struct list_head node;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800254
255 /* list of snapshots */
256 struct list_head snaps;
257
258 /* sysfs related */
259 struct device dev;
Alex Elder42382b72012-11-16 09:29:16 -0600260 unsigned long open_count;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800261};
262
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700263static DEFINE_MUTEX(ctl_mutex); /* Serialize open/close/setup/teardown */
Alex Eldere124a82f2012-01-29 13:57:44 -0600264
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700265static LIST_HEAD(rbd_dev_list); /* devices */
Alex Eldere124a82f2012-01-29 13:57:44 -0600266static DEFINE_SPINLOCK(rbd_dev_list_lock);
267
Alex Elder432b8582012-01-29 13:57:44 -0600268static LIST_HEAD(rbd_client_list); /* clients */
269static DEFINE_SPINLOCK(rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700270
Alex Elder304f6802012-08-31 17:29:52 -0500271static int rbd_dev_snaps_update(struct rbd_device *rbd_dev);
272static int rbd_dev_snaps_register(struct rbd_device *rbd_dev);
273
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800274static void rbd_dev_release(struct device *dev);
Alex Elder41f38c22012-10-25 23:34:40 -0500275static void rbd_remove_snap_dev(struct rbd_snap *snap);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800276
Alex Elderf0f8cef2012-01-29 13:57:44 -0600277static ssize_t rbd_add(struct bus_type *bus, const char *buf,
278 size_t count);
279static ssize_t rbd_remove(struct bus_type *bus, const char *buf,
280 size_t count);
281
282static struct bus_attribute rbd_bus_attrs[] = {
283 __ATTR(add, S_IWUSR, NULL, rbd_add),
284 __ATTR(remove, S_IWUSR, NULL, rbd_remove),
285 __ATTR_NULL
286};
287
288static struct bus_type rbd_bus_type = {
289 .name = "rbd",
290 .bus_attrs = rbd_bus_attrs,
291};
292
293static void rbd_root_dev_release(struct device *dev)
294{
295}
296
297static struct device rbd_root_dev = {
298 .init_name = "rbd",
299 .release = rbd_root_dev_release,
300};
301
Alex Elder06ecc6c2012-11-01 10:17:15 -0500302static __printf(2, 3)
303void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...)
304{
305 struct va_format vaf;
306 va_list args;
307
308 va_start(args, fmt);
309 vaf.fmt = fmt;
310 vaf.va = &args;
311
312 if (!rbd_dev)
313 printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf);
314 else if (rbd_dev->disk)
315 printk(KERN_WARNING "%s: %s: %pV\n",
316 RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf);
317 else if (rbd_dev->spec && rbd_dev->spec->image_name)
318 printk(KERN_WARNING "%s: image %s: %pV\n",
319 RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf);
320 else if (rbd_dev->spec && rbd_dev->spec->image_id)
321 printk(KERN_WARNING "%s: id %s: %pV\n",
322 RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf);
323 else /* punt */
324 printk(KERN_WARNING "%s: rbd_dev %p: %pV\n",
325 RBD_DRV_NAME, rbd_dev, &vaf);
326 va_end(args);
327}
328
Alex Elderaafb2302012-09-06 16:00:54 -0500329#ifdef RBD_DEBUG
330#define rbd_assert(expr) \
331 if (unlikely(!(expr))) { \
332 printk(KERN_ERR "\nAssertion failure in %s() " \
333 "at line %d:\n\n" \
334 "\trbd_assert(%s);\n\n", \
335 __func__, __LINE__, #expr); \
336 BUG(); \
337 }
338#else /* !RBD_DEBUG */
339# define rbd_assert(expr) ((void) 0)
340#endif /* !RBD_DEBUG */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800341
Alex Elder117973f2012-08-31 17:29:55 -0500342static int rbd_dev_refresh(struct rbd_device *rbd_dev, u64 *hver);
343static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev, u64 *hver);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700344
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700345static int rbd_open(struct block_device *bdev, fmode_t mode)
346{
Alex Elderf0f8cef2012-01-29 13:57:44 -0600347 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700348
Alex Elderf84344f2012-08-31 17:29:51 -0500349 if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700350 return -EROFS;
351
Alex Elder42382b72012-11-16 09:29:16 -0600352 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Alex Elderc3e946c2012-11-16 09:29:16 -0600353 (void) get_device(&rbd_dev->dev);
Alex Elderf84344f2012-08-31 17:29:51 -0500354 set_device_ro(bdev, rbd_dev->mapping.read_only);
Alex Elder42382b72012-11-16 09:29:16 -0600355 rbd_dev->open_count++;
356 mutex_unlock(&ctl_mutex);
Alex Elder340c7a22012-08-10 13:12:07 -0700357
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700358 return 0;
359}
360
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800361static int rbd_release(struct gendisk *disk, fmode_t mode)
362{
363 struct rbd_device *rbd_dev = disk->private_data;
364
Alex Elder42382b72012-11-16 09:29:16 -0600365 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
366 rbd_assert(rbd_dev->open_count > 0);
367 rbd_dev->open_count--;
Alex Elderc3e946c2012-11-16 09:29:16 -0600368 put_device(&rbd_dev->dev);
Alex Elder42382b72012-11-16 09:29:16 -0600369 mutex_unlock(&ctl_mutex);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800370
371 return 0;
372}
373
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700374static const struct block_device_operations rbd_bd_ops = {
375 .owner = THIS_MODULE,
376 .open = rbd_open,
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800377 .release = rbd_release,
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700378};
379
380/*
381 * Initialize an rbd client instance.
Alex Elder43ae4702012-07-03 16:01:18 -0500382 * We own *ceph_opts.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700383 */
Alex Elderf8c38922012-08-10 13:12:07 -0700384static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700385{
386 struct rbd_client *rbdc;
387 int ret = -ENOMEM;
388
389 dout("rbd_client_create\n");
390 rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL);
391 if (!rbdc)
392 goto out_opt;
393
394 kref_init(&rbdc->kref);
395 INIT_LIST_HEAD(&rbdc->node);
396
Alex Elderbc534d82012-01-29 13:57:44 -0600397 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
398
Alex Elder43ae4702012-07-03 16:01:18 -0500399 rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700400 if (IS_ERR(rbdc->client))
Alex Elderbc534d82012-01-29 13:57:44 -0600401 goto out_mutex;
Alex Elder43ae4702012-07-03 16:01:18 -0500402 ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700403
404 ret = ceph_open_session(rbdc->client);
405 if (ret < 0)
406 goto out_err;
407
Alex Elder432b8582012-01-29 13:57:44 -0600408 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700409 list_add_tail(&rbdc->node, &rbd_client_list);
Alex Elder432b8582012-01-29 13:57:44 -0600410 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700411
Alex Elderbc534d82012-01-29 13:57:44 -0600412 mutex_unlock(&ctl_mutex);
413
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700414 dout("rbd_client_create created %p\n", rbdc);
415 return rbdc;
416
417out_err:
418 ceph_destroy_client(rbdc->client);
Alex Elderbc534d82012-01-29 13:57:44 -0600419out_mutex:
420 mutex_unlock(&ctl_mutex);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700421 kfree(rbdc);
422out_opt:
Alex Elder43ae4702012-07-03 16:01:18 -0500423 if (ceph_opts)
424 ceph_destroy_options(ceph_opts);
Vasiliy Kulikov28f259b2010-09-26 12:59:37 +0400425 return ERR_PTR(ret);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700426}
427
428/*
Alex Elder1f7ba332012-08-10 13:12:07 -0700429 * Find a ceph client with specific addr and configuration. If
430 * found, bump its reference count.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700431 */
Alex Elder1f7ba332012-08-10 13:12:07 -0700432static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700433{
434 struct rbd_client *client_node;
Alex Elder1f7ba332012-08-10 13:12:07 -0700435 bool found = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700436
Alex Elder43ae4702012-07-03 16:01:18 -0500437 if (ceph_opts->flags & CEPH_OPT_NOSHARE)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700438 return NULL;
439
Alex Elder1f7ba332012-08-10 13:12:07 -0700440 spin_lock(&rbd_client_list_lock);
441 list_for_each_entry(client_node, &rbd_client_list, node) {
442 if (!ceph_compare_options(ceph_opts, client_node->client)) {
443 kref_get(&client_node->kref);
444 found = true;
445 break;
446 }
447 }
448 spin_unlock(&rbd_client_list_lock);
449
450 return found ? client_node : NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700451}
452
453/*
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700454 * mount options
455 */
456enum {
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700457 Opt_last_int,
458 /* int args above */
459 Opt_last_string,
460 /* string args above */
Alex Eldercc0538b2012-08-10 13:12:07 -0700461 Opt_read_only,
462 Opt_read_write,
463 /* Boolean args above */
464 Opt_last_bool,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700465};
466
Alex Elder43ae4702012-07-03 16:01:18 -0500467static match_table_t rbd_opts_tokens = {
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700468 /* int args above */
469 /* string args above */
Alex Elderbe466c12012-10-22 11:31:26 -0500470 {Opt_read_only, "read_only"},
Alex Eldercc0538b2012-08-10 13:12:07 -0700471 {Opt_read_only, "ro"}, /* Alternate spelling */
472 {Opt_read_write, "read_write"},
473 {Opt_read_write, "rw"}, /* Alternate spelling */
474 /* Boolean args above */
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700475 {-1, NULL}
476};
477
478static int parse_rbd_opts_token(char *c, void *private)
479{
Alex Elder43ae4702012-07-03 16:01:18 -0500480 struct rbd_options *rbd_opts = private;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700481 substring_t argstr[MAX_OPT_ARGS];
482 int token, intval, ret;
483
Alex Elder43ae4702012-07-03 16:01:18 -0500484 token = match_token(c, rbd_opts_tokens, argstr);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700485 if (token < 0)
486 return -EINVAL;
487
488 if (token < Opt_last_int) {
489 ret = match_int(&argstr[0], &intval);
490 if (ret < 0) {
491 pr_err("bad mount option arg (not int) "
492 "at '%s'\n", c);
493 return ret;
494 }
495 dout("got int token %d val %d\n", token, intval);
496 } else if (token > Opt_last_int && token < Opt_last_string) {
497 dout("got string token %d val %s\n", token,
498 argstr[0].from);
Alex Eldercc0538b2012-08-10 13:12:07 -0700499 } else if (token > Opt_last_string && token < Opt_last_bool) {
500 dout("got Boolean token %d\n", token);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700501 } else {
502 dout("got token %d\n", token);
503 }
504
505 switch (token) {
Alex Eldercc0538b2012-08-10 13:12:07 -0700506 case Opt_read_only:
507 rbd_opts->read_only = true;
508 break;
509 case Opt_read_write:
510 rbd_opts->read_only = false;
511 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700512 default:
Alex Elderaafb2302012-09-06 16:00:54 -0500513 rbd_assert(false);
514 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700515 }
516 return 0;
517}
518
519/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700520 * Get a ceph client with specific addr and configuration, if one does
521 * not exist create it.
522 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500523static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700524{
Alex Elderf8c38922012-08-10 13:12:07 -0700525 struct rbd_client *rbdc;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700526
Alex Elder1f7ba332012-08-10 13:12:07 -0700527 rbdc = rbd_client_find(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500528 if (rbdc) /* using an existing client */
Alex Elder43ae4702012-07-03 16:01:18 -0500529 ceph_destroy_options(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500530 else
Alex Elderf8c38922012-08-10 13:12:07 -0700531 rbdc = rbd_client_create(ceph_opts);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700532
Alex Elder9d3997f2012-10-25 23:34:42 -0500533 return rbdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700534}
535
536/*
537 * Destroy ceph client
Alex Elderd23a4b32012-01-29 13:57:43 -0600538 *
Alex Elder432b8582012-01-29 13:57:44 -0600539 * Caller must hold rbd_client_list_lock.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700540 */
541static void rbd_client_release(struct kref *kref)
542{
543 struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
544
545 dout("rbd_release_client %p\n", rbdc);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500546 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700547 list_del(&rbdc->node);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500548 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700549
550 ceph_destroy_client(rbdc->client);
551 kfree(rbdc);
552}
553
554/*
555 * Drop reference to ceph client node. If it's not referenced anymore, release
556 * it.
557 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500558static void rbd_put_client(struct rbd_client *rbdc)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700559{
Alex Elderc53d5892012-10-25 23:34:42 -0500560 if (rbdc)
561 kref_put(&rbdc->kref, rbd_client_release);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700562}
563
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700564/*
565 * Destroy requests collection
566 */
567static void rbd_coll_release(struct kref *kref)
568{
569 struct rbd_req_coll *coll =
570 container_of(kref, struct rbd_req_coll, kref);
571
572 dout("rbd_coll_release %p\n", coll);
573 kfree(coll);
574}
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700575
Alex Eldera30b71b2012-07-10 20:30:11 -0500576static bool rbd_image_format_valid(u32 image_format)
577{
578 return image_format == 1 || image_format == 2;
579}
580
Alex Elder8e94af82012-07-25 09:32:40 -0500581static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
582{
Alex Elder103a1502012-08-02 11:29:45 -0500583 size_t size;
584 u32 snap_count;
585
586 /* The header has to start with the magic rbd header text */
587 if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT)))
588 return false;
589
Alex Elderdb2388b2012-10-20 22:17:27 -0500590 /* The bio layer requires at least sector-sized I/O */
591
592 if (ondisk->options.order < SECTOR_SHIFT)
593 return false;
594
595 /* If we use u64 in a few spots we may be able to loosen this */
596
597 if (ondisk->options.order > 8 * sizeof (int) - 1)
598 return false;
599
Alex Elder103a1502012-08-02 11:29:45 -0500600 /*
601 * The size of a snapshot header has to fit in a size_t, and
602 * that limits the number of snapshots.
603 */
604 snap_count = le32_to_cpu(ondisk->snap_count);
605 size = SIZE_MAX - sizeof (struct ceph_snap_context);
606 if (snap_count > size / sizeof (__le64))
607 return false;
608
609 /*
610 * Not only that, but the size of the entire the snapshot
611 * header must also be representable in a size_t.
612 */
613 size -= snap_count * sizeof (__le64);
614 if ((u64) size < le64_to_cpu(ondisk->snap_names_len))
615 return false;
616
617 return true;
Alex Elder8e94af82012-07-25 09:32:40 -0500618}
619
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700620/*
621 * Create a new header structure, translate header format from the on-disk
622 * header.
623 */
624static int rbd_header_from_disk(struct rbd_image_header *header,
Alex Elder4156d992012-08-02 11:29:46 -0500625 struct rbd_image_header_ondisk *ondisk)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700626{
Alex Elderccece232012-07-10 20:30:10 -0500627 u32 snap_count;
Alex Elder58c17b02012-08-23 23:22:06 -0500628 size_t len;
Alex Elderd2bb24e2012-07-26 23:37:14 -0500629 size_t size;
Alex Elder621901d2012-08-23 23:22:06 -0500630 u32 i;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700631
Alex Elder6a523252012-07-19 17:12:59 -0500632 memset(header, 0, sizeof (*header));
633
Alex Elder103a1502012-08-02 11:29:45 -0500634 snap_count = le32_to_cpu(ondisk->snap_count);
635
Alex Elder58c17b02012-08-23 23:22:06 -0500636 len = strnlen(ondisk->object_prefix, sizeof (ondisk->object_prefix));
637 header->object_prefix = kmalloc(len + 1, GFP_KERNEL);
Alex Elder6a523252012-07-19 17:12:59 -0500638 if (!header->object_prefix)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700639 return -ENOMEM;
Alex Elder58c17b02012-08-23 23:22:06 -0500640 memcpy(header->object_prefix, ondisk->object_prefix, len);
641 header->object_prefix[len] = '\0';
Alex Elder00f1f362012-02-07 12:03:36 -0600642
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700643 if (snap_count) {
Alex Elderf785cc12012-08-23 23:22:06 -0500644 u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len);
645
Alex Elder621901d2012-08-23 23:22:06 -0500646 /* Save a copy of the snapshot names */
647
Alex Elderf785cc12012-08-23 23:22:06 -0500648 if (snap_names_len > (u64) SIZE_MAX)
649 return -EIO;
650 header->snap_names = kmalloc(snap_names_len, GFP_KERNEL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700651 if (!header->snap_names)
Alex Elder6a523252012-07-19 17:12:59 -0500652 goto out_err;
Alex Elderf785cc12012-08-23 23:22:06 -0500653 /*
654 * Note that rbd_dev_v1_header_read() guarantees
655 * the ondisk buffer we're working with has
656 * snap_names_len bytes beyond the end of the
657 * snapshot id array, this memcpy() is safe.
658 */
659 memcpy(header->snap_names, &ondisk->snaps[snap_count],
660 snap_names_len);
Alex Elder6a523252012-07-19 17:12:59 -0500661
Alex Elder621901d2012-08-23 23:22:06 -0500662 /* Record each snapshot's size */
663
Alex Elderd2bb24e2012-07-26 23:37:14 -0500664 size = snap_count * sizeof (*header->snap_sizes);
665 header->snap_sizes = kmalloc(size, GFP_KERNEL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700666 if (!header->snap_sizes)
Alex Elder6a523252012-07-19 17:12:59 -0500667 goto out_err;
Alex Elder621901d2012-08-23 23:22:06 -0500668 for (i = 0; i < snap_count; i++)
669 header->snap_sizes[i] =
670 le64_to_cpu(ondisk->snaps[i].image_size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700671 } else {
Alex Elderccece232012-07-10 20:30:10 -0500672 WARN_ON(ondisk->snap_names_len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700673 header->snap_names = NULL;
674 header->snap_sizes = NULL;
675 }
Alex Elder849b4262012-07-09 21:04:24 -0500676
Alex Elder34b13182012-07-13 20:35:12 -0500677 header->features = 0; /* No features support in v1 images */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700678 header->obj_order = ondisk->options.order;
679 header->crypt_type = ondisk->options.crypt_type;
680 header->comp_type = ondisk->options.comp_type;
Alex Elder6a523252012-07-19 17:12:59 -0500681
Alex Elder621901d2012-08-23 23:22:06 -0500682 /* Allocate and fill in the snapshot context */
683
Alex Elderf84344f2012-08-31 17:29:51 -0500684 header->image_size = le64_to_cpu(ondisk->image_size);
Alex Elder6a523252012-07-19 17:12:59 -0500685 size = sizeof (struct ceph_snap_context);
686 size += snap_count * sizeof (header->snapc->snaps[0]);
687 header->snapc = kzalloc(size, GFP_KERNEL);
688 if (!header->snapc)
689 goto out_err;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700690
691 atomic_set(&header->snapc->nref, 1);
Alex Elder505cbb92012-07-19 08:49:18 -0500692 header->snapc->seq = le64_to_cpu(ondisk->snap_seq);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700693 header->snapc->num_snaps = snap_count;
Alex Elder621901d2012-08-23 23:22:06 -0500694 for (i = 0; i < snap_count; i++)
695 header->snapc->snaps[i] =
696 le64_to_cpu(ondisk->snaps[i].id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700697
698 return 0;
699
Alex Elder6a523252012-07-19 17:12:59 -0500700out_err:
Alex Elder849b4262012-07-09 21:04:24 -0500701 kfree(header->snap_sizes);
Alex Elderccece232012-07-10 20:30:10 -0500702 header->snap_sizes = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700703 kfree(header->snap_names);
Alex Elderccece232012-07-10 20:30:10 -0500704 header->snap_names = NULL;
Alex Elder6a523252012-07-19 17:12:59 -0500705 kfree(header->object_prefix);
706 header->object_prefix = NULL;
Alex Elderccece232012-07-10 20:30:10 -0500707
Alex Elder00f1f362012-02-07 12:03:36 -0600708 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700709}
710
Alex Elder9e15b772012-10-30 19:40:33 -0500711static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id)
712{
713 struct rbd_snap *snap;
714
715 if (snap_id == CEPH_NOSNAP)
716 return RBD_SNAP_HEAD_NAME;
717
718 list_for_each_entry(snap, &rbd_dev->snaps, node)
719 if (snap_id == snap->id)
720 return snap->name;
721
722 return NULL;
723}
724
Alex Elder8836b992012-08-30 14:42:15 -0500725static int snap_by_name(struct rbd_device *rbd_dev, const char *snap_name)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700726{
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700727
Alex Eldere86924a2012-07-10 20:30:11 -0500728 struct rbd_snap *snap;
Alex Elder00f1f362012-02-07 12:03:36 -0600729
Alex Eldere86924a2012-07-10 20:30:11 -0500730 list_for_each_entry(snap, &rbd_dev->snaps, node) {
731 if (!strcmp(snap_name, snap->name)) {
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500732 rbd_dev->spec->snap_id = snap->id;
Alex Eldere86924a2012-07-10 20:30:11 -0500733 rbd_dev->mapping.size = snap->size;
Alex Elder34b13182012-07-13 20:35:12 -0500734 rbd_dev->mapping.features = snap->features;
Alex Elder00f1f362012-02-07 12:03:36 -0600735
Alex Eldere86924a2012-07-10 20:30:11 -0500736 return 0;
Alex Elder00f1f362012-02-07 12:03:36 -0600737 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700738 }
Alex Eldere86924a2012-07-10 20:30:11 -0500739
Alex Elder00f1f362012-02-07 12:03:36 -0600740 return -ENOENT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700741}
742
Alex Elder819d52b2012-10-25 23:34:41 -0500743static int rbd_dev_set_mapping(struct rbd_device *rbd_dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700744{
Alex Elder78dc4472012-07-19 08:49:18 -0500745 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700746
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500747 if (!memcmp(rbd_dev->spec->snap_name, RBD_SNAP_HEAD_NAME,
Josh Durgincc9d7342011-11-21 18:19:13 -0800748 sizeof (RBD_SNAP_HEAD_NAME))) {
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500749 rbd_dev->spec->snap_id = CEPH_NOSNAP;
Alex Elder99c1f082012-08-30 14:42:15 -0500750 rbd_dev->mapping.size = rbd_dev->header.image_size;
Alex Elder34b13182012-07-13 20:35:12 -0500751 rbd_dev->mapping.features = rbd_dev->header.features;
Alex Eldere86924a2012-07-10 20:30:11 -0500752 ret = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700753 } else {
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500754 ret = snap_by_name(rbd_dev, rbd_dev->spec->snap_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700755 if (ret < 0)
756 goto done;
Alex Elderf84344f2012-08-31 17:29:51 -0500757 rbd_dev->mapping.read_only = true;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700758 }
Alex Elderd78b6502012-11-09 08:43:15 -0600759 atomic_set(&rbd_dev->exists, 1);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700760done:
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700761 return ret;
762}
763
764static void rbd_header_free(struct rbd_image_header *header)
765{
Alex Elder849b4262012-07-09 21:04:24 -0500766 kfree(header->object_prefix);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500767 header->object_prefix = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700768 kfree(header->snap_sizes);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500769 header->snap_sizes = NULL;
Alex Elder849b4262012-07-09 21:04:24 -0500770 kfree(header->snap_names);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500771 header->snap_names = NULL;
Josh Durgind1d25642011-12-05 14:03:05 -0800772 ceph_put_snap_context(header->snapc);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500773 header->snapc = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700774}
775
Alex Elder65ccfe22012-08-09 10:33:26 -0700776static char *rbd_segment_name(struct rbd_device *rbd_dev, u64 offset)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700777{
Alex Elder65ccfe22012-08-09 10:33:26 -0700778 char *name;
779 u64 segment;
780 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700781
Alex Elder2fd82b92012-11-09 15:05:54 -0600782 name = kmalloc(MAX_OBJ_NAME_SIZE + 1, GFP_NOIO);
Alex Elder65ccfe22012-08-09 10:33:26 -0700783 if (!name)
784 return NULL;
785 segment = offset >> rbd_dev->header.obj_order;
Alex Elder2fd82b92012-11-09 15:05:54 -0600786 ret = snprintf(name, MAX_OBJ_NAME_SIZE + 1, "%s.%012llx",
Alex Elder65ccfe22012-08-09 10:33:26 -0700787 rbd_dev->header.object_prefix, segment);
Alex Elder2fd82b92012-11-09 15:05:54 -0600788 if (ret < 0 || ret > MAX_OBJ_NAME_SIZE) {
Alex Elder65ccfe22012-08-09 10:33:26 -0700789 pr_err("error formatting segment name for #%llu (%d)\n",
790 segment, ret);
791 kfree(name);
792 name = NULL;
793 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700794
Alex Elder65ccfe22012-08-09 10:33:26 -0700795 return name;
796}
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700797
Alex Elder65ccfe22012-08-09 10:33:26 -0700798static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset)
799{
800 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700801
Alex Elder65ccfe22012-08-09 10:33:26 -0700802 return offset & (segment_size - 1);
803}
804
805static u64 rbd_segment_length(struct rbd_device *rbd_dev,
806 u64 offset, u64 length)
807{
808 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
809
810 offset &= segment_size - 1;
811
Alex Elderaafb2302012-09-06 16:00:54 -0500812 rbd_assert(length <= U64_MAX - offset);
Alex Elder65ccfe22012-08-09 10:33:26 -0700813 if (offset + length > segment_size)
814 length = segment_size - offset;
815
816 return length;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700817}
818
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700819static int rbd_get_num_segments(struct rbd_image_header *header,
820 u64 ofs, u64 len)
821{
Alex Elderdf111be2012-08-09 10:33:26 -0700822 u64 start_seg;
823 u64 end_seg;
824
825 if (!len)
826 return 0;
827 if (len - 1 > U64_MAX - ofs)
828 return -ERANGE;
829
830 start_seg = ofs >> header->obj_order;
831 end_seg = (ofs + len - 1) >> header->obj_order;
832
Yehuda Sadeh1fec7092011-05-13 13:52:56 -0700833 return end_seg - start_seg + 1;
834}
835
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700836/*
Josh Durgin029bcbd2011-07-22 11:35:23 -0700837 * returns the size of an object in the image
838 */
839static u64 rbd_obj_bytes(struct rbd_image_header *header)
840{
841 return 1 << header->obj_order;
842}
843
844/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700845 * bio helpers
846 */
847
848static void bio_chain_put(struct bio *chain)
849{
850 struct bio *tmp;
851
852 while (chain) {
853 tmp = chain;
854 chain = chain->bi_next;
855 bio_put(tmp);
856 }
857}
858
859/*
860 * zeros a bio chain, starting at specific offset
861 */
862static void zero_bio_chain(struct bio *chain, int start_ofs)
863{
864 struct bio_vec *bv;
865 unsigned long flags;
866 void *buf;
867 int i;
868 int pos = 0;
869
870 while (chain) {
871 bio_for_each_segment(bv, chain, i) {
872 if (pos + bv->bv_len > start_ofs) {
873 int remainder = max(start_ofs - pos, 0);
874 buf = bvec_kmap_irq(bv, &flags);
875 memset(buf + remainder, 0,
876 bv->bv_len - remainder);
Dan Carpenter85b5aaa2010-10-11 21:15:11 +0200877 bvec_kunmap_irq(buf, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700878 }
879 pos += bv->bv_len;
880 }
881
882 chain = chain->bi_next;
883 }
884}
885
886/*
Alex Elderf7760da2012-10-20 22:17:27 -0500887 * Clone a portion of a bio, starting at the given byte offset
888 * and continuing for the number of bytes indicated.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700889 */
Alex Elderf7760da2012-10-20 22:17:27 -0500890static struct bio *bio_clone_range(struct bio *bio_src,
891 unsigned int offset,
892 unsigned int len,
893 gfp_t gfpmask)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700894{
Alex Elderf7760da2012-10-20 22:17:27 -0500895 struct bio_vec *bv;
896 unsigned int resid;
897 unsigned short idx;
898 unsigned int voff;
899 unsigned short end_idx;
900 unsigned short vcnt;
901 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700902
Alex Elderf7760da2012-10-20 22:17:27 -0500903 /* Handle the easy case for the caller */
904
905 if (!offset && len == bio_src->bi_size)
906 return bio_clone(bio_src, gfpmask);
907
908 if (WARN_ON_ONCE(!len))
909 return NULL;
910 if (WARN_ON_ONCE(len > bio_src->bi_size))
911 return NULL;
912 if (WARN_ON_ONCE(offset > bio_src->bi_size - len))
913 return NULL;
914
915 /* Find first affected segment... */
916
917 resid = offset;
918 __bio_for_each_segment(bv, bio_src, idx, 0) {
919 if (resid < bv->bv_len)
920 break;
921 resid -= bv->bv_len;
922 }
923 voff = resid;
924
925 /* ...and the last affected segment */
926
927 resid += len;
928 __bio_for_each_segment(bv, bio_src, end_idx, idx) {
929 if (resid <= bv->bv_len)
930 break;
931 resid -= bv->bv_len;
932 }
933 vcnt = end_idx - idx + 1;
934
935 /* Build the clone */
936
937 bio = bio_alloc(gfpmask, (unsigned int) vcnt);
938 if (!bio)
939 return NULL; /* ENOMEM */
940
941 bio->bi_bdev = bio_src->bi_bdev;
942 bio->bi_sector = bio_src->bi_sector + (offset >> SECTOR_SHIFT);
943 bio->bi_rw = bio_src->bi_rw;
944 bio->bi_flags |= 1 << BIO_CLONED;
945
946 /*
947 * Copy over our part of the bio_vec, then update the first
948 * and last (or only) entries.
949 */
950 memcpy(&bio->bi_io_vec[0], &bio_src->bi_io_vec[idx],
951 vcnt * sizeof (struct bio_vec));
952 bio->bi_io_vec[0].bv_offset += voff;
953 if (vcnt > 1) {
954 bio->bi_io_vec[0].bv_len -= voff;
955 bio->bi_io_vec[vcnt - 1].bv_len = resid;
956 } else {
957 bio->bi_io_vec[0].bv_len = len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700958 }
959
Alex Elderf7760da2012-10-20 22:17:27 -0500960 bio->bi_vcnt = vcnt;
961 bio->bi_size = len;
962 bio->bi_idx = 0;
Alex Elder542582f2012-08-09 10:33:25 -0700963
Alex Elderf7760da2012-10-20 22:17:27 -0500964 return bio;
965}
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700966
Alex Elderf7760da2012-10-20 22:17:27 -0500967/*
968 * Clone a portion of a bio chain, starting at the given byte offset
969 * into the first bio in the source chain and continuing for the
970 * number of bytes indicated. The result is another bio chain of
971 * exactly the given length, or a null pointer on error.
972 *
973 * The bio_src and offset parameters are both in-out. On entry they
974 * refer to the first source bio and the offset into that bio where
975 * the start of data to be cloned is located.
976 *
977 * On return, bio_src is updated to refer to the bio in the source
978 * chain that contains first un-cloned byte, and *offset will
979 * contain the offset of that byte within that bio.
980 */
981static struct bio *bio_chain_clone_range(struct bio **bio_src,
982 unsigned int *offset,
983 unsigned int len,
984 gfp_t gfpmask)
985{
986 struct bio *bi = *bio_src;
987 unsigned int off = *offset;
988 struct bio *chain = NULL;
989 struct bio **end;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700990
Alex Elderf7760da2012-10-20 22:17:27 -0500991 /* Build up a chain of clone bios up to the limit */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700992
Alex Elderf7760da2012-10-20 22:17:27 -0500993 if (!bi || off >= bi->bi_size || !len)
994 return NULL; /* Nothing to clone */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700995
Alex Elderf7760da2012-10-20 22:17:27 -0500996 end = &chain;
997 while (len) {
998 unsigned int bi_size;
999 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001000
Alex Elderf5400b72012-11-01 10:17:15 -05001001 if (!bi) {
1002 rbd_warn(NULL, "bio_chain exhausted with %u left", len);
Alex Elderf7760da2012-10-20 22:17:27 -05001003 goto out_err; /* EINVAL; ran out of bio's */
Alex Elderf5400b72012-11-01 10:17:15 -05001004 }
Alex Elderf7760da2012-10-20 22:17:27 -05001005 bi_size = min_t(unsigned int, bi->bi_size - off, len);
1006 bio = bio_clone_range(bi, off, bi_size, gfpmask);
1007 if (!bio)
1008 goto out_err; /* ENOMEM */
1009
1010 *end = bio;
1011 end = &bio->bi_next;
1012
1013 off += bi_size;
1014 if (off == bi->bi_size) {
1015 bi = bi->bi_next;
1016 off = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001017 }
Alex Elderf7760da2012-10-20 22:17:27 -05001018 len -= bi_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001019 }
Alex Elderf7760da2012-10-20 22:17:27 -05001020 *bio_src = bi;
1021 *offset = off;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001022
Alex Elderf7760da2012-10-20 22:17:27 -05001023 return chain;
1024out_err:
1025 bio_chain_put(chain);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001026
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001027 return NULL;
1028}
1029
Alex Elder8d23bf22012-11-19 22:55:21 -06001030struct ceph_osd_req_op *rbd_osd_req_op_create(u16 opcode, ...)
1031{
1032 struct ceph_osd_req_op *op;
1033 va_list args;
Alex Elder2647ba32012-11-19 22:55:21 -06001034 size_t size;
Alex Elder8d23bf22012-11-19 22:55:21 -06001035
1036 op = kzalloc(sizeof (*op), GFP_NOIO);
1037 if (!op)
1038 return NULL;
1039 op->op = opcode;
1040 va_start(args, opcode);
1041 switch (opcode) {
1042 case CEPH_OSD_OP_READ:
1043 case CEPH_OSD_OP_WRITE:
1044 /* rbd_osd_req_op_create(READ, offset, length) */
1045 /* rbd_osd_req_op_create(WRITE, offset, length) */
1046 op->extent.offset = va_arg(args, u64);
1047 op->extent.length = va_arg(args, u64);
1048 if (opcode == CEPH_OSD_OP_WRITE)
1049 op->payload_len = op->extent.length;
1050 break;
Alex Elder2647ba32012-11-19 22:55:21 -06001051 case CEPH_OSD_OP_CALL:
1052 /* rbd_osd_req_op_create(CALL, class, method, data, datalen) */
1053 op->cls.class_name = va_arg(args, char *);
1054 size = strlen(op->cls.class_name);
1055 rbd_assert(size <= (size_t) U8_MAX);
1056 op->cls.class_len = size;
1057 op->payload_len = size;
1058
1059 op->cls.method_name = va_arg(args, char *);
1060 size = strlen(op->cls.method_name);
1061 rbd_assert(size <= (size_t) U8_MAX);
1062 op->cls.method_len = size;
1063 op->payload_len += size;
1064
1065 op->cls.argc = 0;
1066 op->cls.indata = va_arg(args, void *);
1067 size = va_arg(args, size_t);
1068 rbd_assert(size <= (size_t) U32_MAX);
1069 op->cls.indata_len = (u32) size;
1070 op->payload_len += size;
1071 break;
Alex Elder5efea492012-11-19 22:55:21 -06001072 case CEPH_OSD_OP_NOTIFY_ACK:
1073 case CEPH_OSD_OP_WATCH:
1074 /* rbd_osd_req_op_create(NOTIFY_ACK, cookie, version) */
1075 /* rbd_osd_req_op_create(WATCH, cookie, version, flag) */
1076 op->watch.cookie = va_arg(args, u64);
1077 op->watch.ver = va_arg(args, u64);
1078 op->watch.ver = cpu_to_le64(op->watch.ver);
1079 if (opcode == CEPH_OSD_OP_WATCH && va_arg(args, int))
1080 op->watch.flag = (u8) 1;
1081 break;
Alex Elder8d23bf22012-11-19 22:55:21 -06001082 default:
1083 rbd_warn(NULL, "unsupported opcode %hu\n", opcode);
1084 kfree(op);
1085 op = NULL;
1086 break;
1087 }
1088 va_end(args);
1089
1090 return op;
1091}
1092
1093static void rbd_osd_req_op_destroy(struct ceph_osd_req_op *op)
1094{
1095 kfree(op);
1096}
1097
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001098static void rbd_coll_end_req_index(struct request *rq,
1099 struct rbd_req_coll *coll,
1100 int index,
Alex Elder8986cb32012-11-08 08:01:39 -06001101 s32 ret, u64 len)
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001102{
1103 struct request_queue *q;
1104 int min, max, i;
1105
Alex Elderbd919d42012-07-13 20:35:11 -05001106 dout("rbd_coll_end_req_index %p index %d ret %d len %llu\n",
Alex Elder8986cb32012-11-08 08:01:39 -06001107 coll, index, (int)ret, (unsigned long long)len);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001108
1109 if (!rq)
1110 return;
1111
1112 if (!coll) {
1113 blk_end_request(rq, ret, len);
1114 return;
1115 }
1116
1117 q = rq->q;
1118
1119 spin_lock_irq(q->queue_lock);
1120 coll->status[index].done = 1;
1121 coll->status[index].rc = ret;
1122 coll->status[index].bytes = len;
1123 max = min = coll->num_done;
1124 while (max < coll->total && coll->status[max].done)
1125 max++;
1126
1127 for (i = min; i<max; i++) {
Alex Elder8986cb32012-11-08 08:01:39 -06001128 __blk_end_request(rq, (int)coll->status[i].rc,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001129 coll->status[i].bytes);
1130 coll->num_done++;
1131 kref_put(&coll->kref, rbd_coll_release);
1132 }
1133 spin_unlock_irq(q->queue_lock);
1134}
1135
Alex Elder725afc92012-11-08 08:01:39 -06001136static void rbd_coll_end_req(struct rbd_request *rbd_req,
Alex Elder8986cb32012-11-08 08:01:39 -06001137 s32 ret, u64 len)
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001138{
Alex Elder725afc92012-11-08 08:01:39 -06001139 rbd_coll_end_req_index(rbd_req->rq,
1140 rbd_req->coll, rbd_req->coll_index,
1141 ret, len);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001142}
1143
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001144/*
1145 * Send ceph osd request
1146 */
1147static int rbd_do_request(struct request *rq,
Alex Elder0ce1a792012-07-03 16:01:18 -05001148 struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001149 struct ceph_snap_context *snapc,
1150 u64 snapid,
Alex Elderaded07e2012-07-03 16:01:18 -05001151 const char *object_name, u64 ofs, u64 len,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001152 struct bio *bio,
1153 struct page **pages,
1154 int num_pages,
1155 int flags,
Alex Elder30573d62012-11-13 21:11:15 -06001156 struct ceph_osd_req_op *op,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001157 struct rbd_req_coll *coll,
1158 int coll_index,
Alex Elder5f29ddd2012-11-08 08:01:39 -06001159 void (*rbd_cb)(struct ceph_osd_request *,
1160 struct ceph_msg *),
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001161 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001162{
Alex Elder1dbb4392012-01-24 10:08:37 -06001163 struct ceph_osd_client *osdc;
Alex Elder2e53c6c2012-11-30 09:59:47 -06001164 struct ceph_osd_request *osd_req;
1165 struct rbd_request *rbd_req = NULL;
1166 struct timespec mtime = CURRENT_TIME;
1167 int ret;
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001168
Alex Elderf7760da2012-10-20 22:17:27 -05001169 dout("rbd_do_request object_name=%s ofs=%llu len=%llu coll=%p[%d]\n",
1170 object_name, (unsigned long long) ofs,
1171 (unsigned long long) len, coll, coll_index);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001172
Alex Elder0ce1a792012-07-03 16:01:18 -05001173 osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder30573d62012-11-13 21:11:15 -06001174 osd_req = ceph_osdc_alloc_request(osdc, snapc, 1, false, GFP_NOIO);
Alex Elder2e53c6c2012-11-30 09:59:47 -06001175 if (!osd_req)
1176 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001177
Alex Elderd178a9e2012-11-13 21:11:15 -06001178 osd_req->r_flags = flags;
Alex Elder54a54002012-11-13 21:11:15 -06001179 osd_req->r_pages = pages;
1180 if (bio) {
1181 osd_req->r_bio = bio;
1182 bio_get(osd_req->r_bio);
1183 }
Alex Elder2e53c6c2012-11-30 09:59:47 -06001184
Alex Elder18216652012-11-30 09:59:47 -06001185 if (coll) {
Alex Elder2e53c6c2012-11-30 09:59:47 -06001186 ret = -ENOMEM;
1187 rbd_req = kmalloc(sizeof(*rbd_req), GFP_NOIO);
1188 if (!rbd_req)
1189 goto done_osd_req;
1190
1191 rbd_req->rq = rq;
1192 rbd_req->bio = bio;
1193 rbd_req->pages = pages;
1194 rbd_req->len = len;
1195 rbd_req->coll = coll;
Alex Elder18216652012-11-30 09:59:47 -06001196 rbd_req->coll_index = coll_index;
Alex Elder2e53c6c2012-11-30 09:59:47 -06001197 }
1198
Alex Elder5f29ddd2012-11-08 08:01:39 -06001199 osd_req->r_callback = rbd_cb;
Alex Elder5f29ddd2012-11-08 08:01:39 -06001200 osd_req->r_priv = rbd_req;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001201
Alex Elder5f29ddd2012-11-08 08:01:39 -06001202 strncpy(osd_req->r_oid, object_name, sizeof(osd_req->r_oid));
1203 osd_req->r_oid_len = strlen(osd_req->r_oid);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001204
Alex Elder0903e872012-11-14 12:25:19 -06001205 osd_req->r_file_layout = rbd_dev->layout; /* struct */
Alex Eldere01e7922012-11-14 12:25:18 -06001206 osd_req->r_num_pages = calc_pages_for(ofs, len);
1207 osd_req->r_page_alignment = ofs & ~PAGE_MASK;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001208
Alex Elder30573d62012-11-13 21:11:15 -06001209 ceph_osdc_build_request(osd_req, ofs, len, 1, op,
Alex Elderae7ca4a32012-11-13 21:11:15 -06001210 snapc, snapid, &mtime);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001211
Alex Elder8b84de72012-11-20 14:17:17 -06001212 if (op->op == CEPH_OSD_OP_WATCH && op->watch.flag) {
Alex Elder5f29ddd2012-11-08 08:01:39 -06001213 ceph_osdc_set_request_linger(osdc, osd_req);
Alex Elder8b84de72012-11-20 14:17:17 -06001214 rbd_dev->watch_request = osd_req;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001215 }
1216
Alex Elder5f29ddd2012-11-08 08:01:39 -06001217 ret = ceph_osdc_start_request(osdc, osd_req, false);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001218 if (ret < 0)
1219 goto done_err;
1220
1221 if (!rbd_cb) {
Alex Elder5f29ddd2012-11-08 08:01:39 -06001222 u64 version;
1223
1224 ret = ceph_osdc_wait_request(osdc, osd_req);
1225 version = le64_to_cpu(osd_req->r_reassert_version.version);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001226 if (ver)
Alex Elder5f29ddd2012-11-08 08:01:39 -06001227 *ver = version;
1228 dout("reassert_ver=%llu\n", (unsigned long long) version);
1229 ceph_osdc_put_request(osd_req);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001230 }
1231 return ret;
1232
1233done_err:
Alex Elder2e53c6c2012-11-30 09:59:47 -06001234 if (bio)
1235 bio_chain_put(osd_req->r_bio);
Alex Elder725afc92012-11-08 08:01:39 -06001236 kfree(rbd_req);
Alex Elder2e53c6c2012-11-30 09:59:47 -06001237done_osd_req:
1238 ceph_osdc_put_request(osd_req);
1239
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001240 return ret;
1241}
1242
1243/*
1244 * Ceph osd op callback
1245 */
Alex Elder5f29ddd2012-11-08 08:01:39 -06001246static void rbd_req_cb(struct ceph_osd_request *osd_req, struct ceph_msg *msg)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001247{
Alex Elder5f29ddd2012-11-08 08:01:39 -06001248 struct rbd_request *rbd_req = osd_req->r_priv;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001249 struct ceph_osd_reply_head *replyhead;
1250 struct ceph_osd_op *op;
Alex Elder8986cb32012-11-08 08:01:39 -06001251 s32 rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001252 u64 bytes;
1253 int read_op;
1254
1255 /* parse reply */
1256 replyhead = msg->front.iov_base;
1257 WARN_ON(le32_to_cpu(replyhead->num_ops) == 0);
1258 op = (void *)(replyhead + 1);
Alex Elder8986cb32012-11-08 08:01:39 -06001259 rc = (s32)le32_to_cpu(replyhead->result);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001260 bytes = le64_to_cpu(op->extent.length);
Dan Carpenter895cfcc2012-06-06 09:15:33 -05001261 read_op = (le16_to_cpu(op->op) == CEPH_OSD_OP_READ);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001262
Alex Elderbd919d42012-07-13 20:35:11 -05001263 dout("rbd_req_cb bytes=%llu readop=%d rc=%d\n",
1264 (unsigned long long) bytes, read_op, (int) rc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001265
Alex Elder8986cb32012-11-08 08:01:39 -06001266 if (rc == (s32)-ENOENT && read_op) {
Alex Elder725afc92012-11-08 08:01:39 -06001267 zero_bio_chain(rbd_req->bio, 0);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001268 rc = 0;
Alex Elder725afc92012-11-08 08:01:39 -06001269 } else if (rc == 0 && read_op && bytes < rbd_req->len) {
1270 zero_bio_chain(rbd_req->bio, bytes);
1271 bytes = rbd_req->len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001272 }
1273
Alex Elder725afc92012-11-08 08:01:39 -06001274 rbd_coll_end_req(rbd_req, rc, bytes);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001275
Alex Elder725afc92012-11-08 08:01:39 -06001276 if (rbd_req->bio)
1277 bio_chain_put(rbd_req->bio);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001278
Alex Elder5f29ddd2012-11-08 08:01:39 -06001279 ceph_osdc_put_request(osd_req);
Alex Elder725afc92012-11-08 08:01:39 -06001280 kfree(rbd_req);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001281}
1282
Alex Elder5f29ddd2012-11-08 08:01:39 -06001283static void rbd_simple_req_cb(struct ceph_osd_request *osd_req,
1284 struct ceph_msg *msg)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001285{
Alex Elder5f29ddd2012-11-08 08:01:39 -06001286 ceph_osdc_put_request(osd_req);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001287}
1288
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001289/*
1290 * Do a synchronous ceph osd operation
1291 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001292static int rbd_req_sync_op(struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001293 int flags,
Alex Elder30573d62012-11-13 21:11:15 -06001294 struct ceph_osd_req_op *op,
Alex Elderaded07e2012-07-03 16:01:18 -05001295 const char *object_name,
Alex Elderf8d4de62012-07-03 16:01:19 -05001296 u64 ofs, u64 inbound_size,
1297 char *inbound,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001298 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001299{
1300 int ret;
1301 struct page **pages;
1302 int num_pages;
Alex Elder913d2fd2012-06-26 12:57:03 -07001303
Alex Elder30573d62012-11-13 21:11:15 -06001304 rbd_assert(op != NULL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001305
Alex Elderf8d4de62012-07-03 16:01:19 -05001306 num_pages = calc_pages_for(ofs, inbound_size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001307 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
Dan Carpenterb8d06382010-10-11 21:14:23 +02001308 if (IS_ERR(pages))
1309 return PTR_ERR(pages);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001310
Alex Elder25704ac2012-11-09 08:43:16 -06001311 ret = rbd_do_request(NULL, rbd_dev, NULL, CEPH_NOSNAP,
Alex Elderf8d4de62012-07-03 16:01:19 -05001312 object_name, ofs, inbound_size, NULL,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001313 pages, num_pages,
1314 flags,
Alex Elder30573d62012-11-13 21:11:15 -06001315 op,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001316 NULL, 0,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001317 NULL,
Alex Elder8b84de72012-11-20 14:17:17 -06001318 ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001319 if (ret < 0)
Alex Elder913d2fd2012-06-26 12:57:03 -07001320 goto done;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001321
Alex Elderf8d4de62012-07-03 16:01:19 -05001322 if ((flags & CEPH_OSD_FLAG_READ) && inbound)
1323 ret = ceph_copy_from_page_vector(pages, inbound, ofs, ret);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001324
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001325done:
1326 ceph_release_page_vector(pages, num_pages);
1327 return ret;
1328}
1329
1330/*
1331 * Do an asynchronous ceph osd operation
1332 */
1333static int rbd_do_op(struct request *rq,
Alex Elder0ce1a792012-07-03 16:01:18 -05001334 struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001335 struct ceph_snap_context *snapc,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001336 u64 ofs, u64 len,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001337 struct bio *bio,
1338 struct rbd_req_coll *coll,
1339 int coll_index)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001340{
1341 char *seg_name;
1342 u64 seg_ofs;
1343 u64 seg_len;
1344 int ret;
Alex Elder139b4312012-11-13 21:11:15 -06001345 struct ceph_osd_req_op *op;
Alex Elderff2e4bb2012-10-10 18:59:29 -07001346 int opcode;
1347 int flags;
Alex Elder46342462012-10-10 18:59:29 -07001348 u64 snapid;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001349
Alex Elder65ccfe22012-08-09 10:33:26 -07001350 seg_name = rbd_segment_name(rbd_dev, ofs);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001351 if (!seg_name)
1352 return -ENOMEM;
Alex Elder65ccfe22012-08-09 10:33:26 -07001353 seg_len = rbd_segment_length(rbd_dev, ofs, len);
1354 seg_ofs = rbd_segment_offset(rbd_dev, ofs);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001355
Alex Elderff2e4bb2012-10-10 18:59:29 -07001356 if (rq_data_dir(rq) == WRITE) {
1357 opcode = CEPH_OSD_OP_WRITE;
1358 flags = CEPH_OSD_FLAG_WRITE|CEPH_OSD_FLAG_ONDISK;
Alex Elder46342462012-10-10 18:59:29 -07001359 snapid = CEPH_NOSNAP;
Alex Elderff2e4bb2012-10-10 18:59:29 -07001360 } else {
1361 opcode = CEPH_OSD_OP_READ;
1362 flags = CEPH_OSD_FLAG_READ;
Alex Eldera7b4c652012-11-09 08:43:15 -06001363 rbd_assert(!snapc);
Alex Elder0d7dbfc2012-10-25 23:34:41 -05001364 snapid = rbd_dev->spec->snap_id;
Alex Elderff2e4bb2012-10-10 18:59:29 -07001365 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001366
Alex Elder57cfc102012-06-26 12:57:03 -07001367 ret = -ENOMEM;
Alex Elder8d23bf22012-11-19 22:55:21 -06001368 op = rbd_osd_req_op_create(opcode, seg_ofs, seg_len);
Alex Elder139b4312012-11-13 21:11:15 -06001369 if (!op)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001370 goto done;
1371
1372 /* we've taken care of segment sizes earlier when we
1373 cloned the bios. We should never have a segment
1374 truncated at this point */
Alex Elderaafb2302012-09-06 16:00:54 -05001375 rbd_assert(seg_len == len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001376
1377 ret = rbd_do_request(rq, rbd_dev, snapc, snapid,
1378 seg_name, seg_ofs, seg_len,
1379 bio,
1380 NULL, 0,
1381 flags,
Alex Elder30573d62012-11-13 21:11:15 -06001382 op,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001383 coll, coll_index,
Alex Elder8b84de72012-11-20 14:17:17 -06001384 rbd_req_cb, NULL);
Alex Eldercd323ac2012-11-08 08:01:39 -06001385 if (ret < 0)
1386 rbd_coll_end_req_index(rq, coll, coll_index,
1387 (s32)ret, seg_len);
Alex Elder8d23bf22012-11-19 22:55:21 -06001388 rbd_osd_req_op_destroy(op);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001389done:
1390 kfree(seg_name);
1391 return ret;
1392}
1393
1394/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001395 * Request sync osd read
1396 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001397static int rbd_req_sync_read(struct rbd_device *rbd_dev,
Alex Elderaded07e2012-07-03 16:01:18 -05001398 const char *object_name,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001399 u64 ofs, u64 len,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001400 char *buf,
1401 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001402{
Alex Elder139b4312012-11-13 21:11:15 -06001403 struct ceph_osd_req_op *op;
Alex Elder913d2fd2012-06-26 12:57:03 -07001404 int ret;
1405
Alex Elder8d23bf22012-11-19 22:55:21 -06001406 op = rbd_osd_req_op_create(CEPH_OSD_OP_READ, ofs, len);
Alex Elder139b4312012-11-13 21:11:15 -06001407 if (!op)
Alex Elder913d2fd2012-06-26 12:57:03 -07001408 return -ENOMEM;
1409
Alex Elder25704ac2012-11-09 08:43:16 -06001410 ret = rbd_req_sync_op(rbd_dev, CEPH_OSD_FLAG_READ,
Alex Elder8b84de72012-11-20 14:17:17 -06001411 op, object_name, ofs, len, buf, ver);
Alex Elder8d23bf22012-11-19 22:55:21 -06001412 rbd_osd_req_op_destroy(op);
Alex Elder913d2fd2012-06-26 12:57:03 -07001413
1414 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001415}
1416
1417/*
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001418 * Request sync osd watch
1419 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001420static int rbd_req_sync_notify_ack(struct rbd_device *rbd_dev,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001421 u64 ver,
Alex Elder7f0a24d2012-07-25 09:32:40 -05001422 u64 notify_id)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001423{
Alex Elder139b4312012-11-13 21:11:15 -06001424 struct ceph_osd_req_op *op;
Sage Weil11f77002011-05-12 16:13:54 -07001425 int ret;
1426
Alex Elder5efea492012-11-19 22:55:21 -06001427 op = rbd_osd_req_op_create(CEPH_OSD_OP_NOTIFY_ACK, notify_id, ver);
Alex Elder139b4312012-11-13 21:11:15 -06001428 if (!op)
Alex Elder57cfc102012-06-26 12:57:03 -07001429 return -ENOMEM;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001430
Alex Elder0ce1a792012-07-03 16:01:18 -05001431 ret = rbd_do_request(NULL, rbd_dev, NULL, CEPH_NOSNAP,
Alex Elder7f0a24d2012-07-25 09:32:40 -05001432 rbd_dev->header_name, 0, 0, NULL,
Alex Elderad4f2322012-07-03 16:01:19 -05001433 NULL, 0,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001434 CEPH_OSD_FLAG_READ,
Alex Elder30573d62012-11-13 21:11:15 -06001435 op,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001436 NULL, 0,
Alex Elder8b84de72012-11-20 14:17:17 -06001437 rbd_simple_req_cb, NULL);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001438
Alex Elder5efea492012-11-19 22:55:21 -06001439 rbd_osd_req_op_destroy(op);
1440
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001441 return ret;
1442}
1443
1444static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
1445{
Alex Elder0ce1a792012-07-03 16:01:18 -05001446 struct rbd_device *rbd_dev = (struct rbd_device *)data;
Josh Durgina71b8912011-12-05 18:10:44 -08001447 u64 hver;
Sage Weil13143d22011-05-12 16:08:30 -07001448 int rc;
1449
Alex Elder0ce1a792012-07-03 16:01:18 -05001450 if (!rbd_dev)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001451 return;
1452
Alex Elderbd919d42012-07-13 20:35:11 -05001453 dout("rbd_watch_cb %s notify_id=%llu opcode=%u\n",
1454 rbd_dev->header_name, (unsigned long long) notify_id,
1455 (unsigned int) opcode);
Alex Elder117973f2012-08-31 17:29:55 -05001456 rc = rbd_dev_refresh(rbd_dev, &hver);
Sage Weil13143d22011-05-12 16:08:30 -07001457 if (rc)
Alex Elder06ecc6c2012-11-01 10:17:15 -05001458 rbd_warn(rbd_dev, "got notification but failed to "
1459 " update snaps: %d\n", rc);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001460
Alex Elder7f0a24d2012-07-25 09:32:40 -05001461 rbd_req_sync_notify_ack(rbd_dev, hver, notify_id);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001462}
1463
1464/*
Alex Elder907703d2012-11-13 21:11:15 -06001465 * Request sync osd watch/unwatch. The value of "start" determines
1466 * whether a watch request is being initiated or torn down.
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001467 */
Alex Elder907703d2012-11-13 21:11:15 -06001468static int rbd_req_sync_watch(struct rbd_device *rbd_dev, int start)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001469{
Alex Elder5efea492012-11-19 22:55:21 -06001470 struct ceph_osd_req_op *op;
1471 int ret = 0;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001472
Alex Elder907703d2012-11-13 21:11:15 -06001473 if (start) {
1474 struct ceph_osd_client *osdc;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001475
Alex Elder907703d2012-11-13 21:11:15 -06001476 osdc = &rbd_dev->rbd_client->client->osdc;
1477 ret = ceph_osdc_create_event(osdc, rbd_watch_cb, 0, rbd_dev,
1478 &rbd_dev->watch_event);
1479 if (ret < 0)
Alex Elder5efea492012-11-19 22:55:21 -06001480 return ret;
Alex Elder5efea492012-11-19 22:55:21 -06001481 } else {
1482 rbd_assert(rbd_dev->watch_request != NULL);
Alex Elder907703d2012-11-13 21:11:15 -06001483 }
1484
Alex Elder5efea492012-11-19 22:55:21 -06001485 op = rbd_osd_req_op_create(CEPH_OSD_OP_WATCH,
1486 rbd_dev->watch_event->cookie,
1487 rbd_dev->header.obj_version, start);
1488 if (op)
1489 ret = rbd_req_sync_op(rbd_dev,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001490 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
Alex Elder907703d2012-11-13 21:11:15 -06001491 op, rbd_dev->header_name,
Alex Elder8b84de72012-11-20 14:17:17 -06001492 0, 0, NULL, NULL);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001493
Alex Elder5efea492012-11-19 22:55:21 -06001494 /* Cancel the event if we're tearing down, or on error */
1495
1496 if (!start || !op || ret < 0) {
Alex Elder907703d2012-11-13 21:11:15 -06001497 ceph_osdc_cancel_event(rbd_dev->watch_event);
1498 rbd_dev->watch_event = NULL;
1499 }
Alex Elder5efea492012-11-19 22:55:21 -06001500 rbd_osd_req_op_destroy(op);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001501
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001502 return ret;
1503}
1504
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001505/*
Alex Elder3cb4a682012-06-26 12:57:03 -07001506 * Synchronous osd object method call
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001507 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001508static int rbd_req_sync_exec(struct rbd_device *rbd_dev,
Alex Elderaded07e2012-07-03 16:01:18 -05001509 const char *object_name,
1510 const char *class_name,
1511 const char *method_name,
Alex Elder3cb4a682012-06-26 12:57:03 -07001512 const char *outbound,
1513 size_t outbound_size,
Alex Elderf8d4de62012-07-03 16:01:19 -05001514 char *inbound,
1515 size_t inbound_size,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001516 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001517{
Alex Elder139b4312012-11-13 21:11:15 -06001518 struct ceph_osd_req_op *op;
Alex Elder57cfc102012-06-26 12:57:03 -07001519 int ret;
1520
Alex Elder3cb4a682012-06-26 12:57:03 -07001521 /*
1522 * Any input parameters required by the method we're calling
1523 * will be sent along with the class and method names as
1524 * part of the message payload. That data and its size are
1525 * supplied via the indata and indata_len fields (named from
1526 * the perspective of the server side) in the OSD request
1527 * operation.
1528 */
Alex Elder2647ba32012-11-19 22:55:21 -06001529 op = rbd_osd_req_op_create(CEPH_OSD_OP_CALL, class_name,
1530 method_name, outbound, outbound_size);
Alex Elder139b4312012-11-13 21:11:15 -06001531 if (!op)
Alex Elder57cfc102012-06-26 12:57:03 -07001532 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001533
Alex Elder30573d62012-11-13 21:11:15 -06001534 ret = rbd_req_sync_op(rbd_dev, CEPH_OSD_FLAG_READ, op,
Alex Elderf8d4de62012-07-03 16:01:19 -05001535 object_name, 0, inbound_size, inbound,
Alex Elder8b84de72012-11-20 14:17:17 -06001536 ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001537
Alex Elder2647ba32012-11-19 22:55:21 -06001538 rbd_osd_req_op_destroy(op);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001539
1540 dout("cls_exec returned %d\n", ret);
1541 return ret;
1542}
1543
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001544static struct rbd_req_coll *rbd_alloc_coll(int num_reqs)
1545{
1546 struct rbd_req_coll *coll =
1547 kzalloc(sizeof(struct rbd_req_coll) +
1548 sizeof(struct rbd_req_status) * num_reqs,
1549 GFP_ATOMIC);
1550
1551 if (!coll)
1552 return NULL;
1553 coll->total = num_reqs;
1554 kref_init(&coll->kref);
1555 return coll;
1556}
1557
Alex Elder8295cda2012-11-08 08:01:39 -06001558static int rbd_dev_do_request(struct request *rq,
1559 struct rbd_device *rbd_dev,
1560 struct ceph_snap_context *snapc,
1561 u64 ofs, unsigned int size,
1562 struct bio *bio_chain)
1563{
1564 int num_segs;
1565 struct rbd_req_coll *coll;
1566 unsigned int bio_offset;
1567 int cur_seg = 0;
1568
1569 dout("%s 0x%x bytes at 0x%llx\n",
1570 rq_data_dir(rq) == WRITE ? "write" : "read",
1571 size, (unsigned long long) blk_rq_pos(rq) * SECTOR_SIZE);
1572
1573 num_segs = rbd_get_num_segments(&rbd_dev->header, ofs, size);
1574 if (num_segs <= 0)
1575 return num_segs;
1576
1577 coll = rbd_alloc_coll(num_segs);
1578 if (!coll)
1579 return -ENOMEM;
1580
1581 bio_offset = 0;
1582 do {
1583 u64 limit = rbd_segment_length(rbd_dev, ofs, size);
1584 unsigned int clone_size;
1585 struct bio *bio_clone;
1586
1587 BUG_ON(limit > (u64)UINT_MAX);
1588 clone_size = (unsigned int)limit;
1589 dout("bio_chain->bi_vcnt=%hu\n", bio_chain->bi_vcnt);
1590
1591 kref_get(&coll->kref);
1592
1593 /* Pass a cloned bio chain via an osd request */
1594
1595 bio_clone = bio_chain_clone_range(&bio_chain,
1596 &bio_offset, clone_size,
1597 GFP_ATOMIC);
1598 if (bio_clone)
1599 (void)rbd_do_op(rq, rbd_dev, snapc,
1600 ofs, clone_size,
1601 bio_clone, coll, cur_seg);
1602 else
1603 rbd_coll_end_req_index(rq, coll, cur_seg,
1604 (s32)-ENOMEM,
1605 clone_size);
1606 size -= clone_size;
1607 ofs += clone_size;
1608
1609 cur_seg++;
1610 } while (size > 0);
1611 kref_put(&coll->kref, rbd_coll_release);
1612
1613 return 0;
1614}
1615
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001616/*
1617 * block device queue callback
1618 */
1619static void rbd_rq_fn(struct request_queue *q)
1620{
1621 struct rbd_device *rbd_dev = q->queuedata;
Alex Elderb395e8b2012-11-08 08:01:39 -06001622 bool read_only = rbd_dev->mapping.read_only;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001623 struct request *rq;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001624
Alex Elder00f1f362012-02-07 12:03:36 -06001625 while ((rq = blk_fetch_request(q))) {
Alex Elderb395e8b2012-11-08 08:01:39 -06001626 struct ceph_snap_context *snapc = NULL;
1627 unsigned int size = 0;
Alex Elder8295cda2012-11-08 08:01:39 -06001628 int result;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001629
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001630 dout("fetched request\n");
1631
Alex Elderb395e8b2012-11-08 08:01:39 -06001632 /* Filter out block requests we don't understand */
1633
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001634 if ((rq->cmd_type != REQ_TYPE_FS)) {
1635 __blk_end_request_all(rq, 0);
Alex Elder00f1f362012-02-07 12:03:36 -06001636 continue;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001637 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001638 spin_unlock_irq(q->queue_lock);
1639
Alex Eldera7b4c652012-11-09 08:43:15 -06001640 /* Write requests need a reference to the snapshot context */
Alex Elderb395e8b2012-11-08 08:01:39 -06001641
Alex Eldera7b4c652012-11-09 08:43:15 -06001642 if (rq_data_dir(rq) == WRITE) {
1643 result = -EROFS;
1644 if (read_only) /* Can't write to a read-only device */
1645 goto out_end_request;
Alex Elderb395e8b2012-11-08 08:01:39 -06001646
Alex Eldera7b4c652012-11-09 08:43:15 -06001647 /*
1648 * Note that each osd request will take its
1649 * own reference to the snapshot context
1650 * supplied. The reference we take here
1651 * just guarantees the one we provide stays
1652 * valid.
1653 */
1654 down_read(&rbd_dev->header_rwsem);
Alex Elderb395e8b2012-11-08 08:01:39 -06001655 snapc = ceph_get_snap_context(rbd_dev->header.snapc);
Alex Eldera7b4c652012-11-09 08:43:15 -06001656 up_read(&rbd_dev->header_rwsem);
Alex Elderb395e8b2012-11-08 08:01:39 -06001657 rbd_assert(snapc != NULL);
Alex Eldera7b4c652012-11-09 08:43:15 -06001658 } else if (!atomic_read(&rbd_dev->exists)) {
Alex Elderb395e8b2012-11-08 08:01:39 -06001659 rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP);
1660 dout("request for non-existent snapshot");
1661 result = -ENXIO;
1662 goto out_end_request;
1663 }
Alex Elderf7760da2012-10-20 22:17:27 -05001664
Alex Elderb395e8b2012-11-08 08:01:39 -06001665 size = blk_rq_bytes(rq);
1666 result = rbd_dev_do_request(rq, rbd_dev, snapc,
1667 blk_rq_pos(rq) * SECTOR_SIZE,
1668 size, rq->bio);
1669out_end_request:
Alex Eldera7b4c652012-11-09 08:43:15 -06001670 if (snapc)
1671 ceph_put_snap_context(snapc);
Alex Elder8295cda2012-11-08 08:01:39 -06001672 spin_lock_irq(q->queue_lock);
1673 if (!size || result < 0)
1674 __blk_end_request_all(rq, result);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001675 }
1676}
1677
1678/*
1679 * a queue callback. Makes sure that we don't create a bio that spans across
1680 * multiple osd objects. One exception would be with a single page bios,
Alex Elderf7760da2012-10-20 22:17:27 -05001681 * which we handle later at bio_chain_clone_range()
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001682 */
1683static int rbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd,
1684 struct bio_vec *bvec)
1685{
1686 struct rbd_device *rbd_dev = q->queuedata;
Alex Eldere5cfeed22012-10-20 22:17:27 -05001687 sector_t sector_offset;
1688 sector_t sectors_per_obj;
1689 sector_t obj_sector_offset;
1690 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001691
Alex Eldere5cfeed22012-10-20 22:17:27 -05001692 /*
1693 * Find how far into its rbd object the partition-relative
1694 * bio start sector is to offset relative to the enclosing
1695 * device.
1696 */
1697 sector_offset = get_start_sect(bmd->bi_bdev) + bmd->bi_sector;
1698 sectors_per_obj = 1 << (rbd_dev->header.obj_order - SECTOR_SHIFT);
1699 obj_sector_offset = sector_offset & (sectors_per_obj - 1);
Alex Elder593a9e72012-02-07 12:03:37 -06001700
Alex Eldere5cfeed22012-10-20 22:17:27 -05001701 /*
1702 * Compute the number of bytes from that offset to the end
1703 * of the object. Account for what's already used by the bio.
1704 */
1705 ret = (int) (sectors_per_obj - obj_sector_offset) << SECTOR_SHIFT;
1706 if (ret > bmd->bi_size)
1707 ret -= bmd->bi_size;
1708 else
1709 ret = 0;
1710
1711 /*
1712 * Don't send back more than was asked for. And if the bio
1713 * was empty, let the whole thing through because: "Note
1714 * that a block device *must* allow a single page to be
1715 * added to an empty bio."
1716 */
1717 rbd_assert(bvec->bv_len <= PAGE_SIZE);
1718 if (ret > (int) bvec->bv_len || !bmd->bi_size)
1719 ret = (int) bvec->bv_len;
1720
1721 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001722}
1723
1724static void rbd_free_disk(struct rbd_device *rbd_dev)
1725{
1726 struct gendisk *disk = rbd_dev->disk;
1727
1728 if (!disk)
1729 return;
1730
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001731 if (disk->flags & GENHD_FL_UP)
1732 del_gendisk(disk);
1733 if (disk->queue)
1734 blk_cleanup_queue(disk->queue);
1735 put_disk(disk);
1736}
1737
1738/*
Alex Elder4156d992012-08-02 11:29:46 -05001739 * Read the complete header for the given rbd device.
1740 *
1741 * Returns a pointer to a dynamically-allocated buffer containing
1742 * the complete and validated header. Caller can pass the address
1743 * of a variable that will be filled in with the version of the
1744 * header object at the time it was read.
1745 *
1746 * Returns a pointer-coded errno if a failure occurs.
1747 */
1748static struct rbd_image_header_ondisk *
1749rbd_dev_v1_header_read(struct rbd_device *rbd_dev, u64 *version)
1750{
1751 struct rbd_image_header_ondisk *ondisk = NULL;
1752 u32 snap_count = 0;
1753 u64 names_size = 0;
1754 u32 want_count;
1755 int ret;
1756
1757 /*
1758 * The complete header will include an array of its 64-bit
1759 * snapshot ids, followed by the names of those snapshots as
1760 * a contiguous block of NUL-terminated strings. Note that
1761 * the number of snapshots could change by the time we read
1762 * it in, in which case we re-read it.
1763 */
1764 do {
1765 size_t size;
1766
1767 kfree(ondisk);
1768
1769 size = sizeof (*ondisk);
1770 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
1771 size += names_size;
1772 ondisk = kmalloc(size, GFP_KERNEL);
1773 if (!ondisk)
1774 return ERR_PTR(-ENOMEM);
1775
Alex Elder47756182012-11-09 08:43:15 -06001776 ret = rbd_req_sync_read(rbd_dev, rbd_dev->header_name,
Alex Elder4156d992012-08-02 11:29:46 -05001777 0, size,
1778 (char *) ondisk, version);
1779
1780 if (ret < 0)
1781 goto out_err;
1782 if (WARN_ON((size_t) ret < size)) {
1783 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05001784 rbd_warn(rbd_dev, "short header read (want %zd got %d)",
1785 size, ret);
Alex Elder4156d992012-08-02 11:29:46 -05001786 goto out_err;
1787 }
1788 if (!rbd_dev_ondisk_valid(ondisk)) {
1789 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05001790 rbd_warn(rbd_dev, "invalid header");
Alex Elder4156d992012-08-02 11:29:46 -05001791 goto out_err;
1792 }
1793
1794 names_size = le64_to_cpu(ondisk->snap_names_len);
1795 want_count = snap_count;
1796 snap_count = le32_to_cpu(ondisk->snap_count);
1797 } while (snap_count != want_count);
1798
1799 return ondisk;
1800
1801out_err:
1802 kfree(ondisk);
1803
1804 return ERR_PTR(ret);
1805}
1806
1807/*
1808 * reload the ondisk the header
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001809 */
1810static int rbd_read_header(struct rbd_device *rbd_dev,
1811 struct rbd_image_header *header)
1812{
Alex Elder4156d992012-08-02 11:29:46 -05001813 struct rbd_image_header_ondisk *ondisk;
1814 u64 ver = 0;
1815 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001816
Alex Elder4156d992012-08-02 11:29:46 -05001817 ondisk = rbd_dev_v1_header_read(rbd_dev, &ver);
1818 if (IS_ERR(ondisk))
1819 return PTR_ERR(ondisk);
1820 ret = rbd_header_from_disk(header, ondisk);
1821 if (ret >= 0)
1822 header->obj_version = ver;
1823 kfree(ondisk);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001824
Alex Elder4156d992012-08-02 11:29:46 -05001825 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001826}
1827
Alex Elder41f38c22012-10-25 23:34:40 -05001828static void rbd_remove_all_snaps(struct rbd_device *rbd_dev)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001829{
1830 struct rbd_snap *snap;
Alex Eldera0593292012-07-19 09:09:27 -05001831 struct rbd_snap *next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001832
Alex Eldera0593292012-07-19 09:09:27 -05001833 list_for_each_entry_safe(snap, next, &rbd_dev->snaps, node)
Alex Elder41f38c22012-10-25 23:34:40 -05001834 rbd_remove_snap_dev(snap);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001835}
1836
Alex Elder94785542012-10-09 13:50:17 -07001837static void rbd_update_mapping_size(struct rbd_device *rbd_dev)
1838{
1839 sector_t size;
1840
Alex Elder0d7dbfc2012-10-25 23:34:41 -05001841 if (rbd_dev->spec->snap_id != CEPH_NOSNAP)
Alex Elder94785542012-10-09 13:50:17 -07001842 return;
1843
1844 size = (sector_t) rbd_dev->header.image_size / SECTOR_SIZE;
1845 dout("setting size to %llu sectors", (unsigned long long) size);
1846 rbd_dev->mapping.size = (u64) size;
1847 set_capacity(rbd_dev->disk, size);
1848}
1849
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001850/*
1851 * only read the first part of the ondisk header, without the snaps info
1852 */
Alex Elder117973f2012-08-31 17:29:55 -05001853static int rbd_dev_v1_refresh(struct rbd_device *rbd_dev, u64 *hver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001854{
1855 int ret;
1856 struct rbd_image_header h;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001857
1858 ret = rbd_read_header(rbd_dev, &h);
1859 if (ret < 0)
1860 return ret;
1861
Josh Durgina51aa0c2011-12-05 10:35:04 -08001862 down_write(&rbd_dev->header_rwsem);
1863
Alex Elder94785542012-10-09 13:50:17 -07001864 /* Update image size, and check for resize of mapped image */
1865 rbd_dev->header.image_size = h.image_size;
1866 rbd_update_mapping_size(rbd_dev);
Sage Weil9db4b3e2011-04-19 22:49:06 -07001867
Alex Elder849b4262012-07-09 21:04:24 -05001868 /* rbd_dev->header.object_prefix shouldn't change */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001869 kfree(rbd_dev->header.snap_sizes);
Alex Elder849b4262012-07-09 21:04:24 -05001870 kfree(rbd_dev->header.snap_names);
Josh Durgind1d25642011-12-05 14:03:05 -08001871 /* osd requests may still refer to snapc */
1872 ceph_put_snap_context(rbd_dev->header.snapc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001873
Alex Elderb8136232012-07-25 09:32:41 -05001874 if (hver)
1875 *hver = h.obj_version;
Josh Durgina71b8912011-12-05 18:10:44 -08001876 rbd_dev->header.obj_version = h.obj_version;
Josh Durgin93a24e02011-12-05 10:41:28 -08001877 rbd_dev->header.image_size = h.image_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001878 rbd_dev->header.snapc = h.snapc;
1879 rbd_dev->header.snap_names = h.snap_names;
1880 rbd_dev->header.snap_sizes = h.snap_sizes;
Alex Elder849b4262012-07-09 21:04:24 -05001881 /* Free the extra copy of the object prefix */
1882 WARN_ON(strcmp(rbd_dev->header.object_prefix, h.object_prefix));
1883 kfree(h.object_prefix);
1884
Alex Elder304f6802012-08-31 17:29:52 -05001885 ret = rbd_dev_snaps_update(rbd_dev);
1886 if (!ret)
1887 ret = rbd_dev_snaps_register(rbd_dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001888
Josh Durginc6666012011-11-21 17:11:12 -08001889 up_write(&rbd_dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001890
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001891 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001892}
1893
Alex Elder117973f2012-08-31 17:29:55 -05001894static int rbd_dev_refresh(struct rbd_device *rbd_dev, u64 *hver)
Alex Elder1fe5e992012-07-25 09:32:41 -05001895{
1896 int ret;
1897
Alex Elder117973f2012-08-31 17:29:55 -05001898 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
Alex Elder1fe5e992012-07-25 09:32:41 -05001899 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Alex Elder117973f2012-08-31 17:29:55 -05001900 if (rbd_dev->image_format == 1)
1901 ret = rbd_dev_v1_refresh(rbd_dev, hver);
1902 else
1903 ret = rbd_dev_v2_refresh(rbd_dev, hver);
Alex Elder1fe5e992012-07-25 09:32:41 -05001904 mutex_unlock(&ctl_mutex);
1905
1906 return ret;
1907}
1908
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001909static int rbd_init_disk(struct rbd_device *rbd_dev)
1910{
1911 struct gendisk *disk;
1912 struct request_queue *q;
Alex Elder593a9e72012-02-07 12:03:37 -06001913 u64 segment_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001914
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001915 /* create gendisk info */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001916 disk = alloc_disk(RBD_MINORS_PER_MAJOR);
1917 if (!disk)
Alex Elder1fcdb8a2012-08-29 17:11:06 -05001918 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001919
Alex Elderf0f8cef2012-01-29 13:57:44 -06001920 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
Alex Elderde71a292012-07-03 16:01:19 -05001921 rbd_dev->dev_id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001922 disk->major = rbd_dev->major;
1923 disk->first_minor = 0;
1924 disk->fops = &rbd_bd_ops;
1925 disk->private_data = rbd_dev;
1926
1927 /* init rq */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001928 q = blk_init_queue(rbd_rq_fn, &rbd_dev->lock);
1929 if (!q)
1930 goto out_disk;
Josh Durgin029bcbd2011-07-22 11:35:23 -07001931
Alex Elder593a9e72012-02-07 12:03:37 -06001932 /* We use the default size, but let's be explicit about it. */
1933 blk_queue_physical_block_size(q, SECTOR_SIZE);
1934
Josh Durgin029bcbd2011-07-22 11:35:23 -07001935 /* set io sizes to object size */
Alex Elder593a9e72012-02-07 12:03:37 -06001936 segment_size = rbd_obj_bytes(&rbd_dev->header);
1937 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
1938 blk_queue_max_segment_size(q, segment_size);
1939 blk_queue_io_min(q, segment_size);
1940 blk_queue_io_opt(q, segment_size);
Josh Durgin029bcbd2011-07-22 11:35:23 -07001941
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001942 blk_queue_merge_bvec(q, rbd_merge_bvec);
1943 disk->queue = q;
1944
1945 q->queuedata = rbd_dev;
1946
1947 rbd_dev->disk = disk;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001948
Alex Elder12f02942012-08-29 17:11:07 -05001949 set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
1950
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001951 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001952out_disk:
1953 put_disk(disk);
Alex Elder1fcdb8a2012-08-29 17:11:06 -05001954
1955 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001956}
1957
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001958/*
1959 sysfs
1960*/
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001961
Alex Elder593a9e72012-02-07 12:03:37 -06001962static struct rbd_device *dev_to_rbd_dev(struct device *dev)
1963{
1964 return container_of(dev, struct rbd_device, dev);
1965}
1966
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001967static ssize_t rbd_size_show(struct device *dev,
1968 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001969{
Alex Elder593a9e72012-02-07 12:03:37 -06001970 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Josh Durgina51aa0c2011-12-05 10:35:04 -08001971 sector_t size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001972
Josh Durgina51aa0c2011-12-05 10:35:04 -08001973 down_read(&rbd_dev->header_rwsem);
1974 size = get_capacity(rbd_dev->disk);
1975 up_read(&rbd_dev->header_rwsem);
1976
1977 return sprintf(buf, "%llu\n", (unsigned long long) size * SECTOR_SIZE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001978}
1979
Alex Elder34b13182012-07-13 20:35:12 -05001980/*
1981 * Note this shows the features for whatever's mapped, which is not
1982 * necessarily the base image.
1983 */
1984static ssize_t rbd_features_show(struct device *dev,
1985 struct device_attribute *attr, char *buf)
1986{
1987 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
1988
1989 return sprintf(buf, "0x%016llx\n",
1990 (unsigned long long) rbd_dev->mapping.features);
1991}
1992
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001993static ssize_t rbd_major_show(struct device *dev,
1994 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001995{
Alex Elder593a9e72012-02-07 12:03:37 -06001996 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001997
1998 return sprintf(buf, "%d\n", rbd_dev->major);
1999}
2000
2001static ssize_t rbd_client_id_show(struct device *dev,
2002 struct device_attribute *attr, char *buf)
2003{
Alex Elder593a9e72012-02-07 12:03:37 -06002004 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002005
Alex Elder1dbb4392012-01-24 10:08:37 -06002006 return sprintf(buf, "client%lld\n",
2007 ceph_client_id(rbd_dev->rbd_client->client));
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002008}
2009
2010static ssize_t rbd_pool_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 Elder0d7dbfc2012-10-25 23:34:41 -05002015 return sprintf(buf, "%s\n", rbd_dev->spec->pool_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002016}
2017
Alex Elder9bb2f332012-07-12 10:46:35 -05002018static ssize_t rbd_pool_id_show(struct device *dev,
2019 struct device_attribute *attr, char *buf)
2020{
2021 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
2022
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002023 return sprintf(buf, "%llu\n",
2024 (unsigned long long) rbd_dev->spec->pool_id);
Alex Elder9bb2f332012-07-12 10:46:35 -05002025}
2026
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002027static ssize_t rbd_name_show(struct device *dev,
2028 struct device_attribute *attr, char *buf)
2029{
Alex Elder593a9e72012-02-07 12:03:37 -06002030 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002031
Alex Eldera92ffdf2012-10-30 19:40:33 -05002032 if (rbd_dev->spec->image_name)
2033 return sprintf(buf, "%s\n", rbd_dev->spec->image_name);
2034
2035 return sprintf(buf, "(unknown)\n");
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002036}
2037
Alex Elder589d30e2012-07-10 20:30:11 -05002038static ssize_t rbd_image_id_show(struct device *dev,
2039 struct device_attribute *attr, char *buf)
2040{
2041 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
2042
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002043 return sprintf(buf, "%s\n", rbd_dev->spec->image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05002044}
2045
Alex Elder34b13182012-07-13 20:35:12 -05002046/*
2047 * Shows the name of the currently-mapped snapshot (or
2048 * RBD_SNAP_HEAD_NAME for the base image).
2049 */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002050static ssize_t rbd_snap_show(struct device *dev,
2051 struct device_attribute *attr,
2052 char *buf)
2053{
Alex Elder593a9e72012-02-07 12:03:37 -06002054 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002055
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002056 return sprintf(buf, "%s\n", rbd_dev->spec->snap_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002057}
2058
Alex Elder86b00e02012-10-25 23:34:42 -05002059/*
2060 * For an rbd v2 image, shows the pool id, image id, and snapshot id
2061 * for the parent image. If there is no parent, simply shows
2062 * "(no parent image)".
2063 */
2064static ssize_t rbd_parent_show(struct device *dev,
2065 struct device_attribute *attr,
2066 char *buf)
2067{
2068 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
2069 struct rbd_spec *spec = rbd_dev->parent_spec;
2070 int count;
2071 char *bufp = buf;
2072
2073 if (!spec)
2074 return sprintf(buf, "(no parent image)\n");
2075
2076 count = sprintf(bufp, "pool_id %llu\npool_name %s\n",
2077 (unsigned long long) spec->pool_id, spec->pool_name);
2078 if (count < 0)
2079 return count;
2080 bufp += count;
2081
2082 count = sprintf(bufp, "image_id %s\nimage_name %s\n", spec->image_id,
2083 spec->image_name ? spec->image_name : "(unknown)");
2084 if (count < 0)
2085 return count;
2086 bufp += count;
2087
2088 count = sprintf(bufp, "snap_id %llu\nsnap_name %s\n",
2089 (unsigned long long) spec->snap_id, spec->snap_name);
2090 if (count < 0)
2091 return count;
2092 bufp += count;
2093
2094 count = sprintf(bufp, "overlap %llu\n", rbd_dev->parent_overlap);
2095 if (count < 0)
2096 return count;
2097 bufp += count;
2098
2099 return (ssize_t) (bufp - buf);
2100}
2101
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002102static ssize_t rbd_image_refresh(struct device *dev,
2103 struct device_attribute *attr,
2104 const char *buf,
2105 size_t size)
2106{
Alex Elder593a9e72012-02-07 12:03:37 -06002107 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Alex Elderb8136232012-07-25 09:32:41 -05002108 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002109
Alex Elder117973f2012-08-31 17:29:55 -05002110 ret = rbd_dev_refresh(rbd_dev, NULL);
Alex Elderb8136232012-07-25 09:32:41 -05002111
2112 return ret < 0 ? ret : size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002113}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002114
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002115static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
Alex Elder34b13182012-07-13 20:35:12 -05002116static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002117static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
2118static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
2119static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
Alex Elder9bb2f332012-07-12 10:46:35 -05002120static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002121static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
Alex Elder589d30e2012-07-10 20:30:11 -05002122static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002123static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
2124static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
Alex Elder86b00e02012-10-25 23:34:42 -05002125static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002126
2127static struct attribute *rbd_attrs[] = {
2128 &dev_attr_size.attr,
Alex Elder34b13182012-07-13 20:35:12 -05002129 &dev_attr_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002130 &dev_attr_major.attr,
2131 &dev_attr_client_id.attr,
2132 &dev_attr_pool.attr,
Alex Elder9bb2f332012-07-12 10:46:35 -05002133 &dev_attr_pool_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002134 &dev_attr_name.attr,
Alex Elder589d30e2012-07-10 20:30:11 -05002135 &dev_attr_image_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002136 &dev_attr_current_snap.attr,
Alex Elder86b00e02012-10-25 23:34:42 -05002137 &dev_attr_parent.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002138 &dev_attr_refresh.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002139 NULL
2140};
2141
2142static struct attribute_group rbd_attr_group = {
2143 .attrs = rbd_attrs,
2144};
2145
2146static const struct attribute_group *rbd_attr_groups[] = {
2147 &rbd_attr_group,
2148 NULL
2149};
2150
2151static void rbd_sysfs_dev_release(struct device *dev)
2152{
2153}
2154
2155static struct device_type rbd_device_type = {
2156 .name = "rbd",
2157 .groups = rbd_attr_groups,
2158 .release = rbd_sysfs_dev_release,
2159};
2160
2161
2162/*
2163 sysfs - snapshots
2164*/
2165
2166static ssize_t rbd_snap_size_show(struct device *dev,
2167 struct device_attribute *attr,
2168 char *buf)
2169{
2170 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2171
Josh Durgin35915382011-12-05 18:25:13 -08002172 return sprintf(buf, "%llu\n", (unsigned long long)snap->size);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002173}
2174
2175static ssize_t rbd_snap_id_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 Durgin35915382011-12-05 18:25:13 -08002181 return sprintf(buf, "%llu\n", (unsigned long long)snap->id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002182}
2183
Alex Elder34b13182012-07-13 20:35:12 -05002184static ssize_t rbd_snap_features_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
2190 return sprintf(buf, "0x%016llx\n",
2191 (unsigned long long) snap->features);
2192}
2193
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002194static DEVICE_ATTR(snap_size, S_IRUGO, rbd_snap_size_show, NULL);
2195static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL);
Alex Elder34b13182012-07-13 20:35:12 -05002196static DEVICE_ATTR(snap_features, S_IRUGO, rbd_snap_features_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002197
2198static struct attribute *rbd_snap_attrs[] = {
2199 &dev_attr_snap_size.attr,
2200 &dev_attr_snap_id.attr,
Alex Elder34b13182012-07-13 20:35:12 -05002201 &dev_attr_snap_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002202 NULL,
2203};
2204
2205static struct attribute_group rbd_snap_attr_group = {
2206 .attrs = rbd_snap_attrs,
2207};
2208
2209static void rbd_snap_dev_release(struct device *dev)
2210{
2211 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2212 kfree(snap->name);
2213 kfree(snap);
2214}
2215
2216static const struct attribute_group *rbd_snap_attr_groups[] = {
2217 &rbd_snap_attr_group,
2218 NULL
2219};
2220
2221static struct device_type rbd_snap_device_type = {
2222 .groups = rbd_snap_attr_groups,
2223 .release = rbd_snap_dev_release,
2224};
2225
Alex Elder8b8fb992012-10-26 17:25:24 -05002226static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)
2227{
2228 kref_get(&spec->kref);
2229
2230 return spec;
2231}
2232
2233static void rbd_spec_free(struct kref *kref);
2234static void rbd_spec_put(struct rbd_spec *spec)
2235{
2236 if (spec)
2237 kref_put(&spec->kref, rbd_spec_free);
2238}
2239
2240static struct rbd_spec *rbd_spec_alloc(void)
2241{
2242 struct rbd_spec *spec;
2243
2244 spec = kzalloc(sizeof (*spec), GFP_KERNEL);
2245 if (!spec)
2246 return NULL;
2247 kref_init(&spec->kref);
2248
2249 rbd_spec_put(rbd_spec_get(spec)); /* TEMPORARY */
2250
2251 return spec;
2252}
2253
2254static void rbd_spec_free(struct kref *kref)
2255{
2256 struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref);
2257
2258 kfree(spec->pool_name);
2259 kfree(spec->image_id);
2260 kfree(spec->image_name);
2261 kfree(spec->snap_name);
2262 kfree(spec);
2263}
2264
Alex Elderc53d5892012-10-25 23:34:42 -05002265struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,
2266 struct rbd_spec *spec)
2267{
2268 struct rbd_device *rbd_dev;
2269
2270 rbd_dev = kzalloc(sizeof (*rbd_dev), GFP_KERNEL);
2271 if (!rbd_dev)
2272 return NULL;
2273
2274 spin_lock_init(&rbd_dev->lock);
Alex Elderd78b6502012-11-09 08:43:15 -06002275 atomic_set(&rbd_dev->exists, 0);
Alex Elderc53d5892012-10-25 23:34:42 -05002276 INIT_LIST_HEAD(&rbd_dev->node);
2277 INIT_LIST_HEAD(&rbd_dev->snaps);
2278 init_rwsem(&rbd_dev->header_rwsem);
2279
2280 rbd_dev->spec = spec;
2281 rbd_dev->rbd_client = rbdc;
2282
Alex Elder0903e872012-11-14 12:25:19 -06002283 /* Initialize the layout used for all rbd requests */
2284
2285 rbd_dev->layout.fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
2286 rbd_dev->layout.fl_stripe_count = cpu_to_le32(1);
2287 rbd_dev->layout.fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
2288 rbd_dev->layout.fl_pg_pool = cpu_to_le32((u32) spec->pool_id);
2289
Alex Elderc53d5892012-10-25 23:34:42 -05002290 return rbd_dev;
2291}
2292
2293static void rbd_dev_destroy(struct rbd_device *rbd_dev)
2294{
Alex Elder86b00e02012-10-25 23:34:42 -05002295 rbd_spec_put(rbd_dev->parent_spec);
Alex Elderc53d5892012-10-25 23:34:42 -05002296 kfree(rbd_dev->header_name);
2297 rbd_put_client(rbd_dev->rbd_client);
2298 rbd_spec_put(rbd_dev->spec);
2299 kfree(rbd_dev);
2300}
2301
Alex Elder304f6802012-08-31 17:29:52 -05002302static bool rbd_snap_registered(struct rbd_snap *snap)
2303{
2304 bool ret = snap->dev.type == &rbd_snap_device_type;
2305 bool reg = device_is_registered(&snap->dev);
2306
2307 rbd_assert(!ret ^ reg);
2308
2309 return ret;
2310}
2311
Alex Elder41f38c22012-10-25 23:34:40 -05002312static void rbd_remove_snap_dev(struct rbd_snap *snap)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002313{
2314 list_del(&snap->node);
Alex Elder304f6802012-08-31 17:29:52 -05002315 if (device_is_registered(&snap->dev))
2316 device_unregister(&snap->dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002317}
2318
Alex Elder14e70852012-07-19 09:09:27 -05002319static int rbd_register_snap_dev(struct rbd_snap *snap,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002320 struct device *parent)
2321{
2322 struct device *dev = &snap->dev;
2323 int ret;
2324
2325 dev->type = &rbd_snap_device_type;
2326 dev->parent = parent;
2327 dev->release = rbd_snap_dev_release;
Alex Elderd4b125e2012-07-03 16:01:19 -05002328 dev_set_name(dev, "%s%s", RBD_SNAP_DEV_NAME_PREFIX, snap->name);
Alex Elder304f6802012-08-31 17:29:52 -05002329 dout("%s: registering device for snapshot %s\n", __func__, snap->name);
2330
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002331 ret = device_register(dev);
2332
2333 return ret;
2334}
2335
Alex Elder4e891e02012-07-10 20:30:10 -05002336static struct rbd_snap *__rbd_add_snap_dev(struct rbd_device *rbd_dev,
Alex Elderc8d18422012-07-10 20:30:11 -05002337 const char *snap_name,
Alex Elder34b13182012-07-13 20:35:12 -05002338 u64 snap_id, u64 snap_size,
2339 u64 snap_features)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002340{
Alex Elder4e891e02012-07-10 20:30:10 -05002341 struct rbd_snap *snap;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002342 int ret;
Alex Elder4e891e02012-07-10 20:30:10 -05002343
2344 snap = kzalloc(sizeof (*snap), GFP_KERNEL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002345 if (!snap)
Alex Elder4e891e02012-07-10 20:30:10 -05002346 return ERR_PTR(-ENOMEM);
2347
2348 ret = -ENOMEM;
Alex Elderc8d18422012-07-10 20:30:11 -05002349 snap->name = kstrdup(snap_name, GFP_KERNEL);
Alex Elder4e891e02012-07-10 20:30:10 -05002350 if (!snap->name)
2351 goto err;
2352
Alex Elderc8d18422012-07-10 20:30:11 -05002353 snap->id = snap_id;
2354 snap->size = snap_size;
Alex Elder34b13182012-07-13 20:35:12 -05002355 snap->features = snap_features;
Alex Elder4e891e02012-07-10 20:30:10 -05002356
2357 return snap;
2358
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002359err:
2360 kfree(snap->name);
2361 kfree(snap);
Alex Elder4e891e02012-07-10 20:30:10 -05002362
2363 return ERR_PTR(ret);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002364}
2365
Alex Eldercd892122012-07-03 16:01:19 -05002366static char *rbd_dev_v1_snap_info(struct rbd_device *rbd_dev, u32 which,
2367 u64 *snap_size, u64 *snap_features)
2368{
2369 char *snap_name;
2370
2371 rbd_assert(which < rbd_dev->header.snapc->num_snaps);
2372
2373 *snap_size = rbd_dev->header.snap_sizes[which];
2374 *snap_features = 0; /* No features for v1 */
2375
2376 /* Skip over names until we find the one we are looking for */
2377
2378 snap_name = rbd_dev->header.snap_names;
2379 while (which--)
2380 snap_name += strlen(snap_name) + 1;
2381
2382 return snap_name;
2383}
2384
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002385/*
Alex Elder9d475de2012-07-03 16:01:19 -05002386 * Get the size and object order for an image snapshot, or if
2387 * snap_id is CEPH_NOSNAP, gets this information for the base
2388 * image.
2389 */
2390static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
2391 u8 *order, u64 *snap_size)
2392{
2393 __le64 snapid = cpu_to_le64(snap_id);
2394 int ret;
2395 struct {
2396 u8 order;
2397 __le64 size;
2398 } __attribute__ ((packed)) size_buf = { 0 };
2399
2400 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2401 "rbd", "get_size",
2402 (char *) &snapid, sizeof (snapid),
Alex Elder07b23912012-11-09 08:43:16 -06002403 (char *) &size_buf, sizeof (size_buf), NULL);
Alex Elder9d475de2012-07-03 16:01:19 -05002404 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2405 if (ret < 0)
2406 return ret;
2407
2408 *order = size_buf.order;
2409 *snap_size = le64_to_cpu(size_buf.size);
2410
2411 dout(" snap_id 0x%016llx order = %u, snap_size = %llu\n",
2412 (unsigned long long) snap_id, (unsigned int) *order,
2413 (unsigned long long) *snap_size);
2414
2415 return 0;
2416}
2417
2418static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
2419{
2420 return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
2421 &rbd_dev->header.obj_order,
2422 &rbd_dev->header.image_size);
2423}
2424
Alex Elder1e130192012-07-03 16:01:19 -05002425static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
2426{
2427 void *reply_buf;
2428 int ret;
2429 void *p;
2430
2431 reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL);
2432 if (!reply_buf)
2433 return -ENOMEM;
2434
2435 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2436 "rbd", "get_object_prefix",
2437 NULL, 0,
Alex Elder07b23912012-11-09 08:43:16 -06002438 reply_buf, RBD_OBJ_PREFIX_LEN_MAX, NULL);
Alex Elder1e130192012-07-03 16:01:19 -05002439 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2440 if (ret < 0)
2441 goto out;
Alex Eldera0ea3a42012-10-10 21:19:13 -07002442 ret = 0; /* rbd_req_sync_exec() can return positive */
Alex Elder1e130192012-07-03 16:01:19 -05002443
2444 p = reply_buf;
2445 rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p,
2446 p + RBD_OBJ_PREFIX_LEN_MAX,
2447 NULL, GFP_NOIO);
2448
2449 if (IS_ERR(rbd_dev->header.object_prefix)) {
2450 ret = PTR_ERR(rbd_dev->header.object_prefix);
2451 rbd_dev->header.object_prefix = NULL;
2452 } else {
2453 dout(" object_prefix = %s\n", rbd_dev->header.object_prefix);
2454 }
2455
2456out:
2457 kfree(reply_buf);
2458
2459 return ret;
2460}
2461
Alex Elderb1b54022012-07-03 16:01:19 -05002462static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
2463 u64 *snap_features)
2464{
2465 __le64 snapid = cpu_to_le64(snap_id);
2466 struct {
2467 __le64 features;
2468 __le64 incompat;
2469 } features_buf = { 0 };
Alex Elderd8891402012-10-09 13:50:17 -07002470 u64 incompat;
Alex Elderb1b54022012-07-03 16:01:19 -05002471 int ret;
2472
2473 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2474 "rbd", "get_features",
2475 (char *) &snapid, sizeof (snapid),
2476 (char *) &features_buf, sizeof (features_buf),
Alex Elder07b23912012-11-09 08:43:16 -06002477 NULL);
Alex Elderb1b54022012-07-03 16:01:19 -05002478 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2479 if (ret < 0)
2480 return ret;
Alex Elderd8891402012-10-09 13:50:17 -07002481
2482 incompat = le64_to_cpu(features_buf.incompat);
2483 if (incompat & ~RBD_FEATURES_ALL)
Alex Elderb8f5c6e2012-11-01 08:39:26 -05002484 return -ENXIO;
Alex Elderd8891402012-10-09 13:50:17 -07002485
Alex Elderb1b54022012-07-03 16:01:19 -05002486 *snap_features = le64_to_cpu(features_buf.features);
2487
2488 dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
2489 (unsigned long long) snap_id,
2490 (unsigned long long) *snap_features,
2491 (unsigned long long) le64_to_cpu(features_buf.incompat));
2492
2493 return 0;
2494}
2495
2496static int rbd_dev_v2_features(struct rbd_device *rbd_dev)
2497{
2498 return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP,
2499 &rbd_dev->header.features);
2500}
2501
Alex Elder86b00e02012-10-25 23:34:42 -05002502static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev)
2503{
2504 struct rbd_spec *parent_spec;
2505 size_t size;
2506 void *reply_buf = NULL;
2507 __le64 snapid;
2508 void *p;
2509 void *end;
2510 char *image_id;
2511 u64 overlap;
Alex Elder86b00e02012-10-25 23:34:42 -05002512 int ret;
2513
2514 parent_spec = rbd_spec_alloc();
2515 if (!parent_spec)
2516 return -ENOMEM;
2517
2518 size = sizeof (__le64) + /* pool_id */
2519 sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */
2520 sizeof (__le64) + /* snap_id */
2521 sizeof (__le64); /* overlap */
2522 reply_buf = kmalloc(size, GFP_KERNEL);
2523 if (!reply_buf) {
2524 ret = -ENOMEM;
2525 goto out_err;
2526 }
2527
2528 snapid = cpu_to_le64(CEPH_NOSNAP);
2529 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2530 "rbd", "get_parent",
2531 (char *) &snapid, sizeof (snapid),
Alex Elder07b23912012-11-09 08:43:16 -06002532 (char *) reply_buf, size, NULL);
Alex Elder86b00e02012-10-25 23:34:42 -05002533 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2534 if (ret < 0)
2535 goto out_err;
2536
2537 ret = -ERANGE;
2538 p = reply_buf;
2539 end = (char *) reply_buf + size;
2540 ceph_decode_64_safe(&p, end, parent_spec->pool_id, out_err);
2541 if (parent_spec->pool_id == CEPH_NOPOOL)
2542 goto out; /* No parent? No problem. */
2543
Alex Elder0903e872012-11-14 12:25:19 -06002544 /* The ceph file layout needs to fit pool id in 32 bits */
2545
2546 ret = -EIO;
2547 if (WARN_ON(parent_spec->pool_id > (u64) U32_MAX))
2548 goto out;
2549
Alex Elder979ed482012-11-01 08:39:26 -05002550 image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elder86b00e02012-10-25 23:34:42 -05002551 if (IS_ERR(image_id)) {
2552 ret = PTR_ERR(image_id);
2553 goto out_err;
2554 }
2555 parent_spec->image_id = image_id;
2556 ceph_decode_64_safe(&p, end, parent_spec->snap_id, out_err);
2557 ceph_decode_64_safe(&p, end, overlap, out_err);
2558
2559 rbd_dev->parent_overlap = overlap;
2560 rbd_dev->parent_spec = parent_spec;
2561 parent_spec = NULL; /* rbd_dev now owns this */
2562out:
2563 ret = 0;
2564out_err:
2565 kfree(reply_buf);
2566 rbd_spec_put(parent_spec);
2567
2568 return ret;
2569}
2570
Alex Elder9e15b772012-10-30 19:40:33 -05002571static char *rbd_dev_image_name(struct rbd_device *rbd_dev)
2572{
2573 size_t image_id_size;
2574 char *image_id;
2575 void *p;
2576 void *end;
2577 size_t size;
2578 void *reply_buf = NULL;
2579 size_t len = 0;
2580 char *image_name = NULL;
2581 int ret;
2582
2583 rbd_assert(!rbd_dev->spec->image_name);
2584
Alex Elder69e7a022012-11-01 08:39:26 -05002585 len = strlen(rbd_dev->spec->image_id);
2586 image_id_size = sizeof (__le32) + len;
Alex Elder9e15b772012-10-30 19:40:33 -05002587 image_id = kmalloc(image_id_size, GFP_KERNEL);
2588 if (!image_id)
2589 return NULL;
2590
2591 p = image_id;
2592 end = (char *) image_id + image_id_size;
Alex Elder69e7a022012-11-01 08:39:26 -05002593 ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32) len);
Alex Elder9e15b772012-10-30 19:40:33 -05002594
2595 size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX;
2596 reply_buf = kmalloc(size, GFP_KERNEL);
2597 if (!reply_buf)
2598 goto out;
2599
2600 ret = rbd_req_sync_exec(rbd_dev, RBD_DIRECTORY,
2601 "rbd", "dir_get_name",
2602 image_id, image_id_size,
Alex Elder07b23912012-11-09 08:43:16 -06002603 (char *) reply_buf, size, NULL);
Alex Elder9e15b772012-10-30 19:40:33 -05002604 if (ret < 0)
2605 goto out;
2606 p = reply_buf;
2607 end = (char *) reply_buf + size;
2608 image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL);
2609 if (IS_ERR(image_name))
2610 image_name = NULL;
2611 else
2612 dout("%s: name is %s len is %zd\n", __func__, image_name, len);
2613out:
2614 kfree(reply_buf);
2615 kfree(image_id);
2616
2617 return image_name;
2618}
2619
2620/*
2621 * When a parent image gets probed, we only have the pool, image,
2622 * and snapshot ids but not the names of any of them. This call
2623 * is made later to fill in those names. It has to be done after
2624 * rbd_dev_snaps_update() has completed because some of the
2625 * information (in particular, snapshot name) is not available
2626 * until then.
2627 */
2628static int rbd_dev_probe_update_spec(struct rbd_device *rbd_dev)
2629{
2630 struct ceph_osd_client *osdc;
2631 const char *name;
2632 void *reply_buf = NULL;
2633 int ret;
2634
2635 if (rbd_dev->spec->pool_name)
2636 return 0; /* Already have the names */
2637
2638 /* Look up the pool name */
2639
2640 osdc = &rbd_dev->rbd_client->client->osdc;
2641 name = ceph_pg_pool_name_by_id(osdc->osdmap, rbd_dev->spec->pool_id);
Alex Elder935dc892012-11-01 10:17:15 -05002642 if (!name) {
2643 rbd_warn(rbd_dev, "there is no pool with id %llu",
2644 rbd_dev->spec->pool_id); /* Really a BUG() */
2645 return -EIO;
2646 }
Alex Elder9e15b772012-10-30 19:40:33 -05002647
2648 rbd_dev->spec->pool_name = kstrdup(name, GFP_KERNEL);
2649 if (!rbd_dev->spec->pool_name)
2650 return -ENOMEM;
2651
2652 /* Fetch the image name; tolerate failure here */
2653
2654 name = rbd_dev_image_name(rbd_dev);
Alex Elder69e7a022012-11-01 08:39:26 -05002655 if (name)
Alex Elder9e15b772012-10-30 19:40:33 -05002656 rbd_dev->spec->image_name = (char *) name;
Alex Elder69e7a022012-11-01 08:39:26 -05002657 else
Alex Elder06ecc6c2012-11-01 10:17:15 -05002658 rbd_warn(rbd_dev, "unable to get image name");
Alex Elder9e15b772012-10-30 19:40:33 -05002659
2660 /* Look up the snapshot name. */
2661
2662 name = rbd_snap_name(rbd_dev, rbd_dev->spec->snap_id);
2663 if (!name) {
Alex Elder935dc892012-11-01 10:17:15 -05002664 rbd_warn(rbd_dev, "no snapshot with id %llu",
2665 rbd_dev->spec->snap_id); /* Really a BUG() */
Alex Elder9e15b772012-10-30 19:40:33 -05002666 ret = -EIO;
2667 goto out_err;
2668 }
2669 rbd_dev->spec->snap_name = kstrdup(name, GFP_KERNEL);
2670 if(!rbd_dev->spec->snap_name)
2671 goto out_err;
2672
2673 return 0;
2674out_err:
2675 kfree(reply_buf);
2676 kfree(rbd_dev->spec->pool_name);
2677 rbd_dev->spec->pool_name = NULL;
2678
2679 return ret;
2680}
2681
Alex Elder6e14b1a2012-07-03 16:01:19 -05002682static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev, u64 *ver)
Alex Elder35d489f2012-07-03 16:01:19 -05002683{
2684 size_t size;
2685 int ret;
2686 void *reply_buf;
2687 void *p;
2688 void *end;
2689 u64 seq;
2690 u32 snap_count;
2691 struct ceph_snap_context *snapc;
2692 u32 i;
2693
2694 /*
2695 * We'll need room for the seq value (maximum snapshot id),
2696 * snapshot count, and array of that many snapshot ids.
2697 * For now we have a fixed upper limit on the number we're
2698 * prepared to receive.
2699 */
2700 size = sizeof (__le64) + sizeof (__le32) +
2701 RBD_MAX_SNAP_COUNT * sizeof (__le64);
2702 reply_buf = kzalloc(size, GFP_KERNEL);
2703 if (!reply_buf)
2704 return -ENOMEM;
2705
2706 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2707 "rbd", "get_snapcontext",
2708 NULL, 0,
Alex Elder07b23912012-11-09 08:43:16 -06002709 reply_buf, size, ver);
Alex Elder35d489f2012-07-03 16:01:19 -05002710 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2711 if (ret < 0)
2712 goto out;
2713
2714 ret = -ERANGE;
2715 p = reply_buf;
2716 end = (char *) reply_buf + size;
2717 ceph_decode_64_safe(&p, end, seq, out);
2718 ceph_decode_32_safe(&p, end, snap_count, out);
2719
2720 /*
2721 * Make sure the reported number of snapshot ids wouldn't go
2722 * beyond the end of our buffer. But before checking that,
2723 * make sure the computed size of the snapshot context we
2724 * allocate is representable in a size_t.
2725 */
2726 if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
2727 / sizeof (u64)) {
2728 ret = -EINVAL;
2729 goto out;
2730 }
2731 if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
2732 goto out;
2733
2734 size = sizeof (struct ceph_snap_context) +
2735 snap_count * sizeof (snapc->snaps[0]);
2736 snapc = kmalloc(size, GFP_KERNEL);
2737 if (!snapc) {
2738 ret = -ENOMEM;
2739 goto out;
2740 }
2741
2742 atomic_set(&snapc->nref, 1);
2743 snapc->seq = seq;
2744 snapc->num_snaps = snap_count;
2745 for (i = 0; i < snap_count; i++)
2746 snapc->snaps[i] = ceph_decode_64(&p);
2747
2748 rbd_dev->header.snapc = snapc;
2749
2750 dout(" snap context seq = %llu, snap_count = %u\n",
2751 (unsigned long long) seq, (unsigned int) snap_count);
2752
2753out:
2754 kfree(reply_buf);
2755
2756 return 0;
2757}
2758
Alex Elderb8b1e2d2012-07-03 16:01:19 -05002759static char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, u32 which)
2760{
2761 size_t size;
2762 void *reply_buf;
2763 __le64 snap_id;
2764 int ret;
2765 void *p;
2766 void *end;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05002767 char *snap_name;
2768
2769 size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN;
2770 reply_buf = kmalloc(size, GFP_KERNEL);
2771 if (!reply_buf)
2772 return ERR_PTR(-ENOMEM);
2773
2774 snap_id = cpu_to_le64(rbd_dev->header.snapc->snaps[which]);
2775 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2776 "rbd", "get_snapshot_name",
2777 (char *) &snap_id, sizeof (snap_id),
Alex Elder07b23912012-11-09 08:43:16 -06002778 reply_buf, size, NULL);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05002779 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2780 if (ret < 0)
2781 goto out;
2782
2783 p = reply_buf;
2784 end = (char *) reply_buf + size;
Alex Eldere5c35532012-10-25 23:34:41 -05002785 snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05002786 if (IS_ERR(snap_name)) {
2787 ret = PTR_ERR(snap_name);
2788 goto out;
2789 } else {
2790 dout(" snap_id 0x%016llx snap_name = %s\n",
2791 (unsigned long long) le64_to_cpu(snap_id), snap_name);
2792 }
2793 kfree(reply_buf);
2794
2795 return snap_name;
2796out:
2797 kfree(reply_buf);
2798
2799 return ERR_PTR(ret);
2800}
2801
2802static char *rbd_dev_v2_snap_info(struct rbd_device *rbd_dev, u32 which,
2803 u64 *snap_size, u64 *snap_features)
2804{
Alex Eldere0b49862013-01-09 14:44:18 -06002805 u64 snap_id;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05002806 u8 order;
2807 int ret;
2808
2809 snap_id = rbd_dev->header.snapc->snaps[which];
2810 ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, &order, snap_size);
2811 if (ret)
2812 return ERR_PTR(ret);
2813 ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, snap_features);
2814 if (ret)
2815 return ERR_PTR(ret);
2816
2817 return rbd_dev_v2_snap_name(rbd_dev, which);
2818}
2819
2820static char *rbd_dev_snap_info(struct rbd_device *rbd_dev, u32 which,
2821 u64 *snap_size, u64 *snap_features)
2822{
2823 if (rbd_dev->image_format == 1)
2824 return rbd_dev_v1_snap_info(rbd_dev, which,
2825 snap_size, snap_features);
2826 if (rbd_dev->image_format == 2)
2827 return rbd_dev_v2_snap_info(rbd_dev, which,
2828 snap_size, snap_features);
2829 return ERR_PTR(-EINVAL);
2830}
2831
Alex Elder117973f2012-08-31 17:29:55 -05002832static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev, u64 *hver)
2833{
2834 int ret;
2835 __u8 obj_order;
2836
2837 down_write(&rbd_dev->header_rwsem);
2838
2839 /* Grab old order first, to see if it changes */
2840
2841 obj_order = rbd_dev->header.obj_order,
2842 ret = rbd_dev_v2_image_size(rbd_dev);
2843 if (ret)
2844 goto out;
2845 if (rbd_dev->header.obj_order != obj_order) {
2846 ret = -EIO;
2847 goto out;
2848 }
2849 rbd_update_mapping_size(rbd_dev);
2850
2851 ret = rbd_dev_v2_snap_context(rbd_dev, hver);
2852 dout("rbd_dev_v2_snap_context returned %d\n", ret);
2853 if (ret)
2854 goto out;
2855 ret = rbd_dev_snaps_update(rbd_dev);
2856 dout("rbd_dev_snaps_update returned %d\n", ret);
2857 if (ret)
2858 goto out;
2859 ret = rbd_dev_snaps_register(rbd_dev);
2860 dout("rbd_dev_snaps_register returned %d\n", ret);
2861out:
2862 up_write(&rbd_dev->header_rwsem);
2863
2864 return ret;
2865}
2866
Alex Elder9d475de2012-07-03 16:01:19 -05002867/*
Alex Elder35938152012-08-02 11:29:46 -05002868 * Scan the rbd device's current snapshot list and compare it to the
2869 * newly-received snapshot context. Remove any existing snapshots
2870 * not present in the new snapshot context. Add a new snapshot for
2871 * any snaphots in the snapshot context not in the current list.
2872 * And verify there are no changes to snapshots we already know
2873 * about.
2874 *
2875 * Assumes the snapshots in the snapshot context are sorted by
2876 * snapshot id, highest id first. (Snapshots in the rbd_dev's list
2877 * are also maintained in that order.)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002878 */
Alex Elder304f6802012-08-31 17:29:52 -05002879static int rbd_dev_snaps_update(struct rbd_device *rbd_dev)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002880{
Alex Elder35938152012-08-02 11:29:46 -05002881 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
2882 const u32 snap_count = snapc->num_snaps;
Alex Elder35938152012-08-02 11:29:46 -05002883 struct list_head *head = &rbd_dev->snaps;
2884 struct list_head *links = head->next;
2885 u32 index = 0;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002886
Alex Elder9fcbb802012-08-23 23:48:49 -05002887 dout("%s: snap count is %u\n", __func__, (unsigned int) snap_count);
Alex Elder35938152012-08-02 11:29:46 -05002888 while (index < snap_count || links != head) {
2889 u64 snap_id;
2890 struct rbd_snap *snap;
Alex Eldercd892122012-07-03 16:01:19 -05002891 char *snap_name;
2892 u64 snap_size = 0;
2893 u64 snap_features = 0;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002894
Alex Elder35938152012-08-02 11:29:46 -05002895 snap_id = index < snap_count ? snapc->snaps[index]
2896 : CEPH_NOSNAP;
2897 snap = links != head ? list_entry(links, struct rbd_snap, node)
2898 : NULL;
Alex Elderaafb2302012-09-06 16:00:54 -05002899 rbd_assert(!snap || snap->id != CEPH_NOSNAP);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002900
Alex Elder35938152012-08-02 11:29:46 -05002901 if (snap_id == CEPH_NOSNAP || (snap && snap->id > snap_id)) {
2902 struct list_head *next = links->next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002903
Alex Elder35938152012-08-02 11:29:46 -05002904 /* Existing snapshot not in the new snap context */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002905
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002906 if (rbd_dev->spec->snap_id == snap->id)
Alex Elderd78b6502012-11-09 08:43:15 -06002907 atomic_set(&rbd_dev->exists, 0);
Alex Elder41f38c22012-10-25 23:34:40 -05002908 rbd_remove_snap_dev(snap);
Alex Elder9fcbb802012-08-23 23:48:49 -05002909 dout("%ssnap id %llu has been removed\n",
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002910 rbd_dev->spec->snap_id == snap->id ?
2911 "mapped " : "",
Alex Elder9fcbb802012-08-23 23:48:49 -05002912 (unsigned long long) snap->id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002913
Alex Elder35938152012-08-02 11:29:46 -05002914 /* Done with this list entry; advance */
2915
2916 links = next;
2917 continue;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002918 }
Alex Elder35938152012-08-02 11:29:46 -05002919
Alex Elderb8b1e2d2012-07-03 16:01:19 -05002920 snap_name = rbd_dev_snap_info(rbd_dev, index,
2921 &snap_size, &snap_features);
Alex Eldercd892122012-07-03 16:01:19 -05002922 if (IS_ERR(snap_name))
2923 return PTR_ERR(snap_name);
2924
Alex Elder9fcbb802012-08-23 23:48:49 -05002925 dout("entry %u: snap_id = %llu\n", (unsigned int) snap_count,
2926 (unsigned long long) snap_id);
Alex Elder35938152012-08-02 11:29:46 -05002927 if (!snap || (snap_id != CEPH_NOSNAP && snap->id < snap_id)) {
2928 struct rbd_snap *new_snap;
2929
2930 /* We haven't seen this snapshot before */
2931
Alex Elderc8d18422012-07-10 20:30:11 -05002932 new_snap = __rbd_add_snap_dev(rbd_dev, snap_name,
Alex Eldercd892122012-07-03 16:01:19 -05002933 snap_id, snap_size, snap_features);
Alex Elder9fcbb802012-08-23 23:48:49 -05002934 if (IS_ERR(new_snap)) {
2935 int err = PTR_ERR(new_snap);
2936
2937 dout(" failed to add dev, error %d\n", err);
2938
2939 return err;
2940 }
Alex Elder35938152012-08-02 11:29:46 -05002941
2942 /* New goes before existing, or at end of list */
2943
Alex Elder9fcbb802012-08-23 23:48:49 -05002944 dout(" added dev%s\n", snap ? "" : " at end\n");
Alex Elder35938152012-08-02 11:29:46 -05002945 if (snap)
2946 list_add_tail(&new_snap->node, &snap->node);
2947 else
Alex Elder523f3252012-08-30 00:16:37 -05002948 list_add_tail(&new_snap->node, head);
Alex Elder35938152012-08-02 11:29:46 -05002949 } else {
2950 /* Already have this one */
2951
Alex Elder9fcbb802012-08-23 23:48:49 -05002952 dout(" already present\n");
2953
Alex Eldercd892122012-07-03 16:01:19 -05002954 rbd_assert(snap->size == snap_size);
Alex Elderaafb2302012-09-06 16:00:54 -05002955 rbd_assert(!strcmp(snap->name, snap_name));
Alex Eldercd892122012-07-03 16:01:19 -05002956 rbd_assert(snap->features == snap_features);
Alex Elder35938152012-08-02 11:29:46 -05002957
2958 /* Done with this list entry; advance */
2959
2960 links = links->next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002961 }
Alex Elder35938152012-08-02 11:29:46 -05002962
2963 /* Advance to the next entry in the snapshot context */
2964
2965 index++;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002966 }
Alex Elder9fcbb802012-08-23 23:48:49 -05002967 dout("%s: done\n", __func__);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002968
2969 return 0;
2970}
2971
Alex Elder304f6802012-08-31 17:29:52 -05002972/*
2973 * Scan the list of snapshots and register the devices for any that
2974 * have not already been registered.
2975 */
2976static int rbd_dev_snaps_register(struct rbd_device *rbd_dev)
2977{
2978 struct rbd_snap *snap;
2979 int ret = 0;
2980
2981 dout("%s called\n", __func__);
Alex Elder86ff77b2012-08-31 17:29:53 -05002982 if (WARN_ON(!device_is_registered(&rbd_dev->dev)))
2983 return -EIO;
Alex Elder304f6802012-08-31 17:29:52 -05002984
2985 list_for_each_entry(snap, &rbd_dev->snaps, node) {
2986 if (!rbd_snap_registered(snap)) {
2987 ret = rbd_register_snap_dev(snap, &rbd_dev->dev);
2988 if (ret < 0)
2989 break;
2990 }
2991 }
2992 dout("%s: returning %d\n", __func__, ret);
2993
2994 return ret;
2995}
2996
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002997static int rbd_bus_add_dev(struct rbd_device *rbd_dev)
2998{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002999 struct device *dev;
Alex Eldercd789ab2012-08-30 00:16:38 -05003000 int ret;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003001
3002 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003003
Alex Eldercd789ab2012-08-30 00:16:38 -05003004 dev = &rbd_dev->dev;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003005 dev->bus = &rbd_bus_type;
3006 dev->type = &rbd_device_type;
3007 dev->parent = &rbd_root_dev;
3008 dev->release = rbd_dev_release;
Alex Elderde71a292012-07-03 16:01:19 -05003009 dev_set_name(dev, "%d", rbd_dev->dev_id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003010 ret = device_register(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003011
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003012 mutex_unlock(&ctl_mutex);
Alex Eldercd789ab2012-08-30 00:16:38 -05003013
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003014 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003015}
3016
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003017static void rbd_bus_del_dev(struct rbd_device *rbd_dev)
3018{
3019 device_unregister(&rbd_dev->dev);
3020}
3021
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003022static int rbd_init_watch_dev(struct rbd_device *rbd_dev)
3023{
3024 int ret, rc;
3025
3026 do {
Alex Elder907703d2012-11-13 21:11:15 -06003027 ret = rbd_req_sync_watch(rbd_dev, 1);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003028 if (ret == -ERANGE) {
Alex Elder117973f2012-08-31 17:29:55 -05003029 rc = rbd_dev_refresh(rbd_dev, NULL);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003030 if (rc < 0)
3031 return rc;
3032 }
3033 } while (ret == -ERANGE);
3034
3035 return ret;
3036}
3037
Alex Eldere2839302012-08-29 17:11:06 -05003038static atomic64_t rbd_dev_id_max = ATOMIC64_INIT(0);
Alex Elder1ddbe942012-01-29 13:57:44 -06003039
3040/*
Alex Elder499afd52012-02-02 08:13:29 -06003041 * Get a unique rbd identifier for the given new rbd_dev, and add
3042 * the rbd_dev to the global list. The minimum rbd id is 1.
Alex Elder1ddbe942012-01-29 13:57:44 -06003043 */
Alex Eldere2839302012-08-29 17:11:06 -05003044static void rbd_dev_id_get(struct rbd_device *rbd_dev)
Alex Elderb7f23c32012-01-29 13:57:43 -06003045{
Alex Eldere2839302012-08-29 17:11:06 -05003046 rbd_dev->dev_id = atomic64_inc_return(&rbd_dev_id_max);
Alex Elder499afd52012-02-02 08:13:29 -06003047
3048 spin_lock(&rbd_dev_list_lock);
3049 list_add_tail(&rbd_dev->node, &rbd_dev_list);
3050 spin_unlock(&rbd_dev_list_lock);
Alex Eldere2839302012-08-29 17:11:06 -05003051 dout("rbd_dev %p given dev id %llu\n", rbd_dev,
3052 (unsigned long long) rbd_dev->dev_id);
Alex Elder1ddbe942012-01-29 13:57:44 -06003053}
Alex Elderb7f23c32012-01-29 13:57:43 -06003054
Alex Elder1ddbe942012-01-29 13:57:44 -06003055/*
Alex Elder499afd52012-02-02 08:13:29 -06003056 * Remove an rbd_dev from the global list, and record that its
3057 * identifier is no longer in use.
Alex Elder1ddbe942012-01-29 13:57:44 -06003058 */
Alex Eldere2839302012-08-29 17:11:06 -05003059static void rbd_dev_id_put(struct rbd_device *rbd_dev)
Alex Elder1ddbe942012-01-29 13:57:44 -06003060{
Alex Elderd184f6b2012-01-29 13:57:44 -06003061 struct list_head *tmp;
Alex Elderde71a292012-07-03 16:01:19 -05003062 int rbd_id = rbd_dev->dev_id;
Alex Elderd184f6b2012-01-29 13:57:44 -06003063 int max_id;
3064
Alex Elderaafb2302012-09-06 16:00:54 -05003065 rbd_assert(rbd_id > 0);
Alex Elder499afd52012-02-02 08:13:29 -06003066
Alex Eldere2839302012-08-29 17:11:06 -05003067 dout("rbd_dev %p released dev id %llu\n", rbd_dev,
3068 (unsigned long long) rbd_dev->dev_id);
Alex Elder499afd52012-02-02 08:13:29 -06003069 spin_lock(&rbd_dev_list_lock);
3070 list_del_init(&rbd_dev->node);
Alex Elderd184f6b2012-01-29 13:57:44 -06003071
3072 /*
3073 * If the id being "put" is not the current maximum, there
3074 * is nothing special we need to do.
3075 */
Alex Eldere2839302012-08-29 17:11:06 -05003076 if (rbd_id != atomic64_read(&rbd_dev_id_max)) {
Alex Elderd184f6b2012-01-29 13:57:44 -06003077 spin_unlock(&rbd_dev_list_lock);
3078 return;
3079 }
3080
3081 /*
3082 * We need to update the current maximum id. Search the
3083 * list to find out what it is. We're more likely to find
3084 * the maximum at the end, so search the list backward.
3085 */
3086 max_id = 0;
3087 list_for_each_prev(tmp, &rbd_dev_list) {
3088 struct rbd_device *rbd_dev;
3089
3090 rbd_dev = list_entry(tmp, struct rbd_device, node);
Alex Elderb213e0b2012-10-10 21:19:13 -07003091 if (rbd_dev->dev_id > max_id)
3092 max_id = rbd_dev->dev_id;
Alex Elderd184f6b2012-01-29 13:57:44 -06003093 }
Alex Elder499afd52012-02-02 08:13:29 -06003094 spin_unlock(&rbd_dev_list_lock);
Alex Elderb7f23c32012-01-29 13:57:43 -06003095
Alex Elder1ddbe942012-01-29 13:57:44 -06003096 /*
Alex Eldere2839302012-08-29 17:11:06 -05003097 * The max id could have been updated by rbd_dev_id_get(), in
Alex Elderd184f6b2012-01-29 13:57:44 -06003098 * which case it now accurately reflects the new maximum.
3099 * Be careful not to overwrite the maximum value in that
3100 * case.
Alex Elder1ddbe942012-01-29 13:57:44 -06003101 */
Alex Eldere2839302012-08-29 17:11:06 -05003102 atomic64_cmpxchg(&rbd_dev_id_max, rbd_id, max_id);
3103 dout(" max dev id has been reset\n");
Alex Elderb7f23c32012-01-29 13:57:43 -06003104}
3105
Alex Eldera725f65e2012-02-02 08:13:30 -06003106/*
Alex Eldere28fff262012-02-02 08:13:30 -06003107 * Skips over white space at *buf, and updates *buf to point to the
3108 * first found non-space character (if any). Returns the length of
Alex Elder593a9e72012-02-07 12:03:37 -06003109 * the token (string of non-white space characters) found. Note
3110 * that *buf must be terminated with '\0'.
Alex Eldere28fff262012-02-02 08:13:30 -06003111 */
3112static inline size_t next_token(const char **buf)
3113{
3114 /*
3115 * These are the characters that produce nonzero for
3116 * isspace() in the "C" and "POSIX" locales.
3117 */
3118 const char *spaces = " \f\n\r\t\v";
3119
3120 *buf += strspn(*buf, spaces); /* Find start of token */
3121
3122 return strcspn(*buf, spaces); /* Return token length */
3123}
3124
3125/*
3126 * Finds the next token in *buf, and if the provided token buffer is
3127 * big enough, copies the found token into it. The result, if
Alex Elder593a9e72012-02-07 12:03:37 -06003128 * copied, is guaranteed to be terminated with '\0'. Note that *buf
3129 * must be terminated with '\0' on entry.
Alex Eldere28fff262012-02-02 08:13:30 -06003130 *
3131 * Returns the length of the token found (not including the '\0').
3132 * Return value will be 0 if no token is found, and it will be >=
3133 * token_size if the token would not fit.
3134 *
Alex Elder593a9e72012-02-07 12:03:37 -06003135 * The *buf pointer will be updated to point beyond the end of the
Alex Eldere28fff262012-02-02 08:13:30 -06003136 * found token. Note that this occurs even if the token buffer is
3137 * too small to hold it.
3138 */
3139static inline size_t copy_token(const char **buf,
3140 char *token,
3141 size_t token_size)
3142{
3143 size_t len;
3144
3145 len = next_token(buf);
3146 if (len < token_size) {
3147 memcpy(token, *buf, len);
3148 *(token + len) = '\0';
3149 }
3150 *buf += len;
3151
3152 return len;
3153}
3154
3155/*
Alex Elderea3352f2012-07-09 21:04:23 -05003156 * Finds the next token in *buf, dynamically allocates a buffer big
3157 * enough to hold a copy of it, and copies the token into the new
3158 * buffer. The copy is guaranteed to be terminated with '\0'. Note
3159 * that a duplicate buffer is created even for a zero-length token.
3160 *
3161 * Returns a pointer to the newly-allocated duplicate, or a null
3162 * pointer if memory for the duplicate was not available. If
3163 * the lenp argument is a non-null pointer, the length of the token
3164 * (not including the '\0') is returned in *lenp.
3165 *
3166 * If successful, the *buf pointer will be updated to point beyond
3167 * the end of the found token.
3168 *
3169 * Note: uses GFP_KERNEL for allocation.
3170 */
3171static inline char *dup_token(const char **buf, size_t *lenp)
3172{
3173 char *dup;
3174 size_t len;
3175
3176 len = next_token(buf);
Alex Elder4caf35f2012-11-01 08:39:27 -05003177 dup = kmemdup(*buf, len + 1, GFP_KERNEL);
Alex Elderea3352f2012-07-09 21:04:23 -05003178 if (!dup)
3179 return NULL;
Alex Elderea3352f2012-07-09 21:04:23 -05003180 *(dup + len) = '\0';
3181 *buf += len;
3182
3183 if (lenp)
3184 *lenp = len;
3185
3186 return dup;
3187}
3188
3189/*
Alex Elder859c31d2012-10-25 23:34:42 -05003190 * Parse the options provided for an "rbd add" (i.e., rbd image
3191 * mapping) request. These arrive via a write to /sys/bus/rbd/add,
3192 * and the data written is passed here via a NUL-terminated buffer.
3193 * Returns 0 if successful or an error code otherwise.
Alex Elderd22f76e2012-07-12 10:46:35 -05003194 *
Alex Elder859c31d2012-10-25 23:34:42 -05003195 * The information extracted from these options is recorded in
3196 * the other parameters which return dynamically-allocated
3197 * structures:
3198 * ceph_opts
3199 * The address of a pointer that will refer to a ceph options
3200 * structure. Caller must release the returned pointer using
3201 * ceph_destroy_options() when it is no longer needed.
3202 * rbd_opts
3203 * Address of an rbd options pointer. Fully initialized by
3204 * this function; caller must release with kfree().
3205 * spec
3206 * Address of an rbd image specification pointer. Fully
3207 * initialized by this function based on parsed options.
3208 * Caller must release with rbd_spec_put().
3209 *
3210 * The options passed take this form:
3211 * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
3212 * where:
3213 * <mon_addrs>
3214 * A comma-separated list of one or more monitor addresses.
3215 * A monitor address is an ip address, optionally followed
3216 * by a port number (separated by a colon).
3217 * I.e.: ip1[:port1][,ip2[:port2]...]
3218 * <options>
3219 * A comma-separated list of ceph and/or rbd options.
3220 * <pool_name>
3221 * The name of the rados pool containing the rbd image.
3222 * <image_name>
3223 * The name of the image in that pool to map.
3224 * <snap_id>
3225 * An optional snapshot id. If provided, the mapping will
3226 * present data from the image at the time that snapshot was
3227 * created. The image head is used if no snapshot id is
3228 * provided. Snapshot mappings are always read-only.
Alex Eldera725f65e2012-02-02 08:13:30 -06003229 */
Alex Elder859c31d2012-10-25 23:34:42 -05003230static int rbd_add_parse_args(const char *buf,
Alex Elderdc79b112012-10-25 23:34:41 -05003231 struct ceph_options **ceph_opts,
Alex Elder859c31d2012-10-25 23:34:42 -05003232 struct rbd_options **opts,
3233 struct rbd_spec **rbd_spec)
Alex Eldera725f65e2012-02-02 08:13:30 -06003234{
Alex Elderd22f76e2012-07-12 10:46:35 -05003235 size_t len;
Alex Elder859c31d2012-10-25 23:34:42 -05003236 char *options;
Alex Elder0ddebc02012-10-25 23:34:41 -05003237 const char *mon_addrs;
3238 size_t mon_addrs_size;
Alex Elder859c31d2012-10-25 23:34:42 -05003239 struct rbd_spec *spec = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05003240 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05003241 struct ceph_options *copts;
Alex Elderdc79b112012-10-25 23:34:41 -05003242 int ret;
Alex Eldere28fff262012-02-02 08:13:30 -06003243
3244 /* The first four tokens are required */
3245
Alex Elder7ef32142012-02-02 08:13:30 -06003246 len = next_token(&buf);
Alex Elder4fb5d6712012-11-01 10:17:15 -05003247 if (!len) {
3248 rbd_warn(NULL, "no monitor address(es) provided");
3249 return -EINVAL;
3250 }
Alex Elder0ddebc02012-10-25 23:34:41 -05003251 mon_addrs = buf;
Alex Elderf28e5652012-10-25 23:34:41 -05003252 mon_addrs_size = len + 1;
Alex Elder7ef32142012-02-02 08:13:30 -06003253 buf += len;
Alex Eldera725f65e2012-02-02 08:13:30 -06003254
Alex Elderdc79b112012-10-25 23:34:41 -05003255 ret = -EINVAL;
Alex Elderf28e5652012-10-25 23:34:41 -05003256 options = dup_token(&buf, NULL);
3257 if (!options)
Alex Elderdc79b112012-10-25 23:34:41 -05003258 return -ENOMEM;
Alex Elder4fb5d6712012-11-01 10:17:15 -05003259 if (!*options) {
3260 rbd_warn(NULL, "no options provided");
3261 goto out_err;
3262 }
Alex Eldera725f65e2012-02-02 08:13:30 -06003263
Alex Elder859c31d2012-10-25 23:34:42 -05003264 spec = rbd_spec_alloc();
3265 if (!spec)
Alex Elderf28e5652012-10-25 23:34:41 -05003266 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05003267
3268 spec->pool_name = dup_token(&buf, NULL);
3269 if (!spec->pool_name)
3270 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05003271 if (!*spec->pool_name) {
3272 rbd_warn(NULL, "no pool name provided");
3273 goto out_err;
3274 }
Alex Eldere28fff262012-02-02 08:13:30 -06003275
Alex Elder69e7a022012-11-01 08:39:26 -05003276 spec->image_name = dup_token(&buf, NULL);
Alex Elder859c31d2012-10-25 23:34:42 -05003277 if (!spec->image_name)
Alex Elderf28e5652012-10-25 23:34:41 -05003278 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05003279 if (!*spec->image_name) {
3280 rbd_warn(NULL, "no image name provided");
3281 goto out_err;
3282 }
Alex Eldere28fff262012-02-02 08:13:30 -06003283
Alex Elderf28e5652012-10-25 23:34:41 -05003284 /*
3285 * Snapshot name is optional; default is to use "-"
3286 * (indicating the head/no snapshot).
3287 */
Alex Elder3feeb8942012-08-31 17:29:52 -05003288 len = next_token(&buf);
Alex Elder820a5f32012-07-09 21:04:24 -05003289 if (!len) {
Alex Elder3feeb8942012-08-31 17:29:52 -05003290 buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
3291 len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
Alex Elderf28e5652012-10-25 23:34:41 -05003292 } else if (len > RBD_MAX_SNAP_NAME_LEN) {
Alex Elderdc79b112012-10-25 23:34:41 -05003293 ret = -ENAMETOOLONG;
Alex Elderf28e5652012-10-25 23:34:41 -05003294 goto out_err;
Alex Elder849b4262012-07-09 21:04:24 -05003295 }
Alex Elder4caf35f2012-11-01 08:39:27 -05003296 spec->snap_name = kmemdup(buf, len + 1, GFP_KERNEL);
Alex Elder859c31d2012-10-25 23:34:42 -05003297 if (!spec->snap_name)
Alex Elderf28e5652012-10-25 23:34:41 -05003298 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05003299 *(spec->snap_name + len) = '\0';
Alex Eldere5c35532012-10-25 23:34:41 -05003300
Alex Elder0ddebc02012-10-25 23:34:41 -05003301 /* Initialize all rbd options to the defaults */
Alex Eldere28fff262012-02-02 08:13:30 -06003302
Alex Elder4e9afeb2012-10-25 23:34:41 -05003303 rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL);
3304 if (!rbd_opts)
3305 goto out_mem;
3306
3307 rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
Alex Elderd22f76e2012-07-12 10:46:35 -05003308
Alex Elder859c31d2012-10-25 23:34:42 -05003309 copts = ceph_parse_options(options, mon_addrs,
Alex Elder0ddebc02012-10-25 23:34:41 -05003310 mon_addrs + mon_addrs_size - 1,
Alex Elder4e9afeb2012-10-25 23:34:41 -05003311 parse_rbd_opts_token, rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05003312 if (IS_ERR(copts)) {
3313 ret = PTR_ERR(copts);
Alex Elderdc79b112012-10-25 23:34:41 -05003314 goto out_err;
3315 }
Alex Elder859c31d2012-10-25 23:34:42 -05003316 kfree(options);
3317
3318 *ceph_opts = copts;
Alex Elder4e9afeb2012-10-25 23:34:41 -05003319 *opts = rbd_opts;
Alex Elder859c31d2012-10-25 23:34:42 -05003320 *rbd_spec = spec;
Alex Elder0ddebc02012-10-25 23:34:41 -05003321
Alex Elderdc79b112012-10-25 23:34:41 -05003322 return 0;
Alex Elderf28e5652012-10-25 23:34:41 -05003323out_mem:
Alex Elderdc79b112012-10-25 23:34:41 -05003324 ret = -ENOMEM;
Alex Elderd22f76e2012-07-12 10:46:35 -05003325out_err:
Alex Elder859c31d2012-10-25 23:34:42 -05003326 kfree(rbd_opts);
3327 rbd_spec_put(spec);
Alex Elderf28e5652012-10-25 23:34:41 -05003328 kfree(options);
Alex Elderd22f76e2012-07-12 10:46:35 -05003329
Alex Elderdc79b112012-10-25 23:34:41 -05003330 return ret;
Alex Eldera725f65e2012-02-02 08:13:30 -06003331}
3332
Alex Elder589d30e2012-07-10 20:30:11 -05003333/*
3334 * An rbd format 2 image has a unique identifier, distinct from the
3335 * name given to it by the user. Internally, that identifier is
3336 * what's used to specify the names of objects related to the image.
3337 *
3338 * A special "rbd id" object is used to map an rbd image name to its
3339 * id. If that object doesn't exist, then there is no v2 rbd image
3340 * with the supplied name.
3341 *
3342 * This function will record the given rbd_dev's image_id field if
3343 * it can be determined, and in that case will return 0. If any
3344 * errors occur a negative errno will be returned and the rbd_dev's
3345 * image_id field will be unchanged (and should be NULL).
3346 */
3347static int rbd_dev_image_id(struct rbd_device *rbd_dev)
3348{
3349 int ret;
3350 size_t size;
3351 char *object_name;
3352 void *response;
3353 void *p;
3354
3355 /*
Alex Elder2c0d0a12012-10-30 19:40:33 -05003356 * When probing a parent image, the image id is already
3357 * known (and the image name likely is not). There's no
3358 * need to fetch the image id again in this case.
3359 */
3360 if (rbd_dev->spec->image_id)
3361 return 0;
3362
3363 /*
Alex Elder589d30e2012-07-10 20:30:11 -05003364 * First, see if the format 2 image id file exists, and if
3365 * so, get the image's persistent id from it.
3366 */
Alex Elder69e7a022012-11-01 08:39:26 -05003367 size = sizeof (RBD_ID_PREFIX) + strlen(rbd_dev->spec->image_name);
Alex Elder589d30e2012-07-10 20:30:11 -05003368 object_name = kmalloc(size, GFP_NOIO);
3369 if (!object_name)
3370 return -ENOMEM;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003371 sprintf(object_name, "%s%s", RBD_ID_PREFIX, rbd_dev->spec->image_name);
Alex Elder589d30e2012-07-10 20:30:11 -05003372 dout("rbd id object name is %s\n", object_name);
3373
3374 /* Response will be an encoded string, which includes a length */
3375
3376 size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
3377 response = kzalloc(size, GFP_NOIO);
3378 if (!response) {
3379 ret = -ENOMEM;
3380 goto out;
3381 }
3382
3383 ret = rbd_req_sync_exec(rbd_dev, object_name,
3384 "rbd", "get_id",
3385 NULL, 0,
Alex Elder07b23912012-11-09 08:43:16 -06003386 response, RBD_IMAGE_ID_LEN_MAX, NULL);
Alex Elder589d30e2012-07-10 20:30:11 -05003387 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
3388 if (ret < 0)
3389 goto out;
Alex Eldera0ea3a42012-10-10 21:19:13 -07003390 ret = 0; /* rbd_req_sync_exec() can return positive */
Alex Elder589d30e2012-07-10 20:30:11 -05003391
3392 p = response;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003393 rbd_dev->spec->image_id = ceph_extract_encoded_string(&p,
Alex Elder589d30e2012-07-10 20:30:11 -05003394 p + RBD_IMAGE_ID_LEN_MAX,
Alex Elder979ed482012-11-01 08:39:26 -05003395 NULL, GFP_NOIO);
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003396 if (IS_ERR(rbd_dev->spec->image_id)) {
3397 ret = PTR_ERR(rbd_dev->spec->image_id);
3398 rbd_dev->spec->image_id = NULL;
Alex Elder589d30e2012-07-10 20:30:11 -05003399 } else {
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003400 dout("image_id is %s\n", rbd_dev->spec->image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05003401 }
3402out:
3403 kfree(response);
3404 kfree(object_name);
3405
3406 return ret;
3407}
3408
Alex Eldera30b71b2012-07-10 20:30:11 -05003409static int rbd_dev_v1_probe(struct rbd_device *rbd_dev)
3410{
3411 int ret;
3412 size_t size;
3413
3414 /* Version 1 images have no id; empty string is used */
3415
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003416 rbd_dev->spec->image_id = kstrdup("", GFP_KERNEL);
3417 if (!rbd_dev->spec->image_id)
Alex Eldera30b71b2012-07-10 20:30:11 -05003418 return -ENOMEM;
Alex Eldera30b71b2012-07-10 20:30:11 -05003419
3420 /* Record the header object name for this rbd image. */
3421
Alex Elder69e7a022012-11-01 08:39:26 -05003422 size = strlen(rbd_dev->spec->image_name) + sizeof (RBD_SUFFIX);
Alex Eldera30b71b2012-07-10 20:30:11 -05003423 rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
3424 if (!rbd_dev->header_name) {
3425 ret = -ENOMEM;
3426 goto out_err;
3427 }
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003428 sprintf(rbd_dev->header_name, "%s%s",
3429 rbd_dev->spec->image_name, RBD_SUFFIX);
Alex Eldera30b71b2012-07-10 20:30:11 -05003430
3431 /* Populate rbd image metadata */
3432
3433 ret = rbd_read_header(rbd_dev, &rbd_dev->header);
3434 if (ret < 0)
3435 goto out_err;
Alex Elder86b00e02012-10-25 23:34:42 -05003436
3437 /* Version 1 images have no parent (no layering) */
3438
3439 rbd_dev->parent_spec = NULL;
3440 rbd_dev->parent_overlap = 0;
3441
Alex Eldera30b71b2012-07-10 20:30:11 -05003442 rbd_dev->image_format = 1;
3443
3444 dout("discovered version 1 image, header name is %s\n",
3445 rbd_dev->header_name);
3446
3447 return 0;
3448
3449out_err:
3450 kfree(rbd_dev->header_name);
3451 rbd_dev->header_name = NULL;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003452 kfree(rbd_dev->spec->image_id);
3453 rbd_dev->spec->image_id = NULL;
Alex Eldera30b71b2012-07-10 20:30:11 -05003454
3455 return ret;
3456}
3457
3458static int rbd_dev_v2_probe(struct rbd_device *rbd_dev)
3459{
3460 size_t size;
Alex Elder9d475de2012-07-03 16:01:19 -05003461 int ret;
Alex Elder6e14b1a2012-07-03 16:01:19 -05003462 u64 ver = 0;
Alex Eldera30b71b2012-07-10 20:30:11 -05003463
3464 /*
3465 * Image id was filled in by the caller. Record the header
3466 * object name for this rbd image.
3467 */
Alex Elder979ed482012-11-01 08:39:26 -05003468 size = sizeof (RBD_HEADER_PREFIX) + strlen(rbd_dev->spec->image_id);
Alex Eldera30b71b2012-07-10 20:30:11 -05003469 rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
3470 if (!rbd_dev->header_name)
3471 return -ENOMEM;
3472 sprintf(rbd_dev->header_name, "%s%s",
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003473 RBD_HEADER_PREFIX, rbd_dev->spec->image_id);
Alex Elder9d475de2012-07-03 16:01:19 -05003474
3475 /* Get the size and object order for the image */
3476
3477 ret = rbd_dev_v2_image_size(rbd_dev);
3478 if (ret < 0)
3479 goto out_err;
Alex Elder1e130192012-07-03 16:01:19 -05003480
3481 /* Get the object prefix (a.k.a. block_name) for the image */
3482
3483 ret = rbd_dev_v2_object_prefix(rbd_dev);
3484 if (ret < 0)
3485 goto out_err;
Alex Elderb1b54022012-07-03 16:01:19 -05003486
Alex Elderd8891402012-10-09 13:50:17 -07003487 /* Get the and check features for the image */
Alex Elderb1b54022012-07-03 16:01:19 -05003488
3489 ret = rbd_dev_v2_features(rbd_dev);
3490 if (ret < 0)
3491 goto out_err;
Alex Elder35d489f2012-07-03 16:01:19 -05003492
Alex Elder86b00e02012-10-25 23:34:42 -05003493 /* If the image supports layering, get the parent info */
3494
3495 if (rbd_dev->header.features & RBD_FEATURE_LAYERING) {
3496 ret = rbd_dev_v2_parent_info(rbd_dev);
3497 if (ret < 0)
3498 goto out_err;
3499 }
3500
Alex Elder6e14b1a2012-07-03 16:01:19 -05003501 /* crypto and compression type aren't (yet) supported for v2 images */
Alex Elder35d489f2012-07-03 16:01:19 -05003502
Alex Elder6e14b1a2012-07-03 16:01:19 -05003503 rbd_dev->header.crypt_type = 0;
3504 rbd_dev->header.comp_type = 0;
3505
3506 /* Get the snapshot context, plus the header version */
3507
3508 ret = rbd_dev_v2_snap_context(rbd_dev, &ver);
Alex Elder35d489f2012-07-03 16:01:19 -05003509 if (ret)
3510 goto out_err;
Alex Elder6e14b1a2012-07-03 16:01:19 -05003511 rbd_dev->header.obj_version = ver;
3512
Alex Eldera30b71b2012-07-10 20:30:11 -05003513 rbd_dev->image_format = 2;
3514
3515 dout("discovered version 2 image, header name is %s\n",
3516 rbd_dev->header_name);
3517
Alex Elder35152972012-08-31 17:29:55 -05003518 return 0;
Alex Elder9d475de2012-07-03 16:01:19 -05003519out_err:
Alex Elder86b00e02012-10-25 23:34:42 -05003520 rbd_dev->parent_overlap = 0;
3521 rbd_spec_put(rbd_dev->parent_spec);
3522 rbd_dev->parent_spec = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05003523 kfree(rbd_dev->header_name);
3524 rbd_dev->header_name = NULL;
Alex Elder1e130192012-07-03 16:01:19 -05003525 kfree(rbd_dev->header.object_prefix);
3526 rbd_dev->header.object_prefix = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05003527
3528 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05003529}
3530
Alex Elder83a06262012-10-30 15:47:17 -05003531static int rbd_dev_probe_finish(struct rbd_device *rbd_dev)
3532{
3533 int ret;
3534
3535 /* no need to lock here, as rbd_dev is not registered yet */
3536 ret = rbd_dev_snaps_update(rbd_dev);
3537 if (ret)
3538 return ret;
3539
Alex Elder9e15b772012-10-30 19:40:33 -05003540 ret = rbd_dev_probe_update_spec(rbd_dev);
3541 if (ret)
3542 goto err_out_snaps;
3543
Alex Elder83a06262012-10-30 15:47:17 -05003544 ret = rbd_dev_set_mapping(rbd_dev);
3545 if (ret)
3546 goto err_out_snaps;
3547
3548 /* generate unique id: find highest unique id, add one */
3549 rbd_dev_id_get(rbd_dev);
3550
3551 /* Fill in the device name, now that we have its id. */
3552 BUILD_BUG_ON(DEV_NAME_LEN
3553 < sizeof (RBD_DRV_NAME) + MAX_INT_FORMAT_WIDTH);
3554 sprintf(rbd_dev->name, "%s%d", RBD_DRV_NAME, rbd_dev->dev_id);
3555
3556 /* Get our block major device number. */
3557
3558 ret = register_blkdev(0, rbd_dev->name);
3559 if (ret < 0)
3560 goto err_out_id;
3561 rbd_dev->major = ret;
3562
3563 /* Set up the blkdev mapping. */
3564
3565 ret = rbd_init_disk(rbd_dev);
3566 if (ret)
3567 goto err_out_blkdev;
3568
3569 ret = rbd_bus_add_dev(rbd_dev);
3570 if (ret)
3571 goto err_out_disk;
3572
3573 /*
3574 * At this point cleanup in the event of an error is the job
3575 * of the sysfs code (initiated by rbd_bus_del_dev()).
3576 */
3577 down_write(&rbd_dev->header_rwsem);
3578 ret = rbd_dev_snaps_register(rbd_dev);
3579 up_write(&rbd_dev->header_rwsem);
3580 if (ret)
3581 goto err_out_bus;
3582
3583 ret = rbd_init_watch_dev(rbd_dev);
3584 if (ret)
3585 goto err_out_bus;
3586
3587 /* Everything's ready. Announce the disk to the world. */
3588
3589 add_disk(rbd_dev->disk);
3590
3591 pr_info("%s: added with size 0x%llx\n", rbd_dev->disk->disk_name,
3592 (unsigned long long) rbd_dev->mapping.size);
3593
3594 return ret;
3595err_out_bus:
3596 /* this will also clean up rest of rbd_dev stuff */
3597
3598 rbd_bus_del_dev(rbd_dev);
3599
3600 return ret;
3601err_out_disk:
3602 rbd_free_disk(rbd_dev);
3603err_out_blkdev:
3604 unregister_blkdev(rbd_dev->major, rbd_dev->name);
3605err_out_id:
3606 rbd_dev_id_put(rbd_dev);
3607err_out_snaps:
3608 rbd_remove_all_snaps(rbd_dev);
3609
3610 return ret;
3611}
3612
Alex Eldera30b71b2012-07-10 20:30:11 -05003613/*
3614 * Probe for the existence of the header object for the given rbd
3615 * device. For format 2 images this includes determining the image
3616 * id.
3617 */
3618static int rbd_dev_probe(struct rbd_device *rbd_dev)
3619{
3620 int ret;
3621
3622 /*
3623 * Get the id from the image id object. If it's not a
3624 * format 2 image, we'll get ENOENT back, and we'll assume
3625 * it's a format 1 image.
3626 */
3627 ret = rbd_dev_image_id(rbd_dev);
3628 if (ret)
3629 ret = rbd_dev_v1_probe(rbd_dev);
3630 else
3631 ret = rbd_dev_v2_probe(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05003632 if (ret) {
Alex Eldera30b71b2012-07-10 20:30:11 -05003633 dout("probe failed, returning %d\n", ret);
3634
Alex Elder83a06262012-10-30 15:47:17 -05003635 return ret;
3636 }
3637
3638 ret = rbd_dev_probe_finish(rbd_dev);
3639 if (ret)
3640 rbd_header_free(&rbd_dev->header);
3641
Alex Eldera30b71b2012-07-10 20:30:11 -05003642 return ret;
3643}
3644
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003645static ssize_t rbd_add(struct bus_type *bus,
3646 const char *buf,
3647 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003648{
Alex Eldercb8627c2012-07-09 21:04:23 -05003649 struct rbd_device *rbd_dev = NULL;
Alex Elderdc79b112012-10-25 23:34:41 -05003650 struct ceph_options *ceph_opts = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05003651 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05003652 struct rbd_spec *spec = NULL;
Alex Elder9d3997f2012-10-25 23:34:42 -05003653 struct rbd_client *rbdc;
Alex Elder27cc2592012-02-02 08:13:30 -06003654 struct ceph_osd_client *osdc;
3655 int rc = -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003656
3657 if (!try_module_get(THIS_MODULE))
3658 return -ENODEV;
3659
Alex Eldera725f65e2012-02-02 08:13:30 -06003660 /* parse add command */
Alex Elder859c31d2012-10-25 23:34:42 -05003661 rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec);
Alex Elderdc79b112012-10-25 23:34:41 -05003662 if (rc < 0)
Alex Elderbd4ba652012-10-25 23:34:42 -05003663 goto err_out_module;
Alex Eldera725f65e2012-02-02 08:13:30 -06003664
Alex Elder9d3997f2012-10-25 23:34:42 -05003665 rbdc = rbd_get_client(ceph_opts);
3666 if (IS_ERR(rbdc)) {
3667 rc = PTR_ERR(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05003668 goto err_out_args;
Alex Elder9d3997f2012-10-25 23:34:42 -05003669 }
Alex Elderc53d5892012-10-25 23:34:42 -05003670 ceph_opts = NULL; /* rbd_dev client now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003671
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003672 /* pick the pool */
Alex Elder9d3997f2012-10-25 23:34:42 -05003673 osdc = &rbdc->client->osdc;
Alex Elder859c31d2012-10-25 23:34:42 -05003674 rc = ceph_pg_poolid_by_name(osdc->osdmap, spec->pool_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003675 if (rc < 0)
3676 goto err_out_client;
Alex Elder859c31d2012-10-25 23:34:42 -05003677 spec->pool_id = (u64) rc;
3678
Alex Elder0903e872012-11-14 12:25:19 -06003679 /* The ceph file layout needs to fit pool id in 32 bits */
3680
3681 if (WARN_ON(spec->pool_id > (u64) U32_MAX)) {
3682 rc = -EIO;
3683 goto err_out_client;
3684 }
3685
Alex Elderc53d5892012-10-25 23:34:42 -05003686 rbd_dev = rbd_dev_create(rbdc, spec);
Alex Elderbd4ba652012-10-25 23:34:42 -05003687 if (!rbd_dev)
3688 goto err_out_client;
Alex Elderc53d5892012-10-25 23:34:42 -05003689 rbdc = NULL; /* rbd_dev now owns this */
3690 spec = NULL; /* rbd_dev now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003691
Alex Elderbd4ba652012-10-25 23:34:42 -05003692 rbd_dev->mapping.read_only = rbd_opts->read_only;
Alex Elderc53d5892012-10-25 23:34:42 -05003693 kfree(rbd_opts);
3694 rbd_opts = NULL; /* done with this */
Alex Elderbd4ba652012-10-25 23:34:42 -05003695
Alex Eldera30b71b2012-07-10 20:30:11 -05003696 rc = rbd_dev_probe(rbd_dev);
3697 if (rc < 0)
Alex Elderc53d5892012-10-25 23:34:42 -05003698 goto err_out_rbd_dev;
Alex Elder05fd6f62012-08-29 17:11:07 -05003699
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003700 return count;
Alex Elderc53d5892012-10-25 23:34:42 -05003701err_out_rbd_dev:
3702 rbd_dev_destroy(rbd_dev);
Alex Elderbd4ba652012-10-25 23:34:42 -05003703err_out_client:
Alex Elder9d3997f2012-10-25 23:34:42 -05003704 rbd_put_client(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05003705err_out_args:
Alex Elder78cea762012-10-25 23:34:41 -05003706 if (ceph_opts)
3707 ceph_destroy_options(ceph_opts);
Alex Elder4e9afeb2012-10-25 23:34:41 -05003708 kfree(rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05003709 rbd_spec_put(spec);
Alex Elderbd4ba652012-10-25 23:34:42 -05003710err_out_module:
3711 module_put(THIS_MODULE);
Alex Elder27cc2592012-02-02 08:13:30 -06003712
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003713 dout("Error adding device %s\n", buf);
Alex Elder27cc2592012-02-02 08:13:30 -06003714
3715 return (ssize_t) rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003716}
3717
Alex Elderde71a292012-07-03 16:01:19 -05003718static struct rbd_device *__rbd_get_dev(unsigned long dev_id)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003719{
3720 struct list_head *tmp;
3721 struct rbd_device *rbd_dev;
3722
Alex Eldere124a82f2012-01-29 13:57:44 -06003723 spin_lock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003724 list_for_each(tmp, &rbd_dev_list) {
3725 rbd_dev = list_entry(tmp, struct rbd_device, node);
Alex Elderde71a292012-07-03 16:01:19 -05003726 if (rbd_dev->dev_id == dev_id) {
Alex Eldere124a82f2012-01-29 13:57:44 -06003727 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003728 return rbd_dev;
Alex Eldere124a82f2012-01-29 13:57:44 -06003729 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003730 }
Alex Eldere124a82f2012-01-29 13:57:44 -06003731 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003732 return NULL;
3733}
3734
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003735static void rbd_dev_release(struct device *dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003736{
Alex Elder593a9e72012-02-07 12:03:37 -06003737 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003738
Alex Elder1dbb4392012-01-24 10:08:37 -06003739 if (rbd_dev->watch_request) {
3740 struct ceph_client *client = rbd_dev->rbd_client->client;
3741
3742 ceph_osdc_unregister_linger_request(&client->osdc,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003743 rbd_dev->watch_request);
Alex Elder1dbb4392012-01-24 10:08:37 -06003744 }
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003745 if (rbd_dev->watch_event)
Alex Elder907703d2012-11-13 21:11:15 -06003746 rbd_req_sync_watch(rbd_dev, 0);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003747
3748 /* clean up and free blkdev */
3749 rbd_free_disk(rbd_dev);
3750 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Alex Elder32eec682012-02-08 16:11:14 -06003751
Alex Elder2ac4e752012-07-10 20:30:10 -05003752 /* release allocated disk header fields */
3753 rbd_header_free(&rbd_dev->header);
3754
Alex Elder32eec682012-02-08 16:11:14 -06003755 /* done with the id, and with the rbd_dev */
Alex Eldere2839302012-08-29 17:11:06 -05003756 rbd_dev_id_put(rbd_dev);
Alex Elderc53d5892012-10-25 23:34:42 -05003757 rbd_assert(rbd_dev->rbd_client != NULL);
3758 rbd_dev_destroy(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003759
3760 /* release module ref */
3761 module_put(THIS_MODULE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003762}
3763
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003764static ssize_t rbd_remove(struct bus_type *bus,
3765 const char *buf,
3766 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003767{
3768 struct rbd_device *rbd_dev = NULL;
3769 int target_id, rc;
3770 unsigned long ul;
3771 int ret = count;
3772
3773 rc = strict_strtoul(buf, 10, &ul);
3774 if (rc)
3775 return rc;
3776
3777 /* convert to int; abort if we lost anything in the conversion */
3778 target_id = (int) ul;
3779 if (target_id != ul)
3780 return -EINVAL;
3781
3782 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
3783
3784 rbd_dev = __rbd_get_dev(target_id);
3785 if (!rbd_dev) {
3786 ret = -ENOENT;
3787 goto done;
3788 }
3789
Alex Elder42382b72012-11-16 09:29:16 -06003790 if (rbd_dev->open_count) {
3791 ret = -EBUSY;
3792 goto done;
3793 }
3794
Alex Elder41f38c22012-10-25 23:34:40 -05003795 rbd_remove_all_snaps(rbd_dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003796 rbd_bus_del_dev(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003797
3798done:
3799 mutex_unlock(&ctl_mutex);
Alex Elderaafb2302012-09-06 16:00:54 -05003800
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003801 return ret;
3802}
3803
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003804/*
3805 * create control files in sysfs
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003806 * /sys/bus/rbd/...
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003807 */
3808static int rbd_sysfs_init(void)
3809{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003810 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003811
Alex Elderfed4c142012-02-07 12:03:36 -06003812 ret = device_register(&rbd_root_dev);
Alex Elder21079782012-01-24 10:08:36 -06003813 if (ret < 0)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003814 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003815
Alex Elderfed4c142012-02-07 12:03:36 -06003816 ret = bus_register(&rbd_bus_type);
3817 if (ret < 0)
3818 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003819
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003820 return ret;
3821}
3822
3823static void rbd_sysfs_cleanup(void)
3824{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003825 bus_unregister(&rbd_bus_type);
Alex Elderfed4c142012-02-07 12:03:36 -06003826 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003827}
3828
3829int __init rbd_init(void)
3830{
3831 int rc;
3832
3833 rc = rbd_sysfs_init();
3834 if (rc)
3835 return rc;
Alex Elderf0f8cef2012-01-29 13:57:44 -06003836 pr_info("loaded " RBD_DRV_NAME_LONG "\n");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003837 return 0;
3838}
3839
3840void __exit rbd_exit(void)
3841{
3842 rbd_sysfs_cleanup();
3843}
3844
3845module_init(rbd_init);
3846module_exit(rbd_exit);
3847
3848MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
3849MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
3850MODULE_DESCRIPTION("rados block device");
3851
3852/* following authorship retained from original osdblk.c */
3853MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
3854
3855MODULE_LICENSE("GPL");