blob: 695ef552aaf0555e5869c413514143d912eb26da [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 Elderecb4dc222013-04-26 09:43:47 -0500188 const char *pool_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500189
Alex Elderecb4dc222013-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 Elderecb4dc222013-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 Overstreet5341a622013-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 Overstreet5341a622013-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,
1512 atomic_read(&obj_request->kref.refcount));
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,
1521 atomic_read(&obj_request->kref.refcount));
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,
1528 atomic_read(&img_request->kref.refcount));
1529 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,
1539 atomic_read(&img_request->kref.refcount));
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 Dryomovbb873b5392016-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 Hellwig7ad18af2015-01-13 17:20:04 +01003968 if (rq->cmd_type != REQ_TYPE_FS) {
3969 dout("%s: non-fs request type %d\n", __func__,
3970 (int) rq->cmd_type);
3971 result = -EIO;
3972 goto err;
3973 }
3974
Mike Christiec2df40d2016-06-05 14:32:17 -05003975 if (req_op(rq) == REQ_OP_DISCARD)
Guangliang Zhao90e98c52014-04-01 22:22:16 +08003976 op_type = OBJ_OP_DISCARD;
Mike Christiec2df40d2016-06-05 14:32:17 -05003977 else if (req_op(rq) == REQ_OP_WRITE)
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08003978 op_type = OBJ_OP_WRITE;
3979 else
3980 op_type = OBJ_OP_READ;
3981
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04003982 /* Ignore/skip any zero-length requests */
3983
3984 if (!length) {
3985 dout("%s: zero-length request\n", __func__);
3986 result = 0;
3987 goto err_rq;
3988 }
3989
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08003990 /* Only reads are allowed to a read-only device */
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04003991
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08003992 if (op_type != OBJ_OP_READ) {
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04003993 if (rbd_dev->mapping.read_only) {
3994 result = -EROFS;
3995 goto err_rq;
3996 }
3997 rbd_assert(rbd_dev->spec->snap_id == CEPH_NOSNAP);
3998 }
3999
4000 /*
4001 * Quit early if the mapped snapshot no longer exists. It's
4002 * still possible the snapshot will have disappeared by the
4003 * time our request arrives at the osd, but there's no sense in
4004 * sending it if we already know.
4005 */
4006 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) {
4007 dout("request for non-existent snapshot");
4008 rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP);
4009 result = -ENXIO;
4010 goto err_rq;
4011 }
4012
4013 if (offset && length > U64_MAX - offset + 1) {
4014 rbd_warn(rbd_dev, "bad request range (%llu~%llu)", offset,
4015 length);
4016 result = -EINVAL;
4017 goto err_rq; /* Shouldn't happen */
4018 }
4019
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004020 blk_mq_start_request(rq);
4021
Josh Durgin4e752f02014-04-08 11:12:11 -07004022 down_read(&rbd_dev->header_rwsem);
4023 mapping_size = rbd_dev->mapping.size;
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004024 if (op_type != OBJ_OP_READ) {
Josh Durgin4e752f02014-04-08 11:12:11 -07004025 snapc = rbd_dev->header.snapc;
4026 ceph_get_snap_context(snapc);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004027 must_be_locked = rbd_is_lock_supported(rbd_dev);
Ilya Dryomov80de1912016-09-20 14:23:17 +02004028 } else {
4029 must_be_locked = rbd_dev->opts->lock_on_read &&
4030 rbd_is_lock_supported(rbd_dev);
Josh Durgin4e752f02014-04-08 11:12:11 -07004031 }
4032 up_read(&rbd_dev->header_rwsem);
4033
4034 if (offset + length > mapping_size) {
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004035 rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)", offset,
Josh Durgin4e752f02014-04-08 11:12:11 -07004036 length, mapping_size);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004037 result = -EIO;
4038 goto err_rq;
4039 }
4040
Ilya Dryomoved95b212016-08-12 16:40:02 +02004041 if (must_be_locked) {
4042 down_read(&rbd_dev->lock_rwsem);
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02004043 if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED &&
4044 !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags))
Ilya Dryomoved95b212016-08-12 16:40:02 +02004045 rbd_wait_state_locked(rbd_dev);
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02004046
4047 WARN_ON((rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) ^
4048 !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags));
4049 if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) {
4050 result = -EBLACKLISTED;
4051 goto err_unlock;
4052 }
Ilya Dryomoved95b212016-08-12 16:40:02 +02004053 }
4054
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004055 img_request = rbd_img_request_create(rbd_dev, offset, length, op_type,
Josh Durgin4e752f02014-04-08 11:12:11 -07004056 snapc);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004057 if (!img_request) {
4058 result = -ENOMEM;
Ilya Dryomoved95b212016-08-12 16:40:02 +02004059 goto err_unlock;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004060 }
4061 img_request->rq = rq;
Ilya Dryomov70b16db2015-11-27 19:23:24 +01004062 snapc = NULL; /* img_request consumes a ref */
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004063
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004064 if (op_type == OBJ_OP_DISCARD)
4065 result = rbd_img_request_fill(img_request, OBJ_REQUEST_NODATA,
4066 NULL);
4067 else
4068 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
4069 rq->bio);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004070 if (result)
4071 goto err_img_request;
4072
4073 result = rbd_img_request_submit(img_request);
4074 if (result)
4075 goto err_img_request;
4076
Ilya Dryomoved95b212016-08-12 16:40:02 +02004077 if (must_be_locked)
4078 up_read(&rbd_dev->lock_rwsem);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004079 return;
4080
4081err_img_request:
4082 rbd_img_request_put(img_request);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004083err_unlock:
4084 if (must_be_locked)
4085 up_read(&rbd_dev->lock_rwsem);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004086err_rq:
4087 if (result)
4088 rbd_warn(rbd_dev, "%s %llx at %llx result %d",
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004089 obj_op_name(op_type), length, offset, result);
SF Markus Elfringe96a6502014-11-02 15:20:59 +01004090 ceph_put_snap_context(snapc);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004091err:
4092 blk_mq_end_request(rq, result);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004093}
4094
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004095static int rbd_queue_rq(struct blk_mq_hw_ctx *hctx,
4096 const struct blk_mq_queue_data *bd)
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004097{
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004098 struct request *rq = bd->rq;
4099 struct work_struct *work = blk_mq_rq_to_pdu(rq);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004100
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004101 queue_work(rbd_wq, work);
4102 return BLK_MQ_RQ_QUEUE_OK;
Alex Elderbf0d5f502012-11-22 00:00:08 -06004103}
4104
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004105static void rbd_free_disk(struct rbd_device *rbd_dev)
4106{
4107 struct gendisk *disk = rbd_dev->disk;
4108
4109 if (!disk)
4110 return;
4111
Alex Eldera0cab922013-04-25 23:15:08 -05004112 rbd_dev->disk = NULL;
4113 if (disk->flags & GENHD_FL_UP) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004114 del_gendisk(disk);
Alex Eldera0cab922013-04-25 23:15:08 -05004115 if (disk->queue)
4116 blk_cleanup_queue(disk->queue);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004117 blk_mq_free_tag_set(&rbd_dev->tag_set);
Alex Eldera0cab922013-04-25 23:15:08 -05004118 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004119 put_disk(disk);
4120}
4121
Alex Elder788e2df2013-01-17 12:25:27 -06004122static int rbd_obj_read_sync(struct rbd_device *rbd_dev,
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004123 struct ceph_object_id *oid,
4124 struct ceph_object_locator *oloc,
4125 void *buf, int buf_len)
Alex Elder788e2df2013-01-17 12:25:27 -06004126
4127{
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004128 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
4129 struct ceph_osd_request *req;
4130 struct page **pages;
4131 int num_pages = calc_pages_for(0, buf_len);
Alex Elder788e2df2013-01-17 12:25:27 -06004132 int ret;
4133
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004134 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL);
4135 if (!req)
4136 return -ENOMEM;
Alex Elder788e2df2013-01-17 12:25:27 -06004137
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004138 ceph_oid_copy(&req->r_base_oid, oid);
4139 ceph_oloc_copy(&req->r_base_oloc, oloc);
4140 req->r_flags = CEPH_OSD_FLAG_READ;
Alex Elder788e2df2013-01-17 12:25:27 -06004141
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004142 ret = ceph_osdc_alloc_messages(req, GFP_KERNEL);
Alex Elder788e2df2013-01-17 12:25:27 -06004143 if (ret)
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004144 goto out_req;
Alex Elder788e2df2013-01-17 12:25:27 -06004145
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004146 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
4147 if (IS_ERR(pages)) {
4148 ret = PTR_ERR(pages);
4149 goto out_req;
4150 }
Alex Elder1ceae7e2013-02-06 13:11:38 -06004151
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004152 osd_req_op_extent_init(req, 0, CEPH_OSD_OP_READ, 0, buf_len, 0, 0);
4153 osd_req_op_extent_osd_data_pages(req, 0, pages, buf_len, 0, false,
4154 true);
Alex Elder788e2df2013-01-17 12:25:27 -06004155
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004156 ceph_osdc_start_request(osdc, req, false);
4157 ret = ceph_osdc_wait_request(osdc, req);
4158 if (ret >= 0)
4159 ceph_copy_from_page_vector(pages, buf, 0, ret);
4160
4161out_req:
4162 ceph_osdc_put_request(req);
Alex Elder788e2df2013-01-17 12:25:27 -06004163 return ret;
4164}
4165
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004166/*
Alex Elder662518b2013-05-06 09:51:29 -05004167 * Read the complete header for the given rbd device. On successful
4168 * return, the rbd_dev->header field will contain up-to-date
4169 * information about the image.
Alex Elder4156d992012-08-02 11:29:46 -05004170 */
Alex Elder99a41eb2013-05-06 09:51:30 -05004171static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev)
Alex Elder4156d992012-08-02 11:29:46 -05004172{
4173 struct rbd_image_header_ondisk *ondisk = NULL;
4174 u32 snap_count = 0;
4175 u64 names_size = 0;
4176 u32 want_count;
4177 int ret;
4178
4179 /*
4180 * The complete header will include an array of its 64-bit
4181 * snapshot ids, followed by the names of those snapshots as
4182 * a contiguous block of NUL-terminated strings. Note that
4183 * the number of snapshots could change by the time we read
4184 * it in, in which case we re-read it.
4185 */
4186 do {
4187 size_t size;
4188
4189 kfree(ondisk);
4190
4191 size = sizeof (*ondisk);
4192 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
4193 size += names_size;
4194 ondisk = kmalloc(size, GFP_KERNEL);
4195 if (!ondisk)
Alex Elder662518b2013-05-06 09:51:29 -05004196 return -ENOMEM;
Alex Elder4156d992012-08-02 11:29:46 -05004197
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004198 ret = rbd_obj_read_sync(rbd_dev, &rbd_dev->header_oid,
4199 &rbd_dev->header_oloc, ondisk, size);
Alex Elder4156d992012-08-02 11:29:46 -05004200 if (ret < 0)
Alex Elder662518b2013-05-06 09:51:29 -05004201 goto out;
Alex Elderc0cd10db2013-04-26 09:43:47 -05004202 if ((size_t)ret < size) {
Alex Elder4156d992012-08-02 11:29:46 -05004203 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05004204 rbd_warn(rbd_dev, "short header read (want %zd got %d)",
4205 size, ret);
Alex Elder662518b2013-05-06 09:51:29 -05004206 goto out;
Alex Elder4156d992012-08-02 11:29:46 -05004207 }
4208 if (!rbd_dev_ondisk_valid(ondisk)) {
4209 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05004210 rbd_warn(rbd_dev, "invalid header");
Alex Elder662518b2013-05-06 09:51:29 -05004211 goto out;
Alex Elder4156d992012-08-02 11:29:46 -05004212 }
4213
4214 names_size = le64_to_cpu(ondisk->snap_names_len);
4215 want_count = snap_count;
4216 snap_count = le32_to_cpu(ondisk->snap_count);
4217 } while (snap_count != want_count);
4218
Alex Elder662518b2013-05-06 09:51:29 -05004219 ret = rbd_header_from_disk(rbd_dev, ondisk);
4220out:
Alex Elder4156d992012-08-02 11:29:46 -05004221 kfree(ondisk);
4222
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004223 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004224}
4225
Alex Elder15228ed2013-05-01 12:43:03 -05004226/*
4227 * Clear the rbd device's EXISTS flag if the snapshot it's mapped to
4228 * has disappeared from the (just updated) snapshot context.
4229 */
4230static void rbd_exists_validate(struct rbd_device *rbd_dev)
4231{
4232 u64 snap_id;
4233
4234 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags))
4235 return;
4236
4237 snap_id = rbd_dev->spec->snap_id;
4238 if (snap_id == CEPH_NOSNAP)
4239 return;
4240
4241 if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX)
4242 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
4243}
4244
Josh Durgin98752012013-08-29 17:26:31 -07004245static void rbd_dev_update_size(struct rbd_device *rbd_dev)
4246{
4247 sector_t size;
Josh Durgin98752012013-08-29 17:26:31 -07004248
4249 /*
Ilya Dryomov811c6682016-04-15 16:22:16 +02004250 * If EXISTS is not set, rbd_dev->disk may be NULL, so don't
4251 * try to update its size. If REMOVING is set, updating size
4252 * is just useless work since the device can't be opened.
Josh Durgin98752012013-08-29 17:26:31 -07004253 */
Ilya Dryomov811c6682016-04-15 16:22:16 +02004254 if (test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags) &&
4255 !test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) {
Josh Durgin98752012013-08-29 17:26:31 -07004256 size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE;
4257 dout("setting size to %llu sectors", (unsigned long long)size);
4258 set_capacity(rbd_dev->disk, size);
4259 revalidate_disk(rbd_dev->disk);
4260 }
4261}
4262
Alex Eldercc4a38bd2013-04-30 00:44:33 -05004263static int rbd_dev_refresh(struct rbd_device *rbd_dev)
Alex Elder1fe5e992012-07-25 09:32:41 -05004264{
Alex Eldere627db02013-05-06 07:40:30 -05004265 u64 mapping_size;
Alex Elder1fe5e992012-07-25 09:32:41 -05004266 int ret;
4267
Alex Eldercfbf6372013-05-31 17:40:45 -05004268 down_write(&rbd_dev->header_rwsem);
Alex Elder3b5cf2a2013-05-29 11:18:59 -05004269 mapping_size = rbd_dev->mapping.size;
Ilya Dryomova720ae02014-07-23 17:11:19 +04004270
4271 ret = rbd_dev_header_info(rbd_dev);
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04004272 if (ret)
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004273 goto out;
Alex Elder15228ed2013-05-01 12:43:03 -05004274
Ilya Dryomove8f59b52014-07-24 10:42:13 +04004275 /*
4276 * If there is a parent, see if it has disappeared due to the
4277 * mapped image getting flattened.
4278 */
4279 if (rbd_dev->parent) {
4280 ret = rbd_dev_v2_parent_info(rbd_dev);
4281 if (ret)
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004282 goto out;
Ilya Dryomove8f59b52014-07-24 10:42:13 +04004283 }
4284
Ilya Dryomov5ff11082014-07-23 17:11:21 +04004285 if (rbd_dev->spec->snap_id == CEPH_NOSNAP) {
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004286 rbd_dev->mapping.size = rbd_dev->header.image_size;
Ilya Dryomov5ff11082014-07-23 17:11:21 +04004287 } else {
4288 /* validate mapped snapshot's EXISTS flag */
4289 rbd_exists_validate(rbd_dev);
4290 }
Alex Elder15228ed2013-05-01 12:43:03 -05004291
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004292out:
Alex Eldercfbf6372013-05-31 17:40:45 -05004293 up_write(&rbd_dev->header_rwsem);
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004294 if (!ret && mapping_size != rbd_dev->mapping.size)
Josh Durgin98752012013-08-29 17:26:31 -07004295 rbd_dev_update_size(rbd_dev);
Alex Elder1fe5e992012-07-25 09:32:41 -05004296
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004297 return ret;
Alex Elder1fe5e992012-07-25 09:32:41 -05004298}
4299
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004300static int rbd_init_request(void *data, struct request *rq,
4301 unsigned int hctx_idx, unsigned int request_idx,
4302 unsigned int numa_node)
4303{
4304 struct work_struct *work = blk_mq_rq_to_pdu(rq);
4305
4306 INIT_WORK(work, rbd_queue_workfn);
4307 return 0;
4308}
4309
4310static struct blk_mq_ops rbd_mq_ops = {
4311 .queue_rq = rbd_queue_rq,
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004312 .init_request = rbd_init_request,
4313};
4314
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004315static int rbd_init_disk(struct rbd_device *rbd_dev)
4316{
4317 struct gendisk *disk;
4318 struct request_queue *q;
Alex Elder593a9e72012-02-07 12:03:37 -06004319 u64 segment_size;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004320 int err;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004321
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004322 /* create gendisk info */
Ilya Dryomov7e513d42013-12-16 19:26:32 +02004323 disk = alloc_disk(single_major ?
4324 (1 << RBD_SINGLE_MAJOR_PART_SHIFT) :
4325 RBD_MINORS_PER_MAJOR);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004326 if (!disk)
Alex Elder1fcdb8a2012-08-29 17:11:06 -05004327 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004328
Alex Elderf0f8cef2012-01-29 13:57:44 -06004329 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
Alex Elderde71a292012-07-03 16:01:19 -05004330 rbd_dev->dev_id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004331 disk->major = rbd_dev->major;
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004332 disk->first_minor = rbd_dev->minor;
Ilya Dryomov7e513d42013-12-16 19:26:32 +02004333 if (single_major)
4334 disk->flags |= GENHD_FL_EXT_DEVT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004335 disk->fops = &rbd_bd_ops;
4336 disk->private_data = rbd_dev;
4337
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004338 memset(&rbd_dev->tag_set, 0, sizeof(rbd_dev->tag_set));
4339 rbd_dev->tag_set.ops = &rbd_mq_ops;
Ilya Dryomovb5584182015-06-23 16:21:19 +03004340 rbd_dev->tag_set.queue_depth = rbd_dev->opts->queue_depth;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004341 rbd_dev->tag_set.numa_node = NUMA_NO_NODE;
Ilya Dryomovb5584182015-06-23 16:21:19 +03004342 rbd_dev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004343 rbd_dev->tag_set.nr_hw_queues = 1;
4344 rbd_dev->tag_set.cmd_size = sizeof(struct work_struct);
4345
4346 err = blk_mq_alloc_tag_set(&rbd_dev->tag_set);
4347 if (err)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004348 goto out_disk;
Josh Durgin029bcbd2011-07-22 11:35:23 -07004349
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004350 q = blk_mq_init_queue(&rbd_dev->tag_set);
4351 if (IS_ERR(q)) {
4352 err = PTR_ERR(q);
4353 goto out_tag_set;
4354 }
4355
Ilya Dryomovd8a2c892015-03-24 16:15:17 +03004356 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
4357 /* QUEUE_FLAG_ADD_RANDOM is off by default for blk-mq */
Alex Elder593a9e72012-02-07 12:03:37 -06004358
Josh Durgin029bcbd2011-07-22 11:35:23 -07004359 /* set io sizes to object size */
Alex Elder593a9e72012-02-07 12:03:37 -06004360 segment_size = rbd_obj_bytes(&rbd_dev->header);
4361 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
Ilya Dryomov0d9fde42015-10-07 16:09:35 +02004362 q->limits.max_sectors = queue_max_hw_sectors(q);
Ilya Dryomovd3834fe2015-06-12 19:19:02 +03004363 blk_queue_max_segments(q, segment_size / SECTOR_SIZE);
Alex Elder593a9e72012-02-07 12:03:37 -06004364 blk_queue_max_segment_size(q, segment_size);
4365 blk_queue_io_min(q, segment_size);
4366 blk_queue_io_opt(q, segment_size);
Josh Durgin029bcbd2011-07-22 11:35:23 -07004367
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004368 /* enable the discard support */
4369 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
4370 q->limits.discard_granularity = segment_size;
4371 q->limits.discard_alignment = segment_size;
Jens Axboe2bb4cd52015-07-14 08:15:12 -06004372 blk_queue_max_discard_sectors(q, segment_size / SECTOR_SIZE);
Josh Durginb76f8232014-04-07 16:52:03 -07004373 q->limits.discard_zeroes_data = 1;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004374
Ronny Hegewaldbae818e2015-10-15 18:50:46 +00004375 if (!ceph_test_opt(rbd_dev->rbd_client->client, NOCRC))
4376 q->backing_dev_info.capabilities |= BDI_CAP_STABLE_WRITES;
4377
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004378 disk->queue = q;
4379
4380 q->queuedata = rbd_dev;
4381
4382 rbd_dev->disk = disk;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004383
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004384 return 0;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004385out_tag_set:
4386 blk_mq_free_tag_set(&rbd_dev->tag_set);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004387out_disk:
4388 put_disk(disk);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004389 return err;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004390}
4391
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004392/*
4393 sysfs
4394*/
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004395
Alex Elder593a9e72012-02-07 12:03:37 -06004396static struct rbd_device *dev_to_rbd_dev(struct device *dev)
4397{
4398 return container_of(dev, struct rbd_device, dev);
4399}
4400
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004401static ssize_t rbd_size_show(struct device *dev,
4402 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004403{
Alex Elder593a9e72012-02-07 12:03:37 -06004404 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004405
Alex Elderfc71d832013-04-26 15:44:36 -05004406 return sprintf(buf, "%llu\n",
4407 (unsigned long long)rbd_dev->mapping.size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004408}
4409
Alex Elder34b13182012-07-13 20:35:12 -05004410/*
4411 * Note this shows the features for whatever's mapped, which is not
4412 * necessarily the base image.
4413 */
4414static ssize_t rbd_features_show(struct device *dev,
4415 struct device_attribute *attr, char *buf)
4416{
4417 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4418
4419 return sprintf(buf, "0x%016llx\n",
Alex Elderfc71d832013-04-26 15:44:36 -05004420 (unsigned long long)rbd_dev->mapping.features);
Alex Elder34b13182012-07-13 20:35:12 -05004421}
4422
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004423static ssize_t rbd_major_show(struct device *dev,
4424 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004425{
Alex Elder593a9e72012-02-07 12:03:37 -06004426 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004427
Alex Elderfc71d832013-04-26 15:44:36 -05004428 if (rbd_dev->major)
4429 return sprintf(buf, "%d\n", rbd_dev->major);
4430
4431 return sprintf(buf, "(none)\n");
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004432}
Alex Elderfc71d832013-04-26 15:44:36 -05004433
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004434static ssize_t rbd_minor_show(struct device *dev,
4435 struct device_attribute *attr, char *buf)
4436{
4437 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4438
4439 return sprintf(buf, "%d\n", rbd_dev->minor);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004440}
4441
Ilya Dryomov005a07bf2016-08-18 18:38:43 +02004442static ssize_t rbd_client_addr_show(struct device *dev,
4443 struct device_attribute *attr, char *buf)
4444{
4445 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4446 struct ceph_entity_addr *client_addr =
4447 ceph_client_addr(rbd_dev->rbd_client->client);
4448
4449 return sprintf(buf, "%pISpc/%u\n", &client_addr->in_addr,
4450 le32_to_cpu(client_addr->nonce));
4451}
4452
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004453static ssize_t rbd_client_id_show(struct device *dev,
4454 struct device_attribute *attr, char *buf)
4455{
Alex Elder593a9e72012-02-07 12:03:37 -06004456 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004457
Alex Elder1dbb4392012-01-24 10:08:37 -06004458 return sprintf(buf, "client%lld\n",
Ilya Dryomov033268a2016-08-12 14:59:58 +02004459 ceph_client_gid(rbd_dev->rbd_client->client));
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004460}
4461
Mike Christie267fb902016-08-18 18:38:43 +02004462static ssize_t rbd_cluster_fsid_show(struct device *dev,
4463 struct device_attribute *attr, char *buf)
4464{
4465 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4466
4467 return sprintf(buf, "%pU\n", &rbd_dev->rbd_client->client->fsid);
4468}
4469
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02004470static ssize_t rbd_config_info_show(struct device *dev,
4471 struct device_attribute *attr, char *buf)
4472{
4473 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4474
4475 return sprintf(buf, "%s\n", rbd_dev->config_info);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004476}
4477
4478static ssize_t rbd_pool_show(struct device *dev,
4479 struct device_attribute *attr, char *buf)
4480{
Alex Elder593a9e72012-02-07 12:03:37 -06004481 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004482
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004483 return sprintf(buf, "%s\n", rbd_dev->spec->pool_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004484}
4485
Alex Elder9bb2f332012-07-12 10:46:35 -05004486static ssize_t rbd_pool_id_show(struct device *dev,
4487 struct device_attribute *attr, char *buf)
4488{
4489 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4490
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004491 return sprintf(buf, "%llu\n",
Alex Elderfc71d832013-04-26 15:44:36 -05004492 (unsigned long long) rbd_dev->spec->pool_id);
Alex Elder9bb2f332012-07-12 10:46:35 -05004493}
4494
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004495static ssize_t rbd_name_show(struct device *dev,
4496 struct device_attribute *attr, char *buf)
4497{
Alex Elder593a9e72012-02-07 12:03:37 -06004498 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004499
Alex Eldera92ffdf2012-10-30 19:40:33 -05004500 if (rbd_dev->spec->image_name)
4501 return sprintf(buf, "%s\n", rbd_dev->spec->image_name);
4502
4503 return sprintf(buf, "(unknown)\n");
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004504}
4505
Alex Elder589d30e2012-07-10 20:30:11 -05004506static ssize_t rbd_image_id_show(struct device *dev,
4507 struct device_attribute *attr, char *buf)
4508{
4509 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4510
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004511 return sprintf(buf, "%s\n", rbd_dev->spec->image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05004512}
4513
Alex Elder34b13182012-07-13 20:35:12 -05004514/*
4515 * Shows the name of the currently-mapped snapshot (or
4516 * RBD_SNAP_HEAD_NAME for the base image).
4517 */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004518static ssize_t rbd_snap_show(struct device *dev,
4519 struct device_attribute *attr,
4520 char *buf)
4521{
Alex Elder593a9e72012-02-07 12:03:37 -06004522 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004523
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004524 return sprintf(buf, "%s\n", rbd_dev->spec->snap_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004525}
4526
Mike Christie92a58672016-08-18 18:38:44 +02004527static ssize_t rbd_snap_id_show(struct device *dev,
4528 struct device_attribute *attr, char *buf)
4529{
4530 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4531
4532 return sprintf(buf, "%llu\n", rbd_dev->spec->snap_id);
4533}
4534
Alex Elder86b00e02012-10-25 23:34:42 -05004535/*
Ilya Dryomovff961282014-07-22 21:53:07 +04004536 * For a v2 image, shows the chain of parent images, separated by empty
4537 * lines. For v1 images or if there is no parent, shows "(no parent
4538 * image)".
Alex Elder86b00e02012-10-25 23:34:42 -05004539 */
4540static ssize_t rbd_parent_show(struct device *dev,
Ilya Dryomovff961282014-07-22 21:53:07 +04004541 struct device_attribute *attr,
4542 char *buf)
Alex Elder86b00e02012-10-25 23:34:42 -05004543{
4544 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Ilya Dryomovff961282014-07-22 21:53:07 +04004545 ssize_t count = 0;
Alex Elder86b00e02012-10-25 23:34:42 -05004546
Ilya Dryomovff961282014-07-22 21:53:07 +04004547 if (!rbd_dev->parent)
Alex Elder86b00e02012-10-25 23:34:42 -05004548 return sprintf(buf, "(no parent image)\n");
4549
Ilya Dryomovff961282014-07-22 21:53:07 +04004550 for ( ; rbd_dev->parent; rbd_dev = rbd_dev->parent) {
4551 struct rbd_spec *spec = rbd_dev->parent_spec;
Alex Elder86b00e02012-10-25 23:34:42 -05004552
Ilya Dryomovff961282014-07-22 21:53:07 +04004553 count += sprintf(&buf[count], "%s"
4554 "pool_id %llu\npool_name %s\n"
4555 "image_id %s\nimage_name %s\n"
4556 "snap_id %llu\nsnap_name %s\n"
4557 "overlap %llu\n",
4558 !count ? "" : "\n", /* first? */
4559 spec->pool_id, spec->pool_name,
4560 spec->image_id, spec->image_name ?: "(unknown)",
4561 spec->snap_id, spec->snap_name,
4562 rbd_dev->parent_overlap);
4563 }
Alex Elder86b00e02012-10-25 23:34:42 -05004564
Ilya Dryomovff961282014-07-22 21:53:07 +04004565 return count;
Alex Elder86b00e02012-10-25 23:34:42 -05004566}
4567
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004568static ssize_t rbd_image_refresh(struct device *dev,
4569 struct device_attribute *attr,
4570 const char *buf,
4571 size_t size)
4572{
Alex Elder593a9e72012-02-07 12:03:37 -06004573 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Alex Elderb8136232012-07-25 09:32:41 -05004574 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004575
Alex Eldercc4a38bd2013-04-30 00:44:33 -05004576 ret = rbd_dev_refresh(rbd_dev);
Alex Eldere627db02013-05-06 07:40:30 -05004577 if (ret)
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04004578 return ret;
Alex Elderb8136232012-07-25 09:32:41 -05004579
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04004580 return size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004581}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004582
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004583static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
Alex Elder34b13182012-07-13 20:35:12 -05004584static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004585static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004586static DEVICE_ATTR(minor, S_IRUGO, rbd_minor_show, NULL);
Ilya Dryomov005a07bf2016-08-18 18:38:43 +02004587static DEVICE_ATTR(client_addr, S_IRUGO, rbd_client_addr_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004588static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
Mike Christie267fb902016-08-18 18:38:43 +02004589static DEVICE_ATTR(cluster_fsid, S_IRUGO, rbd_cluster_fsid_show, NULL);
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02004590static DEVICE_ATTR(config_info, S_IRUSR, rbd_config_info_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004591static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
Alex Elder9bb2f332012-07-12 10:46:35 -05004592static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004593static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
Alex Elder589d30e2012-07-10 20:30:11 -05004594static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004595static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
4596static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
Mike Christie92a58672016-08-18 18:38:44 +02004597static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL);
Alex Elder86b00e02012-10-25 23:34:42 -05004598static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004599
4600static struct attribute *rbd_attrs[] = {
4601 &dev_attr_size.attr,
Alex Elder34b13182012-07-13 20:35:12 -05004602 &dev_attr_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004603 &dev_attr_major.attr,
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004604 &dev_attr_minor.attr,
Ilya Dryomov005a07bf2016-08-18 18:38:43 +02004605 &dev_attr_client_addr.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004606 &dev_attr_client_id.attr,
Mike Christie267fb902016-08-18 18:38:43 +02004607 &dev_attr_cluster_fsid.attr,
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02004608 &dev_attr_config_info.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004609 &dev_attr_pool.attr,
Alex Elder9bb2f332012-07-12 10:46:35 -05004610 &dev_attr_pool_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004611 &dev_attr_name.attr,
Alex Elder589d30e2012-07-10 20:30:11 -05004612 &dev_attr_image_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004613 &dev_attr_current_snap.attr,
Mike Christie92a58672016-08-18 18:38:44 +02004614 &dev_attr_snap_id.attr,
Alex Elder86b00e02012-10-25 23:34:42 -05004615 &dev_attr_parent.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004616 &dev_attr_refresh.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004617 NULL
4618};
4619
4620static struct attribute_group rbd_attr_group = {
4621 .attrs = rbd_attrs,
4622};
4623
4624static const struct attribute_group *rbd_attr_groups[] = {
4625 &rbd_attr_group,
4626 NULL
4627};
4628
Ilya Dryomov6cac4692015-10-16 20:11:25 +02004629static void rbd_dev_release(struct device *dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004630
Bhumika Goyalb9942bc2017-02-11 12:14:38 +05304631static const struct device_type rbd_device_type = {
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004632 .name = "rbd",
4633 .groups = rbd_attr_groups,
Ilya Dryomov6cac4692015-10-16 20:11:25 +02004634 .release = rbd_dev_release,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004635};
4636
Alex Elder8b8fb992012-10-26 17:25:24 -05004637static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)
4638{
4639 kref_get(&spec->kref);
4640
4641 return spec;
4642}
4643
4644static void rbd_spec_free(struct kref *kref);
4645static void rbd_spec_put(struct rbd_spec *spec)
4646{
4647 if (spec)
4648 kref_put(&spec->kref, rbd_spec_free);
4649}
4650
4651static struct rbd_spec *rbd_spec_alloc(void)
4652{
4653 struct rbd_spec *spec;
4654
4655 spec = kzalloc(sizeof (*spec), GFP_KERNEL);
4656 if (!spec)
4657 return NULL;
Ilya Dryomov04077592014-07-23 17:11:20 +04004658
4659 spec->pool_id = CEPH_NOPOOL;
4660 spec->snap_id = CEPH_NOSNAP;
Alex Elder8b8fb992012-10-26 17:25:24 -05004661 kref_init(&spec->kref);
4662
Alex Elder8b8fb992012-10-26 17:25:24 -05004663 return spec;
4664}
4665
4666static void rbd_spec_free(struct kref *kref)
4667{
4668 struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref);
4669
4670 kfree(spec->pool_name);
4671 kfree(spec->image_id);
4672 kfree(spec->image_name);
4673 kfree(spec->snap_name);
4674 kfree(spec);
4675}
4676
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004677static void rbd_dev_free(struct rbd_device *rbd_dev)
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004678{
Ilya Dryomov99d16942016-08-12 16:11:41 +02004679 WARN_ON(rbd_dev->watch_state != RBD_WATCH_STATE_UNREGISTERED);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004680 WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_UNLOCKED);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004681
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004682 ceph_oid_destroy(&rbd_dev->header_oid);
Ilya Dryomov6b6dddb2016-08-05 16:15:38 +02004683 ceph_oloc_destroy(&rbd_dev->header_oloc);
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02004684 kfree(rbd_dev->config_info);
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004685
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004686 rbd_put_client(rbd_dev->rbd_client);
4687 rbd_spec_put(rbd_dev->spec);
4688 kfree(rbd_dev->opts);
4689 kfree(rbd_dev);
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004690}
4691
4692static void rbd_dev_release(struct device *dev)
4693{
4694 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4695 bool need_put = !!rbd_dev->opts;
4696
4697 if (need_put) {
4698 destroy_workqueue(rbd_dev->task_wq);
4699 ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id);
4700 }
4701
4702 rbd_dev_free(rbd_dev);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004703
4704 /*
4705 * This is racy, but way better than putting module outside of
4706 * the release callback. The race window is pretty small, so
4707 * doing something similar to dm (dm-builtin.c) is overkill.
4708 */
4709 if (need_put)
4710 module_put(THIS_MODULE);
4711}
4712
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004713static struct rbd_device *__rbd_dev_create(struct rbd_client *rbdc,
4714 struct rbd_spec *spec)
Alex Elderc53d5892012-10-25 23:34:42 -05004715{
4716 struct rbd_device *rbd_dev;
4717
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004718 rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL);
Alex Elderc53d5892012-10-25 23:34:42 -05004719 if (!rbd_dev)
4720 return NULL;
4721
4722 spin_lock_init(&rbd_dev->lock);
4723 INIT_LIST_HEAD(&rbd_dev->node);
Alex Elderc53d5892012-10-25 23:34:42 -05004724 init_rwsem(&rbd_dev->header_rwsem);
4725
Ilya Dryomov7e973322017-01-25 18:16:22 +01004726 rbd_dev->header.data_pool_id = CEPH_NOPOOL;
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004727 ceph_oid_init(&rbd_dev->header_oid);
Ilya Dryomov431a02c2017-01-25 18:16:21 +01004728 rbd_dev->header_oloc.pool = spec->pool_id;
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004729
Ilya Dryomov99d16942016-08-12 16:11:41 +02004730 mutex_init(&rbd_dev->watch_mutex);
4731 rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED;
4732 INIT_DELAYED_WORK(&rbd_dev->watch_dwork, rbd_reregister_watch);
4733
Ilya Dryomoved95b212016-08-12 16:40:02 +02004734 init_rwsem(&rbd_dev->lock_rwsem);
4735 rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED;
4736 INIT_WORK(&rbd_dev->acquired_lock_work, rbd_notify_acquired_lock);
4737 INIT_WORK(&rbd_dev->released_lock_work, rbd_notify_released_lock);
4738 INIT_DELAYED_WORK(&rbd_dev->lock_dwork, rbd_acquire_lock);
4739 INIT_WORK(&rbd_dev->unlock_work, rbd_release_lock_work);
4740 init_waitqueue_head(&rbd_dev->lock_waitq);
4741
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004742 rbd_dev->dev.bus = &rbd_bus_type;
4743 rbd_dev->dev.type = &rbd_device_type;
4744 rbd_dev->dev.parent = &rbd_root_dev;
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004745 device_initialize(&rbd_dev->dev);
4746
Alex Elderc53d5892012-10-25 23:34:42 -05004747 rbd_dev->rbd_client = rbdc;
Ilya Dryomovd1475432015-06-22 13:24:48 +03004748 rbd_dev->spec = spec;
Alex Elder0903e872012-11-14 12:25:19 -06004749
Alex Elderc53d5892012-10-25 23:34:42 -05004750 return rbd_dev;
4751}
4752
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004753/*
4754 * Create a mapping rbd_dev.
4755 */
4756static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,
4757 struct rbd_spec *spec,
4758 struct rbd_options *opts)
4759{
4760 struct rbd_device *rbd_dev;
4761
4762 rbd_dev = __rbd_dev_create(rbdc, spec);
4763 if (!rbd_dev)
4764 return NULL;
4765
4766 rbd_dev->opts = opts;
4767
4768 /* get an id and fill in device name */
4769 rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0,
4770 minor_to_rbd_dev_id(1 << MINORBITS),
4771 GFP_KERNEL);
4772 if (rbd_dev->dev_id < 0)
4773 goto fail_rbd_dev;
4774
4775 sprintf(rbd_dev->name, RBD_DRV_NAME "%d", rbd_dev->dev_id);
4776 rbd_dev->task_wq = alloc_ordered_workqueue("%s-tasks", WQ_MEM_RECLAIM,
4777 rbd_dev->name);
4778 if (!rbd_dev->task_wq)
4779 goto fail_dev_id;
4780
4781 /* we have a ref from do_rbd_add() */
4782 __module_get(THIS_MODULE);
4783
4784 dout("%s rbd_dev %p dev_id %d\n", __func__, rbd_dev, rbd_dev->dev_id);
4785 return rbd_dev;
4786
4787fail_dev_id:
4788 ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id);
4789fail_rbd_dev:
4790 rbd_dev_free(rbd_dev);
4791 return NULL;
4792}
4793
Alex Elderc53d5892012-10-25 23:34:42 -05004794static void rbd_dev_destroy(struct rbd_device *rbd_dev)
4795{
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004796 if (rbd_dev)
4797 put_device(&rbd_dev->dev);
Alex Elderc53d5892012-10-25 23:34:42 -05004798}
4799
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004800/*
Alex Elder9d475de2012-07-03 16:01:19 -05004801 * Get the size and object order for an image snapshot, or if
4802 * snap_id is CEPH_NOSNAP, gets this information for the base
4803 * image.
4804 */
4805static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
4806 u8 *order, u64 *snap_size)
4807{
4808 __le64 snapid = cpu_to_le64(snap_id);
4809 int ret;
4810 struct {
4811 u8 order;
4812 __le64 size;
4813 } __attribute__ ((packed)) size_buf = { 0 };
4814
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004815 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
4816 &rbd_dev->header_oloc, "get_size",
4817 &snapid, sizeof(snapid),
4818 &size_buf, sizeof(size_buf));
Alex Elder36be9a72013-01-19 00:30:28 -06004819 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder9d475de2012-07-03 16:01:19 -05004820 if (ret < 0)
4821 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05004822 if (ret < sizeof (size_buf))
4823 return -ERANGE;
Alex Elder9d475de2012-07-03 16:01:19 -05004824
Josh Durginc3545572013-08-28 17:08:10 -07004825 if (order) {
Alex Elderc86f86e2013-04-25 15:09:41 -05004826 *order = size_buf.order;
Josh Durginc3545572013-08-28 17:08:10 -07004827 dout(" order %u", (unsigned int)*order);
4828 }
Alex Elder9d475de2012-07-03 16:01:19 -05004829 *snap_size = le64_to_cpu(size_buf.size);
4830
Josh Durginc3545572013-08-28 17:08:10 -07004831 dout(" snap_id 0x%016llx snap_size = %llu\n",
4832 (unsigned long long)snap_id,
Alex Elder57385b52013-04-21 12:14:45 -05004833 (unsigned long long)*snap_size);
Alex Elder9d475de2012-07-03 16:01:19 -05004834
4835 return 0;
4836}
4837
4838static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
4839{
4840 return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
4841 &rbd_dev->header.obj_order,
4842 &rbd_dev->header.image_size);
4843}
4844
Alex Elder1e130192012-07-03 16:01:19 -05004845static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
4846{
4847 void *reply_buf;
4848 int ret;
4849 void *p;
4850
4851 reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL);
4852 if (!reply_buf)
4853 return -ENOMEM;
4854
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004855 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
4856 &rbd_dev->header_oloc, "get_object_prefix",
4857 NULL, 0, reply_buf, RBD_OBJ_PREFIX_LEN_MAX);
Alex Elder36be9a72013-01-19 00:30:28 -06004858 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder1e130192012-07-03 16:01:19 -05004859 if (ret < 0)
4860 goto out;
4861
4862 p = reply_buf;
4863 rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p,
Alex Elder57385b52013-04-21 12:14:45 -05004864 p + ret, NULL, GFP_NOIO);
4865 ret = 0;
Alex Elder1e130192012-07-03 16:01:19 -05004866
4867 if (IS_ERR(rbd_dev->header.object_prefix)) {
4868 ret = PTR_ERR(rbd_dev->header.object_prefix);
4869 rbd_dev->header.object_prefix = NULL;
4870 } else {
4871 dout(" object_prefix = %s\n", rbd_dev->header.object_prefix);
4872 }
Alex Elder1e130192012-07-03 16:01:19 -05004873out:
4874 kfree(reply_buf);
4875
4876 return ret;
4877}
4878
Alex Elderb1b54022012-07-03 16:01:19 -05004879static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
4880 u64 *snap_features)
4881{
4882 __le64 snapid = cpu_to_le64(snap_id);
4883 struct {
4884 __le64 features;
4885 __le64 incompat;
Alex Elder41579762013-04-21 12:14:45 -05004886 } __attribute__ ((packed)) features_buf = { 0 };
Ilya Dryomovd3767f02016-04-13 14:15:50 +02004887 u64 unsup;
Alex Elderb1b54022012-07-03 16:01:19 -05004888 int ret;
4889
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004890 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
4891 &rbd_dev->header_oloc, "get_features",
4892 &snapid, sizeof(snapid),
4893 &features_buf, sizeof(features_buf));
Alex Elder36be9a72013-01-19 00:30:28 -06004894 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderb1b54022012-07-03 16:01:19 -05004895 if (ret < 0)
4896 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05004897 if (ret < sizeof (features_buf))
4898 return -ERANGE;
Alex Elderd8891402012-10-09 13:50:17 -07004899
Ilya Dryomovd3767f02016-04-13 14:15:50 +02004900 unsup = le64_to_cpu(features_buf.incompat) & ~RBD_FEATURES_SUPPORTED;
4901 if (unsup) {
4902 rbd_warn(rbd_dev, "image uses unsupported features: 0x%llx",
4903 unsup);
Alex Elderb8f5c6e2012-11-01 08:39:26 -05004904 return -ENXIO;
Ilya Dryomovd3767f02016-04-13 14:15:50 +02004905 }
Alex Elderd8891402012-10-09 13:50:17 -07004906
Alex Elderb1b54022012-07-03 16:01:19 -05004907 *snap_features = le64_to_cpu(features_buf.features);
4908
4909 dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
Alex Elder57385b52013-04-21 12:14:45 -05004910 (unsigned long long)snap_id,
4911 (unsigned long long)*snap_features,
4912 (unsigned long long)le64_to_cpu(features_buf.incompat));
Alex Elderb1b54022012-07-03 16:01:19 -05004913
4914 return 0;
4915}
4916
4917static int rbd_dev_v2_features(struct rbd_device *rbd_dev)
4918{
4919 return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP,
4920 &rbd_dev->header.features);
4921}
4922
Alex Elder86b00e02012-10-25 23:34:42 -05004923static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev)
4924{
4925 struct rbd_spec *parent_spec;
4926 size_t size;
4927 void *reply_buf = NULL;
4928 __le64 snapid;
4929 void *p;
4930 void *end;
Alex Elder642a2532013-05-06 17:40:33 -05004931 u64 pool_id;
Alex Elder86b00e02012-10-25 23:34:42 -05004932 char *image_id;
Alex Elder3b5cf2a2013-05-29 11:18:59 -05004933 u64 snap_id;
Alex Elder86b00e02012-10-25 23:34:42 -05004934 u64 overlap;
Alex Elder86b00e02012-10-25 23:34:42 -05004935 int ret;
4936
4937 parent_spec = rbd_spec_alloc();
4938 if (!parent_spec)
4939 return -ENOMEM;
4940
4941 size = sizeof (__le64) + /* pool_id */
4942 sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */
4943 sizeof (__le64) + /* snap_id */
4944 sizeof (__le64); /* overlap */
4945 reply_buf = kmalloc(size, GFP_KERNEL);
4946 if (!reply_buf) {
4947 ret = -ENOMEM;
4948 goto out_err;
4949 }
4950
Ilya Dryomov4d9b67c2014-07-24 10:42:13 +04004951 snapid = cpu_to_le64(rbd_dev->spec->snap_id);
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004952 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
4953 &rbd_dev->header_oloc, "get_parent",
4954 &snapid, sizeof(snapid), reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06004955 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder86b00e02012-10-25 23:34:42 -05004956 if (ret < 0)
4957 goto out_err;
4958
Alex Elder86b00e02012-10-25 23:34:42 -05004959 p = reply_buf;
Alex Elder57385b52013-04-21 12:14:45 -05004960 end = reply_buf + ret;
4961 ret = -ERANGE;
Alex Elder642a2532013-05-06 17:40:33 -05004962 ceph_decode_64_safe(&p, end, pool_id, out_err);
Alex Elder392a9da2013-05-06 17:40:33 -05004963 if (pool_id == CEPH_NOPOOL) {
4964 /*
4965 * Either the parent never existed, or we have
4966 * record of it but the image got flattened so it no
4967 * longer has a parent. When the parent of a
4968 * layered image disappears we immediately set the
4969 * overlap to 0. The effect of this is that all new
4970 * requests will be treated as if the image had no
4971 * parent.
4972 */
4973 if (rbd_dev->parent_overlap) {
4974 rbd_dev->parent_overlap = 0;
Alex Elder392a9da2013-05-06 17:40:33 -05004975 rbd_dev_parent_put(rbd_dev);
4976 pr_info("%s: clone image has been flattened\n",
4977 rbd_dev->disk->disk_name);
4978 }
4979
Alex Elder86b00e02012-10-25 23:34:42 -05004980 goto out; /* No parent? No problem. */
Alex Elder392a9da2013-05-06 17:40:33 -05004981 }
Alex Elder86b00e02012-10-25 23:34:42 -05004982
Alex Elder0903e872012-11-14 12:25:19 -06004983 /* The ceph file layout needs to fit pool id in 32 bits */
4984
4985 ret = -EIO;
Alex Elder642a2532013-05-06 17:40:33 -05004986 if (pool_id > (u64)U32_MAX) {
Ilya Dryomov9584d502014-07-11 12:11:20 +04004987 rbd_warn(NULL, "parent pool id too large (%llu > %u)",
Alex Elder642a2532013-05-06 17:40:33 -05004988 (unsigned long long)pool_id, U32_MAX);
Alex Elder57385b52013-04-21 12:14:45 -05004989 goto out_err;
Alex Elderc0cd10db2013-04-26 09:43:47 -05004990 }
Alex Elder0903e872012-11-14 12:25:19 -06004991
Alex Elder979ed482012-11-01 08:39:26 -05004992 image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elder86b00e02012-10-25 23:34:42 -05004993 if (IS_ERR(image_id)) {
4994 ret = PTR_ERR(image_id);
4995 goto out_err;
4996 }
Alex Elder3b5cf2a2013-05-29 11:18:59 -05004997 ceph_decode_64_safe(&p, end, snap_id, out_err);
Alex Elder86b00e02012-10-25 23:34:42 -05004998 ceph_decode_64_safe(&p, end, overlap, out_err);
4999
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005000 /*
5001 * The parent won't change (except when the clone is
5002 * flattened, already handled that). So we only need to
5003 * record the parent spec we have not already done so.
5004 */
5005 if (!rbd_dev->parent_spec) {
5006 parent_spec->pool_id = pool_id;
5007 parent_spec->image_id = image_id;
5008 parent_spec->snap_id = snap_id;
Alex Elder70cf49c2013-05-06 17:40:33 -05005009 rbd_dev->parent_spec = parent_spec;
5010 parent_spec = NULL; /* rbd_dev now owns this */
Ilya Dryomovfbba11b2014-06-27 21:46:33 +04005011 } else {
5012 kfree(image_id);
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005013 }
5014
5015 /*
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005016 * We always update the parent overlap. If it's zero we issue
5017 * a warning, as we will proceed as if there was no parent.
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005018 */
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005019 if (!overlap) {
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005020 if (parent_spec) {
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005021 /* refresh, careful to warn just once */
5022 if (rbd_dev->parent_overlap)
5023 rbd_warn(rbd_dev,
5024 "clone now standalone (overlap became 0)");
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005025 } else {
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005026 /* initial probe */
5027 rbd_warn(rbd_dev, "clone is standalone (overlap 0)");
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005028 }
Alex Elder70cf49c2013-05-06 17:40:33 -05005029 }
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005030 rbd_dev->parent_overlap = overlap;
5031
Alex Elder86b00e02012-10-25 23:34:42 -05005032out:
5033 ret = 0;
5034out_err:
5035 kfree(reply_buf);
5036 rbd_spec_put(parent_spec);
5037
5038 return ret;
5039}
5040
Alex Eldercc070d52013-04-21 12:14:45 -05005041static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev)
5042{
5043 struct {
5044 __le64 stripe_unit;
5045 __le64 stripe_count;
5046 } __attribute__ ((packed)) striping_info_buf = { 0 };
5047 size_t size = sizeof (striping_info_buf);
5048 void *p;
5049 u64 obj_size;
5050 u64 stripe_unit;
5051 u64 stripe_count;
5052 int ret;
5053
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005054 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5055 &rbd_dev->header_oloc, "get_stripe_unit_count",
5056 NULL, 0, &striping_info_buf, size);
Alex Eldercc070d52013-04-21 12:14:45 -05005057 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
5058 if (ret < 0)
5059 return ret;
5060 if (ret < size)
5061 return -ERANGE;
5062
5063 /*
5064 * We don't actually support the "fancy striping" feature
5065 * (STRIPINGV2) yet, but if the striping sizes are the
5066 * defaults the behavior is the same as before. So find
5067 * out, and only fail if the image has non-default values.
5068 */
5069 ret = -EINVAL;
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +01005070 obj_size = rbd_obj_bytes(&rbd_dev->header);
Alex Eldercc070d52013-04-21 12:14:45 -05005071 p = &striping_info_buf;
5072 stripe_unit = ceph_decode_64(&p);
5073 if (stripe_unit != obj_size) {
5074 rbd_warn(rbd_dev, "unsupported stripe unit "
5075 "(got %llu want %llu)",
5076 stripe_unit, obj_size);
5077 return -EINVAL;
5078 }
5079 stripe_count = ceph_decode_64(&p);
5080 if (stripe_count != 1) {
5081 rbd_warn(rbd_dev, "unsupported stripe count "
5082 "(got %llu want 1)", stripe_count);
5083 return -EINVAL;
5084 }
Alex Elder500d0c02013-04-26 09:43:47 -05005085 rbd_dev->header.stripe_unit = stripe_unit;
5086 rbd_dev->header.stripe_count = stripe_count;
Alex Eldercc070d52013-04-21 12:14:45 -05005087
5088 return 0;
5089}
5090
Ilya Dryomov7e973322017-01-25 18:16:22 +01005091static int rbd_dev_v2_data_pool(struct rbd_device *rbd_dev)
5092{
5093 __le64 data_pool_id;
5094 int ret;
5095
5096 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5097 &rbd_dev->header_oloc, "get_data_pool",
5098 NULL, 0, &data_pool_id, sizeof(data_pool_id));
5099 if (ret < 0)
5100 return ret;
5101 if (ret < sizeof(data_pool_id))
5102 return -EBADMSG;
5103
5104 rbd_dev->header.data_pool_id = le64_to_cpu(data_pool_id);
5105 WARN_ON(rbd_dev->header.data_pool_id == CEPH_NOPOOL);
5106 return 0;
5107}
5108
Alex Elder9e15b772012-10-30 19:40:33 -05005109static char *rbd_dev_image_name(struct rbd_device *rbd_dev)
5110{
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005111 CEPH_DEFINE_OID_ONSTACK(oid);
Alex Elder9e15b772012-10-30 19:40:33 -05005112 size_t image_id_size;
5113 char *image_id;
5114 void *p;
5115 void *end;
5116 size_t size;
5117 void *reply_buf = NULL;
5118 size_t len = 0;
5119 char *image_name = NULL;
5120 int ret;
5121
5122 rbd_assert(!rbd_dev->spec->image_name);
5123
Alex Elder69e7a022012-11-01 08:39:26 -05005124 len = strlen(rbd_dev->spec->image_id);
5125 image_id_size = sizeof (__le32) + len;
Alex Elder9e15b772012-10-30 19:40:33 -05005126 image_id = kmalloc(image_id_size, GFP_KERNEL);
5127 if (!image_id)
5128 return NULL;
5129
5130 p = image_id;
Alex Elder41579762013-04-21 12:14:45 -05005131 end = image_id + image_id_size;
Alex Elder57385b52013-04-21 12:14:45 -05005132 ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len);
Alex Elder9e15b772012-10-30 19:40:33 -05005133
5134 size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX;
5135 reply_buf = kmalloc(size, GFP_KERNEL);
5136 if (!reply_buf)
5137 goto out;
5138
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005139 ceph_oid_printf(&oid, "%s", RBD_DIRECTORY);
5140 ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc,
5141 "dir_get_name", image_id, image_id_size,
5142 reply_buf, size);
Alex Elder9e15b772012-10-30 19:40:33 -05005143 if (ret < 0)
5144 goto out;
5145 p = reply_buf;
Alex Elderf40eb342013-04-25 15:09:42 -05005146 end = reply_buf + ret;
5147
Alex Elder9e15b772012-10-30 19:40:33 -05005148 image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL);
5149 if (IS_ERR(image_name))
5150 image_name = NULL;
5151 else
5152 dout("%s: name is %s len is %zd\n", __func__, image_name, len);
5153out:
5154 kfree(reply_buf);
5155 kfree(image_id);
5156
5157 return image_name;
5158}
5159
Alex Elder2ad3d712013-04-30 00:44:33 -05005160static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
5161{
5162 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
5163 const char *snap_name;
5164 u32 which = 0;
5165
5166 /* Skip over names until we find the one we are looking for */
5167
5168 snap_name = rbd_dev->header.snap_names;
5169 while (which < snapc->num_snaps) {
5170 if (!strcmp(name, snap_name))
5171 return snapc->snaps[which];
5172 snap_name += strlen(snap_name) + 1;
5173 which++;
5174 }
5175 return CEPH_NOSNAP;
5176}
5177
5178static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
5179{
5180 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
5181 u32 which;
5182 bool found = false;
5183 u64 snap_id;
5184
5185 for (which = 0; !found && which < snapc->num_snaps; which++) {
5186 const char *snap_name;
5187
5188 snap_id = snapc->snaps[which];
5189 snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id);
Josh Durginefadc982013-08-29 19:16:42 -07005190 if (IS_ERR(snap_name)) {
5191 /* ignore no-longer existing snapshots */
5192 if (PTR_ERR(snap_name) == -ENOENT)
5193 continue;
5194 else
5195 break;
5196 }
Alex Elder2ad3d712013-04-30 00:44:33 -05005197 found = !strcmp(name, snap_name);
5198 kfree(snap_name);
5199 }
5200 return found ? snap_id : CEPH_NOSNAP;
5201}
5202
5203/*
5204 * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if
5205 * no snapshot by that name is found, or if an error occurs.
5206 */
5207static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
5208{
5209 if (rbd_dev->image_format == 1)
5210 return rbd_v1_snap_id_by_name(rbd_dev, name);
5211
5212 return rbd_v2_snap_id_by_name(rbd_dev, name);
5213}
5214
Alex Elder9e15b772012-10-30 19:40:33 -05005215/*
Ilya Dryomov04077592014-07-23 17:11:20 +04005216 * An image being mapped will have everything but the snap id.
Alex Elder9e15b772012-10-30 19:40:33 -05005217 */
Ilya Dryomov04077592014-07-23 17:11:20 +04005218static int rbd_spec_fill_snap_id(struct rbd_device *rbd_dev)
5219{
5220 struct rbd_spec *spec = rbd_dev->spec;
5221
5222 rbd_assert(spec->pool_id != CEPH_NOPOOL && spec->pool_name);
5223 rbd_assert(spec->image_id && spec->image_name);
5224 rbd_assert(spec->snap_name);
5225
5226 if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) {
5227 u64 snap_id;
5228
5229 snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name);
5230 if (snap_id == CEPH_NOSNAP)
5231 return -ENOENT;
5232
5233 spec->snap_id = snap_id;
5234 } else {
5235 spec->snap_id = CEPH_NOSNAP;
5236 }
5237
5238 return 0;
5239}
5240
5241/*
5242 * A parent image will have all ids but none of the names.
5243 *
5244 * All names in an rbd spec are dynamically allocated. It's OK if we
5245 * can't figure out the name for an image id.
5246 */
5247static int rbd_spec_fill_names(struct rbd_device *rbd_dev)
Alex Elder9e15b772012-10-30 19:40:33 -05005248{
Alex Elder2e9f7f12013-04-26 09:43:48 -05005249 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
5250 struct rbd_spec *spec = rbd_dev->spec;
5251 const char *pool_name;
5252 const char *image_name;
5253 const char *snap_name;
Alex Elder9e15b772012-10-30 19:40:33 -05005254 int ret;
5255
Ilya Dryomov04077592014-07-23 17:11:20 +04005256 rbd_assert(spec->pool_id != CEPH_NOPOOL);
5257 rbd_assert(spec->image_id);
5258 rbd_assert(spec->snap_id != CEPH_NOSNAP);
Alex Elder9e15b772012-10-30 19:40:33 -05005259
Alex Elder2e9f7f12013-04-26 09:43:48 -05005260 /* Get the pool name; we have to make our own copy of this */
Alex Elder9e15b772012-10-30 19:40:33 -05005261
Alex Elder2e9f7f12013-04-26 09:43:48 -05005262 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id);
5263 if (!pool_name) {
5264 rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id);
Alex Elder935dc892012-11-01 10:17:15 -05005265 return -EIO;
5266 }
Alex Elder2e9f7f12013-04-26 09:43:48 -05005267 pool_name = kstrdup(pool_name, GFP_KERNEL);
5268 if (!pool_name)
Alex Elder9e15b772012-10-30 19:40:33 -05005269 return -ENOMEM;
5270
5271 /* Fetch the image name; tolerate failure here */
5272
Alex Elder2e9f7f12013-04-26 09:43:48 -05005273 image_name = rbd_dev_image_name(rbd_dev);
5274 if (!image_name)
Alex Elder06ecc6c2012-11-01 10:17:15 -05005275 rbd_warn(rbd_dev, "unable to get image name");
Alex Elder9e15b772012-10-30 19:40:33 -05005276
Ilya Dryomov04077592014-07-23 17:11:20 +04005277 /* Fetch the snapshot name */
Alex Elder9e15b772012-10-30 19:40:33 -05005278
Alex Elder2e9f7f12013-04-26 09:43:48 -05005279 snap_name = rbd_snap_name(rbd_dev, spec->snap_id);
Josh Durginda6a6b62013-09-04 17:57:31 -07005280 if (IS_ERR(snap_name)) {
5281 ret = PTR_ERR(snap_name);
Alex Elder9e15b772012-10-30 19:40:33 -05005282 goto out_err;
Alex Elder2e9f7f12013-04-26 09:43:48 -05005283 }
5284
5285 spec->pool_name = pool_name;
5286 spec->image_name = image_name;
5287 spec->snap_name = snap_name;
Alex Elder9e15b772012-10-30 19:40:33 -05005288
5289 return 0;
Ilya Dryomov04077592014-07-23 17:11:20 +04005290
Alex Elder9e15b772012-10-30 19:40:33 -05005291out_err:
Alex Elder2e9f7f12013-04-26 09:43:48 -05005292 kfree(image_name);
5293 kfree(pool_name);
Alex Elder9e15b772012-10-30 19:40:33 -05005294 return ret;
5295}
5296
Alex Eldercc4a38bd2013-04-30 00:44:33 -05005297static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev)
Alex Elder35d489f2012-07-03 16:01:19 -05005298{
5299 size_t size;
5300 int ret;
5301 void *reply_buf;
5302 void *p;
5303 void *end;
5304 u64 seq;
5305 u32 snap_count;
5306 struct ceph_snap_context *snapc;
5307 u32 i;
5308
5309 /*
5310 * We'll need room for the seq value (maximum snapshot id),
5311 * snapshot count, and array of that many snapshot ids.
5312 * For now we have a fixed upper limit on the number we're
5313 * prepared to receive.
5314 */
5315 size = sizeof (__le64) + sizeof (__le32) +
5316 RBD_MAX_SNAP_COUNT * sizeof (__le64);
5317 reply_buf = kzalloc(size, GFP_KERNEL);
5318 if (!reply_buf)
5319 return -ENOMEM;
5320
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005321 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5322 &rbd_dev->header_oloc, "get_snapcontext",
5323 NULL, 0, reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06005324 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder35d489f2012-07-03 16:01:19 -05005325 if (ret < 0)
5326 goto out;
5327
Alex Elder35d489f2012-07-03 16:01:19 -05005328 p = reply_buf;
Alex Elder57385b52013-04-21 12:14:45 -05005329 end = reply_buf + ret;
5330 ret = -ERANGE;
Alex Elder35d489f2012-07-03 16:01:19 -05005331 ceph_decode_64_safe(&p, end, seq, out);
5332 ceph_decode_32_safe(&p, end, snap_count, out);
5333
5334 /*
5335 * Make sure the reported number of snapshot ids wouldn't go
5336 * beyond the end of our buffer. But before checking that,
5337 * make sure the computed size of the snapshot context we
5338 * allocate is representable in a size_t.
5339 */
5340 if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
5341 / sizeof (u64)) {
5342 ret = -EINVAL;
5343 goto out;
5344 }
5345 if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
5346 goto out;
Alex Elder468521c2013-04-26 09:43:47 -05005347 ret = 0;
Alex Elder35d489f2012-07-03 16:01:19 -05005348
Alex Elder812164f82013-04-30 00:44:32 -05005349 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
Alex Elder35d489f2012-07-03 16:01:19 -05005350 if (!snapc) {
5351 ret = -ENOMEM;
5352 goto out;
5353 }
Alex Elder35d489f2012-07-03 16:01:19 -05005354 snapc->seq = seq;
Alex Elder35d489f2012-07-03 16:01:19 -05005355 for (i = 0; i < snap_count; i++)
5356 snapc->snaps[i] = ceph_decode_64(&p);
5357
Alex Elder49ece552013-05-06 08:37:00 -05005358 ceph_put_snap_context(rbd_dev->header.snapc);
Alex Elder35d489f2012-07-03 16:01:19 -05005359 rbd_dev->header.snapc = snapc;
5360
5361 dout(" snap context seq = %llu, snap_count = %u\n",
Alex Elder57385b52013-04-21 12:14:45 -05005362 (unsigned long long)seq, (unsigned int)snap_count);
Alex Elder35d489f2012-07-03 16:01:19 -05005363out:
5364 kfree(reply_buf);
5365
Alex Elder57385b52013-04-21 12:14:45 -05005366 return ret;
Alex Elder35d489f2012-07-03 16:01:19 -05005367}
5368
Alex Elder54cac612013-04-30 00:44:33 -05005369static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
5370 u64 snap_id)
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005371{
5372 size_t size;
5373 void *reply_buf;
Alex Elder54cac612013-04-30 00:44:33 -05005374 __le64 snapid;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005375 int ret;
5376 void *p;
5377 void *end;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005378 char *snap_name;
5379
5380 size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN;
5381 reply_buf = kmalloc(size, GFP_KERNEL);
5382 if (!reply_buf)
5383 return ERR_PTR(-ENOMEM);
5384
Alex Elder54cac612013-04-30 00:44:33 -05005385 snapid = cpu_to_le64(snap_id);
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005386 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5387 &rbd_dev->header_oloc, "get_snapshot_name",
5388 &snapid, sizeof(snapid), reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06005389 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderf40eb342013-04-25 15:09:42 -05005390 if (ret < 0) {
5391 snap_name = ERR_PTR(ret);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005392 goto out;
Alex Elderf40eb342013-04-25 15:09:42 -05005393 }
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005394
5395 p = reply_buf;
Alex Elderf40eb342013-04-25 15:09:42 -05005396 end = reply_buf + ret;
Alex Eldere5c35532012-10-25 23:34:41 -05005397 snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elderf40eb342013-04-25 15:09:42 -05005398 if (IS_ERR(snap_name))
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005399 goto out;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005400
Alex Elderf40eb342013-04-25 15:09:42 -05005401 dout(" snap_id 0x%016llx snap_name = %s\n",
Alex Elder54cac612013-04-30 00:44:33 -05005402 (unsigned long long)snap_id, snap_name);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005403out:
5404 kfree(reply_buf);
5405
Alex Elderf40eb342013-04-25 15:09:42 -05005406 return snap_name;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005407}
5408
Alex Elder2df3fac2013-05-06 09:51:30 -05005409static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev)
Alex Elder117973f2012-08-31 17:29:55 -05005410{
Alex Elder2df3fac2013-05-06 09:51:30 -05005411 bool first_time = rbd_dev->header.object_prefix == NULL;
Alex Elder117973f2012-08-31 17:29:55 -05005412 int ret;
Alex Elder117973f2012-08-31 17:29:55 -05005413
Josh Durgin1617e402013-06-12 14:43:10 -07005414 ret = rbd_dev_v2_image_size(rbd_dev);
5415 if (ret)
Alex Eldercfbf6372013-05-31 17:40:45 -05005416 return ret;
Josh Durgin1617e402013-06-12 14:43:10 -07005417
Alex Elder2df3fac2013-05-06 09:51:30 -05005418 if (first_time) {
5419 ret = rbd_dev_v2_header_onetime(rbd_dev);
5420 if (ret)
Alex Eldercfbf6372013-05-31 17:40:45 -05005421 return ret;
Alex Elder2df3fac2013-05-06 09:51:30 -05005422 }
5423
Alex Eldercc4a38bd2013-04-30 00:44:33 -05005424 ret = rbd_dev_v2_snap_context(rbd_dev);
Ilya Dryomovd194cd12015-08-31 18:22:10 +03005425 if (ret && first_time) {
5426 kfree(rbd_dev->header.object_prefix);
5427 rbd_dev->header.object_prefix = NULL;
5428 }
Alex Elder117973f2012-08-31 17:29:55 -05005429
5430 return ret;
5431}
5432
Ilya Dryomova720ae02014-07-23 17:11:19 +04005433static int rbd_dev_header_info(struct rbd_device *rbd_dev)
5434{
5435 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
5436
5437 if (rbd_dev->image_format == 1)
5438 return rbd_dev_v1_header_info(rbd_dev);
5439
5440 return rbd_dev_v2_header_info(rbd_dev);
5441}
5442
Alex Elder1ddbe942012-01-29 13:57:44 -06005443/*
Alex Eldere28fff262012-02-02 08:13:30 -06005444 * Skips over white space at *buf, and updates *buf to point to the
5445 * first found non-space character (if any). Returns the length of
Alex Elder593a9e72012-02-07 12:03:37 -06005446 * the token (string of non-white space characters) found. Note
5447 * that *buf must be terminated with '\0'.
Alex Eldere28fff262012-02-02 08:13:30 -06005448 */
5449static inline size_t next_token(const char **buf)
5450{
5451 /*
5452 * These are the characters that produce nonzero for
5453 * isspace() in the "C" and "POSIX" locales.
5454 */
5455 const char *spaces = " \f\n\r\t\v";
5456
5457 *buf += strspn(*buf, spaces); /* Find start of token */
5458
5459 return strcspn(*buf, spaces); /* Return token length */
5460}
5461
5462/*
Alex Elderea3352f2012-07-09 21:04:23 -05005463 * Finds the next token in *buf, dynamically allocates a buffer big
5464 * enough to hold a copy of it, and copies the token into the new
5465 * buffer. The copy is guaranteed to be terminated with '\0'. Note
5466 * that a duplicate buffer is created even for a zero-length token.
5467 *
5468 * Returns a pointer to the newly-allocated duplicate, or a null
5469 * pointer if memory for the duplicate was not available. If
5470 * the lenp argument is a non-null pointer, the length of the token
5471 * (not including the '\0') is returned in *lenp.
5472 *
5473 * If successful, the *buf pointer will be updated to point beyond
5474 * the end of the found token.
5475 *
5476 * Note: uses GFP_KERNEL for allocation.
5477 */
5478static inline char *dup_token(const char **buf, size_t *lenp)
5479{
5480 char *dup;
5481 size_t len;
5482
5483 len = next_token(buf);
Alex Elder4caf35f2012-11-01 08:39:27 -05005484 dup = kmemdup(*buf, len + 1, GFP_KERNEL);
Alex Elderea3352f2012-07-09 21:04:23 -05005485 if (!dup)
5486 return NULL;
Alex Elderea3352f2012-07-09 21:04:23 -05005487 *(dup + len) = '\0';
5488 *buf += len;
5489
5490 if (lenp)
5491 *lenp = len;
5492
5493 return dup;
5494}
5495
5496/*
Alex Elder859c31d2012-10-25 23:34:42 -05005497 * Parse the options provided for an "rbd add" (i.e., rbd image
5498 * mapping) request. These arrive via a write to /sys/bus/rbd/add,
5499 * and the data written is passed here via a NUL-terminated buffer.
5500 * Returns 0 if successful or an error code otherwise.
Alex Elderd22f76e2012-07-12 10:46:35 -05005501 *
Alex Elder859c31d2012-10-25 23:34:42 -05005502 * The information extracted from these options is recorded in
5503 * the other parameters which return dynamically-allocated
5504 * structures:
5505 * ceph_opts
5506 * The address of a pointer that will refer to a ceph options
5507 * structure. Caller must release the returned pointer using
5508 * ceph_destroy_options() when it is no longer needed.
5509 * rbd_opts
5510 * Address of an rbd options pointer. Fully initialized by
5511 * this function; caller must release with kfree().
5512 * spec
5513 * Address of an rbd image specification pointer. Fully
5514 * initialized by this function based on parsed options.
5515 * Caller must release with rbd_spec_put().
5516 *
5517 * The options passed take this form:
5518 * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
5519 * where:
5520 * <mon_addrs>
5521 * A comma-separated list of one or more monitor addresses.
5522 * A monitor address is an ip address, optionally followed
5523 * by a port number (separated by a colon).
5524 * I.e.: ip1[:port1][,ip2[:port2]...]
5525 * <options>
5526 * A comma-separated list of ceph and/or rbd options.
5527 * <pool_name>
5528 * The name of the rados pool containing the rbd image.
5529 * <image_name>
5530 * The name of the image in that pool to map.
5531 * <snap_id>
5532 * An optional snapshot id. If provided, the mapping will
5533 * present data from the image at the time that snapshot was
5534 * created. The image head is used if no snapshot id is
5535 * provided. Snapshot mappings are always read-only.
Alex Eldera725f65e2012-02-02 08:13:30 -06005536 */
Alex Elder859c31d2012-10-25 23:34:42 -05005537static int rbd_add_parse_args(const char *buf,
Alex Elderdc79b112012-10-25 23:34:41 -05005538 struct ceph_options **ceph_opts,
Alex Elder859c31d2012-10-25 23:34:42 -05005539 struct rbd_options **opts,
5540 struct rbd_spec **rbd_spec)
Alex Eldera725f65e2012-02-02 08:13:30 -06005541{
Alex Elderd22f76e2012-07-12 10:46:35 -05005542 size_t len;
Alex Elder859c31d2012-10-25 23:34:42 -05005543 char *options;
Alex Elder0ddebc02012-10-25 23:34:41 -05005544 const char *mon_addrs;
Alex Elderecb4dc222013-04-26 09:43:47 -05005545 char *snap_name;
Alex Elder0ddebc02012-10-25 23:34:41 -05005546 size_t mon_addrs_size;
Alex Elder859c31d2012-10-25 23:34:42 -05005547 struct rbd_spec *spec = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05005548 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05005549 struct ceph_options *copts;
Alex Elderdc79b112012-10-25 23:34:41 -05005550 int ret;
Alex Eldere28fff262012-02-02 08:13:30 -06005551
5552 /* The first four tokens are required */
5553
Alex Elder7ef32142012-02-02 08:13:30 -06005554 len = next_token(&buf);
Alex Elder4fb5d6712012-11-01 10:17:15 -05005555 if (!len) {
5556 rbd_warn(NULL, "no monitor address(es) provided");
5557 return -EINVAL;
5558 }
Alex Elder0ddebc02012-10-25 23:34:41 -05005559 mon_addrs = buf;
Alex Elderf28e5652012-10-25 23:34:41 -05005560 mon_addrs_size = len + 1;
Alex Elder7ef32142012-02-02 08:13:30 -06005561 buf += len;
Alex Eldera725f65e2012-02-02 08:13:30 -06005562
Alex Elderdc79b112012-10-25 23:34:41 -05005563 ret = -EINVAL;
Alex Elderf28e5652012-10-25 23:34:41 -05005564 options = dup_token(&buf, NULL);
5565 if (!options)
Alex Elderdc79b112012-10-25 23:34:41 -05005566 return -ENOMEM;
Alex Elder4fb5d6712012-11-01 10:17:15 -05005567 if (!*options) {
5568 rbd_warn(NULL, "no options provided");
5569 goto out_err;
5570 }
Alex Eldera725f65e2012-02-02 08:13:30 -06005571
Alex Elder859c31d2012-10-25 23:34:42 -05005572 spec = rbd_spec_alloc();
5573 if (!spec)
Alex Elderf28e5652012-10-25 23:34:41 -05005574 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05005575
5576 spec->pool_name = dup_token(&buf, NULL);
5577 if (!spec->pool_name)
5578 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05005579 if (!*spec->pool_name) {
5580 rbd_warn(NULL, "no pool name provided");
5581 goto out_err;
5582 }
Alex Eldere28fff262012-02-02 08:13:30 -06005583
Alex Elder69e7a022012-11-01 08:39:26 -05005584 spec->image_name = dup_token(&buf, NULL);
Alex Elder859c31d2012-10-25 23:34:42 -05005585 if (!spec->image_name)
Alex Elderf28e5652012-10-25 23:34:41 -05005586 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05005587 if (!*spec->image_name) {
5588 rbd_warn(NULL, "no image name provided");
5589 goto out_err;
5590 }
Alex Eldere28fff262012-02-02 08:13:30 -06005591
Alex Elderf28e5652012-10-25 23:34:41 -05005592 /*
5593 * Snapshot name is optional; default is to use "-"
5594 * (indicating the head/no snapshot).
5595 */
Alex Elder3feeb8942012-08-31 17:29:52 -05005596 len = next_token(&buf);
Alex Elder820a5f32012-07-09 21:04:24 -05005597 if (!len) {
Alex Elder3feeb8942012-08-31 17:29:52 -05005598 buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
5599 len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
Alex Elderf28e5652012-10-25 23:34:41 -05005600 } else if (len > RBD_MAX_SNAP_NAME_LEN) {
Alex Elderdc79b112012-10-25 23:34:41 -05005601 ret = -ENAMETOOLONG;
Alex Elderf28e5652012-10-25 23:34:41 -05005602 goto out_err;
Alex Elder849b4262012-07-09 21:04:24 -05005603 }
Alex Elderecb4dc222013-04-26 09:43:47 -05005604 snap_name = kmemdup(buf, len + 1, GFP_KERNEL);
5605 if (!snap_name)
Alex Elderf28e5652012-10-25 23:34:41 -05005606 goto out_mem;
Alex Elderecb4dc222013-04-26 09:43:47 -05005607 *(snap_name + len) = '\0';
5608 spec->snap_name = snap_name;
Alex Eldere5c35532012-10-25 23:34:41 -05005609
Alex Elder0ddebc02012-10-25 23:34:41 -05005610 /* Initialize all rbd options to the defaults */
Alex Eldere28fff262012-02-02 08:13:30 -06005611
Alex Elder4e9afeb2012-10-25 23:34:41 -05005612 rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL);
5613 if (!rbd_opts)
5614 goto out_mem;
5615
5616 rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
Ilya Dryomovb5584182015-06-23 16:21:19 +03005617 rbd_opts->queue_depth = RBD_QUEUE_DEPTH_DEFAULT;
Ilya Dryomov80de1912016-09-20 14:23:17 +02005618 rbd_opts->lock_on_read = RBD_LOCK_ON_READ_DEFAULT;
Alex Elderd22f76e2012-07-12 10:46:35 -05005619
Alex Elder859c31d2012-10-25 23:34:42 -05005620 copts = ceph_parse_options(options, mon_addrs,
Alex Elder0ddebc02012-10-25 23:34:41 -05005621 mon_addrs + mon_addrs_size - 1,
Alex Elder4e9afeb2012-10-25 23:34:41 -05005622 parse_rbd_opts_token, rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05005623 if (IS_ERR(copts)) {
5624 ret = PTR_ERR(copts);
Alex Elderdc79b112012-10-25 23:34:41 -05005625 goto out_err;
5626 }
Alex Elder859c31d2012-10-25 23:34:42 -05005627 kfree(options);
5628
5629 *ceph_opts = copts;
Alex Elder4e9afeb2012-10-25 23:34:41 -05005630 *opts = rbd_opts;
Alex Elder859c31d2012-10-25 23:34:42 -05005631 *rbd_spec = spec;
Alex Elder0ddebc02012-10-25 23:34:41 -05005632
Alex Elderdc79b112012-10-25 23:34:41 -05005633 return 0;
Alex Elderf28e5652012-10-25 23:34:41 -05005634out_mem:
Alex Elderdc79b112012-10-25 23:34:41 -05005635 ret = -ENOMEM;
Alex Elderd22f76e2012-07-12 10:46:35 -05005636out_err:
Alex Elder859c31d2012-10-25 23:34:42 -05005637 kfree(rbd_opts);
5638 rbd_spec_put(spec);
Alex Elderf28e5652012-10-25 23:34:41 -05005639 kfree(options);
Alex Elderd22f76e2012-07-12 10:46:35 -05005640
Alex Elderdc79b112012-10-25 23:34:41 -05005641 return ret;
Alex Eldera725f65e2012-02-02 08:13:30 -06005642}
5643
Alex Elder589d30e2012-07-10 20:30:11 -05005644/*
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005645 * Return pool id (>= 0) or a negative error code.
5646 */
5647static int rbd_add_get_pool_id(struct rbd_client *rbdc, const char *pool_name)
5648{
Ilya Dryomova319bf52015-05-15 12:02:17 +03005649 struct ceph_options *opts = rbdc->client->options;
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005650 u64 newest_epoch;
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005651 int tries = 0;
5652 int ret;
5653
5654again:
5655 ret = ceph_pg_poolid_by_name(rbdc->client->osdc.osdmap, pool_name);
5656 if (ret == -ENOENT && tries++ < 1) {
Ilya Dryomovd0b19702016-04-28 16:07:27 +02005657 ret = ceph_monc_get_version(&rbdc->client->monc, "osdmap",
5658 &newest_epoch);
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005659 if (ret < 0)
5660 return ret;
5661
5662 if (rbdc->client->osdc.osdmap->epoch < newest_epoch) {
Ilya Dryomov7cca78c2016-04-28 16:07:28 +02005663 ceph_osdc_maybe_request_map(&rbdc->client->osdc);
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005664 (void) ceph_monc_wait_osdmap(&rbdc->client->monc,
Ilya Dryomova319bf52015-05-15 12:02:17 +03005665 newest_epoch,
5666 opts->mount_timeout);
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005667 goto again;
5668 } else {
5669 /* the osdmap we have is new enough */
5670 return -ENOENT;
5671 }
5672 }
5673
5674 return ret;
5675}
5676
5677/*
Alex Elder589d30e2012-07-10 20:30:11 -05005678 * An rbd format 2 image has a unique identifier, distinct from the
5679 * name given to it by the user. Internally, that identifier is
5680 * what's used to specify the names of objects related to the image.
5681 *
5682 * A special "rbd id" object is used to map an rbd image name to its
5683 * id. If that object doesn't exist, then there is no v2 rbd image
5684 * with the supplied name.
5685 *
5686 * This function will record the given rbd_dev's image_id field if
5687 * it can be determined, and in that case will return 0. If any
5688 * errors occur a negative errno will be returned and the rbd_dev's
5689 * image_id field will be unchanged (and should be NULL).
5690 */
5691static int rbd_dev_image_id(struct rbd_device *rbd_dev)
5692{
5693 int ret;
5694 size_t size;
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005695 CEPH_DEFINE_OID_ONSTACK(oid);
Alex Elder589d30e2012-07-10 20:30:11 -05005696 void *response;
Alex Elderc0fba362013-04-25 23:15:08 -05005697 char *image_id;
Alex Elder2f82ee52012-10-30 19:40:33 -05005698
Alex Elder589d30e2012-07-10 20:30:11 -05005699 /*
Alex Elder2c0d0a12012-10-30 19:40:33 -05005700 * When probing a parent image, the image id is already
5701 * known (and the image name likely is not). There's no
Alex Elderc0fba362013-04-25 23:15:08 -05005702 * need to fetch the image id again in this case. We
5703 * do still need to set the image format though.
Alex Elder2c0d0a12012-10-30 19:40:33 -05005704 */
Alex Elderc0fba362013-04-25 23:15:08 -05005705 if (rbd_dev->spec->image_id) {
5706 rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1;
5707
Alex Elder2c0d0a12012-10-30 19:40:33 -05005708 return 0;
Alex Elderc0fba362013-04-25 23:15:08 -05005709 }
Alex Elder2c0d0a12012-10-30 19:40:33 -05005710
5711 /*
Alex Elder589d30e2012-07-10 20:30:11 -05005712 * First, see if the format 2 image id file exists, and if
5713 * so, get the image's persistent id from it.
5714 */
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005715 ret = ceph_oid_aprintf(&oid, GFP_KERNEL, "%s%s", RBD_ID_PREFIX,
5716 rbd_dev->spec->image_name);
5717 if (ret)
5718 return ret;
5719
5720 dout("rbd id object name is %s\n", oid.name);
Alex Elder589d30e2012-07-10 20:30:11 -05005721
5722 /* Response will be an encoded string, which includes a length */
5723
5724 size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
5725 response = kzalloc(size, GFP_NOIO);
5726 if (!response) {
5727 ret = -ENOMEM;
5728 goto out;
5729 }
5730
Alex Elderc0fba362013-04-25 23:15:08 -05005731 /* If it doesn't exist we'll assume it's a format 1 image */
5732
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005733 ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc,
5734 "get_id", NULL, 0,
5735 response, RBD_IMAGE_ID_LEN_MAX);
Alex Elder36be9a72013-01-19 00:30:28 -06005736 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderc0fba362013-04-25 23:15:08 -05005737 if (ret == -ENOENT) {
5738 image_id = kstrdup("", GFP_KERNEL);
5739 ret = image_id ? 0 : -ENOMEM;
5740 if (!ret)
5741 rbd_dev->image_format = 1;
Ilya Dryomov7dd440c2014-09-11 18:49:18 +04005742 } else if (ret >= 0) {
Alex Elderc0fba362013-04-25 23:15:08 -05005743 void *p = response;
Alex Elder589d30e2012-07-10 20:30:11 -05005744
Alex Elderc0fba362013-04-25 23:15:08 -05005745 image_id = ceph_extract_encoded_string(&p, p + ret,
Alex Elder979ed482012-11-01 08:39:26 -05005746 NULL, GFP_NOIO);
Duan Jiong461f7582014-04-11 16:38:12 +08005747 ret = PTR_ERR_OR_ZERO(image_id);
Alex Elderc0fba362013-04-25 23:15:08 -05005748 if (!ret)
5749 rbd_dev->image_format = 2;
Alex Elderc0fba362013-04-25 23:15:08 -05005750 }
5751
5752 if (!ret) {
5753 rbd_dev->spec->image_id = image_id;
5754 dout("image_id is %s\n", image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05005755 }
5756out:
5757 kfree(response);
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005758 ceph_oid_destroy(&oid);
Alex Elder589d30e2012-07-10 20:30:11 -05005759 return ret;
5760}
5761
Alex Elder3abef3b2013-05-13 20:35:37 -05005762/*
5763 * Undo whatever state changes are made by v1 or v2 header info
5764 * call.
5765 */
Alex Elder6fd48b32013-04-28 23:32:34 -05005766static void rbd_dev_unprobe(struct rbd_device *rbd_dev)
5767{
5768 struct rbd_image_header *header;
5769
Ilya Dryomove69b8d42015-01-19 12:06:14 +03005770 rbd_dev_parent_put(rbd_dev);
Alex Elder6fd48b32013-04-28 23:32:34 -05005771
5772 /* Free dynamic fields from the header, then zero it out */
5773
5774 header = &rbd_dev->header;
Alex Elder812164f82013-04-30 00:44:32 -05005775 ceph_put_snap_context(header->snapc);
Alex Elder6fd48b32013-04-28 23:32:34 -05005776 kfree(header->snap_sizes);
5777 kfree(header->snap_names);
5778 kfree(header->object_prefix);
5779 memset(header, 0, sizeof (*header));
5780}
5781
Alex Elder2df3fac2013-05-06 09:51:30 -05005782static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev)
Alex Eldera30b71b2012-07-10 20:30:11 -05005783{
5784 int ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05005785
Alex Elder1e130192012-07-03 16:01:19 -05005786 ret = rbd_dev_v2_object_prefix(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05005787 if (ret)
Alex Elder1e130192012-07-03 16:01:19 -05005788 goto out_err;
Alex Elderb1b54022012-07-03 16:01:19 -05005789
Alex Elder2df3fac2013-05-06 09:51:30 -05005790 /*
5791 * Get the and check features for the image. Currently the
5792 * features are assumed to never change.
5793 */
Alex Elderb1b54022012-07-03 16:01:19 -05005794 ret = rbd_dev_v2_features(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05005795 if (ret)
Alex Elderb1b54022012-07-03 16:01:19 -05005796 goto out_err;
Alex Elder35d489f2012-07-03 16:01:19 -05005797
Alex Eldercc070d52013-04-21 12:14:45 -05005798 /* If the image supports fancy striping, get its parameters */
5799
5800 if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) {
5801 ret = rbd_dev_v2_striping_info(rbd_dev);
5802 if (ret < 0)
5803 goto out_err;
5804 }
Alex Eldera30b71b2012-07-10 20:30:11 -05005805
Ilya Dryomov7e973322017-01-25 18:16:22 +01005806 if (rbd_dev->header.features & RBD_FEATURE_DATA_POOL) {
5807 ret = rbd_dev_v2_data_pool(rbd_dev);
5808 if (ret)
5809 goto out_err;
5810 }
5811
Ilya Dryomov263423f2017-01-25 18:16:22 +01005812 rbd_init_layout(rbd_dev);
Alex Elder35152972012-08-31 17:29:55 -05005813 return 0;
Ilya Dryomov263423f2017-01-25 18:16:22 +01005814
Alex Elder9d475de2012-07-03 16:01:19 -05005815out_err:
Alex Elder642a2532013-05-06 17:40:33 -05005816 rbd_dev->header.features = 0;
Alex Elder1e130192012-07-03 16:01:19 -05005817 kfree(rbd_dev->header.object_prefix);
5818 rbd_dev->header.object_prefix = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05005819 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05005820}
5821
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005822/*
5823 * @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() ->
5824 * rbd_dev_image_probe() recursion depth, which means it's also the
5825 * length of the already discovered part of the parent chain.
5826 */
5827static int rbd_dev_probe_parent(struct rbd_device *rbd_dev, int depth)
Alex Elder83a06262012-10-30 15:47:17 -05005828{
Alex Elder2f82ee52012-10-30 19:40:33 -05005829 struct rbd_device *parent = NULL;
Alex Elder124afba2013-04-26 15:44:36 -05005830 int ret;
5831
5832 if (!rbd_dev->parent_spec)
5833 return 0;
Alex Elder124afba2013-04-26 15:44:36 -05005834
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005835 if (++depth > RBD_MAX_PARENT_CHAIN_LEN) {
5836 pr_info("parent chain is too long (%d)\n", depth);
5837 ret = -EINVAL;
5838 goto out_err;
5839 }
5840
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02005841 parent = __rbd_dev_create(rbd_dev->rbd_client, rbd_dev->parent_spec);
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005842 if (!parent) {
5843 ret = -ENOMEM;
Alex Elder124afba2013-04-26 15:44:36 -05005844 goto out_err;
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005845 }
5846
5847 /*
5848 * Images related by parent/child relationships always share
5849 * rbd_client and spec/parent_spec, so bump their refcounts.
5850 */
5851 __rbd_get_client(rbd_dev->rbd_client);
5852 rbd_spec_get(rbd_dev->parent_spec);
Alex Elder124afba2013-04-26 15:44:36 -05005853
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005854 ret = rbd_dev_image_probe(parent, depth);
Alex Elder124afba2013-04-26 15:44:36 -05005855 if (ret < 0)
5856 goto out_err;
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005857
Alex Elder124afba2013-04-26 15:44:36 -05005858 rbd_dev->parent = parent;
Alex Eldera2acd002013-05-08 22:50:04 -05005859 atomic_set(&rbd_dev->parent_ref, 1);
Alex Elder124afba2013-04-26 15:44:36 -05005860 return 0;
Alex Elder124afba2013-04-26 15:44:36 -05005861
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005862out_err:
5863 rbd_dev_unparent(rbd_dev);
Markus Elfring1761b222015-11-23 20:16:45 +01005864 rbd_dev_destroy(parent);
Alex Elder124afba2013-04-26 15:44:36 -05005865 return ret;
5866}
5867
Ilya Dryomov811c6682016-04-15 16:22:16 +02005868/*
5869 * rbd_dev->header_rwsem must be locked for write and will be unlocked
5870 * upon return.
5871 */
Alex Elder200a6a82013-04-28 23:32:34 -05005872static int rbd_dev_device_setup(struct rbd_device *rbd_dev)
Alex Elder124afba2013-04-26 15:44:36 -05005873{
Alex Elder83a06262012-10-30 15:47:17 -05005874 int ret;
Alex Elder83a06262012-10-30 15:47:17 -05005875
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005876 /* Record our major and minor device numbers. */
Alex Elder83a06262012-10-30 15:47:17 -05005877
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005878 if (!single_major) {
5879 ret = register_blkdev(0, rbd_dev->name);
5880 if (ret < 0)
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02005881 goto err_out_unlock;
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005882
5883 rbd_dev->major = ret;
5884 rbd_dev->minor = 0;
5885 } else {
5886 rbd_dev->major = rbd_major;
5887 rbd_dev->minor = rbd_dev_id_to_minor(rbd_dev->dev_id);
5888 }
Alex Elder83a06262012-10-30 15:47:17 -05005889
5890 /* Set up the blkdev mapping. */
5891
5892 ret = rbd_init_disk(rbd_dev);
5893 if (ret)
5894 goto err_out_blkdev;
5895
Alex Elderf35a4de2013-05-06 09:51:29 -05005896 ret = rbd_dev_mapping_set(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05005897 if (ret)
5898 goto err_out_disk;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04005899
Alex Elderf35a4de2013-05-06 09:51:29 -05005900 set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
Josh Durgin22001f62013-09-30 20:10:04 -07005901 set_disk_ro(rbd_dev->disk, rbd_dev->mapping.read_only);
Alex Elderf35a4de2013-05-06 09:51:29 -05005902
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02005903 dev_set_name(&rbd_dev->dev, "%d", rbd_dev->dev_id);
5904 ret = device_add(&rbd_dev->dev);
Alex Elderf35a4de2013-05-06 09:51:29 -05005905 if (ret)
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04005906 goto err_out_mapping;
Alex Elder83a06262012-10-30 15:47:17 -05005907
Alex Elder83a06262012-10-30 15:47:17 -05005908 /* Everything's ready. Announce the disk to the world. */
5909
Alex Elder129b79d2013-04-26 15:44:36 -05005910 set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
Ilya Dryomov811c6682016-04-15 16:22:16 +02005911 up_write(&rbd_dev->header_rwsem);
Alex Elder83a06262012-10-30 15:47:17 -05005912
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02005913 spin_lock(&rbd_dev_list_lock);
5914 list_add_tail(&rbd_dev->node, &rbd_dev_list);
5915 spin_unlock(&rbd_dev_list_lock);
5916
Ilya Dryomov811c6682016-04-15 16:22:16 +02005917 add_disk(rbd_dev->disk);
Ilya Dryomovca7909e2016-08-18 18:38:41 +02005918 pr_info("%s: capacity %llu features 0x%llx\n", rbd_dev->disk->disk_name,
5919 (unsigned long long)get_capacity(rbd_dev->disk) << SECTOR_SHIFT,
5920 rbd_dev->header.features);
Alex Elder83a06262012-10-30 15:47:17 -05005921
5922 return ret;
Alex Elder2f82ee52012-10-30 19:40:33 -05005923
Alex Elderf35a4de2013-05-06 09:51:29 -05005924err_out_mapping:
5925 rbd_dev_mapping_clear(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05005926err_out_disk:
5927 rbd_free_disk(rbd_dev);
5928err_out_blkdev:
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005929 if (!single_major)
5930 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Ilya Dryomov811c6682016-04-15 16:22:16 +02005931err_out_unlock:
5932 up_write(&rbd_dev->header_rwsem);
Alex Elder83a06262012-10-30 15:47:17 -05005933 return ret;
5934}
5935
Alex Elder332bb122013-04-27 09:59:30 -05005936static int rbd_dev_header_name(struct rbd_device *rbd_dev)
5937{
5938 struct rbd_spec *spec = rbd_dev->spec;
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005939 int ret;
Alex Elder332bb122013-04-27 09:59:30 -05005940
5941 /* Record the header object name for this rbd image. */
5942
5943 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
Alex Elder332bb122013-04-27 09:59:30 -05005944 if (rbd_dev->image_format == 1)
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005945 ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s",
5946 spec->image_name, RBD_SUFFIX);
Alex Elder332bb122013-04-27 09:59:30 -05005947 else
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005948 ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s",
5949 RBD_HEADER_PREFIX, spec->image_id);
Alex Elder332bb122013-04-27 09:59:30 -05005950
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005951 return ret;
Alex Elder332bb122013-04-27 09:59:30 -05005952}
5953
Alex Elder200a6a82013-04-28 23:32:34 -05005954static void rbd_dev_image_release(struct rbd_device *rbd_dev)
5955{
Alex Elder6fd48b32013-04-28 23:32:34 -05005956 rbd_dev_unprobe(rbd_dev);
Alex Elder6fd48b32013-04-28 23:32:34 -05005957 rbd_dev->image_format = 0;
5958 kfree(rbd_dev->spec->image_id);
5959 rbd_dev->spec->image_id = NULL;
5960
Alex Elder200a6a82013-04-28 23:32:34 -05005961 rbd_dev_destroy(rbd_dev);
5962}
5963
Alex Eldera30b71b2012-07-10 20:30:11 -05005964/*
5965 * Probe for the existence of the header object for the given rbd
Alex Elder1f3ef782013-05-06 17:40:33 -05005966 * device. If this image is the one being mapped (i.e., not a
5967 * parent), initiate a watch on its header object before using that
5968 * object to get detailed information about the rbd image.
Alex Eldera30b71b2012-07-10 20:30:11 -05005969 */
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005970static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth)
Alex Eldera30b71b2012-07-10 20:30:11 -05005971{
5972 int ret;
5973
5974 /*
Alex Elder3abef3b2013-05-13 20:35:37 -05005975 * Get the id from the image id object. Unless there's an
5976 * error, rbd_dev->spec->image_id will be filled in with
5977 * a dynamically-allocated string, and rbd_dev->image_format
5978 * will be set to either 1 or 2.
Alex Eldera30b71b2012-07-10 20:30:11 -05005979 */
5980 ret = rbd_dev_image_id(rbd_dev);
5981 if (ret)
Alex Elderc0fba362013-04-25 23:15:08 -05005982 return ret;
Alex Elderc0fba362013-04-25 23:15:08 -05005983
Alex Elder332bb122013-04-27 09:59:30 -05005984 ret = rbd_dev_header_name(rbd_dev);
5985 if (ret)
5986 goto err_out_format;
5987
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005988 if (!depth) {
Ilya Dryomov99d16942016-08-12 16:11:41 +02005989 ret = rbd_register_watch(rbd_dev);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03005990 if (ret) {
5991 if (ret == -ENOENT)
5992 pr_info("image %s/%s does not exist\n",
5993 rbd_dev->spec->pool_name,
5994 rbd_dev->spec->image_name);
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005995 goto err_out_format;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03005996 }
Alex Elder1f3ef782013-05-06 17:40:33 -05005997 }
Alex Elderb644de22013-04-27 09:59:31 -05005998
Ilya Dryomova720ae02014-07-23 17:11:19 +04005999 ret = rbd_dev_header_info(rbd_dev);
Alex Elder5655c4d2013-04-25 23:15:08 -05006000 if (ret)
Alex Elderb644de22013-04-27 09:59:31 -05006001 goto err_out_watch;
Alex Elder83a06262012-10-30 15:47:17 -05006002
Ilya Dryomov04077592014-07-23 17:11:20 +04006003 /*
6004 * If this image is the one being mapped, we have pool name and
6005 * id, image name and id, and snap name - need to fill snap id.
6006 * Otherwise this is a parent image, identified by pool, image
6007 * and snap ids - need to fill in names for those ids.
6008 */
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006009 if (!depth)
Ilya Dryomov04077592014-07-23 17:11:20 +04006010 ret = rbd_spec_fill_snap_id(rbd_dev);
6011 else
6012 ret = rbd_spec_fill_names(rbd_dev);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006013 if (ret) {
6014 if (ret == -ENOENT)
6015 pr_info("snap %s/%s@%s does not exist\n",
6016 rbd_dev->spec->pool_name,
6017 rbd_dev->spec->image_name,
6018 rbd_dev->spec->snap_name);
Alex Elder33dca392013-04-30 00:44:33 -05006019 goto err_out_probe;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006020 }
Alex Elder9bb81c92013-04-27 09:59:30 -05006021
Ilya Dryomove8f59b52014-07-24 10:42:13 +04006022 if (rbd_dev->header.features & RBD_FEATURE_LAYERING) {
6023 ret = rbd_dev_v2_parent_info(rbd_dev);
6024 if (ret)
6025 goto err_out_probe;
6026
6027 /*
6028 * Need to warn users if this image is the one being
6029 * mapped and has a parent.
6030 */
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006031 if (!depth && rbd_dev->parent_spec)
Ilya Dryomove8f59b52014-07-24 10:42:13 +04006032 rbd_warn(rbd_dev,
6033 "WARNING: kernel layering is EXPERIMENTAL!");
6034 }
6035
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006036 ret = rbd_dev_probe_parent(rbd_dev, depth);
Alex Elder30d60ba2013-05-06 09:51:30 -05006037 if (ret)
6038 goto err_out_probe;
Alex Elder83a06262012-10-30 15:47:17 -05006039
Alex Elder30d60ba2013-05-06 09:51:30 -05006040 dout("discovered format %u image, header name is %s\n",
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006041 rbd_dev->image_format, rbd_dev->header_oid.name);
Alex Elder30d60ba2013-05-06 09:51:30 -05006042 return 0;
Ilya Dryomove8f59b52014-07-24 10:42:13 +04006043
Alex Elder6fd48b32013-04-28 23:32:34 -05006044err_out_probe:
6045 rbd_dev_unprobe(rbd_dev);
Alex Elderb644de22013-04-27 09:59:31 -05006046err_out_watch:
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006047 if (!depth)
Ilya Dryomov99d16942016-08-12 16:11:41 +02006048 rbd_unregister_watch(rbd_dev);
Alex Elder332bb122013-04-27 09:59:30 -05006049err_out_format:
6050 rbd_dev->image_format = 0;
Alex Elder5655c4d2013-04-25 23:15:08 -05006051 kfree(rbd_dev->spec->image_id);
6052 rbd_dev->spec->image_id = NULL;
Alex Elder5655c4d2013-04-25 23:15:08 -05006053 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05006054}
6055
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006056static ssize_t do_rbd_add(struct bus_type *bus,
6057 const char *buf,
6058 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006059{
Alex Eldercb8627c2012-07-09 21:04:23 -05006060 struct rbd_device *rbd_dev = NULL;
Alex Elderdc79b112012-10-25 23:34:41 -05006061 struct ceph_options *ceph_opts = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05006062 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05006063 struct rbd_spec *spec = NULL;
Alex Elder9d3997f2012-10-25 23:34:42 -05006064 struct rbd_client *rbdc;
Alex Elder51344a32013-05-06 07:40:30 -05006065 bool read_only;
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02006066 int rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006067
6068 if (!try_module_get(THIS_MODULE))
6069 return -ENODEV;
6070
Alex Eldera725f65e2012-02-02 08:13:30 -06006071 /* parse add command */
Alex Elder859c31d2012-10-25 23:34:42 -05006072 rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec);
Alex Elderdc79b112012-10-25 23:34:41 -05006073 if (rc < 0)
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006074 goto out;
Alex Eldera725f65e2012-02-02 08:13:30 -06006075
Alex Elder9d3997f2012-10-25 23:34:42 -05006076 rbdc = rbd_get_client(ceph_opts);
6077 if (IS_ERR(rbdc)) {
6078 rc = PTR_ERR(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05006079 goto err_out_args;
Alex Elder9d3997f2012-10-25 23:34:42 -05006080 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006081
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006082 /* pick the pool */
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04006083 rc = rbd_add_get_pool_id(rbdc, spec->pool_name);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006084 if (rc < 0) {
6085 if (rc == -ENOENT)
6086 pr_info("pool %s does not exist\n", spec->pool_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006087 goto err_out_client;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006088 }
Alex Elderc0cd10db2013-04-26 09:43:47 -05006089 spec->pool_id = (u64)rc;
Alex Elder859c31d2012-10-25 23:34:42 -05006090
Ilya Dryomovd1475432015-06-22 13:24:48 +03006091 rbd_dev = rbd_dev_create(rbdc, spec, rbd_opts);
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02006092 if (!rbd_dev) {
6093 rc = -ENOMEM;
Alex Elderbd4ba652012-10-25 23:34:42 -05006094 goto err_out_client;
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02006095 }
Alex Elderc53d5892012-10-25 23:34:42 -05006096 rbdc = NULL; /* rbd_dev now owns this */
6097 spec = NULL; /* rbd_dev now owns this */
Ilya Dryomovd1475432015-06-22 13:24:48 +03006098 rbd_opts = NULL; /* rbd_dev now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006099
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02006100 rbd_dev->config_info = kstrdup(buf, GFP_KERNEL);
6101 if (!rbd_dev->config_info) {
6102 rc = -ENOMEM;
6103 goto err_out_rbd_dev;
6104 }
6105
Ilya Dryomov811c6682016-04-15 16:22:16 +02006106 down_write(&rbd_dev->header_rwsem);
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006107 rc = rbd_dev_image_probe(rbd_dev, 0);
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02006108 if (rc < 0) {
6109 up_write(&rbd_dev->header_rwsem);
Alex Elderc53d5892012-10-25 23:34:42 -05006110 goto err_out_rbd_dev;
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02006111 }
Alex Elder05fd6f62012-08-29 17:11:07 -05006112
Alex Elder7ce4eef2013-05-06 17:40:33 -05006113 /* If we are mapping a snapshot it must be marked read-only */
6114
Ilya Dryomovd1475432015-06-22 13:24:48 +03006115 read_only = rbd_dev->opts->read_only;
Alex Elder7ce4eef2013-05-06 17:40:33 -05006116 if (rbd_dev->spec->snap_id != CEPH_NOSNAP)
6117 read_only = true;
6118 rbd_dev->mapping.read_only = read_only;
6119
Alex Elderb536f692013-04-28 23:32:34 -05006120 rc = rbd_dev_device_setup(rbd_dev);
Alex Elder3abef3b2013-05-13 20:35:37 -05006121 if (rc) {
Ilya Dryomove37180c2013-12-16 18:02:41 +02006122 /*
Ilya Dryomov99d16942016-08-12 16:11:41 +02006123 * rbd_unregister_watch() can't be moved into
Ilya Dryomove37180c2013-12-16 18:02:41 +02006124 * rbd_dev_image_release() without refactoring, see
6125 * commit 1f3ef78861ac.
6126 */
Ilya Dryomov99d16942016-08-12 16:11:41 +02006127 rbd_unregister_watch(rbd_dev);
Alex Elder3abef3b2013-05-13 20:35:37 -05006128 rbd_dev_image_release(rbd_dev);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006129 goto out;
Alex Elder3abef3b2013-05-13 20:35:37 -05006130 }
Alex Elderb536f692013-04-28 23:32:34 -05006131
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006132 rc = count;
6133out:
6134 module_put(THIS_MODULE);
6135 return rc;
Alex Elder3abef3b2013-05-13 20:35:37 -05006136
Alex Elderc53d5892012-10-25 23:34:42 -05006137err_out_rbd_dev:
6138 rbd_dev_destroy(rbd_dev);
Alex Elderbd4ba652012-10-25 23:34:42 -05006139err_out_client:
Alex Elder9d3997f2012-10-25 23:34:42 -05006140 rbd_put_client(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05006141err_out_args:
Alex Elder859c31d2012-10-25 23:34:42 -05006142 rbd_spec_put(spec);
Ilya Dryomovd1475432015-06-22 13:24:48 +03006143 kfree(rbd_opts);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006144 goto out;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006145}
6146
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006147static ssize_t rbd_add(struct bus_type *bus,
6148 const char *buf,
6149 size_t count)
6150{
6151 if (single_major)
6152 return -EINVAL;
6153
6154 return do_rbd_add(bus, buf, count);
6155}
6156
6157static ssize_t rbd_add_single_major(struct bus_type *bus,
6158 const char *buf,
6159 size_t count)
6160{
6161 return do_rbd_add(bus, buf, count);
6162}
6163
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006164static void rbd_dev_device_release(struct rbd_device *rbd_dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006165{
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006166 rbd_free_disk(rbd_dev);
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02006167
6168 spin_lock(&rbd_dev_list_lock);
6169 list_del_init(&rbd_dev->node);
6170 spin_unlock(&rbd_dev_list_lock);
6171
Alex Elder200a6a82013-04-28 23:32:34 -05006172 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006173 device_del(&rbd_dev->dev);
Alex Elder6d80b132013-05-06 07:40:30 -05006174 rbd_dev_mapping_clear(rbd_dev);
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006175 if (!single_major)
6176 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006177}
6178
Alex Elder05a46af2013-04-26 15:44:36 -05006179static void rbd_dev_remove_parent(struct rbd_device *rbd_dev)
6180{
Alex Elderad945fc2013-04-26 15:44:36 -05006181 while (rbd_dev->parent) {
Alex Elder05a46af2013-04-26 15:44:36 -05006182 struct rbd_device *first = rbd_dev;
6183 struct rbd_device *second = first->parent;
6184 struct rbd_device *third;
6185
6186 /*
6187 * Follow to the parent with no grandparent and
6188 * remove it.
6189 */
6190 while (second && (third = second->parent)) {
6191 first = second;
6192 second = third;
6193 }
Alex Elderad945fc2013-04-26 15:44:36 -05006194 rbd_assert(second);
Alex Elder8ad42cd2013-04-28 23:32:34 -05006195 rbd_dev_image_release(second);
Alex Elderad945fc2013-04-26 15:44:36 -05006196 first->parent = NULL;
6197 first->parent_overlap = 0;
6198
6199 rbd_assert(first->parent_spec);
Alex Elder05a46af2013-04-26 15:44:36 -05006200 rbd_spec_put(first->parent_spec);
6201 first->parent_spec = NULL;
Alex Elder05a46af2013-04-26 15:44:36 -05006202 }
6203}
6204
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006205static ssize_t do_rbd_remove(struct bus_type *bus,
6206 const char *buf,
6207 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006208{
6209 struct rbd_device *rbd_dev = NULL;
Alex Elder751cc0e2013-05-31 15:17:01 -05006210 struct list_head *tmp;
6211 int dev_id;
Mike Christie0276dca2016-08-18 18:38:45 +02006212 char opt_buf[6];
Alex Elder82a442d2013-05-31 17:40:44 -05006213 bool already = false;
Mike Christie0276dca2016-08-18 18:38:45 +02006214 bool force = false;
Alex Elder0d8189e2013-04-27 09:59:30 -05006215 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006216
Mike Christie0276dca2016-08-18 18:38:45 +02006217 dev_id = -1;
6218 opt_buf[0] = '\0';
6219 sscanf(buf, "%d %5s", &dev_id, opt_buf);
6220 if (dev_id < 0) {
6221 pr_err("dev_id out of range\n");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006222 return -EINVAL;
Mike Christie0276dca2016-08-18 18:38:45 +02006223 }
6224 if (opt_buf[0] != '\0') {
6225 if (!strcmp(opt_buf, "force")) {
6226 force = true;
6227 } else {
6228 pr_err("bad remove option at '%s'\n", opt_buf);
6229 return -EINVAL;
6230 }
6231 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006232
Alex Elder751cc0e2013-05-31 15:17:01 -05006233 ret = -ENOENT;
6234 spin_lock(&rbd_dev_list_lock);
6235 list_for_each(tmp, &rbd_dev_list) {
6236 rbd_dev = list_entry(tmp, struct rbd_device, node);
6237 if (rbd_dev->dev_id == dev_id) {
6238 ret = 0;
6239 break;
6240 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006241 }
Alex Elder751cc0e2013-05-31 15:17:01 -05006242 if (!ret) {
6243 spin_lock_irq(&rbd_dev->lock);
Mike Christie0276dca2016-08-18 18:38:45 +02006244 if (rbd_dev->open_count && !force)
Alex Elder751cc0e2013-05-31 15:17:01 -05006245 ret = -EBUSY;
6246 else
Alex Elder82a442d2013-05-31 17:40:44 -05006247 already = test_and_set_bit(RBD_DEV_FLAG_REMOVING,
6248 &rbd_dev->flags);
Alex Elder751cc0e2013-05-31 15:17:01 -05006249 spin_unlock_irq(&rbd_dev->lock);
6250 }
6251 spin_unlock(&rbd_dev_list_lock);
Alex Elder82a442d2013-05-31 17:40:44 -05006252 if (ret < 0 || already)
Alex Elder1ba0f1e2013-05-31 15:17:01 -05006253 return ret;
Alex Elder751cc0e2013-05-31 15:17:01 -05006254
Mike Christie0276dca2016-08-18 18:38:45 +02006255 if (force) {
6256 /*
6257 * Prevent new IO from being queued and wait for existing
6258 * IO to complete/fail.
6259 */
6260 blk_mq_freeze_queue(rbd_dev->disk->queue);
6261 blk_set_queue_dying(rbd_dev->disk->queue);
6262 }
6263
Ilya Dryomoved95b212016-08-12 16:40:02 +02006264 down_write(&rbd_dev->lock_rwsem);
6265 if (__rbd_is_lock_owner(rbd_dev))
6266 rbd_unlock(rbd_dev);
6267 up_write(&rbd_dev->lock_rwsem);
Ilya Dryomov99d16942016-08-12 16:11:41 +02006268 rbd_unregister_watch(rbd_dev);
Ilya Dryomovfca27062013-12-16 18:02:40 +02006269
Josh Durgin98752012013-08-29 17:26:31 -07006270 /*
6271 * Don't free anything from rbd_dev->disk until after all
6272 * notifies are completely processed. Otherwise
6273 * rbd_bus_del_dev() will race with rbd_watch_cb(), resulting
6274 * in a potential use after free of rbd_dev->disk or rbd_dev.
6275 */
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006276 rbd_dev_device_release(rbd_dev);
Alex Elder8ad42cd2013-04-28 23:32:34 -05006277 rbd_dev_image_release(rbd_dev);
Alex Elderaafb2302012-09-06 16:00:54 -05006278
Alex Elder1ba0f1e2013-05-31 15:17:01 -05006279 return count;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006280}
6281
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006282static ssize_t rbd_remove(struct bus_type *bus,
6283 const char *buf,
6284 size_t count)
6285{
6286 if (single_major)
6287 return -EINVAL;
6288
6289 return do_rbd_remove(bus, buf, count);
6290}
6291
6292static ssize_t rbd_remove_single_major(struct bus_type *bus,
6293 const char *buf,
6294 size_t count)
6295{
6296 return do_rbd_remove(bus, buf, count);
6297}
6298
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006299/*
6300 * create control files in sysfs
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006301 * /sys/bus/rbd/...
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006302 */
6303static int rbd_sysfs_init(void)
6304{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006305 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006306
Alex Elderfed4c142012-02-07 12:03:36 -06006307 ret = device_register(&rbd_root_dev);
Alex Elder21079782012-01-24 10:08:36 -06006308 if (ret < 0)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006309 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006310
Alex Elderfed4c142012-02-07 12:03:36 -06006311 ret = bus_register(&rbd_bus_type);
6312 if (ret < 0)
6313 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006314
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006315 return ret;
6316}
6317
6318static void rbd_sysfs_cleanup(void)
6319{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006320 bus_unregister(&rbd_bus_type);
Alex Elderfed4c142012-02-07 12:03:36 -06006321 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006322}
6323
Alex Elder1c2a9df2013-05-01 12:43:03 -05006324static int rbd_slab_init(void)
6325{
6326 rbd_assert(!rbd_img_request_cache);
Geliang Tang03d94402016-03-13 15:17:32 +08006327 rbd_img_request_cache = KMEM_CACHE(rbd_img_request, 0);
Alex Elder868311b2013-05-01 12:43:03 -05006328 if (!rbd_img_request_cache)
6329 return -ENOMEM;
6330
6331 rbd_assert(!rbd_obj_request_cache);
Geliang Tang03d94402016-03-13 15:17:32 +08006332 rbd_obj_request_cache = KMEM_CACHE(rbd_obj_request, 0);
Alex Elder78c2a442013-05-01 12:43:04 -05006333 if (!rbd_obj_request_cache)
6334 goto out_err;
6335
Ilya Dryomov6c696d82017-01-25 18:16:23 +01006336 return 0;
Alex Elder1c2a9df2013-05-01 12:43:03 -05006337
Ilya Dryomov6c696d82017-01-25 18:16:23 +01006338out_err:
Alex Elder868311b2013-05-01 12:43:03 -05006339 kmem_cache_destroy(rbd_img_request_cache);
6340 rbd_img_request_cache = NULL;
Alex Elder1c2a9df2013-05-01 12:43:03 -05006341 return -ENOMEM;
6342}
6343
6344static void rbd_slab_exit(void)
6345{
Alex Elder868311b2013-05-01 12:43:03 -05006346 rbd_assert(rbd_obj_request_cache);
6347 kmem_cache_destroy(rbd_obj_request_cache);
6348 rbd_obj_request_cache = NULL;
6349
Alex Elder1c2a9df2013-05-01 12:43:03 -05006350 rbd_assert(rbd_img_request_cache);
6351 kmem_cache_destroy(rbd_img_request_cache);
6352 rbd_img_request_cache = NULL;
6353}
6354
Alex Eldercc344fa2013-02-19 12:25:56 -06006355static int __init rbd_init(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006356{
6357 int rc;
6358
Alex Elder1e32d342013-01-30 11:13:33 -06006359 if (!libceph_compatible(NULL)) {
6360 rbd_warn(NULL, "libceph incompatibility (quitting)");
Alex Elder1e32d342013-01-30 11:13:33 -06006361 return -EINVAL;
6362 }
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006363
Alex Elder1c2a9df2013-05-01 12:43:03 -05006364 rc = rbd_slab_init();
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006365 if (rc)
6366 return rc;
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006367
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006368 /*
6369 * The number of active work items is limited by the number of
Ilya Dryomovf77303b2015-04-22 18:28:13 +03006370 * rbd devices * queue depth, so leave @max_active at default.
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006371 */
6372 rbd_wq = alloc_workqueue(RBD_DRV_NAME, WQ_MEM_RECLAIM, 0);
6373 if (!rbd_wq) {
6374 rc = -ENOMEM;
6375 goto err_out_slab;
6376 }
6377
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006378 if (single_major) {
6379 rbd_major = register_blkdev(0, RBD_DRV_NAME);
6380 if (rbd_major < 0) {
6381 rc = rbd_major;
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006382 goto err_out_wq;
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006383 }
6384 }
6385
Alex Elder1c2a9df2013-05-01 12:43:03 -05006386 rc = rbd_sysfs_init();
6387 if (rc)
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006388 goto err_out_blkdev;
Alex Elder1c2a9df2013-05-01 12:43:03 -05006389
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006390 if (single_major)
6391 pr_info("loaded (major %d)\n", rbd_major);
6392 else
6393 pr_info("loaded\n");
6394
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006395 return 0;
6396
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006397err_out_blkdev:
6398 if (single_major)
6399 unregister_blkdev(rbd_major, RBD_DRV_NAME);
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006400err_out_wq:
6401 destroy_workqueue(rbd_wq);
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006402err_out_slab:
6403 rbd_slab_exit();
Alex Elder1c2a9df2013-05-01 12:43:03 -05006404 return rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006405}
6406
Alex Eldercc344fa2013-02-19 12:25:56 -06006407static void __exit rbd_exit(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006408{
Ilya Dryomovffe312c2014-05-20 15:46:04 +04006409 ida_destroy(&rbd_dev_id_ida);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006410 rbd_sysfs_cleanup();
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006411 if (single_major)
6412 unregister_blkdev(rbd_major, RBD_DRV_NAME);
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006413 destroy_workqueue(rbd_wq);
Alex Elder1c2a9df2013-05-01 12:43:03 -05006414 rbd_slab_exit();
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006415}
6416
6417module_init(rbd_init);
6418module_exit(rbd_exit);
6419
Alex Elderd552c612013-05-31 20:13:09 -05006420MODULE_AUTHOR("Alex Elder <elder@inktank.com>");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006421MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
6422MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006423/* following authorship retained from original osdblk.c */
6424MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
6425
Ilya Dryomov90da2582013-12-13 15:28:56 +02006426MODULE_DESCRIPTION("RADOS Block Device (RBD) driver");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006427MODULE_LICENSE("GPL");