blob: 760be82548d94dc787fd62c06954be312bdf32bc [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 Durgin3591538f2011-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 -06001030static struct ceph_osd_req_op *rbd_create_rw_op(int opcode, u64 ofs, u64 len)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001031{
Alex Elder139b4312012-11-13 21:11:15 -06001032 struct ceph_osd_req_op *op;
Alex Elder57cfc102012-06-26 12:57:03 -07001033
Alex Elder139b4312012-11-13 21:11:15 -06001034 op = kzalloc(sizeof (*op), GFP_NOIO);
1035 if (!op)
Alex Elder57cfc102012-06-26 12:57:03 -07001036 return NULL;
Alex Elder8d23bf22012-11-19 22:55:21 -06001037
Alex Elder139b4312012-11-13 21:11:15 -06001038 op->op = opcode;
Alex Elder57cfc102012-06-26 12:57:03 -07001039
Alex Elder139b4312012-11-13 21:11:15 -06001040 return op;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001041}
1042
Alex Elder139b4312012-11-13 21:11:15 -06001043static void rbd_destroy_op(struct ceph_osd_req_op *op)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001044{
Alex Elder139b4312012-11-13 21:11:15 -06001045 kfree(op);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001046}
1047
Alex Elder8d23bf22012-11-19 22:55:21 -06001048struct ceph_osd_req_op *rbd_osd_req_op_create(u16 opcode, ...)
1049{
1050 struct ceph_osd_req_op *op;
1051 va_list args;
Alex Elder2647ba32012-11-19 22:55:21 -06001052 size_t size;
Alex Elder8d23bf22012-11-19 22:55:21 -06001053
1054 op = kzalloc(sizeof (*op), GFP_NOIO);
1055 if (!op)
1056 return NULL;
1057 op->op = opcode;
1058 va_start(args, opcode);
1059 switch (opcode) {
1060 case CEPH_OSD_OP_READ:
1061 case CEPH_OSD_OP_WRITE:
1062 /* rbd_osd_req_op_create(READ, offset, length) */
1063 /* rbd_osd_req_op_create(WRITE, offset, length) */
1064 op->extent.offset = va_arg(args, u64);
1065 op->extent.length = va_arg(args, u64);
1066 if (opcode == CEPH_OSD_OP_WRITE)
1067 op->payload_len = op->extent.length;
1068 break;
Alex Elder2647ba32012-11-19 22:55:21 -06001069 case CEPH_OSD_OP_CALL:
1070 /* rbd_osd_req_op_create(CALL, class, method, data, datalen) */
1071 op->cls.class_name = va_arg(args, char *);
1072 size = strlen(op->cls.class_name);
1073 rbd_assert(size <= (size_t) U8_MAX);
1074 op->cls.class_len = size;
1075 op->payload_len = size;
1076
1077 op->cls.method_name = va_arg(args, char *);
1078 size = strlen(op->cls.method_name);
1079 rbd_assert(size <= (size_t) U8_MAX);
1080 op->cls.method_len = size;
1081 op->payload_len += size;
1082
1083 op->cls.argc = 0;
1084 op->cls.indata = va_arg(args, void *);
1085 size = va_arg(args, size_t);
1086 rbd_assert(size <= (size_t) U32_MAX);
1087 op->cls.indata_len = (u32) size;
1088 op->payload_len += size;
1089 break;
Alex Elder8d23bf22012-11-19 22:55:21 -06001090 default:
1091 rbd_warn(NULL, "unsupported opcode %hu\n", opcode);
1092 kfree(op);
1093 op = NULL;
1094 break;
1095 }
1096 va_end(args);
1097
1098 return op;
1099}
1100
1101static void rbd_osd_req_op_destroy(struct ceph_osd_req_op *op)
1102{
1103 kfree(op);
1104}
1105
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001106static void rbd_coll_end_req_index(struct request *rq,
1107 struct rbd_req_coll *coll,
1108 int index,
Alex Elder8986cb32012-11-08 08:01:39 -06001109 s32 ret, u64 len)
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001110{
1111 struct request_queue *q;
1112 int min, max, i;
1113
Alex Elderbd919d42012-07-13 20:35:11 -05001114 dout("rbd_coll_end_req_index %p index %d ret %d len %llu\n",
Alex Elder8986cb32012-11-08 08:01:39 -06001115 coll, index, (int)ret, (unsigned long long)len);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001116
1117 if (!rq)
1118 return;
1119
1120 if (!coll) {
1121 blk_end_request(rq, ret, len);
1122 return;
1123 }
1124
1125 q = rq->q;
1126
1127 spin_lock_irq(q->queue_lock);
1128 coll->status[index].done = 1;
1129 coll->status[index].rc = ret;
1130 coll->status[index].bytes = len;
1131 max = min = coll->num_done;
1132 while (max < coll->total && coll->status[max].done)
1133 max++;
1134
1135 for (i = min; i<max; i++) {
Alex Elder8986cb32012-11-08 08:01:39 -06001136 __blk_end_request(rq, (int)coll->status[i].rc,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001137 coll->status[i].bytes);
1138 coll->num_done++;
1139 kref_put(&coll->kref, rbd_coll_release);
1140 }
1141 spin_unlock_irq(q->queue_lock);
1142}
1143
Alex Elder725afc92012-11-08 08:01:39 -06001144static void rbd_coll_end_req(struct rbd_request *rbd_req,
Alex Elder8986cb32012-11-08 08:01:39 -06001145 s32 ret, u64 len)
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001146{
Alex Elder725afc92012-11-08 08:01:39 -06001147 rbd_coll_end_req_index(rbd_req->rq,
1148 rbd_req->coll, rbd_req->coll_index,
1149 ret, len);
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001150}
1151
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001152/*
1153 * Send ceph osd request
1154 */
1155static int rbd_do_request(struct request *rq,
Alex Elder0ce1a792012-07-03 16:01:18 -05001156 struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001157 struct ceph_snap_context *snapc,
1158 u64 snapid,
Alex Elderaded07e2012-07-03 16:01:18 -05001159 const char *object_name, u64 ofs, u64 len,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001160 struct bio *bio,
1161 struct page **pages,
1162 int num_pages,
1163 int flags,
Alex Elder30573d62012-11-13 21:11:15 -06001164 struct ceph_osd_req_op *op,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001165 struct rbd_req_coll *coll,
1166 int coll_index,
Alex Elder5f29ddd2012-11-08 08:01:39 -06001167 void (*rbd_cb)(struct ceph_osd_request *,
1168 struct ceph_msg *),
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001169 struct ceph_osd_request **linger_req,
1170 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001171{
Alex Elder1dbb4392012-01-24 10:08:37 -06001172 struct ceph_osd_client *osdc;
Alex Elder2e53c6c2012-11-30 09:59:47 -06001173 struct ceph_osd_request *osd_req;
1174 struct rbd_request *rbd_req = NULL;
1175 struct timespec mtime = CURRENT_TIME;
1176 int ret;
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001177
Alex Elderf7760da2012-10-20 22:17:27 -05001178 dout("rbd_do_request object_name=%s ofs=%llu len=%llu coll=%p[%d]\n",
1179 object_name, (unsigned long long) ofs,
1180 (unsigned long long) len, coll, coll_index);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001181
Alex Elder0ce1a792012-07-03 16:01:18 -05001182 osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder30573d62012-11-13 21:11:15 -06001183 osd_req = ceph_osdc_alloc_request(osdc, snapc, 1, false, GFP_NOIO);
Alex Elder2e53c6c2012-11-30 09:59:47 -06001184 if (!osd_req)
1185 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001186
Alex Elderd178a9e2012-11-13 21:11:15 -06001187 osd_req->r_flags = flags;
Alex Elder54a54002012-11-13 21:11:15 -06001188 osd_req->r_pages = pages;
1189 if (bio) {
1190 osd_req->r_bio = bio;
1191 bio_get(osd_req->r_bio);
1192 }
Alex Elder2e53c6c2012-11-30 09:59:47 -06001193
Alex Elder18216652012-11-30 09:59:47 -06001194 if (coll) {
Alex Elder2e53c6c2012-11-30 09:59:47 -06001195 ret = -ENOMEM;
1196 rbd_req = kmalloc(sizeof(*rbd_req), GFP_NOIO);
1197 if (!rbd_req)
1198 goto done_osd_req;
1199
1200 rbd_req->rq = rq;
1201 rbd_req->bio = bio;
1202 rbd_req->pages = pages;
1203 rbd_req->len = len;
1204 rbd_req->coll = coll;
Alex Elder18216652012-11-30 09:59:47 -06001205 rbd_req->coll_index = coll_index;
Alex Elder2e53c6c2012-11-30 09:59:47 -06001206 }
1207
Alex Elder5f29ddd2012-11-08 08:01:39 -06001208 osd_req->r_callback = rbd_cb;
Alex Elder5f29ddd2012-11-08 08:01:39 -06001209 osd_req->r_priv = rbd_req;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001210
Alex Elder5f29ddd2012-11-08 08:01:39 -06001211 strncpy(osd_req->r_oid, object_name, sizeof(osd_req->r_oid));
1212 osd_req->r_oid_len = strlen(osd_req->r_oid);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001213
Alex Elder0903e872012-11-14 12:25:19 -06001214 osd_req->r_file_layout = rbd_dev->layout; /* struct */
Alex Eldere01e7922012-11-14 12:25:18 -06001215 osd_req->r_num_pages = calc_pages_for(ofs, len);
1216 osd_req->r_page_alignment = ofs & ~PAGE_MASK;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001217
Alex Elder30573d62012-11-13 21:11:15 -06001218 ceph_osdc_build_request(osd_req, ofs, len, 1, op,
Alex Elderae7ca4a32012-11-13 21:11:15 -06001219 snapc, snapid, &mtime);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001220
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001221 if (linger_req) {
Alex Elder5f29ddd2012-11-08 08:01:39 -06001222 ceph_osdc_set_request_linger(osdc, osd_req);
1223 *linger_req = osd_req;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001224 }
1225
Alex Elder5f29ddd2012-11-08 08:01:39 -06001226 ret = ceph_osdc_start_request(osdc, osd_req, false);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001227 if (ret < 0)
1228 goto done_err;
1229
1230 if (!rbd_cb) {
Alex Elder5f29ddd2012-11-08 08:01:39 -06001231 u64 version;
1232
1233 ret = ceph_osdc_wait_request(osdc, osd_req);
1234 version = le64_to_cpu(osd_req->r_reassert_version.version);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001235 if (ver)
Alex Elder5f29ddd2012-11-08 08:01:39 -06001236 *ver = version;
1237 dout("reassert_ver=%llu\n", (unsigned long long) version);
1238 ceph_osdc_put_request(osd_req);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001239 }
1240 return ret;
1241
1242done_err:
Alex Elder2e53c6c2012-11-30 09:59:47 -06001243 if (bio)
1244 bio_chain_put(osd_req->r_bio);
Alex Elder725afc92012-11-08 08:01:39 -06001245 kfree(rbd_req);
Alex Elder2e53c6c2012-11-30 09:59:47 -06001246done_osd_req:
1247 ceph_osdc_put_request(osd_req);
1248
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001249 return ret;
1250}
1251
1252/*
1253 * Ceph osd op callback
1254 */
Alex Elder5f29ddd2012-11-08 08:01:39 -06001255static void rbd_req_cb(struct ceph_osd_request *osd_req, struct ceph_msg *msg)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001256{
Alex Elder5f29ddd2012-11-08 08:01:39 -06001257 struct rbd_request *rbd_req = osd_req->r_priv;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001258 struct ceph_osd_reply_head *replyhead;
1259 struct ceph_osd_op *op;
Alex Elder8986cb32012-11-08 08:01:39 -06001260 s32 rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001261 u64 bytes;
1262 int read_op;
1263
1264 /* parse reply */
1265 replyhead = msg->front.iov_base;
1266 WARN_ON(le32_to_cpu(replyhead->num_ops) == 0);
1267 op = (void *)(replyhead + 1);
Alex Elder8986cb32012-11-08 08:01:39 -06001268 rc = (s32)le32_to_cpu(replyhead->result);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001269 bytes = le64_to_cpu(op->extent.length);
Dan Carpenter895cfcc2012-06-06 09:15:33 -05001270 read_op = (le16_to_cpu(op->op) == CEPH_OSD_OP_READ);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001271
Alex Elderbd919d42012-07-13 20:35:11 -05001272 dout("rbd_req_cb bytes=%llu readop=%d rc=%d\n",
1273 (unsigned long long) bytes, read_op, (int) rc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001274
Alex Elder8986cb32012-11-08 08:01:39 -06001275 if (rc == (s32)-ENOENT && read_op) {
Alex Elder725afc92012-11-08 08:01:39 -06001276 zero_bio_chain(rbd_req->bio, 0);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001277 rc = 0;
Alex Elder725afc92012-11-08 08:01:39 -06001278 } else if (rc == 0 && read_op && bytes < rbd_req->len) {
1279 zero_bio_chain(rbd_req->bio, bytes);
1280 bytes = rbd_req->len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001281 }
1282
Alex Elder725afc92012-11-08 08:01:39 -06001283 rbd_coll_end_req(rbd_req, rc, bytes);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001284
Alex Elder725afc92012-11-08 08:01:39 -06001285 if (rbd_req->bio)
1286 bio_chain_put(rbd_req->bio);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001287
Alex Elder5f29ddd2012-11-08 08:01:39 -06001288 ceph_osdc_put_request(osd_req);
Alex Elder725afc92012-11-08 08:01:39 -06001289 kfree(rbd_req);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001290}
1291
Alex Elder5f29ddd2012-11-08 08:01:39 -06001292static void rbd_simple_req_cb(struct ceph_osd_request *osd_req,
1293 struct ceph_msg *msg)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001294{
Alex Elder5f29ddd2012-11-08 08:01:39 -06001295 ceph_osdc_put_request(osd_req);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001296}
1297
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001298/*
1299 * Do a synchronous ceph osd operation
1300 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001301static int rbd_req_sync_op(struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001302 int flags,
Alex Elder30573d62012-11-13 21:11:15 -06001303 struct ceph_osd_req_op *op,
Alex Elderaded07e2012-07-03 16:01:18 -05001304 const char *object_name,
Alex Elderf8d4de62012-07-03 16:01:19 -05001305 u64 ofs, u64 inbound_size,
1306 char *inbound,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001307 struct ceph_osd_request **linger_req,
1308 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001309{
1310 int ret;
1311 struct page **pages;
1312 int num_pages;
Alex Elder913d2fd2012-06-26 12:57:03 -07001313
Alex Elder30573d62012-11-13 21:11:15 -06001314 rbd_assert(op != NULL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001315
Alex Elderf8d4de62012-07-03 16:01:19 -05001316 num_pages = calc_pages_for(ofs, inbound_size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001317 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
Dan Carpenterb8d06382010-10-11 21:14:23 +02001318 if (IS_ERR(pages))
1319 return PTR_ERR(pages);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001320
Alex Elder25704ac2012-11-09 08:43:16 -06001321 ret = rbd_do_request(NULL, rbd_dev, NULL, CEPH_NOSNAP,
Alex Elderf8d4de62012-07-03 16:01:19 -05001322 object_name, ofs, inbound_size, NULL,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001323 pages, num_pages,
1324 flags,
Alex Elder30573d62012-11-13 21:11:15 -06001325 op,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001326 NULL, 0,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001327 NULL,
1328 linger_req, ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001329 if (ret < 0)
Alex Elder913d2fd2012-06-26 12:57:03 -07001330 goto done;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001331
Alex Elderf8d4de62012-07-03 16:01:19 -05001332 if ((flags & CEPH_OSD_FLAG_READ) && inbound)
1333 ret = ceph_copy_from_page_vector(pages, inbound, ofs, ret);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001334
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001335done:
1336 ceph_release_page_vector(pages, num_pages);
1337 return ret;
1338}
1339
1340/*
1341 * Do an asynchronous ceph osd operation
1342 */
1343static int rbd_do_op(struct request *rq,
Alex Elder0ce1a792012-07-03 16:01:18 -05001344 struct rbd_device *rbd_dev,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001345 struct ceph_snap_context *snapc,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001346 u64 ofs, u64 len,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001347 struct bio *bio,
1348 struct rbd_req_coll *coll,
1349 int coll_index)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001350{
1351 char *seg_name;
1352 u64 seg_ofs;
1353 u64 seg_len;
1354 int ret;
Alex Elder139b4312012-11-13 21:11:15 -06001355 struct ceph_osd_req_op *op;
Alex Elderff2e4bb2012-10-10 18:59:29 -07001356 int opcode;
1357 int flags;
Alex Elder46342462012-10-10 18:59:29 -07001358 u64 snapid;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001359
Alex Elder65ccfe22012-08-09 10:33:26 -07001360 seg_name = rbd_segment_name(rbd_dev, ofs);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001361 if (!seg_name)
1362 return -ENOMEM;
Alex Elder65ccfe22012-08-09 10:33:26 -07001363 seg_len = rbd_segment_length(rbd_dev, ofs, len);
1364 seg_ofs = rbd_segment_offset(rbd_dev, ofs);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001365
Alex Elderff2e4bb2012-10-10 18:59:29 -07001366 if (rq_data_dir(rq) == WRITE) {
1367 opcode = CEPH_OSD_OP_WRITE;
1368 flags = CEPH_OSD_FLAG_WRITE|CEPH_OSD_FLAG_ONDISK;
Alex Elder46342462012-10-10 18:59:29 -07001369 snapid = CEPH_NOSNAP;
Alex Elderff2e4bb2012-10-10 18:59:29 -07001370 } else {
1371 opcode = CEPH_OSD_OP_READ;
1372 flags = CEPH_OSD_FLAG_READ;
Alex Eldera7b4c652012-11-09 08:43:15 -06001373 rbd_assert(!snapc);
Alex Elder0d7dbfc2012-10-25 23:34:41 -05001374 snapid = rbd_dev->spec->snap_id;
Alex Elderff2e4bb2012-10-10 18:59:29 -07001375 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001376
Alex Elder57cfc102012-06-26 12:57:03 -07001377 ret = -ENOMEM;
Alex Elder8d23bf22012-11-19 22:55:21 -06001378 op = rbd_osd_req_op_create(opcode, seg_ofs, seg_len);
Alex Elder139b4312012-11-13 21:11:15 -06001379 if (!op)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001380 goto done;
1381
1382 /* we've taken care of segment sizes earlier when we
1383 cloned the bios. We should never have a segment
1384 truncated at this point */
Alex Elderaafb2302012-09-06 16:00:54 -05001385 rbd_assert(seg_len == len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001386
1387 ret = rbd_do_request(rq, rbd_dev, snapc, snapid,
1388 seg_name, seg_ofs, seg_len,
1389 bio,
1390 NULL, 0,
1391 flags,
Alex Elder30573d62012-11-13 21:11:15 -06001392 op,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001393 coll, coll_index,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001394 rbd_req_cb, 0, NULL);
Alex Eldercd323ac2012-11-08 08:01:39 -06001395 if (ret < 0)
1396 rbd_coll_end_req_index(rq, coll, coll_index,
1397 (s32)ret, seg_len);
Alex Elder8d23bf22012-11-19 22:55:21 -06001398 rbd_osd_req_op_destroy(op);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001399done:
1400 kfree(seg_name);
1401 return ret;
1402}
1403
1404/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001405 * Request sync osd read
1406 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001407static int rbd_req_sync_read(struct rbd_device *rbd_dev,
Alex Elderaded07e2012-07-03 16:01:18 -05001408 const char *object_name,
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001409 u64 ofs, u64 len,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001410 char *buf,
1411 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001412{
Alex Elder139b4312012-11-13 21:11:15 -06001413 struct ceph_osd_req_op *op;
Alex Elder913d2fd2012-06-26 12:57:03 -07001414 int ret;
1415
Alex Elder8d23bf22012-11-19 22:55:21 -06001416 op = rbd_osd_req_op_create(CEPH_OSD_OP_READ, ofs, len);
Alex Elder139b4312012-11-13 21:11:15 -06001417 if (!op)
Alex Elder913d2fd2012-06-26 12:57:03 -07001418 return -ENOMEM;
1419
Alex Elder25704ac2012-11-09 08:43:16 -06001420 ret = rbd_req_sync_op(rbd_dev, CEPH_OSD_FLAG_READ,
Alex Elder30573d62012-11-13 21:11:15 -06001421 op, object_name, ofs, len, buf, NULL, ver);
Alex Elder8d23bf22012-11-19 22:55:21 -06001422 rbd_osd_req_op_destroy(op);
Alex Elder913d2fd2012-06-26 12:57:03 -07001423
1424 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001425}
1426
1427/*
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001428 * Request sync osd watch
1429 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001430static int rbd_req_sync_notify_ack(struct rbd_device *rbd_dev,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001431 u64 ver,
Alex Elder7f0a24d2012-07-25 09:32:40 -05001432 u64 notify_id)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001433{
Alex Elder139b4312012-11-13 21:11:15 -06001434 struct ceph_osd_req_op *op;
Sage Weil11f77002011-05-12 16:13:54 -07001435 int ret;
1436
Alex Elder8d23bf22012-11-19 22:55:21 -06001437 op = rbd_create_rw_op(CEPH_OSD_OP_NOTIFY_ACK, 0, 0);
Alex Elder139b4312012-11-13 21:11:15 -06001438 if (!op)
Alex Elder57cfc102012-06-26 12:57:03 -07001439 return -ENOMEM;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001440
Alex Elder139b4312012-11-13 21:11:15 -06001441 op->watch.ver = cpu_to_le64(ver);
1442 op->watch.cookie = notify_id;
1443 op->watch.flag = 0;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001444
Alex Elder0ce1a792012-07-03 16:01:18 -05001445 ret = rbd_do_request(NULL, rbd_dev, NULL, CEPH_NOSNAP,
Alex Elder7f0a24d2012-07-25 09:32:40 -05001446 rbd_dev->header_name, 0, 0, NULL,
Alex Elderad4f2322012-07-03 16:01:19 -05001447 NULL, 0,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001448 CEPH_OSD_FLAG_READ,
Alex Elder30573d62012-11-13 21:11:15 -06001449 op,
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001450 NULL, 0,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001451 rbd_simple_req_cb, 0, NULL);
1452
Alex Elder139b4312012-11-13 21:11:15 -06001453 rbd_destroy_op(op);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001454 return ret;
1455}
1456
1457static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
1458{
Alex Elder0ce1a792012-07-03 16:01:18 -05001459 struct rbd_device *rbd_dev = (struct rbd_device *)data;
Josh Durgina71b8912011-12-05 18:10:44 -08001460 u64 hver;
Sage Weil13143d22011-05-12 16:08:30 -07001461 int rc;
1462
Alex Elder0ce1a792012-07-03 16:01:18 -05001463 if (!rbd_dev)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001464 return;
1465
Alex Elderbd919d42012-07-13 20:35:11 -05001466 dout("rbd_watch_cb %s notify_id=%llu opcode=%u\n",
1467 rbd_dev->header_name, (unsigned long long) notify_id,
1468 (unsigned int) opcode);
Alex Elder117973f2012-08-31 17:29:55 -05001469 rc = rbd_dev_refresh(rbd_dev, &hver);
Sage Weil13143d22011-05-12 16:08:30 -07001470 if (rc)
Alex Elder06ecc6c2012-11-01 10:17:15 -05001471 rbd_warn(rbd_dev, "got notification but failed to "
1472 " update snaps: %d\n", rc);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001473
Alex Elder7f0a24d2012-07-25 09:32:40 -05001474 rbd_req_sync_notify_ack(rbd_dev, hver, notify_id);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001475}
1476
1477/*
Alex Elder907703d2012-11-13 21:11:15 -06001478 * Request sync osd watch/unwatch. The value of "start" determines
1479 * whether a watch request is being initiated or torn down.
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001480 */
Alex Elder907703d2012-11-13 21:11:15 -06001481static int rbd_req_sync_watch(struct rbd_device *rbd_dev, int start)
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001482{
Alex Elder139b4312012-11-13 21:11:15 -06001483 struct ceph_osd_req_op *op;
Alex Elder907703d2012-11-13 21:11:15 -06001484 struct ceph_osd_request **linger_req = NULL;
1485 __le64 version = 0;
Alex Elder57cfc102012-06-26 12:57:03 -07001486 int ret;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001487
Alex Elder8d23bf22012-11-19 22:55:21 -06001488 op = rbd_create_rw_op(CEPH_OSD_OP_WATCH, 0, 0);
Alex Elder139b4312012-11-13 21:11:15 -06001489 if (!op)
Alex Elder57cfc102012-06-26 12:57:03 -07001490 return -ENOMEM;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001491
Alex Elder907703d2012-11-13 21:11:15 -06001492 if (start) {
1493 struct ceph_osd_client *osdc;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001494
Alex Elder907703d2012-11-13 21:11:15 -06001495 osdc = &rbd_dev->rbd_client->client->osdc;
1496 ret = ceph_osdc_create_event(osdc, rbd_watch_cb, 0, rbd_dev,
1497 &rbd_dev->watch_event);
1498 if (ret < 0)
1499 goto done;
1500 version = cpu_to_le64(rbd_dev->header.obj_version);
1501 linger_req = &rbd_dev->watch_request;
1502 }
1503
1504 op->watch.ver = version;
Alex Elder139b4312012-11-13 21:11:15 -06001505 op->watch.cookie = cpu_to_le64(rbd_dev->watch_event->cookie);
Alex Elder907703d2012-11-13 21:11:15 -06001506 op->watch.flag = (u8) start ? 1 : 0;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001507
Alex Elder25704ac2012-11-09 08:43:16 -06001508 ret = rbd_req_sync_op(rbd_dev,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001509 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
Alex Elder907703d2012-11-13 21:11:15 -06001510 op, rbd_dev->header_name,
1511 0, 0, NULL, linger_req, NULL);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001512
Alex Elder907703d2012-11-13 21:11:15 -06001513 if (!start || ret < 0) {
1514 ceph_osdc_cancel_event(rbd_dev->watch_event);
1515 rbd_dev->watch_event = NULL;
1516 }
1517done:
Alex Elder139b4312012-11-13 21:11:15 -06001518 rbd_destroy_op(op);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001519
Yehuda Sadeh79e3057c2011-07-12 16:56:57 -07001520 return ret;
1521}
1522
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001523/*
Alex Elder3cb4a682012-06-26 12:57:03 -07001524 * Synchronous osd object method call
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001525 */
Alex Elder0ce1a792012-07-03 16:01:18 -05001526static int rbd_req_sync_exec(struct rbd_device *rbd_dev,
Alex Elderaded07e2012-07-03 16:01:18 -05001527 const char *object_name,
1528 const char *class_name,
1529 const char *method_name,
Alex Elder3cb4a682012-06-26 12:57:03 -07001530 const char *outbound,
1531 size_t outbound_size,
Alex Elderf8d4de62012-07-03 16:01:19 -05001532 char *inbound,
1533 size_t inbound_size,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07001534 u64 *ver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001535{
Alex Elder139b4312012-11-13 21:11:15 -06001536 struct ceph_osd_req_op *op;
Alex Elder57cfc102012-06-26 12:57:03 -07001537 int ret;
1538
Alex Elder3cb4a682012-06-26 12:57:03 -07001539 /*
1540 * Any input parameters required by the method we're calling
1541 * will be sent along with the class and method names as
1542 * part of the message payload. That data and its size are
1543 * supplied via the indata and indata_len fields (named from
1544 * the perspective of the server side) in the OSD request
1545 * operation.
1546 */
Alex Elder2647ba32012-11-19 22:55:21 -06001547 op = rbd_osd_req_op_create(CEPH_OSD_OP_CALL, class_name,
1548 method_name, outbound, outbound_size);
Alex Elder139b4312012-11-13 21:11:15 -06001549 if (!op)
Alex Elder57cfc102012-06-26 12:57:03 -07001550 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001551
Alex Elder30573d62012-11-13 21:11:15 -06001552 ret = rbd_req_sync_op(rbd_dev, CEPH_OSD_FLAG_READ, op,
Alex Elderf8d4de62012-07-03 16:01:19 -05001553 object_name, 0, inbound_size, inbound,
1554 NULL, ver);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001555
Alex Elder2647ba32012-11-19 22:55:21 -06001556 rbd_osd_req_op_destroy(op);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001557
1558 dout("cls_exec returned %d\n", ret);
1559 return ret;
1560}
1561
Yehuda Sadeh1fec7092011-05-13 13:52:56 -07001562static struct rbd_req_coll *rbd_alloc_coll(int num_reqs)
1563{
1564 struct rbd_req_coll *coll =
1565 kzalloc(sizeof(struct rbd_req_coll) +
1566 sizeof(struct rbd_req_status) * num_reqs,
1567 GFP_ATOMIC);
1568
1569 if (!coll)
1570 return NULL;
1571 coll->total = num_reqs;
1572 kref_init(&coll->kref);
1573 return coll;
1574}
1575
Alex Elder8295cda2012-11-08 08:01:39 -06001576static int rbd_dev_do_request(struct request *rq,
1577 struct rbd_device *rbd_dev,
1578 struct ceph_snap_context *snapc,
1579 u64 ofs, unsigned int size,
1580 struct bio *bio_chain)
1581{
1582 int num_segs;
1583 struct rbd_req_coll *coll;
1584 unsigned int bio_offset;
1585 int cur_seg = 0;
1586
1587 dout("%s 0x%x bytes at 0x%llx\n",
1588 rq_data_dir(rq) == WRITE ? "write" : "read",
1589 size, (unsigned long long) blk_rq_pos(rq) * SECTOR_SIZE);
1590
1591 num_segs = rbd_get_num_segments(&rbd_dev->header, ofs, size);
1592 if (num_segs <= 0)
1593 return num_segs;
1594
1595 coll = rbd_alloc_coll(num_segs);
1596 if (!coll)
1597 return -ENOMEM;
1598
1599 bio_offset = 0;
1600 do {
1601 u64 limit = rbd_segment_length(rbd_dev, ofs, size);
1602 unsigned int clone_size;
1603 struct bio *bio_clone;
1604
1605 BUG_ON(limit > (u64)UINT_MAX);
1606 clone_size = (unsigned int)limit;
1607 dout("bio_chain->bi_vcnt=%hu\n", bio_chain->bi_vcnt);
1608
1609 kref_get(&coll->kref);
1610
1611 /* Pass a cloned bio chain via an osd request */
1612
1613 bio_clone = bio_chain_clone_range(&bio_chain,
1614 &bio_offset, clone_size,
1615 GFP_ATOMIC);
1616 if (bio_clone)
1617 (void)rbd_do_op(rq, rbd_dev, snapc,
1618 ofs, clone_size,
1619 bio_clone, coll, cur_seg);
1620 else
1621 rbd_coll_end_req_index(rq, coll, cur_seg,
1622 (s32)-ENOMEM,
1623 clone_size);
1624 size -= clone_size;
1625 ofs += clone_size;
1626
1627 cur_seg++;
1628 } while (size > 0);
1629 kref_put(&coll->kref, rbd_coll_release);
1630
1631 return 0;
1632}
1633
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001634/*
1635 * block device queue callback
1636 */
1637static void rbd_rq_fn(struct request_queue *q)
1638{
1639 struct rbd_device *rbd_dev = q->queuedata;
Alex Elderb395e8b2012-11-08 08:01:39 -06001640 bool read_only = rbd_dev->mapping.read_only;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001641 struct request *rq;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001642
Alex Elder00f1f362012-02-07 12:03:36 -06001643 while ((rq = blk_fetch_request(q))) {
Alex Elderb395e8b2012-11-08 08:01:39 -06001644 struct ceph_snap_context *snapc = NULL;
1645 unsigned int size = 0;
Alex Elder8295cda2012-11-08 08:01:39 -06001646 int result;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001647
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001648 dout("fetched request\n");
1649
Alex Elderb395e8b2012-11-08 08:01:39 -06001650 /* Filter out block requests we don't understand */
1651
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001652 if ((rq->cmd_type != REQ_TYPE_FS)) {
1653 __blk_end_request_all(rq, 0);
Alex Elder00f1f362012-02-07 12:03:36 -06001654 continue;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001655 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001656 spin_unlock_irq(q->queue_lock);
1657
Alex Eldera7b4c652012-11-09 08:43:15 -06001658 /* Write requests need a reference to the snapshot context */
Alex Elderb395e8b2012-11-08 08:01:39 -06001659
Alex Eldera7b4c652012-11-09 08:43:15 -06001660 if (rq_data_dir(rq) == WRITE) {
1661 result = -EROFS;
1662 if (read_only) /* Can't write to a read-only device */
1663 goto out_end_request;
Alex Elderb395e8b2012-11-08 08:01:39 -06001664
Alex Eldera7b4c652012-11-09 08:43:15 -06001665 /*
1666 * Note that each osd request will take its
1667 * own reference to the snapshot context
1668 * supplied. The reference we take here
1669 * just guarantees the one we provide stays
1670 * valid.
1671 */
1672 down_read(&rbd_dev->header_rwsem);
Alex Elderb395e8b2012-11-08 08:01:39 -06001673 snapc = ceph_get_snap_context(rbd_dev->header.snapc);
Alex Eldera7b4c652012-11-09 08:43:15 -06001674 up_read(&rbd_dev->header_rwsem);
Alex Elderb395e8b2012-11-08 08:01:39 -06001675 rbd_assert(snapc != NULL);
Alex Eldera7b4c652012-11-09 08:43:15 -06001676 } else if (!atomic_read(&rbd_dev->exists)) {
Alex Elderb395e8b2012-11-08 08:01:39 -06001677 rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP);
1678 dout("request for non-existent snapshot");
1679 result = -ENXIO;
1680 goto out_end_request;
1681 }
Alex Elderf7760da2012-10-20 22:17:27 -05001682
Alex Elderb395e8b2012-11-08 08:01:39 -06001683 size = blk_rq_bytes(rq);
1684 result = rbd_dev_do_request(rq, rbd_dev, snapc,
1685 blk_rq_pos(rq) * SECTOR_SIZE,
1686 size, rq->bio);
1687out_end_request:
Alex Eldera7b4c652012-11-09 08:43:15 -06001688 if (snapc)
1689 ceph_put_snap_context(snapc);
Alex Elder8295cda2012-11-08 08:01:39 -06001690 spin_lock_irq(q->queue_lock);
1691 if (!size || result < 0)
1692 __blk_end_request_all(rq, result);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001693 }
1694}
1695
1696/*
1697 * a queue callback. Makes sure that we don't create a bio that spans across
1698 * multiple osd objects. One exception would be with a single page bios,
Alex Elderf7760da2012-10-20 22:17:27 -05001699 * which we handle later at bio_chain_clone_range()
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001700 */
1701static int rbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd,
1702 struct bio_vec *bvec)
1703{
1704 struct rbd_device *rbd_dev = q->queuedata;
Alex Eldere5cfeed22012-10-20 22:17:27 -05001705 sector_t sector_offset;
1706 sector_t sectors_per_obj;
1707 sector_t obj_sector_offset;
1708 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001709
Alex Eldere5cfeed22012-10-20 22:17:27 -05001710 /*
1711 * Find how far into its rbd object the partition-relative
1712 * bio start sector is to offset relative to the enclosing
1713 * device.
1714 */
1715 sector_offset = get_start_sect(bmd->bi_bdev) + bmd->bi_sector;
1716 sectors_per_obj = 1 << (rbd_dev->header.obj_order - SECTOR_SHIFT);
1717 obj_sector_offset = sector_offset & (sectors_per_obj - 1);
Alex Elder593a9e72012-02-07 12:03:37 -06001718
Alex Eldere5cfeed22012-10-20 22:17:27 -05001719 /*
1720 * Compute the number of bytes from that offset to the end
1721 * of the object. Account for what's already used by the bio.
1722 */
1723 ret = (int) (sectors_per_obj - obj_sector_offset) << SECTOR_SHIFT;
1724 if (ret > bmd->bi_size)
1725 ret -= bmd->bi_size;
1726 else
1727 ret = 0;
1728
1729 /*
1730 * Don't send back more than was asked for. And if the bio
1731 * was empty, let the whole thing through because: "Note
1732 * that a block device *must* allow a single page to be
1733 * added to an empty bio."
1734 */
1735 rbd_assert(bvec->bv_len <= PAGE_SIZE);
1736 if (ret > (int) bvec->bv_len || !bmd->bi_size)
1737 ret = (int) bvec->bv_len;
1738
1739 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001740}
1741
1742static void rbd_free_disk(struct rbd_device *rbd_dev)
1743{
1744 struct gendisk *disk = rbd_dev->disk;
1745
1746 if (!disk)
1747 return;
1748
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001749 if (disk->flags & GENHD_FL_UP)
1750 del_gendisk(disk);
1751 if (disk->queue)
1752 blk_cleanup_queue(disk->queue);
1753 put_disk(disk);
1754}
1755
1756/*
Alex Elder4156d992012-08-02 11:29:46 -05001757 * Read the complete header for the given rbd device.
1758 *
1759 * Returns a pointer to a dynamically-allocated buffer containing
1760 * the complete and validated header. Caller can pass the address
1761 * of a variable that will be filled in with the version of the
1762 * header object at the time it was read.
1763 *
1764 * Returns a pointer-coded errno if a failure occurs.
1765 */
1766static struct rbd_image_header_ondisk *
1767rbd_dev_v1_header_read(struct rbd_device *rbd_dev, u64 *version)
1768{
1769 struct rbd_image_header_ondisk *ondisk = NULL;
1770 u32 snap_count = 0;
1771 u64 names_size = 0;
1772 u32 want_count;
1773 int ret;
1774
1775 /*
1776 * The complete header will include an array of its 64-bit
1777 * snapshot ids, followed by the names of those snapshots as
1778 * a contiguous block of NUL-terminated strings. Note that
1779 * the number of snapshots could change by the time we read
1780 * it in, in which case we re-read it.
1781 */
1782 do {
1783 size_t size;
1784
1785 kfree(ondisk);
1786
1787 size = sizeof (*ondisk);
1788 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
1789 size += names_size;
1790 ondisk = kmalloc(size, GFP_KERNEL);
1791 if (!ondisk)
1792 return ERR_PTR(-ENOMEM);
1793
Alex Elder47756182012-11-09 08:43:15 -06001794 ret = rbd_req_sync_read(rbd_dev, rbd_dev->header_name,
Alex Elder4156d992012-08-02 11:29:46 -05001795 0, size,
1796 (char *) ondisk, version);
1797
1798 if (ret < 0)
1799 goto out_err;
1800 if (WARN_ON((size_t) ret < size)) {
1801 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05001802 rbd_warn(rbd_dev, "short header read (want %zd got %d)",
1803 size, ret);
Alex Elder4156d992012-08-02 11:29:46 -05001804 goto out_err;
1805 }
1806 if (!rbd_dev_ondisk_valid(ondisk)) {
1807 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05001808 rbd_warn(rbd_dev, "invalid header");
Alex Elder4156d992012-08-02 11:29:46 -05001809 goto out_err;
1810 }
1811
1812 names_size = le64_to_cpu(ondisk->snap_names_len);
1813 want_count = snap_count;
1814 snap_count = le32_to_cpu(ondisk->snap_count);
1815 } while (snap_count != want_count);
1816
1817 return ondisk;
1818
1819out_err:
1820 kfree(ondisk);
1821
1822 return ERR_PTR(ret);
1823}
1824
1825/*
1826 * reload the ondisk the header
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001827 */
1828static int rbd_read_header(struct rbd_device *rbd_dev,
1829 struct rbd_image_header *header)
1830{
Alex Elder4156d992012-08-02 11:29:46 -05001831 struct rbd_image_header_ondisk *ondisk;
1832 u64 ver = 0;
1833 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001834
Alex Elder4156d992012-08-02 11:29:46 -05001835 ondisk = rbd_dev_v1_header_read(rbd_dev, &ver);
1836 if (IS_ERR(ondisk))
1837 return PTR_ERR(ondisk);
1838 ret = rbd_header_from_disk(header, ondisk);
1839 if (ret >= 0)
1840 header->obj_version = ver;
1841 kfree(ondisk);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001842
Alex Elder4156d992012-08-02 11:29:46 -05001843 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001844}
1845
Alex Elder41f38c22012-10-25 23:34:40 -05001846static void rbd_remove_all_snaps(struct rbd_device *rbd_dev)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001847{
1848 struct rbd_snap *snap;
Alex Eldera0593292012-07-19 09:09:27 -05001849 struct rbd_snap *next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001850
Alex Eldera0593292012-07-19 09:09:27 -05001851 list_for_each_entry_safe(snap, next, &rbd_dev->snaps, node)
Alex Elder41f38c22012-10-25 23:34:40 -05001852 rbd_remove_snap_dev(snap);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001853}
1854
Alex Elder94785542012-10-09 13:50:17 -07001855static void rbd_update_mapping_size(struct rbd_device *rbd_dev)
1856{
1857 sector_t size;
1858
Alex Elder0d7dbfc2012-10-25 23:34:41 -05001859 if (rbd_dev->spec->snap_id != CEPH_NOSNAP)
Alex Elder94785542012-10-09 13:50:17 -07001860 return;
1861
1862 size = (sector_t) rbd_dev->header.image_size / SECTOR_SIZE;
1863 dout("setting size to %llu sectors", (unsigned long long) size);
1864 rbd_dev->mapping.size = (u64) size;
1865 set_capacity(rbd_dev->disk, size);
1866}
1867
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001868/*
1869 * only read the first part of the ondisk header, without the snaps info
1870 */
Alex Elder117973f2012-08-31 17:29:55 -05001871static int rbd_dev_v1_refresh(struct rbd_device *rbd_dev, u64 *hver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001872{
1873 int ret;
1874 struct rbd_image_header h;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001875
1876 ret = rbd_read_header(rbd_dev, &h);
1877 if (ret < 0)
1878 return ret;
1879
Josh Durgina51aa0c2011-12-05 10:35:04 -08001880 down_write(&rbd_dev->header_rwsem);
1881
Alex Elder94785542012-10-09 13:50:17 -07001882 /* Update image size, and check for resize of mapped image */
1883 rbd_dev->header.image_size = h.image_size;
1884 rbd_update_mapping_size(rbd_dev);
Sage Weil9db4b3e2011-04-19 22:49:06 -07001885
Alex Elder849b4262012-07-09 21:04:24 -05001886 /* rbd_dev->header.object_prefix shouldn't change */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001887 kfree(rbd_dev->header.snap_sizes);
Alex Elder849b4262012-07-09 21:04:24 -05001888 kfree(rbd_dev->header.snap_names);
Josh Durgind1d25642011-12-05 14:03:05 -08001889 /* osd requests may still refer to snapc */
1890 ceph_put_snap_context(rbd_dev->header.snapc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001891
Alex Elderb8136232012-07-25 09:32:41 -05001892 if (hver)
1893 *hver = h.obj_version;
Josh Durgina71b8912011-12-05 18:10:44 -08001894 rbd_dev->header.obj_version = h.obj_version;
Josh Durgin93a24e02011-12-05 10:41:28 -08001895 rbd_dev->header.image_size = h.image_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001896 rbd_dev->header.snapc = h.snapc;
1897 rbd_dev->header.snap_names = h.snap_names;
1898 rbd_dev->header.snap_sizes = h.snap_sizes;
Alex Elder849b4262012-07-09 21:04:24 -05001899 /* Free the extra copy of the object prefix */
1900 WARN_ON(strcmp(rbd_dev->header.object_prefix, h.object_prefix));
1901 kfree(h.object_prefix);
1902
Alex Elder304f6802012-08-31 17:29:52 -05001903 ret = rbd_dev_snaps_update(rbd_dev);
1904 if (!ret)
1905 ret = rbd_dev_snaps_register(rbd_dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001906
Josh Durginc6666012011-11-21 17:11:12 -08001907 up_write(&rbd_dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001908
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001909 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001910}
1911
Alex Elder117973f2012-08-31 17:29:55 -05001912static int rbd_dev_refresh(struct rbd_device *rbd_dev, u64 *hver)
Alex Elder1fe5e992012-07-25 09:32:41 -05001913{
1914 int ret;
1915
Alex Elder117973f2012-08-31 17:29:55 -05001916 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
Alex Elder1fe5e992012-07-25 09:32:41 -05001917 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Alex Elder117973f2012-08-31 17:29:55 -05001918 if (rbd_dev->image_format == 1)
1919 ret = rbd_dev_v1_refresh(rbd_dev, hver);
1920 else
1921 ret = rbd_dev_v2_refresh(rbd_dev, hver);
Alex Elder1fe5e992012-07-25 09:32:41 -05001922 mutex_unlock(&ctl_mutex);
1923
1924 return ret;
1925}
1926
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001927static int rbd_init_disk(struct rbd_device *rbd_dev)
1928{
1929 struct gendisk *disk;
1930 struct request_queue *q;
Alex Elder593a9e72012-02-07 12:03:37 -06001931 u64 segment_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001932
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001933 /* create gendisk info */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001934 disk = alloc_disk(RBD_MINORS_PER_MAJOR);
1935 if (!disk)
Alex Elder1fcdb8a2012-08-29 17:11:06 -05001936 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001937
Alex Elderf0f8cef2012-01-29 13:57:44 -06001938 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
Alex Elderde71a292012-07-03 16:01:19 -05001939 rbd_dev->dev_id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001940 disk->major = rbd_dev->major;
1941 disk->first_minor = 0;
1942 disk->fops = &rbd_bd_ops;
1943 disk->private_data = rbd_dev;
1944
1945 /* init rq */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001946 q = blk_init_queue(rbd_rq_fn, &rbd_dev->lock);
1947 if (!q)
1948 goto out_disk;
Josh Durgin029bcbd2011-07-22 11:35:23 -07001949
Alex Elder593a9e72012-02-07 12:03:37 -06001950 /* We use the default size, but let's be explicit about it. */
1951 blk_queue_physical_block_size(q, SECTOR_SIZE);
1952
Josh Durgin029bcbd2011-07-22 11:35:23 -07001953 /* set io sizes to object size */
Alex Elder593a9e72012-02-07 12:03:37 -06001954 segment_size = rbd_obj_bytes(&rbd_dev->header);
1955 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
1956 blk_queue_max_segment_size(q, segment_size);
1957 blk_queue_io_min(q, segment_size);
1958 blk_queue_io_opt(q, segment_size);
Josh Durgin029bcbd2011-07-22 11:35:23 -07001959
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001960 blk_queue_merge_bvec(q, rbd_merge_bvec);
1961 disk->queue = q;
1962
1963 q->queuedata = rbd_dev;
1964
1965 rbd_dev->disk = disk;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001966
Alex Elder12f02942012-08-29 17:11:07 -05001967 set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
1968
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001969 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001970out_disk:
1971 put_disk(disk);
Alex Elder1fcdb8a2012-08-29 17:11:06 -05001972
1973 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001974}
1975
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001976/*
1977 sysfs
1978*/
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001979
Alex Elder593a9e72012-02-07 12:03:37 -06001980static struct rbd_device *dev_to_rbd_dev(struct device *dev)
1981{
1982 return container_of(dev, struct rbd_device, dev);
1983}
1984
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001985static ssize_t rbd_size_show(struct device *dev,
1986 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001987{
Alex Elder593a9e72012-02-07 12:03:37 -06001988 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Josh Durgina51aa0c2011-12-05 10:35:04 -08001989 sector_t size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08001990
Josh Durgina51aa0c2011-12-05 10:35:04 -08001991 down_read(&rbd_dev->header_rwsem);
1992 size = get_capacity(rbd_dev->disk);
1993 up_read(&rbd_dev->header_rwsem);
1994
1995 return sprintf(buf, "%llu\n", (unsigned long long) size * SECTOR_SIZE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001996}
1997
Alex Elder34b13182012-07-13 20:35:12 -05001998/*
1999 * Note this shows the features for whatever's mapped, which is not
2000 * necessarily the base image.
2001 */
2002static ssize_t rbd_features_show(struct device *dev,
2003 struct device_attribute *attr, char *buf)
2004{
2005 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
2006
2007 return sprintf(buf, "0x%016llx\n",
2008 (unsigned long long) rbd_dev->mapping.features);
2009}
2010
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002011static ssize_t rbd_major_show(struct device *dev,
2012 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002013{
Alex Elder593a9e72012-02-07 12:03:37 -06002014 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002015
2016 return sprintf(buf, "%d\n", rbd_dev->major);
2017}
2018
2019static ssize_t rbd_client_id_show(struct device *dev,
2020 struct device_attribute *attr, char *buf)
2021{
Alex Elder593a9e72012-02-07 12:03:37 -06002022 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002023
Alex Elder1dbb4392012-01-24 10:08:37 -06002024 return sprintf(buf, "client%lld\n",
2025 ceph_client_id(rbd_dev->rbd_client->client));
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002026}
2027
2028static ssize_t rbd_pool_show(struct device *dev,
2029 struct device_attribute *attr, char *buf)
2030{
Alex Elder593a9e72012-02-07 12:03:37 -06002031 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002032
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002033 return sprintf(buf, "%s\n", rbd_dev->spec->pool_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002034}
2035
Alex Elder9bb2f332012-07-12 10:46:35 -05002036static ssize_t rbd_pool_id_show(struct device *dev,
2037 struct device_attribute *attr, char *buf)
2038{
2039 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
2040
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002041 return sprintf(buf, "%llu\n",
2042 (unsigned long long) rbd_dev->spec->pool_id);
Alex Elder9bb2f332012-07-12 10:46:35 -05002043}
2044
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002045static ssize_t rbd_name_show(struct device *dev,
2046 struct device_attribute *attr, char *buf)
2047{
Alex Elder593a9e72012-02-07 12:03:37 -06002048 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002049
Alex Eldera92ffdf2012-10-30 19:40:33 -05002050 if (rbd_dev->spec->image_name)
2051 return sprintf(buf, "%s\n", rbd_dev->spec->image_name);
2052
2053 return sprintf(buf, "(unknown)\n");
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002054}
2055
Alex Elder589d30e2012-07-10 20:30:11 -05002056static ssize_t rbd_image_id_show(struct device *dev,
2057 struct device_attribute *attr, char *buf)
2058{
2059 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
2060
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002061 return sprintf(buf, "%s\n", rbd_dev->spec->image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05002062}
2063
Alex Elder34b13182012-07-13 20:35:12 -05002064/*
2065 * Shows the name of the currently-mapped snapshot (or
2066 * RBD_SNAP_HEAD_NAME for the base image).
2067 */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002068static ssize_t rbd_snap_show(struct device *dev,
2069 struct device_attribute *attr,
2070 char *buf)
2071{
Alex Elder593a9e72012-02-07 12:03:37 -06002072 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002073
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002074 return sprintf(buf, "%s\n", rbd_dev->spec->snap_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002075}
2076
Alex Elder86b00e02012-10-25 23:34:42 -05002077/*
2078 * For an rbd v2 image, shows the pool id, image id, and snapshot id
2079 * for the parent image. If there is no parent, simply shows
2080 * "(no parent image)".
2081 */
2082static ssize_t rbd_parent_show(struct device *dev,
2083 struct device_attribute *attr,
2084 char *buf)
2085{
2086 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
2087 struct rbd_spec *spec = rbd_dev->parent_spec;
2088 int count;
2089 char *bufp = buf;
2090
2091 if (!spec)
2092 return sprintf(buf, "(no parent image)\n");
2093
2094 count = sprintf(bufp, "pool_id %llu\npool_name %s\n",
2095 (unsigned long long) spec->pool_id, spec->pool_name);
2096 if (count < 0)
2097 return count;
2098 bufp += count;
2099
2100 count = sprintf(bufp, "image_id %s\nimage_name %s\n", spec->image_id,
2101 spec->image_name ? spec->image_name : "(unknown)");
2102 if (count < 0)
2103 return count;
2104 bufp += count;
2105
2106 count = sprintf(bufp, "snap_id %llu\nsnap_name %s\n",
2107 (unsigned long long) spec->snap_id, spec->snap_name);
2108 if (count < 0)
2109 return count;
2110 bufp += count;
2111
2112 count = sprintf(bufp, "overlap %llu\n", rbd_dev->parent_overlap);
2113 if (count < 0)
2114 return count;
2115 bufp += count;
2116
2117 return (ssize_t) (bufp - buf);
2118}
2119
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002120static ssize_t rbd_image_refresh(struct device *dev,
2121 struct device_attribute *attr,
2122 const char *buf,
2123 size_t size)
2124{
Alex Elder593a9e72012-02-07 12:03:37 -06002125 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Alex Elderb8136232012-07-25 09:32:41 -05002126 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002127
Alex Elder117973f2012-08-31 17:29:55 -05002128 ret = rbd_dev_refresh(rbd_dev, NULL);
Alex Elderb8136232012-07-25 09:32:41 -05002129
2130 return ret < 0 ? ret : size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002131}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002132
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002133static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
Alex Elder34b13182012-07-13 20:35:12 -05002134static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002135static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
2136static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
2137static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
Alex Elder9bb2f332012-07-12 10:46:35 -05002138static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002139static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
Alex Elder589d30e2012-07-10 20:30:11 -05002140static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002141static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
2142static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
Alex Elder86b00e02012-10-25 23:34:42 -05002143static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002144
2145static struct attribute *rbd_attrs[] = {
2146 &dev_attr_size.attr,
Alex Elder34b13182012-07-13 20:35:12 -05002147 &dev_attr_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002148 &dev_attr_major.attr,
2149 &dev_attr_client_id.attr,
2150 &dev_attr_pool.attr,
Alex Elder9bb2f332012-07-12 10:46:35 -05002151 &dev_attr_pool_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002152 &dev_attr_name.attr,
Alex Elder589d30e2012-07-10 20:30:11 -05002153 &dev_attr_image_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002154 &dev_attr_current_snap.attr,
Alex Elder86b00e02012-10-25 23:34:42 -05002155 &dev_attr_parent.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002156 &dev_attr_refresh.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002157 NULL
2158};
2159
2160static struct attribute_group rbd_attr_group = {
2161 .attrs = rbd_attrs,
2162};
2163
2164static const struct attribute_group *rbd_attr_groups[] = {
2165 &rbd_attr_group,
2166 NULL
2167};
2168
2169static void rbd_sysfs_dev_release(struct device *dev)
2170{
2171}
2172
2173static struct device_type rbd_device_type = {
2174 .name = "rbd",
2175 .groups = rbd_attr_groups,
2176 .release = rbd_sysfs_dev_release,
2177};
2178
2179
2180/*
2181 sysfs - snapshots
2182*/
2183
2184static ssize_t rbd_snap_size_show(struct device *dev,
2185 struct device_attribute *attr,
2186 char *buf)
2187{
2188 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2189
Josh Durgin3591538f2011-12-05 18:25:13 -08002190 return sprintf(buf, "%llu\n", (unsigned long long)snap->size);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002191}
2192
2193static ssize_t rbd_snap_id_show(struct device *dev,
2194 struct device_attribute *attr,
2195 char *buf)
2196{
2197 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2198
Josh Durgin3591538f2011-12-05 18:25:13 -08002199 return sprintf(buf, "%llu\n", (unsigned long long)snap->id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002200}
2201
Alex Elder34b13182012-07-13 20:35:12 -05002202static ssize_t rbd_snap_features_show(struct device *dev,
2203 struct device_attribute *attr,
2204 char *buf)
2205{
2206 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2207
2208 return sprintf(buf, "0x%016llx\n",
2209 (unsigned long long) snap->features);
2210}
2211
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002212static DEVICE_ATTR(snap_size, S_IRUGO, rbd_snap_size_show, NULL);
2213static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL);
Alex Elder34b13182012-07-13 20:35:12 -05002214static DEVICE_ATTR(snap_features, S_IRUGO, rbd_snap_features_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002215
2216static struct attribute *rbd_snap_attrs[] = {
2217 &dev_attr_snap_size.attr,
2218 &dev_attr_snap_id.attr,
Alex Elder34b13182012-07-13 20:35:12 -05002219 &dev_attr_snap_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002220 NULL,
2221};
2222
2223static struct attribute_group rbd_snap_attr_group = {
2224 .attrs = rbd_snap_attrs,
2225};
2226
2227static void rbd_snap_dev_release(struct device *dev)
2228{
2229 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2230 kfree(snap->name);
2231 kfree(snap);
2232}
2233
2234static const struct attribute_group *rbd_snap_attr_groups[] = {
2235 &rbd_snap_attr_group,
2236 NULL
2237};
2238
2239static struct device_type rbd_snap_device_type = {
2240 .groups = rbd_snap_attr_groups,
2241 .release = rbd_snap_dev_release,
2242};
2243
Alex Elder8b8fb992012-10-26 17:25:24 -05002244static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)
2245{
2246 kref_get(&spec->kref);
2247
2248 return spec;
2249}
2250
2251static void rbd_spec_free(struct kref *kref);
2252static void rbd_spec_put(struct rbd_spec *spec)
2253{
2254 if (spec)
2255 kref_put(&spec->kref, rbd_spec_free);
2256}
2257
2258static struct rbd_spec *rbd_spec_alloc(void)
2259{
2260 struct rbd_spec *spec;
2261
2262 spec = kzalloc(sizeof (*spec), GFP_KERNEL);
2263 if (!spec)
2264 return NULL;
2265 kref_init(&spec->kref);
2266
2267 rbd_spec_put(rbd_spec_get(spec)); /* TEMPORARY */
2268
2269 return spec;
2270}
2271
2272static void rbd_spec_free(struct kref *kref)
2273{
2274 struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref);
2275
2276 kfree(spec->pool_name);
2277 kfree(spec->image_id);
2278 kfree(spec->image_name);
2279 kfree(spec->snap_name);
2280 kfree(spec);
2281}
2282
Alex Elderc53d5892012-10-25 23:34:42 -05002283struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,
2284 struct rbd_spec *spec)
2285{
2286 struct rbd_device *rbd_dev;
2287
2288 rbd_dev = kzalloc(sizeof (*rbd_dev), GFP_KERNEL);
2289 if (!rbd_dev)
2290 return NULL;
2291
2292 spin_lock_init(&rbd_dev->lock);
Alex Elderd78b6502012-11-09 08:43:15 -06002293 atomic_set(&rbd_dev->exists, 0);
Alex Elderc53d5892012-10-25 23:34:42 -05002294 INIT_LIST_HEAD(&rbd_dev->node);
2295 INIT_LIST_HEAD(&rbd_dev->snaps);
2296 init_rwsem(&rbd_dev->header_rwsem);
2297
2298 rbd_dev->spec = spec;
2299 rbd_dev->rbd_client = rbdc;
2300
Alex Elder0903e872012-11-14 12:25:19 -06002301 /* Initialize the layout used for all rbd requests */
2302
2303 rbd_dev->layout.fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
2304 rbd_dev->layout.fl_stripe_count = cpu_to_le32(1);
2305 rbd_dev->layout.fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
2306 rbd_dev->layout.fl_pg_pool = cpu_to_le32((u32) spec->pool_id);
2307
Alex Elderc53d5892012-10-25 23:34:42 -05002308 return rbd_dev;
2309}
2310
2311static void rbd_dev_destroy(struct rbd_device *rbd_dev)
2312{
Alex Elder86b00e02012-10-25 23:34:42 -05002313 rbd_spec_put(rbd_dev->parent_spec);
Alex Elderc53d5892012-10-25 23:34:42 -05002314 kfree(rbd_dev->header_name);
2315 rbd_put_client(rbd_dev->rbd_client);
2316 rbd_spec_put(rbd_dev->spec);
2317 kfree(rbd_dev);
2318}
2319
Alex Elder304f6802012-08-31 17:29:52 -05002320static bool rbd_snap_registered(struct rbd_snap *snap)
2321{
2322 bool ret = snap->dev.type == &rbd_snap_device_type;
2323 bool reg = device_is_registered(&snap->dev);
2324
2325 rbd_assert(!ret ^ reg);
2326
2327 return ret;
2328}
2329
Alex Elder41f38c22012-10-25 23:34:40 -05002330static void rbd_remove_snap_dev(struct rbd_snap *snap)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002331{
2332 list_del(&snap->node);
Alex Elder304f6802012-08-31 17:29:52 -05002333 if (device_is_registered(&snap->dev))
2334 device_unregister(&snap->dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002335}
2336
Alex Elder14e70852012-07-19 09:09:27 -05002337static int rbd_register_snap_dev(struct rbd_snap *snap,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002338 struct device *parent)
2339{
2340 struct device *dev = &snap->dev;
2341 int ret;
2342
2343 dev->type = &rbd_snap_device_type;
2344 dev->parent = parent;
2345 dev->release = rbd_snap_dev_release;
Alex Elderd4b125e2012-07-03 16:01:19 -05002346 dev_set_name(dev, "%s%s", RBD_SNAP_DEV_NAME_PREFIX, snap->name);
Alex Elder304f6802012-08-31 17:29:52 -05002347 dout("%s: registering device for snapshot %s\n", __func__, snap->name);
2348
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002349 ret = device_register(dev);
2350
2351 return ret;
2352}
2353
Alex Elder4e891e02012-07-10 20:30:10 -05002354static struct rbd_snap *__rbd_add_snap_dev(struct rbd_device *rbd_dev,
Alex Elderc8d18422012-07-10 20:30:11 -05002355 const char *snap_name,
Alex Elder34b13182012-07-13 20:35:12 -05002356 u64 snap_id, u64 snap_size,
2357 u64 snap_features)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002358{
Alex Elder4e891e02012-07-10 20:30:10 -05002359 struct rbd_snap *snap;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002360 int ret;
Alex Elder4e891e02012-07-10 20:30:10 -05002361
2362 snap = kzalloc(sizeof (*snap), GFP_KERNEL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002363 if (!snap)
Alex Elder4e891e02012-07-10 20:30:10 -05002364 return ERR_PTR(-ENOMEM);
2365
2366 ret = -ENOMEM;
Alex Elderc8d18422012-07-10 20:30:11 -05002367 snap->name = kstrdup(snap_name, GFP_KERNEL);
Alex Elder4e891e02012-07-10 20:30:10 -05002368 if (!snap->name)
2369 goto err;
2370
Alex Elderc8d18422012-07-10 20:30:11 -05002371 snap->id = snap_id;
2372 snap->size = snap_size;
Alex Elder34b13182012-07-13 20:35:12 -05002373 snap->features = snap_features;
Alex Elder4e891e02012-07-10 20:30:10 -05002374
2375 return snap;
2376
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002377err:
2378 kfree(snap->name);
2379 kfree(snap);
Alex Elder4e891e02012-07-10 20:30:10 -05002380
2381 return ERR_PTR(ret);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002382}
2383
Alex Eldercd892122012-07-03 16:01:19 -05002384static char *rbd_dev_v1_snap_info(struct rbd_device *rbd_dev, u32 which,
2385 u64 *snap_size, u64 *snap_features)
2386{
2387 char *snap_name;
2388
2389 rbd_assert(which < rbd_dev->header.snapc->num_snaps);
2390
2391 *snap_size = rbd_dev->header.snap_sizes[which];
2392 *snap_features = 0; /* No features for v1 */
2393
2394 /* Skip over names until we find the one we are looking for */
2395
2396 snap_name = rbd_dev->header.snap_names;
2397 while (which--)
2398 snap_name += strlen(snap_name) + 1;
2399
2400 return snap_name;
2401}
2402
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002403/*
Alex Elder9d475de2012-07-03 16:01:19 -05002404 * Get the size and object order for an image snapshot, or if
2405 * snap_id is CEPH_NOSNAP, gets this information for the base
2406 * image.
2407 */
2408static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
2409 u8 *order, u64 *snap_size)
2410{
2411 __le64 snapid = cpu_to_le64(snap_id);
2412 int ret;
2413 struct {
2414 u8 order;
2415 __le64 size;
2416 } __attribute__ ((packed)) size_buf = { 0 };
2417
2418 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2419 "rbd", "get_size",
2420 (char *) &snapid, sizeof (snapid),
Alex Elder07b23912012-11-09 08:43:16 -06002421 (char *) &size_buf, sizeof (size_buf), NULL);
Alex Elder9d475de2012-07-03 16:01:19 -05002422 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2423 if (ret < 0)
2424 return ret;
2425
2426 *order = size_buf.order;
2427 *snap_size = le64_to_cpu(size_buf.size);
2428
2429 dout(" snap_id 0x%016llx order = %u, snap_size = %llu\n",
2430 (unsigned long long) snap_id, (unsigned int) *order,
2431 (unsigned long long) *snap_size);
2432
2433 return 0;
2434}
2435
2436static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
2437{
2438 return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
2439 &rbd_dev->header.obj_order,
2440 &rbd_dev->header.image_size);
2441}
2442
Alex Elder1e130192012-07-03 16:01:19 -05002443static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
2444{
2445 void *reply_buf;
2446 int ret;
2447 void *p;
2448
2449 reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL);
2450 if (!reply_buf)
2451 return -ENOMEM;
2452
2453 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2454 "rbd", "get_object_prefix",
2455 NULL, 0,
Alex Elder07b23912012-11-09 08:43:16 -06002456 reply_buf, RBD_OBJ_PREFIX_LEN_MAX, NULL);
Alex Elder1e130192012-07-03 16:01:19 -05002457 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2458 if (ret < 0)
2459 goto out;
Alex Eldera0ea3a42012-10-10 21:19:13 -07002460 ret = 0; /* rbd_req_sync_exec() can return positive */
Alex Elder1e130192012-07-03 16:01:19 -05002461
2462 p = reply_buf;
2463 rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p,
2464 p + RBD_OBJ_PREFIX_LEN_MAX,
2465 NULL, GFP_NOIO);
2466
2467 if (IS_ERR(rbd_dev->header.object_prefix)) {
2468 ret = PTR_ERR(rbd_dev->header.object_prefix);
2469 rbd_dev->header.object_prefix = NULL;
2470 } else {
2471 dout(" object_prefix = %s\n", rbd_dev->header.object_prefix);
2472 }
2473
2474out:
2475 kfree(reply_buf);
2476
2477 return ret;
2478}
2479
Alex Elderb1b54022012-07-03 16:01:19 -05002480static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
2481 u64 *snap_features)
2482{
2483 __le64 snapid = cpu_to_le64(snap_id);
2484 struct {
2485 __le64 features;
2486 __le64 incompat;
2487 } features_buf = { 0 };
Alex Elderd8891402012-10-09 13:50:17 -07002488 u64 incompat;
Alex Elderb1b54022012-07-03 16:01:19 -05002489 int ret;
2490
2491 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2492 "rbd", "get_features",
2493 (char *) &snapid, sizeof (snapid),
2494 (char *) &features_buf, sizeof (features_buf),
Alex Elder07b23912012-11-09 08:43:16 -06002495 NULL);
Alex Elderb1b54022012-07-03 16:01:19 -05002496 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2497 if (ret < 0)
2498 return ret;
Alex Elderd8891402012-10-09 13:50:17 -07002499
2500 incompat = le64_to_cpu(features_buf.incompat);
2501 if (incompat & ~RBD_FEATURES_ALL)
Alex Elderb8f5c6e2012-11-01 08:39:26 -05002502 return -ENXIO;
Alex Elderd8891402012-10-09 13:50:17 -07002503
Alex Elderb1b54022012-07-03 16:01:19 -05002504 *snap_features = le64_to_cpu(features_buf.features);
2505
2506 dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
2507 (unsigned long long) snap_id,
2508 (unsigned long long) *snap_features,
2509 (unsigned long long) le64_to_cpu(features_buf.incompat));
2510
2511 return 0;
2512}
2513
2514static int rbd_dev_v2_features(struct rbd_device *rbd_dev)
2515{
2516 return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP,
2517 &rbd_dev->header.features);
2518}
2519
Alex Elder86b00e02012-10-25 23:34:42 -05002520static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev)
2521{
2522 struct rbd_spec *parent_spec;
2523 size_t size;
2524 void *reply_buf = NULL;
2525 __le64 snapid;
2526 void *p;
2527 void *end;
2528 char *image_id;
2529 u64 overlap;
Alex Elder86b00e02012-10-25 23:34:42 -05002530 int ret;
2531
2532 parent_spec = rbd_spec_alloc();
2533 if (!parent_spec)
2534 return -ENOMEM;
2535
2536 size = sizeof (__le64) + /* pool_id */
2537 sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */
2538 sizeof (__le64) + /* snap_id */
2539 sizeof (__le64); /* overlap */
2540 reply_buf = kmalloc(size, GFP_KERNEL);
2541 if (!reply_buf) {
2542 ret = -ENOMEM;
2543 goto out_err;
2544 }
2545
2546 snapid = cpu_to_le64(CEPH_NOSNAP);
2547 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2548 "rbd", "get_parent",
2549 (char *) &snapid, sizeof (snapid),
Alex Elder07b23912012-11-09 08:43:16 -06002550 (char *) reply_buf, size, NULL);
Alex Elder86b00e02012-10-25 23:34:42 -05002551 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2552 if (ret < 0)
2553 goto out_err;
2554
2555 ret = -ERANGE;
2556 p = reply_buf;
2557 end = (char *) reply_buf + size;
2558 ceph_decode_64_safe(&p, end, parent_spec->pool_id, out_err);
2559 if (parent_spec->pool_id == CEPH_NOPOOL)
2560 goto out; /* No parent? No problem. */
2561
Alex Elder0903e872012-11-14 12:25:19 -06002562 /* The ceph file layout needs to fit pool id in 32 bits */
2563
2564 ret = -EIO;
2565 if (WARN_ON(parent_spec->pool_id > (u64) U32_MAX))
2566 goto out;
2567
Alex Elder979ed482012-11-01 08:39:26 -05002568 image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elder86b00e02012-10-25 23:34:42 -05002569 if (IS_ERR(image_id)) {
2570 ret = PTR_ERR(image_id);
2571 goto out_err;
2572 }
2573 parent_spec->image_id = image_id;
2574 ceph_decode_64_safe(&p, end, parent_spec->snap_id, out_err);
2575 ceph_decode_64_safe(&p, end, overlap, out_err);
2576
2577 rbd_dev->parent_overlap = overlap;
2578 rbd_dev->parent_spec = parent_spec;
2579 parent_spec = NULL; /* rbd_dev now owns this */
2580out:
2581 ret = 0;
2582out_err:
2583 kfree(reply_buf);
2584 rbd_spec_put(parent_spec);
2585
2586 return ret;
2587}
2588
Alex Elder9e15b772012-10-30 19:40:33 -05002589static char *rbd_dev_image_name(struct rbd_device *rbd_dev)
2590{
2591 size_t image_id_size;
2592 char *image_id;
2593 void *p;
2594 void *end;
2595 size_t size;
2596 void *reply_buf = NULL;
2597 size_t len = 0;
2598 char *image_name = NULL;
2599 int ret;
2600
2601 rbd_assert(!rbd_dev->spec->image_name);
2602
Alex Elder69e7a022012-11-01 08:39:26 -05002603 len = strlen(rbd_dev->spec->image_id);
2604 image_id_size = sizeof (__le32) + len;
Alex Elder9e15b772012-10-30 19:40:33 -05002605 image_id = kmalloc(image_id_size, GFP_KERNEL);
2606 if (!image_id)
2607 return NULL;
2608
2609 p = image_id;
2610 end = (char *) image_id + image_id_size;
Alex Elder69e7a022012-11-01 08:39:26 -05002611 ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32) len);
Alex Elder9e15b772012-10-30 19:40:33 -05002612
2613 size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX;
2614 reply_buf = kmalloc(size, GFP_KERNEL);
2615 if (!reply_buf)
2616 goto out;
2617
2618 ret = rbd_req_sync_exec(rbd_dev, RBD_DIRECTORY,
2619 "rbd", "dir_get_name",
2620 image_id, image_id_size,
Alex Elder07b23912012-11-09 08:43:16 -06002621 (char *) reply_buf, size, NULL);
Alex Elder9e15b772012-10-30 19:40:33 -05002622 if (ret < 0)
2623 goto out;
2624 p = reply_buf;
2625 end = (char *) reply_buf + size;
2626 image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL);
2627 if (IS_ERR(image_name))
2628 image_name = NULL;
2629 else
2630 dout("%s: name is %s len is %zd\n", __func__, image_name, len);
2631out:
2632 kfree(reply_buf);
2633 kfree(image_id);
2634
2635 return image_name;
2636}
2637
2638/*
2639 * When a parent image gets probed, we only have the pool, image,
2640 * and snapshot ids but not the names of any of them. This call
2641 * is made later to fill in those names. It has to be done after
2642 * rbd_dev_snaps_update() has completed because some of the
2643 * information (in particular, snapshot name) is not available
2644 * until then.
2645 */
2646static int rbd_dev_probe_update_spec(struct rbd_device *rbd_dev)
2647{
2648 struct ceph_osd_client *osdc;
2649 const char *name;
2650 void *reply_buf = NULL;
2651 int ret;
2652
2653 if (rbd_dev->spec->pool_name)
2654 return 0; /* Already have the names */
2655
2656 /* Look up the pool name */
2657
2658 osdc = &rbd_dev->rbd_client->client->osdc;
2659 name = ceph_pg_pool_name_by_id(osdc->osdmap, rbd_dev->spec->pool_id);
Alex Elder935dc892012-11-01 10:17:15 -05002660 if (!name) {
2661 rbd_warn(rbd_dev, "there is no pool with id %llu",
2662 rbd_dev->spec->pool_id); /* Really a BUG() */
2663 return -EIO;
2664 }
Alex Elder9e15b772012-10-30 19:40:33 -05002665
2666 rbd_dev->spec->pool_name = kstrdup(name, GFP_KERNEL);
2667 if (!rbd_dev->spec->pool_name)
2668 return -ENOMEM;
2669
2670 /* Fetch the image name; tolerate failure here */
2671
2672 name = rbd_dev_image_name(rbd_dev);
Alex Elder69e7a022012-11-01 08:39:26 -05002673 if (name)
Alex Elder9e15b772012-10-30 19:40:33 -05002674 rbd_dev->spec->image_name = (char *) name;
Alex Elder69e7a022012-11-01 08:39:26 -05002675 else
Alex Elder06ecc6c2012-11-01 10:17:15 -05002676 rbd_warn(rbd_dev, "unable to get image name");
Alex Elder9e15b772012-10-30 19:40:33 -05002677
2678 /* Look up the snapshot name. */
2679
2680 name = rbd_snap_name(rbd_dev, rbd_dev->spec->snap_id);
2681 if (!name) {
Alex Elder935dc892012-11-01 10:17:15 -05002682 rbd_warn(rbd_dev, "no snapshot with id %llu",
2683 rbd_dev->spec->snap_id); /* Really a BUG() */
Alex Elder9e15b772012-10-30 19:40:33 -05002684 ret = -EIO;
2685 goto out_err;
2686 }
2687 rbd_dev->spec->snap_name = kstrdup(name, GFP_KERNEL);
2688 if(!rbd_dev->spec->snap_name)
2689 goto out_err;
2690
2691 return 0;
2692out_err:
2693 kfree(reply_buf);
2694 kfree(rbd_dev->spec->pool_name);
2695 rbd_dev->spec->pool_name = NULL;
2696
2697 return ret;
2698}
2699
Alex Elder6e14b1a2012-07-03 16:01:19 -05002700static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev, u64 *ver)
Alex Elder35d489f2012-07-03 16:01:19 -05002701{
2702 size_t size;
2703 int ret;
2704 void *reply_buf;
2705 void *p;
2706 void *end;
2707 u64 seq;
2708 u32 snap_count;
2709 struct ceph_snap_context *snapc;
2710 u32 i;
2711
2712 /*
2713 * We'll need room for the seq value (maximum snapshot id),
2714 * snapshot count, and array of that many snapshot ids.
2715 * For now we have a fixed upper limit on the number we're
2716 * prepared to receive.
2717 */
2718 size = sizeof (__le64) + sizeof (__le32) +
2719 RBD_MAX_SNAP_COUNT * sizeof (__le64);
2720 reply_buf = kzalloc(size, GFP_KERNEL);
2721 if (!reply_buf)
2722 return -ENOMEM;
2723
2724 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2725 "rbd", "get_snapcontext",
2726 NULL, 0,
Alex Elder07b23912012-11-09 08:43:16 -06002727 reply_buf, size, ver);
Alex Elder35d489f2012-07-03 16:01:19 -05002728 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2729 if (ret < 0)
2730 goto out;
2731
2732 ret = -ERANGE;
2733 p = reply_buf;
2734 end = (char *) reply_buf + size;
2735 ceph_decode_64_safe(&p, end, seq, out);
2736 ceph_decode_32_safe(&p, end, snap_count, out);
2737
2738 /*
2739 * Make sure the reported number of snapshot ids wouldn't go
2740 * beyond the end of our buffer. But before checking that,
2741 * make sure the computed size of the snapshot context we
2742 * allocate is representable in a size_t.
2743 */
2744 if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
2745 / sizeof (u64)) {
2746 ret = -EINVAL;
2747 goto out;
2748 }
2749 if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
2750 goto out;
2751
2752 size = sizeof (struct ceph_snap_context) +
2753 snap_count * sizeof (snapc->snaps[0]);
2754 snapc = kmalloc(size, GFP_KERNEL);
2755 if (!snapc) {
2756 ret = -ENOMEM;
2757 goto out;
2758 }
2759
2760 atomic_set(&snapc->nref, 1);
2761 snapc->seq = seq;
2762 snapc->num_snaps = snap_count;
2763 for (i = 0; i < snap_count; i++)
2764 snapc->snaps[i] = ceph_decode_64(&p);
2765
2766 rbd_dev->header.snapc = snapc;
2767
2768 dout(" snap context seq = %llu, snap_count = %u\n",
2769 (unsigned long long) seq, (unsigned int) snap_count);
2770
2771out:
2772 kfree(reply_buf);
2773
2774 return 0;
2775}
2776
Alex Elderb8b1e2d2012-07-03 16:01:19 -05002777static char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, u32 which)
2778{
2779 size_t size;
2780 void *reply_buf;
2781 __le64 snap_id;
2782 int ret;
2783 void *p;
2784 void *end;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05002785 char *snap_name;
2786
2787 size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN;
2788 reply_buf = kmalloc(size, GFP_KERNEL);
2789 if (!reply_buf)
2790 return ERR_PTR(-ENOMEM);
2791
2792 snap_id = cpu_to_le64(rbd_dev->header.snapc->snaps[which]);
2793 ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
2794 "rbd", "get_snapshot_name",
2795 (char *) &snap_id, sizeof (snap_id),
Alex Elder07b23912012-11-09 08:43:16 -06002796 reply_buf, size, NULL);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05002797 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
2798 if (ret < 0)
2799 goto out;
2800
2801 p = reply_buf;
2802 end = (char *) reply_buf + size;
Alex Eldere5c35532012-10-25 23:34:41 -05002803 snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05002804 if (IS_ERR(snap_name)) {
2805 ret = PTR_ERR(snap_name);
2806 goto out;
2807 } else {
2808 dout(" snap_id 0x%016llx snap_name = %s\n",
2809 (unsigned long long) le64_to_cpu(snap_id), snap_name);
2810 }
2811 kfree(reply_buf);
2812
2813 return snap_name;
2814out:
2815 kfree(reply_buf);
2816
2817 return ERR_PTR(ret);
2818}
2819
2820static char *rbd_dev_v2_snap_info(struct rbd_device *rbd_dev, u32 which,
2821 u64 *snap_size, u64 *snap_features)
2822{
2823 __le64 snap_id;
2824 u8 order;
2825 int ret;
2826
2827 snap_id = rbd_dev->header.snapc->snaps[which];
2828 ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, &order, snap_size);
2829 if (ret)
2830 return ERR_PTR(ret);
2831 ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, snap_features);
2832 if (ret)
2833 return ERR_PTR(ret);
2834
2835 return rbd_dev_v2_snap_name(rbd_dev, which);
2836}
2837
2838static char *rbd_dev_snap_info(struct rbd_device *rbd_dev, u32 which,
2839 u64 *snap_size, u64 *snap_features)
2840{
2841 if (rbd_dev->image_format == 1)
2842 return rbd_dev_v1_snap_info(rbd_dev, which,
2843 snap_size, snap_features);
2844 if (rbd_dev->image_format == 2)
2845 return rbd_dev_v2_snap_info(rbd_dev, which,
2846 snap_size, snap_features);
2847 return ERR_PTR(-EINVAL);
2848}
2849
Alex Elder117973f2012-08-31 17:29:55 -05002850static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev, u64 *hver)
2851{
2852 int ret;
2853 __u8 obj_order;
2854
2855 down_write(&rbd_dev->header_rwsem);
2856
2857 /* Grab old order first, to see if it changes */
2858
2859 obj_order = rbd_dev->header.obj_order,
2860 ret = rbd_dev_v2_image_size(rbd_dev);
2861 if (ret)
2862 goto out;
2863 if (rbd_dev->header.obj_order != obj_order) {
2864 ret = -EIO;
2865 goto out;
2866 }
2867 rbd_update_mapping_size(rbd_dev);
2868
2869 ret = rbd_dev_v2_snap_context(rbd_dev, hver);
2870 dout("rbd_dev_v2_snap_context returned %d\n", ret);
2871 if (ret)
2872 goto out;
2873 ret = rbd_dev_snaps_update(rbd_dev);
2874 dout("rbd_dev_snaps_update returned %d\n", ret);
2875 if (ret)
2876 goto out;
2877 ret = rbd_dev_snaps_register(rbd_dev);
2878 dout("rbd_dev_snaps_register returned %d\n", ret);
2879out:
2880 up_write(&rbd_dev->header_rwsem);
2881
2882 return ret;
2883}
2884
Alex Elder9d475de2012-07-03 16:01:19 -05002885/*
Alex Elder35938152012-08-02 11:29:46 -05002886 * Scan the rbd device's current snapshot list and compare it to the
2887 * newly-received snapshot context. Remove any existing snapshots
2888 * not present in the new snapshot context. Add a new snapshot for
2889 * any snaphots in the snapshot context not in the current list.
2890 * And verify there are no changes to snapshots we already know
2891 * about.
2892 *
2893 * Assumes the snapshots in the snapshot context are sorted by
2894 * snapshot id, highest id first. (Snapshots in the rbd_dev's list
2895 * are also maintained in that order.)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002896 */
Alex Elder304f6802012-08-31 17:29:52 -05002897static int rbd_dev_snaps_update(struct rbd_device *rbd_dev)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002898{
Alex Elder35938152012-08-02 11:29:46 -05002899 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
2900 const u32 snap_count = snapc->num_snaps;
Alex Elder35938152012-08-02 11:29:46 -05002901 struct list_head *head = &rbd_dev->snaps;
2902 struct list_head *links = head->next;
2903 u32 index = 0;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002904
Alex Elder9fcbb802012-08-23 23:48:49 -05002905 dout("%s: snap count is %u\n", __func__, (unsigned int) snap_count);
Alex Elder35938152012-08-02 11:29:46 -05002906 while (index < snap_count || links != head) {
2907 u64 snap_id;
2908 struct rbd_snap *snap;
Alex Eldercd892122012-07-03 16:01:19 -05002909 char *snap_name;
2910 u64 snap_size = 0;
2911 u64 snap_features = 0;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002912
Alex Elder35938152012-08-02 11:29:46 -05002913 snap_id = index < snap_count ? snapc->snaps[index]
2914 : CEPH_NOSNAP;
2915 snap = links != head ? list_entry(links, struct rbd_snap, node)
2916 : NULL;
Alex Elderaafb2302012-09-06 16:00:54 -05002917 rbd_assert(!snap || snap->id != CEPH_NOSNAP);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002918
Alex Elder35938152012-08-02 11:29:46 -05002919 if (snap_id == CEPH_NOSNAP || (snap && snap->id > snap_id)) {
2920 struct list_head *next = links->next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002921
Alex Elder35938152012-08-02 11:29:46 -05002922 /* Existing snapshot not in the new snap context */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002923
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002924 if (rbd_dev->spec->snap_id == snap->id)
Alex Elderd78b6502012-11-09 08:43:15 -06002925 atomic_set(&rbd_dev->exists, 0);
Alex Elder41f38c22012-10-25 23:34:40 -05002926 rbd_remove_snap_dev(snap);
Alex Elder9fcbb802012-08-23 23:48:49 -05002927 dout("%ssnap id %llu has been removed\n",
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002928 rbd_dev->spec->snap_id == snap->id ?
2929 "mapped " : "",
Alex Elder9fcbb802012-08-23 23:48:49 -05002930 (unsigned long long) snap->id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002931
Alex Elder35938152012-08-02 11:29:46 -05002932 /* Done with this list entry; advance */
2933
2934 links = next;
2935 continue;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002936 }
Alex Elder35938152012-08-02 11:29:46 -05002937
Alex Elderb8b1e2d2012-07-03 16:01:19 -05002938 snap_name = rbd_dev_snap_info(rbd_dev, index,
2939 &snap_size, &snap_features);
Alex Eldercd892122012-07-03 16:01:19 -05002940 if (IS_ERR(snap_name))
2941 return PTR_ERR(snap_name);
2942
Alex Elder9fcbb802012-08-23 23:48:49 -05002943 dout("entry %u: snap_id = %llu\n", (unsigned int) snap_count,
2944 (unsigned long long) snap_id);
Alex Elder35938152012-08-02 11:29:46 -05002945 if (!snap || (snap_id != CEPH_NOSNAP && snap->id < snap_id)) {
2946 struct rbd_snap *new_snap;
2947
2948 /* We haven't seen this snapshot before */
2949
Alex Elderc8d18422012-07-10 20:30:11 -05002950 new_snap = __rbd_add_snap_dev(rbd_dev, snap_name,
Alex Eldercd892122012-07-03 16:01:19 -05002951 snap_id, snap_size, snap_features);
Alex Elder9fcbb802012-08-23 23:48:49 -05002952 if (IS_ERR(new_snap)) {
2953 int err = PTR_ERR(new_snap);
2954
2955 dout(" failed to add dev, error %d\n", err);
2956
2957 return err;
2958 }
Alex Elder35938152012-08-02 11:29:46 -05002959
2960 /* New goes before existing, or at end of list */
2961
Alex Elder9fcbb802012-08-23 23:48:49 -05002962 dout(" added dev%s\n", snap ? "" : " at end\n");
Alex Elder35938152012-08-02 11:29:46 -05002963 if (snap)
2964 list_add_tail(&new_snap->node, &snap->node);
2965 else
Alex Elder523f3252012-08-30 00:16:37 -05002966 list_add_tail(&new_snap->node, head);
Alex Elder35938152012-08-02 11:29:46 -05002967 } else {
2968 /* Already have this one */
2969
Alex Elder9fcbb802012-08-23 23:48:49 -05002970 dout(" already present\n");
2971
Alex Eldercd892122012-07-03 16:01:19 -05002972 rbd_assert(snap->size == snap_size);
Alex Elderaafb2302012-09-06 16:00:54 -05002973 rbd_assert(!strcmp(snap->name, snap_name));
Alex Eldercd892122012-07-03 16:01:19 -05002974 rbd_assert(snap->features == snap_features);
Alex Elder35938152012-08-02 11:29:46 -05002975
2976 /* Done with this list entry; advance */
2977
2978 links = links->next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002979 }
Alex Elder35938152012-08-02 11:29:46 -05002980
2981 /* Advance to the next entry in the snapshot context */
2982
2983 index++;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002984 }
Alex Elder9fcbb802012-08-23 23:48:49 -05002985 dout("%s: done\n", __func__);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002986
2987 return 0;
2988}
2989
Alex Elder304f6802012-08-31 17:29:52 -05002990/*
2991 * Scan the list of snapshots and register the devices for any that
2992 * have not already been registered.
2993 */
2994static int rbd_dev_snaps_register(struct rbd_device *rbd_dev)
2995{
2996 struct rbd_snap *snap;
2997 int ret = 0;
2998
2999 dout("%s called\n", __func__);
Alex Elder86ff77b2012-08-31 17:29:53 -05003000 if (WARN_ON(!device_is_registered(&rbd_dev->dev)))
3001 return -EIO;
Alex Elder304f6802012-08-31 17:29:52 -05003002
3003 list_for_each_entry(snap, &rbd_dev->snaps, node) {
3004 if (!rbd_snap_registered(snap)) {
3005 ret = rbd_register_snap_dev(snap, &rbd_dev->dev);
3006 if (ret < 0)
3007 break;
3008 }
3009 }
3010 dout("%s: returning %d\n", __func__, ret);
3011
3012 return ret;
3013}
3014
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003015static int rbd_bus_add_dev(struct rbd_device *rbd_dev)
3016{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003017 struct device *dev;
Alex Eldercd789ab2012-08-30 00:16:38 -05003018 int ret;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003019
3020 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003021
Alex Eldercd789ab2012-08-30 00:16:38 -05003022 dev = &rbd_dev->dev;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003023 dev->bus = &rbd_bus_type;
3024 dev->type = &rbd_device_type;
3025 dev->parent = &rbd_root_dev;
3026 dev->release = rbd_dev_release;
Alex Elderde71a292012-07-03 16:01:19 -05003027 dev_set_name(dev, "%d", rbd_dev->dev_id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003028 ret = device_register(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003029
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003030 mutex_unlock(&ctl_mutex);
Alex Eldercd789ab2012-08-30 00:16:38 -05003031
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003032 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003033}
3034
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003035static void rbd_bus_del_dev(struct rbd_device *rbd_dev)
3036{
3037 device_unregister(&rbd_dev->dev);
3038}
3039
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003040static int rbd_init_watch_dev(struct rbd_device *rbd_dev)
3041{
3042 int ret, rc;
3043
3044 do {
Alex Elder907703d2012-11-13 21:11:15 -06003045 ret = rbd_req_sync_watch(rbd_dev, 1);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003046 if (ret == -ERANGE) {
Alex Elder117973f2012-08-31 17:29:55 -05003047 rc = rbd_dev_refresh(rbd_dev, NULL);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003048 if (rc < 0)
3049 return rc;
3050 }
3051 } while (ret == -ERANGE);
3052
3053 return ret;
3054}
3055
Alex Eldere2839302012-08-29 17:11:06 -05003056static atomic64_t rbd_dev_id_max = ATOMIC64_INIT(0);
Alex Elder1ddbe942012-01-29 13:57:44 -06003057
3058/*
Alex Elder499afd52012-02-02 08:13:29 -06003059 * Get a unique rbd identifier for the given new rbd_dev, and add
3060 * the rbd_dev to the global list. The minimum rbd id is 1.
Alex Elder1ddbe942012-01-29 13:57:44 -06003061 */
Alex Eldere2839302012-08-29 17:11:06 -05003062static void rbd_dev_id_get(struct rbd_device *rbd_dev)
Alex Elderb7f23c32012-01-29 13:57:43 -06003063{
Alex Eldere2839302012-08-29 17:11:06 -05003064 rbd_dev->dev_id = atomic64_inc_return(&rbd_dev_id_max);
Alex Elder499afd52012-02-02 08:13:29 -06003065
3066 spin_lock(&rbd_dev_list_lock);
3067 list_add_tail(&rbd_dev->node, &rbd_dev_list);
3068 spin_unlock(&rbd_dev_list_lock);
Alex Eldere2839302012-08-29 17:11:06 -05003069 dout("rbd_dev %p given dev id %llu\n", rbd_dev,
3070 (unsigned long long) rbd_dev->dev_id);
Alex Elder1ddbe942012-01-29 13:57:44 -06003071}
Alex Elderb7f23c32012-01-29 13:57:43 -06003072
Alex Elder1ddbe942012-01-29 13:57:44 -06003073/*
Alex Elder499afd52012-02-02 08:13:29 -06003074 * Remove an rbd_dev from the global list, and record that its
3075 * identifier is no longer in use.
Alex Elder1ddbe942012-01-29 13:57:44 -06003076 */
Alex Eldere2839302012-08-29 17:11:06 -05003077static void rbd_dev_id_put(struct rbd_device *rbd_dev)
Alex Elder1ddbe942012-01-29 13:57:44 -06003078{
Alex Elderd184f6b2012-01-29 13:57:44 -06003079 struct list_head *tmp;
Alex Elderde71a292012-07-03 16:01:19 -05003080 int rbd_id = rbd_dev->dev_id;
Alex Elderd184f6b2012-01-29 13:57:44 -06003081 int max_id;
3082
Alex Elderaafb2302012-09-06 16:00:54 -05003083 rbd_assert(rbd_id > 0);
Alex Elder499afd52012-02-02 08:13:29 -06003084
Alex Eldere2839302012-08-29 17:11:06 -05003085 dout("rbd_dev %p released dev id %llu\n", rbd_dev,
3086 (unsigned long long) rbd_dev->dev_id);
Alex Elder499afd52012-02-02 08:13:29 -06003087 spin_lock(&rbd_dev_list_lock);
3088 list_del_init(&rbd_dev->node);
Alex Elderd184f6b2012-01-29 13:57:44 -06003089
3090 /*
3091 * If the id being "put" is not the current maximum, there
3092 * is nothing special we need to do.
3093 */
Alex Eldere2839302012-08-29 17:11:06 -05003094 if (rbd_id != atomic64_read(&rbd_dev_id_max)) {
Alex Elderd184f6b2012-01-29 13:57:44 -06003095 spin_unlock(&rbd_dev_list_lock);
3096 return;
3097 }
3098
3099 /*
3100 * We need to update the current maximum id. Search the
3101 * list to find out what it is. We're more likely to find
3102 * the maximum at the end, so search the list backward.
3103 */
3104 max_id = 0;
3105 list_for_each_prev(tmp, &rbd_dev_list) {
3106 struct rbd_device *rbd_dev;
3107
3108 rbd_dev = list_entry(tmp, struct rbd_device, node);
Alex Elderb213e0b2012-10-10 21:19:13 -07003109 if (rbd_dev->dev_id > max_id)
3110 max_id = rbd_dev->dev_id;
Alex Elderd184f6b2012-01-29 13:57:44 -06003111 }
Alex Elder499afd52012-02-02 08:13:29 -06003112 spin_unlock(&rbd_dev_list_lock);
Alex Elderb7f23c32012-01-29 13:57:43 -06003113
Alex Elder1ddbe942012-01-29 13:57:44 -06003114 /*
Alex Eldere2839302012-08-29 17:11:06 -05003115 * The max id could have been updated by rbd_dev_id_get(), in
Alex Elderd184f6b2012-01-29 13:57:44 -06003116 * which case it now accurately reflects the new maximum.
3117 * Be careful not to overwrite the maximum value in that
3118 * case.
Alex Elder1ddbe942012-01-29 13:57:44 -06003119 */
Alex Eldere2839302012-08-29 17:11:06 -05003120 atomic64_cmpxchg(&rbd_dev_id_max, rbd_id, max_id);
3121 dout(" max dev id has been reset\n");
Alex Elderb7f23c32012-01-29 13:57:43 -06003122}
3123
Alex Eldera725f65e2012-02-02 08:13:30 -06003124/*
Alex Eldere28fff262012-02-02 08:13:30 -06003125 * Skips over white space at *buf, and updates *buf to point to the
3126 * first found non-space character (if any). Returns the length of
Alex Elder593a9e72012-02-07 12:03:37 -06003127 * the token (string of non-white space characters) found. Note
3128 * that *buf must be terminated with '\0'.
Alex Eldere28fff262012-02-02 08:13:30 -06003129 */
3130static inline size_t next_token(const char **buf)
3131{
3132 /*
3133 * These are the characters that produce nonzero for
3134 * isspace() in the "C" and "POSIX" locales.
3135 */
3136 const char *spaces = " \f\n\r\t\v";
3137
3138 *buf += strspn(*buf, spaces); /* Find start of token */
3139
3140 return strcspn(*buf, spaces); /* Return token length */
3141}
3142
3143/*
3144 * Finds the next token in *buf, and if the provided token buffer is
3145 * big enough, copies the found token into it. The result, if
Alex Elder593a9e72012-02-07 12:03:37 -06003146 * copied, is guaranteed to be terminated with '\0'. Note that *buf
3147 * must be terminated with '\0' on entry.
Alex Eldere28fff262012-02-02 08:13:30 -06003148 *
3149 * Returns the length of the token found (not including the '\0').
3150 * Return value will be 0 if no token is found, and it will be >=
3151 * token_size if the token would not fit.
3152 *
Alex Elder593a9e72012-02-07 12:03:37 -06003153 * The *buf pointer will be updated to point beyond the end of the
Alex Eldere28fff262012-02-02 08:13:30 -06003154 * found token. Note that this occurs even if the token buffer is
3155 * too small to hold it.
3156 */
3157static inline size_t copy_token(const char **buf,
3158 char *token,
3159 size_t token_size)
3160{
3161 size_t len;
3162
3163 len = next_token(buf);
3164 if (len < token_size) {
3165 memcpy(token, *buf, len);
3166 *(token + len) = '\0';
3167 }
3168 *buf += len;
3169
3170 return len;
3171}
3172
3173/*
Alex Elderea3352f2012-07-09 21:04:23 -05003174 * Finds the next token in *buf, dynamically allocates a buffer big
3175 * enough to hold a copy of it, and copies the token into the new
3176 * buffer. The copy is guaranteed to be terminated with '\0'. Note
3177 * that a duplicate buffer is created even for a zero-length token.
3178 *
3179 * Returns a pointer to the newly-allocated duplicate, or a null
3180 * pointer if memory for the duplicate was not available. If
3181 * the lenp argument is a non-null pointer, the length of the token
3182 * (not including the '\0') is returned in *lenp.
3183 *
3184 * If successful, the *buf pointer will be updated to point beyond
3185 * the end of the found token.
3186 *
3187 * Note: uses GFP_KERNEL for allocation.
3188 */
3189static inline char *dup_token(const char **buf, size_t *lenp)
3190{
3191 char *dup;
3192 size_t len;
3193
3194 len = next_token(buf);
Alex Elder4caf35f2012-11-01 08:39:27 -05003195 dup = kmemdup(*buf, len + 1, GFP_KERNEL);
Alex Elderea3352f2012-07-09 21:04:23 -05003196 if (!dup)
3197 return NULL;
Alex Elderea3352f2012-07-09 21:04:23 -05003198 *(dup + len) = '\0';
3199 *buf += len;
3200
3201 if (lenp)
3202 *lenp = len;
3203
3204 return dup;
3205}
3206
3207/*
Alex Elder859c31d2012-10-25 23:34:42 -05003208 * Parse the options provided for an "rbd add" (i.e., rbd image
3209 * mapping) request. These arrive via a write to /sys/bus/rbd/add,
3210 * and the data written is passed here via a NUL-terminated buffer.
3211 * Returns 0 if successful or an error code otherwise.
Alex Elderd22f76e2012-07-12 10:46:35 -05003212 *
Alex Elder859c31d2012-10-25 23:34:42 -05003213 * The information extracted from these options is recorded in
3214 * the other parameters which return dynamically-allocated
3215 * structures:
3216 * ceph_opts
3217 * The address of a pointer that will refer to a ceph options
3218 * structure. Caller must release the returned pointer using
3219 * ceph_destroy_options() when it is no longer needed.
3220 * rbd_opts
3221 * Address of an rbd options pointer. Fully initialized by
3222 * this function; caller must release with kfree().
3223 * spec
3224 * Address of an rbd image specification pointer. Fully
3225 * initialized by this function based on parsed options.
3226 * Caller must release with rbd_spec_put().
3227 *
3228 * The options passed take this form:
3229 * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
3230 * where:
3231 * <mon_addrs>
3232 * A comma-separated list of one or more monitor addresses.
3233 * A monitor address is an ip address, optionally followed
3234 * by a port number (separated by a colon).
3235 * I.e.: ip1[:port1][,ip2[:port2]...]
3236 * <options>
3237 * A comma-separated list of ceph and/or rbd options.
3238 * <pool_name>
3239 * The name of the rados pool containing the rbd image.
3240 * <image_name>
3241 * The name of the image in that pool to map.
3242 * <snap_id>
3243 * An optional snapshot id. If provided, the mapping will
3244 * present data from the image at the time that snapshot was
3245 * created. The image head is used if no snapshot id is
3246 * provided. Snapshot mappings are always read-only.
Alex Eldera725f65e2012-02-02 08:13:30 -06003247 */
Alex Elder859c31d2012-10-25 23:34:42 -05003248static int rbd_add_parse_args(const char *buf,
Alex Elderdc79b112012-10-25 23:34:41 -05003249 struct ceph_options **ceph_opts,
Alex Elder859c31d2012-10-25 23:34:42 -05003250 struct rbd_options **opts,
3251 struct rbd_spec **rbd_spec)
Alex Eldera725f65e2012-02-02 08:13:30 -06003252{
Alex Elderd22f76e2012-07-12 10:46:35 -05003253 size_t len;
Alex Elder859c31d2012-10-25 23:34:42 -05003254 char *options;
Alex Elder0ddebc02012-10-25 23:34:41 -05003255 const char *mon_addrs;
3256 size_t mon_addrs_size;
Alex Elder859c31d2012-10-25 23:34:42 -05003257 struct rbd_spec *spec = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05003258 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05003259 struct ceph_options *copts;
Alex Elderdc79b112012-10-25 23:34:41 -05003260 int ret;
Alex Eldere28fff262012-02-02 08:13:30 -06003261
3262 /* The first four tokens are required */
3263
Alex Elder7ef32142012-02-02 08:13:30 -06003264 len = next_token(&buf);
Alex Elder4fb5d6712012-11-01 10:17:15 -05003265 if (!len) {
3266 rbd_warn(NULL, "no monitor address(es) provided");
3267 return -EINVAL;
3268 }
Alex Elder0ddebc02012-10-25 23:34:41 -05003269 mon_addrs = buf;
Alex Elderf28e5652012-10-25 23:34:41 -05003270 mon_addrs_size = len + 1;
Alex Elder7ef32142012-02-02 08:13:30 -06003271 buf += len;
Alex Eldera725f65e2012-02-02 08:13:30 -06003272
Alex Elderdc79b112012-10-25 23:34:41 -05003273 ret = -EINVAL;
Alex Elderf28e5652012-10-25 23:34:41 -05003274 options = dup_token(&buf, NULL);
3275 if (!options)
Alex Elderdc79b112012-10-25 23:34:41 -05003276 return -ENOMEM;
Alex Elder4fb5d6712012-11-01 10:17:15 -05003277 if (!*options) {
3278 rbd_warn(NULL, "no options provided");
3279 goto out_err;
3280 }
Alex Eldera725f65e2012-02-02 08:13:30 -06003281
Alex Elder859c31d2012-10-25 23:34:42 -05003282 spec = rbd_spec_alloc();
3283 if (!spec)
Alex Elderf28e5652012-10-25 23:34:41 -05003284 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05003285
3286 spec->pool_name = dup_token(&buf, NULL);
3287 if (!spec->pool_name)
3288 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05003289 if (!*spec->pool_name) {
3290 rbd_warn(NULL, "no pool name provided");
3291 goto out_err;
3292 }
Alex Eldere28fff262012-02-02 08:13:30 -06003293
Alex Elder69e7a022012-11-01 08:39:26 -05003294 spec->image_name = dup_token(&buf, NULL);
Alex Elder859c31d2012-10-25 23:34:42 -05003295 if (!spec->image_name)
Alex Elderf28e5652012-10-25 23:34:41 -05003296 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05003297 if (!*spec->image_name) {
3298 rbd_warn(NULL, "no image name provided");
3299 goto out_err;
3300 }
Alex Eldere28fff262012-02-02 08:13:30 -06003301
Alex Elderf28e5652012-10-25 23:34:41 -05003302 /*
3303 * Snapshot name is optional; default is to use "-"
3304 * (indicating the head/no snapshot).
3305 */
Alex Elder3feeb8942012-08-31 17:29:52 -05003306 len = next_token(&buf);
Alex Elder820a5f32012-07-09 21:04:24 -05003307 if (!len) {
Alex Elder3feeb8942012-08-31 17:29:52 -05003308 buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
3309 len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
Alex Elderf28e5652012-10-25 23:34:41 -05003310 } else if (len > RBD_MAX_SNAP_NAME_LEN) {
Alex Elderdc79b112012-10-25 23:34:41 -05003311 ret = -ENAMETOOLONG;
Alex Elderf28e5652012-10-25 23:34:41 -05003312 goto out_err;
Alex Elder849b4262012-07-09 21:04:24 -05003313 }
Alex Elder4caf35f2012-11-01 08:39:27 -05003314 spec->snap_name = kmemdup(buf, len + 1, GFP_KERNEL);
Alex Elder859c31d2012-10-25 23:34:42 -05003315 if (!spec->snap_name)
Alex Elderf28e5652012-10-25 23:34:41 -05003316 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05003317 *(spec->snap_name + len) = '\0';
Alex Eldere5c35532012-10-25 23:34:41 -05003318
Alex Elder0ddebc02012-10-25 23:34:41 -05003319 /* Initialize all rbd options to the defaults */
Alex Eldere28fff262012-02-02 08:13:30 -06003320
Alex Elder4e9afeb2012-10-25 23:34:41 -05003321 rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL);
3322 if (!rbd_opts)
3323 goto out_mem;
3324
3325 rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
Alex Elderd22f76e2012-07-12 10:46:35 -05003326
Alex Elder859c31d2012-10-25 23:34:42 -05003327 copts = ceph_parse_options(options, mon_addrs,
Alex Elder0ddebc02012-10-25 23:34:41 -05003328 mon_addrs + mon_addrs_size - 1,
Alex Elder4e9afeb2012-10-25 23:34:41 -05003329 parse_rbd_opts_token, rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05003330 if (IS_ERR(copts)) {
3331 ret = PTR_ERR(copts);
Alex Elderdc79b112012-10-25 23:34:41 -05003332 goto out_err;
3333 }
Alex Elder859c31d2012-10-25 23:34:42 -05003334 kfree(options);
3335
3336 *ceph_opts = copts;
Alex Elder4e9afeb2012-10-25 23:34:41 -05003337 *opts = rbd_opts;
Alex Elder859c31d2012-10-25 23:34:42 -05003338 *rbd_spec = spec;
Alex Elder0ddebc02012-10-25 23:34:41 -05003339
Alex Elderdc79b112012-10-25 23:34:41 -05003340 return 0;
Alex Elderf28e5652012-10-25 23:34:41 -05003341out_mem:
Alex Elderdc79b112012-10-25 23:34:41 -05003342 ret = -ENOMEM;
Alex Elderd22f76e2012-07-12 10:46:35 -05003343out_err:
Alex Elder859c31d2012-10-25 23:34:42 -05003344 kfree(rbd_opts);
3345 rbd_spec_put(spec);
Alex Elderf28e5652012-10-25 23:34:41 -05003346 kfree(options);
Alex Elderd22f76e2012-07-12 10:46:35 -05003347
Alex Elderdc79b112012-10-25 23:34:41 -05003348 return ret;
Alex Eldera725f65e2012-02-02 08:13:30 -06003349}
3350
Alex Elder589d30e2012-07-10 20:30:11 -05003351/*
3352 * An rbd format 2 image has a unique identifier, distinct from the
3353 * name given to it by the user. Internally, that identifier is
3354 * what's used to specify the names of objects related to the image.
3355 *
3356 * A special "rbd id" object is used to map an rbd image name to its
3357 * id. If that object doesn't exist, then there is no v2 rbd image
3358 * with the supplied name.
3359 *
3360 * This function will record the given rbd_dev's image_id field if
3361 * it can be determined, and in that case will return 0. If any
3362 * errors occur a negative errno will be returned and the rbd_dev's
3363 * image_id field will be unchanged (and should be NULL).
3364 */
3365static int rbd_dev_image_id(struct rbd_device *rbd_dev)
3366{
3367 int ret;
3368 size_t size;
3369 char *object_name;
3370 void *response;
3371 void *p;
3372
3373 /*
Alex Elder2c0d0a12012-10-30 19:40:33 -05003374 * When probing a parent image, the image id is already
3375 * known (and the image name likely is not). There's no
3376 * need to fetch the image id again in this case.
3377 */
3378 if (rbd_dev->spec->image_id)
3379 return 0;
3380
3381 /*
Alex Elder589d30e2012-07-10 20:30:11 -05003382 * First, see if the format 2 image id file exists, and if
3383 * so, get the image's persistent id from it.
3384 */
Alex Elder69e7a022012-11-01 08:39:26 -05003385 size = sizeof (RBD_ID_PREFIX) + strlen(rbd_dev->spec->image_name);
Alex Elder589d30e2012-07-10 20:30:11 -05003386 object_name = kmalloc(size, GFP_NOIO);
3387 if (!object_name)
3388 return -ENOMEM;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003389 sprintf(object_name, "%s%s", RBD_ID_PREFIX, rbd_dev->spec->image_name);
Alex Elder589d30e2012-07-10 20:30:11 -05003390 dout("rbd id object name is %s\n", object_name);
3391
3392 /* Response will be an encoded string, which includes a length */
3393
3394 size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
3395 response = kzalloc(size, GFP_NOIO);
3396 if (!response) {
3397 ret = -ENOMEM;
3398 goto out;
3399 }
3400
3401 ret = rbd_req_sync_exec(rbd_dev, object_name,
3402 "rbd", "get_id",
3403 NULL, 0,
Alex Elder07b23912012-11-09 08:43:16 -06003404 response, RBD_IMAGE_ID_LEN_MAX, NULL);
Alex Elder589d30e2012-07-10 20:30:11 -05003405 dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
3406 if (ret < 0)
3407 goto out;
Alex Eldera0ea3a42012-10-10 21:19:13 -07003408 ret = 0; /* rbd_req_sync_exec() can return positive */
Alex Elder589d30e2012-07-10 20:30:11 -05003409
3410 p = response;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003411 rbd_dev->spec->image_id = ceph_extract_encoded_string(&p,
Alex Elder589d30e2012-07-10 20:30:11 -05003412 p + RBD_IMAGE_ID_LEN_MAX,
Alex Elder979ed482012-11-01 08:39:26 -05003413 NULL, GFP_NOIO);
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003414 if (IS_ERR(rbd_dev->spec->image_id)) {
3415 ret = PTR_ERR(rbd_dev->spec->image_id);
3416 rbd_dev->spec->image_id = NULL;
Alex Elder589d30e2012-07-10 20:30:11 -05003417 } else {
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003418 dout("image_id is %s\n", rbd_dev->spec->image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05003419 }
3420out:
3421 kfree(response);
3422 kfree(object_name);
3423
3424 return ret;
3425}
3426
Alex Eldera30b71b2012-07-10 20:30:11 -05003427static int rbd_dev_v1_probe(struct rbd_device *rbd_dev)
3428{
3429 int ret;
3430 size_t size;
3431
3432 /* Version 1 images have no id; empty string is used */
3433
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003434 rbd_dev->spec->image_id = kstrdup("", GFP_KERNEL);
3435 if (!rbd_dev->spec->image_id)
Alex Eldera30b71b2012-07-10 20:30:11 -05003436 return -ENOMEM;
Alex Eldera30b71b2012-07-10 20:30:11 -05003437
3438 /* Record the header object name for this rbd image. */
3439
Alex Elder69e7a022012-11-01 08:39:26 -05003440 size = strlen(rbd_dev->spec->image_name) + sizeof (RBD_SUFFIX);
Alex Eldera30b71b2012-07-10 20:30:11 -05003441 rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
3442 if (!rbd_dev->header_name) {
3443 ret = -ENOMEM;
3444 goto out_err;
3445 }
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003446 sprintf(rbd_dev->header_name, "%s%s",
3447 rbd_dev->spec->image_name, RBD_SUFFIX);
Alex Eldera30b71b2012-07-10 20:30:11 -05003448
3449 /* Populate rbd image metadata */
3450
3451 ret = rbd_read_header(rbd_dev, &rbd_dev->header);
3452 if (ret < 0)
3453 goto out_err;
Alex Elder86b00e02012-10-25 23:34:42 -05003454
3455 /* Version 1 images have no parent (no layering) */
3456
3457 rbd_dev->parent_spec = NULL;
3458 rbd_dev->parent_overlap = 0;
3459
Alex Eldera30b71b2012-07-10 20:30:11 -05003460 rbd_dev->image_format = 1;
3461
3462 dout("discovered version 1 image, header name is %s\n",
3463 rbd_dev->header_name);
3464
3465 return 0;
3466
3467out_err:
3468 kfree(rbd_dev->header_name);
3469 rbd_dev->header_name = NULL;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003470 kfree(rbd_dev->spec->image_id);
3471 rbd_dev->spec->image_id = NULL;
Alex Eldera30b71b2012-07-10 20:30:11 -05003472
3473 return ret;
3474}
3475
3476static int rbd_dev_v2_probe(struct rbd_device *rbd_dev)
3477{
3478 size_t size;
Alex Elder9d475de2012-07-03 16:01:19 -05003479 int ret;
Alex Elder6e14b1a2012-07-03 16:01:19 -05003480 u64 ver = 0;
Alex Eldera30b71b2012-07-10 20:30:11 -05003481
3482 /*
3483 * Image id was filled in by the caller. Record the header
3484 * object name for this rbd image.
3485 */
Alex Elder979ed482012-11-01 08:39:26 -05003486 size = sizeof (RBD_HEADER_PREFIX) + strlen(rbd_dev->spec->image_id);
Alex Eldera30b71b2012-07-10 20:30:11 -05003487 rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
3488 if (!rbd_dev->header_name)
3489 return -ENOMEM;
3490 sprintf(rbd_dev->header_name, "%s%s",
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003491 RBD_HEADER_PREFIX, rbd_dev->spec->image_id);
Alex Elder9d475de2012-07-03 16:01:19 -05003492
3493 /* Get the size and object order for the image */
3494
3495 ret = rbd_dev_v2_image_size(rbd_dev);
3496 if (ret < 0)
3497 goto out_err;
Alex Elder1e130192012-07-03 16:01:19 -05003498
3499 /* Get the object prefix (a.k.a. block_name) for the image */
3500
3501 ret = rbd_dev_v2_object_prefix(rbd_dev);
3502 if (ret < 0)
3503 goto out_err;
Alex Elderb1b54022012-07-03 16:01:19 -05003504
Alex Elderd8891402012-10-09 13:50:17 -07003505 /* Get the and check features for the image */
Alex Elderb1b54022012-07-03 16:01:19 -05003506
3507 ret = rbd_dev_v2_features(rbd_dev);
3508 if (ret < 0)
3509 goto out_err;
Alex Elder35d489f2012-07-03 16:01:19 -05003510
Alex Elder86b00e02012-10-25 23:34:42 -05003511 /* If the image supports layering, get the parent info */
3512
3513 if (rbd_dev->header.features & RBD_FEATURE_LAYERING) {
3514 ret = rbd_dev_v2_parent_info(rbd_dev);
3515 if (ret < 0)
3516 goto out_err;
3517 }
3518
Alex Elder6e14b1a2012-07-03 16:01:19 -05003519 /* crypto and compression type aren't (yet) supported for v2 images */
Alex Elder35d489f2012-07-03 16:01:19 -05003520
Alex Elder6e14b1a2012-07-03 16:01:19 -05003521 rbd_dev->header.crypt_type = 0;
3522 rbd_dev->header.comp_type = 0;
3523
3524 /* Get the snapshot context, plus the header version */
3525
3526 ret = rbd_dev_v2_snap_context(rbd_dev, &ver);
Alex Elder35d489f2012-07-03 16:01:19 -05003527 if (ret)
3528 goto out_err;
Alex Elder6e14b1a2012-07-03 16:01:19 -05003529 rbd_dev->header.obj_version = ver;
3530
Alex Eldera30b71b2012-07-10 20:30:11 -05003531 rbd_dev->image_format = 2;
3532
3533 dout("discovered version 2 image, header name is %s\n",
3534 rbd_dev->header_name);
3535
Alex Elder35152972012-08-31 17:29:55 -05003536 return 0;
Alex Elder9d475de2012-07-03 16:01:19 -05003537out_err:
Alex Elder86b00e02012-10-25 23:34:42 -05003538 rbd_dev->parent_overlap = 0;
3539 rbd_spec_put(rbd_dev->parent_spec);
3540 rbd_dev->parent_spec = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05003541 kfree(rbd_dev->header_name);
3542 rbd_dev->header_name = NULL;
Alex Elder1e130192012-07-03 16:01:19 -05003543 kfree(rbd_dev->header.object_prefix);
3544 rbd_dev->header.object_prefix = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05003545
3546 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05003547}
3548
Alex Elder83a06262012-10-30 15:47:17 -05003549static int rbd_dev_probe_finish(struct rbd_device *rbd_dev)
3550{
3551 int ret;
3552
3553 /* no need to lock here, as rbd_dev is not registered yet */
3554 ret = rbd_dev_snaps_update(rbd_dev);
3555 if (ret)
3556 return ret;
3557
Alex Elder9e15b772012-10-30 19:40:33 -05003558 ret = rbd_dev_probe_update_spec(rbd_dev);
3559 if (ret)
3560 goto err_out_snaps;
3561
Alex Elder83a06262012-10-30 15:47:17 -05003562 ret = rbd_dev_set_mapping(rbd_dev);
3563 if (ret)
3564 goto err_out_snaps;
3565
3566 /* generate unique id: find highest unique id, add one */
3567 rbd_dev_id_get(rbd_dev);
3568
3569 /* Fill in the device name, now that we have its id. */
3570 BUILD_BUG_ON(DEV_NAME_LEN
3571 < sizeof (RBD_DRV_NAME) + MAX_INT_FORMAT_WIDTH);
3572 sprintf(rbd_dev->name, "%s%d", RBD_DRV_NAME, rbd_dev->dev_id);
3573
3574 /* Get our block major device number. */
3575
3576 ret = register_blkdev(0, rbd_dev->name);
3577 if (ret < 0)
3578 goto err_out_id;
3579 rbd_dev->major = ret;
3580
3581 /* Set up the blkdev mapping. */
3582
3583 ret = rbd_init_disk(rbd_dev);
3584 if (ret)
3585 goto err_out_blkdev;
3586
3587 ret = rbd_bus_add_dev(rbd_dev);
3588 if (ret)
3589 goto err_out_disk;
3590
3591 /*
3592 * At this point cleanup in the event of an error is the job
3593 * of the sysfs code (initiated by rbd_bus_del_dev()).
3594 */
3595 down_write(&rbd_dev->header_rwsem);
3596 ret = rbd_dev_snaps_register(rbd_dev);
3597 up_write(&rbd_dev->header_rwsem);
3598 if (ret)
3599 goto err_out_bus;
3600
3601 ret = rbd_init_watch_dev(rbd_dev);
3602 if (ret)
3603 goto err_out_bus;
3604
3605 /* Everything's ready. Announce the disk to the world. */
3606
3607 add_disk(rbd_dev->disk);
3608
3609 pr_info("%s: added with size 0x%llx\n", rbd_dev->disk->disk_name,
3610 (unsigned long long) rbd_dev->mapping.size);
3611
3612 return ret;
3613err_out_bus:
3614 /* this will also clean up rest of rbd_dev stuff */
3615
3616 rbd_bus_del_dev(rbd_dev);
3617
3618 return ret;
3619err_out_disk:
3620 rbd_free_disk(rbd_dev);
3621err_out_blkdev:
3622 unregister_blkdev(rbd_dev->major, rbd_dev->name);
3623err_out_id:
3624 rbd_dev_id_put(rbd_dev);
3625err_out_snaps:
3626 rbd_remove_all_snaps(rbd_dev);
3627
3628 return ret;
3629}
3630
Alex Eldera30b71b2012-07-10 20:30:11 -05003631/*
3632 * Probe for the existence of the header object for the given rbd
3633 * device. For format 2 images this includes determining the image
3634 * id.
3635 */
3636static int rbd_dev_probe(struct rbd_device *rbd_dev)
3637{
3638 int ret;
3639
3640 /*
3641 * Get the id from the image id object. If it's not a
3642 * format 2 image, we'll get ENOENT back, and we'll assume
3643 * it's a format 1 image.
3644 */
3645 ret = rbd_dev_image_id(rbd_dev);
3646 if (ret)
3647 ret = rbd_dev_v1_probe(rbd_dev);
3648 else
3649 ret = rbd_dev_v2_probe(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05003650 if (ret) {
Alex Eldera30b71b2012-07-10 20:30:11 -05003651 dout("probe failed, returning %d\n", ret);
3652
Alex Elder83a06262012-10-30 15:47:17 -05003653 return ret;
3654 }
3655
3656 ret = rbd_dev_probe_finish(rbd_dev);
3657 if (ret)
3658 rbd_header_free(&rbd_dev->header);
3659
Alex Eldera30b71b2012-07-10 20:30:11 -05003660 return ret;
3661}
3662
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003663static ssize_t rbd_add(struct bus_type *bus,
3664 const char *buf,
3665 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003666{
Alex Eldercb8627c2012-07-09 21:04:23 -05003667 struct rbd_device *rbd_dev = NULL;
Alex Elderdc79b112012-10-25 23:34:41 -05003668 struct ceph_options *ceph_opts = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05003669 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05003670 struct rbd_spec *spec = NULL;
Alex Elder9d3997f2012-10-25 23:34:42 -05003671 struct rbd_client *rbdc;
Alex Elder27cc2592012-02-02 08:13:30 -06003672 struct ceph_osd_client *osdc;
3673 int rc = -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003674
3675 if (!try_module_get(THIS_MODULE))
3676 return -ENODEV;
3677
Alex Eldera725f65e2012-02-02 08:13:30 -06003678 /* parse add command */
Alex Elder859c31d2012-10-25 23:34:42 -05003679 rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec);
Alex Elderdc79b112012-10-25 23:34:41 -05003680 if (rc < 0)
Alex Elderbd4ba652012-10-25 23:34:42 -05003681 goto err_out_module;
Alex Eldera725f65e2012-02-02 08:13:30 -06003682
Alex Elder9d3997f2012-10-25 23:34:42 -05003683 rbdc = rbd_get_client(ceph_opts);
3684 if (IS_ERR(rbdc)) {
3685 rc = PTR_ERR(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05003686 goto err_out_args;
Alex Elder9d3997f2012-10-25 23:34:42 -05003687 }
Alex Elderc53d5892012-10-25 23:34:42 -05003688 ceph_opts = NULL; /* rbd_dev client now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003689
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003690 /* pick the pool */
Alex Elder9d3997f2012-10-25 23:34:42 -05003691 osdc = &rbdc->client->osdc;
Alex Elder859c31d2012-10-25 23:34:42 -05003692 rc = ceph_pg_poolid_by_name(osdc->osdmap, spec->pool_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003693 if (rc < 0)
3694 goto err_out_client;
Alex Elder859c31d2012-10-25 23:34:42 -05003695 spec->pool_id = (u64) rc;
3696
Alex Elder0903e872012-11-14 12:25:19 -06003697 /* The ceph file layout needs to fit pool id in 32 bits */
3698
3699 if (WARN_ON(spec->pool_id > (u64) U32_MAX)) {
3700 rc = -EIO;
3701 goto err_out_client;
3702 }
3703
Alex Elderc53d5892012-10-25 23:34:42 -05003704 rbd_dev = rbd_dev_create(rbdc, spec);
Alex Elderbd4ba652012-10-25 23:34:42 -05003705 if (!rbd_dev)
3706 goto err_out_client;
Alex Elderc53d5892012-10-25 23:34:42 -05003707 rbdc = NULL; /* rbd_dev now owns this */
3708 spec = NULL; /* rbd_dev now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003709
Alex Elderbd4ba652012-10-25 23:34:42 -05003710 rbd_dev->mapping.read_only = rbd_opts->read_only;
Alex Elderc53d5892012-10-25 23:34:42 -05003711 kfree(rbd_opts);
3712 rbd_opts = NULL; /* done with this */
Alex Elderbd4ba652012-10-25 23:34:42 -05003713
Alex Eldera30b71b2012-07-10 20:30:11 -05003714 rc = rbd_dev_probe(rbd_dev);
3715 if (rc < 0)
Alex Elderc53d5892012-10-25 23:34:42 -05003716 goto err_out_rbd_dev;
Alex Elder05fd6f62012-08-29 17:11:07 -05003717
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003718 return count;
Alex Elderc53d5892012-10-25 23:34:42 -05003719err_out_rbd_dev:
3720 rbd_dev_destroy(rbd_dev);
Alex Elderbd4ba652012-10-25 23:34:42 -05003721err_out_client:
Alex Elder9d3997f2012-10-25 23:34:42 -05003722 rbd_put_client(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05003723err_out_args:
Alex Elder78cea762012-10-25 23:34:41 -05003724 if (ceph_opts)
3725 ceph_destroy_options(ceph_opts);
Alex Elder4e9afeb2012-10-25 23:34:41 -05003726 kfree(rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05003727 rbd_spec_put(spec);
Alex Elderbd4ba652012-10-25 23:34:42 -05003728err_out_module:
3729 module_put(THIS_MODULE);
Alex Elder27cc2592012-02-02 08:13:30 -06003730
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003731 dout("Error adding device %s\n", buf);
Alex Elder27cc2592012-02-02 08:13:30 -06003732
3733 return (ssize_t) rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003734}
3735
Alex Elderde71a292012-07-03 16:01:19 -05003736static struct rbd_device *__rbd_get_dev(unsigned long dev_id)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003737{
3738 struct list_head *tmp;
3739 struct rbd_device *rbd_dev;
3740
Alex Eldere124a82f2012-01-29 13:57:44 -06003741 spin_lock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003742 list_for_each(tmp, &rbd_dev_list) {
3743 rbd_dev = list_entry(tmp, struct rbd_device, node);
Alex Elderde71a292012-07-03 16:01:19 -05003744 if (rbd_dev->dev_id == dev_id) {
Alex Eldere124a82f2012-01-29 13:57:44 -06003745 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003746 return rbd_dev;
Alex Eldere124a82f2012-01-29 13:57:44 -06003747 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003748 }
Alex Eldere124a82f2012-01-29 13:57:44 -06003749 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003750 return NULL;
3751}
3752
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003753static void rbd_dev_release(struct device *dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003754{
Alex Elder593a9e72012-02-07 12:03:37 -06003755 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003756
Alex Elder1dbb4392012-01-24 10:08:37 -06003757 if (rbd_dev->watch_request) {
3758 struct ceph_client *client = rbd_dev->rbd_client->client;
3759
3760 ceph_osdc_unregister_linger_request(&client->osdc,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003761 rbd_dev->watch_request);
Alex Elder1dbb4392012-01-24 10:08:37 -06003762 }
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07003763 if (rbd_dev->watch_event)
Alex Elder907703d2012-11-13 21:11:15 -06003764 rbd_req_sync_watch(rbd_dev, 0);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003765
3766 /* clean up and free blkdev */
3767 rbd_free_disk(rbd_dev);
3768 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Alex Elder32eec682012-02-08 16:11:14 -06003769
Alex Elder2ac4e752012-07-10 20:30:10 -05003770 /* release allocated disk header fields */
3771 rbd_header_free(&rbd_dev->header);
3772
Alex Elder32eec682012-02-08 16:11:14 -06003773 /* done with the id, and with the rbd_dev */
Alex Eldere2839302012-08-29 17:11:06 -05003774 rbd_dev_id_put(rbd_dev);
Alex Elderc53d5892012-10-25 23:34:42 -05003775 rbd_assert(rbd_dev->rbd_client != NULL);
3776 rbd_dev_destroy(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003777
3778 /* release module ref */
3779 module_put(THIS_MODULE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003780}
3781
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003782static ssize_t rbd_remove(struct bus_type *bus,
3783 const char *buf,
3784 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003785{
3786 struct rbd_device *rbd_dev = NULL;
3787 int target_id, rc;
3788 unsigned long ul;
3789 int ret = count;
3790
3791 rc = strict_strtoul(buf, 10, &ul);
3792 if (rc)
3793 return rc;
3794
3795 /* convert to int; abort if we lost anything in the conversion */
3796 target_id = (int) ul;
3797 if (target_id != ul)
3798 return -EINVAL;
3799
3800 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
3801
3802 rbd_dev = __rbd_get_dev(target_id);
3803 if (!rbd_dev) {
3804 ret = -ENOENT;
3805 goto done;
3806 }
3807
Alex Elder42382b72012-11-16 09:29:16 -06003808 if (rbd_dev->open_count) {
3809 ret = -EBUSY;
3810 goto done;
3811 }
3812
Alex Elder41f38c22012-10-25 23:34:40 -05003813 rbd_remove_all_snaps(rbd_dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003814 rbd_bus_del_dev(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003815
3816done:
3817 mutex_unlock(&ctl_mutex);
Alex Elderaafb2302012-09-06 16:00:54 -05003818
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003819 return ret;
3820}
3821
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003822/*
3823 * create control files in sysfs
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003824 * /sys/bus/rbd/...
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003825 */
3826static int rbd_sysfs_init(void)
3827{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003828 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003829
Alex Elderfed4c142012-02-07 12:03:36 -06003830 ret = device_register(&rbd_root_dev);
Alex Elder21079782012-01-24 10:08:36 -06003831 if (ret < 0)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003832 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003833
Alex Elderfed4c142012-02-07 12:03:36 -06003834 ret = bus_register(&rbd_bus_type);
3835 if (ret < 0)
3836 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003837
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003838 return ret;
3839}
3840
3841static void rbd_sysfs_cleanup(void)
3842{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003843 bus_unregister(&rbd_bus_type);
Alex Elderfed4c142012-02-07 12:03:36 -06003844 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003845}
3846
3847int __init rbd_init(void)
3848{
3849 int rc;
3850
3851 rc = rbd_sysfs_init();
3852 if (rc)
3853 return rc;
Alex Elderf0f8cef2012-01-29 13:57:44 -06003854 pr_info("loaded " RBD_DRV_NAME_LONG "\n");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003855 return 0;
3856}
3857
3858void __exit rbd_exit(void)
3859{
3860 rbd_sysfs_cleanup();
3861}
3862
3863module_init(rbd_init);
3864module_exit(rbd_exit);
3865
3866MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
3867MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
3868MODULE_DESCRIPTION("rados block device");
3869
3870/* following authorship retained from original osdblk.c */
3871MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
3872
3873MODULE_LICENSE("GPL");