blob: 9c2b20a88be2422e476768b681828228cb6bb975 [file] [log] [blame]
Alex Eldere2a58ee2013-04-30 00:44:33 -05001
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002/*
3 rbd.c -- Export ceph rados objects as a Linux block device
4
5
6 based on drivers/block/osdblk.c:
7
8 Copyright 2009 Red Hat, Inc.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; see the file COPYING. If not, write to
21 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22
23
24
Yehuda Sadehdfc56062010-11-19 14:51:04 -080025 For usage instructions, please refer to:
Yehuda Sadeh602adf42010-08-12 16:11:25 -070026
Yehuda Sadehdfc56062010-11-19 14:51:04 -080027 Documentation/ABI/testing/sysfs-bus-rbd
Yehuda Sadeh602adf42010-08-12 16:11:25 -070028
29 */
30
31#include <linux/ceph/libceph.h>
32#include <linux/ceph/osd_client.h>
33#include <linux/ceph/mon_client.h>
34#include <linux/ceph/decode.h>
Yehuda Sadeh59c2be12011-03-21 15:10:11 -070035#include <linux/parser.h>
Alex Elder30d1cff2013-05-01 12:43:03 -050036#include <linux/bsearch.h>
Yehuda Sadeh602adf42010-08-12 16:11:25 -070037
38#include <linux/kernel.h>
39#include <linux/device.h>
40#include <linux/module.h>
41#include <linux/fs.h>
42#include <linux/blkdev.h>
Alex Elder1c2a9df2013-05-01 12:43:03 -050043#include <linux/slab.h>
Yehuda Sadeh602adf42010-08-12 16:11:25 -070044
45#include "rbd_types.h"
46
Alex Elderaafb2302012-09-06 16:00:54 -050047#define RBD_DEBUG /* Activate rbd_assert() calls */
48
Alex Elder593a9e72012-02-07 12:03:37 -060049/*
50 * The basic unit of block I/O is a sector. It is interpreted in a
51 * number of contexts in Linux (blk, bio, genhd), but the default is
52 * universally 512 bytes. These symbols are just slightly more
53 * meaningful than the bare numbers they represent.
54 */
55#define SECTOR_SHIFT 9
56#define SECTOR_SIZE (1ULL << SECTOR_SHIFT)
57
Alex Elderf0f8cef2012-01-29 13:57:44 -060058#define RBD_DRV_NAME "rbd"
59#define RBD_DRV_NAME_LONG "rbd (rados block device)"
Yehuda Sadeh602adf42010-08-12 16:11:25 -070060
61#define RBD_MINORS_PER_MAJOR 256 /* max minors per blkdev */
62
Alex Elderd4b125e2012-07-03 16:01:19 -050063#define RBD_SNAP_DEV_NAME_PREFIX "snap_"
64#define RBD_MAX_SNAP_NAME_LEN \
65 (NAME_MAX - (sizeof (RBD_SNAP_DEV_NAME_PREFIX) - 1))
66
Alex Elder35d489f2012-07-03 16:01:19 -050067#define RBD_MAX_SNAP_COUNT 510 /* allows max snapc to fit in 4KB */
Yehuda Sadeh602adf42010-08-12 16:11:25 -070068
69#define RBD_SNAP_HEAD_NAME "-"
70
Alex Elder9682fc62013-04-30 00:44:33 -050071#define BAD_SNAP_INDEX U32_MAX /* invalid index into snap array */
72
Alex Elder9e15b772012-10-30 19:40:33 -050073/* This allows a single page to hold an image name sent by OSD */
74#define RBD_IMAGE_NAME_LEN_MAX (PAGE_SIZE - sizeof (__le32) - 1)
Alex Elder1e130192012-07-03 16:01:19 -050075#define RBD_IMAGE_ID_LEN_MAX 64
Alex Elder9e15b772012-10-30 19:40:33 -050076
Alex Elder1e130192012-07-03 16:01:19 -050077#define RBD_OBJ_PREFIX_LEN_MAX 64
Alex Elder589d30e2012-07-10 20:30:11 -050078
Alex Elderd8891402012-10-09 13:50:17 -070079/* Feature bits */
80
Alex Elder5cbf6f122013-04-11 09:29:48 -050081#define RBD_FEATURE_LAYERING (1<<0)
82#define RBD_FEATURE_STRIPINGV2 (1<<1)
83#define RBD_FEATURES_ALL \
84 (RBD_FEATURE_LAYERING | RBD_FEATURE_STRIPINGV2)
Alex Elderd8891402012-10-09 13:50:17 -070085
86/* Features supported by this (client software) implementation. */
87
Alex Elder770eba62012-10-25 23:34:40 -050088#define RBD_FEATURES_SUPPORTED (RBD_FEATURES_ALL)
Alex Elderd8891402012-10-09 13:50:17 -070089
Alex Elder81a89792012-02-02 08:13:30 -060090/*
91 * An RBD device name will be "rbd#", where the "rbd" comes from
92 * RBD_DRV_NAME above, and # is a unique integer identifier.
93 * MAX_INT_FORMAT_WIDTH is used in ensuring DEV_NAME_LEN is big
94 * enough to hold all possible device names.
95 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -070096#define DEV_NAME_LEN 32
Alex Elder81a89792012-02-02 08:13:30 -060097#define MAX_INT_FORMAT_WIDTH ((5 * sizeof (int)) / 2 + 1)
Yehuda Sadeh602adf42010-08-12 16:11:25 -070098
99/*
100 * block device image metadata (in-memory version)
101 */
102struct rbd_image_header {
Alex Elderf35a4de2013-05-06 09:51:29 -0500103 /* These six fields never change for a given rbd image */
Alex Elder849b4262012-07-09 21:04:24 -0500104 char *object_prefix;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700105 __u8 obj_order;
106 __u8 crypt_type;
107 __u8 comp_type;
Alex Elderf35a4de2013-05-06 09:51:29 -0500108 u64 stripe_unit;
109 u64 stripe_count;
110 u64 features; /* Might be changeable someday? */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700111
Alex Elderf84344f2012-08-31 17:29:51 -0500112 /* The remaining fields need to be updated occasionally */
113 u64 image_size;
114 struct ceph_snap_context *snapc;
Alex Elderf35a4de2013-05-06 09:51:29 -0500115 char *snap_names; /* format 1 only */
116 u64 *snap_sizes; /* format 1 only */
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700117};
118
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500119/*
120 * An rbd image specification.
121 *
122 * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely
Alex Elderc66c6e02012-11-01 08:39:26 -0500123 * identify an image. Each rbd_dev structure includes a pointer to
124 * an rbd_spec structure that encapsulates this identity.
125 *
126 * Each of the id's in an rbd_spec has an associated name. For a
127 * user-mapped image, the names are supplied and the id's associated
128 * with them are looked up. For a layered image, a parent image is
129 * defined by the tuple, and the names are looked up.
130 *
131 * An rbd_dev structure contains a parent_spec pointer which is
132 * non-null if the image it represents is a child in a layered
133 * image. This pointer will refer to the rbd_spec structure used
134 * by the parent rbd_dev for its own identity (i.e., the structure
135 * is shared between the parent and child).
136 *
137 * Since these structures are populated once, during the discovery
138 * phase of image construction, they are effectively immutable so
139 * we make no effort to synchronize access to them.
140 *
141 * Note that code herein does not assume the image name is known (it
142 * could be a null pointer).
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500143 */
144struct rbd_spec {
145 u64 pool_id;
Alex Elderecb4dc222013-04-26 09:43:47 -0500146 const char *pool_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500147
Alex Elderecb4dc222013-04-26 09:43:47 -0500148 const char *image_id;
149 const char *image_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500150
151 u64 snap_id;
Alex Elderecb4dc222013-04-26 09:43:47 -0500152 const char *snap_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500153
154 struct kref kref;
155};
156
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700157/*
Alex Elderf0f8cef2012-01-29 13:57:44 -0600158 * an instance of the client. multiple devices may share an rbd client.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700159 */
160struct rbd_client {
161 struct ceph_client *client;
162 struct kref kref;
163 struct list_head node;
164};
165
Alex Elderbf0d5f502012-11-22 00:00:08 -0600166struct rbd_img_request;
167typedef void (*rbd_img_callback_t)(struct rbd_img_request *);
168
169#define BAD_WHICH U32_MAX /* Good which or bad which, which? */
170
171struct rbd_obj_request;
172typedef void (*rbd_obj_callback_t)(struct rbd_obj_request *);
173
Alex Elder9969ebc2013-01-18 12:31:10 -0600174enum obj_request_type {
175 OBJ_REQUEST_NODATA, OBJ_REQUEST_BIO, OBJ_REQUEST_PAGES
176};
Alex Elderbf0d5f502012-11-22 00:00:08 -0600177
Alex Elder926f9b32013-02-11 12:33:24 -0600178enum obj_req_flags {
179 OBJ_REQ_DONE, /* completion flag: not done = 0, done = 1 */
Alex Elder6365d332013-02-11 12:33:24 -0600180 OBJ_REQ_IMG_DATA, /* object usage: standalone = 0, image = 1 */
Alex Elder5679c592013-02-11 12:33:24 -0600181 OBJ_REQ_KNOWN, /* EXISTS flag valid: no = 0, yes = 1 */
182 OBJ_REQ_EXISTS, /* target exists: no = 0, yes = 1 */
Alex Elder926f9b32013-02-11 12:33:24 -0600183};
184
Alex Elderbf0d5f502012-11-22 00:00:08 -0600185struct rbd_obj_request {
186 const char *object_name;
187 u64 offset; /* object start byte */
188 u64 length; /* bytes from offset */
Alex Elder926f9b32013-02-11 12:33:24 -0600189 unsigned long flags;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600190
Alex Elderc5b5ef62013-02-11 12:33:24 -0600191 /*
192 * An object request associated with an image will have its
193 * img_data flag set; a standalone object request will not.
194 *
195 * A standalone object request will have which == BAD_WHICH
196 * and a null obj_request pointer.
197 *
198 * An object request initiated in support of a layered image
199 * object (to check for its existence before a write) will
200 * have which == BAD_WHICH and a non-null obj_request pointer.
201 *
202 * Finally, an object request for rbd image data will have
203 * which != BAD_WHICH, and will have a non-null img_request
204 * pointer. The value of which will be in the range
205 * 0..(img_request->obj_request_count-1).
206 */
207 union {
208 struct rbd_obj_request *obj_request; /* STAT op */
209 struct {
210 struct rbd_img_request *img_request;
211 u64 img_offset;
212 /* links for img_request->obj_requests list */
213 struct list_head links;
214 };
215 };
Alex Elderbf0d5f502012-11-22 00:00:08 -0600216 u32 which; /* posn image request list */
217
218 enum obj_request_type type;
Alex Elder788e2df2013-01-17 12:25:27 -0600219 union {
220 struct bio *bio_list;
221 struct {
222 struct page **pages;
223 u32 page_count;
224 };
225 };
Alex Elder0eefd472013-04-19 15:34:50 -0500226 struct page **copyup_pages;
Alex Elderebda6402013-05-10 16:29:22 -0500227 u32 copyup_page_count;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600228
229 struct ceph_osd_request *osd_req;
230
231 u64 xferred; /* bytes transferred */
Sage Weil1b83bef2013-02-25 16:11:12 -0800232 int result;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600233
234 rbd_obj_callback_t callback;
Alex Elder788e2df2013-01-17 12:25:27 -0600235 struct completion completion;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600236
237 struct kref kref;
238};
239
Alex Elder0c425242013-02-08 09:55:49 -0600240enum img_req_flags {
Alex Elder9849e982013-01-24 16:13:36 -0600241 IMG_REQ_WRITE, /* I/O direction: read = 0, write = 1 */
242 IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */
Alex Elderd0b2e942013-01-24 16:13:36 -0600243 IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */
Alex Elder0c425242013-02-08 09:55:49 -0600244};
245
Alex Elderbf0d5f502012-11-22 00:00:08 -0600246struct rbd_img_request {
Alex Elderbf0d5f502012-11-22 00:00:08 -0600247 struct rbd_device *rbd_dev;
248 u64 offset; /* starting image byte offset */
249 u64 length; /* byte count from offset */
Alex Elder0c425242013-02-08 09:55:49 -0600250 unsigned long flags;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600251 union {
Alex Elder9849e982013-01-24 16:13:36 -0600252 u64 snap_id; /* for reads */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600253 struct ceph_snap_context *snapc; /* for writes */
Alex Elder9849e982013-01-24 16:13:36 -0600254 };
255 union {
256 struct request *rq; /* block request */
257 struct rbd_obj_request *obj_request; /* obj req initiator */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600258 };
Alex Elder3d7efd12013-04-19 15:34:50 -0500259 struct page **copyup_pages;
Alex Elderebda6402013-05-10 16:29:22 -0500260 u32 copyup_page_count;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600261 spinlock_t completion_lock;/* protects next_completion */
262 u32 next_completion;
263 rbd_img_callback_t callback;
Alex Elder55f27e02013-04-10 12:34:25 -0500264 u64 xferred;/* aggregate bytes transferred */
Alex Eldera5a337d2013-01-24 16:13:36 -0600265 int result; /* first nonzero obj_request result */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600266
267 u32 obj_request_count;
268 struct list_head obj_requests; /* rbd_obj_request structs */
269
270 struct kref kref;
271};
272
273#define for_each_obj_request(ireq, oreq) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600274 list_for_each_entry(oreq, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600275#define for_each_obj_request_from(ireq, oreq) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600276 list_for_each_entry_from(oreq, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600277#define for_each_obj_request_safe(ireq, oreq, n) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600278 list_for_each_entry_safe_reverse(oreq, n, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600279
Alex Elderf84344f2012-08-31 17:29:51 -0500280struct rbd_mapping {
Alex Elder99c1f082012-08-30 14:42:15 -0500281 u64 size;
Alex Elder34b13182012-07-13 20:35:12 -0500282 u64 features;
Alex Elderf84344f2012-08-31 17:29:51 -0500283 bool read_only;
284};
285
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700286/*
287 * a single device
288 */
289struct rbd_device {
Alex Elderde71a292012-07-03 16:01:19 -0500290 int dev_id; /* blkdev unique id */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700291
292 int major; /* blkdev assigned major */
293 struct gendisk *disk; /* blkdev's gendisk and rq */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700294
Alex Eldera30b71b2012-07-10 20:30:11 -0500295 u32 image_format; /* Either 1 or 2 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700296 struct rbd_client *rbd_client;
297
298 char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */
299
Alex Elderb82d1672013-01-14 12:43:31 -0600300 spinlock_t lock; /* queue, flags, open_count */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700301
302 struct rbd_image_header header;
Alex Elderb82d1672013-01-14 12:43:31 -0600303 unsigned long flags; /* possibly lock protected */
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500304 struct rbd_spec *spec;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700305
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500306 char *header_name;
Alex Elder971f8392012-10-25 23:34:41 -0500307
Alex Elder0903e872012-11-14 12:25:19 -0600308 struct ceph_file_layout layout;
309
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700310 struct ceph_osd_event *watch_event;
Alex Elder975241a2013-01-25 17:08:55 -0600311 struct rbd_obj_request *watch_request;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700312
Alex Elder86b00e02012-10-25 23:34:42 -0500313 struct rbd_spec *parent_spec;
314 u64 parent_overlap;
Alex Elder2f82ee52012-10-30 19:40:33 -0500315 struct rbd_device *parent;
Alex Elder86b00e02012-10-25 23:34:42 -0500316
Josh Durginc6666012011-11-21 17:11:12 -0800317 /* protects updating the header */
318 struct rw_semaphore header_rwsem;
Alex Elderf84344f2012-08-31 17:29:51 -0500319
320 struct rbd_mapping mapping;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700321
322 struct list_head node;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800323
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800324 /* sysfs related */
325 struct device dev;
Alex Elderb82d1672013-01-14 12:43:31 -0600326 unsigned long open_count; /* protected by lock */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800327};
328
Alex Elderb82d1672013-01-14 12:43:31 -0600329/*
330 * Flag bits for rbd_dev->flags. If atomicity is required,
331 * rbd_dev->lock is used to protect access.
332 *
333 * Currently, only the "removing" flag (which is coupled with the
334 * "open_count" field) requires atomic access.
335 */
Alex Elder6d292902013-01-14 12:43:31 -0600336enum rbd_dev_flags {
337 RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */
Alex Elderb82d1672013-01-14 12:43:31 -0600338 RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */
Alex Elder6d292902013-01-14 12:43:31 -0600339};
340
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700341static DEFINE_MUTEX(ctl_mutex); /* Serialize open/close/setup/teardown */
Alex Eldere124a82f2012-01-29 13:57:44 -0600342
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700343static LIST_HEAD(rbd_dev_list); /* devices */
Alex Eldere124a82f2012-01-29 13:57:44 -0600344static DEFINE_SPINLOCK(rbd_dev_list_lock);
345
Alex Elder432b8582012-01-29 13:57:44 -0600346static LIST_HEAD(rbd_client_list); /* clients */
347static DEFINE_SPINLOCK(rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700348
Alex Elder78c2a442013-05-01 12:43:04 -0500349/* Slab caches for frequently-allocated structures */
350
Alex Elder1c2a9df2013-05-01 12:43:03 -0500351static struct kmem_cache *rbd_img_request_cache;
Alex Elder868311b2013-05-01 12:43:03 -0500352static struct kmem_cache *rbd_obj_request_cache;
Alex Elder78c2a442013-05-01 12:43:04 -0500353static struct kmem_cache *rbd_segment_name_cache;
Alex Elder1c2a9df2013-05-01 12:43:03 -0500354
Alex Elder3d7efd12013-04-19 15:34:50 -0500355static int rbd_img_request_submit(struct rbd_img_request *img_request);
356
Alex Elder200a6a82013-04-28 23:32:34 -0500357static void rbd_dev_device_release(struct device *dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800358
Alex Elderf0f8cef2012-01-29 13:57:44 -0600359static ssize_t rbd_add(struct bus_type *bus, const char *buf,
360 size_t count);
361static ssize_t rbd_remove(struct bus_type *bus, const char *buf,
362 size_t count);
Alex Elder1f3ef782013-05-06 17:40:33 -0500363static int rbd_dev_image_probe(struct rbd_device *rbd_dev, bool mapping);
Alex Elderf0f8cef2012-01-29 13:57:44 -0600364
365static struct bus_attribute rbd_bus_attrs[] = {
366 __ATTR(add, S_IWUSR, NULL, rbd_add),
367 __ATTR(remove, S_IWUSR, NULL, rbd_remove),
368 __ATTR_NULL
369};
370
371static struct bus_type rbd_bus_type = {
372 .name = "rbd",
373 .bus_attrs = rbd_bus_attrs,
374};
375
376static void rbd_root_dev_release(struct device *dev)
377{
378}
379
380static struct device rbd_root_dev = {
381 .init_name = "rbd",
382 .release = rbd_root_dev_release,
383};
384
Alex Elder06ecc6c2012-11-01 10:17:15 -0500385static __printf(2, 3)
386void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...)
387{
388 struct va_format vaf;
389 va_list args;
390
391 va_start(args, fmt);
392 vaf.fmt = fmt;
393 vaf.va = &args;
394
395 if (!rbd_dev)
396 printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf);
397 else if (rbd_dev->disk)
398 printk(KERN_WARNING "%s: %s: %pV\n",
399 RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf);
400 else if (rbd_dev->spec && rbd_dev->spec->image_name)
401 printk(KERN_WARNING "%s: image %s: %pV\n",
402 RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf);
403 else if (rbd_dev->spec && rbd_dev->spec->image_id)
404 printk(KERN_WARNING "%s: id %s: %pV\n",
405 RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf);
406 else /* punt */
407 printk(KERN_WARNING "%s: rbd_dev %p: %pV\n",
408 RBD_DRV_NAME, rbd_dev, &vaf);
409 va_end(args);
410}
411
Alex Elderaafb2302012-09-06 16:00:54 -0500412#ifdef RBD_DEBUG
413#define rbd_assert(expr) \
414 if (unlikely(!(expr))) { \
415 printk(KERN_ERR "\nAssertion failure in %s() " \
416 "at line %d:\n\n" \
417 "\trbd_assert(%s);\n\n", \
418 __func__, __LINE__, #expr); \
419 BUG(); \
420 }
421#else /* !RBD_DEBUG */
422# define rbd_assert(expr) ((void) 0)
423#endif /* !RBD_DEBUG */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800424
Alex Elderb454e362013-04-19 15:34:50 -0500425static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request);
Alex Elder05a46af2013-04-26 15:44:36 -0500426static void rbd_img_parent_read(struct rbd_obj_request *obj_request);
427static void rbd_dev_remove_parent(struct rbd_device *rbd_dev);
Alex Elder8b3e1a52013-01-24 16:13:36 -0600428
Alex Eldercc4a38bd2013-04-30 00:44:33 -0500429static int rbd_dev_refresh(struct rbd_device *rbd_dev);
Alex Elder2df3fac2013-05-06 09:51:30 -0500430static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev);
431static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev);
Alex Elder54cac612013-04-30 00:44:33 -0500432static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
433 u64 snap_id);
Alex Elder2ad3d712013-04-30 00:44:33 -0500434static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
435 u8 *order, u64 *snap_size);
436static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
437 u64 *snap_features);
438static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700439
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700440static int rbd_open(struct block_device *bdev, fmode_t mode)
441{
Alex Elderf0f8cef2012-01-29 13:57:44 -0600442 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
Alex Elderb82d1672013-01-14 12:43:31 -0600443 bool removing = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700444
Alex Elderf84344f2012-08-31 17:29:51 -0500445 if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700446 return -EROFS;
447
Alex Eldera14ea262013-02-05 13:23:12 -0600448 spin_lock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600449 if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags))
450 removing = true;
451 else
452 rbd_dev->open_count++;
Alex Eldera14ea262013-02-05 13:23:12 -0600453 spin_unlock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600454 if (removing)
455 return -ENOENT;
456
Alex Elder42382b72012-11-16 09:29:16 -0600457 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Alex Elderc3e946c2012-11-16 09:29:16 -0600458 (void) get_device(&rbd_dev->dev);
Alex Elderf84344f2012-08-31 17:29:51 -0500459 set_device_ro(bdev, rbd_dev->mapping.read_only);
Alex Elder42382b72012-11-16 09:29:16 -0600460 mutex_unlock(&ctl_mutex);
Alex Elder340c7a22012-08-10 13:12:07 -0700461
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700462 return 0;
463}
464
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800465static int rbd_release(struct gendisk *disk, fmode_t mode)
466{
467 struct rbd_device *rbd_dev = disk->private_data;
Alex Elderb82d1672013-01-14 12:43:31 -0600468 unsigned long open_count_before;
469
Alex Eldera14ea262013-02-05 13:23:12 -0600470 spin_lock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600471 open_count_before = rbd_dev->open_count--;
Alex Eldera14ea262013-02-05 13:23:12 -0600472 spin_unlock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600473 rbd_assert(open_count_before > 0);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800474
Alex Elder42382b72012-11-16 09:29:16 -0600475 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Alex Elderc3e946c2012-11-16 09:29:16 -0600476 put_device(&rbd_dev->dev);
Alex Elder42382b72012-11-16 09:29:16 -0600477 mutex_unlock(&ctl_mutex);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800478
479 return 0;
480}
481
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700482static const struct block_device_operations rbd_bd_ops = {
483 .owner = THIS_MODULE,
484 .open = rbd_open,
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800485 .release = rbd_release,
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700486};
487
488/*
489 * Initialize an rbd client instance.
Alex Elder43ae4702012-07-03 16:01:18 -0500490 * We own *ceph_opts.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700491 */
Alex Elderf8c38922012-08-10 13:12:07 -0700492static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700493{
494 struct rbd_client *rbdc;
495 int ret = -ENOMEM;
496
Alex Elder37206ee2013-02-20 17:32:08 -0600497 dout("%s:\n", __func__);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700498 rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL);
499 if (!rbdc)
500 goto out_opt;
501
502 kref_init(&rbdc->kref);
503 INIT_LIST_HEAD(&rbdc->node);
504
Alex Elderbc534d82012-01-29 13:57:44 -0600505 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
506
Alex Elder43ae4702012-07-03 16:01:18 -0500507 rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700508 if (IS_ERR(rbdc->client))
Alex Elderbc534d82012-01-29 13:57:44 -0600509 goto out_mutex;
Alex Elder43ae4702012-07-03 16:01:18 -0500510 ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700511
512 ret = ceph_open_session(rbdc->client);
513 if (ret < 0)
514 goto out_err;
515
Alex Elder432b8582012-01-29 13:57:44 -0600516 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700517 list_add_tail(&rbdc->node, &rbd_client_list);
Alex Elder432b8582012-01-29 13:57:44 -0600518 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700519
Alex Elderbc534d82012-01-29 13:57:44 -0600520 mutex_unlock(&ctl_mutex);
Alex Elder37206ee2013-02-20 17:32:08 -0600521 dout("%s: rbdc %p\n", __func__, rbdc);
Alex Elderbc534d82012-01-29 13:57:44 -0600522
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700523 return rbdc;
524
525out_err:
526 ceph_destroy_client(rbdc->client);
Alex Elderbc534d82012-01-29 13:57:44 -0600527out_mutex:
528 mutex_unlock(&ctl_mutex);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700529 kfree(rbdc);
530out_opt:
Alex Elder43ae4702012-07-03 16:01:18 -0500531 if (ceph_opts)
532 ceph_destroy_options(ceph_opts);
Alex Elder37206ee2013-02-20 17:32:08 -0600533 dout("%s: error %d\n", __func__, ret);
534
Vasiliy Kulikov28f259b2010-09-26 12:59:37 +0400535 return ERR_PTR(ret);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700536}
537
Alex Elder2f82ee52012-10-30 19:40:33 -0500538static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc)
539{
540 kref_get(&rbdc->kref);
541
542 return rbdc;
543}
544
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700545/*
Alex Elder1f7ba332012-08-10 13:12:07 -0700546 * Find a ceph client with specific addr and configuration. If
547 * found, bump its reference count.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700548 */
Alex Elder1f7ba332012-08-10 13:12:07 -0700549static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700550{
551 struct rbd_client *client_node;
Alex Elder1f7ba332012-08-10 13:12:07 -0700552 bool found = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700553
Alex Elder43ae4702012-07-03 16:01:18 -0500554 if (ceph_opts->flags & CEPH_OPT_NOSHARE)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700555 return NULL;
556
Alex Elder1f7ba332012-08-10 13:12:07 -0700557 spin_lock(&rbd_client_list_lock);
558 list_for_each_entry(client_node, &rbd_client_list, node) {
559 if (!ceph_compare_options(ceph_opts, client_node->client)) {
Alex Elder2f82ee52012-10-30 19:40:33 -0500560 __rbd_get_client(client_node);
561
Alex Elder1f7ba332012-08-10 13:12:07 -0700562 found = true;
563 break;
564 }
565 }
566 spin_unlock(&rbd_client_list_lock);
567
568 return found ? client_node : NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700569}
570
571/*
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700572 * mount options
573 */
574enum {
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700575 Opt_last_int,
576 /* int args above */
577 Opt_last_string,
578 /* string args above */
Alex Eldercc0538b2012-08-10 13:12:07 -0700579 Opt_read_only,
580 Opt_read_write,
581 /* Boolean args above */
582 Opt_last_bool,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700583};
584
Alex Elder43ae4702012-07-03 16:01:18 -0500585static match_table_t rbd_opts_tokens = {
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700586 /* int args above */
587 /* string args above */
Alex Elderbe466c12012-10-22 11:31:26 -0500588 {Opt_read_only, "read_only"},
Alex Eldercc0538b2012-08-10 13:12:07 -0700589 {Opt_read_only, "ro"}, /* Alternate spelling */
590 {Opt_read_write, "read_write"},
591 {Opt_read_write, "rw"}, /* Alternate spelling */
592 /* Boolean args above */
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700593 {-1, NULL}
594};
595
Alex Elder98571b52013-01-20 14:44:42 -0600596struct rbd_options {
597 bool read_only;
598};
599
600#define RBD_READ_ONLY_DEFAULT false
601
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700602static int parse_rbd_opts_token(char *c, void *private)
603{
Alex Elder43ae4702012-07-03 16:01:18 -0500604 struct rbd_options *rbd_opts = private;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700605 substring_t argstr[MAX_OPT_ARGS];
606 int token, intval, ret;
607
Alex Elder43ae4702012-07-03 16:01:18 -0500608 token = match_token(c, rbd_opts_tokens, argstr);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700609 if (token < 0)
610 return -EINVAL;
611
612 if (token < Opt_last_int) {
613 ret = match_int(&argstr[0], &intval);
614 if (ret < 0) {
615 pr_err("bad mount option arg (not int) "
616 "at '%s'\n", c);
617 return ret;
618 }
619 dout("got int token %d val %d\n", token, intval);
620 } else if (token > Opt_last_int && token < Opt_last_string) {
621 dout("got string token %d val %s\n", token,
622 argstr[0].from);
Alex Eldercc0538b2012-08-10 13:12:07 -0700623 } else if (token > Opt_last_string && token < Opt_last_bool) {
624 dout("got Boolean token %d\n", token);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700625 } else {
626 dout("got token %d\n", token);
627 }
628
629 switch (token) {
Alex Eldercc0538b2012-08-10 13:12:07 -0700630 case Opt_read_only:
631 rbd_opts->read_only = true;
632 break;
633 case Opt_read_write:
634 rbd_opts->read_only = false;
635 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700636 default:
Alex Elderaafb2302012-09-06 16:00:54 -0500637 rbd_assert(false);
638 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700639 }
640 return 0;
641}
642
643/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700644 * Get a ceph client with specific addr and configuration, if one does
645 * not exist create it.
646 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500647static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700648{
Alex Elderf8c38922012-08-10 13:12:07 -0700649 struct rbd_client *rbdc;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700650
Alex Elder1f7ba332012-08-10 13:12:07 -0700651 rbdc = rbd_client_find(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500652 if (rbdc) /* using an existing client */
Alex Elder43ae4702012-07-03 16:01:18 -0500653 ceph_destroy_options(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500654 else
Alex Elderf8c38922012-08-10 13:12:07 -0700655 rbdc = rbd_client_create(ceph_opts);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700656
Alex Elder9d3997f2012-10-25 23:34:42 -0500657 return rbdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700658}
659
660/*
661 * Destroy ceph client
Alex Elderd23a4b32012-01-29 13:57:43 -0600662 *
Alex Elder432b8582012-01-29 13:57:44 -0600663 * Caller must hold rbd_client_list_lock.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700664 */
665static void rbd_client_release(struct kref *kref)
666{
667 struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
668
Alex Elder37206ee2013-02-20 17:32:08 -0600669 dout("%s: rbdc %p\n", __func__, rbdc);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500670 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700671 list_del(&rbdc->node);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500672 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700673
674 ceph_destroy_client(rbdc->client);
675 kfree(rbdc);
676}
677
678/*
679 * Drop reference to ceph client node. If it's not referenced anymore, release
680 * it.
681 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500682static void rbd_put_client(struct rbd_client *rbdc)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700683{
Alex Elderc53d5892012-10-25 23:34:42 -0500684 if (rbdc)
685 kref_put(&rbdc->kref, rbd_client_release);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700686}
687
Alex Eldera30b71b2012-07-10 20:30:11 -0500688static bool rbd_image_format_valid(u32 image_format)
689{
690 return image_format == 1 || image_format == 2;
691}
692
Alex Elder8e94af82012-07-25 09:32:40 -0500693static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
694{
Alex Elder103a1502012-08-02 11:29:45 -0500695 size_t size;
696 u32 snap_count;
697
698 /* The header has to start with the magic rbd header text */
699 if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT)))
700 return false;
701
Alex Elderdb2388b2012-10-20 22:17:27 -0500702 /* The bio layer requires at least sector-sized I/O */
703
704 if (ondisk->options.order < SECTOR_SHIFT)
705 return false;
706
707 /* If we use u64 in a few spots we may be able to loosen this */
708
709 if (ondisk->options.order > 8 * sizeof (int) - 1)
710 return false;
711
Alex Elder103a1502012-08-02 11:29:45 -0500712 /*
713 * The size of a snapshot header has to fit in a size_t, and
714 * that limits the number of snapshots.
715 */
716 snap_count = le32_to_cpu(ondisk->snap_count);
717 size = SIZE_MAX - sizeof (struct ceph_snap_context);
718 if (snap_count > size / sizeof (__le64))
719 return false;
720
721 /*
722 * Not only that, but the size of the entire the snapshot
723 * header must also be representable in a size_t.
724 */
725 size -= snap_count * sizeof (__le64);
726 if ((u64) size < le64_to_cpu(ondisk->snap_names_len))
727 return false;
728
729 return true;
Alex Elder8e94af82012-07-25 09:32:40 -0500730}
731
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700732/*
Alex Elderbb23e372013-05-06 09:51:29 -0500733 * Fill an rbd image header with information from the given format 1
734 * on-disk header.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700735 */
Alex Elder662518b2013-05-06 09:51:29 -0500736static int rbd_header_from_disk(struct rbd_device *rbd_dev,
Alex Elder4156d992012-08-02 11:29:46 -0500737 struct rbd_image_header_ondisk *ondisk)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700738{
Alex Elder662518b2013-05-06 09:51:29 -0500739 struct rbd_image_header *header = &rbd_dev->header;
Alex Elderbb23e372013-05-06 09:51:29 -0500740 bool first_time = header->object_prefix == NULL;
741 struct ceph_snap_context *snapc;
742 char *object_prefix = NULL;
743 char *snap_names = NULL;
744 u64 *snap_sizes = NULL;
Alex Elderccece232012-07-10 20:30:10 -0500745 u32 snap_count;
Alex Elderd2bb24e2012-07-26 23:37:14 -0500746 size_t size;
Alex Elderbb23e372013-05-06 09:51:29 -0500747 int ret = -ENOMEM;
Alex Elder621901d2012-08-23 23:22:06 -0500748 u32 i;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700749
Alex Elderbb23e372013-05-06 09:51:29 -0500750 /* Allocate this now to avoid having to handle failure below */
751
752 if (first_time) {
753 size_t len;
754
755 len = strnlen(ondisk->object_prefix,
756 sizeof (ondisk->object_prefix));
757 object_prefix = kmalloc(len + 1, GFP_KERNEL);
758 if (!object_prefix)
759 return -ENOMEM;
760 memcpy(object_prefix, ondisk->object_prefix, len);
761 object_prefix[len] = '\0';
762 }
763
764 /* Allocate the snapshot context and fill it in */
765
Alex Elder103a1502012-08-02 11:29:45 -0500766 snap_count = le32_to_cpu(ondisk->snap_count);
Alex Elderbb23e372013-05-06 09:51:29 -0500767 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
768 if (!snapc)
769 goto out_err;
770 snapc->seq = le64_to_cpu(ondisk->snap_seq);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700771 if (snap_count) {
Alex Elderbb23e372013-05-06 09:51:29 -0500772 struct rbd_image_snap_ondisk *snaps;
Alex Elderf785cc12012-08-23 23:22:06 -0500773 u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len);
774
Alex Elderbb23e372013-05-06 09:51:29 -0500775 /* We'll keep a copy of the snapshot names... */
Alex Elder621901d2012-08-23 23:22:06 -0500776
Alex Elderbb23e372013-05-06 09:51:29 -0500777 if (snap_names_len > (u64)SIZE_MAX)
778 goto out_2big;
779 snap_names = kmalloc(snap_names_len, GFP_KERNEL);
780 if (!snap_names)
Alex Elder6a523252012-07-19 17:12:59 -0500781 goto out_err;
Alex Elderbb23e372013-05-06 09:51:29 -0500782
783 /* ...as well as the array of their sizes. */
784
785 size = snap_count * sizeof (*header->snap_sizes);
786 snap_sizes = kmalloc(size, GFP_KERNEL);
787 if (!snap_sizes)
788 goto out_err;
789
Alex Elderf785cc12012-08-23 23:22:06 -0500790 /*
Alex Elderbb23e372013-05-06 09:51:29 -0500791 * Copy the names, and fill in each snapshot's id
792 * and size.
793 *
Alex Elder99a41eb2013-05-06 09:51:30 -0500794 * Note that rbd_dev_v1_header_info() guarantees the
Alex Elderbb23e372013-05-06 09:51:29 -0500795 * ondisk buffer we're working with has
Alex Elderf785cc12012-08-23 23:22:06 -0500796 * snap_names_len bytes beyond the end of the
797 * snapshot id array, this memcpy() is safe.
798 */
Alex Elderbb23e372013-05-06 09:51:29 -0500799 memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len);
800 snaps = ondisk->snaps;
801 for (i = 0; i < snap_count; i++) {
802 snapc->snaps[i] = le64_to_cpu(snaps[i].id);
803 snap_sizes[i] = le64_to_cpu(snaps[i].image_size);
804 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700805 }
Alex Elder849b4262012-07-09 21:04:24 -0500806
Alex Elderbb23e372013-05-06 09:51:29 -0500807 /* We won't fail any more, fill in the header */
Alex Elder6a523252012-07-19 17:12:59 -0500808
Alex Elder662518b2013-05-06 09:51:29 -0500809 down_write(&rbd_dev->header_rwsem);
Alex Elderbb23e372013-05-06 09:51:29 -0500810 if (first_time) {
811 header->object_prefix = object_prefix;
812 header->obj_order = ondisk->options.order;
813 header->crypt_type = ondisk->options.crypt_type;
814 header->comp_type = ondisk->options.comp_type;
815 /* The rest aren't used for format 1 images */
816 header->stripe_unit = 0;
817 header->stripe_count = 0;
818 header->features = 0;
Alex Elder662518b2013-05-06 09:51:29 -0500819 } else {
820 ceph_put_snap_context(header->snapc);
821 kfree(header->snap_names);
822 kfree(header->snap_sizes);
Alex Elderbb23e372013-05-06 09:51:29 -0500823 }
824
825 /* The remaining fields always get updated (when we refresh) */
Alex Elder621901d2012-08-23 23:22:06 -0500826
Alex Elderf84344f2012-08-31 17:29:51 -0500827 header->image_size = le64_to_cpu(ondisk->image_size);
Alex Elderbb23e372013-05-06 09:51:29 -0500828 header->snapc = snapc;
829 header->snap_names = snap_names;
830 header->snap_sizes = snap_sizes;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700831
Alex Elder662518b2013-05-06 09:51:29 -0500832 /* Make sure mapping size is consistent with header info */
833
834 if (rbd_dev->spec->snap_id == CEPH_NOSNAP || first_time)
835 if (rbd_dev->mapping.size != header->image_size)
836 rbd_dev->mapping.size = header->image_size;
837
838 up_write(&rbd_dev->header_rwsem);
839
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700840 return 0;
Alex Elderbb23e372013-05-06 09:51:29 -0500841out_2big:
842 ret = -EIO;
Alex Elder6a523252012-07-19 17:12:59 -0500843out_err:
Alex Elderbb23e372013-05-06 09:51:29 -0500844 kfree(snap_sizes);
845 kfree(snap_names);
846 ceph_put_snap_context(snapc);
847 kfree(object_prefix);
Alex Elderccece232012-07-10 20:30:10 -0500848
Alex Elderbb23e372013-05-06 09:51:29 -0500849 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700850}
851
Alex Elder9682fc62013-04-30 00:44:33 -0500852static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which)
853{
854 const char *snap_name;
855
856 rbd_assert(which < rbd_dev->header.snapc->num_snaps);
857
858 /* Skip over names until we find the one we are looking for */
859
860 snap_name = rbd_dev->header.snap_names;
861 while (which--)
862 snap_name += strlen(snap_name) + 1;
863
864 return kstrdup(snap_name, GFP_KERNEL);
865}
866
Alex Elder30d1cff2013-05-01 12:43:03 -0500867/*
868 * Snapshot id comparison function for use with qsort()/bsearch().
869 * Note that result is for snapshots in *descending* order.
870 */
871static int snapid_compare_reverse(const void *s1, const void *s2)
872{
873 u64 snap_id1 = *(u64 *)s1;
874 u64 snap_id2 = *(u64 *)s2;
875
876 if (snap_id1 < snap_id2)
877 return 1;
878 return snap_id1 == snap_id2 ? 0 : -1;
879}
880
881/*
882 * Search a snapshot context to see if the given snapshot id is
883 * present.
884 *
885 * Returns the position of the snapshot id in the array if it's found,
886 * or BAD_SNAP_INDEX otherwise.
887 *
888 * Note: The snapshot array is in kept sorted (by the osd) in
889 * reverse order, highest snapshot id first.
890 */
Alex Elder9682fc62013-04-30 00:44:33 -0500891static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id)
892{
893 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
Alex Elder30d1cff2013-05-01 12:43:03 -0500894 u64 *found;
Alex Elder9682fc62013-04-30 00:44:33 -0500895
Alex Elder30d1cff2013-05-01 12:43:03 -0500896 found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps,
897 sizeof (snap_id), snapid_compare_reverse);
Alex Elder9682fc62013-04-30 00:44:33 -0500898
Alex Elder30d1cff2013-05-01 12:43:03 -0500899 return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX;
Alex Elder9682fc62013-04-30 00:44:33 -0500900}
901
Alex Elder2ad3d712013-04-30 00:44:33 -0500902static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev,
903 u64 snap_id)
Alex Elder54cac612013-04-30 00:44:33 -0500904{
905 u32 which;
906
907 which = rbd_dev_snap_index(rbd_dev, snap_id);
908 if (which == BAD_SNAP_INDEX)
909 return NULL;
910
911 return _rbd_dev_v1_snap_name(rbd_dev, which);
912}
913
Alex Elder9e15b772012-10-30 19:40:33 -0500914static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id)
915{
Alex Elder9e15b772012-10-30 19:40:33 -0500916 if (snap_id == CEPH_NOSNAP)
917 return RBD_SNAP_HEAD_NAME;
918
Alex Elder54cac612013-04-30 00:44:33 -0500919 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
920 if (rbd_dev->image_format == 1)
921 return rbd_dev_v1_snap_name(rbd_dev, snap_id);
Alex Elder9e15b772012-10-30 19:40:33 -0500922
Alex Elder54cac612013-04-30 00:44:33 -0500923 return rbd_dev_v2_snap_name(rbd_dev, snap_id);
Alex Elder9e15b772012-10-30 19:40:33 -0500924}
925
Alex Elder2ad3d712013-04-30 00:44:33 -0500926static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
927 u64 *snap_size)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700928{
Alex Elder2ad3d712013-04-30 00:44:33 -0500929 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
930 if (snap_id == CEPH_NOSNAP) {
931 *snap_size = rbd_dev->header.image_size;
932 } else if (rbd_dev->image_format == 1) {
933 u32 which;
Alex Elder00f1f362012-02-07 12:03:36 -0600934
Alex Elder2ad3d712013-04-30 00:44:33 -0500935 which = rbd_dev_snap_index(rbd_dev, snap_id);
936 if (which == BAD_SNAP_INDEX)
937 return -ENOENT;
Alex Elder00f1f362012-02-07 12:03:36 -0600938
Alex Elder2ad3d712013-04-30 00:44:33 -0500939 *snap_size = rbd_dev->header.snap_sizes[which];
940 } else {
941 u64 size = 0;
942 int ret;
943
944 ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size);
945 if (ret)
946 return ret;
947
948 *snap_size = size;
949 }
950 return 0;
951}
952
953static int rbd_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
954 u64 *snap_features)
955{
956 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
957 if (snap_id == CEPH_NOSNAP) {
958 *snap_features = rbd_dev->header.features;
959 } else if (rbd_dev->image_format == 1) {
960 *snap_features = 0; /* No features for format 1 */
961 } else {
962 u64 features = 0;
963 int ret;
964
965 ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, &features);
966 if (ret)
967 return ret;
968
969 *snap_features = features;
970 }
971 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700972}
973
Alex Elderd1cf5782013-04-27 09:59:30 -0500974static int rbd_dev_mapping_set(struct rbd_device *rbd_dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700975{
Alex Elder8f4b7d92013-05-06 07:40:30 -0500976 u64 snap_id = rbd_dev->spec->snap_id;
Alex Elder2ad3d712013-04-30 00:44:33 -0500977 u64 size = 0;
978 u64 features = 0;
979 int ret;
Alex Elder8b0241f2013-04-25 23:15:08 -0500980
Alex Elder2ad3d712013-04-30 00:44:33 -0500981 ret = rbd_snap_size(rbd_dev, snap_id, &size);
982 if (ret)
983 return ret;
984 ret = rbd_snap_features(rbd_dev, snap_id, &features);
985 if (ret)
986 return ret;
987
988 rbd_dev->mapping.size = size;
989 rbd_dev->mapping.features = features;
990
Alex Elder8b0241f2013-04-25 23:15:08 -0500991 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700992}
993
Alex Elderd1cf5782013-04-27 09:59:30 -0500994static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev)
995{
996 rbd_dev->mapping.size = 0;
997 rbd_dev->mapping.features = 0;
Alex Elderd1cf5782013-04-27 09:59:30 -0500998}
999
Alex Elder98571b52013-01-20 14:44:42 -06001000static const char *rbd_segment_name(struct rbd_device *rbd_dev, u64 offset)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001001{
Alex Elder65ccfe22012-08-09 10:33:26 -07001002 char *name;
1003 u64 segment;
1004 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001005
Alex Elder78c2a442013-05-01 12:43:04 -05001006 name = kmem_cache_alloc(rbd_segment_name_cache, GFP_NOIO);
Alex Elder65ccfe22012-08-09 10:33:26 -07001007 if (!name)
1008 return NULL;
1009 segment = offset >> rbd_dev->header.obj_order;
Alex Elder2fd82b92012-11-09 15:05:54 -06001010 ret = snprintf(name, MAX_OBJ_NAME_SIZE + 1, "%s.%012llx",
Alex Elder65ccfe22012-08-09 10:33:26 -07001011 rbd_dev->header.object_prefix, segment);
Alex Elder2fd82b92012-11-09 15:05:54 -06001012 if (ret < 0 || ret > MAX_OBJ_NAME_SIZE) {
Alex Elder65ccfe22012-08-09 10:33:26 -07001013 pr_err("error formatting segment name for #%llu (%d)\n",
1014 segment, ret);
1015 kfree(name);
1016 name = NULL;
1017 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001018
Alex Elder65ccfe22012-08-09 10:33:26 -07001019 return name;
1020}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001021
Alex Elder78c2a442013-05-01 12:43:04 -05001022static void rbd_segment_name_free(const char *name)
1023{
1024 /* The explicit cast here is needed to drop the const qualifier */
1025
1026 kmem_cache_free(rbd_segment_name_cache, (void *)name);
1027}
1028
Alex Elder65ccfe22012-08-09 10:33:26 -07001029static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset)
1030{
1031 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001032
Alex Elder65ccfe22012-08-09 10:33:26 -07001033 return offset & (segment_size - 1);
1034}
1035
1036static u64 rbd_segment_length(struct rbd_device *rbd_dev,
1037 u64 offset, u64 length)
1038{
1039 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
1040
1041 offset &= segment_size - 1;
1042
Alex Elderaafb2302012-09-06 16:00:54 -05001043 rbd_assert(length <= U64_MAX - offset);
Alex Elder65ccfe22012-08-09 10:33:26 -07001044 if (offset + length > segment_size)
1045 length = segment_size - offset;
1046
1047 return length;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001048}
1049
1050/*
Josh Durgin029bcbd2011-07-22 11:35:23 -07001051 * returns the size of an object in the image
1052 */
1053static u64 rbd_obj_bytes(struct rbd_image_header *header)
1054{
1055 return 1 << header->obj_order;
1056}
1057
1058/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001059 * bio helpers
1060 */
1061
1062static void bio_chain_put(struct bio *chain)
1063{
1064 struct bio *tmp;
1065
1066 while (chain) {
1067 tmp = chain;
1068 chain = chain->bi_next;
1069 bio_put(tmp);
1070 }
1071}
1072
1073/*
1074 * zeros a bio chain, starting at specific offset
1075 */
1076static void zero_bio_chain(struct bio *chain, int start_ofs)
1077{
1078 struct bio_vec *bv;
1079 unsigned long flags;
1080 void *buf;
1081 int i;
1082 int pos = 0;
1083
1084 while (chain) {
1085 bio_for_each_segment(bv, chain, i) {
1086 if (pos + bv->bv_len > start_ofs) {
1087 int remainder = max(start_ofs - pos, 0);
1088 buf = bvec_kmap_irq(bv, &flags);
1089 memset(buf + remainder, 0,
1090 bv->bv_len - remainder);
Dan Carpenter85b5aaa2010-10-11 21:15:11 +02001091 bvec_kunmap_irq(buf, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001092 }
1093 pos += bv->bv_len;
1094 }
1095
1096 chain = chain->bi_next;
1097 }
1098}
1099
1100/*
Alex Elderb9434c52013-04-19 15:34:50 -05001101 * similar to zero_bio_chain(), zeros data defined by a page array,
1102 * starting at the given byte offset from the start of the array and
1103 * continuing up to the given end offset. The pages array is
1104 * assumed to be big enough to hold all bytes up to the end.
1105 */
1106static void zero_pages(struct page **pages, u64 offset, u64 end)
1107{
1108 struct page **page = &pages[offset >> PAGE_SHIFT];
1109
1110 rbd_assert(end > offset);
1111 rbd_assert(end - offset <= (u64)SIZE_MAX);
1112 while (offset < end) {
1113 size_t page_offset;
1114 size_t length;
1115 unsigned long flags;
1116 void *kaddr;
1117
1118 page_offset = (size_t)(offset & ~PAGE_MASK);
1119 length = min(PAGE_SIZE - page_offset, (size_t)(end - offset));
1120 local_irq_save(flags);
1121 kaddr = kmap_atomic(*page);
1122 memset(kaddr + page_offset, 0, length);
1123 kunmap_atomic(kaddr);
1124 local_irq_restore(flags);
1125
1126 offset += length;
1127 page++;
1128 }
1129}
1130
1131/*
Alex Elderf7760da2012-10-20 22:17:27 -05001132 * Clone a portion of a bio, starting at the given byte offset
1133 * and continuing for the number of bytes indicated.
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001134 */
Alex Elderf7760da2012-10-20 22:17:27 -05001135static struct bio *bio_clone_range(struct bio *bio_src,
1136 unsigned int offset,
1137 unsigned int len,
1138 gfp_t gfpmask)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001139{
Alex Elderf7760da2012-10-20 22:17:27 -05001140 struct bio_vec *bv;
1141 unsigned int resid;
1142 unsigned short idx;
1143 unsigned int voff;
1144 unsigned short end_idx;
1145 unsigned short vcnt;
1146 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001147
Alex Elderf7760da2012-10-20 22:17:27 -05001148 /* Handle the easy case for the caller */
1149
1150 if (!offset && len == bio_src->bi_size)
1151 return bio_clone(bio_src, gfpmask);
1152
1153 if (WARN_ON_ONCE(!len))
1154 return NULL;
1155 if (WARN_ON_ONCE(len > bio_src->bi_size))
1156 return NULL;
1157 if (WARN_ON_ONCE(offset > bio_src->bi_size - len))
1158 return NULL;
1159
1160 /* Find first affected segment... */
1161
1162 resid = offset;
1163 __bio_for_each_segment(bv, bio_src, idx, 0) {
1164 if (resid < bv->bv_len)
1165 break;
1166 resid -= bv->bv_len;
1167 }
1168 voff = resid;
1169
1170 /* ...and the last affected segment */
1171
1172 resid += len;
1173 __bio_for_each_segment(bv, bio_src, end_idx, idx) {
1174 if (resid <= bv->bv_len)
1175 break;
1176 resid -= bv->bv_len;
1177 }
1178 vcnt = end_idx - idx + 1;
1179
1180 /* Build the clone */
1181
1182 bio = bio_alloc(gfpmask, (unsigned int) vcnt);
1183 if (!bio)
1184 return NULL; /* ENOMEM */
1185
1186 bio->bi_bdev = bio_src->bi_bdev;
1187 bio->bi_sector = bio_src->bi_sector + (offset >> SECTOR_SHIFT);
1188 bio->bi_rw = bio_src->bi_rw;
1189 bio->bi_flags |= 1 << BIO_CLONED;
1190
1191 /*
1192 * Copy over our part of the bio_vec, then update the first
1193 * and last (or only) entries.
1194 */
1195 memcpy(&bio->bi_io_vec[0], &bio_src->bi_io_vec[idx],
1196 vcnt * sizeof (struct bio_vec));
1197 bio->bi_io_vec[0].bv_offset += voff;
1198 if (vcnt > 1) {
1199 bio->bi_io_vec[0].bv_len -= voff;
1200 bio->bi_io_vec[vcnt - 1].bv_len = resid;
1201 } else {
1202 bio->bi_io_vec[0].bv_len = len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001203 }
1204
Alex Elderf7760da2012-10-20 22:17:27 -05001205 bio->bi_vcnt = vcnt;
1206 bio->bi_size = len;
1207 bio->bi_idx = 0;
Alex Elder542582f2012-08-09 10:33:25 -07001208
Alex Elderf7760da2012-10-20 22:17:27 -05001209 return bio;
1210}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001211
Alex Elderf7760da2012-10-20 22:17:27 -05001212/*
1213 * Clone a portion of a bio chain, starting at the given byte offset
1214 * into the first bio in the source chain and continuing for the
1215 * number of bytes indicated. The result is another bio chain of
1216 * exactly the given length, or a null pointer on error.
1217 *
1218 * The bio_src and offset parameters are both in-out. On entry they
1219 * refer to the first source bio and the offset into that bio where
1220 * the start of data to be cloned is located.
1221 *
1222 * On return, bio_src is updated to refer to the bio in the source
1223 * chain that contains first un-cloned byte, and *offset will
1224 * contain the offset of that byte within that bio.
1225 */
1226static struct bio *bio_chain_clone_range(struct bio **bio_src,
1227 unsigned int *offset,
1228 unsigned int len,
1229 gfp_t gfpmask)
1230{
1231 struct bio *bi = *bio_src;
1232 unsigned int off = *offset;
1233 struct bio *chain = NULL;
1234 struct bio **end;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001235
Alex Elderf7760da2012-10-20 22:17:27 -05001236 /* Build up a chain of clone bios up to the limit */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001237
Alex Elderf7760da2012-10-20 22:17:27 -05001238 if (!bi || off >= bi->bi_size || !len)
1239 return NULL; /* Nothing to clone */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001240
Alex Elderf7760da2012-10-20 22:17:27 -05001241 end = &chain;
1242 while (len) {
1243 unsigned int bi_size;
1244 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001245
Alex Elderf5400b72012-11-01 10:17:15 -05001246 if (!bi) {
1247 rbd_warn(NULL, "bio_chain exhausted with %u left", len);
Alex Elderf7760da2012-10-20 22:17:27 -05001248 goto out_err; /* EINVAL; ran out of bio's */
Alex Elderf5400b72012-11-01 10:17:15 -05001249 }
Alex Elderf7760da2012-10-20 22:17:27 -05001250 bi_size = min_t(unsigned int, bi->bi_size - off, len);
1251 bio = bio_clone_range(bi, off, bi_size, gfpmask);
1252 if (!bio)
1253 goto out_err; /* ENOMEM */
1254
1255 *end = bio;
1256 end = &bio->bi_next;
1257
1258 off += bi_size;
1259 if (off == bi->bi_size) {
1260 bi = bi->bi_next;
1261 off = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001262 }
Alex Elderf7760da2012-10-20 22:17:27 -05001263 len -= bi_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001264 }
Alex Elderf7760da2012-10-20 22:17:27 -05001265 *bio_src = bi;
1266 *offset = off;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001267
Alex Elderf7760da2012-10-20 22:17:27 -05001268 return chain;
1269out_err:
1270 bio_chain_put(chain);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001271
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001272 return NULL;
1273}
1274
Alex Elder926f9b32013-02-11 12:33:24 -06001275/*
1276 * The default/initial value for all object request flags is 0. For
1277 * each flag, once its value is set to 1 it is never reset to 0
1278 * again.
1279 */
Alex Elder6365d332013-02-11 12:33:24 -06001280static void obj_request_img_data_set(struct rbd_obj_request *obj_request)
1281{
1282 if (test_and_set_bit(OBJ_REQ_IMG_DATA, &obj_request->flags)) {
Alex Elder6365d332013-02-11 12:33:24 -06001283 struct rbd_device *rbd_dev;
1284
Alex Elder57acbaa2013-02-11 12:33:24 -06001285 rbd_dev = obj_request->img_request->rbd_dev;
Alex Elder6365d332013-02-11 12:33:24 -06001286 rbd_warn(rbd_dev, "obj_request %p already marked img_data\n",
1287 obj_request);
1288 }
1289}
1290
1291static bool obj_request_img_data_test(struct rbd_obj_request *obj_request)
1292{
1293 smp_mb();
1294 return test_bit(OBJ_REQ_IMG_DATA, &obj_request->flags) != 0;
1295}
1296
Alex Elder57acbaa2013-02-11 12:33:24 -06001297static void obj_request_done_set(struct rbd_obj_request *obj_request)
1298{
1299 if (test_and_set_bit(OBJ_REQ_DONE, &obj_request->flags)) {
1300 struct rbd_device *rbd_dev = NULL;
1301
1302 if (obj_request_img_data_test(obj_request))
1303 rbd_dev = obj_request->img_request->rbd_dev;
1304 rbd_warn(rbd_dev, "obj_request %p already marked done\n",
1305 obj_request);
1306 }
1307}
1308
1309static bool obj_request_done_test(struct rbd_obj_request *obj_request)
1310{
1311 smp_mb();
1312 return test_bit(OBJ_REQ_DONE, &obj_request->flags) != 0;
1313}
1314
Alex Elder5679c592013-02-11 12:33:24 -06001315/*
1316 * This sets the KNOWN flag after (possibly) setting the EXISTS
1317 * flag. The latter is set based on the "exists" value provided.
1318 *
1319 * Note that for our purposes once an object exists it never goes
1320 * away again. It's possible that the response from two existence
1321 * checks are separated by the creation of the target object, and
1322 * the first ("doesn't exist") response arrives *after* the second
1323 * ("does exist"). In that case we ignore the second one.
1324 */
1325static void obj_request_existence_set(struct rbd_obj_request *obj_request,
1326 bool exists)
1327{
1328 if (exists)
1329 set_bit(OBJ_REQ_EXISTS, &obj_request->flags);
1330 set_bit(OBJ_REQ_KNOWN, &obj_request->flags);
1331 smp_mb();
1332}
1333
1334static bool obj_request_known_test(struct rbd_obj_request *obj_request)
1335{
1336 smp_mb();
1337 return test_bit(OBJ_REQ_KNOWN, &obj_request->flags) != 0;
1338}
1339
1340static bool obj_request_exists_test(struct rbd_obj_request *obj_request)
1341{
1342 smp_mb();
1343 return test_bit(OBJ_REQ_EXISTS, &obj_request->flags) != 0;
1344}
1345
Alex Elderbf0d5f502012-11-22 00:00:08 -06001346static void rbd_obj_request_get(struct rbd_obj_request *obj_request)
1347{
Alex Elder37206ee2013-02-20 17:32:08 -06001348 dout("%s: obj %p (was %d)\n", __func__, obj_request,
1349 atomic_read(&obj_request->kref.refcount));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001350 kref_get(&obj_request->kref);
1351}
1352
1353static void rbd_obj_request_destroy(struct kref *kref);
1354static void rbd_obj_request_put(struct rbd_obj_request *obj_request)
1355{
1356 rbd_assert(obj_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001357 dout("%s: obj %p (was %d)\n", __func__, obj_request,
1358 atomic_read(&obj_request->kref.refcount));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001359 kref_put(&obj_request->kref, rbd_obj_request_destroy);
1360}
1361
Alex Elderbf0d5f502012-11-22 00:00:08 -06001362static void rbd_img_request_destroy(struct kref *kref);
1363static void rbd_img_request_put(struct rbd_img_request *img_request)
1364{
1365 rbd_assert(img_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001366 dout("%s: img %p (was %d)\n", __func__, img_request,
1367 atomic_read(&img_request->kref.refcount));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001368 kref_put(&img_request->kref, rbd_img_request_destroy);
1369}
1370
1371static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request,
1372 struct rbd_obj_request *obj_request)
1373{
Alex Elder25dcf952013-01-25 17:08:55 -06001374 rbd_assert(obj_request->img_request == NULL);
1375
Alex Elderb155e862013-04-15 14:50:37 -05001376 /* Image request now owns object's original reference */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001377 obj_request->img_request = img_request;
Alex Elder25dcf952013-01-25 17:08:55 -06001378 obj_request->which = img_request->obj_request_count;
Alex Elder6365d332013-02-11 12:33:24 -06001379 rbd_assert(!obj_request_img_data_test(obj_request));
1380 obj_request_img_data_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001381 rbd_assert(obj_request->which != BAD_WHICH);
Alex Elder25dcf952013-01-25 17:08:55 -06001382 img_request->obj_request_count++;
1383 list_add_tail(&obj_request->links, &img_request->obj_requests);
Alex Elder37206ee2013-02-20 17:32:08 -06001384 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1385 obj_request->which);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001386}
1387
1388static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request,
1389 struct rbd_obj_request *obj_request)
1390{
1391 rbd_assert(obj_request->which != BAD_WHICH);
Alex Elder25dcf952013-01-25 17:08:55 -06001392
Alex Elder37206ee2013-02-20 17:32:08 -06001393 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1394 obj_request->which);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001395 list_del(&obj_request->links);
Alex Elder25dcf952013-01-25 17:08:55 -06001396 rbd_assert(img_request->obj_request_count > 0);
1397 img_request->obj_request_count--;
1398 rbd_assert(obj_request->which == img_request->obj_request_count);
1399 obj_request->which = BAD_WHICH;
Alex Elder6365d332013-02-11 12:33:24 -06001400 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001401 rbd_assert(obj_request->img_request == img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001402 obj_request->img_request = NULL;
Alex Elder25dcf952013-01-25 17:08:55 -06001403 obj_request->callback = NULL;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001404 rbd_obj_request_put(obj_request);
1405}
1406
1407static bool obj_request_type_valid(enum obj_request_type type)
1408{
1409 switch (type) {
Alex Elder9969ebc2013-01-18 12:31:10 -06001410 case OBJ_REQUEST_NODATA:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001411 case OBJ_REQUEST_BIO:
Alex Elder788e2df2013-01-17 12:25:27 -06001412 case OBJ_REQUEST_PAGES:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001413 return true;
1414 default:
1415 return false;
1416 }
1417}
1418
Alex Elderbf0d5f502012-11-22 00:00:08 -06001419static int rbd_obj_request_submit(struct ceph_osd_client *osdc,
1420 struct rbd_obj_request *obj_request)
1421{
Alex Elder37206ee2013-02-20 17:32:08 -06001422 dout("%s: osdc %p obj %p\n", __func__, osdc, obj_request);
1423
Alex Elderbf0d5f502012-11-22 00:00:08 -06001424 return ceph_osdc_start_request(osdc, obj_request->osd_req, false);
1425}
1426
1427static void rbd_img_request_complete(struct rbd_img_request *img_request)
1428{
Alex Elder55f27e02013-04-10 12:34:25 -05001429
Alex Elder37206ee2013-02-20 17:32:08 -06001430 dout("%s: img %p\n", __func__, img_request);
Alex Elder55f27e02013-04-10 12:34:25 -05001431
1432 /*
1433 * If no error occurred, compute the aggregate transfer
1434 * count for the image request. We could instead use
1435 * atomic64_cmpxchg() to update it as each object request
1436 * completes; not clear which way is better off hand.
1437 */
1438 if (!img_request->result) {
1439 struct rbd_obj_request *obj_request;
1440 u64 xferred = 0;
1441
1442 for_each_obj_request(img_request, obj_request)
1443 xferred += obj_request->xferred;
1444 img_request->xferred = xferred;
1445 }
1446
Alex Elderbf0d5f502012-11-22 00:00:08 -06001447 if (img_request->callback)
1448 img_request->callback(img_request);
1449 else
1450 rbd_img_request_put(img_request);
1451}
1452
Alex Elder788e2df2013-01-17 12:25:27 -06001453/* Caller is responsible for rbd_obj_request_destroy(obj_request) */
1454
1455static int rbd_obj_request_wait(struct rbd_obj_request *obj_request)
1456{
Alex Elder37206ee2013-02-20 17:32:08 -06001457 dout("%s: obj %p\n", __func__, obj_request);
1458
Alex Elder788e2df2013-01-17 12:25:27 -06001459 return wait_for_completion_interruptible(&obj_request->completion);
1460}
1461
Alex Elder0c425242013-02-08 09:55:49 -06001462/*
1463 * The default/initial value for all image request flags is 0. Each
1464 * is conditionally set to 1 at image request initialization time
1465 * and currently never change thereafter.
1466 */
1467static void img_request_write_set(struct rbd_img_request *img_request)
1468{
1469 set_bit(IMG_REQ_WRITE, &img_request->flags);
1470 smp_mb();
1471}
1472
1473static bool img_request_write_test(struct rbd_img_request *img_request)
1474{
1475 smp_mb();
1476 return test_bit(IMG_REQ_WRITE, &img_request->flags) != 0;
1477}
1478
Alex Elder9849e982013-01-24 16:13:36 -06001479static void img_request_child_set(struct rbd_img_request *img_request)
1480{
1481 set_bit(IMG_REQ_CHILD, &img_request->flags);
1482 smp_mb();
1483}
1484
1485static bool img_request_child_test(struct rbd_img_request *img_request)
1486{
1487 smp_mb();
1488 return test_bit(IMG_REQ_CHILD, &img_request->flags) != 0;
1489}
1490
Alex Elderd0b2e942013-01-24 16:13:36 -06001491static void img_request_layered_set(struct rbd_img_request *img_request)
1492{
1493 set_bit(IMG_REQ_LAYERED, &img_request->flags);
1494 smp_mb();
1495}
1496
1497static bool img_request_layered_test(struct rbd_img_request *img_request)
1498{
1499 smp_mb();
1500 return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0;
1501}
1502
Alex Elder6e2a4502013-03-27 09:16:30 -05001503static void
1504rbd_img_obj_request_read_callback(struct rbd_obj_request *obj_request)
1505{
Alex Elderb9434c52013-04-19 15:34:50 -05001506 u64 xferred = obj_request->xferred;
1507 u64 length = obj_request->length;
1508
Alex Elder6e2a4502013-03-27 09:16:30 -05001509 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1510 obj_request, obj_request->img_request, obj_request->result,
Alex Elderb9434c52013-04-19 15:34:50 -05001511 xferred, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001512 /*
1513 * ENOENT means a hole in the image. We zero-fill the
1514 * entire length of the request. A short read also implies
1515 * zero-fill to the end of the request. Either way we
1516 * update the xferred count to indicate the whole request
1517 * was satisfied.
1518 */
Alex Elderb9434c52013-04-19 15:34:50 -05001519 rbd_assert(obj_request->type != OBJ_REQUEST_NODATA);
Alex Elder6e2a4502013-03-27 09:16:30 -05001520 if (obj_request->result == -ENOENT) {
Alex Elderb9434c52013-04-19 15:34:50 -05001521 if (obj_request->type == OBJ_REQUEST_BIO)
1522 zero_bio_chain(obj_request->bio_list, 0);
1523 else
1524 zero_pages(obj_request->pages, 0, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001525 obj_request->result = 0;
Alex Elderb9434c52013-04-19 15:34:50 -05001526 obj_request->xferred = length;
1527 } else if (xferred < length && !obj_request->result) {
1528 if (obj_request->type == OBJ_REQUEST_BIO)
1529 zero_bio_chain(obj_request->bio_list, xferred);
1530 else
1531 zero_pages(obj_request->pages, xferred, length);
1532 obj_request->xferred = length;
Alex Elder6e2a4502013-03-27 09:16:30 -05001533 }
1534 obj_request_done_set(obj_request);
1535}
1536
Alex Elderbf0d5f502012-11-22 00:00:08 -06001537static void rbd_obj_request_complete(struct rbd_obj_request *obj_request)
1538{
Alex Elder37206ee2013-02-20 17:32:08 -06001539 dout("%s: obj %p cb %p\n", __func__, obj_request,
1540 obj_request->callback);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001541 if (obj_request->callback)
1542 obj_request->callback(obj_request);
Alex Elder788e2df2013-01-17 12:25:27 -06001543 else
1544 complete_all(&obj_request->completion);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001545}
1546
Alex Elderc47f9372013-02-26 14:23:07 -06001547static void rbd_osd_trivial_callback(struct rbd_obj_request *obj_request)
Alex Elder39bf2c52013-02-26 14:23:07 -06001548{
1549 dout("%s: obj %p\n", __func__, obj_request);
1550 obj_request_done_set(obj_request);
1551}
1552
Alex Elderc47f9372013-02-26 14:23:07 -06001553static void rbd_osd_read_callback(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001554{
Alex Elder57acbaa2013-02-11 12:33:24 -06001555 struct rbd_img_request *img_request = NULL;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001556 struct rbd_device *rbd_dev = NULL;
Alex Elder57acbaa2013-02-11 12:33:24 -06001557 bool layered = false;
1558
1559 if (obj_request_img_data_test(obj_request)) {
1560 img_request = obj_request->img_request;
1561 layered = img_request && img_request_layered_test(img_request);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001562 rbd_dev = img_request->rbd_dev;
Alex Elder57acbaa2013-02-11 12:33:24 -06001563 }
Alex Elder8b3e1a52013-01-24 16:13:36 -06001564
1565 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1566 obj_request, img_request, obj_request->result,
1567 obj_request->xferred, obj_request->length);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001568 if (layered && obj_request->result == -ENOENT &&
1569 obj_request->img_offset < rbd_dev->parent_overlap)
Alex Elder8b3e1a52013-01-24 16:13:36 -06001570 rbd_img_parent_read(obj_request);
1571 else if (img_request)
Alex Elder6e2a4502013-03-27 09:16:30 -05001572 rbd_img_obj_request_read_callback(obj_request);
1573 else
1574 obj_request_done_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001575}
1576
Alex Elderc47f9372013-02-26 14:23:07 -06001577static void rbd_osd_write_callback(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001578{
Sage Weil1b83bef2013-02-25 16:11:12 -08001579 dout("%s: obj %p result %d %llu\n", __func__, obj_request,
1580 obj_request->result, obj_request->length);
1581 /*
Alex Elder8b3e1a52013-01-24 16:13:36 -06001582 * There is no such thing as a successful short write. Set
1583 * it to our originally-requested length.
Sage Weil1b83bef2013-02-25 16:11:12 -08001584 */
1585 obj_request->xferred = obj_request->length;
Alex Elder07741302013-02-05 23:41:50 -06001586 obj_request_done_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001587}
1588
Alex Elderfbfab532013-02-08 09:55:48 -06001589/*
1590 * For a simple stat call there's nothing to do. We'll do more if
1591 * this is part of a write sequence for a layered image.
1592 */
Alex Elderc47f9372013-02-26 14:23:07 -06001593static void rbd_osd_stat_callback(struct rbd_obj_request *obj_request)
Alex Elderfbfab532013-02-08 09:55:48 -06001594{
Alex Elder37206ee2013-02-20 17:32:08 -06001595 dout("%s: obj %p\n", __func__, obj_request);
Alex Elderfbfab532013-02-08 09:55:48 -06001596 obj_request_done_set(obj_request);
1597}
1598
Alex Elderbf0d5f502012-11-22 00:00:08 -06001599static void rbd_osd_req_callback(struct ceph_osd_request *osd_req,
1600 struct ceph_msg *msg)
1601{
1602 struct rbd_obj_request *obj_request = osd_req->r_priv;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001603 u16 opcode;
1604
Alex Elder37206ee2013-02-20 17:32:08 -06001605 dout("%s: osd_req %p msg %p\n", __func__, osd_req, msg);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001606 rbd_assert(osd_req == obj_request->osd_req);
Alex Elder57acbaa2013-02-11 12:33:24 -06001607 if (obj_request_img_data_test(obj_request)) {
1608 rbd_assert(obj_request->img_request);
1609 rbd_assert(obj_request->which != BAD_WHICH);
1610 } else {
1611 rbd_assert(obj_request->which == BAD_WHICH);
1612 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06001613
Sage Weil1b83bef2013-02-25 16:11:12 -08001614 if (osd_req->r_result < 0)
1615 obj_request->result = osd_req->r_result;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001616
Alex Elder0eefd472013-04-19 15:34:50 -05001617 BUG_ON(osd_req->r_num_ops > 2);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001618
Alex Elderc47f9372013-02-26 14:23:07 -06001619 /*
1620 * We support a 64-bit length, but ultimately it has to be
1621 * passed to blk_end_request(), which takes an unsigned int.
1622 */
Sage Weil1b83bef2013-02-25 16:11:12 -08001623 obj_request->xferred = osd_req->r_reply_op_len[0];
Alex Elder8b3e1a52013-01-24 16:13:36 -06001624 rbd_assert(obj_request->xferred < (u64)UINT_MAX);
Alex Elder79528732013-04-03 21:32:51 -05001625 opcode = osd_req->r_ops[0].op;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001626 switch (opcode) {
1627 case CEPH_OSD_OP_READ:
Alex Elderc47f9372013-02-26 14:23:07 -06001628 rbd_osd_read_callback(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001629 break;
1630 case CEPH_OSD_OP_WRITE:
Alex Elderc47f9372013-02-26 14:23:07 -06001631 rbd_osd_write_callback(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001632 break;
Alex Elderfbfab532013-02-08 09:55:48 -06001633 case CEPH_OSD_OP_STAT:
Alex Elderc47f9372013-02-26 14:23:07 -06001634 rbd_osd_stat_callback(obj_request);
Alex Elderfbfab532013-02-08 09:55:48 -06001635 break;
Alex Elder36be9a72013-01-19 00:30:28 -06001636 case CEPH_OSD_OP_CALL:
Alex Elderb8d70032012-11-30 17:53:04 -06001637 case CEPH_OSD_OP_NOTIFY_ACK:
Alex Elder9969ebc2013-01-18 12:31:10 -06001638 case CEPH_OSD_OP_WATCH:
Alex Elderc47f9372013-02-26 14:23:07 -06001639 rbd_osd_trivial_callback(obj_request);
Alex Elder9969ebc2013-01-18 12:31:10 -06001640 break;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001641 default:
1642 rbd_warn(NULL, "%s: unsupported op %hu\n",
1643 obj_request->object_name, (unsigned short) opcode);
1644 break;
1645 }
1646
Alex Elder07741302013-02-05 23:41:50 -06001647 if (obj_request_done_test(obj_request))
Alex Elderbf0d5f502012-11-22 00:00:08 -06001648 rbd_obj_request_complete(obj_request);
1649}
1650
Alex Elder9d4df012013-04-19 15:34:50 -05001651static void rbd_osd_req_format_read(struct rbd_obj_request *obj_request)
Alex Elder430c28c2013-04-03 21:32:51 -05001652{
1653 struct rbd_img_request *img_request = obj_request->img_request;
Alex Elder8c042b02013-04-03 01:28:58 -05001654 struct ceph_osd_request *osd_req = obj_request->osd_req;
Alex Elder9d4df012013-04-19 15:34:50 -05001655 u64 snap_id;
Alex Elder430c28c2013-04-03 21:32:51 -05001656
Alex Elder8c042b02013-04-03 01:28:58 -05001657 rbd_assert(osd_req != NULL);
Alex Elder430c28c2013-04-03 21:32:51 -05001658
Alex Elder9d4df012013-04-19 15:34:50 -05001659 snap_id = img_request ? img_request->snap_id : CEPH_NOSNAP;
Alex Elder8c042b02013-04-03 01:28:58 -05001660 ceph_osdc_build_request(osd_req, obj_request->offset,
Alex Elder9d4df012013-04-19 15:34:50 -05001661 NULL, snap_id, NULL);
1662}
1663
1664static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request)
1665{
1666 struct rbd_img_request *img_request = obj_request->img_request;
1667 struct ceph_osd_request *osd_req = obj_request->osd_req;
1668 struct ceph_snap_context *snapc;
1669 struct timespec mtime = CURRENT_TIME;
1670
1671 rbd_assert(osd_req != NULL);
1672
1673 snapc = img_request ? img_request->snapc : NULL;
1674 ceph_osdc_build_request(osd_req, obj_request->offset,
1675 snapc, CEPH_NOSNAP, &mtime);
Alex Elder430c28c2013-04-03 21:32:51 -05001676}
1677
Alex Elderbf0d5f502012-11-22 00:00:08 -06001678static struct ceph_osd_request *rbd_osd_req_create(
1679 struct rbd_device *rbd_dev,
1680 bool write_request,
Alex Elder430c28c2013-04-03 21:32:51 -05001681 struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001682{
Alex Elderbf0d5f502012-11-22 00:00:08 -06001683 struct ceph_snap_context *snapc = NULL;
1684 struct ceph_osd_client *osdc;
1685 struct ceph_osd_request *osd_req;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001686
Alex Elder6365d332013-02-11 12:33:24 -06001687 if (obj_request_img_data_test(obj_request)) {
1688 struct rbd_img_request *img_request = obj_request->img_request;
1689
Alex Elder0c425242013-02-08 09:55:49 -06001690 rbd_assert(write_request ==
1691 img_request_write_test(img_request));
1692 if (write_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001693 snapc = img_request->snapc;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001694 }
1695
1696 /* Allocate and initialize the request, for the single op */
1697
1698 osdc = &rbd_dev->rbd_client->client->osdc;
1699 osd_req = ceph_osdc_alloc_request(osdc, snapc, 1, false, GFP_ATOMIC);
1700 if (!osd_req)
1701 return NULL; /* ENOMEM */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001702
Alex Elder430c28c2013-04-03 21:32:51 -05001703 if (write_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001704 osd_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK;
Alex Elder430c28c2013-04-03 21:32:51 -05001705 else
Alex Elderbf0d5f502012-11-22 00:00:08 -06001706 osd_req->r_flags = CEPH_OSD_FLAG_READ;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001707
1708 osd_req->r_callback = rbd_osd_req_callback;
1709 osd_req->r_priv = obj_request;
1710
1711 osd_req->r_oid_len = strlen(obj_request->object_name);
1712 rbd_assert(osd_req->r_oid_len < sizeof (osd_req->r_oid));
1713 memcpy(osd_req->r_oid, obj_request->object_name, osd_req->r_oid_len);
1714
1715 osd_req->r_file_layout = rbd_dev->layout; /* struct */
1716
Alex Elderbf0d5f502012-11-22 00:00:08 -06001717 return osd_req;
1718}
1719
Alex Elder0eefd472013-04-19 15:34:50 -05001720/*
1721 * Create a copyup osd request based on the information in the
1722 * object request supplied. A copyup request has two osd ops,
1723 * a copyup method call, and a "normal" write request.
1724 */
1725static struct ceph_osd_request *
1726rbd_osd_req_create_copyup(struct rbd_obj_request *obj_request)
1727{
1728 struct rbd_img_request *img_request;
1729 struct ceph_snap_context *snapc;
1730 struct rbd_device *rbd_dev;
1731 struct ceph_osd_client *osdc;
1732 struct ceph_osd_request *osd_req;
1733
1734 rbd_assert(obj_request_img_data_test(obj_request));
1735 img_request = obj_request->img_request;
1736 rbd_assert(img_request);
1737 rbd_assert(img_request_write_test(img_request));
1738
1739 /* Allocate and initialize the request, for the two ops */
1740
1741 snapc = img_request->snapc;
1742 rbd_dev = img_request->rbd_dev;
1743 osdc = &rbd_dev->rbd_client->client->osdc;
1744 osd_req = ceph_osdc_alloc_request(osdc, snapc, 2, false, GFP_ATOMIC);
1745 if (!osd_req)
1746 return NULL; /* ENOMEM */
1747
1748 osd_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK;
1749 osd_req->r_callback = rbd_osd_req_callback;
1750 osd_req->r_priv = obj_request;
1751
1752 osd_req->r_oid_len = strlen(obj_request->object_name);
1753 rbd_assert(osd_req->r_oid_len < sizeof (osd_req->r_oid));
1754 memcpy(osd_req->r_oid, obj_request->object_name, osd_req->r_oid_len);
1755
1756 osd_req->r_file_layout = rbd_dev->layout; /* struct */
1757
1758 return osd_req;
1759}
1760
1761
Alex Elderbf0d5f502012-11-22 00:00:08 -06001762static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req)
1763{
1764 ceph_osdc_put_request(osd_req);
1765}
1766
1767/* object_name is assumed to be a non-null pointer and NUL-terminated */
1768
1769static struct rbd_obj_request *rbd_obj_request_create(const char *object_name,
1770 u64 offset, u64 length,
1771 enum obj_request_type type)
1772{
1773 struct rbd_obj_request *obj_request;
1774 size_t size;
1775 char *name;
1776
1777 rbd_assert(obj_request_type_valid(type));
1778
1779 size = strlen(object_name) + 1;
Alex Elderf907ad52013-05-01 12:43:03 -05001780 name = kmalloc(size, GFP_KERNEL);
1781 if (!name)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001782 return NULL;
1783
Alex Elder868311b2013-05-01 12:43:03 -05001784 obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_KERNEL);
Alex Elderf907ad52013-05-01 12:43:03 -05001785 if (!obj_request) {
1786 kfree(name);
1787 return NULL;
1788 }
1789
Alex Elderbf0d5f502012-11-22 00:00:08 -06001790 obj_request->object_name = memcpy(name, object_name, size);
1791 obj_request->offset = offset;
1792 obj_request->length = length;
Alex Elder926f9b32013-02-11 12:33:24 -06001793 obj_request->flags = 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001794 obj_request->which = BAD_WHICH;
1795 obj_request->type = type;
1796 INIT_LIST_HEAD(&obj_request->links);
Alex Elder788e2df2013-01-17 12:25:27 -06001797 init_completion(&obj_request->completion);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001798 kref_init(&obj_request->kref);
1799
Alex Elder37206ee2013-02-20 17:32:08 -06001800 dout("%s: \"%s\" %llu/%llu %d -> obj %p\n", __func__, object_name,
1801 offset, length, (int)type, obj_request);
1802
Alex Elderbf0d5f502012-11-22 00:00:08 -06001803 return obj_request;
1804}
1805
1806static void rbd_obj_request_destroy(struct kref *kref)
1807{
1808 struct rbd_obj_request *obj_request;
1809
1810 obj_request = container_of(kref, struct rbd_obj_request, kref);
1811
Alex Elder37206ee2013-02-20 17:32:08 -06001812 dout("%s: obj %p\n", __func__, obj_request);
1813
Alex Elderbf0d5f502012-11-22 00:00:08 -06001814 rbd_assert(obj_request->img_request == NULL);
1815 rbd_assert(obj_request->which == BAD_WHICH);
1816
1817 if (obj_request->osd_req)
1818 rbd_osd_req_destroy(obj_request->osd_req);
1819
1820 rbd_assert(obj_request_type_valid(obj_request->type));
1821 switch (obj_request->type) {
Alex Elder9969ebc2013-01-18 12:31:10 -06001822 case OBJ_REQUEST_NODATA:
1823 break; /* Nothing to do */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001824 case OBJ_REQUEST_BIO:
1825 if (obj_request->bio_list)
1826 bio_chain_put(obj_request->bio_list);
1827 break;
Alex Elder788e2df2013-01-17 12:25:27 -06001828 case OBJ_REQUEST_PAGES:
1829 if (obj_request->pages)
1830 ceph_release_page_vector(obj_request->pages,
1831 obj_request->page_count);
1832 break;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001833 }
1834
Alex Elderf907ad52013-05-01 12:43:03 -05001835 kfree(obj_request->object_name);
Alex Elder868311b2013-05-01 12:43:03 -05001836 obj_request->object_name = NULL;
1837 kmem_cache_free(rbd_obj_request_cache, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001838}
1839
Alex Elderfb65d2282013-05-08 22:50:04 -05001840/* It's OK to call this for a device with no parent */
1841
1842static void rbd_spec_put(struct rbd_spec *spec);
1843static void rbd_dev_unparent(struct rbd_device *rbd_dev)
1844{
1845 rbd_dev_remove_parent(rbd_dev);
1846 rbd_spec_put(rbd_dev->parent_spec);
1847 rbd_dev->parent_spec = NULL;
1848 rbd_dev->parent_overlap = 0;
1849}
1850
Alex Elderbf0d5f502012-11-22 00:00:08 -06001851/*
1852 * Caller is responsible for filling in the list of object requests
1853 * that comprises the image request, and the Linux request pointer
1854 * (if there is one).
1855 */
Alex Eldercc344fa2013-02-19 12:25:56 -06001856static struct rbd_img_request *rbd_img_request_create(
1857 struct rbd_device *rbd_dev,
Alex Elderbf0d5f502012-11-22 00:00:08 -06001858 u64 offset, u64 length,
Alex Elder9849e982013-01-24 16:13:36 -06001859 bool write_request,
1860 bool child_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001861{
1862 struct rbd_img_request *img_request;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001863
Alex Elder1c2a9df2013-05-01 12:43:03 -05001864 img_request = kmem_cache_alloc(rbd_img_request_cache, GFP_ATOMIC);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001865 if (!img_request)
1866 return NULL;
1867
1868 if (write_request) {
1869 down_read(&rbd_dev->header_rwsem);
Alex Elder812164f82013-04-30 00:44:32 -05001870 ceph_get_snap_context(rbd_dev->header.snapc);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001871 up_read(&rbd_dev->header_rwsem);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001872 }
1873
1874 img_request->rq = NULL;
1875 img_request->rbd_dev = rbd_dev;
1876 img_request->offset = offset;
1877 img_request->length = length;
Alex Elder0c425242013-02-08 09:55:49 -06001878 img_request->flags = 0;
1879 if (write_request) {
1880 img_request_write_set(img_request);
Alex Elder468521c2013-04-26 09:43:47 -05001881 img_request->snapc = rbd_dev->header.snapc;
Alex Elder0c425242013-02-08 09:55:49 -06001882 } else {
Alex Elderbf0d5f502012-11-22 00:00:08 -06001883 img_request->snap_id = rbd_dev->spec->snap_id;
Alex Elder0c425242013-02-08 09:55:49 -06001884 }
Alex Elder9849e982013-01-24 16:13:36 -06001885 if (child_request)
1886 img_request_child_set(img_request);
Alex Elder642a2532013-05-06 17:40:33 -05001887 if (rbd_dev->parent_overlap)
Alex Elderd0b2e942013-01-24 16:13:36 -06001888 img_request_layered_set(img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001889 spin_lock_init(&img_request->completion_lock);
1890 img_request->next_completion = 0;
1891 img_request->callback = NULL;
Alex Eldera5a337d2013-01-24 16:13:36 -06001892 img_request->result = 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001893 img_request->obj_request_count = 0;
1894 INIT_LIST_HEAD(&img_request->obj_requests);
1895 kref_init(&img_request->kref);
1896
Alex Elder37206ee2013-02-20 17:32:08 -06001897 dout("%s: rbd_dev %p %s %llu/%llu -> img %p\n", __func__, rbd_dev,
1898 write_request ? "write" : "read", offset, length,
1899 img_request);
1900
Alex Elderbf0d5f502012-11-22 00:00:08 -06001901 return img_request;
1902}
1903
1904static void rbd_img_request_destroy(struct kref *kref)
1905{
1906 struct rbd_img_request *img_request;
1907 struct rbd_obj_request *obj_request;
1908 struct rbd_obj_request *next_obj_request;
1909
1910 img_request = container_of(kref, struct rbd_img_request, kref);
1911
Alex Elder37206ee2013-02-20 17:32:08 -06001912 dout("%s: img %p\n", __func__, img_request);
1913
Alex Elderbf0d5f502012-11-22 00:00:08 -06001914 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
1915 rbd_img_obj_request_del(img_request, obj_request);
Alex Elder25dcf952013-01-25 17:08:55 -06001916 rbd_assert(img_request->obj_request_count == 0);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001917
Alex Elder0c425242013-02-08 09:55:49 -06001918 if (img_request_write_test(img_request))
Alex Elder812164f82013-04-30 00:44:32 -05001919 ceph_put_snap_context(img_request->snapc);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001920
Alex Elder8b3e1a52013-01-24 16:13:36 -06001921 if (img_request_child_test(img_request))
1922 rbd_obj_request_put(img_request->obj_request);
1923
Alex Elder1c2a9df2013-05-01 12:43:03 -05001924 kmem_cache_free(rbd_img_request_cache, img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001925}
1926
Alex Elder12178572013-02-08 09:55:49 -06001927static bool rbd_img_obj_end_request(struct rbd_obj_request *obj_request)
1928{
Alex Elder6365d332013-02-11 12:33:24 -06001929 struct rbd_img_request *img_request;
Alex Elder12178572013-02-08 09:55:49 -06001930 unsigned int xferred;
1931 int result;
Alex Elder8b3e1a52013-01-24 16:13:36 -06001932 bool more;
Alex Elder12178572013-02-08 09:55:49 -06001933
Alex Elder6365d332013-02-11 12:33:24 -06001934 rbd_assert(obj_request_img_data_test(obj_request));
1935 img_request = obj_request->img_request;
1936
Alex Elder12178572013-02-08 09:55:49 -06001937 rbd_assert(obj_request->xferred <= (u64)UINT_MAX);
1938 xferred = (unsigned int)obj_request->xferred;
1939 result = obj_request->result;
1940 if (result) {
1941 struct rbd_device *rbd_dev = img_request->rbd_dev;
1942
1943 rbd_warn(rbd_dev, "%s %llx at %llx (%llx)\n",
1944 img_request_write_test(img_request) ? "write" : "read",
1945 obj_request->length, obj_request->img_offset,
1946 obj_request->offset);
1947 rbd_warn(rbd_dev, " result %d xferred %x\n",
1948 result, xferred);
1949 if (!img_request->result)
1950 img_request->result = result;
1951 }
1952
Alex Elderf1a47392013-04-19 15:34:50 -05001953 /* Image object requests don't own their page array */
1954
1955 if (obj_request->type == OBJ_REQUEST_PAGES) {
1956 obj_request->pages = NULL;
1957 obj_request->page_count = 0;
1958 }
1959
Alex Elder8b3e1a52013-01-24 16:13:36 -06001960 if (img_request_child_test(img_request)) {
1961 rbd_assert(img_request->obj_request != NULL);
1962 more = obj_request->which < img_request->obj_request_count - 1;
1963 } else {
1964 rbd_assert(img_request->rq != NULL);
1965 more = blk_end_request(img_request->rq, result, xferred);
1966 }
1967
1968 return more;
Alex Elder12178572013-02-08 09:55:49 -06001969}
1970
Alex Elder21692382013-04-05 01:27:12 -05001971static void rbd_img_obj_callback(struct rbd_obj_request *obj_request)
1972{
1973 struct rbd_img_request *img_request;
1974 u32 which = obj_request->which;
1975 bool more = true;
1976
Alex Elder6365d332013-02-11 12:33:24 -06001977 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elder21692382013-04-05 01:27:12 -05001978 img_request = obj_request->img_request;
1979
1980 dout("%s: img %p obj %p\n", __func__, img_request, obj_request);
1981 rbd_assert(img_request != NULL);
Alex Elder21692382013-04-05 01:27:12 -05001982 rbd_assert(img_request->obj_request_count > 0);
1983 rbd_assert(which != BAD_WHICH);
1984 rbd_assert(which < img_request->obj_request_count);
1985 rbd_assert(which >= img_request->next_completion);
1986
1987 spin_lock_irq(&img_request->completion_lock);
1988 if (which != img_request->next_completion)
1989 goto out;
1990
1991 for_each_obj_request_from(img_request, obj_request) {
Alex Elder21692382013-04-05 01:27:12 -05001992 rbd_assert(more);
1993 rbd_assert(which < img_request->obj_request_count);
1994
1995 if (!obj_request_done_test(obj_request))
1996 break;
Alex Elder12178572013-02-08 09:55:49 -06001997 more = rbd_img_obj_end_request(obj_request);
Alex Elder21692382013-04-05 01:27:12 -05001998 which++;
1999 }
2000
2001 rbd_assert(more ^ (which == img_request->obj_request_count));
2002 img_request->next_completion = which;
2003out:
2004 spin_unlock_irq(&img_request->completion_lock);
2005
2006 if (!more)
2007 rbd_img_request_complete(img_request);
2008}
2009
Alex Elderf1a47392013-04-19 15:34:50 -05002010/*
2011 * Split up an image request into one or more object requests, each
2012 * to a different object. The "type" parameter indicates whether
2013 * "data_desc" is the pointer to the head of a list of bio
2014 * structures, or the base of a page array. In either case this
2015 * function assumes data_desc describes memory sufficient to hold
2016 * all data described by the image request.
2017 */
2018static int rbd_img_request_fill(struct rbd_img_request *img_request,
2019 enum obj_request_type type,
2020 void *data_desc)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002021{
2022 struct rbd_device *rbd_dev = img_request->rbd_dev;
2023 struct rbd_obj_request *obj_request = NULL;
2024 struct rbd_obj_request *next_obj_request;
Alex Elder0c425242013-02-08 09:55:49 -06002025 bool write_request = img_request_write_test(img_request);
Alex Elderf1a47392013-04-19 15:34:50 -05002026 struct bio *bio_list;
2027 unsigned int bio_offset = 0;
2028 struct page **pages;
Alex Elder7da22d22013-01-24 16:13:36 -06002029 u64 img_offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002030 u64 resid;
2031 u16 opcode;
2032
Alex Elderf1a47392013-04-19 15:34:50 -05002033 dout("%s: img %p type %d data_desc %p\n", __func__, img_request,
2034 (int)type, data_desc);
Alex Elder37206ee2013-02-20 17:32:08 -06002035
Alex Elder430c28c2013-04-03 21:32:51 -05002036 opcode = write_request ? CEPH_OSD_OP_WRITE : CEPH_OSD_OP_READ;
Alex Elder7da22d22013-01-24 16:13:36 -06002037 img_offset = img_request->offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002038 resid = img_request->length;
Alex Elder4dda41d2013-02-20 21:59:33 -06002039 rbd_assert(resid > 0);
Alex Elderf1a47392013-04-19 15:34:50 -05002040
2041 if (type == OBJ_REQUEST_BIO) {
2042 bio_list = data_desc;
2043 rbd_assert(img_offset == bio_list->bi_sector << SECTOR_SHIFT);
2044 } else {
2045 rbd_assert(type == OBJ_REQUEST_PAGES);
2046 pages = data_desc;
2047 }
2048
Alex Elderbf0d5f502012-11-22 00:00:08 -06002049 while (resid) {
Alex Elder2fa12322013-04-05 01:27:12 -05002050 struct ceph_osd_request *osd_req;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002051 const char *object_name;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002052 u64 offset;
2053 u64 length;
2054
Alex Elder7da22d22013-01-24 16:13:36 -06002055 object_name = rbd_segment_name(rbd_dev, img_offset);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002056 if (!object_name)
2057 goto out_unwind;
Alex Elder7da22d22013-01-24 16:13:36 -06002058 offset = rbd_segment_offset(rbd_dev, img_offset);
2059 length = rbd_segment_length(rbd_dev, img_offset, resid);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002060 obj_request = rbd_obj_request_create(object_name,
Alex Elderf1a47392013-04-19 15:34:50 -05002061 offset, length, type);
Alex Elder78c2a442013-05-01 12:43:04 -05002062 /* object request has its own copy of the object name */
2063 rbd_segment_name_free(object_name);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002064 if (!obj_request)
2065 goto out_unwind;
2066
Alex Elderf1a47392013-04-19 15:34:50 -05002067 if (type == OBJ_REQUEST_BIO) {
2068 unsigned int clone_size;
2069
2070 rbd_assert(length <= (u64)UINT_MAX);
2071 clone_size = (unsigned int)length;
2072 obj_request->bio_list =
2073 bio_chain_clone_range(&bio_list,
2074 &bio_offset,
2075 clone_size,
2076 GFP_ATOMIC);
2077 if (!obj_request->bio_list)
2078 goto out_partial;
2079 } else {
2080 unsigned int page_count;
2081
2082 obj_request->pages = pages;
2083 page_count = (u32)calc_pages_for(offset, length);
2084 obj_request->page_count = page_count;
2085 if ((offset + length) & ~PAGE_MASK)
2086 page_count--; /* more on last page */
2087 pages += page_count;
2088 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06002089
Alex Elder2fa12322013-04-05 01:27:12 -05002090 osd_req = rbd_osd_req_create(rbd_dev, write_request,
2091 obj_request);
2092 if (!osd_req)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002093 goto out_partial;
Alex Elder2fa12322013-04-05 01:27:12 -05002094 obj_request->osd_req = osd_req;
Alex Elder21692382013-04-05 01:27:12 -05002095 obj_request->callback = rbd_img_obj_callback;
Alex Elder430c28c2013-04-03 21:32:51 -05002096
Alex Elder2fa12322013-04-05 01:27:12 -05002097 osd_req_op_extent_init(osd_req, 0, opcode, offset, length,
2098 0, 0);
Alex Elderf1a47392013-04-19 15:34:50 -05002099 if (type == OBJ_REQUEST_BIO)
2100 osd_req_op_extent_osd_data_bio(osd_req, 0,
2101 obj_request->bio_list, length);
2102 else
2103 osd_req_op_extent_osd_data_pages(osd_req, 0,
2104 obj_request->pages, length,
2105 offset & ~PAGE_MASK, false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05002106
2107 if (write_request)
2108 rbd_osd_req_format_write(obj_request);
2109 else
2110 rbd_osd_req_format_read(obj_request);
Alex Elder430c28c2013-04-03 21:32:51 -05002111
Alex Elder7da22d22013-01-24 16:13:36 -06002112 obj_request->img_offset = img_offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002113 rbd_img_obj_request_add(img_request, obj_request);
2114
Alex Elder7da22d22013-01-24 16:13:36 -06002115 img_offset += length;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002116 resid -= length;
2117 }
2118
2119 return 0;
2120
2121out_partial:
2122 rbd_obj_request_put(obj_request);
2123out_unwind:
2124 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
2125 rbd_obj_request_put(obj_request);
2126
2127 return -ENOMEM;
2128}
2129
Alex Elder3d7efd12013-04-19 15:34:50 -05002130static void
Alex Elder0eefd472013-04-19 15:34:50 -05002131rbd_img_obj_copyup_callback(struct rbd_obj_request *obj_request)
2132{
2133 struct rbd_img_request *img_request;
2134 struct rbd_device *rbd_dev;
Alex Elderebda6402013-05-10 16:29:22 -05002135 struct page **pages;
Alex Elder0eefd472013-04-19 15:34:50 -05002136 u32 page_count;
2137
2138 rbd_assert(obj_request->type == OBJ_REQUEST_BIO);
2139 rbd_assert(obj_request_img_data_test(obj_request));
2140 img_request = obj_request->img_request;
2141 rbd_assert(img_request);
2142
2143 rbd_dev = img_request->rbd_dev;
2144 rbd_assert(rbd_dev);
Alex Elder0eefd472013-04-19 15:34:50 -05002145
Alex Elderebda6402013-05-10 16:29:22 -05002146 pages = obj_request->copyup_pages;
2147 rbd_assert(pages != NULL);
Alex Elder0eefd472013-04-19 15:34:50 -05002148 obj_request->copyup_pages = NULL;
Alex Elderebda6402013-05-10 16:29:22 -05002149 page_count = obj_request->copyup_page_count;
2150 rbd_assert(page_count);
2151 obj_request->copyup_page_count = 0;
2152 ceph_release_page_vector(pages, page_count);
Alex Elder0eefd472013-04-19 15:34:50 -05002153
2154 /*
2155 * We want the transfer count to reflect the size of the
2156 * original write request. There is no such thing as a
2157 * successful short write, so if the request was successful
2158 * we can just set it to the originally-requested length.
2159 */
2160 if (!obj_request->result)
2161 obj_request->xferred = obj_request->length;
2162
2163 /* Finish up with the normal image object callback */
2164
2165 rbd_img_obj_callback(obj_request);
2166}
2167
2168static void
Alex Elder3d7efd12013-04-19 15:34:50 -05002169rbd_img_obj_parent_read_full_callback(struct rbd_img_request *img_request)
2170{
2171 struct rbd_obj_request *orig_request;
Alex Elder0eefd472013-04-19 15:34:50 -05002172 struct ceph_osd_request *osd_req;
2173 struct ceph_osd_client *osdc;
2174 struct rbd_device *rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002175 struct page **pages;
Alex Elderebda6402013-05-10 16:29:22 -05002176 u32 page_count;
Alex Elder3d7efd12013-04-19 15:34:50 -05002177 int result;
Alex Elderebda6402013-05-10 16:29:22 -05002178 u64 parent_length;
Alex Elderb91f09f2013-05-10 16:29:22 -05002179 u64 offset;
2180 u64 length;
Alex Elder3d7efd12013-04-19 15:34:50 -05002181
2182 rbd_assert(img_request_child_test(img_request));
2183
2184 /* First get what we need from the image request */
2185
2186 pages = img_request->copyup_pages;
2187 rbd_assert(pages != NULL);
2188 img_request->copyup_pages = NULL;
Alex Elderebda6402013-05-10 16:29:22 -05002189 page_count = img_request->copyup_page_count;
2190 rbd_assert(page_count);
2191 img_request->copyup_page_count = 0;
Alex Elder3d7efd12013-04-19 15:34:50 -05002192
2193 orig_request = img_request->obj_request;
2194 rbd_assert(orig_request != NULL);
Alex Elderb91f09f2013-05-10 16:29:22 -05002195 rbd_assert(obj_request_type_valid(orig_request->type));
Alex Elder3d7efd12013-04-19 15:34:50 -05002196 result = img_request->result;
Alex Elderebda6402013-05-10 16:29:22 -05002197 parent_length = img_request->length;
2198 rbd_assert(parent_length == img_request->xferred);
Alex Elder91c6feb2013-05-06 17:40:32 -05002199 rbd_img_request_put(img_request);
Alex Elder3d7efd12013-04-19 15:34:50 -05002200
Alex Elder91c6feb2013-05-06 17:40:32 -05002201 rbd_assert(orig_request->img_request);
2202 rbd_dev = orig_request->img_request->rbd_dev;
Alex Elder0eefd472013-04-19 15:34:50 -05002203 rbd_assert(rbd_dev);
Alex Elder0eefd472013-04-19 15:34:50 -05002204
Alex Elder0eefd472013-04-19 15:34:50 -05002205 if (result)
2206 goto out_err;
Alex Elder3d7efd12013-04-19 15:34:50 -05002207
Alex Elder8785b1d2013-05-09 10:08:49 -05002208 /*
2209 * The original osd request is of no use to use any more.
2210 * We need a new one that can hold the two ops in a copyup
2211 * request. Allocate the new copyup osd request for the
2212 * original request, and release the old one.
2213 */
Alex Elder0eefd472013-04-19 15:34:50 -05002214 result = -ENOMEM;
Alex Elder0eefd472013-04-19 15:34:50 -05002215 osd_req = rbd_osd_req_create_copyup(orig_request);
2216 if (!osd_req)
2217 goto out_err;
Alex Elder8785b1d2013-05-09 10:08:49 -05002218 rbd_osd_req_destroy(orig_request->osd_req);
Alex Elder0eefd472013-04-19 15:34:50 -05002219 orig_request->osd_req = osd_req;
2220 orig_request->copyup_pages = pages;
Alex Elderebda6402013-05-10 16:29:22 -05002221 orig_request->copyup_page_count = page_count;
Alex Elder3d7efd12013-04-19 15:34:50 -05002222
Alex Elder0eefd472013-04-19 15:34:50 -05002223 /* Initialize the copyup op */
2224
2225 osd_req_op_cls_init(osd_req, 0, CEPH_OSD_OP_CALL, "rbd", "copyup");
Alex Elderebda6402013-05-10 16:29:22 -05002226 osd_req_op_cls_request_data_pages(osd_req, 0, pages, parent_length, 0,
Alex Elder0eefd472013-04-19 15:34:50 -05002227 false, false);
2228
2229 /* Then the original write request op */
2230
Alex Elderb91f09f2013-05-10 16:29:22 -05002231 offset = orig_request->offset;
2232 length = orig_request->length;
Alex Elder0eefd472013-04-19 15:34:50 -05002233 osd_req_op_extent_init(osd_req, 1, CEPH_OSD_OP_WRITE,
Alex Elderb91f09f2013-05-10 16:29:22 -05002234 offset, length, 0, 0);
2235 if (orig_request->type == OBJ_REQUEST_BIO)
2236 osd_req_op_extent_osd_data_bio(osd_req, 1,
2237 orig_request->bio_list, length);
2238 else
2239 osd_req_op_extent_osd_data_pages(osd_req, 1,
2240 orig_request->pages, length,
2241 offset & ~PAGE_MASK, false, false);
Alex Elder0eefd472013-04-19 15:34:50 -05002242
2243 rbd_osd_req_format_write(orig_request);
2244
2245 /* All set, send it off. */
2246
2247 orig_request->callback = rbd_img_obj_copyup_callback;
2248 osdc = &rbd_dev->rbd_client->client->osdc;
2249 result = rbd_obj_request_submit(osdc, orig_request);
2250 if (!result)
2251 return;
2252out_err:
2253 /* Record the error code and complete the request */
2254
2255 orig_request->result = result;
2256 orig_request->xferred = 0;
2257 obj_request_done_set(orig_request);
2258 rbd_obj_request_complete(orig_request);
Alex Elder3d7efd12013-04-19 15:34:50 -05002259}
2260
2261/*
2262 * Read from the parent image the range of data that covers the
2263 * entire target of the given object request. This is used for
2264 * satisfying a layered image write request when the target of an
2265 * object request from the image request does not exist.
2266 *
2267 * A page array big enough to hold the returned data is allocated
2268 * and supplied to rbd_img_request_fill() as the "data descriptor."
2269 * When the read completes, this page array will be transferred to
2270 * the original object request for the copyup operation.
2271 *
2272 * If an error occurs, record it as the result of the original
2273 * object request and mark it done so it gets completed.
2274 */
2275static int rbd_img_obj_parent_read_full(struct rbd_obj_request *obj_request)
2276{
2277 struct rbd_img_request *img_request = NULL;
2278 struct rbd_img_request *parent_request = NULL;
2279 struct rbd_device *rbd_dev;
2280 u64 img_offset;
2281 u64 length;
2282 struct page **pages = NULL;
2283 u32 page_count;
2284 int result;
2285
2286 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elderb91f09f2013-05-10 16:29:22 -05002287 rbd_assert(obj_request_type_valid(obj_request->type));
Alex Elder3d7efd12013-04-19 15:34:50 -05002288
2289 img_request = obj_request->img_request;
2290 rbd_assert(img_request != NULL);
2291 rbd_dev = img_request->rbd_dev;
2292 rbd_assert(rbd_dev->parent != NULL);
2293
2294 /*
2295 * Determine the byte range covered by the object in the
2296 * child image to which the original request was to be sent.
2297 */
2298 img_offset = obj_request->img_offset - obj_request->offset;
2299 length = (u64)1 << rbd_dev->header.obj_order;
2300
2301 /*
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002302 * There is no defined parent data beyond the parent
2303 * overlap, so limit what we read at that boundary if
2304 * necessary.
2305 */
2306 if (img_offset + length > rbd_dev->parent_overlap) {
2307 rbd_assert(img_offset < rbd_dev->parent_overlap);
2308 length = rbd_dev->parent_overlap - img_offset;
2309 }
2310
2311 /*
Alex Elder3d7efd12013-04-19 15:34:50 -05002312 * Allocate a page array big enough to receive the data read
2313 * from the parent.
2314 */
2315 page_count = (u32)calc_pages_for(0, length);
2316 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2317 if (IS_ERR(pages)) {
2318 result = PTR_ERR(pages);
2319 pages = NULL;
2320 goto out_err;
2321 }
2322
2323 result = -ENOMEM;
2324 parent_request = rbd_img_request_create(rbd_dev->parent,
2325 img_offset, length,
2326 false, true);
2327 if (!parent_request)
2328 goto out_err;
2329 rbd_obj_request_get(obj_request);
2330 parent_request->obj_request = obj_request;
2331
2332 result = rbd_img_request_fill(parent_request, OBJ_REQUEST_PAGES, pages);
2333 if (result)
2334 goto out_err;
2335 parent_request->copyup_pages = pages;
Alex Elderebda6402013-05-10 16:29:22 -05002336 parent_request->copyup_page_count = page_count;
Alex Elder3d7efd12013-04-19 15:34:50 -05002337
2338 parent_request->callback = rbd_img_obj_parent_read_full_callback;
2339 result = rbd_img_request_submit(parent_request);
2340 if (!result)
2341 return 0;
2342
2343 parent_request->copyup_pages = NULL;
Alex Elderebda6402013-05-10 16:29:22 -05002344 parent_request->copyup_page_count = 0;
Alex Elder3d7efd12013-04-19 15:34:50 -05002345 parent_request->obj_request = NULL;
2346 rbd_obj_request_put(obj_request);
2347out_err:
2348 if (pages)
2349 ceph_release_page_vector(pages, page_count);
2350 if (parent_request)
2351 rbd_img_request_put(parent_request);
2352 obj_request->result = result;
2353 obj_request->xferred = 0;
2354 obj_request_done_set(obj_request);
2355
2356 return result;
2357}
2358
Alex Elderc5b5ef62013-02-11 12:33:24 -06002359static void rbd_img_obj_exists_callback(struct rbd_obj_request *obj_request)
2360{
Alex Elderc5b5ef62013-02-11 12:33:24 -06002361 struct rbd_obj_request *orig_request;
2362 int result;
2363
2364 rbd_assert(!obj_request_img_data_test(obj_request));
2365
2366 /*
2367 * All we need from the object request is the original
2368 * request and the result of the STAT op. Grab those, then
2369 * we're done with the request.
2370 */
2371 orig_request = obj_request->obj_request;
2372 obj_request->obj_request = NULL;
2373 rbd_assert(orig_request);
2374 rbd_assert(orig_request->img_request);
2375
2376 result = obj_request->result;
2377 obj_request->result = 0;
2378
2379 dout("%s: obj %p for obj %p result %d %llu/%llu\n", __func__,
2380 obj_request, orig_request, result,
2381 obj_request->xferred, obj_request->length);
2382 rbd_obj_request_put(obj_request);
2383
2384 rbd_assert(orig_request);
2385 rbd_assert(orig_request->img_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002386
2387 /*
2388 * Our only purpose here is to determine whether the object
2389 * exists, and we don't want to treat the non-existence as
2390 * an error. If something else comes back, transfer the
2391 * error to the original request and complete it now.
2392 */
2393 if (!result) {
2394 obj_request_existence_set(orig_request, true);
2395 } else if (result == -ENOENT) {
2396 obj_request_existence_set(orig_request, false);
2397 } else if (result) {
2398 orig_request->result = result;
Alex Elder3d7efd12013-04-19 15:34:50 -05002399 goto out;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002400 }
2401
2402 /*
2403 * Resubmit the original request now that we have recorded
2404 * whether the target object exists.
2405 */
Alex Elderb454e362013-04-19 15:34:50 -05002406 orig_request->result = rbd_img_obj_request_submit(orig_request);
Alex Elder3d7efd12013-04-19 15:34:50 -05002407out:
Alex Elderc5b5ef62013-02-11 12:33:24 -06002408 if (orig_request->result)
2409 rbd_obj_request_complete(orig_request);
2410 rbd_obj_request_put(orig_request);
2411}
2412
2413static int rbd_img_obj_exists_submit(struct rbd_obj_request *obj_request)
2414{
2415 struct rbd_obj_request *stat_request;
2416 struct rbd_device *rbd_dev;
2417 struct ceph_osd_client *osdc;
2418 struct page **pages = NULL;
2419 u32 page_count;
2420 size_t size;
2421 int ret;
2422
2423 /*
2424 * The response data for a STAT call consists of:
2425 * le64 length;
2426 * struct {
2427 * le32 tv_sec;
2428 * le32 tv_nsec;
2429 * } mtime;
2430 */
2431 size = sizeof (__le64) + sizeof (__le32) + sizeof (__le32);
2432 page_count = (u32)calc_pages_for(0, size);
2433 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2434 if (IS_ERR(pages))
2435 return PTR_ERR(pages);
2436
2437 ret = -ENOMEM;
2438 stat_request = rbd_obj_request_create(obj_request->object_name, 0, 0,
2439 OBJ_REQUEST_PAGES);
2440 if (!stat_request)
2441 goto out;
2442
2443 rbd_obj_request_get(obj_request);
2444 stat_request->obj_request = obj_request;
2445 stat_request->pages = pages;
2446 stat_request->page_count = page_count;
2447
2448 rbd_assert(obj_request->img_request);
2449 rbd_dev = obj_request->img_request->rbd_dev;
2450 stat_request->osd_req = rbd_osd_req_create(rbd_dev, false,
2451 stat_request);
2452 if (!stat_request->osd_req)
2453 goto out;
2454 stat_request->callback = rbd_img_obj_exists_callback;
2455
2456 osd_req_op_init(stat_request->osd_req, 0, CEPH_OSD_OP_STAT);
2457 osd_req_op_raw_data_in_pages(stat_request->osd_req, 0, pages, size, 0,
2458 false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05002459 rbd_osd_req_format_read(stat_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002460
2461 osdc = &rbd_dev->rbd_client->client->osdc;
2462 ret = rbd_obj_request_submit(osdc, stat_request);
2463out:
2464 if (ret)
2465 rbd_obj_request_put(obj_request);
2466
2467 return ret;
2468}
2469
Alex Elderb454e362013-04-19 15:34:50 -05002470static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request)
2471{
2472 struct rbd_img_request *img_request;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002473 struct rbd_device *rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002474 bool known;
Alex Elderb454e362013-04-19 15:34:50 -05002475
2476 rbd_assert(obj_request_img_data_test(obj_request));
2477
2478 img_request = obj_request->img_request;
2479 rbd_assert(img_request);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002480 rbd_dev = img_request->rbd_dev;
Alex Elderb454e362013-04-19 15:34:50 -05002481
Alex Elderb454e362013-04-19 15:34:50 -05002482 /*
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002483 * Only writes to layered images need special handling.
2484 * Reads and non-layered writes are simple object requests.
2485 * Layered writes that start beyond the end of the overlap
2486 * with the parent have no parent data, so they too are
2487 * simple object requests. Finally, if the target object is
2488 * known to already exist, its parent data has already been
2489 * copied, so a write to the object can also be handled as a
2490 * simple object request.
Alex Elderb454e362013-04-19 15:34:50 -05002491 */
2492 if (!img_request_write_test(img_request) ||
2493 !img_request_layered_test(img_request) ||
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002494 rbd_dev->parent_overlap <= obj_request->img_offset ||
Alex Elder3d7efd12013-04-19 15:34:50 -05002495 ((known = obj_request_known_test(obj_request)) &&
2496 obj_request_exists_test(obj_request))) {
Alex Elderb454e362013-04-19 15:34:50 -05002497
2498 struct rbd_device *rbd_dev;
2499 struct ceph_osd_client *osdc;
2500
2501 rbd_dev = obj_request->img_request->rbd_dev;
2502 osdc = &rbd_dev->rbd_client->client->osdc;
2503
2504 return rbd_obj_request_submit(osdc, obj_request);
2505 }
2506
2507 /*
Alex Elder3d7efd12013-04-19 15:34:50 -05002508 * It's a layered write. The target object might exist but
2509 * we may not know that yet. If we know it doesn't exist,
2510 * start by reading the data for the full target object from
2511 * the parent so we can use it for a copyup to the target.
Alex Elderb454e362013-04-19 15:34:50 -05002512 */
Alex Elder3d7efd12013-04-19 15:34:50 -05002513 if (known)
2514 return rbd_img_obj_parent_read_full(obj_request);
2515
2516 /* We don't know whether the target exists. Go find out. */
Alex Elderb454e362013-04-19 15:34:50 -05002517
2518 return rbd_img_obj_exists_submit(obj_request);
2519}
2520
Alex Elderbf0d5f502012-11-22 00:00:08 -06002521static int rbd_img_request_submit(struct rbd_img_request *img_request)
2522{
Alex Elderbf0d5f502012-11-22 00:00:08 -06002523 struct rbd_obj_request *obj_request;
Alex Elder46faeed2013-04-10 17:47:46 -05002524 struct rbd_obj_request *next_obj_request;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002525
Alex Elder37206ee2013-02-20 17:32:08 -06002526 dout("%s: img %p\n", __func__, img_request);
Alex Elder46faeed2013-04-10 17:47:46 -05002527 for_each_obj_request_safe(img_request, obj_request, next_obj_request) {
Alex Elderbf0d5f502012-11-22 00:00:08 -06002528 int ret;
2529
Alex Elderb454e362013-04-19 15:34:50 -05002530 ret = rbd_img_obj_request_submit(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002531 if (ret)
2532 return ret;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002533 }
2534
2535 return 0;
2536}
2537
Alex Elder8b3e1a52013-01-24 16:13:36 -06002538static void rbd_img_parent_read_callback(struct rbd_img_request *img_request)
2539{
2540 struct rbd_obj_request *obj_request;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002541 struct rbd_device *rbd_dev;
2542 u64 obj_end;
Alex Elder8b3e1a52013-01-24 16:13:36 -06002543
2544 rbd_assert(img_request_child_test(img_request));
2545
2546 obj_request = img_request->obj_request;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002547 rbd_assert(obj_request);
2548 rbd_assert(obj_request->img_request);
Alex Elder8b3e1a52013-01-24 16:13:36 -06002549
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002550 obj_request->result = img_request->result;
2551 if (obj_request->result)
2552 goto out;
2553
2554 /*
2555 * We need to zero anything beyond the parent overlap
2556 * boundary. Since rbd_img_obj_request_read_callback()
2557 * will zero anything beyond the end of a short read, an
2558 * easy way to do this is to pretend the data from the
2559 * parent came up short--ending at the overlap boundary.
2560 */
2561 rbd_assert(obj_request->img_offset < U64_MAX - obj_request->length);
2562 obj_end = obj_request->img_offset + obj_request->length;
2563 rbd_dev = obj_request->img_request->rbd_dev;
2564 if (obj_end > rbd_dev->parent_overlap) {
2565 u64 xferred = 0;
2566
2567 if (obj_request->img_offset < rbd_dev->parent_overlap)
2568 xferred = rbd_dev->parent_overlap -
2569 obj_request->img_offset;
2570
2571 obj_request->xferred = min(img_request->xferred, xferred);
2572 } else {
2573 obj_request->xferred = img_request->xferred;
2574 }
2575out:
Alex Elderb5b09be2013-05-01 21:37:07 -05002576 rbd_img_request_put(img_request);
Alex Elder8b3e1a52013-01-24 16:13:36 -06002577 rbd_img_obj_request_read_callback(obj_request);
2578 rbd_obj_request_complete(obj_request);
2579}
2580
2581static void rbd_img_parent_read(struct rbd_obj_request *obj_request)
2582{
2583 struct rbd_device *rbd_dev;
2584 struct rbd_img_request *img_request;
2585 int result;
2586
2587 rbd_assert(obj_request_img_data_test(obj_request));
2588 rbd_assert(obj_request->img_request != NULL);
2589 rbd_assert(obj_request->result == (s32) -ENOENT);
Alex Elder5b2ab722013-05-06 17:40:33 -05002590 rbd_assert(obj_request_type_valid(obj_request->type));
Alex Elder8b3e1a52013-01-24 16:13:36 -06002591
2592 rbd_dev = obj_request->img_request->rbd_dev;
2593 rbd_assert(rbd_dev->parent != NULL);
2594 /* rbd_read_finish(obj_request, obj_request->length); */
2595 img_request = rbd_img_request_create(rbd_dev->parent,
2596 obj_request->img_offset,
2597 obj_request->length,
2598 false, true);
2599 result = -ENOMEM;
2600 if (!img_request)
2601 goto out_err;
2602
2603 rbd_obj_request_get(obj_request);
2604 img_request->obj_request = obj_request;
2605
Alex Elder5b2ab722013-05-06 17:40:33 -05002606 if (obj_request->type == OBJ_REQUEST_BIO)
2607 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
2608 obj_request->bio_list);
2609 else
2610 result = rbd_img_request_fill(img_request, OBJ_REQUEST_PAGES,
2611 obj_request->pages);
Alex Elder8b3e1a52013-01-24 16:13:36 -06002612 if (result)
2613 goto out_err;
2614
2615 img_request->callback = rbd_img_parent_read_callback;
2616 result = rbd_img_request_submit(img_request);
2617 if (result)
2618 goto out_err;
2619
2620 return;
2621out_err:
2622 if (img_request)
2623 rbd_img_request_put(img_request);
2624 obj_request->result = result;
2625 obj_request->xferred = 0;
2626 obj_request_done_set(obj_request);
2627}
2628
Alex Eldercc4a38bd2013-04-30 00:44:33 -05002629static int rbd_obj_notify_ack(struct rbd_device *rbd_dev, u64 notify_id)
Alex Elderb8d70032012-11-30 17:53:04 -06002630{
2631 struct rbd_obj_request *obj_request;
Alex Elder21692382013-04-05 01:27:12 -05002632 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elderb8d70032012-11-30 17:53:04 -06002633 int ret;
2634
2635 obj_request = rbd_obj_request_create(rbd_dev->header_name, 0, 0,
2636 OBJ_REQUEST_NODATA);
2637 if (!obj_request)
2638 return -ENOMEM;
2639
2640 ret = -ENOMEM;
Alex Elder430c28c2013-04-03 21:32:51 -05002641 obj_request->osd_req = rbd_osd_req_create(rbd_dev, false, obj_request);
Alex Elderb8d70032012-11-30 17:53:04 -06002642 if (!obj_request->osd_req)
2643 goto out;
Alex Elder21692382013-04-05 01:27:12 -05002644 obj_request->callback = rbd_obj_request_put;
Alex Elderb8d70032012-11-30 17:53:04 -06002645
Alex Elderc99d2d42013-04-05 01:27:11 -05002646 osd_req_op_watch_init(obj_request->osd_req, 0, CEPH_OSD_OP_NOTIFY_ACK,
Alex Eldercc4a38bd2013-04-30 00:44:33 -05002647 notify_id, 0, 0);
Alex Elder9d4df012013-04-19 15:34:50 -05002648 rbd_osd_req_format_read(obj_request);
Alex Elder430c28c2013-04-03 21:32:51 -05002649
Alex Elderb8d70032012-11-30 17:53:04 -06002650 ret = rbd_obj_request_submit(osdc, obj_request);
Alex Elderb8d70032012-11-30 17:53:04 -06002651out:
Alex Eldercf81b602013-01-17 12:18:46 -06002652 if (ret)
2653 rbd_obj_request_put(obj_request);
Alex Elderb8d70032012-11-30 17:53:04 -06002654
2655 return ret;
2656}
2657
2658static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
2659{
2660 struct rbd_device *rbd_dev = (struct rbd_device *)data;
Alex Eldere627db02013-05-06 07:40:30 -05002661 int ret;
Alex Elderb8d70032012-11-30 17:53:04 -06002662
2663 if (!rbd_dev)
2664 return;
2665
Alex Elder37206ee2013-02-20 17:32:08 -06002666 dout("%s: \"%s\" notify_id %llu opcode %u\n", __func__,
Alex Eldercc4a38bd2013-04-30 00:44:33 -05002667 rbd_dev->header_name, (unsigned long long)notify_id,
2668 (unsigned int)opcode);
Alex Eldere627db02013-05-06 07:40:30 -05002669 ret = rbd_dev_refresh(rbd_dev);
2670 if (ret)
2671 rbd_warn(rbd_dev, ": header refresh error (%d)\n", ret);
Alex Elderb8d70032012-11-30 17:53:04 -06002672
Alex Eldercc4a38bd2013-04-30 00:44:33 -05002673 rbd_obj_notify_ack(rbd_dev, notify_id);
Alex Elderb8d70032012-11-30 17:53:04 -06002674}
2675
Alex Elder9969ebc2013-01-18 12:31:10 -06002676/*
2677 * Request sync osd watch/unwatch. The value of "start" determines
2678 * whether a watch request is being initiated or torn down.
2679 */
Alex Elder1f3ef782013-05-06 17:40:33 -05002680static int rbd_dev_header_watch_sync(struct rbd_device *rbd_dev, bool start)
Alex Elder9969ebc2013-01-18 12:31:10 -06002681{
2682 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
2683 struct rbd_obj_request *obj_request;
Alex Elder9969ebc2013-01-18 12:31:10 -06002684 int ret;
2685
2686 rbd_assert(start ^ !!rbd_dev->watch_event);
2687 rbd_assert(start ^ !!rbd_dev->watch_request);
2688
2689 if (start) {
Alex Elder3c663bb2013-02-15 11:42:30 -06002690 ret = ceph_osdc_create_event(osdc, rbd_watch_cb, rbd_dev,
Alex Elder9969ebc2013-01-18 12:31:10 -06002691 &rbd_dev->watch_event);
2692 if (ret < 0)
2693 return ret;
Alex Elder8eb87562013-01-25 17:08:55 -06002694 rbd_assert(rbd_dev->watch_event != NULL);
Alex Elder9969ebc2013-01-18 12:31:10 -06002695 }
2696
2697 ret = -ENOMEM;
2698 obj_request = rbd_obj_request_create(rbd_dev->header_name, 0, 0,
2699 OBJ_REQUEST_NODATA);
2700 if (!obj_request)
2701 goto out_cancel;
2702
Alex Elder430c28c2013-04-03 21:32:51 -05002703 obj_request->osd_req = rbd_osd_req_create(rbd_dev, true, obj_request);
2704 if (!obj_request->osd_req)
2705 goto out_cancel;
2706
Alex Elder8eb87562013-01-25 17:08:55 -06002707 if (start)
Alex Elder975241a2013-01-25 17:08:55 -06002708 ceph_osdc_set_request_linger(osdc, obj_request->osd_req);
Alex Elder8eb87562013-01-25 17:08:55 -06002709 else
Alex Elder6977c3f2013-01-25 17:08:55 -06002710 ceph_osdc_unregister_linger_request(osdc,
Alex Elder975241a2013-01-25 17:08:55 -06002711 rbd_dev->watch_request->osd_req);
Alex Elder21692382013-04-05 01:27:12 -05002712
2713 osd_req_op_watch_init(obj_request->osd_req, 0, CEPH_OSD_OP_WATCH,
Alex Elder1f3ef782013-05-06 17:40:33 -05002714 rbd_dev->watch_event->cookie, 0, start ? 1 : 0);
Alex Elder9d4df012013-04-19 15:34:50 -05002715 rbd_osd_req_format_write(obj_request);
Alex Elder21692382013-04-05 01:27:12 -05002716
Alex Elder9969ebc2013-01-18 12:31:10 -06002717 ret = rbd_obj_request_submit(osdc, obj_request);
2718 if (ret)
2719 goto out_cancel;
2720 ret = rbd_obj_request_wait(obj_request);
2721 if (ret)
2722 goto out_cancel;
Alex Elder9969ebc2013-01-18 12:31:10 -06002723 ret = obj_request->result;
2724 if (ret)
2725 goto out_cancel;
2726
Alex Elder8eb87562013-01-25 17:08:55 -06002727 /*
2728 * A watch request is set to linger, so the underlying osd
2729 * request won't go away until we unregister it. We retain
2730 * a pointer to the object request during that time (in
2731 * rbd_dev->watch_request), so we'll keep a reference to
2732 * it. We'll drop that reference (below) after we've
2733 * unregistered it.
2734 */
2735 if (start) {
2736 rbd_dev->watch_request = obj_request;
2737
2738 return 0;
2739 }
2740
2741 /* We have successfully torn down the watch request */
2742
2743 rbd_obj_request_put(rbd_dev->watch_request);
2744 rbd_dev->watch_request = NULL;
Alex Elder9969ebc2013-01-18 12:31:10 -06002745out_cancel:
2746 /* Cancel the event if we're tearing down, or on error */
2747 ceph_osdc_cancel_event(rbd_dev->watch_event);
2748 rbd_dev->watch_event = NULL;
Alex Elder9969ebc2013-01-18 12:31:10 -06002749 if (obj_request)
2750 rbd_obj_request_put(obj_request);
2751
2752 return ret;
2753}
2754
Alex Elder36be9a72013-01-19 00:30:28 -06002755/*
Alex Elderf40eb342013-04-25 15:09:42 -05002756 * Synchronous osd object method call. Returns the number of bytes
2757 * returned in the outbound buffer, or a negative error code.
Alex Elder36be9a72013-01-19 00:30:28 -06002758 */
2759static int rbd_obj_method_sync(struct rbd_device *rbd_dev,
2760 const char *object_name,
2761 const char *class_name,
2762 const char *method_name,
Alex Elder41579762013-04-21 12:14:45 -05002763 const void *outbound,
Alex Elder36be9a72013-01-19 00:30:28 -06002764 size_t outbound_size,
Alex Elder41579762013-04-21 12:14:45 -05002765 void *inbound,
Alex Eldere2a58ee2013-04-30 00:44:33 -05002766 size_t inbound_size)
Alex Elder36be9a72013-01-19 00:30:28 -06002767{
Alex Elder21692382013-04-05 01:27:12 -05002768 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder36be9a72013-01-19 00:30:28 -06002769 struct rbd_obj_request *obj_request;
Alex Elder36be9a72013-01-19 00:30:28 -06002770 struct page **pages;
2771 u32 page_count;
2772 int ret;
2773
2774 /*
Alex Elder6010a452013-04-05 01:27:11 -05002775 * Method calls are ultimately read operations. The result
2776 * should placed into the inbound buffer provided. They
2777 * also supply outbound data--parameters for the object
2778 * method. Currently if this is present it will be a
2779 * snapshot id.
Alex Elder36be9a72013-01-19 00:30:28 -06002780 */
Alex Elder57385b52013-04-21 12:14:45 -05002781 page_count = (u32)calc_pages_for(0, inbound_size);
Alex Elder36be9a72013-01-19 00:30:28 -06002782 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2783 if (IS_ERR(pages))
2784 return PTR_ERR(pages);
2785
2786 ret = -ENOMEM;
Alex Elder6010a452013-04-05 01:27:11 -05002787 obj_request = rbd_obj_request_create(object_name, 0, inbound_size,
Alex Elder36be9a72013-01-19 00:30:28 -06002788 OBJ_REQUEST_PAGES);
2789 if (!obj_request)
2790 goto out;
2791
2792 obj_request->pages = pages;
2793 obj_request->page_count = page_count;
2794
Alex Elder430c28c2013-04-03 21:32:51 -05002795 obj_request->osd_req = rbd_osd_req_create(rbd_dev, false, obj_request);
Alex Elder36be9a72013-01-19 00:30:28 -06002796 if (!obj_request->osd_req)
2797 goto out;
2798
Alex Elderc99d2d42013-04-05 01:27:11 -05002799 osd_req_op_cls_init(obj_request->osd_req, 0, CEPH_OSD_OP_CALL,
Alex Elder04017e22013-04-05 14:46:02 -05002800 class_name, method_name);
2801 if (outbound_size) {
2802 struct ceph_pagelist *pagelist;
2803
2804 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
2805 if (!pagelist)
2806 goto out;
2807
2808 ceph_pagelist_init(pagelist);
2809 ceph_pagelist_append(pagelist, outbound, outbound_size);
2810 osd_req_op_cls_request_data_pagelist(obj_request->osd_req, 0,
2811 pagelist);
2812 }
Alex Eldera4ce40a2013-04-05 01:27:12 -05002813 osd_req_op_cls_response_data_pages(obj_request->osd_req, 0,
2814 obj_request->pages, inbound_size,
Alex Elder44cd1882013-04-05 01:27:12 -05002815 0, false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05002816 rbd_osd_req_format_read(obj_request);
Alex Elder430c28c2013-04-03 21:32:51 -05002817
Alex Elder36be9a72013-01-19 00:30:28 -06002818 ret = rbd_obj_request_submit(osdc, obj_request);
2819 if (ret)
2820 goto out;
2821 ret = rbd_obj_request_wait(obj_request);
2822 if (ret)
2823 goto out;
2824
2825 ret = obj_request->result;
2826 if (ret < 0)
2827 goto out;
Alex Elder57385b52013-04-21 12:14:45 -05002828
2829 rbd_assert(obj_request->xferred < (u64)INT_MAX);
2830 ret = (int)obj_request->xferred;
Alex Elder903bb322013-02-06 13:11:38 -06002831 ceph_copy_from_page_vector(pages, inbound, 0, obj_request->xferred);
Alex Elder36be9a72013-01-19 00:30:28 -06002832out:
2833 if (obj_request)
2834 rbd_obj_request_put(obj_request);
2835 else
2836 ceph_release_page_vector(pages, page_count);
2837
2838 return ret;
2839}
2840
Alex Elderbf0d5f502012-11-22 00:00:08 -06002841static void rbd_request_fn(struct request_queue *q)
Alex Eldercc344fa2013-02-19 12:25:56 -06002842 __releases(q->queue_lock) __acquires(q->queue_lock)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002843{
2844 struct rbd_device *rbd_dev = q->queuedata;
2845 bool read_only = rbd_dev->mapping.read_only;
2846 struct request *rq;
2847 int result;
2848
2849 while ((rq = blk_fetch_request(q))) {
2850 bool write_request = rq_data_dir(rq) == WRITE;
2851 struct rbd_img_request *img_request;
2852 u64 offset;
2853 u64 length;
2854
2855 /* Ignore any non-FS requests that filter through. */
2856
2857 if (rq->cmd_type != REQ_TYPE_FS) {
Alex Elder4dda41d2013-02-20 21:59:33 -06002858 dout("%s: non-fs request type %d\n", __func__,
2859 (int) rq->cmd_type);
2860 __blk_end_request_all(rq, 0);
2861 continue;
2862 }
2863
2864 /* Ignore/skip any zero-length requests */
2865
2866 offset = (u64) blk_rq_pos(rq) << SECTOR_SHIFT;
2867 length = (u64) blk_rq_bytes(rq);
2868
2869 if (!length) {
2870 dout("%s: zero-length request\n", __func__);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002871 __blk_end_request_all(rq, 0);
2872 continue;
2873 }
2874
2875 spin_unlock_irq(q->queue_lock);
2876
2877 /* Disallow writes to a read-only device */
2878
2879 if (write_request) {
2880 result = -EROFS;
2881 if (read_only)
2882 goto end_request;
2883 rbd_assert(rbd_dev->spec->snap_id == CEPH_NOSNAP);
2884 }
2885
Alex Elder6d292902013-01-14 12:43:31 -06002886 /*
2887 * Quit early if the mapped snapshot no longer
2888 * exists. It's still possible the snapshot will
2889 * have disappeared by the time our request arrives
2890 * at the osd, but there's no sense in sending it if
2891 * we already know.
2892 */
2893 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) {
Alex Elderbf0d5f502012-11-22 00:00:08 -06002894 dout("request for non-existent snapshot");
2895 rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP);
2896 result = -ENXIO;
2897 goto end_request;
2898 }
2899
Alex Elderbf0d5f502012-11-22 00:00:08 -06002900 result = -EINVAL;
Alex Elderc0cd10db2013-04-26 09:43:47 -05002901 if (offset && length > U64_MAX - offset + 1) {
2902 rbd_warn(rbd_dev, "bad request range (%llu~%llu)\n",
2903 offset, length);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002904 goto end_request; /* Shouldn't happen */
Alex Elderc0cd10db2013-04-26 09:43:47 -05002905 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06002906
Alex Elder00a653e2013-05-06 07:40:30 -05002907 result = -EIO;
2908 if (offset + length > rbd_dev->mapping.size) {
2909 rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)\n",
2910 offset, length, rbd_dev->mapping.size);
2911 goto end_request;
2912 }
2913
Alex Elderbf0d5f502012-11-22 00:00:08 -06002914 result = -ENOMEM;
2915 img_request = rbd_img_request_create(rbd_dev, offset, length,
Alex Elder9849e982013-01-24 16:13:36 -06002916 write_request, false);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002917 if (!img_request)
2918 goto end_request;
2919
2920 img_request->rq = rq;
2921
Alex Elderf1a47392013-04-19 15:34:50 -05002922 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
2923 rq->bio);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002924 if (!result)
2925 result = rbd_img_request_submit(img_request);
2926 if (result)
2927 rbd_img_request_put(img_request);
2928end_request:
2929 spin_lock_irq(q->queue_lock);
2930 if (result < 0) {
Alex Elder7da22d22013-01-24 16:13:36 -06002931 rbd_warn(rbd_dev, "%s %llx at %llx result %d\n",
2932 write_request ? "write" : "read",
2933 length, offset, result);
2934
Alex Elderbf0d5f502012-11-22 00:00:08 -06002935 __blk_end_request_all(rq, result);
2936 }
2937 }
2938}
2939
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002940/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002941 * a queue callback. Makes sure that we don't create a bio that spans across
2942 * multiple osd objects. One exception would be with a single page bios,
Alex Elderf7760da2012-10-20 22:17:27 -05002943 * which we handle later at bio_chain_clone_range()
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002944 */
2945static int rbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd,
2946 struct bio_vec *bvec)
2947{
2948 struct rbd_device *rbd_dev = q->queuedata;
Alex Eldere5cfeed22012-10-20 22:17:27 -05002949 sector_t sector_offset;
2950 sector_t sectors_per_obj;
2951 sector_t obj_sector_offset;
2952 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002953
Alex Eldere5cfeed22012-10-20 22:17:27 -05002954 /*
2955 * Find how far into its rbd object the partition-relative
2956 * bio start sector is to offset relative to the enclosing
2957 * device.
2958 */
2959 sector_offset = get_start_sect(bmd->bi_bdev) + bmd->bi_sector;
2960 sectors_per_obj = 1 << (rbd_dev->header.obj_order - SECTOR_SHIFT);
2961 obj_sector_offset = sector_offset & (sectors_per_obj - 1);
Alex Elder593a9e72012-02-07 12:03:37 -06002962
Alex Eldere5cfeed22012-10-20 22:17:27 -05002963 /*
2964 * Compute the number of bytes from that offset to the end
2965 * of the object. Account for what's already used by the bio.
2966 */
2967 ret = (int) (sectors_per_obj - obj_sector_offset) << SECTOR_SHIFT;
2968 if (ret > bmd->bi_size)
2969 ret -= bmd->bi_size;
2970 else
2971 ret = 0;
2972
2973 /*
2974 * Don't send back more than was asked for. And if the bio
2975 * was empty, let the whole thing through because: "Note
2976 * that a block device *must* allow a single page to be
2977 * added to an empty bio."
2978 */
2979 rbd_assert(bvec->bv_len <= PAGE_SIZE);
2980 if (ret > (int) bvec->bv_len || !bmd->bi_size)
2981 ret = (int) bvec->bv_len;
2982
2983 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002984}
2985
2986static void rbd_free_disk(struct rbd_device *rbd_dev)
2987{
2988 struct gendisk *disk = rbd_dev->disk;
2989
2990 if (!disk)
2991 return;
2992
Alex Eldera0cab922013-04-25 23:15:08 -05002993 rbd_dev->disk = NULL;
2994 if (disk->flags & GENHD_FL_UP) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002995 del_gendisk(disk);
Alex Eldera0cab922013-04-25 23:15:08 -05002996 if (disk->queue)
2997 blk_cleanup_queue(disk->queue);
2998 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002999 put_disk(disk);
3000}
3001
Alex Elder788e2df2013-01-17 12:25:27 -06003002static int rbd_obj_read_sync(struct rbd_device *rbd_dev,
3003 const char *object_name,
Alex Elder7097f8d2013-04-30 00:44:33 -05003004 u64 offset, u64 length, void *buf)
Alex Elder788e2df2013-01-17 12:25:27 -06003005
3006{
Alex Elder21692382013-04-05 01:27:12 -05003007 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder788e2df2013-01-17 12:25:27 -06003008 struct rbd_obj_request *obj_request;
Alex Elder788e2df2013-01-17 12:25:27 -06003009 struct page **pages = NULL;
3010 u32 page_count;
Alex Elder1ceae7e2013-02-06 13:11:38 -06003011 size_t size;
Alex Elder788e2df2013-01-17 12:25:27 -06003012 int ret;
3013
3014 page_count = (u32) calc_pages_for(offset, length);
3015 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
3016 if (IS_ERR(pages))
3017 ret = PTR_ERR(pages);
3018
3019 ret = -ENOMEM;
3020 obj_request = rbd_obj_request_create(object_name, offset, length,
Alex Elder36be9a72013-01-19 00:30:28 -06003021 OBJ_REQUEST_PAGES);
Alex Elder788e2df2013-01-17 12:25:27 -06003022 if (!obj_request)
3023 goto out;
3024
3025 obj_request->pages = pages;
3026 obj_request->page_count = page_count;
3027
Alex Elder430c28c2013-04-03 21:32:51 -05003028 obj_request->osd_req = rbd_osd_req_create(rbd_dev, false, obj_request);
Alex Elder788e2df2013-01-17 12:25:27 -06003029 if (!obj_request->osd_req)
3030 goto out;
3031
Alex Elderc99d2d42013-04-05 01:27:11 -05003032 osd_req_op_extent_init(obj_request->osd_req, 0, CEPH_OSD_OP_READ,
3033 offset, length, 0, 0);
Alex Elder406e2c92013-04-15 14:50:36 -05003034 osd_req_op_extent_osd_data_pages(obj_request->osd_req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05003035 obj_request->pages,
Alex Elder44cd1882013-04-05 01:27:12 -05003036 obj_request->length,
3037 obj_request->offset & ~PAGE_MASK,
3038 false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05003039 rbd_osd_req_format_read(obj_request);
Alex Elder430c28c2013-04-03 21:32:51 -05003040
Alex Elder788e2df2013-01-17 12:25:27 -06003041 ret = rbd_obj_request_submit(osdc, obj_request);
3042 if (ret)
3043 goto out;
3044 ret = rbd_obj_request_wait(obj_request);
3045 if (ret)
3046 goto out;
3047
3048 ret = obj_request->result;
3049 if (ret < 0)
3050 goto out;
Alex Elder1ceae7e2013-02-06 13:11:38 -06003051
3052 rbd_assert(obj_request->xferred <= (u64) SIZE_MAX);
3053 size = (size_t) obj_request->xferred;
Alex Elder903bb322013-02-06 13:11:38 -06003054 ceph_copy_from_page_vector(pages, buf, 0, size);
Alex Elder7097f8d2013-04-30 00:44:33 -05003055 rbd_assert(size <= (size_t)INT_MAX);
3056 ret = (int)size;
Alex Elder788e2df2013-01-17 12:25:27 -06003057out:
3058 if (obj_request)
3059 rbd_obj_request_put(obj_request);
3060 else
3061 ceph_release_page_vector(pages, page_count);
3062
3063 return ret;
3064}
3065
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003066/*
Alex Elder662518b2013-05-06 09:51:29 -05003067 * Read the complete header for the given rbd device. On successful
3068 * return, the rbd_dev->header field will contain up-to-date
3069 * information about the image.
Alex Elder4156d992012-08-02 11:29:46 -05003070 */
Alex Elder99a41eb2013-05-06 09:51:30 -05003071static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev)
Alex Elder4156d992012-08-02 11:29:46 -05003072{
3073 struct rbd_image_header_ondisk *ondisk = NULL;
3074 u32 snap_count = 0;
3075 u64 names_size = 0;
3076 u32 want_count;
3077 int ret;
3078
3079 /*
3080 * The complete header will include an array of its 64-bit
3081 * snapshot ids, followed by the names of those snapshots as
3082 * a contiguous block of NUL-terminated strings. Note that
3083 * the number of snapshots could change by the time we read
3084 * it in, in which case we re-read it.
3085 */
3086 do {
3087 size_t size;
3088
3089 kfree(ondisk);
3090
3091 size = sizeof (*ondisk);
3092 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
3093 size += names_size;
3094 ondisk = kmalloc(size, GFP_KERNEL);
3095 if (!ondisk)
Alex Elder662518b2013-05-06 09:51:29 -05003096 return -ENOMEM;
Alex Elder4156d992012-08-02 11:29:46 -05003097
Alex Elder788e2df2013-01-17 12:25:27 -06003098 ret = rbd_obj_read_sync(rbd_dev, rbd_dev->header_name,
Alex Elder7097f8d2013-04-30 00:44:33 -05003099 0, size, ondisk);
Alex Elder4156d992012-08-02 11:29:46 -05003100 if (ret < 0)
Alex Elder662518b2013-05-06 09:51:29 -05003101 goto out;
Alex Elderc0cd10db2013-04-26 09:43:47 -05003102 if ((size_t)ret < size) {
Alex Elder4156d992012-08-02 11:29:46 -05003103 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05003104 rbd_warn(rbd_dev, "short header read (want %zd got %d)",
3105 size, ret);
Alex Elder662518b2013-05-06 09:51:29 -05003106 goto out;
Alex Elder4156d992012-08-02 11:29:46 -05003107 }
3108 if (!rbd_dev_ondisk_valid(ondisk)) {
3109 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05003110 rbd_warn(rbd_dev, "invalid header");
Alex Elder662518b2013-05-06 09:51:29 -05003111 goto out;
Alex Elder4156d992012-08-02 11:29:46 -05003112 }
3113
3114 names_size = le64_to_cpu(ondisk->snap_names_len);
3115 want_count = snap_count;
3116 snap_count = le32_to_cpu(ondisk->snap_count);
3117 } while (snap_count != want_count);
3118
Alex Elder662518b2013-05-06 09:51:29 -05003119 ret = rbd_header_from_disk(rbd_dev, ondisk);
3120out:
Alex Elder4156d992012-08-02 11:29:46 -05003121 kfree(ondisk);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003122
Alex Elder4156d992012-08-02 11:29:46 -05003123 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003124}
3125
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003126/*
Alex Elder15228ed2013-05-01 12:43:03 -05003127 * Clear the rbd device's EXISTS flag if the snapshot it's mapped to
3128 * has disappeared from the (just updated) snapshot context.
3129 */
3130static void rbd_exists_validate(struct rbd_device *rbd_dev)
3131{
3132 u64 snap_id;
3133
3134 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags))
3135 return;
3136
3137 snap_id = rbd_dev->spec->snap_id;
3138 if (snap_id == CEPH_NOSNAP)
3139 return;
3140
3141 if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX)
3142 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
3143}
3144
Alex Eldercc4a38bd2013-04-30 00:44:33 -05003145static int rbd_dev_refresh(struct rbd_device *rbd_dev)
Alex Elder1fe5e992012-07-25 09:32:41 -05003146{
Alex Eldere627db02013-05-06 07:40:30 -05003147 u64 mapping_size;
Alex Elder1fe5e992012-07-25 09:32:41 -05003148 int ret;
3149
Alex Elder117973f2012-08-31 17:29:55 -05003150 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
Alex Eldere627db02013-05-06 07:40:30 -05003151 mapping_size = rbd_dev->mapping.size;
Alex Elder1fe5e992012-07-25 09:32:41 -05003152 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Alex Elder117973f2012-08-31 17:29:55 -05003153 if (rbd_dev->image_format == 1)
Alex Elder99a41eb2013-05-06 09:51:30 -05003154 ret = rbd_dev_v1_header_info(rbd_dev);
Alex Elder117973f2012-08-31 17:29:55 -05003155 else
Alex Elder2df3fac2013-05-06 09:51:30 -05003156 ret = rbd_dev_v2_header_info(rbd_dev);
Alex Elder15228ed2013-05-01 12:43:03 -05003157
3158 /* If it's a mapped snapshot, validate its EXISTS flag */
3159
3160 rbd_exists_validate(rbd_dev);
Alex Elder1fe5e992012-07-25 09:32:41 -05003161 mutex_unlock(&ctl_mutex);
Alex Elder00a653e2013-05-06 07:40:30 -05003162 if (mapping_size != rbd_dev->mapping.size) {
3163 sector_t size;
3164
3165 size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE;
3166 dout("setting size to %llu sectors", (unsigned long long)size);
3167 set_capacity(rbd_dev->disk, size);
Alex Eldera3fbe5d2013-04-30 00:44:32 -05003168 revalidate_disk(rbd_dev->disk);
Alex Elder00a653e2013-05-06 07:40:30 -05003169 }
Alex Elder1fe5e992012-07-25 09:32:41 -05003170
3171 return ret;
3172}
3173
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003174static int rbd_init_disk(struct rbd_device *rbd_dev)
3175{
3176 struct gendisk *disk;
3177 struct request_queue *q;
Alex Elder593a9e72012-02-07 12:03:37 -06003178 u64 segment_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003179
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003180 /* create gendisk info */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003181 disk = alloc_disk(RBD_MINORS_PER_MAJOR);
3182 if (!disk)
Alex Elder1fcdb8a2012-08-29 17:11:06 -05003183 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003184
Alex Elderf0f8cef2012-01-29 13:57:44 -06003185 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
Alex Elderde71a292012-07-03 16:01:19 -05003186 rbd_dev->dev_id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003187 disk->major = rbd_dev->major;
3188 disk->first_minor = 0;
3189 disk->fops = &rbd_bd_ops;
3190 disk->private_data = rbd_dev;
3191
Alex Elderbf0d5f502012-11-22 00:00:08 -06003192 q = blk_init_queue(rbd_request_fn, &rbd_dev->lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003193 if (!q)
3194 goto out_disk;
Josh Durgin029bcbd2011-07-22 11:35:23 -07003195
Alex Elder593a9e72012-02-07 12:03:37 -06003196 /* We use the default size, but let's be explicit about it. */
3197 blk_queue_physical_block_size(q, SECTOR_SIZE);
3198
Josh Durgin029bcbd2011-07-22 11:35:23 -07003199 /* set io sizes to object size */
Alex Elder593a9e72012-02-07 12:03:37 -06003200 segment_size = rbd_obj_bytes(&rbd_dev->header);
3201 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
3202 blk_queue_max_segment_size(q, segment_size);
3203 blk_queue_io_min(q, segment_size);
3204 blk_queue_io_opt(q, segment_size);
Josh Durgin029bcbd2011-07-22 11:35:23 -07003205
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003206 blk_queue_merge_bvec(q, rbd_merge_bvec);
3207 disk->queue = q;
3208
3209 q->queuedata = rbd_dev;
3210
3211 rbd_dev->disk = disk;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003212
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003213 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003214out_disk:
3215 put_disk(disk);
Alex Elder1fcdb8a2012-08-29 17:11:06 -05003216
3217 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003218}
3219
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003220/*
3221 sysfs
3222*/
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003223
Alex Elder593a9e72012-02-07 12:03:37 -06003224static struct rbd_device *dev_to_rbd_dev(struct device *dev)
3225{
3226 return container_of(dev, struct rbd_device, dev);
3227}
3228
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003229static ssize_t rbd_size_show(struct device *dev,
3230 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003231{
Alex Elder593a9e72012-02-07 12:03:37 -06003232 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003233
Alex Elderfc71d832013-04-26 15:44:36 -05003234 return sprintf(buf, "%llu\n",
3235 (unsigned long long)rbd_dev->mapping.size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003236}
3237
Alex Elder34b13182012-07-13 20:35:12 -05003238/*
3239 * Note this shows the features for whatever's mapped, which is not
3240 * necessarily the base image.
3241 */
3242static ssize_t rbd_features_show(struct device *dev,
3243 struct device_attribute *attr, char *buf)
3244{
3245 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3246
3247 return sprintf(buf, "0x%016llx\n",
Alex Elderfc71d832013-04-26 15:44:36 -05003248 (unsigned long long)rbd_dev->mapping.features);
Alex Elder34b13182012-07-13 20:35:12 -05003249}
3250
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003251static ssize_t rbd_major_show(struct device *dev,
3252 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003253{
Alex Elder593a9e72012-02-07 12:03:37 -06003254 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003255
Alex Elderfc71d832013-04-26 15:44:36 -05003256 if (rbd_dev->major)
3257 return sprintf(buf, "%d\n", rbd_dev->major);
3258
3259 return sprintf(buf, "(none)\n");
3260
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003261}
3262
3263static ssize_t rbd_client_id_show(struct device *dev,
3264 struct device_attribute *attr, char *buf)
3265{
Alex Elder593a9e72012-02-07 12:03:37 -06003266 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003267
Alex Elder1dbb4392012-01-24 10:08:37 -06003268 return sprintf(buf, "client%lld\n",
3269 ceph_client_id(rbd_dev->rbd_client->client));
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003270}
3271
3272static ssize_t rbd_pool_show(struct device *dev,
3273 struct device_attribute *attr, char *buf)
3274{
Alex Elder593a9e72012-02-07 12:03:37 -06003275 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003276
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003277 return sprintf(buf, "%s\n", rbd_dev->spec->pool_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003278}
3279
Alex Elder9bb2f332012-07-12 10:46:35 -05003280static ssize_t rbd_pool_id_show(struct device *dev,
3281 struct device_attribute *attr, char *buf)
3282{
3283 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3284
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003285 return sprintf(buf, "%llu\n",
Alex Elderfc71d832013-04-26 15:44:36 -05003286 (unsigned long long) rbd_dev->spec->pool_id);
Alex Elder9bb2f332012-07-12 10:46:35 -05003287}
3288
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003289static ssize_t rbd_name_show(struct device *dev,
3290 struct device_attribute *attr, char *buf)
3291{
Alex Elder593a9e72012-02-07 12:03:37 -06003292 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003293
Alex Eldera92ffdf2012-10-30 19:40:33 -05003294 if (rbd_dev->spec->image_name)
3295 return sprintf(buf, "%s\n", rbd_dev->spec->image_name);
3296
3297 return sprintf(buf, "(unknown)\n");
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003298}
3299
Alex Elder589d30e2012-07-10 20:30:11 -05003300static ssize_t rbd_image_id_show(struct device *dev,
3301 struct device_attribute *attr, char *buf)
3302{
3303 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3304
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003305 return sprintf(buf, "%s\n", rbd_dev->spec->image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05003306}
3307
Alex Elder34b13182012-07-13 20:35:12 -05003308/*
3309 * Shows the name of the currently-mapped snapshot (or
3310 * RBD_SNAP_HEAD_NAME for the base image).
3311 */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003312static ssize_t rbd_snap_show(struct device *dev,
3313 struct device_attribute *attr,
3314 char *buf)
3315{
Alex Elder593a9e72012-02-07 12:03:37 -06003316 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003317
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003318 return sprintf(buf, "%s\n", rbd_dev->spec->snap_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003319}
3320
Alex Elder86b00e02012-10-25 23:34:42 -05003321/*
3322 * For an rbd v2 image, shows the pool id, image id, and snapshot id
3323 * for the parent image. If there is no parent, simply shows
3324 * "(no parent image)".
3325 */
3326static ssize_t rbd_parent_show(struct device *dev,
3327 struct device_attribute *attr,
3328 char *buf)
3329{
3330 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3331 struct rbd_spec *spec = rbd_dev->parent_spec;
3332 int count;
3333 char *bufp = buf;
3334
3335 if (!spec)
3336 return sprintf(buf, "(no parent image)\n");
3337
3338 count = sprintf(bufp, "pool_id %llu\npool_name %s\n",
3339 (unsigned long long) spec->pool_id, spec->pool_name);
3340 if (count < 0)
3341 return count;
3342 bufp += count;
3343
3344 count = sprintf(bufp, "image_id %s\nimage_name %s\n", spec->image_id,
3345 spec->image_name ? spec->image_name : "(unknown)");
3346 if (count < 0)
3347 return count;
3348 bufp += count;
3349
3350 count = sprintf(bufp, "snap_id %llu\nsnap_name %s\n",
3351 (unsigned long long) spec->snap_id, spec->snap_name);
3352 if (count < 0)
3353 return count;
3354 bufp += count;
3355
3356 count = sprintf(bufp, "overlap %llu\n", rbd_dev->parent_overlap);
3357 if (count < 0)
3358 return count;
3359 bufp += count;
3360
3361 return (ssize_t) (bufp - buf);
3362}
3363
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003364static ssize_t rbd_image_refresh(struct device *dev,
3365 struct device_attribute *attr,
3366 const char *buf,
3367 size_t size)
3368{
Alex Elder593a9e72012-02-07 12:03:37 -06003369 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Alex Elderb8136232012-07-25 09:32:41 -05003370 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003371
Alex Eldercc4a38bd2013-04-30 00:44:33 -05003372 ret = rbd_dev_refresh(rbd_dev);
Alex Eldere627db02013-05-06 07:40:30 -05003373 if (ret)
3374 rbd_warn(rbd_dev, ": manual header refresh error (%d)\n", ret);
Alex Elderb8136232012-07-25 09:32:41 -05003375
3376 return ret < 0 ? ret : size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003377}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003378
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003379static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
Alex Elder34b13182012-07-13 20:35:12 -05003380static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003381static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
3382static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
3383static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
Alex Elder9bb2f332012-07-12 10:46:35 -05003384static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003385static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
Alex Elder589d30e2012-07-10 20:30:11 -05003386static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003387static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
3388static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
Alex Elder86b00e02012-10-25 23:34:42 -05003389static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003390
3391static struct attribute *rbd_attrs[] = {
3392 &dev_attr_size.attr,
Alex Elder34b13182012-07-13 20:35:12 -05003393 &dev_attr_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003394 &dev_attr_major.attr,
3395 &dev_attr_client_id.attr,
3396 &dev_attr_pool.attr,
Alex Elder9bb2f332012-07-12 10:46:35 -05003397 &dev_attr_pool_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003398 &dev_attr_name.attr,
Alex Elder589d30e2012-07-10 20:30:11 -05003399 &dev_attr_image_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003400 &dev_attr_current_snap.attr,
Alex Elder86b00e02012-10-25 23:34:42 -05003401 &dev_attr_parent.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003402 &dev_attr_refresh.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003403 NULL
3404};
3405
3406static struct attribute_group rbd_attr_group = {
3407 .attrs = rbd_attrs,
3408};
3409
3410static const struct attribute_group *rbd_attr_groups[] = {
3411 &rbd_attr_group,
3412 NULL
3413};
3414
3415static void rbd_sysfs_dev_release(struct device *dev)
3416{
3417}
3418
3419static struct device_type rbd_device_type = {
3420 .name = "rbd",
3421 .groups = rbd_attr_groups,
3422 .release = rbd_sysfs_dev_release,
3423};
3424
Alex Elder8b8fb992012-10-26 17:25:24 -05003425static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)
3426{
3427 kref_get(&spec->kref);
3428
3429 return spec;
3430}
3431
3432static void rbd_spec_free(struct kref *kref);
3433static void rbd_spec_put(struct rbd_spec *spec)
3434{
3435 if (spec)
3436 kref_put(&spec->kref, rbd_spec_free);
3437}
3438
3439static struct rbd_spec *rbd_spec_alloc(void)
3440{
3441 struct rbd_spec *spec;
3442
3443 spec = kzalloc(sizeof (*spec), GFP_KERNEL);
3444 if (!spec)
3445 return NULL;
3446 kref_init(&spec->kref);
3447
Alex Elder8b8fb992012-10-26 17:25:24 -05003448 return spec;
3449}
3450
3451static void rbd_spec_free(struct kref *kref)
3452{
3453 struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref);
3454
3455 kfree(spec->pool_name);
3456 kfree(spec->image_id);
3457 kfree(spec->image_name);
3458 kfree(spec->snap_name);
3459 kfree(spec);
3460}
3461
Alex Eldercc344fa2013-02-19 12:25:56 -06003462static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,
Alex Elderc53d5892012-10-25 23:34:42 -05003463 struct rbd_spec *spec)
3464{
3465 struct rbd_device *rbd_dev;
3466
3467 rbd_dev = kzalloc(sizeof (*rbd_dev), GFP_KERNEL);
3468 if (!rbd_dev)
3469 return NULL;
3470
3471 spin_lock_init(&rbd_dev->lock);
Alex Elder6d292902013-01-14 12:43:31 -06003472 rbd_dev->flags = 0;
Alex Elderc53d5892012-10-25 23:34:42 -05003473 INIT_LIST_HEAD(&rbd_dev->node);
Alex Elderc53d5892012-10-25 23:34:42 -05003474 init_rwsem(&rbd_dev->header_rwsem);
3475
3476 rbd_dev->spec = spec;
3477 rbd_dev->rbd_client = rbdc;
3478
Alex Elder0903e872012-11-14 12:25:19 -06003479 /* Initialize the layout used for all rbd requests */
3480
3481 rbd_dev->layout.fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
3482 rbd_dev->layout.fl_stripe_count = cpu_to_le32(1);
3483 rbd_dev->layout.fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
3484 rbd_dev->layout.fl_pg_pool = cpu_to_le32((u32) spec->pool_id);
3485
Alex Elderc53d5892012-10-25 23:34:42 -05003486 return rbd_dev;
3487}
3488
3489static void rbd_dev_destroy(struct rbd_device *rbd_dev)
3490{
Alex Elderc53d5892012-10-25 23:34:42 -05003491 rbd_put_client(rbd_dev->rbd_client);
3492 rbd_spec_put(rbd_dev->spec);
3493 kfree(rbd_dev);
3494}
3495
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003496/*
Alex Elder9d475de2012-07-03 16:01:19 -05003497 * Get the size and object order for an image snapshot, or if
3498 * snap_id is CEPH_NOSNAP, gets this information for the base
3499 * image.
3500 */
3501static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
3502 u8 *order, u64 *snap_size)
3503{
3504 __le64 snapid = cpu_to_le64(snap_id);
3505 int ret;
3506 struct {
3507 u8 order;
3508 __le64 size;
3509 } __attribute__ ((packed)) size_buf = { 0 };
3510
Alex Elder36be9a72013-01-19 00:30:28 -06003511 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elder9d475de2012-07-03 16:01:19 -05003512 "rbd", "get_size",
Alex Elder41579762013-04-21 12:14:45 -05003513 &snapid, sizeof (snapid),
Alex Eldere2a58ee2013-04-30 00:44:33 -05003514 &size_buf, sizeof (size_buf));
Alex Elder36be9a72013-01-19 00:30:28 -06003515 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder9d475de2012-07-03 16:01:19 -05003516 if (ret < 0)
3517 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05003518 if (ret < sizeof (size_buf))
3519 return -ERANGE;
Alex Elder9d475de2012-07-03 16:01:19 -05003520
Alex Elderc86f86e2013-04-25 15:09:41 -05003521 if (order)
3522 *order = size_buf.order;
Alex Elder9d475de2012-07-03 16:01:19 -05003523 *snap_size = le64_to_cpu(size_buf.size);
3524
3525 dout(" snap_id 0x%016llx order = %u, snap_size = %llu\n",
Alex Elder57385b52013-04-21 12:14:45 -05003526 (unsigned long long)snap_id, (unsigned int)*order,
3527 (unsigned long long)*snap_size);
Alex Elder9d475de2012-07-03 16:01:19 -05003528
3529 return 0;
3530}
3531
3532static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
3533{
3534 return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
3535 &rbd_dev->header.obj_order,
3536 &rbd_dev->header.image_size);
3537}
3538
Alex Elder1e130192012-07-03 16:01:19 -05003539static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
3540{
3541 void *reply_buf;
3542 int ret;
3543 void *p;
3544
3545 reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL);
3546 if (!reply_buf)
3547 return -ENOMEM;
3548
Alex Elder36be9a72013-01-19 00:30:28 -06003549 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elder41579762013-04-21 12:14:45 -05003550 "rbd", "get_object_prefix", NULL, 0,
Alex Eldere2a58ee2013-04-30 00:44:33 -05003551 reply_buf, RBD_OBJ_PREFIX_LEN_MAX);
Alex Elder36be9a72013-01-19 00:30:28 -06003552 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder1e130192012-07-03 16:01:19 -05003553 if (ret < 0)
3554 goto out;
3555
3556 p = reply_buf;
3557 rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p,
Alex Elder57385b52013-04-21 12:14:45 -05003558 p + ret, NULL, GFP_NOIO);
3559 ret = 0;
Alex Elder1e130192012-07-03 16:01:19 -05003560
3561 if (IS_ERR(rbd_dev->header.object_prefix)) {
3562 ret = PTR_ERR(rbd_dev->header.object_prefix);
3563 rbd_dev->header.object_prefix = NULL;
3564 } else {
3565 dout(" object_prefix = %s\n", rbd_dev->header.object_prefix);
3566 }
Alex Elder1e130192012-07-03 16:01:19 -05003567out:
3568 kfree(reply_buf);
3569
3570 return ret;
3571}
3572
Alex Elderb1b54022012-07-03 16:01:19 -05003573static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
3574 u64 *snap_features)
3575{
3576 __le64 snapid = cpu_to_le64(snap_id);
3577 struct {
3578 __le64 features;
3579 __le64 incompat;
Alex Elder41579762013-04-21 12:14:45 -05003580 } __attribute__ ((packed)) features_buf = { 0 };
Alex Elderd8891402012-10-09 13:50:17 -07003581 u64 incompat;
Alex Elderb1b54022012-07-03 16:01:19 -05003582 int ret;
3583
Alex Elder36be9a72013-01-19 00:30:28 -06003584 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elderb1b54022012-07-03 16:01:19 -05003585 "rbd", "get_features",
Alex Elder41579762013-04-21 12:14:45 -05003586 &snapid, sizeof (snapid),
Alex Eldere2a58ee2013-04-30 00:44:33 -05003587 &features_buf, sizeof (features_buf));
Alex Elder36be9a72013-01-19 00:30:28 -06003588 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderb1b54022012-07-03 16:01:19 -05003589 if (ret < 0)
3590 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05003591 if (ret < sizeof (features_buf))
3592 return -ERANGE;
Alex Elderd8891402012-10-09 13:50:17 -07003593
3594 incompat = le64_to_cpu(features_buf.incompat);
Alex Elder5cbf6f122013-04-11 09:29:48 -05003595 if (incompat & ~RBD_FEATURES_SUPPORTED)
Alex Elderb8f5c6e2012-11-01 08:39:26 -05003596 return -ENXIO;
Alex Elderd8891402012-10-09 13:50:17 -07003597
Alex Elderb1b54022012-07-03 16:01:19 -05003598 *snap_features = le64_to_cpu(features_buf.features);
3599
3600 dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
Alex Elder57385b52013-04-21 12:14:45 -05003601 (unsigned long long)snap_id,
3602 (unsigned long long)*snap_features,
3603 (unsigned long long)le64_to_cpu(features_buf.incompat));
Alex Elderb1b54022012-07-03 16:01:19 -05003604
3605 return 0;
3606}
3607
3608static int rbd_dev_v2_features(struct rbd_device *rbd_dev)
3609{
3610 return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP,
3611 &rbd_dev->header.features);
3612}
3613
Alex Elder86b00e02012-10-25 23:34:42 -05003614static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev)
3615{
3616 struct rbd_spec *parent_spec;
3617 size_t size;
3618 void *reply_buf = NULL;
3619 __le64 snapid;
3620 void *p;
3621 void *end;
Alex Elder642a2532013-05-06 17:40:33 -05003622 u64 pool_id;
Alex Elder86b00e02012-10-25 23:34:42 -05003623 char *image_id;
3624 u64 overlap;
Alex Elder86b00e02012-10-25 23:34:42 -05003625 int ret;
3626
3627 parent_spec = rbd_spec_alloc();
3628 if (!parent_spec)
3629 return -ENOMEM;
3630
3631 size = sizeof (__le64) + /* pool_id */
3632 sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */
3633 sizeof (__le64) + /* snap_id */
3634 sizeof (__le64); /* overlap */
3635 reply_buf = kmalloc(size, GFP_KERNEL);
3636 if (!reply_buf) {
3637 ret = -ENOMEM;
3638 goto out_err;
3639 }
3640
3641 snapid = cpu_to_le64(CEPH_NOSNAP);
Alex Elder36be9a72013-01-19 00:30:28 -06003642 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elder86b00e02012-10-25 23:34:42 -05003643 "rbd", "get_parent",
Alex Elder41579762013-04-21 12:14:45 -05003644 &snapid, sizeof (snapid),
Alex Eldere2a58ee2013-04-30 00:44:33 -05003645 reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06003646 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder86b00e02012-10-25 23:34:42 -05003647 if (ret < 0)
3648 goto out_err;
3649
Alex Elder86b00e02012-10-25 23:34:42 -05003650 p = reply_buf;
Alex Elder57385b52013-04-21 12:14:45 -05003651 end = reply_buf + ret;
3652 ret = -ERANGE;
Alex Elder642a2532013-05-06 17:40:33 -05003653 ceph_decode_64_safe(&p, end, pool_id, out_err);
3654 if (pool_id == CEPH_NOPOOL)
Alex Elder86b00e02012-10-25 23:34:42 -05003655 goto out; /* No parent? No problem. */
3656
Alex Elder0903e872012-11-14 12:25:19 -06003657 /* The ceph file layout needs to fit pool id in 32 bits */
3658
3659 ret = -EIO;
Alex Elder642a2532013-05-06 17:40:33 -05003660 if (pool_id > (u64)U32_MAX) {
Alex Elderc0cd10db2013-04-26 09:43:47 -05003661 rbd_warn(NULL, "parent pool id too large (%llu > %u)\n",
Alex Elder642a2532013-05-06 17:40:33 -05003662 (unsigned long long)pool_id, U32_MAX);
Alex Elder57385b52013-04-21 12:14:45 -05003663 goto out_err;
Alex Elderc0cd10db2013-04-26 09:43:47 -05003664 }
Alex Elder642a2532013-05-06 17:40:33 -05003665 parent_spec->pool_id = pool_id;
Alex Elder0903e872012-11-14 12:25:19 -06003666
Alex Elder979ed482012-11-01 08:39:26 -05003667 image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elder86b00e02012-10-25 23:34:42 -05003668 if (IS_ERR(image_id)) {
3669 ret = PTR_ERR(image_id);
3670 goto out_err;
3671 }
3672 parent_spec->image_id = image_id;
3673 ceph_decode_64_safe(&p, end, parent_spec->snap_id, out_err);
3674 ceph_decode_64_safe(&p, end, overlap, out_err);
3675
Alex Elder70cf49c2013-05-06 17:40:33 -05003676 if (overlap) {
Alex Elder642a2532013-05-06 17:40:33 -05003677 rbd_spec_put(rbd_dev->parent_spec);
Alex Elder70cf49c2013-05-06 17:40:33 -05003678 rbd_dev->parent_spec = parent_spec;
3679 parent_spec = NULL; /* rbd_dev now owns this */
3680 rbd_dev->parent_overlap = overlap;
3681 } else {
3682 rbd_warn(rbd_dev, "ignoring parent of clone with overlap 0\n");
3683 }
Alex Elder86b00e02012-10-25 23:34:42 -05003684out:
3685 ret = 0;
3686out_err:
3687 kfree(reply_buf);
3688 rbd_spec_put(parent_spec);
3689
3690 return ret;
3691}
3692
Alex Eldercc070d52013-04-21 12:14:45 -05003693static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev)
3694{
3695 struct {
3696 __le64 stripe_unit;
3697 __le64 stripe_count;
3698 } __attribute__ ((packed)) striping_info_buf = { 0 };
3699 size_t size = sizeof (striping_info_buf);
3700 void *p;
3701 u64 obj_size;
3702 u64 stripe_unit;
3703 u64 stripe_count;
3704 int ret;
3705
3706 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
3707 "rbd", "get_stripe_unit_count", NULL, 0,
Alex Eldere2a58ee2013-04-30 00:44:33 -05003708 (char *)&striping_info_buf, size);
Alex Eldercc070d52013-04-21 12:14:45 -05003709 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
3710 if (ret < 0)
3711 return ret;
3712 if (ret < size)
3713 return -ERANGE;
3714
3715 /*
3716 * We don't actually support the "fancy striping" feature
3717 * (STRIPINGV2) yet, but if the striping sizes are the
3718 * defaults the behavior is the same as before. So find
3719 * out, and only fail if the image has non-default values.
3720 */
3721 ret = -EINVAL;
3722 obj_size = (u64)1 << rbd_dev->header.obj_order;
3723 p = &striping_info_buf;
3724 stripe_unit = ceph_decode_64(&p);
3725 if (stripe_unit != obj_size) {
3726 rbd_warn(rbd_dev, "unsupported stripe unit "
3727 "(got %llu want %llu)",
3728 stripe_unit, obj_size);
3729 return -EINVAL;
3730 }
3731 stripe_count = ceph_decode_64(&p);
3732 if (stripe_count != 1) {
3733 rbd_warn(rbd_dev, "unsupported stripe count "
3734 "(got %llu want 1)", stripe_count);
3735 return -EINVAL;
3736 }
Alex Elder500d0c02013-04-26 09:43:47 -05003737 rbd_dev->header.stripe_unit = stripe_unit;
3738 rbd_dev->header.stripe_count = stripe_count;
Alex Eldercc070d52013-04-21 12:14:45 -05003739
3740 return 0;
3741}
3742
Alex Elder9e15b772012-10-30 19:40:33 -05003743static char *rbd_dev_image_name(struct rbd_device *rbd_dev)
3744{
3745 size_t image_id_size;
3746 char *image_id;
3747 void *p;
3748 void *end;
3749 size_t size;
3750 void *reply_buf = NULL;
3751 size_t len = 0;
3752 char *image_name = NULL;
3753 int ret;
3754
3755 rbd_assert(!rbd_dev->spec->image_name);
3756
Alex Elder69e7a022012-11-01 08:39:26 -05003757 len = strlen(rbd_dev->spec->image_id);
3758 image_id_size = sizeof (__le32) + len;
Alex Elder9e15b772012-10-30 19:40:33 -05003759 image_id = kmalloc(image_id_size, GFP_KERNEL);
3760 if (!image_id)
3761 return NULL;
3762
3763 p = image_id;
Alex Elder41579762013-04-21 12:14:45 -05003764 end = image_id + image_id_size;
Alex Elder57385b52013-04-21 12:14:45 -05003765 ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len);
Alex Elder9e15b772012-10-30 19:40:33 -05003766
3767 size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX;
3768 reply_buf = kmalloc(size, GFP_KERNEL);
3769 if (!reply_buf)
3770 goto out;
3771
Alex Elder36be9a72013-01-19 00:30:28 -06003772 ret = rbd_obj_method_sync(rbd_dev, RBD_DIRECTORY,
Alex Elder9e15b772012-10-30 19:40:33 -05003773 "rbd", "dir_get_name",
3774 image_id, image_id_size,
Alex Eldere2a58ee2013-04-30 00:44:33 -05003775 reply_buf, size);
Alex Elder9e15b772012-10-30 19:40:33 -05003776 if (ret < 0)
3777 goto out;
3778 p = reply_buf;
Alex Elderf40eb342013-04-25 15:09:42 -05003779 end = reply_buf + ret;
3780
Alex Elder9e15b772012-10-30 19:40:33 -05003781 image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL);
3782 if (IS_ERR(image_name))
3783 image_name = NULL;
3784 else
3785 dout("%s: name is %s len is %zd\n", __func__, image_name, len);
3786out:
3787 kfree(reply_buf);
3788 kfree(image_id);
3789
3790 return image_name;
3791}
3792
Alex Elder2ad3d712013-04-30 00:44:33 -05003793static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
3794{
3795 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
3796 const char *snap_name;
3797 u32 which = 0;
3798
3799 /* Skip over names until we find the one we are looking for */
3800
3801 snap_name = rbd_dev->header.snap_names;
3802 while (which < snapc->num_snaps) {
3803 if (!strcmp(name, snap_name))
3804 return snapc->snaps[which];
3805 snap_name += strlen(snap_name) + 1;
3806 which++;
3807 }
3808 return CEPH_NOSNAP;
3809}
3810
3811static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
3812{
3813 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
3814 u32 which;
3815 bool found = false;
3816 u64 snap_id;
3817
3818 for (which = 0; !found && which < snapc->num_snaps; which++) {
3819 const char *snap_name;
3820
3821 snap_id = snapc->snaps[which];
3822 snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id);
3823 if (IS_ERR(snap_name))
3824 break;
3825 found = !strcmp(name, snap_name);
3826 kfree(snap_name);
3827 }
3828 return found ? snap_id : CEPH_NOSNAP;
3829}
3830
3831/*
3832 * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if
3833 * no snapshot by that name is found, or if an error occurs.
3834 */
3835static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
3836{
3837 if (rbd_dev->image_format == 1)
3838 return rbd_v1_snap_id_by_name(rbd_dev, name);
3839
3840 return rbd_v2_snap_id_by_name(rbd_dev, name);
3841}
3842
Alex Elder9e15b772012-10-30 19:40:33 -05003843/*
Alex Elder2e9f7f12013-04-26 09:43:48 -05003844 * When an rbd image has a parent image, it is identified by the
3845 * pool, image, and snapshot ids (not names). This function fills
3846 * in the names for those ids. (It's OK if we can't figure out the
3847 * name for an image id, but the pool and snapshot ids should always
3848 * exist and have names.) All names in an rbd spec are dynamically
3849 * allocated.
Alex Eldere1d42132013-04-25 23:15:08 -05003850 *
3851 * When an image being mapped (not a parent) is probed, we have the
3852 * pool name and pool id, image name and image id, and the snapshot
3853 * name. The only thing we're missing is the snapshot id.
Alex Elder9e15b772012-10-30 19:40:33 -05003854 */
Alex Elder2e9f7f12013-04-26 09:43:48 -05003855static int rbd_dev_spec_update(struct rbd_device *rbd_dev)
Alex Elder9e15b772012-10-30 19:40:33 -05003856{
Alex Elder2e9f7f12013-04-26 09:43:48 -05003857 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3858 struct rbd_spec *spec = rbd_dev->spec;
3859 const char *pool_name;
3860 const char *image_name;
3861 const char *snap_name;
Alex Elder9e15b772012-10-30 19:40:33 -05003862 int ret;
3863
Alex Eldere1d42132013-04-25 23:15:08 -05003864 /*
3865 * An image being mapped will have the pool name (etc.), but
3866 * we need to look up the snapshot id.
3867 */
Alex Elder2e9f7f12013-04-26 09:43:48 -05003868 if (spec->pool_name) {
3869 if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) {
Alex Elder2ad3d712013-04-30 00:44:33 -05003870 u64 snap_id;
Alex Eldere1d42132013-04-25 23:15:08 -05003871
Alex Elder2ad3d712013-04-30 00:44:33 -05003872 snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name);
3873 if (snap_id == CEPH_NOSNAP)
Alex Eldere1d42132013-04-25 23:15:08 -05003874 return -ENOENT;
Alex Elder2ad3d712013-04-30 00:44:33 -05003875 spec->snap_id = snap_id;
Alex Eldere1d42132013-04-25 23:15:08 -05003876 } else {
Alex Elder2e9f7f12013-04-26 09:43:48 -05003877 spec->snap_id = CEPH_NOSNAP;
Alex Eldere1d42132013-04-25 23:15:08 -05003878 }
3879
3880 return 0;
3881 }
Alex Elder9e15b772012-10-30 19:40:33 -05003882
Alex Elder2e9f7f12013-04-26 09:43:48 -05003883 /* Get the pool name; we have to make our own copy of this */
Alex Elder9e15b772012-10-30 19:40:33 -05003884
Alex Elder2e9f7f12013-04-26 09:43:48 -05003885 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id);
3886 if (!pool_name) {
3887 rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id);
Alex Elder935dc892012-11-01 10:17:15 -05003888 return -EIO;
3889 }
Alex Elder2e9f7f12013-04-26 09:43:48 -05003890 pool_name = kstrdup(pool_name, GFP_KERNEL);
3891 if (!pool_name)
Alex Elder9e15b772012-10-30 19:40:33 -05003892 return -ENOMEM;
3893
3894 /* Fetch the image name; tolerate failure here */
3895
Alex Elder2e9f7f12013-04-26 09:43:48 -05003896 image_name = rbd_dev_image_name(rbd_dev);
3897 if (!image_name)
Alex Elder06ecc6c2012-11-01 10:17:15 -05003898 rbd_warn(rbd_dev, "unable to get image name");
Alex Elder9e15b772012-10-30 19:40:33 -05003899
Alex Elder2e9f7f12013-04-26 09:43:48 -05003900 /* Look up the snapshot name, and make a copy */
Alex Elder9e15b772012-10-30 19:40:33 -05003901
Alex Elder2e9f7f12013-04-26 09:43:48 -05003902 snap_name = rbd_snap_name(rbd_dev, spec->snap_id);
3903 if (!snap_name) {
Alex Elder2e9f7f12013-04-26 09:43:48 -05003904 ret = -ENOMEM;
Alex Elder9e15b772012-10-30 19:40:33 -05003905 goto out_err;
Alex Elder2e9f7f12013-04-26 09:43:48 -05003906 }
3907
3908 spec->pool_name = pool_name;
3909 spec->image_name = image_name;
3910 spec->snap_name = snap_name;
Alex Elder9e15b772012-10-30 19:40:33 -05003911
3912 return 0;
3913out_err:
Alex Elder2e9f7f12013-04-26 09:43:48 -05003914 kfree(image_name);
3915 kfree(pool_name);
Alex Elder9e15b772012-10-30 19:40:33 -05003916
3917 return ret;
3918}
3919
Alex Eldercc4a38bd2013-04-30 00:44:33 -05003920static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev)
Alex Elder35d489f2012-07-03 16:01:19 -05003921{
3922 size_t size;
3923 int ret;
3924 void *reply_buf;
3925 void *p;
3926 void *end;
3927 u64 seq;
3928 u32 snap_count;
3929 struct ceph_snap_context *snapc;
3930 u32 i;
3931
3932 /*
3933 * We'll need room for the seq value (maximum snapshot id),
3934 * snapshot count, and array of that many snapshot ids.
3935 * For now we have a fixed upper limit on the number we're
3936 * prepared to receive.
3937 */
3938 size = sizeof (__le64) + sizeof (__le32) +
3939 RBD_MAX_SNAP_COUNT * sizeof (__le64);
3940 reply_buf = kzalloc(size, GFP_KERNEL);
3941 if (!reply_buf)
3942 return -ENOMEM;
3943
Alex Elder36be9a72013-01-19 00:30:28 -06003944 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elder41579762013-04-21 12:14:45 -05003945 "rbd", "get_snapcontext", NULL, 0,
Alex Eldere2a58ee2013-04-30 00:44:33 -05003946 reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06003947 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder35d489f2012-07-03 16:01:19 -05003948 if (ret < 0)
3949 goto out;
3950
Alex Elder35d489f2012-07-03 16:01:19 -05003951 p = reply_buf;
Alex Elder57385b52013-04-21 12:14:45 -05003952 end = reply_buf + ret;
3953 ret = -ERANGE;
Alex Elder35d489f2012-07-03 16:01:19 -05003954 ceph_decode_64_safe(&p, end, seq, out);
3955 ceph_decode_32_safe(&p, end, snap_count, out);
3956
3957 /*
3958 * Make sure the reported number of snapshot ids wouldn't go
3959 * beyond the end of our buffer. But before checking that,
3960 * make sure the computed size of the snapshot context we
3961 * allocate is representable in a size_t.
3962 */
3963 if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
3964 / sizeof (u64)) {
3965 ret = -EINVAL;
3966 goto out;
3967 }
3968 if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
3969 goto out;
Alex Elder468521c2013-04-26 09:43:47 -05003970 ret = 0;
Alex Elder35d489f2012-07-03 16:01:19 -05003971
Alex Elder812164f82013-04-30 00:44:32 -05003972 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
Alex Elder35d489f2012-07-03 16:01:19 -05003973 if (!snapc) {
3974 ret = -ENOMEM;
3975 goto out;
3976 }
Alex Elder35d489f2012-07-03 16:01:19 -05003977 snapc->seq = seq;
Alex Elder35d489f2012-07-03 16:01:19 -05003978 for (i = 0; i < snap_count; i++)
3979 snapc->snaps[i] = ceph_decode_64(&p);
3980
Alex Elder49ece552013-05-06 08:37:00 -05003981 ceph_put_snap_context(rbd_dev->header.snapc);
Alex Elder35d489f2012-07-03 16:01:19 -05003982 rbd_dev->header.snapc = snapc;
3983
3984 dout(" snap context seq = %llu, snap_count = %u\n",
Alex Elder57385b52013-04-21 12:14:45 -05003985 (unsigned long long)seq, (unsigned int)snap_count);
Alex Elder35d489f2012-07-03 16:01:19 -05003986out:
3987 kfree(reply_buf);
3988
Alex Elder57385b52013-04-21 12:14:45 -05003989 return ret;
Alex Elder35d489f2012-07-03 16:01:19 -05003990}
3991
Alex Elder54cac612013-04-30 00:44:33 -05003992static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
3993 u64 snap_id)
Alex Elderb8b1e2d2012-07-03 16:01:19 -05003994{
3995 size_t size;
3996 void *reply_buf;
Alex Elder54cac612013-04-30 00:44:33 -05003997 __le64 snapid;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05003998 int ret;
3999 void *p;
4000 void *end;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004001 char *snap_name;
4002
4003 size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN;
4004 reply_buf = kmalloc(size, GFP_KERNEL);
4005 if (!reply_buf)
4006 return ERR_PTR(-ENOMEM);
4007
Alex Elder54cac612013-04-30 00:44:33 -05004008 snapid = cpu_to_le64(snap_id);
Alex Elder36be9a72013-01-19 00:30:28 -06004009 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004010 "rbd", "get_snapshot_name",
Alex Elder54cac612013-04-30 00:44:33 -05004011 &snapid, sizeof (snapid),
Alex Eldere2a58ee2013-04-30 00:44:33 -05004012 reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06004013 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderf40eb342013-04-25 15:09:42 -05004014 if (ret < 0) {
4015 snap_name = ERR_PTR(ret);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004016 goto out;
Alex Elderf40eb342013-04-25 15:09:42 -05004017 }
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004018
4019 p = reply_buf;
Alex Elderf40eb342013-04-25 15:09:42 -05004020 end = reply_buf + ret;
Alex Eldere5c35532012-10-25 23:34:41 -05004021 snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elderf40eb342013-04-25 15:09:42 -05004022 if (IS_ERR(snap_name))
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004023 goto out;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004024
Alex Elderf40eb342013-04-25 15:09:42 -05004025 dout(" snap_id 0x%016llx snap_name = %s\n",
Alex Elder54cac612013-04-30 00:44:33 -05004026 (unsigned long long)snap_id, snap_name);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004027out:
4028 kfree(reply_buf);
4029
Alex Elderf40eb342013-04-25 15:09:42 -05004030 return snap_name;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004031}
4032
Alex Elder2df3fac2013-05-06 09:51:30 -05004033static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev)
Alex Elder117973f2012-08-31 17:29:55 -05004034{
Alex Elder2df3fac2013-05-06 09:51:30 -05004035 bool first_time = rbd_dev->header.object_prefix == NULL;
Alex Elder117973f2012-08-31 17:29:55 -05004036 int ret;
Alex Elder117973f2012-08-31 17:29:55 -05004037
4038 down_write(&rbd_dev->header_rwsem);
4039
Alex Elder2df3fac2013-05-06 09:51:30 -05004040 if (first_time) {
4041 ret = rbd_dev_v2_header_onetime(rbd_dev);
4042 if (ret)
4043 goto out;
4044 }
4045
Alex Elder642a2532013-05-06 17:40:33 -05004046 /*
4047 * If the image supports layering, get the parent info. We
4048 * need to probe the first time regardless. Thereafter we
4049 * only need to if there's a parent, to see if it has
4050 * disappeared due to the mapped image getting flattened.
4051 */
4052 if (rbd_dev->header.features & RBD_FEATURE_LAYERING &&
4053 (first_time || rbd_dev->parent_spec)) {
4054 bool warn;
4055
4056 ret = rbd_dev_v2_parent_info(rbd_dev);
4057 if (ret)
4058 goto out;
4059
4060 /*
4061 * Print a warning if this is the initial probe and
4062 * the image has a parent. Don't print it if the
4063 * image now being probed is itself a parent. We
4064 * can tell at this point because we won't know its
4065 * pool name yet (just its pool id).
4066 */
4067 warn = rbd_dev->parent_spec && rbd_dev->spec->pool_name;
4068 if (first_time && warn)
4069 rbd_warn(rbd_dev, "WARNING: kernel layering "
4070 "is EXPERIMENTAL!");
4071 }
4072
Alex Elder117973f2012-08-31 17:29:55 -05004073 ret = rbd_dev_v2_image_size(rbd_dev);
4074 if (ret)
4075 goto out;
Alex Elder642a2532013-05-06 17:40:33 -05004076
Alex Elder29334ba2013-05-06 07:40:30 -05004077 if (rbd_dev->spec->snap_id == CEPH_NOSNAP)
4078 if (rbd_dev->mapping.size != rbd_dev->header.image_size)
4079 rbd_dev->mapping.size = rbd_dev->header.image_size;
Alex Elder117973f2012-08-31 17:29:55 -05004080
Alex Eldercc4a38bd2013-04-30 00:44:33 -05004081 ret = rbd_dev_v2_snap_context(rbd_dev);
Alex Elder117973f2012-08-31 17:29:55 -05004082 dout("rbd_dev_v2_snap_context returned %d\n", ret);
Alex Elder117973f2012-08-31 17:29:55 -05004083out:
4084 up_write(&rbd_dev->header_rwsem);
4085
4086 return ret;
4087}
4088
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004089static int rbd_bus_add_dev(struct rbd_device *rbd_dev)
4090{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004091 struct device *dev;
Alex Eldercd789ab2012-08-30 00:16:38 -05004092 int ret;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004093
4094 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004095
Alex Eldercd789ab2012-08-30 00:16:38 -05004096 dev = &rbd_dev->dev;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004097 dev->bus = &rbd_bus_type;
4098 dev->type = &rbd_device_type;
4099 dev->parent = &rbd_root_dev;
Alex Elder200a6a82013-04-28 23:32:34 -05004100 dev->release = rbd_dev_device_release;
Alex Elderde71a292012-07-03 16:01:19 -05004101 dev_set_name(dev, "%d", rbd_dev->dev_id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004102 ret = device_register(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004103
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004104 mutex_unlock(&ctl_mutex);
Alex Eldercd789ab2012-08-30 00:16:38 -05004105
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004106 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004107}
4108
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004109static void rbd_bus_del_dev(struct rbd_device *rbd_dev)
4110{
4111 device_unregister(&rbd_dev->dev);
4112}
4113
Alex Eldere2839302012-08-29 17:11:06 -05004114static atomic64_t rbd_dev_id_max = ATOMIC64_INIT(0);
Alex Elder1ddbe942012-01-29 13:57:44 -06004115
4116/*
Alex Elder499afd52012-02-02 08:13:29 -06004117 * Get a unique rbd identifier for the given new rbd_dev, and add
4118 * the rbd_dev to the global list. The minimum rbd id is 1.
Alex Elder1ddbe942012-01-29 13:57:44 -06004119 */
Alex Eldere2839302012-08-29 17:11:06 -05004120static void rbd_dev_id_get(struct rbd_device *rbd_dev)
Alex Elderb7f23c32012-01-29 13:57:43 -06004121{
Alex Eldere2839302012-08-29 17:11:06 -05004122 rbd_dev->dev_id = atomic64_inc_return(&rbd_dev_id_max);
Alex Elder499afd52012-02-02 08:13:29 -06004123
4124 spin_lock(&rbd_dev_list_lock);
4125 list_add_tail(&rbd_dev->node, &rbd_dev_list);
4126 spin_unlock(&rbd_dev_list_lock);
Alex Eldere2839302012-08-29 17:11:06 -05004127 dout("rbd_dev %p given dev id %llu\n", rbd_dev,
4128 (unsigned long long) rbd_dev->dev_id);
Alex Elder1ddbe942012-01-29 13:57:44 -06004129}
Alex Elderb7f23c32012-01-29 13:57:43 -06004130
Alex Elder1ddbe942012-01-29 13:57:44 -06004131/*
Alex Elder499afd52012-02-02 08:13:29 -06004132 * Remove an rbd_dev from the global list, and record that its
4133 * identifier is no longer in use.
Alex Elder1ddbe942012-01-29 13:57:44 -06004134 */
Alex Eldere2839302012-08-29 17:11:06 -05004135static void rbd_dev_id_put(struct rbd_device *rbd_dev)
Alex Elder1ddbe942012-01-29 13:57:44 -06004136{
Alex Elderd184f6b2012-01-29 13:57:44 -06004137 struct list_head *tmp;
Alex Elderde71a292012-07-03 16:01:19 -05004138 int rbd_id = rbd_dev->dev_id;
Alex Elderd184f6b2012-01-29 13:57:44 -06004139 int max_id;
4140
Alex Elderaafb2302012-09-06 16:00:54 -05004141 rbd_assert(rbd_id > 0);
Alex Elder499afd52012-02-02 08:13:29 -06004142
Alex Eldere2839302012-08-29 17:11:06 -05004143 dout("rbd_dev %p released dev id %llu\n", rbd_dev,
4144 (unsigned long long) rbd_dev->dev_id);
Alex Elder499afd52012-02-02 08:13:29 -06004145 spin_lock(&rbd_dev_list_lock);
4146 list_del_init(&rbd_dev->node);
Alex Elderd184f6b2012-01-29 13:57:44 -06004147
4148 /*
4149 * If the id being "put" is not the current maximum, there
4150 * is nothing special we need to do.
4151 */
Alex Eldere2839302012-08-29 17:11:06 -05004152 if (rbd_id != atomic64_read(&rbd_dev_id_max)) {
Alex Elderd184f6b2012-01-29 13:57:44 -06004153 spin_unlock(&rbd_dev_list_lock);
4154 return;
4155 }
4156
4157 /*
4158 * We need to update the current maximum id. Search the
4159 * list to find out what it is. We're more likely to find
4160 * the maximum at the end, so search the list backward.
4161 */
4162 max_id = 0;
4163 list_for_each_prev(tmp, &rbd_dev_list) {
4164 struct rbd_device *rbd_dev;
4165
4166 rbd_dev = list_entry(tmp, struct rbd_device, node);
Alex Elderb213e0b2012-10-10 21:19:13 -07004167 if (rbd_dev->dev_id > max_id)
4168 max_id = rbd_dev->dev_id;
Alex Elderd184f6b2012-01-29 13:57:44 -06004169 }
Alex Elder499afd52012-02-02 08:13:29 -06004170 spin_unlock(&rbd_dev_list_lock);
Alex Elderb7f23c32012-01-29 13:57:43 -06004171
Alex Elder1ddbe942012-01-29 13:57:44 -06004172 /*
Alex Eldere2839302012-08-29 17:11:06 -05004173 * The max id could have been updated by rbd_dev_id_get(), in
Alex Elderd184f6b2012-01-29 13:57:44 -06004174 * which case it now accurately reflects the new maximum.
4175 * Be careful not to overwrite the maximum value in that
4176 * case.
Alex Elder1ddbe942012-01-29 13:57:44 -06004177 */
Alex Eldere2839302012-08-29 17:11:06 -05004178 atomic64_cmpxchg(&rbd_dev_id_max, rbd_id, max_id);
4179 dout(" max dev id has been reset\n");
Alex Elderb7f23c32012-01-29 13:57:43 -06004180}
4181
Alex Eldera725f65e2012-02-02 08:13:30 -06004182/*
Alex Eldere28fff262012-02-02 08:13:30 -06004183 * Skips over white space at *buf, and updates *buf to point to the
4184 * first found non-space character (if any). Returns the length of
Alex Elder593a9e72012-02-07 12:03:37 -06004185 * the token (string of non-white space characters) found. Note
4186 * that *buf must be terminated with '\0'.
Alex Eldere28fff262012-02-02 08:13:30 -06004187 */
4188static inline size_t next_token(const char **buf)
4189{
4190 /*
4191 * These are the characters that produce nonzero for
4192 * isspace() in the "C" and "POSIX" locales.
4193 */
4194 const char *spaces = " \f\n\r\t\v";
4195
4196 *buf += strspn(*buf, spaces); /* Find start of token */
4197
4198 return strcspn(*buf, spaces); /* Return token length */
4199}
4200
4201/*
4202 * Finds the next token in *buf, and if the provided token buffer is
4203 * big enough, copies the found token into it. The result, if
Alex Elder593a9e72012-02-07 12:03:37 -06004204 * copied, is guaranteed to be terminated with '\0'. Note that *buf
4205 * must be terminated with '\0' on entry.
Alex Eldere28fff262012-02-02 08:13:30 -06004206 *
4207 * Returns the length of the token found (not including the '\0').
4208 * Return value will be 0 if no token is found, and it will be >=
4209 * token_size if the token would not fit.
4210 *
Alex Elder593a9e72012-02-07 12:03:37 -06004211 * The *buf pointer will be updated to point beyond the end of the
Alex Eldere28fff262012-02-02 08:13:30 -06004212 * found token. Note that this occurs even if the token buffer is
4213 * too small to hold it.
4214 */
4215static inline size_t copy_token(const char **buf,
4216 char *token,
4217 size_t token_size)
4218{
4219 size_t len;
4220
4221 len = next_token(buf);
4222 if (len < token_size) {
4223 memcpy(token, *buf, len);
4224 *(token + len) = '\0';
4225 }
4226 *buf += len;
4227
4228 return len;
4229}
4230
4231/*
Alex Elderea3352f2012-07-09 21:04:23 -05004232 * Finds the next token in *buf, dynamically allocates a buffer big
4233 * enough to hold a copy of it, and copies the token into the new
4234 * buffer. The copy is guaranteed to be terminated with '\0'. Note
4235 * that a duplicate buffer is created even for a zero-length token.
4236 *
4237 * Returns a pointer to the newly-allocated duplicate, or a null
4238 * pointer if memory for the duplicate was not available. If
4239 * the lenp argument is a non-null pointer, the length of the token
4240 * (not including the '\0') is returned in *lenp.
4241 *
4242 * If successful, the *buf pointer will be updated to point beyond
4243 * the end of the found token.
4244 *
4245 * Note: uses GFP_KERNEL for allocation.
4246 */
4247static inline char *dup_token(const char **buf, size_t *lenp)
4248{
4249 char *dup;
4250 size_t len;
4251
4252 len = next_token(buf);
Alex Elder4caf35f2012-11-01 08:39:27 -05004253 dup = kmemdup(*buf, len + 1, GFP_KERNEL);
Alex Elderea3352f2012-07-09 21:04:23 -05004254 if (!dup)
4255 return NULL;
Alex Elderea3352f2012-07-09 21:04:23 -05004256 *(dup + len) = '\0';
4257 *buf += len;
4258
4259 if (lenp)
4260 *lenp = len;
4261
4262 return dup;
4263}
4264
4265/*
Alex Elder859c31d2012-10-25 23:34:42 -05004266 * Parse the options provided for an "rbd add" (i.e., rbd image
4267 * mapping) request. These arrive via a write to /sys/bus/rbd/add,
4268 * and the data written is passed here via a NUL-terminated buffer.
4269 * Returns 0 if successful or an error code otherwise.
Alex Elderd22f76e2012-07-12 10:46:35 -05004270 *
Alex Elder859c31d2012-10-25 23:34:42 -05004271 * The information extracted from these options is recorded in
4272 * the other parameters which return dynamically-allocated
4273 * structures:
4274 * ceph_opts
4275 * The address of a pointer that will refer to a ceph options
4276 * structure. Caller must release the returned pointer using
4277 * ceph_destroy_options() when it is no longer needed.
4278 * rbd_opts
4279 * Address of an rbd options pointer. Fully initialized by
4280 * this function; caller must release with kfree().
4281 * spec
4282 * Address of an rbd image specification pointer. Fully
4283 * initialized by this function based on parsed options.
4284 * Caller must release with rbd_spec_put().
4285 *
4286 * The options passed take this form:
4287 * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
4288 * where:
4289 * <mon_addrs>
4290 * A comma-separated list of one or more monitor addresses.
4291 * A monitor address is an ip address, optionally followed
4292 * by a port number (separated by a colon).
4293 * I.e.: ip1[:port1][,ip2[:port2]...]
4294 * <options>
4295 * A comma-separated list of ceph and/or rbd options.
4296 * <pool_name>
4297 * The name of the rados pool containing the rbd image.
4298 * <image_name>
4299 * The name of the image in that pool to map.
4300 * <snap_id>
4301 * An optional snapshot id. If provided, the mapping will
4302 * present data from the image at the time that snapshot was
4303 * created. The image head is used if no snapshot id is
4304 * provided. Snapshot mappings are always read-only.
Alex Eldera725f65e2012-02-02 08:13:30 -06004305 */
Alex Elder859c31d2012-10-25 23:34:42 -05004306static int rbd_add_parse_args(const char *buf,
Alex Elderdc79b112012-10-25 23:34:41 -05004307 struct ceph_options **ceph_opts,
Alex Elder859c31d2012-10-25 23:34:42 -05004308 struct rbd_options **opts,
4309 struct rbd_spec **rbd_spec)
Alex Eldera725f65e2012-02-02 08:13:30 -06004310{
Alex Elderd22f76e2012-07-12 10:46:35 -05004311 size_t len;
Alex Elder859c31d2012-10-25 23:34:42 -05004312 char *options;
Alex Elder0ddebc02012-10-25 23:34:41 -05004313 const char *mon_addrs;
Alex Elderecb4dc222013-04-26 09:43:47 -05004314 char *snap_name;
Alex Elder0ddebc02012-10-25 23:34:41 -05004315 size_t mon_addrs_size;
Alex Elder859c31d2012-10-25 23:34:42 -05004316 struct rbd_spec *spec = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05004317 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05004318 struct ceph_options *copts;
Alex Elderdc79b112012-10-25 23:34:41 -05004319 int ret;
Alex Eldere28fff262012-02-02 08:13:30 -06004320
4321 /* The first four tokens are required */
4322
Alex Elder7ef32142012-02-02 08:13:30 -06004323 len = next_token(&buf);
Alex Elder4fb5d6712012-11-01 10:17:15 -05004324 if (!len) {
4325 rbd_warn(NULL, "no monitor address(es) provided");
4326 return -EINVAL;
4327 }
Alex Elder0ddebc02012-10-25 23:34:41 -05004328 mon_addrs = buf;
Alex Elderf28e5652012-10-25 23:34:41 -05004329 mon_addrs_size = len + 1;
Alex Elder7ef32142012-02-02 08:13:30 -06004330 buf += len;
Alex Eldera725f65e2012-02-02 08:13:30 -06004331
Alex Elderdc79b112012-10-25 23:34:41 -05004332 ret = -EINVAL;
Alex Elderf28e5652012-10-25 23:34:41 -05004333 options = dup_token(&buf, NULL);
4334 if (!options)
Alex Elderdc79b112012-10-25 23:34:41 -05004335 return -ENOMEM;
Alex Elder4fb5d6712012-11-01 10:17:15 -05004336 if (!*options) {
4337 rbd_warn(NULL, "no options provided");
4338 goto out_err;
4339 }
Alex Eldera725f65e2012-02-02 08:13:30 -06004340
Alex Elder859c31d2012-10-25 23:34:42 -05004341 spec = rbd_spec_alloc();
4342 if (!spec)
Alex Elderf28e5652012-10-25 23:34:41 -05004343 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05004344
4345 spec->pool_name = dup_token(&buf, NULL);
4346 if (!spec->pool_name)
4347 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05004348 if (!*spec->pool_name) {
4349 rbd_warn(NULL, "no pool name provided");
4350 goto out_err;
4351 }
Alex Eldere28fff262012-02-02 08:13:30 -06004352
Alex Elder69e7a022012-11-01 08:39:26 -05004353 spec->image_name = dup_token(&buf, NULL);
Alex Elder859c31d2012-10-25 23:34:42 -05004354 if (!spec->image_name)
Alex Elderf28e5652012-10-25 23:34:41 -05004355 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05004356 if (!*spec->image_name) {
4357 rbd_warn(NULL, "no image name provided");
4358 goto out_err;
4359 }
Alex Eldere28fff262012-02-02 08:13:30 -06004360
Alex Elderf28e5652012-10-25 23:34:41 -05004361 /*
4362 * Snapshot name is optional; default is to use "-"
4363 * (indicating the head/no snapshot).
4364 */
Alex Elder3feeb8942012-08-31 17:29:52 -05004365 len = next_token(&buf);
Alex Elder820a5f32012-07-09 21:04:24 -05004366 if (!len) {
Alex Elder3feeb8942012-08-31 17:29:52 -05004367 buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
4368 len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
Alex Elderf28e5652012-10-25 23:34:41 -05004369 } else if (len > RBD_MAX_SNAP_NAME_LEN) {
Alex Elderdc79b112012-10-25 23:34:41 -05004370 ret = -ENAMETOOLONG;
Alex Elderf28e5652012-10-25 23:34:41 -05004371 goto out_err;
Alex Elder849b4262012-07-09 21:04:24 -05004372 }
Alex Elderecb4dc222013-04-26 09:43:47 -05004373 snap_name = kmemdup(buf, len + 1, GFP_KERNEL);
4374 if (!snap_name)
Alex Elderf28e5652012-10-25 23:34:41 -05004375 goto out_mem;
Alex Elderecb4dc222013-04-26 09:43:47 -05004376 *(snap_name + len) = '\0';
4377 spec->snap_name = snap_name;
Alex Eldere5c35532012-10-25 23:34:41 -05004378
Alex Elder0ddebc02012-10-25 23:34:41 -05004379 /* Initialize all rbd options to the defaults */
Alex Eldere28fff262012-02-02 08:13:30 -06004380
Alex Elder4e9afeb2012-10-25 23:34:41 -05004381 rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL);
4382 if (!rbd_opts)
4383 goto out_mem;
4384
4385 rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
Alex Elderd22f76e2012-07-12 10:46:35 -05004386
Alex Elder859c31d2012-10-25 23:34:42 -05004387 copts = ceph_parse_options(options, mon_addrs,
Alex Elder0ddebc02012-10-25 23:34:41 -05004388 mon_addrs + mon_addrs_size - 1,
Alex Elder4e9afeb2012-10-25 23:34:41 -05004389 parse_rbd_opts_token, rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05004390 if (IS_ERR(copts)) {
4391 ret = PTR_ERR(copts);
Alex Elderdc79b112012-10-25 23:34:41 -05004392 goto out_err;
4393 }
Alex Elder859c31d2012-10-25 23:34:42 -05004394 kfree(options);
4395
4396 *ceph_opts = copts;
Alex Elder4e9afeb2012-10-25 23:34:41 -05004397 *opts = rbd_opts;
Alex Elder859c31d2012-10-25 23:34:42 -05004398 *rbd_spec = spec;
Alex Elder0ddebc02012-10-25 23:34:41 -05004399
Alex Elderdc79b112012-10-25 23:34:41 -05004400 return 0;
Alex Elderf28e5652012-10-25 23:34:41 -05004401out_mem:
Alex Elderdc79b112012-10-25 23:34:41 -05004402 ret = -ENOMEM;
Alex Elderd22f76e2012-07-12 10:46:35 -05004403out_err:
Alex Elder859c31d2012-10-25 23:34:42 -05004404 kfree(rbd_opts);
4405 rbd_spec_put(spec);
Alex Elderf28e5652012-10-25 23:34:41 -05004406 kfree(options);
Alex Elderd22f76e2012-07-12 10:46:35 -05004407
Alex Elderdc79b112012-10-25 23:34:41 -05004408 return ret;
Alex Eldera725f65e2012-02-02 08:13:30 -06004409}
4410
Alex Elder589d30e2012-07-10 20:30:11 -05004411/*
4412 * An rbd format 2 image has a unique identifier, distinct from the
4413 * name given to it by the user. Internally, that identifier is
4414 * what's used to specify the names of objects related to the image.
4415 *
4416 * A special "rbd id" object is used to map an rbd image name to its
4417 * id. If that object doesn't exist, then there is no v2 rbd image
4418 * with the supplied name.
4419 *
4420 * This function will record the given rbd_dev's image_id field if
4421 * it can be determined, and in that case will return 0. If any
4422 * errors occur a negative errno will be returned and the rbd_dev's
4423 * image_id field will be unchanged (and should be NULL).
4424 */
4425static int rbd_dev_image_id(struct rbd_device *rbd_dev)
4426{
4427 int ret;
4428 size_t size;
4429 char *object_name;
4430 void *response;
Alex Elderc0fba362013-04-25 23:15:08 -05004431 char *image_id;
Alex Elder2f82ee52012-10-30 19:40:33 -05004432
Alex Elder589d30e2012-07-10 20:30:11 -05004433 /*
Alex Elder2c0d0a12012-10-30 19:40:33 -05004434 * When probing a parent image, the image id is already
4435 * known (and the image name likely is not). There's no
Alex Elderc0fba362013-04-25 23:15:08 -05004436 * need to fetch the image id again in this case. We
4437 * do still need to set the image format though.
Alex Elder2c0d0a12012-10-30 19:40:33 -05004438 */
Alex Elderc0fba362013-04-25 23:15:08 -05004439 if (rbd_dev->spec->image_id) {
4440 rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1;
4441
Alex Elder2c0d0a12012-10-30 19:40:33 -05004442 return 0;
Alex Elderc0fba362013-04-25 23:15:08 -05004443 }
Alex Elder2c0d0a12012-10-30 19:40:33 -05004444
4445 /*
Alex Elder589d30e2012-07-10 20:30:11 -05004446 * First, see if the format 2 image id file exists, and if
4447 * so, get the image's persistent id from it.
4448 */
Alex Elder69e7a022012-11-01 08:39:26 -05004449 size = sizeof (RBD_ID_PREFIX) + strlen(rbd_dev->spec->image_name);
Alex Elder589d30e2012-07-10 20:30:11 -05004450 object_name = kmalloc(size, GFP_NOIO);
4451 if (!object_name)
4452 return -ENOMEM;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004453 sprintf(object_name, "%s%s", RBD_ID_PREFIX, rbd_dev->spec->image_name);
Alex Elder589d30e2012-07-10 20:30:11 -05004454 dout("rbd id object name is %s\n", object_name);
4455
4456 /* Response will be an encoded string, which includes a length */
4457
4458 size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
4459 response = kzalloc(size, GFP_NOIO);
4460 if (!response) {
4461 ret = -ENOMEM;
4462 goto out;
4463 }
4464
Alex Elderc0fba362013-04-25 23:15:08 -05004465 /* If it doesn't exist we'll assume it's a format 1 image */
4466
Alex Elder36be9a72013-01-19 00:30:28 -06004467 ret = rbd_obj_method_sync(rbd_dev, object_name,
Alex Elder41579762013-04-21 12:14:45 -05004468 "rbd", "get_id", NULL, 0,
Alex Eldere2a58ee2013-04-30 00:44:33 -05004469 response, RBD_IMAGE_ID_LEN_MAX);
Alex Elder36be9a72013-01-19 00:30:28 -06004470 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderc0fba362013-04-25 23:15:08 -05004471 if (ret == -ENOENT) {
4472 image_id = kstrdup("", GFP_KERNEL);
4473 ret = image_id ? 0 : -ENOMEM;
4474 if (!ret)
4475 rbd_dev->image_format = 1;
4476 } else if (ret > sizeof (__le32)) {
4477 void *p = response;
Alex Elder589d30e2012-07-10 20:30:11 -05004478
Alex Elderc0fba362013-04-25 23:15:08 -05004479 image_id = ceph_extract_encoded_string(&p, p + ret,
Alex Elder979ed482012-11-01 08:39:26 -05004480 NULL, GFP_NOIO);
Alex Elderc0fba362013-04-25 23:15:08 -05004481 ret = IS_ERR(image_id) ? PTR_ERR(image_id) : 0;
4482 if (!ret)
4483 rbd_dev->image_format = 2;
Alex Elder589d30e2012-07-10 20:30:11 -05004484 } else {
Alex Elderc0fba362013-04-25 23:15:08 -05004485 ret = -EINVAL;
4486 }
4487
4488 if (!ret) {
4489 rbd_dev->spec->image_id = image_id;
4490 dout("image_id is %s\n", image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05004491 }
4492out:
4493 kfree(response);
4494 kfree(object_name);
4495
4496 return ret;
4497}
4498
Alex Elder6fd48b32013-04-28 23:32:34 -05004499/* Undo whatever state changes are made by v1 or v2 image probe */
4500
4501static void rbd_dev_unprobe(struct rbd_device *rbd_dev)
4502{
4503 struct rbd_image_header *header;
4504
Alex Elderfb65d2282013-05-08 22:50:04 -05004505 rbd_dev_unparent(rbd_dev);
Alex Elder6fd48b32013-04-28 23:32:34 -05004506
4507 /* Free dynamic fields from the header, then zero it out */
4508
4509 header = &rbd_dev->header;
Alex Elder812164f82013-04-30 00:44:32 -05004510 ceph_put_snap_context(header->snapc);
Alex Elder6fd48b32013-04-28 23:32:34 -05004511 kfree(header->snap_sizes);
4512 kfree(header->snap_names);
4513 kfree(header->object_prefix);
4514 memset(header, 0, sizeof (*header));
4515}
4516
Alex Elder2df3fac2013-05-06 09:51:30 -05004517static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev)
Alex Eldera30b71b2012-07-10 20:30:11 -05004518{
Alex Elder9d475de2012-07-03 16:01:19 -05004519 int ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05004520
Alex Elder1e130192012-07-03 16:01:19 -05004521 ret = rbd_dev_v2_object_prefix(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05004522 if (ret)
Alex Elder1e130192012-07-03 16:01:19 -05004523 goto out_err;
Alex Elderb1b54022012-07-03 16:01:19 -05004524
Alex Elder2df3fac2013-05-06 09:51:30 -05004525 /*
4526 * Get the and check features for the image. Currently the
4527 * features are assumed to never change.
4528 */
Alex Elderb1b54022012-07-03 16:01:19 -05004529 ret = rbd_dev_v2_features(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05004530 if (ret)
Alex Elderb1b54022012-07-03 16:01:19 -05004531 goto out_err;
Alex Elder35d489f2012-07-03 16:01:19 -05004532
Alex Eldercc070d52013-04-21 12:14:45 -05004533 /* If the image supports fancy striping, get its parameters */
4534
4535 if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) {
4536 ret = rbd_dev_v2_striping_info(rbd_dev);
4537 if (ret < 0)
4538 goto out_err;
4539 }
Alex Elder2df3fac2013-05-06 09:51:30 -05004540 /* No support for crypto and compression type format 2 images */
Alex Elder6e14b1a2012-07-03 16:01:19 -05004541
Alex Elder35152972012-08-31 17:29:55 -05004542 return 0;
Alex Elder9d475de2012-07-03 16:01:19 -05004543out_err:
Alex Elder642a2532013-05-06 17:40:33 -05004544 rbd_dev->header.features = 0;
Alex Elder1e130192012-07-03 16:01:19 -05004545 kfree(rbd_dev->header.object_prefix);
4546 rbd_dev->header.object_prefix = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05004547
4548 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05004549}
4550
Alex Elder124afba2013-04-26 15:44:36 -05004551static int rbd_dev_probe_parent(struct rbd_device *rbd_dev)
Alex Elder83a06262012-10-30 15:47:17 -05004552{
Alex Elder2f82ee52012-10-30 19:40:33 -05004553 struct rbd_device *parent = NULL;
Alex Elder124afba2013-04-26 15:44:36 -05004554 struct rbd_spec *parent_spec;
4555 struct rbd_client *rbdc;
4556 int ret;
4557
4558 if (!rbd_dev->parent_spec)
4559 return 0;
4560 /*
4561 * We need to pass a reference to the client and the parent
4562 * spec when creating the parent rbd_dev. Images related by
4563 * parent/child relationships always share both.
4564 */
4565 parent_spec = rbd_spec_get(rbd_dev->parent_spec);
4566 rbdc = __rbd_get_client(rbd_dev->rbd_client);
4567
4568 ret = -ENOMEM;
4569 parent = rbd_dev_create(rbdc, parent_spec);
4570 if (!parent)
4571 goto out_err;
4572
Alex Elder1f3ef782013-05-06 17:40:33 -05004573 ret = rbd_dev_image_probe(parent, false);
Alex Elder124afba2013-04-26 15:44:36 -05004574 if (ret < 0)
4575 goto out_err;
4576 rbd_dev->parent = parent;
4577
4578 return 0;
4579out_err:
4580 if (parent) {
Alex Elderfb65d2282013-05-08 22:50:04 -05004581 rbd_dev_unparent(rbd_dev);
Alex Elder124afba2013-04-26 15:44:36 -05004582 kfree(rbd_dev->header_name);
4583 rbd_dev_destroy(parent);
4584 } else {
4585 rbd_put_client(rbdc);
4586 rbd_spec_put(parent_spec);
4587 }
4588
4589 return ret;
4590}
4591
Alex Elder200a6a82013-04-28 23:32:34 -05004592static int rbd_dev_device_setup(struct rbd_device *rbd_dev)
Alex Elder124afba2013-04-26 15:44:36 -05004593{
Alex Elder83a06262012-10-30 15:47:17 -05004594 int ret;
Alex Elder83a06262012-10-30 15:47:17 -05004595
Alex Elder83a06262012-10-30 15:47:17 -05004596 /* generate unique id: find highest unique id, add one */
4597 rbd_dev_id_get(rbd_dev);
4598
4599 /* Fill in the device name, now that we have its id. */
4600 BUILD_BUG_ON(DEV_NAME_LEN
4601 < sizeof (RBD_DRV_NAME) + MAX_INT_FORMAT_WIDTH);
4602 sprintf(rbd_dev->name, "%s%d", RBD_DRV_NAME, rbd_dev->dev_id);
4603
4604 /* Get our block major device number. */
4605
4606 ret = register_blkdev(0, rbd_dev->name);
4607 if (ret < 0)
4608 goto err_out_id;
4609 rbd_dev->major = ret;
4610
4611 /* Set up the blkdev mapping. */
4612
4613 ret = rbd_init_disk(rbd_dev);
4614 if (ret)
4615 goto err_out_blkdev;
4616
Alex Elderf35a4de2013-05-06 09:51:29 -05004617 ret = rbd_dev_mapping_set(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05004618 if (ret)
4619 goto err_out_disk;
Alex Elderf35a4de2013-05-06 09:51:29 -05004620 set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
4621
4622 ret = rbd_bus_add_dev(rbd_dev);
4623 if (ret)
4624 goto err_out_mapping;
Alex Elder83a06262012-10-30 15:47:17 -05004625
Alex Elder83a06262012-10-30 15:47:17 -05004626 /* Everything's ready. Announce the disk to the world. */
4627
Alex Elder129b79d2013-04-26 15:44:36 -05004628 set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
Alex Elder83a06262012-10-30 15:47:17 -05004629 add_disk(rbd_dev->disk);
4630
4631 pr_info("%s: added with size 0x%llx\n", rbd_dev->disk->disk_name,
4632 (unsigned long long) rbd_dev->mapping.size);
4633
4634 return ret;
Alex Elder2f82ee52012-10-30 19:40:33 -05004635
Alex Elderf35a4de2013-05-06 09:51:29 -05004636err_out_mapping:
4637 rbd_dev_mapping_clear(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05004638err_out_disk:
4639 rbd_free_disk(rbd_dev);
4640err_out_blkdev:
4641 unregister_blkdev(rbd_dev->major, rbd_dev->name);
4642err_out_id:
4643 rbd_dev_id_put(rbd_dev);
Alex Elderd1cf5782013-04-27 09:59:30 -05004644 rbd_dev_mapping_clear(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05004645
4646 return ret;
4647}
4648
Alex Elder332bb122013-04-27 09:59:30 -05004649static int rbd_dev_header_name(struct rbd_device *rbd_dev)
4650{
4651 struct rbd_spec *spec = rbd_dev->spec;
4652 size_t size;
4653
4654 /* Record the header object name for this rbd image. */
4655
4656 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
4657
4658 if (rbd_dev->image_format == 1)
4659 size = strlen(spec->image_name) + sizeof (RBD_SUFFIX);
4660 else
4661 size = sizeof (RBD_HEADER_PREFIX) + strlen(spec->image_id);
4662
4663 rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
4664 if (!rbd_dev->header_name)
4665 return -ENOMEM;
4666
4667 if (rbd_dev->image_format == 1)
4668 sprintf(rbd_dev->header_name, "%s%s",
4669 spec->image_name, RBD_SUFFIX);
4670 else
4671 sprintf(rbd_dev->header_name, "%s%s",
4672 RBD_HEADER_PREFIX, spec->image_id);
4673 return 0;
4674}
4675
Alex Elder200a6a82013-04-28 23:32:34 -05004676static void rbd_dev_image_release(struct rbd_device *rbd_dev)
4677{
Alex Elder6fd48b32013-04-28 23:32:34 -05004678 rbd_dev_unprobe(rbd_dev);
Alex Elder200a6a82013-04-28 23:32:34 -05004679 kfree(rbd_dev->header_name);
Alex Elder6fd48b32013-04-28 23:32:34 -05004680 rbd_dev->header_name = NULL;
4681 rbd_dev->image_format = 0;
4682 kfree(rbd_dev->spec->image_id);
4683 rbd_dev->spec->image_id = NULL;
4684
Alex Elder200a6a82013-04-28 23:32:34 -05004685 rbd_dev_destroy(rbd_dev);
4686}
4687
Alex Eldera30b71b2012-07-10 20:30:11 -05004688/*
4689 * Probe for the existence of the header object for the given rbd
Alex Elder1f3ef782013-05-06 17:40:33 -05004690 * device. If this image is the one being mapped (i.e., not a
4691 * parent), initiate a watch on its header object before using that
4692 * object to get detailed information about the rbd image.
Alex Eldera30b71b2012-07-10 20:30:11 -05004693 */
Alex Elder1f3ef782013-05-06 17:40:33 -05004694static int rbd_dev_image_probe(struct rbd_device *rbd_dev, bool mapping)
Alex Eldera30b71b2012-07-10 20:30:11 -05004695{
4696 int ret;
Alex Elderb644de22013-04-27 09:59:31 -05004697 int tmp;
Alex Eldera30b71b2012-07-10 20:30:11 -05004698
4699 /*
4700 * Get the id from the image id object. If it's not a
4701 * format 2 image, we'll get ENOENT back, and we'll assume
4702 * it's a format 1 image.
4703 */
4704 ret = rbd_dev_image_id(rbd_dev);
4705 if (ret)
Alex Elderc0fba362013-04-25 23:15:08 -05004706 return ret;
4707 rbd_assert(rbd_dev->spec->image_id);
4708 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
4709
Alex Elder332bb122013-04-27 09:59:30 -05004710 ret = rbd_dev_header_name(rbd_dev);
4711 if (ret)
4712 goto err_out_format;
4713
Alex Elder1f3ef782013-05-06 17:40:33 -05004714 if (mapping) {
4715 ret = rbd_dev_header_watch_sync(rbd_dev, true);
4716 if (ret)
4717 goto out_header_name;
4718 }
Alex Elderb644de22013-04-27 09:59:31 -05004719
Alex Elderc0fba362013-04-25 23:15:08 -05004720 if (rbd_dev->image_format == 1)
Alex Elder99a41eb2013-05-06 09:51:30 -05004721 ret = rbd_dev_v1_header_info(rbd_dev);
Alex Eldera30b71b2012-07-10 20:30:11 -05004722 else
Alex Elder2df3fac2013-05-06 09:51:30 -05004723 ret = rbd_dev_v2_header_info(rbd_dev);
Alex Elder5655c4d2013-04-25 23:15:08 -05004724 if (ret)
Alex Elderb644de22013-04-27 09:59:31 -05004725 goto err_out_watch;
Alex Elder83a06262012-10-30 15:47:17 -05004726
Alex Elder9bb81c92013-04-27 09:59:30 -05004727 ret = rbd_dev_spec_update(rbd_dev);
4728 if (ret)
Alex Elder33dca392013-04-30 00:44:33 -05004729 goto err_out_probe;
Alex Elder9bb81c92013-04-27 09:59:30 -05004730
4731 ret = rbd_dev_probe_parent(rbd_dev);
Alex Elder30d60ba2013-05-06 09:51:30 -05004732 if (ret)
4733 goto err_out_probe;
Alex Elder83a06262012-10-30 15:47:17 -05004734
Alex Elder30d60ba2013-05-06 09:51:30 -05004735 dout("discovered format %u image, header name is %s\n",
4736 rbd_dev->image_format, rbd_dev->header_name);
4737
4738 return 0;
Alex Elder6fd48b32013-04-28 23:32:34 -05004739err_out_probe:
4740 rbd_dev_unprobe(rbd_dev);
Alex Elderb644de22013-04-27 09:59:31 -05004741err_out_watch:
Alex Elder1f3ef782013-05-06 17:40:33 -05004742 if (mapping) {
4743 tmp = rbd_dev_header_watch_sync(rbd_dev, false);
4744 if (tmp)
4745 rbd_warn(rbd_dev, "unable to tear down "
4746 "watch request (%d)\n", tmp);
4747 }
Alex Elder332bb122013-04-27 09:59:30 -05004748out_header_name:
4749 kfree(rbd_dev->header_name);
4750 rbd_dev->header_name = NULL;
4751err_out_format:
4752 rbd_dev->image_format = 0;
Alex Elder5655c4d2013-04-25 23:15:08 -05004753 kfree(rbd_dev->spec->image_id);
4754 rbd_dev->spec->image_id = NULL;
4755
4756 dout("probe failed, returning %d\n", ret);
4757
4758 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05004759}
4760
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07004761static ssize_t rbd_add(struct bus_type *bus,
4762 const char *buf,
4763 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004764{
Alex Eldercb8627c2012-07-09 21:04:23 -05004765 struct rbd_device *rbd_dev = NULL;
Alex Elderdc79b112012-10-25 23:34:41 -05004766 struct ceph_options *ceph_opts = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05004767 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05004768 struct rbd_spec *spec = NULL;
Alex Elder9d3997f2012-10-25 23:34:42 -05004769 struct rbd_client *rbdc;
Alex Elder27cc2592012-02-02 08:13:30 -06004770 struct ceph_osd_client *osdc;
Alex Elder51344a32013-05-06 07:40:30 -05004771 bool read_only;
Alex Elder27cc2592012-02-02 08:13:30 -06004772 int rc = -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004773
4774 if (!try_module_get(THIS_MODULE))
4775 return -ENODEV;
4776
Alex Eldera725f65e2012-02-02 08:13:30 -06004777 /* parse add command */
Alex Elder859c31d2012-10-25 23:34:42 -05004778 rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec);
Alex Elderdc79b112012-10-25 23:34:41 -05004779 if (rc < 0)
Alex Elderbd4ba652012-10-25 23:34:42 -05004780 goto err_out_module;
Alex Elder51344a32013-05-06 07:40:30 -05004781 read_only = rbd_opts->read_only;
4782 kfree(rbd_opts);
4783 rbd_opts = NULL; /* done with this */
Alex Eldera725f65e2012-02-02 08:13:30 -06004784
Alex Elder9d3997f2012-10-25 23:34:42 -05004785 rbdc = rbd_get_client(ceph_opts);
4786 if (IS_ERR(rbdc)) {
4787 rc = PTR_ERR(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05004788 goto err_out_args;
Alex Elder9d3997f2012-10-25 23:34:42 -05004789 }
Alex Elderc53d5892012-10-25 23:34:42 -05004790 ceph_opts = NULL; /* rbd_dev client now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004791
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004792 /* pick the pool */
Alex Elder9d3997f2012-10-25 23:34:42 -05004793 osdc = &rbdc->client->osdc;
Alex Elder859c31d2012-10-25 23:34:42 -05004794 rc = ceph_pg_poolid_by_name(osdc->osdmap, spec->pool_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004795 if (rc < 0)
4796 goto err_out_client;
Alex Elderc0cd10db2013-04-26 09:43:47 -05004797 spec->pool_id = (u64)rc;
Alex Elder859c31d2012-10-25 23:34:42 -05004798
Alex Elder0903e872012-11-14 12:25:19 -06004799 /* The ceph file layout needs to fit pool id in 32 bits */
4800
Alex Elderc0cd10db2013-04-26 09:43:47 -05004801 if (spec->pool_id > (u64)U32_MAX) {
4802 rbd_warn(NULL, "pool id too large (%llu > %u)\n",
4803 (unsigned long long)spec->pool_id, U32_MAX);
Alex Elder0903e872012-11-14 12:25:19 -06004804 rc = -EIO;
4805 goto err_out_client;
4806 }
4807
Alex Elderc53d5892012-10-25 23:34:42 -05004808 rbd_dev = rbd_dev_create(rbdc, spec);
Alex Elderbd4ba652012-10-25 23:34:42 -05004809 if (!rbd_dev)
4810 goto err_out_client;
Alex Elderc53d5892012-10-25 23:34:42 -05004811 rbdc = NULL; /* rbd_dev now owns this */
4812 spec = NULL; /* rbd_dev now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004813
Alex Elder1f3ef782013-05-06 17:40:33 -05004814 rc = rbd_dev_image_probe(rbd_dev, true);
Alex Eldera30b71b2012-07-10 20:30:11 -05004815 if (rc < 0)
Alex Elderc53d5892012-10-25 23:34:42 -05004816 goto err_out_rbd_dev;
Alex Elder05fd6f62012-08-29 17:11:07 -05004817
Alex Elder7ce4eef2013-05-06 17:40:33 -05004818 /* If we are mapping a snapshot it must be marked read-only */
4819
4820 if (rbd_dev->spec->snap_id != CEPH_NOSNAP)
4821 read_only = true;
4822 rbd_dev->mapping.read_only = read_only;
4823
Alex Elderb536f692013-04-28 23:32:34 -05004824 rc = rbd_dev_device_setup(rbd_dev);
4825 if (!rc)
4826 return count;
4827
4828 rbd_dev_image_release(rbd_dev);
Alex Elderc53d5892012-10-25 23:34:42 -05004829err_out_rbd_dev:
4830 rbd_dev_destroy(rbd_dev);
Alex Elderbd4ba652012-10-25 23:34:42 -05004831err_out_client:
Alex Elder9d3997f2012-10-25 23:34:42 -05004832 rbd_put_client(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05004833err_out_args:
Alex Elder78cea762012-10-25 23:34:41 -05004834 if (ceph_opts)
4835 ceph_destroy_options(ceph_opts);
Alex Elder4e9afeb2012-10-25 23:34:41 -05004836 kfree(rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05004837 rbd_spec_put(spec);
Alex Elderbd4ba652012-10-25 23:34:42 -05004838err_out_module:
4839 module_put(THIS_MODULE);
Alex Elder27cc2592012-02-02 08:13:30 -06004840
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004841 dout("Error adding device %s\n", buf);
Alex Elder27cc2592012-02-02 08:13:30 -06004842
Alex Elderc0cd10db2013-04-26 09:43:47 -05004843 return (ssize_t)rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004844}
4845
Alex Elderde71a292012-07-03 16:01:19 -05004846static struct rbd_device *__rbd_get_dev(unsigned long dev_id)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004847{
4848 struct list_head *tmp;
4849 struct rbd_device *rbd_dev;
4850
Alex Eldere124a82f2012-01-29 13:57:44 -06004851 spin_lock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004852 list_for_each(tmp, &rbd_dev_list) {
4853 rbd_dev = list_entry(tmp, struct rbd_device, node);
Alex Elderde71a292012-07-03 16:01:19 -05004854 if (rbd_dev->dev_id == dev_id) {
Alex Eldere124a82f2012-01-29 13:57:44 -06004855 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004856 return rbd_dev;
Alex Eldere124a82f2012-01-29 13:57:44 -06004857 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004858 }
Alex Eldere124a82f2012-01-29 13:57:44 -06004859 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004860 return NULL;
4861}
4862
Alex Elder200a6a82013-04-28 23:32:34 -05004863static void rbd_dev_device_release(struct device *dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004864{
Alex Elder593a9e72012-02-07 12:03:37 -06004865 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004866
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004867 rbd_free_disk(rbd_dev);
Alex Elder200a6a82013-04-28 23:32:34 -05004868 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
Alex Elder6d80b132013-05-06 07:40:30 -05004869 rbd_dev_mapping_clear(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004870 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Alex Elder200a6a82013-04-28 23:32:34 -05004871 rbd_dev->major = 0;
Alex Eldere2839302012-08-29 17:11:06 -05004872 rbd_dev_id_put(rbd_dev);
Alex Elderd1cf5782013-04-27 09:59:30 -05004873 rbd_dev_mapping_clear(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004874}
4875
Alex Elder05a46af2013-04-26 15:44:36 -05004876static void rbd_dev_remove_parent(struct rbd_device *rbd_dev)
4877{
Alex Elderad945fc2013-04-26 15:44:36 -05004878 while (rbd_dev->parent) {
Alex Elder05a46af2013-04-26 15:44:36 -05004879 struct rbd_device *first = rbd_dev;
4880 struct rbd_device *second = first->parent;
4881 struct rbd_device *third;
4882
4883 /*
4884 * Follow to the parent with no grandparent and
4885 * remove it.
4886 */
4887 while (second && (third = second->parent)) {
4888 first = second;
4889 second = third;
4890 }
Alex Elderad945fc2013-04-26 15:44:36 -05004891 rbd_assert(second);
Alex Elder8ad42cd2013-04-28 23:32:34 -05004892 rbd_dev_image_release(second);
Alex Elderad945fc2013-04-26 15:44:36 -05004893 first->parent = NULL;
4894 first->parent_overlap = 0;
4895
4896 rbd_assert(first->parent_spec);
Alex Elder05a46af2013-04-26 15:44:36 -05004897 rbd_spec_put(first->parent_spec);
4898 first->parent_spec = NULL;
Alex Elder05a46af2013-04-26 15:44:36 -05004899 }
4900}
4901
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004902static ssize_t rbd_remove(struct bus_type *bus,
4903 const char *buf,
4904 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004905{
4906 struct rbd_device *rbd_dev = NULL;
Alex Elder0d8189e2013-04-27 09:59:30 -05004907 int target_id;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004908 unsigned long ul;
Alex Elder0d8189e2013-04-27 09:59:30 -05004909 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004910
Alex Elder0d8189e2013-04-27 09:59:30 -05004911 ret = strict_strtoul(buf, 10, &ul);
4912 if (ret)
4913 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004914
4915 /* convert to int; abort if we lost anything in the conversion */
4916 target_id = (int) ul;
4917 if (target_id != ul)
4918 return -EINVAL;
4919
4920 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
4921
4922 rbd_dev = __rbd_get_dev(target_id);
4923 if (!rbd_dev) {
4924 ret = -ENOENT;
4925 goto done;
4926 }
4927
Alex Eldera14ea262013-02-05 13:23:12 -06004928 spin_lock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -06004929 if (rbd_dev->open_count)
Alex Elder42382b72012-11-16 09:29:16 -06004930 ret = -EBUSY;
Alex Elderb82d1672013-01-14 12:43:31 -06004931 else
4932 set_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags);
Alex Eldera14ea262013-02-05 13:23:12 -06004933 spin_unlock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -06004934 if (ret < 0)
Alex Elder42382b72012-11-16 09:29:16 -06004935 goto done;
Alex Elderb4808152013-04-26 15:44:36 -05004936 rbd_bus_del_dev(rbd_dev);
Alex Elder1f3ef782013-05-06 17:40:33 -05004937 ret = rbd_dev_header_watch_sync(rbd_dev, false);
4938 if (ret)
4939 rbd_warn(rbd_dev, "failed to cancel watch event (%d)\n", ret);
Alex Elder8ad42cd2013-04-28 23:32:34 -05004940 rbd_dev_image_release(rbd_dev);
Alex Elder79ab7552013-04-28 23:32:34 -05004941 module_put(THIS_MODULE);
Alex Elder1f3ef782013-05-06 17:40:33 -05004942 ret = count;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004943done:
4944 mutex_unlock(&ctl_mutex);
Alex Elderaafb2302012-09-06 16:00:54 -05004945
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004946 return ret;
4947}
4948
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004949/*
4950 * create control files in sysfs
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004951 * /sys/bus/rbd/...
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004952 */
4953static int rbd_sysfs_init(void)
4954{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004955 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004956
Alex Elderfed4c142012-02-07 12:03:36 -06004957 ret = device_register(&rbd_root_dev);
Alex Elder21079782012-01-24 10:08:36 -06004958 if (ret < 0)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004959 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004960
Alex Elderfed4c142012-02-07 12:03:36 -06004961 ret = bus_register(&rbd_bus_type);
4962 if (ret < 0)
4963 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004964
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004965 return ret;
4966}
4967
4968static void rbd_sysfs_cleanup(void)
4969{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004970 bus_unregister(&rbd_bus_type);
Alex Elderfed4c142012-02-07 12:03:36 -06004971 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004972}
4973
Alex Elder1c2a9df2013-05-01 12:43:03 -05004974static int rbd_slab_init(void)
4975{
4976 rbd_assert(!rbd_img_request_cache);
4977 rbd_img_request_cache = kmem_cache_create("rbd_img_request",
4978 sizeof (struct rbd_img_request),
4979 __alignof__(struct rbd_img_request),
4980 0, NULL);
Alex Elder868311b2013-05-01 12:43:03 -05004981 if (!rbd_img_request_cache)
4982 return -ENOMEM;
4983
4984 rbd_assert(!rbd_obj_request_cache);
4985 rbd_obj_request_cache = kmem_cache_create("rbd_obj_request",
4986 sizeof (struct rbd_obj_request),
4987 __alignof__(struct rbd_obj_request),
4988 0, NULL);
Alex Elder78c2a442013-05-01 12:43:04 -05004989 if (!rbd_obj_request_cache)
4990 goto out_err;
4991
4992 rbd_assert(!rbd_segment_name_cache);
4993 rbd_segment_name_cache = kmem_cache_create("rbd_segment_name",
4994 MAX_OBJ_NAME_SIZE + 1, 1, 0, NULL);
4995 if (rbd_segment_name_cache)
Alex Elder1c2a9df2013-05-01 12:43:03 -05004996 return 0;
Alex Elder78c2a442013-05-01 12:43:04 -05004997out_err:
4998 if (rbd_obj_request_cache) {
4999 kmem_cache_destroy(rbd_obj_request_cache);
5000 rbd_obj_request_cache = NULL;
5001 }
Alex Elder1c2a9df2013-05-01 12:43:03 -05005002
Alex Elder868311b2013-05-01 12:43:03 -05005003 kmem_cache_destroy(rbd_img_request_cache);
5004 rbd_img_request_cache = NULL;
5005
Alex Elder1c2a9df2013-05-01 12:43:03 -05005006 return -ENOMEM;
5007}
5008
5009static void rbd_slab_exit(void)
5010{
Alex Elder78c2a442013-05-01 12:43:04 -05005011 rbd_assert(rbd_segment_name_cache);
5012 kmem_cache_destroy(rbd_segment_name_cache);
5013 rbd_segment_name_cache = NULL;
5014
Alex Elder868311b2013-05-01 12:43:03 -05005015 rbd_assert(rbd_obj_request_cache);
5016 kmem_cache_destroy(rbd_obj_request_cache);
5017 rbd_obj_request_cache = NULL;
5018
Alex Elder1c2a9df2013-05-01 12:43:03 -05005019 rbd_assert(rbd_img_request_cache);
5020 kmem_cache_destroy(rbd_img_request_cache);
5021 rbd_img_request_cache = NULL;
5022}
5023
Alex Eldercc344fa2013-02-19 12:25:56 -06005024static int __init rbd_init(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005025{
5026 int rc;
5027
Alex Elder1e32d342013-01-30 11:13:33 -06005028 if (!libceph_compatible(NULL)) {
5029 rbd_warn(NULL, "libceph incompatibility (quitting)");
5030
5031 return -EINVAL;
5032 }
Alex Elder1c2a9df2013-05-01 12:43:03 -05005033 rc = rbd_slab_init();
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005034 if (rc)
5035 return rc;
Alex Elder1c2a9df2013-05-01 12:43:03 -05005036 rc = rbd_sysfs_init();
5037 if (rc)
5038 rbd_slab_exit();
5039 else
5040 pr_info("loaded " RBD_DRV_NAME_LONG "\n");
5041
5042 return rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005043}
5044
Alex Eldercc344fa2013-02-19 12:25:56 -06005045static void __exit rbd_exit(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005046{
5047 rbd_sysfs_cleanup();
Alex Elder1c2a9df2013-05-01 12:43:03 -05005048 rbd_slab_exit();
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005049}
5050
5051module_init(rbd_init);
5052module_exit(rbd_exit);
5053
5054MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
5055MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
5056MODULE_DESCRIPTION("rados block device");
5057
5058/* following authorship retained from original osdblk.c */
5059MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
5060
5061MODULE_LICENSE("GPL");