blob: 4d99d40034e1792d1acb801a5732407d6a6287a4 [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
111 u64 obj_version;
112};
113
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500114/*
115 * An rbd image specification.
116 *
117 * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely
Alex Elderc66c6e02012-11-01 08:39:26 -0500118 * identify an image. Each rbd_dev structure includes a pointer to
119 * an rbd_spec structure that encapsulates this identity.
120 *
121 * Each of the id's in an rbd_spec has an associated name. For a
122 * user-mapped image, the names are supplied and the id's associated
123 * with them are looked up. For a layered image, a parent image is
124 * defined by the tuple, and the names are looked up.
125 *
126 * An rbd_dev structure contains a parent_spec pointer which is
127 * non-null if the image it represents is a child in a layered
128 * image. This pointer will refer to the rbd_spec structure used
129 * by the parent rbd_dev for its own identity (i.e., the structure
130 * is shared between the parent and child).
131 *
132 * Since these structures are populated once, during the discovery
133 * phase of image construction, they are effectively immutable so
134 * we make no effort to synchronize access to them.
135 *
136 * Note that code herein does not assume the image name is known (it
137 * could be a null pointer).
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500138 */
139struct rbd_spec {
140 u64 pool_id;
141 char *pool_name;
142
143 char *image_id;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500144 char *image_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500145
146 u64 snap_id;
147 char *snap_name;
148
149 struct kref kref;
150};
151
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700152/*
Alex Elderf0f8cef2012-01-29 13:57:44 -0600153 * an instance of the client. multiple devices may share an rbd client.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700154 */
155struct rbd_client {
156 struct ceph_client *client;
157 struct kref kref;
158 struct list_head node;
159};
160
Alex Elderbf0d5f502012-11-22 00:00:08 -0600161struct rbd_img_request;
162typedef void (*rbd_img_callback_t)(struct rbd_img_request *);
163
164#define BAD_WHICH U32_MAX /* Good which or bad which, which? */
165
166struct rbd_obj_request;
167typedef void (*rbd_obj_callback_t)(struct rbd_obj_request *);
168
Alex Elder9969ebc2013-01-18 12:31:10 -0600169enum obj_request_type {
170 OBJ_REQUEST_NODATA, OBJ_REQUEST_BIO, OBJ_REQUEST_PAGES
171};
Alex Elderbf0d5f502012-11-22 00:00:08 -0600172
Alex Elder926f9b32013-02-11 12:33:24 -0600173enum obj_req_flags {
174 OBJ_REQ_DONE, /* completion flag: not done = 0, done = 1 */
Alex Elder6365d332013-02-11 12:33:24 -0600175 OBJ_REQ_IMG_DATA, /* object usage: standalone = 0, image = 1 */
Alex Elder5679c592013-02-11 12:33:24 -0600176 OBJ_REQ_KNOWN, /* EXISTS flag valid: no = 0, yes = 1 */
177 OBJ_REQ_EXISTS, /* target exists: no = 0, yes = 1 */
Alex Elder926f9b32013-02-11 12:33:24 -0600178};
179
Alex Elderbf0d5f502012-11-22 00:00:08 -0600180struct rbd_obj_request {
181 const char *object_name;
182 u64 offset; /* object start byte */
183 u64 length; /* bytes from offset */
Alex Elder926f9b32013-02-11 12:33:24 -0600184 unsigned long flags;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600185
Alex Elderc5b5ef62013-02-11 12:33:24 -0600186 /*
187 * An object request associated with an image will have its
188 * img_data flag set; a standalone object request will not.
189 *
190 * A standalone object request will have which == BAD_WHICH
191 * and a null obj_request pointer.
192 *
193 * An object request initiated in support of a layered image
194 * object (to check for its existence before a write) will
195 * have which == BAD_WHICH and a non-null obj_request pointer.
196 *
197 * Finally, an object request for rbd image data will have
198 * which != BAD_WHICH, and will have a non-null img_request
199 * pointer. The value of which will be in the range
200 * 0..(img_request->obj_request_count-1).
201 */
202 union {
203 struct rbd_obj_request *obj_request; /* STAT op */
204 struct {
205 struct rbd_img_request *img_request;
206 u64 img_offset;
207 /* links for img_request->obj_requests list */
208 struct list_head links;
209 };
210 };
Alex Elderbf0d5f502012-11-22 00:00:08 -0600211 u32 which; /* posn image request list */
212
213 enum obj_request_type type;
Alex Elder788e2df2013-01-17 12:25:27 -0600214 union {
215 struct bio *bio_list;
216 struct {
217 struct page **pages;
218 u32 page_count;
219 };
220 };
Alex Elder0eefd472013-04-19 15:34:50 -0500221 struct page **copyup_pages;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600222
223 struct ceph_osd_request *osd_req;
224
225 u64 xferred; /* bytes transferred */
226 u64 version;
Sage Weil1b83bef2013-02-25 16:11:12 -0800227 int result;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600228
229 rbd_obj_callback_t callback;
Alex Elder788e2df2013-01-17 12:25:27 -0600230 struct completion completion;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600231
232 struct kref kref;
233};
234
Alex Elder0c425242013-02-08 09:55:49 -0600235enum img_req_flags {
Alex Elder9849e982013-01-24 16:13:36 -0600236 IMG_REQ_WRITE, /* I/O direction: read = 0, write = 1 */
237 IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */
Alex Elderd0b2e942013-01-24 16:13:36 -0600238 IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */
Alex Elder0c425242013-02-08 09:55:49 -0600239};
240
Alex Elderbf0d5f502012-11-22 00:00:08 -0600241struct rbd_img_request {
Alex Elderbf0d5f502012-11-22 00:00:08 -0600242 struct rbd_device *rbd_dev;
243 u64 offset; /* starting image byte offset */
244 u64 length; /* byte count from offset */
Alex Elder0c425242013-02-08 09:55:49 -0600245 unsigned long flags;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600246 union {
Alex Elder9849e982013-01-24 16:13:36 -0600247 u64 snap_id; /* for reads */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600248 struct ceph_snap_context *snapc; /* for writes */
Alex Elder9849e982013-01-24 16:13:36 -0600249 };
250 union {
251 struct request *rq; /* block request */
252 struct rbd_obj_request *obj_request; /* obj req initiator */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600253 };
Alex Elder3d7efd12013-04-19 15:34:50 -0500254 struct page **copyup_pages;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600255 spinlock_t completion_lock;/* protects next_completion */
256 u32 next_completion;
257 rbd_img_callback_t callback;
Alex Elder55f27e02013-04-10 12:34:25 -0500258 u64 xferred;/* aggregate bytes transferred */
Alex Eldera5a337d2013-01-24 16:13:36 -0600259 int result; /* first nonzero obj_request result */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600260
261 u32 obj_request_count;
262 struct list_head obj_requests; /* rbd_obj_request structs */
263
264 struct kref kref;
265};
266
267#define for_each_obj_request(ireq, oreq) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600268 list_for_each_entry(oreq, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600269#define for_each_obj_request_from(ireq, oreq) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600270 list_for_each_entry_from(oreq, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600271#define for_each_obj_request_safe(ireq, oreq, n) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600272 list_for_each_entry_safe_reverse(oreq, n, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600273
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800274struct rbd_snap {
275 struct device dev;
276 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
Alex Eldercc070d52013-04-21 12:14:45 -0500320 u64 stripe_unit;
321 u64 stripe_count;
322
Josh Durginc6666012011-11-21 17:11:12 -0800323 /* protects updating the header */
324 struct rw_semaphore header_rwsem;
Alex Elderf84344f2012-08-31 17:29:51 -0500325
326 struct rbd_mapping mapping;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700327
328 struct list_head node;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800329
330 /* list of snapshots */
331 struct list_head snaps;
332
333 /* sysfs related */
334 struct device dev;
Alex Elderb82d1672013-01-14 12:43:31 -0600335 unsigned long open_count; /* protected by lock */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800336};
337
Alex Elderb82d1672013-01-14 12:43:31 -0600338/*
339 * Flag bits for rbd_dev->flags. If atomicity is required,
340 * rbd_dev->lock is used to protect access.
341 *
342 * Currently, only the "removing" flag (which is coupled with the
343 * "open_count" field) requires atomic access.
344 */
Alex Elder6d292902013-01-14 12:43:31 -0600345enum rbd_dev_flags {
346 RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */
Alex Elderb82d1672013-01-14 12:43:31 -0600347 RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */
Alex Elder6d292902013-01-14 12:43:31 -0600348};
349
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700350static DEFINE_MUTEX(ctl_mutex); /* Serialize open/close/setup/teardown */
Alex Eldere124a82f2012-01-29 13:57:44 -0600351
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700352static LIST_HEAD(rbd_dev_list); /* devices */
Alex Eldere124a82f2012-01-29 13:57:44 -0600353static DEFINE_SPINLOCK(rbd_dev_list_lock);
354
Alex Elder432b8582012-01-29 13:57:44 -0600355static LIST_HEAD(rbd_client_list); /* clients */
356static DEFINE_SPINLOCK(rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700357
Alex Elder3d7efd12013-04-19 15:34:50 -0500358static int rbd_img_request_submit(struct rbd_img_request *img_request);
359
Alex Elder304f6802012-08-31 17:29:52 -0500360static int rbd_dev_snaps_update(struct rbd_device *rbd_dev);
361static int rbd_dev_snaps_register(struct rbd_device *rbd_dev);
362
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800363static void rbd_dev_release(struct device *dev);
Alex Elder41f38c22012-10-25 23:34:40 -0500364static void rbd_remove_snap_dev(struct rbd_snap *snap);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800365
Alex Elderf0f8cef2012-01-29 13:57:44 -0600366static ssize_t rbd_add(struct bus_type *bus, const char *buf,
367 size_t count);
368static ssize_t rbd_remove(struct bus_type *bus, const char *buf,
369 size_t count);
Alex Elder2f82ee52012-10-30 19:40:33 -0500370static int rbd_dev_probe(struct rbd_device *rbd_dev);
Alex Elderf0f8cef2012-01-29 13:57:44 -0600371
372static struct bus_attribute rbd_bus_attrs[] = {
373 __ATTR(add, S_IWUSR, NULL, rbd_add),
374 __ATTR(remove, S_IWUSR, NULL, rbd_remove),
375 __ATTR_NULL
376};
377
378static struct bus_type rbd_bus_type = {
379 .name = "rbd",
380 .bus_attrs = rbd_bus_attrs,
381};
382
383static void rbd_root_dev_release(struct device *dev)
384{
385}
386
387static struct device rbd_root_dev = {
388 .init_name = "rbd",
389 .release = rbd_root_dev_release,
390};
391
Alex Elder06ecc6c2012-11-01 10:17:15 -0500392static __printf(2, 3)
393void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...)
394{
395 struct va_format vaf;
396 va_list args;
397
398 va_start(args, fmt);
399 vaf.fmt = fmt;
400 vaf.va = &args;
401
402 if (!rbd_dev)
403 printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf);
404 else if (rbd_dev->disk)
405 printk(KERN_WARNING "%s: %s: %pV\n",
406 RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf);
407 else if (rbd_dev->spec && rbd_dev->spec->image_name)
408 printk(KERN_WARNING "%s: image %s: %pV\n",
409 RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf);
410 else if (rbd_dev->spec && rbd_dev->spec->image_id)
411 printk(KERN_WARNING "%s: id %s: %pV\n",
412 RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf);
413 else /* punt */
414 printk(KERN_WARNING "%s: rbd_dev %p: %pV\n",
415 RBD_DRV_NAME, rbd_dev, &vaf);
416 va_end(args);
417}
418
Alex Elderaafb2302012-09-06 16:00:54 -0500419#ifdef RBD_DEBUG
420#define rbd_assert(expr) \
421 if (unlikely(!(expr))) { \
422 printk(KERN_ERR "\nAssertion failure in %s() " \
423 "at line %d:\n\n" \
424 "\trbd_assert(%s);\n\n", \
425 __func__, __LINE__, #expr); \
426 BUG(); \
427 }
428#else /* !RBD_DEBUG */
429# define rbd_assert(expr) ((void) 0)
430#endif /* !RBD_DEBUG */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800431
Alex Elder8b3e1a52013-01-24 16:13:36 -0600432static void rbd_img_parent_read(struct rbd_obj_request *obj_request);
Alex Elderb454e362013-04-19 15:34:50 -0500433static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request);
Alex Elder8b3e1a52013-01-24 16:13:36 -0600434
Alex Elder117973f2012-08-31 17:29:55 -0500435static int rbd_dev_refresh(struct rbd_device *rbd_dev, u64 *hver);
436static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev, u64 *hver);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700437
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700438static int rbd_open(struct block_device *bdev, fmode_t mode)
439{
Alex Elderf0f8cef2012-01-29 13:57:44 -0600440 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
Alex Elderb82d1672013-01-14 12:43:31 -0600441 bool removing = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700442
Alex Elderf84344f2012-08-31 17:29:51 -0500443 if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700444 return -EROFS;
445
Alex Eldera14ea262013-02-05 13:23:12 -0600446 spin_lock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600447 if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags))
448 removing = true;
449 else
450 rbd_dev->open_count++;
Alex Eldera14ea262013-02-05 13:23:12 -0600451 spin_unlock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600452 if (removing)
453 return -ENOENT;
454
Alex Elder42382b72012-11-16 09:29:16 -0600455 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Alex Elderc3e946c2012-11-16 09:29:16 -0600456 (void) get_device(&rbd_dev->dev);
Alex Elderf84344f2012-08-31 17:29:51 -0500457 set_device_ro(bdev, rbd_dev->mapping.read_only);
Alex Elder42382b72012-11-16 09:29:16 -0600458 mutex_unlock(&ctl_mutex);
Alex Elder340c7a22012-08-10 13:12:07 -0700459
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700460 return 0;
461}
462
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800463static int rbd_release(struct gendisk *disk, fmode_t mode)
464{
465 struct rbd_device *rbd_dev = disk->private_data;
Alex Elderb82d1672013-01-14 12:43:31 -0600466 unsigned long open_count_before;
467
Alex Eldera14ea262013-02-05 13:23:12 -0600468 spin_lock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600469 open_count_before = rbd_dev->open_count--;
Alex Eldera14ea262013-02-05 13:23:12 -0600470 spin_unlock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600471 rbd_assert(open_count_before > 0);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800472
Alex Elder42382b72012-11-16 09:29:16 -0600473 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Alex Elderc3e946c2012-11-16 09:29:16 -0600474 put_device(&rbd_dev->dev);
Alex Elder42382b72012-11-16 09:29:16 -0600475 mutex_unlock(&ctl_mutex);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800476
477 return 0;
478}
479
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700480static const struct block_device_operations rbd_bd_ops = {
481 .owner = THIS_MODULE,
482 .open = rbd_open,
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800483 .release = rbd_release,
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700484};
485
486/*
487 * Initialize an rbd client instance.
Alex Elder43ae4702012-07-03 16:01:18 -0500488 * We own *ceph_opts.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700489 */
Alex Elderf8c38922012-08-10 13:12:07 -0700490static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700491{
492 struct rbd_client *rbdc;
493 int ret = -ENOMEM;
494
Alex Elder37206ee2013-02-20 17:32:08 -0600495 dout("%s:\n", __func__);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700496 rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL);
497 if (!rbdc)
498 goto out_opt;
499
500 kref_init(&rbdc->kref);
501 INIT_LIST_HEAD(&rbdc->node);
502
Alex Elderbc534d82012-01-29 13:57:44 -0600503 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
504
Alex Elder43ae4702012-07-03 16:01:18 -0500505 rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700506 if (IS_ERR(rbdc->client))
Alex Elderbc534d82012-01-29 13:57:44 -0600507 goto out_mutex;
Alex Elder43ae4702012-07-03 16:01:18 -0500508 ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700509
510 ret = ceph_open_session(rbdc->client);
511 if (ret < 0)
512 goto out_err;
513
Alex Elder432b8582012-01-29 13:57:44 -0600514 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700515 list_add_tail(&rbdc->node, &rbd_client_list);
Alex Elder432b8582012-01-29 13:57:44 -0600516 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700517
Alex Elderbc534d82012-01-29 13:57:44 -0600518 mutex_unlock(&ctl_mutex);
Alex Elder37206ee2013-02-20 17:32:08 -0600519 dout("%s: rbdc %p\n", __func__, rbdc);
Alex Elderbc534d82012-01-29 13:57:44 -0600520
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700521 return rbdc;
522
523out_err:
524 ceph_destroy_client(rbdc->client);
Alex Elderbc534d82012-01-29 13:57:44 -0600525out_mutex:
526 mutex_unlock(&ctl_mutex);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700527 kfree(rbdc);
528out_opt:
Alex Elder43ae4702012-07-03 16:01:18 -0500529 if (ceph_opts)
530 ceph_destroy_options(ceph_opts);
Alex Elder37206ee2013-02-20 17:32:08 -0600531 dout("%s: error %d\n", __func__, ret);
532
Vasiliy Kulikov28f259b2010-09-26 12:59:37 +0400533 return ERR_PTR(ret);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700534}
535
Alex Elder2f82ee52012-10-30 19:40:33 -0500536static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc)
537{
538 kref_get(&rbdc->kref);
539
540 return rbdc;
541}
542
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700543/*
Alex Elder1f7ba332012-08-10 13:12:07 -0700544 * Find a ceph client with specific addr and configuration. If
545 * found, bump its reference count.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700546 */
Alex Elder1f7ba332012-08-10 13:12:07 -0700547static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700548{
549 struct rbd_client *client_node;
Alex Elder1f7ba332012-08-10 13:12:07 -0700550 bool found = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700551
Alex Elder43ae4702012-07-03 16:01:18 -0500552 if (ceph_opts->flags & CEPH_OPT_NOSHARE)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700553 return NULL;
554
Alex Elder1f7ba332012-08-10 13:12:07 -0700555 spin_lock(&rbd_client_list_lock);
556 list_for_each_entry(client_node, &rbd_client_list, node) {
557 if (!ceph_compare_options(ceph_opts, client_node->client)) {
Alex Elder2f82ee52012-10-30 19:40:33 -0500558 __rbd_get_client(client_node);
559
Alex Elder1f7ba332012-08-10 13:12:07 -0700560 found = true;
561 break;
562 }
563 }
564 spin_unlock(&rbd_client_list_lock);
565
566 return found ? client_node : NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700567}
568
569/*
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700570 * mount options
571 */
572enum {
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700573 Opt_last_int,
574 /* int args above */
575 Opt_last_string,
576 /* string args above */
Alex Eldercc0538b2012-08-10 13:12:07 -0700577 Opt_read_only,
578 Opt_read_write,
579 /* Boolean args above */
580 Opt_last_bool,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700581};
582
Alex Elder43ae4702012-07-03 16:01:18 -0500583static match_table_t rbd_opts_tokens = {
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700584 /* int args above */
585 /* string args above */
Alex Elderbe466c12012-10-22 11:31:26 -0500586 {Opt_read_only, "read_only"},
Alex Eldercc0538b2012-08-10 13:12:07 -0700587 {Opt_read_only, "ro"}, /* Alternate spelling */
588 {Opt_read_write, "read_write"},
589 {Opt_read_write, "rw"}, /* Alternate spelling */
590 /* Boolean args above */
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700591 {-1, NULL}
592};
593
Alex Elder98571b52013-01-20 14:44:42 -0600594struct rbd_options {
595 bool read_only;
596};
597
598#define RBD_READ_ONLY_DEFAULT false
599
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700600static int parse_rbd_opts_token(char *c, void *private)
601{
Alex Elder43ae4702012-07-03 16:01:18 -0500602 struct rbd_options *rbd_opts = private;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700603 substring_t argstr[MAX_OPT_ARGS];
604 int token, intval, ret;
605
Alex Elder43ae4702012-07-03 16:01:18 -0500606 token = match_token(c, rbd_opts_tokens, argstr);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700607 if (token < 0)
608 return -EINVAL;
609
610 if (token < Opt_last_int) {
611 ret = match_int(&argstr[0], &intval);
612 if (ret < 0) {
613 pr_err("bad mount option arg (not int) "
614 "at '%s'\n", c);
615 return ret;
616 }
617 dout("got int token %d val %d\n", token, intval);
618 } else if (token > Opt_last_int && token < Opt_last_string) {
619 dout("got string token %d val %s\n", token,
620 argstr[0].from);
Alex Eldercc0538b2012-08-10 13:12:07 -0700621 } else if (token > Opt_last_string && token < Opt_last_bool) {
622 dout("got Boolean token %d\n", token);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700623 } else {
624 dout("got token %d\n", token);
625 }
626
627 switch (token) {
Alex Eldercc0538b2012-08-10 13:12:07 -0700628 case Opt_read_only:
629 rbd_opts->read_only = true;
630 break;
631 case Opt_read_write:
632 rbd_opts->read_only = false;
633 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700634 default:
Alex Elderaafb2302012-09-06 16:00:54 -0500635 rbd_assert(false);
636 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700637 }
638 return 0;
639}
640
641/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700642 * Get a ceph client with specific addr and configuration, if one does
643 * not exist create it.
644 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500645static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700646{
Alex Elderf8c38922012-08-10 13:12:07 -0700647 struct rbd_client *rbdc;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700648
Alex Elder1f7ba332012-08-10 13:12:07 -0700649 rbdc = rbd_client_find(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500650 if (rbdc) /* using an existing client */
Alex Elder43ae4702012-07-03 16:01:18 -0500651 ceph_destroy_options(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500652 else
Alex Elderf8c38922012-08-10 13:12:07 -0700653 rbdc = rbd_client_create(ceph_opts);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700654
Alex Elder9d3997f2012-10-25 23:34:42 -0500655 return rbdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700656}
657
658/*
659 * Destroy ceph client
Alex Elderd23a4b32012-01-29 13:57:43 -0600660 *
Alex Elder432b8582012-01-29 13:57:44 -0600661 * Caller must hold rbd_client_list_lock.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700662 */
663static void rbd_client_release(struct kref *kref)
664{
665 struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
666
Alex Elder37206ee2013-02-20 17:32:08 -0600667 dout("%s: rbdc %p\n", __func__, rbdc);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500668 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700669 list_del(&rbdc->node);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500670 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700671
672 ceph_destroy_client(rbdc->client);
673 kfree(rbdc);
674}
675
676/*
677 * Drop reference to ceph client node. If it's not referenced anymore, release
678 * it.
679 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500680static void rbd_put_client(struct rbd_client *rbdc)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700681{
Alex Elderc53d5892012-10-25 23:34:42 -0500682 if (rbdc)
683 kref_put(&rbdc->kref, rbd_client_release);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700684}
685
Alex Eldera30b71b2012-07-10 20:30:11 -0500686static bool rbd_image_format_valid(u32 image_format)
687{
688 return image_format == 1 || image_format == 2;
689}
690
Alex Elder8e94af82012-07-25 09:32:40 -0500691static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
692{
Alex Elder103a1502012-08-02 11:29:45 -0500693 size_t size;
694 u32 snap_count;
695
696 /* The header has to start with the magic rbd header text */
697 if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT)))
698 return false;
699
Alex Elderdb2388b2012-10-20 22:17:27 -0500700 /* The bio layer requires at least sector-sized I/O */
701
702 if (ondisk->options.order < SECTOR_SHIFT)
703 return false;
704
705 /* If we use u64 in a few spots we may be able to loosen this */
706
707 if (ondisk->options.order > 8 * sizeof (int) - 1)
708 return false;
709
Alex Elder103a1502012-08-02 11:29:45 -0500710 /*
711 * The size of a snapshot header has to fit in a size_t, and
712 * that limits the number of snapshots.
713 */
714 snap_count = le32_to_cpu(ondisk->snap_count);
715 size = SIZE_MAX - sizeof (struct ceph_snap_context);
716 if (snap_count > size / sizeof (__le64))
717 return false;
718
719 /*
720 * Not only that, but the size of the entire the snapshot
721 * header must also be representable in a size_t.
722 */
723 size -= snap_count * sizeof (__le64);
724 if ((u64) size < le64_to_cpu(ondisk->snap_names_len))
725 return false;
726
727 return true;
Alex Elder8e94af82012-07-25 09:32:40 -0500728}
729
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700730/*
731 * Create a new header structure, translate header format from the on-disk
732 * header.
733 */
734static int rbd_header_from_disk(struct rbd_image_header *header,
Alex Elder4156d992012-08-02 11:29:46 -0500735 struct rbd_image_header_ondisk *ondisk)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700736{
Alex Elderccece232012-07-10 20:30:10 -0500737 u32 snap_count;
Alex Elder58c17b02012-08-23 23:22:06 -0500738 size_t len;
Alex Elderd2bb24e2012-07-26 23:37:14 -0500739 size_t size;
Alex Elder621901d2012-08-23 23:22:06 -0500740 u32 i;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700741
Alex Elder6a523252012-07-19 17:12:59 -0500742 memset(header, 0, sizeof (*header));
743
Alex Elder103a1502012-08-02 11:29:45 -0500744 snap_count = le32_to_cpu(ondisk->snap_count);
745
Alex Elder58c17b02012-08-23 23:22:06 -0500746 len = strnlen(ondisk->object_prefix, sizeof (ondisk->object_prefix));
747 header->object_prefix = kmalloc(len + 1, GFP_KERNEL);
Alex Elder6a523252012-07-19 17:12:59 -0500748 if (!header->object_prefix)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700749 return -ENOMEM;
Alex Elder58c17b02012-08-23 23:22:06 -0500750 memcpy(header->object_prefix, ondisk->object_prefix, len);
751 header->object_prefix[len] = '\0';
Alex Elder00f1f362012-02-07 12:03:36 -0600752
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700753 if (snap_count) {
Alex Elderf785cc12012-08-23 23:22:06 -0500754 u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len);
755
Alex Elder621901d2012-08-23 23:22:06 -0500756 /* Save a copy of the snapshot names */
757
Alex Elderf785cc12012-08-23 23:22:06 -0500758 if (snap_names_len > (u64) SIZE_MAX)
759 return -EIO;
760 header->snap_names = kmalloc(snap_names_len, GFP_KERNEL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700761 if (!header->snap_names)
Alex Elder6a523252012-07-19 17:12:59 -0500762 goto out_err;
Alex Elderf785cc12012-08-23 23:22:06 -0500763 /*
764 * Note that rbd_dev_v1_header_read() guarantees
765 * the ondisk buffer we're working with has
766 * snap_names_len bytes beyond the end of the
767 * snapshot id array, this memcpy() is safe.
768 */
769 memcpy(header->snap_names, &ondisk->snaps[snap_count],
770 snap_names_len);
Alex Elder6a523252012-07-19 17:12:59 -0500771
Alex Elder621901d2012-08-23 23:22:06 -0500772 /* Record each snapshot's size */
773
Alex Elderd2bb24e2012-07-26 23:37:14 -0500774 size = snap_count * sizeof (*header->snap_sizes);
775 header->snap_sizes = kmalloc(size, GFP_KERNEL);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700776 if (!header->snap_sizes)
Alex Elder6a523252012-07-19 17:12:59 -0500777 goto out_err;
Alex Elder621901d2012-08-23 23:22:06 -0500778 for (i = 0; i < snap_count; i++)
779 header->snap_sizes[i] =
780 le64_to_cpu(ondisk->snaps[i].image_size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700781 } else {
Alex Elderccece232012-07-10 20:30:10 -0500782 WARN_ON(ondisk->snap_names_len);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700783 header->snap_names = NULL;
784 header->snap_sizes = NULL;
785 }
Alex Elder849b4262012-07-09 21:04:24 -0500786
Alex Elder34b13182012-07-13 20:35:12 -0500787 header->features = 0; /* No features support in v1 images */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700788 header->obj_order = ondisk->options.order;
789 header->crypt_type = ondisk->options.crypt_type;
790 header->comp_type = ondisk->options.comp_type;
Alex Elder6a523252012-07-19 17:12:59 -0500791
Alex Elder621901d2012-08-23 23:22:06 -0500792 /* Allocate and fill in the snapshot context */
793
Alex Elderf84344f2012-08-31 17:29:51 -0500794 header->image_size = le64_to_cpu(ondisk->image_size);
Alex Elder6a523252012-07-19 17:12:59 -0500795 size = sizeof (struct ceph_snap_context);
796 size += snap_count * sizeof (header->snapc->snaps[0]);
797 header->snapc = kzalloc(size, GFP_KERNEL);
798 if (!header->snapc)
799 goto out_err;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700800
801 atomic_set(&header->snapc->nref, 1);
Alex Elder505cbb92012-07-19 08:49:18 -0500802 header->snapc->seq = le64_to_cpu(ondisk->snap_seq);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700803 header->snapc->num_snaps = snap_count;
Alex Elder621901d2012-08-23 23:22:06 -0500804 for (i = 0; i < snap_count; i++)
805 header->snapc->snaps[i] =
806 le64_to_cpu(ondisk->snaps[i].id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700807
808 return 0;
809
Alex Elder6a523252012-07-19 17:12:59 -0500810out_err:
Alex Elder849b4262012-07-09 21:04:24 -0500811 kfree(header->snap_sizes);
Alex Elderccece232012-07-10 20:30:10 -0500812 header->snap_sizes = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700813 kfree(header->snap_names);
Alex Elderccece232012-07-10 20:30:10 -0500814 header->snap_names = NULL;
Alex Elder6a523252012-07-19 17:12:59 -0500815 kfree(header->object_prefix);
816 header->object_prefix = NULL;
Alex Elderccece232012-07-10 20:30:10 -0500817
Alex Elder00f1f362012-02-07 12:03:36 -0600818 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700819}
820
Alex Elder9e15b772012-10-30 19:40:33 -0500821static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id)
822{
823 struct rbd_snap *snap;
824
825 if (snap_id == CEPH_NOSNAP)
826 return RBD_SNAP_HEAD_NAME;
827
828 list_for_each_entry(snap, &rbd_dev->snaps, node)
829 if (snap_id == snap->id)
830 return snap->name;
831
832 return NULL;
833}
834
Alex Elder8836b992012-08-30 14:42:15 -0500835static int snap_by_name(struct rbd_device *rbd_dev, const char *snap_name)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700836{
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700837
Alex Eldere86924a2012-07-10 20:30:11 -0500838 struct rbd_snap *snap;
Alex Elder00f1f362012-02-07 12:03:36 -0600839
Alex Eldere86924a2012-07-10 20:30:11 -0500840 list_for_each_entry(snap, &rbd_dev->snaps, node) {
841 if (!strcmp(snap_name, snap->name)) {
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500842 rbd_dev->spec->snap_id = snap->id;
Alex Eldere86924a2012-07-10 20:30:11 -0500843 rbd_dev->mapping.size = snap->size;
Alex Elder34b13182012-07-13 20:35:12 -0500844 rbd_dev->mapping.features = snap->features;
Alex Elder00f1f362012-02-07 12:03:36 -0600845
Alex Eldere86924a2012-07-10 20:30:11 -0500846 return 0;
Alex Elder00f1f362012-02-07 12:03:36 -0600847 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700848 }
Alex Eldere86924a2012-07-10 20:30:11 -0500849
Alex Elder00f1f362012-02-07 12:03:36 -0600850 return -ENOENT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700851}
852
Alex Elder819d52b2012-10-25 23:34:41 -0500853static int rbd_dev_set_mapping(struct rbd_device *rbd_dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700854{
Alex Elder78dc4472012-07-19 08:49:18 -0500855 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700856
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500857 if (!memcmp(rbd_dev->spec->snap_name, RBD_SNAP_HEAD_NAME,
Josh Durgincc9d7342011-11-21 18:19:13 -0800858 sizeof (RBD_SNAP_HEAD_NAME))) {
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500859 rbd_dev->spec->snap_id = CEPH_NOSNAP;
Alex Elder99c1f082012-08-30 14:42:15 -0500860 rbd_dev->mapping.size = rbd_dev->header.image_size;
Alex Elder34b13182012-07-13 20:35:12 -0500861 rbd_dev->mapping.features = rbd_dev->header.features;
Alex Eldere86924a2012-07-10 20:30:11 -0500862 ret = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700863 } else {
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500864 ret = snap_by_name(rbd_dev, rbd_dev->spec->snap_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700865 if (ret < 0)
866 goto done;
Alex Elderf84344f2012-08-31 17:29:51 -0500867 rbd_dev->mapping.read_only = true;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700868 }
Alex Elder6d292902013-01-14 12:43:31 -0600869 set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
870
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700871done:
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700872 return ret;
873}
874
875static void rbd_header_free(struct rbd_image_header *header)
876{
Alex Elder849b4262012-07-09 21:04:24 -0500877 kfree(header->object_prefix);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500878 header->object_prefix = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700879 kfree(header->snap_sizes);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500880 header->snap_sizes = NULL;
Alex Elder849b4262012-07-09 21:04:24 -0500881 kfree(header->snap_names);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500882 header->snap_names = NULL;
Josh Durgind1d25642011-12-05 14:03:05 -0800883 ceph_put_snap_context(header->snapc);
Alex Elderd78fd7a2012-07-26 23:37:14 -0500884 header->snapc = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700885}
886
Alex Elder98571b52013-01-20 14:44:42 -0600887static const char *rbd_segment_name(struct rbd_device *rbd_dev, u64 offset)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700888{
Alex Elder65ccfe22012-08-09 10:33:26 -0700889 char *name;
890 u64 segment;
891 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700892
Alex Elder2fd82b92012-11-09 15:05:54 -0600893 name = kmalloc(MAX_OBJ_NAME_SIZE + 1, GFP_NOIO);
Alex Elder65ccfe22012-08-09 10:33:26 -0700894 if (!name)
895 return NULL;
896 segment = offset >> rbd_dev->header.obj_order;
Alex Elder2fd82b92012-11-09 15:05:54 -0600897 ret = snprintf(name, MAX_OBJ_NAME_SIZE + 1, "%s.%012llx",
Alex Elder65ccfe22012-08-09 10:33:26 -0700898 rbd_dev->header.object_prefix, segment);
Alex Elder2fd82b92012-11-09 15:05:54 -0600899 if (ret < 0 || ret > MAX_OBJ_NAME_SIZE) {
Alex Elder65ccfe22012-08-09 10:33:26 -0700900 pr_err("error formatting segment name for #%llu (%d)\n",
901 segment, ret);
902 kfree(name);
903 name = NULL;
904 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700905
Alex Elder65ccfe22012-08-09 10:33:26 -0700906 return name;
907}
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700908
Alex Elder65ccfe22012-08-09 10:33:26 -0700909static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset)
910{
911 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700912
Alex Elder65ccfe22012-08-09 10:33:26 -0700913 return offset & (segment_size - 1);
914}
915
916static u64 rbd_segment_length(struct rbd_device *rbd_dev,
917 u64 offset, u64 length)
918{
919 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
920
921 offset &= segment_size - 1;
922
Alex Elderaafb2302012-09-06 16:00:54 -0500923 rbd_assert(length <= U64_MAX - offset);
Alex Elder65ccfe22012-08-09 10:33:26 -0700924 if (offset + length > segment_size)
925 length = segment_size - offset;
926
927 return length;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700928}
929
930/*
Josh Durgin029bcbd2011-07-22 11:35:23 -0700931 * returns the size of an object in the image
932 */
933static u64 rbd_obj_bytes(struct rbd_image_header *header)
934{
935 return 1 << header->obj_order;
936}
937
938/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700939 * bio helpers
940 */
941
942static void bio_chain_put(struct bio *chain)
943{
944 struct bio *tmp;
945
946 while (chain) {
947 tmp = chain;
948 chain = chain->bi_next;
949 bio_put(tmp);
950 }
951}
952
953/*
954 * zeros a bio chain, starting at specific offset
955 */
956static void zero_bio_chain(struct bio *chain, int start_ofs)
957{
958 struct bio_vec *bv;
959 unsigned long flags;
960 void *buf;
961 int i;
962 int pos = 0;
963
964 while (chain) {
965 bio_for_each_segment(bv, chain, i) {
966 if (pos + bv->bv_len > start_ofs) {
967 int remainder = max(start_ofs - pos, 0);
968 buf = bvec_kmap_irq(bv, &flags);
969 memset(buf + remainder, 0,
970 bv->bv_len - remainder);
Dan Carpenter85b5aaa2010-10-11 21:15:11 +0200971 bvec_kunmap_irq(buf, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700972 }
973 pos += bv->bv_len;
974 }
975
976 chain = chain->bi_next;
977 }
978}
979
980/*
Alex Elderb9434c52013-04-19 15:34:50 -0500981 * similar to zero_bio_chain(), zeros data defined by a page array,
982 * starting at the given byte offset from the start of the array and
983 * continuing up to the given end offset. The pages array is
984 * assumed to be big enough to hold all bytes up to the end.
985 */
986static void zero_pages(struct page **pages, u64 offset, u64 end)
987{
988 struct page **page = &pages[offset >> PAGE_SHIFT];
989
990 rbd_assert(end > offset);
991 rbd_assert(end - offset <= (u64)SIZE_MAX);
992 while (offset < end) {
993 size_t page_offset;
994 size_t length;
995 unsigned long flags;
996 void *kaddr;
997
998 page_offset = (size_t)(offset & ~PAGE_MASK);
999 length = min(PAGE_SIZE - page_offset, (size_t)(end - offset));
1000 local_irq_save(flags);
1001 kaddr = kmap_atomic(*page);
1002 memset(kaddr + page_offset, 0, length);
1003 kunmap_atomic(kaddr);
1004 local_irq_restore(flags);
1005
1006 offset += length;
1007 page++;
1008 }
1009}
1010
1011/*
Alex Elderf7760da2012-10-20 22:17:27 -05001012 * Clone a portion of a bio, starting at the given byte offset
1013 * and continuing for the number of bytes indicated.
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001014 */
Alex Elderf7760da2012-10-20 22:17:27 -05001015static struct bio *bio_clone_range(struct bio *bio_src,
1016 unsigned int offset,
1017 unsigned int len,
1018 gfp_t gfpmask)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001019{
Alex Elderf7760da2012-10-20 22:17:27 -05001020 struct bio_vec *bv;
1021 unsigned int resid;
1022 unsigned short idx;
1023 unsigned int voff;
1024 unsigned short end_idx;
1025 unsigned short vcnt;
1026 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001027
Alex Elderf7760da2012-10-20 22:17:27 -05001028 /* Handle the easy case for the caller */
1029
1030 if (!offset && len == bio_src->bi_size)
1031 return bio_clone(bio_src, gfpmask);
1032
1033 if (WARN_ON_ONCE(!len))
1034 return NULL;
1035 if (WARN_ON_ONCE(len > bio_src->bi_size))
1036 return NULL;
1037 if (WARN_ON_ONCE(offset > bio_src->bi_size - len))
1038 return NULL;
1039
1040 /* Find first affected segment... */
1041
1042 resid = offset;
1043 __bio_for_each_segment(bv, bio_src, idx, 0) {
1044 if (resid < bv->bv_len)
1045 break;
1046 resid -= bv->bv_len;
1047 }
1048 voff = resid;
1049
1050 /* ...and the last affected segment */
1051
1052 resid += len;
1053 __bio_for_each_segment(bv, bio_src, end_idx, idx) {
1054 if (resid <= bv->bv_len)
1055 break;
1056 resid -= bv->bv_len;
1057 }
1058 vcnt = end_idx - idx + 1;
1059
1060 /* Build the clone */
1061
1062 bio = bio_alloc(gfpmask, (unsigned int) vcnt);
1063 if (!bio)
1064 return NULL; /* ENOMEM */
1065
1066 bio->bi_bdev = bio_src->bi_bdev;
1067 bio->bi_sector = bio_src->bi_sector + (offset >> SECTOR_SHIFT);
1068 bio->bi_rw = bio_src->bi_rw;
1069 bio->bi_flags |= 1 << BIO_CLONED;
1070
1071 /*
1072 * Copy over our part of the bio_vec, then update the first
1073 * and last (or only) entries.
1074 */
1075 memcpy(&bio->bi_io_vec[0], &bio_src->bi_io_vec[idx],
1076 vcnt * sizeof (struct bio_vec));
1077 bio->bi_io_vec[0].bv_offset += voff;
1078 if (vcnt > 1) {
1079 bio->bi_io_vec[0].bv_len -= voff;
1080 bio->bi_io_vec[vcnt - 1].bv_len = resid;
1081 } else {
1082 bio->bi_io_vec[0].bv_len = len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001083 }
1084
Alex Elderf7760da2012-10-20 22:17:27 -05001085 bio->bi_vcnt = vcnt;
1086 bio->bi_size = len;
1087 bio->bi_idx = 0;
Alex Elder542582f2012-08-09 10:33:25 -07001088
Alex Elderf7760da2012-10-20 22:17:27 -05001089 return bio;
1090}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001091
Alex Elderf7760da2012-10-20 22:17:27 -05001092/*
1093 * Clone a portion of a bio chain, starting at the given byte offset
1094 * into the first bio in the source chain and continuing for the
1095 * number of bytes indicated. The result is another bio chain of
1096 * exactly the given length, or a null pointer on error.
1097 *
1098 * The bio_src and offset parameters are both in-out. On entry they
1099 * refer to the first source bio and the offset into that bio where
1100 * the start of data to be cloned is located.
1101 *
1102 * On return, bio_src is updated to refer to the bio in the source
1103 * chain that contains first un-cloned byte, and *offset will
1104 * contain the offset of that byte within that bio.
1105 */
1106static struct bio *bio_chain_clone_range(struct bio **bio_src,
1107 unsigned int *offset,
1108 unsigned int len,
1109 gfp_t gfpmask)
1110{
1111 struct bio *bi = *bio_src;
1112 unsigned int off = *offset;
1113 struct bio *chain = NULL;
1114 struct bio **end;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001115
Alex Elderf7760da2012-10-20 22:17:27 -05001116 /* Build up a chain of clone bios up to the limit */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001117
Alex Elderf7760da2012-10-20 22:17:27 -05001118 if (!bi || off >= bi->bi_size || !len)
1119 return NULL; /* Nothing to clone */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001120
Alex Elderf7760da2012-10-20 22:17:27 -05001121 end = &chain;
1122 while (len) {
1123 unsigned int bi_size;
1124 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001125
Alex Elderf5400b72012-11-01 10:17:15 -05001126 if (!bi) {
1127 rbd_warn(NULL, "bio_chain exhausted with %u left", len);
Alex Elderf7760da2012-10-20 22:17:27 -05001128 goto out_err; /* EINVAL; ran out of bio's */
Alex Elderf5400b72012-11-01 10:17:15 -05001129 }
Alex Elderf7760da2012-10-20 22:17:27 -05001130 bi_size = min_t(unsigned int, bi->bi_size - off, len);
1131 bio = bio_clone_range(bi, off, bi_size, gfpmask);
1132 if (!bio)
1133 goto out_err; /* ENOMEM */
1134
1135 *end = bio;
1136 end = &bio->bi_next;
1137
1138 off += bi_size;
1139 if (off == bi->bi_size) {
1140 bi = bi->bi_next;
1141 off = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001142 }
Alex Elderf7760da2012-10-20 22:17:27 -05001143 len -= bi_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001144 }
Alex Elderf7760da2012-10-20 22:17:27 -05001145 *bio_src = bi;
1146 *offset = off;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001147
Alex Elderf7760da2012-10-20 22:17:27 -05001148 return chain;
1149out_err:
1150 bio_chain_put(chain);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001151
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001152 return NULL;
1153}
1154
Alex Elder926f9b32013-02-11 12:33:24 -06001155/*
1156 * The default/initial value for all object request flags is 0. For
1157 * each flag, once its value is set to 1 it is never reset to 0
1158 * again.
1159 */
Alex Elder6365d332013-02-11 12:33:24 -06001160static void obj_request_img_data_set(struct rbd_obj_request *obj_request)
1161{
1162 if (test_and_set_bit(OBJ_REQ_IMG_DATA, &obj_request->flags)) {
Alex Elder6365d332013-02-11 12:33:24 -06001163 struct rbd_device *rbd_dev;
1164
Alex Elder57acbaa2013-02-11 12:33:24 -06001165 rbd_dev = obj_request->img_request->rbd_dev;
Alex Elder6365d332013-02-11 12:33:24 -06001166 rbd_warn(rbd_dev, "obj_request %p already marked img_data\n",
1167 obj_request);
1168 }
1169}
1170
1171static bool obj_request_img_data_test(struct rbd_obj_request *obj_request)
1172{
1173 smp_mb();
1174 return test_bit(OBJ_REQ_IMG_DATA, &obj_request->flags) != 0;
1175}
1176
Alex Elder57acbaa2013-02-11 12:33:24 -06001177static void obj_request_done_set(struct rbd_obj_request *obj_request)
1178{
1179 if (test_and_set_bit(OBJ_REQ_DONE, &obj_request->flags)) {
1180 struct rbd_device *rbd_dev = NULL;
1181
1182 if (obj_request_img_data_test(obj_request))
1183 rbd_dev = obj_request->img_request->rbd_dev;
1184 rbd_warn(rbd_dev, "obj_request %p already marked done\n",
1185 obj_request);
1186 }
1187}
1188
1189static bool obj_request_done_test(struct rbd_obj_request *obj_request)
1190{
1191 smp_mb();
1192 return test_bit(OBJ_REQ_DONE, &obj_request->flags) != 0;
1193}
1194
Alex Elder5679c592013-02-11 12:33:24 -06001195/*
1196 * This sets the KNOWN flag after (possibly) setting the EXISTS
1197 * flag. The latter is set based on the "exists" value provided.
1198 *
1199 * Note that for our purposes once an object exists it never goes
1200 * away again. It's possible that the response from two existence
1201 * checks are separated by the creation of the target object, and
1202 * the first ("doesn't exist") response arrives *after* the second
1203 * ("does exist"). In that case we ignore the second one.
1204 */
1205static void obj_request_existence_set(struct rbd_obj_request *obj_request,
1206 bool exists)
1207{
1208 if (exists)
1209 set_bit(OBJ_REQ_EXISTS, &obj_request->flags);
1210 set_bit(OBJ_REQ_KNOWN, &obj_request->flags);
1211 smp_mb();
1212}
1213
1214static bool obj_request_known_test(struct rbd_obj_request *obj_request)
1215{
1216 smp_mb();
1217 return test_bit(OBJ_REQ_KNOWN, &obj_request->flags) != 0;
1218}
1219
1220static bool obj_request_exists_test(struct rbd_obj_request *obj_request)
1221{
1222 smp_mb();
1223 return test_bit(OBJ_REQ_EXISTS, &obj_request->flags) != 0;
1224}
1225
Alex Elderbf0d5f502012-11-22 00:00:08 -06001226static void rbd_obj_request_get(struct rbd_obj_request *obj_request)
1227{
Alex Elder37206ee2013-02-20 17:32:08 -06001228 dout("%s: obj %p (was %d)\n", __func__, obj_request,
1229 atomic_read(&obj_request->kref.refcount));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001230 kref_get(&obj_request->kref);
1231}
1232
1233static void rbd_obj_request_destroy(struct kref *kref);
1234static void rbd_obj_request_put(struct rbd_obj_request *obj_request)
1235{
1236 rbd_assert(obj_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001237 dout("%s: obj %p (was %d)\n", __func__, obj_request,
1238 atomic_read(&obj_request->kref.refcount));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001239 kref_put(&obj_request->kref, rbd_obj_request_destroy);
1240}
1241
1242static void rbd_img_request_get(struct rbd_img_request *img_request)
1243{
Alex Elder37206ee2013-02-20 17:32:08 -06001244 dout("%s: img %p (was %d)\n", __func__, img_request,
1245 atomic_read(&img_request->kref.refcount));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001246 kref_get(&img_request->kref);
1247}
1248
1249static void rbd_img_request_destroy(struct kref *kref);
1250static void rbd_img_request_put(struct rbd_img_request *img_request)
1251{
1252 rbd_assert(img_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001253 dout("%s: img %p (was %d)\n", __func__, img_request,
1254 atomic_read(&img_request->kref.refcount));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001255 kref_put(&img_request->kref, rbd_img_request_destroy);
1256}
1257
1258static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request,
1259 struct rbd_obj_request *obj_request)
1260{
Alex Elder25dcf952013-01-25 17:08:55 -06001261 rbd_assert(obj_request->img_request == NULL);
1262
Alex Elderb155e862013-04-15 14:50:37 -05001263 /* Image request now owns object's original reference */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001264 obj_request->img_request = img_request;
Alex Elder25dcf952013-01-25 17:08:55 -06001265 obj_request->which = img_request->obj_request_count;
Alex Elder6365d332013-02-11 12:33:24 -06001266 rbd_assert(!obj_request_img_data_test(obj_request));
1267 obj_request_img_data_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001268 rbd_assert(obj_request->which != BAD_WHICH);
Alex Elder25dcf952013-01-25 17:08:55 -06001269 img_request->obj_request_count++;
1270 list_add_tail(&obj_request->links, &img_request->obj_requests);
Alex Elder37206ee2013-02-20 17:32:08 -06001271 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1272 obj_request->which);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001273}
1274
1275static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request,
1276 struct rbd_obj_request *obj_request)
1277{
1278 rbd_assert(obj_request->which != BAD_WHICH);
Alex Elder25dcf952013-01-25 17:08:55 -06001279
Alex Elder37206ee2013-02-20 17:32:08 -06001280 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1281 obj_request->which);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001282 list_del(&obj_request->links);
Alex Elder25dcf952013-01-25 17:08:55 -06001283 rbd_assert(img_request->obj_request_count > 0);
1284 img_request->obj_request_count--;
1285 rbd_assert(obj_request->which == img_request->obj_request_count);
1286 obj_request->which = BAD_WHICH;
Alex Elder6365d332013-02-11 12:33:24 -06001287 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001288 rbd_assert(obj_request->img_request == img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001289 obj_request->img_request = NULL;
Alex Elder25dcf952013-01-25 17:08:55 -06001290 obj_request->callback = NULL;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001291 rbd_obj_request_put(obj_request);
1292}
1293
1294static bool obj_request_type_valid(enum obj_request_type type)
1295{
1296 switch (type) {
Alex Elder9969ebc2013-01-18 12:31:10 -06001297 case OBJ_REQUEST_NODATA:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001298 case OBJ_REQUEST_BIO:
Alex Elder788e2df2013-01-17 12:25:27 -06001299 case OBJ_REQUEST_PAGES:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001300 return true;
1301 default:
1302 return false;
1303 }
1304}
1305
Alex Elderbf0d5f502012-11-22 00:00:08 -06001306static int rbd_obj_request_submit(struct ceph_osd_client *osdc,
1307 struct rbd_obj_request *obj_request)
1308{
Alex Elder37206ee2013-02-20 17:32:08 -06001309 dout("%s: osdc %p obj %p\n", __func__, osdc, obj_request);
1310
Alex Elderbf0d5f502012-11-22 00:00:08 -06001311 return ceph_osdc_start_request(osdc, obj_request->osd_req, false);
1312}
1313
1314static void rbd_img_request_complete(struct rbd_img_request *img_request)
1315{
Alex Elder55f27e02013-04-10 12:34:25 -05001316
Alex Elder37206ee2013-02-20 17:32:08 -06001317 dout("%s: img %p\n", __func__, img_request);
Alex Elder55f27e02013-04-10 12:34:25 -05001318
1319 /*
1320 * If no error occurred, compute the aggregate transfer
1321 * count for the image request. We could instead use
1322 * atomic64_cmpxchg() to update it as each object request
1323 * completes; not clear which way is better off hand.
1324 */
1325 if (!img_request->result) {
1326 struct rbd_obj_request *obj_request;
1327 u64 xferred = 0;
1328
1329 for_each_obj_request(img_request, obj_request)
1330 xferred += obj_request->xferred;
1331 img_request->xferred = xferred;
1332 }
1333
Alex Elderbf0d5f502012-11-22 00:00:08 -06001334 if (img_request->callback)
1335 img_request->callback(img_request);
1336 else
1337 rbd_img_request_put(img_request);
1338}
1339
Alex Elder788e2df2013-01-17 12:25:27 -06001340/* Caller is responsible for rbd_obj_request_destroy(obj_request) */
1341
1342static int rbd_obj_request_wait(struct rbd_obj_request *obj_request)
1343{
Alex Elder37206ee2013-02-20 17:32:08 -06001344 dout("%s: obj %p\n", __func__, obj_request);
1345
Alex Elder788e2df2013-01-17 12:25:27 -06001346 return wait_for_completion_interruptible(&obj_request->completion);
1347}
1348
Alex Elder0c425242013-02-08 09:55:49 -06001349/*
1350 * The default/initial value for all image request flags is 0. Each
1351 * is conditionally set to 1 at image request initialization time
1352 * and currently never change thereafter.
1353 */
1354static void img_request_write_set(struct rbd_img_request *img_request)
1355{
1356 set_bit(IMG_REQ_WRITE, &img_request->flags);
1357 smp_mb();
1358}
1359
1360static bool img_request_write_test(struct rbd_img_request *img_request)
1361{
1362 smp_mb();
1363 return test_bit(IMG_REQ_WRITE, &img_request->flags) != 0;
1364}
1365
Alex Elder9849e982013-01-24 16:13:36 -06001366static void img_request_child_set(struct rbd_img_request *img_request)
1367{
1368 set_bit(IMG_REQ_CHILD, &img_request->flags);
1369 smp_mb();
1370}
1371
1372static bool img_request_child_test(struct rbd_img_request *img_request)
1373{
1374 smp_mb();
1375 return test_bit(IMG_REQ_CHILD, &img_request->flags) != 0;
1376}
1377
Alex Elderd0b2e942013-01-24 16:13:36 -06001378static void img_request_layered_set(struct rbd_img_request *img_request)
1379{
1380 set_bit(IMG_REQ_LAYERED, &img_request->flags);
1381 smp_mb();
1382}
1383
1384static bool img_request_layered_test(struct rbd_img_request *img_request)
1385{
1386 smp_mb();
1387 return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0;
1388}
1389
Alex Elder6e2a4502013-03-27 09:16:30 -05001390static void
1391rbd_img_obj_request_read_callback(struct rbd_obj_request *obj_request)
1392{
Alex Elderb9434c52013-04-19 15:34:50 -05001393 u64 xferred = obj_request->xferred;
1394 u64 length = obj_request->length;
1395
Alex Elder6e2a4502013-03-27 09:16:30 -05001396 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1397 obj_request, obj_request->img_request, obj_request->result,
Alex Elderb9434c52013-04-19 15:34:50 -05001398 xferred, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001399 /*
1400 * ENOENT means a hole in the image. We zero-fill the
1401 * entire length of the request. A short read also implies
1402 * zero-fill to the end of the request. Either way we
1403 * update the xferred count to indicate the whole request
1404 * was satisfied.
1405 */
Alex Elderb9434c52013-04-19 15:34:50 -05001406 rbd_assert(obj_request->type != OBJ_REQUEST_NODATA);
Alex Elder6e2a4502013-03-27 09:16:30 -05001407 if (obj_request->result == -ENOENT) {
Alex Elderb9434c52013-04-19 15:34:50 -05001408 if (obj_request->type == OBJ_REQUEST_BIO)
1409 zero_bio_chain(obj_request->bio_list, 0);
1410 else
1411 zero_pages(obj_request->pages, 0, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001412 obj_request->result = 0;
Alex Elderb9434c52013-04-19 15:34:50 -05001413 obj_request->xferred = length;
1414 } else if (xferred < length && !obj_request->result) {
1415 if (obj_request->type == OBJ_REQUEST_BIO)
1416 zero_bio_chain(obj_request->bio_list, xferred);
1417 else
1418 zero_pages(obj_request->pages, xferred, length);
1419 obj_request->xferred = length;
Alex Elder6e2a4502013-03-27 09:16:30 -05001420 }
1421 obj_request_done_set(obj_request);
1422}
1423
Alex Elderbf0d5f502012-11-22 00:00:08 -06001424static void rbd_obj_request_complete(struct rbd_obj_request *obj_request)
1425{
Alex Elder37206ee2013-02-20 17:32:08 -06001426 dout("%s: obj %p cb %p\n", __func__, obj_request,
1427 obj_request->callback);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001428 if (obj_request->callback)
1429 obj_request->callback(obj_request);
Alex Elder788e2df2013-01-17 12:25:27 -06001430 else
1431 complete_all(&obj_request->completion);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001432}
1433
Alex Elderc47f9372013-02-26 14:23:07 -06001434static void rbd_osd_trivial_callback(struct rbd_obj_request *obj_request)
Alex Elder39bf2c52013-02-26 14:23:07 -06001435{
1436 dout("%s: obj %p\n", __func__, obj_request);
1437 obj_request_done_set(obj_request);
1438}
1439
Alex Elderc47f9372013-02-26 14:23:07 -06001440static void rbd_osd_read_callback(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001441{
Alex Elder57acbaa2013-02-11 12:33:24 -06001442 struct rbd_img_request *img_request = NULL;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001443 struct rbd_device *rbd_dev = NULL;
Alex Elder57acbaa2013-02-11 12:33:24 -06001444 bool layered = false;
1445
1446 if (obj_request_img_data_test(obj_request)) {
1447 img_request = obj_request->img_request;
1448 layered = img_request && img_request_layered_test(img_request);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001449 rbd_dev = img_request->rbd_dev;
Alex Elder57acbaa2013-02-11 12:33:24 -06001450 }
Alex Elder8b3e1a52013-01-24 16:13:36 -06001451
1452 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1453 obj_request, img_request, obj_request->result,
1454 obj_request->xferred, obj_request->length);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001455 if (layered && obj_request->result == -ENOENT &&
1456 obj_request->img_offset < rbd_dev->parent_overlap)
Alex Elder8b3e1a52013-01-24 16:13:36 -06001457 rbd_img_parent_read(obj_request);
1458 else if (img_request)
Alex Elder6e2a4502013-03-27 09:16:30 -05001459 rbd_img_obj_request_read_callback(obj_request);
1460 else
1461 obj_request_done_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001462}
1463
Alex Elderc47f9372013-02-26 14:23:07 -06001464static void rbd_osd_write_callback(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001465{
Sage Weil1b83bef2013-02-25 16:11:12 -08001466 dout("%s: obj %p result %d %llu\n", __func__, obj_request,
1467 obj_request->result, obj_request->length);
1468 /*
Alex Elder8b3e1a52013-01-24 16:13:36 -06001469 * There is no such thing as a successful short write. Set
1470 * it to our originally-requested length.
Sage Weil1b83bef2013-02-25 16:11:12 -08001471 */
1472 obj_request->xferred = obj_request->length;
Alex Elder07741302013-02-05 23:41:50 -06001473 obj_request_done_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001474}
1475
Alex Elderfbfab532013-02-08 09:55:48 -06001476/*
1477 * For a simple stat call there's nothing to do. We'll do more if
1478 * this is part of a write sequence for a layered image.
1479 */
Alex Elderc47f9372013-02-26 14:23:07 -06001480static void rbd_osd_stat_callback(struct rbd_obj_request *obj_request)
Alex Elderfbfab532013-02-08 09:55:48 -06001481{
Alex Elder37206ee2013-02-20 17:32:08 -06001482 dout("%s: obj %p\n", __func__, obj_request);
Alex Elderfbfab532013-02-08 09:55:48 -06001483 obj_request_done_set(obj_request);
1484}
1485
Alex Elderbf0d5f502012-11-22 00:00:08 -06001486static void rbd_osd_req_callback(struct ceph_osd_request *osd_req,
1487 struct ceph_msg *msg)
1488{
1489 struct rbd_obj_request *obj_request = osd_req->r_priv;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001490 u16 opcode;
1491
Alex Elder37206ee2013-02-20 17:32:08 -06001492 dout("%s: osd_req %p msg %p\n", __func__, osd_req, msg);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001493 rbd_assert(osd_req == obj_request->osd_req);
Alex Elder57acbaa2013-02-11 12:33:24 -06001494 if (obj_request_img_data_test(obj_request)) {
1495 rbd_assert(obj_request->img_request);
1496 rbd_assert(obj_request->which != BAD_WHICH);
1497 } else {
1498 rbd_assert(obj_request->which == BAD_WHICH);
1499 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06001500
Sage Weil1b83bef2013-02-25 16:11:12 -08001501 if (osd_req->r_result < 0)
1502 obj_request->result = osd_req->r_result;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001503 obj_request->version = le64_to_cpu(osd_req->r_reassert_version.version);
1504
Alex Elder0eefd472013-04-19 15:34:50 -05001505 BUG_ON(osd_req->r_num_ops > 2);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001506
Alex Elderc47f9372013-02-26 14:23:07 -06001507 /*
1508 * We support a 64-bit length, but ultimately it has to be
1509 * passed to blk_end_request(), which takes an unsigned int.
1510 */
Sage Weil1b83bef2013-02-25 16:11:12 -08001511 obj_request->xferred = osd_req->r_reply_op_len[0];
Alex Elder8b3e1a52013-01-24 16:13:36 -06001512 rbd_assert(obj_request->xferred < (u64)UINT_MAX);
Alex Elder79528732013-04-03 21:32:51 -05001513 opcode = osd_req->r_ops[0].op;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001514 switch (opcode) {
1515 case CEPH_OSD_OP_READ:
Alex Elderc47f9372013-02-26 14:23:07 -06001516 rbd_osd_read_callback(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001517 break;
1518 case CEPH_OSD_OP_WRITE:
Alex Elderc47f9372013-02-26 14:23:07 -06001519 rbd_osd_write_callback(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001520 break;
Alex Elderfbfab532013-02-08 09:55:48 -06001521 case CEPH_OSD_OP_STAT:
Alex Elderc47f9372013-02-26 14:23:07 -06001522 rbd_osd_stat_callback(obj_request);
Alex Elderfbfab532013-02-08 09:55:48 -06001523 break;
Alex Elder36be9a72013-01-19 00:30:28 -06001524 case CEPH_OSD_OP_CALL:
Alex Elderb8d70032012-11-30 17:53:04 -06001525 case CEPH_OSD_OP_NOTIFY_ACK:
Alex Elder9969ebc2013-01-18 12:31:10 -06001526 case CEPH_OSD_OP_WATCH:
Alex Elderc47f9372013-02-26 14:23:07 -06001527 rbd_osd_trivial_callback(obj_request);
Alex Elder9969ebc2013-01-18 12:31:10 -06001528 break;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001529 default:
1530 rbd_warn(NULL, "%s: unsupported op %hu\n",
1531 obj_request->object_name, (unsigned short) opcode);
1532 break;
1533 }
1534
Alex Elder07741302013-02-05 23:41:50 -06001535 if (obj_request_done_test(obj_request))
Alex Elderbf0d5f502012-11-22 00:00:08 -06001536 rbd_obj_request_complete(obj_request);
1537}
1538
Alex Elder9d4df012013-04-19 15:34:50 -05001539static void rbd_osd_req_format_read(struct rbd_obj_request *obj_request)
Alex Elder430c28c2013-04-03 21:32:51 -05001540{
1541 struct rbd_img_request *img_request = obj_request->img_request;
Alex Elder8c042b02013-04-03 01:28:58 -05001542 struct ceph_osd_request *osd_req = obj_request->osd_req;
Alex Elder9d4df012013-04-19 15:34:50 -05001543 u64 snap_id;
Alex Elder430c28c2013-04-03 21:32:51 -05001544
Alex Elder8c042b02013-04-03 01:28:58 -05001545 rbd_assert(osd_req != NULL);
Alex Elder430c28c2013-04-03 21:32:51 -05001546
Alex Elder9d4df012013-04-19 15:34:50 -05001547 snap_id = img_request ? img_request->snap_id : CEPH_NOSNAP;
Alex Elder8c042b02013-04-03 01:28:58 -05001548 ceph_osdc_build_request(osd_req, obj_request->offset,
Alex Elder9d4df012013-04-19 15:34:50 -05001549 NULL, snap_id, NULL);
1550}
1551
1552static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request)
1553{
1554 struct rbd_img_request *img_request = obj_request->img_request;
1555 struct ceph_osd_request *osd_req = obj_request->osd_req;
1556 struct ceph_snap_context *snapc;
1557 struct timespec mtime = CURRENT_TIME;
1558
1559 rbd_assert(osd_req != NULL);
1560
1561 snapc = img_request ? img_request->snapc : NULL;
1562 ceph_osdc_build_request(osd_req, obj_request->offset,
1563 snapc, CEPH_NOSNAP, &mtime);
Alex Elder430c28c2013-04-03 21:32:51 -05001564}
1565
Alex Elderbf0d5f502012-11-22 00:00:08 -06001566static struct ceph_osd_request *rbd_osd_req_create(
1567 struct rbd_device *rbd_dev,
1568 bool write_request,
Alex Elder430c28c2013-04-03 21:32:51 -05001569 struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001570{
Alex Elderbf0d5f502012-11-22 00:00:08 -06001571 struct ceph_snap_context *snapc = NULL;
1572 struct ceph_osd_client *osdc;
1573 struct ceph_osd_request *osd_req;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001574
Alex Elder6365d332013-02-11 12:33:24 -06001575 if (obj_request_img_data_test(obj_request)) {
1576 struct rbd_img_request *img_request = obj_request->img_request;
1577
Alex Elder0c425242013-02-08 09:55:49 -06001578 rbd_assert(write_request ==
1579 img_request_write_test(img_request));
1580 if (write_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001581 snapc = img_request->snapc;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001582 }
1583
1584 /* Allocate and initialize the request, for the single op */
1585
1586 osdc = &rbd_dev->rbd_client->client->osdc;
1587 osd_req = ceph_osdc_alloc_request(osdc, snapc, 1, false, GFP_ATOMIC);
1588 if (!osd_req)
1589 return NULL; /* ENOMEM */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001590
Alex Elder430c28c2013-04-03 21:32:51 -05001591 if (write_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001592 osd_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK;
Alex Elder430c28c2013-04-03 21:32:51 -05001593 else
Alex Elderbf0d5f502012-11-22 00:00:08 -06001594 osd_req->r_flags = CEPH_OSD_FLAG_READ;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001595
1596 osd_req->r_callback = rbd_osd_req_callback;
1597 osd_req->r_priv = obj_request;
1598
1599 osd_req->r_oid_len = strlen(obj_request->object_name);
1600 rbd_assert(osd_req->r_oid_len < sizeof (osd_req->r_oid));
1601 memcpy(osd_req->r_oid, obj_request->object_name, osd_req->r_oid_len);
1602
1603 osd_req->r_file_layout = rbd_dev->layout; /* struct */
1604
Alex Elderbf0d5f502012-11-22 00:00:08 -06001605 return osd_req;
1606}
1607
Alex Elder0eefd472013-04-19 15:34:50 -05001608/*
1609 * Create a copyup osd request based on the information in the
1610 * object request supplied. A copyup request has two osd ops,
1611 * a copyup method call, and a "normal" write request.
1612 */
1613static struct ceph_osd_request *
1614rbd_osd_req_create_copyup(struct rbd_obj_request *obj_request)
1615{
1616 struct rbd_img_request *img_request;
1617 struct ceph_snap_context *snapc;
1618 struct rbd_device *rbd_dev;
1619 struct ceph_osd_client *osdc;
1620 struct ceph_osd_request *osd_req;
1621
1622 rbd_assert(obj_request_img_data_test(obj_request));
1623 img_request = obj_request->img_request;
1624 rbd_assert(img_request);
1625 rbd_assert(img_request_write_test(img_request));
1626
1627 /* Allocate and initialize the request, for the two ops */
1628
1629 snapc = img_request->snapc;
1630 rbd_dev = img_request->rbd_dev;
1631 osdc = &rbd_dev->rbd_client->client->osdc;
1632 osd_req = ceph_osdc_alloc_request(osdc, snapc, 2, false, GFP_ATOMIC);
1633 if (!osd_req)
1634 return NULL; /* ENOMEM */
1635
1636 osd_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK;
1637 osd_req->r_callback = rbd_osd_req_callback;
1638 osd_req->r_priv = obj_request;
1639
1640 osd_req->r_oid_len = strlen(obj_request->object_name);
1641 rbd_assert(osd_req->r_oid_len < sizeof (osd_req->r_oid));
1642 memcpy(osd_req->r_oid, obj_request->object_name, osd_req->r_oid_len);
1643
1644 osd_req->r_file_layout = rbd_dev->layout; /* struct */
1645
1646 return osd_req;
1647}
1648
1649
Alex Elderbf0d5f502012-11-22 00:00:08 -06001650static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req)
1651{
1652 ceph_osdc_put_request(osd_req);
1653}
1654
1655/* object_name is assumed to be a non-null pointer and NUL-terminated */
1656
1657static struct rbd_obj_request *rbd_obj_request_create(const char *object_name,
1658 u64 offset, u64 length,
1659 enum obj_request_type type)
1660{
1661 struct rbd_obj_request *obj_request;
1662 size_t size;
1663 char *name;
1664
1665 rbd_assert(obj_request_type_valid(type));
1666
1667 size = strlen(object_name) + 1;
1668 obj_request = kzalloc(sizeof (*obj_request) + size, GFP_KERNEL);
1669 if (!obj_request)
1670 return NULL;
1671
1672 name = (char *)(obj_request + 1);
1673 obj_request->object_name = memcpy(name, object_name, size);
1674 obj_request->offset = offset;
1675 obj_request->length = length;
Alex Elder926f9b32013-02-11 12:33:24 -06001676 obj_request->flags = 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001677 obj_request->which = BAD_WHICH;
1678 obj_request->type = type;
1679 INIT_LIST_HEAD(&obj_request->links);
Alex Elder788e2df2013-01-17 12:25:27 -06001680 init_completion(&obj_request->completion);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001681 kref_init(&obj_request->kref);
1682
Alex Elder37206ee2013-02-20 17:32:08 -06001683 dout("%s: \"%s\" %llu/%llu %d -> obj %p\n", __func__, object_name,
1684 offset, length, (int)type, obj_request);
1685
Alex Elderbf0d5f502012-11-22 00:00:08 -06001686 return obj_request;
1687}
1688
1689static void rbd_obj_request_destroy(struct kref *kref)
1690{
1691 struct rbd_obj_request *obj_request;
1692
1693 obj_request = container_of(kref, struct rbd_obj_request, kref);
1694
Alex Elder37206ee2013-02-20 17:32:08 -06001695 dout("%s: obj %p\n", __func__, obj_request);
1696
Alex Elderbf0d5f502012-11-22 00:00:08 -06001697 rbd_assert(obj_request->img_request == NULL);
1698 rbd_assert(obj_request->which == BAD_WHICH);
1699
1700 if (obj_request->osd_req)
1701 rbd_osd_req_destroy(obj_request->osd_req);
1702
1703 rbd_assert(obj_request_type_valid(obj_request->type));
1704 switch (obj_request->type) {
Alex Elder9969ebc2013-01-18 12:31:10 -06001705 case OBJ_REQUEST_NODATA:
1706 break; /* Nothing to do */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001707 case OBJ_REQUEST_BIO:
1708 if (obj_request->bio_list)
1709 bio_chain_put(obj_request->bio_list);
1710 break;
Alex Elder788e2df2013-01-17 12:25:27 -06001711 case OBJ_REQUEST_PAGES:
1712 if (obj_request->pages)
1713 ceph_release_page_vector(obj_request->pages,
1714 obj_request->page_count);
1715 break;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001716 }
1717
1718 kfree(obj_request);
1719}
1720
1721/*
1722 * Caller is responsible for filling in the list of object requests
1723 * that comprises the image request, and the Linux request pointer
1724 * (if there is one).
1725 */
Alex Eldercc344fa2013-02-19 12:25:56 -06001726static struct rbd_img_request *rbd_img_request_create(
1727 struct rbd_device *rbd_dev,
Alex Elderbf0d5f502012-11-22 00:00:08 -06001728 u64 offset, u64 length,
Alex Elder9849e982013-01-24 16:13:36 -06001729 bool write_request,
1730 bool child_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001731{
1732 struct rbd_img_request *img_request;
1733 struct ceph_snap_context *snapc = NULL;
1734
1735 img_request = kmalloc(sizeof (*img_request), GFP_ATOMIC);
1736 if (!img_request)
1737 return NULL;
1738
1739 if (write_request) {
1740 down_read(&rbd_dev->header_rwsem);
1741 snapc = ceph_get_snap_context(rbd_dev->header.snapc);
1742 up_read(&rbd_dev->header_rwsem);
1743 if (WARN_ON(!snapc)) {
1744 kfree(img_request);
1745 return NULL; /* Shouldn't happen */
1746 }
Alex Elder0c425242013-02-08 09:55:49 -06001747
Alex Elderbf0d5f502012-11-22 00:00:08 -06001748 }
1749
1750 img_request->rq = NULL;
1751 img_request->rbd_dev = rbd_dev;
1752 img_request->offset = offset;
1753 img_request->length = length;
Alex Elder0c425242013-02-08 09:55:49 -06001754 img_request->flags = 0;
1755 if (write_request) {
1756 img_request_write_set(img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001757 img_request->snapc = snapc;
Alex Elder0c425242013-02-08 09:55:49 -06001758 } else {
Alex Elderbf0d5f502012-11-22 00:00:08 -06001759 img_request->snap_id = rbd_dev->spec->snap_id;
Alex Elder0c425242013-02-08 09:55:49 -06001760 }
Alex Elder9849e982013-01-24 16:13:36 -06001761 if (child_request)
1762 img_request_child_set(img_request);
Alex Elderd0b2e942013-01-24 16:13:36 -06001763 if (rbd_dev->parent_spec)
1764 img_request_layered_set(img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001765 spin_lock_init(&img_request->completion_lock);
1766 img_request->next_completion = 0;
1767 img_request->callback = NULL;
Alex Eldera5a337d2013-01-24 16:13:36 -06001768 img_request->result = 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001769 img_request->obj_request_count = 0;
1770 INIT_LIST_HEAD(&img_request->obj_requests);
1771 kref_init(&img_request->kref);
1772
1773 rbd_img_request_get(img_request); /* Avoid a warning */
1774 rbd_img_request_put(img_request); /* TEMPORARY */
1775
Alex Elder37206ee2013-02-20 17:32:08 -06001776 dout("%s: rbd_dev %p %s %llu/%llu -> img %p\n", __func__, rbd_dev,
1777 write_request ? "write" : "read", offset, length,
1778 img_request);
1779
Alex Elderbf0d5f502012-11-22 00:00:08 -06001780 return img_request;
1781}
1782
1783static void rbd_img_request_destroy(struct kref *kref)
1784{
1785 struct rbd_img_request *img_request;
1786 struct rbd_obj_request *obj_request;
1787 struct rbd_obj_request *next_obj_request;
1788
1789 img_request = container_of(kref, struct rbd_img_request, kref);
1790
Alex Elder37206ee2013-02-20 17:32:08 -06001791 dout("%s: img %p\n", __func__, img_request);
1792
Alex Elderbf0d5f502012-11-22 00:00:08 -06001793 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
1794 rbd_img_obj_request_del(img_request, obj_request);
Alex Elder25dcf952013-01-25 17:08:55 -06001795 rbd_assert(img_request->obj_request_count == 0);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001796
Alex Elder0c425242013-02-08 09:55:49 -06001797 if (img_request_write_test(img_request))
Alex Elderbf0d5f502012-11-22 00:00:08 -06001798 ceph_put_snap_context(img_request->snapc);
1799
Alex Elder8b3e1a52013-01-24 16:13:36 -06001800 if (img_request_child_test(img_request))
1801 rbd_obj_request_put(img_request->obj_request);
1802
Alex Elderbf0d5f502012-11-22 00:00:08 -06001803 kfree(img_request);
1804}
1805
Alex Elder12178572013-02-08 09:55:49 -06001806static bool rbd_img_obj_end_request(struct rbd_obj_request *obj_request)
1807{
Alex Elder6365d332013-02-11 12:33:24 -06001808 struct rbd_img_request *img_request;
Alex Elder12178572013-02-08 09:55:49 -06001809 unsigned int xferred;
1810 int result;
Alex Elder8b3e1a52013-01-24 16:13:36 -06001811 bool more;
Alex Elder12178572013-02-08 09:55:49 -06001812
Alex Elder6365d332013-02-11 12:33:24 -06001813 rbd_assert(obj_request_img_data_test(obj_request));
1814 img_request = obj_request->img_request;
1815
Alex Elder12178572013-02-08 09:55:49 -06001816 rbd_assert(obj_request->xferred <= (u64)UINT_MAX);
1817 xferred = (unsigned int)obj_request->xferred;
1818 result = obj_request->result;
1819 if (result) {
1820 struct rbd_device *rbd_dev = img_request->rbd_dev;
1821
1822 rbd_warn(rbd_dev, "%s %llx at %llx (%llx)\n",
1823 img_request_write_test(img_request) ? "write" : "read",
1824 obj_request->length, obj_request->img_offset,
1825 obj_request->offset);
1826 rbd_warn(rbd_dev, " result %d xferred %x\n",
1827 result, xferred);
1828 if (!img_request->result)
1829 img_request->result = result;
1830 }
1831
Alex Elderf1a47392013-04-19 15:34:50 -05001832 /* Image object requests don't own their page array */
1833
1834 if (obj_request->type == OBJ_REQUEST_PAGES) {
1835 obj_request->pages = NULL;
1836 obj_request->page_count = 0;
1837 }
1838
Alex Elder8b3e1a52013-01-24 16:13:36 -06001839 if (img_request_child_test(img_request)) {
1840 rbd_assert(img_request->obj_request != NULL);
1841 more = obj_request->which < img_request->obj_request_count - 1;
1842 } else {
1843 rbd_assert(img_request->rq != NULL);
1844 more = blk_end_request(img_request->rq, result, xferred);
1845 }
1846
1847 return more;
Alex Elder12178572013-02-08 09:55:49 -06001848}
1849
Alex Elder21692382013-04-05 01:27:12 -05001850static void rbd_img_obj_callback(struct rbd_obj_request *obj_request)
1851{
1852 struct rbd_img_request *img_request;
1853 u32 which = obj_request->which;
1854 bool more = true;
1855
Alex Elder6365d332013-02-11 12:33:24 -06001856 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elder21692382013-04-05 01:27:12 -05001857 img_request = obj_request->img_request;
1858
1859 dout("%s: img %p obj %p\n", __func__, img_request, obj_request);
1860 rbd_assert(img_request != NULL);
Alex Elder21692382013-04-05 01:27:12 -05001861 rbd_assert(img_request->obj_request_count > 0);
1862 rbd_assert(which != BAD_WHICH);
1863 rbd_assert(which < img_request->obj_request_count);
1864 rbd_assert(which >= img_request->next_completion);
1865
1866 spin_lock_irq(&img_request->completion_lock);
1867 if (which != img_request->next_completion)
1868 goto out;
1869
1870 for_each_obj_request_from(img_request, obj_request) {
Alex Elder21692382013-04-05 01:27:12 -05001871 rbd_assert(more);
1872 rbd_assert(which < img_request->obj_request_count);
1873
1874 if (!obj_request_done_test(obj_request))
1875 break;
Alex Elder12178572013-02-08 09:55:49 -06001876 more = rbd_img_obj_end_request(obj_request);
Alex Elder21692382013-04-05 01:27:12 -05001877 which++;
1878 }
1879
1880 rbd_assert(more ^ (which == img_request->obj_request_count));
1881 img_request->next_completion = which;
1882out:
1883 spin_unlock_irq(&img_request->completion_lock);
1884
1885 if (!more)
1886 rbd_img_request_complete(img_request);
1887}
1888
Alex Elderf1a47392013-04-19 15:34:50 -05001889/*
1890 * Split up an image request into one or more object requests, each
1891 * to a different object. The "type" parameter indicates whether
1892 * "data_desc" is the pointer to the head of a list of bio
1893 * structures, or the base of a page array. In either case this
1894 * function assumes data_desc describes memory sufficient to hold
1895 * all data described by the image request.
1896 */
1897static int rbd_img_request_fill(struct rbd_img_request *img_request,
1898 enum obj_request_type type,
1899 void *data_desc)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001900{
1901 struct rbd_device *rbd_dev = img_request->rbd_dev;
1902 struct rbd_obj_request *obj_request = NULL;
1903 struct rbd_obj_request *next_obj_request;
Alex Elder0c425242013-02-08 09:55:49 -06001904 bool write_request = img_request_write_test(img_request);
Alex Elderf1a47392013-04-19 15:34:50 -05001905 struct bio *bio_list;
1906 unsigned int bio_offset = 0;
1907 struct page **pages;
Alex Elder7da22d22013-01-24 16:13:36 -06001908 u64 img_offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001909 u64 resid;
1910 u16 opcode;
1911
Alex Elderf1a47392013-04-19 15:34:50 -05001912 dout("%s: img %p type %d data_desc %p\n", __func__, img_request,
1913 (int)type, data_desc);
Alex Elder37206ee2013-02-20 17:32:08 -06001914
Alex Elder430c28c2013-04-03 21:32:51 -05001915 opcode = write_request ? CEPH_OSD_OP_WRITE : CEPH_OSD_OP_READ;
Alex Elder7da22d22013-01-24 16:13:36 -06001916 img_offset = img_request->offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001917 resid = img_request->length;
Alex Elder4dda41d2013-02-20 21:59:33 -06001918 rbd_assert(resid > 0);
Alex Elderf1a47392013-04-19 15:34:50 -05001919
1920 if (type == OBJ_REQUEST_BIO) {
1921 bio_list = data_desc;
1922 rbd_assert(img_offset == bio_list->bi_sector << SECTOR_SHIFT);
1923 } else {
1924 rbd_assert(type == OBJ_REQUEST_PAGES);
1925 pages = data_desc;
1926 }
1927
Alex Elderbf0d5f502012-11-22 00:00:08 -06001928 while (resid) {
Alex Elder2fa12322013-04-05 01:27:12 -05001929 struct ceph_osd_request *osd_req;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001930 const char *object_name;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001931 u64 offset;
1932 u64 length;
1933
Alex Elder7da22d22013-01-24 16:13:36 -06001934 object_name = rbd_segment_name(rbd_dev, img_offset);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001935 if (!object_name)
1936 goto out_unwind;
Alex Elder7da22d22013-01-24 16:13:36 -06001937 offset = rbd_segment_offset(rbd_dev, img_offset);
1938 length = rbd_segment_length(rbd_dev, img_offset, resid);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001939 obj_request = rbd_obj_request_create(object_name,
Alex Elderf1a47392013-04-19 15:34:50 -05001940 offset, length, type);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001941 kfree(object_name); /* object request has its own copy */
1942 if (!obj_request)
1943 goto out_unwind;
1944
Alex Elderf1a47392013-04-19 15:34:50 -05001945 if (type == OBJ_REQUEST_BIO) {
1946 unsigned int clone_size;
1947
1948 rbd_assert(length <= (u64)UINT_MAX);
1949 clone_size = (unsigned int)length;
1950 obj_request->bio_list =
1951 bio_chain_clone_range(&bio_list,
1952 &bio_offset,
1953 clone_size,
1954 GFP_ATOMIC);
1955 if (!obj_request->bio_list)
1956 goto out_partial;
1957 } else {
1958 unsigned int page_count;
1959
1960 obj_request->pages = pages;
1961 page_count = (u32)calc_pages_for(offset, length);
1962 obj_request->page_count = page_count;
1963 if ((offset + length) & ~PAGE_MASK)
1964 page_count--; /* more on last page */
1965 pages += page_count;
1966 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06001967
Alex Elder2fa12322013-04-05 01:27:12 -05001968 osd_req = rbd_osd_req_create(rbd_dev, write_request,
1969 obj_request);
1970 if (!osd_req)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001971 goto out_partial;
Alex Elder2fa12322013-04-05 01:27:12 -05001972 obj_request->osd_req = osd_req;
Alex Elder21692382013-04-05 01:27:12 -05001973 obj_request->callback = rbd_img_obj_callback;
Alex Elder430c28c2013-04-03 21:32:51 -05001974
Alex Elder2fa12322013-04-05 01:27:12 -05001975 osd_req_op_extent_init(osd_req, 0, opcode, offset, length,
1976 0, 0);
Alex Elderf1a47392013-04-19 15:34:50 -05001977 if (type == OBJ_REQUEST_BIO)
1978 osd_req_op_extent_osd_data_bio(osd_req, 0,
1979 obj_request->bio_list, length);
1980 else
1981 osd_req_op_extent_osd_data_pages(osd_req, 0,
1982 obj_request->pages, length,
1983 offset & ~PAGE_MASK, false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05001984
1985 if (write_request)
1986 rbd_osd_req_format_write(obj_request);
1987 else
1988 rbd_osd_req_format_read(obj_request);
Alex Elder430c28c2013-04-03 21:32:51 -05001989
Alex Elder7da22d22013-01-24 16:13:36 -06001990 obj_request->img_offset = img_offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001991 rbd_img_obj_request_add(img_request, obj_request);
1992
Alex Elder7da22d22013-01-24 16:13:36 -06001993 img_offset += length;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001994 resid -= length;
1995 }
1996
1997 return 0;
1998
1999out_partial:
2000 rbd_obj_request_put(obj_request);
2001out_unwind:
2002 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
2003 rbd_obj_request_put(obj_request);
2004
2005 return -ENOMEM;
2006}
2007
Alex Elder3d7efd12013-04-19 15:34:50 -05002008static void
Alex Elder0eefd472013-04-19 15:34:50 -05002009rbd_img_obj_copyup_callback(struct rbd_obj_request *obj_request)
2010{
2011 struct rbd_img_request *img_request;
2012 struct rbd_device *rbd_dev;
2013 u64 length;
2014 u32 page_count;
2015
2016 rbd_assert(obj_request->type == OBJ_REQUEST_BIO);
2017 rbd_assert(obj_request_img_data_test(obj_request));
2018 img_request = obj_request->img_request;
2019 rbd_assert(img_request);
2020
2021 rbd_dev = img_request->rbd_dev;
2022 rbd_assert(rbd_dev);
2023 length = (u64)1 << rbd_dev->header.obj_order;
2024 page_count = (u32)calc_pages_for(0, length);
2025
2026 rbd_assert(obj_request->copyup_pages);
2027 ceph_release_page_vector(obj_request->copyup_pages, page_count);
2028 obj_request->copyup_pages = NULL;
2029
2030 /*
2031 * We want the transfer count to reflect the size of the
2032 * original write request. There is no such thing as a
2033 * successful short write, so if the request was successful
2034 * we can just set it to the originally-requested length.
2035 */
2036 if (!obj_request->result)
2037 obj_request->xferred = obj_request->length;
2038
2039 /* Finish up with the normal image object callback */
2040
2041 rbd_img_obj_callback(obj_request);
2042}
2043
2044static void
Alex Elder3d7efd12013-04-19 15:34:50 -05002045rbd_img_obj_parent_read_full_callback(struct rbd_img_request *img_request)
2046{
2047 struct rbd_obj_request *orig_request;
Alex Elder0eefd472013-04-19 15:34:50 -05002048 struct ceph_osd_request *osd_req;
2049 struct ceph_osd_client *osdc;
2050 struct rbd_device *rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002051 struct page **pages;
Alex Elder3d7efd12013-04-19 15:34:50 -05002052 int result;
2053 u64 obj_size;
2054 u64 xferred;
2055
2056 rbd_assert(img_request_child_test(img_request));
2057
2058 /* First get what we need from the image request */
2059
2060 pages = img_request->copyup_pages;
2061 rbd_assert(pages != NULL);
2062 img_request->copyup_pages = NULL;
2063
2064 orig_request = img_request->obj_request;
2065 rbd_assert(orig_request != NULL);
Alex Elder0eefd472013-04-19 15:34:50 -05002066 rbd_assert(orig_request->type == OBJ_REQUEST_BIO);
Alex Elder3d7efd12013-04-19 15:34:50 -05002067 result = img_request->result;
2068 obj_size = img_request->length;
2069 xferred = img_request->xferred;
2070
Alex Elder0eefd472013-04-19 15:34:50 -05002071 rbd_dev = img_request->rbd_dev;
2072 rbd_assert(rbd_dev);
2073 rbd_assert(obj_size == (u64)1 << rbd_dev->header.obj_order);
2074
Alex Elder3d7efd12013-04-19 15:34:50 -05002075 rbd_img_request_put(img_request);
2076
Alex Elder0eefd472013-04-19 15:34:50 -05002077 if (result)
2078 goto out_err;
Alex Elder3d7efd12013-04-19 15:34:50 -05002079
Alex Elder0eefd472013-04-19 15:34:50 -05002080 /* Allocate the new copyup osd request for the original request */
Alex Elder3d7efd12013-04-19 15:34:50 -05002081
Alex Elder0eefd472013-04-19 15:34:50 -05002082 result = -ENOMEM;
2083 rbd_assert(!orig_request->osd_req);
2084 osd_req = rbd_osd_req_create_copyup(orig_request);
2085 if (!osd_req)
2086 goto out_err;
2087 orig_request->osd_req = osd_req;
2088 orig_request->copyup_pages = pages;
Alex Elder3d7efd12013-04-19 15:34:50 -05002089
Alex Elder0eefd472013-04-19 15:34:50 -05002090 /* Initialize the copyup op */
2091
2092 osd_req_op_cls_init(osd_req, 0, CEPH_OSD_OP_CALL, "rbd", "copyup");
2093 osd_req_op_cls_request_data_pages(osd_req, 0, pages, obj_size, 0,
2094 false, false);
2095
2096 /* Then the original write request op */
2097
2098 osd_req_op_extent_init(osd_req, 1, CEPH_OSD_OP_WRITE,
2099 orig_request->offset,
2100 orig_request->length, 0, 0);
2101 osd_req_op_extent_osd_data_bio(osd_req, 1, orig_request->bio_list,
2102 orig_request->length);
2103
2104 rbd_osd_req_format_write(orig_request);
2105
2106 /* All set, send it off. */
2107
2108 orig_request->callback = rbd_img_obj_copyup_callback;
2109 osdc = &rbd_dev->rbd_client->client->osdc;
2110 result = rbd_obj_request_submit(osdc, orig_request);
2111 if (!result)
2112 return;
2113out_err:
2114 /* Record the error code and complete the request */
2115
2116 orig_request->result = result;
2117 orig_request->xferred = 0;
2118 obj_request_done_set(orig_request);
2119 rbd_obj_request_complete(orig_request);
Alex Elder3d7efd12013-04-19 15:34:50 -05002120}
2121
2122/*
2123 * Read from the parent image the range of data that covers the
2124 * entire target of the given object request. This is used for
2125 * satisfying a layered image write request when the target of an
2126 * object request from the image request does not exist.
2127 *
2128 * A page array big enough to hold the returned data is allocated
2129 * and supplied to rbd_img_request_fill() as the "data descriptor."
2130 * When the read completes, this page array will be transferred to
2131 * the original object request for the copyup operation.
2132 *
2133 * If an error occurs, record it as the result of the original
2134 * object request and mark it done so it gets completed.
2135 */
2136static int rbd_img_obj_parent_read_full(struct rbd_obj_request *obj_request)
2137{
2138 struct rbd_img_request *img_request = NULL;
2139 struct rbd_img_request *parent_request = NULL;
2140 struct rbd_device *rbd_dev;
2141 u64 img_offset;
2142 u64 length;
2143 struct page **pages = NULL;
2144 u32 page_count;
2145 int result;
2146
2147 rbd_assert(obj_request_img_data_test(obj_request));
2148 rbd_assert(obj_request->type == OBJ_REQUEST_BIO);
2149
2150 img_request = obj_request->img_request;
2151 rbd_assert(img_request != NULL);
2152 rbd_dev = img_request->rbd_dev;
2153 rbd_assert(rbd_dev->parent != NULL);
2154
2155 /*
Alex Elder0eefd472013-04-19 15:34:50 -05002156 * First things first. The original osd request is of no
2157 * use to use any more, we'll need a new one that can hold
2158 * the two ops in a copyup request. We'll get that later,
2159 * but for now we can release the old one.
2160 */
2161 rbd_osd_req_destroy(obj_request->osd_req);
2162 obj_request->osd_req = NULL;
2163
2164 /*
Alex Elder3d7efd12013-04-19 15:34:50 -05002165 * Determine the byte range covered by the object in the
2166 * child image to which the original request was to be sent.
2167 */
2168 img_offset = obj_request->img_offset - obj_request->offset;
2169 length = (u64)1 << rbd_dev->header.obj_order;
2170
2171 /*
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002172 * There is no defined parent data beyond the parent
2173 * overlap, so limit what we read at that boundary if
2174 * necessary.
2175 */
2176 if (img_offset + length > rbd_dev->parent_overlap) {
2177 rbd_assert(img_offset < rbd_dev->parent_overlap);
2178 length = rbd_dev->parent_overlap - img_offset;
2179 }
2180
2181 /*
Alex Elder3d7efd12013-04-19 15:34:50 -05002182 * Allocate a page array big enough to receive the data read
2183 * from the parent.
2184 */
2185 page_count = (u32)calc_pages_for(0, length);
2186 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2187 if (IS_ERR(pages)) {
2188 result = PTR_ERR(pages);
2189 pages = NULL;
2190 goto out_err;
2191 }
2192
2193 result = -ENOMEM;
2194 parent_request = rbd_img_request_create(rbd_dev->parent,
2195 img_offset, length,
2196 false, true);
2197 if (!parent_request)
2198 goto out_err;
2199 rbd_obj_request_get(obj_request);
2200 parent_request->obj_request = obj_request;
2201
2202 result = rbd_img_request_fill(parent_request, OBJ_REQUEST_PAGES, pages);
2203 if (result)
2204 goto out_err;
2205 parent_request->copyup_pages = pages;
2206
2207 parent_request->callback = rbd_img_obj_parent_read_full_callback;
2208 result = rbd_img_request_submit(parent_request);
2209 if (!result)
2210 return 0;
2211
2212 parent_request->copyup_pages = NULL;
2213 parent_request->obj_request = NULL;
2214 rbd_obj_request_put(obj_request);
2215out_err:
2216 if (pages)
2217 ceph_release_page_vector(pages, page_count);
2218 if (parent_request)
2219 rbd_img_request_put(parent_request);
2220 obj_request->result = result;
2221 obj_request->xferred = 0;
2222 obj_request_done_set(obj_request);
2223
2224 return result;
2225}
2226
Alex Elderc5b5ef62013-02-11 12:33:24 -06002227static void rbd_img_obj_exists_callback(struct rbd_obj_request *obj_request)
2228{
Alex Elderc5b5ef62013-02-11 12:33:24 -06002229 struct rbd_obj_request *orig_request;
2230 int result;
2231
2232 rbd_assert(!obj_request_img_data_test(obj_request));
2233
2234 /*
2235 * All we need from the object request is the original
2236 * request and the result of the STAT op. Grab those, then
2237 * we're done with the request.
2238 */
2239 orig_request = obj_request->obj_request;
2240 obj_request->obj_request = NULL;
2241 rbd_assert(orig_request);
2242 rbd_assert(orig_request->img_request);
2243
2244 result = obj_request->result;
2245 obj_request->result = 0;
2246
2247 dout("%s: obj %p for obj %p result %d %llu/%llu\n", __func__,
2248 obj_request, orig_request, result,
2249 obj_request->xferred, obj_request->length);
2250 rbd_obj_request_put(obj_request);
2251
2252 rbd_assert(orig_request);
2253 rbd_assert(orig_request->img_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002254
2255 /*
2256 * Our only purpose here is to determine whether the object
2257 * exists, and we don't want to treat the non-existence as
2258 * an error. If something else comes back, transfer the
2259 * error to the original request and complete it now.
2260 */
2261 if (!result) {
2262 obj_request_existence_set(orig_request, true);
2263 } else if (result == -ENOENT) {
2264 obj_request_existence_set(orig_request, false);
2265 } else if (result) {
2266 orig_request->result = result;
Alex Elder3d7efd12013-04-19 15:34:50 -05002267 goto out;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002268 }
2269
2270 /*
2271 * Resubmit the original request now that we have recorded
2272 * whether the target object exists.
2273 */
Alex Elderb454e362013-04-19 15:34:50 -05002274 orig_request->result = rbd_img_obj_request_submit(orig_request);
Alex Elder3d7efd12013-04-19 15:34:50 -05002275out:
Alex Elderc5b5ef62013-02-11 12:33:24 -06002276 if (orig_request->result)
2277 rbd_obj_request_complete(orig_request);
2278 rbd_obj_request_put(orig_request);
2279}
2280
2281static int rbd_img_obj_exists_submit(struct rbd_obj_request *obj_request)
2282{
2283 struct rbd_obj_request *stat_request;
2284 struct rbd_device *rbd_dev;
2285 struct ceph_osd_client *osdc;
2286 struct page **pages = NULL;
2287 u32 page_count;
2288 size_t size;
2289 int ret;
2290
2291 /*
2292 * The response data for a STAT call consists of:
2293 * le64 length;
2294 * struct {
2295 * le32 tv_sec;
2296 * le32 tv_nsec;
2297 * } mtime;
2298 */
2299 size = sizeof (__le64) + sizeof (__le32) + sizeof (__le32);
2300 page_count = (u32)calc_pages_for(0, size);
2301 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2302 if (IS_ERR(pages))
2303 return PTR_ERR(pages);
2304
2305 ret = -ENOMEM;
2306 stat_request = rbd_obj_request_create(obj_request->object_name, 0, 0,
2307 OBJ_REQUEST_PAGES);
2308 if (!stat_request)
2309 goto out;
2310
2311 rbd_obj_request_get(obj_request);
2312 stat_request->obj_request = obj_request;
2313 stat_request->pages = pages;
2314 stat_request->page_count = page_count;
2315
2316 rbd_assert(obj_request->img_request);
2317 rbd_dev = obj_request->img_request->rbd_dev;
2318 stat_request->osd_req = rbd_osd_req_create(rbd_dev, false,
2319 stat_request);
2320 if (!stat_request->osd_req)
2321 goto out;
2322 stat_request->callback = rbd_img_obj_exists_callback;
2323
2324 osd_req_op_init(stat_request->osd_req, 0, CEPH_OSD_OP_STAT);
2325 osd_req_op_raw_data_in_pages(stat_request->osd_req, 0, pages, size, 0,
2326 false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05002327 rbd_osd_req_format_read(stat_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002328
2329 osdc = &rbd_dev->rbd_client->client->osdc;
2330 ret = rbd_obj_request_submit(osdc, stat_request);
2331out:
2332 if (ret)
2333 rbd_obj_request_put(obj_request);
2334
2335 return ret;
2336}
2337
Alex Elderb454e362013-04-19 15:34:50 -05002338static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request)
2339{
2340 struct rbd_img_request *img_request;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002341 struct rbd_device *rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002342 bool known;
Alex Elderb454e362013-04-19 15:34:50 -05002343
2344 rbd_assert(obj_request_img_data_test(obj_request));
2345
2346 img_request = obj_request->img_request;
2347 rbd_assert(img_request);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002348 rbd_dev = img_request->rbd_dev;
Alex Elderb454e362013-04-19 15:34:50 -05002349
Alex Elderb454e362013-04-19 15:34:50 -05002350 /*
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002351 * Only writes to layered images need special handling.
2352 * Reads and non-layered writes are simple object requests.
2353 * Layered writes that start beyond the end of the overlap
2354 * with the parent have no parent data, so they too are
2355 * simple object requests. Finally, if the target object is
2356 * known to already exist, its parent data has already been
2357 * copied, so a write to the object can also be handled as a
2358 * simple object request.
Alex Elderb454e362013-04-19 15:34:50 -05002359 */
2360 if (!img_request_write_test(img_request) ||
2361 !img_request_layered_test(img_request) ||
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002362 rbd_dev->parent_overlap <= obj_request->img_offset ||
Alex Elder3d7efd12013-04-19 15:34:50 -05002363 ((known = obj_request_known_test(obj_request)) &&
2364 obj_request_exists_test(obj_request))) {
Alex Elderb454e362013-04-19 15:34:50 -05002365
2366 struct rbd_device *rbd_dev;
2367 struct ceph_osd_client *osdc;
2368
2369 rbd_dev = obj_request->img_request->rbd_dev;
2370 osdc = &rbd_dev->rbd_client->client->osdc;
2371
2372 return rbd_obj_request_submit(osdc, obj_request);
2373 }
2374
2375 /*
Alex Elder3d7efd12013-04-19 15:34:50 -05002376 * It's a layered write. The target object might exist but
2377 * we may not know that yet. If we know it doesn't exist,
2378 * start by reading the data for the full target object from
2379 * the parent so we can use it for a copyup to the target.
Alex Elderb454e362013-04-19 15:34:50 -05002380 */
Alex Elder3d7efd12013-04-19 15:34:50 -05002381 if (known)
2382 return rbd_img_obj_parent_read_full(obj_request);
2383
2384 /* We don't know whether the target exists. Go find out. */
Alex Elderb454e362013-04-19 15:34:50 -05002385
2386 return rbd_img_obj_exists_submit(obj_request);
2387}
2388
Alex Elderbf0d5f502012-11-22 00:00:08 -06002389static int rbd_img_request_submit(struct rbd_img_request *img_request)
2390{
Alex Elderbf0d5f502012-11-22 00:00:08 -06002391 struct rbd_obj_request *obj_request;
Alex Elder46faeed2013-04-10 17:47:46 -05002392 struct rbd_obj_request *next_obj_request;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002393
Alex Elder37206ee2013-02-20 17:32:08 -06002394 dout("%s: img %p\n", __func__, img_request);
Alex Elder46faeed2013-04-10 17:47:46 -05002395 for_each_obj_request_safe(img_request, obj_request, next_obj_request) {
Alex Elderbf0d5f502012-11-22 00:00:08 -06002396 int ret;
2397
Alex Elderb454e362013-04-19 15:34:50 -05002398 ret = rbd_img_obj_request_submit(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002399 if (ret)
2400 return ret;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002401 }
2402
2403 return 0;
2404}
2405
Alex Elder8b3e1a52013-01-24 16:13:36 -06002406static void rbd_img_parent_read_callback(struct rbd_img_request *img_request)
2407{
2408 struct rbd_obj_request *obj_request;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002409 struct rbd_device *rbd_dev;
2410 u64 obj_end;
Alex Elder8b3e1a52013-01-24 16:13:36 -06002411
2412 rbd_assert(img_request_child_test(img_request));
2413
2414 obj_request = img_request->obj_request;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002415 rbd_assert(obj_request);
2416 rbd_assert(obj_request->img_request);
Alex Elder8b3e1a52013-01-24 16:13:36 -06002417
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002418 obj_request->result = img_request->result;
2419 if (obj_request->result)
2420 goto out;
2421
2422 /*
2423 * We need to zero anything beyond the parent overlap
2424 * boundary. Since rbd_img_obj_request_read_callback()
2425 * will zero anything beyond the end of a short read, an
2426 * easy way to do this is to pretend the data from the
2427 * parent came up short--ending at the overlap boundary.
2428 */
2429 rbd_assert(obj_request->img_offset < U64_MAX - obj_request->length);
2430 obj_end = obj_request->img_offset + obj_request->length;
2431 rbd_dev = obj_request->img_request->rbd_dev;
2432 if (obj_end > rbd_dev->parent_overlap) {
2433 u64 xferred = 0;
2434
2435 if (obj_request->img_offset < rbd_dev->parent_overlap)
2436 xferred = rbd_dev->parent_overlap -
2437 obj_request->img_offset;
2438
2439 obj_request->xferred = min(img_request->xferred, xferred);
2440 } else {
2441 obj_request->xferred = img_request->xferred;
2442 }
2443out:
Alex Elder8b3e1a52013-01-24 16:13:36 -06002444 rbd_img_obj_request_read_callback(obj_request);
2445 rbd_obj_request_complete(obj_request);
2446}
2447
2448static void rbd_img_parent_read(struct rbd_obj_request *obj_request)
2449{
2450 struct rbd_device *rbd_dev;
2451 struct rbd_img_request *img_request;
2452 int result;
2453
2454 rbd_assert(obj_request_img_data_test(obj_request));
2455 rbd_assert(obj_request->img_request != NULL);
2456 rbd_assert(obj_request->result == (s32) -ENOENT);
2457 rbd_assert(obj_request->type == OBJ_REQUEST_BIO);
2458
2459 rbd_dev = obj_request->img_request->rbd_dev;
2460 rbd_assert(rbd_dev->parent != NULL);
2461 /* rbd_read_finish(obj_request, obj_request->length); */
2462 img_request = rbd_img_request_create(rbd_dev->parent,
2463 obj_request->img_offset,
2464 obj_request->length,
2465 false, true);
2466 result = -ENOMEM;
2467 if (!img_request)
2468 goto out_err;
2469
2470 rbd_obj_request_get(obj_request);
2471 img_request->obj_request = obj_request;
2472
Alex Elderf1a47392013-04-19 15:34:50 -05002473 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
2474 obj_request->bio_list);
Alex Elder8b3e1a52013-01-24 16:13:36 -06002475 if (result)
2476 goto out_err;
2477
2478 img_request->callback = rbd_img_parent_read_callback;
2479 result = rbd_img_request_submit(img_request);
2480 if (result)
2481 goto out_err;
2482
2483 return;
2484out_err:
2485 if (img_request)
2486 rbd_img_request_put(img_request);
2487 obj_request->result = result;
2488 obj_request->xferred = 0;
2489 obj_request_done_set(obj_request);
2490}
2491
Alex Eldercf81b602013-01-17 12:18:46 -06002492static int rbd_obj_notify_ack(struct rbd_device *rbd_dev,
Alex Elderb8d70032012-11-30 17:53:04 -06002493 u64 ver, u64 notify_id)
2494{
2495 struct rbd_obj_request *obj_request;
Alex Elder21692382013-04-05 01:27:12 -05002496 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elderb8d70032012-11-30 17:53:04 -06002497 int ret;
2498
2499 obj_request = rbd_obj_request_create(rbd_dev->header_name, 0, 0,
2500 OBJ_REQUEST_NODATA);
2501 if (!obj_request)
2502 return -ENOMEM;
2503
2504 ret = -ENOMEM;
Alex Elder430c28c2013-04-03 21:32:51 -05002505 obj_request->osd_req = rbd_osd_req_create(rbd_dev, false, obj_request);
Alex Elderb8d70032012-11-30 17:53:04 -06002506 if (!obj_request->osd_req)
2507 goto out;
Alex Elder21692382013-04-05 01:27:12 -05002508 obj_request->callback = rbd_obj_request_put;
Alex Elderb8d70032012-11-30 17:53:04 -06002509
Alex Elderc99d2d42013-04-05 01:27:11 -05002510 osd_req_op_watch_init(obj_request->osd_req, 0, CEPH_OSD_OP_NOTIFY_ACK,
2511 notify_id, ver, 0);
Alex Elder9d4df012013-04-19 15:34:50 -05002512 rbd_osd_req_format_read(obj_request);
Alex Elder430c28c2013-04-03 21:32:51 -05002513
Alex Elderb8d70032012-11-30 17:53:04 -06002514 ret = rbd_obj_request_submit(osdc, obj_request);
Alex Elderb8d70032012-11-30 17:53:04 -06002515out:
Alex Eldercf81b602013-01-17 12:18:46 -06002516 if (ret)
2517 rbd_obj_request_put(obj_request);
Alex Elderb8d70032012-11-30 17:53:04 -06002518
2519 return ret;
2520}
2521
2522static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
2523{
2524 struct rbd_device *rbd_dev = (struct rbd_device *)data;
2525 u64 hver;
2526 int rc;
2527
2528 if (!rbd_dev)
2529 return;
2530
Alex Elder37206ee2013-02-20 17:32:08 -06002531 dout("%s: \"%s\" notify_id %llu opcode %u\n", __func__,
Alex Elderb8d70032012-11-30 17:53:04 -06002532 rbd_dev->header_name, (unsigned long long) notify_id,
2533 (unsigned int) opcode);
2534 rc = rbd_dev_refresh(rbd_dev, &hver);
2535 if (rc)
2536 rbd_warn(rbd_dev, "got notification but failed to "
2537 " update snaps: %d\n", rc);
2538
Alex Eldercf81b602013-01-17 12:18:46 -06002539 rbd_obj_notify_ack(rbd_dev, hver, notify_id);
Alex Elderb8d70032012-11-30 17:53:04 -06002540}
2541
Alex Elder9969ebc2013-01-18 12:31:10 -06002542/*
2543 * Request sync osd watch/unwatch. The value of "start" determines
2544 * whether a watch request is being initiated or torn down.
2545 */
2546static int rbd_dev_header_watch_sync(struct rbd_device *rbd_dev, int start)
2547{
2548 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
2549 struct rbd_obj_request *obj_request;
Alex Elder9969ebc2013-01-18 12:31:10 -06002550 int ret;
2551
2552 rbd_assert(start ^ !!rbd_dev->watch_event);
2553 rbd_assert(start ^ !!rbd_dev->watch_request);
2554
2555 if (start) {
Alex Elder3c663bb2013-02-15 11:42:30 -06002556 ret = ceph_osdc_create_event(osdc, rbd_watch_cb, rbd_dev,
Alex Elder9969ebc2013-01-18 12:31:10 -06002557 &rbd_dev->watch_event);
2558 if (ret < 0)
2559 return ret;
Alex Elder8eb87562013-01-25 17:08:55 -06002560 rbd_assert(rbd_dev->watch_event != NULL);
Alex Elder9969ebc2013-01-18 12:31:10 -06002561 }
2562
2563 ret = -ENOMEM;
2564 obj_request = rbd_obj_request_create(rbd_dev->header_name, 0, 0,
2565 OBJ_REQUEST_NODATA);
2566 if (!obj_request)
2567 goto out_cancel;
2568
Alex Elder430c28c2013-04-03 21:32:51 -05002569 obj_request->osd_req = rbd_osd_req_create(rbd_dev, true, obj_request);
2570 if (!obj_request->osd_req)
2571 goto out_cancel;
2572
Alex Elder8eb87562013-01-25 17:08:55 -06002573 if (start)
Alex Elder975241a2013-01-25 17:08:55 -06002574 ceph_osdc_set_request_linger(osdc, obj_request->osd_req);
Alex Elder8eb87562013-01-25 17:08:55 -06002575 else
Alex Elder6977c3f2013-01-25 17:08:55 -06002576 ceph_osdc_unregister_linger_request(osdc,
Alex Elder975241a2013-01-25 17:08:55 -06002577 rbd_dev->watch_request->osd_req);
Alex Elder21692382013-04-05 01:27:12 -05002578
2579 osd_req_op_watch_init(obj_request->osd_req, 0, CEPH_OSD_OP_WATCH,
2580 rbd_dev->watch_event->cookie,
2581 rbd_dev->header.obj_version, start);
Alex Elder9d4df012013-04-19 15:34:50 -05002582 rbd_osd_req_format_write(obj_request);
Alex Elder21692382013-04-05 01:27:12 -05002583
Alex Elder9969ebc2013-01-18 12:31:10 -06002584 ret = rbd_obj_request_submit(osdc, obj_request);
2585 if (ret)
2586 goto out_cancel;
2587 ret = rbd_obj_request_wait(obj_request);
2588 if (ret)
2589 goto out_cancel;
Alex Elder9969ebc2013-01-18 12:31:10 -06002590 ret = obj_request->result;
2591 if (ret)
2592 goto out_cancel;
2593
Alex Elder8eb87562013-01-25 17:08:55 -06002594 /*
2595 * A watch request is set to linger, so the underlying osd
2596 * request won't go away until we unregister it. We retain
2597 * a pointer to the object request during that time (in
2598 * rbd_dev->watch_request), so we'll keep a reference to
2599 * it. We'll drop that reference (below) after we've
2600 * unregistered it.
2601 */
2602 if (start) {
2603 rbd_dev->watch_request = obj_request;
2604
2605 return 0;
2606 }
2607
2608 /* We have successfully torn down the watch request */
2609
2610 rbd_obj_request_put(rbd_dev->watch_request);
2611 rbd_dev->watch_request = NULL;
Alex Elder9969ebc2013-01-18 12:31:10 -06002612out_cancel:
2613 /* Cancel the event if we're tearing down, or on error */
2614 ceph_osdc_cancel_event(rbd_dev->watch_event);
2615 rbd_dev->watch_event = NULL;
Alex Elder9969ebc2013-01-18 12:31:10 -06002616 if (obj_request)
2617 rbd_obj_request_put(obj_request);
2618
2619 return ret;
2620}
2621
Alex Elder36be9a72013-01-19 00:30:28 -06002622/*
2623 * Synchronous osd object method call
2624 */
2625static int rbd_obj_method_sync(struct rbd_device *rbd_dev,
2626 const char *object_name,
2627 const char *class_name,
2628 const char *method_name,
Alex Elder41579762013-04-21 12:14:45 -05002629 const void *outbound,
Alex Elder36be9a72013-01-19 00:30:28 -06002630 size_t outbound_size,
Alex Elder41579762013-04-21 12:14:45 -05002631 void *inbound,
Alex Elder36be9a72013-01-19 00:30:28 -06002632 size_t inbound_size,
2633 u64 *version)
2634{
Alex Elder21692382013-04-05 01:27:12 -05002635 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder36be9a72013-01-19 00:30:28 -06002636 struct rbd_obj_request *obj_request;
Alex Elder36be9a72013-01-19 00:30:28 -06002637 struct page **pages;
2638 u32 page_count;
2639 int ret;
2640
2641 /*
Alex Elder6010a452013-04-05 01:27:11 -05002642 * Method calls are ultimately read operations. The result
2643 * should placed into the inbound buffer provided. They
2644 * also supply outbound data--parameters for the object
2645 * method. Currently if this is present it will be a
2646 * snapshot id.
Alex Elder36be9a72013-01-19 00:30:28 -06002647 */
Alex Elder57385b52013-04-21 12:14:45 -05002648 page_count = (u32)calc_pages_for(0, inbound_size);
Alex Elder36be9a72013-01-19 00:30:28 -06002649 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2650 if (IS_ERR(pages))
2651 return PTR_ERR(pages);
2652
2653 ret = -ENOMEM;
Alex Elder6010a452013-04-05 01:27:11 -05002654 obj_request = rbd_obj_request_create(object_name, 0, inbound_size,
Alex Elder36be9a72013-01-19 00:30:28 -06002655 OBJ_REQUEST_PAGES);
2656 if (!obj_request)
2657 goto out;
2658
2659 obj_request->pages = pages;
2660 obj_request->page_count = page_count;
2661
Alex Elder430c28c2013-04-03 21:32:51 -05002662 obj_request->osd_req = rbd_osd_req_create(rbd_dev, false, obj_request);
Alex Elder36be9a72013-01-19 00:30:28 -06002663 if (!obj_request->osd_req)
2664 goto out;
2665
Alex Elderc99d2d42013-04-05 01:27:11 -05002666 osd_req_op_cls_init(obj_request->osd_req, 0, CEPH_OSD_OP_CALL,
Alex Elder04017e22013-04-05 14:46:02 -05002667 class_name, method_name);
2668 if (outbound_size) {
2669 struct ceph_pagelist *pagelist;
2670
2671 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
2672 if (!pagelist)
2673 goto out;
2674
2675 ceph_pagelist_init(pagelist);
2676 ceph_pagelist_append(pagelist, outbound, outbound_size);
2677 osd_req_op_cls_request_data_pagelist(obj_request->osd_req, 0,
2678 pagelist);
2679 }
Alex Eldera4ce40a2013-04-05 01:27:12 -05002680 osd_req_op_cls_response_data_pages(obj_request->osd_req, 0,
2681 obj_request->pages, inbound_size,
Alex Elder44cd1882013-04-05 01:27:12 -05002682 0, false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05002683 rbd_osd_req_format_read(obj_request);
Alex Elder430c28c2013-04-03 21:32:51 -05002684
Alex Elder36be9a72013-01-19 00:30:28 -06002685 ret = rbd_obj_request_submit(osdc, obj_request);
2686 if (ret)
2687 goto out;
2688 ret = rbd_obj_request_wait(obj_request);
2689 if (ret)
2690 goto out;
2691
2692 ret = obj_request->result;
2693 if (ret < 0)
2694 goto out;
Alex Elder57385b52013-04-21 12:14:45 -05002695
2696 rbd_assert(obj_request->xferred < (u64)INT_MAX);
2697 ret = (int)obj_request->xferred;
Alex Elder903bb322013-02-06 13:11:38 -06002698 ceph_copy_from_page_vector(pages, inbound, 0, obj_request->xferred);
Alex Elder36be9a72013-01-19 00:30:28 -06002699 if (version)
2700 *version = obj_request->version;
2701out:
2702 if (obj_request)
2703 rbd_obj_request_put(obj_request);
2704 else
2705 ceph_release_page_vector(pages, page_count);
2706
2707 return ret;
2708}
2709
Alex Elderbf0d5f502012-11-22 00:00:08 -06002710static void rbd_request_fn(struct request_queue *q)
Alex Eldercc344fa2013-02-19 12:25:56 -06002711 __releases(q->queue_lock) __acquires(q->queue_lock)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002712{
2713 struct rbd_device *rbd_dev = q->queuedata;
2714 bool read_only = rbd_dev->mapping.read_only;
2715 struct request *rq;
2716 int result;
2717
2718 while ((rq = blk_fetch_request(q))) {
2719 bool write_request = rq_data_dir(rq) == WRITE;
2720 struct rbd_img_request *img_request;
2721 u64 offset;
2722 u64 length;
2723
2724 /* Ignore any non-FS requests that filter through. */
2725
2726 if (rq->cmd_type != REQ_TYPE_FS) {
Alex Elder4dda41d2013-02-20 21:59:33 -06002727 dout("%s: non-fs request type %d\n", __func__,
2728 (int) rq->cmd_type);
2729 __blk_end_request_all(rq, 0);
2730 continue;
2731 }
2732
2733 /* Ignore/skip any zero-length requests */
2734
2735 offset = (u64) blk_rq_pos(rq) << SECTOR_SHIFT;
2736 length = (u64) blk_rq_bytes(rq);
2737
2738 if (!length) {
2739 dout("%s: zero-length request\n", __func__);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002740 __blk_end_request_all(rq, 0);
2741 continue;
2742 }
2743
2744 spin_unlock_irq(q->queue_lock);
2745
2746 /* Disallow writes to a read-only device */
2747
2748 if (write_request) {
2749 result = -EROFS;
2750 if (read_only)
2751 goto end_request;
2752 rbd_assert(rbd_dev->spec->snap_id == CEPH_NOSNAP);
2753 }
2754
Alex Elder6d292902013-01-14 12:43:31 -06002755 /*
2756 * Quit early if the mapped snapshot no longer
2757 * exists. It's still possible the snapshot will
2758 * have disappeared by the time our request arrives
2759 * at the osd, but there's no sense in sending it if
2760 * we already know.
2761 */
2762 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) {
Alex Elderbf0d5f502012-11-22 00:00:08 -06002763 dout("request for non-existent snapshot");
2764 rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP);
2765 result = -ENXIO;
2766 goto end_request;
2767 }
2768
Alex Elderbf0d5f502012-11-22 00:00:08 -06002769 result = -EINVAL;
2770 if (WARN_ON(offset && length > U64_MAX - offset + 1))
2771 goto end_request; /* Shouldn't happen */
2772
2773 result = -ENOMEM;
2774 img_request = rbd_img_request_create(rbd_dev, offset, length,
Alex Elder9849e982013-01-24 16:13:36 -06002775 write_request, false);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002776 if (!img_request)
2777 goto end_request;
2778
2779 img_request->rq = rq;
2780
Alex Elderf1a47392013-04-19 15:34:50 -05002781 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
2782 rq->bio);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002783 if (!result)
2784 result = rbd_img_request_submit(img_request);
2785 if (result)
2786 rbd_img_request_put(img_request);
2787end_request:
2788 spin_lock_irq(q->queue_lock);
2789 if (result < 0) {
Alex Elder7da22d22013-01-24 16:13:36 -06002790 rbd_warn(rbd_dev, "%s %llx at %llx result %d\n",
2791 write_request ? "write" : "read",
2792 length, offset, result);
2793
Alex Elderbf0d5f502012-11-22 00:00:08 -06002794 __blk_end_request_all(rq, result);
2795 }
2796 }
2797}
2798
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002799/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002800 * a queue callback. Makes sure that we don't create a bio that spans across
2801 * multiple osd objects. One exception would be with a single page bios,
Alex Elderf7760da2012-10-20 22:17:27 -05002802 * which we handle later at bio_chain_clone_range()
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002803 */
2804static int rbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd,
2805 struct bio_vec *bvec)
2806{
2807 struct rbd_device *rbd_dev = q->queuedata;
Alex Eldere5cfeed22012-10-20 22:17:27 -05002808 sector_t sector_offset;
2809 sector_t sectors_per_obj;
2810 sector_t obj_sector_offset;
2811 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002812
Alex Eldere5cfeed22012-10-20 22:17:27 -05002813 /*
2814 * Find how far into its rbd object the partition-relative
2815 * bio start sector is to offset relative to the enclosing
2816 * device.
2817 */
2818 sector_offset = get_start_sect(bmd->bi_bdev) + bmd->bi_sector;
2819 sectors_per_obj = 1 << (rbd_dev->header.obj_order - SECTOR_SHIFT);
2820 obj_sector_offset = sector_offset & (sectors_per_obj - 1);
Alex Elder593a9e72012-02-07 12:03:37 -06002821
Alex Eldere5cfeed22012-10-20 22:17:27 -05002822 /*
2823 * Compute the number of bytes from that offset to the end
2824 * of the object. Account for what's already used by the bio.
2825 */
2826 ret = (int) (sectors_per_obj - obj_sector_offset) << SECTOR_SHIFT;
2827 if (ret > bmd->bi_size)
2828 ret -= bmd->bi_size;
2829 else
2830 ret = 0;
2831
2832 /*
2833 * Don't send back more than was asked for. And if the bio
2834 * was empty, let the whole thing through because: "Note
2835 * that a block device *must* allow a single page to be
2836 * added to an empty bio."
2837 */
2838 rbd_assert(bvec->bv_len <= PAGE_SIZE);
2839 if (ret > (int) bvec->bv_len || !bmd->bi_size)
2840 ret = (int) bvec->bv_len;
2841
2842 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002843}
2844
2845static void rbd_free_disk(struct rbd_device *rbd_dev)
2846{
2847 struct gendisk *disk = rbd_dev->disk;
2848
2849 if (!disk)
2850 return;
2851
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002852 if (disk->flags & GENHD_FL_UP)
2853 del_gendisk(disk);
2854 if (disk->queue)
2855 blk_cleanup_queue(disk->queue);
2856 put_disk(disk);
2857}
2858
Alex Elder788e2df2013-01-17 12:25:27 -06002859static int rbd_obj_read_sync(struct rbd_device *rbd_dev,
2860 const char *object_name,
2861 u64 offset, u64 length,
Alex Elder80ef15b2013-04-21 12:14:45 -05002862 void *buf, u64 *version)
Alex Elder788e2df2013-01-17 12:25:27 -06002863
2864{
Alex Elder21692382013-04-05 01:27:12 -05002865 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder788e2df2013-01-17 12:25:27 -06002866 struct rbd_obj_request *obj_request;
Alex Elder788e2df2013-01-17 12:25:27 -06002867 struct page **pages = NULL;
2868 u32 page_count;
Alex Elder1ceae7e2013-02-06 13:11:38 -06002869 size_t size;
Alex Elder788e2df2013-01-17 12:25:27 -06002870 int ret;
2871
2872 page_count = (u32) calc_pages_for(offset, length);
2873 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2874 if (IS_ERR(pages))
2875 ret = PTR_ERR(pages);
2876
2877 ret = -ENOMEM;
2878 obj_request = rbd_obj_request_create(object_name, offset, length,
Alex Elder36be9a72013-01-19 00:30:28 -06002879 OBJ_REQUEST_PAGES);
Alex Elder788e2df2013-01-17 12:25:27 -06002880 if (!obj_request)
2881 goto out;
2882
2883 obj_request->pages = pages;
2884 obj_request->page_count = page_count;
2885
Alex Elder430c28c2013-04-03 21:32:51 -05002886 obj_request->osd_req = rbd_osd_req_create(rbd_dev, false, obj_request);
Alex Elder788e2df2013-01-17 12:25:27 -06002887 if (!obj_request->osd_req)
2888 goto out;
2889
Alex Elderc99d2d42013-04-05 01:27:11 -05002890 osd_req_op_extent_init(obj_request->osd_req, 0, CEPH_OSD_OP_READ,
2891 offset, length, 0, 0);
Alex Elder406e2c92013-04-15 14:50:36 -05002892 osd_req_op_extent_osd_data_pages(obj_request->osd_req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05002893 obj_request->pages,
Alex Elder44cd1882013-04-05 01:27:12 -05002894 obj_request->length,
2895 obj_request->offset & ~PAGE_MASK,
2896 false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05002897 rbd_osd_req_format_read(obj_request);
Alex Elder430c28c2013-04-03 21:32:51 -05002898
Alex Elder788e2df2013-01-17 12:25:27 -06002899 ret = rbd_obj_request_submit(osdc, obj_request);
2900 if (ret)
2901 goto out;
2902 ret = rbd_obj_request_wait(obj_request);
2903 if (ret)
2904 goto out;
2905
2906 ret = obj_request->result;
2907 if (ret < 0)
2908 goto out;
Alex Elder1ceae7e2013-02-06 13:11:38 -06002909
2910 rbd_assert(obj_request->xferred <= (u64) SIZE_MAX);
2911 size = (size_t) obj_request->xferred;
Alex Elder903bb322013-02-06 13:11:38 -06002912 ceph_copy_from_page_vector(pages, buf, 0, size);
Alex Elder23ed6e12013-02-06 13:11:38 -06002913 rbd_assert(size <= (size_t) INT_MAX);
2914 ret = (int) size;
Alex Elder788e2df2013-01-17 12:25:27 -06002915 if (version)
2916 *version = obj_request->version;
2917out:
2918 if (obj_request)
2919 rbd_obj_request_put(obj_request);
2920 else
2921 ceph_release_page_vector(pages, page_count);
2922
2923 return ret;
2924}
2925
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002926/*
Alex Elder4156d992012-08-02 11:29:46 -05002927 * Read the complete header for the given rbd device.
2928 *
2929 * Returns a pointer to a dynamically-allocated buffer containing
2930 * the complete and validated header. Caller can pass the address
2931 * of a variable that will be filled in with the version of the
2932 * header object at the time it was read.
2933 *
2934 * Returns a pointer-coded errno if a failure occurs.
2935 */
2936static struct rbd_image_header_ondisk *
2937rbd_dev_v1_header_read(struct rbd_device *rbd_dev, u64 *version)
2938{
2939 struct rbd_image_header_ondisk *ondisk = NULL;
2940 u32 snap_count = 0;
2941 u64 names_size = 0;
2942 u32 want_count;
2943 int ret;
2944
2945 /*
2946 * The complete header will include an array of its 64-bit
2947 * snapshot ids, followed by the names of those snapshots as
2948 * a contiguous block of NUL-terminated strings. Note that
2949 * the number of snapshots could change by the time we read
2950 * it in, in which case we re-read it.
2951 */
2952 do {
2953 size_t size;
2954
2955 kfree(ondisk);
2956
2957 size = sizeof (*ondisk);
2958 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
2959 size += names_size;
2960 ondisk = kmalloc(size, GFP_KERNEL);
2961 if (!ondisk)
2962 return ERR_PTR(-ENOMEM);
2963
Alex Elder788e2df2013-01-17 12:25:27 -06002964 ret = rbd_obj_read_sync(rbd_dev, rbd_dev->header_name,
Alex Elder80ef15b2013-04-21 12:14:45 -05002965 0, size, ondisk, version);
Alex Elder4156d992012-08-02 11:29:46 -05002966 if (ret < 0)
2967 goto out_err;
2968 if (WARN_ON((size_t) ret < size)) {
2969 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05002970 rbd_warn(rbd_dev, "short header read (want %zd got %d)",
2971 size, ret);
Alex Elder4156d992012-08-02 11:29:46 -05002972 goto out_err;
2973 }
2974 if (!rbd_dev_ondisk_valid(ondisk)) {
2975 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05002976 rbd_warn(rbd_dev, "invalid header");
Alex Elder4156d992012-08-02 11:29:46 -05002977 goto out_err;
2978 }
2979
2980 names_size = le64_to_cpu(ondisk->snap_names_len);
2981 want_count = snap_count;
2982 snap_count = le32_to_cpu(ondisk->snap_count);
2983 } while (snap_count != want_count);
2984
2985 return ondisk;
2986
2987out_err:
2988 kfree(ondisk);
2989
2990 return ERR_PTR(ret);
2991}
2992
2993/*
2994 * reload the ondisk the header
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002995 */
2996static int rbd_read_header(struct rbd_device *rbd_dev,
2997 struct rbd_image_header *header)
2998{
Alex Elder4156d992012-08-02 11:29:46 -05002999 struct rbd_image_header_ondisk *ondisk;
3000 u64 ver = 0;
3001 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003002
Alex Elder4156d992012-08-02 11:29:46 -05003003 ondisk = rbd_dev_v1_header_read(rbd_dev, &ver);
3004 if (IS_ERR(ondisk))
3005 return PTR_ERR(ondisk);
3006 ret = rbd_header_from_disk(header, ondisk);
3007 if (ret >= 0)
3008 header->obj_version = ver;
3009 kfree(ondisk);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003010
Alex Elder4156d992012-08-02 11:29:46 -05003011 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003012}
3013
Alex Elder41f38c22012-10-25 23:34:40 -05003014static void rbd_remove_all_snaps(struct rbd_device *rbd_dev)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003015{
3016 struct rbd_snap *snap;
Alex Eldera0593292012-07-19 09:09:27 -05003017 struct rbd_snap *next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003018
Alex Eldera0593292012-07-19 09:09:27 -05003019 list_for_each_entry_safe(snap, next, &rbd_dev->snaps, node)
Alex Elder41f38c22012-10-25 23:34:40 -05003020 rbd_remove_snap_dev(snap);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003021}
3022
Alex Elder94785542012-10-09 13:50:17 -07003023static void rbd_update_mapping_size(struct rbd_device *rbd_dev)
3024{
3025 sector_t size;
3026
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003027 if (rbd_dev->spec->snap_id != CEPH_NOSNAP)
Alex Elder94785542012-10-09 13:50:17 -07003028 return;
3029
3030 size = (sector_t) rbd_dev->header.image_size / SECTOR_SIZE;
3031 dout("setting size to %llu sectors", (unsigned long long) size);
3032 rbd_dev->mapping.size = (u64) size;
3033 set_capacity(rbd_dev->disk, size);
3034}
3035
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003036/*
3037 * only read the first part of the ondisk header, without the snaps info
3038 */
Alex Elder117973f2012-08-31 17:29:55 -05003039static int rbd_dev_v1_refresh(struct rbd_device *rbd_dev, u64 *hver)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003040{
3041 int ret;
3042 struct rbd_image_header h;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003043
3044 ret = rbd_read_header(rbd_dev, &h);
3045 if (ret < 0)
3046 return ret;
3047
Josh Durgina51aa0c2011-12-05 10:35:04 -08003048 down_write(&rbd_dev->header_rwsem);
3049
Alex Elder94785542012-10-09 13:50:17 -07003050 /* Update image size, and check for resize of mapped image */
3051 rbd_dev->header.image_size = h.image_size;
3052 rbd_update_mapping_size(rbd_dev);
Sage Weil9db4b3e2011-04-19 22:49:06 -07003053
Alex Elder849b4262012-07-09 21:04:24 -05003054 /* rbd_dev->header.object_prefix shouldn't change */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003055 kfree(rbd_dev->header.snap_sizes);
Alex Elder849b4262012-07-09 21:04:24 -05003056 kfree(rbd_dev->header.snap_names);
Josh Durgind1d25642011-12-05 14:03:05 -08003057 /* osd requests may still refer to snapc */
3058 ceph_put_snap_context(rbd_dev->header.snapc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003059
Alex Elderb8136232012-07-25 09:32:41 -05003060 if (hver)
3061 *hver = h.obj_version;
Josh Durgina71b8912011-12-05 18:10:44 -08003062 rbd_dev->header.obj_version = h.obj_version;
Josh Durgin93a24e02011-12-05 10:41:28 -08003063 rbd_dev->header.image_size = h.image_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003064 rbd_dev->header.snapc = h.snapc;
3065 rbd_dev->header.snap_names = h.snap_names;
3066 rbd_dev->header.snap_sizes = h.snap_sizes;
Alex Elder849b4262012-07-09 21:04:24 -05003067 /* Free the extra copy of the object prefix */
3068 WARN_ON(strcmp(rbd_dev->header.object_prefix, h.object_prefix));
3069 kfree(h.object_prefix);
3070
Alex Elder304f6802012-08-31 17:29:52 -05003071 ret = rbd_dev_snaps_update(rbd_dev);
3072 if (!ret)
3073 ret = rbd_dev_snaps_register(rbd_dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003074
Josh Durginc6666012011-11-21 17:11:12 -08003075 up_write(&rbd_dev->header_rwsem);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003076
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003077 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003078}
3079
Alex Elder117973f2012-08-31 17:29:55 -05003080static int rbd_dev_refresh(struct rbd_device *rbd_dev, u64 *hver)
Alex Elder1fe5e992012-07-25 09:32:41 -05003081{
3082 int ret;
3083
Alex Elder117973f2012-08-31 17:29:55 -05003084 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
Alex Elder1fe5e992012-07-25 09:32:41 -05003085 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Alex Elder117973f2012-08-31 17:29:55 -05003086 if (rbd_dev->image_format == 1)
3087 ret = rbd_dev_v1_refresh(rbd_dev, hver);
3088 else
3089 ret = rbd_dev_v2_refresh(rbd_dev, hver);
Alex Elder1fe5e992012-07-25 09:32:41 -05003090 mutex_unlock(&ctl_mutex);
Laurent Barbed98df632013-04-10 17:47:46 -05003091 revalidate_disk(rbd_dev->disk);
Alex Elder1fe5e992012-07-25 09:32:41 -05003092
3093 return ret;
3094}
3095
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003096static int rbd_init_disk(struct rbd_device *rbd_dev)
3097{
3098 struct gendisk *disk;
3099 struct request_queue *q;
Alex Elder593a9e72012-02-07 12:03:37 -06003100 u64 segment_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003101
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003102 /* create gendisk info */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003103 disk = alloc_disk(RBD_MINORS_PER_MAJOR);
3104 if (!disk)
Alex Elder1fcdb8a2012-08-29 17:11:06 -05003105 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003106
Alex Elderf0f8cef2012-01-29 13:57:44 -06003107 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
Alex Elderde71a292012-07-03 16:01:19 -05003108 rbd_dev->dev_id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003109 disk->major = rbd_dev->major;
3110 disk->first_minor = 0;
3111 disk->fops = &rbd_bd_ops;
3112 disk->private_data = rbd_dev;
3113
Alex Elderbf0d5f502012-11-22 00:00:08 -06003114 q = blk_init_queue(rbd_request_fn, &rbd_dev->lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003115 if (!q)
3116 goto out_disk;
Josh Durgin029bcbd2011-07-22 11:35:23 -07003117
Alex Elder593a9e72012-02-07 12:03:37 -06003118 /* We use the default size, but let's be explicit about it. */
3119 blk_queue_physical_block_size(q, SECTOR_SIZE);
3120
Josh Durgin029bcbd2011-07-22 11:35:23 -07003121 /* set io sizes to object size */
Alex Elder593a9e72012-02-07 12:03:37 -06003122 segment_size = rbd_obj_bytes(&rbd_dev->header);
3123 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
3124 blk_queue_max_segment_size(q, segment_size);
3125 blk_queue_io_min(q, segment_size);
3126 blk_queue_io_opt(q, segment_size);
Josh Durgin029bcbd2011-07-22 11:35:23 -07003127
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003128 blk_queue_merge_bvec(q, rbd_merge_bvec);
3129 disk->queue = q;
3130
3131 q->queuedata = rbd_dev;
3132
3133 rbd_dev->disk = disk;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003134
Alex Elder12f02942012-08-29 17:11:07 -05003135 set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
3136
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003137 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003138out_disk:
3139 put_disk(disk);
Alex Elder1fcdb8a2012-08-29 17:11:06 -05003140
3141 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003142}
3143
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003144/*
3145 sysfs
3146*/
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003147
Alex Elder593a9e72012-02-07 12:03:37 -06003148static struct rbd_device *dev_to_rbd_dev(struct device *dev)
3149{
3150 return container_of(dev, struct rbd_device, dev);
3151}
3152
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003153static ssize_t rbd_size_show(struct device *dev,
3154 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003155{
Alex Elder593a9e72012-02-07 12:03:37 -06003156 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Josh Durgina51aa0c2011-12-05 10:35:04 -08003157 sector_t size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003158
Josh Durgina51aa0c2011-12-05 10:35:04 -08003159 down_read(&rbd_dev->header_rwsem);
3160 size = get_capacity(rbd_dev->disk);
3161 up_read(&rbd_dev->header_rwsem);
3162
3163 return sprintf(buf, "%llu\n", (unsigned long long) size * SECTOR_SIZE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003164}
3165
Alex Elder34b13182012-07-13 20:35:12 -05003166/*
3167 * Note this shows the features for whatever's mapped, which is not
3168 * necessarily the base image.
3169 */
3170static ssize_t rbd_features_show(struct device *dev,
3171 struct device_attribute *attr, char *buf)
3172{
3173 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3174
3175 return sprintf(buf, "0x%016llx\n",
3176 (unsigned long long) rbd_dev->mapping.features);
3177}
3178
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003179static ssize_t rbd_major_show(struct device *dev,
3180 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003181{
Alex Elder593a9e72012-02-07 12:03:37 -06003182 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003183
3184 return sprintf(buf, "%d\n", rbd_dev->major);
3185}
3186
3187static ssize_t rbd_client_id_show(struct device *dev,
3188 struct device_attribute *attr, char *buf)
3189{
Alex Elder593a9e72012-02-07 12:03:37 -06003190 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003191
Alex Elder1dbb4392012-01-24 10:08:37 -06003192 return sprintf(buf, "client%lld\n",
3193 ceph_client_id(rbd_dev->rbd_client->client));
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003194}
3195
3196static ssize_t rbd_pool_show(struct device *dev,
3197 struct device_attribute *attr, char *buf)
3198{
Alex Elder593a9e72012-02-07 12:03:37 -06003199 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003200
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003201 return sprintf(buf, "%s\n", rbd_dev->spec->pool_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003202}
3203
Alex Elder9bb2f332012-07-12 10:46:35 -05003204static ssize_t rbd_pool_id_show(struct device *dev,
3205 struct device_attribute *attr, char *buf)
3206{
3207 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3208
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003209 return sprintf(buf, "%llu\n",
3210 (unsigned long long) rbd_dev->spec->pool_id);
Alex Elder9bb2f332012-07-12 10:46:35 -05003211}
3212
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003213static ssize_t rbd_name_show(struct device *dev,
3214 struct device_attribute *attr, char *buf)
3215{
Alex Elder593a9e72012-02-07 12:03:37 -06003216 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003217
Alex Eldera92ffdf2012-10-30 19:40:33 -05003218 if (rbd_dev->spec->image_name)
3219 return sprintf(buf, "%s\n", rbd_dev->spec->image_name);
3220
3221 return sprintf(buf, "(unknown)\n");
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003222}
3223
Alex Elder589d30e2012-07-10 20:30:11 -05003224static ssize_t rbd_image_id_show(struct device *dev,
3225 struct device_attribute *attr, char *buf)
3226{
3227 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3228
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003229 return sprintf(buf, "%s\n", rbd_dev->spec->image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05003230}
3231
Alex Elder34b13182012-07-13 20:35:12 -05003232/*
3233 * Shows the name of the currently-mapped snapshot (or
3234 * RBD_SNAP_HEAD_NAME for the base image).
3235 */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003236static ssize_t rbd_snap_show(struct device *dev,
3237 struct device_attribute *attr,
3238 char *buf)
3239{
Alex Elder593a9e72012-02-07 12:03:37 -06003240 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003241
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003242 return sprintf(buf, "%s\n", rbd_dev->spec->snap_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003243}
3244
Alex Elder86b00e02012-10-25 23:34:42 -05003245/*
3246 * For an rbd v2 image, shows the pool id, image id, and snapshot id
3247 * for the parent image. If there is no parent, simply shows
3248 * "(no parent image)".
3249 */
3250static ssize_t rbd_parent_show(struct device *dev,
3251 struct device_attribute *attr,
3252 char *buf)
3253{
3254 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3255 struct rbd_spec *spec = rbd_dev->parent_spec;
3256 int count;
3257 char *bufp = buf;
3258
3259 if (!spec)
3260 return sprintf(buf, "(no parent image)\n");
3261
3262 count = sprintf(bufp, "pool_id %llu\npool_name %s\n",
3263 (unsigned long long) spec->pool_id, spec->pool_name);
3264 if (count < 0)
3265 return count;
3266 bufp += count;
3267
3268 count = sprintf(bufp, "image_id %s\nimage_name %s\n", spec->image_id,
3269 spec->image_name ? spec->image_name : "(unknown)");
3270 if (count < 0)
3271 return count;
3272 bufp += count;
3273
3274 count = sprintf(bufp, "snap_id %llu\nsnap_name %s\n",
3275 (unsigned long long) spec->snap_id, spec->snap_name);
3276 if (count < 0)
3277 return count;
3278 bufp += count;
3279
3280 count = sprintf(bufp, "overlap %llu\n", rbd_dev->parent_overlap);
3281 if (count < 0)
3282 return count;
3283 bufp += count;
3284
3285 return (ssize_t) (bufp - buf);
3286}
3287
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003288static ssize_t rbd_image_refresh(struct device *dev,
3289 struct device_attribute *attr,
3290 const char *buf,
3291 size_t size)
3292{
Alex Elder593a9e72012-02-07 12:03:37 -06003293 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Alex Elderb8136232012-07-25 09:32:41 -05003294 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003295
Alex Elder117973f2012-08-31 17:29:55 -05003296 ret = rbd_dev_refresh(rbd_dev, NULL);
Alex Elderb8136232012-07-25 09:32:41 -05003297
3298 return ret < 0 ? ret : size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003299}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003300
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003301static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
Alex Elder34b13182012-07-13 20:35:12 -05003302static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003303static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
3304static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
3305static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
Alex Elder9bb2f332012-07-12 10:46:35 -05003306static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003307static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
Alex Elder589d30e2012-07-10 20:30:11 -05003308static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003309static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
3310static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
Alex Elder86b00e02012-10-25 23:34:42 -05003311static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003312
3313static struct attribute *rbd_attrs[] = {
3314 &dev_attr_size.attr,
Alex Elder34b13182012-07-13 20:35:12 -05003315 &dev_attr_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003316 &dev_attr_major.attr,
3317 &dev_attr_client_id.attr,
3318 &dev_attr_pool.attr,
Alex Elder9bb2f332012-07-12 10:46:35 -05003319 &dev_attr_pool_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003320 &dev_attr_name.attr,
Alex Elder589d30e2012-07-10 20:30:11 -05003321 &dev_attr_image_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003322 &dev_attr_current_snap.attr,
Alex Elder86b00e02012-10-25 23:34:42 -05003323 &dev_attr_parent.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003324 &dev_attr_refresh.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003325 NULL
3326};
3327
3328static struct attribute_group rbd_attr_group = {
3329 .attrs = rbd_attrs,
3330};
3331
3332static const struct attribute_group *rbd_attr_groups[] = {
3333 &rbd_attr_group,
3334 NULL
3335};
3336
3337static void rbd_sysfs_dev_release(struct device *dev)
3338{
3339}
3340
3341static struct device_type rbd_device_type = {
3342 .name = "rbd",
3343 .groups = rbd_attr_groups,
3344 .release = rbd_sysfs_dev_release,
3345};
3346
3347
3348/*
3349 sysfs - snapshots
3350*/
3351
3352static ssize_t rbd_snap_size_show(struct device *dev,
3353 struct device_attribute *attr,
3354 char *buf)
3355{
3356 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
3357
Josh Durgin3591538f2011-12-05 18:25:13 -08003358 return sprintf(buf, "%llu\n", (unsigned long long)snap->size);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003359}
3360
3361static ssize_t rbd_snap_id_show(struct device *dev,
3362 struct device_attribute *attr,
3363 char *buf)
3364{
3365 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
3366
Josh Durgin3591538f2011-12-05 18:25:13 -08003367 return sprintf(buf, "%llu\n", (unsigned long long)snap->id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003368}
3369
Alex Elder34b13182012-07-13 20:35:12 -05003370static ssize_t rbd_snap_features_show(struct device *dev,
3371 struct device_attribute *attr,
3372 char *buf)
3373{
3374 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
3375
3376 return sprintf(buf, "0x%016llx\n",
3377 (unsigned long long) snap->features);
3378}
3379
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003380static DEVICE_ATTR(snap_size, S_IRUGO, rbd_snap_size_show, NULL);
3381static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL);
Alex Elder34b13182012-07-13 20:35:12 -05003382static DEVICE_ATTR(snap_features, S_IRUGO, rbd_snap_features_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003383
3384static struct attribute *rbd_snap_attrs[] = {
3385 &dev_attr_snap_size.attr,
3386 &dev_attr_snap_id.attr,
Alex Elder34b13182012-07-13 20:35:12 -05003387 &dev_attr_snap_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003388 NULL,
3389};
3390
3391static struct attribute_group rbd_snap_attr_group = {
3392 .attrs = rbd_snap_attrs,
3393};
3394
3395static void rbd_snap_dev_release(struct device *dev)
3396{
3397 struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
3398 kfree(snap->name);
3399 kfree(snap);
3400}
3401
3402static const struct attribute_group *rbd_snap_attr_groups[] = {
3403 &rbd_snap_attr_group,
3404 NULL
3405};
3406
3407static struct device_type rbd_snap_device_type = {
3408 .groups = rbd_snap_attr_groups,
3409 .release = rbd_snap_dev_release,
3410};
3411
Alex Elder8b8fb992012-10-26 17:25:24 -05003412static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)
3413{
3414 kref_get(&spec->kref);
3415
3416 return spec;
3417}
3418
3419static void rbd_spec_free(struct kref *kref);
3420static void rbd_spec_put(struct rbd_spec *spec)
3421{
3422 if (spec)
3423 kref_put(&spec->kref, rbd_spec_free);
3424}
3425
3426static struct rbd_spec *rbd_spec_alloc(void)
3427{
3428 struct rbd_spec *spec;
3429
3430 spec = kzalloc(sizeof (*spec), GFP_KERNEL);
3431 if (!spec)
3432 return NULL;
3433 kref_init(&spec->kref);
3434
Alex Elder8b8fb992012-10-26 17:25:24 -05003435 return spec;
3436}
3437
3438static void rbd_spec_free(struct kref *kref)
3439{
3440 struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref);
3441
3442 kfree(spec->pool_name);
3443 kfree(spec->image_id);
3444 kfree(spec->image_name);
3445 kfree(spec->snap_name);
3446 kfree(spec);
3447}
3448
Alex Eldercc344fa2013-02-19 12:25:56 -06003449static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,
Alex Elderc53d5892012-10-25 23:34:42 -05003450 struct rbd_spec *spec)
3451{
3452 struct rbd_device *rbd_dev;
3453
3454 rbd_dev = kzalloc(sizeof (*rbd_dev), GFP_KERNEL);
3455 if (!rbd_dev)
3456 return NULL;
3457
3458 spin_lock_init(&rbd_dev->lock);
Alex Elder6d292902013-01-14 12:43:31 -06003459 rbd_dev->flags = 0;
Alex Elderc53d5892012-10-25 23:34:42 -05003460 INIT_LIST_HEAD(&rbd_dev->node);
3461 INIT_LIST_HEAD(&rbd_dev->snaps);
3462 init_rwsem(&rbd_dev->header_rwsem);
3463
3464 rbd_dev->spec = spec;
3465 rbd_dev->rbd_client = rbdc;
3466
Alex Elder0903e872012-11-14 12:25:19 -06003467 /* Initialize the layout used for all rbd requests */
3468
3469 rbd_dev->layout.fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
3470 rbd_dev->layout.fl_stripe_count = cpu_to_le32(1);
3471 rbd_dev->layout.fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
3472 rbd_dev->layout.fl_pg_pool = cpu_to_le32((u32) spec->pool_id);
3473
Alex Elderc53d5892012-10-25 23:34:42 -05003474 return rbd_dev;
3475}
3476
3477static void rbd_dev_destroy(struct rbd_device *rbd_dev)
3478{
Alex Elder86b00e02012-10-25 23:34:42 -05003479 rbd_spec_put(rbd_dev->parent_spec);
Alex Elderc53d5892012-10-25 23:34:42 -05003480 kfree(rbd_dev->header_name);
3481 rbd_put_client(rbd_dev->rbd_client);
3482 rbd_spec_put(rbd_dev->spec);
3483 kfree(rbd_dev);
3484}
3485
Alex Elder304f6802012-08-31 17:29:52 -05003486static bool rbd_snap_registered(struct rbd_snap *snap)
3487{
3488 bool ret = snap->dev.type == &rbd_snap_device_type;
3489 bool reg = device_is_registered(&snap->dev);
3490
3491 rbd_assert(!ret ^ reg);
3492
3493 return ret;
3494}
3495
Alex Elder41f38c22012-10-25 23:34:40 -05003496static void rbd_remove_snap_dev(struct rbd_snap *snap)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003497{
3498 list_del(&snap->node);
Alex Elder304f6802012-08-31 17:29:52 -05003499 if (device_is_registered(&snap->dev))
3500 device_unregister(&snap->dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003501}
3502
Alex Elder14e70852012-07-19 09:09:27 -05003503static int rbd_register_snap_dev(struct rbd_snap *snap,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003504 struct device *parent)
3505{
3506 struct device *dev = &snap->dev;
3507 int ret;
3508
3509 dev->type = &rbd_snap_device_type;
3510 dev->parent = parent;
3511 dev->release = rbd_snap_dev_release;
Alex Elderd4b125e2012-07-03 16:01:19 -05003512 dev_set_name(dev, "%s%s", RBD_SNAP_DEV_NAME_PREFIX, snap->name);
Alex Elder304f6802012-08-31 17:29:52 -05003513 dout("%s: registering device for snapshot %s\n", __func__, snap->name);
3514
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003515 ret = device_register(dev);
3516
3517 return ret;
3518}
3519
Alex Elder4e891e02012-07-10 20:30:10 -05003520static struct rbd_snap *__rbd_add_snap_dev(struct rbd_device *rbd_dev,
Alex Elderc8d18422012-07-10 20:30:11 -05003521 const char *snap_name,
Alex Elder34b13182012-07-13 20:35:12 -05003522 u64 snap_id, u64 snap_size,
3523 u64 snap_features)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003524{
Alex Elder4e891e02012-07-10 20:30:10 -05003525 struct rbd_snap *snap;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003526 int ret;
Alex Elder4e891e02012-07-10 20:30:10 -05003527
3528 snap = kzalloc(sizeof (*snap), GFP_KERNEL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003529 if (!snap)
Alex Elder4e891e02012-07-10 20:30:10 -05003530 return ERR_PTR(-ENOMEM);
3531
3532 ret = -ENOMEM;
Alex Elderc8d18422012-07-10 20:30:11 -05003533 snap->name = kstrdup(snap_name, GFP_KERNEL);
Alex Elder4e891e02012-07-10 20:30:10 -05003534 if (!snap->name)
3535 goto err;
3536
Alex Elderc8d18422012-07-10 20:30:11 -05003537 snap->id = snap_id;
3538 snap->size = snap_size;
Alex Elder34b13182012-07-13 20:35:12 -05003539 snap->features = snap_features;
Alex Elder4e891e02012-07-10 20:30:10 -05003540
3541 return snap;
3542
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003543err:
3544 kfree(snap->name);
3545 kfree(snap);
Alex Elder4e891e02012-07-10 20:30:10 -05003546
3547 return ERR_PTR(ret);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003548}
3549
Alex Eldercd892122012-07-03 16:01:19 -05003550static char *rbd_dev_v1_snap_info(struct rbd_device *rbd_dev, u32 which,
3551 u64 *snap_size, u64 *snap_features)
3552{
3553 char *snap_name;
3554
3555 rbd_assert(which < rbd_dev->header.snapc->num_snaps);
3556
3557 *snap_size = rbd_dev->header.snap_sizes[which];
3558 *snap_features = 0; /* No features for v1 */
3559
3560 /* Skip over names until we find the one we are looking for */
3561
3562 snap_name = rbd_dev->header.snap_names;
3563 while (which--)
3564 snap_name += strlen(snap_name) + 1;
3565
3566 return snap_name;
3567}
3568
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003569/*
Alex Elder9d475de2012-07-03 16:01:19 -05003570 * Get the size and object order for an image snapshot, or if
3571 * snap_id is CEPH_NOSNAP, gets this information for the base
3572 * image.
3573 */
3574static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
3575 u8 *order, u64 *snap_size)
3576{
3577 __le64 snapid = cpu_to_le64(snap_id);
3578 int ret;
3579 struct {
3580 u8 order;
3581 __le64 size;
3582 } __attribute__ ((packed)) size_buf = { 0 };
3583
Alex Elder36be9a72013-01-19 00:30:28 -06003584 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elder9d475de2012-07-03 16:01:19 -05003585 "rbd", "get_size",
Alex Elder41579762013-04-21 12:14:45 -05003586 &snapid, sizeof (snapid),
3587 &size_buf, sizeof (size_buf), NULL);
Alex Elder36be9a72013-01-19 00:30:28 -06003588 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder9d475de2012-07-03 16:01:19 -05003589 if (ret < 0)
3590 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05003591 if (ret < sizeof (size_buf))
3592 return -ERANGE;
Alex Elder9d475de2012-07-03 16:01:19 -05003593
3594 *order = size_buf.order;
3595 *snap_size = le64_to_cpu(size_buf.size);
3596
3597 dout(" snap_id 0x%016llx order = %u, snap_size = %llu\n",
Alex Elder57385b52013-04-21 12:14:45 -05003598 (unsigned long long)snap_id, (unsigned int)*order,
3599 (unsigned long long)*snap_size);
Alex Elder9d475de2012-07-03 16:01:19 -05003600
3601 return 0;
3602}
3603
3604static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
3605{
3606 return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
3607 &rbd_dev->header.obj_order,
3608 &rbd_dev->header.image_size);
3609}
3610
Alex Elder1e130192012-07-03 16:01:19 -05003611static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
3612{
3613 void *reply_buf;
3614 int ret;
3615 void *p;
3616
3617 reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL);
3618 if (!reply_buf)
3619 return -ENOMEM;
3620
Alex Elder36be9a72013-01-19 00:30:28 -06003621 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elder41579762013-04-21 12:14:45 -05003622 "rbd", "get_object_prefix", NULL, 0,
Alex Elder07b23912012-11-09 08:43:16 -06003623 reply_buf, RBD_OBJ_PREFIX_LEN_MAX, NULL);
Alex Elder36be9a72013-01-19 00:30:28 -06003624 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder1e130192012-07-03 16:01:19 -05003625 if (ret < 0)
3626 goto out;
3627
3628 p = reply_buf;
3629 rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p,
Alex Elder57385b52013-04-21 12:14:45 -05003630 p + ret, NULL, GFP_NOIO);
3631 ret = 0;
Alex Elder1e130192012-07-03 16:01:19 -05003632
3633 if (IS_ERR(rbd_dev->header.object_prefix)) {
3634 ret = PTR_ERR(rbd_dev->header.object_prefix);
3635 rbd_dev->header.object_prefix = NULL;
3636 } else {
3637 dout(" object_prefix = %s\n", rbd_dev->header.object_prefix);
3638 }
Alex Elder1e130192012-07-03 16:01:19 -05003639out:
3640 kfree(reply_buf);
3641
3642 return ret;
3643}
3644
Alex Elderb1b54022012-07-03 16:01:19 -05003645static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
3646 u64 *snap_features)
3647{
3648 __le64 snapid = cpu_to_le64(snap_id);
3649 struct {
3650 __le64 features;
3651 __le64 incompat;
Alex Elder41579762013-04-21 12:14:45 -05003652 } __attribute__ ((packed)) features_buf = { 0 };
Alex Elderd8891402012-10-09 13:50:17 -07003653 u64 incompat;
Alex Elderb1b54022012-07-03 16:01:19 -05003654 int ret;
3655
Alex Elder36be9a72013-01-19 00:30:28 -06003656 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elderb1b54022012-07-03 16:01:19 -05003657 "rbd", "get_features",
Alex Elder41579762013-04-21 12:14:45 -05003658 &snapid, sizeof (snapid),
3659 &features_buf, sizeof (features_buf), NULL);
Alex Elder36be9a72013-01-19 00:30:28 -06003660 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderb1b54022012-07-03 16:01:19 -05003661 if (ret < 0)
3662 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05003663 if (ret < sizeof (features_buf))
3664 return -ERANGE;
Alex Elderd8891402012-10-09 13:50:17 -07003665
3666 incompat = le64_to_cpu(features_buf.incompat);
Alex Elder5cbf6f122013-04-11 09:29:48 -05003667 if (incompat & ~RBD_FEATURES_SUPPORTED)
Alex Elderb8f5c6e2012-11-01 08:39:26 -05003668 return -ENXIO;
Alex Elderd8891402012-10-09 13:50:17 -07003669
Alex Elderb1b54022012-07-03 16:01:19 -05003670 *snap_features = le64_to_cpu(features_buf.features);
3671
3672 dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
Alex Elder57385b52013-04-21 12:14:45 -05003673 (unsigned long long)snap_id,
3674 (unsigned long long)*snap_features,
3675 (unsigned long long)le64_to_cpu(features_buf.incompat));
Alex Elderb1b54022012-07-03 16:01:19 -05003676
3677 return 0;
3678}
3679
3680static int rbd_dev_v2_features(struct rbd_device *rbd_dev)
3681{
3682 return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP,
3683 &rbd_dev->header.features);
3684}
3685
Alex Elder86b00e02012-10-25 23:34:42 -05003686static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev)
3687{
3688 struct rbd_spec *parent_spec;
3689 size_t size;
3690 void *reply_buf = NULL;
3691 __le64 snapid;
3692 void *p;
3693 void *end;
3694 char *image_id;
3695 u64 overlap;
Alex Elder86b00e02012-10-25 23:34:42 -05003696 int ret;
3697
3698 parent_spec = rbd_spec_alloc();
3699 if (!parent_spec)
3700 return -ENOMEM;
3701
3702 size = sizeof (__le64) + /* pool_id */
3703 sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */
3704 sizeof (__le64) + /* snap_id */
3705 sizeof (__le64); /* overlap */
3706 reply_buf = kmalloc(size, GFP_KERNEL);
3707 if (!reply_buf) {
3708 ret = -ENOMEM;
3709 goto out_err;
3710 }
3711
3712 snapid = cpu_to_le64(CEPH_NOSNAP);
Alex Elder36be9a72013-01-19 00:30:28 -06003713 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elder86b00e02012-10-25 23:34:42 -05003714 "rbd", "get_parent",
Alex Elder41579762013-04-21 12:14:45 -05003715 &snapid, sizeof (snapid),
3716 reply_buf, size, NULL);
Alex Elder36be9a72013-01-19 00:30:28 -06003717 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder86b00e02012-10-25 23:34:42 -05003718 if (ret < 0)
3719 goto out_err;
3720
Alex Elder86b00e02012-10-25 23:34:42 -05003721 p = reply_buf;
Alex Elder57385b52013-04-21 12:14:45 -05003722 end = reply_buf + ret;
3723 ret = -ERANGE;
Alex Elder86b00e02012-10-25 23:34:42 -05003724 ceph_decode_64_safe(&p, end, parent_spec->pool_id, out_err);
3725 if (parent_spec->pool_id == CEPH_NOPOOL)
3726 goto out; /* No parent? No problem. */
3727
Alex Elder0903e872012-11-14 12:25:19 -06003728 /* The ceph file layout needs to fit pool id in 32 bits */
3729
3730 ret = -EIO;
Alex Elder57385b52013-04-21 12:14:45 -05003731 if (WARN_ON(parent_spec->pool_id > (u64)U32_MAX))
3732 goto out_err;
Alex Elder0903e872012-11-14 12:25:19 -06003733
Alex Elder979ed482012-11-01 08:39:26 -05003734 image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elder86b00e02012-10-25 23:34:42 -05003735 if (IS_ERR(image_id)) {
3736 ret = PTR_ERR(image_id);
3737 goto out_err;
3738 }
3739 parent_spec->image_id = image_id;
3740 ceph_decode_64_safe(&p, end, parent_spec->snap_id, out_err);
3741 ceph_decode_64_safe(&p, end, overlap, out_err);
3742
3743 rbd_dev->parent_overlap = overlap;
3744 rbd_dev->parent_spec = parent_spec;
3745 parent_spec = NULL; /* rbd_dev now owns this */
3746out:
3747 ret = 0;
3748out_err:
3749 kfree(reply_buf);
3750 rbd_spec_put(parent_spec);
3751
3752 return ret;
3753}
3754
Alex Eldercc070d52013-04-21 12:14:45 -05003755static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev)
3756{
3757 struct {
3758 __le64 stripe_unit;
3759 __le64 stripe_count;
3760 } __attribute__ ((packed)) striping_info_buf = { 0 };
3761 size_t size = sizeof (striping_info_buf);
3762 void *p;
3763 u64 obj_size;
3764 u64 stripe_unit;
3765 u64 stripe_count;
3766 int ret;
3767
3768 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
3769 "rbd", "get_stripe_unit_count", NULL, 0,
3770 (char *)&striping_info_buf, size, NULL);
3771 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
3772 if (ret < 0)
3773 return ret;
3774 if (ret < size)
3775 return -ERANGE;
3776
3777 /*
3778 * We don't actually support the "fancy striping" feature
3779 * (STRIPINGV2) yet, but if the striping sizes are the
3780 * defaults the behavior is the same as before. So find
3781 * out, and only fail if the image has non-default values.
3782 */
3783 ret = -EINVAL;
3784 obj_size = (u64)1 << rbd_dev->header.obj_order;
3785 p = &striping_info_buf;
3786 stripe_unit = ceph_decode_64(&p);
3787 if (stripe_unit != obj_size) {
3788 rbd_warn(rbd_dev, "unsupported stripe unit "
3789 "(got %llu want %llu)",
3790 stripe_unit, obj_size);
3791 return -EINVAL;
3792 }
3793 stripe_count = ceph_decode_64(&p);
3794 if (stripe_count != 1) {
3795 rbd_warn(rbd_dev, "unsupported stripe count "
3796 "(got %llu want 1)", stripe_count);
3797 return -EINVAL;
3798 }
3799 rbd_dev->stripe_unit = stripe_unit;
3800 rbd_dev->stripe_count = stripe_count;
3801
3802 return 0;
3803}
3804
Alex Elder9e15b772012-10-30 19:40:33 -05003805static char *rbd_dev_image_name(struct rbd_device *rbd_dev)
3806{
3807 size_t image_id_size;
3808 char *image_id;
3809 void *p;
3810 void *end;
3811 size_t size;
3812 void *reply_buf = NULL;
3813 size_t len = 0;
3814 char *image_name = NULL;
3815 int ret;
3816
3817 rbd_assert(!rbd_dev->spec->image_name);
3818
Alex Elder69e7a022012-11-01 08:39:26 -05003819 len = strlen(rbd_dev->spec->image_id);
3820 image_id_size = sizeof (__le32) + len;
Alex Elder9e15b772012-10-30 19:40:33 -05003821 image_id = kmalloc(image_id_size, GFP_KERNEL);
3822 if (!image_id)
3823 return NULL;
3824
3825 p = image_id;
Alex Elder41579762013-04-21 12:14:45 -05003826 end = image_id + image_id_size;
Alex Elder57385b52013-04-21 12:14:45 -05003827 ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len);
Alex Elder9e15b772012-10-30 19:40:33 -05003828
3829 size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX;
3830 reply_buf = kmalloc(size, GFP_KERNEL);
3831 if (!reply_buf)
3832 goto out;
3833
Alex Elder36be9a72013-01-19 00:30:28 -06003834 ret = rbd_obj_method_sync(rbd_dev, RBD_DIRECTORY,
Alex Elder9e15b772012-10-30 19:40:33 -05003835 "rbd", "dir_get_name",
3836 image_id, image_id_size,
Alex Elder41579762013-04-21 12:14:45 -05003837 reply_buf, size, NULL);
Alex Elder9e15b772012-10-30 19:40:33 -05003838 if (ret < 0)
3839 goto out;
3840 p = reply_buf;
Alex Elder41579762013-04-21 12:14:45 -05003841 end = reply_buf + size;
Alex Elder9e15b772012-10-30 19:40:33 -05003842 image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL);
3843 if (IS_ERR(image_name))
3844 image_name = NULL;
3845 else
3846 dout("%s: name is %s len is %zd\n", __func__, image_name, len);
3847out:
3848 kfree(reply_buf);
3849 kfree(image_id);
3850
3851 return image_name;
3852}
3853
3854/*
3855 * When a parent image gets probed, we only have the pool, image,
3856 * and snapshot ids but not the names of any of them. This call
3857 * is made later to fill in those names. It has to be done after
3858 * rbd_dev_snaps_update() has completed because some of the
3859 * information (in particular, snapshot name) is not available
3860 * until then.
3861 */
3862static int rbd_dev_probe_update_spec(struct rbd_device *rbd_dev)
3863{
3864 struct ceph_osd_client *osdc;
3865 const char *name;
3866 void *reply_buf = NULL;
3867 int ret;
3868
3869 if (rbd_dev->spec->pool_name)
3870 return 0; /* Already have the names */
3871
3872 /* Look up the pool name */
3873
3874 osdc = &rbd_dev->rbd_client->client->osdc;
3875 name = ceph_pg_pool_name_by_id(osdc->osdmap, rbd_dev->spec->pool_id);
Alex Elder935dc892012-11-01 10:17:15 -05003876 if (!name) {
3877 rbd_warn(rbd_dev, "there is no pool with id %llu",
3878 rbd_dev->spec->pool_id); /* Really a BUG() */
3879 return -EIO;
3880 }
Alex Elder9e15b772012-10-30 19:40:33 -05003881
3882 rbd_dev->spec->pool_name = kstrdup(name, GFP_KERNEL);
3883 if (!rbd_dev->spec->pool_name)
3884 return -ENOMEM;
3885
3886 /* Fetch the image name; tolerate failure here */
3887
3888 name = rbd_dev_image_name(rbd_dev);
Alex Elder69e7a022012-11-01 08:39:26 -05003889 if (name)
Alex Elder41579762013-04-21 12:14:45 -05003890 rbd_dev->spec->image_name = (char *)name;
Alex Elder69e7a022012-11-01 08:39:26 -05003891 else
Alex Elder06ecc6c2012-11-01 10:17:15 -05003892 rbd_warn(rbd_dev, "unable to get image name");
Alex Elder9e15b772012-10-30 19:40:33 -05003893
3894 /* Look up the snapshot name. */
3895
3896 name = rbd_snap_name(rbd_dev, rbd_dev->spec->snap_id);
3897 if (!name) {
Alex Elder935dc892012-11-01 10:17:15 -05003898 rbd_warn(rbd_dev, "no snapshot with id %llu",
3899 rbd_dev->spec->snap_id); /* Really a BUG() */
Alex Elder9e15b772012-10-30 19:40:33 -05003900 ret = -EIO;
3901 goto out_err;
3902 }
3903 rbd_dev->spec->snap_name = kstrdup(name, GFP_KERNEL);
3904 if(!rbd_dev->spec->snap_name)
3905 goto out_err;
3906
3907 return 0;
3908out_err:
3909 kfree(reply_buf);
3910 kfree(rbd_dev->spec->pool_name);
3911 rbd_dev->spec->pool_name = NULL;
3912
3913 return ret;
3914}
3915
Alex Elder6e14b1a2012-07-03 16:01:19 -05003916static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev, u64 *ver)
Alex Elder35d489f2012-07-03 16:01:19 -05003917{
3918 size_t size;
3919 int ret;
3920 void *reply_buf;
3921 void *p;
3922 void *end;
3923 u64 seq;
3924 u32 snap_count;
3925 struct ceph_snap_context *snapc;
3926 u32 i;
3927
3928 /*
3929 * We'll need room for the seq value (maximum snapshot id),
3930 * snapshot count, and array of that many snapshot ids.
3931 * For now we have a fixed upper limit on the number we're
3932 * prepared to receive.
3933 */
3934 size = sizeof (__le64) + sizeof (__le32) +
3935 RBD_MAX_SNAP_COUNT * sizeof (__le64);
3936 reply_buf = kzalloc(size, GFP_KERNEL);
3937 if (!reply_buf)
3938 return -ENOMEM;
3939
Alex Elder36be9a72013-01-19 00:30:28 -06003940 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elder41579762013-04-21 12:14:45 -05003941 "rbd", "get_snapcontext", NULL, 0,
Alex Elder07b23912012-11-09 08:43:16 -06003942 reply_buf, size, ver);
Alex Elder36be9a72013-01-19 00:30:28 -06003943 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder35d489f2012-07-03 16:01:19 -05003944 if (ret < 0)
3945 goto out;
3946
Alex Elder35d489f2012-07-03 16:01:19 -05003947 p = reply_buf;
Alex Elder57385b52013-04-21 12:14:45 -05003948 end = reply_buf + ret;
3949 ret = -ERANGE;
Alex Elder35d489f2012-07-03 16:01:19 -05003950 ceph_decode_64_safe(&p, end, seq, out);
3951 ceph_decode_32_safe(&p, end, snap_count, out);
3952
3953 /*
3954 * Make sure the reported number of snapshot ids wouldn't go
3955 * beyond the end of our buffer. But before checking that,
3956 * make sure the computed size of the snapshot context we
3957 * allocate is representable in a size_t.
3958 */
3959 if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
3960 / sizeof (u64)) {
3961 ret = -EINVAL;
3962 goto out;
3963 }
3964 if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
3965 goto out;
3966
3967 size = sizeof (struct ceph_snap_context) +
3968 snap_count * sizeof (snapc->snaps[0]);
3969 snapc = kmalloc(size, GFP_KERNEL);
3970 if (!snapc) {
3971 ret = -ENOMEM;
3972 goto out;
3973 }
Alex Elder57385b52013-04-21 12:14:45 -05003974 ret = 0;
Alex Elder35d489f2012-07-03 16:01:19 -05003975
3976 atomic_set(&snapc->nref, 1);
3977 snapc->seq = seq;
3978 snapc->num_snaps = snap_count;
3979 for (i = 0; i < snap_count; i++)
3980 snapc->snaps[i] = ceph_decode_64(&p);
3981
3982 rbd_dev->header.snapc = snapc;
3983
3984 dout(" snap context seq = %llu, snap_count = %u\n",
Alex Elder57385b52013-04-21 12:14:45 -05003985 (unsigned long long)seq, (unsigned int)snap_count);
Alex Elder35d489f2012-07-03 16:01:19 -05003986out:
3987 kfree(reply_buf);
3988
Alex Elder57385b52013-04-21 12:14:45 -05003989 return ret;
Alex Elder35d489f2012-07-03 16:01:19 -05003990}
3991
Alex Elderb8b1e2d2012-07-03 16:01:19 -05003992static char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, u32 which)
3993{
3994 size_t size;
3995 void *reply_buf;
3996 __le64 snap_id;
3997 int ret;
3998 void *p;
3999 void *end;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004000 char *snap_name;
4001
4002 size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN;
4003 reply_buf = kmalloc(size, GFP_KERNEL);
4004 if (!reply_buf)
4005 return ERR_PTR(-ENOMEM);
4006
4007 snap_id = cpu_to_le64(rbd_dev->header.snapc->snaps[which]);
Alex Elder36be9a72013-01-19 00:30:28 -06004008 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004009 "rbd", "get_snapshot_name",
Alex Elder41579762013-04-21 12:14:45 -05004010 &snap_id, sizeof (snap_id),
Alex Elder07b23912012-11-09 08:43:16 -06004011 reply_buf, size, NULL);
Alex Elder36be9a72013-01-19 00:30:28 -06004012 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004013 if (ret < 0)
4014 goto out;
4015
4016 p = reply_buf;
Alex Elder41579762013-04-21 12:14:45 -05004017 end = reply_buf + size;
Alex Eldere5c35532012-10-25 23:34:41 -05004018 snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004019 if (IS_ERR(snap_name)) {
4020 ret = PTR_ERR(snap_name);
4021 goto out;
4022 } else {
4023 dout(" snap_id 0x%016llx snap_name = %s\n",
Alex Elder57385b52013-04-21 12:14:45 -05004024 (unsigned long long)le64_to_cpu(snap_id), snap_name);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004025 }
4026 kfree(reply_buf);
4027
4028 return snap_name;
4029out:
4030 kfree(reply_buf);
4031
4032 return ERR_PTR(ret);
4033}
4034
4035static char *rbd_dev_v2_snap_info(struct rbd_device *rbd_dev, u32 which,
4036 u64 *snap_size, u64 *snap_features)
4037{
Alex Eldere0b49862013-01-09 14:44:18 -06004038 u64 snap_id;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004039 u8 order;
4040 int ret;
4041
4042 snap_id = rbd_dev->header.snapc->snaps[which];
4043 ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, &order, snap_size);
4044 if (ret)
4045 return ERR_PTR(ret);
4046 ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, snap_features);
4047 if (ret)
4048 return ERR_PTR(ret);
4049
4050 return rbd_dev_v2_snap_name(rbd_dev, which);
4051}
4052
4053static char *rbd_dev_snap_info(struct rbd_device *rbd_dev, u32 which,
4054 u64 *snap_size, u64 *snap_features)
4055{
4056 if (rbd_dev->image_format == 1)
4057 return rbd_dev_v1_snap_info(rbd_dev, which,
4058 snap_size, snap_features);
4059 if (rbd_dev->image_format == 2)
4060 return rbd_dev_v2_snap_info(rbd_dev, which,
4061 snap_size, snap_features);
4062 return ERR_PTR(-EINVAL);
4063}
4064
Alex Elder117973f2012-08-31 17:29:55 -05004065static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev, u64 *hver)
4066{
4067 int ret;
4068 __u8 obj_order;
4069
4070 down_write(&rbd_dev->header_rwsem);
4071
4072 /* Grab old order first, to see if it changes */
4073
4074 obj_order = rbd_dev->header.obj_order,
4075 ret = rbd_dev_v2_image_size(rbd_dev);
4076 if (ret)
4077 goto out;
4078 if (rbd_dev->header.obj_order != obj_order) {
4079 ret = -EIO;
4080 goto out;
4081 }
4082 rbd_update_mapping_size(rbd_dev);
4083
4084 ret = rbd_dev_v2_snap_context(rbd_dev, hver);
4085 dout("rbd_dev_v2_snap_context returned %d\n", ret);
4086 if (ret)
4087 goto out;
4088 ret = rbd_dev_snaps_update(rbd_dev);
4089 dout("rbd_dev_snaps_update returned %d\n", ret);
4090 if (ret)
4091 goto out;
4092 ret = rbd_dev_snaps_register(rbd_dev);
4093 dout("rbd_dev_snaps_register returned %d\n", ret);
4094out:
4095 up_write(&rbd_dev->header_rwsem);
4096
4097 return ret;
4098}
4099
Alex Elder9d475de2012-07-03 16:01:19 -05004100/*
Alex Elder35938152012-08-02 11:29:46 -05004101 * Scan the rbd device's current snapshot list and compare it to the
4102 * newly-received snapshot context. Remove any existing snapshots
4103 * not present in the new snapshot context. Add a new snapshot for
4104 * any snaphots in the snapshot context not in the current list.
4105 * And verify there are no changes to snapshots we already know
4106 * about.
4107 *
4108 * Assumes the snapshots in the snapshot context are sorted by
4109 * snapshot id, highest id first. (Snapshots in the rbd_dev's list
4110 * are also maintained in that order.)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004111 */
Alex Elder304f6802012-08-31 17:29:52 -05004112static int rbd_dev_snaps_update(struct rbd_device *rbd_dev)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004113{
Alex Elder35938152012-08-02 11:29:46 -05004114 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
4115 const u32 snap_count = snapc->num_snaps;
Alex Elder35938152012-08-02 11:29:46 -05004116 struct list_head *head = &rbd_dev->snaps;
4117 struct list_head *links = head->next;
4118 u32 index = 0;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004119
Alex Elder9fcbb802012-08-23 23:48:49 -05004120 dout("%s: snap count is %u\n", __func__, (unsigned int) snap_count);
Alex Elder35938152012-08-02 11:29:46 -05004121 while (index < snap_count || links != head) {
4122 u64 snap_id;
4123 struct rbd_snap *snap;
Alex Eldercd892122012-07-03 16:01:19 -05004124 char *snap_name;
4125 u64 snap_size = 0;
4126 u64 snap_features = 0;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004127
Alex Elder35938152012-08-02 11:29:46 -05004128 snap_id = index < snap_count ? snapc->snaps[index]
4129 : CEPH_NOSNAP;
4130 snap = links != head ? list_entry(links, struct rbd_snap, node)
4131 : NULL;
Alex Elderaafb2302012-09-06 16:00:54 -05004132 rbd_assert(!snap || snap->id != CEPH_NOSNAP);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004133
Alex Elder35938152012-08-02 11:29:46 -05004134 if (snap_id == CEPH_NOSNAP || (snap && snap->id > snap_id)) {
4135 struct list_head *next = links->next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004136
Alex Elder6d292902013-01-14 12:43:31 -06004137 /*
4138 * A previously-existing snapshot is not in
4139 * the new snap context.
4140 *
4141 * If the now missing snapshot is the one the
4142 * image is mapped to, clear its exists flag
4143 * so we can avoid sending any more requests
4144 * to it.
4145 */
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004146 if (rbd_dev->spec->snap_id == snap->id)
Alex Elder6d292902013-01-14 12:43:31 -06004147 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
Alex Elder41f38c22012-10-25 23:34:40 -05004148 rbd_remove_snap_dev(snap);
Alex Elder9fcbb802012-08-23 23:48:49 -05004149 dout("%ssnap id %llu has been removed\n",
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004150 rbd_dev->spec->snap_id == snap->id ?
4151 "mapped " : "",
Alex Elder9fcbb802012-08-23 23:48:49 -05004152 (unsigned long long) snap->id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004153
Alex Elder35938152012-08-02 11:29:46 -05004154 /* Done with this list entry; advance */
4155
4156 links = next;
4157 continue;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004158 }
Alex Elder35938152012-08-02 11:29:46 -05004159
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004160 snap_name = rbd_dev_snap_info(rbd_dev, index,
4161 &snap_size, &snap_features);
Alex Eldercd892122012-07-03 16:01:19 -05004162 if (IS_ERR(snap_name))
4163 return PTR_ERR(snap_name);
4164
Alex Elder9fcbb802012-08-23 23:48:49 -05004165 dout("entry %u: snap_id = %llu\n", (unsigned int) snap_count,
4166 (unsigned long long) snap_id);
Alex Elder35938152012-08-02 11:29:46 -05004167 if (!snap || (snap_id != CEPH_NOSNAP && snap->id < snap_id)) {
4168 struct rbd_snap *new_snap;
4169
4170 /* We haven't seen this snapshot before */
4171
Alex Elderc8d18422012-07-10 20:30:11 -05004172 new_snap = __rbd_add_snap_dev(rbd_dev, snap_name,
Alex Eldercd892122012-07-03 16:01:19 -05004173 snap_id, snap_size, snap_features);
Alex Elder9fcbb802012-08-23 23:48:49 -05004174 if (IS_ERR(new_snap)) {
4175 int err = PTR_ERR(new_snap);
4176
4177 dout(" failed to add dev, error %d\n", err);
4178
4179 return err;
4180 }
Alex Elder35938152012-08-02 11:29:46 -05004181
4182 /* New goes before existing, or at end of list */
4183
Alex Elder9fcbb802012-08-23 23:48:49 -05004184 dout(" added dev%s\n", snap ? "" : " at end\n");
Alex Elder35938152012-08-02 11:29:46 -05004185 if (snap)
4186 list_add_tail(&new_snap->node, &snap->node);
4187 else
Alex Elder523f3252012-08-30 00:16:37 -05004188 list_add_tail(&new_snap->node, head);
Alex Elder35938152012-08-02 11:29:46 -05004189 } else {
4190 /* Already have this one */
4191
Alex Elder9fcbb802012-08-23 23:48:49 -05004192 dout(" already present\n");
4193
Alex Eldercd892122012-07-03 16:01:19 -05004194 rbd_assert(snap->size == snap_size);
Alex Elderaafb2302012-09-06 16:00:54 -05004195 rbd_assert(!strcmp(snap->name, snap_name));
Alex Eldercd892122012-07-03 16:01:19 -05004196 rbd_assert(snap->features == snap_features);
Alex Elder35938152012-08-02 11:29:46 -05004197
4198 /* Done with this list entry; advance */
4199
4200 links = links->next;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004201 }
Alex Elder35938152012-08-02 11:29:46 -05004202
4203 /* Advance to the next entry in the snapshot context */
4204
4205 index++;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004206 }
Alex Elder9fcbb802012-08-23 23:48:49 -05004207 dout("%s: done\n", __func__);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004208
4209 return 0;
4210}
4211
Alex Elder304f6802012-08-31 17:29:52 -05004212/*
4213 * Scan the list of snapshots and register the devices for any that
4214 * have not already been registered.
4215 */
4216static int rbd_dev_snaps_register(struct rbd_device *rbd_dev)
4217{
4218 struct rbd_snap *snap;
4219 int ret = 0;
4220
Alex Elder37206ee2013-02-20 17:32:08 -06004221 dout("%s:\n", __func__);
Alex Elder86ff77b2012-08-31 17:29:53 -05004222 if (WARN_ON(!device_is_registered(&rbd_dev->dev)))
4223 return -EIO;
Alex Elder304f6802012-08-31 17:29:52 -05004224
4225 list_for_each_entry(snap, &rbd_dev->snaps, node) {
4226 if (!rbd_snap_registered(snap)) {
4227 ret = rbd_register_snap_dev(snap, &rbd_dev->dev);
4228 if (ret < 0)
4229 break;
4230 }
4231 }
4232 dout("%s: returning %d\n", __func__, ret);
4233
4234 return ret;
4235}
4236
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004237static int rbd_bus_add_dev(struct rbd_device *rbd_dev)
4238{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004239 struct device *dev;
Alex Eldercd789ab2012-08-30 00:16:38 -05004240 int ret;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004241
4242 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004243
Alex Eldercd789ab2012-08-30 00:16:38 -05004244 dev = &rbd_dev->dev;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004245 dev->bus = &rbd_bus_type;
4246 dev->type = &rbd_device_type;
4247 dev->parent = &rbd_root_dev;
4248 dev->release = rbd_dev_release;
Alex Elderde71a292012-07-03 16:01:19 -05004249 dev_set_name(dev, "%d", rbd_dev->dev_id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004250 ret = device_register(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004251
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004252 mutex_unlock(&ctl_mutex);
Alex Eldercd789ab2012-08-30 00:16:38 -05004253
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004254 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004255}
4256
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004257static void rbd_bus_del_dev(struct rbd_device *rbd_dev)
4258{
4259 device_unregister(&rbd_dev->dev);
4260}
4261
Alex Eldere2839302012-08-29 17:11:06 -05004262static atomic64_t rbd_dev_id_max = ATOMIC64_INIT(0);
Alex Elder1ddbe942012-01-29 13:57:44 -06004263
4264/*
Alex Elder499afd52012-02-02 08:13:29 -06004265 * Get a unique rbd identifier for the given new rbd_dev, and add
4266 * the rbd_dev to the global list. The minimum rbd id is 1.
Alex Elder1ddbe942012-01-29 13:57:44 -06004267 */
Alex Eldere2839302012-08-29 17:11:06 -05004268static void rbd_dev_id_get(struct rbd_device *rbd_dev)
Alex Elderb7f23c32012-01-29 13:57:43 -06004269{
Alex Eldere2839302012-08-29 17:11:06 -05004270 rbd_dev->dev_id = atomic64_inc_return(&rbd_dev_id_max);
Alex Elder499afd52012-02-02 08:13:29 -06004271
4272 spin_lock(&rbd_dev_list_lock);
4273 list_add_tail(&rbd_dev->node, &rbd_dev_list);
4274 spin_unlock(&rbd_dev_list_lock);
Alex Eldere2839302012-08-29 17:11:06 -05004275 dout("rbd_dev %p given dev id %llu\n", rbd_dev,
4276 (unsigned long long) rbd_dev->dev_id);
Alex Elder1ddbe942012-01-29 13:57:44 -06004277}
Alex Elderb7f23c32012-01-29 13:57:43 -06004278
Alex Elder1ddbe942012-01-29 13:57:44 -06004279/*
Alex Elder499afd52012-02-02 08:13:29 -06004280 * Remove an rbd_dev from the global list, and record that its
4281 * identifier is no longer in use.
Alex Elder1ddbe942012-01-29 13:57:44 -06004282 */
Alex Eldere2839302012-08-29 17:11:06 -05004283static void rbd_dev_id_put(struct rbd_device *rbd_dev)
Alex Elder1ddbe942012-01-29 13:57:44 -06004284{
Alex Elderd184f6b2012-01-29 13:57:44 -06004285 struct list_head *tmp;
Alex Elderde71a292012-07-03 16:01:19 -05004286 int rbd_id = rbd_dev->dev_id;
Alex Elderd184f6b2012-01-29 13:57:44 -06004287 int max_id;
4288
Alex Elderaafb2302012-09-06 16:00:54 -05004289 rbd_assert(rbd_id > 0);
Alex Elder499afd52012-02-02 08:13:29 -06004290
Alex Eldere2839302012-08-29 17:11:06 -05004291 dout("rbd_dev %p released dev id %llu\n", rbd_dev,
4292 (unsigned long long) rbd_dev->dev_id);
Alex Elder499afd52012-02-02 08:13:29 -06004293 spin_lock(&rbd_dev_list_lock);
4294 list_del_init(&rbd_dev->node);
Alex Elderd184f6b2012-01-29 13:57:44 -06004295
4296 /*
4297 * If the id being "put" is not the current maximum, there
4298 * is nothing special we need to do.
4299 */
Alex Eldere2839302012-08-29 17:11:06 -05004300 if (rbd_id != atomic64_read(&rbd_dev_id_max)) {
Alex Elderd184f6b2012-01-29 13:57:44 -06004301 spin_unlock(&rbd_dev_list_lock);
4302 return;
4303 }
4304
4305 /*
4306 * We need to update the current maximum id. Search the
4307 * list to find out what it is. We're more likely to find
4308 * the maximum at the end, so search the list backward.
4309 */
4310 max_id = 0;
4311 list_for_each_prev(tmp, &rbd_dev_list) {
4312 struct rbd_device *rbd_dev;
4313
4314 rbd_dev = list_entry(tmp, struct rbd_device, node);
Alex Elderb213e0b2012-10-10 21:19:13 -07004315 if (rbd_dev->dev_id > max_id)
4316 max_id = rbd_dev->dev_id;
Alex Elderd184f6b2012-01-29 13:57:44 -06004317 }
Alex Elder499afd52012-02-02 08:13:29 -06004318 spin_unlock(&rbd_dev_list_lock);
Alex Elderb7f23c32012-01-29 13:57:43 -06004319
Alex Elder1ddbe942012-01-29 13:57:44 -06004320 /*
Alex Eldere2839302012-08-29 17:11:06 -05004321 * The max id could have been updated by rbd_dev_id_get(), in
Alex Elderd184f6b2012-01-29 13:57:44 -06004322 * which case it now accurately reflects the new maximum.
4323 * Be careful not to overwrite the maximum value in that
4324 * case.
Alex Elder1ddbe942012-01-29 13:57:44 -06004325 */
Alex Eldere2839302012-08-29 17:11:06 -05004326 atomic64_cmpxchg(&rbd_dev_id_max, rbd_id, max_id);
4327 dout(" max dev id has been reset\n");
Alex Elderb7f23c32012-01-29 13:57:43 -06004328}
4329
Alex Eldera725f65e2012-02-02 08:13:30 -06004330/*
Alex Eldere28fff262012-02-02 08:13:30 -06004331 * Skips over white space at *buf, and updates *buf to point to the
4332 * first found non-space character (if any). Returns the length of
Alex Elder593a9e72012-02-07 12:03:37 -06004333 * the token (string of non-white space characters) found. Note
4334 * that *buf must be terminated with '\0'.
Alex Eldere28fff262012-02-02 08:13:30 -06004335 */
4336static inline size_t next_token(const char **buf)
4337{
4338 /*
4339 * These are the characters that produce nonzero for
4340 * isspace() in the "C" and "POSIX" locales.
4341 */
4342 const char *spaces = " \f\n\r\t\v";
4343
4344 *buf += strspn(*buf, spaces); /* Find start of token */
4345
4346 return strcspn(*buf, spaces); /* Return token length */
4347}
4348
4349/*
4350 * Finds the next token in *buf, and if the provided token buffer is
4351 * big enough, copies the found token into it. The result, if
Alex Elder593a9e72012-02-07 12:03:37 -06004352 * copied, is guaranteed to be terminated with '\0'. Note that *buf
4353 * must be terminated with '\0' on entry.
Alex Eldere28fff262012-02-02 08:13:30 -06004354 *
4355 * Returns the length of the token found (not including the '\0').
4356 * Return value will be 0 if no token is found, and it will be >=
4357 * token_size if the token would not fit.
4358 *
Alex Elder593a9e72012-02-07 12:03:37 -06004359 * The *buf pointer will be updated to point beyond the end of the
Alex Eldere28fff262012-02-02 08:13:30 -06004360 * found token. Note that this occurs even if the token buffer is
4361 * too small to hold it.
4362 */
4363static inline size_t copy_token(const char **buf,
4364 char *token,
4365 size_t token_size)
4366{
4367 size_t len;
4368
4369 len = next_token(buf);
4370 if (len < token_size) {
4371 memcpy(token, *buf, len);
4372 *(token + len) = '\0';
4373 }
4374 *buf += len;
4375
4376 return len;
4377}
4378
4379/*
Alex Elderea3352f2012-07-09 21:04:23 -05004380 * Finds the next token in *buf, dynamically allocates a buffer big
4381 * enough to hold a copy of it, and copies the token into the new
4382 * buffer. The copy is guaranteed to be terminated with '\0'. Note
4383 * that a duplicate buffer is created even for a zero-length token.
4384 *
4385 * Returns a pointer to the newly-allocated duplicate, or a null
4386 * pointer if memory for the duplicate was not available. If
4387 * the lenp argument is a non-null pointer, the length of the token
4388 * (not including the '\0') is returned in *lenp.
4389 *
4390 * If successful, the *buf pointer will be updated to point beyond
4391 * the end of the found token.
4392 *
4393 * Note: uses GFP_KERNEL for allocation.
4394 */
4395static inline char *dup_token(const char **buf, size_t *lenp)
4396{
4397 char *dup;
4398 size_t len;
4399
4400 len = next_token(buf);
Alex Elder4caf35f2012-11-01 08:39:27 -05004401 dup = kmemdup(*buf, len + 1, GFP_KERNEL);
Alex Elderea3352f2012-07-09 21:04:23 -05004402 if (!dup)
4403 return NULL;
Alex Elderea3352f2012-07-09 21:04:23 -05004404 *(dup + len) = '\0';
4405 *buf += len;
4406
4407 if (lenp)
4408 *lenp = len;
4409
4410 return dup;
4411}
4412
4413/*
Alex Elder859c31d2012-10-25 23:34:42 -05004414 * Parse the options provided for an "rbd add" (i.e., rbd image
4415 * mapping) request. These arrive via a write to /sys/bus/rbd/add,
4416 * and the data written is passed here via a NUL-terminated buffer.
4417 * Returns 0 if successful or an error code otherwise.
Alex Elderd22f76e2012-07-12 10:46:35 -05004418 *
Alex Elder859c31d2012-10-25 23:34:42 -05004419 * The information extracted from these options is recorded in
4420 * the other parameters which return dynamically-allocated
4421 * structures:
4422 * ceph_opts
4423 * The address of a pointer that will refer to a ceph options
4424 * structure. Caller must release the returned pointer using
4425 * ceph_destroy_options() when it is no longer needed.
4426 * rbd_opts
4427 * Address of an rbd options pointer. Fully initialized by
4428 * this function; caller must release with kfree().
4429 * spec
4430 * Address of an rbd image specification pointer. Fully
4431 * initialized by this function based on parsed options.
4432 * Caller must release with rbd_spec_put().
4433 *
4434 * The options passed take this form:
4435 * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
4436 * where:
4437 * <mon_addrs>
4438 * A comma-separated list of one or more monitor addresses.
4439 * A monitor address is an ip address, optionally followed
4440 * by a port number (separated by a colon).
4441 * I.e.: ip1[:port1][,ip2[:port2]...]
4442 * <options>
4443 * A comma-separated list of ceph and/or rbd options.
4444 * <pool_name>
4445 * The name of the rados pool containing the rbd image.
4446 * <image_name>
4447 * The name of the image in that pool to map.
4448 * <snap_id>
4449 * An optional snapshot id. If provided, the mapping will
4450 * present data from the image at the time that snapshot was
4451 * created. The image head is used if no snapshot id is
4452 * provided. Snapshot mappings are always read-only.
Alex Eldera725f65e2012-02-02 08:13:30 -06004453 */
Alex Elder859c31d2012-10-25 23:34:42 -05004454static int rbd_add_parse_args(const char *buf,
Alex Elderdc79b112012-10-25 23:34:41 -05004455 struct ceph_options **ceph_opts,
Alex Elder859c31d2012-10-25 23:34:42 -05004456 struct rbd_options **opts,
4457 struct rbd_spec **rbd_spec)
Alex Eldera725f65e2012-02-02 08:13:30 -06004458{
Alex Elderd22f76e2012-07-12 10:46:35 -05004459 size_t len;
Alex Elder859c31d2012-10-25 23:34:42 -05004460 char *options;
Alex Elder0ddebc02012-10-25 23:34:41 -05004461 const char *mon_addrs;
4462 size_t mon_addrs_size;
Alex Elder859c31d2012-10-25 23:34:42 -05004463 struct rbd_spec *spec = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05004464 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05004465 struct ceph_options *copts;
Alex Elderdc79b112012-10-25 23:34:41 -05004466 int ret;
Alex Eldere28fff262012-02-02 08:13:30 -06004467
4468 /* The first four tokens are required */
4469
Alex Elder7ef32142012-02-02 08:13:30 -06004470 len = next_token(&buf);
Alex Elder4fb5d6712012-11-01 10:17:15 -05004471 if (!len) {
4472 rbd_warn(NULL, "no monitor address(es) provided");
4473 return -EINVAL;
4474 }
Alex Elder0ddebc02012-10-25 23:34:41 -05004475 mon_addrs = buf;
Alex Elderf28e5652012-10-25 23:34:41 -05004476 mon_addrs_size = len + 1;
Alex Elder7ef32142012-02-02 08:13:30 -06004477 buf += len;
Alex Eldera725f65e2012-02-02 08:13:30 -06004478
Alex Elderdc79b112012-10-25 23:34:41 -05004479 ret = -EINVAL;
Alex Elderf28e5652012-10-25 23:34:41 -05004480 options = dup_token(&buf, NULL);
4481 if (!options)
Alex Elderdc79b112012-10-25 23:34:41 -05004482 return -ENOMEM;
Alex Elder4fb5d6712012-11-01 10:17:15 -05004483 if (!*options) {
4484 rbd_warn(NULL, "no options provided");
4485 goto out_err;
4486 }
Alex Eldera725f65e2012-02-02 08:13:30 -06004487
Alex Elder859c31d2012-10-25 23:34:42 -05004488 spec = rbd_spec_alloc();
4489 if (!spec)
Alex Elderf28e5652012-10-25 23:34:41 -05004490 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05004491
4492 spec->pool_name = dup_token(&buf, NULL);
4493 if (!spec->pool_name)
4494 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05004495 if (!*spec->pool_name) {
4496 rbd_warn(NULL, "no pool name provided");
4497 goto out_err;
4498 }
Alex Eldere28fff262012-02-02 08:13:30 -06004499
Alex Elder69e7a022012-11-01 08:39:26 -05004500 spec->image_name = dup_token(&buf, NULL);
Alex Elder859c31d2012-10-25 23:34:42 -05004501 if (!spec->image_name)
Alex Elderf28e5652012-10-25 23:34:41 -05004502 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05004503 if (!*spec->image_name) {
4504 rbd_warn(NULL, "no image name provided");
4505 goto out_err;
4506 }
Alex Eldere28fff262012-02-02 08:13:30 -06004507
Alex Elderf28e5652012-10-25 23:34:41 -05004508 /*
4509 * Snapshot name is optional; default is to use "-"
4510 * (indicating the head/no snapshot).
4511 */
Alex Elder3feeb8942012-08-31 17:29:52 -05004512 len = next_token(&buf);
Alex Elder820a5f32012-07-09 21:04:24 -05004513 if (!len) {
Alex Elder3feeb8942012-08-31 17:29:52 -05004514 buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
4515 len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
Alex Elderf28e5652012-10-25 23:34:41 -05004516 } else if (len > RBD_MAX_SNAP_NAME_LEN) {
Alex Elderdc79b112012-10-25 23:34:41 -05004517 ret = -ENAMETOOLONG;
Alex Elderf28e5652012-10-25 23:34:41 -05004518 goto out_err;
Alex Elder849b4262012-07-09 21:04:24 -05004519 }
Alex Elder4caf35f2012-11-01 08:39:27 -05004520 spec->snap_name = kmemdup(buf, len + 1, GFP_KERNEL);
Alex Elder859c31d2012-10-25 23:34:42 -05004521 if (!spec->snap_name)
Alex Elderf28e5652012-10-25 23:34:41 -05004522 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05004523 *(spec->snap_name + len) = '\0';
Alex Eldere5c35532012-10-25 23:34:41 -05004524
Alex Elder0ddebc02012-10-25 23:34:41 -05004525 /* Initialize all rbd options to the defaults */
Alex Eldere28fff262012-02-02 08:13:30 -06004526
Alex Elder4e9afeb2012-10-25 23:34:41 -05004527 rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL);
4528 if (!rbd_opts)
4529 goto out_mem;
4530
4531 rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
Alex Elderd22f76e2012-07-12 10:46:35 -05004532
Alex Elder859c31d2012-10-25 23:34:42 -05004533 copts = ceph_parse_options(options, mon_addrs,
Alex Elder0ddebc02012-10-25 23:34:41 -05004534 mon_addrs + mon_addrs_size - 1,
Alex Elder4e9afeb2012-10-25 23:34:41 -05004535 parse_rbd_opts_token, rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05004536 if (IS_ERR(copts)) {
4537 ret = PTR_ERR(copts);
Alex Elderdc79b112012-10-25 23:34:41 -05004538 goto out_err;
4539 }
Alex Elder859c31d2012-10-25 23:34:42 -05004540 kfree(options);
4541
4542 *ceph_opts = copts;
Alex Elder4e9afeb2012-10-25 23:34:41 -05004543 *opts = rbd_opts;
Alex Elder859c31d2012-10-25 23:34:42 -05004544 *rbd_spec = spec;
Alex Elder0ddebc02012-10-25 23:34:41 -05004545
Alex Elderdc79b112012-10-25 23:34:41 -05004546 return 0;
Alex Elderf28e5652012-10-25 23:34:41 -05004547out_mem:
Alex Elderdc79b112012-10-25 23:34:41 -05004548 ret = -ENOMEM;
Alex Elderd22f76e2012-07-12 10:46:35 -05004549out_err:
Alex Elder859c31d2012-10-25 23:34:42 -05004550 kfree(rbd_opts);
4551 rbd_spec_put(spec);
Alex Elderf28e5652012-10-25 23:34:41 -05004552 kfree(options);
Alex Elderd22f76e2012-07-12 10:46:35 -05004553
Alex Elderdc79b112012-10-25 23:34:41 -05004554 return ret;
Alex Eldera725f65e2012-02-02 08:13:30 -06004555}
4556
Alex Elder589d30e2012-07-10 20:30:11 -05004557/*
4558 * An rbd format 2 image has a unique identifier, distinct from the
4559 * name given to it by the user. Internally, that identifier is
4560 * what's used to specify the names of objects related to the image.
4561 *
4562 * A special "rbd id" object is used to map an rbd image name to its
4563 * id. If that object doesn't exist, then there is no v2 rbd image
4564 * with the supplied name.
4565 *
4566 * This function will record the given rbd_dev's image_id field if
4567 * it can be determined, and in that case will return 0. If any
4568 * errors occur a negative errno will be returned and the rbd_dev's
4569 * image_id field will be unchanged (and should be NULL).
4570 */
4571static int rbd_dev_image_id(struct rbd_device *rbd_dev)
4572{
4573 int ret;
4574 size_t size;
4575 char *object_name;
4576 void *response;
4577 void *p;
4578
Alex Elder2f82ee52012-10-30 19:40:33 -05004579 /* If we already have it we don't need to look it up */
4580
4581 if (rbd_dev->spec->image_id)
4582 return 0;
4583
Alex Elder589d30e2012-07-10 20:30:11 -05004584 /*
Alex Elder2c0d0a12012-10-30 19:40:33 -05004585 * When probing a parent image, the image id is already
4586 * known (and the image name likely is not). There's no
4587 * need to fetch the image id again in this case.
4588 */
4589 if (rbd_dev->spec->image_id)
4590 return 0;
4591
4592 /*
Alex Elder589d30e2012-07-10 20:30:11 -05004593 * First, see if the format 2 image id file exists, and if
4594 * so, get the image's persistent id from it.
4595 */
Alex Elder69e7a022012-11-01 08:39:26 -05004596 size = sizeof (RBD_ID_PREFIX) + strlen(rbd_dev->spec->image_name);
Alex Elder589d30e2012-07-10 20:30:11 -05004597 object_name = kmalloc(size, GFP_NOIO);
4598 if (!object_name)
4599 return -ENOMEM;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004600 sprintf(object_name, "%s%s", RBD_ID_PREFIX, rbd_dev->spec->image_name);
Alex Elder589d30e2012-07-10 20:30:11 -05004601 dout("rbd id object name is %s\n", object_name);
4602
4603 /* Response will be an encoded string, which includes a length */
4604
4605 size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
4606 response = kzalloc(size, GFP_NOIO);
4607 if (!response) {
4608 ret = -ENOMEM;
4609 goto out;
4610 }
4611
Alex Elder36be9a72013-01-19 00:30:28 -06004612 ret = rbd_obj_method_sync(rbd_dev, object_name,
Alex Elder41579762013-04-21 12:14:45 -05004613 "rbd", "get_id", NULL, 0,
Alex Elder07b23912012-11-09 08:43:16 -06004614 response, RBD_IMAGE_ID_LEN_MAX, NULL);
Alex Elder36be9a72013-01-19 00:30:28 -06004615 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder589d30e2012-07-10 20:30:11 -05004616 if (ret < 0)
4617 goto out;
4618
4619 p = response;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004620 rbd_dev->spec->image_id = ceph_extract_encoded_string(&p,
Alex Elder57385b52013-04-21 12:14:45 -05004621 p + ret,
Alex Elder979ed482012-11-01 08:39:26 -05004622 NULL, GFP_NOIO);
Alex Elder57385b52013-04-21 12:14:45 -05004623 ret = 0;
4624
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004625 if (IS_ERR(rbd_dev->spec->image_id)) {
4626 ret = PTR_ERR(rbd_dev->spec->image_id);
4627 rbd_dev->spec->image_id = NULL;
Alex Elder589d30e2012-07-10 20:30:11 -05004628 } else {
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004629 dout("image_id is %s\n", rbd_dev->spec->image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05004630 }
4631out:
4632 kfree(response);
4633 kfree(object_name);
4634
4635 return ret;
4636}
4637
Alex Eldera30b71b2012-07-10 20:30:11 -05004638static int rbd_dev_v1_probe(struct rbd_device *rbd_dev)
4639{
4640 int ret;
4641 size_t size;
4642
4643 /* Version 1 images have no id; empty string is used */
4644
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004645 rbd_dev->spec->image_id = kstrdup("", GFP_KERNEL);
4646 if (!rbd_dev->spec->image_id)
Alex Eldera30b71b2012-07-10 20:30:11 -05004647 return -ENOMEM;
Alex Eldera30b71b2012-07-10 20:30:11 -05004648
4649 /* Record the header object name for this rbd image. */
4650
Alex Elder69e7a022012-11-01 08:39:26 -05004651 size = strlen(rbd_dev->spec->image_name) + sizeof (RBD_SUFFIX);
Alex Eldera30b71b2012-07-10 20:30:11 -05004652 rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
4653 if (!rbd_dev->header_name) {
4654 ret = -ENOMEM;
4655 goto out_err;
4656 }
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004657 sprintf(rbd_dev->header_name, "%s%s",
4658 rbd_dev->spec->image_name, RBD_SUFFIX);
Alex Eldera30b71b2012-07-10 20:30:11 -05004659
4660 /* Populate rbd image metadata */
4661
4662 ret = rbd_read_header(rbd_dev, &rbd_dev->header);
4663 if (ret < 0)
4664 goto out_err;
Alex Elder86b00e02012-10-25 23:34:42 -05004665
4666 /* Version 1 images have no parent (no layering) */
4667
4668 rbd_dev->parent_spec = NULL;
4669 rbd_dev->parent_overlap = 0;
4670
Alex Eldera30b71b2012-07-10 20:30:11 -05004671 rbd_dev->image_format = 1;
4672
4673 dout("discovered version 1 image, header name is %s\n",
4674 rbd_dev->header_name);
4675
4676 return 0;
4677
4678out_err:
4679 kfree(rbd_dev->header_name);
4680 rbd_dev->header_name = NULL;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004681 kfree(rbd_dev->spec->image_id);
4682 rbd_dev->spec->image_id = NULL;
Alex Eldera30b71b2012-07-10 20:30:11 -05004683
4684 return ret;
4685}
4686
4687static int rbd_dev_v2_probe(struct rbd_device *rbd_dev)
4688{
4689 size_t size;
Alex Elder9d475de2012-07-03 16:01:19 -05004690 int ret;
Alex Elder6e14b1a2012-07-03 16:01:19 -05004691 u64 ver = 0;
Alex Eldera30b71b2012-07-10 20:30:11 -05004692
4693 /*
4694 * Image id was filled in by the caller. Record the header
4695 * object name for this rbd image.
4696 */
Alex Elder979ed482012-11-01 08:39:26 -05004697 size = sizeof (RBD_HEADER_PREFIX) + strlen(rbd_dev->spec->image_id);
Alex Eldera30b71b2012-07-10 20:30:11 -05004698 rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
4699 if (!rbd_dev->header_name)
4700 return -ENOMEM;
4701 sprintf(rbd_dev->header_name, "%s%s",
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004702 RBD_HEADER_PREFIX, rbd_dev->spec->image_id);
Alex Elder9d475de2012-07-03 16:01:19 -05004703
4704 /* Get the size and object order for the image */
Alex Elder9d475de2012-07-03 16:01:19 -05004705 ret = rbd_dev_v2_image_size(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05004706 if (ret)
Alex Elder9d475de2012-07-03 16:01:19 -05004707 goto out_err;
Alex Elder1e130192012-07-03 16:01:19 -05004708
4709 /* Get the object prefix (a.k.a. block_name) for the image */
4710
4711 ret = rbd_dev_v2_object_prefix(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05004712 if (ret)
Alex Elder1e130192012-07-03 16:01:19 -05004713 goto out_err;
Alex Elderb1b54022012-07-03 16:01:19 -05004714
Alex Elderd8891402012-10-09 13:50:17 -07004715 /* Get the and check features for the image */
Alex Elderb1b54022012-07-03 16:01:19 -05004716
4717 ret = rbd_dev_v2_features(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05004718 if (ret)
Alex Elderb1b54022012-07-03 16:01:19 -05004719 goto out_err;
Alex Elder35d489f2012-07-03 16:01:19 -05004720
Alex Elder86b00e02012-10-25 23:34:42 -05004721 /* If the image supports layering, get the parent info */
4722
4723 if (rbd_dev->header.features & RBD_FEATURE_LAYERING) {
4724 ret = rbd_dev_v2_parent_info(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05004725 if (ret)
Alex Elder86b00e02012-10-25 23:34:42 -05004726 goto out_err;
Alex Elder770eba62012-10-25 23:34:40 -05004727 rbd_warn(rbd_dev, "WARNING: kernel support for "
4728 "layered rbd images is EXPERIMENTAL!");
Alex Elder86b00e02012-10-25 23:34:42 -05004729 }
4730
Alex Eldercc070d52013-04-21 12:14:45 -05004731 /* If the image supports fancy striping, get its parameters */
4732
4733 if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) {
4734 ret = rbd_dev_v2_striping_info(rbd_dev);
4735 if (ret < 0)
4736 goto out_err;
4737 }
4738
Alex Elder6e14b1a2012-07-03 16:01:19 -05004739 /* crypto and compression type aren't (yet) supported for v2 images */
Alex Elder35d489f2012-07-03 16:01:19 -05004740
Alex Elder6e14b1a2012-07-03 16:01:19 -05004741 rbd_dev->header.crypt_type = 0;
4742 rbd_dev->header.comp_type = 0;
4743
4744 /* Get the snapshot context, plus the header version */
4745
4746 ret = rbd_dev_v2_snap_context(rbd_dev, &ver);
Alex Elder35d489f2012-07-03 16:01:19 -05004747 if (ret)
4748 goto out_err;
Alex Elder6e14b1a2012-07-03 16:01:19 -05004749 rbd_dev->header.obj_version = ver;
4750
Alex Eldera30b71b2012-07-10 20:30:11 -05004751 rbd_dev->image_format = 2;
4752
4753 dout("discovered version 2 image, header name is %s\n",
4754 rbd_dev->header_name);
4755
Alex Elder35152972012-08-31 17:29:55 -05004756 return 0;
Alex Elder9d475de2012-07-03 16:01:19 -05004757out_err:
Alex Elder86b00e02012-10-25 23:34:42 -05004758 rbd_dev->parent_overlap = 0;
4759 rbd_spec_put(rbd_dev->parent_spec);
4760 rbd_dev->parent_spec = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05004761 kfree(rbd_dev->header_name);
4762 rbd_dev->header_name = NULL;
Alex Elder1e130192012-07-03 16:01:19 -05004763 kfree(rbd_dev->header.object_prefix);
4764 rbd_dev->header.object_prefix = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05004765
4766 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05004767}
4768
Alex Elder83a06262012-10-30 15:47:17 -05004769static int rbd_dev_probe_finish(struct rbd_device *rbd_dev)
4770{
Alex Elder2f82ee52012-10-30 19:40:33 -05004771 struct rbd_device *parent = NULL;
4772 struct rbd_spec *parent_spec = NULL;
4773 struct rbd_client *rbdc = NULL;
Alex Elder83a06262012-10-30 15:47:17 -05004774 int ret;
4775
4776 /* no need to lock here, as rbd_dev is not registered yet */
4777 ret = rbd_dev_snaps_update(rbd_dev);
4778 if (ret)
4779 return ret;
4780
Alex Elder9e15b772012-10-30 19:40:33 -05004781 ret = rbd_dev_probe_update_spec(rbd_dev);
4782 if (ret)
4783 goto err_out_snaps;
4784
Alex Elder83a06262012-10-30 15:47:17 -05004785 ret = rbd_dev_set_mapping(rbd_dev);
4786 if (ret)
4787 goto err_out_snaps;
4788
4789 /* generate unique id: find highest unique id, add one */
4790 rbd_dev_id_get(rbd_dev);
4791
4792 /* Fill in the device name, now that we have its id. */
4793 BUILD_BUG_ON(DEV_NAME_LEN
4794 < sizeof (RBD_DRV_NAME) + MAX_INT_FORMAT_WIDTH);
4795 sprintf(rbd_dev->name, "%s%d", RBD_DRV_NAME, rbd_dev->dev_id);
4796
4797 /* Get our block major device number. */
4798
4799 ret = register_blkdev(0, rbd_dev->name);
4800 if (ret < 0)
4801 goto err_out_id;
4802 rbd_dev->major = ret;
4803
4804 /* Set up the blkdev mapping. */
4805
4806 ret = rbd_init_disk(rbd_dev);
4807 if (ret)
4808 goto err_out_blkdev;
4809
4810 ret = rbd_bus_add_dev(rbd_dev);
4811 if (ret)
4812 goto err_out_disk;
4813
4814 /*
4815 * At this point cleanup in the event of an error is the job
4816 * of the sysfs code (initiated by rbd_bus_del_dev()).
4817 */
Alex Elder2f82ee52012-10-30 19:40:33 -05004818 /* Probe the parent if there is one */
4819
4820 if (rbd_dev->parent_spec) {
4821 /*
4822 * We need to pass a reference to the client and the
4823 * parent spec when creating the parent rbd_dev.
4824 * Images related by parent/child relationships
4825 * always share both.
4826 */
4827 parent_spec = rbd_spec_get(rbd_dev->parent_spec);
4828 rbdc = __rbd_get_client(rbd_dev->rbd_client);
4829
4830 parent = rbd_dev_create(rbdc, parent_spec);
4831 if (!parent) {
4832 ret = -ENOMEM;
4833 goto err_out_spec;
4834 }
4835 rbdc = NULL; /* parent now owns reference */
4836 parent_spec = NULL; /* parent now owns reference */
4837 ret = rbd_dev_probe(parent);
4838 if (ret < 0)
4839 goto err_out_parent;
4840 rbd_dev->parent = parent;
4841 }
4842
Alex Elder83a06262012-10-30 15:47:17 -05004843 down_write(&rbd_dev->header_rwsem);
4844 ret = rbd_dev_snaps_register(rbd_dev);
4845 up_write(&rbd_dev->header_rwsem);
4846 if (ret)
4847 goto err_out_bus;
4848
Alex Elder9969ebc2013-01-18 12:31:10 -06004849 ret = rbd_dev_header_watch_sync(rbd_dev, 1);
Alex Elder83a06262012-10-30 15:47:17 -05004850 if (ret)
4851 goto err_out_bus;
4852
4853 /* Everything's ready. Announce the disk to the world. */
4854
4855 add_disk(rbd_dev->disk);
4856
4857 pr_info("%s: added with size 0x%llx\n", rbd_dev->disk->disk_name,
4858 (unsigned long long) rbd_dev->mapping.size);
4859
4860 return ret;
Alex Elder2f82ee52012-10-30 19:40:33 -05004861
4862err_out_parent:
4863 rbd_dev_destroy(parent);
4864err_out_spec:
4865 rbd_spec_put(parent_spec);
4866 rbd_put_client(rbdc);
Alex Elder83a06262012-10-30 15:47:17 -05004867err_out_bus:
4868 /* this will also clean up rest of rbd_dev stuff */
4869
4870 rbd_bus_del_dev(rbd_dev);
4871
4872 return ret;
4873err_out_disk:
4874 rbd_free_disk(rbd_dev);
4875err_out_blkdev:
4876 unregister_blkdev(rbd_dev->major, rbd_dev->name);
4877err_out_id:
4878 rbd_dev_id_put(rbd_dev);
4879err_out_snaps:
4880 rbd_remove_all_snaps(rbd_dev);
4881
4882 return ret;
4883}
4884
Alex Eldera30b71b2012-07-10 20:30:11 -05004885/*
4886 * Probe for the existence of the header object for the given rbd
4887 * device. For format 2 images this includes determining the image
4888 * id.
4889 */
4890static int rbd_dev_probe(struct rbd_device *rbd_dev)
4891{
4892 int ret;
4893
4894 /*
4895 * Get the id from the image id object. If it's not a
4896 * format 2 image, we'll get ENOENT back, and we'll assume
4897 * it's a format 1 image.
4898 */
4899 ret = rbd_dev_image_id(rbd_dev);
4900 if (ret)
4901 ret = rbd_dev_v1_probe(rbd_dev);
4902 else
4903 ret = rbd_dev_v2_probe(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05004904 if (ret) {
Alex Eldera30b71b2012-07-10 20:30:11 -05004905 dout("probe failed, returning %d\n", ret);
4906
Alex Elder83a06262012-10-30 15:47:17 -05004907 return ret;
4908 }
4909
4910 ret = rbd_dev_probe_finish(rbd_dev);
4911 if (ret)
4912 rbd_header_free(&rbd_dev->header);
4913
Alex Eldera30b71b2012-07-10 20:30:11 -05004914 return ret;
4915}
4916
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07004917static ssize_t rbd_add(struct bus_type *bus,
4918 const char *buf,
4919 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004920{
Alex Eldercb8627c2012-07-09 21:04:23 -05004921 struct rbd_device *rbd_dev = NULL;
Alex Elderdc79b112012-10-25 23:34:41 -05004922 struct ceph_options *ceph_opts = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05004923 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05004924 struct rbd_spec *spec = NULL;
Alex Elder9d3997f2012-10-25 23:34:42 -05004925 struct rbd_client *rbdc;
Alex Elder27cc2592012-02-02 08:13:30 -06004926 struct ceph_osd_client *osdc;
4927 int rc = -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004928
4929 if (!try_module_get(THIS_MODULE))
4930 return -ENODEV;
4931
Alex Eldera725f65e2012-02-02 08:13:30 -06004932 /* parse add command */
Alex Elder859c31d2012-10-25 23:34:42 -05004933 rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec);
Alex Elderdc79b112012-10-25 23:34:41 -05004934 if (rc < 0)
Alex Elderbd4ba652012-10-25 23:34:42 -05004935 goto err_out_module;
Alex Eldera725f65e2012-02-02 08:13:30 -06004936
Alex Elder9d3997f2012-10-25 23:34:42 -05004937 rbdc = rbd_get_client(ceph_opts);
4938 if (IS_ERR(rbdc)) {
4939 rc = PTR_ERR(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05004940 goto err_out_args;
Alex Elder9d3997f2012-10-25 23:34:42 -05004941 }
Alex Elderc53d5892012-10-25 23:34:42 -05004942 ceph_opts = NULL; /* rbd_dev client now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004943
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004944 /* pick the pool */
Alex Elder9d3997f2012-10-25 23:34:42 -05004945 osdc = &rbdc->client->osdc;
Alex Elder859c31d2012-10-25 23:34:42 -05004946 rc = ceph_pg_poolid_by_name(osdc->osdmap, spec->pool_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004947 if (rc < 0)
4948 goto err_out_client;
Alex Elder859c31d2012-10-25 23:34:42 -05004949 spec->pool_id = (u64) rc;
4950
Alex Elder0903e872012-11-14 12:25:19 -06004951 /* The ceph file layout needs to fit pool id in 32 bits */
4952
4953 if (WARN_ON(spec->pool_id > (u64) U32_MAX)) {
4954 rc = -EIO;
4955 goto err_out_client;
4956 }
4957
Alex Elderc53d5892012-10-25 23:34:42 -05004958 rbd_dev = rbd_dev_create(rbdc, spec);
Alex Elderbd4ba652012-10-25 23:34:42 -05004959 if (!rbd_dev)
4960 goto err_out_client;
Alex Elderc53d5892012-10-25 23:34:42 -05004961 rbdc = NULL; /* rbd_dev now owns this */
4962 spec = NULL; /* rbd_dev now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004963
Alex Elderbd4ba652012-10-25 23:34:42 -05004964 rbd_dev->mapping.read_only = rbd_opts->read_only;
Alex Elderc53d5892012-10-25 23:34:42 -05004965 kfree(rbd_opts);
4966 rbd_opts = NULL; /* done with this */
Alex Elderbd4ba652012-10-25 23:34:42 -05004967
Alex Eldera30b71b2012-07-10 20:30:11 -05004968 rc = rbd_dev_probe(rbd_dev);
4969 if (rc < 0)
Alex Elderc53d5892012-10-25 23:34:42 -05004970 goto err_out_rbd_dev;
Alex Elder05fd6f62012-08-29 17:11:07 -05004971
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004972 return count;
Alex Elderc53d5892012-10-25 23:34:42 -05004973err_out_rbd_dev:
4974 rbd_dev_destroy(rbd_dev);
Alex Elderbd4ba652012-10-25 23:34:42 -05004975err_out_client:
Alex Elder9d3997f2012-10-25 23:34:42 -05004976 rbd_put_client(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05004977err_out_args:
Alex Elder78cea762012-10-25 23:34:41 -05004978 if (ceph_opts)
4979 ceph_destroy_options(ceph_opts);
Alex Elder4e9afeb2012-10-25 23:34:41 -05004980 kfree(rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05004981 rbd_spec_put(spec);
Alex Elderbd4ba652012-10-25 23:34:42 -05004982err_out_module:
4983 module_put(THIS_MODULE);
Alex Elder27cc2592012-02-02 08:13:30 -06004984
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004985 dout("Error adding device %s\n", buf);
Alex Elder27cc2592012-02-02 08:13:30 -06004986
4987 return (ssize_t) rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004988}
4989
Alex Elderde71a292012-07-03 16:01:19 -05004990static struct rbd_device *__rbd_get_dev(unsigned long dev_id)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004991{
4992 struct list_head *tmp;
4993 struct rbd_device *rbd_dev;
4994
Alex Eldere124a82f2012-01-29 13:57:44 -06004995 spin_lock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004996 list_for_each(tmp, &rbd_dev_list) {
4997 rbd_dev = list_entry(tmp, struct rbd_device, node);
Alex Elderde71a292012-07-03 16:01:19 -05004998 if (rbd_dev->dev_id == dev_id) {
Alex Eldere124a82f2012-01-29 13:57:44 -06004999 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005000 return rbd_dev;
Alex Eldere124a82f2012-01-29 13:57:44 -06005001 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005002 }
Alex Eldere124a82f2012-01-29 13:57:44 -06005003 spin_unlock(&rbd_dev_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005004 return NULL;
5005}
5006
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005007static void rbd_dev_release(struct device *dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005008{
Alex Elder593a9e72012-02-07 12:03:37 -06005009 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005010
Yehuda Sadeh59c2be12011-03-21 15:10:11 -07005011 if (rbd_dev->watch_event)
Alex Elder9969ebc2013-01-18 12:31:10 -06005012 rbd_dev_header_watch_sync(rbd_dev, 0);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005013
5014 /* clean up and free blkdev */
5015 rbd_free_disk(rbd_dev);
5016 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Alex Elder32eec682012-02-08 16:11:14 -06005017
Alex Elder2ac4e752012-07-10 20:30:10 -05005018 /* release allocated disk header fields */
5019 rbd_header_free(&rbd_dev->header);
5020
Alex Elder32eec682012-02-08 16:11:14 -06005021 /* done with the id, and with the rbd_dev */
Alex Eldere2839302012-08-29 17:11:06 -05005022 rbd_dev_id_put(rbd_dev);
Alex Elderc53d5892012-10-25 23:34:42 -05005023 rbd_assert(rbd_dev->rbd_client != NULL);
5024 rbd_dev_destroy(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005025
5026 /* release module ref */
5027 module_put(THIS_MODULE);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005028}
5029
Alex Elder2f82ee52012-10-30 19:40:33 -05005030static void __rbd_remove(struct rbd_device *rbd_dev)
5031{
5032 rbd_remove_all_snaps(rbd_dev);
5033 rbd_bus_del_dev(rbd_dev);
5034}
5035
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005036static ssize_t rbd_remove(struct bus_type *bus,
5037 const char *buf,
5038 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005039{
5040 struct rbd_device *rbd_dev = NULL;
5041 int target_id, rc;
5042 unsigned long ul;
5043 int ret = count;
5044
5045 rc = strict_strtoul(buf, 10, &ul);
5046 if (rc)
5047 return rc;
5048
5049 /* convert to int; abort if we lost anything in the conversion */
5050 target_id = (int) ul;
5051 if (target_id != ul)
5052 return -EINVAL;
5053
5054 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
5055
5056 rbd_dev = __rbd_get_dev(target_id);
5057 if (!rbd_dev) {
5058 ret = -ENOENT;
5059 goto done;
5060 }
5061
Alex Eldera14ea262013-02-05 13:23:12 -06005062 spin_lock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -06005063 if (rbd_dev->open_count)
Alex Elder42382b72012-11-16 09:29:16 -06005064 ret = -EBUSY;
Alex Elderb82d1672013-01-14 12:43:31 -06005065 else
5066 set_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags);
Alex Eldera14ea262013-02-05 13:23:12 -06005067 spin_unlock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -06005068 if (ret < 0)
Alex Elder42382b72012-11-16 09:29:16 -06005069 goto done;
Alex Elder42382b72012-11-16 09:29:16 -06005070
Alex Elder2f82ee52012-10-30 19:40:33 -05005071 while (rbd_dev->parent_spec) {
5072 struct rbd_device *first = rbd_dev;
5073 struct rbd_device *second = first->parent;
5074 struct rbd_device *third;
5075
5076 /*
5077 * Follow to the parent with no grandparent and
5078 * remove it.
5079 */
5080 while (second && (third = second->parent)) {
5081 first = second;
5082 second = third;
5083 }
5084 __rbd_remove(second);
5085 rbd_spec_put(first->parent_spec);
5086 first->parent_spec = NULL;
5087 first->parent_overlap = 0;
5088 first->parent = NULL;
5089 }
5090 __rbd_remove(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005091
5092done:
5093 mutex_unlock(&ctl_mutex);
Alex Elderaafb2302012-09-06 16:00:54 -05005094
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005095 return ret;
5096}
5097
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005098/*
5099 * create control files in sysfs
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005100 * /sys/bus/rbd/...
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005101 */
5102static int rbd_sysfs_init(void)
5103{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005104 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005105
Alex Elderfed4c142012-02-07 12:03:36 -06005106 ret = device_register(&rbd_root_dev);
Alex Elder21079782012-01-24 10:08:36 -06005107 if (ret < 0)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005108 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005109
Alex Elderfed4c142012-02-07 12:03:36 -06005110 ret = bus_register(&rbd_bus_type);
5111 if (ret < 0)
5112 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005113
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005114 return ret;
5115}
5116
5117static void rbd_sysfs_cleanup(void)
5118{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005119 bus_unregister(&rbd_bus_type);
Alex Elderfed4c142012-02-07 12:03:36 -06005120 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005121}
5122
Alex Eldercc344fa2013-02-19 12:25:56 -06005123static int __init rbd_init(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005124{
5125 int rc;
5126
Alex Elder1e32d342013-01-30 11:13:33 -06005127 if (!libceph_compatible(NULL)) {
5128 rbd_warn(NULL, "libceph incompatibility (quitting)");
5129
5130 return -EINVAL;
5131 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005132 rc = rbd_sysfs_init();
5133 if (rc)
5134 return rc;
Alex Elderf0f8cef2012-01-29 13:57:44 -06005135 pr_info("loaded " RBD_DRV_NAME_LONG "\n");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005136 return 0;
5137}
5138
Alex Eldercc344fa2013-02-19 12:25:56 -06005139static void __exit rbd_exit(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005140{
5141 rbd_sysfs_cleanup();
5142}
5143
5144module_init(rbd_init);
5145module_exit(rbd_exit);
5146
5147MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
5148MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
5149MODULE_DESCRIPTION("rados block device");
5150
5151/* following authorship retained from original osdblk.c */
5152MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
5153
5154MODULE_LICENSE("GPL");