blob: 8295b3afa8e0f8015217af4326f1a098aa8c7e67 [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{
Kent Overstreet79886132013-11-23 17:19:00 -08001159 struct bio_vec bv;
1160 struct bvec_iter iter;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001161 unsigned long flags;
1162 void *buf;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001163 int pos = 0;
1164
1165 while (chain) {
Kent Overstreet79886132013-11-23 17:19:00 -08001166 bio_for_each_segment(bv, chain, iter) {
1167 if (pos + bv.bv_len > start_ofs) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001168 int remainder = max(start_ofs - pos, 0);
Kent Overstreet79886132013-11-23 17:19:00 -08001169 buf = bvec_kmap_irq(&bv, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001170 memset(buf + remainder, 0,
Kent Overstreet79886132013-11-23 17:19:00 -08001171 bv.bv_len - remainder);
1172 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 }
Kent Overstreet79886132013-11-23 17:19:00 -08001175 pos += bv.bv_len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001176 }
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 *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001224
Kent Overstreet5341a6272013-08-07 14:31:11 -07001225 bio = bio_clone(bio_src, gfpmask);
Alex Elderf7760da2012-10-20 22:17:27 -05001226 if (!bio)
1227 return NULL; /* ENOMEM */
1228
Kent Overstreet5341a6272013-08-07 14:31:11 -07001229 bio_advance(bio, offset);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001230 bio->bi_iter.bi_size = len;
Alex Elder542582f2012-08-09 10:33:25 -07001231
Alex Elderf7760da2012-10-20 22:17:27 -05001232 return bio;
1233}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001234
Alex Elderf7760da2012-10-20 22:17:27 -05001235/*
1236 * Clone a portion of a bio chain, starting at the given byte offset
1237 * into the first bio in the source chain and continuing for the
1238 * number of bytes indicated. The result is another bio chain of
1239 * exactly the given length, or a null pointer on error.
1240 *
1241 * The bio_src and offset parameters are both in-out. On entry they
1242 * refer to the first source bio and the offset into that bio where
1243 * the start of data to be cloned is located.
1244 *
1245 * On return, bio_src is updated to refer to the bio in the source
1246 * chain that contains first un-cloned byte, and *offset will
1247 * contain the offset of that byte within that bio.
1248 */
1249static struct bio *bio_chain_clone_range(struct bio **bio_src,
1250 unsigned int *offset,
1251 unsigned int len,
1252 gfp_t gfpmask)
1253{
1254 struct bio *bi = *bio_src;
1255 unsigned int off = *offset;
1256 struct bio *chain = NULL;
1257 struct bio **end;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001258
Alex Elderf7760da2012-10-20 22:17:27 -05001259 /* Build up a chain of clone bios up to the limit */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001260
Kent Overstreet4f024f32013-10-11 15:44:27 -07001261 if (!bi || off >= bi->bi_iter.bi_size || !len)
Alex Elderf7760da2012-10-20 22:17:27 -05001262 return NULL; /* Nothing to clone */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001263
Alex Elderf7760da2012-10-20 22:17:27 -05001264 end = &chain;
1265 while (len) {
1266 unsigned int bi_size;
1267 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001268
Alex Elderf5400b72012-11-01 10:17:15 -05001269 if (!bi) {
1270 rbd_warn(NULL, "bio_chain exhausted with %u left", len);
Alex Elderf7760da2012-10-20 22:17:27 -05001271 goto out_err; /* EINVAL; ran out of bio's */
Alex Elderf5400b72012-11-01 10:17:15 -05001272 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07001273 bi_size = min_t(unsigned int, bi->bi_iter.bi_size - off, len);
Alex Elderf7760da2012-10-20 22:17:27 -05001274 bio = bio_clone_range(bi, off, bi_size, gfpmask);
1275 if (!bio)
1276 goto out_err; /* ENOMEM */
1277
1278 *end = bio;
1279 end = &bio->bi_next;
1280
1281 off += bi_size;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001282 if (off == bi->bi_iter.bi_size) {
Alex Elderf7760da2012-10-20 22:17:27 -05001283 bi = bi->bi_next;
1284 off = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001285 }
Alex Elderf7760da2012-10-20 22:17:27 -05001286 len -= bi_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001287 }
Alex Elderf7760da2012-10-20 22:17:27 -05001288 *bio_src = bi;
1289 *offset = off;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001290
Alex Elderf7760da2012-10-20 22:17:27 -05001291 return chain;
1292out_err:
1293 bio_chain_put(chain);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001294
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001295 return NULL;
1296}
1297
Alex Elder926f9b32013-02-11 12:33:24 -06001298/*
1299 * The default/initial value for all object request flags is 0. For
1300 * each flag, once its value is set to 1 it is never reset to 0
1301 * again.
1302 */
Alex Elder6365d332013-02-11 12:33:24 -06001303static void obj_request_img_data_set(struct rbd_obj_request *obj_request)
1304{
1305 if (test_and_set_bit(OBJ_REQ_IMG_DATA, &obj_request->flags)) {
Alex Elder6365d332013-02-11 12:33:24 -06001306 struct rbd_device *rbd_dev;
1307
Alex Elder57acbaa2013-02-11 12:33:24 -06001308 rbd_dev = obj_request->img_request->rbd_dev;
Alex Elder6365d332013-02-11 12:33:24 -06001309 rbd_warn(rbd_dev, "obj_request %p already marked img_data\n",
1310 obj_request);
1311 }
1312}
1313
1314static bool obj_request_img_data_test(struct rbd_obj_request *obj_request)
1315{
1316 smp_mb();
1317 return test_bit(OBJ_REQ_IMG_DATA, &obj_request->flags) != 0;
1318}
1319
Alex Elder57acbaa2013-02-11 12:33:24 -06001320static void obj_request_done_set(struct rbd_obj_request *obj_request)
1321{
1322 if (test_and_set_bit(OBJ_REQ_DONE, &obj_request->flags)) {
1323 struct rbd_device *rbd_dev = NULL;
1324
1325 if (obj_request_img_data_test(obj_request))
1326 rbd_dev = obj_request->img_request->rbd_dev;
1327 rbd_warn(rbd_dev, "obj_request %p already marked done\n",
1328 obj_request);
1329 }
1330}
1331
1332static bool obj_request_done_test(struct rbd_obj_request *obj_request)
1333{
1334 smp_mb();
1335 return test_bit(OBJ_REQ_DONE, &obj_request->flags) != 0;
1336}
1337
Alex Elder5679c592013-02-11 12:33:24 -06001338/*
1339 * This sets the KNOWN flag after (possibly) setting the EXISTS
1340 * flag. The latter is set based on the "exists" value provided.
1341 *
1342 * Note that for our purposes once an object exists it never goes
1343 * away again. It's possible that the response from two existence
1344 * checks are separated by the creation of the target object, and
1345 * the first ("doesn't exist") response arrives *after* the second
1346 * ("does exist"). In that case we ignore the second one.
1347 */
1348static void obj_request_existence_set(struct rbd_obj_request *obj_request,
1349 bool exists)
1350{
1351 if (exists)
1352 set_bit(OBJ_REQ_EXISTS, &obj_request->flags);
1353 set_bit(OBJ_REQ_KNOWN, &obj_request->flags);
1354 smp_mb();
1355}
1356
1357static bool obj_request_known_test(struct rbd_obj_request *obj_request)
1358{
1359 smp_mb();
1360 return test_bit(OBJ_REQ_KNOWN, &obj_request->flags) != 0;
1361}
1362
1363static bool obj_request_exists_test(struct rbd_obj_request *obj_request)
1364{
1365 smp_mb();
1366 return test_bit(OBJ_REQ_EXISTS, &obj_request->flags) != 0;
1367}
1368
Alex Elderbf0d5f502012-11-22 00:00:08 -06001369static void rbd_obj_request_get(struct rbd_obj_request *obj_request)
1370{
Alex Elder37206ee2013-02-20 17:32:08 -06001371 dout("%s: obj %p (was %d)\n", __func__, obj_request,
1372 atomic_read(&obj_request->kref.refcount));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001373 kref_get(&obj_request->kref);
1374}
1375
1376static void rbd_obj_request_destroy(struct kref *kref);
1377static void rbd_obj_request_put(struct rbd_obj_request *obj_request)
1378{
1379 rbd_assert(obj_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001380 dout("%s: obj %p (was %d)\n", __func__, obj_request,
1381 atomic_read(&obj_request->kref.refcount));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001382 kref_put(&obj_request->kref, rbd_obj_request_destroy);
1383}
1384
Alex Elder0f2d5be2014-04-26 14:21:44 +04001385static void rbd_img_request_get(struct rbd_img_request *img_request)
1386{
1387 dout("%s: img %p (was %d)\n", __func__, img_request,
1388 atomic_read(&img_request->kref.refcount));
1389 kref_get(&img_request->kref);
1390}
1391
Alex Eldere93f3152013-05-08 22:50:04 -05001392static bool img_request_child_test(struct rbd_img_request *img_request);
1393static void rbd_parent_request_destroy(struct kref *kref);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001394static void rbd_img_request_destroy(struct kref *kref);
1395static void rbd_img_request_put(struct rbd_img_request *img_request)
1396{
1397 rbd_assert(img_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001398 dout("%s: img %p (was %d)\n", __func__, img_request,
1399 atomic_read(&img_request->kref.refcount));
Alex Eldere93f3152013-05-08 22:50:04 -05001400 if (img_request_child_test(img_request))
1401 kref_put(&img_request->kref, rbd_parent_request_destroy);
1402 else
1403 kref_put(&img_request->kref, rbd_img_request_destroy);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001404}
1405
1406static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request,
1407 struct rbd_obj_request *obj_request)
1408{
Alex Elder25dcf952013-01-25 17:08:55 -06001409 rbd_assert(obj_request->img_request == NULL);
1410
Alex Elderb155e862013-04-15 14:50:37 -05001411 /* Image request now owns object's original reference */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001412 obj_request->img_request = img_request;
Alex Elder25dcf952013-01-25 17:08:55 -06001413 obj_request->which = img_request->obj_request_count;
Alex Elder6365d332013-02-11 12:33:24 -06001414 rbd_assert(!obj_request_img_data_test(obj_request));
1415 obj_request_img_data_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001416 rbd_assert(obj_request->which != BAD_WHICH);
Alex Elder25dcf952013-01-25 17:08:55 -06001417 img_request->obj_request_count++;
1418 list_add_tail(&obj_request->links, &img_request->obj_requests);
Alex Elder37206ee2013-02-20 17:32:08 -06001419 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1420 obj_request->which);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001421}
1422
1423static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request,
1424 struct rbd_obj_request *obj_request)
1425{
1426 rbd_assert(obj_request->which != BAD_WHICH);
Alex Elder25dcf952013-01-25 17:08:55 -06001427
Alex Elder37206ee2013-02-20 17:32:08 -06001428 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1429 obj_request->which);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001430 list_del(&obj_request->links);
Alex Elder25dcf952013-01-25 17:08:55 -06001431 rbd_assert(img_request->obj_request_count > 0);
1432 img_request->obj_request_count--;
1433 rbd_assert(obj_request->which == img_request->obj_request_count);
1434 obj_request->which = BAD_WHICH;
Alex Elder6365d332013-02-11 12:33:24 -06001435 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001436 rbd_assert(obj_request->img_request == img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001437 obj_request->img_request = NULL;
Alex Elder25dcf952013-01-25 17:08:55 -06001438 obj_request->callback = NULL;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001439 rbd_obj_request_put(obj_request);
1440}
1441
1442static bool obj_request_type_valid(enum obj_request_type type)
1443{
1444 switch (type) {
Alex Elder9969ebc2013-01-18 12:31:10 -06001445 case OBJ_REQUEST_NODATA:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001446 case OBJ_REQUEST_BIO:
Alex Elder788e2df2013-01-17 12:25:27 -06001447 case OBJ_REQUEST_PAGES:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001448 return true;
1449 default:
1450 return false;
1451 }
1452}
1453
Alex Elderbf0d5f502012-11-22 00:00:08 -06001454static int rbd_obj_request_submit(struct ceph_osd_client *osdc,
1455 struct rbd_obj_request *obj_request)
1456{
Alex Elder37206ee2013-02-20 17:32:08 -06001457 dout("%s: osdc %p obj %p\n", __func__, osdc, obj_request);
1458
Alex Elderbf0d5f502012-11-22 00:00:08 -06001459 return ceph_osdc_start_request(osdc, obj_request->osd_req, false);
1460}
1461
1462static void rbd_img_request_complete(struct rbd_img_request *img_request)
1463{
Alex Elder55f27e02013-04-10 12:34:25 -05001464
Alex Elder37206ee2013-02-20 17:32:08 -06001465 dout("%s: img %p\n", __func__, img_request);
Alex Elder55f27e02013-04-10 12:34:25 -05001466
1467 /*
1468 * If no error occurred, compute the aggregate transfer
1469 * count for the image request. We could instead use
1470 * atomic64_cmpxchg() to update it as each object request
1471 * completes; not clear which way is better off hand.
1472 */
1473 if (!img_request->result) {
1474 struct rbd_obj_request *obj_request;
1475 u64 xferred = 0;
1476
1477 for_each_obj_request(img_request, obj_request)
1478 xferred += obj_request->xferred;
1479 img_request->xferred = xferred;
1480 }
1481
Alex Elderbf0d5f502012-11-22 00:00:08 -06001482 if (img_request->callback)
1483 img_request->callback(img_request);
1484 else
1485 rbd_img_request_put(img_request);
1486}
1487
Alex Elder788e2df2013-01-17 12:25:27 -06001488/* Caller is responsible for rbd_obj_request_destroy(obj_request) */
1489
1490static int rbd_obj_request_wait(struct rbd_obj_request *obj_request)
1491{
Alex Elder37206ee2013-02-20 17:32:08 -06001492 dout("%s: obj %p\n", __func__, obj_request);
1493
Alex Elder788e2df2013-01-17 12:25:27 -06001494 return wait_for_completion_interruptible(&obj_request->completion);
1495}
1496
Alex Elder0c425242013-02-08 09:55:49 -06001497/*
1498 * The default/initial value for all image request flags is 0. Each
1499 * is conditionally set to 1 at image request initialization time
1500 * and currently never change thereafter.
1501 */
1502static void img_request_write_set(struct rbd_img_request *img_request)
1503{
1504 set_bit(IMG_REQ_WRITE, &img_request->flags);
1505 smp_mb();
1506}
1507
1508static bool img_request_write_test(struct rbd_img_request *img_request)
1509{
1510 smp_mb();
1511 return test_bit(IMG_REQ_WRITE, &img_request->flags) != 0;
1512}
1513
Alex Elder9849e982013-01-24 16:13:36 -06001514static void img_request_child_set(struct rbd_img_request *img_request)
1515{
1516 set_bit(IMG_REQ_CHILD, &img_request->flags);
1517 smp_mb();
1518}
1519
Alex Eldere93f3152013-05-08 22:50:04 -05001520static void img_request_child_clear(struct rbd_img_request *img_request)
1521{
1522 clear_bit(IMG_REQ_CHILD, &img_request->flags);
1523 smp_mb();
1524}
1525
Alex Elder9849e982013-01-24 16:13:36 -06001526static bool img_request_child_test(struct rbd_img_request *img_request)
1527{
1528 smp_mb();
1529 return test_bit(IMG_REQ_CHILD, &img_request->flags) != 0;
1530}
1531
Alex Elderd0b2e942013-01-24 16:13:36 -06001532static void img_request_layered_set(struct rbd_img_request *img_request)
1533{
1534 set_bit(IMG_REQ_LAYERED, &img_request->flags);
1535 smp_mb();
1536}
1537
Alex Eldera2acd002013-05-08 22:50:04 -05001538static void img_request_layered_clear(struct rbd_img_request *img_request)
1539{
1540 clear_bit(IMG_REQ_LAYERED, &img_request->flags);
1541 smp_mb();
1542}
1543
Alex Elderd0b2e942013-01-24 16:13:36 -06001544static bool img_request_layered_test(struct rbd_img_request *img_request)
1545{
1546 smp_mb();
1547 return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0;
1548}
1549
Alex Elder6e2a4502013-03-27 09:16:30 -05001550static void
1551rbd_img_obj_request_read_callback(struct rbd_obj_request *obj_request)
1552{
Alex Elderb9434c52013-04-19 15:34:50 -05001553 u64 xferred = obj_request->xferred;
1554 u64 length = obj_request->length;
1555
Alex Elder6e2a4502013-03-27 09:16:30 -05001556 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1557 obj_request, obj_request->img_request, obj_request->result,
Alex Elderb9434c52013-04-19 15:34:50 -05001558 xferred, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001559 /*
Josh Durgin17c1cc12013-08-26 17:55:38 -07001560 * ENOENT means a hole in the image. We zero-fill the entire
1561 * length of the request. A short read also implies zero-fill
1562 * to the end of the request. An error requires the whole
1563 * length of the request to be reported finished with an error
1564 * to the block layer. In each case we update the xferred
1565 * count to indicate the whole request was satisfied.
Alex Elder6e2a4502013-03-27 09:16:30 -05001566 */
Alex Elderb9434c52013-04-19 15:34:50 -05001567 rbd_assert(obj_request->type != OBJ_REQUEST_NODATA);
Alex Elder6e2a4502013-03-27 09:16:30 -05001568 if (obj_request->result == -ENOENT) {
Alex Elderb9434c52013-04-19 15:34:50 -05001569 if (obj_request->type == OBJ_REQUEST_BIO)
1570 zero_bio_chain(obj_request->bio_list, 0);
1571 else
1572 zero_pages(obj_request->pages, 0, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001573 obj_request->result = 0;
Alex Elderb9434c52013-04-19 15:34:50 -05001574 } else if (xferred < length && !obj_request->result) {
1575 if (obj_request->type == OBJ_REQUEST_BIO)
1576 zero_bio_chain(obj_request->bio_list, xferred);
1577 else
1578 zero_pages(obj_request->pages, xferred, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001579 }
Josh Durgin17c1cc12013-08-26 17:55:38 -07001580 obj_request->xferred = length;
Alex Elder6e2a4502013-03-27 09:16:30 -05001581 obj_request_done_set(obj_request);
1582}
1583
Alex Elderbf0d5f502012-11-22 00:00:08 -06001584static void rbd_obj_request_complete(struct rbd_obj_request *obj_request)
1585{
Alex Elder37206ee2013-02-20 17:32:08 -06001586 dout("%s: obj %p cb %p\n", __func__, obj_request,
1587 obj_request->callback);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001588 if (obj_request->callback)
1589 obj_request->callback(obj_request);
Alex Elder788e2df2013-01-17 12:25:27 -06001590 else
1591 complete_all(&obj_request->completion);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001592}
1593
Alex Elderc47f9372013-02-26 14:23:07 -06001594static void rbd_osd_trivial_callback(struct rbd_obj_request *obj_request)
Alex Elder39bf2c52013-02-26 14:23:07 -06001595{
1596 dout("%s: obj %p\n", __func__, obj_request);
1597 obj_request_done_set(obj_request);
1598}
1599
Alex Elderc47f9372013-02-26 14:23:07 -06001600static void rbd_osd_read_callback(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001601{
Alex Elder57acbaa2013-02-11 12:33:24 -06001602 struct rbd_img_request *img_request = NULL;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001603 struct rbd_device *rbd_dev = NULL;
Alex Elder57acbaa2013-02-11 12:33:24 -06001604 bool layered = false;
1605
1606 if (obj_request_img_data_test(obj_request)) {
1607 img_request = obj_request->img_request;
1608 layered = img_request && img_request_layered_test(img_request);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001609 rbd_dev = img_request->rbd_dev;
Alex Elder57acbaa2013-02-11 12:33:24 -06001610 }
Alex Elder8b3e1a52013-01-24 16:13:36 -06001611
1612 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1613 obj_request, img_request, obj_request->result,
1614 obj_request->xferred, obj_request->length);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001615 if (layered && obj_request->result == -ENOENT &&
1616 obj_request->img_offset < rbd_dev->parent_overlap)
Alex Elder8b3e1a52013-01-24 16:13:36 -06001617 rbd_img_parent_read(obj_request);
1618 else if (img_request)
Alex Elder6e2a4502013-03-27 09:16:30 -05001619 rbd_img_obj_request_read_callback(obj_request);
1620 else
1621 obj_request_done_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001622}
1623
Alex Elderc47f9372013-02-26 14:23:07 -06001624static void rbd_osd_write_callback(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001625{
Sage Weil1b83bef2013-02-25 16:11:12 -08001626 dout("%s: obj %p result %d %llu\n", __func__, obj_request,
1627 obj_request->result, obj_request->length);
1628 /*
Alex Elder8b3e1a52013-01-24 16:13:36 -06001629 * There is no such thing as a successful short write. Set
1630 * it to our originally-requested length.
Sage Weil1b83bef2013-02-25 16:11:12 -08001631 */
1632 obj_request->xferred = obj_request->length;
Alex Elder07741302013-02-05 23:41:50 -06001633 obj_request_done_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001634}
1635
Alex Elderfbfab532013-02-08 09:55:48 -06001636/*
1637 * For a simple stat call there's nothing to do. We'll do more if
1638 * this is part of a write sequence for a layered image.
1639 */
Alex Elderc47f9372013-02-26 14:23:07 -06001640static void rbd_osd_stat_callback(struct rbd_obj_request *obj_request)
Alex Elderfbfab532013-02-08 09:55:48 -06001641{
Alex Elder37206ee2013-02-20 17:32:08 -06001642 dout("%s: obj %p\n", __func__, obj_request);
Alex Elderfbfab532013-02-08 09:55:48 -06001643 obj_request_done_set(obj_request);
1644}
1645
Alex Elderbf0d5f502012-11-22 00:00:08 -06001646static void rbd_osd_req_callback(struct ceph_osd_request *osd_req,
1647 struct ceph_msg *msg)
1648{
1649 struct rbd_obj_request *obj_request = osd_req->r_priv;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001650 u16 opcode;
1651
Alex Elder37206ee2013-02-20 17:32:08 -06001652 dout("%s: osd_req %p msg %p\n", __func__, osd_req, msg);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001653 rbd_assert(osd_req == obj_request->osd_req);
Alex Elder57acbaa2013-02-11 12:33:24 -06001654 if (obj_request_img_data_test(obj_request)) {
1655 rbd_assert(obj_request->img_request);
1656 rbd_assert(obj_request->which != BAD_WHICH);
1657 } else {
1658 rbd_assert(obj_request->which == BAD_WHICH);
1659 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06001660
Sage Weil1b83bef2013-02-25 16:11:12 -08001661 if (osd_req->r_result < 0)
1662 obj_request->result = osd_req->r_result;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001663
Ilya Dryomov7cc69d42014-02-25 16:22:27 +02001664 rbd_assert(osd_req->r_num_ops <= CEPH_OSD_MAX_OP);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001665
Alex Elderc47f9372013-02-26 14:23:07 -06001666 /*
1667 * We support a 64-bit length, but ultimately it has to be
1668 * passed to blk_end_request(), which takes an unsigned int.
1669 */
Sage Weil1b83bef2013-02-25 16:11:12 -08001670 obj_request->xferred = osd_req->r_reply_op_len[0];
Alex Elder8b3e1a52013-01-24 16:13:36 -06001671 rbd_assert(obj_request->xferred < (u64)UINT_MAX);
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001672
Alex Elder79528732013-04-03 21:32:51 -05001673 opcode = osd_req->r_ops[0].op;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001674 switch (opcode) {
1675 case CEPH_OSD_OP_READ:
Alex Elderc47f9372013-02-26 14:23:07 -06001676 rbd_osd_read_callback(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001677 break;
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001678 case CEPH_OSD_OP_SETALLOCHINT:
1679 rbd_assert(osd_req->r_ops[1].op == CEPH_OSD_OP_WRITE);
1680 /* fall through */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001681 case CEPH_OSD_OP_WRITE:
Alex Elderc47f9372013-02-26 14:23:07 -06001682 rbd_osd_write_callback(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001683 break;
Alex Elderfbfab532013-02-08 09:55:48 -06001684 case CEPH_OSD_OP_STAT:
Alex Elderc47f9372013-02-26 14:23:07 -06001685 rbd_osd_stat_callback(obj_request);
Alex Elderfbfab532013-02-08 09:55:48 -06001686 break;
Alex Elder36be9a72013-01-19 00:30:28 -06001687 case CEPH_OSD_OP_CALL:
Alex Elderb8d70032012-11-30 17:53:04 -06001688 case CEPH_OSD_OP_NOTIFY_ACK:
Alex Elder9969ebc2013-01-18 12:31:10 -06001689 case CEPH_OSD_OP_WATCH:
Alex Elderc47f9372013-02-26 14:23:07 -06001690 rbd_osd_trivial_callback(obj_request);
Alex Elder9969ebc2013-01-18 12:31:10 -06001691 break;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001692 default:
1693 rbd_warn(NULL, "%s: unsupported op %hu\n",
1694 obj_request->object_name, (unsigned short) opcode);
1695 break;
1696 }
1697
Alex Elder07741302013-02-05 23:41:50 -06001698 if (obj_request_done_test(obj_request))
Alex Elderbf0d5f502012-11-22 00:00:08 -06001699 rbd_obj_request_complete(obj_request);
1700}
1701
Alex Elder9d4df012013-04-19 15:34:50 -05001702static void rbd_osd_req_format_read(struct rbd_obj_request *obj_request)
Alex Elder430c28c2013-04-03 21:32:51 -05001703{
1704 struct rbd_img_request *img_request = obj_request->img_request;
Alex Elder8c042b02013-04-03 01:28:58 -05001705 struct ceph_osd_request *osd_req = obj_request->osd_req;
Alex Elder9d4df012013-04-19 15:34:50 -05001706 u64 snap_id;
Alex Elder430c28c2013-04-03 21:32:51 -05001707
Alex Elder8c042b02013-04-03 01:28:58 -05001708 rbd_assert(osd_req != NULL);
Alex Elder430c28c2013-04-03 21:32:51 -05001709
Alex Elder9d4df012013-04-19 15:34:50 -05001710 snap_id = img_request ? img_request->snap_id : CEPH_NOSNAP;
Alex Elder8c042b02013-04-03 01:28:58 -05001711 ceph_osdc_build_request(osd_req, obj_request->offset,
Alex Elder9d4df012013-04-19 15:34:50 -05001712 NULL, snap_id, NULL);
1713}
1714
1715static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request)
1716{
1717 struct rbd_img_request *img_request = obj_request->img_request;
1718 struct ceph_osd_request *osd_req = obj_request->osd_req;
1719 struct ceph_snap_context *snapc;
1720 struct timespec mtime = CURRENT_TIME;
1721
1722 rbd_assert(osd_req != NULL);
1723
1724 snapc = img_request ? img_request->snapc : NULL;
1725 ceph_osdc_build_request(osd_req, obj_request->offset,
1726 snapc, CEPH_NOSNAP, &mtime);
Alex Elder430c28c2013-04-03 21:32:51 -05001727}
1728
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001729/*
1730 * Create an osd request. A read request has one osd op (read).
1731 * A write request has either one (watch) or two (hint+write) osd ops.
1732 * (All rbd data writes are prefixed with an allocation hint op, but
1733 * technically osd watch is a write request, hence this distinction.)
1734 */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001735static struct ceph_osd_request *rbd_osd_req_create(
1736 struct rbd_device *rbd_dev,
1737 bool write_request,
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02001738 unsigned int num_ops,
Alex Elder430c28c2013-04-03 21:32:51 -05001739 struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001740{
Alex Elderbf0d5f502012-11-22 00:00:08 -06001741 struct ceph_snap_context *snapc = NULL;
1742 struct ceph_osd_client *osdc;
1743 struct ceph_osd_request *osd_req;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001744
Alex Elder6365d332013-02-11 12:33:24 -06001745 if (obj_request_img_data_test(obj_request)) {
1746 struct rbd_img_request *img_request = obj_request->img_request;
1747
Alex Elder0c425242013-02-08 09:55:49 -06001748 rbd_assert(write_request ==
1749 img_request_write_test(img_request));
1750 if (write_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001751 snapc = img_request->snapc;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001752 }
1753
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001754 rbd_assert(num_ops == 1 || (write_request && num_ops == 2));
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02001755
1756 /* Allocate and initialize the request, for the num_ops ops */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001757
1758 osdc = &rbd_dev->rbd_client->client->osdc;
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02001759 osd_req = ceph_osdc_alloc_request(osdc, snapc, num_ops, false,
1760 GFP_ATOMIC);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001761 if (!osd_req)
1762 return NULL; /* ENOMEM */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001763
Alex Elder430c28c2013-04-03 21:32:51 -05001764 if (write_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001765 osd_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK;
Alex Elder430c28c2013-04-03 21:32:51 -05001766 else
Alex Elderbf0d5f502012-11-22 00:00:08 -06001767 osd_req->r_flags = CEPH_OSD_FLAG_READ;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001768
1769 osd_req->r_callback = rbd_osd_req_callback;
1770 osd_req->r_priv = obj_request;
1771
Ilya Dryomov3c972c92014-01-27 17:40:20 +02001772 osd_req->r_base_oloc.pool = ceph_file_layout_pg_pool(rbd_dev->layout);
1773 ceph_oid_set_name(&osd_req->r_base_oid, obj_request->object_name);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001774
Alex Elderbf0d5f502012-11-22 00:00:08 -06001775 return osd_req;
1776}
1777
Alex Elder0eefd472013-04-19 15:34:50 -05001778/*
1779 * Create a copyup osd request based on the information in the
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001780 * object request supplied. A copyup request has three osd ops,
1781 * a copyup method call, a hint op, and a write op.
Alex Elder0eefd472013-04-19 15:34:50 -05001782 */
1783static struct ceph_osd_request *
1784rbd_osd_req_create_copyup(struct rbd_obj_request *obj_request)
1785{
1786 struct rbd_img_request *img_request;
1787 struct ceph_snap_context *snapc;
1788 struct rbd_device *rbd_dev;
1789 struct ceph_osd_client *osdc;
1790 struct ceph_osd_request *osd_req;
1791
1792 rbd_assert(obj_request_img_data_test(obj_request));
1793 img_request = obj_request->img_request;
1794 rbd_assert(img_request);
1795 rbd_assert(img_request_write_test(img_request));
1796
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001797 /* Allocate and initialize the request, for the three ops */
Alex Elder0eefd472013-04-19 15:34:50 -05001798
1799 snapc = img_request->snapc;
1800 rbd_dev = img_request->rbd_dev;
1801 osdc = &rbd_dev->rbd_client->client->osdc;
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001802 osd_req = ceph_osdc_alloc_request(osdc, snapc, 3, false, GFP_ATOMIC);
Alex Elder0eefd472013-04-19 15:34:50 -05001803 if (!osd_req)
1804 return NULL; /* ENOMEM */
1805
1806 osd_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK;
1807 osd_req->r_callback = rbd_osd_req_callback;
1808 osd_req->r_priv = obj_request;
1809
Ilya Dryomov3c972c92014-01-27 17:40:20 +02001810 osd_req->r_base_oloc.pool = ceph_file_layout_pg_pool(rbd_dev->layout);
1811 ceph_oid_set_name(&osd_req->r_base_oid, obj_request->object_name);
Alex Elder0eefd472013-04-19 15:34:50 -05001812
Alex Elder0eefd472013-04-19 15:34:50 -05001813 return osd_req;
1814}
1815
1816
Alex Elderbf0d5f502012-11-22 00:00:08 -06001817static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req)
1818{
1819 ceph_osdc_put_request(osd_req);
1820}
1821
1822/* object_name is assumed to be a non-null pointer and NUL-terminated */
1823
1824static struct rbd_obj_request *rbd_obj_request_create(const char *object_name,
1825 u64 offset, u64 length,
1826 enum obj_request_type type)
1827{
1828 struct rbd_obj_request *obj_request;
1829 size_t size;
1830 char *name;
1831
1832 rbd_assert(obj_request_type_valid(type));
1833
1834 size = strlen(object_name) + 1;
Alex Elderf907ad52013-05-01 12:43:03 -05001835 name = kmalloc(size, GFP_KERNEL);
1836 if (!name)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001837 return NULL;
1838
Alex Elder868311b2013-05-01 12:43:03 -05001839 obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_KERNEL);
Alex Elderf907ad52013-05-01 12:43:03 -05001840 if (!obj_request) {
1841 kfree(name);
1842 return NULL;
1843 }
1844
Alex Elderbf0d5f502012-11-22 00:00:08 -06001845 obj_request->object_name = memcpy(name, object_name, size);
1846 obj_request->offset = offset;
1847 obj_request->length = length;
Alex Elder926f9b32013-02-11 12:33:24 -06001848 obj_request->flags = 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001849 obj_request->which = BAD_WHICH;
1850 obj_request->type = type;
1851 INIT_LIST_HEAD(&obj_request->links);
Alex Elder788e2df2013-01-17 12:25:27 -06001852 init_completion(&obj_request->completion);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001853 kref_init(&obj_request->kref);
1854
Alex Elder37206ee2013-02-20 17:32:08 -06001855 dout("%s: \"%s\" %llu/%llu %d -> obj %p\n", __func__, object_name,
1856 offset, length, (int)type, obj_request);
1857
Alex Elderbf0d5f502012-11-22 00:00:08 -06001858 return obj_request;
1859}
1860
1861static void rbd_obj_request_destroy(struct kref *kref)
1862{
1863 struct rbd_obj_request *obj_request;
1864
1865 obj_request = container_of(kref, struct rbd_obj_request, kref);
1866
Alex Elder37206ee2013-02-20 17:32:08 -06001867 dout("%s: obj %p\n", __func__, obj_request);
1868
Alex Elderbf0d5f502012-11-22 00:00:08 -06001869 rbd_assert(obj_request->img_request == NULL);
1870 rbd_assert(obj_request->which == BAD_WHICH);
1871
1872 if (obj_request->osd_req)
1873 rbd_osd_req_destroy(obj_request->osd_req);
1874
1875 rbd_assert(obj_request_type_valid(obj_request->type));
1876 switch (obj_request->type) {
Alex Elder9969ebc2013-01-18 12:31:10 -06001877 case OBJ_REQUEST_NODATA:
1878 break; /* Nothing to do */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001879 case OBJ_REQUEST_BIO:
1880 if (obj_request->bio_list)
1881 bio_chain_put(obj_request->bio_list);
1882 break;
Alex Elder788e2df2013-01-17 12:25:27 -06001883 case OBJ_REQUEST_PAGES:
1884 if (obj_request->pages)
1885 ceph_release_page_vector(obj_request->pages,
1886 obj_request->page_count);
1887 break;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001888 }
1889
Alex Elderf907ad52013-05-01 12:43:03 -05001890 kfree(obj_request->object_name);
Alex Elder868311b2013-05-01 12:43:03 -05001891 obj_request->object_name = NULL;
1892 kmem_cache_free(rbd_obj_request_cache, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001893}
1894
Alex Elderfb65d2282013-05-08 22:50:04 -05001895/* It's OK to call this for a device with no parent */
1896
1897static void rbd_spec_put(struct rbd_spec *spec);
1898static void rbd_dev_unparent(struct rbd_device *rbd_dev)
1899{
1900 rbd_dev_remove_parent(rbd_dev);
1901 rbd_spec_put(rbd_dev->parent_spec);
1902 rbd_dev->parent_spec = NULL;
1903 rbd_dev->parent_overlap = 0;
1904}
1905
Alex Elderbf0d5f502012-11-22 00:00:08 -06001906/*
Alex Eldera2acd002013-05-08 22:50:04 -05001907 * Parent image reference counting is used to determine when an
1908 * image's parent fields can be safely torn down--after there are no
1909 * more in-flight requests to the parent image. When the last
1910 * reference is dropped, cleaning them up is safe.
1911 */
1912static void rbd_dev_parent_put(struct rbd_device *rbd_dev)
1913{
1914 int counter;
1915
1916 if (!rbd_dev->parent_spec)
1917 return;
1918
1919 counter = atomic_dec_return_safe(&rbd_dev->parent_ref);
1920 if (counter > 0)
1921 return;
1922
1923 /* Last reference; clean up parent data structures */
1924
1925 if (!counter)
1926 rbd_dev_unparent(rbd_dev);
1927 else
1928 rbd_warn(rbd_dev, "parent reference underflow\n");
1929}
1930
1931/*
1932 * If an image has a non-zero parent overlap, get a reference to its
1933 * parent.
1934 *
Alex Elder392a9da2013-05-06 17:40:33 -05001935 * We must get the reference before checking for the overlap to
1936 * coordinate properly with zeroing the parent overlap in
1937 * rbd_dev_v2_parent_info() when an image gets flattened. We
1938 * drop it again if there is no overlap.
1939 *
Alex Eldera2acd002013-05-08 22:50:04 -05001940 * Returns true if the rbd device has a parent with a non-zero
1941 * overlap and a reference for it was successfully taken, or
1942 * false otherwise.
1943 */
1944static bool rbd_dev_parent_get(struct rbd_device *rbd_dev)
1945{
1946 int counter;
1947
1948 if (!rbd_dev->parent_spec)
1949 return false;
1950
1951 counter = atomic_inc_return_safe(&rbd_dev->parent_ref);
1952 if (counter > 0 && rbd_dev->parent_overlap)
1953 return true;
1954
1955 /* Image was flattened, but parent is not yet torn down */
1956
1957 if (counter < 0)
1958 rbd_warn(rbd_dev, "parent reference overflow\n");
1959
1960 return false;
1961}
1962
Alex Elderbf0d5f502012-11-22 00:00:08 -06001963/*
1964 * Caller is responsible for filling in the list of object requests
1965 * that comprises the image request, and the Linux request pointer
1966 * (if there is one).
1967 */
Alex Eldercc344fa2013-02-19 12:25:56 -06001968static struct rbd_img_request *rbd_img_request_create(
1969 struct rbd_device *rbd_dev,
Alex Elderbf0d5f502012-11-22 00:00:08 -06001970 u64 offset, u64 length,
Alex Eldere93f3152013-05-08 22:50:04 -05001971 bool write_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001972{
1973 struct rbd_img_request *img_request;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001974
Alex Elder1c2a9df2013-05-01 12:43:03 -05001975 img_request = kmem_cache_alloc(rbd_img_request_cache, GFP_ATOMIC);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001976 if (!img_request)
1977 return NULL;
1978
1979 if (write_request) {
1980 down_read(&rbd_dev->header_rwsem);
Alex Elder812164f82013-04-30 00:44:32 -05001981 ceph_get_snap_context(rbd_dev->header.snapc);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001982 up_read(&rbd_dev->header_rwsem);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001983 }
1984
1985 img_request->rq = NULL;
1986 img_request->rbd_dev = rbd_dev;
1987 img_request->offset = offset;
1988 img_request->length = length;
Alex Elder0c425242013-02-08 09:55:49 -06001989 img_request->flags = 0;
1990 if (write_request) {
1991 img_request_write_set(img_request);
Alex Elder468521c2013-04-26 09:43:47 -05001992 img_request->snapc = rbd_dev->header.snapc;
Alex Elder0c425242013-02-08 09:55:49 -06001993 } else {
Alex Elderbf0d5f502012-11-22 00:00:08 -06001994 img_request->snap_id = rbd_dev->spec->snap_id;
Alex Elder0c425242013-02-08 09:55:49 -06001995 }
Alex Eldera2acd002013-05-08 22:50:04 -05001996 if (rbd_dev_parent_get(rbd_dev))
Alex Elderd0b2e942013-01-24 16:13:36 -06001997 img_request_layered_set(img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001998 spin_lock_init(&img_request->completion_lock);
1999 img_request->next_completion = 0;
2000 img_request->callback = NULL;
Alex Eldera5a337d2013-01-24 16:13:36 -06002001 img_request->result = 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002002 img_request->obj_request_count = 0;
2003 INIT_LIST_HEAD(&img_request->obj_requests);
2004 kref_init(&img_request->kref);
2005
Alex Elder37206ee2013-02-20 17:32:08 -06002006 dout("%s: rbd_dev %p %s %llu/%llu -> img %p\n", __func__, rbd_dev,
2007 write_request ? "write" : "read", offset, length,
2008 img_request);
2009
Alex Elderbf0d5f502012-11-22 00:00:08 -06002010 return img_request;
2011}
2012
2013static void rbd_img_request_destroy(struct kref *kref)
2014{
2015 struct rbd_img_request *img_request;
2016 struct rbd_obj_request *obj_request;
2017 struct rbd_obj_request *next_obj_request;
2018
2019 img_request = container_of(kref, struct rbd_img_request, kref);
2020
Alex Elder37206ee2013-02-20 17:32:08 -06002021 dout("%s: img %p\n", __func__, img_request);
2022
Alex Elderbf0d5f502012-11-22 00:00:08 -06002023 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
2024 rbd_img_obj_request_del(img_request, obj_request);
Alex Elder25dcf952013-01-25 17:08:55 -06002025 rbd_assert(img_request->obj_request_count == 0);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002026
Alex Eldera2acd002013-05-08 22:50:04 -05002027 if (img_request_layered_test(img_request)) {
2028 img_request_layered_clear(img_request);
2029 rbd_dev_parent_put(img_request->rbd_dev);
2030 }
2031
Alex Elder0c425242013-02-08 09:55:49 -06002032 if (img_request_write_test(img_request))
Alex Elder812164f82013-04-30 00:44:32 -05002033 ceph_put_snap_context(img_request->snapc);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002034
Alex Elder1c2a9df2013-05-01 12:43:03 -05002035 kmem_cache_free(rbd_img_request_cache, img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002036}
2037
Alex Eldere93f3152013-05-08 22:50:04 -05002038static struct rbd_img_request *rbd_parent_request_create(
2039 struct rbd_obj_request *obj_request,
2040 u64 img_offset, u64 length)
2041{
2042 struct rbd_img_request *parent_request;
2043 struct rbd_device *rbd_dev;
2044
2045 rbd_assert(obj_request->img_request);
2046 rbd_dev = obj_request->img_request->rbd_dev;
2047
2048 parent_request = rbd_img_request_create(rbd_dev->parent,
2049 img_offset, length, false);
2050 if (!parent_request)
2051 return NULL;
2052
2053 img_request_child_set(parent_request);
2054 rbd_obj_request_get(obj_request);
2055 parent_request->obj_request = obj_request;
2056
2057 return parent_request;
2058}
2059
2060static void rbd_parent_request_destroy(struct kref *kref)
2061{
2062 struct rbd_img_request *parent_request;
2063 struct rbd_obj_request *orig_request;
2064
2065 parent_request = container_of(kref, struct rbd_img_request, kref);
2066 orig_request = parent_request->obj_request;
2067
2068 parent_request->obj_request = NULL;
2069 rbd_obj_request_put(orig_request);
2070 img_request_child_clear(parent_request);
2071
2072 rbd_img_request_destroy(kref);
2073}
2074
Alex Elder12178572013-02-08 09:55:49 -06002075static bool rbd_img_obj_end_request(struct rbd_obj_request *obj_request)
2076{
Alex Elder6365d332013-02-11 12:33:24 -06002077 struct rbd_img_request *img_request;
Alex Elder12178572013-02-08 09:55:49 -06002078 unsigned int xferred;
2079 int result;
Alex Elder8b3e1a52013-01-24 16:13:36 -06002080 bool more;
Alex Elder12178572013-02-08 09:55:49 -06002081
Alex Elder6365d332013-02-11 12:33:24 -06002082 rbd_assert(obj_request_img_data_test(obj_request));
2083 img_request = obj_request->img_request;
2084
Alex Elder12178572013-02-08 09:55:49 -06002085 rbd_assert(obj_request->xferred <= (u64)UINT_MAX);
2086 xferred = (unsigned int)obj_request->xferred;
2087 result = obj_request->result;
2088 if (result) {
2089 struct rbd_device *rbd_dev = img_request->rbd_dev;
2090
2091 rbd_warn(rbd_dev, "%s %llx at %llx (%llx)\n",
2092 img_request_write_test(img_request) ? "write" : "read",
2093 obj_request->length, obj_request->img_offset,
2094 obj_request->offset);
2095 rbd_warn(rbd_dev, " result %d xferred %x\n",
2096 result, xferred);
2097 if (!img_request->result)
2098 img_request->result = result;
2099 }
2100
Alex Elderf1a47392013-04-19 15:34:50 -05002101 /* Image object requests don't own their page array */
2102
2103 if (obj_request->type == OBJ_REQUEST_PAGES) {
2104 obj_request->pages = NULL;
2105 obj_request->page_count = 0;
2106 }
2107
Alex Elder8b3e1a52013-01-24 16:13:36 -06002108 if (img_request_child_test(img_request)) {
2109 rbd_assert(img_request->obj_request != NULL);
2110 more = obj_request->which < img_request->obj_request_count - 1;
2111 } else {
2112 rbd_assert(img_request->rq != NULL);
2113 more = blk_end_request(img_request->rq, result, xferred);
2114 }
2115
2116 return more;
Alex Elder12178572013-02-08 09:55:49 -06002117}
2118
Alex Elder21692382013-04-05 01:27:12 -05002119static void rbd_img_obj_callback(struct rbd_obj_request *obj_request)
2120{
2121 struct rbd_img_request *img_request;
2122 u32 which = obj_request->which;
2123 bool more = true;
2124
Alex Elder6365d332013-02-11 12:33:24 -06002125 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elder21692382013-04-05 01:27:12 -05002126 img_request = obj_request->img_request;
2127
2128 dout("%s: img %p obj %p\n", __func__, img_request, obj_request);
2129 rbd_assert(img_request != NULL);
Alex Elder21692382013-04-05 01:27:12 -05002130 rbd_assert(img_request->obj_request_count > 0);
2131 rbd_assert(which != BAD_WHICH);
2132 rbd_assert(which < img_request->obj_request_count);
Alex Elder21692382013-04-05 01:27:12 -05002133
2134 spin_lock_irq(&img_request->completion_lock);
2135 if (which != img_request->next_completion)
2136 goto out;
2137
2138 for_each_obj_request_from(img_request, obj_request) {
Alex Elder21692382013-04-05 01:27:12 -05002139 rbd_assert(more);
2140 rbd_assert(which < img_request->obj_request_count);
2141
2142 if (!obj_request_done_test(obj_request))
2143 break;
Alex Elder12178572013-02-08 09:55:49 -06002144 more = rbd_img_obj_end_request(obj_request);
Alex Elder21692382013-04-05 01:27:12 -05002145 which++;
2146 }
2147
2148 rbd_assert(more ^ (which == img_request->obj_request_count));
2149 img_request->next_completion = which;
2150out:
2151 spin_unlock_irq(&img_request->completion_lock);
Alex Elder0f2d5be2014-04-26 14:21:44 +04002152 rbd_img_request_put(img_request);
Alex Elder21692382013-04-05 01:27:12 -05002153
2154 if (!more)
2155 rbd_img_request_complete(img_request);
2156}
2157
Alex Elderf1a47392013-04-19 15:34:50 -05002158/*
2159 * Split up an image request into one or more object requests, each
2160 * to a different object. The "type" parameter indicates whether
2161 * "data_desc" is the pointer to the head of a list of bio
2162 * structures, or the base of a page array. In either case this
2163 * function assumes data_desc describes memory sufficient to hold
2164 * all data described by the image request.
2165 */
2166static int rbd_img_request_fill(struct rbd_img_request *img_request,
2167 enum obj_request_type type,
2168 void *data_desc)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002169{
2170 struct rbd_device *rbd_dev = img_request->rbd_dev;
2171 struct rbd_obj_request *obj_request = NULL;
2172 struct rbd_obj_request *next_obj_request;
Alex Elder0c425242013-02-08 09:55:49 -06002173 bool write_request = img_request_write_test(img_request);
Jingoo Hana1580732013-08-09 13:04:35 +09002174 struct bio *bio_list = NULL;
Alex Elderf1a47392013-04-19 15:34:50 -05002175 unsigned int bio_offset = 0;
Jingoo Hana1580732013-08-09 13:04:35 +09002176 struct page **pages = NULL;
Alex Elder7da22d22013-01-24 16:13:36 -06002177 u64 img_offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002178 u64 resid;
2179 u16 opcode;
2180
Alex Elderf1a47392013-04-19 15:34:50 -05002181 dout("%s: img %p type %d data_desc %p\n", __func__, img_request,
2182 (int)type, data_desc);
Alex Elder37206ee2013-02-20 17:32:08 -06002183
Alex Elder430c28c2013-04-03 21:32:51 -05002184 opcode = write_request ? CEPH_OSD_OP_WRITE : CEPH_OSD_OP_READ;
Alex Elder7da22d22013-01-24 16:13:36 -06002185 img_offset = img_request->offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002186 resid = img_request->length;
Alex Elder4dda41d2013-02-20 21:59:33 -06002187 rbd_assert(resid > 0);
Alex Elderf1a47392013-04-19 15:34:50 -05002188
2189 if (type == OBJ_REQUEST_BIO) {
2190 bio_list = data_desc;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002191 rbd_assert(img_offset ==
2192 bio_list->bi_iter.bi_sector << SECTOR_SHIFT);
Alex Elderf1a47392013-04-19 15:34:50 -05002193 } else {
2194 rbd_assert(type == OBJ_REQUEST_PAGES);
2195 pages = data_desc;
2196 }
2197
Alex Elderbf0d5f502012-11-22 00:00:08 -06002198 while (resid) {
Alex Elder2fa12322013-04-05 01:27:12 -05002199 struct ceph_osd_request *osd_req;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002200 const char *object_name;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002201 u64 offset;
2202 u64 length;
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02002203 unsigned int which = 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002204
Alex Elder7da22d22013-01-24 16:13:36 -06002205 object_name = rbd_segment_name(rbd_dev, img_offset);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002206 if (!object_name)
2207 goto out_unwind;
Alex Elder7da22d22013-01-24 16:13:36 -06002208 offset = rbd_segment_offset(rbd_dev, img_offset);
2209 length = rbd_segment_length(rbd_dev, img_offset, resid);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002210 obj_request = rbd_obj_request_create(object_name,
Alex Elderf1a47392013-04-19 15:34:50 -05002211 offset, length, type);
Alex Elder78c2a442013-05-01 12:43:04 -05002212 /* object request has its own copy of the object name */
2213 rbd_segment_name_free(object_name);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002214 if (!obj_request)
2215 goto out_unwind;
Ilya Dryomov62054da2014-03-04 11:57:17 +02002216
Josh Durgin03507db2013-08-27 14:45:46 -07002217 /*
2218 * set obj_request->img_request before creating the
2219 * osd_request so that it gets the right snapc
2220 */
2221 rbd_img_obj_request_add(img_request, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002222
Alex Elderf1a47392013-04-19 15:34:50 -05002223 if (type == OBJ_REQUEST_BIO) {
2224 unsigned int clone_size;
2225
2226 rbd_assert(length <= (u64)UINT_MAX);
2227 clone_size = (unsigned int)length;
2228 obj_request->bio_list =
2229 bio_chain_clone_range(&bio_list,
2230 &bio_offset,
2231 clone_size,
2232 GFP_ATOMIC);
2233 if (!obj_request->bio_list)
Ilya Dryomov62054da2014-03-04 11:57:17 +02002234 goto out_unwind;
Alex Elderf1a47392013-04-19 15:34:50 -05002235 } else {
2236 unsigned int page_count;
2237
2238 obj_request->pages = pages;
2239 page_count = (u32)calc_pages_for(offset, length);
2240 obj_request->page_count = page_count;
2241 if ((offset + length) & ~PAGE_MASK)
2242 page_count--; /* more on last page */
2243 pages += page_count;
2244 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06002245
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02002246 osd_req = rbd_osd_req_create(rbd_dev, write_request,
2247 (write_request ? 2 : 1),
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02002248 obj_request);
Alex Elder2fa12322013-04-05 01:27:12 -05002249 if (!osd_req)
Ilya Dryomov62054da2014-03-04 11:57:17 +02002250 goto out_unwind;
Alex Elder2fa12322013-04-05 01:27:12 -05002251 obj_request->osd_req = osd_req;
Alex Elder21692382013-04-05 01:27:12 -05002252 obj_request->callback = rbd_img_obj_callback;
Alex Elder0f2d5be2014-04-26 14:21:44 +04002253 rbd_img_request_get(img_request);
Alex Elder430c28c2013-04-03 21:32:51 -05002254
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02002255 if (write_request) {
2256 osd_req_op_alloc_hint_init(osd_req, which,
2257 rbd_obj_bytes(&rbd_dev->header),
2258 rbd_obj_bytes(&rbd_dev->header));
2259 which++;
2260 }
2261
2262 osd_req_op_extent_init(osd_req, which, opcode, offset, length,
2263 0, 0);
Alex Elderf1a47392013-04-19 15:34:50 -05002264 if (type == OBJ_REQUEST_BIO)
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02002265 osd_req_op_extent_osd_data_bio(osd_req, which,
Alex Elderf1a47392013-04-19 15:34:50 -05002266 obj_request->bio_list, length);
2267 else
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02002268 osd_req_op_extent_osd_data_pages(osd_req, which,
Alex Elderf1a47392013-04-19 15:34:50 -05002269 obj_request->pages, length,
2270 offset & ~PAGE_MASK, false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05002271
2272 if (write_request)
2273 rbd_osd_req_format_write(obj_request);
2274 else
2275 rbd_osd_req_format_read(obj_request);
Alex Elder430c28c2013-04-03 21:32:51 -05002276
Alex Elder7da22d22013-01-24 16:13:36 -06002277 obj_request->img_offset = img_offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002278
Alex Elder7da22d22013-01-24 16:13:36 -06002279 img_offset += length;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002280 resid -= length;
2281 }
2282
2283 return 0;
2284
Alex Elderbf0d5f502012-11-22 00:00:08 -06002285out_unwind:
2286 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
Ilya Dryomov42dd0372014-03-04 11:57:17 +02002287 rbd_img_obj_request_del(img_request, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002288
2289 return -ENOMEM;
2290}
2291
Alex Elder3d7efd12013-04-19 15:34:50 -05002292static void
Alex Elder0eefd472013-04-19 15:34:50 -05002293rbd_img_obj_copyup_callback(struct rbd_obj_request *obj_request)
2294{
2295 struct rbd_img_request *img_request;
2296 struct rbd_device *rbd_dev;
Alex Elderebda6402013-05-10 16:29:22 -05002297 struct page **pages;
Alex Elder0eefd472013-04-19 15:34:50 -05002298 u32 page_count;
2299
2300 rbd_assert(obj_request->type == OBJ_REQUEST_BIO);
2301 rbd_assert(obj_request_img_data_test(obj_request));
2302 img_request = obj_request->img_request;
2303 rbd_assert(img_request);
2304
2305 rbd_dev = img_request->rbd_dev;
2306 rbd_assert(rbd_dev);
Alex Elder0eefd472013-04-19 15:34:50 -05002307
Alex Elderebda6402013-05-10 16:29:22 -05002308 pages = obj_request->copyup_pages;
2309 rbd_assert(pages != NULL);
Alex Elder0eefd472013-04-19 15:34:50 -05002310 obj_request->copyup_pages = NULL;
Alex Elderebda6402013-05-10 16:29:22 -05002311 page_count = obj_request->copyup_page_count;
2312 rbd_assert(page_count);
2313 obj_request->copyup_page_count = 0;
2314 ceph_release_page_vector(pages, page_count);
Alex Elder0eefd472013-04-19 15:34:50 -05002315
2316 /*
2317 * We want the transfer count to reflect the size of the
2318 * original write request. There is no such thing as a
2319 * successful short write, so if the request was successful
2320 * we can just set it to the originally-requested length.
2321 */
2322 if (!obj_request->result)
2323 obj_request->xferred = obj_request->length;
2324
2325 /* Finish up with the normal image object callback */
2326
2327 rbd_img_obj_callback(obj_request);
2328}
2329
2330static void
Alex Elder3d7efd12013-04-19 15:34:50 -05002331rbd_img_obj_parent_read_full_callback(struct rbd_img_request *img_request)
2332{
2333 struct rbd_obj_request *orig_request;
Alex Elder0eefd472013-04-19 15:34:50 -05002334 struct ceph_osd_request *osd_req;
2335 struct ceph_osd_client *osdc;
2336 struct rbd_device *rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002337 struct page **pages;
Alex Elderebda6402013-05-10 16:29:22 -05002338 u32 page_count;
Alex Elderbbea1c12013-05-06 17:40:33 -05002339 int img_result;
Alex Elderebda6402013-05-10 16:29:22 -05002340 u64 parent_length;
Alex Elderb91f09f2013-05-10 16:29:22 -05002341 u64 offset;
2342 u64 length;
Alex Elder3d7efd12013-04-19 15:34:50 -05002343
2344 rbd_assert(img_request_child_test(img_request));
2345
2346 /* First get what we need from the image request */
2347
2348 pages = img_request->copyup_pages;
2349 rbd_assert(pages != NULL);
2350 img_request->copyup_pages = NULL;
Alex Elderebda6402013-05-10 16:29:22 -05002351 page_count = img_request->copyup_page_count;
2352 rbd_assert(page_count);
2353 img_request->copyup_page_count = 0;
Alex Elder3d7efd12013-04-19 15:34:50 -05002354
2355 orig_request = img_request->obj_request;
2356 rbd_assert(orig_request != NULL);
Alex Elderb91f09f2013-05-10 16:29:22 -05002357 rbd_assert(obj_request_type_valid(orig_request->type));
Alex Elderbbea1c12013-05-06 17:40:33 -05002358 img_result = img_request->result;
Alex Elderebda6402013-05-10 16:29:22 -05002359 parent_length = img_request->length;
2360 rbd_assert(parent_length == img_request->xferred);
Alex Elder3d7efd12013-04-19 15:34:50 -05002361 rbd_img_request_put(img_request);
2362
Alex Elder91c6feb2013-05-06 17:40:32 -05002363 rbd_assert(orig_request->img_request);
2364 rbd_dev = orig_request->img_request->rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002365 rbd_assert(rbd_dev);
Alex Elder3d7efd12013-04-19 15:34:50 -05002366
Alex Elderbbea1c12013-05-06 17:40:33 -05002367 /*
2368 * If the overlap has become 0 (most likely because the
2369 * image has been flattened) we need to free the pages
2370 * and re-submit the original write request.
2371 */
2372 if (!rbd_dev->parent_overlap) {
2373 struct ceph_osd_client *osdc;
2374
2375 ceph_release_page_vector(pages, page_count);
2376 osdc = &rbd_dev->rbd_client->client->osdc;
2377 img_result = rbd_obj_request_submit(osdc, orig_request);
2378 if (!img_result)
2379 return;
2380 }
2381
2382 if (img_result)
Alex Elder0eefd472013-04-19 15:34:50 -05002383 goto out_err;
Alex Elder3d7efd12013-04-19 15:34:50 -05002384
Alex Elder8785b1d2013-05-09 10:08:49 -05002385 /*
2386 * The original osd request is of no use to use any more.
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02002387 * We need a new one that can hold the three ops in a copyup
Alex Elder8785b1d2013-05-09 10:08:49 -05002388 * request. Allocate the new copyup osd request for the
2389 * original request, and release the old one.
2390 */
Alex Elderbbea1c12013-05-06 17:40:33 -05002391 img_result = -ENOMEM;
Alex Elder0eefd472013-04-19 15:34:50 -05002392 osd_req = rbd_osd_req_create_copyup(orig_request);
2393 if (!osd_req)
2394 goto out_err;
Alex Elder8785b1d2013-05-09 10:08:49 -05002395 rbd_osd_req_destroy(orig_request->osd_req);
Alex Elder0eefd472013-04-19 15:34:50 -05002396 orig_request->osd_req = osd_req;
2397 orig_request->copyup_pages = pages;
Alex Elderebda6402013-05-10 16:29:22 -05002398 orig_request->copyup_page_count = page_count;
Alex Elder3d7efd12013-04-19 15:34:50 -05002399
Alex Elder0eefd472013-04-19 15:34:50 -05002400 /* Initialize the copyup op */
2401
2402 osd_req_op_cls_init(osd_req, 0, CEPH_OSD_OP_CALL, "rbd", "copyup");
Alex Elderebda6402013-05-10 16:29:22 -05002403 osd_req_op_cls_request_data_pages(osd_req, 0, pages, parent_length, 0,
Alex Elder0eefd472013-04-19 15:34:50 -05002404 false, false);
2405
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02002406 /* Then the hint op */
2407
2408 osd_req_op_alloc_hint_init(osd_req, 1, rbd_obj_bytes(&rbd_dev->header),
2409 rbd_obj_bytes(&rbd_dev->header));
2410
2411 /* And the original write request op */
Alex Elder0eefd472013-04-19 15:34:50 -05002412
Alex Elderb91f09f2013-05-10 16:29:22 -05002413 offset = orig_request->offset;
2414 length = orig_request->length;
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02002415 osd_req_op_extent_init(osd_req, 2, CEPH_OSD_OP_WRITE,
Alex Elderb91f09f2013-05-10 16:29:22 -05002416 offset, length, 0, 0);
2417 if (orig_request->type == OBJ_REQUEST_BIO)
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02002418 osd_req_op_extent_osd_data_bio(osd_req, 2,
Alex Elderb91f09f2013-05-10 16:29:22 -05002419 orig_request->bio_list, length);
2420 else
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02002421 osd_req_op_extent_osd_data_pages(osd_req, 2,
Alex Elderb91f09f2013-05-10 16:29:22 -05002422 orig_request->pages, length,
2423 offset & ~PAGE_MASK, false, false);
Alex Elder0eefd472013-04-19 15:34:50 -05002424
2425 rbd_osd_req_format_write(orig_request);
2426
2427 /* All set, send it off. */
2428
2429 orig_request->callback = rbd_img_obj_copyup_callback;
2430 osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elderbbea1c12013-05-06 17:40:33 -05002431 img_result = rbd_obj_request_submit(osdc, orig_request);
2432 if (!img_result)
Alex Elder0eefd472013-04-19 15:34:50 -05002433 return;
2434out_err:
2435 /* Record the error code and complete the request */
2436
Alex Elderbbea1c12013-05-06 17:40:33 -05002437 orig_request->result = img_result;
Alex Elder0eefd472013-04-19 15:34:50 -05002438 orig_request->xferred = 0;
2439 obj_request_done_set(orig_request);
2440 rbd_obj_request_complete(orig_request);
Alex Elder3d7efd12013-04-19 15:34:50 -05002441}
2442
2443/*
2444 * Read from the parent image the range of data that covers the
2445 * entire target of the given object request. This is used for
2446 * satisfying a layered image write request when the target of an
2447 * object request from the image request does not exist.
2448 *
2449 * A page array big enough to hold the returned data is allocated
2450 * and supplied to rbd_img_request_fill() as the "data descriptor."
2451 * When the read completes, this page array will be transferred to
2452 * the original object request for the copyup operation.
2453 *
2454 * If an error occurs, record it as the result of the original
2455 * object request and mark it done so it gets completed.
2456 */
2457static int rbd_img_obj_parent_read_full(struct rbd_obj_request *obj_request)
2458{
2459 struct rbd_img_request *img_request = NULL;
2460 struct rbd_img_request *parent_request = NULL;
2461 struct rbd_device *rbd_dev;
2462 u64 img_offset;
2463 u64 length;
2464 struct page **pages = NULL;
2465 u32 page_count;
2466 int result;
2467
2468 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elderb91f09f2013-05-10 16:29:22 -05002469 rbd_assert(obj_request_type_valid(obj_request->type));
Alex Elder3d7efd12013-04-19 15:34:50 -05002470
2471 img_request = obj_request->img_request;
2472 rbd_assert(img_request != NULL);
2473 rbd_dev = img_request->rbd_dev;
2474 rbd_assert(rbd_dev->parent != NULL);
2475
2476 /*
2477 * Determine the byte range covered by the object in the
2478 * child image to which the original request was to be sent.
2479 */
2480 img_offset = obj_request->img_offset - obj_request->offset;
2481 length = (u64)1 << rbd_dev->header.obj_order;
2482
2483 /*
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002484 * There is no defined parent data beyond the parent
2485 * overlap, so limit what we read at that boundary if
2486 * necessary.
2487 */
2488 if (img_offset + length > rbd_dev->parent_overlap) {
2489 rbd_assert(img_offset < rbd_dev->parent_overlap);
2490 length = rbd_dev->parent_overlap - img_offset;
2491 }
2492
2493 /*
Alex Elder3d7efd12013-04-19 15:34:50 -05002494 * Allocate a page array big enough to receive the data read
2495 * from the parent.
2496 */
2497 page_count = (u32)calc_pages_for(0, length);
2498 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2499 if (IS_ERR(pages)) {
2500 result = PTR_ERR(pages);
2501 pages = NULL;
2502 goto out_err;
2503 }
2504
2505 result = -ENOMEM;
Alex Eldere93f3152013-05-08 22:50:04 -05002506 parent_request = rbd_parent_request_create(obj_request,
2507 img_offset, length);
Alex Elder3d7efd12013-04-19 15:34:50 -05002508 if (!parent_request)
2509 goto out_err;
Alex Elder3d7efd12013-04-19 15:34:50 -05002510
2511 result = rbd_img_request_fill(parent_request, OBJ_REQUEST_PAGES, pages);
2512 if (result)
2513 goto out_err;
2514 parent_request->copyup_pages = pages;
Alex Elderebda6402013-05-10 16:29:22 -05002515 parent_request->copyup_page_count = page_count;
Alex Elder3d7efd12013-04-19 15:34:50 -05002516
2517 parent_request->callback = rbd_img_obj_parent_read_full_callback;
2518 result = rbd_img_request_submit(parent_request);
2519 if (!result)
2520 return 0;
2521
2522 parent_request->copyup_pages = NULL;
Alex Elderebda6402013-05-10 16:29:22 -05002523 parent_request->copyup_page_count = 0;
Alex Elder3d7efd12013-04-19 15:34:50 -05002524 parent_request->obj_request = NULL;
2525 rbd_obj_request_put(obj_request);
2526out_err:
2527 if (pages)
2528 ceph_release_page_vector(pages, page_count);
2529 if (parent_request)
2530 rbd_img_request_put(parent_request);
2531 obj_request->result = result;
2532 obj_request->xferred = 0;
2533 obj_request_done_set(obj_request);
2534
2535 return result;
2536}
2537
Alex Elderc5b5ef62013-02-11 12:33:24 -06002538static void rbd_img_obj_exists_callback(struct rbd_obj_request *obj_request)
2539{
Alex Elderc5b5ef62013-02-11 12:33:24 -06002540 struct rbd_obj_request *orig_request;
Alex Elder638f5ab2013-05-06 17:40:33 -05002541 struct rbd_device *rbd_dev;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002542 int result;
2543
2544 rbd_assert(!obj_request_img_data_test(obj_request));
2545
2546 /*
2547 * All we need from the object request is the original
2548 * request and the result of the STAT op. Grab those, then
2549 * we're done with the request.
2550 */
2551 orig_request = obj_request->obj_request;
2552 obj_request->obj_request = NULL;
Alex Elder912c3172013-05-13 20:35:38 -05002553 rbd_obj_request_put(orig_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002554 rbd_assert(orig_request);
2555 rbd_assert(orig_request->img_request);
2556
2557 result = obj_request->result;
2558 obj_request->result = 0;
2559
2560 dout("%s: obj %p for obj %p result %d %llu/%llu\n", __func__,
2561 obj_request, orig_request, result,
2562 obj_request->xferred, obj_request->length);
2563 rbd_obj_request_put(obj_request);
2564
Alex Elder638f5ab2013-05-06 17:40:33 -05002565 /*
2566 * If the overlap has become 0 (most likely because the
2567 * image has been flattened) we need to free the pages
2568 * and re-submit the original write request.
2569 */
2570 rbd_dev = orig_request->img_request->rbd_dev;
2571 if (!rbd_dev->parent_overlap) {
2572 struct ceph_osd_client *osdc;
2573
Alex Elder638f5ab2013-05-06 17:40:33 -05002574 osdc = &rbd_dev->rbd_client->client->osdc;
2575 result = rbd_obj_request_submit(osdc, orig_request);
2576 if (!result)
2577 return;
2578 }
Alex Elderc5b5ef62013-02-11 12:33:24 -06002579
2580 /*
2581 * Our only purpose here is to determine whether the object
2582 * exists, and we don't want to treat the non-existence as
2583 * an error. If something else comes back, transfer the
2584 * error to the original request and complete it now.
2585 */
2586 if (!result) {
2587 obj_request_existence_set(orig_request, true);
2588 } else if (result == -ENOENT) {
2589 obj_request_existence_set(orig_request, false);
2590 } else if (result) {
2591 orig_request->result = result;
Alex Elder3d7efd12013-04-19 15:34:50 -05002592 goto out;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002593 }
2594
2595 /*
2596 * Resubmit the original request now that we have recorded
2597 * whether the target object exists.
2598 */
Alex Elderb454e362013-04-19 15:34:50 -05002599 orig_request->result = rbd_img_obj_request_submit(orig_request);
Alex Elder3d7efd12013-04-19 15:34:50 -05002600out:
Alex Elderc5b5ef62013-02-11 12:33:24 -06002601 if (orig_request->result)
2602 rbd_obj_request_complete(orig_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002603}
2604
2605static int rbd_img_obj_exists_submit(struct rbd_obj_request *obj_request)
2606{
2607 struct rbd_obj_request *stat_request;
2608 struct rbd_device *rbd_dev;
2609 struct ceph_osd_client *osdc;
2610 struct page **pages = NULL;
2611 u32 page_count;
2612 size_t size;
2613 int ret;
2614
2615 /*
2616 * The response data for a STAT call consists of:
2617 * le64 length;
2618 * struct {
2619 * le32 tv_sec;
2620 * le32 tv_nsec;
2621 * } mtime;
2622 */
2623 size = sizeof (__le64) + sizeof (__le32) + sizeof (__le32);
2624 page_count = (u32)calc_pages_for(0, size);
2625 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2626 if (IS_ERR(pages))
2627 return PTR_ERR(pages);
2628
2629 ret = -ENOMEM;
2630 stat_request = rbd_obj_request_create(obj_request->object_name, 0, 0,
2631 OBJ_REQUEST_PAGES);
2632 if (!stat_request)
2633 goto out;
2634
2635 rbd_obj_request_get(obj_request);
2636 stat_request->obj_request = obj_request;
2637 stat_request->pages = pages;
2638 stat_request->page_count = page_count;
2639
2640 rbd_assert(obj_request->img_request);
2641 rbd_dev = obj_request->img_request->rbd_dev;
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02002642 stat_request->osd_req = rbd_osd_req_create(rbd_dev, false, 1,
2643 stat_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002644 if (!stat_request->osd_req)
2645 goto out;
2646 stat_request->callback = rbd_img_obj_exists_callback;
2647
2648 osd_req_op_init(stat_request->osd_req, 0, CEPH_OSD_OP_STAT);
2649 osd_req_op_raw_data_in_pages(stat_request->osd_req, 0, pages, size, 0,
2650 false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05002651 rbd_osd_req_format_read(stat_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002652
2653 osdc = &rbd_dev->rbd_client->client->osdc;
2654 ret = rbd_obj_request_submit(osdc, stat_request);
2655out:
2656 if (ret)
2657 rbd_obj_request_put(obj_request);
2658
2659 return ret;
2660}
2661
Alex Elderb454e362013-04-19 15:34:50 -05002662static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request)
2663{
2664 struct rbd_img_request *img_request;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002665 struct rbd_device *rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002666 bool known;
Alex Elderb454e362013-04-19 15:34:50 -05002667
2668 rbd_assert(obj_request_img_data_test(obj_request));
2669
2670 img_request = obj_request->img_request;
2671 rbd_assert(img_request);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002672 rbd_dev = img_request->rbd_dev;
Alex Elderb454e362013-04-19 15:34:50 -05002673
Alex Elderb454e362013-04-19 15:34:50 -05002674 /*
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002675 * Only writes to layered images need special handling.
2676 * Reads and non-layered writes are simple object requests.
2677 * Layered writes that start beyond the end of the overlap
2678 * with the parent have no parent data, so they too are
2679 * simple object requests. Finally, if the target object is
2680 * known to already exist, its parent data has already been
2681 * copied, so a write to the object can also be handled as a
2682 * simple object request.
Alex Elderb454e362013-04-19 15:34:50 -05002683 */
2684 if (!img_request_write_test(img_request) ||
2685 !img_request_layered_test(img_request) ||
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002686 rbd_dev->parent_overlap <= obj_request->img_offset ||
Alex Elder3d7efd12013-04-19 15:34:50 -05002687 ((known = obj_request_known_test(obj_request)) &&
2688 obj_request_exists_test(obj_request))) {
Alex Elderb454e362013-04-19 15:34:50 -05002689
2690 struct rbd_device *rbd_dev;
2691 struct ceph_osd_client *osdc;
2692
2693 rbd_dev = obj_request->img_request->rbd_dev;
2694 osdc = &rbd_dev->rbd_client->client->osdc;
2695
2696 return rbd_obj_request_submit(osdc, obj_request);
2697 }
2698
2699 /*
Alex Elder3d7efd12013-04-19 15:34:50 -05002700 * It's a layered write. The target object might exist but
2701 * we may not know that yet. If we know it doesn't exist,
2702 * start by reading the data for the full target object from
2703 * the parent so we can use it for a copyup to the target.
Alex Elderb454e362013-04-19 15:34:50 -05002704 */
Alex Elder3d7efd12013-04-19 15:34:50 -05002705 if (known)
2706 return rbd_img_obj_parent_read_full(obj_request);
2707
2708 /* We don't know whether the target exists. Go find out. */
Alex Elderb454e362013-04-19 15:34:50 -05002709
2710 return rbd_img_obj_exists_submit(obj_request);
2711}
2712
Alex Elderbf0d5f502012-11-22 00:00:08 -06002713static int rbd_img_request_submit(struct rbd_img_request *img_request)
2714{
Alex Elderbf0d5f502012-11-22 00:00:08 -06002715 struct rbd_obj_request *obj_request;
Alex Elder46faeed2013-04-10 17:47:46 -05002716 struct rbd_obj_request *next_obj_request;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002717
Alex Elder37206ee2013-02-20 17:32:08 -06002718 dout("%s: img %p\n", __func__, img_request);
Alex Elder46faeed2013-04-10 17:47:46 -05002719 for_each_obj_request_safe(img_request, obj_request, next_obj_request) {
Alex Elderbf0d5f502012-11-22 00:00:08 -06002720 int ret;
2721
Alex Elderb454e362013-04-19 15:34:50 -05002722 ret = rbd_img_obj_request_submit(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002723 if (ret)
2724 return ret;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002725 }
2726
2727 return 0;
2728}
2729
Alex Elder8b3e1a52013-01-24 16:13:36 -06002730static void rbd_img_parent_read_callback(struct rbd_img_request *img_request)
2731{
2732 struct rbd_obj_request *obj_request;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002733 struct rbd_device *rbd_dev;
2734 u64 obj_end;
Alex Elder02c74fb2013-05-06 17:40:33 -05002735 u64 img_xferred;
2736 int img_result;
Alex Elder8b3e1a52013-01-24 16:13:36 -06002737
2738 rbd_assert(img_request_child_test(img_request));
2739
Alex Elder02c74fb2013-05-06 17:40:33 -05002740 /* First get what we need from the image request and release it */
2741
Alex Elder8b3e1a52013-01-24 16:13:36 -06002742 obj_request = img_request->obj_request;
Alex Elder02c74fb2013-05-06 17:40:33 -05002743 img_xferred = img_request->xferred;
2744 img_result = img_request->result;
2745 rbd_img_request_put(img_request);
2746
2747 /*
2748 * If the overlap has become 0 (most likely because the
2749 * image has been flattened) we need to re-submit the
2750 * original request.
2751 */
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002752 rbd_assert(obj_request);
2753 rbd_assert(obj_request->img_request);
Alex Elder02c74fb2013-05-06 17:40:33 -05002754 rbd_dev = obj_request->img_request->rbd_dev;
2755 if (!rbd_dev->parent_overlap) {
2756 struct ceph_osd_client *osdc;
Alex Elder8b3e1a52013-01-24 16:13:36 -06002757
Alex Elder02c74fb2013-05-06 17:40:33 -05002758 osdc = &rbd_dev->rbd_client->client->osdc;
2759 img_result = rbd_obj_request_submit(osdc, obj_request);
2760 if (!img_result)
2761 return;
2762 }
2763
2764 obj_request->result = img_result;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002765 if (obj_request->result)
2766 goto out;
2767
2768 /*
2769 * We need to zero anything beyond the parent overlap
2770 * boundary. Since rbd_img_obj_request_read_callback()
2771 * will zero anything beyond the end of a short read, an
2772 * easy way to do this is to pretend the data from the
2773 * parent came up short--ending at the overlap boundary.
2774 */
2775 rbd_assert(obj_request->img_offset < U64_MAX - obj_request->length);
2776 obj_end = obj_request->img_offset + obj_request->length;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002777 if (obj_end > rbd_dev->parent_overlap) {
2778 u64 xferred = 0;
2779
2780 if (obj_request->img_offset < rbd_dev->parent_overlap)
2781 xferred = rbd_dev->parent_overlap -
2782 obj_request->img_offset;
2783
Alex Elder02c74fb2013-05-06 17:40:33 -05002784 obj_request->xferred = min(img_xferred, xferred);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002785 } else {
Alex Elder02c74fb2013-05-06 17:40:33 -05002786 obj_request->xferred = img_xferred;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002787 }
2788out:
Alex Elder8b3e1a52013-01-24 16:13:36 -06002789 rbd_img_obj_request_read_callback(obj_request);
2790 rbd_obj_request_complete(obj_request);
2791}
2792
2793static void rbd_img_parent_read(struct rbd_obj_request *obj_request)
2794{
Alex Elder8b3e1a52013-01-24 16:13:36 -06002795 struct rbd_img_request *img_request;
2796 int result;
2797
2798 rbd_assert(obj_request_img_data_test(obj_request));
2799 rbd_assert(obj_request->img_request != NULL);
2800 rbd_assert(obj_request->result == (s32) -ENOENT);
Alex Elder5b2ab722013-05-06 17:40:33 -05002801 rbd_assert(obj_request_type_valid(obj_request->type));
Alex Elder8b3e1a52013-01-24 16:13:36 -06002802
Alex Elder8b3e1a52013-01-24 16:13:36 -06002803 /* rbd_read_finish(obj_request, obj_request->length); */
Alex Eldere93f3152013-05-08 22:50:04 -05002804 img_request = rbd_parent_request_create(obj_request,
Alex Elder8b3e1a52013-01-24 16:13:36 -06002805 obj_request->img_offset,
Alex Eldere93f3152013-05-08 22:50:04 -05002806 obj_request->length);
Alex Elder8b3e1a52013-01-24 16:13:36 -06002807 result = -ENOMEM;
2808 if (!img_request)
2809 goto out_err;
2810
Alex Elder5b2ab722013-05-06 17:40:33 -05002811 if (obj_request->type == OBJ_REQUEST_BIO)
2812 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
2813 obj_request->bio_list);
2814 else
2815 result = rbd_img_request_fill(img_request, OBJ_REQUEST_PAGES,
2816 obj_request->pages);
Alex Elder8b3e1a52013-01-24 16:13:36 -06002817 if (result)
2818 goto out_err;
2819
2820 img_request->callback = rbd_img_parent_read_callback;
2821 result = rbd_img_request_submit(img_request);
2822 if (result)
2823 goto out_err;
2824
2825 return;
2826out_err:
2827 if (img_request)
2828 rbd_img_request_put(img_request);
2829 obj_request->result = result;
2830 obj_request->xferred = 0;
2831 obj_request_done_set(obj_request);
2832}
2833
Josh Durgin20e0af62013-08-29 17:36:03 -07002834static int rbd_obj_notify_ack_sync(struct rbd_device *rbd_dev, u64 notify_id)
Alex Elderb8d70032012-11-30 17:53:04 -06002835{
2836 struct rbd_obj_request *obj_request;
Alex Elder21692382013-04-05 01:27:12 -05002837 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elderb8d70032012-11-30 17:53:04 -06002838 int ret;
2839
2840 obj_request = rbd_obj_request_create(rbd_dev->header_name, 0, 0,
2841 OBJ_REQUEST_NODATA);
2842 if (!obj_request)
2843 return -ENOMEM;
2844
2845 ret = -ENOMEM;
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02002846 obj_request->osd_req = rbd_osd_req_create(rbd_dev, false, 1,
2847 obj_request);
Alex Elderb8d70032012-11-30 17:53:04 -06002848 if (!obj_request->osd_req)
2849 goto out;
2850
Alex Elderc99d2d42013-04-05 01:27:11 -05002851 osd_req_op_watch_init(obj_request->osd_req, 0, CEPH_OSD_OP_NOTIFY_ACK,
Alex Eldercc4a38bd2013-04-30 00:44:33 -05002852 notify_id, 0, 0);
Alex Elder9d4df012013-04-19 15:34:50 -05002853 rbd_osd_req_format_read(obj_request);
Alex Elder430c28c2013-04-03 21:32:51 -05002854
Alex Elderb8d70032012-11-30 17:53:04 -06002855 ret = rbd_obj_request_submit(osdc, obj_request);
Alex Eldercf81b602013-01-17 12:18:46 -06002856 if (ret)
Josh Durgin20e0af62013-08-29 17:36:03 -07002857 goto out;
2858 ret = rbd_obj_request_wait(obj_request);
2859out:
2860 rbd_obj_request_put(obj_request);
Alex Elderb8d70032012-11-30 17:53:04 -06002861
2862 return ret;
2863}
2864
2865static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
2866{
2867 struct rbd_device *rbd_dev = (struct rbd_device *)data;
Alex Eldere627db02013-05-06 07:40:30 -05002868 int ret;
Alex Elderb8d70032012-11-30 17:53:04 -06002869
2870 if (!rbd_dev)
2871 return;
2872
Alex Elder37206ee2013-02-20 17:32:08 -06002873 dout("%s: \"%s\" notify_id %llu opcode %u\n", __func__,
Alex Eldercc4a38bd2013-04-30 00:44:33 -05002874 rbd_dev->header_name, (unsigned long long)notify_id,
2875 (unsigned int)opcode);
Alex Eldere627db02013-05-06 07:40:30 -05002876 ret = rbd_dev_refresh(rbd_dev);
2877 if (ret)
Alex Elder3b5cf2a2013-05-29 11:18:59 -05002878 rbd_warn(rbd_dev, "header refresh error (%d)\n", ret);
Alex Elderb8d70032012-11-30 17:53:04 -06002879
Josh Durgin20e0af62013-08-29 17:36:03 -07002880 rbd_obj_notify_ack_sync(rbd_dev, notify_id);
Alex Elderb8d70032012-11-30 17:53:04 -06002881}
2882
Alex Elder9969ebc2013-01-18 12:31:10 -06002883/*
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04002884 * Initiate a watch request, synchronously.
Alex Elder9969ebc2013-01-18 12:31:10 -06002885 */
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04002886static int rbd_dev_header_watch_sync(struct rbd_device *rbd_dev)
Alex Elder9969ebc2013-01-18 12:31:10 -06002887{
2888 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
2889 struct rbd_obj_request *obj_request;
Alex Elder9969ebc2013-01-18 12:31:10 -06002890 int ret;
2891
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04002892 rbd_assert(!rbd_dev->watch_event);
2893 rbd_assert(!rbd_dev->watch_request);
Alex Elder9969ebc2013-01-18 12:31:10 -06002894
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04002895 ret = ceph_osdc_create_event(osdc, rbd_watch_cb, rbd_dev,
2896 &rbd_dev->watch_event);
2897 if (ret < 0)
2898 return ret;
Alex Elder9969ebc2013-01-18 12:31:10 -06002899
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04002900 rbd_assert(rbd_dev->watch_event);
2901
Alex Elder9969ebc2013-01-18 12:31:10 -06002902 obj_request = rbd_obj_request_create(rbd_dev->header_name, 0, 0,
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04002903 OBJ_REQUEST_NODATA);
2904 if (!obj_request) {
2905 ret = -ENOMEM;
Alex Elder9969ebc2013-01-18 12:31:10 -06002906 goto out_cancel;
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04002907 }
Alex Elder9969ebc2013-01-18 12:31:10 -06002908
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02002909 obj_request->osd_req = rbd_osd_req_create(rbd_dev, true, 1,
2910 obj_request);
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04002911 if (!obj_request->osd_req) {
2912 ret = -ENOMEM;
2913 goto out_put;
2914 }
Alex Elder430c28c2013-04-03 21:32:51 -05002915
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04002916 ceph_osdc_set_request_linger(osdc, obj_request->osd_req);
Alex Elder21692382013-04-05 01:27:12 -05002917
2918 osd_req_op_watch_init(obj_request->osd_req, 0, CEPH_OSD_OP_WATCH,
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04002919 rbd_dev->watch_event->cookie, 0, 1);
Alex Elder9d4df012013-04-19 15:34:50 -05002920 rbd_osd_req_format_write(obj_request);
Alex Elder21692382013-04-05 01:27:12 -05002921
Alex Elder9969ebc2013-01-18 12:31:10 -06002922 ret = rbd_obj_request_submit(osdc, obj_request);
2923 if (ret)
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04002924 goto out_linger;
2925
Alex Elder9969ebc2013-01-18 12:31:10 -06002926 ret = rbd_obj_request_wait(obj_request);
2927 if (ret)
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04002928 goto out_linger;
2929
Alex Elder9969ebc2013-01-18 12:31:10 -06002930 ret = obj_request->result;
2931 if (ret)
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04002932 goto out_linger;
Alex Elder9969ebc2013-01-18 12:31:10 -06002933
Alex Elder8eb87562013-01-25 17:08:55 -06002934 /*
2935 * A watch request is set to linger, so the underlying osd
2936 * request won't go away until we unregister it. We retain
2937 * a pointer to the object request during that time (in
2938 * rbd_dev->watch_request), so we'll keep a reference to
2939 * it. We'll drop that reference (below) after we've
2940 * unregistered it.
2941 */
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04002942 rbd_dev->watch_request = obj_request;
Alex Elder8eb87562013-01-25 17:08:55 -06002943
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04002944 return 0;
Alex Elder8eb87562013-01-25 17:08:55 -06002945
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04002946out_linger:
2947 ceph_osdc_unregister_linger_request(osdc, obj_request->osd_req);
2948out_put:
2949 rbd_obj_request_put(obj_request);
Alex Elder9969ebc2013-01-18 12:31:10 -06002950out_cancel:
Alex Elder9969ebc2013-01-18 12:31:10 -06002951 ceph_osdc_cancel_event(rbd_dev->watch_event);
2952 rbd_dev->watch_event = NULL;
Alex Elder9969ebc2013-01-18 12:31:10 -06002953
2954 return ret;
2955}
2956
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04002957/*
2958 * Tear down a watch request, synchronously.
2959 */
2960static int __rbd_dev_header_unwatch_sync(struct rbd_device *rbd_dev)
Ilya Dryomovfca27062013-12-16 18:02:40 +02002961{
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04002962 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
2963 struct rbd_obj_request *obj_request;
2964 int ret;
2965
2966 rbd_assert(rbd_dev->watch_event);
2967 rbd_assert(rbd_dev->watch_request);
2968
2969 obj_request = rbd_obj_request_create(rbd_dev->header_name, 0, 0,
2970 OBJ_REQUEST_NODATA);
2971 if (!obj_request) {
2972 ret = -ENOMEM;
2973 goto out_cancel;
2974 }
2975
2976 obj_request->osd_req = rbd_osd_req_create(rbd_dev, true, 1,
2977 obj_request);
2978 if (!obj_request->osd_req) {
2979 ret = -ENOMEM;
2980 goto out_put;
2981 }
2982
2983 osd_req_op_watch_init(obj_request->osd_req, 0, CEPH_OSD_OP_WATCH,
2984 rbd_dev->watch_event->cookie, 0, 0);
2985 rbd_osd_req_format_write(obj_request);
2986
2987 ret = rbd_obj_request_submit(osdc, obj_request);
2988 if (ret)
2989 goto out_put;
2990
2991 ret = rbd_obj_request_wait(obj_request);
2992 if (ret)
2993 goto out_put;
2994
2995 ret = obj_request->result;
2996 if (ret)
2997 goto out_put;
2998
2999 /* We have successfully torn down the watch request */
3000
3001 ceph_osdc_unregister_linger_request(osdc,
3002 rbd_dev->watch_request->osd_req);
3003 rbd_obj_request_put(rbd_dev->watch_request);
3004 rbd_dev->watch_request = NULL;
3005
3006out_put:
3007 rbd_obj_request_put(obj_request);
3008out_cancel:
3009 ceph_osdc_cancel_event(rbd_dev->watch_event);
3010 rbd_dev->watch_event = NULL;
3011
3012 return ret;
Ilya Dryomovfca27062013-12-16 18:02:40 +02003013}
3014
3015static void rbd_dev_header_unwatch_sync(struct rbd_device *rbd_dev)
3016{
3017 int ret;
3018
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003019 ret = __rbd_dev_header_unwatch_sync(rbd_dev);
Ilya Dryomovfca27062013-12-16 18:02:40 +02003020 if (ret) {
3021 rbd_warn(rbd_dev, "unable to tear down watch request: %d\n",
3022 ret);
3023 }
3024}
3025
Alex Elder36be9a72013-01-19 00:30:28 -06003026/*
Alex Elderf40eb342013-04-25 15:09:42 -05003027 * Synchronous osd object method call. Returns the number of bytes
3028 * returned in the outbound buffer, or a negative error code.
Alex Elder36be9a72013-01-19 00:30:28 -06003029 */
3030static int rbd_obj_method_sync(struct rbd_device *rbd_dev,
3031 const char *object_name,
3032 const char *class_name,
3033 const char *method_name,
Alex Elder41579762013-04-21 12:14:45 -05003034 const void *outbound,
Alex Elder36be9a72013-01-19 00:30:28 -06003035 size_t outbound_size,
Alex Elder41579762013-04-21 12:14:45 -05003036 void *inbound,
Alex Eldere2a58ee2013-04-30 00:44:33 -05003037 size_t inbound_size)
Alex Elder36be9a72013-01-19 00:30:28 -06003038{
Alex Elder21692382013-04-05 01:27:12 -05003039 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder36be9a72013-01-19 00:30:28 -06003040 struct rbd_obj_request *obj_request;
Alex Elder36be9a72013-01-19 00:30:28 -06003041 struct page **pages;
3042 u32 page_count;
3043 int ret;
3044
3045 /*
Alex Elder6010a452013-04-05 01:27:11 -05003046 * Method calls are ultimately read operations. The result
3047 * should placed into the inbound buffer provided. They
3048 * also supply outbound data--parameters for the object
3049 * method. Currently if this is present it will be a
3050 * snapshot id.
Alex Elder36be9a72013-01-19 00:30:28 -06003051 */
Alex Elder57385b52013-04-21 12:14:45 -05003052 page_count = (u32)calc_pages_for(0, inbound_size);
Alex Elder36be9a72013-01-19 00:30:28 -06003053 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
3054 if (IS_ERR(pages))
3055 return PTR_ERR(pages);
3056
3057 ret = -ENOMEM;
Alex Elder6010a452013-04-05 01:27:11 -05003058 obj_request = rbd_obj_request_create(object_name, 0, inbound_size,
Alex Elder36be9a72013-01-19 00:30:28 -06003059 OBJ_REQUEST_PAGES);
3060 if (!obj_request)
3061 goto out;
3062
3063 obj_request->pages = pages;
3064 obj_request->page_count = page_count;
3065
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02003066 obj_request->osd_req = rbd_osd_req_create(rbd_dev, false, 1,
3067 obj_request);
Alex Elder36be9a72013-01-19 00:30:28 -06003068 if (!obj_request->osd_req)
3069 goto out;
3070
Alex Elderc99d2d42013-04-05 01:27:11 -05003071 osd_req_op_cls_init(obj_request->osd_req, 0, CEPH_OSD_OP_CALL,
Alex Elder04017e22013-04-05 14:46:02 -05003072 class_name, method_name);
3073 if (outbound_size) {
3074 struct ceph_pagelist *pagelist;
3075
3076 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
3077 if (!pagelist)
3078 goto out;
3079
3080 ceph_pagelist_init(pagelist);
3081 ceph_pagelist_append(pagelist, outbound, outbound_size);
3082 osd_req_op_cls_request_data_pagelist(obj_request->osd_req, 0,
3083 pagelist);
3084 }
Alex Eldera4ce40a2013-04-05 01:27:12 -05003085 osd_req_op_cls_response_data_pages(obj_request->osd_req, 0,
3086 obj_request->pages, inbound_size,
Alex Elder44cd1882013-04-05 01:27:12 -05003087 0, false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05003088 rbd_osd_req_format_read(obj_request);
Alex Elder430c28c2013-04-03 21:32:51 -05003089
Alex Elder36be9a72013-01-19 00:30:28 -06003090 ret = rbd_obj_request_submit(osdc, obj_request);
3091 if (ret)
3092 goto out;
3093 ret = rbd_obj_request_wait(obj_request);
3094 if (ret)
3095 goto out;
3096
3097 ret = obj_request->result;
3098 if (ret < 0)
3099 goto out;
Alex Elder57385b52013-04-21 12:14:45 -05003100
3101 rbd_assert(obj_request->xferred < (u64)INT_MAX);
3102 ret = (int)obj_request->xferred;
Alex Elder903bb322013-02-06 13:11:38 -06003103 ceph_copy_from_page_vector(pages, inbound, 0, obj_request->xferred);
Alex Elder36be9a72013-01-19 00:30:28 -06003104out:
3105 if (obj_request)
3106 rbd_obj_request_put(obj_request);
3107 else
3108 ceph_release_page_vector(pages, page_count);
3109
3110 return ret;
3111}
3112
Alex Elderbf0d5f502012-11-22 00:00:08 -06003113static void rbd_request_fn(struct request_queue *q)
Alex Eldercc344fa2013-02-19 12:25:56 -06003114 __releases(q->queue_lock) __acquires(q->queue_lock)
Alex Elderbf0d5f502012-11-22 00:00:08 -06003115{
3116 struct rbd_device *rbd_dev = q->queuedata;
3117 bool read_only = rbd_dev->mapping.read_only;
3118 struct request *rq;
3119 int result;
3120
3121 while ((rq = blk_fetch_request(q))) {
3122 bool write_request = rq_data_dir(rq) == WRITE;
3123 struct rbd_img_request *img_request;
3124 u64 offset;
3125 u64 length;
3126
3127 /* Ignore any non-FS requests that filter through. */
3128
3129 if (rq->cmd_type != REQ_TYPE_FS) {
Alex Elder4dda41d2013-02-20 21:59:33 -06003130 dout("%s: non-fs request type %d\n", __func__,
3131 (int) rq->cmd_type);
3132 __blk_end_request_all(rq, 0);
3133 continue;
3134 }
3135
3136 /* Ignore/skip any zero-length requests */
3137
3138 offset = (u64) blk_rq_pos(rq) << SECTOR_SHIFT;
3139 length = (u64) blk_rq_bytes(rq);
3140
3141 if (!length) {
3142 dout("%s: zero-length request\n", __func__);
Alex Elderbf0d5f502012-11-22 00:00:08 -06003143 __blk_end_request_all(rq, 0);
3144 continue;
3145 }
3146
3147 spin_unlock_irq(q->queue_lock);
3148
3149 /* Disallow writes to a read-only device */
3150
3151 if (write_request) {
3152 result = -EROFS;
3153 if (read_only)
3154 goto end_request;
3155 rbd_assert(rbd_dev->spec->snap_id == CEPH_NOSNAP);
3156 }
3157
Alex Elder6d292902013-01-14 12:43:31 -06003158 /*
3159 * Quit early if the mapped snapshot no longer
3160 * exists. It's still possible the snapshot will
3161 * have disappeared by the time our request arrives
3162 * at the osd, but there's no sense in sending it if
3163 * we already know.
3164 */
3165 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) {
Alex Elderbf0d5f502012-11-22 00:00:08 -06003166 dout("request for non-existent snapshot");
3167 rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP);
3168 result = -ENXIO;
3169 goto end_request;
3170 }
3171
Alex Elderbf0d5f502012-11-22 00:00:08 -06003172 result = -EINVAL;
Alex Elderc0cd10db2013-04-26 09:43:47 -05003173 if (offset && length > U64_MAX - offset + 1) {
3174 rbd_warn(rbd_dev, "bad request range (%llu~%llu)\n",
3175 offset, length);
Alex Elderbf0d5f502012-11-22 00:00:08 -06003176 goto end_request; /* Shouldn't happen */
Alex Elderc0cd10db2013-04-26 09:43:47 -05003177 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06003178
Alex Elder00a653e2013-05-06 07:40:30 -05003179 result = -EIO;
3180 if (offset + length > rbd_dev->mapping.size) {
3181 rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)\n",
3182 offset, length, rbd_dev->mapping.size);
3183 goto end_request;
3184 }
3185
Alex Elderbf0d5f502012-11-22 00:00:08 -06003186 result = -ENOMEM;
3187 img_request = rbd_img_request_create(rbd_dev, offset, length,
Alex Eldere93f3152013-05-08 22:50:04 -05003188 write_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06003189 if (!img_request)
3190 goto end_request;
3191
3192 img_request->rq = rq;
3193
Alex Elderf1a47392013-04-19 15:34:50 -05003194 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
3195 rq->bio);
Alex Elderbf0d5f502012-11-22 00:00:08 -06003196 if (!result)
3197 result = rbd_img_request_submit(img_request);
3198 if (result)
3199 rbd_img_request_put(img_request);
3200end_request:
3201 spin_lock_irq(q->queue_lock);
3202 if (result < 0) {
Alex Elder7da22d22013-01-24 16:13:36 -06003203 rbd_warn(rbd_dev, "%s %llx at %llx result %d\n",
3204 write_request ? "write" : "read",
3205 length, offset, result);
3206
Alex Elderbf0d5f502012-11-22 00:00:08 -06003207 __blk_end_request_all(rq, result);
3208 }
3209 }
3210}
3211
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003212/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003213 * a queue callback. Makes sure that we don't create a bio that spans across
3214 * multiple osd objects. One exception would be with a single page bios,
Alex Elderf7760da2012-10-20 22:17:27 -05003215 * which we handle later at bio_chain_clone_range()
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003216 */
3217static int rbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd,
3218 struct bio_vec *bvec)
3219{
3220 struct rbd_device *rbd_dev = q->queuedata;
Alex Eldere5cfeed22012-10-20 22:17:27 -05003221 sector_t sector_offset;
3222 sector_t sectors_per_obj;
3223 sector_t obj_sector_offset;
3224 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003225
Alex Eldere5cfeed22012-10-20 22:17:27 -05003226 /*
3227 * Find how far into its rbd object the partition-relative
3228 * bio start sector is to offset relative to the enclosing
3229 * device.
3230 */
3231 sector_offset = get_start_sect(bmd->bi_bdev) + bmd->bi_sector;
3232 sectors_per_obj = 1 << (rbd_dev->header.obj_order - SECTOR_SHIFT);
3233 obj_sector_offset = sector_offset & (sectors_per_obj - 1);
Alex Elder593a9e72012-02-07 12:03:37 -06003234
Alex Eldere5cfeed22012-10-20 22:17:27 -05003235 /*
3236 * Compute the number of bytes from that offset to the end
3237 * of the object. Account for what's already used by the bio.
3238 */
3239 ret = (int) (sectors_per_obj - obj_sector_offset) << SECTOR_SHIFT;
3240 if (ret > bmd->bi_size)
3241 ret -= bmd->bi_size;
3242 else
3243 ret = 0;
3244
3245 /*
3246 * Don't send back more than was asked for. And if the bio
3247 * was empty, let the whole thing through because: "Note
3248 * that a block device *must* allow a single page to be
3249 * added to an empty bio."
3250 */
3251 rbd_assert(bvec->bv_len <= PAGE_SIZE);
3252 if (ret > (int) bvec->bv_len || !bmd->bi_size)
3253 ret = (int) bvec->bv_len;
3254
3255 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003256}
3257
3258static void rbd_free_disk(struct rbd_device *rbd_dev)
3259{
3260 struct gendisk *disk = rbd_dev->disk;
3261
3262 if (!disk)
3263 return;
3264
Alex Eldera0cab922013-04-25 23:15:08 -05003265 rbd_dev->disk = NULL;
3266 if (disk->flags & GENHD_FL_UP) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003267 del_gendisk(disk);
Alex Eldera0cab922013-04-25 23:15:08 -05003268 if (disk->queue)
3269 blk_cleanup_queue(disk->queue);
3270 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003271 put_disk(disk);
3272}
3273
Alex Elder788e2df2013-01-17 12:25:27 -06003274static int rbd_obj_read_sync(struct rbd_device *rbd_dev,
3275 const char *object_name,
Alex Elder7097f8d2013-04-30 00:44:33 -05003276 u64 offset, u64 length, void *buf)
Alex Elder788e2df2013-01-17 12:25:27 -06003277
3278{
Alex Elder21692382013-04-05 01:27:12 -05003279 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Alex Elder788e2df2013-01-17 12:25:27 -06003280 struct rbd_obj_request *obj_request;
Alex Elder788e2df2013-01-17 12:25:27 -06003281 struct page **pages = NULL;
3282 u32 page_count;
Alex Elder1ceae7e2013-02-06 13:11:38 -06003283 size_t size;
Alex Elder788e2df2013-01-17 12:25:27 -06003284 int ret;
3285
3286 page_count = (u32) calc_pages_for(offset, length);
3287 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
3288 if (IS_ERR(pages))
3289 ret = PTR_ERR(pages);
3290
3291 ret = -ENOMEM;
3292 obj_request = rbd_obj_request_create(object_name, offset, length,
Alex Elder36be9a72013-01-19 00:30:28 -06003293 OBJ_REQUEST_PAGES);
Alex Elder788e2df2013-01-17 12:25:27 -06003294 if (!obj_request)
3295 goto out;
3296
3297 obj_request->pages = pages;
3298 obj_request->page_count = page_count;
3299
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02003300 obj_request->osd_req = rbd_osd_req_create(rbd_dev, false, 1,
3301 obj_request);
Alex Elder788e2df2013-01-17 12:25:27 -06003302 if (!obj_request->osd_req)
3303 goto out;
3304
Alex Elderc99d2d42013-04-05 01:27:11 -05003305 osd_req_op_extent_init(obj_request->osd_req, 0, CEPH_OSD_OP_READ,
3306 offset, length, 0, 0);
Alex Elder406e2c92013-04-15 14:50:36 -05003307 osd_req_op_extent_osd_data_pages(obj_request->osd_req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05003308 obj_request->pages,
Alex Elder44cd1882013-04-05 01:27:12 -05003309 obj_request->length,
3310 obj_request->offset & ~PAGE_MASK,
3311 false, false);
Alex Elder9d4df012013-04-19 15:34:50 -05003312 rbd_osd_req_format_read(obj_request);
Alex Elder430c28c2013-04-03 21:32:51 -05003313
Alex Elder788e2df2013-01-17 12:25:27 -06003314 ret = rbd_obj_request_submit(osdc, obj_request);
3315 if (ret)
3316 goto out;
3317 ret = rbd_obj_request_wait(obj_request);
3318 if (ret)
3319 goto out;
3320
3321 ret = obj_request->result;
3322 if (ret < 0)
3323 goto out;
Alex Elder1ceae7e2013-02-06 13:11:38 -06003324
3325 rbd_assert(obj_request->xferred <= (u64) SIZE_MAX);
3326 size = (size_t) obj_request->xferred;
Alex Elder903bb322013-02-06 13:11:38 -06003327 ceph_copy_from_page_vector(pages, buf, 0, size);
Alex Elder7097f8d2013-04-30 00:44:33 -05003328 rbd_assert(size <= (size_t)INT_MAX);
3329 ret = (int)size;
Alex Elder788e2df2013-01-17 12:25:27 -06003330out:
3331 if (obj_request)
3332 rbd_obj_request_put(obj_request);
3333 else
3334 ceph_release_page_vector(pages, page_count);
3335
3336 return ret;
3337}
3338
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003339/*
Alex Elder662518b2013-05-06 09:51:29 -05003340 * Read the complete header for the given rbd device. On successful
3341 * return, the rbd_dev->header field will contain up-to-date
3342 * information about the image.
Alex Elder4156d992012-08-02 11:29:46 -05003343 */
Alex Elder99a41eb2013-05-06 09:51:30 -05003344static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev)
Alex Elder4156d992012-08-02 11:29:46 -05003345{
3346 struct rbd_image_header_ondisk *ondisk = NULL;
3347 u32 snap_count = 0;
3348 u64 names_size = 0;
3349 u32 want_count;
3350 int ret;
3351
3352 /*
3353 * The complete header will include an array of its 64-bit
3354 * snapshot ids, followed by the names of those snapshots as
3355 * a contiguous block of NUL-terminated strings. Note that
3356 * the number of snapshots could change by the time we read
3357 * it in, in which case we re-read it.
3358 */
3359 do {
3360 size_t size;
3361
3362 kfree(ondisk);
3363
3364 size = sizeof (*ondisk);
3365 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
3366 size += names_size;
3367 ondisk = kmalloc(size, GFP_KERNEL);
3368 if (!ondisk)
Alex Elder662518b2013-05-06 09:51:29 -05003369 return -ENOMEM;
Alex Elder4156d992012-08-02 11:29:46 -05003370
Alex Elder788e2df2013-01-17 12:25:27 -06003371 ret = rbd_obj_read_sync(rbd_dev, rbd_dev->header_name,
Alex Elder7097f8d2013-04-30 00:44:33 -05003372 0, size, ondisk);
Alex Elder4156d992012-08-02 11:29:46 -05003373 if (ret < 0)
Alex Elder662518b2013-05-06 09:51:29 -05003374 goto out;
Alex Elderc0cd10db2013-04-26 09:43:47 -05003375 if ((size_t)ret < size) {
Alex Elder4156d992012-08-02 11:29:46 -05003376 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05003377 rbd_warn(rbd_dev, "short header read (want %zd got %d)",
3378 size, ret);
Alex Elder662518b2013-05-06 09:51:29 -05003379 goto out;
Alex Elder4156d992012-08-02 11:29:46 -05003380 }
3381 if (!rbd_dev_ondisk_valid(ondisk)) {
3382 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05003383 rbd_warn(rbd_dev, "invalid header");
Alex Elder662518b2013-05-06 09:51:29 -05003384 goto out;
Alex Elder4156d992012-08-02 11:29:46 -05003385 }
3386
3387 names_size = le64_to_cpu(ondisk->snap_names_len);
3388 want_count = snap_count;
3389 snap_count = le32_to_cpu(ondisk->snap_count);
3390 } while (snap_count != want_count);
3391
Alex Elder662518b2013-05-06 09:51:29 -05003392 ret = rbd_header_from_disk(rbd_dev, ondisk);
3393out:
Alex Elder4156d992012-08-02 11:29:46 -05003394 kfree(ondisk);
3395
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003396 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003397}
3398
Alex Elder15228ed2013-05-01 12:43:03 -05003399/*
3400 * Clear the rbd device's EXISTS flag if the snapshot it's mapped to
3401 * has disappeared from the (just updated) snapshot context.
3402 */
3403static void rbd_exists_validate(struct rbd_device *rbd_dev)
3404{
3405 u64 snap_id;
3406
3407 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags))
3408 return;
3409
3410 snap_id = rbd_dev->spec->snap_id;
3411 if (snap_id == CEPH_NOSNAP)
3412 return;
3413
3414 if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX)
3415 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
3416}
3417
Josh Durgin98752012013-08-29 17:26:31 -07003418static void rbd_dev_update_size(struct rbd_device *rbd_dev)
3419{
3420 sector_t size;
3421 bool removing;
3422
3423 /*
3424 * Don't hold the lock while doing disk operations,
3425 * or lock ordering will conflict with the bdev mutex via:
3426 * rbd_add() -> blkdev_get() -> rbd_open()
3427 */
3428 spin_lock_irq(&rbd_dev->lock);
3429 removing = test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags);
3430 spin_unlock_irq(&rbd_dev->lock);
3431 /*
3432 * If the device is being removed, rbd_dev->disk has
3433 * been destroyed, so don't try to update its size
3434 */
3435 if (!removing) {
3436 size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE;
3437 dout("setting size to %llu sectors", (unsigned long long)size);
3438 set_capacity(rbd_dev->disk, size);
3439 revalidate_disk(rbd_dev->disk);
3440 }
3441}
3442
Alex Eldercc4a38bd2013-04-30 00:44:33 -05003443static int rbd_dev_refresh(struct rbd_device *rbd_dev)
Alex Elder1fe5e992012-07-25 09:32:41 -05003444{
Alex Eldere627db02013-05-06 07:40:30 -05003445 u64 mapping_size;
Alex Elder1fe5e992012-07-25 09:32:41 -05003446 int ret;
3447
Alex Elder117973f2012-08-31 17:29:55 -05003448 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
Alex Eldercfbf6372013-05-31 17:40:45 -05003449 down_write(&rbd_dev->header_rwsem);
Alex Elder3b5cf2a2013-05-29 11:18:59 -05003450 mapping_size = rbd_dev->mapping.size;
Alex Elder117973f2012-08-31 17:29:55 -05003451 if (rbd_dev->image_format == 1)
Alex Elder99a41eb2013-05-06 09:51:30 -05003452 ret = rbd_dev_v1_header_info(rbd_dev);
Alex Elder117973f2012-08-31 17:29:55 -05003453 else
Alex Elder2df3fac2013-05-06 09:51:30 -05003454 ret = rbd_dev_v2_header_info(rbd_dev);
Alex Elder15228ed2013-05-01 12:43:03 -05003455
3456 /* If it's a mapped snapshot, validate its EXISTS flag */
3457
3458 rbd_exists_validate(rbd_dev);
Alex Eldercfbf6372013-05-31 17:40:45 -05003459 up_write(&rbd_dev->header_rwsem);
3460
Alex Elder00a653e2013-05-06 07:40:30 -05003461 if (mapping_size != rbd_dev->mapping.size) {
Josh Durgin98752012013-08-29 17:26:31 -07003462 rbd_dev_update_size(rbd_dev);
Alex Elder00a653e2013-05-06 07:40:30 -05003463 }
Alex Elder1fe5e992012-07-25 09:32:41 -05003464
3465 return ret;
3466}
3467
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003468static int rbd_init_disk(struct rbd_device *rbd_dev)
3469{
3470 struct gendisk *disk;
3471 struct request_queue *q;
Alex Elder593a9e72012-02-07 12:03:37 -06003472 u64 segment_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003473
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003474 /* create gendisk info */
Ilya Dryomov7e513d42013-12-16 19:26:32 +02003475 disk = alloc_disk(single_major ?
3476 (1 << RBD_SINGLE_MAJOR_PART_SHIFT) :
3477 RBD_MINORS_PER_MAJOR);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003478 if (!disk)
Alex Elder1fcdb8a2012-08-29 17:11:06 -05003479 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003480
Alex Elderf0f8cef2012-01-29 13:57:44 -06003481 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
Alex Elderde71a292012-07-03 16:01:19 -05003482 rbd_dev->dev_id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003483 disk->major = rbd_dev->major;
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02003484 disk->first_minor = rbd_dev->minor;
Ilya Dryomov7e513d42013-12-16 19:26:32 +02003485 if (single_major)
3486 disk->flags |= GENHD_FL_EXT_DEVT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003487 disk->fops = &rbd_bd_ops;
3488 disk->private_data = rbd_dev;
3489
Alex Elderbf0d5f502012-11-22 00:00:08 -06003490 q = blk_init_queue(rbd_request_fn, &rbd_dev->lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003491 if (!q)
3492 goto out_disk;
Josh Durgin029bcbd2011-07-22 11:35:23 -07003493
Alex Elder593a9e72012-02-07 12:03:37 -06003494 /* We use the default size, but let's be explicit about it. */
3495 blk_queue_physical_block_size(q, SECTOR_SIZE);
3496
Josh Durgin029bcbd2011-07-22 11:35:23 -07003497 /* set io sizes to object size */
Alex Elder593a9e72012-02-07 12:03:37 -06003498 segment_size = rbd_obj_bytes(&rbd_dev->header);
3499 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
3500 blk_queue_max_segment_size(q, segment_size);
3501 blk_queue_io_min(q, segment_size);
3502 blk_queue_io_opt(q, segment_size);
Josh Durgin029bcbd2011-07-22 11:35:23 -07003503
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003504 blk_queue_merge_bvec(q, rbd_merge_bvec);
3505 disk->queue = q;
3506
3507 q->queuedata = rbd_dev;
3508
3509 rbd_dev->disk = disk;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003510
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003511 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003512out_disk:
3513 put_disk(disk);
Alex Elder1fcdb8a2012-08-29 17:11:06 -05003514
3515 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003516}
3517
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003518/*
3519 sysfs
3520*/
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003521
Alex Elder593a9e72012-02-07 12:03:37 -06003522static struct rbd_device *dev_to_rbd_dev(struct device *dev)
3523{
3524 return container_of(dev, struct rbd_device, dev);
3525}
3526
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003527static ssize_t rbd_size_show(struct device *dev,
3528 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003529{
Alex Elder593a9e72012-02-07 12:03:37 -06003530 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003531
Alex Elderfc71d832013-04-26 15:44:36 -05003532 return sprintf(buf, "%llu\n",
3533 (unsigned long long)rbd_dev->mapping.size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003534}
3535
Alex Elder34b13182012-07-13 20:35:12 -05003536/*
3537 * Note this shows the features for whatever's mapped, which is not
3538 * necessarily the base image.
3539 */
3540static ssize_t rbd_features_show(struct device *dev,
3541 struct device_attribute *attr, char *buf)
3542{
3543 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3544
3545 return sprintf(buf, "0x%016llx\n",
Alex Elderfc71d832013-04-26 15:44:36 -05003546 (unsigned long long)rbd_dev->mapping.features);
Alex Elder34b13182012-07-13 20:35:12 -05003547}
3548
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003549static ssize_t rbd_major_show(struct device *dev,
3550 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003551{
Alex Elder593a9e72012-02-07 12:03:37 -06003552 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003553
Alex Elderfc71d832013-04-26 15:44:36 -05003554 if (rbd_dev->major)
3555 return sprintf(buf, "%d\n", rbd_dev->major);
3556
3557 return sprintf(buf, "(none)\n");
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02003558}
Alex Elderfc71d832013-04-26 15:44:36 -05003559
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02003560static ssize_t rbd_minor_show(struct device *dev,
3561 struct device_attribute *attr, char *buf)
3562{
3563 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3564
3565 return sprintf(buf, "%d\n", rbd_dev->minor);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003566}
3567
3568static ssize_t rbd_client_id_show(struct device *dev,
3569 struct device_attribute *attr, char *buf)
3570{
Alex Elder593a9e72012-02-07 12:03:37 -06003571 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003572
Alex Elder1dbb4392012-01-24 10:08:37 -06003573 return sprintf(buf, "client%lld\n",
3574 ceph_client_id(rbd_dev->rbd_client->client));
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003575}
3576
3577static ssize_t rbd_pool_show(struct device *dev,
3578 struct device_attribute *attr, char *buf)
3579{
Alex Elder593a9e72012-02-07 12:03:37 -06003580 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003581
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003582 return sprintf(buf, "%s\n", rbd_dev->spec->pool_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003583}
3584
Alex Elder9bb2f332012-07-12 10:46:35 -05003585static ssize_t rbd_pool_id_show(struct device *dev,
3586 struct device_attribute *attr, char *buf)
3587{
3588 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3589
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003590 return sprintf(buf, "%llu\n",
Alex Elderfc71d832013-04-26 15:44:36 -05003591 (unsigned long long) rbd_dev->spec->pool_id);
Alex Elder9bb2f332012-07-12 10:46:35 -05003592}
3593
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003594static ssize_t rbd_name_show(struct device *dev,
3595 struct device_attribute *attr, char *buf)
3596{
Alex Elder593a9e72012-02-07 12:03:37 -06003597 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003598
Alex Eldera92ffdf2012-10-30 19:40:33 -05003599 if (rbd_dev->spec->image_name)
3600 return sprintf(buf, "%s\n", rbd_dev->spec->image_name);
3601
3602 return sprintf(buf, "(unknown)\n");
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003603}
3604
Alex Elder589d30e2012-07-10 20:30:11 -05003605static ssize_t rbd_image_id_show(struct device *dev,
3606 struct device_attribute *attr, char *buf)
3607{
3608 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3609
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003610 return sprintf(buf, "%s\n", rbd_dev->spec->image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05003611}
3612
Alex Elder34b13182012-07-13 20:35:12 -05003613/*
3614 * Shows the name of the currently-mapped snapshot (or
3615 * RBD_SNAP_HEAD_NAME for the base image).
3616 */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003617static ssize_t rbd_snap_show(struct device *dev,
3618 struct device_attribute *attr,
3619 char *buf)
3620{
Alex Elder593a9e72012-02-07 12:03:37 -06003621 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003622
Alex Elder0d7dbfc2012-10-25 23:34:41 -05003623 return sprintf(buf, "%s\n", rbd_dev->spec->snap_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003624}
3625
Alex Elder86b00e02012-10-25 23:34:42 -05003626/*
3627 * For an rbd v2 image, shows the pool id, image id, and snapshot id
3628 * for the parent image. If there is no parent, simply shows
3629 * "(no parent image)".
3630 */
3631static ssize_t rbd_parent_show(struct device *dev,
3632 struct device_attribute *attr,
3633 char *buf)
3634{
3635 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3636 struct rbd_spec *spec = rbd_dev->parent_spec;
3637 int count;
3638 char *bufp = buf;
3639
3640 if (!spec)
3641 return sprintf(buf, "(no parent image)\n");
3642
3643 count = sprintf(bufp, "pool_id %llu\npool_name %s\n",
3644 (unsigned long long) spec->pool_id, spec->pool_name);
3645 if (count < 0)
3646 return count;
3647 bufp += count;
3648
3649 count = sprintf(bufp, "image_id %s\nimage_name %s\n", spec->image_id,
3650 spec->image_name ? spec->image_name : "(unknown)");
3651 if (count < 0)
3652 return count;
3653 bufp += count;
3654
3655 count = sprintf(bufp, "snap_id %llu\nsnap_name %s\n",
3656 (unsigned long long) spec->snap_id, spec->snap_name);
3657 if (count < 0)
3658 return count;
3659 bufp += count;
3660
3661 count = sprintf(bufp, "overlap %llu\n", rbd_dev->parent_overlap);
3662 if (count < 0)
3663 return count;
3664 bufp += count;
3665
3666 return (ssize_t) (bufp - buf);
3667}
3668
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003669static ssize_t rbd_image_refresh(struct device *dev,
3670 struct device_attribute *attr,
3671 const char *buf,
3672 size_t size)
3673{
Alex Elder593a9e72012-02-07 12:03:37 -06003674 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Alex Elderb8136232012-07-25 09:32:41 -05003675 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003676
Alex Eldercc4a38bd2013-04-30 00:44:33 -05003677 ret = rbd_dev_refresh(rbd_dev);
Alex Eldere627db02013-05-06 07:40:30 -05003678 if (ret)
3679 rbd_warn(rbd_dev, ": manual header refresh error (%d)\n", ret);
Alex Elderb8136232012-07-25 09:32:41 -05003680
3681 return ret < 0 ? ret : size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003682}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003683
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003684static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
Alex Elder34b13182012-07-13 20:35:12 -05003685static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003686static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02003687static DEVICE_ATTR(minor, S_IRUGO, rbd_minor_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003688static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
3689static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
Alex Elder9bb2f332012-07-12 10:46:35 -05003690static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003691static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
Alex Elder589d30e2012-07-10 20:30:11 -05003692static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003693static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
3694static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
Alex Elder86b00e02012-10-25 23:34:42 -05003695static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003696
3697static struct attribute *rbd_attrs[] = {
3698 &dev_attr_size.attr,
Alex Elder34b13182012-07-13 20:35:12 -05003699 &dev_attr_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003700 &dev_attr_major.attr,
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02003701 &dev_attr_minor.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003702 &dev_attr_client_id.attr,
3703 &dev_attr_pool.attr,
Alex Elder9bb2f332012-07-12 10:46:35 -05003704 &dev_attr_pool_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003705 &dev_attr_name.attr,
Alex Elder589d30e2012-07-10 20:30:11 -05003706 &dev_attr_image_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003707 &dev_attr_current_snap.attr,
Alex Elder86b00e02012-10-25 23:34:42 -05003708 &dev_attr_parent.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003709 &dev_attr_refresh.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003710 NULL
3711};
3712
3713static struct attribute_group rbd_attr_group = {
3714 .attrs = rbd_attrs,
3715};
3716
3717static const struct attribute_group *rbd_attr_groups[] = {
3718 &rbd_attr_group,
3719 NULL
3720};
3721
3722static void rbd_sysfs_dev_release(struct device *dev)
3723{
3724}
3725
3726static struct device_type rbd_device_type = {
3727 .name = "rbd",
3728 .groups = rbd_attr_groups,
3729 .release = rbd_sysfs_dev_release,
3730};
3731
Alex Elder8b8fb992012-10-26 17:25:24 -05003732static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)
3733{
3734 kref_get(&spec->kref);
3735
3736 return spec;
3737}
3738
3739static void rbd_spec_free(struct kref *kref);
3740static void rbd_spec_put(struct rbd_spec *spec)
3741{
3742 if (spec)
3743 kref_put(&spec->kref, rbd_spec_free);
3744}
3745
3746static struct rbd_spec *rbd_spec_alloc(void)
3747{
3748 struct rbd_spec *spec;
3749
3750 spec = kzalloc(sizeof (*spec), GFP_KERNEL);
3751 if (!spec)
3752 return NULL;
3753 kref_init(&spec->kref);
3754
Alex Elder8b8fb992012-10-26 17:25:24 -05003755 return spec;
3756}
3757
3758static void rbd_spec_free(struct kref *kref)
3759{
3760 struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref);
3761
3762 kfree(spec->pool_name);
3763 kfree(spec->image_id);
3764 kfree(spec->image_name);
3765 kfree(spec->snap_name);
3766 kfree(spec);
3767}
3768
Alex Eldercc344fa2013-02-19 12:25:56 -06003769static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,
Alex Elderc53d5892012-10-25 23:34:42 -05003770 struct rbd_spec *spec)
3771{
3772 struct rbd_device *rbd_dev;
3773
3774 rbd_dev = kzalloc(sizeof (*rbd_dev), GFP_KERNEL);
3775 if (!rbd_dev)
3776 return NULL;
3777
3778 spin_lock_init(&rbd_dev->lock);
Alex Elder6d292902013-01-14 12:43:31 -06003779 rbd_dev->flags = 0;
Alex Eldera2acd002013-05-08 22:50:04 -05003780 atomic_set(&rbd_dev->parent_ref, 0);
Alex Elderc53d5892012-10-25 23:34:42 -05003781 INIT_LIST_HEAD(&rbd_dev->node);
Alex Elderc53d5892012-10-25 23:34:42 -05003782 init_rwsem(&rbd_dev->header_rwsem);
3783
3784 rbd_dev->spec = spec;
3785 rbd_dev->rbd_client = rbdc;
3786
Alex Elder0903e872012-11-14 12:25:19 -06003787 /* Initialize the layout used for all rbd requests */
3788
3789 rbd_dev->layout.fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
3790 rbd_dev->layout.fl_stripe_count = cpu_to_le32(1);
3791 rbd_dev->layout.fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
3792 rbd_dev->layout.fl_pg_pool = cpu_to_le32((u32) spec->pool_id);
3793
Alex Elderc53d5892012-10-25 23:34:42 -05003794 return rbd_dev;
3795}
3796
3797static void rbd_dev_destroy(struct rbd_device *rbd_dev)
3798{
Alex Elderc53d5892012-10-25 23:34:42 -05003799 rbd_put_client(rbd_dev->rbd_client);
3800 rbd_spec_put(rbd_dev->spec);
3801 kfree(rbd_dev);
3802}
3803
Yehuda Sadehdfc56062010-11-19 14:51:04 -08003804/*
Alex Elder9d475de2012-07-03 16:01:19 -05003805 * Get the size and object order for an image snapshot, or if
3806 * snap_id is CEPH_NOSNAP, gets this information for the base
3807 * image.
3808 */
3809static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
3810 u8 *order, u64 *snap_size)
3811{
3812 __le64 snapid = cpu_to_le64(snap_id);
3813 int ret;
3814 struct {
3815 u8 order;
3816 __le64 size;
3817 } __attribute__ ((packed)) size_buf = { 0 };
3818
Alex Elder36be9a72013-01-19 00:30:28 -06003819 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elder9d475de2012-07-03 16:01:19 -05003820 "rbd", "get_size",
Alex Elder41579762013-04-21 12:14:45 -05003821 &snapid, sizeof (snapid),
Alex Eldere2a58ee2013-04-30 00:44:33 -05003822 &size_buf, sizeof (size_buf));
Alex Elder36be9a72013-01-19 00:30:28 -06003823 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder9d475de2012-07-03 16:01:19 -05003824 if (ret < 0)
3825 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05003826 if (ret < sizeof (size_buf))
3827 return -ERANGE;
Alex Elder9d475de2012-07-03 16:01:19 -05003828
Josh Durginc3545572013-08-28 17:08:10 -07003829 if (order) {
Alex Elderc86f86e2013-04-25 15:09:41 -05003830 *order = size_buf.order;
Josh Durginc3545572013-08-28 17:08:10 -07003831 dout(" order %u", (unsigned int)*order);
3832 }
Alex Elder9d475de2012-07-03 16:01:19 -05003833 *snap_size = le64_to_cpu(size_buf.size);
3834
Josh Durginc3545572013-08-28 17:08:10 -07003835 dout(" snap_id 0x%016llx snap_size = %llu\n",
3836 (unsigned long long)snap_id,
Alex Elder57385b52013-04-21 12:14:45 -05003837 (unsigned long long)*snap_size);
Alex Elder9d475de2012-07-03 16:01:19 -05003838
3839 return 0;
3840}
3841
3842static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
3843{
3844 return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
3845 &rbd_dev->header.obj_order,
3846 &rbd_dev->header.image_size);
3847}
3848
Alex Elder1e130192012-07-03 16:01:19 -05003849static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
3850{
3851 void *reply_buf;
3852 int ret;
3853 void *p;
3854
3855 reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL);
3856 if (!reply_buf)
3857 return -ENOMEM;
3858
Alex Elder36be9a72013-01-19 00:30:28 -06003859 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elder41579762013-04-21 12:14:45 -05003860 "rbd", "get_object_prefix", NULL, 0,
Alex Eldere2a58ee2013-04-30 00:44:33 -05003861 reply_buf, RBD_OBJ_PREFIX_LEN_MAX);
Alex Elder36be9a72013-01-19 00:30:28 -06003862 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder1e130192012-07-03 16:01:19 -05003863 if (ret < 0)
3864 goto out;
3865
3866 p = reply_buf;
3867 rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p,
Alex Elder57385b52013-04-21 12:14:45 -05003868 p + ret, NULL, GFP_NOIO);
3869 ret = 0;
Alex Elder1e130192012-07-03 16:01:19 -05003870
3871 if (IS_ERR(rbd_dev->header.object_prefix)) {
3872 ret = PTR_ERR(rbd_dev->header.object_prefix);
3873 rbd_dev->header.object_prefix = NULL;
3874 } else {
3875 dout(" object_prefix = %s\n", rbd_dev->header.object_prefix);
3876 }
Alex Elder1e130192012-07-03 16:01:19 -05003877out:
3878 kfree(reply_buf);
3879
3880 return ret;
3881}
3882
Alex Elderb1b54022012-07-03 16:01:19 -05003883static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
3884 u64 *snap_features)
3885{
3886 __le64 snapid = cpu_to_le64(snap_id);
3887 struct {
3888 __le64 features;
3889 __le64 incompat;
Alex Elder41579762013-04-21 12:14:45 -05003890 } __attribute__ ((packed)) features_buf = { 0 };
Alex Elderd8891402012-10-09 13:50:17 -07003891 u64 incompat;
Alex Elderb1b54022012-07-03 16:01:19 -05003892 int ret;
3893
Alex Elder36be9a72013-01-19 00:30:28 -06003894 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elderb1b54022012-07-03 16:01:19 -05003895 "rbd", "get_features",
Alex Elder41579762013-04-21 12:14:45 -05003896 &snapid, sizeof (snapid),
Alex Eldere2a58ee2013-04-30 00:44:33 -05003897 &features_buf, sizeof (features_buf));
Alex Elder36be9a72013-01-19 00:30:28 -06003898 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderb1b54022012-07-03 16:01:19 -05003899 if (ret < 0)
3900 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05003901 if (ret < sizeof (features_buf))
3902 return -ERANGE;
Alex Elderd8891402012-10-09 13:50:17 -07003903
3904 incompat = le64_to_cpu(features_buf.incompat);
Alex Elder5cbf6f122013-04-11 09:29:48 -05003905 if (incompat & ~RBD_FEATURES_SUPPORTED)
Alex Elderb8f5c6e2012-11-01 08:39:26 -05003906 return -ENXIO;
Alex Elderd8891402012-10-09 13:50:17 -07003907
Alex Elderb1b54022012-07-03 16:01:19 -05003908 *snap_features = le64_to_cpu(features_buf.features);
3909
3910 dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
Alex Elder57385b52013-04-21 12:14:45 -05003911 (unsigned long long)snap_id,
3912 (unsigned long long)*snap_features,
3913 (unsigned long long)le64_to_cpu(features_buf.incompat));
Alex Elderb1b54022012-07-03 16:01:19 -05003914
3915 return 0;
3916}
3917
3918static int rbd_dev_v2_features(struct rbd_device *rbd_dev)
3919{
3920 return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP,
3921 &rbd_dev->header.features);
3922}
3923
Alex Elder86b00e02012-10-25 23:34:42 -05003924static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev)
3925{
3926 struct rbd_spec *parent_spec;
3927 size_t size;
3928 void *reply_buf = NULL;
3929 __le64 snapid;
3930 void *p;
3931 void *end;
Alex Elder642a2532013-05-06 17:40:33 -05003932 u64 pool_id;
Alex Elder86b00e02012-10-25 23:34:42 -05003933 char *image_id;
Alex Elder3b5cf2a2013-05-29 11:18:59 -05003934 u64 snap_id;
Alex Elder86b00e02012-10-25 23:34:42 -05003935 u64 overlap;
Alex Elder86b00e02012-10-25 23:34:42 -05003936 int ret;
3937
3938 parent_spec = rbd_spec_alloc();
3939 if (!parent_spec)
3940 return -ENOMEM;
3941
3942 size = sizeof (__le64) + /* pool_id */
3943 sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */
3944 sizeof (__le64) + /* snap_id */
3945 sizeof (__le64); /* overlap */
3946 reply_buf = kmalloc(size, GFP_KERNEL);
3947 if (!reply_buf) {
3948 ret = -ENOMEM;
3949 goto out_err;
3950 }
3951
3952 snapid = cpu_to_le64(CEPH_NOSNAP);
Alex Elder36be9a72013-01-19 00:30:28 -06003953 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elder86b00e02012-10-25 23:34:42 -05003954 "rbd", "get_parent",
Alex Elder41579762013-04-21 12:14:45 -05003955 &snapid, sizeof (snapid),
Alex Eldere2a58ee2013-04-30 00:44:33 -05003956 reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06003957 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder86b00e02012-10-25 23:34:42 -05003958 if (ret < 0)
3959 goto out_err;
3960
Alex Elder86b00e02012-10-25 23:34:42 -05003961 p = reply_buf;
Alex Elder57385b52013-04-21 12:14:45 -05003962 end = reply_buf + ret;
3963 ret = -ERANGE;
Alex Elder642a2532013-05-06 17:40:33 -05003964 ceph_decode_64_safe(&p, end, pool_id, out_err);
Alex Elder392a9da2013-05-06 17:40:33 -05003965 if (pool_id == CEPH_NOPOOL) {
3966 /*
3967 * Either the parent never existed, or we have
3968 * record of it but the image got flattened so it no
3969 * longer has a parent. When the parent of a
3970 * layered image disappears we immediately set the
3971 * overlap to 0. The effect of this is that all new
3972 * requests will be treated as if the image had no
3973 * parent.
3974 */
3975 if (rbd_dev->parent_overlap) {
3976 rbd_dev->parent_overlap = 0;
3977 smp_mb();
3978 rbd_dev_parent_put(rbd_dev);
3979 pr_info("%s: clone image has been flattened\n",
3980 rbd_dev->disk->disk_name);
3981 }
3982
Alex Elder86b00e02012-10-25 23:34:42 -05003983 goto out; /* No parent? No problem. */
Alex Elder392a9da2013-05-06 17:40:33 -05003984 }
Alex Elder86b00e02012-10-25 23:34:42 -05003985
Alex Elder0903e872012-11-14 12:25:19 -06003986 /* The ceph file layout needs to fit pool id in 32 bits */
3987
3988 ret = -EIO;
Alex Elder642a2532013-05-06 17:40:33 -05003989 if (pool_id > (u64)U32_MAX) {
Alex Elderc0cd10db2013-04-26 09:43:47 -05003990 rbd_warn(NULL, "parent pool id too large (%llu > %u)\n",
Alex Elder642a2532013-05-06 17:40:33 -05003991 (unsigned long long)pool_id, U32_MAX);
Alex Elder57385b52013-04-21 12:14:45 -05003992 goto out_err;
Alex Elderc0cd10db2013-04-26 09:43:47 -05003993 }
Alex Elder0903e872012-11-14 12:25:19 -06003994
Alex Elder979ed482012-11-01 08:39:26 -05003995 image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elder86b00e02012-10-25 23:34:42 -05003996 if (IS_ERR(image_id)) {
3997 ret = PTR_ERR(image_id);
3998 goto out_err;
3999 }
Alex Elder3b5cf2a2013-05-29 11:18:59 -05004000 ceph_decode_64_safe(&p, end, snap_id, out_err);
Alex Elder86b00e02012-10-25 23:34:42 -05004001 ceph_decode_64_safe(&p, end, overlap, out_err);
4002
Alex Elder3b5cf2a2013-05-29 11:18:59 -05004003 /*
4004 * The parent won't change (except when the clone is
4005 * flattened, already handled that). So we only need to
4006 * record the parent spec we have not already done so.
4007 */
4008 if (!rbd_dev->parent_spec) {
4009 parent_spec->pool_id = pool_id;
4010 parent_spec->image_id = image_id;
4011 parent_spec->snap_id = snap_id;
Alex Elder70cf49c2013-05-06 17:40:33 -05004012 rbd_dev->parent_spec = parent_spec;
4013 parent_spec = NULL; /* rbd_dev now owns this */
Alex Elder3b5cf2a2013-05-29 11:18:59 -05004014 }
4015
4016 /*
4017 * We always update the parent overlap. If it's zero we
4018 * treat it specially.
4019 */
4020 rbd_dev->parent_overlap = overlap;
4021 smp_mb();
4022 if (!overlap) {
4023
4024 /* A null parent_spec indicates it's the initial probe */
4025
4026 if (parent_spec) {
4027 /*
4028 * The overlap has become zero, so the clone
4029 * must have been resized down to 0 at some
4030 * point. Treat this the same as a flatten.
4031 */
4032 rbd_dev_parent_put(rbd_dev);
4033 pr_info("%s: clone image now standalone\n",
4034 rbd_dev->disk->disk_name);
4035 } else {
4036 /*
4037 * For the initial probe, if we find the
4038 * overlap is zero we just pretend there was
4039 * no parent image.
4040 */
4041 rbd_warn(rbd_dev, "ignoring parent of "
4042 "clone with overlap 0\n");
4043 }
Alex Elder70cf49c2013-05-06 17:40:33 -05004044 }
Alex Elder86b00e02012-10-25 23:34:42 -05004045out:
4046 ret = 0;
4047out_err:
4048 kfree(reply_buf);
4049 rbd_spec_put(parent_spec);
4050
4051 return ret;
4052}
4053
Alex Eldercc070d52013-04-21 12:14:45 -05004054static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev)
4055{
4056 struct {
4057 __le64 stripe_unit;
4058 __le64 stripe_count;
4059 } __attribute__ ((packed)) striping_info_buf = { 0 };
4060 size_t size = sizeof (striping_info_buf);
4061 void *p;
4062 u64 obj_size;
4063 u64 stripe_unit;
4064 u64 stripe_count;
4065 int ret;
4066
4067 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
4068 "rbd", "get_stripe_unit_count", NULL, 0,
Alex Eldere2a58ee2013-04-30 00:44:33 -05004069 (char *)&striping_info_buf, size);
Alex Eldercc070d52013-04-21 12:14:45 -05004070 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
4071 if (ret < 0)
4072 return ret;
4073 if (ret < size)
4074 return -ERANGE;
4075
4076 /*
4077 * We don't actually support the "fancy striping" feature
4078 * (STRIPINGV2) yet, but if the striping sizes are the
4079 * defaults the behavior is the same as before. So find
4080 * out, and only fail if the image has non-default values.
4081 */
4082 ret = -EINVAL;
4083 obj_size = (u64)1 << rbd_dev->header.obj_order;
4084 p = &striping_info_buf;
4085 stripe_unit = ceph_decode_64(&p);
4086 if (stripe_unit != obj_size) {
4087 rbd_warn(rbd_dev, "unsupported stripe unit "
4088 "(got %llu want %llu)",
4089 stripe_unit, obj_size);
4090 return -EINVAL;
4091 }
4092 stripe_count = ceph_decode_64(&p);
4093 if (stripe_count != 1) {
4094 rbd_warn(rbd_dev, "unsupported stripe count "
4095 "(got %llu want 1)", stripe_count);
4096 return -EINVAL;
4097 }
Alex Elder500d0c02013-04-26 09:43:47 -05004098 rbd_dev->header.stripe_unit = stripe_unit;
4099 rbd_dev->header.stripe_count = stripe_count;
Alex Eldercc070d52013-04-21 12:14:45 -05004100
4101 return 0;
4102}
4103
Alex Elder9e15b772012-10-30 19:40:33 -05004104static char *rbd_dev_image_name(struct rbd_device *rbd_dev)
4105{
4106 size_t image_id_size;
4107 char *image_id;
4108 void *p;
4109 void *end;
4110 size_t size;
4111 void *reply_buf = NULL;
4112 size_t len = 0;
4113 char *image_name = NULL;
4114 int ret;
4115
4116 rbd_assert(!rbd_dev->spec->image_name);
4117
Alex Elder69e7a022012-11-01 08:39:26 -05004118 len = strlen(rbd_dev->spec->image_id);
4119 image_id_size = sizeof (__le32) + len;
Alex Elder9e15b772012-10-30 19:40:33 -05004120 image_id = kmalloc(image_id_size, GFP_KERNEL);
4121 if (!image_id)
4122 return NULL;
4123
4124 p = image_id;
Alex Elder41579762013-04-21 12:14:45 -05004125 end = image_id + image_id_size;
Alex Elder57385b52013-04-21 12:14:45 -05004126 ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len);
Alex Elder9e15b772012-10-30 19:40:33 -05004127
4128 size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX;
4129 reply_buf = kmalloc(size, GFP_KERNEL);
4130 if (!reply_buf)
4131 goto out;
4132
Alex Elder36be9a72013-01-19 00:30:28 -06004133 ret = rbd_obj_method_sync(rbd_dev, RBD_DIRECTORY,
Alex Elder9e15b772012-10-30 19:40:33 -05004134 "rbd", "dir_get_name",
4135 image_id, image_id_size,
Alex Eldere2a58ee2013-04-30 00:44:33 -05004136 reply_buf, size);
Alex Elder9e15b772012-10-30 19:40:33 -05004137 if (ret < 0)
4138 goto out;
4139 p = reply_buf;
Alex Elderf40eb342013-04-25 15:09:42 -05004140 end = reply_buf + ret;
4141
Alex Elder9e15b772012-10-30 19:40:33 -05004142 image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL);
4143 if (IS_ERR(image_name))
4144 image_name = NULL;
4145 else
4146 dout("%s: name is %s len is %zd\n", __func__, image_name, len);
4147out:
4148 kfree(reply_buf);
4149 kfree(image_id);
4150
4151 return image_name;
4152}
4153
Alex Elder2ad3d712013-04-30 00:44:33 -05004154static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
4155{
4156 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
4157 const char *snap_name;
4158 u32 which = 0;
4159
4160 /* Skip over names until we find the one we are looking for */
4161
4162 snap_name = rbd_dev->header.snap_names;
4163 while (which < snapc->num_snaps) {
4164 if (!strcmp(name, snap_name))
4165 return snapc->snaps[which];
4166 snap_name += strlen(snap_name) + 1;
4167 which++;
4168 }
4169 return CEPH_NOSNAP;
4170}
4171
4172static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
4173{
4174 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
4175 u32 which;
4176 bool found = false;
4177 u64 snap_id;
4178
4179 for (which = 0; !found && which < snapc->num_snaps; which++) {
4180 const char *snap_name;
4181
4182 snap_id = snapc->snaps[which];
4183 snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id);
Josh Durginefadc982013-08-29 19:16:42 -07004184 if (IS_ERR(snap_name)) {
4185 /* ignore no-longer existing snapshots */
4186 if (PTR_ERR(snap_name) == -ENOENT)
4187 continue;
4188 else
4189 break;
4190 }
Alex Elder2ad3d712013-04-30 00:44:33 -05004191 found = !strcmp(name, snap_name);
4192 kfree(snap_name);
4193 }
4194 return found ? snap_id : CEPH_NOSNAP;
4195}
4196
4197/*
4198 * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if
4199 * no snapshot by that name is found, or if an error occurs.
4200 */
4201static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
4202{
4203 if (rbd_dev->image_format == 1)
4204 return rbd_v1_snap_id_by_name(rbd_dev, name);
4205
4206 return rbd_v2_snap_id_by_name(rbd_dev, name);
4207}
4208
Alex Elder9e15b772012-10-30 19:40:33 -05004209/*
Alex Elder2e9f7f12013-04-26 09:43:48 -05004210 * When an rbd image has a parent image, it is identified by the
4211 * pool, image, and snapshot ids (not names). This function fills
4212 * in the names for those ids. (It's OK if we can't figure out the
4213 * name for an image id, but the pool and snapshot ids should always
4214 * exist and have names.) All names in an rbd spec are dynamically
4215 * allocated.
Alex Eldere1d42132013-04-25 23:15:08 -05004216 *
4217 * When an image being mapped (not a parent) is probed, we have the
4218 * pool name and pool id, image name and image id, and the snapshot
4219 * name. The only thing we're missing is the snapshot id.
Alex Elder9e15b772012-10-30 19:40:33 -05004220 */
Alex Elder2e9f7f12013-04-26 09:43:48 -05004221static int rbd_dev_spec_update(struct rbd_device *rbd_dev)
Alex Elder9e15b772012-10-30 19:40:33 -05004222{
Alex Elder2e9f7f12013-04-26 09:43:48 -05004223 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
4224 struct rbd_spec *spec = rbd_dev->spec;
4225 const char *pool_name;
4226 const char *image_name;
4227 const char *snap_name;
Alex Elder9e15b772012-10-30 19:40:33 -05004228 int ret;
4229
Alex Eldere1d42132013-04-25 23:15:08 -05004230 /*
4231 * An image being mapped will have the pool name (etc.), but
4232 * we need to look up the snapshot id.
4233 */
Alex Elder2e9f7f12013-04-26 09:43:48 -05004234 if (spec->pool_name) {
4235 if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) {
Alex Elder2ad3d712013-04-30 00:44:33 -05004236 u64 snap_id;
Alex Eldere1d42132013-04-25 23:15:08 -05004237
Alex Elder2ad3d712013-04-30 00:44:33 -05004238 snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name);
4239 if (snap_id == CEPH_NOSNAP)
Alex Eldere1d42132013-04-25 23:15:08 -05004240 return -ENOENT;
Alex Elder2ad3d712013-04-30 00:44:33 -05004241 spec->snap_id = snap_id;
Alex Eldere1d42132013-04-25 23:15:08 -05004242 } else {
Alex Elder2e9f7f12013-04-26 09:43:48 -05004243 spec->snap_id = CEPH_NOSNAP;
Alex Eldere1d42132013-04-25 23:15:08 -05004244 }
4245
4246 return 0;
4247 }
Alex Elder9e15b772012-10-30 19:40:33 -05004248
Alex Elder2e9f7f12013-04-26 09:43:48 -05004249 /* Get the pool name; we have to make our own copy of this */
Alex Elder9e15b772012-10-30 19:40:33 -05004250
Alex Elder2e9f7f12013-04-26 09:43:48 -05004251 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id);
4252 if (!pool_name) {
4253 rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id);
Alex Elder935dc892012-11-01 10:17:15 -05004254 return -EIO;
4255 }
Alex Elder2e9f7f12013-04-26 09:43:48 -05004256 pool_name = kstrdup(pool_name, GFP_KERNEL);
4257 if (!pool_name)
Alex Elder9e15b772012-10-30 19:40:33 -05004258 return -ENOMEM;
4259
4260 /* Fetch the image name; tolerate failure here */
4261
Alex Elder2e9f7f12013-04-26 09:43:48 -05004262 image_name = rbd_dev_image_name(rbd_dev);
4263 if (!image_name)
Alex Elder06ecc6c2012-11-01 10:17:15 -05004264 rbd_warn(rbd_dev, "unable to get image name");
Alex Elder9e15b772012-10-30 19:40:33 -05004265
Alex Elder2e9f7f12013-04-26 09:43:48 -05004266 /* Look up the snapshot name, and make a copy */
Alex Elder9e15b772012-10-30 19:40:33 -05004267
Alex Elder2e9f7f12013-04-26 09:43:48 -05004268 snap_name = rbd_snap_name(rbd_dev, spec->snap_id);
Josh Durginda6a6b62013-09-04 17:57:31 -07004269 if (IS_ERR(snap_name)) {
4270 ret = PTR_ERR(snap_name);
Alex Elder9e15b772012-10-30 19:40:33 -05004271 goto out_err;
Alex Elder2e9f7f12013-04-26 09:43:48 -05004272 }
4273
4274 spec->pool_name = pool_name;
4275 spec->image_name = image_name;
4276 spec->snap_name = snap_name;
Alex Elder9e15b772012-10-30 19:40:33 -05004277
4278 return 0;
4279out_err:
Alex Elder2e9f7f12013-04-26 09:43:48 -05004280 kfree(image_name);
4281 kfree(pool_name);
Alex Elder9e15b772012-10-30 19:40:33 -05004282
4283 return ret;
4284}
4285
Alex Eldercc4a38bd2013-04-30 00:44:33 -05004286static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev)
Alex Elder35d489f2012-07-03 16:01:19 -05004287{
4288 size_t size;
4289 int ret;
4290 void *reply_buf;
4291 void *p;
4292 void *end;
4293 u64 seq;
4294 u32 snap_count;
4295 struct ceph_snap_context *snapc;
4296 u32 i;
4297
4298 /*
4299 * We'll need room for the seq value (maximum snapshot id),
4300 * snapshot count, and array of that many snapshot ids.
4301 * For now we have a fixed upper limit on the number we're
4302 * prepared to receive.
4303 */
4304 size = sizeof (__le64) + sizeof (__le32) +
4305 RBD_MAX_SNAP_COUNT * sizeof (__le64);
4306 reply_buf = kzalloc(size, GFP_KERNEL);
4307 if (!reply_buf)
4308 return -ENOMEM;
4309
Alex Elder36be9a72013-01-19 00:30:28 -06004310 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elder41579762013-04-21 12:14:45 -05004311 "rbd", "get_snapcontext", NULL, 0,
Alex Eldere2a58ee2013-04-30 00:44:33 -05004312 reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06004313 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder35d489f2012-07-03 16:01:19 -05004314 if (ret < 0)
4315 goto out;
4316
Alex Elder35d489f2012-07-03 16:01:19 -05004317 p = reply_buf;
Alex Elder57385b52013-04-21 12:14:45 -05004318 end = reply_buf + ret;
4319 ret = -ERANGE;
Alex Elder35d489f2012-07-03 16:01:19 -05004320 ceph_decode_64_safe(&p, end, seq, out);
4321 ceph_decode_32_safe(&p, end, snap_count, out);
4322
4323 /*
4324 * Make sure the reported number of snapshot ids wouldn't go
4325 * beyond the end of our buffer. But before checking that,
4326 * make sure the computed size of the snapshot context we
4327 * allocate is representable in a size_t.
4328 */
4329 if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
4330 / sizeof (u64)) {
4331 ret = -EINVAL;
4332 goto out;
4333 }
4334 if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
4335 goto out;
Alex Elder468521c2013-04-26 09:43:47 -05004336 ret = 0;
Alex Elder35d489f2012-07-03 16:01:19 -05004337
Alex Elder812164f82013-04-30 00:44:32 -05004338 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
Alex Elder35d489f2012-07-03 16:01:19 -05004339 if (!snapc) {
4340 ret = -ENOMEM;
4341 goto out;
4342 }
Alex Elder35d489f2012-07-03 16:01:19 -05004343 snapc->seq = seq;
Alex Elder35d489f2012-07-03 16:01:19 -05004344 for (i = 0; i < snap_count; i++)
4345 snapc->snaps[i] = ceph_decode_64(&p);
4346
Alex Elder49ece552013-05-06 08:37:00 -05004347 ceph_put_snap_context(rbd_dev->header.snapc);
Alex Elder35d489f2012-07-03 16:01:19 -05004348 rbd_dev->header.snapc = snapc;
4349
4350 dout(" snap context seq = %llu, snap_count = %u\n",
Alex Elder57385b52013-04-21 12:14:45 -05004351 (unsigned long long)seq, (unsigned int)snap_count);
Alex Elder35d489f2012-07-03 16:01:19 -05004352out:
4353 kfree(reply_buf);
4354
Alex Elder57385b52013-04-21 12:14:45 -05004355 return ret;
Alex Elder35d489f2012-07-03 16:01:19 -05004356}
4357
Alex Elder54cac612013-04-30 00:44:33 -05004358static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
4359 u64 snap_id)
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004360{
4361 size_t size;
4362 void *reply_buf;
Alex Elder54cac612013-04-30 00:44:33 -05004363 __le64 snapid;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004364 int ret;
4365 void *p;
4366 void *end;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004367 char *snap_name;
4368
4369 size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN;
4370 reply_buf = kmalloc(size, GFP_KERNEL);
4371 if (!reply_buf)
4372 return ERR_PTR(-ENOMEM);
4373
Alex Elder54cac612013-04-30 00:44:33 -05004374 snapid = cpu_to_le64(snap_id);
Alex Elder36be9a72013-01-19 00:30:28 -06004375 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004376 "rbd", "get_snapshot_name",
Alex Elder54cac612013-04-30 00:44:33 -05004377 &snapid, sizeof (snapid),
Alex Eldere2a58ee2013-04-30 00:44:33 -05004378 reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06004379 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderf40eb342013-04-25 15:09:42 -05004380 if (ret < 0) {
4381 snap_name = ERR_PTR(ret);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004382 goto out;
Alex Elderf40eb342013-04-25 15:09:42 -05004383 }
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004384
4385 p = reply_buf;
Alex Elderf40eb342013-04-25 15:09:42 -05004386 end = reply_buf + ret;
Alex Eldere5c35532012-10-25 23:34:41 -05004387 snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elderf40eb342013-04-25 15:09:42 -05004388 if (IS_ERR(snap_name))
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004389 goto out;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004390
Alex Elderf40eb342013-04-25 15:09:42 -05004391 dout(" snap_id 0x%016llx snap_name = %s\n",
Alex Elder54cac612013-04-30 00:44:33 -05004392 (unsigned long long)snap_id, snap_name);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004393out:
4394 kfree(reply_buf);
4395
Alex Elderf40eb342013-04-25 15:09:42 -05004396 return snap_name;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05004397}
4398
Alex Elder2df3fac2013-05-06 09:51:30 -05004399static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev)
Alex Elder117973f2012-08-31 17:29:55 -05004400{
Alex Elder2df3fac2013-05-06 09:51:30 -05004401 bool first_time = rbd_dev->header.object_prefix == NULL;
Alex Elder117973f2012-08-31 17:29:55 -05004402 int ret;
Alex Elder117973f2012-08-31 17:29:55 -05004403
Josh Durgin1617e402013-06-12 14:43:10 -07004404 ret = rbd_dev_v2_image_size(rbd_dev);
4405 if (ret)
Alex Eldercfbf6372013-05-31 17:40:45 -05004406 return ret;
Josh Durgin1617e402013-06-12 14:43:10 -07004407
Alex Elder2df3fac2013-05-06 09:51:30 -05004408 if (first_time) {
4409 ret = rbd_dev_v2_header_onetime(rbd_dev);
4410 if (ret)
Alex Eldercfbf6372013-05-31 17:40:45 -05004411 return ret;
Alex Elder2df3fac2013-05-06 09:51:30 -05004412 }
4413
Alex Elder642a2532013-05-06 17:40:33 -05004414 /*
4415 * If the image supports layering, get the parent info. We
4416 * need to probe the first time regardless. Thereafter we
4417 * only need to if there's a parent, to see if it has
4418 * disappeared due to the mapped image getting flattened.
4419 */
4420 if (rbd_dev->header.features & RBD_FEATURE_LAYERING &&
4421 (first_time || rbd_dev->parent_spec)) {
4422 bool warn;
4423
4424 ret = rbd_dev_v2_parent_info(rbd_dev);
4425 if (ret)
Alex Eldercfbf6372013-05-31 17:40:45 -05004426 return ret;
Alex Elder642a2532013-05-06 17:40:33 -05004427
4428 /*
4429 * Print a warning if this is the initial probe and
4430 * the image has a parent. Don't print it if the
4431 * image now being probed is itself a parent. We
4432 * can tell at this point because we won't know its
4433 * pool name yet (just its pool id).
4434 */
4435 warn = rbd_dev->parent_spec && rbd_dev->spec->pool_name;
4436 if (first_time && warn)
4437 rbd_warn(rbd_dev, "WARNING: kernel layering "
4438 "is EXPERIMENTAL!");
4439 }
4440
Alex Elder29334ba2013-05-06 07:40:30 -05004441 if (rbd_dev->spec->snap_id == CEPH_NOSNAP)
4442 if (rbd_dev->mapping.size != rbd_dev->header.image_size)
4443 rbd_dev->mapping.size = rbd_dev->header.image_size;
Alex Elder117973f2012-08-31 17:29:55 -05004444
Alex Eldercc4a38bd2013-04-30 00:44:33 -05004445 ret = rbd_dev_v2_snap_context(rbd_dev);
Alex Elder117973f2012-08-31 17:29:55 -05004446 dout("rbd_dev_v2_snap_context returned %d\n", ret);
Alex Elder117973f2012-08-31 17:29:55 -05004447
4448 return ret;
4449}
4450
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004451static int rbd_bus_add_dev(struct rbd_device *rbd_dev)
4452{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004453 struct device *dev;
Alex Eldercd789ab2012-08-30 00:16:38 -05004454 int ret;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004455
Alex Eldercd789ab2012-08-30 00:16:38 -05004456 dev = &rbd_dev->dev;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004457 dev->bus = &rbd_bus_type;
4458 dev->type = &rbd_device_type;
4459 dev->parent = &rbd_root_dev;
Alex Elder200a6a82013-04-28 23:32:34 -05004460 dev->release = rbd_dev_device_release;
Alex Elderde71a292012-07-03 16:01:19 -05004461 dev_set_name(dev, "%d", rbd_dev->dev_id);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004462 ret = device_register(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004463
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004464 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004465}
4466
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004467static void rbd_bus_del_dev(struct rbd_device *rbd_dev)
4468{
4469 device_unregister(&rbd_dev->dev);
4470}
4471
Alex Elder1ddbe942012-01-29 13:57:44 -06004472/*
Alex Elder499afd52012-02-02 08:13:29 -06004473 * Get a unique rbd identifier for the given new rbd_dev, and add
Ilya Dryomovf8a22fc2013-12-13 15:28:57 +02004474 * the rbd_dev to the global list.
Alex Elder1ddbe942012-01-29 13:57:44 -06004475 */
Ilya Dryomovf8a22fc2013-12-13 15:28:57 +02004476static int rbd_dev_id_get(struct rbd_device *rbd_dev)
Alex Elderb7f23c32012-01-29 13:57:43 -06004477{
Ilya Dryomovf8a22fc2013-12-13 15:28:57 +02004478 int new_dev_id;
4479
Ilya Dryomov9b60e702013-12-13 15:28:57 +02004480 new_dev_id = ida_simple_get(&rbd_dev_id_ida,
4481 0, minor_to_rbd_dev_id(1 << MINORBITS),
4482 GFP_KERNEL);
Ilya Dryomovf8a22fc2013-12-13 15:28:57 +02004483 if (new_dev_id < 0)
4484 return new_dev_id;
4485
4486 rbd_dev->dev_id = new_dev_id;
Alex Elder499afd52012-02-02 08:13:29 -06004487
4488 spin_lock(&rbd_dev_list_lock);
4489 list_add_tail(&rbd_dev->node, &rbd_dev_list);
4490 spin_unlock(&rbd_dev_list_lock);
Ilya Dryomovf8a22fc2013-12-13 15:28:57 +02004491
Ilya Dryomov70eebd22013-12-13 15:28:56 +02004492 dout("rbd_dev %p given dev id %d\n", rbd_dev, rbd_dev->dev_id);
Ilya Dryomovf8a22fc2013-12-13 15:28:57 +02004493
4494 return 0;
Alex Elder1ddbe942012-01-29 13:57:44 -06004495}
Alex Elderb7f23c32012-01-29 13:57:43 -06004496
Alex Elder1ddbe942012-01-29 13:57:44 -06004497/*
Alex Elder499afd52012-02-02 08:13:29 -06004498 * Remove an rbd_dev from the global list, and record that its
4499 * identifier is no longer in use.
Alex Elder1ddbe942012-01-29 13:57:44 -06004500 */
Alex Eldere2839302012-08-29 17:11:06 -05004501static void rbd_dev_id_put(struct rbd_device *rbd_dev)
Alex Elder1ddbe942012-01-29 13:57:44 -06004502{
Alex Elder499afd52012-02-02 08:13:29 -06004503 spin_lock(&rbd_dev_list_lock);
4504 list_del_init(&rbd_dev->node);
4505 spin_unlock(&rbd_dev_list_lock);
Alex Elderb7f23c32012-01-29 13:57:43 -06004506
Ilya Dryomovf8a22fc2013-12-13 15:28:57 +02004507 ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id);
4508
4509 dout("rbd_dev %p released dev id %d\n", rbd_dev, rbd_dev->dev_id);
Alex Elderb7f23c32012-01-29 13:57:43 -06004510}
4511
Alex Eldera725f65e2012-02-02 08:13:30 -06004512/*
Alex Eldere28fff262012-02-02 08:13:30 -06004513 * Skips over white space at *buf, and updates *buf to point to the
4514 * first found non-space character (if any). Returns the length of
Alex Elder593a9e72012-02-07 12:03:37 -06004515 * the token (string of non-white space characters) found. Note
4516 * that *buf must be terminated with '\0'.
Alex Eldere28fff262012-02-02 08:13:30 -06004517 */
4518static inline size_t next_token(const char **buf)
4519{
4520 /*
4521 * These are the characters that produce nonzero for
4522 * isspace() in the "C" and "POSIX" locales.
4523 */
4524 const char *spaces = " \f\n\r\t\v";
4525
4526 *buf += strspn(*buf, spaces); /* Find start of token */
4527
4528 return strcspn(*buf, spaces); /* Return token length */
4529}
4530
4531/*
4532 * Finds the next token in *buf, and if the provided token buffer is
4533 * big enough, copies the found token into it. The result, if
Alex Elder593a9e72012-02-07 12:03:37 -06004534 * copied, is guaranteed to be terminated with '\0'. Note that *buf
4535 * must be terminated with '\0' on entry.
Alex Eldere28fff262012-02-02 08:13:30 -06004536 *
4537 * Returns the length of the token found (not including the '\0').
4538 * Return value will be 0 if no token is found, and it will be >=
4539 * token_size if the token would not fit.
4540 *
Alex Elder593a9e72012-02-07 12:03:37 -06004541 * The *buf pointer will be updated to point beyond the end of the
Alex Eldere28fff262012-02-02 08:13:30 -06004542 * found token. Note that this occurs even if the token buffer is
4543 * too small to hold it.
4544 */
4545static inline size_t copy_token(const char **buf,
4546 char *token,
4547 size_t token_size)
4548{
4549 size_t len;
4550
4551 len = next_token(buf);
4552 if (len < token_size) {
4553 memcpy(token, *buf, len);
4554 *(token + len) = '\0';
4555 }
4556 *buf += len;
4557
4558 return len;
4559}
4560
4561/*
Alex Elderea3352f2012-07-09 21:04:23 -05004562 * Finds the next token in *buf, dynamically allocates a buffer big
4563 * enough to hold a copy of it, and copies the token into the new
4564 * buffer. The copy is guaranteed to be terminated with '\0'. Note
4565 * that a duplicate buffer is created even for a zero-length token.
4566 *
4567 * Returns a pointer to the newly-allocated duplicate, or a null
4568 * pointer if memory for the duplicate was not available. If
4569 * the lenp argument is a non-null pointer, the length of the token
4570 * (not including the '\0') is returned in *lenp.
4571 *
4572 * If successful, the *buf pointer will be updated to point beyond
4573 * the end of the found token.
4574 *
4575 * Note: uses GFP_KERNEL for allocation.
4576 */
4577static inline char *dup_token(const char **buf, size_t *lenp)
4578{
4579 char *dup;
4580 size_t len;
4581
4582 len = next_token(buf);
Alex Elder4caf35f2012-11-01 08:39:27 -05004583 dup = kmemdup(*buf, len + 1, GFP_KERNEL);
Alex Elderea3352f2012-07-09 21:04:23 -05004584 if (!dup)
4585 return NULL;
Alex Elderea3352f2012-07-09 21:04:23 -05004586 *(dup + len) = '\0';
4587 *buf += len;
4588
4589 if (lenp)
4590 *lenp = len;
4591
4592 return dup;
4593}
4594
4595/*
Alex Elder859c31d2012-10-25 23:34:42 -05004596 * Parse the options provided for an "rbd add" (i.e., rbd image
4597 * mapping) request. These arrive via a write to /sys/bus/rbd/add,
4598 * and the data written is passed here via a NUL-terminated buffer.
4599 * Returns 0 if successful or an error code otherwise.
Alex Elderd22f76e2012-07-12 10:46:35 -05004600 *
Alex Elder859c31d2012-10-25 23:34:42 -05004601 * The information extracted from these options is recorded in
4602 * the other parameters which return dynamically-allocated
4603 * structures:
4604 * ceph_opts
4605 * The address of a pointer that will refer to a ceph options
4606 * structure. Caller must release the returned pointer using
4607 * ceph_destroy_options() when it is no longer needed.
4608 * rbd_opts
4609 * Address of an rbd options pointer. Fully initialized by
4610 * this function; caller must release with kfree().
4611 * spec
4612 * Address of an rbd image specification pointer. Fully
4613 * initialized by this function based on parsed options.
4614 * Caller must release with rbd_spec_put().
4615 *
4616 * The options passed take this form:
4617 * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
4618 * where:
4619 * <mon_addrs>
4620 * A comma-separated list of one or more monitor addresses.
4621 * A monitor address is an ip address, optionally followed
4622 * by a port number (separated by a colon).
4623 * I.e.: ip1[:port1][,ip2[:port2]...]
4624 * <options>
4625 * A comma-separated list of ceph and/or rbd options.
4626 * <pool_name>
4627 * The name of the rados pool containing the rbd image.
4628 * <image_name>
4629 * The name of the image in that pool to map.
4630 * <snap_id>
4631 * An optional snapshot id. If provided, the mapping will
4632 * present data from the image at the time that snapshot was
4633 * created. The image head is used if no snapshot id is
4634 * provided. Snapshot mappings are always read-only.
Alex Eldera725f65e2012-02-02 08:13:30 -06004635 */
Alex Elder859c31d2012-10-25 23:34:42 -05004636static int rbd_add_parse_args(const char *buf,
Alex Elderdc79b112012-10-25 23:34:41 -05004637 struct ceph_options **ceph_opts,
Alex Elder859c31d2012-10-25 23:34:42 -05004638 struct rbd_options **opts,
4639 struct rbd_spec **rbd_spec)
Alex Eldera725f65e2012-02-02 08:13:30 -06004640{
Alex Elderd22f76e2012-07-12 10:46:35 -05004641 size_t len;
Alex Elder859c31d2012-10-25 23:34:42 -05004642 char *options;
Alex Elder0ddebc02012-10-25 23:34:41 -05004643 const char *mon_addrs;
Alex Elderecb4dc22013-04-26 09:43:47 -05004644 char *snap_name;
Alex Elder0ddebc02012-10-25 23:34:41 -05004645 size_t mon_addrs_size;
Alex Elder859c31d2012-10-25 23:34:42 -05004646 struct rbd_spec *spec = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05004647 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05004648 struct ceph_options *copts;
Alex Elderdc79b112012-10-25 23:34:41 -05004649 int ret;
Alex Eldere28fff262012-02-02 08:13:30 -06004650
4651 /* The first four tokens are required */
4652
Alex Elder7ef32142012-02-02 08:13:30 -06004653 len = next_token(&buf);
Alex Elder4fb5d6712012-11-01 10:17:15 -05004654 if (!len) {
4655 rbd_warn(NULL, "no monitor address(es) provided");
4656 return -EINVAL;
4657 }
Alex Elder0ddebc02012-10-25 23:34:41 -05004658 mon_addrs = buf;
Alex Elderf28e5652012-10-25 23:34:41 -05004659 mon_addrs_size = len + 1;
Alex Elder7ef32142012-02-02 08:13:30 -06004660 buf += len;
Alex Eldera725f65e2012-02-02 08:13:30 -06004661
Alex Elderdc79b112012-10-25 23:34:41 -05004662 ret = -EINVAL;
Alex Elderf28e5652012-10-25 23:34:41 -05004663 options = dup_token(&buf, NULL);
4664 if (!options)
Alex Elderdc79b112012-10-25 23:34:41 -05004665 return -ENOMEM;
Alex Elder4fb5d6712012-11-01 10:17:15 -05004666 if (!*options) {
4667 rbd_warn(NULL, "no options provided");
4668 goto out_err;
4669 }
Alex Eldera725f65e2012-02-02 08:13:30 -06004670
Alex Elder859c31d2012-10-25 23:34:42 -05004671 spec = rbd_spec_alloc();
4672 if (!spec)
Alex Elderf28e5652012-10-25 23:34:41 -05004673 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05004674
4675 spec->pool_name = dup_token(&buf, NULL);
4676 if (!spec->pool_name)
4677 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05004678 if (!*spec->pool_name) {
4679 rbd_warn(NULL, "no pool name provided");
4680 goto out_err;
4681 }
Alex Eldere28fff262012-02-02 08:13:30 -06004682
Alex Elder69e7a022012-11-01 08:39:26 -05004683 spec->image_name = dup_token(&buf, NULL);
Alex Elder859c31d2012-10-25 23:34:42 -05004684 if (!spec->image_name)
Alex Elderf28e5652012-10-25 23:34:41 -05004685 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05004686 if (!*spec->image_name) {
4687 rbd_warn(NULL, "no image name provided");
4688 goto out_err;
4689 }
Alex Eldere28fff262012-02-02 08:13:30 -06004690
Alex Elderf28e5652012-10-25 23:34:41 -05004691 /*
4692 * Snapshot name is optional; default is to use "-"
4693 * (indicating the head/no snapshot).
4694 */
Alex Elder3feeb8942012-08-31 17:29:52 -05004695 len = next_token(&buf);
Alex Elder820a5f32012-07-09 21:04:24 -05004696 if (!len) {
Alex Elder3feeb8942012-08-31 17:29:52 -05004697 buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
4698 len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
Alex Elderf28e5652012-10-25 23:34:41 -05004699 } else if (len > RBD_MAX_SNAP_NAME_LEN) {
Alex Elderdc79b112012-10-25 23:34:41 -05004700 ret = -ENAMETOOLONG;
Alex Elderf28e5652012-10-25 23:34:41 -05004701 goto out_err;
Alex Elder849b4262012-07-09 21:04:24 -05004702 }
Alex Elderecb4dc22013-04-26 09:43:47 -05004703 snap_name = kmemdup(buf, len + 1, GFP_KERNEL);
4704 if (!snap_name)
Alex Elderf28e5652012-10-25 23:34:41 -05004705 goto out_mem;
Alex Elderecb4dc22013-04-26 09:43:47 -05004706 *(snap_name + len) = '\0';
4707 spec->snap_name = snap_name;
Alex Eldere5c35532012-10-25 23:34:41 -05004708
Alex Elder0ddebc02012-10-25 23:34:41 -05004709 /* Initialize all rbd options to the defaults */
Alex Eldere28fff262012-02-02 08:13:30 -06004710
Alex Elder4e9afeb2012-10-25 23:34:41 -05004711 rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL);
4712 if (!rbd_opts)
4713 goto out_mem;
4714
4715 rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
Alex Elderd22f76e2012-07-12 10:46:35 -05004716
Alex Elder859c31d2012-10-25 23:34:42 -05004717 copts = ceph_parse_options(options, mon_addrs,
Alex Elder0ddebc02012-10-25 23:34:41 -05004718 mon_addrs + mon_addrs_size - 1,
Alex Elder4e9afeb2012-10-25 23:34:41 -05004719 parse_rbd_opts_token, rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05004720 if (IS_ERR(copts)) {
4721 ret = PTR_ERR(copts);
Alex Elderdc79b112012-10-25 23:34:41 -05004722 goto out_err;
4723 }
Alex Elder859c31d2012-10-25 23:34:42 -05004724 kfree(options);
4725
4726 *ceph_opts = copts;
Alex Elder4e9afeb2012-10-25 23:34:41 -05004727 *opts = rbd_opts;
Alex Elder859c31d2012-10-25 23:34:42 -05004728 *rbd_spec = spec;
Alex Elder0ddebc02012-10-25 23:34:41 -05004729
Alex Elderdc79b112012-10-25 23:34:41 -05004730 return 0;
Alex Elderf28e5652012-10-25 23:34:41 -05004731out_mem:
Alex Elderdc79b112012-10-25 23:34:41 -05004732 ret = -ENOMEM;
Alex Elderd22f76e2012-07-12 10:46:35 -05004733out_err:
Alex Elder859c31d2012-10-25 23:34:42 -05004734 kfree(rbd_opts);
4735 rbd_spec_put(spec);
Alex Elderf28e5652012-10-25 23:34:41 -05004736 kfree(options);
Alex Elderd22f76e2012-07-12 10:46:35 -05004737
Alex Elderdc79b112012-10-25 23:34:41 -05004738 return ret;
Alex Eldera725f65e2012-02-02 08:13:30 -06004739}
4740
Alex Elder589d30e2012-07-10 20:30:11 -05004741/*
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04004742 * Return pool id (>= 0) or a negative error code.
4743 */
4744static int rbd_add_get_pool_id(struct rbd_client *rbdc, const char *pool_name)
4745{
4746 u64 newest_epoch;
4747 unsigned long timeout = rbdc->client->options->mount_timeout * HZ;
4748 int tries = 0;
4749 int ret;
4750
4751again:
4752 ret = ceph_pg_poolid_by_name(rbdc->client->osdc.osdmap, pool_name);
4753 if (ret == -ENOENT && tries++ < 1) {
4754 ret = ceph_monc_do_get_version(&rbdc->client->monc, "osdmap",
4755 &newest_epoch);
4756 if (ret < 0)
4757 return ret;
4758
4759 if (rbdc->client->osdc.osdmap->epoch < newest_epoch) {
4760 ceph_monc_request_next_osdmap(&rbdc->client->monc);
4761 (void) ceph_monc_wait_osdmap(&rbdc->client->monc,
4762 newest_epoch, timeout);
4763 goto again;
4764 } else {
4765 /* the osdmap we have is new enough */
4766 return -ENOENT;
4767 }
4768 }
4769
4770 return ret;
4771}
4772
4773/*
Alex Elder589d30e2012-07-10 20:30:11 -05004774 * An rbd format 2 image has a unique identifier, distinct from the
4775 * name given to it by the user. Internally, that identifier is
4776 * what's used to specify the names of objects related to the image.
4777 *
4778 * A special "rbd id" object is used to map an rbd image name to its
4779 * id. If that object doesn't exist, then there is no v2 rbd image
4780 * with the supplied name.
4781 *
4782 * This function will record the given rbd_dev's image_id field if
4783 * it can be determined, and in that case will return 0. If any
4784 * errors occur a negative errno will be returned and the rbd_dev's
4785 * image_id field will be unchanged (and should be NULL).
4786 */
4787static int rbd_dev_image_id(struct rbd_device *rbd_dev)
4788{
4789 int ret;
4790 size_t size;
4791 char *object_name;
4792 void *response;
Alex Elderc0fba362013-04-25 23:15:08 -05004793 char *image_id;
Alex Elder2f82ee52012-10-30 19:40:33 -05004794
Alex Elder589d30e2012-07-10 20:30:11 -05004795 /*
Alex Elder2c0d0a12012-10-30 19:40:33 -05004796 * When probing a parent image, the image id is already
4797 * known (and the image name likely is not). There's no
Alex Elderc0fba362013-04-25 23:15:08 -05004798 * need to fetch the image id again in this case. We
4799 * do still need to set the image format though.
Alex Elder2c0d0a12012-10-30 19:40:33 -05004800 */
Alex Elderc0fba362013-04-25 23:15:08 -05004801 if (rbd_dev->spec->image_id) {
4802 rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1;
4803
Alex Elder2c0d0a12012-10-30 19:40:33 -05004804 return 0;
Alex Elderc0fba362013-04-25 23:15:08 -05004805 }
Alex Elder2c0d0a12012-10-30 19:40:33 -05004806
4807 /*
Alex Elder589d30e2012-07-10 20:30:11 -05004808 * First, see if the format 2 image id file exists, and if
4809 * so, get the image's persistent id from it.
4810 */
Alex Elder69e7a022012-11-01 08:39:26 -05004811 size = sizeof (RBD_ID_PREFIX) + strlen(rbd_dev->spec->image_name);
Alex Elder589d30e2012-07-10 20:30:11 -05004812 object_name = kmalloc(size, GFP_NOIO);
4813 if (!object_name)
4814 return -ENOMEM;
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004815 sprintf(object_name, "%s%s", RBD_ID_PREFIX, rbd_dev->spec->image_name);
Alex Elder589d30e2012-07-10 20:30:11 -05004816 dout("rbd id object name is %s\n", object_name);
4817
4818 /* Response will be an encoded string, which includes a length */
4819
4820 size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
4821 response = kzalloc(size, GFP_NOIO);
4822 if (!response) {
4823 ret = -ENOMEM;
4824 goto out;
4825 }
4826
Alex Elderc0fba362013-04-25 23:15:08 -05004827 /* If it doesn't exist we'll assume it's a format 1 image */
4828
Alex Elder36be9a72013-01-19 00:30:28 -06004829 ret = rbd_obj_method_sync(rbd_dev, object_name,
Alex Elder41579762013-04-21 12:14:45 -05004830 "rbd", "get_id", NULL, 0,
Alex Eldere2a58ee2013-04-30 00:44:33 -05004831 response, RBD_IMAGE_ID_LEN_MAX);
Alex Elder36be9a72013-01-19 00:30:28 -06004832 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderc0fba362013-04-25 23:15:08 -05004833 if (ret == -ENOENT) {
4834 image_id = kstrdup("", GFP_KERNEL);
4835 ret = image_id ? 0 : -ENOMEM;
4836 if (!ret)
4837 rbd_dev->image_format = 1;
4838 } else if (ret > sizeof (__le32)) {
4839 void *p = response;
Alex Elder589d30e2012-07-10 20:30:11 -05004840
Alex Elderc0fba362013-04-25 23:15:08 -05004841 image_id = ceph_extract_encoded_string(&p, p + ret,
Alex Elder979ed482012-11-01 08:39:26 -05004842 NULL, GFP_NOIO);
Duan Jiong461f7582014-04-11 16:38:12 +08004843 ret = PTR_ERR_OR_ZERO(image_id);
Alex Elderc0fba362013-04-25 23:15:08 -05004844 if (!ret)
4845 rbd_dev->image_format = 2;
Alex Elder589d30e2012-07-10 20:30:11 -05004846 } else {
Alex Elderc0fba362013-04-25 23:15:08 -05004847 ret = -EINVAL;
4848 }
4849
4850 if (!ret) {
4851 rbd_dev->spec->image_id = image_id;
4852 dout("image_id is %s\n", image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05004853 }
4854out:
4855 kfree(response);
4856 kfree(object_name);
4857
4858 return ret;
4859}
4860
Alex Elder3abef3b2013-05-13 20:35:37 -05004861/*
4862 * Undo whatever state changes are made by v1 or v2 header info
4863 * call.
4864 */
Alex Elder6fd48b32013-04-28 23:32:34 -05004865static void rbd_dev_unprobe(struct rbd_device *rbd_dev)
4866{
4867 struct rbd_image_header *header;
4868
Alex Elder392a9da2013-05-06 17:40:33 -05004869 /* Drop parent reference unless it's already been done (or none) */
4870
4871 if (rbd_dev->parent_overlap)
4872 rbd_dev_parent_put(rbd_dev);
Alex Elder6fd48b32013-04-28 23:32:34 -05004873
4874 /* Free dynamic fields from the header, then zero it out */
4875
4876 header = &rbd_dev->header;
Alex Elder812164f82013-04-30 00:44:32 -05004877 ceph_put_snap_context(header->snapc);
Alex Elder6fd48b32013-04-28 23:32:34 -05004878 kfree(header->snap_sizes);
4879 kfree(header->snap_names);
4880 kfree(header->object_prefix);
4881 memset(header, 0, sizeof (*header));
4882}
4883
Alex Elder2df3fac2013-05-06 09:51:30 -05004884static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev)
Alex Eldera30b71b2012-07-10 20:30:11 -05004885{
4886 int ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05004887
Alex Elder1e130192012-07-03 16:01:19 -05004888 ret = rbd_dev_v2_object_prefix(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05004889 if (ret)
Alex Elder1e130192012-07-03 16:01:19 -05004890 goto out_err;
Alex Elderb1b54022012-07-03 16:01:19 -05004891
Alex Elder2df3fac2013-05-06 09:51:30 -05004892 /*
4893 * Get the and check features for the image. Currently the
4894 * features are assumed to never change.
4895 */
Alex Elderb1b54022012-07-03 16:01:19 -05004896 ret = rbd_dev_v2_features(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05004897 if (ret)
Alex Elderb1b54022012-07-03 16:01:19 -05004898 goto out_err;
Alex Elder35d489f2012-07-03 16:01:19 -05004899
Alex Eldercc070d52013-04-21 12:14:45 -05004900 /* If the image supports fancy striping, get its parameters */
4901
4902 if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) {
4903 ret = rbd_dev_v2_striping_info(rbd_dev);
4904 if (ret < 0)
4905 goto out_err;
4906 }
Alex Elder2df3fac2013-05-06 09:51:30 -05004907 /* No support for crypto and compression type format 2 images */
Alex Eldera30b71b2012-07-10 20:30:11 -05004908
Alex Elder35152972012-08-31 17:29:55 -05004909 return 0;
Alex Elder9d475de2012-07-03 16:01:19 -05004910out_err:
Alex Elder642a2532013-05-06 17:40:33 -05004911 rbd_dev->header.features = 0;
Alex Elder1e130192012-07-03 16:01:19 -05004912 kfree(rbd_dev->header.object_prefix);
4913 rbd_dev->header.object_prefix = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05004914
4915 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05004916}
4917
Alex Elder124afba2013-04-26 15:44:36 -05004918static int rbd_dev_probe_parent(struct rbd_device *rbd_dev)
Alex Elder83a06262012-10-30 15:47:17 -05004919{
Alex Elder2f82ee52012-10-30 19:40:33 -05004920 struct rbd_device *parent = NULL;
Alex Elder124afba2013-04-26 15:44:36 -05004921 struct rbd_spec *parent_spec;
4922 struct rbd_client *rbdc;
4923 int ret;
4924
4925 if (!rbd_dev->parent_spec)
4926 return 0;
4927 /*
4928 * We need to pass a reference to the client and the parent
4929 * spec when creating the parent rbd_dev. Images related by
4930 * parent/child relationships always share both.
4931 */
4932 parent_spec = rbd_spec_get(rbd_dev->parent_spec);
4933 rbdc = __rbd_get_client(rbd_dev->rbd_client);
4934
4935 ret = -ENOMEM;
4936 parent = rbd_dev_create(rbdc, parent_spec);
4937 if (!parent)
4938 goto out_err;
4939
Alex Elder1f3ef782013-05-06 17:40:33 -05004940 ret = rbd_dev_image_probe(parent, false);
Alex Elder124afba2013-04-26 15:44:36 -05004941 if (ret < 0)
4942 goto out_err;
4943 rbd_dev->parent = parent;
Alex Eldera2acd002013-05-08 22:50:04 -05004944 atomic_set(&rbd_dev->parent_ref, 1);
Alex Elder124afba2013-04-26 15:44:36 -05004945
4946 return 0;
4947out_err:
4948 if (parent) {
Alex Elderfb65d2282013-05-08 22:50:04 -05004949 rbd_dev_unparent(rbd_dev);
Alex Elder124afba2013-04-26 15:44:36 -05004950 kfree(rbd_dev->header_name);
4951 rbd_dev_destroy(parent);
4952 } else {
4953 rbd_put_client(rbdc);
4954 rbd_spec_put(parent_spec);
4955 }
4956
4957 return ret;
4958}
4959
Alex Elder200a6a82013-04-28 23:32:34 -05004960static int rbd_dev_device_setup(struct rbd_device *rbd_dev)
Alex Elder124afba2013-04-26 15:44:36 -05004961{
Alex Elder83a06262012-10-30 15:47:17 -05004962 int ret;
Alex Elder83a06262012-10-30 15:47:17 -05004963
Ilya Dryomovf8a22fc2013-12-13 15:28:57 +02004964 /* Get an id and fill in device name. */
Alex Elder83a06262012-10-30 15:47:17 -05004965
Ilya Dryomovf8a22fc2013-12-13 15:28:57 +02004966 ret = rbd_dev_id_get(rbd_dev);
4967 if (ret)
4968 return ret;
4969
Alex Elder83a06262012-10-30 15:47:17 -05004970 BUILD_BUG_ON(DEV_NAME_LEN
4971 < sizeof (RBD_DRV_NAME) + MAX_INT_FORMAT_WIDTH);
4972 sprintf(rbd_dev->name, "%s%d", RBD_DRV_NAME, rbd_dev->dev_id);
4973
Ilya Dryomov9b60e702013-12-13 15:28:57 +02004974 /* Record our major and minor device numbers. */
Alex Elder83a06262012-10-30 15:47:17 -05004975
Ilya Dryomov9b60e702013-12-13 15:28:57 +02004976 if (!single_major) {
4977 ret = register_blkdev(0, rbd_dev->name);
4978 if (ret < 0)
4979 goto err_out_id;
4980
4981 rbd_dev->major = ret;
4982 rbd_dev->minor = 0;
4983 } else {
4984 rbd_dev->major = rbd_major;
4985 rbd_dev->minor = rbd_dev_id_to_minor(rbd_dev->dev_id);
4986 }
Alex Elder83a06262012-10-30 15:47:17 -05004987
4988 /* Set up the blkdev mapping. */
4989
4990 ret = rbd_init_disk(rbd_dev);
4991 if (ret)
4992 goto err_out_blkdev;
4993
Alex Elderf35a4de2013-05-06 09:51:29 -05004994 ret = rbd_dev_mapping_set(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05004995 if (ret)
4996 goto err_out_disk;
Alex Elderf35a4de2013-05-06 09:51:29 -05004997 set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
4998
4999 ret = rbd_bus_add_dev(rbd_dev);
5000 if (ret)
5001 goto err_out_mapping;
Alex Elder83a06262012-10-30 15:47:17 -05005002
Alex Elder83a06262012-10-30 15:47:17 -05005003 /* Everything's ready. Announce the disk to the world. */
5004
Alex Elder129b79d2013-04-26 15:44:36 -05005005 set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
Alex Elder83a06262012-10-30 15:47:17 -05005006 add_disk(rbd_dev->disk);
5007
5008 pr_info("%s: added with size 0x%llx\n", rbd_dev->disk->disk_name,
5009 (unsigned long long) rbd_dev->mapping.size);
5010
5011 return ret;
Alex Elder2f82ee52012-10-30 19:40:33 -05005012
Alex Elderf35a4de2013-05-06 09:51:29 -05005013err_out_mapping:
5014 rbd_dev_mapping_clear(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05005015err_out_disk:
5016 rbd_free_disk(rbd_dev);
5017err_out_blkdev:
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005018 if (!single_major)
5019 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Alex Elder83a06262012-10-30 15:47:17 -05005020err_out_id:
5021 rbd_dev_id_put(rbd_dev);
Alex Elderd1cf5782013-04-27 09:59:30 -05005022 rbd_dev_mapping_clear(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05005023
5024 return ret;
5025}
5026
Alex Elder332bb122013-04-27 09:59:30 -05005027static int rbd_dev_header_name(struct rbd_device *rbd_dev)
5028{
5029 struct rbd_spec *spec = rbd_dev->spec;
5030 size_t size;
5031
5032 /* Record the header object name for this rbd image. */
5033
5034 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
5035
5036 if (rbd_dev->image_format == 1)
5037 size = strlen(spec->image_name) + sizeof (RBD_SUFFIX);
5038 else
5039 size = sizeof (RBD_HEADER_PREFIX) + strlen(spec->image_id);
5040
5041 rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
5042 if (!rbd_dev->header_name)
5043 return -ENOMEM;
5044
5045 if (rbd_dev->image_format == 1)
5046 sprintf(rbd_dev->header_name, "%s%s",
5047 spec->image_name, RBD_SUFFIX);
5048 else
5049 sprintf(rbd_dev->header_name, "%s%s",
5050 RBD_HEADER_PREFIX, spec->image_id);
5051 return 0;
5052}
5053
Alex Elder200a6a82013-04-28 23:32:34 -05005054static void rbd_dev_image_release(struct rbd_device *rbd_dev)
5055{
Alex Elder6fd48b32013-04-28 23:32:34 -05005056 rbd_dev_unprobe(rbd_dev);
Alex Elder200a6a82013-04-28 23:32:34 -05005057 kfree(rbd_dev->header_name);
Alex Elder6fd48b32013-04-28 23:32:34 -05005058 rbd_dev->header_name = NULL;
5059 rbd_dev->image_format = 0;
5060 kfree(rbd_dev->spec->image_id);
5061 rbd_dev->spec->image_id = NULL;
5062
Alex Elder200a6a82013-04-28 23:32:34 -05005063 rbd_dev_destroy(rbd_dev);
5064}
5065
Alex Eldera30b71b2012-07-10 20:30:11 -05005066/*
5067 * Probe for the existence of the header object for the given rbd
Alex Elder1f3ef782013-05-06 17:40:33 -05005068 * device. If this image is the one being mapped (i.e., not a
5069 * parent), initiate a watch on its header object before using that
5070 * object to get detailed information about the rbd image.
Alex Eldera30b71b2012-07-10 20:30:11 -05005071 */
Alex Elder1f3ef782013-05-06 17:40:33 -05005072static int rbd_dev_image_probe(struct rbd_device *rbd_dev, bool mapping)
Alex Eldera30b71b2012-07-10 20:30:11 -05005073{
5074 int ret;
5075
5076 /*
Alex Elder3abef3b2013-05-13 20:35:37 -05005077 * Get the id from the image id object. Unless there's an
5078 * error, rbd_dev->spec->image_id will be filled in with
5079 * a dynamically-allocated string, and rbd_dev->image_format
5080 * will be set to either 1 or 2.
Alex Eldera30b71b2012-07-10 20:30:11 -05005081 */
5082 ret = rbd_dev_image_id(rbd_dev);
5083 if (ret)
Alex Elderc0fba362013-04-25 23:15:08 -05005084 return ret;
5085 rbd_assert(rbd_dev->spec->image_id);
5086 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
5087
Alex Elder332bb122013-04-27 09:59:30 -05005088 ret = rbd_dev_header_name(rbd_dev);
5089 if (ret)
5090 goto err_out_format;
5091
Alex Elder1f3ef782013-05-06 17:40:33 -05005092 if (mapping) {
Ilya Dryomovfca27062013-12-16 18:02:40 +02005093 ret = rbd_dev_header_watch_sync(rbd_dev);
Alex Elder1f3ef782013-05-06 17:40:33 -05005094 if (ret)
5095 goto out_header_name;
5096 }
Alex Elderb644de22013-04-27 09:59:31 -05005097
Alex Elderc0fba362013-04-25 23:15:08 -05005098 if (rbd_dev->image_format == 1)
Alex Elder99a41eb2013-05-06 09:51:30 -05005099 ret = rbd_dev_v1_header_info(rbd_dev);
Alex Eldera30b71b2012-07-10 20:30:11 -05005100 else
Alex Elder2df3fac2013-05-06 09:51:30 -05005101 ret = rbd_dev_v2_header_info(rbd_dev);
Alex Elder5655c4d2013-04-25 23:15:08 -05005102 if (ret)
Alex Elderb644de22013-04-27 09:59:31 -05005103 goto err_out_watch;
Alex Elder83a06262012-10-30 15:47:17 -05005104
Alex Elder9bb81c92013-04-27 09:59:30 -05005105 ret = rbd_dev_spec_update(rbd_dev);
5106 if (ret)
Alex Elder33dca392013-04-30 00:44:33 -05005107 goto err_out_probe;
Alex Elder9bb81c92013-04-27 09:59:30 -05005108
5109 ret = rbd_dev_probe_parent(rbd_dev);
Alex Elder30d60ba2013-05-06 09:51:30 -05005110 if (ret)
5111 goto err_out_probe;
Alex Elder83a06262012-10-30 15:47:17 -05005112
Alex Elder30d60ba2013-05-06 09:51:30 -05005113 dout("discovered format %u image, header name is %s\n",
5114 rbd_dev->image_format, rbd_dev->header_name);
5115
5116 return 0;
Alex Elder6fd48b32013-04-28 23:32:34 -05005117err_out_probe:
5118 rbd_dev_unprobe(rbd_dev);
Alex Elderb644de22013-04-27 09:59:31 -05005119err_out_watch:
Ilya Dryomovfca27062013-12-16 18:02:40 +02005120 if (mapping)
5121 rbd_dev_header_unwatch_sync(rbd_dev);
Alex Elder332bb122013-04-27 09:59:30 -05005122out_header_name:
5123 kfree(rbd_dev->header_name);
5124 rbd_dev->header_name = NULL;
5125err_out_format:
5126 rbd_dev->image_format = 0;
Alex Elder5655c4d2013-04-25 23:15:08 -05005127 kfree(rbd_dev->spec->image_id);
5128 rbd_dev->spec->image_id = NULL;
5129
5130 dout("probe failed, returning %d\n", ret);
5131
5132 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05005133}
5134
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005135static ssize_t do_rbd_add(struct bus_type *bus,
5136 const char *buf,
5137 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005138{
Alex Eldercb8627c2012-07-09 21:04:23 -05005139 struct rbd_device *rbd_dev = NULL;
Alex Elderdc79b112012-10-25 23:34:41 -05005140 struct ceph_options *ceph_opts = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05005141 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05005142 struct rbd_spec *spec = NULL;
Alex Elder9d3997f2012-10-25 23:34:42 -05005143 struct rbd_client *rbdc;
Alex Elder51344a32013-05-06 07:40:30 -05005144 bool read_only;
Alex Elder27cc2592012-02-02 08:13:30 -06005145 int rc = -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005146
5147 if (!try_module_get(THIS_MODULE))
5148 return -ENODEV;
5149
Alex Eldera725f65e2012-02-02 08:13:30 -06005150 /* parse add command */
Alex Elder859c31d2012-10-25 23:34:42 -05005151 rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec);
Alex Elderdc79b112012-10-25 23:34:41 -05005152 if (rc < 0)
Alex Elderbd4ba652012-10-25 23:34:42 -05005153 goto err_out_module;
Alex Elder51344a32013-05-06 07:40:30 -05005154 read_only = rbd_opts->read_only;
5155 kfree(rbd_opts);
5156 rbd_opts = NULL; /* done with this */
Alex Eldera725f65e2012-02-02 08:13:30 -06005157
Alex Elder9d3997f2012-10-25 23:34:42 -05005158 rbdc = rbd_get_client(ceph_opts);
5159 if (IS_ERR(rbdc)) {
5160 rc = PTR_ERR(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05005161 goto err_out_args;
Alex Elder9d3997f2012-10-25 23:34:42 -05005162 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005163
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005164 /* pick the pool */
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005165 rc = rbd_add_get_pool_id(rbdc, spec->pool_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005166 if (rc < 0)
5167 goto err_out_client;
Alex Elderc0cd10db2013-04-26 09:43:47 -05005168 spec->pool_id = (u64)rc;
Alex Elder859c31d2012-10-25 23:34:42 -05005169
Alex Elder0903e872012-11-14 12:25:19 -06005170 /* The ceph file layout needs to fit pool id in 32 bits */
5171
Alex Elderc0cd10db2013-04-26 09:43:47 -05005172 if (spec->pool_id > (u64)U32_MAX) {
5173 rbd_warn(NULL, "pool id too large (%llu > %u)\n",
5174 (unsigned long long)spec->pool_id, U32_MAX);
Alex Elder0903e872012-11-14 12:25:19 -06005175 rc = -EIO;
5176 goto err_out_client;
5177 }
5178
Alex Elderc53d5892012-10-25 23:34:42 -05005179 rbd_dev = rbd_dev_create(rbdc, spec);
Alex Elderbd4ba652012-10-25 23:34:42 -05005180 if (!rbd_dev)
5181 goto err_out_client;
Alex Elderc53d5892012-10-25 23:34:42 -05005182 rbdc = NULL; /* rbd_dev now owns this */
5183 spec = NULL; /* rbd_dev now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005184
Alex Elder1f3ef782013-05-06 17:40:33 -05005185 rc = rbd_dev_image_probe(rbd_dev, true);
Alex Eldera30b71b2012-07-10 20:30:11 -05005186 if (rc < 0)
Alex Elderc53d5892012-10-25 23:34:42 -05005187 goto err_out_rbd_dev;
Alex Elder05fd6f62012-08-29 17:11:07 -05005188
Alex Elder7ce4eef2013-05-06 17:40:33 -05005189 /* If we are mapping a snapshot it must be marked read-only */
5190
5191 if (rbd_dev->spec->snap_id != CEPH_NOSNAP)
5192 read_only = true;
5193 rbd_dev->mapping.read_only = read_only;
5194
Alex Elderb536f692013-04-28 23:32:34 -05005195 rc = rbd_dev_device_setup(rbd_dev);
Alex Elder3abef3b2013-05-13 20:35:37 -05005196 if (rc) {
Ilya Dryomove37180c2013-12-16 18:02:41 +02005197 /*
5198 * rbd_dev_header_unwatch_sync() can't be moved into
5199 * rbd_dev_image_release() without refactoring, see
5200 * commit 1f3ef78861ac.
5201 */
5202 rbd_dev_header_unwatch_sync(rbd_dev);
Alex Elder3abef3b2013-05-13 20:35:37 -05005203 rbd_dev_image_release(rbd_dev);
5204 goto err_out_module;
5205 }
Alex Elderb536f692013-04-28 23:32:34 -05005206
Alex Elder3abef3b2013-05-13 20:35:37 -05005207 return count;
5208
Alex Elderc53d5892012-10-25 23:34:42 -05005209err_out_rbd_dev:
5210 rbd_dev_destroy(rbd_dev);
Alex Elderbd4ba652012-10-25 23:34:42 -05005211err_out_client:
Alex Elder9d3997f2012-10-25 23:34:42 -05005212 rbd_put_client(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05005213err_out_args:
Alex Elder859c31d2012-10-25 23:34:42 -05005214 rbd_spec_put(spec);
Alex Elderbd4ba652012-10-25 23:34:42 -05005215err_out_module:
5216 module_put(THIS_MODULE);
Alex Elder27cc2592012-02-02 08:13:30 -06005217
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005218 dout("Error adding device %s\n", buf);
Alex Elder27cc2592012-02-02 08:13:30 -06005219
Alex Elderc0cd10db2013-04-26 09:43:47 -05005220 return (ssize_t)rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005221}
5222
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005223static ssize_t rbd_add(struct bus_type *bus,
5224 const char *buf,
5225 size_t count)
5226{
5227 if (single_major)
5228 return -EINVAL;
5229
5230 return do_rbd_add(bus, buf, count);
5231}
5232
5233static ssize_t rbd_add_single_major(struct bus_type *bus,
5234 const char *buf,
5235 size_t count)
5236{
5237 return do_rbd_add(bus, buf, count);
5238}
5239
Alex Elder200a6a82013-04-28 23:32:34 -05005240static void rbd_dev_device_release(struct device *dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005241{
Alex Elder593a9e72012-02-07 12:03:37 -06005242 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005243
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005244 rbd_free_disk(rbd_dev);
Alex Elder200a6a82013-04-28 23:32:34 -05005245 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
Alex Elder6d80b132013-05-06 07:40:30 -05005246 rbd_dev_mapping_clear(rbd_dev);
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005247 if (!single_major)
5248 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Alex Eldere2839302012-08-29 17:11:06 -05005249 rbd_dev_id_put(rbd_dev);
Alex Elderd1cf5782013-04-27 09:59:30 -05005250 rbd_dev_mapping_clear(rbd_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005251}
5252
Alex Elder05a46af2013-04-26 15:44:36 -05005253static void rbd_dev_remove_parent(struct rbd_device *rbd_dev)
5254{
Alex Elderad945fc2013-04-26 15:44:36 -05005255 while (rbd_dev->parent) {
Alex Elder05a46af2013-04-26 15:44:36 -05005256 struct rbd_device *first = rbd_dev;
5257 struct rbd_device *second = first->parent;
5258 struct rbd_device *third;
5259
5260 /*
5261 * Follow to the parent with no grandparent and
5262 * remove it.
5263 */
5264 while (second && (third = second->parent)) {
5265 first = second;
5266 second = third;
5267 }
Alex Elderad945fc2013-04-26 15:44:36 -05005268 rbd_assert(second);
Alex Elder8ad42cd2013-04-28 23:32:34 -05005269 rbd_dev_image_release(second);
Alex Elderad945fc2013-04-26 15:44:36 -05005270 first->parent = NULL;
5271 first->parent_overlap = 0;
5272
5273 rbd_assert(first->parent_spec);
Alex Elder05a46af2013-04-26 15:44:36 -05005274 rbd_spec_put(first->parent_spec);
5275 first->parent_spec = NULL;
Alex Elder05a46af2013-04-26 15:44:36 -05005276 }
5277}
5278
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005279static ssize_t do_rbd_remove(struct bus_type *bus,
5280 const char *buf,
5281 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005282{
5283 struct rbd_device *rbd_dev = NULL;
Alex Elder751cc0e2013-05-31 15:17:01 -05005284 struct list_head *tmp;
5285 int dev_id;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005286 unsigned long ul;
Alex Elder82a442d2013-05-31 17:40:44 -05005287 bool already = false;
Alex Elder0d8189e2013-04-27 09:59:30 -05005288 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005289
Jingoo Hanbb8e0e82013-09-11 14:20:07 -07005290 ret = kstrtoul(buf, 10, &ul);
Alex Elder0d8189e2013-04-27 09:59:30 -05005291 if (ret)
5292 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005293
5294 /* convert to int; abort if we lost anything in the conversion */
Alex Elder751cc0e2013-05-31 15:17:01 -05005295 dev_id = (int)ul;
5296 if (dev_id != ul)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005297 return -EINVAL;
5298
Alex Elder751cc0e2013-05-31 15:17:01 -05005299 ret = -ENOENT;
5300 spin_lock(&rbd_dev_list_lock);
5301 list_for_each(tmp, &rbd_dev_list) {
5302 rbd_dev = list_entry(tmp, struct rbd_device, node);
5303 if (rbd_dev->dev_id == dev_id) {
5304 ret = 0;
5305 break;
5306 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005307 }
Alex Elder751cc0e2013-05-31 15:17:01 -05005308 if (!ret) {
5309 spin_lock_irq(&rbd_dev->lock);
5310 if (rbd_dev->open_count)
5311 ret = -EBUSY;
5312 else
Alex Elder82a442d2013-05-31 17:40:44 -05005313 already = test_and_set_bit(RBD_DEV_FLAG_REMOVING,
5314 &rbd_dev->flags);
Alex Elder751cc0e2013-05-31 15:17:01 -05005315 spin_unlock_irq(&rbd_dev->lock);
5316 }
5317 spin_unlock(&rbd_dev_list_lock);
Alex Elder82a442d2013-05-31 17:40:44 -05005318 if (ret < 0 || already)
Alex Elder1ba0f1e2013-05-31 15:17:01 -05005319 return ret;
Alex Elder751cc0e2013-05-31 15:17:01 -05005320
Ilya Dryomovfca27062013-12-16 18:02:40 +02005321 rbd_dev_header_unwatch_sync(rbd_dev);
Josh Durgin9abc5992013-08-29 17:31:03 -07005322 /*
5323 * flush remaining watch callbacks - these must be complete
5324 * before the osd_client is shutdown
5325 */
5326 dout("%s: flushing notifies", __func__);
5327 ceph_osdc_flush_notifies(&rbd_dev->rbd_client->client->osdc);
Ilya Dryomovfca27062013-12-16 18:02:40 +02005328
Josh Durgin98752012013-08-29 17:26:31 -07005329 /*
5330 * Don't free anything from rbd_dev->disk until after all
5331 * notifies are completely processed. Otherwise
5332 * rbd_bus_del_dev() will race with rbd_watch_cb(), resulting
5333 * in a potential use after free of rbd_dev->disk or rbd_dev.
5334 */
5335 rbd_bus_del_dev(rbd_dev);
Alex Elder8ad42cd2013-04-28 23:32:34 -05005336 rbd_dev_image_release(rbd_dev);
Alex Elder79ab7552013-04-28 23:32:34 -05005337 module_put(THIS_MODULE);
Alex Elderaafb2302012-09-06 16:00:54 -05005338
Alex Elder1ba0f1e2013-05-31 15:17:01 -05005339 return count;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005340}
5341
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005342static ssize_t rbd_remove(struct bus_type *bus,
5343 const char *buf,
5344 size_t count)
5345{
5346 if (single_major)
5347 return -EINVAL;
5348
5349 return do_rbd_remove(bus, buf, count);
5350}
5351
5352static ssize_t rbd_remove_single_major(struct bus_type *bus,
5353 const char *buf,
5354 size_t count)
5355{
5356 return do_rbd_remove(bus, buf, count);
5357}
5358
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005359/*
5360 * create control files in sysfs
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005361 * /sys/bus/rbd/...
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005362 */
5363static int rbd_sysfs_init(void)
5364{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005365 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005366
Alex Elderfed4c142012-02-07 12:03:36 -06005367 ret = device_register(&rbd_root_dev);
Alex Elder21079782012-01-24 10:08:36 -06005368 if (ret < 0)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005369 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005370
Alex Elderfed4c142012-02-07 12:03:36 -06005371 ret = bus_register(&rbd_bus_type);
5372 if (ret < 0)
5373 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005374
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005375 return ret;
5376}
5377
5378static void rbd_sysfs_cleanup(void)
5379{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005380 bus_unregister(&rbd_bus_type);
Alex Elderfed4c142012-02-07 12:03:36 -06005381 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005382}
5383
Alex Elder1c2a9df2013-05-01 12:43:03 -05005384static int rbd_slab_init(void)
5385{
5386 rbd_assert(!rbd_img_request_cache);
5387 rbd_img_request_cache = kmem_cache_create("rbd_img_request",
5388 sizeof (struct rbd_img_request),
5389 __alignof__(struct rbd_img_request),
5390 0, NULL);
Alex Elder868311b2013-05-01 12:43:03 -05005391 if (!rbd_img_request_cache)
5392 return -ENOMEM;
5393
5394 rbd_assert(!rbd_obj_request_cache);
5395 rbd_obj_request_cache = kmem_cache_create("rbd_obj_request",
5396 sizeof (struct rbd_obj_request),
5397 __alignof__(struct rbd_obj_request),
5398 0, NULL);
Alex Elder78c2a442013-05-01 12:43:04 -05005399 if (!rbd_obj_request_cache)
5400 goto out_err;
5401
5402 rbd_assert(!rbd_segment_name_cache);
5403 rbd_segment_name_cache = kmem_cache_create("rbd_segment_name",
Ilya Dryomov2d0ebc52014-01-27 17:40:18 +02005404 CEPH_MAX_OID_NAME_LEN + 1, 1, 0, NULL);
Alex Elder78c2a442013-05-01 12:43:04 -05005405 if (rbd_segment_name_cache)
Alex Elder1c2a9df2013-05-01 12:43:03 -05005406 return 0;
Alex Elder78c2a442013-05-01 12:43:04 -05005407out_err:
5408 if (rbd_obj_request_cache) {
5409 kmem_cache_destroy(rbd_obj_request_cache);
5410 rbd_obj_request_cache = NULL;
5411 }
Alex Elder1c2a9df2013-05-01 12:43:03 -05005412
Alex Elder868311b2013-05-01 12:43:03 -05005413 kmem_cache_destroy(rbd_img_request_cache);
5414 rbd_img_request_cache = NULL;
5415
Alex Elder1c2a9df2013-05-01 12:43:03 -05005416 return -ENOMEM;
5417}
5418
5419static void rbd_slab_exit(void)
5420{
Alex Elder78c2a442013-05-01 12:43:04 -05005421 rbd_assert(rbd_segment_name_cache);
5422 kmem_cache_destroy(rbd_segment_name_cache);
5423 rbd_segment_name_cache = NULL;
5424
Alex Elder868311b2013-05-01 12:43:03 -05005425 rbd_assert(rbd_obj_request_cache);
5426 kmem_cache_destroy(rbd_obj_request_cache);
5427 rbd_obj_request_cache = NULL;
5428
Alex Elder1c2a9df2013-05-01 12:43:03 -05005429 rbd_assert(rbd_img_request_cache);
5430 kmem_cache_destroy(rbd_img_request_cache);
5431 rbd_img_request_cache = NULL;
5432}
5433
Alex Eldercc344fa2013-02-19 12:25:56 -06005434static int __init rbd_init(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005435{
5436 int rc;
5437
Alex Elder1e32d342013-01-30 11:13:33 -06005438 if (!libceph_compatible(NULL)) {
5439 rbd_warn(NULL, "libceph incompatibility (quitting)");
Alex Elder1e32d342013-01-30 11:13:33 -06005440 return -EINVAL;
5441 }
Ilya Dryomove1b4d962013-12-13 15:28:57 +02005442
Alex Elder1c2a9df2013-05-01 12:43:03 -05005443 rc = rbd_slab_init();
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005444 if (rc)
5445 return rc;
Ilya Dryomove1b4d962013-12-13 15:28:57 +02005446
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005447 if (single_major) {
5448 rbd_major = register_blkdev(0, RBD_DRV_NAME);
5449 if (rbd_major < 0) {
5450 rc = rbd_major;
5451 goto err_out_slab;
5452 }
5453 }
5454
Alex Elder1c2a9df2013-05-01 12:43:03 -05005455 rc = rbd_sysfs_init();
5456 if (rc)
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005457 goto err_out_blkdev;
Alex Elder1c2a9df2013-05-01 12:43:03 -05005458
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005459 if (single_major)
5460 pr_info("loaded (major %d)\n", rbd_major);
5461 else
5462 pr_info("loaded\n");
5463
Ilya Dryomove1b4d962013-12-13 15:28:57 +02005464 return 0;
5465
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005466err_out_blkdev:
5467 if (single_major)
5468 unregister_blkdev(rbd_major, RBD_DRV_NAME);
Ilya Dryomove1b4d962013-12-13 15:28:57 +02005469err_out_slab:
5470 rbd_slab_exit();
Alex Elder1c2a9df2013-05-01 12:43:03 -05005471 return rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005472}
5473
Alex Eldercc344fa2013-02-19 12:25:56 -06005474static void __exit rbd_exit(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005475{
Ilya Dryomovffe312c2014-05-20 15:46:04 +04005476 ida_destroy(&rbd_dev_id_ida);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005477 rbd_sysfs_cleanup();
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005478 if (single_major)
5479 unregister_blkdev(rbd_major, RBD_DRV_NAME);
Alex Elder1c2a9df2013-05-01 12:43:03 -05005480 rbd_slab_exit();
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005481}
5482
5483module_init(rbd_init);
5484module_exit(rbd_exit);
5485
Alex Elderd552c612013-05-31 20:13:09 -05005486MODULE_AUTHOR("Alex Elder <elder@inktank.com>");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005487MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
5488MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005489/* following authorship retained from original osdblk.c */
5490MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
5491
Ilya Dryomov90da2582013-12-13 15:28:56 +02005492MODULE_DESCRIPTION("RADOS Block Device (RBD) driver");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005493MODULE_LICENSE("GPL");