blob: 8875bebbacfca13e46536c31c8c961da74ec19c7 [file] [log] [blame]
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001/*
2 rbd.c -- Export ceph rados objects as a Linux block device
3
4
5 based on drivers/block/osdblk.c:
6
7 Copyright 2009 Red Hat, Inc.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; see the file COPYING. If not, write to
20 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21
22
23
Yehuda Sadehdfc56062010-11-19 14:51:04 -080024 For usage instructions, please refer to:
Yehuda Sadeh602adf42010-08-12 16:11:25 -070025
Yehuda Sadehdfc56062010-11-19 14:51:04 -080026 Documentation/ABI/testing/sysfs-bus-rbd
Yehuda Sadeh602adf42010-08-12 16:11:25 -070027
28 */
29
30#include <linux/ceph/libceph.h>
31#include <linux/ceph/osd_client.h>
32#include <linux/ceph/mon_client.h>
33#include <linux/ceph/decode.h>
Yehuda Sadeh59c2be12011-03-21 15:10:11 -070034#include <linux/parser.h>
Yehuda Sadeh602adf42010-08-12 16:11:25 -070035
36#include <linux/kernel.h>
37#include <linux/device.h>
38#include <linux/module.h>
39#include <linux/fs.h>
40#include <linux/blkdev.h>
41
42#include "rbd_types.h"
43
Alex Elderaafb2302012-09-06 16:00:54 -050044#define RBD_DEBUG /* Activate rbd_assert() calls */
45
Alex Elder593a9e72012-02-07 12:03:37 -060046/*
47 * The basic unit of block I/O is a sector. It is interpreted in a
48 * number of contexts in Linux (blk, bio, genhd), but the default is
49 * universally 512 bytes. These symbols are just slightly more
50 * meaningful than the bare numbers they represent.
51 */
52#define SECTOR_SHIFT 9
53#define SECTOR_SIZE (1ULL << SECTOR_SHIFT)
54
Alex Elderf0f8cef2012-01-29 13:57:44 -060055#define RBD_DRV_NAME "rbd"
56#define RBD_DRV_NAME_LONG "rbd (rados block device)"
Yehuda Sadeh602adf42010-08-12 16:11:25 -070057
58#define RBD_MINORS_PER_MAJOR 256 /* max minors per blkdev */
59
Alex Elderd4b125e2012-07-03 16:01:19 -050060#define RBD_SNAP_DEV_NAME_PREFIX "snap_"
61#define RBD_MAX_SNAP_NAME_LEN \
62 (NAME_MAX - (sizeof (RBD_SNAP_DEV_NAME_PREFIX) - 1))
63
Alex Elder35d489f2012-07-03 16:01:19 -050064#define RBD_MAX_SNAP_COUNT 510 /* allows max snapc to fit in 4KB */
Yehuda Sadeh602adf42010-08-12 16:11:25 -070065
66#define RBD_SNAP_HEAD_NAME "-"
67
Alex Elder9e15b772012-10-30 19:40:33 -050068/* This allows a single page to hold an image name sent by OSD */
69#define RBD_IMAGE_NAME_LEN_MAX (PAGE_SIZE - sizeof (__le32) - 1)
Alex Elder1e130192012-07-03 16:01:19 -050070#define RBD_IMAGE_ID_LEN_MAX 64
Alex Elder9e15b772012-10-30 19:40:33 -050071
Alex Elder1e130192012-07-03 16:01:19 -050072#define RBD_OBJ_PREFIX_LEN_MAX 64
Alex Elder589d30e2012-07-10 20:30:11 -050073
Alex Elderd8891402012-10-09 13:50:17 -070074/* Feature bits */
75
Alex Elder5cbf6f122013-04-11 09:29:48 -050076#define RBD_FEATURE_LAYERING (1<<0)
77#define RBD_FEATURE_STRIPINGV2 (1<<1)
78#define RBD_FEATURES_ALL \
79 (RBD_FEATURE_LAYERING | RBD_FEATURE_STRIPINGV2)
Alex Elderd8891402012-10-09 13:50:17 -070080
81/* Features supported by this (client software) implementation. */
82
Alex Elder770eba62012-10-25 23:34:40 -050083#define RBD_FEATURES_SUPPORTED (RBD_FEATURES_ALL)
Alex Elderd8891402012-10-09 13:50:17 -070084
Alex Elder81a89792012-02-02 08:13:30 -060085/*
86 * An RBD device name will be "rbd#", where the "rbd" comes from
87 * RBD_DRV_NAME above, and # is a unique integer identifier.
88 * MAX_INT_FORMAT_WIDTH is used in ensuring DEV_NAME_LEN is big
89 * enough to hold all possible device names.
90 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -070091#define DEV_NAME_LEN 32
Alex Elder81a89792012-02-02 08:13:30 -060092#define MAX_INT_FORMAT_WIDTH ((5 * sizeof (int)) / 2 + 1)
Yehuda Sadeh602adf42010-08-12 16:11:25 -070093
94/*
95 * block device image metadata (in-memory version)
96 */
97struct rbd_image_header {
Alex Elderf84344f2012-08-31 17:29:51 -050098 /* These four fields never change for a given rbd image */
Alex Elder849b4262012-07-09 21:04:24 -050099 char *object_prefix;
Alex Elder34b13182012-07-13 20:35:12 -0500100 u64 features;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700101 __u8 obj_order;
102 __u8 crypt_type;
103 __u8 comp_type;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700104
Alex Elderf84344f2012-08-31 17:29:51 -0500105 /* The remaining fields need to be updated occasionally */
106 u64 image_size;
107 struct ceph_snap_context *snapc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700108 char *snap_names;
109 u64 *snap_sizes;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700110
Alex Elder500d0c02013-04-26 09:43:47 -0500111 u64 stripe_unit;
112 u64 stripe_count;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700113};
114
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500115/*
116 * An rbd image specification.
117 *
118 * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely
Alex Elderc66c6e02012-11-01 08:39:26 -0500119 * identify an image. Each rbd_dev structure includes a pointer to
120 * an rbd_spec structure that encapsulates this identity.
121 *
122 * Each of the id's in an rbd_spec has an associated name. For a
123 * user-mapped image, the names are supplied and the id's associated
124 * with them are looked up. For a layered image, a parent image is
125 * defined by the tuple, and the names are looked up.
126 *
127 * An rbd_dev structure contains a parent_spec pointer which is
128 * non-null if the image it represents is a child in a layered
129 * image. This pointer will refer to the rbd_spec structure used
130 * by the parent rbd_dev for its own identity (i.e., the structure
131 * is shared between the parent and child).
132 *
133 * Since these structures are populated once, during the discovery
134 * phase of image construction, they are effectively immutable so
135 * we make no effort to synchronize access to them.
136 *
137 * Note that code herein does not assume the image name is known (it
138 * could be a null pointer).
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500139 */
140struct rbd_spec {
141 u64 pool_id;
Alex Elderecb4dc222013-04-26 09:43:47 -0500142 const char *pool_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500143
Alex Elderecb4dc222013-04-26 09:43:47 -0500144 const char *image_id;
145 const char *image_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500146
147 u64 snap_id;
Alex Elderecb4dc222013-04-26 09:43:47 -0500148 const char *snap_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500149
150 struct kref kref;
151};
152
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700153/*
Alex Elderf0f8cef2012-01-29 13:57:44 -0600154 * an instance of the client. multiple devices may share an rbd client.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700155 */
156struct rbd_client {
157 struct ceph_client *client;
158 struct kref kref;
159 struct list_head node;
160};
161
Alex Elderbf0d5f502012-11-22 00:00:08 -0600162struct rbd_img_request;
163typedef void (*rbd_img_callback_t)(struct rbd_img_request *);
164
165#define BAD_WHICH U32_MAX /* Good which or bad which, which? */
166
167struct rbd_obj_request;
168typedef void (*rbd_obj_callback_t)(struct rbd_obj_request *);
169
Alex Elder9969ebc2013-01-18 12:31:10 -0600170enum obj_request_type {
171 OBJ_REQUEST_NODATA, OBJ_REQUEST_BIO, OBJ_REQUEST_PAGES
172};
Alex Elderbf0d5f502012-11-22 00:00:08 -0600173
Alex Elder926f9b32013-02-11 12:33:24 -0600174enum obj_req_flags {
175 OBJ_REQ_DONE, /* completion flag: not done = 0, done = 1 */
Alex Elder6365d332013-02-11 12:33:24 -0600176 OBJ_REQ_IMG_DATA, /* object usage: standalone = 0, image = 1 */
Alex Elder5679c592013-02-11 12:33:24 -0600177 OBJ_REQ_KNOWN, /* EXISTS flag valid: no = 0, yes = 1 */
178 OBJ_REQ_EXISTS, /* target exists: no = 0, yes = 1 */
Alex Elder926f9b32013-02-11 12:33:24 -0600179};
180
Alex Elderbf0d5f502012-11-22 00:00:08 -0600181struct rbd_obj_request {
182 const char *object_name;
183 u64 offset; /* object start byte */
184 u64 length; /* bytes from offset */
Alex Elder926f9b32013-02-11 12:33:24 -0600185 unsigned long flags;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600186
Alex Elderc5b5ef62013-02-11 12:33:24 -0600187 /*
188 * An object request associated with an image will have its
189 * img_data flag set; a standalone object request will not.
190 *
191 * A standalone object request will have which == BAD_WHICH
192 * and a null obj_request pointer.
193 *
194 * An object request initiated in support of a layered image
195 * object (to check for its existence before a write) will
196 * have which == BAD_WHICH and a non-null obj_request pointer.
197 *
198 * Finally, an object request for rbd image data will have
199 * which != BAD_WHICH, and will have a non-null img_request
200 * pointer. The value of which will be in the range
201 * 0..(img_request->obj_request_count-1).
202 */
203 union {
204 struct rbd_obj_request *obj_request; /* STAT op */
205 struct {
206 struct rbd_img_request *img_request;
207 u64 img_offset;
208 /* links for img_request->obj_requests list */
209 struct list_head links;
210 };
211 };
Alex Elderbf0d5f502012-11-22 00:00:08 -0600212 u32 which; /* posn image request list */
213
214 enum obj_request_type type;
Alex Elder788e2df2013-01-17 12:25:27 -0600215 union {
216 struct bio *bio_list;
217 struct {
218 struct page **pages;
219 u32 page_count;
220 };
221 };
Alex Elder0eefd472013-04-19 15:34:50 -0500222 struct page **copyup_pages;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600223
224 struct ceph_osd_request *osd_req;
225
226 u64 xferred; /* bytes transferred */
227 u64 version;
Sage Weil1b83bef2013-02-25 16:11:12 -0800228 int result;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600229
230 rbd_obj_callback_t callback;
Alex Elder788e2df2013-01-17 12:25:27 -0600231 struct completion completion;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600232
233 struct kref kref;
234};
235
Alex Elder0c425242013-02-08 09:55:49 -0600236enum img_req_flags {
Alex Elder9849e982013-01-24 16:13:36 -0600237 IMG_REQ_WRITE, /* I/O direction: read = 0, write = 1 */
238 IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */
Alex Elderd0b2e942013-01-24 16:13:36 -0600239 IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */
Alex Elder0c425242013-02-08 09:55:49 -0600240};
241
Alex Elderbf0d5f502012-11-22 00:00:08 -0600242struct rbd_img_request {
Alex Elderbf0d5f502012-11-22 00:00:08 -0600243 struct rbd_device *rbd_dev;
244 u64 offset; /* starting image byte offset */
245 u64 length; /* byte count from offset */
Alex Elder0c425242013-02-08 09:55:49 -0600246 unsigned long flags;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600247 union {
Alex Elder9849e982013-01-24 16:13:36 -0600248 u64 snap_id; /* for reads */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600249 struct ceph_snap_context *snapc; /* for writes */
Alex Elder9849e982013-01-24 16:13:36 -0600250 };
251 union {
252 struct request *rq; /* block request */
253 struct rbd_obj_request *obj_request; /* obj req initiator */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600254 };
Alex Elder3d7efd12013-04-19 15:34:50 -0500255 struct page **copyup_pages;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600256 spinlock_t completion_lock;/* protects next_completion */
257 u32 next_completion;
258 rbd_img_callback_t callback;
Alex Elder55f27e02013-04-10 12:34:25 -0500259 u64 xferred;/* aggregate bytes transferred */
Alex Eldera5a337d2013-01-24 16:13:36 -0600260 int result; /* first nonzero obj_request result */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600261
262 u32 obj_request_count;
263 struct list_head obj_requests; /* rbd_obj_request structs */
264
265 struct kref kref;
266};
267
268#define for_each_obj_request(ireq, oreq) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600269 list_for_each_entry(oreq, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600270#define for_each_obj_request_from(ireq, oreq) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600271 list_for_each_entry_from(oreq, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600272#define for_each_obj_request_safe(ireq, oreq, n) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600273 list_for_each_entry_safe_reverse(oreq, n, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600274
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800275struct rbd_snap {
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800276 const char *name;
Josh Durgin3591538f2011-12-05 18:25:13 -0800277 u64 size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800278 struct list_head node;
279 u64 id;
Alex Elder34b13182012-07-13 20:35:12 -0500280 u64 features;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800281};
282
Alex Elderf84344f2012-08-31 17:29:51 -0500283struct rbd_mapping {
Alex Elder99c1f082012-08-30 14:42:15 -0500284 u64 size;
Alex Elder34b13182012-07-13 20:35:12 -0500285 u64 features;
Alex Elderf84344f2012-08-31 17:29:51 -0500286 bool read_only;
287};
288
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700289/*
290 * a single device
291 */
292struct rbd_device {
Alex Elderde71a292012-07-03 16:01:19 -0500293 int dev_id; /* blkdev unique id */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700294
295 int major; /* blkdev assigned major */
296 struct gendisk *disk; /* blkdev's gendisk and rq */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700297
Alex Eldera30b71b2012-07-10 20:30:11 -0500298 u32 image_format; /* Either 1 or 2 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700299 struct rbd_client *rbd_client;
300
301 char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */
302
Alex Elderb82d1672013-01-14 12:43:31 -0600303 spinlock_t lock; /* queue, flags, open_count */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700304
305 struct rbd_image_header header;
Alex Elderb82d1672013-01-14 12:43:31 -0600306 unsigned long flags; /* possibly lock protected */
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500307 struct rbd_spec *spec;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700308
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500309 char *header_name;
Alex Elder971f8392012-10-25 23:34:41 -0500310
Alex Elder0903e872012-11-14 12:25:19 -0600311 struct ceph_file_layout layout;
312
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700313 struct ceph_osd_event *watch_event;
Alex Elder975241a2013-01-25 17:08:55 -0600314 struct rbd_obj_request *watch_request;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700315
Alex Elder86b00e02012-10-25 23:34:42 -0500316 struct rbd_spec *parent_spec;
317 u64 parent_overlap;
Alex Elder2f82ee52012-10-30 19:40:33 -0500318 struct rbd_device *parent;
Alex Elder86b00e02012-10-25 23:34:42 -0500319
Josh Durginc6666012011-11-21 17:11:12 -0800320 /* protects updating the header */
321 struct rw_semaphore header_rwsem;
Alex Elderf84344f2012-08-31 17:29:51 -0500322
323 struct rbd_mapping mapping;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700324
325 struct list_head node;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800326
327 /* list of snapshots */
328 struct list_head snaps;
329
330 /* sysfs related */
331 struct device dev;
Alex Elderb82d1672013-01-14 12:43:31 -0600332 unsigned long open_count; /* protected by lock */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800333};
334
Alex Elderb82d1672013-01-14 12:43:31 -0600335/*
336 * Flag bits for rbd_dev->flags. If atomicity is required,
337 * rbd_dev->lock is used to protect access.
338 *
339 * Currently, only the "removing" flag (which is coupled with the
340 * "open_count" field) requires atomic access.
341 */
Alex Elder6d292902013-01-14 12:43:31 -0600342enum rbd_dev_flags {
343 RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */
Alex Elderb82d1672013-01-14 12:43:31 -0600344 RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */
Alex Elder6d292902013-01-14 12:43:31 -0600345};
346
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700347static DEFINE_MUTEX(ctl_mutex); /* Serialize open/close/setup/teardown */
Alex Eldere124a82f2012-01-29 13:57:44 -0600348
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700349static LIST_HEAD(rbd_dev_list); /* devices */
Alex Eldere124a82f2012-01-29 13:57:44 -0600350static DEFINE_SPINLOCK(rbd_dev_list_lock);
351
Alex Elder432b8582012-01-29 13:57:44 -0600352static LIST_HEAD(rbd_client_list); /* clients */
353static DEFINE_SPINLOCK(rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700354
Alex Elder3d7efd12013-04-19 15:34:50 -0500355static int rbd_img_request_submit(struct rbd_img_request *img_request);
356
Alex Elder304f6802012-08-31 17:29:52 -0500357static int rbd_dev_snaps_update(struct rbd_device *rbd_dev);
Alex Elder304f6802012-08-31 17:29:52 -0500358
Alex Elder200a6a82013-04-28 23:32:34 -0500359static void rbd_dev_device_release(struct device *dev);
Alex Elder6087b512013-04-25 15:09:41 -0500360static void rbd_snap_destroy(struct rbd_snap *snap);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800361
Alex Elderf0f8cef2012-01-29 13:57:44 -0600362static ssize_t rbd_add(struct bus_type *bus, const char *buf,
363 size_t count);
364static ssize_t rbd_remove(struct bus_type *bus, const char *buf,
365 size_t count);
Alex Elder71f293e2013-04-26 09:43:48 -0500366static int rbd_dev_image_probe(struct rbd_device *rbd_dev);
Alex Elderf0f8cef2012-01-29 13:57:44 -0600367
368static struct bus_attribute rbd_bus_attrs[] = {
369 __ATTR(add, S_IWUSR, NULL, rbd_add),
370 __ATTR(remove, S_IWUSR, NULL, rbd_remove),
371 __ATTR_NULL
372};
373
374static struct bus_type rbd_bus_type = {
375 .name = "rbd",
376 .bus_attrs = rbd_bus_attrs,
377};
378
379static void rbd_root_dev_release(struct device *dev)
380{
381}
382
383static struct device rbd_root_dev = {
384 .init_name = "rbd",
385 .release = rbd_root_dev_release,
386};
387
Alex Elder06ecc6c2012-11-01 10:17:15 -0500388static __printf(2, 3)
389void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...)
390{
391 struct va_format vaf;
392 va_list args;
393
394 va_start(args, fmt);
395 vaf.fmt = fmt;
396 vaf.va = &args;
397
398 if (!rbd_dev)
399 printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf);
400 else if (rbd_dev->disk)
401 printk(KERN_WARNING "%s: %s: %pV\n",
402 RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf);
403 else if (rbd_dev->spec && rbd_dev->spec->image_name)
404 printk(KERN_WARNING "%s: image %s: %pV\n",
405 RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf);
406 else if (rbd_dev->spec && rbd_dev->spec->image_id)
407 printk(KERN_WARNING "%s: id %s: %pV\n",
408 RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf);
409 else /* punt */
410 printk(KERN_WARNING "%s: rbd_dev %p: %pV\n",
411 RBD_DRV_NAME, rbd_dev, &vaf);
412 va_end(args);
413}
414
Alex Elderaafb2302012-09-06 16:00:54 -0500415#ifdef RBD_DEBUG
416#define rbd_assert(expr) \
417 if (unlikely(!(expr))) { \
418 printk(KERN_ERR "\nAssertion failure in %s() " \
419 "at line %d:\n\n" \
420 "\trbd_assert(%s);\n\n", \
421 __func__, __LINE__, #expr); \
422 BUG(); \
423 }
424#else /* !RBD_DEBUG */
425# define rbd_assert(expr) ((void) 0)
426#endif /* !RBD_DEBUG */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800427
Alex Elderb454e362013-04-19 15:34:50 -0500428static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request);
Alex Elder05a46af2013-04-26 15:44:36 -0500429static void rbd_img_parent_read(struct rbd_obj_request *obj_request);
430static void rbd_dev_remove_parent(struct rbd_device *rbd_dev);
Alex Elder8b3e1a52013-01-24 16:13:36 -0600431
Alex Elder117973f2012-08-31 17:29:55 -0500432static int rbd_dev_refresh(struct rbd_device *rbd_dev, u64 *hver);
433static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev, u64 *hver);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700434
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700435static int rbd_open(struct block_device *bdev, fmode_t mode)
436{
Alex Elderf0f8cef2012-01-29 13:57:44 -0600437 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
Alex Elderb82d1672013-01-14 12:43:31 -0600438 bool removing = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700439
Alex Elderf84344f2012-08-31 17:29:51 -0500440 if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700441 return -EROFS;
442
Alex Eldera14ea262013-02-05 13:23:12 -0600443 spin_lock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600444 if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags))
445 removing = true;
446 else
447 rbd_dev->open_count++;
Alex Eldera14ea262013-02-05 13:23:12 -0600448 spin_unlock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600449 if (removing)
450 return -ENOENT;
451
Alex Elder42382b72012-11-16 09:29:16 -0600452 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Alex Elderc3e946c2012-11-16 09:29:16 -0600453 (void) get_device(&rbd_dev->dev);
Alex Elderf84344f2012-08-31 17:29:51 -0500454 set_device_ro(bdev, rbd_dev->mapping.read_only);
Alex Elder42382b72012-11-16 09:29:16 -0600455 mutex_unlock(&ctl_mutex);
Alex Elder340c7a22012-08-10 13:12:07 -0700456
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700457 return 0;
458}
459
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800460static int rbd_release(struct gendisk *disk, fmode_t mode)
461{
462 struct rbd_device *rbd_dev = disk->private_data;
Alex Elderb82d1672013-01-14 12:43:31 -0600463 unsigned long open_count_before;
464
Alex Eldera14ea262013-02-05 13:23:12 -0600465 spin_lock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600466 open_count_before = rbd_dev->open_count--;
Alex Eldera14ea262013-02-05 13:23:12 -0600467 spin_unlock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600468 rbd_assert(open_count_before > 0);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800469
Alex Elder42382b72012-11-16 09:29:16 -0600470 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Alex Elderc3e946c2012-11-16 09:29:16 -0600471 put_device(&rbd_dev->dev);
Alex Elder42382b72012-11-16 09:29:16 -0600472 mutex_unlock(&ctl_mutex);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800473
474 return 0;
475}
476
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700477static const struct block_device_operations rbd_bd_ops = {
478 .owner = THIS_MODULE,
479 .open = rbd_open,
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800480 .release = rbd_release,
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700481};
482
483/*
484 * Initialize an rbd client instance.
Alex Elder43ae4702012-07-03 16:01:18 -0500485 * We own *ceph_opts.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700486 */
Alex Elderf8c38922012-08-10 13:12:07 -0700487static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700488{
489 struct rbd_client *rbdc;
490 int ret = -ENOMEM;
491
Alex Elder37206ee2013-02-20 17:32:08 -0600492 dout("%s:\n", __func__);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700493 rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL);
494 if (!rbdc)
495 goto out_opt;
496
497 kref_init(&rbdc->kref);
498 INIT_LIST_HEAD(&rbdc->node);
499
Alex Elderbc534d82012-01-29 13:57:44 -0600500 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
501
Alex Elder43ae4702012-07-03 16:01:18 -0500502 rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700503 if (IS_ERR(rbdc->client))
Alex Elderbc534d82012-01-29 13:57:44 -0600504 goto out_mutex;
Alex Elder43ae4702012-07-03 16:01:18 -0500505 ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700506
507 ret = ceph_open_session(rbdc->client);
508 if (ret < 0)
509 goto out_err;
510
Alex Elder432b8582012-01-29 13:57:44 -0600511 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700512 list_add_tail(&rbdc->node, &rbd_client_list);
Alex Elder432b8582012-01-29 13:57:44 -0600513 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700514
Alex Elderbc534d82012-01-29 13:57:44 -0600515 mutex_unlock(&ctl_mutex);
Alex Elder37206ee2013-02-20 17:32:08 -0600516 dout("%s: rbdc %p\n", __func__, rbdc);
Alex Elderbc534d82012-01-29 13:57:44 -0600517
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700518 return rbdc;
519
520out_err:
521 ceph_destroy_client(rbdc->client);
Alex Elderbc534d82012-01-29 13:57:44 -0600522out_mutex:
523 mutex_unlock(&ctl_mutex);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700524 kfree(rbdc);
525out_opt:
Alex Elder43ae4702012-07-03 16:01:18 -0500526 if (ceph_opts)
527 ceph_destroy_options(ceph_opts);
Alex Elder37206ee2013-02-20 17:32:08 -0600528 dout("%s: error %d\n", __func__, ret);
529
Vasiliy Kulikov28f259b2010-09-26 12:59:37 +0400530 return ERR_PTR(ret);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700531}
532
Alex Elder2f82ee52012-10-30 19:40:33 -0500533static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc)
534{
535 kref_get(&rbdc->kref);
536
537 return rbdc;
538}
539
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700540/*
Alex Elder1f7ba332012-08-10 13:12:07 -0700541 * Find a ceph client with specific addr and configuration. If
542 * found, bump its reference count.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700543 */
Alex Elder1f7ba332012-08-10 13:12:07 -0700544static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700545{
546 struct rbd_client *client_node;
Alex Elder1f7ba332012-08-10 13:12:07 -0700547 bool found = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700548
Alex Elder43ae4702012-07-03 16:01:18 -0500549 if (ceph_opts->flags & CEPH_OPT_NOSHARE)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700550 return NULL;
551
Alex Elder1f7ba332012-08-10 13:12:07 -0700552 spin_lock(&rbd_client_list_lock);
553 list_for_each_entry(client_node, &rbd_client_list, node) {
554 if (!ceph_compare_options(ceph_opts, client_node->client)) {
Alex Elder2f82ee52012-10-30 19:40:33 -0500555 __rbd_get_client(client_node);
556
Alex Elder1f7ba332012-08-10 13:12:07 -0700557 found = true;
558 break;
559 }
560 }
561 spin_unlock(&rbd_client_list_lock);
562
563 return found ? client_node : NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700564}
565
566/*
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700567 * mount options
568 */
569enum {
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700570 Opt_last_int,
571 /* int args above */
572 Opt_last_string,
573 /* string args above */
Alex Eldercc0538b2012-08-10 13:12:07 -0700574 Opt_read_only,
575 Opt_read_write,
576 /* Boolean args above */
577 Opt_last_bool,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700578};
579
Alex Elder43ae4702012-07-03 16:01:18 -0500580static match_table_t rbd_opts_tokens = {
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700581 /* int args above */
582 /* string args above */
Alex Elderbe466c12012-10-22 11:31:26 -0500583 {Opt_read_only, "read_only"},
Alex Eldercc0538b2012-08-10 13:12:07 -0700584 {Opt_read_only, "ro"}, /* Alternate spelling */
585 {Opt_read_write, "read_write"},
586 {Opt_read_write, "rw"}, /* Alternate spelling */
587 /* Boolean args above */
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700588 {-1, NULL}
589};
590
Alex Elder98571b52013-01-20 14:44:42 -0600591struct rbd_options {
592 bool read_only;
593};
594
595#define RBD_READ_ONLY_DEFAULT false
596
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700597static int parse_rbd_opts_token(char *c, void *private)
598{
Alex Elder43ae4702012-07-03 16:01:18 -0500599 struct rbd_options *rbd_opts = private;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700600 substring_t argstr[MAX_OPT_ARGS];
601 int token, intval, ret;
602
Alex Elder43ae4702012-07-03 16:01:18 -0500603 token = match_token(c, rbd_opts_tokens, argstr);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700604 if (token < 0)
605 return -EINVAL;
606
607 if (token < Opt_last_int) {
608 ret = match_int(&argstr[0], &intval);
609 if (ret < 0) {
610 pr_err("bad mount option arg (not int) "
611 "at '%s'\n", c);
612 return ret;
613 }
614 dout("got int token %d val %d\n", token, intval);
615 } else if (token > Opt_last_int && token < Opt_last_string) {
616 dout("got string token %d val %s\n", token,
617 argstr[0].from);
Alex Eldercc0538b2012-08-10 13:12:07 -0700618 } else if (token > Opt_last_string && token < Opt_last_bool) {
619 dout("got Boolean token %d\n", token);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700620 } else {
621 dout("got token %d\n", token);
622 }
623
624 switch (token) {
Alex Eldercc0538b2012-08-10 13:12:07 -0700625 case Opt_read_only:
626 rbd_opts->read_only = true;
627 break;
628 case Opt_read_write:
629 rbd_opts->read_only = false;
630 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700631 default:
Alex Elderaafb2302012-09-06 16:00:54 -0500632 rbd_assert(false);
633 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700634 }
635 return 0;
636}
637
638/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700639 * Get a ceph client with specific addr and configuration, if one does
640 * not exist create it.
641 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500642static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700643{
Alex Elderf8c38922012-08-10 13:12:07 -0700644 struct rbd_client *rbdc;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700645
Alex Elder1f7ba332012-08-10 13:12:07 -0700646 rbdc = rbd_client_find(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500647 if (rbdc) /* using an existing client */
Alex Elder43ae4702012-07-03 16:01:18 -0500648 ceph_destroy_options(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500649 else
Alex Elderf8c38922012-08-10 13:12:07 -0700650 rbdc = rbd_client_create(ceph_opts);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700651
Alex Elder9d3997f2012-10-25 23:34:42 -0500652 return rbdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700653}
654
655/*
656 * Destroy ceph client
Alex Elderd23a4b32012-01-29 13:57:43 -0600657 *
Alex Elder432b8582012-01-29 13:57:44 -0600658 * Caller must hold rbd_client_list_lock.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700659 */
660static void rbd_client_release(struct kref *kref)
661{
662 struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
663
Alex Elder37206ee2013-02-20 17:32:08 -0600664 dout("%s: rbdc %p\n", __func__, rbdc);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500665 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700666 list_del(&rbdc->node);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500667 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700668
669 ceph_destroy_client(rbdc->client);
670 kfree(rbdc);
671}
672
673/*
674 * Drop reference to ceph client node. If it's not referenced anymore, release
675 * it.
676 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500677static void rbd_put_client(struct rbd_client *rbdc)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700678{
Alex Elderc53d5892012-10-25 23:34:42 -0500679 if (rbdc)
680 kref_put(&rbdc->kref, rbd_client_release);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700681}
682
Alex Eldera30b71b2012-07-10 20:30:11 -0500683static bool rbd_image_format_valid(u32 image_format)
684{
685 return image_format == 1 || image_format == 2;
686}
687
Alex Elder8e94af82012-07-25 09:32:40 -0500688static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
689{
Alex Elder103a1502012-08-02 11:29:45 -0500690 size_t size;
691 u32 snap_count;
692
693 /* The header has to start with the magic rbd header text */
694 if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT)))
695 return false;
696
Alex Elderdb2388b2012-10-20 22:17:27 -0500697 /* The bio layer requires at least sector-sized I/O */
698
699 if (ondisk->options.order < SECTOR_SHIFT)
700 return false;
701
702 /* If we use u64 in a few spots we may be able to loosen this */
703
704 if (ondisk->options.order > 8 * sizeof (int) - 1)
705 return false;
706
Alex Elder103a1502012-08-02 11:29:45 -0500707 /*
708 * The size of a snapshot header has to fit in a size_t, and
709 * that limits the number of snapshots.
710 */
711 snap_count = le32_to_cpu(ondisk->snap_count);
712 size = SIZE_MAX - sizeof (struct ceph_snap_context);
713 if (snap_count > size / sizeof (__le64))
714 return false;
715
716 /*
717 * Not only that, but the size of the entire the snapshot
718 * header must also be representable in a size_t.
719 */
720 size -= snap_count * sizeof (__le64);
721 if ((u64) size < le64_to_cpu(ondisk->snap_names_len))
722 return false;
723
724 return true;
Alex Elder8e94af82012-07-25 09:32:40 -0500725}
726
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700727/*
728 * Create a new header structure, translate header format from the on-disk
729 * header.
730 */
731static int rbd_header_from_disk(struct rbd_image_header *header,
Alex Elder4156d992012-08-02 11:29:46 -0500732 struct rbd_image_header_ondisk *ondisk)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700733{
Alex Elderccece232012-07-10 20:30:10 -0500734 u32 snap_count;
Alex Elder58c17b02012-08-23 23:22:06 -0500735 size_t len;
Alex Elderd2bb24e2012-07-26 23:37:14 -0500736 size_t size;
Alex Elder621901d2012-08-23 23:22:06 -0500737 u32 i;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700738
Alex Elder6a523252012-07-19 17:12:59 -0500739 memset(header, 0, sizeof (*header));
740
Alex Elder103a1502012-08-02 11:29:45 -0500741 snap_count = le32_to_cpu(ondisk->snap_count);
742
Alex Elder58c17b02012-08-23 23:22:06 -0500743 len = strnlen(ondisk->object_prefix, sizeof (ondisk->object_prefix));
744 header->object_prefix = kmalloc(len + 1, GFP_KERNEL);
Alex Elder6a523252012-07-19 17:12:59 -0500745 if (!header->object_prefix)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700746 return -ENOMEM;
Alex Elder58c17b02012-08-23 23:22:06 -0500747 memcpy(header->object_prefix, ondisk->object_prefix, len);
748 header->object_prefix[len] = '\0';
Alex Elder00f1f362012-02-07 12:03:36 -0600749
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700750 if (snap_count) {
Alex Elderf785cc12012-08-23 23:22:06 -0500751 u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len);
752
Alex Elder621901d2012-08-23 23:22:06 -0500753 /* Save a copy of the snapshot names */
754
Alex Elderf785cc12012-08-23 23:22:06 -0500755 if (snap_names_len > (u64) SIZE_MAX)
756 return -EIO;
757 header->snap_names = kmalloc(snap_names_len, GFP_KERNEL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700758 if (!header->snap_names)
Alex Elder6a523252012-07-19 17:12:59 -0500759 goto out_err;
Alex Elderf785cc12012-08-23 23:22:06 -0500760 /*
761 * Note that rbd_dev_v1_header_read() guarantees
762 * the ondisk buffer we're working with has
763 * snap_names_len bytes beyond the end of the
764 * snapshot id array, this memcpy() is safe.
765 */
766 memcpy(header->snap_names, &ondisk->snaps[snap_count],
767 snap_names_len);
Alex Elder6a523252012-07-19 17:12:59 -0500768
Alex Elder621901d2012-08-23 23:22:06 -0500769 /* Record each snapshot's size */
770
Alex Elderd2bb24e2012-07-26 23:37:14 -0500771 size = snap_count * sizeof (*header->snap_sizes);
772 header->snap_sizes = kmalloc(size, GFP_KERNEL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700773 if (!header->snap_sizes)
Alex Elder6a523252012-07-19 17:12:59 -0500774 goto out_err;
Alex Elder621901d2012-08-23 23:22:06 -0500775 for (i = 0; i < snap_count; i++)
776 header->snap_sizes[i] =
777 le64_to_cpu(ondisk->snaps[i].image_size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700778 } else {
779 header->snap_names = NULL;
780 header->snap_sizes = NULL;
781 }
Alex Elder849b4262012-07-09 21:04:24 -0500782
Alex Elder34b13182012-07-13 20:35:12 -0500783 header->features = 0; /* No features support in v1 images */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700784 header->obj_order = ondisk->options.order;
785 header->crypt_type = ondisk->options.crypt_type;
786 header->comp_type = ondisk->options.comp_type;
Alex Elder6a523252012-07-19 17:12:59 -0500787
Alex Elder621901d2012-08-23 23:22:06 -0500788 /* Allocate and fill in the snapshot context */
789
Alex Elderf84344f2012-08-31 17:29:51 -0500790 header->image_size = le64_to_cpu(ondisk->image_size);
Alex Elder468521c2013-04-26 09:43:47 -0500791
Alex Elder812164f82013-04-30 00:44:32 -0500792 header->snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
Alex Elder6a523252012-07-19 17:12:59 -0500793 if (!header->snapc)
794 goto out_err;
Alex Elder505cbb92012-07-19 08:49:18 -0500795 header->snapc->seq = le64_to_cpu(ondisk->snap_seq);
Alex Elder621901d2012-08-23 23:22:06 -0500796 for (i = 0; i < snap_count; i++)
Alex Elder468521c2013-04-26 09:43:47 -0500797 header->snapc->snaps[i] = le64_to_cpu(ondisk->snaps[i].id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700798
799 return 0;
800
Alex Elder6a523252012-07-19 17:12:59 -0500801out_err:
Alex Elder849b4262012-07-09 21:04:24 -0500802 kfree(header->snap_sizes);
Alex Elderccece232012-07-10 20:30:10 -0500803 header->snap_sizes = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700804 kfree(header->snap_names);
Alex Elderccece232012-07-10 20:30:10 -0500805 header->snap_names = NULL;
Alex Elder6a523252012-07-19 17:12:59 -0500806 kfree(header->object_prefix);
807 header->object_prefix = NULL;
Alex Elderccece232012-07-10 20:30:10 -0500808
Alex Elder00f1f362012-02-07 12:03:36 -0600809 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700810}
811
Alex Elder9e15b772012-10-30 19:40:33 -0500812static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id)
813{
814 struct rbd_snap *snap;
815
816 if (snap_id == CEPH_NOSNAP)
817 return RBD_SNAP_HEAD_NAME;
818
819 list_for_each_entry(snap, &rbd_dev->snaps, node)
820 if (snap_id == snap->id)
821 return snap->name;
822
823 return NULL;
824}
825
Alex Elder8b0241f2013-04-25 23:15:08 -0500826static struct rbd_snap *snap_by_name(struct rbd_device *rbd_dev,
827 const char *snap_name)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700828{
Alex Eldere86924a2012-07-10 20:30:11 -0500829 struct rbd_snap *snap;
Alex Elder00f1f362012-02-07 12:03:36 -0600830
Alex Elder8b0241f2013-04-25 23:15:08 -0500831 list_for_each_entry(snap, &rbd_dev->snaps, node)
832 if (!strcmp(snap_name, snap->name))
833 return snap;
Alex Elder00f1f362012-02-07 12:03:36 -0600834
Alex Elder8b0241f2013-04-25 23:15:08 -0500835 return NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700836}
837
Alex Elderd1cf5782013-04-27 09:59:30 -0500838static int rbd_dev_mapping_set(struct rbd_device *rbd_dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700839{
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500840 if (!memcmp(rbd_dev->spec->snap_name, RBD_SNAP_HEAD_NAME,
Josh Durgincc9d7342011-11-21 18:19:13 -0800841 sizeof (RBD_SNAP_HEAD_NAME))) {
Alex Elder99c1f082012-08-30 14:42:15 -0500842 rbd_dev->mapping.size = rbd_dev->header.image_size;
Alex Elder34b13182012-07-13 20:35:12 -0500843 rbd_dev->mapping.features = rbd_dev->header.features;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700844 } else {
Alex Elder8b0241f2013-04-25 23:15:08 -0500845 struct rbd_snap *snap;
846
847 snap = snap_by_name(rbd_dev, rbd_dev->spec->snap_name);
848 if (!snap)
849 return -ENOENT;
Alex Elder8b0241f2013-04-25 23:15:08 -0500850 rbd_dev->mapping.size = snap->size;
851 rbd_dev->mapping.features = snap->features;
Alex Elderf84344f2012-08-31 17:29:51 -0500852 rbd_dev->mapping.read_only = true;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700853 }
Alex Elder6d292902013-01-14 12:43:31 -0600854
Alex Elder8b0241f2013-04-25 23:15:08 -0500855 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700856}
857
Alex Elderd1cf5782013-04-27 09:59:30 -0500858static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev)
859{
860 rbd_dev->mapping.size = 0;
861 rbd_dev->mapping.features = 0;
862 rbd_dev->mapping.read_only = true;
863}
864
Alex Elder200a6a82013-04-28 23:32:34 -0500865static void rbd_dev_clear_mapping(struct rbd_device *rbd_dev)
866{
867 rbd_dev->mapping.size = 0;
868 rbd_dev->mapping.features = 0;
869 rbd_dev->mapping.read_only = true;
870}
871
Alex Elder98571b52013-01-20 14:44:42 -0600872static const char *rbd_segment_name(struct rbd_device *rbd_dev, u64 offset)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700873{
Alex Elder65ccfe22012-08-09 10:33:26 -0700874 char *name;
875 u64 segment;
876 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700877
Alex Elder2fd82b92012-11-09 15:05:54 -0600878 name = kmalloc(MAX_OBJ_NAME_SIZE + 1, GFP_NOIO);
Alex Elder65ccfe22012-08-09 10:33:26 -0700879 if (!name)
880 return NULL;
881 segment = offset >> rbd_dev->header.obj_order;
Alex Elder2fd82b92012-11-09 15:05:54 -0600882 ret = snprintf(name, MAX_OBJ_NAME_SIZE + 1, "%s.%012llx",
Alex Elder65ccfe22012-08-09 10:33:26 -0700883 rbd_dev->header.object_prefix, segment);
Alex Elder2fd82b92012-11-09 15:05:54 -0600884 if (ret < 0 || ret > MAX_OBJ_NAME_SIZE) {
Alex Elder65ccfe22012-08-09 10:33:26 -0700885 pr_err("error formatting segment name for #%llu (%d)\n",
886 segment, ret);
887 kfree(name);
888 name = NULL;
889 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700890
Alex Elder65ccfe22012-08-09 10:33:26 -0700891 return name;
892}
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700893
Alex Elder65ccfe22012-08-09 10:33:26 -0700894static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset)
895{
896 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700897
Alex Elder65ccfe22012-08-09 10:33:26 -0700898 return offset & (segment_size - 1);
899}
900
901static u64 rbd_segment_length(struct rbd_device *rbd_dev,
902 u64 offset, u64 length)
903{
904 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
905
906 offset &= segment_size - 1;
907
Alex Elderaafb2302012-09-06 16:00:54 -0500908 rbd_assert(length <= U64_MAX - offset);
Alex Elder65ccfe22012-08-09 10:33:26 -0700909 if (offset + length > segment_size)
910 length = segment_size - offset;
911
912 return length;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700913}
914
915/*
Josh Durgin029bcbd2011-07-22 11:35:23 -0700916 * returns the size of an object in the image
917 */
918static u64 rbd_obj_bytes(struct rbd_image_header *header)
919{
920 return 1 << header->obj_order;
921}
922
923/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700924 * bio helpers
925 */
926
927static void bio_chain_put(struct bio *chain)
928{
929 struct bio *tmp;
930
931 while (chain) {
932 tmp = chain;
933 chain = chain->bi_next;
934 bio_put(tmp);
935 }
936}
937
938/*
939 * zeros a bio chain, starting at specific offset
940 */
941static void zero_bio_chain(struct bio *chain, int start_ofs)
942{
943 struct bio_vec *bv;
944 unsigned long flags;
945 void *buf;
946 int i;
947 int pos = 0;
948
949 while (chain) {
950 bio_for_each_segment(bv, chain, i) {
951 if (pos + bv->bv_len > start_ofs) {
952 int remainder = max(start_ofs - pos, 0);
953 buf = bvec_kmap_irq(bv, &flags);
954 memset(buf + remainder, 0,
955 bv->bv_len - remainder);
Dan Carpenter85b5aaa2010-10-11 21:15:11 +0200956 bvec_kunmap_irq(buf, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700957 }
958 pos += bv->bv_len;
959 }
960
961 chain = chain->bi_next;
962 }
963}
964
965/*
Alex Elderb9434c52013-04-19 15:34:50 -0500966 * similar to zero_bio_chain(), zeros data defined by a page array,
967 * starting at the given byte offset from the start of the array and
968 * continuing up to the given end offset. The pages array is
969 * assumed to be big enough to hold all bytes up to the end.
970 */
971static void zero_pages(struct page **pages, u64 offset, u64 end)
972{
973 struct page **page = &pages[offset >> PAGE_SHIFT];
974
975 rbd_assert(end > offset);
976 rbd_assert(end - offset <= (u64)SIZE_MAX);
977 while (offset < end) {
978 size_t page_offset;
979 size_t length;
980 unsigned long flags;
981 void *kaddr;
982
983 page_offset = (size_t)(offset & ~PAGE_MASK);
984 length = min(PAGE_SIZE - page_offset, (size_t)(end - offset));
985 local_irq_save(flags);
986 kaddr = kmap_atomic(*page);
987 memset(kaddr + page_offset, 0, length);
988 kunmap_atomic(kaddr);
989 local_irq_restore(flags);
990
991 offset += length;
992 page++;
993 }
994}
995
996/*
Alex Elderf7760da2012-10-20 22:17:27 -0500997 * Clone a portion of a bio, starting at the given byte offset
998 * and continuing for the number of bytes indicated.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700999 */
Alex Elderf7760da2012-10-20 22:17:27 -05001000static struct bio *bio_clone_range(struct bio *bio_src,
1001 unsigned int offset,
1002 unsigned int len,
1003 gfp_t gfpmask)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001004{
Alex Elderf7760da2012-10-20 22:17:27 -05001005 struct bio_vec *bv;
1006 unsigned int resid;
1007 unsigned short idx;
1008 unsigned int voff;
1009 unsigned short end_idx;
1010 unsigned short vcnt;
1011 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001012
Alex Elderf7760da2012-10-20 22:17:27 -05001013 /* Handle the easy case for the caller */
1014
1015 if (!offset && len == bio_src->bi_size)
1016 return bio_clone(bio_src, gfpmask);
1017
1018 if (WARN_ON_ONCE(!len))
1019 return NULL;
1020 if (WARN_ON_ONCE(len > bio_src->bi_size))
1021 return NULL;
1022 if (WARN_ON_ONCE(offset > bio_src->bi_size - len))
1023 return NULL;
1024
1025 /* Find first affected segment... */
1026
1027 resid = offset;
1028 __bio_for_each_segment(bv, bio_src, idx, 0) {
1029 if (resid < bv->bv_len)
1030 break;
1031 resid -= bv->bv_len;
1032 }
1033 voff = resid;
1034
1035 /* ...and the last affected segment */
1036
1037 resid += len;
1038 __bio_for_each_segment(bv, bio_src, end_idx, idx) {
1039 if (resid <= bv->bv_len)
1040 break;
1041 resid -= bv->bv_len;
1042 }
1043 vcnt = end_idx - idx + 1;
1044
1045 /* Build the clone */
1046
1047 bio = bio_alloc(gfpmask, (unsigned int) vcnt);
1048 if (!bio)
1049 return NULL; /* ENOMEM */
1050
1051 bio->bi_bdev = bio_src->bi_bdev;
1052 bio->bi_sector = bio_src->bi_sector + (offset >> SECTOR_SHIFT);
1053 bio->bi_rw = bio_src->bi_rw;
1054 bio->bi_flags |= 1 << BIO_CLONED;
1055
1056 /*
1057 * Copy over our part of the bio_vec, then update the first
1058 * and last (or only) entries.
1059 */
1060 memcpy(&bio->bi_io_vec[0], &bio_src->bi_io_vec[idx],
1061 vcnt * sizeof (struct bio_vec));
1062 bio->bi_io_vec[0].bv_offset += voff;
1063 if (vcnt > 1) {
1064 bio->bi_io_vec[0].bv_len -= voff;
1065 bio->bi_io_vec[vcnt - 1].bv_len = resid;
1066 } else {
1067 bio->bi_io_vec[0].bv_len = len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001068 }
1069
Alex Elderf7760da2012-10-20 22:17:27 -05001070 bio->bi_vcnt = vcnt;
1071 bio->bi_size = len;
1072 bio->bi_idx = 0;
Alex Elder542582f2012-08-09 10:33:25 -07001073
Alex Elderf7760da2012-10-20 22:17:27 -05001074 return bio;
1075}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001076
Alex Elderf7760da2012-10-20 22:17:27 -05001077/*
1078 * Clone a portion of a bio chain, starting at the given byte offset
1079 * into the first bio in the source chain and continuing for the
1080 * number of bytes indicated. The result is another bio chain of
1081 * exactly the given length, or a null pointer on error.
1082 *
1083 * The bio_src and offset parameters are both in-out. On entry they
1084 * refer to the first source bio and the offset into that bio where
1085 * the start of data to be cloned is located.
1086 *
1087 * On return, bio_src is updated to refer to the bio in the source
1088 * chain that contains first un-cloned byte, and *offset will
1089 * contain the offset of that byte within that bio.
1090 */
1091static struct bio *bio_chain_clone_range(struct bio **bio_src,
1092 unsigned int *offset,
1093 unsigned int len,
1094 gfp_t gfpmask)
1095{
1096 struct bio *bi = *bio_src;
1097 unsigned int off = *offset;
1098 struct bio *chain = NULL;
1099 struct bio **end;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001100
Alex Elderf7760da2012-10-20 22:17:27 -05001101 /* Build up a chain of clone bios up to the limit */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001102
Alex Elderf7760da2012-10-20 22:17:27 -05001103 if (!bi || off >= bi->bi_size || !len)
1104 return NULL; /* Nothing to clone */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001105
Alex Elderf7760da2012-10-20 22:17:27 -05001106 end = &chain;
1107 while (len) {
1108 unsigned int bi_size;
1109 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001110
Alex Elderf5400b72012-11-01 10:17:15 -05001111 if (!bi) {
1112 rbd_warn(NULL, "bio_chain exhausted with %u left", len);
Alex Elderf7760da2012-10-20 22:17:27 -05001113 goto out_err; /* EINVAL; ran out of bio's */
Alex Elderf5400b72012-11-01 10:17:15 -05001114 }
Alex Elderf7760da2012-10-20 22:17:27 -05001115 bi_size = min_t(unsigned int, bi->bi_size - off, len);
1116 bio = bio_clone_range(bi, off, bi_size, gfpmask);
1117 if (!bio)
1118 goto out_err; /* ENOMEM */
1119
1120 *end = bio;
1121 end = &bio->bi_next;
1122
1123 off += bi_size;
1124 if (off == bi->bi_size) {
1125 bi = bi->bi_next;
1126 off = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001127 }
Alex Elderf7760da2012-10-20 22:17:27 -05001128 len -= bi_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001129 }
Alex Elderf7760da2012-10-20 22:17:27 -05001130 *bio_src = bi;
1131 *offset = off;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001132
Alex Elderf7760da2012-10-20 22:17:27 -05001133 return chain;
1134out_err:
1135 bio_chain_put(chain);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001136
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001137 return NULL;
1138}
1139
Alex Elder926f9b32013-02-11 12:33:24 -06001140/*
1141 * The default/initial value for all object request flags is 0. For
1142 * each flag, once its value is set to 1 it is never reset to 0
1143 * again.
1144 */
Alex Elder6365d332013-02-11 12:33:24 -06001145static void obj_request_img_data_set(struct rbd_obj_request *obj_request)
1146{
1147 if (test_and_set_bit(OBJ_REQ_IMG_DATA, &obj_request->flags)) {
Alex Elder6365d332013-02-11 12:33:24 -06001148 struct rbd_device *rbd_dev;
1149
Alex Elder57acbaa2013-02-11 12:33:24 -06001150 rbd_dev = obj_request->img_request->rbd_dev;
Alex Elder6365d332013-02-11 12:33:24 -06001151 rbd_warn(rbd_dev, "obj_request %p already marked img_data\n",
1152 obj_request);
1153 }
1154}
1155
1156static bool obj_request_img_data_test(struct rbd_obj_request *obj_request)
1157{
1158 smp_mb();
1159 return test_bit(OBJ_REQ_IMG_DATA, &obj_request->flags) != 0;
1160}
1161
Alex Elder57acbaa2013-02-11 12:33:24 -06001162static void obj_request_done_set(struct rbd_obj_request *obj_request)
1163{
1164 if (test_and_set_bit(OBJ_REQ_DONE, &obj_request->flags)) {
1165 struct rbd_device *rbd_dev = NULL;
1166
1167 if (obj_request_img_data_test(obj_request))
1168 rbd_dev = obj_request->img_request->rbd_dev;
1169 rbd_warn(rbd_dev, "obj_request %p already marked done\n",
1170 obj_request);
1171 }
1172}
1173
1174static bool obj_request_done_test(struct rbd_obj_request *obj_request)
1175{
1176 smp_mb();
1177 return test_bit(OBJ_REQ_DONE, &obj_request->flags) != 0;
1178}
1179
Alex Elder5679c592013-02-11 12:33:24 -06001180/*
1181 * This sets the KNOWN flag after (possibly) setting the EXISTS
1182 * flag. The latter is set based on the "exists" value provided.
1183 *
1184 * Note that for our purposes once an object exists it never goes
1185 * away again. It's possible that the response from two existence
1186 * checks are separated by the creation of the target object, and
1187 * the first ("doesn't exist") response arrives *after* the second
1188 * ("does exist"). In that case we ignore the second one.
1189 */
1190static void obj_request_existence_set(struct rbd_obj_request *obj_request,
1191 bool exists)
1192{
1193 if (exists)
1194 set_bit(OBJ_REQ_EXISTS, &obj_request->flags);
1195 set_bit(OBJ_REQ_KNOWN, &obj_request->flags);
1196 smp_mb();
1197}
1198
1199static bool obj_request_known_test(struct rbd_obj_request *obj_request)
1200{
1201 smp_mb();
1202 return test_bit(OBJ_REQ_KNOWN, &obj_request->flags) != 0;
1203}
1204
1205static bool obj_request_exists_test(struct rbd_obj_request *obj_request)
1206{
1207 smp_mb();
1208 return test_bit(OBJ_REQ_EXISTS, &obj_request->flags) != 0;
1209}
1210
Alex Elderbf0d5f502012-11-22 00:00:08 -06001211static void rbd_obj_request_get(struct rbd_obj_request *obj_request)
1212{
Alex Elder37206ee2013-02-20 17:32:08 -06001213 dout("%s: obj %p (was %d)\n", __func__, obj_request,
1214 atomic_read(&obj_request->kref.refcount));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001215 kref_get(&obj_request->kref);
1216}
1217
1218static void rbd_obj_request_destroy(struct kref *kref);
1219static void rbd_obj_request_put(struct rbd_obj_request *obj_request)
1220{
1221 rbd_assert(obj_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001222 dout("%s: obj %p (was %d)\n", __func__, obj_request,
1223 atomic_read(&obj_request->kref.refcount));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001224 kref_put(&obj_request->kref, rbd_obj_request_destroy);
1225}
1226
1227static void rbd_img_request_get(struct rbd_img_request *img_request)
1228{
Alex Elder37206ee2013-02-20 17:32:08 -06001229 dout("%s: img %p (was %d)\n", __func__, img_request,
1230 atomic_read(&img_request->kref.refcount));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001231 kref_get(&img_request->kref);
1232}
1233
1234static void rbd_img_request_destroy(struct kref *kref);
1235static void rbd_img_request_put(struct rbd_img_request *img_request)
1236{
1237 rbd_assert(img_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001238 dout("%s: img %p (was %d)\n", __func__, img_request,
1239 atomic_read(&img_request->kref.refcount));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001240 kref_put(&img_request->kref, rbd_img_request_destroy);
1241}
1242
1243static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request,
1244 struct rbd_obj_request *obj_request)
1245{
Alex Elder25dcf952013-01-25 17:08:55 -06001246 rbd_assert(obj_request->img_request == NULL);
1247
Alex Elderb155e862013-04-15 14:50:37 -05001248 /* Image request now owns object's original reference */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001249 obj_request->img_request = img_request;
Alex Elder25dcf952013-01-25 17:08:55 -06001250 obj_request->which = img_request->obj_request_count;
Alex Elder6365d332013-02-11 12:33:24 -06001251 rbd_assert(!obj_request_img_data_test(obj_request));
1252 obj_request_img_data_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001253 rbd_assert(obj_request->which != BAD_WHICH);
Alex Elder25dcf952013-01-25 17:08:55 -06001254 img_request->obj_request_count++;
1255 list_add_tail(&obj_request->links, &img_request->obj_requests);
Alex Elder37206ee2013-02-20 17:32:08 -06001256 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1257 obj_request->which);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001258}
1259
1260static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request,
1261 struct rbd_obj_request *obj_request)
1262{
1263 rbd_assert(obj_request->which != BAD_WHICH);
Alex Elder25dcf952013-01-25 17:08:55 -06001264
Alex Elder37206ee2013-02-20 17:32:08 -06001265 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1266 obj_request->which);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001267 list_del(&obj_request->links);
Alex Elder25dcf952013-01-25 17:08:55 -06001268 rbd_assert(img_request->obj_request_count > 0);
1269 img_request->obj_request_count--;
1270 rbd_assert(obj_request->which == img_request->obj_request_count);
1271 obj_request->which = BAD_WHICH;
Alex Elder6365d332013-02-11 12:33:24 -06001272 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001273 rbd_assert(obj_request->img_request == img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001274 obj_request->img_request = NULL;
Alex Elder25dcf952013-01-25 17:08:55 -06001275 obj_request->callback = NULL;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001276 rbd_obj_request_put(obj_request);
1277}
1278
1279static bool obj_request_type_valid(enum obj_request_type type)
1280{
1281 switch (type) {
Alex Elder9969ebc2013-01-18 12:31:10 -06001282 case OBJ_REQUEST_NODATA:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001283 case OBJ_REQUEST_BIO:
Alex Elder788e2df2013-01-17 12:25:27 -06001284 case OBJ_REQUEST_PAGES:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001285 return true;
1286 default:
1287 return false;
1288 }
1289}
1290
Alex Elderbf0d5f502012-11-22 00:00:08 -06001291static int rbd_obj_request_submit(struct ceph_osd_client *osdc,
1292 struct rbd_obj_request *obj_request)
1293{
Alex Elder37206ee2013-02-20 17:32:08 -06001294 dout("%s: osdc %p obj %p\n", __func__, osdc, obj_request);
1295
Alex Elderbf0d5f502012-11-22 00:00:08 -06001296 return ceph_osdc_start_request(osdc, obj_request->osd_req, false);
1297}
1298
1299static void rbd_img_request_complete(struct rbd_img_request *img_request)
1300{
Alex Elder55f27e02013-04-10 12:34:25 -05001301
Alex Elder37206ee2013-02-20 17:32:08 -06001302 dout("%s: img %p\n", __func__, img_request);
Alex Elder55f27e02013-04-10 12:34:25 -05001303
1304 /*
1305 * If no error occurred, compute the aggregate transfer
1306 * count for the image request. We could instead use
1307 * atomic64_cmpxchg() to update it as each object request
1308 * completes; not clear which way is better off hand.
1309 */
1310 if (!img_request->result) {
1311 struct rbd_obj_request *obj_request;
1312 u64 xferred = 0;
1313
1314 for_each_obj_request(img_request, obj_request)
1315 xferred += obj_request->xferred;
1316 img_request->xferred = xferred;
1317 }
1318
Alex Elderbf0d5f502012-11-22 00:00:08 -06001319 if (img_request->callback)
1320 img_request->callback(img_request);
1321 else
1322 rbd_img_request_put(img_request);
1323}
1324
Alex Elder788e2df2013-01-17 12:25:27 -06001325/* Caller is responsible for rbd_obj_request_destroy(obj_request) */
1326
1327static int rbd_obj_request_wait(struct rbd_obj_request *obj_request)
1328{
Alex Elder37206ee2013-02-20 17:32:08 -06001329 dout("%s: obj %p\n", __func__, obj_request);
1330
Alex Elder788e2df2013-01-17 12:25:27 -06001331 return wait_for_completion_interruptible(&obj_request->completion);
1332}
1333
Alex Elder0c425242013-02-08 09:55:49 -06001334/*
1335 * The default/initial value for all image request flags is 0. Each
1336 * is conditionally set to 1 at image request initialization time
1337 * and currently never change thereafter.
1338 */
1339static void img_request_write_set(struct rbd_img_request *img_request)
1340{
1341 set_bit(IMG_REQ_WRITE, &img_request->flags);
1342 smp_mb();
1343}
1344
1345static bool img_request_write_test(struct rbd_img_request *img_request)
1346{
1347 smp_mb();
1348 return test_bit(IMG_REQ_WRITE, &img_request->flags) != 0;
1349}
1350
Alex Elder9849e982013-01-24 16:13:36 -06001351static void img_request_child_set(struct rbd_img_request *img_request)
1352{
1353 set_bit(IMG_REQ_CHILD, &img_request->flags);
1354 smp_mb();
1355}
1356
1357static bool img_request_child_test(struct rbd_img_request *img_request)
1358{
1359 smp_mb();
1360 return test_bit(IMG_REQ_CHILD, &img_request->flags) != 0;
1361}
1362
Alex Elderd0b2e942013-01-24 16:13:36 -06001363static void img_request_layered_set(struct rbd_img_request *img_request)
1364{
1365 set_bit(IMG_REQ_LAYERED, &img_request->flags);
1366 smp_mb();
1367}
1368
1369static bool img_request_layered_test(struct rbd_img_request *img_request)
1370{
1371 smp_mb();
1372 return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0;
1373}
1374
Alex Elder6e2a4502013-03-27 09:16:30 -05001375static void
1376rbd_img_obj_request_read_callback(struct rbd_obj_request *obj_request)
1377{
Alex Elderb9434c52013-04-19 15:34:50 -05001378 u64 xferred = obj_request->xferred;
1379 u64 length = obj_request->length;
1380
Alex Elder6e2a4502013-03-27 09:16:30 -05001381 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1382 obj_request, obj_request->img_request, obj_request->result,
Alex Elderb9434c52013-04-19 15:34:50 -05001383 xferred, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001384 /*
1385 * ENOENT means a hole in the image. We zero-fill the
1386 * entire length of the request. A short read also implies
1387 * zero-fill to the end of the request. Either way we
1388 * update the xferred count to indicate the whole request
1389 * was satisfied.
1390 */
Alex Elderb9434c52013-04-19 15:34:50 -05001391 rbd_assert(obj_request->type != OBJ_REQUEST_NODATA);
Alex Elder6e2a4502013-03-27 09:16:30 -05001392 if (obj_request->result == -ENOENT) {
Alex Elderb9434c52013-04-19 15:34:50 -05001393 if (obj_request->type == OBJ_REQUEST_BIO)
1394 zero_bio_chain(obj_request->bio_list, 0);
1395 else
1396 zero_pages(obj_request->pages, 0, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001397 obj_request->result = 0;
Alex Elderb9434c52013-04-19 15:34:50 -05001398 obj_request->xferred = length;
1399 } else if (xferred < length && !obj_request->result) {
1400 if (obj_request->type == OBJ_REQUEST_BIO)
1401 zero_bio_chain(obj_request->bio_list, xferred);
1402 else
1403 zero_pages(obj_request->pages, xferred, length);
1404 obj_request->xferred = length;
Alex Elder6e2a4502013-03-27 09:16:30 -05001405 }
1406 obj_request_done_set(obj_request);
1407}
1408
Alex Elderbf0d5f502012-11-22 00:00:08 -06001409static void rbd_obj_request_complete(struct rbd_obj_request *obj_request)
1410{
Alex Elder37206ee2013-02-20 17:32:08 -06001411 dout("%s: obj %p cb %p\n", __func__, obj_request,
1412 obj_request->callback);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001413 if (obj_request->callback)
1414 obj_request->callback(obj_request);
Alex Elder788e2df2013-01-17 12:25:27 -06001415 else
1416 complete_all(&obj_request->completion);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001417}
1418
Alex Elderc47f9372013-02-26 14:23:07 -06001419static void rbd_osd_trivial_callback(struct rbd_obj_request *obj_request)
Alex Elder39bf2c52013-02-26 14:23:07 -06001420{
1421 dout("%s: obj %p\n", __func__, obj_request);
1422 obj_request_done_set(obj_request);
1423}
1424
Alex Elderc47f9372013-02-26 14:23:07 -06001425static void rbd_osd_read_callback(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001426{
Alex Elder57acbaa2013-02-11 12:33:24 -06001427 struct rbd_img_request *img_request = NULL;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001428 struct rbd_device *rbd_dev = NULL;
Alex Elder57acbaa2013-02-11 12:33:24 -06001429 bool layered = false;
1430
1431 if (obj_request_img_data_test(obj_request)) {
1432 img_request = obj_request->img_request;
1433 layered = img_request && img_request_layered_test(img_request);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001434 rbd_dev = img_request->rbd_dev;
Alex Elder57acbaa2013-02-11 12:33:24 -06001435 }
Alex Elder8b3e1a52013-01-24 16:13:36 -06001436
1437 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1438 obj_request, img_request, obj_request->result,
1439 obj_request->xferred, obj_request->length);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001440 if (layered && obj_request->result == -ENOENT &&
1441 obj_request->img_offset < rbd_dev->parent_overlap)
Alex Elder8b3e1a52013-01-24 16:13:36 -06001442 rbd_img_parent_read(obj_request);
1443 else if (img_request)
Alex Elder6e2a4502013-03-27 09:16:30 -05001444 rbd_img_obj_request_read_callback(obj_request);
1445 else
1446 obj_request_done_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001447}
1448
Alex Elderc47f9372013-02-26 14:23:07 -06001449static void rbd_osd_write_callback(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001450{
Sage Weil1b83bef2013-02-25 16:11:12 -08001451 dout("%s: obj %p result %d %llu\n", __func__, obj_request,
1452 obj_request->result, obj_request->length);
1453 /*
Alex Elder8b3e1a52013-01-24 16:13:36 -06001454 * There is no such thing as a successful short write. Set
1455 * it to our originally-requested length.
Sage Weil1b83bef2013-02-25 16:11:12 -08001456 */
1457 obj_request->xferred = obj_request->length;
Alex Elder07741302013-02-05 23:41:50 -06001458 obj_request_done_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001459}
1460
Alex Elderfbfab532013-02-08 09:55:48 -06001461/*
1462 * For a simple stat call there's nothing to do. We'll do more if
1463 * this is part of a write sequence for a layered image.
1464 */
Alex Elderc47f9372013-02-26 14:23:07 -06001465static void rbd_osd_stat_callback(struct rbd_obj_request *obj_request)
Alex Elderfbfab532013-02-08 09:55:48 -06001466{
Alex Elder37206ee2013-02-20 17:32:08 -06001467 dout("%s: obj %p\n", __func__, obj_request);
Alex Elderfbfab532013-02-08 09:55:48 -06001468 obj_request_done_set(obj_request);
1469}
1470
Alex Elderbf0d5f502012-11-22 00:00:08 -06001471static void rbd_osd_req_callback(struct ceph_osd_request *osd_req,
1472 struct ceph_msg *msg)
1473{
1474 struct rbd_obj_request *obj_request = osd_req->r_priv;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001475 u16 opcode;
1476
Alex Elder37206ee2013-02-20 17:32:08 -06001477 dout("%s: osd_req %p msg %p\n", __func__, osd_req, msg);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001478 rbd_assert(osd_req == obj_request->osd_req);
Alex Elder57acbaa2013-02-11 12:33:24 -06001479 if (obj_request_img_data_test(obj_request)) {
1480 rbd_assert(obj_request->img_request);
1481 rbd_assert(obj_request->which != BAD_WHICH);
1482 } else {
1483 rbd_assert(obj_request->which == BAD_WHICH);
1484 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06001485
Sage Weil1b83bef2013-02-25 16:11:12 -08001486 if (osd_req->r_result < 0)
1487 obj_request->result = osd_req->r_result;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001488 obj_request->version = le64_to_cpu(osd_req->r_reassert_version.version);
1489
Alex Elder0eefd472013-04-19 15:34:50 -05001490 BUG_ON(osd_req->r_num_ops > 2);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001491
Alex Elderc47f9372013-02-26 14:23:07 -06001492 /*
1493 * We support a 64-bit length, but ultimately it has to be
1494 * passed to blk_end_request(), which takes an unsigned int.
1495 */
Sage Weil1b83bef2013-02-25 16:11:12 -08001496 obj_request->xferred = osd_req->r_reply_op_len[0];
Alex Elder8b3e1a52013-01-24 16:13:36 -06001497 rbd_assert(obj_request->xferred < (u64)UINT_MAX);
Alex Elder79528732013-04-03 21:32:51 -05001498 opcode = osd_req->r_ops[0].op;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001499 switch (opcode) {
1500 case CEPH_OSD_OP_READ:
Alex Elderc47f9372013-02-26 14:23:07 -06001501 rbd_osd_read_callback(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001502 break;
1503 case CEPH_OSD_OP_WRITE:
Alex Elderc47f9372013-02-26 14:23:07 -06001504 rbd_osd_write_callback(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001505 break;
Alex Elderfbfab532013-02-08 09:55:48 -06001506 case CEPH_OSD_OP_STAT:
Alex Elderc47f9372013-02-26 14:23:07 -06001507 rbd_osd_stat_callback(obj_request);
Alex Elderfbfab532013-02-08 09:55:48 -06001508 break;
Alex Elder36be9a72013-01-19 00:30:28 -06001509 case CEPH_OSD_OP_CALL:
Alex Elderb8d70032012-11-30 17:53:04 -06001510 case CEPH_OSD_OP_NOTIFY_ACK:
Alex Elder9969ebc2013-01-18 12:31:10 -06001511 case CEPH_OSD_OP_WATCH:
Alex Elderc47f9372013-02-26 14:23:07 -06001512 rbd_osd_trivial_callback(obj_request);
Alex Elder9969ebc2013-01-18 12:31:10 -06001513 break;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001514 default:
1515 rbd_warn(NULL, "%s: unsupported op %hu\n",
1516 obj_request->object_name, (unsigned short) opcode);
1517 break;
1518 }
1519
Alex Elder07741302013-02-05 23:41:50 -06001520 if (obj_request_done_test(obj_request))
Alex Elderbf0d5f502012-11-22 00:00:08 -06001521 rbd_obj_request_complete(obj_request);
1522}
1523
Alex Elder9d4df012013-04-19 15:34:50 -05001524static void rbd_osd_req_format_read(struct rbd_obj_request *obj_request)
Alex Elder430c28c2013-04-03 21:32:51 -05001525{
1526 struct rbd_img_request *img_request = obj_request->img_request;
Alex Elder8c042b02013-04-03 01:28:58 -05001527 struct ceph_osd_request *osd_req = obj_request->osd_req;
Alex Elder9d4df012013-04-19 15:34:50 -05001528 u64 snap_id;
Alex Elder430c28c2013-04-03 21:32:51 -05001529
Alex Elder8c042b02013-04-03 01:28:58 -05001530 rbd_assert(osd_req != NULL);
Alex Elder430c28c2013-04-03 21:32:51 -05001531
Alex Elder9d4df012013-04-19 15:34:50 -05001532 snap_id = img_request ? img_request->snap_id : CEPH_NOSNAP;
Alex Elder8c042b02013-04-03 01:28:58 -05001533 ceph_osdc_build_request(osd_req, obj_request->offset,
Alex Elder9d4df012013-04-19 15:34:50 -05001534 NULL, snap_id, NULL);
1535}
1536
1537static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request)
1538{
1539 struct rbd_img_request *img_request = obj_request->img_request;
1540 struct ceph_osd_request *osd_req = obj_request->osd_req;
1541 struct ceph_snap_context *snapc;
1542 struct timespec mtime = CURRENT_TIME;
1543
1544 rbd_assert(osd_req != NULL);
1545
1546 snapc = img_request ? img_request->snapc : NULL;
1547 ceph_osdc_build_request(osd_req, obj_request->offset,
1548 snapc, CEPH_NOSNAP, &mtime);
Alex Elder430c28c2013-04-03 21:32:51 -05001549}
1550
Alex Elderbf0d5f502012-11-22 00:00:08 -06001551static struct ceph_osd_request *rbd_osd_req_create(
1552 struct rbd_device *rbd_dev,
1553 bool write_request,
Alex Elder430c28c2013-04-03 21:32:51 -05001554 struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001555{
Alex Elderbf0d5f502012-11-22 00:00:08 -06001556 struct ceph_snap_context *snapc = NULL;
1557 struct ceph_osd_client *osdc;
1558 struct ceph_osd_request *osd_req;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001559
Alex Elder6365d332013-02-11 12:33:24 -06001560 if (obj_request_img_data_test(obj_request)) {
1561 struct rbd_img_request *img_request = obj_request->img_request;
1562
Alex Elder0c425242013-02-08 09:55:49 -06001563 rbd_assert(write_request ==
1564 img_request_write_test(img_request));
1565 if (write_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001566 snapc = img_request->snapc;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001567 }
1568
1569 /* Allocate and initialize the request, for the single op */
1570
1571 osdc = &rbd_dev->rbd_client->client->osdc;
1572 osd_req = ceph_osdc_alloc_request(osdc, snapc, 1, false, GFP_ATOMIC);
1573 if (!osd_req)
1574 return NULL; /* ENOMEM */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001575
Alex Elder430c28c2013-04-03 21:32:51 -05001576 if (write_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001577 osd_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK;
Alex Elder430c28c2013-04-03 21:32:51 -05001578 else
Alex Elderbf0d5f502012-11-22 00:00:08 -06001579 osd_req->r_flags = CEPH_OSD_FLAG_READ;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001580
1581 osd_req->r_callback = rbd_osd_req_callback;
1582 osd_req->r_priv = obj_request;
1583
1584 osd_req->r_oid_len = strlen(obj_request->object_name);
1585 rbd_assert(osd_req->r_oid_len < sizeof (osd_req->r_oid));
1586 memcpy(osd_req->r_oid, obj_request->object_name, osd_req->r_oid_len);
1587
1588 osd_req->r_file_layout = rbd_dev->layout; /* struct */
1589
Alex Elderbf0d5f502012-11-22 00:00:08 -06001590 return osd_req;
1591}
1592
Alex Elder0eefd472013-04-19 15:34:50 -05001593/*
1594 * Create a copyup osd request based on the information in the
1595 * object request supplied. A copyup request has two osd ops,
1596 * a copyup method call, and a "normal" write request.
1597 */
1598static struct ceph_osd_request *
1599rbd_osd_req_create_copyup(struct rbd_obj_request *obj_request)
1600{
1601 struct rbd_img_request *img_request;
1602 struct ceph_snap_context *snapc;
1603 struct rbd_device *rbd_dev;
1604 struct ceph_osd_client *osdc;
1605 struct ceph_osd_request *osd_req;
1606
1607 rbd_assert(obj_request_img_data_test(obj_request));
1608 img_request = obj_request->img_request;
1609 rbd_assert(img_request);
1610 rbd_assert(img_request_write_test(img_request));
1611
1612 /* Allocate and initialize the request, for the two ops */
1613
1614 snapc = img_request->snapc;
1615 rbd_dev = img_request->rbd_dev;
1616 osdc = &rbd_dev->rbd_client->client->osdc;
1617 osd_req = ceph_osdc_alloc_request(osdc, snapc, 2, false, GFP_ATOMIC);
1618 if (!osd_req)
1619 return NULL; /* ENOMEM */
1620
1621 osd_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK;
1622 osd_req->r_callback = rbd_osd_req_callback;
1623 osd_req->r_priv = obj_request;
1624
1625 osd_req->r_oid_len = strlen(obj_request->object_name);
1626 rbd_assert(osd_req->r_oid_len < sizeof (osd_req->r_oid));
1627 memcpy(osd_req->r_oid, obj_request->object_name, osd_req->r_oid_len);
1628
1629 osd_req->r_file_layout = rbd_dev->layout; /* struct */
1630
1631 return osd_req;
1632}
1633
1634
Alex Elderbf0d5f502012-11-22 00:00:08 -06001635static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req)
1636{
1637 ceph_osdc_put_request(osd_req);
1638}
1639
1640/* object_name is assumed to be a non-null pointer and NUL-terminated */
1641
1642static struct rbd_obj_request *rbd_obj_request_create(const char *object_name,
1643 u64 offset, u64 length,
1644 enum obj_request_type type)
1645{
1646 struct rbd_obj_request *obj_request;
1647 size_t size;
1648 char *name;
1649
1650 rbd_assert(obj_request_type_valid(type));
1651
1652 size = strlen(object_name) + 1;
1653 obj_request = kzalloc(sizeof (*obj_request) + size, GFP_KERNEL);
1654 if (!obj_request)
1655 return NULL;
1656
1657 name = (char *)(obj_request + 1);
1658 obj_request->object_name = memcpy(name, object_name, size);
1659 obj_request->offset = offset;
1660 obj_request->length = length;
Alex Elder926f9b32013-02-11 12:33:24 -06001661 obj_request->flags = 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001662 obj_request->which = BAD_WHICH;
1663 obj_request->type = type;
1664 INIT_LIST_HEAD(&obj_request->links);
Alex Elder788e2df2013-01-17 12:25:27 -06001665 init_completion(&obj_request->completion);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001666 kref_init(&obj_request->kref);
1667
Alex Elder37206ee2013-02-20 17:32:08 -06001668 dout("%s: \"%s\" %llu/%llu %d -> obj %p\n", __func__, object_name,
1669 offset, length, (int)type, obj_request);
1670
Alex Elderbf0d5f502012-11-22 00:00:08 -06001671 return obj_request;
1672}
1673
1674static void rbd_obj_request_destroy(struct kref *kref)
1675{
1676 struct rbd_obj_request *obj_request;
1677
1678 obj_request = container_of(kref, struct rbd_obj_request, kref);
1679
Alex Elder37206ee2013-02-20 17:32:08 -06001680 dout("%s: obj %p\n", __func__, obj_request);
1681
Alex Elderbf0d5f502012-11-22 00:00:08 -06001682 rbd_assert(obj_request->img_request == NULL);
1683 rbd_assert(obj_request->which == BAD_WHICH);
1684
1685 if (obj_request->osd_req)
1686 rbd_osd_req_destroy(obj_request->osd_req);
1687
1688 rbd_assert(obj_request_type_valid(obj_request->type));
1689 switch (obj_request->type) {
Alex Elder9969ebc2013-01-18 12:31:10 -06001690 case OBJ_REQUEST_NODATA:
1691 break; /* Nothing to do */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001692 case OBJ_REQUEST_BIO:
1693 if (obj_request->bio_list)
1694 bio_chain_put(obj_request->bio_list);
1695 break;
Alex Elder788e2df2013-01-17 12:25:27 -06001696 case OBJ_REQUEST_PAGES:
1697 if (obj_request->pages)
1698 ceph_release_page_vector(obj_request->pages,
1699 obj_request->page_count);
1700 break;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001701 }
1702
1703 kfree(obj_request);
1704}
1705
1706/*
1707 * Caller is responsible for filling in the list of object requests
1708 * that comprises the image request, and the Linux request pointer
1709 * (if there is one).
1710 */
Alex Eldercc344fa2013-02-19 12:25:56 -06001711static struct rbd_img_request *rbd_img_request_create(
1712 struct rbd_device *rbd_dev,
Alex Elderbf0d5f502012-11-22 00:00:08 -06001713 u64 offset, u64 length,
Alex Elder9849e982013-01-24 16:13:36 -06001714 bool write_request,
1715 bool child_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001716{
1717 struct rbd_img_request *img_request;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001718
1719 img_request = kmalloc(sizeof (*img_request), GFP_ATOMIC);
1720 if (!img_request)
1721 return NULL;
1722
1723 if (write_request) {
1724 down_read(&rbd_dev->header_rwsem);
Alex Elder812164f82013-04-30 00:44:32 -05001725 ceph_get_snap_context(rbd_dev->header.snapc);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001726 up_read(&rbd_dev->header_rwsem);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001727 }
1728
1729 img_request->rq = NULL;
1730 img_request->rbd_dev = rbd_dev;
1731 img_request->offset = offset;
1732 img_request->length = length;
Alex Elder0c425242013-02-08 09:55:49 -06001733 img_request->flags = 0;
1734 if (write_request) {
1735 img_request_write_set(img_request);
Alex Elder468521c2013-04-26 09:43:47 -05001736 img_request->snapc = rbd_dev->header.snapc;
Alex Elder0c425242013-02-08 09:55:49 -06001737 } else {
Alex Elderbf0d5f502012-11-22 00:00:08 -06001738 img_request->snap_id = rbd_dev->spec->snap_id;
Alex Elder0c425242013-02-08 09:55:49 -06001739 }
Alex Elder9849e982013-01-24 16:13:36 -06001740 if (child_request)
1741 img_request_child_set(img_request);
Alex Elderd0b2e942013-01-24 16:13:36 -06001742 if (rbd_dev->parent_spec)
1743 img_request_layered_set(img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001744 spin_lock_init(&img_request->completion_lock);
1745 img_request->next_completion = 0;
1746 img_request->callback = NULL;
Alex Eldera5a337d2013-01-24 16:13:36 -06001747 img_request->result = 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001748 img_request->obj_request_count = 0;
1749 INIT_LIST_HEAD(&img_request->obj_requests);
1750 kref_init(&img_request->kref);
1751
1752 rbd_img_request_get(img_request); /* Avoid a warning */
1753 rbd_img_request_put(img_request); /* TEMPORARY */
1754
Alex Elder37206ee2013-02-20 17:32:08 -06001755 dout("%s: rbd_dev %p %s %llu/%llu -> img %p\n", __func__, rbd_dev,
1756 write_request ? "write" : "read", offset, length,
1757 img_request);
1758
Alex Elderbf0d5f502012-11-22 00:00:08 -06001759 return img_request;
1760}
1761
1762static void rbd_img_request_destroy(struct kref *kref)
1763{
1764 struct rbd_img_request *img_request;
1765 struct rbd_obj_request *obj_request;
1766 struct rbd_obj_request *next_obj_request;
1767
1768 img_request = container_of(kref, struct rbd_img_request, kref);
1769
Alex Elder37206ee2013-02-20 17:32:08 -06001770 dout("%s: img %p\n", __func__, img_request);
1771
Alex Elderbf0d5f502012-11-22 00:00:08 -06001772 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
1773 rbd_img_obj_request_del(img_request, obj_request);
Alex Elder25dcf952013-01-25 17:08:55 -06001774 rbd_assert(img_request->obj_request_count == 0);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001775
Alex Elder0c425242013-02-08 09:55:49 -06001776 if (img_request_write_test(img_request))
Alex Elder812164f82013-04-30 00:44:32 -05001777 ceph_put_snap_context(img_request->snapc);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001778
Alex Elder8b3e1a52013-01-24 16:13:36 -06001779 if (img_request_child_test(img_request))
1780 rbd_obj_request_put(img_request->obj_request);
1781
Alex Elderbf0d5f502012-11-22 00:00:08 -06001782 kfree(img_request);
1783}
1784
Alex Elder12178572013-02-08 09:55:49 -06001785static bool rbd_img_obj_end_request(struct rbd_obj_request *obj_request)
1786{
Alex Elder6365d332013-02-11 12:33:24 -06001787 struct rbd_img_request *img_request;
Alex Elder12178572013-02-08 09:55:49 -06001788 unsigned int xferred;
1789 int result;
Alex Elder8b3e1a52013-01-24 16:13:36 -06001790 bool more;
Alex Elder12178572013-02-08 09:55:49 -06001791
Alex Elder6365d332013-02-11 12:33:24 -06001792 rbd_assert(obj_request_img_data_test(obj_request));
1793 img_request = obj_request->img_request;
1794
Alex Elder12178572013-02-08 09:55:49 -06001795 rbd_assert(obj_request->xferred <= (u64)UINT_MAX);
1796 xferred = (unsigned int)obj_request->xferred;
1797 result = obj_request->result;
1798 if (result) {
1799 struct rbd_device *rbd_dev = img_request->rbd_dev;
1800
1801 rbd_warn(rbd_dev, "%s %llx at %llx (%llx)\n",
1802 img_request_write_test(img_request) ? "write" : "read",
1803 obj_request->length, obj_request->img_offset,
1804 obj_request->offset);
1805 rbd_warn(rbd_dev, " result %d xferred %x\n",
1806 result, xferred);
1807 if (!img_request->result)
1808 img_request->result = result;
1809 }
1810
Alex Elderf1a47392013-04-19 15:34:50 -05001811 /* Image object requests don't own their page array */
1812
1813 if (obj_request->type == OBJ_REQUEST_PAGES) {
1814 obj_request->pages = NULL;
1815 obj_request->page_count = 0;
1816 }
1817
Alex Elder8b3e1a52013-01-24 16:13:36 -06001818 if (img_request_child_test(img_request)) {
1819 rbd_assert(img_request->obj_request != NULL);
1820 more = obj_request->which < img_request->obj_request_count - 1;
1821 } else {
1822 rbd_assert(img_request->rq != NULL);
1823 more = blk_end_request(img_request->rq, result, xferred);
1824 }
1825
1826 return more;
Alex Elder12178572013-02-08 09:55:49 -06001827}
1828
Alex Elder21692382013-04-05 01:27:12 -05001829static void rbd_img_obj_callback(struct rbd_obj_request *obj_request)
1830{
1831 struct rbd_img_request *img_request;
1832 u32 which = obj_request->which;
1833 bool more = true;
1834
Alex Elder6365d332013-02-11 12:33:24 -06001835 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elder21692382013-04-05 01:27:12 -05001836 img_request = obj_request->img_request;
1837
1838 dout("%s: img %p obj %p\n", __func__, img_request, obj_request);
1839 rbd_assert(img_request != NULL);
Alex Elder21692382013-04-05 01:27:12 -05001840 rbd_assert(img_request->obj_request_count > 0);
1841 rbd_assert(which != BAD_WHICH);
1842 rbd_assert(which < img_request->obj_request_count);
1843 rbd_assert(which >= img_request->next_completion);
1844
1845 spin_lock_irq(&img_request->completion_lock);
1846 if (which != img_request->next_completion)
1847 goto out;
1848
1849 for_each_obj_request_from(img_request, obj_request) {
Alex Elder21692382013-04-05 01:27:12 -05001850 rbd_assert(more);
1851 rbd_assert(which < img_request->obj_request_count);
1852
1853 if (!obj_request_done_test(obj_request))
1854 break;
Alex Elder12178572013-02-08 09:55:49 -06001855 more = rbd_img_obj_end_request(obj_request);
Alex Elder21692382013-04-05 01:27:12 -05001856 which++;
1857 }
1858
1859 rbd_assert(more ^ (which == img_request->obj_request_count));
1860 img_request->next_completion = which;
1861out:
1862 spin_unlock_irq(&img_request->completion_lock);
1863
1864 if (!more)
1865 rbd_img_request_complete(img_request);
1866}
1867
Alex Elderf1a47392013-04-19 15:34:50 -05001868/*
1869 * Split up an image request into one or more object requests, each
1870 * to a different object. The "type" parameter indicates whether
1871 * "data_desc" is the pointer to the head of a list of bio
1872 * structures, or the base of a page array. In either case this
1873 * function assumes data_desc describes memory sufficient to hold
1874 * all data described by the image request.
1875 */
1876static int rbd_img_request_fill(struct rbd_img_request *img_request,
1877 enum obj_request_type type,
1878 void *data_desc)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001879{
1880 struct rbd_device *rbd_dev = img_request->rbd_dev;
1881 struct rbd_obj_request *obj_request = NULL;
1882 struct rbd_obj_request *next_obj_request;
Alex Elder0c425242013-02-08 09:55:49 -06001883 bool write_request = img_request_write_test(img_request);
Alex Elderf1a47392013-04-19 15:34:50 -05001884 struct bio *bio_list;
1885 unsigned int bio_offset = 0;
1886 struct page **pages;
Alex Elder7da22d22013-01-24 16:13:36 -06001887 u64 img_offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001888 u64 resid;
1889 u16 opcode;
1890
Alex Elderf1a47392013-04-19 15:34:50 -05001891 dout("%s: img %p type %d data_desc %p\n", __func__, img_request,
1892 (int)type, data_desc);
Alex Elder37206ee2013-02-20 17:32:08 -06001893
Alex Elder430c28c2013-04-03 21:32:51 -05001894 opcode = write_request ? CEPH_OSD_OP_WRITE : CEPH_OSD_OP_READ;
Alex Elder7da22d22013-01-24 16:13:36 -06001895 img_offset = img_request->offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001896 resid = img_request->length;
Alex Elder4dda41d2013-02-20 21:59:33 -06001897 rbd_assert(resid > 0);
Alex Elderf1a47392013-04-19 15:34:50 -05001898
1899 if (type == OBJ_REQUEST_BIO) {
1900 bio_list = data_desc;
1901 rbd_assert(img_offset == bio_list->bi_sector << SECTOR_SHIFT);
1902 } else {
1903 rbd_assert(type == OBJ_REQUEST_PAGES);
1904 pages = data_desc;
1905 }
1906
Alex Elderbf0d5f502012-11-22 00:00:08 -06001907 while (resid) {
Alex Elder2fa12322013-04-05 01:27:12 -05001908 struct ceph_osd_request *osd_req;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001909 const char *object_name;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001910 u64 offset;
1911 u64 length;
1912
Alex Elder7da22d22013-01-24 16:13:36 -06001913 object_name = rbd_segment_name(rbd_dev, img_offset);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001914 if (!object_name)
1915 goto out_unwind;
Alex Elder7da22d22013-01-24 16:13:36 -06001916 offset = rbd_segment_offset(rbd_dev, img_offset);
1917 length = rbd_segment_length(rbd_dev, img_offset, resid);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001918 obj_request = rbd_obj_request_create(object_name,
Alex Elderf1a47392013-04-19 15:34:50 -05001919 offset, length, type);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001920 kfree(object_name); /* object request has its own copy */
1921 if (!obj_request)
1922 goto out_unwind;
1923
Alex Elderf1a47392013-04-19 15:34:50 -05001924 if (type == OBJ_REQUEST_BIO) {
1925 unsigned int clone_size;
1926
1927 rbd_assert(length <= (u64)UINT_MAX);
1928 clone_size = (unsigned int)length;
1929 obj_request->bio_list =
1930 bio_chain_clone_range(&bio_list,
1931 &bio_offset,
1932 clone_size,
1933 GFP_ATOMIC);
1934 if (!obj_request->bio_list)
1935 goto out_partial;
1936 } else {
1937 unsigned int page_count;
1938
1939 obj_request->pages = pages;
1940 page_count = (u32)calc_pages_for(offset, length);
1941 obj_request->page_count = page_count;
1942 if ((offset + length) & ~PAGE_MASK)
1943 page_count--; /* more on last page */
1944 pages += page_count;
1945 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06001946
Alex Elder2fa12322013-04-05 01:27:12 -05001947 osd_req = rbd_osd_req_create(rbd_dev, write_request,
1948 obj_request);
1949 if (!osd_req)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001950 goto out_partial;
Alex Elder2fa12322013-04-05 01:27:12 -05001951 obj_request->osd_req = osd_req;
Alex Elder21692382013-04-05 01:27:12 -05001952 obj_request->callback = rbd_img_obj_callback;
Alex Elder430c28c2013-04-03 21:32:51 -05001953
Alex Elder2fa12322013-04-05 01:27:12 -05001954 osd_req_op_extent_init(osd_req, 0, opcode, offset, length,
1955 0, 0);
Alex Elderf1a47392013-04-19 15:34:50 -05001956 if (type == OBJ_REQUEST_BIO)
1957 osd_req_op_extent_osd_data_bio(osd_req, 0,
1958 obj_request->bio_list, length);
1959 else
1960 osd_req_op_extent_osd_data_pages(osd_req, 0,
1961 obj_request->pages, length,
1962 offset & ~PAGE_MASK, false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05001963
1964 if (write_request)
1965 rbd_osd_req_format_write(obj_request);
1966 else
1967 rbd_osd_req_format_read(obj_request);
Alex Elder430c28c2013-04-03 21:32:51 -05001968
Alex Elder7da22d22013-01-24 16:13:36 -06001969 obj_request->img_offset = img_offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001970 rbd_img_obj_request_add(img_request, obj_request);
1971
Alex Elder7da22d22013-01-24 16:13:36 -06001972 img_offset += length;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001973 resid -= length;
1974 }
1975
1976 return 0;
1977
1978out_partial:
1979 rbd_obj_request_put(obj_request);
1980out_unwind:
1981 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
1982 rbd_obj_request_put(obj_request);
1983
1984 return -ENOMEM;
1985}
1986
Alex Elder3d7efd12013-04-19 15:34:50 -05001987static void
Alex Elder0eefd472013-04-19 15:34:50 -05001988rbd_img_obj_copyup_callback(struct rbd_obj_request *obj_request)
1989{
1990 struct rbd_img_request *img_request;
1991 struct rbd_device *rbd_dev;
1992 u64 length;
1993 u32 page_count;
1994
1995 rbd_assert(obj_request->type == OBJ_REQUEST_BIO);
1996 rbd_assert(obj_request_img_data_test(obj_request));
1997 img_request = obj_request->img_request;
1998 rbd_assert(img_request);
1999
2000 rbd_dev = img_request->rbd_dev;
2001 rbd_assert(rbd_dev);
2002 length = (u64)1 << rbd_dev->header.obj_order;
2003 page_count = (u32)calc_pages_for(0, length);
2004
2005 rbd_assert(obj_request->copyup_pages);
2006 ceph_release_page_vector(obj_request->copyup_pages, page_count);
2007 obj_request->copyup_pages = NULL;
2008
2009 /*
2010 * We want the transfer count to reflect the size of the
2011 * original write request. There is no such thing as a
2012 * successful short write, so if the request was successful
2013 * we can just set it to the originally-requested length.
2014 */
2015 if (!obj_request->result)
2016 obj_request->xferred = obj_request->length;
2017
2018 /* Finish up with the normal image object callback */
2019
2020 rbd_img_obj_callback(obj_request);
2021}
2022
2023static void
Alex Elder3d7efd12013-04-19 15:34:50 -05002024rbd_img_obj_parent_read_full_callback(struct rbd_img_request *img_request)
2025{
2026 struct rbd_obj_request *orig_request;
Alex Elder0eefd472013-04-19 15:34:50 -05002027 struct ceph_osd_request *osd_req;
2028 struct ceph_osd_client *osdc;
2029 struct rbd_device *rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002030 struct page **pages;
Alex Elder3d7efd12013-04-19 15:34:50 -05002031 int result;
2032 u64 obj_size;
2033 u64 xferred;
2034
2035 rbd_assert(img_request_child_test(img_request));
2036
2037 /* First get what we need from the image request */
2038
2039 pages = img_request->copyup_pages;
2040 rbd_assert(pages != NULL);
2041 img_request->copyup_pages = NULL;
2042
2043 orig_request = img_request->obj_request;
2044 rbd_assert(orig_request != NULL);
Alex Elder0eefd472013-04-19 15:34:50 -05002045 rbd_assert(orig_request->type == OBJ_REQUEST_BIO);
Alex Elder3d7efd12013-04-19 15:34:50 -05002046 result = img_request->result;
2047 obj_size = img_request->length;
2048 xferred = img_request->xferred;
2049
Alex Elder0eefd472013-04-19 15:34:50 -05002050 rbd_dev = img_request->rbd_dev;
2051 rbd_assert(rbd_dev);
2052 rbd_assert(obj_size == (u64)1 << rbd_dev->header.obj_order);
2053
Alex Elder3d7efd12013-04-19 15:34:50 -05002054 rbd_img_request_put(img_request);
2055
Alex Elder0eefd472013-04-19 15:34:50 -05002056 if (result)
2057 goto out_err;
Alex Elder3d7efd12013-04-19 15:34:50 -05002058
Alex Elder0eefd472013-04-19 15:34:50 -05002059 /* Allocate the new copyup osd request for the original request */
Alex Elder3d7efd12013-04-19 15:34:50 -05002060
Alex Elder0eefd472013-04-19 15:34:50 -05002061 result = -ENOMEM;
2062 rbd_assert(!orig_request->osd_req);
2063 osd_req = rbd_osd_req_create_copyup(orig_request);
2064 if (!osd_req)
2065 goto out_err;
2066 orig_request->osd_req = osd_req;
2067 orig_request->copyup_pages = pages;
Alex Elder3d7efd12013-04-19 15:34:50 -05002068
Alex Elder0eefd472013-04-19 15:34:50 -05002069 /* Initialize the copyup op */
2070
2071 osd_req_op_cls_init(osd_req, 0, CEPH_OSD_OP_CALL, "rbd", "copyup");
2072 osd_req_op_cls_request_data_pages(osd_req, 0, pages, obj_size, 0,
2073 false, false);
2074
2075 /* Then the original write request op */
2076
2077 osd_req_op_extent_init(osd_req, 1, CEPH_OSD_OP_WRITE,
2078 orig_request->offset,
2079 orig_request->length, 0, 0);
2080 osd_req_op_extent_osd_data_bio(osd_req, 1, orig_request->bio_list,
2081 orig_request->length);
2082
2083 rbd_osd_req_format_write(orig_request);
2084
2085 /* All set, send it off. */
2086
2087 orig_request->callback = rbd_img_obj_copyup_callback;
2088 osdc = &rbd_dev->rbd_client->client->osdc;
2089 result = rbd_obj_request_submit(osdc, orig_request);
2090 if (!result)
2091 return;
2092out_err:
2093 /* Record the error code and complete the request */
2094
2095 orig_request->result = result;
2096 orig_request->xferred = 0;
2097 obj_request_done_set(orig_request);
2098 rbd_obj_request_complete(orig_request);
Alex Elder3d7efd12013-04-19 15:34:50 -05002099}
2100
2101/*
2102 * Read from the parent image the range of data that covers the
2103 * entire target of the given object request. This is used for
2104 * satisfying a layered image write request when the target of an
2105 * object request from the image request does not exist.
2106 *
2107 * A page array big enough to hold the returned data is allocated
2108 * and supplied to rbd_img_request_fill() as the "data descriptor."
2109 * When the read completes, this page array will be transferred to
2110 * the original object request for the copyup operation.
2111 *
2112 * If an error occurs, record it as the result of the original
2113 * object request and mark it done so it gets completed.
2114 */
2115static int rbd_img_obj_parent_read_full(struct rbd_obj_request *obj_request)
2116{
2117 struct rbd_img_request *img_request = NULL;
2118 struct rbd_img_request *parent_request = NULL;
2119 struct rbd_device *rbd_dev;
2120 u64 img_offset;
2121 u64 length;
2122 struct page **pages = NULL;
2123 u32 page_count;
2124 int result;
2125
2126 rbd_assert(obj_request_img_data_test(obj_request));
2127 rbd_assert(obj_request->type == OBJ_REQUEST_BIO);
2128
2129 img_request = obj_request->img_request;
2130 rbd_assert(img_request != NULL);
2131 rbd_dev = img_request->rbd_dev;
2132 rbd_assert(rbd_dev->parent != NULL);
2133
2134 /*
Alex Elder0eefd472013-04-19 15:34:50 -05002135 * First things first. The original osd request is of no
2136 * use to use any more, we'll need a new one that can hold
2137 * the two ops in a copyup request. We'll get that later,
2138 * but for now we can release the old one.
2139 */
2140 rbd_osd_req_destroy(obj_request->osd_req);
2141 obj_request->osd_req = NULL;
2142
2143 /*
Alex Elder3d7efd12013-04-19 15:34:50 -05002144 * Determine the byte range covered by the object in the
2145 * child image to which the original request was to be sent.
2146 */
2147 img_offset = obj_request->img_offset - obj_request->offset;
2148 length = (u64)1 << rbd_dev->header.obj_order;
2149
2150 /*
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002151 * There is no defined parent data beyond the parent
2152 * overlap, so limit what we read at that boundary if
2153 * necessary.
2154 */
2155 if (img_offset + length > rbd_dev->parent_overlap) {
2156 rbd_assert(img_offset < rbd_dev->parent_overlap);
2157 length = rbd_dev->parent_overlap - img_offset;
2158 }
2159
2160 /*
Alex Elder3d7efd12013-04-19 15:34:50 -05002161 * Allocate a page array big enough to receive the data read
2162 * from the parent.
2163 */
2164 page_count = (u32)calc_pages_for(0, length);
2165 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2166 if (IS_ERR(pages)) {
2167 result = PTR_ERR(pages);
2168 pages = NULL;
2169 goto out_err;
2170 }
2171
2172 result = -ENOMEM;
2173 parent_request = rbd_img_request_create(rbd_dev->parent,
2174 img_offset, length,
2175 false, true);
2176 if (!parent_request)
2177 goto out_err;
2178 rbd_obj_request_get(obj_request);
2179 parent_request->obj_request = obj_request;
2180
2181 result = rbd_img_request_fill(parent_request, OBJ_REQUEST_PAGES, pages);
2182 if (result)
2183 goto out_err;
2184 parent_request->copyup_pages = pages;
2185
2186 parent_request->callback = rbd_img_obj_parent_read_full_callback;
2187 result = rbd_img_request_submit(parent_request);
2188 if (!result)
2189 return 0;
2190
2191 parent_request->copyup_pages = NULL;
2192 parent_request->obj_request = NULL;
2193 rbd_obj_request_put(obj_request);
2194out_err:
2195 if (pages)
2196 ceph_release_page_vector(pages, page_count);
2197 if (parent_request)
2198 rbd_img_request_put(parent_request);
2199 obj_request->result = result;
2200 obj_request->xferred = 0;
2201 obj_request_done_set(obj_request);
2202
2203 return result;
2204}
2205
Alex Elderc5b5ef62013-02-11 12:33:24 -06002206static void rbd_img_obj_exists_callback(struct rbd_obj_request *obj_request)
2207{
Alex Elderc5b5ef62013-02-11 12:33:24 -06002208 struct rbd_obj_request *orig_request;
2209 int result;
2210
2211 rbd_assert(!obj_request_img_data_test(obj_request));
2212
2213 /*
2214 * All we need from the object request is the original
2215 * request and the result of the STAT op. Grab those, then
2216 * we're done with the request.
2217 */
2218 orig_request = obj_request->obj_request;
2219 obj_request->obj_request = NULL;
2220 rbd_assert(orig_request);
2221 rbd_assert(orig_request->img_request);
2222
2223 result = obj_request->result;
2224 obj_request->result = 0;
2225
2226 dout("%s: obj %p for obj %p result %d %llu/%llu\n", __func__,
2227 obj_request, orig_request, result,
2228 obj_request->xferred, obj_request->length);
2229 rbd_obj_request_put(obj_request);
2230
2231 rbd_assert(orig_request);
2232 rbd_assert(orig_request->img_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002233
2234 /*
2235 * Our only purpose here is to determine whether the object
2236 * exists, and we don't want to treat the non-existence as
2237 * an error. If something else comes back, transfer the
2238 * error to the original request and complete it now.
2239 */
2240 if (!result) {
2241 obj_request_existence_set(orig_request, true);
2242 } else if (result == -ENOENT) {
2243 obj_request_existence_set(orig_request, false);
2244 } else if (result) {
2245 orig_request->result = result;
Alex Elder3d7efd12013-04-19 15:34:50 -05002246 goto out;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002247 }
2248
2249 /*
2250 * Resubmit the original request now that we have recorded
2251 * whether the target object exists.
2252 */
Alex Elderb454e362013-04-19 15:34:50 -05002253 orig_request->result = rbd_img_obj_request_submit(orig_request);
Alex Elder3d7efd12013-04-19 15:34:50 -05002254out:
Alex Elderc5b5ef62013-02-11 12:33:24 -06002255 if (orig_request->result)
2256 rbd_obj_request_complete(orig_request);
2257 rbd_obj_request_put(orig_request);
2258}
2259
2260static int rbd_img_obj_exists_submit(struct rbd_obj_request *obj_request)
2261{
2262 struct rbd_obj_request *stat_request;
2263 struct rbd_device *rbd_dev;
2264 struct ceph_osd_client *osdc;
2265 struct page **pages = NULL;
2266 u32 page_count;
2267 size_t size;
2268 int ret;
2269
2270 /*
2271 * The response data for a STAT call consists of:
2272 * le64 length;
2273 * struct {
2274 * le32 tv_sec;
2275 * le32 tv_nsec;
2276 * } mtime;
2277 */
2278 size = sizeof (__le64) + sizeof (__le32) + sizeof (__le32);
2279 page_count = (u32)calc_pages_for(0, size);
2280 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2281 if (IS_ERR(pages))
2282 return PTR_ERR(pages);
2283
2284 ret = -ENOMEM;
2285 stat_request = rbd_obj_request_create(obj_request->object_name, 0, 0,
2286 OBJ_REQUEST_PAGES);
2287 if (!stat_request)
2288 goto out;
2289
2290 rbd_obj_request_get(obj_request);
2291 stat_request->obj_request = obj_request;
2292 stat_request->pages = pages;
2293 stat_request->page_count = page_count;
2294
2295 rbd_assert(obj_request->img_request);
2296 rbd_dev = obj_request->img_request->rbd_dev;
2297 stat_request->osd_req = rbd_osd_req_create(rbd_dev, false,
2298 stat_request);
2299 if (!stat_request->osd_req)
2300 goto out;
2301 stat_request->callback = rbd_img_obj_exists_callback;
2302
2303 osd_req_op_init(stat_request->osd_req, 0, CEPH_OSD_OP_STAT);
2304 osd_req_op_raw_data_in_pages(stat_request->osd_req, 0, pages, size, 0,
2305 false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05002306 rbd_osd_req_format_read(stat_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002307
2308 osdc = &rbd_dev->rbd_client->client->osdc;
2309 ret = rbd_obj_request_submit(osdc, stat_request);
2310out:
2311 if (ret)
2312 rbd_obj_request_put(obj_request);
2313
2314 return ret;
2315}
2316
Alex Elderb454e362013-04-19 15:34:50 -05002317static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request)
2318{
2319 struct rbd_img_request *img_request;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002320 struct rbd_device *rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002321 bool known;
Alex Elderb454e362013-04-19 15:34:50 -05002322
2323 rbd_assert(obj_request_img_data_test(obj_request));
2324
2325 img_request = obj_request->img_request;
2326 rbd_assert(img_request);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002327 rbd_dev = img_request->rbd_dev;
Alex Elderb454e362013-04-19 15:34:50 -05002328
Alex Elderb454e362013-04-19 15:34:50 -05002329 /*
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002330 * Only writes to layered images need special handling.
2331 * Reads and non-layered writes are simple object requests.
2332 * Layered writes that start beyond the end of the overlap
2333 * with the parent have no parent data, so they too are
2334 * simple object requests. Finally, if the target object is
2335 * known to already exist, its parent data has already been
2336 * copied, so a write to the object can also be handled as a
2337 * simple object request.
Alex Elderb454e362013-04-19 15:34:50 -05002338 */
2339 if (!img_request_write_test(img_request) ||
2340 !img_request_layered_test(img_request) ||
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002341 rbd_dev->parent_overlap <= obj_request->img_offset ||
Alex Elder3d7efd12013-04-19 15:34:50 -05002342 ((known = obj_request_known_test(obj_request)) &&
2343 obj_request_exists_test(obj_request))) {
Alex Elderb454e362013-04-19 15:34:50 -05002344
2345 struct rbd_device *rbd_dev;
2346 struct ceph_osd_client *osdc;
2347
2348 rbd_dev = obj_request->img_request->rbd_dev;
2349 osdc = &rbd_dev->rbd_client->client->osdc;
2350
2351 return rbd_obj_request_submit(osdc, obj_request);
2352 }
2353
2354 /*
Alex Elder3d7efd12013-04-19 15:34:50 -05002355 * It's a layered write. The target object might exist but
2356 * we may not know that yet. If we know it doesn't exist,
2357 * start by reading the data for the full target object from
2358 * the parent so we can use it for a copyup to the target.
Alex Elderb454e362013-04-19 15:34:50 -05002359 */
Alex Elder3d7efd12013-04-19 15:34:50 -05002360 if (known)
2361 return rbd_img_obj_parent_read_full(obj_request);
2362
2363 /* We don't know whether the target exists. Go find out. */
Alex Elderb454e362013-04-19 15:34:50 -05002364
2365 return rbd_img_obj_exists_submit(obj_request);
2366}
2367
Alex Elderbf0d5f502012-11-22 00:00:08 -06002368static int rbd_img_request_submit(struct rbd_img_request *img_request)
2369{
Alex Elderbf0d5f502012-11-22 00:00:08 -06002370 struct rbd_obj_request *obj_request;
Alex Elder46faeed2013-04-10 17:47:46 -05002371 struct rbd_obj_request *next_obj_request;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002372
Alex Elder37206ee2013-02-20 17:32:08 -06002373 dout("%s: img %p\n", __func__, img_request);
Alex Elder46faeed2013-04-10 17:47:46 -05002374 for_each_obj_request_safe(img_request, obj_request, next_obj_request) {
Alex Elderbf0d5f502012-11-22 00:00:08 -06002375 int ret;
2376
Alex Elderb454e362013-04-19 15:34:50 -05002377 ret = rbd_img_obj_request_submit(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002378 if (ret)
2379 return ret;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002380 }
2381
2382 return 0;
2383}
2384
Alex Elder8b3e1a52013-01-24 16:13:36 -06002385static void rbd_img_parent_read_callback(struct rbd_img_request *img_request)
2386{
2387 struct rbd_obj_request *obj_request;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002388 struct rbd_device *rbd_dev;
2389 u64 obj_end;
Alex Elder8b3e1a52013-01-24 16:13:36 -06002390
2391 rbd_assert(img_request_child_test(img_request));
2392
2393 obj_request = img_request->obj_request;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002394 rbd_assert(obj_request);
2395 rbd_assert(obj_request->img_request);
Alex Elder8b3e1a52013-01-24 16:13:36 -06002396
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002397 obj_request->result = img_request->result;
2398 if (obj_request->result)
2399 goto out;
2400
2401 /*
2402 * We need to zero anything beyond the parent overlap
2403 * boundary. Since rbd_img_obj_request_read_callback()
2404 * will zero anything beyond the end of a short read, an
2405 * easy way to do this is to pretend the data from the
2406 * parent came up short--ending at the overlap boundary.
2407 */
2408 rbd_assert(obj_request->img_offset < U64_MAX - obj_request->length);
2409 obj_end = obj_request->img_offset + obj_request->length;
2410 rbd_dev = obj_request->img_request->rbd_dev;
2411 if (obj_end > rbd_dev->parent_overlap) {
2412 u64 xferred = 0;
2413
2414 if (obj_request->img_offset < rbd_dev->parent_overlap)
2415 xferred = rbd_dev->parent_overlap -
2416 obj_request->img_offset;
2417
2418 obj_request->xferred = min(img_request->xferred, xferred);
2419 } else {
2420 obj_request->xferred = img_request->xferred;
2421 }
2422out:
Alex Elder8b3e1a52013-01-24 16:13:36 -06002423 rbd_img_obj_request_read_callback(obj_request);
2424 rbd_obj_request_complete(obj_request);
2425}
2426
2427static void rbd_img_parent_read(struct rbd_obj_request *obj_request)
2428{
2429 struct rbd_device *rbd_dev;
2430 struct rbd_img_request *img_request;
2431 int result;
2432
2433 rbd_assert(obj_request_img_data_test(obj_request));
2434 rbd_assert(obj_request->img_request != NULL);
2435 rbd_assert(obj_request->result == (s32) -ENOENT);
2436 rbd_assert(obj_request->type == OBJ_REQUEST_BIO);
2437
2438 rbd_dev = obj_request->img_request->rbd_dev;
2439 rbd_assert(rbd_dev->parent != NULL);
2440 /* rbd_read_finish(obj_request, obj_request->length); */
2441 img_request = rbd_img_request_create(rbd_dev->parent,
2442 obj_request->img_offset,
2443 obj_request->length,
2444 false, true);
2445 result = -ENOMEM;
2446 if (!img_request)
2447 goto out_err;
2448
2449 rbd_obj_request_get(obj_request);
2450 img_request->obj_request = obj_request;
2451
Alex Elderf1a47392013-04-19 15:34:50 -05002452 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
2453 obj_request->bio_list);
Alex Elder8b3e1a52013-01-24 16:13:36 -06002454 if (result)
2455 goto out_err;
2456
2457 img_request->callback = rbd_img_parent_read_callback;
2458 result = rbd_img_request_submit(img_request);
2459 if (result)
2460 goto out_err;
2461
2462 return;
2463out_err:
2464 if (img_request)
2465 rbd_img_request_put(img_request);
2466 obj_request->result = result;
2467 obj_request->xferred = 0;
2468 obj_request_done_set(obj_request);
2469}
2470
Alex Eldercf81b602013-01-17 12:18:46 -06002471static int rbd_obj_notify_ack(struct rbd_device *rbd_dev,
Alex Elderb8d70032012-11-30 17:53:04 -06002472 u64 ver, u64 notify_id)
2473{
2474 struct rbd_obj_request *obj_request;
Alex Elder21692382013-04-05 01:27:12 -05002475 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elderb8d70032012-11-30 17:53:04 -06002476 int ret;
2477
2478 obj_request = rbd_obj_request_create(rbd_dev->header_name, 0, 0,
2479 OBJ_REQUEST_NODATA);
2480 if (!obj_request)
2481 return -ENOMEM;
2482
2483 ret = -ENOMEM;
Alex Elder430c28c2013-04-03 21:32:51 -05002484 obj_request->osd_req = rbd_osd_req_create(rbd_dev, false, obj_request);
Alex Elderb8d70032012-11-30 17:53:04 -06002485 if (!obj_request->osd_req)
2486 goto out;
Alex Elder21692382013-04-05 01:27:12 -05002487 obj_request->callback = rbd_obj_request_put;
Alex Elderb8d70032012-11-30 17:53:04 -06002488
Alex Elderc99d2d42013-04-05 01:27:11 -05002489 osd_req_op_watch_init(obj_request->osd_req, 0, CEPH_OSD_OP_NOTIFY_ACK,
2490 notify_id, ver, 0);
Alex Elder9d4df012013-04-19 15:34:50 -05002491 rbd_osd_req_format_read(obj_request);
Alex Elder430c28c2013-04-03 21:32:51 -05002492
Alex Elderb8d70032012-11-30 17:53:04 -06002493 ret = rbd_obj_request_submit(osdc, obj_request);
Alex Elderb8d70032012-11-30 17:53:04 -06002494out:
Alex Eldercf81b602013-01-17 12:18:46 -06002495 if (ret)
2496 rbd_obj_request_put(obj_request);
Alex Elderb8d70032012-11-30 17:53:04 -06002497
2498 return ret;
2499}
2500
2501static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
2502{
2503 struct rbd_device *rbd_dev = (struct rbd_device *)data;
2504 u64 hver;
Alex Elderb8d70032012-11-30 17:53:04 -06002505
2506 if (!rbd_dev)
2507 return;
2508
Alex Elder37206ee2013-02-20 17:32:08 -06002509 dout("%s: \"%s\" notify_id %llu opcode %u\n", __func__,
Alex Elderb8d70032012-11-30 17:53:04 -06002510 rbd_dev->header_name, (unsigned long long) notify_id,
2511 (unsigned int) opcode);
Alex Elder522a0cc2013-04-25 15:09:41 -05002512 (void)rbd_dev_refresh(rbd_dev, &hver);
Alex Elderb8d70032012-11-30 17:53:04 -06002513
Alex Eldercf81b602013-01-17 12:18:46 -06002514 rbd_obj_notify_ack(rbd_dev, hver, notify_id);
Alex Elderb8d70032012-11-30 17:53:04 -06002515}
2516
Alex Elder9969ebc2013-01-18 12:31:10 -06002517/*
2518 * Request sync osd watch/unwatch. The value of "start" determines
2519 * whether a watch request is being initiated or torn down.
2520 */
2521static int rbd_dev_header_watch_sync(struct rbd_device *rbd_dev, int start)
2522{
2523 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
2524 struct rbd_obj_request *obj_request;
Alex Elder9969ebc2013-01-18 12:31:10 -06002525 int ret;
2526
2527 rbd_assert(start ^ !!rbd_dev->watch_event);
2528 rbd_assert(start ^ !!rbd_dev->watch_request);
2529
2530 if (start) {
Alex Elder3c663bb2013-02-15 11:42:30 -06002531 ret = ceph_osdc_create_event(osdc, rbd_watch_cb, rbd_dev,
Alex Elder9969ebc2013-01-18 12:31:10 -06002532 &rbd_dev->watch_event);
2533 if (ret < 0)
2534 return ret;
Alex Elder8eb87562013-01-25 17:08:55 -06002535 rbd_assert(rbd_dev->watch_event != NULL);
Alex Elder9969ebc2013-01-18 12:31:10 -06002536 }
2537
2538 ret = -ENOMEM;
2539 obj_request = rbd_obj_request_create(rbd_dev->header_name, 0, 0,
2540 OBJ_REQUEST_NODATA);
2541 if (!obj_request)
2542 goto out_cancel;
2543
Alex Elder430c28c2013-04-03 21:32:51 -05002544 obj_request->osd_req = rbd_osd_req_create(rbd_dev, true, obj_request);
2545 if (!obj_request->osd_req)
2546 goto out_cancel;
2547
Alex Elder8eb87562013-01-25 17:08:55 -06002548 if (start)
Alex Elder975241a2013-01-25 17:08:55 -06002549 ceph_osdc_set_request_linger(osdc, obj_request->osd_req);
Alex Elder8eb87562013-01-25 17:08:55 -06002550 else
Alex Elder6977c3f2013-01-25 17:08:55 -06002551 ceph_osdc_unregister_linger_request(osdc,
Alex Elder975241a2013-01-25 17:08:55 -06002552 rbd_dev->watch_request->osd_req);
Alex Elder21692382013-04-05 01:27:12 -05002553
2554 osd_req_op_watch_init(obj_request->osd_req, 0, CEPH_OSD_OP_WATCH,
Alex Elderb21ebdd2013-04-30 00:44:32 -05002555 rbd_dev->watch_event->cookie, 0, start);
Alex Elder9d4df012013-04-19 15:34:50 -05002556 rbd_osd_req_format_write(obj_request);
Alex Elder21692382013-04-05 01:27:12 -05002557
Alex Elder9969ebc2013-01-18 12:31:10 -06002558 ret = rbd_obj_request_submit(osdc, obj_request);
2559 if (ret)
2560 goto out_cancel;
2561 ret = rbd_obj_request_wait(obj_request);
2562 if (ret)
2563 goto out_cancel;
Alex Elder9969ebc2013-01-18 12:31:10 -06002564 ret = obj_request->result;
2565 if (ret)
2566 goto out_cancel;
2567
Alex Elder8eb87562013-01-25 17:08:55 -06002568 /*
2569 * A watch request is set to linger, so the underlying osd
2570 * request won't go away until we unregister it. We retain
2571 * a pointer to the object request during that time (in
2572 * rbd_dev->watch_request), so we'll keep a reference to
2573 * it. We'll drop that reference (below) after we've
2574 * unregistered it.
2575 */
2576 if (start) {
2577 rbd_dev->watch_request = obj_request;
2578
2579 return 0;
2580 }
2581
2582 /* We have successfully torn down the watch request */
2583
2584 rbd_obj_request_put(rbd_dev->watch_request);
2585 rbd_dev->watch_request = NULL;
Alex Elder9969ebc2013-01-18 12:31:10 -06002586out_cancel:
2587 /* Cancel the event if we're tearing down, or on error */
2588 ceph_osdc_cancel_event(rbd_dev->watch_event);
2589 rbd_dev->watch_event = NULL;
Alex Elder9969ebc2013-01-18 12:31:10 -06002590 if (obj_request)
2591 rbd_obj_request_put(obj_request);
2592
2593 return ret;
2594}
2595
Alex Elder36be9a72013-01-19 00:30:28 -06002596/*
Alex Elderf40eb342013-04-25 15:09:42 -05002597 * Synchronous osd object method call. Returns the number of bytes
2598 * returned in the outbound buffer, or a negative error code.
Alex Elder36be9a72013-01-19 00:30:28 -06002599 */
2600static int rbd_obj_method_sync(struct rbd_device *rbd_dev,
2601 const char *object_name,
2602 const char *class_name,
2603 const char *method_name,
Alex Elder41579762013-04-21 12:14:45 -05002604 const void *outbound,
Alex Elder36be9a72013-01-19 00:30:28 -06002605 size_t outbound_size,
Alex Elder41579762013-04-21 12:14:45 -05002606 void *inbound,
Alex Elder36be9a72013-01-19 00:30:28 -06002607 size_t inbound_size,
2608 u64 *version)
2609{
Alex Elder21692382013-04-05 01:27:12 -05002610 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder36be9a72013-01-19 00:30:28 -06002611 struct rbd_obj_request *obj_request;
Alex Elder36be9a72013-01-19 00:30:28 -06002612 struct page **pages;
2613 u32 page_count;
2614 int ret;
2615
2616 /*
Alex Elder6010a452013-04-05 01:27:11 -05002617 * Method calls are ultimately read operations. The result
2618 * should placed into the inbound buffer provided. They
2619 * also supply outbound data--parameters for the object
2620 * method. Currently if this is present it will be a
2621 * snapshot id.
Alex Elder36be9a72013-01-19 00:30:28 -06002622 */
Alex Elder57385b52013-04-21 12:14:45 -05002623 page_count = (u32)calc_pages_for(0, inbound_size);
Alex Elder36be9a72013-01-19 00:30:28 -06002624 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2625 if (IS_ERR(pages))
2626 return PTR_ERR(pages);
2627
2628 ret = -ENOMEM;
Alex Elder6010a452013-04-05 01:27:11 -05002629 obj_request = rbd_obj_request_create(object_name, 0, inbound_size,
Alex Elder36be9a72013-01-19 00:30:28 -06002630 OBJ_REQUEST_PAGES);
2631 if (!obj_request)
2632 goto out;
2633
2634 obj_request->pages = pages;
2635 obj_request->page_count = page_count;
2636
Alex Elder430c28c2013-04-03 21:32:51 -05002637 obj_request->osd_req = rbd_osd_req_create(rbd_dev, false, obj_request);
Alex Elder36be9a72013-01-19 00:30:28 -06002638 if (!obj_request->osd_req)
2639 goto out;
2640
Alex Elderc99d2d42013-04-05 01:27:11 -05002641 osd_req_op_cls_init(obj_request->osd_req, 0, CEPH_OSD_OP_CALL,
Alex Elder04017e22013-04-05 14:46:02 -05002642 class_name, method_name);
2643 if (outbound_size) {
2644 struct ceph_pagelist *pagelist;
2645
2646 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
2647 if (!pagelist)
2648 goto out;
2649
2650 ceph_pagelist_init(pagelist);
2651 ceph_pagelist_append(pagelist, outbound, outbound_size);
2652 osd_req_op_cls_request_data_pagelist(obj_request->osd_req, 0,
2653 pagelist);
2654 }
Alex Eldera4ce40a2013-04-05 01:27:12 -05002655 osd_req_op_cls_response_data_pages(obj_request->osd_req, 0,
2656 obj_request->pages, inbound_size,
Alex Elder44cd1882013-04-05 01:27:12 -05002657 0, false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05002658 rbd_osd_req_format_read(obj_request);
Alex Elder430c28c2013-04-03 21:32:51 -05002659
Alex Elder36be9a72013-01-19 00:30:28 -06002660 ret = rbd_obj_request_submit(osdc, obj_request);
2661 if (ret)
2662 goto out;
2663 ret = rbd_obj_request_wait(obj_request);
2664 if (ret)
2665 goto out;
2666
2667 ret = obj_request->result;
2668 if (ret < 0)
2669 goto out;
Alex Elder57385b52013-04-21 12:14:45 -05002670
2671 rbd_assert(obj_request->xferred < (u64)INT_MAX);
2672 ret = (int)obj_request->xferred;
Alex Elder903bb322013-02-06 13:11:38 -06002673 ceph_copy_from_page_vector(pages, inbound, 0, obj_request->xferred);
Alex Elder36be9a72013-01-19 00:30:28 -06002674 if (version)
2675 *version = obj_request->version;
2676out:
2677 if (obj_request)
2678 rbd_obj_request_put(obj_request);
2679 else
2680 ceph_release_page_vector(pages, page_count);
2681
2682 return ret;
2683}
2684
Alex Elderbf0d5f502012-11-22 00:00:08 -06002685static void rbd_request_fn(struct request_queue *q)
Alex Eldercc344fa2013-02-19 12:25:56 -06002686 __releases(q->queue_lock) __acquires(q->queue_lock)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002687{
2688 struct rbd_device *rbd_dev = q->queuedata;
2689 bool read_only = rbd_dev->mapping.read_only;
2690 struct request *rq;
2691 int result;
2692
2693 while ((rq = blk_fetch_request(q))) {
2694 bool write_request = rq_data_dir(rq) == WRITE;
2695 struct rbd_img_request *img_request;
2696 u64 offset;
2697 u64 length;
2698
2699 /* Ignore any non-FS requests that filter through. */
2700
2701 if (rq->cmd_type != REQ_TYPE_FS) {
Alex Elder4dda41d2013-02-20 21:59:33 -06002702 dout("%s: non-fs request type %d\n", __func__,
2703 (int) rq->cmd_type);
2704 __blk_end_request_all(rq, 0);
2705 continue;
2706 }
2707
2708 /* Ignore/skip any zero-length requests */
2709
2710 offset = (u64) blk_rq_pos(rq) << SECTOR_SHIFT;
2711 length = (u64) blk_rq_bytes(rq);
2712
2713 if (!length) {
2714 dout("%s: zero-length request\n", __func__);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002715 __blk_end_request_all(rq, 0);
2716 continue;
2717 }
2718
2719 spin_unlock_irq(q->queue_lock);
2720
2721 /* Disallow writes to a read-only device */
2722
2723 if (write_request) {
2724 result = -EROFS;
2725 if (read_only)
2726 goto end_request;
2727 rbd_assert(rbd_dev->spec->snap_id == CEPH_NOSNAP);
2728 }
2729
Alex Elder6d292902013-01-14 12:43:31 -06002730 /*
2731 * Quit early if the mapped snapshot no longer
2732 * exists. It's still possible the snapshot will
2733 * have disappeared by the time our request arrives
2734 * at the osd, but there's no sense in sending it if
2735 * we already know.
2736 */
2737 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) {
Alex Elderbf0d5f502012-11-22 00:00:08 -06002738 dout("request for non-existent snapshot");
2739 rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP);
2740 result = -ENXIO;
2741 goto end_request;
2742 }
2743
Alex Elderbf0d5f502012-11-22 00:00:08 -06002744 result = -EINVAL;
Alex Elderc0cd10db2013-04-26 09:43:47 -05002745 if (offset && length > U64_MAX - offset + 1) {
2746 rbd_warn(rbd_dev, "bad request range (%llu~%llu)\n",
2747 offset, length);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002748 goto end_request; /* Shouldn't happen */
Alex Elderc0cd10db2013-04-26 09:43:47 -05002749 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06002750
2751 result = -ENOMEM;
2752 img_request = rbd_img_request_create(rbd_dev, offset, length,
Alex Elder9849e982013-01-24 16:13:36 -06002753 write_request, false);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002754 if (!img_request)
2755 goto end_request;
2756
2757 img_request->rq = rq;
2758
Alex Elderf1a47392013-04-19 15:34:50 -05002759 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
2760 rq->bio);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002761 if (!result)
2762 result = rbd_img_request_submit(img_request);
2763 if (result)
2764 rbd_img_request_put(img_request);
2765end_request:
2766 spin_lock_irq(q->queue_lock);
2767 if (result < 0) {
Alex Elder7da22d22013-01-24 16:13:36 -06002768 rbd_warn(rbd_dev, "%s %llx at %llx result %d\n",
2769 write_request ? "write" : "read",
2770 length, offset, result);
2771
Alex Elderbf0d5f502012-11-22 00:00:08 -06002772 __blk_end_request_all(rq, result);
2773 }
2774 }
2775}
2776
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002777/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002778 * a queue callback. Makes sure that we don't create a bio that spans across
2779 * multiple osd objects. One exception would be with a single page bios,
Alex Elderf7760da2012-10-20 22:17:27 -05002780 * which we handle later at bio_chain_clone_range()
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002781 */
2782static int rbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd,
2783 struct bio_vec *bvec)
2784{
2785 struct rbd_device *rbd_dev = q->queuedata;
Alex Eldere5cfeed22012-10-20 22:17:27 -05002786 sector_t sector_offset;
2787 sector_t sectors_per_obj;
2788 sector_t obj_sector_offset;
2789 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002790
Alex Eldere5cfeed22012-10-20 22:17:27 -05002791 /*
2792 * Find how far into its rbd object the partition-relative
2793 * bio start sector is to offset relative to the enclosing
2794 * device.
2795 */
2796 sector_offset = get_start_sect(bmd->bi_bdev) + bmd->bi_sector;
2797 sectors_per_obj = 1 << (rbd_dev->header.obj_order - SECTOR_SHIFT);
2798 obj_sector_offset = sector_offset & (sectors_per_obj - 1);
Alex Elder593a9e72012-02-07 12:03:37 -06002799
Alex Eldere5cfeed22012-10-20 22:17:27 -05002800 /*
2801 * Compute the number of bytes from that offset to the end
2802 * of the object. Account for what's already used by the bio.
2803 */
2804 ret = (int) (sectors_per_obj - obj_sector_offset) << SECTOR_SHIFT;
2805 if (ret > bmd->bi_size)
2806 ret -= bmd->bi_size;
2807 else
2808 ret = 0;
2809
2810 /*
2811 * Don't send back more than was asked for. And if the bio
2812 * was empty, let the whole thing through because: "Note
2813 * that a block device *must* allow a single page to be
2814 * added to an empty bio."
2815 */
2816 rbd_assert(bvec->bv_len <= PAGE_SIZE);
2817 if (ret > (int) bvec->bv_len || !bmd->bi_size)
2818 ret = (int) bvec->bv_len;
2819
2820 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002821}
2822
2823static void rbd_free_disk(struct rbd_device *rbd_dev)
2824{
2825 struct gendisk *disk = rbd_dev->disk;
2826
2827 if (!disk)
2828 return;
2829
Alex Eldera0cab922013-04-25 23:15:08 -05002830 rbd_dev->disk = NULL;
2831 if (disk->flags & GENHD_FL_UP) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002832 del_gendisk(disk);
Alex Eldera0cab922013-04-25 23:15:08 -05002833 if (disk->queue)
2834 blk_cleanup_queue(disk->queue);
2835 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002836 put_disk(disk);
2837}
2838
Alex Elder788e2df2013-01-17 12:25:27 -06002839static int rbd_obj_read_sync(struct rbd_device *rbd_dev,
2840 const char *object_name,
2841 u64 offset, u64 length,
Alex Elder80ef15b2013-04-21 12:14:45 -05002842 void *buf, u64 *version)
Alex Elder788e2df2013-01-17 12:25:27 -06002843
2844{
Alex Elder21692382013-04-05 01:27:12 -05002845 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder788e2df2013-01-17 12:25:27 -06002846 struct rbd_obj_request *obj_request;
Alex Elder788e2df2013-01-17 12:25:27 -06002847 struct page **pages = NULL;
2848 u32 page_count;
Alex Elder1ceae7e2013-02-06 13:11:38 -06002849 size_t size;
Alex Elder788e2df2013-01-17 12:25:27 -06002850 int ret;
2851
2852 page_count = (u32) calc_pages_for(offset, length);
2853 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2854 if (IS_ERR(pages))
2855 ret = PTR_ERR(pages);
2856
2857 ret = -ENOMEM;
2858 obj_request = rbd_obj_request_create(object_name, offset, length,
Alex Elder36be9a72013-01-19 00:30:28 -06002859 OBJ_REQUEST_PAGES);
Alex Elder788e2df2013-01-17 12:25:27 -06002860 if (!obj_request)
2861 goto out;
2862
2863 obj_request->pages = pages;
2864 obj_request->page_count = page_count;
2865
Alex Elder430c28c2013-04-03 21:32:51 -05002866 obj_request->osd_req = rbd_osd_req_create(rbd_dev, false, obj_request);
Alex Elder788e2df2013-01-17 12:25:27 -06002867 if (!obj_request->osd_req)
2868 goto out;
2869
Alex Elderc99d2d42013-04-05 01:27:11 -05002870 osd_req_op_extent_init(obj_request->osd_req, 0, CEPH_OSD_OP_READ,
2871 offset, length, 0, 0);
Alex Elder406e2c92013-04-15 14:50:36 -05002872 osd_req_op_extent_osd_data_pages(obj_request->osd_req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05002873 obj_request->pages,
Alex Elder44cd1882013-04-05 01:27:12 -05002874 obj_request->length,
2875 obj_request->offset & ~PAGE_MASK,
2876 false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05002877 rbd_osd_req_format_read(obj_request);
Alex Elder430c28c2013-04-03 21:32:51 -05002878
Alex Elder788e2df2013-01-17 12:25:27 -06002879 ret = rbd_obj_request_submit(osdc, obj_request);
2880 if (ret)
2881 goto out;
2882 ret = rbd_obj_request_wait(obj_request);
2883 if (ret)
2884 goto out;
2885
2886 ret = obj_request->result;
2887 if (ret < 0)
2888 goto out;
Alex Elder1ceae7e2013-02-06 13:11:38 -06002889
2890 rbd_assert(obj_request->xferred <= (u64) SIZE_MAX);
2891 size = (size_t) obj_request->xferred;
Alex Elder903bb322013-02-06 13:11:38 -06002892 ceph_copy_from_page_vector(pages, buf, 0, size);
Alex Elder23ed6e12013-02-06 13:11:38 -06002893 rbd_assert(size <= (size_t) INT_MAX);
2894 ret = (int) size;
Alex Elder788e2df2013-01-17 12:25:27 -06002895 if (version)
2896 *version = obj_request->version;
2897out:
2898 if (obj_request)
2899 rbd_obj_request_put(obj_request);
2900 else
2901 ceph_release_page_vector(pages, page_count);
2902
2903 return ret;
2904}
2905
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002906/*
Alex Elder4156d992012-08-02 11:29:46 -05002907 * Read the complete header for the given rbd device.
2908 *
2909 * Returns a pointer to a dynamically-allocated buffer containing
2910 * the complete and validated header. Caller can pass the address
2911 * of a variable that will be filled in with the version of the
2912 * header object at the time it was read.
2913 *
2914 * Returns a pointer-coded errno if a failure occurs.
2915 */
2916static struct rbd_image_header_ondisk *
2917rbd_dev_v1_header_read(struct rbd_device *rbd_dev, u64 *version)
2918{
2919 struct rbd_image_header_ondisk *ondisk = NULL;
2920 u32 snap_count = 0;
2921 u64 names_size = 0;
2922 u32 want_count;
2923 int ret;
2924
2925 /*
2926 * The complete header will include an array of its 64-bit
2927 * snapshot ids, followed by the names of those snapshots as
2928 * a contiguous block of NUL-terminated strings. Note that
2929 * the number of snapshots could change by the time we read
2930 * it in, in which case we re-read it.
2931 */
2932 do {
2933 size_t size;
2934
2935 kfree(ondisk);
2936
2937 size = sizeof (*ondisk);
2938 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
2939 size += names_size;
2940 ondisk = kmalloc(size, GFP_KERNEL);
2941 if (!ondisk)
2942 return ERR_PTR(-ENOMEM);
2943
Alex Elder788e2df2013-01-17 12:25:27 -06002944 ret = rbd_obj_read_sync(rbd_dev, rbd_dev->header_name,
Alex Elder80ef15b2013-04-21 12:14:45 -05002945 0, size, ondisk, version);
Alex Elder4156d992012-08-02 11:29:46 -05002946 if (ret < 0)
2947 goto out_err;
Alex Elderc0cd10db2013-04-26 09:43:47 -05002948 if ((size_t)ret < size) {
Alex Elder4156d992012-08-02 11:29:46 -05002949 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05002950 rbd_warn(rbd_dev, "short header read (want %zd got %d)",
2951 size, ret);
Alex Elder4156d992012-08-02 11:29:46 -05002952 goto out_err;
2953 }
2954 if (!rbd_dev_ondisk_valid(ondisk)) {
2955 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05002956 rbd_warn(rbd_dev, "invalid header");
Alex Elder4156d992012-08-02 11:29:46 -05002957 goto out_err;
2958 }
2959
2960 names_size = le64_to_cpu(ondisk->snap_names_len);
2961 want_count = snap_count;
2962 snap_count = le32_to_cpu(ondisk->snap_count);
2963 } while (snap_count != want_count);
2964
2965 return ondisk;
2966
2967out_err:
2968 kfree(ondisk);
2969
2970 return ERR_PTR(ret);
2971}
2972
2973/*
2974 * reload the ondisk the header
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002975 */
2976static int rbd_read_header(struct rbd_device *rbd_dev,
2977 struct rbd_image_header *header)
2978{
Alex Elder4156d992012-08-02 11:29:46 -05002979 struct rbd_image_header_ondisk *ondisk;
2980 u64 ver = 0;
2981 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002982
Alex Elder4156d992012-08-02 11:29:46 -05002983 ondisk = rbd_dev_v1_header_read(rbd_dev, &ver);
2984 if (IS_ERR(ondisk))
2985 return PTR_ERR(ondisk);
2986 ret = rbd_header_from_disk(header, ondisk);
Alex Elder4156d992012-08-02 11:29:46 -05002987 kfree(ondisk);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002988
Alex Elder4156d992012-08-02 11:29:46 -05002989 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002990}
2991
Alex Elder41f38c22012-10-25 23:34:40 -05002992static void rbd_remove_all_snaps(struct rbd_device *rbd_dev)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002993{
2994 struct rbd_snap *snap;
Alex Eldera0593292012-07-19 09:09:27 -05002995 struct rbd_snap *next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08002996
Alex Elder6087b512013-04-25 15:09:41 -05002997 list_for_each_entry_safe(snap, next, &rbd_dev->snaps, node) {
2998 list_del(&snap->node);
2999 rbd_snap_destroy(snap);
3000 }
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003001}
3002
Alex Elder94785542012-10-09 13:50:17 -07003003static void rbd_update_mapping_size(struct rbd_device *rbd_dev)
3004{
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003005 if (rbd_dev->spec->snap_id != CEPH_NOSNAP)
Alex Elder94785542012-10-09 13:50:17 -07003006 return;
3007
Alex Eldere28626a2013-04-26 15:44:35 -05003008 if (rbd_dev->mapping.size != rbd_dev->header.image_size) {
3009 sector_t size;
3010
3011 rbd_dev->mapping.size = rbd_dev->header.image_size;
3012 size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE;
3013 dout("setting size to %llu sectors", (unsigned long long)size);
3014 set_capacity(rbd_dev->disk, size);
3015 }
Alex Elder94785542012-10-09 13:50:17 -07003016}
3017
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003018/*
3019 * only read the first part of the ondisk header, without the snaps info
3020 */
Alex Elder117973f2012-08-31 17:29:55 -05003021static int rbd_dev_v1_refresh(struct rbd_device *rbd_dev, u64 *hver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003022{
3023 int ret;
3024 struct rbd_image_header h;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003025
3026 ret = rbd_read_header(rbd_dev, &h);
3027 if (ret < 0)
3028 return ret;
3029
Josh Durgina51aa0c2011-12-05 10:35:04 -08003030 down_write(&rbd_dev->header_rwsem);
3031
Alex Elder94785542012-10-09 13:50:17 -07003032 /* Update image size, and check for resize of mapped image */
3033 rbd_dev->header.image_size = h.image_size;
3034 rbd_update_mapping_size(rbd_dev);
Sage Weil9db4b3e2011-04-19 22:49:06 -07003035
Alex Elder849b4262012-07-09 21:04:24 -05003036 /* rbd_dev->header.object_prefix shouldn't change */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003037 kfree(rbd_dev->header.snap_sizes);
Alex Elder849b4262012-07-09 21:04:24 -05003038 kfree(rbd_dev->header.snap_names);
Josh Durgind1d25642011-12-05 14:03:05 -08003039 /* osd requests may still refer to snapc */
Alex Elder812164f82013-04-30 00:44:32 -05003040 ceph_put_snap_context(rbd_dev->header.snapc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003041
Josh Durgin93a24e02011-12-05 10:41:28 -08003042 rbd_dev->header.image_size = h.image_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003043 rbd_dev->header.snapc = h.snapc;
3044 rbd_dev->header.snap_names = h.snap_names;
3045 rbd_dev->header.snap_sizes = h.snap_sizes;
Alex Elder849b4262012-07-09 21:04:24 -05003046 /* Free the extra copy of the object prefix */
Alex Elderc0cd10db2013-04-26 09:43:47 -05003047 if (strcmp(rbd_dev->header.object_prefix, h.object_prefix))
3048 rbd_warn(rbd_dev, "object prefix changed (ignoring)");
Alex Elder849b4262012-07-09 21:04:24 -05003049 kfree(h.object_prefix);
3050
Alex Elder304f6802012-08-31 17:29:52 -05003051 ret = rbd_dev_snaps_update(rbd_dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003052
Josh Durginc6666012011-11-21 17:11:12 -08003053 up_write(&rbd_dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003054
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003055 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003056}
3057
Alex Elder117973f2012-08-31 17:29:55 -05003058static int rbd_dev_refresh(struct rbd_device *rbd_dev, u64 *hver)
Alex Elder1fe5e992012-07-25 09:32:41 -05003059{
Alex Eldera3fbe5d2013-04-30 00:44:32 -05003060 u64 image_size;
Alex Elder1fe5e992012-07-25 09:32:41 -05003061 int ret;
3062
Alex Elder117973f2012-08-31 17:29:55 -05003063 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
Alex Eldera3fbe5d2013-04-30 00:44:32 -05003064 image_size = rbd_dev->header.image_size;
Alex Elder1fe5e992012-07-25 09:32:41 -05003065 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Alex Elder117973f2012-08-31 17:29:55 -05003066 if (rbd_dev->image_format == 1)
3067 ret = rbd_dev_v1_refresh(rbd_dev, hver);
3068 else
3069 ret = rbd_dev_v2_refresh(rbd_dev, hver);
Alex Elder1fe5e992012-07-25 09:32:41 -05003070 mutex_unlock(&ctl_mutex);
Alex Elder522a0cc2013-04-25 15:09:41 -05003071 if (ret)
3072 rbd_warn(rbd_dev, "got notification but failed to "
3073 " update snaps: %d\n", ret);
Alex Eldera3fbe5d2013-04-30 00:44:32 -05003074 if (image_size != rbd_dev->header.image_size)
3075 revalidate_disk(rbd_dev->disk);
Alex Elder1fe5e992012-07-25 09:32:41 -05003076
3077 return ret;
3078}
3079
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003080static int rbd_init_disk(struct rbd_device *rbd_dev)
3081{
3082 struct gendisk *disk;
3083 struct request_queue *q;
Alex Elder593a9e72012-02-07 12:03:37 -06003084 u64 segment_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003085
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003086 /* create gendisk info */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003087 disk = alloc_disk(RBD_MINORS_PER_MAJOR);
3088 if (!disk)
Alex Elder1fcdb8a2012-08-29 17:11:06 -05003089 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003090
Alex Elderf0f8cef2012-01-29 13:57:44 -06003091 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
Alex Elderde71a292012-07-03 16:01:19 -05003092 rbd_dev->dev_id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003093 disk->major = rbd_dev->major;
3094 disk->first_minor = 0;
3095 disk->fops = &rbd_bd_ops;
3096 disk->private_data = rbd_dev;
3097
Alex Elderbf0d5f502012-11-22 00:00:08 -06003098 q = blk_init_queue(rbd_request_fn, &rbd_dev->lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003099 if (!q)
3100 goto out_disk;
Josh Durgin029bcbd2011-07-22 11:35:23 -07003101
Alex Elder593a9e72012-02-07 12:03:37 -06003102 /* We use the default size, but let's be explicit about it. */
3103 blk_queue_physical_block_size(q, SECTOR_SIZE);
3104
Josh Durgin029bcbd2011-07-22 11:35:23 -07003105 /* set io sizes to object size */
Alex Elder593a9e72012-02-07 12:03:37 -06003106 segment_size = rbd_obj_bytes(&rbd_dev->header);
3107 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
3108 blk_queue_max_segment_size(q, segment_size);
3109 blk_queue_io_min(q, segment_size);
3110 blk_queue_io_opt(q, segment_size);
Josh Durgin029bcbd2011-07-22 11:35:23 -07003111
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003112 blk_queue_merge_bvec(q, rbd_merge_bvec);
3113 disk->queue = q;
3114
3115 q->queuedata = rbd_dev;
3116
3117 rbd_dev->disk = disk;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003118
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003119 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003120out_disk:
3121 put_disk(disk);
Alex Elder1fcdb8a2012-08-29 17:11:06 -05003122
3123 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003124}
3125
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003126/*
3127 sysfs
3128*/
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003129
Alex Elder593a9e72012-02-07 12:03:37 -06003130static struct rbd_device *dev_to_rbd_dev(struct device *dev)
3131{
3132 return container_of(dev, struct rbd_device, dev);
3133}
3134
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003135static ssize_t rbd_size_show(struct device *dev,
3136 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003137{
Alex Elder593a9e72012-02-07 12:03:37 -06003138 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003139
Alex Elderfc71d832013-04-26 15:44:36 -05003140 return sprintf(buf, "%llu\n",
3141 (unsigned long long)rbd_dev->mapping.size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003142}
3143
Alex Elder34b13182012-07-13 20:35:12 -05003144/*
3145 * Note this shows the features for whatever's mapped, which is not
3146 * necessarily the base image.
3147 */
3148static ssize_t rbd_features_show(struct device *dev,
3149 struct device_attribute *attr, char *buf)
3150{
3151 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3152
3153 return sprintf(buf, "0x%016llx\n",
Alex Elderfc71d832013-04-26 15:44:36 -05003154 (unsigned long long)rbd_dev->mapping.features);
Alex Elder34b13182012-07-13 20:35:12 -05003155}
3156
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003157static ssize_t rbd_major_show(struct device *dev,
3158 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003159{
Alex Elder593a9e72012-02-07 12:03:37 -06003160 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003161
Alex Elderfc71d832013-04-26 15:44:36 -05003162 if (rbd_dev->major)
3163 return sprintf(buf, "%d\n", rbd_dev->major);
3164
3165 return sprintf(buf, "(none)\n");
3166
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003167}
3168
3169static ssize_t rbd_client_id_show(struct device *dev,
3170 struct device_attribute *attr, char *buf)
3171{
Alex Elder593a9e72012-02-07 12:03:37 -06003172 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003173
Alex Elder1dbb4392012-01-24 10:08:37 -06003174 return sprintf(buf, "client%lld\n",
3175 ceph_client_id(rbd_dev->rbd_client->client));
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003176}
3177
3178static ssize_t rbd_pool_show(struct device *dev,
3179 struct device_attribute *attr, char *buf)
3180{
Alex Elder593a9e72012-02-07 12:03:37 -06003181 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003182
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003183 return sprintf(buf, "%s\n", rbd_dev->spec->pool_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003184}
3185
Alex Elder9bb2f332012-07-12 10:46:35 -05003186static ssize_t rbd_pool_id_show(struct device *dev,
3187 struct device_attribute *attr, char *buf)
3188{
3189 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3190
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003191 return sprintf(buf, "%llu\n",
Alex Elderfc71d832013-04-26 15:44:36 -05003192 (unsigned long long) rbd_dev->spec->pool_id);
Alex Elder9bb2f332012-07-12 10:46:35 -05003193}
3194
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003195static ssize_t rbd_name_show(struct device *dev,
3196 struct device_attribute *attr, char *buf)
3197{
Alex Elder593a9e72012-02-07 12:03:37 -06003198 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003199
Alex Eldera92ffdf2012-10-30 19:40:33 -05003200 if (rbd_dev->spec->image_name)
3201 return sprintf(buf, "%s\n", rbd_dev->spec->image_name);
3202
3203 return sprintf(buf, "(unknown)\n");
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003204}
3205
Alex Elder589d30e2012-07-10 20:30:11 -05003206static ssize_t rbd_image_id_show(struct device *dev,
3207 struct device_attribute *attr, char *buf)
3208{
3209 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3210
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003211 return sprintf(buf, "%s\n", rbd_dev->spec->image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05003212}
3213
Alex Elder34b13182012-07-13 20:35:12 -05003214/*
3215 * Shows the name of the currently-mapped snapshot (or
3216 * RBD_SNAP_HEAD_NAME for the base image).
3217 */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003218static ssize_t rbd_snap_show(struct device *dev,
3219 struct device_attribute *attr,
3220 char *buf)
3221{
Alex Elder593a9e72012-02-07 12:03:37 -06003222 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003223
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003224 return sprintf(buf, "%s\n", rbd_dev->spec->snap_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003225}
3226
Alex Elder86b00e02012-10-25 23:34:42 -05003227/*
3228 * For an rbd v2 image, shows the pool id, image id, and snapshot id
3229 * for the parent image. If there is no parent, simply shows
3230 * "(no parent image)".
3231 */
3232static ssize_t rbd_parent_show(struct device *dev,
3233 struct device_attribute *attr,
3234 char *buf)
3235{
3236 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3237 struct rbd_spec *spec = rbd_dev->parent_spec;
3238 int count;
3239 char *bufp = buf;
3240
3241 if (!spec)
3242 return sprintf(buf, "(no parent image)\n");
3243
3244 count = sprintf(bufp, "pool_id %llu\npool_name %s\n",
3245 (unsigned long long) spec->pool_id, spec->pool_name);
3246 if (count < 0)
3247 return count;
3248 bufp += count;
3249
3250 count = sprintf(bufp, "image_id %s\nimage_name %s\n", spec->image_id,
3251 spec->image_name ? spec->image_name : "(unknown)");
3252 if (count < 0)
3253 return count;
3254 bufp += count;
3255
3256 count = sprintf(bufp, "snap_id %llu\nsnap_name %s\n",
3257 (unsigned long long) spec->snap_id, spec->snap_name);
3258 if (count < 0)
3259 return count;
3260 bufp += count;
3261
3262 count = sprintf(bufp, "overlap %llu\n", rbd_dev->parent_overlap);
3263 if (count < 0)
3264 return count;
3265 bufp += count;
3266
3267 return (ssize_t) (bufp - buf);
3268}
3269
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003270static ssize_t rbd_image_refresh(struct device *dev,
3271 struct device_attribute *attr,
3272 const char *buf,
3273 size_t size)
3274{
Alex Elder593a9e72012-02-07 12:03:37 -06003275 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Alex Elderb8136232012-07-25 09:32:41 -05003276 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003277
Alex Elder117973f2012-08-31 17:29:55 -05003278 ret = rbd_dev_refresh(rbd_dev, NULL);
Alex Elderb8136232012-07-25 09:32:41 -05003279
3280 return ret < 0 ? ret : size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003281}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003282
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003283static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
Alex Elder34b13182012-07-13 20:35:12 -05003284static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003285static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
3286static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
3287static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
Alex Elder9bb2f332012-07-12 10:46:35 -05003288static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003289static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
Alex Elder589d30e2012-07-10 20:30:11 -05003290static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003291static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
3292static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
Alex Elder86b00e02012-10-25 23:34:42 -05003293static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003294
3295static struct attribute *rbd_attrs[] = {
3296 &dev_attr_size.attr,
Alex Elder34b13182012-07-13 20:35:12 -05003297 &dev_attr_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003298 &dev_attr_major.attr,
3299 &dev_attr_client_id.attr,
3300 &dev_attr_pool.attr,
Alex Elder9bb2f332012-07-12 10:46:35 -05003301 &dev_attr_pool_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003302 &dev_attr_name.attr,
Alex Elder589d30e2012-07-10 20:30:11 -05003303 &dev_attr_image_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003304 &dev_attr_current_snap.attr,
Alex Elder86b00e02012-10-25 23:34:42 -05003305 &dev_attr_parent.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003306 &dev_attr_refresh.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003307 NULL
3308};
3309
3310static struct attribute_group rbd_attr_group = {
3311 .attrs = rbd_attrs,
3312};
3313
3314static const struct attribute_group *rbd_attr_groups[] = {
3315 &rbd_attr_group,
3316 NULL
3317};
3318
3319static void rbd_sysfs_dev_release(struct device *dev)
3320{
3321}
3322
3323static struct device_type rbd_device_type = {
3324 .name = "rbd",
3325 .groups = rbd_attr_groups,
3326 .release = rbd_sysfs_dev_release,
3327};
3328
Alex Elder8b8fb992012-10-26 17:25:24 -05003329static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)
3330{
3331 kref_get(&spec->kref);
3332
3333 return spec;
3334}
3335
3336static void rbd_spec_free(struct kref *kref);
3337static void rbd_spec_put(struct rbd_spec *spec)
3338{
3339 if (spec)
3340 kref_put(&spec->kref, rbd_spec_free);
3341}
3342
3343static struct rbd_spec *rbd_spec_alloc(void)
3344{
3345 struct rbd_spec *spec;
3346
3347 spec = kzalloc(sizeof (*spec), GFP_KERNEL);
3348 if (!spec)
3349 return NULL;
3350 kref_init(&spec->kref);
3351
Alex Elder8b8fb992012-10-26 17:25:24 -05003352 return spec;
3353}
3354
3355static void rbd_spec_free(struct kref *kref)
3356{
3357 struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref);
3358
3359 kfree(spec->pool_name);
3360 kfree(spec->image_id);
3361 kfree(spec->image_name);
3362 kfree(spec->snap_name);
3363 kfree(spec);
3364}
3365
Alex Eldercc344fa2013-02-19 12:25:56 -06003366static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,
Alex Elderc53d5892012-10-25 23:34:42 -05003367 struct rbd_spec *spec)
3368{
3369 struct rbd_device *rbd_dev;
3370
3371 rbd_dev = kzalloc(sizeof (*rbd_dev), GFP_KERNEL);
3372 if (!rbd_dev)
3373 return NULL;
3374
3375 spin_lock_init(&rbd_dev->lock);
Alex Elder6d292902013-01-14 12:43:31 -06003376 rbd_dev->flags = 0;
Alex Elderc53d5892012-10-25 23:34:42 -05003377 INIT_LIST_HEAD(&rbd_dev->node);
3378 INIT_LIST_HEAD(&rbd_dev->snaps);
3379 init_rwsem(&rbd_dev->header_rwsem);
3380
3381 rbd_dev->spec = spec;
3382 rbd_dev->rbd_client = rbdc;
3383
Alex Elder0903e872012-11-14 12:25:19 -06003384 /* Initialize the layout used for all rbd requests */
3385
3386 rbd_dev->layout.fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
3387 rbd_dev->layout.fl_stripe_count = cpu_to_le32(1);
3388 rbd_dev->layout.fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
3389 rbd_dev->layout.fl_pg_pool = cpu_to_le32((u32) spec->pool_id);
3390
Alex Elderc53d5892012-10-25 23:34:42 -05003391 return rbd_dev;
3392}
3393
3394static void rbd_dev_destroy(struct rbd_device *rbd_dev)
3395{
Alex Elderc53d5892012-10-25 23:34:42 -05003396 rbd_put_client(rbd_dev->rbd_client);
3397 rbd_spec_put(rbd_dev->spec);
3398 kfree(rbd_dev);
3399}
3400
Alex Elder6087b512013-04-25 15:09:41 -05003401static void rbd_snap_destroy(struct rbd_snap *snap)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003402{
Alex Elder3e83b652013-04-23 13:52:53 -05003403 kfree(snap->name);
3404 kfree(snap);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003405}
3406
Alex Elder6087b512013-04-25 15:09:41 -05003407static struct rbd_snap *rbd_snap_create(struct rbd_device *rbd_dev,
Alex Elderc8d18422012-07-10 20:30:11 -05003408 const char *snap_name,
Alex Elder34b13182012-07-13 20:35:12 -05003409 u64 snap_id, u64 snap_size,
3410 u64 snap_features)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003411{
Alex Elder4e891e02012-07-10 20:30:10 -05003412 struct rbd_snap *snap;
Alex Elder4e891e02012-07-10 20:30:10 -05003413
3414 snap = kzalloc(sizeof (*snap), GFP_KERNEL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003415 if (!snap)
Alex Elder4e891e02012-07-10 20:30:10 -05003416 return ERR_PTR(-ENOMEM);
3417
Alex Elder6e584f52013-04-25 15:09:42 -05003418 snap->name = snap_name;
Alex Elderc8d18422012-07-10 20:30:11 -05003419 snap->id = snap_id;
3420 snap->size = snap_size;
Alex Elder34b13182012-07-13 20:35:12 -05003421 snap->features = snap_features;
Alex Elder4e891e02012-07-10 20:30:10 -05003422
3423 return snap;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003424}
3425
Alex Elder6e584f52013-04-25 15:09:42 -05003426/*
3427 * Returns a dynamically-allocated snapshot name if successful, or a
3428 * pointer-coded error otherwise.
3429 */
Alex Eldercb752232013-04-30 00:44:33 -05003430static const char *rbd_dev_v1_snap_info(struct rbd_device *rbd_dev, u32 which,
Alex Eldercd892122012-07-03 16:01:19 -05003431 u64 *snap_size, u64 *snap_features)
3432{
Alex Eldercb752232013-04-30 00:44:33 -05003433 const char *snap_name;
Alex Elder6e584f52013-04-25 15:09:42 -05003434 int i;
Alex Eldercd892122012-07-03 16:01:19 -05003435
3436 rbd_assert(which < rbd_dev->header.snapc->num_snaps);
3437
Alex Eldercd892122012-07-03 16:01:19 -05003438 /* Skip over names until we find the one we are looking for */
3439
3440 snap_name = rbd_dev->header.snap_names;
Alex Elder6e584f52013-04-25 15:09:42 -05003441 for (i = 0; i < which; i++)
Alex Eldercd892122012-07-03 16:01:19 -05003442 snap_name += strlen(snap_name) + 1;
3443
Alex Elder6e584f52013-04-25 15:09:42 -05003444 snap_name = kstrdup(snap_name, GFP_KERNEL);
3445 if (!snap_name)
3446 return ERR_PTR(-ENOMEM);
3447
3448 *snap_size = rbd_dev->header.snap_sizes[which];
3449 *snap_features = 0; /* No features for v1 */
3450
Alex Eldercd892122012-07-03 16:01:19 -05003451 return snap_name;
3452}
3453
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003454/*
Alex Elder9d475de2012-07-03 16:01:19 -05003455 * Get the size and object order for an image snapshot, or if
3456 * snap_id is CEPH_NOSNAP, gets this information for the base
3457 * image.
3458 */
3459static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
3460 u8 *order, u64 *snap_size)
3461{
3462 __le64 snapid = cpu_to_le64(snap_id);
3463 int ret;
3464 struct {
3465 u8 order;
3466 __le64 size;
3467 } __attribute__ ((packed)) size_buf = { 0 };
3468
Alex Elder36be9a72013-01-19 00:30:28 -06003469 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elder9d475de2012-07-03 16:01:19 -05003470 "rbd", "get_size",
Alex Elder41579762013-04-21 12:14:45 -05003471 &snapid, sizeof (snapid),
3472 &size_buf, sizeof (size_buf), NULL);
Alex Elder36be9a72013-01-19 00:30:28 -06003473 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder9d475de2012-07-03 16:01:19 -05003474 if (ret < 0)
3475 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05003476 if (ret < sizeof (size_buf))
3477 return -ERANGE;
Alex Elder9d475de2012-07-03 16:01:19 -05003478
Alex Elderc86f86e2013-04-25 15:09:41 -05003479 if (order)
3480 *order = size_buf.order;
Alex Elder9d475de2012-07-03 16:01:19 -05003481 *snap_size = le64_to_cpu(size_buf.size);
3482
3483 dout(" snap_id 0x%016llx order = %u, snap_size = %llu\n",
Alex Elder57385b52013-04-21 12:14:45 -05003484 (unsigned long long)snap_id, (unsigned int)*order,
3485 (unsigned long long)*snap_size);
Alex Elder9d475de2012-07-03 16:01:19 -05003486
3487 return 0;
3488}
3489
3490static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
3491{
3492 return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
3493 &rbd_dev->header.obj_order,
3494 &rbd_dev->header.image_size);
3495}
3496
Alex Elder1e130192012-07-03 16:01:19 -05003497static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
3498{
3499 void *reply_buf;
3500 int ret;
3501 void *p;
3502
3503 reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL);
3504 if (!reply_buf)
3505 return -ENOMEM;
3506
Alex Elder36be9a72013-01-19 00:30:28 -06003507 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elder41579762013-04-21 12:14:45 -05003508 "rbd", "get_object_prefix", NULL, 0,
Alex Elder07b23912012-11-09 08:43:16 -06003509 reply_buf, RBD_OBJ_PREFIX_LEN_MAX, NULL);
Alex Elder36be9a72013-01-19 00:30:28 -06003510 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder1e130192012-07-03 16:01:19 -05003511 if (ret < 0)
3512 goto out;
3513
3514 p = reply_buf;
3515 rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p,
Alex Elder57385b52013-04-21 12:14:45 -05003516 p + ret, NULL, GFP_NOIO);
3517 ret = 0;
Alex Elder1e130192012-07-03 16:01:19 -05003518
3519 if (IS_ERR(rbd_dev->header.object_prefix)) {
3520 ret = PTR_ERR(rbd_dev->header.object_prefix);
3521 rbd_dev->header.object_prefix = NULL;
3522 } else {
3523 dout(" object_prefix = %s\n", rbd_dev->header.object_prefix);
3524 }
Alex Elder1e130192012-07-03 16:01:19 -05003525out:
3526 kfree(reply_buf);
3527
3528 return ret;
3529}
3530
Alex Elderb1b54022012-07-03 16:01:19 -05003531static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
3532 u64 *snap_features)
3533{
3534 __le64 snapid = cpu_to_le64(snap_id);
3535 struct {
3536 __le64 features;
3537 __le64 incompat;
Alex Elder41579762013-04-21 12:14:45 -05003538 } __attribute__ ((packed)) features_buf = { 0 };
Alex Elderd8891402012-10-09 13:50:17 -07003539 u64 incompat;
Alex Elderb1b54022012-07-03 16:01:19 -05003540 int ret;
3541
Alex Elder36be9a72013-01-19 00:30:28 -06003542 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elderb1b54022012-07-03 16:01:19 -05003543 "rbd", "get_features",
Alex Elder41579762013-04-21 12:14:45 -05003544 &snapid, sizeof (snapid),
3545 &features_buf, sizeof (features_buf), NULL);
Alex Elder36be9a72013-01-19 00:30:28 -06003546 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderb1b54022012-07-03 16:01:19 -05003547 if (ret < 0)
3548 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05003549 if (ret < sizeof (features_buf))
3550 return -ERANGE;
Alex Elderd8891402012-10-09 13:50:17 -07003551
3552 incompat = le64_to_cpu(features_buf.incompat);
Alex Elder5cbf6f122013-04-11 09:29:48 -05003553 if (incompat & ~RBD_FEATURES_SUPPORTED)
Alex Elderb8f5c6e2012-11-01 08:39:26 -05003554 return -ENXIO;
Alex Elderd8891402012-10-09 13:50:17 -07003555
Alex Elderb1b54022012-07-03 16:01:19 -05003556 *snap_features = le64_to_cpu(features_buf.features);
3557
3558 dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
Alex Elder57385b52013-04-21 12:14:45 -05003559 (unsigned long long)snap_id,
3560 (unsigned long long)*snap_features,
3561 (unsigned long long)le64_to_cpu(features_buf.incompat));
Alex Elderb1b54022012-07-03 16:01:19 -05003562
3563 return 0;
3564}
3565
3566static int rbd_dev_v2_features(struct rbd_device *rbd_dev)
3567{
3568 return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP,
3569 &rbd_dev->header.features);
3570}
3571
Alex Elder86b00e02012-10-25 23:34:42 -05003572static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev)
3573{
3574 struct rbd_spec *parent_spec;
3575 size_t size;
3576 void *reply_buf = NULL;
3577 __le64 snapid;
3578 void *p;
3579 void *end;
3580 char *image_id;
3581 u64 overlap;
Alex Elder86b00e02012-10-25 23:34:42 -05003582 int ret;
3583
3584 parent_spec = rbd_spec_alloc();
3585 if (!parent_spec)
3586 return -ENOMEM;
3587
3588 size = sizeof (__le64) + /* pool_id */
3589 sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */
3590 sizeof (__le64) + /* snap_id */
3591 sizeof (__le64); /* overlap */
3592 reply_buf = kmalloc(size, GFP_KERNEL);
3593 if (!reply_buf) {
3594 ret = -ENOMEM;
3595 goto out_err;
3596 }
3597
3598 snapid = cpu_to_le64(CEPH_NOSNAP);
Alex Elder36be9a72013-01-19 00:30:28 -06003599 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elder86b00e02012-10-25 23:34:42 -05003600 "rbd", "get_parent",
Alex Elder41579762013-04-21 12:14:45 -05003601 &snapid, sizeof (snapid),
3602 reply_buf, size, NULL);
Alex Elder36be9a72013-01-19 00:30:28 -06003603 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder86b00e02012-10-25 23:34:42 -05003604 if (ret < 0)
3605 goto out_err;
3606
Alex Elder86b00e02012-10-25 23:34:42 -05003607 p = reply_buf;
Alex Elder57385b52013-04-21 12:14:45 -05003608 end = reply_buf + ret;
3609 ret = -ERANGE;
Alex Elder86b00e02012-10-25 23:34:42 -05003610 ceph_decode_64_safe(&p, end, parent_spec->pool_id, out_err);
3611 if (parent_spec->pool_id == CEPH_NOPOOL)
3612 goto out; /* No parent? No problem. */
3613
Alex Elder0903e872012-11-14 12:25:19 -06003614 /* The ceph file layout needs to fit pool id in 32 bits */
3615
3616 ret = -EIO;
Alex Elderc0cd10db2013-04-26 09:43:47 -05003617 if (parent_spec->pool_id > (u64)U32_MAX) {
3618 rbd_warn(NULL, "parent pool id too large (%llu > %u)\n",
3619 (unsigned long long)parent_spec->pool_id, U32_MAX);
Alex Elder57385b52013-04-21 12:14:45 -05003620 goto out_err;
Alex Elderc0cd10db2013-04-26 09:43:47 -05003621 }
Alex Elder0903e872012-11-14 12:25:19 -06003622
Alex Elder979ed482012-11-01 08:39:26 -05003623 image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elder86b00e02012-10-25 23:34:42 -05003624 if (IS_ERR(image_id)) {
3625 ret = PTR_ERR(image_id);
3626 goto out_err;
3627 }
3628 parent_spec->image_id = image_id;
3629 ceph_decode_64_safe(&p, end, parent_spec->snap_id, out_err);
3630 ceph_decode_64_safe(&p, end, overlap, out_err);
3631
3632 rbd_dev->parent_overlap = overlap;
3633 rbd_dev->parent_spec = parent_spec;
3634 parent_spec = NULL; /* rbd_dev now owns this */
3635out:
3636 ret = 0;
3637out_err:
3638 kfree(reply_buf);
3639 rbd_spec_put(parent_spec);
3640
3641 return ret;
3642}
3643
Alex Eldercc070d52013-04-21 12:14:45 -05003644static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev)
3645{
3646 struct {
3647 __le64 stripe_unit;
3648 __le64 stripe_count;
3649 } __attribute__ ((packed)) striping_info_buf = { 0 };
3650 size_t size = sizeof (striping_info_buf);
3651 void *p;
3652 u64 obj_size;
3653 u64 stripe_unit;
3654 u64 stripe_count;
3655 int ret;
3656
3657 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
3658 "rbd", "get_stripe_unit_count", NULL, 0,
3659 (char *)&striping_info_buf, size, NULL);
3660 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
3661 if (ret < 0)
3662 return ret;
3663 if (ret < size)
3664 return -ERANGE;
3665
3666 /*
3667 * We don't actually support the "fancy striping" feature
3668 * (STRIPINGV2) yet, but if the striping sizes are the
3669 * defaults the behavior is the same as before. So find
3670 * out, and only fail if the image has non-default values.
3671 */
3672 ret = -EINVAL;
3673 obj_size = (u64)1 << rbd_dev->header.obj_order;
3674 p = &striping_info_buf;
3675 stripe_unit = ceph_decode_64(&p);
3676 if (stripe_unit != obj_size) {
3677 rbd_warn(rbd_dev, "unsupported stripe unit "
3678 "(got %llu want %llu)",
3679 stripe_unit, obj_size);
3680 return -EINVAL;
3681 }
3682 stripe_count = ceph_decode_64(&p);
3683 if (stripe_count != 1) {
3684 rbd_warn(rbd_dev, "unsupported stripe count "
3685 "(got %llu want 1)", stripe_count);
3686 return -EINVAL;
3687 }
Alex Elder500d0c02013-04-26 09:43:47 -05003688 rbd_dev->header.stripe_unit = stripe_unit;
3689 rbd_dev->header.stripe_count = stripe_count;
Alex Eldercc070d52013-04-21 12:14:45 -05003690
3691 return 0;
3692}
3693
Alex Elder9e15b772012-10-30 19:40:33 -05003694static char *rbd_dev_image_name(struct rbd_device *rbd_dev)
3695{
3696 size_t image_id_size;
3697 char *image_id;
3698 void *p;
3699 void *end;
3700 size_t size;
3701 void *reply_buf = NULL;
3702 size_t len = 0;
3703 char *image_name = NULL;
3704 int ret;
3705
3706 rbd_assert(!rbd_dev->spec->image_name);
3707
Alex Elder69e7a022012-11-01 08:39:26 -05003708 len = strlen(rbd_dev->spec->image_id);
3709 image_id_size = sizeof (__le32) + len;
Alex Elder9e15b772012-10-30 19:40:33 -05003710 image_id = kmalloc(image_id_size, GFP_KERNEL);
3711 if (!image_id)
3712 return NULL;
3713
3714 p = image_id;
Alex Elder41579762013-04-21 12:14:45 -05003715 end = image_id + image_id_size;
Alex Elder57385b52013-04-21 12:14:45 -05003716 ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len);
Alex Elder9e15b772012-10-30 19:40:33 -05003717
3718 size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX;
3719 reply_buf = kmalloc(size, GFP_KERNEL);
3720 if (!reply_buf)
3721 goto out;
3722
Alex Elder36be9a72013-01-19 00:30:28 -06003723 ret = rbd_obj_method_sync(rbd_dev, RBD_DIRECTORY,
Alex Elder9e15b772012-10-30 19:40:33 -05003724 "rbd", "dir_get_name",
3725 image_id, image_id_size,
Alex Elder41579762013-04-21 12:14:45 -05003726 reply_buf, size, NULL);
Alex Elder9e15b772012-10-30 19:40:33 -05003727 if (ret < 0)
3728 goto out;
3729 p = reply_buf;
Alex Elderf40eb342013-04-25 15:09:42 -05003730 end = reply_buf + ret;
3731
Alex Elder9e15b772012-10-30 19:40:33 -05003732 image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL);
3733 if (IS_ERR(image_name))
3734 image_name = NULL;
3735 else
3736 dout("%s: name is %s len is %zd\n", __func__, image_name, len);
3737out:
3738 kfree(reply_buf);
3739 kfree(image_id);
3740
3741 return image_name;
3742}
3743
3744/*
Alex Elder2e9f7f12013-04-26 09:43:48 -05003745 * When an rbd image has a parent image, it is identified by the
3746 * pool, image, and snapshot ids (not names). This function fills
3747 * in the names for those ids. (It's OK if we can't figure out the
3748 * name for an image id, but the pool and snapshot ids should always
3749 * exist and have names.) All names in an rbd spec are dynamically
3750 * allocated.
Alex Eldere1d42132013-04-25 23:15:08 -05003751 *
3752 * When an image being mapped (not a parent) is probed, we have the
3753 * pool name and pool id, image name and image id, and the snapshot
3754 * name. The only thing we're missing is the snapshot id.
Alex Elder2e9f7f12013-04-26 09:43:48 -05003755 *
3756 * The set of snapshots for an image is not known until they have
3757 * been read by rbd_dev_snaps_update(), so we can't completely fill
3758 * in this information until after that has been called.
Alex Elder9e15b772012-10-30 19:40:33 -05003759 */
Alex Elder2e9f7f12013-04-26 09:43:48 -05003760static int rbd_dev_spec_update(struct rbd_device *rbd_dev)
Alex Elder9e15b772012-10-30 19:40:33 -05003761{
Alex Elder2e9f7f12013-04-26 09:43:48 -05003762 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3763 struct rbd_spec *spec = rbd_dev->spec;
3764 const char *pool_name;
3765 const char *image_name;
3766 const char *snap_name;
Alex Elder9e15b772012-10-30 19:40:33 -05003767 int ret;
3768
Alex Eldere1d42132013-04-25 23:15:08 -05003769 /*
3770 * An image being mapped will have the pool name (etc.), but
3771 * we need to look up the snapshot id.
3772 */
Alex Elder2e9f7f12013-04-26 09:43:48 -05003773 if (spec->pool_name) {
3774 if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) {
Alex Eldere1d42132013-04-25 23:15:08 -05003775 struct rbd_snap *snap;
3776
Alex Elder2e9f7f12013-04-26 09:43:48 -05003777 snap = snap_by_name(rbd_dev, spec->snap_name);
Alex Eldere1d42132013-04-25 23:15:08 -05003778 if (!snap)
3779 return -ENOENT;
Alex Elder2e9f7f12013-04-26 09:43:48 -05003780 spec->snap_id = snap->id;
Alex Eldere1d42132013-04-25 23:15:08 -05003781 } else {
Alex Elder2e9f7f12013-04-26 09:43:48 -05003782 spec->snap_id = CEPH_NOSNAP;
Alex Eldere1d42132013-04-25 23:15:08 -05003783 }
3784
3785 return 0;
3786 }
Alex Elder9e15b772012-10-30 19:40:33 -05003787
Alex Elder2e9f7f12013-04-26 09:43:48 -05003788 /* Get the pool name; we have to make our own copy of this */
Alex Elder9e15b772012-10-30 19:40:33 -05003789
Alex Elder2e9f7f12013-04-26 09:43:48 -05003790 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id);
3791 if (!pool_name) {
3792 rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id);
Alex Elder935dc892012-11-01 10:17:15 -05003793 return -EIO;
3794 }
Alex Elder2e9f7f12013-04-26 09:43:48 -05003795 pool_name = kstrdup(pool_name, GFP_KERNEL);
3796 if (!pool_name)
Alex Elder9e15b772012-10-30 19:40:33 -05003797 return -ENOMEM;
3798
3799 /* Fetch the image name; tolerate failure here */
3800
Alex Elder2e9f7f12013-04-26 09:43:48 -05003801 image_name = rbd_dev_image_name(rbd_dev);
3802 if (!image_name)
Alex Elder06ecc6c2012-11-01 10:17:15 -05003803 rbd_warn(rbd_dev, "unable to get image name");
Alex Elder9e15b772012-10-30 19:40:33 -05003804
Alex Elder2e9f7f12013-04-26 09:43:48 -05003805 /* Look up the snapshot name, and make a copy */
Alex Elder9e15b772012-10-30 19:40:33 -05003806
Alex Elder2e9f7f12013-04-26 09:43:48 -05003807 snap_name = rbd_snap_name(rbd_dev, spec->snap_id);
3808 if (!snap_name) {
3809 rbd_warn(rbd_dev, "no snapshot with id %llu", spec->snap_id);
Alex Elder9e15b772012-10-30 19:40:33 -05003810 ret = -EIO;
3811 goto out_err;
3812 }
Alex Elder2e9f7f12013-04-26 09:43:48 -05003813 snap_name = kstrdup(snap_name, GFP_KERNEL);
3814 if (!snap_name) {
3815 ret = -ENOMEM;
Alex Elder9e15b772012-10-30 19:40:33 -05003816 goto out_err;
Alex Elder2e9f7f12013-04-26 09:43:48 -05003817 }
3818
3819 spec->pool_name = pool_name;
3820 spec->image_name = image_name;
3821 spec->snap_name = snap_name;
Alex Elder9e15b772012-10-30 19:40:33 -05003822
3823 return 0;
3824out_err:
Alex Elder2e9f7f12013-04-26 09:43:48 -05003825 kfree(image_name);
3826 kfree(pool_name);
Alex Elder9e15b772012-10-30 19:40:33 -05003827
3828 return ret;
3829}
3830
Alex Elder6e14b1a2012-07-03 16:01:19 -05003831static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev, u64 *ver)
Alex Elder35d489f2012-07-03 16:01:19 -05003832{
3833 size_t size;
3834 int ret;
3835 void *reply_buf;
3836 void *p;
3837 void *end;
3838 u64 seq;
3839 u32 snap_count;
3840 struct ceph_snap_context *snapc;
3841 u32 i;
3842
3843 /*
3844 * We'll need room for the seq value (maximum snapshot id),
3845 * snapshot count, and array of that many snapshot ids.
3846 * For now we have a fixed upper limit on the number we're
3847 * prepared to receive.
3848 */
3849 size = sizeof (__le64) + sizeof (__le32) +
3850 RBD_MAX_SNAP_COUNT * sizeof (__le64);
3851 reply_buf = kzalloc(size, GFP_KERNEL);
3852 if (!reply_buf)
3853 return -ENOMEM;
3854
Alex Elder36be9a72013-01-19 00:30:28 -06003855 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elder41579762013-04-21 12:14:45 -05003856 "rbd", "get_snapcontext", NULL, 0,
Alex Elder07b23912012-11-09 08:43:16 -06003857 reply_buf, size, ver);
Alex Elder36be9a72013-01-19 00:30:28 -06003858 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder35d489f2012-07-03 16:01:19 -05003859 if (ret < 0)
3860 goto out;
3861
Alex Elder35d489f2012-07-03 16:01:19 -05003862 p = reply_buf;
Alex Elder57385b52013-04-21 12:14:45 -05003863 end = reply_buf + ret;
3864 ret = -ERANGE;
Alex Elder35d489f2012-07-03 16:01:19 -05003865 ceph_decode_64_safe(&p, end, seq, out);
3866 ceph_decode_32_safe(&p, end, snap_count, out);
3867
3868 /*
3869 * Make sure the reported number of snapshot ids wouldn't go
3870 * beyond the end of our buffer. But before checking that,
3871 * make sure the computed size of the snapshot context we
3872 * allocate is representable in a size_t.
3873 */
3874 if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
3875 / sizeof (u64)) {
3876 ret = -EINVAL;
3877 goto out;
3878 }
3879 if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
3880 goto out;
Alex Elder468521c2013-04-26 09:43:47 -05003881 ret = 0;
Alex Elder35d489f2012-07-03 16:01:19 -05003882
Alex Elder812164f82013-04-30 00:44:32 -05003883 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
Alex Elder35d489f2012-07-03 16:01:19 -05003884 if (!snapc) {
3885 ret = -ENOMEM;
3886 goto out;
3887 }
Alex Elder35d489f2012-07-03 16:01:19 -05003888 snapc->seq = seq;
Alex Elder35d489f2012-07-03 16:01:19 -05003889 for (i = 0; i < snap_count; i++)
3890 snapc->snaps[i] = ceph_decode_64(&p);
3891
3892 rbd_dev->header.snapc = snapc;
3893
3894 dout(" snap context seq = %llu, snap_count = %u\n",
Alex Elder57385b52013-04-21 12:14:45 -05003895 (unsigned long long)seq, (unsigned int)snap_count);
Alex Elder35d489f2012-07-03 16:01:19 -05003896out:
3897 kfree(reply_buf);
3898
Alex Elder57385b52013-04-21 12:14:45 -05003899 return ret;
Alex Elder35d489f2012-07-03 16:01:19 -05003900}
3901
Alex Eldercb752232013-04-30 00:44:33 -05003902static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, u32 which)
Alex Elderb8b1e2d2012-07-03 16:01:19 -05003903{
3904 size_t size;
3905 void *reply_buf;
3906 __le64 snap_id;
3907 int ret;
3908 void *p;
3909 void *end;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05003910 char *snap_name;
3911
3912 size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN;
3913 reply_buf = kmalloc(size, GFP_KERNEL);
3914 if (!reply_buf)
3915 return ERR_PTR(-ENOMEM);
3916
Alex Elderacb1b6c2013-04-25 15:09:41 -05003917 rbd_assert(which < rbd_dev->header.snapc->num_snaps);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05003918 snap_id = cpu_to_le64(rbd_dev->header.snapc->snaps[which]);
Alex Elder36be9a72013-01-19 00:30:28 -06003919 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elderb8b1e2d2012-07-03 16:01:19 -05003920 "rbd", "get_snapshot_name",
Alex Elder41579762013-04-21 12:14:45 -05003921 &snap_id, sizeof (snap_id),
Alex Elder07b23912012-11-09 08:43:16 -06003922 reply_buf, size, NULL);
Alex Elder36be9a72013-01-19 00:30:28 -06003923 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderf40eb342013-04-25 15:09:42 -05003924 if (ret < 0) {
3925 snap_name = ERR_PTR(ret);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05003926 goto out;
Alex Elderf40eb342013-04-25 15:09:42 -05003927 }
Alex Elderb8b1e2d2012-07-03 16:01:19 -05003928
3929 p = reply_buf;
Alex Elderf40eb342013-04-25 15:09:42 -05003930 end = reply_buf + ret;
Alex Eldere5c35532012-10-25 23:34:41 -05003931 snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elderf40eb342013-04-25 15:09:42 -05003932 if (IS_ERR(snap_name))
Alex Elderb8b1e2d2012-07-03 16:01:19 -05003933 goto out;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05003934
Alex Elderf40eb342013-04-25 15:09:42 -05003935 dout(" snap_id 0x%016llx snap_name = %s\n",
3936 (unsigned long long)le64_to_cpu(snap_id), snap_name);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05003937out:
3938 kfree(reply_buf);
3939
Alex Elderf40eb342013-04-25 15:09:42 -05003940 return snap_name;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05003941}
3942
Alex Eldercb752232013-04-30 00:44:33 -05003943static const char *rbd_dev_v2_snap_info(struct rbd_device *rbd_dev, u32 which,
Alex Elderb8b1e2d2012-07-03 16:01:19 -05003944 u64 *snap_size, u64 *snap_features)
3945{
Alex Eldere0b49862013-01-09 14:44:18 -06003946 u64 snap_id;
Alex Elderacb1b6c2013-04-25 15:09:41 -05003947 u64 size;
3948 u64 features;
Alex Eldercb752232013-04-30 00:44:33 -05003949 const char *snap_name;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05003950 int ret;
3951
Alex Elderacb1b6c2013-04-25 15:09:41 -05003952 rbd_assert(which < rbd_dev->header.snapc->num_snaps);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05003953 snap_id = rbd_dev->header.snapc->snaps[which];
Alex Elderacb1b6c2013-04-25 15:09:41 -05003954 ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05003955 if (ret)
Alex Elderacb1b6c2013-04-25 15:09:41 -05003956 goto out_err;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05003957
Alex Elderacb1b6c2013-04-25 15:09:41 -05003958 ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, &features);
3959 if (ret)
3960 goto out_err;
3961
3962 snap_name = rbd_dev_v2_snap_name(rbd_dev, which);
3963 if (!IS_ERR(snap_name)) {
3964 *snap_size = size;
3965 *snap_features = features;
3966 }
3967
3968 return snap_name;
3969out_err:
3970 return ERR_PTR(ret);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05003971}
3972
Alex Eldercb752232013-04-30 00:44:33 -05003973static const char *rbd_dev_snap_info(struct rbd_device *rbd_dev, u32 which,
Alex Elderb8b1e2d2012-07-03 16:01:19 -05003974 u64 *snap_size, u64 *snap_features)
3975{
3976 if (rbd_dev->image_format == 1)
3977 return rbd_dev_v1_snap_info(rbd_dev, which,
3978 snap_size, snap_features);
3979 if (rbd_dev->image_format == 2)
3980 return rbd_dev_v2_snap_info(rbd_dev, which,
3981 snap_size, snap_features);
3982 return ERR_PTR(-EINVAL);
3983}
3984
Alex Elder117973f2012-08-31 17:29:55 -05003985static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev, u64 *hver)
3986{
3987 int ret;
Alex Elder117973f2012-08-31 17:29:55 -05003988
3989 down_write(&rbd_dev->header_rwsem);
3990
Alex Elder117973f2012-08-31 17:29:55 -05003991 ret = rbd_dev_v2_image_size(rbd_dev);
3992 if (ret)
3993 goto out;
Alex Elder117973f2012-08-31 17:29:55 -05003994 rbd_update_mapping_size(rbd_dev);
3995
3996 ret = rbd_dev_v2_snap_context(rbd_dev, hver);
3997 dout("rbd_dev_v2_snap_context returned %d\n", ret);
3998 if (ret)
3999 goto out;
4000 ret = rbd_dev_snaps_update(rbd_dev);
4001 dout("rbd_dev_snaps_update returned %d\n", ret);
4002 if (ret)
4003 goto out;
Alex Elder117973f2012-08-31 17:29:55 -05004004out:
4005 up_write(&rbd_dev->header_rwsem);
4006
4007 return ret;
4008}
4009
Alex Elder9d475de2012-07-03 16:01:19 -05004010/*
Alex Elder35938152012-08-02 11:29:46 -05004011 * Scan the rbd device's current snapshot list and compare it to the
4012 * newly-received snapshot context. Remove any existing snapshots
4013 * not present in the new snapshot context. Add a new snapshot for
4014 * any snaphots in the snapshot context not in the current list.
4015 * And verify there are no changes to snapshots we already know
4016 * about.
4017 *
4018 * Assumes the snapshots in the snapshot context are sorted by
4019 * snapshot id, highest id first. (Snapshots in the rbd_dev's list
4020 * are also maintained in that order.)
Alex Elder522a0cc2013-04-25 15:09:41 -05004021 *
4022 * Note that any error occurs while updating the snapshot list
4023 * aborts the update, and the entire list is cleared. The snapshot
4024 * list becomes inconsistent at that point anyway, so it might as
4025 * well be empty.
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004026 */
Alex Elder304f6802012-08-31 17:29:52 -05004027static int rbd_dev_snaps_update(struct rbd_device *rbd_dev)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004028{
Alex Elder35938152012-08-02 11:29:46 -05004029 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
4030 const u32 snap_count = snapc->num_snaps;
Alex Elder35938152012-08-02 11:29:46 -05004031 struct list_head *head = &rbd_dev->snaps;
4032 struct list_head *links = head->next;
4033 u32 index = 0;
Alex Elder522a0cc2013-04-25 15:09:41 -05004034 int ret = 0;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004035
Alex Elder522a0cc2013-04-25 15:09:41 -05004036 dout("%s: snap count is %u\n", __func__, (unsigned int)snap_count);
Alex Elder35938152012-08-02 11:29:46 -05004037 while (index < snap_count || links != head) {
4038 u64 snap_id;
4039 struct rbd_snap *snap;
Alex Eldercb752232013-04-30 00:44:33 -05004040 const char *snap_name;
Alex Eldercd892122012-07-03 16:01:19 -05004041 u64 snap_size = 0;
4042 u64 snap_features = 0;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004043
Alex Elder35938152012-08-02 11:29:46 -05004044 snap_id = index < snap_count ? snapc->snaps[index]
4045 : CEPH_NOSNAP;
4046 snap = links != head ? list_entry(links, struct rbd_snap, node)
4047 : NULL;
Alex Elderaafb2302012-09-06 16:00:54 -05004048 rbd_assert(!snap || snap->id != CEPH_NOSNAP);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004049
Alex Elder35938152012-08-02 11:29:46 -05004050 if (snap_id == CEPH_NOSNAP || (snap && snap->id > snap_id)) {
4051 struct list_head *next = links->next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004052
Alex Elder6d292902013-01-14 12:43:31 -06004053 /*
4054 * A previously-existing snapshot is not in
4055 * the new snap context.
4056 *
Alex Elder522a0cc2013-04-25 15:09:41 -05004057 * If the now-missing snapshot is the one
4058 * the image represents, clear its existence
4059 * flag so we can avoid sending any more
4060 * requests to it.
Alex Elder6d292902013-01-14 12:43:31 -06004061 */
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004062 if (rbd_dev->spec->snap_id == snap->id)
Alex Elder6d292902013-01-14 12:43:31 -06004063 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
Alex Elder3e83b652013-04-23 13:52:53 -05004064 dout("removing %ssnap id %llu\n",
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004065 rbd_dev->spec->snap_id == snap->id ?
4066 "mapped " : "",
Alex Elder522a0cc2013-04-25 15:09:41 -05004067 (unsigned long long)snap->id);
Alex Elder6087b512013-04-25 15:09:41 -05004068
4069 list_del(&snap->node);
4070 rbd_snap_destroy(snap);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004071
Alex Elder35938152012-08-02 11:29:46 -05004072 /* Done with this list entry; advance */
4073
4074 links = next;
4075 continue;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004076 }
Alex Elder35938152012-08-02 11:29:46 -05004077
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004078 snap_name = rbd_dev_snap_info(rbd_dev, index,
4079 &snap_size, &snap_features);
Alex Elder522a0cc2013-04-25 15:09:41 -05004080 if (IS_ERR(snap_name)) {
4081 ret = PTR_ERR(snap_name);
4082 dout("failed to get snap info, error %d\n", ret);
4083 goto out_err;
4084 }
Alex Eldercd892122012-07-03 16:01:19 -05004085
Alex Elder522a0cc2013-04-25 15:09:41 -05004086 dout("entry %u: snap_id = %llu\n", (unsigned int)snap_count,
4087 (unsigned long long)snap_id);
Alex Elder35938152012-08-02 11:29:46 -05004088 if (!snap || (snap_id != CEPH_NOSNAP && snap->id < snap_id)) {
4089 struct rbd_snap *new_snap;
4090
4091 /* We haven't seen this snapshot before */
4092
Alex Elder6087b512013-04-25 15:09:41 -05004093 new_snap = rbd_snap_create(rbd_dev, snap_name,
Alex Eldercd892122012-07-03 16:01:19 -05004094 snap_id, snap_size, snap_features);
Alex Elder9fcbb802012-08-23 23:48:49 -05004095 if (IS_ERR(new_snap)) {
Alex Elder522a0cc2013-04-25 15:09:41 -05004096 ret = PTR_ERR(new_snap);
4097 dout(" failed to add dev, error %d\n", ret);
4098 goto out_err;
Alex Elder9fcbb802012-08-23 23:48:49 -05004099 }
Alex Elder35938152012-08-02 11:29:46 -05004100
4101 /* New goes before existing, or at end of list */
4102
Alex Elder9fcbb802012-08-23 23:48:49 -05004103 dout(" added dev%s\n", snap ? "" : " at end\n");
Alex Elder35938152012-08-02 11:29:46 -05004104 if (snap)
4105 list_add_tail(&new_snap->node, &snap->node);
4106 else
Alex Elder523f3252012-08-30 00:16:37 -05004107 list_add_tail(&new_snap->node, head);
Alex Elder35938152012-08-02 11:29:46 -05004108 } else {
4109 /* Already have this one */
4110
Alex Elder9fcbb802012-08-23 23:48:49 -05004111 dout(" already present\n");
4112
Alex Eldercd892122012-07-03 16:01:19 -05004113 rbd_assert(snap->size == snap_size);
Alex Elderaafb2302012-09-06 16:00:54 -05004114 rbd_assert(!strcmp(snap->name, snap_name));
Alex Eldercd892122012-07-03 16:01:19 -05004115 rbd_assert(snap->features == snap_features);
Alex Elder35938152012-08-02 11:29:46 -05004116
4117 /* Done with this list entry; advance */
4118
4119 links = links->next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004120 }
Alex Elder35938152012-08-02 11:29:46 -05004121
4122 /* Advance to the next entry in the snapshot context */
4123
4124 index++;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004125 }
Alex Elder9fcbb802012-08-23 23:48:49 -05004126 dout("%s: done\n", __func__);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004127
4128 return 0;
Alex Elder522a0cc2013-04-25 15:09:41 -05004129out_err:
4130 rbd_remove_all_snaps(rbd_dev);
4131
4132 return ret;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004133}
4134
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004135static int rbd_bus_add_dev(struct rbd_device *rbd_dev)
4136{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004137 struct device *dev;
Alex Eldercd789ab2012-08-30 00:16:38 -05004138 int ret;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004139
4140 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004141
Alex Eldercd789ab2012-08-30 00:16:38 -05004142 dev = &rbd_dev->dev;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004143 dev->bus = &rbd_bus_type;
4144 dev->type = &rbd_device_type;
4145 dev->parent = &rbd_root_dev;
Alex Elder200a6a82013-04-28 23:32:34 -05004146 dev->release = rbd_dev_device_release;
Alex Elderde71a292012-07-03 16:01:19 -05004147 dev_set_name(dev, "%d", rbd_dev->dev_id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004148 ret = device_register(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004149
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004150 mutex_unlock(&ctl_mutex);
Alex Eldercd789ab2012-08-30 00:16:38 -05004151
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004152 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004153}
4154
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004155static void rbd_bus_del_dev(struct rbd_device *rbd_dev)
4156{
4157 device_unregister(&rbd_dev->dev);
4158}
4159
Alex Eldere2839302012-08-29 17:11:06 -05004160static atomic64_t rbd_dev_id_max = ATOMIC64_INIT(0);
Alex Elder1ddbe942012-01-29 13:57:44 -06004161
4162/*
Alex Elder499afd52012-02-02 08:13:29 -06004163 * Get a unique rbd identifier for the given new rbd_dev, and add
4164 * the rbd_dev to the global list. The minimum rbd id is 1.
Alex Elder1ddbe942012-01-29 13:57:44 -06004165 */
Alex Eldere2839302012-08-29 17:11:06 -05004166static void rbd_dev_id_get(struct rbd_device *rbd_dev)
Alex Elderb7f23c32012-01-29 13:57:43 -06004167{
Alex Eldere2839302012-08-29 17:11:06 -05004168 rbd_dev->dev_id = atomic64_inc_return(&rbd_dev_id_max);
Alex Elder499afd52012-02-02 08:13:29 -06004169
4170 spin_lock(&rbd_dev_list_lock);
4171 list_add_tail(&rbd_dev->node, &rbd_dev_list);
4172 spin_unlock(&rbd_dev_list_lock);
Alex Eldere2839302012-08-29 17:11:06 -05004173 dout("rbd_dev %p given dev id %llu\n", rbd_dev,
4174 (unsigned long long) rbd_dev->dev_id);
Alex Elder1ddbe942012-01-29 13:57:44 -06004175}
Alex Elderb7f23c32012-01-29 13:57:43 -06004176
Alex Elder1ddbe942012-01-29 13:57:44 -06004177/*
Alex Elder499afd52012-02-02 08:13:29 -06004178 * Remove an rbd_dev from the global list, and record that its
4179 * identifier is no longer in use.
Alex Elder1ddbe942012-01-29 13:57:44 -06004180 */
Alex Eldere2839302012-08-29 17:11:06 -05004181static void rbd_dev_id_put(struct rbd_device *rbd_dev)
Alex Elder1ddbe942012-01-29 13:57:44 -06004182{
Alex Elderd184f6b2012-01-29 13:57:44 -06004183 struct list_head *tmp;
Alex Elderde71a292012-07-03 16:01:19 -05004184 int rbd_id = rbd_dev->dev_id;
Alex Elderd184f6b2012-01-29 13:57:44 -06004185 int max_id;
4186
Alex Elderaafb2302012-09-06 16:00:54 -05004187 rbd_assert(rbd_id > 0);
Alex Elder499afd52012-02-02 08:13:29 -06004188
Alex Eldere2839302012-08-29 17:11:06 -05004189 dout("rbd_dev %p released dev id %llu\n", rbd_dev,
4190 (unsigned long long) rbd_dev->dev_id);
Alex Elder499afd52012-02-02 08:13:29 -06004191 spin_lock(&rbd_dev_list_lock);
4192 list_del_init(&rbd_dev->node);
Alex Elderd184f6b2012-01-29 13:57:44 -06004193
4194 /*
4195 * If the id being "put" is not the current maximum, there
4196 * is nothing special we need to do.
4197 */
Alex Eldere2839302012-08-29 17:11:06 -05004198 if (rbd_id != atomic64_read(&rbd_dev_id_max)) {
Alex Elderd184f6b2012-01-29 13:57:44 -06004199 spin_unlock(&rbd_dev_list_lock);
4200 return;
4201 }
4202
4203 /*
4204 * We need to update the current maximum id. Search the
4205 * list to find out what it is. We're more likely to find
4206 * the maximum at the end, so search the list backward.
4207 */
4208 max_id = 0;
4209 list_for_each_prev(tmp, &rbd_dev_list) {
4210 struct rbd_device *rbd_dev;
4211
4212 rbd_dev = list_entry(tmp, struct rbd_device, node);
Alex Elderb213e0b2012-10-10 21:19:13 -07004213 if (rbd_dev->dev_id > max_id)
4214 max_id = rbd_dev->dev_id;
Alex Elderd184f6b2012-01-29 13:57:44 -06004215 }
Alex Elder499afd52012-02-02 08:13:29 -06004216 spin_unlock(&rbd_dev_list_lock);
Alex Elderb7f23c32012-01-29 13:57:43 -06004217
Alex Elder1ddbe942012-01-29 13:57:44 -06004218 /*
Alex Eldere2839302012-08-29 17:11:06 -05004219 * The max id could have been updated by rbd_dev_id_get(), in
Alex Elderd184f6b2012-01-29 13:57:44 -06004220 * which case it now accurately reflects the new maximum.
4221 * Be careful not to overwrite the maximum value in that
4222 * case.
Alex Elder1ddbe942012-01-29 13:57:44 -06004223 */
Alex Eldere2839302012-08-29 17:11:06 -05004224 atomic64_cmpxchg(&rbd_dev_id_max, rbd_id, max_id);
4225 dout(" max dev id has been reset\n");
Alex Elderb7f23c32012-01-29 13:57:43 -06004226}
4227
Alex Eldera725f65e2012-02-02 08:13:30 -06004228/*
Alex Eldere28fff262012-02-02 08:13:30 -06004229 * Skips over white space at *buf, and updates *buf to point to the
4230 * first found non-space character (if any). Returns the length of
Alex Elder593a9e72012-02-07 12:03:37 -06004231 * the token (string of non-white space characters) found. Note
4232 * that *buf must be terminated with '\0'.
Alex Eldere28fff262012-02-02 08:13:30 -06004233 */
4234static inline size_t next_token(const char **buf)
4235{
4236 /*
4237 * These are the characters that produce nonzero for
4238 * isspace() in the "C" and "POSIX" locales.
4239 */
4240 const char *spaces = " \f\n\r\t\v";
4241
4242 *buf += strspn(*buf, spaces); /* Find start of token */
4243
4244 return strcspn(*buf, spaces); /* Return token length */
4245}
4246
4247/*
4248 * Finds the next token in *buf, and if the provided token buffer is
4249 * big enough, copies the found token into it. The result, if
Alex Elder593a9e72012-02-07 12:03:37 -06004250 * copied, is guaranteed to be terminated with '\0'. Note that *buf
4251 * must be terminated with '\0' on entry.
Alex Eldere28fff262012-02-02 08:13:30 -06004252 *
4253 * Returns the length of the token found (not including the '\0').
4254 * Return value will be 0 if no token is found, and it will be >=
4255 * token_size if the token would not fit.
4256 *
Alex Elder593a9e72012-02-07 12:03:37 -06004257 * The *buf pointer will be updated to point beyond the end of the
Alex Eldere28fff262012-02-02 08:13:30 -06004258 * found token. Note that this occurs even if the token buffer is
4259 * too small to hold it.
4260 */
4261static inline size_t copy_token(const char **buf,
4262 char *token,
4263 size_t token_size)
4264{
4265 size_t len;
4266
4267 len = next_token(buf);
4268 if (len < token_size) {
4269 memcpy(token, *buf, len);
4270 *(token + len) = '\0';
4271 }
4272 *buf += len;
4273
4274 return len;
4275}
4276
4277/*
Alex Elderea3352f2012-07-09 21:04:23 -05004278 * Finds the next token in *buf, dynamically allocates a buffer big
4279 * enough to hold a copy of it, and copies the token into the new
4280 * buffer. The copy is guaranteed to be terminated with '\0'. Note
4281 * that a duplicate buffer is created even for a zero-length token.
4282 *
4283 * Returns a pointer to the newly-allocated duplicate, or a null
4284 * pointer if memory for the duplicate was not available. If
4285 * the lenp argument is a non-null pointer, the length of the token
4286 * (not including the '\0') is returned in *lenp.
4287 *
4288 * If successful, the *buf pointer will be updated to point beyond
4289 * the end of the found token.
4290 *
4291 * Note: uses GFP_KERNEL for allocation.
4292 */
4293static inline char *dup_token(const char **buf, size_t *lenp)
4294{
4295 char *dup;
4296 size_t len;
4297
4298 len = next_token(buf);
Alex Elder4caf35f2012-11-01 08:39:27 -05004299 dup = kmemdup(*buf, len + 1, GFP_KERNEL);
Alex Elderea3352f2012-07-09 21:04:23 -05004300 if (!dup)
4301 return NULL;
Alex Elderea3352f2012-07-09 21:04:23 -05004302 *(dup + len) = '\0';
4303 *buf += len;
4304
4305 if (lenp)
4306 *lenp = len;
4307
4308 return dup;
4309}
4310
4311/*
Alex Elder859c31d2012-10-25 23:34:42 -05004312 * Parse the options provided for an "rbd add" (i.e., rbd image
4313 * mapping) request. These arrive via a write to /sys/bus/rbd/add,
4314 * and the data written is passed here via a NUL-terminated buffer.
4315 * Returns 0 if successful or an error code otherwise.
Alex Elderd22f76e2012-07-12 10:46:35 -05004316 *
Alex Elder859c31d2012-10-25 23:34:42 -05004317 * The information extracted from these options is recorded in
4318 * the other parameters which return dynamically-allocated
4319 * structures:
4320 * ceph_opts
4321 * The address of a pointer that will refer to a ceph options
4322 * structure. Caller must release the returned pointer using
4323 * ceph_destroy_options() when it is no longer needed.
4324 * rbd_opts
4325 * Address of an rbd options pointer. Fully initialized by
4326 * this function; caller must release with kfree().
4327 * spec
4328 * Address of an rbd image specification pointer. Fully
4329 * initialized by this function based on parsed options.
4330 * Caller must release with rbd_spec_put().
4331 *
4332 * The options passed take this form:
4333 * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
4334 * where:
4335 * <mon_addrs>
4336 * A comma-separated list of one or more monitor addresses.
4337 * A monitor address is an ip address, optionally followed
4338 * by a port number (separated by a colon).
4339 * I.e.: ip1[:port1][,ip2[:port2]...]
4340 * <options>
4341 * A comma-separated list of ceph and/or rbd options.
4342 * <pool_name>
4343 * The name of the rados pool containing the rbd image.
4344 * <image_name>
4345 * The name of the image in that pool to map.
4346 * <snap_id>
4347 * An optional snapshot id. If provided, the mapping will
4348 * present data from the image at the time that snapshot was
4349 * created. The image head is used if no snapshot id is
4350 * provided. Snapshot mappings are always read-only.
Alex Eldera725f65e2012-02-02 08:13:30 -06004351 */
Alex Elder859c31d2012-10-25 23:34:42 -05004352static int rbd_add_parse_args(const char *buf,
Alex Elderdc79b112012-10-25 23:34:41 -05004353 struct ceph_options **ceph_opts,
Alex Elder859c31d2012-10-25 23:34:42 -05004354 struct rbd_options **opts,
4355 struct rbd_spec **rbd_spec)
Alex Eldera725f65e2012-02-02 08:13:30 -06004356{
Alex Elderd22f76e2012-07-12 10:46:35 -05004357 size_t len;
Alex Elder859c31d2012-10-25 23:34:42 -05004358 char *options;
Alex Elder0ddebc02012-10-25 23:34:41 -05004359 const char *mon_addrs;
Alex Elderecb4dc222013-04-26 09:43:47 -05004360 char *snap_name;
Alex Elder0ddebc02012-10-25 23:34:41 -05004361 size_t mon_addrs_size;
Alex Elder859c31d2012-10-25 23:34:42 -05004362 struct rbd_spec *spec = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05004363 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05004364 struct ceph_options *copts;
Alex Elderdc79b112012-10-25 23:34:41 -05004365 int ret;
Alex Eldere28fff262012-02-02 08:13:30 -06004366
4367 /* The first four tokens are required */
4368
Alex Elder7ef32142012-02-02 08:13:30 -06004369 len = next_token(&buf);
Alex Elder4fb5d6712012-11-01 10:17:15 -05004370 if (!len) {
4371 rbd_warn(NULL, "no monitor address(es) provided");
4372 return -EINVAL;
4373 }
Alex Elder0ddebc02012-10-25 23:34:41 -05004374 mon_addrs = buf;
Alex Elderf28e5652012-10-25 23:34:41 -05004375 mon_addrs_size = len + 1;
Alex Elder7ef32142012-02-02 08:13:30 -06004376 buf += len;
Alex Eldera725f65e2012-02-02 08:13:30 -06004377
Alex Elderdc79b112012-10-25 23:34:41 -05004378 ret = -EINVAL;
Alex Elderf28e5652012-10-25 23:34:41 -05004379 options = dup_token(&buf, NULL);
4380 if (!options)
Alex Elderdc79b112012-10-25 23:34:41 -05004381 return -ENOMEM;
Alex Elder4fb5d6712012-11-01 10:17:15 -05004382 if (!*options) {
4383 rbd_warn(NULL, "no options provided");
4384 goto out_err;
4385 }
Alex Eldera725f65e2012-02-02 08:13:30 -06004386
Alex Elder859c31d2012-10-25 23:34:42 -05004387 spec = rbd_spec_alloc();
4388 if (!spec)
Alex Elderf28e5652012-10-25 23:34:41 -05004389 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05004390
4391 spec->pool_name = dup_token(&buf, NULL);
4392 if (!spec->pool_name)
4393 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05004394 if (!*spec->pool_name) {
4395 rbd_warn(NULL, "no pool name provided");
4396 goto out_err;
4397 }
Alex Eldere28fff262012-02-02 08:13:30 -06004398
Alex Elder69e7a022012-11-01 08:39:26 -05004399 spec->image_name = dup_token(&buf, NULL);
Alex Elder859c31d2012-10-25 23:34:42 -05004400 if (!spec->image_name)
Alex Elderf28e5652012-10-25 23:34:41 -05004401 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05004402 if (!*spec->image_name) {
4403 rbd_warn(NULL, "no image name provided");
4404 goto out_err;
4405 }
Alex Eldere28fff262012-02-02 08:13:30 -06004406
Alex Elderf28e5652012-10-25 23:34:41 -05004407 /*
4408 * Snapshot name is optional; default is to use "-"
4409 * (indicating the head/no snapshot).
4410 */
Alex Elder3feeb8942012-08-31 17:29:52 -05004411 len = next_token(&buf);
Alex Elder820a5f32012-07-09 21:04:24 -05004412 if (!len) {
Alex Elder3feeb8942012-08-31 17:29:52 -05004413 buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
4414 len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
Alex Elderf28e5652012-10-25 23:34:41 -05004415 } else if (len > RBD_MAX_SNAP_NAME_LEN) {
Alex Elderdc79b112012-10-25 23:34:41 -05004416 ret = -ENAMETOOLONG;
Alex Elderf28e5652012-10-25 23:34:41 -05004417 goto out_err;
Alex Elder849b4262012-07-09 21:04:24 -05004418 }
Alex Elderecb4dc222013-04-26 09:43:47 -05004419 snap_name = kmemdup(buf, len + 1, GFP_KERNEL);
4420 if (!snap_name)
Alex Elderf28e5652012-10-25 23:34:41 -05004421 goto out_mem;
Alex Elderecb4dc222013-04-26 09:43:47 -05004422 *(snap_name + len) = '\0';
4423 spec->snap_name = snap_name;
Alex Eldere5c35532012-10-25 23:34:41 -05004424
Alex Elder0ddebc02012-10-25 23:34:41 -05004425 /* Initialize all rbd options to the defaults */
Alex Eldere28fff262012-02-02 08:13:30 -06004426
Alex Elder4e9afeb2012-10-25 23:34:41 -05004427 rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL);
4428 if (!rbd_opts)
4429 goto out_mem;
4430
4431 rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
Alex Elderd22f76e2012-07-12 10:46:35 -05004432
Alex Elder859c31d2012-10-25 23:34:42 -05004433 copts = ceph_parse_options(options, mon_addrs,
Alex Elder0ddebc02012-10-25 23:34:41 -05004434 mon_addrs + mon_addrs_size - 1,
Alex Elder4e9afeb2012-10-25 23:34:41 -05004435 parse_rbd_opts_token, rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05004436 if (IS_ERR(copts)) {
4437 ret = PTR_ERR(copts);
Alex Elderdc79b112012-10-25 23:34:41 -05004438 goto out_err;
4439 }
Alex Elder859c31d2012-10-25 23:34:42 -05004440 kfree(options);
4441
4442 *ceph_opts = copts;
Alex Elder4e9afeb2012-10-25 23:34:41 -05004443 *opts = rbd_opts;
Alex Elder859c31d2012-10-25 23:34:42 -05004444 *rbd_spec = spec;
Alex Elder0ddebc02012-10-25 23:34:41 -05004445
Alex Elderdc79b112012-10-25 23:34:41 -05004446 return 0;
Alex Elderf28e5652012-10-25 23:34:41 -05004447out_mem:
Alex Elderdc79b112012-10-25 23:34:41 -05004448 ret = -ENOMEM;
Alex Elderd22f76e2012-07-12 10:46:35 -05004449out_err:
Alex Elder859c31d2012-10-25 23:34:42 -05004450 kfree(rbd_opts);
4451 rbd_spec_put(spec);
Alex Elderf28e5652012-10-25 23:34:41 -05004452 kfree(options);
Alex Elderd22f76e2012-07-12 10:46:35 -05004453
Alex Elderdc79b112012-10-25 23:34:41 -05004454 return ret;
Alex Eldera725f65e2012-02-02 08:13:30 -06004455}
4456
Alex Elder589d30e2012-07-10 20:30:11 -05004457/*
4458 * An rbd format 2 image has a unique identifier, distinct from the
4459 * name given to it by the user. Internally, that identifier is
4460 * what's used to specify the names of objects related to the image.
4461 *
4462 * A special "rbd id" object is used to map an rbd image name to its
4463 * id. If that object doesn't exist, then there is no v2 rbd image
4464 * with the supplied name.
4465 *
4466 * This function will record the given rbd_dev's image_id field if
4467 * it can be determined, and in that case will return 0. If any
4468 * errors occur a negative errno will be returned and the rbd_dev's
4469 * image_id field will be unchanged (and should be NULL).
4470 */
4471static int rbd_dev_image_id(struct rbd_device *rbd_dev)
4472{
4473 int ret;
4474 size_t size;
4475 char *object_name;
4476 void *response;
Alex Elderc0fba362013-04-25 23:15:08 -05004477 char *image_id;
Alex Elder2f82ee52012-10-30 19:40:33 -05004478
Alex Elder589d30e2012-07-10 20:30:11 -05004479 /*
Alex Elder2c0d0a12012-10-30 19:40:33 -05004480 * When probing a parent image, the image id is already
4481 * known (and the image name likely is not). There's no
Alex Elderc0fba362013-04-25 23:15:08 -05004482 * need to fetch the image id again in this case. We
4483 * do still need to set the image format though.
Alex Elder2c0d0a12012-10-30 19:40:33 -05004484 */
Alex Elderc0fba362013-04-25 23:15:08 -05004485 if (rbd_dev->spec->image_id) {
4486 rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1;
4487
Alex Elder2c0d0a12012-10-30 19:40:33 -05004488 return 0;
Alex Elderc0fba362013-04-25 23:15:08 -05004489 }
Alex Elder2c0d0a12012-10-30 19:40:33 -05004490
4491 /*
Alex Elder589d30e2012-07-10 20:30:11 -05004492 * First, see if the format 2 image id file exists, and if
4493 * so, get the image's persistent id from it.
4494 */
Alex Elder69e7a022012-11-01 08:39:26 -05004495 size = sizeof (RBD_ID_PREFIX) + strlen(rbd_dev->spec->image_name);
Alex Elder589d30e2012-07-10 20:30:11 -05004496 object_name = kmalloc(size, GFP_NOIO);
4497 if (!object_name)
4498 return -ENOMEM;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004499 sprintf(object_name, "%s%s", RBD_ID_PREFIX, rbd_dev->spec->image_name);
Alex Elder589d30e2012-07-10 20:30:11 -05004500 dout("rbd id object name is %s\n", object_name);
4501
4502 /* Response will be an encoded string, which includes a length */
4503
4504 size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
4505 response = kzalloc(size, GFP_NOIO);
4506 if (!response) {
4507 ret = -ENOMEM;
4508 goto out;
4509 }
4510
Alex Elderc0fba362013-04-25 23:15:08 -05004511 /* If it doesn't exist we'll assume it's a format 1 image */
4512
Alex Elder36be9a72013-01-19 00:30:28 -06004513 ret = rbd_obj_method_sync(rbd_dev, object_name,
Alex Elder41579762013-04-21 12:14:45 -05004514 "rbd", "get_id", NULL, 0,
Alex Elder07b23912012-11-09 08:43:16 -06004515 response, RBD_IMAGE_ID_LEN_MAX, NULL);
Alex Elder36be9a72013-01-19 00:30:28 -06004516 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderc0fba362013-04-25 23:15:08 -05004517 if (ret == -ENOENT) {
4518 image_id = kstrdup("", GFP_KERNEL);
4519 ret = image_id ? 0 : -ENOMEM;
4520 if (!ret)
4521 rbd_dev->image_format = 1;
4522 } else if (ret > sizeof (__le32)) {
4523 void *p = response;
Alex Elder589d30e2012-07-10 20:30:11 -05004524
Alex Elderc0fba362013-04-25 23:15:08 -05004525 image_id = ceph_extract_encoded_string(&p, p + ret,
Alex Elder979ed482012-11-01 08:39:26 -05004526 NULL, GFP_NOIO);
Alex Elderc0fba362013-04-25 23:15:08 -05004527 ret = IS_ERR(image_id) ? PTR_ERR(image_id) : 0;
4528 if (!ret)
4529 rbd_dev->image_format = 2;
Alex Elder589d30e2012-07-10 20:30:11 -05004530 } else {
Alex Elderc0fba362013-04-25 23:15:08 -05004531 ret = -EINVAL;
4532 }
4533
4534 if (!ret) {
4535 rbd_dev->spec->image_id = image_id;
4536 dout("image_id is %s\n", image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05004537 }
4538out:
4539 kfree(response);
4540 kfree(object_name);
4541
4542 return ret;
4543}
4544
Alex Elder6fd48b32013-04-28 23:32:34 -05004545/* Undo whatever state changes are made by v1 or v2 image probe */
4546
4547static void rbd_dev_unprobe(struct rbd_device *rbd_dev)
4548{
4549 struct rbd_image_header *header;
4550
4551 rbd_dev_remove_parent(rbd_dev);
4552 rbd_spec_put(rbd_dev->parent_spec);
4553 rbd_dev->parent_spec = NULL;
4554 rbd_dev->parent_overlap = 0;
4555
4556 /* Free dynamic fields from the header, then zero it out */
4557
4558 header = &rbd_dev->header;
Alex Elder812164f82013-04-30 00:44:32 -05004559 ceph_put_snap_context(header->snapc);
Alex Elder6fd48b32013-04-28 23:32:34 -05004560 kfree(header->snap_sizes);
4561 kfree(header->snap_names);
4562 kfree(header->object_prefix);
4563 memset(header, 0, sizeof (*header));
4564}
4565
Alex Eldera30b71b2012-07-10 20:30:11 -05004566static int rbd_dev_v1_probe(struct rbd_device *rbd_dev)
4567{
4568 int ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05004569
4570 /* Populate rbd image metadata */
4571
4572 ret = rbd_read_header(rbd_dev, &rbd_dev->header);
4573 if (ret < 0)
4574 goto out_err;
Alex Elder86b00e02012-10-25 23:34:42 -05004575
4576 /* Version 1 images have no parent (no layering) */
4577
4578 rbd_dev->parent_spec = NULL;
4579 rbd_dev->parent_overlap = 0;
4580
Alex Eldera30b71b2012-07-10 20:30:11 -05004581 dout("discovered version 1 image, header name is %s\n",
4582 rbd_dev->header_name);
4583
4584 return 0;
4585
4586out_err:
4587 kfree(rbd_dev->header_name);
4588 rbd_dev->header_name = NULL;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004589 kfree(rbd_dev->spec->image_id);
4590 rbd_dev->spec->image_id = NULL;
Alex Eldera30b71b2012-07-10 20:30:11 -05004591
4592 return ret;
4593}
4594
4595static int rbd_dev_v2_probe(struct rbd_device *rbd_dev)
4596{
Alex Elder9d475de2012-07-03 16:01:19 -05004597 int ret;
Alex Elder6e14b1a2012-07-03 16:01:19 -05004598 u64 ver = 0;
Alex Eldera30b71b2012-07-10 20:30:11 -05004599
Alex Elder9d475de2012-07-03 16:01:19 -05004600 ret = rbd_dev_v2_image_size(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05004601 if (ret)
Alex Elder9d475de2012-07-03 16:01:19 -05004602 goto out_err;
Alex Elder1e130192012-07-03 16:01:19 -05004603
4604 /* Get the object prefix (a.k.a. block_name) for the image */
4605
4606 ret = rbd_dev_v2_object_prefix(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05004607 if (ret)
Alex Elder1e130192012-07-03 16:01:19 -05004608 goto out_err;
Alex Elderb1b54022012-07-03 16:01:19 -05004609
Alex Elderd8891402012-10-09 13:50:17 -07004610 /* Get the and check features for the image */
Alex Elderb1b54022012-07-03 16:01:19 -05004611
4612 ret = rbd_dev_v2_features(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05004613 if (ret)
Alex Elderb1b54022012-07-03 16:01:19 -05004614 goto out_err;
Alex Elder35d489f2012-07-03 16:01:19 -05004615
Alex Elder86b00e02012-10-25 23:34:42 -05004616 /* If the image supports layering, get the parent info */
4617
4618 if (rbd_dev->header.features & RBD_FEATURE_LAYERING) {
4619 ret = rbd_dev_v2_parent_info(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05004620 if (ret)
Alex Elder86b00e02012-10-25 23:34:42 -05004621 goto out_err;
Alex Elder96882f52013-04-30 00:44:32 -05004622
4623 /*
4624 * Don't print a warning for parent images. We can
4625 * tell this point because we won't know its pool
4626 * name yet (just its pool id).
4627 */
4628 if (rbd_dev->spec->pool_name)
4629 rbd_warn(rbd_dev, "WARNING: kernel layering "
4630 "is EXPERIMENTAL!");
Alex Elder86b00e02012-10-25 23:34:42 -05004631 }
4632
Alex Eldercc070d52013-04-21 12:14:45 -05004633 /* If the image supports fancy striping, get its parameters */
4634
4635 if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) {
4636 ret = rbd_dev_v2_striping_info(rbd_dev);
4637 if (ret < 0)
4638 goto out_err;
4639 }
4640
Alex Elder6e14b1a2012-07-03 16:01:19 -05004641 /* crypto and compression type aren't (yet) supported for v2 images */
Alex Elder35d489f2012-07-03 16:01:19 -05004642
Alex Elder6e14b1a2012-07-03 16:01:19 -05004643 rbd_dev->header.crypt_type = 0;
4644 rbd_dev->header.comp_type = 0;
4645
4646 /* Get the snapshot context, plus the header version */
4647
4648 ret = rbd_dev_v2_snap_context(rbd_dev, &ver);
Alex Elder35d489f2012-07-03 16:01:19 -05004649 if (ret)
4650 goto out_err;
Alex Elder6e14b1a2012-07-03 16:01:19 -05004651
Alex Eldera30b71b2012-07-10 20:30:11 -05004652 dout("discovered version 2 image, header name is %s\n",
4653 rbd_dev->header_name);
4654
Alex Elder35152972012-08-31 17:29:55 -05004655 return 0;
Alex Elder9d475de2012-07-03 16:01:19 -05004656out_err:
Alex Elder86b00e02012-10-25 23:34:42 -05004657 rbd_dev->parent_overlap = 0;
4658 rbd_spec_put(rbd_dev->parent_spec);
4659 rbd_dev->parent_spec = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05004660 kfree(rbd_dev->header_name);
4661 rbd_dev->header_name = NULL;
Alex Elder1e130192012-07-03 16:01:19 -05004662 kfree(rbd_dev->header.object_prefix);
4663 rbd_dev->header.object_prefix = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05004664
4665 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05004666}
4667
Alex Elder124afba2013-04-26 15:44:36 -05004668static int rbd_dev_probe_parent(struct rbd_device *rbd_dev)
Alex Elder83a06262012-10-30 15:47:17 -05004669{
Alex Elder2f82ee52012-10-30 19:40:33 -05004670 struct rbd_device *parent = NULL;
Alex Elder124afba2013-04-26 15:44:36 -05004671 struct rbd_spec *parent_spec;
4672 struct rbd_client *rbdc;
4673 int ret;
4674
4675 if (!rbd_dev->parent_spec)
4676 return 0;
4677 /*
4678 * We need to pass a reference to the client and the parent
4679 * spec when creating the parent rbd_dev. Images related by
4680 * parent/child relationships always share both.
4681 */
4682 parent_spec = rbd_spec_get(rbd_dev->parent_spec);
4683 rbdc = __rbd_get_client(rbd_dev->rbd_client);
4684
4685 ret = -ENOMEM;
4686 parent = rbd_dev_create(rbdc, parent_spec);
4687 if (!parent)
4688 goto out_err;
4689
4690 ret = rbd_dev_image_probe(parent);
4691 if (ret < 0)
4692 goto out_err;
4693 rbd_dev->parent = parent;
4694
4695 return 0;
4696out_err:
4697 if (parent) {
4698 rbd_spec_put(rbd_dev->parent_spec);
4699 kfree(rbd_dev->header_name);
4700 rbd_dev_destroy(parent);
4701 } else {
4702 rbd_put_client(rbdc);
4703 rbd_spec_put(parent_spec);
4704 }
4705
4706 return ret;
4707}
4708
Alex Elder200a6a82013-04-28 23:32:34 -05004709static int rbd_dev_device_setup(struct rbd_device *rbd_dev)
Alex Elder124afba2013-04-26 15:44:36 -05004710{
Alex Elder83a06262012-10-30 15:47:17 -05004711 int ret;
Alex Elder83a06262012-10-30 15:47:17 -05004712
Alex Elderd1cf5782013-04-27 09:59:30 -05004713 ret = rbd_dev_mapping_set(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05004714 if (ret)
Alex Elder9bb81c92013-04-27 09:59:30 -05004715 return ret;
Alex Elder5de10f32013-04-26 15:44:37 -05004716
Alex Elder83a06262012-10-30 15:47:17 -05004717 /* generate unique id: find highest unique id, add one */
4718 rbd_dev_id_get(rbd_dev);
4719
4720 /* Fill in the device name, now that we have its id. */
4721 BUILD_BUG_ON(DEV_NAME_LEN
4722 < sizeof (RBD_DRV_NAME) + MAX_INT_FORMAT_WIDTH);
4723 sprintf(rbd_dev->name, "%s%d", RBD_DRV_NAME, rbd_dev->dev_id);
4724
4725 /* Get our block major device number. */
4726
4727 ret = register_blkdev(0, rbd_dev->name);
4728 if (ret < 0)
4729 goto err_out_id;
4730 rbd_dev->major = ret;
4731
4732 /* Set up the blkdev mapping. */
4733
4734 ret = rbd_init_disk(rbd_dev);
4735 if (ret)
4736 goto err_out_blkdev;
4737
4738 ret = rbd_bus_add_dev(rbd_dev);
4739 if (ret)
4740 goto err_out_disk;
4741
Alex Elder83a06262012-10-30 15:47:17 -05004742 /* Everything's ready. Announce the disk to the world. */
4743
Alex Elderb5156e72013-04-26 15:44:36 -05004744 set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
Alex Elder129b79d2013-04-26 15:44:36 -05004745 set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
Alex Elder83a06262012-10-30 15:47:17 -05004746 add_disk(rbd_dev->disk);
4747
4748 pr_info("%s: added with size 0x%llx\n", rbd_dev->disk->disk_name,
4749 (unsigned long long) rbd_dev->mapping.size);
4750
4751 return ret;
Alex Elder2f82ee52012-10-30 19:40:33 -05004752
Alex Elder83a06262012-10-30 15:47:17 -05004753err_out_disk:
4754 rbd_free_disk(rbd_dev);
4755err_out_blkdev:
4756 unregister_blkdev(rbd_dev->major, rbd_dev->name);
4757err_out_id:
4758 rbd_dev_id_put(rbd_dev);
Alex Elderd1cf5782013-04-27 09:59:30 -05004759 rbd_dev_mapping_clear(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05004760
4761 return ret;
4762}
4763
Alex Elder332bb122013-04-27 09:59:30 -05004764static int rbd_dev_header_name(struct rbd_device *rbd_dev)
4765{
4766 struct rbd_spec *spec = rbd_dev->spec;
4767 size_t size;
4768
4769 /* Record the header object name for this rbd image. */
4770
4771 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
4772
4773 if (rbd_dev->image_format == 1)
4774 size = strlen(spec->image_name) + sizeof (RBD_SUFFIX);
4775 else
4776 size = sizeof (RBD_HEADER_PREFIX) + strlen(spec->image_id);
4777
4778 rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
4779 if (!rbd_dev->header_name)
4780 return -ENOMEM;
4781
4782 if (rbd_dev->image_format == 1)
4783 sprintf(rbd_dev->header_name, "%s%s",
4784 spec->image_name, RBD_SUFFIX);
4785 else
4786 sprintf(rbd_dev->header_name, "%s%s",
4787 RBD_HEADER_PREFIX, spec->image_id);
4788 return 0;
4789}
4790
Alex Elder200a6a82013-04-28 23:32:34 -05004791static void rbd_dev_image_release(struct rbd_device *rbd_dev)
4792{
Alex Elder6fd48b32013-04-28 23:32:34 -05004793 int ret;
4794
4795 rbd_remove_all_snaps(rbd_dev);
4796 rbd_dev_unprobe(rbd_dev);
4797 ret = rbd_dev_header_watch_sync(rbd_dev, 0);
4798 if (ret)
4799 rbd_warn(rbd_dev, "failed to cancel watch event (%d)\n", ret);
Alex Elder200a6a82013-04-28 23:32:34 -05004800 kfree(rbd_dev->header_name);
Alex Elder6fd48b32013-04-28 23:32:34 -05004801 rbd_dev->header_name = NULL;
4802 rbd_dev->image_format = 0;
4803 kfree(rbd_dev->spec->image_id);
4804 rbd_dev->spec->image_id = NULL;
4805
Alex Elder200a6a82013-04-28 23:32:34 -05004806 rbd_dev_destroy(rbd_dev);
4807}
4808
Alex Eldera30b71b2012-07-10 20:30:11 -05004809/*
4810 * Probe for the existence of the header object for the given rbd
4811 * device. For format 2 images this includes determining the image
4812 * id.
4813 */
Alex Elder71f293e2013-04-26 09:43:48 -05004814static int rbd_dev_image_probe(struct rbd_device *rbd_dev)
Alex Eldera30b71b2012-07-10 20:30:11 -05004815{
4816 int ret;
Alex Elderb644de22013-04-27 09:59:31 -05004817 int tmp;
Alex Eldera30b71b2012-07-10 20:30:11 -05004818
4819 /*
4820 * Get the id from the image id object. If it's not a
4821 * format 2 image, we'll get ENOENT back, and we'll assume
4822 * it's a format 1 image.
4823 */
4824 ret = rbd_dev_image_id(rbd_dev);
4825 if (ret)
Alex Elderc0fba362013-04-25 23:15:08 -05004826 return ret;
4827 rbd_assert(rbd_dev->spec->image_id);
4828 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
4829
Alex Elder332bb122013-04-27 09:59:30 -05004830 ret = rbd_dev_header_name(rbd_dev);
4831 if (ret)
4832 goto err_out_format;
4833
Alex Elderb644de22013-04-27 09:59:31 -05004834 ret = rbd_dev_header_watch_sync(rbd_dev, 1);
4835 if (ret)
4836 goto out_header_name;
4837
Alex Elderc0fba362013-04-25 23:15:08 -05004838 if (rbd_dev->image_format == 1)
Alex Eldera30b71b2012-07-10 20:30:11 -05004839 ret = rbd_dev_v1_probe(rbd_dev);
4840 else
4841 ret = rbd_dev_v2_probe(rbd_dev);
Alex Elder5655c4d2013-04-25 23:15:08 -05004842 if (ret)
Alex Elderb644de22013-04-27 09:59:31 -05004843 goto err_out_watch;
Alex Elder83a06262012-10-30 15:47:17 -05004844
Alex Elder9bb81c92013-04-27 09:59:30 -05004845 ret = rbd_dev_snaps_update(rbd_dev);
4846 if (ret)
Alex Elder6fd48b32013-04-28 23:32:34 -05004847 goto err_out_probe;
Alex Elder9bb81c92013-04-27 09:59:30 -05004848
4849 ret = rbd_dev_spec_update(rbd_dev);
4850 if (ret)
4851 goto err_out_snaps;
4852
4853 ret = rbd_dev_probe_parent(rbd_dev);
Alex Elder6fd48b32013-04-28 23:32:34 -05004854 if (!ret)
4855 return 0;
Alex Elder83a06262012-10-30 15:47:17 -05004856
Alex Elder9bb81c92013-04-27 09:59:30 -05004857err_out_snaps:
4858 rbd_remove_all_snaps(rbd_dev);
Alex Elder6fd48b32013-04-28 23:32:34 -05004859err_out_probe:
4860 rbd_dev_unprobe(rbd_dev);
Alex Elderb644de22013-04-27 09:59:31 -05004861err_out_watch:
4862 tmp = rbd_dev_header_watch_sync(rbd_dev, 0);
4863 if (tmp)
4864 rbd_warn(rbd_dev, "unable to tear down watch request\n");
Alex Elder332bb122013-04-27 09:59:30 -05004865out_header_name:
4866 kfree(rbd_dev->header_name);
4867 rbd_dev->header_name = NULL;
4868err_out_format:
4869 rbd_dev->image_format = 0;
Alex Elder5655c4d2013-04-25 23:15:08 -05004870 kfree(rbd_dev->spec->image_id);
4871 rbd_dev->spec->image_id = NULL;
4872
4873 dout("probe failed, returning %d\n", ret);
4874
4875 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05004876}
4877
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07004878static ssize_t rbd_add(struct bus_type *bus,
4879 const char *buf,
4880 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004881{
Alex Eldercb8627c2012-07-09 21:04:23 -05004882 struct rbd_device *rbd_dev = NULL;
Alex Elderdc79b112012-10-25 23:34:41 -05004883 struct ceph_options *ceph_opts = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05004884 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05004885 struct rbd_spec *spec = NULL;
Alex Elder9d3997f2012-10-25 23:34:42 -05004886 struct rbd_client *rbdc;
Alex Elder27cc2592012-02-02 08:13:30 -06004887 struct ceph_osd_client *osdc;
4888 int rc = -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004889
4890 if (!try_module_get(THIS_MODULE))
4891 return -ENODEV;
4892
Alex Eldera725f65e2012-02-02 08:13:30 -06004893 /* parse add command */
Alex Elder859c31d2012-10-25 23:34:42 -05004894 rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec);
Alex Elderdc79b112012-10-25 23:34:41 -05004895 if (rc < 0)
Alex Elderbd4ba652012-10-25 23:34:42 -05004896 goto err_out_module;
Alex Eldera725f65e2012-02-02 08:13:30 -06004897
Alex Elder9d3997f2012-10-25 23:34:42 -05004898 rbdc = rbd_get_client(ceph_opts);
4899 if (IS_ERR(rbdc)) {
4900 rc = PTR_ERR(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05004901 goto err_out_args;
Alex Elder9d3997f2012-10-25 23:34:42 -05004902 }
Alex Elderc53d5892012-10-25 23:34:42 -05004903 ceph_opts = NULL; /* rbd_dev client now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004904
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004905 /* pick the pool */
Alex Elder9d3997f2012-10-25 23:34:42 -05004906 osdc = &rbdc->client->osdc;
Alex Elder859c31d2012-10-25 23:34:42 -05004907 rc = ceph_pg_poolid_by_name(osdc->osdmap, spec->pool_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004908 if (rc < 0)
4909 goto err_out_client;
Alex Elderc0cd10db2013-04-26 09:43:47 -05004910 spec->pool_id = (u64)rc;
Alex Elder859c31d2012-10-25 23:34:42 -05004911
Alex Elder0903e872012-11-14 12:25:19 -06004912 /* The ceph file layout needs to fit pool id in 32 bits */
4913
Alex Elderc0cd10db2013-04-26 09:43:47 -05004914 if (spec->pool_id > (u64)U32_MAX) {
4915 rbd_warn(NULL, "pool id too large (%llu > %u)\n",
4916 (unsigned long long)spec->pool_id, U32_MAX);
Alex Elder0903e872012-11-14 12:25:19 -06004917 rc = -EIO;
4918 goto err_out_client;
4919 }
4920
Alex Elderc53d5892012-10-25 23:34:42 -05004921 rbd_dev = rbd_dev_create(rbdc, spec);
Alex Elderbd4ba652012-10-25 23:34:42 -05004922 if (!rbd_dev)
4923 goto err_out_client;
Alex Elderc53d5892012-10-25 23:34:42 -05004924 rbdc = NULL; /* rbd_dev now owns this */
4925 spec = NULL; /* rbd_dev now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004926
Alex Elderbd4ba652012-10-25 23:34:42 -05004927 rbd_dev->mapping.read_only = rbd_opts->read_only;
Alex Elderc53d5892012-10-25 23:34:42 -05004928 kfree(rbd_opts);
4929 rbd_opts = NULL; /* done with this */
Alex Elderbd4ba652012-10-25 23:34:42 -05004930
Alex Elder71f293e2013-04-26 09:43:48 -05004931 rc = rbd_dev_image_probe(rbd_dev);
Alex Eldera30b71b2012-07-10 20:30:11 -05004932 if (rc < 0)
Alex Elderc53d5892012-10-25 23:34:42 -05004933 goto err_out_rbd_dev;
Alex Elder05fd6f62012-08-29 17:11:07 -05004934
Alex Elderb536f692013-04-28 23:32:34 -05004935 rc = rbd_dev_device_setup(rbd_dev);
4936 if (!rc)
4937 return count;
4938
4939 rbd_dev_image_release(rbd_dev);
Alex Elderc53d5892012-10-25 23:34:42 -05004940err_out_rbd_dev:
4941 rbd_dev_destroy(rbd_dev);
Alex Elderbd4ba652012-10-25 23:34:42 -05004942err_out_client:
Alex Elder9d3997f2012-10-25 23:34:42 -05004943 rbd_put_client(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05004944err_out_args:
Alex Elder78cea762012-10-25 23:34:41 -05004945 if (ceph_opts)
4946 ceph_destroy_options(ceph_opts);
Alex Elder4e9afeb2012-10-25 23:34:41 -05004947 kfree(rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05004948 rbd_spec_put(spec);
Alex Elderbd4ba652012-10-25 23:34:42 -05004949err_out_module:
4950 module_put(THIS_MODULE);
Alex Elder27cc2592012-02-02 08:13:30 -06004951
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004952 dout("Error adding device %s\n", buf);
Alex Elder27cc2592012-02-02 08:13:30 -06004953
Alex Elderc0cd10db2013-04-26 09:43:47 -05004954 return (ssize_t)rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004955}
4956
Alex Elderde71a292012-07-03 16:01:19 -05004957static struct rbd_device *__rbd_get_dev(unsigned long dev_id)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004958{
4959 struct list_head *tmp;
4960 struct rbd_device *rbd_dev;
4961
Alex Eldere124a82f2012-01-29 13:57:44 -06004962 spin_lock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004963 list_for_each(tmp, &rbd_dev_list) {
4964 rbd_dev = list_entry(tmp, struct rbd_device, node);
Alex Elderde71a292012-07-03 16:01:19 -05004965 if (rbd_dev->dev_id == dev_id) {
Alex Eldere124a82f2012-01-29 13:57:44 -06004966 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004967 return rbd_dev;
Alex Eldere124a82f2012-01-29 13:57:44 -06004968 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004969 }
Alex Eldere124a82f2012-01-29 13:57:44 -06004970 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004971 return NULL;
4972}
4973
Alex Elder200a6a82013-04-28 23:32:34 -05004974static void rbd_dev_device_release(struct device *dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004975{
Alex Elder593a9e72012-02-07 12:03:37 -06004976 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004977
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004978 rbd_free_disk(rbd_dev);
Alex Elder200a6a82013-04-28 23:32:34 -05004979 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
4980 rbd_dev_clear_mapping(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004981 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Alex Elder200a6a82013-04-28 23:32:34 -05004982 rbd_dev->major = 0;
Alex Eldere2839302012-08-29 17:11:06 -05004983 rbd_dev_id_put(rbd_dev);
Alex Elderd1cf5782013-04-27 09:59:30 -05004984 rbd_dev_mapping_clear(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004985}
4986
Alex Elder05a46af2013-04-26 15:44:36 -05004987static void rbd_dev_remove_parent(struct rbd_device *rbd_dev)
4988{
Alex Elderad945fc2013-04-26 15:44:36 -05004989 while (rbd_dev->parent) {
Alex Elder05a46af2013-04-26 15:44:36 -05004990 struct rbd_device *first = rbd_dev;
4991 struct rbd_device *second = first->parent;
4992 struct rbd_device *third;
4993
4994 /*
4995 * Follow to the parent with no grandparent and
4996 * remove it.
4997 */
4998 while (second && (third = second->parent)) {
4999 first = second;
5000 second = third;
5001 }
Alex Elderad945fc2013-04-26 15:44:36 -05005002 rbd_assert(second);
Alex Elder8ad42cd2013-04-28 23:32:34 -05005003 rbd_dev_image_release(second);
Alex Elderad945fc2013-04-26 15:44:36 -05005004 first->parent = NULL;
5005 first->parent_overlap = 0;
5006
5007 rbd_assert(first->parent_spec);
Alex Elder05a46af2013-04-26 15:44:36 -05005008 rbd_spec_put(first->parent_spec);
5009 first->parent_spec = NULL;
Alex Elder05a46af2013-04-26 15:44:36 -05005010 }
5011}
5012
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005013static ssize_t rbd_remove(struct bus_type *bus,
5014 const char *buf,
5015 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005016{
5017 struct rbd_device *rbd_dev = NULL;
Alex Elder0d8189e2013-04-27 09:59:30 -05005018 int target_id;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005019 unsigned long ul;
Alex Elder0d8189e2013-04-27 09:59:30 -05005020 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005021
Alex Elder0d8189e2013-04-27 09:59:30 -05005022 ret = strict_strtoul(buf, 10, &ul);
5023 if (ret)
5024 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005025
5026 /* convert to int; abort if we lost anything in the conversion */
5027 target_id = (int) ul;
5028 if (target_id != ul)
5029 return -EINVAL;
5030
5031 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
5032
5033 rbd_dev = __rbd_get_dev(target_id);
5034 if (!rbd_dev) {
5035 ret = -ENOENT;
5036 goto done;
5037 }
5038
Alex Eldera14ea262013-02-05 13:23:12 -06005039 spin_lock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -06005040 if (rbd_dev->open_count)
Alex Elder42382b72012-11-16 09:29:16 -06005041 ret = -EBUSY;
Alex Elderb82d1672013-01-14 12:43:31 -06005042 else
5043 set_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags);
Alex Eldera14ea262013-02-05 13:23:12 -06005044 spin_unlock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -06005045 if (ret < 0)
Alex Elder42382b72012-11-16 09:29:16 -06005046 goto done;
Alex Elder0d8189e2013-04-27 09:59:30 -05005047 ret = count;
Alex Elderb4808152013-04-26 15:44:36 -05005048 rbd_bus_del_dev(rbd_dev);
Alex Elder8ad42cd2013-04-28 23:32:34 -05005049 rbd_dev_image_release(rbd_dev);
Alex Elder79ab7552013-04-28 23:32:34 -05005050 module_put(THIS_MODULE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005051done:
5052 mutex_unlock(&ctl_mutex);
Alex Elderaafb2302012-09-06 16:00:54 -05005053
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005054 return ret;
5055}
5056
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005057/*
5058 * create control files in sysfs
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005059 * /sys/bus/rbd/...
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005060 */
5061static int rbd_sysfs_init(void)
5062{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005063 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005064
Alex Elderfed4c142012-02-07 12:03:36 -06005065 ret = device_register(&rbd_root_dev);
Alex Elder21079782012-01-24 10:08:36 -06005066 if (ret < 0)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005067 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005068
Alex Elderfed4c142012-02-07 12:03:36 -06005069 ret = bus_register(&rbd_bus_type);
5070 if (ret < 0)
5071 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005072
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005073 return ret;
5074}
5075
5076static void rbd_sysfs_cleanup(void)
5077{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005078 bus_unregister(&rbd_bus_type);
Alex Elderfed4c142012-02-07 12:03:36 -06005079 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005080}
5081
Alex Eldercc344fa2013-02-19 12:25:56 -06005082static int __init rbd_init(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005083{
5084 int rc;
5085
Alex Elder1e32d342013-01-30 11:13:33 -06005086 if (!libceph_compatible(NULL)) {
5087 rbd_warn(NULL, "libceph incompatibility (quitting)");
5088
5089 return -EINVAL;
5090 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005091 rc = rbd_sysfs_init();
5092 if (rc)
5093 return rc;
Alex Elderf0f8cef2012-01-29 13:57:44 -06005094 pr_info("loaded " RBD_DRV_NAME_LONG "\n");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005095 return 0;
5096}
5097
Alex Eldercc344fa2013-02-19 12:25:56 -06005098static void __exit rbd_exit(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005099{
5100 rbd_sysfs_cleanup();
5101}
5102
5103module_init(rbd_init);
5104module_exit(rbd_exit);
5105
5106MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
5107MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
5108MODULE_DESCRIPTION("rados block device");
5109
5110/* following authorship retained from original osdblk.c */
5111MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
5112
5113MODULE_LICENSE("GPL");