blob: 98792b2a7f6544f18caec7a264063f2032369afc [file] [log] [blame]
Alex Eldere2a58ee2013-04-30 00:44:33 -05001
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002/*
3 rbd.c -- Export ceph rados objects as a Linux block device
4
5
6 based on drivers/block/osdblk.c:
7
8 Copyright 2009 Red Hat, Inc.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; see the file COPYING. If not, write to
21 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22
23
24
Yehuda Sadehdfc56062010-11-19 14:51:04 -080025 For usage instructions, please refer to:
Yehuda Sadeh602adf42010-08-12 16:11:25 -070026
Yehuda Sadehdfc56062010-11-19 14:51:04 -080027 Documentation/ABI/testing/sysfs-bus-rbd
Yehuda Sadeh602adf42010-08-12 16:11:25 -070028
29 */
30
31#include <linux/ceph/libceph.h>
32#include <linux/ceph/osd_client.h>
33#include <linux/ceph/mon_client.h>
34#include <linux/ceph/decode.h>
Yehuda Sadeh59c2be12011-03-21 15:10:11 -070035#include <linux/parser.h>
Alex Elder30d1cff2013-05-01 12:43:03 -050036#include <linux/bsearch.h>
Yehuda Sadeh602adf42010-08-12 16:11:25 -070037
38#include <linux/kernel.h>
39#include <linux/device.h>
40#include <linux/module.h>
41#include <linux/fs.h>
42#include <linux/blkdev.h>
Alex Elder1c2a9df2013-05-01 12:43:03 -050043#include <linux/slab.h>
Ilya Dryomovf8a22fc2013-12-13 15:28:57 +020044#include <linux/idr.h>
Yehuda Sadeh602adf42010-08-12 16:11:25 -070045
46#include "rbd_types.h"
47
Alex Elderaafb2302012-09-06 16:00:54 -050048#define RBD_DEBUG /* Activate rbd_assert() calls */
49
Alex Elder593a9e72012-02-07 12:03:37 -060050/*
51 * The basic unit of block I/O is a sector. It is interpreted in a
52 * number of contexts in Linux (blk, bio, genhd), but the default is
53 * universally 512 bytes. These symbols are just slightly more
54 * meaningful than the bare numbers they represent.
55 */
56#define SECTOR_SHIFT 9
57#define SECTOR_SIZE (1ULL << SECTOR_SHIFT)
58
Alex Eldera2acd002013-05-08 22:50:04 -050059/*
60 * Increment the given counter and return its updated value.
61 * If the counter is already 0 it will not be incremented.
62 * If the counter is already at its maximum value returns
63 * -EINVAL without updating it.
64 */
65static int atomic_inc_return_safe(atomic_t *v)
66{
67 unsigned int counter;
68
69 counter = (unsigned int)__atomic_add_unless(v, 1, 0);
70 if (counter <= (unsigned int)INT_MAX)
71 return (int)counter;
72
73 atomic_dec(v);
74
75 return -EINVAL;
76}
77
78/* Decrement the counter. Return the resulting value, or -EINVAL */
79static int atomic_dec_return_safe(atomic_t *v)
80{
81 int counter;
82
83 counter = atomic_dec_return(v);
84 if (counter >= 0)
85 return counter;
86
87 atomic_inc(v);
88
89 return -EINVAL;
90}
91
Alex Elderf0f8cef2012-01-29 13:57:44 -060092#define RBD_DRV_NAME "rbd"
Yehuda Sadeh602adf42010-08-12 16:11:25 -070093
Ilya Dryomov7e513d42013-12-16 19:26:32 +020094#define RBD_MINORS_PER_MAJOR 256
95#define RBD_SINGLE_MAJOR_PART_SHIFT 4
Yehuda Sadeh602adf42010-08-12 16:11:25 -070096
Alex Elderd4b125e2012-07-03 16:01:19 -050097#define RBD_SNAP_DEV_NAME_PREFIX "snap_"
98#define RBD_MAX_SNAP_NAME_LEN \
99 (NAME_MAX - (sizeof (RBD_SNAP_DEV_NAME_PREFIX) - 1))
100
Alex Elder35d489f2012-07-03 16:01:19 -0500101#define RBD_MAX_SNAP_COUNT 510 /* allows max snapc to fit in 4KB */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700102
103#define RBD_SNAP_HEAD_NAME "-"
104
Alex Elder9682fc62013-04-30 00:44:33 -0500105#define BAD_SNAP_INDEX U32_MAX /* invalid index into snap array */
106
Alex Elder9e15b772012-10-30 19:40:33 -0500107/* This allows a single page to hold an image name sent by OSD */
108#define RBD_IMAGE_NAME_LEN_MAX (PAGE_SIZE - sizeof (__le32) - 1)
Alex Elder1e130192012-07-03 16:01:19 -0500109#define RBD_IMAGE_ID_LEN_MAX 64
Alex Elder9e15b772012-10-30 19:40:33 -0500110
Alex Elder1e130192012-07-03 16:01:19 -0500111#define RBD_OBJ_PREFIX_LEN_MAX 64
Alex Elder589d30e2012-07-10 20:30:11 -0500112
Alex Elderd8891402012-10-09 13:50:17 -0700113/* Feature bits */
114
Alex Elder5cbf6f122013-04-11 09:29:48 -0500115#define RBD_FEATURE_LAYERING (1<<0)
116#define RBD_FEATURE_STRIPINGV2 (1<<1)
117#define RBD_FEATURES_ALL \
118 (RBD_FEATURE_LAYERING | RBD_FEATURE_STRIPINGV2)
Alex Elderd8891402012-10-09 13:50:17 -0700119
120/* Features supported by this (client software) implementation. */
121
Alex Elder770eba62012-10-25 23:34:40 -0500122#define RBD_FEATURES_SUPPORTED (RBD_FEATURES_ALL)
Alex Elderd8891402012-10-09 13:50:17 -0700123
Alex Elder81a89792012-02-02 08:13:30 -0600124/*
125 * An RBD device name will be "rbd#", where the "rbd" comes from
126 * RBD_DRV_NAME above, and # is a unique integer identifier.
127 * MAX_INT_FORMAT_WIDTH is used in ensuring DEV_NAME_LEN is big
128 * enough to hold all possible device names.
129 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700130#define DEV_NAME_LEN 32
Alex Elder81a89792012-02-02 08:13:30 -0600131#define MAX_INT_FORMAT_WIDTH ((5 * sizeof (int)) / 2 + 1)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700132
133/*
134 * block device image metadata (in-memory version)
135 */
136struct rbd_image_header {
Alex Elderf35a4de2013-05-06 09:51:29 -0500137 /* These six fields never change for a given rbd image */
Alex Elder849b4262012-07-09 21:04:24 -0500138 char *object_prefix;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700139 __u8 obj_order;
140 __u8 crypt_type;
141 __u8 comp_type;
Alex Elderf35a4de2013-05-06 09:51:29 -0500142 u64 stripe_unit;
143 u64 stripe_count;
144 u64 features; /* Might be changeable someday? */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700145
Alex Elderf84344f2012-08-31 17:29:51 -0500146 /* The remaining fields need to be updated occasionally */
147 u64 image_size;
148 struct ceph_snap_context *snapc;
Alex Elderf35a4de2013-05-06 09:51:29 -0500149 char *snap_names; /* format 1 only */
150 u64 *snap_sizes; /* format 1 only */
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700151};
152
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500153/*
154 * An rbd image specification.
155 *
156 * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely
Alex Elderc66c6e02012-11-01 08:39:26 -0500157 * identify an image. Each rbd_dev structure includes a pointer to
158 * an rbd_spec structure that encapsulates this identity.
159 *
160 * Each of the id's in an rbd_spec has an associated name. For a
161 * user-mapped image, the names are supplied and the id's associated
162 * with them are looked up. For a layered image, a parent image is
163 * defined by the tuple, and the names are looked up.
164 *
165 * An rbd_dev structure contains a parent_spec pointer which is
166 * non-null if the image it represents is a child in a layered
167 * image. This pointer will refer to the rbd_spec structure used
168 * by the parent rbd_dev for its own identity (i.e., the structure
169 * is shared between the parent and child).
170 *
171 * Since these structures are populated once, during the discovery
172 * phase of image construction, they are effectively immutable so
173 * we make no effort to synchronize access to them.
174 *
175 * Note that code herein does not assume the image name is known (it
176 * could be a null pointer).
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500177 */
178struct rbd_spec {
179 u64 pool_id;
Alex Elderecb4dc22013-04-26 09:43:47 -0500180 const char *pool_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500181
Alex Elderecb4dc22013-04-26 09:43:47 -0500182 const char *image_id;
183 const char *image_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500184
185 u64 snap_id;
Alex Elderecb4dc22013-04-26 09:43:47 -0500186 const char *snap_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500187
188 struct kref kref;
189};
190
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700191/*
Alex Elderf0f8cef2012-01-29 13:57:44 -0600192 * an instance of the client. multiple devices may share an rbd client.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700193 */
194struct rbd_client {
195 struct ceph_client *client;
196 struct kref kref;
197 struct list_head node;
198};
199
Alex Elderbf0d5f502012-11-22 00:00:08 -0600200struct rbd_img_request;
201typedef void (*rbd_img_callback_t)(struct rbd_img_request *);
202
203#define BAD_WHICH U32_MAX /* Good which or bad which, which? */
204
205struct rbd_obj_request;
206typedef void (*rbd_obj_callback_t)(struct rbd_obj_request *);
207
Alex Elder9969ebc2013-01-18 12:31:10 -0600208enum obj_request_type {
209 OBJ_REQUEST_NODATA, OBJ_REQUEST_BIO, OBJ_REQUEST_PAGES
210};
Alex Elderbf0d5f502012-11-22 00:00:08 -0600211
Alex Elder926f9b32013-02-11 12:33:24 -0600212enum obj_req_flags {
213 OBJ_REQ_DONE, /* completion flag: not done = 0, done = 1 */
Alex Elder6365d332013-02-11 12:33:24 -0600214 OBJ_REQ_IMG_DATA, /* object usage: standalone = 0, image = 1 */
Alex Elder5679c592013-02-11 12:33:24 -0600215 OBJ_REQ_KNOWN, /* EXISTS flag valid: no = 0, yes = 1 */
216 OBJ_REQ_EXISTS, /* target exists: no = 0, yes = 1 */
Alex Elder926f9b32013-02-11 12:33:24 -0600217};
218
Alex Elderbf0d5f502012-11-22 00:00:08 -0600219struct rbd_obj_request {
220 const char *object_name;
221 u64 offset; /* object start byte */
222 u64 length; /* bytes from offset */
Alex Elder926f9b32013-02-11 12:33:24 -0600223 unsigned long flags;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600224
Alex Elderc5b5ef62013-02-11 12:33:24 -0600225 /*
226 * An object request associated with an image will have its
227 * img_data flag set; a standalone object request will not.
228 *
229 * A standalone object request will have which == BAD_WHICH
230 * and a null obj_request pointer.
231 *
232 * An object request initiated in support of a layered image
233 * object (to check for its existence before a write) will
234 * have which == BAD_WHICH and a non-null obj_request pointer.
235 *
236 * Finally, an object request for rbd image data will have
237 * which != BAD_WHICH, and will have a non-null img_request
238 * pointer. The value of which will be in the range
239 * 0..(img_request->obj_request_count-1).
240 */
241 union {
242 struct rbd_obj_request *obj_request; /* STAT op */
243 struct {
244 struct rbd_img_request *img_request;
245 u64 img_offset;
246 /* links for img_request->obj_requests list */
247 struct list_head links;
248 };
249 };
Alex Elderbf0d5f502012-11-22 00:00:08 -0600250 u32 which; /* posn image request list */
251
252 enum obj_request_type type;
Alex Elder788e2df2013-01-17 12:25:27 -0600253 union {
254 struct bio *bio_list;
255 struct {
256 struct page **pages;
257 u32 page_count;
258 };
259 };
Alex Elder0eefd472013-04-19 15:34:50 -0500260 struct page **copyup_pages;
Alex Elderebda6402013-05-10 16:29:22 -0500261 u32 copyup_page_count;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600262
263 struct ceph_osd_request *osd_req;
264
265 u64 xferred; /* bytes transferred */
Sage Weil1b83bef2013-02-25 16:11:12 -0800266 int result;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600267
268 rbd_obj_callback_t callback;
Alex Elder788e2df2013-01-17 12:25:27 -0600269 struct completion completion;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600270
271 struct kref kref;
272};
273
Alex Elder0c425242013-02-08 09:55:49 -0600274enum img_req_flags {
Alex Elder9849e982013-01-24 16:13:36 -0600275 IMG_REQ_WRITE, /* I/O direction: read = 0, write = 1 */
276 IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */
Alex Elderd0b2e942013-01-24 16:13:36 -0600277 IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */
Alex Elder0c425242013-02-08 09:55:49 -0600278};
279
Alex Elderbf0d5f502012-11-22 00:00:08 -0600280struct rbd_img_request {
Alex Elderbf0d5f502012-11-22 00:00:08 -0600281 struct rbd_device *rbd_dev;
282 u64 offset; /* starting image byte offset */
283 u64 length; /* byte count from offset */
Alex Elder0c425242013-02-08 09:55:49 -0600284 unsigned long flags;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600285 union {
Alex Elder9849e982013-01-24 16:13:36 -0600286 u64 snap_id; /* for reads */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600287 struct ceph_snap_context *snapc; /* for writes */
Alex Elder9849e982013-01-24 16:13:36 -0600288 };
289 union {
290 struct request *rq; /* block request */
291 struct rbd_obj_request *obj_request; /* obj req initiator */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600292 };
Alex Elder3d7efd12013-04-19 15:34:50 -0500293 struct page **copyup_pages;
Alex Elderebda6402013-05-10 16:29:22 -0500294 u32 copyup_page_count;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600295 spinlock_t completion_lock;/* protects next_completion */
296 u32 next_completion;
297 rbd_img_callback_t callback;
Alex Elder55f27e02013-04-10 12:34:25 -0500298 u64 xferred;/* aggregate bytes transferred */
Alex Eldera5a337d2013-01-24 16:13:36 -0600299 int result; /* first nonzero obj_request result */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600300
301 u32 obj_request_count;
302 struct list_head obj_requests; /* rbd_obj_request structs */
303
304 struct kref kref;
305};
306
307#define for_each_obj_request(ireq, oreq) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600308 list_for_each_entry(oreq, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600309#define for_each_obj_request_from(ireq, oreq) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600310 list_for_each_entry_from(oreq, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600311#define for_each_obj_request_safe(ireq, oreq, n) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600312 list_for_each_entry_safe_reverse(oreq, n, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600313
Alex Elderf84344f2012-08-31 17:29:51 -0500314struct rbd_mapping {
Alex Elder99c1f082012-08-30 14:42:15 -0500315 u64 size;
Alex Elder34b13182012-07-13 20:35:12 -0500316 u64 features;
Alex Elderf84344f2012-08-31 17:29:51 -0500317 bool read_only;
318};
319
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700320/*
321 * a single device
322 */
323struct rbd_device {
Alex Elderde71a292012-07-03 16:01:19 -0500324 int dev_id; /* blkdev unique id */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700325
326 int major; /* blkdev assigned major */
Ilya Dryomovdd82fff2013-12-13 15:28:57 +0200327 int minor;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700328 struct gendisk *disk; /* blkdev's gendisk and rq */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700329
Alex Eldera30b71b2012-07-10 20:30:11 -0500330 u32 image_format; /* Either 1 or 2 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700331 struct rbd_client *rbd_client;
332
333 char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */
334
Alex Elderb82d1672013-01-14 12:43:31 -0600335 spinlock_t lock; /* queue, flags, open_count */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700336
337 struct rbd_image_header header;
Alex Elderb82d1672013-01-14 12:43:31 -0600338 unsigned long flags; /* possibly lock protected */
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500339 struct rbd_spec *spec;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700340
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500341 char *header_name;
Alex Elder971f8392012-10-25 23:34:41 -0500342
Alex Elder0903e872012-11-14 12:25:19 -0600343 struct ceph_file_layout layout;
344
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700345 struct ceph_osd_event *watch_event;
Alex Elder975241a2013-01-25 17:08:55 -0600346 struct rbd_obj_request *watch_request;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700347
Alex Elder86b00e02012-10-25 23:34:42 -0500348 struct rbd_spec *parent_spec;
349 u64 parent_overlap;
Alex Eldera2acd002013-05-08 22:50:04 -0500350 atomic_t parent_ref;
Alex Elder2f82ee52012-10-30 19:40:33 -0500351 struct rbd_device *parent;
Alex Elder86b00e02012-10-25 23:34:42 -0500352
Josh Durginc6666012011-11-21 17:11:12 -0800353 /* protects updating the header */
354 struct rw_semaphore header_rwsem;
Alex Elderf84344f2012-08-31 17:29:51 -0500355
356 struct rbd_mapping mapping;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700357
358 struct list_head node;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800359
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800360 /* sysfs related */
361 struct device dev;
Alex Elderb82d1672013-01-14 12:43:31 -0600362 unsigned long open_count; /* protected by lock */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800363};
364
Alex Elderb82d1672013-01-14 12:43:31 -0600365/*
366 * Flag bits for rbd_dev->flags. If atomicity is required,
367 * rbd_dev->lock is used to protect access.
368 *
369 * Currently, only the "removing" flag (which is coupled with the
370 * "open_count" field) requires atomic access.
371 */
Alex Elder6d292902013-01-14 12:43:31 -0600372enum rbd_dev_flags {
373 RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */
Alex Elderb82d1672013-01-14 12:43:31 -0600374 RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */
Alex Elder6d292902013-01-14 12:43:31 -0600375};
376
Alex Eldercfbf6372013-05-31 17:40:45 -0500377static DEFINE_MUTEX(client_mutex); /* Serialize client creation */
Alex Eldere124a82f2012-01-29 13:57:44 -0600378
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700379static LIST_HEAD(rbd_dev_list); /* devices */
Alex Eldere124a82f2012-01-29 13:57:44 -0600380static DEFINE_SPINLOCK(rbd_dev_list_lock);
381
Alex Elder432b8582012-01-29 13:57:44 -0600382static LIST_HEAD(rbd_client_list); /* clients */
383static DEFINE_SPINLOCK(rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700384
Alex Elder78c2a442013-05-01 12:43:04 -0500385/* Slab caches for frequently-allocated structures */
386
Alex Elder1c2a9df2013-05-01 12:43:03 -0500387static struct kmem_cache *rbd_img_request_cache;
Alex Elder868311b2013-05-01 12:43:03 -0500388static struct kmem_cache *rbd_obj_request_cache;
Alex Elder78c2a442013-05-01 12:43:04 -0500389static struct kmem_cache *rbd_segment_name_cache;
Alex Elder1c2a9df2013-05-01 12:43:03 -0500390
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200391static int rbd_major;
Ilya Dryomovf8a22fc2013-12-13 15:28:57 +0200392static DEFINE_IDA(rbd_dev_id_ida);
393
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200394/*
395 * Default to false for now, as single-major requires >= 0.75 version of
396 * userspace rbd utility.
397 */
398static bool single_major = false;
399module_param(single_major, bool, S_IRUGO);
400MODULE_PARM_DESC(single_major, "Use a single major number for all rbd devices (default: false)");
401
Alex Elder3d7efd12013-04-19 15:34:50 -0500402static int rbd_img_request_submit(struct rbd_img_request *img_request);
403
Alex Elder200a6a82013-04-28 23:32:34 -0500404static void rbd_dev_device_release(struct device *dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800405
Alex Elderf0f8cef2012-01-29 13:57:44 -0600406static ssize_t rbd_add(struct bus_type *bus, const char *buf,
407 size_t count);
408static ssize_t rbd_remove(struct bus_type *bus, const char *buf,
409 size_t count);
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200410static ssize_t rbd_add_single_major(struct bus_type *bus, const char *buf,
411 size_t count);
412static ssize_t rbd_remove_single_major(struct bus_type *bus, const char *buf,
413 size_t count);
Alex Elder1f3ef782013-05-06 17:40:33 -0500414static int rbd_dev_image_probe(struct rbd_device *rbd_dev, bool mapping);
Alex Eldera2acd002013-05-08 22:50:04 -0500415static void rbd_spec_put(struct rbd_spec *spec);
Alex Elderf0f8cef2012-01-29 13:57:44 -0600416
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200417static int rbd_dev_id_to_minor(int dev_id)
418{
Ilya Dryomov7e513d42013-12-16 19:26:32 +0200419 return dev_id << RBD_SINGLE_MAJOR_PART_SHIFT;
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200420}
421
422static int minor_to_rbd_dev_id(int minor)
423{
Ilya Dryomov7e513d42013-12-16 19:26:32 +0200424 return minor >> RBD_SINGLE_MAJOR_PART_SHIFT;
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200425}
426
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700427static BUS_ATTR(add, S_IWUSR, NULL, rbd_add);
428static BUS_ATTR(remove, S_IWUSR, NULL, rbd_remove);
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200429static BUS_ATTR(add_single_major, S_IWUSR, NULL, rbd_add_single_major);
430static BUS_ATTR(remove_single_major, S_IWUSR, NULL, rbd_remove_single_major);
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700431
432static struct attribute *rbd_bus_attrs[] = {
433 &bus_attr_add.attr,
434 &bus_attr_remove.attr,
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200435 &bus_attr_add_single_major.attr,
436 &bus_attr_remove_single_major.attr,
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700437 NULL,
Alex Elderf0f8cef2012-01-29 13:57:44 -0600438};
Ilya Dryomov92c76dc2013-12-13 15:28:57 +0200439
440static umode_t rbd_bus_is_visible(struct kobject *kobj,
441 struct attribute *attr, int index)
442{
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200443 if (!single_major &&
444 (attr == &bus_attr_add_single_major.attr ||
445 attr == &bus_attr_remove_single_major.attr))
446 return 0;
447
Ilya Dryomov92c76dc2013-12-13 15:28:57 +0200448 return attr->mode;
449}
450
451static const struct attribute_group rbd_bus_group = {
452 .attrs = rbd_bus_attrs,
453 .is_visible = rbd_bus_is_visible,
454};
455__ATTRIBUTE_GROUPS(rbd_bus);
Alex Elderf0f8cef2012-01-29 13:57:44 -0600456
457static struct bus_type rbd_bus_type = {
458 .name = "rbd",
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700459 .bus_groups = rbd_bus_groups,
Alex Elderf0f8cef2012-01-29 13:57:44 -0600460};
461
462static void rbd_root_dev_release(struct device *dev)
463{
464}
465
466static struct device rbd_root_dev = {
467 .init_name = "rbd",
468 .release = rbd_root_dev_release,
469};
470
Alex Elder06ecc6c2012-11-01 10:17:15 -0500471static __printf(2, 3)
472void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...)
473{
474 struct va_format vaf;
475 va_list args;
476
477 va_start(args, fmt);
478 vaf.fmt = fmt;
479 vaf.va = &args;
480
481 if (!rbd_dev)
482 printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf);
483 else if (rbd_dev->disk)
484 printk(KERN_WARNING "%s: %s: %pV\n",
485 RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf);
486 else if (rbd_dev->spec && rbd_dev->spec->image_name)
487 printk(KERN_WARNING "%s: image %s: %pV\n",
488 RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf);
489 else if (rbd_dev->spec && rbd_dev->spec->image_id)
490 printk(KERN_WARNING "%s: id %s: %pV\n",
491 RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf);
492 else /* punt */
493 printk(KERN_WARNING "%s: rbd_dev %p: %pV\n",
494 RBD_DRV_NAME, rbd_dev, &vaf);
495 va_end(args);
496}
497
Alex Elderaafb2302012-09-06 16:00:54 -0500498#ifdef RBD_DEBUG
499#define rbd_assert(expr) \
500 if (unlikely(!(expr))) { \
501 printk(KERN_ERR "\nAssertion failure in %s() " \
502 "at line %d:\n\n" \
503 "\trbd_assert(%s);\n\n", \
504 __func__, __LINE__, #expr); \
505 BUG(); \
506 }
507#else /* !RBD_DEBUG */
508# define rbd_assert(expr) ((void) 0)
509#endif /* !RBD_DEBUG */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800510
Alex Elderb454e362013-04-19 15:34:50 -0500511static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request);
Alex Elder05a46af2013-04-26 15:44:36 -0500512static void rbd_img_parent_read(struct rbd_obj_request *obj_request);
513static void rbd_dev_remove_parent(struct rbd_device *rbd_dev);
Alex Elder8b3e1a52013-01-24 16:13:36 -0600514
Alex Eldercc4a38bd2013-04-30 00:44:33 -0500515static int rbd_dev_refresh(struct rbd_device *rbd_dev);
Alex Elder2df3fac2013-05-06 09:51:30 -0500516static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev);
517static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev);
Alex Elder54cac612013-04-30 00:44:33 -0500518static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
519 u64 snap_id);
Alex Elder2ad3d712013-04-30 00:44:33 -0500520static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
521 u8 *order, u64 *snap_size);
522static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
523 u64 *snap_features);
524static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700525
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700526static int rbd_open(struct block_device *bdev, fmode_t mode)
527{
Alex Elderf0f8cef2012-01-29 13:57:44 -0600528 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
Alex Elderb82d1672013-01-14 12:43:31 -0600529 bool removing = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700530
Alex Elderf84344f2012-08-31 17:29:51 -0500531 if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700532 return -EROFS;
533
Alex Eldera14ea262013-02-05 13:23:12 -0600534 spin_lock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600535 if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags))
536 removing = true;
537 else
538 rbd_dev->open_count++;
Alex Eldera14ea262013-02-05 13:23:12 -0600539 spin_unlock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600540 if (removing)
541 return -ENOENT;
542
Alex Elderc3e946c2012-11-16 09:29:16 -0600543 (void) get_device(&rbd_dev->dev);
Alex Elderf84344f2012-08-31 17:29:51 -0500544 set_device_ro(bdev, rbd_dev->mapping.read_only);
Alex Elder340c7a22012-08-10 13:12:07 -0700545
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700546 return 0;
547}
548
Al Virodb2a1442013-05-05 21:52:57 -0400549static void rbd_release(struct gendisk *disk, fmode_t mode)
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800550{
551 struct rbd_device *rbd_dev = disk->private_data;
Alex Elderb82d1672013-01-14 12:43:31 -0600552 unsigned long open_count_before;
553
Alex Eldera14ea262013-02-05 13:23:12 -0600554 spin_lock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600555 open_count_before = rbd_dev->open_count--;
Alex Eldera14ea262013-02-05 13:23:12 -0600556 spin_unlock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600557 rbd_assert(open_count_before > 0);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800558
Alex Elderc3e946c2012-11-16 09:29:16 -0600559 put_device(&rbd_dev->dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800560}
561
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700562static const struct block_device_operations rbd_bd_ops = {
563 .owner = THIS_MODULE,
564 .open = rbd_open,
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800565 .release = rbd_release,
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700566};
567
568/*
Alex Elder7262cfc2013-05-16 15:04:20 -0500569 * Initialize an rbd client instance. Success or not, this function
Alex Eldercfbf6372013-05-31 17:40:45 -0500570 * consumes ceph_opts. Caller holds client_mutex.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700571 */
Alex Elderf8c38922012-08-10 13:12:07 -0700572static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700573{
574 struct rbd_client *rbdc;
575 int ret = -ENOMEM;
576
Alex Elder37206ee2013-02-20 17:32:08 -0600577 dout("%s:\n", __func__);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700578 rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL);
579 if (!rbdc)
580 goto out_opt;
581
582 kref_init(&rbdc->kref);
583 INIT_LIST_HEAD(&rbdc->node);
584
Alex Elder43ae4702012-07-03 16:01:18 -0500585 rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700586 if (IS_ERR(rbdc->client))
Alex Elder08f75462013-05-29 11:19:00 -0500587 goto out_rbdc;
Alex Elder43ae4702012-07-03 16:01:18 -0500588 ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700589
590 ret = ceph_open_session(rbdc->client);
591 if (ret < 0)
Alex Elder08f75462013-05-29 11:19:00 -0500592 goto out_client;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700593
Alex Elder432b8582012-01-29 13:57:44 -0600594 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700595 list_add_tail(&rbdc->node, &rbd_client_list);
Alex Elder432b8582012-01-29 13:57:44 -0600596 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700597
Alex Elder37206ee2013-02-20 17:32:08 -0600598 dout("%s: rbdc %p\n", __func__, rbdc);
Alex Elderbc534d82012-01-29 13:57:44 -0600599
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700600 return rbdc;
Alex Elder08f75462013-05-29 11:19:00 -0500601out_client:
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700602 ceph_destroy_client(rbdc->client);
Alex Elder08f75462013-05-29 11:19:00 -0500603out_rbdc:
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700604 kfree(rbdc);
605out_opt:
Alex Elder43ae4702012-07-03 16:01:18 -0500606 if (ceph_opts)
607 ceph_destroy_options(ceph_opts);
Alex Elder37206ee2013-02-20 17:32:08 -0600608 dout("%s: error %d\n", __func__, ret);
609
Vasiliy Kulikov28f259b2010-09-26 12:59:37 +0400610 return ERR_PTR(ret);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700611}
612
Alex Elder2f82ee52012-10-30 19:40:33 -0500613static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc)
614{
615 kref_get(&rbdc->kref);
616
617 return rbdc;
618}
619
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700620/*
Alex Elder1f7ba332012-08-10 13:12:07 -0700621 * Find a ceph client with specific addr and configuration. If
622 * found, bump its reference count.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700623 */
Alex Elder1f7ba332012-08-10 13:12:07 -0700624static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700625{
626 struct rbd_client *client_node;
Alex Elder1f7ba332012-08-10 13:12:07 -0700627 bool found = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700628
Alex Elder43ae4702012-07-03 16:01:18 -0500629 if (ceph_opts->flags & CEPH_OPT_NOSHARE)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700630 return NULL;
631
Alex Elder1f7ba332012-08-10 13:12:07 -0700632 spin_lock(&rbd_client_list_lock);
633 list_for_each_entry(client_node, &rbd_client_list, node) {
634 if (!ceph_compare_options(ceph_opts, client_node->client)) {
Alex Elder2f82ee52012-10-30 19:40:33 -0500635 __rbd_get_client(client_node);
636
Alex Elder1f7ba332012-08-10 13:12:07 -0700637 found = true;
638 break;
639 }
640 }
641 spin_unlock(&rbd_client_list_lock);
642
643 return found ? client_node : NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700644}
645
646/*
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700647 * mount options
648 */
649enum {
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700650 Opt_last_int,
651 /* int args above */
652 Opt_last_string,
653 /* string args above */
Alex Eldercc0538b2012-08-10 13:12:07 -0700654 Opt_read_only,
655 Opt_read_write,
656 /* Boolean args above */
657 Opt_last_bool,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700658};
659
Alex Elder43ae4702012-07-03 16:01:18 -0500660static match_table_t rbd_opts_tokens = {
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700661 /* int args above */
662 /* string args above */
Alex Elderbe466c12012-10-22 11:31:26 -0500663 {Opt_read_only, "read_only"},
Alex Eldercc0538b2012-08-10 13:12:07 -0700664 {Opt_read_only, "ro"}, /* Alternate spelling */
665 {Opt_read_write, "read_write"},
666 {Opt_read_write, "rw"}, /* Alternate spelling */
667 /* Boolean args above */
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700668 {-1, NULL}
669};
670
Alex Elder98571b52013-01-20 14:44:42 -0600671struct rbd_options {
672 bool read_only;
673};
674
675#define RBD_READ_ONLY_DEFAULT false
676
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700677static int parse_rbd_opts_token(char *c, void *private)
678{
Alex Elder43ae4702012-07-03 16:01:18 -0500679 struct rbd_options *rbd_opts = private;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700680 substring_t argstr[MAX_OPT_ARGS];
681 int token, intval, ret;
682
Alex Elder43ae4702012-07-03 16:01:18 -0500683 token = match_token(c, rbd_opts_tokens, argstr);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700684 if (token < 0)
685 return -EINVAL;
686
687 if (token < Opt_last_int) {
688 ret = match_int(&argstr[0], &intval);
689 if (ret < 0) {
690 pr_err("bad mount option arg (not int) "
691 "at '%s'\n", c);
692 return ret;
693 }
694 dout("got int token %d val %d\n", token, intval);
695 } else if (token > Opt_last_int && token < Opt_last_string) {
696 dout("got string token %d val %s\n", token,
697 argstr[0].from);
Alex Eldercc0538b2012-08-10 13:12:07 -0700698 } else if (token > Opt_last_string && token < Opt_last_bool) {
699 dout("got Boolean token %d\n", token);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700700 } else {
701 dout("got token %d\n", token);
702 }
703
704 switch (token) {
Alex Eldercc0538b2012-08-10 13:12:07 -0700705 case Opt_read_only:
706 rbd_opts->read_only = true;
707 break;
708 case Opt_read_write:
709 rbd_opts->read_only = false;
710 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700711 default:
Alex Elderaafb2302012-09-06 16:00:54 -0500712 rbd_assert(false);
713 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700714 }
715 return 0;
716}
717
718/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700719 * Get a ceph client with specific addr and configuration, if one does
Alex Elder7262cfc2013-05-16 15:04:20 -0500720 * not exist create it. Either way, ceph_opts is consumed by this
721 * function.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700722 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500723static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700724{
Alex Elderf8c38922012-08-10 13:12:07 -0700725 struct rbd_client *rbdc;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700726
Alex Eldercfbf6372013-05-31 17:40:45 -0500727 mutex_lock_nested(&client_mutex, SINGLE_DEPTH_NESTING);
Alex Elder1f7ba332012-08-10 13:12:07 -0700728 rbdc = rbd_client_find(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500729 if (rbdc) /* using an existing client */
Alex Elder43ae4702012-07-03 16:01:18 -0500730 ceph_destroy_options(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500731 else
Alex Elderf8c38922012-08-10 13:12:07 -0700732 rbdc = rbd_client_create(ceph_opts);
Alex Eldercfbf6372013-05-31 17:40:45 -0500733 mutex_unlock(&client_mutex);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700734
Alex Elder9d3997f2012-10-25 23:34:42 -0500735 return rbdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700736}
737
738/*
739 * Destroy ceph client
Alex Elderd23a4b32012-01-29 13:57:43 -0600740 *
Alex Elder432b8582012-01-29 13:57:44 -0600741 * Caller must hold rbd_client_list_lock.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700742 */
743static void rbd_client_release(struct kref *kref)
744{
745 struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
746
Alex Elder37206ee2013-02-20 17:32:08 -0600747 dout("%s: rbdc %p\n", __func__, rbdc);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500748 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700749 list_del(&rbdc->node);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500750 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700751
752 ceph_destroy_client(rbdc->client);
753 kfree(rbdc);
754}
755
756/*
757 * Drop reference to ceph client node. If it's not referenced anymore, release
758 * it.
759 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500760static void rbd_put_client(struct rbd_client *rbdc)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700761{
Alex Elderc53d5892012-10-25 23:34:42 -0500762 if (rbdc)
763 kref_put(&rbdc->kref, rbd_client_release);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700764}
765
Alex Eldera30b71b2012-07-10 20:30:11 -0500766static bool rbd_image_format_valid(u32 image_format)
767{
768 return image_format == 1 || image_format == 2;
769}
770
Alex Elder8e94af82012-07-25 09:32:40 -0500771static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
772{
Alex Elder103a1502012-08-02 11:29:45 -0500773 size_t size;
774 u32 snap_count;
775
776 /* The header has to start with the magic rbd header text */
777 if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT)))
778 return false;
779
Alex Elderdb2388b2012-10-20 22:17:27 -0500780 /* The bio layer requires at least sector-sized I/O */
781
782 if (ondisk->options.order < SECTOR_SHIFT)
783 return false;
784
785 /* If we use u64 in a few spots we may be able to loosen this */
786
787 if (ondisk->options.order > 8 * sizeof (int) - 1)
788 return false;
789
Alex Elder103a1502012-08-02 11:29:45 -0500790 /*
791 * The size of a snapshot header has to fit in a size_t, and
792 * that limits the number of snapshots.
793 */
794 snap_count = le32_to_cpu(ondisk->snap_count);
795 size = SIZE_MAX - sizeof (struct ceph_snap_context);
796 if (snap_count > size / sizeof (__le64))
797 return false;
798
799 /*
800 * Not only that, but the size of the entire the snapshot
801 * header must also be representable in a size_t.
802 */
803 size -= snap_count * sizeof (__le64);
804 if ((u64) size < le64_to_cpu(ondisk->snap_names_len))
805 return false;
806
807 return true;
Alex Elder8e94af82012-07-25 09:32:40 -0500808}
809
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700810/*
Alex Elderbb23e372013-05-06 09:51:29 -0500811 * Fill an rbd image header with information from the given format 1
812 * on-disk header.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700813 */
Alex Elder662518b2013-05-06 09:51:29 -0500814static int rbd_header_from_disk(struct rbd_device *rbd_dev,
Alex Elder4156d992012-08-02 11:29:46 -0500815 struct rbd_image_header_ondisk *ondisk)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700816{
Alex Elder662518b2013-05-06 09:51:29 -0500817 struct rbd_image_header *header = &rbd_dev->header;
Alex Elderbb23e372013-05-06 09:51:29 -0500818 bool first_time = header->object_prefix == NULL;
819 struct ceph_snap_context *snapc;
820 char *object_prefix = NULL;
821 char *snap_names = NULL;
822 u64 *snap_sizes = NULL;
Alex Elderccece232012-07-10 20:30:10 -0500823 u32 snap_count;
Alex Elderd2bb24e2012-07-26 23:37:14 -0500824 size_t size;
Alex Elderbb23e372013-05-06 09:51:29 -0500825 int ret = -ENOMEM;
Alex Elder621901d2012-08-23 23:22:06 -0500826 u32 i;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700827
Alex Elderbb23e372013-05-06 09:51:29 -0500828 /* Allocate this now to avoid having to handle failure below */
829
830 if (first_time) {
831 size_t len;
832
833 len = strnlen(ondisk->object_prefix,
834 sizeof (ondisk->object_prefix));
835 object_prefix = kmalloc(len + 1, GFP_KERNEL);
836 if (!object_prefix)
837 return -ENOMEM;
838 memcpy(object_prefix, ondisk->object_prefix, len);
839 object_prefix[len] = '\0';
840 }
841
842 /* Allocate the snapshot context and fill it in */
Alex Elder6a523252012-07-19 17:12:59 -0500843
Alex Elder103a1502012-08-02 11:29:45 -0500844 snap_count = le32_to_cpu(ondisk->snap_count);
Alex Elderbb23e372013-05-06 09:51:29 -0500845 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
846 if (!snapc)
847 goto out_err;
848 snapc->seq = le64_to_cpu(ondisk->snap_seq);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700849 if (snap_count) {
Alex Elderbb23e372013-05-06 09:51:29 -0500850 struct rbd_image_snap_ondisk *snaps;
Alex Elderf785cc12012-08-23 23:22:06 -0500851 u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len);
852
Alex Elderbb23e372013-05-06 09:51:29 -0500853 /* We'll keep a copy of the snapshot names... */
Alex Elder621901d2012-08-23 23:22:06 -0500854
Alex Elderbb23e372013-05-06 09:51:29 -0500855 if (snap_names_len > (u64)SIZE_MAX)
856 goto out_2big;
857 snap_names = kmalloc(snap_names_len, GFP_KERNEL);
858 if (!snap_names)
Alex Elder6a523252012-07-19 17:12:59 -0500859 goto out_err;
Alex Elderbb23e372013-05-06 09:51:29 -0500860
861 /* ...as well as the array of their sizes. */
862
863 size = snap_count * sizeof (*header->snap_sizes);
864 snap_sizes = kmalloc(size, GFP_KERNEL);
865 if (!snap_sizes)
866 goto out_err;
867
Alex Elderf785cc12012-08-23 23:22:06 -0500868 /*
Alex Elderbb23e372013-05-06 09:51:29 -0500869 * Copy the names, and fill in each snapshot's id
870 * and size.
871 *
Alex Elder99a41eb2013-05-06 09:51:30 -0500872 * Note that rbd_dev_v1_header_info() guarantees the
Alex Elderbb23e372013-05-06 09:51:29 -0500873 * ondisk buffer we're working with has
Alex Elderf785cc12012-08-23 23:22:06 -0500874 * snap_names_len bytes beyond the end of the
875 * snapshot id array, this memcpy() is safe.
876 */
Alex Elderbb23e372013-05-06 09:51:29 -0500877 memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len);
878 snaps = ondisk->snaps;
879 for (i = 0; i < snap_count; i++) {
880 snapc->snaps[i] = le64_to_cpu(snaps[i].id);
881 snap_sizes[i] = le64_to_cpu(snaps[i].image_size);
882 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700883 }
Alex Elder849b4262012-07-09 21:04:24 -0500884
Alex Elderbb23e372013-05-06 09:51:29 -0500885 /* We won't fail any more, fill in the header */
Alex Elder6a523252012-07-19 17:12:59 -0500886
Alex Elderbb23e372013-05-06 09:51:29 -0500887 if (first_time) {
888 header->object_prefix = object_prefix;
889 header->obj_order = ondisk->options.order;
890 header->crypt_type = ondisk->options.crypt_type;
891 header->comp_type = ondisk->options.comp_type;
892 /* The rest aren't used for format 1 images */
893 header->stripe_unit = 0;
894 header->stripe_count = 0;
895 header->features = 0;
Alex Elder662518b2013-05-06 09:51:29 -0500896 } else {
897 ceph_put_snap_context(header->snapc);
898 kfree(header->snap_names);
899 kfree(header->snap_sizes);
Alex Elderbb23e372013-05-06 09:51:29 -0500900 }
901
902 /* The remaining fields always get updated (when we refresh) */
Alex Elder621901d2012-08-23 23:22:06 -0500903
Alex Elderf84344f2012-08-31 17:29:51 -0500904 header->image_size = le64_to_cpu(ondisk->image_size);
Alex Elderbb23e372013-05-06 09:51:29 -0500905 header->snapc = snapc;
906 header->snap_names = snap_names;
907 header->snap_sizes = snap_sizes;
Alex Elder468521c2013-04-26 09:43:47 -0500908
Alex Elder662518b2013-05-06 09:51:29 -0500909 /* Make sure mapping size is consistent with header info */
910
911 if (rbd_dev->spec->snap_id == CEPH_NOSNAP || first_time)
912 if (rbd_dev->mapping.size != header->image_size)
913 rbd_dev->mapping.size = header->image_size;
914
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700915 return 0;
Alex Elderbb23e372013-05-06 09:51:29 -0500916out_2big:
917 ret = -EIO;
Alex Elder6a523252012-07-19 17:12:59 -0500918out_err:
Alex Elderbb23e372013-05-06 09:51:29 -0500919 kfree(snap_sizes);
920 kfree(snap_names);
921 ceph_put_snap_context(snapc);
922 kfree(object_prefix);
Alex Elderccece232012-07-10 20:30:10 -0500923
Alex Elderbb23e372013-05-06 09:51:29 -0500924 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700925}
926
Alex Elder9682fc62013-04-30 00:44:33 -0500927static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which)
928{
929 const char *snap_name;
930
931 rbd_assert(which < rbd_dev->header.snapc->num_snaps);
932
933 /* Skip over names until we find the one we are looking for */
934
935 snap_name = rbd_dev->header.snap_names;
936 while (which--)
937 snap_name += strlen(snap_name) + 1;
938
939 return kstrdup(snap_name, GFP_KERNEL);
940}
941
Alex Elder30d1cff2013-05-01 12:43:03 -0500942/*
943 * Snapshot id comparison function for use with qsort()/bsearch().
944 * Note that result is for snapshots in *descending* order.
945 */
946static int snapid_compare_reverse(const void *s1, const void *s2)
947{
948 u64 snap_id1 = *(u64 *)s1;
949 u64 snap_id2 = *(u64 *)s2;
950
951 if (snap_id1 < snap_id2)
952 return 1;
953 return snap_id1 == snap_id2 ? 0 : -1;
954}
955
956/*
957 * Search a snapshot context to see if the given snapshot id is
958 * present.
959 *
960 * Returns the position of the snapshot id in the array if it's found,
961 * or BAD_SNAP_INDEX otherwise.
962 *
963 * Note: The snapshot array is in kept sorted (by the osd) in
964 * reverse order, highest snapshot id first.
965 */
Alex Elder9682fc62013-04-30 00:44:33 -0500966static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id)
967{
968 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
Alex Elder30d1cff2013-05-01 12:43:03 -0500969 u64 *found;
Alex Elder9682fc62013-04-30 00:44:33 -0500970
Alex Elder30d1cff2013-05-01 12:43:03 -0500971 found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps,
972 sizeof (snap_id), snapid_compare_reverse);
Alex Elder9682fc62013-04-30 00:44:33 -0500973
Alex Elder30d1cff2013-05-01 12:43:03 -0500974 return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX;
Alex Elder9682fc62013-04-30 00:44:33 -0500975}
976
Alex Elder2ad3d712013-04-30 00:44:33 -0500977static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev,
978 u64 snap_id)
Alex Elder54cac612013-04-30 00:44:33 -0500979{
980 u32 which;
Josh Durginda6a6b62013-09-04 17:57:31 -0700981 const char *snap_name;
Alex Elder54cac612013-04-30 00:44:33 -0500982
983 which = rbd_dev_snap_index(rbd_dev, snap_id);
984 if (which == BAD_SNAP_INDEX)
Josh Durginda6a6b62013-09-04 17:57:31 -0700985 return ERR_PTR(-ENOENT);
Alex Elder54cac612013-04-30 00:44:33 -0500986
Josh Durginda6a6b62013-09-04 17:57:31 -0700987 snap_name = _rbd_dev_v1_snap_name(rbd_dev, which);
988 return snap_name ? snap_name : ERR_PTR(-ENOMEM);
Alex Elder54cac612013-04-30 00:44:33 -0500989}
990
Alex Elder9e15b772012-10-30 19:40:33 -0500991static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id)
992{
Alex Elder9e15b772012-10-30 19:40:33 -0500993 if (snap_id == CEPH_NOSNAP)
994 return RBD_SNAP_HEAD_NAME;
995
Alex Elder54cac612013-04-30 00:44:33 -0500996 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
997 if (rbd_dev->image_format == 1)
998 return rbd_dev_v1_snap_name(rbd_dev, snap_id);
Alex Elder9e15b772012-10-30 19:40:33 -0500999
Alex Elder54cac612013-04-30 00:44:33 -05001000 return rbd_dev_v2_snap_name(rbd_dev, snap_id);
Alex Elder9e15b772012-10-30 19:40:33 -05001001}
1002
Alex Elder2ad3d712013-04-30 00:44:33 -05001003static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
1004 u64 *snap_size)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001005{
Alex Elder2ad3d712013-04-30 00:44:33 -05001006 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1007 if (snap_id == CEPH_NOSNAP) {
1008 *snap_size = rbd_dev->header.image_size;
1009 } else if (rbd_dev->image_format == 1) {
1010 u32 which;
Alex Elder00f1f362012-02-07 12:03:36 -06001011
Alex Elder2ad3d712013-04-30 00:44:33 -05001012 which = rbd_dev_snap_index(rbd_dev, snap_id);
1013 if (which == BAD_SNAP_INDEX)
1014 return -ENOENT;
Alex Elder00f1f362012-02-07 12:03:36 -06001015
Alex Elder2ad3d712013-04-30 00:44:33 -05001016 *snap_size = rbd_dev->header.snap_sizes[which];
1017 } else {
1018 u64 size = 0;
1019 int ret;
1020
1021 ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size);
1022 if (ret)
1023 return ret;
1024
1025 *snap_size = size;
1026 }
1027 return 0;
1028}
1029
1030static int rbd_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
1031 u64 *snap_features)
1032{
1033 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1034 if (snap_id == CEPH_NOSNAP) {
1035 *snap_features = rbd_dev->header.features;
1036 } else if (rbd_dev->image_format == 1) {
1037 *snap_features = 0; /* No features for format 1 */
1038 } else {
1039 u64 features = 0;
1040 int ret;
1041
1042 ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, &features);
1043 if (ret)
1044 return ret;
1045
1046 *snap_features = features;
1047 }
1048 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001049}
1050
Alex Elderd1cf5782013-04-27 09:59:30 -05001051static int rbd_dev_mapping_set(struct rbd_device *rbd_dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001052{
Alex Elder8f4b7d92013-05-06 07:40:30 -05001053 u64 snap_id = rbd_dev->spec->snap_id;
Alex Elder2ad3d712013-04-30 00:44:33 -05001054 u64 size = 0;
1055 u64 features = 0;
1056 int ret;
Alex Elder8b0241f2013-04-25 23:15:08 -05001057
Alex Elder2ad3d712013-04-30 00:44:33 -05001058 ret = rbd_snap_size(rbd_dev, snap_id, &size);
1059 if (ret)
1060 return ret;
1061 ret = rbd_snap_features(rbd_dev, snap_id, &features);
1062 if (ret)
1063 return ret;
1064
1065 rbd_dev->mapping.size = size;
1066 rbd_dev->mapping.features = features;
1067
Alex Elder8b0241f2013-04-25 23:15:08 -05001068 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001069}
1070
Alex Elderd1cf5782013-04-27 09:59:30 -05001071static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev)
1072{
1073 rbd_dev->mapping.size = 0;
1074 rbd_dev->mapping.features = 0;
Alex Elder200a6a82013-04-28 23:32:34 -05001075}
1076
Alex Elder98571b52013-01-20 14:44:42 -06001077static const char *rbd_segment_name(struct rbd_device *rbd_dev, u64 offset)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001078{
Alex Elder65ccfe22012-08-09 10:33:26 -07001079 char *name;
1080 u64 segment;
1081 int ret;
Josh Durgin3a96d5c2013-06-12 19:15:06 -07001082 char *name_format;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001083
Alex Elder78c2a442013-05-01 12:43:04 -05001084 name = kmem_cache_alloc(rbd_segment_name_cache, GFP_NOIO);
Alex Elder65ccfe22012-08-09 10:33:26 -07001085 if (!name)
1086 return NULL;
1087 segment = offset >> rbd_dev->header.obj_order;
Josh Durgin3a96d5c2013-06-12 19:15:06 -07001088 name_format = "%s.%012llx";
1089 if (rbd_dev->image_format == 2)
1090 name_format = "%s.%016llx";
Ilya Dryomov2d0ebc52014-01-27 17:40:18 +02001091 ret = snprintf(name, CEPH_MAX_OID_NAME_LEN + 1, name_format,
Alex Elder65ccfe22012-08-09 10:33:26 -07001092 rbd_dev->header.object_prefix, segment);
Ilya Dryomov2d0ebc52014-01-27 17:40:18 +02001093 if (ret < 0 || ret > CEPH_MAX_OID_NAME_LEN) {
Alex Elder65ccfe22012-08-09 10:33:26 -07001094 pr_err("error formatting segment name for #%llu (%d)\n",
1095 segment, ret);
1096 kfree(name);
1097 name = NULL;
1098 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001099
Alex Elder65ccfe22012-08-09 10:33:26 -07001100 return name;
1101}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001102
Alex Elder78c2a442013-05-01 12:43:04 -05001103static void rbd_segment_name_free(const char *name)
1104{
1105 /* The explicit cast here is needed to drop the const qualifier */
1106
1107 kmem_cache_free(rbd_segment_name_cache, (void *)name);
1108}
1109
Alex Elder65ccfe22012-08-09 10:33:26 -07001110static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset)
1111{
1112 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001113
Alex Elder65ccfe22012-08-09 10:33:26 -07001114 return offset & (segment_size - 1);
1115}
1116
1117static u64 rbd_segment_length(struct rbd_device *rbd_dev,
1118 u64 offset, u64 length)
1119{
1120 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
1121
1122 offset &= segment_size - 1;
1123
Alex Elderaafb2302012-09-06 16:00:54 -05001124 rbd_assert(length <= U64_MAX - offset);
Alex Elder65ccfe22012-08-09 10:33:26 -07001125 if (offset + length > segment_size)
1126 length = segment_size - offset;
1127
1128 return length;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001129}
1130
1131/*
Josh Durgin029bcbd2011-07-22 11:35:23 -07001132 * returns the size of an object in the image
1133 */
1134static u64 rbd_obj_bytes(struct rbd_image_header *header)
1135{
1136 return 1 << header->obj_order;
1137}
1138
1139/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001140 * bio helpers
1141 */
1142
1143static void bio_chain_put(struct bio *chain)
1144{
1145 struct bio *tmp;
1146
1147 while (chain) {
1148 tmp = chain;
1149 chain = chain->bi_next;
1150 bio_put(tmp);
1151 }
1152}
1153
1154/*
1155 * zeros a bio chain, starting at specific offset
1156 */
1157static void zero_bio_chain(struct bio *chain, int start_ofs)
1158{
1159 struct bio_vec *bv;
1160 unsigned long flags;
1161 void *buf;
1162 int i;
1163 int pos = 0;
1164
1165 while (chain) {
1166 bio_for_each_segment(bv, chain, i) {
1167 if (pos + bv->bv_len > start_ofs) {
1168 int remainder = max(start_ofs - pos, 0);
1169 buf = bvec_kmap_irq(bv, &flags);
1170 memset(buf + remainder, 0,
1171 bv->bv_len - remainder);
Alex Eldere2156052013-05-22 20:54:25 -05001172 flush_dcache_page(bv->bv_page);
Dan Carpenter85b5aaa2010-10-11 21:15:11 +02001173 bvec_kunmap_irq(buf, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001174 }
1175 pos += bv->bv_len;
1176 }
1177
1178 chain = chain->bi_next;
1179 }
1180}
1181
1182/*
Alex Elderb9434c52013-04-19 15:34:50 -05001183 * similar to zero_bio_chain(), zeros data defined by a page array,
1184 * starting at the given byte offset from the start of the array and
1185 * continuing up to the given end offset. The pages array is
1186 * assumed to be big enough to hold all bytes up to the end.
1187 */
1188static void zero_pages(struct page **pages, u64 offset, u64 end)
1189{
1190 struct page **page = &pages[offset >> PAGE_SHIFT];
1191
1192 rbd_assert(end > offset);
1193 rbd_assert(end - offset <= (u64)SIZE_MAX);
1194 while (offset < end) {
1195 size_t page_offset;
1196 size_t length;
1197 unsigned long flags;
1198 void *kaddr;
1199
Geert Uytterhoeven491205a2013-05-13 20:35:37 -05001200 page_offset = offset & ~PAGE_MASK;
1201 length = min_t(size_t, PAGE_SIZE - page_offset, end - offset);
Alex Elderb9434c52013-04-19 15:34:50 -05001202 local_irq_save(flags);
1203 kaddr = kmap_atomic(*page);
1204 memset(kaddr + page_offset, 0, length);
Alex Eldere2156052013-05-22 20:54:25 -05001205 flush_dcache_page(*page);
Alex Elderb9434c52013-04-19 15:34:50 -05001206 kunmap_atomic(kaddr);
1207 local_irq_restore(flags);
1208
1209 offset += length;
1210 page++;
1211 }
1212}
1213
1214/*
Alex Elderf7760da2012-10-20 22:17:27 -05001215 * Clone a portion of a bio, starting at the given byte offset
1216 * and continuing for the number of bytes indicated.
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001217 */
Alex Elderf7760da2012-10-20 22:17:27 -05001218static struct bio *bio_clone_range(struct bio *bio_src,
1219 unsigned int offset,
1220 unsigned int len,
1221 gfp_t gfpmask)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001222{
Alex Elderf7760da2012-10-20 22:17:27 -05001223 struct bio_vec *bv;
1224 unsigned int resid;
1225 unsigned short idx;
1226 unsigned int voff;
1227 unsigned short end_idx;
1228 unsigned short vcnt;
1229 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001230
Alex Elderf7760da2012-10-20 22:17:27 -05001231 /* Handle the easy case for the caller */
1232
1233 if (!offset && len == bio_src->bi_size)
1234 return bio_clone(bio_src, gfpmask);
1235
1236 if (WARN_ON_ONCE(!len))
1237 return NULL;
1238 if (WARN_ON_ONCE(len > bio_src->bi_size))
1239 return NULL;
1240 if (WARN_ON_ONCE(offset > bio_src->bi_size - len))
1241 return NULL;
1242
1243 /* Find first affected segment... */
1244
1245 resid = offset;
Kent Overstreetd74c6d52013-02-06 12:23:11 -08001246 bio_for_each_segment(bv, bio_src, idx) {
Alex Elderf7760da2012-10-20 22:17:27 -05001247 if (resid < bv->bv_len)
1248 break;
1249 resid -= bv->bv_len;
1250 }
1251 voff = resid;
1252
1253 /* ...and the last affected segment */
1254
1255 resid += len;
1256 __bio_for_each_segment(bv, bio_src, end_idx, idx) {
1257 if (resid <= bv->bv_len)
1258 break;
1259 resid -= bv->bv_len;
1260 }
1261 vcnt = end_idx - idx + 1;
1262
1263 /* Build the clone */
1264
1265 bio = bio_alloc(gfpmask, (unsigned int) vcnt);
1266 if (!bio)
1267 return NULL; /* ENOMEM */
1268
1269 bio->bi_bdev = bio_src->bi_bdev;
1270 bio->bi_sector = bio_src->bi_sector + (offset >> SECTOR_SHIFT);
1271 bio->bi_rw = bio_src->bi_rw;
1272 bio->bi_flags |= 1 << BIO_CLONED;
1273
1274 /*
1275 * Copy over our part of the bio_vec, then update the first
1276 * and last (or only) entries.
1277 */
1278 memcpy(&bio->bi_io_vec[0], &bio_src->bi_io_vec[idx],
1279 vcnt * sizeof (struct bio_vec));
1280 bio->bi_io_vec[0].bv_offset += voff;
1281 if (vcnt > 1) {
1282 bio->bi_io_vec[0].bv_len -= voff;
1283 bio->bi_io_vec[vcnt - 1].bv_len = resid;
1284 } else {
1285 bio->bi_io_vec[0].bv_len = len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001286 }
1287
Alex Elderf7760da2012-10-20 22:17:27 -05001288 bio->bi_vcnt = vcnt;
1289 bio->bi_size = len;
1290 bio->bi_idx = 0;
Alex Elder542582f2012-08-09 10:33:25 -07001291
Alex Elderf7760da2012-10-20 22:17:27 -05001292 return bio;
1293}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001294
Alex Elderf7760da2012-10-20 22:17:27 -05001295/*
1296 * Clone a portion of a bio chain, starting at the given byte offset
1297 * into the first bio in the source chain and continuing for the
1298 * number of bytes indicated. The result is another bio chain of
1299 * exactly the given length, or a null pointer on error.
1300 *
1301 * The bio_src and offset parameters are both in-out. On entry they
1302 * refer to the first source bio and the offset into that bio where
1303 * the start of data to be cloned is located.
1304 *
1305 * On return, bio_src is updated to refer to the bio in the source
1306 * chain that contains first un-cloned byte, and *offset will
1307 * contain the offset of that byte within that bio.
1308 */
1309static struct bio *bio_chain_clone_range(struct bio **bio_src,
1310 unsigned int *offset,
1311 unsigned int len,
1312 gfp_t gfpmask)
1313{
1314 struct bio *bi = *bio_src;
1315 unsigned int off = *offset;
1316 struct bio *chain = NULL;
1317 struct bio **end;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001318
Alex Elderf7760da2012-10-20 22:17:27 -05001319 /* Build up a chain of clone bios up to the limit */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001320
Alex Elderf7760da2012-10-20 22:17:27 -05001321 if (!bi || off >= bi->bi_size || !len)
1322 return NULL; /* Nothing to clone */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001323
Alex Elderf7760da2012-10-20 22:17:27 -05001324 end = &chain;
1325 while (len) {
1326 unsigned int bi_size;
1327 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001328
Alex Elderf5400b72012-11-01 10:17:15 -05001329 if (!bi) {
1330 rbd_warn(NULL, "bio_chain exhausted with %u left", len);
Alex Elderf7760da2012-10-20 22:17:27 -05001331 goto out_err; /* EINVAL; ran out of bio's */
Alex Elderf5400b72012-11-01 10:17:15 -05001332 }
Alex Elderf7760da2012-10-20 22:17:27 -05001333 bi_size = min_t(unsigned int, bi->bi_size - off, len);
1334 bio = bio_clone_range(bi, off, bi_size, gfpmask);
1335 if (!bio)
1336 goto out_err; /* ENOMEM */
1337
1338 *end = bio;
1339 end = &bio->bi_next;
1340
1341 off += bi_size;
1342 if (off == bi->bi_size) {
1343 bi = bi->bi_next;
1344 off = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001345 }
Alex Elderf7760da2012-10-20 22:17:27 -05001346 len -= bi_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001347 }
Alex Elderf7760da2012-10-20 22:17:27 -05001348 *bio_src = bi;
1349 *offset = off;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001350
Alex Elderf7760da2012-10-20 22:17:27 -05001351 return chain;
1352out_err:
1353 bio_chain_put(chain);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001354
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001355 return NULL;
1356}
1357
Alex Elder926f9b32013-02-11 12:33:24 -06001358/*
1359 * The default/initial value for all object request flags is 0. For
1360 * each flag, once its value is set to 1 it is never reset to 0
1361 * again.
1362 */
Alex Elder6365d332013-02-11 12:33:24 -06001363static void obj_request_img_data_set(struct rbd_obj_request *obj_request)
1364{
1365 if (test_and_set_bit(OBJ_REQ_IMG_DATA, &obj_request->flags)) {
Alex Elder6365d332013-02-11 12:33:24 -06001366 struct rbd_device *rbd_dev;
1367
Alex Elder57acbaa2013-02-11 12:33:24 -06001368 rbd_dev = obj_request->img_request->rbd_dev;
Alex Elder6365d332013-02-11 12:33:24 -06001369 rbd_warn(rbd_dev, "obj_request %p already marked img_data\n",
1370 obj_request);
1371 }
1372}
1373
1374static bool obj_request_img_data_test(struct rbd_obj_request *obj_request)
1375{
1376 smp_mb();
1377 return test_bit(OBJ_REQ_IMG_DATA, &obj_request->flags) != 0;
1378}
1379
Alex Elder57acbaa2013-02-11 12:33:24 -06001380static void obj_request_done_set(struct rbd_obj_request *obj_request)
1381{
1382 if (test_and_set_bit(OBJ_REQ_DONE, &obj_request->flags)) {
1383 struct rbd_device *rbd_dev = NULL;
1384
1385 if (obj_request_img_data_test(obj_request))
1386 rbd_dev = obj_request->img_request->rbd_dev;
1387 rbd_warn(rbd_dev, "obj_request %p already marked done\n",
1388 obj_request);
1389 }
1390}
1391
1392static bool obj_request_done_test(struct rbd_obj_request *obj_request)
1393{
1394 smp_mb();
1395 return test_bit(OBJ_REQ_DONE, &obj_request->flags) != 0;
1396}
1397
Alex Elder5679c592013-02-11 12:33:24 -06001398/*
1399 * This sets the KNOWN flag after (possibly) setting the EXISTS
1400 * flag. The latter is set based on the "exists" value provided.
1401 *
1402 * Note that for our purposes once an object exists it never goes
1403 * away again. It's possible that the response from two existence
1404 * checks are separated by the creation of the target object, and
1405 * the first ("doesn't exist") response arrives *after* the second
1406 * ("does exist"). In that case we ignore the second one.
1407 */
1408static void obj_request_existence_set(struct rbd_obj_request *obj_request,
1409 bool exists)
1410{
1411 if (exists)
1412 set_bit(OBJ_REQ_EXISTS, &obj_request->flags);
1413 set_bit(OBJ_REQ_KNOWN, &obj_request->flags);
1414 smp_mb();
1415}
1416
1417static bool obj_request_known_test(struct rbd_obj_request *obj_request)
1418{
1419 smp_mb();
1420 return test_bit(OBJ_REQ_KNOWN, &obj_request->flags) != 0;
1421}
1422
1423static bool obj_request_exists_test(struct rbd_obj_request *obj_request)
1424{
1425 smp_mb();
1426 return test_bit(OBJ_REQ_EXISTS, &obj_request->flags) != 0;
1427}
1428
Alex Elderbf0d5f502012-11-22 00:00:08 -06001429static void rbd_obj_request_get(struct rbd_obj_request *obj_request)
1430{
Alex Elder37206ee2013-02-20 17:32:08 -06001431 dout("%s: obj %p (was %d)\n", __func__, obj_request,
1432 atomic_read(&obj_request->kref.refcount));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001433 kref_get(&obj_request->kref);
1434}
1435
1436static void rbd_obj_request_destroy(struct kref *kref);
1437static void rbd_obj_request_put(struct rbd_obj_request *obj_request)
1438{
1439 rbd_assert(obj_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001440 dout("%s: obj %p (was %d)\n", __func__, obj_request,
1441 atomic_read(&obj_request->kref.refcount));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001442 kref_put(&obj_request->kref, rbd_obj_request_destroy);
1443}
1444
Alex Eldere93f3152013-05-08 22:50:04 -05001445static bool img_request_child_test(struct rbd_img_request *img_request);
1446static void rbd_parent_request_destroy(struct kref *kref);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001447static void rbd_img_request_destroy(struct kref *kref);
1448static void rbd_img_request_put(struct rbd_img_request *img_request)
1449{
1450 rbd_assert(img_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001451 dout("%s: img %p (was %d)\n", __func__, img_request,
1452 atomic_read(&img_request->kref.refcount));
Alex Eldere93f3152013-05-08 22:50:04 -05001453 if (img_request_child_test(img_request))
1454 kref_put(&img_request->kref, rbd_parent_request_destroy);
1455 else
1456 kref_put(&img_request->kref, rbd_img_request_destroy);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001457}
1458
1459static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request,
1460 struct rbd_obj_request *obj_request)
1461{
Alex Elder25dcf952013-01-25 17:08:55 -06001462 rbd_assert(obj_request->img_request == NULL);
1463
Alex Elderb155e862013-04-15 14:50:37 -05001464 /* Image request now owns object's original reference */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001465 obj_request->img_request = img_request;
Alex Elder25dcf952013-01-25 17:08:55 -06001466 obj_request->which = img_request->obj_request_count;
Alex Elder6365d332013-02-11 12:33:24 -06001467 rbd_assert(!obj_request_img_data_test(obj_request));
1468 obj_request_img_data_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001469 rbd_assert(obj_request->which != BAD_WHICH);
Alex Elder25dcf952013-01-25 17:08:55 -06001470 img_request->obj_request_count++;
1471 list_add_tail(&obj_request->links, &img_request->obj_requests);
Alex Elder37206ee2013-02-20 17:32:08 -06001472 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1473 obj_request->which);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001474}
1475
1476static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request,
1477 struct rbd_obj_request *obj_request)
1478{
1479 rbd_assert(obj_request->which != BAD_WHICH);
Alex Elder25dcf952013-01-25 17:08:55 -06001480
Alex Elder37206ee2013-02-20 17:32:08 -06001481 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1482 obj_request->which);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001483 list_del(&obj_request->links);
Alex Elder25dcf952013-01-25 17:08:55 -06001484 rbd_assert(img_request->obj_request_count > 0);
1485 img_request->obj_request_count--;
1486 rbd_assert(obj_request->which == img_request->obj_request_count);
1487 obj_request->which = BAD_WHICH;
Alex Elder6365d332013-02-11 12:33:24 -06001488 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001489 rbd_assert(obj_request->img_request == img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001490 obj_request->img_request = NULL;
Alex Elder25dcf952013-01-25 17:08:55 -06001491 obj_request->callback = NULL;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001492 rbd_obj_request_put(obj_request);
1493}
1494
1495static bool obj_request_type_valid(enum obj_request_type type)
1496{
1497 switch (type) {
Alex Elder9969ebc2013-01-18 12:31:10 -06001498 case OBJ_REQUEST_NODATA:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001499 case OBJ_REQUEST_BIO:
Alex Elder788e2df2013-01-17 12:25:27 -06001500 case OBJ_REQUEST_PAGES:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001501 return true;
1502 default:
1503 return false;
1504 }
1505}
1506
Alex Elderbf0d5f502012-11-22 00:00:08 -06001507static int rbd_obj_request_submit(struct ceph_osd_client *osdc,
1508 struct rbd_obj_request *obj_request)
1509{
Alex Elder37206ee2013-02-20 17:32:08 -06001510 dout("%s: osdc %p obj %p\n", __func__, osdc, obj_request);
1511
Alex Elderbf0d5f502012-11-22 00:00:08 -06001512 return ceph_osdc_start_request(osdc, obj_request->osd_req, false);
1513}
1514
1515static void rbd_img_request_complete(struct rbd_img_request *img_request)
1516{
Alex Elder55f27e02013-04-10 12:34:25 -05001517
Alex Elder37206ee2013-02-20 17:32:08 -06001518 dout("%s: img %p\n", __func__, img_request);
Alex Elder55f27e02013-04-10 12:34:25 -05001519
1520 /*
1521 * If no error occurred, compute the aggregate transfer
1522 * count for the image request. We could instead use
1523 * atomic64_cmpxchg() to update it as each object request
1524 * completes; not clear which way is better off hand.
1525 */
1526 if (!img_request->result) {
1527 struct rbd_obj_request *obj_request;
1528 u64 xferred = 0;
1529
1530 for_each_obj_request(img_request, obj_request)
1531 xferred += obj_request->xferred;
1532 img_request->xferred = xferred;
1533 }
1534
Alex Elderbf0d5f502012-11-22 00:00:08 -06001535 if (img_request->callback)
1536 img_request->callback(img_request);
1537 else
1538 rbd_img_request_put(img_request);
1539}
1540
Alex Elder788e2df2013-01-17 12:25:27 -06001541/* Caller is responsible for rbd_obj_request_destroy(obj_request) */
1542
1543static int rbd_obj_request_wait(struct rbd_obj_request *obj_request)
1544{
Alex Elder37206ee2013-02-20 17:32:08 -06001545 dout("%s: obj %p\n", __func__, obj_request);
1546
Alex Elder788e2df2013-01-17 12:25:27 -06001547 return wait_for_completion_interruptible(&obj_request->completion);
1548}
1549
Alex Elder0c425242013-02-08 09:55:49 -06001550/*
1551 * The default/initial value for all image request flags is 0. Each
1552 * is conditionally set to 1 at image request initialization time
1553 * and currently never change thereafter.
1554 */
1555static void img_request_write_set(struct rbd_img_request *img_request)
1556{
1557 set_bit(IMG_REQ_WRITE, &img_request->flags);
1558 smp_mb();
1559}
1560
1561static bool img_request_write_test(struct rbd_img_request *img_request)
1562{
1563 smp_mb();
1564 return test_bit(IMG_REQ_WRITE, &img_request->flags) != 0;
1565}
1566
Alex Elder9849e982013-01-24 16:13:36 -06001567static void img_request_child_set(struct rbd_img_request *img_request)
1568{
1569 set_bit(IMG_REQ_CHILD, &img_request->flags);
1570 smp_mb();
1571}
1572
Alex Eldere93f3152013-05-08 22:50:04 -05001573static void img_request_child_clear(struct rbd_img_request *img_request)
1574{
1575 clear_bit(IMG_REQ_CHILD, &img_request->flags);
1576 smp_mb();
1577}
1578
Alex Elder9849e982013-01-24 16:13:36 -06001579static bool img_request_child_test(struct rbd_img_request *img_request)
1580{
1581 smp_mb();
1582 return test_bit(IMG_REQ_CHILD, &img_request->flags) != 0;
1583}
1584
Alex Elderd0b2e942013-01-24 16:13:36 -06001585static void img_request_layered_set(struct rbd_img_request *img_request)
1586{
1587 set_bit(IMG_REQ_LAYERED, &img_request->flags);
1588 smp_mb();
1589}
1590
Alex Eldera2acd002013-05-08 22:50:04 -05001591static void img_request_layered_clear(struct rbd_img_request *img_request)
1592{
1593 clear_bit(IMG_REQ_LAYERED, &img_request->flags);
1594 smp_mb();
1595}
1596
Alex Elderd0b2e942013-01-24 16:13:36 -06001597static bool img_request_layered_test(struct rbd_img_request *img_request)
1598{
1599 smp_mb();
1600 return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0;
1601}
1602
Alex Elder6e2a4502013-03-27 09:16:30 -05001603static void
1604rbd_img_obj_request_read_callback(struct rbd_obj_request *obj_request)
1605{
Alex Elderb9434c52013-04-19 15:34:50 -05001606 u64 xferred = obj_request->xferred;
1607 u64 length = obj_request->length;
1608
Alex Elder6e2a4502013-03-27 09:16:30 -05001609 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1610 obj_request, obj_request->img_request, obj_request->result,
Alex Elderb9434c52013-04-19 15:34:50 -05001611 xferred, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001612 /*
Josh Durgin17c1cc12013-08-26 17:55:38 -07001613 * ENOENT means a hole in the image. We zero-fill the entire
1614 * length of the request. A short read also implies zero-fill
1615 * to the end of the request. An error requires the whole
1616 * length of the request to be reported finished with an error
1617 * to the block layer. In each case we update the xferred
1618 * count to indicate the whole request was satisfied.
Alex Elder6e2a4502013-03-27 09:16:30 -05001619 */
Alex Elderb9434c52013-04-19 15:34:50 -05001620 rbd_assert(obj_request->type != OBJ_REQUEST_NODATA);
Alex Elder6e2a4502013-03-27 09:16:30 -05001621 if (obj_request->result == -ENOENT) {
Alex Elderb9434c52013-04-19 15:34:50 -05001622 if (obj_request->type == OBJ_REQUEST_BIO)
1623 zero_bio_chain(obj_request->bio_list, 0);
1624 else
1625 zero_pages(obj_request->pages, 0, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001626 obj_request->result = 0;
Alex Elderb9434c52013-04-19 15:34:50 -05001627 } else if (xferred < length && !obj_request->result) {
1628 if (obj_request->type == OBJ_REQUEST_BIO)
1629 zero_bio_chain(obj_request->bio_list, xferred);
1630 else
1631 zero_pages(obj_request->pages, xferred, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001632 }
Josh Durgin17c1cc12013-08-26 17:55:38 -07001633 obj_request->xferred = length;
Alex Elder6e2a4502013-03-27 09:16:30 -05001634 obj_request_done_set(obj_request);
1635}
1636
Alex Elderbf0d5f502012-11-22 00:00:08 -06001637static void rbd_obj_request_complete(struct rbd_obj_request *obj_request)
1638{
Alex Elder37206ee2013-02-20 17:32:08 -06001639 dout("%s: obj %p cb %p\n", __func__, obj_request,
1640 obj_request->callback);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001641 if (obj_request->callback)
1642 obj_request->callback(obj_request);
Alex Elder788e2df2013-01-17 12:25:27 -06001643 else
1644 complete_all(&obj_request->completion);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001645}
1646
Alex Elderc47f9372013-02-26 14:23:07 -06001647static void rbd_osd_trivial_callback(struct rbd_obj_request *obj_request)
Alex Elder39bf2c52013-02-26 14:23:07 -06001648{
1649 dout("%s: obj %p\n", __func__, obj_request);
1650 obj_request_done_set(obj_request);
1651}
1652
Alex Elderc47f9372013-02-26 14:23:07 -06001653static void rbd_osd_read_callback(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001654{
Alex Elder57acbaa2013-02-11 12:33:24 -06001655 struct rbd_img_request *img_request = NULL;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001656 struct rbd_device *rbd_dev = NULL;
Alex Elder57acbaa2013-02-11 12:33:24 -06001657 bool layered = false;
1658
1659 if (obj_request_img_data_test(obj_request)) {
1660 img_request = obj_request->img_request;
1661 layered = img_request && img_request_layered_test(img_request);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001662 rbd_dev = img_request->rbd_dev;
Alex Elder57acbaa2013-02-11 12:33:24 -06001663 }
Alex Elder8b3e1a52013-01-24 16:13:36 -06001664
1665 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1666 obj_request, img_request, obj_request->result,
1667 obj_request->xferred, obj_request->length);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001668 if (layered && obj_request->result == -ENOENT &&
1669 obj_request->img_offset < rbd_dev->parent_overlap)
Alex Elder8b3e1a52013-01-24 16:13:36 -06001670 rbd_img_parent_read(obj_request);
1671 else if (img_request)
Alex Elder6e2a4502013-03-27 09:16:30 -05001672 rbd_img_obj_request_read_callback(obj_request);
1673 else
1674 obj_request_done_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001675}
1676
Alex Elderc47f9372013-02-26 14:23:07 -06001677static void rbd_osd_write_callback(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001678{
Sage Weil1b83bef2013-02-25 16:11:12 -08001679 dout("%s: obj %p result %d %llu\n", __func__, obj_request,
1680 obj_request->result, obj_request->length);
1681 /*
Alex Elder8b3e1a52013-01-24 16:13:36 -06001682 * There is no such thing as a successful short write. Set
1683 * it to our originally-requested length.
Sage Weil1b83bef2013-02-25 16:11:12 -08001684 */
1685 obj_request->xferred = obj_request->length;
Alex Elder07741302013-02-05 23:41:50 -06001686 obj_request_done_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001687}
1688
Alex Elderfbfab532013-02-08 09:55:48 -06001689/*
1690 * For a simple stat call there's nothing to do. We'll do more if
1691 * this is part of a write sequence for a layered image.
1692 */
Alex Elderc47f9372013-02-26 14:23:07 -06001693static void rbd_osd_stat_callback(struct rbd_obj_request *obj_request)
Alex Elderfbfab532013-02-08 09:55:48 -06001694{
Alex Elder37206ee2013-02-20 17:32:08 -06001695 dout("%s: obj %p\n", __func__, obj_request);
Alex Elderfbfab532013-02-08 09:55:48 -06001696 obj_request_done_set(obj_request);
1697}
1698
Alex Elderbf0d5f502012-11-22 00:00:08 -06001699static void rbd_osd_req_callback(struct ceph_osd_request *osd_req,
1700 struct ceph_msg *msg)
1701{
1702 struct rbd_obj_request *obj_request = osd_req->r_priv;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001703 u16 opcode;
1704
Alex Elder37206ee2013-02-20 17:32:08 -06001705 dout("%s: osd_req %p msg %p\n", __func__, osd_req, msg);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001706 rbd_assert(osd_req == obj_request->osd_req);
Alex Elder57acbaa2013-02-11 12:33:24 -06001707 if (obj_request_img_data_test(obj_request)) {
1708 rbd_assert(obj_request->img_request);
1709 rbd_assert(obj_request->which != BAD_WHICH);
1710 } else {
1711 rbd_assert(obj_request->which == BAD_WHICH);
1712 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06001713
Sage Weil1b83bef2013-02-25 16:11:12 -08001714 if (osd_req->r_result < 0)
1715 obj_request->result = osd_req->r_result;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001716
Alex Elder0eefd472013-04-19 15:34:50 -05001717 BUG_ON(osd_req->r_num_ops > 2);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001718
Alex Elderc47f9372013-02-26 14:23:07 -06001719 /*
1720 * We support a 64-bit length, but ultimately it has to be
1721 * passed to blk_end_request(), which takes an unsigned int.
1722 */
Sage Weil1b83bef2013-02-25 16:11:12 -08001723 obj_request->xferred = osd_req->r_reply_op_len[0];
Alex Elder8b3e1a52013-01-24 16:13:36 -06001724 rbd_assert(obj_request->xferred < (u64)UINT_MAX);
Alex Elder79528732013-04-03 21:32:51 -05001725 opcode = osd_req->r_ops[0].op;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001726 switch (opcode) {
1727 case CEPH_OSD_OP_READ:
Alex Elderc47f9372013-02-26 14:23:07 -06001728 rbd_osd_read_callback(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001729 break;
1730 case CEPH_OSD_OP_WRITE:
Alex Elderc47f9372013-02-26 14:23:07 -06001731 rbd_osd_write_callback(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001732 break;
Alex Elderfbfab532013-02-08 09:55:48 -06001733 case CEPH_OSD_OP_STAT:
Alex Elderc47f9372013-02-26 14:23:07 -06001734 rbd_osd_stat_callback(obj_request);
Alex Elderfbfab532013-02-08 09:55:48 -06001735 break;
Alex Elder36be9a72013-01-19 00:30:28 -06001736 case CEPH_OSD_OP_CALL:
Alex Elderb8d70032012-11-30 17:53:04 -06001737 case CEPH_OSD_OP_NOTIFY_ACK:
Alex Elder9969ebc2013-01-18 12:31:10 -06001738 case CEPH_OSD_OP_WATCH:
Alex Elderc47f9372013-02-26 14:23:07 -06001739 rbd_osd_trivial_callback(obj_request);
Alex Elder9969ebc2013-01-18 12:31:10 -06001740 break;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001741 default:
1742 rbd_warn(NULL, "%s: unsupported op %hu\n",
1743 obj_request->object_name, (unsigned short) opcode);
1744 break;
1745 }
1746
Alex Elder07741302013-02-05 23:41:50 -06001747 if (obj_request_done_test(obj_request))
Alex Elderbf0d5f502012-11-22 00:00:08 -06001748 rbd_obj_request_complete(obj_request);
1749}
1750
Alex Elder9d4df012013-04-19 15:34:50 -05001751static void rbd_osd_req_format_read(struct rbd_obj_request *obj_request)
Alex Elder430c28c2013-04-03 21:32:51 -05001752{
1753 struct rbd_img_request *img_request = obj_request->img_request;
Alex Elder8c042b02013-04-03 01:28:58 -05001754 struct ceph_osd_request *osd_req = obj_request->osd_req;
Alex Elder9d4df012013-04-19 15:34:50 -05001755 u64 snap_id;
Alex Elder430c28c2013-04-03 21:32:51 -05001756
Alex Elder8c042b02013-04-03 01:28:58 -05001757 rbd_assert(osd_req != NULL);
Alex Elder430c28c2013-04-03 21:32:51 -05001758
Alex Elder9d4df012013-04-19 15:34:50 -05001759 snap_id = img_request ? img_request->snap_id : CEPH_NOSNAP;
Alex Elder8c042b02013-04-03 01:28:58 -05001760 ceph_osdc_build_request(osd_req, obj_request->offset,
Alex Elder9d4df012013-04-19 15:34:50 -05001761 NULL, snap_id, NULL);
1762}
1763
1764static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request)
1765{
1766 struct rbd_img_request *img_request = obj_request->img_request;
1767 struct ceph_osd_request *osd_req = obj_request->osd_req;
1768 struct ceph_snap_context *snapc;
1769 struct timespec mtime = CURRENT_TIME;
1770
1771 rbd_assert(osd_req != NULL);
1772
1773 snapc = img_request ? img_request->snapc : NULL;
1774 ceph_osdc_build_request(osd_req, obj_request->offset,
1775 snapc, CEPH_NOSNAP, &mtime);
Alex Elder430c28c2013-04-03 21:32:51 -05001776}
1777
Alex Elderbf0d5f502012-11-22 00:00:08 -06001778static struct ceph_osd_request *rbd_osd_req_create(
1779 struct rbd_device *rbd_dev,
1780 bool write_request,
Alex Elder430c28c2013-04-03 21:32:51 -05001781 struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001782{
Alex Elderbf0d5f502012-11-22 00:00:08 -06001783 struct ceph_snap_context *snapc = NULL;
1784 struct ceph_osd_client *osdc;
1785 struct ceph_osd_request *osd_req;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001786
Alex Elder6365d332013-02-11 12:33:24 -06001787 if (obj_request_img_data_test(obj_request)) {
1788 struct rbd_img_request *img_request = obj_request->img_request;
1789
Alex Elder0c425242013-02-08 09:55:49 -06001790 rbd_assert(write_request ==
1791 img_request_write_test(img_request));
1792 if (write_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001793 snapc = img_request->snapc;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001794 }
1795
1796 /* Allocate and initialize the request, for the single op */
1797
1798 osdc = &rbd_dev->rbd_client->client->osdc;
1799 osd_req = ceph_osdc_alloc_request(osdc, snapc, 1, false, GFP_ATOMIC);
1800 if (!osd_req)
1801 return NULL; /* ENOMEM */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001802
Alex Elder430c28c2013-04-03 21:32:51 -05001803 if (write_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001804 osd_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK;
Alex Elder430c28c2013-04-03 21:32:51 -05001805 else
Alex Elderbf0d5f502012-11-22 00:00:08 -06001806 osd_req->r_flags = CEPH_OSD_FLAG_READ;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001807
1808 osd_req->r_callback = rbd_osd_req_callback;
1809 osd_req->r_priv = obj_request;
1810
Ilya Dryomov221165252014-01-27 17:40:18 +02001811 osd_req->r_oloc.pool = ceph_file_layout_pg_pool(rbd_dev->layout);
1812
Alex Elderbf0d5f502012-11-22 00:00:08 -06001813 osd_req->r_oid_len = strlen(obj_request->object_name);
1814 rbd_assert(osd_req->r_oid_len < sizeof (osd_req->r_oid));
1815 memcpy(osd_req->r_oid, obj_request->object_name, osd_req->r_oid_len);
1816
Alex Elderbf0d5f502012-11-22 00:00:08 -06001817 return osd_req;
1818}
1819
Alex Elder0eefd472013-04-19 15:34:50 -05001820/*
1821 * Create a copyup osd request based on the information in the
1822 * object request supplied. A copyup request has two osd ops,
1823 * a copyup method call, and a "normal" write request.
1824 */
1825static struct ceph_osd_request *
1826rbd_osd_req_create_copyup(struct rbd_obj_request *obj_request)
1827{
1828 struct rbd_img_request *img_request;
1829 struct ceph_snap_context *snapc;
1830 struct rbd_device *rbd_dev;
1831 struct ceph_osd_client *osdc;
1832 struct ceph_osd_request *osd_req;
1833
1834 rbd_assert(obj_request_img_data_test(obj_request));
1835 img_request = obj_request->img_request;
1836 rbd_assert(img_request);
1837 rbd_assert(img_request_write_test(img_request));
1838
1839 /* Allocate and initialize the request, for the two ops */
1840
1841 snapc = img_request->snapc;
1842 rbd_dev = img_request->rbd_dev;
1843 osdc = &rbd_dev->rbd_client->client->osdc;
1844 osd_req = ceph_osdc_alloc_request(osdc, snapc, 2, false, GFP_ATOMIC);
1845 if (!osd_req)
1846 return NULL; /* ENOMEM */
1847
1848 osd_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK;
1849 osd_req->r_callback = rbd_osd_req_callback;
1850 osd_req->r_priv = obj_request;
1851
Ilya Dryomov221165252014-01-27 17:40:18 +02001852 osd_req->r_oloc.pool = ceph_file_layout_pg_pool(rbd_dev->layout);
1853
Alex Elder0eefd472013-04-19 15:34:50 -05001854 osd_req->r_oid_len = strlen(obj_request->object_name);
1855 rbd_assert(osd_req->r_oid_len < sizeof (osd_req->r_oid));
1856 memcpy(osd_req->r_oid, obj_request->object_name, osd_req->r_oid_len);
1857
Alex Elder0eefd472013-04-19 15:34:50 -05001858 return osd_req;
1859}
1860
1861
Alex Elderbf0d5f502012-11-22 00:00:08 -06001862static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req)
1863{
1864 ceph_osdc_put_request(osd_req);
1865}
1866
1867/* object_name is assumed to be a non-null pointer and NUL-terminated */
1868
1869static struct rbd_obj_request *rbd_obj_request_create(const char *object_name,
1870 u64 offset, u64 length,
1871 enum obj_request_type type)
1872{
1873 struct rbd_obj_request *obj_request;
1874 size_t size;
1875 char *name;
1876
1877 rbd_assert(obj_request_type_valid(type));
1878
1879 size = strlen(object_name) + 1;
Alex Elderf907ad52013-05-01 12:43:03 -05001880 name = kmalloc(size, GFP_KERNEL);
1881 if (!name)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001882 return NULL;
1883
Alex Elder868311b2013-05-01 12:43:03 -05001884 obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_KERNEL);
Alex Elderf907ad52013-05-01 12:43:03 -05001885 if (!obj_request) {
1886 kfree(name);
1887 return NULL;
1888 }
1889
Alex Elderbf0d5f502012-11-22 00:00:08 -06001890 obj_request->object_name = memcpy(name, object_name, size);
1891 obj_request->offset = offset;
1892 obj_request->length = length;
Alex Elder926f9b32013-02-11 12:33:24 -06001893 obj_request->flags = 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001894 obj_request->which = BAD_WHICH;
1895 obj_request->type = type;
1896 INIT_LIST_HEAD(&obj_request->links);
Alex Elder788e2df2013-01-17 12:25:27 -06001897 init_completion(&obj_request->completion);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001898 kref_init(&obj_request->kref);
1899
Alex Elder37206ee2013-02-20 17:32:08 -06001900 dout("%s: \"%s\" %llu/%llu %d -> obj %p\n", __func__, object_name,
1901 offset, length, (int)type, obj_request);
1902
Alex Elderbf0d5f502012-11-22 00:00:08 -06001903 return obj_request;
1904}
1905
1906static void rbd_obj_request_destroy(struct kref *kref)
1907{
1908 struct rbd_obj_request *obj_request;
1909
1910 obj_request = container_of(kref, struct rbd_obj_request, kref);
1911
Alex Elder37206ee2013-02-20 17:32:08 -06001912 dout("%s: obj %p\n", __func__, obj_request);
1913
Alex Elderbf0d5f502012-11-22 00:00:08 -06001914 rbd_assert(obj_request->img_request == NULL);
1915 rbd_assert(obj_request->which == BAD_WHICH);
1916
1917 if (obj_request->osd_req)
1918 rbd_osd_req_destroy(obj_request->osd_req);
1919
1920 rbd_assert(obj_request_type_valid(obj_request->type));
1921 switch (obj_request->type) {
Alex Elder9969ebc2013-01-18 12:31:10 -06001922 case OBJ_REQUEST_NODATA:
1923 break; /* Nothing to do */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001924 case OBJ_REQUEST_BIO:
1925 if (obj_request->bio_list)
1926 bio_chain_put(obj_request->bio_list);
1927 break;
Alex Elder788e2df2013-01-17 12:25:27 -06001928 case OBJ_REQUEST_PAGES:
1929 if (obj_request->pages)
1930 ceph_release_page_vector(obj_request->pages,
1931 obj_request->page_count);
1932 break;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001933 }
1934
Alex Elderf907ad52013-05-01 12:43:03 -05001935 kfree(obj_request->object_name);
Alex Elder868311b2013-05-01 12:43:03 -05001936 obj_request->object_name = NULL;
1937 kmem_cache_free(rbd_obj_request_cache, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001938}
1939
Alex Elderfb65d2282013-05-08 22:50:04 -05001940/* It's OK to call this for a device with no parent */
1941
1942static void rbd_spec_put(struct rbd_spec *spec);
1943static void rbd_dev_unparent(struct rbd_device *rbd_dev)
1944{
1945 rbd_dev_remove_parent(rbd_dev);
1946 rbd_spec_put(rbd_dev->parent_spec);
1947 rbd_dev->parent_spec = NULL;
1948 rbd_dev->parent_overlap = 0;
1949}
1950
Alex Elderbf0d5f502012-11-22 00:00:08 -06001951/*
Alex Eldera2acd002013-05-08 22:50:04 -05001952 * Parent image reference counting is used to determine when an
1953 * image's parent fields can be safely torn down--after there are no
1954 * more in-flight requests to the parent image. When the last
1955 * reference is dropped, cleaning them up is safe.
1956 */
1957static void rbd_dev_parent_put(struct rbd_device *rbd_dev)
1958{
1959 int counter;
1960
1961 if (!rbd_dev->parent_spec)
1962 return;
1963
1964 counter = atomic_dec_return_safe(&rbd_dev->parent_ref);
1965 if (counter > 0)
1966 return;
1967
1968 /* Last reference; clean up parent data structures */
1969
1970 if (!counter)
1971 rbd_dev_unparent(rbd_dev);
1972 else
1973 rbd_warn(rbd_dev, "parent reference underflow\n");
1974}
1975
1976/*
1977 * If an image has a non-zero parent overlap, get a reference to its
1978 * parent.
1979 *
Alex Elder392a9da2013-05-06 17:40:33 -05001980 * We must get the reference before checking for the overlap to
1981 * coordinate properly with zeroing the parent overlap in
1982 * rbd_dev_v2_parent_info() when an image gets flattened. We
1983 * drop it again if there is no overlap.
1984 *
Alex Eldera2acd002013-05-08 22:50:04 -05001985 * Returns true if the rbd device has a parent with a non-zero
1986 * overlap and a reference for it was successfully taken, or
1987 * false otherwise.
1988 */
1989static bool rbd_dev_parent_get(struct rbd_device *rbd_dev)
1990{
1991 int counter;
1992
1993 if (!rbd_dev->parent_spec)
1994 return false;
1995
1996 counter = atomic_inc_return_safe(&rbd_dev->parent_ref);
1997 if (counter > 0 && rbd_dev->parent_overlap)
1998 return true;
1999
2000 /* Image was flattened, but parent is not yet torn down */
2001
2002 if (counter < 0)
2003 rbd_warn(rbd_dev, "parent reference overflow\n");
2004
2005 return false;
2006}
2007
Alex Elderbf0d5f502012-11-22 00:00:08 -06002008/*
2009 * Caller is responsible for filling in the list of object requests
2010 * that comprises the image request, and the Linux request pointer
2011 * (if there is one).
2012 */
Alex Eldercc344fa2013-02-19 12:25:56 -06002013static struct rbd_img_request *rbd_img_request_create(
2014 struct rbd_device *rbd_dev,
Alex Elderbf0d5f502012-11-22 00:00:08 -06002015 u64 offset, u64 length,
Alex Eldere93f3152013-05-08 22:50:04 -05002016 bool write_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002017{
2018 struct rbd_img_request *img_request;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002019
Alex Elder1c2a9df2013-05-01 12:43:03 -05002020 img_request = kmem_cache_alloc(rbd_img_request_cache, GFP_ATOMIC);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002021 if (!img_request)
2022 return NULL;
2023
2024 if (write_request) {
2025 down_read(&rbd_dev->header_rwsem);
Alex Elder812164f82013-04-30 00:44:32 -05002026 ceph_get_snap_context(rbd_dev->header.snapc);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002027 up_read(&rbd_dev->header_rwsem);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002028 }
2029
2030 img_request->rq = NULL;
2031 img_request->rbd_dev = rbd_dev;
2032 img_request->offset = offset;
2033 img_request->length = length;
Alex Elder0c425242013-02-08 09:55:49 -06002034 img_request->flags = 0;
2035 if (write_request) {
2036 img_request_write_set(img_request);
Alex Elder468521c2013-04-26 09:43:47 -05002037 img_request->snapc = rbd_dev->header.snapc;
Alex Elder0c425242013-02-08 09:55:49 -06002038 } else {
Alex Elderbf0d5f502012-11-22 00:00:08 -06002039 img_request->snap_id = rbd_dev->spec->snap_id;
Alex Elder0c425242013-02-08 09:55:49 -06002040 }
Alex Eldera2acd002013-05-08 22:50:04 -05002041 if (rbd_dev_parent_get(rbd_dev))
Alex Elderd0b2e942013-01-24 16:13:36 -06002042 img_request_layered_set(img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002043 spin_lock_init(&img_request->completion_lock);
2044 img_request->next_completion = 0;
2045 img_request->callback = NULL;
Alex Eldera5a337d2013-01-24 16:13:36 -06002046 img_request->result = 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002047 img_request->obj_request_count = 0;
2048 INIT_LIST_HEAD(&img_request->obj_requests);
2049 kref_init(&img_request->kref);
2050
Alex Elder37206ee2013-02-20 17:32:08 -06002051 dout("%s: rbd_dev %p %s %llu/%llu -> img %p\n", __func__, rbd_dev,
2052 write_request ? "write" : "read", offset, length,
2053 img_request);
2054
Alex Elderbf0d5f502012-11-22 00:00:08 -06002055 return img_request;
2056}
2057
2058static void rbd_img_request_destroy(struct kref *kref)
2059{
2060 struct rbd_img_request *img_request;
2061 struct rbd_obj_request *obj_request;
2062 struct rbd_obj_request *next_obj_request;
2063
2064 img_request = container_of(kref, struct rbd_img_request, kref);
2065
Alex Elder37206ee2013-02-20 17:32:08 -06002066 dout("%s: img %p\n", __func__, img_request);
2067
Alex Elderbf0d5f502012-11-22 00:00:08 -06002068 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
2069 rbd_img_obj_request_del(img_request, obj_request);
Alex Elder25dcf952013-01-25 17:08:55 -06002070 rbd_assert(img_request->obj_request_count == 0);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002071
Alex Eldera2acd002013-05-08 22:50:04 -05002072 if (img_request_layered_test(img_request)) {
2073 img_request_layered_clear(img_request);
2074 rbd_dev_parent_put(img_request->rbd_dev);
2075 }
2076
Alex Elder0c425242013-02-08 09:55:49 -06002077 if (img_request_write_test(img_request))
Alex Elder812164f82013-04-30 00:44:32 -05002078 ceph_put_snap_context(img_request->snapc);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002079
Alex Elder1c2a9df2013-05-01 12:43:03 -05002080 kmem_cache_free(rbd_img_request_cache, img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002081}
2082
Alex Eldere93f3152013-05-08 22:50:04 -05002083static struct rbd_img_request *rbd_parent_request_create(
2084 struct rbd_obj_request *obj_request,
2085 u64 img_offset, u64 length)
2086{
2087 struct rbd_img_request *parent_request;
2088 struct rbd_device *rbd_dev;
2089
2090 rbd_assert(obj_request->img_request);
2091 rbd_dev = obj_request->img_request->rbd_dev;
2092
2093 parent_request = rbd_img_request_create(rbd_dev->parent,
2094 img_offset, length, false);
2095 if (!parent_request)
2096 return NULL;
2097
2098 img_request_child_set(parent_request);
2099 rbd_obj_request_get(obj_request);
2100 parent_request->obj_request = obj_request;
2101
2102 return parent_request;
2103}
2104
2105static void rbd_parent_request_destroy(struct kref *kref)
2106{
2107 struct rbd_img_request *parent_request;
2108 struct rbd_obj_request *orig_request;
2109
2110 parent_request = container_of(kref, struct rbd_img_request, kref);
2111 orig_request = parent_request->obj_request;
2112
2113 parent_request->obj_request = NULL;
2114 rbd_obj_request_put(orig_request);
2115 img_request_child_clear(parent_request);
2116
2117 rbd_img_request_destroy(kref);
2118}
2119
Alex Elder12178572013-02-08 09:55:49 -06002120static bool rbd_img_obj_end_request(struct rbd_obj_request *obj_request)
2121{
Alex Elder6365d332013-02-11 12:33:24 -06002122 struct rbd_img_request *img_request;
Alex Elder12178572013-02-08 09:55:49 -06002123 unsigned int xferred;
2124 int result;
Alex Elder8b3e1a52013-01-24 16:13:36 -06002125 bool more;
Alex Elder12178572013-02-08 09:55:49 -06002126
Alex Elder6365d332013-02-11 12:33:24 -06002127 rbd_assert(obj_request_img_data_test(obj_request));
2128 img_request = obj_request->img_request;
2129
Alex Elder12178572013-02-08 09:55:49 -06002130 rbd_assert(obj_request->xferred <= (u64)UINT_MAX);
2131 xferred = (unsigned int)obj_request->xferred;
2132 result = obj_request->result;
2133 if (result) {
2134 struct rbd_device *rbd_dev = img_request->rbd_dev;
2135
2136 rbd_warn(rbd_dev, "%s %llx at %llx (%llx)\n",
2137 img_request_write_test(img_request) ? "write" : "read",
2138 obj_request->length, obj_request->img_offset,
2139 obj_request->offset);
2140 rbd_warn(rbd_dev, " result %d xferred %x\n",
2141 result, xferred);
2142 if (!img_request->result)
2143 img_request->result = result;
2144 }
2145
Alex Elderf1a47392013-04-19 15:34:50 -05002146 /* Image object requests don't own their page array */
2147
2148 if (obj_request->type == OBJ_REQUEST_PAGES) {
2149 obj_request->pages = NULL;
2150 obj_request->page_count = 0;
2151 }
2152
Alex Elder8b3e1a52013-01-24 16:13:36 -06002153 if (img_request_child_test(img_request)) {
2154 rbd_assert(img_request->obj_request != NULL);
2155 more = obj_request->which < img_request->obj_request_count - 1;
2156 } else {
2157 rbd_assert(img_request->rq != NULL);
2158 more = blk_end_request(img_request->rq, result, xferred);
2159 }
2160
2161 return more;
Alex Elder12178572013-02-08 09:55:49 -06002162}
2163
Alex Elder21692382013-04-05 01:27:12 -05002164static void rbd_img_obj_callback(struct rbd_obj_request *obj_request)
2165{
2166 struct rbd_img_request *img_request;
2167 u32 which = obj_request->which;
2168 bool more = true;
2169
Alex Elder6365d332013-02-11 12:33:24 -06002170 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elder21692382013-04-05 01:27:12 -05002171 img_request = obj_request->img_request;
2172
2173 dout("%s: img %p obj %p\n", __func__, img_request, obj_request);
2174 rbd_assert(img_request != NULL);
Alex Elder21692382013-04-05 01:27:12 -05002175 rbd_assert(img_request->obj_request_count > 0);
2176 rbd_assert(which != BAD_WHICH);
2177 rbd_assert(which < img_request->obj_request_count);
2178 rbd_assert(which >= img_request->next_completion);
2179
2180 spin_lock_irq(&img_request->completion_lock);
2181 if (which != img_request->next_completion)
2182 goto out;
2183
2184 for_each_obj_request_from(img_request, obj_request) {
Alex Elder21692382013-04-05 01:27:12 -05002185 rbd_assert(more);
2186 rbd_assert(which < img_request->obj_request_count);
2187
2188 if (!obj_request_done_test(obj_request))
2189 break;
Alex Elder12178572013-02-08 09:55:49 -06002190 more = rbd_img_obj_end_request(obj_request);
Alex Elder21692382013-04-05 01:27:12 -05002191 which++;
2192 }
2193
2194 rbd_assert(more ^ (which == img_request->obj_request_count));
2195 img_request->next_completion = which;
2196out:
2197 spin_unlock_irq(&img_request->completion_lock);
2198
2199 if (!more)
2200 rbd_img_request_complete(img_request);
2201}
2202
Alex Elderf1a47392013-04-19 15:34:50 -05002203/*
2204 * Split up an image request into one or more object requests, each
2205 * to a different object. The "type" parameter indicates whether
2206 * "data_desc" is the pointer to the head of a list of bio
2207 * structures, or the base of a page array. In either case this
2208 * function assumes data_desc describes memory sufficient to hold
2209 * all data described by the image request.
2210 */
2211static int rbd_img_request_fill(struct rbd_img_request *img_request,
2212 enum obj_request_type type,
2213 void *data_desc)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002214{
2215 struct rbd_device *rbd_dev = img_request->rbd_dev;
2216 struct rbd_obj_request *obj_request = NULL;
2217 struct rbd_obj_request *next_obj_request;
Alex Elder0c425242013-02-08 09:55:49 -06002218 bool write_request = img_request_write_test(img_request);
Jingoo Hana1580732013-08-09 13:04:35 +09002219 struct bio *bio_list = NULL;
Alex Elderf1a47392013-04-19 15:34:50 -05002220 unsigned int bio_offset = 0;
Jingoo Hana1580732013-08-09 13:04:35 +09002221 struct page **pages = NULL;
Alex Elder7da22d22013-01-24 16:13:36 -06002222 u64 img_offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002223 u64 resid;
2224 u16 opcode;
2225
Alex Elderf1a47392013-04-19 15:34:50 -05002226 dout("%s: img %p type %d data_desc %p\n", __func__, img_request,
2227 (int)type, data_desc);
Alex Elder37206ee2013-02-20 17:32:08 -06002228
Alex Elder430c28c2013-04-03 21:32:51 -05002229 opcode = write_request ? CEPH_OSD_OP_WRITE : CEPH_OSD_OP_READ;
Alex Elder7da22d22013-01-24 16:13:36 -06002230 img_offset = img_request->offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002231 resid = img_request->length;
Alex Elder4dda41d2013-02-20 21:59:33 -06002232 rbd_assert(resid > 0);
Alex Elderf1a47392013-04-19 15:34:50 -05002233
2234 if (type == OBJ_REQUEST_BIO) {
2235 bio_list = data_desc;
2236 rbd_assert(img_offset == bio_list->bi_sector << SECTOR_SHIFT);
2237 } else {
2238 rbd_assert(type == OBJ_REQUEST_PAGES);
2239 pages = data_desc;
2240 }
2241
Alex Elderbf0d5f502012-11-22 00:00:08 -06002242 while (resid) {
Alex Elder2fa12322013-04-05 01:27:12 -05002243 struct ceph_osd_request *osd_req;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002244 const char *object_name;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002245 u64 offset;
2246 u64 length;
2247
Alex Elder7da22d22013-01-24 16:13:36 -06002248 object_name = rbd_segment_name(rbd_dev, img_offset);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002249 if (!object_name)
2250 goto out_unwind;
Alex Elder7da22d22013-01-24 16:13:36 -06002251 offset = rbd_segment_offset(rbd_dev, img_offset);
2252 length = rbd_segment_length(rbd_dev, img_offset, resid);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002253 obj_request = rbd_obj_request_create(object_name,
Alex Elderf1a47392013-04-19 15:34:50 -05002254 offset, length, type);
Alex Elder78c2a442013-05-01 12:43:04 -05002255 /* object request has its own copy of the object name */
2256 rbd_segment_name_free(object_name);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002257 if (!obj_request)
2258 goto out_unwind;
Josh Durgin03507db2013-08-27 14:45:46 -07002259 /*
2260 * set obj_request->img_request before creating the
2261 * osd_request so that it gets the right snapc
2262 */
2263 rbd_img_obj_request_add(img_request, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002264
Alex Elderf1a47392013-04-19 15:34:50 -05002265 if (type == OBJ_REQUEST_BIO) {
2266 unsigned int clone_size;
2267
2268 rbd_assert(length <= (u64)UINT_MAX);
2269 clone_size = (unsigned int)length;
2270 obj_request->bio_list =
2271 bio_chain_clone_range(&bio_list,
2272 &bio_offset,
2273 clone_size,
2274 GFP_ATOMIC);
2275 if (!obj_request->bio_list)
2276 goto out_partial;
2277 } else {
2278 unsigned int page_count;
2279
2280 obj_request->pages = pages;
2281 page_count = (u32)calc_pages_for(offset, length);
2282 obj_request->page_count = page_count;
2283 if ((offset + length) & ~PAGE_MASK)
2284 page_count--; /* more on last page */
2285 pages += page_count;
2286 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06002287
Alex Elder2fa12322013-04-05 01:27:12 -05002288 osd_req = rbd_osd_req_create(rbd_dev, write_request,
2289 obj_request);
2290 if (!osd_req)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002291 goto out_partial;
Alex Elder2fa12322013-04-05 01:27:12 -05002292 obj_request->osd_req = osd_req;
Alex Elder21692382013-04-05 01:27:12 -05002293 obj_request->callback = rbd_img_obj_callback;
Alex Elder430c28c2013-04-03 21:32:51 -05002294
Alex Elder2fa12322013-04-05 01:27:12 -05002295 osd_req_op_extent_init(osd_req, 0, opcode, offset, length,
2296 0, 0);
Alex Elderf1a47392013-04-19 15:34:50 -05002297 if (type == OBJ_REQUEST_BIO)
2298 osd_req_op_extent_osd_data_bio(osd_req, 0,
2299 obj_request->bio_list, length);
2300 else
2301 osd_req_op_extent_osd_data_pages(osd_req, 0,
2302 obj_request->pages, length,
2303 offset & ~PAGE_MASK, false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05002304
2305 if (write_request)
2306 rbd_osd_req_format_write(obj_request);
2307 else
2308 rbd_osd_req_format_read(obj_request);
Alex Elder430c28c2013-04-03 21:32:51 -05002309
Alex Elder7da22d22013-01-24 16:13:36 -06002310 obj_request->img_offset = img_offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002311
Alex Elder7da22d22013-01-24 16:13:36 -06002312 img_offset += length;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002313 resid -= length;
2314 }
2315
2316 return 0;
2317
2318out_partial:
2319 rbd_obj_request_put(obj_request);
2320out_unwind:
2321 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
2322 rbd_obj_request_put(obj_request);
2323
2324 return -ENOMEM;
2325}
2326
Alex Elder3d7efd12013-04-19 15:34:50 -05002327static void
Alex Elder0eefd472013-04-19 15:34:50 -05002328rbd_img_obj_copyup_callback(struct rbd_obj_request *obj_request)
2329{
2330 struct rbd_img_request *img_request;
2331 struct rbd_device *rbd_dev;
Alex Elderebda6402013-05-10 16:29:22 -05002332 struct page **pages;
Alex Elder0eefd472013-04-19 15:34:50 -05002333 u32 page_count;
2334
2335 rbd_assert(obj_request->type == OBJ_REQUEST_BIO);
2336 rbd_assert(obj_request_img_data_test(obj_request));
2337 img_request = obj_request->img_request;
2338 rbd_assert(img_request);
2339
2340 rbd_dev = img_request->rbd_dev;
2341 rbd_assert(rbd_dev);
Alex Elder0eefd472013-04-19 15:34:50 -05002342
Alex Elderebda6402013-05-10 16:29:22 -05002343 pages = obj_request->copyup_pages;
2344 rbd_assert(pages != NULL);
Alex Elder0eefd472013-04-19 15:34:50 -05002345 obj_request->copyup_pages = NULL;
Alex Elderebda6402013-05-10 16:29:22 -05002346 page_count = obj_request->copyup_page_count;
2347 rbd_assert(page_count);
2348 obj_request->copyup_page_count = 0;
2349 ceph_release_page_vector(pages, page_count);
Alex Elder0eefd472013-04-19 15:34:50 -05002350
2351 /*
2352 * We want the transfer count to reflect the size of the
2353 * original write request. There is no such thing as a
2354 * successful short write, so if the request was successful
2355 * we can just set it to the originally-requested length.
2356 */
2357 if (!obj_request->result)
2358 obj_request->xferred = obj_request->length;
2359
2360 /* Finish up with the normal image object callback */
2361
2362 rbd_img_obj_callback(obj_request);
2363}
2364
2365static void
Alex Elder3d7efd12013-04-19 15:34:50 -05002366rbd_img_obj_parent_read_full_callback(struct rbd_img_request *img_request)
2367{
2368 struct rbd_obj_request *orig_request;
Alex Elder0eefd472013-04-19 15:34:50 -05002369 struct ceph_osd_request *osd_req;
2370 struct ceph_osd_client *osdc;
2371 struct rbd_device *rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002372 struct page **pages;
Alex Elderebda6402013-05-10 16:29:22 -05002373 u32 page_count;
Alex Elderbbea1c12013-05-06 17:40:33 -05002374 int img_result;
Alex Elderebda6402013-05-10 16:29:22 -05002375 u64 parent_length;
Alex Elderb91f09f2013-05-10 16:29:22 -05002376 u64 offset;
2377 u64 length;
Alex Elder3d7efd12013-04-19 15:34:50 -05002378
2379 rbd_assert(img_request_child_test(img_request));
2380
2381 /* First get what we need from the image request */
2382
2383 pages = img_request->copyup_pages;
2384 rbd_assert(pages != NULL);
2385 img_request->copyup_pages = NULL;
Alex Elderebda6402013-05-10 16:29:22 -05002386 page_count = img_request->copyup_page_count;
2387 rbd_assert(page_count);
2388 img_request->copyup_page_count = 0;
Alex Elder3d7efd12013-04-19 15:34:50 -05002389
2390 orig_request = img_request->obj_request;
2391 rbd_assert(orig_request != NULL);
Alex Elderb91f09f2013-05-10 16:29:22 -05002392 rbd_assert(obj_request_type_valid(orig_request->type));
Alex Elderbbea1c12013-05-06 17:40:33 -05002393 img_result = img_request->result;
Alex Elderebda6402013-05-10 16:29:22 -05002394 parent_length = img_request->length;
2395 rbd_assert(parent_length == img_request->xferred);
Alex Elder3d7efd12013-04-19 15:34:50 -05002396 rbd_img_request_put(img_request);
2397
Alex Elder91c6feb2013-05-06 17:40:32 -05002398 rbd_assert(orig_request->img_request);
2399 rbd_dev = orig_request->img_request->rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002400 rbd_assert(rbd_dev);
Alex Elder3d7efd12013-04-19 15:34:50 -05002401
Alex Elderbbea1c12013-05-06 17:40:33 -05002402 /*
2403 * If the overlap has become 0 (most likely because the
2404 * image has been flattened) we need to free the pages
2405 * and re-submit the original write request.
2406 */
2407 if (!rbd_dev->parent_overlap) {
2408 struct ceph_osd_client *osdc;
2409
2410 ceph_release_page_vector(pages, page_count);
2411 osdc = &rbd_dev->rbd_client->client->osdc;
2412 img_result = rbd_obj_request_submit(osdc, orig_request);
2413 if (!img_result)
2414 return;
2415 }
2416
2417 if (img_result)
Alex Elder0eefd472013-04-19 15:34:50 -05002418 goto out_err;
Alex Elder3d7efd12013-04-19 15:34:50 -05002419
Alex Elder8785b1d2013-05-09 10:08:49 -05002420 /*
2421 * The original osd request is of no use to use any more.
2422 * We need a new one that can hold the two ops in a copyup
2423 * request. Allocate the new copyup osd request for the
2424 * original request, and release the old one.
2425 */
Alex Elderbbea1c12013-05-06 17:40:33 -05002426 img_result = -ENOMEM;
Alex Elder0eefd472013-04-19 15:34:50 -05002427 osd_req = rbd_osd_req_create_copyup(orig_request);
2428 if (!osd_req)
2429 goto out_err;
Alex Elder8785b1d2013-05-09 10:08:49 -05002430 rbd_osd_req_destroy(orig_request->osd_req);
Alex Elder0eefd472013-04-19 15:34:50 -05002431 orig_request->osd_req = osd_req;
2432 orig_request->copyup_pages = pages;
Alex Elderebda6402013-05-10 16:29:22 -05002433 orig_request->copyup_page_count = page_count;
Alex Elder3d7efd12013-04-19 15:34:50 -05002434
Alex Elder0eefd472013-04-19 15:34:50 -05002435 /* Initialize the copyup op */
2436
2437 osd_req_op_cls_init(osd_req, 0, CEPH_OSD_OP_CALL, "rbd", "copyup");
Alex Elderebda6402013-05-10 16:29:22 -05002438 osd_req_op_cls_request_data_pages(osd_req, 0, pages, parent_length, 0,
Alex Elder0eefd472013-04-19 15:34:50 -05002439 false, false);
2440
2441 /* Then the original write request op */
2442
Alex Elderb91f09f2013-05-10 16:29:22 -05002443 offset = orig_request->offset;
2444 length = orig_request->length;
Alex Elder0eefd472013-04-19 15:34:50 -05002445 osd_req_op_extent_init(osd_req, 1, CEPH_OSD_OP_WRITE,
Alex Elderb91f09f2013-05-10 16:29:22 -05002446 offset, length, 0, 0);
2447 if (orig_request->type == OBJ_REQUEST_BIO)
2448 osd_req_op_extent_osd_data_bio(osd_req, 1,
2449 orig_request->bio_list, length);
2450 else
2451 osd_req_op_extent_osd_data_pages(osd_req, 1,
2452 orig_request->pages, length,
2453 offset & ~PAGE_MASK, false, false);
Alex Elder0eefd472013-04-19 15:34:50 -05002454
2455 rbd_osd_req_format_write(orig_request);
2456
2457 /* All set, send it off. */
2458
2459 orig_request->callback = rbd_img_obj_copyup_callback;
2460 osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elderbbea1c12013-05-06 17:40:33 -05002461 img_result = rbd_obj_request_submit(osdc, orig_request);
2462 if (!img_result)
Alex Elder0eefd472013-04-19 15:34:50 -05002463 return;
2464out_err:
2465 /* Record the error code and complete the request */
2466
Alex Elderbbea1c12013-05-06 17:40:33 -05002467 orig_request->result = img_result;
Alex Elder0eefd472013-04-19 15:34:50 -05002468 orig_request->xferred = 0;
2469 obj_request_done_set(orig_request);
2470 rbd_obj_request_complete(orig_request);
Alex Elder3d7efd12013-04-19 15:34:50 -05002471}
2472
2473/*
2474 * Read from the parent image the range of data that covers the
2475 * entire target of the given object request. This is used for
2476 * satisfying a layered image write request when the target of an
2477 * object request from the image request does not exist.
2478 *
2479 * A page array big enough to hold the returned data is allocated
2480 * and supplied to rbd_img_request_fill() as the "data descriptor."
2481 * When the read completes, this page array will be transferred to
2482 * the original object request for the copyup operation.
2483 *
2484 * If an error occurs, record it as the result of the original
2485 * object request and mark it done so it gets completed.
2486 */
2487static int rbd_img_obj_parent_read_full(struct rbd_obj_request *obj_request)
2488{
2489 struct rbd_img_request *img_request = NULL;
2490 struct rbd_img_request *parent_request = NULL;
2491 struct rbd_device *rbd_dev;
2492 u64 img_offset;
2493 u64 length;
2494 struct page **pages = NULL;
2495 u32 page_count;
2496 int result;
2497
2498 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elderb91f09f2013-05-10 16:29:22 -05002499 rbd_assert(obj_request_type_valid(obj_request->type));
Alex Elder3d7efd12013-04-19 15:34:50 -05002500
2501 img_request = obj_request->img_request;
2502 rbd_assert(img_request != NULL);
2503 rbd_dev = img_request->rbd_dev;
2504 rbd_assert(rbd_dev->parent != NULL);
2505
2506 /*
2507 * Determine the byte range covered by the object in the
2508 * child image to which the original request was to be sent.
2509 */
2510 img_offset = obj_request->img_offset - obj_request->offset;
2511 length = (u64)1 << rbd_dev->header.obj_order;
2512
2513 /*
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002514 * There is no defined parent data beyond the parent
2515 * overlap, so limit what we read at that boundary if
2516 * necessary.
2517 */
2518 if (img_offset + length > rbd_dev->parent_overlap) {
2519 rbd_assert(img_offset < rbd_dev->parent_overlap);
2520 length = rbd_dev->parent_overlap - img_offset;
2521 }
2522
2523 /*
Alex Elder3d7efd12013-04-19 15:34:50 -05002524 * Allocate a page array big enough to receive the data read
2525 * from the parent.
2526 */
2527 page_count = (u32)calc_pages_for(0, length);
2528 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2529 if (IS_ERR(pages)) {
2530 result = PTR_ERR(pages);
2531 pages = NULL;
2532 goto out_err;
2533 }
2534
2535 result = -ENOMEM;
Alex Eldere93f3152013-05-08 22:50:04 -05002536 parent_request = rbd_parent_request_create(obj_request,
2537 img_offset, length);
Alex Elder3d7efd12013-04-19 15:34:50 -05002538 if (!parent_request)
2539 goto out_err;
Alex Elder3d7efd12013-04-19 15:34:50 -05002540
2541 result = rbd_img_request_fill(parent_request, OBJ_REQUEST_PAGES, pages);
2542 if (result)
2543 goto out_err;
2544 parent_request->copyup_pages = pages;
Alex Elderebda6402013-05-10 16:29:22 -05002545 parent_request->copyup_page_count = page_count;
Alex Elder3d7efd12013-04-19 15:34:50 -05002546
2547 parent_request->callback = rbd_img_obj_parent_read_full_callback;
2548 result = rbd_img_request_submit(parent_request);
2549 if (!result)
2550 return 0;
2551
2552 parent_request->copyup_pages = NULL;
Alex Elderebda6402013-05-10 16:29:22 -05002553 parent_request->copyup_page_count = 0;
Alex Elder3d7efd12013-04-19 15:34:50 -05002554 parent_request->obj_request = NULL;
2555 rbd_obj_request_put(obj_request);
2556out_err:
2557 if (pages)
2558 ceph_release_page_vector(pages, page_count);
2559 if (parent_request)
2560 rbd_img_request_put(parent_request);
2561 obj_request->result = result;
2562 obj_request->xferred = 0;
2563 obj_request_done_set(obj_request);
2564
2565 return result;
2566}
2567
Alex Elderc5b5ef62013-02-11 12:33:24 -06002568static void rbd_img_obj_exists_callback(struct rbd_obj_request *obj_request)
2569{
Alex Elderc5b5ef62013-02-11 12:33:24 -06002570 struct rbd_obj_request *orig_request;
Alex Elder638f5ab2013-05-06 17:40:33 -05002571 struct rbd_device *rbd_dev;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002572 int result;
2573
2574 rbd_assert(!obj_request_img_data_test(obj_request));
2575
2576 /*
2577 * All we need from the object request is the original
2578 * request and the result of the STAT op. Grab those, then
2579 * we're done with the request.
2580 */
2581 orig_request = obj_request->obj_request;
2582 obj_request->obj_request = NULL;
Alex Elder912c3172013-05-13 20:35:38 -05002583 rbd_obj_request_put(orig_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002584 rbd_assert(orig_request);
2585 rbd_assert(orig_request->img_request);
2586
2587 result = obj_request->result;
2588 obj_request->result = 0;
2589
2590 dout("%s: obj %p for obj %p result %d %llu/%llu\n", __func__,
2591 obj_request, orig_request, result,
2592 obj_request->xferred, obj_request->length);
2593 rbd_obj_request_put(obj_request);
2594
Alex Elder638f5ab2013-05-06 17:40:33 -05002595 /*
2596 * If the overlap has become 0 (most likely because the
2597 * image has been flattened) we need to free the pages
2598 * and re-submit the original write request.
2599 */
2600 rbd_dev = orig_request->img_request->rbd_dev;
2601 if (!rbd_dev->parent_overlap) {
2602 struct ceph_osd_client *osdc;
2603
Alex Elder638f5ab2013-05-06 17:40:33 -05002604 osdc = &rbd_dev->rbd_client->client->osdc;
2605 result = rbd_obj_request_submit(osdc, orig_request);
2606 if (!result)
2607 return;
2608 }
Alex Elderc5b5ef62013-02-11 12:33:24 -06002609
2610 /*
2611 * Our only purpose here is to determine whether the object
2612 * exists, and we don't want to treat the non-existence as
2613 * an error. If something else comes back, transfer the
2614 * error to the original request and complete it now.
2615 */
2616 if (!result) {
2617 obj_request_existence_set(orig_request, true);
2618 } else if (result == -ENOENT) {
2619 obj_request_existence_set(orig_request, false);
2620 } else if (result) {
2621 orig_request->result = result;
Alex Elder3d7efd12013-04-19 15:34:50 -05002622 goto out;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002623 }
2624
2625 /*
2626 * Resubmit the original request now that we have recorded
2627 * whether the target object exists.
2628 */
Alex Elderb454e362013-04-19 15:34:50 -05002629 orig_request->result = rbd_img_obj_request_submit(orig_request);
Alex Elder3d7efd12013-04-19 15:34:50 -05002630out:
Alex Elderc5b5ef62013-02-11 12:33:24 -06002631 if (orig_request->result)
2632 rbd_obj_request_complete(orig_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002633}
2634
2635static int rbd_img_obj_exists_submit(struct rbd_obj_request *obj_request)
2636{
2637 struct rbd_obj_request *stat_request;
2638 struct rbd_device *rbd_dev;
2639 struct ceph_osd_client *osdc;
2640 struct page **pages = NULL;
2641 u32 page_count;
2642 size_t size;
2643 int ret;
2644
2645 /*
2646 * The response data for a STAT call consists of:
2647 * le64 length;
2648 * struct {
2649 * le32 tv_sec;
2650 * le32 tv_nsec;
2651 * } mtime;
2652 */
2653 size = sizeof (__le64) + sizeof (__le32) + sizeof (__le32);
2654 page_count = (u32)calc_pages_for(0, size);
2655 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2656 if (IS_ERR(pages))
2657 return PTR_ERR(pages);
2658
2659 ret = -ENOMEM;
2660 stat_request = rbd_obj_request_create(obj_request->object_name, 0, 0,
2661 OBJ_REQUEST_PAGES);
2662 if (!stat_request)
2663 goto out;
2664
2665 rbd_obj_request_get(obj_request);
2666 stat_request->obj_request = obj_request;
2667 stat_request->pages = pages;
2668 stat_request->page_count = page_count;
2669
2670 rbd_assert(obj_request->img_request);
2671 rbd_dev = obj_request->img_request->rbd_dev;
2672 stat_request->osd_req = rbd_osd_req_create(rbd_dev, false,
2673 stat_request);
2674 if (!stat_request->osd_req)
2675 goto out;
2676 stat_request->callback = rbd_img_obj_exists_callback;
2677
2678 osd_req_op_init(stat_request->osd_req, 0, CEPH_OSD_OP_STAT);
2679 osd_req_op_raw_data_in_pages(stat_request->osd_req, 0, pages, size, 0,
2680 false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05002681 rbd_osd_req_format_read(stat_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002682
2683 osdc = &rbd_dev->rbd_client->client->osdc;
2684 ret = rbd_obj_request_submit(osdc, stat_request);
2685out:
2686 if (ret)
2687 rbd_obj_request_put(obj_request);
2688
2689 return ret;
2690}
2691
Alex Elderb454e362013-04-19 15:34:50 -05002692static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request)
2693{
2694 struct rbd_img_request *img_request;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002695 struct rbd_device *rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002696 bool known;
Alex Elderb454e362013-04-19 15:34:50 -05002697
2698 rbd_assert(obj_request_img_data_test(obj_request));
2699
2700 img_request = obj_request->img_request;
2701 rbd_assert(img_request);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002702 rbd_dev = img_request->rbd_dev;
Alex Elderb454e362013-04-19 15:34:50 -05002703
Alex Elderb454e362013-04-19 15:34:50 -05002704 /*
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002705 * Only writes to layered images need special handling.
2706 * Reads and non-layered writes are simple object requests.
2707 * Layered writes that start beyond the end of the overlap
2708 * with the parent have no parent data, so they too are
2709 * simple object requests. Finally, if the target object is
2710 * known to already exist, its parent data has already been
2711 * copied, so a write to the object can also be handled as a
2712 * simple object request.
Alex Elderb454e362013-04-19 15:34:50 -05002713 */
2714 if (!img_request_write_test(img_request) ||
2715 !img_request_layered_test(img_request) ||
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002716 rbd_dev->parent_overlap <= obj_request->img_offset ||
Alex Elder3d7efd12013-04-19 15:34:50 -05002717 ((known = obj_request_known_test(obj_request)) &&
2718 obj_request_exists_test(obj_request))) {
Alex Elderb454e362013-04-19 15:34:50 -05002719
2720 struct rbd_device *rbd_dev;
2721 struct ceph_osd_client *osdc;
2722
2723 rbd_dev = obj_request->img_request->rbd_dev;
2724 osdc = &rbd_dev->rbd_client->client->osdc;
2725
2726 return rbd_obj_request_submit(osdc, obj_request);
2727 }
2728
2729 /*
Alex Elder3d7efd12013-04-19 15:34:50 -05002730 * It's a layered write. The target object might exist but
2731 * we may not know that yet. If we know it doesn't exist,
2732 * start by reading the data for the full target object from
2733 * the parent so we can use it for a copyup to the target.
Alex Elderb454e362013-04-19 15:34:50 -05002734 */
Alex Elder3d7efd12013-04-19 15:34:50 -05002735 if (known)
2736 return rbd_img_obj_parent_read_full(obj_request);
2737
2738 /* We don't know whether the target exists. Go find out. */
Alex Elderb454e362013-04-19 15:34:50 -05002739
2740 return rbd_img_obj_exists_submit(obj_request);
2741}
2742
Alex Elderbf0d5f502012-11-22 00:00:08 -06002743static int rbd_img_request_submit(struct rbd_img_request *img_request)
2744{
Alex Elderbf0d5f502012-11-22 00:00:08 -06002745 struct rbd_obj_request *obj_request;
Alex Elder46faeed2013-04-10 17:47:46 -05002746 struct rbd_obj_request *next_obj_request;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002747
Alex Elder37206ee2013-02-20 17:32:08 -06002748 dout("%s: img %p\n", __func__, img_request);
Alex Elder46faeed2013-04-10 17:47:46 -05002749 for_each_obj_request_safe(img_request, obj_request, next_obj_request) {
Alex Elderbf0d5f502012-11-22 00:00:08 -06002750 int ret;
2751
Alex Elderb454e362013-04-19 15:34:50 -05002752 ret = rbd_img_obj_request_submit(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002753 if (ret)
2754 return ret;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002755 }
2756
2757 return 0;
2758}
2759
Alex Elder8b3e1a52013-01-24 16:13:36 -06002760static void rbd_img_parent_read_callback(struct rbd_img_request *img_request)
2761{
2762 struct rbd_obj_request *obj_request;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002763 struct rbd_device *rbd_dev;
2764 u64 obj_end;
Alex Elder02c74fb2013-05-06 17:40:33 -05002765 u64 img_xferred;
2766 int img_result;
Alex Elder8b3e1a52013-01-24 16:13:36 -06002767
2768 rbd_assert(img_request_child_test(img_request));
2769
Alex Elder02c74fb2013-05-06 17:40:33 -05002770 /* First get what we need from the image request and release it */
2771
Alex Elder8b3e1a52013-01-24 16:13:36 -06002772 obj_request = img_request->obj_request;
Alex Elder02c74fb2013-05-06 17:40:33 -05002773 img_xferred = img_request->xferred;
2774 img_result = img_request->result;
2775 rbd_img_request_put(img_request);
2776
2777 /*
2778 * If the overlap has become 0 (most likely because the
2779 * image has been flattened) we need to re-submit the
2780 * original request.
2781 */
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002782 rbd_assert(obj_request);
2783 rbd_assert(obj_request->img_request);
Alex Elder02c74fb2013-05-06 17:40:33 -05002784 rbd_dev = obj_request->img_request->rbd_dev;
2785 if (!rbd_dev->parent_overlap) {
2786 struct ceph_osd_client *osdc;
Alex Elder8b3e1a52013-01-24 16:13:36 -06002787
Alex Elder02c74fb2013-05-06 17:40:33 -05002788 osdc = &rbd_dev->rbd_client->client->osdc;
2789 img_result = rbd_obj_request_submit(osdc, obj_request);
2790 if (!img_result)
2791 return;
2792 }
2793
2794 obj_request->result = img_result;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002795 if (obj_request->result)
2796 goto out;
2797
2798 /*
2799 * We need to zero anything beyond the parent overlap
2800 * boundary. Since rbd_img_obj_request_read_callback()
2801 * will zero anything beyond the end of a short read, an
2802 * easy way to do this is to pretend the data from the
2803 * parent came up short--ending at the overlap boundary.
2804 */
2805 rbd_assert(obj_request->img_offset < U64_MAX - obj_request->length);
2806 obj_end = obj_request->img_offset + obj_request->length;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002807 if (obj_end > rbd_dev->parent_overlap) {
2808 u64 xferred = 0;
2809
2810 if (obj_request->img_offset < rbd_dev->parent_overlap)
2811 xferred = rbd_dev->parent_overlap -
2812 obj_request->img_offset;
2813
Alex Elder02c74fb2013-05-06 17:40:33 -05002814 obj_request->xferred = min(img_xferred, xferred);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002815 } else {
Alex Elder02c74fb2013-05-06 17:40:33 -05002816 obj_request->xferred = img_xferred;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002817 }
2818out:
Alex Elder8b3e1a52013-01-24 16:13:36 -06002819 rbd_img_obj_request_read_callback(obj_request);
2820 rbd_obj_request_complete(obj_request);
2821}
2822
2823static void rbd_img_parent_read(struct rbd_obj_request *obj_request)
2824{
Alex Elder8b3e1a52013-01-24 16:13:36 -06002825 struct rbd_img_request *img_request;
2826 int result;
2827
2828 rbd_assert(obj_request_img_data_test(obj_request));
2829 rbd_assert(obj_request->img_request != NULL);
2830 rbd_assert(obj_request->result == (s32) -ENOENT);
Alex Elder5b2ab722013-05-06 17:40:33 -05002831 rbd_assert(obj_request_type_valid(obj_request->type));
Alex Elder8b3e1a52013-01-24 16:13:36 -06002832
Alex Elder8b3e1a52013-01-24 16:13:36 -06002833 /* rbd_read_finish(obj_request, obj_request->length); */
Alex Eldere93f3152013-05-08 22:50:04 -05002834 img_request = rbd_parent_request_create(obj_request,
Alex Elder8b3e1a52013-01-24 16:13:36 -06002835 obj_request->img_offset,
Alex Eldere93f3152013-05-08 22:50:04 -05002836 obj_request->length);
Alex Elder8b3e1a52013-01-24 16:13:36 -06002837 result = -ENOMEM;
2838 if (!img_request)
2839 goto out_err;
2840
Alex Elder5b2ab722013-05-06 17:40:33 -05002841 if (obj_request->type == OBJ_REQUEST_BIO)
2842 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
2843 obj_request->bio_list);
2844 else
2845 result = rbd_img_request_fill(img_request, OBJ_REQUEST_PAGES,
2846 obj_request->pages);
Alex Elder8b3e1a52013-01-24 16:13:36 -06002847 if (result)
2848 goto out_err;
2849
2850 img_request->callback = rbd_img_parent_read_callback;
2851 result = rbd_img_request_submit(img_request);
2852 if (result)
2853 goto out_err;
2854
2855 return;
2856out_err:
2857 if (img_request)
2858 rbd_img_request_put(img_request);
2859 obj_request->result = result;
2860 obj_request->xferred = 0;
2861 obj_request_done_set(obj_request);
2862}
2863
Josh Durgin20e0af62013-08-29 17:36:03 -07002864static int rbd_obj_notify_ack_sync(struct rbd_device *rbd_dev, u64 notify_id)
Alex Elderb8d70032012-11-30 17:53:04 -06002865{
2866 struct rbd_obj_request *obj_request;
Alex Elder21692382013-04-05 01:27:12 -05002867 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elderb8d70032012-11-30 17:53:04 -06002868 int ret;
2869
2870 obj_request = rbd_obj_request_create(rbd_dev->header_name, 0, 0,
2871 OBJ_REQUEST_NODATA);
2872 if (!obj_request)
2873 return -ENOMEM;
2874
2875 ret = -ENOMEM;
Alex Elder430c28c2013-04-03 21:32:51 -05002876 obj_request->osd_req = rbd_osd_req_create(rbd_dev, false, obj_request);
Alex Elderb8d70032012-11-30 17:53:04 -06002877 if (!obj_request->osd_req)
2878 goto out;
2879
Alex Elderc99d2d42013-04-05 01:27:11 -05002880 osd_req_op_watch_init(obj_request->osd_req, 0, CEPH_OSD_OP_NOTIFY_ACK,
Alex Eldercc4a38bd2013-04-30 00:44:33 -05002881 notify_id, 0, 0);
Alex Elder9d4df012013-04-19 15:34:50 -05002882 rbd_osd_req_format_read(obj_request);
Alex Elder430c28c2013-04-03 21:32:51 -05002883
Alex Elderb8d70032012-11-30 17:53:04 -06002884 ret = rbd_obj_request_submit(osdc, obj_request);
Alex Eldercf81b602013-01-17 12:18:46 -06002885 if (ret)
Josh Durgin20e0af62013-08-29 17:36:03 -07002886 goto out;
2887 ret = rbd_obj_request_wait(obj_request);
2888out:
2889 rbd_obj_request_put(obj_request);
Alex Elderb8d70032012-11-30 17:53:04 -06002890
2891 return ret;
2892}
2893
2894static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
2895{
2896 struct rbd_device *rbd_dev = (struct rbd_device *)data;
Alex Eldere627db02013-05-06 07:40:30 -05002897 int ret;
Alex Elderb8d70032012-11-30 17:53:04 -06002898
2899 if (!rbd_dev)
2900 return;
2901
Alex Elder37206ee2013-02-20 17:32:08 -06002902 dout("%s: \"%s\" notify_id %llu opcode %u\n", __func__,
Alex Eldercc4a38bd2013-04-30 00:44:33 -05002903 rbd_dev->header_name, (unsigned long long)notify_id,
2904 (unsigned int)opcode);
Alex Eldere627db02013-05-06 07:40:30 -05002905 ret = rbd_dev_refresh(rbd_dev);
2906 if (ret)
Alex Elder3b5cf2a2013-05-29 11:18:59 -05002907 rbd_warn(rbd_dev, "header refresh error (%d)\n", ret);
Alex Elderb8d70032012-11-30 17:53:04 -06002908
Josh Durgin20e0af62013-08-29 17:36:03 -07002909 rbd_obj_notify_ack_sync(rbd_dev, notify_id);
Alex Elderb8d70032012-11-30 17:53:04 -06002910}
2911
Alex Elder9969ebc2013-01-18 12:31:10 -06002912/*
2913 * Request sync osd watch/unwatch. The value of "start" determines
2914 * whether a watch request is being initiated or torn down.
2915 */
Ilya Dryomovfca27062013-12-16 18:02:40 +02002916static int __rbd_dev_header_watch_sync(struct rbd_device *rbd_dev, bool start)
Alex Elder9969ebc2013-01-18 12:31:10 -06002917{
2918 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
2919 struct rbd_obj_request *obj_request;
Alex Elder9969ebc2013-01-18 12:31:10 -06002920 int ret;
2921
2922 rbd_assert(start ^ !!rbd_dev->watch_event);
2923 rbd_assert(start ^ !!rbd_dev->watch_request);
2924
2925 if (start) {
Alex Elder3c663bb2013-02-15 11:42:30 -06002926 ret = ceph_osdc_create_event(osdc, rbd_watch_cb, rbd_dev,
Alex Elder9969ebc2013-01-18 12:31:10 -06002927 &rbd_dev->watch_event);
2928 if (ret < 0)
2929 return ret;
Alex Elder8eb87562013-01-25 17:08:55 -06002930 rbd_assert(rbd_dev->watch_event != NULL);
Alex Elder9969ebc2013-01-18 12:31:10 -06002931 }
2932
2933 ret = -ENOMEM;
2934 obj_request = rbd_obj_request_create(rbd_dev->header_name, 0, 0,
2935 OBJ_REQUEST_NODATA);
2936 if (!obj_request)
2937 goto out_cancel;
2938
Alex Elder430c28c2013-04-03 21:32:51 -05002939 obj_request->osd_req = rbd_osd_req_create(rbd_dev, true, obj_request);
2940 if (!obj_request->osd_req)
2941 goto out_cancel;
2942
Alex Elder8eb87562013-01-25 17:08:55 -06002943 if (start)
Alex Elder975241a2013-01-25 17:08:55 -06002944 ceph_osdc_set_request_linger(osdc, obj_request->osd_req);
Alex Elder8eb87562013-01-25 17:08:55 -06002945 else
Alex Elder6977c3f2013-01-25 17:08:55 -06002946 ceph_osdc_unregister_linger_request(osdc,
Alex Elder975241a2013-01-25 17:08:55 -06002947 rbd_dev->watch_request->osd_req);
Alex Elder21692382013-04-05 01:27:12 -05002948
2949 osd_req_op_watch_init(obj_request->osd_req, 0, CEPH_OSD_OP_WATCH,
Alex Elder1f3ef782013-05-06 17:40:33 -05002950 rbd_dev->watch_event->cookie, 0, start ? 1 : 0);
Alex Elder9d4df012013-04-19 15:34:50 -05002951 rbd_osd_req_format_write(obj_request);
Alex Elder21692382013-04-05 01:27:12 -05002952
Alex Elder9969ebc2013-01-18 12:31:10 -06002953 ret = rbd_obj_request_submit(osdc, obj_request);
2954 if (ret)
2955 goto out_cancel;
2956 ret = rbd_obj_request_wait(obj_request);
2957 if (ret)
2958 goto out_cancel;
Alex Elder9969ebc2013-01-18 12:31:10 -06002959 ret = obj_request->result;
2960 if (ret)
2961 goto out_cancel;
2962
Alex Elder8eb87562013-01-25 17:08:55 -06002963 /*
2964 * A watch request is set to linger, so the underlying osd
2965 * request won't go away until we unregister it. We retain
2966 * a pointer to the object request during that time (in
2967 * rbd_dev->watch_request), so we'll keep a reference to
2968 * it. We'll drop that reference (below) after we've
2969 * unregistered it.
2970 */
2971 if (start) {
2972 rbd_dev->watch_request = obj_request;
2973
2974 return 0;
2975 }
2976
2977 /* We have successfully torn down the watch request */
2978
2979 rbd_obj_request_put(rbd_dev->watch_request);
2980 rbd_dev->watch_request = NULL;
Alex Elder9969ebc2013-01-18 12:31:10 -06002981out_cancel:
2982 /* Cancel the event if we're tearing down, or on error */
2983 ceph_osdc_cancel_event(rbd_dev->watch_event);
2984 rbd_dev->watch_event = NULL;
Alex Elder9969ebc2013-01-18 12:31:10 -06002985 if (obj_request)
2986 rbd_obj_request_put(obj_request);
2987
2988 return ret;
2989}
2990
Ilya Dryomovfca27062013-12-16 18:02:40 +02002991static int rbd_dev_header_watch_sync(struct rbd_device *rbd_dev)
2992{
2993 return __rbd_dev_header_watch_sync(rbd_dev, true);
2994}
2995
2996static void rbd_dev_header_unwatch_sync(struct rbd_device *rbd_dev)
2997{
2998 int ret;
2999
3000 ret = __rbd_dev_header_watch_sync(rbd_dev, false);
3001 if (ret) {
3002 rbd_warn(rbd_dev, "unable to tear down watch request: %d\n",
3003 ret);
3004 }
3005}
3006
Alex Elder36be9a72013-01-19 00:30:28 -06003007/*
Alex Elderf40eb342013-04-25 15:09:42 -05003008 * Synchronous osd object method call. Returns the number of bytes
3009 * returned in the outbound buffer, or a negative error code.
Alex Elder36be9a72013-01-19 00:30:28 -06003010 */
3011static int rbd_obj_method_sync(struct rbd_device *rbd_dev,
3012 const char *object_name,
3013 const char *class_name,
3014 const char *method_name,
Alex Elder41579762013-04-21 12:14:45 -05003015 const void *outbound,
Alex Elder36be9a72013-01-19 00:30:28 -06003016 size_t outbound_size,
Alex Elder41579762013-04-21 12:14:45 -05003017 void *inbound,
Alex Eldere2a58ee2013-04-30 00:44:33 -05003018 size_t inbound_size)
Alex Elder36be9a72013-01-19 00:30:28 -06003019{
Alex Elder21692382013-04-05 01:27:12 -05003020 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder36be9a72013-01-19 00:30:28 -06003021 struct rbd_obj_request *obj_request;
Alex Elder36be9a72013-01-19 00:30:28 -06003022 struct page **pages;
3023 u32 page_count;
3024 int ret;
3025
3026 /*
Alex Elder6010a452013-04-05 01:27:11 -05003027 * Method calls are ultimately read operations. The result
3028 * should placed into the inbound buffer provided. They
3029 * also supply outbound data--parameters for the object
3030 * method. Currently if this is present it will be a
3031 * snapshot id.
Alex Elder36be9a72013-01-19 00:30:28 -06003032 */
Alex Elder57385b52013-04-21 12:14:45 -05003033 page_count = (u32)calc_pages_for(0, inbound_size);
Alex Elder36be9a72013-01-19 00:30:28 -06003034 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
3035 if (IS_ERR(pages))
3036 return PTR_ERR(pages);
3037
3038 ret = -ENOMEM;
Alex Elder6010a452013-04-05 01:27:11 -05003039 obj_request = rbd_obj_request_create(object_name, 0, inbound_size,
Alex Elder36be9a72013-01-19 00:30:28 -06003040 OBJ_REQUEST_PAGES);
3041 if (!obj_request)
3042 goto out;
3043
3044 obj_request->pages = pages;
3045 obj_request->page_count = page_count;
3046
Alex Elder430c28c2013-04-03 21:32:51 -05003047 obj_request->osd_req = rbd_osd_req_create(rbd_dev, false, obj_request);
Alex Elder36be9a72013-01-19 00:30:28 -06003048 if (!obj_request->osd_req)
3049 goto out;
3050
Alex Elderc99d2d42013-04-05 01:27:11 -05003051 osd_req_op_cls_init(obj_request->osd_req, 0, CEPH_OSD_OP_CALL,
Alex Elder04017e22013-04-05 14:46:02 -05003052 class_name, method_name);
3053 if (outbound_size) {
3054 struct ceph_pagelist *pagelist;
3055
3056 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
3057 if (!pagelist)
3058 goto out;
3059
3060 ceph_pagelist_init(pagelist);
3061 ceph_pagelist_append(pagelist, outbound, outbound_size);
3062 osd_req_op_cls_request_data_pagelist(obj_request->osd_req, 0,
3063 pagelist);
3064 }
Alex Eldera4ce40a2013-04-05 01:27:12 -05003065 osd_req_op_cls_response_data_pages(obj_request->osd_req, 0,
3066 obj_request->pages, inbound_size,
Alex Elder44cd1882013-04-05 01:27:12 -05003067 0, false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05003068 rbd_osd_req_format_read(obj_request);
Alex Elder430c28c2013-04-03 21:32:51 -05003069
Alex Elder36be9a72013-01-19 00:30:28 -06003070 ret = rbd_obj_request_submit(osdc, obj_request);
3071 if (ret)
3072 goto out;
3073 ret = rbd_obj_request_wait(obj_request);
3074 if (ret)
3075 goto out;
3076
3077 ret = obj_request->result;
3078 if (ret < 0)
3079 goto out;
Alex Elder57385b52013-04-21 12:14:45 -05003080
3081 rbd_assert(obj_request->xferred < (u64)INT_MAX);
3082 ret = (int)obj_request->xferred;
Alex Elder903bb322013-02-06 13:11:38 -06003083 ceph_copy_from_page_vector(pages, inbound, 0, obj_request->xferred);
Alex Elder36be9a72013-01-19 00:30:28 -06003084out:
3085 if (obj_request)
3086 rbd_obj_request_put(obj_request);
3087 else
3088 ceph_release_page_vector(pages, page_count);
3089
3090 return ret;
3091}
3092
Alex Elderbf0d5f502012-11-22 00:00:08 -06003093static void rbd_request_fn(struct request_queue *q)
Alex Eldercc344fa2013-02-19 12:25:56 -06003094 __releases(q->queue_lock) __acquires(q->queue_lock)
Alex Elderbf0d5f502012-11-22 00:00:08 -06003095{
3096 struct rbd_device *rbd_dev = q->queuedata;
3097 bool read_only = rbd_dev->mapping.read_only;
3098 struct request *rq;
3099 int result;
3100
3101 while ((rq = blk_fetch_request(q))) {
3102 bool write_request = rq_data_dir(rq) == WRITE;
3103 struct rbd_img_request *img_request;
3104 u64 offset;
3105 u64 length;
3106
3107 /* Ignore any non-FS requests that filter through. */
3108
3109 if (rq->cmd_type != REQ_TYPE_FS) {
Alex Elder4dda41d2013-02-20 21:59:33 -06003110 dout("%s: non-fs request type %d\n", __func__,
3111 (int) rq->cmd_type);
3112 __blk_end_request_all(rq, 0);
3113 continue;
3114 }
3115
3116 /* Ignore/skip any zero-length requests */
3117
3118 offset = (u64) blk_rq_pos(rq) << SECTOR_SHIFT;
3119 length = (u64) blk_rq_bytes(rq);
3120
3121 if (!length) {
3122 dout("%s: zero-length request\n", __func__);
Alex Elderbf0d5f502012-11-22 00:00:08 -06003123 __blk_end_request_all(rq, 0);
3124 continue;
3125 }
3126
3127 spin_unlock_irq(q->queue_lock);
3128
3129 /* Disallow writes to a read-only device */
3130
3131 if (write_request) {
3132 result = -EROFS;
3133 if (read_only)
3134 goto end_request;
3135 rbd_assert(rbd_dev->spec->snap_id == CEPH_NOSNAP);
3136 }
3137
Alex Elder6d292902013-01-14 12:43:31 -06003138 /*
3139 * Quit early if the mapped snapshot no longer
3140 * exists. It's still possible the snapshot will
3141 * have disappeared by the time our request arrives
3142 * at the osd, but there's no sense in sending it if
3143 * we already know.
3144 */
3145 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) {
Alex Elderbf0d5f502012-11-22 00:00:08 -06003146 dout("request for non-existent snapshot");
3147 rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP);
3148 result = -ENXIO;
3149 goto end_request;
3150 }
3151
Alex Elderbf0d5f502012-11-22 00:00:08 -06003152 result = -EINVAL;
Alex Elderc0cd10db2013-04-26 09:43:47 -05003153 if (offset && length > U64_MAX - offset + 1) {
3154 rbd_warn(rbd_dev, "bad request range (%llu~%llu)\n",
3155 offset, length);
Alex Elderbf0d5f502012-11-22 00:00:08 -06003156 goto end_request; /* Shouldn't happen */
Alex Elderc0cd10db2013-04-26 09:43:47 -05003157 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06003158
Alex Elder00a653e2013-05-06 07:40:30 -05003159 result = -EIO;
3160 if (offset + length > rbd_dev->mapping.size) {
3161 rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)\n",
3162 offset, length, rbd_dev->mapping.size);
3163 goto end_request;
3164 }
3165
Alex Elderbf0d5f502012-11-22 00:00:08 -06003166 result = -ENOMEM;
3167 img_request = rbd_img_request_create(rbd_dev, offset, length,
Alex Eldere93f3152013-05-08 22:50:04 -05003168 write_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06003169 if (!img_request)
3170 goto end_request;
3171
3172 img_request->rq = rq;
3173
Alex Elderf1a47392013-04-19 15:34:50 -05003174 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
3175 rq->bio);
Alex Elderbf0d5f502012-11-22 00:00:08 -06003176 if (!result)
3177 result = rbd_img_request_submit(img_request);
3178 if (result)
3179 rbd_img_request_put(img_request);
3180end_request:
3181 spin_lock_irq(q->queue_lock);
3182 if (result < 0) {
Alex Elder7da22d22013-01-24 16:13:36 -06003183 rbd_warn(rbd_dev, "%s %llx at %llx result %d\n",
3184 write_request ? "write" : "read",
3185 length, offset, result);
3186
Alex Elderbf0d5f502012-11-22 00:00:08 -06003187 __blk_end_request_all(rq, result);
3188 }
3189 }
3190}
3191
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003192/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003193 * a queue callback. Makes sure that we don't create a bio that spans across
3194 * multiple osd objects. One exception would be with a single page bios,
Alex Elderf7760da2012-10-20 22:17:27 -05003195 * which we handle later at bio_chain_clone_range()
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003196 */
3197static int rbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd,
3198 struct bio_vec *bvec)
3199{
3200 struct rbd_device *rbd_dev = q->queuedata;
Alex Eldere5cfeed22012-10-20 22:17:27 -05003201 sector_t sector_offset;
3202 sector_t sectors_per_obj;
3203 sector_t obj_sector_offset;
3204 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003205
Alex Eldere5cfeed22012-10-20 22:17:27 -05003206 /*
3207 * Find how far into its rbd object the partition-relative
3208 * bio start sector is to offset relative to the enclosing
3209 * device.
3210 */
3211 sector_offset = get_start_sect(bmd->bi_bdev) + bmd->bi_sector;
3212 sectors_per_obj = 1 << (rbd_dev->header.obj_order - SECTOR_SHIFT);
3213 obj_sector_offset = sector_offset & (sectors_per_obj - 1);
Alex Elder593a9e72012-02-07 12:03:37 -06003214
Alex Eldere5cfeed22012-10-20 22:17:27 -05003215 /*
3216 * Compute the number of bytes from that offset to the end
3217 * of the object. Account for what's already used by the bio.
3218 */
3219 ret = (int) (sectors_per_obj - obj_sector_offset) << SECTOR_SHIFT;
3220 if (ret > bmd->bi_size)
3221 ret -= bmd->bi_size;
3222 else
3223 ret = 0;
3224
3225 /*
3226 * Don't send back more than was asked for. And if the bio
3227 * was empty, let the whole thing through because: "Note
3228 * that a block device *must* allow a single page to be
3229 * added to an empty bio."
3230 */
3231 rbd_assert(bvec->bv_len <= PAGE_SIZE);
3232 if (ret > (int) bvec->bv_len || !bmd->bi_size)
3233 ret = (int) bvec->bv_len;
3234
3235 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003236}
3237
3238static void rbd_free_disk(struct rbd_device *rbd_dev)
3239{
3240 struct gendisk *disk = rbd_dev->disk;
3241
3242 if (!disk)
3243 return;
3244
Alex Eldera0cab922013-04-25 23:15:08 -05003245 rbd_dev->disk = NULL;
3246 if (disk->flags & GENHD_FL_UP) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003247 del_gendisk(disk);
Alex Eldera0cab922013-04-25 23:15:08 -05003248 if (disk->queue)
3249 blk_cleanup_queue(disk->queue);
3250 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003251 put_disk(disk);
3252}
3253
Alex Elder788e2df2013-01-17 12:25:27 -06003254static int rbd_obj_read_sync(struct rbd_device *rbd_dev,
3255 const char *object_name,
Alex Elder7097f8d2013-04-30 00:44:33 -05003256 u64 offset, u64 length, void *buf)
Alex Elder788e2df2013-01-17 12:25:27 -06003257
3258{
Alex Elder21692382013-04-05 01:27:12 -05003259 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder788e2df2013-01-17 12:25:27 -06003260 struct rbd_obj_request *obj_request;
Alex Elder788e2df2013-01-17 12:25:27 -06003261 struct page **pages = NULL;
3262 u32 page_count;
Alex Elder1ceae7e2013-02-06 13:11:38 -06003263 size_t size;
Alex Elder788e2df2013-01-17 12:25:27 -06003264 int ret;
3265
3266 page_count = (u32) calc_pages_for(offset, length);
3267 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
3268 if (IS_ERR(pages))
3269 ret = PTR_ERR(pages);
3270
3271 ret = -ENOMEM;
3272 obj_request = rbd_obj_request_create(object_name, offset, length,
Alex Elder36be9a72013-01-19 00:30:28 -06003273 OBJ_REQUEST_PAGES);
Alex Elder788e2df2013-01-17 12:25:27 -06003274 if (!obj_request)
3275 goto out;
3276
3277 obj_request->pages = pages;
3278 obj_request->page_count = page_count;
3279
Alex Elder430c28c2013-04-03 21:32:51 -05003280 obj_request->osd_req = rbd_osd_req_create(rbd_dev, false, obj_request);
Alex Elder788e2df2013-01-17 12:25:27 -06003281 if (!obj_request->osd_req)
3282 goto out;
3283
Alex Elderc99d2d42013-04-05 01:27:11 -05003284 osd_req_op_extent_init(obj_request->osd_req, 0, CEPH_OSD_OP_READ,
3285 offset, length, 0, 0);
Alex Elder406e2c92013-04-15 14:50:36 -05003286 osd_req_op_extent_osd_data_pages(obj_request->osd_req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05003287 obj_request->pages,
Alex Elder44cd1882013-04-05 01:27:12 -05003288 obj_request->length,
3289 obj_request->offset & ~PAGE_MASK,
3290 false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05003291 rbd_osd_req_format_read(obj_request);
Alex Elder430c28c2013-04-03 21:32:51 -05003292
Alex Elder788e2df2013-01-17 12:25:27 -06003293 ret = rbd_obj_request_submit(osdc, obj_request);
3294 if (ret)
3295 goto out;
3296 ret = rbd_obj_request_wait(obj_request);
3297 if (ret)
3298 goto out;
3299
3300 ret = obj_request->result;
3301 if (ret < 0)
3302 goto out;
Alex Elder1ceae7e2013-02-06 13:11:38 -06003303
3304 rbd_assert(obj_request->xferred <= (u64) SIZE_MAX);
3305 size = (size_t) obj_request->xferred;
Alex Elder903bb322013-02-06 13:11:38 -06003306 ceph_copy_from_page_vector(pages, buf, 0, size);
Alex Elder7097f8d2013-04-30 00:44:33 -05003307 rbd_assert(size <= (size_t)INT_MAX);
3308 ret = (int)size;
Alex Elder788e2df2013-01-17 12:25:27 -06003309out:
3310 if (obj_request)
3311 rbd_obj_request_put(obj_request);
3312 else
3313 ceph_release_page_vector(pages, page_count);
3314
3315 return ret;
3316}
3317
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003318/*
Alex Elder662518b2013-05-06 09:51:29 -05003319 * Read the complete header for the given rbd device. On successful
3320 * return, the rbd_dev->header field will contain up-to-date
3321 * information about the image.
Alex Elder4156d992012-08-02 11:29:46 -05003322 */
Alex Elder99a41eb2013-05-06 09:51:30 -05003323static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev)
Alex Elder4156d992012-08-02 11:29:46 -05003324{
3325 struct rbd_image_header_ondisk *ondisk = NULL;
3326 u32 snap_count = 0;
3327 u64 names_size = 0;
3328 u32 want_count;
3329 int ret;
3330
3331 /*
3332 * The complete header will include an array of its 64-bit
3333 * snapshot ids, followed by the names of those snapshots as
3334 * a contiguous block of NUL-terminated strings. Note that
3335 * the number of snapshots could change by the time we read
3336 * it in, in which case we re-read it.
3337 */
3338 do {
3339 size_t size;
3340
3341 kfree(ondisk);
3342
3343 size = sizeof (*ondisk);
3344 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
3345 size += names_size;
3346 ondisk = kmalloc(size, GFP_KERNEL);
3347 if (!ondisk)
Alex Elder662518b2013-05-06 09:51:29 -05003348 return -ENOMEM;
Alex Elder4156d992012-08-02 11:29:46 -05003349
Alex Elder788e2df2013-01-17 12:25:27 -06003350 ret = rbd_obj_read_sync(rbd_dev, rbd_dev->header_name,
Alex Elder7097f8d2013-04-30 00:44:33 -05003351 0, size, ondisk);
Alex Elder4156d992012-08-02 11:29:46 -05003352 if (ret < 0)
Alex Elder662518b2013-05-06 09:51:29 -05003353 goto out;
Alex Elderc0cd10db2013-04-26 09:43:47 -05003354 if ((size_t)ret < size) {
Alex Elder4156d992012-08-02 11:29:46 -05003355 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05003356 rbd_warn(rbd_dev, "short header read (want %zd got %d)",
3357 size, ret);
Alex Elder662518b2013-05-06 09:51:29 -05003358 goto out;
Alex Elder4156d992012-08-02 11:29:46 -05003359 }
3360 if (!rbd_dev_ondisk_valid(ondisk)) {
3361 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05003362 rbd_warn(rbd_dev, "invalid header");
Alex Elder662518b2013-05-06 09:51:29 -05003363 goto out;
Alex Elder4156d992012-08-02 11:29:46 -05003364 }
3365
3366 names_size = le64_to_cpu(ondisk->snap_names_len);
3367 want_count = snap_count;
3368 snap_count = le32_to_cpu(ondisk->snap_count);
3369 } while (snap_count != want_count);
3370
Alex Elder662518b2013-05-06 09:51:29 -05003371 ret = rbd_header_from_disk(rbd_dev, ondisk);
3372out:
Alex Elder4156d992012-08-02 11:29:46 -05003373 kfree(ondisk);
3374
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003375 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003376}
3377
Alex Elder15228ed2013-05-01 12:43:03 -05003378/*
3379 * Clear the rbd device's EXISTS flag if the snapshot it's mapped to
3380 * has disappeared from the (just updated) snapshot context.
3381 */
3382static void rbd_exists_validate(struct rbd_device *rbd_dev)
3383{
3384 u64 snap_id;
3385
3386 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags))
3387 return;
3388
3389 snap_id = rbd_dev->spec->snap_id;
3390 if (snap_id == CEPH_NOSNAP)
3391 return;
3392
3393 if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX)
3394 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
3395}
3396
Josh Durgin98752012013-08-29 17:26:31 -07003397static void rbd_dev_update_size(struct rbd_device *rbd_dev)
3398{
3399 sector_t size;
3400 bool removing;
3401
3402 /*
3403 * Don't hold the lock while doing disk operations,
3404 * or lock ordering will conflict with the bdev mutex via:
3405 * rbd_add() -> blkdev_get() -> rbd_open()
3406 */
3407 spin_lock_irq(&rbd_dev->lock);
3408 removing = test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags);
3409 spin_unlock_irq(&rbd_dev->lock);
3410 /*
3411 * If the device is being removed, rbd_dev->disk has
3412 * been destroyed, so don't try to update its size
3413 */
3414 if (!removing) {
3415 size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE;
3416 dout("setting size to %llu sectors", (unsigned long long)size);
3417 set_capacity(rbd_dev->disk, size);
3418 revalidate_disk(rbd_dev->disk);
3419 }
3420}
3421
Alex Eldercc4a38bd2013-04-30 00:44:33 -05003422static int rbd_dev_refresh(struct rbd_device *rbd_dev)
Alex Elder1fe5e992012-07-25 09:32:41 -05003423{
Alex Eldere627db02013-05-06 07:40:30 -05003424 u64 mapping_size;
Alex Elder1fe5e992012-07-25 09:32:41 -05003425 int ret;
3426
Alex Elder117973f2012-08-31 17:29:55 -05003427 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
Alex Eldercfbf6372013-05-31 17:40:45 -05003428 down_write(&rbd_dev->header_rwsem);
Alex Elder3b5cf2a2013-05-29 11:18:59 -05003429 mapping_size = rbd_dev->mapping.size;
Alex Elder117973f2012-08-31 17:29:55 -05003430 if (rbd_dev->image_format == 1)
Alex Elder99a41eb2013-05-06 09:51:30 -05003431 ret = rbd_dev_v1_header_info(rbd_dev);
Alex Elder117973f2012-08-31 17:29:55 -05003432 else
Alex Elder2df3fac2013-05-06 09:51:30 -05003433 ret = rbd_dev_v2_header_info(rbd_dev);
Alex Elder15228ed2013-05-01 12:43:03 -05003434
3435 /* If it's a mapped snapshot, validate its EXISTS flag */
3436
3437 rbd_exists_validate(rbd_dev);
Alex Eldercfbf6372013-05-31 17:40:45 -05003438 up_write(&rbd_dev->header_rwsem);
3439
Alex Elder00a653e2013-05-06 07:40:30 -05003440 if (mapping_size != rbd_dev->mapping.size) {
Josh Durgin98752012013-08-29 17:26:31 -07003441 rbd_dev_update_size(rbd_dev);
Alex Elder00a653e2013-05-06 07:40:30 -05003442 }
Alex Elder1fe5e992012-07-25 09:32:41 -05003443
3444 return ret;
3445}
3446
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003447static int rbd_init_disk(struct rbd_device *rbd_dev)
3448{
3449 struct gendisk *disk;
3450 struct request_queue *q;
Alex Elder593a9e72012-02-07 12:03:37 -06003451 u64 segment_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003452
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003453 /* create gendisk info */
Ilya Dryomov7e513d42013-12-16 19:26:32 +02003454 disk = alloc_disk(single_major ?
3455 (1 << RBD_SINGLE_MAJOR_PART_SHIFT) :
3456 RBD_MINORS_PER_MAJOR);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003457 if (!disk)
Alex Elder1fcdb8a2012-08-29 17:11:06 -05003458 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003459
Alex Elderf0f8cef2012-01-29 13:57:44 -06003460 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
Alex Elderde71a292012-07-03 16:01:19 -05003461 rbd_dev->dev_id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003462 disk->major = rbd_dev->major;
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02003463 disk->first_minor = rbd_dev->minor;
Ilya Dryomov7e513d42013-12-16 19:26:32 +02003464 if (single_major)
3465 disk->flags |= GENHD_FL_EXT_DEVT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003466 disk->fops = &rbd_bd_ops;
3467 disk->private_data = rbd_dev;
3468
Alex Elderbf0d5f502012-11-22 00:00:08 -06003469 q = blk_init_queue(rbd_request_fn, &rbd_dev->lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003470 if (!q)
3471 goto out_disk;
Josh Durgin029bcbd2011-07-22 11:35:23 -07003472
Alex Elder593a9e72012-02-07 12:03:37 -06003473 /* We use the default size, but let's be explicit about it. */
3474 blk_queue_physical_block_size(q, SECTOR_SIZE);
3475
Josh Durgin029bcbd2011-07-22 11:35:23 -07003476 /* set io sizes to object size */
Alex Elder593a9e72012-02-07 12:03:37 -06003477 segment_size = rbd_obj_bytes(&rbd_dev->header);
3478 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
3479 blk_queue_max_segment_size(q, segment_size);
3480 blk_queue_io_min(q, segment_size);
3481 blk_queue_io_opt(q, segment_size);
Josh Durgin029bcbd2011-07-22 11:35:23 -07003482
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003483 blk_queue_merge_bvec(q, rbd_merge_bvec);
3484 disk->queue = q;
3485
3486 q->queuedata = rbd_dev;
3487
3488 rbd_dev->disk = disk;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003489
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003490 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003491out_disk:
3492 put_disk(disk);
Alex Elder1fcdb8a2012-08-29 17:11:06 -05003493
3494 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003495}
3496
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003497/*
3498 sysfs
3499*/
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003500
Alex Elder593a9e72012-02-07 12:03:37 -06003501static struct rbd_device *dev_to_rbd_dev(struct device *dev)
3502{
3503 return container_of(dev, struct rbd_device, dev);
3504}
3505
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003506static ssize_t rbd_size_show(struct device *dev,
3507 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003508{
Alex Elder593a9e72012-02-07 12:03:37 -06003509 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003510
Alex Elderfc71d832013-04-26 15:44:36 -05003511 return sprintf(buf, "%llu\n",
3512 (unsigned long long)rbd_dev->mapping.size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003513}
3514
Alex Elder34b13182012-07-13 20:35:12 -05003515/*
3516 * Note this shows the features for whatever's mapped, which is not
3517 * necessarily the base image.
3518 */
3519static ssize_t rbd_features_show(struct device *dev,
3520 struct device_attribute *attr, char *buf)
3521{
3522 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3523
3524 return sprintf(buf, "0x%016llx\n",
Alex Elderfc71d832013-04-26 15:44:36 -05003525 (unsigned long long)rbd_dev->mapping.features);
Alex Elder34b13182012-07-13 20:35:12 -05003526}
3527
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003528static ssize_t rbd_major_show(struct device *dev,
3529 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003530{
Alex Elder593a9e72012-02-07 12:03:37 -06003531 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003532
Alex Elderfc71d832013-04-26 15:44:36 -05003533 if (rbd_dev->major)
3534 return sprintf(buf, "%d\n", rbd_dev->major);
3535
3536 return sprintf(buf, "(none)\n");
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02003537}
Alex Elderfc71d832013-04-26 15:44:36 -05003538
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02003539static ssize_t rbd_minor_show(struct device *dev,
3540 struct device_attribute *attr, char *buf)
3541{
3542 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3543
3544 return sprintf(buf, "%d\n", rbd_dev->minor);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003545}
3546
3547static ssize_t rbd_client_id_show(struct device *dev,
3548 struct device_attribute *attr, char *buf)
3549{
Alex Elder593a9e72012-02-07 12:03:37 -06003550 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003551
Alex Elder1dbb4392012-01-24 10:08:37 -06003552 return sprintf(buf, "client%lld\n",
3553 ceph_client_id(rbd_dev->rbd_client->client));
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003554}
3555
3556static ssize_t rbd_pool_show(struct device *dev,
3557 struct device_attribute *attr, char *buf)
3558{
Alex Elder593a9e72012-02-07 12:03:37 -06003559 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003560
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003561 return sprintf(buf, "%s\n", rbd_dev->spec->pool_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003562}
3563
Alex Elder9bb2f332012-07-12 10:46:35 -05003564static ssize_t rbd_pool_id_show(struct device *dev,
3565 struct device_attribute *attr, char *buf)
3566{
3567 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3568
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003569 return sprintf(buf, "%llu\n",
Alex Elderfc71d832013-04-26 15:44:36 -05003570 (unsigned long long) rbd_dev->spec->pool_id);
Alex Elder9bb2f332012-07-12 10:46:35 -05003571}
3572
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003573static ssize_t rbd_name_show(struct device *dev,
3574 struct device_attribute *attr, char *buf)
3575{
Alex Elder593a9e72012-02-07 12:03:37 -06003576 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003577
Alex Eldera92ffdf2012-10-30 19:40:33 -05003578 if (rbd_dev->spec->image_name)
3579 return sprintf(buf, "%s\n", rbd_dev->spec->image_name);
3580
3581 return sprintf(buf, "(unknown)\n");
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003582}
3583
Alex Elder589d30e2012-07-10 20:30:11 -05003584static ssize_t rbd_image_id_show(struct device *dev,
3585 struct device_attribute *attr, char *buf)
3586{
3587 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3588
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003589 return sprintf(buf, "%s\n", rbd_dev->spec->image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05003590}
3591
Alex Elder34b13182012-07-13 20:35:12 -05003592/*
3593 * Shows the name of the currently-mapped snapshot (or
3594 * RBD_SNAP_HEAD_NAME for the base image).
3595 */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003596static ssize_t rbd_snap_show(struct device *dev,
3597 struct device_attribute *attr,
3598 char *buf)
3599{
Alex Elder593a9e72012-02-07 12:03:37 -06003600 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003601
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003602 return sprintf(buf, "%s\n", rbd_dev->spec->snap_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003603}
3604
Alex Elder86b00e02012-10-25 23:34:42 -05003605/*
3606 * For an rbd v2 image, shows the pool id, image id, and snapshot id
3607 * for the parent image. If there is no parent, simply shows
3608 * "(no parent image)".
3609 */
3610static ssize_t rbd_parent_show(struct device *dev,
3611 struct device_attribute *attr,
3612 char *buf)
3613{
3614 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3615 struct rbd_spec *spec = rbd_dev->parent_spec;
3616 int count;
3617 char *bufp = buf;
3618
3619 if (!spec)
3620 return sprintf(buf, "(no parent image)\n");
3621
3622 count = sprintf(bufp, "pool_id %llu\npool_name %s\n",
3623 (unsigned long long) spec->pool_id, spec->pool_name);
3624 if (count < 0)
3625 return count;
3626 bufp += count;
3627
3628 count = sprintf(bufp, "image_id %s\nimage_name %s\n", spec->image_id,
3629 spec->image_name ? spec->image_name : "(unknown)");
3630 if (count < 0)
3631 return count;
3632 bufp += count;
3633
3634 count = sprintf(bufp, "snap_id %llu\nsnap_name %s\n",
3635 (unsigned long long) spec->snap_id, spec->snap_name);
3636 if (count < 0)
3637 return count;
3638 bufp += count;
3639
3640 count = sprintf(bufp, "overlap %llu\n", rbd_dev->parent_overlap);
3641 if (count < 0)
3642 return count;
3643 bufp += count;
3644
3645 return (ssize_t) (bufp - buf);
3646}
3647
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003648static ssize_t rbd_image_refresh(struct device *dev,
3649 struct device_attribute *attr,
3650 const char *buf,
3651 size_t size)
3652{
Alex Elder593a9e72012-02-07 12:03:37 -06003653 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Alex Elderb8136232012-07-25 09:32:41 -05003654 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003655
Alex Eldercc4a38bd2013-04-30 00:44:33 -05003656 ret = rbd_dev_refresh(rbd_dev);
Alex Eldere627db02013-05-06 07:40:30 -05003657 if (ret)
3658 rbd_warn(rbd_dev, ": manual header refresh error (%d)\n", ret);
Alex Elderb8136232012-07-25 09:32:41 -05003659
3660 return ret < 0 ? ret : size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003661}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003662
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003663static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
Alex Elder34b13182012-07-13 20:35:12 -05003664static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003665static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02003666static DEVICE_ATTR(minor, S_IRUGO, rbd_minor_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003667static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
3668static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
Alex Elder9bb2f332012-07-12 10:46:35 -05003669static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003670static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
Alex Elder589d30e2012-07-10 20:30:11 -05003671static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003672static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
3673static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
Alex Elder86b00e02012-10-25 23:34:42 -05003674static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003675
3676static struct attribute *rbd_attrs[] = {
3677 &dev_attr_size.attr,
Alex Elder34b13182012-07-13 20:35:12 -05003678 &dev_attr_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003679 &dev_attr_major.attr,
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02003680 &dev_attr_minor.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003681 &dev_attr_client_id.attr,
3682 &dev_attr_pool.attr,
Alex Elder9bb2f332012-07-12 10:46:35 -05003683 &dev_attr_pool_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003684 &dev_attr_name.attr,
Alex Elder589d30e2012-07-10 20:30:11 -05003685 &dev_attr_image_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003686 &dev_attr_current_snap.attr,
Alex Elder86b00e02012-10-25 23:34:42 -05003687 &dev_attr_parent.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003688 &dev_attr_refresh.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003689 NULL
3690};
3691
3692static struct attribute_group rbd_attr_group = {
3693 .attrs = rbd_attrs,
3694};
3695
3696static const struct attribute_group *rbd_attr_groups[] = {
3697 &rbd_attr_group,
3698 NULL
3699};
3700
3701static void rbd_sysfs_dev_release(struct device *dev)
3702{
3703}
3704
3705static struct device_type rbd_device_type = {
3706 .name = "rbd",
3707 .groups = rbd_attr_groups,
3708 .release = rbd_sysfs_dev_release,
3709};
3710
Alex Elder8b8fb992012-10-26 17:25:24 -05003711static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)
3712{
3713 kref_get(&spec->kref);
3714
3715 return spec;
3716}
3717
3718static void rbd_spec_free(struct kref *kref);
3719static void rbd_spec_put(struct rbd_spec *spec)
3720{
3721 if (spec)
3722 kref_put(&spec->kref, rbd_spec_free);
3723}
3724
3725static struct rbd_spec *rbd_spec_alloc(void)
3726{
3727 struct rbd_spec *spec;
3728
3729 spec = kzalloc(sizeof (*spec), GFP_KERNEL);
3730 if (!spec)
3731 return NULL;
3732 kref_init(&spec->kref);
3733
Alex Elder8b8fb992012-10-26 17:25:24 -05003734 return spec;
3735}
3736
3737static void rbd_spec_free(struct kref *kref)
3738{
3739 struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref);
3740
3741 kfree(spec->pool_name);
3742 kfree(spec->image_id);
3743 kfree(spec->image_name);
3744 kfree(spec->snap_name);
3745 kfree(spec);
3746}
3747
Alex Eldercc344fa2013-02-19 12:25:56 -06003748static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,
Alex Elderc53d5892012-10-25 23:34:42 -05003749 struct rbd_spec *spec)
3750{
3751 struct rbd_device *rbd_dev;
3752
3753 rbd_dev = kzalloc(sizeof (*rbd_dev), GFP_KERNEL);
3754 if (!rbd_dev)
3755 return NULL;
3756
3757 spin_lock_init(&rbd_dev->lock);
Alex Elder6d292902013-01-14 12:43:31 -06003758 rbd_dev->flags = 0;
Alex Eldera2acd002013-05-08 22:50:04 -05003759 atomic_set(&rbd_dev->parent_ref, 0);
Alex Elderc53d5892012-10-25 23:34:42 -05003760 INIT_LIST_HEAD(&rbd_dev->node);
Alex Elderc53d5892012-10-25 23:34:42 -05003761 init_rwsem(&rbd_dev->header_rwsem);
3762
3763 rbd_dev->spec = spec;
3764 rbd_dev->rbd_client = rbdc;
3765
Alex Elder0903e872012-11-14 12:25:19 -06003766 /* Initialize the layout used for all rbd requests */
3767
3768 rbd_dev->layout.fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
3769 rbd_dev->layout.fl_stripe_count = cpu_to_le32(1);
3770 rbd_dev->layout.fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
3771 rbd_dev->layout.fl_pg_pool = cpu_to_le32((u32) spec->pool_id);
3772
Alex Elderc53d5892012-10-25 23:34:42 -05003773 return rbd_dev;
3774}
3775
3776static void rbd_dev_destroy(struct rbd_device *rbd_dev)
3777{
Alex Elderc53d5892012-10-25 23:34:42 -05003778 rbd_put_client(rbd_dev->rbd_client);
3779 rbd_spec_put(rbd_dev->spec);
3780 kfree(rbd_dev);
3781}
3782
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003783/*
Alex Elder9d475de2012-07-03 16:01:19 -05003784 * Get the size and object order for an image snapshot, or if
3785 * snap_id is CEPH_NOSNAP, gets this information for the base
3786 * image.
3787 */
3788static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
3789 u8 *order, u64 *snap_size)
3790{
3791 __le64 snapid = cpu_to_le64(snap_id);
3792 int ret;
3793 struct {
3794 u8 order;
3795 __le64 size;
3796 } __attribute__ ((packed)) size_buf = { 0 };
3797
Alex Elder36be9a72013-01-19 00:30:28 -06003798 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elder9d475de2012-07-03 16:01:19 -05003799 "rbd", "get_size",
Alex Elder41579762013-04-21 12:14:45 -05003800 &snapid, sizeof (snapid),
Alex Eldere2a58ee2013-04-30 00:44:33 -05003801 &size_buf, sizeof (size_buf));
Alex Elder36be9a72013-01-19 00:30:28 -06003802 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder9d475de2012-07-03 16:01:19 -05003803 if (ret < 0)
3804 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05003805 if (ret < sizeof (size_buf))
3806 return -ERANGE;
Alex Elder9d475de2012-07-03 16:01:19 -05003807
Josh Durginc3545572013-08-28 17:08:10 -07003808 if (order) {
Alex Elderc86f86e2013-04-25 15:09:41 -05003809 *order = size_buf.order;
Josh Durginc3545572013-08-28 17:08:10 -07003810 dout(" order %u", (unsigned int)*order);
3811 }
Alex Elder9d475de2012-07-03 16:01:19 -05003812 *snap_size = le64_to_cpu(size_buf.size);
3813
Josh Durginc3545572013-08-28 17:08:10 -07003814 dout(" snap_id 0x%016llx snap_size = %llu\n",
3815 (unsigned long long)snap_id,
Alex Elder57385b52013-04-21 12:14:45 -05003816 (unsigned long long)*snap_size);
Alex Elder9d475de2012-07-03 16:01:19 -05003817
3818 return 0;
3819}
3820
3821static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
3822{
3823 return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
3824 &rbd_dev->header.obj_order,
3825 &rbd_dev->header.image_size);
3826}
3827
Alex Elder1e130192012-07-03 16:01:19 -05003828static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
3829{
3830 void *reply_buf;
3831 int ret;
3832 void *p;
3833
3834 reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL);
3835 if (!reply_buf)
3836 return -ENOMEM;
3837
Alex Elder36be9a72013-01-19 00:30:28 -06003838 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elder41579762013-04-21 12:14:45 -05003839 "rbd", "get_object_prefix", NULL, 0,
Alex Eldere2a58ee2013-04-30 00:44:33 -05003840 reply_buf, RBD_OBJ_PREFIX_LEN_MAX);
Alex Elder36be9a72013-01-19 00:30:28 -06003841 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder1e130192012-07-03 16:01:19 -05003842 if (ret < 0)
3843 goto out;
3844
3845 p = reply_buf;
3846 rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p,
Alex Elder57385b52013-04-21 12:14:45 -05003847 p + ret, NULL, GFP_NOIO);
3848 ret = 0;
Alex Elder1e130192012-07-03 16:01:19 -05003849
3850 if (IS_ERR(rbd_dev->header.object_prefix)) {
3851 ret = PTR_ERR(rbd_dev->header.object_prefix);
3852 rbd_dev->header.object_prefix = NULL;
3853 } else {
3854 dout(" object_prefix = %s\n", rbd_dev->header.object_prefix);
3855 }
Alex Elder1e130192012-07-03 16:01:19 -05003856out:
3857 kfree(reply_buf);
3858
3859 return ret;
3860}
3861
Alex Elderb1b54022012-07-03 16:01:19 -05003862static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
3863 u64 *snap_features)
3864{
3865 __le64 snapid = cpu_to_le64(snap_id);
3866 struct {
3867 __le64 features;
3868 __le64 incompat;
Alex Elder41579762013-04-21 12:14:45 -05003869 } __attribute__ ((packed)) features_buf = { 0 };
Alex Elderd8891402012-10-09 13:50:17 -07003870 u64 incompat;
Alex Elderb1b54022012-07-03 16:01:19 -05003871 int ret;
3872
Alex Elder36be9a72013-01-19 00:30:28 -06003873 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elderb1b54022012-07-03 16:01:19 -05003874 "rbd", "get_features",
Alex Elder41579762013-04-21 12:14:45 -05003875 &snapid, sizeof (snapid),
Alex Eldere2a58ee2013-04-30 00:44:33 -05003876 &features_buf, sizeof (features_buf));
Alex Elder36be9a72013-01-19 00:30:28 -06003877 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderb1b54022012-07-03 16:01:19 -05003878 if (ret < 0)
3879 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05003880 if (ret < sizeof (features_buf))
3881 return -ERANGE;
Alex Elderd8891402012-10-09 13:50:17 -07003882
3883 incompat = le64_to_cpu(features_buf.incompat);
Alex Elder5cbf6f122013-04-11 09:29:48 -05003884 if (incompat & ~RBD_FEATURES_SUPPORTED)
Alex Elderb8f5c6e2012-11-01 08:39:26 -05003885 return -ENXIO;
Alex Elderd8891402012-10-09 13:50:17 -07003886
Alex Elderb1b54022012-07-03 16:01:19 -05003887 *snap_features = le64_to_cpu(features_buf.features);
3888
3889 dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
Alex Elder57385b52013-04-21 12:14:45 -05003890 (unsigned long long)snap_id,
3891 (unsigned long long)*snap_features,
3892 (unsigned long long)le64_to_cpu(features_buf.incompat));
Alex Elderb1b54022012-07-03 16:01:19 -05003893
3894 return 0;
3895}
3896
3897static int rbd_dev_v2_features(struct rbd_device *rbd_dev)
3898{
3899 return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP,
3900 &rbd_dev->header.features);
3901}
3902
Alex Elder86b00e02012-10-25 23:34:42 -05003903static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev)
3904{
3905 struct rbd_spec *parent_spec;
3906 size_t size;
3907 void *reply_buf = NULL;
3908 __le64 snapid;
3909 void *p;
3910 void *end;
Alex Elder642a2532013-05-06 17:40:33 -05003911 u64 pool_id;
Alex Elder86b00e02012-10-25 23:34:42 -05003912 char *image_id;
Alex Elder3b5cf2a2013-05-29 11:18:59 -05003913 u64 snap_id;
Alex Elder86b00e02012-10-25 23:34:42 -05003914 u64 overlap;
Alex Elder86b00e02012-10-25 23:34:42 -05003915 int ret;
3916
3917 parent_spec = rbd_spec_alloc();
3918 if (!parent_spec)
3919 return -ENOMEM;
3920
3921 size = sizeof (__le64) + /* pool_id */
3922 sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */
3923 sizeof (__le64) + /* snap_id */
3924 sizeof (__le64); /* overlap */
3925 reply_buf = kmalloc(size, GFP_KERNEL);
3926 if (!reply_buf) {
3927 ret = -ENOMEM;
3928 goto out_err;
3929 }
3930
3931 snapid = cpu_to_le64(CEPH_NOSNAP);
Alex Elder36be9a72013-01-19 00:30:28 -06003932 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elder86b00e02012-10-25 23:34:42 -05003933 "rbd", "get_parent",
Alex Elder41579762013-04-21 12:14:45 -05003934 &snapid, sizeof (snapid),
Alex Eldere2a58ee2013-04-30 00:44:33 -05003935 reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06003936 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder86b00e02012-10-25 23:34:42 -05003937 if (ret < 0)
3938 goto out_err;
3939
Alex Elder86b00e02012-10-25 23:34:42 -05003940 p = reply_buf;
Alex Elder57385b52013-04-21 12:14:45 -05003941 end = reply_buf + ret;
3942 ret = -ERANGE;
Alex Elder642a2532013-05-06 17:40:33 -05003943 ceph_decode_64_safe(&p, end, pool_id, out_err);
Alex Elder392a9da2013-05-06 17:40:33 -05003944 if (pool_id == CEPH_NOPOOL) {
3945 /*
3946 * Either the parent never existed, or we have
3947 * record of it but the image got flattened so it no
3948 * longer has a parent. When the parent of a
3949 * layered image disappears we immediately set the
3950 * overlap to 0. The effect of this is that all new
3951 * requests will be treated as if the image had no
3952 * parent.
3953 */
3954 if (rbd_dev->parent_overlap) {
3955 rbd_dev->parent_overlap = 0;
3956 smp_mb();
3957 rbd_dev_parent_put(rbd_dev);
3958 pr_info("%s: clone image has been flattened\n",
3959 rbd_dev->disk->disk_name);
3960 }
3961
Alex Elder86b00e02012-10-25 23:34:42 -05003962 goto out; /* No parent? No problem. */
Alex Elder392a9da2013-05-06 17:40:33 -05003963 }
Alex Elder86b00e02012-10-25 23:34:42 -05003964
Alex Elder0903e872012-11-14 12:25:19 -06003965 /* The ceph file layout needs to fit pool id in 32 bits */
3966
3967 ret = -EIO;
Alex Elder642a2532013-05-06 17:40:33 -05003968 if (pool_id > (u64)U32_MAX) {
Alex Elderc0cd10db2013-04-26 09:43:47 -05003969 rbd_warn(NULL, "parent pool id too large (%llu > %u)\n",
Alex Elder642a2532013-05-06 17:40:33 -05003970 (unsigned long long)pool_id, U32_MAX);
Alex Elder57385b52013-04-21 12:14:45 -05003971 goto out_err;
Alex Elderc0cd10db2013-04-26 09:43:47 -05003972 }
Alex Elder0903e872012-11-14 12:25:19 -06003973
Alex Elder979ed482012-11-01 08:39:26 -05003974 image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elder86b00e02012-10-25 23:34:42 -05003975 if (IS_ERR(image_id)) {
3976 ret = PTR_ERR(image_id);
3977 goto out_err;
3978 }
Alex Elder3b5cf2a2013-05-29 11:18:59 -05003979 ceph_decode_64_safe(&p, end, snap_id, out_err);
Alex Elder86b00e02012-10-25 23:34:42 -05003980 ceph_decode_64_safe(&p, end, overlap, out_err);
3981
Alex Elder3b5cf2a2013-05-29 11:18:59 -05003982 /*
3983 * The parent won't change (except when the clone is
3984 * flattened, already handled that). So we only need to
3985 * record the parent spec we have not already done so.
3986 */
3987 if (!rbd_dev->parent_spec) {
3988 parent_spec->pool_id = pool_id;
3989 parent_spec->image_id = image_id;
3990 parent_spec->snap_id = snap_id;
Alex Elder70cf49c2013-05-06 17:40:33 -05003991 rbd_dev->parent_spec = parent_spec;
3992 parent_spec = NULL; /* rbd_dev now owns this */
Alex Elder3b5cf2a2013-05-29 11:18:59 -05003993 }
3994
3995 /*
3996 * We always update the parent overlap. If it's zero we
3997 * treat it specially.
3998 */
3999 rbd_dev->parent_overlap = overlap;
4000 smp_mb();
4001 if (!overlap) {
4002
4003 /* A null parent_spec indicates it's the initial probe */
4004
4005 if (parent_spec) {
4006 /*
4007 * The overlap has become zero, so the clone
4008 * must have been resized down to 0 at some
4009 * point. Treat this the same as a flatten.
4010 */
4011 rbd_dev_parent_put(rbd_dev);
4012 pr_info("%s: clone image now standalone\n",
4013 rbd_dev->disk->disk_name);
4014 } else {
4015 /*
4016 * For the initial probe, if we find the
4017 * overlap is zero we just pretend there was
4018 * no parent image.
4019 */
4020 rbd_warn(rbd_dev, "ignoring parent of "
4021 "clone with overlap 0\n");
4022 }
Alex Elder70cf49c2013-05-06 17:40:33 -05004023 }
Alex Elder86b00e02012-10-25 23:34:42 -05004024out:
4025 ret = 0;
4026out_err:
4027 kfree(reply_buf);
4028 rbd_spec_put(parent_spec);
4029
4030 return ret;
4031}
4032
Alex Eldercc070d52013-04-21 12:14:45 -05004033static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev)
4034{
4035 struct {
4036 __le64 stripe_unit;
4037 __le64 stripe_count;
4038 } __attribute__ ((packed)) striping_info_buf = { 0 };
4039 size_t size = sizeof (striping_info_buf);
4040 void *p;
4041 u64 obj_size;
4042 u64 stripe_unit;
4043 u64 stripe_count;
4044 int ret;
4045
4046 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
4047 "rbd", "get_stripe_unit_count", NULL, 0,
Alex Eldere2a58ee2013-04-30 00:44:33 -05004048 (char *)&striping_info_buf, size);
Alex Eldercc070d52013-04-21 12:14:45 -05004049 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
4050 if (ret < 0)
4051 return ret;
4052 if (ret < size)
4053 return -ERANGE;
4054
4055 /*
4056 * We don't actually support the "fancy striping" feature
4057 * (STRIPINGV2) yet, but if the striping sizes are the
4058 * defaults the behavior is the same as before. So find
4059 * out, and only fail if the image has non-default values.
4060 */
4061 ret = -EINVAL;
4062 obj_size = (u64)1 << rbd_dev->header.obj_order;
4063 p = &striping_info_buf;
4064 stripe_unit = ceph_decode_64(&p);
4065 if (stripe_unit != obj_size) {
4066 rbd_warn(rbd_dev, "unsupported stripe unit "
4067 "(got %llu want %llu)",
4068 stripe_unit, obj_size);
4069 return -EINVAL;
4070 }
4071 stripe_count = ceph_decode_64(&p);
4072 if (stripe_count != 1) {
4073 rbd_warn(rbd_dev, "unsupported stripe count "
4074 "(got %llu want 1)", stripe_count);
4075 return -EINVAL;
4076 }
Alex Elder500d0c02013-04-26 09:43:47 -05004077 rbd_dev->header.stripe_unit = stripe_unit;
4078 rbd_dev->header.stripe_count = stripe_count;
Alex Eldercc070d52013-04-21 12:14:45 -05004079
4080 return 0;
4081}
4082
Alex Elder9e15b772012-10-30 19:40:33 -05004083static char *rbd_dev_image_name(struct rbd_device *rbd_dev)
4084{
4085 size_t image_id_size;
4086 char *image_id;
4087 void *p;
4088 void *end;
4089 size_t size;
4090 void *reply_buf = NULL;
4091 size_t len = 0;
4092 char *image_name = NULL;
4093 int ret;
4094
4095 rbd_assert(!rbd_dev->spec->image_name);
4096
Alex Elder69e7a022012-11-01 08:39:26 -05004097 len = strlen(rbd_dev->spec->image_id);
4098 image_id_size = sizeof (__le32) + len;
Alex Elder9e15b772012-10-30 19:40:33 -05004099 image_id = kmalloc(image_id_size, GFP_KERNEL);
4100 if (!image_id)
4101 return NULL;
4102
4103 p = image_id;
Alex Elder41579762013-04-21 12:14:45 -05004104 end = image_id + image_id_size;
Alex Elder57385b52013-04-21 12:14:45 -05004105 ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len);
Alex Elder9e15b772012-10-30 19:40:33 -05004106
4107 size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX;
4108 reply_buf = kmalloc(size, GFP_KERNEL);
4109 if (!reply_buf)
4110 goto out;
4111
Alex Elder36be9a72013-01-19 00:30:28 -06004112 ret = rbd_obj_method_sync(rbd_dev, RBD_DIRECTORY,
Alex Elder9e15b772012-10-30 19:40:33 -05004113 "rbd", "dir_get_name",
4114 image_id, image_id_size,
Alex Eldere2a58ee2013-04-30 00:44:33 -05004115 reply_buf, size);
Alex Elder9e15b772012-10-30 19:40:33 -05004116 if (ret < 0)
4117 goto out;
4118 p = reply_buf;
Alex Elderf40eb342013-04-25 15:09:42 -05004119 end = reply_buf + ret;
4120
Alex Elder9e15b772012-10-30 19:40:33 -05004121 image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL);
4122 if (IS_ERR(image_name))
4123 image_name = NULL;
4124 else
4125 dout("%s: name is %s len is %zd\n", __func__, image_name, len);
4126out:
4127 kfree(reply_buf);
4128 kfree(image_id);
4129
4130 return image_name;
4131}
4132
Alex Elder2ad3d712013-04-30 00:44:33 -05004133static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
4134{
4135 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
4136 const char *snap_name;
4137 u32 which = 0;
4138
4139 /* Skip over names until we find the one we are looking for */
4140
4141 snap_name = rbd_dev->header.snap_names;
4142 while (which < snapc->num_snaps) {
4143 if (!strcmp(name, snap_name))
4144 return snapc->snaps[which];
4145 snap_name += strlen(snap_name) + 1;
4146 which++;
4147 }
4148 return CEPH_NOSNAP;
4149}
4150
4151static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
4152{
4153 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
4154 u32 which;
4155 bool found = false;
4156 u64 snap_id;
4157
4158 for (which = 0; !found && which < snapc->num_snaps; which++) {
4159 const char *snap_name;
4160
4161 snap_id = snapc->snaps[which];
4162 snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id);
Josh Durginefadc982013-08-29 19:16:42 -07004163 if (IS_ERR(snap_name)) {
4164 /* ignore no-longer existing snapshots */
4165 if (PTR_ERR(snap_name) == -ENOENT)
4166 continue;
4167 else
4168 break;
4169 }
Alex Elder2ad3d712013-04-30 00:44:33 -05004170 found = !strcmp(name, snap_name);
4171 kfree(snap_name);
4172 }
4173 return found ? snap_id : CEPH_NOSNAP;
4174}
4175
4176/*
4177 * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if
4178 * no snapshot by that name is found, or if an error occurs.
4179 */
4180static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
4181{
4182 if (rbd_dev->image_format == 1)
4183 return rbd_v1_snap_id_by_name(rbd_dev, name);
4184
4185 return rbd_v2_snap_id_by_name(rbd_dev, name);
4186}
4187
Alex Elder9e15b772012-10-30 19:40:33 -05004188/*
Alex Elder2e9f7f12013-04-26 09:43:48 -05004189 * When an rbd image has a parent image, it is identified by the
4190 * pool, image, and snapshot ids (not names). This function fills
4191 * in the names for those ids. (It's OK if we can't figure out the
4192 * name for an image id, but the pool and snapshot ids should always
4193 * exist and have names.) All names in an rbd spec are dynamically
4194 * allocated.
Alex Eldere1d42132013-04-25 23:15:08 -05004195 *
4196 * When an image being mapped (not a parent) is probed, we have the
4197 * pool name and pool id, image name and image id, and the snapshot
4198 * name. The only thing we're missing is the snapshot id.
Alex Elder9e15b772012-10-30 19:40:33 -05004199 */
Alex Elder2e9f7f12013-04-26 09:43:48 -05004200static int rbd_dev_spec_update(struct rbd_device *rbd_dev)
Alex Elder9e15b772012-10-30 19:40:33 -05004201{
Alex Elder2e9f7f12013-04-26 09:43:48 -05004202 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
4203 struct rbd_spec *spec = rbd_dev->spec;
4204 const char *pool_name;
4205 const char *image_name;
4206 const char *snap_name;
Alex Elder9e15b772012-10-30 19:40:33 -05004207 int ret;
4208
Alex Eldere1d42132013-04-25 23:15:08 -05004209 /*
4210 * An image being mapped will have the pool name (etc.), but
4211 * we need to look up the snapshot id.
4212 */
Alex Elder2e9f7f12013-04-26 09:43:48 -05004213 if (spec->pool_name) {
4214 if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) {
Alex Elder2ad3d712013-04-30 00:44:33 -05004215 u64 snap_id;
Alex Eldere1d42132013-04-25 23:15:08 -05004216
Alex Elder2ad3d712013-04-30 00:44:33 -05004217 snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name);
4218 if (snap_id == CEPH_NOSNAP)
Alex Eldere1d42132013-04-25 23:15:08 -05004219 return -ENOENT;
Alex Elder2ad3d712013-04-30 00:44:33 -05004220 spec->snap_id = snap_id;
Alex Eldere1d42132013-04-25 23:15:08 -05004221 } else {
Alex Elder2e9f7f12013-04-26 09:43:48 -05004222 spec->snap_id = CEPH_NOSNAP;
Alex Eldere1d42132013-04-25 23:15:08 -05004223 }
4224
4225 return 0;
4226 }
Alex Elder9e15b772012-10-30 19:40:33 -05004227
Alex Elder2e9f7f12013-04-26 09:43:48 -05004228 /* Get the pool name; we have to make our own copy of this */
Alex Elder9e15b772012-10-30 19:40:33 -05004229
Alex Elder2e9f7f12013-04-26 09:43:48 -05004230 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id);
4231 if (!pool_name) {
4232 rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id);
Alex Elder935dc892012-11-01 10:17:15 -05004233 return -EIO;
4234 }
Alex Elder2e9f7f12013-04-26 09:43:48 -05004235 pool_name = kstrdup(pool_name, GFP_KERNEL);
4236 if (!pool_name)
Alex Elder9e15b772012-10-30 19:40:33 -05004237 return -ENOMEM;
4238
4239 /* Fetch the image name; tolerate failure here */
4240
Alex Elder2e9f7f12013-04-26 09:43:48 -05004241 image_name = rbd_dev_image_name(rbd_dev);
4242 if (!image_name)
Alex Elder06ecc6c2012-11-01 10:17:15 -05004243 rbd_warn(rbd_dev, "unable to get image name");
Alex Elder9e15b772012-10-30 19:40:33 -05004244
Alex Elder2e9f7f12013-04-26 09:43:48 -05004245 /* Look up the snapshot name, and make a copy */
Alex Elder9e15b772012-10-30 19:40:33 -05004246
Alex Elder2e9f7f12013-04-26 09:43:48 -05004247 snap_name = rbd_snap_name(rbd_dev, spec->snap_id);
Josh Durginda6a6b62013-09-04 17:57:31 -07004248 if (IS_ERR(snap_name)) {
4249 ret = PTR_ERR(snap_name);
Alex Elder9e15b772012-10-30 19:40:33 -05004250 goto out_err;
Alex Elder2e9f7f12013-04-26 09:43:48 -05004251 }
4252
4253 spec->pool_name = pool_name;
4254 spec->image_name = image_name;
4255 spec->snap_name = snap_name;
Alex Elder9e15b772012-10-30 19:40:33 -05004256
4257 return 0;
4258out_err:
Alex Elder2e9f7f12013-04-26 09:43:48 -05004259 kfree(image_name);
4260 kfree(pool_name);
Alex Elder9e15b772012-10-30 19:40:33 -05004261
4262 return ret;
4263}
4264
Alex Eldercc4a38bd2013-04-30 00:44:33 -05004265static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev)
Alex Elder35d489f2012-07-03 16:01:19 -05004266{
4267 size_t size;
4268 int ret;
4269 void *reply_buf;
4270 void *p;
4271 void *end;
4272 u64 seq;
4273 u32 snap_count;
4274 struct ceph_snap_context *snapc;
4275 u32 i;
4276
4277 /*
4278 * We'll need room for the seq value (maximum snapshot id),
4279 * snapshot count, and array of that many snapshot ids.
4280 * For now we have a fixed upper limit on the number we're
4281 * prepared to receive.
4282 */
4283 size = sizeof (__le64) + sizeof (__le32) +
4284 RBD_MAX_SNAP_COUNT * sizeof (__le64);
4285 reply_buf = kzalloc(size, GFP_KERNEL);
4286 if (!reply_buf)
4287 return -ENOMEM;
4288
Alex Elder36be9a72013-01-19 00:30:28 -06004289 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elder41579762013-04-21 12:14:45 -05004290 "rbd", "get_snapcontext", NULL, 0,
Alex Eldere2a58ee2013-04-30 00:44:33 -05004291 reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06004292 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder35d489f2012-07-03 16:01:19 -05004293 if (ret < 0)
4294 goto out;
4295
Alex Elder35d489f2012-07-03 16:01:19 -05004296 p = reply_buf;
Alex Elder57385b52013-04-21 12:14:45 -05004297 end = reply_buf + ret;
4298 ret = -ERANGE;
Alex Elder35d489f2012-07-03 16:01:19 -05004299 ceph_decode_64_safe(&p, end, seq, out);
4300 ceph_decode_32_safe(&p, end, snap_count, out);
4301
4302 /*
4303 * Make sure the reported number of snapshot ids wouldn't go
4304 * beyond the end of our buffer. But before checking that,
4305 * make sure the computed size of the snapshot context we
4306 * allocate is representable in a size_t.
4307 */
4308 if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
4309 / sizeof (u64)) {
4310 ret = -EINVAL;
4311 goto out;
4312 }
4313 if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
4314 goto out;
Alex Elder468521c2013-04-26 09:43:47 -05004315 ret = 0;
Alex Elder35d489f2012-07-03 16:01:19 -05004316
Alex Elder812164f82013-04-30 00:44:32 -05004317 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
Alex Elder35d489f2012-07-03 16:01:19 -05004318 if (!snapc) {
4319 ret = -ENOMEM;
4320 goto out;
4321 }
Alex Elder35d489f2012-07-03 16:01:19 -05004322 snapc->seq = seq;
Alex Elder35d489f2012-07-03 16:01:19 -05004323 for (i = 0; i < snap_count; i++)
4324 snapc->snaps[i] = ceph_decode_64(&p);
4325
Alex Elder49ece552013-05-06 08:37:00 -05004326 ceph_put_snap_context(rbd_dev->header.snapc);
Alex Elder35d489f2012-07-03 16:01:19 -05004327 rbd_dev->header.snapc = snapc;
4328
4329 dout(" snap context seq = %llu, snap_count = %u\n",
Alex Elder57385b52013-04-21 12:14:45 -05004330 (unsigned long long)seq, (unsigned int)snap_count);
Alex Elder35d489f2012-07-03 16:01:19 -05004331out:
4332 kfree(reply_buf);
4333
Alex Elder57385b52013-04-21 12:14:45 -05004334 return ret;
Alex Elder35d489f2012-07-03 16:01:19 -05004335}
4336
Alex Elder54cac612013-04-30 00:44:33 -05004337static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
4338 u64 snap_id)
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004339{
4340 size_t size;
4341 void *reply_buf;
Alex Elder54cac612013-04-30 00:44:33 -05004342 __le64 snapid;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004343 int ret;
4344 void *p;
4345 void *end;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004346 char *snap_name;
4347
4348 size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN;
4349 reply_buf = kmalloc(size, GFP_KERNEL);
4350 if (!reply_buf)
4351 return ERR_PTR(-ENOMEM);
4352
Alex Elder54cac612013-04-30 00:44:33 -05004353 snapid = cpu_to_le64(snap_id);
Alex Elder36be9a72013-01-19 00:30:28 -06004354 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004355 "rbd", "get_snapshot_name",
Alex Elder54cac612013-04-30 00:44:33 -05004356 &snapid, sizeof (snapid),
Alex Eldere2a58ee2013-04-30 00:44:33 -05004357 reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06004358 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderf40eb342013-04-25 15:09:42 -05004359 if (ret < 0) {
4360 snap_name = ERR_PTR(ret);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004361 goto out;
Alex Elderf40eb342013-04-25 15:09:42 -05004362 }
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004363
4364 p = reply_buf;
Alex Elderf40eb342013-04-25 15:09:42 -05004365 end = reply_buf + ret;
Alex Eldere5c35532012-10-25 23:34:41 -05004366 snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elderf40eb342013-04-25 15:09:42 -05004367 if (IS_ERR(snap_name))
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004368 goto out;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004369
Alex Elderf40eb342013-04-25 15:09:42 -05004370 dout(" snap_id 0x%016llx snap_name = %s\n",
Alex Elder54cac612013-04-30 00:44:33 -05004371 (unsigned long long)snap_id, snap_name);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004372out:
4373 kfree(reply_buf);
4374
Alex Elderf40eb342013-04-25 15:09:42 -05004375 return snap_name;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004376}
4377
Alex Elder2df3fac2013-05-06 09:51:30 -05004378static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev)
Alex Elder117973f2012-08-31 17:29:55 -05004379{
Alex Elder2df3fac2013-05-06 09:51:30 -05004380 bool first_time = rbd_dev->header.object_prefix == NULL;
Alex Elder117973f2012-08-31 17:29:55 -05004381 int ret;
Alex Elder117973f2012-08-31 17:29:55 -05004382
Josh Durgin1617e402013-06-12 14:43:10 -07004383 ret = rbd_dev_v2_image_size(rbd_dev);
4384 if (ret)
Alex Eldercfbf6372013-05-31 17:40:45 -05004385 return ret;
Josh Durgin1617e402013-06-12 14:43:10 -07004386
Alex Elder2df3fac2013-05-06 09:51:30 -05004387 if (first_time) {
4388 ret = rbd_dev_v2_header_onetime(rbd_dev);
4389 if (ret)
Alex Eldercfbf6372013-05-31 17:40:45 -05004390 return ret;
Alex Elder2df3fac2013-05-06 09:51:30 -05004391 }
4392
Alex Elder642a2532013-05-06 17:40:33 -05004393 /*
4394 * If the image supports layering, get the parent info. We
4395 * need to probe the first time regardless. Thereafter we
4396 * only need to if there's a parent, to see if it has
4397 * disappeared due to the mapped image getting flattened.
4398 */
4399 if (rbd_dev->header.features & RBD_FEATURE_LAYERING &&
4400 (first_time || rbd_dev->parent_spec)) {
4401 bool warn;
4402
4403 ret = rbd_dev_v2_parent_info(rbd_dev);
4404 if (ret)
Alex Eldercfbf6372013-05-31 17:40:45 -05004405 return ret;
Alex Elder642a2532013-05-06 17:40:33 -05004406
4407 /*
4408 * Print a warning if this is the initial probe and
4409 * the image has a parent. Don't print it if the
4410 * image now being probed is itself a parent. We
4411 * can tell at this point because we won't know its
4412 * pool name yet (just its pool id).
4413 */
4414 warn = rbd_dev->parent_spec && rbd_dev->spec->pool_name;
4415 if (first_time && warn)
4416 rbd_warn(rbd_dev, "WARNING: kernel layering "
4417 "is EXPERIMENTAL!");
4418 }
4419
Alex Elder29334ba2013-05-06 07:40:30 -05004420 if (rbd_dev->spec->snap_id == CEPH_NOSNAP)
4421 if (rbd_dev->mapping.size != rbd_dev->header.image_size)
4422 rbd_dev->mapping.size = rbd_dev->header.image_size;
Alex Elder117973f2012-08-31 17:29:55 -05004423
Alex Eldercc4a38bd2013-04-30 00:44:33 -05004424 ret = rbd_dev_v2_snap_context(rbd_dev);
Alex Elder117973f2012-08-31 17:29:55 -05004425 dout("rbd_dev_v2_snap_context returned %d\n", ret);
Alex Elder117973f2012-08-31 17:29:55 -05004426
4427 return ret;
4428}
4429
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004430static int rbd_bus_add_dev(struct rbd_device *rbd_dev)
4431{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004432 struct device *dev;
Alex Eldercd789ab2012-08-30 00:16:38 -05004433 int ret;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004434
Alex Eldercd789ab2012-08-30 00:16:38 -05004435 dev = &rbd_dev->dev;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004436 dev->bus = &rbd_bus_type;
4437 dev->type = &rbd_device_type;
4438 dev->parent = &rbd_root_dev;
Alex Elder200a6a82013-04-28 23:32:34 -05004439 dev->release = rbd_dev_device_release;
Alex Elderde71a292012-07-03 16:01:19 -05004440 dev_set_name(dev, "%d", rbd_dev->dev_id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004441 ret = device_register(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004442
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004443 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004444}
4445
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004446static void rbd_bus_del_dev(struct rbd_device *rbd_dev)
4447{
4448 device_unregister(&rbd_dev->dev);
4449}
4450
Alex Elder1ddbe942012-01-29 13:57:44 -06004451/*
Alex Elder499afd52012-02-02 08:13:29 -06004452 * Get a unique rbd identifier for the given new rbd_dev, and add
Ilya Dryomovf8a22fc2013-12-13 15:28:57 +02004453 * the rbd_dev to the global list.
Alex Elder1ddbe942012-01-29 13:57:44 -06004454 */
Ilya Dryomovf8a22fc2013-12-13 15:28:57 +02004455static int rbd_dev_id_get(struct rbd_device *rbd_dev)
Alex Elderb7f23c32012-01-29 13:57:43 -06004456{
Ilya Dryomovf8a22fc2013-12-13 15:28:57 +02004457 int new_dev_id;
4458
Ilya Dryomov9b60e702013-12-13 15:28:57 +02004459 new_dev_id = ida_simple_get(&rbd_dev_id_ida,
4460 0, minor_to_rbd_dev_id(1 << MINORBITS),
4461 GFP_KERNEL);
Ilya Dryomovf8a22fc2013-12-13 15:28:57 +02004462 if (new_dev_id < 0)
4463 return new_dev_id;
4464
4465 rbd_dev->dev_id = new_dev_id;
Alex Elder499afd52012-02-02 08:13:29 -06004466
4467 spin_lock(&rbd_dev_list_lock);
4468 list_add_tail(&rbd_dev->node, &rbd_dev_list);
4469 spin_unlock(&rbd_dev_list_lock);
Ilya Dryomovf8a22fc2013-12-13 15:28:57 +02004470
Ilya Dryomov70eebd22013-12-13 15:28:56 +02004471 dout("rbd_dev %p given dev id %d\n", rbd_dev, rbd_dev->dev_id);
Ilya Dryomovf8a22fc2013-12-13 15:28:57 +02004472
4473 return 0;
Alex Elder1ddbe942012-01-29 13:57:44 -06004474}
Alex Elderb7f23c32012-01-29 13:57:43 -06004475
Alex Elder1ddbe942012-01-29 13:57:44 -06004476/*
Alex Elder499afd52012-02-02 08:13:29 -06004477 * Remove an rbd_dev from the global list, and record that its
4478 * identifier is no longer in use.
Alex Elder1ddbe942012-01-29 13:57:44 -06004479 */
Alex Eldere2839302012-08-29 17:11:06 -05004480static void rbd_dev_id_put(struct rbd_device *rbd_dev)
Alex Elder1ddbe942012-01-29 13:57:44 -06004481{
Alex Elder499afd52012-02-02 08:13:29 -06004482 spin_lock(&rbd_dev_list_lock);
4483 list_del_init(&rbd_dev->node);
4484 spin_unlock(&rbd_dev_list_lock);
Alex Elderb7f23c32012-01-29 13:57:43 -06004485
Ilya Dryomovf8a22fc2013-12-13 15:28:57 +02004486 ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id);
4487
4488 dout("rbd_dev %p released dev id %d\n", rbd_dev, rbd_dev->dev_id);
Alex Elderb7f23c32012-01-29 13:57:43 -06004489}
4490
Alex Eldera725f65e2012-02-02 08:13:30 -06004491/*
Alex Eldere28fff262012-02-02 08:13:30 -06004492 * Skips over white space at *buf, and updates *buf to point to the
4493 * first found non-space character (if any). Returns the length of
Alex Elder593a9e72012-02-07 12:03:37 -06004494 * the token (string of non-white space characters) found. Note
4495 * that *buf must be terminated with '\0'.
Alex Eldere28fff262012-02-02 08:13:30 -06004496 */
4497static inline size_t next_token(const char **buf)
4498{
4499 /*
4500 * These are the characters that produce nonzero for
4501 * isspace() in the "C" and "POSIX" locales.
4502 */
4503 const char *spaces = " \f\n\r\t\v";
4504
4505 *buf += strspn(*buf, spaces); /* Find start of token */
4506
4507 return strcspn(*buf, spaces); /* Return token length */
4508}
4509
4510/*
4511 * Finds the next token in *buf, and if the provided token buffer is
4512 * big enough, copies the found token into it. The result, if
Alex Elder593a9e72012-02-07 12:03:37 -06004513 * copied, is guaranteed to be terminated with '\0'. Note that *buf
4514 * must be terminated with '\0' on entry.
Alex Eldere28fff262012-02-02 08:13:30 -06004515 *
4516 * Returns the length of the token found (not including the '\0').
4517 * Return value will be 0 if no token is found, and it will be >=
4518 * token_size if the token would not fit.
4519 *
Alex Elder593a9e72012-02-07 12:03:37 -06004520 * The *buf pointer will be updated to point beyond the end of the
Alex Eldere28fff262012-02-02 08:13:30 -06004521 * found token. Note that this occurs even if the token buffer is
4522 * too small to hold it.
4523 */
4524static inline size_t copy_token(const char **buf,
4525 char *token,
4526 size_t token_size)
4527{
4528 size_t len;
4529
4530 len = next_token(buf);
4531 if (len < token_size) {
4532 memcpy(token, *buf, len);
4533 *(token + len) = '\0';
4534 }
4535 *buf += len;
4536
4537 return len;
4538}
4539
4540/*
Alex Elderea3352f2012-07-09 21:04:23 -05004541 * Finds the next token in *buf, dynamically allocates a buffer big
4542 * enough to hold a copy of it, and copies the token into the new
4543 * buffer. The copy is guaranteed to be terminated with '\0'. Note
4544 * that a duplicate buffer is created even for a zero-length token.
4545 *
4546 * Returns a pointer to the newly-allocated duplicate, or a null
4547 * pointer if memory for the duplicate was not available. If
4548 * the lenp argument is a non-null pointer, the length of the token
4549 * (not including the '\0') is returned in *lenp.
4550 *
4551 * If successful, the *buf pointer will be updated to point beyond
4552 * the end of the found token.
4553 *
4554 * Note: uses GFP_KERNEL for allocation.
4555 */
4556static inline char *dup_token(const char **buf, size_t *lenp)
4557{
4558 char *dup;
4559 size_t len;
4560
4561 len = next_token(buf);
Alex Elder4caf35f2012-11-01 08:39:27 -05004562 dup = kmemdup(*buf, len + 1, GFP_KERNEL);
Alex Elderea3352f2012-07-09 21:04:23 -05004563 if (!dup)
4564 return NULL;
Alex Elderea3352f2012-07-09 21:04:23 -05004565 *(dup + len) = '\0';
4566 *buf += len;
4567
4568 if (lenp)
4569 *lenp = len;
4570
4571 return dup;
4572}
4573
4574/*
Alex Elder859c31d2012-10-25 23:34:42 -05004575 * Parse the options provided for an "rbd add" (i.e., rbd image
4576 * mapping) request. These arrive via a write to /sys/bus/rbd/add,
4577 * and the data written is passed here via a NUL-terminated buffer.
4578 * Returns 0 if successful or an error code otherwise.
Alex Elderd22f76e2012-07-12 10:46:35 -05004579 *
Alex Elder859c31d2012-10-25 23:34:42 -05004580 * The information extracted from these options is recorded in
4581 * the other parameters which return dynamically-allocated
4582 * structures:
4583 * ceph_opts
4584 * The address of a pointer that will refer to a ceph options
4585 * structure. Caller must release the returned pointer using
4586 * ceph_destroy_options() when it is no longer needed.
4587 * rbd_opts
4588 * Address of an rbd options pointer. Fully initialized by
4589 * this function; caller must release with kfree().
4590 * spec
4591 * Address of an rbd image specification pointer. Fully
4592 * initialized by this function based on parsed options.
4593 * Caller must release with rbd_spec_put().
4594 *
4595 * The options passed take this form:
4596 * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
4597 * where:
4598 * <mon_addrs>
4599 * A comma-separated list of one or more monitor addresses.
4600 * A monitor address is an ip address, optionally followed
4601 * by a port number (separated by a colon).
4602 * I.e.: ip1[:port1][,ip2[:port2]...]
4603 * <options>
4604 * A comma-separated list of ceph and/or rbd options.
4605 * <pool_name>
4606 * The name of the rados pool containing the rbd image.
4607 * <image_name>
4608 * The name of the image in that pool to map.
4609 * <snap_id>
4610 * An optional snapshot id. If provided, the mapping will
4611 * present data from the image at the time that snapshot was
4612 * created. The image head is used if no snapshot id is
4613 * provided. Snapshot mappings are always read-only.
Alex Eldera725f65e2012-02-02 08:13:30 -06004614 */
Alex Elder859c31d2012-10-25 23:34:42 -05004615static int rbd_add_parse_args(const char *buf,
Alex Elderdc79b112012-10-25 23:34:41 -05004616 struct ceph_options **ceph_opts,
Alex Elder859c31d2012-10-25 23:34:42 -05004617 struct rbd_options **opts,
4618 struct rbd_spec **rbd_spec)
Alex Eldera725f65e2012-02-02 08:13:30 -06004619{
Alex Elderd22f76e2012-07-12 10:46:35 -05004620 size_t len;
Alex Elder859c31d2012-10-25 23:34:42 -05004621 char *options;
Alex Elder0ddebc02012-10-25 23:34:41 -05004622 const char *mon_addrs;
Alex Elderecb4dc22013-04-26 09:43:47 -05004623 char *snap_name;
Alex Elder0ddebc02012-10-25 23:34:41 -05004624 size_t mon_addrs_size;
Alex Elder859c31d2012-10-25 23:34:42 -05004625 struct rbd_spec *spec = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05004626 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05004627 struct ceph_options *copts;
Alex Elderdc79b112012-10-25 23:34:41 -05004628 int ret;
Alex Eldere28fff262012-02-02 08:13:30 -06004629
4630 /* The first four tokens are required */
4631
Alex Elder7ef32142012-02-02 08:13:30 -06004632 len = next_token(&buf);
Alex Elder4fb5d6712012-11-01 10:17:15 -05004633 if (!len) {
4634 rbd_warn(NULL, "no monitor address(es) provided");
4635 return -EINVAL;
4636 }
Alex Elder0ddebc02012-10-25 23:34:41 -05004637 mon_addrs = buf;
Alex Elderf28e5652012-10-25 23:34:41 -05004638 mon_addrs_size = len + 1;
Alex Elder7ef32142012-02-02 08:13:30 -06004639 buf += len;
Alex Eldera725f65e2012-02-02 08:13:30 -06004640
Alex Elderdc79b112012-10-25 23:34:41 -05004641 ret = -EINVAL;
Alex Elderf28e5652012-10-25 23:34:41 -05004642 options = dup_token(&buf, NULL);
4643 if (!options)
Alex Elderdc79b112012-10-25 23:34:41 -05004644 return -ENOMEM;
Alex Elder4fb5d6712012-11-01 10:17:15 -05004645 if (!*options) {
4646 rbd_warn(NULL, "no options provided");
4647 goto out_err;
4648 }
Alex Eldera725f65e2012-02-02 08:13:30 -06004649
Alex Elder859c31d2012-10-25 23:34:42 -05004650 spec = rbd_spec_alloc();
4651 if (!spec)
Alex Elderf28e5652012-10-25 23:34:41 -05004652 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05004653
4654 spec->pool_name = dup_token(&buf, NULL);
4655 if (!spec->pool_name)
4656 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05004657 if (!*spec->pool_name) {
4658 rbd_warn(NULL, "no pool name provided");
4659 goto out_err;
4660 }
Alex Eldere28fff262012-02-02 08:13:30 -06004661
Alex Elder69e7a022012-11-01 08:39:26 -05004662 spec->image_name = dup_token(&buf, NULL);
Alex Elder859c31d2012-10-25 23:34:42 -05004663 if (!spec->image_name)
Alex Elderf28e5652012-10-25 23:34:41 -05004664 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05004665 if (!*spec->image_name) {
4666 rbd_warn(NULL, "no image name provided");
4667 goto out_err;
4668 }
Alex Eldere28fff262012-02-02 08:13:30 -06004669
Alex Elderf28e5652012-10-25 23:34:41 -05004670 /*
4671 * Snapshot name is optional; default is to use "-"
4672 * (indicating the head/no snapshot).
4673 */
Alex Elder3feeb8942012-08-31 17:29:52 -05004674 len = next_token(&buf);
Alex Elder820a5f32012-07-09 21:04:24 -05004675 if (!len) {
Alex Elder3feeb8942012-08-31 17:29:52 -05004676 buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
4677 len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
Alex Elderf28e5652012-10-25 23:34:41 -05004678 } else if (len > RBD_MAX_SNAP_NAME_LEN) {
Alex Elderdc79b112012-10-25 23:34:41 -05004679 ret = -ENAMETOOLONG;
Alex Elderf28e5652012-10-25 23:34:41 -05004680 goto out_err;
Alex Elder849b4262012-07-09 21:04:24 -05004681 }
Alex Elderecb4dc22013-04-26 09:43:47 -05004682 snap_name = kmemdup(buf, len + 1, GFP_KERNEL);
4683 if (!snap_name)
Alex Elderf28e5652012-10-25 23:34:41 -05004684 goto out_mem;
Alex Elderecb4dc22013-04-26 09:43:47 -05004685 *(snap_name + len) = '\0';
4686 spec->snap_name = snap_name;
Alex Eldere5c35532012-10-25 23:34:41 -05004687
Alex Elder0ddebc02012-10-25 23:34:41 -05004688 /* Initialize all rbd options to the defaults */
Alex Eldere28fff262012-02-02 08:13:30 -06004689
Alex Elder4e9afeb2012-10-25 23:34:41 -05004690 rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL);
4691 if (!rbd_opts)
4692 goto out_mem;
4693
4694 rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
Alex Elderd22f76e2012-07-12 10:46:35 -05004695
Alex Elder859c31d2012-10-25 23:34:42 -05004696 copts = ceph_parse_options(options, mon_addrs,
Alex Elder0ddebc02012-10-25 23:34:41 -05004697 mon_addrs + mon_addrs_size - 1,
Alex Elder4e9afeb2012-10-25 23:34:41 -05004698 parse_rbd_opts_token, rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05004699 if (IS_ERR(copts)) {
4700 ret = PTR_ERR(copts);
Alex Elderdc79b112012-10-25 23:34:41 -05004701 goto out_err;
4702 }
Alex Elder859c31d2012-10-25 23:34:42 -05004703 kfree(options);
4704
4705 *ceph_opts = copts;
Alex Elder4e9afeb2012-10-25 23:34:41 -05004706 *opts = rbd_opts;
Alex Elder859c31d2012-10-25 23:34:42 -05004707 *rbd_spec = spec;
Alex Elder0ddebc02012-10-25 23:34:41 -05004708
Alex Elderdc79b112012-10-25 23:34:41 -05004709 return 0;
Alex Elderf28e5652012-10-25 23:34:41 -05004710out_mem:
Alex Elderdc79b112012-10-25 23:34:41 -05004711 ret = -ENOMEM;
Alex Elderd22f76e2012-07-12 10:46:35 -05004712out_err:
Alex Elder859c31d2012-10-25 23:34:42 -05004713 kfree(rbd_opts);
4714 rbd_spec_put(spec);
Alex Elderf28e5652012-10-25 23:34:41 -05004715 kfree(options);
Alex Elderd22f76e2012-07-12 10:46:35 -05004716
Alex Elderdc79b112012-10-25 23:34:41 -05004717 return ret;
Alex Eldera725f65e2012-02-02 08:13:30 -06004718}
4719
Alex Elder589d30e2012-07-10 20:30:11 -05004720/*
4721 * An rbd format 2 image has a unique identifier, distinct from the
4722 * name given to it by the user. Internally, that identifier is
4723 * what's used to specify the names of objects related to the image.
4724 *
4725 * A special "rbd id" object is used to map an rbd image name to its
4726 * id. If that object doesn't exist, then there is no v2 rbd image
4727 * with the supplied name.
4728 *
4729 * This function will record the given rbd_dev's image_id field if
4730 * it can be determined, and in that case will return 0. If any
4731 * errors occur a negative errno will be returned and the rbd_dev's
4732 * image_id field will be unchanged (and should be NULL).
4733 */
4734static int rbd_dev_image_id(struct rbd_device *rbd_dev)
4735{
4736 int ret;
4737 size_t size;
4738 char *object_name;
4739 void *response;
Alex Elderc0fba362013-04-25 23:15:08 -05004740 char *image_id;
Alex Elder2f82ee52012-10-30 19:40:33 -05004741
Alex Elder589d30e2012-07-10 20:30:11 -05004742 /*
Alex Elder2c0d0a12012-10-30 19:40:33 -05004743 * When probing a parent image, the image id is already
4744 * known (and the image name likely is not). There's no
Alex Elderc0fba362013-04-25 23:15:08 -05004745 * need to fetch the image id again in this case. We
4746 * do still need to set the image format though.
Alex Elder2c0d0a12012-10-30 19:40:33 -05004747 */
Alex Elderc0fba362013-04-25 23:15:08 -05004748 if (rbd_dev->spec->image_id) {
4749 rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1;
4750
Alex Elder2c0d0a12012-10-30 19:40:33 -05004751 return 0;
Alex Elderc0fba362013-04-25 23:15:08 -05004752 }
Alex Elder2c0d0a12012-10-30 19:40:33 -05004753
4754 /*
Alex Elder589d30e2012-07-10 20:30:11 -05004755 * First, see if the format 2 image id file exists, and if
4756 * so, get the image's persistent id from it.
4757 */
Alex Elder69e7a022012-11-01 08:39:26 -05004758 size = sizeof (RBD_ID_PREFIX) + strlen(rbd_dev->spec->image_name);
Alex Elder589d30e2012-07-10 20:30:11 -05004759 object_name = kmalloc(size, GFP_NOIO);
4760 if (!object_name)
4761 return -ENOMEM;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004762 sprintf(object_name, "%s%s", RBD_ID_PREFIX, rbd_dev->spec->image_name);
Alex Elder589d30e2012-07-10 20:30:11 -05004763 dout("rbd id object name is %s\n", object_name);
4764
4765 /* Response will be an encoded string, which includes a length */
4766
4767 size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
4768 response = kzalloc(size, GFP_NOIO);
4769 if (!response) {
4770 ret = -ENOMEM;
4771 goto out;
4772 }
4773
Alex Elderc0fba362013-04-25 23:15:08 -05004774 /* If it doesn't exist we'll assume it's a format 1 image */
4775
Alex Elder36be9a72013-01-19 00:30:28 -06004776 ret = rbd_obj_method_sync(rbd_dev, object_name,
Alex Elder41579762013-04-21 12:14:45 -05004777 "rbd", "get_id", NULL, 0,
Alex Eldere2a58ee2013-04-30 00:44:33 -05004778 response, RBD_IMAGE_ID_LEN_MAX);
Alex Elder36be9a72013-01-19 00:30:28 -06004779 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderc0fba362013-04-25 23:15:08 -05004780 if (ret == -ENOENT) {
4781 image_id = kstrdup("", GFP_KERNEL);
4782 ret = image_id ? 0 : -ENOMEM;
4783 if (!ret)
4784 rbd_dev->image_format = 1;
4785 } else if (ret > sizeof (__le32)) {
4786 void *p = response;
Alex Elder589d30e2012-07-10 20:30:11 -05004787
Alex Elderc0fba362013-04-25 23:15:08 -05004788 image_id = ceph_extract_encoded_string(&p, p + ret,
Alex Elder979ed482012-11-01 08:39:26 -05004789 NULL, GFP_NOIO);
Alex Elderc0fba362013-04-25 23:15:08 -05004790 ret = IS_ERR(image_id) ? PTR_ERR(image_id) : 0;
4791 if (!ret)
4792 rbd_dev->image_format = 2;
Alex Elder589d30e2012-07-10 20:30:11 -05004793 } else {
Alex Elderc0fba362013-04-25 23:15:08 -05004794 ret = -EINVAL;
4795 }
4796
4797 if (!ret) {
4798 rbd_dev->spec->image_id = image_id;
4799 dout("image_id is %s\n", image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05004800 }
4801out:
4802 kfree(response);
4803 kfree(object_name);
4804
4805 return ret;
4806}
4807
Alex Elder3abef3b2013-05-13 20:35:37 -05004808/*
4809 * Undo whatever state changes are made by v1 or v2 header info
4810 * call.
4811 */
Alex Elder6fd48b32013-04-28 23:32:34 -05004812static void rbd_dev_unprobe(struct rbd_device *rbd_dev)
4813{
4814 struct rbd_image_header *header;
4815
Alex Elder392a9da2013-05-06 17:40:33 -05004816 /* Drop parent reference unless it's already been done (or none) */
4817
4818 if (rbd_dev->parent_overlap)
4819 rbd_dev_parent_put(rbd_dev);
Alex Elder6fd48b32013-04-28 23:32:34 -05004820
4821 /* Free dynamic fields from the header, then zero it out */
4822
4823 header = &rbd_dev->header;
Alex Elder812164f82013-04-30 00:44:32 -05004824 ceph_put_snap_context(header->snapc);
Alex Elder6fd48b32013-04-28 23:32:34 -05004825 kfree(header->snap_sizes);
4826 kfree(header->snap_names);
4827 kfree(header->object_prefix);
4828 memset(header, 0, sizeof (*header));
4829}
4830
Alex Elder2df3fac2013-05-06 09:51:30 -05004831static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev)
Alex Eldera30b71b2012-07-10 20:30:11 -05004832{
4833 int ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05004834
Alex Elder1e130192012-07-03 16:01:19 -05004835 ret = rbd_dev_v2_object_prefix(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05004836 if (ret)
Alex Elder1e130192012-07-03 16:01:19 -05004837 goto out_err;
Alex Elderb1b54022012-07-03 16:01:19 -05004838
Alex Elder2df3fac2013-05-06 09:51:30 -05004839 /*
4840 * Get the and check features for the image. Currently the
4841 * features are assumed to never change.
4842 */
Alex Elderb1b54022012-07-03 16:01:19 -05004843 ret = rbd_dev_v2_features(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05004844 if (ret)
Alex Elderb1b54022012-07-03 16:01:19 -05004845 goto out_err;
Alex Elder35d489f2012-07-03 16:01:19 -05004846
Alex Eldercc070d52013-04-21 12:14:45 -05004847 /* If the image supports fancy striping, get its parameters */
4848
4849 if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) {
4850 ret = rbd_dev_v2_striping_info(rbd_dev);
4851 if (ret < 0)
4852 goto out_err;
4853 }
Alex Elder2df3fac2013-05-06 09:51:30 -05004854 /* No support for crypto and compression type format 2 images */
Alex Eldera30b71b2012-07-10 20:30:11 -05004855
Alex Elder35152972012-08-31 17:29:55 -05004856 return 0;
Alex Elder9d475de2012-07-03 16:01:19 -05004857out_err:
Alex Elder642a2532013-05-06 17:40:33 -05004858 rbd_dev->header.features = 0;
Alex Elder1e130192012-07-03 16:01:19 -05004859 kfree(rbd_dev->header.object_prefix);
4860 rbd_dev->header.object_prefix = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05004861
4862 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05004863}
4864
Alex Elder124afba2013-04-26 15:44:36 -05004865static int rbd_dev_probe_parent(struct rbd_device *rbd_dev)
Alex Elder83a06262012-10-30 15:47:17 -05004866{
Alex Elder2f82ee52012-10-30 19:40:33 -05004867 struct rbd_device *parent = NULL;
Alex Elder124afba2013-04-26 15:44:36 -05004868 struct rbd_spec *parent_spec;
4869 struct rbd_client *rbdc;
4870 int ret;
4871
4872 if (!rbd_dev->parent_spec)
4873 return 0;
4874 /*
4875 * We need to pass a reference to the client and the parent
4876 * spec when creating the parent rbd_dev. Images related by
4877 * parent/child relationships always share both.
4878 */
4879 parent_spec = rbd_spec_get(rbd_dev->parent_spec);
4880 rbdc = __rbd_get_client(rbd_dev->rbd_client);
4881
4882 ret = -ENOMEM;
4883 parent = rbd_dev_create(rbdc, parent_spec);
4884 if (!parent)
4885 goto out_err;
4886
Alex Elder1f3ef782013-05-06 17:40:33 -05004887 ret = rbd_dev_image_probe(parent, false);
Alex Elder124afba2013-04-26 15:44:36 -05004888 if (ret < 0)
4889 goto out_err;
4890 rbd_dev->parent = parent;
Alex Eldera2acd002013-05-08 22:50:04 -05004891 atomic_set(&rbd_dev->parent_ref, 1);
Alex Elder124afba2013-04-26 15:44:36 -05004892
4893 return 0;
4894out_err:
4895 if (parent) {
Alex Elderfb65d2282013-05-08 22:50:04 -05004896 rbd_dev_unparent(rbd_dev);
Alex Elder124afba2013-04-26 15:44:36 -05004897 kfree(rbd_dev->header_name);
4898 rbd_dev_destroy(parent);
4899 } else {
4900 rbd_put_client(rbdc);
4901 rbd_spec_put(parent_spec);
4902 }
4903
4904 return ret;
4905}
4906
Alex Elder200a6a82013-04-28 23:32:34 -05004907static int rbd_dev_device_setup(struct rbd_device *rbd_dev)
Alex Elder124afba2013-04-26 15:44:36 -05004908{
Alex Elder83a06262012-10-30 15:47:17 -05004909 int ret;
Alex Elder83a06262012-10-30 15:47:17 -05004910
Ilya Dryomovf8a22fc2013-12-13 15:28:57 +02004911 /* Get an id and fill in device name. */
Alex Elder83a06262012-10-30 15:47:17 -05004912
Ilya Dryomovf8a22fc2013-12-13 15:28:57 +02004913 ret = rbd_dev_id_get(rbd_dev);
4914 if (ret)
4915 return ret;
4916
Alex Elder83a06262012-10-30 15:47:17 -05004917 BUILD_BUG_ON(DEV_NAME_LEN
4918 < sizeof (RBD_DRV_NAME) + MAX_INT_FORMAT_WIDTH);
4919 sprintf(rbd_dev->name, "%s%d", RBD_DRV_NAME, rbd_dev->dev_id);
4920
Ilya Dryomov9b60e702013-12-13 15:28:57 +02004921 /* Record our major and minor device numbers. */
Alex Elder83a06262012-10-30 15:47:17 -05004922
Ilya Dryomov9b60e702013-12-13 15:28:57 +02004923 if (!single_major) {
4924 ret = register_blkdev(0, rbd_dev->name);
4925 if (ret < 0)
4926 goto err_out_id;
4927
4928 rbd_dev->major = ret;
4929 rbd_dev->minor = 0;
4930 } else {
4931 rbd_dev->major = rbd_major;
4932 rbd_dev->minor = rbd_dev_id_to_minor(rbd_dev->dev_id);
4933 }
Alex Elder83a06262012-10-30 15:47:17 -05004934
4935 /* Set up the blkdev mapping. */
4936
4937 ret = rbd_init_disk(rbd_dev);
4938 if (ret)
4939 goto err_out_blkdev;
4940
Alex Elderf35a4de2013-05-06 09:51:29 -05004941 ret = rbd_dev_mapping_set(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05004942 if (ret)
4943 goto err_out_disk;
Alex Elderf35a4de2013-05-06 09:51:29 -05004944 set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
4945
4946 ret = rbd_bus_add_dev(rbd_dev);
4947 if (ret)
4948 goto err_out_mapping;
Alex Elder83a06262012-10-30 15:47:17 -05004949
Alex Elder83a06262012-10-30 15:47:17 -05004950 /* Everything's ready. Announce the disk to the world. */
4951
Alex Elder129b79d2013-04-26 15:44:36 -05004952 set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
Alex Elder83a06262012-10-30 15:47:17 -05004953 add_disk(rbd_dev->disk);
4954
4955 pr_info("%s: added with size 0x%llx\n", rbd_dev->disk->disk_name,
4956 (unsigned long long) rbd_dev->mapping.size);
4957
4958 return ret;
Alex Elder2f82ee52012-10-30 19:40:33 -05004959
Alex Elderf35a4de2013-05-06 09:51:29 -05004960err_out_mapping:
4961 rbd_dev_mapping_clear(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05004962err_out_disk:
4963 rbd_free_disk(rbd_dev);
4964err_out_blkdev:
Ilya Dryomov9b60e702013-12-13 15:28:57 +02004965 if (!single_major)
4966 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Alex Elder83a06262012-10-30 15:47:17 -05004967err_out_id:
4968 rbd_dev_id_put(rbd_dev);
Alex Elderd1cf5782013-04-27 09:59:30 -05004969 rbd_dev_mapping_clear(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05004970
4971 return ret;
4972}
4973
Alex Elder332bb122013-04-27 09:59:30 -05004974static int rbd_dev_header_name(struct rbd_device *rbd_dev)
4975{
4976 struct rbd_spec *spec = rbd_dev->spec;
4977 size_t size;
4978
4979 /* Record the header object name for this rbd image. */
4980
4981 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
4982
4983 if (rbd_dev->image_format == 1)
4984 size = strlen(spec->image_name) + sizeof (RBD_SUFFIX);
4985 else
4986 size = sizeof (RBD_HEADER_PREFIX) + strlen(spec->image_id);
4987
4988 rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
4989 if (!rbd_dev->header_name)
4990 return -ENOMEM;
4991
4992 if (rbd_dev->image_format == 1)
4993 sprintf(rbd_dev->header_name, "%s%s",
4994 spec->image_name, RBD_SUFFIX);
4995 else
4996 sprintf(rbd_dev->header_name, "%s%s",
4997 RBD_HEADER_PREFIX, spec->image_id);
4998 return 0;
4999}
5000
Alex Elder200a6a82013-04-28 23:32:34 -05005001static void rbd_dev_image_release(struct rbd_device *rbd_dev)
5002{
Alex Elder6fd48b32013-04-28 23:32:34 -05005003 rbd_dev_unprobe(rbd_dev);
Alex Elder200a6a82013-04-28 23:32:34 -05005004 kfree(rbd_dev->header_name);
Alex Elder6fd48b32013-04-28 23:32:34 -05005005 rbd_dev->header_name = NULL;
5006 rbd_dev->image_format = 0;
5007 kfree(rbd_dev->spec->image_id);
5008 rbd_dev->spec->image_id = NULL;
5009
Alex Elder200a6a82013-04-28 23:32:34 -05005010 rbd_dev_destroy(rbd_dev);
5011}
5012
Alex Eldera30b71b2012-07-10 20:30:11 -05005013/*
5014 * Probe for the existence of the header object for the given rbd
Alex Elder1f3ef782013-05-06 17:40:33 -05005015 * device. If this image is the one being mapped (i.e., not a
5016 * parent), initiate a watch on its header object before using that
5017 * object to get detailed information about the rbd image.
Alex Eldera30b71b2012-07-10 20:30:11 -05005018 */
Alex Elder1f3ef782013-05-06 17:40:33 -05005019static int rbd_dev_image_probe(struct rbd_device *rbd_dev, bool mapping)
Alex Eldera30b71b2012-07-10 20:30:11 -05005020{
5021 int ret;
5022
5023 /*
Alex Elder3abef3b2013-05-13 20:35:37 -05005024 * Get the id from the image id object. Unless there's an
5025 * error, rbd_dev->spec->image_id will be filled in with
5026 * a dynamically-allocated string, and rbd_dev->image_format
5027 * will be set to either 1 or 2.
Alex Eldera30b71b2012-07-10 20:30:11 -05005028 */
5029 ret = rbd_dev_image_id(rbd_dev);
5030 if (ret)
Alex Elderc0fba362013-04-25 23:15:08 -05005031 return ret;
5032 rbd_assert(rbd_dev->spec->image_id);
5033 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
5034
Alex Elder332bb122013-04-27 09:59:30 -05005035 ret = rbd_dev_header_name(rbd_dev);
5036 if (ret)
5037 goto err_out_format;
5038
Alex Elder1f3ef782013-05-06 17:40:33 -05005039 if (mapping) {
Ilya Dryomovfca27062013-12-16 18:02:40 +02005040 ret = rbd_dev_header_watch_sync(rbd_dev);
Alex Elder1f3ef782013-05-06 17:40:33 -05005041 if (ret)
5042 goto out_header_name;
5043 }
Alex Elderb644de22013-04-27 09:59:31 -05005044
Alex Elderc0fba362013-04-25 23:15:08 -05005045 if (rbd_dev->image_format == 1)
Alex Elder99a41eb2013-05-06 09:51:30 -05005046 ret = rbd_dev_v1_header_info(rbd_dev);
Alex Eldera30b71b2012-07-10 20:30:11 -05005047 else
Alex Elder2df3fac2013-05-06 09:51:30 -05005048 ret = rbd_dev_v2_header_info(rbd_dev);
Alex Elder5655c4d2013-04-25 23:15:08 -05005049 if (ret)
Alex Elderb644de22013-04-27 09:59:31 -05005050 goto err_out_watch;
Alex Elder83a06262012-10-30 15:47:17 -05005051
Alex Elder9bb81c92013-04-27 09:59:30 -05005052 ret = rbd_dev_spec_update(rbd_dev);
5053 if (ret)
Alex Elder33dca392013-04-30 00:44:33 -05005054 goto err_out_probe;
Alex Elder9bb81c92013-04-27 09:59:30 -05005055
5056 ret = rbd_dev_probe_parent(rbd_dev);
Alex Elder30d60ba2013-05-06 09:51:30 -05005057 if (ret)
5058 goto err_out_probe;
Alex Elder83a06262012-10-30 15:47:17 -05005059
Alex Elder30d60ba2013-05-06 09:51:30 -05005060 dout("discovered format %u image, header name is %s\n",
5061 rbd_dev->image_format, rbd_dev->header_name);
5062
5063 return 0;
Alex Elder6fd48b32013-04-28 23:32:34 -05005064err_out_probe:
5065 rbd_dev_unprobe(rbd_dev);
Alex Elderb644de22013-04-27 09:59:31 -05005066err_out_watch:
Ilya Dryomovfca27062013-12-16 18:02:40 +02005067 if (mapping)
5068 rbd_dev_header_unwatch_sync(rbd_dev);
Alex Elder332bb122013-04-27 09:59:30 -05005069out_header_name:
5070 kfree(rbd_dev->header_name);
5071 rbd_dev->header_name = NULL;
5072err_out_format:
5073 rbd_dev->image_format = 0;
Alex Elder5655c4d2013-04-25 23:15:08 -05005074 kfree(rbd_dev->spec->image_id);
5075 rbd_dev->spec->image_id = NULL;
5076
5077 dout("probe failed, returning %d\n", ret);
5078
5079 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05005080}
5081
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005082static ssize_t do_rbd_add(struct bus_type *bus,
5083 const char *buf,
5084 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005085{
Alex Eldercb8627c2012-07-09 21:04:23 -05005086 struct rbd_device *rbd_dev = NULL;
Alex Elderdc79b112012-10-25 23:34:41 -05005087 struct ceph_options *ceph_opts = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05005088 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05005089 struct rbd_spec *spec = NULL;
Alex Elder9d3997f2012-10-25 23:34:42 -05005090 struct rbd_client *rbdc;
Alex Elder27cc2592012-02-02 08:13:30 -06005091 struct ceph_osd_client *osdc;
Alex Elder51344a32013-05-06 07:40:30 -05005092 bool read_only;
Alex Elder27cc2592012-02-02 08:13:30 -06005093 int rc = -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005094
5095 if (!try_module_get(THIS_MODULE))
5096 return -ENODEV;
5097
Alex Eldera725f65e2012-02-02 08:13:30 -06005098 /* parse add command */
Alex Elder859c31d2012-10-25 23:34:42 -05005099 rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec);
Alex Elderdc79b112012-10-25 23:34:41 -05005100 if (rc < 0)
Alex Elderbd4ba652012-10-25 23:34:42 -05005101 goto err_out_module;
Alex Elder51344a32013-05-06 07:40:30 -05005102 read_only = rbd_opts->read_only;
5103 kfree(rbd_opts);
5104 rbd_opts = NULL; /* done with this */
Alex Eldera725f65e2012-02-02 08:13:30 -06005105
Alex Elder9d3997f2012-10-25 23:34:42 -05005106 rbdc = rbd_get_client(ceph_opts);
5107 if (IS_ERR(rbdc)) {
5108 rc = PTR_ERR(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05005109 goto err_out_args;
Alex Elder9d3997f2012-10-25 23:34:42 -05005110 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005111
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005112 /* pick the pool */
Alex Elder9d3997f2012-10-25 23:34:42 -05005113 osdc = &rbdc->client->osdc;
Alex Elder859c31d2012-10-25 23:34:42 -05005114 rc = ceph_pg_poolid_by_name(osdc->osdmap, spec->pool_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005115 if (rc < 0)
5116 goto err_out_client;
Alex Elderc0cd10db2013-04-26 09:43:47 -05005117 spec->pool_id = (u64)rc;
Alex Elder859c31d2012-10-25 23:34:42 -05005118
Alex Elder0903e872012-11-14 12:25:19 -06005119 /* The ceph file layout needs to fit pool id in 32 bits */
5120
Alex Elderc0cd10db2013-04-26 09:43:47 -05005121 if (spec->pool_id > (u64)U32_MAX) {
5122 rbd_warn(NULL, "pool id too large (%llu > %u)\n",
5123 (unsigned long long)spec->pool_id, U32_MAX);
Alex Elder0903e872012-11-14 12:25:19 -06005124 rc = -EIO;
5125 goto err_out_client;
5126 }
5127
Alex Elderc53d5892012-10-25 23:34:42 -05005128 rbd_dev = rbd_dev_create(rbdc, spec);
Alex Elderbd4ba652012-10-25 23:34:42 -05005129 if (!rbd_dev)
5130 goto err_out_client;
Alex Elderc53d5892012-10-25 23:34:42 -05005131 rbdc = NULL; /* rbd_dev now owns this */
5132 spec = NULL; /* rbd_dev now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005133
Alex Elder1f3ef782013-05-06 17:40:33 -05005134 rc = rbd_dev_image_probe(rbd_dev, true);
Alex Eldera30b71b2012-07-10 20:30:11 -05005135 if (rc < 0)
Alex Elderc53d5892012-10-25 23:34:42 -05005136 goto err_out_rbd_dev;
Alex Elder05fd6f62012-08-29 17:11:07 -05005137
Alex Elder7ce4eef2013-05-06 17:40:33 -05005138 /* If we are mapping a snapshot it must be marked read-only */
5139
5140 if (rbd_dev->spec->snap_id != CEPH_NOSNAP)
5141 read_only = true;
5142 rbd_dev->mapping.read_only = read_only;
5143
Alex Elderb536f692013-04-28 23:32:34 -05005144 rc = rbd_dev_device_setup(rbd_dev);
Alex Elder3abef3b2013-05-13 20:35:37 -05005145 if (rc) {
Ilya Dryomove37180c2013-12-16 18:02:41 +02005146 /*
5147 * rbd_dev_header_unwatch_sync() can't be moved into
5148 * rbd_dev_image_release() without refactoring, see
5149 * commit 1f3ef78861ac.
5150 */
5151 rbd_dev_header_unwatch_sync(rbd_dev);
Alex Elder3abef3b2013-05-13 20:35:37 -05005152 rbd_dev_image_release(rbd_dev);
5153 goto err_out_module;
5154 }
Alex Elderb536f692013-04-28 23:32:34 -05005155
Alex Elder3abef3b2013-05-13 20:35:37 -05005156 return count;
5157
Alex Elderc53d5892012-10-25 23:34:42 -05005158err_out_rbd_dev:
5159 rbd_dev_destroy(rbd_dev);
Alex Elderbd4ba652012-10-25 23:34:42 -05005160err_out_client:
Alex Elder9d3997f2012-10-25 23:34:42 -05005161 rbd_put_client(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05005162err_out_args:
Alex Elder859c31d2012-10-25 23:34:42 -05005163 rbd_spec_put(spec);
Alex Elderbd4ba652012-10-25 23:34:42 -05005164err_out_module:
5165 module_put(THIS_MODULE);
Alex Elder27cc2592012-02-02 08:13:30 -06005166
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005167 dout("Error adding device %s\n", buf);
Alex Elder27cc2592012-02-02 08:13:30 -06005168
Alex Elderc0cd10db2013-04-26 09:43:47 -05005169 return (ssize_t)rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005170}
5171
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005172static ssize_t rbd_add(struct bus_type *bus,
5173 const char *buf,
5174 size_t count)
5175{
5176 if (single_major)
5177 return -EINVAL;
5178
5179 return do_rbd_add(bus, buf, count);
5180}
5181
5182static ssize_t rbd_add_single_major(struct bus_type *bus,
5183 const char *buf,
5184 size_t count)
5185{
5186 return do_rbd_add(bus, buf, count);
5187}
5188
Alex Elder200a6a82013-04-28 23:32:34 -05005189static void rbd_dev_device_release(struct device *dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005190{
Alex Elder593a9e72012-02-07 12:03:37 -06005191 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005192
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005193 rbd_free_disk(rbd_dev);
Alex Elder200a6a82013-04-28 23:32:34 -05005194 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
Alex Elder6d80b132013-05-06 07:40:30 -05005195 rbd_dev_mapping_clear(rbd_dev);
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005196 if (!single_major)
5197 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Alex Eldere2839302012-08-29 17:11:06 -05005198 rbd_dev_id_put(rbd_dev);
Alex Elderd1cf5782013-04-27 09:59:30 -05005199 rbd_dev_mapping_clear(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005200}
5201
Alex Elder05a46af2013-04-26 15:44:36 -05005202static void rbd_dev_remove_parent(struct rbd_device *rbd_dev)
5203{
Alex Elderad945fc2013-04-26 15:44:36 -05005204 while (rbd_dev->parent) {
Alex Elder05a46af2013-04-26 15:44:36 -05005205 struct rbd_device *first = rbd_dev;
5206 struct rbd_device *second = first->parent;
5207 struct rbd_device *third;
5208
5209 /*
5210 * Follow to the parent with no grandparent and
5211 * remove it.
5212 */
5213 while (second && (third = second->parent)) {
5214 first = second;
5215 second = third;
5216 }
Alex Elderad945fc2013-04-26 15:44:36 -05005217 rbd_assert(second);
Alex Elder8ad42cd2013-04-28 23:32:34 -05005218 rbd_dev_image_release(second);
Alex Elderad945fc2013-04-26 15:44:36 -05005219 first->parent = NULL;
5220 first->parent_overlap = 0;
5221
5222 rbd_assert(first->parent_spec);
Alex Elder05a46af2013-04-26 15:44:36 -05005223 rbd_spec_put(first->parent_spec);
5224 first->parent_spec = NULL;
Alex Elder05a46af2013-04-26 15:44:36 -05005225 }
5226}
5227
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005228static ssize_t do_rbd_remove(struct bus_type *bus,
5229 const char *buf,
5230 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005231{
5232 struct rbd_device *rbd_dev = NULL;
Alex Elder751cc0e2013-05-31 15:17:01 -05005233 struct list_head *tmp;
5234 int dev_id;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005235 unsigned long ul;
Alex Elder82a442d2013-05-31 17:40:44 -05005236 bool already = false;
Alex Elder0d8189e2013-04-27 09:59:30 -05005237 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005238
Jingoo Hanbb8e0e82013-09-11 14:20:07 -07005239 ret = kstrtoul(buf, 10, &ul);
Alex Elder0d8189e2013-04-27 09:59:30 -05005240 if (ret)
5241 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005242
5243 /* convert to int; abort if we lost anything in the conversion */
Alex Elder751cc0e2013-05-31 15:17:01 -05005244 dev_id = (int)ul;
5245 if (dev_id != ul)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005246 return -EINVAL;
5247
Alex Elder751cc0e2013-05-31 15:17:01 -05005248 ret = -ENOENT;
5249 spin_lock(&rbd_dev_list_lock);
5250 list_for_each(tmp, &rbd_dev_list) {
5251 rbd_dev = list_entry(tmp, struct rbd_device, node);
5252 if (rbd_dev->dev_id == dev_id) {
5253 ret = 0;
5254 break;
5255 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005256 }
Alex Elder751cc0e2013-05-31 15:17:01 -05005257 if (!ret) {
5258 spin_lock_irq(&rbd_dev->lock);
5259 if (rbd_dev->open_count)
5260 ret = -EBUSY;
5261 else
Alex Elder82a442d2013-05-31 17:40:44 -05005262 already = test_and_set_bit(RBD_DEV_FLAG_REMOVING,
5263 &rbd_dev->flags);
Alex Elder751cc0e2013-05-31 15:17:01 -05005264 spin_unlock_irq(&rbd_dev->lock);
5265 }
5266 spin_unlock(&rbd_dev_list_lock);
Alex Elder82a442d2013-05-31 17:40:44 -05005267 if (ret < 0 || already)
Alex Elder1ba0f1e2013-05-31 15:17:01 -05005268 return ret;
Alex Elder751cc0e2013-05-31 15:17:01 -05005269
Ilya Dryomovfca27062013-12-16 18:02:40 +02005270 rbd_dev_header_unwatch_sync(rbd_dev);
Josh Durgin9abc5992013-08-29 17:31:03 -07005271 /*
5272 * flush remaining watch callbacks - these must be complete
5273 * before the osd_client is shutdown
5274 */
5275 dout("%s: flushing notifies", __func__);
5276 ceph_osdc_flush_notifies(&rbd_dev->rbd_client->client->osdc);
Ilya Dryomovfca27062013-12-16 18:02:40 +02005277
Josh Durgin98752012013-08-29 17:26:31 -07005278 /*
5279 * Don't free anything from rbd_dev->disk until after all
5280 * notifies are completely processed. Otherwise
5281 * rbd_bus_del_dev() will race with rbd_watch_cb(), resulting
5282 * in a potential use after free of rbd_dev->disk or rbd_dev.
5283 */
5284 rbd_bus_del_dev(rbd_dev);
Alex Elder8ad42cd2013-04-28 23:32:34 -05005285 rbd_dev_image_release(rbd_dev);
Alex Elder79ab7552013-04-28 23:32:34 -05005286 module_put(THIS_MODULE);
Alex Elderaafb2302012-09-06 16:00:54 -05005287
Alex Elder1ba0f1e2013-05-31 15:17:01 -05005288 return count;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005289}
5290
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005291static ssize_t rbd_remove(struct bus_type *bus,
5292 const char *buf,
5293 size_t count)
5294{
5295 if (single_major)
5296 return -EINVAL;
5297
5298 return do_rbd_remove(bus, buf, count);
5299}
5300
5301static ssize_t rbd_remove_single_major(struct bus_type *bus,
5302 const char *buf,
5303 size_t count)
5304{
5305 return do_rbd_remove(bus, buf, count);
5306}
5307
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005308/*
5309 * create control files in sysfs
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005310 * /sys/bus/rbd/...
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005311 */
5312static int rbd_sysfs_init(void)
5313{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005314 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005315
Alex Elderfed4c142012-02-07 12:03:36 -06005316 ret = device_register(&rbd_root_dev);
Alex Elder21079782012-01-24 10:08:36 -06005317 if (ret < 0)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005318 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005319
Alex Elderfed4c142012-02-07 12:03:36 -06005320 ret = bus_register(&rbd_bus_type);
5321 if (ret < 0)
5322 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005323
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005324 return ret;
5325}
5326
5327static void rbd_sysfs_cleanup(void)
5328{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005329 bus_unregister(&rbd_bus_type);
Alex Elderfed4c142012-02-07 12:03:36 -06005330 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005331}
5332
Alex Elder1c2a9df2013-05-01 12:43:03 -05005333static int rbd_slab_init(void)
5334{
5335 rbd_assert(!rbd_img_request_cache);
5336 rbd_img_request_cache = kmem_cache_create("rbd_img_request",
5337 sizeof (struct rbd_img_request),
5338 __alignof__(struct rbd_img_request),
5339 0, NULL);
Alex Elder868311b2013-05-01 12:43:03 -05005340 if (!rbd_img_request_cache)
5341 return -ENOMEM;
5342
5343 rbd_assert(!rbd_obj_request_cache);
5344 rbd_obj_request_cache = kmem_cache_create("rbd_obj_request",
5345 sizeof (struct rbd_obj_request),
5346 __alignof__(struct rbd_obj_request),
5347 0, NULL);
Alex Elder78c2a442013-05-01 12:43:04 -05005348 if (!rbd_obj_request_cache)
5349 goto out_err;
5350
5351 rbd_assert(!rbd_segment_name_cache);
5352 rbd_segment_name_cache = kmem_cache_create("rbd_segment_name",
Ilya Dryomov2d0ebc52014-01-27 17:40:18 +02005353 CEPH_MAX_OID_NAME_LEN + 1, 1, 0, NULL);
Alex Elder78c2a442013-05-01 12:43:04 -05005354 if (rbd_segment_name_cache)
Alex Elder1c2a9df2013-05-01 12:43:03 -05005355 return 0;
Alex Elder78c2a442013-05-01 12:43:04 -05005356out_err:
5357 if (rbd_obj_request_cache) {
5358 kmem_cache_destroy(rbd_obj_request_cache);
5359 rbd_obj_request_cache = NULL;
5360 }
Alex Elder1c2a9df2013-05-01 12:43:03 -05005361
Alex Elder868311b2013-05-01 12:43:03 -05005362 kmem_cache_destroy(rbd_img_request_cache);
5363 rbd_img_request_cache = NULL;
5364
Alex Elder1c2a9df2013-05-01 12:43:03 -05005365 return -ENOMEM;
5366}
5367
5368static void rbd_slab_exit(void)
5369{
Alex Elder78c2a442013-05-01 12:43:04 -05005370 rbd_assert(rbd_segment_name_cache);
5371 kmem_cache_destroy(rbd_segment_name_cache);
5372 rbd_segment_name_cache = NULL;
5373
Alex Elder868311b2013-05-01 12:43:03 -05005374 rbd_assert(rbd_obj_request_cache);
5375 kmem_cache_destroy(rbd_obj_request_cache);
5376 rbd_obj_request_cache = NULL;
5377
Alex Elder1c2a9df2013-05-01 12:43:03 -05005378 rbd_assert(rbd_img_request_cache);
5379 kmem_cache_destroy(rbd_img_request_cache);
5380 rbd_img_request_cache = NULL;
5381}
5382
Alex Eldercc344fa2013-02-19 12:25:56 -06005383static int __init rbd_init(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005384{
5385 int rc;
5386
Alex Elder1e32d342013-01-30 11:13:33 -06005387 if (!libceph_compatible(NULL)) {
5388 rbd_warn(NULL, "libceph incompatibility (quitting)");
Alex Elder1e32d342013-01-30 11:13:33 -06005389 return -EINVAL;
5390 }
Ilya Dryomove1b4d962013-12-13 15:28:57 +02005391
Alex Elder1c2a9df2013-05-01 12:43:03 -05005392 rc = rbd_slab_init();
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005393 if (rc)
5394 return rc;
Ilya Dryomove1b4d962013-12-13 15:28:57 +02005395
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005396 if (single_major) {
5397 rbd_major = register_blkdev(0, RBD_DRV_NAME);
5398 if (rbd_major < 0) {
5399 rc = rbd_major;
5400 goto err_out_slab;
5401 }
5402 }
5403
Alex Elder1c2a9df2013-05-01 12:43:03 -05005404 rc = rbd_sysfs_init();
5405 if (rc)
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005406 goto err_out_blkdev;
Alex Elder1c2a9df2013-05-01 12:43:03 -05005407
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005408 if (single_major)
5409 pr_info("loaded (major %d)\n", rbd_major);
5410 else
5411 pr_info("loaded\n");
5412
Ilya Dryomove1b4d962013-12-13 15:28:57 +02005413 return 0;
5414
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005415err_out_blkdev:
5416 if (single_major)
5417 unregister_blkdev(rbd_major, RBD_DRV_NAME);
Ilya Dryomove1b4d962013-12-13 15:28:57 +02005418err_out_slab:
5419 rbd_slab_exit();
Alex Elder1c2a9df2013-05-01 12:43:03 -05005420 return rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005421}
5422
Alex Eldercc344fa2013-02-19 12:25:56 -06005423static void __exit rbd_exit(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005424{
5425 rbd_sysfs_cleanup();
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005426 if (single_major)
5427 unregister_blkdev(rbd_major, RBD_DRV_NAME);
Alex Elder1c2a9df2013-05-01 12:43:03 -05005428 rbd_slab_exit();
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005429}
5430
5431module_init(rbd_init);
5432module_exit(rbd_exit);
5433
Alex Elderd552c612013-05-31 20:13:09 -05005434MODULE_AUTHOR("Alex Elder <elder@inktank.com>");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005435MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
5436MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005437/* following authorship retained from original osdblk.c */
5438MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
5439
Ilya Dryomov90da2582013-12-13 15:28:56 +02005440MODULE_DESCRIPTION("RADOS Block Device (RBD) driver");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005441MODULE_LICENSE("GPL");