blob: 5d5e3f0b5fb4e86d31a46f9af31f3dbd77cc0cee [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 Elderf84344f2012-08-31 17:29:51 -0500103 /* These four fields never change for a given rbd image */
Alex Elder849b4262012-07-09 21:04:24 -0500104 char *object_prefix;
Alex Elder34b13182012-07-13 20:35:12 -0500105 u64 features;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700106 __u8 obj_order;
107 __u8 crypt_type;
108 __u8 comp_type;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700109
Alex Elderf84344f2012-08-31 17:29:51 -0500110 /* The remaining fields need to be updated occasionally */
111 u64 image_size;
112 struct ceph_snap_context *snapc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700113 char *snap_names;
114 u64 *snap_sizes;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700115
Alex Elder500d0c02013-04-26 09:43:47 -0500116 u64 stripe_unit;
117 u64 stripe_count;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700118};
119
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500120/*
121 * An rbd image specification.
122 *
123 * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely
Alex Elderc66c6e02012-11-01 08:39:26 -0500124 * identify an image. Each rbd_dev structure includes a pointer to
125 * an rbd_spec structure that encapsulates this identity.
126 *
127 * Each of the id's in an rbd_spec has an associated name. For a
128 * user-mapped image, the names are supplied and the id's associated
129 * with them are looked up. For a layered image, a parent image is
130 * defined by the tuple, and the names are looked up.
131 *
132 * An rbd_dev structure contains a parent_spec pointer which is
133 * non-null if the image it represents is a child in a layered
134 * image. This pointer will refer to the rbd_spec structure used
135 * by the parent rbd_dev for its own identity (i.e., the structure
136 * is shared between the parent and child).
137 *
138 * Since these structures are populated once, during the discovery
139 * phase of image construction, they are effectively immutable so
140 * we make no effort to synchronize access to them.
141 *
142 * Note that code herein does not assume the image name is known (it
143 * could be a null pointer).
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500144 */
145struct rbd_spec {
146 u64 pool_id;
Alex Elderecb4dc222013-04-26 09:43:47 -0500147 const char *pool_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500148
Alex Elderecb4dc222013-04-26 09:43:47 -0500149 const char *image_id;
150 const char *image_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500151
152 u64 snap_id;
Alex Elderecb4dc222013-04-26 09:43:47 -0500153 const char *snap_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500154
155 struct kref kref;
156};
157
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700158/*
Alex Elderf0f8cef2012-01-29 13:57:44 -0600159 * an instance of the client. multiple devices may share an rbd client.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700160 */
161struct rbd_client {
162 struct ceph_client *client;
163 struct kref kref;
164 struct list_head node;
165};
166
Alex Elderbf0d5f502012-11-22 00:00:08 -0600167struct rbd_img_request;
168typedef void (*rbd_img_callback_t)(struct rbd_img_request *);
169
170#define BAD_WHICH U32_MAX /* Good which or bad which, which? */
171
172struct rbd_obj_request;
173typedef void (*rbd_obj_callback_t)(struct rbd_obj_request *);
174
Alex Elder9969ebc2013-01-18 12:31:10 -0600175enum obj_request_type {
176 OBJ_REQUEST_NODATA, OBJ_REQUEST_BIO, OBJ_REQUEST_PAGES
177};
Alex Elderbf0d5f502012-11-22 00:00:08 -0600178
Alex Elder926f9b32013-02-11 12:33:24 -0600179enum obj_req_flags {
180 OBJ_REQ_DONE, /* completion flag: not done = 0, done = 1 */
Alex Elder6365d332013-02-11 12:33:24 -0600181 OBJ_REQ_IMG_DATA, /* object usage: standalone = 0, image = 1 */
Alex Elder5679c592013-02-11 12:33:24 -0600182 OBJ_REQ_KNOWN, /* EXISTS flag valid: no = 0, yes = 1 */
183 OBJ_REQ_EXISTS, /* target exists: no = 0, yes = 1 */
Alex Elder926f9b32013-02-11 12:33:24 -0600184};
185
Alex Elderbf0d5f502012-11-22 00:00:08 -0600186struct rbd_obj_request {
187 const char *object_name;
188 u64 offset; /* object start byte */
189 u64 length; /* bytes from offset */
Alex Elder926f9b32013-02-11 12:33:24 -0600190 unsigned long flags;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600191
Alex Elderc5b5ef62013-02-11 12:33:24 -0600192 /*
193 * An object request associated with an image will have its
194 * img_data flag set; a standalone object request will not.
195 *
196 * A standalone object request will have which == BAD_WHICH
197 * and a null obj_request pointer.
198 *
199 * An object request initiated in support of a layered image
200 * object (to check for its existence before a write) will
201 * have which == BAD_WHICH and a non-null obj_request pointer.
202 *
203 * Finally, an object request for rbd image data will have
204 * which != BAD_WHICH, and will have a non-null img_request
205 * pointer. The value of which will be in the range
206 * 0..(img_request->obj_request_count-1).
207 */
208 union {
209 struct rbd_obj_request *obj_request; /* STAT op */
210 struct {
211 struct rbd_img_request *img_request;
212 u64 img_offset;
213 /* links for img_request->obj_requests list */
214 struct list_head links;
215 };
216 };
Alex Elderbf0d5f502012-11-22 00:00:08 -0600217 u32 which; /* posn image request list */
218
219 enum obj_request_type type;
Alex Elder788e2df2013-01-17 12:25:27 -0600220 union {
221 struct bio *bio_list;
222 struct {
223 struct page **pages;
224 u32 page_count;
225 };
226 };
Alex Elder0eefd472013-04-19 15:34:50 -0500227 struct page **copyup_pages;
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 Elderbf0d5f502012-11-22 00:00:08 -0600260 spinlock_t completion_lock;/* protects next_completion */
261 u32 next_completion;
262 rbd_img_callback_t callback;
Alex Elder55f27e02013-04-10 12:34:25 -0500263 u64 xferred;/* aggregate bytes transferred */
Alex Eldera5a337d2013-01-24 16:13:36 -0600264 int result; /* first nonzero obj_request result */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600265
266 u32 obj_request_count;
267 struct list_head obj_requests; /* rbd_obj_request structs */
268
269 struct kref kref;
270};
271
272#define for_each_obj_request(ireq, oreq) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600273 list_for_each_entry(oreq, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600274#define for_each_obj_request_from(ireq, oreq) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600275 list_for_each_entry_from(oreq, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600276#define for_each_obj_request_safe(ireq, oreq, n) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600277 list_for_each_entry_safe_reverse(oreq, n, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600278
Alex Elderf84344f2012-08-31 17:29:51 -0500279struct rbd_mapping {
Alex Elder99c1f082012-08-30 14:42:15 -0500280 u64 size;
Alex Elder34b13182012-07-13 20:35:12 -0500281 u64 features;
Alex Elderf84344f2012-08-31 17:29:51 -0500282 bool read_only;
283};
284
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700285/*
286 * a single device
287 */
288struct rbd_device {
Alex Elderde71a292012-07-03 16:01:19 -0500289 int dev_id; /* blkdev unique id */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700290
291 int major; /* blkdev assigned major */
292 struct gendisk *disk; /* blkdev's gendisk and rq */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700293
Alex Eldera30b71b2012-07-10 20:30:11 -0500294 u32 image_format; /* Either 1 or 2 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700295 struct rbd_client *rbd_client;
296
297 char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */
298
Alex Elderb82d1672013-01-14 12:43:31 -0600299 spinlock_t lock; /* queue, flags, open_count */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700300
301 struct rbd_image_header header;
Alex Elderb82d1672013-01-14 12:43:31 -0600302 unsigned long flags; /* possibly lock protected */
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500303 struct rbd_spec *spec;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700304
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500305 char *header_name;
Alex Elder971f8392012-10-25 23:34:41 -0500306
Alex Elder0903e872012-11-14 12:25:19 -0600307 struct ceph_file_layout layout;
308
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700309 struct ceph_osd_event *watch_event;
Alex Elder975241a2013-01-25 17:08:55 -0600310 struct rbd_obj_request *watch_request;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700311
Alex Elder86b00e02012-10-25 23:34:42 -0500312 struct rbd_spec *parent_spec;
313 u64 parent_overlap;
Alex Elder2f82ee52012-10-30 19:40:33 -0500314 struct rbd_device *parent;
Alex Elder86b00e02012-10-25 23:34:42 -0500315
Josh Durginc6666012011-11-21 17:11:12 -0800316 /* protects updating the header */
317 struct rw_semaphore header_rwsem;
Alex Elderf84344f2012-08-31 17:29:51 -0500318
319 struct rbd_mapping mapping;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700320
321 struct list_head node;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800322
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800323 /* sysfs related */
324 struct device dev;
Alex Elderb82d1672013-01-14 12:43:31 -0600325 unsigned long open_count; /* protected by lock */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800326};
327
Alex Elderb82d1672013-01-14 12:43:31 -0600328/*
329 * Flag bits for rbd_dev->flags. If atomicity is required,
330 * rbd_dev->lock is used to protect access.
331 *
332 * Currently, only the "removing" flag (which is coupled with the
333 * "open_count" field) requires atomic access.
334 */
Alex Elder6d292902013-01-14 12:43:31 -0600335enum rbd_dev_flags {
336 RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */
Alex Elderb82d1672013-01-14 12:43:31 -0600337 RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */
Alex Elder6d292902013-01-14 12:43:31 -0600338};
339
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700340static DEFINE_MUTEX(ctl_mutex); /* Serialize open/close/setup/teardown */
Alex Eldere124a82f2012-01-29 13:57:44 -0600341
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700342static LIST_HEAD(rbd_dev_list); /* devices */
Alex Eldere124a82f2012-01-29 13:57:44 -0600343static DEFINE_SPINLOCK(rbd_dev_list_lock);
344
Alex Elder432b8582012-01-29 13:57:44 -0600345static LIST_HEAD(rbd_client_list); /* clients */
346static DEFINE_SPINLOCK(rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700347
Alex Elder78c2a442013-05-01 12:43:04 -0500348/* Slab caches for frequently-allocated structures */
349
Alex Elder1c2a9df2013-05-01 12:43:03 -0500350static struct kmem_cache *rbd_img_request_cache;
Alex Elder868311b2013-05-01 12:43:03 -0500351static struct kmem_cache *rbd_obj_request_cache;
Alex Elder78c2a442013-05-01 12:43:04 -0500352static struct kmem_cache *rbd_segment_name_cache;
Alex Elder1c2a9df2013-05-01 12:43:03 -0500353
Alex Elder3d7efd12013-04-19 15:34:50 -0500354static int rbd_img_request_submit(struct rbd_img_request *img_request);
355
Alex Elder200a6a82013-04-28 23:32:34 -0500356static void rbd_dev_device_release(struct device *dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800357
Alex Elderf0f8cef2012-01-29 13:57:44 -0600358static ssize_t rbd_add(struct bus_type *bus, const char *buf,
359 size_t count);
360static ssize_t rbd_remove(struct bus_type *bus, const char *buf,
361 size_t count);
Alex Elder71f293e2013-04-26 09:43:48 -0500362static int rbd_dev_image_probe(struct rbd_device *rbd_dev);
Alex Elderf0f8cef2012-01-29 13:57:44 -0600363
364static struct bus_attribute rbd_bus_attrs[] = {
365 __ATTR(add, S_IWUSR, NULL, rbd_add),
366 __ATTR(remove, S_IWUSR, NULL, rbd_remove),
367 __ATTR_NULL
368};
369
370static struct bus_type rbd_bus_type = {
371 .name = "rbd",
372 .bus_attrs = rbd_bus_attrs,
373};
374
375static void rbd_root_dev_release(struct device *dev)
376{
377}
378
379static struct device rbd_root_dev = {
380 .init_name = "rbd",
381 .release = rbd_root_dev_release,
382};
383
Alex Elder06ecc6c2012-11-01 10:17:15 -0500384static __printf(2, 3)
385void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...)
386{
387 struct va_format vaf;
388 va_list args;
389
390 va_start(args, fmt);
391 vaf.fmt = fmt;
392 vaf.va = &args;
393
394 if (!rbd_dev)
395 printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf);
396 else if (rbd_dev->disk)
397 printk(KERN_WARNING "%s: %s: %pV\n",
398 RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf);
399 else if (rbd_dev->spec && rbd_dev->spec->image_name)
400 printk(KERN_WARNING "%s: image %s: %pV\n",
401 RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf);
402 else if (rbd_dev->spec && rbd_dev->spec->image_id)
403 printk(KERN_WARNING "%s: id %s: %pV\n",
404 RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf);
405 else /* punt */
406 printk(KERN_WARNING "%s: rbd_dev %p: %pV\n",
407 RBD_DRV_NAME, rbd_dev, &vaf);
408 va_end(args);
409}
410
Alex Elderaafb2302012-09-06 16:00:54 -0500411#ifdef RBD_DEBUG
412#define rbd_assert(expr) \
413 if (unlikely(!(expr))) { \
414 printk(KERN_ERR "\nAssertion failure in %s() " \
415 "at line %d:\n\n" \
416 "\trbd_assert(%s);\n\n", \
417 __func__, __LINE__, #expr); \
418 BUG(); \
419 }
420#else /* !RBD_DEBUG */
421# define rbd_assert(expr) ((void) 0)
422#endif /* !RBD_DEBUG */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800423
Alex Elderb454e362013-04-19 15:34:50 -0500424static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request);
Alex Elder05a46af2013-04-26 15:44:36 -0500425static void rbd_img_parent_read(struct rbd_obj_request *obj_request);
426static void rbd_dev_remove_parent(struct rbd_device *rbd_dev);
Alex Elder8b3e1a52013-01-24 16:13:36 -0600427
Alex Eldercc4a38bd2013-04-30 00:44:33 -0500428static int rbd_dev_refresh(struct rbd_device *rbd_dev);
429static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev);
Alex Elder54cac612013-04-30 00:44:33 -0500430static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
431 u64 snap_id);
Alex Elder2ad3d712013-04-30 00:44:33 -0500432static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
433 u8 *order, u64 *snap_size);
434static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
435 u64 *snap_features);
436static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700437
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700438static int rbd_open(struct block_device *bdev, fmode_t mode)
439{
Alex Elderf0f8cef2012-01-29 13:57:44 -0600440 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
Alex Elderb82d1672013-01-14 12:43:31 -0600441 bool removing = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700442
Alex Elderf84344f2012-08-31 17:29:51 -0500443 if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700444 return -EROFS;
445
Alex Eldera14ea262013-02-05 13:23:12 -0600446 spin_lock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600447 if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags))
448 removing = true;
449 else
450 rbd_dev->open_count++;
Alex Eldera14ea262013-02-05 13:23:12 -0600451 spin_unlock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600452 if (removing)
453 return -ENOENT;
454
Alex Elder42382b72012-11-16 09:29:16 -0600455 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Alex Elderc3e946c2012-11-16 09:29:16 -0600456 (void) get_device(&rbd_dev->dev);
Alex Elderf84344f2012-08-31 17:29:51 -0500457 set_device_ro(bdev, rbd_dev->mapping.read_only);
Alex Elder42382b72012-11-16 09:29:16 -0600458 mutex_unlock(&ctl_mutex);
Alex Elder340c7a22012-08-10 13:12:07 -0700459
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700460 return 0;
461}
462
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800463static int rbd_release(struct gendisk *disk, fmode_t mode)
464{
465 struct rbd_device *rbd_dev = disk->private_data;
Alex Elderb82d1672013-01-14 12:43:31 -0600466 unsigned long open_count_before;
467
Alex Eldera14ea262013-02-05 13:23:12 -0600468 spin_lock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600469 open_count_before = rbd_dev->open_count--;
Alex Eldera14ea262013-02-05 13:23:12 -0600470 spin_unlock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600471 rbd_assert(open_count_before > 0);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800472
Alex Elder42382b72012-11-16 09:29:16 -0600473 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Alex Elderc3e946c2012-11-16 09:29:16 -0600474 put_device(&rbd_dev->dev);
Alex Elder42382b72012-11-16 09:29:16 -0600475 mutex_unlock(&ctl_mutex);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800476
477 return 0;
478}
479
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700480static const struct block_device_operations rbd_bd_ops = {
481 .owner = THIS_MODULE,
482 .open = rbd_open,
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800483 .release = rbd_release,
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700484};
485
486/*
487 * Initialize an rbd client instance.
Alex Elder43ae4702012-07-03 16:01:18 -0500488 * We own *ceph_opts.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700489 */
Alex Elderf8c38922012-08-10 13:12:07 -0700490static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700491{
492 struct rbd_client *rbdc;
493 int ret = -ENOMEM;
494
Alex Elder37206ee2013-02-20 17:32:08 -0600495 dout("%s:\n", __func__);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700496 rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL);
497 if (!rbdc)
498 goto out_opt;
499
500 kref_init(&rbdc->kref);
501 INIT_LIST_HEAD(&rbdc->node);
502
Alex Elderbc534d82012-01-29 13:57:44 -0600503 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
504
Alex Elder43ae4702012-07-03 16:01:18 -0500505 rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700506 if (IS_ERR(rbdc->client))
Alex Elderbc534d82012-01-29 13:57:44 -0600507 goto out_mutex;
Alex Elder43ae4702012-07-03 16:01:18 -0500508 ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700509
510 ret = ceph_open_session(rbdc->client);
511 if (ret < 0)
512 goto out_err;
513
Alex Elder432b8582012-01-29 13:57:44 -0600514 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700515 list_add_tail(&rbdc->node, &rbd_client_list);
Alex Elder432b8582012-01-29 13:57:44 -0600516 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700517
Alex Elderbc534d82012-01-29 13:57:44 -0600518 mutex_unlock(&ctl_mutex);
Alex Elder37206ee2013-02-20 17:32:08 -0600519 dout("%s: rbdc %p\n", __func__, rbdc);
Alex Elderbc534d82012-01-29 13:57:44 -0600520
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700521 return rbdc;
522
523out_err:
524 ceph_destroy_client(rbdc->client);
Alex Elderbc534d82012-01-29 13:57:44 -0600525out_mutex:
526 mutex_unlock(&ctl_mutex);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700527 kfree(rbdc);
528out_opt:
Alex Elder43ae4702012-07-03 16:01:18 -0500529 if (ceph_opts)
530 ceph_destroy_options(ceph_opts);
Alex Elder37206ee2013-02-20 17:32:08 -0600531 dout("%s: error %d\n", __func__, ret);
532
Vasiliy Kulikov28f259b2010-09-26 12:59:37 +0400533 return ERR_PTR(ret);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700534}
535
Alex Elder2f82ee52012-10-30 19:40:33 -0500536static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc)
537{
538 kref_get(&rbdc->kref);
539
540 return rbdc;
541}
542
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700543/*
Alex Elder1f7ba332012-08-10 13:12:07 -0700544 * Find a ceph client with specific addr and configuration. If
545 * found, bump its reference count.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700546 */
Alex Elder1f7ba332012-08-10 13:12:07 -0700547static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700548{
549 struct rbd_client *client_node;
Alex Elder1f7ba332012-08-10 13:12:07 -0700550 bool found = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700551
Alex Elder43ae4702012-07-03 16:01:18 -0500552 if (ceph_opts->flags & CEPH_OPT_NOSHARE)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700553 return NULL;
554
Alex Elder1f7ba332012-08-10 13:12:07 -0700555 spin_lock(&rbd_client_list_lock);
556 list_for_each_entry(client_node, &rbd_client_list, node) {
557 if (!ceph_compare_options(ceph_opts, client_node->client)) {
Alex Elder2f82ee52012-10-30 19:40:33 -0500558 __rbd_get_client(client_node);
559
Alex Elder1f7ba332012-08-10 13:12:07 -0700560 found = true;
561 break;
562 }
563 }
564 spin_unlock(&rbd_client_list_lock);
565
566 return found ? client_node : NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700567}
568
569/*
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700570 * mount options
571 */
572enum {
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700573 Opt_last_int,
574 /* int args above */
575 Opt_last_string,
576 /* string args above */
Alex Eldercc0538b2012-08-10 13:12:07 -0700577 Opt_read_only,
578 Opt_read_write,
579 /* Boolean args above */
580 Opt_last_bool,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700581};
582
Alex Elder43ae4702012-07-03 16:01:18 -0500583static match_table_t rbd_opts_tokens = {
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700584 /* int args above */
585 /* string args above */
Alex Elderbe466c12012-10-22 11:31:26 -0500586 {Opt_read_only, "read_only"},
Alex Eldercc0538b2012-08-10 13:12:07 -0700587 {Opt_read_only, "ro"}, /* Alternate spelling */
588 {Opt_read_write, "read_write"},
589 {Opt_read_write, "rw"}, /* Alternate spelling */
590 /* Boolean args above */
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700591 {-1, NULL}
592};
593
Alex Elder98571b52013-01-20 14:44:42 -0600594struct rbd_options {
595 bool read_only;
596};
597
598#define RBD_READ_ONLY_DEFAULT false
599
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700600static int parse_rbd_opts_token(char *c, void *private)
601{
Alex Elder43ae4702012-07-03 16:01:18 -0500602 struct rbd_options *rbd_opts = private;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700603 substring_t argstr[MAX_OPT_ARGS];
604 int token, intval, ret;
605
Alex Elder43ae4702012-07-03 16:01:18 -0500606 token = match_token(c, rbd_opts_tokens, argstr);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700607 if (token < 0)
608 return -EINVAL;
609
610 if (token < Opt_last_int) {
611 ret = match_int(&argstr[0], &intval);
612 if (ret < 0) {
613 pr_err("bad mount option arg (not int) "
614 "at '%s'\n", c);
615 return ret;
616 }
617 dout("got int token %d val %d\n", token, intval);
618 } else if (token > Opt_last_int && token < Opt_last_string) {
619 dout("got string token %d val %s\n", token,
620 argstr[0].from);
Alex Eldercc0538b2012-08-10 13:12:07 -0700621 } else if (token > Opt_last_string && token < Opt_last_bool) {
622 dout("got Boolean token %d\n", token);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700623 } else {
624 dout("got token %d\n", token);
625 }
626
627 switch (token) {
Alex Eldercc0538b2012-08-10 13:12:07 -0700628 case Opt_read_only:
629 rbd_opts->read_only = true;
630 break;
631 case Opt_read_write:
632 rbd_opts->read_only = false;
633 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700634 default:
Alex Elderaafb2302012-09-06 16:00:54 -0500635 rbd_assert(false);
636 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700637 }
638 return 0;
639}
640
641/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700642 * Get a ceph client with specific addr and configuration, if one does
643 * not exist create it.
644 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500645static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700646{
Alex Elderf8c38922012-08-10 13:12:07 -0700647 struct rbd_client *rbdc;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700648
Alex Elder1f7ba332012-08-10 13:12:07 -0700649 rbdc = rbd_client_find(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500650 if (rbdc) /* using an existing client */
Alex Elder43ae4702012-07-03 16:01:18 -0500651 ceph_destroy_options(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500652 else
Alex Elderf8c38922012-08-10 13:12:07 -0700653 rbdc = rbd_client_create(ceph_opts);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700654
Alex Elder9d3997f2012-10-25 23:34:42 -0500655 return rbdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700656}
657
658/*
659 * Destroy ceph client
Alex Elderd23a4b32012-01-29 13:57:43 -0600660 *
Alex Elder432b8582012-01-29 13:57:44 -0600661 * Caller must hold rbd_client_list_lock.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700662 */
663static void rbd_client_release(struct kref *kref)
664{
665 struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
666
Alex Elder37206ee2013-02-20 17:32:08 -0600667 dout("%s: rbdc %p\n", __func__, rbdc);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500668 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700669 list_del(&rbdc->node);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500670 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700671
672 ceph_destroy_client(rbdc->client);
673 kfree(rbdc);
674}
675
676/*
677 * Drop reference to ceph client node. If it's not referenced anymore, release
678 * it.
679 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500680static void rbd_put_client(struct rbd_client *rbdc)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700681{
Alex Elderc53d5892012-10-25 23:34:42 -0500682 if (rbdc)
683 kref_put(&rbdc->kref, rbd_client_release);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700684}
685
Alex Eldera30b71b2012-07-10 20:30:11 -0500686static bool rbd_image_format_valid(u32 image_format)
687{
688 return image_format == 1 || image_format == 2;
689}
690
Alex Elder8e94af82012-07-25 09:32:40 -0500691static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
692{
Alex Elder103a1502012-08-02 11:29:45 -0500693 size_t size;
694 u32 snap_count;
695
696 /* The header has to start with the magic rbd header text */
697 if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT)))
698 return false;
699
Alex Elderdb2388b2012-10-20 22:17:27 -0500700 /* The bio layer requires at least sector-sized I/O */
701
702 if (ondisk->options.order < SECTOR_SHIFT)
703 return false;
704
705 /* If we use u64 in a few spots we may be able to loosen this */
706
707 if (ondisk->options.order > 8 * sizeof (int) - 1)
708 return false;
709
Alex Elder103a1502012-08-02 11:29:45 -0500710 /*
711 * The size of a snapshot header has to fit in a size_t, and
712 * that limits the number of snapshots.
713 */
714 snap_count = le32_to_cpu(ondisk->snap_count);
715 size = SIZE_MAX - sizeof (struct ceph_snap_context);
716 if (snap_count > size / sizeof (__le64))
717 return false;
718
719 /*
720 * Not only that, but the size of the entire the snapshot
721 * header must also be representable in a size_t.
722 */
723 size -= snap_count * sizeof (__le64);
724 if ((u64) size < le64_to_cpu(ondisk->snap_names_len))
725 return false;
726
727 return true;
Alex Elder8e94af82012-07-25 09:32:40 -0500728}
729
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700730/*
731 * Create a new header structure, translate header format from the on-disk
732 * header.
733 */
734static int rbd_header_from_disk(struct rbd_image_header *header,
Alex Elder4156d992012-08-02 11:29:46 -0500735 struct rbd_image_header_ondisk *ondisk)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700736{
Alex Elderccece232012-07-10 20:30:10 -0500737 u32 snap_count;
Alex Elder58c17b02012-08-23 23:22:06 -0500738 size_t len;
Alex Elderd2bb24e2012-07-26 23:37:14 -0500739 size_t size;
Alex Elder621901d2012-08-23 23:22:06 -0500740 u32 i;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700741
Alex Elder6a523252012-07-19 17:12:59 -0500742 memset(header, 0, sizeof (*header));
743
Alex Elder103a1502012-08-02 11:29:45 -0500744 snap_count = le32_to_cpu(ondisk->snap_count);
745
Alex Elder58c17b02012-08-23 23:22:06 -0500746 len = strnlen(ondisk->object_prefix, sizeof (ondisk->object_prefix));
747 header->object_prefix = kmalloc(len + 1, GFP_KERNEL);
Alex Elder6a523252012-07-19 17:12:59 -0500748 if (!header->object_prefix)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700749 return -ENOMEM;
Alex Elder58c17b02012-08-23 23:22:06 -0500750 memcpy(header->object_prefix, ondisk->object_prefix, len);
751 header->object_prefix[len] = '\0';
Alex Elder00f1f362012-02-07 12:03:36 -0600752
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700753 if (snap_count) {
Alex Elderf785cc12012-08-23 23:22:06 -0500754 u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len);
755
Alex Elder621901d2012-08-23 23:22:06 -0500756 /* Save a copy of the snapshot names */
757
Alex Elderf785cc12012-08-23 23:22:06 -0500758 if (snap_names_len > (u64) SIZE_MAX)
759 return -EIO;
760 header->snap_names = kmalloc(snap_names_len, GFP_KERNEL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700761 if (!header->snap_names)
Alex Elder6a523252012-07-19 17:12:59 -0500762 goto out_err;
Alex Elderf785cc12012-08-23 23:22:06 -0500763 /*
764 * Note that rbd_dev_v1_header_read() guarantees
765 * the ondisk buffer we're working with has
766 * snap_names_len bytes beyond the end of the
767 * snapshot id array, this memcpy() is safe.
768 */
769 memcpy(header->snap_names, &ondisk->snaps[snap_count],
770 snap_names_len);
Alex Elder6a523252012-07-19 17:12:59 -0500771
Alex Elder621901d2012-08-23 23:22:06 -0500772 /* Record each snapshot's size */
773
Alex Elderd2bb24e2012-07-26 23:37:14 -0500774 size = snap_count * sizeof (*header->snap_sizes);
775 header->snap_sizes = kmalloc(size, GFP_KERNEL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700776 if (!header->snap_sizes)
Alex Elder6a523252012-07-19 17:12:59 -0500777 goto out_err;
Alex Elder621901d2012-08-23 23:22:06 -0500778 for (i = 0; i < snap_count; i++)
779 header->snap_sizes[i] =
780 le64_to_cpu(ondisk->snaps[i].image_size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700781 } else {
782 header->snap_names = NULL;
783 header->snap_sizes = NULL;
784 }
Alex Elder849b4262012-07-09 21:04:24 -0500785
Alex Elder34b13182012-07-13 20:35:12 -0500786 header->features = 0; /* No features support in v1 images */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700787 header->obj_order = ondisk->options.order;
788 header->crypt_type = ondisk->options.crypt_type;
789 header->comp_type = ondisk->options.comp_type;
Alex Elder6a523252012-07-19 17:12:59 -0500790
Alex Elder621901d2012-08-23 23:22:06 -0500791 /* Allocate and fill in the snapshot context */
792
Alex Elderf84344f2012-08-31 17:29:51 -0500793 header->image_size = le64_to_cpu(ondisk->image_size);
Alex Elder468521c2013-04-26 09:43:47 -0500794
Alex Elder812164f82013-04-30 00:44:32 -0500795 header->snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
Alex Elder6a523252012-07-19 17:12:59 -0500796 if (!header->snapc)
797 goto out_err;
Alex Elder505cbb92012-07-19 08:49:18 -0500798 header->snapc->seq = le64_to_cpu(ondisk->snap_seq);
Alex Elder621901d2012-08-23 23:22:06 -0500799 for (i = 0; i < snap_count; i++)
Alex Elder468521c2013-04-26 09:43:47 -0500800 header->snapc->snaps[i] = le64_to_cpu(ondisk->snaps[i].id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700801
802 return 0;
803
Alex Elder6a523252012-07-19 17:12:59 -0500804out_err:
Alex Elder849b4262012-07-09 21:04:24 -0500805 kfree(header->snap_sizes);
Alex Elderccece232012-07-10 20:30:10 -0500806 header->snap_sizes = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700807 kfree(header->snap_names);
Alex Elderccece232012-07-10 20:30:10 -0500808 header->snap_names = NULL;
Alex Elder6a523252012-07-19 17:12:59 -0500809 kfree(header->object_prefix);
810 header->object_prefix = NULL;
Alex Elderccece232012-07-10 20:30:10 -0500811
Alex Elder00f1f362012-02-07 12:03:36 -0600812 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700813}
814
Alex Elder9682fc62013-04-30 00:44:33 -0500815static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which)
816{
817 const char *snap_name;
818
819 rbd_assert(which < rbd_dev->header.snapc->num_snaps);
820
821 /* Skip over names until we find the one we are looking for */
822
823 snap_name = rbd_dev->header.snap_names;
824 while (which--)
825 snap_name += strlen(snap_name) + 1;
826
827 return kstrdup(snap_name, GFP_KERNEL);
828}
829
Alex Elder30d1cff2013-05-01 12:43:03 -0500830/*
831 * Snapshot id comparison function for use with qsort()/bsearch().
832 * Note that result is for snapshots in *descending* order.
833 */
834static int snapid_compare_reverse(const void *s1, const void *s2)
835{
836 u64 snap_id1 = *(u64 *)s1;
837 u64 snap_id2 = *(u64 *)s2;
838
839 if (snap_id1 < snap_id2)
840 return 1;
841 return snap_id1 == snap_id2 ? 0 : -1;
842}
843
844/*
845 * Search a snapshot context to see if the given snapshot id is
846 * present.
847 *
848 * Returns the position of the snapshot id in the array if it's found,
849 * or BAD_SNAP_INDEX otherwise.
850 *
851 * Note: The snapshot array is in kept sorted (by the osd) in
852 * reverse order, highest snapshot id first.
853 */
Alex Elder9682fc62013-04-30 00:44:33 -0500854static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id)
855{
856 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
Alex Elder30d1cff2013-05-01 12:43:03 -0500857 u64 *found;
Alex Elder9682fc62013-04-30 00:44:33 -0500858
Alex Elder30d1cff2013-05-01 12:43:03 -0500859 found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps,
860 sizeof (snap_id), snapid_compare_reverse);
Alex Elder9682fc62013-04-30 00:44:33 -0500861
Alex Elder30d1cff2013-05-01 12:43:03 -0500862 return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX;
Alex Elder9682fc62013-04-30 00:44:33 -0500863}
864
Alex Elder2ad3d712013-04-30 00:44:33 -0500865static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev,
866 u64 snap_id)
Alex Elder54cac612013-04-30 00:44:33 -0500867{
868 u32 which;
869
870 which = rbd_dev_snap_index(rbd_dev, snap_id);
871 if (which == BAD_SNAP_INDEX)
872 return NULL;
873
874 return _rbd_dev_v1_snap_name(rbd_dev, which);
875}
876
Alex Elder9e15b772012-10-30 19:40:33 -0500877static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id)
878{
Alex Elder9e15b772012-10-30 19:40:33 -0500879 if (snap_id == CEPH_NOSNAP)
880 return RBD_SNAP_HEAD_NAME;
881
Alex Elder54cac612013-04-30 00:44:33 -0500882 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
883 if (rbd_dev->image_format == 1)
884 return rbd_dev_v1_snap_name(rbd_dev, snap_id);
Alex Elder9e15b772012-10-30 19:40:33 -0500885
Alex Elder54cac612013-04-30 00:44:33 -0500886 return rbd_dev_v2_snap_name(rbd_dev, snap_id);
Alex Elder9e15b772012-10-30 19:40:33 -0500887}
888
Alex Elder2ad3d712013-04-30 00:44:33 -0500889static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
890 u64 *snap_size)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700891{
Alex Elder2ad3d712013-04-30 00:44:33 -0500892 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
893 if (snap_id == CEPH_NOSNAP) {
894 *snap_size = rbd_dev->header.image_size;
895 } else if (rbd_dev->image_format == 1) {
896 u32 which;
Alex Elder00f1f362012-02-07 12:03:36 -0600897
Alex Elder2ad3d712013-04-30 00:44:33 -0500898 which = rbd_dev_snap_index(rbd_dev, snap_id);
899 if (which == BAD_SNAP_INDEX)
900 return -ENOENT;
Alex Elder00f1f362012-02-07 12:03:36 -0600901
Alex Elder2ad3d712013-04-30 00:44:33 -0500902 *snap_size = rbd_dev->header.snap_sizes[which];
903 } else {
904 u64 size = 0;
905 int ret;
906
907 ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size);
908 if (ret)
909 return ret;
910
911 *snap_size = size;
912 }
913 return 0;
914}
915
916static int rbd_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
917 u64 *snap_features)
918{
919 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
920 if (snap_id == CEPH_NOSNAP) {
921 *snap_features = rbd_dev->header.features;
922 } else if (rbd_dev->image_format == 1) {
923 *snap_features = 0; /* No features for format 1 */
924 } else {
925 u64 features = 0;
926 int ret;
927
928 ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, &features);
929 if (ret)
930 return ret;
931
932 *snap_features = features;
933 }
934 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700935}
936
Alex Elderd1cf5782013-04-27 09:59:30 -0500937static int rbd_dev_mapping_set(struct rbd_device *rbd_dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700938{
Alex Elder2ad3d712013-04-30 00:44:33 -0500939 const char *snap_name = rbd_dev->spec->snap_name;
940 u64 snap_id;
941 u64 size = 0;
942 u64 features = 0;
943 int ret;
Alex Elder8b0241f2013-04-25 23:15:08 -0500944
Alex Elder2ad3d712013-04-30 00:44:33 -0500945 if (strcmp(snap_name, RBD_SNAP_HEAD_NAME)) {
946 snap_id = rbd_snap_id_by_name(rbd_dev, snap_name);
947 if (snap_id == CEPH_NOSNAP)
Alex Elder8b0241f2013-04-25 23:15:08 -0500948 return -ENOENT;
Alex Elder2ad3d712013-04-30 00:44:33 -0500949 } else {
950 snap_id = CEPH_NOSNAP;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700951 }
Alex Elder6d292902013-01-14 12:43:31 -0600952
Alex Elder2ad3d712013-04-30 00:44:33 -0500953 ret = rbd_snap_size(rbd_dev, snap_id, &size);
954 if (ret)
955 return ret;
956 ret = rbd_snap_features(rbd_dev, snap_id, &features);
957 if (ret)
958 return ret;
959
960 rbd_dev->mapping.size = size;
961 rbd_dev->mapping.features = features;
962
963 /* If we are mapping a snapshot it must be marked read-only */
964
965 if (snap_id != CEPH_NOSNAP)
966 rbd_dev->mapping.read_only = true;
967
Alex Elder8b0241f2013-04-25 23:15:08 -0500968 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700969}
970
Alex Elderd1cf5782013-04-27 09:59:30 -0500971static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev)
972{
973 rbd_dev->mapping.size = 0;
974 rbd_dev->mapping.features = 0;
975 rbd_dev->mapping.read_only = true;
976}
977
Alex Elder200a6a82013-04-28 23:32:34 -0500978static void rbd_dev_clear_mapping(struct rbd_device *rbd_dev)
979{
980 rbd_dev->mapping.size = 0;
981 rbd_dev->mapping.features = 0;
982 rbd_dev->mapping.read_only = true;
983}
984
Alex Elder98571b52013-01-20 14:44:42 -0600985static const char *rbd_segment_name(struct rbd_device *rbd_dev, u64 offset)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700986{
Alex Elder65ccfe22012-08-09 10:33:26 -0700987 char *name;
988 u64 segment;
989 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700990
Alex Elder78c2a442013-05-01 12:43:04 -0500991 name = kmem_cache_alloc(rbd_segment_name_cache, GFP_NOIO);
Alex Elder65ccfe22012-08-09 10:33:26 -0700992 if (!name)
993 return NULL;
994 segment = offset >> rbd_dev->header.obj_order;
Alex Elder2fd82b92012-11-09 15:05:54 -0600995 ret = snprintf(name, MAX_OBJ_NAME_SIZE + 1, "%s.%012llx",
Alex Elder65ccfe22012-08-09 10:33:26 -0700996 rbd_dev->header.object_prefix, segment);
Alex Elder2fd82b92012-11-09 15:05:54 -0600997 if (ret < 0 || ret > MAX_OBJ_NAME_SIZE) {
Alex Elder65ccfe22012-08-09 10:33:26 -0700998 pr_err("error formatting segment name for #%llu (%d)\n",
999 segment, ret);
1000 kfree(name);
1001 name = NULL;
1002 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001003
Alex Elder65ccfe22012-08-09 10:33:26 -07001004 return name;
1005}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001006
Alex Elder78c2a442013-05-01 12:43:04 -05001007static void rbd_segment_name_free(const char *name)
1008{
1009 /* The explicit cast here is needed to drop the const qualifier */
1010
1011 kmem_cache_free(rbd_segment_name_cache, (void *)name);
1012}
1013
Alex Elder65ccfe22012-08-09 10:33:26 -07001014static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset)
1015{
1016 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001017
Alex Elder65ccfe22012-08-09 10:33:26 -07001018 return offset & (segment_size - 1);
1019}
1020
1021static u64 rbd_segment_length(struct rbd_device *rbd_dev,
1022 u64 offset, u64 length)
1023{
1024 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
1025
1026 offset &= segment_size - 1;
1027
Alex Elderaafb2302012-09-06 16:00:54 -05001028 rbd_assert(length <= U64_MAX - offset);
Alex Elder65ccfe22012-08-09 10:33:26 -07001029 if (offset + length > segment_size)
1030 length = segment_size - offset;
1031
1032 return length;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001033}
1034
1035/*
Josh Durgin029bcbd2011-07-22 11:35:23 -07001036 * returns the size of an object in the image
1037 */
1038static u64 rbd_obj_bytes(struct rbd_image_header *header)
1039{
1040 return 1 << header->obj_order;
1041}
1042
1043/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001044 * bio helpers
1045 */
1046
1047static void bio_chain_put(struct bio *chain)
1048{
1049 struct bio *tmp;
1050
1051 while (chain) {
1052 tmp = chain;
1053 chain = chain->bi_next;
1054 bio_put(tmp);
1055 }
1056}
1057
1058/*
1059 * zeros a bio chain, starting at specific offset
1060 */
1061static void zero_bio_chain(struct bio *chain, int start_ofs)
1062{
1063 struct bio_vec *bv;
1064 unsigned long flags;
1065 void *buf;
1066 int i;
1067 int pos = 0;
1068
1069 while (chain) {
1070 bio_for_each_segment(bv, chain, i) {
1071 if (pos + bv->bv_len > start_ofs) {
1072 int remainder = max(start_ofs - pos, 0);
1073 buf = bvec_kmap_irq(bv, &flags);
1074 memset(buf + remainder, 0,
1075 bv->bv_len - remainder);
Dan Carpenter85b5aaa2010-10-11 21:15:11 +02001076 bvec_kunmap_irq(buf, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001077 }
1078 pos += bv->bv_len;
1079 }
1080
1081 chain = chain->bi_next;
1082 }
1083}
1084
1085/*
Alex Elderb9434c52013-04-19 15:34:50 -05001086 * similar to zero_bio_chain(), zeros data defined by a page array,
1087 * starting at the given byte offset from the start of the array and
1088 * continuing up to the given end offset. The pages array is
1089 * assumed to be big enough to hold all bytes up to the end.
1090 */
1091static void zero_pages(struct page **pages, u64 offset, u64 end)
1092{
1093 struct page **page = &pages[offset >> PAGE_SHIFT];
1094
1095 rbd_assert(end > offset);
1096 rbd_assert(end - offset <= (u64)SIZE_MAX);
1097 while (offset < end) {
1098 size_t page_offset;
1099 size_t length;
1100 unsigned long flags;
1101 void *kaddr;
1102
1103 page_offset = (size_t)(offset & ~PAGE_MASK);
1104 length = min(PAGE_SIZE - page_offset, (size_t)(end - offset));
1105 local_irq_save(flags);
1106 kaddr = kmap_atomic(*page);
1107 memset(kaddr + page_offset, 0, length);
1108 kunmap_atomic(kaddr);
1109 local_irq_restore(flags);
1110
1111 offset += length;
1112 page++;
1113 }
1114}
1115
1116/*
Alex Elderf7760da2012-10-20 22:17:27 -05001117 * Clone a portion of a bio, starting at the given byte offset
1118 * and continuing for the number of bytes indicated.
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001119 */
Alex Elderf7760da2012-10-20 22:17:27 -05001120static struct bio *bio_clone_range(struct bio *bio_src,
1121 unsigned int offset,
1122 unsigned int len,
1123 gfp_t gfpmask)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001124{
Alex Elderf7760da2012-10-20 22:17:27 -05001125 struct bio_vec *bv;
1126 unsigned int resid;
1127 unsigned short idx;
1128 unsigned int voff;
1129 unsigned short end_idx;
1130 unsigned short vcnt;
1131 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001132
Alex Elderf7760da2012-10-20 22:17:27 -05001133 /* Handle the easy case for the caller */
1134
1135 if (!offset && len == bio_src->bi_size)
1136 return bio_clone(bio_src, gfpmask);
1137
1138 if (WARN_ON_ONCE(!len))
1139 return NULL;
1140 if (WARN_ON_ONCE(len > bio_src->bi_size))
1141 return NULL;
1142 if (WARN_ON_ONCE(offset > bio_src->bi_size - len))
1143 return NULL;
1144
1145 /* Find first affected segment... */
1146
1147 resid = offset;
1148 __bio_for_each_segment(bv, bio_src, idx, 0) {
1149 if (resid < bv->bv_len)
1150 break;
1151 resid -= bv->bv_len;
1152 }
1153 voff = resid;
1154
1155 /* ...and the last affected segment */
1156
1157 resid += len;
1158 __bio_for_each_segment(bv, bio_src, end_idx, idx) {
1159 if (resid <= bv->bv_len)
1160 break;
1161 resid -= bv->bv_len;
1162 }
1163 vcnt = end_idx - idx + 1;
1164
1165 /* Build the clone */
1166
1167 bio = bio_alloc(gfpmask, (unsigned int) vcnt);
1168 if (!bio)
1169 return NULL; /* ENOMEM */
1170
1171 bio->bi_bdev = bio_src->bi_bdev;
1172 bio->bi_sector = bio_src->bi_sector + (offset >> SECTOR_SHIFT);
1173 bio->bi_rw = bio_src->bi_rw;
1174 bio->bi_flags |= 1 << BIO_CLONED;
1175
1176 /*
1177 * Copy over our part of the bio_vec, then update the first
1178 * and last (or only) entries.
1179 */
1180 memcpy(&bio->bi_io_vec[0], &bio_src->bi_io_vec[idx],
1181 vcnt * sizeof (struct bio_vec));
1182 bio->bi_io_vec[0].bv_offset += voff;
1183 if (vcnt > 1) {
1184 bio->bi_io_vec[0].bv_len -= voff;
1185 bio->bi_io_vec[vcnt - 1].bv_len = resid;
1186 } else {
1187 bio->bi_io_vec[0].bv_len = len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001188 }
1189
Alex Elderf7760da2012-10-20 22:17:27 -05001190 bio->bi_vcnt = vcnt;
1191 bio->bi_size = len;
1192 bio->bi_idx = 0;
Alex Elder542582f2012-08-09 10:33:25 -07001193
Alex Elderf7760da2012-10-20 22:17:27 -05001194 return bio;
1195}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001196
Alex Elderf7760da2012-10-20 22:17:27 -05001197/*
1198 * Clone a portion of a bio chain, starting at the given byte offset
1199 * into the first bio in the source chain and continuing for the
1200 * number of bytes indicated. The result is another bio chain of
1201 * exactly the given length, or a null pointer on error.
1202 *
1203 * The bio_src and offset parameters are both in-out. On entry they
1204 * refer to the first source bio and the offset into that bio where
1205 * the start of data to be cloned is located.
1206 *
1207 * On return, bio_src is updated to refer to the bio in the source
1208 * chain that contains first un-cloned byte, and *offset will
1209 * contain the offset of that byte within that bio.
1210 */
1211static struct bio *bio_chain_clone_range(struct bio **bio_src,
1212 unsigned int *offset,
1213 unsigned int len,
1214 gfp_t gfpmask)
1215{
1216 struct bio *bi = *bio_src;
1217 unsigned int off = *offset;
1218 struct bio *chain = NULL;
1219 struct bio **end;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001220
Alex Elderf7760da2012-10-20 22:17:27 -05001221 /* Build up a chain of clone bios up to the limit */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001222
Alex Elderf7760da2012-10-20 22:17:27 -05001223 if (!bi || off >= bi->bi_size || !len)
1224 return NULL; /* Nothing to clone */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001225
Alex Elderf7760da2012-10-20 22:17:27 -05001226 end = &chain;
1227 while (len) {
1228 unsigned int bi_size;
1229 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001230
Alex Elderf5400b72012-11-01 10:17:15 -05001231 if (!bi) {
1232 rbd_warn(NULL, "bio_chain exhausted with %u left", len);
Alex Elderf7760da2012-10-20 22:17:27 -05001233 goto out_err; /* EINVAL; ran out of bio's */
Alex Elderf5400b72012-11-01 10:17:15 -05001234 }
Alex Elderf7760da2012-10-20 22:17:27 -05001235 bi_size = min_t(unsigned int, bi->bi_size - off, len);
1236 bio = bio_clone_range(bi, off, bi_size, gfpmask);
1237 if (!bio)
1238 goto out_err; /* ENOMEM */
1239
1240 *end = bio;
1241 end = &bio->bi_next;
1242
1243 off += bi_size;
1244 if (off == bi->bi_size) {
1245 bi = bi->bi_next;
1246 off = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001247 }
Alex Elderf7760da2012-10-20 22:17:27 -05001248 len -= bi_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001249 }
Alex Elderf7760da2012-10-20 22:17:27 -05001250 *bio_src = bi;
1251 *offset = off;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001252
Alex Elderf7760da2012-10-20 22:17:27 -05001253 return chain;
1254out_err:
1255 bio_chain_put(chain);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001256
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001257 return NULL;
1258}
1259
Alex Elder926f9b32013-02-11 12:33:24 -06001260/*
1261 * The default/initial value for all object request flags is 0. For
1262 * each flag, once its value is set to 1 it is never reset to 0
1263 * again.
1264 */
Alex Elder6365d332013-02-11 12:33:24 -06001265static void obj_request_img_data_set(struct rbd_obj_request *obj_request)
1266{
1267 if (test_and_set_bit(OBJ_REQ_IMG_DATA, &obj_request->flags)) {
Alex Elder6365d332013-02-11 12:33:24 -06001268 struct rbd_device *rbd_dev;
1269
Alex Elder57acbaa2013-02-11 12:33:24 -06001270 rbd_dev = obj_request->img_request->rbd_dev;
Alex Elder6365d332013-02-11 12:33:24 -06001271 rbd_warn(rbd_dev, "obj_request %p already marked img_data\n",
1272 obj_request);
1273 }
1274}
1275
1276static bool obj_request_img_data_test(struct rbd_obj_request *obj_request)
1277{
1278 smp_mb();
1279 return test_bit(OBJ_REQ_IMG_DATA, &obj_request->flags) != 0;
1280}
1281
Alex Elder57acbaa2013-02-11 12:33:24 -06001282static void obj_request_done_set(struct rbd_obj_request *obj_request)
1283{
1284 if (test_and_set_bit(OBJ_REQ_DONE, &obj_request->flags)) {
1285 struct rbd_device *rbd_dev = NULL;
1286
1287 if (obj_request_img_data_test(obj_request))
1288 rbd_dev = obj_request->img_request->rbd_dev;
1289 rbd_warn(rbd_dev, "obj_request %p already marked done\n",
1290 obj_request);
1291 }
1292}
1293
1294static bool obj_request_done_test(struct rbd_obj_request *obj_request)
1295{
1296 smp_mb();
1297 return test_bit(OBJ_REQ_DONE, &obj_request->flags) != 0;
1298}
1299
Alex Elder5679c592013-02-11 12:33:24 -06001300/*
1301 * This sets the KNOWN flag after (possibly) setting the EXISTS
1302 * flag. The latter is set based on the "exists" value provided.
1303 *
1304 * Note that for our purposes once an object exists it never goes
1305 * away again. It's possible that the response from two existence
1306 * checks are separated by the creation of the target object, and
1307 * the first ("doesn't exist") response arrives *after* the second
1308 * ("does exist"). In that case we ignore the second one.
1309 */
1310static void obj_request_existence_set(struct rbd_obj_request *obj_request,
1311 bool exists)
1312{
1313 if (exists)
1314 set_bit(OBJ_REQ_EXISTS, &obj_request->flags);
1315 set_bit(OBJ_REQ_KNOWN, &obj_request->flags);
1316 smp_mb();
1317}
1318
1319static bool obj_request_known_test(struct rbd_obj_request *obj_request)
1320{
1321 smp_mb();
1322 return test_bit(OBJ_REQ_KNOWN, &obj_request->flags) != 0;
1323}
1324
1325static bool obj_request_exists_test(struct rbd_obj_request *obj_request)
1326{
1327 smp_mb();
1328 return test_bit(OBJ_REQ_EXISTS, &obj_request->flags) != 0;
1329}
1330
Alex Elderbf0d5f502012-11-22 00:00:08 -06001331static void rbd_obj_request_get(struct rbd_obj_request *obj_request)
1332{
Alex Elder37206ee2013-02-20 17:32:08 -06001333 dout("%s: obj %p (was %d)\n", __func__, obj_request,
1334 atomic_read(&obj_request->kref.refcount));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001335 kref_get(&obj_request->kref);
1336}
1337
1338static void rbd_obj_request_destroy(struct kref *kref);
1339static void rbd_obj_request_put(struct rbd_obj_request *obj_request)
1340{
1341 rbd_assert(obj_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001342 dout("%s: obj %p (was %d)\n", __func__, obj_request,
1343 atomic_read(&obj_request->kref.refcount));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001344 kref_put(&obj_request->kref, rbd_obj_request_destroy);
1345}
1346
1347static void rbd_img_request_get(struct rbd_img_request *img_request)
1348{
Alex Elder37206ee2013-02-20 17:32:08 -06001349 dout("%s: img %p (was %d)\n", __func__, img_request,
1350 atomic_read(&img_request->kref.refcount));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001351 kref_get(&img_request->kref);
1352}
1353
1354static void rbd_img_request_destroy(struct kref *kref);
1355static void rbd_img_request_put(struct rbd_img_request *img_request)
1356{
1357 rbd_assert(img_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001358 dout("%s: img %p (was %d)\n", __func__, img_request,
1359 atomic_read(&img_request->kref.refcount));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001360 kref_put(&img_request->kref, rbd_img_request_destroy);
1361}
1362
1363static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request,
1364 struct rbd_obj_request *obj_request)
1365{
Alex Elder25dcf952013-01-25 17:08:55 -06001366 rbd_assert(obj_request->img_request == NULL);
1367
Alex Elderb155e862013-04-15 14:50:37 -05001368 /* Image request now owns object's original reference */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001369 obj_request->img_request = img_request;
Alex Elder25dcf952013-01-25 17:08:55 -06001370 obj_request->which = img_request->obj_request_count;
Alex Elder6365d332013-02-11 12:33:24 -06001371 rbd_assert(!obj_request_img_data_test(obj_request));
1372 obj_request_img_data_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001373 rbd_assert(obj_request->which != BAD_WHICH);
Alex Elder25dcf952013-01-25 17:08:55 -06001374 img_request->obj_request_count++;
1375 list_add_tail(&obj_request->links, &img_request->obj_requests);
Alex Elder37206ee2013-02-20 17:32:08 -06001376 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1377 obj_request->which);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001378}
1379
1380static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request,
1381 struct rbd_obj_request *obj_request)
1382{
1383 rbd_assert(obj_request->which != BAD_WHICH);
Alex Elder25dcf952013-01-25 17:08:55 -06001384
Alex Elder37206ee2013-02-20 17:32:08 -06001385 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1386 obj_request->which);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001387 list_del(&obj_request->links);
Alex Elder25dcf952013-01-25 17:08:55 -06001388 rbd_assert(img_request->obj_request_count > 0);
1389 img_request->obj_request_count--;
1390 rbd_assert(obj_request->which == img_request->obj_request_count);
1391 obj_request->which = BAD_WHICH;
Alex Elder6365d332013-02-11 12:33:24 -06001392 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001393 rbd_assert(obj_request->img_request == img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001394 obj_request->img_request = NULL;
Alex Elder25dcf952013-01-25 17:08:55 -06001395 obj_request->callback = NULL;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001396 rbd_obj_request_put(obj_request);
1397}
1398
1399static bool obj_request_type_valid(enum obj_request_type type)
1400{
1401 switch (type) {
Alex Elder9969ebc2013-01-18 12:31:10 -06001402 case OBJ_REQUEST_NODATA:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001403 case OBJ_REQUEST_BIO:
Alex Elder788e2df2013-01-17 12:25:27 -06001404 case OBJ_REQUEST_PAGES:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001405 return true;
1406 default:
1407 return false;
1408 }
1409}
1410
Alex Elderbf0d5f502012-11-22 00:00:08 -06001411static int rbd_obj_request_submit(struct ceph_osd_client *osdc,
1412 struct rbd_obj_request *obj_request)
1413{
Alex Elder37206ee2013-02-20 17:32:08 -06001414 dout("%s: osdc %p obj %p\n", __func__, osdc, obj_request);
1415
Alex Elderbf0d5f502012-11-22 00:00:08 -06001416 return ceph_osdc_start_request(osdc, obj_request->osd_req, false);
1417}
1418
1419static void rbd_img_request_complete(struct rbd_img_request *img_request)
1420{
Alex Elder55f27e02013-04-10 12:34:25 -05001421
Alex Elder37206ee2013-02-20 17:32:08 -06001422 dout("%s: img %p\n", __func__, img_request);
Alex Elder55f27e02013-04-10 12:34:25 -05001423
1424 /*
1425 * If no error occurred, compute the aggregate transfer
1426 * count for the image request. We could instead use
1427 * atomic64_cmpxchg() to update it as each object request
1428 * completes; not clear which way is better off hand.
1429 */
1430 if (!img_request->result) {
1431 struct rbd_obj_request *obj_request;
1432 u64 xferred = 0;
1433
1434 for_each_obj_request(img_request, obj_request)
1435 xferred += obj_request->xferred;
1436 img_request->xferred = xferred;
1437 }
1438
Alex Elderbf0d5f502012-11-22 00:00:08 -06001439 if (img_request->callback)
1440 img_request->callback(img_request);
1441 else
1442 rbd_img_request_put(img_request);
1443}
1444
Alex Elder788e2df2013-01-17 12:25:27 -06001445/* Caller is responsible for rbd_obj_request_destroy(obj_request) */
1446
1447static int rbd_obj_request_wait(struct rbd_obj_request *obj_request)
1448{
Alex Elder37206ee2013-02-20 17:32:08 -06001449 dout("%s: obj %p\n", __func__, obj_request);
1450
Alex Elder788e2df2013-01-17 12:25:27 -06001451 return wait_for_completion_interruptible(&obj_request->completion);
1452}
1453
Alex Elder0c425242013-02-08 09:55:49 -06001454/*
1455 * The default/initial value for all image request flags is 0. Each
1456 * is conditionally set to 1 at image request initialization time
1457 * and currently never change thereafter.
1458 */
1459static void img_request_write_set(struct rbd_img_request *img_request)
1460{
1461 set_bit(IMG_REQ_WRITE, &img_request->flags);
1462 smp_mb();
1463}
1464
1465static bool img_request_write_test(struct rbd_img_request *img_request)
1466{
1467 smp_mb();
1468 return test_bit(IMG_REQ_WRITE, &img_request->flags) != 0;
1469}
1470
Alex Elder9849e982013-01-24 16:13:36 -06001471static void img_request_child_set(struct rbd_img_request *img_request)
1472{
1473 set_bit(IMG_REQ_CHILD, &img_request->flags);
1474 smp_mb();
1475}
1476
1477static bool img_request_child_test(struct rbd_img_request *img_request)
1478{
1479 smp_mb();
1480 return test_bit(IMG_REQ_CHILD, &img_request->flags) != 0;
1481}
1482
Alex Elderd0b2e942013-01-24 16:13:36 -06001483static void img_request_layered_set(struct rbd_img_request *img_request)
1484{
1485 set_bit(IMG_REQ_LAYERED, &img_request->flags);
1486 smp_mb();
1487}
1488
1489static bool img_request_layered_test(struct rbd_img_request *img_request)
1490{
1491 smp_mb();
1492 return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0;
1493}
1494
Alex Elder6e2a4502013-03-27 09:16:30 -05001495static void
1496rbd_img_obj_request_read_callback(struct rbd_obj_request *obj_request)
1497{
Alex Elderb9434c52013-04-19 15:34:50 -05001498 u64 xferred = obj_request->xferred;
1499 u64 length = obj_request->length;
1500
Alex Elder6e2a4502013-03-27 09:16:30 -05001501 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1502 obj_request, obj_request->img_request, obj_request->result,
Alex Elderb9434c52013-04-19 15:34:50 -05001503 xferred, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001504 /*
1505 * ENOENT means a hole in the image. We zero-fill the
1506 * entire length of the request. A short read also implies
1507 * zero-fill to the end of the request. Either way we
1508 * update the xferred count to indicate the whole request
1509 * was satisfied.
1510 */
Alex Elderb9434c52013-04-19 15:34:50 -05001511 rbd_assert(obj_request->type != OBJ_REQUEST_NODATA);
Alex Elder6e2a4502013-03-27 09:16:30 -05001512 if (obj_request->result == -ENOENT) {
Alex Elderb9434c52013-04-19 15:34:50 -05001513 if (obj_request->type == OBJ_REQUEST_BIO)
1514 zero_bio_chain(obj_request->bio_list, 0);
1515 else
1516 zero_pages(obj_request->pages, 0, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001517 obj_request->result = 0;
Alex Elderb9434c52013-04-19 15:34:50 -05001518 obj_request->xferred = length;
1519 } else if (xferred < length && !obj_request->result) {
1520 if (obj_request->type == OBJ_REQUEST_BIO)
1521 zero_bio_chain(obj_request->bio_list, xferred);
1522 else
1523 zero_pages(obj_request->pages, xferred, length);
1524 obj_request->xferred = length;
Alex Elder6e2a4502013-03-27 09:16:30 -05001525 }
1526 obj_request_done_set(obj_request);
1527}
1528
Alex Elderbf0d5f502012-11-22 00:00:08 -06001529static void rbd_obj_request_complete(struct rbd_obj_request *obj_request)
1530{
Alex Elder37206ee2013-02-20 17:32:08 -06001531 dout("%s: obj %p cb %p\n", __func__, obj_request,
1532 obj_request->callback);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001533 if (obj_request->callback)
1534 obj_request->callback(obj_request);
Alex Elder788e2df2013-01-17 12:25:27 -06001535 else
1536 complete_all(&obj_request->completion);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001537}
1538
Alex Elderc47f9372013-02-26 14:23:07 -06001539static void rbd_osd_trivial_callback(struct rbd_obj_request *obj_request)
Alex Elder39bf2c52013-02-26 14:23:07 -06001540{
1541 dout("%s: obj %p\n", __func__, obj_request);
1542 obj_request_done_set(obj_request);
1543}
1544
Alex Elderc47f9372013-02-26 14:23:07 -06001545static void rbd_osd_read_callback(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001546{
Alex Elder57acbaa2013-02-11 12:33:24 -06001547 struct rbd_img_request *img_request = NULL;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001548 struct rbd_device *rbd_dev = NULL;
Alex Elder57acbaa2013-02-11 12:33:24 -06001549 bool layered = false;
1550
1551 if (obj_request_img_data_test(obj_request)) {
1552 img_request = obj_request->img_request;
1553 layered = img_request && img_request_layered_test(img_request);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001554 rbd_dev = img_request->rbd_dev;
Alex Elder57acbaa2013-02-11 12:33:24 -06001555 }
Alex Elder8b3e1a52013-01-24 16:13:36 -06001556
1557 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1558 obj_request, img_request, obj_request->result,
1559 obj_request->xferred, obj_request->length);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001560 if (layered && obj_request->result == -ENOENT &&
1561 obj_request->img_offset < rbd_dev->parent_overlap)
Alex Elder8b3e1a52013-01-24 16:13:36 -06001562 rbd_img_parent_read(obj_request);
1563 else if (img_request)
Alex Elder6e2a4502013-03-27 09:16:30 -05001564 rbd_img_obj_request_read_callback(obj_request);
1565 else
1566 obj_request_done_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001567}
1568
Alex Elderc47f9372013-02-26 14:23:07 -06001569static void rbd_osd_write_callback(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001570{
Sage Weil1b83bef2013-02-25 16:11:12 -08001571 dout("%s: obj %p result %d %llu\n", __func__, obj_request,
1572 obj_request->result, obj_request->length);
1573 /*
Alex Elder8b3e1a52013-01-24 16:13:36 -06001574 * There is no such thing as a successful short write. Set
1575 * it to our originally-requested length.
Sage Weil1b83bef2013-02-25 16:11:12 -08001576 */
1577 obj_request->xferred = obj_request->length;
Alex Elder07741302013-02-05 23:41:50 -06001578 obj_request_done_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001579}
1580
Alex Elderfbfab532013-02-08 09:55:48 -06001581/*
1582 * For a simple stat call there's nothing to do. We'll do more if
1583 * this is part of a write sequence for a layered image.
1584 */
Alex Elderc47f9372013-02-26 14:23:07 -06001585static void rbd_osd_stat_callback(struct rbd_obj_request *obj_request)
Alex Elderfbfab532013-02-08 09:55:48 -06001586{
Alex Elder37206ee2013-02-20 17:32:08 -06001587 dout("%s: obj %p\n", __func__, obj_request);
Alex Elderfbfab532013-02-08 09:55:48 -06001588 obj_request_done_set(obj_request);
1589}
1590
Alex Elderbf0d5f502012-11-22 00:00:08 -06001591static void rbd_osd_req_callback(struct ceph_osd_request *osd_req,
1592 struct ceph_msg *msg)
1593{
1594 struct rbd_obj_request *obj_request = osd_req->r_priv;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001595 u16 opcode;
1596
Alex Elder37206ee2013-02-20 17:32:08 -06001597 dout("%s: osd_req %p msg %p\n", __func__, osd_req, msg);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001598 rbd_assert(osd_req == obj_request->osd_req);
Alex Elder57acbaa2013-02-11 12:33:24 -06001599 if (obj_request_img_data_test(obj_request)) {
1600 rbd_assert(obj_request->img_request);
1601 rbd_assert(obj_request->which != BAD_WHICH);
1602 } else {
1603 rbd_assert(obj_request->which == BAD_WHICH);
1604 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06001605
Sage Weil1b83bef2013-02-25 16:11:12 -08001606 if (osd_req->r_result < 0)
1607 obj_request->result = osd_req->r_result;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001608
Alex Elder0eefd472013-04-19 15:34:50 -05001609 BUG_ON(osd_req->r_num_ops > 2);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001610
Alex Elderc47f9372013-02-26 14:23:07 -06001611 /*
1612 * We support a 64-bit length, but ultimately it has to be
1613 * passed to blk_end_request(), which takes an unsigned int.
1614 */
Sage Weil1b83bef2013-02-25 16:11:12 -08001615 obj_request->xferred = osd_req->r_reply_op_len[0];
Alex Elder8b3e1a52013-01-24 16:13:36 -06001616 rbd_assert(obj_request->xferred < (u64)UINT_MAX);
Alex Elder79528732013-04-03 21:32:51 -05001617 opcode = osd_req->r_ops[0].op;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001618 switch (opcode) {
1619 case CEPH_OSD_OP_READ:
Alex Elderc47f9372013-02-26 14:23:07 -06001620 rbd_osd_read_callback(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001621 break;
1622 case CEPH_OSD_OP_WRITE:
Alex Elderc47f9372013-02-26 14:23:07 -06001623 rbd_osd_write_callback(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001624 break;
Alex Elderfbfab532013-02-08 09:55:48 -06001625 case CEPH_OSD_OP_STAT:
Alex Elderc47f9372013-02-26 14:23:07 -06001626 rbd_osd_stat_callback(obj_request);
Alex Elderfbfab532013-02-08 09:55:48 -06001627 break;
Alex Elder36be9a72013-01-19 00:30:28 -06001628 case CEPH_OSD_OP_CALL:
Alex Elderb8d70032012-11-30 17:53:04 -06001629 case CEPH_OSD_OP_NOTIFY_ACK:
Alex Elder9969ebc2013-01-18 12:31:10 -06001630 case CEPH_OSD_OP_WATCH:
Alex Elderc47f9372013-02-26 14:23:07 -06001631 rbd_osd_trivial_callback(obj_request);
Alex Elder9969ebc2013-01-18 12:31:10 -06001632 break;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001633 default:
1634 rbd_warn(NULL, "%s: unsupported op %hu\n",
1635 obj_request->object_name, (unsigned short) opcode);
1636 break;
1637 }
1638
Alex Elder07741302013-02-05 23:41:50 -06001639 if (obj_request_done_test(obj_request))
Alex Elderbf0d5f502012-11-22 00:00:08 -06001640 rbd_obj_request_complete(obj_request);
1641}
1642
Alex Elder9d4df012013-04-19 15:34:50 -05001643static void rbd_osd_req_format_read(struct rbd_obj_request *obj_request)
Alex Elder430c28c2013-04-03 21:32:51 -05001644{
1645 struct rbd_img_request *img_request = obj_request->img_request;
Alex Elder8c042b02013-04-03 01:28:58 -05001646 struct ceph_osd_request *osd_req = obj_request->osd_req;
Alex Elder9d4df012013-04-19 15:34:50 -05001647 u64 snap_id;
Alex Elder430c28c2013-04-03 21:32:51 -05001648
Alex Elder8c042b02013-04-03 01:28:58 -05001649 rbd_assert(osd_req != NULL);
Alex Elder430c28c2013-04-03 21:32:51 -05001650
Alex Elder9d4df012013-04-19 15:34:50 -05001651 snap_id = img_request ? img_request->snap_id : CEPH_NOSNAP;
Alex Elder8c042b02013-04-03 01:28:58 -05001652 ceph_osdc_build_request(osd_req, obj_request->offset,
Alex Elder9d4df012013-04-19 15:34:50 -05001653 NULL, snap_id, NULL);
1654}
1655
1656static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request)
1657{
1658 struct rbd_img_request *img_request = obj_request->img_request;
1659 struct ceph_osd_request *osd_req = obj_request->osd_req;
1660 struct ceph_snap_context *snapc;
1661 struct timespec mtime = CURRENT_TIME;
1662
1663 rbd_assert(osd_req != NULL);
1664
1665 snapc = img_request ? img_request->snapc : NULL;
1666 ceph_osdc_build_request(osd_req, obj_request->offset,
1667 snapc, CEPH_NOSNAP, &mtime);
Alex Elder430c28c2013-04-03 21:32:51 -05001668}
1669
Alex Elderbf0d5f502012-11-22 00:00:08 -06001670static struct ceph_osd_request *rbd_osd_req_create(
1671 struct rbd_device *rbd_dev,
1672 bool write_request,
Alex Elder430c28c2013-04-03 21:32:51 -05001673 struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001674{
Alex Elderbf0d5f502012-11-22 00:00:08 -06001675 struct ceph_snap_context *snapc = NULL;
1676 struct ceph_osd_client *osdc;
1677 struct ceph_osd_request *osd_req;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001678
Alex Elder6365d332013-02-11 12:33:24 -06001679 if (obj_request_img_data_test(obj_request)) {
1680 struct rbd_img_request *img_request = obj_request->img_request;
1681
Alex Elder0c425242013-02-08 09:55:49 -06001682 rbd_assert(write_request ==
1683 img_request_write_test(img_request));
1684 if (write_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001685 snapc = img_request->snapc;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001686 }
1687
1688 /* Allocate and initialize the request, for the single op */
1689
1690 osdc = &rbd_dev->rbd_client->client->osdc;
1691 osd_req = ceph_osdc_alloc_request(osdc, snapc, 1, false, GFP_ATOMIC);
1692 if (!osd_req)
1693 return NULL; /* ENOMEM */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001694
Alex Elder430c28c2013-04-03 21:32:51 -05001695 if (write_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001696 osd_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK;
Alex Elder430c28c2013-04-03 21:32:51 -05001697 else
Alex Elderbf0d5f502012-11-22 00:00:08 -06001698 osd_req->r_flags = CEPH_OSD_FLAG_READ;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001699
1700 osd_req->r_callback = rbd_osd_req_callback;
1701 osd_req->r_priv = obj_request;
1702
1703 osd_req->r_oid_len = strlen(obj_request->object_name);
1704 rbd_assert(osd_req->r_oid_len < sizeof (osd_req->r_oid));
1705 memcpy(osd_req->r_oid, obj_request->object_name, osd_req->r_oid_len);
1706
1707 osd_req->r_file_layout = rbd_dev->layout; /* struct */
1708
Alex Elderbf0d5f502012-11-22 00:00:08 -06001709 return osd_req;
1710}
1711
Alex Elder0eefd472013-04-19 15:34:50 -05001712/*
1713 * Create a copyup osd request based on the information in the
1714 * object request supplied. A copyup request has two osd ops,
1715 * a copyup method call, and a "normal" write request.
1716 */
1717static struct ceph_osd_request *
1718rbd_osd_req_create_copyup(struct rbd_obj_request *obj_request)
1719{
1720 struct rbd_img_request *img_request;
1721 struct ceph_snap_context *snapc;
1722 struct rbd_device *rbd_dev;
1723 struct ceph_osd_client *osdc;
1724 struct ceph_osd_request *osd_req;
1725
1726 rbd_assert(obj_request_img_data_test(obj_request));
1727 img_request = obj_request->img_request;
1728 rbd_assert(img_request);
1729 rbd_assert(img_request_write_test(img_request));
1730
1731 /* Allocate and initialize the request, for the two ops */
1732
1733 snapc = img_request->snapc;
1734 rbd_dev = img_request->rbd_dev;
1735 osdc = &rbd_dev->rbd_client->client->osdc;
1736 osd_req = ceph_osdc_alloc_request(osdc, snapc, 2, false, GFP_ATOMIC);
1737 if (!osd_req)
1738 return NULL; /* ENOMEM */
1739
1740 osd_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK;
1741 osd_req->r_callback = rbd_osd_req_callback;
1742 osd_req->r_priv = obj_request;
1743
1744 osd_req->r_oid_len = strlen(obj_request->object_name);
1745 rbd_assert(osd_req->r_oid_len < sizeof (osd_req->r_oid));
1746 memcpy(osd_req->r_oid, obj_request->object_name, osd_req->r_oid_len);
1747
1748 osd_req->r_file_layout = rbd_dev->layout; /* struct */
1749
1750 return osd_req;
1751}
1752
1753
Alex Elderbf0d5f502012-11-22 00:00:08 -06001754static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req)
1755{
1756 ceph_osdc_put_request(osd_req);
1757}
1758
1759/* object_name is assumed to be a non-null pointer and NUL-terminated */
1760
1761static struct rbd_obj_request *rbd_obj_request_create(const char *object_name,
1762 u64 offset, u64 length,
1763 enum obj_request_type type)
1764{
1765 struct rbd_obj_request *obj_request;
1766 size_t size;
1767 char *name;
1768
1769 rbd_assert(obj_request_type_valid(type));
1770
1771 size = strlen(object_name) + 1;
Alex Elderf907ad52013-05-01 12:43:03 -05001772 name = kmalloc(size, GFP_KERNEL);
1773 if (!name)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001774 return NULL;
1775
Alex Elder868311b2013-05-01 12:43:03 -05001776 obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_KERNEL);
Alex Elderf907ad52013-05-01 12:43:03 -05001777 if (!obj_request) {
1778 kfree(name);
1779 return NULL;
1780 }
1781
Alex Elderbf0d5f502012-11-22 00:00:08 -06001782 obj_request->object_name = memcpy(name, object_name, size);
1783 obj_request->offset = offset;
1784 obj_request->length = length;
Alex Elder926f9b32013-02-11 12:33:24 -06001785 obj_request->flags = 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001786 obj_request->which = BAD_WHICH;
1787 obj_request->type = type;
1788 INIT_LIST_HEAD(&obj_request->links);
Alex Elder788e2df2013-01-17 12:25:27 -06001789 init_completion(&obj_request->completion);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001790 kref_init(&obj_request->kref);
1791
Alex Elder37206ee2013-02-20 17:32:08 -06001792 dout("%s: \"%s\" %llu/%llu %d -> obj %p\n", __func__, object_name,
1793 offset, length, (int)type, obj_request);
1794
Alex Elderbf0d5f502012-11-22 00:00:08 -06001795 return obj_request;
1796}
1797
1798static void rbd_obj_request_destroy(struct kref *kref)
1799{
1800 struct rbd_obj_request *obj_request;
1801
1802 obj_request = container_of(kref, struct rbd_obj_request, kref);
1803
Alex Elder37206ee2013-02-20 17:32:08 -06001804 dout("%s: obj %p\n", __func__, obj_request);
1805
Alex Elderbf0d5f502012-11-22 00:00:08 -06001806 rbd_assert(obj_request->img_request == NULL);
1807 rbd_assert(obj_request->which == BAD_WHICH);
1808
1809 if (obj_request->osd_req)
1810 rbd_osd_req_destroy(obj_request->osd_req);
1811
1812 rbd_assert(obj_request_type_valid(obj_request->type));
1813 switch (obj_request->type) {
Alex Elder9969ebc2013-01-18 12:31:10 -06001814 case OBJ_REQUEST_NODATA:
1815 break; /* Nothing to do */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001816 case OBJ_REQUEST_BIO:
1817 if (obj_request->bio_list)
1818 bio_chain_put(obj_request->bio_list);
1819 break;
Alex Elder788e2df2013-01-17 12:25:27 -06001820 case OBJ_REQUEST_PAGES:
1821 if (obj_request->pages)
1822 ceph_release_page_vector(obj_request->pages,
1823 obj_request->page_count);
1824 break;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001825 }
1826
Alex Elderf907ad52013-05-01 12:43:03 -05001827 kfree(obj_request->object_name);
Alex Elder868311b2013-05-01 12:43:03 -05001828 obj_request->object_name = NULL;
1829 kmem_cache_free(rbd_obj_request_cache, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001830}
1831
1832/*
1833 * Caller is responsible for filling in the list of object requests
1834 * that comprises the image request, and the Linux request pointer
1835 * (if there is one).
1836 */
Alex Eldercc344fa2013-02-19 12:25:56 -06001837static struct rbd_img_request *rbd_img_request_create(
1838 struct rbd_device *rbd_dev,
Alex Elderbf0d5f502012-11-22 00:00:08 -06001839 u64 offset, u64 length,
Alex Elder9849e982013-01-24 16:13:36 -06001840 bool write_request,
1841 bool child_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001842{
1843 struct rbd_img_request *img_request;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001844
Alex Elder1c2a9df2013-05-01 12:43:03 -05001845 img_request = kmem_cache_alloc(rbd_img_request_cache, GFP_ATOMIC);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001846 if (!img_request)
1847 return NULL;
1848
1849 if (write_request) {
1850 down_read(&rbd_dev->header_rwsem);
Alex Elder812164f82013-04-30 00:44:32 -05001851 ceph_get_snap_context(rbd_dev->header.snapc);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001852 up_read(&rbd_dev->header_rwsem);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001853 }
1854
1855 img_request->rq = NULL;
1856 img_request->rbd_dev = rbd_dev;
1857 img_request->offset = offset;
1858 img_request->length = length;
Alex Elder0c425242013-02-08 09:55:49 -06001859 img_request->flags = 0;
1860 if (write_request) {
1861 img_request_write_set(img_request);
Alex Elder468521c2013-04-26 09:43:47 -05001862 img_request->snapc = rbd_dev->header.snapc;
Alex Elder0c425242013-02-08 09:55:49 -06001863 } else {
Alex Elderbf0d5f502012-11-22 00:00:08 -06001864 img_request->snap_id = rbd_dev->spec->snap_id;
Alex Elder0c425242013-02-08 09:55:49 -06001865 }
Alex Elder9849e982013-01-24 16:13:36 -06001866 if (child_request)
1867 img_request_child_set(img_request);
Alex Elderd0b2e942013-01-24 16:13:36 -06001868 if (rbd_dev->parent_spec)
1869 img_request_layered_set(img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001870 spin_lock_init(&img_request->completion_lock);
1871 img_request->next_completion = 0;
1872 img_request->callback = NULL;
Alex Eldera5a337d2013-01-24 16:13:36 -06001873 img_request->result = 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001874 img_request->obj_request_count = 0;
1875 INIT_LIST_HEAD(&img_request->obj_requests);
1876 kref_init(&img_request->kref);
1877
1878 rbd_img_request_get(img_request); /* Avoid a warning */
1879 rbd_img_request_put(img_request); /* TEMPORARY */
1880
Alex Elder37206ee2013-02-20 17:32:08 -06001881 dout("%s: rbd_dev %p %s %llu/%llu -> img %p\n", __func__, rbd_dev,
1882 write_request ? "write" : "read", offset, length,
1883 img_request);
1884
Alex Elderbf0d5f502012-11-22 00:00:08 -06001885 return img_request;
1886}
1887
1888static void rbd_img_request_destroy(struct kref *kref)
1889{
1890 struct rbd_img_request *img_request;
1891 struct rbd_obj_request *obj_request;
1892 struct rbd_obj_request *next_obj_request;
1893
1894 img_request = container_of(kref, struct rbd_img_request, kref);
1895
Alex Elder37206ee2013-02-20 17:32:08 -06001896 dout("%s: img %p\n", __func__, img_request);
1897
Alex Elderbf0d5f502012-11-22 00:00:08 -06001898 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
1899 rbd_img_obj_request_del(img_request, obj_request);
Alex Elder25dcf952013-01-25 17:08:55 -06001900 rbd_assert(img_request->obj_request_count == 0);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001901
Alex Elder0c425242013-02-08 09:55:49 -06001902 if (img_request_write_test(img_request))
Alex Elder812164f82013-04-30 00:44:32 -05001903 ceph_put_snap_context(img_request->snapc);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001904
Alex Elder8b3e1a52013-01-24 16:13:36 -06001905 if (img_request_child_test(img_request))
1906 rbd_obj_request_put(img_request->obj_request);
1907
Alex Elder1c2a9df2013-05-01 12:43:03 -05001908 kmem_cache_free(rbd_img_request_cache, img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001909}
1910
Alex Elder12178572013-02-08 09:55:49 -06001911static bool rbd_img_obj_end_request(struct rbd_obj_request *obj_request)
1912{
Alex Elder6365d332013-02-11 12:33:24 -06001913 struct rbd_img_request *img_request;
Alex Elder12178572013-02-08 09:55:49 -06001914 unsigned int xferred;
1915 int result;
Alex Elder8b3e1a52013-01-24 16:13:36 -06001916 bool more;
Alex Elder12178572013-02-08 09:55:49 -06001917
Alex Elder6365d332013-02-11 12:33:24 -06001918 rbd_assert(obj_request_img_data_test(obj_request));
1919 img_request = obj_request->img_request;
1920
Alex Elder12178572013-02-08 09:55:49 -06001921 rbd_assert(obj_request->xferred <= (u64)UINT_MAX);
1922 xferred = (unsigned int)obj_request->xferred;
1923 result = obj_request->result;
1924 if (result) {
1925 struct rbd_device *rbd_dev = img_request->rbd_dev;
1926
1927 rbd_warn(rbd_dev, "%s %llx at %llx (%llx)\n",
1928 img_request_write_test(img_request) ? "write" : "read",
1929 obj_request->length, obj_request->img_offset,
1930 obj_request->offset);
1931 rbd_warn(rbd_dev, " result %d xferred %x\n",
1932 result, xferred);
1933 if (!img_request->result)
1934 img_request->result = result;
1935 }
1936
Alex Elderf1a47392013-04-19 15:34:50 -05001937 /* Image object requests don't own their page array */
1938
1939 if (obj_request->type == OBJ_REQUEST_PAGES) {
1940 obj_request->pages = NULL;
1941 obj_request->page_count = 0;
1942 }
1943
Alex Elder8b3e1a52013-01-24 16:13:36 -06001944 if (img_request_child_test(img_request)) {
1945 rbd_assert(img_request->obj_request != NULL);
1946 more = obj_request->which < img_request->obj_request_count - 1;
1947 } else {
1948 rbd_assert(img_request->rq != NULL);
1949 more = blk_end_request(img_request->rq, result, xferred);
1950 }
1951
1952 return more;
Alex Elder12178572013-02-08 09:55:49 -06001953}
1954
Alex Elder21692382013-04-05 01:27:12 -05001955static void rbd_img_obj_callback(struct rbd_obj_request *obj_request)
1956{
1957 struct rbd_img_request *img_request;
1958 u32 which = obj_request->which;
1959 bool more = true;
1960
Alex Elder6365d332013-02-11 12:33:24 -06001961 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elder21692382013-04-05 01:27:12 -05001962 img_request = obj_request->img_request;
1963
1964 dout("%s: img %p obj %p\n", __func__, img_request, obj_request);
1965 rbd_assert(img_request != NULL);
Alex Elder21692382013-04-05 01:27:12 -05001966 rbd_assert(img_request->obj_request_count > 0);
1967 rbd_assert(which != BAD_WHICH);
1968 rbd_assert(which < img_request->obj_request_count);
1969 rbd_assert(which >= img_request->next_completion);
1970
1971 spin_lock_irq(&img_request->completion_lock);
1972 if (which != img_request->next_completion)
1973 goto out;
1974
1975 for_each_obj_request_from(img_request, obj_request) {
Alex Elder21692382013-04-05 01:27:12 -05001976 rbd_assert(more);
1977 rbd_assert(which < img_request->obj_request_count);
1978
1979 if (!obj_request_done_test(obj_request))
1980 break;
Alex Elder12178572013-02-08 09:55:49 -06001981 more = rbd_img_obj_end_request(obj_request);
Alex Elder21692382013-04-05 01:27:12 -05001982 which++;
1983 }
1984
1985 rbd_assert(more ^ (which == img_request->obj_request_count));
1986 img_request->next_completion = which;
1987out:
1988 spin_unlock_irq(&img_request->completion_lock);
1989
1990 if (!more)
1991 rbd_img_request_complete(img_request);
1992}
1993
Alex Elderf1a47392013-04-19 15:34:50 -05001994/*
1995 * Split up an image request into one or more object requests, each
1996 * to a different object. The "type" parameter indicates whether
1997 * "data_desc" is the pointer to the head of a list of bio
1998 * structures, or the base of a page array. In either case this
1999 * function assumes data_desc describes memory sufficient to hold
2000 * all data described by the image request.
2001 */
2002static int rbd_img_request_fill(struct rbd_img_request *img_request,
2003 enum obj_request_type type,
2004 void *data_desc)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002005{
2006 struct rbd_device *rbd_dev = img_request->rbd_dev;
2007 struct rbd_obj_request *obj_request = NULL;
2008 struct rbd_obj_request *next_obj_request;
Alex Elder0c425242013-02-08 09:55:49 -06002009 bool write_request = img_request_write_test(img_request);
Alex Elderf1a47392013-04-19 15:34:50 -05002010 struct bio *bio_list;
2011 unsigned int bio_offset = 0;
2012 struct page **pages;
Alex Elder7da22d22013-01-24 16:13:36 -06002013 u64 img_offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002014 u64 resid;
2015 u16 opcode;
2016
Alex Elderf1a47392013-04-19 15:34:50 -05002017 dout("%s: img %p type %d data_desc %p\n", __func__, img_request,
2018 (int)type, data_desc);
Alex Elder37206ee2013-02-20 17:32:08 -06002019
Alex Elder430c28c2013-04-03 21:32:51 -05002020 opcode = write_request ? CEPH_OSD_OP_WRITE : CEPH_OSD_OP_READ;
Alex Elder7da22d22013-01-24 16:13:36 -06002021 img_offset = img_request->offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002022 resid = img_request->length;
Alex Elder4dda41d2013-02-20 21:59:33 -06002023 rbd_assert(resid > 0);
Alex Elderf1a47392013-04-19 15:34:50 -05002024
2025 if (type == OBJ_REQUEST_BIO) {
2026 bio_list = data_desc;
2027 rbd_assert(img_offset == bio_list->bi_sector << SECTOR_SHIFT);
2028 } else {
2029 rbd_assert(type == OBJ_REQUEST_PAGES);
2030 pages = data_desc;
2031 }
2032
Alex Elderbf0d5f502012-11-22 00:00:08 -06002033 while (resid) {
Alex Elder2fa12322013-04-05 01:27:12 -05002034 struct ceph_osd_request *osd_req;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002035 const char *object_name;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002036 u64 offset;
2037 u64 length;
2038
Alex Elder7da22d22013-01-24 16:13:36 -06002039 object_name = rbd_segment_name(rbd_dev, img_offset);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002040 if (!object_name)
2041 goto out_unwind;
Alex Elder7da22d22013-01-24 16:13:36 -06002042 offset = rbd_segment_offset(rbd_dev, img_offset);
2043 length = rbd_segment_length(rbd_dev, img_offset, resid);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002044 obj_request = rbd_obj_request_create(object_name,
Alex Elderf1a47392013-04-19 15:34:50 -05002045 offset, length, type);
Alex Elder78c2a442013-05-01 12:43:04 -05002046 /* object request has its own copy of the object name */
2047 rbd_segment_name_free(object_name);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002048 if (!obj_request)
2049 goto out_unwind;
2050
Alex Elderf1a47392013-04-19 15:34:50 -05002051 if (type == OBJ_REQUEST_BIO) {
2052 unsigned int clone_size;
2053
2054 rbd_assert(length <= (u64)UINT_MAX);
2055 clone_size = (unsigned int)length;
2056 obj_request->bio_list =
2057 bio_chain_clone_range(&bio_list,
2058 &bio_offset,
2059 clone_size,
2060 GFP_ATOMIC);
2061 if (!obj_request->bio_list)
2062 goto out_partial;
2063 } else {
2064 unsigned int page_count;
2065
2066 obj_request->pages = pages;
2067 page_count = (u32)calc_pages_for(offset, length);
2068 obj_request->page_count = page_count;
2069 if ((offset + length) & ~PAGE_MASK)
2070 page_count--; /* more on last page */
2071 pages += page_count;
2072 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06002073
Alex Elder2fa12322013-04-05 01:27:12 -05002074 osd_req = rbd_osd_req_create(rbd_dev, write_request,
2075 obj_request);
2076 if (!osd_req)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002077 goto out_partial;
Alex Elder2fa12322013-04-05 01:27:12 -05002078 obj_request->osd_req = osd_req;
Alex Elder21692382013-04-05 01:27:12 -05002079 obj_request->callback = rbd_img_obj_callback;
Alex Elder430c28c2013-04-03 21:32:51 -05002080
Alex Elder2fa12322013-04-05 01:27:12 -05002081 osd_req_op_extent_init(osd_req, 0, opcode, offset, length,
2082 0, 0);
Alex Elderf1a47392013-04-19 15:34:50 -05002083 if (type == OBJ_REQUEST_BIO)
2084 osd_req_op_extent_osd_data_bio(osd_req, 0,
2085 obj_request->bio_list, length);
2086 else
2087 osd_req_op_extent_osd_data_pages(osd_req, 0,
2088 obj_request->pages, length,
2089 offset & ~PAGE_MASK, false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05002090
2091 if (write_request)
2092 rbd_osd_req_format_write(obj_request);
2093 else
2094 rbd_osd_req_format_read(obj_request);
Alex Elder430c28c2013-04-03 21:32:51 -05002095
Alex Elder7da22d22013-01-24 16:13:36 -06002096 obj_request->img_offset = img_offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002097 rbd_img_obj_request_add(img_request, obj_request);
2098
Alex Elder7da22d22013-01-24 16:13:36 -06002099 img_offset += length;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002100 resid -= length;
2101 }
2102
2103 return 0;
2104
2105out_partial:
2106 rbd_obj_request_put(obj_request);
2107out_unwind:
2108 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
2109 rbd_obj_request_put(obj_request);
2110
2111 return -ENOMEM;
2112}
2113
Alex Elder3d7efd12013-04-19 15:34:50 -05002114static void
Alex Elder0eefd472013-04-19 15:34:50 -05002115rbd_img_obj_copyup_callback(struct rbd_obj_request *obj_request)
2116{
2117 struct rbd_img_request *img_request;
2118 struct rbd_device *rbd_dev;
2119 u64 length;
2120 u32 page_count;
2121
2122 rbd_assert(obj_request->type == OBJ_REQUEST_BIO);
2123 rbd_assert(obj_request_img_data_test(obj_request));
2124 img_request = obj_request->img_request;
2125 rbd_assert(img_request);
2126
2127 rbd_dev = img_request->rbd_dev;
2128 rbd_assert(rbd_dev);
2129 length = (u64)1 << rbd_dev->header.obj_order;
2130 page_count = (u32)calc_pages_for(0, length);
2131
2132 rbd_assert(obj_request->copyup_pages);
2133 ceph_release_page_vector(obj_request->copyup_pages, page_count);
2134 obj_request->copyup_pages = NULL;
2135
2136 /*
2137 * We want the transfer count to reflect the size of the
2138 * original write request. There is no such thing as a
2139 * successful short write, so if the request was successful
2140 * we can just set it to the originally-requested length.
2141 */
2142 if (!obj_request->result)
2143 obj_request->xferred = obj_request->length;
2144
2145 /* Finish up with the normal image object callback */
2146
2147 rbd_img_obj_callback(obj_request);
2148}
2149
2150static void
Alex Elder3d7efd12013-04-19 15:34:50 -05002151rbd_img_obj_parent_read_full_callback(struct rbd_img_request *img_request)
2152{
2153 struct rbd_obj_request *orig_request;
Alex Elder0eefd472013-04-19 15:34:50 -05002154 struct ceph_osd_request *osd_req;
2155 struct ceph_osd_client *osdc;
2156 struct rbd_device *rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002157 struct page **pages;
Alex Elder3d7efd12013-04-19 15:34:50 -05002158 int result;
2159 u64 obj_size;
2160 u64 xferred;
2161
2162 rbd_assert(img_request_child_test(img_request));
2163
2164 /* First get what we need from the image request */
2165
2166 pages = img_request->copyup_pages;
2167 rbd_assert(pages != NULL);
2168 img_request->copyup_pages = NULL;
2169
2170 orig_request = img_request->obj_request;
2171 rbd_assert(orig_request != NULL);
Alex Elder0eefd472013-04-19 15:34:50 -05002172 rbd_assert(orig_request->type == OBJ_REQUEST_BIO);
Alex Elder3d7efd12013-04-19 15:34:50 -05002173 result = img_request->result;
2174 obj_size = img_request->length;
2175 xferred = img_request->xferred;
2176
Alex Elder0eefd472013-04-19 15:34:50 -05002177 rbd_dev = img_request->rbd_dev;
2178 rbd_assert(rbd_dev);
2179 rbd_assert(obj_size == (u64)1 << rbd_dev->header.obj_order);
2180
Alex Elder3d7efd12013-04-19 15:34:50 -05002181 rbd_img_request_put(img_request);
2182
Alex Elder0eefd472013-04-19 15:34:50 -05002183 if (result)
2184 goto out_err;
Alex Elder3d7efd12013-04-19 15:34:50 -05002185
Alex Elder0eefd472013-04-19 15:34:50 -05002186 /* Allocate the new copyup osd request for the original request */
Alex Elder3d7efd12013-04-19 15:34:50 -05002187
Alex Elder0eefd472013-04-19 15:34:50 -05002188 result = -ENOMEM;
2189 rbd_assert(!orig_request->osd_req);
2190 osd_req = rbd_osd_req_create_copyup(orig_request);
2191 if (!osd_req)
2192 goto out_err;
2193 orig_request->osd_req = osd_req;
2194 orig_request->copyup_pages = pages;
Alex Elder3d7efd12013-04-19 15:34:50 -05002195
Alex Elder0eefd472013-04-19 15:34:50 -05002196 /* Initialize the copyup op */
2197
2198 osd_req_op_cls_init(osd_req, 0, CEPH_OSD_OP_CALL, "rbd", "copyup");
2199 osd_req_op_cls_request_data_pages(osd_req, 0, pages, obj_size, 0,
2200 false, false);
2201
2202 /* Then the original write request op */
2203
2204 osd_req_op_extent_init(osd_req, 1, CEPH_OSD_OP_WRITE,
2205 orig_request->offset,
2206 orig_request->length, 0, 0);
2207 osd_req_op_extent_osd_data_bio(osd_req, 1, orig_request->bio_list,
2208 orig_request->length);
2209
2210 rbd_osd_req_format_write(orig_request);
2211
2212 /* All set, send it off. */
2213
2214 orig_request->callback = rbd_img_obj_copyup_callback;
2215 osdc = &rbd_dev->rbd_client->client->osdc;
2216 result = rbd_obj_request_submit(osdc, orig_request);
2217 if (!result)
2218 return;
2219out_err:
2220 /* Record the error code and complete the request */
2221
2222 orig_request->result = result;
2223 orig_request->xferred = 0;
2224 obj_request_done_set(orig_request);
2225 rbd_obj_request_complete(orig_request);
Alex Elder3d7efd12013-04-19 15:34:50 -05002226}
2227
2228/*
2229 * Read from the parent image the range of data that covers the
2230 * entire target of the given object request. This is used for
2231 * satisfying a layered image write request when the target of an
2232 * object request from the image request does not exist.
2233 *
2234 * A page array big enough to hold the returned data is allocated
2235 * and supplied to rbd_img_request_fill() as the "data descriptor."
2236 * When the read completes, this page array will be transferred to
2237 * the original object request for the copyup operation.
2238 *
2239 * If an error occurs, record it as the result of the original
2240 * object request and mark it done so it gets completed.
2241 */
2242static int rbd_img_obj_parent_read_full(struct rbd_obj_request *obj_request)
2243{
2244 struct rbd_img_request *img_request = NULL;
2245 struct rbd_img_request *parent_request = NULL;
2246 struct rbd_device *rbd_dev;
2247 u64 img_offset;
2248 u64 length;
2249 struct page **pages = NULL;
2250 u32 page_count;
2251 int result;
2252
2253 rbd_assert(obj_request_img_data_test(obj_request));
2254 rbd_assert(obj_request->type == OBJ_REQUEST_BIO);
2255
2256 img_request = obj_request->img_request;
2257 rbd_assert(img_request != NULL);
2258 rbd_dev = img_request->rbd_dev;
2259 rbd_assert(rbd_dev->parent != NULL);
2260
2261 /*
Alex Elder0eefd472013-04-19 15:34:50 -05002262 * First things first. The original osd request is of no
2263 * use to use any more, we'll need a new one that can hold
2264 * the two ops in a copyup request. We'll get that later,
2265 * but for now we can release the old one.
2266 */
2267 rbd_osd_req_destroy(obj_request->osd_req);
2268 obj_request->osd_req = NULL;
2269
2270 /*
Alex Elder3d7efd12013-04-19 15:34:50 -05002271 * Determine the byte range covered by the object in the
2272 * child image to which the original request was to be sent.
2273 */
2274 img_offset = obj_request->img_offset - obj_request->offset;
2275 length = (u64)1 << rbd_dev->header.obj_order;
2276
2277 /*
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002278 * There is no defined parent data beyond the parent
2279 * overlap, so limit what we read at that boundary if
2280 * necessary.
2281 */
2282 if (img_offset + length > rbd_dev->parent_overlap) {
2283 rbd_assert(img_offset < rbd_dev->parent_overlap);
2284 length = rbd_dev->parent_overlap - img_offset;
2285 }
2286
2287 /*
Alex Elder3d7efd12013-04-19 15:34:50 -05002288 * Allocate a page array big enough to receive the data read
2289 * from the parent.
2290 */
2291 page_count = (u32)calc_pages_for(0, length);
2292 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2293 if (IS_ERR(pages)) {
2294 result = PTR_ERR(pages);
2295 pages = NULL;
2296 goto out_err;
2297 }
2298
2299 result = -ENOMEM;
2300 parent_request = rbd_img_request_create(rbd_dev->parent,
2301 img_offset, length,
2302 false, true);
2303 if (!parent_request)
2304 goto out_err;
2305 rbd_obj_request_get(obj_request);
2306 parent_request->obj_request = obj_request;
2307
2308 result = rbd_img_request_fill(parent_request, OBJ_REQUEST_PAGES, pages);
2309 if (result)
2310 goto out_err;
2311 parent_request->copyup_pages = pages;
2312
2313 parent_request->callback = rbd_img_obj_parent_read_full_callback;
2314 result = rbd_img_request_submit(parent_request);
2315 if (!result)
2316 return 0;
2317
2318 parent_request->copyup_pages = NULL;
2319 parent_request->obj_request = NULL;
2320 rbd_obj_request_put(obj_request);
2321out_err:
2322 if (pages)
2323 ceph_release_page_vector(pages, page_count);
2324 if (parent_request)
2325 rbd_img_request_put(parent_request);
2326 obj_request->result = result;
2327 obj_request->xferred = 0;
2328 obj_request_done_set(obj_request);
2329
2330 return result;
2331}
2332
Alex Elderc5b5ef62013-02-11 12:33:24 -06002333static void rbd_img_obj_exists_callback(struct rbd_obj_request *obj_request)
2334{
Alex Elderc5b5ef62013-02-11 12:33:24 -06002335 struct rbd_obj_request *orig_request;
2336 int result;
2337
2338 rbd_assert(!obj_request_img_data_test(obj_request));
2339
2340 /*
2341 * All we need from the object request is the original
2342 * request and the result of the STAT op. Grab those, then
2343 * we're done with the request.
2344 */
2345 orig_request = obj_request->obj_request;
2346 obj_request->obj_request = NULL;
2347 rbd_assert(orig_request);
2348 rbd_assert(orig_request->img_request);
2349
2350 result = obj_request->result;
2351 obj_request->result = 0;
2352
2353 dout("%s: obj %p for obj %p result %d %llu/%llu\n", __func__,
2354 obj_request, orig_request, result,
2355 obj_request->xferred, obj_request->length);
2356 rbd_obj_request_put(obj_request);
2357
2358 rbd_assert(orig_request);
2359 rbd_assert(orig_request->img_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002360
2361 /*
2362 * Our only purpose here is to determine whether the object
2363 * exists, and we don't want to treat the non-existence as
2364 * an error. If something else comes back, transfer the
2365 * error to the original request and complete it now.
2366 */
2367 if (!result) {
2368 obj_request_existence_set(orig_request, true);
2369 } else if (result == -ENOENT) {
2370 obj_request_existence_set(orig_request, false);
2371 } else if (result) {
2372 orig_request->result = result;
Alex Elder3d7efd12013-04-19 15:34:50 -05002373 goto out;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002374 }
2375
2376 /*
2377 * Resubmit the original request now that we have recorded
2378 * whether the target object exists.
2379 */
Alex Elderb454e362013-04-19 15:34:50 -05002380 orig_request->result = rbd_img_obj_request_submit(orig_request);
Alex Elder3d7efd12013-04-19 15:34:50 -05002381out:
Alex Elderc5b5ef62013-02-11 12:33:24 -06002382 if (orig_request->result)
2383 rbd_obj_request_complete(orig_request);
2384 rbd_obj_request_put(orig_request);
2385}
2386
2387static int rbd_img_obj_exists_submit(struct rbd_obj_request *obj_request)
2388{
2389 struct rbd_obj_request *stat_request;
2390 struct rbd_device *rbd_dev;
2391 struct ceph_osd_client *osdc;
2392 struct page **pages = NULL;
2393 u32 page_count;
2394 size_t size;
2395 int ret;
2396
2397 /*
2398 * The response data for a STAT call consists of:
2399 * le64 length;
2400 * struct {
2401 * le32 tv_sec;
2402 * le32 tv_nsec;
2403 * } mtime;
2404 */
2405 size = sizeof (__le64) + sizeof (__le32) + sizeof (__le32);
2406 page_count = (u32)calc_pages_for(0, size);
2407 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2408 if (IS_ERR(pages))
2409 return PTR_ERR(pages);
2410
2411 ret = -ENOMEM;
2412 stat_request = rbd_obj_request_create(obj_request->object_name, 0, 0,
2413 OBJ_REQUEST_PAGES);
2414 if (!stat_request)
2415 goto out;
2416
2417 rbd_obj_request_get(obj_request);
2418 stat_request->obj_request = obj_request;
2419 stat_request->pages = pages;
2420 stat_request->page_count = page_count;
2421
2422 rbd_assert(obj_request->img_request);
2423 rbd_dev = obj_request->img_request->rbd_dev;
2424 stat_request->osd_req = rbd_osd_req_create(rbd_dev, false,
2425 stat_request);
2426 if (!stat_request->osd_req)
2427 goto out;
2428 stat_request->callback = rbd_img_obj_exists_callback;
2429
2430 osd_req_op_init(stat_request->osd_req, 0, CEPH_OSD_OP_STAT);
2431 osd_req_op_raw_data_in_pages(stat_request->osd_req, 0, pages, size, 0,
2432 false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05002433 rbd_osd_req_format_read(stat_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002434
2435 osdc = &rbd_dev->rbd_client->client->osdc;
2436 ret = rbd_obj_request_submit(osdc, stat_request);
2437out:
2438 if (ret)
2439 rbd_obj_request_put(obj_request);
2440
2441 return ret;
2442}
2443
Alex Elderb454e362013-04-19 15:34:50 -05002444static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request)
2445{
2446 struct rbd_img_request *img_request;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002447 struct rbd_device *rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002448 bool known;
Alex Elderb454e362013-04-19 15:34:50 -05002449
2450 rbd_assert(obj_request_img_data_test(obj_request));
2451
2452 img_request = obj_request->img_request;
2453 rbd_assert(img_request);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002454 rbd_dev = img_request->rbd_dev;
Alex Elderb454e362013-04-19 15:34:50 -05002455
Alex Elderb454e362013-04-19 15:34:50 -05002456 /*
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002457 * Only writes to layered images need special handling.
2458 * Reads and non-layered writes are simple object requests.
2459 * Layered writes that start beyond the end of the overlap
2460 * with the parent have no parent data, so they too are
2461 * simple object requests. Finally, if the target object is
2462 * known to already exist, its parent data has already been
2463 * copied, so a write to the object can also be handled as a
2464 * simple object request.
Alex Elderb454e362013-04-19 15:34:50 -05002465 */
2466 if (!img_request_write_test(img_request) ||
2467 !img_request_layered_test(img_request) ||
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002468 rbd_dev->parent_overlap <= obj_request->img_offset ||
Alex Elder3d7efd12013-04-19 15:34:50 -05002469 ((known = obj_request_known_test(obj_request)) &&
2470 obj_request_exists_test(obj_request))) {
Alex Elderb454e362013-04-19 15:34:50 -05002471
2472 struct rbd_device *rbd_dev;
2473 struct ceph_osd_client *osdc;
2474
2475 rbd_dev = obj_request->img_request->rbd_dev;
2476 osdc = &rbd_dev->rbd_client->client->osdc;
2477
2478 return rbd_obj_request_submit(osdc, obj_request);
2479 }
2480
2481 /*
Alex Elder3d7efd12013-04-19 15:34:50 -05002482 * It's a layered write. The target object might exist but
2483 * we may not know that yet. If we know it doesn't exist,
2484 * start by reading the data for the full target object from
2485 * the parent so we can use it for a copyup to the target.
Alex Elderb454e362013-04-19 15:34:50 -05002486 */
Alex Elder3d7efd12013-04-19 15:34:50 -05002487 if (known)
2488 return rbd_img_obj_parent_read_full(obj_request);
2489
2490 /* We don't know whether the target exists. Go find out. */
Alex Elderb454e362013-04-19 15:34:50 -05002491
2492 return rbd_img_obj_exists_submit(obj_request);
2493}
2494
Alex Elderbf0d5f502012-11-22 00:00:08 -06002495static int rbd_img_request_submit(struct rbd_img_request *img_request)
2496{
Alex Elderbf0d5f502012-11-22 00:00:08 -06002497 struct rbd_obj_request *obj_request;
Alex Elder46faeed2013-04-10 17:47:46 -05002498 struct rbd_obj_request *next_obj_request;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002499
Alex Elder37206ee2013-02-20 17:32:08 -06002500 dout("%s: img %p\n", __func__, img_request);
Alex Elder46faeed2013-04-10 17:47:46 -05002501 for_each_obj_request_safe(img_request, obj_request, next_obj_request) {
Alex Elderbf0d5f502012-11-22 00:00:08 -06002502 int ret;
2503
Alex Elderb454e362013-04-19 15:34:50 -05002504 ret = rbd_img_obj_request_submit(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002505 if (ret)
2506 return ret;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002507 }
2508
2509 return 0;
2510}
2511
Alex Elder8b3e1a52013-01-24 16:13:36 -06002512static void rbd_img_parent_read_callback(struct rbd_img_request *img_request)
2513{
2514 struct rbd_obj_request *obj_request;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002515 struct rbd_device *rbd_dev;
2516 u64 obj_end;
Alex Elder8b3e1a52013-01-24 16:13:36 -06002517
2518 rbd_assert(img_request_child_test(img_request));
2519
2520 obj_request = img_request->obj_request;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002521 rbd_assert(obj_request);
2522 rbd_assert(obj_request->img_request);
Alex Elder8b3e1a52013-01-24 16:13:36 -06002523
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002524 obj_request->result = img_request->result;
2525 if (obj_request->result)
2526 goto out;
2527
2528 /*
2529 * We need to zero anything beyond the parent overlap
2530 * boundary. Since rbd_img_obj_request_read_callback()
2531 * will zero anything beyond the end of a short read, an
2532 * easy way to do this is to pretend the data from the
2533 * parent came up short--ending at the overlap boundary.
2534 */
2535 rbd_assert(obj_request->img_offset < U64_MAX - obj_request->length);
2536 obj_end = obj_request->img_offset + obj_request->length;
2537 rbd_dev = obj_request->img_request->rbd_dev;
2538 if (obj_end > rbd_dev->parent_overlap) {
2539 u64 xferred = 0;
2540
2541 if (obj_request->img_offset < rbd_dev->parent_overlap)
2542 xferred = rbd_dev->parent_overlap -
2543 obj_request->img_offset;
2544
2545 obj_request->xferred = min(img_request->xferred, xferred);
2546 } else {
2547 obj_request->xferred = img_request->xferred;
2548 }
2549out:
Alex Elderb5b09be2013-05-01 21:37:07 -05002550 rbd_img_request_put(img_request);
Alex Elder8b3e1a52013-01-24 16:13:36 -06002551 rbd_img_obj_request_read_callback(obj_request);
2552 rbd_obj_request_complete(obj_request);
2553}
2554
2555static void rbd_img_parent_read(struct rbd_obj_request *obj_request)
2556{
2557 struct rbd_device *rbd_dev;
2558 struct rbd_img_request *img_request;
2559 int result;
2560
2561 rbd_assert(obj_request_img_data_test(obj_request));
2562 rbd_assert(obj_request->img_request != NULL);
2563 rbd_assert(obj_request->result == (s32) -ENOENT);
2564 rbd_assert(obj_request->type == OBJ_REQUEST_BIO);
2565
2566 rbd_dev = obj_request->img_request->rbd_dev;
2567 rbd_assert(rbd_dev->parent != NULL);
2568 /* rbd_read_finish(obj_request, obj_request->length); */
2569 img_request = rbd_img_request_create(rbd_dev->parent,
2570 obj_request->img_offset,
2571 obj_request->length,
2572 false, true);
2573 result = -ENOMEM;
2574 if (!img_request)
2575 goto out_err;
2576
2577 rbd_obj_request_get(obj_request);
2578 img_request->obj_request = obj_request;
2579
Alex Elderf1a47392013-04-19 15:34:50 -05002580 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
2581 obj_request->bio_list);
Alex Elder8b3e1a52013-01-24 16:13:36 -06002582 if (result)
2583 goto out_err;
2584
2585 img_request->callback = rbd_img_parent_read_callback;
2586 result = rbd_img_request_submit(img_request);
2587 if (result)
2588 goto out_err;
2589
2590 return;
2591out_err:
2592 if (img_request)
2593 rbd_img_request_put(img_request);
2594 obj_request->result = result;
2595 obj_request->xferred = 0;
2596 obj_request_done_set(obj_request);
2597}
2598
Alex Eldercc4a38bd2013-04-30 00:44:33 -05002599static int rbd_obj_notify_ack(struct rbd_device *rbd_dev, u64 notify_id)
Alex Elderb8d70032012-11-30 17:53:04 -06002600{
2601 struct rbd_obj_request *obj_request;
Alex Elder21692382013-04-05 01:27:12 -05002602 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elderb8d70032012-11-30 17:53:04 -06002603 int ret;
2604
2605 obj_request = rbd_obj_request_create(rbd_dev->header_name, 0, 0,
2606 OBJ_REQUEST_NODATA);
2607 if (!obj_request)
2608 return -ENOMEM;
2609
2610 ret = -ENOMEM;
Alex Elder430c28c2013-04-03 21:32:51 -05002611 obj_request->osd_req = rbd_osd_req_create(rbd_dev, false, obj_request);
Alex Elderb8d70032012-11-30 17:53:04 -06002612 if (!obj_request->osd_req)
2613 goto out;
Alex Elder21692382013-04-05 01:27:12 -05002614 obj_request->callback = rbd_obj_request_put;
Alex Elderb8d70032012-11-30 17:53:04 -06002615
Alex Elderc99d2d42013-04-05 01:27:11 -05002616 osd_req_op_watch_init(obj_request->osd_req, 0, CEPH_OSD_OP_NOTIFY_ACK,
Alex Eldercc4a38bd2013-04-30 00:44:33 -05002617 notify_id, 0, 0);
Alex Elder9d4df012013-04-19 15:34:50 -05002618 rbd_osd_req_format_read(obj_request);
Alex Elder430c28c2013-04-03 21:32:51 -05002619
Alex Elderb8d70032012-11-30 17:53:04 -06002620 ret = rbd_obj_request_submit(osdc, obj_request);
Alex Elderb8d70032012-11-30 17:53:04 -06002621out:
Alex Eldercf81b602013-01-17 12:18:46 -06002622 if (ret)
2623 rbd_obj_request_put(obj_request);
Alex Elderb8d70032012-11-30 17:53:04 -06002624
2625 return ret;
2626}
2627
2628static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
2629{
2630 struct rbd_device *rbd_dev = (struct rbd_device *)data;
Alex Eldere627db02013-05-06 07:40:30 -05002631 int ret;
Alex Elderb8d70032012-11-30 17:53:04 -06002632
2633 if (!rbd_dev)
2634 return;
2635
Alex Elder37206ee2013-02-20 17:32:08 -06002636 dout("%s: \"%s\" notify_id %llu opcode %u\n", __func__,
Alex Eldercc4a38bd2013-04-30 00:44:33 -05002637 rbd_dev->header_name, (unsigned long long)notify_id,
2638 (unsigned int)opcode);
Alex Eldere627db02013-05-06 07:40:30 -05002639 ret = rbd_dev_refresh(rbd_dev);
2640 if (ret)
2641 rbd_warn(rbd_dev, ": header refresh error (%d)\n", ret);
Alex Elderb8d70032012-11-30 17:53:04 -06002642
Alex Eldercc4a38bd2013-04-30 00:44:33 -05002643 rbd_obj_notify_ack(rbd_dev, notify_id);
Alex Elderb8d70032012-11-30 17:53:04 -06002644}
2645
Alex Elder9969ebc2013-01-18 12:31:10 -06002646/*
2647 * Request sync osd watch/unwatch. The value of "start" determines
2648 * whether a watch request is being initiated or torn down.
2649 */
2650static int rbd_dev_header_watch_sync(struct rbd_device *rbd_dev, int start)
2651{
2652 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
2653 struct rbd_obj_request *obj_request;
Alex Elder9969ebc2013-01-18 12:31:10 -06002654 int ret;
2655
2656 rbd_assert(start ^ !!rbd_dev->watch_event);
2657 rbd_assert(start ^ !!rbd_dev->watch_request);
2658
2659 if (start) {
Alex Elder3c663bb2013-02-15 11:42:30 -06002660 ret = ceph_osdc_create_event(osdc, rbd_watch_cb, rbd_dev,
Alex Elder9969ebc2013-01-18 12:31:10 -06002661 &rbd_dev->watch_event);
2662 if (ret < 0)
2663 return ret;
Alex Elder8eb87562013-01-25 17:08:55 -06002664 rbd_assert(rbd_dev->watch_event != NULL);
Alex Elder9969ebc2013-01-18 12:31:10 -06002665 }
2666
2667 ret = -ENOMEM;
2668 obj_request = rbd_obj_request_create(rbd_dev->header_name, 0, 0,
2669 OBJ_REQUEST_NODATA);
2670 if (!obj_request)
2671 goto out_cancel;
2672
Alex Elder430c28c2013-04-03 21:32:51 -05002673 obj_request->osd_req = rbd_osd_req_create(rbd_dev, true, obj_request);
2674 if (!obj_request->osd_req)
2675 goto out_cancel;
2676
Alex Elder8eb87562013-01-25 17:08:55 -06002677 if (start)
Alex Elder975241a2013-01-25 17:08:55 -06002678 ceph_osdc_set_request_linger(osdc, obj_request->osd_req);
Alex Elder8eb87562013-01-25 17:08:55 -06002679 else
Alex Elder6977c3f2013-01-25 17:08:55 -06002680 ceph_osdc_unregister_linger_request(osdc,
Alex Elder975241a2013-01-25 17:08:55 -06002681 rbd_dev->watch_request->osd_req);
Alex Elder21692382013-04-05 01:27:12 -05002682
2683 osd_req_op_watch_init(obj_request->osd_req, 0, CEPH_OSD_OP_WATCH,
Alex Elderb21ebdd2013-04-30 00:44:32 -05002684 rbd_dev->watch_event->cookie, 0, start);
Alex Elder9d4df012013-04-19 15:34:50 -05002685 rbd_osd_req_format_write(obj_request);
Alex Elder21692382013-04-05 01:27:12 -05002686
Alex Elder9969ebc2013-01-18 12:31:10 -06002687 ret = rbd_obj_request_submit(osdc, obj_request);
2688 if (ret)
2689 goto out_cancel;
2690 ret = rbd_obj_request_wait(obj_request);
2691 if (ret)
2692 goto out_cancel;
Alex Elder9969ebc2013-01-18 12:31:10 -06002693 ret = obj_request->result;
2694 if (ret)
2695 goto out_cancel;
2696
Alex Elder8eb87562013-01-25 17:08:55 -06002697 /*
2698 * A watch request is set to linger, so the underlying osd
2699 * request won't go away until we unregister it. We retain
2700 * a pointer to the object request during that time (in
2701 * rbd_dev->watch_request), so we'll keep a reference to
2702 * it. We'll drop that reference (below) after we've
2703 * unregistered it.
2704 */
2705 if (start) {
2706 rbd_dev->watch_request = obj_request;
2707
2708 return 0;
2709 }
2710
2711 /* We have successfully torn down the watch request */
2712
2713 rbd_obj_request_put(rbd_dev->watch_request);
2714 rbd_dev->watch_request = NULL;
Alex Elder9969ebc2013-01-18 12:31:10 -06002715out_cancel:
2716 /* Cancel the event if we're tearing down, or on error */
2717 ceph_osdc_cancel_event(rbd_dev->watch_event);
2718 rbd_dev->watch_event = NULL;
Alex Elder9969ebc2013-01-18 12:31:10 -06002719 if (obj_request)
2720 rbd_obj_request_put(obj_request);
2721
2722 return ret;
2723}
2724
Alex Elder36be9a72013-01-19 00:30:28 -06002725/*
Alex Elderf40eb342013-04-25 15:09:42 -05002726 * Synchronous osd object method call. Returns the number of bytes
2727 * returned in the outbound buffer, or a negative error code.
Alex Elder36be9a72013-01-19 00:30:28 -06002728 */
2729static int rbd_obj_method_sync(struct rbd_device *rbd_dev,
2730 const char *object_name,
2731 const char *class_name,
2732 const char *method_name,
Alex Elder41579762013-04-21 12:14:45 -05002733 const void *outbound,
Alex Elder36be9a72013-01-19 00:30:28 -06002734 size_t outbound_size,
Alex Elder41579762013-04-21 12:14:45 -05002735 void *inbound,
Alex Eldere2a58ee2013-04-30 00:44:33 -05002736 size_t inbound_size)
Alex Elder36be9a72013-01-19 00:30:28 -06002737{
Alex Elder21692382013-04-05 01:27:12 -05002738 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder36be9a72013-01-19 00:30:28 -06002739 struct rbd_obj_request *obj_request;
Alex Elder36be9a72013-01-19 00:30:28 -06002740 struct page **pages;
2741 u32 page_count;
2742 int ret;
2743
2744 /*
Alex Elder6010a452013-04-05 01:27:11 -05002745 * Method calls are ultimately read operations. The result
2746 * should placed into the inbound buffer provided. They
2747 * also supply outbound data--parameters for the object
2748 * method. Currently if this is present it will be a
2749 * snapshot id.
Alex Elder36be9a72013-01-19 00:30:28 -06002750 */
Alex Elder57385b52013-04-21 12:14:45 -05002751 page_count = (u32)calc_pages_for(0, inbound_size);
Alex Elder36be9a72013-01-19 00:30:28 -06002752 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2753 if (IS_ERR(pages))
2754 return PTR_ERR(pages);
2755
2756 ret = -ENOMEM;
Alex Elder6010a452013-04-05 01:27:11 -05002757 obj_request = rbd_obj_request_create(object_name, 0, inbound_size,
Alex Elder36be9a72013-01-19 00:30:28 -06002758 OBJ_REQUEST_PAGES);
2759 if (!obj_request)
2760 goto out;
2761
2762 obj_request->pages = pages;
2763 obj_request->page_count = page_count;
2764
Alex Elder430c28c2013-04-03 21:32:51 -05002765 obj_request->osd_req = rbd_osd_req_create(rbd_dev, false, obj_request);
Alex Elder36be9a72013-01-19 00:30:28 -06002766 if (!obj_request->osd_req)
2767 goto out;
2768
Alex Elderc99d2d42013-04-05 01:27:11 -05002769 osd_req_op_cls_init(obj_request->osd_req, 0, CEPH_OSD_OP_CALL,
Alex Elder04017e22013-04-05 14:46:02 -05002770 class_name, method_name);
2771 if (outbound_size) {
2772 struct ceph_pagelist *pagelist;
2773
2774 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
2775 if (!pagelist)
2776 goto out;
2777
2778 ceph_pagelist_init(pagelist);
2779 ceph_pagelist_append(pagelist, outbound, outbound_size);
2780 osd_req_op_cls_request_data_pagelist(obj_request->osd_req, 0,
2781 pagelist);
2782 }
Alex Eldera4ce40a2013-04-05 01:27:12 -05002783 osd_req_op_cls_response_data_pages(obj_request->osd_req, 0,
2784 obj_request->pages, inbound_size,
Alex Elder44cd1882013-04-05 01:27:12 -05002785 0, false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05002786 rbd_osd_req_format_read(obj_request);
Alex Elder430c28c2013-04-03 21:32:51 -05002787
Alex Elder36be9a72013-01-19 00:30:28 -06002788 ret = rbd_obj_request_submit(osdc, obj_request);
2789 if (ret)
2790 goto out;
2791 ret = rbd_obj_request_wait(obj_request);
2792 if (ret)
2793 goto out;
2794
2795 ret = obj_request->result;
2796 if (ret < 0)
2797 goto out;
Alex Elder57385b52013-04-21 12:14:45 -05002798
2799 rbd_assert(obj_request->xferred < (u64)INT_MAX);
2800 ret = (int)obj_request->xferred;
Alex Elder903bb322013-02-06 13:11:38 -06002801 ceph_copy_from_page_vector(pages, inbound, 0, obj_request->xferred);
Alex Elder36be9a72013-01-19 00:30:28 -06002802out:
2803 if (obj_request)
2804 rbd_obj_request_put(obj_request);
2805 else
2806 ceph_release_page_vector(pages, page_count);
2807
2808 return ret;
2809}
2810
Alex Elderbf0d5f502012-11-22 00:00:08 -06002811static void rbd_request_fn(struct request_queue *q)
Alex Eldercc344fa2013-02-19 12:25:56 -06002812 __releases(q->queue_lock) __acquires(q->queue_lock)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002813{
2814 struct rbd_device *rbd_dev = q->queuedata;
2815 bool read_only = rbd_dev->mapping.read_only;
2816 struct request *rq;
2817 int result;
2818
2819 while ((rq = blk_fetch_request(q))) {
2820 bool write_request = rq_data_dir(rq) == WRITE;
2821 struct rbd_img_request *img_request;
2822 u64 offset;
2823 u64 length;
2824
2825 /* Ignore any non-FS requests that filter through. */
2826
2827 if (rq->cmd_type != REQ_TYPE_FS) {
Alex Elder4dda41d2013-02-20 21:59:33 -06002828 dout("%s: non-fs request type %d\n", __func__,
2829 (int) rq->cmd_type);
2830 __blk_end_request_all(rq, 0);
2831 continue;
2832 }
2833
2834 /* Ignore/skip any zero-length requests */
2835
2836 offset = (u64) blk_rq_pos(rq) << SECTOR_SHIFT;
2837 length = (u64) blk_rq_bytes(rq);
2838
2839 if (!length) {
2840 dout("%s: zero-length request\n", __func__);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002841 __blk_end_request_all(rq, 0);
2842 continue;
2843 }
2844
2845 spin_unlock_irq(q->queue_lock);
2846
2847 /* Disallow writes to a read-only device */
2848
2849 if (write_request) {
2850 result = -EROFS;
2851 if (read_only)
2852 goto end_request;
2853 rbd_assert(rbd_dev->spec->snap_id == CEPH_NOSNAP);
2854 }
2855
Alex Elder6d292902013-01-14 12:43:31 -06002856 /*
2857 * Quit early if the mapped snapshot no longer
2858 * exists. It's still possible the snapshot will
2859 * have disappeared by the time our request arrives
2860 * at the osd, but there's no sense in sending it if
2861 * we already know.
2862 */
2863 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) {
Alex Elderbf0d5f502012-11-22 00:00:08 -06002864 dout("request for non-existent snapshot");
2865 rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP);
2866 result = -ENXIO;
2867 goto end_request;
2868 }
2869
Alex Elderbf0d5f502012-11-22 00:00:08 -06002870 result = -EINVAL;
Alex Elderc0cd10db2013-04-26 09:43:47 -05002871 if (offset && length > U64_MAX - offset + 1) {
2872 rbd_warn(rbd_dev, "bad request range (%llu~%llu)\n",
2873 offset, length);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002874 goto end_request; /* Shouldn't happen */
Alex Elderc0cd10db2013-04-26 09:43:47 -05002875 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06002876
2877 result = -ENOMEM;
2878 img_request = rbd_img_request_create(rbd_dev, offset, length,
Alex Elder9849e982013-01-24 16:13:36 -06002879 write_request, false);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002880 if (!img_request)
2881 goto end_request;
2882
2883 img_request->rq = rq;
2884
Alex Elderf1a47392013-04-19 15:34:50 -05002885 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
2886 rq->bio);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002887 if (!result)
2888 result = rbd_img_request_submit(img_request);
2889 if (result)
2890 rbd_img_request_put(img_request);
2891end_request:
2892 spin_lock_irq(q->queue_lock);
2893 if (result < 0) {
Alex Elder7da22d22013-01-24 16:13:36 -06002894 rbd_warn(rbd_dev, "%s %llx at %llx result %d\n",
2895 write_request ? "write" : "read",
2896 length, offset, result);
2897
Alex Elderbf0d5f502012-11-22 00:00:08 -06002898 __blk_end_request_all(rq, result);
2899 }
2900 }
2901}
2902
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002903/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002904 * a queue callback. Makes sure that we don't create a bio that spans across
2905 * multiple osd objects. One exception would be with a single page bios,
Alex Elderf7760da2012-10-20 22:17:27 -05002906 * which we handle later at bio_chain_clone_range()
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002907 */
2908static int rbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd,
2909 struct bio_vec *bvec)
2910{
2911 struct rbd_device *rbd_dev = q->queuedata;
Alex Eldere5cfeed22012-10-20 22:17:27 -05002912 sector_t sector_offset;
2913 sector_t sectors_per_obj;
2914 sector_t obj_sector_offset;
2915 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002916
Alex Eldere5cfeed22012-10-20 22:17:27 -05002917 /*
2918 * Find how far into its rbd object the partition-relative
2919 * bio start sector is to offset relative to the enclosing
2920 * device.
2921 */
2922 sector_offset = get_start_sect(bmd->bi_bdev) + bmd->bi_sector;
2923 sectors_per_obj = 1 << (rbd_dev->header.obj_order - SECTOR_SHIFT);
2924 obj_sector_offset = sector_offset & (sectors_per_obj - 1);
Alex Elder593a9e72012-02-07 12:03:37 -06002925
Alex Eldere5cfeed22012-10-20 22:17:27 -05002926 /*
2927 * Compute the number of bytes from that offset to the end
2928 * of the object. Account for what's already used by the bio.
2929 */
2930 ret = (int) (sectors_per_obj - obj_sector_offset) << SECTOR_SHIFT;
2931 if (ret > bmd->bi_size)
2932 ret -= bmd->bi_size;
2933 else
2934 ret = 0;
2935
2936 /*
2937 * Don't send back more than was asked for. And if the bio
2938 * was empty, let the whole thing through because: "Note
2939 * that a block device *must* allow a single page to be
2940 * added to an empty bio."
2941 */
2942 rbd_assert(bvec->bv_len <= PAGE_SIZE);
2943 if (ret > (int) bvec->bv_len || !bmd->bi_size)
2944 ret = (int) bvec->bv_len;
2945
2946 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002947}
2948
2949static void rbd_free_disk(struct rbd_device *rbd_dev)
2950{
2951 struct gendisk *disk = rbd_dev->disk;
2952
2953 if (!disk)
2954 return;
2955
Alex Eldera0cab922013-04-25 23:15:08 -05002956 rbd_dev->disk = NULL;
2957 if (disk->flags & GENHD_FL_UP) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002958 del_gendisk(disk);
Alex Eldera0cab922013-04-25 23:15:08 -05002959 if (disk->queue)
2960 blk_cleanup_queue(disk->queue);
2961 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002962 put_disk(disk);
2963}
2964
Alex Elder788e2df2013-01-17 12:25:27 -06002965static int rbd_obj_read_sync(struct rbd_device *rbd_dev,
2966 const char *object_name,
Alex Elder7097f8d2013-04-30 00:44:33 -05002967 u64 offset, u64 length, void *buf)
Alex Elder788e2df2013-01-17 12:25:27 -06002968
2969{
Alex Elder21692382013-04-05 01:27:12 -05002970 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder788e2df2013-01-17 12:25:27 -06002971 struct rbd_obj_request *obj_request;
Alex Elder788e2df2013-01-17 12:25:27 -06002972 struct page **pages = NULL;
2973 u32 page_count;
Alex Elder1ceae7e2013-02-06 13:11:38 -06002974 size_t size;
Alex Elder788e2df2013-01-17 12:25:27 -06002975 int ret;
2976
2977 page_count = (u32) calc_pages_for(offset, length);
2978 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2979 if (IS_ERR(pages))
2980 ret = PTR_ERR(pages);
2981
2982 ret = -ENOMEM;
2983 obj_request = rbd_obj_request_create(object_name, offset, length,
Alex Elder36be9a72013-01-19 00:30:28 -06002984 OBJ_REQUEST_PAGES);
Alex Elder788e2df2013-01-17 12:25:27 -06002985 if (!obj_request)
2986 goto out;
2987
2988 obj_request->pages = pages;
2989 obj_request->page_count = page_count;
2990
Alex Elder430c28c2013-04-03 21:32:51 -05002991 obj_request->osd_req = rbd_osd_req_create(rbd_dev, false, obj_request);
Alex Elder788e2df2013-01-17 12:25:27 -06002992 if (!obj_request->osd_req)
2993 goto out;
2994
Alex Elderc99d2d42013-04-05 01:27:11 -05002995 osd_req_op_extent_init(obj_request->osd_req, 0, CEPH_OSD_OP_READ,
2996 offset, length, 0, 0);
Alex Elder406e2c92013-04-15 14:50:36 -05002997 osd_req_op_extent_osd_data_pages(obj_request->osd_req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05002998 obj_request->pages,
Alex Elder44cd1882013-04-05 01:27:12 -05002999 obj_request->length,
3000 obj_request->offset & ~PAGE_MASK,
3001 false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05003002 rbd_osd_req_format_read(obj_request);
Alex Elder430c28c2013-04-03 21:32:51 -05003003
Alex Elder788e2df2013-01-17 12:25:27 -06003004 ret = rbd_obj_request_submit(osdc, obj_request);
3005 if (ret)
3006 goto out;
3007 ret = rbd_obj_request_wait(obj_request);
3008 if (ret)
3009 goto out;
3010
3011 ret = obj_request->result;
3012 if (ret < 0)
3013 goto out;
Alex Elder1ceae7e2013-02-06 13:11:38 -06003014
3015 rbd_assert(obj_request->xferred <= (u64) SIZE_MAX);
3016 size = (size_t) obj_request->xferred;
Alex Elder903bb322013-02-06 13:11:38 -06003017 ceph_copy_from_page_vector(pages, buf, 0, size);
Alex Elder7097f8d2013-04-30 00:44:33 -05003018 rbd_assert(size <= (size_t)INT_MAX);
3019 ret = (int)size;
Alex Elder788e2df2013-01-17 12:25:27 -06003020out:
3021 if (obj_request)
3022 rbd_obj_request_put(obj_request);
3023 else
3024 ceph_release_page_vector(pages, page_count);
3025
3026 return ret;
3027}
3028
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003029/*
Alex Elder4156d992012-08-02 11:29:46 -05003030 * Read the complete header for the given rbd device.
3031 *
3032 * Returns a pointer to a dynamically-allocated buffer containing
3033 * the complete and validated header. Caller can pass the address
3034 * of a variable that will be filled in with the version of the
3035 * header object at the time it was read.
3036 *
3037 * Returns a pointer-coded errno if a failure occurs.
3038 */
3039static struct rbd_image_header_ondisk *
Alex Elder7097f8d2013-04-30 00:44:33 -05003040rbd_dev_v1_header_read(struct rbd_device *rbd_dev)
Alex Elder4156d992012-08-02 11:29:46 -05003041{
3042 struct rbd_image_header_ondisk *ondisk = NULL;
3043 u32 snap_count = 0;
3044 u64 names_size = 0;
3045 u32 want_count;
3046 int ret;
3047
3048 /*
3049 * The complete header will include an array of its 64-bit
3050 * snapshot ids, followed by the names of those snapshots as
3051 * a contiguous block of NUL-terminated strings. Note that
3052 * the number of snapshots could change by the time we read
3053 * it in, in which case we re-read it.
3054 */
3055 do {
3056 size_t size;
3057
3058 kfree(ondisk);
3059
3060 size = sizeof (*ondisk);
3061 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
3062 size += names_size;
3063 ondisk = kmalloc(size, GFP_KERNEL);
3064 if (!ondisk)
3065 return ERR_PTR(-ENOMEM);
3066
Alex Elder788e2df2013-01-17 12:25:27 -06003067 ret = rbd_obj_read_sync(rbd_dev, rbd_dev->header_name,
Alex Elder7097f8d2013-04-30 00:44:33 -05003068 0, size, ondisk);
Alex Elder4156d992012-08-02 11:29:46 -05003069 if (ret < 0)
3070 goto out_err;
Alex Elderc0cd10db2013-04-26 09:43:47 -05003071 if ((size_t)ret < size) {
Alex Elder4156d992012-08-02 11:29:46 -05003072 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05003073 rbd_warn(rbd_dev, "short header read (want %zd got %d)",
3074 size, ret);
Alex Elder4156d992012-08-02 11:29:46 -05003075 goto out_err;
3076 }
3077 if (!rbd_dev_ondisk_valid(ondisk)) {
3078 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05003079 rbd_warn(rbd_dev, "invalid header");
Alex Elder4156d992012-08-02 11:29:46 -05003080 goto out_err;
3081 }
3082
3083 names_size = le64_to_cpu(ondisk->snap_names_len);
3084 want_count = snap_count;
3085 snap_count = le32_to_cpu(ondisk->snap_count);
3086 } while (snap_count != want_count);
3087
3088 return ondisk;
3089
3090out_err:
3091 kfree(ondisk);
3092
3093 return ERR_PTR(ret);
3094}
3095
3096/*
3097 * reload the ondisk the header
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003098 */
3099static int rbd_read_header(struct rbd_device *rbd_dev,
3100 struct rbd_image_header *header)
3101{
Alex Elder4156d992012-08-02 11:29:46 -05003102 struct rbd_image_header_ondisk *ondisk;
Alex Elder4156d992012-08-02 11:29:46 -05003103 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003104
Alex Elder7097f8d2013-04-30 00:44:33 -05003105 ondisk = rbd_dev_v1_header_read(rbd_dev);
Alex Elder4156d992012-08-02 11:29:46 -05003106 if (IS_ERR(ondisk))
3107 return PTR_ERR(ondisk);
3108 ret = rbd_header_from_disk(header, ondisk);
Alex Elder4156d992012-08-02 11:29:46 -05003109 kfree(ondisk);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003110
Alex Elder4156d992012-08-02 11:29:46 -05003111 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003112}
3113
Alex Elder94785542012-10-09 13:50:17 -07003114static void rbd_update_mapping_size(struct rbd_device *rbd_dev)
3115{
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003116 if (rbd_dev->spec->snap_id != CEPH_NOSNAP)
Alex Elder94785542012-10-09 13:50:17 -07003117 return;
3118
Alex Eldere28626a2013-04-26 15:44:35 -05003119 if (rbd_dev->mapping.size != rbd_dev->header.image_size) {
3120 sector_t size;
3121
3122 rbd_dev->mapping.size = rbd_dev->header.image_size;
3123 size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE;
3124 dout("setting size to %llu sectors", (unsigned long long)size);
3125 set_capacity(rbd_dev->disk, size);
3126 }
Alex Elder94785542012-10-09 13:50:17 -07003127}
3128
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003129/*
3130 * only read the first part of the ondisk header, without the snaps info
3131 */
Alex Eldercc4a38bd2013-04-30 00:44:33 -05003132static int rbd_dev_v1_refresh(struct rbd_device *rbd_dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003133{
3134 int ret;
3135 struct rbd_image_header h;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003136
3137 ret = rbd_read_header(rbd_dev, &h);
3138 if (ret < 0)
3139 return ret;
3140
Josh Durgina51aa0c2011-12-05 10:35:04 -08003141 down_write(&rbd_dev->header_rwsem);
3142
Alex Elder94785542012-10-09 13:50:17 -07003143 /* Update image size, and check for resize of mapped image */
3144 rbd_dev->header.image_size = h.image_size;
3145 rbd_update_mapping_size(rbd_dev);
Sage Weil9db4b3e2011-04-19 22:49:06 -07003146
Alex Elder849b4262012-07-09 21:04:24 -05003147 /* rbd_dev->header.object_prefix shouldn't change */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003148 kfree(rbd_dev->header.snap_sizes);
Alex Elder849b4262012-07-09 21:04:24 -05003149 kfree(rbd_dev->header.snap_names);
Josh Durgind1d25642011-12-05 14:03:05 -08003150 /* osd requests may still refer to snapc */
Alex Elder812164f82013-04-30 00:44:32 -05003151 ceph_put_snap_context(rbd_dev->header.snapc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003152
Josh Durgin93a24e02011-12-05 10:41:28 -08003153 rbd_dev->header.image_size = h.image_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003154 rbd_dev->header.snapc = h.snapc;
3155 rbd_dev->header.snap_names = h.snap_names;
3156 rbd_dev->header.snap_sizes = h.snap_sizes;
Alex Elder849b4262012-07-09 21:04:24 -05003157 /* Free the extra copy of the object prefix */
Alex Elderc0cd10db2013-04-26 09:43:47 -05003158 if (strcmp(rbd_dev->header.object_prefix, h.object_prefix))
3159 rbd_warn(rbd_dev, "object prefix changed (ignoring)");
Alex Elder849b4262012-07-09 21:04:24 -05003160 kfree(h.object_prefix);
3161
Josh Durginc6666012011-11-21 17:11:12 -08003162 up_write(&rbd_dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003163
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003164 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003165}
3166
Alex Elder15228ed2013-05-01 12:43:03 -05003167/*
3168 * Clear the rbd device's EXISTS flag if the snapshot it's mapped to
3169 * has disappeared from the (just updated) snapshot context.
3170 */
3171static void rbd_exists_validate(struct rbd_device *rbd_dev)
3172{
3173 u64 snap_id;
3174
3175 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags))
3176 return;
3177
3178 snap_id = rbd_dev->spec->snap_id;
3179 if (snap_id == CEPH_NOSNAP)
3180 return;
3181
3182 if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX)
3183 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
3184}
3185
Alex Eldercc4a38bd2013-04-30 00:44:33 -05003186static int rbd_dev_refresh(struct rbd_device *rbd_dev)
Alex Elder1fe5e992012-07-25 09:32:41 -05003187{
Alex Eldere627db02013-05-06 07:40:30 -05003188 u64 mapping_size;
Alex Elder1fe5e992012-07-25 09:32:41 -05003189 int ret;
3190
Alex Elder117973f2012-08-31 17:29:55 -05003191 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
Alex Eldere627db02013-05-06 07:40:30 -05003192 mapping_size = rbd_dev->mapping.size;
Alex Elder1fe5e992012-07-25 09:32:41 -05003193 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Alex Elder117973f2012-08-31 17:29:55 -05003194 if (rbd_dev->image_format == 1)
Alex Eldercc4a38bd2013-04-30 00:44:33 -05003195 ret = rbd_dev_v1_refresh(rbd_dev);
Alex Elder117973f2012-08-31 17:29:55 -05003196 else
Alex Eldercc4a38bd2013-04-30 00:44:33 -05003197 ret = rbd_dev_v2_refresh(rbd_dev);
Alex Elder15228ed2013-05-01 12:43:03 -05003198
3199 /* If it's a mapped snapshot, validate its EXISTS flag */
3200
3201 rbd_exists_validate(rbd_dev);
Alex Elder1fe5e992012-07-25 09:32:41 -05003202 mutex_unlock(&ctl_mutex);
Alex Eldere627db02013-05-06 07:40:30 -05003203 if (mapping_size != rbd_dev->mapping.size)
Alex Eldera3fbe5d2013-04-30 00:44:32 -05003204 revalidate_disk(rbd_dev->disk);
Alex Elder1fe5e992012-07-25 09:32:41 -05003205
3206 return ret;
3207}
3208
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003209static int rbd_init_disk(struct rbd_device *rbd_dev)
3210{
3211 struct gendisk *disk;
3212 struct request_queue *q;
Alex Elder593a9e72012-02-07 12:03:37 -06003213 u64 segment_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003214
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003215 /* create gendisk info */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003216 disk = alloc_disk(RBD_MINORS_PER_MAJOR);
3217 if (!disk)
Alex Elder1fcdb8a2012-08-29 17:11:06 -05003218 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003219
Alex Elderf0f8cef2012-01-29 13:57:44 -06003220 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
Alex Elderde71a292012-07-03 16:01:19 -05003221 rbd_dev->dev_id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003222 disk->major = rbd_dev->major;
3223 disk->first_minor = 0;
3224 disk->fops = &rbd_bd_ops;
3225 disk->private_data = rbd_dev;
3226
Alex Elderbf0d5f502012-11-22 00:00:08 -06003227 q = blk_init_queue(rbd_request_fn, &rbd_dev->lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003228 if (!q)
3229 goto out_disk;
Josh Durgin029bcbd2011-07-22 11:35:23 -07003230
Alex Elder593a9e72012-02-07 12:03:37 -06003231 /* We use the default size, but let's be explicit about it. */
3232 blk_queue_physical_block_size(q, SECTOR_SIZE);
3233
Josh Durgin029bcbd2011-07-22 11:35:23 -07003234 /* set io sizes to object size */
Alex Elder593a9e72012-02-07 12:03:37 -06003235 segment_size = rbd_obj_bytes(&rbd_dev->header);
3236 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
3237 blk_queue_max_segment_size(q, segment_size);
3238 blk_queue_io_min(q, segment_size);
3239 blk_queue_io_opt(q, segment_size);
Josh Durgin029bcbd2011-07-22 11:35:23 -07003240
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003241 blk_queue_merge_bvec(q, rbd_merge_bvec);
3242 disk->queue = q;
3243
3244 q->queuedata = rbd_dev;
3245
3246 rbd_dev->disk = disk;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003247
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003248 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003249out_disk:
3250 put_disk(disk);
Alex Elder1fcdb8a2012-08-29 17:11:06 -05003251
3252 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003253}
3254
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003255/*
3256 sysfs
3257*/
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003258
Alex Elder593a9e72012-02-07 12:03:37 -06003259static struct rbd_device *dev_to_rbd_dev(struct device *dev)
3260{
3261 return container_of(dev, struct rbd_device, dev);
3262}
3263
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003264static ssize_t rbd_size_show(struct device *dev,
3265 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003266{
Alex Elder593a9e72012-02-07 12:03:37 -06003267 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003268
Alex Elderfc71d832013-04-26 15:44:36 -05003269 return sprintf(buf, "%llu\n",
3270 (unsigned long long)rbd_dev->mapping.size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003271}
3272
Alex Elder34b13182012-07-13 20:35:12 -05003273/*
3274 * Note this shows the features for whatever's mapped, which is not
3275 * necessarily the base image.
3276 */
3277static ssize_t rbd_features_show(struct device *dev,
3278 struct device_attribute *attr, char *buf)
3279{
3280 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3281
3282 return sprintf(buf, "0x%016llx\n",
Alex Elderfc71d832013-04-26 15:44:36 -05003283 (unsigned long long)rbd_dev->mapping.features);
Alex Elder34b13182012-07-13 20:35:12 -05003284}
3285
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003286static ssize_t rbd_major_show(struct device *dev,
3287 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003288{
Alex Elder593a9e72012-02-07 12:03:37 -06003289 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003290
Alex Elderfc71d832013-04-26 15:44:36 -05003291 if (rbd_dev->major)
3292 return sprintf(buf, "%d\n", rbd_dev->major);
3293
3294 return sprintf(buf, "(none)\n");
3295
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003296}
3297
3298static ssize_t rbd_client_id_show(struct device *dev,
3299 struct device_attribute *attr, char *buf)
3300{
Alex Elder593a9e72012-02-07 12:03:37 -06003301 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003302
Alex Elder1dbb4392012-01-24 10:08:37 -06003303 return sprintf(buf, "client%lld\n",
3304 ceph_client_id(rbd_dev->rbd_client->client));
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003305}
3306
3307static ssize_t rbd_pool_show(struct device *dev,
3308 struct device_attribute *attr, char *buf)
3309{
Alex Elder593a9e72012-02-07 12:03:37 -06003310 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003311
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003312 return sprintf(buf, "%s\n", rbd_dev->spec->pool_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003313}
3314
Alex Elder9bb2f332012-07-12 10:46:35 -05003315static ssize_t rbd_pool_id_show(struct device *dev,
3316 struct device_attribute *attr, char *buf)
3317{
3318 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3319
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003320 return sprintf(buf, "%llu\n",
Alex Elderfc71d832013-04-26 15:44:36 -05003321 (unsigned long long) rbd_dev->spec->pool_id);
Alex Elder9bb2f332012-07-12 10:46:35 -05003322}
3323
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003324static ssize_t rbd_name_show(struct device *dev,
3325 struct device_attribute *attr, char *buf)
3326{
Alex Elder593a9e72012-02-07 12:03:37 -06003327 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003328
Alex Eldera92ffdf2012-10-30 19:40:33 -05003329 if (rbd_dev->spec->image_name)
3330 return sprintf(buf, "%s\n", rbd_dev->spec->image_name);
3331
3332 return sprintf(buf, "(unknown)\n");
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003333}
3334
Alex Elder589d30e2012-07-10 20:30:11 -05003335static ssize_t rbd_image_id_show(struct device *dev,
3336 struct device_attribute *attr, char *buf)
3337{
3338 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3339
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003340 return sprintf(buf, "%s\n", rbd_dev->spec->image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05003341}
3342
Alex Elder34b13182012-07-13 20:35:12 -05003343/*
3344 * Shows the name of the currently-mapped snapshot (or
3345 * RBD_SNAP_HEAD_NAME for the base image).
3346 */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003347static ssize_t rbd_snap_show(struct device *dev,
3348 struct device_attribute *attr,
3349 char *buf)
3350{
Alex Elder593a9e72012-02-07 12:03:37 -06003351 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003352
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003353 return sprintf(buf, "%s\n", rbd_dev->spec->snap_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003354}
3355
Alex Elder86b00e02012-10-25 23:34:42 -05003356/*
3357 * For an rbd v2 image, shows the pool id, image id, and snapshot id
3358 * for the parent image. If there is no parent, simply shows
3359 * "(no parent image)".
3360 */
3361static ssize_t rbd_parent_show(struct device *dev,
3362 struct device_attribute *attr,
3363 char *buf)
3364{
3365 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3366 struct rbd_spec *spec = rbd_dev->parent_spec;
3367 int count;
3368 char *bufp = buf;
3369
3370 if (!spec)
3371 return sprintf(buf, "(no parent image)\n");
3372
3373 count = sprintf(bufp, "pool_id %llu\npool_name %s\n",
3374 (unsigned long long) spec->pool_id, spec->pool_name);
3375 if (count < 0)
3376 return count;
3377 bufp += count;
3378
3379 count = sprintf(bufp, "image_id %s\nimage_name %s\n", spec->image_id,
3380 spec->image_name ? spec->image_name : "(unknown)");
3381 if (count < 0)
3382 return count;
3383 bufp += count;
3384
3385 count = sprintf(bufp, "snap_id %llu\nsnap_name %s\n",
3386 (unsigned long long) spec->snap_id, spec->snap_name);
3387 if (count < 0)
3388 return count;
3389 bufp += count;
3390
3391 count = sprintf(bufp, "overlap %llu\n", rbd_dev->parent_overlap);
3392 if (count < 0)
3393 return count;
3394 bufp += count;
3395
3396 return (ssize_t) (bufp - buf);
3397}
3398
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003399static ssize_t rbd_image_refresh(struct device *dev,
3400 struct device_attribute *attr,
3401 const char *buf,
3402 size_t size)
3403{
Alex Elder593a9e72012-02-07 12:03:37 -06003404 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Alex Elderb8136232012-07-25 09:32:41 -05003405 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003406
Alex Eldercc4a38bd2013-04-30 00:44:33 -05003407 ret = rbd_dev_refresh(rbd_dev);
Alex Eldere627db02013-05-06 07:40:30 -05003408 if (ret)
3409 rbd_warn(rbd_dev, ": manual header refresh error (%d)\n", ret);
Alex Elderb8136232012-07-25 09:32:41 -05003410
3411 return ret < 0 ? ret : size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003412}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003413
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003414static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
Alex Elder34b13182012-07-13 20:35:12 -05003415static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003416static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
3417static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
3418static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
Alex Elder9bb2f332012-07-12 10:46:35 -05003419static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003420static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
Alex Elder589d30e2012-07-10 20:30:11 -05003421static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003422static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
3423static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
Alex Elder86b00e02012-10-25 23:34:42 -05003424static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003425
3426static struct attribute *rbd_attrs[] = {
3427 &dev_attr_size.attr,
Alex Elder34b13182012-07-13 20:35:12 -05003428 &dev_attr_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003429 &dev_attr_major.attr,
3430 &dev_attr_client_id.attr,
3431 &dev_attr_pool.attr,
Alex Elder9bb2f332012-07-12 10:46:35 -05003432 &dev_attr_pool_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003433 &dev_attr_name.attr,
Alex Elder589d30e2012-07-10 20:30:11 -05003434 &dev_attr_image_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003435 &dev_attr_current_snap.attr,
Alex Elder86b00e02012-10-25 23:34:42 -05003436 &dev_attr_parent.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003437 &dev_attr_refresh.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003438 NULL
3439};
3440
3441static struct attribute_group rbd_attr_group = {
3442 .attrs = rbd_attrs,
3443};
3444
3445static const struct attribute_group *rbd_attr_groups[] = {
3446 &rbd_attr_group,
3447 NULL
3448};
3449
3450static void rbd_sysfs_dev_release(struct device *dev)
3451{
3452}
3453
3454static struct device_type rbd_device_type = {
3455 .name = "rbd",
3456 .groups = rbd_attr_groups,
3457 .release = rbd_sysfs_dev_release,
3458};
3459
Alex Elder8b8fb992012-10-26 17:25:24 -05003460static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)
3461{
3462 kref_get(&spec->kref);
3463
3464 return spec;
3465}
3466
3467static void rbd_spec_free(struct kref *kref);
3468static void rbd_spec_put(struct rbd_spec *spec)
3469{
3470 if (spec)
3471 kref_put(&spec->kref, rbd_spec_free);
3472}
3473
3474static struct rbd_spec *rbd_spec_alloc(void)
3475{
3476 struct rbd_spec *spec;
3477
3478 spec = kzalloc(sizeof (*spec), GFP_KERNEL);
3479 if (!spec)
3480 return NULL;
3481 kref_init(&spec->kref);
3482
Alex Elder8b8fb992012-10-26 17:25:24 -05003483 return spec;
3484}
3485
3486static void rbd_spec_free(struct kref *kref)
3487{
3488 struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref);
3489
3490 kfree(spec->pool_name);
3491 kfree(spec->image_id);
3492 kfree(spec->image_name);
3493 kfree(spec->snap_name);
3494 kfree(spec);
3495}
3496
Alex Eldercc344fa2013-02-19 12:25:56 -06003497static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,
Alex Elderc53d5892012-10-25 23:34:42 -05003498 struct rbd_spec *spec)
3499{
3500 struct rbd_device *rbd_dev;
3501
3502 rbd_dev = kzalloc(sizeof (*rbd_dev), GFP_KERNEL);
3503 if (!rbd_dev)
3504 return NULL;
3505
3506 spin_lock_init(&rbd_dev->lock);
Alex Elder6d292902013-01-14 12:43:31 -06003507 rbd_dev->flags = 0;
Alex Elderc53d5892012-10-25 23:34:42 -05003508 INIT_LIST_HEAD(&rbd_dev->node);
Alex Elderc53d5892012-10-25 23:34:42 -05003509 init_rwsem(&rbd_dev->header_rwsem);
3510
3511 rbd_dev->spec = spec;
3512 rbd_dev->rbd_client = rbdc;
3513
Alex Elder0903e872012-11-14 12:25:19 -06003514 /* Initialize the layout used for all rbd requests */
3515
3516 rbd_dev->layout.fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
3517 rbd_dev->layout.fl_stripe_count = cpu_to_le32(1);
3518 rbd_dev->layout.fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
3519 rbd_dev->layout.fl_pg_pool = cpu_to_le32((u32) spec->pool_id);
3520
Alex Elderc53d5892012-10-25 23:34:42 -05003521 return rbd_dev;
3522}
3523
3524static void rbd_dev_destroy(struct rbd_device *rbd_dev)
3525{
Alex Elderc53d5892012-10-25 23:34:42 -05003526 rbd_put_client(rbd_dev->rbd_client);
3527 rbd_spec_put(rbd_dev->spec);
3528 kfree(rbd_dev);
3529}
3530
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003531/*
Alex Elder9d475de2012-07-03 16:01:19 -05003532 * Get the size and object order for an image snapshot, or if
3533 * snap_id is CEPH_NOSNAP, gets this information for the base
3534 * image.
3535 */
3536static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
3537 u8 *order, u64 *snap_size)
3538{
3539 __le64 snapid = cpu_to_le64(snap_id);
3540 int ret;
3541 struct {
3542 u8 order;
3543 __le64 size;
3544 } __attribute__ ((packed)) size_buf = { 0 };
3545
Alex Elder36be9a72013-01-19 00:30:28 -06003546 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elder9d475de2012-07-03 16:01:19 -05003547 "rbd", "get_size",
Alex Elder41579762013-04-21 12:14:45 -05003548 &snapid, sizeof (snapid),
Alex Eldere2a58ee2013-04-30 00:44:33 -05003549 &size_buf, sizeof (size_buf));
Alex Elder36be9a72013-01-19 00:30:28 -06003550 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder9d475de2012-07-03 16:01:19 -05003551 if (ret < 0)
3552 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05003553 if (ret < sizeof (size_buf))
3554 return -ERANGE;
Alex Elder9d475de2012-07-03 16:01:19 -05003555
Alex Elderc86f86e2013-04-25 15:09:41 -05003556 if (order)
3557 *order = size_buf.order;
Alex Elder9d475de2012-07-03 16:01:19 -05003558 *snap_size = le64_to_cpu(size_buf.size);
3559
3560 dout(" snap_id 0x%016llx order = %u, snap_size = %llu\n",
Alex Elder57385b52013-04-21 12:14:45 -05003561 (unsigned long long)snap_id, (unsigned int)*order,
3562 (unsigned long long)*snap_size);
Alex Elder9d475de2012-07-03 16:01:19 -05003563
3564 return 0;
3565}
3566
3567static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
3568{
3569 return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
3570 &rbd_dev->header.obj_order,
3571 &rbd_dev->header.image_size);
3572}
3573
Alex Elder1e130192012-07-03 16:01:19 -05003574static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
3575{
3576 void *reply_buf;
3577 int ret;
3578 void *p;
3579
3580 reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL);
3581 if (!reply_buf)
3582 return -ENOMEM;
3583
Alex Elder36be9a72013-01-19 00:30:28 -06003584 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elder41579762013-04-21 12:14:45 -05003585 "rbd", "get_object_prefix", NULL, 0,
Alex Eldere2a58ee2013-04-30 00:44:33 -05003586 reply_buf, RBD_OBJ_PREFIX_LEN_MAX);
Alex Elder36be9a72013-01-19 00:30:28 -06003587 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder1e130192012-07-03 16:01:19 -05003588 if (ret < 0)
3589 goto out;
3590
3591 p = reply_buf;
3592 rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p,
Alex Elder57385b52013-04-21 12:14:45 -05003593 p + ret, NULL, GFP_NOIO);
3594 ret = 0;
Alex Elder1e130192012-07-03 16:01:19 -05003595
3596 if (IS_ERR(rbd_dev->header.object_prefix)) {
3597 ret = PTR_ERR(rbd_dev->header.object_prefix);
3598 rbd_dev->header.object_prefix = NULL;
3599 } else {
3600 dout(" object_prefix = %s\n", rbd_dev->header.object_prefix);
3601 }
Alex Elder1e130192012-07-03 16:01:19 -05003602out:
3603 kfree(reply_buf);
3604
3605 return ret;
3606}
3607
Alex Elderb1b54022012-07-03 16:01:19 -05003608static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
3609 u64 *snap_features)
3610{
3611 __le64 snapid = cpu_to_le64(snap_id);
3612 struct {
3613 __le64 features;
3614 __le64 incompat;
Alex Elder41579762013-04-21 12:14:45 -05003615 } __attribute__ ((packed)) features_buf = { 0 };
Alex Elderd8891402012-10-09 13:50:17 -07003616 u64 incompat;
Alex Elderb1b54022012-07-03 16:01:19 -05003617 int ret;
3618
Alex Elder36be9a72013-01-19 00:30:28 -06003619 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elderb1b54022012-07-03 16:01:19 -05003620 "rbd", "get_features",
Alex Elder41579762013-04-21 12:14:45 -05003621 &snapid, sizeof (snapid),
Alex Eldere2a58ee2013-04-30 00:44:33 -05003622 &features_buf, sizeof (features_buf));
Alex Elder36be9a72013-01-19 00:30:28 -06003623 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderb1b54022012-07-03 16:01:19 -05003624 if (ret < 0)
3625 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05003626 if (ret < sizeof (features_buf))
3627 return -ERANGE;
Alex Elderd8891402012-10-09 13:50:17 -07003628
3629 incompat = le64_to_cpu(features_buf.incompat);
Alex Elder5cbf6f122013-04-11 09:29:48 -05003630 if (incompat & ~RBD_FEATURES_SUPPORTED)
Alex Elderb8f5c6e2012-11-01 08:39:26 -05003631 return -ENXIO;
Alex Elderd8891402012-10-09 13:50:17 -07003632
Alex Elderb1b54022012-07-03 16:01:19 -05003633 *snap_features = le64_to_cpu(features_buf.features);
3634
3635 dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
Alex Elder57385b52013-04-21 12:14:45 -05003636 (unsigned long long)snap_id,
3637 (unsigned long long)*snap_features,
3638 (unsigned long long)le64_to_cpu(features_buf.incompat));
Alex Elderb1b54022012-07-03 16:01:19 -05003639
3640 return 0;
3641}
3642
3643static int rbd_dev_v2_features(struct rbd_device *rbd_dev)
3644{
3645 return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP,
3646 &rbd_dev->header.features);
3647}
3648
Alex Elder86b00e02012-10-25 23:34:42 -05003649static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev)
3650{
3651 struct rbd_spec *parent_spec;
3652 size_t size;
3653 void *reply_buf = NULL;
3654 __le64 snapid;
3655 void *p;
3656 void *end;
3657 char *image_id;
3658 u64 overlap;
Alex Elder86b00e02012-10-25 23:34:42 -05003659 int ret;
3660
3661 parent_spec = rbd_spec_alloc();
3662 if (!parent_spec)
3663 return -ENOMEM;
3664
3665 size = sizeof (__le64) + /* pool_id */
3666 sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */
3667 sizeof (__le64) + /* snap_id */
3668 sizeof (__le64); /* overlap */
3669 reply_buf = kmalloc(size, GFP_KERNEL);
3670 if (!reply_buf) {
3671 ret = -ENOMEM;
3672 goto out_err;
3673 }
3674
3675 snapid = cpu_to_le64(CEPH_NOSNAP);
Alex Elder36be9a72013-01-19 00:30:28 -06003676 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elder86b00e02012-10-25 23:34:42 -05003677 "rbd", "get_parent",
Alex Elder41579762013-04-21 12:14:45 -05003678 &snapid, sizeof (snapid),
Alex Eldere2a58ee2013-04-30 00:44:33 -05003679 reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06003680 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder86b00e02012-10-25 23:34:42 -05003681 if (ret < 0)
3682 goto out_err;
3683
Alex Elder86b00e02012-10-25 23:34:42 -05003684 p = reply_buf;
Alex Elder57385b52013-04-21 12:14:45 -05003685 end = reply_buf + ret;
3686 ret = -ERANGE;
Alex Elder86b00e02012-10-25 23:34:42 -05003687 ceph_decode_64_safe(&p, end, parent_spec->pool_id, out_err);
3688 if (parent_spec->pool_id == CEPH_NOPOOL)
3689 goto out; /* No parent? No problem. */
3690
Alex Elder0903e872012-11-14 12:25:19 -06003691 /* The ceph file layout needs to fit pool id in 32 bits */
3692
3693 ret = -EIO;
Alex Elderc0cd10db2013-04-26 09:43:47 -05003694 if (parent_spec->pool_id > (u64)U32_MAX) {
3695 rbd_warn(NULL, "parent pool id too large (%llu > %u)\n",
3696 (unsigned long long)parent_spec->pool_id, U32_MAX);
Alex Elder57385b52013-04-21 12:14:45 -05003697 goto out_err;
Alex Elderc0cd10db2013-04-26 09:43:47 -05003698 }
Alex Elder0903e872012-11-14 12:25:19 -06003699
Alex Elder979ed482012-11-01 08:39:26 -05003700 image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elder86b00e02012-10-25 23:34:42 -05003701 if (IS_ERR(image_id)) {
3702 ret = PTR_ERR(image_id);
3703 goto out_err;
3704 }
3705 parent_spec->image_id = image_id;
3706 ceph_decode_64_safe(&p, end, parent_spec->snap_id, out_err);
3707 ceph_decode_64_safe(&p, end, overlap, out_err);
3708
3709 rbd_dev->parent_overlap = overlap;
3710 rbd_dev->parent_spec = parent_spec;
3711 parent_spec = NULL; /* rbd_dev now owns this */
3712out:
3713 ret = 0;
3714out_err:
3715 kfree(reply_buf);
3716 rbd_spec_put(parent_spec);
3717
3718 return ret;
3719}
3720
Alex Eldercc070d52013-04-21 12:14:45 -05003721static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev)
3722{
3723 struct {
3724 __le64 stripe_unit;
3725 __le64 stripe_count;
3726 } __attribute__ ((packed)) striping_info_buf = { 0 };
3727 size_t size = sizeof (striping_info_buf);
3728 void *p;
3729 u64 obj_size;
3730 u64 stripe_unit;
3731 u64 stripe_count;
3732 int ret;
3733
3734 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
3735 "rbd", "get_stripe_unit_count", NULL, 0,
Alex Eldere2a58ee2013-04-30 00:44:33 -05003736 (char *)&striping_info_buf, size);
Alex Eldercc070d52013-04-21 12:14:45 -05003737 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
3738 if (ret < 0)
3739 return ret;
3740 if (ret < size)
3741 return -ERANGE;
3742
3743 /*
3744 * We don't actually support the "fancy striping" feature
3745 * (STRIPINGV2) yet, but if the striping sizes are the
3746 * defaults the behavior is the same as before. So find
3747 * out, and only fail if the image has non-default values.
3748 */
3749 ret = -EINVAL;
3750 obj_size = (u64)1 << rbd_dev->header.obj_order;
3751 p = &striping_info_buf;
3752 stripe_unit = ceph_decode_64(&p);
3753 if (stripe_unit != obj_size) {
3754 rbd_warn(rbd_dev, "unsupported stripe unit "
3755 "(got %llu want %llu)",
3756 stripe_unit, obj_size);
3757 return -EINVAL;
3758 }
3759 stripe_count = ceph_decode_64(&p);
3760 if (stripe_count != 1) {
3761 rbd_warn(rbd_dev, "unsupported stripe count "
3762 "(got %llu want 1)", stripe_count);
3763 return -EINVAL;
3764 }
Alex Elder500d0c02013-04-26 09:43:47 -05003765 rbd_dev->header.stripe_unit = stripe_unit;
3766 rbd_dev->header.stripe_count = stripe_count;
Alex Eldercc070d52013-04-21 12:14:45 -05003767
3768 return 0;
3769}
3770
Alex Elder9e15b772012-10-30 19:40:33 -05003771static char *rbd_dev_image_name(struct rbd_device *rbd_dev)
3772{
3773 size_t image_id_size;
3774 char *image_id;
3775 void *p;
3776 void *end;
3777 size_t size;
3778 void *reply_buf = NULL;
3779 size_t len = 0;
3780 char *image_name = NULL;
3781 int ret;
3782
3783 rbd_assert(!rbd_dev->spec->image_name);
3784
Alex Elder69e7a022012-11-01 08:39:26 -05003785 len = strlen(rbd_dev->spec->image_id);
3786 image_id_size = sizeof (__le32) + len;
Alex Elder9e15b772012-10-30 19:40:33 -05003787 image_id = kmalloc(image_id_size, GFP_KERNEL);
3788 if (!image_id)
3789 return NULL;
3790
3791 p = image_id;
Alex Elder41579762013-04-21 12:14:45 -05003792 end = image_id + image_id_size;
Alex Elder57385b52013-04-21 12:14:45 -05003793 ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len);
Alex Elder9e15b772012-10-30 19:40:33 -05003794
3795 size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX;
3796 reply_buf = kmalloc(size, GFP_KERNEL);
3797 if (!reply_buf)
3798 goto out;
3799
Alex Elder36be9a72013-01-19 00:30:28 -06003800 ret = rbd_obj_method_sync(rbd_dev, RBD_DIRECTORY,
Alex Elder9e15b772012-10-30 19:40:33 -05003801 "rbd", "dir_get_name",
3802 image_id, image_id_size,
Alex Eldere2a58ee2013-04-30 00:44:33 -05003803 reply_buf, size);
Alex Elder9e15b772012-10-30 19:40:33 -05003804 if (ret < 0)
3805 goto out;
3806 p = reply_buf;
Alex Elderf40eb342013-04-25 15:09:42 -05003807 end = reply_buf + ret;
3808
Alex Elder9e15b772012-10-30 19:40:33 -05003809 image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL);
3810 if (IS_ERR(image_name))
3811 image_name = NULL;
3812 else
3813 dout("%s: name is %s len is %zd\n", __func__, image_name, len);
3814out:
3815 kfree(reply_buf);
3816 kfree(image_id);
3817
3818 return image_name;
3819}
3820
Alex Elder2ad3d712013-04-30 00:44:33 -05003821static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
3822{
3823 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
3824 const char *snap_name;
3825 u32 which = 0;
3826
3827 /* Skip over names until we find the one we are looking for */
3828
3829 snap_name = rbd_dev->header.snap_names;
3830 while (which < snapc->num_snaps) {
3831 if (!strcmp(name, snap_name))
3832 return snapc->snaps[which];
3833 snap_name += strlen(snap_name) + 1;
3834 which++;
3835 }
3836 return CEPH_NOSNAP;
3837}
3838
3839static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
3840{
3841 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
3842 u32 which;
3843 bool found = false;
3844 u64 snap_id;
3845
3846 for (which = 0; !found && which < snapc->num_snaps; which++) {
3847 const char *snap_name;
3848
3849 snap_id = snapc->snaps[which];
3850 snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id);
3851 if (IS_ERR(snap_name))
3852 break;
3853 found = !strcmp(name, snap_name);
3854 kfree(snap_name);
3855 }
3856 return found ? snap_id : CEPH_NOSNAP;
3857}
3858
3859/*
3860 * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if
3861 * no snapshot by that name is found, or if an error occurs.
3862 */
3863static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
3864{
3865 if (rbd_dev->image_format == 1)
3866 return rbd_v1_snap_id_by_name(rbd_dev, name);
3867
3868 return rbd_v2_snap_id_by_name(rbd_dev, name);
3869}
3870
Alex Elder9e15b772012-10-30 19:40:33 -05003871/*
Alex Elder2e9f7f12013-04-26 09:43:48 -05003872 * When an rbd image has a parent image, it is identified by the
3873 * pool, image, and snapshot ids (not names). This function fills
3874 * in the names for those ids. (It's OK if we can't figure out the
3875 * name for an image id, but the pool and snapshot ids should always
3876 * exist and have names.) All names in an rbd spec are dynamically
3877 * allocated.
Alex Eldere1d42132013-04-25 23:15:08 -05003878 *
3879 * When an image being mapped (not a parent) is probed, we have the
3880 * pool name and pool id, image name and image id, and the snapshot
3881 * name. The only thing we're missing is the snapshot id.
Alex Elder9e15b772012-10-30 19:40:33 -05003882 */
Alex Elder2e9f7f12013-04-26 09:43:48 -05003883static int rbd_dev_spec_update(struct rbd_device *rbd_dev)
Alex Elder9e15b772012-10-30 19:40:33 -05003884{
Alex Elder2e9f7f12013-04-26 09:43:48 -05003885 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3886 struct rbd_spec *spec = rbd_dev->spec;
3887 const char *pool_name;
3888 const char *image_name;
3889 const char *snap_name;
Alex Elder9e15b772012-10-30 19:40:33 -05003890 int ret;
3891
Alex Eldere1d42132013-04-25 23:15:08 -05003892 /*
3893 * An image being mapped will have the pool name (etc.), but
3894 * we need to look up the snapshot id.
3895 */
Alex Elder2e9f7f12013-04-26 09:43:48 -05003896 if (spec->pool_name) {
3897 if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) {
Alex Elder2ad3d712013-04-30 00:44:33 -05003898 u64 snap_id;
Alex Eldere1d42132013-04-25 23:15:08 -05003899
Alex Elder2ad3d712013-04-30 00:44:33 -05003900 snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name);
3901 if (snap_id == CEPH_NOSNAP)
Alex Eldere1d42132013-04-25 23:15:08 -05003902 return -ENOENT;
Alex Elder2ad3d712013-04-30 00:44:33 -05003903 spec->snap_id = snap_id;
Alex Eldere1d42132013-04-25 23:15:08 -05003904 } else {
Alex Elder2e9f7f12013-04-26 09:43:48 -05003905 spec->snap_id = CEPH_NOSNAP;
Alex Eldere1d42132013-04-25 23:15:08 -05003906 }
3907
3908 return 0;
3909 }
Alex Elder9e15b772012-10-30 19:40:33 -05003910
Alex Elder2e9f7f12013-04-26 09:43:48 -05003911 /* Get the pool name; we have to make our own copy of this */
Alex Elder9e15b772012-10-30 19:40:33 -05003912
Alex Elder2e9f7f12013-04-26 09:43:48 -05003913 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id);
3914 if (!pool_name) {
3915 rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id);
Alex Elder935dc892012-11-01 10:17:15 -05003916 return -EIO;
3917 }
Alex Elder2e9f7f12013-04-26 09:43:48 -05003918 pool_name = kstrdup(pool_name, GFP_KERNEL);
3919 if (!pool_name)
Alex Elder9e15b772012-10-30 19:40:33 -05003920 return -ENOMEM;
3921
3922 /* Fetch the image name; tolerate failure here */
3923
Alex Elder2e9f7f12013-04-26 09:43:48 -05003924 image_name = rbd_dev_image_name(rbd_dev);
3925 if (!image_name)
Alex Elder06ecc6c2012-11-01 10:17:15 -05003926 rbd_warn(rbd_dev, "unable to get image name");
Alex Elder9e15b772012-10-30 19:40:33 -05003927
Alex Elder2e9f7f12013-04-26 09:43:48 -05003928 /* Look up the snapshot name, and make a copy */
Alex Elder9e15b772012-10-30 19:40:33 -05003929
Alex Elder2e9f7f12013-04-26 09:43:48 -05003930 snap_name = rbd_snap_name(rbd_dev, spec->snap_id);
3931 if (!snap_name) {
Alex Elder2e9f7f12013-04-26 09:43:48 -05003932 ret = -ENOMEM;
Alex Elder9e15b772012-10-30 19:40:33 -05003933 goto out_err;
Alex Elder2e9f7f12013-04-26 09:43:48 -05003934 }
3935
3936 spec->pool_name = pool_name;
3937 spec->image_name = image_name;
3938 spec->snap_name = snap_name;
Alex Elder9e15b772012-10-30 19:40:33 -05003939
3940 return 0;
3941out_err:
Alex Elder2e9f7f12013-04-26 09:43:48 -05003942 kfree(image_name);
3943 kfree(pool_name);
Alex Elder9e15b772012-10-30 19:40:33 -05003944
3945 return ret;
3946}
3947
Alex Eldercc4a38bd2013-04-30 00:44:33 -05003948static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev)
Alex Elder35d489f2012-07-03 16:01:19 -05003949{
3950 size_t size;
3951 int ret;
3952 void *reply_buf;
3953 void *p;
3954 void *end;
3955 u64 seq;
3956 u32 snap_count;
3957 struct ceph_snap_context *snapc;
3958 u32 i;
3959
3960 /*
3961 * We'll need room for the seq value (maximum snapshot id),
3962 * snapshot count, and array of that many snapshot ids.
3963 * For now we have a fixed upper limit on the number we're
3964 * prepared to receive.
3965 */
3966 size = sizeof (__le64) + sizeof (__le32) +
3967 RBD_MAX_SNAP_COUNT * sizeof (__le64);
3968 reply_buf = kzalloc(size, GFP_KERNEL);
3969 if (!reply_buf)
3970 return -ENOMEM;
3971
Alex Elder36be9a72013-01-19 00:30:28 -06003972 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elder41579762013-04-21 12:14:45 -05003973 "rbd", "get_snapcontext", NULL, 0,
Alex Eldere2a58ee2013-04-30 00:44:33 -05003974 reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06003975 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder35d489f2012-07-03 16:01:19 -05003976 if (ret < 0)
3977 goto out;
3978
Alex Elder35d489f2012-07-03 16:01:19 -05003979 p = reply_buf;
Alex Elder57385b52013-04-21 12:14:45 -05003980 end = reply_buf + ret;
3981 ret = -ERANGE;
Alex Elder35d489f2012-07-03 16:01:19 -05003982 ceph_decode_64_safe(&p, end, seq, out);
3983 ceph_decode_32_safe(&p, end, snap_count, out);
3984
3985 /*
3986 * Make sure the reported number of snapshot ids wouldn't go
3987 * beyond the end of our buffer. But before checking that,
3988 * make sure the computed size of the snapshot context we
3989 * allocate is representable in a size_t.
3990 */
3991 if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
3992 / sizeof (u64)) {
3993 ret = -EINVAL;
3994 goto out;
3995 }
3996 if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
3997 goto out;
Alex Elder468521c2013-04-26 09:43:47 -05003998 ret = 0;
Alex Elder35d489f2012-07-03 16:01:19 -05003999
Alex Elder812164f82013-04-30 00:44:32 -05004000 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
Alex Elder35d489f2012-07-03 16:01:19 -05004001 if (!snapc) {
4002 ret = -ENOMEM;
4003 goto out;
4004 }
Alex Elder35d489f2012-07-03 16:01:19 -05004005 snapc->seq = seq;
Alex Elder35d489f2012-07-03 16:01:19 -05004006 for (i = 0; i < snap_count; i++)
4007 snapc->snaps[i] = ceph_decode_64(&p);
4008
Alex Elder49ece552013-05-06 08:37:00 -05004009 ceph_put_snap_context(rbd_dev->header.snapc);
Alex Elder35d489f2012-07-03 16:01:19 -05004010 rbd_dev->header.snapc = snapc;
4011
4012 dout(" snap context seq = %llu, snap_count = %u\n",
Alex Elder57385b52013-04-21 12:14:45 -05004013 (unsigned long long)seq, (unsigned int)snap_count);
Alex Elder35d489f2012-07-03 16:01:19 -05004014out:
4015 kfree(reply_buf);
4016
Alex Elder57385b52013-04-21 12:14:45 -05004017 return ret;
Alex Elder35d489f2012-07-03 16:01:19 -05004018}
4019
Alex Elder54cac612013-04-30 00:44:33 -05004020static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
4021 u64 snap_id)
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004022{
4023 size_t size;
4024 void *reply_buf;
Alex Elder54cac612013-04-30 00:44:33 -05004025 __le64 snapid;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004026 int ret;
4027 void *p;
4028 void *end;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004029 char *snap_name;
4030
4031 size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN;
4032 reply_buf = kmalloc(size, GFP_KERNEL);
4033 if (!reply_buf)
4034 return ERR_PTR(-ENOMEM);
4035
Alex Elder54cac612013-04-30 00:44:33 -05004036 snapid = cpu_to_le64(snap_id);
Alex Elder36be9a72013-01-19 00:30:28 -06004037 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004038 "rbd", "get_snapshot_name",
Alex Elder54cac612013-04-30 00:44:33 -05004039 &snapid, sizeof (snapid),
Alex Eldere2a58ee2013-04-30 00:44:33 -05004040 reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06004041 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderf40eb342013-04-25 15:09:42 -05004042 if (ret < 0) {
4043 snap_name = ERR_PTR(ret);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004044 goto out;
Alex Elderf40eb342013-04-25 15:09:42 -05004045 }
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004046
4047 p = reply_buf;
Alex Elderf40eb342013-04-25 15:09:42 -05004048 end = reply_buf + ret;
Alex Eldere5c35532012-10-25 23:34:41 -05004049 snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elderf40eb342013-04-25 15:09:42 -05004050 if (IS_ERR(snap_name))
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004051 goto out;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004052
Alex Elderf40eb342013-04-25 15:09:42 -05004053 dout(" snap_id 0x%016llx snap_name = %s\n",
Alex Elder54cac612013-04-30 00:44:33 -05004054 (unsigned long long)snap_id, snap_name);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004055out:
4056 kfree(reply_buf);
4057
Alex Elderf40eb342013-04-25 15:09:42 -05004058 return snap_name;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004059}
4060
Alex Eldercc4a38bd2013-04-30 00:44:33 -05004061static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev)
Alex Elder117973f2012-08-31 17:29:55 -05004062{
4063 int ret;
Alex Elder117973f2012-08-31 17:29:55 -05004064
4065 down_write(&rbd_dev->header_rwsem);
4066
Alex Elder117973f2012-08-31 17:29:55 -05004067 ret = rbd_dev_v2_image_size(rbd_dev);
4068 if (ret)
4069 goto out;
Alex Elder117973f2012-08-31 17:29:55 -05004070 rbd_update_mapping_size(rbd_dev);
4071
Alex Eldercc4a38bd2013-04-30 00:44:33 -05004072 ret = rbd_dev_v2_snap_context(rbd_dev);
Alex Elder117973f2012-08-31 17:29:55 -05004073 dout("rbd_dev_v2_snap_context returned %d\n", ret);
4074 if (ret)
4075 goto out;
Alex Elder117973f2012-08-31 17:29:55 -05004076out:
4077 up_write(&rbd_dev->header_rwsem);
4078
4079 return ret;
4080}
4081
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004082static int rbd_bus_add_dev(struct rbd_device *rbd_dev)
4083{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004084 struct device *dev;
Alex Eldercd789ab2012-08-30 00:16:38 -05004085 int ret;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004086
4087 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004088
Alex Eldercd789ab2012-08-30 00:16:38 -05004089 dev = &rbd_dev->dev;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004090 dev->bus = &rbd_bus_type;
4091 dev->type = &rbd_device_type;
4092 dev->parent = &rbd_root_dev;
Alex Elder200a6a82013-04-28 23:32:34 -05004093 dev->release = rbd_dev_device_release;
Alex Elderde71a292012-07-03 16:01:19 -05004094 dev_set_name(dev, "%d", rbd_dev->dev_id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004095 ret = device_register(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004096
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004097 mutex_unlock(&ctl_mutex);
Alex Eldercd789ab2012-08-30 00:16:38 -05004098
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004099 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004100}
4101
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004102static void rbd_bus_del_dev(struct rbd_device *rbd_dev)
4103{
4104 device_unregister(&rbd_dev->dev);
4105}
4106
Alex Eldere2839302012-08-29 17:11:06 -05004107static atomic64_t rbd_dev_id_max = ATOMIC64_INIT(0);
Alex Elder1ddbe942012-01-29 13:57:44 -06004108
4109/*
Alex Elder499afd52012-02-02 08:13:29 -06004110 * Get a unique rbd identifier for the given new rbd_dev, and add
4111 * the rbd_dev to the global list. The minimum rbd id is 1.
Alex Elder1ddbe942012-01-29 13:57:44 -06004112 */
Alex Eldere2839302012-08-29 17:11:06 -05004113static void rbd_dev_id_get(struct rbd_device *rbd_dev)
Alex Elderb7f23c32012-01-29 13:57:43 -06004114{
Alex Eldere2839302012-08-29 17:11:06 -05004115 rbd_dev->dev_id = atomic64_inc_return(&rbd_dev_id_max);
Alex Elder499afd52012-02-02 08:13:29 -06004116
4117 spin_lock(&rbd_dev_list_lock);
4118 list_add_tail(&rbd_dev->node, &rbd_dev_list);
4119 spin_unlock(&rbd_dev_list_lock);
Alex Eldere2839302012-08-29 17:11:06 -05004120 dout("rbd_dev %p given dev id %llu\n", rbd_dev,
4121 (unsigned long long) rbd_dev->dev_id);
Alex Elder1ddbe942012-01-29 13:57:44 -06004122}
Alex Elderb7f23c32012-01-29 13:57:43 -06004123
Alex Elder1ddbe942012-01-29 13:57:44 -06004124/*
Alex Elder499afd52012-02-02 08:13:29 -06004125 * Remove an rbd_dev from the global list, and record that its
4126 * identifier is no longer in use.
Alex Elder1ddbe942012-01-29 13:57:44 -06004127 */
Alex Eldere2839302012-08-29 17:11:06 -05004128static void rbd_dev_id_put(struct rbd_device *rbd_dev)
Alex Elder1ddbe942012-01-29 13:57:44 -06004129{
Alex Elderd184f6b2012-01-29 13:57:44 -06004130 struct list_head *tmp;
Alex Elderde71a292012-07-03 16:01:19 -05004131 int rbd_id = rbd_dev->dev_id;
Alex Elderd184f6b2012-01-29 13:57:44 -06004132 int max_id;
4133
Alex Elderaafb2302012-09-06 16:00:54 -05004134 rbd_assert(rbd_id > 0);
Alex Elder499afd52012-02-02 08:13:29 -06004135
Alex Eldere2839302012-08-29 17:11:06 -05004136 dout("rbd_dev %p released dev id %llu\n", rbd_dev,
4137 (unsigned long long) rbd_dev->dev_id);
Alex Elder499afd52012-02-02 08:13:29 -06004138 spin_lock(&rbd_dev_list_lock);
4139 list_del_init(&rbd_dev->node);
Alex Elderd184f6b2012-01-29 13:57:44 -06004140
4141 /*
4142 * If the id being "put" is not the current maximum, there
4143 * is nothing special we need to do.
4144 */
Alex Eldere2839302012-08-29 17:11:06 -05004145 if (rbd_id != atomic64_read(&rbd_dev_id_max)) {
Alex Elderd184f6b2012-01-29 13:57:44 -06004146 spin_unlock(&rbd_dev_list_lock);
4147 return;
4148 }
4149
4150 /*
4151 * We need to update the current maximum id. Search the
4152 * list to find out what it is. We're more likely to find
4153 * the maximum at the end, so search the list backward.
4154 */
4155 max_id = 0;
4156 list_for_each_prev(tmp, &rbd_dev_list) {
4157 struct rbd_device *rbd_dev;
4158
4159 rbd_dev = list_entry(tmp, struct rbd_device, node);
Alex Elderb213e0b2012-10-10 21:19:13 -07004160 if (rbd_dev->dev_id > max_id)
4161 max_id = rbd_dev->dev_id;
Alex Elderd184f6b2012-01-29 13:57:44 -06004162 }
Alex Elder499afd52012-02-02 08:13:29 -06004163 spin_unlock(&rbd_dev_list_lock);
Alex Elderb7f23c32012-01-29 13:57:43 -06004164
Alex Elder1ddbe942012-01-29 13:57:44 -06004165 /*
Alex Eldere2839302012-08-29 17:11:06 -05004166 * The max id could have been updated by rbd_dev_id_get(), in
Alex Elderd184f6b2012-01-29 13:57:44 -06004167 * which case it now accurately reflects the new maximum.
4168 * Be careful not to overwrite the maximum value in that
4169 * case.
Alex Elder1ddbe942012-01-29 13:57:44 -06004170 */
Alex Eldere2839302012-08-29 17:11:06 -05004171 atomic64_cmpxchg(&rbd_dev_id_max, rbd_id, max_id);
4172 dout(" max dev id has been reset\n");
Alex Elderb7f23c32012-01-29 13:57:43 -06004173}
4174
Alex Eldera725f65e2012-02-02 08:13:30 -06004175/*
Alex Eldere28fff262012-02-02 08:13:30 -06004176 * Skips over white space at *buf, and updates *buf to point to the
4177 * first found non-space character (if any). Returns the length of
Alex Elder593a9e72012-02-07 12:03:37 -06004178 * the token (string of non-white space characters) found. Note
4179 * that *buf must be terminated with '\0'.
Alex Eldere28fff262012-02-02 08:13:30 -06004180 */
4181static inline size_t next_token(const char **buf)
4182{
4183 /*
4184 * These are the characters that produce nonzero for
4185 * isspace() in the "C" and "POSIX" locales.
4186 */
4187 const char *spaces = " \f\n\r\t\v";
4188
4189 *buf += strspn(*buf, spaces); /* Find start of token */
4190
4191 return strcspn(*buf, spaces); /* Return token length */
4192}
4193
4194/*
4195 * Finds the next token in *buf, and if the provided token buffer is
4196 * big enough, copies the found token into it. The result, if
Alex Elder593a9e72012-02-07 12:03:37 -06004197 * copied, is guaranteed to be terminated with '\0'. Note that *buf
4198 * must be terminated with '\0' on entry.
Alex Eldere28fff262012-02-02 08:13:30 -06004199 *
4200 * Returns the length of the token found (not including the '\0').
4201 * Return value will be 0 if no token is found, and it will be >=
4202 * token_size if the token would not fit.
4203 *
Alex Elder593a9e72012-02-07 12:03:37 -06004204 * The *buf pointer will be updated to point beyond the end of the
Alex Eldere28fff262012-02-02 08:13:30 -06004205 * found token. Note that this occurs even if the token buffer is
4206 * too small to hold it.
4207 */
4208static inline size_t copy_token(const char **buf,
4209 char *token,
4210 size_t token_size)
4211{
4212 size_t len;
4213
4214 len = next_token(buf);
4215 if (len < token_size) {
4216 memcpy(token, *buf, len);
4217 *(token + len) = '\0';
4218 }
4219 *buf += len;
4220
4221 return len;
4222}
4223
4224/*
Alex Elderea3352f2012-07-09 21:04:23 -05004225 * Finds the next token in *buf, dynamically allocates a buffer big
4226 * enough to hold a copy of it, and copies the token into the new
4227 * buffer. The copy is guaranteed to be terminated with '\0'. Note
4228 * that a duplicate buffer is created even for a zero-length token.
4229 *
4230 * Returns a pointer to the newly-allocated duplicate, or a null
4231 * pointer if memory for the duplicate was not available. If
4232 * the lenp argument is a non-null pointer, the length of the token
4233 * (not including the '\0') is returned in *lenp.
4234 *
4235 * If successful, the *buf pointer will be updated to point beyond
4236 * the end of the found token.
4237 *
4238 * Note: uses GFP_KERNEL for allocation.
4239 */
4240static inline char *dup_token(const char **buf, size_t *lenp)
4241{
4242 char *dup;
4243 size_t len;
4244
4245 len = next_token(buf);
Alex Elder4caf35f2012-11-01 08:39:27 -05004246 dup = kmemdup(*buf, len + 1, GFP_KERNEL);
Alex Elderea3352f2012-07-09 21:04:23 -05004247 if (!dup)
4248 return NULL;
Alex Elderea3352f2012-07-09 21:04:23 -05004249 *(dup + len) = '\0';
4250 *buf += len;
4251
4252 if (lenp)
4253 *lenp = len;
4254
4255 return dup;
4256}
4257
4258/*
Alex Elder859c31d2012-10-25 23:34:42 -05004259 * Parse the options provided for an "rbd add" (i.e., rbd image
4260 * mapping) request. These arrive via a write to /sys/bus/rbd/add,
4261 * and the data written is passed here via a NUL-terminated buffer.
4262 * Returns 0 if successful or an error code otherwise.
Alex Elderd22f76e2012-07-12 10:46:35 -05004263 *
Alex Elder859c31d2012-10-25 23:34:42 -05004264 * The information extracted from these options is recorded in
4265 * the other parameters which return dynamically-allocated
4266 * structures:
4267 * ceph_opts
4268 * The address of a pointer that will refer to a ceph options
4269 * structure. Caller must release the returned pointer using
4270 * ceph_destroy_options() when it is no longer needed.
4271 * rbd_opts
4272 * Address of an rbd options pointer. Fully initialized by
4273 * this function; caller must release with kfree().
4274 * spec
4275 * Address of an rbd image specification pointer. Fully
4276 * initialized by this function based on parsed options.
4277 * Caller must release with rbd_spec_put().
4278 *
4279 * The options passed take this form:
4280 * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
4281 * where:
4282 * <mon_addrs>
4283 * A comma-separated list of one or more monitor addresses.
4284 * A monitor address is an ip address, optionally followed
4285 * by a port number (separated by a colon).
4286 * I.e.: ip1[:port1][,ip2[:port2]...]
4287 * <options>
4288 * A comma-separated list of ceph and/or rbd options.
4289 * <pool_name>
4290 * The name of the rados pool containing the rbd image.
4291 * <image_name>
4292 * The name of the image in that pool to map.
4293 * <snap_id>
4294 * An optional snapshot id. If provided, the mapping will
4295 * present data from the image at the time that snapshot was
4296 * created. The image head is used if no snapshot id is
4297 * provided. Snapshot mappings are always read-only.
Alex Eldera725f65e2012-02-02 08:13:30 -06004298 */
Alex Elder859c31d2012-10-25 23:34:42 -05004299static int rbd_add_parse_args(const char *buf,
Alex Elderdc79b112012-10-25 23:34:41 -05004300 struct ceph_options **ceph_opts,
Alex Elder859c31d2012-10-25 23:34:42 -05004301 struct rbd_options **opts,
4302 struct rbd_spec **rbd_spec)
Alex Eldera725f65e2012-02-02 08:13:30 -06004303{
Alex Elderd22f76e2012-07-12 10:46:35 -05004304 size_t len;
Alex Elder859c31d2012-10-25 23:34:42 -05004305 char *options;
Alex Elder0ddebc02012-10-25 23:34:41 -05004306 const char *mon_addrs;
Alex Elderecb4dc222013-04-26 09:43:47 -05004307 char *snap_name;
Alex Elder0ddebc02012-10-25 23:34:41 -05004308 size_t mon_addrs_size;
Alex Elder859c31d2012-10-25 23:34:42 -05004309 struct rbd_spec *spec = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05004310 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05004311 struct ceph_options *copts;
Alex Elderdc79b112012-10-25 23:34:41 -05004312 int ret;
Alex Eldere28fff262012-02-02 08:13:30 -06004313
4314 /* The first four tokens are required */
4315
Alex Elder7ef32142012-02-02 08:13:30 -06004316 len = next_token(&buf);
Alex Elder4fb5d6712012-11-01 10:17:15 -05004317 if (!len) {
4318 rbd_warn(NULL, "no monitor address(es) provided");
4319 return -EINVAL;
4320 }
Alex Elder0ddebc02012-10-25 23:34:41 -05004321 mon_addrs = buf;
Alex Elderf28e5652012-10-25 23:34:41 -05004322 mon_addrs_size = len + 1;
Alex Elder7ef32142012-02-02 08:13:30 -06004323 buf += len;
Alex Eldera725f65e2012-02-02 08:13:30 -06004324
Alex Elderdc79b112012-10-25 23:34:41 -05004325 ret = -EINVAL;
Alex Elderf28e5652012-10-25 23:34:41 -05004326 options = dup_token(&buf, NULL);
4327 if (!options)
Alex Elderdc79b112012-10-25 23:34:41 -05004328 return -ENOMEM;
Alex Elder4fb5d6712012-11-01 10:17:15 -05004329 if (!*options) {
4330 rbd_warn(NULL, "no options provided");
4331 goto out_err;
4332 }
Alex Eldera725f65e2012-02-02 08:13:30 -06004333
Alex Elder859c31d2012-10-25 23:34:42 -05004334 spec = rbd_spec_alloc();
4335 if (!spec)
Alex Elderf28e5652012-10-25 23:34:41 -05004336 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05004337
4338 spec->pool_name = dup_token(&buf, NULL);
4339 if (!spec->pool_name)
4340 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05004341 if (!*spec->pool_name) {
4342 rbd_warn(NULL, "no pool name provided");
4343 goto out_err;
4344 }
Alex Eldere28fff262012-02-02 08:13:30 -06004345
Alex Elder69e7a022012-11-01 08:39:26 -05004346 spec->image_name = dup_token(&buf, NULL);
Alex Elder859c31d2012-10-25 23:34:42 -05004347 if (!spec->image_name)
Alex Elderf28e5652012-10-25 23:34:41 -05004348 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05004349 if (!*spec->image_name) {
4350 rbd_warn(NULL, "no image name provided");
4351 goto out_err;
4352 }
Alex Eldere28fff262012-02-02 08:13:30 -06004353
Alex Elderf28e5652012-10-25 23:34:41 -05004354 /*
4355 * Snapshot name is optional; default is to use "-"
4356 * (indicating the head/no snapshot).
4357 */
Alex Elder3feeb8942012-08-31 17:29:52 -05004358 len = next_token(&buf);
Alex Elder820a5f32012-07-09 21:04:24 -05004359 if (!len) {
Alex Elder3feeb8942012-08-31 17:29:52 -05004360 buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
4361 len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
Alex Elderf28e5652012-10-25 23:34:41 -05004362 } else if (len > RBD_MAX_SNAP_NAME_LEN) {
Alex Elderdc79b112012-10-25 23:34:41 -05004363 ret = -ENAMETOOLONG;
Alex Elderf28e5652012-10-25 23:34:41 -05004364 goto out_err;
Alex Elder849b4262012-07-09 21:04:24 -05004365 }
Alex Elderecb4dc222013-04-26 09:43:47 -05004366 snap_name = kmemdup(buf, len + 1, GFP_KERNEL);
4367 if (!snap_name)
Alex Elderf28e5652012-10-25 23:34:41 -05004368 goto out_mem;
Alex Elderecb4dc222013-04-26 09:43:47 -05004369 *(snap_name + len) = '\0';
4370 spec->snap_name = snap_name;
Alex Eldere5c35532012-10-25 23:34:41 -05004371
Alex Elder0ddebc02012-10-25 23:34:41 -05004372 /* Initialize all rbd options to the defaults */
Alex Eldere28fff262012-02-02 08:13:30 -06004373
Alex Elder4e9afeb2012-10-25 23:34:41 -05004374 rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL);
4375 if (!rbd_opts)
4376 goto out_mem;
4377
4378 rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
Alex Elderd22f76e2012-07-12 10:46:35 -05004379
Alex Elder859c31d2012-10-25 23:34:42 -05004380 copts = ceph_parse_options(options, mon_addrs,
Alex Elder0ddebc02012-10-25 23:34:41 -05004381 mon_addrs + mon_addrs_size - 1,
Alex Elder4e9afeb2012-10-25 23:34:41 -05004382 parse_rbd_opts_token, rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05004383 if (IS_ERR(copts)) {
4384 ret = PTR_ERR(copts);
Alex Elderdc79b112012-10-25 23:34:41 -05004385 goto out_err;
4386 }
Alex Elder859c31d2012-10-25 23:34:42 -05004387 kfree(options);
4388
4389 *ceph_opts = copts;
Alex Elder4e9afeb2012-10-25 23:34:41 -05004390 *opts = rbd_opts;
Alex Elder859c31d2012-10-25 23:34:42 -05004391 *rbd_spec = spec;
Alex Elder0ddebc02012-10-25 23:34:41 -05004392
Alex Elderdc79b112012-10-25 23:34:41 -05004393 return 0;
Alex Elderf28e5652012-10-25 23:34:41 -05004394out_mem:
Alex Elderdc79b112012-10-25 23:34:41 -05004395 ret = -ENOMEM;
Alex Elderd22f76e2012-07-12 10:46:35 -05004396out_err:
Alex Elder859c31d2012-10-25 23:34:42 -05004397 kfree(rbd_opts);
4398 rbd_spec_put(spec);
Alex Elderf28e5652012-10-25 23:34:41 -05004399 kfree(options);
Alex Elderd22f76e2012-07-12 10:46:35 -05004400
Alex Elderdc79b112012-10-25 23:34:41 -05004401 return ret;
Alex Eldera725f65e2012-02-02 08:13:30 -06004402}
4403
Alex Elder589d30e2012-07-10 20:30:11 -05004404/*
4405 * An rbd format 2 image has a unique identifier, distinct from the
4406 * name given to it by the user. Internally, that identifier is
4407 * what's used to specify the names of objects related to the image.
4408 *
4409 * A special "rbd id" object is used to map an rbd image name to its
4410 * id. If that object doesn't exist, then there is no v2 rbd image
4411 * with the supplied name.
4412 *
4413 * This function will record the given rbd_dev's image_id field if
4414 * it can be determined, and in that case will return 0. If any
4415 * errors occur a negative errno will be returned and the rbd_dev's
4416 * image_id field will be unchanged (and should be NULL).
4417 */
4418static int rbd_dev_image_id(struct rbd_device *rbd_dev)
4419{
4420 int ret;
4421 size_t size;
4422 char *object_name;
4423 void *response;
Alex Elderc0fba362013-04-25 23:15:08 -05004424 char *image_id;
Alex Elder2f82ee52012-10-30 19:40:33 -05004425
Alex Elder589d30e2012-07-10 20:30:11 -05004426 /*
Alex Elder2c0d0a12012-10-30 19:40:33 -05004427 * When probing a parent image, the image id is already
4428 * known (and the image name likely is not). There's no
Alex Elderc0fba362013-04-25 23:15:08 -05004429 * need to fetch the image id again in this case. We
4430 * do still need to set the image format though.
Alex Elder2c0d0a12012-10-30 19:40:33 -05004431 */
Alex Elderc0fba362013-04-25 23:15:08 -05004432 if (rbd_dev->spec->image_id) {
4433 rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1;
4434
Alex Elder2c0d0a12012-10-30 19:40:33 -05004435 return 0;
Alex Elderc0fba362013-04-25 23:15:08 -05004436 }
Alex Elder2c0d0a12012-10-30 19:40:33 -05004437
4438 /*
Alex Elder589d30e2012-07-10 20:30:11 -05004439 * First, see if the format 2 image id file exists, and if
4440 * so, get the image's persistent id from it.
4441 */
Alex Elder69e7a022012-11-01 08:39:26 -05004442 size = sizeof (RBD_ID_PREFIX) + strlen(rbd_dev->spec->image_name);
Alex Elder589d30e2012-07-10 20:30:11 -05004443 object_name = kmalloc(size, GFP_NOIO);
4444 if (!object_name)
4445 return -ENOMEM;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004446 sprintf(object_name, "%s%s", RBD_ID_PREFIX, rbd_dev->spec->image_name);
Alex Elder589d30e2012-07-10 20:30:11 -05004447 dout("rbd id object name is %s\n", object_name);
4448
4449 /* Response will be an encoded string, which includes a length */
4450
4451 size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
4452 response = kzalloc(size, GFP_NOIO);
4453 if (!response) {
4454 ret = -ENOMEM;
4455 goto out;
4456 }
4457
Alex Elderc0fba362013-04-25 23:15:08 -05004458 /* If it doesn't exist we'll assume it's a format 1 image */
4459
Alex Elder36be9a72013-01-19 00:30:28 -06004460 ret = rbd_obj_method_sync(rbd_dev, object_name,
Alex Elder41579762013-04-21 12:14:45 -05004461 "rbd", "get_id", NULL, 0,
Alex Eldere2a58ee2013-04-30 00:44:33 -05004462 response, RBD_IMAGE_ID_LEN_MAX);
Alex Elder36be9a72013-01-19 00:30:28 -06004463 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderc0fba362013-04-25 23:15:08 -05004464 if (ret == -ENOENT) {
4465 image_id = kstrdup("", GFP_KERNEL);
4466 ret = image_id ? 0 : -ENOMEM;
4467 if (!ret)
4468 rbd_dev->image_format = 1;
4469 } else if (ret > sizeof (__le32)) {
4470 void *p = response;
Alex Elder589d30e2012-07-10 20:30:11 -05004471
Alex Elderc0fba362013-04-25 23:15:08 -05004472 image_id = ceph_extract_encoded_string(&p, p + ret,
Alex Elder979ed482012-11-01 08:39:26 -05004473 NULL, GFP_NOIO);
Alex Elderc0fba362013-04-25 23:15:08 -05004474 ret = IS_ERR(image_id) ? PTR_ERR(image_id) : 0;
4475 if (!ret)
4476 rbd_dev->image_format = 2;
Alex Elder589d30e2012-07-10 20:30:11 -05004477 } else {
Alex Elderc0fba362013-04-25 23:15:08 -05004478 ret = -EINVAL;
4479 }
4480
4481 if (!ret) {
4482 rbd_dev->spec->image_id = image_id;
4483 dout("image_id is %s\n", image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05004484 }
4485out:
4486 kfree(response);
4487 kfree(object_name);
4488
4489 return ret;
4490}
4491
Alex Elder6fd48b32013-04-28 23:32:34 -05004492/* Undo whatever state changes are made by v1 or v2 image probe */
4493
4494static void rbd_dev_unprobe(struct rbd_device *rbd_dev)
4495{
4496 struct rbd_image_header *header;
4497
4498 rbd_dev_remove_parent(rbd_dev);
4499 rbd_spec_put(rbd_dev->parent_spec);
4500 rbd_dev->parent_spec = NULL;
4501 rbd_dev->parent_overlap = 0;
4502
4503 /* Free dynamic fields from the header, then zero it out */
4504
4505 header = &rbd_dev->header;
Alex Elder812164f82013-04-30 00:44:32 -05004506 ceph_put_snap_context(header->snapc);
Alex Elder6fd48b32013-04-28 23:32:34 -05004507 kfree(header->snap_sizes);
4508 kfree(header->snap_names);
4509 kfree(header->object_prefix);
4510 memset(header, 0, sizeof (*header));
4511}
4512
Alex Eldera30b71b2012-07-10 20:30:11 -05004513static int rbd_dev_v1_probe(struct rbd_device *rbd_dev)
4514{
4515 int ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05004516
4517 /* Populate rbd image metadata */
4518
4519 ret = rbd_read_header(rbd_dev, &rbd_dev->header);
4520 if (ret < 0)
4521 goto out_err;
Alex Elder86b00e02012-10-25 23:34:42 -05004522
4523 /* Version 1 images have no parent (no layering) */
4524
4525 rbd_dev->parent_spec = NULL;
4526 rbd_dev->parent_overlap = 0;
4527
Alex Eldera30b71b2012-07-10 20:30:11 -05004528 dout("discovered version 1 image, header name is %s\n",
4529 rbd_dev->header_name);
4530
4531 return 0;
4532
4533out_err:
4534 kfree(rbd_dev->header_name);
4535 rbd_dev->header_name = NULL;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004536 kfree(rbd_dev->spec->image_id);
4537 rbd_dev->spec->image_id = NULL;
Alex Eldera30b71b2012-07-10 20:30:11 -05004538
4539 return ret;
4540}
4541
4542static int rbd_dev_v2_probe(struct rbd_device *rbd_dev)
4543{
Alex Elder9d475de2012-07-03 16:01:19 -05004544 int ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05004545
Alex Elder9d475de2012-07-03 16:01:19 -05004546 ret = rbd_dev_v2_image_size(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05004547 if (ret)
Alex Elder9d475de2012-07-03 16:01:19 -05004548 goto out_err;
Alex Elder1e130192012-07-03 16:01:19 -05004549
4550 /* Get the object prefix (a.k.a. block_name) for the image */
4551
4552 ret = rbd_dev_v2_object_prefix(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05004553 if (ret)
Alex Elder1e130192012-07-03 16:01:19 -05004554 goto out_err;
Alex Elderb1b54022012-07-03 16:01:19 -05004555
Alex Elderd8891402012-10-09 13:50:17 -07004556 /* Get the and check features for the image */
Alex Elderb1b54022012-07-03 16:01:19 -05004557
4558 ret = rbd_dev_v2_features(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05004559 if (ret)
Alex Elderb1b54022012-07-03 16:01:19 -05004560 goto out_err;
Alex Elder35d489f2012-07-03 16:01:19 -05004561
Alex Elder86b00e02012-10-25 23:34:42 -05004562 /* If the image supports layering, get the parent info */
4563
4564 if (rbd_dev->header.features & RBD_FEATURE_LAYERING) {
4565 ret = rbd_dev_v2_parent_info(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05004566 if (ret)
Alex Elder86b00e02012-10-25 23:34:42 -05004567 goto out_err;
Alex Elder96882f52013-04-30 00:44:32 -05004568
4569 /*
4570 * Don't print a warning for parent images. We can
4571 * tell this point because we won't know its pool
4572 * name yet (just its pool id).
4573 */
4574 if (rbd_dev->spec->pool_name)
4575 rbd_warn(rbd_dev, "WARNING: kernel layering "
4576 "is EXPERIMENTAL!");
Alex Elder86b00e02012-10-25 23:34:42 -05004577 }
4578
Alex Eldercc070d52013-04-21 12:14:45 -05004579 /* If the image supports fancy striping, get its parameters */
4580
4581 if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) {
4582 ret = rbd_dev_v2_striping_info(rbd_dev);
4583 if (ret < 0)
4584 goto out_err;
4585 }
4586
Alex Elder6e14b1a2012-07-03 16:01:19 -05004587 /* crypto and compression type aren't (yet) supported for v2 images */
Alex Elder35d489f2012-07-03 16:01:19 -05004588
Alex Elder6e14b1a2012-07-03 16:01:19 -05004589 rbd_dev->header.crypt_type = 0;
4590 rbd_dev->header.comp_type = 0;
4591
4592 /* Get the snapshot context, plus the header version */
4593
Alex Eldercc4a38bd2013-04-30 00:44:33 -05004594 ret = rbd_dev_v2_snap_context(rbd_dev);
Alex Elder35d489f2012-07-03 16:01:19 -05004595 if (ret)
4596 goto out_err;
Alex Elder6e14b1a2012-07-03 16:01:19 -05004597
Alex Eldera30b71b2012-07-10 20:30:11 -05004598 dout("discovered version 2 image, header name is %s\n",
4599 rbd_dev->header_name);
4600
Alex Elder35152972012-08-31 17:29:55 -05004601 return 0;
Alex Elder9d475de2012-07-03 16:01:19 -05004602out_err:
Alex Elder86b00e02012-10-25 23:34:42 -05004603 rbd_dev->parent_overlap = 0;
4604 rbd_spec_put(rbd_dev->parent_spec);
4605 rbd_dev->parent_spec = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05004606 kfree(rbd_dev->header_name);
4607 rbd_dev->header_name = NULL;
Alex Elder1e130192012-07-03 16:01:19 -05004608 kfree(rbd_dev->header.object_prefix);
4609 rbd_dev->header.object_prefix = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05004610
4611 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05004612}
4613
Alex Elder124afba2013-04-26 15:44:36 -05004614static int rbd_dev_probe_parent(struct rbd_device *rbd_dev)
Alex Elder83a06262012-10-30 15:47:17 -05004615{
Alex Elder2f82ee52012-10-30 19:40:33 -05004616 struct rbd_device *parent = NULL;
Alex Elder124afba2013-04-26 15:44:36 -05004617 struct rbd_spec *parent_spec;
4618 struct rbd_client *rbdc;
4619 int ret;
4620
4621 if (!rbd_dev->parent_spec)
4622 return 0;
4623 /*
4624 * We need to pass a reference to the client and the parent
4625 * spec when creating the parent rbd_dev. Images related by
4626 * parent/child relationships always share both.
4627 */
4628 parent_spec = rbd_spec_get(rbd_dev->parent_spec);
4629 rbdc = __rbd_get_client(rbd_dev->rbd_client);
4630
4631 ret = -ENOMEM;
4632 parent = rbd_dev_create(rbdc, parent_spec);
4633 if (!parent)
4634 goto out_err;
4635
4636 ret = rbd_dev_image_probe(parent);
4637 if (ret < 0)
4638 goto out_err;
4639 rbd_dev->parent = parent;
4640
4641 return 0;
4642out_err:
4643 if (parent) {
4644 rbd_spec_put(rbd_dev->parent_spec);
4645 kfree(rbd_dev->header_name);
4646 rbd_dev_destroy(parent);
4647 } else {
4648 rbd_put_client(rbdc);
4649 rbd_spec_put(parent_spec);
4650 }
4651
4652 return ret;
4653}
4654
Alex Elder200a6a82013-04-28 23:32:34 -05004655static int rbd_dev_device_setup(struct rbd_device *rbd_dev)
Alex Elder124afba2013-04-26 15:44:36 -05004656{
Alex Elder83a06262012-10-30 15:47:17 -05004657 int ret;
Alex Elder83a06262012-10-30 15:47:17 -05004658
Alex Elderd1cf5782013-04-27 09:59:30 -05004659 ret = rbd_dev_mapping_set(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05004660 if (ret)
Alex Elder9bb81c92013-04-27 09:59:30 -05004661 return ret;
Alex Elder5de10f32013-04-26 15:44:37 -05004662
Alex Elder83a06262012-10-30 15:47:17 -05004663 /* generate unique id: find highest unique id, add one */
4664 rbd_dev_id_get(rbd_dev);
4665
4666 /* Fill in the device name, now that we have its id. */
4667 BUILD_BUG_ON(DEV_NAME_LEN
4668 < sizeof (RBD_DRV_NAME) + MAX_INT_FORMAT_WIDTH);
4669 sprintf(rbd_dev->name, "%s%d", RBD_DRV_NAME, rbd_dev->dev_id);
4670
4671 /* Get our block major device number. */
4672
4673 ret = register_blkdev(0, rbd_dev->name);
4674 if (ret < 0)
4675 goto err_out_id;
4676 rbd_dev->major = ret;
4677
4678 /* Set up the blkdev mapping. */
4679
4680 ret = rbd_init_disk(rbd_dev);
4681 if (ret)
4682 goto err_out_blkdev;
4683
4684 ret = rbd_bus_add_dev(rbd_dev);
4685 if (ret)
4686 goto err_out_disk;
4687
Alex Elder83a06262012-10-30 15:47:17 -05004688 /* Everything's ready. Announce the disk to the world. */
4689
Alex Elderb5156e72013-04-26 15:44:36 -05004690 set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
Alex Elder129b79d2013-04-26 15:44:36 -05004691 set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
Alex Elder83a06262012-10-30 15:47:17 -05004692 add_disk(rbd_dev->disk);
4693
4694 pr_info("%s: added with size 0x%llx\n", rbd_dev->disk->disk_name,
4695 (unsigned long long) rbd_dev->mapping.size);
4696
4697 return ret;
Alex Elder2f82ee52012-10-30 19:40:33 -05004698
Alex Elder83a06262012-10-30 15:47:17 -05004699err_out_disk:
4700 rbd_free_disk(rbd_dev);
4701err_out_blkdev:
4702 unregister_blkdev(rbd_dev->major, rbd_dev->name);
4703err_out_id:
4704 rbd_dev_id_put(rbd_dev);
Alex Elderd1cf5782013-04-27 09:59:30 -05004705 rbd_dev_mapping_clear(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05004706
4707 return ret;
4708}
4709
Alex Elder332bb122013-04-27 09:59:30 -05004710static int rbd_dev_header_name(struct rbd_device *rbd_dev)
4711{
4712 struct rbd_spec *spec = rbd_dev->spec;
4713 size_t size;
4714
4715 /* Record the header object name for this rbd image. */
4716
4717 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
4718
4719 if (rbd_dev->image_format == 1)
4720 size = strlen(spec->image_name) + sizeof (RBD_SUFFIX);
4721 else
4722 size = sizeof (RBD_HEADER_PREFIX) + strlen(spec->image_id);
4723
4724 rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
4725 if (!rbd_dev->header_name)
4726 return -ENOMEM;
4727
4728 if (rbd_dev->image_format == 1)
4729 sprintf(rbd_dev->header_name, "%s%s",
4730 spec->image_name, RBD_SUFFIX);
4731 else
4732 sprintf(rbd_dev->header_name, "%s%s",
4733 RBD_HEADER_PREFIX, spec->image_id);
4734 return 0;
4735}
4736
Alex Elder200a6a82013-04-28 23:32:34 -05004737static void rbd_dev_image_release(struct rbd_device *rbd_dev)
4738{
Alex Elder6fd48b32013-04-28 23:32:34 -05004739 int ret;
4740
Alex Elder6fd48b32013-04-28 23:32:34 -05004741 rbd_dev_unprobe(rbd_dev);
4742 ret = rbd_dev_header_watch_sync(rbd_dev, 0);
4743 if (ret)
4744 rbd_warn(rbd_dev, "failed to cancel watch event (%d)\n", ret);
Alex Elder200a6a82013-04-28 23:32:34 -05004745 kfree(rbd_dev->header_name);
Alex Elder6fd48b32013-04-28 23:32:34 -05004746 rbd_dev->header_name = NULL;
4747 rbd_dev->image_format = 0;
4748 kfree(rbd_dev->spec->image_id);
4749 rbd_dev->spec->image_id = NULL;
4750
Alex Elder200a6a82013-04-28 23:32:34 -05004751 rbd_dev_destroy(rbd_dev);
4752}
4753
Alex Eldera30b71b2012-07-10 20:30:11 -05004754/*
4755 * Probe for the existence of the header object for the given rbd
4756 * device. For format 2 images this includes determining the image
4757 * id.
4758 */
Alex Elder71f293e2013-04-26 09:43:48 -05004759static int rbd_dev_image_probe(struct rbd_device *rbd_dev)
Alex Eldera30b71b2012-07-10 20:30:11 -05004760{
4761 int ret;
Alex Elderb644de22013-04-27 09:59:31 -05004762 int tmp;
Alex Eldera30b71b2012-07-10 20:30:11 -05004763
4764 /*
4765 * Get the id from the image id object. If it's not a
4766 * format 2 image, we'll get ENOENT back, and we'll assume
4767 * it's a format 1 image.
4768 */
4769 ret = rbd_dev_image_id(rbd_dev);
4770 if (ret)
Alex Elderc0fba362013-04-25 23:15:08 -05004771 return ret;
4772 rbd_assert(rbd_dev->spec->image_id);
4773 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
4774
Alex Elder332bb122013-04-27 09:59:30 -05004775 ret = rbd_dev_header_name(rbd_dev);
4776 if (ret)
4777 goto err_out_format;
4778
Alex Elderb644de22013-04-27 09:59:31 -05004779 ret = rbd_dev_header_watch_sync(rbd_dev, 1);
4780 if (ret)
4781 goto out_header_name;
4782
Alex Elderc0fba362013-04-25 23:15:08 -05004783 if (rbd_dev->image_format == 1)
Alex Eldera30b71b2012-07-10 20:30:11 -05004784 ret = rbd_dev_v1_probe(rbd_dev);
4785 else
4786 ret = rbd_dev_v2_probe(rbd_dev);
Alex Elder5655c4d2013-04-25 23:15:08 -05004787 if (ret)
Alex Elderb644de22013-04-27 09:59:31 -05004788 goto err_out_watch;
Alex Elder83a06262012-10-30 15:47:17 -05004789
Alex Elder9bb81c92013-04-27 09:59:30 -05004790 ret = rbd_dev_spec_update(rbd_dev);
4791 if (ret)
Alex Elder33dca392013-04-30 00:44:33 -05004792 goto err_out_probe;
Alex Elder9bb81c92013-04-27 09:59:30 -05004793
4794 ret = rbd_dev_probe_parent(rbd_dev);
Alex Elder6fd48b32013-04-28 23:32:34 -05004795 if (!ret)
4796 return 0;
Alex Elder83a06262012-10-30 15:47:17 -05004797
Alex Elder6fd48b32013-04-28 23:32:34 -05004798err_out_probe:
4799 rbd_dev_unprobe(rbd_dev);
Alex Elderb644de22013-04-27 09:59:31 -05004800err_out_watch:
4801 tmp = rbd_dev_header_watch_sync(rbd_dev, 0);
4802 if (tmp)
4803 rbd_warn(rbd_dev, "unable to tear down watch request\n");
Alex Elder332bb122013-04-27 09:59:30 -05004804out_header_name:
4805 kfree(rbd_dev->header_name);
4806 rbd_dev->header_name = NULL;
4807err_out_format:
4808 rbd_dev->image_format = 0;
Alex Elder5655c4d2013-04-25 23:15:08 -05004809 kfree(rbd_dev->spec->image_id);
4810 rbd_dev->spec->image_id = NULL;
4811
4812 dout("probe failed, returning %d\n", ret);
4813
4814 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05004815}
4816
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07004817static ssize_t rbd_add(struct bus_type *bus,
4818 const char *buf,
4819 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004820{
Alex Eldercb8627c2012-07-09 21:04:23 -05004821 struct rbd_device *rbd_dev = NULL;
Alex Elderdc79b112012-10-25 23:34:41 -05004822 struct ceph_options *ceph_opts = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05004823 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05004824 struct rbd_spec *spec = NULL;
Alex Elder9d3997f2012-10-25 23:34:42 -05004825 struct rbd_client *rbdc;
Alex Elder27cc2592012-02-02 08:13:30 -06004826 struct ceph_osd_client *osdc;
4827 int rc = -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004828
4829 if (!try_module_get(THIS_MODULE))
4830 return -ENODEV;
4831
Alex Eldera725f65e2012-02-02 08:13:30 -06004832 /* parse add command */
Alex Elder859c31d2012-10-25 23:34:42 -05004833 rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec);
Alex Elderdc79b112012-10-25 23:34:41 -05004834 if (rc < 0)
Alex Elderbd4ba652012-10-25 23:34:42 -05004835 goto err_out_module;
Alex Eldera725f65e2012-02-02 08:13:30 -06004836
Alex Elder9d3997f2012-10-25 23:34:42 -05004837 rbdc = rbd_get_client(ceph_opts);
4838 if (IS_ERR(rbdc)) {
4839 rc = PTR_ERR(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05004840 goto err_out_args;
Alex Elder9d3997f2012-10-25 23:34:42 -05004841 }
Alex Elderc53d5892012-10-25 23:34:42 -05004842 ceph_opts = NULL; /* rbd_dev client now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004843
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004844 /* pick the pool */
Alex Elder9d3997f2012-10-25 23:34:42 -05004845 osdc = &rbdc->client->osdc;
Alex Elder859c31d2012-10-25 23:34:42 -05004846 rc = ceph_pg_poolid_by_name(osdc->osdmap, spec->pool_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004847 if (rc < 0)
4848 goto err_out_client;
Alex Elderc0cd10db2013-04-26 09:43:47 -05004849 spec->pool_id = (u64)rc;
Alex Elder859c31d2012-10-25 23:34:42 -05004850
Alex Elder0903e872012-11-14 12:25:19 -06004851 /* The ceph file layout needs to fit pool id in 32 bits */
4852
Alex Elderc0cd10db2013-04-26 09:43:47 -05004853 if (spec->pool_id > (u64)U32_MAX) {
4854 rbd_warn(NULL, "pool id too large (%llu > %u)\n",
4855 (unsigned long long)spec->pool_id, U32_MAX);
Alex Elder0903e872012-11-14 12:25:19 -06004856 rc = -EIO;
4857 goto err_out_client;
4858 }
4859
Alex Elderc53d5892012-10-25 23:34:42 -05004860 rbd_dev = rbd_dev_create(rbdc, spec);
Alex Elderbd4ba652012-10-25 23:34:42 -05004861 if (!rbd_dev)
4862 goto err_out_client;
Alex Elderc53d5892012-10-25 23:34:42 -05004863 rbdc = NULL; /* rbd_dev now owns this */
4864 spec = NULL; /* rbd_dev now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004865
Alex Elderbd4ba652012-10-25 23:34:42 -05004866 rbd_dev->mapping.read_only = rbd_opts->read_only;
Alex Elderc53d5892012-10-25 23:34:42 -05004867 kfree(rbd_opts);
4868 rbd_opts = NULL; /* done with this */
Alex Elderbd4ba652012-10-25 23:34:42 -05004869
Alex Elder71f293e2013-04-26 09:43:48 -05004870 rc = rbd_dev_image_probe(rbd_dev);
Alex Eldera30b71b2012-07-10 20:30:11 -05004871 if (rc < 0)
Alex Elderc53d5892012-10-25 23:34:42 -05004872 goto err_out_rbd_dev;
Alex Elder05fd6f62012-08-29 17:11:07 -05004873
Alex Elderb536f692013-04-28 23:32:34 -05004874 rc = rbd_dev_device_setup(rbd_dev);
4875 if (!rc)
4876 return count;
4877
4878 rbd_dev_image_release(rbd_dev);
Alex Elderc53d5892012-10-25 23:34:42 -05004879err_out_rbd_dev:
4880 rbd_dev_destroy(rbd_dev);
Alex Elderbd4ba652012-10-25 23:34:42 -05004881err_out_client:
Alex Elder9d3997f2012-10-25 23:34:42 -05004882 rbd_put_client(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05004883err_out_args:
Alex Elder78cea762012-10-25 23:34:41 -05004884 if (ceph_opts)
4885 ceph_destroy_options(ceph_opts);
Alex Elder4e9afeb2012-10-25 23:34:41 -05004886 kfree(rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05004887 rbd_spec_put(spec);
Alex Elderbd4ba652012-10-25 23:34:42 -05004888err_out_module:
4889 module_put(THIS_MODULE);
Alex Elder27cc2592012-02-02 08:13:30 -06004890
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004891 dout("Error adding device %s\n", buf);
Alex Elder27cc2592012-02-02 08:13:30 -06004892
Alex Elderc0cd10db2013-04-26 09:43:47 -05004893 return (ssize_t)rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004894}
4895
Alex Elderde71a292012-07-03 16:01:19 -05004896static struct rbd_device *__rbd_get_dev(unsigned long dev_id)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004897{
4898 struct list_head *tmp;
4899 struct rbd_device *rbd_dev;
4900
Alex Eldere124a82f2012-01-29 13:57:44 -06004901 spin_lock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004902 list_for_each(tmp, &rbd_dev_list) {
4903 rbd_dev = list_entry(tmp, struct rbd_device, node);
Alex Elderde71a292012-07-03 16:01:19 -05004904 if (rbd_dev->dev_id == dev_id) {
Alex Eldere124a82f2012-01-29 13:57:44 -06004905 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004906 return rbd_dev;
Alex Eldere124a82f2012-01-29 13:57:44 -06004907 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004908 }
Alex Eldere124a82f2012-01-29 13:57:44 -06004909 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004910 return NULL;
4911}
4912
Alex Elder200a6a82013-04-28 23:32:34 -05004913static void rbd_dev_device_release(struct device *dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004914{
Alex Elder593a9e72012-02-07 12:03:37 -06004915 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004916
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004917 rbd_free_disk(rbd_dev);
Alex Elder200a6a82013-04-28 23:32:34 -05004918 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
4919 rbd_dev_clear_mapping(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004920 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Alex Elder200a6a82013-04-28 23:32:34 -05004921 rbd_dev->major = 0;
Alex Eldere2839302012-08-29 17:11:06 -05004922 rbd_dev_id_put(rbd_dev);
Alex Elderd1cf5782013-04-27 09:59:30 -05004923 rbd_dev_mapping_clear(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004924}
4925
Alex Elder05a46af2013-04-26 15:44:36 -05004926static void rbd_dev_remove_parent(struct rbd_device *rbd_dev)
4927{
Alex Elderad945fc2013-04-26 15:44:36 -05004928 while (rbd_dev->parent) {
Alex Elder05a46af2013-04-26 15:44:36 -05004929 struct rbd_device *first = rbd_dev;
4930 struct rbd_device *second = first->parent;
4931 struct rbd_device *third;
4932
4933 /*
4934 * Follow to the parent with no grandparent and
4935 * remove it.
4936 */
4937 while (second && (third = second->parent)) {
4938 first = second;
4939 second = third;
4940 }
Alex Elderad945fc2013-04-26 15:44:36 -05004941 rbd_assert(second);
Alex Elder8ad42cd2013-04-28 23:32:34 -05004942 rbd_dev_image_release(second);
Alex Elderad945fc2013-04-26 15:44:36 -05004943 first->parent = NULL;
4944 first->parent_overlap = 0;
4945
4946 rbd_assert(first->parent_spec);
Alex Elder05a46af2013-04-26 15:44:36 -05004947 rbd_spec_put(first->parent_spec);
4948 first->parent_spec = NULL;
Alex Elder05a46af2013-04-26 15:44:36 -05004949 }
4950}
4951
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004952static ssize_t rbd_remove(struct bus_type *bus,
4953 const char *buf,
4954 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004955{
4956 struct rbd_device *rbd_dev = NULL;
Alex Elder0d8189e2013-04-27 09:59:30 -05004957 int target_id;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004958 unsigned long ul;
Alex Elder0d8189e2013-04-27 09:59:30 -05004959 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004960
Alex Elder0d8189e2013-04-27 09:59:30 -05004961 ret = strict_strtoul(buf, 10, &ul);
4962 if (ret)
4963 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004964
4965 /* convert to int; abort if we lost anything in the conversion */
4966 target_id = (int) ul;
4967 if (target_id != ul)
4968 return -EINVAL;
4969
4970 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
4971
4972 rbd_dev = __rbd_get_dev(target_id);
4973 if (!rbd_dev) {
4974 ret = -ENOENT;
4975 goto done;
4976 }
4977
Alex Eldera14ea262013-02-05 13:23:12 -06004978 spin_lock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -06004979 if (rbd_dev->open_count)
Alex Elder42382b72012-11-16 09:29:16 -06004980 ret = -EBUSY;
Alex Elderb82d1672013-01-14 12:43:31 -06004981 else
4982 set_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags);
Alex Eldera14ea262013-02-05 13:23:12 -06004983 spin_unlock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -06004984 if (ret < 0)
Alex Elder42382b72012-11-16 09:29:16 -06004985 goto done;
Alex Elder0d8189e2013-04-27 09:59:30 -05004986 ret = count;
Alex Elderb4808152013-04-26 15:44:36 -05004987 rbd_bus_del_dev(rbd_dev);
Alex Elder8ad42cd2013-04-28 23:32:34 -05004988 rbd_dev_image_release(rbd_dev);
Alex Elder79ab7552013-04-28 23:32:34 -05004989 module_put(THIS_MODULE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004990done:
4991 mutex_unlock(&ctl_mutex);
Alex Elderaafb2302012-09-06 16:00:54 -05004992
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004993 return ret;
4994}
4995
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004996/*
4997 * create control files in sysfs
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004998 * /sys/bus/rbd/...
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004999 */
5000static int rbd_sysfs_init(void)
5001{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005002 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005003
Alex Elderfed4c142012-02-07 12:03:36 -06005004 ret = device_register(&rbd_root_dev);
Alex Elder21079782012-01-24 10:08:36 -06005005 if (ret < 0)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005006 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005007
Alex Elderfed4c142012-02-07 12:03:36 -06005008 ret = bus_register(&rbd_bus_type);
5009 if (ret < 0)
5010 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005011
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005012 return ret;
5013}
5014
5015static void rbd_sysfs_cleanup(void)
5016{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005017 bus_unregister(&rbd_bus_type);
Alex Elderfed4c142012-02-07 12:03:36 -06005018 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005019}
5020
Alex Elder1c2a9df2013-05-01 12:43:03 -05005021static int rbd_slab_init(void)
5022{
5023 rbd_assert(!rbd_img_request_cache);
5024 rbd_img_request_cache = kmem_cache_create("rbd_img_request",
5025 sizeof (struct rbd_img_request),
5026 __alignof__(struct rbd_img_request),
5027 0, NULL);
Alex Elder868311b2013-05-01 12:43:03 -05005028 if (!rbd_img_request_cache)
5029 return -ENOMEM;
5030
5031 rbd_assert(!rbd_obj_request_cache);
5032 rbd_obj_request_cache = kmem_cache_create("rbd_obj_request",
5033 sizeof (struct rbd_obj_request),
5034 __alignof__(struct rbd_obj_request),
5035 0, NULL);
Alex Elder78c2a442013-05-01 12:43:04 -05005036 if (!rbd_obj_request_cache)
5037 goto out_err;
5038
5039 rbd_assert(!rbd_segment_name_cache);
5040 rbd_segment_name_cache = kmem_cache_create("rbd_segment_name",
5041 MAX_OBJ_NAME_SIZE + 1, 1, 0, NULL);
5042 if (rbd_segment_name_cache)
Alex Elder1c2a9df2013-05-01 12:43:03 -05005043 return 0;
Alex Elder78c2a442013-05-01 12:43:04 -05005044out_err:
5045 if (rbd_obj_request_cache) {
5046 kmem_cache_destroy(rbd_obj_request_cache);
5047 rbd_obj_request_cache = NULL;
5048 }
Alex Elder1c2a9df2013-05-01 12:43:03 -05005049
Alex Elder868311b2013-05-01 12:43:03 -05005050 kmem_cache_destroy(rbd_img_request_cache);
5051 rbd_img_request_cache = NULL;
5052
Alex Elder1c2a9df2013-05-01 12:43:03 -05005053 return -ENOMEM;
5054}
5055
5056static void rbd_slab_exit(void)
5057{
Alex Elder78c2a442013-05-01 12:43:04 -05005058 rbd_assert(rbd_segment_name_cache);
5059 kmem_cache_destroy(rbd_segment_name_cache);
5060 rbd_segment_name_cache = NULL;
5061
Alex Elder868311b2013-05-01 12:43:03 -05005062 rbd_assert(rbd_obj_request_cache);
5063 kmem_cache_destroy(rbd_obj_request_cache);
5064 rbd_obj_request_cache = NULL;
5065
Alex Elder1c2a9df2013-05-01 12:43:03 -05005066 rbd_assert(rbd_img_request_cache);
5067 kmem_cache_destroy(rbd_img_request_cache);
5068 rbd_img_request_cache = NULL;
5069}
5070
Alex Eldercc344fa2013-02-19 12:25:56 -06005071static int __init rbd_init(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005072{
5073 int rc;
5074
Alex Elder1e32d342013-01-30 11:13:33 -06005075 if (!libceph_compatible(NULL)) {
5076 rbd_warn(NULL, "libceph incompatibility (quitting)");
5077
5078 return -EINVAL;
5079 }
Alex Elder1c2a9df2013-05-01 12:43:03 -05005080 rc = rbd_slab_init();
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005081 if (rc)
5082 return rc;
Alex Elder1c2a9df2013-05-01 12:43:03 -05005083 rc = rbd_sysfs_init();
5084 if (rc)
5085 rbd_slab_exit();
5086 else
5087 pr_info("loaded " RBD_DRV_NAME_LONG "\n");
5088
5089 return rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005090}
5091
Alex Eldercc344fa2013-02-19 12:25:56 -06005092static void __exit rbd_exit(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005093{
5094 rbd_sysfs_cleanup();
Alex Elder1c2a9df2013-05-01 12:43:03 -05005095 rbd_slab_exit();
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005096}
5097
5098module_init(rbd_init);
5099module_exit(rbd_exit);
5100
5101MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
5102MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
5103MODULE_DESCRIPTION("rados block device");
5104
5105/* following authorship retained from original osdblk.c */
5106MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
5107
5108MODULE_LICENSE("GPL");