blob: 13a381b2a7793c087478386a0db05562740512e7 [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 Elderf0f8cef2012-01-29 13:57:44 -060055#define RBD_DRV_NAME "rbd"
56#define RBD_DRV_NAME_LONG "rbd (rados block device)"
Yehuda Sadeh602adf42010-08-12 16:11:25 -070057
58#define RBD_MINORS_PER_MAJOR 256 /* max minors per blkdev */
59
Alex Elderd4b125e2012-07-03 16:01:19 -050060#define RBD_SNAP_DEV_NAME_PREFIX "snap_"
61#define RBD_MAX_SNAP_NAME_LEN \
62 (NAME_MAX - (sizeof (RBD_SNAP_DEV_NAME_PREFIX) - 1))
63
Alex Elder35d489f2012-07-03 16:01:19 -050064#define RBD_MAX_SNAP_COUNT 510 /* allows max snapc to fit in 4KB */
Yehuda Sadeh602adf42010-08-12 16:11:25 -070065
66#define RBD_SNAP_HEAD_NAME "-"
67
Alex Elder9e15b772012-10-30 19:40:33 -050068/* This allows a single page to hold an image name sent by OSD */
69#define RBD_IMAGE_NAME_LEN_MAX (PAGE_SIZE - sizeof (__le32) - 1)
Alex Elder1e130192012-07-03 16:01:19 -050070#define RBD_IMAGE_ID_LEN_MAX 64
Alex Elder9e15b772012-10-30 19:40:33 -050071
Alex Elder1e130192012-07-03 16:01:19 -050072#define RBD_OBJ_PREFIX_LEN_MAX 64
Alex Elder589d30e2012-07-10 20:30:11 -050073
Alex Elderd8891402012-10-09 13:50:17 -070074/* Feature bits */
75
Alex Elder5cbf6f122013-04-11 09:29:48 -050076#define RBD_FEATURE_LAYERING (1<<0)
77#define RBD_FEATURE_STRIPINGV2 (1<<1)
78#define RBD_FEATURES_ALL \
79 (RBD_FEATURE_LAYERING | RBD_FEATURE_STRIPINGV2)
Alex Elderd8891402012-10-09 13:50:17 -070080
81/* Features supported by this (client software) implementation. */
82
Alex Elder5cbf6f122013-04-11 09:29:48 -050083#define RBD_FEATURES_SUPPORTED (0)
Alex Elderd8891402012-10-09 13:50:17 -070084
Alex Elder81a89792012-02-02 08:13:30 -060085/*
86 * An RBD device name will be "rbd#", where the "rbd" comes from
87 * RBD_DRV_NAME above, and # is a unique integer identifier.
88 * MAX_INT_FORMAT_WIDTH is used in ensuring DEV_NAME_LEN is big
89 * enough to hold all possible device names.
90 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -070091#define DEV_NAME_LEN 32
Alex Elder81a89792012-02-02 08:13:30 -060092#define MAX_INT_FORMAT_WIDTH ((5 * sizeof (int)) / 2 + 1)
Yehuda Sadeh602adf42010-08-12 16:11:25 -070093
94/*
95 * block device image metadata (in-memory version)
96 */
97struct rbd_image_header {
Alex Elderf84344f2012-08-31 17:29:51 -050098 /* These four fields never change for a given rbd image */
Alex Elder849b4262012-07-09 21:04:24 -050099 char *object_prefix;
Alex Elder34b13182012-07-13 20:35:12 -0500100 u64 features;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700101 __u8 obj_order;
102 __u8 crypt_type;
103 __u8 comp_type;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700104
Alex Elderf84344f2012-08-31 17:29:51 -0500105 /* The remaining fields need to be updated occasionally */
106 u64 image_size;
107 struct ceph_snap_context *snapc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700108 char *snap_names;
109 u64 *snap_sizes;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700110
111 u64 obj_version;
112};
113
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500114/*
115 * An rbd image specification.
116 *
117 * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely
Alex Elderc66c6e02012-11-01 08:39:26 -0500118 * identify an image. Each rbd_dev structure includes a pointer to
119 * an rbd_spec structure that encapsulates this identity.
120 *
121 * Each of the id's in an rbd_spec has an associated name. For a
122 * user-mapped image, the names are supplied and the id's associated
123 * with them are looked up. For a layered image, a parent image is
124 * defined by the tuple, and the names are looked up.
125 *
126 * An rbd_dev structure contains a parent_spec pointer which is
127 * non-null if the image it represents is a child in a layered
128 * image. This pointer will refer to the rbd_spec structure used
129 * by the parent rbd_dev for its own identity (i.e., the structure
130 * is shared between the parent and child).
131 *
132 * Since these structures are populated once, during the discovery
133 * phase of image construction, they are effectively immutable so
134 * we make no effort to synchronize access to them.
135 *
136 * Note that code herein does not assume the image name is known (it
137 * could be a null pointer).
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500138 */
139struct rbd_spec {
140 u64 pool_id;
141 char *pool_name;
142
143 char *image_id;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500144 char *image_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500145
146 u64 snap_id;
147 char *snap_name;
148
149 struct kref kref;
150};
151
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700152/*
Alex Elderf0f8cef2012-01-29 13:57:44 -0600153 * an instance of the client. multiple devices may share an rbd client.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700154 */
155struct rbd_client {
156 struct ceph_client *client;
157 struct kref kref;
158 struct list_head node;
159};
160
Alex Elderbf0d5f502012-11-22 00:00:08 -0600161struct rbd_img_request;
162typedef void (*rbd_img_callback_t)(struct rbd_img_request *);
163
164#define BAD_WHICH U32_MAX /* Good which or bad which, which? */
165
166struct rbd_obj_request;
167typedef void (*rbd_obj_callback_t)(struct rbd_obj_request *);
168
Alex Elder9969ebc2013-01-18 12:31:10 -0600169enum obj_request_type {
170 OBJ_REQUEST_NODATA, OBJ_REQUEST_BIO, OBJ_REQUEST_PAGES
171};
Alex Elderbf0d5f502012-11-22 00:00:08 -0600172
Alex Elder926f9b32013-02-11 12:33:24 -0600173enum obj_req_flags {
174 OBJ_REQ_DONE, /* completion flag: not done = 0, done = 1 */
Alex Elder6365d332013-02-11 12:33:24 -0600175 OBJ_REQ_IMG_DATA, /* object usage: standalone = 0, image = 1 */
Alex Elder926f9b32013-02-11 12:33:24 -0600176};
177
Alex Elderbf0d5f502012-11-22 00:00:08 -0600178struct rbd_obj_request {
179 const char *object_name;
180 u64 offset; /* object start byte */
181 u64 length; /* bytes from offset */
Alex Elder926f9b32013-02-11 12:33:24 -0600182 unsigned long flags;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600183
184 struct rbd_img_request *img_request;
Alex Elder7da22d22013-01-24 16:13:36 -0600185 u64 img_offset; /* image relative offset */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600186 struct list_head links; /* img_request->obj_requests */
187 u32 which; /* posn image request list */
188
189 enum obj_request_type type;
Alex Elder788e2df2013-01-17 12:25:27 -0600190 union {
191 struct bio *bio_list;
192 struct {
193 struct page **pages;
194 u32 page_count;
195 };
196 };
Alex Elderbf0d5f502012-11-22 00:00:08 -0600197
198 struct ceph_osd_request *osd_req;
199
200 u64 xferred; /* bytes transferred */
201 u64 version;
Sage Weil1b83bef2013-02-25 16:11:12 -0800202 int result;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600203
204 rbd_obj_callback_t callback;
Alex Elder788e2df2013-01-17 12:25:27 -0600205 struct completion completion;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600206
207 struct kref kref;
208};
209
Alex Elder0c425242013-02-08 09:55:49 -0600210enum img_req_flags {
Alex Elder9849e982013-01-24 16:13:36 -0600211 IMG_REQ_WRITE, /* I/O direction: read = 0, write = 1 */
212 IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */
Alex Elderd0b2e942013-01-24 16:13:36 -0600213 IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */
Alex Elder0c425242013-02-08 09:55:49 -0600214};
215
Alex Elderbf0d5f502012-11-22 00:00:08 -0600216struct rbd_img_request {
Alex Elderbf0d5f502012-11-22 00:00:08 -0600217 struct rbd_device *rbd_dev;
218 u64 offset; /* starting image byte offset */
219 u64 length; /* byte count from offset */
Alex Elder0c425242013-02-08 09:55:49 -0600220 unsigned long flags;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600221 union {
Alex Elder9849e982013-01-24 16:13:36 -0600222 u64 snap_id; /* for reads */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600223 struct ceph_snap_context *snapc; /* for writes */
Alex Elder9849e982013-01-24 16:13:36 -0600224 };
225 union {
226 struct request *rq; /* block request */
227 struct rbd_obj_request *obj_request; /* obj req initiator */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600228 };
229 spinlock_t completion_lock;/* protects next_completion */
230 u32 next_completion;
231 rbd_img_callback_t callback;
Alex Elder55f27e02013-04-10 12:34:25 -0500232 u64 xferred;/* aggregate bytes transferred */
Alex Eldera5a337d2013-01-24 16:13:36 -0600233 int result; /* first nonzero obj_request result */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600234
235 u32 obj_request_count;
236 struct list_head obj_requests; /* rbd_obj_request structs */
237
238 struct kref kref;
239};
240
241#define for_each_obj_request(ireq, oreq) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600242 list_for_each_entry(oreq, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600243#define for_each_obj_request_from(ireq, oreq) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600244 list_for_each_entry_from(oreq, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600245#define for_each_obj_request_safe(ireq, oreq, n) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600246 list_for_each_entry_safe_reverse(oreq, n, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600247
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800248struct rbd_snap {
249 struct device dev;
250 const char *name;
Josh Durgin3591538f2011-12-05 18:25:13 -0800251 u64 size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800252 struct list_head node;
253 u64 id;
Alex Elder34b13182012-07-13 20:35:12 -0500254 u64 features;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800255};
256
Alex Elderf84344f2012-08-31 17:29:51 -0500257struct rbd_mapping {
Alex Elder99c1f082012-08-30 14:42:15 -0500258 u64 size;
Alex Elder34b13182012-07-13 20:35:12 -0500259 u64 features;
Alex Elderf84344f2012-08-31 17:29:51 -0500260 bool read_only;
261};
262
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700263/*
264 * a single device
265 */
266struct rbd_device {
Alex Elderde71a292012-07-03 16:01:19 -0500267 int dev_id; /* blkdev unique id */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700268
269 int major; /* blkdev assigned major */
270 struct gendisk *disk; /* blkdev's gendisk and rq */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700271
Alex Eldera30b71b2012-07-10 20:30:11 -0500272 u32 image_format; /* Either 1 or 2 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700273 struct rbd_client *rbd_client;
274
275 char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */
276
Alex Elderb82d1672013-01-14 12:43:31 -0600277 spinlock_t lock; /* queue, flags, open_count */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700278
279 struct rbd_image_header header;
Alex Elderb82d1672013-01-14 12:43:31 -0600280 unsigned long flags; /* possibly lock protected */
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500281 struct rbd_spec *spec;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700282
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500283 char *header_name;
Alex Elder971f8392012-10-25 23:34:41 -0500284
Alex Elder0903e872012-11-14 12:25:19 -0600285 struct ceph_file_layout layout;
286
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700287 struct ceph_osd_event *watch_event;
Alex Elder975241a2013-01-25 17:08:55 -0600288 struct rbd_obj_request *watch_request;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700289
Alex Elder86b00e02012-10-25 23:34:42 -0500290 struct rbd_spec *parent_spec;
291 u64 parent_overlap;
Alex Elder2f82ee52012-10-30 19:40:33 -0500292 struct rbd_device *parent;
Alex Elder86b00e02012-10-25 23:34:42 -0500293
Josh Durginc6666012011-11-21 17:11:12 -0800294 /* protects updating the header */
295 struct rw_semaphore header_rwsem;
Alex Elderf84344f2012-08-31 17:29:51 -0500296
297 struct rbd_mapping mapping;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700298
299 struct list_head node;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800300
301 /* list of snapshots */
302 struct list_head snaps;
303
304 /* sysfs related */
305 struct device dev;
Alex Elderb82d1672013-01-14 12:43:31 -0600306 unsigned long open_count; /* protected by lock */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800307};
308
Alex Elderb82d1672013-01-14 12:43:31 -0600309/*
310 * Flag bits for rbd_dev->flags. If atomicity is required,
311 * rbd_dev->lock is used to protect access.
312 *
313 * Currently, only the "removing" flag (which is coupled with the
314 * "open_count" field) requires atomic access.
315 */
Alex Elder6d292902013-01-14 12:43:31 -0600316enum rbd_dev_flags {
317 RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */
Alex Elderb82d1672013-01-14 12:43:31 -0600318 RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */
Alex Elder6d292902013-01-14 12:43:31 -0600319};
320
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700321static DEFINE_MUTEX(ctl_mutex); /* Serialize open/close/setup/teardown */
Alex Eldere124a82f2012-01-29 13:57:44 -0600322
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700323static LIST_HEAD(rbd_dev_list); /* devices */
Alex Eldere124a82f2012-01-29 13:57:44 -0600324static DEFINE_SPINLOCK(rbd_dev_list_lock);
325
Alex Elder432b8582012-01-29 13:57:44 -0600326static LIST_HEAD(rbd_client_list); /* clients */
327static DEFINE_SPINLOCK(rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700328
Alex Elder304f6802012-08-31 17:29:52 -0500329static int rbd_dev_snaps_update(struct rbd_device *rbd_dev);
330static int rbd_dev_snaps_register(struct rbd_device *rbd_dev);
331
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800332static void rbd_dev_release(struct device *dev);
Alex Elder41f38c22012-10-25 23:34:40 -0500333static void rbd_remove_snap_dev(struct rbd_snap *snap);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800334
Alex Elderf0f8cef2012-01-29 13:57:44 -0600335static ssize_t rbd_add(struct bus_type *bus, const char *buf,
336 size_t count);
337static ssize_t rbd_remove(struct bus_type *bus, const char *buf,
338 size_t count);
Alex Elder2f82ee52012-10-30 19:40:33 -0500339static int rbd_dev_probe(struct rbd_device *rbd_dev);
Alex Elderf0f8cef2012-01-29 13:57:44 -0600340
341static struct bus_attribute rbd_bus_attrs[] = {
342 __ATTR(add, S_IWUSR, NULL, rbd_add),
343 __ATTR(remove, S_IWUSR, NULL, rbd_remove),
344 __ATTR_NULL
345};
346
347static struct bus_type rbd_bus_type = {
348 .name = "rbd",
349 .bus_attrs = rbd_bus_attrs,
350};
351
352static void rbd_root_dev_release(struct device *dev)
353{
354}
355
356static struct device rbd_root_dev = {
357 .init_name = "rbd",
358 .release = rbd_root_dev_release,
359};
360
Alex Elder06ecc6c2012-11-01 10:17:15 -0500361static __printf(2, 3)
362void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...)
363{
364 struct va_format vaf;
365 va_list args;
366
367 va_start(args, fmt);
368 vaf.fmt = fmt;
369 vaf.va = &args;
370
371 if (!rbd_dev)
372 printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf);
373 else if (rbd_dev->disk)
374 printk(KERN_WARNING "%s: %s: %pV\n",
375 RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf);
376 else if (rbd_dev->spec && rbd_dev->spec->image_name)
377 printk(KERN_WARNING "%s: image %s: %pV\n",
378 RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf);
379 else if (rbd_dev->spec && rbd_dev->spec->image_id)
380 printk(KERN_WARNING "%s: id %s: %pV\n",
381 RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf);
382 else /* punt */
383 printk(KERN_WARNING "%s: rbd_dev %p: %pV\n",
384 RBD_DRV_NAME, rbd_dev, &vaf);
385 va_end(args);
386}
387
Alex Elderaafb2302012-09-06 16:00:54 -0500388#ifdef RBD_DEBUG
389#define rbd_assert(expr) \
390 if (unlikely(!(expr))) { \
391 printk(KERN_ERR "\nAssertion failure in %s() " \
392 "at line %d:\n\n" \
393 "\trbd_assert(%s);\n\n", \
394 __func__, __LINE__, #expr); \
395 BUG(); \
396 }
397#else /* !RBD_DEBUG */
398# define rbd_assert(expr) ((void) 0)
399#endif /* !RBD_DEBUG */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800400
Alex Elder8b3e1a52013-01-24 16:13:36 -0600401static void rbd_img_parent_read(struct rbd_obj_request *obj_request);
402
Alex Elder117973f2012-08-31 17:29:55 -0500403static int rbd_dev_refresh(struct rbd_device *rbd_dev, u64 *hver);
404static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev, u64 *hver);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700405
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700406static int rbd_open(struct block_device *bdev, fmode_t mode)
407{
Alex Elderf0f8cef2012-01-29 13:57:44 -0600408 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
Alex Elderb82d1672013-01-14 12:43:31 -0600409 bool removing = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700410
Alex Elderf84344f2012-08-31 17:29:51 -0500411 if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700412 return -EROFS;
413
Alex Eldera14ea262013-02-05 13:23:12 -0600414 spin_lock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600415 if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags))
416 removing = true;
417 else
418 rbd_dev->open_count++;
Alex Eldera14ea262013-02-05 13:23:12 -0600419 spin_unlock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600420 if (removing)
421 return -ENOENT;
422
Alex Elder42382b72012-11-16 09:29:16 -0600423 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Alex Elderc3e946c2012-11-16 09:29:16 -0600424 (void) get_device(&rbd_dev->dev);
Alex Elderf84344f2012-08-31 17:29:51 -0500425 set_device_ro(bdev, rbd_dev->mapping.read_only);
Alex Elder42382b72012-11-16 09:29:16 -0600426 mutex_unlock(&ctl_mutex);
Alex Elder340c7a22012-08-10 13:12:07 -0700427
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700428 return 0;
429}
430
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800431static int rbd_release(struct gendisk *disk, fmode_t mode)
432{
433 struct rbd_device *rbd_dev = disk->private_data;
Alex Elderb82d1672013-01-14 12:43:31 -0600434 unsigned long open_count_before;
435
Alex Eldera14ea262013-02-05 13:23:12 -0600436 spin_lock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600437 open_count_before = rbd_dev->open_count--;
Alex Eldera14ea262013-02-05 13:23:12 -0600438 spin_unlock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600439 rbd_assert(open_count_before > 0);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800440
Alex Elder42382b72012-11-16 09:29:16 -0600441 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Alex Elderc3e946c2012-11-16 09:29:16 -0600442 put_device(&rbd_dev->dev);
Alex Elder42382b72012-11-16 09:29:16 -0600443 mutex_unlock(&ctl_mutex);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800444
445 return 0;
446}
447
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700448static const struct block_device_operations rbd_bd_ops = {
449 .owner = THIS_MODULE,
450 .open = rbd_open,
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800451 .release = rbd_release,
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700452};
453
454/*
455 * Initialize an rbd client instance.
Alex Elder43ae4702012-07-03 16:01:18 -0500456 * We own *ceph_opts.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700457 */
Alex Elderf8c38922012-08-10 13:12:07 -0700458static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700459{
460 struct rbd_client *rbdc;
461 int ret = -ENOMEM;
462
Alex Elder37206ee2013-02-20 17:32:08 -0600463 dout("%s:\n", __func__);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700464 rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL);
465 if (!rbdc)
466 goto out_opt;
467
468 kref_init(&rbdc->kref);
469 INIT_LIST_HEAD(&rbdc->node);
470
Alex Elderbc534d82012-01-29 13:57:44 -0600471 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
472
Alex Elder43ae4702012-07-03 16:01:18 -0500473 rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700474 if (IS_ERR(rbdc->client))
Alex Elderbc534d82012-01-29 13:57:44 -0600475 goto out_mutex;
Alex Elder43ae4702012-07-03 16:01:18 -0500476 ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700477
478 ret = ceph_open_session(rbdc->client);
479 if (ret < 0)
480 goto out_err;
481
Alex Elder432b8582012-01-29 13:57:44 -0600482 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700483 list_add_tail(&rbdc->node, &rbd_client_list);
Alex Elder432b8582012-01-29 13:57:44 -0600484 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700485
Alex Elderbc534d82012-01-29 13:57:44 -0600486 mutex_unlock(&ctl_mutex);
Alex Elder37206ee2013-02-20 17:32:08 -0600487 dout("%s: rbdc %p\n", __func__, rbdc);
Alex Elderbc534d82012-01-29 13:57:44 -0600488
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700489 return rbdc;
490
491out_err:
492 ceph_destroy_client(rbdc->client);
Alex Elderbc534d82012-01-29 13:57:44 -0600493out_mutex:
494 mutex_unlock(&ctl_mutex);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700495 kfree(rbdc);
496out_opt:
Alex Elder43ae4702012-07-03 16:01:18 -0500497 if (ceph_opts)
498 ceph_destroy_options(ceph_opts);
Alex Elder37206ee2013-02-20 17:32:08 -0600499 dout("%s: error %d\n", __func__, ret);
500
Vasiliy Kulikov28f259b2010-09-26 12:59:37 +0400501 return ERR_PTR(ret);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700502}
503
Alex Elder2f82ee52012-10-30 19:40:33 -0500504static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc)
505{
506 kref_get(&rbdc->kref);
507
508 return rbdc;
509}
510
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700511/*
Alex Elder1f7ba332012-08-10 13:12:07 -0700512 * Find a ceph client with specific addr and configuration. If
513 * found, bump its reference count.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700514 */
Alex Elder1f7ba332012-08-10 13:12:07 -0700515static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700516{
517 struct rbd_client *client_node;
Alex Elder1f7ba332012-08-10 13:12:07 -0700518 bool found = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700519
Alex Elder43ae4702012-07-03 16:01:18 -0500520 if (ceph_opts->flags & CEPH_OPT_NOSHARE)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700521 return NULL;
522
Alex Elder1f7ba332012-08-10 13:12:07 -0700523 spin_lock(&rbd_client_list_lock);
524 list_for_each_entry(client_node, &rbd_client_list, node) {
525 if (!ceph_compare_options(ceph_opts, client_node->client)) {
Alex Elder2f82ee52012-10-30 19:40:33 -0500526 __rbd_get_client(client_node);
527
Alex Elder1f7ba332012-08-10 13:12:07 -0700528 found = true;
529 break;
530 }
531 }
532 spin_unlock(&rbd_client_list_lock);
533
534 return found ? client_node : NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700535}
536
537/*
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700538 * mount options
539 */
540enum {
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700541 Opt_last_int,
542 /* int args above */
543 Opt_last_string,
544 /* string args above */
Alex Eldercc0538b2012-08-10 13:12:07 -0700545 Opt_read_only,
546 Opt_read_write,
547 /* Boolean args above */
548 Opt_last_bool,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700549};
550
Alex Elder43ae4702012-07-03 16:01:18 -0500551static match_table_t rbd_opts_tokens = {
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700552 /* int args above */
553 /* string args above */
Alex Elderbe466c12012-10-22 11:31:26 -0500554 {Opt_read_only, "read_only"},
Alex Eldercc0538b2012-08-10 13:12:07 -0700555 {Opt_read_only, "ro"}, /* Alternate spelling */
556 {Opt_read_write, "read_write"},
557 {Opt_read_write, "rw"}, /* Alternate spelling */
558 /* Boolean args above */
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700559 {-1, NULL}
560};
561
Alex Elder98571b52013-01-20 14:44:42 -0600562struct rbd_options {
563 bool read_only;
564};
565
566#define RBD_READ_ONLY_DEFAULT false
567
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700568static int parse_rbd_opts_token(char *c, void *private)
569{
Alex Elder43ae4702012-07-03 16:01:18 -0500570 struct rbd_options *rbd_opts = private;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700571 substring_t argstr[MAX_OPT_ARGS];
572 int token, intval, ret;
573
Alex Elder43ae4702012-07-03 16:01:18 -0500574 token = match_token(c, rbd_opts_tokens, argstr);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700575 if (token < 0)
576 return -EINVAL;
577
578 if (token < Opt_last_int) {
579 ret = match_int(&argstr[0], &intval);
580 if (ret < 0) {
581 pr_err("bad mount option arg (not int) "
582 "at '%s'\n", c);
583 return ret;
584 }
585 dout("got int token %d val %d\n", token, intval);
586 } else if (token > Opt_last_int && token < Opt_last_string) {
587 dout("got string token %d val %s\n", token,
588 argstr[0].from);
Alex Eldercc0538b2012-08-10 13:12:07 -0700589 } else if (token > Opt_last_string && token < Opt_last_bool) {
590 dout("got Boolean token %d\n", token);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700591 } else {
592 dout("got token %d\n", token);
593 }
594
595 switch (token) {
Alex Eldercc0538b2012-08-10 13:12:07 -0700596 case Opt_read_only:
597 rbd_opts->read_only = true;
598 break;
599 case Opt_read_write:
600 rbd_opts->read_only = false;
601 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700602 default:
Alex Elderaafb2302012-09-06 16:00:54 -0500603 rbd_assert(false);
604 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700605 }
606 return 0;
607}
608
609/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700610 * Get a ceph client with specific addr and configuration, if one does
611 * not exist create it.
612 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500613static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700614{
Alex Elderf8c38922012-08-10 13:12:07 -0700615 struct rbd_client *rbdc;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700616
Alex Elder1f7ba332012-08-10 13:12:07 -0700617 rbdc = rbd_client_find(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500618 if (rbdc) /* using an existing client */
Alex Elder43ae4702012-07-03 16:01:18 -0500619 ceph_destroy_options(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500620 else
Alex Elderf8c38922012-08-10 13:12:07 -0700621 rbdc = rbd_client_create(ceph_opts);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700622
Alex Elder9d3997f2012-10-25 23:34:42 -0500623 return rbdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700624}
625
626/*
627 * Destroy ceph client
Alex Elderd23a4b32012-01-29 13:57:43 -0600628 *
Alex Elder432b8582012-01-29 13:57:44 -0600629 * Caller must hold rbd_client_list_lock.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700630 */
631static void rbd_client_release(struct kref *kref)
632{
633 struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
634
Alex Elder37206ee2013-02-20 17:32:08 -0600635 dout("%s: rbdc %p\n", __func__, rbdc);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500636 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700637 list_del(&rbdc->node);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500638 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700639
640 ceph_destroy_client(rbdc->client);
641 kfree(rbdc);
642}
643
644/*
645 * Drop reference to ceph client node. If it's not referenced anymore, release
646 * it.
647 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500648static void rbd_put_client(struct rbd_client *rbdc)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700649{
Alex Elderc53d5892012-10-25 23:34:42 -0500650 if (rbdc)
651 kref_put(&rbdc->kref, rbd_client_release);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700652}
653
Alex Eldera30b71b2012-07-10 20:30:11 -0500654static bool rbd_image_format_valid(u32 image_format)
655{
656 return image_format == 1 || image_format == 2;
657}
658
Alex Elder8e94af82012-07-25 09:32:40 -0500659static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
660{
Alex Elder103a1502012-08-02 11:29:45 -0500661 size_t size;
662 u32 snap_count;
663
664 /* The header has to start with the magic rbd header text */
665 if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT)))
666 return false;
667
Alex Elderdb2388b2012-10-20 22:17:27 -0500668 /* The bio layer requires at least sector-sized I/O */
669
670 if (ondisk->options.order < SECTOR_SHIFT)
671 return false;
672
673 /* If we use u64 in a few spots we may be able to loosen this */
674
675 if (ondisk->options.order > 8 * sizeof (int) - 1)
676 return false;
677
Alex Elder103a1502012-08-02 11:29:45 -0500678 /*
679 * The size of a snapshot header has to fit in a size_t, and
680 * that limits the number of snapshots.
681 */
682 snap_count = le32_to_cpu(ondisk->snap_count);
683 size = SIZE_MAX - sizeof (struct ceph_snap_context);
684 if (snap_count > size / sizeof (__le64))
685 return false;
686
687 /*
688 * Not only that, but the size of the entire the snapshot
689 * header must also be representable in a size_t.
690 */
691 size -= snap_count * sizeof (__le64);
692 if ((u64) size < le64_to_cpu(ondisk->snap_names_len))
693 return false;
694
695 return true;
Alex Elder8e94af82012-07-25 09:32:40 -0500696}
697
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700698/*
699 * Create a new header structure, translate header format from the on-disk
700 * header.
701 */
702static int rbd_header_from_disk(struct rbd_image_header *header,
Alex Elder4156d992012-08-02 11:29:46 -0500703 struct rbd_image_header_ondisk *ondisk)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700704{
Alex Elderccece232012-07-10 20:30:10 -0500705 u32 snap_count;
Alex Elder58c17b02012-08-23 23:22:06 -0500706 size_t len;
Alex Elderd2bb24e2012-07-26 23:37:14 -0500707 size_t size;
Alex Elder621901d2012-08-23 23:22:06 -0500708 u32 i;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700709
Alex Elder6a523252012-07-19 17:12:59 -0500710 memset(header, 0, sizeof (*header));
711
Alex Elder103a1502012-08-02 11:29:45 -0500712 snap_count = le32_to_cpu(ondisk->snap_count);
713
Alex Elder58c17b02012-08-23 23:22:06 -0500714 len = strnlen(ondisk->object_prefix, sizeof (ondisk->object_prefix));
715 header->object_prefix = kmalloc(len + 1, GFP_KERNEL);
Alex Elder6a523252012-07-19 17:12:59 -0500716 if (!header->object_prefix)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700717 return -ENOMEM;
Alex Elder58c17b02012-08-23 23:22:06 -0500718 memcpy(header->object_prefix, ondisk->object_prefix, len);
719 header->object_prefix[len] = '\0';
Alex Elder00f1f362012-02-07 12:03:36 -0600720
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700721 if (snap_count) {
Alex Elderf785cc12012-08-23 23:22:06 -0500722 u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len);
723
Alex Elder621901d2012-08-23 23:22:06 -0500724 /* Save a copy of the snapshot names */
725
Alex Elderf785cc12012-08-23 23:22:06 -0500726 if (snap_names_len > (u64) SIZE_MAX)
727 return -EIO;
728 header->snap_names = kmalloc(snap_names_len, GFP_KERNEL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700729 if (!header->snap_names)
Alex Elder6a523252012-07-19 17:12:59 -0500730 goto out_err;
Alex Elderf785cc12012-08-23 23:22:06 -0500731 /*
732 * Note that rbd_dev_v1_header_read() guarantees
733 * the ondisk buffer we're working with has
734 * snap_names_len bytes beyond the end of the
735 * snapshot id array, this memcpy() is safe.
736 */
737 memcpy(header->snap_names, &ondisk->snaps[snap_count],
738 snap_names_len);
Alex Elder6a523252012-07-19 17:12:59 -0500739
Alex Elder621901d2012-08-23 23:22:06 -0500740 /* Record each snapshot's size */
741
Alex Elderd2bb24e2012-07-26 23:37:14 -0500742 size = snap_count * sizeof (*header->snap_sizes);
743 header->snap_sizes = kmalloc(size, GFP_KERNEL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700744 if (!header->snap_sizes)
Alex Elder6a523252012-07-19 17:12:59 -0500745 goto out_err;
Alex Elder621901d2012-08-23 23:22:06 -0500746 for (i = 0; i < snap_count; i++)
747 header->snap_sizes[i] =
748 le64_to_cpu(ondisk->snaps[i].image_size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700749 } else {
Alex Elderccece232012-07-10 20:30:10 -0500750 WARN_ON(ondisk->snap_names_len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700751 header->snap_names = NULL;
752 header->snap_sizes = NULL;
753 }
Alex Elder849b4262012-07-09 21:04:24 -0500754
Alex Elder34b13182012-07-13 20:35:12 -0500755 header->features = 0; /* No features support in v1 images */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700756 header->obj_order = ondisk->options.order;
757 header->crypt_type = ondisk->options.crypt_type;
758 header->comp_type = ondisk->options.comp_type;
Alex Elder6a523252012-07-19 17:12:59 -0500759
Alex Elder621901d2012-08-23 23:22:06 -0500760 /* Allocate and fill in the snapshot context */
761
Alex Elderf84344f2012-08-31 17:29:51 -0500762 header->image_size = le64_to_cpu(ondisk->image_size);
Alex Elder6a523252012-07-19 17:12:59 -0500763 size = sizeof (struct ceph_snap_context);
764 size += snap_count * sizeof (header->snapc->snaps[0]);
765 header->snapc = kzalloc(size, GFP_KERNEL);
766 if (!header->snapc)
767 goto out_err;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700768
769 atomic_set(&header->snapc->nref, 1);
Alex Elder505cbb92012-07-19 08:49:18 -0500770 header->snapc->seq = le64_to_cpu(ondisk->snap_seq);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700771 header->snapc->num_snaps = snap_count;
Alex Elder621901d2012-08-23 23:22:06 -0500772 for (i = 0; i < snap_count; i++)
773 header->snapc->snaps[i] =
774 le64_to_cpu(ondisk->snaps[i].id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700775
776 return 0;
777
Alex Elder6a523252012-07-19 17:12:59 -0500778out_err:
Alex Elder849b4262012-07-09 21:04:24 -0500779 kfree(header->snap_sizes);
Alex Elderccece232012-07-10 20:30:10 -0500780 header->snap_sizes = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700781 kfree(header->snap_names);
Alex Elderccece232012-07-10 20:30:10 -0500782 header->snap_names = NULL;
Alex Elder6a523252012-07-19 17:12:59 -0500783 kfree(header->object_prefix);
784 header->object_prefix = NULL;
Alex Elderccece232012-07-10 20:30:10 -0500785
Alex Elder00f1f362012-02-07 12:03:36 -0600786 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700787}
788
Alex Elder9e15b772012-10-30 19:40:33 -0500789static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id)
790{
791 struct rbd_snap *snap;
792
793 if (snap_id == CEPH_NOSNAP)
794 return RBD_SNAP_HEAD_NAME;
795
796 list_for_each_entry(snap, &rbd_dev->snaps, node)
797 if (snap_id == snap->id)
798 return snap->name;
799
800 return NULL;
801}
802
Alex Elder8836b992012-08-30 14:42:15 -0500803static int snap_by_name(struct rbd_device *rbd_dev, const char *snap_name)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700804{
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700805
Alex Eldere86924a2012-07-10 20:30:11 -0500806 struct rbd_snap *snap;
Alex Elder00f1f362012-02-07 12:03:36 -0600807
Alex Eldere86924a2012-07-10 20:30:11 -0500808 list_for_each_entry(snap, &rbd_dev->snaps, node) {
809 if (!strcmp(snap_name, snap->name)) {
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500810 rbd_dev->spec->snap_id = snap->id;
Alex Eldere86924a2012-07-10 20:30:11 -0500811 rbd_dev->mapping.size = snap->size;
Alex Elder34b13182012-07-13 20:35:12 -0500812 rbd_dev->mapping.features = snap->features;
Alex Elder00f1f362012-02-07 12:03:36 -0600813
Alex Eldere86924a2012-07-10 20:30:11 -0500814 return 0;
Alex Elder00f1f362012-02-07 12:03:36 -0600815 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700816 }
Alex Eldere86924a2012-07-10 20:30:11 -0500817
Alex Elder00f1f362012-02-07 12:03:36 -0600818 return -ENOENT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700819}
820
Alex Elder819d52b2012-10-25 23:34:41 -0500821static int rbd_dev_set_mapping(struct rbd_device *rbd_dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700822{
Alex Elder78dc4472012-07-19 08:49:18 -0500823 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700824
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500825 if (!memcmp(rbd_dev->spec->snap_name, RBD_SNAP_HEAD_NAME,
Josh Durgincc9d7342011-11-21 18:19:13 -0800826 sizeof (RBD_SNAP_HEAD_NAME))) {
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500827 rbd_dev->spec->snap_id = CEPH_NOSNAP;
Alex Elder99c1f082012-08-30 14:42:15 -0500828 rbd_dev->mapping.size = rbd_dev->header.image_size;
Alex Elder34b13182012-07-13 20:35:12 -0500829 rbd_dev->mapping.features = rbd_dev->header.features;
Alex Eldere86924a2012-07-10 20:30:11 -0500830 ret = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700831 } else {
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500832 ret = snap_by_name(rbd_dev, rbd_dev->spec->snap_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700833 if (ret < 0)
834 goto done;
Alex Elderf84344f2012-08-31 17:29:51 -0500835 rbd_dev->mapping.read_only = true;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700836 }
Alex Elder6d292902013-01-14 12:43:31 -0600837 set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
838
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700839done:
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700840 return ret;
841}
842
843static void rbd_header_free(struct rbd_image_header *header)
844{
Alex Elder849b4262012-07-09 21:04:24 -0500845 kfree(header->object_prefix);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500846 header->object_prefix = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700847 kfree(header->snap_sizes);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500848 header->snap_sizes = NULL;
Alex Elder849b4262012-07-09 21:04:24 -0500849 kfree(header->snap_names);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500850 header->snap_names = NULL;
Josh Durgind1d25642011-12-05 14:03:05 -0800851 ceph_put_snap_context(header->snapc);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500852 header->snapc = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700853}
854
Alex Elder98571b52013-01-20 14:44:42 -0600855static const char *rbd_segment_name(struct rbd_device *rbd_dev, u64 offset)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700856{
Alex Elder65ccfe22012-08-09 10:33:26 -0700857 char *name;
858 u64 segment;
859 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700860
Alex Elder2fd82b92012-11-09 15:05:54 -0600861 name = kmalloc(MAX_OBJ_NAME_SIZE + 1, GFP_NOIO);
Alex Elder65ccfe22012-08-09 10:33:26 -0700862 if (!name)
863 return NULL;
864 segment = offset >> rbd_dev->header.obj_order;
Alex Elder2fd82b92012-11-09 15:05:54 -0600865 ret = snprintf(name, MAX_OBJ_NAME_SIZE + 1, "%s.%012llx",
Alex Elder65ccfe22012-08-09 10:33:26 -0700866 rbd_dev->header.object_prefix, segment);
Alex Elder2fd82b92012-11-09 15:05:54 -0600867 if (ret < 0 || ret > MAX_OBJ_NAME_SIZE) {
Alex Elder65ccfe22012-08-09 10:33:26 -0700868 pr_err("error formatting segment name for #%llu (%d)\n",
869 segment, ret);
870 kfree(name);
871 name = NULL;
872 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700873
Alex Elder65ccfe22012-08-09 10:33:26 -0700874 return name;
875}
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700876
Alex Elder65ccfe22012-08-09 10:33:26 -0700877static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset)
878{
879 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700880
Alex Elder65ccfe22012-08-09 10:33:26 -0700881 return offset & (segment_size - 1);
882}
883
884static u64 rbd_segment_length(struct rbd_device *rbd_dev,
885 u64 offset, u64 length)
886{
887 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
888
889 offset &= segment_size - 1;
890
Alex Elderaafb2302012-09-06 16:00:54 -0500891 rbd_assert(length <= U64_MAX - offset);
Alex Elder65ccfe22012-08-09 10:33:26 -0700892 if (offset + length > segment_size)
893 length = segment_size - offset;
894
895 return length;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700896}
897
898/*
Josh Durgin029bcbd2011-07-22 11:35:23 -0700899 * returns the size of an object in the image
900 */
901static u64 rbd_obj_bytes(struct rbd_image_header *header)
902{
903 return 1 << header->obj_order;
904}
905
906/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700907 * bio helpers
908 */
909
910static void bio_chain_put(struct bio *chain)
911{
912 struct bio *tmp;
913
914 while (chain) {
915 tmp = chain;
916 chain = chain->bi_next;
917 bio_put(tmp);
918 }
919}
920
921/*
922 * zeros a bio chain, starting at specific offset
923 */
924static void zero_bio_chain(struct bio *chain, int start_ofs)
925{
926 struct bio_vec *bv;
927 unsigned long flags;
928 void *buf;
929 int i;
930 int pos = 0;
931
932 while (chain) {
933 bio_for_each_segment(bv, chain, i) {
934 if (pos + bv->bv_len > start_ofs) {
935 int remainder = max(start_ofs - pos, 0);
936 buf = bvec_kmap_irq(bv, &flags);
937 memset(buf + remainder, 0,
938 bv->bv_len - remainder);
Dan Carpenter85b5aaa2010-10-11 21:15:11 +0200939 bvec_kunmap_irq(buf, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700940 }
941 pos += bv->bv_len;
942 }
943
944 chain = chain->bi_next;
945 }
946}
947
948/*
Alex Elderf7760da2012-10-20 22:17:27 -0500949 * Clone a portion of a bio, starting at the given byte offset
950 * and continuing for the number of bytes indicated.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700951 */
Alex Elderf7760da2012-10-20 22:17:27 -0500952static struct bio *bio_clone_range(struct bio *bio_src,
953 unsigned int offset,
954 unsigned int len,
955 gfp_t gfpmask)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700956{
Alex Elderf7760da2012-10-20 22:17:27 -0500957 struct bio_vec *bv;
958 unsigned int resid;
959 unsigned short idx;
960 unsigned int voff;
961 unsigned short end_idx;
962 unsigned short vcnt;
963 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700964
Alex Elderf7760da2012-10-20 22:17:27 -0500965 /* Handle the easy case for the caller */
966
967 if (!offset && len == bio_src->bi_size)
968 return bio_clone(bio_src, gfpmask);
969
970 if (WARN_ON_ONCE(!len))
971 return NULL;
972 if (WARN_ON_ONCE(len > bio_src->bi_size))
973 return NULL;
974 if (WARN_ON_ONCE(offset > bio_src->bi_size - len))
975 return NULL;
976
977 /* Find first affected segment... */
978
979 resid = offset;
980 __bio_for_each_segment(bv, bio_src, idx, 0) {
981 if (resid < bv->bv_len)
982 break;
983 resid -= bv->bv_len;
984 }
985 voff = resid;
986
987 /* ...and the last affected segment */
988
989 resid += len;
990 __bio_for_each_segment(bv, bio_src, end_idx, idx) {
991 if (resid <= bv->bv_len)
992 break;
993 resid -= bv->bv_len;
994 }
995 vcnt = end_idx - idx + 1;
996
997 /* Build the clone */
998
999 bio = bio_alloc(gfpmask, (unsigned int) vcnt);
1000 if (!bio)
1001 return NULL; /* ENOMEM */
1002
1003 bio->bi_bdev = bio_src->bi_bdev;
1004 bio->bi_sector = bio_src->bi_sector + (offset >> SECTOR_SHIFT);
1005 bio->bi_rw = bio_src->bi_rw;
1006 bio->bi_flags |= 1 << BIO_CLONED;
1007
1008 /*
1009 * Copy over our part of the bio_vec, then update the first
1010 * and last (or only) entries.
1011 */
1012 memcpy(&bio->bi_io_vec[0], &bio_src->bi_io_vec[idx],
1013 vcnt * sizeof (struct bio_vec));
1014 bio->bi_io_vec[0].bv_offset += voff;
1015 if (vcnt > 1) {
1016 bio->bi_io_vec[0].bv_len -= voff;
1017 bio->bi_io_vec[vcnt - 1].bv_len = resid;
1018 } else {
1019 bio->bi_io_vec[0].bv_len = len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001020 }
1021
Alex Elderf7760da2012-10-20 22:17:27 -05001022 bio->bi_vcnt = vcnt;
1023 bio->bi_size = len;
1024 bio->bi_idx = 0;
Alex Elder542582f2012-08-09 10:33:25 -07001025
Alex Elderf7760da2012-10-20 22:17:27 -05001026 return bio;
1027}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001028
Alex Elderf7760da2012-10-20 22:17:27 -05001029/*
1030 * Clone a portion of a bio chain, starting at the given byte offset
1031 * into the first bio in the source chain and continuing for the
1032 * number of bytes indicated. The result is another bio chain of
1033 * exactly the given length, or a null pointer on error.
1034 *
1035 * The bio_src and offset parameters are both in-out. On entry they
1036 * refer to the first source bio and the offset into that bio where
1037 * the start of data to be cloned is located.
1038 *
1039 * On return, bio_src is updated to refer to the bio in the source
1040 * chain that contains first un-cloned byte, and *offset will
1041 * contain the offset of that byte within that bio.
1042 */
1043static struct bio *bio_chain_clone_range(struct bio **bio_src,
1044 unsigned int *offset,
1045 unsigned int len,
1046 gfp_t gfpmask)
1047{
1048 struct bio *bi = *bio_src;
1049 unsigned int off = *offset;
1050 struct bio *chain = NULL;
1051 struct bio **end;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001052
Alex Elderf7760da2012-10-20 22:17:27 -05001053 /* Build up a chain of clone bios up to the limit */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001054
Alex Elderf7760da2012-10-20 22:17:27 -05001055 if (!bi || off >= bi->bi_size || !len)
1056 return NULL; /* Nothing to clone */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001057
Alex Elderf7760da2012-10-20 22:17:27 -05001058 end = &chain;
1059 while (len) {
1060 unsigned int bi_size;
1061 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001062
Alex Elderf5400b72012-11-01 10:17:15 -05001063 if (!bi) {
1064 rbd_warn(NULL, "bio_chain exhausted with %u left", len);
Alex Elderf7760da2012-10-20 22:17:27 -05001065 goto out_err; /* EINVAL; ran out of bio's */
Alex Elderf5400b72012-11-01 10:17:15 -05001066 }
Alex Elderf7760da2012-10-20 22:17:27 -05001067 bi_size = min_t(unsigned int, bi->bi_size - off, len);
1068 bio = bio_clone_range(bi, off, bi_size, gfpmask);
1069 if (!bio)
1070 goto out_err; /* ENOMEM */
1071
1072 *end = bio;
1073 end = &bio->bi_next;
1074
1075 off += bi_size;
1076 if (off == bi->bi_size) {
1077 bi = bi->bi_next;
1078 off = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001079 }
Alex Elderf7760da2012-10-20 22:17:27 -05001080 len -= bi_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001081 }
Alex Elderf7760da2012-10-20 22:17:27 -05001082 *bio_src = bi;
1083 *offset = off;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001084
Alex Elderf7760da2012-10-20 22:17:27 -05001085 return chain;
1086out_err:
1087 bio_chain_put(chain);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001088
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001089 return NULL;
1090}
1091
Alex Elder926f9b32013-02-11 12:33:24 -06001092/*
1093 * The default/initial value for all object request flags is 0. For
1094 * each flag, once its value is set to 1 it is never reset to 0
1095 * again.
1096 */
1097static void obj_request_done_set(struct rbd_obj_request *obj_request)
1098{
1099 if (test_and_set_bit(OBJ_REQ_DONE, &obj_request->flags)) {
1100 struct rbd_img_request *img_request = obj_request->img_request;
1101 struct rbd_device *rbd_dev;
1102
1103 rbd_dev = img_request ? img_request->rbd_dev : NULL;
1104 rbd_warn(rbd_dev, "obj_request %p already marked done\n",
1105 obj_request);
1106 }
1107}
1108
1109static bool obj_request_done_test(struct rbd_obj_request *obj_request)
1110{
1111 smp_mb();
1112 return test_bit(OBJ_REQ_DONE, &obj_request->flags) != 0;
1113}
1114
Alex Elder6365d332013-02-11 12:33:24 -06001115static void obj_request_img_data_set(struct rbd_obj_request *obj_request)
1116{
1117 if (test_and_set_bit(OBJ_REQ_IMG_DATA, &obj_request->flags)) {
1118 struct rbd_img_request *img_request = obj_request->img_request;
1119 struct rbd_device *rbd_dev;
1120
1121 rbd_dev = img_request ? img_request->rbd_dev : NULL;
1122 rbd_warn(rbd_dev, "obj_request %p already marked img_data\n",
1123 obj_request);
1124 }
1125}
1126
1127static bool obj_request_img_data_test(struct rbd_obj_request *obj_request)
1128{
1129 smp_mb();
1130 return test_bit(OBJ_REQ_IMG_DATA, &obj_request->flags) != 0;
1131}
1132
Alex Elderbf0d5f502012-11-22 00:00:08 -06001133static void rbd_obj_request_get(struct rbd_obj_request *obj_request)
1134{
Alex Elder37206ee2013-02-20 17:32:08 -06001135 dout("%s: obj %p (was %d)\n", __func__, obj_request,
1136 atomic_read(&obj_request->kref.refcount));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001137 kref_get(&obj_request->kref);
1138}
1139
1140static void rbd_obj_request_destroy(struct kref *kref);
1141static void rbd_obj_request_put(struct rbd_obj_request *obj_request)
1142{
1143 rbd_assert(obj_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001144 dout("%s: obj %p (was %d)\n", __func__, obj_request,
1145 atomic_read(&obj_request->kref.refcount));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001146 kref_put(&obj_request->kref, rbd_obj_request_destroy);
1147}
1148
1149static void rbd_img_request_get(struct rbd_img_request *img_request)
1150{
Alex Elder37206ee2013-02-20 17:32:08 -06001151 dout("%s: img %p (was %d)\n", __func__, img_request,
1152 atomic_read(&img_request->kref.refcount));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001153 kref_get(&img_request->kref);
1154}
1155
1156static void rbd_img_request_destroy(struct kref *kref);
1157static void rbd_img_request_put(struct rbd_img_request *img_request)
1158{
1159 rbd_assert(img_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001160 dout("%s: img %p (was %d)\n", __func__, img_request,
1161 atomic_read(&img_request->kref.refcount));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001162 kref_put(&img_request->kref, rbd_img_request_destroy);
1163}
1164
1165static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request,
1166 struct rbd_obj_request *obj_request)
1167{
Alex Elder25dcf952013-01-25 17:08:55 -06001168 rbd_assert(obj_request->img_request == NULL);
1169
Alex Elderbf0d5f502012-11-22 00:00:08 -06001170 rbd_obj_request_get(obj_request);
1171 obj_request->img_request = img_request;
Alex Elder25dcf952013-01-25 17:08:55 -06001172 obj_request->which = img_request->obj_request_count;
Alex Elder6365d332013-02-11 12:33:24 -06001173 rbd_assert(!obj_request_img_data_test(obj_request));
1174 obj_request_img_data_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001175 rbd_assert(obj_request->which != BAD_WHICH);
Alex Elder25dcf952013-01-25 17:08:55 -06001176 img_request->obj_request_count++;
1177 list_add_tail(&obj_request->links, &img_request->obj_requests);
Alex Elder37206ee2013-02-20 17:32:08 -06001178 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1179 obj_request->which);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001180}
1181
1182static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request,
1183 struct rbd_obj_request *obj_request)
1184{
1185 rbd_assert(obj_request->which != BAD_WHICH);
Alex Elder25dcf952013-01-25 17:08:55 -06001186
Alex Elder37206ee2013-02-20 17:32:08 -06001187 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1188 obj_request->which);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001189 list_del(&obj_request->links);
Alex Elder25dcf952013-01-25 17:08:55 -06001190 rbd_assert(img_request->obj_request_count > 0);
1191 img_request->obj_request_count--;
1192 rbd_assert(obj_request->which == img_request->obj_request_count);
1193 obj_request->which = BAD_WHICH;
Alex Elder6365d332013-02-11 12:33:24 -06001194 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001195 rbd_assert(obj_request->img_request == img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001196 obj_request->img_request = NULL;
Alex Elder25dcf952013-01-25 17:08:55 -06001197 obj_request->callback = NULL;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001198 rbd_obj_request_put(obj_request);
1199}
1200
1201static bool obj_request_type_valid(enum obj_request_type type)
1202{
1203 switch (type) {
Alex Elder9969ebc2013-01-18 12:31:10 -06001204 case OBJ_REQUEST_NODATA:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001205 case OBJ_REQUEST_BIO:
Alex Elder788e2df2013-01-17 12:25:27 -06001206 case OBJ_REQUEST_PAGES:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001207 return true;
1208 default:
1209 return false;
1210 }
1211}
1212
Alex Elderbf0d5f502012-11-22 00:00:08 -06001213static int rbd_obj_request_submit(struct ceph_osd_client *osdc,
1214 struct rbd_obj_request *obj_request)
1215{
Alex Elder37206ee2013-02-20 17:32:08 -06001216 dout("%s: osdc %p obj %p\n", __func__, osdc, obj_request);
1217
Alex Elderbf0d5f502012-11-22 00:00:08 -06001218 return ceph_osdc_start_request(osdc, obj_request->osd_req, false);
1219}
1220
1221static void rbd_img_request_complete(struct rbd_img_request *img_request)
1222{
Alex Elder55f27e02013-04-10 12:34:25 -05001223
Alex Elder37206ee2013-02-20 17:32:08 -06001224 dout("%s: img %p\n", __func__, img_request);
Alex Elder55f27e02013-04-10 12:34:25 -05001225
1226 /*
1227 * If no error occurred, compute the aggregate transfer
1228 * count for the image request. We could instead use
1229 * atomic64_cmpxchg() to update it as each object request
1230 * completes; not clear which way is better off hand.
1231 */
1232 if (!img_request->result) {
1233 struct rbd_obj_request *obj_request;
1234 u64 xferred = 0;
1235
1236 for_each_obj_request(img_request, obj_request)
1237 xferred += obj_request->xferred;
1238 img_request->xferred = xferred;
1239 }
1240
Alex Elderbf0d5f502012-11-22 00:00:08 -06001241 if (img_request->callback)
1242 img_request->callback(img_request);
1243 else
1244 rbd_img_request_put(img_request);
1245}
1246
Alex Elder788e2df2013-01-17 12:25:27 -06001247/* Caller is responsible for rbd_obj_request_destroy(obj_request) */
1248
1249static int rbd_obj_request_wait(struct rbd_obj_request *obj_request)
1250{
Alex Elder37206ee2013-02-20 17:32:08 -06001251 dout("%s: obj %p\n", __func__, obj_request);
1252
Alex Elder788e2df2013-01-17 12:25:27 -06001253 return wait_for_completion_interruptible(&obj_request->completion);
1254}
1255
Alex Elder0c425242013-02-08 09:55:49 -06001256/*
1257 * The default/initial value for all image request flags is 0. Each
1258 * is conditionally set to 1 at image request initialization time
1259 * and currently never change thereafter.
1260 */
1261static void img_request_write_set(struct rbd_img_request *img_request)
1262{
1263 set_bit(IMG_REQ_WRITE, &img_request->flags);
1264 smp_mb();
1265}
1266
1267static bool img_request_write_test(struct rbd_img_request *img_request)
1268{
1269 smp_mb();
1270 return test_bit(IMG_REQ_WRITE, &img_request->flags) != 0;
1271}
1272
Alex Elder9849e982013-01-24 16:13:36 -06001273static void img_request_child_set(struct rbd_img_request *img_request)
1274{
1275 set_bit(IMG_REQ_CHILD, &img_request->flags);
1276 smp_mb();
1277}
1278
1279static bool img_request_child_test(struct rbd_img_request *img_request)
1280{
1281 smp_mb();
1282 return test_bit(IMG_REQ_CHILD, &img_request->flags) != 0;
1283}
1284
Alex Elderd0b2e942013-01-24 16:13:36 -06001285static void img_request_layered_set(struct rbd_img_request *img_request)
1286{
1287 set_bit(IMG_REQ_LAYERED, &img_request->flags);
1288 smp_mb();
1289}
1290
1291static bool img_request_layered_test(struct rbd_img_request *img_request)
1292{
1293 smp_mb();
1294 return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0;
1295}
1296
Alex Elder6e2a4502013-03-27 09:16:30 -05001297static void
1298rbd_img_obj_request_read_callback(struct rbd_obj_request *obj_request)
1299{
1300 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1301 obj_request, obj_request->img_request, obj_request->result,
1302 obj_request->xferred, obj_request->length);
1303 /*
1304 * ENOENT means a hole in the image. We zero-fill the
1305 * entire length of the request. A short read also implies
1306 * zero-fill to the end of the request. Either way we
1307 * update the xferred count to indicate the whole request
1308 * was satisfied.
1309 */
1310 BUG_ON(obj_request->type != OBJ_REQUEST_BIO);
1311 if (obj_request->result == -ENOENT) {
1312 zero_bio_chain(obj_request->bio_list, 0);
1313 obj_request->result = 0;
1314 obj_request->xferred = obj_request->length;
1315 } else if (obj_request->xferred < obj_request->length &&
1316 !obj_request->result) {
1317 zero_bio_chain(obj_request->bio_list, obj_request->xferred);
1318 obj_request->xferred = obj_request->length;
1319 }
1320 obj_request_done_set(obj_request);
1321}
1322
Alex Elderbf0d5f502012-11-22 00:00:08 -06001323static void rbd_obj_request_complete(struct rbd_obj_request *obj_request)
1324{
Alex Elder37206ee2013-02-20 17:32:08 -06001325 dout("%s: obj %p cb %p\n", __func__, obj_request,
1326 obj_request->callback);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001327 if (obj_request->callback)
1328 obj_request->callback(obj_request);
Alex Elder788e2df2013-01-17 12:25:27 -06001329 else
1330 complete_all(&obj_request->completion);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001331}
1332
Alex Elderc47f9372013-02-26 14:23:07 -06001333static void rbd_osd_trivial_callback(struct rbd_obj_request *obj_request)
Alex Elder39bf2c52013-02-26 14:23:07 -06001334{
1335 dout("%s: obj %p\n", __func__, obj_request);
1336 obj_request_done_set(obj_request);
1337}
1338
Alex Elderc47f9372013-02-26 14:23:07 -06001339static void rbd_osd_read_callback(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001340{
Alex Elder8b3e1a52013-01-24 16:13:36 -06001341 struct rbd_img_request *img_request = obj_request->img_request;
1342 bool layered = img_request && img_request_layered_test(img_request);
1343
1344 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1345 obj_request, img_request, obj_request->result,
1346 obj_request->xferred, obj_request->length);
1347 if (layered && obj_request->result == -ENOENT)
1348 rbd_img_parent_read(obj_request);
1349 else if (img_request)
Alex Elder6e2a4502013-03-27 09:16:30 -05001350 rbd_img_obj_request_read_callback(obj_request);
1351 else
1352 obj_request_done_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001353}
1354
Alex Elderc47f9372013-02-26 14:23:07 -06001355static void rbd_osd_write_callback(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001356{
Sage Weil1b83bef2013-02-25 16:11:12 -08001357 dout("%s: obj %p result %d %llu\n", __func__, obj_request,
1358 obj_request->result, obj_request->length);
1359 /*
Alex Elder8b3e1a52013-01-24 16:13:36 -06001360 * There is no such thing as a successful short write. Set
1361 * it to our originally-requested length.
Sage Weil1b83bef2013-02-25 16:11:12 -08001362 */
1363 obj_request->xferred = obj_request->length;
Alex Elder07741302013-02-05 23:41:50 -06001364 obj_request_done_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001365}
1366
Alex Elderfbfab532013-02-08 09:55:48 -06001367/*
1368 * For a simple stat call there's nothing to do. We'll do more if
1369 * this is part of a write sequence for a layered image.
1370 */
Alex Elderc47f9372013-02-26 14:23:07 -06001371static void rbd_osd_stat_callback(struct rbd_obj_request *obj_request)
Alex Elderfbfab532013-02-08 09:55:48 -06001372{
Alex Elder37206ee2013-02-20 17:32:08 -06001373 dout("%s: obj %p\n", __func__, obj_request);
Alex Elderfbfab532013-02-08 09:55:48 -06001374 obj_request_done_set(obj_request);
1375}
1376
Alex Elderbf0d5f502012-11-22 00:00:08 -06001377static void rbd_osd_req_callback(struct ceph_osd_request *osd_req,
1378 struct ceph_msg *msg)
1379{
1380 struct rbd_obj_request *obj_request = osd_req->r_priv;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001381 u16 opcode;
1382
Alex Elder37206ee2013-02-20 17:32:08 -06001383 dout("%s: osd_req %p msg %p\n", __func__, osd_req, msg);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001384 rbd_assert(osd_req == obj_request->osd_req);
Alex Elder6365d332013-02-11 12:33:24 -06001385 rbd_assert(obj_request_img_data_test(obj_request) ^
1386 !obj_request->img_request);
1387 rbd_assert(obj_request_img_data_test(obj_request) ^
Alex Elderbf0d5f502012-11-22 00:00:08 -06001388 (obj_request->which == BAD_WHICH));
1389
Sage Weil1b83bef2013-02-25 16:11:12 -08001390 if (osd_req->r_result < 0)
1391 obj_request->result = osd_req->r_result;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001392 obj_request->version = le64_to_cpu(osd_req->r_reassert_version.version);
1393
Sage Weil1b83bef2013-02-25 16:11:12 -08001394 WARN_ON(osd_req->r_num_ops != 1); /* For now */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001395
Alex Elderc47f9372013-02-26 14:23:07 -06001396 /*
1397 * We support a 64-bit length, but ultimately it has to be
1398 * passed to blk_end_request(), which takes an unsigned int.
1399 */
Sage Weil1b83bef2013-02-25 16:11:12 -08001400 obj_request->xferred = osd_req->r_reply_op_len[0];
Alex Elder8b3e1a52013-01-24 16:13:36 -06001401 rbd_assert(obj_request->xferred < (u64)UINT_MAX);
Alex Elder79528732013-04-03 21:32:51 -05001402 opcode = osd_req->r_ops[0].op;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001403 switch (opcode) {
1404 case CEPH_OSD_OP_READ:
Alex Elderc47f9372013-02-26 14:23:07 -06001405 rbd_osd_read_callback(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001406 break;
1407 case CEPH_OSD_OP_WRITE:
Alex Elderc47f9372013-02-26 14:23:07 -06001408 rbd_osd_write_callback(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001409 break;
Alex Elderfbfab532013-02-08 09:55:48 -06001410 case CEPH_OSD_OP_STAT:
Alex Elderc47f9372013-02-26 14:23:07 -06001411 rbd_osd_stat_callback(obj_request);
Alex Elderfbfab532013-02-08 09:55:48 -06001412 break;
Alex Elder36be9a72013-01-19 00:30:28 -06001413 case CEPH_OSD_OP_CALL:
Alex Elderb8d70032012-11-30 17:53:04 -06001414 case CEPH_OSD_OP_NOTIFY_ACK:
Alex Elder9969ebc2013-01-18 12:31:10 -06001415 case CEPH_OSD_OP_WATCH:
Alex Elderc47f9372013-02-26 14:23:07 -06001416 rbd_osd_trivial_callback(obj_request);
Alex Elder9969ebc2013-01-18 12:31:10 -06001417 break;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001418 default:
1419 rbd_warn(NULL, "%s: unsupported op %hu\n",
1420 obj_request->object_name, (unsigned short) opcode);
1421 break;
1422 }
1423
Alex Elder07741302013-02-05 23:41:50 -06001424 if (obj_request_done_test(obj_request))
Alex Elderbf0d5f502012-11-22 00:00:08 -06001425 rbd_obj_request_complete(obj_request);
1426}
1427
Alex Elder2fa12322013-04-05 01:27:12 -05001428static void rbd_osd_req_format(struct rbd_obj_request *obj_request,
Alex Elder79528732013-04-03 21:32:51 -05001429 bool write_request)
Alex Elder430c28c2013-04-03 21:32:51 -05001430{
1431 struct rbd_img_request *img_request = obj_request->img_request;
Alex Elder8c042b02013-04-03 01:28:58 -05001432 struct ceph_osd_request *osd_req = obj_request->osd_req;
Alex Elder430c28c2013-04-03 21:32:51 -05001433 struct ceph_snap_context *snapc = NULL;
1434 u64 snap_id = CEPH_NOSNAP;
1435 struct timespec *mtime = NULL;
1436 struct timespec now;
1437
Alex Elder8c042b02013-04-03 01:28:58 -05001438 rbd_assert(osd_req != NULL);
Alex Elder430c28c2013-04-03 21:32:51 -05001439
1440 if (write_request) {
1441 now = CURRENT_TIME;
1442 mtime = &now;
1443 if (img_request)
1444 snapc = img_request->snapc;
Alex Elder2fa12322013-04-05 01:27:12 -05001445 } else if (img_request) {
1446 snap_id = img_request->snap_id;
Alex Elder8c042b02013-04-03 01:28:58 -05001447 }
1448 ceph_osdc_build_request(osd_req, obj_request->offset,
Alex Elder79528732013-04-03 21:32:51 -05001449 snapc, snap_id, mtime);
Alex Elder430c28c2013-04-03 21:32:51 -05001450}
1451
Alex Elderbf0d5f502012-11-22 00:00:08 -06001452static struct ceph_osd_request *rbd_osd_req_create(
1453 struct rbd_device *rbd_dev,
1454 bool write_request,
Alex Elder430c28c2013-04-03 21:32:51 -05001455 struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001456{
Alex Elderbf0d5f502012-11-22 00:00:08 -06001457 struct ceph_snap_context *snapc = NULL;
1458 struct ceph_osd_client *osdc;
1459 struct ceph_osd_request *osd_req;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001460
Alex Elder6365d332013-02-11 12:33:24 -06001461 if (obj_request_img_data_test(obj_request)) {
1462 struct rbd_img_request *img_request = obj_request->img_request;
1463
Alex Elder0c425242013-02-08 09:55:49 -06001464 rbd_assert(write_request ==
1465 img_request_write_test(img_request));
1466 if (write_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001467 snapc = img_request->snapc;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001468 }
1469
1470 /* Allocate and initialize the request, for the single op */
1471
1472 osdc = &rbd_dev->rbd_client->client->osdc;
1473 osd_req = ceph_osdc_alloc_request(osdc, snapc, 1, false, GFP_ATOMIC);
1474 if (!osd_req)
1475 return NULL; /* ENOMEM */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001476
Alex Elder430c28c2013-04-03 21:32:51 -05001477 if (write_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001478 osd_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK;
Alex Elder430c28c2013-04-03 21:32:51 -05001479 else
Alex Elderbf0d5f502012-11-22 00:00:08 -06001480 osd_req->r_flags = CEPH_OSD_FLAG_READ;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001481
1482 osd_req->r_callback = rbd_osd_req_callback;
1483 osd_req->r_priv = obj_request;
1484
1485 osd_req->r_oid_len = strlen(obj_request->object_name);
1486 rbd_assert(osd_req->r_oid_len < sizeof (osd_req->r_oid));
1487 memcpy(osd_req->r_oid, obj_request->object_name, osd_req->r_oid_len);
1488
1489 osd_req->r_file_layout = rbd_dev->layout; /* struct */
1490
Alex Elderbf0d5f502012-11-22 00:00:08 -06001491 return osd_req;
1492}
1493
1494static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req)
1495{
1496 ceph_osdc_put_request(osd_req);
1497}
1498
1499/* object_name is assumed to be a non-null pointer and NUL-terminated */
1500
1501static struct rbd_obj_request *rbd_obj_request_create(const char *object_name,
1502 u64 offset, u64 length,
1503 enum obj_request_type type)
1504{
1505 struct rbd_obj_request *obj_request;
1506 size_t size;
1507 char *name;
1508
1509 rbd_assert(obj_request_type_valid(type));
1510
1511 size = strlen(object_name) + 1;
1512 obj_request = kzalloc(sizeof (*obj_request) + size, GFP_KERNEL);
1513 if (!obj_request)
1514 return NULL;
1515
1516 name = (char *)(obj_request + 1);
1517 obj_request->object_name = memcpy(name, object_name, size);
1518 obj_request->offset = offset;
1519 obj_request->length = length;
Alex Elder926f9b32013-02-11 12:33:24 -06001520 obj_request->flags = 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001521 obj_request->which = BAD_WHICH;
1522 obj_request->type = type;
1523 INIT_LIST_HEAD(&obj_request->links);
Alex Elder788e2df2013-01-17 12:25:27 -06001524 init_completion(&obj_request->completion);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001525 kref_init(&obj_request->kref);
1526
Alex Elder37206ee2013-02-20 17:32:08 -06001527 dout("%s: \"%s\" %llu/%llu %d -> obj %p\n", __func__, object_name,
1528 offset, length, (int)type, obj_request);
1529
Alex Elderbf0d5f502012-11-22 00:00:08 -06001530 return obj_request;
1531}
1532
1533static void rbd_obj_request_destroy(struct kref *kref)
1534{
1535 struct rbd_obj_request *obj_request;
1536
1537 obj_request = container_of(kref, struct rbd_obj_request, kref);
1538
Alex Elder37206ee2013-02-20 17:32:08 -06001539 dout("%s: obj %p\n", __func__, obj_request);
1540
Alex Elderbf0d5f502012-11-22 00:00:08 -06001541 rbd_assert(obj_request->img_request == NULL);
1542 rbd_assert(obj_request->which == BAD_WHICH);
1543
1544 if (obj_request->osd_req)
1545 rbd_osd_req_destroy(obj_request->osd_req);
1546
1547 rbd_assert(obj_request_type_valid(obj_request->type));
1548 switch (obj_request->type) {
Alex Elder9969ebc2013-01-18 12:31:10 -06001549 case OBJ_REQUEST_NODATA:
1550 break; /* Nothing to do */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001551 case OBJ_REQUEST_BIO:
1552 if (obj_request->bio_list)
1553 bio_chain_put(obj_request->bio_list);
1554 break;
Alex Elder788e2df2013-01-17 12:25:27 -06001555 case OBJ_REQUEST_PAGES:
1556 if (obj_request->pages)
1557 ceph_release_page_vector(obj_request->pages,
1558 obj_request->page_count);
1559 break;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001560 }
1561
1562 kfree(obj_request);
1563}
1564
1565/*
1566 * Caller is responsible for filling in the list of object requests
1567 * that comprises the image request, and the Linux request pointer
1568 * (if there is one).
1569 */
Alex Eldercc344fa2013-02-19 12:25:56 -06001570static struct rbd_img_request *rbd_img_request_create(
1571 struct rbd_device *rbd_dev,
Alex Elderbf0d5f502012-11-22 00:00:08 -06001572 u64 offset, u64 length,
Alex Elder9849e982013-01-24 16:13:36 -06001573 bool write_request,
1574 bool child_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001575{
1576 struct rbd_img_request *img_request;
1577 struct ceph_snap_context *snapc = NULL;
1578
1579 img_request = kmalloc(sizeof (*img_request), GFP_ATOMIC);
1580 if (!img_request)
1581 return NULL;
1582
1583 if (write_request) {
1584 down_read(&rbd_dev->header_rwsem);
1585 snapc = ceph_get_snap_context(rbd_dev->header.snapc);
1586 up_read(&rbd_dev->header_rwsem);
1587 if (WARN_ON(!snapc)) {
1588 kfree(img_request);
1589 return NULL; /* Shouldn't happen */
1590 }
Alex Elder0c425242013-02-08 09:55:49 -06001591
Alex Elderbf0d5f502012-11-22 00:00:08 -06001592 }
1593
1594 img_request->rq = NULL;
1595 img_request->rbd_dev = rbd_dev;
1596 img_request->offset = offset;
1597 img_request->length = length;
Alex Elder0c425242013-02-08 09:55:49 -06001598 img_request->flags = 0;
1599 if (write_request) {
1600 img_request_write_set(img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001601 img_request->snapc = snapc;
Alex Elder0c425242013-02-08 09:55:49 -06001602 } else {
Alex Elderbf0d5f502012-11-22 00:00:08 -06001603 img_request->snap_id = rbd_dev->spec->snap_id;
Alex Elder0c425242013-02-08 09:55:49 -06001604 }
Alex Elder9849e982013-01-24 16:13:36 -06001605 if (child_request)
1606 img_request_child_set(img_request);
Alex Elderd0b2e942013-01-24 16:13:36 -06001607 if (rbd_dev->parent_spec)
1608 img_request_layered_set(img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001609 spin_lock_init(&img_request->completion_lock);
1610 img_request->next_completion = 0;
1611 img_request->callback = NULL;
Alex Eldera5a337d2013-01-24 16:13:36 -06001612 img_request->result = 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001613 img_request->obj_request_count = 0;
1614 INIT_LIST_HEAD(&img_request->obj_requests);
1615 kref_init(&img_request->kref);
1616
1617 rbd_img_request_get(img_request); /* Avoid a warning */
1618 rbd_img_request_put(img_request); /* TEMPORARY */
1619
Alex Elder37206ee2013-02-20 17:32:08 -06001620 dout("%s: rbd_dev %p %s %llu/%llu -> img %p\n", __func__, rbd_dev,
1621 write_request ? "write" : "read", offset, length,
1622 img_request);
1623
Alex Elderbf0d5f502012-11-22 00:00:08 -06001624 return img_request;
1625}
1626
1627static void rbd_img_request_destroy(struct kref *kref)
1628{
1629 struct rbd_img_request *img_request;
1630 struct rbd_obj_request *obj_request;
1631 struct rbd_obj_request *next_obj_request;
1632
1633 img_request = container_of(kref, struct rbd_img_request, kref);
1634
Alex Elder37206ee2013-02-20 17:32:08 -06001635 dout("%s: img %p\n", __func__, img_request);
1636
Alex Elderbf0d5f502012-11-22 00:00:08 -06001637 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
1638 rbd_img_obj_request_del(img_request, obj_request);
Alex Elder25dcf952013-01-25 17:08:55 -06001639 rbd_assert(img_request->obj_request_count == 0);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001640
Alex Elder0c425242013-02-08 09:55:49 -06001641 if (img_request_write_test(img_request))
Alex Elderbf0d5f502012-11-22 00:00:08 -06001642 ceph_put_snap_context(img_request->snapc);
1643
Alex Elder8b3e1a52013-01-24 16:13:36 -06001644 if (img_request_child_test(img_request))
1645 rbd_obj_request_put(img_request->obj_request);
1646
Alex Elderbf0d5f502012-11-22 00:00:08 -06001647 kfree(img_request);
1648}
1649
Alex Elder12178572013-02-08 09:55:49 -06001650static bool rbd_img_obj_end_request(struct rbd_obj_request *obj_request)
1651{
Alex Elder6365d332013-02-11 12:33:24 -06001652 struct rbd_img_request *img_request;
Alex Elder12178572013-02-08 09:55:49 -06001653 unsigned int xferred;
1654 int result;
Alex Elder8b3e1a52013-01-24 16:13:36 -06001655 bool more;
Alex Elder12178572013-02-08 09:55:49 -06001656
Alex Elder6365d332013-02-11 12:33:24 -06001657 rbd_assert(obj_request_img_data_test(obj_request));
1658 img_request = obj_request->img_request;
1659
Alex Elder12178572013-02-08 09:55:49 -06001660 rbd_assert(obj_request->xferred <= (u64)UINT_MAX);
1661 xferred = (unsigned int)obj_request->xferred;
1662 result = obj_request->result;
1663 if (result) {
1664 struct rbd_device *rbd_dev = img_request->rbd_dev;
1665
1666 rbd_warn(rbd_dev, "%s %llx at %llx (%llx)\n",
1667 img_request_write_test(img_request) ? "write" : "read",
1668 obj_request->length, obj_request->img_offset,
1669 obj_request->offset);
1670 rbd_warn(rbd_dev, " result %d xferred %x\n",
1671 result, xferred);
1672 if (!img_request->result)
1673 img_request->result = result;
1674 }
1675
Alex Elder8b3e1a52013-01-24 16:13:36 -06001676 if (img_request_child_test(img_request)) {
1677 rbd_assert(img_request->obj_request != NULL);
1678 more = obj_request->which < img_request->obj_request_count - 1;
1679 } else {
1680 rbd_assert(img_request->rq != NULL);
1681 more = blk_end_request(img_request->rq, result, xferred);
1682 }
1683
1684 return more;
Alex Elder12178572013-02-08 09:55:49 -06001685}
1686
Alex Elder21692382013-04-05 01:27:12 -05001687static void rbd_img_obj_callback(struct rbd_obj_request *obj_request)
1688{
1689 struct rbd_img_request *img_request;
1690 u32 which = obj_request->which;
1691 bool more = true;
1692
Alex Elder6365d332013-02-11 12:33:24 -06001693 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elder21692382013-04-05 01:27:12 -05001694 img_request = obj_request->img_request;
1695
1696 dout("%s: img %p obj %p\n", __func__, img_request, obj_request);
1697 rbd_assert(img_request != NULL);
Alex Elder21692382013-04-05 01:27:12 -05001698 rbd_assert(img_request->obj_request_count > 0);
1699 rbd_assert(which != BAD_WHICH);
1700 rbd_assert(which < img_request->obj_request_count);
1701 rbd_assert(which >= img_request->next_completion);
1702
1703 spin_lock_irq(&img_request->completion_lock);
1704 if (which != img_request->next_completion)
1705 goto out;
1706
1707 for_each_obj_request_from(img_request, obj_request) {
Alex Elder21692382013-04-05 01:27:12 -05001708 rbd_assert(more);
1709 rbd_assert(which < img_request->obj_request_count);
1710
1711 if (!obj_request_done_test(obj_request))
1712 break;
Alex Elder12178572013-02-08 09:55:49 -06001713 more = rbd_img_obj_end_request(obj_request);
Alex Elder21692382013-04-05 01:27:12 -05001714 which++;
1715 }
1716
1717 rbd_assert(more ^ (which == img_request->obj_request_count));
1718 img_request->next_completion = which;
1719out:
1720 spin_unlock_irq(&img_request->completion_lock);
1721
1722 if (!more)
1723 rbd_img_request_complete(img_request);
1724}
1725
Alex Elderbf0d5f502012-11-22 00:00:08 -06001726static int rbd_img_request_fill_bio(struct rbd_img_request *img_request,
1727 struct bio *bio_list)
1728{
1729 struct rbd_device *rbd_dev = img_request->rbd_dev;
1730 struct rbd_obj_request *obj_request = NULL;
1731 struct rbd_obj_request *next_obj_request;
Alex Elder0c425242013-02-08 09:55:49 -06001732 bool write_request = img_request_write_test(img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001733 unsigned int bio_offset;
Alex Elder7da22d22013-01-24 16:13:36 -06001734 u64 img_offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001735 u64 resid;
1736 u16 opcode;
1737
Alex Elder37206ee2013-02-20 17:32:08 -06001738 dout("%s: img %p bio %p\n", __func__, img_request, bio_list);
1739
Alex Elder430c28c2013-04-03 21:32:51 -05001740 opcode = write_request ? CEPH_OSD_OP_WRITE : CEPH_OSD_OP_READ;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001741 bio_offset = 0;
Alex Elder7da22d22013-01-24 16:13:36 -06001742 img_offset = img_request->offset;
1743 rbd_assert(img_offset == bio_list->bi_sector << SECTOR_SHIFT);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001744 resid = img_request->length;
Alex Elder4dda41d2013-02-20 21:59:33 -06001745 rbd_assert(resid > 0);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001746 while (resid) {
Alex Elder2fa12322013-04-05 01:27:12 -05001747 struct ceph_osd_request *osd_req;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001748 const char *object_name;
1749 unsigned int clone_size;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001750 u64 offset;
1751 u64 length;
1752
Alex Elder7da22d22013-01-24 16:13:36 -06001753 object_name = rbd_segment_name(rbd_dev, img_offset);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001754 if (!object_name)
1755 goto out_unwind;
Alex Elder7da22d22013-01-24 16:13:36 -06001756 offset = rbd_segment_offset(rbd_dev, img_offset);
1757 length = rbd_segment_length(rbd_dev, img_offset, resid);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001758 obj_request = rbd_obj_request_create(object_name,
1759 offset, length,
1760 OBJ_REQUEST_BIO);
1761 kfree(object_name); /* object request has its own copy */
1762 if (!obj_request)
1763 goto out_unwind;
1764
1765 rbd_assert(length <= (u64) UINT_MAX);
1766 clone_size = (unsigned int) length;
1767 obj_request->bio_list = bio_chain_clone_range(&bio_list,
1768 &bio_offset, clone_size,
1769 GFP_ATOMIC);
1770 if (!obj_request->bio_list)
1771 goto out_partial;
1772
Alex Elder2fa12322013-04-05 01:27:12 -05001773 osd_req = rbd_osd_req_create(rbd_dev, write_request,
1774 obj_request);
1775 if (!osd_req)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001776 goto out_partial;
Alex Elder2fa12322013-04-05 01:27:12 -05001777 obj_request->osd_req = osd_req;
Alex Elder21692382013-04-05 01:27:12 -05001778 obj_request->callback = rbd_img_obj_callback;
Alex Elder430c28c2013-04-03 21:32:51 -05001779
Alex Elder2fa12322013-04-05 01:27:12 -05001780 osd_req_op_extent_init(osd_req, 0, opcode, offset, length,
1781 0, 0);
Alex Eldera4ce40a2013-04-05 01:27:12 -05001782 osd_req_op_extent_osd_data_bio(osd_req, 0, write_request,
1783 obj_request->bio_list, obj_request->length);
Alex Elder2fa12322013-04-05 01:27:12 -05001784 rbd_osd_req_format(obj_request, write_request);
Alex Elder430c28c2013-04-03 21:32:51 -05001785
Alex Elder7da22d22013-01-24 16:13:36 -06001786 obj_request->img_offset = img_offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001787 rbd_img_obj_request_add(img_request, obj_request);
1788
Alex Elder7da22d22013-01-24 16:13:36 -06001789 img_offset += length;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001790 resid -= length;
1791 }
1792
1793 return 0;
1794
1795out_partial:
1796 rbd_obj_request_put(obj_request);
1797out_unwind:
1798 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
1799 rbd_obj_request_put(obj_request);
1800
1801 return -ENOMEM;
1802}
1803
Alex Elderbf0d5f502012-11-22 00:00:08 -06001804static int rbd_img_request_submit(struct rbd_img_request *img_request)
1805{
1806 struct rbd_device *rbd_dev = img_request->rbd_dev;
1807 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
1808 struct rbd_obj_request *obj_request;
Alex Elder46faeed2013-04-10 17:47:46 -05001809 struct rbd_obj_request *next_obj_request;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001810
Alex Elder37206ee2013-02-20 17:32:08 -06001811 dout("%s: img %p\n", __func__, img_request);
Alex Elder46faeed2013-04-10 17:47:46 -05001812 for_each_obj_request_safe(img_request, obj_request, next_obj_request) {
Alex Elderbf0d5f502012-11-22 00:00:08 -06001813 int ret;
1814
Alex Elderbf0d5f502012-11-22 00:00:08 -06001815 ret = rbd_obj_request_submit(osdc, obj_request);
1816 if (ret)
1817 return ret;
1818 /*
1819 * The image request has its own reference to each
1820 * of its object requests, so we can safely drop the
1821 * initial one here.
1822 */
1823 rbd_obj_request_put(obj_request);
1824 }
1825
1826 return 0;
1827}
1828
Alex Elder8b3e1a52013-01-24 16:13:36 -06001829static void rbd_img_parent_read_callback(struct rbd_img_request *img_request)
1830{
1831 struct rbd_obj_request *obj_request;
1832
1833 rbd_assert(img_request_child_test(img_request));
1834
1835 obj_request = img_request->obj_request;
1836 rbd_assert(obj_request != NULL);
1837 obj_request->result = img_request->result;
1838 obj_request->xferred = img_request->xferred;
1839
1840 rbd_img_obj_request_read_callback(obj_request);
1841 rbd_obj_request_complete(obj_request);
1842}
1843
1844static void rbd_img_parent_read(struct rbd_obj_request *obj_request)
1845{
1846 struct rbd_device *rbd_dev;
1847 struct rbd_img_request *img_request;
1848 int result;
1849
1850 rbd_assert(obj_request_img_data_test(obj_request));
1851 rbd_assert(obj_request->img_request != NULL);
1852 rbd_assert(obj_request->result == (s32) -ENOENT);
1853 rbd_assert(obj_request->type == OBJ_REQUEST_BIO);
1854
1855 rbd_dev = obj_request->img_request->rbd_dev;
1856 rbd_assert(rbd_dev->parent != NULL);
1857 /* rbd_read_finish(obj_request, obj_request->length); */
1858 img_request = rbd_img_request_create(rbd_dev->parent,
1859 obj_request->img_offset,
1860 obj_request->length,
1861 false, true);
1862 result = -ENOMEM;
1863 if (!img_request)
1864 goto out_err;
1865
1866 rbd_obj_request_get(obj_request);
1867 img_request->obj_request = obj_request;
1868
1869 result = rbd_img_request_fill_bio(img_request, obj_request->bio_list);
1870 if (result)
1871 goto out_err;
1872
1873 img_request->callback = rbd_img_parent_read_callback;
1874 result = rbd_img_request_submit(img_request);
1875 if (result)
1876 goto out_err;
1877
1878 return;
1879out_err:
1880 if (img_request)
1881 rbd_img_request_put(img_request);
1882 obj_request->result = result;
1883 obj_request->xferred = 0;
1884 obj_request_done_set(obj_request);
1885}
1886
Alex Eldercf81b602013-01-17 12:18:46 -06001887static int rbd_obj_notify_ack(struct rbd_device *rbd_dev,
Alex Elderb8d70032012-11-30 17:53:04 -06001888 u64 ver, u64 notify_id)
1889{
1890 struct rbd_obj_request *obj_request;
Alex Elder21692382013-04-05 01:27:12 -05001891 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elderb8d70032012-11-30 17:53:04 -06001892 int ret;
1893
1894 obj_request = rbd_obj_request_create(rbd_dev->header_name, 0, 0,
1895 OBJ_REQUEST_NODATA);
1896 if (!obj_request)
1897 return -ENOMEM;
1898
1899 ret = -ENOMEM;
Alex Elder430c28c2013-04-03 21:32:51 -05001900 obj_request->osd_req = rbd_osd_req_create(rbd_dev, false, obj_request);
Alex Elderb8d70032012-11-30 17:53:04 -06001901 if (!obj_request->osd_req)
1902 goto out;
Alex Elder21692382013-04-05 01:27:12 -05001903 obj_request->callback = rbd_obj_request_put;
Alex Elderb8d70032012-11-30 17:53:04 -06001904
Alex Elderc99d2d42013-04-05 01:27:11 -05001905 osd_req_op_watch_init(obj_request->osd_req, 0, CEPH_OSD_OP_NOTIFY_ACK,
1906 notify_id, ver, 0);
Alex Elder2fa12322013-04-05 01:27:12 -05001907 rbd_osd_req_format(obj_request, false);
Alex Elder430c28c2013-04-03 21:32:51 -05001908
Alex Elderb8d70032012-11-30 17:53:04 -06001909 ret = rbd_obj_request_submit(osdc, obj_request);
Alex Elderb8d70032012-11-30 17:53:04 -06001910out:
Alex Eldercf81b602013-01-17 12:18:46 -06001911 if (ret)
1912 rbd_obj_request_put(obj_request);
Alex Elderb8d70032012-11-30 17:53:04 -06001913
1914 return ret;
1915}
1916
1917static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
1918{
1919 struct rbd_device *rbd_dev = (struct rbd_device *)data;
1920 u64 hver;
1921 int rc;
1922
1923 if (!rbd_dev)
1924 return;
1925
Alex Elder37206ee2013-02-20 17:32:08 -06001926 dout("%s: \"%s\" notify_id %llu opcode %u\n", __func__,
Alex Elderb8d70032012-11-30 17:53:04 -06001927 rbd_dev->header_name, (unsigned long long) notify_id,
1928 (unsigned int) opcode);
1929 rc = rbd_dev_refresh(rbd_dev, &hver);
1930 if (rc)
1931 rbd_warn(rbd_dev, "got notification but failed to "
1932 " update snaps: %d\n", rc);
1933
Alex Eldercf81b602013-01-17 12:18:46 -06001934 rbd_obj_notify_ack(rbd_dev, hver, notify_id);
Alex Elderb8d70032012-11-30 17:53:04 -06001935}
1936
Alex Elder9969ebc2013-01-18 12:31:10 -06001937/*
1938 * Request sync osd watch/unwatch. The value of "start" determines
1939 * whether a watch request is being initiated or torn down.
1940 */
1941static int rbd_dev_header_watch_sync(struct rbd_device *rbd_dev, int start)
1942{
1943 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
1944 struct rbd_obj_request *obj_request;
Alex Elder9969ebc2013-01-18 12:31:10 -06001945 int ret;
1946
1947 rbd_assert(start ^ !!rbd_dev->watch_event);
1948 rbd_assert(start ^ !!rbd_dev->watch_request);
1949
1950 if (start) {
Alex Elder3c663bb2013-02-15 11:42:30 -06001951 ret = ceph_osdc_create_event(osdc, rbd_watch_cb, rbd_dev,
Alex Elder9969ebc2013-01-18 12:31:10 -06001952 &rbd_dev->watch_event);
1953 if (ret < 0)
1954 return ret;
Alex Elder8eb87562013-01-25 17:08:55 -06001955 rbd_assert(rbd_dev->watch_event != NULL);
Alex Elder9969ebc2013-01-18 12:31:10 -06001956 }
1957
1958 ret = -ENOMEM;
1959 obj_request = rbd_obj_request_create(rbd_dev->header_name, 0, 0,
1960 OBJ_REQUEST_NODATA);
1961 if (!obj_request)
1962 goto out_cancel;
1963
Alex Elder430c28c2013-04-03 21:32:51 -05001964 obj_request->osd_req = rbd_osd_req_create(rbd_dev, true, obj_request);
1965 if (!obj_request->osd_req)
1966 goto out_cancel;
1967
Alex Elder8eb87562013-01-25 17:08:55 -06001968 if (start)
Alex Elder975241a2013-01-25 17:08:55 -06001969 ceph_osdc_set_request_linger(osdc, obj_request->osd_req);
Alex Elder8eb87562013-01-25 17:08:55 -06001970 else
Alex Elder6977c3f2013-01-25 17:08:55 -06001971 ceph_osdc_unregister_linger_request(osdc,
Alex Elder975241a2013-01-25 17:08:55 -06001972 rbd_dev->watch_request->osd_req);
Alex Elder21692382013-04-05 01:27:12 -05001973
1974 osd_req_op_watch_init(obj_request->osd_req, 0, CEPH_OSD_OP_WATCH,
1975 rbd_dev->watch_event->cookie,
1976 rbd_dev->header.obj_version, start);
1977 rbd_osd_req_format(obj_request, true);
1978
Alex Elder9969ebc2013-01-18 12:31:10 -06001979 ret = rbd_obj_request_submit(osdc, obj_request);
1980 if (ret)
1981 goto out_cancel;
1982 ret = rbd_obj_request_wait(obj_request);
1983 if (ret)
1984 goto out_cancel;
Alex Elder9969ebc2013-01-18 12:31:10 -06001985 ret = obj_request->result;
1986 if (ret)
1987 goto out_cancel;
1988
Alex Elder8eb87562013-01-25 17:08:55 -06001989 /*
1990 * A watch request is set to linger, so the underlying osd
1991 * request won't go away until we unregister it. We retain
1992 * a pointer to the object request during that time (in
1993 * rbd_dev->watch_request), so we'll keep a reference to
1994 * it. We'll drop that reference (below) after we've
1995 * unregistered it.
1996 */
1997 if (start) {
1998 rbd_dev->watch_request = obj_request;
1999
2000 return 0;
2001 }
2002
2003 /* We have successfully torn down the watch request */
2004
2005 rbd_obj_request_put(rbd_dev->watch_request);
2006 rbd_dev->watch_request = NULL;
Alex Elder9969ebc2013-01-18 12:31:10 -06002007out_cancel:
2008 /* Cancel the event if we're tearing down, or on error */
2009 ceph_osdc_cancel_event(rbd_dev->watch_event);
2010 rbd_dev->watch_event = NULL;
Alex Elder9969ebc2013-01-18 12:31:10 -06002011 if (obj_request)
2012 rbd_obj_request_put(obj_request);
2013
2014 return ret;
2015}
2016
Alex Elder36be9a72013-01-19 00:30:28 -06002017/*
2018 * Synchronous osd object method call
2019 */
2020static int rbd_obj_method_sync(struct rbd_device *rbd_dev,
2021 const char *object_name,
2022 const char *class_name,
2023 const char *method_name,
2024 const char *outbound,
2025 size_t outbound_size,
2026 char *inbound,
2027 size_t inbound_size,
2028 u64 *version)
2029{
Alex Elder21692382013-04-05 01:27:12 -05002030 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder36be9a72013-01-19 00:30:28 -06002031 struct rbd_obj_request *obj_request;
Alex Elder36be9a72013-01-19 00:30:28 -06002032 struct page **pages;
2033 u32 page_count;
2034 int ret;
2035
2036 /*
Alex Elder6010a452013-04-05 01:27:11 -05002037 * Method calls are ultimately read operations. The result
2038 * should placed into the inbound buffer provided. They
2039 * also supply outbound data--parameters for the object
2040 * method. Currently if this is present it will be a
2041 * snapshot id.
Alex Elder36be9a72013-01-19 00:30:28 -06002042 */
2043 page_count = (u32) calc_pages_for(0, inbound_size);
2044 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2045 if (IS_ERR(pages))
2046 return PTR_ERR(pages);
2047
2048 ret = -ENOMEM;
Alex Elder6010a452013-04-05 01:27:11 -05002049 obj_request = rbd_obj_request_create(object_name, 0, inbound_size,
Alex Elder36be9a72013-01-19 00:30:28 -06002050 OBJ_REQUEST_PAGES);
2051 if (!obj_request)
2052 goto out;
2053
2054 obj_request->pages = pages;
2055 obj_request->page_count = page_count;
2056
Alex Elder430c28c2013-04-03 21:32:51 -05002057 obj_request->osd_req = rbd_osd_req_create(rbd_dev, false, obj_request);
Alex Elder36be9a72013-01-19 00:30:28 -06002058 if (!obj_request->osd_req)
2059 goto out;
2060
Alex Elderc99d2d42013-04-05 01:27:11 -05002061 osd_req_op_cls_init(obj_request->osd_req, 0, CEPH_OSD_OP_CALL,
Alex Elder04017e22013-04-05 14:46:02 -05002062 class_name, method_name);
2063 if (outbound_size) {
2064 struct ceph_pagelist *pagelist;
2065
2066 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
2067 if (!pagelist)
2068 goto out;
2069
2070 ceph_pagelist_init(pagelist);
2071 ceph_pagelist_append(pagelist, outbound, outbound_size);
2072 osd_req_op_cls_request_data_pagelist(obj_request->osd_req, 0,
2073 pagelist);
2074 }
Alex Eldera4ce40a2013-04-05 01:27:12 -05002075 osd_req_op_cls_response_data_pages(obj_request->osd_req, 0,
2076 obj_request->pages, inbound_size,
Alex Elder44cd1882013-04-05 01:27:12 -05002077 0, false, false);
Alex Elder2fa12322013-04-05 01:27:12 -05002078 rbd_osd_req_format(obj_request, false);
Alex Elder430c28c2013-04-03 21:32:51 -05002079
Alex Elder36be9a72013-01-19 00:30:28 -06002080 ret = rbd_obj_request_submit(osdc, obj_request);
2081 if (ret)
2082 goto out;
2083 ret = rbd_obj_request_wait(obj_request);
2084 if (ret)
2085 goto out;
2086
2087 ret = obj_request->result;
2088 if (ret < 0)
2089 goto out;
Alex Elder23ed6e12013-02-06 13:11:38 -06002090 ret = 0;
Alex Elder903bb322013-02-06 13:11:38 -06002091 ceph_copy_from_page_vector(pages, inbound, 0, obj_request->xferred);
Alex Elder36be9a72013-01-19 00:30:28 -06002092 if (version)
2093 *version = obj_request->version;
2094out:
2095 if (obj_request)
2096 rbd_obj_request_put(obj_request);
2097 else
2098 ceph_release_page_vector(pages, page_count);
2099
2100 return ret;
2101}
2102
Alex Elderbf0d5f502012-11-22 00:00:08 -06002103static void rbd_request_fn(struct request_queue *q)
Alex Eldercc344fa2013-02-19 12:25:56 -06002104 __releases(q->queue_lock) __acquires(q->queue_lock)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002105{
2106 struct rbd_device *rbd_dev = q->queuedata;
2107 bool read_only = rbd_dev->mapping.read_only;
2108 struct request *rq;
2109 int result;
2110
2111 while ((rq = blk_fetch_request(q))) {
2112 bool write_request = rq_data_dir(rq) == WRITE;
2113 struct rbd_img_request *img_request;
2114 u64 offset;
2115 u64 length;
2116
2117 /* Ignore any non-FS requests that filter through. */
2118
2119 if (rq->cmd_type != REQ_TYPE_FS) {
Alex Elder4dda41d2013-02-20 21:59:33 -06002120 dout("%s: non-fs request type %d\n", __func__,
2121 (int) rq->cmd_type);
2122 __blk_end_request_all(rq, 0);
2123 continue;
2124 }
2125
2126 /* Ignore/skip any zero-length requests */
2127
2128 offset = (u64) blk_rq_pos(rq) << SECTOR_SHIFT;
2129 length = (u64) blk_rq_bytes(rq);
2130
2131 if (!length) {
2132 dout("%s: zero-length request\n", __func__);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002133 __blk_end_request_all(rq, 0);
2134 continue;
2135 }
2136
2137 spin_unlock_irq(q->queue_lock);
2138
2139 /* Disallow writes to a read-only device */
2140
2141 if (write_request) {
2142 result = -EROFS;
2143 if (read_only)
2144 goto end_request;
2145 rbd_assert(rbd_dev->spec->snap_id == CEPH_NOSNAP);
2146 }
2147
Alex Elder6d292902013-01-14 12:43:31 -06002148 /*
2149 * Quit early if the mapped snapshot no longer
2150 * exists. It's still possible the snapshot will
2151 * have disappeared by the time our request arrives
2152 * at the osd, but there's no sense in sending it if
2153 * we already know.
2154 */
2155 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) {
Alex Elderbf0d5f502012-11-22 00:00:08 -06002156 dout("request for non-existent snapshot");
2157 rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP);
2158 result = -ENXIO;
2159 goto end_request;
2160 }
2161
Alex Elderbf0d5f502012-11-22 00:00:08 -06002162 result = -EINVAL;
2163 if (WARN_ON(offset && length > U64_MAX - offset + 1))
2164 goto end_request; /* Shouldn't happen */
2165
2166 result = -ENOMEM;
2167 img_request = rbd_img_request_create(rbd_dev, offset, length,
Alex Elder9849e982013-01-24 16:13:36 -06002168 write_request, false);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002169 if (!img_request)
2170 goto end_request;
2171
2172 img_request->rq = rq;
2173
2174 result = rbd_img_request_fill_bio(img_request, rq->bio);
2175 if (!result)
2176 result = rbd_img_request_submit(img_request);
2177 if (result)
2178 rbd_img_request_put(img_request);
2179end_request:
2180 spin_lock_irq(q->queue_lock);
2181 if (result < 0) {
Alex Elder7da22d22013-01-24 16:13:36 -06002182 rbd_warn(rbd_dev, "%s %llx at %llx result %d\n",
2183 write_request ? "write" : "read",
2184 length, offset, result);
2185
Alex Elderbf0d5f502012-11-22 00:00:08 -06002186 __blk_end_request_all(rq, result);
2187 }
2188 }
2189}
2190
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002191/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002192 * a queue callback. Makes sure that we don't create a bio that spans across
2193 * multiple osd objects. One exception would be with a single page bios,
Alex Elderf7760da2012-10-20 22:17:27 -05002194 * which we handle later at bio_chain_clone_range()
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002195 */
2196static int rbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd,
2197 struct bio_vec *bvec)
2198{
2199 struct rbd_device *rbd_dev = q->queuedata;
Alex Eldere5cfeed22012-10-20 22:17:27 -05002200 sector_t sector_offset;
2201 sector_t sectors_per_obj;
2202 sector_t obj_sector_offset;
2203 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002204
Alex Eldere5cfeed22012-10-20 22:17:27 -05002205 /*
2206 * Find how far into its rbd object the partition-relative
2207 * bio start sector is to offset relative to the enclosing
2208 * device.
2209 */
2210 sector_offset = get_start_sect(bmd->bi_bdev) + bmd->bi_sector;
2211 sectors_per_obj = 1 << (rbd_dev->header.obj_order - SECTOR_SHIFT);
2212 obj_sector_offset = sector_offset & (sectors_per_obj - 1);
Alex Elder593a9e72012-02-07 12:03:37 -06002213
Alex Eldere5cfeed22012-10-20 22:17:27 -05002214 /*
2215 * Compute the number of bytes from that offset to the end
2216 * of the object. Account for what's already used by the bio.
2217 */
2218 ret = (int) (sectors_per_obj - obj_sector_offset) << SECTOR_SHIFT;
2219 if (ret > bmd->bi_size)
2220 ret -= bmd->bi_size;
2221 else
2222 ret = 0;
2223
2224 /*
2225 * Don't send back more than was asked for. And if the bio
2226 * was empty, let the whole thing through because: "Note
2227 * that a block device *must* allow a single page to be
2228 * added to an empty bio."
2229 */
2230 rbd_assert(bvec->bv_len <= PAGE_SIZE);
2231 if (ret > (int) bvec->bv_len || !bmd->bi_size)
2232 ret = (int) bvec->bv_len;
2233
2234 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002235}
2236
2237static void rbd_free_disk(struct rbd_device *rbd_dev)
2238{
2239 struct gendisk *disk = rbd_dev->disk;
2240
2241 if (!disk)
2242 return;
2243
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002244 if (disk->flags & GENHD_FL_UP)
2245 del_gendisk(disk);
2246 if (disk->queue)
2247 blk_cleanup_queue(disk->queue);
2248 put_disk(disk);
2249}
2250
Alex Elder788e2df2013-01-17 12:25:27 -06002251static int rbd_obj_read_sync(struct rbd_device *rbd_dev,
2252 const char *object_name,
2253 u64 offset, u64 length,
2254 char *buf, u64 *version)
2255
2256{
Alex Elder21692382013-04-05 01:27:12 -05002257 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder788e2df2013-01-17 12:25:27 -06002258 struct rbd_obj_request *obj_request;
Alex Elder788e2df2013-01-17 12:25:27 -06002259 struct page **pages = NULL;
2260 u32 page_count;
Alex Elder1ceae7e2013-02-06 13:11:38 -06002261 size_t size;
Alex Elder788e2df2013-01-17 12:25:27 -06002262 int ret;
2263
2264 page_count = (u32) calc_pages_for(offset, length);
2265 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2266 if (IS_ERR(pages))
2267 ret = PTR_ERR(pages);
2268
2269 ret = -ENOMEM;
2270 obj_request = rbd_obj_request_create(object_name, offset, length,
Alex Elder36be9a72013-01-19 00:30:28 -06002271 OBJ_REQUEST_PAGES);
Alex Elder788e2df2013-01-17 12:25:27 -06002272 if (!obj_request)
2273 goto out;
2274
2275 obj_request->pages = pages;
2276 obj_request->page_count = page_count;
2277
Alex Elder430c28c2013-04-03 21:32:51 -05002278 obj_request->osd_req = rbd_osd_req_create(rbd_dev, false, obj_request);
Alex Elder788e2df2013-01-17 12:25:27 -06002279 if (!obj_request->osd_req)
2280 goto out;
2281
Alex Elderc99d2d42013-04-05 01:27:11 -05002282 osd_req_op_extent_init(obj_request->osd_req, 0, CEPH_OSD_OP_READ,
2283 offset, length, 0, 0);
Alex Eldera4ce40a2013-04-05 01:27:12 -05002284 osd_req_op_extent_osd_data_pages(obj_request->osd_req, 0, false,
2285 obj_request->pages,
Alex Elder44cd1882013-04-05 01:27:12 -05002286 obj_request->length,
2287 obj_request->offset & ~PAGE_MASK,
2288 false, false);
Alex Elder2fa12322013-04-05 01:27:12 -05002289 rbd_osd_req_format(obj_request, false);
Alex Elder430c28c2013-04-03 21:32:51 -05002290
Alex Elder788e2df2013-01-17 12:25:27 -06002291 ret = rbd_obj_request_submit(osdc, obj_request);
2292 if (ret)
2293 goto out;
2294 ret = rbd_obj_request_wait(obj_request);
2295 if (ret)
2296 goto out;
2297
2298 ret = obj_request->result;
2299 if (ret < 0)
2300 goto out;
Alex Elder1ceae7e2013-02-06 13:11:38 -06002301
2302 rbd_assert(obj_request->xferred <= (u64) SIZE_MAX);
2303 size = (size_t) obj_request->xferred;
Alex Elder903bb322013-02-06 13:11:38 -06002304 ceph_copy_from_page_vector(pages, buf, 0, size);
Alex Elder23ed6e12013-02-06 13:11:38 -06002305 rbd_assert(size <= (size_t) INT_MAX);
2306 ret = (int) size;
Alex Elder788e2df2013-01-17 12:25:27 -06002307 if (version)
2308 *version = obj_request->version;
2309out:
2310 if (obj_request)
2311 rbd_obj_request_put(obj_request);
2312 else
2313 ceph_release_page_vector(pages, page_count);
2314
2315 return ret;
2316}
2317
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002318/*
Alex Elder4156d992012-08-02 11:29:46 -05002319 * Read the complete header for the given rbd device.
2320 *
2321 * Returns a pointer to a dynamically-allocated buffer containing
2322 * the complete and validated header. Caller can pass the address
2323 * of a variable that will be filled in with the version of the
2324 * header object at the time it was read.
2325 *
2326 * Returns a pointer-coded errno if a failure occurs.
2327 */
2328static struct rbd_image_header_ondisk *
2329rbd_dev_v1_header_read(struct rbd_device *rbd_dev, u64 *version)
2330{
2331 struct rbd_image_header_ondisk *ondisk = NULL;
2332 u32 snap_count = 0;
2333 u64 names_size = 0;
2334 u32 want_count;
2335 int ret;
2336
2337 /*
2338 * The complete header will include an array of its 64-bit
2339 * snapshot ids, followed by the names of those snapshots as
2340 * a contiguous block of NUL-terminated strings. Note that
2341 * the number of snapshots could change by the time we read
2342 * it in, in which case we re-read it.
2343 */
2344 do {
2345 size_t size;
2346
2347 kfree(ondisk);
2348
2349 size = sizeof (*ondisk);
2350 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
2351 size += names_size;
2352 ondisk = kmalloc(size, GFP_KERNEL);
2353 if (!ondisk)
2354 return ERR_PTR(-ENOMEM);
2355
Alex Elder788e2df2013-01-17 12:25:27 -06002356 ret = rbd_obj_read_sync(rbd_dev, rbd_dev->header_name,
Alex Elder4156d992012-08-02 11:29:46 -05002357 0, size,
2358 (char *) ondisk, version);
Alex Elder4156d992012-08-02 11:29:46 -05002359 if (ret < 0)
2360 goto out_err;
2361 if (WARN_ON((size_t) ret < size)) {
2362 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05002363 rbd_warn(rbd_dev, "short header read (want %zd got %d)",
2364 size, ret);
Alex Elder4156d992012-08-02 11:29:46 -05002365 goto out_err;
2366 }
2367 if (!rbd_dev_ondisk_valid(ondisk)) {
2368 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05002369 rbd_warn(rbd_dev, "invalid header");
Alex Elder4156d992012-08-02 11:29:46 -05002370 goto out_err;
2371 }
2372
2373 names_size = le64_to_cpu(ondisk->snap_names_len);
2374 want_count = snap_count;
2375 snap_count = le32_to_cpu(ondisk->snap_count);
2376 } while (snap_count != want_count);
2377
2378 return ondisk;
2379
2380out_err:
2381 kfree(ondisk);
2382
2383 return ERR_PTR(ret);
2384}
2385
2386/*
2387 * reload the ondisk the header
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002388 */
2389static int rbd_read_header(struct rbd_device *rbd_dev,
2390 struct rbd_image_header *header)
2391{
Alex Elder4156d992012-08-02 11:29:46 -05002392 struct rbd_image_header_ondisk *ondisk;
2393 u64 ver = 0;
2394 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002395
Alex Elder4156d992012-08-02 11:29:46 -05002396 ondisk = rbd_dev_v1_header_read(rbd_dev, &ver);
2397 if (IS_ERR(ondisk))
2398 return PTR_ERR(ondisk);
2399 ret = rbd_header_from_disk(header, ondisk);
2400 if (ret >= 0)
2401 header->obj_version = ver;
2402 kfree(ondisk);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002403
Alex Elder4156d992012-08-02 11:29:46 -05002404 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002405}
2406
Alex Elder41f38c22012-10-25 23:34:40 -05002407static void rbd_remove_all_snaps(struct rbd_device *rbd_dev)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002408{
2409 struct rbd_snap *snap;
Alex Eldera0593292012-07-19 09:09:27 -05002410 struct rbd_snap *next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002411
Alex Eldera0593292012-07-19 09:09:27 -05002412 list_for_each_entry_safe(snap, next, &rbd_dev->snaps, node)
Alex Elder41f38c22012-10-25 23:34:40 -05002413 rbd_remove_snap_dev(snap);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002414}
2415
Alex Elder94785542012-10-09 13:50:17 -07002416static void rbd_update_mapping_size(struct rbd_device *rbd_dev)
2417{
2418 sector_t size;
2419
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002420 if (rbd_dev->spec->snap_id != CEPH_NOSNAP)
Alex Elder94785542012-10-09 13:50:17 -07002421 return;
2422
2423 size = (sector_t) rbd_dev->header.image_size / SECTOR_SIZE;
2424 dout("setting size to %llu sectors", (unsigned long long) size);
2425 rbd_dev->mapping.size = (u64) size;
2426 set_capacity(rbd_dev->disk, size);
2427}
2428
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002429/*
2430 * only read the first part of the ondisk header, without the snaps info
2431 */
Alex Elder117973f2012-08-31 17:29:55 -05002432static int rbd_dev_v1_refresh(struct rbd_device *rbd_dev, u64 *hver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002433{
2434 int ret;
2435 struct rbd_image_header h;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002436
2437 ret = rbd_read_header(rbd_dev, &h);
2438 if (ret < 0)
2439 return ret;
2440
Josh Durgina51aa0c2011-12-05 10:35:04 -08002441 down_write(&rbd_dev->header_rwsem);
2442
Alex Elder94785542012-10-09 13:50:17 -07002443 /* Update image size, and check for resize of mapped image */
2444 rbd_dev->header.image_size = h.image_size;
2445 rbd_update_mapping_size(rbd_dev);
Sage Weil9db4b3e2011-04-19 22:49:06 -07002446
Alex Elder849b4262012-07-09 21:04:24 -05002447 /* rbd_dev->header.object_prefix shouldn't change */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002448 kfree(rbd_dev->header.snap_sizes);
Alex Elder849b4262012-07-09 21:04:24 -05002449 kfree(rbd_dev->header.snap_names);
Josh Durgind1d25642011-12-05 14:03:05 -08002450 /* osd requests may still refer to snapc */
2451 ceph_put_snap_context(rbd_dev->header.snapc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002452
Alex Elderb8136232012-07-25 09:32:41 -05002453 if (hver)
2454 *hver = h.obj_version;
Josh Durgina71b8912011-12-05 18:10:44 -08002455 rbd_dev->header.obj_version = h.obj_version;
Josh Durgin93a24e02011-12-05 10:41:28 -08002456 rbd_dev->header.image_size = h.image_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002457 rbd_dev->header.snapc = h.snapc;
2458 rbd_dev->header.snap_names = h.snap_names;
2459 rbd_dev->header.snap_sizes = h.snap_sizes;
Alex Elder849b4262012-07-09 21:04:24 -05002460 /* Free the extra copy of the object prefix */
2461 WARN_ON(strcmp(rbd_dev->header.object_prefix, h.object_prefix));
2462 kfree(h.object_prefix);
2463
Alex Elder304f6802012-08-31 17:29:52 -05002464 ret = rbd_dev_snaps_update(rbd_dev);
2465 if (!ret)
2466 ret = rbd_dev_snaps_register(rbd_dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002467
Josh Durginc6666012011-11-21 17:11:12 -08002468 up_write(&rbd_dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002469
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002470 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002471}
2472
Alex Elder117973f2012-08-31 17:29:55 -05002473static int rbd_dev_refresh(struct rbd_device *rbd_dev, u64 *hver)
Alex Elder1fe5e992012-07-25 09:32:41 -05002474{
2475 int ret;
2476
Alex Elder117973f2012-08-31 17:29:55 -05002477 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
Alex Elder1fe5e992012-07-25 09:32:41 -05002478 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Alex Elder117973f2012-08-31 17:29:55 -05002479 if (rbd_dev->image_format == 1)
2480 ret = rbd_dev_v1_refresh(rbd_dev, hver);
2481 else
2482 ret = rbd_dev_v2_refresh(rbd_dev, hver);
Alex Elder1fe5e992012-07-25 09:32:41 -05002483 mutex_unlock(&ctl_mutex);
2484
2485 return ret;
2486}
2487
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002488static int rbd_init_disk(struct rbd_device *rbd_dev)
2489{
2490 struct gendisk *disk;
2491 struct request_queue *q;
Alex Elder593a9e72012-02-07 12:03:37 -06002492 u64 segment_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002493
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002494 /* create gendisk info */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002495 disk = alloc_disk(RBD_MINORS_PER_MAJOR);
2496 if (!disk)
Alex Elder1fcdb8a2012-08-29 17:11:06 -05002497 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002498
Alex Elderf0f8cef2012-01-29 13:57:44 -06002499 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
Alex Elderde71a292012-07-03 16:01:19 -05002500 rbd_dev->dev_id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002501 disk->major = rbd_dev->major;
2502 disk->first_minor = 0;
2503 disk->fops = &rbd_bd_ops;
2504 disk->private_data = rbd_dev;
2505
Alex Elderbf0d5f502012-11-22 00:00:08 -06002506 q = blk_init_queue(rbd_request_fn, &rbd_dev->lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002507 if (!q)
2508 goto out_disk;
Josh Durgin029bcbd2011-07-22 11:35:23 -07002509
Alex Elder593a9e72012-02-07 12:03:37 -06002510 /* We use the default size, but let's be explicit about it. */
2511 blk_queue_physical_block_size(q, SECTOR_SIZE);
2512
Josh Durgin029bcbd2011-07-22 11:35:23 -07002513 /* set io sizes to object size */
Alex Elder593a9e72012-02-07 12:03:37 -06002514 segment_size = rbd_obj_bytes(&rbd_dev->header);
2515 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
2516 blk_queue_max_segment_size(q, segment_size);
2517 blk_queue_io_min(q, segment_size);
2518 blk_queue_io_opt(q, segment_size);
Josh Durgin029bcbd2011-07-22 11:35:23 -07002519
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002520 blk_queue_merge_bvec(q, rbd_merge_bvec);
2521 disk->queue = q;
2522
2523 q->queuedata = rbd_dev;
2524
2525 rbd_dev->disk = disk;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002526
Alex Elder12f02942012-08-29 17:11:07 -05002527 set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
2528
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002529 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002530out_disk:
2531 put_disk(disk);
Alex Elder1fcdb8a2012-08-29 17:11:06 -05002532
2533 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002534}
2535
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002536/*
2537 sysfs
2538*/
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002539
Alex Elder593a9e72012-02-07 12:03:37 -06002540static struct rbd_device *dev_to_rbd_dev(struct device *dev)
2541{
2542 return container_of(dev, struct rbd_device, dev);
2543}
2544
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002545static ssize_t rbd_size_show(struct device *dev,
2546 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002547{
Alex Elder593a9e72012-02-07 12:03:37 -06002548 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Josh Durgina51aa0c2011-12-05 10:35:04 -08002549 sector_t size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002550
Josh Durgina51aa0c2011-12-05 10:35:04 -08002551 down_read(&rbd_dev->header_rwsem);
2552 size = get_capacity(rbd_dev->disk);
2553 up_read(&rbd_dev->header_rwsem);
2554
2555 return sprintf(buf, "%llu\n", (unsigned long long) size * SECTOR_SIZE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002556}
2557
Alex Elder34b13182012-07-13 20:35:12 -05002558/*
2559 * Note this shows the features for whatever's mapped, which is not
2560 * necessarily the base image.
2561 */
2562static ssize_t rbd_features_show(struct device *dev,
2563 struct device_attribute *attr, char *buf)
2564{
2565 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
2566
2567 return sprintf(buf, "0x%016llx\n",
2568 (unsigned long long) rbd_dev->mapping.features);
2569}
2570
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002571static ssize_t rbd_major_show(struct device *dev,
2572 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002573{
Alex Elder593a9e72012-02-07 12:03:37 -06002574 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002575
2576 return sprintf(buf, "%d\n", rbd_dev->major);
2577}
2578
2579static ssize_t rbd_client_id_show(struct device *dev,
2580 struct device_attribute *attr, char *buf)
2581{
Alex Elder593a9e72012-02-07 12:03:37 -06002582 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002583
Alex Elder1dbb4392012-01-24 10:08:37 -06002584 return sprintf(buf, "client%lld\n",
2585 ceph_client_id(rbd_dev->rbd_client->client));
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002586}
2587
2588static ssize_t rbd_pool_show(struct device *dev,
2589 struct device_attribute *attr, char *buf)
2590{
Alex Elder593a9e72012-02-07 12:03:37 -06002591 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002592
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002593 return sprintf(buf, "%s\n", rbd_dev->spec->pool_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002594}
2595
Alex Elder9bb2f332012-07-12 10:46:35 -05002596static ssize_t rbd_pool_id_show(struct device *dev,
2597 struct device_attribute *attr, char *buf)
2598{
2599 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
2600
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002601 return sprintf(buf, "%llu\n",
2602 (unsigned long long) rbd_dev->spec->pool_id);
Alex Elder9bb2f332012-07-12 10:46:35 -05002603}
2604
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002605static ssize_t rbd_name_show(struct device *dev,
2606 struct device_attribute *attr, char *buf)
2607{
Alex Elder593a9e72012-02-07 12:03:37 -06002608 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002609
Alex Eldera92ffdf2012-10-30 19:40:33 -05002610 if (rbd_dev->spec->image_name)
2611 return sprintf(buf, "%s\n", rbd_dev->spec->image_name);
2612
2613 return sprintf(buf, "(unknown)\n");
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002614}
2615
Alex Elder589d30e2012-07-10 20:30:11 -05002616static ssize_t rbd_image_id_show(struct device *dev,
2617 struct device_attribute *attr, char *buf)
2618{
2619 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
2620
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002621 return sprintf(buf, "%s\n", rbd_dev->spec->image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05002622}
2623
Alex Elder34b13182012-07-13 20:35:12 -05002624/*
2625 * Shows the name of the currently-mapped snapshot (or
2626 * RBD_SNAP_HEAD_NAME for the base image).
2627 */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002628static ssize_t rbd_snap_show(struct device *dev,
2629 struct device_attribute *attr,
2630 char *buf)
2631{
Alex Elder593a9e72012-02-07 12:03:37 -06002632 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002633
Alex Elder0d7dbfc2012-10-25 23:34:41 -05002634 return sprintf(buf, "%s\n", rbd_dev->spec->snap_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002635}
2636
Alex Elder86b00e02012-10-25 23:34:42 -05002637/*
2638 * For an rbd v2 image, shows the pool id, image id, and snapshot id
2639 * for the parent image. If there is no parent, simply shows
2640 * "(no parent image)".
2641 */
2642static ssize_t rbd_parent_show(struct device *dev,
2643 struct device_attribute *attr,
2644 char *buf)
2645{
2646 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
2647 struct rbd_spec *spec = rbd_dev->parent_spec;
2648 int count;
2649 char *bufp = buf;
2650
2651 if (!spec)
2652 return sprintf(buf, "(no parent image)\n");
2653
2654 count = sprintf(bufp, "pool_id %llu\npool_name %s\n",
2655 (unsigned long long) spec->pool_id, spec->pool_name);
2656 if (count < 0)
2657 return count;
2658 bufp += count;
2659
2660 count = sprintf(bufp, "image_id %s\nimage_name %s\n", spec->image_id,
2661 spec->image_name ? spec->image_name : "(unknown)");
2662 if (count < 0)
2663 return count;
2664 bufp += count;
2665
2666 count = sprintf(bufp, "snap_id %llu\nsnap_name %s\n",
2667 (unsigned long long) spec->snap_id, spec->snap_name);
2668 if (count < 0)
2669 return count;
2670 bufp += count;
2671
2672 count = sprintf(bufp, "overlap %llu\n", rbd_dev->parent_overlap);
2673 if (count < 0)
2674 return count;
2675 bufp += count;
2676
2677 return (ssize_t) (bufp - buf);
2678}
2679
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002680static ssize_t rbd_image_refresh(struct device *dev,
2681 struct device_attribute *attr,
2682 const char *buf,
2683 size_t size)
2684{
Alex Elder593a9e72012-02-07 12:03:37 -06002685 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Alex Elderb8136232012-07-25 09:32:41 -05002686 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002687
Alex Elder117973f2012-08-31 17:29:55 -05002688 ret = rbd_dev_refresh(rbd_dev, NULL);
Alex Elderb8136232012-07-25 09:32:41 -05002689
2690 return ret < 0 ? ret : size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002691}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002692
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002693static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
Alex Elder34b13182012-07-13 20:35:12 -05002694static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002695static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
2696static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
2697static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
Alex Elder9bb2f332012-07-12 10:46:35 -05002698static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002699static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
Alex Elder589d30e2012-07-10 20:30:11 -05002700static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002701static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
2702static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
Alex Elder86b00e02012-10-25 23:34:42 -05002703static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002704
2705static struct attribute *rbd_attrs[] = {
2706 &dev_attr_size.attr,
Alex Elder34b13182012-07-13 20:35:12 -05002707 &dev_attr_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002708 &dev_attr_major.attr,
2709 &dev_attr_client_id.attr,
2710 &dev_attr_pool.attr,
Alex Elder9bb2f332012-07-12 10:46:35 -05002711 &dev_attr_pool_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002712 &dev_attr_name.attr,
Alex Elder589d30e2012-07-10 20:30:11 -05002713 &dev_attr_image_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002714 &dev_attr_current_snap.attr,
Alex Elder86b00e02012-10-25 23:34:42 -05002715 &dev_attr_parent.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002716 &dev_attr_refresh.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002717 NULL
2718};
2719
2720static struct attribute_group rbd_attr_group = {
2721 .attrs = rbd_attrs,
2722};
2723
2724static const struct attribute_group *rbd_attr_groups[] = {
2725 &rbd_attr_group,
2726 NULL
2727};
2728
2729static void rbd_sysfs_dev_release(struct device *dev)
2730{
2731}
2732
2733static struct device_type rbd_device_type = {
2734 .name = "rbd",
2735 .groups = rbd_attr_groups,
2736 .release = rbd_sysfs_dev_release,
2737};
2738
2739
2740/*
2741 sysfs - snapshots
2742*/
2743
2744static ssize_t rbd_snap_size_show(struct device *dev,
2745 struct device_attribute *attr,
2746 char *buf)
2747{
2748 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2749
Josh Durgin3591538f2011-12-05 18:25:13 -08002750 return sprintf(buf, "%llu\n", (unsigned long long)snap->size);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002751}
2752
2753static ssize_t rbd_snap_id_show(struct device *dev,
2754 struct device_attribute *attr,
2755 char *buf)
2756{
2757 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2758
Josh Durgin3591538f2011-12-05 18:25:13 -08002759 return sprintf(buf, "%llu\n", (unsigned long long)snap->id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002760}
2761
Alex Elder34b13182012-07-13 20:35:12 -05002762static ssize_t rbd_snap_features_show(struct device *dev,
2763 struct device_attribute *attr,
2764 char *buf)
2765{
2766 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2767
2768 return sprintf(buf, "0x%016llx\n",
2769 (unsigned long long) snap->features);
2770}
2771
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002772static DEVICE_ATTR(snap_size, S_IRUGO, rbd_snap_size_show, NULL);
2773static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL);
Alex Elder34b13182012-07-13 20:35:12 -05002774static DEVICE_ATTR(snap_features, S_IRUGO, rbd_snap_features_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002775
2776static struct attribute *rbd_snap_attrs[] = {
2777 &dev_attr_snap_size.attr,
2778 &dev_attr_snap_id.attr,
Alex Elder34b13182012-07-13 20:35:12 -05002779 &dev_attr_snap_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002780 NULL,
2781};
2782
2783static struct attribute_group rbd_snap_attr_group = {
2784 .attrs = rbd_snap_attrs,
2785};
2786
2787static void rbd_snap_dev_release(struct device *dev)
2788{
2789 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
2790 kfree(snap->name);
2791 kfree(snap);
2792}
2793
2794static const struct attribute_group *rbd_snap_attr_groups[] = {
2795 &rbd_snap_attr_group,
2796 NULL
2797};
2798
2799static struct device_type rbd_snap_device_type = {
2800 .groups = rbd_snap_attr_groups,
2801 .release = rbd_snap_dev_release,
2802};
2803
Alex Elder8b8fb992012-10-26 17:25:24 -05002804static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)
2805{
2806 kref_get(&spec->kref);
2807
2808 return spec;
2809}
2810
2811static void rbd_spec_free(struct kref *kref);
2812static void rbd_spec_put(struct rbd_spec *spec)
2813{
2814 if (spec)
2815 kref_put(&spec->kref, rbd_spec_free);
2816}
2817
2818static struct rbd_spec *rbd_spec_alloc(void)
2819{
2820 struct rbd_spec *spec;
2821
2822 spec = kzalloc(sizeof (*spec), GFP_KERNEL);
2823 if (!spec)
2824 return NULL;
2825 kref_init(&spec->kref);
2826
Alex Elder8b8fb992012-10-26 17:25:24 -05002827 return spec;
2828}
2829
2830static void rbd_spec_free(struct kref *kref)
2831{
2832 struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref);
2833
2834 kfree(spec->pool_name);
2835 kfree(spec->image_id);
2836 kfree(spec->image_name);
2837 kfree(spec->snap_name);
2838 kfree(spec);
2839}
2840
Alex Eldercc344fa2013-02-19 12:25:56 -06002841static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,
Alex Elderc53d5892012-10-25 23:34:42 -05002842 struct rbd_spec *spec)
2843{
2844 struct rbd_device *rbd_dev;
2845
2846 rbd_dev = kzalloc(sizeof (*rbd_dev), GFP_KERNEL);
2847 if (!rbd_dev)
2848 return NULL;
2849
2850 spin_lock_init(&rbd_dev->lock);
Alex Elder6d292902013-01-14 12:43:31 -06002851 rbd_dev->flags = 0;
Alex Elderc53d5892012-10-25 23:34:42 -05002852 INIT_LIST_HEAD(&rbd_dev->node);
2853 INIT_LIST_HEAD(&rbd_dev->snaps);
2854 init_rwsem(&rbd_dev->header_rwsem);
2855
2856 rbd_dev->spec = spec;
2857 rbd_dev->rbd_client = rbdc;
2858
Alex Elder0903e872012-11-14 12:25:19 -06002859 /* Initialize the layout used for all rbd requests */
2860
2861 rbd_dev->layout.fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
2862 rbd_dev->layout.fl_stripe_count = cpu_to_le32(1);
2863 rbd_dev->layout.fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
2864 rbd_dev->layout.fl_pg_pool = cpu_to_le32((u32) spec->pool_id);
2865
Alex Elderc53d5892012-10-25 23:34:42 -05002866 return rbd_dev;
2867}
2868
2869static void rbd_dev_destroy(struct rbd_device *rbd_dev)
2870{
Alex Elder86b00e02012-10-25 23:34:42 -05002871 rbd_spec_put(rbd_dev->parent_spec);
Alex Elderc53d5892012-10-25 23:34:42 -05002872 kfree(rbd_dev->header_name);
2873 rbd_put_client(rbd_dev->rbd_client);
2874 rbd_spec_put(rbd_dev->spec);
2875 kfree(rbd_dev);
2876}
2877
Alex Elder304f6802012-08-31 17:29:52 -05002878static bool rbd_snap_registered(struct rbd_snap *snap)
2879{
2880 bool ret = snap->dev.type == &rbd_snap_device_type;
2881 bool reg = device_is_registered(&snap->dev);
2882
2883 rbd_assert(!ret ^ reg);
2884
2885 return ret;
2886}
2887
Alex Elder41f38c22012-10-25 23:34:40 -05002888static void rbd_remove_snap_dev(struct rbd_snap *snap)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002889{
2890 list_del(&snap->node);
Alex Elder304f6802012-08-31 17:29:52 -05002891 if (device_is_registered(&snap->dev))
2892 device_unregister(&snap->dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002893}
2894
Alex Elder14e70852012-07-19 09:09:27 -05002895static int rbd_register_snap_dev(struct rbd_snap *snap,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002896 struct device *parent)
2897{
2898 struct device *dev = &snap->dev;
2899 int ret;
2900
2901 dev->type = &rbd_snap_device_type;
2902 dev->parent = parent;
2903 dev->release = rbd_snap_dev_release;
Alex Elderd4b125e2012-07-03 16:01:19 -05002904 dev_set_name(dev, "%s%s", RBD_SNAP_DEV_NAME_PREFIX, snap->name);
Alex Elder304f6802012-08-31 17:29:52 -05002905 dout("%s: registering device for snapshot %s\n", __func__, snap->name);
2906
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002907 ret = device_register(dev);
2908
2909 return ret;
2910}
2911
Alex Elder4e891e02012-07-10 20:30:10 -05002912static struct rbd_snap *__rbd_add_snap_dev(struct rbd_device *rbd_dev,
Alex Elderc8d18422012-07-10 20:30:11 -05002913 const char *snap_name,
Alex Elder34b13182012-07-13 20:35:12 -05002914 u64 snap_id, u64 snap_size,
2915 u64 snap_features)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002916{
Alex Elder4e891e02012-07-10 20:30:10 -05002917 struct rbd_snap *snap;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002918 int ret;
Alex Elder4e891e02012-07-10 20:30:10 -05002919
2920 snap = kzalloc(sizeof (*snap), GFP_KERNEL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002921 if (!snap)
Alex Elder4e891e02012-07-10 20:30:10 -05002922 return ERR_PTR(-ENOMEM);
2923
2924 ret = -ENOMEM;
Alex Elderc8d18422012-07-10 20:30:11 -05002925 snap->name = kstrdup(snap_name, GFP_KERNEL);
Alex Elder4e891e02012-07-10 20:30:10 -05002926 if (!snap->name)
2927 goto err;
2928
Alex Elderc8d18422012-07-10 20:30:11 -05002929 snap->id = snap_id;
2930 snap->size = snap_size;
Alex Elder34b13182012-07-13 20:35:12 -05002931 snap->features = snap_features;
Alex Elder4e891e02012-07-10 20:30:10 -05002932
2933 return snap;
2934
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002935err:
2936 kfree(snap->name);
2937 kfree(snap);
Alex Elder4e891e02012-07-10 20:30:10 -05002938
2939 return ERR_PTR(ret);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002940}
2941
Alex Eldercd892122012-07-03 16:01:19 -05002942static char *rbd_dev_v1_snap_info(struct rbd_device *rbd_dev, u32 which,
2943 u64 *snap_size, u64 *snap_features)
2944{
2945 char *snap_name;
2946
2947 rbd_assert(which < rbd_dev->header.snapc->num_snaps);
2948
2949 *snap_size = rbd_dev->header.snap_sizes[which];
2950 *snap_features = 0; /* No features for v1 */
2951
2952 /* Skip over names until we find the one we are looking for */
2953
2954 snap_name = rbd_dev->header.snap_names;
2955 while (which--)
2956 snap_name += strlen(snap_name) + 1;
2957
2958 return snap_name;
2959}
2960
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002961/*
Alex Elder9d475de2012-07-03 16:01:19 -05002962 * Get the size and object order for an image snapshot, or if
2963 * snap_id is CEPH_NOSNAP, gets this information for the base
2964 * image.
2965 */
2966static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
2967 u8 *order, u64 *snap_size)
2968{
2969 __le64 snapid = cpu_to_le64(snap_id);
2970 int ret;
2971 struct {
2972 u8 order;
2973 __le64 size;
2974 } __attribute__ ((packed)) size_buf = { 0 };
2975
Alex Elder36be9a72013-01-19 00:30:28 -06002976 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elder9d475de2012-07-03 16:01:19 -05002977 "rbd", "get_size",
2978 (char *) &snapid, sizeof (snapid),
Alex Elder07b23912012-11-09 08:43:16 -06002979 (char *) &size_buf, sizeof (size_buf), NULL);
Alex Elder36be9a72013-01-19 00:30:28 -06002980 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder9d475de2012-07-03 16:01:19 -05002981 if (ret < 0)
2982 return ret;
2983
2984 *order = size_buf.order;
2985 *snap_size = le64_to_cpu(size_buf.size);
2986
2987 dout(" snap_id 0x%016llx order = %u, snap_size = %llu\n",
2988 (unsigned long long) snap_id, (unsigned int) *order,
2989 (unsigned long long) *snap_size);
2990
2991 return 0;
2992}
2993
2994static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
2995{
2996 return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
2997 &rbd_dev->header.obj_order,
2998 &rbd_dev->header.image_size);
2999}
3000
Alex Elder1e130192012-07-03 16:01:19 -05003001static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
3002{
3003 void *reply_buf;
3004 int ret;
3005 void *p;
3006
3007 reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL);
3008 if (!reply_buf)
3009 return -ENOMEM;
3010
Alex Elder36be9a72013-01-19 00:30:28 -06003011 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elder1e130192012-07-03 16:01:19 -05003012 "rbd", "get_object_prefix",
3013 NULL, 0,
Alex Elder07b23912012-11-09 08:43:16 -06003014 reply_buf, RBD_OBJ_PREFIX_LEN_MAX, NULL);
Alex Elder36be9a72013-01-19 00:30:28 -06003015 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder1e130192012-07-03 16:01:19 -05003016 if (ret < 0)
3017 goto out;
3018
3019 p = reply_buf;
3020 rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p,
3021 p + RBD_OBJ_PREFIX_LEN_MAX,
3022 NULL, GFP_NOIO);
3023
3024 if (IS_ERR(rbd_dev->header.object_prefix)) {
3025 ret = PTR_ERR(rbd_dev->header.object_prefix);
3026 rbd_dev->header.object_prefix = NULL;
3027 } else {
3028 dout(" object_prefix = %s\n", rbd_dev->header.object_prefix);
3029 }
3030
3031out:
3032 kfree(reply_buf);
3033
3034 return ret;
3035}
3036
Alex Elderb1b54022012-07-03 16:01:19 -05003037static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
3038 u64 *snap_features)
3039{
3040 __le64 snapid = cpu_to_le64(snap_id);
3041 struct {
3042 __le64 features;
3043 __le64 incompat;
3044 } features_buf = { 0 };
Alex Elderd8891402012-10-09 13:50:17 -07003045 u64 incompat;
Alex Elderb1b54022012-07-03 16:01:19 -05003046 int ret;
3047
Alex Elder36be9a72013-01-19 00:30:28 -06003048 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elderb1b54022012-07-03 16:01:19 -05003049 "rbd", "get_features",
3050 (char *) &snapid, sizeof (snapid),
3051 (char *) &features_buf, sizeof (features_buf),
Alex Elder07b23912012-11-09 08:43:16 -06003052 NULL);
Alex Elder36be9a72013-01-19 00:30:28 -06003053 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderb1b54022012-07-03 16:01:19 -05003054 if (ret < 0)
3055 return ret;
Alex Elderd8891402012-10-09 13:50:17 -07003056
3057 incompat = le64_to_cpu(features_buf.incompat);
Alex Elder5cbf6f122013-04-11 09:29:48 -05003058 if (incompat & ~RBD_FEATURES_SUPPORTED)
Alex Elderb8f5c6e2012-11-01 08:39:26 -05003059 return -ENXIO;
Alex Elderd8891402012-10-09 13:50:17 -07003060
Alex Elderb1b54022012-07-03 16:01:19 -05003061 *snap_features = le64_to_cpu(features_buf.features);
3062
3063 dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
3064 (unsigned long long) snap_id,
3065 (unsigned long long) *snap_features,
3066 (unsigned long long) le64_to_cpu(features_buf.incompat));
3067
3068 return 0;
3069}
3070
3071static int rbd_dev_v2_features(struct rbd_device *rbd_dev)
3072{
3073 return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP,
3074 &rbd_dev->header.features);
3075}
3076
Alex Elder86b00e02012-10-25 23:34:42 -05003077static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev)
3078{
3079 struct rbd_spec *parent_spec;
3080 size_t size;
3081 void *reply_buf = NULL;
3082 __le64 snapid;
3083 void *p;
3084 void *end;
3085 char *image_id;
3086 u64 overlap;
Alex Elder86b00e02012-10-25 23:34:42 -05003087 int ret;
3088
3089 parent_spec = rbd_spec_alloc();
3090 if (!parent_spec)
3091 return -ENOMEM;
3092
3093 size = sizeof (__le64) + /* pool_id */
3094 sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */
3095 sizeof (__le64) + /* snap_id */
3096 sizeof (__le64); /* overlap */
3097 reply_buf = kmalloc(size, GFP_KERNEL);
3098 if (!reply_buf) {
3099 ret = -ENOMEM;
3100 goto out_err;
3101 }
3102
3103 snapid = cpu_to_le64(CEPH_NOSNAP);
Alex Elder36be9a72013-01-19 00:30:28 -06003104 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elder86b00e02012-10-25 23:34:42 -05003105 "rbd", "get_parent",
3106 (char *) &snapid, sizeof (snapid),
Alex Elder07b23912012-11-09 08:43:16 -06003107 (char *) reply_buf, size, NULL);
Alex Elder36be9a72013-01-19 00:30:28 -06003108 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder86b00e02012-10-25 23:34:42 -05003109 if (ret < 0)
3110 goto out_err;
3111
3112 ret = -ERANGE;
3113 p = reply_buf;
3114 end = (char *) reply_buf + size;
3115 ceph_decode_64_safe(&p, end, parent_spec->pool_id, out_err);
3116 if (parent_spec->pool_id == CEPH_NOPOOL)
3117 goto out; /* No parent? No problem. */
3118
Alex Elder0903e872012-11-14 12:25:19 -06003119 /* The ceph file layout needs to fit pool id in 32 bits */
3120
3121 ret = -EIO;
3122 if (WARN_ON(parent_spec->pool_id > (u64) U32_MAX))
3123 goto out;
3124
Alex Elder979ed482012-11-01 08:39:26 -05003125 image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elder86b00e02012-10-25 23:34:42 -05003126 if (IS_ERR(image_id)) {
3127 ret = PTR_ERR(image_id);
3128 goto out_err;
3129 }
3130 parent_spec->image_id = image_id;
3131 ceph_decode_64_safe(&p, end, parent_spec->snap_id, out_err);
3132 ceph_decode_64_safe(&p, end, overlap, out_err);
3133
3134 rbd_dev->parent_overlap = overlap;
3135 rbd_dev->parent_spec = parent_spec;
3136 parent_spec = NULL; /* rbd_dev now owns this */
3137out:
3138 ret = 0;
3139out_err:
3140 kfree(reply_buf);
3141 rbd_spec_put(parent_spec);
3142
3143 return ret;
3144}
3145
Alex Elder9e15b772012-10-30 19:40:33 -05003146static char *rbd_dev_image_name(struct rbd_device *rbd_dev)
3147{
3148 size_t image_id_size;
3149 char *image_id;
3150 void *p;
3151 void *end;
3152 size_t size;
3153 void *reply_buf = NULL;
3154 size_t len = 0;
3155 char *image_name = NULL;
3156 int ret;
3157
3158 rbd_assert(!rbd_dev->spec->image_name);
3159
Alex Elder69e7a022012-11-01 08:39:26 -05003160 len = strlen(rbd_dev->spec->image_id);
3161 image_id_size = sizeof (__le32) + len;
Alex Elder9e15b772012-10-30 19:40:33 -05003162 image_id = kmalloc(image_id_size, GFP_KERNEL);
3163 if (!image_id)
3164 return NULL;
3165
3166 p = image_id;
3167 end = (char *) image_id + image_id_size;
Alex Elder69e7a022012-11-01 08:39:26 -05003168 ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32) len);
Alex Elder9e15b772012-10-30 19:40:33 -05003169
3170 size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX;
3171 reply_buf = kmalloc(size, GFP_KERNEL);
3172 if (!reply_buf)
3173 goto out;
3174
Alex Elder36be9a72013-01-19 00:30:28 -06003175 ret = rbd_obj_method_sync(rbd_dev, RBD_DIRECTORY,
Alex Elder9e15b772012-10-30 19:40:33 -05003176 "rbd", "dir_get_name",
3177 image_id, image_id_size,
Alex Elder07b23912012-11-09 08:43:16 -06003178 (char *) reply_buf, size, NULL);
Alex Elder9e15b772012-10-30 19:40:33 -05003179 if (ret < 0)
3180 goto out;
3181 p = reply_buf;
3182 end = (char *) reply_buf + size;
3183 image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL);
3184 if (IS_ERR(image_name))
3185 image_name = NULL;
3186 else
3187 dout("%s: name is %s len is %zd\n", __func__, image_name, len);
3188out:
3189 kfree(reply_buf);
3190 kfree(image_id);
3191
3192 return image_name;
3193}
3194
3195/*
3196 * When a parent image gets probed, we only have the pool, image,
3197 * and snapshot ids but not the names of any of them. This call
3198 * is made later to fill in those names. It has to be done after
3199 * rbd_dev_snaps_update() has completed because some of the
3200 * information (in particular, snapshot name) is not available
3201 * until then.
3202 */
3203static int rbd_dev_probe_update_spec(struct rbd_device *rbd_dev)
3204{
3205 struct ceph_osd_client *osdc;
3206 const char *name;
3207 void *reply_buf = NULL;
3208 int ret;
3209
3210 if (rbd_dev->spec->pool_name)
3211 return 0; /* Already have the names */
3212
3213 /* Look up the pool name */
3214
3215 osdc = &rbd_dev->rbd_client->client->osdc;
3216 name = ceph_pg_pool_name_by_id(osdc->osdmap, rbd_dev->spec->pool_id);
Alex Elder935dc892012-11-01 10:17:15 -05003217 if (!name) {
3218 rbd_warn(rbd_dev, "there is no pool with id %llu",
3219 rbd_dev->spec->pool_id); /* Really a BUG() */
3220 return -EIO;
3221 }
Alex Elder9e15b772012-10-30 19:40:33 -05003222
3223 rbd_dev->spec->pool_name = kstrdup(name, GFP_KERNEL);
3224 if (!rbd_dev->spec->pool_name)
3225 return -ENOMEM;
3226
3227 /* Fetch the image name; tolerate failure here */
3228
3229 name = rbd_dev_image_name(rbd_dev);
Alex Elder69e7a022012-11-01 08:39:26 -05003230 if (name)
Alex Elder9e15b772012-10-30 19:40:33 -05003231 rbd_dev->spec->image_name = (char *) name;
Alex Elder69e7a022012-11-01 08:39:26 -05003232 else
Alex Elder06ecc6c2012-11-01 10:17:15 -05003233 rbd_warn(rbd_dev, "unable to get image name");
Alex Elder9e15b772012-10-30 19:40:33 -05003234
3235 /* Look up the snapshot name. */
3236
3237 name = rbd_snap_name(rbd_dev, rbd_dev->spec->snap_id);
3238 if (!name) {
Alex Elder935dc892012-11-01 10:17:15 -05003239 rbd_warn(rbd_dev, "no snapshot with id %llu",
3240 rbd_dev->spec->snap_id); /* Really a BUG() */
Alex Elder9e15b772012-10-30 19:40:33 -05003241 ret = -EIO;
3242 goto out_err;
3243 }
3244 rbd_dev->spec->snap_name = kstrdup(name, GFP_KERNEL);
3245 if(!rbd_dev->spec->snap_name)
3246 goto out_err;
3247
3248 return 0;
3249out_err:
3250 kfree(reply_buf);
3251 kfree(rbd_dev->spec->pool_name);
3252 rbd_dev->spec->pool_name = NULL;
3253
3254 return ret;
3255}
3256
Alex Elder6e14b1a2012-07-03 16:01:19 -05003257static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev, u64 *ver)
Alex Elder35d489f2012-07-03 16:01:19 -05003258{
3259 size_t size;
3260 int ret;
3261 void *reply_buf;
3262 void *p;
3263 void *end;
3264 u64 seq;
3265 u32 snap_count;
3266 struct ceph_snap_context *snapc;
3267 u32 i;
3268
3269 /*
3270 * We'll need room for the seq value (maximum snapshot id),
3271 * snapshot count, and array of that many snapshot ids.
3272 * For now we have a fixed upper limit on the number we're
3273 * prepared to receive.
3274 */
3275 size = sizeof (__le64) + sizeof (__le32) +
3276 RBD_MAX_SNAP_COUNT * sizeof (__le64);
3277 reply_buf = kzalloc(size, GFP_KERNEL);
3278 if (!reply_buf)
3279 return -ENOMEM;
3280
Alex Elder36be9a72013-01-19 00:30:28 -06003281 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elder35d489f2012-07-03 16:01:19 -05003282 "rbd", "get_snapcontext",
3283 NULL, 0,
Alex Elder07b23912012-11-09 08:43:16 -06003284 reply_buf, size, ver);
Alex Elder36be9a72013-01-19 00:30:28 -06003285 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder35d489f2012-07-03 16:01:19 -05003286 if (ret < 0)
3287 goto out;
3288
3289 ret = -ERANGE;
3290 p = reply_buf;
3291 end = (char *) reply_buf + size;
3292 ceph_decode_64_safe(&p, end, seq, out);
3293 ceph_decode_32_safe(&p, end, snap_count, out);
3294
3295 /*
3296 * Make sure the reported number of snapshot ids wouldn't go
3297 * beyond the end of our buffer. But before checking that,
3298 * make sure the computed size of the snapshot context we
3299 * allocate is representable in a size_t.
3300 */
3301 if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
3302 / sizeof (u64)) {
3303 ret = -EINVAL;
3304 goto out;
3305 }
3306 if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
3307 goto out;
3308
3309 size = sizeof (struct ceph_snap_context) +
3310 snap_count * sizeof (snapc->snaps[0]);
3311 snapc = kmalloc(size, GFP_KERNEL);
3312 if (!snapc) {
3313 ret = -ENOMEM;
3314 goto out;
3315 }
3316
3317 atomic_set(&snapc->nref, 1);
3318 snapc->seq = seq;
3319 snapc->num_snaps = snap_count;
3320 for (i = 0; i < snap_count; i++)
3321 snapc->snaps[i] = ceph_decode_64(&p);
3322
3323 rbd_dev->header.snapc = snapc;
3324
3325 dout(" snap context seq = %llu, snap_count = %u\n",
3326 (unsigned long long) seq, (unsigned int) snap_count);
3327
3328out:
3329 kfree(reply_buf);
3330
3331 return 0;
3332}
3333
Alex Elderb8b1e2d2012-07-03 16:01:19 -05003334static char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, u32 which)
3335{
3336 size_t size;
3337 void *reply_buf;
3338 __le64 snap_id;
3339 int ret;
3340 void *p;
3341 void *end;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05003342 char *snap_name;
3343
3344 size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN;
3345 reply_buf = kmalloc(size, GFP_KERNEL);
3346 if (!reply_buf)
3347 return ERR_PTR(-ENOMEM);
3348
3349 snap_id = cpu_to_le64(rbd_dev->header.snapc->snaps[which]);
Alex Elder36be9a72013-01-19 00:30:28 -06003350 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elderb8b1e2d2012-07-03 16:01:19 -05003351 "rbd", "get_snapshot_name",
3352 (char *) &snap_id, sizeof (snap_id),
Alex Elder07b23912012-11-09 08:43:16 -06003353 reply_buf, size, NULL);
Alex Elder36be9a72013-01-19 00:30:28 -06003354 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05003355 if (ret < 0)
3356 goto out;
3357
3358 p = reply_buf;
3359 end = (char *) reply_buf + size;
Alex Eldere5c35532012-10-25 23:34:41 -05003360 snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05003361 if (IS_ERR(snap_name)) {
3362 ret = PTR_ERR(snap_name);
3363 goto out;
3364 } else {
3365 dout(" snap_id 0x%016llx snap_name = %s\n",
3366 (unsigned long long) le64_to_cpu(snap_id), snap_name);
3367 }
3368 kfree(reply_buf);
3369
3370 return snap_name;
3371out:
3372 kfree(reply_buf);
3373
3374 return ERR_PTR(ret);
3375}
3376
3377static char *rbd_dev_v2_snap_info(struct rbd_device *rbd_dev, u32 which,
3378 u64 *snap_size, u64 *snap_features)
3379{
Alex Eldere0b49862013-01-09 14:44:18 -06003380 u64 snap_id;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05003381 u8 order;
3382 int ret;
3383
3384 snap_id = rbd_dev->header.snapc->snaps[which];
3385 ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, &order, snap_size);
3386 if (ret)
3387 return ERR_PTR(ret);
3388 ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, snap_features);
3389 if (ret)
3390 return ERR_PTR(ret);
3391
3392 return rbd_dev_v2_snap_name(rbd_dev, which);
3393}
3394
3395static char *rbd_dev_snap_info(struct rbd_device *rbd_dev, u32 which,
3396 u64 *snap_size, u64 *snap_features)
3397{
3398 if (rbd_dev->image_format == 1)
3399 return rbd_dev_v1_snap_info(rbd_dev, which,
3400 snap_size, snap_features);
3401 if (rbd_dev->image_format == 2)
3402 return rbd_dev_v2_snap_info(rbd_dev, which,
3403 snap_size, snap_features);
3404 return ERR_PTR(-EINVAL);
3405}
3406
Alex Elder117973f2012-08-31 17:29:55 -05003407static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev, u64 *hver)
3408{
3409 int ret;
3410 __u8 obj_order;
3411
3412 down_write(&rbd_dev->header_rwsem);
3413
3414 /* Grab old order first, to see if it changes */
3415
3416 obj_order = rbd_dev->header.obj_order,
3417 ret = rbd_dev_v2_image_size(rbd_dev);
3418 if (ret)
3419 goto out;
3420 if (rbd_dev->header.obj_order != obj_order) {
3421 ret = -EIO;
3422 goto out;
3423 }
3424 rbd_update_mapping_size(rbd_dev);
3425
3426 ret = rbd_dev_v2_snap_context(rbd_dev, hver);
3427 dout("rbd_dev_v2_snap_context returned %d\n", ret);
3428 if (ret)
3429 goto out;
3430 ret = rbd_dev_snaps_update(rbd_dev);
3431 dout("rbd_dev_snaps_update returned %d\n", ret);
3432 if (ret)
3433 goto out;
3434 ret = rbd_dev_snaps_register(rbd_dev);
3435 dout("rbd_dev_snaps_register returned %d\n", ret);
3436out:
3437 up_write(&rbd_dev->header_rwsem);
3438
3439 return ret;
3440}
3441
Alex Elder9d475de2012-07-03 16:01:19 -05003442/*
Alex Elder35938152012-08-02 11:29:46 -05003443 * Scan the rbd device's current snapshot list and compare it to the
3444 * newly-received snapshot context. Remove any existing snapshots
3445 * not present in the new snapshot context. Add a new snapshot for
3446 * any snaphots in the snapshot context not in the current list.
3447 * And verify there are no changes to snapshots we already know
3448 * about.
3449 *
3450 * Assumes the snapshots in the snapshot context are sorted by
3451 * snapshot id, highest id first. (Snapshots in the rbd_dev's list
3452 * are also maintained in that order.)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003453 */
Alex Elder304f6802012-08-31 17:29:52 -05003454static int rbd_dev_snaps_update(struct rbd_device *rbd_dev)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003455{
Alex Elder35938152012-08-02 11:29:46 -05003456 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
3457 const u32 snap_count = snapc->num_snaps;
Alex Elder35938152012-08-02 11:29:46 -05003458 struct list_head *head = &rbd_dev->snaps;
3459 struct list_head *links = head->next;
3460 u32 index = 0;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003461
Alex Elder9fcbb802012-08-23 23:48:49 -05003462 dout("%s: snap count is %u\n", __func__, (unsigned int) snap_count);
Alex Elder35938152012-08-02 11:29:46 -05003463 while (index < snap_count || links != head) {
3464 u64 snap_id;
3465 struct rbd_snap *snap;
Alex Eldercd892122012-07-03 16:01:19 -05003466 char *snap_name;
3467 u64 snap_size = 0;
3468 u64 snap_features = 0;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003469
Alex Elder35938152012-08-02 11:29:46 -05003470 snap_id = index < snap_count ? snapc->snaps[index]
3471 : CEPH_NOSNAP;
3472 snap = links != head ? list_entry(links, struct rbd_snap, node)
3473 : NULL;
Alex Elderaafb2302012-09-06 16:00:54 -05003474 rbd_assert(!snap || snap->id != CEPH_NOSNAP);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003475
Alex Elder35938152012-08-02 11:29:46 -05003476 if (snap_id == CEPH_NOSNAP || (snap && snap->id > snap_id)) {
3477 struct list_head *next = links->next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003478
Alex Elder6d292902013-01-14 12:43:31 -06003479 /*
3480 * A previously-existing snapshot is not in
3481 * the new snap context.
3482 *
3483 * If the now missing snapshot is the one the
3484 * image is mapped to, clear its exists flag
3485 * so we can avoid sending any more requests
3486 * to it.
3487 */
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003488 if (rbd_dev->spec->snap_id == snap->id)
Alex Elder6d292902013-01-14 12:43:31 -06003489 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
Alex Elder41f38c22012-10-25 23:34:40 -05003490 rbd_remove_snap_dev(snap);
Alex Elder9fcbb802012-08-23 23:48:49 -05003491 dout("%ssnap id %llu has been removed\n",
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003492 rbd_dev->spec->snap_id == snap->id ?
3493 "mapped " : "",
Alex Elder9fcbb802012-08-23 23:48:49 -05003494 (unsigned long long) snap->id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003495
Alex Elder35938152012-08-02 11:29:46 -05003496 /* Done with this list entry; advance */
3497
3498 links = next;
3499 continue;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003500 }
Alex Elder35938152012-08-02 11:29:46 -05003501
Alex Elderb8b1e2d2012-07-03 16:01:19 -05003502 snap_name = rbd_dev_snap_info(rbd_dev, index,
3503 &snap_size, &snap_features);
Alex Eldercd892122012-07-03 16:01:19 -05003504 if (IS_ERR(snap_name))
3505 return PTR_ERR(snap_name);
3506
Alex Elder9fcbb802012-08-23 23:48:49 -05003507 dout("entry %u: snap_id = %llu\n", (unsigned int) snap_count,
3508 (unsigned long long) snap_id);
Alex Elder35938152012-08-02 11:29:46 -05003509 if (!snap || (snap_id != CEPH_NOSNAP && snap->id < snap_id)) {
3510 struct rbd_snap *new_snap;
3511
3512 /* We haven't seen this snapshot before */
3513
Alex Elderc8d18422012-07-10 20:30:11 -05003514 new_snap = __rbd_add_snap_dev(rbd_dev, snap_name,
Alex Eldercd892122012-07-03 16:01:19 -05003515 snap_id, snap_size, snap_features);
Alex Elder9fcbb802012-08-23 23:48:49 -05003516 if (IS_ERR(new_snap)) {
3517 int err = PTR_ERR(new_snap);
3518
3519 dout(" failed to add dev, error %d\n", err);
3520
3521 return err;
3522 }
Alex Elder35938152012-08-02 11:29:46 -05003523
3524 /* New goes before existing, or at end of list */
3525
Alex Elder9fcbb802012-08-23 23:48:49 -05003526 dout(" added dev%s\n", snap ? "" : " at end\n");
Alex Elder35938152012-08-02 11:29:46 -05003527 if (snap)
3528 list_add_tail(&new_snap->node, &snap->node);
3529 else
Alex Elder523f3252012-08-30 00:16:37 -05003530 list_add_tail(&new_snap->node, head);
Alex Elder35938152012-08-02 11:29:46 -05003531 } else {
3532 /* Already have this one */
3533
Alex Elder9fcbb802012-08-23 23:48:49 -05003534 dout(" already present\n");
3535
Alex Eldercd892122012-07-03 16:01:19 -05003536 rbd_assert(snap->size == snap_size);
Alex Elderaafb2302012-09-06 16:00:54 -05003537 rbd_assert(!strcmp(snap->name, snap_name));
Alex Eldercd892122012-07-03 16:01:19 -05003538 rbd_assert(snap->features == snap_features);
Alex Elder35938152012-08-02 11:29:46 -05003539
3540 /* Done with this list entry; advance */
3541
3542 links = links->next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003543 }
Alex Elder35938152012-08-02 11:29:46 -05003544
3545 /* Advance to the next entry in the snapshot context */
3546
3547 index++;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003548 }
Alex Elder9fcbb802012-08-23 23:48:49 -05003549 dout("%s: done\n", __func__);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003550
3551 return 0;
3552}
3553
Alex Elder304f6802012-08-31 17:29:52 -05003554/*
3555 * Scan the list of snapshots and register the devices for any that
3556 * have not already been registered.
3557 */
3558static int rbd_dev_snaps_register(struct rbd_device *rbd_dev)
3559{
3560 struct rbd_snap *snap;
3561 int ret = 0;
3562
Alex Elder37206ee2013-02-20 17:32:08 -06003563 dout("%s:\n", __func__);
Alex Elder86ff77b2012-08-31 17:29:53 -05003564 if (WARN_ON(!device_is_registered(&rbd_dev->dev)))
3565 return -EIO;
Alex Elder304f6802012-08-31 17:29:52 -05003566
3567 list_for_each_entry(snap, &rbd_dev->snaps, node) {
3568 if (!rbd_snap_registered(snap)) {
3569 ret = rbd_register_snap_dev(snap, &rbd_dev->dev);
3570 if (ret < 0)
3571 break;
3572 }
3573 }
3574 dout("%s: returning %d\n", __func__, ret);
3575
3576 return ret;
3577}
3578
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003579static int rbd_bus_add_dev(struct rbd_device *rbd_dev)
3580{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003581 struct device *dev;
Alex Eldercd789ab2012-08-30 00:16:38 -05003582 int ret;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003583
3584 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003585
Alex Eldercd789ab2012-08-30 00:16:38 -05003586 dev = &rbd_dev->dev;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003587 dev->bus = &rbd_bus_type;
3588 dev->type = &rbd_device_type;
3589 dev->parent = &rbd_root_dev;
3590 dev->release = rbd_dev_release;
Alex Elderde71a292012-07-03 16:01:19 -05003591 dev_set_name(dev, "%d", rbd_dev->dev_id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003592 ret = device_register(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003593
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003594 mutex_unlock(&ctl_mutex);
Alex Eldercd789ab2012-08-30 00:16:38 -05003595
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003596 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003597}
3598
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003599static void rbd_bus_del_dev(struct rbd_device *rbd_dev)
3600{
3601 device_unregister(&rbd_dev->dev);
3602}
3603
Alex Eldere2839302012-08-29 17:11:06 -05003604static atomic64_t rbd_dev_id_max = ATOMIC64_INIT(0);
Alex Elder1ddbe942012-01-29 13:57:44 -06003605
3606/*
Alex Elder499afd52012-02-02 08:13:29 -06003607 * Get a unique rbd identifier for the given new rbd_dev, and add
3608 * the rbd_dev to the global list. The minimum rbd id is 1.
Alex Elder1ddbe942012-01-29 13:57:44 -06003609 */
Alex Eldere2839302012-08-29 17:11:06 -05003610static void rbd_dev_id_get(struct rbd_device *rbd_dev)
Alex Elderb7f23c32012-01-29 13:57:43 -06003611{
Alex Eldere2839302012-08-29 17:11:06 -05003612 rbd_dev->dev_id = atomic64_inc_return(&rbd_dev_id_max);
Alex Elder499afd52012-02-02 08:13:29 -06003613
3614 spin_lock(&rbd_dev_list_lock);
3615 list_add_tail(&rbd_dev->node, &rbd_dev_list);
3616 spin_unlock(&rbd_dev_list_lock);
Alex Eldere2839302012-08-29 17:11:06 -05003617 dout("rbd_dev %p given dev id %llu\n", rbd_dev,
3618 (unsigned long long) rbd_dev->dev_id);
Alex Elder1ddbe942012-01-29 13:57:44 -06003619}
Alex Elderb7f23c32012-01-29 13:57:43 -06003620
Alex Elder1ddbe942012-01-29 13:57:44 -06003621/*
Alex Elder499afd52012-02-02 08:13:29 -06003622 * Remove an rbd_dev from the global list, and record that its
3623 * identifier is no longer in use.
Alex Elder1ddbe942012-01-29 13:57:44 -06003624 */
Alex Eldere2839302012-08-29 17:11:06 -05003625static void rbd_dev_id_put(struct rbd_device *rbd_dev)
Alex Elder1ddbe942012-01-29 13:57:44 -06003626{
Alex Elderd184f6b2012-01-29 13:57:44 -06003627 struct list_head *tmp;
Alex Elderde71a292012-07-03 16:01:19 -05003628 int rbd_id = rbd_dev->dev_id;
Alex Elderd184f6b2012-01-29 13:57:44 -06003629 int max_id;
3630
Alex Elderaafb2302012-09-06 16:00:54 -05003631 rbd_assert(rbd_id > 0);
Alex Elder499afd52012-02-02 08:13:29 -06003632
Alex Eldere2839302012-08-29 17:11:06 -05003633 dout("rbd_dev %p released dev id %llu\n", rbd_dev,
3634 (unsigned long long) rbd_dev->dev_id);
Alex Elder499afd52012-02-02 08:13:29 -06003635 spin_lock(&rbd_dev_list_lock);
3636 list_del_init(&rbd_dev->node);
Alex Elderd184f6b2012-01-29 13:57:44 -06003637
3638 /*
3639 * If the id being "put" is not the current maximum, there
3640 * is nothing special we need to do.
3641 */
Alex Eldere2839302012-08-29 17:11:06 -05003642 if (rbd_id != atomic64_read(&rbd_dev_id_max)) {
Alex Elderd184f6b2012-01-29 13:57:44 -06003643 spin_unlock(&rbd_dev_list_lock);
3644 return;
3645 }
3646
3647 /*
3648 * We need to update the current maximum id. Search the
3649 * list to find out what it is. We're more likely to find
3650 * the maximum at the end, so search the list backward.
3651 */
3652 max_id = 0;
3653 list_for_each_prev(tmp, &rbd_dev_list) {
3654 struct rbd_device *rbd_dev;
3655
3656 rbd_dev = list_entry(tmp, struct rbd_device, node);
Alex Elderb213e0b2012-10-10 21:19:13 -07003657 if (rbd_dev->dev_id > max_id)
3658 max_id = rbd_dev->dev_id;
Alex Elderd184f6b2012-01-29 13:57:44 -06003659 }
Alex Elder499afd52012-02-02 08:13:29 -06003660 spin_unlock(&rbd_dev_list_lock);
Alex Elderb7f23c32012-01-29 13:57:43 -06003661
Alex Elder1ddbe942012-01-29 13:57:44 -06003662 /*
Alex Eldere2839302012-08-29 17:11:06 -05003663 * The max id could have been updated by rbd_dev_id_get(), in
Alex Elderd184f6b2012-01-29 13:57:44 -06003664 * which case it now accurately reflects the new maximum.
3665 * Be careful not to overwrite the maximum value in that
3666 * case.
Alex Elder1ddbe942012-01-29 13:57:44 -06003667 */
Alex Eldere2839302012-08-29 17:11:06 -05003668 atomic64_cmpxchg(&rbd_dev_id_max, rbd_id, max_id);
3669 dout(" max dev id has been reset\n");
Alex Elderb7f23c32012-01-29 13:57:43 -06003670}
3671
Alex Eldera725f65e2012-02-02 08:13:30 -06003672/*
Alex Eldere28fff262012-02-02 08:13:30 -06003673 * Skips over white space at *buf, and updates *buf to point to the
3674 * first found non-space character (if any). Returns the length of
Alex Elder593a9e72012-02-07 12:03:37 -06003675 * the token (string of non-white space characters) found. Note
3676 * that *buf must be terminated with '\0'.
Alex Eldere28fff262012-02-02 08:13:30 -06003677 */
3678static inline size_t next_token(const char **buf)
3679{
3680 /*
3681 * These are the characters that produce nonzero for
3682 * isspace() in the "C" and "POSIX" locales.
3683 */
3684 const char *spaces = " \f\n\r\t\v";
3685
3686 *buf += strspn(*buf, spaces); /* Find start of token */
3687
3688 return strcspn(*buf, spaces); /* Return token length */
3689}
3690
3691/*
3692 * Finds the next token in *buf, and if the provided token buffer is
3693 * big enough, copies the found token into it. The result, if
Alex Elder593a9e72012-02-07 12:03:37 -06003694 * copied, is guaranteed to be terminated with '\0'. Note that *buf
3695 * must be terminated with '\0' on entry.
Alex Eldere28fff262012-02-02 08:13:30 -06003696 *
3697 * Returns the length of the token found (not including the '\0').
3698 * Return value will be 0 if no token is found, and it will be >=
3699 * token_size if the token would not fit.
3700 *
Alex Elder593a9e72012-02-07 12:03:37 -06003701 * The *buf pointer will be updated to point beyond the end of the
Alex Eldere28fff262012-02-02 08:13:30 -06003702 * found token. Note that this occurs even if the token buffer is
3703 * too small to hold it.
3704 */
3705static inline size_t copy_token(const char **buf,
3706 char *token,
3707 size_t token_size)
3708{
3709 size_t len;
3710
3711 len = next_token(buf);
3712 if (len < token_size) {
3713 memcpy(token, *buf, len);
3714 *(token + len) = '\0';
3715 }
3716 *buf += len;
3717
3718 return len;
3719}
3720
3721/*
Alex Elderea3352f2012-07-09 21:04:23 -05003722 * Finds the next token in *buf, dynamically allocates a buffer big
3723 * enough to hold a copy of it, and copies the token into the new
3724 * buffer. The copy is guaranteed to be terminated with '\0'. Note
3725 * that a duplicate buffer is created even for a zero-length token.
3726 *
3727 * Returns a pointer to the newly-allocated duplicate, or a null
3728 * pointer if memory for the duplicate was not available. If
3729 * the lenp argument is a non-null pointer, the length of the token
3730 * (not including the '\0') is returned in *lenp.
3731 *
3732 * If successful, the *buf pointer will be updated to point beyond
3733 * the end of the found token.
3734 *
3735 * Note: uses GFP_KERNEL for allocation.
3736 */
3737static inline char *dup_token(const char **buf, size_t *lenp)
3738{
3739 char *dup;
3740 size_t len;
3741
3742 len = next_token(buf);
Alex Elder4caf35f2012-11-01 08:39:27 -05003743 dup = kmemdup(*buf, len + 1, GFP_KERNEL);
Alex Elderea3352f2012-07-09 21:04:23 -05003744 if (!dup)
3745 return NULL;
Alex Elderea3352f2012-07-09 21:04:23 -05003746 *(dup + len) = '\0';
3747 *buf += len;
3748
3749 if (lenp)
3750 *lenp = len;
3751
3752 return dup;
3753}
3754
3755/*
Alex Elder859c31d2012-10-25 23:34:42 -05003756 * Parse the options provided for an "rbd add" (i.e., rbd image
3757 * mapping) request. These arrive via a write to /sys/bus/rbd/add,
3758 * and the data written is passed here via a NUL-terminated buffer.
3759 * Returns 0 if successful or an error code otherwise.
Alex Elderd22f76e2012-07-12 10:46:35 -05003760 *
Alex Elder859c31d2012-10-25 23:34:42 -05003761 * The information extracted from these options is recorded in
3762 * the other parameters which return dynamically-allocated
3763 * structures:
3764 * ceph_opts
3765 * The address of a pointer that will refer to a ceph options
3766 * structure. Caller must release the returned pointer using
3767 * ceph_destroy_options() when it is no longer needed.
3768 * rbd_opts
3769 * Address of an rbd options pointer. Fully initialized by
3770 * this function; caller must release with kfree().
3771 * spec
3772 * Address of an rbd image specification pointer. Fully
3773 * initialized by this function based on parsed options.
3774 * Caller must release with rbd_spec_put().
3775 *
3776 * The options passed take this form:
3777 * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
3778 * where:
3779 * <mon_addrs>
3780 * A comma-separated list of one or more monitor addresses.
3781 * A monitor address is an ip address, optionally followed
3782 * by a port number (separated by a colon).
3783 * I.e.: ip1[:port1][,ip2[:port2]...]
3784 * <options>
3785 * A comma-separated list of ceph and/or rbd options.
3786 * <pool_name>
3787 * The name of the rados pool containing the rbd image.
3788 * <image_name>
3789 * The name of the image in that pool to map.
3790 * <snap_id>
3791 * An optional snapshot id. If provided, the mapping will
3792 * present data from the image at the time that snapshot was
3793 * created. The image head is used if no snapshot id is
3794 * provided. Snapshot mappings are always read-only.
Alex Eldera725f65e2012-02-02 08:13:30 -06003795 */
Alex Elder859c31d2012-10-25 23:34:42 -05003796static int rbd_add_parse_args(const char *buf,
Alex Elderdc79b112012-10-25 23:34:41 -05003797 struct ceph_options **ceph_opts,
Alex Elder859c31d2012-10-25 23:34:42 -05003798 struct rbd_options **opts,
3799 struct rbd_spec **rbd_spec)
Alex Eldera725f65e2012-02-02 08:13:30 -06003800{
Alex Elderd22f76e2012-07-12 10:46:35 -05003801 size_t len;
Alex Elder859c31d2012-10-25 23:34:42 -05003802 char *options;
Alex Elder0ddebc02012-10-25 23:34:41 -05003803 const char *mon_addrs;
3804 size_t mon_addrs_size;
Alex Elder859c31d2012-10-25 23:34:42 -05003805 struct rbd_spec *spec = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05003806 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05003807 struct ceph_options *copts;
Alex Elderdc79b112012-10-25 23:34:41 -05003808 int ret;
Alex Eldere28fff262012-02-02 08:13:30 -06003809
3810 /* The first four tokens are required */
3811
Alex Elder7ef32142012-02-02 08:13:30 -06003812 len = next_token(&buf);
Alex Elder4fb5d6712012-11-01 10:17:15 -05003813 if (!len) {
3814 rbd_warn(NULL, "no monitor address(es) provided");
3815 return -EINVAL;
3816 }
Alex Elder0ddebc02012-10-25 23:34:41 -05003817 mon_addrs = buf;
Alex Elderf28e5652012-10-25 23:34:41 -05003818 mon_addrs_size = len + 1;
Alex Elder7ef32142012-02-02 08:13:30 -06003819 buf += len;
Alex Eldera725f65e2012-02-02 08:13:30 -06003820
Alex Elderdc79b112012-10-25 23:34:41 -05003821 ret = -EINVAL;
Alex Elderf28e5652012-10-25 23:34:41 -05003822 options = dup_token(&buf, NULL);
3823 if (!options)
Alex Elderdc79b112012-10-25 23:34:41 -05003824 return -ENOMEM;
Alex Elder4fb5d6712012-11-01 10:17:15 -05003825 if (!*options) {
3826 rbd_warn(NULL, "no options provided");
3827 goto out_err;
3828 }
Alex Eldera725f65e2012-02-02 08:13:30 -06003829
Alex Elder859c31d2012-10-25 23:34:42 -05003830 spec = rbd_spec_alloc();
3831 if (!spec)
Alex Elderf28e5652012-10-25 23:34:41 -05003832 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05003833
3834 spec->pool_name = dup_token(&buf, NULL);
3835 if (!spec->pool_name)
3836 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05003837 if (!*spec->pool_name) {
3838 rbd_warn(NULL, "no pool name provided");
3839 goto out_err;
3840 }
Alex Eldere28fff262012-02-02 08:13:30 -06003841
Alex Elder69e7a022012-11-01 08:39:26 -05003842 spec->image_name = dup_token(&buf, NULL);
Alex Elder859c31d2012-10-25 23:34:42 -05003843 if (!spec->image_name)
Alex Elderf28e5652012-10-25 23:34:41 -05003844 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05003845 if (!*spec->image_name) {
3846 rbd_warn(NULL, "no image name provided");
3847 goto out_err;
3848 }
Alex Eldere28fff262012-02-02 08:13:30 -06003849
Alex Elderf28e5652012-10-25 23:34:41 -05003850 /*
3851 * Snapshot name is optional; default is to use "-"
3852 * (indicating the head/no snapshot).
3853 */
Alex Elder3feeb8942012-08-31 17:29:52 -05003854 len = next_token(&buf);
Alex Elder820a5f32012-07-09 21:04:24 -05003855 if (!len) {
Alex Elder3feeb8942012-08-31 17:29:52 -05003856 buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
3857 len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
Alex Elderf28e5652012-10-25 23:34:41 -05003858 } else if (len > RBD_MAX_SNAP_NAME_LEN) {
Alex Elderdc79b112012-10-25 23:34:41 -05003859 ret = -ENAMETOOLONG;
Alex Elderf28e5652012-10-25 23:34:41 -05003860 goto out_err;
Alex Elder849b4262012-07-09 21:04:24 -05003861 }
Alex Elder4caf35f2012-11-01 08:39:27 -05003862 spec->snap_name = kmemdup(buf, len + 1, GFP_KERNEL);
Alex Elder859c31d2012-10-25 23:34:42 -05003863 if (!spec->snap_name)
Alex Elderf28e5652012-10-25 23:34:41 -05003864 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05003865 *(spec->snap_name + len) = '\0';
Alex Eldere5c35532012-10-25 23:34:41 -05003866
Alex Elder0ddebc02012-10-25 23:34:41 -05003867 /* Initialize all rbd options to the defaults */
Alex Eldere28fff262012-02-02 08:13:30 -06003868
Alex Elder4e9afeb2012-10-25 23:34:41 -05003869 rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL);
3870 if (!rbd_opts)
3871 goto out_mem;
3872
3873 rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
Alex Elderd22f76e2012-07-12 10:46:35 -05003874
Alex Elder859c31d2012-10-25 23:34:42 -05003875 copts = ceph_parse_options(options, mon_addrs,
Alex Elder0ddebc02012-10-25 23:34:41 -05003876 mon_addrs + mon_addrs_size - 1,
Alex Elder4e9afeb2012-10-25 23:34:41 -05003877 parse_rbd_opts_token, rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05003878 if (IS_ERR(copts)) {
3879 ret = PTR_ERR(copts);
Alex Elderdc79b112012-10-25 23:34:41 -05003880 goto out_err;
3881 }
Alex Elder859c31d2012-10-25 23:34:42 -05003882 kfree(options);
3883
3884 *ceph_opts = copts;
Alex Elder4e9afeb2012-10-25 23:34:41 -05003885 *opts = rbd_opts;
Alex Elder859c31d2012-10-25 23:34:42 -05003886 *rbd_spec = spec;
Alex Elder0ddebc02012-10-25 23:34:41 -05003887
Alex Elderdc79b112012-10-25 23:34:41 -05003888 return 0;
Alex Elderf28e5652012-10-25 23:34:41 -05003889out_mem:
Alex Elderdc79b112012-10-25 23:34:41 -05003890 ret = -ENOMEM;
Alex Elderd22f76e2012-07-12 10:46:35 -05003891out_err:
Alex Elder859c31d2012-10-25 23:34:42 -05003892 kfree(rbd_opts);
3893 rbd_spec_put(spec);
Alex Elderf28e5652012-10-25 23:34:41 -05003894 kfree(options);
Alex Elderd22f76e2012-07-12 10:46:35 -05003895
Alex Elderdc79b112012-10-25 23:34:41 -05003896 return ret;
Alex Eldera725f65e2012-02-02 08:13:30 -06003897}
3898
Alex Elder589d30e2012-07-10 20:30:11 -05003899/*
3900 * An rbd format 2 image has a unique identifier, distinct from the
3901 * name given to it by the user. Internally, that identifier is
3902 * what's used to specify the names of objects related to the image.
3903 *
3904 * A special "rbd id" object is used to map an rbd image name to its
3905 * id. If that object doesn't exist, then there is no v2 rbd image
3906 * with the supplied name.
3907 *
3908 * This function will record the given rbd_dev's image_id field if
3909 * it can be determined, and in that case will return 0. If any
3910 * errors occur a negative errno will be returned and the rbd_dev's
3911 * image_id field will be unchanged (and should be NULL).
3912 */
3913static int rbd_dev_image_id(struct rbd_device *rbd_dev)
3914{
3915 int ret;
3916 size_t size;
3917 char *object_name;
3918 void *response;
3919 void *p;
3920
Alex Elder2f82ee52012-10-30 19:40:33 -05003921 /* If we already have it we don't need to look it up */
3922
3923 if (rbd_dev->spec->image_id)
3924 return 0;
3925
Alex Elder589d30e2012-07-10 20:30:11 -05003926 /*
Alex Elder2c0d0a12012-10-30 19:40:33 -05003927 * When probing a parent image, the image id is already
3928 * known (and the image name likely is not). There's no
3929 * need to fetch the image id again in this case.
3930 */
3931 if (rbd_dev->spec->image_id)
3932 return 0;
3933
3934 /*
Alex Elder589d30e2012-07-10 20:30:11 -05003935 * First, see if the format 2 image id file exists, and if
3936 * so, get the image's persistent id from it.
3937 */
Alex Elder69e7a022012-11-01 08:39:26 -05003938 size = sizeof (RBD_ID_PREFIX) + strlen(rbd_dev->spec->image_name);
Alex Elder589d30e2012-07-10 20:30:11 -05003939 object_name = kmalloc(size, GFP_NOIO);
3940 if (!object_name)
3941 return -ENOMEM;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003942 sprintf(object_name, "%s%s", RBD_ID_PREFIX, rbd_dev->spec->image_name);
Alex Elder589d30e2012-07-10 20:30:11 -05003943 dout("rbd id object name is %s\n", object_name);
3944
3945 /* Response will be an encoded string, which includes a length */
3946
3947 size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
3948 response = kzalloc(size, GFP_NOIO);
3949 if (!response) {
3950 ret = -ENOMEM;
3951 goto out;
3952 }
3953
Alex Elder36be9a72013-01-19 00:30:28 -06003954 ret = rbd_obj_method_sync(rbd_dev, object_name,
Alex Elder589d30e2012-07-10 20:30:11 -05003955 "rbd", "get_id",
3956 NULL, 0,
Alex Elder07b23912012-11-09 08:43:16 -06003957 response, RBD_IMAGE_ID_LEN_MAX, NULL);
Alex Elder36be9a72013-01-19 00:30:28 -06003958 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder589d30e2012-07-10 20:30:11 -05003959 if (ret < 0)
3960 goto out;
3961
3962 p = response;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003963 rbd_dev->spec->image_id = ceph_extract_encoded_string(&p,
Alex Elder589d30e2012-07-10 20:30:11 -05003964 p + RBD_IMAGE_ID_LEN_MAX,
Alex Elder979ed482012-11-01 08:39:26 -05003965 NULL, GFP_NOIO);
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003966 if (IS_ERR(rbd_dev->spec->image_id)) {
3967 ret = PTR_ERR(rbd_dev->spec->image_id);
3968 rbd_dev->spec->image_id = NULL;
Alex Elder589d30e2012-07-10 20:30:11 -05003969 } else {
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003970 dout("image_id is %s\n", rbd_dev->spec->image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05003971 }
3972out:
3973 kfree(response);
3974 kfree(object_name);
3975
3976 return ret;
3977}
3978
Alex Eldera30b71b2012-07-10 20:30:11 -05003979static int rbd_dev_v1_probe(struct rbd_device *rbd_dev)
3980{
3981 int ret;
3982 size_t size;
3983
3984 /* Version 1 images have no id; empty string is used */
3985
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003986 rbd_dev->spec->image_id = kstrdup("", GFP_KERNEL);
3987 if (!rbd_dev->spec->image_id)
Alex Eldera30b71b2012-07-10 20:30:11 -05003988 return -ENOMEM;
Alex Eldera30b71b2012-07-10 20:30:11 -05003989
3990 /* Record the header object name for this rbd image. */
3991
Alex Elder69e7a022012-11-01 08:39:26 -05003992 size = strlen(rbd_dev->spec->image_name) + sizeof (RBD_SUFFIX);
Alex Eldera30b71b2012-07-10 20:30:11 -05003993 rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
3994 if (!rbd_dev->header_name) {
3995 ret = -ENOMEM;
3996 goto out_err;
3997 }
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003998 sprintf(rbd_dev->header_name, "%s%s",
3999 rbd_dev->spec->image_name, RBD_SUFFIX);
Alex Eldera30b71b2012-07-10 20:30:11 -05004000
4001 /* Populate rbd image metadata */
4002
4003 ret = rbd_read_header(rbd_dev, &rbd_dev->header);
4004 if (ret < 0)
4005 goto out_err;
Alex Elder86b00e02012-10-25 23:34:42 -05004006
4007 /* Version 1 images have no parent (no layering) */
4008
4009 rbd_dev->parent_spec = NULL;
4010 rbd_dev->parent_overlap = 0;
4011
Alex Eldera30b71b2012-07-10 20:30:11 -05004012 rbd_dev->image_format = 1;
4013
4014 dout("discovered version 1 image, header name is %s\n",
4015 rbd_dev->header_name);
4016
4017 return 0;
4018
4019out_err:
4020 kfree(rbd_dev->header_name);
4021 rbd_dev->header_name = NULL;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004022 kfree(rbd_dev->spec->image_id);
4023 rbd_dev->spec->image_id = NULL;
Alex Eldera30b71b2012-07-10 20:30:11 -05004024
4025 return ret;
4026}
4027
4028static int rbd_dev_v2_probe(struct rbd_device *rbd_dev)
4029{
4030 size_t size;
Alex Elder9d475de2012-07-03 16:01:19 -05004031 int ret;
Alex Elder6e14b1a2012-07-03 16:01:19 -05004032 u64 ver = 0;
Alex Eldera30b71b2012-07-10 20:30:11 -05004033
4034 /*
4035 * Image id was filled in by the caller. Record the header
4036 * object name for this rbd image.
4037 */
Alex Elder979ed482012-11-01 08:39:26 -05004038 size = sizeof (RBD_HEADER_PREFIX) + strlen(rbd_dev->spec->image_id);
Alex Eldera30b71b2012-07-10 20:30:11 -05004039 rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
4040 if (!rbd_dev->header_name)
4041 return -ENOMEM;
4042 sprintf(rbd_dev->header_name, "%s%s",
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004043 RBD_HEADER_PREFIX, rbd_dev->spec->image_id);
Alex Elder9d475de2012-07-03 16:01:19 -05004044
4045 /* Get the size and object order for the image */
4046
4047 ret = rbd_dev_v2_image_size(rbd_dev);
4048 if (ret < 0)
4049 goto out_err;
Alex Elder1e130192012-07-03 16:01:19 -05004050
4051 /* Get the object prefix (a.k.a. block_name) for the image */
4052
4053 ret = rbd_dev_v2_object_prefix(rbd_dev);
4054 if (ret < 0)
4055 goto out_err;
Alex Elderb1b54022012-07-03 16:01:19 -05004056
Alex Elderd8891402012-10-09 13:50:17 -07004057 /* Get the and check features for the image */
Alex Elderb1b54022012-07-03 16:01:19 -05004058
4059 ret = rbd_dev_v2_features(rbd_dev);
4060 if (ret < 0)
4061 goto out_err;
Alex Elder35d489f2012-07-03 16:01:19 -05004062
Alex Elder86b00e02012-10-25 23:34:42 -05004063 /* If the image supports layering, get the parent info */
4064
4065 if (rbd_dev->header.features & RBD_FEATURE_LAYERING) {
4066 ret = rbd_dev_v2_parent_info(rbd_dev);
4067 if (ret < 0)
4068 goto out_err;
4069 }
4070
Alex Elder6e14b1a2012-07-03 16:01:19 -05004071 /* crypto and compression type aren't (yet) supported for v2 images */
Alex Elder35d489f2012-07-03 16:01:19 -05004072
Alex Elder6e14b1a2012-07-03 16:01:19 -05004073 rbd_dev->header.crypt_type = 0;
4074 rbd_dev->header.comp_type = 0;
4075
4076 /* Get the snapshot context, plus the header version */
4077
4078 ret = rbd_dev_v2_snap_context(rbd_dev, &ver);
Alex Elder35d489f2012-07-03 16:01:19 -05004079 if (ret)
4080 goto out_err;
Alex Elder6e14b1a2012-07-03 16:01:19 -05004081 rbd_dev->header.obj_version = ver;
4082
Alex Eldera30b71b2012-07-10 20:30:11 -05004083 rbd_dev->image_format = 2;
4084
4085 dout("discovered version 2 image, header name is %s\n",
4086 rbd_dev->header_name);
4087
Alex Elder35152972012-08-31 17:29:55 -05004088 return 0;
Alex Elder9d475de2012-07-03 16:01:19 -05004089out_err:
Alex Elder86b00e02012-10-25 23:34:42 -05004090 rbd_dev->parent_overlap = 0;
4091 rbd_spec_put(rbd_dev->parent_spec);
4092 rbd_dev->parent_spec = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05004093 kfree(rbd_dev->header_name);
4094 rbd_dev->header_name = NULL;
Alex Elder1e130192012-07-03 16:01:19 -05004095 kfree(rbd_dev->header.object_prefix);
4096 rbd_dev->header.object_prefix = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05004097
4098 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05004099}
4100
Alex Elder83a06262012-10-30 15:47:17 -05004101static int rbd_dev_probe_finish(struct rbd_device *rbd_dev)
4102{
Alex Elder2f82ee52012-10-30 19:40:33 -05004103 struct rbd_device *parent = NULL;
4104 struct rbd_spec *parent_spec = NULL;
4105 struct rbd_client *rbdc = NULL;
Alex Elder83a06262012-10-30 15:47:17 -05004106 int ret;
4107
4108 /* no need to lock here, as rbd_dev is not registered yet */
4109 ret = rbd_dev_snaps_update(rbd_dev);
4110 if (ret)
4111 return ret;
4112
Alex Elder9e15b772012-10-30 19:40:33 -05004113 ret = rbd_dev_probe_update_spec(rbd_dev);
4114 if (ret)
4115 goto err_out_snaps;
4116
Alex Elder83a06262012-10-30 15:47:17 -05004117 ret = rbd_dev_set_mapping(rbd_dev);
4118 if (ret)
4119 goto err_out_snaps;
4120
4121 /* generate unique id: find highest unique id, add one */
4122 rbd_dev_id_get(rbd_dev);
4123
4124 /* Fill in the device name, now that we have its id. */
4125 BUILD_BUG_ON(DEV_NAME_LEN
4126 < sizeof (RBD_DRV_NAME) + MAX_INT_FORMAT_WIDTH);
4127 sprintf(rbd_dev->name, "%s%d", RBD_DRV_NAME, rbd_dev->dev_id);
4128
4129 /* Get our block major device number. */
4130
4131 ret = register_blkdev(0, rbd_dev->name);
4132 if (ret < 0)
4133 goto err_out_id;
4134 rbd_dev->major = ret;
4135
4136 /* Set up the blkdev mapping. */
4137
4138 ret = rbd_init_disk(rbd_dev);
4139 if (ret)
4140 goto err_out_blkdev;
4141
4142 ret = rbd_bus_add_dev(rbd_dev);
4143 if (ret)
4144 goto err_out_disk;
4145
4146 /*
4147 * At this point cleanup in the event of an error is the job
4148 * of the sysfs code (initiated by rbd_bus_del_dev()).
4149 */
Alex Elder2f82ee52012-10-30 19:40:33 -05004150 /* Probe the parent if there is one */
4151
4152 if (rbd_dev->parent_spec) {
4153 /*
4154 * We need to pass a reference to the client and the
4155 * parent spec when creating the parent rbd_dev.
4156 * Images related by parent/child relationships
4157 * always share both.
4158 */
4159 parent_spec = rbd_spec_get(rbd_dev->parent_spec);
4160 rbdc = __rbd_get_client(rbd_dev->rbd_client);
4161
4162 parent = rbd_dev_create(rbdc, parent_spec);
4163 if (!parent) {
4164 ret = -ENOMEM;
4165 goto err_out_spec;
4166 }
4167 rbdc = NULL; /* parent now owns reference */
4168 parent_spec = NULL; /* parent now owns reference */
4169 ret = rbd_dev_probe(parent);
4170 if (ret < 0)
4171 goto err_out_parent;
4172 rbd_dev->parent = parent;
4173 }
4174
Alex Elder83a06262012-10-30 15:47:17 -05004175 down_write(&rbd_dev->header_rwsem);
4176 ret = rbd_dev_snaps_register(rbd_dev);
4177 up_write(&rbd_dev->header_rwsem);
4178 if (ret)
4179 goto err_out_bus;
4180
Alex Elder9969ebc2013-01-18 12:31:10 -06004181 ret = rbd_dev_header_watch_sync(rbd_dev, 1);
Alex Elder83a06262012-10-30 15:47:17 -05004182 if (ret)
4183 goto err_out_bus;
4184
4185 /* Everything's ready. Announce the disk to the world. */
4186
4187 add_disk(rbd_dev->disk);
4188
4189 pr_info("%s: added with size 0x%llx\n", rbd_dev->disk->disk_name,
4190 (unsigned long long) rbd_dev->mapping.size);
4191
4192 return ret;
Alex Elder2f82ee52012-10-30 19:40:33 -05004193
4194err_out_parent:
4195 rbd_dev_destroy(parent);
4196err_out_spec:
4197 rbd_spec_put(parent_spec);
4198 rbd_put_client(rbdc);
Alex Elder83a06262012-10-30 15:47:17 -05004199err_out_bus:
4200 /* this will also clean up rest of rbd_dev stuff */
4201
4202 rbd_bus_del_dev(rbd_dev);
4203
4204 return ret;
4205err_out_disk:
4206 rbd_free_disk(rbd_dev);
4207err_out_blkdev:
4208 unregister_blkdev(rbd_dev->major, rbd_dev->name);
4209err_out_id:
4210 rbd_dev_id_put(rbd_dev);
4211err_out_snaps:
4212 rbd_remove_all_snaps(rbd_dev);
4213
4214 return ret;
4215}
4216
Alex Eldera30b71b2012-07-10 20:30:11 -05004217/*
4218 * Probe for the existence of the header object for the given rbd
4219 * device. For format 2 images this includes determining the image
4220 * id.
4221 */
4222static int rbd_dev_probe(struct rbd_device *rbd_dev)
4223{
4224 int ret;
4225
4226 /*
4227 * Get the id from the image id object. If it's not a
4228 * format 2 image, we'll get ENOENT back, and we'll assume
4229 * it's a format 1 image.
4230 */
4231 ret = rbd_dev_image_id(rbd_dev);
4232 if (ret)
4233 ret = rbd_dev_v1_probe(rbd_dev);
4234 else
4235 ret = rbd_dev_v2_probe(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05004236 if (ret) {
Alex Eldera30b71b2012-07-10 20:30:11 -05004237 dout("probe failed, returning %d\n", ret);
4238
Alex Elder83a06262012-10-30 15:47:17 -05004239 return ret;
4240 }
4241
4242 ret = rbd_dev_probe_finish(rbd_dev);
4243 if (ret)
4244 rbd_header_free(&rbd_dev->header);
4245
Alex Eldera30b71b2012-07-10 20:30:11 -05004246 return ret;
4247}
4248
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07004249static ssize_t rbd_add(struct bus_type *bus,
4250 const char *buf,
4251 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004252{
Alex Eldercb8627c2012-07-09 21:04:23 -05004253 struct rbd_device *rbd_dev = NULL;
Alex Elderdc79b112012-10-25 23:34:41 -05004254 struct ceph_options *ceph_opts = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05004255 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05004256 struct rbd_spec *spec = NULL;
Alex Elder9d3997f2012-10-25 23:34:42 -05004257 struct rbd_client *rbdc;
Alex Elder27cc2592012-02-02 08:13:30 -06004258 struct ceph_osd_client *osdc;
4259 int rc = -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004260
4261 if (!try_module_get(THIS_MODULE))
4262 return -ENODEV;
4263
Alex Eldera725f65e2012-02-02 08:13:30 -06004264 /* parse add command */
Alex Elder859c31d2012-10-25 23:34:42 -05004265 rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec);
Alex Elderdc79b112012-10-25 23:34:41 -05004266 if (rc < 0)
Alex Elderbd4ba652012-10-25 23:34:42 -05004267 goto err_out_module;
Alex Eldera725f65e2012-02-02 08:13:30 -06004268
Alex Elder9d3997f2012-10-25 23:34:42 -05004269 rbdc = rbd_get_client(ceph_opts);
4270 if (IS_ERR(rbdc)) {
4271 rc = PTR_ERR(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05004272 goto err_out_args;
Alex Elder9d3997f2012-10-25 23:34:42 -05004273 }
Alex Elderc53d5892012-10-25 23:34:42 -05004274 ceph_opts = NULL; /* rbd_dev client now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004275
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004276 /* pick the pool */
Alex Elder9d3997f2012-10-25 23:34:42 -05004277 osdc = &rbdc->client->osdc;
Alex Elder859c31d2012-10-25 23:34:42 -05004278 rc = ceph_pg_poolid_by_name(osdc->osdmap, spec->pool_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004279 if (rc < 0)
4280 goto err_out_client;
Alex Elder859c31d2012-10-25 23:34:42 -05004281 spec->pool_id = (u64) rc;
4282
Alex Elder0903e872012-11-14 12:25:19 -06004283 /* The ceph file layout needs to fit pool id in 32 bits */
4284
4285 if (WARN_ON(spec->pool_id > (u64) U32_MAX)) {
4286 rc = -EIO;
4287 goto err_out_client;
4288 }
4289
Alex Elderc53d5892012-10-25 23:34:42 -05004290 rbd_dev = rbd_dev_create(rbdc, spec);
Alex Elderbd4ba652012-10-25 23:34:42 -05004291 if (!rbd_dev)
4292 goto err_out_client;
Alex Elderc53d5892012-10-25 23:34:42 -05004293 rbdc = NULL; /* rbd_dev now owns this */
4294 spec = NULL; /* rbd_dev now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004295
Alex Elderbd4ba652012-10-25 23:34:42 -05004296 rbd_dev->mapping.read_only = rbd_opts->read_only;
Alex Elderc53d5892012-10-25 23:34:42 -05004297 kfree(rbd_opts);
4298 rbd_opts = NULL; /* done with this */
Alex Elderbd4ba652012-10-25 23:34:42 -05004299
Alex Eldera30b71b2012-07-10 20:30:11 -05004300 rc = rbd_dev_probe(rbd_dev);
4301 if (rc < 0)
Alex Elderc53d5892012-10-25 23:34:42 -05004302 goto err_out_rbd_dev;
Alex Elder05fd6f62012-08-29 17:11:07 -05004303
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004304 return count;
Alex Elderc53d5892012-10-25 23:34:42 -05004305err_out_rbd_dev:
4306 rbd_dev_destroy(rbd_dev);
Alex Elderbd4ba652012-10-25 23:34:42 -05004307err_out_client:
Alex Elder9d3997f2012-10-25 23:34:42 -05004308 rbd_put_client(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05004309err_out_args:
Alex Elder78cea762012-10-25 23:34:41 -05004310 if (ceph_opts)
4311 ceph_destroy_options(ceph_opts);
Alex Elder4e9afeb2012-10-25 23:34:41 -05004312 kfree(rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05004313 rbd_spec_put(spec);
Alex Elderbd4ba652012-10-25 23:34:42 -05004314err_out_module:
4315 module_put(THIS_MODULE);
Alex Elder27cc2592012-02-02 08:13:30 -06004316
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004317 dout("Error adding device %s\n", buf);
Alex Elder27cc2592012-02-02 08:13:30 -06004318
4319 return (ssize_t) rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004320}
4321
Alex Elderde71a292012-07-03 16:01:19 -05004322static struct rbd_device *__rbd_get_dev(unsigned long dev_id)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004323{
4324 struct list_head *tmp;
4325 struct rbd_device *rbd_dev;
4326
Alex Eldere124a82f2012-01-29 13:57:44 -06004327 spin_lock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004328 list_for_each(tmp, &rbd_dev_list) {
4329 rbd_dev = list_entry(tmp, struct rbd_device, node);
Alex Elderde71a292012-07-03 16:01:19 -05004330 if (rbd_dev->dev_id == dev_id) {
Alex Eldere124a82f2012-01-29 13:57:44 -06004331 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004332 return rbd_dev;
Alex Eldere124a82f2012-01-29 13:57:44 -06004333 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004334 }
Alex Eldere124a82f2012-01-29 13:57:44 -06004335 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004336 return NULL;
4337}
4338
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004339static void rbd_dev_release(struct device *dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004340{
Alex Elder593a9e72012-02-07 12:03:37 -06004341 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004342
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07004343 if (rbd_dev->watch_event)
Alex Elder9969ebc2013-01-18 12:31:10 -06004344 rbd_dev_header_watch_sync(rbd_dev, 0);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004345
4346 /* clean up and free blkdev */
4347 rbd_free_disk(rbd_dev);
4348 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Alex Elder32eec682012-02-08 16:11:14 -06004349
Alex Elder2ac4e752012-07-10 20:30:10 -05004350 /* release allocated disk header fields */
4351 rbd_header_free(&rbd_dev->header);
4352
Alex Elder32eec682012-02-08 16:11:14 -06004353 /* done with the id, and with the rbd_dev */
Alex Eldere2839302012-08-29 17:11:06 -05004354 rbd_dev_id_put(rbd_dev);
Alex Elderc53d5892012-10-25 23:34:42 -05004355 rbd_assert(rbd_dev->rbd_client != NULL);
4356 rbd_dev_destroy(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004357
4358 /* release module ref */
4359 module_put(THIS_MODULE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004360}
4361
Alex Elder2f82ee52012-10-30 19:40:33 -05004362static void __rbd_remove(struct rbd_device *rbd_dev)
4363{
4364 rbd_remove_all_snaps(rbd_dev);
4365 rbd_bus_del_dev(rbd_dev);
4366}
4367
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004368static ssize_t rbd_remove(struct bus_type *bus,
4369 const char *buf,
4370 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004371{
4372 struct rbd_device *rbd_dev = NULL;
4373 int target_id, rc;
4374 unsigned long ul;
4375 int ret = count;
4376
4377 rc = strict_strtoul(buf, 10, &ul);
4378 if (rc)
4379 return rc;
4380
4381 /* convert to int; abort if we lost anything in the conversion */
4382 target_id = (int) ul;
4383 if (target_id != ul)
4384 return -EINVAL;
4385
4386 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
4387
4388 rbd_dev = __rbd_get_dev(target_id);
4389 if (!rbd_dev) {
4390 ret = -ENOENT;
4391 goto done;
4392 }
4393
Alex Eldera14ea262013-02-05 13:23:12 -06004394 spin_lock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -06004395 if (rbd_dev->open_count)
Alex Elder42382b72012-11-16 09:29:16 -06004396 ret = -EBUSY;
Alex Elderb82d1672013-01-14 12:43:31 -06004397 else
4398 set_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags);
Alex Eldera14ea262013-02-05 13:23:12 -06004399 spin_unlock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -06004400 if (ret < 0)
Alex Elder42382b72012-11-16 09:29:16 -06004401 goto done;
Alex Elder42382b72012-11-16 09:29:16 -06004402
Alex Elder2f82ee52012-10-30 19:40:33 -05004403 while (rbd_dev->parent_spec) {
4404 struct rbd_device *first = rbd_dev;
4405 struct rbd_device *second = first->parent;
4406 struct rbd_device *third;
4407
4408 /*
4409 * Follow to the parent with no grandparent and
4410 * remove it.
4411 */
4412 while (second && (third = second->parent)) {
4413 first = second;
4414 second = third;
4415 }
4416 __rbd_remove(second);
4417 rbd_spec_put(first->parent_spec);
4418 first->parent_spec = NULL;
4419 first->parent_overlap = 0;
4420 first->parent = NULL;
4421 }
4422 __rbd_remove(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004423
4424done:
4425 mutex_unlock(&ctl_mutex);
Alex Elderaafb2302012-09-06 16:00:54 -05004426
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004427 return ret;
4428}
4429
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004430/*
4431 * create control files in sysfs
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004432 * /sys/bus/rbd/...
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004433 */
4434static int rbd_sysfs_init(void)
4435{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004436 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004437
Alex Elderfed4c142012-02-07 12:03:36 -06004438 ret = device_register(&rbd_root_dev);
Alex Elder21079782012-01-24 10:08:36 -06004439 if (ret < 0)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004440 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004441
Alex Elderfed4c142012-02-07 12:03:36 -06004442 ret = bus_register(&rbd_bus_type);
4443 if (ret < 0)
4444 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004445
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004446 return ret;
4447}
4448
4449static void rbd_sysfs_cleanup(void)
4450{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004451 bus_unregister(&rbd_bus_type);
Alex Elderfed4c142012-02-07 12:03:36 -06004452 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004453}
4454
Alex Eldercc344fa2013-02-19 12:25:56 -06004455static int __init rbd_init(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004456{
4457 int rc;
4458
Alex Elder1e32d342013-01-30 11:13:33 -06004459 if (!libceph_compatible(NULL)) {
4460 rbd_warn(NULL, "libceph incompatibility (quitting)");
4461
4462 return -EINVAL;
4463 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004464 rc = rbd_sysfs_init();
4465 if (rc)
4466 return rc;
Alex Elderf0f8cef2012-01-29 13:57:44 -06004467 pr_info("loaded " RBD_DRV_NAME_LONG "\n");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004468 return 0;
4469}
4470
Alex Eldercc344fa2013-02-19 12:25:56 -06004471static void __exit rbd_exit(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004472{
4473 rbd_sysfs_cleanup();
4474}
4475
4476module_init(rbd_init);
4477module_exit(rbd_exit);
4478
4479MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
4480MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
4481MODULE_DESCRIPTION("rados block device");
4482
4483/* following authorship retained from original osdblk.c */
4484MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
4485
4486MODULE_LICENSE("GPL");