blob: 4d680772379828423d8605b1cae8c5da271ec5b8 [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>
Ilya Dryomoved95b212016-08-12 16:40:02 +020034#include <linux/ceph/cls_lock_client.h>
Yehuda Sadeh602adf42010-08-12 16:11:25 -070035#include <linux/ceph/decode.h>
Yehuda Sadeh59c2be12011-03-21 15:10:11 -070036#include <linux/parser.h>
Alex Elder30d1cff2013-05-01 12:43:03 -050037#include <linux/bsearch.h>
Yehuda Sadeh602adf42010-08-12 16:11:25 -070038
39#include <linux/kernel.h>
40#include <linux/device.h>
41#include <linux/module.h>
Christoph Hellwig7ad18af2015-01-13 17:20:04 +010042#include <linux/blk-mq.h>
Yehuda Sadeh602adf42010-08-12 16:11:25 -070043#include <linux/fs.h>
44#include <linux/blkdev.h>
Alex Elder1c2a9df2013-05-01 12:43:03 -050045#include <linux/slab.h>
Ilya Dryomovf8a22fc2013-12-13 15:28:57 +020046#include <linux/idr.h>
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +040047#include <linux/workqueue.h>
Yehuda Sadeh602adf42010-08-12 16:11:25 -070048
49#include "rbd_types.h"
50
Alex Elderaafb2302012-09-06 16:00:54 -050051#define RBD_DEBUG /* Activate rbd_assert() calls */
52
Alex Elder593a9e72012-02-07 12:03:37 -060053/*
54 * The basic unit of block I/O is a sector. It is interpreted in a
55 * number of contexts in Linux (blk, bio, genhd), but the default is
56 * universally 512 bytes. These symbols are just slightly more
57 * meaningful than the bare numbers they represent.
58 */
59#define SECTOR_SHIFT 9
60#define SECTOR_SIZE (1ULL << SECTOR_SHIFT)
61
Alex Eldera2acd002013-05-08 22:50:04 -050062/*
63 * Increment the given counter and return its updated value.
64 * If the counter is already 0 it will not be incremented.
65 * If the counter is already at its maximum value returns
66 * -EINVAL without updating it.
67 */
68static int atomic_inc_return_safe(atomic_t *v)
69{
70 unsigned int counter;
71
72 counter = (unsigned int)__atomic_add_unless(v, 1, 0);
73 if (counter <= (unsigned int)INT_MAX)
74 return (int)counter;
75
76 atomic_dec(v);
77
78 return -EINVAL;
79}
80
81/* Decrement the counter. Return the resulting value, or -EINVAL */
82static int atomic_dec_return_safe(atomic_t *v)
83{
84 int counter;
85
86 counter = atomic_dec_return(v);
87 if (counter >= 0)
88 return counter;
89
90 atomic_inc(v);
91
92 return -EINVAL;
93}
94
Alex Elderf0f8cef2012-01-29 13:57:44 -060095#define RBD_DRV_NAME "rbd"
Yehuda Sadeh602adf42010-08-12 16:11:25 -070096
Ilya Dryomov7e513d42013-12-16 19:26:32 +020097#define RBD_MINORS_PER_MAJOR 256
98#define RBD_SINGLE_MAJOR_PART_SHIFT 4
Yehuda Sadeh602adf42010-08-12 16:11:25 -070099
Ilya Dryomov6d69bb532015-10-11 19:38:00 +0200100#define RBD_MAX_PARENT_CHAIN_LEN 16
101
Alex Elderd4b125e2012-07-03 16:01:19 -0500102#define RBD_SNAP_DEV_NAME_PREFIX "snap_"
103#define RBD_MAX_SNAP_NAME_LEN \
104 (NAME_MAX - (sizeof (RBD_SNAP_DEV_NAME_PREFIX) - 1))
105
Alex Elder35d489f2012-07-03 16:01:19 -0500106#define RBD_MAX_SNAP_COUNT 510 /* allows max snapc to fit in 4KB */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700107
108#define RBD_SNAP_HEAD_NAME "-"
109
Alex Elder9682fc62013-04-30 00:44:33 -0500110#define BAD_SNAP_INDEX U32_MAX /* invalid index into snap array */
111
Alex Elder9e15b772012-10-30 19:40:33 -0500112/* This allows a single page to hold an image name sent by OSD */
113#define RBD_IMAGE_NAME_LEN_MAX (PAGE_SIZE - sizeof (__le32) - 1)
Alex Elder1e130192012-07-03 16:01:19 -0500114#define RBD_IMAGE_ID_LEN_MAX 64
Alex Elder9e15b772012-10-30 19:40:33 -0500115
Alex Elder1e130192012-07-03 16:01:19 -0500116#define RBD_OBJ_PREFIX_LEN_MAX 64
Alex Elder589d30e2012-07-10 20:30:11 -0500117
Ilya Dryomoved95b212016-08-12 16:40:02 +0200118#define RBD_NOTIFY_TIMEOUT 5 /* seconds */
Ilya Dryomov99d16942016-08-12 16:11:41 +0200119#define RBD_RETRY_DELAY msecs_to_jiffies(1000)
120
Alex Elderd8891402012-10-09 13:50:17 -0700121/* Feature bits */
122
Alex Elder5cbf6f122013-04-11 09:29:48 -0500123#define RBD_FEATURE_LAYERING (1<<0)
124#define RBD_FEATURE_STRIPINGV2 (1<<1)
Ilya Dryomoved95b212016-08-12 16:40:02 +0200125#define RBD_FEATURE_EXCLUSIVE_LOCK (1<<2)
Ilya Dryomov7e973322017-01-25 18:16:22 +0100126#define RBD_FEATURE_DATA_POOL (1<<7)
Ilya Dryomoved95b212016-08-12 16:40:02 +0200127#define RBD_FEATURES_ALL (RBD_FEATURE_LAYERING | \
128 RBD_FEATURE_STRIPINGV2 | \
Ilya Dryomov7e973322017-01-25 18:16:22 +0100129 RBD_FEATURE_EXCLUSIVE_LOCK | \
130 RBD_FEATURE_DATA_POOL)
Alex Elderd8891402012-10-09 13:50:17 -0700131
132/* Features supported by this (client software) implementation. */
133
Alex Elder770eba62012-10-25 23:34:40 -0500134#define RBD_FEATURES_SUPPORTED (RBD_FEATURES_ALL)
Alex Elderd8891402012-10-09 13:50:17 -0700135
Alex Elder81a89792012-02-02 08:13:30 -0600136/*
137 * An RBD device name will be "rbd#", where the "rbd" comes from
138 * RBD_DRV_NAME above, and # is a unique integer identifier.
Alex Elder81a89792012-02-02 08:13:30 -0600139 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700140#define DEV_NAME_LEN 32
141
142/*
143 * block device image metadata (in-memory version)
144 */
145struct rbd_image_header {
Alex Elderf35a4de2013-05-06 09:51:29 -0500146 /* These six fields never change for a given rbd image */
Alex Elder849b4262012-07-09 21:04:24 -0500147 char *object_prefix;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700148 __u8 obj_order;
Alex Elderf35a4de2013-05-06 09:51:29 -0500149 u64 stripe_unit;
150 u64 stripe_count;
Ilya Dryomov7e973322017-01-25 18:16:22 +0100151 s64 data_pool_id;
Alex Elderf35a4de2013-05-06 09:51:29 -0500152 u64 features; /* Might be changeable someday? */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700153
Alex Elderf84344f2012-08-31 17:29:51 -0500154 /* The remaining fields need to be updated occasionally */
155 u64 image_size;
156 struct ceph_snap_context *snapc;
Alex Elderf35a4de2013-05-06 09:51:29 -0500157 char *snap_names; /* format 1 only */
158 u64 *snap_sizes; /* format 1 only */
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700159};
160
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500161/*
162 * An rbd image specification.
163 *
164 * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely
Alex Elderc66c6e02012-11-01 08:39:26 -0500165 * identify an image. Each rbd_dev structure includes a pointer to
166 * an rbd_spec structure that encapsulates this identity.
167 *
168 * Each of the id's in an rbd_spec has an associated name. For a
169 * user-mapped image, the names are supplied and the id's associated
170 * with them are looked up. For a layered image, a parent image is
171 * defined by the tuple, and the names are looked up.
172 *
173 * An rbd_dev structure contains a parent_spec pointer which is
174 * non-null if the image it represents is a child in a layered
175 * image. This pointer will refer to the rbd_spec structure used
176 * by the parent rbd_dev for its own identity (i.e., the structure
177 * is shared between the parent and child).
178 *
179 * Since these structures are populated once, during the discovery
180 * phase of image construction, they are effectively immutable so
181 * we make no effort to synchronize access to them.
182 *
183 * Note that code herein does not assume the image name is known (it
184 * could be a null pointer).
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500185 */
186struct rbd_spec {
187 u64 pool_id;
Alex Elderecb4dc22013-04-26 09:43:47 -0500188 const char *pool_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500189
Alex Elderecb4dc22013-04-26 09:43:47 -0500190 const char *image_id;
191 const char *image_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500192
193 u64 snap_id;
Alex Elderecb4dc22013-04-26 09:43:47 -0500194 const char *snap_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500195
196 struct kref kref;
197};
198
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700199/*
Alex Elderf0f8cef2012-01-29 13:57:44 -0600200 * an instance of the client. multiple devices may share an rbd client.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700201 */
202struct rbd_client {
203 struct ceph_client *client;
204 struct kref kref;
205 struct list_head node;
206};
207
Alex Elderbf0d5f502012-11-22 00:00:08 -0600208struct rbd_img_request;
209typedef void (*rbd_img_callback_t)(struct rbd_img_request *);
210
211#define BAD_WHICH U32_MAX /* Good which or bad which, which? */
212
213struct rbd_obj_request;
214typedef void (*rbd_obj_callback_t)(struct rbd_obj_request *);
215
Alex Elder9969ebc2013-01-18 12:31:10 -0600216enum obj_request_type {
217 OBJ_REQUEST_NODATA, OBJ_REQUEST_BIO, OBJ_REQUEST_PAGES
218};
Alex Elderbf0d5f502012-11-22 00:00:08 -0600219
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800220enum obj_operation_type {
221 OBJ_OP_WRITE,
222 OBJ_OP_READ,
Guangliang Zhao90e98c52014-04-01 22:22:16 +0800223 OBJ_OP_DISCARD,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800224};
225
Alex Elder926f9b32013-02-11 12:33:24 -0600226enum obj_req_flags {
227 OBJ_REQ_DONE, /* completion flag: not done = 0, done = 1 */
Alex Elder6365d332013-02-11 12:33:24 -0600228 OBJ_REQ_IMG_DATA, /* object usage: standalone = 0, image = 1 */
Alex Elder5679c592013-02-11 12:33:24 -0600229 OBJ_REQ_KNOWN, /* EXISTS flag valid: no = 0, yes = 1 */
230 OBJ_REQ_EXISTS, /* target exists: no = 0, yes = 1 */
Alex Elder926f9b32013-02-11 12:33:24 -0600231};
232
Alex Elderbf0d5f502012-11-22 00:00:08 -0600233struct rbd_obj_request {
Ilya Dryomova90bb0c2017-01-25 18:16:23 +0100234 u64 object_no;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600235 u64 offset; /* object start byte */
236 u64 length; /* bytes from offset */
Alex Elder926f9b32013-02-11 12:33:24 -0600237 unsigned long flags;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600238
Alex Elderc5b5ef62013-02-11 12:33:24 -0600239 /*
240 * An object request associated with an image will have its
241 * img_data flag set; a standalone object request will not.
242 *
243 * A standalone object request will have which == BAD_WHICH
244 * and a null obj_request pointer.
245 *
246 * An object request initiated in support of a layered image
247 * object (to check for its existence before a write) will
248 * have which == BAD_WHICH and a non-null obj_request pointer.
249 *
250 * Finally, an object request for rbd image data will have
251 * which != BAD_WHICH, and will have a non-null img_request
252 * pointer. The value of which will be in the range
253 * 0..(img_request->obj_request_count-1).
254 */
255 union {
256 struct rbd_obj_request *obj_request; /* STAT op */
257 struct {
258 struct rbd_img_request *img_request;
259 u64 img_offset;
260 /* links for img_request->obj_requests list */
261 struct list_head links;
262 };
263 };
Alex Elderbf0d5f502012-11-22 00:00:08 -0600264 u32 which; /* posn image request list */
265
266 enum obj_request_type type;
Alex Elder788e2df2013-01-17 12:25:27 -0600267 union {
268 struct bio *bio_list;
269 struct {
270 struct page **pages;
271 u32 page_count;
272 };
273 };
Alex Elder0eefd472013-04-19 15:34:50 -0500274 struct page **copyup_pages;
Alex Elderebda6402013-05-10 16:29:22 -0500275 u32 copyup_page_count;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600276
277 struct ceph_osd_request *osd_req;
278
279 u64 xferred; /* bytes transferred */
Sage Weil1b83bef2013-02-25 16:11:12 -0800280 int result;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600281
282 rbd_obj_callback_t callback;
Alex Elder788e2df2013-01-17 12:25:27 -0600283 struct completion completion;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600284
285 struct kref kref;
286};
287
Alex Elder0c425242013-02-08 09:55:49 -0600288enum img_req_flags {
Alex Elder9849e982013-01-24 16:13:36 -0600289 IMG_REQ_WRITE, /* I/O direction: read = 0, write = 1 */
290 IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */
Alex Elderd0b2e942013-01-24 16:13:36 -0600291 IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */
Guangliang Zhao90e98c52014-04-01 22:22:16 +0800292 IMG_REQ_DISCARD, /* discard: normal = 0, discard request = 1 */
Alex Elder0c425242013-02-08 09:55:49 -0600293};
294
Alex Elderbf0d5f502012-11-22 00:00:08 -0600295struct rbd_img_request {
Alex Elderbf0d5f502012-11-22 00:00:08 -0600296 struct rbd_device *rbd_dev;
297 u64 offset; /* starting image byte offset */
298 u64 length; /* byte count from offset */
Alex Elder0c425242013-02-08 09:55:49 -0600299 unsigned long flags;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600300 union {
Alex Elder9849e982013-01-24 16:13:36 -0600301 u64 snap_id; /* for reads */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600302 struct ceph_snap_context *snapc; /* for writes */
Alex Elder9849e982013-01-24 16:13:36 -0600303 };
304 union {
305 struct request *rq; /* block request */
306 struct rbd_obj_request *obj_request; /* obj req initiator */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600307 };
Alex Elder3d7efd12013-04-19 15:34:50 -0500308 struct page **copyup_pages;
Alex Elderebda6402013-05-10 16:29:22 -0500309 u32 copyup_page_count;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600310 spinlock_t completion_lock;/* protects next_completion */
311 u32 next_completion;
312 rbd_img_callback_t callback;
Alex Elder55f27e02013-04-10 12:34:25 -0500313 u64 xferred;/* aggregate bytes transferred */
Alex Eldera5a337d2013-01-24 16:13:36 -0600314 int result; /* first nonzero obj_request result */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600315
316 u32 obj_request_count;
317 struct list_head obj_requests; /* rbd_obj_request structs */
318
319 struct kref kref;
320};
321
322#define for_each_obj_request(ireq, oreq) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600323 list_for_each_entry(oreq, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600324#define for_each_obj_request_from(ireq, oreq) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600325 list_for_each_entry_from(oreq, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600326#define for_each_obj_request_safe(ireq, oreq, n) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600327 list_for_each_entry_safe_reverse(oreq, n, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600328
Ilya Dryomov99d16942016-08-12 16:11:41 +0200329enum rbd_watch_state {
330 RBD_WATCH_STATE_UNREGISTERED,
331 RBD_WATCH_STATE_REGISTERED,
332 RBD_WATCH_STATE_ERROR,
333};
334
Ilya Dryomoved95b212016-08-12 16:40:02 +0200335enum rbd_lock_state {
336 RBD_LOCK_STATE_UNLOCKED,
337 RBD_LOCK_STATE_LOCKED,
338 RBD_LOCK_STATE_RELEASING,
339};
340
341/* WatchNotify::ClientId */
342struct rbd_client_id {
343 u64 gid;
344 u64 handle;
345};
346
Alex Elderf84344f2012-08-31 17:29:51 -0500347struct rbd_mapping {
Alex Elder99c1f082012-08-30 14:42:15 -0500348 u64 size;
Alex Elder34b13182012-07-13 20:35:12 -0500349 u64 features;
Alex Elderf84344f2012-08-31 17:29:51 -0500350 bool read_only;
351};
352
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700353/*
354 * a single device
355 */
356struct rbd_device {
Alex Elderde71a292012-07-03 16:01:19 -0500357 int dev_id; /* blkdev unique id */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700358
359 int major; /* blkdev assigned major */
Ilya Dryomovdd82fff2013-12-13 15:28:57 +0200360 int minor;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700361 struct gendisk *disk; /* blkdev's gendisk and rq */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700362
Alex Eldera30b71b2012-07-10 20:30:11 -0500363 u32 image_format; /* Either 1 or 2 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700364 struct rbd_client *rbd_client;
365
366 char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */
367
Alex Elderb82d1672013-01-14 12:43:31 -0600368 spinlock_t lock; /* queue, flags, open_count */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700369
370 struct rbd_image_header header;
Alex Elderb82d1672013-01-14 12:43:31 -0600371 unsigned long flags; /* possibly lock protected */
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500372 struct rbd_spec *spec;
Ilya Dryomovd1475432015-06-22 13:24:48 +0300373 struct rbd_options *opts;
Mike Christie0d6d1e9c2016-08-18 18:38:45 +0200374 char *config_info; /* add{,_single_major} string */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700375
Ilya Dryomovc41d13a2016-04-29 20:01:25 +0200376 struct ceph_object_id header_oid;
Ilya Dryomov922dab62016-05-26 01:15:02 +0200377 struct ceph_object_locator header_oloc;
Alex Elder971f8392012-10-25 23:34:41 -0500378
Ilya Dryomov1643dfa2016-08-12 15:45:52 +0200379 struct ceph_file_layout layout; /* used for all rbd requests */
Alex Elder0903e872012-11-14 12:25:19 -0600380
Ilya Dryomov99d16942016-08-12 16:11:41 +0200381 struct mutex watch_mutex;
382 enum rbd_watch_state watch_state;
Ilya Dryomov922dab62016-05-26 01:15:02 +0200383 struct ceph_osd_linger_request *watch_handle;
Ilya Dryomov99d16942016-08-12 16:11:41 +0200384 u64 watch_cookie;
385 struct delayed_work watch_dwork;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700386
Ilya Dryomoved95b212016-08-12 16:40:02 +0200387 struct rw_semaphore lock_rwsem;
388 enum rbd_lock_state lock_state;
389 struct rbd_client_id owner_cid;
390 struct work_struct acquired_lock_work;
391 struct work_struct released_lock_work;
392 struct delayed_work lock_dwork;
393 struct work_struct unlock_work;
394 wait_queue_head_t lock_waitq;
395
Ilya Dryomov1643dfa2016-08-12 15:45:52 +0200396 struct workqueue_struct *task_wq;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700397
Alex Elder86b00e02012-10-25 23:34:42 -0500398 struct rbd_spec *parent_spec;
399 u64 parent_overlap;
Alex Eldera2acd002013-05-08 22:50:04 -0500400 atomic_t parent_ref;
Alex Elder2f82ee52012-10-30 19:40:33 -0500401 struct rbd_device *parent;
Alex Elder86b00e02012-10-25 23:34:42 -0500402
Christoph Hellwig7ad18af2015-01-13 17:20:04 +0100403 /* Block layer tags. */
404 struct blk_mq_tag_set tag_set;
405
Josh Durginc6666012011-11-21 17:11:12 -0800406 /* protects updating the header */
407 struct rw_semaphore header_rwsem;
Alex Elderf84344f2012-08-31 17:29:51 -0500408
409 struct rbd_mapping mapping;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700410
411 struct list_head node;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800412
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800413 /* sysfs related */
414 struct device dev;
Alex Elderb82d1672013-01-14 12:43:31 -0600415 unsigned long open_count; /* protected by lock */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800416};
417
Alex Elderb82d1672013-01-14 12:43:31 -0600418/*
Ilya Dryomov87c0fde2016-09-29 13:41:05 +0200419 * Flag bits for rbd_dev->flags:
420 * - REMOVING (which is coupled with rbd_dev->open_count) is protected
421 * by rbd_dev->lock
422 * - BLACKLISTED is protected by rbd_dev->lock_rwsem
Alex Elderb82d1672013-01-14 12:43:31 -0600423 */
Alex Elder6d292902013-01-14 12:43:31 -0600424enum rbd_dev_flags {
425 RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */
Alex Elderb82d1672013-01-14 12:43:31 -0600426 RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */
Ilya Dryomov87c0fde2016-09-29 13:41:05 +0200427 RBD_DEV_FLAG_BLACKLISTED, /* our ceph_client is blacklisted */
Alex Elder6d292902013-01-14 12:43:31 -0600428};
429
Alex Eldercfbf6372013-05-31 17:40:45 -0500430static DEFINE_MUTEX(client_mutex); /* Serialize client creation */
Alex Eldere124a82f2012-01-29 13:57:44 -0600431
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700432static LIST_HEAD(rbd_dev_list); /* devices */
Alex Eldere124a82f2012-01-29 13:57:44 -0600433static DEFINE_SPINLOCK(rbd_dev_list_lock);
434
Alex Elder432b8582012-01-29 13:57:44 -0600435static LIST_HEAD(rbd_client_list); /* clients */
436static DEFINE_SPINLOCK(rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700437
Alex Elder78c2a442013-05-01 12:43:04 -0500438/* Slab caches for frequently-allocated structures */
439
Alex Elder1c2a9df2013-05-01 12:43:03 -0500440static struct kmem_cache *rbd_img_request_cache;
Alex Elder868311b2013-05-01 12:43:03 -0500441static struct kmem_cache *rbd_obj_request_cache;
Alex Elder1c2a9df2013-05-01 12:43:03 -0500442
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200443static int rbd_major;
Ilya Dryomovf8a22fc2013-12-13 15:28:57 +0200444static DEFINE_IDA(rbd_dev_id_ida);
445
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +0400446static struct workqueue_struct *rbd_wq;
447
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200448/*
449 * Default to false for now, as single-major requires >= 0.75 version of
450 * userspace rbd utility.
451 */
452static bool single_major = false;
453module_param(single_major, bool, S_IRUGO);
454MODULE_PARM_DESC(single_major, "Use a single major number for all rbd devices (default: false)");
455
Alex Elder3d7efd12013-04-19 15:34:50 -0500456static int rbd_img_request_submit(struct rbd_img_request *img_request);
457
Alex Elderf0f8cef2012-01-29 13:57:44 -0600458static ssize_t rbd_add(struct bus_type *bus, const char *buf,
459 size_t count);
460static ssize_t rbd_remove(struct bus_type *bus, const char *buf,
461 size_t count);
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200462static ssize_t rbd_add_single_major(struct bus_type *bus, const char *buf,
463 size_t count);
464static ssize_t rbd_remove_single_major(struct bus_type *bus, const char *buf,
465 size_t count);
Ilya Dryomov6d69bb532015-10-11 19:38:00 +0200466static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth);
Alex Eldera2acd002013-05-08 22:50:04 -0500467static void rbd_spec_put(struct rbd_spec *spec);
Alex Elderf0f8cef2012-01-29 13:57:44 -0600468
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200469static int rbd_dev_id_to_minor(int dev_id)
470{
Ilya Dryomov7e513d42013-12-16 19:26:32 +0200471 return dev_id << RBD_SINGLE_MAJOR_PART_SHIFT;
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200472}
473
474static int minor_to_rbd_dev_id(int minor)
475{
Ilya Dryomov7e513d42013-12-16 19:26:32 +0200476 return minor >> RBD_SINGLE_MAJOR_PART_SHIFT;
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200477}
478
Ilya Dryomoved95b212016-08-12 16:40:02 +0200479static bool rbd_is_lock_supported(struct rbd_device *rbd_dev)
480{
481 return (rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK) &&
482 rbd_dev->spec->snap_id == CEPH_NOSNAP &&
483 !rbd_dev->mapping.read_only;
484}
485
486static bool __rbd_is_lock_owner(struct rbd_device *rbd_dev)
487{
488 return rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED ||
489 rbd_dev->lock_state == RBD_LOCK_STATE_RELEASING;
490}
491
492static bool rbd_is_lock_owner(struct rbd_device *rbd_dev)
493{
494 bool is_lock_owner;
495
496 down_read(&rbd_dev->lock_rwsem);
497 is_lock_owner = __rbd_is_lock_owner(rbd_dev);
498 up_read(&rbd_dev->lock_rwsem);
499 return is_lock_owner;
500}
501
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700502static BUS_ATTR(add, S_IWUSR, NULL, rbd_add);
503static BUS_ATTR(remove, S_IWUSR, NULL, rbd_remove);
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200504static BUS_ATTR(add_single_major, S_IWUSR, NULL, rbd_add_single_major);
505static BUS_ATTR(remove_single_major, S_IWUSR, NULL, rbd_remove_single_major);
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700506
507static struct attribute *rbd_bus_attrs[] = {
508 &bus_attr_add.attr,
509 &bus_attr_remove.attr,
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200510 &bus_attr_add_single_major.attr,
511 &bus_attr_remove_single_major.attr,
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700512 NULL,
Alex Elderf0f8cef2012-01-29 13:57:44 -0600513};
Ilya Dryomov92c76dc2013-12-13 15:28:57 +0200514
515static umode_t rbd_bus_is_visible(struct kobject *kobj,
516 struct attribute *attr, int index)
517{
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200518 if (!single_major &&
519 (attr == &bus_attr_add_single_major.attr ||
520 attr == &bus_attr_remove_single_major.attr))
521 return 0;
522
Ilya Dryomov92c76dc2013-12-13 15:28:57 +0200523 return attr->mode;
524}
525
526static const struct attribute_group rbd_bus_group = {
527 .attrs = rbd_bus_attrs,
528 .is_visible = rbd_bus_is_visible,
529};
530__ATTRIBUTE_GROUPS(rbd_bus);
Alex Elderf0f8cef2012-01-29 13:57:44 -0600531
532static struct bus_type rbd_bus_type = {
533 .name = "rbd",
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700534 .bus_groups = rbd_bus_groups,
Alex Elderf0f8cef2012-01-29 13:57:44 -0600535};
536
537static void rbd_root_dev_release(struct device *dev)
538{
539}
540
541static struct device rbd_root_dev = {
542 .init_name = "rbd",
543 .release = rbd_root_dev_release,
544};
545
Alex Elder06ecc6c2012-11-01 10:17:15 -0500546static __printf(2, 3)
547void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...)
548{
549 struct va_format vaf;
550 va_list args;
551
552 va_start(args, fmt);
553 vaf.fmt = fmt;
554 vaf.va = &args;
555
556 if (!rbd_dev)
557 printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf);
558 else if (rbd_dev->disk)
559 printk(KERN_WARNING "%s: %s: %pV\n",
560 RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf);
561 else if (rbd_dev->spec && rbd_dev->spec->image_name)
562 printk(KERN_WARNING "%s: image %s: %pV\n",
563 RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf);
564 else if (rbd_dev->spec && rbd_dev->spec->image_id)
565 printk(KERN_WARNING "%s: id %s: %pV\n",
566 RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf);
567 else /* punt */
568 printk(KERN_WARNING "%s: rbd_dev %p: %pV\n",
569 RBD_DRV_NAME, rbd_dev, &vaf);
570 va_end(args);
571}
572
Alex Elderaafb2302012-09-06 16:00:54 -0500573#ifdef RBD_DEBUG
574#define rbd_assert(expr) \
575 if (unlikely(!(expr))) { \
576 printk(KERN_ERR "\nAssertion failure in %s() " \
577 "at line %d:\n\n" \
578 "\trbd_assert(%s);\n\n", \
579 __func__, __LINE__, #expr); \
580 BUG(); \
581 }
582#else /* !RBD_DEBUG */
583# define rbd_assert(expr) ((void) 0)
584#endif /* !RBD_DEBUG */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800585
Ilya Dryomov27617132015-07-16 17:36:11 +0300586static void rbd_osd_copyup_callback(struct rbd_obj_request *obj_request);
Alex Elderb454e362013-04-19 15:34:50 -0500587static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request);
Alex Elder05a46af2013-04-26 15:44:36 -0500588static void rbd_img_parent_read(struct rbd_obj_request *obj_request);
589static void rbd_dev_remove_parent(struct rbd_device *rbd_dev);
Alex Elder8b3e1a52013-01-24 16:13:36 -0600590
Alex Eldercc4a38bd2013-04-30 00:44:33 -0500591static int rbd_dev_refresh(struct rbd_device *rbd_dev);
Alex Elder2df3fac2013-05-06 09:51:30 -0500592static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev);
Ilya Dryomova720ae02014-07-23 17:11:19 +0400593static int rbd_dev_header_info(struct rbd_device *rbd_dev);
Ilya Dryomove8f59b52014-07-24 10:42:13 +0400594static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev);
Alex Elder54cac612013-04-30 00:44:33 -0500595static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
596 u64 snap_id);
Alex Elder2ad3d712013-04-30 00:44:33 -0500597static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
598 u8 *order, u64 *snap_size);
599static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
600 u64 *snap_features);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700601
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700602static int rbd_open(struct block_device *bdev, fmode_t mode)
603{
Alex Elderf0f8cef2012-01-29 13:57:44 -0600604 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
Alex Elderb82d1672013-01-14 12:43:31 -0600605 bool removing = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700606
Alex Elderf84344f2012-08-31 17:29:51 -0500607 if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700608 return -EROFS;
609
Alex Eldera14ea262013-02-05 13:23:12 -0600610 spin_lock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600611 if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags))
612 removing = true;
613 else
614 rbd_dev->open_count++;
Alex Eldera14ea262013-02-05 13:23:12 -0600615 spin_unlock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600616 if (removing)
617 return -ENOENT;
618
Alex Elderc3e946c2012-11-16 09:29:16 -0600619 (void) get_device(&rbd_dev->dev);
Alex Elder340c7a22012-08-10 13:12:07 -0700620
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700621 return 0;
622}
623
Al Virodb2a1442013-05-05 21:52:57 -0400624static void rbd_release(struct gendisk *disk, fmode_t mode)
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800625{
626 struct rbd_device *rbd_dev = disk->private_data;
Alex Elderb82d1672013-01-14 12:43:31 -0600627 unsigned long open_count_before;
628
Alex Eldera14ea262013-02-05 13:23:12 -0600629 spin_lock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600630 open_count_before = rbd_dev->open_count--;
Alex Eldera14ea262013-02-05 13:23:12 -0600631 spin_unlock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600632 rbd_assert(open_count_before > 0);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800633
Alex Elderc3e946c2012-11-16 09:29:16 -0600634 put_device(&rbd_dev->dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800635}
636
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800637static int rbd_ioctl_set_ro(struct rbd_device *rbd_dev, unsigned long arg)
638{
Josh Durgin77f33c02013-09-30 17:09:54 -0700639 int ret = 0;
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800640 int val;
641 bool ro;
Josh Durgin77f33c02013-09-30 17:09:54 -0700642 bool ro_changed = false;
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800643
Josh Durgin77f33c02013-09-30 17:09:54 -0700644 /* get_user() may sleep, so call it before taking rbd_dev->lock */
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800645 if (get_user(val, (int __user *)(arg)))
646 return -EFAULT;
647
648 ro = val ? true : false;
649 /* Snapshot doesn't allow to write*/
650 if (rbd_dev->spec->snap_id != CEPH_NOSNAP && !ro)
651 return -EROFS;
652
Josh Durgin77f33c02013-09-30 17:09:54 -0700653 spin_lock_irq(&rbd_dev->lock);
654 /* prevent others open this device */
655 if (rbd_dev->open_count > 1) {
656 ret = -EBUSY;
657 goto out;
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800658 }
659
Josh Durgin77f33c02013-09-30 17:09:54 -0700660 if (rbd_dev->mapping.read_only != ro) {
661 rbd_dev->mapping.read_only = ro;
662 ro_changed = true;
663 }
664
665out:
666 spin_unlock_irq(&rbd_dev->lock);
667 /* set_disk_ro() may sleep, so call it after releasing rbd_dev->lock */
668 if (ret == 0 && ro_changed)
669 set_disk_ro(rbd_dev->disk, ro ? 1 : 0);
670
671 return ret;
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800672}
673
674static int rbd_ioctl(struct block_device *bdev, fmode_t mode,
675 unsigned int cmd, unsigned long arg)
676{
677 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
678 int ret = 0;
679
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800680 switch (cmd) {
681 case BLKROSET:
682 ret = rbd_ioctl_set_ro(rbd_dev, arg);
683 break;
684 default:
685 ret = -ENOTTY;
686 }
687
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800688 return ret;
689}
690
691#ifdef CONFIG_COMPAT
692static int rbd_compat_ioctl(struct block_device *bdev, fmode_t mode,
693 unsigned int cmd, unsigned long arg)
694{
695 return rbd_ioctl(bdev, mode, cmd, arg);
696}
697#endif /* CONFIG_COMPAT */
698
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700699static const struct block_device_operations rbd_bd_ops = {
700 .owner = THIS_MODULE,
701 .open = rbd_open,
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800702 .release = rbd_release,
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800703 .ioctl = rbd_ioctl,
704#ifdef CONFIG_COMPAT
705 .compat_ioctl = rbd_compat_ioctl,
706#endif
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700707};
708
709/*
Alex Elder7262cfc2013-05-16 15:04:20 -0500710 * Initialize an rbd client instance. Success or not, this function
Alex Eldercfbf6372013-05-31 17:40:45 -0500711 * consumes ceph_opts. Caller holds client_mutex.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700712 */
Alex Elderf8c38922012-08-10 13:12:07 -0700713static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700714{
715 struct rbd_client *rbdc;
716 int ret = -ENOMEM;
717
Alex Elder37206ee2013-02-20 17:32:08 -0600718 dout("%s:\n", __func__);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700719 rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL);
720 if (!rbdc)
721 goto out_opt;
722
723 kref_init(&rbdc->kref);
724 INIT_LIST_HEAD(&rbdc->node);
725
Alex Elder43ae4702012-07-03 16:01:18 -0500726 rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700727 if (IS_ERR(rbdc->client))
Alex Elder08f75462013-05-29 11:19:00 -0500728 goto out_rbdc;
Alex Elder43ae4702012-07-03 16:01:18 -0500729 ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700730
731 ret = ceph_open_session(rbdc->client);
732 if (ret < 0)
Alex Elder08f75462013-05-29 11:19:00 -0500733 goto out_client;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700734
Alex Elder432b8582012-01-29 13:57:44 -0600735 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700736 list_add_tail(&rbdc->node, &rbd_client_list);
Alex Elder432b8582012-01-29 13:57:44 -0600737 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700738
Alex Elder37206ee2013-02-20 17:32:08 -0600739 dout("%s: rbdc %p\n", __func__, rbdc);
Alex Elderbc534d82012-01-29 13:57:44 -0600740
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700741 return rbdc;
Alex Elder08f75462013-05-29 11:19:00 -0500742out_client:
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700743 ceph_destroy_client(rbdc->client);
Alex Elder08f75462013-05-29 11:19:00 -0500744out_rbdc:
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700745 kfree(rbdc);
746out_opt:
Alex Elder43ae4702012-07-03 16:01:18 -0500747 if (ceph_opts)
748 ceph_destroy_options(ceph_opts);
Alex Elder37206ee2013-02-20 17:32:08 -0600749 dout("%s: error %d\n", __func__, ret);
750
Vasiliy Kulikov28f259b2010-09-26 12:59:37 +0400751 return ERR_PTR(ret);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700752}
753
Alex Elder2f82ee52012-10-30 19:40:33 -0500754static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc)
755{
756 kref_get(&rbdc->kref);
757
758 return rbdc;
759}
760
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700761/*
Alex Elder1f7ba332012-08-10 13:12:07 -0700762 * Find a ceph client with specific addr and configuration. If
763 * found, bump its reference count.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700764 */
Alex Elder1f7ba332012-08-10 13:12:07 -0700765static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700766{
767 struct rbd_client *client_node;
Alex Elder1f7ba332012-08-10 13:12:07 -0700768 bool found = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700769
Alex Elder43ae4702012-07-03 16:01:18 -0500770 if (ceph_opts->flags & CEPH_OPT_NOSHARE)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700771 return NULL;
772
Alex Elder1f7ba332012-08-10 13:12:07 -0700773 spin_lock(&rbd_client_list_lock);
774 list_for_each_entry(client_node, &rbd_client_list, node) {
775 if (!ceph_compare_options(ceph_opts, client_node->client)) {
Alex Elder2f82ee52012-10-30 19:40:33 -0500776 __rbd_get_client(client_node);
777
Alex Elder1f7ba332012-08-10 13:12:07 -0700778 found = true;
779 break;
780 }
781 }
782 spin_unlock(&rbd_client_list_lock);
783
784 return found ? client_node : NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700785}
786
787/*
Ilya Dryomov210c1042015-06-22 13:24:48 +0300788 * (Per device) rbd map options
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700789 */
790enum {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300791 Opt_queue_depth,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700792 Opt_last_int,
793 /* int args above */
794 Opt_last_string,
795 /* string args above */
Alex Eldercc0538b2012-08-10 13:12:07 -0700796 Opt_read_only,
797 Opt_read_write,
Ilya Dryomov80de1912016-09-20 14:23:17 +0200798 Opt_lock_on_read,
Ilya Dryomov210c1042015-06-22 13:24:48 +0300799 Opt_err
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700800};
801
Alex Elder43ae4702012-07-03 16:01:18 -0500802static match_table_t rbd_opts_tokens = {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300803 {Opt_queue_depth, "queue_depth=%d"},
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700804 /* int args above */
805 /* string args above */
Alex Elderbe466c12012-10-22 11:31:26 -0500806 {Opt_read_only, "read_only"},
Alex Eldercc0538b2012-08-10 13:12:07 -0700807 {Opt_read_only, "ro"}, /* Alternate spelling */
808 {Opt_read_write, "read_write"},
809 {Opt_read_write, "rw"}, /* Alternate spelling */
Ilya Dryomov80de1912016-09-20 14:23:17 +0200810 {Opt_lock_on_read, "lock_on_read"},
Ilya Dryomov210c1042015-06-22 13:24:48 +0300811 {Opt_err, NULL}
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700812};
813
Alex Elder98571b52013-01-20 14:44:42 -0600814struct rbd_options {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300815 int queue_depth;
Alex Elder98571b52013-01-20 14:44:42 -0600816 bool read_only;
Ilya Dryomov80de1912016-09-20 14:23:17 +0200817 bool lock_on_read;
Alex Elder98571b52013-01-20 14:44:42 -0600818};
819
Ilya Dryomovb5584182015-06-23 16:21:19 +0300820#define RBD_QUEUE_DEPTH_DEFAULT BLKDEV_MAX_RQ
Alex Elder98571b52013-01-20 14:44:42 -0600821#define RBD_READ_ONLY_DEFAULT false
Ilya Dryomov80de1912016-09-20 14:23:17 +0200822#define RBD_LOCK_ON_READ_DEFAULT false
Alex Elder98571b52013-01-20 14:44:42 -0600823
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700824static int parse_rbd_opts_token(char *c, void *private)
825{
Alex Elder43ae4702012-07-03 16:01:18 -0500826 struct rbd_options *rbd_opts = private;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700827 substring_t argstr[MAX_OPT_ARGS];
828 int token, intval, ret;
829
Alex Elder43ae4702012-07-03 16:01:18 -0500830 token = match_token(c, rbd_opts_tokens, argstr);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700831 if (token < Opt_last_int) {
832 ret = match_int(&argstr[0], &intval);
833 if (ret < 0) {
Ilya Dryomov210c1042015-06-22 13:24:48 +0300834 pr_err("bad mount option arg (not int) at '%s'\n", c);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700835 return ret;
836 }
837 dout("got int token %d val %d\n", token, intval);
838 } else if (token > Opt_last_int && token < Opt_last_string) {
Ilya Dryomov210c1042015-06-22 13:24:48 +0300839 dout("got string token %d val %s\n", token, argstr[0].from);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700840 } else {
841 dout("got token %d\n", token);
842 }
843
844 switch (token) {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300845 case Opt_queue_depth:
846 if (intval < 1) {
847 pr_err("queue_depth out of range\n");
848 return -EINVAL;
849 }
850 rbd_opts->queue_depth = intval;
851 break;
Alex Eldercc0538b2012-08-10 13:12:07 -0700852 case Opt_read_only:
853 rbd_opts->read_only = true;
854 break;
855 case Opt_read_write:
856 rbd_opts->read_only = false;
857 break;
Ilya Dryomov80de1912016-09-20 14:23:17 +0200858 case Opt_lock_on_read:
859 rbd_opts->lock_on_read = true;
860 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700861 default:
Ilya Dryomov210c1042015-06-22 13:24:48 +0300862 /* libceph prints "bad option" msg */
863 return -EINVAL;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700864 }
Ilya Dryomov210c1042015-06-22 13:24:48 +0300865
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700866 return 0;
867}
868
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800869static char* obj_op_name(enum obj_operation_type op_type)
870{
871 switch (op_type) {
872 case OBJ_OP_READ:
873 return "read";
874 case OBJ_OP_WRITE:
875 return "write";
Guangliang Zhao90e98c52014-04-01 22:22:16 +0800876 case OBJ_OP_DISCARD:
877 return "discard";
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800878 default:
879 return "???";
880 }
881}
882
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700883/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700884 * Get a ceph client with specific addr and configuration, if one does
Alex Elder7262cfc2013-05-16 15:04:20 -0500885 * not exist create it. Either way, ceph_opts is consumed by this
886 * function.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700887 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500888static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700889{
Alex Elderf8c38922012-08-10 13:12:07 -0700890 struct rbd_client *rbdc;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700891
Alex Eldercfbf6372013-05-31 17:40:45 -0500892 mutex_lock_nested(&client_mutex, SINGLE_DEPTH_NESTING);
Alex Elder1f7ba332012-08-10 13:12:07 -0700893 rbdc = rbd_client_find(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500894 if (rbdc) /* using an existing client */
Alex Elder43ae4702012-07-03 16:01:18 -0500895 ceph_destroy_options(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500896 else
Alex Elderf8c38922012-08-10 13:12:07 -0700897 rbdc = rbd_client_create(ceph_opts);
Alex Eldercfbf6372013-05-31 17:40:45 -0500898 mutex_unlock(&client_mutex);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700899
Alex Elder9d3997f2012-10-25 23:34:42 -0500900 return rbdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700901}
902
903/*
904 * Destroy ceph client
Alex Elderd23a4b32012-01-29 13:57:43 -0600905 *
Alex Elder432b8582012-01-29 13:57:44 -0600906 * Caller must hold rbd_client_list_lock.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700907 */
908static void rbd_client_release(struct kref *kref)
909{
910 struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
911
Alex Elder37206ee2013-02-20 17:32:08 -0600912 dout("%s: rbdc %p\n", __func__, rbdc);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500913 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700914 list_del(&rbdc->node);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500915 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700916
917 ceph_destroy_client(rbdc->client);
918 kfree(rbdc);
919}
920
921/*
922 * Drop reference to ceph client node. If it's not referenced anymore, release
923 * it.
924 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500925static void rbd_put_client(struct rbd_client *rbdc)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700926{
Alex Elderc53d5892012-10-25 23:34:42 -0500927 if (rbdc)
928 kref_put(&rbdc->kref, rbd_client_release);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700929}
930
Alex Eldera30b71b2012-07-10 20:30:11 -0500931static bool rbd_image_format_valid(u32 image_format)
932{
933 return image_format == 1 || image_format == 2;
934}
935
Alex Elder8e94af82012-07-25 09:32:40 -0500936static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
937{
Alex Elder103a1502012-08-02 11:29:45 -0500938 size_t size;
939 u32 snap_count;
940
941 /* The header has to start with the magic rbd header text */
942 if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT)))
943 return false;
944
Alex Elderdb2388b2012-10-20 22:17:27 -0500945 /* The bio layer requires at least sector-sized I/O */
946
947 if (ondisk->options.order < SECTOR_SHIFT)
948 return false;
949
950 /* If we use u64 in a few spots we may be able to loosen this */
951
952 if (ondisk->options.order > 8 * sizeof (int) - 1)
953 return false;
954
Alex Elder103a1502012-08-02 11:29:45 -0500955 /*
956 * The size of a snapshot header has to fit in a size_t, and
957 * that limits the number of snapshots.
958 */
959 snap_count = le32_to_cpu(ondisk->snap_count);
960 size = SIZE_MAX - sizeof (struct ceph_snap_context);
961 if (snap_count > size / sizeof (__le64))
962 return false;
963
964 /*
965 * Not only that, but the size of the entire the snapshot
966 * header must also be representable in a size_t.
967 */
968 size -= snap_count * sizeof (__le64);
969 if ((u64) size < le64_to_cpu(ondisk->snap_names_len))
970 return false;
971
972 return true;
Alex Elder8e94af82012-07-25 09:32:40 -0500973}
974
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700975/*
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +0100976 * returns the size of an object in the image
977 */
978static u32 rbd_obj_bytes(struct rbd_image_header *header)
979{
980 return 1U << header->obj_order;
981}
982
Ilya Dryomov263423f2017-01-25 18:16:22 +0100983static void rbd_init_layout(struct rbd_device *rbd_dev)
984{
985 if (rbd_dev->header.stripe_unit == 0 ||
986 rbd_dev->header.stripe_count == 0) {
987 rbd_dev->header.stripe_unit = rbd_obj_bytes(&rbd_dev->header);
988 rbd_dev->header.stripe_count = 1;
989 }
990
991 rbd_dev->layout.stripe_unit = rbd_dev->header.stripe_unit;
992 rbd_dev->layout.stripe_count = rbd_dev->header.stripe_count;
993 rbd_dev->layout.object_size = rbd_obj_bytes(&rbd_dev->header);
Ilya Dryomov7e973322017-01-25 18:16:22 +0100994 rbd_dev->layout.pool_id = rbd_dev->header.data_pool_id == CEPH_NOPOOL ?
995 rbd_dev->spec->pool_id : rbd_dev->header.data_pool_id;
Ilya Dryomov263423f2017-01-25 18:16:22 +0100996 RCU_INIT_POINTER(rbd_dev->layout.pool_ns, NULL);
997}
998
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +0100999/*
Alex Elderbb23e372013-05-06 09:51:29 -05001000 * Fill an rbd image header with information from the given format 1
1001 * on-disk header.
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001002 */
Alex Elder662518b2013-05-06 09:51:29 -05001003static int rbd_header_from_disk(struct rbd_device *rbd_dev,
Alex Elder4156d992012-08-02 11:29:46 -05001004 struct rbd_image_header_ondisk *ondisk)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001005{
Alex Elder662518b2013-05-06 09:51:29 -05001006 struct rbd_image_header *header = &rbd_dev->header;
Alex Elderbb23e372013-05-06 09:51:29 -05001007 bool first_time = header->object_prefix == NULL;
1008 struct ceph_snap_context *snapc;
1009 char *object_prefix = NULL;
1010 char *snap_names = NULL;
1011 u64 *snap_sizes = NULL;
Alex Elderccece232012-07-10 20:30:10 -05001012 u32 snap_count;
Alex Elderbb23e372013-05-06 09:51:29 -05001013 int ret = -ENOMEM;
Alex Elder621901d2012-08-23 23:22:06 -05001014 u32 i;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001015
Alex Elderbb23e372013-05-06 09:51:29 -05001016 /* Allocate this now to avoid having to handle failure below */
1017
1018 if (first_time) {
Ilya Dryomov848d7962017-01-25 18:16:21 +01001019 object_prefix = kstrndup(ondisk->object_prefix,
1020 sizeof(ondisk->object_prefix),
1021 GFP_KERNEL);
Alex Elderbb23e372013-05-06 09:51:29 -05001022 if (!object_prefix)
1023 return -ENOMEM;
Alex Elderbb23e372013-05-06 09:51:29 -05001024 }
1025
1026 /* Allocate the snapshot context and fill it in */
Alex Elder6a523252012-07-19 17:12:59 -05001027
Alex Elder103a1502012-08-02 11:29:45 -05001028 snap_count = le32_to_cpu(ondisk->snap_count);
Alex Elderbb23e372013-05-06 09:51:29 -05001029 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
1030 if (!snapc)
1031 goto out_err;
1032 snapc->seq = le64_to_cpu(ondisk->snap_seq);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001033 if (snap_count) {
Alex Elderbb23e372013-05-06 09:51:29 -05001034 struct rbd_image_snap_ondisk *snaps;
Alex Elderf785cc12012-08-23 23:22:06 -05001035 u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len);
1036
Alex Elderbb23e372013-05-06 09:51:29 -05001037 /* We'll keep a copy of the snapshot names... */
Alex Elder621901d2012-08-23 23:22:06 -05001038
Alex Elderbb23e372013-05-06 09:51:29 -05001039 if (snap_names_len > (u64)SIZE_MAX)
1040 goto out_2big;
1041 snap_names = kmalloc(snap_names_len, GFP_KERNEL);
1042 if (!snap_names)
Alex Elder6a523252012-07-19 17:12:59 -05001043 goto out_err;
Alex Elderbb23e372013-05-06 09:51:29 -05001044
1045 /* ...as well as the array of their sizes. */
Markus Elfring88a25a52016-09-11 12:21:25 +02001046 snap_sizes = kmalloc_array(snap_count,
1047 sizeof(*header->snap_sizes),
1048 GFP_KERNEL);
Alex Elderbb23e372013-05-06 09:51:29 -05001049 if (!snap_sizes)
1050 goto out_err;
1051
Alex Elderf785cc12012-08-23 23:22:06 -05001052 /*
Alex Elderbb23e372013-05-06 09:51:29 -05001053 * Copy the names, and fill in each snapshot's id
1054 * and size.
1055 *
Alex Elder99a41eb2013-05-06 09:51:30 -05001056 * Note that rbd_dev_v1_header_info() guarantees the
Alex Elderbb23e372013-05-06 09:51:29 -05001057 * ondisk buffer we're working with has
Alex Elderf785cc12012-08-23 23:22:06 -05001058 * snap_names_len bytes beyond the end of the
1059 * snapshot id array, this memcpy() is safe.
1060 */
Alex Elderbb23e372013-05-06 09:51:29 -05001061 memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len);
1062 snaps = ondisk->snaps;
1063 for (i = 0; i < snap_count; i++) {
1064 snapc->snaps[i] = le64_to_cpu(snaps[i].id);
1065 snap_sizes[i] = le64_to_cpu(snaps[i].image_size);
1066 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001067 }
Alex Elder849b4262012-07-09 21:04:24 -05001068
Alex Elderbb23e372013-05-06 09:51:29 -05001069 /* We won't fail any more, fill in the header */
Alex Elder6a523252012-07-19 17:12:59 -05001070
Alex Elderbb23e372013-05-06 09:51:29 -05001071 if (first_time) {
1072 header->object_prefix = object_prefix;
1073 header->obj_order = ondisk->options.order;
Ilya Dryomov263423f2017-01-25 18:16:22 +01001074 rbd_init_layout(rbd_dev);
Alex Elder662518b2013-05-06 09:51:29 -05001075 } else {
1076 ceph_put_snap_context(header->snapc);
1077 kfree(header->snap_names);
1078 kfree(header->snap_sizes);
Alex Elderbb23e372013-05-06 09:51:29 -05001079 }
1080
1081 /* The remaining fields always get updated (when we refresh) */
Alex Elder621901d2012-08-23 23:22:06 -05001082
Alex Elderf84344f2012-08-31 17:29:51 -05001083 header->image_size = le64_to_cpu(ondisk->image_size);
Alex Elderbb23e372013-05-06 09:51:29 -05001084 header->snapc = snapc;
1085 header->snap_names = snap_names;
1086 header->snap_sizes = snap_sizes;
Alex Elder468521c2013-04-26 09:43:47 -05001087
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001088 return 0;
Alex Elderbb23e372013-05-06 09:51:29 -05001089out_2big:
1090 ret = -EIO;
Alex Elder6a523252012-07-19 17:12:59 -05001091out_err:
Alex Elderbb23e372013-05-06 09:51:29 -05001092 kfree(snap_sizes);
1093 kfree(snap_names);
1094 ceph_put_snap_context(snapc);
1095 kfree(object_prefix);
Alex Elderccece232012-07-10 20:30:10 -05001096
Alex Elderbb23e372013-05-06 09:51:29 -05001097 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001098}
1099
Alex Elder9682fc62013-04-30 00:44:33 -05001100static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which)
1101{
1102 const char *snap_name;
1103
1104 rbd_assert(which < rbd_dev->header.snapc->num_snaps);
1105
1106 /* Skip over names until we find the one we are looking for */
1107
1108 snap_name = rbd_dev->header.snap_names;
1109 while (which--)
1110 snap_name += strlen(snap_name) + 1;
1111
1112 return kstrdup(snap_name, GFP_KERNEL);
1113}
1114
Alex Elder30d1cff2013-05-01 12:43:03 -05001115/*
1116 * Snapshot id comparison function for use with qsort()/bsearch().
1117 * Note that result is for snapshots in *descending* order.
1118 */
1119static int snapid_compare_reverse(const void *s1, const void *s2)
1120{
1121 u64 snap_id1 = *(u64 *)s1;
1122 u64 snap_id2 = *(u64 *)s2;
1123
1124 if (snap_id1 < snap_id2)
1125 return 1;
1126 return snap_id1 == snap_id2 ? 0 : -1;
1127}
1128
1129/*
1130 * Search a snapshot context to see if the given snapshot id is
1131 * present.
1132 *
1133 * Returns the position of the snapshot id in the array if it's found,
1134 * or BAD_SNAP_INDEX otherwise.
1135 *
1136 * Note: The snapshot array is in kept sorted (by the osd) in
1137 * reverse order, highest snapshot id first.
1138 */
Alex Elder9682fc62013-04-30 00:44:33 -05001139static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id)
1140{
1141 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
Alex Elder30d1cff2013-05-01 12:43:03 -05001142 u64 *found;
Alex Elder9682fc62013-04-30 00:44:33 -05001143
Alex Elder30d1cff2013-05-01 12:43:03 -05001144 found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps,
1145 sizeof (snap_id), snapid_compare_reverse);
Alex Elder9682fc62013-04-30 00:44:33 -05001146
Alex Elder30d1cff2013-05-01 12:43:03 -05001147 return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX;
Alex Elder9682fc62013-04-30 00:44:33 -05001148}
1149
Alex Elder2ad3d712013-04-30 00:44:33 -05001150static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev,
1151 u64 snap_id)
Alex Elder54cac612013-04-30 00:44:33 -05001152{
1153 u32 which;
Josh Durginda6a6b62013-09-04 17:57:31 -07001154 const char *snap_name;
Alex Elder54cac612013-04-30 00:44:33 -05001155
1156 which = rbd_dev_snap_index(rbd_dev, snap_id);
1157 if (which == BAD_SNAP_INDEX)
Josh Durginda6a6b62013-09-04 17:57:31 -07001158 return ERR_PTR(-ENOENT);
Alex Elder54cac612013-04-30 00:44:33 -05001159
Josh Durginda6a6b62013-09-04 17:57:31 -07001160 snap_name = _rbd_dev_v1_snap_name(rbd_dev, which);
1161 return snap_name ? snap_name : ERR_PTR(-ENOMEM);
Alex Elder54cac612013-04-30 00:44:33 -05001162}
1163
Alex Elder9e15b772012-10-30 19:40:33 -05001164static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id)
1165{
Alex Elder9e15b772012-10-30 19:40:33 -05001166 if (snap_id == CEPH_NOSNAP)
1167 return RBD_SNAP_HEAD_NAME;
1168
Alex Elder54cac612013-04-30 00:44:33 -05001169 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1170 if (rbd_dev->image_format == 1)
1171 return rbd_dev_v1_snap_name(rbd_dev, snap_id);
Alex Elder9e15b772012-10-30 19:40:33 -05001172
Alex Elder54cac612013-04-30 00:44:33 -05001173 return rbd_dev_v2_snap_name(rbd_dev, snap_id);
Alex Elder9e15b772012-10-30 19:40:33 -05001174}
1175
Alex Elder2ad3d712013-04-30 00:44:33 -05001176static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
1177 u64 *snap_size)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001178{
Alex Elder2ad3d712013-04-30 00:44:33 -05001179 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1180 if (snap_id == CEPH_NOSNAP) {
1181 *snap_size = rbd_dev->header.image_size;
1182 } else if (rbd_dev->image_format == 1) {
1183 u32 which;
Alex Elder00f1f362012-02-07 12:03:36 -06001184
Alex Elder2ad3d712013-04-30 00:44:33 -05001185 which = rbd_dev_snap_index(rbd_dev, snap_id);
1186 if (which == BAD_SNAP_INDEX)
1187 return -ENOENT;
Alex Elder00f1f362012-02-07 12:03:36 -06001188
Alex Elder2ad3d712013-04-30 00:44:33 -05001189 *snap_size = rbd_dev->header.snap_sizes[which];
1190 } else {
1191 u64 size = 0;
1192 int ret;
1193
1194 ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size);
1195 if (ret)
1196 return ret;
1197
1198 *snap_size = size;
1199 }
1200 return 0;
1201}
1202
1203static int rbd_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
1204 u64 *snap_features)
1205{
1206 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1207 if (snap_id == CEPH_NOSNAP) {
1208 *snap_features = rbd_dev->header.features;
1209 } else if (rbd_dev->image_format == 1) {
1210 *snap_features = 0; /* No features for format 1 */
1211 } else {
1212 u64 features = 0;
1213 int ret;
1214
1215 ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, &features);
1216 if (ret)
1217 return ret;
1218
1219 *snap_features = features;
1220 }
1221 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001222}
1223
Alex Elderd1cf5782013-04-27 09:59:30 -05001224static int rbd_dev_mapping_set(struct rbd_device *rbd_dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001225{
Alex Elder8f4b7d92013-05-06 07:40:30 -05001226 u64 snap_id = rbd_dev->spec->snap_id;
Alex Elder2ad3d712013-04-30 00:44:33 -05001227 u64 size = 0;
1228 u64 features = 0;
1229 int ret;
Alex Elder8b0241f2013-04-25 23:15:08 -05001230
Alex Elder2ad3d712013-04-30 00:44:33 -05001231 ret = rbd_snap_size(rbd_dev, snap_id, &size);
1232 if (ret)
1233 return ret;
1234 ret = rbd_snap_features(rbd_dev, snap_id, &features);
1235 if (ret)
1236 return ret;
1237
1238 rbd_dev->mapping.size = size;
1239 rbd_dev->mapping.features = features;
1240
Alex Elder8b0241f2013-04-25 23:15:08 -05001241 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001242}
1243
Alex Elderd1cf5782013-04-27 09:59:30 -05001244static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev)
1245{
1246 rbd_dev->mapping.size = 0;
1247 rbd_dev->mapping.features = 0;
Alex Elder200a6a82013-04-28 23:32:34 -05001248}
1249
Alex Elder65ccfe22012-08-09 10:33:26 -07001250static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset)
1251{
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +01001252 u64 segment_size = rbd_obj_bytes(&rbd_dev->header);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001253
Alex Elder65ccfe22012-08-09 10:33:26 -07001254 return offset & (segment_size - 1);
1255}
1256
1257static u64 rbd_segment_length(struct rbd_device *rbd_dev,
1258 u64 offset, u64 length)
1259{
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +01001260 u64 segment_size = rbd_obj_bytes(&rbd_dev->header);
Alex Elder65ccfe22012-08-09 10:33:26 -07001261
1262 offset &= segment_size - 1;
1263
Alex Elderaafb2302012-09-06 16:00:54 -05001264 rbd_assert(length <= U64_MAX - offset);
Alex Elder65ccfe22012-08-09 10:33:26 -07001265 if (offset + length > segment_size)
1266 length = segment_size - offset;
1267
1268 return length;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001269}
1270
1271/*
1272 * bio helpers
1273 */
1274
1275static void bio_chain_put(struct bio *chain)
1276{
1277 struct bio *tmp;
1278
1279 while (chain) {
1280 tmp = chain;
1281 chain = chain->bi_next;
1282 bio_put(tmp);
1283 }
1284}
1285
1286/*
1287 * zeros a bio chain, starting at specific offset
1288 */
1289static void zero_bio_chain(struct bio *chain, int start_ofs)
1290{
Kent Overstreet79886132013-11-23 17:19:00 -08001291 struct bio_vec bv;
1292 struct bvec_iter iter;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001293 unsigned long flags;
1294 void *buf;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001295 int pos = 0;
1296
1297 while (chain) {
Kent Overstreet79886132013-11-23 17:19:00 -08001298 bio_for_each_segment(bv, chain, iter) {
1299 if (pos + bv.bv_len > start_ofs) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001300 int remainder = max(start_ofs - pos, 0);
Kent Overstreet79886132013-11-23 17:19:00 -08001301 buf = bvec_kmap_irq(&bv, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001302 memset(buf + remainder, 0,
Kent Overstreet79886132013-11-23 17:19:00 -08001303 bv.bv_len - remainder);
1304 flush_dcache_page(bv.bv_page);
Dan Carpenter85b5aaa2010-10-11 21:15:11 +02001305 bvec_kunmap_irq(buf, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001306 }
Kent Overstreet79886132013-11-23 17:19:00 -08001307 pos += bv.bv_len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001308 }
1309
1310 chain = chain->bi_next;
1311 }
1312}
1313
1314/*
Alex Elderb9434c52013-04-19 15:34:50 -05001315 * similar to zero_bio_chain(), zeros data defined by a page array,
1316 * starting at the given byte offset from the start of the array and
1317 * continuing up to the given end offset. The pages array is
1318 * assumed to be big enough to hold all bytes up to the end.
1319 */
1320static void zero_pages(struct page **pages, u64 offset, u64 end)
1321{
1322 struct page **page = &pages[offset >> PAGE_SHIFT];
1323
1324 rbd_assert(end > offset);
1325 rbd_assert(end - offset <= (u64)SIZE_MAX);
1326 while (offset < end) {
1327 size_t page_offset;
1328 size_t length;
1329 unsigned long flags;
1330 void *kaddr;
1331
Geert Uytterhoeven491205a2013-05-13 20:35:37 -05001332 page_offset = offset & ~PAGE_MASK;
1333 length = min_t(size_t, PAGE_SIZE - page_offset, end - offset);
Alex Elderb9434c52013-04-19 15:34:50 -05001334 local_irq_save(flags);
1335 kaddr = kmap_atomic(*page);
1336 memset(kaddr + page_offset, 0, length);
Alex Eldere2156052013-05-22 20:54:25 -05001337 flush_dcache_page(*page);
Alex Elderb9434c52013-04-19 15:34:50 -05001338 kunmap_atomic(kaddr);
1339 local_irq_restore(flags);
1340
1341 offset += length;
1342 page++;
1343 }
1344}
1345
1346/*
Alex Elderf7760da2012-10-20 22:17:27 -05001347 * Clone a portion of a bio, starting at the given byte offset
1348 * and continuing for the number of bytes indicated.
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001349 */
Alex Elderf7760da2012-10-20 22:17:27 -05001350static struct bio *bio_clone_range(struct bio *bio_src,
1351 unsigned int offset,
1352 unsigned int len,
1353 gfp_t gfpmask)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001354{
Alex Elderf7760da2012-10-20 22:17:27 -05001355 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001356
Kent Overstreet5341a6272013-08-07 14:31:11 -07001357 bio = bio_clone(bio_src, gfpmask);
Alex Elderf7760da2012-10-20 22:17:27 -05001358 if (!bio)
1359 return NULL; /* ENOMEM */
1360
Kent Overstreet5341a6272013-08-07 14:31:11 -07001361 bio_advance(bio, offset);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001362 bio->bi_iter.bi_size = len;
Alex Elder542582f2012-08-09 10:33:25 -07001363
Alex Elderf7760da2012-10-20 22:17:27 -05001364 return bio;
1365}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001366
Alex Elderf7760da2012-10-20 22:17:27 -05001367/*
1368 * Clone a portion of a bio chain, starting at the given byte offset
1369 * into the first bio in the source chain and continuing for the
1370 * number of bytes indicated. The result is another bio chain of
1371 * exactly the given length, or a null pointer on error.
1372 *
1373 * The bio_src and offset parameters are both in-out. On entry they
1374 * refer to the first source bio and the offset into that bio where
1375 * the start of data to be cloned is located.
1376 *
1377 * On return, bio_src is updated to refer to the bio in the source
1378 * chain that contains first un-cloned byte, and *offset will
1379 * contain the offset of that byte within that bio.
1380 */
1381static struct bio *bio_chain_clone_range(struct bio **bio_src,
1382 unsigned int *offset,
1383 unsigned int len,
1384 gfp_t gfpmask)
1385{
1386 struct bio *bi = *bio_src;
1387 unsigned int off = *offset;
1388 struct bio *chain = NULL;
1389 struct bio **end;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001390
Alex Elderf7760da2012-10-20 22:17:27 -05001391 /* Build up a chain of clone bios up to the limit */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001392
Kent Overstreet4f024f32013-10-11 15:44:27 -07001393 if (!bi || off >= bi->bi_iter.bi_size || !len)
Alex Elderf7760da2012-10-20 22:17:27 -05001394 return NULL; /* Nothing to clone */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001395
Alex Elderf7760da2012-10-20 22:17:27 -05001396 end = &chain;
1397 while (len) {
1398 unsigned int bi_size;
1399 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001400
Alex Elderf5400b72012-11-01 10:17:15 -05001401 if (!bi) {
1402 rbd_warn(NULL, "bio_chain exhausted with %u left", len);
Alex Elderf7760da2012-10-20 22:17:27 -05001403 goto out_err; /* EINVAL; ran out of bio's */
Alex Elderf5400b72012-11-01 10:17:15 -05001404 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07001405 bi_size = min_t(unsigned int, bi->bi_iter.bi_size - off, len);
Alex Elderf7760da2012-10-20 22:17:27 -05001406 bio = bio_clone_range(bi, off, bi_size, gfpmask);
1407 if (!bio)
1408 goto out_err; /* ENOMEM */
1409
1410 *end = bio;
1411 end = &bio->bi_next;
1412
1413 off += bi_size;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001414 if (off == bi->bi_iter.bi_size) {
Alex Elderf7760da2012-10-20 22:17:27 -05001415 bi = bi->bi_next;
1416 off = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001417 }
Alex Elderf7760da2012-10-20 22:17:27 -05001418 len -= bi_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001419 }
Alex Elderf7760da2012-10-20 22:17:27 -05001420 *bio_src = bi;
1421 *offset = off;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001422
Alex Elderf7760da2012-10-20 22:17:27 -05001423 return chain;
1424out_err:
1425 bio_chain_put(chain);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001426
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001427 return NULL;
1428}
1429
Alex Elder926f9b32013-02-11 12:33:24 -06001430/*
1431 * The default/initial value for all object request flags is 0. For
1432 * each flag, once its value is set to 1 it is never reset to 0
1433 * again.
1434 */
Alex Elder6365d332013-02-11 12:33:24 -06001435static void obj_request_img_data_set(struct rbd_obj_request *obj_request)
1436{
1437 if (test_and_set_bit(OBJ_REQ_IMG_DATA, &obj_request->flags)) {
Alex Elder6365d332013-02-11 12:33:24 -06001438 struct rbd_device *rbd_dev;
1439
Alex Elder57acbaa2013-02-11 12:33:24 -06001440 rbd_dev = obj_request->img_request->rbd_dev;
Ilya Dryomov9584d502014-07-11 12:11:20 +04001441 rbd_warn(rbd_dev, "obj_request %p already marked img_data",
Alex Elder6365d332013-02-11 12:33:24 -06001442 obj_request);
1443 }
1444}
1445
1446static bool obj_request_img_data_test(struct rbd_obj_request *obj_request)
1447{
1448 smp_mb();
1449 return test_bit(OBJ_REQ_IMG_DATA, &obj_request->flags) != 0;
1450}
1451
Alex Elder57acbaa2013-02-11 12:33:24 -06001452static void obj_request_done_set(struct rbd_obj_request *obj_request)
1453{
1454 if (test_and_set_bit(OBJ_REQ_DONE, &obj_request->flags)) {
1455 struct rbd_device *rbd_dev = NULL;
1456
1457 if (obj_request_img_data_test(obj_request))
1458 rbd_dev = obj_request->img_request->rbd_dev;
Ilya Dryomov9584d502014-07-11 12:11:20 +04001459 rbd_warn(rbd_dev, "obj_request %p already marked done",
Alex Elder57acbaa2013-02-11 12:33:24 -06001460 obj_request);
1461 }
1462}
1463
1464static bool obj_request_done_test(struct rbd_obj_request *obj_request)
1465{
1466 smp_mb();
1467 return test_bit(OBJ_REQ_DONE, &obj_request->flags) != 0;
1468}
1469
Alex Elder5679c592013-02-11 12:33:24 -06001470/*
1471 * This sets the KNOWN flag after (possibly) setting the EXISTS
1472 * flag. The latter is set based on the "exists" value provided.
1473 *
1474 * Note that for our purposes once an object exists it never goes
1475 * away again. It's possible that the response from two existence
1476 * checks are separated by the creation of the target object, and
1477 * the first ("doesn't exist") response arrives *after* the second
1478 * ("does exist"). In that case we ignore the second one.
1479 */
1480static void obj_request_existence_set(struct rbd_obj_request *obj_request,
1481 bool exists)
1482{
1483 if (exists)
1484 set_bit(OBJ_REQ_EXISTS, &obj_request->flags);
1485 set_bit(OBJ_REQ_KNOWN, &obj_request->flags);
1486 smp_mb();
1487}
1488
1489static bool obj_request_known_test(struct rbd_obj_request *obj_request)
1490{
1491 smp_mb();
1492 return test_bit(OBJ_REQ_KNOWN, &obj_request->flags) != 0;
1493}
1494
1495static bool obj_request_exists_test(struct rbd_obj_request *obj_request)
1496{
1497 smp_mb();
1498 return test_bit(OBJ_REQ_EXISTS, &obj_request->flags) != 0;
1499}
1500
Ilya Dryomov96385562014-06-10 13:53:29 +04001501static bool obj_request_overlaps_parent(struct rbd_obj_request *obj_request)
1502{
1503 struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev;
1504
1505 return obj_request->img_offset <
1506 round_up(rbd_dev->parent_overlap, rbd_obj_bytes(&rbd_dev->header));
1507}
1508
Alex Elderbf0d5f502012-11-22 00:00:08 -06001509static void rbd_obj_request_get(struct rbd_obj_request *obj_request)
1510{
Alex Elder37206ee2013-02-20 17:32:08 -06001511 dout("%s: obj %p (was %d)\n", __func__, obj_request,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01001512 kref_read(&obj_request->kref));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001513 kref_get(&obj_request->kref);
1514}
1515
1516static void rbd_obj_request_destroy(struct kref *kref);
1517static void rbd_obj_request_put(struct rbd_obj_request *obj_request)
1518{
1519 rbd_assert(obj_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001520 dout("%s: obj %p (was %d)\n", __func__, obj_request,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01001521 kref_read(&obj_request->kref));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001522 kref_put(&obj_request->kref, rbd_obj_request_destroy);
1523}
1524
Alex Elder0f2d5be2014-04-26 14:21:44 +04001525static void rbd_img_request_get(struct rbd_img_request *img_request)
1526{
1527 dout("%s: img %p (was %d)\n", __func__, img_request,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01001528 kref_read(&img_request->kref));
Alex Elder0f2d5be2014-04-26 14:21:44 +04001529 kref_get(&img_request->kref);
1530}
1531
Alex Eldere93f3152013-05-08 22:50:04 -05001532static bool img_request_child_test(struct rbd_img_request *img_request);
1533static void rbd_parent_request_destroy(struct kref *kref);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001534static void rbd_img_request_destroy(struct kref *kref);
1535static void rbd_img_request_put(struct rbd_img_request *img_request)
1536{
1537 rbd_assert(img_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001538 dout("%s: img %p (was %d)\n", __func__, img_request,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01001539 kref_read(&img_request->kref));
Alex Eldere93f3152013-05-08 22:50:04 -05001540 if (img_request_child_test(img_request))
1541 kref_put(&img_request->kref, rbd_parent_request_destroy);
1542 else
1543 kref_put(&img_request->kref, rbd_img_request_destroy);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001544}
1545
1546static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request,
1547 struct rbd_obj_request *obj_request)
1548{
Alex Elder25dcf952013-01-25 17:08:55 -06001549 rbd_assert(obj_request->img_request == NULL);
1550
Alex Elderb155e862013-04-15 14:50:37 -05001551 /* Image request now owns object's original reference */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001552 obj_request->img_request = img_request;
Alex Elder25dcf952013-01-25 17:08:55 -06001553 obj_request->which = img_request->obj_request_count;
Alex Elder6365d332013-02-11 12:33:24 -06001554 rbd_assert(!obj_request_img_data_test(obj_request));
1555 obj_request_img_data_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001556 rbd_assert(obj_request->which != BAD_WHICH);
Alex Elder25dcf952013-01-25 17:08:55 -06001557 img_request->obj_request_count++;
1558 list_add_tail(&obj_request->links, &img_request->obj_requests);
Alex Elder37206ee2013-02-20 17:32:08 -06001559 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1560 obj_request->which);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001561}
1562
1563static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request,
1564 struct rbd_obj_request *obj_request)
1565{
1566 rbd_assert(obj_request->which != BAD_WHICH);
Alex Elder25dcf952013-01-25 17:08:55 -06001567
Alex Elder37206ee2013-02-20 17:32:08 -06001568 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1569 obj_request->which);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001570 list_del(&obj_request->links);
Alex Elder25dcf952013-01-25 17:08:55 -06001571 rbd_assert(img_request->obj_request_count > 0);
1572 img_request->obj_request_count--;
1573 rbd_assert(obj_request->which == img_request->obj_request_count);
1574 obj_request->which = BAD_WHICH;
Alex Elder6365d332013-02-11 12:33:24 -06001575 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001576 rbd_assert(obj_request->img_request == img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001577 obj_request->img_request = NULL;
Alex Elder25dcf952013-01-25 17:08:55 -06001578 obj_request->callback = NULL;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001579 rbd_obj_request_put(obj_request);
1580}
1581
1582static bool obj_request_type_valid(enum obj_request_type type)
1583{
1584 switch (type) {
Alex Elder9969ebc2013-01-18 12:31:10 -06001585 case OBJ_REQUEST_NODATA:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001586 case OBJ_REQUEST_BIO:
Alex Elder788e2df2013-01-17 12:25:27 -06001587 case OBJ_REQUEST_PAGES:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001588 return true;
1589 default:
1590 return false;
1591 }
1592}
1593
Ilya Dryomov4a17dad2016-09-13 21:08:10 +02001594static void rbd_img_obj_callback(struct rbd_obj_request *obj_request);
1595
Ilya Dryomov980917f2016-09-12 18:59:42 +02001596static void rbd_obj_request_submit(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001597{
Ilya Dryomov980917f2016-09-12 18:59:42 +02001598 struct ceph_osd_request *osd_req = obj_request->osd_req;
1599
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01001600 dout("%s %p object_no %016llx %llu~%llu osd_req %p\n", __func__,
1601 obj_request, obj_request->object_no, obj_request->offset,
Ilya Dryomov67e2b652017-01-25 18:16:22 +01001602 obj_request->length, osd_req);
Ilya Dryomov4a17dad2016-09-13 21:08:10 +02001603 if (obj_request_img_data_test(obj_request)) {
1604 WARN_ON(obj_request->callback != rbd_img_obj_callback);
1605 rbd_img_request_get(obj_request->img_request);
1606 }
Ilya Dryomov980917f2016-09-12 18:59:42 +02001607 ceph_osdc_start_request(osd_req->r_osdc, osd_req, false);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001608}
1609
1610static void rbd_img_request_complete(struct rbd_img_request *img_request)
1611{
Alex Elder55f27e02013-04-10 12:34:25 -05001612
Alex Elder37206ee2013-02-20 17:32:08 -06001613 dout("%s: img %p\n", __func__, img_request);
Alex Elder55f27e02013-04-10 12:34:25 -05001614
1615 /*
1616 * If no error occurred, compute the aggregate transfer
1617 * count for the image request. We could instead use
1618 * atomic64_cmpxchg() to update it as each object request
1619 * completes; not clear which way is better off hand.
1620 */
1621 if (!img_request->result) {
1622 struct rbd_obj_request *obj_request;
1623 u64 xferred = 0;
1624
1625 for_each_obj_request(img_request, obj_request)
1626 xferred += obj_request->xferred;
1627 img_request->xferred = xferred;
1628 }
1629
Alex Elderbf0d5f502012-11-22 00:00:08 -06001630 if (img_request->callback)
1631 img_request->callback(img_request);
1632 else
1633 rbd_img_request_put(img_request);
1634}
1635
Alex Elder0c425242013-02-08 09:55:49 -06001636/*
1637 * The default/initial value for all image request flags is 0. Each
1638 * is conditionally set to 1 at image request initialization time
1639 * and currently never change thereafter.
1640 */
1641static void img_request_write_set(struct rbd_img_request *img_request)
1642{
1643 set_bit(IMG_REQ_WRITE, &img_request->flags);
1644 smp_mb();
1645}
1646
1647static bool img_request_write_test(struct rbd_img_request *img_request)
1648{
1649 smp_mb();
1650 return test_bit(IMG_REQ_WRITE, &img_request->flags) != 0;
1651}
1652
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001653/*
1654 * Set the discard flag when the img_request is an discard request
1655 */
1656static void img_request_discard_set(struct rbd_img_request *img_request)
1657{
1658 set_bit(IMG_REQ_DISCARD, &img_request->flags);
1659 smp_mb();
1660}
1661
1662static bool img_request_discard_test(struct rbd_img_request *img_request)
1663{
1664 smp_mb();
1665 return test_bit(IMG_REQ_DISCARD, &img_request->flags) != 0;
1666}
1667
Alex Elder9849e982013-01-24 16:13:36 -06001668static void img_request_child_set(struct rbd_img_request *img_request)
1669{
1670 set_bit(IMG_REQ_CHILD, &img_request->flags);
1671 smp_mb();
1672}
1673
Alex Eldere93f3152013-05-08 22:50:04 -05001674static void img_request_child_clear(struct rbd_img_request *img_request)
1675{
1676 clear_bit(IMG_REQ_CHILD, &img_request->flags);
1677 smp_mb();
1678}
1679
Alex Elder9849e982013-01-24 16:13:36 -06001680static bool img_request_child_test(struct rbd_img_request *img_request)
1681{
1682 smp_mb();
1683 return test_bit(IMG_REQ_CHILD, &img_request->flags) != 0;
1684}
1685
Alex Elderd0b2e942013-01-24 16:13:36 -06001686static void img_request_layered_set(struct rbd_img_request *img_request)
1687{
1688 set_bit(IMG_REQ_LAYERED, &img_request->flags);
1689 smp_mb();
1690}
1691
Alex Eldera2acd002013-05-08 22:50:04 -05001692static void img_request_layered_clear(struct rbd_img_request *img_request)
1693{
1694 clear_bit(IMG_REQ_LAYERED, &img_request->flags);
1695 smp_mb();
1696}
1697
Alex Elderd0b2e942013-01-24 16:13:36 -06001698static bool img_request_layered_test(struct rbd_img_request *img_request)
1699{
1700 smp_mb();
1701 return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0;
1702}
1703
Josh Durgin3b434a2a2014-04-04 17:32:15 -07001704static enum obj_operation_type
1705rbd_img_request_op_type(struct rbd_img_request *img_request)
1706{
1707 if (img_request_write_test(img_request))
1708 return OBJ_OP_WRITE;
1709 else if (img_request_discard_test(img_request))
1710 return OBJ_OP_DISCARD;
1711 else
1712 return OBJ_OP_READ;
1713}
1714
Alex Elder6e2a4502013-03-27 09:16:30 -05001715static void
1716rbd_img_obj_request_read_callback(struct rbd_obj_request *obj_request)
1717{
Alex Elderb9434c52013-04-19 15:34:50 -05001718 u64 xferred = obj_request->xferred;
1719 u64 length = obj_request->length;
1720
Alex Elder6e2a4502013-03-27 09:16:30 -05001721 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1722 obj_request, obj_request->img_request, obj_request->result,
Alex Elderb9434c52013-04-19 15:34:50 -05001723 xferred, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001724 /*
Josh Durgin17c1cc12013-08-26 17:55:38 -07001725 * ENOENT means a hole in the image. We zero-fill the entire
1726 * length of the request. A short read also implies zero-fill
1727 * to the end of the request. An error requires the whole
1728 * length of the request to be reported finished with an error
1729 * to the block layer. In each case we update the xferred
1730 * count to indicate the whole request was satisfied.
Alex Elder6e2a4502013-03-27 09:16:30 -05001731 */
Alex Elderb9434c52013-04-19 15:34:50 -05001732 rbd_assert(obj_request->type != OBJ_REQUEST_NODATA);
Alex Elder6e2a4502013-03-27 09:16:30 -05001733 if (obj_request->result == -ENOENT) {
Alex Elderb9434c52013-04-19 15:34:50 -05001734 if (obj_request->type == OBJ_REQUEST_BIO)
1735 zero_bio_chain(obj_request->bio_list, 0);
1736 else
1737 zero_pages(obj_request->pages, 0, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001738 obj_request->result = 0;
Alex Elderb9434c52013-04-19 15:34:50 -05001739 } else if (xferred < length && !obj_request->result) {
1740 if (obj_request->type == OBJ_REQUEST_BIO)
1741 zero_bio_chain(obj_request->bio_list, xferred);
1742 else
1743 zero_pages(obj_request->pages, xferred, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001744 }
Josh Durgin17c1cc12013-08-26 17:55:38 -07001745 obj_request->xferred = length;
Alex Elder6e2a4502013-03-27 09:16:30 -05001746 obj_request_done_set(obj_request);
1747}
1748
Alex Elderbf0d5f502012-11-22 00:00:08 -06001749static void rbd_obj_request_complete(struct rbd_obj_request *obj_request)
1750{
Alex Elder37206ee2013-02-20 17:32:08 -06001751 dout("%s: obj %p cb %p\n", __func__, obj_request,
1752 obj_request->callback);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001753 if (obj_request->callback)
1754 obj_request->callback(obj_request);
Alex Elder788e2df2013-01-17 12:25:27 -06001755 else
1756 complete_all(&obj_request->completion);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001757}
1758
Ilya Dryomov0dcc6852016-09-26 15:43:52 +02001759static void rbd_obj_request_error(struct rbd_obj_request *obj_request, int err)
1760{
1761 obj_request->result = err;
1762 obj_request->xferred = 0;
1763 /*
1764 * kludge - mirror rbd_obj_request_submit() to match a put in
1765 * rbd_img_obj_callback()
1766 */
1767 if (obj_request_img_data_test(obj_request)) {
1768 WARN_ON(obj_request->callback != rbd_img_obj_callback);
1769 rbd_img_request_get(obj_request->img_request);
1770 }
1771 obj_request_done_set(obj_request);
1772 rbd_obj_request_complete(obj_request);
1773}
1774
Alex Elderc47f9372013-02-26 14:23:07 -06001775static void rbd_osd_read_callback(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001776{
Alex Elder57acbaa2013-02-11 12:33:24 -06001777 struct rbd_img_request *img_request = NULL;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001778 struct rbd_device *rbd_dev = NULL;
Alex Elder57acbaa2013-02-11 12:33:24 -06001779 bool layered = false;
1780
1781 if (obj_request_img_data_test(obj_request)) {
1782 img_request = obj_request->img_request;
1783 layered = img_request && img_request_layered_test(img_request);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001784 rbd_dev = img_request->rbd_dev;
Alex Elder57acbaa2013-02-11 12:33:24 -06001785 }
Alex Elder8b3e1a52013-01-24 16:13:36 -06001786
1787 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1788 obj_request, img_request, obj_request->result,
1789 obj_request->xferred, obj_request->length);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001790 if (layered && obj_request->result == -ENOENT &&
1791 obj_request->img_offset < rbd_dev->parent_overlap)
Alex Elder8b3e1a52013-01-24 16:13:36 -06001792 rbd_img_parent_read(obj_request);
1793 else if (img_request)
Alex Elder6e2a4502013-03-27 09:16:30 -05001794 rbd_img_obj_request_read_callback(obj_request);
1795 else
1796 obj_request_done_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001797}
1798
Alex Elderc47f9372013-02-26 14:23:07 -06001799static void rbd_osd_write_callback(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001800{
Sage Weil1b83bef2013-02-25 16:11:12 -08001801 dout("%s: obj %p result %d %llu\n", __func__, obj_request,
1802 obj_request->result, obj_request->length);
1803 /*
Alex Elder8b3e1a52013-01-24 16:13:36 -06001804 * There is no such thing as a successful short write. Set
1805 * it to our originally-requested length.
Sage Weil1b83bef2013-02-25 16:11:12 -08001806 */
1807 obj_request->xferred = obj_request->length;
Alex Elder07741302013-02-05 23:41:50 -06001808 obj_request_done_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001809}
1810
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001811static void rbd_osd_discard_callback(struct rbd_obj_request *obj_request)
1812{
1813 dout("%s: obj %p result %d %llu\n", __func__, obj_request,
1814 obj_request->result, obj_request->length);
1815 /*
1816 * There is no such thing as a successful short discard. Set
1817 * it to our originally-requested length.
1818 */
1819 obj_request->xferred = obj_request->length;
Josh Durgind0265de2014-04-07 16:54:10 -07001820 /* discarding a non-existent object is not a problem */
1821 if (obj_request->result == -ENOENT)
1822 obj_request->result = 0;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001823 obj_request_done_set(obj_request);
1824}
1825
Alex Elderfbfab532013-02-08 09:55:48 -06001826/*
1827 * For a simple stat call there's nothing to do. We'll do more if
1828 * this is part of a write sequence for a layered image.
1829 */
Alex Elderc47f9372013-02-26 14:23:07 -06001830static void rbd_osd_stat_callback(struct rbd_obj_request *obj_request)
Alex Elderfbfab532013-02-08 09:55:48 -06001831{
Alex Elder37206ee2013-02-20 17:32:08 -06001832 dout("%s: obj %p\n", __func__, obj_request);
Alex Elderfbfab532013-02-08 09:55:48 -06001833 obj_request_done_set(obj_request);
1834}
1835
Ilya Dryomov27617132015-07-16 17:36:11 +03001836static void rbd_osd_call_callback(struct rbd_obj_request *obj_request)
1837{
1838 dout("%s: obj %p\n", __func__, obj_request);
1839
1840 if (obj_request_img_data_test(obj_request))
1841 rbd_osd_copyup_callback(obj_request);
1842 else
1843 obj_request_done_set(obj_request);
1844}
1845
Ilya Dryomov85e084f2016-04-28 16:07:24 +02001846static void rbd_osd_req_callback(struct ceph_osd_request *osd_req)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001847{
1848 struct rbd_obj_request *obj_request = osd_req->r_priv;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001849 u16 opcode;
1850
Ilya Dryomov85e084f2016-04-28 16:07:24 +02001851 dout("%s: osd_req %p\n", __func__, osd_req);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001852 rbd_assert(osd_req == obj_request->osd_req);
Alex Elder57acbaa2013-02-11 12:33:24 -06001853 if (obj_request_img_data_test(obj_request)) {
1854 rbd_assert(obj_request->img_request);
1855 rbd_assert(obj_request->which != BAD_WHICH);
1856 } else {
1857 rbd_assert(obj_request->which == BAD_WHICH);
1858 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06001859
Sage Weil1b83bef2013-02-25 16:11:12 -08001860 if (osd_req->r_result < 0)
1861 obj_request->result = osd_req->r_result;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001862
Alex Elderc47f9372013-02-26 14:23:07 -06001863 /*
1864 * We support a 64-bit length, but ultimately it has to be
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01001865 * passed to the block layer, which just supports a 32-bit
1866 * length field.
Alex Elderc47f9372013-02-26 14:23:07 -06001867 */
Yan, Zheng7665d852016-01-07 16:48:57 +08001868 obj_request->xferred = osd_req->r_ops[0].outdata_len;
Alex Elder8b3e1a52013-01-24 16:13:36 -06001869 rbd_assert(obj_request->xferred < (u64)UINT_MAX);
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001870
Alex Elder79528732013-04-03 21:32:51 -05001871 opcode = osd_req->r_ops[0].op;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001872 switch (opcode) {
1873 case CEPH_OSD_OP_READ:
Alex Elderc47f9372013-02-26 14:23:07 -06001874 rbd_osd_read_callback(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001875 break;
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001876 case CEPH_OSD_OP_SETALLOCHINT:
Ilya Dryomove30b7572015-10-07 17:27:17 +02001877 rbd_assert(osd_req->r_ops[1].op == CEPH_OSD_OP_WRITE ||
1878 osd_req->r_ops[1].op == CEPH_OSD_OP_WRITEFULL);
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001879 /* fall through */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001880 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +02001881 case CEPH_OSD_OP_WRITEFULL:
Alex Elderc47f9372013-02-26 14:23:07 -06001882 rbd_osd_write_callback(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001883 break;
Alex Elderfbfab532013-02-08 09:55:48 -06001884 case CEPH_OSD_OP_STAT:
Alex Elderc47f9372013-02-26 14:23:07 -06001885 rbd_osd_stat_callback(obj_request);
Alex Elderfbfab532013-02-08 09:55:48 -06001886 break;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001887 case CEPH_OSD_OP_DELETE:
1888 case CEPH_OSD_OP_TRUNCATE:
1889 case CEPH_OSD_OP_ZERO:
1890 rbd_osd_discard_callback(obj_request);
1891 break;
Alex Elder36be9a72013-01-19 00:30:28 -06001892 case CEPH_OSD_OP_CALL:
Ilya Dryomov27617132015-07-16 17:36:11 +03001893 rbd_osd_call_callback(obj_request);
1894 break;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001895 default:
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01001896 rbd_warn(NULL, "unexpected OSD op: object_no %016llx opcode %d",
1897 obj_request->object_no, opcode);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001898 break;
1899 }
1900
Alex Elder07741302013-02-05 23:41:50 -06001901 if (obj_request_done_test(obj_request))
Alex Elderbf0d5f502012-11-22 00:00:08 -06001902 rbd_obj_request_complete(obj_request);
1903}
1904
Alex Elder9d4df012013-04-19 15:34:50 -05001905static void rbd_osd_req_format_read(struct rbd_obj_request *obj_request)
Alex Elder430c28c2013-04-03 21:32:51 -05001906{
Alex Elder8c042b02013-04-03 01:28:58 -05001907 struct ceph_osd_request *osd_req = obj_request->osd_req;
Alex Elder430c28c2013-04-03 21:32:51 -05001908
Ilya Dryomov7c848832016-09-15 17:56:39 +02001909 rbd_assert(obj_request_img_data_test(obj_request));
1910 osd_req->r_snapid = obj_request->img_request->snap_id;
Alex Elder9d4df012013-04-19 15:34:50 -05001911}
1912
1913static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request)
1914{
Alex Elder9d4df012013-04-19 15:34:50 -05001915 struct ceph_osd_request *osd_req = obj_request->osd_req;
Alex Elder9d4df012013-04-19 15:34:50 -05001916
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001917 osd_req->r_mtime = CURRENT_TIME;
1918 osd_req->r_data_offset = obj_request->offset;
Alex Elder430c28c2013-04-03 21:32:51 -05001919}
1920
Ilya Dryomovbc812072017-01-25 18:16:23 +01001921static struct ceph_osd_request *
1922__rbd_osd_req_create(struct rbd_device *rbd_dev,
1923 struct ceph_snap_context *snapc,
1924 int num_ops, unsigned int flags,
1925 struct rbd_obj_request *obj_request)
1926{
1927 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
1928 struct ceph_osd_request *req;
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01001929 const char *name_format = rbd_dev->image_format == 1 ?
1930 RBD_V1_DATA_FORMAT : RBD_V2_DATA_FORMAT;
Ilya Dryomovbc812072017-01-25 18:16:23 +01001931
1932 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, false, GFP_NOIO);
1933 if (!req)
1934 return NULL;
1935
1936 req->r_flags = flags;
1937 req->r_callback = rbd_osd_req_callback;
1938 req->r_priv = obj_request;
1939
1940 req->r_base_oloc.pool = rbd_dev->layout.pool_id;
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01001941 if (ceph_oid_aprintf(&req->r_base_oid, GFP_NOIO, name_format,
1942 rbd_dev->header.object_prefix, obj_request->object_no))
Ilya Dryomovbc812072017-01-25 18:16:23 +01001943 goto err_req;
1944
1945 if (ceph_osdc_alloc_messages(req, GFP_NOIO))
1946 goto err_req;
1947
1948 return req;
1949
1950err_req:
1951 ceph_osdc_put_request(req);
1952 return NULL;
1953}
1954
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001955/*
1956 * Create an osd request. A read request has one osd op (read).
1957 * A write request has either one (watch) or two (hint+write) osd ops.
1958 * (All rbd data writes are prefixed with an allocation hint op, but
1959 * technically osd watch is a write request, hence this distinction.)
1960 */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001961static struct ceph_osd_request *rbd_osd_req_create(
1962 struct rbd_device *rbd_dev,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08001963 enum obj_operation_type op_type,
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02001964 unsigned int num_ops,
Alex Elder430c28c2013-04-03 21:32:51 -05001965 struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001966{
Alex Elderbf0d5f502012-11-22 00:00:08 -06001967 struct ceph_snap_context *snapc = NULL;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001968
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001969 if (obj_request_img_data_test(obj_request) &&
1970 (op_type == OBJ_OP_DISCARD || op_type == OBJ_OP_WRITE)) {
Alex Elder6365d332013-02-11 12:33:24 -06001971 struct rbd_img_request *img_request = obj_request->img_request;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001972 if (op_type == OBJ_OP_WRITE) {
1973 rbd_assert(img_request_write_test(img_request));
1974 } else {
1975 rbd_assert(img_request_discard_test(img_request));
1976 }
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08001977 snapc = img_request->snapc;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001978 }
1979
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08001980 rbd_assert(num_ops == 1 || ((op_type == OBJ_OP_WRITE) && num_ops == 2));
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02001981
Ilya Dryomovbc812072017-01-25 18:16:23 +01001982 return __rbd_osd_req_create(rbd_dev, snapc, num_ops,
1983 (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD) ?
Ilya Dryomov54ea0042017-02-11 18:48:41 +01001984 CEPH_OSD_FLAG_WRITE : CEPH_OSD_FLAG_READ, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001985}
1986
Alex Elder0eefd472013-04-19 15:34:50 -05001987/*
Josh Durgind3246fb2014-04-07 16:49:21 -07001988 * Create a copyup osd request based on the information in the object
1989 * request supplied. A copyup request has two or three osd ops, a
1990 * copyup method call, potentially a hint op, and a write or truncate
1991 * or zero op.
Alex Elder0eefd472013-04-19 15:34:50 -05001992 */
1993static struct ceph_osd_request *
1994rbd_osd_req_create_copyup(struct rbd_obj_request *obj_request)
1995{
1996 struct rbd_img_request *img_request;
Josh Durgind3246fb2014-04-07 16:49:21 -07001997 int num_osd_ops = 3;
Alex Elder0eefd472013-04-19 15:34:50 -05001998
1999 rbd_assert(obj_request_img_data_test(obj_request));
2000 img_request = obj_request->img_request;
2001 rbd_assert(img_request);
Josh Durgind3246fb2014-04-07 16:49:21 -07002002 rbd_assert(img_request_write_test(img_request) ||
2003 img_request_discard_test(img_request));
Alex Elder0eefd472013-04-19 15:34:50 -05002004
Josh Durgind3246fb2014-04-07 16:49:21 -07002005 if (img_request_discard_test(img_request))
2006 num_osd_ops = 2;
2007
Ilya Dryomovbc812072017-01-25 18:16:23 +01002008 return __rbd_osd_req_create(img_request->rbd_dev,
2009 img_request->snapc, num_osd_ops,
Ilya Dryomov54ea0042017-02-11 18:48:41 +01002010 CEPH_OSD_FLAG_WRITE, obj_request);
Alex Elder0eefd472013-04-19 15:34:50 -05002011}
2012
Alex Elderbf0d5f502012-11-22 00:00:08 -06002013static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req)
2014{
2015 ceph_osdc_put_request(osd_req);
2016}
2017
Ilya Dryomov6c696d82017-01-25 18:16:23 +01002018static struct rbd_obj_request *
2019rbd_obj_request_create(enum obj_request_type type)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002020{
2021 struct rbd_obj_request *obj_request;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002022
2023 rbd_assert(obj_request_type_valid(type));
2024
Ilya Dryomov5a60e872015-06-24 17:24:33 +03002025 obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_NOIO);
Ilya Dryomov6c696d82017-01-25 18:16:23 +01002026 if (!obj_request)
Alex Elderf907ad52013-05-01 12:43:03 -05002027 return NULL;
Alex Elderf907ad52013-05-01 12:43:03 -05002028
Alex Elderbf0d5f502012-11-22 00:00:08 -06002029 obj_request->which = BAD_WHICH;
2030 obj_request->type = type;
2031 INIT_LIST_HEAD(&obj_request->links);
Alex Elder788e2df2013-01-17 12:25:27 -06002032 init_completion(&obj_request->completion);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002033 kref_init(&obj_request->kref);
2034
Ilya Dryomov67e2b652017-01-25 18:16:22 +01002035 dout("%s %p\n", __func__, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002036 return obj_request;
2037}
2038
2039static void rbd_obj_request_destroy(struct kref *kref)
2040{
2041 struct rbd_obj_request *obj_request;
2042
2043 obj_request = container_of(kref, struct rbd_obj_request, kref);
2044
Alex Elder37206ee2013-02-20 17:32:08 -06002045 dout("%s: obj %p\n", __func__, obj_request);
2046
Alex Elderbf0d5f502012-11-22 00:00:08 -06002047 rbd_assert(obj_request->img_request == NULL);
2048 rbd_assert(obj_request->which == BAD_WHICH);
2049
2050 if (obj_request->osd_req)
2051 rbd_osd_req_destroy(obj_request->osd_req);
2052
2053 rbd_assert(obj_request_type_valid(obj_request->type));
2054 switch (obj_request->type) {
Alex Elder9969ebc2013-01-18 12:31:10 -06002055 case OBJ_REQUEST_NODATA:
2056 break; /* Nothing to do */
Alex Elderbf0d5f502012-11-22 00:00:08 -06002057 case OBJ_REQUEST_BIO:
2058 if (obj_request->bio_list)
2059 bio_chain_put(obj_request->bio_list);
2060 break;
Alex Elder788e2df2013-01-17 12:25:27 -06002061 case OBJ_REQUEST_PAGES:
Ilya Dryomov04dc9232016-09-15 18:05:16 +02002062 /* img_data requests don't own their page array */
2063 if (obj_request->pages &&
2064 !obj_request_img_data_test(obj_request))
Alex Elder788e2df2013-01-17 12:25:27 -06002065 ceph_release_page_vector(obj_request->pages,
2066 obj_request->page_count);
2067 break;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002068 }
2069
Alex Elder868311b2013-05-01 12:43:03 -05002070 kmem_cache_free(rbd_obj_request_cache, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002071}
2072
Alex Elderfb65d2282013-05-08 22:50:04 -05002073/* It's OK to call this for a device with no parent */
2074
2075static void rbd_spec_put(struct rbd_spec *spec);
2076static void rbd_dev_unparent(struct rbd_device *rbd_dev)
2077{
2078 rbd_dev_remove_parent(rbd_dev);
2079 rbd_spec_put(rbd_dev->parent_spec);
2080 rbd_dev->parent_spec = NULL;
2081 rbd_dev->parent_overlap = 0;
2082}
2083
Alex Elderbf0d5f502012-11-22 00:00:08 -06002084/*
Alex Eldera2acd002013-05-08 22:50:04 -05002085 * Parent image reference counting is used to determine when an
2086 * image's parent fields can be safely torn down--after there are no
2087 * more in-flight requests to the parent image. When the last
2088 * reference is dropped, cleaning them up is safe.
2089 */
2090static void rbd_dev_parent_put(struct rbd_device *rbd_dev)
2091{
2092 int counter;
2093
2094 if (!rbd_dev->parent_spec)
2095 return;
2096
2097 counter = atomic_dec_return_safe(&rbd_dev->parent_ref);
2098 if (counter > 0)
2099 return;
2100
2101 /* Last reference; clean up parent data structures */
2102
2103 if (!counter)
2104 rbd_dev_unparent(rbd_dev);
2105 else
Ilya Dryomov9584d502014-07-11 12:11:20 +04002106 rbd_warn(rbd_dev, "parent reference underflow");
Alex Eldera2acd002013-05-08 22:50:04 -05002107}
2108
2109/*
2110 * If an image has a non-zero parent overlap, get a reference to its
2111 * parent.
2112 *
2113 * Returns true if the rbd device has a parent with a non-zero
2114 * overlap and a reference for it was successfully taken, or
2115 * false otherwise.
2116 */
2117static bool rbd_dev_parent_get(struct rbd_device *rbd_dev)
2118{
Ilya Dryomovae43e9d2015-01-19 18:13:43 +03002119 int counter = 0;
Alex Eldera2acd002013-05-08 22:50:04 -05002120
2121 if (!rbd_dev->parent_spec)
2122 return false;
2123
Ilya Dryomovae43e9d2015-01-19 18:13:43 +03002124 down_read(&rbd_dev->header_rwsem);
2125 if (rbd_dev->parent_overlap)
2126 counter = atomic_inc_return_safe(&rbd_dev->parent_ref);
2127 up_read(&rbd_dev->header_rwsem);
Alex Eldera2acd002013-05-08 22:50:04 -05002128
2129 if (counter < 0)
Ilya Dryomov9584d502014-07-11 12:11:20 +04002130 rbd_warn(rbd_dev, "parent reference overflow");
Alex Eldera2acd002013-05-08 22:50:04 -05002131
Ilya Dryomovae43e9d2015-01-19 18:13:43 +03002132 return counter > 0;
Alex Eldera2acd002013-05-08 22:50:04 -05002133}
2134
Alex Elderbf0d5f502012-11-22 00:00:08 -06002135/*
2136 * Caller is responsible for filling in the list of object requests
2137 * that comprises the image request, and the Linux request pointer
2138 * (if there is one).
2139 */
Alex Eldercc344fa2013-02-19 12:25:56 -06002140static struct rbd_img_request *rbd_img_request_create(
2141 struct rbd_device *rbd_dev,
Alex Elderbf0d5f502012-11-22 00:00:08 -06002142 u64 offset, u64 length,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002143 enum obj_operation_type op_type,
Josh Durgin4e752f02014-04-08 11:12:11 -07002144 struct ceph_snap_context *snapc)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002145{
2146 struct rbd_img_request *img_request;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002147
Ilya Dryomov7a716aa2014-08-05 11:25:54 +04002148 img_request = kmem_cache_alloc(rbd_img_request_cache, GFP_NOIO);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002149 if (!img_request)
2150 return NULL;
2151
Alex Elderbf0d5f502012-11-22 00:00:08 -06002152 img_request->rq = NULL;
2153 img_request->rbd_dev = rbd_dev;
2154 img_request->offset = offset;
2155 img_request->length = length;
Alex Elder0c425242013-02-08 09:55:49 -06002156 img_request->flags = 0;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002157 if (op_type == OBJ_OP_DISCARD) {
2158 img_request_discard_set(img_request);
2159 img_request->snapc = snapc;
2160 } else if (op_type == OBJ_OP_WRITE) {
Alex Elder0c425242013-02-08 09:55:49 -06002161 img_request_write_set(img_request);
Josh Durgin4e752f02014-04-08 11:12:11 -07002162 img_request->snapc = snapc;
Alex Elder0c425242013-02-08 09:55:49 -06002163 } else {
Alex Elderbf0d5f502012-11-22 00:00:08 -06002164 img_request->snap_id = rbd_dev->spec->snap_id;
Alex Elder0c425242013-02-08 09:55:49 -06002165 }
Alex Eldera2acd002013-05-08 22:50:04 -05002166 if (rbd_dev_parent_get(rbd_dev))
Alex Elderd0b2e942013-01-24 16:13:36 -06002167 img_request_layered_set(img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002168 spin_lock_init(&img_request->completion_lock);
2169 img_request->next_completion = 0;
2170 img_request->callback = NULL;
Alex Eldera5a337d2013-01-24 16:13:36 -06002171 img_request->result = 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002172 img_request->obj_request_count = 0;
2173 INIT_LIST_HEAD(&img_request->obj_requests);
2174 kref_init(&img_request->kref);
2175
Alex Elder37206ee2013-02-20 17:32:08 -06002176 dout("%s: rbd_dev %p %s %llu/%llu -> img %p\n", __func__, rbd_dev,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002177 obj_op_name(op_type), offset, length, img_request);
Alex Elder37206ee2013-02-20 17:32:08 -06002178
Alex Elderbf0d5f502012-11-22 00:00:08 -06002179 return img_request;
2180}
2181
2182static void rbd_img_request_destroy(struct kref *kref)
2183{
2184 struct rbd_img_request *img_request;
2185 struct rbd_obj_request *obj_request;
2186 struct rbd_obj_request *next_obj_request;
2187
2188 img_request = container_of(kref, struct rbd_img_request, kref);
2189
Alex Elder37206ee2013-02-20 17:32:08 -06002190 dout("%s: img %p\n", __func__, img_request);
2191
Alex Elderbf0d5f502012-11-22 00:00:08 -06002192 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
2193 rbd_img_obj_request_del(img_request, obj_request);
Alex Elder25dcf952013-01-25 17:08:55 -06002194 rbd_assert(img_request->obj_request_count == 0);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002195
Alex Eldera2acd002013-05-08 22:50:04 -05002196 if (img_request_layered_test(img_request)) {
2197 img_request_layered_clear(img_request);
2198 rbd_dev_parent_put(img_request->rbd_dev);
2199 }
2200
Josh Durginbef95452014-04-04 17:47:52 -07002201 if (img_request_write_test(img_request) ||
2202 img_request_discard_test(img_request))
Alex Elder812164f82013-04-30 00:44:32 -05002203 ceph_put_snap_context(img_request->snapc);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002204
Alex Elder1c2a9df2013-05-01 12:43:03 -05002205 kmem_cache_free(rbd_img_request_cache, img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002206}
2207
Alex Eldere93f3152013-05-08 22:50:04 -05002208static struct rbd_img_request *rbd_parent_request_create(
2209 struct rbd_obj_request *obj_request,
2210 u64 img_offset, u64 length)
2211{
2212 struct rbd_img_request *parent_request;
2213 struct rbd_device *rbd_dev;
2214
2215 rbd_assert(obj_request->img_request);
2216 rbd_dev = obj_request->img_request->rbd_dev;
2217
Josh Durgin4e752f02014-04-08 11:12:11 -07002218 parent_request = rbd_img_request_create(rbd_dev->parent, img_offset,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002219 length, OBJ_OP_READ, NULL);
Alex Eldere93f3152013-05-08 22:50:04 -05002220 if (!parent_request)
2221 return NULL;
2222
2223 img_request_child_set(parent_request);
2224 rbd_obj_request_get(obj_request);
2225 parent_request->obj_request = obj_request;
2226
2227 return parent_request;
2228}
2229
2230static void rbd_parent_request_destroy(struct kref *kref)
2231{
2232 struct rbd_img_request *parent_request;
2233 struct rbd_obj_request *orig_request;
2234
2235 parent_request = container_of(kref, struct rbd_img_request, kref);
2236 orig_request = parent_request->obj_request;
2237
2238 parent_request->obj_request = NULL;
2239 rbd_obj_request_put(orig_request);
2240 img_request_child_clear(parent_request);
2241
2242 rbd_img_request_destroy(kref);
2243}
2244
Alex Elder12178572013-02-08 09:55:49 -06002245static bool rbd_img_obj_end_request(struct rbd_obj_request *obj_request)
2246{
Alex Elder6365d332013-02-11 12:33:24 -06002247 struct rbd_img_request *img_request;
Alex Elder12178572013-02-08 09:55:49 -06002248 unsigned int xferred;
2249 int result;
Alex Elder8b3e1a52013-01-24 16:13:36 -06002250 bool more;
Alex Elder12178572013-02-08 09:55:49 -06002251
Alex Elder6365d332013-02-11 12:33:24 -06002252 rbd_assert(obj_request_img_data_test(obj_request));
2253 img_request = obj_request->img_request;
2254
Alex Elder12178572013-02-08 09:55:49 -06002255 rbd_assert(obj_request->xferred <= (u64)UINT_MAX);
2256 xferred = (unsigned int)obj_request->xferred;
2257 result = obj_request->result;
2258 if (result) {
2259 struct rbd_device *rbd_dev = img_request->rbd_dev;
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002260 enum obj_operation_type op_type;
2261
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002262 if (img_request_discard_test(img_request))
2263 op_type = OBJ_OP_DISCARD;
2264 else if (img_request_write_test(img_request))
2265 op_type = OBJ_OP_WRITE;
2266 else
2267 op_type = OBJ_OP_READ;
Alex Elder12178572013-02-08 09:55:49 -06002268
Ilya Dryomov9584d502014-07-11 12:11:20 +04002269 rbd_warn(rbd_dev, "%s %llx at %llx (%llx)",
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002270 obj_op_name(op_type), obj_request->length,
2271 obj_request->img_offset, obj_request->offset);
Ilya Dryomov9584d502014-07-11 12:11:20 +04002272 rbd_warn(rbd_dev, " result %d xferred %x",
Alex Elder12178572013-02-08 09:55:49 -06002273 result, xferred);
2274 if (!img_request->result)
2275 img_request->result = result;
Ilya Dryomov082a75d2015-04-25 15:56:15 +03002276 /*
2277 * Need to end I/O on the entire obj_request worth of
2278 * bytes in case of error.
2279 */
2280 xferred = obj_request->length;
Alex Elder12178572013-02-08 09:55:49 -06002281 }
2282
Alex Elder8b3e1a52013-01-24 16:13:36 -06002283 if (img_request_child_test(img_request)) {
2284 rbd_assert(img_request->obj_request != NULL);
2285 more = obj_request->which < img_request->obj_request_count - 1;
2286 } else {
2287 rbd_assert(img_request->rq != NULL);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01002288
2289 more = blk_update_request(img_request->rq, result, xferred);
2290 if (!more)
2291 __blk_mq_end_request(img_request->rq, result);
Alex Elder8b3e1a52013-01-24 16:13:36 -06002292 }
2293
2294 return more;
Alex Elder12178572013-02-08 09:55:49 -06002295}
2296
Alex Elder21692382013-04-05 01:27:12 -05002297static void rbd_img_obj_callback(struct rbd_obj_request *obj_request)
2298{
2299 struct rbd_img_request *img_request;
2300 u32 which = obj_request->which;
2301 bool more = true;
2302
Alex Elder6365d332013-02-11 12:33:24 -06002303 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elder21692382013-04-05 01:27:12 -05002304 img_request = obj_request->img_request;
2305
2306 dout("%s: img %p obj %p\n", __func__, img_request, obj_request);
2307 rbd_assert(img_request != NULL);
Alex Elder21692382013-04-05 01:27:12 -05002308 rbd_assert(img_request->obj_request_count > 0);
2309 rbd_assert(which != BAD_WHICH);
2310 rbd_assert(which < img_request->obj_request_count);
Alex Elder21692382013-04-05 01:27:12 -05002311
2312 spin_lock_irq(&img_request->completion_lock);
2313 if (which != img_request->next_completion)
2314 goto out;
2315
2316 for_each_obj_request_from(img_request, obj_request) {
Alex Elder21692382013-04-05 01:27:12 -05002317 rbd_assert(more);
2318 rbd_assert(which < img_request->obj_request_count);
2319
2320 if (!obj_request_done_test(obj_request))
2321 break;
Alex Elder12178572013-02-08 09:55:49 -06002322 more = rbd_img_obj_end_request(obj_request);
Alex Elder21692382013-04-05 01:27:12 -05002323 which++;
2324 }
2325
2326 rbd_assert(more ^ (which == img_request->obj_request_count));
2327 img_request->next_completion = which;
2328out:
2329 spin_unlock_irq(&img_request->completion_lock);
Alex Elder0f2d5be2014-04-26 14:21:44 +04002330 rbd_img_request_put(img_request);
Alex Elder21692382013-04-05 01:27:12 -05002331
2332 if (!more)
2333 rbd_img_request_complete(img_request);
2334}
2335
Alex Elderf1a47392013-04-19 15:34:50 -05002336/*
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002337 * Add individual osd ops to the given ceph_osd_request and prepare
2338 * them for submission. num_ops is the current number of
2339 * osd operations already to the object request.
2340 */
2341static void rbd_img_obj_request_fill(struct rbd_obj_request *obj_request,
2342 struct ceph_osd_request *osd_request,
2343 enum obj_operation_type op_type,
2344 unsigned int num_ops)
2345{
2346 struct rbd_img_request *img_request = obj_request->img_request;
2347 struct rbd_device *rbd_dev = img_request->rbd_dev;
2348 u64 object_size = rbd_obj_bytes(&rbd_dev->header);
2349 u64 offset = obj_request->offset;
2350 u64 length = obj_request->length;
2351 u64 img_end;
2352 u16 opcode;
2353
2354 if (op_type == OBJ_OP_DISCARD) {
Josh Durgind3246fb2014-04-07 16:49:21 -07002355 if (!offset && length == object_size &&
2356 (!img_request_layered_test(img_request) ||
2357 !obj_request_overlaps_parent(obj_request))) {
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002358 opcode = CEPH_OSD_OP_DELETE;
2359 } else if ((offset + length == object_size)) {
2360 opcode = CEPH_OSD_OP_TRUNCATE;
2361 } else {
2362 down_read(&rbd_dev->header_rwsem);
2363 img_end = rbd_dev->header.image_size;
2364 up_read(&rbd_dev->header_rwsem);
2365
2366 if (obj_request->img_offset + length == img_end)
2367 opcode = CEPH_OSD_OP_TRUNCATE;
2368 else
2369 opcode = CEPH_OSD_OP_ZERO;
2370 }
2371 } else if (op_type == OBJ_OP_WRITE) {
Ilya Dryomove30b7572015-10-07 17:27:17 +02002372 if (!offset && length == object_size)
2373 opcode = CEPH_OSD_OP_WRITEFULL;
2374 else
2375 opcode = CEPH_OSD_OP_WRITE;
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002376 osd_req_op_alloc_hint_init(osd_request, num_ops,
2377 object_size, object_size);
2378 num_ops++;
2379 } else {
2380 opcode = CEPH_OSD_OP_READ;
2381 }
2382
Ilya Dryomov7e868b62014-11-21 22:16:43 +03002383 if (opcode == CEPH_OSD_OP_DELETE)
Yan, Zheng144cba12015-04-27 11:09:54 +08002384 osd_req_op_init(osd_request, num_ops, opcode, 0);
Ilya Dryomov7e868b62014-11-21 22:16:43 +03002385 else
2386 osd_req_op_extent_init(osd_request, num_ops, opcode,
2387 offset, length, 0, 0);
2388
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002389 if (obj_request->type == OBJ_REQUEST_BIO)
2390 osd_req_op_extent_osd_data_bio(osd_request, num_ops,
2391 obj_request->bio_list, length);
2392 else if (obj_request->type == OBJ_REQUEST_PAGES)
2393 osd_req_op_extent_osd_data_pages(osd_request, num_ops,
2394 obj_request->pages, length,
2395 offset & ~PAGE_MASK, false, false);
2396
2397 /* Discards are also writes */
2398 if (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD)
2399 rbd_osd_req_format_write(obj_request);
2400 else
2401 rbd_osd_req_format_read(obj_request);
2402}
2403
2404/*
Alex Elderf1a47392013-04-19 15:34:50 -05002405 * Split up an image request into one or more object requests, each
2406 * to a different object. The "type" parameter indicates whether
2407 * "data_desc" is the pointer to the head of a list of bio
2408 * structures, or the base of a page array. In either case this
2409 * function assumes data_desc describes memory sufficient to hold
2410 * all data described by the image request.
2411 */
2412static int rbd_img_request_fill(struct rbd_img_request *img_request,
2413 enum obj_request_type type,
2414 void *data_desc)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002415{
2416 struct rbd_device *rbd_dev = img_request->rbd_dev;
2417 struct rbd_obj_request *obj_request = NULL;
2418 struct rbd_obj_request *next_obj_request;
Jingoo Hana1580732013-08-09 13:04:35 +09002419 struct bio *bio_list = NULL;
Alex Elderf1a47392013-04-19 15:34:50 -05002420 unsigned int bio_offset = 0;
Jingoo Hana1580732013-08-09 13:04:35 +09002421 struct page **pages = NULL;
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002422 enum obj_operation_type op_type;
Alex Elder7da22d22013-01-24 16:13:36 -06002423 u64 img_offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002424 u64 resid;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002425
Alex Elderf1a47392013-04-19 15:34:50 -05002426 dout("%s: img %p type %d data_desc %p\n", __func__, img_request,
2427 (int)type, data_desc);
Alex Elder37206ee2013-02-20 17:32:08 -06002428
Alex Elder7da22d22013-01-24 16:13:36 -06002429 img_offset = img_request->offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002430 resid = img_request->length;
Alex Elder4dda41d2013-02-20 21:59:33 -06002431 rbd_assert(resid > 0);
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002432 op_type = rbd_img_request_op_type(img_request);
Alex Elderf1a47392013-04-19 15:34:50 -05002433
2434 if (type == OBJ_REQUEST_BIO) {
2435 bio_list = data_desc;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002436 rbd_assert(img_offset ==
2437 bio_list->bi_iter.bi_sector << SECTOR_SHIFT);
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002438 } else if (type == OBJ_REQUEST_PAGES) {
Alex Elderf1a47392013-04-19 15:34:50 -05002439 pages = data_desc;
2440 }
2441
Alex Elderbf0d5f502012-11-22 00:00:08 -06002442 while (resid) {
Alex Elder2fa12322013-04-05 01:27:12 -05002443 struct ceph_osd_request *osd_req;
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01002444 u64 object_no = img_offset >> rbd_dev->header.obj_order;
Ilya Dryomov67e2b652017-01-25 18:16:22 +01002445 u64 offset = rbd_segment_offset(rbd_dev, img_offset);
2446 u64 length = rbd_segment_length(rbd_dev, img_offset, resid);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002447
Ilya Dryomov6c696d82017-01-25 18:16:23 +01002448 obj_request = rbd_obj_request_create(type);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002449 if (!obj_request)
2450 goto out_unwind;
Ilya Dryomov62054da2014-03-04 11:57:17 +02002451
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01002452 obj_request->object_no = object_no;
Ilya Dryomov67e2b652017-01-25 18:16:22 +01002453 obj_request->offset = offset;
2454 obj_request->length = length;
2455
Josh Durgin03507db2013-08-27 14:45:46 -07002456 /*
2457 * set obj_request->img_request before creating the
2458 * osd_request so that it gets the right snapc
2459 */
2460 rbd_img_obj_request_add(img_request, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002461
Alex Elderf1a47392013-04-19 15:34:50 -05002462 if (type == OBJ_REQUEST_BIO) {
2463 unsigned int clone_size;
2464
2465 rbd_assert(length <= (u64)UINT_MAX);
2466 clone_size = (unsigned int)length;
2467 obj_request->bio_list =
2468 bio_chain_clone_range(&bio_list,
2469 &bio_offset,
2470 clone_size,
David Disseldorp2224d872016-04-05 11:13:39 +02002471 GFP_NOIO);
Alex Elderf1a47392013-04-19 15:34:50 -05002472 if (!obj_request->bio_list)
Ilya Dryomov62054da2014-03-04 11:57:17 +02002473 goto out_unwind;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002474 } else if (type == OBJ_REQUEST_PAGES) {
Alex Elderf1a47392013-04-19 15:34:50 -05002475 unsigned int page_count;
2476
2477 obj_request->pages = pages;
2478 page_count = (u32)calc_pages_for(offset, length);
2479 obj_request->page_count = page_count;
2480 if ((offset + length) & ~PAGE_MASK)
2481 page_count--; /* more on last page */
2482 pages += page_count;
2483 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06002484
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002485 osd_req = rbd_osd_req_create(rbd_dev, op_type,
2486 (op_type == OBJ_OP_WRITE) ? 2 : 1,
2487 obj_request);
Alex Elder2fa12322013-04-05 01:27:12 -05002488 if (!osd_req)
Ilya Dryomov62054da2014-03-04 11:57:17 +02002489 goto out_unwind;
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002490
Alex Elder2fa12322013-04-05 01:27:12 -05002491 obj_request->osd_req = osd_req;
Alex Elder21692382013-04-05 01:27:12 -05002492 obj_request->callback = rbd_img_obj_callback;
Alex Elder7da22d22013-01-24 16:13:36 -06002493 obj_request->img_offset = img_offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002494
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002495 rbd_img_obj_request_fill(obj_request, osd_req, op_type, 0);
2496
Alex Elder7da22d22013-01-24 16:13:36 -06002497 img_offset += length;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002498 resid -= length;
2499 }
2500
2501 return 0;
2502
Alex Elderbf0d5f502012-11-22 00:00:08 -06002503out_unwind:
2504 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
Ilya Dryomov42dd0372014-03-04 11:57:17 +02002505 rbd_img_obj_request_del(img_request, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002506
2507 return -ENOMEM;
2508}
2509
Alex Elder3d7efd12013-04-19 15:34:50 -05002510static void
Ilya Dryomov27617132015-07-16 17:36:11 +03002511rbd_osd_copyup_callback(struct rbd_obj_request *obj_request)
Alex Elder0eefd472013-04-19 15:34:50 -05002512{
2513 struct rbd_img_request *img_request;
2514 struct rbd_device *rbd_dev;
Alex Elderebda6402013-05-10 16:29:22 -05002515 struct page **pages;
Alex Elder0eefd472013-04-19 15:34:50 -05002516 u32 page_count;
2517
Ilya Dryomov27617132015-07-16 17:36:11 +03002518 dout("%s: obj %p\n", __func__, obj_request);
2519
Josh Durgind3246fb2014-04-07 16:49:21 -07002520 rbd_assert(obj_request->type == OBJ_REQUEST_BIO ||
2521 obj_request->type == OBJ_REQUEST_NODATA);
Alex Elder0eefd472013-04-19 15:34:50 -05002522 rbd_assert(obj_request_img_data_test(obj_request));
2523 img_request = obj_request->img_request;
2524 rbd_assert(img_request);
2525
2526 rbd_dev = img_request->rbd_dev;
2527 rbd_assert(rbd_dev);
Alex Elder0eefd472013-04-19 15:34:50 -05002528
Alex Elderebda6402013-05-10 16:29:22 -05002529 pages = obj_request->copyup_pages;
2530 rbd_assert(pages != NULL);
Alex Elder0eefd472013-04-19 15:34:50 -05002531 obj_request->copyup_pages = NULL;
Alex Elderebda6402013-05-10 16:29:22 -05002532 page_count = obj_request->copyup_page_count;
2533 rbd_assert(page_count);
2534 obj_request->copyup_page_count = 0;
2535 ceph_release_page_vector(pages, page_count);
Alex Elder0eefd472013-04-19 15:34:50 -05002536
2537 /*
2538 * We want the transfer count to reflect the size of the
2539 * original write request. There is no such thing as a
2540 * successful short write, so if the request was successful
2541 * we can just set it to the originally-requested length.
2542 */
2543 if (!obj_request->result)
2544 obj_request->xferred = obj_request->length;
2545
Ilya Dryomov27617132015-07-16 17:36:11 +03002546 obj_request_done_set(obj_request);
Alex Elder0eefd472013-04-19 15:34:50 -05002547}
2548
2549static void
Alex Elder3d7efd12013-04-19 15:34:50 -05002550rbd_img_obj_parent_read_full_callback(struct rbd_img_request *img_request)
2551{
2552 struct rbd_obj_request *orig_request;
Alex Elder0eefd472013-04-19 15:34:50 -05002553 struct ceph_osd_request *osd_req;
Alex Elder0eefd472013-04-19 15:34:50 -05002554 struct rbd_device *rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002555 struct page **pages;
Josh Durgind3246fb2014-04-07 16:49:21 -07002556 enum obj_operation_type op_type;
Alex Elderebda6402013-05-10 16:29:22 -05002557 u32 page_count;
Alex Elderbbea1c12013-05-06 17:40:33 -05002558 int img_result;
Alex Elderebda6402013-05-10 16:29:22 -05002559 u64 parent_length;
Alex Elder3d7efd12013-04-19 15:34:50 -05002560
2561 rbd_assert(img_request_child_test(img_request));
2562
2563 /* First get what we need from the image request */
2564
2565 pages = img_request->copyup_pages;
2566 rbd_assert(pages != NULL);
2567 img_request->copyup_pages = NULL;
Alex Elderebda6402013-05-10 16:29:22 -05002568 page_count = img_request->copyup_page_count;
2569 rbd_assert(page_count);
2570 img_request->copyup_page_count = 0;
Alex Elder3d7efd12013-04-19 15:34:50 -05002571
2572 orig_request = img_request->obj_request;
2573 rbd_assert(orig_request != NULL);
Alex Elderb91f09f2013-05-10 16:29:22 -05002574 rbd_assert(obj_request_type_valid(orig_request->type));
Alex Elderbbea1c12013-05-06 17:40:33 -05002575 img_result = img_request->result;
Alex Elderebda6402013-05-10 16:29:22 -05002576 parent_length = img_request->length;
Ilya Dryomovfa355112016-09-16 15:20:42 +02002577 rbd_assert(img_result || parent_length == img_request->xferred);
Alex Elder3d7efd12013-04-19 15:34:50 -05002578 rbd_img_request_put(img_request);
2579
Alex Elder91c6feb2013-05-06 17:40:32 -05002580 rbd_assert(orig_request->img_request);
2581 rbd_dev = orig_request->img_request->rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002582 rbd_assert(rbd_dev);
Alex Elder3d7efd12013-04-19 15:34:50 -05002583
Alex Elderbbea1c12013-05-06 17:40:33 -05002584 /*
2585 * If the overlap has become 0 (most likely because the
2586 * image has been flattened) we need to free the pages
2587 * and re-submit the original write request.
2588 */
2589 if (!rbd_dev->parent_overlap) {
Alex Elderbbea1c12013-05-06 17:40:33 -05002590 ceph_release_page_vector(pages, page_count);
Ilya Dryomov980917f2016-09-12 18:59:42 +02002591 rbd_obj_request_submit(orig_request);
2592 return;
Alex Elderbbea1c12013-05-06 17:40:33 -05002593 }
2594
2595 if (img_result)
Alex Elder0eefd472013-04-19 15:34:50 -05002596 goto out_err;
Alex Elder3d7efd12013-04-19 15:34:50 -05002597
Alex Elder8785b1d2013-05-09 10:08:49 -05002598 /*
2599 * The original osd request is of no use to use any more.
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02002600 * We need a new one that can hold the three ops in a copyup
Alex Elder8785b1d2013-05-09 10:08:49 -05002601 * request. Allocate the new copyup osd request for the
2602 * original request, and release the old one.
2603 */
Alex Elderbbea1c12013-05-06 17:40:33 -05002604 img_result = -ENOMEM;
Alex Elder0eefd472013-04-19 15:34:50 -05002605 osd_req = rbd_osd_req_create_copyup(orig_request);
2606 if (!osd_req)
2607 goto out_err;
Alex Elder8785b1d2013-05-09 10:08:49 -05002608 rbd_osd_req_destroy(orig_request->osd_req);
Alex Elder0eefd472013-04-19 15:34:50 -05002609 orig_request->osd_req = osd_req;
2610 orig_request->copyup_pages = pages;
Alex Elderebda6402013-05-10 16:29:22 -05002611 orig_request->copyup_page_count = page_count;
Alex Elder3d7efd12013-04-19 15:34:50 -05002612
Alex Elder0eefd472013-04-19 15:34:50 -05002613 /* Initialize the copyup op */
2614
2615 osd_req_op_cls_init(osd_req, 0, CEPH_OSD_OP_CALL, "rbd", "copyup");
Alex Elderebda6402013-05-10 16:29:22 -05002616 osd_req_op_cls_request_data_pages(osd_req, 0, pages, parent_length, 0,
Alex Elder0eefd472013-04-19 15:34:50 -05002617 false, false);
2618
Josh Durgind3246fb2014-04-07 16:49:21 -07002619 /* Add the other op(s) */
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02002620
Josh Durgind3246fb2014-04-07 16:49:21 -07002621 op_type = rbd_img_request_op_type(orig_request->img_request);
2622 rbd_img_obj_request_fill(orig_request, osd_req, op_type, 1);
Alex Elder0eefd472013-04-19 15:34:50 -05002623
2624 /* All set, send it off. */
2625
Ilya Dryomov980917f2016-09-12 18:59:42 +02002626 rbd_obj_request_submit(orig_request);
2627 return;
Alex Elder0eefd472013-04-19 15:34:50 -05002628
Alex Elder0eefd472013-04-19 15:34:50 -05002629out_err:
Ilya Dryomovfa355112016-09-16 15:20:42 +02002630 ceph_release_page_vector(pages, page_count);
Ilya Dryomov0dcc6852016-09-26 15:43:52 +02002631 rbd_obj_request_error(orig_request, img_result);
Alex Elder3d7efd12013-04-19 15:34:50 -05002632}
2633
2634/*
2635 * Read from the parent image the range of data that covers the
2636 * entire target of the given object request. This is used for
2637 * satisfying a layered image write request when the target of an
2638 * object request from the image request does not exist.
2639 *
2640 * A page array big enough to hold the returned data is allocated
2641 * and supplied to rbd_img_request_fill() as the "data descriptor."
2642 * When the read completes, this page array will be transferred to
2643 * the original object request for the copyup operation.
2644 *
Ilya Dryomovc2e82412016-09-13 20:18:01 +02002645 * If an error occurs, it is recorded as the result of the original
2646 * object request in rbd_img_obj_exists_callback().
Alex Elder3d7efd12013-04-19 15:34:50 -05002647 */
2648static int rbd_img_obj_parent_read_full(struct rbd_obj_request *obj_request)
2649{
Ilya Dryomov058aa992016-09-12 14:44:45 +02002650 struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002651 struct rbd_img_request *parent_request = NULL;
Alex Elder3d7efd12013-04-19 15:34:50 -05002652 u64 img_offset;
2653 u64 length;
2654 struct page **pages = NULL;
2655 u32 page_count;
2656 int result;
2657
Alex Elder3d7efd12013-04-19 15:34:50 -05002658 rbd_assert(rbd_dev->parent != NULL);
2659
2660 /*
2661 * Determine the byte range covered by the object in the
2662 * child image to which the original request was to be sent.
2663 */
2664 img_offset = obj_request->img_offset - obj_request->offset;
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +01002665 length = rbd_obj_bytes(&rbd_dev->header);
Alex Elder3d7efd12013-04-19 15:34:50 -05002666
2667 /*
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002668 * There is no defined parent data beyond the parent
2669 * overlap, so limit what we read at that boundary if
2670 * necessary.
2671 */
2672 if (img_offset + length > rbd_dev->parent_overlap) {
2673 rbd_assert(img_offset < rbd_dev->parent_overlap);
2674 length = rbd_dev->parent_overlap - img_offset;
2675 }
2676
2677 /*
Alex Elder3d7efd12013-04-19 15:34:50 -05002678 * Allocate a page array big enough to receive the data read
2679 * from the parent.
2680 */
2681 page_count = (u32)calc_pages_for(0, length);
2682 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2683 if (IS_ERR(pages)) {
2684 result = PTR_ERR(pages);
2685 pages = NULL;
2686 goto out_err;
2687 }
2688
2689 result = -ENOMEM;
Alex Eldere93f3152013-05-08 22:50:04 -05002690 parent_request = rbd_parent_request_create(obj_request,
2691 img_offset, length);
Alex Elder3d7efd12013-04-19 15:34:50 -05002692 if (!parent_request)
2693 goto out_err;
Alex Elder3d7efd12013-04-19 15:34:50 -05002694
2695 result = rbd_img_request_fill(parent_request, OBJ_REQUEST_PAGES, pages);
2696 if (result)
2697 goto out_err;
Ilya Dryomov058aa992016-09-12 14:44:45 +02002698
Alex Elder3d7efd12013-04-19 15:34:50 -05002699 parent_request->copyup_pages = pages;
Alex Elderebda6402013-05-10 16:29:22 -05002700 parent_request->copyup_page_count = page_count;
Alex Elder3d7efd12013-04-19 15:34:50 -05002701 parent_request->callback = rbd_img_obj_parent_read_full_callback;
Ilya Dryomov058aa992016-09-12 14:44:45 +02002702
Alex Elder3d7efd12013-04-19 15:34:50 -05002703 result = rbd_img_request_submit(parent_request);
2704 if (!result)
2705 return 0;
2706
2707 parent_request->copyup_pages = NULL;
Alex Elderebda6402013-05-10 16:29:22 -05002708 parent_request->copyup_page_count = 0;
Alex Elder3d7efd12013-04-19 15:34:50 -05002709 parent_request->obj_request = NULL;
2710 rbd_obj_request_put(obj_request);
2711out_err:
2712 if (pages)
2713 ceph_release_page_vector(pages, page_count);
2714 if (parent_request)
2715 rbd_img_request_put(parent_request);
Alex Elder3d7efd12013-04-19 15:34:50 -05002716 return result;
2717}
2718
Alex Elderc5b5ef62013-02-11 12:33:24 -06002719static void rbd_img_obj_exists_callback(struct rbd_obj_request *obj_request)
2720{
Alex Elderc5b5ef62013-02-11 12:33:24 -06002721 struct rbd_obj_request *orig_request;
Alex Elder638f5ab2013-05-06 17:40:33 -05002722 struct rbd_device *rbd_dev;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002723 int result;
2724
2725 rbd_assert(!obj_request_img_data_test(obj_request));
2726
2727 /*
2728 * All we need from the object request is the original
2729 * request and the result of the STAT op. Grab those, then
2730 * we're done with the request.
2731 */
2732 orig_request = obj_request->obj_request;
2733 obj_request->obj_request = NULL;
Alex Elder912c3172013-05-13 20:35:38 -05002734 rbd_obj_request_put(orig_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002735 rbd_assert(orig_request);
2736 rbd_assert(orig_request->img_request);
2737
2738 result = obj_request->result;
2739 obj_request->result = 0;
2740
2741 dout("%s: obj %p for obj %p result %d %llu/%llu\n", __func__,
2742 obj_request, orig_request, result,
2743 obj_request->xferred, obj_request->length);
2744 rbd_obj_request_put(obj_request);
2745
Alex Elder638f5ab2013-05-06 17:40:33 -05002746 /*
2747 * If the overlap has become 0 (most likely because the
Ilya Dryomov980917f2016-09-12 18:59:42 +02002748 * image has been flattened) we need to re-submit the
2749 * original request.
Alex Elder638f5ab2013-05-06 17:40:33 -05002750 */
2751 rbd_dev = orig_request->img_request->rbd_dev;
2752 if (!rbd_dev->parent_overlap) {
Ilya Dryomov980917f2016-09-12 18:59:42 +02002753 rbd_obj_request_submit(orig_request);
2754 return;
Alex Elder638f5ab2013-05-06 17:40:33 -05002755 }
Alex Elderc5b5ef62013-02-11 12:33:24 -06002756
2757 /*
2758 * Our only purpose here is to determine whether the object
2759 * exists, and we don't want to treat the non-existence as
2760 * an error. If something else comes back, transfer the
2761 * error to the original request and complete it now.
2762 */
2763 if (!result) {
2764 obj_request_existence_set(orig_request, true);
2765 } else if (result == -ENOENT) {
2766 obj_request_existence_set(orig_request, false);
Ilya Dryomovc2e82412016-09-13 20:18:01 +02002767 } else {
2768 goto fail_orig_request;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002769 }
2770
2771 /*
2772 * Resubmit the original request now that we have recorded
2773 * whether the target object exists.
2774 */
Ilya Dryomovc2e82412016-09-13 20:18:01 +02002775 result = rbd_img_obj_request_submit(orig_request);
2776 if (result)
2777 goto fail_orig_request;
2778
2779 return;
2780
2781fail_orig_request:
Ilya Dryomov0dcc6852016-09-26 15:43:52 +02002782 rbd_obj_request_error(orig_request, result);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002783}
2784
2785static int rbd_img_obj_exists_submit(struct rbd_obj_request *obj_request)
2786{
Ilya Dryomov058aa992016-09-12 14:44:45 +02002787 struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002788 struct rbd_obj_request *stat_request;
Ilya Dryomov710214e2016-09-15 17:53:32 +02002789 struct page **pages;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002790 u32 page_count;
2791 size_t size;
2792 int ret;
2793
Ilya Dryomov6c696d82017-01-25 18:16:23 +01002794 stat_request = rbd_obj_request_create(OBJ_REQUEST_PAGES);
Ilya Dryomov710214e2016-09-15 17:53:32 +02002795 if (!stat_request)
2796 return -ENOMEM;
2797
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01002798 stat_request->object_no = obj_request->object_no;
2799
Ilya Dryomov710214e2016-09-15 17:53:32 +02002800 stat_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_READ, 1,
2801 stat_request);
2802 if (!stat_request->osd_req) {
2803 ret = -ENOMEM;
2804 goto fail_stat_request;
2805 }
2806
Alex Elderc5b5ef62013-02-11 12:33:24 -06002807 /*
2808 * The response data for a STAT call consists of:
2809 * le64 length;
2810 * struct {
2811 * le32 tv_sec;
2812 * le32 tv_nsec;
2813 * } mtime;
2814 */
2815 size = sizeof (__le64) + sizeof (__le32) + sizeof (__le32);
2816 page_count = (u32)calc_pages_for(0, size);
2817 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
Ilya Dryomov710214e2016-09-15 17:53:32 +02002818 if (IS_ERR(pages)) {
2819 ret = PTR_ERR(pages);
2820 goto fail_stat_request;
2821 }
Alex Elderc5b5ef62013-02-11 12:33:24 -06002822
Ilya Dryomov710214e2016-09-15 17:53:32 +02002823 osd_req_op_init(stat_request->osd_req, 0, CEPH_OSD_OP_STAT, 0);
2824 osd_req_op_raw_data_in_pages(stat_request->osd_req, 0, pages, size, 0,
2825 false, false);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002826
2827 rbd_obj_request_get(obj_request);
2828 stat_request->obj_request = obj_request;
2829 stat_request->pages = pages;
2830 stat_request->page_count = page_count;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002831 stat_request->callback = rbd_img_obj_exists_callback;
2832
Ilya Dryomov980917f2016-09-12 18:59:42 +02002833 rbd_obj_request_submit(stat_request);
2834 return 0;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002835
Ilya Dryomov710214e2016-09-15 17:53:32 +02002836fail_stat_request:
2837 rbd_obj_request_put(stat_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002838 return ret;
2839}
2840
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002841static bool img_obj_request_simple(struct rbd_obj_request *obj_request)
Alex Elderb454e362013-04-19 15:34:50 -05002842{
Ilya Dryomov058aa992016-09-12 14:44:45 +02002843 struct rbd_img_request *img_request = obj_request->img_request;
2844 struct rbd_device *rbd_dev = img_request->rbd_dev;
Alex Elderb454e362013-04-19 15:34:50 -05002845
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002846 /* Reads */
Josh Durgin1c220882014-04-04 17:49:12 -07002847 if (!img_request_write_test(img_request) &&
2848 !img_request_discard_test(img_request))
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002849 return true;
Alex Elderb454e362013-04-19 15:34:50 -05002850
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002851 /* Non-layered writes */
2852 if (!img_request_layered_test(img_request))
2853 return true;
2854
2855 /*
2856 * Layered writes outside of the parent overlap range don't
2857 * share any data with the parent.
2858 */
2859 if (!obj_request_overlaps_parent(obj_request))
2860 return true;
2861
2862 /*
Guangliang Zhaoc622d222014-04-01 22:22:15 +08002863 * Entire-object layered writes - we will overwrite whatever
2864 * parent data there is anyway.
2865 */
2866 if (!obj_request->offset &&
2867 obj_request->length == rbd_obj_bytes(&rbd_dev->header))
2868 return true;
2869
2870 /*
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002871 * If the object is known to already exist, its parent data has
2872 * already been copied.
2873 */
2874 if (obj_request_known_test(obj_request) &&
2875 obj_request_exists_test(obj_request))
2876 return true;
2877
2878 return false;
2879}
2880
2881static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request)
2882{
Ilya Dryomov058aa992016-09-12 14:44:45 +02002883 rbd_assert(obj_request_img_data_test(obj_request));
2884 rbd_assert(obj_request_type_valid(obj_request->type));
2885 rbd_assert(obj_request->img_request);
2886
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002887 if (img_obj_request_simple(obj_request)) {
Ilya Dryomov980917f2016-09-12 18:59:42 +02002888 rbd_obj_request_submit(obj_request);
2889 return 0;
Alex Elderb454e362013-04-19 15:34:50 -05002890 }
2891
2892 /*
Alex Elder3d7efd12013-04-19 15:34:50 -05002893 * It's a layered write. The target object might exist but
2894 * we may not know that yet. If we know it doesn't exist,
2895 * start by reading the data for the full target object from
2896 * the parent so we can use it for a copyup to the target.
Alex Elderb454e362013-04-19 15:34:50 -05002897 */
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002898 if (obj_request_known_test(obj_request))
Alex Elder3d7efd12013-04-19 15:34:50 -05002899 return rbd_img_obj_parent_read_full(obj_request);
2900
2901 /* We don't know whether the target exists. Go find out. */
Alex Elderb454e362013-04-19 15:34:50 -05002902
2903 return rbd_img_obj_exists_submit(obj_request);
2904}
2905
Alex Elderbf0d5f502012-11-22 00:00:08 -06002906static int rbd_img_request_submit(struct rbd_img_request *img_request)
2907{
Alex Elderbf0d5f502012-11-22 00:00:08 -06002908 struct rbd_obj_request *obj_request;
Alex Elder46faeed2013-04-10 17:47:46 -05002909 struct rbd_obj_request *next_obj_request;
Ilya Dryomov663ae2c2016-05-16 13:18:57 +02002910 int ret = 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002911
Alex Elder37206ee2013-02-20 17:32:08 -06002912 dout("%s: img %p\n", __func__, img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002913
Ilya Dryomov663ae2c2016-05-16 13:18:57 +02002914 rbd_img_request_get(img_request);
2915 for_each_obj_request_safe(img_request, obj_request, next_obj_request) {
Alex Elderb454e362013-04-19 15:34:50 -05002916 ret = rbd_img_obj_request_submit(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002917 if (ret)
Ilya Dryomov663ae2c2016-05-16 13:18:57 +02002918 goto out_put_ireq;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002919 }
2920
Ilya Dryomov663ae2c2016-05-16 13:18:57 +02002921out_put_ireq:
2922 rbd_img_request_put(img_request);
2923 return ret;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002924}
2925
Alex Elder8b3e1a52013-01-24 16:13:36 -06002926static void rbd_img_parent_read_callback(struct rbd_img_request *img_request)
2927{
2928 struct rbd_obj_request *obj_request;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002929 struct rbd_device *rbd_dev;
2930 u64 obj_end;
Alex Elder02c74fb2013-05-06 17:40:33 -05002931 u64 img_xferred;
2932 int img_result;
Alex Elder8b3e1a52013-01-24 16:13:36 -06002933
2934 rbd_assert(img_request_child_test(img_request));
2935
Alex Elder02c74fb2013-05-06 17:40:33 -05002936 /* First get what we need from the image request and release it */
2937
Alex Elder8b3e1a52013-01-24 16:13:36 -06002938 obj_request = img_request->obj_request;
Alex Elder02c74fb2013-05-06 17:40:33 -05002939 img_xferred = img_request->xferred;
2940 img_result = img_request->result;
2941 rbd_img_request_put(img_request);
2942
2943 /*
2944 * If the overlap has become 0 (most likely because the
2945 * image has been flattened) we need to re-submit the
2946 * original request.
2947 */
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002948 rbd_assert(obj_request);
2949 rbd_assert(obj_request->img_request);
Alex Elder02c74fb2013-05-06 17:40:33 -05002950 rbd_dev = obj_request->img_request->rbd_dev;
2951 if (!rbd_dev->parent_overlap) {
Ilya Dryomov980917f2016-09-12 18:59:42 +02002952 rbd_obj_request_submit(obj_request);
2953 return;
Alex Elder02c74fb2013-05-06 17:40:33 -05002954 }
2955
2956 obj_request->result = img_result;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002957 if (obj_request->result)
2958 goto out;
2959
2960 /*
2961 * We need to zero anything beyond the parent overlap
2962 * boundary. Since rbd_img_obj_request_read_callback()
2963 * will zero anything beyond the end of a short read, an
2964 * easy way to do this is to pretend the data from the
2965 * parent came up short--ending at the overlap boundary.
2966 */
2967 rbd_assert(obj_request->img_offset < U64_MAX - obj_request->length);
2968 obj_end = obj_request->img_offset + obj_request->length;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002969 if (obj_end > rbd_dev->parent_overlap) {
2970 u64 xferred = 0;
2971
2972 if (obj_request->img_offset < rbd_dev->parent_overlap)
2973 xferred = rbd_dev->parent_overlap -
2974 obj_request->img_offset;
2975
Alex Elder02c74fb2013-05-06 17:40:33 -05002976 obj_request->xferred = min(img_xferred, xferred);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002977 } else {
Alex Elder02c74fb2013-05-06 17:40:33 -05002978 obj_request->xferred = img_xferred;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002979 }
2980out:
Alex Elder8b3e1a52013-01-24 16:13:36 -06002981 rbd_img_obj_request_read_callback(obj_request);
2982 rbd_obj_request_complete(obj_request);
2983}
2984
2985static void rbd_img_parent_read(struct rbd_obj_request *obj_request)
2986{
Alex Elder8b3e1a52013-01-24 16:13:36 -06002987 struct rbd_img_request *img_request;
2988 int result;
2989
2990 rbd_assert(obj_request_img_data_test(obj_request));
2991 rbd_assert(obj_request->img_request != NULL);
2992 rbd_assert(obj_request->result == (s32) -ENOENT);
Alex Elder5b2ab722013-05-06 17:40:33 -05002993 rbd_assert(obj_request_type_valid(obj_request->type));
Alex Elder8b3e1a52013-01-24 16:13:36 -06002994
Alex Elder8b3e1a52013-01-24 16:13:36 -06002995 /* rbd_read_finish(obj_request, obj_request->length); */
Alex Eldere93f3152013-05-08 22:50:04 -05002996 img_request = rbd_parent_request_create(obj_request,
Alex Elder8b3e1a52013-01-24 16:13:36 -06002997 obj_request->img_offset,
Alex Eldere93f3152013-05-08 22:50:04 -05002998 obj_request->length);
Alex Elder8b3e1a52013-01-24 16:13:36 -06002999 result = -ENOMEM;
3000 if (!img_request)
3001 goto out_err;
3002
Alex Elder5b2ab722013-05-06 17:40:33 -05003003 if (obj_request->type == OBJ_REQUEST_BIO)
3004 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
3005 obj_request->bio_list);
3006 else
3007 result = rbd_img_request_fill(img_request, OBJ_REQUEST_PAGES,
3008 obj_request->pages);
Alex Elder8b3e1a52013-01-24 16:13:36 -06003009 if (result)
3010 goto out_err;
3011
3012 img_request->callback = rbd_img_parent_read_callback;
3013 result = rbd_img_request_submit(img_request);
3014 if (result)
3015 goto out_err;
3016
3017 return;
3018out_err:
3019 if (img_request)
3020 rbd_img_request_put(img_request);
3021 obj_request->result = result;
3022 obj_request->xferred = 0;
3023 obj_request_done_set(obj_request);
3024}
3025
Ilya Dryomoved95b212016-08-12 16:40:02 +02003026static const struct rbd_client_id rbd_empty_cid;
3027
3028static bool rbd_cid_equal(const struct rbd_client_id *lhs,
3029 const struct rbd_client_id *rhs)
3030{
3031 return lhs->gid == rhs->gid && lhs->handle == rhs->handle;
3032}
3033
3034static struct rbd_client_id rbd_get_cid(struct rbd_device *rbd_dev)
3035{
3036 struct rbd_client_id cid;
3037
3038 mutex_lock(&rbd_dev->watch_mutex);
3039 cid.gid = ceph_client_gid(rbd_dev->rbd_client->client);
3040 cid.handle = rbd_dev->watch_cookie;
3041 mutex_unlock(&rbd_dev->watch_mutex);
3042 return cid;
3043}
3044
3045/*
3046 * lock_rwsem must be held for write
3047 */
3048static void rbd_set_owner_cid(struct rbd_device *rbd_dev,
3049 const struct rbd_client_id *cid)
3050{
3051 dout("%s rbd_dev %p %llu-%llu -> %llu-%llu\n", __func__, rbd_dev,
3052 rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle,
3053 cid->gid, cid->handle);
3054 rbd_dev->owner_cid = *cid; /* struct */
3055}
3056
3057static void format_lock_cookie(struct rbd_device *rbd_dev, char *buf)
3058{
3059 mutex_lock(&rbd_dev->watch_mutex);
3060 sprintf(buf, "%s %llu", RBD_LOCK_COOKIE_PREFIX, rbd_dev->watch_cookie);
3061 mutex_unlock(&rbd_dev->watch_mutex);
3062}
3063
3064/*
3065 * lock_rwsem must be held for write
3066 */
3067static int rbd_lock(struct rbd_device *rbd_dev)
3068{
3069 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3070 struct rbd_client_id cid = rbd_get_cid(rbd_dev);
3071 char cookie[32];
3072 int ret;
3073
3074 WARN_ON(__rbd_is_lock_owner(rbd_dev));
3075
3076 format_lock_cookie(rbd_dev, cookie);
3077 ret = ceph_cls_lock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc,
3078 RBD_LOCK_NAME, CEPH_CLS_LOCK_EXCLUSIVE, cookie,
3079 RBD_LOCK_TAG, "", 0);
3080 if (ret)
3081 return ret;
3082
3083 rbd_dev->lock_state = RBD_LOCK_STATE_LOCKED;
3084 rbd_set_owner_cid(rbd_dev, &cid);
3085 queue_work(rbd_dev->task_wq, &rbd_dev->acquired_lock_work);
3086 return 0;
3087}
3088
3089/*
3090 * lock_rwsem must be held for write
3091 */
3092static int rbd_unlock(struct rbd_device *rbd_dev)
3093{
3094 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3095 char cookie[32];
3096 int ret;
3097
3098 WARN_ON(!__rbd_is_lock_owner(rbd_dev));
3099
3100 rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED;
3101
3102 format_lock_cookie(rbd_dev, cookie);
3103 ret = ceph_cls_unlock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc,
3104 RBD_LOCK_NAME, cookie);
3105 if (ret && ret != -ENOENT) {
3106 rbd_warn(rbd_dev, "cls_unlock failed: %d", ret);
3107 return ret;
3108 }
3109
3110 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
3111 queue_work(rbd_dev->task_wq, &rbd_dev->released_lock_work);
3112 return 0;
3113}
3114
3115static int __rbd_notify_op_lock(struct rbd_device *rbd_dev,
3116 enum rbd_notify_op notify_op,
3117 struct page ***preply_pages,
3118 size_t *preply_len)
3119{
3120 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3121 struct rbd_client_id cid = rbd_get_cid(rbd_dev);
3122 int buf_size = 4 + 8 + 8 + CEPH_ENCODING_START_BLK_LEN;
3123 char buf[buf_size];
3124 void *p = buf;
3125
3126 dout("%s rbd_dev %p notify_op %d\n", __func__, rbd_dev, notify_op);
3127
3128 /* encode *LockPayload NotifyMessage (op + ClientId) */
3129 ceph_start_encoding(&p, 2, 1, buf_size - CEPH_ENCODING_START_BLK_LEN);
3130 ceph_encode_32(&p, notify_op);
3131 ceph_encode_64(&p, cid.gid);
3132 ceph_encode_64(&p, cid.handle);
3133
3134 return ceph_osdc_notify(osdc, &rbd_dev->header_oid,
3135 &rbd_dev->header_oloc, buf, buf_size,
3136 RBD_NOTIFY_TIMEOUT, preply_pages, preply_len);
3137}
3138
3139static void rbd_notify_op_lock(struct rbd_device *rbd_dev,
3140 enum rbd_notify_op notify_op)
3141{
3142 struct page **reply_pages;
3143 size_t reply_len;
3144
3145 __rbd_notify_op_lock(rbd_dev, notify_op, &reply_pages, &reply_len);
3146 ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len));
3147}
3148
3149static void rbd_notify_acquired_lock(struct work_struct *work)
3150{
3151 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
3152 acquired_lock_work);
3153
3154 rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_ACQUIRED_LOCK);
3155}
3156
3157static void rbd_notify_released_lock(struct work_struct *work)
3158{
3159 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
3160 released_lock_work);
3161
3162 rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_RELEASED_LOCK);
3163}
3164
3165static int rbd_request_lock(struct rbd_device *rbd_dev)
3166{
3167 struct page **reply_pages;
3168 size_t reply_len;
3169 bool lock_owner_responded = false;
3170 int ret;
3171
3172 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3173
3174 ret = __rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_REQUEST_LOCK,
3175 &reply_pages, &reply_len);
3176 if (ret && ret != -ETIMEDOUT) {
3177 rbd_warn(rbd_dev, "failed to request lock: %d", ret);
3178 goto out;
3179 }
3180
3181 if (reply_len > 0 && reply_len <= PAGE_SIZE) {
3182 void *p = page_address(reply_pages[0]);
3183 void *const end = p + reply_len;
3184 u32 n;
3185
3186 ceph_decode_32_safe(&p, end, n, e_inval); /* num_acks */
3187 while (n--) {
3188 u8 struct_v;
3189 u32 len;
3190
3191 ceph_decode_need(&p, end, 8 + 8, e_inval);
3192 p += 8 + 8; /* skip gid and cookie */
3193
3194 ceph_decode_32_safe(&p, end, len, e_inval);
3195 if (!len)
3196 continue;
3197
3198 if (lock_owner_responded) {
3199 rbd_warn(rbd_dev,
3200 "duplicate lock owners detected");
3201 ret = -EIO;
3202 goto out;
3203 }
3204
3205 lock_owner_responded = true;
3206 ret = ceph_start_decoding(&p, end, 1, "ResponseMessage",
3207 &struct_v, &len);
3208 if (ret) {
3209 rbd_warn(rbd_dev,
3210 "failed to decode ResponseMessage: %d",
3211 ret);
3212 goto e_inval;
3213 }
3214
3215 ret = ceph_decode_32(&p);
3216 }
3217 }
3218
3219 if (!lock_owner_responded) {
3220 rbd_warn(rbd_dev, "no lock owners detected");
3221 ret = -ETIMEDOUT;
3222 }
3223
3224out:
3225 ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len));
3226 return ret;
3227
3228e_inval:
3229 ret = -EINVAL;
3230 goto out;
3231}
3232
3233static void wake_requests(struct rbd_device *rbd_dev, bool wake_all)
3234{
3235 dout("%s rbd_dev %p wake_all %d\n", __func__, rbd_dev, wake_all);
3236
3237 cancel_delayed_work(&rbd_dev->lock_dwork);
3238 if (wake_all)
3239 wake_up_all(&rbd_dev->lock_waitq);
3240 else
3241 wake_up(&rbd_dev->lock_waitq);
3242}
3243
3244static int get_lock_owner_info(struct rbd_device *rbd_dev,
3245 struct ceph_locker **lockers, u32 *num_lockers)
3246{
3247 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3248 u8 lock_type;
3249 char *lock_tag;
3250 int ret;
3251
3252 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3253
3254 ret = ceph_cls_lock_info(osdc, &rbd_dev->header_oid,
3255 &rbd_dev->header_oloc, RBD_LOCK_NAME,
3256 &lock_type, &lock_tag, lockers, num_lockers);
3257 if (ret)
3258 return ret;
3259
3260 if (*num_lockers == 0) {
3261 dout("%s rbd_dev %p no lockers detected\n", __func__, rbd_dev);
3262 goto out;
3263 }
3264
3265 if (strcmp(lock_tag, RBD_LOCK_TAG)) {
3266 rbd_warn(rbd_dev, "locked by external mechanism, tag %s",
3267 lock_tag);
3268 ret = -EBUSY;
3269 goto out;
3270 }
3271
3272 if (lock_type == CEPH_CLS_LOCK_SHARED) {
3273 rbd_warn(rbd_dev, "shared lock type detected");
3274 ret = -EBUSY;
3275 goto out;
3276 }
3277
3278 if (strncmp((*lockers)[0].id.cookie, RBD_LOCK_COOKIE_PREFIX,
3279 strlen(RBD_LOCK_COOKIE_PREFIX))) {
3280 rbd_warn(rbd_dev, "locked by external mechanism, cookie %s",
3281 (*lockers)[0].id.cookie);
3282 ret = -EBUSY;
3283 goto out;
3284 }
3285
3286out:
3287 kfree(lock_tag);
3288 return ret;
3289}
3290
3291static int find_watcher(struct rbd_device *rbd_dev,
3292 const struct ceph_locker *locker)
3293{
3294 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3295 struct ceph_watch_item *watchers;
3296 u32 num_watchers;
3297 u64 cookie;
3298 int i;
3299 int ret;
3300
3301 ret = ceph_osdc_list_watchers(osdc, &rbd_dev->header_oid,
3302 &rbd_dev->header_oloc, &watchers,
3303 &num_watchers);
3304 if (ret)
3305 return ret;
3306
3307 sscanf(locker->id.cookie, RBD_LOCK_COOKIE_PREFIX " %llu", &cookie);
3308 for (i = 0; i < num_watchers; i++) {
3309 if (!memcmp(&watchers[i].addr, &locker->info.addr,
3310 sizeof(locker->info.addr)) &&
3311 watchers[i].cookie == cookie) {
3312 struct rbd_client_id cid = {
3313 .gid = le64_to_cpu(watchers[i].name.num),
3314 .handle = cookie,
3315 };
3316
3317 dout("%s rbd_dev %p found cid %llu-%llu\n", __func__,
3318 rbd_dev, cid.gid, cid.handle);
3319 rbd_set_owner_cid(rbd_dev, &cid);
3320 ret = 1;
3321 goto out;
3322 }
3323 }
3324
3325 dout("%s rbd_dev %p no watchers\n", __func__, rbd_dev);
3326 ret = 0;
3327out:
3328 kfree(watchers);
3329 return ret;
3330}
3331
3332/*
3333 * lock_rwsem must be held for write
3334 */
3335static int rbd_try_lock(struct rbd_device *rbd_dev)
3336{
3337 struct ceph_client *client = rbd_dev->rbd_client->client;
3338 struct ceph_locker *lockers;
3339 u32 num_lockers;
3340 int ret;
3341
3342 for (;;) {
3343 ret = rbd_lock(rbd_dev);
3344 if (ret != -EBUSY)
3345 return ret;
3346
3347 /* determine if the current lock holder is still alive */
3348 ret = get_lock_owner_info(rbd_dev, &lockers, &num_lockers);
3349 if (ret)
3350 return ret;
3351
3352 if (num_lockers == 0)
3353 goto again;
3354
3355 ret = find_watcher(rbd_dev, lockers);
3356 if (ret) {
3357 if (ret > 0)
3358 ret = 0; /* have to request lock */
3359 goto out;
3360 }
3361
3362 rbd_warn(rbd_dev, "%s%llu seems dead, breaking lock",
3363 ENTITY_NAME(lockers[0].id.name));
3364
3365 ret = ceph_monc_blacklist_add(&client->monc,
3366 &lockers[0].info.addr);
3367 if (ret) {
3368 rbd_warn(rbd_dev, "blacklist of %s%llu failed: %d",
3369 ENTITY_NAME(lockers[0].id.name), ret);
3370 goto out;
3371 }
3372
3373 ret = ceph_cls_break_lock(&client->osdc, &rbd_dev->header_oid,
3374 &rbd_dev->header_oloc, RBD_LOCK_NAME,
3375 lockers[0].id.cookie,
3376 &lockers[0].id.name);
3377 if (ret && ret != -ENOENT)
3378 goto out;
3379
3380again:
3381 ceph_free_lockers(lockers, num_lockers);
3382 }
3383
3384out:
3385 ceph_free_lockers(lockers, num_lockers);
3386 return ret;
3387}
3388
3389/*
3390 * ret is set only if lock_state is RBD_LOCK_STATE_UNLOCKED
3391 */
3392static enum rbd_lock_state rbd_try_acquire_lock(struct rbd_device *rbd_dev,
3393 int *pret)
3394{
3395 enum rbd_lock_state lock_state;
3396
3397 down_read(&rbd_dev->lock_rwsem);
3398 dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev,
3399 rbd_dev->lock_state);
3400 if (__rbd_is_lock_owner(rbd_dev)) {
3401 lock_state = rbd_dev->lock_state;
3402 up_read(&rbd_dev->lock_rwsem);
3403 return lock_state;
3404 }
3405
3406 up_read(&rbd_dev->lock_rwsem);
3407 down_write(&rbd_dev->lock_rwsem);
3408 dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev,
3409 rbd_dev->lock_state);
3410 if (!__rbd_is_lock_owner(rbd_dev)) {
3411 *pret = rbd_try_lock(rbd_dev);
3412 if (*pret)
3413 rbd_warn(rbd_dev, "failed to acquire lock: %d", *pret);
3414 }
3415
3416 lock_state = rbd_dev->lock_state;
3417 up_write(&rbd_dev->lock_rwsem);
3418 return lock_state;
3419}
3420
3421static void rbd_acquire_lock(struct work_struct *work)
3422{
3423 struct rbd_device *rbd_dev = container_of(to_delayed_work(work),
3424 struct rbd_device, lock_dwork);
3425 enum rbd_lock_state lock_state;
3426 int ret;
3427
3428 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3429again:
3430 lock_state = rbd_try_acquire_lock(rbd_dev, &ret);
3431 if (lock_state != RBD_LOCK_STATE_UNLOCKED || ret == -EBLACKLISTED) {
3432 if (lock_state == RBD_LOCK_STATE_LOCKED)
3433 wake_requests(rbd_dev, true);
3434 dout("%s rbd_dev %p lock_state %d ret %d - done\n", __func__,
3435 rbd_dev, lock_state, ret);
3436 return;
3437 }
3438
3439 ret = rbd_request_lock(rbd_dev);
3440 if (ret == -ETIMEDOUT) {
3441 goto again; /* treat this as a dead client */
3442 } else if (ret < 0) {
3443 rbd_warn(rbd_dev, "error requesting lock: %d", ret);
3444 mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork,
3445 RBD_RETRY_DELAY);
3446 } else {
3447 /*
3448 * lock owner acked, but resend if we don't see them
3449 * release the lock
3450 */
3451 dout("%s rbd_dev %p requeueing lock_dwork\n", __func__,
3452 rbd_dev);
3453 mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork,
3454 msecs_to_jiffies(2 * RBD_NOTIFY_TIMEOUT * MSEC_PER_SEC));
3455 }
3456}
3457
3458/*
3459 * lock_rwsem must be held for write
3460 */
3461static bool rbd_release_lock(struct rbd_device *rbd_dev)
3462{
3463 dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev,
3464 rbd_dev->lock_state);
3465 if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED)
3466 return false;
3467
3468 rbd_dev->lock_state = RBD_LOCK_STATE_RELEASING;
3469 downgrade_write(&rbd_dev->lock_rwsem);
3470 /*
3471 * Ensure that all in-flight IO is flushed.
3472 *
3473 * FIXME: ceph_osdc_sync() flushes the entire OSD client, which
3474 * may be shared with other devices.
3475 */
3476 ceph_osdc_sync(&rbd_dev->rbd_client->client->osdc);
3477 up_read(&rbd_dev->lock_rwsem);
3478
3479 down_write(&rbd_dev->lock_rwsem);
3480 dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev,
3481 rbd_dev->lock_state);
3482 if (rbd_dev->lock_state != RBD_LOCK_STATE_RELEASING)
3483 return false;
3484
3485 if (!rbd_unlock(rbd_dev))
3486 /*
3487 * Give others a chance to grab the lock - we would re-acquire
3488 * almost immediately if we got new IO during ceph_osdc_sync()
3489 * otherwise. We need to ack our own notifications, so this
3490 * lock_dwork will be requeued from rbd_wait_state_locked()
3491 * after wake_requests() in rbd_handle_released_lock().
3492 */
3493 cancel_delayed_work(&rbd_dev->lock_dwork);
3494
3495 return true;
3496}
3497
3498static void rbd_release_lock_work(struct work_struct *work)
3499{
3500 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
3501 unlock_work);
3502
3503 down_write(&rbd_dev->lock_rwsem);
3504 rbd_release_lock(rbd_dev);
3505 up_write(&rbd_dev->lock_rwsem);
3506}
3507
3508static void rbd_handle_acquired_lock(struct rbd_device *rbd_dev, u8 struct_v,
3509 void **p)
3510{
3511 struct rbd_client_id cid = { 0 };
3512
3513 if (struct_v >= 2) {
3514 cid.gid = ceph_decode_64(p);
3515 cid.handle = ceph_decode_64(p);
3516 }
3517
3518 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
3519 cid.handle);
3520 if (!rbd_cid_equal(&cid, &rbd_empty_cid)) {
3521 down_write(&rbd_dev->lock_rwsem);
3522 if (rbd_cid_equal(&cid, &rbd_dev->owner_cid)) {
3523 /*
3524 * we already know that the remote client is
3525 * the owner
3526 */
3527 up_write(&rbd_dev->lock_rwsem);
3528 return;
3529 }
3530
3531 rbd_set_owner_cid(rbd_dev, &cid);
3532 downgrade_write(&rbd_dev->lock_rwsem);
3533 } else {
3534 down_read(&rbd_dev->lock_rwsem);
3535 }
3536
3537 if (!__rbd_is_lock_owner(rbd_dev))
3538 wake_requests(rbd_dev, false);
3539 up_read(&rbd_dev->lock_rwsem);
3540}
3541
3542static void rbd_handle_released_lock(struct rbd_device *rbd_dev, u8 struct_v,
3543 void **p)
3544{
3545 struct rbd_client_id cid = { 0 };
3546
3547 if (struct_v >= 2) {
3548 cid.gid = ceph_decode_64(p);
3549 cid.handle = ceph_decode_64(p);
3550 }
3551
3552 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
3553 cid.handle);
3554 if (!rbd_cid_equal(&cid, &rbd_empty_cid)) {
3555 down_write(&rbd_dev->lock_rwsem);
3556 if (!rbd_cid_equal(&cid, &rbd_dev->owner_cid)) {
3557 dout("%s rbd_dev %p unexpected owner, cid %llu-%llu != owner_cid %llu-%llu\n",
3558 __func__, rbd_dev, cid.gid, cid.handle,
3559 rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle);
3560 up_write(&rbd_dev->lock_rwsem);
3561 return;
3562 }
3563
3564 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
3565 downgrade_write(&rbd_dev->lock_rwsem);
3566 } else {
3567 down_read(&rbd_dev->lock_rwsem);
3568 }
3569
3570 if (!__rbd_is_lock_owner(rbd_dev))
3571 wake_requests(rbd_dev, false);
3572 up_read(&rbd_dev->lock_rwsem);
3573}
3574
3575static bool rbd_handle_request_lock(struct rbd_device *rbd_dev, u8 struct_v,
3576 void **p)
3577{
3578 struct rbd_client_id my_cid = rbd_get_cid(rbd_dev);
3579 struct rbd_client_id cid = { 0 };
3580 bool need_to_send;
3581
3582 if (struct_v >= 2) {
3583 cid.gid = ceph_decode_64(p);
3584 cid.handle = ceph_decode_64(p);
3585 }
3586
3587 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
3588 cid.handle);
3589 if (rbd_cid_equal(&cid, &my_cid))
3590 return false;
3591
3592 down_read(&rbd_dev->lock_rwsem);
3593 need_to_send = __rbd_is_lock_owner(rbd_dev);
3594 if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) {
3595 if (!rbd_cid_equal(&rbd_dev->owner_cid, &rbd_empty_cid)) {
3596 dout("%s rbd_dev %p queueing unlock_work\n", __func__,
3597 rbd_dev);
3598 queue_work(rbd_dev->task_wq, &rbd_dev->unlock_work);
3599 }
3600 }
3601 up_read(&rbd_dev->lock_rwsem);
3602 return need_to_send;
3603}
3604
3605static void __rbd_acknowledge_notify(struct rbd_device *rbd_dev,
3606 u64 notify_id, u64 cookie, s32 *result)
3607{
3608 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3609 int buf_size = 4 + CEPH_ENCODING_START_BLK_LEN;
3610 char buf[buf_size];
3611 int ret;
3612
3613 if (result) {
3614 void *p = buf;
3615
3616 /* encode ResponseMessage */
3617 ceph_start_encoding(&p, 1, 1,
3618 buf_size - CEPH_ENCODING_START_BLK_LEN);
3619 ceph_encode_32(&p, *result);
3620 } else {
3621 buf_size = 0;
3622 }
3623
3624 ret = ceph_osdc_notify_ack(osdc, &rbd_dev->header_oid,
3625 &rbd_dev->header_oloc, notify_id, cookie,
3626 buf, buf_size);
3627 if (ret)
3628 rbd_warn(rbd_dev, "acknowledge_notify failed: %d", ret);
3629}
3630
3631static void rbd_acknowledge_notify(struct rbd_device *rbd_dev, u64 notify_id,
3632 u64 cookie)
3633{
3634 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3635 __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, NULL);
3636}
3637
3638static void rbd_acknowledge_notify_result(struct rbd_device *rbd_dev,
3639 u64 notify_id, u64 cookie, s32 result)
3640{
3641 dout("%s rbd_dev %p result %d\n", __func__, rbd_dev, result);
3642 __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, &result);
3643}
Ilya Dryomov922dab62016-05-26 01:15:02 +02003644
3645static void rbd_watch_cb(void *arg, u64 notify_id, u64 cookie,
3646 u64 notifier_id, void *data, size_t data_len)
Alex Elderb8d70032012-11-30 17:53:04 -06003647{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003648 struct rbd_device *rbd_dev = arg;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003649 void *p = data;
3650 void *const end = p + data_len;
Ilya Dryomovd4c22692016-09-06 11:15:48 +02003651 u8 struct_v = 0;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003652 u32 len;
3653 u32 notify_op;
Alex Elderb8d70032012-11-30 17:53:04 -06003654 int ret;
3655
Ilya Dryomoved95b212016-08-12 16:40:02 +02003656 dout("%s rbd_dev %p cookie %llu notify_id %llu data_len %zu\n",
3657 __func__, rbd_dev, cookie, notify_id, data_len);
3658 if (data_len) {
3659 ret = ceph_start_decoding(&p, end, 1, "NotifyMessage",
3660 &struct_v, &len);
3661 if (ret) {
3662 rbd_warn(rbd_dev, "failed to decode NotifyMessage: %d",
3663 ret);
3664 return;
3665 }
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04003666
Ilya Dryomoved95b212016-08-12 16:40:02 +02003667 notify_op = ceph_decode_32(&p);
3668 } else {
3669 /* legacy notification for header updates */
3670 notify_op = RBD_NOTIFY_OP_HEADER_UPDATE;
3671 len = 0;
3672 }
Alex Elderb8d70032012-11-30 17:53:04 -06003673
Ilya Dryomoved95b212016-08-12 16:40:02 +02003674 dout("%s rbd_dev %p notify_op %u\n", __func__, rbd_dev, notify_op);
3675 switch (notify_op) {
3676 case RBD_NOTIFY_OP_ACQUIRED_LOCK:
3677 rbd_handle_acquired_lock(rbd_dev, struct_v, &p);
3678 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3679 break;
3680 case RBD_NOTIFY_OP_RELEASED_LOCK:
3681 rbd_handle_released_lock(rbd_dev, struct_v, &p);
3682 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3683 break;
3684 case RBD_NOTIFY_OP_REQUEST_LOCK:
3685 if (rbd_handle_request_lock(rbd_dev, struct_v, &p))
3686 /*
3687 * send ResponseMessage(0) back so the client
3688 * can detect a missing owner
3689 */
3690 rbd_acknowledge_notify_result(rbd_dev, notify_id,
3691 cookie, 0);
3692 else
3693 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3694 break;
3695 case RBD_NOTIFY_OP_HEADER_UPDATE:
3696 ret = rbd_dev_refresh(rbd_dev);
3697 if (ret)
3698 rbd_warn(rbd_dev, "refresh failed: %d", ret);
3699
3700 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3701 break;
3702 default:
3703 if (rbd_is_lock_owner(rbd_dev))
3704 rbd_acknowledge_notify_result(rbd_dev, notify_id,
3705 cookie, -EOPNOTSUPP);
3706 else
3707 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3708 break;
3709 }
Alex Elderb8d70032012-11-30 17:53:04 -06003710}
3711
Ilya Dryomov99d16942016-08-12 16:11:41 +02003712static void __rbd_unregister_watch(struct rbd_device *rbd_dev);
3713
Ilya Dryomov922dab62016-05-26 01:15:02 +02003714static void rbd_watch_errcb(void *arg, u64 cookie, int err)
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003715{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003716 struct rbd_device *rbd_dev = arg;
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003717
Ilya Dryomov922dab62016-05-26 01:15:02 +02003718 rbd_warn(rbd_dev, "encountered watch error: %d", err);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003719
Ilya Dryomoved95b212016-08-12 16:40:02 +02003720 down_write(&rbd_dev->lock_rwsem);
3721 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
3722 up_write(&rbd_dev->lock_rwsem);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003723
Ilya Dryomov99d16942016-08-12 16:11:41 +02003724 mutex_lock(&rbd_dev->watch_mutex);
3725 if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) {
3726 __rbd_unregister_watch(rbd_dev);
3727 rbd_dev->watch_state = RBD_WATCH_STATE_ERROR;
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003728
Ilya Dryomov99d16942016-08-12 16:11:41 +02003729 queue_delayed_work(rbd_dev->task_wq, &rbd_dev->watch_dwork, 0);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003730 }
Ilya Dryomov99d16942016-08-12 16:11:41 +02003731 mutex_unlock(&rbd_dev->watch_mutex);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003732}
3733
3734/*
Ilya Dryomov99d16942016-08-12 16:11:41 +02003735 * watch_mutex must be locked
Alex Elder9969ebc2013-01-18 12:31:10 -06003736 */
Ilya Dryomov99d16942016-08-12 16:11:41 +02003737static int __rbd_register_watch(struct rbd_device *rbd_dev)
Alex Elder9969ebc2013-01-18 12:31:10 -06003738{
3739 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Ilya Dryomov922dab62016-05-26 01:15:02 +02003740 struct ceph_osd_linger_request *handle;
Alex Elder9969ebc2013-01-18 12:31:10 -06003741
Ilya Dryomov922dab62016-05-26 01:15:02 +02003742 rbd_assert(!rbd_dev->watch_handle);
Ilya Dryomov99d16942016-08-12 16:11:41 +02003743 dout("%s rbd_dev %p\n", __func__, rbd_dev);
Alex Elder9969ebc2013-01-18 12:31:10 -06003744
Ilya Dryomov922dab62016-05-26 01:15:02 +02003745 handle = ceph_osdc_watch(osdc, &rbd_dev->header_oid,
3746 &rbd_dev->header_oloc, rbd_watch_cb,
3747 rbd_watch_errcb, rbd_dev);
3748 if (IS_ERR(handle))
3749 return PTR_ERR(handle);
Alex Elder9969ebc2013-01-18 12:31:10 -06003750
Ilya Dryomov922dab62016-05-26 01:15:02 +02003751 rbd_dev->watch_handle = handle;
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003752 return 0;
Alex Elder9969ebc2013-01-18 12:31:10 -06003753}
3754
Ilya Dryomov99d16942016-08-12 16:11:41 +02003755/*
3756 * watch_mutex must be locked
3757 */
3758static void __rbd_unregister_watch(struct rbd_device *rbd_dev)
Ilya Dryomovfca27062013-12-16 18:02:40 +02003759{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003760 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3761 int ret;
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003762
Ilya Dryomov99d16942016-08-12 16:11:41 +02003763 rbd_assert(rbd_dev->watch_handle);
3764 dout("%s rbd_dev %p\n", __func__, rbd_dev);
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003765
Ilya Dryomov922dab62016-05-26 01:15:02 +02003766 ret = ceph_osdc_unwatch(osdc, rbd_dev->watch_handle);
3767 if (ret)
3768 rbd_warn(rbd_dev, "failed to unwatch: %d", ret);
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003769
Ilya Dryomov922dab62016-05-26 01:15:02 +02003770 rbd_dev->watch_handle = NULL;
Ilya Dryomovc525f032016-04-28 16:07:26 +02003771}
3772
Ilya Dryomov99d16942016-08-12 16:11:41 +02003773static int rbd_register_watch(struct rbd_device *rbd_dev)
Ilya Dryomovc525f032016-04-28 16:07:26 +02003774{
Ilya Dryomov99d16942016-08-12 16:11:41 +02003775 int ret;
Ilya Dryomov811c6682016-04-15 16:22:16 +02003776
Ilya Dryomov99d16942016-08-12 16:11:41 +02003777 mutex_lock(&rbd_dev->watch_mutex);
3778 rbd_assert(rbd_dev->watch_state == RBD_WATCH_STATE_UNREGISTERED);
3779 ret = __rbd_register_watch(rbd_dev);
3780 if (ret)
3781 goto out;
3782
3783 rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED;
3784 rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id;
3785
3786out:
3787 mutex_unlock(&rbd_dev->watch_mutex);
3788 return ret;
3789}
3790
3791static void cancel_tasks_sync(struct rbd_device *rbd_dev)
3792{
3793 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3794
3795 cancel_delayed_work_sync(&rbd_dev->watch_dwork);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003796 cancel_work_sync(&rbd_dev->acquired_lock_work);
3797 cancel_work_sync(&rbd_dev->released_lock_work);
3798 cancel_delayed_work_sync(&rbd_dev->lock_dwork);
3799 cancel_work_sync(&rbd_dev->unlock_work);
Ilya Dryomov99d16942016-08-12 16:11:41 +02003800}
3801
3802static void rbd_unregister_watch(struct rbd_device *rbd_dev)
3803{
Ilya Dryomoved95b212016-08-12 16:40:02 +02003804 WARN_ON(waitqueue_active(&rbd_dev->lock_waitq));
Ilya Dryomov99d16942016-08-12 16:11:41 +02003805 cancel_tasks_sync(rbd_dev);
3806
3807 mutex_lock(&rbd_dev->watch_mutex);
3808 if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED)
3809 __rbd_unregister_watch(rbd_dev);
3810 rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED;
3811 mutex_unlock(&rbd_dev->watch_mutex);
3812
Ilya Dryomov811c6682016-04-15 16:22:16 +02003813 ceph_osdc_flush_notifies(&rbd_dev->rbd_client->client->osdc);
Ilya Dryomovfca27062013-12-16 18:02:40 +02003814}
3815
Ilya Dryomov99d16942016-08-12 16:11:41 +02003816static void rbd_reregister_watch(struct work_struct *work)
3817{
3818 struct rbd_device *rbd_dev = container_of(to_delayed_work(work),
3819 struct rbd_device, watch_dwork);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003820 bool was_lock_owner = false;
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003821 bool need_to_wake = false;
Ilya Dryomov99d16942016-08-12 16:11:41 +02003822 int ret;
3823
3824 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3825
Ilya Dryomoved95b212016-08-12 16:40:02 +02003826 down_write(&rbd_dev->lock_rwsem);
3827 if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED)
3828 was_lock_owner = rbd_release_lock(rbd_dev);
3829
Ilya Dryomov99d16942016-08-12 16:11:41 +02003830 mutex_lock(&rbd_dev->watch_mutex);
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003831 if (rbd_dev->watch_state != RBD_WATCH_STATE_ERROR) {
3832 mutex_unlock(&rbd_dev->watch_mutex);
3833 goto out;
3834 }
Ilya Dryomov99d16942016-08-12 16:11:41 +02003835
3836 ret = __rbd_register_watch(rbd_dev);
3837 if (ret) {
3838 rbd_warn(rbd_dev, "failed to reregister watch: %d", ret);
Ilya Dryomov4d736442016-09-29 14:23:12 +02003839 if (ret == -EBLACKLISTED || ret == -ENOENT) {
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003840 set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags);
3841 need_to_wake = true;
3842 } else {
Ilya Dryomov99d16942016-08-12 16:11:41 +02003843 queue_delayed_work(rbd_dev->task_wq,
3844 &rbd_dev->watch_dwork,
3845 RBD_RETRY_DELAY);
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003846 }
3847 mutex_unlock(&rbd_dev->watch_mutex);
3848 goto out;
Ilya Dryomov99d16942016-08-12 16:11:41 +02003849 }
3850
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003851 need_to_wake = true;
Ilya Dryomov99d16942016-08-12 16:11:41 +02003852 rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED;
3853 rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id;
3854 mutex_unlock(&rbd_dev->watch_mutex);
3855
3856 ret = rbd_dev_refresh(rbd_dev);
3857 if (ret)
3858 rbd_warn(rbd_dev, "reregisteration refresh failed: %d", ret);
3859
Ilya Dryomoved95b212016-08-12 16:40:02 +02003860 if (was_lock_owner) {
3861 ret = rbd_try_lock(rbd_dev);
3862 if (ret)
3863 rbd_warn(rbd_dev, "reregisteration lock failed: %d",
3864 ret);
3865 }
3866
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003867out:
Ilya Dryomoved95b212016-08-12 16:40:02 +02003868 up_write(&rbd_dev->lock_rwsem);
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003869 if (need_to_wake)
3870 wake_requests(rbd_dev, true);
Ilya Dryomov99d16942016-08-12 16:11:41 +02003871}
3872
Alex Elder36be9a72013-01-19 00:30:28 -06003873/*
Alex Elderf40eb342013-04-25 15:09:42 -05003874 * Synchronous osd object method call. Returns the number of bytes
3875 * returned in the outbound buffer, or a negative error code.
Alex Elder36be9a72013-01-19 00:30:28 -06003876 */
3877static int rbd_obj_method_sync(struct rbd_device *rbd_dev,
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003878 struct ceph_object_id *oid,
3879 struct ceph_object_locator *oloc,
Alex Elder36be9a72013-01-19 00:30:28 -06003880 const char *method_name,
Alex Elder41579762013-04-21 12:14:45 -05003881 const void *outbound,
Alex Elder36be9a72013-01-19 00:30:28 -06003882 size_t outbound_size,
Alex Elder41579762013-04-21 12:14:45 -05003883 void *inbound,
Alex Eldere2a58ee2013-04-30 00:44:33 -05003884 size_t inbound_size)
Alex Elder36be9a72013-01-19 00:30:28 -06003885{
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003886 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3887 struct page *req_page = NULL;
3888 struct page *reply_page;
Alex Elder36be9a72013-01-19 00:30:28 -06003889 int ret;
3890
3891 /*
Alex Elder6010a452013-04-05 01:27:11 -05003892 * Method calls are ultimately read operations. The result
3893 * should placed into the inbound buffer provided. They
3894 * also supply outbound data--parameters for the object
3895 * method. Currently if this is present it will be a
3896 * snapshot id.
Alex Elder36be9a72013-01-19 00:30:28 -06003897 */
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003898 if (outbound) {
3899 if (outbound_size > PAGE_SIZE)
3900 return -E2BIG;
Alex Elder36be9a72013-01-19 00:30:28 -06003901
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003902 req_page = alloc_page(GFP_KERNEL);
3903 if (!req_page)
3904 return -ENOMEM;
Alex Elder36be9a72013-01-19 00:30:28 -06003905
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003906 memcpy(page_address(req_page), outbound, outbound_size);
Alex Elder04017e22013-04-05 14:46:02 -05003907 }
Alex Elder430c28c2013-04-03 21:32:51 -05003908
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003909 reply_page = alloc_page(GFP_KERNEL);
3910 if (!reply_page) {
3911 if (req_page)
3912 __free_page(req_page);
3913 return -ENOMEM;
3914 }
Alex Elder36be9a72013-01-19 00:30:28 -06003915
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003916 ret = ceph_osdc_call(osdc, oid, oloc, RBD_DRV_NAME, method_name,
3917 CEPH_OSD_FLAG_READ, req_page, outbound_size,
3918 reply_page, &inbound_size);
3919 if (!ret) {
3920 memcpy(inbound, page_address(reply_page), inbound_size);
3921 ret = inbound_size;
3922 }
Alex Elder57385b52013-04-21 12:14:45 -05003923
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003924 if (req_page)
3925 __free_page(req_page);
3926 __free_page(reply_page);
Alex Elder36be9a72013-01-19 00:30:28 -06003927 return ret;
3928}
3929
Ilya Dryomoved95b212016-08-12 16:40:02 +02003930/*
3931 * lock_rwsem must be held for read
3932 */
3933static void rbd_wait_state_locked(struct rbd_device *rbd_dev)
3934{
3935 DEFINE_WAIT(wait);
3936
3937 do {
3938 /*
3939 * Note the use of mod_delayed_work() in rbd_acquire_lock()
3940 * and cancel_delayed_work() in wake_requests().
3941 */
3942 dout("%s rbd_dev %p queueing lock_dwork\n", __func__, rbd_dev);
3943 queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0);
3944 prepare_to_wait_exclusive(&rbd_dev->lock_waitq, &wait,
3945 TASK_UNINTERRUPTIBLE);
3946 up_read(&rbd_dev->lock_rwsem);
3947 schedule();
3948 down_read(&rbd_dev->lock_rwsem);
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003949 } while (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED &&
3950 !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags));
3951
Ilya Dryomoved95b212016-08-12 16:40:02 +02003952 finish_wait(&rbd_dev->lock_waitq, &wait);
3953}
3954
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01003955static void rbd_queue_workfn(struct work_struct *work)
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04003956{
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01003957 struct request *rq = blk_mq_rq_from_pdu(work);
3958 struct rbd_device *rbd_dev = rq->q->queuedata;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04003959 struct rbd_img_request *img_request;
Josh Durgin4e752f02014-04-08 11:12:11 -07003960 struct ceph_snap_context *snapc = NULL;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04003961 u64 offset = (u64)blk_rq_pos(rq) << SECTOR_SHIFT;
3962 u64 length = blk_rq_bytes(rq);
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08003963 enum obj_operation_type op_type;
Josh Durgin4e752f02014-04-08 11:12:11 -07003964 u64 mapping_size;
Ilya Dryomov80de1912016-09-20 14:23:17 +02003965 bool must_be_locked;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04003966 int result;
3967
Christoph Hellwigaebf5262017-01-31 16:57:31 +01003968 switch (req_op(rq)) {
3969 case REQ_OP_DISCARD:
3970 op_type = OBJ_OP_DISCARD;
3971 break;
3972 case REQ_OP_WRITE:
3973 op_type = OBJ_OP_WRITE;
3974 break;
3975 case REQ_OP_READ:
3976 op_type = OBJ_OP_READ;
3977 break;
3978 default:
3979 dout("%s: non-fs request type %d\n", __func__, req_op(rq));
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01003980 result = -EIO;
3981 goto err;
3982 }
3983
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04003984 /* Ignore/skip any zero-length requests */
3985
3986 if (!length) {
3987 dout("%s: zero-length request\n", __func__);
3988 result = 0;
3989 goto err_rq;
3990 }
3991
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08003992 /* Only reads are allowed to a read-only device */
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04003993
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08003994 if (op_type != OBJ_OP_READ) {
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04003995 if (rbd_dev->mapping.read_only) {
3996 result = -EROFS;
3997 goto err_rq;
3998 }
3999 rbd_assert(rbd_dev->spec->snap_id == CEPH_NOSNAP);
4000 }
4001
4002 /*
4003 * Quit early if the mapped snapshot no longer exists. It's
4004 * still possible the snapshot will have disappeared by the
4005 * time our request arrives at the osd, but there's no sense in
4006 * sending it if we already know.
4007 */
4008 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) {
4009 dout("request for non-existent snapshot");
4010 rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP);
4011 result = -ENXIO;
4012 goto err_rq;
4013 }
4014
4015 if (offset && length > U64_MAX - offset + 1) {
4016 rbd_warn(rbd_dev, "bad request range (%llu~%llu)", offset,
4017 length);
4018 result = -EINVAL;
4019 goto err_rq; /* Shouldn't happen */
4020 }
4021
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004022 blk_mq_start_request(rq);
4023
Josh Durgin4e752f02014-04-08 11:12:11 -07004024 down_read(&rbd_dev->header_rwsem);
4025 mapping_size = rbd_dev->mapping.size;
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004026 if (op_type != OBJ_OP_READ) {
Josh Durgin4e752f02014-04-08 11:12:11 -07004027 snapc = rbd_dev->header.snapc;
4028 ceph_get_snap_context(snapc);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004029 must_be_locked = rbd_is_lock_supported(rbd_dev);
Ilya Dryomov80de1912016-09-20 14:23:17 +02004030 } else {
4031 must_be_locked = rbd_dev->opts->lock_on_read &&
4032 rbd_is_lock_supported(rbd_dev);
Josh Durgin4e752f02014-04-08 11:12:11 -07004033 }
4034 up_read(&rbd_dev->header_rwsem);
4035
4036 if (offset + length > mapping_size) {
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004037 rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)", offset,
Josh Durgin4e752f02014-04-08 11:12:11 -07004038 length, mapping_size);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004039 result = -EIO;
4040 goto err_rq;
4041 }
4042
Ilya Dryomoved95b212016-08-12 16:40:02 +02004043 if (must_be_locked) {
4044 down_read(&rbd_dev->lock_rwsem);
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02004045 if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED &&
4046 !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags))
Ilya Dryomoved95b212016-08-12 16:40:02 +02004047 rbd_wait_state_locked(rbd_dev);
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02004048
4049 WARN_ON((rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) ^
4050 !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags));
4051 if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) {
4052 result = -EBLACKLISTED;
4053 goto err_unlock;
4054 }
Ilya Dryomoved95b212016-08-12 16:40:02 +02004055 }
4056
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004057 img_request = rbd_img_request_create(rbd_dev, offset, length, op_type,
Josh Durgin4e752f02014-04-08 11:12:11 -07004058 snapc);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004059 if (!img_request) {
4060 result = -ENOMEM;
Ilya Dryomoved95b212016-08-12 16:40:02 +02004061 goto err_unlock;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004062 }
4063 img_request->rq = rq;
Ilya Dryomov70b16db2015-11-27 19:23:24 +01004064 snapc = NULL; /* img_request consumes a ref */
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004065
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004066 if (op_type == OBJ_OP_DISCARD)
4067 result = rbd_img_request_fill(img_request, OBJ_REQUEST_NODATA,
4068 NULL);
4069 else
4070 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
4071 rq->bio);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004072 if (result)
4073 goto err_img_request;
4074
4075 result = rbd_img_request_submit(img_request);
4076 if (result)
4077 goto err_img_request;
4078
Ilya Dryomoved95b212016-08-12 16:40:02 +02004079 if (must_be_locked)
4080 up_read(&rbd_dev->lock_rwsem);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004081 return;
4082
4083err_img_request:
4084 rbd_img_request_put(img_request);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004085err_unlock:
4086 if (must_be_locked)
4087 up_read(&rbd_dev->lock_rwsem);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004088err_rq:
4089 if (result)
4090 rbd_warn(rbd_dev, "%s %llx at %llx result %d",
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004091 obj_op_name(op_type), length, offset, result);
SF Markus Elfringe96a6502014-11-02 15:20:59 +01004092 ceph_put_snap_context(snapc);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004093err:
4094 blk_mq_end_request(rq, result);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004095}
4096
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004097static int rbd_queue_rq(struct blk_mq_hw_ctx *hctx,
4098 const struct blk_mq_queue_data *bd)
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004099{
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004100 struct request *rq = bd->rq;
4101 struct work_struct *work = blk_mq_rq_to_pdu(rq);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004102
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004103 queue_work(rbd_wq, work);
4104 return BLK_MQ_RQ_QUEUE_OK;
Alex Elderbf0d5f502012-11-22 00:00:08 -06004105}
4106
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004107static void rbd_free_disk(struct rbd_device *rbd_dev)
4108{
4109 struct gendisk *disk = rbd_dev->disk;
4110
4111 if (!disk)
4112 return;
4113
Alex Eldera0cab922013-04-25 23:15:08 -05004114 rbd_dev->disk = NULL;
4115 if (disk->flags & GENHD_FL_UP) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004116 del_gendisk(disk);
Alex Eldera0cab922013-04-25 23:15:08 -05004117 if (disk->queue)
4118 blk_cleanup_queue(disk->queue);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004119 blk_mq_free_tag_set(&rbd_dev->tag_set);
Alex Eldera0cab922013-04-25 23:15:08 -05004120 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004121 put_disk(disk);
4122}
4123
Alex Elder788e2df2013-01-17 12:25:27 -06004124static int rbd_obj_read_sync(struct rbd_device *rbd_dev,
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004125 struct ceph_object_id *oid,
4126 struct ceph_object_locator *oloc,
4127 void *buf, int buf_len)
Alex Elder788e2df2013-01-17 12:25:27 -06004128
4129{
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004130 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
4131 struct ceph_osd_request *req;
4132 struct page **pages;
4133 int num_pages = calc_pages_for(0, buf_len);
Alex Elder788e2df2013-01-17 12:25:27 -06004134 int ret;
4135
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004136 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL);
4137 if (!req)
4138 return -ENOMEM;
Alex Elder788e2df2013-01-17 12:25:27 -06004139
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004140 ceph_oid_copy(&req->r_base_oid, oid);
4141 ceph_oloc_copy(&req->r_base_oloc, oloc);
4142 req->r_flags = CEPH_OSD_FLAG_READ;
Alex Elder788e2df2013-01-17 12:25:27 -06004143
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004144 ret = ceph_osdc_alloc_messages(req, GFP_KERNEL);
Alex Elder788e2df2013-01-17 12:25:27 -06004145 if (ret)
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004146 goto out_req;
Alex Elder788e2df2013-01-17 12:25:27 -06004147
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004148 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
4149 if (IS_ERR(pages)) {
4150 ret = PTR_ERR(pages);
4151 goto out_req;
4152 }
Alex Elder1ceae7e2013-02-06 13:11:38 -06004153
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004154 osd_req_op_extent_init(req, 0, CEPH_OSD_OP_READ, 0, buf_len, 0, 0);
4155 osd_req_op_extent_osd_data_pages(req, 0, pages, buf_len, 0, false,
4156 true);
Alex Elder788e2df2013-01-17 12:25:27 -06004157
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004158 ceph_osdc_start_request(osdc, req, false);
4159 ret = ceph_osdc_wait_request(osdc, req);
4160 if (ret >= 0)
4161 ceph_copy_from_page_vector(pages, buf, 0, ret);
4162
4163out_req:
4164 ceph_osdc_put_request(req);
Alex Elder788e2df2013-01-17 12:25:27 -06004165 return ret;
4166}
4167
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004168/*
Alex Elder662518b2013-05-06 09:51:29 -05004169 * Read the complete header for the given rbd device. On successful
4170 * return, the rbd_dev->header field will contain up-to-date
4171 * information about the image.
Alex Elder4156d992012-08-02 11:29:46 -05004172 */
Alex Elder99a41eb2013-05-06 09:51:30 -05004173static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev)
Alex Elder4156d992012-08-02 11:29:46 -05004174{
4175 struct rbd_image_header_ondisk *ondisk = NULL;
4176 u32 snap_count = 0;
4177 u64 names_size = 0;
4178 u32 want_count;
4179 int ret;
4180
4181 /*
4182 * The complete header will include an array of its 64-bit
4183 * snapshot ids, followed by the names of those snapshots as
4184 * a contiguous block of NUL-terminated strings. Note that
4185 * the number of snapshots could change by the time we read
4186 * it in, in which case we re-read it.
4187 */
4188 do {
4189 size_t size;
4190
4191 kfree(ondisk);
4192
4193 size = sizeof (*ondisk);
4194 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
4195 size += names_size;
4196 ondisk = kmalloc(size, GFP_KERNEL);
4197 if (!ondisk)
Alex Elder662518b2013-05-06 09:51:29 -05004198 return -ENOMEM;
Alex Elder4156d992012-08-02 11:29:46 -05004199
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004200 ret = rbd_obj_read_sync(rbd_dev, &rbd_dev->header_oid,
4201 &rbd_dev->header_oloc, ondisk, size);
Alex Elder4156d992012-08-02 11:29:46 -05004202 if (ret < 0)
Alex Elder662518b2013-05-06 09:51:29 -05004203 goto out;
Alex Elderc0cd10db2013-04-26 09:43:47 -05004204 if ((size_t)ret < size) {
Alex Elder4156d992012-08-02 11:29:46 -05004205 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05004206 rbd_warn(rbd_dev, "short header read (want %zd got %d)",
4207 size, ret);
Alex Elder662518b2013-05-06 09:51:29 -05004208 goto out;
Alex Elder4156d992012-08-02 11:29:46 -05004209 }
4210 if (!rbd_dev_ondisk_valid(ondisk)) {
4211 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05004212 rbd_warn(rbd_dev, "invalid header");
Alex Elder662518b2013-05-06 09:51:29 -05004213 goto out;
Alex Elder4156d992012-08-02 11:29:46 -05004214 }
4215
4216 names_size = le64_to_cpu(ondisk->snap_names_len);
4217 want_count = snap_count;
4218 snap_count = le32_to_cpu(ondisk->snap_count);
4219 } while (snap_count != want_count);
4220
Alex Elder662518b2013-05-06 09:51:29 -05004221 ret = rbd_header_from_disk(rbd_dev, ondisk);
4222out:
Alex Elder4156d992012-08-02 11:29:46 -05004223 kfree(ondisk);
4224
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004225 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004226}
4227
Alex Elder15228ed2013-05-01 12:43:03 -05004228/*
4229 * Clear the rbd device's EXISTS flag if the snapshot it's mapped to
4230 * has disappeared from the (just updated) snapshot context.
4231 */
4232static void rbd_exists_validate(struct rbd_device *rbd_dev)
4233{
4234 u64 snap_id;
4235
4236 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags))
4237 return;
4238
4239 snap_id = rbd_dev->spec->snap_id;
4240 if (snap_id == CEPH_NOSNAP)
4241 return;
4242
4243 if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX)
4244 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
4245}
4246
Josh Durgin98752012013-08-29 17:26:31 -07004247static void rbd_dev_update_size(struct rbd_device *rbd_dev)
4248{
4249 sector_t size;
Josh Durgin98752012013-08-29 17:26:31 -07004250
4251 /*
Ilya Dryomov811c6682016-04-15 16:22:16 +02004252 * If EXISTS is not set, rbd_dev->disk may be NULL, so don't
4253 * try to update its size. If REMOVING is set, updating size
4254 * is just useless work since the device can't be opened.
Josh Durgin98752012013-08-29 17:26:31 -07004255 */
Ilya Dryomov811c6682016-04-15 16:22:16 +02004256 if (test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags) &&
4257 !test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) {
Josh Durgin98752012013-08-29 17:26:31 -07004258 size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE;
4259 dout("setting size to %llu sectors", (unsigned long long)size);
4260 set_capacity(rbd_dev->disk, size);
4261 revalidate_disk(rbd_dev->disk);
4262 }
4263}
4264
Alex Eldercc4a38bd2013-04-30 00:44:33 -05004265static int rbd_dev_refresh(struct rbd_device *rbd_dev)
Alex Elder1fe5e992012-07-25 09:32:41 -05004266{
Alex Eldere627db02013-05-06 07:40:30 -05004267 u64 mapping_size;
Alex Elder1fe5e992012-07-25 09:32:41 -05004268 int ret;
4269
Alex Eldercfbf6372013-05-31 17:40:45 -05004270 down_write(&rbd_dev->header_rwsem);
Alex Elder3b5cf2a2013-05-29 11:18:59 -05004271 mapping_size = rbd_dev->mapping.size;
Ilya Dryomova720ae02014-07-23 17:11:19 +04004272
4273 ret = rbd_dev_header_info(rbd_dev);
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04004274 if (ret)
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004275 goto out;
Alex Elder15228ed2013-05-01 12:43:03 -05004276
Ilya Dryomove8f59b52014-07-24 10:42:13 +04004277 /*
4278 * If there is a parent, see if it has disappeared due to the
4279 * mapped image getting flattened.
4280 */
4281 if (rbd_dev->parent) {
4282 ret = rbd_dev_v2_parent_info(rbd_dev);
4283 if (ret)
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004284 goto out;
Ilya Dryomove8f59b52014-07-24 10:42:13 +04004285 }
4286
Ilya Dryomov5ff11082014-07-23 17:11:21 +04004287 if (rbd_dev->spec->snap_id == CEPH_NOSNAP) {
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004288 rbd_dev->mapping.size = rbd_dev->header.image_size;
Ilya Dryomov5ff11082014-07-23 17:11:21 +04004289 } else {
4290 /* validate mapped snapshot's EXISTS flag */
4291 rbd_exists_validate(rbd_dev);
4292 }
Alex Elder15228ed2013-05-01 12:43:03 -05004293
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004294out:
Alex Eldercfbf6372013-05-31 17:40:45 -05004295 up_write(&rbd_dev->header_rwsem);
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004296 if (!ret && mapping_size != rbd_dev->mapping.size)
Josh Durgin98752012013-08-29 17:26:31 -07004297 rbd_dev_update_size(rbd_dev);
Alex Elder1fe5e992012-07-25 09:32:41 -05004298
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004299 return ret;
Alex Elder1fe5e992012-07-25 09:32:41 -05004300}
4301
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004302static int rbd_init_request(void *data, struct request *rq,
4303 unsigned int hctx_idx, unsigned int request_idx,
4304 unsigned int numa_node)
4305{
4306 struct work_struct *work = blk_mq_rq_to_pdu(rq);
4307
4308 INIT_WORK(work, rbd_queue_workfn);
4309 return 0;
4310}
4311
4312static struct blk_mq_ops rbd_mq_ops = {
4313 .queue_rq = rbd_queue_rq,
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004314 .init_request = rbd_init_request,
4315};
4316
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004317static int rbd_init_disk(struct rbd_device *rbd_dev)
4318{
4319 struct gendisk *disk;
4320 struct request_queue *q;
Alex Elder593a9e72012-02-07 12:03:37 -06004321 u64 segment_size;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004322 int err;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004323
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004324 /* create gendisk info */
Ilya Dryomov7e513d42013-12-16 19:26:32 +02004325 disk = alloc_disk(single_major ?
4326 (1 << RBD_SINGLE_MAJOR_PART_SHIFT) :
4327 RBD_MINORS_PER_MAJOR);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004328 if (!disk)
Alex Elder1fcdb8a2012-08-29 17:11:06 -05004329 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004330
Alex Elderf0f8cef2012-01-29 13:57:44 -06004331 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
Alex Elderde71a292012-07-03 16:01:19 -05004332 rbd_dev->dev_id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004333 disk->major = rbd_dev->major;
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004334 disk->first_minor = rbd_dev->minor;
Ilya Dryomov7e513d42013-12-16 19:26:32 +02004335 if (single_major)
4336 disk->flags |= GENHD_FL_EXT_DEVT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004337 disk->fops = &rbd_bd_ops;
4338 disk->private_data = rbd_dev;
4339
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004340 memset(&rbd_dev->tag_set, 0, sizeof(rbd_dev->tag_set));
4341 rbd_dev->tag_set.ops = &rbd_mq_ops;
Ilya Dryomovb5584182015-06-23 16:21:19 +03004342 rbd_dev->tag_set.queue_depth = rbd_dev->opts->queue_depth;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004343 rbd_dev->tag_set.numa_node = NUMA_NO_NODE;
Ilya Dryomovb5584182015-06-23 16:21:19 +03004344 rbd_dev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004345 rbd_dev->tag_set.nr_hw_queues = 1;
4346 rbd_dev->tag_set.cmd_size = sizeof(struct work_struct);
4347
4348 err = blk_mq_alloc_tag_set(&rbd_dev->tag_set);
4349 if (err)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004350 goto out_disk;
Josh Durgin029bcbd2011-07-22 11:35:23 -07004351
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004352 q = blk_mq_init_queue(&rbd_dev->tag_set);
4353 if (IS_ERR(q)) {
4354 err = PTR_ERR(q);
4355 goto out_tag_set;
4356 }
4357
Ilya Dryomovd8a2c892015-03-24 16:15:17 +03004358 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
4359 /* QUEUE_FLAG_ADD_RANDOM is off by default for blk-mq */
Alex Elder593a9e72012-02-07 12:03:37 -06004360
Josh Durgin029bcbd2011-07-22 11:35:23 -07004361 /* set io sizes to object size */
Alex Elder593a9e72012-02-07 12:03:37 -06004362 segment_size = rbd_obj_bytes(&rbd_dev->header);
4363 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
Ilya Dryomov0d9fde42015-10-07 16:09:35 +02004364 q->limits.max_sectors = queue_max_hw_sectors(q);
Ilya Dryomovd3834fe2015-06-12 19:19:02 +03004365 blk_queue_max_segments(q, segment_size / SECTOR_SIZE);
Alex Elder593a9e72012-02-07 12:03:37 -06004366 blk_queue_max_segment_size(q, segment_size);
4367 blk_queue_io_min(q, segment_size);
4368 blk_queue_io_opt(q, segment_size);
Josh Durgin029bcbd2011-07-22 11:35:23 -07004369
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004370 /* enable the discard support */
4371 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
4372 q->limits.discard_granularity = segment_size;
4373 q->limits.discard_alignment = segment_size;
Jens Axboe2bb4cd52015-07-14 08:15:12 -06004374 blk_queue_max_discard_sectors(q, segment_size / SECTOR_SIZE);
Josh Durginb76f8232014-04-07 16:52:03 -07004375 q->limits.discard_zeroes_data = 1;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004376
Ronny Hegewaldbae818e2015-10-15 18:50:46 +00004377 if (!ceph_test_opt(rbd_dev->rbd_client->client, NOCRC))
Jan Karadc3b17c2017-02-02 15:56:50 +01004378 q->backing_dev_info->capabilities |= BDI_CAP_STABLE_WRITES;
Ronny Hegewaldbae818e2015-10-15 18:50:46 +00004379
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004380 disk->queue = q;
4381
4382 q->queuedata = rbd_dev;
4383
4384 rbd_dev->disk = disk;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004385
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004386 return 0;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004387out_tag_set:
4388 blk_mq_free_tag_set(&rbd_dev->tag_set);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004389out_disk:
4390 put_disk(disk);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004391 return err;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004392}
4393
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004394/*
4395 sysfs
4396*/
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004397
Alex Elder593a9e72012-02-07 12:03:37 -06004398static struct rbd_device *dev_to_rbd_dev(struct device *dev)
4399{
4400 return container_of(dev, struct rbd_device, dev);
4401}
4402
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004403static ssize_t rbd_size_show(struct device *dev,
4404 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004405{
Alex Elder593a9e72012-02-07 12:03:37 -06004406 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004407
Alex Elderfc71d832013-04-26 15:44:36 -05004408 return sprintf(buf, "%llu\n",
4409 (unsigned long long)rbd_dev->mapping.size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004410}
4411
Alex Elder34b13182012-07-13 20:35:12 -05004412/*
4413 * Note this shows the features for whatever's mapped, which is not
4414 * necessarily the base image.
4415 */
4416static ssize_t rbd_features_show(struct device *dev,
4417 struct device_attribute *attr, char *buf)
4418{
4419 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4420
4421 return sprintf(buf, "0x%016llx\n",
Alex Elderfc71d832013-04-26 15:44:36 -05004422 (unsigned long long)rbd_dev->mapping.features);
Alex Elder34b13182012-07-13 20:35:12 -05004423}
4424
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004425static ssize_t rbd_major_show(struct device *dev,
4426 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004427{
Alex Elder593a9e72012-02-07 12:03:37 -06004428 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004429
Alex Elderfc71d832013-04-26 15:44:36 -05004430 if (rbd_dev->major)
4431 return sprintf(buf, "%d\n", rbd_dev->major);
4432
4433 return sprintf(buf, "(none)\n");
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004434}
Alex Elderfc71d832013-04-26 15:44:36 -05004435
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004436static ssize_t rbd_minor_show(struct device *dev,
4437 struct device_attribute *attr, char *buf)
4438{
4439 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4440
4441 return sprintf(buf, "%d\n", rbd_dev->minor);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004442}
4443
Ilya Dryomov005a07bf2016-08-18 18:38:43 +02004444static ssize_t rbd_client_addr_show(struct device *dev,
4445 struct device_attribute *attr, char *buf)
4446{
4447 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4448 struct ceph_entity_addr *client_addr =
4449 ceph_client_addr(rbd_dev->rbd_client->client);
4450
4451 return sprintf(buf, "%pISpc/%u\n", &client_addr->in_addr,
4452 le32_to_cpu(client_addr->nonce));
4453}
4454
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004455static ssize_t rbd_client_id_show(struct device *dev,
4456 struct device_attribute *attr, char *buf)
4457{
Alex Elder593a9e72012-02-07 12:03:37 -06004458 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004459
Alex Elder1dbb4392012-01-24 10:08:37 -06004460 return sprintf(buf, "client%lld\n",
Ilya Dryomov033268a2016-08-12 14:59:58 +02004461 ceph_client_gid(rbd_dev->rbd_client->client));
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004462}
4463
Mike Christie267fb902016-08-18 18:38:43 +02004464static ssize_t rbd_cluster_fsid_show(struct device *dev,
4465 struct device_attribute *attr, char *buf)
4466{
4467 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4468
4469 return sprintf(buf, "%pU\n", &rbd_dev->rbd_client->client->fsid);
4470}
4471
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02004472static ssize_t rbd_config_info_show(struct device *dev,
4473 struct device_attribute *attr, char *buf)
4474{
4475 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4476
4477 return sprintf(buf, "%s\n", rbd_dev->config_info);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004478}
4479
4480static ssize_t rbd_pool_show(struct device *dev,
4481 struct device_attribute *attr, char *buf)
4482{
Alex Elder593a9e72012-02-07 12:03:37 -06004483 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004484
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004485 return sprintf(buf, "%s\n", rbd_dev->spec->pool_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004486}
4487
Alex Elder9bb2f332012-07-12 10:46:35 -05004488static ssize_t rbd_pool_id_show(struct device *dev,
4489 struct device_attribute *attr, char *buf)
4490{
4491 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4492
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004493 return sprintf(buf, "%llu\n",
Alex Elderfc71d832013-04-26 15:44:36 -05004494 (unsigned long long) rbd_dev->spec->pool_id);
Alex Elder9bb2f332012-07-12 10:46:35 -05004495}
4496
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004497static ssize_t rbd_name_show(struct device *dev,
4498 struct device_attribute *attr, char *buf)
4499{
Alex Elder593a9e72012-02-07 12:03:37 -06004500 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004501
Alex Eldera92ffdf2012-10-30 19:40:33 -05004502 if (rbd_dev->spec->image_name)
4503 return sprintf(buf, "%s\n", rbd_dev->spec->image_name);
4504
4505 return sprintf(buf, "(unknown)\n");
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004506}
4507
Alex Elder589d30e2012-07-10 20:30:11 -05004508static ssize_t rbd_image_id_show(struct device *dev,
4509 struct device_attribute *attr, char *buf)
4510{
4511 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4512
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004513 return sprintf(buf, "%s\n", rbd_dev->spec->image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05004514}
4515
Alex Elder34b13182012-07-13 20:35:12 -05004516/*
4517 * Shows the name of the currently-mapped snapshot (or
4518 * RBD_SNAP_HEAD_NAME for the base image).
4519 */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004520static ssize_t rbd_snap_show(struct device *dev,
4521 struct device_attribute *attr,
4522 char *buf)
4523{
Alex Elder593a9e72012-02-07 12:03:37 -06004524 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004525
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004526 return sprintf(buf, "%s\n", rbd_dev->spec->snap_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004527}
4528
Mike Christie92a58672016-08-18 18:38:44 +02004529static ssize_t rbd_snap_id_show(struct device *dev,
4530 struct device_attribute *attr, char *buf)
4531{
4532 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4533
4534 return sprintf(buf, "%llu\n", rbd_dev->spec->snap_id);
4535}
4536
Alex Elder86b00e02012-10-25 23:34:42 -05004537/*
Ilya Dryomovff961282014-07-22 21:53:07 +04004538 * For a v2 image, shows the chain of parent images, separated by empty
4539 * lines. For v1 images or if there is no parent, shows "(no parent
4540 * image)".
Alex Elder86b00e02012-10-25 23:34:42 -05004541 */
4542static ssize_t rbd_parent_show(struct device *dev,
Ilya Dryomovff961282014-07-22 21:53:07 +04004543 struct device_attribute *attr,
4544 char *buf)
Alex Elder86b00e02012-10-25 23:34:42 -05004545{
4546 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Ilya Dryomovff961282014-07-22 21:53:07 +04004547 ssize_t count = 0;
Alex Elder86b00e02012-10-25 23:34:42 -05004548
Ilya Dryomovff961282014-07-22 21:53:07 +04004549 if (!rbd_dev->parent)
Alex Elder86b00e02012-10-25 23:34:42 -05004550 return sprintf(buf, "(no parent image)\n");
4551
Ilya Dryomovff961282014-07-22 21:53:07 +04004552 for ( ; rbd_dev->parent; rbd_dev = rbd_dev->parent) {
4553 struct rbd_spec *spec = rbd_dev->parent_spec;
Alex Elder86b00e02012-10-25 23:34:42 -05004554
Ilya Dryomovff961282014-07-22 21:53:07 +04004555 count += sprintf(&buf[count], "%s"
4556 "pool_id %llu\npool_name %s\n"
4557 "image_id %s\nimage_name %s\n"
4558 "snap_id %llu\nsnap_name %s\n"
4559 "overlap %llu\n",
4560 !count ? "" : "\n", /* first? */
4561 spec->pool_id, spec->pool_name,
4562 spec->image_id, spec->image_name ?: "(unknown)",
4563 spec->snap_id, spec->snap_name,
4564 rbd_dev->parent_overlap);
4565 }
Alex Elder86b00e02012-10-25 23:34:42 -05004566
Ilya Dryomovff961282014-07-22 21:53:07 +04004567 return count;
Alex Elder86b00e02012-10-25 23:34:42 -05004568}
4569
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004570static ssize_t rbd_image_refresh(struct device *dev,
4571 struct device_attribute *attr,
4572 const char *buf,
4573 size_t size)
4574{
Alex Elder593a9e72012-02-07 12:03:37 -06004575 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Alex Elderb8136232012-07-25 09:32:41 -05004576 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004577
Alex Eldercc4a38bd2013-04-30 00:44:33 -05004578 ret = rbd_dev_refresh(rbd_dev);
Alex Eldere627db02013-05-06 07:40:30 -05004579 if (ret)
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04004580 return ret;
Alex Elderb8136232012-07-25 09:32:41 -05004581
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04004582 return size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004583}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004584
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004585static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
Alex Elder34b13182012-07-13 20:35:12 -05004586static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004587static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004588static DEVICE_ATTR(minor, S_IRUGO, rbd_minor_show, NULL);
Ilya Dryomov005a07bf2016-08-18 18:38:43 +02004589static DEVICE_ATTR(client_addr, S_IRUGO, rbd_client_addr_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004590static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
Mike Christie267fb902016-08-18 18:38:43 +02004591static DEVICE_ATTR(cluster_fsid, S_IRUGO, rbd_cluster_fsid_show, NULL);
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02004592static DEVICE_ATTR(config_info, S_IRUSR, rbd_config_info_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004593static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
Alex Elder9bb2f332012-07-12 10:46:35 -05004594static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004595static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
Alex Elder589d30e2012-07-10 20:30:11 -05004596static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004597static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
4598static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
Mike Christie92a58672016-08-18 18:38:44 +02004599static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL);
Alex Elder86b00e02012-10-25 23:34:42 -05004600static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004601
4602static struct attribute *rbd_attrs[] = {
4603 &dev_attr_size.attr,
Alex Elder34b13182012-07-13 20:35:12 -05004604 &dev_attr_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004605 &dev_attr_major.attr,
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004606 &dev_attr_minor.attr,
Ilya Dryomov005a07bf2016-08-18 18:38:43 +02004607 &dev_attr_client_addr.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004608 &dev_attr_client_id.attr,
Mike Christie267fb902016-08-18 18:38:43 +02004609 &dev_attr_cluster_fsid.attr,
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02004610 &dev_attr_config_info.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004611 &dev_attr_pool.attr,
Alex Elder9bb2f332012-07-12 10:46:35 -05004612 &dev_attr_pool_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004613 &dev_attr_name.attr,
Alex Elder589d30e2012-07-10 20:30:11 -05004614 &dev_attr_image_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004615 &dev_attr_current_snap.attr,
Mike Christie92a58672016-08-18 18:38:44 +02004616 &dev_attr_snap_id.attr,
Alex Elder86b00e02012-10-25 23:34:42 -05004617 &dev_attr_parent.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004618 &dev_attr_refresh.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004619 NULL
4620};
4621
4622static struct attribute_group rbd_attr_group = {
4623 .attrs = rbd_attrs,
4624};
4625
4626static const struct attribute_group *rbd_attr_groups[] = {
4627 &rbd_attr_group,
4628 NULL
4629};
4630
Ilya Dryomov6cac4692015-10-16 20:11:25 +02004631static void rbd_dev_release(struct device *dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004632
Bhumika Goyalb9942bc2017-02-11 12:14:38 +05304633static const struct device_type rbd_device_type = {
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004634 .name = "rbd",
4635 .groups = rbd_attr_groups,
Ilya Dryomov6cac4692015-10-16 20:11:25 +02004636 .release = rbd_dev_release,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004637};
4638
Alex Elder8b8fb992012-10-26 17:25:24 -05004639static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)
4640{
4641 kref_get(&spec->kref);
4642
4643 return spec;
4644}
4645
4646static void rbd_spec_free(struct kref *kref);
4647static void rbd_spec_put(struct rbd_spec *spec)
4648{
4649 if (spec)
4650 kref_put(&spec->kref, rbd_spec_free);
4651}
4652
4653static struct rbd_spec *rbd_spec_alloc(void)
4654{
4655 struct rbd_spec *spec;
4656
4657 spec = kzalloc(sizeof (*spec), GFP_KERNEL);
4658 if (!spec)
4659 return NULL;
Ilya Dryomov04077592014-07-23 17:11:20 +04004660
4661 spec->pool_id = CEPH_NOPOOL;
4662 spec->snap_id = CEPH_NOSNAP;
Alex Elder8b8fb992012-10-26 17:25:24 -05004663 kref_init(&spec->kref);
4664
Alex Elder8b8fb992012-10-26 17:25:24 -05004665 return spec;
4666}
4667
4668static void rbd_spec_free(struct kref *kref)
4669{
4670 struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref);
4671
4672 kfree(spec->pool_name);
4673 kfree(spec->image_id);
4674 kfree(spec->image_name);
4675 kfree(spec->snap_name);
4676 kfree(spec);
4677}
4678
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004679static void rbd_dev_free(struct rbd_device *rbd_dev)
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004680{
Ilya Dryomov99d16942016-08-12 16:11:41 +02004681 WARN_ON(rbd_dev->watch_state != RBD_WATCH_STATE_UNREGISTERED);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004682 WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_UNLOCKED);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004683
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004684 ceph_oid_destroy(&rbd_dev->header_oid);
Ilya Dryomov6b6dddb2016-08-05 16:15:38 +02004685 ceph_oloc_destroy(&rbd_dev->header_oloc);
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02004686 kfree(rbd_dev->config_info);
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004687
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004688 rbd_put_client(rbd_dev->rbd_client);
4689 rbd_spec_put(rbd_dev->spec);
4690 kfree(rbd_dev->opts);
4691 kfree(rbd_dev);
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004692}
4693
4694static void rbd_dev_release(struct device *dev)
4695{
4696 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4697 bool need_put = !!rbd_dev->opts;
4698
4699 if (need_put) {
4700 destroy_workqueue(rbd_dev->task_wq);
4701 ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id);
4702 }
4703
4704 rbd_dev_free(rbd_dev);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004705
4706 /*
4707 * This is racy, but way better than putting module outside of
4708 * the release callback. The race window is pretty small, so
4709 * doing something similar to dm (dm-builtin.c) is overkill.
4710 */
4711 if (need_put)
4712 module_put(THIS_MODULE);
4713}
4714
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004715static struct rbd_device *__rbd_dev_create(struct rbd_client *rbdc,
4716 struct rbd_spec *spec)
Alex Elderc53d5892012-10-25 23:34:42 -05004717{
4718 struct rbd_device *rbd_dev;
4719
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004720 rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL);
Alex Elderc53d5892012-10-25 23:34:42 -05004721 if (!rbd_dev)
4722 return NULL;
4723
4724 spin_lock_init(&rbd_dev->lock);
4725 INIT_LIST_HEAD(&rbd_dev->node);
Alex Elderc53d5892012-10-25 23:34:42 -05004726 init_rwsem(&rbd_dev->header_rwsem);
4727
Ilya Dryomov7e973322017-01-25 18:16:22 +01004728 rbd_dev->header.data_pool_id = CEPH_NOPOOL;
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004729 ceph_oid_init(&rbd_dev->header_oid);
Ilya Dryomov431a02c2017-01-25 18:16:21 +01004730 rbd_dev->header_oloc.pool = spec->pool_id;
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004731
Ilya Dryomov99d16942016-08-12 16:11:41 +02004732 mutex_init(&rbd_dev->watch_mutex);
4733 rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED;
4734 INIT_DELAYED_WORK(&rbd_dev->watch_dwork, rbd_reregister_watch);
4735
Ilya Dryomoved95b212016-08-12 16:40:02 +02004736 init_rwsem(&rbd_dev->lock_rwsem);
4737 rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED;
4738 INIT_WORK(&rbd_dev->acquired_lock_work, rbd_notify_acquired_lock);
4739 INIT_WORK(&rbd_dev->released_lock_work, rbd_notify_released_lock);
4740 INIT_DELAYED_WORK(&rbd_dev->lock_dwork, rbd_acquire_lock);
4741 INIT_WORK(&rbd_dev->unlock_work, rbd_release_lock_work);
4742 init_waitqueue_head(&rbd_dev->lock_waitq);
4743
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004744 rbd_dev->dev.bus = &rbd_bus_type;
4745 rbd_dev->dev.type = &rbd_device_type;
4746 rbd_dev->dev.parent = &rbd_root_dev;
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004747 device_initialize(&rbd_dev->dev);
4748
Alex Elderc53d5892012-10-25 23:34:42 -05004749 rbd_dev->rbd_client = rbdc;
Ilya Dryomovd1475432015-06-22 13:24:48 +03004750 rbd_dev->spec = spec;
Alex Elder0903e872012-11-14 12:25:19 -06004751
Alex Elderc53d5892012-10-25 23:34:42 -05004752 return rbd_dev;
4753}
4754
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004755/*
4756 * Create a mapping rbd_dev.
4757 */
4758static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,
4759 struct rbd_spec *spec,
4760 struct rbd_options *opts)
4761{
4762 struct rbd_device *rbd_dev;
4763
4764 rbd_dev = __rbd_dev_create(rbdc, spec);
4765 if (!rbd_dev)
4766 return NULL;
4767
4768 rbd_dev->opts = opts;
4769
4770 /* get an id and fill in device name */
4771 rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0,
4772 minor_to_rbd_dev_id(1 << MINORBITS),
4773 GFP_KERNEL);
4774 if (rbd_dev->dev_id < 0)
4775 goto fail_rbd_dev;
4776
4777 sprintf(rbd_dev->name, RBD_DRV_NAME "%d", rbd_dev->dev_id);
4778 rbd_dev->task_wq = alloc_ordered_workqueue("%s-tasks", WQ_MEM_RECLAIM,
4779 rbd_dev->name);
4780 if (!rbd_dev->task_wq)
4781 goto fail_dev_id;
4782
4783 /* we have a ref from do_rbd_add() */
4784 __module_get(THIS_MODULE);
4785
4786 dout("%s rbd_dev %p dev_id %d\n", __func__, rbd_dev, rbd_dev->dev_id);
4787 return rbd_dev;
4788
4789fail_dev_id:
4790 ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id);
4791fail_rbd_dev:
4792 rbd_dev_free(rbd_dev);
4793 return NULL;
4794}
4795
Alex Elderc53d5892012-10-25 23:34:42 -05004796static void rbd_dev_destroy(struct rbd_device *rbd_dev)
4797{
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004798 if (rbd_dev)
4799 put_device(&rbd_dev->dev);
Alex Elderc53d5892012-10-25 23:34:42 -05004800}
4801
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004802/*
Alex Elder9d475de2012-07-03 16:01:19 -05004803 * Get the size and object order for an image snapshot, or if
4804 * snap_id is CEPH_NOSNAP, gets this information for the base
4805 * image.
4806 */
4807static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
4808 u8 *order, u64 *snap_size)
4809{
4810 __le64 snapid = cpu_to_le64(snap_id);
4811 int ret;
4812 struct {
4813 u8 order;
4814 __le64 size;
4815 } __attribute__ ((packed)) size_buf = { 0 };
4816
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004817 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
4818 &rbd_dev->header_oloc, "get_size",
4819 &snapid, sizeof(snapid),
4820 &size_buf, sizeof(size_buf));
Alex Elder36be9a72013-01-19 00:30:28 -06004821 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder9d475de2012-07-03 16:01:19 -05004822 if (ret < 0)
4823 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05004824 if (ret < sizeof (size_buf))
4825 return -ERANGE;
Alex Elder9d475de2012-07-03 16:01:19 -05004826
Josh Durginc3545572013-08-28 17:08:10 -07004827 if (order) {
Alex Elderc86f86e2013-04-25 15:09:41 -05004828 *order = size_buf.order;
Josh Durginc3545572013-08-28 17:08:10 -07004829 dout(" order %u", (unsigned int)*order);
4830 }
Alex Elder9d475de2012-07-03 16:01:19 -05004831 *snap_size = le64_to_cpu(size_buf.size);
4832
Josh Durginc3545572013-08-28 17:08:10 -07004833 dout(" snap_id 0x%016llx snap_size = %llu\n",
4834 (unsigned long long)snap_id,
Alex Elder57385b52013-04-21 12:14:45 -05004835 (unsigned long long)*snap_size);
Alex Elder9d475de2012-07-03 16:01:19 -05004836
4837 return 0;
4838}
4839
4840static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
4841{
4842 return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
4843 &rbd_dev->header.obj_order,
4844 &rbd_dev->header.image_size);
4845}
4846
Alex Elder1e130192012-07-03 16:01:19 -05004847static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
4848{
4849 void *reply_buf;
4850 int ret;
4851 void *p;
4852
4853 reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL);
4854 if (!reply_buf)
4855 return -ENOMEM;
4856
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004857 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
4858 &rbd_dev->header_oloc, "get_object_prefix",
4859 NULL, 0, reply_buf, RBD_OBJ_PREFIX_LEN_MAX);
Alex Elder36be9a72013-01-19 00:30:28 -06004860 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder1e130192012-07-03 16:01:19 -05004861 if (ret < 0)
4862 goto out;
4863
4864 p = reply_buf;
4865 rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p,
Alex Elder57385b52013-04-21 12:14:45 -05004866 p + ret, NULL, GFP_NOIO);
4867 ret = 0;
Alex Elder1e130192012-07-03 16:01:19 -05004868
4869 if (IS_ERR(rbd_dev->header.object_prefix)) {
4870 ret = PTR_ERR(rbd_dev->header.object_prefix);
4871 rbd_dev->header.object_prefix = NULL;
4872 } else {
4873 dout(" object_prefix = %s\n", rbd_dev->header.object_prefix);
4874 }
Alex Elder1e130192012-07-03 16:01:19 -05004875out:
4876 kfree(reply_buf);
4877
4878 return ret;
4879}
4880
Alex Elderb1b54022012-07-03 16:01:19 -05004881static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
4882 u64 *snap_features)
4883{
4884 __le64 snapid = cpu_to_le64(snap_id);
4885 struct {
4886 __le64 features;
4887 __le64 incompat;
Alex Elder41579762013-04-21 12:14:45 -05004888 } __attribute__ ((packed)) features_buf = { 0 };
Ilya Dryomovd3767f02016-04-13 14:15:50 +02004889 u64 unsup;
Alex Elderb1b54022012-07-03 16:01:19 -05004890 int ret;
4891
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004892 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
4893 &rbd_dev->header_oloc, "get_features",
4894 &snapid, sizeof(snapid),
4895 &features_buf, sizeof(features_buf));
Alex Elder36be9a72013-01-19 00:30:28 -06004896 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderb1b54022012-07-03 16:01:19 -05004897 if (ret < 0)
4898 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05004899 if (ret < sizeof (features_buf))
4900 return -ERANGE;
Alex Elderd8891402012-10-09 13:50:17 -07004901
Ilya Dryomovd3767f02016-04-13 14:15:50 +02004902 unsup = le64_to_cpu(features_buf.incompat) & ~RBD_FEATURES_SUPPORTED;
4903 if (unsup) {
4904 rbd_warn(rbd_dev, "image uses unsupported features: 0x%llx",
4905 unsup);
Alex Elderb8f5c6e2012-11-01 08:39:26 -05004906 return -ENXIO;
Ilya Dryomovd3767f02016-04-13 14:15:50 +02004907 }
Alex Elderd8891402012-10-09 13:50:17 -07004908
Alex Elderb1b54022012-07-03 16:01:19 -05004909 *snap_features = le64_to_cpu(features_buf.features);
4910
4911 dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
Alex Elder57385b52013-04-21 12:14:45 -05004912 (unsigned long long)snap_id,
4913 (unsigned long long)*snap_features,
4914 (unsigned long long)le64_to_cpu(features_buf.incompat));
Alex Elderb1b54022012-07-03 16:01:19 -05004915
4916 return 0;
4917}
4918
4919static int rbd_dev_v2_features(struct rbd_device *rbd_dev)
4920{
4921 return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP,
4922 &rbd_dev->header.features);
4923}
4924
Alex Elder86b00e02012-10-25 23:34:42 -05004925static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev)
4926{
4927 struct rbd_spec *parent_spec;
4928 size_t size;
4929 void *reply_buf = NULL;
4930 __le64 snapid;
4931 void *p;
4932 void *end;
Alex Elder642a2532013-05-06 17:40:33 -05004933 u64 pool_id;
Alex Elder86b00e02012-10-25 23:34:42 -05004934 char *image_id;
Alex Elder3b5cf2a2013-05-29 11:18:59 -05004935 u64 snap_id;
Alex Elder86b00e02012-10-25 23:34:42 -05004936 u64 overlap;
Alex Elder86b00e02012-10-25 23:34:42 -05004937 int ret;
4938
4939 parent_spec = rbd_spec_alloc();
4940 if (!parent_spec)
4941 return -ENOMEM;
4942
4943 size = sizeof (__le64) + /* pool_id */
4944 sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */
4945 sizeof (__le64) + /* snap_id */
4946 sizeof (__le64); /* overlap */
4947 reply_buf = kmalloc(size, GFP_KERNEL);
4948 if (!reply_buf) {
4949 ret = -ENOMEM;
4950 goto out_err;
4951 }
4952
Ilya Dryomov4d9b67c2014-07-24 10:42:13 +04004953 snapid = cpu_to_le64(rbd_dev->spec->snap_id);
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004954 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
4955 &rbd_dev->header_oloc, "get_parent",
4956 &snapid, sizeof(snapid), reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06004957 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder86b00e02012-10-25 23:34:42 -05004958 if (ret < 0)
4959 goto out_err;
4960
Alex Elder86b00e02012-10-25 23:34:42 -05004961 p = reply_buf;
Alex Elder57385b52013-04-21 12:14:45 -05004962 end = reply_buf + ret;
4963 ret = -ERANGE;
Alex Elder642a2532013-05-06 17:40:33 -05004964 ceph_decode_64_safe(&p, end, pool_id, out_err);
Alex Elder392a9da2013-05-06 17:40:33 -05004965 if (pool_id == CEPH_NOPOOL) {
4966 /*
4967 * Either the parent never existed, or we have
4968 * record of it but the image got flattened so it no
4969 * longer has a parent. When the parent of a
4970 * layered image disappears we immediately set the
4971 * overlap to 0. The effect of this is that all new
4972 * requests will be treated as if the image had no
4973 * parent.
4974 */
4975 if (rbd_dev->parent_overlap) {
4976 rbd_dev->parent_overlap = 0;
Alex Elder392a9da2013-05-06 17:40:33 -05004977 rbd_dev_parent_put(rbd_dev);
4978 pr_info("%s: clone image has been flattened\n",
4979 rbd_dev->disk->disk_name);
4980 }
4981
Alex Elder86b00e02012-10-25 23:34:42 -05004982 goto out; /* No parent? No problem. */
Alex Elder392a9da2013-05-06 17:40:33 -05004983 }
Alex Elder86b00e02012-10-25 23:34:42 -05004984
Alex Elder0903e872012-11-14 12:25:19 -06004985 /* The ceph file layout needs to fit pool id in 32 bits */
4986
4987 ret = -EIO;
Alex Elder642a2532013-05-06 17:40:33 -05004988 if (pool_id > (u64)U32_MAX) {
Ilya Dryomov9584d502014-07-11 12:11:20 +04004989 rbd_warn(NULL, "parent pool id too large (%llu > %u)",
Alex Elder642a2532013-05-06 17:40:33 -05004990 (unsigned long long)pool_id, U32_MAX);
Alex Elder57385b52013-04-21 12:14:45 -05004991 goto out_err;
Alex Elderc0cd10db2013-04-26 09:43:47 -05004992 }
Alex Elder0903e872012-11-14 12:25:19 -06004993
Alex Elder979ed482012-11-01 08:39:26 -05004994 image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elder86b00e02012-10-25 23:34:42 -05004995 if (IS_ERR(image_id)) {
4996 ret = PTR_ERR(image_id);
4997 goto out_err;
4998 }
Alex Elder3b5cf2a2013-05-29 11:18:59 -05004999 ceph_decode_64_safe(&p, end, snap_id, out_err);
Alex Elder86b00e02012-10-25 23:34:42 -05005000 ceph_decode_64_safe(&p, end, overlap, out_err);
5001
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005002 /*
5003 * The parent won't change (except when the clone is
5004 * flattened, already handled that). So we only need to
5005 * record the parent spec we have not already done so.
5006 */
5007 if (!rbd_dev->parent_spec) {
5008 parent_spec->pool_id = pool_id;
5009 parent_spec->image_id = image_id;
5010 parent_spec->snap_id = snap_id;
Alex Elder70cf49c2013-05-06 17:40:33 -05005011 rbd_dev->parent_spec = parent_spec;
5012 parent_spec = NULL; /* rbd_dev now owns this */
Ilya Dryomovfbba11b2014-06-27 21:46:33 +04005013 } else {
5014 kfree(image_id);
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005015 }
5016
5017 /*
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005018 * We always update the parent overlap. If it's zero we issue
5019 * a warning, as we will proceed as if there was no parent.
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005020 */
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005021 if (!overlap) {
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005022 if (parent_spec) {
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005023 /* refresh, careful to warn just once */
5024 if (rbd_dev->parent_overlap)
5025 rbd_warn(rbd_dev,
5026 "clone now standalone (overlap became 0)");
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005027 } else {
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005028 /* initial probe */
5029 rbd_warn(rbd_dev, "clone is standalone (overlap 0)");
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005030 }
Alex Elder70cf49c2013-05-06 17:40:33 -05005031 }
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005032 rbd_dev->parent_overlap = overlap;
5033
Alex Elder86b00e02012-10-25 23:34:42 -05005034out:
5035 ret = 0;
5036out_err:
5037 kfree(reply_buf);
5038 rbd_spec_put(parent_spec);
5039
5040 return ret;
5041}
5042
Alex Eldercc070d52013-04-21 12:14:45 -05005043static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev)
5044{
5045 struct {
5046 __le64 stripe_unit;
5047 __le64 stripe_count;
5048 } __attribute__ ((packed)) striping_info_buf = { 0 };
5049 size_t size = sizeof (striping_info_buf);
5050 void *p;
5051 u64 obj_size;
5052 u64 stripe_unit;
5053 u64 stripe_count;
5054 int ret;
5055
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005056 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5057 &rbd_dev->header_oloc, "get_stripe_unit_count",
5058 NULL, 0, &striping_info_buf, size);
Alex Eldercc070d52013-04-21 12:14:45 -05005059 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
5060 if (ret < 0)
5061 return ret;
5062 if (ret < size)
5063 return -ERANGE;
5064
5065 /*
5066 * We don't actually support the "fancy striping" feature
5067 * (STRIPINGV2) yet, but if the striping sizes are the
5068 * defaults the behavior is the same as before. So find
5069 * out, and only fail if the image has non-default values.
5070 */
5071 ret = -EINVAL;
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +01005072 obj_size = rbd_obj_bytes(&rbd_dev->header);
Alex Eldercc070d52013-04-21 12:14:45 -05005073 p = &striping_info_buf;
5074 stripe_unit = ceph_decode_64(&p);
5075 if (stripe_unit != obj_size) {
5076 rbd_warn(rbd_dev, "unsupported stripe unit "
5077 "(got %llu want %llu)",
5078 stripe_unit, obj_size);
5079 return -EINVAL;
5080 }
5081 stripe_count = ceph_decode_64(&p);
5082 if (stripe_count != 1) {
5083 rbd_warn(rbd_dev, "unsupported stripe count "
5084 "(got %llu want 1)", stripe_count);
5085 return -EINVAL;
5086 }
Alex Elder500d0c02013-04-26 09:43:47 -05005087 rbd_dev->header.stripe_unit = stripe_unit;
5088 rbd_dev->header.stripe_count = stripe_count;
Alex Eldercc070d52013-04-21 12:14:45 -05005089
5090 return 0;
5091}
5092
Ilya Dryomov7e973322017-01-25 18:16:22 +01005093static int rbd_dev_v2_data_pool(struct rbd_device *rbd_dev)
5094{
5095 __le64 data_pool_id;
5096 int ret;
5097
5098 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5099 &rbd_dev->header_oloc, "get_data_pool",
5100 NULL, 0, &data_pool_id, sizeof(data_pool_id));
5101 if (ret < 0)
5102 return ret;
5103 if (ret < sizeof(data_pool_id))
5104 return -EBADMSG;
5105
5106 rbd_dev->header.data_pool_id = le64_to_cpu(data_pool_id);
5107 WARN_ON(rbd_dev->header.data_pool_id == CEPH_NOPOOL);
5108 return 0;
5109}
5110
Alex Elder9e15b772012-10-30 19:40:33 -05005111static char *rbd_dev_image_name(struct rbd_device *rbd_dev)
5112{
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005113 CEPH_DEFINE_OID_ONSTACK(oid);
Alex Elder9e15b772012-10-30 19:40:33 -05005114 size_t image_id_size;
5115 char *image_id;
5116 void *p;
5117 void *end;
5118 size_t size;
5119 void *reply_buf = NULL;
5120 size_t len = 0;
5121 char *image_name = NULL;
5122 int ret;
5123
5124 rbd_assert(!rbd_dev->spec->image_name);
5125
Alex Elder69e7a022012-11-01 08:39:26 -05005126 len = strlen(rbd_dev->spec->image_id);
5127 image_id_size = sizeof (__le32) + len;
Alex Elder9e15b772012-10-30 19:40:33 -05005128 image_id = kmalloc(image_id_size, GFP_KERNEL);
5129 if (!image_id)
5130 return NULL;
5131
5132 p = image_id;
Alex Elder41579762013-04-21 12:14:45 -05005133 end = image_id + image_id_size;
Alex Elder57385b52013-04-21 12:14:45 -05005134 ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len);
Alex Elder9e15b772012-10-30 19:40:33 -05005135
5136 size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX;
5137 reply_buf = kmalloc(size, GFP_KERNEL);
5138 if (!reply_buf)
5139 goto out;
5140
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005141 ceph_oid_printf(&oid, "%s", RBD_DIRECTORY);
5142 ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc,
5143 "dir_get_name", image_id, image_id_size,
5144 reply_buf, size);
Alex Elder9e15b772012-10-30 19:40:33 -05005145 if (ret < 0)
5146 goto out;
5147 p = reply_buf;
Alex Elderf40eb342013-04-25 15:09:42 -05005148 end = reply_buf + ret;
5149
Alex Elder9e15b772012-10-30 19:40:33 -05005150 image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL);
5151 if (IS_ERR(image_name))
5152 image_name = NULL;
5153 else
5154 dout("%s: name is %s len is %zd\n", __func__, image_name, len);
5155out:
5156 kfree(reply_buf);
5157 kfree(image_id);
5158
5159 return image_name;
5160}
5161
Alex Elder2ad3d712013-04-30 00:44:33 -05005162static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
5163{
5164 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
5165 const char *snap_name;
5166 u32 which = 0;
5167
5168 /* Skip over names until we find the one we are looking for */
5169
5170 snap_name = rbd_dev->header.snap_names;
5171 while (which < snapc->num_snaps) {
5172 if (!strcmp(name, snap_name))
5173 return snapc->snaps[which];
5174 snap_name += strlen(snap_name) + 1;
5175 which++;
5176 }
5177 return CEPH_NOSNAP;
5178}
5179
5180static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
5181{
5182 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
5183 u32 which;
5184 bool found = false;
5185 u64 snap_id;
5186
5187 for (which = 0; !found && which < snapc->num_snaps; which++) {
5188 const char *snap_name;
5189
5190 snap_id = snapc->snaps[which];
5191 snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id);
Josh Durginefadc982013-08-29 19:16:42 -07005192 if (IS_ERR(snap_name)) {
5193 /* ignore no-longer existing snapshots */
5194 if (PTR_ERR(snap_name) == -ENOENT)
5195 continue;
5196 else
5197 break;
5198 }
Alex Elder2ad3d712013-04-30 00:44:33 -05005199 found = !strcmp(name, snap_name);
5200 kfree(snap_name);
5201 }
5202 return found ? snap_id : CEPH_NOSNAP;
5203}
5204
5205/*
5206 * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if
5207 * no snapshot by that name is found, or if an error occurs.
5208 */
5209static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
5210{
5211 if (rbd_dev->image_format == 1)
5212 return rbd_v1_snap_id_by_name(rbd_dev, name);
5213
5214 return rbd_v2_snap_id_by_name(rbd_dev, name);
5215}
5216
Alex Elder9e15b772012-10-30 19:40:33 -05005217/*
Ilya Dryomov04077592014-07-23 17:11:20 +04005218 * An image being mapped will have everything but the snap id.
Alex Elder9e15b772012-10-30 19:40:33 -05005219 */
Ilya Dryomov04077592014-07-23 17:11:20 +04005220static int rbd_spec_fill_snap_id(struct rbd_device *rbd_dev)
5221{
5222 struct rbd_spec *spec = rbd_dev->spec;
5223
5224 rbd_assert(spec->pool_id != CEPH_NOPOOL && spec->pool_name);
5225 rbd_assert(spec->image_id && spec->image_name);
5226 rbd_assert(spec->snap_name);
5227
5228 if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) {
5229 u64 snap_id;
5230
5231 snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name);
5232 if (snap_id == CEPH_NOSNAP)
5233 return -ENOENT;
5234
5235 spec->snap_id = snap_id;
5236 } else {
5237 spec->snap_id = CEPH_NOSNAP;
5238 }
5239
5240 return 0;
5241}
5242
5243/*
5244 * A parent image will have all ids but none of the names.
5245 *
5246 * All names in an rbd spec are dynamically allocated. It's OK if we
5247 * can't figure out the name for an image id.
5248 */
5249static int rbd_spec_fill_names(struct rbd_device *rbd_dev)
Alex Elder9e15b772012-10-30 19:40:33 -05005250{
Alex Elder2e9f7f12013-04-26 09:43:48 -05005251 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
5252 struct rbd_spec *spec = rbd_dev->spec;
5253 const char *pool_name;
5254 const char *image_name;
5255 const char *snap_name;
Alex Elder9e15b772012-10-30 19:40:33 -05005256 int ret;
5257
Ilya Dryomov04077592014-07-23 17:11:20 +04005258 rbd_assert(spec->pool_id != CEPH_NOPOOL);
5259 rbd_assert(spec->image_id);
5260 rbd_assert(spec->snap_id != CEPH_NOSNAP);
Alex Elder9e15b772012-10-30 19:40:33 -05005261
Alex Elder2e9f7f12013-04-26 09:43:48 -05005262 /* Get the pool name; we have to make our own copy of this */
Alex Elder9e15b772012-10-30 19:40:33 -05005263
Alex Elder2e9f7f12013-04-26 09:43:48 -05005264 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id);
5265 if (!pool_name) {
5266 rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id);
Alex Elder935dc892012-11-01 10:17:15 -05005267 return -EIO;
5268 }
Alex Elder2e9f7f12013-04-26 09:43:48 -05005269 pool_name = kstrdup(pool_name, GFP_KERNEL);
5270 if (!pool_name)
Alex Elder9e15b772012-10-30 19:40:33 -05005271 return -ENOMEM;
5272
5273 /* Fetch the image name; tolerate failure here */
5274
Alex Elder2e9f7f12013-04-26 09:43:48 -05005275 image_name = rbd_dev_image_name(rbd_dev);
5276 if (!image_name)
Alex Elder06ecc6c2012-11-01 10:17:15 -05005277 rbd_warn(rbd_dev, "unable to get image name");
Alex Elder9e15b772012-10-30 19:40:33 -05005278
Ilya Dryomov04077592014-07-23 17:11:20 +04005279 /* Fetch the snapshot name */
Alex Elder9e15b772012-10-30 19:40:33 -05005280
Alex Elder2e9f7f12013-04-26 09:43:48 -05005281 snap_name = rbd_snap_name(rbd_dev, spec->snap_id);
Josh Durginda6a6b62013-09-04 17:57:31 -07005282 if (IS_ERR(snap_name)) {
5283 ret = PTR_ERR(snap_name);
Alex Elder9e15b772012-10-30 19:40:33 -05005284 goto out_err;
Alex Elder2e9f7f12013-04-26 09:43:48 -05005285 }
5286
5287 spec->pool_name = pool_name;
5288 spec->image_name = image_name;
5289 spec->snap_name = snap_name;
Alex Elder9e15b772012-10-30 19:40:33 -05005290
5291 return 0;
Ilya Dryomov04077592014-07-23 17:11:20 +04005292
Alex Elder9e15b772012-10-30 19:40:33 -05005293out_err:
Alex Elder2e9f7f12013-04-26 09:43:48 -05005294 kfree(image_name);
5295 kfree(pool_name);
Alex Elder9e15b772012-10-30 19:40:33 -05005296 return ret;
5297}
5298
Alex Eldercc4a38bd2013-04-30 00:44:33 -05005299static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev)
Alex Elder35d489f2012-07-03 16:01:19 -05005300{
5301 size_t size;
5302 int ret;
5303 void *reply_buf;
5304 void *p;
5305 void *end;
5306 u64 seq;
5307 u32 snap_count;
5308 struct ceph_snap_context *snapc;
5309 u32 i;
5310
5311 /*
5312 * We'll need room for the seq value (maximum snapshot id),
5313 * snapshot count, and array of that many snapshot ids.
5314 * For now we have a fixed upper limit on the number we're
5315 * prepared to receive.
5316 */
5317 size = sizeof (__le64) + sizeof (__le32) +
5318 RBD_MAX_SNAP_COUNT * sizeof (__le64);
5319 reply_buf = kzalloc(size, GFP_KERNEL);
5320 if (!reply_buf)
5321 return -ENOMEM;
5322
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005323 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5324 &rbd_dev->header_oloc, "get_snapcontext",
5325 NULL, 0, reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06005326 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder35d489f2012-07-03 16:01:19 -05005327 if (ret < 0)
5328 goto out;
5329
Alex Elder35d489f2012-07-03 16:01:19 -05005330 p = reply_buf;
Alex Elder57385b52013-04-21 12:14:45 -05005331 end = reply_buf + ret;
5332 ret = -ERANGE;
Alex Elder35d489f2012-07-03 16:01:19 -05005333 ceph_decode_64_safe(&p, end, seq, out);
5334 ceph_decode_32_safe(&p, end, snap_count, out);
5335
5336 /*
5337 * Make sure the reported number of snapshot ids wouldn't go
5338 * beyond the end of our buffer. But before checking that,
5339 * make sure the computed size of the snapshot context we
5340 * allocate is representable in a size_t.
5341 */
5342 if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
5343 / sizeof (u64)) {
5344 ret = -EINVAL;
5345 goto out;
5346 }
5347 if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
5348 goto out;
Alex Elder468521c2013-04-26 09:43:47 -05005349 ret = 0;
Alex Elder35d489f2012-07-03 16:01:19 -05005350
Alex Elder812164f82013-04-30 00:44:32 -05005351 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
Alex Elder35d489f2012-07-03 16:01:19 -05005352 if (!snapc) {
5353 ret = -ENOMEM;
5354 goto out;
5355 }
Alex Elder35d489f2012-07-03 16:01:19 -05005356 snapc->seq = seq;
Alex Elder35d489f2012-07-03 16:01:19 -05005357 for (i = 0; i < snap_count; i++)
5358 snapc->snaps[i] = ceph_decode_64(&p);
5359
Alex Elder49ece552013-05-06 08:37:00 -05005360 ceph_put_snap_context(rbd_dev->header.snapc);
Alex Elder35d489f2012-07-03 16:01:19 -05005361 rbd_dev->header.snapc = snapc;
5362
5363 dout(" snap context seq = %llu, snap_count = %u\n",
Alex Elder57385b52013-04-21 12:14:45 -05005364 (unsigned long long)seq, (unsigned int)snap_count);
Alex Elder35d489f2012-07-03 16:01:19 -05005365out:
5366 kfree(reply_buf);
5367
Alex Elder57385b52013-04-21 12:14:45 -05005368 return ret;
Alex Elder35d489f2012-07-03 16:01:19 -05005369}
5370
Alex Elder54cac612013-04-30 00:44:33 -05005371static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
5372 u64 snap_id)
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005373{
5374 size_t size;
5375 void *reply_buf;
Alex Elder54cac612013-04-30 00:44:33 -05005376 __le64 snapid;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005377 int ret;
5378 void *p;
5379 void *end;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005380 char *snap_name;
5381
5382 size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN;
5383 reply_buf = kmalloc(size, GFP_KERNEL);
5384 if (!reply_buf)
5385 return ERR_PTR(-ENOMEM);
5386
Alex Elder54cac612013-04-30 00:44:33 -05005387 snapid = cpu_to_le64(snap_id);
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005388 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5389 &rbd_dev->header_oloc, "get_snapshot_name",
5390 &snapid, sizeof(snapid), reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06005391 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderf40eb342013-04-25 15:09:42 -05005392 if (ret < 0) {
5393 snap_name = ERR_PTR(ret);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005394 goto out;
Alex Elderf40eb342013-04-25 15:09:42 -05005395 }
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005396
5397 p = reply_buf;
Alex Elderf40eb342013-04-25 15:09:42 -05005398 end = reply_buf + ret;
Alex Eldere5c35532012-10-25 23:34:41 -05005399 snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elderf40eb342013-04-25 15:09:42 -05005400 if (IS_ERR(snap_name))
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005401 goto out;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005402
Alex Elderf40eb342013-04-25 15:09:42 -05005403 dout(" snap_id 0x%016llx snap_name = %s\n",
Alex Elder54cac612013-04-30 00:44:33 -05005404 (unsigned long long)snap_id, snap_name);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005405out:
5406 kfree(reply_buf);
5407
Alex Elderf40eb342013-04-25 15:09:42 -05005408 return snap_name;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005409}
5410
Alex Elder2df3fac2013-05-06 09:51:30 -05005411static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev)
Alex Elder117973f2012-08-31 17:29:55 -05005412{
Alex Elder2df3fac2013-05-06 09:51:30 -05005413 bool first_time = rbd_dev->header.object_prefix == NULL;
Alex Elder117973f2012-08-31 17:29:55 -05005414 int ret;
Alex Elder117973f2012-08-31 17:29:55 -05005415
Josh Durgin1617e402013-06-12 14:43:10 -07005416 ret = rbd_dev_v2_image_size(rbd_dev);
5417 if (ret)
Alex Eldercfbf6372013-05-31 17:40:45 -05005418 return ret;
Josh Durgin1617e402013-06-12 14:43:10 -07005419
Alex Elder2df3fac2013-05-06 09:51:30 -05005420 if (first_time) {
5421 ret = rbd_dev_v2_header_onetime(rbd_dev);
5422 if (ret)
Alex Eldercfbf6372013-05-31 17:40:45 -05005423 return ret;
Alex Elder2df3fac2013-05-06 09:51:30 -05005424 }
5425
Alex Eldercc4a38bd2013-04-30 00:44:33 -05005426 ret = rbd_dev_v2_snap_context(rbd_dev);
Ilya Dryomovd194cd12015-08-31 18:22:10 +03005427 if (ret && first_time) {
5428 kfree(rbd_dev->header.object_prefix);
5429 rbd_dev->header.object_prefix = NULL;
5430 }
Alex Elder117973f2012-08-31 17:29:55 -05005431
5432 return ret;
5433}
5434
Ilya Dryomova720ae02014-07-23 17:11:19 +04005435static int rbd_dev_header_info(struct rbd_device *rbd_dev)
5436{
5437 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
5438
5439 if (rbd_dev->image_format == 1)
5440 return rbd_dev_v1_header_info(rbd_dev);
5441
5442 return rbd_dev_v2_header_info(rbd_dev);
5443}
5444
Alex Elder1ddbe942012-01-29 13:57:44 -06005445/*
Alex Eldere28fff262012-02-02 08:13:30 -06005446 * Skips over white space at *buf, and updates *buf to point to the
5447 * first found non-space character (if any). Returns the length of
Alex Elder593a9e72012-02-07 12:03:37 -06005448 * the token (string of non-white space characters) found. Note
5449 * that *buf must be terminated with '\0'.
Alex Eldere28fff262012-02-02 08:13:30 -06005450 */
5451static inline size_t next_token(const char **buf)
5452{
5453 /*
5454 * These are the characters that produce nonzero for
5455 * isspace() in the "C" and "POSIX" locales.
5456 */
5457 const char *spaces = " \f\n\r\t\v";
5458
5459 *buf += strspn(*buf, spaces); /* Find start of token */
5460
5461 return strcspn(*buf, spaces); /* Return token length */
5462}
5463
5464/*
Alex Elderea3352f2012-07-09 21:04:23 -05005465 * Finds the next token in *buf, dynamically allocates a buffer big
5466 * enough to hold a copy of it, and copies the token into the new
5467 * buffer. The copy is guaranteed to be terminated with '\0'. Note
5468 * that a duplicate buffer is created even for a zero-length token.
5469 *
5470 * Returns a pointer to the newly-allocated duplicate, or a null
5471 * pointer if memory for the duplicate was not available. If
5472 * the lenp argument is a non-null pointer, the length of the token
5473 * (not including the '\0') is returned in *lenp.
5474 *
5475 * If successful, the *buf pointer will be updated to point beyond
5476 * the end of the found token.
5477 *
5478 * Note: uses GFP_KERNEL for allocation.
5479 */
5480static inline char *dup_token(const char **buf, size_t *lenp)
5481{
5482 char *dup;
5483 size_t len;
5484
5485 len = next_token(buf);
Alex Elder4caf35f2012-11-01 08:39:27 -05005486 dup = kmemdup(*buf, len + 1, GFP_KERNEL);
Alex Elderea3352f2012-07-09 21:04:23 -05005487 if (!dup)
5488 return NULL;
Alex Elderea3352f2012-07-09 21:04:23 -05005489 *(dup + len) = '\0';
5490 *buf += len;
5491
5492 if (lenp)
5493 *lenp = len;
5494
5495 return dup;
5496}
5497
5498/*
Alex Elder859c31d2012-10-25 23:34:42 -05005499 * Parse the options provided for an "rbd add" (i.e., rbd image
5500 * mapping) request. These arrive via a write to /sys/bus/rbd/add,
5501 * and the data written is passed here via a NUL-terminated buffer.
5502 * Returns 0 if successful or an error code otherwise.
Alex Elderd22f76e2012-07-12 10:46:35 -05005503 *
Alex Elder859c31d2012-10-25 23:34:42 -05005504 * The information extracted from these options is recorded in
5505 * the other parameters which return dynamically-allocated
5506 * structures:
5507 * ceph_opts
5508 * The address of a pointer that will refer to a ceph options
5509 * structure. Caller must release the returned pointer using
5510 * ceph_destroy_options() when it is no longer needed.
5511 * rbd_opts
5512 * Address of an rbd options pointer. Fully initialized by
5513 * this function; caller must release with kfree().
5514 * spec
5515 * Address of an rbd image specification pointer. Fully
5516 * initialized by this function based on parsed options.
5517 * Caller must release with rbd_spec_put().
5518 *
5519 * The options passed take this form:
5520 * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
5521 * where:
5522 * <mon_addrs>
5523 * A comma-separated list of one or more monitor addresses.
5524 * A monitor address is an ip address, optionally followed
5525 * by a port number (separated by a colon).
5526 * I.e.: ip1[:port1][,ip2[:port2]...]
5527 * <options>
5528 * A comma-separated list of ceph and/or rbd options.
5529 * <pool_name>
5530 * The name of the rados pool containing the rbd image.
5531 * <image_name>
5532 * The name of the image in that pool to map.
5533 * <snap_id>
5534 * An optional snapshot id. If provided, the mapping will
5535 * present data from the image at the time that snapshot was
5536 * created. The image head is used if no snapshot id is
5537 * provided. Snapshot mappings are always read-only.
Alex Eldera725f65e2012-02-02 08:13:30 -06005538 */
Alex Elder859c31d2012-10-25 23:34:42 -05005539static int rbd_add_parse_args(const char *buf,
Alex Elderdc79b112012-10-25 23:34:41 -05005540 struct ceph_options **ceph_opts,
Alex Elder859c31d2012-10-25 23:34:42 -05005541 struct rbd_options **opts,
5542 struct rbd_spec **rbd_spec)
Alex Eldera725f65e2012-02-02 08:13:30 -06005543{
Alex Elderd22f76e2012-07-12 10:46:35 -05005544 size_t len;
Alex Elder859c31d2012-10-25 23:34:42 -05005545 char *options;
Alex Elder0ddebc02012-10-25 23:34:41 -05005546 const char *mon_addrs;
Alex Elderecb4dc22013-04-26 09:43:47 -05005547 char *snap_name;
Alex Elder0ddebc02012-10-25 23:34:41 -05005548 size_t mon_addrs_size;
Alex Elder859c31d2012-10-25 23:34:42 -05005549 struct rbd_spec *spec = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05005550 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05005551 struct ceph_options *copts;
Alex Elderdc79b112012-10-25 23:34:41 -05005552 int ret;
Alex Eldere28fff262012-02-02 08:13:30 -06005553
5554 /* The first four tokens are required */
5555
Alex Elder7ef32142012-02-02 08:13:30 -06005556 len = next_token(&buf);
Alex Elder4fb5d6712012-11-01 10:17:15 -05005557 if (!len) {
5558 rbd_warn(NULL, "no monitor address(es) provided");
5559 return -EINVAL;
5560 }
Alex Elder0ddebc02012-10-25 23:34:41 -05005561 mon_addrs = buf;
Alex Elderf28e5652012-10-25 23:34:41 -05005562 mon_addrs_size = len + 1;
Alex Elder7ef32142012-02-02 08:13:30 -06005563 buf += len;
Alex Eldera725f65e2012-02-02 08:13:30 -06005564
Alex Elderdc79b112012-10-25 23:34:41 -05005565 ret = -EINVAL;
Alex Elderf28e5652012-10-25 23:34:41 -05005566 options = dup_token(&buf, NULL);
5567 if (!options)
Alex Elderdc79b112012-10-25 23:34:41 -05005568 return -ENOMEM;
Alex Elder4fb5d6712012-11-01 10:17:15 -05005569 if (!*options) {
5570 rbd_warn(NULL, "no options provided");
5571 goto out_err;
5572 }
Alex Eldera725f65e2012-02-02 08:13:30 -06005573
Alex Elder859c31d2012-10-25 23:34:42 -05005574 spec = rbd_spec_alloc();
5575 if (!spec)
Alex Elderf28e5652012-10-25 23:34:41 -05005576 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05005577
5578 spec->pool_name = dup_token(&buf, NULL);
5579 if (!spec->pool_name)
5580 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05005581 if (!*spec->pool_name) {
5582 rbd_warn(NULL, "no pool name provided");
5583 goto out_err;
5584 }
Alex Eldere28fff262012-02-02 08:13:30 -06005585
Alex Elder69e7a022012-11-01 08:39:26 -05005586 spec->image_name = dup_token(&buf, NULL);
Alex Elder859c31d2012-10-25 23:34:42 -05005587 if (!spec->image_name)
Alex Elderf28e5652012-10-25 23:34:41 -05005588 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05005589 if (!*spec->image_name) {
5590 rbd_warn(NULL, "no image name provided");
5591 goto out_err;
5592 }
Alex Eldere28fff262012-02-02 08:13:30 -06005593
Alex Elderf28e5652012-10-25 23:34:41 -05005594 /*
5595 * Snapshot name is optional; default is to use "-"
5596 * (indicating the head/no snapshot).
5597 */
Alex Elder3feeb8942012-08-31 17:29:52 -05005598 len = next_token(&buf);
Alex Elder820a5f32012-07-09 21:04:24 -05005599 if (!len) {
Alex Elder3feeb8942012-08-31 17:29:52 -05005600 buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
5601 len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
Alex Elderf28e5652012-10-25 23:34:41 -05005602 } else if (len > RBD_MAX_SNAP_NAME_LEN) {
Alex Elderdc79b112012-10-25 23:34:41 -05005603 ret = -ENAMETOOLONG;
Alex Elderf28e5652012-10-25 23:34:41 -05005604 goto out_err;
Alex Elder849b4262012-07-09 21:04:24 -05005605 }
Alex Elderecb4dc22013-04-26 09:43:47 -05005606 snap_name = kmemdup(buf, len + 1, GFP_KERNEL);
5607 if (!snap_name)
Alex Elderf28e5652012-10-25 23:34:41 -05005608 goto out_mem;
Alex Elderecb4dc22013-04-26 09:43:47 -05005609 *(snap_name + len) = '\0';
5610 spec->snap_name = snap_name;
Alex Eldere5c35532012-10-25 23:34:41 -05005611
Alex Elder0ddebc02012-10-25 23:34:41 -05005612 /* Initialize all rbd options to the defaults */
Alex Eldere28fff262012-02-02 08:13:30 -06005613
Alex Elder4e9afeb2012-10-25 23:34:41 -05005614 rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL);
5615 if (!rbd_opts)
5616 goto out_mem;
5617
5618 rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
Ilya Dryomovb5584182015-06-23 16:21:19 +03005619 rbd_opts->queue_depth = RBD_QUEUE_DEPTH_DEFAULT;
Ilya Dryomov80de1912016-09-20 14:23:17 +02005620 rbd_opts->lock_on_read = RBD_LOCK_ON_READ_DEFAULT;
Alex Elderd22f76e2012-07-12 10:46:35 -05005621
Alex Elder859c31d2012-10-25 23:34:42 -05005622 copts = ceph_parse_options(options, mon_addrs,
Alex Elder0ddebc02012-10-25 23:34:41 -05005623 mon_addrs + mon_addrs_size - 1,
Alex Elder4e9afeb2012-10-25 23:34:41 -05005624 parse_rbd_opts_token, rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05005625 if (IS_ERR(copts)) {
5626 ret = PTR_ERR(copts);
Alex Elderdc79b112012-10-25 23:34:41 -05005627 goto out_err;
5628 }
Alex Elder859c31d2012-10-25 23:34:42 -05005629 kfree(options);
5630
5631 *ceph_opts = copts;
Alex Elder4e9afeb2012-10-25 23:34:41 -05005632 *opts = rbd_opts;
Alex Elder859c31d2012-10-25 23:34:42 -05005633 *rbd_spec = spec;
Alex Elder0ddebc02012-10-25 23:34:41 -05005634
Alex Elderdc79b112012-10-25 23:34:41 -05005635 return 0;
Alex Elderf28e5652012-10-25 23:34:41 -05005636out_mem:
Alex Elderdc79b112012-10-25 23:34:41 -05005637 ret = -ENOMEM;
Alex Elderd22f76e2012-07-12 10:46:35 -05005638out_err:
Alex Elder859c31d2012-10-25 23:34:42 -05005639 kfree(rbd_opts);
5640 rbd_spec_put(spec);
Alex Elderf28e5652012-10-25 23:34:41 -05005641 kfree(options);
Alex Elderd22f76e2012-07-12 10:46:35 -05005642
Alex Elderdc79b112012-10-25 23:34:41 -05005643 return ret;
Alex Eldera725f65e2012-02-02 08:13:30 -06005644}
5645
Alex Elder589d30e2012-07-10 20:30:11 -05005646/*
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005647 * Return pool id (>= 0) or a negative error code.
5648 */
5649static int rbd_add_get_pool_id(struct rbd_client *rbdc, const char *pool_name)
5650{
Ilya Dryomova319bf52015-05-15 12:02:17 +03005651 struct ceph_options *opts = rbdc->client->options;
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005652 u64 newest_epoch;
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005653 int tries = 0;
5654 int ret;
5655
5656again:
5657 ret = ceph_pg_poolid_by_name(rbdc->client->osdc.osdmap, pool_name);
5658 if (ret == -ENOENT && tries++ < 1) {
Ilya Dryomovd0b19702016-04-28 16:07:27 +02005659 ret = ceph_monc_get_version(&rbdc->client->monc, "osdmap",
5660 &newest_epoch);
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005661 if (ret < 0)
5662 return ret;
5663
5664 if (rbdc->client->osdc.osdmap->epoch < newest_epoch) {
Ilya Dryomov7cca78c2016-04-28 16:07:28 +02005665 ceph_osdc_maybe_request_map(&rbdc->client->osdc);
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005666 (void) ceph_monc_wait_osdmap(&rbdc->client->monc,
Ilya Dryomova319bf52015-05-15 12:02:17 +03005667 newest_epoch,
5668 opts->mount_timeout);
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005669 goto again;
5670 } else {
5671 /* the osdmap we have is new enough */
5672 return -ENOENT;
5673 }
5674 }
5675
5676 return ret;
5677}
5678
5679/*
Alex Elder589d30e2012-07-10 20:30:11 -05005680 * An rbd format 2 image has a unique identifier, distinct from the
5681 * name given to it by the user. Internally, that identifier is
5682 * what's used to specify the names of objects related to the image.
5683 *
5684 * A special "rbd id" object is used to map an rbd image name to its
5685 * id. If that object doesn't exist, then there is no v2 rbd image
5686 * with the supplied name.
5687 *
5688 * This function will record the given rbd_dev's image_id field if
5689 * it can be determined, and in that case will return 0. If any
5690 * errors occur a negative errno will be returned and the rbd_dev's
5691 * image_id field will be unchanged (and should be NULL).
5692 */
5693static int rbd_dev_image_id(struct rbd_device *rbd_dev)
5694{
5695 int ret;
5696 size_t size;
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005697 CEPH_DEFINE_OID_ONSTACK(oid);
Alex Elder589d30e2012-07-10 20:30:11 -05005698 void *response;
Alex Elderc0fba362013-04-25 23:15:08 -05005699 char *image_id;
Alex Elder2f82ee52012-10-30 19:40:33 -05005700
Alex Elder589d30e2012-07-10 20:30:11 -05005701 /*
Alex Elder2c0d0a12012-10-30 19:40:33 -05005702 * When probing a parent image, the image id is already
5703 * known (and the image name likely is not). There's no
Alex Elderc0fba362013-04-25 23:15:08 -05005704 * need to fetch the image id again in this case. We
5705 * do still need to set the image format though.
Alex Elder2c0d0a12012-10-30 19:40:33 -05005706 */
Alex Elderc0fba362013-04-25 23:15:08 -05005707 if (rbd_dev->spec->image_id) {
5708 rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1;
5709
Alex Elder2c0d0a12012-10-30 19:40:33 -05005710 return 0;
Alex Elderc0fba362013-04-25 23:15:08 -05005711 }
Alex Elder2c0d0a12012-10-30 19:40:33 -05005712
5713 /*
Alex Elder589d30e2012-07-10 20:30:11 -05005714 * First, see if the format 2 image id file exists, and if
5715 * so, get the image's persistent id from it.
5716 */
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005717 ret = ceph_oid_aprintf(&oid, GFP_KERNEL, "%s%s", RBD_ID_PREFIX,
5718 rbd_dev->spec->image_name);
5719 if (ret)
5720 return ret;
5721
5722 dout("rbd id object name is %s\n", oid.name);
Alex Elder589d30e2012-07-10 20:30:11 -05005723
5724 /* Response will be an encoded string, which includes a length */
5725
5726 size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
5727 response = kzalloc(size, GFP_NOIO);
5728 if (!response) {
5729 ret = -ENOMEM;
5730 goto out;
5731 }
5732
Alex Elderc0fba362013-04-25 23:15:08 -05005733 /* If it doesn't exist we'll assume it's a format 1 image */
5734
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005735 ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc,
5736 "get_id", NULL, 0,
5737 response, RBD_IMAGE_ID_LEN_MAX);
Alex Elder36be9a72013-01-19 00:30:28 -06005738 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderc0fba362013-04-25 23:15:08 -05005739 if (ret == -ENOENT) {
5740 image_id = kstrdup("", GFP_KERNEL);
5741 ret = image_id ? 0 : -ENOMEM;
5742 if (!ret)
5743 rbd_dev->image_format = 1;
Ilya Dryomov7dd440c2014-09-11 18:49:18 +04005744 } else if (ret >= 0) {
Alex Elderc0fba362013-04-25 23:15:08 -05005745 void *p = response;
Alex Elder589d30e2012-07-10 20:30:11 -05005746
Alex Elderc0fba362013-04-25 23:15:08 -05005747 image_id = ceph_extract_encoded_string(&p, p + ret,
Alex Elder979ed482012-11-01 08:39:26 -05005748 NULL, GFP_NOIO);
Duan Jiong461f7582014-04-11 16:38:12 +08005749 ret = PTR_ERR_OR_ZERO(image_id);
Alex Elderc0fba362013-04-25 23:15:08 -05005750 if (!ret)
5751 rbd_dev->image_format = 2;
Alex Elderc0fba362013-04-25 23:15:08 -05005752 }
5753
5754 if (!ret) {
5755 rbd_dev->spec->image_id = image_id;
5756 dout("image_id is %s\n", image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05005757 }
5758out:
5759 kfree(response);
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005760 ceph_oid_destroy(&oid);
Alex Elder589d30e2012-07-10 20:30:11 -05005761 return ret;
5762}
5763
Alex Elder3abef3b2013-05-13 20:35:37 -05005764/*
5765 * Undo whatever state changes are made by v1 or v2 header info
5766 * call.
5767 */
Alex Elder6fd48b32013-04-28 23:32:34 -05005768static void rbd_dev_unprobe(struct rbd_device *rbd_dev)
5769{
5770 struct rbd_image_header *header;
5771
Ilya Dryomove69b8d42015-01-19 12:06:14 +03005772 rbd_dev_parent_put(rbd_dev);
Alex Elder6fd48b32013-04-28 23:32:34 -05005773
5774 /* Free dynamic fields from the header, then zero it out */
5775
5776 header = &rbd_dev->header;
Alex Elder812164f82013-04-30 00:44:32 -05005777 ceph_put_snap_context(header->snapc);
Alex Elder6fd48b32013-04-28 23:32:34 -05005778 kfree(header->snap_sizes);
5779 kfree(header->snap_names);
5780 kfree(header->object_prefix);
5781 memset(header, 0, sizeof (*header));
5782}
5783
Alex Elder2df3fac2013-05-06 09:51:30 -05005784static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev)
Alex Eldera30b71b2012-07-10 20:30:11 -05005785{
5786 int ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05005787
Alex Elder1e130192012-07-03 16:01:19 -05005788 ret = rbd_dev_v2_object_prefix(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05005789 if (ret)
Alex Elder1e130192012-07-03 16:01:19 -05005790 goto out_err;
Alex Elderb1b54022012-07-03 16:01:19 -05005791
Alex Elder2df3fac2013-05-06 09:51:30 -05005792 /*
5793 * Get the and check features for the image. Currently the
5794 * features are assumed to never change.
5795 */
Alex Elderb1b54022012-07-03 16:01:19 -05005796 ret = rbd_dev_v2_features(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05005797 if (ret)
Alex Elderb1b54022012-07-03 16:01:19 -05005798 goto out_err;
Alex Elder35d489f2012-07-03 16:01:19 -05005799
Alex Eldercc070d52013-04-21 12:14:45 -05005800 /* If the image supports fancy striping, get its parameters */
5801
5802 if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) {
5803 ret = rbd_dev_v2_striping_info(rbd_dev);
5804 if (ret < 0)
5805 goto out_err;
5806 }
Alex Eldera30b71b2012-07-10 20:30:11 -05005807
Ilya Dryomov7e973322017-01-25 18:16:22 +01005808 if (rbd_dev->header.features & RBD_FEATURE_DATA_POOL) {
5809 ret = rbd_dev_v2_data_pool(rbd_dev);
5810 if (ret)
5811 goto out_err;
5812 }
5813
Ilya Dryomov263423f2017-01-25 18:16:22 +01005814 rbd_init_layout(rbd_dev);
Alex Elder35152972012-08-31 17:29:55 -05005815 return 0;
Ilya Dryomov263423f2017-01-25 18:16:22 +01005816
Alex Elder9d475de2012-07-03 16:01:19 -05005817out_err:
Alex Elder642a2532013-05-06 17:40:33 -05005818 rbd_dev->header.features = 0;
Alex Elder1e130192012-07-03 16:01:19 -05005819 kfree(rbd_dev->header.object_prefix);
5820 rbd_dev->header.object_prefix = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05005821 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05005822}
5823
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005824/*
5825 * @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() ->
5826 * rbd_dev_image_probe() recursion depth, which means it's also the
5827 * length of the already discovered part of the parent chain.
5828 */
5829static int rbd_dev_probe_parent(struct rbd_device *rbd_dev, int depth)
Alex Elder83a06262012-10-30 15:47:17 -05005830{
Alex Elder2f82ee52012-10-30 19:40:33 -05005831 struct rbd_device *parent = NULL;
Alex Elder124afba2013-04-26 15:44:36 -05005832 int ret;
5833
5834 if (!rbd_dev->parent_spec)
5835 return 0;
Alex Elder124afba2013-04-26 15:44:36 -05005836
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005837 if (++depth > RBD_MAX_PARENT_CHAIN_LEN) {
5838 pr_info("parent chain is too long (%d)\n", depth);
5839 ret = -EINVAL;
5840 goto out_err;
5841 }
5842
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02005843 parent = __rbd_dev_create(rbd_dev->rbd_client, rbd_dev->parent_spec);
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005844 if (!parent) {
5845 ret = -ENOMEM;
Alex Elder124afba2013-04-26 15:44:36 -05005846 goto out_err;
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005847 }
5848
5849 /*
5850 * Images related by parent/child relationships always share
5851 * rbd_client and spec/parent_spec, so bump their refcounts.
5852 */
5853 __rbd_get_client(rbd_dev->rbd_client);
5854 rbd_spec_get(rbd_dev->parent_spec);
Alex Elder124afba2013-04-26 15:44:36 -05005855
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005856 ret = rbd_dev_image_probe(parent, depth);
Alex Elder124afba2013-04-26 15:44:36 -05005857 if (ret < 0)
5858 goto out_err;
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005859
Alex Elder124afba2013-04-26 15:44:36 -05005860 rbd_dev->parent = parent;
Alex Eldera2acd002013-05-08 22:50:04 -05005861 atomic_set(&rbd_dev->parent_ref, 1);
Alex Elder124afba2013-04-26 15:44:36 -05005862 return 0;
Alex Elder124afba2013-04-26 15:44:36 -05005863
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005864out_err:
5865 rbd_dev_unparent(rbd_dev);
Markus Elfring1761b222015-11-23 20:16:45 +01005866 rbd_dev_destroy(parent);
Alex Elder124afba2013-04-26 15:44:36 -05005867 return ret;
5868}
5869
Ilya Dryomov811c6682016-04-15 16:22:16 +02005870/*
5871 * rbd_dev->header_rwsem must be locked for write and will be unlocked
5872 * upon return.
5873 */
Alex Elder200a6a82013-04-28 23:32:34 -05005874static int rbd_dev_device_setup(struct rbd_device *rbd_dev)
Alex Elder124afba2013-04-26 15:44:36 -05005875{
Alex Elder83a06262012-10-30 15:47:17 -05005876 int ret;
Alex Elder83a06262012-10-30 15:47:17 -05005877
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005878 /* Record our major and minor device numbers. */
Alex Elder83a06262012-10-30 15:47:17 -05005879
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005880 if (!single_major) {
5881 ret = register_blkdev(0, rbd_dev->name);
5882 if (ret < 0)
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02005883 goto err_out_unlock;
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005884
5885 rbd_dev->major = ret;
5886 rbd_dev->minor = 0;
5887 } else {
5888 rbd_dev->major = rbd_major;
5889 rbd_dev->minor = rbd_dev_id_to_minor(rbd_dev->dev_id);
5890 }
Alex Elder83a06262012-10-30 15:47:17 -05005891
5892 /* Set up the blkdev mapping. */
5893
5894 ret = rbd_init_disk(rbd_dev);
5895 if (ret)
5896 goto err_out_blkdev;
5897
Alex Elderf35a4de2013-05-06 09:51:29 -05005898 ret = rbd_dev_mapping_set(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05005899 if (ret)
5900 goto err_out_disk;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04005901
Alex Elderf35a4de2013-05-06 09:51:29 -05005902 set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
Josh Durgin22001f62013-09-30 20:10:04 -07005903 set_disk_ro(rbd_dev->disk, rbd_dev->mapping.read_only);
Alex Elderf35a4de2013-05-06 09:51:29 -05005904
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02005905 dev_set_name(&rbd_dev->dev, "%d", rbd_dev->dev_id);
5906 ret = device_add(&rbd_dev->dev);
Alex Elderf35a4de2013-05-06 09:51:29 -05005907 if (ret)
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04005908 goto err_out_mapping;
Alex Elder83a06262012-10-30 15:47:17 -05005909
Alex Elder83a06262012-10-30 15:47:17 -05005910 /* Everything's ready. Announce the disk to the world. */
5911
Alex Elder129b79d2013-04-26 15:44:36 -05005912 set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
Ilya Dryomov811c6682016-04-15 16:22:16 +02005913 up_write(&rbd_dev->header_rwsem);
Alex Elder83a06262012-10-30 15:47:17 -05005914
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02005915 spin_lock(&rbd_dev_list_lock);
5916 list_add_tail(&rbd_dev->node, &rbd_dev_list);
5917 spin_unlock(&rbd_dev_list_lock);
5918
Ilya Dryomov811c6682016-04-15 16:22:16 +02005919 add_disk(rbd_dev->disk);
Ilya Dryomovca7909e2016-08-18 18:38:41 +02005920 pr_info("%s: capacity %llu features 0x%llx\n", rbd_dev->disk->disk_name,
5921 (unsigned long long)get_capacity(rbd_dev->disk) << SECTOR_SHIFT,
5922 rbd_dev->header.features);
Alex Elder83a06262012-10-30 15:47:17 -05005923
5924 return ret;
Alex Elder2f82ee52012-10-30 19:40:33 -05005925
Alex Elderf35a4de2013-05-06 09:51:29 -05005926err_out_mapping:
5927 rbd_dev_mapping_clear(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05005928err_out_disk:
5929 rbd_free_disk(rbd_dev);
5930err_out_blkdev:
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005931 if (!single_major)
5932 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Ilya Dryomov811c6682016-04-15 16:22:16 +02005933err_out_unlock:
5934 up_write(&rbd_dev->header_rwsem);
Alex Elder83a06262012-10-30 15:47:17 -05005935 return ret;
5936}
5937
Alex Elder332bb122013-04-27 09:59:30 -05005938static int rbd_dev_header_name(struct rbd_device *rbd_dev)
5939{
5940 struct rbd_spec *spec = rbd_dev->spec;
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005941 int ret;
Alex Elder332bb122013-04-27 09:59:30 -05005942
5943 /* Record the header object name for this rbd image. */
5944
5945 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
Alex Elder332bb122013-04-27 09:59:30 -05005946 if (rbd_dev->image_format == 1)
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005947 ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s",
5948 spec->image_name, RBD_SUFFIX);
Alex Elder332bb122013-04-27 09:59:30 -05005949 else
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005950 ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s",
5951 RBD_HEADER_PREFIX, spec->image_id);
Alex Elder332bb122013-04-27 09:59:30 -05005952
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005953 return ret;
Alex Elder332bb122013-04-27 09:59:30 -05005954}
5955
Alex Elder200a6a82013-04-28 23:32:34 -05005956static void rbd_dev_image_release(struct rbd_device *rbd_dev)
5957{
Alex Elder6fd48b32013-04-28 23:32:34 -05005958 rbd_dev_unprobe(rbd_dev);
Alex Elder6fd48b32013-04-28 23:32:34 -05005959 rbd_dev->image_format = 0;
5960 kfree(rbd_dev->spec->image_id);
5961 rbd_dev->spec->image_id = NULL;
5962
Alex Elder200a6a82013-04-28 23:32:34 -05005963 rbd_dev_destroy(rbd_dev);
5964}
5965
Alex Eldera30b71b2012-07-10 20:30:11 -05005966/*
5967 * Probe for the existence of the header object for the given rbd
Alex Elder1f3ef782013-05-06 17:40:33 -05005968 * device. If this image is the one being mapped (i.e., not a
5969 * parent), initiate a watch on its header object before using that
5970 * object to get detailed information about the rbd image.
Alex Eldera30b71b2012-07-10 20:30:11 -05005971 */
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005972static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth)
Alex Eldera30b71b2012-07-10 20:30:11 -05005973{
5974 int ret;
5975
5976 /*
Alex Elder3abef3b2013-05-13 20:35:37 -05005977 * Get the id from the image id object. Unless there's an
5978 * error, rbd_dev->spec->image_id will be filled in with
5979 * a dynamically-allocated string, and rbd_dev->image_format
5980 * will be set to either 1 or 2.
Alex Eldera30b71b2012-07-10 20:30:11 -05005981 */
5982 ret = rbd_dev_image_id(rbd_dev);
5983 if (ret)
Alex Elderc0fba362013-04-25 23:15:08 -05005984 return ret;
Alex Elderc0fba362013-04-25 23:15:08 -05005985
Alex Elder332bb122013-04-27 09:59:30 -05005986 ret = rbd_dev_header_name(rbd_dev);
5987 if (ret)
5988 goto err_out_format;
5989
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005990 if (!depth) {
Ilya Dryomov99d16942016-08-12 16:11:41 +02005991 ret = rbd_register_watch(rbd_dev);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03005992 if (ret) {
5993 if (ret == -ENOENT)
5994 pr_info("image %s/%s does not exist\n",
5995 rbd_dev->spec->pool_name,
5996 rbd_dev->spec->image_name);
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005997 goto err_out_format;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03005998 }
Alex Elder1f3ef782013-05-06 17:40:33 -05005999 }
Alex Elderb644de22013-04-27 09:59:31 -05006000
Ilya Dryomova720ae02014-07-23 17:11:19 +04006001 ret = rbd_dev_header_info(rbd_dev);
Alex Elder5655c4d2013-04-25 23:15:08 -05006002 if (ret)
Alex Elderb644de22013-04-27 09:59:31 -05006003 goto err_out_watch;
Alex Elder83a06262012-10-30 15:47:17 -05006004
Ilya Dryomov04077592014-07-23 17:11:20 +04006005 /*
6006 * If this image is the one being mapped, we have pool name and
6007 * id, image name and id, and snap name - need to fill snap id.
6008 * Otherwise this is a parent image, identified by pool, image
6009 * and snap ids - need to fill in names for those ids.
6010 */
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006011 if (!depth)
Ilya Dryomov04077592014-07-23 17:11:20 +04006012 ret = rbd_spec_fill_snap_id(rbd_dev);
6013 else
6014 ret = rbd_spec_fill_names(rbd_dev);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006015 if (ret) {
6016 if (ret == -ENOENT)
6017 pr_info("snap %s/%s@%s does not exist\n",
6018 rbd_dev->spec->pool_name,
6019 rbd_dev->spec->image_name,
6020 rbd_dev->spec->snap_name);
Alex Elder33dca392013-04-30 00:44:33 -05006021 goto err_out_probe;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006022 }
Alex Elder9bb81c92013-04-27 09:59:30 -05006023
Ilya Dryomove8f59b52014-07-24 10:42:13 +04006024 if (rbd_dev->header.features & RBD_FEATURE_LAYERING) {
6025 ret = rbd_dev_v2_parent_info(rbd_dev);
6026 if (ret)
6027 goto err_out_probe;
6028
6029 /*
6030 * Need to warn users if this image is the one being
6031 * mapped and has a parent.
6032 */
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006033 if (!depth && rbd_dev->parent_spec)
Ilya Dryomove8f59b52014-07-24 10:42:13 +04006034 rbd_warn(rbd_dev,
6035 "WARNING: kernel layering is EXPERIMENTAL!");
6036 }
6037
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006038 ret = rbd_dev_probe_parent(rbd_dev, depth);
Alex Elder30d60ba2013-05-06 09:51:30 -05006039 if (ret)
6040 goto err_out_probe;
Alex Elder83a06262012-10-30 15:47:17 -05006041
Alex Elder30d60ba2013-05-06 09:51:30 -05006042 dout("discovered format %u image, header name is %s\n",
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006043 rbd_dev->image_format, rbd_dev->header_oid.name);
Alex Elder30d60ba2013-05-06 09:51:30 -05006044 return 0;
Ilya Dryomove8f59b52014-07-24 10:42:13 +04006045
Alex Elder6fd48b32013-04-28 23:32:34 -05006046err_out_probe:
6047 rbd_dev_unprobe(rbd_dev);
Alex Elderb644de22013-04-27 09:59:31 -05006048err_out_watch:
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006049 if (!depth)
Ilya Dryomov99d16942016-08-12 16:11:41 +02006050 rbd_unregister_watch(rbd_dev);
Alex Elder332bb122013-04-27 09:59:30 -05006051err_out_format:
6052 rbd_dev->image_format = 0;
Alex Elder5655c4d2013-04-25 23:15:08 -05006053 kfree(rbd_dev->spec->image_id);
6054 rbd_dev->spec->image_id = NULL;
Alex Elder5655c4d2013-04-25 23:15:08 -05006055 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05006056}
6057
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006058static ssize_t do_rbd_add(struct bus_type *bus,
6059 const char *buf,
6060 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006061{
Alex Eldercb8627c2012-07-09 21:04:23 -05006062 struct rbd_device *rbd_dev = NULL;
Alex Elderdc79b112012-10-25 23:34:41 -05006063 struct ceph_options *ceph_opts = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05006064 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05006065 struct rbd_spec *spec = NULL;
Alex Elder9d3997f2012-10-25 23:34:42 -05006066 struct rbd_client *rbdc;
Alex Elder51344a32013-05-06 07:40:30 -05006067 bool read_only;
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02006068 int rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006069
6070 if (!try_module_get(THIS_MODULE))
6071 return -ENODEV;
6072
Alex Eldera725f65e2012-02-02 08:13:30 -06006073 /* parse add command */
Alex Elder859c31d2012-10-25 23:34:42 -05006074 rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec);
Alex Elderdc79b112012-10-25 23:34:41 -05006075 if (rc < 0)
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006076 goto out;
Alex Eldera725f65e2012-02-02 08:13:30 -06006077
Alex Elder9d3997f2012-10-25 23:34:42 -05006078 rbdc = rbd_get_client(ceph_opts);
6079 if (IS_ERR(rbdc)) {
6080 rc = PTR_ERR(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05006081 goto err_out_args;
Alex Elder9d3997f2012-10-25 23:34:42 -05006082 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006083
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006084 /* pick the pool */
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04006085 rc = rbd_add_get_pool_id(rbdc, spec->pool_name);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006086 if (rc < 0) {
6087 if (rc == -ENOENT)
6088 pr_info("pool %s does not exist\n", spec->pool_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006089 goto err_out_client;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006090 }
Alex Elderc0cd10db2013-04-26 09:43:47 -05006091 spec->pool_id = (u64)rc;
Alex Elder859c31d2012-10-25 23:34:42 -05006092
Ilya Dryomovd1475432015-06-22 13:24:48 +03006093 rbd_dev = rbd_dev_create(rbdc, spec, rbd_opts);
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02006094 if (!rbd_dev) {
6095 rc = -ENOMEM;
Alex Elderbd4ba652012-10-25 23:34:42 -05006096 goto err_out_client;
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02006097 }
Alex Elderc53d5892012-10-25 23:34:42 -05006098 rbdc = NULL; /* rbd_dev now owns this */
6099 spec = NULL; /* rbd_dev now owns this */
Ilya Dryomovd1475432015-06-22 13:24:48 +03006100 rbd_opts = NULL; /* rbd_dev now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006101
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02006102 rbd_dev->config_info = kstrdup(buf, GFP_KERNEL);
6103 if (!rbd_dev->config_info) {
6104 rc = -ENOMEM;
6105 goto err_out_rbd_dev;
6106 }
6107
Ilya Dryomov811c6682016-04-15 16:22:16 +02006108 down_write(&rbd_dev->header_rwsem);
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006109 rc = rbd_dev_image_probe(rbd_dev, 0);
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02006110 if (rc < 0) {
6111 up_write(&rbd_dev->header_rwsem);
Alex Elderc53d5892012-10-25 23:34:42 -05006112 goto err_out_rbd_dev;
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02006113 }
Alex Elder05fd6f62012-08-29 17:11:07 -05006114
Alex Elder7ce4eef2013-05-06 17:40:33 -05006115 /* If we are mapping a snapshot it must be marked read-only */
6116
Ilya Dryomovd1475432015-06-22 13:24:48 +03006117 read_only = rbd_dev->opts->read_only;
Alex Elder7ce4eef2013-05-06 17:40:33 -05006118 if (rbd_dev->spec->snap_id != CEPH_NOSNAP)
6119 read_only = true;
6120 rbd_dev->mapping.read_only = read_only;
6121
Alex Elderb536f692013-04-28 23:32:34 -05006122 rc = rbd_dev_device_setup(rbd_dev);
Alex Elder3abef3b2013-05-13 20:35:37 -05006123 if (rc) {
Ilya Dryomove37180c2013-12-16 18:02:41 +02006124 /*
Ilya Dryomov99d16942016-08-12 16:11:41 +02006125 * rbd_unregister_watch() can't be moved into
Ilya Dryomove37180c2013-12-16 18:02:41 +02006126 * rbd_dev_image_release() without refactoring, see
6127 * commit 1f3ef78861ac.
6128 */
Ilya Dryomov99d16942016-08-12 16:11:41 +02006129 rbd_unregister_watch(rbd_dev);
Alex Elder3abef3b2013-05-13 20:35:37 -05006130 rbd_dev_image_release(rbd_dev);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006131 goto out;
Alex Elder3abef3b2013-05-13 20:35:37 -05006132 }
Alex Elderb536f692013-04-28 23:32:34 -05006133
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006134 rc = count;
6135out:
6136 module_put(THIS_MODULE);
6137 return rc;
Alex Elder3abef3b2013-05-13 20:35:37 -05006138
Alex Elderc53d5892012-10-25 23:34:42 -05006139err_out_rbd_dev:
6140 rbd_dev_destroy(rbd_dev);
Alex Elderbd4ba652012-10-25 23:34:42 -05006141err_out_client:
Alex Elder9d3997f2012-10-25 23:34:42 -05006142 rbd_put_client(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05006143err_out_args:
Alex Elder859c31d2012-10-25 23:34:42 -05006144 rbd_spec_put(spec);
Ilya Dryomovd1475432015-06-22 13:24:48 +03006145 kfree(rbd_opts);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006146 goto out;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006147}
6148
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006149static ssize_t rbd_add(struct bus_type *bus,
6150 const char *buf,
6151 size_t count)
6152{
6153 if (single_major)
6154 return -EINVAL;
6155
6156 return do_rbd_add(bus, buf, count);
6157}
6158
6159static ssize_t rbd_add_single_major(struct bus_type *bus,
6160 const char *buf,
6161 size_t count)
6162{
6163 return do_rbd_add(bus, buf, count);
6164}
6165
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006166static void rbd_dev_device_release(struct rbd_device *rbd_dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006167{
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006168 rbd_free_disk(rbd_dev);
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02006169
6170 spin_lock(&rbd_dev_list_lock);
6171 list_del_init(&rbd_dev->node);
6172 spin_unlock(&rbd_dev_list_lock);
6173
Alex Elder200a6a82013-04-28 23:32:34 -05006174 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006175 device_del(&rbd_dev->dev);
Alex Elder6d80b132013-05-06 07:40:30 -05006176 rbd_dev_mapping_clear(rbd_dev);
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006177 if (!single_major)
6178 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006179}
6180
Alex Elder05a46af2013-04-26 15:44:36 -05006181static void rbd_dev_remove_parent(struct rbd_device *rbd_dev)
6182{
Alex Elderad945fc2013-04-26 15:44:36 -05006183 while (rbd_dev->parent) {
Alex Elder05a46af2013-04-26 15:44:36 -05006184 struct rbd_device *first = rbd_dev;
6185 struct rbd_device *second = first->parent;
6186 struct rbd_device *third;
6187
6188 /*
6189 * Follow to the parent with no grandparent and
6190 * remove it.
6191 */
6192 while (second && (third = second->parent)) {
6193 first = second;
6194 second = third;
6195 }
Alex Elderad945fc2013-04-26 15:44:36 -05006196 rbd_assert(second);
Alex Elder8ad42cd2013-04-28 23:32:34 -05006197 rbd_dev_image_release(second);
Alex Elderad945fc2013-04-26 15:44:36 -05006198 first->parent = NULL;
6199 first->parent_overlap = 0;
6200
6201 rbd_assert(first->parent_spec);
Alex Elder05a46af2013-04-26 15:44:36 -05006202 rbd_spec_put(first->parent_spec);
6203 first->parent_spec = NULL;
Alex Elder05a46af2013-04-26 15:44:36 -05006204 }
6205}
6206
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006207static ssize_t do_rbd_remove(struct bus_type *bus,
6208 const char *buf,
6209 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006210{
6211 struct rbd_device *rbd_dev = NULL;
Alex Elder751cc0e2013-05-31 15:17:01 -05006212 struct list_head *tmp;
6213 int dev_id;
Mike Christie0276dca2016-08-18 18:38:45 +02006214 char opt_buf[6];
Alex Elder82a442d2013-05-31 17:40:44 -05006215 bool already = false;
Mike Christie0276dca2016-08-18 18:38:45 +02006216 bool force = false;
Alex Elder0d8189e2013-04-27 09:59:30 -05006217 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006218
Mike Christie0276dca2016-08-18 18:38:45 +02006219 dev_id = -1;
6220 opt_buf[0] = '\0';
6221 sscanf(buf, "%d %5s", &dev_id, opt_buf);
6222 if (dev_id < 0) {
6223 pr_err("dev_id out of range\n");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006224 return -EINVAL;
Mike Christie0276dca2016-08-18 18:38:45 +02006225 }
6226 if (opt_buf[0] != '\0') {
6227 if (!strcmp(opt_buf, "force")) {
6228 force = true;
6229 } else {
6230 pr_err("bad remove option at '%s'\n", opt_buf);
6231 return -EINVAL;
6232 }
6233 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006234
Alex Elder751cc0e2013-05-31 15:17:01 -05006235 ret = -ENOENT;
6236 spin_lock(&rbd_dev_list_lock);
6237 list_for_each(tmp, &rbd_dev_list) {
6238 rbd_dev = list_entry(tmp, struct rbd_device, node);
6239 if (rbd_dev->dev_id == dev_id) {
6240 ret = 0;
6241 break;
6242 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006243 }
Alex Elder751cc0e2013-05-31 15:17:01 -05006244 if (!ret) {
6245 spin_lock_irq(&rbd_dev->lock);
Mike Christie0276dca2016-08-18 18:38:45 +02006246 if (rbd_dev->open_count && !force)
Alex Elder751cc0e2013-05-31 15:17:01 -05006247 ret = -EBUSY;
6248 else
Alex Elder82a442d2013-05-31 17:40:44 -05006249 already = test_and_set_bit(RBD_DEV_FLAG_REMOVING,
6250 &rbd_dev->flags);
Alex Elder751cc0e2013-05-31 15:17:01 -05006251 spin_unlock_irq(&rbd_dev->lock);
6252 }
6253 spin_unlock(&rbd_dev_list_lock);
Alex Elder82a442d2013-05-31 17:40:44 -05006254 if (ret < 0 || already)
Alex Elder1ba0f1e2013-05-31 15:17:01 -05006255 return ret;
Alex Elder751cc0e2013-05-31 15:17:01 -05006256
Mike Christie0276dca2016-08-18 18:38:45 +02006257 if (force) {
6258 /*
6259 * Prevent new IO from being queued and wait for existing
6260 * IO to complete/fail.
6261 */
6262 blk_mq_freeze_queue(rbd_dev->disk->queue);
6263 blk_set_queue_dying(rbd_dev->disk->queue);
6264 }
6265
Ilya Dryomoved95b212016-08-12 16:40:02 +02006266 down_write(&rbd_dev->lock_rwsem);
6267 if (__rbd_is_lock_owner(rbd_dev))
6268 rbd_unlock(rbd_dev);
6269 up_write(&rbd_dev->lock_rwsem);
Ilya Dryomov99d16942016-08-12 16:11:41 +02006270 rbd_unregister_watch(rbd_dev);
Ilya Dryomovfca27062013-12-16 18:02:40 +02006271
Josh Durgin98752012013-08-29 17:26:31 -07006272 /*
6273 * Don't free anything from rbd_dev->disk until after all
6274 * notifies are completely processed. Otherwise
6275 * rbd_bus_del_dev() will race with rbd_watch_cb(), resulting
6276 * in a potential use after free of rbd_dev->disk or rbd_dev.
6277 */
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006278 rbd_dev_device_release(rbd_dev);
Alex Elder8ad42cd2013-04-28 23:32:34 -05006279 rbd_dev_image_release(rbd_dev);
Alex Elderaafb2302012-09-06 16:00:54 -05006280
Alex Elder1ba0f1e2013-05-31 15:17:01 -05006281 return count;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006282}
6283
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006284static ssize_t rbd_remove(struct bus_type *bus,
6285 const char *buf,
6286 size_t count)
6287{
6288 if (single_major)
6289 return -EINVAL;
6290
6291 return do_rbd_remove(bus, buf, count);
6292}
6293
6294static ssize_t rbd_remove_single_major(struct bus_type *bus,
6295 const char *buf,
6296 size_t count)
6297{
6298 return do_rbd_remove(bus, buf, count);
6299}
6300
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006301/*
6302 * create control files in sysfs
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006303 * /sys/bus/rbd/...
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006304 */
6305static int rbd_sysfs_init(void)
6306{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006307 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006308
Alex Elderfed4c142012-02-07 12:03:36 -06006309 ret = device_register(&rbd_root_dev);
Alex Elder21079782012-01-24 10:08:36 -06006310 if (ret < 0)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006311 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006312
Alex Elderfed4c142012-02-07 12:03:36 -06006313 ret = bus_register(&rbd_bus_type);
6314 if (ret < 0)
6315 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006316
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006317 return ret;
6318}
6319
6320static void rbd_sysfs_cleanup(void)
6321{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006322 bus_unregister(&rbd_bus_type);
Alex Elderfed4c142012-02-07 12:03:36 -06006323 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006324}
6325
Alex Elder1c2a9df2013-05-01 12:43:03 -05006326static int rbd_slab_init(void)
6327{
6328 rbd_assert(!rbd_img_request_cache);
Geliang Tang03d94402016-03-13 15:17:32 +08006329 rbd_img_request_cache = KMEM_CACHE(rbd_img_request, 0);
Alex Elder868311b2013-05-01 12:43:03 -05006330 if (!rbd_img_request_cache)
6331 return -ENOMEM;
6332
6333 rbd_assert(!rbd_obj_request_cache);
Geliang Tang03d94402016-03-13 15:17:32 +08006334 rbd_obj_request_cache = KMEM_CACHE(rbd_obj_request, 0);
Alex Elder78c2a442013-05-01 12:43:04 -05006335 if (!rbd_obj_request_cache)
6336 goto out_err;
6337
Ilya Dryomov6c696d82017-01-25 18:16:23 +01006338 return 0;
Alex Elder1c2a9df2013-05-01 12:43:03 -05006339
Ilya Dryomov6c696d82017-01-25 18:16:23 +01006340out_err:
Alex Elder868311b2013-05-01 12:43:03 -05006341 kmem_cache_destroy(rbd_img_request_cache);
6342 rbd_img_request_cache = NULL;
Alex Elder1c2a9df2013-05-01 12:43:03 -05006343 return -ENOMEM;
6344}
6345
6346static void rbd_slab_exit(void)
6347{
Alex Elder868311b2013-05-01 12:43:03 -05006348 rbd_assert(rbd_obj_request_cache);
6349 kmem_cache_destroy(rbd_obj_request_cache);
6350 rbd_obj_request_cache = NULL;
6351
Alex Elder1c2a9df2013-05-01 12:43:03 -05006352 rbd_assert(rbd_img_request_cache);
6353 kmem_cache_destroy(rbd_img_request_cache);
6354 rbd_img_request_cache = NULL;
6355}
6356
Alex Eldercc344fa2013-02-19 12:25:56 -06006357static int __init rbd_init(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006358{
6359 int rc;
6360
Alex Elder1e32d342013-01-30 11:13:33 -06006361 if (!libceph_compatible(NULL)) {
6362 rbd_warn(NULL, "libceph incompatibility (quitting)");
Alex Elder1e32d342013-01-30 11:13:33 -06006363 return -EINVAL;
6364 }
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006365
Alex Elder1c2a9df2013-05-01 12:43:03 -05006366 rc = rbd_slab_init();
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006367 if (rc)
6368 return rc;
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006369
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006370 /*
6371 * The number of active work items is limited by the number of
Ilya Dryomovf77303b2015-04-22 18:28:13 +03006372 * rbd devices * queue depth, so leave @max_active at default.
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006373 */
6374 rbd_wq = alloc_workqueue(RBD_DRV_NAME, WQ_MEM_RECLAIM, 0);
6375 if (!rbd_wq) {
6376 rc = -ENOMEM;
6377 goto err_out_slab;
6378 }
6379
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006380 if (single_major) {
6381 rbd_major = register_blkdev(0, RBD_DRV_NAME);
6382 if (rbd_major < 0) {
6383 rc = rbd_major;
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006384 goto err_out_wq;
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006385 }
6386 }
6387
Alex Elder1c2a9df2013-05-01 12:43:03 -05006388 rc = rbd_sysfs_init();
6389 if (rc)
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006390 goto err_out_blkdev;
Alex Elder1c2a9df2013-05-01 12:43:03 -05006391
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006392 if (single_major)
6393 pr_info("loaded (major %d)\n", rbd_major);
6394 else
6395 pr_info("loaded\n");
6396
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006397 return 0;
6398
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006399err_out_blkdev:
6400 if (single_major)
6401 unregister_blkdev(rbd_major, RBD_DRV_NAME);
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006402err_out_wq:
6403 destroy_workqueue(rbd_wq);
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006404err_out_slab:
6405 rbd_slab_exit();
Alex Elder1c2a9df2013-05-01 12:43:03 -05006406 return rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006407}
6408
Alex Eldercc344fa2013-02-19 12:25:56 -06006409static void __exit rbd_exit(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006410{
Ilya Dryomovffe312c2014-05-20 15:46:04 +04006411 ida_destroy(&rbd_dev_id_ida);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006412 rbd_sysfs_cleanup();
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006413 if (single_major)
6414 unregister_blkdev(rbd_major, RBD_DRV_NAME);
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006415 destroy_workqueue(rbd_wq);
Alex Elder1c2a9df2013-05-01 12:43:03 -05006416 rbd_slab_exit();
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006417}
6418
6419module_init(rbd_init);
6420module_exit(rbd_exit);
6421
Alex Elderd552c612013-05-31 20:13:09 -05006422MODULE_AUTHOR("Alex Elder <elder@inktank.com>");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006423MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
6424MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006425/* following authorship retained from original osdblk.c */
6426MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
6427
Ilya Dryomov90da2582013-12-13 15:28:56 +02006428MODULE_DESCRIPTION("RADOS Block Device (RBD) driver");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006429MODULE_LICENSE("GPL");