blob: 6c094b580eae6c3339e0542f3f340032665bd42e [file] [log] [blame]
Alex Eldere2a58ee2013-04-30 00:44:33 -05001
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002/*
3 rbd.c -- Export ceph rados objects as a Linux block device
4
5
6 based on drivers/block/osdblk.c:
7
8 Copyright 2009 Red Hat, Inc.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; see the file COPYING. If not, write to
21 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22
23
24
Yehuda Sadehdfc56062010-11-19 14:51:04 -080025 For usage instructions, please refer to:
Yehuda Sadeh602adf42010-08-12 16:11:25 -070026
Yehuda Sadehdfc56062010-11-19 14:51:04 -080027 Documentation/ABI/testing/sysfs-bus-rbd
Yehuda Sadeh602adf42010-08-12 16:11:25 -070028
29 */
30
31#include <linux/ceph/libceph.h>
32#include <linux/ceph/osd_client.h>
33#include <linux/ceph/mon_client.h>
Ilya Dryomoved95b212016-08-12 16:40:02 +020034#include <linux/ceph/cls_lock_client.h>
Yehuda Sadeh602adf42010-08-12 16:11:25 -070035#include <linux/ceph/decode.h>
Yehuda Sadeh59c2be12011-03-21 15:10:11 -070036#include <linux/parser.h>
Alex Elder30d1cff2013-05-01 12:43:03 -050037#include <linux/bsearch.h>
Yehuda Sadeh602adf42010-08-12 16:11:25 -070038
39#include <linux/kernel.h>
40#include <linux/device.h>
41#include <linux/module.h>
Christoph Hellwig7ad18af2015-01-13 17:20:04 +010042#include <linux/blk-mq.h>
Yehuda Sadeh602adf42010-08-12 16:11:25 -070043#include <linux/fs.h>
44#include <linux/blkdev.h>
Alex Elder1c2a9df2013-05-01 12:43:03 -050045#include <linux/slab.h>
Ilya Dryomovf8a22fc2013-12-13 15:28:57 +020046#include <linux/idr.h>
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +040047#include <linux/workqueue.h>
Yehuda Sadeh602adf42010-08-12 16:11:25 -070048
49#include "rbd_types.h"
50
Alex Elderaafb2302012-09-06 16:00:54 -050051#define RBD_DEBUG /* Activate rbd_assert() calls */
52
Alex Elder593a9e72012-02-07 12:03:37 -060053/*
54 * The basic unit of block I/O is a sector. It is interpreted in a
55 * number of contexts in Linux (blk, bio, genhd), but the default is
56 * universally 512 bytes. These symbols are just slightly more
57 * meaningful than the bare numbers they represent.
58 */
59#define SECTOR_SHIFT 9
60#define SECTOR_SIZE (1ULL << SECTOR_SHIFT)
61
Alex Eldera2acd002013-05-08 22:50:04 -050062/*
63 * Increment the given counter and return its updated value.
64 * If the counter is already 0 it will not be incremented.
65 * If the counter is already at its maximum value returns
66 * -EINVAL without updating it.
67 */
68static int atomic_inc_return_safe(atomic_t *v)
69{
70 unsigned int counter;
71
72 counter = (unsigned int)__atomic_add_unless(v, 1, 0);
73 if (counter <= (unsigned int)INT_MAX)
74 return (int)counter;
75
76 atomic_dec(v);
77
78 return -EINVAL;
79}
80
81/* Decrement the counter. Return the resulting value, or -EINVAL */
82static int atomic_dec_return_safe(atomic_t *v)
83{
84 int counter;
85
86 counter = atomic_dec_return(v);
87 if (counter >= 0)
88 return counter;
89
90 atomic_inc(v);
91
92 return -EINVAL;
93}
94
Alex Elderf0f8cef2012-01-29 13:57:44 -060095#define RBD_DRV_NAME "rbd"
Yehuda Sadeh602adf42010-08-12 16:11:25 -070096
Ilya Dryomov7e513d42013-12-16 19:26:32 +020097#define RBD_MINORS_PER_MAJOR 256
98#define RBD_SINGLE_MAJOR_PART_SHIFT 4
Yehuda Sadeh602adf42010-08-12 16:11:25 -070099
Ilya Dryomov6d69bb532015-10-11 19:38:00 +0200100#define RBD_MAX_PARENT_CHAIN_LEN 16
101
Alex Elderd4b125e2012-07-03 16:01:19 -0500102#define RBD_SNAP_DEV_NAME_PREFIX "snap_"
103#define RBD_MAX_SNAP_NAME_LEN \
104 (NAME_MAX - (sizeof (RBD_SNAP_DEV_NAME_PREFIX) - 1))
105
Alex Elder35d489f2012-07-03 16:01:19 -0500106#define RBD_MAX_SNAP_COUNT 510 /* allows max snapc to fit in 4KB */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700107
108#define RBD_SNAP_HEAD_NAME "-"
109
Alex Elder9682fc62013-04-30 00:44:33 -0500110#define BAD_SNAP_INDEX U32_MAX /* invalid index into snap array */
111
Alex Elder9e15b772012-10-30 19:40:33 -0500112/* This allows a single page to hold an image name sent by OSD */
113#define RBD_IMAGE_NAME_LEN_MAX (PAGE_SIZE - sizeof (__le32) - 1)
Alex Elder1e130192012-07-03 16:01:19 -0500114#define RBD_IMAGE_ID_LEN_MAX 64
Alex Elder9e15b772012-10-30 19:40:33 -0500115
Alex Elder1e130192012-07-03 16:01:19 -0500116#define RBD_OBJ_PREFIX_LEN_MAX 64
Alex Elder589d30e2012-07-10 20:30:11 -0500117
Ilya Dryomoved95b212016-08-12 16:40:02 +0200118#define RBD_NOTIFY_TIMEOUT 5 /* seconds */
Ilya Dryomov99d16942016-08-12 16:11:41 +0200119#define RBD_RETRY_DELAY msecs_to_jiffies(1000)
120
Alex Elderd8891402012-10-09 13:50:17 -0700121/* Feature bits */
122
Alex Elder5cbf6f122013-04-11 09:29:48 -0500123#define RBD_FEATURE_LAYERING (1<<0)
124#define RBD_FEATURE_STRIPINGV2 (1<<1)
Ilya Dryomoved95b212016-08-12 16:40:02 +0200125#define RBD_FEATURE_EXCLUSIVE_LOCK (1<<2)
Ilya Dryomov7e973322017-01-25 18:16:22 +0100126#define RBD_FEATURE_DATA_POOL (1<<7)
Ilya Dryomoved95b212016-08-12 16:40:02 +0200127#define RBD_FEATURES_ALL (RBD_FEATURE_LAYERING | \
128 RBD_FEATURE_STRIPINGV2 | \
Ilya Dryomov7e973322017-01-25 18:16:22 +0100129 RBD_FEATURE_EXCLUSIVE_LOCK | \
130 RBD_FEATURE_DATA_POOL)
Alex Elderd8891402012-10-09 13:50:17 -0700131
132/* Features supported by this (client software) implementation. */
133
Alex Elder770eba62012-10-25 23:34:40 -0500134#define RBD_FEATURES_SUPPORTED (RBD_FEATURES_ALL)
Alex Elderd8891402012-10-09 13:50:17 -0700135
Alex Elder81a89792012-02-02 08:13:30 -0600136/*
137 * An RBD device name will be "rbd#", where the "rbd" comes from
138 * RBD_DRV_NAME above, and # is a unique integer identifier.
Alex Elder81a89792012-02-02 08:13:30 -0600139 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700140#define DEV_NAME_LEN 32
141
142/*
143 * block device image metadata (in-memory version)
144 */
145struct rbd_image_header {
Alex Elderf35a4de2013-05-06 09:51:29 -0500146 /* These six fields never change for a given rbd image */
Alex Elder849b4262012-07-09 21:04:24 -0500147 char *object_prefix;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700148 __u8 obj_order;
Alex Elderf35a4de2013-05-06 09:51:29 -0500149 u64 stripe_unit;
150 u64 stripe_count;
Ilya Dryomov7e973322017-01-25 18:16:22 +0100151 s64 data_pool_id;
Alex Elderf35a4de2013-05-06 09:51:29 -0500152 u64 features; /* Might be changeable someday? */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700153
Alex Elderf84344f2012-08-31 17:29:51 -0500154 /* The remaining fields need to be updated occasionally */
155 u64 image_size;
156 struct ceph_snap_context *snapc;
Alex Elderf35a4de2013-05-06 09:51:29 -0500157 char *snap_names; /* format 1 only */
158 u64 *snap_sizes; /* format 1 only */
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700159};
160
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500161/*
162 * An rbd image specification.
163 *
164 * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely
Alex Elderc66c6e02012-11-01 08:39:26 -0500165 * identify an image. Each rbd_dev structure includes a pointer to
166 * an rbd_spec structure that encapsulates this identity.
167 *
168 * Each of the id's in an rbd_spec has an associated name. For a
169 * user-mapped image, the names are supplied and the id's associated
170 * with them are looked up. For a layered image, a parent image is
171 * defined by the tuple, and the names are looked up.
172 *
173 * An rbd_dev structure contains a parent_spec pointer which is
174 * non-null if the image it represents is a child in a layered
175 * image. This pointer will refer to the rbd_spec structure used
176 * by the parent rbd_dev for its own identity (i.e., the structure
177 * is shared between the parent and child).
178 *
179 * Since these structures are populated once, during the discovery
180 * phase of image construction, they are effectively immutable so
181 * we make no effort to synchronize access to them.
182 *
183 * Note that code herein does not assume the image name is known (it
184 * could be a null pointer).
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500185 */
186struct rbd_spec {
187 u64 pool_id;
Alex Elderecb4dc22013-04-26 09:43:47 -0500188 const char *pool_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500189
Alex Elderecb4dc22013-04-26 09:43:47 -0500190 const char *image_id;
191 const char *image_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500192
193 u64 snap_id;
Alex Elderecb4dc22013-04-26 09:43:47 -0500194 const char *snap_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500195
196 struct kref kref;
197};
198
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700199/*
Alex Elderf0f8cef2012-01-29 13:57:44 -0600200 * an instance of the client. multiple devices may share an rbd client.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700201 */
202struct rbd_client {
203 struct ceph_client *client;
204 struct kref kref;
205 struct list_head node;
206};
207
Alex Elderbf0d5f502012-11-22 00:00:08 -0600208struct rbd_img_request;
209typedef void (*rbd_img_callback_t)(struct rbd_img_request *);
210
211#define BAD_WHICH U32_MAX /* Good which or bad which, which? */
212
213struct rbd_obj_request;
214typedef void (*rbd_obj_callback_t)(struct rbd_obj_request *);
215
Alex Elder9969ebc2013-01-18 12:31:10 -0600216enum obj_request_type {
217 OBJ_REQUEST_NODATA, OBJ_REQUEST_BIO, OBJ_REQUEST_PAGES
218};
Alex Elderbf0d5f502012-11-22 00:00:08 -0600219
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800220enum obj_operation_type {
221 OBJ_OP_WRITE,
222 OBJ_OP_READ,
Guangliang Zhao90e98c52014-04-01 22:22:16 +0800223 OBJ_OP_DISCARD,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800224};
225
Alex Elder926f9b32013-02-11 12:33:24 -0600226enum obj_req_flags {
227 OBJ_REQ_DONE, /* completion flag: not done = 0, done = 1 */
Alex Elder6365d332013-02-11 12:33:24 -0600228 OBJ_REQ_IMG_DATA, /* object usage: standalone = 0, image = 1 */
Alex Elder5679c592013-02-11 12:33:24 -0600229 OBJ_REQ_KNOWN, /* EXISTS flag valid: no = 0, yes = 1 */
230 OBJ_REQ_EXISTS, /* target exists: no = 0, yes = 1 */
Alex Elder926f9b32013-02-11 12:33:24 -0600231};
232
Alex Elderbf0d5f502012-11-22 00:00:08 -0600233struct rbd_obj_request {
234 const char *object_name;
Ilya Dryomova90bb0c2017-01-25 18:16:23 +0100235 u64 object_no;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600236 u64 offset; /* object start byte */
237 u64 length; /* bytes from offset */
Alex Elder926f9b32013-02-11 12:33:24 -0600238 unsigned long flags;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600239
Alex Elderc5b5ef62013-02-11 12:33:24 -0600240 /*
241 * An object request associated with an image will have its
242 * img_data flag set; a standalone object request will not.
243 *
244 * A standalone object request will have which == BAD_WHICH
245 * and a null obj_request pointer.
246 *
247 * An object request initiated in support of a layered image
248 * object (to check for its existence before a write) will
249 * have which == BAD_WHICH and a non-null obj_request pointer.
250 *
251 * Finally, an object request for rbd image data will have
252 * which != BAD_WHICH, and will have a non-null img_request
253 * pointer. The value of which will be in the range
254 * 0..(img_request->obj_request_count-1).
255 */
256 union {
257 struct rbd_obj_request *obj_request; /* STAT op */
258 struct {
259 struct rbd_img_request *img_request;
260 u64 img_offset;
261 /* links for img_request->obj_requests list */
262 struct list_head links;
263 };
264 };
Alex Elderbf0d5f502012-11-22 00:00:08 -0600265 u32 which; /* posn image request list */
266
267 enum obj_request_type type;
Alex Elder788e2df2013-01-17 12:25:27 -0600268 union {
269 struct bio *bio_list;
270 struct {
271 struct page **pages;
272 u32 page_count;
273 };
274 };
Alex Elder0eefd472013-04-19 15:34:50 -0500275 struct page **copyup_pages;
Alex Elderebda6402013-05-10 16:29:22 -0500276 u32 copyup_page_count;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600277
278 struct ceph_osd_request *osd_req;
279
280 u64 xferred; /* bytes transferred */
Sage Weil1b83bef2013-02-25 16:11:12 -0800281 int result;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600282
283 rbd_obj_callback_t callback;
Alex Elder788e2df2013-01-17 12:25:27 -0600284 struct completion completion;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600285
286 struct kref kref;
287};
288
Alex Elder0c425242013-02-08 09:55:49 -0600289enum img_req_flags {
Alex Elder9849e982013-01-24 16:13:36 -0600290 IMG_REQ_WRITE, /* I/O direction: read = 0, write = 1 */
291 IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */
Alex Elderd0b2e942013-01-24 16:13:36 -0600292 IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */
Guangliang Zhao90e98c52014-04-01 22:22:16 +0800293 IMG_REQ_DISCARD, /* discard: normal = 0, discard request = 1 */
Alex Elder0c425242013-02-08 09:55:49 -0600294};
295
Alex Elderbf0d5f502012-11-22 00:00:08 -0600296struct rbd_img_request {
Alex Elderbf0d5f502012-11-22 00:00:08 -0600297 struct rbd_device *rbd_dev;
298 u64 offset; /* starting image byte offset */
299 u64 length; /* byte count from offset */
Alex Elder0c425242013-02-08 09:55:49 -0600300 unsigned long flags;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600301 union {
Alex Elder9849e982013-01-24 16:13:36 -0600302 u64 snap_id; /* for reads */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600303 struct ceph_snap_context *snapc; /* for writes */
Alex Elder9849e982013-01-24 16:13:36 -0600304 };
305 union {
306 struct request *rq; /* block request */
307 struct rbd_obj_request *obj_request; /* obj req initiator */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600308 };
Alex Elder3d7efd12013-04-19 15:34:50 -0500309 struct page **copyup_pages;
Alex Elderebda6402013-05-10 16:29:22 -0500310 u32 copyup_page_count;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600311 spinlock_t completion_lock;/* protects next_completion */
312 u32 next_completion;
313 rbd_img_callback_t callback;
Alex Elder55f27e02013-04-10 12:34:25 -0500314 u64 xferred;/* aggregate bytes transferred */
Alex Eldera5a337d2013-01-24 16:13:36 -0600315 int result; /* first nonzero obj_request result */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600316
317 u32 obj_request_count;
318 struct list_head obj_requests; /* rbd_obj_request structs */
319
320 struct kref kref;
321};
322
323#define for_each_obj_request(ireq, oreq) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600324 list_for_each_entry(oreq, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600325#define for_each_obj_request_from(ireq, oreq) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600326 list_for_each_entry_from(oreq, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600327#define for_each_obj_request_safe(ireq, oreq, n) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600328 list_for_each_entry_safe_reverse(oreq, n, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600329
Ilya Dryomov99d16942016-08-12 16:11:41 +0200330enum rbd_watch_state {
331 RBD_WATCH_STATE_UNREGISTERED,
332 RBD_WATCH_STATE_REGISTERED,
333 RBD_WATCH_STATE_ERROR,
334};
335
Ilya Dryomoved95b212016-08-12 16:40:02 +0200336enum rbd_lock_state {
337 RBD_LOCK_STATE_UNLOCKED,
338 RBD_LOCK_STATE_LOCKED,
339 RBD_LOCK_STATE_RELEASING,
340};
341
342/* WatchNotify::ClientId */
343struct rbd_client_id {
344 u64 gid;
345 u64 handle;
346};
347
Alex Elderf84344f2012-08-31 17:29:51 -0500348struct rbd_mapping {
Alex Elder99c1f082012-08-30 14:42:15 -0500349 u64 size;
Alex Elder34b13182012-07-13 20:35:12 -0500350 u64 features;
Alex Elderf84344f2012-08-31 17:29:51 -0500351 bool read_only;
352};
353
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700354/*
355 * a single device
356 */
357struct rbd_device {
Alex Elderde71a292012-07-03 16:01:19 -0500358 int dev_id; /* blkdev unique id */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700359
360 int major; /* blkdev assigned major */
Ilya Dryomovdd82fff2013-12-13 15:28:57 +0200361 int minor;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700362 struct gendisk *disk; /* blkdev's gendisk and rq */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700363
Alex Eldera30b71b2012-07-10 20:30:11 -0500364 u32 image_format; /* Either 1 or 2 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700365 struct rbd_client *rbd_client;
366
367 char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */
368
Alex Elderb82d1672013-01-14 12:43:31 -0600369 spinlock_t lock; /* queue, flags, open_count */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700370
371 struct rbd_image_header header;
Alex Elderb82d1672013-01-14 12:43:31 -0600372 unsigned long flags; /* possibly lock protected */
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500373 struct rbd_spec *spec;
Ilya Dryomovd1475432015-06-22 13:24:48 +0300374 struct rbd_options *opts;
Mike Christie0d6d1e9c2016-08-18 18:38:45 +0200375 char *config_info; /* add{,_single_major} string */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700376
Ilya Dryomovc41d13a2016-04-29 20:01:25 +0200377 struct ceph_object_id header_oid;
Ilya Dryomov922dab62016-05-26 01:15:02 +0200378 struct ceph_object_locator header_oloc;
Alex Elder971f8392012-10-25 23:34:41 -0500379
Ilya Dryomov1643dfa2016-08-12 15:45:52 +0200380 struct ceph_file_layout layout; /* used for all rbd requests */
Alex Elder0903e872012-11-14 12:25:19 -0600381
Ilya Dryomov99d16942016-08-12 16:11:41 +0200382 struct mutex watch_mutex;
383 enum rbd_watch_state watch_state;
Ilya Dryomov922dab62016-05-26 01:15:02 +0200384 struct ceph_osd_linger_request *watch_handle;
Ilya Dryomov99d16942016-08-12 16:11:41 +0200385 u64 watch_cookie;
386 struct delayed_work watch_dwork;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700387
Ilya Dryomoved95b212016-08-12 16:40:02 +0200388 struct rw_semaphore lock_rwsem;
389 enum rbd_lock_state lock_state;
390 struct rbd_client_id owner_cid;
391 struct work_struct acquired_lock_work;
392 struct work_struct released_lock_work;
393 struct delayed_work lock_dwork;
394 struct work_struct unlock_work;
395 wait_queue_head_t lock_waitq;
396
Ilya Dryomov1643dfa2016-08-12 15:45:52 +0200397 struct workqueue_struct *task_wq;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700398
Alex Elder86b00e02012-10-25 23:34:42 -0500399 struct rbd_spec *parent_spec;
400 u64 parent_overlap;
Alex Eldera2acd002013-05-08 22:50:04 -0500401 atomic_t parent_ref;
Alex Elder2f82ee52012-10-30 19:40:33 -0500402 struct rbd_device *parent;
Alex Elder86b00e02012-10-25 23:34:42 -0500403
Christoph Hellwig7ad18af2015-01-13 17:20:04 +0100404 /* Block layer tags. */
405 struct blk_mq_tag_set tag_set;
406
Josh Durginc6666012011-11-21 17:11:12 -0800407 /* protects updating the header */
408 struct rw_semaphore header_rwsem;
Alex Elderf84344f2012-08-31 17:29:51 -0500409
410 struct rbd_mapping mapping;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700411
412 struct list_head node;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800413
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800414 /* sysfs related */
415 struct device dev;
Alex Elderb82d1672013-01-14 12:43:31 -0600416 unsigned long open_count; /* protected by lock */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800417};
418
Alex Elderb82d1672013-01-14 12:43:31 -0600419/*
Ilya Dryomov87c0fde2016-09-29 13:41:05 +0200420 * Flag bits for rbd_dev->flags:
421 * - REMOVING (which is coupled with rbd_dev->open_count) is protected
422 * by rbd_dev->lock
423 * - BLACKLISTED is protected by rbd_dev->lock_rwsem
Alex Elderb82d1672013-01-14 12:43:31 -0600424 */
Alex Elder6d292902013-01-14 12:43:31 -0600425enum rbd_dev_flags {
426 RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */
Alex Elderb82d1672013-01-14 12:43:31 -0600427 RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */
Ilya Dryomov87c0fde2016-09-29 13:41:05 +0200428 RBD_DEV_FLAG_BLACKLISTED, /* our ceph_client is blacklisted */
Alex Elder6d292902013-01-14 12:43:31 -0600429};
430
Alex Eldercfbf6372013-05-31 17:40:45 -0500431static DEFINE_MUTEX(client_mutex); /* Serialize client creation */
Alex Eldere124a82f2012-01-29 13:57:44 -0600432
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700433static LIST_HEAD(rbd_dev_list); /* devices */
Alex Eldere124a82f2012-01-29 13:57:44 -0600434static DEFINE_SPINLOCK(rbd_dev_list_lock);
435
Alex Elder432b8582012-01-29 13:57:44 -0600436static LIST_HEAD(rbd_client_list); /* clients */
437static DEFINE_SPINLOCK(rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700438
Alex Elder78c2a442013-05-01 12:43:04 -0500439/* Slab caches for frequently-allocated structures */
440
Alex Elder1c2a9df2013-05-01 12:43:03 -0500441static struct kmem_cache *rbd_img_request_cache;
Alex Elder868311b2013-05-01 12:43:03 -0500442static struct kmem_cache *rbd_obj_request_cache;
Alex Elder78c2a442013-05-01 12:43:04 -0500443static struct kmem_cache *rbd_segment_name_cache;
Alex Elder1c2a9df2013-05-01 12:43:03 -0500444
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200445static int rbd_major;
Ilya Dryomovf8a22fc2013-12-13 15:28:57 +0200446static DEFINE_IDA(rbd_dev_id_ida);
447
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +0400448static struct workqueue_struct *rbd_wq;
449
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200450/*
451 * Default to false for now, as single-major requires >= 0.75 version of
452 * userspace rbd utility.
453 */
454static bool single_major = false;
455module_param(single_major, bool, S_IRUGO);
456MODULE_PARM_DESC(single_major, "Use a single major number for all rbd devices (default: false)");
457
Alex Elder3d7efd12013-04-19 15:34:50 -0500458static int rbd_img_request_submit(struct rbd_img_request *img_request);
459
Alex Elderf0f8cef2012-01-29 13:57:44 -0600460static ssize_t rbd_add(struct bus_type *bus, const char *buf,
461 size_t count);
462static ssize_t rbd_remove(struct bus_type *bus, const char *buf,
463 size_t count);
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200464static ssize_t rbd_add_single_major(struct bus_type *bus, const char *buf,
465 size_t count);
466static ssize_t rbd_remove_single_major(struct bus_type *bus, const char *buf,
467 size_t count);
Ilya Dryomov6d69bb532015-10-11 19:38:00 +0200468static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth);
Alex Eldera2acd002013-05-08 22:50:04 -0500469static void rbd_spec_put(struct rbd_spec *spec);
Alex Elderf0f8cef2012-01-29 13:57:44 -0600470
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200471static int rbd_dev_id_to_minor(int dev_id)
472{
Ilya Dryomov7e513d42013-12-16 19:26:32 +0200473 return dev_id << RBD_SINGLE_MAJOR_PART_SHIFT;
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200474}
475
476static int minor_to_rbd_dev_id(int minor)
477{
Ilya Dryomov7e513d42013-12-16 19:26:32 +0200478 return minor >> RBD_SINGLE_MAJOR_PART_SHIFT;
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200479}
480
Ilya Dryomoved95b212016-08-12 16:40:02 +0200481static bool rbd_is_lock_supported(struct rbd_device *rbd_dev)
482{
483 return (rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK) &&
484 rbd_dev->spec->snap_id == CEPH_NOSNAP &&
485 !rbd_dev->mapping.read_only;
486}
487
488static bool __rbd_is_lock_owner(struct rbd_device *rbd_dev)
489{
490 return rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED ||
491 rbd_dev->lock_state == RBD_LOCK_STATE_RELEASING;
492}
493
494static bool rbd_is_lock_owner(struct rbd_device *rbd_dev)
495{
496 bool is_lock_owner;
497
498 down_read(&rbd_dev->lock_rwsem);
499 is_lock_owner = __rbd_is_lock_owner(rbd_dev);
500 up_read(&rbd_dev->lock_rwsem);
501 return is_lock_owner;
502}
503
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700504static BUS_ATTR(add, S_IWUSR, NULL, rbd_add);
505static BUS_ATTR(remove, S_IWUSR, NULL, rbd_remove);
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200506static BUS_ATTR(add_single_major, S_IWUSR, NULL, rbd_add_single_major);
507static BUS_ATTR(remove_single_major, S_IWUSR, NULL, rbd_remove_single_major);
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700508
509static struct attribute *rbd_bus_attrs[] = {
510 &bus_attr_add.attr,
511 &bus_attr_remove.attr,
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200512 &bus_attr_add_single_major.attr,
513 &bus_attr_remove_single_major.attr,
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700514 NULL,
Alex Elderf0f8cef2012-01-29 13:57:44 -0600515};
Ilya Dryomov92c76dc2013-12-13 15:28:57 +0200516
517static umode_t rbd_bus_is_visible(struct kobject *kobj,
518 struct attribute *attr, int index)
519{
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200520 if (!single_major &&
521 (attr == &bus_attr_add_single_major.attr ||
522 attr == &bus_attr_remove_single_major.attr))
523 return 0;
524
Ilya Dryomov92c76dc2013-12-13 15:28:57 +0200525 return attr->mode;
526}
527
528static const struct attribute_group rbd_bus_group = {
529 .attrs = rbd_bus_attrs,
530 .is_visible = rbd_bus_is_visible,
531};
532__ATTRIBUTE_GROUPS(rbd_bus);
Alex Elderf0f8cef2012-01-29 13:57:44 -0600533
534static struct bus_type rbd_bus_type = {
535 .name = "rbd",
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700536 .bus_groups = rbd_bus_groups,
Alex Elderf0f8cef2012-01-29 13:57:44 -0600537};
538
539static void rbd_root_dev_release(struct device *dev)
540{
541}
542
543static struct device rbd_root_dev = {
544 .init_name = "rbd",
545 .release = rbd_root_dev_release,
546};
547
Alex Elder06ecc6c2012-11-01 10:17:15 -0500548static __printf(2, 3)
549void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...)
550{
551 struct va_format vaf;
552 va_list args;
553
554 va_start(args, fmt);
555 vaf.fmt = fmt;
556 vaf.va = &args;
557
558 if (!rbd_dev)
559 printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf);
560 else if (rbd_dev->disk)
561 printk(KERN_WARNING "%s: %s: %pV\n",
562 RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf);
563 else if (rbd_dev->spec && rbd_dev->spec->image_name)
564 printk(KERN_WARNING "%s: image %s: %pV\n",
565 RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf);
566 else if (rbd_dev->spec && rbd_dev->spec->image_id)
567 printk(KERN_WARNING "%s: id %s: %pV\n",
568 RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf);
569 else /* punt */
570 printk(KERN_WARNING "%s: rbd_dev %p: %pV\n",
571 RBD_DRV_NAME, rbd_dev, &vaf);
572 va_end(args);
573}
574
Alex Elderaafb2302012-09-06 16:00:54 -0500575#ifdef RBD_DEBUG
576#define rbd_assert(expr) \
577 if (unlikely(!(expr))) { \
578 printk(KERN_ERR "\nAssertion failure in %s() " \
579 "at line %d:\n\n" \
580 "\trbd_assert(%s);\n\n", \
581 __func__, __LINE__, #expr); \
582 BUG(); \
583 }
584#else /* !RBD_DEBUG */
585# define rbd_assert(expr) ((void) 0)
586#endif /* !RBD_DEBUG */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800587
Ilya Dryomov27617132015-07-16 17:36:11 +0300588static void rbd_osd_copyup_callback(struct rbd_obj_request *obj_request);
Alex Elderb454e362013-04-19 15:34:50 -0500589static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request);
Alex Elder05a46af2013-04-26 15:44:36 -0500590static void rbd_img_parent_read(struct rbd_obj_request *obj_request);
591static void rbd_dev_remove_parent(struct rbd_device *rbd_dev);
Alex Elder8b3e1a52013-01-24 16:13:36 -0600592
Alex Eldercc4a38bd2013-04-30 00:44:33 -0500593static int rbd_dev_refresh(struct rbd_device *rbd_dev);
Alex Elder2df3fac2013-05-06 09:51:30 -0500594static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev);
Ilya Dryomova720ae02014-07-23 17:11:19 +0400595static int rbd_dev_header_info(struct rbd_device *rbd_dev);
Ilya Dryomove8f59b52014-07-24 10:42:13 +0400596static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev);
Alex Elder54cac612013-04-30 00:44:33 -0500597static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
598 u64 snap_id);
Alex Elder2ad3d712013-04-30 00:44:33 -0500599static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
600 u8 *order, u64 *snap_size);
601static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
602 u64 *snap_features);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700603
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700604static int rbd_open(struct block_device *bdev, fmode_t mode)
605{
Alex Elderf0f8cef2012-01-29 13:57:44 -0600606 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
Alex Elderb82d1672013-01-14 12:43:31 -0600607 bool removing = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700608
Alex Elderf84344f2012-08-31 17:29:51 -0500609 if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700610 return -EROFS;
611
Alex Eldera14ea262013-02-05 13:23:12 -0600612 spin_lock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600613 if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags))
614 removing = true;
615 else
616 rbd_dev->open_count++;
Alex Eldera14ea262013-02-05 13:23:12 -0600617 spin_unlock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600618 if (removing)
619 return -ENOENT;
620
Alex Elderc3e946c2012-11-16 09:29:16 -0600621 (void) get_device(&rbd_dev->dev);
Alex Elder340c7a22012-08-10 13:12:07 -0700622
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700623 return 0;
624}
625
Al Virodb2a1442013-05-05 21:52:57 -0400626static void rbd_release(struct gendisk *disk, fmode_t mode)
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800627{
628 struct rbd_device *rbd_dev = disk->private_data;
Alex Elderb82d1672013-01-14 12:43:31 -0600629 unsigned long open_count_before;
630
Alex Eldera14ea262013-02-05 13:23:12 -0600631 spin_lock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600632 open_count_before = rbd_dev->open_count--;
Alex Eldera14ea262013-02-05 13:23:12 -0600633 spin_unlock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600634 rbd_assert(open_count_before > 0);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800635
Alex Elderc3e946c2012-11-16 09:29:16 -0600636 put_device(&rbd_dev->dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800637}
638
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800639static int rbd_ioctl_set_ro(struct rbd_device *rbd_dev, unsigned long arg)
640{
Josh Durgin77f33c02013-09-30 17:09:54 -0700641 int ret = 0;
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800642 int val;
643 bool ro;
Josh Durgin77f33c02013-09-30 17:09:54 -0700644 bool ro_changed = false;
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800645
Josh Durgin77f33c02013-09-30 17:09:54 -0700646 /* get_user() may sleep, so call it before taking rbd_dev->lock */
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800647 if (get_user(val, (int __user *)(arg)))
648 return -EFAULT;
649
650 ro = val ? true : false;
651 /* Snapshot doesn't allow to write*/
652 if (rbd_dev->spec->snap_id != CEPH_NOSNAP && !ro)
653 return -EROFS;
654
Josh Durgin77f33c02013-09-30 17:09:54 -0700655 spin_lock_irq(&rbd_dev->lock);
656 /* prevent others open this device */
657 if (rbd_dev->open_count > 1) {
658 ret = -EBUSY;
659 goto out;
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800660 }
661
Josh Durgin77f33c02013-09-30 17:09:54 -0700662 if (rbd_dev->mapping.read_only != ro) {
663 rbd_dev->mapping.read_only = ro;
664 ro_changed = true;
665 }
666
667out:
668 spin_unlock_irq(&rbd_dev->lock);
669 /* set_disk_ro() may sleep, so call it after releasing rbd_dev->lock */
670 if (ret == 0 && ro_changed)
671 set_disk_ro(rbd_dev->disk, ro ? 1 : 0);
672
673 return ret;
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800674}
675
676static int rbd_ioctl(struct block_device *bdev, fmode_t mode,
677 unsigned int cmd, unsigned long arg)
678{
679 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
680 int ret = 0;
681
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800682 switch (cmd) {
683 case BLKROSET:
684 ret = rbd_ioctl_set_ro(rbd_dev, arg);
685 break;
686 default:
687 ret = -ENOTTY;
688 }
689
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800690 return ret;
691}
692
693#ifdef CONFIG_COMPAT
694static int rbd_compat_ioctl(struct block_device *bdev, fmode_t mode,
695 unsigned int cmd, unsigned long arg)
696{
697 return rbd_ioctl(bdev, mode, cmd, arg);
698}
699#endif /* CONFIG_COMPAT */
700
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700701static const struct block_device_operations rbd_bd_ops = {
702 .owner = THIS_MODULE,
703 .open = rbd_open,
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800704 .release = rbd_release,
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800705 .ioctl = rbd_ioctl,
706#ifdef CONFIG_COMPAT
707 .compat_ioctl = rbd_compat_ioctl,
708#endif
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700709};
710
711/*
Alex Elder7262cfc2013-05-16 15:04:20 -0500712 * Initialize an rbd client instance. Success or not, this function
Alex Eldercfbf6372013-05-31 17:40:45 -0500713 * consumes ceph_opts. Caller holds client_mutex.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700714 */
Alex Elderf8c38922012-08-10 13:12:07 -0700715static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700716{
717 struct rbd_client *rbdc;
718 int ret = -ENOMEM;
719
Alex Elder37206ee2013-02-20 17:32:08 -0600720 dout("%s:\n", __func__);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700721 rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL);
722 if (!rbdc)
723 goto out_opt;
724
725 kref_init(&rbdc->kref);
726 INIT_LIST_HEAD(&rbdc->node);
727
Alex Elder43ae4702012-07-03 16:01:18 -0500728 rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700729 if (IS_ERR(rbdc->client))
Alex Elder08f75462013-05-29 11:19:00 -0500730 goto out_rbdc;
Alex Elder43ae4702012-07-03 16:01:18 -0500731 ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700732
733 ret = ceph_open_session(rbdc->client);
734 if (ret < 0)
Alex Elder08f75462013-05-29 11:19:00 -0500735 goto out_client;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700736
Alex Elder432b8582012-01-29 13:57:44 -0600737 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700738 list_add_tail(&rbdc->node, &rbd_client_list);
Alex Elder432b8582012-01-29 13:57:44 -0600739 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700740
Alex Elder37206ee2013-02-20 17:32:08 -0600741 dout("%s: rbdc %p\n", __func__, rbdc);
Alex Elderbc534d82012-01-29 13:57:44 -0600742
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700743 return rbdc;
Alex Elder08f75462013-05-29 11:19:00 -0500744out_client:
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700745 ceph_destroy_client(rbdc->client);
Alex Elder08f75462013-05-29 11:19:00 -0500746out_rbdc:
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700747 kfree(rbdc);
748out_opt:
Alex Elder43ae4702012-07-03 16:01:18 -0500749 if (ceph_opts)
750 ceph_destroy_options(ceph_opts);
Alex Elder37206ee2013-02-20 17:32:08 -0600751 dout("%s: error %d\n", __func__, ret);
752
Vasiliy Kulikov28f259b2010-09-26 12:59:37 +0400753 return ERR_PTR(ret);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700754}
755
Alex Elder2f82ee52012-10-30 19:40:33 -0500756static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc)
757{
758 kref_get(&rbdc->kref);
759
760 return rbdc;
761}
762
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700763/*
Alex Elder1f7ba332012-08-10 13:12:07 -0700764 * Find a ceph client with specific addr and configuration. If
765 * found, bump its reference count.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700766 */
Alex Elder1f7ba332012-08-10 13:12:07 -0700767static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700768{
769 struct rbd_client *client_node;
Alex Elder1f7ba332012-08-10 13:12:07 -0700770 bool found = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700771
Alex Elder43ae4702012-07-03 16:01:18 -0500772 if (ceph_opts->flags & CEPH_OPT_NOSHARE)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700773 return NULL;
774
Alex Elder1f7ba332012-08-10 13:12:07 -0700775 spin_lock(&rbd_client_list_lock);
776 list_for_each_entry(client_node, &rbd_client_list, node) {
777 if (!ceph_compare_options(ceph_opts, client_node->client)) {
Alex Elder2f82ee52012-10-30 19:40:33 -0500778 __rbd_get_client(client_node);
779
Alex Elder1f7ba332012-08-10 13:12:07 -0700780 found = true;
781 break;
782 }
783 }
784 spin_unlock(&rbd_client_list_lock);
785
786 return found ? client_node : NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700787}
788
789/*
Ilya Dryomov210c1042015-06-22 13:24:48 +0300790 * (Per device) rbd map options
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700791 */
792enum {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300793 Opt_queue_depth,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700794 Opt_last_int,
795 /* int args above */
796 Opt_last_string,
797 /* string args above */
Alex Eldercc0538b2012-08-10 13:12:07 -0700798 Opt_read_only,
799 Opt_read_write,
Ilya Dryomov80de1912016-09-20 14:23:17 +0200800 Opt_lock_on_read,
Ilya Dryomov210c1042015-06-22 13:24:48 +0300801 Opt_err
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700802};
803
Alex Elder43ae4702012-07-03 16:01:18 -0500804static match_table_t rbd_opts_tokens = {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300805 {Opt_queue_depth, "queue_depth=%d"},
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700806 /* int args above */
807 /* string args above */
Alex Elderbe466c12012-10-22 11:31:26 -0500808 {Opt_read_only, "read_only"},
Alex Eldercc0538b2012-08-10 13:12:07 -0700809 {Opt_read_only, "ro"}, /* Alternate spelling */
810 {Opt_read_write, "read_write"},
811 {Opt_read_write, "rw"}, /* Alternate spelling */
Ilya Dryomov80de1912016-09-20 14:23:17 +0200812 {Opt_lock_on_read, "lock_on_read"},
Ilya Dryomov210c1042015-06-22 13:24:48 +0300813 {Opt_err, NULL}
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700814};
815
Alex Elder98571b52013-01-20 14:44:42 -0600816struct rbd_options {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300817 int queue_depth;
Alex Elder98571b52013-01-20 14:44:42 -0600818 bool read_only;
Ilya Dryomov80de1912016-09-20 14:23:17 +0200819 bool lock_on_read;
Alex Elder98571b52013-01-20 14:44:42 -0600820};
821
Ilya Dryomovb5584182015-06-23 16:21:19 +0300822#define RBD_QUEUE_DEPTH_DEFAULT BLKDEV_MAX_RQ
Alex Elder98571b52013-01-20 14:44:42 -0600823#define RBD_READ_ONLY_DEFAULT false
Ilya Dryomov80de1912016-09-20 14:23:17 +0200824#define RBD_LOCK_ON_READ_DEFAULT false
Alex Elder98571b52013-01-20 14:44:42 -0600825
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700826static int parse_rbd_opts_token(char *c, void *private)
827{
Alex Elder43ae4702012-07-03 16:01:18 -0500828 struct rbd_options *rbd_opts = private;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700829 substring_t argstr[MAX_OPT_ARGS];
830 int token, intval, ret;
831
Alex Elder43ae4702012-07-03 16:01:18 -0500832 token = match_token(c, rbd_opts_tokens, argstr);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700833 if (token < Opt_last_int) {
834 ret = match_int(&argstr[0], &intval);
835 if (ret < 0) {
Ilya Dryomov210c1042015-06-22 13:24:48 +0300836 pr_err("bad mount option arg (not int) at '%s'\n", c);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700837 return ret;
838 }
839 dout("got int token %d val %d\n", token, intval);
840 } else if (token > Opt_last_int && token < Opt_last_string) {
Ilya Dryomov210c1042015-06-22 13:24:48 +0300841 dout("got string token %d val %s\n", token, argstr[0].from);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700842 } else {
843 dout("got token %d\n", token);
844 }
845
846 switch (token) {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300847 case Opt_queue_depth:
848 if (intval < 1) {
849 pr_err("queue_depth out of range\n");
850 return -EINVAL;
851 }
852 rbd_opts->queue_depth = intval;
853 break;
Alex Eldercc0538b2012-08-10 13:12:07 -0700854 case Opt_read_only:
855 rbd_opts->read_only = true;
856 break;
857 case Opt_read_write:
858 rbd_opts->read_only = false;
859 break;
Ilya Dryomov80de1912016-09-20 14:23:17 +0200860 case Opt_lock_on_read:
861 rbd_opts->lock_on_read = true;
862 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700863 default:
Ilya Dryomov210c1042015-06-22 13:24:48 +0300864 /* libceph prints "bad option" msg */
865 return -EINVAL;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700866 }
Ilya Dryomov210c1042015-06-22 13:24:48 +0300867
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700868 return 0;
869}
870
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800871static char* obj_op_name(enum obj_operation_type op_type)
872{
873 switch (op_type) {
874 case OBJ_OP_READ:
875 return "read";
876 case OBJ_OP_WRITE:
877 return "write";
Guangliang Zhao90e98c52014-04-01 22:22:16 +0800878 case OBJ_OP_DISCARD:
879 return "discard";
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800880 default:
881 return "???";
882 }
883}
884
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700885/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700886 * Get a ceph client with specific addr and configuration, if one does
Alex Elder7262cfc2013-05-16 15:04:20 -0500887 * not exist create it. Either way, ceph_opts is consumed by this
888 * function.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700889 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500890static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700891{
Alex Elderf8c38922012-08-10 13:12:07 -0700892 struct rbd_client *rbdc;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700893
Alex Eldercfbf6372013-05-31 17:40:45 -0500894 mutex_lock_nested(&client_mutex, SINGLE_DEPTH_NESTING);
Alex Elder1f7ba332012-08-10 13:12:07 -0700895 rbdc = rbd_client_find(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500896 if (rbdc) /* using an existing client */
Alex Elder43ae4702012-07-03 16:01:18 -0500897 ceph_destroy_options(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500898 else
Alex Elderf8c38922012-08-10 13:12:07 -0700899 rbdc = rbd_client_create(ceph_opts);
Alex Eldercfbf6372013-05-31 17:40:45 -0500900 mutex_unlock(&client_mutex);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700901
Alex Elder9d3997f2012-10-25 23:34:42 -0500902 return rbdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700903}
904
905/*
906 * Destroy ceph client
Alex Elderd23a4b32012-01-29 13:57:43 -0600907 *
Alex Elder432b8582012-01-29 13:57:44 -0600908 * Caller must hold rbd_client_list_lock.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700909 */
910static void rbd_client_release(struct kref *kref)
911{
912 struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
913
Alex Elder37206ee2013-02-20 17:32:08 -0600914 dout("%s: rbdc %p\n", __func__, rbdc);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500915 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700916 list_del(&rbdc->node);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500917 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700918
919 ceph_destroy_client(rbdc->client);
920 kfree(rbdc);
921}
922
923/*
924 * Drop reference to ceph client node. If it's not referenced anymore, release
925 * it.
926 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500927static void rbd_put_client(struct rbd_client *rbdc)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700928{
Alex Elderc53d5892012-10-25 23:34:42 -0500929 if (rbdc)
930 kref_put(&rbdc->kref, rbd_client_release);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700931}
932
Alex Eldera30b71b2012-07-10 20:30:11 -0500933static bool rbd_image_format_valid(u32 image_format)
934{
935 return image_format == 1 || image_format == 2;
936}
937
Alex Elder8e94af82012-07-25 09:32:40 -0500938static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
939{
Alex Elder103a1502012-08-02 11:29:45 -0500940 size_t size;
941 u32 snap_count;
942
943 /* The header has to start with the magic rbd header text */
944 if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT)))
945 return false;
946
Alex Elderdb2388b2012-10-20 22:17:27 -0500947 /* The bio layer requires at least sector-sized I/O */
948
949 if (ondisk->options.order < SECTOR_SHIFT)
950 return false;
951
952 /* If we use u64 in a few spots we may be able to loosen this */
953
954 if (ondisk->options.order > 8 * sizeof (int) - 1)
955 return false;
956
Alex Elder103a1502012-08-02 11:29:45 -0500957 /*
958 * The size of a snapshot header has to fit in a size_t, and
959 * that limits the number of snapshots.
960 */
961 snap_count = le32_to_cpu(ondisk->snap_count);
962 size = SIZE_MAX - sizeof (struct ceph_snap_context);
963 if (snap_count > size / sizeof (__le64))
964 return false;
965
966 /*
967 * Not only that, but the size of the entire the snapshot
968 * header must also be representable in a size_t.
969 */
970 size -= snap_count * sizeof (__le64);
971 if ((u64) size < le64_to_cpu(ondisk->snap_names_len))
972 return false;
973
974 return true;
Alex Elder8e94af82012-07-25 09:32:40 -0500975}
976
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700977/*
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +0100978 * returns the size of an object in the image
979 */
980static u32 rbd_obj_bytes(struct rbd_image_header *header)
981{
982 return 1U << header->obj_order;
983}
984
Ilya Dryomov263423f2017-01-25 18:16:22 +0100985static void rbd_init_layout(struct rbd_device *rbd_dev)
986{
987 if (rbd_dev->header.stripe_unit == 0 ||
988 rbd_dev->header.stripe_count == 0) {
989 rbd_dev->header.stripe_unit = rbd_obj_bytes(&rbd_dev->header);
990 rbd_dev->header.stripe_count = 1;
991 }
992
993 rbd_dev->layout.stripe_unit = rbd_dev->header.stripe_unit;
994 rbd_dev->layout.stripe_count = rbd_dev->header.stripe_count;
995 rbd_dev->layout.object_size = rbd_obj_bytes(&rbd_dev->header);
Ilya Dryomov7e973322017-01-25 18:16:22 +0100996 rbd_dev->layout.pool_id = rbd_dev->header.data_pool_id == CEPH_NOPOOL ?
997 rbd_dev->spec->pool_id : rbd_dev->header.data_pool_id;
Ilya Dryomov263423f2017-01-25 18:16:22 +0100998 RCU_INIT_POINTER(rbd_dev->layout.pool_ns, NULL);
999}
1000
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +01001001/*
Alex Elderbb23e372013-05-06 09:51:29 -05001002 * Fill an rbd image header with information from the given format 1
1003 * on-disk header.
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001004 */
Alex Elder662518b2013-05-06 09:51:29 -05001005static int rbd_header_from_disk(struct rbd_device *rbd_dev,
Alex Elder4156d992012-08-02 11:29:46 -05001006 struct rbd_image_header_ondisk *ondisk)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001007{
Alex Elder662518b2013-05-06 09:51:29 -05001008 struct rbd_image_header *header = &rbd_dev->header;
Alex Elderbb23e372013-05-06 09:51:29 -05001009 bool first_time = header->object_prefix == NULL;
1010 struct ceph_snap_context *snapc;
1011 char *object_prefix = NULL;
1012 char *snap_names = NULL;
1013 u64 *snap_sizes = NULL;
Alex Elderccece232012-07-10 20:30:10 -05001014 u32 snap_count;
Alex Elderbb23e372013-05-06 09:51:29 -05001015 int ret = -ENOMEM;
Alex Elder621901d2012-08-23 23:22:06 -05001016 u32 i;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001017
Alex Elderbb23e372013-05-06 09:51:29 -05001018 /* Allocate this now to avoid having to handle failure below */
1019
1020 if (first_time) {
Ilya Dryomov848d7962017-01-25 18:16:21 +01001021 object_prefix = kstrndup(ondisk->object_prefix,
1022 sizeof(ondisk->object_prefix),
1023 GFP_KERNEL);
Alex Elderbb23e372013-05-06 09:51:29 -05001024 if (!object_prefix)
1025 return -ENOMEM;
Alex Elderbb23e372013-05-06 09:51:29 -05001026 }
1027
1028 /* Allocate the snapshot context and fill it in */
Alex Elder6a523252012-07-19 17:12:59 -05001029
Alex Elder103a1502012-08-02 11:29:45 -05001030 snap_count = le32_to_cpu(ondisk->snap_count);
Alex Elderbb23e372013-05-06 09:51:29 -05001031 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
1032 if (!snapc)
1033 goto out_err;
1034 snapc->seq = le64_to_cpu(ondisk->snap_seq);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001035 if (snap_count) {
Alex Elderbb23e372013-05-06 09:51:29 -05001036 struct rbd_image_snap_ondisk *snaps;
Alex Elderf785cc12012-08-23 23:22:06 -05001037 u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len);
1038
Alex Elderbb23e372013-05-06 09:51:29 -05001039 /* We'll keep a copy of the snapshot names... */
Alex Elder621901d2012-08-23 23:22:06 -05001040
Alex Elderbb23e372013-05-06 09:51:29 -05001041 if (snap_names_len > (u64)SIZE_MAX)
1042 goto out_2big;
1043 snap_names = kmalloc(snap_names_len, GFP_KERNEL);
1044 if (!snap_names)
Alex Elder6a523252012-07-19 17:12:59 -05001045 goto out_err;
Alex Elderbb23e372013-05-06 09:51:29 -05001046
1047 /* ...as well as the array of their sizes. */
Markus Elfring88a25a52016-09-11 12:21:25 +02001048 snap_sizes = kmalloc_array(snap_count,
1049 sizeof(*header->snap_sizes),
1050 GFP_KERNEL);
Alex Elderbb23e372013-05-06 09:51:29 -05001051 if (!snap_sizes)
1052 goto out_err;
1053
Alex Elderf785cc12012-08-23 23:22:06 -05001054 /*
Alex Elderbb23e372013-05-06 09:51:29 -05001055 * Copy the names, and fill in each snapshot's id
1056 * and size.
1057 *
Alex Elder99a41eb2013-05-06 09:51:30 -05001058 * Note that rbd_dev_v1_header_info() guarantees the
Alex Elderbb23e372013-05-06 09:51:29 -05001059 * ondisk buffer we're working with has
Alex Elderf785cc12012-08-23 23:22:06 -05001060 * snap_names_len bytes beyond the end of the
1061 * snapshot id array, this memcpy() is safe.
1062 */
Alex Elderbb23e372013-05-06 09:51:29 -05001063 memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len);
1064 snaps = ondisk->snaps;
1065 for (i = 0; i < snap_count; i++) {
1066 snapc->snaps[i] = le64_to_cpu(snaps[i].id);
1067 snap_sizes[i] = le64_to_cpu(snaps[i].image_size);
1068 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001069 }
Alex Elder849b4262012-07-09 21:04:24 -05001070
Alex Elderbb23e372013-05-06 09:51:29 -05001071 /* We won't fail any more, fill in the header */
Alex Elder6a523252012-07-19 17:12:59 -05001072
Alex Elderbb23e372013-05-06 09:51:29 -05001073 if (first_time) {
1074 header->object_prefix = object_prefix;
1075 header->obj_order = ondisk->options.order;
Ilya Dryomov263423f2017-01-25 18:16:22 +01001076 rbd_init_layout(rbd_dev);
Alex Elder662518b2013-05-06 09:51:29 -05001077 } else {
1078 ceph_put_snap_context(header->snapc);
1079 kfree(header->snap_names);
1080 kfree(header->snap_sizes);
Alex Elderbb23e372013-05-06 09:51:29 -05001081 }
1082
1083 /* The remaining fields always get updated (when we refresh) */
Alex Elder621901d2012-08-23 23:22:06 -05001084
Alex Elderf84344f2012-08-31 17:29:51 -05001085 header->image_size = le64_to_cpu(ondisk->image_size);
Alex Elderbb23e372013-05-06 09:51:29 -05001086 header->snapc = snapc;
1087 header->snap_names = snap_names;
1088 header->snap_sizes = snap_sizes;
Alex Elder468521c2013-04-26 09:43:47 -05001089
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001090 return 0;
Alex Elderbb23e372013-05-06 09:51:29 -05001091out_2big:
1092 ret = -EIO;
Alex Elder6a523252012-07-19 17:12:59 -05001093out_err:
Alex Elderbb23e372013-05-06 09:51:29 -05001094 kfree(snap_sizes);
1095 kfree(snap_names);
1096 ceph_put_snap_context(snapc);
1097 kfree(object_prefix);
Alex Elderccece232012-07-10 20:30:10 -05001098
Alex Elderbb23e372013-05-06 09:51:29 -05001099 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001100}
1101
Alex Elder9682fc62013-04-30 00:44:33 -05001102static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which)
1103{
1104 const char *snap_name;
1105
1106 rbd_assert(which < rbd_dev->header.snapc->num_snaps);
1107
1108 /* Skip over names until we find the one we are looking for */
1109
1110 snap_name = rbd_dev->header.snap_names;
1111 while (which--)
1112 snap_name += strlen(snap_name) + 1;
1113
1114 return kstrdup(snap_name, GFP_KERNEL);
1115}
1116
Alex Elder30d1cff2013-05-01 12:43:03 -05001117/*
1118 * Snapshot id comparison function for use with qsort()/bsearch().
1119 * Note that result is for snapshots in *descending* order.
1120 */
1121static int snapid_compare_reverse(const void *s1, const void *s2)
1122{
1123 u64 snap_id1 = *(u64 *)s1;
1124 u64 snap_id2 = *(u64 *)s2;
1125
1126 if (snap_id1 < snap_id2)
1127 return 1;
1128 return snap_id1 == snap_id2 ? 0 : -1;
1129}
1130
1131/*
1132 * Search a snapshot context to see if the given snapshot id is
1133 * present.
1134 *
1135 * Returns the position of the snapshot id in the array if it's found,
1136 * or BAD_SNAP_INDEX otherwise.
1137 *
1138 * Note: The snapshot array is in kept sorted (by the osd) in
1139 * reverse order, highest snapshot id first.
1140 */
Alex Elder9682fc62013-04-30 00:44:33 -05001141static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id)
1142{
1143 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
Alex Elder30d1cff2013-05-01 12:43:03 -05001144 u64 *found;
Alex Elder9682fc62013-04-30 00:44:33 -05001145
Alex Elder30d1cff2013-05-01 12:43:03 -05001146 found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps,
1147 sizeof (snap_id), snapid_compare_reverse);
Alex Elder9682fc62013-04-30 00:44:33 -05001148
Alex Elder30d1cff2013-05-01 12:43:03 -05001149 return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX;
Alex Elder9682fc62013-04-30 00:44:33 -05001150}
1151
Alex Elder2ad3d712013-04-30 00:44:33 -05001152static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev,
1153 u64 snap_id)
Alex Elder54cac612013-04-30 00:44:33 -05001154{
1155 u32 which;
Josh Durginda6a6b62013-09-04 17:57:31 -07001156 const char *snap_name;
Alex Elder54cac612013-04-30 00:44:33 -05001157
1158 which = rbd_dev_snap_index(rbd_dev, snap_id);
1159 if (which == BAD_SNAP_INDEX)
Josh Durginda6a6b62013-09-04 17:57:31 -07001160 return ERR_PTR(-ENOENT);
Alex Elder54cac612013-04-30 00:44:33 -05001161
Josh Durginda6a6b62013-09-04 17:57:31 -07001162 snap_name = _rbd_dev_v1_snap_name(rbd_dev, which);
1163 return snap_name ? snap_name : ERR_PTR(-ENOMEM);
Alex Elder54cac612013-04-30 00:44:33 -05001164}
1165
Alex Elder9e15b772012-10-30 19:40:33 -05001166static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id)
1167{
Alex Elder9e15b772012-10-30 19:40:33 -05001168 if (snap_id == CEPH_NOSNAP)
1169 return RBD_SNAP_HEAD_NAME;
1170
Alex Elder54cac612013-04-30 00:44:33 -05001171 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1172 if (rbd_dev->image_format == 1)
1173 return rbd_dev_v1_snap_name(rbd_dev, snap_id);
Alex Elder9e15b772012-10-30 19:40:33 -05001174
Alex Elder54cac612013-04-30 00:44:33 -05001175 return rbd_dev_v2_snap_name(rbd_dev, snap_id);
Alex Elder9e15b772012-10-30 19:40:33 -05001176}
1177
Alex Elder2ad3d712013-04-30 00:44:33 -05001178static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
1179 u64 *snap_size)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001180{
Alex Elder2ad3d712013-04-30 00:44:33 -05001181 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1182 if (snap_id == CEPH_NOSNAP) {
1183 *snap_size = rbd_dev->header.image_size;
1184 } else if (rbd_dev->image_format == 1) {
1185 u32 which;
Alex Elder00f1f362012-02-07 12:03:36 -06001186
Alex Elder2ad3d712013-04-30 00:44:33 -05001187 which = rbd_dev_snap_index(rbd_dev, snap_id);
1188 if (which == BAD_SNAP_INDEX)
1189 return -ENOENT;
Alex Elder00f1f362012-02-07 12:03:36 -06001190
Alex Elder2ad3d712013-04-30 00:44:33 -05001191 *snap_size = rbd_dev->header.snap_sizes[which];
1192 } else {
1193 u64 size = 0;
1194 int ret;
1195
1196 ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size);
1197 if (ret)
1198 return ret;
1199
1200 *snap_size = size;
1201 }
1202 return 0;
1203}
1204
1205static int rbd_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
1206 u64 *snap_features)
1207{
1208 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1209 if (snap_id == CEPH_NOSNAP) {
1210 *snap_features = rbd_dev->header.features;
1211 } else if (rbd_dev->image_format == 1) {
1212 *snap_features = 0; /* No features for format 1 */
1213 } else {
1214 u64 features = 0;
1215 int ret;
1216
1217 ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, &features);
1218 if (ret)
1219 return ret;
1220
1221 *snap_features = features;
1222 }
1223 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001224}
1225
Alex Elderd1cf5782013-04-27 09:59:30 -05001226static int rbd_dev_mapping_set(struct rbd_device *rbd_dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001227{
Alex Elder8f4b7d92013-05-06 07:40:30 -05001228 u64 snap_id = rbd_dev->spec->snap_id;
Alex Elder2ad3d712013-04-30 00:44:33 -05001229 u64 size = 0;
1230 u64 features = 0;
1231 int ret;
Alex Elder8b0241f2013-04-25 23:15:08 -05001232
Alex Elder2ad3d712013-04-30 00:44:33 -05001233 ret = rbd_snap_size(rbd_dev, snap_id, &size);
1234 if (ret)
1235 return ret;
1236 ret = rbd_snap_features(rbd_dev, snap_id, &features);
1237 if (ret)
1238 return ret;
1239
1240 rbd_dev->mapping.size = size;
1241 rbd_dev->mapping.features = features;
1242
Alex Elder8b0241f2013-04-25 23:15:08 -05001243 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001244}
1245
Alex Elderd1cf5782013-04-27 09:59:30 -05001246static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev)
1247{
1248 rbd_dev->mapping.size = 0;
1249 rbd_dev->mapping.features = 0;
Alex Elder200a6a82013-04-28 23:32:34 -05001250}
1251
Himangi Saraogi7d5079a2014-07-24 03:17:07 +05301252static void rbd_segment_name_free(const char *name)
1253{
1254 /* The explicit cast here is needed to drop the const qualifier */
1255
1256 kmem_cache_free(rbd_segment_name_cache, (void *)name);
1257}
1258
Alex Elder98571b52013-01-20 14:44:42 -06001259static const char *rbd_segment_name(struct rbd_device *rbd_dev, u64 offset)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001260{
Ilya Dryomov223768d2017-01-25 18:16:23 +01001261 const char *name_format = rbd_dev->image_format == 1 ?
1262 RBD_V1_DATA_FORMAT : RBD_V2_DATA_FORMAT;
Alex Elder65ccfe22012-08-09 10:33:26 -07001263 char *name;
1264 u64 segment;
1265 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001266
Alex Elder78c2a442013-05-01 12:43:04 -05001267 name = kmem_cache_alloc(rbd_segment_name_cache, GFP_NOIO);
Alex Elder65ccfe22012-08-09 10:33:26 -07001268 if (!name)
1269 return NULL;
1270 segment = offset >> rbd_dev->header.obj_order;
Ilya Dryomov2d0ebc52014-01-27 17:40:18 +02001271 ret = snprintf(name, CEPH_MAX_OID_NAME_LEN + 1, name_format,
Alex Elder65ccfe22012-08-09 10:33:26 -07001272 rbd_dev->header.object_prefix, segment);
Ilya Dryomov2d0ebc52014-01-27 17:40:18 +02001273 if (ret < 0 || ret > CEPH_MAX_OID_NAME_LEN) {
Alex Elder65ccfe22012-08-09 10:33:26 -07001274 pr_err("error formatting segment name for #%llu (%d)\n",
1275 segment, ret);
Himangi Saraogi7d5079a2014-07-24 03:17:07 +05301276 rbd_segment_name_free(name);
Alex Elder65ccfe22012-08-09 10:33:26 -07001277 name = NULL;
1278 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001279
Alex Elder65ccfe22012-08-09 10:33:26 -07001280 return name;
1281}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001282
Alex Elder65ccfe22012-08-09 10:33:26 -07001283static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset)
1284{
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +01001285 u64 segment_size = rbd_obj_bytes(&rbd_dev->header);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001286
Alex Elder65ccfe22012-08-09 10:33:26 -07001287 return offset & (segment_size - 1);
1288}
1289
1290static u64 rbd_segment_length(struct rbd_device *rbd_dev,
1291 u64 offset, u64 length)
1292{
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +01001293 u64 segment_size = rbd_obj_bytes(&rbd_dev->header);
Alex Elder65ccfe22012-08-09 10:33:26 -07001294
1295 offset &= segment_size - 1;
1296
Alex Elderaafb2302012-09-06 16:00:54 -05001297 rbd_assert(length <= U64_MAX - offset);
Alex Elder65ccfe22012-08-09 10:33:26 -07001298 if (offset + length > segment_size)
1299 length = segment_size - offset;
1300
1301 return length;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001302}
1303
1304/*
1305 * bio helpers
1306 */
1307
1308static void bio_chain_put(struct bio *chain)
1309{
1310 struct bio *tmp;
1311
1312 while (chain) {
1313 tmp = chain;
1314 chain = chain->bi_next;
1315 bio_put(tmp);
1316 }
1317}
1318
1319/*
1320 * zeros a bio chain, starting at specific offset
1321 */
1322static void zero_bio_chain(struct bio *chain, int start_ofs)
1323{
Kent Overstreet79886132013-11-23 17:19:00 -08001324 struct bio_vec bv;
1325 struct bvec_iter iter;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001326 unsigned long flags;
1327 void *buf;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001328 int pos = 0;
1329
1330 while (chain) {
Kent Overstreet79886132013-11-23 17:19:00 -08001331 bio_for_each_segment(bv, chain, iter) {
1332 if (pos + bv.bv_len > start_ofs) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001333 int remainder = max(start_ofs - pos, 0);
Kent Overstreet79886132013-11-23 17:19:00 -08001334 buf = bvec_kmap_irq(&bv, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001335 memset(buf + remainder, 0,
Kent Overstreet79886132013-11-23 17:19:00 -08001336 bv.bv_len - remainder);
1337 flush_dcache_page(bv.bv_page);
Dan Carpenter85b5aaa2010-10-11 21:15:11 +02001338 bvec_kunmap_irq(buf, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001339 }
Kent Overstreet79886132013-11-23 17:19:00 -08001340 pos += bv.bv_len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001341 }
1342
1343 chain = chain->bi_next;
1344 }
1345}
1346
1347/*
Alex Elderb9434c52013-04-19 15:34:50 -05001348 * similar to zero_bio_chain(), zeros data defined by a page array,
1349 * starting at the given byte offset from the start of the array and
1350 * continuing up to the given end offset. The pages array is
1351 * assumed to be big enough to hold all bytes up to the end.
1352 */
1353static void zero_pages(struct page **pages, u64 offset, u64 end)
1354{
1355 struct page **page = &pages[offset >> PAGE_SHIFT];
1356
1357 rbd_assert(end > offset);
1358 rbd_assert(end - offset <= (u64)SIZE_MAX);
1359 while (offset < end) {
1360 size_t page_offset;
1361 size_t length;
1362 unsigned long flags;
1363 void *kaddr;
1364
Geert Uytterhoeven491205a2013-05-13 20:35:37 -05001365 page_offset = offset & ~PAGE_MASK;
1366 length = min_t(size_t, PAGE_SIZE - page_offset, end - offset);
Alex Elderb9434c52013-04-19 15:34:50 -05001367 local_irq_save(flags);
1368 kaddr = kmap_atomic(*page);
1369 memset(kaddr + page_offset, 0, length);
Alex Eldere2156052013-05-22 20:54:25 -05001370 flush_dcache_page(*page);
Alex Elderb9434c52013-04-19 15:34:50 -05001371 kunmap_atomic(kaddr);
1372 local_irq_restore(flags);
1373
1374 offset += length;
1375 page++;
1376 }
1377}
1378
1379/*
Alex Elderf7760da2012-10-20 22:17:27 -05001380 * Clone a portion of a bio, starting at the given byte offset
1381 * and continuing for the number of bytes indicated.
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001382 */
Alex Elderf7760da2012-10-20 22:17:27 -05001383static struct bio *bio_clone_range(struct bio *bio_src,
1384 unsigned int offset,
1385 unsigned int len,
1386 gfp_t gfpmask)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001387{
Alex Elderf7760da2012-10-20 22:17:27 -05001388 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001389
Kent Overstreet5341a6272013-08-07 14:31:11 -07001390 bio = bio_clone(bio_src, gfpmask);
Alex Elderf7760da2012-10-20 22:17:27 -05001391 if (!bio)
1392 return NULL; /* ENOMEM */
1393
Kent Overstreet5341a6272013-08-07 14:31:11 -07001394 bio_advance(bio, offset);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001395 bio->bi_iter.bi_size = len;
Alex Elder542582f2012-08-09 10:33:25 -07001396
Alex Elderf7760da2012-10-20 22:17:27 -05001397 return bio;
1398}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001399
Alex Elderf7760da2012-10-20 22:17:27 -05001400/*
1401 * Clone a portion of a bio chain, starting at the given byte offset
1402 * into the first bio in the source chain and continuing for the
1403 * number of bytes indicated. The result is another bio chain of
1404 * exactly the given length, or a null pointer on error.
1405 *
1406 * The bio_src and offset parameters are both in-out. On entry they
1407 * refer to the first source bio and the offset into that bio where
1408 * the start of data to be cloned is located.
1409 *
1410 * On return, bio_src is updated to refer to the bio in the source
1411 * chain that contains first un-cloned byte, and *offset will
1412 * contain the offset of that byte within that bio.
1413 */
1414static struct bio *bio_chain_clone_range(struct bio **bio_src,
1415 unsigned int *offset,
1416 unsigned int len,
1417 gfp_t gfpmask)
1418{
1419 struct bio *bi = *bio_src;
1420 unsigned int off = *offset;
1421 struct bio *chain = NULL;
1422 struct bio **end;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001423
Alex Elderf7760da2012-10-20 22:17:27 -05001424 /* Build up a chain of clone bios up to the limit */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001425
Kent Overstreet4f024f32013-10-11 15:44:27 -07001426 if (!bi || off >= bi->bi_iter.bi_size || !len)
Alex Elderf7760da2012-10-20 22:17:27 -05001427 return NULL; /* Nothing to clone */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001428
Alex Elderf7760da2012-10-20 22:17:27 -05001429 end = &chain;
1430 while (len) {
1431 unsigned int bi_size;
1432 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001433
Alex Elderf5400b72012-11-01 10:17:15 -05001434 if (!bi) {
1435 rbd_warn(NULL, "bio_chain exhausted with %u left", len);
Alex Elderf7760da2012-10-20 22:17:27 -05001436 goto out_err; /* EINVAL; ran out of bio's */
Alex Elderf5400b72012-11-01 10:17:15 -05001437 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07001438 bi_size = min_t(unsigned int, bi->bi_iter.bi_size - off, len);
Alex Elderf7760da2012-10-20 22:17:27 -05001439 bio = bio_clone_range(bi, off, bi_size, gfpmask);
1440 if (!bio)
1441 goto out_err; /* ENOMEM */
1442
1443 *end = bio;
1444 end = &bio->bi_next;
1445
1446 off += bi_size;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001447 if (off == bi->bi_iter.bi_size) {
Alex Elderf7760da2012-10-20 22:17:27 -05001448 bi = bi->bi_next;
1449 off = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001450 }
Alex Elderf7760da2012-10-20 22:17:27 -05001451 len -= bi_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001452 }
Alex Elderf7760da2012-10-20 22:17:27 -05001453 *bio_src = bi;
1454 *offset = off;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001455
Alex Elderf7760da2012-10-20 22:17:27 -05001456 return chain;
1457out_err:
1458 bio_chain_put(chain);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001459
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001460 return NULL;
1461}
1462
Alex Elder926f9b32013-02-11 12:33:24 -06001463/*
1464 * The default/initial value for all object request flags is 0. For
1465 * each flag, once its value is set to 1 it is never reset to 0
1466 * again.
1467 */
Alex Elder6365d332013-02-11 12:33:24 -06001468static void obj_request_img_data_set(struct rbd_obj_request *obj_request)
1469{
1470 if (test_and_set_bit(OBJ_REQ_IMG_DATA, &obj_request->flags)) {
Alex Elder6365d332013-02-11 12:33:24 -06001471 struct rbd_device *rbd_dev;
1472
Alex Elder57acbaa2013-02-11 12:33:24 -06001473 rbd_dev = obj_request->img_request->rbd_dev;
Ilya Dryomov9584d502014-07-11 12:11:20 +04001474 rbd_warn(rbd_dev, "obj_request %p already marked img_data",
Alex Elder6365d332013-02-11 12:33:24 -06001475 obj_request);
1476 }
1477}
1478
1479static bool obj_request_img_data_test(struct rbd_obj_request *obj_request)
1480{
1481 smp_mb();
1482 return test_bit(OBJ_REQ_IMG_DATA, &obj_request->flags) != 0;
1483}
1484
Alex Elder57acbaa2013-02-11 12:33:24 -06001485static void obj_request_done_set(struct rbd_obj_request *obj_request)
1486{
1487 if (test_and_set_bit(OBJ_REQ_DONE, &obj_request->flags)) {
1488 struct rbd_device *rbd_dev = NULL;
1489
1490 if (obj_request_img_data_test(obj_request))
1491 rbd_dev = obj_request->img_request->rbd_dev;
Ilya Dryomov9584d502014-07-11 12:11:20 +04001492 rbd_warn(rbd_dev, "obj_request %p already marked done",
Alex Elder57acbaa2013-02-11 12:33:24 -06001493 obj_request);
1494 }
1495}
1496
1497static bool obj_request_done_test(struct rbd_obj_request *obj_request)
1498{
1499 smp_mb();
1500 return test_bit(OBJ_REQ_DONE, &obj_request->flags) != 0;
1501}
1502
Alex Elder5679c592013-02-11 12:33:24 -06001503/*
1504 * This sets the KNOWN flag after (possibly) setting the EXISTS
1505 * flag. The latter is set based on the "exists" value provided.
1506 *
1507 * Note that for our purposes once an object exists it never goes
1508 * away again. It's possible that the response from two existence
1509 * checks are separated by the creation of the target object, and
1510 * the first ("doesn't exist") response arrives *after* the second
1511 * ("does exist"). In that case we ignore the second one.
1512 */
1513static void obj_request_existence_set(struct rbd_obj_request *obj_request,
1514 bool exists)
1515{
1516 if (exists)
1517 set_bit(OBJ_REQ_EXISTS, &obj_request->flags);
1518 set_bit(OBJ_REQ_KNOWN, &obj_request->flags);
1519 smp_mb();
1520}
1521
1522static bool obj_request_known_test(struct rbd_obj_request *obj_request)
1523{
1524 smp_mb();
1525 return test_bit(OBJ_REQ_KNOWN, &obj_request->flags) != 0;
1526}
1527
1528static bool obj_request_exists_test(struct rbd_obj_request *obj_request)
1529{
1530 smp_mb();
1531 return test_bit(OBJ_REQ_EXISTS, &obj_request->flags) != 0;
1532}
1533
Ilya Dryomov96385562014-06-10 13:53:29 +04001534static bool obj_request_overlaps_parent(struct rbd_obj_request *obj_request)
1535{
1536 struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev;
1537
1538 return obj_request->img_offset <
1539 round_up(rbd_dev->parent_overlap, rbd_obj_bytes(&rbd_dev->header));
1540}
1541
Alex Elderbf0d5f502012-11-22 00:00:08 -06001542static void rbd_obj_request_get(struct rbd_obj_request *obj_request)
1543{
Alex Elder37206ee2013-02-20 17:32:08 -06001544 dout("%s: obj %p (was %d)\n", __func__, obj_request,
1545 atomic_read(&obj_request->kref.refcount));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001546 kref_get(&obj_request->kref);
1547}
1548
1549static void rbd_obj_request_destroy(struct kref *kref);
1550static void rbd_obj_request_put(struct rbd_obj_request *obj_request)
1551{
1552 rbd_assert(obj_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001553 dout("%s: obj %p (was %d)\n", __func__, obj_request,
1554 atomic_read(&obj_request->kref.refcount));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001555 kref_put(&obj_request->kref, rbd_obj_request_destroy);
1556}
1557
Alex Elder0f2d5be2014-04-26 14:21:44 +04001558static void rbd_img_request_get(struct rbd_img_request *img_request)
1559{
1560 dout("%s: img %p (was %d)\n", __func__, img_request,
1561 atomic_read(&img_request->kref.refcount));
1562 kref_get(&img_request->kref);
1563}
1564
Alex Eldere93f3152013-05-08 22:50:04 -05001565static bool img_request_child_test(struct rbd_img_request *img_request);
1566static void rbd_parent_request_destroy(struct kref *kref);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001567static void rbd_img_request_destroy(struct kref *kref);
1568static void rbd_img_request_put(struct rbd_img_request *img_request)
1569{
1570 rbd_assert(img_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001571 dout("%s: img %p (was %d)\n", __func__, img_request,
1572 atomic_read(&img_request->kref.refcount));
Alex Eldere93f3152013-05-08 22:50:04 -05001573 if (img_request_child_test(img_request))
1574 kref_put(&img_request->kref, rbd_parent_request_destroy);
1575 else
1576 kref_put(&img_request->kref, rbd_img_request_destroy);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001577}
1578
1579static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request,
1580 struct rbd_obj_request *obj_request)
1581{
Alex Elder25dcf952013-01-25 17:08:55 -06001582 rbd_assert(obj_request->img_request == NULL);
1583
Alex Elderb155e862013-04-15 14:50:37 -05001584 /* Image request now owns object's original reference */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001585 obj_request->img_request = img_request;
Alex Elder25dcf952013-01-25 17:08:55 -06001586 obj_request->which = img_request->obj_request_count;
Alex Elder6365d332013-02-11 12:33:24 -06001587 rbd_assert(!obj_request_img_data_test(obj_request));
1588 obj_request_img_data_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001589 rbd_assert(obj_request->which != BAD_WHICH);
Alex Elder25dcf952013-01-25 17:08:55 -06001590 img_request->obj_request_count++;
1591 list_add_tail(&obj_request->links, &img_request->obj_requests);
Alex Elder37206ee2013-02-20 17:32:08 -06001592 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1593 obj_request->which);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001594}
1595
1596static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request,
1597 struct rbd_obj_request *obj_request)
1598{
1599 rbd_assert(obj_request->which != BAD_WHICH);
Alex Elder25dcf952013-01-25 17:08:55 -06001600
Alex Elder37206ee2013-02-20 17:32:08 -06001601 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1602 obj_request->which);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001603 list_del(&obj_request->links);
Alex Elder25dcf952013-01-25 17:08:55 -06001604 rbd_assert(img_request->obj_request_count > 0);
1605 img_request->obj_request_count--;
1606 rbd_assert(obj_request->which == img_request->obj_request_count);
1607 obj_request->which = BAD_WHICH;
Alex Elder6365d332013-02-11 12:33:24 -06001608 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001609 rbd_assert(obj_request->img_request == img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001610 obj_request->img_request = NULL;
Alex Elder25dcf952013-01-25 17:08:55 -06001611 obj_request->callback = NULL;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001612 rbd_obj_request_put(obj_request);
1613}
1614
1615static bool obj_request_type_valid(enum obj_request_type type)
1616{
1617 switch (type) {
Alex Elder9969ebc2013-01-18 12:31:10 -06001618 case OBJ_REQUEST_NODATA:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001619 case OBJ_REQUEST_BIO:
Alex Elder788e2df2013-01-17 12:25:27 -06001620 case OBJ_REQUEST_PAGES:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001621 return true;
1622 default:
1623 return false;
1624 }
1625}
1626
Ilya Dryomov4a17dad2016-09-13 21:08:10 +02001627static void rbd_img_obj_callback(struct rbd_obj_request *obj_request);
1628
Ilya Dryomov980917f2016-09-12 18:59:42 +02001629static void rbd_obj_request_submit(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001630{
Ilya Dryomov980917f2016-09-12 18:59:42 +02001631 struct ceph_osd_request *osd_req = obj_request->osd_req;
1632
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01001633 dout("%s %p object_no %016llx %llu~%llu osd_req %p\n", __func__,
1634 obj_request, obj_request->object_no, obj_request->offset,
Ilya Dryomov67e2b652017-01-25 18:16:22 +01001635 obj_request->length, osd_req);
Ilya Dryomov4a17dad2016-09-13 21:08:10 +02001636 if (obj_request_img_data_test(obj_request)) {
1637 WARN_ON(obj_request->callback != rbd_img_obj_callback);
1638 rbd_img_request_get(obj_request->img_request);
1639 }
Ilya Dryomov980917f2016-09-12 18:59:42 +02001640 ceph_osdc_start_request(osd_req->r_osdc, osd_req, false);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001641}
1642
1643static void rbd_img_request_complete(struct rbd_img_request *img_request)
1644{
Alex Elder55f27e02013-04-10 12:34:25 -05001645
Alex Elder37206ee2013-02-20 17:32:08 -06001646 dout("%s: img %p\n", __func__, img_request);
Alex Elder55f27e02013-04-10 12:34:25 -05001647
1648 /*
1649 * If no error occurred, compute the aggregate transfer
1650 * count for the image request. We could instead use
1651 * atomic64_cmpxchg() to update it as each object request
1652 * completes; not clear which way is better off hand.
1653 */
1654 if (!img_request->result) {
1655 struct rbd_obj_request *obj_request;
1656 u64 xferred = 0;
1657
1658 for_each_obj_request(img_request, obj_request)
1659 xferred += obj_request->xferred;
1660 img_request->xferred = xferred;
1661 }
1662
Alex Elderbf0d5f502012-11-22 00:00:08 -06001663 if (img_request->callback)
1664 img_request->callback(img_request);
1665 else
1666 rbd_img_request_put(img_request);
1667}
1668
Alex Elder0c425242013-02-08 09:55:49 -06001669/*
1670 * The default/initial value for all image request flags is 0. Each
1671 * is conditionally set to 1 at image request initialization time
1672 * and currently never change thereafter.
1673 */
1674static void img_request_write_set(struct rbd_img_request *img_request)
1675{
1676 set_bit(IMG_REQ_WRITE, &img_request->flags);
1677 smp_mb();
1678}
1679
1680static bool img_request_write_test(struct rbd_img_request *img_request)
1681{
1682 smp_mb();
1683 return test_bit(IMG_REQ_WRITE, &img_request->flags) != 0;
1684}
1685
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001686/*
1687 * Set the discard flag when the img_request is an discard request
1688 */
1689static void img_request_discard_set(struct rbd_img_request *img_request)
1690{
1691 set_bit(IMG_REQ_DISCARD, &img_request->flags);
1692 smp_mb();
1693}
1694
1695static bool img_request_discard_test(struct rbd_img_request *img_request)
1696{
1697 smp_mb();
1698 return test_bit(IMG_REQ_DISCARD, &img_request->flags) != 0;
1699}
1700
Alex Elder9849e982013-01-24 16:13:36 -06001701static void img_request_child_set(struct rbd_img_request *img_request)
1702{
1703 set_bit(IMG_REQ_CHILD, &img_request->flags);
1704 smp_mb();
1705}
1706
Alex Eldere93f3152013-05-08 22:50:04 -05001707static void img_request_child_clear(struct rbd_img_request *img_request)
1708{
1709 clear_bit(IMG_REQ_CHILD, &img_request->flags);
1710 smp_mb();
1711}
1712
Alex Elder9849e982013-01-24 16:13:36 -06001713static bool img_request_child_test(struct rbd_img_request *img_request)
1714{
1715 smp_mb();
1716 return test_bit(IMG_REQ_CHILD, &img_request->flags) != 0;
1717}
1718
Alex Elderd0b2e942013-01-24 16:13:36 -06001719static void img_request_layered_set(struct rbd_img_request *img_request)
1720{
1721 set_bit(IMG_REQ_LAYERED, &img_request->flags);
1722 smp_mb();
1723}
1724
Alex Eldera2acd002013-05-08 22:50:04 -05001725static void img_request_layered_clear(struct rbd_img_request *img_request)
1726{
1727 clear_bit(IMG_REQ_LAYERED, &img_request->flags);
1728 smp_mb();
1729}
1730
Alex Elderd0b2e942013-01-24 16:13:36 -06001731static bool img_request_layered_test(struct rbd_img_request *img_request)
1732{
1733 smp_mb();
1734 return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0;
1735}
1736
Josh Durgin3b434a2a2014-04-04 17:32:15 -07001737static enum obj_operation_type
1738rbd_img_request_op_type(struct rbd_img_request *img_request)
1739{
1740 if (img_request_write_test(img_request))
1741 return OBJ_OP_WRITE;
1742 else if (img_request_discard_test(img_request))
1743 return OBJ_OP_DISCARD;
1744 else
1745 return OBJ_OP_READ;
1746}
1747
Alex Elder6e2a4502013-03-27 09:16:30 -05001748static void
1749rbd_img_obj_request_read_callback(struct rbd_obj_request *obj_request)
1750{
Alex Elderb9434c52013-04-19 15:34:50 -05001751 u64 xferred = obj_request->xferred;
1752 u64 length = obj_request->length;
1753
Alex Elder6e2a4502013-03-27 09:16:30 -05001754 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1755 obj_request, obj_request->img_request, obj_request->result,
Alex Elderb9434c52013-04-19 15:34:50 -05001756 xferred, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001757 /*
Josh Durgin17c1cc12013-08-26 17:55:38 -07001758 * ENOENT means a hole in the image. We zero-fill the entire
1759 * length of the request. A short read also implies zero-fill
1760 * to the end of the request. An error requires the whole
1761 * length of the request to be reported finished with an error
1762 * to the block layer. In each case we update the xferred
1763 * count to indicate the whole request was satisfied.
Alex Elder6e2a4502013-03-27 09:16:30 -05001764 */
Alex Elderb9434c52013-04-19 15:34:50 -05001765 rbd_assert(obj_request->type != OBJ_REQUEST_NODATA);
Alex Elder6e2a4502013-03-27 09:16:30 -05001766 if (obj_request->result == -ENOENT) {
Alex Elderb9434c52013-04-19 15:34:50 -05001767 if (obj_request->type == OBJ_REQUEST_BIO)
1768 zero_bio_chain(obj_request->bio_list, 0);
1769 else
1770 zero_pages(obj_request->pages, 0, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001771 obj_request->result = 0;
Alex Elderb9434c52013-04-19 15:34:50 -05001772 } else if (xferred < length && !obj_request->result) {
1773 if (obj_request->type == OBJ_REQUEST_BIO)
1774 zero_bio_chain(obj_request->bio_list, xferred);
1775 else
1776 zero_pages(obj_request->pages, xferred, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001777 }
Josh Durgin17c1cc12013-08-26 17:55:38 -07001778 obj_request->xferred = length;
Alex Elder6e2a4502013-03-27 09:16:30 -05001779 obj_request_done_set(obj_request);
1780}
1781
Alex Elderbf0d5f502012-11-22 00:00:08 -06001782static void rbd_obj_request_complete(struct rbd_obj_request *obj_request)
1783{
Alex Elder37206ee2013-02-20 17:32:08 -06001784 dout("%s: obj %p cb %p\n", __func__, obj_request,
1785 obj_request->callback);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001786 if (obj_request->callback)
1787 obj_request->callback(obj_request);
Alex Elder788e2df2013-01-17 12:25:27 -06001788 else
1789 complete_all(&obj_request->completion);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001790}
1791
Ilya Dryomov0dcc6852016-09-26 15:43:52 +02001792static void rbd_obj_request_error(struct rbd_obj_request *obj_request, int err)
1793{
1794 obj_request->result = err;
1795 obj_request->xferred = 0;
1796 /*
1797 * kludge - mirror rbd_obj_request_submit() to match a put in
1798 * rbd_img_obj_callback()
1799 */
1800 if (obj_request_img_data_test(obj_request)) {
1801 WARN_ON(obj_request->callback != rbd_img_obj_callback);
1802 rbd_img_request_get(obj_request->img_request);
1803 }
1804 obj_request_done_set(obj_request);
1805 rbd_obj_request_complete(obj_request);
1806}
1807
Alex Elderc47f9372013-02-26 14:23:07 -06001808static void rbd_osd_read_callback(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001809{
Alex Elder57acbaa2013-02-11 12:33:24 -06001810 struct rbd_img_request *img_request = NULL;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001811 struct rbd_device *rbd_dev = NULL;
Alex Elder57acbaa2013-02-11 12:33:24 -06001812 bool layered = false;
1813
1814 if (obj_request_img_data_test(obj_request)) {
1815 img_request = obj_request->img_request;
1816 layered = img_request && img_request_layered_test(img_request);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001817 rbd_dev = img_request->rbd_dev;
Alex Elder57acbaa2013-02-11 12:33:24 -06001818 }
Alex Elder8b3e1a52013-01-24 16:13:36 -06001819
1820 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1821 obj_request, img_request, obj_request->result,
1822 obj_request->xferred, obj_request->length);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001823 if (layered && obj_request->result == -ENOENT &&
1824 obj_request->img_offset < rbd_dev->parent_overlap)
Alex Elder8b3e1a52013-01-24 16:13:36 -06001825 rbd_img_parent_read(obj_request);
1826 else if (img_request)
Alex Elder6e2a4502013-03-27 09:16:30 -05001827 rbd_img_obj_request_read_callback(obj_request);
1828 else
1829 obj_request_done_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001830}
1831
Alex Elderc47f9372013-02-26 14:23:07 -06001832static void rbd_osd_write_callback(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001833{
Sage Weil1b83bef2013-02-25 16:11:12 -08001834 dout("%s: obj %p result %d %llu\n", __func__, obj_request,
1835 obj_request->result, obj_request->length);
1836 /*
Alex Elder8b3e1a52013-01-24 16:13:36 -06001837 * There is no such thing as a successful short write. Set
1838 * it to our originally-requested length.
Sage Weil1b83bef2013-02-25 16:11:12 -08001839 */
1840 obj_request->xferred = obj_request->length;
Alex Elder07741302013-02-05 23:41:50 -06001841 obj_request_done_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001842}
1843
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001844static void rbd_osd_discard_callback(struct rbd_obj_request *obj_request)
1845{
1846 dout("%s: obj %p result %d %llu\n", __func__, obj_request,
1847 obj_request->result, obj_request->length);
1848 /*
1849 * There is no such thing as a successful short discard. Set
1850 * it to our originally-requested length.
1851 */
1852 obj_request->xferred = obj_request->length;
Josh Durgind0265de2014-04-07 16:54:10 -07001853 /* discarding a non-existent object is not a problem */
1854 if (obj_request->result == -ENOENT)
1855 obj_request->result = 0;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001856 obj_request_done_set(obj_request);
1857}
1858
Alex Elderfbfab532013-02-08 09:55:48 -06001859/*
1860 * For a simple stat call there's nothing to do. We'll do more if
1861 * this is part of a write sequence for a layered image.
1862 */
Alex Elderc47f9372013-02-26 14:23:07 -06001863static void rbd_osd_stat_callback(struct rbd_obj_request *obj_request)
Alex Elderfbfab532013-02-08 09:55:48 -06001864{
Alex Elder37206ee2013-02-20 17:32:08 -06001865 dout("%s: obj %p\n", __func__, obj_request);
Alex Elderfbfab532013-02-08 09:55:48 -06001866 obj_request_done_set(obj_request);
1867}
1868
Ilya Dryomov27617132015-07-16 17:36:11 +03001869static void rbd_osd_call_callback(struct rbd_obj_request *obj_request)
1870{
1871 dout("%s: obj %p\n", __func__, obj_request);
1872
1873 if (obj_request_img_data_test(obj_request))
1874 rbd_osd_copyup_callback(obj_request);
1875 else
1876 obj_request_done_set(obj_request);
1877}
1878
Ilya Dryomov85e084f2016-04-28 16:07:24 +02001879static void rbd_osd_req_callback(struct ceph_osd_request *osd_req)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001880{
1881 struct rbd_obj_request *obj_request = osd_req->r_priv;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001882 u16 opcode;
1883
Ilya Dryomov85e084f2016-04-28 16:07:24 +02001884 dout("%s: osd_req %p\n", __func__, osd_req);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001885 rbd_assert(osd_req == obj_request->osd_req);
Alex Elder57acbaa2013-02-11 12:33:24 -06001886 if (obj_request_img_data_test(obj_request)) {
1887 rbd_assert(obj_request->img_request);
1888 rbd_assert(obj_request->which != BAD_WHICH);
1889 } else {
1890 rbd_assert(obj_request->which == BAD_WHICH);
1891 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06001892
Sage Weil1b83bef2013-02-25 16:11:12 -08001893 if (osd_req->r_result < 0)
1894 obj_request->result = osd_req->r_result;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001895
Alex Elderc47f9372013-02-26 14:23:07 -06001896 /*
1897 * We support a 64-bit length, but ultimately it has to be
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01001898 * passed to the block layer, which just supports a 32-bit
1899 * length field.
Alex Elderc47f9372013-02-26 14:23:07 -06001900 */
Yan, Zheng7665d852016-01-07 16:48:57 +08001901 obj_request->xferred = osd_req->r_ops[0].outdata_len;
Alex Elder8b3e1a52013-01-24 16:13:36 -06001902 rbd_assert(obj_request->xferred < (u64)UINT_MAX);
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001903
Alex Elder79528732013-04-03 21:32:51 -05001904 opcode = osd_req->r_ops[0].op;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001905 switch (opcode) {
1906 case CEPH_OSD_OP_READ:
Alex Elderc47f9372013-02-26 14:23:07 -06001907 rbd_osd_read_callback(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001908 break;
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001909 case CEPH_OSD_OP_SETALLOCHINT:
Ilya Dryomove30b7572015-10-07 17:27:17 +02001910 rbd_assert(osd_req->r_ops[1].op == CEPH_OSD_OP_WRITE ||
1911 osd_req->r_ops[1].op == CEPH_OSD_OP_WRITEFULL);
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001912 /* fall through */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001913 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +02001914 case CEPH_OSD_OP_WRITEFULL:
Alex Elderc47f9372013-02-26 14:23:07 -06001915 rbd_osd_write_callback(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001916 break;
Alex Elderfbfab532013-02-08 09:55:48 -06001917 case CEPH_OSD_OP_STAT:
Alex Elderc47f9372013-02-26 14:23:07 -06001918 rbd_osd_stat_callback(obj_request);
Alex Elderfbfab532013-02-08 09:55:48 -06001919 break;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001920 case CEPH_OSD_OP_DELETE:
1921 case CEPH_OSD_OP_TRUNCATE:
1922 case CEPH_OSD_OP_ZERO:
1923 rbd_osd_discard_callback(obj_request);
1924 break;
Alex Elder36be9a72013-01-19 00:30:28 -06001925 case CEPH_OSD_OP_CALL:
Ilya Dryomov27617132015-07-16 17:36:11 +03001926 rbd_osd_call_callback(obj_request);
1927 break;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001928 default:
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01001929 rbd_warn(NULL, "unexpected OSD op: object_no %016llx opcode %d",
1930 obj_request->object_no, opcode);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001931 break;
1932 }
1933
Alex Elder07741302013-02-05 23:41:50 -06001934 if (obj_request_done_test(obj_request))
Alex Elderbf0d5f502012-11-22 00:00:08 -06001935 rbd_obj_request_complete(obj_request);
1936}
1937
Alex Elder9d4df012013-04-19 15:34:50 -05001938static void rbd_osd_req_format_read(struct rbd_obj_request *obj_request)
Alex Elder430c28c2013-04-03 21:32:51 -05001939{
Alex Elder8c042b02013-04-03 01:28:58 -05001940 struct ceph_osd_request *osd_req = obj_request->osd_req;
Alex Elder430c28c2013-04-03 21:32:51 -05001941
Ilya Dryomov7c848832016-09-15 17:56:39 +02001942 rbd_assert(obj_request_img_data_test(obj_request));
1943 osd_req->r_snapid = obj_request->img_request->snap_id;
Alex Elder9d4df012013-04-19 15:34:50 -05001944}
1945
1946static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request)
1947{
Alex Elder9d4df012013-04-19 15:34:50 -05001948 struct ceph_osd_request *osd_req = obj_request->osd_req;
Alex Elder9d4df012013-04-19 15:34:50 -05001949
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001950 osd_req->r_mtime = CURRENT_TIME;
1951 osd_req->r_data_offset = obj_request->offset;
Alex Elder430c28c2013-04-03 21:32:51 -05001952}
1953
Ilya Dryomovbc812072017-01-25 18:16:23 +01001954static struct ceph_osd_request *
1955__rbd_osd_req_create(struct rbd_device *rbd_dev,
1956 struct ceph_snap_context *snapc,
1957 int num_ops, unsigned int flags,
1958 struct rbd_obj_request *obj_request)
1959{
1960 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
1961 struct ceph_osd_request *req;
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01001962 const char *name_format = rbd_dev->image_format == 1 ?
1963 RBD_V1_DATA_FORMAT : RBD_V2_DATA_FORMAT;
Ilya Dryomovbc812072017-01-25 18:16:23 +01001964
1965 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, false, GFP_NOIO);
1966 if (!req)
1967 return NULL;
1968
1969 req->r_flags = flags;
1970 req->r_callback = rbd_osd_req_callback;
1971 req->r_priv = obj_request;
1972
1973 req->r_base_oloc.pool = rbd_dev->layout.pool_id;
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01001974 if (ceph_oid_aprintf(&req->r_base_oid, GFP_NOIO, name_format,
1975 rbd_dev->header.object_prefix, obj_request->object_no))
Ilya Dryomovbc812072017-01-25 18:16:23 +01001976 goto err_req;
1977
1978 if (ceph_osdc_alloc_messages(req, GFP_NOIO))
1979 goto err_req;
1980
1981 return req;
1982
1983err_req:
1984 ceph_osdc_put_request(req);
1985 return NULL;
1986}
1987
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001988/*
1989 * Create an osd request. A read request has one osd op (read).
1990 * A write request has either one (watch) or two (hint+write) osd ops.
1991 * (All rbd data writes are prefixed with an allocation hint op, but
1992 * technically osd watch is a write request, hence this distinction.)
1993 */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001994static struct ceph_osd_request *rbd_osd_req_create(
1995 struct rbd_device *rbd_dev,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08001996 enum obj_operation_type op_type,
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02001997 unsigned int num_ops,
Alex Elder430c28c2013-04-03 21:32:51 -05001998 struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001999{
Alex Elderbf0d5f502012-11-22 00:00:08 -06002000 struct ceph_snap_context *snapc = NULL;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002001
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002002 if (obj_request_img_data_test(obj_request) &&
2003 (op_type == OBJ_OP_DISCARD || op_type == OBJ_OP_WRITE)) {
Alex Elder6365d332013-02-11 12:33:24 -06002004 struct rbd_img_request *img_request = obj_request->img_request;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002005 if (op_type == OBJ_OP_WRITE) {
2006 rbd_assert(img_request_write_test(img_request));
2007 } else {
2008 rbd_assert(img_request_discard_test(img_request));
2009 }
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002010 snapc = img_request->snapc;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002011 }
2012
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002013 rbd_assert(num_ops == 1 || ((op_type == OBJ_OP_WRITE) && num_ops == 2));
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02002014
Ilya Dryomovbc812072017-01-25 18:16:23 +01002015 return __rbd_osd_req_create(rbd_dev, snapc, num_ops,
2016 (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD) ?
2017 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK : CEPH_OSD_FLAG_READ,
2018 obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002019}
2020
Alex Elder0eefd472013-04-19 15:34:50 -05002021/*
Josh Durgind3246fb2014-04-07 16:49:21 -07002022 * Create a copyup osd request based on the information in the object
2023 * request supplied. A copyup request has two or three osd ops, a
2024 * copyup method call, potentially a hint op, and a write or truncate
2025 * or zero op.
Alex Elder0eefd472013-04-19 15:34:50 -05002026 */
2027static struct ceph_osd_request *
2028rbd_osd_req_create_copyup(struct rbd_obj_request *obj_request)
2029{
2030 struct rbd_img_request *img_request;
Josh Durgind3246fb2014-04-07 16:49:21 -07002031 int num_osd_ops = 3;
Alex Elder0eefd472013-04-19 15:34:50 -05002032
2033 rbd_assert(obj_request_img_data_test(obj_request));
2034 img_request = obj_request->img_request;
2035 rbd_assert(img_request);
Josh Durgind3246fb2014-04-07 16:49:21 -07002036 rbd_assert(img_request_write_test(img_request) ||
2037 img_request_discard_test(img_request));
Alex Elder0eefd472013-04-19 15:34:50 -05002038
Josh Durgind3246fb2014-04-07 16:49:21 -07002039 if (img_request_discard_test(img_request))
2040 num_osd_ops = 2;
2041
Ilya Dryomovbc812072017-01-25 18:16:23 +01002042 return __rbd_osd_req_create(img_request->rbd_dev,
2043 img_request->snapc, num_osd_ops,
2044 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
2045 obj_request);
Alex Elder0eefd472013-04-19 15:34:50 -05002046}
2047
Alex Elderbf0d5f502012-11-22 00:00:08 -06002048static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req)
2049{
2050 ceph_osdc_put_request(osd_req);
2051}
2052
2053/* object_name is assumed to be a non-null pointer and NUL-terminated */
2054
2055static struct rbd_obj_request *rbd_obj_request_create(const char *object_name,
Alex Elderbf0d5f502012-11-22 00:00:08 -06002056 enum obj_request_type type)
2057{
2058 struct rbd_obj_request *obj_request;
2059 size_t size;
2060 char *name;
2061
2062 rbd_assert(obj_request_type_valid(type));
2063
2064 size = strlen(object_name) + 1;
Ilya Dryomov5a60e872015-06-24 17:24:33 +03002065 name = kmalloc(size, GFP_NOIO);
Alex Elderf907ad52013-05-01 12:43:03 -05002066 if (!name)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002067 return NULL;
2068
Ilya Dryomov5a60e872015-06-24 17:24:33 +03002069 obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_NOIO);
Alex Elderf907ad52013-05-01 12:43:03 -05002070 if (!obj_request) {
2071 kfree(name);
2072 return NULL;
2073 }
2074
Alex Elderbf0d5f502012-11-22 00:00:08 -06002075 obj_request->object_name = memcpy(name, object_name, size);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002076 obj_request->which = BAD_WHICH;
2077 obj_request->type = type;
2078 INIT_LIST_HEAD(&obj_request->links);
Alex Elder788e2df2013-01-17 12:25:27 -06002079 init_completion(&obj_request->completion);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002080 kref_init(&obj_request->kref);
2081
Ilya Dryomov67e2b652017-01-25 18:16:22 +01002082 dout("%s %p\n", __func__, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002083 return obj_request;
2084}
2085
2086static void rbd_obj_request_destroy(struct kref *kref)
2087{
2088 struct rbd_obj_request *obj_request;
2089
2090 obj_request = container_of(kref, struct rbd_obj_request, kref);
2091
Alex Elder37206ee2013-02-20 17:32:08 -06002092 dout("%s: obj %p\n", __func__, obj_request);
2093
Alex Elderbf0d5f502012-11-22 00:00:08 -06002094 rbd_assert(obj_request->img_request == NULL);
2095 rbd_assert(obj_request->which == BAD_WHICH);
2096
2097 if (obj_request->osd_req)
2098 rbd_osd_req_destroy(obj_request->osd_req);
2099
2100 rbd_assert(obj_request_type_valid(obj_request->type));
2101 switch (obj_request->type) {
Alex Elder9969ebc2013-01-18 12:31:10 -06002102 case OBJ_REQUEST_NODATA:
2103 break; /* Nothing to do */
Alex Elderbf0d5f502012-11-22 00:00:08 -06002104 case OBJ_REQUEST_BIO:
2105 if (obj_request->bio_list)
2106 bio_chain_put(obj_request->bio_list);
2107 break;
Alex Elder788e2df2013-01-17 12:25:27 -06002108 case OBJ_REQUEST_PAGES:
Ilya Dryomov04dc9232016-09-15 18:05:16 +02002109 /* img_data requests don't own their page array */
2110 if (obj_request->pages &&
2111 !obj_request_img_data_test(obj_request))
Alex Elder788e2df2013-01-17 12:25:27 -06002112 ceph_release_page_vector(obj_request->pages,
2113 obj_request->page_count);
2114 break;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002115 }
2116
Alex Elderf907ad52013-05-01 12:43:03 -05002117 kfree(obj_request->object_name);
Alex Elder868311b2013-05-01 12:43:03 -05002118 obj_request->object_name = NULL;
2119 kmem_cache_free(rbd_obj_request_cache, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002120}
2121
Alex Elderfb65d2282013-05-08 22:50:04 -05002122/* It's OK to call this for a device with no parent */
2123
2124static void rbd_spec_put(struct rbd_spec *spec);
2125static void rbd_dev_unparent(struct rbd_device *rbd_dev)
2126{
2127 rbd_dev_remove_parent(rbd_dev);
2128 rbd_spec_put(rbd_dev->parent_spec);
2129 rbd_dev->parent_spec = NULL;
2130 rbd_dev->parent_overlap = 0;
2131}
2132
Alex Elderbf0d5f502012-11-22 00:00:08 -06002133/*
Alex Eldera2acd002013-05-08 22:50:04 -05002134 * Parent image reference counting is used to determine when an
2135 * image's parent fields can be safely torn down--after there are no
2136 * more in-flight requests to the parent image. When the last
2137 * reference is dropped, cleaning them up is safe.
2138 */
2139static void rbd_dev_parent_put(struct rbd_device *rbd_dev)
2140{
2141 int counter;
2142
2143 if (!rbd_dev->parent_spec)
2144 return;
2145
2146 counter = atomic_dec_return_safe(&rbd_dev->parent_ref);
2147 if (counter > 0)
2148 return;
2149
2150 /* Last reference; clean up parent data structures */
2151
2152 if (!counter)
2153 rbd_dev_unparent(rbd_dev);
2154 else
Ilya Dryomov9584d502014-07-11 12:11:20 +04002155 rbd_warn(rbd_dev, "parent reference underflow");
Alex Eldera2acd002013-05-08 22:50:04 -05002156}
2157
2158/*
2159 * If an image has a non-zero parent overlap, get a reference to its
2160 * parent.
2161 *
2162 * Returns true if the rbd device has a parent with a non-zero
2163 * overlap and a reference for it was successfully taken, or
2164 * false otherwise.
2165 */
2166static bool rbd_dev_parent_get(struct rbd_device *rbd_dev)
2167{
Ilya Dryomovae43e9d2015-01-19 18:13:43 +03002168 int counter = 0;
Alex Eldera2acd002013-05-08 22:50:04 -05002169
2170 if (!rbd_dev->parent_spec)
2171 return false;
2172
Ilya Dryomovae43e9d2015-01-19 18:13:43 +03002173 down_read(&rbd_dev->header_rwsem);
2174 if (rbd_dev->parent_overlap)
2175 counter = atomic_inc_return_safe(&rbd_dev->parent_ref);
2176 up_read(&rbd_dev->header_rwsem);
Alex Eldera2acd002013-05-08 22:50:04 -05002177
2178 if (counter < 0)
Ilya Dryomov9584d502014-07-11 12:11:20 +04002179 rbd_warn(rbd_dev, "parent reference overflow");
Alex Eldera2acd002013-05-08 22:50:04 -05002180
Ilya Dryomovae43e9d2015-01-19 18:13:43 +03002181 return counter > 0;
Alex Eldera2acd002013-05-08 22:50:04 -05002182}
2183
Alex Elderbf0d5f502012-11-22 00:00:08 -06002184/*
2185 * Caller is responsible for filling in the list of object requests
2186 * that comprises the image request, and the Linux request pointer
2187 * (if there is one).
2188 */
Alex Eldercc344fa2013-02-19 12:25:56 -06002189static struct rbd_img_request *rbd_img_request_create(
2190 struct rbd_device *rbd_dev,
Alex Elderbf0d5f502012-11-22 00:00:08 -06002191 u64 offset, u64 length,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002192 enum obj_operation_type op_type,
Josh Durgin4e752f02014-04-08 11:12:11 -07002193 struct ceph_snap_context *snapc)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002194{
2195 struct rbd_img_request *img_request;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002196
Ilya Dryomov7a716aa2014-08-05 11:25:54 +04002197 img_request = kmem_cache_alloc(rbd_img_request_cache, GFP_NOIO);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002198 if (!img_request)
2199 return NULL;
2200
Alex Elderbf0d5f502012-11-22 00:00:08 -06002201 img_request->rq = NULL;
2202 img_request->rbd_dev = rbd_dev;
2203 img_request->offset = offset;
2204 img_request->length = length;
Alex Elder0c425242013-02-08 09:55:49 -06002205 img_request->flags = 0;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002206 if (op_type == OBJ_OP_DISCARD) {
2207 img_request_discard_set(img_request);
2208 img_request->snapc = snapc;
2209 } else if (op_type == OBJ_OP_WRITE) {
Alex Elder0c425242013-02-08 09:55:49 -06002210 img_request_write_set(img_request);
Josh Durgin4e752f02014-04-08 11:12:11 -07002211 img_request->snapc = snapc;
Alex Elder0c425242013-02-08 09:55:49 -06002212 } else {
Alex Elderbf0d5f502012-11-22 00:00:08 -06002213 img_request->snap_id = rbd_dev->spec->snap_id;
Alex Elder0c425242013-02-08 09:55:49 -06002214 }
Alex Eldera2acd002013-05-08 22:50:04 -05002215 if (rbd_dev_parent_get(rbd_dev))
Alex Elderd0b2e942013-01-24 16:13:36 -06002216 img_request_layered_set(img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002217 spin_lock_init(&img_request->completion_lock);
2218 img_request->next_completion = 0;
2219 img_request->callback = NULL;
Alex Eldera5a337d2013-01-24 16:13:36 -06002220 img_request->result = 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002221 img_request->obj_request_count = 0;
2222 INIT_LIST_HEAD(&img_request->obj_requests);
2223 kref_init(&img_request->kref);
2224
Alex Elder37206ee2013-02-20 17:32:08 -06002225 dout("%s: rbd_dev %p %s %llu/%llu -> img %p\n", __func__, rbd_dev,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002226 obj_op_name(op_type), offset, length, img_request);
Alex Elder37206ee2013-02-20 17:32:08 -06002227
Alex Elderbf0d5f502012-11-22 00:00:08 -06002228 return img_request;
2229}
2230
2231static void rbd_img_request_destroy(struct kref *kref)
2232{
2233 struct rbd_img_request *img_request;
2234 struct rbd_obj_request *obj_request;
2235 struct rbd_obj_request *next_obj_request;
2236
2237 img_request = container_of(kref, struct rbd_img_request, kref);
2238
Alex Elder37206ee2013-02-20 17:32:08 -06002239 dout("%s: img %p\n", __func__, img_request);
2240
Alex Elderbf0d5f502012-11-22 00:00:08 -06002241 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
2242 rbd_img_obj_request_del(img_request, obj_request);
Alex Elder25dcf952013-01-25 17:08:55 -06002243 rbd_assert(img_request->obj_request_count == 0);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002244
Alex Eldera2acd002013-05-08 22:50:04 -05002245 if (img_request_layered_test(img_request)) {
2246 img_request_layered_clear(img_request);
2247 rbd_dev_parent_put(img_request->rbd_dev);
2248 }
2249
Josh Durginbef95452014-04-04 17:47:52 -07002250 if (img_request_write_test(img_request) ||
2251 img_request_discard_test(img_request))
Alex Elder812164f82013-04-30 00:44:32 -05002252 ceph_put_snap_context(img_request->snapc);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002253
Alex Elder1c2a9df2013-05-01 12:43:03 -05002254 kmem_cache_free(rbd_img_request_cache, img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002255}
2256
Alex Eldere93f3152013-05-08 22:50:04 -05002257static struct rbd_img_request *rbd_parent_request_create(
2258 struct rbd_obj_request *obj_request,
2259 u64 img_offset, u64 length)
2260{
2261 struct rbd_img_request *parent_request;
2262 struct rbd_device *rbd_dev;
2263
2264 rbd_assert(obj_request->img_request);
2265 rbd_dev = obj_request->img_request->rbd_dev;
2266
Josh Durgin4e752f02014-04-08 11:12:11 -07002267 parent_request = rbd_img_request_create(rbd_dev->parent, img_offset,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002268 length, OBJ_OP_READ, NULL);
Alex Eldere93f3152013-05-08 22:50:04 -05002269 if (!parent_request)
2270 return NULL;
2271
2272 img_request_child_set(parent_request);
2273 rbd_obj_request_get(obj_request);
2274 parent_request->obj_request = obj_request;
2275
2276 return parent_request;
2277}
2278
2279static void rbd_parent_request_destroy(struct kref *kref)
2280{
2281 struct rbd_img_request *parent_request;
2282 struct rbd_obj_request *orig_request;
2283
2284 parent_request = container_of(kref, struct rbd_img_request, kref);
2285 orig_request = parent_request->obj_request;
2286
2287 parent_request->obj_request = NULL;
2288 rbd_obj_request_put(orig_request);
2289 img_request_child_clear(parent_request);
2290
2291 rbd_img_request_destroy(kref);
2292}
2293
Alex Elder12178572013-02-08 09:55:49 -06002294static bool rbd_img_obj_end_request(struct rbd_obj_request *obj_request)
2295{
Alex Elder6365d332013-02-11 12:33:24 -06002296 struct rbd_img_request *img_request;
Alex Elder12178572013-02-08 09:55:49 -06002297 unsigned int xferred;
2298 int result;
Alex Elder8b3e1a52013-01-24 16:13:36 -06002299 bool more;
Alex Elder12178572013-02-08 09:55:49 -06002300
Alex Elder6365d332013-02-11 12:33:24 -06002301 rbd_assert(obj_request_img_data_test(obj_request));
2302 img_request = obj_request->img_request;
2303
Alex Elder12178572013-02-08 09:55:49 -06002304 rbd_assert(obj_request->xferred <= (u64)UINT_MAX);
2305 xferred = (unsigned int)obj_request->xferred;
2306 result = obj_request->result;
2307 if (result) {
2308 struct rbd_device *rbd_dev = img_request->rbd_dev;
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002309 enum obj_operation_type op_type;
2310
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002311 if (img_request_discard_test(img_request))
2312 op_type = OBJ_OP_DISCARD;
2313 else if (img_request_write_test(img_request))
2314 op_type = OBJ_OP_WRITE;
2315 else
2316 op_type = OBJ_OP_READ;
Alex Elder12178572013-02-08 09:55:49 -06002317
Ilya Dryomov9584d502014-07-11 12:11:20 +04002318 rbd_warn(rbd_dev, "%s %llx at %llx (%llx)",
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002319 obj_op_name(op_type), obj_request->length,
2320 obj_request->img_offset, obj_request->offset);
Ilya Dryomov9584d502014-07-11 12:11:20 +04002321 rbd_warn(rbd_dev, " result %d xferred %x",
Alex Elder12178572013-02-08 09:55:49 -06002322 result, xferred);
2323 if (!img_request->result)
2324 img_request->result = result;
Ilya Dryomov082a75d2015-04-25 15:56:15 +03002325 /*
2326 * Need to end I/O on the entire obj_request worth of
2327 * bytes in case of error.
2328 */
2329 xferred = obj_request->length;
Alex Elder12178572013-02-08 09:55:49 -06002330 }
2331
Alex Elder8b3e1a52013-01-24 16:13:36 -06002332 if (img_request_child_test(img_request)) {
2333 rbd_assert(img_request->obj_request != NULL);
2334 more = obj_request->which < img_request->obj_request_count - 1;
2335 } else {
2336 rbd_assert(img_request->rq != NULL);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01002337
2338 more = blk_update_request(img_request->rq, result, xferred);
2339 if (!more)
2340 __blk_mq_end_request(img_request->rq, result);
Alex Elder8b3e1a52013-01-24 16:13:36 -06002341 }
2342
2343 return more;
Alex Elder12178572013-02-08 09:55:49 -06002344}
2345
Alex Elder21692382013-04-05 01:27:12 -05002346static void rbd_img_obj_callback(struct rbd_obj_request *obj_request)
2347{
2348 struct rbd_img_request *img_request;
2349 u32 which = obj_request->which;
2350 bool more = true;
2351
Alex Elder6365d332013-02-11 12:33:24 -06002352 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elder21692382013-04-05 01:27:12 -05002353 img_request = obj_request->img_request;
2354
2355 dout("%s: img %p obj %p\n", __func__, img_request, obj_request);
2356 rbd_assert(img_request != NULL);
Alex Elder21692382013-04-05 01:27:12 -05002357 rbd_assert(img_request->obj_request_count > 0);
2358 rbd_assert(which != BAD_WHICH);
2359 rbd_assert(which < img_request->obj_request_count);
Alex Elder21692382013-04-05 01:27:12 -05002360
2361 spin_lock_irq(&img_request->completion_lock);
2362 if (which != img_request->next_completion)
2363 goto out;
2364
2365 for_each_obj_request_from(img_request, obj_request) {
Alex Elder21692382013-04-05 01:27:12 -05002366 rbd_assert(more);
2367 rbd_assert(which < img_request->obj_request_count);
2368
2369 if (!obj_request_done_test(obj_request))
2370 break;
Alex Elder12178572013-02-08 09:55:49 -06002371 more = rbd_img_obj_end_request(obj_request);
Alex Elder21692382013-04-05 01:27:12 -05002372 which++;
2373 }
2374
2375 rbd_assert(more ^ (which == img_request->obj_request_count));
2376 img_request->next_completion = which;
2377out:
2378 spin_unlock_irq(&img_request->completion_lock);
Alex Elder0f2d5be2014-04-26 14:21:44 +04002379 rbd_img_request_put(img_request);
Alex Elder21692382013-04-05 01:27:12 -05002380
2381 if (!more)
2382 rbd_img_request_complete(img_request);
2383}
2384
Alex Elderf1a47392013-04-19 15:34:50 -05002385/*
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002386 * Add individual osd ops to the given ceph_osd_request and prepare
2387 * them for submission. num_ops is the current number of
2388 * osd operations already to the object request.
2389 */
2390static void rbd_img_obj_request_fill(struct rbd_obj_request *obj_request,
2391 struct ceph_osd_request *osd_request,
2392 enum obj_operation_type op_type,
2393 unsigned int num_ops)
2394{
2395 struct rbd_img_request *img_request = obj_request->img_request;
2396 struct rbd_device *rbd_dev = img_request->rbd_dev;
2397 u64 object_size = rbd_obj_bytes(&rbd_dev->header);
2398 u64 offset = obj_request->offset;
2399 u64 length = obj_request->length;
2400 u64 img_end;
2401 u16 opcode;
2402
2403 if (op_type == OBJ_OP_DISCARD) {
Josh Durgind3246fb2014-04-07 16:49:21 -07002404 if (!offset && length == object_size &&
2405 (!img_request_layered_test(img_request) ||
2406 !obj_request_overlaps_parent(obj_request))) {
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002407 opcode = CEPH_OSD_OP_DELETE;
2408 } else if ((offset + length == object_size)) {
2409 opcode = CEPH_OSD_OP_TRUNCATE;
2410 } else {
2411 down_read(&rbd_dev->header_rwsem);
2412 img_end = rbd_dev->header.image_size;
2413 up_read(&rbd_dev->header_rwsem);
2414
2415 if (obj_request->img_offset + length == img_end)
2416 opcode = CEPH_OSD_OP_TRUNCATE;
2417 else
2418 opcode = CEPH_OSD_OP_ZERO;
2419 }
2420 } else if (op_type == OBJ_OP_WRITE) {
Ilya Dryomove30b7572015-10-07 17:27:17 +02002421 if (!offset && length == object_size)
2422 opcode = CEPH_OSD_OP_WRITEFULL;
2423 else
2424 opcode = CEPH_OSD_OP_WRITE;
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002425 osd_req_op_alloc_hint_init(osd_request, num_ops,
2426 object_size, object_size);
2427 num_ops++;
2428 } else {
2429 opcode = CEPH_OSD_OP_READ;
2430 }
2431
Ilya Dryomov7e868b62014-11-21 22:16:43 +03002432 if (opcode == CEPH_OSD_OP_DELETE)
Yan, Zheng144cba12015-04-27 11:09:54 +08002433 osd_req_op_init(osd_request, num_ops, opcode, 0);
Ilya Dryomov7e868b62014-11-21 22:16:43 +03002434 else
2435 osd_req_op_extent_init(osd_request, num_ops, opcode,
2436 offset, length, 0, 0);
2437
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002438 if (obj_request->type == OBJ_REQUEST_BIO)
2439 osd_req_op_extent_osd_data_bio(osd_request, num_ops,
2440 obj_request->bio_list, length);
2441 else if (obj_request->type == OBJ_REQUEST_PAGES)
2442 osd_req_op_extent_osd_data_pages(osd_request, num_ops,
2443 obj_request->pages, length,
2444 offset & ~PAGE_MASK, false, false);
2445
2446 /* Discards are also writes */
2447 if (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD)
2448 rbd_osd_req_format_write(obj_request);
2449 else
2450 rbd_osd_req_format_read(obj_request);
2451}
2452
2453/*
Alex Elderf1a47392013-04-19 15:34:50 -05002454 * Split up an image request into one or more object requests, each
2455 * to a different object. The "type" parameter indicates whether
2456 * "data_desc" is the pointer to the head of a list of bio
2457 * structures, or the base of a page array. In either case this
2458 * function assumes data_desc describes memory sufficient to hold
2459 * all data described by the image request.
2460 */
2461static int rbd_img_request_fill(struct rbd_img_request *img_request,
2462 enum obj_request_type type,
2463 void *data_desc)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002464{
2465 struct rbd_device *rbd_dev = img_request->rbd_dev;
2466 struct rbd_obj_request *obj_request = NULL;
2467 struct rbd_obj_request *next_obj_request;
Jingoo Hana1580732013-08-09 13:04:35 +09002468 struct bio *bio_list = NULL;
Alex Elderf1a47392013-04-19 15:34:50 -05002469 unsigned int bio_offset = 0;
Jingoo Hana1580732013-08-09 13:04:35 +09002470 struct page **pages = NULL;
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002471 enum obj_operation_type op_type;
Alex Elder7da22d22013-01-24 16:13:36 -06002472 u64 img_offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002473 u64 resid;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002474
Alex Elderf1a47392013-04-19 15:34:50 -05002475 dout("%s: img %p type %d data_desc %p\n", __func__, img_request,
2476 (int)type, data_desc);
Alex Elder37206ee2013-02-20 17:32:08 -06002477
Alex Elder7da22d22013-01-24 16:13:36 -06002478 img_offset = img_request->offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002479 resid = img_request->length;
Alex Elder4dda41d2013-02-20 21:59:33 -06002480 rbd_assert(resid > 0);
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002481 op_type = rbd_img_request_op_type(img_request);
Alex Elderf1a47392013-04-19 15:34:50 -05002482
2483 if (type == OBJ_REQUEST_BIO) {
2484 bio_list = data_desc;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002485 rbd_assert(img_offset ==
2486 bio_list->bi_iter.bi_sector << SECTOR_SHIFT);
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002487 } else if (type == OBJ_REQUEST_PAGES) {
Alex Elderf1a47392013-04-19 15:34:50 -05002488 pages = data_desc;
2489 }
2490
Alex Elderbf0d5f502012-11-22 00:00:08 -06002491 while (resid) {
Alex Elder2fa12322013-04-05 01:27:12 -05002492 struct ceph_osd_request *osd_req;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002493 const char *object_name;
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01002494 u64 object_no = img_offset >> rbd_dev->header.obj_order;
Ilya Dryomov67e2b652017-01-25 18:16:22 +01002495 u64 offset = rbd_segment_offset(rbd_dev, img_offset);
2496 u64 length = rbd_segment_length(rbd_dev, img_offset, resid);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002497
Alex Elder7da22d22013-01-24 16:13:36 -06002498 object_name = rbd_segment_name(rbd_dev, img_offset);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002499 if (!object_name)
2500 goto out_unwind;
Ilya Dryomov67e2b652017-01-25 18:16:22 +01002501 obj_request = rbd_obj_request_create(object_name, type);
Alex Elder78c2a442013-05-01 12:43:04 -05002502 /* object request has its own copy of the object name */
2503 rbd_segment_name_free(object_name);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002504 if (!obj_request)
2505 goto out_unwind;
Ilya Dryomov62054da2014-03-04 11:57:17 +02002506
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01002507 obj_request->object_no = object_no;
Ilya Dryomov67e2b652017-01-25 18:16:22 +01002508 obj_request->offset = offset;
2509 obj_request->length = length;
2510
Josh Durgin03507db2013-08-27 14:45:46 -07002511 /*
2512 * set obj_request->img_request before creating the
2513 * osd_request so that it gets the right snapc
2514 */
2515 rbd_img_obj_request_add(img_request, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002516
Alex Elderf1a47392013-04-19 15:34:50 -05002517 if (type == OBJ_REQUEST_BIO) {
2518 unsigned int clone_size;
2519
2520 rbd_assert(length <= (u64)UINT_MAX);
2521 clone_size = (unsigned int)length;
2522 obj_request->bio_list =
2523 bio_chain_clone_range(&bio_list,
2524 &bio_offset,
2525 clone_size,
David Disseldorp2224d872016-04-05 11:13:39 +02002526 GFP_NOIO);
Alex Elderf1a47392013-04-19 15:34:50 -05002527 if (!obj_request->bio_list)
Ilya Dryomov62054da2014-03-04 11:57:17 +02002528 goto out_unwind;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002529 } else if (type == OBJ_REQUEST_PAGES) {
Alex Elderf1a47392013-04-19 15:34:50 -05002530 unsigned int page_count;
2531
2532 obj_request->pages = pages;
2533 page_count = (u32)calc_pages_for(offset, length);
2534 obj_request->page_count = page_count;
2535 if ((offset + length) & ~PAGE_MASK)
2536 page_count--; /* more on last page */
2537 pages += page_count;
2538 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06002539
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002540 osd_req = rbd_osd_req_create(rbd_dev, op_type,
2541 (op_type == OBJ_OP_WRITE) ? 2 : 1,
2542 obj_request);
Alex Elder2fa12322013-04-05 01:27:12 -05002543 if (!osd_req)
Ilya Dryomov62054da2014-03-04 11:57:17 +02002544 goto out_unwind;
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002545
Alex Elder2fa12322013-04-05 01:27:12 -05002546 obj_request->osd_req = osd_req;
Alex Elder21692382013-04-05 01:27:12 -05002547 obj_request->callback = rbd_img_obj_callback;
Alex Elder7da22d22013-01-24 16:13:36 -06002548 obj_request->img_offset = img_offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002549
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002550 rbd_img_obj_request_fill(obj_request, osd_req, op_type, 0);
2551
Alex Elder7da22d22013-01-24 16:13:36 -06002552 img_offset += length;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002553 resid -= length;
2554 }
2555
2556 return 0;
2557
Alex Elderbf0d5f502012-11-22 00:00:08 -06002558out_unwind:
2559 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
Ilya Dryomov42dd0372014-03-04 11:57:17 +02002560 rbd_img_obj_request_del(img_request, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002561
2562 return -ENOMEM;
2563}
2564
Alex Elder3d7efd12013-04-19 15:34:50 -05002565static void
Ilya Dryomov27617132015-07-16 17:36:11 +03002566rbd_osd_copyup_callback(struct rbd_obj_request *obj_request)
Alex Elder0eefd472013-04-19 15:34:50 -05002567{
2568 struct rbd_img_request *img_request;
2569 struct rbd_device *rbd_dev;
Alex Elderebda6402013-05-10 16:29:22 -05002570 struct page **pages;
Alex Elder0eefd472013-04-19 15:34:50 -05002571 u32 page_count;
2572
Ilya Dryomov27617132015-07-16 17:36:11 +03002573 dout("%s: obj %p\n", __func__, obj_request);
2574
Josh Durgind3246fb2014-04-07 16:49:21 -07002575 rbd_assert(obj_request->type == OBJ_REQUEST_BIO ||
2576 obj_request->type == OBJ_REQUEST_NODATA);
Alex Elder0eefd472013-04-19 15:34:50 -05002577 rbd_assert(obj_request_img_data_test(obj_request));
2578 img_request = obj_request->img_request;
2579 rbd_assert(img_request);
2580
2581 rbd_dev = img_request->rbd_dev;
2582 rbd_assert(rbd_dev);
Alex Elder0eefd472013-04-19 15:34:50 -05002583
Alex Elderebda6402013-05-10 16:29:22 -05002584 pages = obj_request->copyup_pages;
2585 rbd_assert(pages != NULL);
Alex Elder0eefd472013-04-19 15:34:50 -05002586 obj_request->copyup_pages = NULL;
Alex Elderebda6402013-05-10 16:29:22 -05002587 page_count = obj_request->copyup_page_count;
2588 rbd_assert(page_count);
2589 obj_request->copyup_page_count = 0;
2590 ceph_release_page_vector(pages, page_count);
Alex Elder0eefd472013-04-19 15:34:50 -05002591
2592 /*
2593 * We want the transfer count to reflect the size of the
2594 * original write request. There is no such thing as a
2595 * successful short write, so if the request was successful
2596 * we can just set it to the originally-requested length.
2597 */
2598 if (!obj_request->result)
2599 obj_request->xferred = obj_request->length;
2600
Ilya Dryomov27617132015-07-16 17:36:11 +03002601 obj_request_done_set(obj_request);
Alex Elder0eefd472013-04-19 15:34:50 -05002602}
2603
2604static void
Alex Elder3d7efd12013-04-19 15:34:50 -05002605rbd_img_obj_parent_read_full_callback(struct rbd_img_request *img_request)
2606{
2607 struct rbd_obj_request *orig_request;
Alex Elder0eefd472013-04-19 15:34:50 -05002608 struct ceph_osd_request *osd_req;
Alex Elder0eefd472013-04-19 15:34:50 -05002609 struct rbd_device *rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002610 struct page **pages;
Josh Durgind3246fb2014-04-07 16:49:21 -07002611 enum obj_operation_type op_type;
Alex Elderebda6402013-05-10 16:29:22 -05002612 u32 page_count;
Alex Elderbbea1c12013-05-06 17:40:33 -05002613 int img_result;
Alex Elderebda6402013-05-10 16:29:22 -05002614 u64 parent_length;
Alex Elder3d7efd12013-04-19 15:34:50 -05002615
2616 rbd_assert(img_request_child_test(img_request));
2617
2618 /* First get what we need from the image request */
2619
2620 pages = img_request->copyup_pages;
2621 rbd_assert(pages != NULL);
2622 img_request->copyup_pages = NULL;
Alex Elderebda6402013-05-10 16:29:22 -05002623 page_count = img_request->copyup_page_count;
2624 rbd_assert(page_count);
2625 img_request->copyup_page_count = 0;
Alex Elder3d7efd12013-04-19 15:34:50 -05002626
2627 orig_request = img_request->obj_request;
2628 rbd_assert(orig_request != NULL);
Alex Elderb91f09f2013-05-10 16:29:22 -05002629 rbd_assert(obj_request_type_valid(orig_request->type));
Alex Elderbbea1c12013-05-06 17:40:33 -05002630 img_result = img_request->result;
Alex Elderebda6402013-05-10 16:29:22 -05002631 parent_length = img_request->length;
Ilya Dryomovfa355112016-09-16 15:20:42 +02002632 rbd_assert(img_result || parent_length == img_request->xferred);
Alex Elder3d7efd12013-04-19 15:34:50 -05002633 rbd_img_request_put(img_request);
2634
Alex Elder91c6feb2013-05-06 17:40:32 -05002635 rbd_assert(orig_request->img_request);
2636 rbd_dev = orig_request->img_request->rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002637 rbd_assert(rbd_dev);
Alex Elder3d7efd12013-04-19 15:34:50 -05002638
Alex Elderbbea1c12013-05-06 17:40:33 -05002639 /*
2640 * If the overlap has become 0 (most likely because the
2641 * image has been flattened) we need to free the pages
2642 * and re-submit the original write request.
2643 */
2644 if (!rbd_dev->parent_overlap) {
Alex Elderbbea1c12013-05-06 17:40:33 -05002645 ceph_release_page_vector(pages, page_count);
Ilya Dryomov980917f2016-09-12 18:59:42 +02002646 rbd_obj_request_submit(orig_request);
2647 return;
Alex Elderbbea1c12013-05-06 17:40:33 -05002648 }
2649
2650 if (img_result)
Alex Elder0eefd472013-04-19 15:34:50 -05002651 goto out_err;
Alex Elder3d7efd12013-04-19 15:34:50 -05002652
Alex Elder8785b1d2013-05-09 10:08:49 -05002653 /*
2654 * The original osd request is of no use to use any more.
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02002655 * We need a new one that can hold the three ops in a copyup
Alex Elder8785b1d2013-05-09 10:08:49 -05002656 * request. Allocate the new copyup osd request for the
2657 * original request, and release the old one.
2658 */
Alex Elderbbea1c12013-05-06 17:40:33 -05002659 img_result = -ENOMEM;
Alex Elder0eefd472013-04-19 15:34:50 -05002660 osd_req = rbd_osd_req_create_copyup(orig_request);
2661 if (!osd_req)
2662 goto out_err;
Alex Elder8785b1d2013-05-09 10:08:49 -05002663 rbd_osd_req_destroy(orig_request->osd_req);
Alex Elder0eefd472013-04-19 15:34:50 -05002664 orig_request->osd_req = osd_req;
2665 orig_request->copyup_pages = pages;
Alex Elderebda6402013-05-10 16:29:22 -05002666 orig_request->copyup_page_count = page_count;
Alex Elder3d7efd12013-04-19 15:34:50 -05002667
Alex Elder0eefd472013-04-19 15:34:50 -05002668 /* Initialize the copyup op */
2669
2670 osd_req_op_cls_init(osd_req, 0, CEPH_OSD_OP_CALL, "rbd", "copyup");
Alex Elderebda6402013-05-10 16:29:22 -05002671 osd_req_op_cls_request_data_pages(osd_req, 0, pages, parent_length, 0,
Alex Elder0eefd472013-04-19 15:34:50 -05002672 false, false);
2673
Josh Durgind3246fb2014-04-07 16:49:21 -07002674 /* Add the other op(s) */
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02002675
Josh Durgind3246fb2014-04-07 16:49:21 -07002676 op_type = rbd_img_request_op_type(orig_request->img_request);
2677 rbd_img_obj_request_fill(orig_request, osd_req, op_type, 1);
Alex Elder0eefd472013-04-19 15:34:50 -05002678
2679 /* All set, send it off. */
2680
Ilya Dryomov980917f2016-09-12 18:59:42 +02002681 rbd_obj_request_submit(orig_request);
2682 return;
Alex Elder0eefd472013-04-19 15:34:50 -05002683
Alex Elder0eefd472013-04-19 15:34:50 -05002684out_err:
Ilya Dryomovfa355112016-09-16 15:20:42 +02002685 ceph_release_page_vector(pages, page_count);
Ilya Dryomov0dcc6852016-09-26 15:43:52 +02002686 rbd_obj_request_error(orig_request, img_result);
Alex Elder3d7efd12013-04-19 15:34:50 -05002687}
2688
2689/*
2690 * Read from the parent image the range of data that covers the
2691 * entire target of the given object request. This is used for
2692 * satisfying a layered image write request when the target of an
2693 * object request from the image request does not exist.
2694 *
2695 * A page array big enough to hold the returned data is allocated
2696 * and supplied to rbd_img_request_fill() as the "data descriptor."
2697 * When the read completes, this page array will be transferred to
2698 * the original object request for the copyup operation.
2699 *
Ilya Dryomovc2e82412016-09-13 20:18:01 +02002700 * If an error occurs, it is recorded as the result of the original
2701 * object request in rbd_img_obj_exists_callback().
Alex Elder3d7efd12013-04-19 15:34:50 -05002702 */
2703static int rbd_img_obj_parent_read_full(struct rbd_obj_request *obj_request)
2704{
Ilya Dryomov058aa992016-09-12 14:44:45 +02002705 struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002706 struct rbd_img_request *parent_request = NULL;
Alex Elder3d7efd12013-04-19 15:34:50 -05002707 u64 img_offset;
2708 u64 length;
2709 struct page **pages = NULL;
2710 u32 page_count;
2711 int result;
2712
Alex Elder3d7efd12013-04-19 15:34:50 -05002713 rbd_assert(rbd_dev->parent != NULL);
2714
2715 /*
2716 * Determine the byte range covered by the object in the
2717 * child image to which the original request was to be sent.
2718 */
2719 img_offset = obj_request->img_offset - obj_request->offset;
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +01002720 length = rbd_obj_bytes(&rbd_dev->header);
Alex Elder3d7efd12013-04-19 15:34:50 -05002721
2722 /*
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002723 * There is no defined parent data beyond the parent
2724 * overlap, so limit what we read at that boundary if
2725 * necessary.
2726 */
2727 if (img_offset + length > rbd_dev->parent_overlap) {
2728 rbd_assert(img_offset < rbd_dev->parent_overlap);
2729 length = rbd_dev->parent_overlap - img_offset;
2730 }
2731
2732 /*
Alex Elder3d7efd12013-04-19 15:34:50 -05002733 * Allocate a page array big enough to receive the data read
2734 * from the parent.
2735 */
2736 page_count = (u32)calc_pages_for(0, length);
2737 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2738 if (IS_ERR(pages)) {
2739 result = PTR_ERR(pages);
2740 pages = NULL;
2741 goto out_err;
2742 }
2743
2744 result = -ENOMEM;
Alex Eldere93f3152013-05-08 22:50:04 -05002745 parent_request = rbd_parent_request_create(obj_request,
2746 img_offset, length);
Alex Elder3d7efd12013-04-19 15:34:50 -05002747 if (!parent_request)
2748 goto out_err;
Alex Elder3d7efd12013-04-19 15:34:50 -05002749
2750 result = rbd_img_request_fill(parent_request, OBJ_REQUEST_PAGES, pages);
2751 if (result)
2752 goto out_err;
Ilya Dryomov058aa992016-09-12 14:44:45 +02002753
Alex Elder3d7efd12013-04-19 15:34:50 -05002754 parent_request->copyup_pages = pages;
Alex Elderebda6402013-05-10 16:29:22 -05002755 parent_request->copyup_page_count = page_count;
Alex Elder3d7efd12013-04-19 15:34:50 -05002756 parent_request->callback = rbd_img_obj_parent_read_full_callback;
Ilya Dryomov058aa992016-09-12 14:44:45 +02002757
Alex Elder3d7efd12013-04-19 15:34:50 -05002758 result = rbd_img_request_submit(parent_request);
2759 if (!result)
2760 return 0;
2761
2762 parent_request->copyup_pages = NULL;
Alex Elderebda6402013-05-10 16:29:22 -05002763 parent_request->copyup_page_count = 0;
Alex Elder3d7efd12013-04-19 15:34:50 -05002764 parent_request->obj_request = NULL;
2765 rbd_obj_request_put(obj_request);
2766out_err:
2767 if (pages)
2768 ceph_release_page_vector(pages, page_count);
2769 if (parent_request)
2770 rbd_img_request_put(parent_request);
Alex Elder3d7efd12013-04-19 15:34:50 -05002771 return result;
2772}
2773
Alex Elderc5b5ef62013-02-11 12:33:24 -06002774static void rbd_img_obj_exists_callback(struct rbd_obj_request *obj_request)
2775{
Alex Elderc5b5ef62013-02-11 12:33:24 -06002776 struct rbd_obj_request *orig_request;
Alex Elder638f5ab2013-05-06 17:40:33 -05002777 struct rbd_device *rbd_dev;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002778 int result;
2779
2780 rbd_assert(!obj_request_img_data_test(obj_request));
2781
2782 /*
2783 * All we need from the object request is the original
2784 * request and the result of the STAT op. Grab those, then
2785 * we're done with the request.
2786 */
2787 orig_request = obj_request->obj_request;
2788 obj_request->obj_request = NULL;
Alex Elder912c3172013-05-13 20:35:38 -05002789 rbd_obj_request_put(orig_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002790 rbd_assert(orig_request);
2791 rbd_assert(orig_request->img_request);
2792
2793 result = obj_request->result;
2794 obj_request->result = 0;
2795
2796 dout("%s: obj %p for obj %p result %d %llu/%llu\n", __func__,
2797 obj_request, orig_request, result,
2798 obj_request->xferred, obj_request->length);
2799 rbd_obj_request_put(obj_request);
2800
Alex Elder638f5ab2013-05-06 17:40:33 -05002801 /*
2802 * If the overlap has become 0 (most likely because the
Ilya Dryomov980917f2016-09-12 18:59:42 +02002803 * image has been flattened) we need to re-submit the
2804 * original request.
Alex Elder638f5ab2013-05-06 17:40:33 -05002805 */
2806 rbd_dev = orig_request->img_request->rbd_dev;
2807 if (!rbd_dev->parent_overlap) {
Ilya Dryomov980917f2016-09-12 18:59:42 +02002808 rbd_obj_request_submit(orig_request);
2809 return;
Alex Elder638f5ab2013-05-06 17:40:33 -05002810 }
Alex Elderc5b5ef62013-02-11 12:33:24 -06002811
2812 /*
2813 * Our only purpose here is to determine whether the object
2814 * exists, and we don't want to treat the non-existence as
2815 * an error. If something else comes back, transfer the
2816 * error to the original request and complete it now.
2817 */
2818 if (!result) {
2819 obj_request_existence_set(orig_request, true);
2820 } else if (result == -ENOENT) {
2821 obj_request_existence_set(orig_request, false);
Ilya Dryomovc2e82412016-09-13 20:18:01 +02002822 } else {
2823 goto fail_orig_request;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002824 }
2825
2826 /*
2827 * Resubmit the original request now that we have recorded
2828 * whether the target object exists.
2829 */
Ilya Dryomovc2e82412016-09-13 20:18:01 +02002830 result = rbd_img_obj_request_submit(orig_request);
2831 if (result)
2832 goto fail_orig_request;
2833
2834 return;
2835
2836fail_orig_request:
Ilya Dryomov0dcc6852016-09-26 15:43:52 +02002837 rbd_obj_request_error(orig_request, result);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002838}
2839
2840static int rbd_img_obj_exists_submit(struct rbd_obj_request *obj_request)
2841{
Ilya Dryomov058aa992016-09-12 14:44:45 +02002842 struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002843 struct rbd_obj_request *stat_request;
Ilya Dryomov710214e2016-09-15 17:53:32 +02002844 struct page **pages;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002845 u32 page_count;
2846 size_t size;
2847 int ret;
2848
Ilya Dryomov67e2b652017-01-25 18:16:22 +01002849 stat_request = rbd_obj_request_create(obj_request->object_name,
Ilya Dryomov710214e2016-09-15 17:53:32 +02002850 OBJ_REQUEST_PAGES);
2851 if (!stat_request)
2852 return -ENOMEM;
2853
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01002854 stat_request->object_no = obj_request->object_no;
2855
Ilya Dryomov710214e2016-09-15 17:53:32 +02002856 stat_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_READ, 1,
2857 stat_request);
2858 if (!stat_request->osd_req) {
2859 ret = -ENOMEM;
2860 goto fail_stat_request;
2861 }
2862
Alex Elderc5b5ef62013-02-11 12:33:24 -06002863 /*
2864 * The response data for a STAT call consists of:
2865 * le64 length;
2866 * struct {
2867 * le32 tv_sec;
2868 * le32 tv_nsec;
2869 * } mtime;
2870 */
2871 size = sizeof (__le64) + sizeof (__le32) + sizeof (__le32);
2872 page_count = (u32)calc_pages_for(0, size);
2873 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
Ilya Dryomov710214e2016-09-15 17:53:32 +02002874 if (IS_ERR(pages)) {
2875 ret = PTR_ERR(pages);
2876 goto fail_stat_request;
2877 }
Alex Elderc5b5ef62013-02-11 12:33:24 -06002878
Ilya Dryomov710214e2016-09-15 17:53:32 +02002879 osd_req_op_init(stat_request->osd_req, 0, CEPH_OSD_OP_STAT, 0);
2880 osd_req_op_raw_data_in_pages(stat_request->osd_req, 0, pages, size, 0,
2881 false, false);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002882
2883 rbd_obj_request_get(obj_request);
2884 stat_request->obj_request = obj_request;
2885 stat_request->pages = pages;
2886 stat_request->page_count = page_count;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002887 stat_request->callback = rbd_img_obj_exists_callback;
2888
Ilya Dryomov980917f2016-09-12 18:59:42 +02002889 rbd_obj_request_submit(stat_request);
2890 return 0;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002891
Ilya Dryomov710214e2016-09-15 17:53:32 +02002892fail_stat_request:
2893 rbd_obj_request_put(stat_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002894 return ret;
2895}
2896
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002897static bool img_obj_request_simple(struct rbd_obj_request *obj_request)
Alex Elderb454e362013-04-19 15:34:50 -05002898{
Ilya Dryomov058aa992016-09-12 14:44:45 +02002899 struct rbd_img_request *img_request = obj_request->img_request;
2900 struct rbd_device *rbd_dev = img_request->rbd_dev;
Alex Elderb454e362013-04-19 15:34:50 -05002901
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002902 /* Reads */
Josh Durgin1c220882014-04-04 17:49:12 -07002903 if (!img_request_write_test(img_request) &&
2904 !img_request_discard_test(img_request))
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002905 return true;
Alex Elderb454e362013-04-19 15:34:50 -05002906
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002907 /* Non-layered writes */
2908 if (!img_request_layered_test(img_request))
2909 return true;
2910
2911 /*
2912 * Layered writes outside of the parent overlap range don't
2913 * share any data with the parent.
2914 */
2915 if (!obj_request_overlaps_parent(obj_request))
2916 return true;
2917
2918 /*
Guangliang Zhaoc622d222014-04-01 22:22:15 +08002919 * Entire-object layered writes - we will overwrite whatever
2920 * parent data there is anyway.
2921 */
2922 if (!obj_request->offset &&
2923 obj_request->length == rbd_obj_bytes(&rbd_dev->header))
2924 return true;
2925
2926 /*
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002927 * If the object is known to already exist, its parent data has
2928 * already been copied.
2929 */
2930 if (obj_request_known_test(obj_request) &&
2931 obj_request_exists_test(obj_request))
2932 return true;
2933
2934 return false;
2935}
2936
2937static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request)
2938{
Ilya Dryomov058aa992016-09-12 14:44:45 +02002939 rbd_assert(obj_request_img_data_test(obj_request));
2940 rbd_assert(obj_request_type_valid(obj_request->type));
2941 rbd_assert(obj_request->img_request);
2942
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002943 if (img_obj_request_simple(obj_request)) {
Ilya Dryomov980917f2016-09-12 18:59:42 +02002944 rbd_obj_request_submit(obj_request);
2945 return 0;
Alex Elderb454e362013-04-19 15:34:50 -05002946 }
2947
2948 /*
Alex Elder3d7efd12013-04-19 15:34:50 -05002949 * It's a layered write. The target object might exist but
2950 * we may not know that yet. If we know it doesn't exist,
2951 * start by reading the data for the full target object from
2952 * the parent so we can use it for a copyup to the target.
Alex Elderb454e362013-04-19 15:34:50 -05002953 */
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002954 if (obj_request_known_test(obj_request))
Alex Elder3d7efd12013-04-19 15:34:50 -05002955 return rbd_img_obj_parent_read_full(obj_request);
2956
2957 /* We don't know whether the target exists. Go find out. */
Alex Elderb454e362013-04-19 15:34:50 -05002958
2959 return rbd_img_obj_exists_submit(obj_request);
2960}
2961
Alex Elderbf0d5f502012-11-22 00:00:08 -06002962static int rbd_img_request_submit(struct rbd_img_request *img_request)
2963{
Alex Elderbf0d5f502012-11-22 00:00:08 -06002964 struct rbd_obj_request *obj_request;
Alex Elder46faeed2013-04-10 17:47:46 -05002965 struct rbd_obj_request *next_obj_request;
Ilya Dryomov663ae2c2016-05-16 13:18:57 +02002966 int ret = 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002967
Alex Elder37206ee2013-02-20 17:32:08 -06002968 dout("%s: img %p\n", __func__, img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002969
Ilya Dryomov663ae2c2016-05-16 13:18:57 +02002970 rbd_img_request_get(img_request);
2971 for_each_obj_request_safe(img_request, obj_request, next_obj_request) {
Alex Elderb454e362013-04-19 15:34:50 -05002972 ret = rbd_img_obj_request_submit(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002973 if (ret)
Ilya Dryomov663ae2c2016-05-16 13:18:57 +02002974 goto out_put_ireq;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002975 }
2976
Ilya Dryomov663ae2c2016-05-16 13:18:57 +02002977out_put_ireq:
2978 rbd_img_request_put(img_request);
2979 return ret;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002980}
2981
Alex Elder8b3e1a52013-01-24 16:13:36 -06002982static void rbd_img_parent_read_callback(struct rbd_img_request *img_request)
2983{
2984 struct rbd_obj_request *obj_request;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002985 struct rbd_device *rbd_dev;
2986 u64 obj_end;
Alex Elder02c74fb2013-05-06 17:40:33 -05002987 u64 img_xferred;
2988 int img_result;
Alex Elder8b3e1a52013-01-24 16:13:36 -06002989
2990 rbd_assert(img_request_child_test(img_request));
2991
Alex Elder02c74fb2013-05-06 17:40:33 -05002992 /* First get what we need from the image request and release it */
2993
Alex Elder8b3e1a52013-01-24 16:13:36 -06002994 obj_request = img_request->obj_request;
Alex Elder02c74fb2013-05-06 17:40:33 -05002995 img_xferred = img_request->xferred;
2996 img_result = img_request->result;
2997 rbd_img_request_put(img_request);
2998
2999 /*
3000 * If the overlap has become 0 (most likely because the
3001 * image has been flattened) we need to re-submit the
3002 * original request.
3003 */
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05003004 rbd_assert(obj_request);
3005 rbd_assert(obj_request->img_request);
Alex Elder02c74fb2013-05-06 17:40:33 -05003006 rbd_dev = obj_request->img_request->rbd_dev;
3007 if (!rbd_dev->parent_overlap) {
Ilya Dryomov980917f2016-09-12 18:59:42 +02003008 rbd_obj_request_submit(obj_request);
3009 return;
Alex Elder02c74fb2013-05-06 17:40:33 -05003010 }
3011
3012 obj_request->result = img_result;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05003013 if (obj_request->result)
3014 goto out;
3015
3016 /*
3017 * We need to zero anything beyond the parent overlap
3018 * boundary. Since rbd_img_obj_request_read_callback()
3019 * will zero anything beyond the end of a short read, an
3020 * easy way to do this is to pretend the data from the
3021 * parent came up short--ending at the overlap boundary.
3022 */
3023 rbd_assert(obj_request->img_offset < U64_MAX - obj_request->length);
3024 obj_end = obj_request->img_offset + obj_request->length;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05003025 if (obj_end > rbd_dev->parent_overlap) {
3026 u64 xferred = 0;
3027
3028 if (obj_request->img_offset < rbd_dev->parent_overlap)
3029 xferred = rbd_dev->parent_overlap -
3030 obj_request->img_offset;
3031
Alex Elder02c74fb2013-05-06 17:40:33 -05003032 obj_request->xferred = min(img_xferred, xferred);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05003033 } else {
Alex Elder02c74fb2013-05-06 17:40:33 -05003034 obj_request->xferred = img_xferred;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05003035 }
3036out:
Alex Elder8b3e1a52013-01-24 16:13:36 -06003037 rbd_img_obj_request_read_callback(obj_request);
3038 rbd_obj_request_complete(obj_request);
3039}
3040
3041static void rbd_img_parent_read(struct rbd_obj_request *obj_request)
3042{
Alex Elder8b3e1a52013-01-24 16:13:36 -06003043 struct rbd_img_request *img_request;
3044 int result;
3045
3046 rbd_assert(obj_request_img_data_test(obj_request));
3047 rbd_assert(obj_request->img_request != NULL);
3048 rbd_assert(obj_request->result == (s32) -ENOENT);
Alex Elder5b2ab722013-05-06 17:40:33 -05003049 rbd_assert(obj_request_type_valid(obj_request->type));
Alex Elder8b3e1a52013-01-24 16:13:36 -06003050
Alex Elder8b3e1a52013-01-24 16:13:36 -06003051 /* rbd_read_finish(obj_request, obj_request->length); */
Alex Eldere93f3152013-05-08 22:50:04 -05003052 img_request = rbd_parent_request_create(obj_request,
Alex Elder8b3e1a52013-01-24 16:13:36 -06003053 obj_request->img_offset,
Alex Eldere93f3152013-05-08 22:50:04 -05003054 obj_request->length);
Alex Elder8b3e1a52013-01-24 16:13:36 -06003055 result = -ENOMEM;
3056 if (!img_request)
3057 goto out_err;
3058
Alex Elder5b2ab722013-05-06 17:40:33 -05003059 if (obj_request->type == OBJ_REQUEST_BIO)
3060 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
3061 obj_request->bio_list);
3062 else
3063 result = rbd_img_request_fill(img_request, OBJ_REQUEST_PAGES,
3064 obj_request->pages);
Alex Elder8b3e1a52013-01-24 16:13:36 -06003065 if (result)
3066 goto out_err;
3067
3068 img_request->callback = rbd_img_parent_read_callback;
3069 result = rbd_img_request_submit(img_request);
3070 if (result)
3071 goto out_err;
3072
3073 return;
3074out_err:
3075 if (img_request)
3076 rbd_img_request_put(img_request);
3077 obj_request->result = result;
3078 obj_request->xferred = 0;
3079 obj_request_done_set(obj_request);
3080}
3081
Ilya Dryomoved95b212016-08-12 16:40:02 +02003082static const struct rbd_client_id rbd_empty_cid;
3083
3084static bool rbd_cid_equal(const struct rbd_client_id *lhs,
3085 const struct rbd_client_id *rhs)
3086{
3087 return lhs->gid == rhs->gid && lhs->handle == rhs->handle;
3088}
3089
3090static struct rbd_client_id rbd_get_cid(struct rbd_device *rbd_dev)
3091{
3092 struct rbd_client_id cid;
3093
3094 mutex_lock(&rbd_dev->watch_mutex);
3095 cid.gid = ceph_client_gid(rbd_dev->rbd_client->client);
3096 cid.handle = rbd_dev->watch_cookie;
3097 mutex_unlock(&rbd_dev->watch_mutex);
3098 return cid;
3099}
3100
3101/*
3102 * lock_rwsem must be held for write
3103 */
3104static void rbd_set_owner_cid(struct rbd_device *rbd_dev,
3105 const struct rbd_client_id *cid)
3106{
3107 dout("%s rbd_dev %p %llu-%llu -> %llu-%llu\n", __func__, rbd_dev,
3108 rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle,
3109 cid->gid, cid->handle);
3110 rbd_dev->owner_cid = *cid; /* struct */
3111}
3112
3113static void format_lock_cookie(struct rbd_device *rbd_dev, char *buf)
3114{
3115 mutex_lock(&rbd_dev->watch_mutex);
3116 sprintf(buf, "%s %llu", RBD_LOCK_COOKIE_PREFIX, rbd_dev->watch_cookie);
3117 mutex_unlock(&rbd_dev->watch_mutex);
3118}
3119
3120/*
3121 * lock_rwsem must be held for write
3122 */
3123static int rbd_lock(struct rbd_device *rbd_dev)
3124{
3125 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3126 struct rbd_client_id cid = rbd_get_cid(rbd_dev);
3127 char cookie[32];
3128 int ret;
3129
3130 WARN_ON(__rbd_is_lock_owner(rbd_dev));
3131
3132 format_lock_cookie(rbd_dev, cookie);
3133 ret = ceph_cls_lock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc,
3134 RBD_LOCK_NAME, CEPH_CLS_LOCK_EXCLUSIVE, cookie,
3135 RBD_LOCK_TAG, "", 0);
3136 if (ret)
3137 return ret;
3138
3139 rbd_dev->lock_state = RBD_LOCK_STATE_LOCKED;
3140 rbd_set_owner_cid(rbd_dev, &cid);
3141 queue_work(rbd_dev->task_wq, &rbd_dev->acquired_lock_work);
3142 return 0;
3143}
3144
3145/*
3146 * lock_rwsem must be held for write
3147 */
3148static int rbd_unlock(struct rbd_device *rbd_dev)
3149{
3150 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3151 char cookie[32];
3152 int ret;
3153
3154 WARN_ON(!__rbd_is_lock_owner(rbd_dev));
3155
3156 rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED;
3157
3158 format_lock_cookie(rbd_dev, cookie);
3159 ret = ceph_cls_unlock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc,
3160 RBD_LOCK_NAME, cookie);
3161 if (ret && ret != -ENOENT) {
3162 rbd_warn(rbd_dev, "cls_unlock failed: %d", ret);
3163 return ret;
3164 }
3165
3166 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
3167 queue_work(rbd_dev->task_wq, &rbd_dev->released_lock_work);
3168 return 0;
3169}
3170
3171static int __rbd_notify_op_lock(struct rbd_device *rbd_dev,
3172 enum rbd_notify_op notify_op,
3173 struct page ***preply_pages,
3174 size_t *preply_len)
3175{
3176 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3177 struct rbd_client_id cid = rbd_get_cid(rbd_dev);
3178 int buf_size = 4 + 8 + 8 + CEPH_ENCODING_START_BLK_LEN;
3179 char buf[buf_size];
3180 void *p = buf;
3181
3182 dout("%s rbd_dev %p notify_op %d\n", __func__, rbd_dev, notify_op);
3183
3184 /* encode *LockPayload NotifyMessage (op + ClientId) */
3185 ceph_start_encoding(&p, 2, 1, buf_size - CEPH_ENCODING_START_BLK_LEN);
3186 ceph_encode_32(&p, notify_op);
3187 ceph_encode_64(&p, cid.gid);
3188 ceph_encode_64(&p, cid.handle);
3189
3190 return ceph_osdc_notify(osdc, &rbd_dev->header_oid,
3191 &rbd_dev->header_oloc, buf, buf_size,
3192 RBD_NOTIFY_TIMEOUT, preply_pages, preply_len);
3193}
3194
3195static void rbd_notify_op_lock(struct rbd_device *rbd_dev,
3196 enum rbd_notify_op notify_op)
3197{
3198 struct page **reply_pages;
3199 size_t reply_len;
3200
3201 __rbd_notify_op_lock(rbd_dev, notify_op, &reply_pages, &reply_len);
3202 ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len));
3203}
3204
3205static void rbd_notify_acquired_lock(struct work_struct *work)
3206{
3207 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
3208 acquired_lock_work);
3209
3210 rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_ACQUIRED_LOCK);
3211}
3212
3213static void rbd_notify_released_lock(struct work_struct *work)
3214{
3215 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
3216 released_lock_work);
3217
3218 rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_RELEASED_LOCK);
3219}
3220
3221static int rbd_request_lock(struct rbd_device *rbd_dev)
3222{
3223 struct page **reply_pages;
3224 size_t reply_len;
3225 bool lock_owner_responded = false;
3226 int ret;
3227
3228 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3229
3230 ret = __rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_REQUEST_LOCK,
3231 &reply_pages, &reply_len);
3232 if (ret && ret != -ETIMEDOUT) {
3233 rbd_warn(rbd_dev, "failed to request lock: %d", ret);
3234 goto out;
3235 }
3236
3237 if (reply_len > 0 && reply_len <= PAGE_SIZE) {
3238 void *p = page_address(reply_pages[0]);
3239 void *const end = p + reply_len;
3240 u32 n;
3241
3242 ceph_decode_32_safe(&p, end, n, e_inval); /* num_acks */
3243 while (n--) {
3244 u8 struct_v;
3245 u32 len;
3246
3247 ceph_decode_need(&p, end, 8 + 8, e_inval);
3248 p += 8 + 8; /* skip gid and cookie */
3249
3250 ceph_decode_32_safe(&p, end, len, e_inval);
3251 if (!len)
3252 continue;
3253
3254 if (lock_owner_responded) {
3255 rbd_warn(rbd_dev,
3256 "duplicate lock owners detected");
3257 ret = -EIO;
3258 goto out;
3259 }
3260
3261 lock_owner_responded = true;
3262 ret = ceph_start_decoding(&p, end, 1, "ResponseMessage",
3263 &struct_v, &len);
3264 if (ret) {
3265 rbd_warn(rbd_dev,
3266 "failed to decode ResponseMessage: %d",
3267 ret);
3268 goto e_inval;
3269 }
3270
3271 ret = ceph_decode_32(&p);
3272 }
3273 }
3274
3275 if (!lock_owner_responded) {
3276 rbd_warn(rbd_dev, "no lock owners detected");
3277 ret = -ETIMEDOUT;
3278 }
3279
3280out:
3281 ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len));
3282 return ret;
3283
3284e_inval:
3285 ret = -EINVAL;
3286 goto out;
3287}
3288
3289static void wake_requests(struct rbd_device *rbd_dev, bool wake_all)
3290{
3291 dout("%s rbd_dev %p wake_all %d\n", __func__, rbd_dev, wake_all);
3292
3293 cancel_delayed_work(&rbd_dev->lock_dwork);
3294 if (wake_all)
3295 wake_up_all(&rbd_dev->lock_waitq);
3296 else
3297 wake_up(&rbd_dev->lock_waitq);
3298}
3299
3300static int get_lock_owner_info(struct rbd_device *rbd_dev,
3301 struct ceph_locker **lockers, u32 *num_lockers)
3302{
3303 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3304 u8 lock_type;
3305 char *lock_tag;
3306 int ret;
3307
3308 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3309
3310 ret = ceph_cls_lock_info(osdc, &rbd_dev->header_oid,
3311 &rbd_dev->header_oloc, RBD_LOCK_NAME,
3312 &lock_type, &lock_tag, lockers, num_lockers);
3313 if (ret)
3314 return ret;
3315
3316 if (*num_lockers == 0) {
3317 dout("%s rbd_dev %p no lockers detected\n", __func__, rbd_dev);
3318 goto out;
3319 }
3320
3321 if (strcmp(lock_tag, RBD_LOCK_TAG)) {
3322 rbd_warn(rbd_dev, "locked by external mechanism, tag %s",
3323 lock_tag);
3324 ret = -EBUSY;
3325 goto out;
3326 }
3327
3328 if (lock_type == CEPH_CLS_LOCK_SHARED) {
3329 rbd_warn(rbd_dev, "shared lock type detected");
3330 ret = -EBUSY;
3331 goto out;
3332 }
3333
3334 if (strncmp((*lockers)[0].id.cookie, RBD_LOCK_COOKIE_PREFIX,
3335 strlen(RBD_LOCK_COOKIE_PREFIX))) {
3336 rbd_warn(rbd_dev, "locked by external mechanism, cookie %s",
3337 (*lockers)[0].id.cookie);
3338 ret = -EBUSY;
3339 goto out;
3340 }
3341
3342out:
3343 kfree(lock_tag);
3344 return ret;
3345}
3346
3347static int find_watcher(struct rbd_device *rbd_dev,
3348 const struct ceph_locker *locker)
3349{
3350 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3351 struct ceph_watch_item *watchers;
3352 u32 num_watchers;
3353 u64 cookie;
3354 int i;
3355 int ret;
3356
3357 ret = ceph_osdc_list_watchers(osdc, &rbd_dev->header_oid,
3358 &rbd_dev->header_oloc, &watchers,
3359 &num_watchers);
3360 if (ret)
3361 return ret;
3362
3363 sscanf(locker->id.cookie, RBD_LOCK_COOKIE_PREFIX " %llu", &cookie);
3364 for (i = 0; i < num_watchers; i++) {
3365 if (!memcmp(&watchers[i].addr, &locker->info.addr,
3366 sizeof(locker->info.addr)) &&
3367 watchers[i].cookie == cookie) {
3368 struct rbd_client_id cid = {
3369 .gid = le64_to_cpu(watchers[i].name.num),
3370 .handle = cookie,
3371 };
3372
3373 dout("%s rbd_dev %p found cid %llu-%llu\n", __func__,
3374 rbd_dev, cid.gid, cid.handle);
3375 rbd_set_owner_cid(rbd_dev, &cid);
3376 ret = 1;
3377 goto out;
3378 }
3379 }
3380
3381 dout("%s rbd_dev %p no watchers\n", __func__, rbd_dev);
3382 ret = 0;
3383out:
3384 kfree(watchers);
3385 return ret;
3386}
3387
3388/*
3389 * lock_rwsem must be held for write
3390 */
3391static int rbd_try_lock(struct rbd_device *rbd_dev)
3392{
3393 struct ceph_client *client = rbd_dev->rbd_client->client;
3394 struct ceph_locker *lockers;
3395 u32 num_lockers;
3396 int ret;
3397
3398 for (;;) {
3399 ret = rbd_lock(rbd_dev);
3400 if (ret != -EBUSY)
3401 return ret;
3402
3403 /* determine if the current lock holder is still alive */
3404 ret = get_lock_owner_info(rbd_dev, &lockers, &num_lockers);
3405 if (ret)
3406 return ret;
3407
3408 if (num_lockers == 0)
3409 goto again;
3410
3411 ret = find_watcher(rbd_dev, lockers);
3412 if (ret) {
3413 if (ret > 0)
3414 ret = 0; /* have to request lock */
3415 goto out;
3416 }
3417
3418 rbd_warn(rbd_dev, "%s%llu seems dead, breaking lock",
3419 ENTITY_NAME(lockers[0].id.name));
3420
3421 ret = ceph_monc_blacklist_add(&client->monc,
3422 &lockers[0].info.addr);
3423 if (ret) {
3424 rbd_warn(rbd_dev, "blacklist of %s%llu failed: %d",
3425 ENTITY_NAME(lockers[0].id.name), ret);
3426 goto out;
3427 }
3428
3429 ret = ceph_cls_break_lock(&client->osdc, &rbd_dev->header_oid,
3430 &rbd_dev->header_oloc, RBD_LOCK_NAME,
3431 lockers[0].id.cookie,
3432 &lockers[0].id.name);
3433 if (ret && ret != -ENOENT)
3434 goto out;
3435
3436again:
3437 ceph_free_lockers(lockers, num_lockers);
3438 }
3439
3440out:
3441 ceph_free_lockers(lockers, num_lockers);
3442 return ret;
3443}
3444
3445/*
3446 * ret is set only if lock_state is RBD_LOCK_STATE_UNLOCKED
3447 */
3448static enum rbd_lock_state rbd_try_acquire_lock(struct rbd_device *rbd_dev,
3449 int *pret)
3450{
3451 enum rbd_lock_state lock_state;
3452
3453 down_read(&rbd_dev->lock_rwsem);
3454 dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev,
3455 rbd_dev->lock_state);
3456 if (__rbd_is_lock_owner(rbd_dev)) {
3457 lock_state = rbd_dev->lock_state;
3458 up_read(&rbd_dev->lock_rwsem);
3459 return lock_state;
3460 }
3461
3462 up_read(&rbd_dev->lock_rwsem);
3463 down_write(&rbd_dev->lock_rwsem);
3464 dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev,
3465 rbd_dev->lock_state);
3466 if (!__rbd_is_lock_owner(rbd_dev)) {
3467 *pret = rbd_try_lock(rbd_dev);
3468 if (*pret)
3469 rbd_warn(rbd_dev, "failed to acquire lock: %d", *pret);
3470 }
3471
3472 lock_state = rbd_dev->lock_state;
3473 up_write(&rbd_dev->lock_rwsem);
3474 return lock_state;
3475}
3476
3477static void rbd_acquire_lock(struct work_struct *work)
3478{
3479 struct rbd_device *rbd_dev = container_of(to_delayed_work(work),
3480 struct rbd_device, lock_dwork);
3481 enum rbd_lock_state lock_state;
3482 int ret;
3483
3484 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3485again:
3486 lock_state = rbd_try_acquire_lock(rbd_dev, &ret);
3487 if (lock_state != RBD_LOCK_STATE_UNLOCKED || ret == -EBLACKLISTED) {
3488 if (lock_state == RBD_LOCK_STATE_LOCKED)
3489 wake_requests(rbd_dev, true);
3490 dout("%s rbd_dev %p lock_state %d ret %d - done\n", __func__,
3491 rbd_dev, lock_state, ret);
3492 return;
3493 }
3494
3495 ret = rbd_request_lock(rbd_dev);
3496 if (ret == -ETIMEDOUT) {
3497 goto again; /* treat this as a dead client */
3498 } else if (ret < 0) {
3499 rbd_warn(rbd_dev, "error requesting lock: %d", ret);
3500 mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork,
3501 RBD_RETRY_DELAY);
3502 } else {
3503 /*
3504 * lock owner acked, but resend if we don't see them
3505 * release the lock
3506 */
3507 dout("%s rbd_dev %p requeueing lock_dwork\n", __func__,
3508 rbd_dev);
3509 mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork,
3510 msecs_to_jiffies(2 * RBD_NOTIFY_TIMEOUT * MSEC_PER_SEC));
3511 }
3512}
3513
3514/*
3515 * lock_rwsem must be held for write
3516 */
3517static bool rbd_release_lock(struct rbd_device *rbd_dev)
3518{
3519 dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev,
3520 rbd_dev->lock_state);
3521 if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED)
3522 return false;
3523
3524 rbd_dev->lock_state = RBD_LOCK_STATE_RELEASING;
3525 downgrade_write(&rbd_dev->lock_rwsem);
3526 /*
3527 * Ensure that all in-flight IO is flushed.
3528 *
3529 * FIXME: ceph_osdc_sync() flushes the entire OSD client, which
3530 * may be shared with other devices.
3531 */
3532 ceph_osdc_sync(&rbd_dev->rbd_client->client->osdc);
3533 up_read(&rbd_dev->lock_rwsem);
3534
3535 down_write(&rbd_dev->lock_rwsem);
3536 dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev,
3537 rbd_dev->lock_state);
3538 if (rbd_dev->lock_state != RBD_LOCK_STATE_RELEASING)
3539 return false;
3540
3541 if (!rbd_unlock(rbd_dev))
3542 /*
3543 * Give others a chance to grab the lock - we would re-acquire
3544 * almost immediately if we got new IO during ceph_osdc_sync()
3545 * otherwise. We need to ack our own notifications, so this
3546 * lock_dwork will be requeued from rbd_wait_state_locked()
3547 * after wake_requests() in rbd_handle_released_lock().
3548 */
3549 cancel_delayed_work(&rbd_dev->lock_dwork);
3550
3551 return true;
3552}
3553
3554static void rbd_release_lock_work(struct work_struct *work)
3555{
3556 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
3557 unlock_work);
3558
3559 down_write(&rbd_dev->lock_rwsem);
3560 rbd_release_lock(rbd_dev);
3561 up_write(&rbd_dev->lock_rwsem);
3562}
3563
3564static void rbd_handle_acquired_lock(struct rbd_device *rbd_dev, u8 struct_v,
3565 void **p)
3566{
3567 struct rbd_client_id cid = { 0 };
3568
3569 if (struct_v >= 2) {
3570 cid.gid = ceph_decode_64(p);
3571 cid.handle = ceph_decode_64(p);
3572 }
3573
3574 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
3575 cid.handle);
3576 if (!rbd_cid_equal(&cid, &rbd_empty_cid)) {
3577 down_write(&rbd_dev->lock_rwsem);
3578 if (rbd_cid_equal(&cid, &rbd_dev->owner_cid)) {
3579 /*
3580 * we already know that the remote client is
3581 * the owner
3582 */
3583 up_write(&rbd_dev->lock_rwsem);
3584 return;
3585 }
3586
3587 rbd_set_owner_cid(rbd_dev, &cid);
3588 downgrade_write(&rbd_dev->lock_rwsem);
3589 } else {
3590 down_read(&rbd_dev->lock_rwsem);
3591 }
3592
3593 if (!__rbd_is_lock_owner(rbd_dev))
3594 wake_requests(rbd_dev, false);
3595 up_read(&rbd_dev->lock_rwsem);
3596}
3597
3598static void rbd_handle_released_lock(struct rbd_device *rbd_dev, u8 struct_v,
3599 void **p)
3600{
3601 struct rbd_client_id cid = { 0 };
3602
3603 if (struct_v >= 2) {
3604 cid.gid = ceph_decode_64(p);
3605 cid.handle = ceph_decode_64(p);
3606 }
3607
3608 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
3609 cid.handle);
3610 if (!rbd_cid_equal(&cid, &rbd_empty_cid)) {
3611 down_write(&rbd_dev->lock_rwsem);
3612 if (!rbd_cid_equal(&cid, &rbd_dev->owner_cid)) {
3613 dout("%s rbd_dev %p unexpected owner, cid %llu-%llu != owner_cid %llu-%llu\n",
3614 __func__, rbd_dev, cid.gid, cid.handle,
3615 rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle);
3616 up_write(&rbd_dev->lock_rwsem);
3617 return;
3618 }
3619
3620 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
3621 downgrade_write(&rbd_dev->lock_rwsem);
3622 } else {
3623 down_read(&rbd_dev->lock_rwsem);
3624 }
3625
3626 if (!__rbd_is_lock_owner(rbd_dev))
3627 wake_requests(rbd_dev, false);
3628 up_read(&rbd_dev->lock_rwsem);
3629}
3630
3631static bool rbd_handle_request_lock(struct rbd_device *rbd_dev, u8 struct_v,
3632 void **p)
3633{
3634 struct rbd_client_id my_cid = rbd_get_cid(rbd_dev);
3635 struct rbd_client_id cid = { 0 };
3636 bool need_to_send;
3637
3638 if (struct_v >= 2) {
3639 cid.gid = ceph_decode_64(p);
3640 cid.handle = ceph_decode_64(p);
3641 }
3642
3643 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
3644 cid.handle);
3645 if (rbd_cid_equal(&cid, &my_cid))
3646 return false;
3647
3648 down_read(&rbd_dev->lock_rwsem);
3649 need_to_send = __rbd_is_lock_owner(rbd_dev);
3650 if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) {
3651 if (!rbd_cid_equal(&rbd_dev->owner_cid, &rbd_empty_cid)) {
3652 dout("%s rbd_dev %p queueing unlock_work\n", __func__,
3653 rbd_dev);
3654 queue_work(rbd_dev->task_wq, &rbd_dev->unlock_work);
3655 }
3656 }
3657 up_read(&rbd_dev->lock_rwsem);
3658 return need_to_send;
3659}
3660
3661static void __rbd_acknowledge_notify(struct rbd_device *rbd_dev,
3662 u64 notify_id, u64 cookie, s32 *result)
3663{
3664 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3665 int buf_size = 4 + CEPH_ENCODING_START_BLK_LEN;
3666 char buf[buf_size];
3667 int ret;
3668
3669 if (result) {
3670 void *p = buf;
3671
3672 /* encode ResponseMessage */
3673 ceph_start_encoding(&p, 1, 1,
3674 buf_size - CEPH_ENCODING_START_BLK_LEN);
3675 ceph_encode_32(&p, *result);
3676 } else {
3677 buf_size = 0;
3678 }
3679
3680 ret = ceph_osdc_notify_ack(osdc, &rbd_dev->header_oid,
3681 &rbd_dev->header_oloc, notify_id, cookie,
3682 buf, buf_size);
3683 if (ret)
3684 rbd_warn(rbd_dev, "acknowledge_notify failed: %d", ret);
3685}
3686
3687static void rbd_acknowledge_notify(struct rbd_device *rbd_dev, u64 notify_id,
3688 u64 cookie)
3689{
3690 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3691 __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, NULL);
3692}
3693
3694static void rbd_acknowledge_notify_result(struct rbd_device *rbd_dev,
3695 u64 notify_id, u64 cookie, s32 result)
3696{
3697 dout("%s rbd_dev %p result %d\n", __func__, rbd_dev, result);
3698 __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, &result);
3699}
Ilya Dryomov922dab62016-05-26 01:15:02 +02003700
3701static void rbd_watch_cb(void *arg, u64 notify_id, u64 cookie,
3702 u64 notifier_id, void *data, size_t data_len)
Alex Elderb8d70032012-11-30 17:53:04 -06003703{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003704 struct rbd_device *rbd_dev = arg;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003705 void *p = data;
3706 void *const end = p + data_len;
Ilya Dryomovd4c22692016-09-06 11:15:48 +02003707 u8 struct_v = 0;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003708 u32 len;
3709 u32 notify_op;
Alex Elderb8d70032012-11-30 17:53:04 -06003710 int ret;
3711
Ilya Dryomoved95b212016-08-12 16:40:02 +02003712 dout("%s rbd_dev %p cookie %llu notify_id %llu data_len %zu\n",
3713 __func__, rbd_dev, cookie, notify_id, data_len);
3714 if (data_len) {
3715 ret = ceph_start_decoding(&p, end, 1, "NotifyMessage",
3716 &struct_v, &len);
3717 if (ret) {
3718 rbd_warn(rbd_dev, "failed to decode NotifyMessage: %d",
3719 ret);
3720 return;
3721 }
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04003722
Ilya Dryomoved95b212016-08-12 16:40:02 +02003723 notify_op = ceph_decode_32(&p);
3724 } else {
3725 /* legacy notification for header updates */
3726 notify_op = RBD_NOTIFY_OP_HEADER_UPDATE;
3727 len = 0;
3728 }
Alex Elderb8d70032012-11-30 17:53:04 -06003729
Ilya Dryomoved95b212016-08-12 16:40:02 +02003730 dout("%s rbd_dev %p notify_op %u\n", __func__, rbd_dev, notify_op);
3731 switch (notify_op) {
3732 case RBD_NOTIFY_OP_ACQUIRED_LOCK:
3733 rbd_handle_acquired_lock(rbd_dev, struct_v, &p);
3734 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3735 break;
3736 case RBD_NOTIFY_OP_RELEASED_LOCK:
3737 rbd_handle_released_lock(rbd_dev, struct_v, &p);
3738 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3739 break;
3740 case RBD_NOTIFY_OP_REQUEST_LOCK:
3741 if (rbd_handle_request_lock(rbd_dev, struct_v, &p))
3742 /*
3743 * send ResponseMessage(0) back so the client
3744 * can detect a missing owner
3745 */
3746 rbd_acknowledge_notify_result(rbd_dev, notify_id,
3747 cookie, 0);
3748 else
3749 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3750 break;
3751 case RBD_NOTIFY_OP_HEADER_UPDATE:
3752 ret = rbd_dev_refresh(rbd_dev);
3753 if (ret)
3754 rbd_warn(rbd_dev, "refresh failed: %d", ret);
3755
3756 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3757 break;
3758 default:
3759 if (rbd_is_lock_owner(rbd_dev))
3760 rbd_acknowledge_notify_result(rbd_dev, notify_id,
3761 cookie, -EOPNOTSUPP);
3762 else
3763 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3764 break;
3765 }
Alex Elderb8d70032012-11-30 17:53:04 -06003766}
3767
Ilya Dryomov99d16942016-08-12 16:11:41 +02003768static void __rbd_unregister_watch(struct rbd_device *rbd_dev);
3769
Ilya Dryomov922dab62016-05-26 01:15:02 +02003770static void rbd_watch_errcb(void *arg, u64 cookie, int err)
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003771{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003772 struct rbd_device *rbd_dev = arg;
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003773
Ilya Dryomov922dab62016-05-26 01:15:02 +02003774 rbd_warn(rbd_dev, "encountered watch error: %d", err);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003775
Ilya Dryomoved95b212016-08-12 16:40:02 +02003776 down_write(&rbd_dev->lock_rwsem);
3777 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
3778 up_write(&rbd_dev->lock_rwsem);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003779
Ilya Dryomov99d16942016-08-12 16:11:41 +02003780 mutex_lock(&rbd_dev->watch_mutex);
3781 if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) {
3782 __rbd_unregister_watch(rbd_dev);
3783 rbd_dev->watch_state = RBD_WATCH_STATE_ERROR;
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003784
Ilya Dryomov99d16942016-08-12 16:11:41 +02003785 queue_delayed_work(rbd_dev->task_wq, &rbd_dev->watch_dwork, 0);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003786 }
Ilya Dryomov99d16942016-08-12 16:11:41 +02003787 mutex_unlock(&rbd_dev->watch_mutex);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003788}
3789
3790/*
Ilya Dryomov99d16942016-08-12 16:11:41 +02003791 * watch_mutex must be locked
Alex Elder9969ebc2013-01-18 12:31:10 -06003792 */
Ilya Dryomov99d16942016-08-12 16:11:41 +02003793static int __rbd_register_watch(struct rbd_device *rbd_dev)
Alex Elder9969ebc2013-01-18 12:31:10 -06003794{
3795 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Ilya Dryomov922dab62016-05-26 01:15:02 +02003796 struct ceph_osd_linger_request *handle;
Alex Elder9969ebc2013-01-18 12:31:10 -06003797
Ilya Dryomov922dab62016-05-26 01:15:02 +02003798 rbd_assert(!rbd_dev->watch_handle);
Ilya Dryomov99d16942016-08-12 16:11:41 +02003799 dout("%s rbd_dev %p\n", __func__, rbd_dev);
Alex Elder9969ebc2013-01-18 12:31:10 -06003800
Ilya Dryomov922dab62016-05-26 01:15:02 +02003801 handle = ceph_osdc_watch(osdc, &rbd_dev->header_oid,
3802 &rbd_dev->header_oloc, rbd_watch_cb,
3803 rbd_watch_errcb, rbd_dev);
3804 if (IS_ERR(handle))
3805 return PTR_ERR(handle);
Alex Elder9969ebc2013-01-18 12:31:10 -06003806
Ilya Dryomov922dab62016-05-26 01:15:02 +02003807 rbd_dev->watch_handle = handle;
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003808 return 0;
Alex Elder9969ebc2013-01-18 12:31:10 -06003809}
3810
Ilya Dryomov99d16942016-08-12 16:11:41 +02003811/*
3812 * watch_mutex must be locked
3813 */
3814static void __rbd_unregister_watch(struct rbd_device *rbd_dev)
Ilya Dryomovfca27062013-12-16 18:02:40 +02003815{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003816 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3817 int ret;
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003818
Ilya Dryomov99d16942016-08-12 16:11:41 +02003819 rbd_assert(rbd_dev->watch_handle);
3820 dout("%s rbd_dev %p\n", __func__, rbd_dev);
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003821
Ilya Dryomov922dab62016-05-26 01:15:02 +02003822 ret = ceph_osdc_unwatch(osdc, rbd_dev->watch_handle);
3823 if (ret)
3824 rbd_warn(rbd_dev, "failed to unwatch: %d", ret);
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003825
Ilya Dryomov922dab62016-05-26 01:15:02 +02003826 rbd_dev->watch_handle = NULL;
Ilya Dryomovc525f032016-04-28 16:07:26 +02003827}
3828
Ilya Dryomov99d16942016-08-12 16:11:41 +02003829static int rbd_register_watch(struct rbd_device *rbd_dev)
Ilya Dryomovc525f032016-04-28 16:07:26 +02003830{
Ilya Dryomov99d16942016-08-12 16:11:41 +02003831 int ret;
Ilya Dryomov811c6682016-04-15 16:22:16 +02003832
Ilya Dryomov99d16942016-08-12 16:11:41 +02003833 mutex_lock(&rbd_dev->watch_mutex);
3834 rbd_assert(rbd_dev->watch_state == RBD_WATCH_STATE_UNREGISTERED);
3835 ret = __rbd_register_watch(rbd_dev);
3836 if (ret)
3837 goto out;
3838
3839 rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED;
3840 rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id;
3841
3842out:
3843 mutex_unlock(&rbd_dev->watch_mutex);
3844 return ret;
3845}
3846
3847static void cancel_tasks_sync(struct rbd_device *rbd_dev)
3848{
3849 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3850
3851 cancel_delayed_work_sync(&rbd_dev->watch_dwork);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003852 cancel_work_sync(&rbd_dev->acquired_lock_work);
3853 cancel_work_sync(&rbd_dev->released_lock_work);
3854 cancel_delayed_work_sync(&rbd_dev->lock_dwork);
3855 cancel_work_sync(&rbd_dev->unlock_work);
Ilya Dryomov99d16942016-08-12 16:11:41 +02003856}
3857
3858static void rbd_unregister_watch(struct rbd_device *rbd_dev)
3859{
Ilya Dryomoved95b212016-08-12 16:40:02 +02003860 WARN_ON(waitqueue_active(&rbd_dev->lock_waitq));
Ilya Dryomov99d16942016-08-12 16:11:41 +02003861 cancel_tasks_sync(rbd_dev);
3862
3863 mutex_lock(&rbd_dev->watch_mutex);
3864 if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED)
3865 __rbd_unregister_watch(rbd_dev);
3866 rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED;
3867 mutex_unlock(&rbd_dev->watch_mutex);
3868
Ilya Dryomov811c6682016-04-15 16:22:16 +02003869 ceph_osdc_flush_notifies(&rbd_dev->rbd_client->client->osdc);
Ilya Dryomovfca27062013-12-16 18:02:40 +02003870}
3871
Ilya Dryomov99d16942016-08-12 16:11:41 +02003872static void rbd_reregister_watch(struct work_struct *work)
3873{
3874 struct rbd_device *rbd_dev = container_of(to_delayed_work(work),
3875 struct rbd_device, watch_dwork);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003876 bool was_lock_owner = false;
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003877 bool need_to_wake = false;
Ilya Dryomov99d16942016-08-12 16:11:41 +02003878 int ret;
3879
3880 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3881
Ilya Dryomoved95b212016-08-12 16:40:02 +02003882 down_write(&rbd_dev->lock_rwsem);
3883 if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED)
3884 was_lock_owner = rbd_release_lock(rbd_dev);
3885
Ilya Dryomov99d16942016-08-12 16:11:41 +02003886 mutex_lock(&rbd_dev->watch_mutex);
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003887 if (rbd_dev->watch_state != RBD_WATCH_STATE_ERROR) {
3888 mutex_unlock(&rbd_dev->watch_mutex);
3889 goto out;
3890 }
Ilya Dryomov99d16942016-08-12 16:11:41 +02003891
3892 ret = __rbd_register_watch(rbd_dev);
3893 if (ret) {
3894 rbd_warn(rbd_dev, "failed to reregister watch: %d", ret);
Ilya Dryomov4d736442016-09-29 14:23:12 +02003895 if (ret == -EBLACKLISTED || ret == -ENOENT) {
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003896 set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags);
3897 need_to_wake = true;
3898 } else {
Ilya Dryomov99d16942016-08-12 16:11:41 +02003899 queue_delayed_work(rbd_dev->task_wq,
3900 &rbd_dev->watch_dwork,
3901 RBD_RETRY_DELAY);
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003902 }
3903 mutex_unlock(&rbd_dev->watch_mutex);
3904 goto out;
Ilya Dryomov99d16942016-08-12 16:11:41 +02003905 }
3906
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003907 need_to_wake = true;
Ilya Dryomov99d16942016-08-12 16:11:41 +02003908 rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED;
3909 rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id;
3910 mutex_unlock(&rbd_dev->watch_mutex);
3911
3912 ret = rbd_dev_refresh(rbd_dev);
3913 if (ret)
3914 rbd_warn(rbd_dev, "reregisteration refresh failed: %d", ret);
3915
Ilya Dryomoved95b212016-08-12 16:40:02 +02003916 if (was_lock_owner) {
3917 ret = rbd_try_lock(rbd_dev);
3918 if (ret)
3919 rbd_warn(rbd_dev, "reregisteration lock failed: %d",
3920 ret);
3921 }
3922
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003923out:
Ilya Dryomoved95b212016-08-12 16:40:02 +02003924 up_write(&rbd_dev->lock_rwsem);
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003925 if (need_to_wake)
3926 wake_requests(rbd_dev, true);
Ilya Dryomov99d16942016-08-12 16:11:41 +02003927}
3928
Alex Elder36be9a72013-01-19 00:30:28 -06003929/*
Alex Elderf40eb342013-04-25 15:09:42 -05003930 * Synchronous osd object method call. Returns the number of bytes
3931 * returned in the outbound buffer, or a negative error code.
Alex Elder36be9a72013-01-19 00:30:28 -06003932 */
3933static int rbd_obj_method_sync(struct rbd_device *rbd_dev,
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003934 struct ceph_object_id *oid,
3935 struct ceph_object_locator *oloc,
Alex Elder36be9a72013-01-19 00:30:28 -06003936 const char *method_name,
Alex Elder41579762013-04-21 12:14:45 -05003937 const void *outbound,
Alex Elder36be9a72013-01-19 00:30:28 -06003938 size_t outbound_size,
Alex Elder41579762013-04-21 12:14:45 -05003939 void *inbound,
Alex Eldere2a58ee2013-04-30 00:44:33 -05003940 size_t inbound_size)
Alex Elder36be9a72013-01-19 00:30:28 -06003941{
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003942 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3943 struct page *req_page = NULL;
3944 struct page *reply_page;
Alex Elder36be9a72013-01-19 00:30:28 -06003945 int ret;
3946
3947 /*
Alex Elder6010a452013-04-05 01:27:11 -05003948 * Method calls are ultimately read operations. The result
3949 * should placed into the inbound buffer provided. They
3950 * also supply outbound data--parameters for the object
3951 * method. Currently if this is present it will be a
3952 * snapshot id.
Alex Elder36be9a72013-01-19 00:30:28 -06003953 */
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003954 if (outbound) {
3955 if (outbound_size > PAGE_SIZE)
3956 return -E2BIG;
Alex Elder36be9a72013-01-19 00:30:28 -06003957
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003958 req_page = alloc_page(GFP_KERNEL);
3959 if (!req_page)
3960 return -ENOMEM;
Alex Elder36be9a72013-01-19 00:30:28 -06003961
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003962 memcpy(page_address(req_page), outbound, outbound_size);
Alex Elder04017e22013-04-05 14:46:02 -05003963 }
Alex Elder430c28c2013-04-03 21:32:51 -05003964
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003965 reply_page = alloc_page(GFP_KERNEL);
3966 if (!reply_page) {
3967 if (req_page)
3968 __free_page(req_page);
3969 return -ENOMEM;
3970 }
Alex Elder36be9a72013-01-19 00:30:28 -06003971
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003972 ret = ceph_osdc_call(osdc, oid, oloc, RBD_DRV_NAME, method_name,
3973 CEPH_OSD_FLAG_READ, req_page, outbound_size,
3974 reply_page, &inbound_size);
3975 if (!ret) {
3976 memcpy(inbound, page_address(reply_page), inbound_size);
3977 ret = inbound_size;
3978 }
Alex Elder57385b52013-04-21 12:14:45 -05003979
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003980 if (req_page)
3981 __free_page(req_page);
3982 __free_page(reply_page);
Alex Elder36be9a72013-01-19 00:30:28 -06003983 return ret;
3984}
3985
Ilya Dryomoved95b212016-08-12 16:40:02 +02003986/*
3987 * lock_rwsem must be held for read
3988 */
3989static void rbd_wait_state_locked(struct rbd_device *rbd_dev)
3990{
3991 DEFINE_WAIT(wait);
3992
3993 do {
3994 /*
3995 * Note the use of mod_delayed_work() in rbd_acquire_lock()
3996 * and cancel_delayed_work() in wake_requests().
3997 */
3998 dout("%s rbd_dev %p queueing lock_dwork\n", __func__, rbd_dev);
3999 queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0);
4000 prepare_to_wait_exclusive(&rbd_dev->lock_waitq, &wait,
4001 TASK_UNINTERRUPTIBLE);
4002 up_read(&rbd_dev->lock_rwsem);
4003 schedule();
4004 down_read(&rbd_dev->lock_rwsem);
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02004005 } while (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED &&
4006 !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags));
4007
Ilya Dryomoved95b212016-08-12 16:40:02 +02004008 finish_wait(&rbd_dev->lock_waitq, &wait);
4009}
4010
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004011static void rbd_queue_workfn(struct work_struct *work)
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004012{
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004013 struct request *rq = blk_mq_rq_from_pdu(work);
4014 struct rbd_device *rbd_dev = rq->q->queuedata;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004015 struct rbd_img_request *img_request;
Josh Durgin4e752f02014-04-08 11:12:11 -07004016 struct ceph_snap_context *snapc = NULL;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004017 u64 offset = (u64)blk_rq_pos(rq) << SECTOR_SHIFT;
4018 u64 length = blk_rq_bytes(rq);
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004019 enum obj_operation_type op_type;
Josh Durgin4e752f02014-04-08 11:12:11 -07004020 u64 mapping_size;
Ilya Dryomov80de1912016-09-20 14:23:17 +02004021 bool must_be_locked;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004022 int result;
4023
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004024 if (rq->cmd_type != REQ_TYPE_FS) {
4025 dout("%s: non-fs request type %d\n", __func__,
4026 (int) rq->cmd_type);
4027 result = -EIO;
4028 goto err;
4029 }
4030
Mike Christiec2df40d2016-06-05 14:32:17 -05004031 if (req_op(rq) == REQ_OP_DISCARD)
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004032 op_type = OBJ_OP_DISCARD;
Mike Christiec2df40d2016-06-05 14:32:17 -05004033 else if (req_op(rq) == REQ_OP_WRITE)
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004034 op_type = OBJ_OP_WRITE;
4035 else
4036 op_type = OBJ_OP_READ;
4037
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004038 /* Ignore/skip any zero-length requests */
4039
4040 if (!length) {
4041 dout("%s: zero-length request\n", __func__);
4042 result = 0;
4043 goto err_rq;
4044 }
4045
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004046 /* Only reads are allowed to a read-only device */
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004047
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004048 if (op_type != OBJ_OP_READ) {
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004049 if (rbd_dev->mapping.read_only) {
4050 result = -EROFS;
4051 goto err_rq;
4052 }
4053 rbd_assert(rbd_dev->spec->snap_id == CEPH_NOSNAP);
4054 }
4055
4056 /*
4057 * Quit early if the mapped snapshot no longer exists. It's
4058 * still possible the snapshot will have disappeared by the
4059 * time our request arrives at the osd, but there's no sense in
4060 * sending it if we already know.
4061 */
4062 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) {
4063 dout("request for non-existent snapshot");
4064 rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP);
4065 result = -ENXIO;
4066 goto err_rq;
4067 }
4068
4069 if (offset && length > U64_MAX - offset + 1) {
4070 rbd_warn(rbd_dev, "bad request range (%llu~%llu)", offset,
4071 length);
4072 result = -EINVAL;
4073 goto err_rq; /* Shouldn't happen */
4074 }
4075
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004076 blk_mq_start_request(rq);
4077
Josh Durgin4e752f02014-04-08 11:12:11 -07004078 down_read(&rbd_dev->header_rwsem);
4079 mapping_size = rbd_dev->mapping.size;
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004080 if (op_type != OBJ_OP_READ) {
Josh Durgin4e752f02014-04-08 11:12:11 -07004081 snapc = rbd_dev->header.snapc;
4082 ceph_get_snap_context(snapc);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004083 must_be_locked = rbd_is_lock_supported(rbd_dev);
Ilya Dryomov80de1912016-09-20 14:23:17 +02004084 } else {
4085 must_be_locked = rbd_dev->opts->lock_on_read &&
4086 rbd_is_lock_supported(rbd_dev);
Josh Durgin4e752f02014-04-08 11:12:11 -07004087 }
4088 up_read(&rbd_dev->header_rwsem);
4089
4090 if (offset + length > mapping_size) {
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004091 rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)", offset,
Josh Durgin4e752f02014-04-08 11:12:11 -07004092 length, mapping_size);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004093 result = -EIO;
4094 goto err_rq;
4095 }
4096
Ilya Dryomoved95b212016-08-12 16:40:02 +02004097 if (must_be_locked) {
4098 down_read(&rbd_dev->lock_rwsem);
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02004099 if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED &&
4100 !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags))
Ilya Dryomoved95b212016-08-12 16:40:02 +02004101 rbd_wait_state_locked(rbd_dev);
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02004102
4103 WARN_ON((rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) ^
4104 !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags));
4105 if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) {
4106 result = -EBLACKLISTED;
4107 goto err_unlock;
4108 }
Ilya Dryomoved95b212016-08-12 16:40:02 +02004109 }
4110
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004111 img_request = rbd_img_request_create(rbd_dev, offset, length, op_type,
Josh Durgin4e752f02014-04-08 11:12:11 -07004112 snapc);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004113 if (!img_request) {
4114 result = -ENOMEM;
Ilya Dryomoved95b212016-08-12 16:40:02 +02004115 goto err_unlock;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004116 }
4117 img_request->rq = rq;
Ilya Dryomov70b16db2015-11-27 19:23:24 +01004118 snapc = NULL; /* img_request consumes a ref */
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004119
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004120 if (op_type == OBJ_OP_DISCARD)
4121 result = rbd_img_request_fill(img_request, OBJ_REQUEST_NODATA,
4122 NULL);
4123 else
4124 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
4125 rq->bio);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004126 if (result)
4127 goto err_img_request;
4128
4129 result = rbd_img_request_submit(img_request);
4130 if (result)
4131 goto err_img_request;
4132
Ilya Dryomoved95b212016-08-12 16:40:02 +02004133 if (must_be_locked)
4134 up_read(&rbd_dev->lock_rwsem);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004135 return;
4136
4137err_img_request:
4138 rbd_img_request_put(img_request);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004139err_unlock:
4140 if (must_be_locked)
4141 up_read(&rbd_dev->lock_rwsem);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004142err_rq:
4143 if (result)
4144 rbd_warn(rbd_dev, "%s %llx at %llx result %d",
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004145 obj_op_name(op_type), length, offset, result);
SF Markus Elfringe96a6502014-11-02 15:20:59 +01004146 ceph_put_snap_context(snapc);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004147err:
4148 blk_mq_end_request(rq, result);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004149}
4150
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004151static int rbd_queue_rq(struct blk_mq_hw_ctx *hctx,
4152 const struct blk_mq_queue_data *bd)
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004153{
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004154 struct request *rq = bd->rq;
4155 struct work_struct *work = blk_mq_rq_to_pdu(rq);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004156
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004157 queue_work(rbd_wq, work);
4158 return BLK_MQ_RQ_QUEUE_OK;
Alex Elderbf0d5f502012-11-22 00:00:08 -06004159}
4160
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004161static void rbd_free_disk(struct rbd_device *rbd_dev)
4162{
4163 struct gendisk *disk = rbd_dev->disk;
4164
4165 if (!disk)
4166 return;
4167
Alex Eldera0cab922013-04-25 23:15:08 -05004168 rbd_dev->disk = NULL;
4169 if (disk->flags & GENHD_FL_UP) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004170 del_gendisk(disk);
Alex Eldera0cab922013-04-25 23:15:08 -05004171 if (disk->queue)
4172 blk_cleanup_queue(disk->queue);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004173 blk_mq_free_tag_set(&rbd_dev->tag_set);
Alex Eldera0cab922013-04-25 23:15:08 -05004174 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004175 put_disk(disk);
4176}
4177
Alex Elder788e2df2013-01-17 12:25:27 -06004178static int rbd_obj_read_sync(struct rbd_device *rbd_dev,
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004179 struct ceph_object_id *oid,
4180 struct ceph_object_locator *oloc,
4181 void *buf, int buf_len)
Alex Elder788e2df2013-01-17 12:25:27 -06004182
4183{
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004184 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
4185 struct ceph_osd_request *req;
4186 struct page **pages;
4187 int num_pages = calc_pages_for(0, buf_len);
Alex Elder788e2df2013-01-17 12:25:27 -06004188 int ret;
4189
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004190 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL);
4191 if (!req)
4192 return -ENOMEM;
Alex Elder788e2df2013-01-17 12:25:27 -06004193
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004194 ceph_oid_copy(&req->r_base_oid, oid);
4195 ceph_oloc_copy(&req->r_base_oloc, oloc);
4196 req->r_flags = CEPH_OSD_FLAG_READ;
Alex Elder788e2df2013-01-17 12:25:27 -06004197
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004198 ret = ceph_osdc_alloc_messages(req, GFP_KERNEL);
Alex Elder788e2df2013-01-17 12:25:27 -06004199 if (ret)
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004200 goto out_req;
Alex Elder788e2df2013-01-17 12:25:27 -06004201
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004202 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
4203 if (IS_ERR(pages)) {
4204 ret = PTR_ERR(pages);
4205 goto out_req;
4206 }
Alex Elder1ceae7e2013-02-06 13:11:38 -06004207
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004208 osd_req_op_extent_init(req, 0, CEPH_OSD_OP_READ, 0, buf_len, 0, 0);
4209 osd_req_op_extent_osd_data_pages(req, 0, pages, buf_len, 0, false,
4210 true);
Alex Elder788e2df2013-01-17 12:25:27 -06004211
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004212 ceph_osdc_start_request(osdc, req, false);
4213 ret = ceph_osdc_wait_request(osdc, req);
4214 if (ret >= 0)
4215 ceph_copy_from_page_vector(pages, buf, 0, ret);
4216
4217out_req:
4218 ceph_osdc_put_request(req);
Alex Elder788e2df2013-01-17 12:25:27 -06004219 return ret;
4220}
4221
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004222/*
Alex Elder662518b2013-05-06 09:51:29 -05004223 * Read the complete header for the given rbd device. On successful
4224 * return, the rbd_dev->header field will contain up-to-date
4225 * information about the image.
Alex Elder4156d992012-08-02 11:29:46 -05004226 */
Alex Elder99a41eb2013-05-06 09:51:30 -05004227static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev)
Alex Elder4156d992012-08-02 11:29:46 -05004228{
4229 struct rbd_image_header_ondisk *ondisk = NULL;
4230 u32 snap_count = 0;
4231 u64 names_size = 0;
4232 u32 want_count;
4233 int ret;
4234
4235 /*
4236 * The complete header will include an array of its 64-bit
4237 * snapshot ids, followed by the names of those snapshots as
4238 * a contiguous block of NUL-terminated strings. Note that
4239 * the number of snapshots could change by the time we read
4240 * it in, in which case we re-read it.
4241 */
4242 do {
4243 size_t size;
4244
4245 kfree(ondisk);
4246
4247 size = sizeof (*ondisk);
4248 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
4249 size += names_size;
4250 ondisk = kmalloc(size, GFP_KERNEL);
4251 if (!ondisk)
Alex Elder662518b2013-05-06 09:51:29 -05004252 return -ENOMEM;
Alex Elder4156d992012-08-02 11:29:46 -05004253
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004254 ret = rbd_obj_read_sync(rbd_dev, &rbd_dev->header_oid,
4255 &rbd_dev->header_oloc, ondisk, size);
Alex Elder4156d992012-08-02 11:29:46 -05004256 if (ret < 0)
Alex Elder662518b2013-05-06 09:51:29 -05004257 goto out;
Alex Elderc0cd10db2013-04-26 09:43:47 -05004258 if ((size_t)ret < size) {
Alex Elder4156d992012-08-02 11:29:46 -05004259 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05004260 rbd_warn(rbd_dev, "short header read (want %zd got %d)",
4261 size, ret);
Alex Elder662518b2013-05-06 09:51:29 -05004262 goto out;
Alex Elder4156d992012-08-02 11:29:46 -05004263 }
4264 if (!rbd_dev_ondisk_valid(ondisk)) {
4265 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05004266 rbd_warn(rbd_dev, "invalid header");
Alex Elder662518b2013-05-06 09:51:29 -05004267 goto out;
Alex Elder4156d992012-08-02 11:29:46 -05004268 }
4269
4270 names_size = le64_to_cpu(ondisk->snap_names_len);
4271 want_count = snap_count;
4272 snap_count = le32_to_cpu(ondisk->snap_count);
4273 } while (snap_count != want_count);
4274
Alex Elder662518b2013-05-06 09:51:29 -05004275 ret = rbd_header_from_disk(rbd_dev, ondisk);
4276out:
Alex Elder4156d992012-08-02 11:29:46 -05004277 kfree(ondisk);
4278
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004279 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004280}
4281
Alex Elder15228ed2013-05-01 12:43:03 -05004282/*
4283 * Clear the rbd device's EXISTS flag if the snapshot it's mapped to
4284 * has disappeared from the (just updated) snapshot context.
4285 */
4286static void rbd_exists_validate(struct rbd_device *rbd_dev)
4287{
4288 u64 snap_id;
4289
4290 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags))
4291 return;
4292
4293 snap_id = rbd_dev->spec->snap_id;
4294 if (snap_id == CEPH_NOSNAP)
4295 return;
4296
4297 if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX)
4298 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
4299}
4300
Josh Durgin98752012013-08-29 17:26:31 -07004301static void rbd_dev_update_size(struct rbd_device *rbd_dev)
4302{
4303 sector_t size;
Josh Durgin98752012013-08-29 17:26:31 -07004304
4305 /*
Ilya Dryomov811c6682016-04-15 16:22:16 +02004306 * If EXISTS is not set, rbd_dev->disk may be NULL, so don't
4307 * try to update its size. If REMOVING is set, updating size
4308 * is just useless work since the device can't be opened.
Josh Durgin98752012013-08-29 17:26:31 -07004309 */
Ilya Dryomov811c6682016-04-15 16:22:16 +02004310 if (test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags) &&
4311 !test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) {
Josh Durgin98752012013-08-29 17:26:31 -07004312 size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE;
4313 dout("setting size to %llu sectors", (unsigned long long)size);
4314 set_capacity(rbd_dev->disk, size);
4315 revalidate_disk(rbd_dev->disk);
4316 }
4317}
4318
Alex Eldercc4a38bd2013-04-30 00:44:33 -05004319static int rbd_dev_refresh(struct rbd_device *rbd_dev)
Alex Elder1fe5e992012-07-25 09:32:41 -05004320{
Alex Eldere627db02013-05-06 07:40:30 -05004321 u64 mapping_size;
Alex Elder1fe5e992012-07-25 09:32:41 -05004322 int ret;
4323
Alex Eldercfbf6372013-05-31 17:40:45 -05004324 down_write(&rbd_dev->header_rwsem);
Alex Elder3b5cf2a2013-05-29 11:18:59 -05004325 mapping_size = rbd_dev->mapping.size;
Ilya Dryomova720ae02014-07-23 17:11:19 +04004326
4327 ret = rbd_dev_header_info(rbd_dev);
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04004328 if (ret)
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004329 goto out;
Alex Elder15228ed2013-05-01 12:43:03 -05004330
Ilya Dryomove8f59b52014-07-24 10:42:13 +04004331 /*
4332 * If there is a parent, see if it has disappeared due to the
4333 * mapped image getting flattened.
4334 */
4335 if (rbd_dev->parent) {
4336 ret = rbd_dev_v2_parent_info(rbd_dev);
4337 if (ret)
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004338 goto out;
Ilya Dryomove8f59b52014-07-24 10:42:13 +04004339 }
4340
Ilya Dryomov5ff11082014-07-23 17:11:21 +04004341 if (rbd_dev->spec->snap_id == CEPH_NOSNAP) {
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004342 rbd_dev->mapping.size = rbd_dev->header.image_size;
Ilya Dryomov5ff11082014-07-23 17:11:21 +04004343 } else {
4344 /* validate mapped snapshot's EXISTS flag */
4345 rbd_exists_validate(rbd_dev);
4346 }
Alex Elder15228ed2013-05-01 12:43:03 -05004347
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004348out:
Alex Eldercfbf6372013-05-31 17:40:45 -05004349 up_write(&rbd_dev->header_rwsem);
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004350 if (!ret && mapping_size != rbd_dev->mapping.size)
Josh Durgin98752012013-08-29 17:26:31 -07004351 rbd_dev_update_size(rbd_dev);
Alex Elder1fe5e992012-07-25 09:32:41 -05004352
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004353 return ret;
Alex Elder1fe5e992012-07-25 09:32:41 -05004354}
4355
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004356static int rbd_init_request(void *data, struct request *rq,
4357 unsigned int hctx_idx, unsigned int request_idx,
4358 unsigned int numa_node)
4359{
4360 struct work_struct *work = blk_mq_rq_to_pdu(rq);
4361
4362 INIT_WORK(work, rbd_queue_workfn);
4363 return 0;
4364}
4365
4366static struct blk_mq_ops rbd_mq_ops = {
4367 .queue_rq = rbd_queue_rq,
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004368 .init_request = rbd_init_request,
4369};
4370
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004371static int rbd_init_disk(struct rbd_device *rbd_dev)
4372{
4373 struct gendisk *disk;
4374 struct request_queue *q;
Alex Elder593a9e72012-02-07 12:03:37 -06004375 u64 segment_size;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004376 int err;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004377
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004378 /* create gendisk info */
Ilya Dryomov7e513d42013-12-16 19:26:32 +02004379 disk = alloc_disk(single_major ?
4380 (1 << RBD_SINGLE_MAJOR_PART_SHIFT) :
4381 RBD_MINORS_PER_MAJOR);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004382 if (!disk)
Alex Elder1fcdb8a2012-08-29 17:11:06 -05004383 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004384
Alex Elderf0f8cef2012-01-29 13:57:44 -06004385 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
Alex Elderde71a292012-07-03 16:01:19 -05004386 rbd_dev->dev_id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004387 disk->major = rbd_dev->major;
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004388 disk->first_minor = rbd_dev->minor;
Ilya Dryomov7e513d42013-12-16 19:26:32 +02004389 if (single_major)
4390 disk->flags |= GENHD_FL_EXT_DEVT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004391 disk->fops = &rbd_bd_ops;
4392 disk->private_data = rbd_dev;
4393
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004394 memset(&rbd_dev->tag_set, 0, sizeof(rbd_dev->tag_set));
4395 rbd_dev->tag_set.ops = &rbd_mq_ops;
Ilya Dryomovb5584182015-06-23 16:21:19 +03004396 rbd_dev->tag_set.queue_depth = rbd_dev->opts->queue_depth;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004397 rbd_dev->tag_set.numa_node = NUMA_NO_NODE;
Ilya Dryomovb5584182015-06-23 16:21:19 +03004398 rbd_dev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004399 rbd_dev->tag_set.nr_hw_queues = 1;
4400 rbd_dev->tag_set.cmd_size = sizeof(struct work_struct);
4401
4402 err = blk_mq_alloc_tag_set(&rbd_dev->tag_set);
4403 if (err)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004404 goto out_disk;
Josh Durgin029bcbd2011-07-22 11:35:23 -07004405
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004406 q = blk_mq_init_queue(&rbd_dev->tag_set);
4407 if (IS_ERR(q)) {
4408 err = PTR_ERR(q);
4409 goto out_tag_set;
4410 }
4411
Ilya Dryomovd8a2c892015-03-24 16:15:17 +03004412 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
4413 /* QUEUE_FLAG_ADD_RANDOM is off by default for blk-mq */
Alex Elder593a9e72012-02-07 12:03:37 -06004414
Josh Durgin029bcbd2011-07-22 11:35:23 -07004415 /* set io sizes to object size */
Alex Elder593a9e72012-02-07 12:03:37 -06004416 segment_size = rbd_obj_bytes(&rbd_dev->header);
4417 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
Ilya Dryomov0d9fde42015-10-07 16:09:35 +02004418 q->limits.max_sectors = queue_max_hw_sectors(q);
Ilya Dryomovd3834fe2015-06-12 19:19:02 +03004419 blk_queue_max_segments(q, segment_size / SECTOR_SIZE);
Alex Elder593a9e72012-02-07 12:03:37 -06004420 blk_queue_max_segment_size(q, segment_size);
4421 blk_queue_io_min(q, segment_size);
4422 blk_queue_io_opt(q, segment_size);
Josh Durgin029bcbd2011-07-22 11:35:23 -07004423
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004424 /* enable the discard support */
4425 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
4426 q->limits.discard_granularity = segment_size;
4427 q->limits.discard_alignment = segment_size;
Jens Axboe2bb4cd52015-07-14 08:15:12 -06004428 blk_queue_max_discard_sectors(q, segment_size / SECTOR_SIZE);
Josh Durginb76f8232014-04-07 16:52:03 -07004429 q->limits.discard_zeroes_data = 1;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004430
Ronny Hegewaldbae818e2015-10-15 18:50:46 +00004431 if (!ceph_test_opt(rbd_dev->rbd_client->client, NOCRC))
4432 q->backing_dev_info.capabilities |= BDI_CAP_STABLE_WRITES;
4433
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004434 disk->queue = q;
4435
4436 q->queuedata = rbd_dev;
4437
4438 rbd_dev->disk = disk;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004439
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004440 return 0;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004441out_tag_set:
4442 blk_mq_free_tag_set(&rbd_dev->tag_set);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004443out_disk:
4444 put_disk(disk);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004445 return err;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004446}
4447
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004448/*
4449 sysfs
4450*/
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004451
Alex Elder593a9e72012-02-07 12:03:37 -06004452static struct rbd_device *dev_to_rbd_dev(struct device *dev)
4453{
4454 return container_of(dev, struct rbd_device, dev);
4455}
4456
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004457static ssize_t rbd_size_show(struct device *dev,
4458 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004459{
Alex Elder593a9e72012-02-07 12:03:37 -06004460 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004461
Alex Elderfc71d832013-04-26 15:44:36 -05004462 return sprintf(buf, "%llu\n",
4463 (unsigned long long)rbd_dev->mapping.size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004464}
4465
Alex Elder34b13182012-07-13 20:35:12 -05004466/*
4467 * Note this shows the features for whatever's mapped, which is not
4468 * necessarily the base image.
4469 */
4470static ssize_t rbd_features_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, "0x%016llx\n",
Alex Elderfc71d832013-04-26 15:44:36 -05004476 (unsigned long long)rbd_dev->mapping.features);
Alex Elder34b13182012-07-13 20:35:12 -05004477}
4478
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004479static ssize_t rbd_major_show(struct device *dev,
4480 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004481{
Alex Elder593a9e72012-02-07 12:03:37 -06004482 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004483
Alex Elderfc71d832013-04-26 15:44:36 -05004484 if (rbd_dev->major)
4485 return sprintf(buf, "%d\n", rbd_dev->major);
4486
4487 return sprintf(buf, "(none)\n");
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004488}
Alex Elderfc71d832013-04-26 15:44:36 -05004489
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004490static ssize_t rbd_minor_show(struct device *dev,
4491 struct device_attribute *attr, char *buf)
4492{
4493 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4494
4495 return sprintf(buf, "%d\n", rbd_dev->minor);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004496}
4497
Ilya Dryomov005a07bf2016-08-18 18:38:43 +02004498static ssize_t rbd_client_addr_show(struct device *dev,
4499 struct device_attribute *attr, char *buf)
4500{
4501 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4502 struct ceph_entity_addr *client_addr =
4503 ceph_client_addr(rbd_dev->rbd_client->client);
4504
4505 return sprintf(buf, "%pISpc/%u\n", &client_addr->in_addr,
4506 le32_to_cpu(client_addr->nonce));
4507}
4508
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004509static ssize_t rbd_client_id_show(struct device *dev,
4510 struct device_attribute *attr, char *buf)
4511{
Alex Elder593a9e72012-02-07 12:03:37 -06004512 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004513
Alex Elder1dbb4392012-01-24 10:08:37 -06004514 return sprintf(buf, "client%lld\n",
Ilya Dryomov033268a2016-08-12 14:59:58 +02004515 ceph_client_gid(rbd_dev->rbd_client->client));
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004516}
4517
Mike Christie267fb902016-08-18 18:38:43 +02004518static ssize_t rbd_cluster_fsid_show(struct device *dev,
4519 struct device_attribute *attr, char *buf)
4520{
4521 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4522
4523 return sprintf(buf, "%pU\n", &rbd_dev->rbd_client->client->fsid);
4524}
4525
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02004526static ssize_t rbd_config_info_show(struct device *dev,
4527 struct device_attribute *attr, char *buf)
4528{
4529 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4530
4531 return sprintf(buf, "%s\n", rbd_dev->config_info);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004532}
4533
4534static ssize_t rbd_pool_show(struct device *dev,
4535 struct device_attribute *attr, char *buf)
4536{
Alex Elder593a9e72012-02-07 12:03:37 -06004537 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004538
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004539 return sprintf(buf, "%s\n", rbd_dev->spec->pool_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004540}
4541
Alex Elder9bb2f332012-07-12 10:46:35 -05004542static ssize_t rbd_pool_id_show(struct device *dev,
4543 struct device_attribute *attr, char *buf)
4544{
4545 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4546
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004547 return sprintf(buf, "%llu\n",
Alex Elderfc71d832013-04-26 15:44:36 -05004548 (unsigned long long) rbd_dev->spec->pool_id);
Alex Elder9bb2f332012-07-12 10:46:35 -05004549}
4550
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004551static ssize_t rbd_name_show(struct device *dev,
4552 struct device_attribute *attr, char *buf)
4553{
Alex Elder593a9e72012-02-07 12:03:37 -06004554 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004555
Alex Eldera92ffdf2012-10-30 19:40:33 -05004556 if (rbd_dev->spec->image_name)
4557 return sprintf(buf, "%s\n", rbd_dev->spec->image_name);
4558
4559 return sprintf(buf, "(unknown)\n");
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004560}
4561
Alex Elder589d30e2012-07-10 20:30:11 -05004562static ssize_t rbd_image_id_show(struct device *dev,
4563 struct device_attribute *attr, char *buf)
4564{
4565 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4566
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004567 return sprintf(buf, "%s\n", rbd_dev->spec->image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05004568}
4569
Alex Elder34b13182012-07-13 20:35:12 -05004570/*
4571 * Shows the name of the currently-mapped snapshot (or
4572 * RBD_SNAP_HEAD_NAME for the base image).
4573 */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004574static ssize_t rbd_snap_show(struct device *dev,
4575 struct device_attribute *attr,
4576 char *buf)
4577{
Alex Elder593a9e72012-02-07 12:03:37 -06004578 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004579
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004580 return sprintf(buf, "%s\n", rbd_dev->spec->snap_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004581}
4582
Mike Christie92a58672016-08-18 18:38:44 +02004583static ssize_t rbd_snap_id_show(struct device *dev,
4584 struct device_attribute *attr, char *buf)
4585{
4586 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4587
4588 return sprintf(buf, "%llu\n", rbd_dev->spec->snap_id);
4589}
4590
Alex Elder86b00e02012-10-25 23:34:42 -05004591/*
Ilya Dryomovff961282014-07-22 21:53:07 +04004592 * For a v2 image, shows the chain of parent images, separated by empty
4593 * lines. For v1 images or if there is no parent, shows "(no parent
4594 * image)".
Alex Elder86b00e02012-10-25 23:34:42 -05004595 */
4596static ssize_t rbd_parent_show(struct device *dev,
Ilya Dryomovff961282014-07-22 21:53:07 +04004597 struct device_attribute *attr,
4598 char *buf)
Alex Elder86b00e02012-10-25 23:34:42 -05004599{
4600 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Ilya Dryomovff961282014-07-22 21:53:07 +04004601 ssize_t count = 0;
Alex Elder86b00e02012-10-25 23:34:42 -05004602
Ilya Dryomovff961282014-07-22 21:53:07 +04004603 if (!rbd_dev->parent)
Alex Elder86b00e02012-10-25 23:34:42 -05004604 return sprintf(buf, "(no parent image)\n");
4605
Ilya Dryomovff961282014-07-22 21:53:07 +04004606 for ( ; rbd_dev->parent; rbd_dev = rbd_dev->parent) {
4607 struct rbd_spec *spec = rbd_dev->parent_spec;
Alex Elder86b00e02012-10-25 23:34:42 -05004608
Ilya Dryomovff961282014-07-22 21:53:07 +04004609 count += sprintf(&buf[count], "%s"
4610 "pool_id %llu\npool_name %s\n"
4611 "image_id %s\nimage_name %s\n"
4612 "snap_id %llu\nsnap_name %s\n"
4613 "overlap %llu\n",
4614 !count ? "" : "\n", /* first? */
4615 spec->pool_id, spec->pool_name,
4616 spec->image_id, spec->image_name ?: "(unknown)",
4617 spec->snap_id, spec->snap_name,
4618 rbd_dev->parent_overlap);
4619 }
Alex Elder86b00e02012-10-25 23:34:42 -05004620
Ilya Dryomovff961282014-07-22 21:53:07 +04004621 return count;
Alex Elder86b00e02012-10-25 23:34:42 -05004622}
4623
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004624static ssize_t rbd_image_refresh(struct device *dev,
4625 struct device_attribute *attr,
4626 const char *buf,
4627 size_t size)
4628{
Alex Elder593a9e72012-02-07 12:03:37 -06004629 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Alex Elderb8136232012-07-25 09:32:41 -05004630 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004631
Alex Eldercc4a38bd2013-04-30 00:44:33 -05004632 ret = rbd_dev_refresh(rbd_dev);
Alex Eldere627db02013-05-06 07:40:30 -05004633 if (ret)
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04004634 return ret;
Alex Elderb8136232012-07-25 09:32:41 -05004635
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04004636 return size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004637}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004638
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004639static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
Alex Elder34b13182012-07-13 20:35:12 -05004640static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004641static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004642static DEVICE_ATTR(minor, S_IRUGO, rbd_minor_show, NULL);
Ilya Dryomov005a07bf2016-08-18 18:38:43 +02004643static DEVICE_ATTR(client_addr, S_IRUGO, rbd_client_addr_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004644static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
Mike Christie267fb902016-08-18 18:38:43 +02004645static DEVICE_ATTR(cluster_fsid, S_IRUGO, rbd_cluster_fsid_show, NULL);
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02004646static DEVICE_ATTR(config_info, S_IRUSR, rbd_config_info_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004647static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
Alex Elder9bb2f332012-07-12 10:46:35 -05004648static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004649static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
Alex Elder589d30e2012-07-10 20:30:11 -05004650static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004651static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
4652static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
Mike Christie92a58672016-08-18 18:38:44 +02004653static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL);
Alex Elder86b00e02012-10-25 23:34:42 -05004654static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004655
4656static struct attribute *rbd_attrs[] = {
4657 &dev_attr_size.attr,
Alex Elder34b13182012-07-13 20:35:12 -05004658 &dev_attr_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004659 &dev_attr_major.attr,
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004660 &dev_attr_minor.attr,
Ilya Dryomov005a07bf2016-08-18 18:38:43 +02004661 &dev_attr_client_addr.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004662 &dev_attr_client_id.attr,
Mike Christie267fb902016-08-18 18:38:43 +02004663 &dev_attr_cluster_fsid.attr,
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02004664 &dev_attr_config_info.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004665 &dev_attr_pool.attr,
Alex Elder9bb2f332012-07-12 10:46:35 -05004666 &dev_attr_pool_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004667 &dev_attr_name.attr,
Alex Elder589d30e2012-07-10 20:30:11 -05004668 &dev_attr_image_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004669 &dev_attr_current_snap.attr,
Mike Christie92a58672016-08-18 18:38:44 +02004670 &dev_attr_snap_id.attr,
Alex Elder86b00e02012-10-25 23:34:42 -05004671 &dev_attr_parent.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004672 &dev_attr_refresh.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004673 NULL
4674};
4675
4676static struct attribute_group rbd_attr_group = {
4677 .attrs = rbd_attrs,
4678};
4679
4680static const struct attribute_group *rbd_attr_groups[] = {
4681 &rbd_attr_group,
4682 NULL
4683};
4684
Ilya Dryomov6cac4692015-10-16 20:11:25 +02004685static void rbd_dev_release(struct device *dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004686
4687static struct device_type rbd_device_type = {
4688 .name = "rbd",
4689 .groups = rbd_attr_groups,
Ilya Dryomov6cac4692015-10-16 20:11:25 +02004690 .release = rbd_dev_release,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004691};
4692
Alex Elder8b8fb992012-10-26 17:25:24 -05004693static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)
4694{
4695 kref_get(&spec->kref);
4696
4697 return spec;
4698}
4699
4700static void rbd_spec_free(struct kref *kref);
4701static void rbd_spec_put(struct rbd_spec *spec)
4702{
4703 if (spec)
4704 kref_put(&spec->kref, rbd_spec_free);
4705}
4706
4707static struct rbd_spec *rbd_spec_alloc(void)
4708{
4709 struct rbd_spec *spec;
4710
4711 spec = kzalloc(sizeof (*spec), GFP_KERNEL);
4712 if (!spec)
4713 return NULL;
Ilya Dryomov04077592014-07-23 17:11:20 +04004714
4715 spec->pool_id = CEPH_NOPOOL;
4716 spec->snap_id = CEPH_NOSNAP;
Alex Elder8b8fb992012-10-26 17:25:24 -05004717 kref_init(&spec->kref);
4718
Alex Elder8b8fb992012-10-26 17:25:24 -05004719 return spec;
4720}
4721
4722static void rbd_spec_free(struct kref *kref)
4723{
4724 struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref);
4725
4726 kfree(spec->pool_name);
4727 kfree(spec->image_id);
4728 kfree(spec->image_name);
4729 kfree(spec->snap_name);
4730 kfree(spec);
4731}
4732
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004733static void rbd_dev_free(struct rbd_device *rbd_dev)
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004734{
Ilya Dryomov99d16942016-08-12 16:11:41 +02004735 WARN_ON(rbd_dev->watch_state != RBD_WATCH_STATE_UNREGISTERED);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004736 WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_UNLOCKED);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004737
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004738 ceph_oid_destroy(&rbd_dev->header_oid);
Ilya Dryomov6b6dddb2016-08-05 16:15:38 +02004739 ceph_oloc_destroy(&rbd_dev->header_oloc);
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02004740 kfree(rbd_dev->config_info);
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004741
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004742 rbd_put_client(rbd_dev->rbd_client);
4743 rbd_spec_put(rbd_dev->spec);
4744 kfree(rbd_dev->opts);
4745 kfree(rbd_dev);
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004746}
4747
4748static void rbd_dev_release(struct device *dev)
4749{
4750 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4751 bool need_put = !!rbd_dev->opts;
4752
4753 if (need_put) {
4754 destroy_workqueue(rbd_dev->task_wq);
4755 ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id);
4756 }
4757
4758 rbd_dev_free(rbd_dev);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004759
4760 /*
4761 * This is racy, but way better than putting module outside of
4762 * the release callback. The race window is pretty small, so
4763 * doing something similar to dm (dm-builtin.c) is overkill.
4764 */
4765 if (need_put)
4766 module_put(THIS_MODULE);
4767}
4768
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004769static struct rbd_device *__rbd_dev_create(struct rbd_client *rbdc,
4770 struct rbd_spec *spec)
Alex Elderc53d5892012-10-25 23:34:42 -05004771{
4772 struct rbd_device *rbd_dev;
4773
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004774 rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL);
Alex Elderc53d5892012-10-25 23:34:42 -05004775 if (!rbd_dev)
4776 return NULL;
4777
4778 spin_lock_init(&rbd_dev->lock);
4779 INIT_LIST_HEAD(&rbd_dev->node);
Alex Elderc53d5892012-10-25 23:34:42 -05004780 init_rwsem(&rbd_dev->header_rwsem);
4781
Ilya Dryomov7e973322017-01-25 18:16:22 +01004782 rbd_dev->header.data_pool_id = CEPH_NOPOOL;
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004783 ceph_oid_init(&rbd_dev->header_oid);
Ilya Dryomov431a02c2017-01-25 18:16:21 +01004784 rbd_dev->header_oloc.pool = spec->pool_id;
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004785
Ilya Dryomov99d16942016-08-12 16:11:41 +02004786 mutex_init(&rbd_dev->watch_mutex);
4787 rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED;
4788 INIT_DELAYED_WORK(&rbd_dev->watch_dwork, rbd_reregister_watch);
4789
Ilya Dryomoved95b212016-08-12 16:40:02 +02004790 init_rwsem(&rbd_dev->lock_rwsem);
4791 rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED;
4792 INIT_WORK(&rbd_dev->acquired_lock_work, rbd_notify_acquired_lock);
4793 INIT_WORK(&rbd_dev->released_lock_work, rbd_notify_released_lock);
4794 INIT_DELAYED_WORK(&rbd_dev->lock_dwork, rbd_acquire_lock);
4795 INIT_WORK(&rbd_dev->unlock_work, rbd_release_lock_work);
4796 init_waitqueue_head(&rbd_dev->lock_waitq);
4797
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004798 rbd_dev->dev.bus = &rbd_bus_type;
4799 rbd_dev->dev.type = &rbd_device_type;
4800 rbd_dev->dev.parent = &rbd_root_dev;
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004801 device_initialize(&rbd_dev->dev);
4802
Alex Elderc53d5892012-10-25 23:34:42 -05004803 rbd_dev->rbd_client = rbdc;
Ilya Dryomovd1475432015-06-22 13:24:48 +03004804 rbd_dev->spec = spec;
Alex Elder0903e872012-11-14 12:25:19 -06004805
Alex Elderc53d5892012-10-25 23:34:42 -05004806 return rbd_dev;
4807}
4808
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004809/*
4810 * Create a mapping rbd_dev.
4811 */
4812static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,
4813 struct rbd_spec *spec,
4814 struct rbd_options *opts)
4815{
4816 struct rbd_device *rbd_dev;
4817
4818 rbd_dev = __rbd_dev_create(rbdc, spec);
4819 if (!rbd_dev)
4820 return NULL;
4821
4822 rbd_dev->opts = opts;
4823
4824 /* get an id and fill in device name */
4825 rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0,
4826 minor_to_rbd_dev_id(1 << MINORBITS),
4827 GFP_KERNEL);
4828 if (rbd_dev->dev_id < 0)
4829 goto fail_rbd_dev;
4830
4831 sprintf(rbd_dev->name, RBD_DRV_NAME "%d", rbd_dev->dev_id);
4832 rbd_dev->task_wq = alloc_ordered_workqueue("%s-tasks", WQ_MEM_RECLAIM,
4833 rbd_dev->name);
4834 if (!rbd_dev->task_wq)
4835 goto fail_dev_id;
4836
4837 /* we have a ref from do_rbd_add() */
4838 __module_get(THIS_MODULE);
4839
4840 dout("%s rbd_dev %p dev_id %d\n", __func__, rbd_dev, rbd_dev->dev_id);
4841 return rbd_dev;
4842
4843fail_dev_id:
4844 ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id);
4845fail_rbd_dev:
4846 rbd_dev_free(rbd_dev);
4847 return NULL;
4848}
4849
Alex Elderc53d5892012-10-25 23:34:42 -05004850static void rbd_dev_destroy(struct rbd_device *rbd_dev)
4851{
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004852 if (rbd_dev)
4853 put_device(&rbd_dev->dev);
Alex Elderc53d5892012-10-25 23:34:42 -05004854}
4855
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004856/*
Alex Elder9d475de2012-07-03 16:01:19 -05004857 * Get the size and object order for an image snapshot, or if
4858 * snap_id is CEPH_NOSNAP, gets this information for the base
4859 * image.
4860 */
4861static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
4862 u8 *order, u64 *snap_size)
4863{
4864 __le64 snapid = cpu_to_le64(snap_id);
4865 int ret;
4866 struct {
4867 u8 order;
4868 __le64 size;
4869 } __attribute__ ((packed)) size_buf = { 0 };
4870
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004871 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
4872 &rbd_dev->header_oloc, "get_size",
4873 &snapid, sizeof(snapid),
4874 &size_buf, sizeof(size_buf));
Alex Elder36be9a72013-01-19 00:30:28 -06004875 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder9d475de2012-07-03 16:01:19 -05004876 if (ret < 0)
4877 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05004878 if (ret < sizeof (size_buf))
4879 return -ERANGE;
Alex Elder9d475de2012-07-03 16:01:19 -05004880
Josh Durginc3545572013-08-28 17:08:10 -07004881 if (order) {
Alex Elderc86f86e2013-04-25 15:09:41 -05004882 *order = size_buf.order;
Josh Durginc3545572013-08-28 17:08:10 -07004883 dout(" order %u", (unsigned int)*order);
4884 }
Alex Elder9d475de2012-07-03 16:01:19 -05004885 *snap_size = le64_to_cpu(size_buf.size);
4886
Josh Durginc3545572013-08-28 17:08:10 -07004887 dout(" snap_id 0x%016llx snap_size = %llu\n",
4888 (unsigned long long)snap_id,
Alex Elder57385b52013-04-21 12:14:45 -05004889 (unsigned long long)*snap_size);
Alex Elder9d475de2012-07-03 16:01:19 -05004890
4891 return 0;
4892}
4893
4894static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
4895{
4896 return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
4897 &rbd_dev->header.obj_order,
4898 &rbd_dev->header.image_size);
4899}
4900
Alex Elder1e130192012-07-03 16:01:19 -05004901static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
4902{
4903 void *reply_buf;
4904 int ret;
4905 void *p;
4906
4907 reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL);
4908 if (!reply_buf)
4909 return -ENOMEM;
4910
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004911 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
4912 &rbd_dev->header_oloc, "get_object_prefix",
4913 NULL, 0, reply_buf, RBD_OBJ_PREFIX_LEN_MAX);
Alex Elder36be9a72013-01-19 00:30:28 -06004914 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder1e130192012-07-03 16:01:19 -05004915 if (ret < 0)
4916 goto out;
4917
4918 p = reply_buf;
4919 rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p,
Alex Elder57385b52013-04-21 12:14:45 -05004920 p + ret, NULL, GFP_NOIO);
4921 ret = 0;
Alex Elder1e130192012-07-03 16:01:19 -05004922
4923 if (IS_ERR(rbd_dev->header.object_prefix)) {
4924 ret = PTR_ERR(rbd_dev->header.object_prefix);
4925 rbd_dev->header.object_prefix = NULL;
4926 } else {
4927 dout(" object_prefix = %s\n", rbd_dev->header.object_prefix);
4928 }
Alex Elder1e130192012-07-03 16:01:19 -05004929out:
4930 kfree(reply_buf);
4931
4932 return ret;
4933}
4934
Alex Elderb1b54022012-07-03 16:01:19 -05004935static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
4936 u64 *snap_features)
4937{
4938 __le64 snapid = cpu_to_le64(snap_id);
4939 struct {
4940 __le64 features;
4941 __le64 incompat;
Alex Elder41579762013-04-21 12:14:45 -05004942 } __attribute__ ((packed)) features_buf = { 0 };
Ilya Dryomovd3767f02016-04-13 14:15:50 +02004943 u64 unsup;
Alex Elderb1b54022012-07-03 16:01:19 -05004944 int ret;
4945
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004946 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
4947 &rbd_dev->header_oloc, "get_features",
4948 &snapid, sizeof(snapid),
4949 &features_buf, sizeof(features_buf));
Alex Elder36be9a72013-01-19 00:30:28 -06004950 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderb1b54022012-07-03 16:01:19 -05004951 if (ret < 0)
4952 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05004953 if (ret < sizeof (features_buf))
4954 return -ERANGE;
Alex Elderd8891402012-10-09 13:50:17 -07004955
Ilya Dryomovd3767f02016-04-13 14:15:50 +02004956 unsup = le64_to_cpu(features_buf.incompat) & ~RBD_FEATURES_SUPPORTED;
4957 if (unsup) {
4958 rbd_warn(rbd_dev, "image uses unsupported features: 0x%llx",
4959 unsup);
Alex Elderb8f5c6e2012-11-01 08:39:26 -05004960 return -ENXIO;
Ilya Dryomovd3767f02016-04-13 14:15:50 +02004961 }
Alex Elderd8891402012-10-09 13:50:17 -07004962
Alex Elderb1b54022012-07-03 16:01:19 -05004963 *snap_features = le64_to_cpu(features_buf.features);
4964
4965 dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
Alex Elder57385b52013-04-21 12:14:45 -05004966 (unsigned long long)snap_id,
4967 (unsigned long long)*snap_features,
4968 (unsigned long long)le64_to_cpu(features_buf.incompat));
Alex Elderb1b54022012-07-03 16:01:19 -05004969
4970 return 0;
4971}
4972
4973static int rbd_dev_v2_features(struct rbd_device *rbd_dev)
4974{
4975 return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP,
4976 &rbd_dev->header.features);
4977}
4978
Alex Elder86b00e02012-10-25 23:34:42 -05004979static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev)
4980{
4981 struct rbd_spec *parent_spec;
4982 size_t size;
4983 void *reply_buf = NULL;
4984 __le64 snapid;
4985 void *p;
4986 void *end;
Alex Elder642a2532013-05-06 17:40:33 -05004987 u64 pool_id;
Alex Elder86b00e02012-10-25 23:34:42 -05004988 char *image_id;
Alex Elder3b5cf2a2013-05-29 11:18:59 -05004989 u64 snap_id;
Alex Elder86b00e02012-10-25 23:34:42 -05004990 u64 overlap;
Alex Elder86b00e02012-10-25 23:34:42 -05004991 int ret;
4992
4993 parent_spec = rbd_spec_alloc();
4994 if (!parent_spec)
4995 return -ENOMEM;
4996
4997 size = sizeof (__le64) + /* pool_id */
4998 sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */
4999 sizeof (__le64) + /* snap_id */
5000 sizeof (__le64); /* overlap */
5001 reply_buf = kmalloc(size, GFP_KERNEL);
5002 if (!reply_buf) {
5003 ret = -ENOMEM;
5004 goto out_err;
5005 }
5006
Ilya Dryomov4d9b67c2014-07-24 10:42:13 +04005007 snapid = cpu_to_le64(rbd_dev->spec->snap_id);
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005008 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5009 &rbd_dev->header_oloc, "get_parent",
5010 &snapid, sizeof(snapid), reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06005011 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder86b00e02012-10-25 23:34:42 -05005012 if (ret < 0)
5013 goto out_err;
5014
Alex Elder86b00e02012-10-25 23:34:42 -05005015 p = reply_buf;
Alex Elder57385b52013-04-21 12:14:45 -05005016 end = reply_buf + ret;
5017 ret = -ERANGE;
Alex Elder642a2532013-05-06 17:40:33 -05005018 ceph_decode_64_safe(&p, end, pool_id, out_err);
Alex Elder392a9da2013-05-06 17:40:33 -05005019 if (pool_id == CEPH_NOPOOL) {
5020 /*
5021 * Either the parent never existed, or we have
5022 * record of it but the image got flattened so it no
5023 * longer has a parent. When the parent of a
5024 * layered image disappears we immediately set the
5025 * overlap to 0. The effect of this is that all new
5026 * requests will be treated as if the image had no
5027 * parent.
5028 */
5029 if (rbd_dev->parent_overlap) {
5030 rbd_dev->parent_overlap = 0;
Alex Elder392a9da2013-05-06 17:40:33 -05005031 rbd_dev_parent_put(rbd_dev);
5032 pr_info("%s: clone image has been flattened\n",
5033 rbd_dev->disk->disk_name);
5034 }
5035
Alex Elder86b00e02012-10-25 23:34:42 -05005036 goto out; /* No parent? No problem. */
Alex Elder392a9da2013-05-06 17:40:33 -05005037 }
Alex Elder86b00e02012-10-25 23:34:42 -05005038
Alex Elder0903e872012-11-14 12:25:19 -06005039 /* The ceph file layout needs to fit pool id in 32 bits */
5040
5041 ret = -EIO;
Alex Elder642a2532013-05-06 17:40:33 -05005042 if (pool_id > (u64)U32_MAX) {
Ilya Dryomov9584d502014-07-11 12:11:20 +04005043 rbd_warn(NULL, "parent pool id too large (%llu > %u)",
Alex Elder642a2532013-05-06 17:40:33 -05005044 (unsigned long long)pool_id, U32_MAX);
Alex Elder57385b52013-04-21 12:14:45 -05005045 goto out_err;
Alex Elderc0cd10db2013-04-26 09:43:47 -05005046 }
Alex Elder0903e872012-11-14 12:25:19 -06005047
Alex Elder979ed482012-11-01 08:39:26 -05005048 image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elder86b00e02012-10-25 23:34:42 -05005049 if (IS_ERR(image_id)) {
5050 ret = PTR_ERR(image_id);
5051 goto out_err;
5052 }
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005053 ceph_decode_64_safe(&p, end, snap_id, out_err);
Alex Elder86b00e02012-10-25 23:34:42 -05005054 ceph_decode_64_safe(&p, end, overlap, out_err);
5055
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005056 /*
5057 * The parent won't change (except when the clone is
5058 * flattened, already handled that). So we only need to
5059 * record the parent spec we have not already done so.
5060 */
5061 if (!rbd_dev->parent_spec) {
5062 parent_spec->pool_id = pool_id;
5063 parent_spec->image_id = image_id;
5064 parent_spec->snap_id = snap_id;
Alex Elder70cf49c2013-05-06 17:40:33 -05005065 rbd_dev->parent_spec = parent_spec;
5066 parent_spec = NULL; /* rbd_dev now owns this */
Ilya Dryomovfbba11b2014-06-27 21:46:33 +04005067 } else {
5068 kfree(image_id);
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005069 }
5070
5071 /*
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005072 * We always update the parent overlap. If it's zero we issue
5073 * a warning, as we will proceed as if there was no parent.
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005074 */
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005075 if (!overlap) {
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005076 if (parent_spec) {
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005077 /* refresh, careful to warn just once */
5078 if (rbd_dev->parent_overlap)
5079 rbd_warn(rbd_dev,
5080 "clone now standalone (overlap became 0)");
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005081 } else {
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005082 /* initial probe */
5083 rbd_warn(rbd_dev, "clone is standalone (overlap 0)");
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005084 }
Alex Elder70cf49c2013-05-06 17:40:33 -05005085 }
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005086 rbd_dev->parent_overlap = overlap;
5087
Alex Elder86b00e02012-10-25 23:34:42 -05005088out:
5089 ret = 0;
5090out_err:
5091 kfree(reply_buf);
5092 rbd_spec_put(parent_spec);
5093
5094 return ret;
5095}
5096
Alex Eldercc070d52013-04-21 12:14:45 -05005097static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev)
5098{
5099 struct {
5100 __le64 stripe_unit;
5101 __le64 stripe_count;
5102 } __attribute__ ((packed)) striping_info_buf = { 0 };
5103 size_t size = sizeof (striping_info_buf);
5104 void *p;
5105 u64 obj_size;
5106 u64 stripe_unit;
5107 u64 stripe_count;
5108 int ret;
5109
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005110 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5111 &rbd_dev->header_oloc, "get_stripe_unit_count",
5112 NULL, 0, &striping_info_buf, size);
Alex Eldercc070d52013-04-21 12:14:45 -05005113 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
5114 if (ret < 0)
5115 return ret;
5116 if (ret < size)
5117 return -ERANGE;
5118
5119 /*
5120 * We don't actually support the "fancy striping" feature
5121 * (STRIPINGV2) yet, but if the striping sizes are the
5122 * defaults the behavior is the same as before. So find
5123 * out, and only fail if the image has non-default values.
5124 */
5125 ret = -EINVAL;
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +01005126 obj_size = rbd_obj_bytes(&rbd_dev->header);
Alex Eldercc070d52013-04-21 12:14:45 -05005127 p = &striping_info_buf;
5128 stripe_unit = ceph_decode_64(&p);
5129 if (stripe_unit != obj_size) {
5130 rbd_warn(rbd_dev, "unsupported stripe unit "
5131 "(got %llu want %llu)",
5132 stripe_unit, obj_size);
5133 return -EINVAL;
5134 }
5135 stripe_count = ceph_decode_64(&p);
5136 if (stripe_count != 1) {
5137 rbd_warn(rbd_dev, "unsupported stripe count "
5138 "(got %llu want 1)", stripe_count);
5139 return -EINVAL;
5140 }
Alex Elder500d0c02013-04-26 09:43:47 -05005141 rbd_dev->header.stripe_unit = stripe_unit;
5142 rbd_dev->header.stripe_count = stripe_count;
Alex Eldercc070d52013-04-21 12:14:45 -05005143
5144 return 0;
5145}
5146
Ilya Dryomov7e973322017-01-25 18:16:22 +01005147static int rbd_dev_v2_data_pool(struct rbd_device *rbd_dev)
5148{
5149 __le64 data_pool_id;
5150 int ret;
5151
5152 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5153 &rbd_dev->header_oloc, "get_data_pool",
5154 NULL, 0, &data_pool_id, sizeof(data_pool_id));
5155 if (ret < 0)
5156 return ret;
5157 if (ret < sizeof(data_pool_id))
5158 return -EBADMSG;
5159
5160 rbd_dev->header.data_pool_id = le64_to_cpu(data_pool_id);
5161 WARN_ON(rbd_dev->header.data_pool_id == CEPH_NOPOOL);
5162 return 0;
5163}
5164
Alex Elder9e15b772012-10-30 19:40:33 -05005165static char *rbd_dev_image_name(struct rbd_device *rbd_dev)
5166{
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005167 CEPH_DEFINE_OID_ONSTACK(oid);
Alex Elder9e15b772012-10-30 19:40:33 -05005168 size_t image_id_size;
5169 char *image_id;
5170 void *p;
5171 void *end;
5172 size_t size;
5173 void *reply_buf = NULL;
5174 size_t len = 0;
5175 char *image_name = NULL;
5176 int ret;
5177
5178 rbd_assert(!rbd_dev->spec->image_name);
5179
Alex Elder69e7a022012-11-01 08:39:26 -05005180 len = strlen(rbd_dev->spec->image_id);
5181 image_id_size = sizeof (__le32) + len;
Alex Elder9e15b772012-10-30 19:40:33 -05005182 image_id = kmalloc(image_id_size, GFP_KERNEL);
5183 if (!image_id)
5184 return NULL;
5185
5186 p = image_id;
Alex Elder41579762013-04-21 12:14:45 -05005187 end = image_id + image_id_size;
Alex Elder57385b52013-04-21 12:14:45 -05005188 ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len);
Alex Elder9e15b772012-10-30 19:40:33 -05005189
5190 size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX;
5191 reply_buf = kmalloc(size, GFP_KERNEL);
5192 if (!reply_buf)
5193 goto out;
5194
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005195 ceph_oid_printf(&oid, "%s", RBD_DIRECTORY);
5196 ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc,
5197 "dir_get_name", image_id, image_id_size,
5198 reply_buf, size);
Alex Elder9e15b772012-10-30 19:40:33 -05005199 if (ret < 0)
5200 goto out;
5201 p = reply_buf;
Alex Elderf40eb342013-04-25 15:09:42 -05005202 end = reply_buf + ret;
5203
Alex Elder9e15b772012-10-30 19:40:33 -05005204 image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL);
5205 if (IS_ERR(image_name))
5206 image_name = NULL;
5207 else
5208 dout("%s: name is %s len is %zd\n", __func__, image_name, len);
5209out:
5210 kfree(reply_buf);
5211 kfree(image_id);
5212
5213 return image_name;
5214}
5215
Alex Elder2ad3d712013-04-30 00:44:33 -05005216static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
5217{
5218 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
5219 const char *snap_name;
5220 u32 which = 0;
5221
5222 /* Skip over names until we find the one we are looking for */
5223
5224 snap_name = rbd_dev->header.snap_names;
5225 while (which < snapc->num_snaps) {
5226 if (!strcmp(name, snap_name))
5227 return snapc->snaps[which];
5228 snap_name += strlen(snap_name) + 1;
5229 which++;
5230 }
5231 return CEPH_NOSNAP;
5232}
5233
5234static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
5235{
5236 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
5237 u32 which;
5238 bool found = false;
5239 u64 snap_id;
5240
5241 for (which = 0; !found && which < snapc->num_snaps; which++) {
5242 const char *snap_name;
5243
5244 snap_id = snapc->snaps[which];
5245 snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id);
Josh Durginefadc982013-08-29 19:16:42 -07005246 if (IS_ERR(snap_name)) {
5247 /* ignore no-longer existing snapshots */
5248 if (PTR_ERR(snap_name) == -ENOENT)
5249 continue;
5250 else
5251 break;
5252 }
Alex Elder2ad3d712013-04-30 00:44:33 -05005253 found = !strcmp(name, snap_name);
5254 kfree(snap_name);
5255 }
5256 return found ? snap_id : CEPH_NOSNAP;
5257}
5258
5259/*
5260 * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if
5261 * no snapshot by that name is found, or if an error occurs.
5262 */
5263static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
5264{
5265 if (rbd_dev->image_format == 1)
5266 return rbd_v1_snap_id_by_name(rbd_dev, name);
5267
5268 return rbd_v2_snap_id_by_name(rbd_dev, name);
5269}
5270
Alex Elder9e15b772012-10-30 19:40:33 -05005271/*
Ilya Dryomov04077592014-07-23 17:11:20 +04005272 * An image being mapped will have everything but the snap id.
Alex Elder9e15b772012-10-30 19:40:33 -05005273 */
Ilya Dryomov04077592014-07-23 17:11:20 +04005274static int rbd_spec_fill_snap_id(struct rbd_device *rbd_dev)
5275{
5276 struct rbd_spec *spec = rbd_dev->spec;
5277
5278 rbd_assert(spec->pool_id != CEPH_NOPOOL && spec->pool_name);
5279 rbd_assert(spec->image_id && spec->image_name);
5280 rbd_assert(spec->snap_name);
5281
5282 if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) {
5283 u64 snap_id;
5284
5285 snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name);
5286 if (snap_id == CEPH_NOSNAP)
5287 return -ENOENT;
5288
5289 spec->snap_id = snap_id;
5290 } else {
5291 spec->snap_id = CEPH_NOSNAP;
5292 }
5293
5294 return 0;
5295}
5296
5297/*
5298 * A parent image will have all ids but none of the names.
5299 *
5300 * All names in an rbd spec are dynamically allocated. It's OK if we
5301 * can't figure out the name for an image id.
5302 */
5303static int rbd_spec_fill_names(struct rbd_device *rbd_dev)
Alex Elder9e15b772012-10-30 19:40:33 -05005304{
Alex Elder2e9f7f12013-04-26 09:43:48 -05005305 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
5306 struct rbd_spec *spec = rbd_dev->spec;
5307 const char *pool_name;
5308 const char *image_name;
5309 const char *snap_name;
Alex Elder9e15b772012-10-30 19:40:33 -05005310 int ret;
5311
Ilya Dryomov04077592014-07-23 17:11:20 +04005312 rbd_assert(spec->pool_id != CEPH_NOPOOL);
5313 rbd_assert(spec->image_id);
5314 rbd_assert(spec->snap_id != CEPH_NOSNAP);
Alex Elder9e15b772012-10-30 19:40:33 -05005315
Alex Elder2e9f7f12013-04-26 09:43:48 -05005316 /* Get the pool name; we have to make our own copy of this */
Alex Elder9e15b772012-10-30 19:40:33 -05005317
Alex Elder2e9f7f12013-04-26 09:43:48 -05005318 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id);
5319 if (!pool_name) {
5320 rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id);
Alex Elder935dc892012-11-01 10:17:15 -05005321 return -EIO;
5322 }
Alex Elder2e9f7f12013-04-26 09:43:48 -05005323 pool_name = kstrdup(pool_name, GFP_KERNEL);
5324 if (!pool_name)
Alex Elder9e15b772012-10-30 19:40:33 -05005325 return -ENOMEM;
5326
5327 /* Fetch the image name; tolerate failure here */
5328
Alex Elder2e9f7f12013-04-26 09:43:48 -05005329 image_name = rbd_dev_image_name(rbd_dev);
5330 if (!image_name)
Alex Elder06ecc6c2012-11-01 10:17:15 -05005331 rbd_warn(rbd_dev, "unable to get image name");
Alex Elder9e15b772012-10-30 19:40:33 -05005332
Ilya Dryomov04077592014-07-23 17:11:20 +04005333 /* Fetch the snapshot name */
Alex Elder9e15b772012-10-30 19:40:33 -05005334
Alex Elder2e9f7f12013-04-26 09:43:48 -05005335 snap_name = rbd_snap_name(rbd_dev, spec->snap_id);
Josh Durginda6a6b62013-09-04 17:57:31 -07005336 if (IS_ERR(snap_name)) {
5337 ret = PTR_ERR(snap_name);
Alex Elder9e15b772012-10-30 19:40:33 -05005338 goto out_err;
Alex Elder2e9f7f12013-04-26 09:43:48 -05005339 }
5340
5341 spec->pool_name = pool_name;
5342 spec->image_name = image_name;
5343 spec->snap_name = snap_name;
Alex Elder9e15b772012-10-30 19:40:33 -05005344
5345 return 0;
Ilya Dryomov04077592014-07-23 17:11:20 +04005346
Alex Elder9e15b772012-10-30 19:40:33 -05005347out_err:
Alex Elder2e9f7f12013-04-26 09:43:48 -05005348 kfree(image_name);
5349 kfree(pool_name);
Alex Elder9e15b772012-10-30 19:40:33 -05005350 return ret;
5351}
5352
Alex Eldercc4a38bd2013-04-30 00:44:33 -05005353static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev)
Alex Elder35d489f2012-07-03 16:01:19 -05005354{
5355 size_t size;
5356 int ret;
5357 void *reply_buf;
5358 void *p;
5359 void *end;
5360 u64 seq;
5361 u32 snap_count;
5362 struct ceph_snap_context *snapc;
5363 u32 i;
5364
5365 /*
5366 * We'll need room for the seq value (maximum snapshot id),
5367 * snapshot count, and array of that many snapshot ids.
5368 * For now we have a fixed upper limit on the number we're
5369 * prepared to receive.
5370 */
5371 size = sizeof (__le64) + sizeof (__le32) +
5372 RBD_MAX_SNAP_COUNT * sizeof (__le64);
5373 reply_buf = kzalloc(size, GFP_KERNEL);
5374 if (!reply_buf)
5375 return -ENOMEM;
5376
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005377 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5378 &rbd_dev->header_oloc, "get_snapcontext",
5379 NULL, 0, reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06005380 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder35d489f2012-07-03 16:01:19 -05005381 if (ret < 0)
5382 goto out;
5383
Alex Elder35d489f2012-07-03 16:01:19 -05005384 p = reply_buf;
Alex Elder57385b52013-04-21 12:14:45 -05005385 end = reply_buf + ret;
5386 ret = -ERANGE;
Alex Elder35d489f2012-07-03 16:01:19 -05005387 ceph_decode_64_safe(&p, end, seq, out);
5388 ceph_decode_32_safe(&p, end, snap_count, out);
5389
5390 /*
5391 * Make sure the reported number of snapshot ids wouldn't go
5392 * beyond the end of our buffer. But before checking that,
5393 * make sure the computed size of the snapshot context we
5394 * allocate is representable in a size_t.
5395 */
5396 if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
5397 / sizeof (u64)) {
5398 ret = -EINVAL;
5399 goto out;
5400 }
5401 if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
5402 goto out;
Alex Elder468521c2013-04-26 09:43:47 -05005403 ret = 0;
Alex Elder35d489f2012-07-03 16:01:19 -05005404
Alex Elder812164f82013-04-30 00:44:32 -05005405 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
Alex Elder35d489f2012-07-03 16:01:19 -05005406 if (!snapc) {
5407 ret = -ENOMEM;
5408 goto out;
5409 }
Alex Elder35d489f2012-07-03 16:01:19 -05005410 snapc->seq = seq;
Alex Elder35d489f2012-07-03 16:01:19 -05005411 for (i = 0; i < snap_count; i++)
5412 snapc->snaps[i] = ceph_decode_64(&p);
5413
Alex Elder49ece552013-05-06 08:37:00 -05005414 ceph_put_snap_context(rbd_dev->header.snapc);
Alex Elder35d489f2012-07-03 16:01:19 -05005415 rbd_dev->header.snapc = snapc;
5416
5417 dout(" snap context seq = %llu, snap_count = %u\n",
Alex Elder57385b52013-04-21 12:14:45 -05005418 (unsigned long long)seq, (unsigned int)snap_count);
Alex Elder35d489f2012-07-03 16:01:19 -05005419out:
5420 kfree(reply_buf);
5421
Alex Elder57385b52013-04-21 12:14:45 -05005422 return ret;
Alex Elder35d489f2012-07-03 16:01:19 -05005423}
5424
Alex Elder54cac612013-04-30 00:44:33 -05005425static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
5426 u64 snap_id)
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005427{
5428 size_t size;
5429 void *reply_buf;
Alex Elder54cac612013-04-30 00:44:33 -05005430 __le64 snapid;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005431 int ret;
5432 void *p;
5433 void *end;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005434 char *snap_name;
5435
5436 size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN;
5437 reply_buf = kmalloc(size, GFP_KERNEL);
5438 if (!reply_buf)
5439 return ERR_PTR(-ENOMEM);
5440
Alex Elder54cac612013-04-30 00:44:33 -05005441 snapid = cpu_to_le64(snap_id);
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005442 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5443 &rbd_dev->header_oloc, "get_snapshot_name",
5444 &snapid, sizeof(snapid), reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06005445 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderf40eb342013-04-25 15:09:42 -05005446 if (ret < 0) {
5447 snap_name = ERR_PTR(ret);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005448 goto out;
Alex Elderf40eb342013-04-25 15:09:42 -05005449 }
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005450
5451 p = reply_buf;
Alex Elderf40eb342013-04-25 15:09:42 -05005452 end = reply_buf + ret;
Alex Eldere5c35532012-10-25 23:34:41 -05005453 snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elderf40eb342013-04-25 15:09:42 -05005454 if (IS_ERR(snap_name))
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005455 goto out;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005456
Alex Elderf40eb342013-04-25 15:09:42 -05005457 dout(" snap_id 0x%016llx snap_name = %s\n",
Alex Elder54cac612013-04-30 00:44:33 -05005458 (unsigned long long)snap_id, snap_name);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005459out:
5460 kfree(reply_buf);
5461
Alex Elderf40eb342013-04-25 15:09:42 -05005462 return snap_name;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005463}
5464
Alex Elder2df3fac2013-05-06 09:51:30 -05005465static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev)
Alex Elder117973f2012-08-31 17:29:55 -05005466{
Alex Elder2df3fac2013-05-06 09:51:30 -05005467 bool first_time = rbd_dev->header.object_prefix == NULL;
Alex Elder117973f2012-08-31 17:29:55 -05005468 int ret;
Alex Elder117973f2012-08-31 17:29:55 -05005469
Josh Durgin1617e402013-06-12 14:43:10 -07005470 ret = rbd_dev_v2_image_size(rbd_dev);
5471 if (ret)
Alex Eldercfbf6372013-05-31 17:40:45 -05005472 return ret;
Josh Durgin1617e402013-06-12 14:43:10 -07005473
Alex Elder2df3fac2013-05-06 09:51:30 -05005474 if (first_time) {
5475 ret = rbd_dev_v2_header_onetime(rbd_dev);
5476 if (ret)
Alex Eldercfbf6372013-05-31 17:40:45 -05005477 return ret;
Alex Elder2df3fac2013-05-06 09:51:30 -05005478 }
5479
Alex Eldercc4a38bd2013-04-30 00:44:33 -05005480 ret = rbd_dev_v2_snap_context(rbd_dev);
Ilya Dryomovd194cd12015-08-31 18:22:10 +03005481 if (ret && first_time) {
5482 kfree(rbd_dev->header.object_prefix);
5483 rbd_dev->header.object_prefix = NULL;
5484 }
Alex Elder117973f2012-08-31 17:29:55 -05005485
5486 return ret;
5487}
5488
Ilya Dryomova720ae02014-07-23 17:11:19 +04005489static int rbd_dev_header_info(struct rbd_device *rbd_dev)
5490{
5491 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
5492
5493 if (rbd_dev->image_format == 1)
5494 return rbd_dev_v1_header_info(rbd_dev);
5495
5496 return rbd_dev_v2_header_info(rbd_dev);
5497}
5498
Alex Elder1ddbe942012-01-29 13:57:44 -06005499/*
Alex Eldere28fff262012-02-02 08:13:30 -06005500 * Skips over white space at *buf, and updates *buf to point to the
5501 * first found non-space character (if any). Returns the length of
Alex Elder593a9e72012-02-07 12:03:37 -06005502 * the token (string of non-white space characters) found. Note
5503 * that *buf must be terminated with '\0'.
Alex Eldere28fff262012-02-02 08:13:30 -06005504 */
5505static inline size_t next_token(const char **buf)
5506{
5507 /*
5508 * These are the characters that produce nonzero for
5509 * isspace() in the "C" and "POSIX" locales.
5510 */
5511 const char *spaces = " \f\n\r\t\v";
5512
5513 *buf += strspn(*buf, spaces); /* Find start of token */
5514
5515 return strcspn(*buf, spaces); /* Return token length */
5516}
5517
5518/*
Alex Elderea3352f2012-07-09 21:04:23 -05005519 * Finds the next token in *buf, dynamically allocates a buffer big
5520 * enough to hold a copy of it, and copies the token into the new
5521 * buffer. The copy is guaranteed to be terminated with '\0'. Note
5522 * that a duplicate buffer is created even for a zero-length token.
5523 *
5524 * Returns a pointer to the newly-allocated duplicate, or a null
5525 * pointer if memory for the duplicate was not available. If
5526 * the lenp argument is a non-null pointer, the length of the token
5527 * (not including the '\0') is returned in *lenp.
5528 *
5529 * If successful, the *buf pointer will be updated to point beyond
5530 * the end of the found token.
5531 *
5532 * Note: uses GFP_KERNEL for allocation.
5533 */
5534static inline char *dup_token(const char **buf, size_t *lenp)
5535{
5536 char *dup;
5537 size_t len;
5538
5539 len = next_token(buf);
Alex Elder4caf35f2012-11-01 08:39:27 -05005540 dup = kmemdup(*buf, len + 1, GFP_KERNEL);
Alex Elderea3352f2012-07-09 21:04:23 -05005541 if (!dup)
5542 return NULL;
Alex Elderea3352f2012-07-09 21:04:23 -05005543 *(dup + len) = '\0';
5544 *buf += len;
5545
5546 if (lenp)
5547 *lenp = len;
5548
5549 return dup;
5550}
5551
5552/*
Alex Elder859c31d2012-10-25 23:34:42 -05005553 * Parse the options provided for an "rbd add" (i.e., rbd image
5554 * mapping) request. These arrive via a write to /sys/bus/rbd/add,
5555 * and the data written is passed here via a NUL-terminated buffer.
5556 * Returns 0 if successful or an error code otherwise.
Alex Elderd22f76e2012-07-12 10:46:35 -05005557 *
Alex Elder859c31d2012-10-25 23:34:42 -05005558 * The information extracted from these options is recorded in
5559 * the other parameters which return dynamically-allocated
5560 * structures:
5561 * ceph_opts
5562 * The address of a pointer that will refer to a ceph options
5563 * structure. Caller must release the returned pointer using
5564 * ceph_destroy_options() when it is no longer needed.
5565 * rbd_opts
5566 * Address of an rbd options pointer. Fully initialized by
5567 * this function; caller must release with kfree().
5568 * spec
5569 * Address of an rbd image specification pointer. Fully
5570 * initialized by this function based on parsed options.
5571 * Caller must release with rbd_spec_put().
5572 *
5573 * The options passed take this form:
5574 * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
5575 * where:
5576 * <mon_addrs>
5577 * A comma-separated list of one or more monitor addresses.
5578 * A monitor address is an ip address, optionally followed
5579 * by a port number (separated by a colon).
5580 * I.e.: ip1[:port1][,ip2[:port2]...]
5581 * <options>
5582 * A comma-separated list of ceph and/or rbd options.
5583 * <pool_name>
5584 * The name of the rados pool containing the rbd image.
5585 * <image_name>
5586 * The name of the image in that pool to map.
5587 * <snap_id>
5588 * An optional snapshot id. If provided, the mapping will
5589 * present data from the image at the time that snapshot was
5590 * created. The image head is used if no snapshot id is
5591 * provided. Snapshot mappings are always read-only.
Alex Eldera725f65e2012-02-02 08:13:30 -06005592 */
Alex Elder859c31d2012-10-25 23:34:42 -05005593static int rbd_add_parse_args(const char *buf,
Alex Elderdc79b112012-10-25 23:34:41 -05005594 struct ceph_options **ceph_opts,
Alex Elder859c31d2012-10-25 23:34:42 -05005595 struct rbd_options **opts,
5596 struct rbd_spec **rbd_spec)
Alex Eldera725f65e2012-02-02 08:13:30 -06005597{
Alex Elderd22f76e2012-07-12 10:46:35 -05005598 size_t len;
Alex Elder859c31d2012-10-25 23:34:42 -05005599 char *options;
Alex Elder0ddebc02012-10-25 23:34:41 -05005600 const char *mon_addrs;
Alex Elderecb4dc22013-04-26 09:43:47 -05005601 char *snap_name;
Alex Elder0ddebc02012-10-25 23:34:41 -05005602 size_t mon_addrs_size;
Alex Elder859c31d2012-10-25 23:34:42 -05005603 struct rbd_spec *spec = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05005604 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05005605 struct ceph_options *copts;
Alex Elderdc79b112012-10-25 23:34:41 -05005606 int ret;
Alex Eldere28fff262012-02-02 08:13:30 -06005607
5608 /* The first four tokens are required */
5609
Alex Elder7ef32142012-02-02 08:13:30 -06005610 len = next_token(&buf);
Alex Elder4fb5d6712012-11-01 10:17:15 -05005611 if (!len) {
5612 rbd_warn(NULL, "no monitor address(es) provided");
5613 return -EINVAL;
5614 }
Alex Elder0ddebc02012-10-25 23:34:41 -05005615 mon_addrs = buf;
Alex Elderf28e5652012-10-25 23:34:41 -05005616 mon_addrs_size = len + 1;
Alex Elder7ef32142012-02-02 08:13:30 -06005617 buf += len;
Alex Eldera725f65e2012-02-02 08:13:30 -06005618
Alex Elderdc79b112012-10-25 23:34:41 -05005619 ret = -EINVAL;
Alex Elderf28e5652012-10-25 23:34:41 -05005620 options = dup_token(&buf, NULL);
5621 if (!options)
Alex Elderdc79b112012-10-25 23:34:41 -05005622 return -ENOMEM;
Alex Elder4fb5d6712012-11-01 10:17:15 -05005623 if (!*options) {
5624 rbd_warn(NULL, "no options provided");
5625 goto out_err;
5626 }
Alex Eldera725f65e2012-02-02 08:13:30 -06005627
Alex Elder859c31d2012-10-25 23:34:42 -05005628 spec = rbd_spec_alloc();
5629 if (!spec)
Alex Elderf28e5652012-10-25 23:34:41 -05005630 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05005631
5632 spec->pool_name = dup_token(&buf, NULL);
5633 if (!spec->pool_name)
5634 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05005635 if (!*spec->pool_name) {
5636 rbd_warn(NULL, "no pool name provided");
5637 goto out_err;
5638 }
Alex Eldere28fff262012-02-02 08:13:30 -06005639
Alex Elder69e7a022012-11-01 08:39:26 -05005640 spec->image_name = dup_token(&buf, NULL);
Alex Elder859c31d2012-10-25 23:34:42 -05005641 if (!spec->image_name)
Alex Elderf28e5652012-10-25 23:34:41 -05005642 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05005643 if (!*spec->image_name) {
5644 rbd_warn(NULL, "no image name provided");
5645 goto out_err;
5646 }
Alex Eldere28fff262012-02-02 08:13:30 -06005647
Alex Elderf28e5652012-10-25 23:34:41 -05005648 /*
5649 * Snapshot name is optional; default is to use "-"
5650 * (indicating the head/no snapshot).
5651 */
Alex Elder3feeb8942012-08-31 17:29:52 -05005652 len = next_token(&buf);
Alex Elder820a5f32012-07-09 21:04:24 -05005653 if (!len) {
Alex Elder3feeb8942012-08-31 17:29:52 -05005654 buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
5655 len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
Alex Elderf28e5652012-10-25 23:34:41 -05005656 } else if (len > RBD_MAX_SNAP_NAME_LEN) {
Alex Elderdc79b112012-10-25 23:34:41 -05005657 ret = -ENAMETOOLONG;
Alex Elderf28e5652012-10-25 23:34:41 -05005658 goto out_err;
Alex Elder849b4262012-07-09 21:04:24 -05005659 }
Alex Elderecb4dc22013-04-26 09:43:47 -05005660 snap_name = kmemdup(buf, len + 1, GFP_KERNEL);
5661 if (!snap_name)
Alex Elderf28e5652012-10-25 23:34:41 -05005662 goto out_mem;
Alex Elderecb4dc22013-04-26 09:43:47 -05005663 *(snap_name + len) = '\0';
5664 spec->snap_name = snap_name;
Alex Eldere5c35532012-10-25 23:34:41 -05005665
Alex Elder0ddebc02012-10-25 23:34:41 -05005666 /* Initialize all rbd options to the defaults */
Alex Eldere28fff262012-02-02 08:13:30 -06005667
Alex Elder4e9afeb2012-10-25 23:34:41 -05005668 rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL);
5669 if (!rbd_opts)
5670 goto out_mem;
5671
5672 rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
Ilya Dryomovb5584182015-06-23 16:21:19 +03005673 rbd_opts->queue_depth = RBD_QUEUE_DEPTH_DEFAULT;
Ilya Dryomov80de1912016-09-20 14:23:17 +02005674 rbd_opts->lock_on_read = RBD_LOCK_ON_READ_DEFAULT;
Alex Elderd22f76e2012-07-12 10:46:35 -05005675
Alex Elder859c31d2012-10-25 23:34:42 -05005676 copts = ceph_parse_options(options, mon_addrs,
Alex Elder0ddebc02012-10-25 23:34:41 -05005677 mon_addrs + mon_addrs_size - 1,
Alex Elder4e9afeb2012-10-25 23:34:41 -05005678 parse_rbd_opts_token, rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05005679 if (IS_ERR(copts)) {
5680 ret = PTR_ERR(copts);
Alex Elderdc79b112012-10-25 23:34:41 -05005681 goto out_err;
5682 }
Alex Elder859c31d2012-10-25 23:34:42 -05005683 kfree(options);
5684
5685 *ceph_opts = copts;
Alex Elder4e9afeb2012-10-25 23:34:41 -05005686 *opts = rbd_opts;
Alex Elder859c31d2012-10-25 23:34:42 -05005687 *rbd_spec = spec;
Alex Elder0ddebc02012-10-25 23:34:41 -05005688
Alex Elderdc79b112012-10-25 23:34:41 -05005689 return 0;
Alex Elderf28e5652012-10-25 23:34:41 -05005690out_mem:
Alex Elderdc79b112012-10-25 23:34:41 -05005691 ret = -ENOMEM;
Alex Elderd22f76e2012-07-12 10:46:35 -05005692out_err:
Alex Elder859c31d2012-10-25 23:34:42 -05005693 kfree(rbd_opts);
5694 rbd_spec_put(spec);
Alex Elderf28e5652012-10-25 23:34:41 -05005695 kfree(options);
Alex Elderd22f76e2012-07-12 10:46:35 -05005696
Alex Elderdc79b112012-10-25 23:34:41 -05005697 return ret;
Alex Eldera725f65e2012-02-02 08:13:30 -06005698}
5699
Alex Elder589d30e2012-07-10 20:30:11 -05005700/*
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005701 * Return pool id (>= 0) or a negative error code.
5702 */
5703static int rbd_add_get_pool_id(struct rbd_client *rbdc, const char *pool_name)
5704{
Ilya Dryomova319bf52015-05-15 12:02:17 +03005705 struct ceph_options *opts = rbdc->client->options;
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005706 u64 newest_epoch;
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005707 int tries = 0;
5708 int ret;
5709
5710again:
5711 ret = ceph_pg_poolid_by_name(rbdc->client->osdc.osdmap, pool_name);
5712 if (ret == -ENOENT && tries++ < 1) {
Ilya Dryomovd0b19702016-04-28 16:07:27 +02005713 ret = ceph_monc_get_version(&rbdc->client->monc, "osdmap",
5714 &newest_epoch);
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005715 if (ret < 0)
5716 return ret;
5717
5718 if (rbdc->client->osdc.osdmap->epoch < newest_epoch) {
Ilya Dryomov7cca78c2016-04-28 16:07:28 +02005719 ceph_osdc_maybe_request_map(&rbdc->client->osdc);
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005720 (void) ceph_monc_wait_osdmap(&rbdc->client->monc,
Ilya Dryomova319bf52015-05-15 12:02:17 +03005721 newest_epoch,
5722 opts->mount_timeout);
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005723 goto again;
5724 } else {
5725 /* the osdmap we have is new enough */
5726 return -ENOENT;
5727 }
5728 }
5729
5730 return ret;
5731}
5732
5733/*
Alex Elder589d30e2012-07-10 20:30:11 -05005734 * An rbd format 2 image has a unique identifier, distinct from the
5735 * name given to it by the user. Internally, that identifier is
5736 * what's used to specify the names of objects related to the image.
5737 *
5738 * A special "rbd id" object is used to map an rbd image name to its
5739 * id. If that object doesn't exist, then there is no v2 rbd image
5740 * with the supplied name.
5741 *
5742 * This function will record the given rbd_dev's image_id field if
5743 * it can be determined, and in that case will return 0. If any
5744 * errors occur a negative errno will be returned and the rbd_dev's
5745 * image_id field will be unchanged (and should be NULL).
5746 */
5747static int rbd_dev_image_id(struct rbd_device *rbd_dev)
5748{
5749 int ret;
5750 size_t size;
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005751 CEPH_DEFINE_OID_ONSTACK(oid);
Alex Elder589d30e2012-07-10 20:30:11 -05005752 void *response;
Alex Elderc0fba362013-04-25 23:15:08 -05005753 char *image_id;
Alex Elder2f82ee52012-10-30 19:40:33 -05005754
Alex Elder589d30e2012-07-10 20:30:11 -05005755 /*
Alex Elder2c0d0a12012-10-30 19:40:33 -05005756 * When probing a parent image, the image id is already
5757 * known (and the image name likely is not). There's no
Alex Elderc0fba362013-04-25 23:15:08 -05005758 * need to fetch the image id again in this case. We
5759 * do still need to set the image format though.
Alex Elder2c0d0a12012-10-30 19:40:33 -05005760 */
Alex Elderc0fba362013-04-25 23:15:08 -05005761 if (rbd_dev->spec->image_id) {
5762 rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1;
5763
Alex Elder2c0d0a12012-10-30 19:40:33 -05005764 return 0;
Alex Elderc0fba362013-04-25 23:15:08 -05005765 }
Alex Elder2c0d0a12012-10-30 19:40:33 -05005766
5767 /*
Alex Elder589d30e2012-07-10 20:30:11 -05005768 * First, see if the format 2 image id file exists, and if
5769 * so, get the image's persistent id from it.
5770 */
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005771 ret = ceph_oid_aprintf(&oid, GFP_KERNEL, "%s%s", RBD_ID_PREFIX,
5772 rbd_dev->spec->image_name);
5773 if (ret)
5774 return ret;
5775
5776 dout("rbd id object name is %s\n", oid.name);
Alex Elder589d30e2012-07-10 20:30:11 -05005777
5778 /* Response will be an encoded string, which includes a length */
5779
5780 size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
5781 response = kzalloc(size, GFP_NOIO);
5782 if (!response) {
5783 ret = -ENOMEM;
5784 goto out;
5785 }
5786
Alex Elderc0fba362013-04-25 23:15:08 -05005787 /* If it doesn't exist we'll assume it's a format 1 image */
5788
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005789 ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc,
5790 "get_id", NULL, 0,
5791 response, RBD_IMAGE_ID_LEN_MAX);
Alex Elder36be9a72013-01-19 00:30:28 -06005792 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderc0fba362013-04-25 23:15:08 -05005793 if (ret == -ENOENT) {
5794 image_id = kstrdup("", GFP_KERNEL);
5795 ret = image_id ? 0 : -ENOMEM;
5796 if (!ret)
5797 rbd_dev->image_format = 1;
Ilya Dryomov7dd440c2014-09-11 18:49:18 +04005798 } else if (ret >= 0) {
Alex Elderc0fba362013-04-25 23:15:08 -05005799 void *p = response;
Alex Elder589d30e2012-07-10 20:30:11 -05005800
Alex Elderc0fba362013-04-25 23:15:08 -05005801 image_id = ceph_extract_encoded_string(&p, p + ret,
Alex Elder979ed482012-11-01 08:39:26 -05005802 NULL, GFP_NOIO);
Duan Jiong461f7582014-04-11 16:38:12 +08005803 ret = PTR_ERR_OR_ZERO(image_id);
Alex Elderc0fba362013-04-25 23:15:08 -05005804 if (!ret)
5805 rbd_dev->image_format = 2;
Alex Elderc0fba362013-04-25 23:15:08 -05005806 }
5807
5808 if (!ret) {
5809 rbd_dev->spec->image_id = image_id;
5810 dout("image_id is %s\n", image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05005811 }
5812out:
5813 kfree(response);
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005814 ceph_oid_destroy(&oid);
Alex Elder589d30e2012-07-10 20:30:11 -05005815 return ret;
5816}
5817
Alex Elder3abef3b2013-05-13 20:35:37 -05005818/*
5819 * Undo whatever state changes are made by v1 or v2 header info
5820 * call.
5821 */
Alex Elder6fd48b32013-04-28 23:32:34 -05005822static void rbd_dev_unprobe(struct rbd_device *rbd_dev)
5823{
5824 struct rbd_image_header *header;
5825
Ilya Dryomove69b8d42015-01-19 12:06:14 +03005826 rbd_dev_parent_put(rbd_dev);
Alex Elder6fd48b32013-04-28 23:32:34 -05005827
5828 /* Free dynamic fields from the header, then zero it out */
5829
5830 header = &rbd_dev->header;
Alex Elder812164f82013-04-30 00:44:32 -05005831 ceph_put_snap_context(header->snapc);
Alex Elder6fd48b32013-04-28 23:32:34 -05005832 kfree(header->snap_sizes);
5833 kfree(header->snap_names);
5834 kfree(header->object_prefix);
5835 memset(header, 0, sizeof (*header));
5836}
5837
Alex Elder2df3fac2013-05-06 09:51:30 -05005838static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev)
Alex Eldera30b71b2012-07-10 20:30:11 -05005839{
5840 int ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05005841
Alex Elder1e130192012-07-03 16:01:19 -05005842 ret = rbd_dev_v2_object_prefix(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05005843 if (ret)
Alex Elder1e130192012-07-03 16:01:19 -05005844 goto out_err;
Alex Elderb1b54022012-07-03 16:01:19 -05005845
Alex Elder2df3fac2013-05-06 09:51:30 -05005846 /*
5847 * Get the and check features for the image. Currently the
5848 * features are assumed to never change.
5849 */
Alex Elderb1b54022012-07-03 16:01:19 -05005850 ret = rbd_dev_v2_features(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05005851 if (ret)
Alex Elderb1b54022012-07-03 16:01:19 -05005852 goto out_err;
Alex Elder35d489f2012-07-03 16:01:19 -05005853
Alex Eldercc070d52013-04-21 12:14:45 -05005854 /* If the image supports fancy striping, get its parameters */
5855
5856 if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) {
5857 ret = rbd_dev_v2_striping_info(rbd_dev);
5858 if (ret < 0)
5859 goto out_err;
5860 }
Alex Eldera30b71b2012-07-10 20:30:11 -05005861
Ilya Dryomov7e973322017-01-25 18:16:22 +01005862 if (rbd_dev->header.features & RBD_FEATURE_DATA_POOL) {
5863 ret = rbd_dev_v2_data_pool(rbd_dev);
5864 if (ret)
5865 goto out_err;
5866 }
5867
Ilya Dryomov263423f2017-01-25 18:16:22 +01005868 rbd_init_layout(rbd_dev);
Alex Elder35152972012-08-31 17:29:55 -05005869 return 0;
Ilya Dryomov263423f2017-01-25 18:16:22 +01005870
Alex Elder9d475de2012-07-03 16:01:19 -05005871out_err:
Alex Elder642a2532013-05-06 17:40:33 -05005872 rbd_dev->header.features = 0;
Alex Elder1e130192012-07-03 16:01:19 -05005873 kfree(rbd_dev->header.object_prefix);
5874 rbd_dev->header.object_prefix = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05005875 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05005876}
5877
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005878/*
5879 * @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() ->
5880 * rbd_dev_image_probe() recursion depth, which means it's also the
5881 * length of the already discovered part of the parent chain.
5882 */
5883static int rbd_dev_probe_parent(struct rbd_device *rbd_dev, int depth)
Alex Elder83a06262012-10-30 15:47:17 -05005884{
Alex Elder2f82ee52012-10-30 19:40:33 -05005885 struct rbd_device *parent = NULL;
Alex Elder124afba2013-04-26 15:44:36 -05005886 int ret;
5887
5888 if (!rbd_dev->parent_spec)
5889 return 0;
Alex Elder124afba2013-04-26 15:44:36 -05005890
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005891 if (++depth > RBD_MAX_PARENT_CHAIN_LEN) {
5892 pr_info("parent chain is too long (%d)\n", depth);
5893 ret = -EINVAL;
5894 goto out_err;
5895 }
5896
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02005897 parent = __rbd_dev_create(rbd_dev->rbd_client, rbd_dev->parent_spec);
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005898 if (!parent) {
5899 ret = -ENOMEM;
Alex Elder124afba2013-04-26 15:44:36 -05005900 goto out_err;
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005901 }
5902
5903 /*
5904 * Images related by parent/child relationships always share
5905 * rbd_client and spec/parent_spec, so bump their refcounts.
5906 */
5907 __rbd_get_client(rbd_dev->rbd_client);
5908 rbd_spec_get(rbd_dev->parent_spec);
Alex Elder124afba2013-04-26 15:44:36 -05005909
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005910 ret = rbd_dev_image_probe(parent, depth);
Alex Elder124afba2013-04-26 15:44:36 -05005911 if (ret < 0)
5912 goto out_err;
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005913
Alex Elder124afba2013-04-26 15:44:36 -05005914 rbd_dev->parent = parent;
Alex Eldera2acd002013-05-08 22:50:04 -05005915 atomic_set(&rbd_dev->parent_ref, 1);
Alex Elder124afba2013-04-26 15:44:36 -05005916 return 0;
Alex Elder124afba2013-04-26 15:44:36 -05005917
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005918out_err:
5919 rbd_dev_unparent(rbd_dev);
Markus Elfring1761b222015-11-23 20:16:45 +01005920 rbd_dev_destroy(parent);
Alex Elder124afba2013-04-26 15:44:36 -05005921 return ret;
5922}
5923
Ilya Dryomov811c6682016-04-15 16:22:16 +02005924/*
5925 * rbd_dev->header_rwsem must be locked for write and will be unlocked
5926 * upon return.
5927 */
Alex Elder200a6a82013-04-28 23:32:34 -05005928static int rbd_dev_device_setup(struct rbd_device *rbd_dev)
Alex Elder124afba2013-04-26 15:44:36 -05005929{
Alex Elder83a06262012-10-30 15:47:17 -05005930 int ret;
Alex Elder83a06262012-10-30 15:47:17 -05005931
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005932 /* Record our major and minor device numbers. */
Alex Elder83a06262012-10-30 15:47:17 -05005933
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005934 if (!single_major) {
5935 ret = register_blkdev(0, rbd_dev->name);
5936 if (ret < 0)
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02005937 goto err_out_unlock;
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005938
5939 rbd_dev->major = ret;
5940 rbd_dev->minor = 0;
5941 } else {
5942 rbd_dev->major = rbd_major;
5943 rbd_dev->minor = rbd_dev_id_to_minor(rbd_dev->dev_id);
5944 }
Alex Elder83a06262012-10-30 15:47:17 -05005945
5946 /* Set up the blkdev mapping. */
5947
5948 ret = rbd_init_disk(rbd_dev);
5949 if (ret)
5950 goto err_out_blkdev;
5951
Alex Elderf35a4de2013-05-06 09:51:29 -05005952 ret = rbd_dev_mapping_set(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05005953 if (ret)
5954 goto err_out_disk;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04005955
Alex Elderf35a4de2013-05-06 09:51:29 -05005956 set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
Josh Durgin22001f62013-09-30 20:10:04 -07005957 set_disk_ro(rbd_dev->disk, rbd_dev->mapping.read_only);
Alex Elderf35a4de2013-05-06 09:51:29 -05005958
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02005959 dev_set_name(&rbd_dev->dev, "%d", rbd_dev->dev_id);
5960 ret = device_add(&rbd_dev->dev);
Alex Elderf35a4de2013-05-06 09:51:29 -05005961 if (ret)
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04005962 goto err_out_mapping;
Alex Elder83a06262012-10-30 15:47:17 -05005963
Alex Elder83a06262012-10-30 15:47:17 -05005964 /* Everything's ready. Announce the disk to the world. */
5965
Alex Elder129b79d2013-04-26 15:44:36 -05005966 set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
Ilya Dryomov811c6682016-04-15 16:22:16 +02005967 up_write(&rbd_dev->header_rwsem);
Alex Elder83a06262012-10-30 15:47:17 -05005968
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02005969 spin_lock(&rbd_dev_list_lock);
5970 list_add_tail(&rbd_dev->node, &rbd_dev_list);
5971 spin_unlock(&rbd_dev_list_lock);
5972
Ilya Dryomov811c6682016-04-15 16:22:16 +02005973 add_disk(rbd_dev->disk);
Ilya Dryomovca7909e2016-08-18 18:38:41 +02005974 pr_info("%s: capacity %llu features 0x%llx\n", rbd_dev->disk->disk_name,
5975 (unsigned long long)get_capacity(rbd_dev->disk) << SECTOR_SHIFT,
5976 rbd_dev->header.features);
Alex Elder83a06262012-10-30 15:47:17 -05005977
5978 return ret;
Alex Elder2f82ee52012-10-30 19:40:33 -05005979
Alex Elderf35a4de2013-05-06 09:51:29 -05005980err_out_mapping:
5981 rbd_dev_mapping_clear(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05005982err_out_disk:
5983 rbd_free_disk(rbd_dev);
5984err_out_blkdev:
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005985 if (!single_major)
5986 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Ilya Dryomov811c6682016-04-15 16:22:16 +02005987err_out_unlock:
5988 up_write(&rbd_dev->header_rwsem);
Alex Elder83a06262012-10-30 15:47:17 -05005989 return ret;
5990}
5991
Alex Elder332bb122013-04-27 09:59:30 -05005992static int rbd_dev_header_name(struct rbd_device *rbd_dev)
5993{
5994 struct rbd_spec *spec = rbd_dev->spec;
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005995 int ret;
Alex Elder332bb122013-04-27 09:59:30 -05005996
5997 /* Record the header object name for this rbd image. */
5998
5999 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
Alex Elder332bb122013-04-27 09:59:30 -05006000 if (rbd_dev->image_format == 1)
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006001 ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s",
6002 spec->image_name, RBD_SUFFIX);
Alex Elder332bb122013-04-27 09:59:30 -05006003 else
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006004 ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s",
6005 RBD_HEADER_PREFIX, spec->image_id);
Alex Elder332bb122013-04-27 09:59:30 -05006006
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006007 return ret;
Alex Elder332bb122013-04-27 09:59:30 -05006008}
6009
Alex Elder200a6a82013-04-28 23:32:34 -05006010static void rbd_dev_image_release(struct rbd_device *rbd_dev)
6011{
Alex Elder6fd48b32013-04-28 23:32:34 -05006012 rbd_dev_unprobe(rbd_dev);
Alex Elder6fd48b32013-04-28 23:32:34 -05006013 rbd_dev->image_format = 0;
6014 kfree(rbd_dev->spec->image_id);
6015 rbd_dev->spec->image_id = NULL;
6016
Alex Elder200a6a82013-04-28 23:32:34 -05006017 rbd_dev_destroy(rbd_dev);
6018}
6019
Alex Eldera30b71b2012-07-10 20:30:11 -05006020/*
6021 * Probe for the existence of the header object for the given rbd
Alex Elder1f3ef782013-05-06 17:40:33 -05006022 * device. If this image is the one being mapped (i.e., not a
6023 * parent), initiate a watch on its header object before using that
6024 * object to get detailed information about the rbd image.
Alex Eldera30b71b2012-07-10 20:30:11 -05006025 */
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006026static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth)
Alex Eldera30b71b2012-07-10 20:30:11 -05006027{
6028 int ret;
6029
6030 /*
Alex Elder3abef3b2013-05-13 20:35:37 -05006031 * Get the id from the image id object. Unless there's an
6032 * error, rbd_dev->spec->image_id will be filled in with
6033 * a dynamically-allocated string, and rbd_dev->image_format
6034 * will be set to either 1 or 2.
Alex Eldera30b71b2012-07-10 20:30:11 -05006035 */
6036 ret = rbd_dev_image_id(rbd_dev);
6037 if (ret)
Alex Elderc0fba362013-04-25 23:15:08 -05006038 return ret;
Alex Elderc0fba362013-04-25 23:15:08 -05006039
Alex Elder332bb122013-04-27 09:59:30 -05006040 ret = rbd_dev_header_name(rbd_dev);
6041 if (ret)
6042 goto err_out_format;
6043
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006044 if (!depth) {
Ilya Dryomov99d16942016-08-12 16:11:41 +02006045 ret = rbd_register_watch(rbd_dev);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006046 if (ret) {
6047 if (ret == -ENOENT)
6048 pr_info("image %s/%s does not exist\n",
6049 rbd_dev->spec->pool_name,
6050 rbd_dev->spec->image_name);
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006051 goto err_out_format;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006052 }
Alex Elder1f3ef782013-05-06 17:40:33 -05006053 }
Alex Elderb644de22013-04-27 09:59:31 -05006054
Ilya Dryomova720ae02014-07-23 17:11:19 +04006055 ret = rbd_dev_header_info(rbd_dev);
Alex Elder5655c4d2013-04-25 23:15:08 -05006056 if (ret)
Alex Elderb644de22013-04-27 09:59:31 -05006057 goto err_out_watch;
Alex Elder83a06262012-10-30 15:47:17 -05006058
Ilya Dryomov04077592014-07-23 17:11:20 +04006059 /*
6060 * If this image is the one being mapped, we have pool name and
6061 * id, image name and id, and snap name - need to fill snap id.
6062 * Otherwise this is a parent image, identified by pool, image
6063 * and snap ids - need to fill in names for those ids.
6064 */
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006065 if (!depth)
Ilya Dryomov04077592014-07-23 17:11:20 +04006066 ret = rbd_spec_fill_snap_id(rbd_dev);
6067 else
6068 ret = rbd_spec_fill_names(rbd_dev);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006069 if (ret) {
6070 if (ret == -ENOENT)
6071 pr_info("snap %s/%s@%s does not exist\n",
6072 rbd_dev->spec->pool_name,
6073 rbd_dev->spec->image_name,
6074 rbd_dev->spec->snap_name);
Alex Elder33dca392013-04-30 00:44:33 -05006075 goto err_out_probe;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006076 }
Alex Elder9bb81c92013-04-27 09:59:30 -05006077
Ilya Dryomove8f59b52014-07-24 10:42:13 +04006078 if (rbd_dev->header.features & RBD_FEATURE_LAYERING) {
6079 ret = rbd_dev_v2_parent_info(rbd_dev);
6080 if (ret)
6081 goto err_out_probe;
6082
6083 /*
6084 * Need to warn users if this image is the one being
6085 * mapped and has a parent.
6086 */
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006087 if (!depth && rbd_dev->parent_spec)
Ilya Dryomove8f59b52014-07-24 10:42:13 +04006088 rbd_warn(rbd_dev,
6089 "WARNING: kernel layering is EXPERIMENTAL!");
6090 }
6091
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006092 ret = rbd_dev_probe_parent(rbd_dev, depth);
Alex Elder30d60ba2013-05-06 09:51:30 -05006093 if (ret)
6094 goto err_out_probe;
Alex Elder83a06262012-10-30 15:47:17 -05006095
Alex Elder30d60ba2013-05-06 09:51:30 -05006096 dout("discovered format %u image, header name is %s\n",
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006097 rbd_dev->image_format, rbd_dev->header_oid.name);
Alex Elder30d60ba2013-05-06 09:51:30 -05006098 return 0;
Ilya Dryomove8f59b52014-07-24 10:42:13 +04006099
Alex Elder6fd48b32013-04-28 23:32:34 -05006100err_out_probe:
6101 rbd_dev_unprobe(rbd_dev);
Alex Elderb644de22013-04-27 09:59:31 -05006102err_out_watch:
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006103 if (!depth)
Ilya Dryomov99d16942016-08-12 16:11:41 +02006104 rbd_unregister_watch(rbd_dev);
Alex Elder332bb122013-04-27 09:59:30 -05006105err_out_format:
6106 rbd_dev->image_format = 0;
Alex Elder5655c4d2013-04-25 23:15:08 -05006107 kfree(rbd_dev->spec->image_id);
6108 rbd_dev->spec->image_id = NULL;
Alex Elder5655c4d2013-04-25 23:15:08 -05006109 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05006110}
6111
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006112static ssize_t do_rbd_add(struct bus_type *bus,
6113 const char *buf,
6114 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006115{
Alex Eldercb8627c2012-07-09 21:04:23 -05006116 struct rbd_device *rbd_dev = NULL;
Alex Elderdc79b112012-10-25 23:34:41 -05006117 struct ceph_options *ceph_opts = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05006118 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05006119 struct rbd_spec *spec = NULL;
Alex Elder9d3997f2012-10-25 23:34:42 -05006120 struct rbd_client *rbdc;
Alex Elder51344a32013-05-06 07:40:30 -05006121 bool read_only;
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02006122 int rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006123
6124 if (!try_module_get(THIS_MODULE))
6125 return -ENODEV;
6126
Alex Eldera725f65e2012-02-02 08:13:30 -06006127 /* parse add command */
Alex Elder859c31d2012-10-25 23:34:42 -05006128 rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec);
Alex Elderdc79b112012-10-25 23:34:41 -05006129 if (rc < 0)
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006130 goto out;
Alex Eldera725f65e2012-02-02 08:13:30 -06006131
Alex Elder9d3997f2012-10-25 23:34:42 -05006132 rbdc = rbd_get_client(ceph_opts);
6133 if (IS_ERR(rbdc)) {
6134 rc = PTR_ERR(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05006135 goto err_out_args;
Alex Elder9d3997f2012-10-25 23:34:42 -05006136 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006137
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006138 /* pick the pool */
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04006139 rc = rbd_add_get_pool_id(rbdc, spec->pool_name);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006140 if (rc < 0) {
6141 if (rc == -ENOENT)
6142 pr_info("pool %s does not exist\n", spec->pool_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006143 goto err_out_client;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006144 }
Alex Elderc0cd10db2013-04-26 09:43:47 -05006145 spec->pool_id = (u64)rc;
Alex Elder859c31d2012-10-25 23:34:42 -05006146
Ilya Dryomovd1475432015-06-22 13:24:48 +03006147 rbd_dev = rbd_dev_create(rbdc, spec, rbd_opts);
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02006148 if (!rbd_dev) {
6149 rc = -ENOMEM;
Alex Elderbd4ba652012-10-25 23:34:42 -05006150 goto err_out_client;
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02006151 }
Alex Elderc53d5892012-10-25 23:34:42 -05006152 rbdc = NULL; /* rbd_dev now owns this */
6153 spec = NULL; /* rbd_dev now owns this */
Ilya Dryomovd1475432015-06-22 13:24:48 +03006154 rbd_opts = NULL; /* rbd_dev now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006155
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02006156 rbd_dev->config_info = kstrdup(buf, GFP_KERNEL);
6157 if (!rbd_dev->config_info) {
6158 rc = -ENOMEM;
6159 goto err_out_rbd_dev;
6160 }
6161
Ilya Dryomov811c6682016-04-15 16:22:16 +02006162 down_write(&rbd_dev->header_rwsem);
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006163 rc = rbd_dev_image_probe(rbd_dev, 0);
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02006164 if (rc < 0) {
6165 up_write(&rbd_dev->header_rwsem);
Alex Elderc53d5892012-10-25 23:34:42 -05006166 goto err_out_rbd_dev;
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02006167 }
Alex Elder05fd6f62012-08-29 17:11:07 -05006168
Alex Elder7ce4eef2013-05-06 17:40:33 -05006169 /* If we are mapping a snapshot it must be marked read-only */
6170
Ilya Dryomovd1475432015-06-22 13:24:48 +03006171 read_only = rbd_dev->opts->read_only;
Alex Elder7ce4eef2013-05-06 17:40:33 -05006172 if (rbd_dev->spec->snap_id != CEPH_NOSNAP)
6173 read_only = true;
6174 rbd_dev->mapping.read_only = read_only;
6175
Alex Elderb536f692013-04-28 23:32:34 -05006176 rc = rbd_dev_device_setup(rbd_dev);
Alex Elder3abef3b2013-05-13 20:35:37 -05006177 if (rc) {
Ilya Dryomove37180c2013-12-16 18:02:41 +02006178 /*
Ilya Dryomov99d16942016-08-12 16:11:41 +02006179 * rbd_unregister_watch() can't be moved into
Ilya Dryomove37180c2013-12-16 18:02:41 +02006180 * rbd_dev_image_release() without refactoring, see
6181 * commit 1f3ef78861ac.
6182 */
Ilya Dryomov99d16942016-08-12 16:11:41 +02006183 rbd_unregister_watch(rbd_dev);
Alex Elder3abef3b2013-05-13 20:35:37 -05006184 rbd_dev_image_release(rbd_dev);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006185 goto out;
Alex Elder3abef3b2013-05-13 20:35:37 -05006186 }
Alex Elderb536f692013-04-28 23:32:34 -05006187
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006188 rc = count;
6189out:
6190 module_put(THIS_MODULE);
6191 return rc;
Alex Elder3abef3b2013-05-13 20:35:37 -05006192
Alex Elderc53d5892012-10-25 23:34:42 -05006193err_out_rbd_dev:
6194 rbd_dev_destroy(rbd_dev);
Alex Elderbd4ba652012-10-25 23:34:42 -05006195err_out_client:
Alex Elder9d3997f2012-10-25 23:34:42 -05006196 rbd_put_client(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05006197err_out_args:
Alex Elder859c31d2012-10-25 23:34:42 -05006198 rbd_spec_put(spec);
Ilya Dryomovd1475432015-06-22 13:24:48 +03006199 kfree(rbd_opts);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006200 goto out;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006201}
6202
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006203static ssize_t rbd_add(struct bus_type *bus,
6204 const char *buf,
6205 size_t count)
6206{
6207 if (single_major)
6208 return -EINVAL;
6209
6210 return do_rbd_add(bus, buf, count);
6211}
6212
6213static ssize_t rbd_add_single_major(struct bus_type *bus,
6214 const char *buf,
6215 size_t count)
6216{
6217 return do_rbd_add(bus, buf, count);
6218}
6219
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006220static void rbd_dev_device_release(struct rbd_device *rbd_dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006221{
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006222 rbd_free_disk(rbd_dev);
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02006223
6224 spin_lock(&rbd_dev_list_lock);
6225 list_del_init(&rbd_dev->node);
6226 spin_unlock(&rbd_dev_list_lock);
6227
Alex Elder200a6a82013-04-28 23:32:34 -05006228 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006229 device_del(&rbd_dev->dev);
Alex Elder6d80b132013-05-06 07:40:30 -05006230 rbd_dev_mapping_clear(rbd_dev);
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006231 if (!single_major)
6232 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006233}
6234
Alex Elder05a46af2013-04-26 15:44:36 -05006235static void rbd_dev_remove_parent(struct rbd_device *rbd_dev)
6236{
Alex Elderad945fc2013-04-26 15:44:36 -05006237 while (rbd_dev->parent) {
Alex Elder05a46af2013-04-26 15:44:36 -05006238 struct rbd_device *first = rbd_dev;
6239 struct rbd_device *second = first->parent;
6240 struct rbd_device *third;
6241
6242 /*
6243 * Follow to the parent with no grandparent and
6244 * remove it.
6245 */
6246 while (second && (third = second->parent)) {
6247 first = second;
6248 second = third;
6249 }
Alex Elderad945fc2013-04-26 15:44:36 -05006250 rbd_assert(second);
Alex Elder8ad42cd2013-04-28 23:32:34 -05006251 rbd_dev_image_release(second);
Alex Elderad945fc2013-04-26 15:44:36 -05006252 first->parent = NULL;
6253 first->parent_overlap = 0;
6254
6255 rbd_assert(first->parent_spec);
Alex Elder05a46af2013-04-26 15:44:36 -05006256 rbd_spec_put(first->parent_spec);
6257 first->parent_spec = NULL;
Alex Elder05a46af2013-04-26 15:44:36 -05006258 }
6259}
6260
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006261static ssize_t do_rbd_remove(struct bus_type *bus,
6262 const char *buf,
6263 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006264{
6265 struct rbd_device *rbd_dev = NULL;
Alex Elder751cc0e2013-05-31 15:17:01 -05006266 struct list_head *tmp;
6267 int dev_id;
Mike Christie0276dca2016-08-18 18:38:45 +02006268 char opt_buf[6];
Alex Elder82a442d2013-05-31 17:40:44 -05006269 bool already = false;
Mike Christie0276dca2016-08-18 18:38:45 +02006270 bool force = false;
Alex Elder0d8189e2013-04-27 09:59:30 -05006271 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006272
Mike Christie0276dca2016-08-18 18:38:45 +02006273 dev_id = -1;
6274 opt_buf[0] = '\0';
6275 sscanf(buf, "%d %5s", &dev_id, opt_buf);
6276 if (dev_id < 0) {
6277 pr_err("dev_id out of range\n");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006278 return -EINVAL;
Mike Christie0276dca2016-08-18 18:38:45 +02006279 }
6280 if (opt_buf[0] != '\0') {
6281 if (!strcmp(opt_buf, "force")) {
6282 force = true;
6283 } else {
6284 pr_err("bad remove option at '%s'\n", opt_buf);
6285 return -EINVAL;
6286 }
6287 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006288
Alex Elder751cc0e2013-05-31 15:17:01 -05006289 ret = -ENOENT;
6290 spin_lock(&rbd_dev_list_lock);
6291 list_for_each(tmp, &rbd_dev_list) {
6292 rbd_dev = list_entry(tmp, struct rbd_device, node);
6293 if (rbd_dev->dev_id == dev_id) {
6294 ret = 0;
6295 break;
6296 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006297 }
Alex Elder751cc0e2013-05-31 15:17:01 -05006298 if (!ret) {
6299 spin_lock_irq(&rbd_dev->lock);
Mike Christie0276dca2016-08-18 18:38:45 +02006300 if (rbd_dev->open_count && !force)
Alex Elder751cc0e2013-05-31 15:17:01 -05006301 ret = -EBUSY;
6302 else
Alex Elder82a442d2013-05-31 17:40:44 -05006303 already = test_and_set_bit(RBD_DEV_FLAG_REMOVING,
6304 &rbd_dev->flags);
Alex Elder751cc0e2013-05-31 15:17:01 -05006305 spin_unlock_irq(&rbd_dev->lock);
6306 }
6307 spin_unlock(&rbd_dev_list_lock);
Alex Elder82a442d2013-05-31 17:40:44 -05006308 if (ret < 0 || already)
Alex Elder1ba0f1e2013-05-31 15:17:01 -05006309 return ret;
Alex Elder751cc0e2013-05-31 15:17:01 -05006310
Mike Christie0276dca2016-08-18 18:38:45 +02006311 if (force) {
6312 /*
6313 * Prevent new IO from being queued and wait for existing
6314 * IO to complete/fail.
6315 */
6316 blk_mq_freeze_queue(rbd_dev->disk->queue);
6317 blk_set_queue_dying(rbd_dev->disk->queue);
6318 }
6319
Ilya Dryomoved95b212016-08-12 16:40:02 +02006320 down_write(&rbd_dev->lock_rwsem);
6321 if (__rbd_is_lock_owner(rbd_dev))
6322 rbd_unlock(rbd_dev);
6323 up_write(&rbd_dev->lock_rwsem);
Ilya Dryomov99d16942016-08-12 16:11:41 +02006324 rbd_unregister_watch(rbd_dev);
Ilya Dryomovfca27062013-12-16 18:02:40 +02006325
Josh Durgin98752012013-08-29 17:26:31 -07006326 /*
6327 * Don't free anything from rbd_dev->disk until after all
6328 * notifies are completely processed. Otherwise
6329 * rbd_bus_del_dev() will race with rbd_watch_cb(), resulting
6330 * in a potential use after free of rbd_dev->disk or rbd_dev.
6331 */
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006332 rbd_dev_device_release(rbd_dev);
Alex Elder8ad42cd2013-04-28 23:32:34 -05006333 rbd_dev_image_release(rbd_dev);
Alex Elderaafb2302012-09-06 16:00:54 -05006334
Alex Elder1ba0f1e2013-05-31 15:17:01 -05006335 return count;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006336}
6337
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006338static ssize_t rbd_remove(struct bus_type *bus,
6339 const char *buf,
6340 size_t count)
6341{
6342 if (single_major)
6343 return -EINVAL;
6344
6345 return do_rbd_remove(bus, buf, count);
6346}
6347
6348static ssize_t rbd_remove_single_major(struct bus_type *bus,
6349 const char *buf,
6350 size_t count)
6351{
6352 return do_rbd_remove(bus, buf, count);
6353}
6354
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006355/*
6356 * create control files in sysfs
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006357 * /sys/bus/rbd/...
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006358 */
6359static int rbd_sysfs_init(void)
6360{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006361 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006362
Alex Elderfed4c142012-02-07 12:03:36 -06006363 ret = device_register(&rbd_root_dev);
Alex Elder21079782012-01-24 10:08:36 -06006364 if (ret < 0)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006365 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006366
Alex Elderfed4c142012-02-07 12:03:36 -06006367 ret = bus_register(&rbd_bus_type);
6368 if (ret < 0)
6369 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006370
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006371 return ret;
6372}
6373
6374static void rbd_sysfs_cleanup(void)
6375{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006376 bus_unregister(&rbd_bus_type);
Alex Elderfed4c142012-02-07 12:03:36 -06006377 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006378}
6379
Alex Elder1c2a9df2013-05-01 12:43:03 -05006380static int rbd_slab_init(void)
6381{
6382 rbd_assert(!rbd_img_request_cache);
Geliang Tang03d94402016-03-13 15:17:32 +08006383 rbd_img_request_cache = KMEM_CACHE(rbd_img_request, 0);
Alex Elder868311b2013-05-01 12:43:03 -05006384 if (!rbd_img_request_cache)
6385 return -ENOMEM;
6386
6387 rbd_assert(!rbd_obj_request_cache);
Geliang Tang03d94402016-03-13 15:17:32 +08006388 rbd_obj_request_cache = KMEM_CACHE(rbd_obj_request, 0);
Alex Elder78c2a442013-05-01 12:43:04 -05006389 if (!rbd_obj_request_cache)
6390 goto out_err;
6391
6392 rbd_assert(!rbd_segment_name_cache);
6393 rbd_segment_name_cache = kmem_cache_create("rbd_segment_name",
Ilya Dryomov2d0ebc52014-01-27 17:40:18 +02006394 CEPH_MAX_OID_NAME_LEN + 1, 1, 0, NULL);
Alex Elder78c2a442013-05-01 12:43:04 -05006395 if (rbd_segment_name_cache)
Alex Elder1c2a9df2013-05-01 12:43:03 -05006396 return 0;
Alex Elder78c2a442013-05-01 12:43:04 -05006397out_err:
Julia Lawall13bf2832015-09-13 14:15:26 +02006398 kmem_cache_destroy(rbd_obj_request_cache);
6399 rbd_obj_request_cache = NULL;
Alex Elder1c2a9df2013-05-01 12:43:03 -05006400
Alex Elder868311b2013-05-01 12:43:03 -05006401 kmem_cache_destroy(rbd_img_request_cache);
6402 rbd_img_request_cache = NULL;
6403
Alex Elder1c2a9df2013-05-01 12:43:03 -05006404 return -ENOMEM;
6405}
6406
6407static void rbd_slab_exit(void)
6408{
Alex Elder78c2a442013-05-01 12:43:04 -05006409 rbd_assert(rbd_segment_name_cache);
6410 kmem_cache_destroy(rbd_segment_name_cache);
6411 rbd_segment_name_cache = NULL;
6412
Alex Elder868311b2013-05-01 12:43:03 -05006413 rbd_assert(rbd_obj_request_cache);
6414 kmem_cache_destroy(rbd_obj_request_cache);
6415 rbd_obj_request_cache = NULL;
6416
Alex Elder1c2a9df2013-05-01 12:43:03 -05006417 rbd_assert(rbd_img_request_cache);
6418 kmem_cache_destroy(rbd_img_request_cache);
6419 rbd_img_request_cache = NULL;
6420}
6421
Alex Eldercc344fa2013-02-19 12:25:56 -06006422static int __init rbd_init(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006423{
6424 int rc;
6425
Alex Elder1e32d342013-01-30 11:13:33 -06006426 if (!libceph_compatible(NULL)) {
6427 rbd_warn(NULL, "libceph incompatibility (quitting)");
Alex Elder1e32d342013-01-30 11:13:33 -06006428 return -EINVAL;
6429 }
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006430
Alex Elder1c2a9df2013-05-01 12:43:03 -05006431 rc = rbd_slab_init();
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006432 if (rc)
6433 return rc;
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006434
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006435 /*
6436 * The number of active work items is limited by the number of
Ilya Dryomovf77303b2015-04-22 18:28:13 +03006437 * rbd devices * queue depth, so leave @max_active at default.
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006438 */
6439 rbd_wq = alloc_workqueue(RBD_DRV_NAME, WQ_MEM_RECLAIM, 0);
6440 if (!rbd_wq) {
6441 rc = -ENOMEM;
6442 goto err_out_slab;
6443 }
6444
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006445 if (single_major) {
6446 rbd_major = register_blkdev(0, RBD_DRV_NAME);
6447 if (rbd_major < 0) {
6448 rc = rbd_major;
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006449 goto err_out_wq;
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006450 }
6451 }
6452
Alex Elder1c2a9df2013-05-01 12:43:03 -05006453 rc = rbd_sysfs_init();
6454 if (rc)
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006455 goto err_out_blkdev;
Alex Elder1c2a9df2013-05-01 12:43:03 -05006456
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006457 if (single_major)
6458 pr_info("loaded (major %d)\n", rbd_major);
6459 else
6460 pr_info("loaded\n");
6461
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006462 return 0;
6463
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006464err_out_blkdev:
6465 if (single_major)
6466 unregister_blkdev(rbd_major, RBD_DRV_NAME);
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006467err_out_wq:
6468 destroy_workqueue(rbd_wq);
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006469err_out_slab:
6470 rbd_slab_exit();
Alex Elder1c2a9df2013-05-01 12:43:03 -05006471 return rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006472}
6473
Alex Eldercc344fa2013-02-19 12:25:56 -06006474static void __exit rbd_exit(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006475{
Ilya Dryomovffe312c2014-05-20 15:46:04 +04006476 ida_destroy(&rbd_dev_id_ida);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006477 rbd_sysfs_cleanup();
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006478 if (single_major)
6479 unregister_blkdev(rbd_major, RBD_DRV_NAME);
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006480 destroy_workqueue(rbd_wq);
Alex Elder1c2a9df2013-05-01 12:43:03 -05006481 rbd_slab_exit();
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006482}
6483
6484module_init(rbd_init);
6485module_exit(rbd_exit);
6486
Alex Elderd552c612013-05-31 20:13:09 -05006487MODULE_AUTHOR("Alex Elder <elder@inktank.com>");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006488MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
6489MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006490/* following authorship retained from original osdblk.c */
6491MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
6492
Ilya Dryomov90da2582013-12-13 15:28:56 +02006493MODULE_DESCRIPTION("RADOS Block Device (RBD) driver");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006494MODULE_LICENSE("GPL");