blob: 8eaebf60961105f158437a4e90197142a67a9920 [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
Ilya Dryomov8767b292017-03-02 19:56:57 +0100123#define RBD_FEATURE_LAYERING (1ULL<<0)
124#define RBD_FEATURE_STRIPINGV2 (1ULL<<1)
125#define RBD_FEATURE_EXCLUSIVE_LOCK (1ULL<<2)
126#define RBD_FEATURE_DATA_POOL (1ULL<<7)
Ilya Dryomove5734272018-01-16 15:41:54 +0100127#define RBD_FEATURE_OPERATIONS (1ULL<<8)
Ilya Dryomov8767b292017-03-02 19:56:57 +0100128
Ilya Dryomoved95b212016-08-12 16:40:02 +0200129#define RBD_FEATURES_ALL (RBD_FEATURE_LAYERING | \
130 RBD_FEATURE_STRIPINGV2 | \
Ilya Dryomov7e973322017-01-25 18:16:22 +0100131 RBD_FEATURE_EXCLUSIVE_LOCK | \
Ilya Dryomove5734272018-01-16 15:41:54 +0100132 RBD_FEATURE_DATA_POOL | \
133 RBD_FEATURE_OPERATIONS)
Alex Elderd8891402012-10-09 13:50:17 -0700134
135/* Features supported by this (client software) implementation. */
136
Alex Elder770eba62012-10-25 23:34:40 -0500137#define RBD_FEATURES_SUPPORTED (RBD_FEATURES_ALL)
Alex Elderd8891402012-10-09 13:50:17 -0700138
Alex Elder81a89792012-02-02 08:13:30 -0600139/*
140 * An RBD device name will be "rbd#", where the "rbd" comes from
141 * RBD_DRV_NAME above, and # is a unique integer identifier.
Alex Elder81a89792012-02-02 08:13:30 -0600142 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700143#define DEV_NAME_LEN 32
144
145/*
146 * block device image metadata (in-memory version)
147 */
148struct rbd_image_header {
Alex Elderf35a4de2013-05-06 09:51:29 -0500149 /* These six fields never change for a given rbd image */
Alex Elder849b4262012-07-09 21:04:24 -0500150 char *object_prefix;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700151 __u8 obj_order;
Alex Elderf35a4de2013-05-06 09:51:29 -0500152 u64 stripe_unit;
153 u64 stripe_count;
Ilya Dryomov7e973322017-01-25 18:16:22 +0100154 s64 data_pool_id;
Alex Elderf35a4de2013-05-06 09:51:29 -0500155 u64 features; /* Might be changeable someday? */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700156
Alex Elderf84344f2012-08-31 17:29:51 -0500157 /* The remaining fields need to be updated occasionally */
158 u64 image_size;
159 struct ceph_snap_context *snapc;
Alex Elderf35a4de2013-05-06 09:51:29 -0500160 char *snap_names; /* format 1 only */
161 u64 *snap_sizes; /* format 1 only */
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700162};
163
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500164/*
165 * An rbd image specification.
166 *
167 * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely
Alex Elderc66c6e02012-11-01 08:39:26 -0500168 * identify an image. Each rbd_dev structure includes a pointer to
169 * an rbd_spec structure that encapsulates this identity.
170 *
171 * Each of the id's in an rbd_spec has an associated name. For a
172 * user-mapped image, the names are supplied and the id's associated
173 * with them are looked up. For a layered image, a parent image is
174 * defined by the tuple, and the names are looked up.
175 *
176 * An rbd_dev structure contains a parent_spec pointer which is
177 * non-null if the image it represents is a child in a layered
178 * image. This pointer will refer to the rbd_spec structure used
179 * by the parent rbd_dev for its own identity (i.e., the structure
180 * is shared between the parent and child).
181 *
182 * Since these structures are populated once, during the discovery
183 * phase of image construction, they are effectively immutable so
184 * we make no effort to synchronize access to them.
185 *
186 * Note that code herein does not assume the image name is known (it
187 * could be a null pointer).
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500188 */
189struct rbd_spec {
190 u64 pool_id;
Alex Elderecb4dc22013-04-26 09:43:47 -0500191 const char *pool_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500192
Alex Elderecb4dc22013-04-26 09:43:47 -0500193 const char *image_id;
194 const char *image_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500195
196 u64 snap_id;
Alex Elderecb4dc22013-04-26 09:43:47 -0500197 const char *snap_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500198
199 struct kref kref;
200};
201
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700202/*
Alex Elderf0f8cef2012-01-29 13:57:44 -0600203 * an instance of the client. multiple devices may share an rbd client.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700204 */
205struct rbd_client {
206 struct ceph_client *client;
207 struct kref kref;
208 struct list_head node;
209};
210
Alex Elderbf0d5f502012-11-22 00:00:08 -0600211struct rbd_img_request;
212typedef void (*rbd_img_callback_t)(struct rbd_img_request *);
213
214#define BAD_WHICH U32_MAX /* Good which or bad which, which? */
215
216struct rbd_obj_request;
217typedef void (*rbd_obj_callback_t)(struct rbd_obj_request *);
218
Alex Elder9969ebc2013-01-18 12:31:10 -0600219enum obj_request_type {
Ilya Dryomova1fbb5e2018-01-16 12:15:02 +0100220 OBJ_REQUEST_NODATA = 1,
Ilya Dryomov5359a172018-01-20 10:30:10 +0100221 OBJ_REQUEST_BIO, /* pointer into provided bio (list) */
Ilya Dryomova1fbb5e2018-01-16 12:15:02 +0100222 OBJ_REQUEST_PAGES,
Alex Elder9969ebc2013-01-18 12:31:10 -0600223};
Alex Elderbf0d5f502012-11-22 00:00:08 -0600224
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800225enum obj_operation_type {
Ilya Dryomova1fbb5e2018-01-16 12:15:02 +0100226 OBJ_OP_READ = 1,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800227 OBJ_OP_WRITE,
Guangliang Zhao90e98c52014-04-01 22:22:16 +0800228 OBJ_OP_DISCARD,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800229};
230
Alex Elder926f9b32013-02-11 12:33:24 -0600231enum obj_req_flags {
232 OBJ_REQ_DONE, /* completion flag: not done = 0, done = 1 */
Alex Elder6365d332013-02-11 12:33:24 -0600233 OBJ_REQ_IMG_DATA, /* object usage: standalone = 0, image = 1 */
Alex Elder5679c592013-02-11 12:33:24 -0600234 OBJ_REQ_KNOWN, /* EXISTS flag valid: no = 0, yes = 1 */
235 OBJ_REQ_EXISTS, /* target exists: no = 0, yes = 1 */
Alex Elder926f9b32013-02-11 12:33:24 -0600236};
237
Alex Elderbf0d5f502012-11-22 00:00:08 -0600238struct rbd_obj_request {
Ilya Dryomova90bb0c2017-01-25 18:16:23 +0100239 u64 object_no;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600240 u64 offset; /* object start byte */
241 u64 length; /* bytes from offset */
Alex Elder926f9b32013-02-11 12:33:24 -0600242 unsigned long flags;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600243
Alex Elderc5b5ef62013-02-11 12:33:24 -0600244 /*
245 * An object request associated with an image will have its
246 * img_data flag set; a standalone object request will not.
247 *
248 * A standalone object request will have which == BAD_WHICH
249 * and a null obj_request pointer.
250 *
251 * An object request initiated in support of a layered image
252 * object (to check for its existence before a write) will
253 * have which == BAD_WHICH and a non-null obj_request pointer.
254 *
255 * Finally, an object request for rbd image data will have
256 * which != BAD_WHICH, and will have a non-null img_request
257 * pointer. The value of which will be in the range
258 * 0..(img_request->obj_request_count-1).
259 */
260 union {
261 struct rbd_obj_request *obj_request; /* STAT op */
262 struct {
263 struct rbd_img_request *img_request;
264 u64 img_offset;
265 /* links for img_request->obj_requests list */
266 struct list_head links;
267 };
268 };
Alex Elderbf0d5f502012-11-22 00:00:08 -0600269 u32 which; /* posn image request list */
270
271 enum obj_request_type type;
Alex Elder788e2df2013-01-17 12:25:27 -0600272 union {
Ilya Dryomov5359a172018-01-20 10:30:10 +0100273 struct ceph_bio_iter bio_pos;
Alex Elder788e2df2013-01-17 12:25:27 -0600274 struct {
275 struct page **pages;
276 u32 page_count;
277 };
278 };
Alex Elder0eefd472013-04-19 15:34:50 -0500279 struct page **copyup_pages;
Alex Elderebda6402013-05-10 16:29:22 -0500280 u32 copyup_page_count;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600281
282 struct ceph_osd_request *osd_req;
283
284 u64 xferred; /* bytes transferred */
Sage Weil1b83bef2013-02-25 16:11:12 -0800285 int result;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600286
287 rbd_obj_callback_t callback;
288
289 struct kref kref;
290};
291
Alex Elder0c425242013-02-08 09:55:49 -0600292enum img_req_flags {
Alex Elder9849e982013-01-24 16:13:36 -0600293 IMG_REQ_WRITE, /* I/O direction: read = 0, write = 1 */
294 IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */
Alex Elderd0b2e942013-01-24 16:13:36 -0600295 IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */
Guangliang Zhao90e98c52014-04-01 22:22:16 +0800296 IMG_REQ_DISCARD, /* discard: normal = 0, discard request = 1 */
Alex Elder0c425242013-02-08 09:55:49 -0600297};
298
Alex Elderbf0d5f502012-11-22 00:00:08 -0600299struct rbd_img_request {
Alex Elderbf0d5f502012-11-22 00:00:08 -0600300 struct rbd_device *rbd_dev;
301 u64 offset; /* starting image byte offset */
302 u64 length; /* byte count from offset */
Alex Elder0c425242013-02-08 09:55:49 -0600303 unsigned long flags;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600304 union {
Alex Elder9849e982013-01-24 16:13:36 -0600305 u64 snap_id; /* for reads */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600306 struct ceph_snap_context *snapc; /* for writes */
Alex Elder9849e982013-01-24 16:13:36 -0600307 };
308 union {
309 struct request *rq; /* block request */
310 struct rbd_obj_request *obj_request; /* obj req initiator */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600311 };
Alex Elder3d7efd12013-04-19 15:34:50 -0500312 struct page **copyup_pages;
Alex Elderebda6402013-05-10 16:29:22 -0500313 u32 copyup_page_count;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600314 spinlock_t completion_lock;/* protects next_completion */
315 u32 next_completion;
316 rbd_img_callback_t callback;
Alex Elder55f27e02013-04-10 12:34:25 -0500317 u64 xferred;/* aggregate bytes transferred */
Alex Eldera5a337d2013-01-24 16:13:36 -0600318 int result; /* first nonzero obj_request result */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600319
320 u32 obj_request_count;
321 struct list_head obj_requests; /* rbd_obj_request structs */
322
323 struct kref kref;
324};
325
326#define for_each_obj_request(ireq, oreq) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600327 list_for_each_entry(oreq, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600328#define for_each_obj_request_from(ireq, oreq) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600329 list_for_each_entry_from(oreq, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600330#define for_each_obj_request_safe(ireq, oreq, n) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600331 list_for_each_entry_safe_reverse(oreq, n, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600332
Ilya Dryomov99d16942016-08-12 16:11:41 +0200333enum rbd_watch_state {
334 RBD_WATCH_STATE_UNREGISTERED,
335 RBD_WATCH_STATE_REGISTERED,
336 RBD_WATCH_STATE_ERROR,
337};
338
Ilya Dryomoved95b212016-08-12 16:40:02 +0200339enum rbd_lock_state {
340 RBD_LOCK_STATE_UNLOCKED,
341 RBD_LOCK_STATE_LOCKED,
342 RBD_LOCK_STATE_RELEASING,
343};
344
345/* WatchNotify::ClientId */
346struct rbd_client_id {
347 u64 gid;
348 u64 handle;
349};
350
Alex Elderf84344f2012-08-31 17:29:51 -0500351struct rbd_mapping {
Alex Elder99c1f082012-08-30 14:42:15 -0500352 u64 size;
Alex Elder34b13182012-07-13 20:35:12 -0500353 u64 features;
Alex Elderf84344f2012-08-31 17:29:51 -0500354};
355
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700356/*
357 * a single device
358 */
359struct rbd_device {
Alex Elderde71a292012-07-03 16:01:19 -0500360 int dev_id; /* blkdev unique id */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700361
362 int major; /* blkdev assigned major */
Ilya Dryomovdd82fff2013-12-13 15:28:57 +0200363 int minor;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700364 struct gendisk *disk; /* blkdev's gendisk and rq */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700365
Alex Eldera30b71b2012-07-10 20:30:11 -0500366 u32 image_format; /* Either 1 or 2 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700367 struct rbd_client *rbd_client;
368
369 char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */
370
Alex Elderb82d1672013-01-14 12:43:31 -0600371 spinlock_t lock; /* queue, flags, open_count */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700372
373 struct rbd_image_header header;
Alex Elderb82d1672013-01-14 12:43:31 -0600374 unsigned long flags; /* possibly lock protected */
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500375 struct rbd_spec *spec;
Ilya Dryomovd1475432015-06-22 13:24:48 +0300376 struct rbd_options *opts;
Mike Christie0d6d1e9c2016-08-18 18:38:45 +0200377 char *config_info; /* add{,_single_major} string */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700378
Ilya Dryomovc41d13a2016-04-29 20:01:25 +0200379 struct ceph_object_id header_oid;
Ilya Dryomov922dab62016-05-26 01:15:02 +0200380 struct ceph_object_locator header_oloc;
Alex Elder971f8392012-10-25 23:34:41 -0500381
Ilya Dryomov1643dfa2016-08-12 15:45:52 +0200382 struct ceph_file_layout layout; /* used for all rbd requests */
Alex Elder0903e872012-11-14 12:25:19 -0600383
Ilya Dryomov99d16942016-08-12 16:11:41 +0200384 struct mutex watch_mutex;
385 enum rbd_watch_state watch_state;
Ilya Dryomov922dab62016-05-26 01:15:02 +0200386 struct ceph_osd_linger_request *watch_handle;
Ilya Dryomov99d16942016-08-12 16:11:41 +0200387 u64 watch_cookie;
388 struct delayed_work watch_dwork;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700389
Ilya Dryomoved95b212016-08-12 16:40:02 +0200390 struct rw_semaphore lock_rwsem;
391 enum rbd_lock_state lock_state;
Ilya Dryomovcbbfb0f2017-04-13 12:17:38 +0200392 char lock_cookie[32];
Ilya Dryomoved95b212016-08-12 16:40:02 +0200393 struct rbd_client_id owner_cid;
394 struct work_struct acquired_lock_work;
395 struct work_struct released_lock_work;
396 struct delayed_work lock_dwork;
397 struct work_struct unlock_work;
398 wait_queue_head_t lock_waitq;
399
Ilya Dryomov1643dfa2016-08-12 15:45:52 +0200400 struct workqueue_struct *task_wq;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700401
Alex Elder86b00e02012-10-25 23:34:42 -0500402 struct rbd_spec *parent_spec;
403 u64 parent_overlap;
Alex Eldera2acd002013-05-08 22:50:04 -0500404 atomic_t parent_ref;
Alex Elder2f82ee52012-10-30 19:40:33 -0500405 struct rbd_device *parent;
Alex Elder86b00e02012-10-25 23:34:42 -0500406
Christoph Hellwig7ad18af2015-01-13 17:20:04 +0100407 /* Block layer tags. */
408 struct blk_mq_tag_set tag_set;
409
Josh Durginc6666012011-11-21 17:11:12 -0800410 /* protects updating the header */
411 struct rw_semaphore header_rwsem;
Alex Elderf84344f2012-08-31 17:29:51 -0500412
413 struct rbd_mapping mapping;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700414
415 struct list_head node;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800416
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800417 /* sysfs related */
418 struct device dev;
Alex Elderb82d1672013-01-14 12:43:31 -0600419 unsigned long open_count; /* protected by lock */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800420};
421
Alex Elderb82d1672013-01-14 12:43:31 -0600422/*
Ilya Dryomov87c0fde2016-09-29 13:41:05 +0200423 * Flag bits for rbd_dev->flags:
424 * - REMOVING (which is coupled with rbd_dev->open_count) is protected
425 * by rbd_dev->lock
426 * - BLACKLISTED is protected by rbd_dev->lock_rwsem
Alex Elderb82d1672013-01-14 12:43:31 -0600427 */
Alex Elder6d292902013-01-14 12:43:31 -0600428enum rbd_dev_flags {
429 RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */
Alex Elderb82d1672013-01-14 12:43:31 -0600430 RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */
Ilya Dryomov87c0fde2016-09-29 13:41:05 +0200431 RBD_DEV_FLAG_BLACKLISTED, /* our ceph_client is blacklisted */
Alex Elder6d292902013-01-14 12:43:31 -0600432};
433
Alex Eldercfbf6372013-05-31 17:40:45 -0500434static DEFINE_MUTEX(client_mutex); /* Serialize client creation */
Alex Eldere124a82f2012-01-29 13:57:44 -0600435
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700436static LIST_HEAD(rbd_dev_list); /* devices */
Alex Eldere124a82f2012-01-29 13:57:44 -0600437static DEFINE_SPINLOCK(rbd_dev_list_lock);
438
Alex Elder432b8582012-01-29 13:57:44 -0600439static LIST_HEAD(rbd_client_list); /* clients */
440static DEFINE_SPINLOCK(rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700441
Alex Elder78c2a442013-05-01 12:43:04 -0500442/* Slab caches for frequently-allocated structures */
443
Alex Elder1c2a9df2013-05-01 12:43:03 -0500444static struct kmem_cache *rbd_img_request_cache;
Alex Elder868311b2013-05-01 12:43:03 -0500445static struct kmem_cache *rbd_obj_request_cache;
Alex Elder1c2a9df2013-05-01 12:43:03 -0500446
NeilBrownf856dc32017-06-18 14:38:58 +1000447static struct bio_set *rbd_bio_clone;
448
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200449static int rbd_major;
Ilya Dryomovf8a22fc2013-12-13 15:28:57 +0200450static DEFINE_IDA(rbd_dev_id_ida);
451
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +0400452static struct workqueue_struct *rbd_wq;
453
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200454/*
Ilya Dryomov3cfa3b12017-11-13 10:35:40 +0100455 * single-major requires >= 0.75 version of userspace rbd utility.
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200456 */
Ilya Dryomov3cfa3b12017-11-13 10:35:40 +0100457static bool single_major = true;
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200458module_param(single_major, bool, S_IRUGO);
Ilya Dryomov3cfa3b12017-11-13 10:35:40 +0100459MODULE_PARM_DESC(single_major, "Use a single major number for all rbd devices (default: true)");
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200460
Alex Elder3d7efd12013-04-19 15:34:50 -0500461static int rbd_img_request_submit(struct rbd_img_request *img_request);
462
Alex Elderf0f8cef2012-01-29 13:57:44 -0600463static ssize_t rbd_add(struct bus_type *bus, const char *buf,
464 size_t count);
465static ssize_t rbd_remove(struct bus_type *bus, const char *buf,
466 size_t count);
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200467static ssize_t rbd_add_single_major(struct bus_type *bus, const char *buf,
468 size_t count);
469static ssize_t rbd_remove_single_major(struct bus_type *bus, const char *buf,
470 size_t count);
Ilya Dryomov6d69bb532015-10-11 19:38:00 +0200471static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth);
Alex Eldera2acd002013-05-08 22:50:04 -0500472static void rbd_spec_put(struct rbd_spec *spec);
Alex Elderf0f8cef2012-01-29 13:57:44 -0600473
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200474static int rbd_dev_id_to_minor(int dev_id)
475{
Ilya Dryomov7e513d42013-12-16 19:26:32 +0200476 return dev_id << RBD_SINGLE_MAJOR_PART_SHIFT;
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200477}
478
479static int minor_to_rbd_dev_id(int minor)
480{
Ilya Dryomov7e513d42013-12-16 19:26:32 +0200481 return minor >> RBD_SINGLE_MAJOR_PART_SHIFT;
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200482}
483
Ilya Dryomoved95b212016-08-12 16:40:02 +0200484static bool __rbd_is_lock_owner(struct rbd_device *rbd_dev)
485{
486 return rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED ||
487 rbd_dev->lock_state == RBD_LOCK_STATE_RELEASING;
488}
489
490static bool rbd_is_lock_owner(struct rbd_device *rbd_dev)
491{
492 bool is_lock_owner;
493
494 down_read(&rbd_dev->lock_rwsem);
495 is_lock_owner = __rbd_is_lock_owner(rbd_dev);
496 up_read(&rbd_dev->lock_rwsem);
497 return is_lock_owner;
498}
499
Ilya Dryomov8767b292017-03-02 19:56:57 +0100500static ssize_t rbd_supported_features_show(struct bus_type *bus, char *buf)
501{
502 return sprintf(buf, "0x%llx\n", RBD_FEATURES_SUPPORTED);
503}
504
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700505static BUS_ATTR(add, S_IWUSR, NULL, rbd_add);
506static BUS_ATTR(remove, S_IWUSR, NULL, rbd_remove);
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200507static BUS_ATTR(add_single_major, S_IWUSR, NULL, rbd_add_single_major);
508static BUS_ATTR(remove_single_major, S_IWUSR, NULL, rbd_remove_single_major);
Ilya Dryomov8767b292017-03-02 19:56:57 +0100509static BUS_ATTR(supported_features, S_IRUGO, rbd_supported_features_show, NULL);
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700510
511static struct attribute *rbd_bus_attrs[] = {
512 &bus_attr_add.attr,
513 &bus_attr_remove.attr,
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200514 &bus_attr_add_single_major.attr,
515 &bus_attr_remove_single_major.attr,
Ilya Dryomov8767b292017-03-02 19:56:57 +0100516 &bus_attr_supported_features.attr,
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700517 NULL,
Alex Elderf0f8cef2012-01-29 13:57:44 -0600518};
Ilya Dryomov92c76dc2013-12-13 15:28:57 +0200519
520static umode_t rbd_bus_is_visible(struct kobject *kobj,
521 struct attribute *attr, int index)
522{
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200523 if (!single_major &&
524 (attr == &bus_attr_add_single_major.attr ||
525 attr == &bus_attr_remove_single_major.attr))
526 return 0;
527
Ilya Dryomov92c76dc2013-12-13 15:28:57 +0200528 return attr->mode;
529}
530
531static const struct attribute_group rbd_bus_group = {
532 .attrs = rbd_bus_attrs,
533 .is_visible = rbd_bus_is_visible,
534};
535__ATTRIBUTE_GROUPS(rbd_bus);
Alex Elderf0f8cef2012-01-29 13:57:44 -0600536
537static struct bus_type rbd_bus_type = {
538 .name = "rbd",
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700539 .bus_groups = rbd_bus_groups,
Alex Elderf0f8cef2012-01-29 13:57:44 -0600540};
541
542static void rbd_root_dev_release(struct device *dev)
543{
544}
545
546static struct device rbd_root_dev = {
547 .init_name = "rbd",
548 .release = rbd_root_dev_release,
549};
550
Alex Elder06ecc6c2012-11-01 10:17:15 -0500551static __printf(2, 3)
552void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...)
553{
554 struct va_format vaf;
555 va_list args;
556
557 va_start(args, fmt);
558 vaf.fmt = fmt;
559 vaf.va = &args;
560
561 if (!rbd_dev)
562 printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf);
563 else if (rbd_dev->disk)
564 printk(KERN_WARNING "%s: %s: %pV\n",
565 RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf);
566 else if (rbd_dev->spec && rbd_dev->spec->image_name)
567 printk(KERN_WARNING "%s: image %s: %pV\n",
568 RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf);
569 else if (rbd_dev->spec && rbd_dev->spec->image_id)
570 printk(KERN_WARNING "%s: id %s: %pV\n",
571 RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf);
572 else /* punt */
573 printk(KERN_WARNING "%s: rbd_dev %p: %pV\n",
574 RBD_DRV_NAME, rbd_dev, &vaf);
575 va_end(args);
576}
577
Alex Elderaafb2302012-09-06 16:00:54 -0500578#ifdef RBD_DEBUG
579#define rbd_assert(expr) \
580 if (unlikely(!(expr))) { \
581 printk(KERN_ERR "\nAssertion failure in %s() " \
582 "at line %d:\n\n" \
583 "\trbd_assert(%s);\n\n", \
584 __func__, __LINE__, #expr); \
585 BUG(); \
586 }
587#else /* !RBD_DEBUG */
588# define rbd_assert(expr) ((void) 0)
589#endif /* !RBD_DEBUG */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800590
Ilya Dryomov27617132015-07-16 17:36:11 +0300591static void rbd_osd_copyup_callback(struct rbd_obj_request *obj_request);
Alex Elderb454e362013-04-19 15:34:50 -0500592static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request);
Alex Elder05a46af2013-04-26 15:44:36 -0500593static void rbd_img_parent_read(struct rbd_obj_request *obj_request);
594static void rbd_dev_remove_parent(struct rbd_device *rbd_dev);
Alex Elder8b3e1a52013-01-24 16:13:36 -0600595
Alex Eldercc4a38bd2013-04-30 00:44:33 -0500596static int rbd_dev_refresh(struct rbd_device *rbd_dev);
Alex Elder2df3fac2013-05-06 09:51:30 -0500597static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev);
Ilya Dryomova720ae02014-07-23 17:11:19 +0400598static int rbd_dev_header_info(struct rbd_device *rbd_dev);
Ilya Dryomove8f59b52014-07-24 10:42:13 +0400599static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev);
Alex Elder54cac612013-04-30 00:44:33 -0500600static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
601 u64 snap_id);
Alex Elder2ad3d712013-04-30 00:44:33 -0500602static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
603 u8 *order, u64 *snap_size);
604static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
605 u64 *snap_features);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700606
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700607static int rbd_open(struct block_device *bdev, fmode_t mode)
608{
Alex Elderf0f8cef2012-01-29 13:57:44 -0600609 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
Alex Elderb82d1672013-01-14 12:43:31 -0600610 bool removing = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700611
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{
Ilya Dryomov1de797b2017-10-12 12:35:19 +0200641 int ro;
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800642
Ilya Dryomov1de797b2017-10-12 12:35:19 +0200643 if (get_user(ro, (int __user *)arg))
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800644 return -EFAULT;
645
Ilya Dryomov1de797b2017-10-12 12:35:19 +0200646 /* Snapshots can't be marked read-write */
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800647 if (rbd_dev->spec->snap_id != CEPH_NOSNAP && !ro)
648 return -EROFS;
649
Ilya Dryomov1de797b2017-10-12 12:35:19 +0200650 /* Let blkdev_roset() handle it */
651 return -ENOTTY;
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800652}
653
654static int rbd_ioctl(struct block_device *bdev, fmode_t mode,
655 unsigned int cmd, unsigned long arg)
656{
657 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
Ilya Dryomov1de797b2017-10-12 12:35:19 +0200658 int ret;
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800659
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800660 switch (cmd) {
661 case BLKROSET:
662 ret = rbd_ioctl_set_ro(rbd_dev, arg);
663 break;
664 default:
665 ret = -ENOTTY;
666 }
667
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800668 return ret;
669}
670
671#ifdef CONFIG_COMPAT
672static int rbd_compat_ioctl(struct block_device *bdev, fmode_t mode,
673 unsigned int cmd, unsigned long arg)
674{
675 return rbd_ioctl(bdev, mode, cmd, arg);
676}
677#endif /* CONFIG_COMPAT */
678
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700679static const struct block_device_operations rbd_bd_ops = {
680 .owner = THIS_MODULE,
681 .open = rbd_open,
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800682 .release = rbd_release,
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800683 .ioctl = rbd_ioctl,
684#ifdef CONFIG_COMPAT
685 .compat_ioctl = rbd_compat_ioctl,
686#endif
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700687};
688
689/*
Alex Elder7262cfc2013-05-16 15:04:20 -0500690 * Initialize an rbd client instance. Success or not, this function
Alex Eldercfbf6372013-05-31 17:40:45 -0500691 * consumes ceph_opts. Caller holds client_mutex.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700692 */
Alex Elderf8c38922012-08-10 13:12:07 -0700693static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700694{
695 struct rbd_client *rbdc;
696 int ret = -ENOMEM;
697
Alex Elder37206ee2013-02-20 17:32:08 -0600698 dout("%s:\n", __func__);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700699 rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL);
700 if (!rbdc)
701 goto out_opt;
702
703 kref_init(&rbdc->kref);
704 INIT_LIST_HEAD(&rbdc->node);
705
Ilya Dryomov74da4a0f2017-03-03 18:16:07 +0100706 rbdc->client = ceph_create_client(ceph_opts, rbdc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700707 if (IS_ERR(rbdc->client))
Alex Elder08f75462013-05-29 11:19:00 -0500708 goto out_rbdc;
Alex Elder43ae4702012-07-03 16:01:18 -0500709 ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700710
711 ret = ceph_open_session(rbdc->client);
712 if (ret < 0)
Alex Elder08f75462013-05-29 11:19:00 -0500713 goto out_client;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700714
Alex Elder432b8582012-01-29 13:57:44 -0600715 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700716 list_add_tail(&rbdc->node, &rbd_client_list);
Alex Elder432b8582012-01-29 13:57:44 -0600717 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700718
Alex Elder37206ee2013-02-20 17:32:08 -0600719 dout("%s: rbdc %p\n", __func__, rbdc);
Alex Elderbc534d82012-01-29 13:57:44 -0600720
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700721 return rbdc;
Alex Elder08f75462013-05-29 11:19:00 -0500722out_client:
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700723 ceph_destroy_client(rbdc->client);
Alex Elder08f75462013-05-29 11:19:00 -0500724out_rbdc:
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700725 kfree(rbdc);
726out_opt:
Alex Elder43ae4702012-07-03 16:01:18 -0500727 if (ceph_opts)
728 ceph_destroy_options(ceph_opts);
Alex Elder37206ee2013-02-20 17:32:08 -0600729 dout("%s: error %d\n", __func__, ret);
730
Vasiliy Kulikov28f259b2010-09-26 12:59:37 +0400731 return ERR_PTR(ret);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700732}
733
Alex Elder2f82ee52012-10-30 19:40:33 -0500734static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc)
735{
736 kref_get(&rbdc->kref);
737
738 return rbdc;
739}
740
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700741/*
Alex Elder1f7ba332012-08-10 13:12:07 -0700742 * Find a ceph client with specific addr and configuration. If
743 * found, bump its reference count.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700744 */
Alex Elder1f7ba332012-08-10 13:12:07 -0700745static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700746{
747 struct rbd_client *client_node;
Alex Elder1f7ba332012-08-10 13:12:07 -0700748 bool found = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700749
Alex Elder43ae4702012-07-03 16:01:18 -0500750 if (ceph_opts->flags & CEPH_OPT_NOSHARE)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700751 return NULL;
752
Alex Elder1f7ba332012-08-10 13:12:07 -0700753 spin_lock(&rbd_client_list_lock);
754 list_for_each_entry(client_node, &rbd_client_list, node) {
755 if (!ceph_compare_options(ceph_opts, client_node->client)) {
Alex Elder2f82ee52012-10-30 19:40:33 -0500756 __rbd_get_client(client_node);
757
Alex Elder1f7ba332012-08-10 13:12:07 -0700758 found = true;
759 break;
760 }
761 }
762 spin_unlock(&rbd_client_list_lock);
763
764 return found ? client_node : NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700765}
766
767/*
Ilya Dryomov210c1042015-06-22 13:24:48 +0300768 * (Per device) rbd map options
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700769 */
770enum {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300771 Opt_queue_depth,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700772 Opt_last_int,
773 /* int args above */
774 Opt_last_string,
775 /* string args above */
Alex Eldercc0538b2012-08-10 13:12:07 -0700776 Opt_read_only,
777 Opt_read_write,
Ilya Dryomov80de1912016-09-20 14:23:17 +0200778 Opt_lock_on_read,
Ilya Dryomove010dd02017-04-13 12:17:39 +0200779 Opt_exclusive,
Ilya Dryomov210c1042015-06-22 13:24:48 +0300780 Opt_err
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700781};
782
Alex Elder43ae4702012-07-03 16:01:18 -0500783static match_table_t rbd_opts_tokens = {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300784 {Opt_queue_depth, "queue_depth=%d"},
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700785 /* int args above */
786 /* string args above */
Alex Elderbe466c12012-10-22 11:31:26 -0500787 {Opt_read_only, "read_only"},
Alex Eldercc0538b2012-08-10 13:12:07 -0700788 {Opt_read_only, "ro"}, /* Alternate spelling */
789 {Opt_read_write, "read_write"},
790 {Opt_read_write, "rw"}, /* Alternate spelling */
Ilya Dryomov80de1912016-09-20 14:23:17 +0200791 {Opt_lock_on_read, "lock_on_read"},
Ilya Dryomove010dd02017-04-13 12:17:39 +0200792 {Opt_exclusive, "exclusive"},
Ilya Dryomov210c1042015-06-22 13:24:48 +0300793 {Opt_err, NULL}
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700794};
795
Alex Elder98571b52013-01-20 14:44:42 -0600796struct rbd_options {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300797 int queue_depth;
Alex Elder98571b52013-01-20 14:44:42 -0600798 bool read_only;
Ilya Dryomov80de1912016-09-20 14:23:17 +0200799 bool lock_on_read;
Ilya Dryomove010dd02017-04-13 12:17:39 +0200800 bool exclusive;
Alex Elder98571b52013-01-20 14:44:42 -0600801};
802
Ilya Dryomovb5584182015-06-23 16:21:19 +0300803#define RBD_QUEUE_DEPTH_DEFAULT BLKDEV_MAX_RQ
Alex Elder98571b52013-01-20 14:44:42 -0600804#define RBD_READ_ONLY_DEFAULT false
Ilya Dryomov80de1912016-09-20 14:23:17 +0200805#define RBD_LOCK_ON_READ_DEFAULT false
Ilya Dryomove010dd02017-04-13 12:17:39 +0200806#define RBD_EXCLUSIVE_DEFAULT false
Alex Elder98571b52013-01-20 14:44:42 -0600807
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700808static int parse_rbd_opts_token(char *c, void *private)
809{
Alex Elder43ae4702012-07-03 16:01:18 -0500810 struct rbd_options *rbd_opts = private;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700811 substring_t argstr[MAX_OPT_ARGS];
812 int token, intval, ret;
813
Alex Elder43ae4702012-07-03 16:01:18 -0500814 token = match_token(c, rbd_opts_tokens, argstr);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700815 if (token < Opt_last_int) {
816 ret = match_int(&argstr[0], &intval);
817 if (ret < 0) {
Ilya Dryomov210c1042015-06-22 13:24:48 +0300818 pr_err("bad mount option arg (not int) at '%s'\n", c);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700819 return ret;
820 }
821 dout("got int token %d val %d\n", token, intval);
822 } else if (token > Opt_last_int && token < Opt_last_string) {
Ilya Dryomov210c1042015-06-22 13:24:48 +0300823 dout("got string token %d val %s\n", token, argstr[0].from);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700824 } else {
825 dout("got token %d\n", token);
826 }
827
828 switch (token) {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300829 case Opt_queue_depth:
830 if (intval < 1) {
831 pr_err("queue_depth out of range\n");
832 return -EINVAL;
833 }
834 rbd_opts->queue_depth = intval;
835 break;
Alex Eldercc0538b2012-08-10 13:12:07 -0700836 case Opt_read_only:
837 rbd_opts->read_only = true;
838 break;
839 case Opt_read_write:
840 rbd_opts->read_only = false;
841 break;
Ilya Dryomov80de1912016-09-20 14:23:17 +0200842 case Opt_lock_on_read:
843 rbd_opts->lock_on_read = true;
844 break;
Ilya Dryomove010dd02017-04-13 12:17:39 +0200845 case Opt_exclusive:
846 rbd_opts->exclusive = true;
847 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700848 default:
Ilya Dryomov210c1042015-06-22 13:24:48 +0300849 /* libceph prints "bad option" msg */
850 return -EINVAL;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700851 }
Ilya Dryomov210c1042015-06-22 13:24:48 +0300852
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700853 return 0;
854}
855
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800856static char* obj_op_name(enum obj_operation_type op_type)
857{
858 switch (op_type) {
859 case OBJ_OP_READ:
860 return "read";
861 case OBJ_OP_WRITE:
862 return "write";
Guangliang Zhao90e98c52014-04-01 22:22:16 +0800863 case OBJ_OP_DISCARD:
864 return "discard";
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800865 default:
866 return "???";
867 }
868}
869
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700870/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700871 * Get a ceph client with specific addr and configuration, if one does
Alex Elder7262cfc2013-05-16 15:04:20 -0500872 * not exist create it. Either way, ceph_opts is consumed by this
873 * function.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700874 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500875static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700876{
Alex Elderf8c38922012-08-10 13:12:07 -0700877 struct rbd_client *rbdc;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700878
Alex Eldercfbf6372013-05-31 17:40:45 -0500879 mutex_lock_nested(&client_mutex, SINGLE_DEPTH_NESTING);
Alex Elder1f7ba332012-08-10 13:12:07 -0700880 rbdc = rbd_client_find(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500881 if (rbdc) /* using an existing client */
Alex Elder43ae4702012-07-03 16:01:18 -0500882 ceph_destroy_options(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500883 else
Alex Elderf8c38922012-08-10 13:12:07 -0700884 rbdc = rbd_client_create(ceph_opts);
Alex Eldercfbf6372013-05-31 17:40:45 -0500885 mutex_unlock(&client_mutex);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700886
Alex Elder9d3997f2012-10-25 23:34:42 -0500887 return rbdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700888}
889
890/*
891 * Destroy ceph client
Alex Elderd23a4b32012-01-29 13:57:43 -0600892 *
Alex Elder432b8582012-01-29 13:57:44 -0600893 * Caller must hold rbd_client_list_lock.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700894 */
895static void rbd_client_release(struct kref *kref)
896{
897 struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
898
Alex Elder37206ee2013-02-20 17:32:08 -0600899 dout("%s: rbdc %p\n", __func__, rbdc);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500900 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700901 list_del(&rbdc->node);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500902 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700903
904 ceph_destroy_client(rbdc->client);
905 kfree(rbdc);
906}
907
908/*
909 * Drop reference to ceph client node. If it's not referenced anymore, release
910 * it.
911 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500912static void rbd_put_client(struct rbd_client *rbdc)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700913{
Alex Elderc53d5892012-10-25 23:34:42 -0500914 if (rbdc)
915 kref_put(&rbdc->kref, rbd_client_release);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700916}
917
Alex Eldera30b71b2012-07-10 20:30:11 -0500918static bool rbd_image_format_valid(u32 image_format)
919{
920 return image_format == 1 || image_format == 2;
921}
922
Alex Elder8e94af82012-07-25 09:32:40 -0500923static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
924{
Alex Elder103a1502012-08-02 11:29:45 -0500925 size_t size;
926 u32 snap_count;
927
928 /* The header has to start with the magic rbd header text */
929 if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT)))
930 return false;
931
Alex Elderdb2388b2012-10-20 22:17:27 -0500932 /* The bio layer requires at least sector-sized I/O */
933
934 if (ondisk->options.order < SECTOR_SHIFT)
935 return false;
936
937 /* If we use u64 in a few spots we may be able to loosen this */
938
939 if (ondisk->options.order > 8 * sizeof (int) - 1)
940 return false;
941
Alex Elder103a1502012-08-02 11:29:45 -0500942 /*
943 * The size of a snapshot header has to fit in a size_t, and
944 * that limits the number of snapshots.
945 */
946 snap_count = le32_to_cpu(ondisk->snap_count);
947 size = SIZE_MAX - sizeof (struct ceph_snap_context);
948 if (snap_count > size / sizeof (__le64))
949 return false;
950
951 /*
952 * Not only that, but the size of the entire the snapshot
953 * header must also be representable in a size_t.
954 */
955 size -= snap_count * sizeof (__le64);
956 if ((u64) size < le64_to_cpu(ondisk->snap_names_len))
957 return false;
958
959 return true;
Alex Elder8e94af82012-07-25 09:32:40 -0500960}
961
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700962/*
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +0100963 * returns the size of an object in the image
964 */
965static u32 rbd_obj_bytes(struct rbd_image_header *header)
966{
967 return 1U << header->obj_order;
968}
969
Ilya Dryomov263423f2017-01-25 18:16:22 +0100970static void rbd_init_layout(struct rbd_device *rbd_dev)
971{
972 if (rbd_dev->header.stripe_unit == 0 ||
973 rbd_dev->header.stripe_count == 0) {
974 rbd_dev->header.stripe_unit = rbd_obj_bytes(&rbd_dev->header);
975 rbd_dev->header.stripe_count = 1;
976 }
977
978 rbd_dev->layout.stripe_unit = rbd_dev->header.stripe_unit;
979 rbd_dev->layout.stripe_count = rbd_dev->header.stripe_count;
980 rbd_dev->layout.object_size = rbd_obj_bytes(&rbd_dev->header);
Ilya Dryomov7e973322017-01-25 18:16:22 +0100981 rbd_dev->layout.pool_id = rbd_dev->header.data_pool_id == CEPH_NOPOOL ?
982 rbd_dev->spec->pool_id : rbd_dev->header.data_pool_id;
Ilya Dryomov263423f2017-01-25 18:16:22 +0100983 RCU_INIT_POINTER(rbd_dev->layout.pool_ns, NULL);
984}
985
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +0100986/*
Alex Elderbb23e372013-05-06 09:51:29 -0500987 * Fill an rbd image header with information from the given format 1
988 * on-disk header.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700989 */
Alex Elder662518b2013-05-06 09:51:29 -0500990static int rbd_header_from_disk(struct rbd_device *rbd_dev,
Alex Elder4156d992012-08-02 11:29:46 -0500991 struct rbd_image_header_ondisk *ondisk)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700992{
Alex Elder662518b2013-05-06 09:51:29 -0500993 struct rbd_image_header *header = &rbd_dev->header;
Alex Elderbb23e372013-05-06 09:51:29 -0500994 bool first_time = header->object_prefix == NULL;
995 struct ceph_snap_context *snapc;
996 char *object_prefix = NULL;
997 char *snap_names = NULL;
998 u64 *snap_sizes = NULL;
Alex Elderccece232012-07-10 20:30:10 -0500999 u32 snap_count;
Alex Elderbb23e372013-05-06 09:51:29 -05001000 int ret = -ENOMEM;
Alex Elder621901d2012-08-23 23:22:06 -05001001 u32 i;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001002
Alex Elderbb23e372013-05-06 09:51:29 -05001003 /* Allocate this now to avoid having to handle failure below */
1004
1005 if (first_time) {
Ilya Dryomov848d7962017-01-25 18:16:21 +01001006 object_prefix = kstrndup(ondisk->object_prefix,
1007 sizeof(ondisk->object_prefix),
1008 GFP_KERNEL);
Alex Elderbb23e372013-05-06 09:51:29 -05001009 if (!object_prefix)
1010 return -ENOMEM;
Alex Elderbb23e372013-05-06 09:51:29 -05001011 }
1012
1013 /* Allocate the snapshot context and fill it in */
Alex Elder6a523252012-07-19 17:12:59 -05001014
Alex Elder103a1502012-08-02 11:29:45 -05001015 snap_count = le32_to_cpu(ondisk->snap_count);
Alex Elderbb23e372013-05-06 09:51:29 -05001016 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
1017 if (!snapc)
1018 goto out_err;
1019 snapc->seq = le64_to_cpu(ondisk->snap_seq);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001020 if (snap_count) {
Alex Elderbb23e372013-05-06 09:51:29 -05001021 struct rbd_image_snap_ondisk *snaps;
Alex Elderf785cc12012-08-23 23:22:06 -05001022 u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len);
1023
Alex Elderbb23e372013-05-06 09:51:29 -05001024 /* We'll keep a copy of the snapshot names... */
Alex Elder621901d2012-08-23 23:22:06 -05001025
Alex Elderbb23e372013-05-06 09:51:29 -05001026 if (snap_names_len > (u64)SIZE_MAX)
1027 goto out_2big;
1028 snap_names = kmalloc(snap_names_len, GFP_KERNEL);
1029 if (!snap_names)
Alex Elder6a523252012-07-19 17:12:59 -05001030 goto out_err;
Alex Elderbb23e372013-05-06 09:51:29 -05001031
1032 /* ...as well as the array of their sizes. */
Markus Elfring88a25a52016-09-11 12:21:25 +02001033 snap_sizes = kmalloc_array(snap_count,
1034 sizeof(*header->snap_sizes),
1035 GFP_KERNEL);
Alex Elderbb23e372013-05-06 09:51:29 -05001036 if (!snap_sizes)
1037 goto out_err;
1038
Alex Elderf785cc12012-08-23 23:22:06 -05001039 /*
Alex Elderbb23e372013-05-06 09:51:29 -05001040 * Copy the names, and fill in each snapshot's id
1041 * and size.
1042 *
Alex Elder99a41eb2013-05-06 09:51:30 -05001043 * Note that rbd_dev_v1_header_info() guarantees the
Alex Elderbb23e372013-05-06 09:51:29 -05001044 * ondisk buffer we're working with has
Alex Elderf785cc12012-08-23 23:22:06 -05001045 * snap_names_len bytes beyond the end of the
1046 * snapshot id array, this memcpy() is safe.
1047 */
Alex Elderbb23e372013-05-06 09:51:29 -05001048 memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len);
1049 snaps = ondisk->snaps;
1050 for (i = 0; i < snap_count; i++) {
1051 snapc->snaps[i] = le64_to_cpu(snaps[i].id);
1052 snap_sizes[i] = le64_to_cpu(snaps[i].image_size);
1053 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001054 }
Alex Elder849b4262012-07-09 21:04:24 -05001055
Alex Elderbb23e372013-05-06 09:51:29 -05001056 /* We won't fail any more, fill in the header */
Alex Elder6a523252012-07-19 17:12:59 -05001057
Alex Elderbb23e372013-05-06 09:51:29 -05001058 if (first_time) {
1059 header->object_prefix = object_prefix;
1060 header->obj_order = ondisk->options.order;
Ilya Dryomov263423f2017-01-25 18:16:22 +01001061 rbd_init_layout(rbd_dev);
Alex Elder662518b2013-05-06 09:51:29 -05001062 } else {
1063 ceph_put_snap_context(header->snapc);
1064 kfree(header->snap_names);
1065 kfree(header->snap_sizes);
Alex Elderbb23e372013-05-06 09:51:29 -05001066 }
1067
1068 /* The remaining fields always get updated (when we refresh) */
Alex Elder621901d2012-08-23 23:22:06 -05001069
Alex Elderf84344f2012-08-31 17:29:51 -05001070 header->image_size = le64_to_cpu(ondisk->image_size);
Alex Elderbb23e372013-05-06 09:51:29 -05001071 header->snapc = snapc;
1072 header->snap_names = snap_names;
1073 header->snap_sizes = snap_sizes;
Alex Elder468521c2013-04-26 09:43:47 -05001074
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001075 return 0;
Alex Elderbb23e372013-05-06 09:51:29 -05001076out_2big:
1077 ret = -EIO;
Alex Elder6a523252012-07-19 17:12:59 -05001078out_err:
Alex Elderbb23e372013-05-06 09:51:29 -05001079 kfree(snap_sizes);
1080 kfree(snap_names);
1081 ceph_put_snap_context(snapc);
1082 kfree(object_prefix);
Alex Elderccece232012-07-10 20:30:10 -05001083
Alex Elderbb23e372013-05-06 09:51:29 -05001084 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001085}
1086
Alex Elder9682fc62013-04-30 00:44:33 -05001087static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which)
1088{
1089 const char *snap_name;
1090
1091 rbd_assert(which < rbd_dev->header.snapc->num_snaps);
1092
1093 /* Skip over names until we find the one we are looking for */
1094
1095 snap_name = rbd_dev->header.snap_names;
1096 while (which--)
1097 snap_name += strlen(snap_name) + 1;
1098
1099 return kstrdup(snap_name, GFP_KERNEL);
1100}
1101
Alex Elder30d1cff2013-05-01 12:43:03 -05001102/*
1103 * Snapshot id comparison function for use with qsort()/bsearch().
1104 * Note that result is for snapshots in *descending* order.
1105 */
1106static int snapid_compare_reverse(const void *s1, const void *s2)
1107{
1108 u64 snap_id1 = *(u64 *)s1;
1109 u64 snap_id2 = *(u64 *)s2;
1110
1111 if (snap_id1 < snap_id2)
1112 return 1;
1113 return snap_id1 == snap_id2 ? 0 : -1;
1114}
1115
1116/*
1117 * Search a snapshot context to see if the given snapshot id is
1118 * present.
1119 *
1120 * Returns the position of the snapshot id in the array if it's found,
1121 * or BAD_SNAP_INDEX otherwise.
1122 *
1123 * Note: The snapshot array is in kept sorted (by the osd) in
1124 * reverse order, highest snapshot id first.
1125 */
Alex Elder9682fc62013-04-30 00:44:33 -05001126static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id)
1127{
1128 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
Alex Elder30d1cff2013-05-01 12:43:03 -05001129 u64 *found;
Alex Elder9682fc62013-04-30 00:44:33 -05001130
Alex Elder30d1cff2013-05-01 12:43:03 -05001131 found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps,
1132 sizeof (snap_id), snapid_compare_reverse);
Alex Elder9682fc62013-04-30 00:44:33 -05001133
Alex Elder30d1cff2013-05-01 12:43:03 -05001134 return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX;
Alex Elder9682fc62013-04-30 00:44:33 -05001135}
1136
Alex Elder2ad3d712013-04-30 00:44:33 -05001137static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev,
1138 u64 snap_id)
Alex Elder54cac612013-04-30 00:44:33 -05001139{
1140 u32 which;
Josh Durginda6a6b62013-09-04 17:57:31 -07001141 const char *snap_name;
Alex Elder54cac612013-04-30 00:44:33 -05001142
1143 which = rbd_dev_snap_index(rbd_dev, snap_id);
1144 if (which == BAD_SNAP_INDEX)
Josh Durginda6a6b62013-09-04 17:57:31 -07001145 return ERR_PTR(-ENOENT);
Alex Elder54cac612013-04-30 00:44:33 -05001146
Josh Durginda6a6b62013-09-04 17:57:31 -07001147 snap_name = _rbd_dev_v1_snap_name(rbd_dev, which);
1148 return snap_name ? snap_name : ERR_PTR(-ENOMEM);
Alex Elder54cac612013-04-30 00:44:33 -05001149}
1150
Alex Elder9e15b772012-10-30 19:40:33 -05001151static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id)
1152{
Alex Elder9e15b772012-10-30 19:40:33 -05001153 if (snap_id == CEPH_NOSNAP)
1154 return RBD_SNAP_HEAD_NAME;
1155
Alex Elder54cac612013-04-30 00:44:33 -05001156 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1157 if (rbd_dev->image_format == 1)
1158 return rbd_dev_v1_snap_name(rbd_dev, snap_id);
Alex Elder9e15b772012-10-30 19:40:33 -05001159
Alex Elder54cac612013-04-30 00:44:33 -05001160 return rbd_dev_v2_snap_name(rbd_dev, snap_id);
Alex Elder9e15b772012-10-30 19:40:33 -05001161}
1162
Alex Elder2ad3d712013-04-30 00:44:33 -05001163static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
1164 u64 *snap_size)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001165{
Alex Elder2ad3d712013-04-30 00:44:33 -05001166 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1167 if (snap_id == CEPH_NOSNAP) {
1168 *snap_size = rbd_dev->header.image_size;
1169 } else if (rbd_dev->image_format == 1) {
1170 u32 which;
Alex Elder00f1f362012-02-07 12:03:36 -06001171
Alex Elder2ad3d712013-04-30 00:44:33 -05001172 which = rbd_dev_snap_index(rbd_dev, snap_id);
1173 if (which == BAD_SNAP_INDEX)
1174 return -ENOENT;
Alex Elder00f1f362012-02-07 12:03:36 -06001175
Alex Elder2ad3d712013-04-30 00:44:33 -05001176 *snap_size = rbd_dev->header.snap_sizes[which];
1177 } else {
1178 u64 size = 0;
1179 int ret;
1180
1181 ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size);
1182 if (ret)
1183 return ret;
1184
1185 *snap_size = size;
1186 }
1187 return 0;
1188}
1189
1190static int rbd_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
1191 u64 *snap_features)
1192{
1193 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1194 if (snap_id == CEPH_NOSNAP) {
1195 *snap_features = rbd_dev->header.features;
1196 } else if (rbd_dev->image_format == 1) {
1197 *snap_features = 0; /* No features for format 1 */
1198 } else {
1199 u64 features = 0;
1200 int ret;
1201
1202 ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, &features);
1203 if (ret)
1204 return ret;
1205
1206 *snap_features = features;
1207 }
1208 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001209}
1210
Alex Elderd1cf5782013-04-27 09:59:30 -05001211static int rbd_dev_mapping_set(struct rbd_device *rbd_dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001212{
Alex Elder8f4b7d92013-05-06 07:40:30 -05001213 u64 snap_id = rbd_dev->spec->snap_id;
Alex Elder2ad3d712013-04-30 00:44:33 -05001214 u64 size = 0;
1215 u64 features = 0;
1216 int ret;
Alex Elder8b0241f2013-04-25 23:15:08 -05001217
Alex Elder2ad3d712013-04-30 00:44:33 -05001218 ret = rbd_snap_size(rbd_dev, snap_id, &size);
1219 if (ret)
1220 return ret;
1221 ret = rbd_snap_features(rbd_dev, snap_id, &features);
1222 if (ret)
1223 return ret;
1224
1225 rbd_dev->mapping.size = size;
1226 rbd_dev->mapping.features = features;
1227
Alex Elder8b0241f2013-04-25 23:15:08 -05001228 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001229}
1230
Alex Elderd1cf5782013-04-27 09:59:30 -05001231static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev)
1232{
1233 rbd_dev->mapping.size = 0;
1234 rbd_dev->mapping.features = 0;
Alex Elder200a6a82013-04-28 23:32:34 -05001235}
1236
Alex Elder65ccfe22012-08-09 10:33:26 -07001237static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset)
1238{
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +01001239 u64 segment_size = rbd_obj_bytes(&rbd_dev->header);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001240
Alex Elder65ccfe22012-08-09 10:33:26 -07001241 return offset & (segment_size - 1);
1242}
1243
1244static u64 rbd_segment_length(struct rbd_device *rbd_dev,
1245 u64 offset, u64 length)
1246{
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +01001247 u64 segment_size = rbd_obj_bytes(&rbd_dev->header);
Alex Elder65ccfe22012-08-09 10:33:26 -07001248
1249 offset &= segment_size - 1;
1250
Alex Elderaafb2302012-09-06 16:00:54 -05001251 rbd_assert(length <= U64_MAX - offset);
Alex Elder65ccfe22012-08-09 10:33:26 -07001252 if (offset + length > segment_size)
1253 length = segment_size - offset;
1254
1255 return length;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001256}
1257
Ilya Dryomov5359a172018-01-20 10:30:10 +01001258static void zero_bvec(struct bio_vec *bv)
1259{
1260 void *buf;
1261 unsigned long flags;
1262
1263 buf = bvec_kmap_irq(bv, &flags);
1264 memset(buf, 0, bv->bv_len);
1265 flush_dcache_page(bv->bv_page);
1266 bvec_kunmap_irq(buf, &flags);
1267}
1268
1269static void zero_bios(struct ceph_bio_iter *bio_pos, u32 off, u32 bytes)
1270{
1271 struct ceph_bio_iter it = *bio_pos;
1272
1273 ceph_bio_iter_advance(&it, off);
1274 ceph_bio_iter_advance_step(&it, bytes, ({
1275 zero_bvec(&bv);
1276 }));
1277}
1278
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001279/*
1280 * bio helpers
1281 */
1282
1283static void bio_chain_put(struct bio *chain)
1284{
1285 struct bio *tmp;
1286
1287 while (chain) {
1288 tmp = chain;
1289 chain = chain->bi_next;
1290 bio_put(tmp);
1291 }
1292}
1293
1294/*
1295 * zeros a bio chain, starting at specific offset
1296 */
1297static void zero_bio_chain(struct bio *chain, int start_ofs)
1298{
Kent Overstreet79886132013-11-23 17:19:00 -08001299 struct bio_vec bv;
1300 struct bvec_iter iter;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001301 unsigned long flags;
1302 void *buf;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001303 int pos = 0;
1304
1305 while (chain) {
Kent Overstreet79886132013-11-23 17:19:00 -08001306 bio_for_each_segment(bv, chain, iter) {
1307 if (pos + bv.bv_len > start_ofs) {
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001308 int remainder = max(start_ofs - pos, 0);
Kent Overstreet79886132013-11-23 17:19:00 -08001309 buf = bvec_kmap_irq(&bv, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001310 memset(buf + remainder, 0,
Kent Overstreet79886132013-11-23 17:19:00 -08001311 bv.bv_len - remainder);
1312 flush_dcache_page(bv.bv_page);
Dan Carpenter85b5aaa2010-10-11 21:15:11 +02001313 bvec_kunmap_irq(buf, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001314 }
Kent Overstreet79886132013-11-23 17:19:00 -08001315 pos += bv.bv_len;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001316 }
1317
1318 chain = chain->bi_next;
1319 }
1320}
1321
1322/*
Alex Elderb9434c52013-04-19 15:34:50 -05001323 * similar to zero_bio_chain(), zeros data defined by a page array,
1324 * starting at the given byte offset from the start of the array and
1325 * continuing up to the given end offset. The pages array is
1326 * assumed to be big enough to hold all bytes up to the end.
1327 */
1328static void zero_pages(struct page **pages, u64 offset, u64 end)
1329{
1330 struct page **page = &pages[offset >> PAGE_SHIFT];
1331
1332 rbd_assert(end > offset);
1333 rbd_assert(end - offset <= (u64)SIZE_MAX);
1334 while (offset < end) {
1335 size_t page_offset;
1336 size_t length;
1337 unsigned long flags;
1338 void *kaddr;
1339
Geert Uytterhoeven491205a2013-05-13 20:35:37 -05001340 page_offset = offset & ~PAGE_MASK;
1341 length = min_t(size_t, PAGE_SIZE - page_offset, end - offset);
Alex Elderb9434c52013-04-19 15:34:50 -05001342 local_irq_save(flags);
1343 kaddr = kmap_atomic(*page);
1344 memset(kaddr + page_offset, 0, length);
Alex Eldere2156052013-05-22 20:54:25 -05001345 flush_dcache_page(*page);
Alex Elderb9434c52013-04-19 15:34:50 -05001346 kunmap_atomic(kaddr);
1347 local_irq_restore(flags);
1348
1349 offset += length;
1350 page++;
1351 }
1352}
1353
1354/*
Alex Elderf7760da2012-10-20 22:17:27 -05001355 * Clone a portion of a bio, starting at the given byte offset
1356 * and continuing for the number of bytes indicated.
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001357 */
Alex Elderf7760da2012-10-20 22:17:27 -05001358static struct bio *bio_clone_range(struct bio *bio_src,
1359 unsigned int offset,
1360 unsigned int len,
1361 gfp_t gfpmask)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001362{
Alex Elderf7760da2012-10-20 22:17:27 -05001363 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001364
NeilBrownf856dc32017-06-18 14:38:58 +10001365 bio = bio_clone_fast(bio_src, gfpmask, rbd_bio_clone);
Alex Elderf7760da2012-10-20 22:17:27 -05001366 if (!bio)
1367 return NULL; /* ENOMEM */
1368
Kent Overstreet5341a6272013-08-07 14:31:11 -07001369 bio_advance(bio, offset);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001370 bio->bi_iter.bi_size = len;
Alex Elder542582f2012-08-09 10:33:25 -07001371
Alex Elderf7760da2012-10-20 22:17:27 -05001372 return bio;
1373}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001374
Alex Elderf7760da2012-10-20 22:17:27 -05001375/*
1376 * Clone a portion of a bio chain, starting at the given byte offset
1377 * into the first bio in the source chain and continuing for the
1378 * number of bytes indicated. The result is another bio chain of
1379 * exactly the given length, or a null pointer on error.
1380 *
1381 * The bio_src and offset parameters are both in-out. On entry they
1382 * refer to the first source bio and the offset into that bio where
1383 * the start of data to be cloned is located.
1384 *
1385 * On return, bio_src is updated to refer to the bio in the source
1386 * chain that contains first un-cloned byte, and *offset will
1387 * contain the offset of that byte within that bio.
1388 */
1389static struct bio *bio_chain_clone_range(struct bio **bio_src,
1390 unsigned int *offset,
1391 unsigned int len,
1392 gfp_t gfpmask)
1393{
1394 struct bio *bi = *bio_src;
1395 unsigned int off = *offset;
1396 struct bio *chain = NULL;
1397 struct bio **end;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001398
Alex Elderf7760da2012-10-20 22:17:27 -05001399 /* Build up a chain of clone bios up to the limit */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001400
Kent Overstreet4f024f32013-10-11 15:44:27 -07001401 if (!bi || off >= bi->bi_iter.bi_size || !len)
Alex Elderf7760da2012-10-20 22:17:27 -05001402 return NULL; /* Nothing to clone */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001403
Alex Elderf7760da2012-10-20 22:17:27 -05001404 end = &chain;
1405 while (len) {
1406 unsigned int bi_size;
1407 struct bio *bio;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001408
Alex Elderf5400b72012-11-01 10:17:15 -05001409 if (!bi) {
1410 rbd_warn(NULL, "bio_chain exhausted with %u left", len);
Alex Elderf7760da2012-10-20 22:17:27 -05001411 goto out_err; /* EINVAL; ran out of bio's */
Alex Elderf5400b72012-11-01 10:17:15 -05001412 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07001413 bi_size = min_t(unsigned int, bi->bi_iter.bi_size - off, len);
Alex Elderf7760da2012-10-20 22:17:27 -05001414 bio = bio_clone_range(bi, off, bi_size, gfpmask);
1415 if (!bio)
1416 goto out_err; /* ENOMEM */
1417
1418 *end = bio;
1419 end = &bio->bi_next;
1420
1421 off += bi_size;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001422 if (off == bi->bi_iter.bi_size) {
Alex Elderf7760da2012-10-20 22:17:27 -05001423 bi = bi->bi_next;
1424 off = 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001425 }
Alex Elderf7760da2012-10-20 22:17:27 -05001426 len -= bi_size;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001427 }
Alex Elderf7760da2012-10-20 22:17:27 -05001428 *bio_src = bi;
1429 *offset = off;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001430
Alex Elderf7760da2012-10-20 22:17:27 -05001431 return chain;
1432out_err:
1433 bio_chain_put(chain);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001434
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001435 return NULL;
1436}
1437
Alex Elder926f9b32013-02-11 12:33:24 -06001438/*
1439 * The default/initial value for all object request flags is 0. For
1440 * each flag, once its value is set to 1 it is never reset to 0
1441 * again.
1442 */
Alex Elder6365d332013-02-11 12:33:24 -06001443static void obj_request_img_data_set(struct rbd_obj_request *obj_request)
1444{
1445 if (test_and_set_bit(OBJ_REQ_IMG_DATA, &obj_request->flags)) {
Alex Elder6365d332013-02-11 12:33:24 -06001446 struct rbd_device *rbd_dev;
1447
Alex Elder57acbaa2013-02-11 12:33:24 -06001448 rbd_dev = obj_request->img_request->rbd_dev;
Ilya Dryomov9584d502014-07-11 12:11:20 +04001449 rbd_warn(rbd_dev, "obj_request %p already marked img_data",
Alex Elder6365d332013-02-11 12:33:24 -06001450 obj_request);
1451 }
1452}
1453
1454static bool obj_request_img_data_test(struct rbd_obj_request *obj_request)
1455{
1456 smp_mb();
1457 return test_bit(OBJ_REQ_IMG_DATA, &obj_request->flags) != 0;
1458}
1459
Alex Elder57acbaa2013-02-11 12:33:24 -06001460static void obj_request_done_set(struct rbd_obj_request *obj_request)
1461{
1462 if (test_and_set_bit(OBJ_REQ_DONE, &obj_request->flags)) {
1463 struct rbd_device *rbd_dev = NULL;
1464
1465 if (obj_request_img_data_test(obj_request))
1466 rbd_dev = obj_request->img_request->rbd_dev;
Ilya Dryomov9584d502014-07-11 12:11:20 +04001467 rbd_warn(rbd_dev, "obj_request %p already marked done",
Alex Elder57acbaa2013-02-11 12:33:24 -06001468 obj_request);
1469 }
1470}
1471
1472static bool obj_request_done_test(struct rbd_obj_request *obj_request)
1473{
1474 smp_mb();
1475 return test_bit(OBJ_REQ_DONE, &obj_request->flags) != 0;
1476}
1477
Alex Elder5679c592013-02-11 12:33:24 -06001478/*
1479 * This sets the KNOWN flag after (possibly) setting the EXISTS
1480 * flag. The latter is set based on the "exists" value provided.
1481 *
1482 * Note that for our purposes once an object exists it never goes
1483 * away again. It's possible that the response from two existence
1484 * checks are separated by the creation of the target object, and
1485 * the first ("doesn't exist") response arrives *after* the second
1486 * ("does exist"). In that case we ignore the second one.
1487 */
1488static void obj_request_existence_set(struct rbd_obj_request *obj_request,
1489 bool exists)
1490{
1491 if (exists)
1492 set_bit(OBJ_REQ_EXISTS, &obj_request->flags);
1493 set_bit(OBJ_REQ_KNOWN, &obj_request->flags);
1494 smp_mb();
1495}
1496
1497static bool obj_request_known_test(struct rbd_obj_request *obj_request)
1498{
1499 smp_mb();
1500 return test_bit(OBJ_REQ_KNOWN, &obj_request->flags) != 0;
1501}
1502
1503static bool obj_request_exists_test(struct rbd_obj_request *obj_request)
1504{
1505 smp_mb();
1506 return test_bit(OBJ_REQ_EXISTS, &obj_request->flags) != 0;
1507}
1508
Ilya Dryomov96385562014-06-10 13:53:29 +04001509static bool obj_request_overlaps_parent(struct rbd_obj_request *obj_request)
1510{
1511 struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev;
1512
1513 return obj_request->img_offset <
1514 round_up(rbd_dev->parent_overlap, rbd_obj_bytes(&rbd_dev->header));
1515}
1516
Alex Elderbf0d5f502012-11-22 00:00:08 -06001517static void rbd_obj_request_get(struct rbd_obj_request *obj_request)
1518{
Alex Elder37206ee2013-02-20 17:32:08 -06001519 dout("%s: obj %p (was %d)\n", __func__, obj_request,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01001520 kref_read(&obj_request->kref));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001521 kref_get(&obj_request->kref);
1522}
1523
1524static void rbd_obj_request_destroy(struct kref *kref);
1525static void rbd_obj_request_put(struct rbd_obj_request *obj_request)
1526{
1527 rbd_assert(obj_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001528 dout("%s: obj %p (was %d)\n", __func__, obj_request,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01001529 kref_read(&obj_request->kref));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001530 kref_put(&obj_request->kref, rbd_obj_request_destroy);
1531}
1532
Alex Elder0f2d5be2014-04-26 14:21:44 +04001533static void rbd_img_request_get(struct rbd_img_request *img_request)
1534{
1535 dout("%s: img %p (was %d)\n", __func__, img_request,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01001536 kref_read(&img_request->kref));
Alex Elder0f2d5be2014-04-26 14:21:44 +04001537 kref_get(&img_request->kref);
1538}
1539
Alex Eldere93f3152013-05-08 22:50:04 -05001540static bool img_request_child_test(struct rbd_img_request *img_request);
1541static void rbd_parent_request_destroy(struct kref *kref);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001542static void rbd_img_request_destroy(struct kref *kref);
1543static void rbd_img_request_put(struct rbd_img_request *img_request)
1544{
1545 rbd_assert(img_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001546 dout("%s: img %p (was %d)\n", __func__, img_request,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01001547 kref_read(&img_request->kref));
Alex Eldere93f3152013-05-08 22:50:04 -05001548 if (img_request_child_test(img_request))
1549 kref_put(&img_request->kref, rbd_parent_request_destroy);
1550 else
1551 kref_put(&img_request->kref, rbd_img_request_destroy);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001552}
1553
1554static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request,
1555 struct rbd_obj_request *obj_request)
1556{
Alex Elder25dcf952013-01-25 17:08:55 -06001557 rbd_assert(obj_request->img_request == NULL);
1558
Alex Elderb155e862013-04-15 14:50:37 -05001559 /* Image request now owns object's original reference */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001560 obj_request->img_request = img_request;
Alex Elder25dcf952013-01-25 17:08:55 -06001561 obj_request->which = img_request->obj_request_count;
Alex Elder6365d332013-02-11 12:33:24 -06001562 rbd_assert(!obj_request_img_data_test(obj_request));
1563 obj_request_img_data_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001564 rbd_assert(obj_request->which != BAD_WHICH);
Alex Elder25dcf952013-01-25 17:08:55 -06001565 img_request->obj_request_count++;
1566 list_add_tail(&obj_request->links, &img_request->obj_requests);
Alex Elder37206ee2013-02-20 17:32:08 -06001567 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1568 obj_request->which);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001569}
1570
1571static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request,
1572 struct rbd_obj_request *obj_request)
1573{
1574 rbd_assert(obj_request->which != BAD_WHICH);
Alex Elder25dcf952013-01-25 17:08:55 -06001575
Alex Elder37206ee2013-02-20 17:32:08 -06001576 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1577 obj_request->which);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001578 list_del(&obj_request->links);
Alex Elder25dcf952013-01-25 17:08:55 -06001579 rbd_assert(img_request->obj_request_count > 0);
1580 img_request->obj_request_count--;
1581 rbd_assert(obj_request->which == img_request->obj_request_count);
1582 obj_request->which = BAD_WHICH;
Alex Elder6365d332013-02-11 12:33:24 -06001583 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001584 rbd_assert(obj_request->img_request == img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001585 obj_request->img_request = NULL;
Alex Elder25dcf952013-01-25 17:08:55 -06001586 obj_request->callback = NULL;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001587 rbd_obj_request_put(obj_request);
1588}
1589
1590static bool obj_request_type_valid(enum obj_request_type type)
1591{
1592 switch (type) {
Alex Elder9969ebc2013-01-18 12:31:10 -06001593 case OBJ_REQUEST_NODATA:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001594 case OBJ_REQUEST_BIO:
Alex Elder788e2df2013-01-17 12:25:27 -06001595 case OBJ_REQUEST_PAGES:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001596 return true;
1597 default:
1598 return false;
1599 }
1600}
1601
Ilya Dryomov4a17dad2016-09-13 21:08:10 +02001602static void rbd_img_obj_callback(struct rbd_obj_request *obj_request);
1603
Ilya Dryomov980917f2016-09-12 18:59:42 +02001604static void rbd_obj_request_submit(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001605{
Ilya Dryomov980917f2016-09-12 18:59:42 +02001606 struct ceph_osd_request *osd_req = obj_request->osd_req;
1607
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01001608 dout("%s %p object_no %016llx %llu~%llu osd_req %p\n", __func__,
1609 obj_request, obj_request->object_no, obj_request->offset,
Ilya Dryomov67e2b652017-01-25 18:16:22 +01001610 obj_request->length, osd_req);
Ilya Dryomov4a17dad2016-09-13 21:08:10 +02001611 if (obj_request_img_data_test(obj_request)) {
1612 WARN_ON(obj_request->callback != rbd_img_obj_callback);
1613 rbd_img_request_get(obj_request->img_request);
1614 }
Ilya Dryomov980917f2016-09-12 18:59:42 +02001615 ceph_osdc_start_request(osd_req->r_osdc, osd_req, false);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001616}
1617
1618static void rbd_img_request_complete(struct rbd_img_request *img_request)
1619{
Alex Elder55f27e02013-04-10 12:34:25 -05001620
Alex Elder37206ee2013-02-20 17:32:08 -06001621 dout("%s: img %p\n", __func__, img_request);
Alex Elder55f27e02013-04-10 12:34:25 -05001622
1623 /*
1624 * If no error occurred, compute the aggregate transfer
1625 * count for the image request. We could instead use
1626 * atomic64_cmpxchg() to update it as each object request
1627 * completes; not clear which way is better off hand.
1628 */
1629 if (!img_request->result) {
1630 struct rbd_obj_request *obj_request;
1631 u64 xferred = 0;
1632
1633 for_each_obj_request(img_request, obj_request)
1634 xferred += obj_request->xferred;
1635 img_request->xferred = xferred;
1636 }
1637
Alex Elderbf0d5f502012-11-22 00:00:08 -06001638 if (img_request->callback)
1639 img_request->callback(img_request);
1640 else
1641 rbd_img_request_put(img_request);
1642}
1643
Alex Elder0c425242013-02-08 09:55:49 -06001644/*
1645 * The default/initial value for all image request flags is 0. Each
1646 * is conditionally set to 1 at image request initialization time
1647 * and currently never change thereafter.
1648 */
1649static void img_request_write_set(struct rbd_img_request *img_request)
1650{
1651 set_bit(IMG_REQ_WRITE, &img_request->flags);
1652 smp_mb();
1653}
1654
1655static bool img_request_write_test(struct rbd_img_request *img_request)
1656{
1657 smp_mb();
1658 return test_bit(IMG_REQ_WRITE, &img_request->flags) != 0;
1659}
1660
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001661/*
1662 * Set the discard flag when the img_request is an discard request
1663 */
1664static void img_request_discard_set(struct rbd_img_request *img_request)
1665{
1666 set_bit(IMG_REQ_DISCARD, &img_request->flags);
1667 smp_mb();
1668}
1669
1670static bool img_request_discard_test(struct rbd_img_request *img_request)
1671{
1672 smp_mb();
1673 return test_bit(IMG_REQ_DISCARD, &img_request->flags) != 0;
1674}
1675
Alex Elder9849e982013-01-24 16:13:36 -06001676static void img_request_child_set(struct rbd_img_request *img_request)
1677{
1678 set_bit(IMG_REQ_CHILD, &img_request->flags);
1679 smp_mb();
1680}
1681
Alex Eldere93f3152013-05-08 22:50:04 -05001682static void img_request_child_clear(struct rbd_img_request *img_request)
1683{
1684 clear_bit(IMG_REQ_CHILD, &img_request->flags);
1685 smp_mb();
1686}
1687
Alex Elder9849e982013-01-24 16:13:36 -06001688static bool img_request_child_test(struct rbd_img_request *img_request)
1689{
1690 smp_mb();
1691 return test_bit(IMG_REQ_CHILD, &img_request->flags) != 0;
1692}
1693
Alex Elderd0b2e942013-01-24 16:13:36 -06001694static void img_request_layered_set(struct rbd_img_request *img_request)
1695{
1696 set_bit(IMG_REQ_LAYERED, &img_request->flags);
1697 smp_mb();
1698}
1699
Alex Eldera2acd002013-05-08 22:50:04 -05001700static void img_request_layered_clear(struct rbd_img_request *img_request)
1701{
1702 clear_bit(IMG_REQ_LAYERED, &img_request->flags);
1703 smp_mb();
1704}
1705
Alex Elderd0b2e942013-01-24 16:13:36 -06001706static bool img_request_layered_test(struct rbd_img_request *img_request)
1707{
1708 smp_mb();
1709 return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0;
1710}
1711
Josh Durgin3b434a2a2014-04-04 17:32:15 -07001712static enum obj_operation_type
1713rbd_img_request_op_type(struct rbd_img_request *img_request)
1714{
1715 if (img_request_write_test(img_request))
1716 return OBJ_OP_WRITE;
1717 else if (img_request_discard_test(img_request))
1718 return OBJ_OP_DISCARD;
1719 else
1720 return OBJ_OP_READ;
1721}
1722
Alex Elder6e2a4502013-03-27 09:16:30 -05001723static void
1724rbd_img_obj_request_read_callback(struct rbd_obj_request *obj_request)
1725{
Alex Elderb9434c52013-04-19 15:34:50 -05001726 u64 xferred = obj_request->xferred;
1727 u64 length = obj_request->length;
1728
Alex Elder6e2a4502013-03-27 09:16:30 -05001729 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1730 obj_request, obj_request->img_request, obj_request->result,
Alex Elderb9434c52013-04-19 15:34:50 -05001731 xferred, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001732 /*
Josh Durgin17c1cc12013-08-26 17:55:38 -07001733 * ENOENT means a hole in the image. We zero-fill the entire
1734 * length of the request. A short read also implies zero-fill
1735 * to the end of the request. An error requires the whole
1736 * length of the request to be reported finished with an error
1737 * to the block layer. In each case we update the xferred
1738 * count to indicate the whole request was satisfied.
Alex Elder6e2a4502013-03-27 09:16:30 -05001739 */
Alex Elderb9434c52013-04-19 15:34:50 -05001740 rbd_assert(obj_request->type != OBJ_REQUEST_NODATA);
Alex Elder6e2a4502013-03-27 09:16:30 -05001741 if (obj_request->result == -ENOENT) {
Alex Elderb9434c52013-04-19 15:34:50 -05001742 if (obj_request->type == OBJ_REQUEST_BIO)
Ilya Dryomov5359a172018-01-20 10:30:10 +01001743 zero_bios(&obj_request->bio_pos, 0, length);
Alex Elderb9434c52013-04-19 15:34:50 -05001744 else
1745 zero_pages(obj_request->pages, 0, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001746 obj_request->result = 0;
Alex Elderb9434c52013-04-19 15:34:50 -05001747 } else if (xferred < length && !obj_request->result) {
1748 if (obj_request->type == OBJ_REQUEST_BIO)
Ilya Dryomov5359a172018-01-20 10:30:10 +01001749 zero_bios(&obj_request->bio_pos, xferred,
1750 length - xferred);
Alex Elderb9434c52013-04-19 15:34:50 -05001751 else
1752 zero_pages(obj_request->pages, xferred, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001753 }
Josh Durgin17c1cc12013-08-26 17:55:38 -07001754 obj_request->xferred = length;
Alex Elder6e2a4502013-03-27 09:16:30 -05001755 obj_request_done_set(obj_request);
1756}
1757
Alex Elderbf0d5f502012-11-22 00:00:08 -06001758static void rbd_obj_request_complete(struct rbd_obj_request *obj_request)
1759{
Alex Elder37206ee2013-02-20 17:32:08 -06001760 dout("%s: obj %p cb %p\n", __func__, obj_request,
1761 obj_request->callback);
Ilya Dryomov2e584bc2018-01-15 17:24:51 +01001762 obj_request->callback(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001763}
1764
Ilya Dryomov0dcc6852016-09-26 15:43:52 +02001765static void rbd_obj_request_error(struct rbd_obj_request *obj_request, int err)
1766{
1767 obj_request->result = err;
1768 obj_request->xferred = 0;
1769 /*
1770 * kludge - mirror rbd_obj_request_submit() to match a put in
1771 * rbd_img_obj_callback()
1772 */
1773 if (obj_request_img_data_test(obj_request)) {
1774 WARN_ON(obj_request->callback != rbd_img_obj_callback);
1775 rbd_img_request_get(obj_request->img_request);
1776 }
1777 obj_request_done_set(obj_request);
1778 rbd_obj_request_complete(obj_request);
1779}
1780
Alex Elderc47f9372013-02-26 14:23:07 -06001781static void rbd_osd_read_callback(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001782{
Alex Elder57acbaa2013-02-11 12:33:24 -06001783 struct rbd_img_request *img_request = NULL;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001784 struct rbd_device *rbd_dev = NULL;
Alex Elder57acbaa2013-02-11 12:33:24 -06001785 bool layered = false;
1786
1787 if (obj_request_img_data_test(obj_request)) {
1788 img_request = obj_request->img_request;
1789 layered = img_request && img_request_layered_test(img_request);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001790 rbd_dev = img_request->rbd_dev;
Alex Elder57acbaa2013-02-11 12:33:24 -06001791 }
Alex Elder8b3e1a52013-01-24 16:13:36 -06001792
1793 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1794 obj_request, img_request, obj_request->result,
1795 obj_request->xferred, obj_request->length);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001796 if (layered && obj_request->result == -ENOENT &&
1797 obj_request->img_offset < rbd_dev->parent_overlap)
Alex Elder8b3e1a52013-01-24 16:13:36 -06001798 rbd_img_parent_read(obj_request);
1799 else if (img_request)
Alex Elder6e2a4502013-03-27 09:16:30 -05001800 rbd_img_obj_request_read_callback(obj_request);
1801 else
1802 obj_request_done_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001803}
1804
Alex Elderc47f9372013-02-26 14:23:07 -06001805static void rbd_osd_write_callback(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001806{
Sage Weil1b83bef2013-02-25 16:11:12 -08001807 dout("%s: obj %p result %d %llu\n", __func__, obj_request,
1808 obj_request->result, obj_request->length);
1809 /*
Alex Elder8b3e1a52013-01-24 16:13:36 -06001810 * There is no such thing as a successful short write. Set
1811 * it to our originally-requested length.
Sage Weil1b83bef2013-02-25 16:11:12 -08001812 */
1813 obj_request->xferred = obj_request->length;
Alex Elder07741302013-02-05 23:41:50 -06001814 obj_request_done_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001815}
1816
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001817static void rbd_osd_discard_callback(struct rbd_obj_request *obj_request)
1818{
1819 dout("%s: obj %p result %d %llu\n", __func__, obj_request,
1820 obj_request->result, obj_request->length);
1821 /*
1822 * There is no such thing as a successful short discard. Set
1823 * it to our originally-requested length.
1824 */
1825 obj_request->xferred = obj_request->length;
Josh Durgind0265de2014-04-07 16:54:10 -07001826 /* discarding a non-existent object is not a problem */
1827 if (obj_request->result == -ENOENT)
1828 obj_request->result = 0;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001829 obj_request_done_set(obj_request);
1830}
1831
Alex Elderfbfab532013-02-08 09:55:48 -06001832/*
1833 * For a simple stat call there's nothing to do. We'll do more if
1834 * this is part of a write sequence for a layered image.
1835 */
Alex Elderc47f9372013-02-26 14:23:07 -06001836static void rbd_osd_stat_callback(struct rbd_obj_request *obj_request)
Alex Elderfbfab532013-02-08 09:55:48 -06001837{
Alex Elder37206ee2013-02-20 17:32:08 -06001838 dout("%s: obj %p\n", __func__, obj_request);
Alex Elderfbfab532013-02-08 09:55:48 -06001839 obj_request_done_set(obj_request);
1840}
1841
Ilya Dryomov27617132015-07-16 17:36:11 +03001842static void rbd_osd_call_callback(struct rbd_obj_request *obj_request)
1843{
1844 dout("%s: obj %p\n", __func__, obj_request);
1845
1846 if (obj_request_img_data_test(obj_request))
1847 rbd_osd_copyup_callback(obj_request);
1848 else
1849 obj_request_done_set(obj_request);
1850}
1851
Ilya Dryomov85e084f2016-04-28 16:07:24 +02001852static void rbd_osd_req_callback(struct ceph_osd_request *osd_req)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001853{
1854 struct rbd_obj_request *obj_request = osd_req->r_priv;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001855 u16 opcode;
1856
Ilya Dryomov85e084f2016-04-28 16:07:24 +02001857 dout("%s: osd_req %p\n", __func__, osd_req);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001858 rbd_assert(osd_req == obj_request->osd_req);
Alex Elder57acbaa2013-02-11 12:33:24 -06001859 if (obj_request_img_data_test(obj_request)) {
1860 rbd_assert(obj_request->img_request);
1861 rbd_assert(obj_request->which != BAD_WHICH);
1862 } else {
1863 rbd_assert(obj_request->which == BAD_WHICH);
1864 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06001865
Sage Weil1b83bef2013-02-25 16:11:12 -08001866 if (osd_req->r_result < 0)
1867 obj_request->result = osd_req->r_result;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001868
Alex Elderc47f9372013-02-26 14:23:07 -06001869 /*
1870 * We support a 64-bit length, but ultimately it has to be
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01001871 * passed to the block layer, which just supports a 32-bit
1872 * length field.
Alex Elderc47f9372013-02-26 14:23:07 -06001873 */
Yan, Zheng7665d852016-01-07 16:48:57 +08001874 obj_request->xferred = osd_req->r_ops[0].outdata_len;
Alex Elder8b3e1a52013-01-24 16:13:36 -06001875 rbd_assert(obj_request->xferred < (u64)UINT_MAX);
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001876
Alex Elder79528732013-04-03 21:32:51 -05001877 opcode = osd_req->r_ops[0].op;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001878 switch (opcode) {
1879 case CEPH_OSD_OP_READ:
Alex Elderc47f9372013-02-26 14:23:07 -06001880 rbd_osd_read_callback(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001881 break;
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001882 case CEPH_OSD_OP_SETALLOCHINT:
Ilya Dryomove30b7572015-10-07 17:27:17 +02001883 rbd_assert(osd_req->r_ops[1].op == CEPH_OSD_OP_WRITE ||
1884 osd_req->r_ops[1].op == CEPH_OSD_OP_WRITEFULL);
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001885 /* fall through */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001886 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +02001887 case CEPH_OSD_OP_WRITEFULL:
Alex Elderc47f9372013-02-26 14:23:07 -06001888 rbd_osd_write_callback(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001889 break;
Alex Elderfbfab532013-02-08 09:55:48 -06001890 case CEPH_OSD_OP_STAT:
Alex Elderc47f9372013-02-26 14:23:07 -06001891 rbd_osd_stat_callback(obj_request);
Alex Elderfbfab532013-02-08 09:55:48 -06001892 break;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001893 case CEPH_OSD_OP_DELETE:
1894 case CEPH_OSD_OP_TRUNCATE:
1895 case CEPH_OSD_OP_ZERO:
1896 rbd_osd_discard_callback(obj_request);
1897 break;
Alex Elder36be9a72013-01-19 00:30:28 -06001898 case CEPH_OSD_OP_CALL:
Ilya Dryomov27617132015-07-16 17:36:11 +03001899 rbd_osd_call_callback(obj_request);
1900 break;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001901 default:
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01001902 rbd_warn(NULL, "unexpected OSD op: object_no %016llx opcode %d",
1903 obj_request->object_no, opcode);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001904 break;
1905 }
1906
Alex Elder07741302013-02-05 23:41:50 -06001907 if (obj_request_done_test(obj_request))
Alex Elderbf0d5f502012-11-22 00:00:08 -06001908 rbd_obj_request_complete(obj_request);
1909}
1910
Alex Elder9d4df012013-04-19 15:34:50 -05001911static void rbd_osd_req_format_read(struct rbd_obj_request *obj_request)
Alex Elder430c28c2013-04-03 21:32:51 -05001912{
Alex Elder8c042b02013-04-03 01:28:58 -05001913 struct ceph_osd_request *osd_req = obj_request->osd_req;
Alex Elder430c28c2013-04-03 21:32:51 -05001914
Ilya Dryomov7c848832016-09-15 17:56:39 +02001915 rbd_assert(obj_request_img_data_test(obj_request));
1916 osd_req->r_snapid = obj_request->img_request->snap_id;
Alex Elder9d4df012013-04-19 15:34:50 -05001917}
1918
1919static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request)
1920{
Alex Elder9d4df012013-04-19 15:34:50 -05001921 struct ceph_osd_request *osd_req = obj_request->osd_req;
Alex Elder9d4df012013-04-19 15:34:50 -05001922
Deepa Dinamani1134e092017-05-08 15:59:19 -07001923 ktime_get_real_ts(&osd_req->r_mtime);
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001924 osd_req->r_data_offset = obj_request->offset;
Alex Elder430c28c2013-04-03 21:32:51 -05001925}
1926
Ilya Dryomovbc812072017-01-25 18:16:23 +01001927static struct ceph_osd_request *
1928__rbd_osd_req_create(struct rbd_device *rbd_dev,
1929 struct ceph_snap_context *snapc,
1930 int num_ops, unsigned int flags,
1931 struct rbd_obj_request *obj_request)
1932{
1933 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
1934 struct ceph_osd_request *req;
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01001935 const char *name_format = rbd_dev->image_format == 1 ?
1936 RBD_V1_DATA_FORMAT : RBD_V2_DATA_FORMAT;
Ilya Dryomovbc812072017-01-25 18:16:23 +01001937
1938 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, false, GFP_NOIO);
1939 if (!req)
1940 return NULL;
1941
1942 req->r_flags = flags;
1943 req->r_callback = rbd_osd_req_callback;
1944 req->r_priv = obj_request;
1945
1946 req->r_base_oloc.pool = rbd_dev->layout.pool_id;
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01001947 if (ceph_oid_aprintf(&req->r_base_oid, GFP_NOIO, name_format,
1948 rbd_dev->header.object_prefix, obj_request->object_no))
Ilya Dryomovbc812072017-01-25 18:16:23 +01001949 goto err_req;
1950
1951 if (ceph_osdc_alloc_messages(req, GFP_NOIO))
1952 goto err_req;
1953
1954 return req;
1955
1956err_req:
1957 ceph_osdc_put_request(req);
1958 return NULL;
1959}
1960
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001961/*
1962 * Create an osd request. A read request has one osd op (read).
1963 * A write request has either one (watch) or two (hint+write) osd ops.
1964 * (All rbd data writes are prefixed with an allocation hint op, but
1965 * technically osd watch is a write request, hence this distinction.)
1966 */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001967static struct ceph_osd_request *rbd_osd_req_create(
1968 struct rbd_device *rbd_dev,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08001969 enum obj_operation_type op_type,
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02001970 unsigned int num_ops,
Alex Elder430c28c2013-04-03 21:32:51 -05001971 struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001972{
Alex Elderbf0d5f502012-11-22 00:00:08 -06001973 struct ceph_snap_context *snapc = NULL;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001974
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001975 if (obj_request_img_data_test(obj_request) &&
1976 (op_type == OBJ_OP_DISCARD || op_type == OBJ_OP_WRITE)) {
Alex Elder6365d332013-02-11 12:33:24 -06001977 struct rbd_img_request *img_request = obj_request->img_request;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001978 if (op_type == OBJ_OP_WRITE) {
1979 rbd_assert(img_request_write_test(img_request));
1980 } else {
1981 rbd_assert(img_request_discard_test(img_request));
1982 }
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08001983 snapc = img_request->snapc;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001984 }
1985
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08001986 rbd_assert(num_ops == 1 || ((op_type == OBJ_OP_WRITE) && num_ops == 2));
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02001987
Ilya Dryomovbc812072017-01-25 18:16:23 +01001988 return __rbd_osd_req_create(rbd_dev, snapc, num_ops,
1989 (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD) ?
Ilya Dryomov54ea0042017-02-11 18:48:41 +01001990 CEPH_OSD_FLAG_WRITE : CEPH_OSD_FLAG_READ, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001991}
1992
Alex Elder0eefd472013-04-19 15:34:50 -05001993/*
Josh Durgind3246fb2014-04-07 16:49:21 -07001994 * Create a copyup osd request based on the information in the object
1995 * request supplied. A copyup request has two or three osd ops, a
1996 * copyup method call, potentially a hint op, and a write or truncate
1997 * or zero op.
Alex Elder0eefd472013-04-19 15:34:50 -05001998 */
1999static struct ceph_osd_request *
2000rbd_osd_req_create_copyup(struct rbd_obj_request *obj_request)
2001{
2002 struct rbd_img_request *img_request;
Josh Durgind3246fb2014-04-07 16:49:21 -07002003 int num_osd_ops = 3;
Alex Elder0eefd472013-04-19 15:34:50 -05002004
2005 rbd_assert(obj_request_img_data_test(obj_request));
2006 img_request = obj_request->img_request;
2007 rbd_assert(img_request);
Josh Durgind3246fb2014-04-07 16:49:21 -07002008 rbd_assert(img_request_write_test(img_request) ||
2009 img_request_discard_test(img_request));
Alex Elder0eefd472013-04-19 15:34:50 -05002010
Josh Durgind3246fb2014-04-07 16:49:21 -07002011 if (img_request_discard_test(img_request))
2012 num_osd_ops = 2;
2013
Ilya Dryomovbc812072017-01-25 18:16:23 +01002014 return __rbd_osd_req_create(img_request->rbd_dev,
2015 img_request->snapc, num_osd_ops,
Ilya Dryomov54ea0042017-02-11 18:48:41 +01002016 CEPH_OSD_FLAG_WRITE, obj_request);
Alex Elder0eefd472013-04-19 15:34:50 -05002017}
2018
Alex Elderbf0d5f502012-11-22 00:00:08 -06002019static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req)
2020{
2021 ceph_osdc_put_request(osd_req);
2022}
2023
Ilya Dryomov6c696d82017-01-25 18:16:23 +01002024static struct rbd_obj_request *
2025rbd_obj_request_create(enum obj_request_type type)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002026{
2027 struct rbd_obj_request *obj_request;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002028
2029 rbd_assert(obj_request_type_valid(type));
2030
Ilya Dryomov5a60e872015-06-24 17:24:33 +03002031 obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_NOIO);
Ilya Dryomov6c696d82017-01-25 18:16:23 +01002032 if (!obj_request)
Alex Elderf907ad52013-05-01 12:43:03 -05002033 return NULL;
Alex Elderf907ad52013-05-01 12:43:03 -05002034
Alex Elderbf0d5f502012-11-22 00:00:08 -06002035 obj_request->which = BAD_WHICH;
2036 obj_request->type = type;
2037 INIT_LIST_HEAD(&obj_request->links);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002038 kref_init(&obj_request->kref);
2039
Ilya Dryomov67e2b652017-01-25 18:16:22 +01002040 dout("%s %p\n", __func__, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002041 return obj_request;
2042}
2043
2044static void rbd_obj_request_destroy(struct kref *kref)
2045{
2046 struct rbd_obj_request *obj_request;
2047
2048 obj_request = container_of(kref, struct rbd_obj_request, kref);
2049
Alex Elder37206ee2013-02-20 17:32:08 -06002050 dout("%s: obj %p\n", __func__, obj_request);
2051
Alex Elderbf0d5f502012-11-22 00:00:08 -06002052 rbd_assert(obj_request->img_request == NULL);
2053 rbd_assert(obj_request->which == BAD_WHICH);
2054
2055 if (obj_request->osd_req)
2056 rbd_osd_req_destroy(obj_request->osd_req);
2057
2058 rbd_assert(obj_request_type_valid(obj_request->type));
2059 switch (obj_request->type) {
Alex Elder9969ebc2013-01-18 12:31:10 -06002060 case OBJ_REQUEST_NODATA:
Alex Elderbf0d5f502012-11-22 00:00:08 -06002061 case OBJ_REQUEST_BIO:
Ilya Dryomov5359a172018-01-20 10:30:10 +01002062 break; /* Nothing to do */
Alex Elder788e2df2013-01-17 12:25:27 -06002063 case OBJ_REQUEST_PAGES:
Ilya Dryomov04dc9232016-09-15 18:05:16 +02002064 /* img_data requests don't own their page array */
2065 if (obj_request->pages &&
2066 !obj_request_img_data_test(obj_request))
Alex Elder788e2df2013-01-17 12:25:27 -06002067 ceph_release_page_vector(obj_request->pages,
2068 obj_request->page_count);
2069 break;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002070 }
2071
Alex Elder868311b2013-05-01 12:43:03 -05002072 kmem_cache_free(rbd_obj_request_cache, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002073}
2074
Alex Elderfb65d2282013-05-08 22:50:04 -05002075/* It's OK to call this for a device with no parent */
2076
2077static void rbd_spec_put(struct rbd_spec *spec);
2078static void rbd_dev_unparent(struct rbd_device *rbd_dev)
2079{
2080 rbd_dev_remove_parent(rbd_dev);
2081 rbd_spec_put(rbd_dev->parent_spec);
2082 rbd_dev->parent_spec = NULL;
2083 rbd_dev->parent_overlap = 0;
2084}
2085
Alex Elderbf0d5f502012-11-22 00:00:08 -06002086/*
Alex Eldera2acd002013-05-08 22:50:04 -05002087 * Parent image reference counting is used to determine when an
2088 * image's parent fields can be safely torn down--after there are no
2089 * more in-flight requests to the parent image. When the last
2090 * reference is dropped, cleaning them up is safe.
2091 */
2092static void rbd_dev_parent_put(struct rbd_device *rbd_dev)
2093{
2094 int counter;
2095
2096 if (!rbd_dev->parent_spec)
2097 return;
2098
2099 counter = atomic_dec_return_safe(&rbd_dev->parent_ref);
2100 if (counter > 0)
2101 return;
2102
2103 /* Last reference; clean up parent data structures */
2104
2105 if (!counter)
2106 rbd_dev_unparent(rbd_dev);
2107 else
Ilya Dryomov9584d502014-07-11 12:11:20 +04002108 rbd_warn(rbd_dev, "parent reference underflow");
Alex Eldera2acd002013-05-08 22:50:04 -05002109}
2110
2111/*
2112 * If an image has a non-zero parent overlap, get a reference to its
2113 * parent.
2114 *
2115 * Returns true if the rbd device has a parent with a non-zero
2116 * overlap and a reference for it was successfully taken, or
2117 * false otherwise.
2118 */
2119static bool rbd_dev_parent_get(struct rbd_device *rbd_dev)
2120{
Ilya Dryomovae43e9d2015-01-19 18:13:43 +03002121 int counter = 0;
Alex Eldera2acd002013-05-08 22:50:04 -05002122
2123 if (!rbd_dev->parent_spec)
2124 return false;
2125
Ilya Dryomovae43e9d2015-01-19 18:13:43 +03002126 down_read(&rbd_dev->header_rwsem);
2127 if (rbd_dev->parent_overlap)
2128 counter = atomic_inc_return_safe(&rbd_dev->parent_ref);
2129 up_read(&rbd_dev->header_rwsem);
Alex Eldera2acd002013-05-08 22:50:04 -05002130
2131 if (counter < 0)
Ilya Dryomov9584d502014-07-11 12:11:20 +04002132 rbd_warn(rbd_dev, "parent reference overflow");
Alex Eldera2acd002013-05-08 22:50:04 -05002133
Ilya Dryomovae43e9d2015-01-19 18:13:43 +03002134 return counter > 0;
Alex Eldera2acd002013-05-08 22:50:04 -05002135}
2136
Alex Elderbf0d5f502012-11-22 00:00:08 -06002137/*
2138 * Caller is responsible for filling in the list of object requests
2139 * that comprises the image request, and the Linux request pointer
2140 * (if there is one).
2141 */
Alex Eldercc344fa2013-02-19 12:25:56 -06002142static struct rbd_img_request *rbd_img_request_create(
2143 struct rbd_device *rbd_dev,
Alex Elderbf0d5f502012-11-22 00:00:08 -06002144 u64 offset, u64 length,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002145 enum obj_operation_type op_type,
Josh Durgin4e752f02014-04-08 11:12:11 -07002146 struct ceph_snap_context *snapc)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002147{
2148 struct rbd_img_request *img_request;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002149
Ilya Dryomova0c58952018-01-22 16:03:06 +01002150 img_request = kmem_cache_zalloc(rbd_img_request_cache, GFP_NOIO);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002151 if (!img_request)
2152 return NULL;
2153
Alex Elderbf0d5f502012-11-22 00:00:08 -06002154 img_request->rbd_dev = rbd_dev;
2155 img_request->offset = offset;
2156 img_request->length = length;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002157 if (op_type == OBJ_OP_DISCARD) {
2158 img_request_discard_set(img_request);
2159 img_request->snapc = snapc;
2160 } else if (op_type == OBJ_OP_WRITE) {
Alex Elder0c425242013-02-08 09:55:49 -06002161 img_request_write_set(img_request);
Josh Durgin4e752f02014-04-08 11:12:11 -07002162 img_request->snapc = snapc;
Alex Elder0c425242013-02-08 09:55:49 -06002163 } else {
Alex Elderbf0d5f502012-11-22 00:00:08 -06002164 img_request->snap_id = rbd_dev->spec->snap_id;
Alex Elder0c425242013-02-08 09:55:49 -06002165 }
Alex Eldera2acd002013-05-08 22:50:04 -05002166 if (rbd_dev_parent_get(rbd_dev))
Alex Elderd0b2e942013-01-24 16:13:36 -06002167 img_request_layered_set(img_request);
Ilya Dryomova0c58952018-01-22 16:03:06 +01002168
Alex Elderbf0d5f502012-11-22 00:00:08 -06002169 spin_lock_init(&img_request->completion_lock);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002170 INIT_LIST_HEAD(&img_request->obj_requests);
2171 kref_init(&img_request->kref);
2172
Alex Elder37206ee2013-02-20 17:32:08 -06002173 dout("%s: rbd_dev %p %s %llu/%llu -> img %p\n", __func__, rbd_dev,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002174 obj_op_name(op_type), offset, length, img_request);
Alex Elder37206ee2013-02-20 17:32:08 -06002175
Alex Elderbf0d5f502012-11-22 00:00:08 -06002176 return img_request;
2177}
2178
2179static void rbd_img_request_destroy(struct kref *kref)
2180{
2181 struct rbd_img_request *img_request;
2182 struct rbd_obj_request *obj_request;
2183 struct rbd_obj_request *next_obj_request;
2184
2185 img_request = container_of(kref, struct rbd_img_request, kref);
2186
Alex Elder37206ee2013-02-20 17:32:08 -06002187 dout("%s: img %p\n", __func__, img_request);
2188
Alex Elderbf0d5f502012-11-22 00:00:08 -06002189 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
2190 rbd_img_obj_request_del(img_request, obj_request);
Alex Elder25dcf952013-01-25 17:08:55 -06002191 rbd_assert(img_request->obj_request_count == 0);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002192
Alex Eldera2acd002013-05-08 22:50:04 -05002193 if (img_request_layered_test(img_request)) {
2194 img_request_layered_clear(img_request);
2195 rbd_dev_parent_put(img_request->rbd_dev);
2196 }
2197
Josh Durginbef95452014-04-04 17:47:52 -07002198 if (img_request_write_test(img_request) ||
2199 img_request_discard_test(img_request))
Alex Elder812164f82013-04-30 00:44:32 -05002200 ceph_put_snap_context(img_request->snapc);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002201
Alex Elder1c2a9df2013-05-01 12:43:03 -05002202 kmem_cache_free(rbd_img_request_cache, img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002203}
2204
Alex Eldere93f3152013-05-08 22:50:04 -05002205static struct rbd_img_request *rbd_parent_request_create(
2206 struct rbd_obj_request *obj_request,
2207 u64 img_offset, u64 length)
2208{
2209 struct rbd_img_request *parent_request;
2210 struct rbd_device *rbd_dev;
2211
2212 rbd_assert(obj_request->img_request);
2213 rbd_dev = obj_request->img_request->rbd_dev;
2214
Josh Durgin4e752f02014-04-08 11:12:11 -07002215 parent_request = rbd_img_request_create(rbd_dev->parent, img_offset,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002216 length, OBJ_OP_READ, NULL);
Alex Eldere93f3152013-05-08 22:50:04 -05002217 if (!parent_request)
2218 return NULL;
2219
2220 img_request_child_set(parent_request);
2221 rbd_obj_request_get(obj_request);
2222 parent_request->obj_request = obj_request;
2223
2224 return parent_request;
2225}
2226
2227static void rbd_parent_request_destroy(struct kref *kref)
2228{
2229 struct rbd_img_request *parent_request;
2230 struct rbd_obj_request *orig_request;
2231
2232 parent_request = container_of(kref, struct rbd_img_request, kref);
2233 orig_request = parent_request->obj_request;
2234
2235 parent_request->obj_request = NULL;
2236 rbd_obj_request_put(orig_request);
2237 img_request_child_clear(parent_request);
2238
2239 rbd_img_request_destroy(kref);
2240}
2241
Alex Elder12178572013-02-08 09:55:49 -06002242static bool rbd_img_obj_end_request(struct rbd_obj_request *obj_request)
2243{
Alex Elder6365d332013-02-11 12:33:24 -06002244 struct rbd_img_request *img_request;
Alex Elder12178572013-02-08 09:55:49 -06002245 unsigned int xferred;
2246 int result;
Alex Elder8b3e1a52013-01-24 16:13:36 -06002247 bool more;
Alex Elder12178572013-02-08 09:55:49 -06002248
Alex Elder6365d332013-02-11 12:33:24 -06002249 rbd_assert(obj_request_img_data_test(obj_request));
2250 img_request = obj_request->img_request;
2251
Alex Elder12178572013-02-08 09:55:49 -06002252 rbd_assert(obj_request->xferred <= (u64)UINT_MAX);
2253 xferred = (unsigned int)obj_request->xferred;
2254 result = obj_request->result;
2255 if (result) {
2256 struct rbd_device *rbd_dev = img_request->rbd_dev;
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002257 enum obj_operation_type op_type;
2258
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002259 if (img_request_discard_test(img_request))
2260 op_type = OBJ_OP_DISCARD;
2261 else if (img_request_write_test(img_request))
2262 op_type = OBJ_OP_WRITE;
2263 else
2264 op_type = OBJ_OP_READ;
Alex Elder12178572013-02-08 09:55:49 -06002265
Ilya Dryomov9584d502014-07-11 12:11:20 +04002266 rbd_warn(rbd_dev, "%s %llx at %llx (%llx)",
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002267 obj_op_name(op_type), obj_request->length,
2268 obj_request->img_offset, obj_request->offset);
Ilya Dryomov9584d502014-07-11 12:11:20 +04002269 rbd_warn(rbd_dev, " result %d xferred %x",
Alex Elder12178572013-02-08 09:55:49 -06002270 result, xferred);
2271 if (!img_request->result)
2272 img_request->result = result;
Ilya Dryomov082a75d2015-04-25 15:56:15 +03002273 /*
2274 * Need to end I/O on the entire obj_request worth of
2275 * bytes in case of error.
2276 */
2277 xferred = obj_request->length;
Alex Elder12178572013-02-08 09:55:49 -06002278 }
2279
Alex Elder8b3e1a52013-01-24 16:13:36 -06002280 if (img_request_child_test(img_request)) {
2281 rbd_assert(img_request->obj_request != NULL);
2282 more = obj_request->which < img_request->obj_request_count - 1;
2283 } else {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002284 blk_status_t status = errno_to_blk_status(result);
2285
Alex Elder8b3e1a52013-01-24 16:13:36 -06002286 rbd_assert(img_request->rq != NULL);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01002287
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002288 more = blk_update_request(img_request->rq, status, xferred);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01002289 if (!more)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002290 __blk_mq_end_request(img_request->rq, status);
Alex Elder8b3e1a52013-01-24 16:13:36 -06002291 }
2292
2293 return more;
Alex Elder12178572013-02-08 09:55:49 -06002294}
2295
Alex Elder21692382013-04-05 01:27:12 -05002296static void rbd_img_obj_callback(struct rbd_obj_request *obj_request)
2297{
2298 struct rbd_img_request *img_request;
2299 u32 which = obj_request->which;
2300 bool more = true;
2301
Alex Elder6365d332013-02-11 12:33:24 -06002302 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elder21692382013-04-05 01:27:12 -05002303 img_request = obj_request->img_request;
2304
2305 dout("%s: img %p obj %p\n", __func__, img_request, obj_request);
2306 rbd_assert(img_request != NULL);
Alex Elder21692382013-04-05 01:27:12 -05002307 rbd_assert(img_request->obj_request_count > 0);
2308 rbd_assert(which != BAD_WHICH);
2309 rbd_assert(which < img_request->obj_request_count);
Alex Elder21692382013-04-05 01:27:12 -05002310
2311 spin_lock_irq(&img_request->completion_lock);
2312 if (which != img_request->next_completion)
2313 goto out;
2314
2315 for_each_obj_request_from(img_request, obj_request) {
Alex Elder21692382013-04-05 01:27:12 -05002316 rbd_assert(more);
2317 rbd_assert(which < img_request->obj_request_count);
2318
2319 if (!obj_request_done_test(obj_request))
2320 break;
Alex Elder12178572013-02-08 09:55:49 -06002321 more = rbd_img_obj_end_request(obj_request);
Alex Elder21692382013-04-05 01:27:12 -05002322 which++;
2323 }
2324
2325 rbd_assert(more ^ (which == img_request->obj_request_count));
2326 img_request->next_completion = which;
2327out:
2328 spin_unlock_irq(&img_request->completion_lock);
Alex Elder0f2d5be2014-04-26 14:21:44 +04002329 rbd_img_request_put(img_request);
Alex Elder21692382013-04-05 01:27:12 -05002330
2331 if (!more)
2332 rbd_img_request_complete(img_request);
2333}
2334
Alex Elderf1a47392013-04-19 15:34:50 -05002335/*
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002336 * Add individual osd ops to the given ceph_osd_request and prepare
2337 * them for submission. num_ops is the current number of
2338 * osd operations already to the object request.
2339 */
2340static void rbd_img_obj_request_fill(struct rbd_obj_request *obj_request,
2341 struct ceph_osd_request *osd_request,
2342 enum obj_operation_type op_type,
2343 unsigned int num_ops)
2344{
2345 struct rbd_img_request *img_request = obj_request->img_request;
2346 struct rbd_device *rbd_dev = img_request->rbd_dev;
2347 u64 object_size = rbd_obj_bytes(&rbd_dev->header);
2348 u64 offset = obj_request->offset;
2349 u64 length = obj_request->length;
2350 u64 img_end;
2351 u16 opcode;
2352
2353 if (op_type == OBJ_OP_DISCARD) {
Josh Durgind3246fb2014-04-07 16:49:21 -07002354 if (!offset && length == object_size &&
2355 (!img_request_layered_test(img_request) ||
2356 !obj_request_overlaps_parent(obj_request))) {
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002357 opcode = CEPH_OSD_OP_DELETE;
2358 } else if ((offset + length == object_size)) {
2359 opcode = CEPH_OSD_OP_TRUNCATE;
2360 } else {
2361 down_read(&rbd_dev->header_rwsem);
2362 img_end = rbd_dev->header.image_size;
2363 up_read(&rbd_dev->header_rwsem);
2364
2365 if (obj_request->img_offset + length == img_end)
2366 opcode = CEPH_OSD_OP_TRUNCATE;
2367 else
2368 opcode = CEPH_OSD_OP_ZERO;
2369 }
2370 } else if (op_type == OBJ_OP_WRITE) {
Ilya Dryomove30b7572015-10-07 17:27:17 +02002371 if (!offset && length == object_size)
2372 opcode = CEPH_OSD_OP_WRITEFULL;
2373 else
2374 opcode = CEPH_OSD_OP_WRITE;
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002375 osd_req_op_alloc_hint_init(osd_request, num_ops,
2376 object_size, object_size);
2377 num_ops++;
2378 } else {
2379 opcode = CEPH_OSD_OP_READ;
2380 }
2381
Ilya Dryomov7e868b62014-11-21 22:16:43 +03002382 if (opcode == CEPH_OSD_OP_DELETE)
Yan, Zheng144cba12015-04-27 11:09:54 +08002383 osd_req_op_init(osd_request, num_ops, opcode, 0);
Ilya Dryomov7e868b62014-11-21 22:16:43 +03002384 else
2385 osd_req_op_extent_init(osd_request, num_ops, opcode,
2386 offset, length, 0, 0);
2387
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002388 if (obj_request->type == OBJ_REQUEST_BIO)
2389 osd_req_op_extent_osd_data_bio(osd_request, num_ops,
Ilya Dryomov5359a172018-01-20 10:30:10 +01002390 &obj_request->bio_pos, length);
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002391 else if (obj_request->type == OBJ_REQUEST_PAGES)
2392 osd_req_op_extent_osd_data_pages(osd_request, num_ops,
2393 obj_request->pages, length,
2394 offset & ~PAGE_MASK, false, false);
2395
2396 /* Discards are also writes */
2397 if (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD)
2398 rbd_osd_req_format_write(obj_request);
2399 else
2400 rbd_osd_req_format_read(obj_request);
2401}
2402
2403/*
Alex Elderf1a47392013-04-19 15:34:50 -05002404 * Split up an image request into one or more object requests, each
2405 * to a different object. The "type" parameter indicates whether
2406 * "data_desc" is the pointer to the head of a list of bio
2407 * structures, or the base of a page array. In either case this
2408 * function assumes data_desc describes memory sufficient to hold
2409 * all data described by the image request.
2410 */
2411static int rbd_img_request_fill(struct rbd_img_request *img_request,
2412 enum obj_request_type type,
2413 void *data_desc)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002414{
2415 struct rbd_device *rbd_dev = img_request->rbd_dev;
2416 struct rbd_obj_request *obj_request = NULL;
2417 struct rbd_obj_request *next_obj_request;
Ilya Dryomov5359a172018-01-20 10:30:10 +01002418 struct ceph_bio_iter bio_it;
Jingoo Hana1580732013-08-09 13:04:35 +09002419 struct page **pages = NULL;
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002420 enum obj_operation_type op_type;
Alex Elder7da22d22013-01-24 16:13:36 -06002421 u64 img_offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002422 u64 resid;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002423
Alex Elderf1a47392013-04-19 15:34:50 -05002424 dout("%s: img %p type %d data_desc %p\n", __func__, img_request,
2425 (int)type, data_desc);
Alex Elder37206ee2013-02-20 17:32:08 -06002426
Alex Elder7da22d22013-01-24 16:13:36 -06002427 img_offset = img_request->offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002428 resid = img_request->length;
Alex Elder4dda41d2013-02-20 21:59:33 -06002429 rbd_assert(resid > 0);
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002430 op_type = rbd_img_request_op_type(img_request);
Alex Elderf1a47392013-04-19 15:34:50 -05002431
2432 if (type == OBJ_REQUEST_BIO) {
Ilya Dryomov5359a172018-01-20 10:30:10 +01002433 bio_it = *(struct ceph_bio_iter *)data_desc;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002434 rbd_assert(img_offset ==
Ilya Dryomov5359a172018-01-20 10:30:10 +01002435 bio_it.iter.bi_sector << SECTOR_SHIFT);
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002436 } else if (type == OBJ_REQUEST_PAGES) {
Alex Elderf1a47392013-04-19 15:34:50 -05002437 pages = data_desc;
2438 }
2439
Alex Elderbf0d5f502012-11-22 00:00:08 -06002440 while (resid) {
Alex Elder2fa12322013-04-05 01:27:12 -05002441 struct ceph_osd_request *osd_req;
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01002442 u64 object_no = img_offset >> rbd_dev->header.obj_order;
Ilya Dryomov67e2b652017-01-25 18:16:22 +01002443 u64 offset = rbd_segment_offset(rbd_dev, img_offset);
2444 u64 length = rbd_segment_length(rbd_dev, img_offset, resid);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002445
Ilya Dryomov6c696d82017-01-25 18:16:23 +01002446 obj_request = rbd_obj_request_create(type);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002447 if (!obj_request)
2448 goto out_unwind;
Ilya Dryomov62054da2014-03-04 11:57:17 +02002449
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01002450 obj_request->object_no = object_no;
Ilya Dryomov67e2b652017-01-25 18:16:22 +01002451 obj_request->offset = offset;
2452 obj_request->length = length;
2453
Josh Durgin03507db2013-08-27 14:45:46 -07002454 /*
2455 * set obj_request->img_request before creating the
2456 * osd_request so that it gets the right snapc
2457 */
2458 rbd_img_obj_request_add(img_request, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002459
Alex Elderf1a47392013-04-19 15:34:50 -05002460 if (type == OBJ_REQUEST_BIO) {
Ilya Dryomov5359a172018-01-20 10:30:10 +01002461 obj_request->bio_pos = bio_it;
2462 ceph_bio_iter_advance(&bio_it, length);
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002463 } else if (type == OBJ_REQUEST_PAGES) {
Alex Elderf1a47392013-04-19 15:34:50 -05002464 unsigned int page_count;
2465
2466 obj_request->pages = pages;
2467 page_count = (u32)calc_pages_for(offset, length);
2468 obj_request->page_count = page_count;
2469 if ((offset + length) & ~PAGE_MASK)
2470 page_count--; /* more on last page */
2471 pages += page_count;
2472 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06002473
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002474 osd_req = rbd_osd_req_create(rbd_dev, op_type,
2475 (op_type == OBJ_OP_WRITE) ? 2 : 1,
2476 obj_request);
Alex Elder2fa12322013-04-05 01:27:12 -05002477 if (!osd_req)
Ilya Dryomov62054da2014-03-04 11:57:17 +02002478 goto out_unwind;
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002479
Alex Elder2fa12322013-04-05 01:27:12 -05002480 obj_request->osd_req = osd_req;
Alex Elder21692382013-04-05 01:27:12 -05002481 obj_request->callback = rbd_img_obj_callback;
Alex Elder7da22d22013-01-24 16:13:36 -06002482 obj_request->img_offset = img_offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002483
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002484 rbd_img_obj_request_fill(obj_request, osd_req, op_type, 0);
2485
Alex Elder7da22d22013-01-24 16:13:36 -06002486 img_offset += length;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002487 resid -= length;
2488 }
2489
2490 return 0;
2491
Alex Elderbf0d5f502012-11-22 00:00:08 -06002492out_unwind:
2493 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
Ilya Dryomov42dd0372014-03-04 11:57:17 +02002494 rbd_img_obj_request_del(img_request, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002495
2496 return -ENOMEM;
2497}
2498
Alex Elder3d7efd12013-04-19 15:34:50 -05002499static void
Ilya Dryomov27617132015-07-16 17:36:11 +03002500rbd_osd_copyup_callback(struct rbd_obj_request *obj_request)
Alex Elder0eefd472013-04-19 15:34:50 -05002501{
2502 struct rbd_img_request *img_request;
2503 struct rbd_device *rbd_dev;
Alex Elderebda6402013-05-10 16:29:22 -05002504 struct page **pages;
Alex Elder0eefd472013-04-19 15:34:50 -05002505 u32 page_count;
2506
Ilya Dryomov27617132015-07-16 17:36:11 +03002507 dout("%s: obj %p\n", __func__, obj_request);
2508
Josh Durgind3246fb2014-04-07 16:49:21 -07002509 rbd_assert(obj_request->type == OBJ_REQUEST_BIO ||
2510 obj_request->type == OBJ_REQUEST_NODATA);
Alex Elder0eefd472013-04-19 15:34:50 -05002511 rbd_assert(obj_request_img_data_test(obj_request));
2512 img_request = obj_request->img_request;
2513 rbd_assert(img_request);
2514
2515 rbd_dev = img_request->rbd_dev;
2516 rbd_assert(rbd_dev);
Alex Elder0eefd472013-04-19 15:34:50 -05002517
Alex Elderebda6402013-05-10 16:29:22 -05002518 pages = obj_request->copyup_pages;
2519 rbd_assert(pages != NULL);
Alex Elder0eefd472013-04-19 15:34:50 -05002520 obj_request->copyup_pages = NULL;
Alex Elderebda6402013-05-10 16:29:22 -05002521 page_count = obj_request->copyup_page_count;
2522 rbd_assert(page_count);
2523 obj_request->copyup_page_count = 0;
2524 ceph_release_page_vector(pages, page_count);
Alex Elder0eefd472013-04-19 15:34:50 -05002525
2526 /*
2527 * We want the transfer count to reflect the size of the
2528 * original write request. There is no such thing as a
2529 * successful short write, so if the request was successful
2530 * we can just set it to the originally-requested length.
2531 */
2532 if (!obj_request->result)
2533 obj_request->xferred = obj_request->length;
2534
Ilya Dryomov27617132015-07-16 17:36:11 +03002535 obj_request_done_set(obj_request);
Alex Elder0eefd472013-04-19 15:34:50 -05002536}
2537
2538static void
Alex Elder3d7efd12013-04-19 15:34:50 -05002539rbd_img_obj_parent_read_full_callback(struct rbd_img_request *img_request)
2540{
2541 struct rbd_obj_request *orig_request;
Alex Elder0eefd472013-04-19 15:34:50 -05002542 struct ceph_osd_request *osd_req;
Alex Elder0eefd472013-04-19 15:34:50 -05002543 struct rbd_device *rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002544 struct page **pages;
Josh Durgind3246fb2014-04-07 16:49:21 -07002545 enum obj_operation_type op_type;
Alex Elderebda6402013-05-10 16:29:22 -05002546 u32 page_count;
Alex Elderbbea1c12013-05-06 17:40:33 -05002547 int img_result;
Alex Elderebda6402013-05-10 16:29:22 -05002548 u64 parent_length;
Alex Elder3d7efd12013-04-19 15:34:50 -05002549
2550 rbd_assert(img_request_child_test(img_request));
2551
2552 /* First get what we need from the image request */
2553
2554 pages = img_request->copyup_pages;
2555 rbd_assert(pages != NULL);
2556 img_request->copyup_pages = NULL;
Alex Elderebda6402013-05-10 16:29:22 -05002557 page_count = img_request->copyup_page_count;
2558 rbd_assert(page_count);
2559 img_request->copyup_page_count = 0;
Alex Elder3d7efd12013-04-19 15:34:50 -05002560
2561 orig_request = img_request->obj_request;
2562 rbd_assert(orig_request != NULL);
Alex Elderb91f09f2013-05-10 16:29:22 -05002563 rbd_assert(obj_request_type_valid(orig_request->type));
Alex Elderbbea1c12013-05-06 17:40:33 -05002564 img_result = img_request->result;
Alex Elderebda6402013-05-10 16:29:22 -05002565 parent_length = img_request->length;
Ilya Dryomovfa355112016-09-16 15:20:42 +02002566 rbd_assert(img_result || parent_length == img_request->xferred);
Alex Elder3d7efd12013-04-19 15:34:50 -05002567 rbd_img_request_put(img_request);
2568
Alex Elder91c6feb2013-05-06 17:40:32 -05002569 rbd_assert(orig_request->img_request);
2570 rbd_dev = orig_request->img_request->rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002571 rbd_assert(rbd_dev);
Alex Elder3d7efd12013-04-19 15:34:50 -05002572
Alex Elderbbea1c12013-05-06 17:40:33 -05002573 /*
2574 * If the overlap has become 0 (most likely because the
2575 * image has been flattened) we need to free the pages
2576 * and re-submit the original write request.
2577 */
2578 if (!rbd_dev->parent_overlap) {
Alex Elderbbea1c12013-05-06 17:40:33 -05002579 ceph_release_page_vector(pages, page_count);
Ilya Dryomov980917f2016-09-12 18:59:42 +02002580 rbd_obj_request_submit(orig_request);
2581 return;
Alex Elderbbea1c12013-05-06 17:40:33 -05002582 }
2583
2584 if (img_result)
Alex Elder0eefd472013-04-19 15:34:50 -05002585 goto out_err;
Alex Elder3d7efd12013-04-19 15:34:50 -05002586
Alex Elder8785b1d2013-05-09 10:08:49 -05002587 /*
2588 * The original osd request is of no use to use any more.
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02002589 * We need a new one that can hold the three ops in a copyup
Alex Elder8785b1d2013-05-09 10:08:49 -05002590 * request. Allocate the new copyup osd request for the
2591 * original request, and release the old one.
2592 */
Alex Elderbbea1c12013-05-06 17:40:33 -05002593 img_result = -ENOMEM;
Alex Elder0eefd472013-04-19 15:34:50 -05002594 osd_req = rbd_osd_req_create_copyup(orig_request);
2595 if (!osd_req)
2596 goto out_err;
Alex Elder8785b1d2013-05-09 10:08:49 -05002597 rbd_osd_req_destroy(orig_request->osd_req);
Alex Elder0eefd472013-04-19 15:34:50 -05002598 orig_request->osd_req = osd_req;
2599 orig_request->copyup_pages = pages;
Alex Elderebda6402013-05-10 16:29:22 -05002600 orig_request->copyup_page_count = page_count;
Alex Elder3d7efd12013-04-19 15:34:50 -05002601
Alex Elder0eefd472013-04-19 15:34:50 -05002602 /* Initialize the copyup op */
2603
2604 osd_req_op_cls_init(osd_req, 0, CEPH_OSD_OP_CALL, "rbd", "copyup");
Alex Elderebda6402013-05-10 16:29:22 -05002605 osd_req_op_cls_request_data_pages(osd_req, 0, pages, parent_length, 0,
Alex Elder0eefd472013-04-19 15:34:50 -05002606 false, false);
2607
Josh Durgind3246fb2014-04-07 16:49:21 -07002608 /* Add the other op(s) */
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02002609
Josh Durgind3246fb2014-04-07 16:49:21 -07002610 op_type = rbd_img_request_op_type(orig_request->img_request);
2611 rbd_img_obj_request_fill(orig_request, osd_req, op_type, 1);
Alex Elder0eefd472013-04-19 15:34:50 -05002612
2613 /* All set, send it off. */
2614
Ilya Dryomov980917f2016-09-12 18:59:42 +02002615 rbd_obj_request_submit(orig_request);
2616 return;
Alex Elder0eefd472013-04-19 15:34:50 -05002617
Alex Elder0eefd472013-04-19 15:34:50 -05002618out_err:
Ilya Dryomovfa355112016-09-16 15:20:42 +02002619 ceph_release_page_vector(pages, page_count);
Ilya Dryomov0dcc6852016-09-26 15:43:52 +02002620 rbd_obj_request_error(orig_request, img_result);
Alex Elder3d7efd12013-04-19 15:34:50 -05002621}
2622
2623/*
2624 * Read from the parent image the range of data that covers the
2625 * entire target of the given object request. This is used for
2626 * satisfying a layered image write request when the target of an
2627 * object request from the image request does not exist.
2628 *
2629 * A page array big enough to hold the returned data is allocated
2630 * and supplied to rbd_img_request_fill() as the "data descriptor."
2631 * When the read completes, this page array will be transferred to
2632 * the original object request for the copyup operation.
2633 *
Ilya Dryomovc2e82412016-09-13 20:18:01 +02002634 * If an error occurs, it is recorded as the result of the original
2635 * object request in rbd_img_obj_exists_callback().
Alex Elder3d7efd12013-04-19 15:34:50 -05002636 */
2637static int rbd_img_obj_parent_read_full(struct rbd_obj_request *obj_request)
2638{
Ilya Dryomov058aa992016-09-12 14:44:45 +02002639 struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002640 struct rbd_img_request *parent_request = NULL;
Alex Elder3d7efd12013-04-19 15:34:50 -05002641 u64 img_offset;
2642 u64 length;
2643 struct page **pages = NULL;
2644 u32 page_count;
2645 int result;
2646
Alex Elder3d7efd12013-04-19 15:34:50 -05002647 rbd_assert(rbd_dev->parent != NULL);
2648
2649 /*
2650 * Determine the byte range covered by the object in the
2651 * child image to which the original request was to be sent.
2652 */
2653 img_offset = obj_request->img_offset - obj_request->offset;
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +01002654 length = rbd_obj_bytes(&rbd_dev->header);
Alex Elder3d7efd12013-04-19 15:34:50 -05002655
2656 /*
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002657 * There is no defined parent data beyond the parent
2658 * overlap, so limit what we read at that boundary if
2659 * necessary.
2660 */
2661 if (img_offset + length > rbd_dev->parent_overlap) {
2662 rbd_assert(img_offset < rbd_dev->parent_overlap);
2663 length = rbd_dev->parent_overlap - img_offset;
2664 }
2665
2666 /*
Alex Elder3d7efd12013-04-19 15:34:50 -05002667 * Allocate a page array big enough to receive the data read
2668 * from the parent.
2669 */
2670 page_count = (u32)calc_pages_for(0, length);
Ilya Dryomov1e37f2f2017-11-06 11:33:36 +01002671 pages = ceph_alloc_page_vector(page_count, GFP_NOIO);
Alex Elder3d7efd12013-04-19 15:34:50 -05002672 if (IS_ERR(pages)) {
2673 result = PTR_ERR(pages);
2674 pages = NULL;
2675 goto out_err;
2676 }
2677
2678 result = -ENOMEM;
Alex Eldere93f3152013-05-08 22:50:04 -05002679 parent_request = rbd_parent_request_create(obj_request,
2680 img_offset, length);
Alex Elder3d7efd12013-04-19 15:34:50 -05002681 if (!parent_request)
2682 goto out_err;
Alex Elder3d7efd12013-04-19 15:34:50 -05002683
2684 result = rbd_img_request_fill(parent_request, OBJ_REQUEST_PAGES, pages);
2685 if (result)
2686 goto out_err;
Ilya Dryomov058aa992016-09-12 14:44:45 +02002687
Alex Elder3d7efd12013-04-19 15:34:50 -05002688 parent_request->copyup_pages = pages;
Alex Elderebda6402013-05-10 16:29:22 -05002689 parent_request->copyup_page_count = page_count;
Alex Elder3d7efd12013-04-19 15:34:50 -05002690 parent_request->callback = rbd_img_obj_parent_read_full_callback;
Ilya Dryomov058aa992016-09-12 14:44:45 +02002691
Alex Elder3d7efd12013-04-19 15:34:50 -05002692 result = rbd_img_request_submit(parent_request);
2693 if (!result)
2694 return 0;
2695
2696 parent_request->copyup_pages = NULL;
Alex Elderebda6402013-05-10 16:29:22 -05002697 parent_request->copyup_page_count = 0;
Alex Elder3d7efd12013-04-19 15:34:50 -05002698out_err:
2699 if (pages)
2700 ceph_release_page_vector(pages, page_count);
2701 if (parent_request)
2702 rbd_img_request_put(parent_request);
Alex Elder3d7efd12013-04-19 15:34:50 -05002703 return result;
2704}
2705
Alex Elderc5b5ef62013-02-11 12:33:24 -06002706static void rbd_img_obj_exists_callback(struct rbd_obj_request *obj_request)
2707{
Alex Elderc5b5ef62013-02-11 12:33:24 -06002708 struct rbd_obj_request *orig_request;
Alex Elder638f5ab2013-05-06 17:40:33 -05002709 struct rbd_device *rbd_dev;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002710 int result;
2711
2712 rbd_assert(!obj_request_img_data_test(obj_request));
2713
2714 /*
2715 * All we need from the object request is the original
2716 * request and the result of the STAT op. Grab those, then
2717 * we're done with the request.
2718 */
2719 orig_request = obj_request->obj_request;
2720 obj_request->obj_request = NULL;
Alex Elder912c3172013-05-13 20:35:38 -05002721 rbd_obj_request_put(orig_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002722 rbd_assert(orig_request);
2723 rbd_assert(orig_request->img_request);
2724
2725 result = obj_request->result;
2726 obj_request->result = 0;
2727
2728 dout("%s: obj %p for obj %p result %d %llu/%llu\n", __func__,
2729 obj_request, orig_request, result,
2730 obj_request->xferred, obj_request->length);
2731 rbd_obj_request_put(obj_request);
2732
Alex Elder638f5ab2013-05-06 17:40:33 -05002733 /*
2734 * If the overlap has become 0 (most likely because the
Ilya Dryomov980917f2016-09-12 18:59:42 +02002735 * image has been flattened) we need to re-submit the
2736 * original request.
Alex Elder638f5ab2013-05-06 17:40:33 -05002737 */
2738 rbd_dev = orig_request->img_request->rbd_dev;
2739 if (!rbd_dev->parent_overlap) {
Ilya Dryomov980917f2016-09-12 18:59:42 +02002740 rbd_obj_request_submit(orig_request);
2741 return;
Alex Elder638f5ab2013-05-06 17:40:33 -05002742 }
Alex Elderc5b5ef62013-02-11 12:33:24 -06002743
2744 /*
2745 * Our only purpose here is to determine whether the object
2746 * exists, and we don't want to treat the non-existence as
2747 * an error. If something else comes back, transfer the
2748 * error to the original request and complete it now.
2749 */
2750 if (!result) {
2751 obj_request_existence_set(orig_request, true);
2752 } else if (result == -ENOENT) {
2753 obj_request_existence_set(orig_request, false);
Ilya Dryomovc2e82412016-09-13 20:18:01 +02002754 } else {
2755 goto fail_orig_request;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002756 }
2757
2758 /*
2759 * Resubmit the original request now that we have recorded
2760 * whether the target object exists.
2761 */
Ilya Dryomovc2e82412016-09-13 20:18:01 +02002762 result = rbd_img_obj_request_submit(orig_request);
2763 if (result)
2764 goto fail_orig_request;
2765
2766 return;
2767
2768fail_orig_request:
Ilya Dryomov0dcc6852016-09-26 15:43:52 +02002769 rbd_obj_request_error(orig_request, result);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002770}
2771
2772static int rbd_img_obj_exists_submit(struct rbd_obj_request *obj_request)
2773{
Ilya Dryomov058aa992016-09-12 14:44:45 +02002774 struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002775 struct rbd_obj_request *stat_request;
Ilya Dryomov710214e2016-09-15 17:53:32 +02002776 struct page **pages;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002777 u32 page_count;
2778 size_t size;
2779 int ret;
2780
Ilya Dryomov6c696d82017-01-25 18:16:23 +01002781 stat_request = rbd_obj_request_create(OBJ_REQUEST_PAGES);
Ilya Dryomov710214e2016-09-15 17:53:32 +02002782 if (!stat_request)
2783 return -ENOMEM;
2784
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01002785 stat_request->object_no = obj_request->object_no;
2786
Ilya Dryomov710214e2016-09-15 17:53:32 +02002787 stat_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_READ, 1,
2788 stat_request);
2789 if (!stat_request->osd_req) {
2790 ret = -ENOMEM;
2791 goto fail_stat_request;
2792 }
2793
Alex Elderc5b5ef62013-02-11 12:33:24 -06002794 /*
2795 * The response data for a STAT call consists of:
2796 * le64 length;
2797 * struct {
2798 * le32 tv_sec;
2799 * le32 tv_nsec;
2800 * } mtime;
2801 */
2802 size = sizeof (__le64) + sizeof (__le32) + sizeof (__le32);
2803 page_count = (u32)calc_pages_for(0, size);
Ilya Dryomov1e37f2f2017-11-06 11:33:36 +01002804 pages = ceph_alloc_page_vector(page_count, GFP_NOIO);
Ilya Dryomov710214e2016-09-15 17:53:32 +02002805 if (IS_ERR(pages)) {
2806 ret = PTR_ERR(pages);
2807 goto fail_stat_request;
2808 }
Alex Elderc5b5ef62013-02-11 12:33:24 -06002809
Ilya Dryomov710214e2016-09-15 17:53:32 +02002810 osd_req_op_init(stat_request->osd_req, 0, CEPH_OSD_OP_STAT, 0);
2811 osd_req_op_raw_data_in_pages(stat_request->osd_req, 0, pages, size, 0,
2812 false, false);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002813
2814 rbd_obj_request_get(obj_request);
2815 stat_request->obj_request = obj_request;
2816 stat_request->pages = pages;
2817 stat_request->page_count = page_count;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002818 stat_request->callback = rbd_img_obj_exists_callback;
2819
Ilya Dryomov980917f2016-09-12 18:59:42 +02002820 rbd_obj_request_submit(stat_request);
2821 return 0;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002822
Ilya Dryomov710214e2016-09-15 17:53:32 +02002823fail_stat_request:
2824 rbd_obj_request_put(stat_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002825 return ret;
2826}
2827
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002828static bool img_obj_request_simple(struct rbd_obj_request *obj_request)
Alex Elderb454e362013-04-19 15:34:50 -05002829{
Ilya Dryomov058aa992016-09-12 14:44:45 +02002830 struct rbd_img_request *img_request = obj_request->img_request;
2831 struct rbd_device *rbd_dev = img_request->rbd_dev;
Alex Elderb454e362013-04-19 15:34:50 -05002832
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002833 /* Reads */
Josh Durgin1c220882014-04-04 17:49:12 -07002834 if (!img_request_write_test(img_request) &&
2835 !img_request_discard_test(img_request))
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002836 return true;
Alex Elderb454e362013-04-19 15:34:50 -05002837
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002838 /* Non-layered writes */
2839 if (!img_request_layered_test(img_request))
2840 return true;
2841
2842 /*
2843 * Layered writes outside of the parent overlap range don't
2844 * share any data with the parent.
2845 */
2846 if (!obj_request_overlaps_parent(obj_request))
2847 return true;
2848
2849 /*
Guangliang Zhaoc622d222014-04-01 22:22:15 +08002850 * Entire-object layered writes - we will overwrite whatever
2851 * parent data there is anyway.
2852 */
2853 if (!obj_request->offset &&
2854 obj_request->length == rbd_obj_bytes(&rbd_dev->header))
2855 return true;
2856
2857 /*
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002858 * If the object is known to already exist, its parent data has
2859 * already been copied.
2860 */
2861 if (obj_request_known_test(obj_request) &&
2862 obj_request_exists_test(obj_request))
2863 return true;
2864
2865 return false;
2866}
2867
2868static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request)
2869{
Ilya Dryomov058aa992016-09-12 14:44:45 +02002870 rbd_assert(obj_request_img_data_test(obj_request));
2871 rbd_assert(obj_request_type_valid(obj_request->type));
2872 rbd_assert(obj_request->img_request);
2873
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002874 if (img_obj_request_simple(obj_request)) {
Ilya Dryomov980917f2016-09-12 18:59:42 +02002875 rbd_obj_request_submit(obj_request);
2876 return 0;
Alex Elderb454e362013-04-19 15:34:50 -05002877 }
2878
2879 /*
Alex Elder3d7efd12013-04-19 15:34:50 -05002880 * It's a layered write. The target object might exist but
2881 * we may not know that yet. If we know it doesn't exist,
2882 * start by reading the data for the full target object from
2883 * the parent so we can use it for a copyup to the target.
Alex Elderb454e362013-04-19 15:34:50 -05002884 */
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002885 if (obj_request_known_test(obj_request))
Alex Elder3d7efd12013-04-19 15:34:50 -05002886 return rbd_img_obj_parent_read_full(obj_request);
2887
2888 /* We don't know whether the target exists. Go find out. */
Alex Elderb454e362013-04-19 15:34:50 -05002889
2890 return rbd_img_obj_exists_submit(obj_request);
2891}
2892
Alex Elderbf0d5f502012-11-22 00:00:08 -06002893static int rbd_img_request_submit(struct rbd_img_request *img_request)
2894{
Alex Elderbf0d5f502012-11-22 00:00:08 -06002895 struct rbd_obj_request *obj_request;
Alex Elder46faeed2013-04-10 17:47:46 -05002896 struct rbd_obj_request *next_obj_request;
Ilya Dryomov663ae2c2016-05-16 13:18:57 +02002897 int ret = 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002898
Alex Elder37206ee2013-02-20 17:32:08 -06002899 dout("%s: img %p\n", __func__, img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002900
Ilya Dryomov663ae2c2016-05-16 13:18:57 +02002901 rbd_img_request_get(img_request);
2902 for_each_obj_request_safe(img_request, obj_request, next_obj_request) {
Alex Elderb454e362013-04-19 15:34:50 -05002903 ret = rbd_img_obj_request_submit(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002904 if (ret)
Ilya Dryomov663ae2c2016-05-16 13:18:57 +02002905 goto out_put_ireq;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002906 }
2907
Ilya Dryomov663ae2c2016-05-16 13:18:57 +02002908out_put_ireq:
2909 rbd_img_request_put(img_request);
2910 return ret;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002911}
2912
Alex Elder8b3e1a52013-01-24 16:13:36 -06002913static void rbd_img_parent_read_callback(struct rbd_img_request *img_request)
2914{
2915 struct rbd_obj_request *obj_request;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002916 struct rbd_device *rbd_dev;
2917 u64 obj_end;
Alex Elder02c74fb2013-05-06 17:40:33 -05002918 u64 img_xferred;
2919 int img_result;
Alex Elder8b3e1a52013-01-24 16:13:36 -06002920
2921 rbd_assert(img_request_child_test(img_request));
2922
Alex Elder02c74fb2013-05-06 17:40:33 -05002923 /* First get what we need from the image request and release it */
2924
Alex Elder8b3e1a52013-01-24 16:13:36 -06002925 obj_request = img_request->obj_request;
Alex Elder02c74fb2013-05-06 17:40:33 -05002926 img_xferred = img_request->xferred;
2927 img_result = img_request->result;
2928 rbd_img_request_put(img_request);
2929
2930 /*
2931 * If the overlap has become 0 (most likely because the
2932 * image has been flattened) we need to re-submit the
2933 * original request.
2934 */
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002935 rbd_assert(obj_request);
2936 rbd_assert(obj_request->img_request);
Alex Elder02c74fb2013-05-06 17:40:33 -05002937 rbd_dev = obj_request->img_request->rbd_dev;
2938 if (!rbd_dev->parent_overlap) {
Ilya Dryomov980917f2016-09-12 18:59:42 +02002939 rbd_obj_request_submit(obj_request);
2940 return;
Alex Elder02c74fb2013-05-06 17:40:33 -05002941 }
2942
2943 obj_request->result = img_result;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002944 if (obj_request->result)
2945 goto out;
2946
2947 /*
2948 * We need to zero anything beyond the parent overlap
2949 * boundary. Since rbd_img_obj_request_read_callback()
2950 * will zero anything beyond the end of a short read, an
2951 * easy way to do this is to pretend the data from the
2952 * parent came up short--ending at the overlap boundary.
2953 */
2954 rbd_assert(obj_request->img_offset < U64_MAX - obj_request->length);
2955 obj_end = obj_request->img_offset + obj_request->length;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002956 if (obj_end > rbd_dev->parent_overlap) {
2957 u64 xferred = 0;
2958
2959 if (obj_request->img_offset < rbd_dev->parent_overlap)
2960 xferred = rbd_dev->parent_overlap -
2961 obj_request->img_offset;
2962
Alex Elder02c74fb2013-05-06 17:40:33 -05002963 obj_request->xferred = min(img_xferred, xferred);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002964 } else {
Alex Elder02c74fb2013-05-06 17:40:33 -05002965 obj_request->xferred = img_xferred;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002966 }
2967out:
Alex Elder8b3e1a52013-01-24 16:13:36 -06002968 rbd_img_obj_request_read_callback(obj_request);
2969 rbd_obj_request_complete(obj_request);
2970}
2971
2972static void rbd_img_parent_read(struct rbd_obj_request *obj_request)
2973{
Alex Elder8b3e1a52013-01-24 16:13:36 -06002974 struct rbd_img_request *img_request;
2975 int result;
2976
2977 rbd_assert(obj_request_img_data_test(obj_request));
2978 rbd_assert(obj_request->img_request != NULL);
2979 rbd_assert(obj_request->result == (s32) -ENOENT);
Alex Elder5b2ab722013-05-06 17:40:33 -05002980 rbd_assert(obj_request_type_valid(obj_request->type));
Alex Elder8b3e1a52013-01-24 16:13:36 -06002981
Alex Elder8b3e1a52013-01-24 16:13:36 -06002982 /* rbd_read_finish(obj_request, obj_request->length); */
Alex Eldere93f3152013-05-08 22:50:04 -05002983 img_request = rbd_parent_request_create(obj_request,
Alex Elder8b3e1a52013-01-24 16:13:36 -06002984 obj_request->img_offset,
Alex Eldere93f3152013-05-08 22:50:04 -05002985 obj_request->length);
Alex Elder8b3e1a52013-01-24 16:13:36 -06002986 result = -ENOMEM;
2987 if (!img_request)
2988 goto out_err;
2989
Alex Elder5b2ab722013-05-06 17:40:33 -05002990 if (obj_request->type == OBJ_REQUEST_BIO)
2991 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
Ilya Dryomov5359a172018-01-20 10:30:10 +01002992 &obj_request->bio_pos);
Alex Elder5b2ab722013-05-06 17:40:33 -05002993 else
2994 result = rbd_img_request_fill(img_request, OBJ_REQUEST_PAGES,
2995 obj_request->pages);
Alex Elder8b3e1a52013-01-24 16:13:36 -06002996 if (result)
2997 goto out_err;
2998
2999 img_request->callback = rbd_img_parent_read_callback;
3000 result = rbd_img_request_submit(img_request);
3001 if (result)
3002 goto out_err;
3003
3004 return;
3005out_err:
3006 if (img_request)
3007 rbd_img_request_put(img_request);
3008 obj_request->result = result;
3009 obj_request->xferred = 0;
3010 obj_request_done_set(obj_request);
3011}
3012
Ilya Dryomoved95b212016-08-12 16:40:02 +02003013static const struct rbd_client_id rbd_empty_cid;
3014
3015static bool rbd_cid_equal(const struct rbd_client_id *lhs,
3016 const struct rbd_client_id *rhs)
3017{
3018 return lhs->gid == rhs->gid && lhs->handle == rhs->handle;
3019}
3020
3021static struct rbd_client_id rbd_get_cid(struct rbd_device *rbd_dev)
3022{
3023 struct rbd_client_id cid;
3024
3025 mutex_lock(&rbd_dev->watch_mutex);
3026 cid.gid = ceph_client_gid(rbd_dev->rbd_client->client);
3027 cid.handle = rbd_dev->watch_cookie;
3028 mutex_unlock(&rbd_dev->watch_mutex);
3029 return cid;
3030}
3031
3032/*
3033 * lock_rwsem must be held for write
3034 */
3035static void rbd_set_owner_cid(struct rbd_device *rbd_dev,
3036 const struct rbd_client_id *cid)
3037{
3038 dout("%s rbd_dev %p %llu-%llu -> %llu-%llu\n", __func__, rbd_dev,
3039 rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle,
3040 cid->gid, cid->handle);
3041 rbd_dev->owner_cid = *cid; /* struct */
3042}
3043
3044static void format_lock_cookie(struct rbd_device *rbd_dev, char *buf)
3045{
3046 mutex_lock(&rbd_dev->watch_mutex);
3047 sprintf(buf, "%s %llu", RBD_LOCK_COOKIE_PREFIX, rbd_dev->watch_cookie);
3048 mutex_unlock(&rbd_dev->watch_mutex);
3049}
3050
Florian Margaineedd8ca82017-12-13 16:43:59 +01003051static void __rbd_lock(struct rbd_device *rbd_dev, const char *cookie)
3052{
3053 struct rbd_client_id cid = rbd_get_cid(rbd_dev);
3054
3055 strcpy(rbd_dev->lock_cookie, cookie);
3056 rbd_set_owner_cid(rbd_dev, &cid);
3057 queue_work(rbd_dev->task_wq, &rbd_dev->acquired_lock_work);
3058}
3059
Ilya Dryomoved95b212016-08-12 16:40:02 +02003060/*
3061 * lock_rwsem must be held for write
3062 */
3063static int rbd_lock(struct rbd_device *rbd_dev)
3064{
3065 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003066 char cookie[32];
3067 int ret;
3068
Ilya Dryomovcbbfb0f2017-04-13 12:17:38 +02003069 WARN_ON(__rbd_is_lock_owner(rbd_dev) ||
3070 rbd_dev->lock_cookie[0] != '\0');
Ilya Dryomoved95b212016-08-12 16:40:02 +02003071
3072 format_lock_cookie(rbd_dev, cookie);
3073 ret = ceph_cls_lock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc,
3074 RBD_LOCK_NAME, CEPH_CLS_LOCK_EXCLUSIVE, cookie,
3075 RBD_LOCK_TAG, "", 0);
3076 if (ret)
3077 return ret;
3078
3079 rbd_dev->lock_state = RBD_LOCK_STATE_LOCKED;
Florian Margaineedd8ca82017-12-13 16:43:59 +01003080 __rbd_lock(rbd_dev, cookie);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003081 return 0;
3082}
3083
3084/*
3085 * lock_rwsem must be held for write
3086 */
Ilya Dryomovbbead742017-04-13 12:17:38 +02003087static void rbd_unlock(struct rbd_device *rbd_dev)
Ilya Dryomoved95b212016-08-12 16:40:02 +02003088{
3089 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003090 int ret;
3091
Ilya Dryomovcbbfb0f2017-04-13 12:17:38 +02003092 WARN_ON(!__rbd_is_lock_owner(rbd_dev) ||
3093 rbd_dev->lock_cookie[0] == '\0');
Ilya Dryomoved95b212016-08-12 16:40:02 +02003094
Ilya Dryomoved95b212016-08-12 16:40:02 +02003095 ret = ceph_cls_unlock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc,
Ilya Dryomovcbbfb0f2017-04-13 12:17:38 +02003096 RBD_LOCK_NAME, rbd_dev->lock_cookie);
Ilya Dryomovbbead742017-04-13 12:17:38 +02003097 if (ret && ret != -ENOENT)
3098 rbd_warn(rbd_dev, "failed to unlock: %d", ret);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003099
Ilya Dryomovbbead742017-04-13 12:17:38 +02003100 /* treat errors as the image is unlocked */
3101 rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED;
Ilya Dryomovcbbfb0f2017-04-13 12:17:38 +02003102 rbd_dev->lock_cookie[0] = '\0';
Ilya Dryomoved95b212016-08-12 16:40:02 +02003103 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
3104 queue_work(rbd_dev->task_wq, &rbd_dev->released_lock_work);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003105}
3106
3107static int __rbd_notify_op_lock(struct rbd_device *rbd_dev,
3108 enum rbd_notify_op notify_op,
3109 struct page ***preply_pages,
3110 size_t *preply_len)
3111{
3112 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3113 struct rbd_client_id cid = rbd_get_cid(rbd_dev);
3114 int buf_size = 4 + 8 + 8 + CEPH_ENCODING_START_BLK_LEN;
3115 char buf[buf_size];
3116 void *p = buf;
3117
3118 dout("%s rbd_dev %p notify_op %d\n", __func__, rbd_dev, notify_op);
3119
3120 /* encode *LockPayload NotifyMessage (op + ClientId) */
3121 ceph_start_encoding(&p, 2, 1, buf_size - CEPH_ENCODING_START_BLK_LEN);
3122 ceph_encode_32(&p, notify_op);
3123 ceph_encode_64(&p, cid.gid);
3124 ceph_encode_64(&p, cid.handle);
3125
3126 return ceph_osdc_notify(osdc, &rbd_dev->header_oid,
3127 &rbd_dev->header_oloc, buf, buf_size,
3128 RBD_NOTIFY_TIMEOUT, preply_pages, preply_len);
3129}
3130
3131static void rbd_notify_op_lock(struct rbd_device *rbd_dev,
3132 enum rbd_notify_op notify_op)
3133{
3134 struct page **reply_pages;
3135 size_t reply_len;
3136
3137 __rbd_notify_op_lock(rbd_dev, notify_op, &reply_pages, &reply_len);
3138 ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len));
3139}
3140
3141static void rbd_notify_acquired_lock(struct work_struct *work)
3142{
3143 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
3144 acquired_lock_work);
3145
3146 rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_ACQUIRED_LOCK);
3147}
3148
3149static void rbd_notify_released_lock(struct work_struct *work)
3150{
3151 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
3152 released_lock_work);
3153
3154 rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_RELEASED_LOCK);
3155}
3156
3157static int rbd_request_lock(struct rbd_device *rbd_dev)
3158{
3159 struct page **reply_pages;
3160 size_t reply_len;
3161 bool lock_owner_responded = false;
3162 int ret;
3163
3164 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3165
3166 ret = __rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_REQUEST_LOCK,
3167 &reply_pages, &reply_len);
3168 if (ret && ret != -ETIMEDOUT) {
3169 rbd_warn(rbd_dev, "failed to request lock: %d", ret);
3170 goto out;
3171 }
3172
3173 if (reply_len > 0 && reply_len <= PAGE_SIZE) {
3174 void *p = page_address(reply_pages[0]);
3175 void *const end = p + reply_len;
3176 u32 n;
3177
3178 ceph_decode_32_safe(&p, end, n, e_inval); /* num_acks */
3179 while (n--) {
3180 u8 struct_v;
3181 u32 len;
3182
3183 ceph_decode_need(&p, end, 8 + 8, e_inval);
3184 p += 8 + 8; /* skip gid and cookie */
3185
3186 ceph_decode_32_safe(&p, end, len, e_inval);
3187 if (!len)
3188 continue;
3189
3190 if (lock_owner_responded) {
3191 rbd_warn(rbd_dev,
3192 "duplicate lock owners detected");
3193 ret = -EIO;
3194 goto out;
3195 }
3196
3197 lock_owner_responded = true;
3198 ret = ceph_start_decoding(&p, end, 1, "ResponseMessage",
3199 &struct_v, &len);
3200 if (ret) {
3201 rbd_warn(rbd_dev,
3202 "failed to decode ResponseMessage: %d",
3203 ret);
3204 goto e_inval;
3205 }
3206
3207 ret = ceph_decode_32(&p);
3208 }
3209 }
3210
3211 if (!lock_owner_responded) {
3212 rbd_warn(rbd_dev, "no lock owners detected");
3213 ret = -ETIMEDOUT;
3214 }
3215
3216out:
3217 ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len));
3218 return ret;
3219
3220e_inval:
3221 ret = -EINVAL;
3222 goto out;
3223}
3224
3225static void wake_requests(struct rbd_device *rbd_dev, bool wake_all)
3226{
3227 dout("%s rbd_dev %p wake_all %d\n", __func__, rbd_dev, wake_all);
3228
3229 cancel_delayed_work(&rbd_dev->lock_dwork);
3230 if (wake_all)
3231 wake_up_all(&rbd_dev->lock_waitq);
3232 else
3233 wake_up(&rbd_dev->lock_waitq);
3234}
3235
3236static int get_lock_owner_info(struct rbd_device *rbd_dev,
3237 struct ceph_locker **lockers, u32 *num_lockers)
3238{
3239 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3240 u8 lock_type;
3241 char *lock_tag;
3242 int ret;
3243
3244 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3245
3246 ret = ceph_cls_lock_info(osdc, &rbd_dev->header_oid,
3247 &rbd_dev->header_oloc, RBD_LOCK_NAME,
3248 &lock_type, &lock_tag, lockers, num_lockers);
3249 if (ret)
3250 return ret;
3251
3252 if (*num_lockers == 0) {
3253 dout("%s rbd_dev %p no lockers detected\n", __func__, rbd_dev);
3254 goto out;
3255 }
3256
3257 if (strcmp(lock_tag, RBD_LOCK_TAG)) {
3258 rbd_warn(rbd_dev, "locked by external mechanism, tag %s",
3259 lock_tag);
3260 ret = -EBUSY;
3261 goto out;
3262 }
3263
3264 if (lock_type == CEPH_CLS_LOCK_SHARED) {
3265 rbd_warn(rbd_dev, "shared lock type detected");
3266 ret = -EBUSY;
3267 goto out;
3268 }
3269
3270 if (strncmp((*lockers)[0].id.cookie, RBD_LOCK_COOKIE_PREFIX,
3271 strlen(RBD_LOCK_COOKIE_PREFIX))) {
3272 rbd_warn(rbd_dev, "locked by external mechanism, cookie %s",
3273 (*lockers)[0].id.cookie);
3274 ret = -EBUSY;
3275 goto out;
3276 }
3277
3278out:
3279 kfree(lock_tag);
3280 return ret;
3281}
3282
3283static int find_watcher(struct rbd_device *rbd_dev,
3284 const struct ceph_locker *locker)
3285{
3286 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3287 struct ceph_watch_item *watchers;
3288 u32 num_watchers;
3289 u64 cookie;
3290 int i;
3291 int ret;
3292
3293 ret = ceph_osdc_list_watchers(osdc, &rbd_dev->header_oid,
3294 &rbd_dev->header_oloc, &watchers,
3295 &num_watchers);
3296 if (ret)
3297 return ret;
3298
3299 sscanf(locker->id.cookie, RBD_LOCK_COOKIE_PREFIX " %llu", &cookie);
3300 for (i = 0; i < num_watchers; i++) {
3301 if (!memcmp(&watchers[i].addr, &locker->info.addr,
3302 sizeof(locker->info.addr)) &&
3303 watchers[i].cookie == cookie) {
3304 struct rbd_client_id cid = {
3305 .gid = le64_to_cpu(watchers[i].name.num),
3306 .handle = cookie,
3307 };
3308
3309 dout("%s rbd_dev %p found cid %llu-%llu\n", __func__,
3310 rbd_dev, cid.gid, cid.handle);
3311 rbd_set_owner_cid(rbd_dev, &cid);
3312 ret = 1;
3313 goto out;
3314 }
3315 }
3316
3317 dout("%s rbd_dev %p no watchers\n", __func__, rbd_dev);
3318 ret = 0;
3319out:
3320 kfree(watchers);
3321 return ret;
3322}
3323
3324/*
3325 * lock_rwsem must be held for write
3326 */
3327static int rbd_try_lock(struct rbd_device *rbd_dev)
3328{
3329 struct ceph_client *client = rbd_dev->rbd_client->client;
3330 struct ceph_locker *lockers;
3331 u32 num_lockers;
3332 int ret;
3333
3334 for (;;) {
3335 ret = rbd_lock(rbd_dev);
3336 if (ret != -EBUSY)
3337 return ret;
3338
3339 /* determine if the current lock holder is still alive */
3340 ret = get_lock_owner_info(rbd_dev, &lockers, &num_lockers);
3341 if (ret)
3342 return ret;
3343
3344 if (num_lockers == 0)
3345 goto again;
3346
3347 ret = find_watcher(rbd_dev, lockers);
3348 if (ret) {
3349 if (ret > 0)
3350 ret = 0; /* have to request lock */
3351 goto out;
3352 }
3353
3354 rbd_warn(rbd_dev, "%s%llu seems dead, breaking lock",
3355 ENTITY_NAME(lockers[0].id.name));
3356
3357 ret = ceph_monc_blacklist_add(&client->monc,
3358 &lockers[0].info.addr);
3359 if (ret) {
3360 rbd_warn(rbd_dev, "blacklist of %s%llu failed: %d",
3361 ENTITY_NAME(lockers[0].id.name), ret);
3362 goto out;
3363 }
3364
3365 ret = ceph_cls_break_lock(&client->osdc, &rbd_dev->header_oid,
3366 &rbd_dev->header_oloc, RBD_LOCK_NAME,
3367 lockers[0].id.cookie,
3368 &lockers[0].id.name);
3369 if (ret && ret != -ENOENT)
3370 goto out;
3371
3372again:
3373 ceph_free_lockers(lockers, num_lockers);
3374 }
3375
3376out:
3377 ceph_free_lockers(lockers, num_lockers);
3378 return ret;
3379}
3380
3381/*
3382 * ret is set only if lock_state is RBD_LOCK_STATE_UNLOCKED
3383 */
3384static enum rbd_lock_state rbd_try_acquire_lock(struct rbd_device *rbd_dev,
3385 int *pret)
3386{
3387 enum rbd_lock_state lock_state;
3388
3389 down_read(&rbd_dev->lock_rwsem);
3390 dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev,
3391 rbd_dev->lock_state);
3392 if (__rbd_is_lock_owner(rbd_dev)) {
3393 lock_state = rbd_dev->lock_state;
3394 up_read(&rbd_dev->lock_rwsem);
3395 return lock_state;
3396 }
3397
3398 up_read(&rbd_dev->lock_rwsem);
3399 down_write(&rbd_dev->lock_rwsem);
3400 dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev,
3401 rbd_dev->lock_state);
3402 if (!__rbd_is_lock_owner(rbd_dev)) {
3403 *pret = rbd_try_lock(rbd_dev);
3404 if (*pret)
3405 rbd_warn(rbd_dev, "failed to acquire lock: %d", *pret);
3406 }
3407
3408 lock_state = rbd_dev->lock_state;
3409 up_write(&rbd_dev->lock_rwsem);
3410 return lock_state;
3411}
3412
3413static void rbd_acquire_lock(struct work_struct *work)
3414{
3415 struct rbd_device *rbd_dev = container_of(to_delayed_work(work),
3416 struct rbd_device, lock_dwork);
3417 enum rbd_lock_state lock_state;
Kefeng Wang37f13252017-07-13 15:46:35 +08003418 int ret = 0;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003419
3420 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3421again:
3422 lock_state = rbd_try_acquire_lock(rbd_dev, &ret);
3423 if (lock_state != RBD_LOCK_STATE_UNLOCKED || ret == -EBLACKLISTED) {
3424 if (lock_state == RBD_LOCK_STATE_LOCKED)
3425 wake_requests(rbd_dev, true);
3426 dout("%s rbd_dev %p lock_state %d ret %d - done\n", __func__,
3427 rbd_dev, lock_state, ret);
3428 return;
3429 }
3430
3431 ret = rbd_request_lock(rbd_dev);
3432 if (ret == -ETIMEDOUT) {
3433 goto again; /* treat this as a dead client */
Ilya Dryomove010dd02017-04-13 12:17:39 +02003434 } else if (ret == -EROFS) {
3435 rbd_warn(rbd_dev, "peer will not release lock");
3436 /*
3437 * If this is rbd_add_acquire_lock(), we want to fail
3438 * immediately -- reuse BLACKLISTED flag. Otherwise we
3439 * want to block.
3440 */
3441 if (!(rbd_dev->disk->flags & GENHD_FL_UP)) {
3442 set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags);
3443 /* wake "rbd map --exclusive" process */
3444 wake_requests(rbd_dev, false);
3445 }
Ilya Dryomoved95b212016-08-12 16:40:02 +02003446 } else if (ret < 0) {
3447 rbd_warn(rbd_dev, "error requesting lock: %d", ret);
3448 mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork,
3449 RBD_RETRY_DELAY);
3450 } else {
3451 /*
3452 * lock owner acked, but resend if we don't see them
3453 * release the lock
3454 */
3455 dout("%s rbd_dev %p requeueing lock_dwork\n", __func__,
3456 rbd_dev);
3457 mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork,
3458 msecs_to_jiffies(2 * RBD_NOTIFY_TIMEOUT * MSEC_PER_SEC));
3459 }
3460}
3461
3462/*
3463 * lock_rwsem must be held for write
3464 */
3465static bool rbd_release_lock(struct rbd_device *rbd_dev)
3466{
3467 dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev,
3468 rbd_dev->lock_state);
3469 if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED)
3470 return false;
3471
3472 rbd_dev->lock_state = RBD_LOCK_STATE_RELEASING;
3473 downgrade_write(&rbd_dev->lock_rwsem);
3474 /*
3475 * Ensure that all in-flight IO is flushed.
3476 *
3477 * FIXME: ceph_osdc_sync() flushes the entire OSD client, which
3478 * may be shared with other devices.
3479 */
3480 ceph_osdc_sync(&rbd_dev->rbd_client->client->osdc);
3481 up_read(&rbd_dev->lock_rwsem);
3482
3483 down_write(&rbd_dev->lock_rwsem);
3484 dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev,
3485 rbd_dev->lock_state);
3486 if (rbd_dev->lock_state != RBD_LOCK_STATE_RELEASING)
3487 return false;
3488
Ilya Dryomovbbead742017-04-13 12:17:38 +02003489 rbd_unlock(rbd_dev);
3490 /*
3491 * Give others a chance to grab the lock - we would re-acquire
3492 * almost immediately if we got new IO during ceph_osdc_sync()
3493 * otherwise. We need to ack our own notifications, so this
3494 * lock_dwork will be requeued from rbd_wait_state_locked()
3495 * after wake_requests() in rbd_handle_released_lock().
3496 */
3497 cancel_delayed_work(&rbd_dev->lock_dwork);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003498 return true;
3499}
3500
3501static void rbd_release_lock_work(struct work_struct *work)
3502{
3503 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
3504 unlock_work);
3505
3506 down_write(&rbd_dev->lock_rwsem);
3507 rbd_release_lock(rbd_dev);
3508 up_write(&rbd_dev->lock_rwsem);
3509}
3510
3511static void rbd_handle_acquired_lock(struct rbd_device *rbd_dev, u8 struct_v,
3512 void **p)
3513{
3514 struct rbd_client_id cid = { 0 };
3515
3516 if (struct_v >= 2) {
3517 cid.gid = ceph_decode_64(p);
3518 cid.handle = ceph_decode_64(p);
3519 }
3520
3521 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
3522 cid.handle);
3523 if (!rbd_cid_equal(&cid, &rbd_empty_cid)) {
3524 down_write(&rbd_dev->lock_rwsem);
3525 if (rbd_cid_equal(&cid, &rbd_dev->owner_cid)) {
3526 /*
3527 * we already know that the remote client is
3528 * the owner
3529 */
3530 up_write(&rbd_dev->lock_rwsem);
3531 return;
3532 }
3533
3534 rbd_set_owner_cid(rbd_dev, &cid);
3535 downgrade_write(&rbd_dev->lock_rwsem);
3536 } else {
3537 down_read(&rbd_dev->lock_rwsem);
3538 }
3539
3540 if (!__rbd_is_lock_owner(rbd_dev))
3541 wake_requests(rbd_dev, false);
3542 up_read(&rbd_dev->lock_rwsem);
3543}
3544
3545static void rbd_handle_released_lock(struct rbd_device *rbd_dev, u8 struct_v,
3546 void **p)
3547{
3548 struct rbd_client_id cid = { 0 };
3549
3550 if (struct_v >= 2) {
3551 cid.gid = ceph_decode_64(p);
3552 cid.handle = ceph_decode_64(p);
3553 }
3554
3555 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
3556 cid.handle);
3557 if (!rbd_cid_equal(&cid, &rbd_empty_cid)) {
3558 down_write(&rbd_dev->lock_rwsem);
3559 if (!rbd_cid_equal(&cid, &rbd_dev->owner_cid)) {
3560 dout("%s rbd_dev %p unexpected owner, cid %llu-%llu != owner_cid %llu-%llu\n",
3561 __func__, rbd_dev, cid.gid, cid.handle,
3562 rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle);
3563 up_write(&rbd_dev->lock_rwsem);
3564 return;
3565 }
3566
3567 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
3568 downgrade_write(&rbd_dev->lock_rwsem);
3569 } else {
3570 down_read(&rbd_dev->lock_rwsem);
3571 }
3572
3573 if (!__rbd_is_lock_owner(rbd_dev))
3574 wake_requests(rbd_dev, false);
3575 up_read(&rbd_dev->lock_rwsem);
3576}
3577
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02003578/*
3579 * Returns result for ResponseMessage to be encoded (<= 0), or 1 if no
3580 * ResponseMessage is needed.
3581 */
3582static int rbd_handle_request_lock(struct rbd_device *rbd_dev, u8 struct_v,
3583 void **p)
Ilya Dryomoved95b212016-08-12 16:40:02 +02003584{
3585 struct rbd_client_id my_cid = rbd_get_cid(rbd_dev);
3586 struct rbd_client_id cid = { 0 };
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02003587 int result = 1;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003588
3589 if (struct_v >= 2) {
3590 cid.gid = ceph_decode_64(p);
3591 cid.handle = ceph_decode_64(p);
3592 }
3593
3594 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
3595 cid.handle);
3596 if (rbd_cid_equal(&cid, &my_cid))
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02003597 return result;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003598
3599 down_read(&rbd_dev->lock_rwsem);
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02003600 if (__rbd_is_lock_owner(rbd_dev)) {
3601 if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED &&
3602 rbd_cid_equal(&rbd_dev->owner_cid, &rbd_empty_cid))
3603 goto out_unlock;
3604
3605 /*
3606 * encode ResponseMessage(0) so the peer can detect
3607 * a missing owner
3608 */
3609 result = 0;
3610
3611 if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) {
Ilya Dryomove010dd02017-04-13 12:17:39 +02003612 if (!rbd_dev->opts->exclusive) {
3613 dout("%s rbd_dev %p queueing unlock_work\n",
3614 __func__, rbd_dev);
3615 queue_work(rbd_dev->task_wq,
3616 &rbd_dev->unlock_work);
3617 } else {
3618 /* refuse to release the lock */
3619 result = -EROFS;
3620 }
Ilya Dryomoved95b212016-08-12 16:40:02 +02003621 }
3622 }
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02003623
3624out_unlock:
Ilya Dryomoved95b212016-08-12 16:40:02 +02003625 up_read(&rbd_dev->lock_rwsem);
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02003626 return result;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003627}
3628
3629static void __rbd_acknowledge_notify(struct rbd_device *rbd_dev,
3630 u64 notify_id, u64 cookie, s32 *result)
3631{
3632 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3633 int buf_size = 4 + CEPH_ENCODING_START_BLK_LEN;
3634 char buf[buf_size];
3635 int ret;
3636
3637 if (result) {
3638 void *p = buf;
3639
3640 /* encode ResponseMessage */
3641 ceph_start_encoding(&p, 1, 1,
3642 buf_size - CEPH_ENCODING_START_BLK_LEN);
3643 ceph_encode_32(&p, *result);
3644 } else {
3645 buf_size = 0;
3646 }
3647
3648 ret = ceph_osdc_notify_ack(osdc, &rbd_dev->header_oid,
3649 &rbd_dev->header_oloc, notify_id, cookie,
3650 buf, buf_size);
3651 if (ret)
3652 rbd_warn(rbd_dev, "acknowledge_notify failed: %d", ret);
3653}
3654
3655static void rbd_acknowledge_notify(struct rbd_device *rbd_dev, u64 notify_id,
3656 u64 cookie)
3657{
3658 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3659 __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, NULL);
3660}
3661
3662static void rbd_acknowledge_notify_result(struct rbd_device *rbd_dev,
3663 u64 notify_id, u64 cookie, s32 result)
3664{
3665 dout("%s rbd_dev %p result %d\n", __func__, rbd_dev, result);
3666 __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, &result);
3667}
Ilya Dryomov922dab62016-05-26 01:15:02 +02003668
3669static void rbd_watch_cb(void *arg, u64 notify_id, u64 cookie,
3670 u64 notifier_id, void *data, size_t data_len)
Alex Elderb8d70032012-11-30 17:53:04 -06003671{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003672 struct rbd_device *rbd_dev = arg;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003673 void *p = data;
3674 void *const end = p + data_len;
Ilya Dryomovd4c22692016-09-06 11:15:48 +02003675 u8 struct_v = 0;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003676 u32 len;
3677 u32 notify_op;
Alex Elderb8d70032012-11-30 17:53:04 -06003678 int ret;
3679
Ilya Dryomoved95b212016-08-12 16:40:02 +02003680 dout("%s rbd_dev %p cookie %llu notify_id %llu data_len %zu\n",
3681 __func__, rbd_dev, cookie, notify_id, data_len);
3682 if (data_len) {
3683 ret = ceph_start_decoding(&p, end, 1, "NotifyMessage",
3684 &struct_v, &len);
3685 if (ret) {
3686 rbd_warn(rbd_dev, "failed to decode NotifyMessage: %d",
3687 ret);
3688 return;
3689 }
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04003690
Ilya Dryomoved95b212016-08-12 16:40:02 +02003691 notify_op = ceph_decode_32(&p);
3692 } else {
3693 /* legacy notification for header updates */
3694 notify_op = RBD_NOTIFY_OP_HEADER_UPDATE;
3695 len = 0;
3696 }
Alex Elderb8d70032012-11-30 17:53:04 -06003697
Ilya Dryomoved95b212016-08-12 16:40:02 +02003698 dout("%s rbd_dev %p notify_op %u\n", __func__, rbd_dev, notify_op);
3699 switch (notify_op) {
3700 case RBD_NOTIFY_OP_ACQUIRED_LOCK:
3701 rbd_handle_acquired_lock(rbd_dev, struct_v, &p);
3702 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3703 break;
3704 case RBD_NOTIFY_OP_RELEASED_LOCK:
3705 rbd_handle_released_lock(rbd_dev, struct_v, &p);
3706 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3707 break;
3708 case RBD_NOTIFY_OP_REQUEST_LOCK:
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02003709 ret = rbd_handle_request_lock(rbd_dev, struct_v, &p);
3710 if (ret <= 0)
Ilya Dryomoved95b212016-08-12 16:40:02 +02003711 rbd_acknowledge_notify_result(rbd_dev, notify_id,
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02003712 cookie, ret);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003713 else
3714 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3715 break;
3716 case RBD_NOTIFY_OP_HEADER_UPDATE:
3717 ret = rbd_dev_refresh(rbd_dev);
3718 if (ret)
3719 rbd_warn(rbd_dev, "refresh failed: %d", ret);
3720
3721 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3722 break;
3723 default:
3724 if (rbd_is_lock_owner(rbd_dev))
3725 rbd_acknowledge_notify_result(rbd_dev, notify_id,
3726 cookie, -EOPNOTSUPP);
3727 else
3728 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3729 break;
3730 }
Alex Elderb8d70032012-11-30 17:53:04 -06003731}
3732
Ilya Dryomov99d16942016-08-12 16:11:41 +02003733static void __rbd_unregister_watch(struct rbd_device *rbd_dev);
3734
Ilya Dryomov922dab62016-05-26 01:15:02 +02003735static void rbd_watch_errcb(void *arg, u64 cookie, int err)
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003736{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003737 struct rbd_device *rbd_dev = arg;
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003738
Ilya Dryomov922dab62016-05-26 01:15:02 +02003739 rbd_warn(rbd_dev, "encountered watch error: %d", err);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003740
Ilya Dryomoved95b212016-08-12 16:40:02 +02003741 down_write(&rbd_dev->lock_rwsem);
3742 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
3743 up_write(&rbd_dev->lock_rwsem);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003744
Ilya Dryomov99d16942016-08-12 16:11:41 +02003745 mutex_lock(&rbd_dev->watch_mutex);
3746 if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) {
3747 __rbd_unregister_watch(rbd_dev);
3748 rbd_dev->watch_state = RBD_WATCH_STATE_ERROR;
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003749
Ilya Dryomov99d16942016-08-12 16:11:41 +02003750 queue_delayed_work(rbd_dev->task_wq, &rbd_dev->watch_dwork, 0);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003751 }
Ilya Dryomov99d16942016-08-12 16:11:41 +02003752 mutex_unlock(&rbd_dev->watch_mutex);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003753}
3754
3755/*
Ilya Dryomov99d16942016-08-12 16:11:41 +02003756 * watch_mutex must be locked
Alex Elder9969ebc2013-01-18 12:31:10 -06003757 */
Ilya Dryomov99d16942016-08-12 16:11:41 +02003758static int __rbd_register_watch(struct rbd_device *rbd_dev)
Alex Elder9969ebc2013-01-18 12:31:10 -06003759{
3760 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Ilya Dryomov922dab62016-05-26 01:15:02 +02003761 struct ceph_osd_linger_request *handle;
Alex Elder9969ebc2013-01-18 12:31:10 -06003762
Ilya Dryomov922dab62016-05-26 01:15:02 +02003763 rbd_assert(!rbd_dev->watch_handle);
Ilya Dryomov99d16942016-08-12 16:11:41 +02003764 dout("%s rbd_dev %p\n", __func__, rbd_dev);
Alex Elder9969ebc2013-01-18 12:31:10 -06003765
Ilya Dryomov922dab62016-05-26 01:15:02 +02003766 handle = ceph_osdc_watch(osdc, &rbd_dev->header_oid,
3767 &rbd_dev->header_oloc, rbd_watch_cb,
3768 rbd_watch_errcb, rbd_dev);
3769 if (IS_ERR(handle))
3770 return PTR_ERR(handle);
Alex Elder9969ebc2013-01-18 12:31:10 -06003771
Ilya Dryomov922dab62016-05-26 01:15:02 +02003772 rbd_dev->watch_handle = handle;
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003773 return 0;
Alex Elder9969ebc2013-01-18 12:31:10 -06003774}
3775
Ilya Dryomov99d16942016-08-12 16:11:41 +02003776/*
3777 * watch_mutex must be locked
3778 */
3779static void __rbd_unregister_watch(struct rbd_device *rbd_dev)
Ilya Dryomovfca27062013-12-16 18:02:40 +02003780{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003781 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3782 int ret;
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003783
Ilya Dryomov99d16942016-08-12 16:11:41 +02003784 rbd_assert(rbd_dev->watch_handle);
3785 dout("%s rbd_dev %p\n", __func__, rbd_dev);
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003786
Ilya Dryomov922dab62016-05-26 01:15:02 +02003787 ret = ceph_osdc_unwatch(osdc, rbd_dev->watch_handle);
3788 if (ret)
3789 rbd_warn(rbd_dev, "failed to unwatch: %d", ret);
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003790
Ilya Dryomov922dab62016-05-26 01:15:02 +02003791 rbd_dev->watch_handle = NULL;
Ilya Dryomovc525f032016-04-28 16:07:26 +02003792}
3793
Ilya Dryomov99d16942016-08-12 16:11:41 +02003794static int rbd_register_watch(struct rbd_device *rbd_dev)
Ilya Dryomovc525f032016-04-28 16:07:26 +02003795{
Ilya Dryomov99d16942016-08-12 16:11:41 +02003796 int ret;
Ilya Dryomov811c6682016-04-15 16:22:16 +02003797
Ilya Dryomov99d16942016-08-12 16:11:41 +02003798 mutex_lock(&rbd_dev->watch_mutex);
3799 rbd_assert(rbd_dev->watch_state == RBD_WATCH_STATE_UNREGISTERED);
3800 ret = __rbd_register_watch(rbd_dev);
3801 if (ret)
3802 goto out;
3803
3804 rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED;
3805 rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id;
3806
3807out:
3808 mutex_unlock(&rbd_dev->watch_mutex);
3809 return ret;
3810}
3811
3812static void cancel_tasks_sync(struct rbd_device *rbd_dev)
3813{
3814 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3815
3816 cancel_delayed_work_sync(&rbd_dev->watch_dwork);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003817 cancel_work_sync(&rbd_dev->acquired_lock_work);
3818 cancel_work_sync(&rbd_dev->released_lock_work);
3819 cancel_delayed_work_sync(&rbd_dev->lock_dwork);
3820 cancel_work_sync(&rbd_dev->unlock_work);
Ilya Dryomov99d16942016-08-12 16:11:41 +02003821}
3822
3823static void rbd_unregister_watch(struct rbd_device *rbd_dev)
3824{
Ilya Dryomoved95b212016-08-12 16:40:02 +02003825 WARN_ON(waitqueue_active(&rbd_dev->lock_waitq));
Ilya Dryomov99d16942016-08-12 16:11:41 +02003826 cancel_tasks_sync(rbd_dev);
3827
3828 mutex_lock(&rbd_dev->watch_mutex);
3829 if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED)
3830 __rbd_unregister_watch(rbd_dev);
3831 rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED;
3832 mutex_unlock(&rbd_dev->watch_mutex);
3833
Ilya Dryomov811c6682016-04-15 16:22:16 +02003834 ceph_osdc_flush_notifies(&rbd_dev->rbd_client->client->osdc);
Ilya Dryomovfca27062013-12-16 18:02:40 +02003835}
3836
Ilya Dryomov14bb2112017-04-13 12:17:38 +02003837/*
3838 * lock_rwsem must be held for write
3839 */
3840static void rbd_reacquire_lock(struct rbd_device *rbd_dev)
3841{
3842 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3843 char cookie[32];
3844 int ret;
3845
3846 WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED);
3847
3848 format_lock_cookie(rbd_dev, cookie);
3849 ret = ceph_cls_set_cookie(osdc, &rbd_dev->header_oid,
3850 &rbd_dev->header_oloc, RBD_LOCK_NAME,
3851 CEPH_CLS_LOCK_EXCLUSIVE, rbd_dev->lock_cookie,
3852 RBD_LOCK_TAG, cookie);
3853 if (ret) {
3854 if (ret != -EOPNOTSUPP)
3855 rbd_warn(rbd_dev, "failed to update lock cookie: %d",
3856 ret);
3857
3858 /*
3859 * Lock cookie cannot be updated on older OSDs, so do
3860 * a manual release and queue an acquire.
3861 */
3862 if (rbd_release_lock(rbd_dev))
3863 queue_delayed_work(rbd_dev->task_wq,
3864 &rbd_dev->lock_dwork, 0);
3865 } else {
Florian Margaineedd8ca82017-12-13 16:43:59 +01003866 __rbd_lock(rbd_dev, cookie);
Ilya Dryomov14bb2112017-04-13 12:17:38 +02003867 }
3868}
3869
Ilya Dryomov99d16942016-08-12 16:11:41 +02003870static void rbd_reregister_watch(struct work_struct *work)
3871{
3872 struct rbd_device *rbd_dev = container_of(to_delayed_work(work),
3873 struct rbd_device, watch_dwork);
3874 int ret;
3875
3876 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3877
3878 mutex_lock(&rbd_dev->watch_mutex);
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003879 if (rbd_dev->watch_state != RBD_WATCH_STATE_ERROR) {
3880 mutex_unlock(&rbd_dev->watch_mutex);
Ilya Dryomov14bb2112017-04-13 12:17:38 +02003881 return;
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003882 }
Ilya Dryomov99d16942016-08-12 16:11:41 +02003883
3884 ret = __rbd_register_watch(rbd_dev);
3885 if (ret) {
3886 rbd_warn(rbd_dev, "failed to reregister watch: %d", ret);
Ilya Dryomov4d736442016-09-29 14:23:12 +02003887 if (ret == -EBLACKLISTED || ret == -ENOENT) {
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003888 set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags);
Ilya Dryomov14bb2112017-04-13 12:17:38 +02003889 wake_requests(rbd_dev, true);
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003890 } else {
Ilya Dryomov99d16942016-08-12 16:11:41 +02003891 queue_delayed_work(rbd_dev->task_wq,
3892 &rbd_dev->watch_dwork,
3893 RBD_RETRY_DELAY);
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003894 }
3895 mutex_unlock(&rbd_dev->watch_mutex);
Ilya Dryomov14bb2112017-04-13 12:17:38 +02003896 return;
Ilya Dryomov99d16942016-08-12 16:11:41 +02003897 }
3898
3899 rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED;
3900 rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id;
3901 mutex_unlock(&rbd_dev->watch_mutex);
3902
Ilya Dryomov14bb2112017-04-13 12:17:38 +02003903 down_write(&rbd_dev->lock_rwsem);
3904 if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED)
3905 rbd_reacquire_lock(rbd_dev);
3906 up_write(&rbd_dev->lock_rwsem);
3907
Ilya Dryomov99d16942016-08-12 16:11:41 +02003908 ret = rbd_dev_refresh(rbd_dev);
3909 if (ret)
3910 rbd_warn(rbd_dev, "reregisteration refresh failed: %d", ret);
Ilya Dryomov99d16942016-08-12 16:11:41 +02003911}
3912
Alex Elder36be9a72013-01-19 00:30:28 -06003913/*
Alex Elderf40eb342013-04-25 15:09:42 -05003914 * Synchronous osd object method call. Returns the number of bytes
3915 * returned in the outbound buffer, or a negative error code.
Alex Elder36be9a72013-01-19 00:30:28 -06003916 */
3917static int rbd_obj_method_sync(struct rbd_device *rbd_dev,
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003918 struct ceph_object_id *oid,
3919 struct ceph_object_locator *oloc,
Alex Elder36be9a72013-01-19 00:30:28 -06003920 const char *method_name,
Alex Elder41579762013-04-21 12:14:45 -05003921 const void *outbound,
Alex Elder36be9a72013-01-19 00:30:28 -06003922 size_t outbound_size,
Alex Elder41579762013-04-21 12:14:45 -05003923 void *inbound,
Alex Eldere2a58ee2013-04-30 00:44:33 -05003924 size_t inbound_size)
Alex Elder36be9a72013-01-19 00:30:28 -06003925{
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003926 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3927 struct page *req_page = NULL;
3928 struct page *reply_page;
Alex Elder36be9a72013-01-19 00:30:28 -06003929 int ret;
3930
3931 /*
Alex Elder6010a452013-04-05 01:27:11 -05003932 * Method calls are ultimately read operations. The result
3933 * should placed into the inbound buffer provided. They
3934 * also supply outbound data--parameters for the object
3935 * method. Currently if this is present it will be a
3936 * snapshot id.
Alex Elder36be9a72013-01-19 00:30:28 -06003937 */
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003938 if (outbound) {
3939 if (outbound_size > PAGE_SIZE)
3940 return -E2BIG;
Alex Elder36be9a72013-01-19 00:30:28 -06003941
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003942 req_page = alloc_page(GFP_KERNEL);
3943 if (!req_page)
3944 return -ENOMEM;
Alex Elder36be9a72013-01-19 00:30:28 -06003945
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003946 memcpy(page_address(req_page), outbound, outbound_size);
Alex Elder04017e22013-04-05 14:46:02 -05003947 }
Alex Elder430c28c2013-04-03 21:32:51 -05003948
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003949 reply_page = alloc_page(GFP_KERNEL);
3950 if (!reply_page) {
3951 if (req_page)
3952 __free_page(req_page);
3953 return -ENOMEM;
3954 }
Alex Elder36be9a72013-01-19 00:30:28 -06003955
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003956 ret = ceph_osdc_call(osdc, oid, oloc, RBD_DRV_NAME, method_name,
3957 CEPH_OSD_FLAG_READ, req_page, outbound_size,
3958 reply_page, &inbound_size);
3959 if (!ret) {
3960 memcpy(inbound, page_address(reply_page), inbound_size);
3961 ret = inbound_size;
3962 }
Alex Elder57385b52013-04-21 12:14:45 -05003963
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003964 if (req_page)
3965 __free_page(req_page);
3966 __free_page(reply_page);
Alex Elder36be9a72013-01-19 00:30:28 -06003967 return ret;
3968}
3969
Ilya Dryomoved95b212016-08-12 16:40:02 +02003970/*
3971 * lock_rwsem must be held for read
3972 */
3973static void rbd_wait_state_locked(struct rbd_device *rbd_dev)
3974{
3975 DEFINE_WAIT(wait);
3976
3977 do {
3978 /*
3979 * Note the use of mod_delayed_work() in rbd_acquire_lock()
3980 * and cancel_delayed_work() in wake_requests().
3981 */
3982 dout("%s rbd_dev %p queueing lock_dwork\n", __func__, rbd_dev);
3983 queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0);
3984 prepare_to_wait_exclusive(&rbd_dev->lock_waitq, &wait,
3985 TASK_UNINTERRUPTIBLE);
3986 up_read(&rbd_dev->lock_rwsem);
3987 schedule();
3988 down_read(&rbd_dev->lock_rwsem);
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003989 } while (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED &&
3990 !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags));
3991
Ilya Dryomoved95b212016-08-12 16:40:02 +02003992 finish_wait(&rbd_dev->lock_waitq, &wait);
3993}
3994
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01003995static void rbd_queue_workfn(struct work_struct *work)
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04003996{
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01003997 struct request *rq = blk_mq_rq_from_pdu(work);
3998 struct rbd_device *rbd_dev = rq->q->queuedata;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04003999 struct rbd_img_request *img_request;
Josh Durgin4e752f02014-04-08 11:12:11 -07004000 struct ceph_snap_context *snapc = NULL;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004001 u64 offset = (u64)blk_rq_pos(rq) << SECTOR_SHIFT;
4002 u64 length = blk_rq_bytes(rq);
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004003 enum obj_operation_type op_type;
Josh Durgin4e752f02014-04-08 11:12:11 -07004004 u64 mapping_size;
Ilya Dryomov80de1912016-09-20 14:23:17 +02004005 bool must_be_locked;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004006 int result;
4007
Christoph Hellwigaebf5262017-01-31 16:57:31 +01004008 switch (req_op(rq)) {
4009 case REQ_OP_DISCARD:
Ilya Dryomov6ac56952017-05-22 19:59:24 +02004010 case REQ_OP_WRITE_ZEROES:
Christoph Hellwigaebf5262017-01-31 16:57:31 +01004011 op_type = OBJ_OP_DISCARD;
4012 break;
4013 case REQ_OP_WRITE:
4014 op_type = OBJ_OP_WRITE;
4015 break;
4016 case REQ_OP_READ:
4017 op_type = OBJ_OP_READ;
4018 break;
4019 default:
4020 dout("%s: non-fs request type %d\n", __func__, req_op(rq));
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004021 result = -EIO;
4022 goto err;
4023 }
4024
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004025 /* Ignore/skip any zero-length requests */
4026
4027 if (!length) {
4028 dout("%s: zero-length request\n", __func__);
4029 result = 0;
4030 goto err_rq;
4031 }
4032
Ilya Dryomov9568c932017-10-12 12:35:19 +02004033 rbd_assert(op_type == OBJ_OP_READ ||
4034 rbd_dev->spec->snap_id == CEPH_NOSNAP);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004035
4036 /*
4037 * Quit early if the mapped snapshot no longer exists. It's
4038 * still possible the snapshot will have disappeared by the
4039 * time our request arrives at the osd, but there's no sense in
4040 * sending it if we already know.
4041 */
4042 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) {
4043 dout("request for non-existent snapshot");
4044 rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP);
4045 result = -ENXIO;
4046 goto err_rq;
4047 }
4048
4049 if (offset && length > U64_MAX - offset + 1) {
4050 rbd_warn(rbd_dev, "bad request range (%llu~%llu)", offset,
4051 length);
4052 result = -EINVAL;
4053 goto err_rq; /* Shouldn't happen */
4054 }
4055
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004056 blk_mq_start_request(rq);
4057
Josh Durgin4e752f02014-04-08 11:12:11 -07004058 down_read(&rbd_dev->header_rwsem);
4059 mapping_size = rbd_dev->mapping.size;
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004060 if (op_type != OBJ_OP_READ) {
Josh Durgin4e752f02014-04-08 11:12:11 -07004061 snapc = rbd_dev->header.snapc;
4062 ceph_get_snap_context(snapc);
4063 }
4064 up_read(&rbd_dev->header_rwsem);
4065
4066 if (offset + length > mapping_size) {
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004067 rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)", offset,
Josh Durgin4e752f02014-04-08 11:12:11 -07004068 length, mapping_size);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004069 result = -EIO;
4070 goto err_rq;
4071 }
4072
Ilya Dryomovf9bebd52017-04-13 12:17:39 +02004073 must_be_locked =
4074 (rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK) &&
4075 (op_type != OBJ_OP_READ || rbd_dev->opts->lock_on_read);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004076 if (must_be_locked) {
4077 down_read(&rbd_dev->lock_rwsem);
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02004078 if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED &&
Ilya Dryomove010dd02017-04-13 12:17:39 +02004079 !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) {
4080 if (rbd_dev->opts->exclusive) {
4081 rbd_warn(rbd_dev, "exclusive lock required");
4082 result = -EROFS;
4083 goto err_unlock;
4084 }
Ilya Dryomoved95b212016-08-12 16:40:02 +02004085 rbd_wait_state_locked(rbd_dev);
Ilya Dryomove010dd02017-04-13 12:17:39 +02004086 }
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02004087 if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) {
4088 result = -EBLACKLISTED;
4089 goto err_unlock;
4090 }
Ilya Dryomoved95b212016-08-12 16:40:02 +02004091 }
4092
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004093 img_request = rbd_img_request_create(rbd_dev, offset, length, op_type,
Josh Durgin4e752f02014-04-08 11:12:11 -07004094 snapc);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004095 if (!img_request) {
4096 result = -ENOMEM;
Ilya Dryomoved95b212016-08-12 16:40:02 +02004097 goto err_unlock;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004098 }
4099 img_request->rq = rq;
Ilya Dryomov70b16db2015-11-27 19:23:24 +01004100 snapc = NULL; /* img_request consumes a ref */
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004101
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004102 if (op_type == OBJ_OP_DISCARD)
4103 result = rbd_img_request_fill(img_request, OBJ_REQUEST_NODATA,
4104 NULL);
Ilya Dryomov5359a172018-01-20 10:30:10 +01004105 else {
4106 struct ceph_bio_iter bio_it = { .bio = rq->bio,
4107 .iter = rq->bio->bi_iter };
4108
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004109 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
Ilya Dryomov5359a172018-01-20 10:30:10 +01004110 &bio_it);
4111 }
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004112 if (result)
4113 goto err_img_request;
4114
4115 result = rbd_img_request_submit(img_request);
4116 if (result)
4117 goto err_img_request;
4118
Ilya Dryomoved95b212016-08-12 16:40:02 +02004119 if (must_be_locked)
4120 up_read(&rbd_dev->lock_rwsem);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004121 return;
4122
4123err_img_request:
4124 rbd_img_request_put(img_request);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004125err_unlock:
4126 if (must_be_locked)
4127 up_read(&rbd_dev->lock_rwsem);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004128err_rq:
4129 if (result)
4130 rbd_warn(rbd_dev, "%s %llx at %llx result %d",
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004131 obj_op_name(op_type), length, offset, result);
SF Markus Elfringe96a6502014-11-02 15:20:59 +01004132 ceph_put_snap_context(snapc);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004133err:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02004134 blk_mq_end_request(rq, errno_to_blk_status(result));
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004135}
4136
Christoph Hellwigfc17b652017-06-03 09:38:05 +02004137static blk_status_t rbd_queue_rq(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004138 const struct blk_mq_queue_data *bd)
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004139{
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004140 struct request *rq = bd->rq;
4141 struct work_struct *work = blk_mq_rq_to_pdu(rq);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004142
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004143 queue_work(rbd_wq, work);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02004144 return BLK_STS_OK;
Alex Elderbf0d5f502012-11-22 00:00:08 -06004145}
4146
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004147static void rbd_free_disk(struct rbd_device *rbd_dev)
4148{
Ilya Dryomov5769ed02017-04-13 12:17:38 +02004149 blk_cleanup_queue(rbd_dev->disk->queue);
4150 blk_mq_free_tag_set(&rbd_dev->tag_set);
4151 put_disk(rbd_dev->disk);
Alex Eldera0cab922013-04-25 23:15:08 -05004152 rbd_dev->disk = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004153}
4154
Alex Elder788e2df2013-01-17 12:25:27 -06004155static int rbd_obj_read_sync(struct rbd_device *rbd_dev,
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004156 struct ceph_object_id *oid,
4157 struct ceph_object_locator *oloc,
4158 void *buf, int buf_len)
Alex Elder788e2df2013-01-17 12:25:27 -06004159
4160{
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004161 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
4162 struct ceph_osd_request *req;
4163 struct page **pages;
4164 int num_pages = calc_pages_for(0, buf_len);
Alex Elder788e2df2013-01-17 12:25:27 -06004165 int ret;
4166
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004167 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL);
4168 if (!req)
4169 return -ENOMEM;
Alex Elder788e2df2013-01-17 12:25:27 -06004170
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004171 ceph_oid_copy(&req->r_base_oid, oid);
4172 ceph_oloc_copy(&req->r_base_oloc, oloc);
4173 req->r_flags = CEPH_OSD_FLAG_READ;
Alex Elder788e2df2013-01-17 12:25:27 -06004174
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004175 ret = ceph_osdc_alloc_messages(req, GFP_KERNEL);
Alex Elder788e2df2013-01-17 12:25:27 -06004176 if (ret)
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004177 goto out_req;
Alex Elder788e2df2013-01-17 12:25:27 -06004178
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004179 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
4180 if (IS_ERR(pages)) {
4181 ret = PTR_ERR(pages);
4182 goto out_req;
4183 }
Alex Elder1ceae7e2013-02-06 13:11:38 -06004184
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004185 osd_req_op_extent_init(req, 0, CEPH_OSD_OP_READ, 0, buf_len, 0, 0);
4186 osd_req_op_extent_osd_data_pages(req, 0, pages, buf_len, 0, false,
4187 true);
Alex Elder788e2df2013-01-17 12:25:27 -06004188
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004189 ceph_osdc_start_request(osdc, req, false);
4190 ret = ceph_osdc_wait_request(osdc, req);
4191 if (ret >= 0)
4192 ceph_copy_from_page_vector(pages, buf, 0, ret);
4193
4194out_req:
4195 ceph_osdc_put_request(req);
Alex Elder788e2df2013-01-17 12:25:27 -06004196 return ret;
4197}
4198
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004199/*
Alex Elder662518b2013-05-06 09:51:29 -05004200 * Read the complete header for the given rbd device. On successful
4201 * return, the rbd_dev->header field will contain up-to-date
4202 * information about the image.
Alex Elder4156d992012-08-02 11:29:46 -05004203 */
Alex Elder99a41eb2013-05-06 09:51:30 -05004204static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev)
Alex Elder4156d992012-08-02 11:29:46 -05004205{
4206 struct rbd_image_header_ondisk *ondisk = NULL;
4207 u32 snap_count = 0;
4208 u64 names_size = 0;
4209 u32 want_count;
4210 int ret;
4211
4212 /*
4213 * The complete header will include an array of its 64-bit
4214 * snapshot ids, followed by the names of those snapshots as
4215 * a contiguous block of NUL-terminated strings. Note that
4216 * the number of snapshots could change by the time we read
4217 * it in, in which case we re-read it.
4218 */
4219 do {
4220 size_t size;
4221
4222 kfree(ondisk);
4223
4224 size = sizeof (*ondisk);
4225 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
4226 size += names_size;
4227 ondisk = kmalloc(size, GFP_KERNEL);
4228 if (!ondisk)
Alex Elder662518b2013-05-06 09:51:29 -05004229 return -ENOMEM;
Alex Elder4156d992012-08-02 11:29:46 -05004230
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004231 ret = rbd_obj_read_sync(rbd_dev, &rbd_dev->header_oid,
4232 &rbd_dev->header_oloc, ondisk, size);
Alex Elder4156d992012-08-02 11:29:46 -05004233 if (ret < 0)
Alex Elder662518b2013-05-06 09:51:29 -05004234 goto out;
Alex Elderc0cd10db2013-04-26 09:43:47 -05004235 if ((size_t)ret < size) {
Alex Elder4156d992012-08-02 11:29:46 -05004236 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05004237 rbd_warn(rbd_dev, "short header read (want %zd got %d)",
4238 size, ret);
Alex Elder662518b2013-05-06 09:51:29 -05004239 goto out;
Alex Elder4156d992012-08-02 11:29:46 -05004240 }
4241 if (!rbd_dev_ondisk_valid(ondisk)) {
4242 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05004243 rbd_warn(rbd_dev, "invalid header");
Alex Elder662518b2013-05-06 09:51:29 -05004244 goto out;
Alex Elder4156d992012-08-02 11:29:46 -05004245 }
4246
4247 names_size = le64_to_cpu(ondisk->snap_names_len);
4248 want_count = snap_count;
4249 snap_count = le32_to_cpu(ondisk->snap_count);
4250 } while (snap_count != want_count);
4251
Alex Elder662518b2013-05-06 09:51:29 -05004252 ret = rbd_header_from_disk(rbd_dev, ondisk);
4253out:
Alex Elder4156d992012-08-02 11:29:46 -05004254 kfree(ondisk);
4255
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004256 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004257}
4258
Alex Elder15228ed2013-05-01 12:43:03 -05004259/*
4260 * Clear the rbd device's EXISTS flag if the snapshot it's mapped to
4261 * has disappeared from the (just updated) snapshot context.
4262 */
4263static void rbd_exists_validate(struct rbd_device *rbd_dev)
4264{
4265 u64 snap_id;
4266
4267 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags))
4268 return;
4269
4270 snap_id = rbd_dev->spec->snap_id;
4271 if (snap_id == CEPH_NOSNAP)
4272 return;
4273
4274 if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX)
4275 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
4276}
4277
Josh Durgin98752012013-08-29 17:26:31 -07004278static void rbd_dev_update_size(struct rbd_device *rbd_dev)
4279{
4280 sector_t size;
Josh Durgin98752012013-08-29 17:26:31 -07004281
4282 /*
Ilya Dryomov811c6682016-04-15 16:22:16 +02004283 * If EXISTS is not set, rbd_dev->disk may be NULL, so don't
4284 * try to update its size. If REMOVING is set, updating size
4285 * is just useless work since the device can't be opened.
Josh Durgin98752012013-08-29 17:26:31 -07004286 */
Ilya Dryomov811c6682016-04-15 16:22:16 +02004287 if (test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags) &&
4288 !test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) {
Josh Durgin98752012013-08-29 17:26:31 -07004289 size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE;
4290 dout("setting size to %llu sectors", (unsigned long long)size);
4291 set_capacity(rbd_dev->disk, size);
4292 revalidate_disk(rbd_dev->disk);
4293 }
4294}
4295
Alex Eldercc4a38bd2013-04-30 00:44:33 -05004296static int rbd_dev_refresh(struct rbd_device *rbd_dev)
Alex Elder1fe5e992012-07-25 09:32:41 -05004297{
Alex Eldere627db02013-05-06 07:40:30 -05004298 u64 mapping_size;
Alex Elder1fe5e992012-07-25 09:32:41 -05004299 int ret;
4300
Alex Eldercfbf6372013-05-31 17:40:45 -05004301 down_write(&rbd_dev->header_rwsem);
Alex Elder3b5cf2a2013-05-29 11:18:59 -05004302 mapping_size = rbd_dev->mapping.size;
Ilya Dryomova720ae02014-07-23 17:11:19 +04004303
4304 ret = rbd_dev_header_info(rbd_dev);
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04004305 if (ret)
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004306 goto out;
Alex Elder15228ed2013-05-01 12:43:03 -05004307
Ilya Dryomove8f59b52014-07-24 10:42:13 +04004308 /*
4309 * If there is a parent, see if it has disappeared due to the
4310 * mapped image getting flattened.
4311 */
4312 if (rbd_dev->parent) {
4313 ret = rbd_dev_v2_parent_info(rbd_dev);
4314 if (ret)
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004315 goto out;
Ilya Dryomove8f59b52014-07-24 10:42:13 +04004316 }
4317
Ilya Dryomov5ff11082014-07-23 17:11:21 +04004318 if (rbd_dev->spec->snap_id == CEPH_NOSNAP) {
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004319 rbd_dev->mapping.size = rbd_dev->header.image_size;
Ilya Dryomov5ff11082014-07-23 17:11:21 +04004320 } else {
4321 /* validate mapped snapshot's EXISTS flag */
4322 rbd_exists_validate(rbd_dev);
4323 }
Alex Elder15228ed2013-05-01 12:43:03 -05004324
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004325out:
Alex Eldercfbf6372013-05-31 17:40:45 -05004326 up_write(&rbd_dev->header_rwsem);
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004327 if (!ret && mapping_size != rbd_dev->mapping.size)
Josh Durgin98752012013-08-29 17:26:31 -07004328 rbd_dev_update_size(rbd_dev);
Alex Elder1fe5e992012-07-25 09:32:41 -05004329
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004330 return ret;
Alex Elder1fe5e992012-07-25 09:32:41 -05004331}
4332
Christoph Hellwigd6296d32017-05-01 10:19:08 -06004333static int rbd_init_request(struct blk_mq_tag_set *set, struct request *rq,
4334 unsigned int hctx_idx, unsigned int numa_node)
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004335{
4336 struct work_struct *work = blk_mq_rq_to_pdu(rq);
4337
4338 INIT_WORK(work, rbd_queue_workfn);
4339 return 0;
4340}
4341
Eric Biggersf363b082017-03-30 13:39:16 -07004342static const struct blk_mq_ops rbd_mq_ops = {
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004343 .queue_rq = rbd_queue_rq,
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004344 .init_request = rbd_init_request,
4345};
4346
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004347static int rbd_init_disk(struct rbd_device *rbd_dev)
4348{
4349 struct gendisk *disk;
4350 struct request_queue *q;
Alex Elder593a9e72012-02-07 12:03:37 -06004351 u64 segment_size;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004352 int err;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004353
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004354 /* create gendisk info */
Ilya Dryomov7e513d42013-12-16 19:26:32 +02004355 disk = alloc_disk(single_major ?
4356 (1 << RBD_SINGLE_MAJOR_PART_SHIFT) :
4357 RBD_MINORS_PER_MAJOR);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004358 if (!disk)
Alex Elder1fcdb8a2012-08-29 17:11:06 -05004359 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004360
Alex Elderf0f8cef2012-01-29 13:57:44 -06004361 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
Alex Elderde71a292012-07-03 16:01:19 -05004362 rbd_dev->dev_id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004363 disk->major = rbd_dev->major;
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004364 disk->first_minor = rbd_dev->minor;
Ilya Dryomov7e513d42013-12-16 19:26:32 +02004365 if (single_major)
4366 disk->flags |= GENHD_FL_EXT_DEVT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004367 disk->fops = &rbd_bd_ops;
4368 disk->private_data = rbd_dev;
4369
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004370 memset(&rbd_dev->tag_set, 0, sizeof(rbd_dev->tag_set));
4371 rbd_dev->tag_set.ops = &rbd_mq_ops;
Ilya Dryomovb5584182015-06-23 16:21:19 +03004372 rbd_dev->tag_set.queue_depth = rbd_dev->opts->queue_depth;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004373 rbd_dev->tag_set.numa_node = NUMA_NO_NODE;
Ilya Dryomovb5584182015-06-23 16:21:19 +03004374 rbd_dev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004375 rbd_dev->tag_set.nr_hw_queues = 1;
4376 rbd_dev->tag_set.cmd_size = sizeof(struct work_struct);
4377
4378 err = blk_mq_alloc_tag_set(&rbd_dev->tag_set);
4379 if (err)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004380 goto out_disk;
Josh Durgin029bcbd2011-07-22 11:35:23 -07004381
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004382 q = blk_mq_init_queue(&rbd_dev->tag_set);
4383 if (IS_ERR(q)) {
4384 err = PTR_ERR(q);
4385 goto out_tag_set;
4386 }
4387
Ilya Dryomovd8a2c892015-03-24 16:15:17 +03004388 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
4389 /* QUEUE_FLAG_ADD_RANDOM is off by default for blk-mq */
Alex Elder593a9e72012-02-07 12:03:37 -06004390
Josh Durgin029bcbd2011-07-22 11:35:23 -07004391 /* set io sizes to object size */
Alex Elder593a9e72012-02-07 12:03:37 -06004392 segment_size = rbd_obj_bytes(&rbd_dev->header);
4393 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
Ilya Dryomov0d9fde42015-10-07 16:09:35 +02004394 q->limits.max_sectors = queue_max_hw_sectors(q);
Ilya Dryomov21acdf42017-12-21 15:35:11 +01004395 blk_queue_max_segments(q, USHRT_MAX);
Ilya Dryomov24f1df62018-01-12 17:22:10 +01004396 blk_queue_max_segment_size(q, UINT_MAX);
Alex Elder593a9e72012-02-07 12:03:37 -06004397 blk_queue_io_min(q, segment_size);
4398 blk_queue_io_opt(q, segment_size);
Josh Durgin029bcbd2011-07-22 11:35:23 -07004399
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004400 /* enable the discard support */
4401 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
4402 q->limits.discard_granularity = segment_size;
Jens Axboe2bb4cd52015-07-14 08:15:12 -06004403 blk_queue_max_discard_sectors(q, segment_size / SECTOR_SIZE);
Ilya Dryomov6ac56952017-05-22 19:59:24 +02004404 blk_queue_max_write_zeroes_sectors(q, segment_size / SECTOR_SIZE);
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004405
Ronny Hegewaldbae818e2015-10-15 18:50:46 +00004406 if (!ceph_test_opt(rbd_dev->rbd_client->client, NOCRC))
Jan Karadc3b17c2017-02-02 15:56:50 +01004407 q->backing_dev_info->capabilities |= BDI_CAP_STABLE_WRITES;
Ronny Hegewaldbae818e2015-10-15 18:50:46 +00004408
Ilya Dryomov5769ed02017-04-13 12:17:38 +02004409 /*
4410 * disk_release() expects a queue ref from add_disk() and will
4411 * put it. Hold an extra ref until add_disk() is called.
4412 */
4413 WARN_ON(!blk_get_queue(q));
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004414 disk->queue = q;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004415 q->queuedata = rbd_dev;
4416
4417 rbd_dev->disk = disk;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004418
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004419 return 0;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004420out_tag_set:
4421 blk_mq_free_tag_set(&rbd_dev->tag_set);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004422out_disk:
4423 put_disk(disk);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004424 return err;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004425}
4426
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004427/*
4428 sysfs
4429*/
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004430
Alex Elder593a9e72012-02-07 12:03:37 -06004431static struct rbd_device *dev_to_rbd_dev(struct device *dev)
4432{
4433 return container_of(dev, struct rbd_device, dev);
4434}
4435
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004436static ssize_t rbd_size_show(struct device *dev,
4437 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004438{
Alex Elder593a9e72012-02-07 12:03:37 -06004439 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004440
Alex Elderfc71d832013-04-26 15:44:36 -05004441 return sprintf(buf, "%llu\n",
4442 (unsigned long long)rbd_dev->mapping.size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004443}
4444
Alex Elder34b13182012-07-13 20:35:12 -05004445/*
4446 * Note this shows the features for whatever's mapped, which is not
4447 * necessarily the base image.
4448 */
4449static ssize_t rbd_features_show(struct device *dev,
4450 struct device_attribute *attr, char *buf)
4451{
4452 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4453
4454 return sprintf(buf, "0x%016llx\n",
Alex Elderfc71d832013-04-26 15:44:36 -05004455 (unsigned long long)rbd_dev->mapping.features);
Alex Elder34b13182012-07-13 20:35:12 -05004456}
4457
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004458static ssize_t rbd_major_show(struct device *dev,
4459 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004460{
Alex Elder593a9e72012-02-07 12:03:37 -06004461 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004462
Alex Elderfc71d832013-04-26 15:44:36 -05004463 if (rbd_dev->major)
4464 return sprintf(buf, "%d\n", rbd_dev->major);
4465
4466 return sprintf(buf, "(none)\n");
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004467}
Alex Elderfc71d832013-04-26 15:44:36 -05004468
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004469static ssize_t rbd_minor_show(struct device *dev,
4470 struct device_attribute *attr, char *buf)
4471{
4472 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4473
4474 return sprintf(buf, "%d\n", rbd_dev->minor);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004475}
4476
Ilya Dryomov005a07bf2016-08-18 18:38:43 +02004477static ssize_t rbd_client_addr_show(struct device *dev,
4478 struct device_attribute *attr, char *buf)
4479{
4480 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4481 struct ceph_entity_addr *client_addr =
4482 ceph_client_addr(rbd_dev->rbd_client->client);
4483
4484 return sprintf(buf, "%pISpc/%u\n", &client_addr->in_addr,
4485 le32_to_cpu(client_addr->nonce));
4486}
4487
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004488static ssize_t rbd_client_id_show(struct device *dev,
4489 struct device_attribute *attr, char *buf)
4490{
Alex Elder593a9e72012-02-07 12:03:37 -06004491 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004492
Alex Elder1dbb4392012-01-24 10:08:37 -06004493 return sprintf(buf, "client%lld\n",
Ilya Dryomov033268a2016-08-12 14:59:58 +02004494 ceph_client_gid(rbd_dev->rbd_client->client));
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004495}
4496
Mike Christie267fb902016-08-18 18:38:43 +02004497static ssize_t rbd_cluster_fsid_show(struct device *dev,
4498 struct device_attribute *attr, char *buf)
4499{
4500 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4501
4502 return sprintf(buf, "%pU\n", &rbd_dev->rbd_client->client->fsid);
4503}
4504
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02004505static ssize_t rbd_config_info_show(struct device *dev,
4506 struct device_attribute *attr, char *buf)
4507{
4508 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4509
4510 return sprintf(buf, "%s\n", rbd_dev->config_info);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004511}
4512
4513static ssize_t rbd_pool_show(struct device *dev,
4514 struct device_attribute *attr, char *buf)
4515{
Alex Elder593a9e72012-02-07 12:03:37 -06004516 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004517
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004518 return sprintf(buf, "%s\n", rbd_dev->spec->pool_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004519}
4520
Alex Elder9bb2f332012-07-12 10:46:35 -05004521static ssize_t rbd_pool_id_show(struct device *dev,
4522 struct device_attribute *attr, char *buf)
4523{
4524 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4525
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004526 return sprintf(buf, "%llu\n",
Alex Elderfc71d832013-04-26 15:44:36 -05004527 (unsigned long long) rbd_dev->spec->pool_id);
Alex Elder9bb2f332012-07-12 10:46:35 -05004528}
4529
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004530static ssize_t rbd_name_show(struct device *dev,
4531 struct device_attribute *attr, char *buf)
4532{
Alex Elder593a9e72012-02-07 12:03:37 -06004533 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004534
Alex Eldera92ffdf2012-10-30 19:40:33 -05004535 if (rbd_dev->spec->image_name)
4536 return sprintf(buf, "%s\n", rbd_dev->spec->image_name);
4537
4538 return sprintf(buf, "(unknown)\n");
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004539}
4540
Alex Elder589d30e2012-07-10 20:30:11 -05004541static ssize_t rbd_image_id_show(struct device *dev,
4542 struct device_attribute *attr, char *buf)
4543{
4544 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4545
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004546 return sprintf(buf, "%s\n", rbd_dev->spec->image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05004547}
4548
Alex Elder34b13182012-07-13 20:35:12 -05004549/*
4550 * Shows the name of the currently-mapped snapshot (or
4551 * RBD_SNAP_HEAD_NAME for the base image).
4552 */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004553static ssize_t rbd_snap_show(struct device *dev,
4554 struct device_attribute *attr,
4555 char *buf)
4556{
Alex Elder593a9e72012-02-07 12:03:37 -06004557 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004558
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004559 return sprintf(buf, "%s\n", rbd_dev->spec->snap_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004560}
4561
Mike Christie92a58672016-08-18 18:38:44 +02004562static ssize_t rbd_snap_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
4567 return sprintf(buf, "%llu\n", rbd_dev->spec->snap_id);
4568}
4569
Alex Elder86b00e02012-10-25 23:34:42 -05004570/*
Ilya Dryomovff961282014-07-22 21:53:07 +04004571 * For a v2 image, shows the chain of parent images, separated by empty
4572 * lines. For v1 images or if there is no parent, shows "(no parent
4573 * image)".
Alex Elder86b00e02012-10-25 23:34:42 -05004574 */
4575static ssize_t rbd_parent_show(struct device *dev,
Ilya Dryomovff961282014-07-22 21:53:07 +04004576 struct device_attribute *attr,
4577 char *buf)
Alex Elder86b00e02012-10-25 23:34:42 -05004578{
4579 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Ilya Dryomovff961282014-07-22 21:53:07 +04004580 ssize_t count = 0;
Alex Elder86b00e02012-10-25 23:34:42 -05004581
Ilya Dryomovff961282014-07-22 21:53:07 +04004582 if (!rbd_dev->parent)
Alex Elder86b00e02012-10-25 23:34:42 -05004583 return sprintf(buf, "(no parent image)\n");
4584
Ilya Dryomovff961282014-07-22 21:53:07 +04004585 for ( ; rbd_dev->parent; rbd_dev = rbd_dev->parent) {
4586 struct rbd_spec *spec = rbd_dev->parent_spec;
Alex Elder86b00e02012-10-25 23:34:42 -05004587
Ilya Dryomovff961282014-07-22 21:53:07 +04004588 count += sprintf(&buf[count], "%s"
4589 "pool_id %llu\npool_name %s\n"
4590 "image_id %s\nimage_name %s\n"
4591 "snap_id %llu\nsnap_name %s\n"
4592 "overlap %llu\n",
4593 !count ? "" : "\n", /* first? */
4594 spec->pool_id, spec->pool_name,
4595 spec->image_id, spec->image_name ?: "(unknown)",
4596 spec->snap_id, spec->snap_name,
4597 rbd_dev->parent_overlap);
4598 }
Alex Elder86b00e02012-10-25 23:34:42 -05004599
Ilya Dryomovff961282014-07-22 21:53:07 +04004600 return count;
Alex Elder86b00e02012-10-25 23:34:42 -05004601}
4602
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004603static ssize_t rbd_image_refresh(struct device *dev,
4604 struct device_attribute *attr,
4605 const char *buf,
4606 size_t size)
4607{
Alex Elder593a9e72012-02-07 12:03:37 -06004608 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Alex Elderb8136232012-07-25 09:32:41 -05004609 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004610
Alex Eldercc4a38bd2013-04-30 00:44:33 -05004611 ret = rbd_dev_refresh(rbd_dev);
Alex Eldere627db02013-05-06 07:40:30 -05004612 if (ret)
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04004613 return ret;
Alex Elderb8136232012-07-25 09:32:41 -05004614
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04004615 return size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004616}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004617
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004618static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
Alex Elder34b13182012-07-13 20:35:12 -05004619static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004620static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004621static DEVICE_ATTR(minor, S_IRUGO, rbd_minor_show, NULL);
Ilya Dryomov005a07bf2016-08-18 18:38:43 +02004622static DEVICE_ATTR(client_addr, S_IRUGO, rbd_client_addr_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004623static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
Mike Christie267fb902016-08-18 18:38:43 +02004624static DEVICE_ATTR(cluster_fsid, S_IRUGO, rbd_cluster_fsid_show, NULL);
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02004625static DEVICE_ATTR(config_info, S_IRUSR, rbd_config_info_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004626static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
Alex Elder9bb2f332012-07-12 10:46:35 -05004627static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004628static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
Alex Elder589d30e2012-07-10 20:30:11 -05004629static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004630static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
4631static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
Mike Christie92a58672016-08-18 18:38:44 +02004632static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL);
Alex Elder86b00e02012-10-25 23:34:42 -05004633static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004634
4635static struct attribute *rbd_attrs[] = {
4636 &dev_attr_size.attr,
Alex Elder34b13182012-07-13 20:35:12 -05004637 &dev_attr_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004638 &dev_attr_major.attr,
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004639 &dev_attr_minor.attr,
Ilya Dryomov005a07bf2016-08-18 18:38:43 +02004640 &dev_attr_client_addr.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004641 &dev_attr_client_id.attr,
Mike Christie267fb902016-08-18 18:38:43 +02004642 &dev_attr_cluster_fsid.attr,
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02004643 &dev_attr_config_info.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004644 &dev_attr_pool.attr,
Alex Elder9bb2f332012-07-12 10:46:35 -05004645 &dev_attr_pool_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004646 &dev_attr_name.attr,
Alex Elder589d30e2012-07-10 20:30:11 -05004647 &dev_attr_image_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004648 &dev_attr_current_snap.attr,
Mike Christie92a58672016-08-18 18:38:44 +02004649 &dev_attr_snap_id.attr,
Alex Elder86b00e02012-10-25 23:34:42 -05004650 &dev_attr_parent.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004651 &dev_attr_refresh.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004652 NULL
4653};
4654
4655static struct attribute_group rbd_attr_group = {
4656 .attrs = rbd_attrs,
4657};
4658
4659static const struct attribute_group *rbd_attr_groups[] = {
4660 &rbd_attr_group,
4661 NULL
4662};
4663
Ilya Dryomov6cac4692015-10-16 20:11:25 +02004664static void rbd_dev_release(struct device *dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004665
Bhumika Goyalb9942bc2017-02-11 12:14:38 +05304666static const struct device_type rbd_device_type = {
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004667 .name = "rbd",
4668 .groups = rbd_attr_groups,
Ilya Dryomov6cac4692015-10-16 20:11:25 +02004669 .release = rbd_dev_release,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004670};
4671
Alex Elder8b8fb992012-10-26 17:25:24 -05004672static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)
4673{
4674 kref_get(&spec->kref);
4675
4676 return spec;
4677}
4678
4679static void rbd_spec_free(struct kref *kref);
4680static void rbd_spec_put(struct rbd_spec *spec)
4681{
4682 if (spec)
4683 kref_put(&spec->kref, rbd_spec_free);
4684}
4685
4686static struct rbd_spec *rbd_spec_alloc(void)
4687{
4688 struct rbd_spec *spec;
4689
4690 spec = kzalloc(sizeof (*spec), GFP_KERNEL);
4691 if (!spec)
4692 return NULL;
Ilya Dryomov04077592014-07-23 17:11:20 +04004693
4694 spec->pool_id = CEPH_NOPOOL;
4695 spec->snap_id = CEPH_NOSNAP;
Alex Elder8b8fb992012-10-26 17:25:24 -05004696 kref_init(&spec->kref);
4697
Alex Elder8b8fb992012-10-26 17:25:24 -05004698 return spec;
4699}
4700
4701static void rbd_spec_free(struct kref *kref)
4702{
4703 struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref);
4704
4705 kfree(spec->pool_name);
4706 kfree(spec->image_id);
4707 kfree(spec->image_name);
4708 kfree(spec->snap_name);
4709 kfree(spec);
4710}
4711
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004712static void rbd_dev_free(struct rbd_device *rbd_dev)
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004713{
Ilya Dryomov99d16942016-08-12 16:11:41 +02004714 WARN_ON(rbd_dev->watch_state != RBD_WATCH_STATE_UNREGISTERED);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004715 WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_UNLOCKED);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004716
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004717 ceph_oid_destroy(&rbd_dev->header_oid);
Ilya Dryomov6b6dddb2016-08-05 16:15:38 +02004718 ceph_oloc_destroy(&rbd_dev->header_oloc);
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02004719 kfree(rbd_dev->config_info);
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004720
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004721 rbd_put_client(rbd_dev->rbd_client);
4722 rbd_spec_put(rbd_dev->spec);
4723 kfree(rbd_dev->opts);
4724 kfree(rbd_dev);
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004725}
4726
4727static void rbd_dev_release(struct device *dev)
4728{
4729 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4730 bool need_put = !!rbd_dev->opts;
4731
4732 if (need_put) {
4733 destroy_workqueue(rbd_dev->task_wq);
4734 ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id);
4735 }
4736
4737 rbd_dev_free(rbd_dev);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004738
4739 /*
4740 * This is racy, but way better than putting module outside of
4741 * the release callback. The race window is pretty small, so
4742 * doing something similar to dm (dm-builtin.c) is overkill.
4743 */
4744 if (need_put)
4745 module_put(THIS_MODULE);
4746}
4747
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004748static struct rbd_device *__rbd_dev_create(struct rbd_client *rbdc,
4749 struct rbd_spec *spec)
Alex Elderc53d5892012-10-25 23:34:42 -05004750{
4751 struct rbd_device *rbd_dev;
4752
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004753 rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL);
Alex Elderc53d5892012-10-25 23:34:42 -05004754 if (!rbd_dev)
4755 return NULL;
4756
4757 spin_lock_init(&rbd_dev->lock);
4758 INIT_LIST_HEAD(&rbd_dev->node);
Alex Elderc53d5892012-10-25 23:34:42 -05004759 init_rwsem(&rbd_dev->header_rwsem);
4760
Ilya Dryomov7e973322017-01-25 18:16:22 +01004761 rbd_dev->header.data_pool_id = CEPH_NOPOOL;
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004762 ceph_oid_init(&rbd_dev->header_oid);
Ilya Dryomov431a02c2017-01-25 18:16:21 +01004763 rbd_dev->header_oloc.pool = spec->pool_id;
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004764
Ilya Dryomov99d16942016-08-12 16:11:41 +02004765 mutex_init(&rbd_dev->watch_mutex);
4766 rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED;
4767 INIT_DELAYED_WORK(&rbd_dev->watch_dwork, rbd_reregister_watch);
4768
Ilya Dryomoved95b212016-08-12 16:40:02 +02004769 init_rwsem(&rbd_dev->lock_rwsem);
4770 rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED;
4771 INIT_WORK(&rbd_dev->acquired_lock_work, rbd_notify_acquired_lock);
4772 INIT_WORK(&rbd_dev->released_lock_work, rbd_notify_released_lock);
4773 INIT_DELAYED_WORK(&rbd_dev->lock_dwork, rbd_acquire_lock);
4774 INIT_WORK(&rbd_dev->unlock_work, rbd_release_lock_work);
4775 init_waitqueue_head(&rbd_dev->lock_waitq);
4776
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004777 rbd_dev->dev.bus = &rbd_bus_type;
4778 rbd_dev->dev.type = &rbd_device_type;
4779 rbd_dev->dev.parent = &rbd_root_dev;
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004780 device_initialize(&rbd_dev->dev);
4781
Alex Elderc53d5892012-10-25 23:34:42 -05004782 rbd_dev->rbd_client = rbdc;
Ilya Dryomovd1475432015-06-22 13:24:48 +03004783 rbd_dev->spec = spec;
Alex Elder0903e872012-11-14 12:25:19 -06004784
Alex Elderc53d5892012-10-25 23:34:42 -05004785 return rbd_dev;
4786}
4787
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004788/*
4789 * Create a mapping rbd_dev.
4790 */
4791static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,
4792 struct rbd_spec *spec,
4793 struct rbd_options *opts)
4794{
4795 struct rbd_device *rbd_dev;
4796
4797 rbd_dev = __rbd_dev_create(rbdc, spec);
4798 if (!rbd_dev)
4799 return NULL;
4800
4801 rbd_dev->opts = opts;
4802
4803 /* get an id and fill in device name */
4804 rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0,
4805 minor_to_rbd_dev_id(1 << MINORBITS),
4806 GFP_KERNEL);
4807 if (rbd_dev->dev_id < 0)
4808 goto fail_rbd_dev;
4809
4810 sprintf(rbd_dev->name, RBD_DRV_NAME "%d", rbd_dev->dev_id);
4811 rbd_dev->task_wq = alloc_ordered_workqueue("%s-tasks", WQ_MEM_RECLAIM,
4812 rbd_dev->name);
4813 if (!rbd_dev->task_wq)
4814 goto fail_dev_id;
4815
4816 /* we have a ref from do_rbd_add() */
4817 __module_get(THIS_MODULE);
4818
4819 dout("%s rbd_dev %p dev_id %d\n", __func__, rbd_dev, rbd_dev->dev_id);
4820 return rbd_dev;
4821
4822fail_dev_id:
4823 ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id);
4824fail_rbd_dev:
4825 rbd_dev_free(rbd_dev);
4826 return NULL;
4827}
4828
Alex Elderc53d5892012-10-25 23:34:42 -05004829static void rbd_dev_destroy(struct rbd_device *rbd_dev)
4830{
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004831 if (rbd_dev)
4832 put_device(&rbd_dev->dev);
Alex Elderc53d5892012-10-25 23:34:42 -05004833}
4834
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004835/*
Alex Elder9d475de2012-07-03 16:01:19 -05004836 * Get the size and object order for an image snapshot, or if
4837 * snap_id is CEPH_NOSNAP, gets this information for the base
4838 * image.
4839 */
4840static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
4841 u8 *order, u64 *snap_size)
4842{
4843 __le64 snapid = cpu_to_le64(snap_id);
4844 int ret;
4845 struct {
4846 u8 order;
4847 __le64 size;
4848 } __attribute__ ((packed)) size_buf = { 0 };
4849
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004850 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
4851 &rbd_dev->header_oloc, "get_size",
4852 &snapid, sizeof(snapid),
4853 &size_buf, sizeof(size_buf));
Alex Elder36be9a72013-01-19 00:30:28 -06004854 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder9d475de2012-07-03 16:01:19 -05004855 if (ret < 0)
4856 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05004857 if (ret < sizeof (size_buf))
4858 return -ERANGE;
Alex Elder9d475de2012-07-03 16:01:19 -05004859
Josh Durginc3545572013-08-28 17:08:10 -07004860 if (order) {
Alex Elderc86f86e2013-04-25 15:09:41 -05004861 *order = size_buf.order;
Josh Durginc3545572013-08-28 17:08:10 -07004862 dout(" order %u", (unsigned int)*order);
4863 }
Alex Elder9d475de2012-07-03 16:01:19 -05004864 *snap_size = le64_to_cpu(size_buf.size);
4865
Josh Durginc3545572013-08-28 17:08:10 -07004866 dout(" snap_id 0x%016llx snap_size = %llu\n",
4867 (unsigned long long)snap_id,
Alex Elder57385b52013-04-21 12:14:45 -05004868 (unsigned long long)*snap_size);
Alex Elder9d475de2012-07-03 16:01:19 -05004869
4870 return 0;
4871}
4872
4873static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
4874{
4875 return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
4876 &rbd_dev->header.obj_order,
4877 &rbd_dev->header.image_size);
4878}
4879
Alex Elder1e130192012-07-03 16:01:19 -05004880static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
4881{
4882 void *reply_buf;
4883 int ret;
4884 void *p;
4885
4886 reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL);
4887 if (!reply_buf)
4888 return -ENOMEM;
4889
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004890 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
4891 &rbd_dev->header_oloc, "get_object_prefix",
4892 NULL, 0, reply_buf, RBD_OBJ_PREFIX_LEN_MAX);
Alex Elder36be9a72013-01-19 00:30:28 -06004893 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder1e130192012-07-03 16:01:19 -05004894 if (ret < 0)
4895 goto out;
4896
4897 p = reply_buf;
4898 rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p,
Alex Elder57385b52013-04-21 12:14:45 -05004899 p + ret, NULL, GFP_NOIO);
4900 ret = 0;
Alex Elder1e130192012-07-03 16:01:19 -05004901
4902 if (IS_ERR(rbd_dev->header.object_prefix)) {
4903 ret = PTR_ERR(rbd_dev->header.object_prefix);
4904 rbd_dev->header.object_prefix = NULL;
4905 } else {
4906 dout(" object_prefix = %s\n", rbd_dev->header.object_prefix);
4907 }
Alex Elder1e130192012-07-03 16:01:19 -05004908out:
4909 kfree(reply_buf);
4910
4911 return ret;
4912}
4913
Alex Elderb1b54022012-07-03 16:01:19 -05004914static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
4915 u64 *snap_features)
4916{
4917 __le64 snapid = cpu_to_le64(snap_id);
4918 struct {
4919 __le64 features;
4920 __le64 incompat;
Alex Elder41579762013-04-21 12:14:45 -05004921 } __attribute__ ((packed)) features_buf = { 0 };
Ilya Dryomovd3767f02016-04-13 14:15:50 +02004922 u64 unsup;
Alex Elderb1b54022012-07-03 16:01:19 -05004923 int ret;
4924
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004925 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
4926 &rbd_dev->header_oloc, "get_features",
4927 &snapid, sizeof(snapid),
4928 &features_buf, sizeof(features_buf));
Alex Elder36be9a72013-01-19 00:30:28 -06004929 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderb1b54022012-07-03 16:01:19 -05004930 if (ret < 0)
4931 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05004932 if (ret < sizeof (features_buf))
4933 return -ERANGE;
Alex Elderd8891402012-10-09 13:50:17 -07004934
Ilya Dryomovd3767f02016-04-13 14:15:50 +02004935 unsup = le64_to_cpu(features_buf.incompat) & ~RBD_FEATURES_SUPPORTED;
4936 if (unsup) {
4937 rbd_warn(rbd_dev, "image uses unsupported features: 0x%llx",
4938 unsup);
Alex Elderb8f5c6e2012-11-01 08:39:26 -05004939 return -ENXIO;
Ilya Dryomovd3767f02016-04-13 14:15:50 +02004940 }
Alex Elderd8891402012-10-09 13:50:17 -07004941
Alex Elderb1b54022012-07-03 16:01:19 -05004942 *snap_features = le64_to_cpu(features_buf.features);
4943
4944 dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
Alex Elder57385b52013-04-21 12:14:45 -05004945 (unsigned long long)snap_id,
4946 (unsigned long long)*snap_features,
4947 (unsigned long long)le64_to_cpu(features_buf.incompat));
Alex Elderb1b54022012-07-03 16:01:19 -05004948
4949 return 0;
4950}
4951
4952static int rbd_dev_v2_features(struct rbd_device *rbd_dev)
4953{
4954 return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP,
4955 &rbd_dev->header.features);
4956}
4957
Alex Elder86b00e02012-10-25 23:34:42 -05004958static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev)
4959{
4960 struct rbd_spec *parent_spec;
4961 size_t size;
4962 void *reply_buf = NULL;
4963 __le64 snapid;
4964 void *p;
4965 void *end;
Alex Elder642a2532013-05-06 17:40:33 -05004966 u64 pool_id;
Alex Elder86b00e02012-10-25 23:34:42 -05004967 char *image_id;
Alex Elder3b5cf2a2013-05-29 11:18:59 -05004968 u64 snap_id;
Alex Elder86b00e02012-10-25 23:34:42 -05004969 u64 overlap;
Alex Elder86b00e02012-10-25 23:34:42 -05004970 int ret;
4971
4972 parent_spec = rbd_spec_alloc();
4973 if (!parent_spec)
4974 return -ENOMEM;
4975
4976 size = sizeof (__le64) + /* pool_id */
4977 sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */
4978 sizeof (__le64) + /* snap_id */
4979 sizeof (__le64); /* overlap */
4980 reply_buf = kmalloc(size, GFP_KERNEL);
4981 if (!reply_buf) {
4982 ret = -ENOMEM;
4983 goto out_err;
4984 }
4985
Ilya Dryomov4d9b67c2014-07-24 10:42:13 +04004986 snapid = cpu_to_le64(rbd_dev->spec->snap_id);
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004987 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
4988 &rbd_dev->header_oloc, "get_parent",
4989 &snapid, sizeof(snapid), reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06004990 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder86b00e02012-10-25 23:34:42 -05004991 if (ret < 0)
4992 goto out_err;
4993
Alex Elder86b00e02012-10-25 23:34:42 -05004994 p = reply_buf;
Alex Elder57385b52013-04-21 12:14:45 -05004995 end = reply_buf + ret;
4996 ret = -ERANGE;
Alex Elder642a2532013-05-06 17:40:33 -05004997 ceph_decode_64_safe(&p, end, pool_id, out_err);
Alex Elder392a9da2013-05-06 17:40:33 -05004998 if (pool_id == CEPH_NOPOOL) {
4999 /*
5000 * Either the parent never existed, or we have
5001 * record of it but the image got flattened so it no
5002 * longer has a parent. When the parent of a
5003 * layered image disappears we immediately set the
5004 * overlap to 0. The effect of this is that all new
5005 * requests will be treated as if the image had no
5006 * parent.
5007 */
5008 if (rbd_dev->parent_overlap) {
5009 rbd_dev->parent_overlap = 0;
Alex Elder392a9da2013-05-06 17:40:33 -05005010 rbd_dev_parent_put(rbd_dev);
5011 pr_info("%s: clone image has been flattened\n",
5012 rbd_dev->disk->disk_name);
5013 }
5014
Alex Elder86b00e02012-10-25 23:34:42 -05005015 goto out; /* No parent? No problem. */
Alex Elder392a9da2013-05-06 17:40:33 -05005016 }
Alex Elder86b00e02012-10-25 23:34:42 -05005017
Alex Elder0903e872012-11-14 12:25:19 -06005018 /* The ceph file layout needs to fit pool id in 32 bits */
5019
5020 ret = -EIO;
Alex Elder642a2532013-05-06 17:40:33 -05005021 if (pool_id > (u64)U32_MAX) {
Ilya Dryomov9584d502014-07-11 12:11:20 +04005022 rbd_warn(NULL, "parent pool id too large (%llu > %u)",
Alex Elder642a2532013-05-06 17:40:33 -05005023 (unsigned long long)pool_id, U32_MAX);
Alex Elder57385b52013-04-21 12:14:45 -05005024 goto out_err;
Alex Elderc0cd10db2013-04-26 09:43:47 -05005025 }
Alex Elder0903e872012-11-14 12:25:19 -06005026
Alex Elder979ed482012-11-01 08:39:26 -05005027 image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elder86b00e02012-10-25 23:34:42 -05005028 if (IS_ERR(image_id)) {
5029 ret = PTR_ERR(image_id);
5030 goto out_err;
5031 }
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005032 ceph_decode_64_safe(&p, end, snap_id, out_err);
Alex Elder86b00e02012-10-25 23:34:42 -05005033 ceph_decode_64_safe(&p, end, overlap, out_err);
5034
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005035 /*
5036 * The parent won't change (except when the clone is
5037 * flattened, already handled that). So we only need to
5038 * record the parent spec we have not already done so.
5039 */
5040 if (!rbd_dev->parent_spec) {
5041 parent_spec->pool_id = pool_id;
5042 parent_spec->image_id = image_id;
5043 parent_spec->snap_id = snap_id;
Alex Elder70cf49c2013-05-06 17:40:33 -05005044 rbd_dev->parent_spec = parent_spec;
5045 parent_spec = NULL; /* rbd_dev now owns this */
Ilya Dryomovfbba11b2014-06-27 21:46:33 +04005046 } else {
5047 kfree(image_id);
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005048 }
5049
5050 /*
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005051 * We always update the parent overlap. If it's zero we issue
5052 * a warning, as we will proceed as if there was no parent.
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005053 */
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005054 if (!overlap) {
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005055 if (parent_spec) {
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005056 /* refresh, careful to warn just once */
5057 if (rbd_dev->parent_overlap)
5058 rbd_warn(rbd_dev,
5059 "clone now standalone (overlap became 0)");
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005060 } else {
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005061 /* initial probe */
5062 rbd_warn(rbd_dev, "clone is standalone (overlap 0)");
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005063 }
Alex Elder70cf49c2013-05-06 17:40:33 -05005064 }
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005065 rbd_dev->parent_overlap = overlap;
5066
Alex Elder86b00e02012-10-25 23:34:42 -05005067out:
5068 ret = 0;
5069out_err:
5070 kfree(reply_buf);
5071 rbd_spec_put(parent_spec);
5072
5073 return ret;
5074}
5075
Alex Eldercc070d52013-04-21 12:14:45 -05005076static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev)
5077{
5078 struct {
5079 __le64 stripe_unit;
5080 __le64 stripe_count;
5081 } __attribute__ ((packed)) striping_info_buf = { 0 };
5082 size_t size = sizeof (striping_info_buf);
5083 void *p;
5084 u64 obj_size;
5085 u64 stripe_unit;
5086 u64 stripe_count;
5087 int ret;
5088
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005089 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5090 &rbd_dev->header_oloc, "get_stripe_unit_count",
5091 NULL, 0, &striping_info_buf, size);
Alex Eldercc070d52013-04-21 12:14:45 -05005092 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
5093 if (ret < 0)
5094 return ret;
5095 if (ret < size)
5096 return -ERANGE;
5097
5098 /*
5099 * We don't actually support the "fancy striping" feature
5100 * (STRIPINGV2) yet, but if the striping sizes are the
5101 * defaults the behavior is the same as before. So find
5102 * out, and only fail if the image has non-default values.
5103 */
5104 ret = -EINVAL;
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +01005105 obj_size = rbd_obj_bytes(&rbd_dev->header);
Alex Eldercc070d52013-04-21 12:14:45 -05005106 p = &striping_info_buf;
5107 stripe_unit = ceph_decode_64(&p);
5108 if (stripe_unit != obj_size) {
5109 rbd_warn(rbd_dev, "unsupported stripe unit "
5110 "(got %llu want %llu)",
5111 stripe_unit, obj_size);
5112 return -EINVAL;
5113 }
5114 stripe_count = ceph_decode_64(&p);
5115 if (stripe_count != 1) {
5116 rbd_warn(rbd_dev, "unsupported stripe count "
5117 "(got %llu want 1)", stripe_count);
5118 return -EINVAL;
5119 }
Alex Elder500d0c02013-04-26 09:43:47 -05005120 rbd_dev->header.stripe_unit = stripe_unit;
5121 rbd_dev->header.stripe_count = stripe_count;
Alex Eldercc070d52013-04-21 12:14:45 -05005122
5123 return 0;
5124}
5125
Ilya Dryomov7e973322017-01-25 18:16:22 +01005126static int rbd_dev_v2_data_pool(struct rbd_device *rbd_dev)
5127{
5128 __le64 data_pool_id;
5129 int ret;
5130
5131 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5132 &rbd_dev->header_oloc, "get_data_pool",
5133 NULL, 0, &data_pool_id, sizeof(data_pool_id));
5134 if (ret < 0)
5135 return ret;
5136 if (ret < sizeof(data_pool_id))
5137 return -EBADMSG;
5138
5139 rbd_dev->header.data_pool_id = le64_to_cpu(data_pool_id);
5140 WARN_ON(rbd_dev->header.data_pool_id == CEPH_NOPOOL);
5141 return 0;
5142}
5143
Alex Elder9e15b772012-10-30 19:40:33 -05005144static char *rbd_dev_image_name(struct rbd_device *rbd_dev)
5145{
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005146 CEPH_DEFINE_OID_ONSTACK(oid);
Alex Elder9e15b772012-10-30 19:40:33 -05005147 size_t image_id_size;
5148 char *image_id;
5149 void *p;
5150 void *end;
5151 size_t size;
5152 void *reply_buf = NULL;
5153 size_t len = 0;
5154 char *image_name = NULL;
5155 int ret;
5156
5157 rbd_assert(!rbd_dev->spec->image_name);
5158
Alex Elder69e7a022012-11-01 08:39:26 -05005159 len = strlen(rbd_dev->spec->image_id);
5160 image_id_size = sizeof (__le32) + len;
Alex Elder9e15b772012-10-30 19:40:33 -05005161 image_id = kmalloc(image_id_size, GFP_KERNEL);
5162 if (!image_id)
5163 return NULL;
5164
5165 p = image_id;
Alex Elder41579762013-04-21 12:14:45 -05005166 end = image_id + image_id_size;
Alex Elder57385b52013-04-21 12:14:45 -05005167 ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len);
Alex Elder9e15b772012-10-30 19:40:33 -05005168
5169 size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX;
5170 reply_buf = kmalloc(size, GFP_KERNEL);
5171 if (!reply_buf)
5172 goto out;
5173
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005174 ceph_oid_printf(&oid, "%s", RBD_DIRECTORY);
5175 ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc,
5176 "dir_get_name", image_id, image_id_size,
5177 reply_buf, size);
Alex Elder9e15b772012-10-30 19:40:33 -05005178 if (ret < 0)
5179 goto out;
5180 p = reply_buf;
Alex Elderf40eb342013-04-25 15:09:42 -05005181 end = reply_buf + ret;
5182
Alex Elder9e15b772012-10-30 19:40:33 -05005183 image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL);
5184 if (IS_ERR(image_name))
5185 image_name = NULL;
5186 else
5187 dout("%s: name is %s len is %zd\n", __func__, image_name, len);
5188out:
5189 kfree(reply_buf);
5190 kfree(image_id);
5191
5192 return image_name;
5193}
5194
Alex Elder2ad3d712013-04-30 00:44:33 -05005195static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
5196{
5197 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
5198 const char *snap_name;
5199 u32 which = 0;
5200
5201 /* Skip over names until we find the one we are looking for */
5202
5203 snap_name = rbd_dev->header.snap_names;
5204 while (which < snapc->num_snaps) {
5205 if (!strcmp(name, snap_name))
5206 return snapc->snaps[which];
5207 snap_name += strlen(snap_name) + 1;
5208 which++;
5209 }
5210 return CEPH_NOSNAP;
5211}
5212
5213static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
5214{
5215 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
5216 u32 which;
5217 bool found = false;
5218 u64 snap_id;
5219
5220 for (which = 0; !found && which < snapc->num_snaps; which++) {
5221 const char *snap_name;
5222
5223 snap_id = snapc->snaps[which];
5224 snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id);
Josh Durginefadc982013-08-29 19:16:42 -07005225 if (IS_ERR(snap_name)) {
5226 /* ignore no-longer existing snapshots */
5227 if (PTR_ERR(snap_name) == -ENOENT)
5228 continue;
5229 else
5230 break;
5231 }
Alex Elder2ad3d712013-04-30 00:44:33 -05005232 found = !strcmp(name, snap_name);
5233 kfree(snap_name);
5234 }
5235 return found ? snap_id : CEPH_NOSNAP;
5236}
5237
5238/*
5239 * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if
5240 * no snapshot by that name is found, or if an error occurs.
5241 */
5242static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
5243{
5244 if (rbd_dev->image_format == 1)
5245 return rbd_v1_snap_id_by_name(rbd_dev, name);
5246
5247 return rbd_v2_snap_id_by_name(rbd_dev, name);
5248}
5249
Alex Elder9e15b772012-10-30 19:40:33 -05005250/*
Ilya Dryomov04077592014-07-23 17:11:20 +04005251 * An image being mapped will have everything but the snap id.
Alex Elder9e15b772012-10-30 19:40:33 -05005252 */
Ilya Dryomov04077592014-07-23 17:11:20 +04005253static int rbd_spec_fill_snap_id(struct rbd_device *rbd_dev)
5254{
5255 struct rbd_spec *spec = rbd_dev->spec;
5256
5257 rbd_assert(spec->pool_id != CEPH_NOPOOL && spec->pool_name);
5258 rbd_assert(spec->image_id && spec->image_name);
5259 rbd_assert(spec->snap_name);
5260
5261 if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) {
5262 u64 snap_id;
5263
5264 snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name);
5265 if (snap_id == CEPH_NOSNAP)
5266 return -ENOENT;
5267
5268 spec->snap_id = snap_id;
5269 } else {
5270 spec->snap_id = CEPH_NOSNAP;
5271 }
5272
5273 return 0;
5274}
5275
5276/*
5277 * A parent image will have all ids but none of the names.
5278 *
5279 * All names in an rbd spec are dynamically allocated. It's OK if we
5280 * can't figure out the name for an image id.
5281 */
5282static int rbd_spec_fill_names(struct rbd_device *rbd_dev)
Alex Elder9e15b772012-10-30 19:40:33 -05005283{
Alex Elder2e9f7f12013-04-26 09:43:48 -05005284 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
5285 struct rbd_spec *spec = rbd_dev->spec;
5286 const char *pool_name;
5287 const char *image_name;
5288 const char *snap_name;
Alex Elder9e15b772012-10-30 19:40:33 -05005289 int ret;
5290
Ilya Dryomov04077592014-07-23 17:11:20 +04005291 rbd_assert(spec->pool_id != CEPH_NOPOOL);
5292 rbd_assert(spec->image_id);
5293 rbd_assert(spec->snap_id != CEPH_NOSNAP);
Alex Elder9e15b772012-10-30 19:40:33 -05005294
Alex Elder2e9f7f12013-04-26 09:43:48 -05005295 /* Get the pool name; we have to make our own copy of this */
Alex Elder9e15b772012-10-30 19:40:33 -05005296
Alex Elder2e9f7f12013-04-26 09:43:48 -05005297 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id);
5298 if (!pool_name) {
5299 rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id);
Alex Elder935dc892012-11-01 10:17:15 -05005300 return -EIO;
5301 }
Alex Elder2e9f7f12013-04-26 09:43:48 -05005302 pool_name = kstrdup(pool_name, GFP_KERNEL);
5303 if (!pool_name)
Alex Elder9e15b772012-10-30 19:40:33 -05005304 return -ENOMEM;
5305
5306 /* Fetch the image name; tolerate failure here */
5307
Alex Elder2e9f7f12013-04-26 09:43:48 -05005308 image_name = rbd_dev_image_name(rbd_dev);
5309 if (!image_name)
Alex Elder06ecc6c2012-11-01 10:17:15 -05005310 rbd_warn(rbd_dev, "unable to get image name");
Alex Elder9e15b772012-10-30 19:40:33 -05005311
Ilya Dryomov04077592014-07-23 17:11:20 +04005312 /* Fetch the snapshot name */
Alex Elder9e15b772012-10-30 19:40:33 -05005313
Alex Elder2e9f7f12013-04-26 09:43:48 -05005314 snap_name = rbd_snap_name(rbd_dev, spec->snap_id);
Josh Durginda6a6b62013-09-04 17:57:31 -07005315 if (IS_ERR(snap_name)) {
5316 ret = PTR_ERR(snap_name);
Alex Elder9e15b772012-10-30 19:40:33 -05005317 goto out_err;
Alex Elder2e9f7f12013-04-26 09:43:48 -05005318 }
5319
5320 spec->pool_name = pool_name;
5321 spec->image_name = image_name;
5322 spec->snap_name = snap_name;
Alex Elder9e15b772012-10-30 19:40:33 -05005323
5324 return 0;
Ilya Dryomov04077592014-07-23 17:11:20 +04005325
Alex Elder9e15b772012-10-30 19:40:33 -05005326out_err:
Alex Elder2e9f7f12013-04-26 09:43:48 -05005327 kfree(image_name);
5328 kfree(pool_name);
Alex Elder9e15b772012-10-30 19:40:33 -05005329 return ret;
5330}
5331
Alex Eldercc4a38bd2013-04-30 00:44:33 -05005332static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev)
Alex Elder35d489f2012-07-03 16:01:19 -05005333{
5334 size_t size;
5335 int ret;
5336 void *reply_buf;
5337 void *p;
5338 void *end;
5339 u64 seq;
5340 u32 snap_count;
5341 struct ceph_snap_context *snapc;
5342 u32 i;
5343
5344 /*
5345 * We'll need room for the seq value (maximum snapshot id),
5346 * snapshot count, and array of that many snapshot ids.
5347 * For now we have a fixed upper limit on the number we're
5348 * prepared to receive.
5349 */
5350 size = sizeof (__le64) + sizeof (__le32) +
5351 RBD_MAX_SNAP_COUNT * sizeof (__le64);
5352 reply_buf = kzalloc(size, GFP_KERNEL);
5353 if (!reply_buf)
5354 return -ENOMEM;
5355
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005356 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5357 &rbd_dev->header_oloc, "get_snapcontext",
5358 NULL, 0, reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06005359 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder35d489f2012-07-03 16:01:19 -05005360 if (ret < 0)
5361 goto out;
5362
Alex Elder35d489f2012-07-03 16:01:19 -05005363 p = reply_buf;
Alex Elder57385b52013-04-21 12:14:45 -05005364 end = reply_buf + ret;
5365 ret = -ERANGE;
Alex Elder35d489f2012-07-03 16:01:19 -05005366 ceph_decode_64_safe(&p, end, seq, out);
5367 ceph_decode_32_safe(&p, end, snap_count, out);
5368
5369 /*
5370 * Make sure the reported number of snapshot ids wouldn't go
5371 * beyond the end of our buffer. But before checking that,
5372 * make sure the computed size of the snapshot context we
5373 * allocate is representable in a size_t.
5374 */
5375 if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
5376 / sizeof (u64)) {
5377 ret = -EINVAL;
5378 goto out;
5379 }
5380 if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
5381 goto out;
Alex Elder468521c2013-04-26 09:43:47 -05005382 ret = 0;
Alex Elder35d489f2012-07-03 16:01:19 -05005383
Alex Elder812164f82013-04-30 00:44:32 -05005384 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
Alex Elder35d489f2012-07-03 16:01:19 -05005385 if (!snapc) {
5386 ret = -ENOMEM;
5387 goto out;
5388 }
Alex Elder35d489f2012-07-03 16:01:19 -05005389 snapc->seq = seq;
Alex Elder35d489f2012-07-03 16:01:19 -05005390 for (i = 0; i < snap_count; i++)
5391 snapc->snaps[i] = ceph_decode_64(&p);
5392
Alex Elder49ece552013-05-06 08:37:00 -05005393 ceph_put_snap_context(rbd_dev->header.snapc);
Alex Elder35d489f2012-07-03 16:01:19 -05005394 rbd_dev->header.snapc = snapc;
5395
5396 dout(" snap context seq = %llu, snap_count = %u\n",
Alex Elder57385b52013-04-21 12:14:45 -05005397 (unsigned long long)seq, (unsigned int)snap_count);
Alex Elder35d489f2012-07-03 16:01:19 -05005398out:
5399 kfree(reply_buf);
5400
Alex Elder57385b52013-04-21 12:14:45 -05005401 return ret;
Alex Elder35d489f2012-07-03 16:01:19 -05005402}
5403
Alex Elder54cac612013-04-30 00:44:33 -05005404static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
5405 u64 snap_id)
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005406{
5407 size_t size;
5408 void *reply_buf;
Alex Elder54cac612013-04-30 00:44:33 -05005409 __le64 snapid;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005410 int ret;
5411 void *p;
5412 void *end;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005413 char *snap_name;
5414
5415 size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN;
5416 reply_buf = kmalloc(size, GFP_KERNEL);
5417 if (!reply_buf)
5418 return ERR_PTR(-ENOMEM);
5419
Alex Elder54cac612013-04-30 00:44:33 -05005420 snapid = cpu_to_le64(snap_id);
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005421 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5422 &rbd_dev->header_oloc, "get_snapshot_name",
5423 &snapid, sizeof(snapid), reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06005424 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderf40eb342013-04-25 15:09:42 -05005425 if (ret < 0) {
5426 snap_name = ERR_PTR(ret);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005427 goto out;
Alex Elderf40eb342013-04-25 15:09:42 -05005428 }
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005429
5430 p = reply_buf;
Alex Elderf40eb342013-04-25 15:09:42 -05005431 end = reply_buf + ret;
Alex Eldere5c35532012-10-25 23:34:41 -05005432 snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elderf40eb342013-04-25 15:09:42 -05005433 if (IS_ERR(snap_name))
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005434 goto out;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005435
Alex Elderf40eb342013-04-25 15:09:42 -05005436 dout(" snap_id 0x%016llx snap_name = %s\n",
Alex Elder54cac612013-04-30 00:44:33 -05005437 (unsigned long long)snap_id, snap_name);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005438out:
5439 kfree(reply_buf);
5440
Alex Elderf40eb342013-04-25 15:09:42 -05005441 return snap_name;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005442}
5443
Alex Elder2df3fac2013-05-06 09:51:30 -05005444static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev)
Alex Elder117973f2012-08-31 17:29:55 -05005445{
Alex Elder2df3fac2013-05-06 09:51:30 -05005446 bool first_time = rbd_dev->header.object_prefix == NULL;
Alex Elder117973f2012-08-31 17:29:55 -05005447 int ret;
Alex Elder117973f2012-08-31 17:29:55 -05005448
Josh Durgin1617e402013-06-12 14:43:10 -07005449 ret = rbd_dev_v2_image_size(rbd_dev);
5450 if (ret)
Alex Eldercfbf6372013-05-31 17:40:45 -05005451 return ret;
Josh Durgin1617e402013-06-12 14:43:10 -07005452
Alex Elder2df3fac2013-05-06 09:51:30 -05005453 if (first_time) {
5454 ret = rbd_dev_v2_header_onetime(rbd_dev);
5455 if (ret)
Alex Eldercfbf6372013-05-31 17:40:45 -05005456 return ret;
Alex Elder2df3fac2013-05-06 09:51:30 -05005457 }
5458
Alex Eldercc4a38bd2013-04-30 00:44:33 -05005459 ret = rbd_dev_v2_snap_context(rbd_dev);
Ilya Dryomovd194cd12015-08-31 18:22:10 +03005460 if (ret && first_time) {
5461 kfree(rbd_dev->header.object_prefix);
5462 rbd_dev->header.object_prefix = NULL;
5463 }
Alex Elder117973f2012-08-31 17:29:55 -05005464
5465 return ret;
5466}
5467
Ilya Dryomova720ae02014-07-23 17:11:19 +04005468static int rbd_dev_header_info(struct rbd_device *rbd_dev)
5469{
5470 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
5471
5472 if (rbd_dev->image_format == 1)
5473 return rbd_dev_v1_header_info(rbd_dev);
5474
5475 return rbd_dev_v2_header_info(rbd_dev);
5476}
5477
Alex Elder1ddbe942012-01-29 13:57:44 -06005478/*
Alex Eldere28fff262012-02-02 08:13:30 -06005479 * Skips over white space at *buf, and updates *buf to point to the
5480 * first found non-space character (if any). Returns the length of
Alex Elder593a9e72012-02-07 12:03:37 -06005481 * the token (string of non-white space characters) found. Note
5482 * that *buf must be terminated with '\0'.
Alex Eldere28fff262012-02-02 08:13:30 -06005483 */
5484static inline size_t next_token(const char **buf)
5485{
5486 /*
5487 * These are the characters that produce nonzero for
5488 * isspace() in the "C" and "POSIX" locales.
5489 */
5490 const char *spaces = " \f\n\r\t\v";
5491
5492 *buf += strspn(*buf, spaces); /* Find start of token */
5493
5494 return strcspn(*buf, spaces); /* Return token length */
5495}
5496
5497/*
Alex Elderea3352f2012-07-09 21:04:23 -05005498 * Finds the next token in *buf, dynamically allocates a buffer big
5499 * enough to hold a copy of it, and copies the token into the new
5500 * buffer. The copy is guaranteed to be terminated with '\0'. Note
5501 * that a duplicate buffer is created even for a zero-length token.
5502 *
5503 * Returns a pointer to the newly-allocated duplicate, or a null
5504 * pointer if memory for the duplicate was not available. If
5505 * the lenp argument is a non-null pointer, the length of the token
5506 * (not including the '\0') is returned in *lenp.
5507 *
5508 * If successful, the *buf pointer will be updated to point beyond
5509 * the end of the found token.
5510 *
5511 * Note: uses GFP_KERNEL for allocation.
5512 */
5513static inline char *dup_token(const char **buf, size_t *lenp)
5514{
5515 char *dup;
5516 size_t len;
5517
5518 len = next_token(buf);
Alex Elder4caf35f2012-11-01 08:39:27 -05005519 dup = kmemdup(*buf, len + 1, GFP_KERNEL);
Alex Elderea3352f2012-07-09 21:04:23 -05005520 if (!dup)
5521 return NULL;
Alex Elderea3352f2012-07-09 21:04:23 -05005522 *(dup + len) = '\0';
5523 *buf += len;
5524
5525 if (lenp)
5526 *lenp = len;
5527
5528 return dup;
5529}
5530
5531/*
Alex Elder859c31d2012-10-25 23:34:42 -05005532 * Parse the options provided for an "rbd add" (i.e., rbd image
5533 * mapping) request. These arrive via a write to /sys/bus/rbd/add,
5534 * and the data written is passed here via a NUL-terminated buffer.
5535 * Returns 0 if successful or an error code otherwise.
Alex Elderd22f76e2012-07-12 10:46:35 -05005536 *
Alex Elder859c31d2012-10-25 23:34:42 -05005537 * The information extracted from these options is recorded in
5538 * the other parameters which return dynamically-allocated
5539 * structures:
5540 * ceph_opts
5541 * The address of a pointer that will refer to a ceph options
5542 * structure. Caller must release the returned pointer using
5543 * ceph_destroy_options() when it is no longer needed.
5544 * rbd_opts
5545 * Address of an rbd options pointer. Fully initialized by
5546 * this function; caller must release with kfree().
5547 * spec
5548 * Address of an rbd image specification pointer. Fully
5549 * initialized by this function based on parsed options.
5550 * Caller must release with rbd_spec_put().
5551 *
5552 * The options passed take this form:
5553 * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
5554 * where:
5555 * <mon_addrs>
5556 * A comma-separated list of one or more monitor addresses.
5557 * A monitor address is an ip address, optionally followed
5558 * by a port number (separated by a colon).
5559 * I.e.: ip1[:port1][,ip2[:port2]...]
5560 * <options>
5561 * A comma-separated list of ceph and/or rbd options.
5562 * <pool_name>
5563 * The name of the rados pool containing the rbd image.
5564 * <image_name>
5565 * The name of the image in that pool to map.
5566 * <snap_id>
5567 * An optional snapshot id. If provided, the mapping will
5568 * present data from the image at the time that snapshot was
5569 * created. The image head is used if no snapshot id is
5570 * provided. Snapshot mappings are always read-only.
Alex Eldera725f65e2012-02-02 08:13:30 -06005571 */
Alex Elder859c31d2012-10-25 23:34:42 -05005572static int rbd_add_parse_args(const char *buf,
Alex Elderdc79b112012-10-25 23:34:41 -05005573 struct ceph_options **ceph_opts,
Alex Elder859c31d2012-10-25 23:34:42 -05005574 struct rbd_options **opts,
5575 struct rbd_spec **rbd_spec)
Alex Eldera725f65e2012-02-02 08:13:30 -06005576{
Alex Elderd22f76e2012-07-12 10:46:35 -05005577 size_t len;
Alex Elder859c31d2012-10-25 23:34:42 -05005578 char *options;
Alex Elder0ddebc02012-10-25 23:34:41 -05005579 const char *mon_addrs;
Alex Elderecb4dc22013-04-26 09:43:47 -05005580 char *snap_name;
Alex Elder0ddebc02012-10-25 23:34:41 -05005581 size_t mon_addrs_size;
Alex Elder859c31d2012-10-25 23:34:42 -05005582 struct rbd_spec *spec = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05005583 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05005584 struct ceph_options *copts;
Alex Elderdc79b112012-10-25 23:34:41 -05005585 int ret;
Alex Eldere28fff262012-02-02 08:13:30 -06005586
5587 /* The first four tokens are required */
5588
Alex Elder7ef32142012-02-02 08:13:30 -06005589 len = next_token(&buf);
Alex Elder4fb5d6712012-11-01 10:17:15 -05005590 if (!len) {
5591 rbd_warn(NULL, "no monitor address(es) provided");
5592 return -EINVAL;
5593 }
Alex Elder0ddebc02012-10-25 23:34:41 -05005594 mon_addrs = buf;
Alex Elderf28e5652012-10-25 23:34:41 -05005595 mon_addrs_size = len + 1;
Alex Elder7ef32142012-02-02 08:13:30 -06005596 buf += len;
Alex Eldera725f65e2012-02-02 08:13:30 -06005597
Alex Elderdc79b112012-10-25 23:34:41 -05005598 ret = -EINVAL;
Alex Elderf28e5652012-10-25 23:34:41 -05005599 options = dup_token(&buf, NULL);
5600 if (!options)
Alex Elderdc79b112012-10-25 23:34:41 -05005601 return -ENOMEM;
Alex Elder4fb5d6712012-11-01 10:17:15 -05005602 if (!*options) {
5603 rbd_warn(NULL, "no options provided");
5604 goto out_err;
5605 }
Alex Eldera725f65e2012-02-02 08:13:30 -06005606
Alex Elder859c31d2012-10-25 23:34:42 -05005607 spec = rbd_spec_alloc();
5608 if (!spec)
Alex Elderf28e5652012-10-25 23:34:41 -05005609 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05005610
5611 spec->pool_name = dup_token(&buf, NULL);
5612 if (!spec->pool_name)
5613 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05005614 if (!*spec->pool_name) {
5615 rbd_warn(NULL, "no pool name provided");
5616 goto out_err;
5617 }
Alex Eldere28fff262012-02-02 08:13:30 -06005618
Alex Elder69e7a022012-11-01 08:39:26 -05005619 spec->image_name = dup_token(&buf, NULL);
Alex Elder859c31d2012-10-25 23:34:42 -05005620 if (!spec->image_name)
Alex Elderf28e5652012-10-25 23:34:41 -05005621 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05005622 if (!*spec->image_name) {
5623 rbd_warn(NULL, "no image name provided");
5624 goto out_err;
5625 }
Alex Eldere28fff262012-02-02 08:13:30 -06005626
Alex Elderf28e5652012-10-25 23:34:41 -05005627 /*
5628 * Snapshot name is optional; default is to use "-"
5629 * (indicating the head/no snapshot).
5630 */
Alex Elder3feeb8942012-08-31 17:29:52 -05005631 len = next_token(&buf);
Alex Elder820a5f32012-07-09 21:04:24 -05005632 if (!len) {
Alex Elder3feeb8942012-08-31 17:29:52 -05005633 buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
5634 len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
Alex Elderf28e5652012-10-25 23:34:41 -05005635 } else if (len > RBD_MAX_SNAP_NAME_LEN) {
Alex Elderdc79b112012-10-25 23:34:41 -05005636 ret = -ENAMETOOLONG;
Alex Elderf28e5652012-10-25 23:34:41 -05005637 goto out_err;
Alex Elder849b4262012-07-09 21:04:24 -05005638 }
Alex Elderecb4dc22013-04-26 09:43:47 -05005639 snap_name = kmemdup(buf, len + 1, GFP_KERNEL);
5640 if (!snap_name)
Alex Elderf28e5652012-10-25 23:34:41 -05005641 goto out_mem;
Alex Elderecb4dc22013-04-26 09:43:47 -05005642 *(snap_name + len) = '\0';
5643 spec->snap_name = snap_name;
Alex Eldere5c35532012-10-25 23:34:41 -05005644
Alex Elder0ddebc02012-10-25 23:34:41 -05005645 /* Initialize all rbd options to the defaults */
Alex Eldere28fff262012-02-02 08:13:30 -06005646
Alex Elder4e9afeb2012-10-25 23:34:41 -05005647 rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL);
5648 if (!rbd_opts)
5649 goto out_mem;
5650
5651 rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
Ilya Dryomovb5584182015-06-23 16:21:19 +03005652 rbd_opts->queue_depth = RBD_QUEUE_DEPTH_DEFAULT;
Ilya Dryomov80de1912016-09-20 14:23:17 +02005653 rbd_opts->lock_on_read = RBD_LOCK_ON_READ_DEFAULT;
Ilya Dryomove010dd02017-04-13 12:17:39 +02005654 rbd_opts->exclusive = RBD_EXCLUSIVE_DEFAULT;
Alex Elderd22f76e2012-07-12 10:46:35 -05005655
Alex Elder859c31d2012-10-25 23:34:42 -05005656 copts = ceph_parse_options(options, mon_addrs,
Alex Elder0ddebc02012-10-25 23:34:41 -05005657 mon_addrs + mon_addrs_size - 1,
Alex Elder4e9afeb2012-10-25 23:34:41 -05005658 parse_rbd_opts_token, rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05005659 if (IS_ERR(copts)) {
5660 ret = PTR_ERR(copts);
Alex Elderdc79b112012-10-25 23:34:41 -05005661 goto out_err;
5662 }
Alex Elder859c31d2012-10-25 23:34:42 -05005663 kfree(options);
5664
5665 *ceph_opts = copts;
Alex Elder4e9afeb2012-10-25 23:34:41 -05005666 *opts = rbd_opts;
Alex Elder859c31d2012-10-25 23:34:42 -05005667 *rbd_spec = spec;
Alex Elder0ddebc02012-10-25 23:34:41 -05005668
Alex Elderdc79b112012-10-25 23:34:41 -05005669 return 0;
Alex Elderf28e5652012-10-25 23:34:41 -05005670out_mem:
Alex Elderdc79b112012-10-25 23:34:41 -05005671 ret = -ENOMEM;
Alex Elderd22f76e2012-07-12 10:46:35 -05005672out_err:
Alex Elder859c31d2012-10-25 23:34:42 -05005673 kfree(rbd_opts);
5674 rbd_spec_put(spec);
Alex Elderf28e5652012-10-25 23:34:41 -05005675 kfree(options);
Alex Elderd22f76e2012-07-12 10:46:35 -05005676
Alex Elderdc79b112012-10-25 23:34:41 -05005677 return ret;
Alex Eldera725f65e2012-02-02 08:13:30 -06005678}
5679
Alex Elder589d30e2012-07-10 20:30:11 -05005680/*
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005681 * Return pool id (>= 0) or a negative error code.
5682 */
5683static int rbd_add_get_pool_id(struct rbd_client *rbdc, const char *pool_name)
5684{
Ilya Dryomova319bf52015-05-15 12:02:17 +03005685 struct ceph_options *opts = rbdc->client->options;
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005686 u64 newest_epoch;
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005687 int tries = 0;
5688 int ret;
5689
5690again:
5691 ret = ceph_pg_poolid_by_name(rbdc->client->osdc.osdmap, pool_name);
5692 if (ret == -ENOENT && tries++ < 1) {
Ilya Dryomovd0b19702016-04-28 16:07:27 +02005693 ret = ceph_monc_get_version(&rbdc->client->monc, "osdmap",
5694 &newest_epoch);
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005695 if (ret < 0)
5696 return ret;
5697
5698 if (rbdc->client->osdc.osdmap->epoch < newest_epoch) {
Ilya Dryomov7cca78c2016-04-28 16:07:28 +02005699 ceph_osdc_maybe_request_map(&rbdc->client->osdc);
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005700 (void) ceph_monc_wait_osdmap(&rbdc->client->monc,
Ilya Dryomova319bf52015-05-15 12:02:17 +03005701 newest_epoch,
5702 opts->mount_timeout);
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005703 goto again;
5704 } else {
5705 /* the osdmap we have is new enough */
5706 return -ENOENT;
5707 }
5708 }
5709
5710 return ret;
5711}
5712
Ilya Dryomove010dd02017-04-13 12:17:39 +02005713static void rbd_dev_image_unlock(struct rbd_device *rbd_dev)
5714{
5715 down_write(&rbd_dev->lock_rwsem);
5716 if (__rbd_is_lock_owner(rbd_dev))
5717 rbd_unlock(rbd_dev);
5718 up_write(&rbd_dev->lock_rwsem);
5719}
5720
5721static int rbd_add_acquire_lock(struct rbd_device *rbd_dev)
5722{
5723 if (!(rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK)) {
5724 rbd_warn(rbd_dev, "exclusive-lock feature is not enabled");
5725 return -EINVAL;
5726 }
5727
5728 /* FIXME: "rbd map --exclusive" should be in interruptible */
5729 down_read(&rbd_dev->lock_rwsem);
5730 rbd_wait_state_locked(rbd_dev);
5731 up_read(&rbd_dev->lock_rwsem);
5732 if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) {
5733 rbd_warn(rbd_dev, "failed to acquire exclusive lock");
5734 return -EROFS;
5735 }
5736
5737 return 0;
5738}
5739
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005740/*
Alex Elder589d30e2012-07-10 20:30:11 -05005741 * An rbd format 2 image has a unique identifier, distinct from the
5742 * name given to it by the user. Internally, that identifier is
5743 * what's used to specify the names of objects related to the image.
5744 *
5745 * A special "rbd id" object is used to map an rbd image name to its
5746 * id. If that object doesn't exist, then there is no v2 rbd image
5747 * with the supplied name.
5748 *
5749 * This function will record the given rbd_dev's image_id field if
5750 * it can be determined, and in that case will return 0. If any
5751 * errors occur a negative errno will be returned and the rbd_dev's
5752 * image_id field will be unchanged (and should be NULL).
5753 */
5754static int rbd_dev_image_id(struct rbd_device *rbd_dev)
5755{
5756 int ret;
5757 size_t size;
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005758 CEPH_DEFINE_OID_ONSTACK(oid);
Alex Elder589d30e2012-07-10 20:30:11 -05005759 void *response;
Alex Elderc0fba362013-04-25 23:15:08 -05005760 char *image_id;
Alex Elder2f82ee52012-10-30 19:40:33 -05005761
Alex Elder589d30e2012-07-10 20:30:11 -05005762 /*
Alex Elder2c0d0a12012-10-30 19:40:33 -05005763 * When probing a parent image, the image id is already
5764 * known (and the image name likely is not). There's no
Alex Elderc0fba362013-04-25 23:15:08 -05005765 * need to fetch the image id again in this case. We
5766 * do still need to set the image format though.
Alex Elder2c0d0a12012-10-30 19:40:33 -05005767 */
Alex Elderc0fba362013-04-25 23:15:08 -05005768 if (rbd_dev->spec->image_id) {
5769 rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1;
5770
Alex Elder2c0d0a12012-10-30 19:40:33 -05005771 return 0;
Alex Elderc0fba362013-04-25 23:15:08 -05005772 }
Alex Elder2c0d0a12012-10-30 19:40:33 -05005773
5774 /*
Alex Elder589d30e2012-07-10 20:30:11 -05005775 * First, see if the format 2 image id file exists, and if
5776 * so, get the image's persistent id from it.
5777 */
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005778 ret = ceph_oid_aprintf(&oid, GFP_KERNEL, "%s%s", RBD_ID_PREFIX,
5779 rbd_dev->spec->image_name);
5780 if (ret)
5781 return ret;
5782
5783 dout("rbd id object name is %s\n", oid.name);
Alex Elder589d30e2012-07-10 20:30:11 -05005784
5785 /* Response will be an encoded string, which includes a length */
5786
5787 size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
5788 response = kzalloc(size, GFP_NOIO);
5789 if (!response) {
5790 ret = -ENOMEM;
5791 goto out;
5792 }
5793
Alex Elderc0fba362013-04-25 23:15:08 -05005794 /* If it doesn't exist we'll assume it's a format 1 image */
5795
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005796 ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc,
5797 "get_id", NULL, 0,
5798 response, RBD_IMAGE_ID_LEN_MAX);
Alex Elder36be9a72013-01-19 00:30:28 -06005799 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderc0fba362013-04-25 23:15:08 -05005800 if (ret == -ENOENT) {
5801 image_id = kstrdup("", GFP_KERNEL);
5802 ret = image_id ? 0 : -ENOMEM;
5803 if (!ret)
5804 rbd_dev->image_format = 1;
Ilya Dryomov7dd440c2014-09-11 18:49:18 +04005805 } else if (ret >= 0) {
Alex Elderc0fba362013-04-25 23:15:08 -05005806 void *p = response;
Alex Elder589d30e2012-07-10 20:30:11 -05005807
Alex Elderc0fba362013-04-25 23:15:08 -05005808 image_id = ceph_extract_encoded_string(&p, p + ret,
Alex Elder979ed482012-11-01 08:39:26 -05005809 NULL, GFP_NOIO);
Duan Jiong461f7582014-04-11 16:38:12 +08005810 ret = PTR_ERR_OR_ZERO(image_id);
Alex Elderc0fba362013-04-25 23:15:08 -05005811 if (!ret)
5812 rbd_dev->image_format = 2;
Alex Elderc0fba362013-04-25 23:15:08 -05005813 }
5814
5815 if (!ret) {
5816 rbd_dev->spec->image_id = image_id;
5817 dout("image_id is %s\n", image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05005818 }
5819out:
5820 kfree(response);
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005821 ceph_oid_destroy(&oid);
Alex Elder589d30e2012-07-10 20:30:11 -05005822 return ret;
5823}
5824
Alex Elder3abef3b2013-05-13 20:35:37 -05005825/*
5826 * Undo whatever state changes are made by v1 or v2 header info
5827 * call.
5828 */
Alex Elder6fd48b32013-04-28 23:32:34 -05005829static void rbd_dev_unprobe(struct rbd_device *rbd_dev)
5830{
5831 struct rbd_image_header *header;
5832
Ilya Dryomove69b8d42015-01-19 12:06:14 +03005833 rbd_dev_parent_put(rbd_dev);
Alex Elder6fd48b32013-04-28 23:32:34 -05005834
5835 /* Free dynamic fields from the header, then zero it out */
5836
5837 header = &rbd_dev->header;
Alex Elder812164f82013-04-30 00:44:32 -05005838 ceph_put_snap_context(header->snapc);
Alex Elder6fd48b32013-04-28 23:32:34 -05005839 kfree(header->snap_sizes);
5840 kfree(header->snap_names);
5841 kfree(header->object_prefix);
5842 memset(header, 0, sizeof (*header));
5843}
5844
Alex Elder2df3fac2013-05-06 09:51:30 -05005845static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev)
Alex Eldera30b71b2012-07-10 20:30:11 -05005846{
5847 int ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05005848
Alex Elder1e130192012-07-03 16:01:19 -05005849 ret = rbd_dev_v2_object_prefix(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05005850 if (ret)
Alex Elder1e130192012-07-03 16:01:19 -05005851 goto out_err;
Alex Elderb1b54022012-07-03 16:01:19 -05005852
Alex Elder2df3fac2013-05-06 09:51:30 -05005853 /*
5854 * Get the and check features for the image. Currently the
5855 * features are assumed to never change.
5856 */
Alex Elderb1b54022012-07-03 16:01:19 -05005857 ret = rbd_dev_v2_features(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05005858 if (ret)
Alex Elderb1b54022012-07-03 16:01:19 -05005859 goto out_err;
Alex Elder35d489f2012-07-03 16:01:19 -05005860
Alex Eldercc070d52013-04-21 12:14:45 -05005861 /* If the image supports fancy striping, get its parameters */
5862
5863 if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) {
5864 ret = rbd_dev_v2_striping_info(rbd_dev);
5865 if (ret < 0)
5866 goto out_err;
5867 }
Alex Eldera30b71b2012-07-10 20:30:11 -05005868
Ilya Dryomov7e973322017-01-25 18:16:22 +01005869 if (rbd_dev->header.features & RBD_FEATURE_DATA_POOL) {
5870 ret = rbd_dev_v2_data_pool(rbd_dev);
5871 if (ret)
5872 goto out_err;
5873 }
5874
Ilya Dryomov263423f2017-01-25 18:16:22 +01005875 rbd_init_layout(rbd_dev);
Alex Elder35152972012-08-31 17:29:55 -05005876 return 0;
Ilya Dryomov263423f2017-01-25 18:16:22 +01005877
Alex Elder9d475de2012-07-03 16:01:19 -05005878out_err:
Alex Elder642a2532013-05-06 17:40:33 -05005879 rbd_dev->header.features = 0;
Alex Elder1e130192012-07-03 16:01:19 -05005880 kfree(rbd_dev->header.object_prefix);
5881 rbd_dev->header.object_prefix = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05005882 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05005883}
5884
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005885/*
5886 * @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() ->
5887 * rbd_dev_image_probe() recursion depth, which means it's also the
5888 * length of the already discovered part of the parent chain.
5889 */
5890static int rbd_dev_probe_parent(struct rbd_device *rbd_dev, int depth)
Alex Elder83a06262012-10-30 15:47:17 -05005891{
Alex Elder2f82ee52012-10-30 19:40:33 -05005892 struct rbd_device *parent = NULL;
Alex Elder124afba2013-04-26 15:44:36 -05005893 int ret;
5894
5895 if (!rbd_dev->parent_spec)
5896 return 0;
Alex Elder124afba2013-04-26 15:44:36 -05005897
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005898 if (++depth > RBD_MAX_PARENT_CHAIN_LEN) {
5899 pr_info("parent chain is too long (%d)\n", depth);
5900 ret = -EINVAL;
5901 goto out_err;
5902 }
5903
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02005904 parent = __rbd_dev_create(rbd_dev->rbd_client, rbd_dev->parent_spec);
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005905 if (!parent) {
5906 ret = -ENOMEM;
Alex Elder124afba2013-04-26 15:44:36 -05005907 goto out_err;
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005908 }
5909
5910 /*
5911 * Images related by parent/child relationships always share
5912 * rbd_client and spec/parent_spec, so bump their refcounts.
5913 */
5914 __rbd_get_client(rbd_dev->rbd_client);
5915 rbd_spec_get(rbd_dev->parent_spec);
Alex Elder124afba2013-04-26 15:44:36 -05005916
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005917 ret = rbd_dev_image_probe(parent, depth);
Alex Elder124afba2013-04-26 15:44:36 -05005918 if (ret < 0)
5919 goto out_err;
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005920
Alex Elder124afba2013-04-26 15:44:36 -05005921 rbd_dev->parent = parent;
Alex Eldera2acd002013-05-08 22:50:04 -05005922 atomic_set(&rbd_dev->parent_ref, 1);
Alex Elder124afba2013-04-26 15:44:36 -05005923 return 0;
Alex Elder124afba2013-04-26 15:44:36 -05005924
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005925out_err:
5926 rbd_dev_unparent(rbd_dev);
Markus Elfring1761b222015-11-23 20:16:45 +01005927 rbd_dev_destroy(parent);
Alex Elder124afba2013-04-26 15:44:36 -05005928 return ret;
5929}
5930
Ilya Dryomov5769ed02017-04-13 12:17:38 +02005931static void rbd_dev_device_release(struct rbd_device *rbd_dev)
5932{
5933 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
5934 rbd_dev_mapping_clear(rbd_dev);
5935 rbd_free_disk(rbd_dev);
5936 if (!single_major)
5937 unregister_blkdev(rbd_dev->major, rbd_dev->name);
5938}
5939
Ilya Dryomov811c6682016-04-15 16:22:16 +02005940/*
5941 * rbd_dev->header_rwsem must be locked for write and will be unlocked
5942 * upon return.
5943 */
Alex Elder200a6a82013-04-28 23:32:34 -05005944static int rbd_dev_device_setup(struct rbd_device *rbd_dev)
Alex Elder124afba2013-04-26 15:44:36 -05005945{
Alex Elder83a06262012-10-30 15:47:17 -05005946 int ret;
Alex Elder83a06262012-10-30 15:47:17 -05005947
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005948 /* Record our major and minor device numbers. */
Alex Elder83a06262012-10-30 15:47:17 -05005949
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005950 if (!single_major) {
5951 ret = register_blkdev(0, rbd_dev->name);
5952 if (ret < 0)
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02005953 goto err_out_unlock;
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005954
5955 rbd_dev->major = ret;
5956 rbd_dev->minor = 0;
5957 } else {
5958 rbd_dev->major = rbd_major;
5959 rbd_dev->minor = rbd_dev_id_to_minor(rbd_dev->dev_id);
5960 }
Alex Elder83a06262012-10-30 15:47:17 -05005961
5962 /* Set up the blkdev mapping. */
5963
5964 ret = rbd_init_disk(rbd_dev);
5965 if (ret)
5966 goto err_out_blkdev;
5967
Alex Elderf35a4de2013-05-06 09:51:29 -05005968 ret = rbd_dev_mapping_set(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05005969 if (ret)
5970 goto err_out_disk;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04005971
Alex Elderf35a4de2013-05-06 09:51:29 -05005972 set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
Ilya Dryomov9568c932017-10-12 12:35:19 +02005973 set_disk_ro(rbd_dev->disk, rbd_dev->opts->read_only);
Alex Elderf35a4de2013-05-06 09:51:29 -05005974
Ilya Dryomov5769ed02017-04-13 12:17:38 +02005975 ret = dev_set_name(&rbd_dev->dev, "%d", rbd_dev->dev_id);
Alex Elderf35a4de2013-05-06 09:51:29 -05005976 if (ret)
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04005977 goto err_out_mapping;
Alex Elder83a06262012-10-30 15:47:17 -05005978
Alex Elder129b79d2013-04-26 15:44:36 -05005979 set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
Ilya Dryomov811c6682016-04-15 16:22:16 +02005980 up_write(&rbd_dev->header_rwsem);
Ilya Dryomov5769ed02017-04-13 12:17:38 +02005981 return 0;
Alex Elder2f82ee52012-10-30 19:40:33 -05005982
Alex Elderf35a4de2013-05-06 09:51:29 -05005983err_out_mapping:
5984 rbd_dev_mapping_clear(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05005985err_out_disk:
5986 rbd_free_disk(rbd_dev);
5987err_out_blkdev:
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005988 if (!single_major)
5989 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Ilya Dryomov811c6682016-04-15 16:22:16 +02005990err_out_unlock:
5991 up_write(&rbd_dev->header_rwsem);
Alex Elder83a06262012-10-30 15:47:17 -05005992 return ret;
5993}
5994
Alex Elder332bb122013-04-27 09:59:30 -05005995static int rbd_dev_header_name(struct rbd_device *rbd_dev)
5996{
5997 struct rbd_spec *spec = rbd_dev->spec;
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005998 int ret;
Alex Elder332bb122013-04-27 09:59:30 -05005999
6000 /* Record the header object name for this rbd image. */
6001
6002 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
Alex Elder332bb122013-04-27 09:59:30 -05006003 if (rbd_dev->image_format == 1)
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006004 ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s",
6005 spec->image_name, RBD_SUFFIX);
Alex Elder332bb122013-04-27 09:59:30 -05006006 else
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006007 ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s",
6008 RBD_HEADER_PREFIX, spec->image_id);
Alex Elder332bb122013-04-27 09:59:30 -05006009
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006010 return ret;
Alex Elder332bb122013-04-27 09:59:30 -05006011}
6012
Alex Elder200a6a82013-04-28 23:32:34 -05006013static void rbd_dev_image_release(struct rbd_device *rbd_dev)
6014{
Alex Elder6fd48b32013-04-28 23:32:34 -05006015 rbd_dev_unprobe(rbd_dev);
Ilya Dryomovfd22aef2017-04-13 12:17:37 +02006016 if (rbd_dev->opts)
6017 rbd_unregister_watch(rbd_dev);
Alex Elder6fd48b32013-04-28 23:32:34 -05006018 rbd_dev->image_format = 0;
6019 kfree(rbd_dev->spec->image_id);
6020 rbd_dev->spec->image_id = NULL;
Alex Elder200a6a82013-04-28 23:32:34 -05006021}
6022
Alex Eldera30b71b2012-07-10 20:30:11 -05006023/*
6024 * Probe for the existence of the header object for the given rbd
Alex Elder1f3ef782013-05-06 17:40:33 -05006025 * device. If this image is the one being mapped (i.e., not a
6026 * parent), initiate a watch on its header object before using that
6027 * object to get detailed information about the rbd image.
Alex Eldera30b71b2012-07-10 20:30:11 -05006028 */
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006029static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth)
Alex Eldera30b71b2012-07-10 20:30:11 -05006030{
6031 int ret;
6032
6033 /*
Alex Elder3abef3b2013-05-13 20:35:37 -05006034 * Get the id from the image id object. Unless there's an
6035 * error, rbd_dev->spec->image_id will be filled in with
6036 * a dynamically-allocated string, and rbd_dev->image_format
6037 * will be set to either 1 or 2.
Alex Eldera30b71b2012-07-10 20:30:11 -05006038 */
6039 ret = rbd_dev_image_id(rbd_dev);
6040 if (ret)
Alex Elderc0fba362013-04-25 23:15:08 -05006041 return ret;
Alex Elderc0fba362013-04-25 23:15:08 -05006042
Alex Elder332bb122013-04-27 09:59:30 -05006043 ret = rbd_dev_header_name(rbd_dev);
6044 if (ret)
6045 goto err_out_format;
6046
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006047 if (!depth) {
Ilya Dryomov99d16942016-08-12 16:11:41 +02006048 ret = rbd_register_watch(rbd_dev);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006049 if (ret) {
6050 if (ret == -ENOENT)
6051 pr_info("image %s/%s does not exist\n",
6052 rbd_dev->spec->pool_name,
6053 rbd_dev->spec->image_name);
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006054 goto err_out_format;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006055 }
Alex Elder1f3ef782013-05-06 17:40:33 -05006056 }
Alex Elderb644de22013-04-27 09:59:31 -05006057
Ilya Dryomova720ae02014-07-23 17:11:19 +04006058 ret = rbd_dev_header_info(rbd_dev);
Alex Elder5655c4d2013-04-25 23:15:08 -05006059 if (ret)
Alex Elderb644de22013-04-27 09:59:31 -05006060 goto err_out_watch;
Alex Elder83a06262012-10-30 15:47:17 -05006061
Ilya Dryomov04077592014-07-23 17:11:20 +04006062 /*
6063 * If this image is the one being mapped, we have pool name and
6064 * id, image name and id, and snap name - need to fill snap id.
6065 * Otherwise this is a parent image, identified by pool, image
6066 * and snap ids - need to fill in names for those ids.
6067 */
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006068 if (!depth)
Ilya Dryomov04077592014-07-23 17:11:20 +04006069 ret = rbd_spec_fill_snap_id(rbd_dev);
6070 else
6071 ret = rbd_spec_fill_names(rbd_dev);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006072 if (ret) {
6073 if (ret == -ENOENT)
6074 pr_info("snap %s/%s@%s does not exist\n",
6075 rbd_dev->spec->pool_name,
6076 rbd_dev->spec->image_name,
6077 rbd_dev->spec->snap_name);
Alex Elder33dca392013-04-30 00:44:33 -05006078 goto err_out_probe;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006079 }
Alex Elder9bb81c92013-04-27 09:59:30 -05006080
Ilya Dryomove8f59b52014-07-24 10:42:13 +04006081 if (rbd_dev->header.features & RBD_FEATURE_LAYERING) {
6082 ret = rbd_dev_v2_parent_info(rbd_dev);
6083 if (ret)
6084 goto err_out_probe;
6085
6086 /*
6087 * Need to warn users if this image is the one being
6088 * mapped and has a parent.
6089 */
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006090 if (!depth && rbd_dev->parent_spec)
Ilya Dryomove8f59b52014-07-24 10:42:13 +04006091 rbd_warn(rbd_dev,
6092 "WARNING: kernel layering is EXPERIMENTAL!");
6093 }
6094
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006095 ret = rbd_dev_probe_parent(rbd_dev, depth);
Alex Elder30d60ba2013-05-06 09:51:30 -05006096 if (ret)
6097 goto err_out_probe;
Alex Elder83a06262012-10-30 15:47:17 -05006098
Alex Elder30d60ba2013-05-06 09:51:30 -05006099 dout("discovered format %u image, header name is %s\n",
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006100 rbd_dev->image_format, rbd_dev->header_oid.name);
Alex Elder30d60ba2013-05-06 09:51:30 -05006101 return 0;
Ilya Dryomove8f59b52014-07-24 10:42:13 +04006102
Alex Elder6fd48b32013-04-28 23:32:34 -05006103err_out_probe:
6104 rbd_dev_unprobe(rbd_dev);
Alex Elderb644de22013-04-27 09:59:31 -05006105err_out_watch:
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006106 if (!depth)
Ilya Dryomov99d16942016-08-12 16:11:41 +02006107 rbd_unregister_watch(rbd_dev);
Alex Elder332bb122013-04-27 09:59:30 -05006108err_out_format:
6109 rbd_dev->image_format = 0;
Alex Elder5655c4d2013-04-25 23:15:08 -05006110 kfree(rbd_dev->spec->image_id);
6111 rbd_dev->spec->image_id = NULL;
Alex Elder5655c4d2013-04-25 23:15:08 -05006112 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05006113}
6114
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006115static ssize_t do_rbd_add(struct bus_type *bus,
6116 const char *buf,
6117 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006118{
Alex Eldercb8627c2012-07-09 21:04:23 -05006119 struct rbd_device *rbd_dev = NULL;
Alex Elderdc79b112012-10-25 23:34:41 -05006120 struct ceph_options *ceph_opts = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05006121 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05006122 struct rbd_spec *spec = NULL;
Alex Elder9d3997f2012-10-25 23:34:42 -05006123 struct rbd_client *rbdc;
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02006124 int rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006125
6126 if (!try_module_get(THIS_MODULE))
6127 return -ENODEV;
6128
Alex Eldera725f65e2012-02-02 08:13:30 -06006129 /* parse add command */
Alex Elder859c31d2012-10-25 23:34:42 -05006130 rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec);
Alex Elderdc79b112012-10-25 23:34:41 -05006131 if (rc < 0)
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006132 goto out;
Alex Eldera725f65e2012-02-02 08:13:30 -06006133
Alex Elder9d3997f2012-10-25 23:34:42 -05006134 rbdc = rbd_get_client(ceph_opts);
6135 if (IS_ERR(rbdc)) {
6136 rc = PTR_ERR(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05006137 goto err_out_args;
Alex Elder9d3997f2012-10-25 23:34:42 -05006138 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006139
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006140 /* pick the pool */
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04006141 rc = rbd_add_get_pool_id(rbdc, spec->pool_name);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006142 if (rc < 0) {
6143 if (rc == -ENOENT)
6144 pr_info("pool %s does not exist\n", spec->pool_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006145 goto err_out_client;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006146 }
Alex Elderc0cd10db2013-04-26 09:43:47 -05006147 spec->pool_id = (u64)rc;
Alex Elder859c31d2012-10-25 23:34:42 -05006148
Ilya Dryomovd1475432015-06-22 13:24:48 +03006149 rbd_dev = rbd_dev_create(rbdc, spec, rbd_opts);
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02006150 if (!rbd_dev) {
6151 rc = -ENOMEM;
Alex Elderbd4ba652012-10-25 23:34:42 -05006152 goto err_out_client;
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02006153 }
Alex Elderc53d5892012-10-25 23:34:42 -05006154 rbdc = NULL; /* rbd_dev now owns this */
6155 spec = NULL; /* rbd_dev now owns this */
Ilya Dryomovd1475432015-06-22 13:24:48 +03006156 rbd_opts = NULL; /* rbd_dev now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006157
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02006158 rbd_dev->config_info = kstrdup(buf, GFP_KERNEL);
6159 if (!rbd_dev->config_info) {
6160 rc = -ENOMEM;
6161 goto err_out_rbd_dev;
6162 }
6163
Ilya Dryomov811c6682016-04-15 16:22:16 +02006164 down_write(&rbd_dev->header_rwsem);
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006165 rc = rbd_dev_image_probe(rbd_dev, 0);
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02006166 if (rc < 0) {
6167 up_write(&rbd_dev->header_rwsem);
Alex Elderc53d5892012-10-25 23:34:42 -05006168 goto err_out_rbd_dev;
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02006169 }
Alex Elder05fd6f62012-08-29 17:11:07 -05006170
Alex Elder7ce4eef2013-05-06 17:40:33 -05006171 /* If we are mapping a snapshot it must be marked read-only */
Alex Elder7ce4eef2013-05-06 17:40:33 -05006172 if (rbd_dev->spec->snap_id != CEPH_NOSNAP)
Ilya Dryomov9568c932017-10-12 12:35:19 +02006173 rbd_dev->opts->read_only = true;
Alex Elder7ce4eef2013-05-06 17:40:33 -05006174
Alex Elderb536f692013-04-28 23:32:34 -05006175 rc = rbd_dev_device_setup(rbd_dev);
Ilya Dryomovfd22aef2017-04-13 12:17:37 +02006176 if (rc)
Ilya Dryomov8b679ec2017-04-13 12:17:37 +02006177 goto err_out_image_probe;
Alex Elderb536f692013-04-28 23:32:34 -05006178
Ilya Dryomove010dd02017-04-13 12:17:39 +02006179 if (rbd_dev->opts->exclusive) {
6180 rc = rbd_add_acquire_lock(rbd_dev);
6181 if (rc)
6182 goto err_out_device_setup;
Alex Elderb536f692013-04-28 23:32:34 -05006183 }
6184
Ilya Dryomov5769ed02017-04-13 12:17:38 +02006185 /* Everything's ready. Announce the disk to the world. */
6186
6187 rc = device_add(&rbd_dev->dev);
6188 if (rc)
Ilya Dryomove010dd02017-04-13 12:17:39 +02006189 goto err_out_image_lock;
Ilya Dryomov5769ed02017-04-13 12:17:38 +02006190
6191 add_disk(rbd_dev->disk);
6192 /* see rbd_init_disk() */
6193 blk_put_queue(rbd_dev->disk->queue);
6194
6195 spin_lock(&rbd_dev_list_lock);
6196 list_add_tail(&rbd_dev->node, &rbd_dev_list);
6197 spin_unlock(&rbd_dev_list_lock);
6198
6199 pr_info("%s: capacity %llu features 0x%llx\n", rbd_dev->disk->disk_name,
6200 (unsigned long long)get_capacity(rbd_dev->disk) << SECTOR_SHIFT,
6201 rbd_dev->header.features);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006202 rc = count;
6203out:
6204 module_put(THIS_MODULE);
6205 return rc;
Alex Elder3abef3b2013-05-13 20:35:37 -05006206
Ilya Dryomove010dd02017-04-13 12:17:39 +02006207err_out_image_lock:
6208 rbd_dev_image_unlock(rbd_dev);
Ilya Dryomov5769ed02017-04-13 12:17:38 +02006209err_out_device_setup:
6210 rbd_dev_device_release(rbd_dev);
Ilya Dryomov8b679ec2017-04-13 12:17:37 +02006211err_out_image_probe:
6212 rbd_dev_image_release(rbd_dev);
Alex Elderc53d5892012-10-25 23:34:42 -05006213err_out_rbd_dev:
6214 rbd_dev_destroy(rbd_dev);
Alex Elderbd4ba652012-10-25 23:34:42 -05006215err_out_client:
Alex Elder9d3997f2012-10-25 23:34:42 -05006216 rbd_put_client(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05006217err_out_args:
Alex Elder859c31d2012-10-25 23:34:42 -05006218 rbd_spec_put(spec);
Ilya Dryomovd1475432015-06-22 13:24:48 +03006219 kfree(rbd_opts);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006220 goto out;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006221}
6222
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006223static ssize_t rbd_add(struct bus_type *bus,
6224 const char *buf,
6225 size_t count)
6226{
6227 if (single_major)
6228 return -EINVAL;
6229
6230 return do_rbd_add(bus, buf, count);
6231}
6232
6233static ssize_t rbd_add_single_major(struct bus_type *bus,
6234 const char *buf,
6235 size_t count)
6236{
6237 return do_rbd_add(bus, buf, count);
6238}
6239
Alex Elder05a46af2013-04-26 15:44:36 -05006240static void rbd_dev_remove_parent(struct rbd_device *rbd_dev)
6241{
Alex Elderad945fc2013-04-26 15:44:36 -05006242 while (rbd_dev->parent) {
Alex Elder05a46af2013-04-26 15:44:36 -05006243 struct rbd_device *first = rbd_dev;
6244 struct rbd_device *second = first->parent;
6245 struct rbd_device *third;
6246
6247 /*
6248 * Follow to the parent with no grandparent and
6249 * remove it.
6250 */
6251 while (second && (third = second->parent)) {
6252 first = second;
6253 second = third;
6254 }
Alex Elderad945fc2013-04-26 15:44:36 -05006255 rbd_assert(second);
Alex Elder8ad42cd2013-04-28 23:32:34 -05006256 rbd_dev_image_release(second);
Ilya Dryomov8b679ec2017-04-13 12:17:37 +02006257 rbd_dev_destroy(second);
Alex Elderad945fc2013-04-26 15:44:36 -05006258 first->parent = NULL;
6259 first->parent_overlap = 0;
6260
6261 rbd_assert(first->parent_spec);
Alex Elder05a46af2013-04-26 15:44:36 -05006262 rbd_spec_put(first->parent_spec);
6263 first->parent_spec = NULL;
Alex Elder05a46af2013-04-26 15:44:36 -05006264 }
6265}
6266
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006267static ssize_t do_rbd_remove(struct bus_type *bus,
6268 const char *buf,
6269 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006270{
6271 struct rbd_device *rbd_dev = NULL;
Alex Elder751cc0e2013-05-31 15:17:01 -05006272 struct list_head *tmp;
6273 int dev_id;
Mike Christie0276dca2016-08-18 18:38:45 +02006274 char opt_buf[6];
Alex Elder82a442d2013-05-31 17:40:44 -05006275 bool already = false;
Mike Christie0276dca2016-08-18 18:38:45 +02006276 bool force = false;
Alex Elder0d8189e2013-04-27 09:59:30 -05006277 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006278
Mike Christie0276dca2016-08-18 18:38:45 +02006279 dev_id = -1;
6280 opt_buf[0] = '\0';
6281 sscanf(buf, "%d %5s", &dev_id, opt_buf);
6282 if (dev_id < 0) {
6283 pr_err("dev_id out of range\n");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006284 return -EINVAL;
Mike Christie0276dca2016-08-18 18:38:45 +02006285 }
6286 if (opt_buf[0] != '\0') {
6287 if (!strcmp(opt_buf, "force")) {
6288 force = true;
6289 } else {
6290 pr_err("bad remove option at '%s'\n", opt_buf);
6291 return -EINVAL;
6292 }
6293 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006294
Alex Elder751cc0e2013-05-31 15:17:01 -05006295 ret = -ENOENT;
6296 spin_lock(&rbd_dev_list_lock);
6297 list_for_each(tmp, &rbd_dev_list) {
6298 rbd_dev = list_entry(tmp, struct rbd_device, node);
6299 if (rbd_dev->dev_id == dev_id) {
6300 ret = 0;
6301 break;
6302 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006303 }
Alex Elder751cc0e2013-05-31 15:17:01 -05006304 if (!ret) {
6305 spin_lock_irq(&rbd_dev->lock);
Mike Christie0276dca2016-08-18 18:38:45 +02006306 if (rbd_dev->open_count && !force)
Alex Elder751cc0e2013-05-31 15:17:01 -05006307 ret = -EBUSY;
6308 else
Alex Elder82a442d2013-05-31 17:40:44 -05006309 already = test_and_set_bit(RBD_DEV_FLAG_REMOVING,
6310 &rbd_dev->flags);
Alex Elder751cc0e2013-05-31 15:17:01 -05006311 spin_unlock_irq(&rbd_dev->lock);
6312 }
6313 spin_unlock(&rbd_dev_list_lock);
Alex Elder82a442d2013-05-31 17:40:44 -05006314 if (ret < 0 || already)
Alex Elder1ba0f1e2013-05-31 15:17:01 -05006315 return ret;
Alex Elder751cc0e2013-05-31 15:17:01 -05006316
Mike Christie0276dca2016-08-18 18:38:45 +02006317 if (force) {
6318 /*
6319 * Prevent new IO from being queued and wait for existing
6320 * IO to complete/fail.
6321 */
6322 blk_mq_freeze_queue(rbd_dev->disk->queue);
6323 blk_set_queue_dying(rbd_dev->disk->queue);
6324 }
6325
Ilya Dryomov5769ed02017-04-13 12:17:38 +02006326 del_gendisk(rbd_dev->disk);
6327 spin_lock(&rbd_dev_list_lock);
6328 list_del_init(&rbd_dev->node);
6329 spin_unlock(&rbd_dev_list_lock);
6330 device_del(&rbd_dev->dev);
Ilya Dryomovfca27062013-12-16 18:02:40 +02006331
Ilya Dryomove010dd02017-04-13 12:17:39 +02006332 rbd_dev_image_unlock(rbd_dev);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006333 rbd_dev_device_release(rbd_dev);
Alex Elder8ad42cd2013-04-28 23:32:34 -05006334 rbd_dev_image_release(rbd_dev);
Ilya Dryomov8b679ec2017-04-13 12:17:37 +02006335 rbd_dev_destroy(rbd_dev);
Alex Elder1ba0f1e2013-05-31 15:17:01 -05006336 return count;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006337}
6338
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006339static ssize_t rbd_remove(struct bus_type *bus,
6340 const char *buf,
6341 size_t count)
6342{
6343 if (single_major)
6344 return -EINVAL;
6345
6346 return do_rbd_remove(bus, buf, count);
6347}
6348
6349static ssize_t rbd_remove_single_major(struct bus_type *bus,
6350 const char *buf,
6351 size_t count)
6352{
6353 return do_rbd_remove(bus, buf, count);
6354}
6355
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006356/*
6357 * create control files in sysfs
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006358 * /sys/bus/rbd/...
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006359 */
6360static int rbd_sysfs_init(void)
6361{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006362 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006363
Alex Elderfed4c142012-02-07 12:03:36 -06006364 ret = device_register(&rbd_root_dev);
Alex Elder21079782012-01-24 10:08:36 -06006365 if (ret < 0)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006366 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006367
Alex Elderfed4c142012-02-07 12:03:36 -06006368 ret = bus_register(&rbd_bus_type);
6369 if (ret < 0)
6370 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006371
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006372 return ret;
6373}
6374
6375static void rbd_sysfs_cleanup(void)
6376{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006377 bus_unregister(&rbd_bus_type);
Alex Elderfed4c142012-02-07 12:03:36 -06006378 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006379}
6380
Alex Elder1c2a9df2013-05-01 12:43:03 -05006381static int rbd_slab_init(void)
6382{
6383 rbd_assert(!rbd_img_request_cache);
Geliang Tang03d94402016-03-13 15:17:32 +08006384 rbd_img_request_cache = KMEM_CACHE(rbd_img_request, 0);
Alex Elder868311b2013-05-01 12:43:03 -05006385 if (!rbd_img_request_cache)
6386 return -ENOMEM;
6387
6388 rbd_assert(!rbd_obj_request_cache);
Geliang Tang03d94402016-03-13 15:17:32 +08006389 rbd_obj_request_cache = KMEM_CACHE(rbd_obj_request, 0);
Alex Elder78c2a442013-05-01 12:43:04 -05006390 if (!rbd_obj_request_cache)
6391 goto out_err;
6392
NeilBrownf856dc32017-06-18 14:38:58 +10006393 rbd_assert(!rbd_bio_clone);
6394 rbd_bio_clone = bioset_create(BIO_POOL_SIZE, 0, 0);
6395 if (!rbd_bio_clone)
6396 goto out_err_clone;
6397
Ilya Dryomov6c696d82017-01-25 18:16:23 +01006398 return 0;
Alex Elder1c2a9df2013-05-01 12:43:03 -05006399
NeilBrownf856dc32017-06-18 14:38:58 +10006400out_err_clone:
6401 kmem_cache_destroy(rbd_obj_request_cache);
6402 rbd_obj_request_cache = NULL;
Ilya Dryomov6c696d82017-01-25 18:16:23 +01006403out_err:
Alex Elder868311b2013-05-01 12:43:03 -05006404 kmem_cache_destroy(rbd_img_request_cache);
6405 rbd_img_request_cache = NULL;
Alex Elder1c2a9df2013-05-01 12:43:03 -05006406 return -ENOMEM;
6407}
6408
6409static void rbd_slab_exit(void)
6410{
Alex Elder868311b2013-05-01 12:43:03 -05006411 rbd_assert(rbd_obj_request_cache);
6412 kmem_cache_destroy(rbd_obj_request_cache);
6413 rbd_obj_request_cache = NULL;
6414
Alex Elder1c2a9df2013-05-01 12:43:03 -05006415 rbd_assert(rbd_img_request_cache);
6416 kmem_cache_destroy(rbd_img_request_cache);
6417 rbd_img_request_cache = NULL;
NeilBrownf856dc32017-06-18 14:38:58 +10006418
6419 rbd_assert(rbd_bio_clone);
6420 bioset_free(rbd_bio_clone);
6421 rbd_bio_clone = NULL;
Alex Elder1c2a9df2013-05-01 12:43:03 -05006422}
6423
Alex Eldercc344fa2013-02-19 12:25:56 -06006424static int __init rbd_init(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006425{
6426 int rc;
6427
Alex Elder1e32d342013-01-30 11:13:33 -06006428 if (!libceph_compatible(NULL)) {
6429 rbd_warn(NULL, "libceph incompatibility (quitting)");
Alex Elder1e32d342013-01-30 11:13:33 -06006430 return -EINVAL;
6431 }
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006432
Alex Elder1c2a9df2013-05-01 12:43:03 -05006433 rc = rbd_slab_init();
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006434 if (rc)
6435 return rc;
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006436
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006437 /*
6438 * The number of active work items is limited by the number of
Ilya Dryomovf77303b2015-04-22 18:28:13 +03006439 * rbd devices * queue depth, so leave @max_active at default.
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006440 */
6441 rbd_wq = alloc_workqueue(RBD_DRV_NAME, WQ_MEM_RECLAIM, 0);
6442 if (!rbd_wq) {
6443 rc = -ENOMEM;
6444 goto err_out_slab;
6445 }
6446
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006447 if (single_major) {
6448 rbd_major = register_blkdev(0, RBD_DRV_NAME);
6449 if (rbd_major < 0) {
6450 rc = rbd_major;
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006451 goto err_out_wq;
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006452 }
6453 }
6454
Alex Elder1c2a9df2013-05-01 12:43:03 -05006455 rc = rbd_sysfs_init();
6456 if (rc)
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006457 goto err_out_blkdev;
Alex Elder1c2a9df2013-05-01 12:43:03 -05006458
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006459 if (single_major)
6460 pr_info("loaded (major %d)\n", rbd_major);
6461 else
6462 pr_info("loaded\n");
6463
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006464 return 0;
6465
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006466err_out_blkdev:
6467 if (single_major)
6468 unregister_blkdev(rbd_major, RBD_DRV_NAME);
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006469err_out_wq:
6470 destroy_workqueue(rbd_wq);
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006471err_out_slab:
6472 rbd_slab_exit();
Alex Elder1c2a9df2013-05-01 12:43:03 -05006473 return rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006474}
6475
Alex Eldercc344fa2013-02-19 12:25:56 -06006476static void __exit rbd_exit(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006477{
Ilya Dryomovffe312c2014-05-20 15:46:04 +04006478 ida_destroy(&rbd_dev_id_ida);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006479 rbd_sysfs_cleanup();
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006480 if (single_major)
6481 unregister_blkdev(rbd_major, RBD_DRV_NAME);
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006482 destroy_workqueue(rbd_wq);
Alex Elder1c2a9df2013-05-01 12:43:03 -05006483 rbd_slab_exit();
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006484}
6485
6486module_init(rbd_init);
6487module_exit(rbd_exit);
6488
Alex Elderd552c612013-05-31 20:13:09 -05006489MODULE_AUTHOR("Alex Elder <elder@inktank.com>");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006490MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
6491MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006492/* following authorship retained from original osdblk.c */
6493MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
6494
Ilya Dryomov90da2582013-12-13 15:28:56 +02006495MODULE_DESCRIPTION("RADOS Block Device (RBD) driver");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006496MODULE_LICENSE("GPL");