blob: 2b184563cd32e8ca6c45ffbad4dcad1626429194 [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>
Ilya Dryomov43df3d32018-02-02 15:23:22 +010035#include <linux/ceph/striper.h>
Yehuda Sadeh602adf42010-08-12 16:11:25 -070036#include <linux/ceph/decode.h>
David Howells82995cc2019-03-25 16:38:32 +000037#include <linux/fs_parser.h>
Alex Elder30d1cff2013-05-01 12:43:03 -050038#include <linux/bsearch.h>
Yehuda Sadeh602adf42010-08-12 16:11:25 -070039
40#include <linux/kernel.h>
41#include <linux/device.h>
42#include <linux/module.h>
Christoph Hellwig7ad18af2015-01-13 17:20:04 +010043#include <linux/blk-mq.h>
Yehuda Sadeh602adf42010-08-12 16:11:25 -070044#include <linux/fs.h>
45#include <linux/blkdev.h>
Alex Elder1c2a9df2013-05-01 12:43:03 -050046#include <linux/slab.h>
Ilya Dryomovf8a22fc2013-12-13 15:28:57 +020047#include <linux/idr.h>
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +040048#include <linux/workqueue.h>
Yehuda Sadeh602adf42010-08-12 16:11:25 -070049
50#include "rbd_types.h"
51
Alex Elderaafb2302012-09-06 16:00:54 -050052#define RBD_DEBUG /* Activate rbd_assert() calls */
53
Alex Elder593a9e72012-02-07 12:03:37 -060054/*
Alex Eldera2acd002013-05-08 22:50:04 -050055 * Increment the given counter and return its updated value.
56 * If the counter is already 0 it will not be incremented.
57 * If the counter is already at its maximum value returns
58 * -EINVAL without updating it.
59 */
60static int atomic_inc_return_safe(atomic_t *v)
61{
62 unsigned int counter;
63
Mark Rutlandbfc18e32018-06-21 13:13:04 +010064 counter = (unsigned int)atomic_fetch_add_unless(v, 1, 0);
Alex Eldera2acd002013-05-08 22:50:04 -050065 if (counter <= (unsigned int)INT_MAX)
66 return (int)counter;
67
68 atomic_dec(v);
69
70 return -EINVAL;
71}
72
73/* Decrement the counter. Return the resulting value, or -EINVAL */
74static int atomic_dec_return_safe(atomic_t *v)
75{
76 int counter;
77
78 counter = atomic_dec_return(v);
79 if (counter >= 0)
80 return counter;
81
82 atomic_inc(v);
83
84 return -EINVAL;
85}
86
Alex Elderf0f8cef2012-01-29 13:57:44 -060087#define RBD_DRV_NAME "rbd"
Yehuda Sadeh602adf42010-08-12 16:11:25 -070088
Ilya Dryomov7e513d42013-12-16 19:26:32 +020089#define RBD_MINORS_PER_MAJOR 256
90#define RBD_SINGLE_MAJOR_PART_SHIFT 4
Yehuda Sadeh602adf42010-08-12 16:11:25 -070091
Ilya Dryomov6d69bb532015-10-11 19:38:00 +020092#define RBD_MAX_PARENT_CHAIN_LEN 16
93
Alex Elderd4b125e2012-07-03 16:01:19 -050094#define RBD_SNAP_DEV_NAME_PREFIX "snap_"
95#define RBD_MAX_SNAP_NAME_LEN \
96 (NAME_MAX - (sizeof (RBD_SNAP_DEV_NAME_PREFIX) - 1))
97
Alex Elder35d489f2012-07-03 16:01:19 -050098#define RBD_MAX_SNAP_COUNT 510 /* allows max snapc to fit in 4KB */
Yehuda Sadeh602adf42010-08-12 16:11:25 -070099
100#define RBD_SNAP_HEAD_NAME "-"
101
Alex Elder9682fc62013-04-30 00:44:33 -0500102#define BAD_SNAP_INDEX U32_MAX /* invalid index into snap array */
103
Alex Elder9e15b772012-10-30 19:40:33 -0500104/* This allows a single page to hold an image name sent by OSD */
105#define RBD_IMAGE_NAME_LEN_MAX (PAGE_SIZE - sizeof (__le32) - 1)
Alex Elder1e130192012-07-03 16:01:19 -0500106#define RBD_IMAGE_ID_LEN_MAX 64
Alex Elder9e15b772012-10-30 19:40:33 -0500107
Alex Elder1e130192012-07-03 16:01:19 -0500108#define RBD_OBJ_PREFIX_LEN_MAX 64
Alex Elder589d30e2012-07-10 20:30:11 -0500109
Ilya Dryomoved95b212016-08-12 16:40:02 +0200110#define RBD_NOTIFY_TIMEOUT 5 /* seconds */
Ilya Dryomov99d16942016-08-12 16:11:41 +0200111#define RBD_RETRY_DELAY msecs_to_jiffies(1000)
112
Alex Elderd8891402012-10-09 13:50:17 -0700113/* Feature bits */
114
Ilya Dryomov8767b292017-03-02 19:56:57 +0100115#define RBD_FEATURE_LAYERING (1ULL<<0)
116#define RBD_FEATURE_STRIPINGV2 (1ULL<<1)
117#define RBD_FEATURE_EXCLUSIVE_LOCK (1ULL<<2)
Ilya Dryomov22e8bd52019-06-05 19:25:11 +0200118#define RBD_FEATURE_OBJECT_MAP (1ULL<<3)
119#define RBD_FEATURE_FAST_DIFF (1ULL<<4)
Ilya Dryomovb9f6d442019-02-25 18:55:38 +0100120#define RBD_FEATURE_DEEP_FLATTEN (1ULL<<5)
Ilya Dryomov8767b292017-03-02 19:56:57 +0100121#define RBD_FEATURE_DATA_POOL (1ULL<<7)
Ilya Dryomove5734272018-01-16 15:41:54 +0100122#define RBD_FEATURE_OPERATIONS (1ULL<<8)
Ilya Dryomov8767b292017-03-02 19:56:57 +0100123
Ilya Dryomoved95b212016-08-12 16:40:02 +0200124#define RBD_FEATURES_ALL (RBD_FEATURE_LAYERING | \
125 RBD_FEATURE_STRIPINGV2 | \
Ilya Dryomov7e973322017-01-25 18:16:22 +0100126 RBD_FEATURE_EXCLUSIVE_LOCK | \
Ilya Dryomov22e8bd52019-06-05 19:25:11 +0200127 RBD_FEATURE_OBJECT_MAP | \
128 RBD_FEATURE_FAST_DIFF | \
Ilya Dryomovb9f6d442019-02-25 18:55:38 +0100129 RBD_FEATURE_DEEP_FLATTEN | \
Ilya Dryomove5734272018-01-16 15:41:54 +0100130 RBD_FEATURE_DATA_POOL | \
131 RBD_FEATURE_OPERATIONS)
Alex Elderd8891402012-10-09 13:50:17 -0700132
133/* Features supported by this (client software) implementation. */
134
Alex Elder770eba62012-10-25 23:34:40 -0500135#define RBD_FEATURES_SUPPORTED (RBD_FEATURES_ALL)
Alex Elderd8891402012-10-09 13:50:17 -0700136
Alex Elder81a89792012-02-02 08:13:30 -0600137/*
138 * An RBD device name will be "rbd#", where the "rbd" comes from
139 * RBD_DRV_NAME above, and # is a unique integer identifier.
Alex Elder81a89792012-02-02 08:13:30 -0600140 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700141#define DEV_NAME_LEN 32
142
143/*
144 * block device image metadata (in-memory version)
145 */
146struct rbd_image_header {
Alex Elderf35a4de2013-05-06 09:51:29 -0500147 /* These six fields never change for a given rbd image */
Alex Elder849b4262012-07-09 21:04:24 -0500148 char *object_prefix;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700149 __u8 obj_order;
Alex Elderf35a4de2013-05-06 09:51:29 -0500150 u64 stripe_unit;
151 u64 stripe_count;
Ilya Dryomov7e973322017-01-25 18:16:22 +0100152 s64 data_pool_id;
Alex Elderf35a4de2013-05-06 09:51:29 -0500153 u64 features; /* Might be changeable someday? */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700154
Alex Elderf84344f2012-08-31 17:29:51 -0500155 /* The remaining fields need to be updated occasionally */
156 u64 image_size;
157 struct ceph_snap_context *snapc;
Alex Elderf35a4de2013-05-06 09:51:29 -0500158 char *snap_names; /* format 1 only */
159 u64 *snap_sizes; /* format 1 only */
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700160};
161
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500162/*
163 * An rbd image specification.
164 *
165 * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely
Alex Elderc66c6e02012-11-01 08:39:26 -0500166 * identify an image. Each rbd_dev structure includes a pointer to
167 * an rbd_spec structure that encapsulates this identity.
168 *
169 * Each of the id's in an rbd_spec has an associated name. For a
170 * user-mapped image, the names are supplied and the id's associated
171 * with them are looked up. For a layered image, a parent image is
172 * defined by the tuple, and the names are looked up.
173 *
174 * An rbd_dev structure contains a parent_spec pointer which is
175 * non-null if the image it represents is a child in a layered
176 * image. This pointer will refer to the rbd_spec structure used
177 * by the parent rbd_dev for its own identity (i.e., the structure
178 * is shared between the parent and child).
179 *
180 * Since these structures are populated once, during the discovery
181 * phase of image construction, they are effectively immutable so
182 * we make no effort to synchronize access to them.
183 *
184 * Note that code herein does not assume the image name is known (it
185 * could be a null pointer).
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500186 */
187struct rbd_spec {
188 u64 pool_id;
Alex Elderecb4dc222013-04-26 09:43:47 -0500189 const char *pool_name;
Ilya Dryomovb26c0472018-07-03 15:28:43 +0200190 const char *pool_ns; /* NULL if default, never "" */
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500191
Alex Elderecb4dc222013-04-26 09:43:47 -0500192 const char *image_id;
193 const char *image_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500194
195 u64 snap_id;
Alex Elderecb4dc222013-04-26 09:43:47 -0500196 const char *snap_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500197
198 struct kref kref;
199};
200
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700201/*
Alex Elderf0f8cef2012-01-29 13:57:44 -0600202 * an instance of the client. multiple devices may share an rbd client.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700203 */
204struct rbd_client {
205 struct ceph_client *client;
206 struct kref kref;
207 struct list_head node;
208};
209
Ilya Dryomov0192ce22019-05-16 15:06:56 +0200210struct pending_result {
211 int result; /* first nonzero result */
212 int num_pending;
213};
214
Alex Elderbf0d5f502012-11-22 00:00:08 -0600215struct rbd_img_request;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600216
Alex Elder9969ebc2013-01-18 12:31:10 -0600217enum obj_request_type {
Ilya Dryomova1fbb5e2018-01-16 12:15:02 +0100218 OBJ_REQUEST_NODATA = 1,
Ilya Dryomov5359a172018-01-20 10:30:10 +0100219 OBJ_REQUEST_BIO, /* pointer into provided bio (list) */
Ilya Dryomov7e07efb2018-01-20 10:30:11 +0100220 OBJ_REQUEST_BVECS, /* pointer into provided bio_vec array */
Ilya Dryomovafb97882018-02-06 19:26:35 +0100221 OBJ_REQUEST_OWN_BVECS, /* private bio_vec array, doesn't own pages */
Alex Elder9969ebc2013-01-18 12:31:10 -0600222};
Alex Elderbf0d5f502012-11-22 00:00:08 -0600223
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800224enum obj_operation_type {
Ilya Dryomova1fbb5e2018-01-16 12:15:02 +0100225 OBJ_OP_READ = 1,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800226 OBJ_OP_WRITE,
Guangliang Zhao90e98c52014-04-01 22:22:16 +0800227 OBJ_OP_DISCARD,
Ilya Dryomov6484cbe2019-01-29 12:46:25 +0100228 OBJ_OP_ZEROOUT,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800229};
230
Ilya Dryomov0ad5d952019-05-14 20:45:38 +0200231#define RBD_OBJ_FLAG_DELETION (1U << 0)
232#define RBD_OBJ_FLAG_COPYUP_ENABLED (1U << 1)
Ilya Dryomov793333a302019-06-13 17:44:08 +0200233#define RBD_OBJ_FLAG_COPYUP_ZEROS (1U << 2)
Ilya Dryomov22e8bd52019-06-05 19:25:11 +0200234#define RBD_OBJ_FLAG_MAY_EXIST (1U << 3)
235#define RBD_OBJ_FLAG_NOOP_FOR_NONEXISTENT (1U << 4)
Ilya Dryomov0ad5d952019-05-14 20:45:38 +0200236
Ilya Dryomova9b67e62019-05-08 13:35:57 +0200237enum rbd_obj_read_state {
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +0200238 RBD_OBJ_READ_START = 1,
239 RBD_OBJ_READ_OBJECT,
Ilya Dryomova9b67e62019-05-08 13:35:57 +0200240 RBD_OBJ_READ_PARENT,
241};
242
Ilya Dryomov3da691b2018-01-29 14:04:08 +0100243/*
244 * Writes go through the following state machine to deal with
245 * layering:
246 *
Ilya Dryomov89a59c12019-02-28 14:20:28 +0100247 * . . . . . RBD_OBJ_WRITE_GUARD. . . . . . . . . . . . . .
248 * . | .
249 * . v .
250 * . RBD_OBJ_WRITE_READ_FROM_PARENT. . . .
251 * . | . .
252 * . v v (deep-copyup .
253 * (image . RBD_OBJ_WRITE_COPYUP_EMPTY_SNAPC . not needed) .
254 * flattened) v | . .
255 * . v . .
256 * . . . .RBD_OBJ_WRITE_COPYUP_OPS. . . . . (copyup .
257 * | not needed) v
258 * v .
259 * done . . . . . . . . . . . . . . . . . .
260 * ^
261 * |
262 * RBD_OBJ_WRITE_FLAT
Ilya Dryomov3da691b2018-01-29 14:04:08 +0100263 *
264 * Writes start in RBD_OBJ_WRITE_GUARD or _FLAT, depending on whether
Ilya Dryomov89a59c12019-02-28 14:20:28 +0100265 * assert_exists guard is needed or not (in some cases it's not needed
266 * even if there is a parent).
Ilya Dryomov3da691b2018-01-29 14:04:08 +0100267 */
268enum rbd_obj_write_state {
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +0200269 RBD_OBJ_WRITE_START = 1,
Ilya Dryomov22e8bd52019-06-05 19:25:11 +0200270 RBD_OBJ_WRITE_PRE_OBJECT_MAP,
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +0200271 RBD_OBJ_WRITE_OBJECT,
Ilya Dryomov793333a302019-06-13 17:44:08 +0200272 __RBD_OBJ_WRITE_COPYUP,
273 RBD_OBJ_WRITE_COPYUP,
Ilya Dryomov22e8bd52019-06-05 19:25:11 +0200274 RBD_OBJ_WRITE_POST_OBJECT_MAP,
Ilya Dryomov793333a302019-06-13 17:44:08 +0200275};
276
277enum rbd_obj_copyup_state {
278 RBD_OBJ_COPYUP_START = 1,
279 RBD_OBJ_COPYUP_READ_PARENT,
Ilya Dryomov22e8bd52019-06-05 19:25:11 +0200280 __RBD_OBJ_COPYUP_OBJECT_MAPS,
281 RBD_OBJ_COPYUP_OBJECT_MAPS,
Ilya Dryomov793333a302019-06-13 17:44:08 +0200282 __RBD_OBJ_COPYUP_WRITE_OBJECT,
283 RBD_OBJ_COPYUP_WRITE_OBJECT,
Alex Elder926f9b32013-02-11 12:33:24 -0600284};
285
Alex Elderbf0d5f502012-11-22 00:00:08 -0600286struct rbd_obj_request {
Ilya Dryomov43df3d32018-02-02 15:23:22 +0100287 struct ceph_object_extent ex;
Ilya Dryomov0ad5d952019-05-14 20:45:38 +0200288 unsigned int flags; /* RBD_OBJ_FLAG_* */
Alex Elderc5b5ef62013-02-11 12:33:24 -0600289 union {
Ilya Dryomova9b67e62019-05-08 13:35:57 +0200290 enum rbd_obj_read_state read_state; /* for reads */
Ilya Dryomov3da691b2018-01-29 14:04:08 +0100291 enum rbd_obj_write_state write_state; /* for writes */
292 };
Alex Elderbf0d5f502012-11-22 00:00:08 -0600293
Ilya Dryomov51c35092018-01-29 14:04:08 +0100294 struct rbd_img_request *img_request;
Ilya Dryomov86bd7992018-02-06 19:26:33 +0100295 struct ceph_file_extent *img_extents;
296 u32 num_img_extents;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600297
Alex Elder788e2df2013-01-17 12:25:27 -0600298 union {
Ilya Dryomov5359a172018-01-20 10:30:10 +0100299 struct ceph_bio_iter bio_pos;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600300 struct {
Ilya Dryomov7e07efb2018-01-20 10:30:11 +0100301 struct ceph_bvec_iter bvec_pos;
302 u32 bvec_count;
Ilya Dryomovafb97882018-02-06 19:26:35 +0100303 u32 bvec_idx;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600304 };
305 };
Ilya Dryomov793333a302019-06-13 17:44:08 +0200306
307 enum rbd_obj_copyup_state copyup_state;
Ilya Dryomov7e07efb2018-01-20 10:30:11 +0100308 struct bio_vec *copyup_bvecs;
309 u32 copyup_bvec_count;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600310
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +0200311 struct list_head osd_reqs; /* w/ r_private_item */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600312
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +0200313 struct mutex state_mutex;
Ilya Dryomov793333a302019-06-13 17:44:08 +0200314 struct pending_result pending;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600315 struct kref kref;
316};
317
Alex Elder0c425242013-02-08 09:55:49 -0600318enum img_req_flags {
Alex Elder9849e982013-01-24 16:13:36 -0600319 IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */
Alex Elderd0b2e942013-01-24 16:13:36 -0600320 IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */
Alex Elder0c425242013-02-08 09:55:49 -0600321};
322
Ilya Dryomov0192ce22019-05-16 15:06:56 +0200323enum rbd_img_state {
324 RBD_IMG_START = 1,
Ilya Dryomov637cd062019-06-06 17:14:49 +0200325 RBD_IMG_EXCLUSIVE_LOCK,
Ilya Dryomov0192ce22019-05-16 15:06:56 +0200326 __RBD_IMG_OBJECT_REQUESTS,
327 RBD_IMG_OBJECT_REQUESTS,
328};
329
Alex Elderbf0d5f502012-11-22 00:00:08 -0600330struct rbd_img_request {
Alex Elderbf0d5f502012-11-22 00:00:08 -0600331 struct rbd_device *rbd_dev;
Ilya Dryomov9bb02482018-01-30 17:52:10 +0100332 enum obj_operation_type op_type;
Ilya Dryomovecc633c2018-02-01 11:50:47 +0100333 enum obj_request_type data_type;
Alex Elder0c425242013-02-08 09:55:49 -0600334 unsigned long flags;
Ilya Dryomov0192ce22019-05-16 15:06:56 +0200335 enum rbd_img_state state;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600336 union {
Alex Elder9849e982013-01-24 16:13:36 -0600337 u64 snap_id; /* for reads */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600338 struct ceph_snap_context *snapc; /* for writes */
Alex Elder9849e982013-01-24 16:13:36 -0600339 };
340 union {
341 struct request *rq; /* block request */
342 struct rbd_obj_request *obj_request; /* obj req initiator */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600343 };
Alex Elderbf0d5f502012-11-22 00:00:08 -0600344
Ilya Dryomove1fddc82019-05-30 16:07:48 +0200345 struct list_head lock_item;
Ilya Dryomov43df3d32018-02-02 15:23:22 +0100346 struct list_head object_extents; /* obj_req.ex structs */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600347
Ilya Dryomov0192ce22019-05-16 15:06:56 +0200348 struct mutex state_mutex;
349 struct pending_result pending;
350 struct work_struct work;
351 int work_result;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600352 struct kref kref;
353};
354
355#define for_each_obj_request(ireq, oreq) \
Ilya Dryomov43df3d32018-02-02 15:23:22 +0100356 list_for_each_entry(oreq, &(ireq)->object_extents, ex.oe_item)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600357#define for_each_obj_request_safe(ireq, oreq, n) \
Ilya Dryomov43df3d32018-02-02 15:23:22 +0100358 list_for_each_entry_safe(oreq, n, &(ireq)->object_extents, ex.oe_item)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600359
Ilya Dryomov99d16942016-08-12 16:11:41 +0200360enum rbd_watch_state {
361 RBD_WATCH_STATE_UNREGISTERED,
362 RBD_WATCH_STATE_REGISTERED,
363 RBD_WATCH_STATE_ERROR,
364};
365
Ilya Dryomoved95b212016-08-12 16:40:02 +0200366enum rbd_lock_state {
367 RBD_LOCK_STATE_UNLOCKED,
368 RBD_LOCK_STATE_LOCKED,
369 RBD_LOCK_STATE_RELEASING,
370};
371
372/* WatchNotify::ClientId */
373struct rbd_client_id {
374 u64 gid;
375 u64 handle;
376};
377
Alex Elderf84344f2012-08-31 17:29:51 -0500378struct rbd_mapping {
Alex Elder99c1f082012-08-30 14:42:15 -0500379 u64 size;
Alex Elderf84344f2012-08-31 17:29:51 -0500380};
381
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700382/*
383 * a single device
384 */
385struct rbd_device {
Alex Elderde71a292012-07-03 16:01:19 -0500386 int dev_id; /* blkdev unique id */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700387
388 int major; /* blkdev assigned major */
Ilya Dryomovdd82fff2013-12-13 15:28:57 +0200389 int minor;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700390 struct gendisk *disk; /* blkdev's gendisk and rq */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700391
Alex Eldera30b71b2012-07-10 20:30:11 -0500392 u32 image_format; /* Either 1 or 2 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700393 struct rbd_client *rbd_client;
394
395 char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */
396
Alex Elderb82d1672013-01-14 12:43:31 -0600397 spinlock_t lock; /* queue, flags, open_count */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700398
399 struct rbd_image_header header;
Alex Elderb82d1672013-01-14 12:43:31 -0600400 unsigned long flags; /* possibly lock protected */
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500401 struct rbd_spec *spec;
Ilya Dryomovd1475432015-06-22 13:24:48 +0300402 struct rbd_options *opts;
Mike Christie0d6d1e9c2016-08-18 18:38:45 +0200403 char *config_info; /* add{,_single_major} string */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700404
Ilya Dryomovc41d13a2016-04-29 20:01:25 +0200405 struct ceph_object_id header_oid;
Ilya Dryomov922dab62016-05-26 01:15:02 +0200406 struct ceph_object_locator header_oloc;
Alex Elder971f8392012-10-25 23:34:41 -0500407
Ilya Dryomov1643dfa2016-08-12 15:45:52 +0200408 struct ceph_file_layout layout; /* used for all rbd requests */
Alex Elder0903e872012-11-14 12:25:19 -0600409
Ilya Dryomov99d16942016-08-12 16:11:41 +0200410 struct mutex watch_mutex;
411 enum rbd_watch_state watch_state;
Ilya Dryomov922dab62016-05-26 01:15:02 +0200412 struct ceph_osd_linger_request *watch_handle;
Ilya Dryomov99d16942016-08-12 16:11:41 +0200413 u64 watch_cookie;
414 struct delayed_work watch_dwork;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700415
Ilya Dryomoved95b212016-08-12 16:40:02 +0200416 struct rw_semaphore lock_rwsem;
417 enum rbd_lock_state lock_state;
Ilya Dryomovcbbfb0f2017-04-13 12:17:38 +0200418 char lock_cookie[32];
Ilya Dryomoved95b212016-08-12 16:40:02 +0200419 struct rbd_client_id owner_cid;
420 struct work_struct acquired_lock_work;
421 struct work_struct released_lock_work;
422 struct delayed_work lock_dwork;
423 struct work_struct unlock_work;
Ilya Dryomove1fddc82019-05-30 16:07:48 +0200424 spinlock_t lock_lists_lock;
Ilya Dryomov637cd062019-06-06 17:14:49 +0200425 struct list_head acquiring_list;
Ilya Dryomove1fddc82019-05-30 16:07:48 +0200426 struct list_head running_list;
Ilya Dryomov637cd062019-06-06 17:14:49 +0200427 struct completion acquire_wait;
428 int acquire_err;
Ilya Dryomove1fddc82019-05-30 16:07:48 +0200429 struct completion releasing_wait;
Ilya Dryomoved95b212016-08-12 16:40:02 +0200430
Ilya Dryomov22e8bd52019-06-05 19:25:11 +0200431 spinlock_t object_map_lock;
432 u8 *object_map;
433 u64 object_map_size; /* in objects */
434 u64 object_map_flags;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700435
Ilya Dryomov1643dfa2016-08-12 15:45:52 +0200436 struct workqueue_struct *task_wq;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700437
Alex Elder86b00e02012-10-25 23:34:42 -0500438 struct rbd_spec *parent_spec;
439 u64 parent_overlap;
Alex Eldera2acd002013-05-08 22:50:04 -0500440 atomic_t parent_ref;
Alex Elder2f82ee52012-10-30 19:40:33 -0500441 struct rbd_device *parent;
Alex Elder86b00e02012-10-25 23:34:42 -0500442
Christoph Hellwig7ad18af2015-01-13 17:20:04 +0100443 /* Block layer tags. */
444 struct blk_mq_tag_set tag_set;
445
Josh Durginc6666012011-11-21 17:11:12 -0800446 /* protects updating the header */
447 struct rw_semaphore header_rwsem;
Alex Elderf84344f2012-08-31 17:29:51 -0500448
449 struct rbd_mapping mapping;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700450
451 struct list_head node;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800452
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800453 /* sysfs related */
454 struct device dev;
Alex Elderb82d1672013-01-14 12:43:31 -0600455 unsigned long open_count; /* protected by lock */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800456};
457
Alex Elderb82d1672013-01-14 12:43:31 -0600458/*
Ilya Dryomov87c0fde2016-09-29 13:41:05 +0200459 * Flag bits for rbd_dev->flags:
460 * - REMOVING (which is coupled with rbd_dev->open_count) is protected
461 * by rbd_dev->lock
Alex Elderb82d1672013-01-14 12:43:31 -0600462 */
Alex Elder6d292902013-01-14 12:43:31 -0600463enum rbd_dev_flags {
Ilya Dryomov686238b2019-11-18 12:51:02 +0100464 RBD_DEV_FLAG_EXISTS, /* rbd_dev_device_setup() ran */
Alex Elderb82d1672013-01-14 12:43:31 -0600465 RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */
Ilya Dryomov39258aa2019-11-07 17:16:23 +0100466 RBD_DEV_FLAG_READONLY, /* -o ro or snapshot */
Alex Elder6d292902013-01-14 12:43:31 -0600467};
468
Alex Eldercfbf6372013-05-31 17:40:45 -0500469static DEFINE_MUTEX(client_mutex); /* Serialize client creation */
Alex Eldere124a82f2012-01-29 13:57:44 -0600470
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700471static LIST_HEAD(rbd_dev_list); /* devices */
Alex Eldere124a82f2012-01-29 13:57:44 -0600472static DEFINE_SPINLOCK(rbd_dev_list_lock);
473
Alex Elder432b8582012-01-29 13:57:44 -0600474static LIST_HEAD(rbd_client_list); /* clients */
475static DEFINE_SPINLOCK(rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700476
Alex Elder78c2a442013-05-01 12:43:04 -0500477/* Slab caches for frequently-allocated structures */
478
Alex Elder1c2a9df2013-05-01 12:43:03 -0500479static struct kmem_cache *rbd_img_request_cache;
Alex Elder868311b2013-05-01 12:43:03 -0500480static struct kmem_cache *rbd_obj_request_cache;
Alex Elder1c2a9df2013-05-01 12:43:03 -0500481
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200482static int rbd_major;
Ilya Dryomovf8a22fc2013-12-13 15:28:57 +0200483static DEFINE_IDA(rbd_dev_id_ida);
484
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +0400485static struct workqueue_struct *rbd_wq;
486
Ilya Dryomov89a59c12019-02-28 14:20:28 +0100487static struct ceph_snap_context rbd_empty_snapc = {
488 .nref = REFCOUNT_INIT(1),
489};
490
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200491/*
Ilya Dryomov3cfa3b12017-11-13 10:35:40 +0100492 * single-major requires >= 0.75 version of userspace rbd utility.
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200493 */
Ilya Dryomov3cfa3b12017-11-13 10:35:40 +0100494static bool single_major = true;
Joe Perches5657a812018-05-24 13:38:59 -0600495module_param(single_major, bool, 0444);
Ilya Dryomov3cfa3b12017-11-13 10:35:40 +0100496MODULE_PARM_DESC(single_major, "Use a single major number for all rbd devices (default: true)");
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200497
Greg Kroah-Hartman7e9586b2018-12-21 08:54:38 +0100498static ssize_t add_store(struct bus_type *bus, const char *buf, size_t count);
499static ssize_t remove_store(struct bus_type *bus, const char *buf,
500 size_t count);
501static ssize_t add_single_major_store(struct bus_type *bus, const char *buf,
502 size_t count);
503static ssize_t remove_single_major_store(struct bus_type *bus, const char *buf,
504 size_t count);
Ilya Dryomov6d69bb532015-10-11 19:38:00 +0200505static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth);
Alex Elderf0f8cef2012-01-29 13:57:44 -0600506
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200507static int rbd_dev_id_to_minor(int dev_id)
508{
Ilya Dryomov7e513d42013-12-16 19:26:32 +0200509 return dev_id << RBD_SINGLE_MAJOR_PART_SHIFT;
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200510}
511
512static int minor_to_rbd_dev_id(int minor)
513{
Ilya Dryomov7e513d42013-12-16 19:26:32 +0200514 return minor >> RBD_SINGLE_MAJOR_PART_SHIFT;
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200515}
516
Ilya Dryomov39258aa2019-11-07 17:16:23 +0100517static bool rbd_is_ro(struct rbd_device *rbd_dev)
518{
519 return test_bit(RBD_DEV_FLAG_READONLY, &rbd_dev->flags);
520}
521
Ilya Dryomovf3c0e452019-11-07 16:22:10 +0100522static bool rbd_is_snap(struct rbd_device *rbd_dev)
523{
524 return rbd_dev->spec->snap_id != CEPH_NOSNAP;
525}
526
Ilya Dryomoved95b212016-08-12 16:40:02 +0200527static bool __rbd_is_lock_owner(struct rbd_device *rbd_dev)
528{
Ilya Dryomov637cd062019-06-06 17:14:49 +0200529 lockdep_assert_held(&rbd_dev->lock_rwsem);
530
Ilya Dryomoved95b212016-08-12 16:40:02 +0200531 return rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED ||
532 rbd_dev->lock_state == RBD_LOCK_STATE_RELEASING;
533}
534
535static bool rbd_is_lock_owner(struct rbd_device *rbd_dev)
536{
537 bool is_lock_owner;
538
539 down_read(&rbd_dev->lock_rwsem);
540 is_lock_owner = __rbd_is_lock_owner(rbd_dev);
541 up_read(&rbd_dev->lock_rwsem);
542 return is_lock_owner;
543}
544
Greg Kroah-Hartman7e9586b2018-12-21 08:54:38 +0100545static ssize_t supported_features_show(struct bus_type *bus, char *buf)
Ilya Dryomov8767b292017-03-02 19:56:57 +0100546{
547 return sprintf(buf, "0x%llx\n", RBD_FEATURES_SUPPORTED);
548}
549
Greg Kroah-Hartman7e9586b2018-12-21 08:54:38 +0100550static BUS_ATTR_WO(add);
551static BUS_ATTR_WO(remove);
552static BUS_ATTR_WO(add_single_major);
553static BUS_ATTR_WO(remove_single_major);
554static BUS_ATTR_RO(supported_features);
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700555
556static struct attribute *rbd_bus_attrs[] = {
557 &bus_attr_add.attr,
558 &bus_attr_remove.attr,
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200559 &bus_attr_add_single_major.attr,
560 &bus_attr_remove_single_major.attr,
Ilya Dryomov8767b292017-03-02 19:56:57 +0100561 &bus_attr_supported_features.attr,
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700562 NULL,
Alex Elderf0f8cef2012-01-29 13:57:44 -0600563};
Ilya Dryomov92c76dc2013-12-13 15:28:57 +0200564
565static umode_t rbd_bus_is_visible(struct kobject *kobj,
566 struct attribute *attr, int index)
567{
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200568 if (!single_major &&
569 (attr == &bus_attr_add_single_major.attr ||
570 attr == &bus_attr_remove_single_major.attr))
571 return 0;
572
Ilya Dryomov92c76dc2013-12-13 15:28:57 +0200573 return attr->mode;
574}
575
576static const struct attribute_group rbd_bus_group = {
577 .attrs = rbd_bus_attrs,
578 .is_visible = rbd_bus_is_visible,
579};
580__ATTRIBUTE_GROUPS(rbd_bus);
Alex Elderf0f8cef2012-01-29 13:57:44 -0600581
582static struct bus_type rbd_bus_type = {
583 .name = "rbd",
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700584 .bus_groups = rbd_bus_groups,
Alex Elderf0f8cef2012-01-29 13:57:44 -0600585};
586
587static void rbd_root_dev_release(struct device *dev)
588{
589}
590
591static struct device rbd_root_dev = {
592 .init_name = "rbd",
593 .release = rbd_root_dev_release,
594};
595
Alex Elder06ecc6c2012-11-01 10:17:15 -0500596static __printf(2, 3)
597void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...)
598{
599 struct va_format vaf;
600 va_list args;
601
602 va_start(args, fmt);
603 vaf.fmt = fmt;
604 vaf.va = &args;
605
606 if (!rbd_dev)
607 printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf);
608 else if (rbd_dev->disk)
609 printk(KERN_WARNING "%s: %s: %pV\n",
610 RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf);
611 else if (rbd_dev->spec && rbd_dev->spec->image_name)
612 printk(KERN_WARNING "%s: image %s: %pV\n",
613 RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf);
614 else if (rbd_dev->spec && rbd_dev->spec->image_id)
615 printk(KERN_WARNING "%s: id %s: %pV\n",
616 RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf);
617 else /* punt */
618 printk(KERN_WARNING "%s: rbd_dev %p: %pV\n",
619 RBD_DRV_NAME, rbd_dev, &vaf);
620 va_end(args);
621}
622
Alex Elderaafb2302012-09-06 16:00:54 -0500623#ifdef RBD_DEBUG
624#define rbd_assert(expr) \
625 if (unlikely(!(expr))) { \
626 printk(KERN_ERR "\nAssertion failure in %s() " \
627 "at line %d:\n\n" \
628 "\trbd_assert(%s);\n\n", \
629 __func__, __LINE__, #expr); \
630 BUG(); \
631 }
632#else /* !RBD_DEBUG */
633# define rbd_assert(expr) ((void) 0)
634#endif /* !RBD_DEBUG */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800635
Alex Elder05a46af2013-04-26 15:44:36 -0500636static void rbd_dev_remove_parent(struct rbd_device *rbd_dev);
Alex Elder8b3e1a52013-01-24 16:13:36 -0600637
Alex Eldercc4a38bd2013-04-30 00:44:33 -0500638static int rbd_dev_refresh(struct rbd_device *rbd_dev);
Alex Elder2df3fac2013-05-06 09:51:30 -0500639static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev);
Ilya Dryomova720ae02014-07-23 17:11:19 +0400640static int rbd_dev_header_info(struct rbd_device *rbd_dev);
Ilya Dryomove8f59b52014-07-24 10:42:13 +0400641static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev);
Alex Elder54cac612013-04-30 00:44:33 -0500642static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
643 u64 snap_id);
Alex Elder2ad3d712013-04-30 00:44:33 -0500644static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
645 u8 *order, u64 *snap_size);
Ilya Dryomov22e8bd52019-06-05 19:25:11 +0200646static int rbd_dev_v2_get_flags(struct rbd_device *rbd_dev);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700647
Ilya Dryomov54ab3b22019-05-11 16:21:49 +0200648static void rbd_obj_handle_request(struct rbd_obj_request *obj_req, int result);
Ilya Dryomov0192ce22019-05-16 15:06:56 +0200649static void rbd_img_handle_request(struct rbd_img_request *img_req, int result);
650
651/*
652 * Return true if nothing else is pending.
653 */
654static bool pending_result_dec(struct pending_result *pending, int *result)
655{
656 rbd_assert(pending->num_pending > 0);
657
658 if (*result && !pending->result)
659 pending->result = *result;
660 if (--pending->num_pending)
661 return false;
662
663 *result = pending->result;
664 return true;
665}
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700666
667static int rbd_open(struct block_device *bdev, fmode_t mode)
668{
Alex Elderf0f8cef2012-01-29 13:57:44 -0600669 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
Alex Elderb82d1672013-01-14 12:43:31 -0600670 bool removing = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700671
Alex Eldera14ea262013-02-05 13:23:12 -0600672 spin_lock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600673 if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags))
674 removing = true;
675 else
676 rbd_dev->open_count++;
Alex Eldera14ea262013-02-05 13:23:12 -0600677 spin_unlock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600678 if (removing)
679 return -ENOENT;
680
Alex Elderc3e946c2012-11-16 09:29:16 -0600681 (void) get_device(&rbd_dev->dev);
Alex Elder340c7a22012-08-10 13:12:07 -0700682
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700683 return 0;
684}
685
Al Virodb2a1442013-05-05 21:52:57 -0400686static void rbd_release(struct gendisk *disk, fmode_t mode)
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800687{
688 struct rbd_device *rbd_dev = disk->private_data;
Alex Elderb82d1672013-01-14 12:43:31 -0600689 unsigned long open_count_before;
690
Alex Eldera14ea262013-02-05 13:23:12 -0600691 spin_lock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600692 open_count_before = rbd_dev->open_count--;
Alex Eldera14ea262013-02-05 13:23:12 -0600693 spin_unlock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600694 rbd_assert(open_count_before > 0);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800695
Alex Elderc3e946c2012-11-16 09:29:16 -0600696 put_device(&rbd_dev->dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800697}
698
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800699static int rbd_ioctl_set_ro(struct rbd_device *rbd_dev, unsigned long arg)
700{
Ilya Dryomov1de797b2017-10-12 12:35:19 +0200701 int ro;
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800702
Ilya Dryomov1de797b2017-10-12 12:35:19 +0200703 if (get_user(ro, (int __user *)arg))
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800704 return -EFAULT;
705
Ilya Dryomovc1b62052019-11-12 19:50:55 +0100706 /*
707 * Both images mapped read-only and snapshots can't be marked
708 * read-write.
709 */
710 if (!ro) {
711 if (rbd_is_ro(rbd_dev))
712 return -EROFS;
713
714 rbd_assert(!rbd_is_snap(rbd_dev));
715 }
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800716
Ilya Dryomov1de797b2017-10-12 12:35:19 +0200717 /* Let blkdev_roset() handle it */
718 return -ENOTTY;
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800719}
720
721static int rbd_ioctl(struct block_device *bdev, fmode_t mode,
722 unsigned int cmd, unsigned long arg)
723{
724 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
Ilya Dryomov1de797b2017-10-12 12:35:19 +0200725 int ret;
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800726
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800727 switch (cmd) {
728 case BLKROSET:
729 ret = rbd_ioctl_set_ro(rbd_dev, arg);
730 break;
731 default:
732 ret = -ENOTTY;
733 }
734
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800735 return ret;
736}
737
738#ifdef CONFIG_COMPAT
739static int rbd_compat_ioctl(struct block_device *bdev, fmode_t mode,
740 unsigned int cmd, unsigned long arg)
741{
742 return rbd_ioctl(bdev, mode, cmd, arg);
743}
744#endif /* CONFIG_COMPAT */
745
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700746static const struct block_device_operations rbd_bd_ops = {
747 .owner = THIS_MODULE,
748 .open = rbd_open,
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800749 .release = rbd_release,
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800750 .ioctl = rbd_ioctl,
751#ifdef CONFIG_COMPAT
752 .compat_ioctl = rbd_compat_ioctl,
753#endif
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700754};
755
756/*
Alex Elder7262cfc2013-05-16 15:04:20 -0500757 * Initialize an rbd client instance. Success or not, this function
Alex Eldercfbf6372013-05-31 17:40:45 -0500758 * consumes ceph_opts. Caller holds client_mutex.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700759 */
Alex Elderf8c38922012-08-10 13:12:07 -0700760static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700761{
762 struct rbd_client *rbdc;
763 int ret = -ENOMEM;
764
Alex Elder37206ee2013-02-20 17:32:08 -0600765 dout("%s:\n", __func__);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700766 rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL);
767 if (!rbdc)
768 goto out_opt;
769
770 kref_init(&rbdc->kref);
771 INIT_LIST_HEAD(&rbdc->node);
772
Ilya Dryomov74da4a0f2017-03-03 18:16:07 +0100773 rbdc->client = ceph_create_client(ceph_opts, rbdc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700774 if (IS_ERR(rbdc->client))
Alex Elder08f75462013-05-29 11:19:00 -0500775 goto out_rbdc;
Alex Elder43ae4702012-07-03 16:01:18 -0500776 ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700777
778 ret = ceph_open_session(rbdc->client);
779 if (ret < 0)
Alex Elder08f75462013-05-29 11:19:00 -0500780 goto out_client;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700781
Alex Elder432b8582012-01-29 13:57:44 -0600782 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700783 list_add_tail(&rbdc->node, &rbd_client_list);
Alex Elder432b8582012-01-29 13:57:44 -0600784 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700785
Alex Elder37206ee2013-02-20 17:32:08 -0600786 dout("%s: rbdc %p\n", __func__, rbdc);
Alex Elderbc534d82012-01-29 13:57:44 -0600787
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700788 return rbdc;
Alex Elder08f75462013-05-29 11:19:00 -0500789out_client:
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700790 ceph_destroy_client(rbdc->client);
Alex Elder08f75462013-05-29 11:19:00 -0500791out_rbdc:
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700792 kfree(rbdc);
793out_opt:
Alex Elder43ae4702012-07-03 16:01:18 -0500794 if (ceph_opts)
795 ceph_destroy_options(ceph_opts);
Alex Elder37206ee2013-02-20 17:32:08 -0600796 dout("%s: error %d\n", __func__, ret);
797
Vasiliy Kulikov28f259b2010-09-26 12:59:37 +0400798 return ERR_PTR(ret);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700799}
800
Alex Elder2f82ee52012-10-30 19:40:33 -0500801static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc)
802{
803 kref_get(&rbdc->kref);
804
805 return rbdc;
806}
807
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700808/*
Alex Elder1f7ba332012-08-10 13:12:07 -0700809 * Find a ceph client with specific addr and configuration. If
810 * found, bump its reference count.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700811 */
Alex Elder1f7ba332012-08-10 13:12:07 -0700812static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700813{
814 struct rbd_client *client_node;
Alex Elder1f7ba332012-08-10 13:12:07 -0700815 bool found = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700816
Alex Elder43ae4702012-07-03 16:01:18 -0500817 if (ceph_opts->flags & CEPH_OPT_NOSHARE)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700818 return NULL;
819
Alex Elder1f7ba332012-08-10 13:12:07 -0700820 spin_lock(&rbd_client_list_lock);
821 list_for_each_entry(client_node, &rbd_client_list, node) {
822 if (!ceph_compare_options(ceph_opts, client_node->client)) {
Alex Elder2f82ee52012-10-30 19:40:33 -0500823 __rbd_get_client(client_node);
824
Alex Elder1f7ba332012-08-10 13:12:07 -0700825 found = true;
826 break;
827 }
828 }
829 spin_unlock(&rbd_client_list_lock);
830
831 return found ? client_node : NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700832}
833
834/*
Ilya Dryomov210c1042015-06-22 13:24:48 +0300835 * (Per device) rbd map options
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700836 */
837enum {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300838 Opt_queue_depth,
Ilya Dryomov0c93e1b2019-01-30 15:14:48 +0100839 Opt_alloc_size,
Dongsheng Yang34f55d02018-03-26 10:22:55 -0400840 Opt_lock_timeout,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700841 /* int args above */
Ilya Dryomovb26c0472018-07-03 15:28:43 +0200842 Opt_pool_ns,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700843 /* string args above */
Alex Eldercc0538b2012-08-10 13:12:07 -0700844 Opt_read_only,
845 Opt_read_write,
Ilya Dryomov80de1912016-09-20 14:23:17 +0200846 Opt_lock_on_read,
Ilya Dryomove010dd02017-04-13 12:17:39 +0200847 Opt_exclusive,
Ilya Dryomovd9360542018-03-23 06:14:47 +0100848 Opt_notrim,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700849};
850
David Howells82995cc2019-03-25 16:38:32 +0000851static const struct fs_parameter_spec rbd_param_specs[] = {
852 fsparam_u32 ("alloc_size", Opt_alloc_size),
853 fsparam_flag ("exclusive", Opt_exclusive),
854 fsparam_flag ("lock_on_read", Opt_lock_on_read),
855 fsparam_u32 ("lock_timeout", Opt_lock_timeout),
856 fsparam_flag ("notrim", Opt_notrim),
857 fsparam_string ("_pool_ns", Opt_pool_ns),
858 fsparam_u32 ("queue_depth", Opt_queue_depth),
859 fsparam_flag ("read_only", Opt_read_only),
860 fsparam_flag ("read_write", Opt_read_write),
861 fsparam_flag ("ro", Opt_read_only),
862 fsparam_flag ("rw", Opt_read_write),
863 {}
864};
865
866static const struct fs_parameter_description rbd_parameters = {
867 .name = "rbd",
868 .specs = rbd_param_specs,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700869};
870
Alex Elder98571b52013-01-20 14:44:42 -0600871struct rbd_options {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300872 int queue_depth;
Ilya Dryomov0c93e1b2019-01-30 15:14:48 +0100873 int alloc_size;
Dongsheng Yang34f55d02018-03-26 10:22:55 -0400874 unsigned long lock_timeout;
Alex Elder98571b52013-01-20 14:44:42 -0600875 bool read_only;
Ilya Dryomov80de1912016-09-20 14:23:17 +0200876 bool lock_on_read;
Ilya Dryomove010dd02017-04-13 12:17:39 +0200877 bool exclusive;
Ilya Dryomovd9360542018-03-23 06:14:47 +0100878 bool trim;
Alex Elder98571b52013-01-20 14:44:42 -0600879};
880
Ilya Dryomovb5584182015-06-23 16:21:19 +0300881#define RBD_QUEUE_DEPTH_DEFAULT BLKDEV_MAX_RQ
Ilya Dryomov0c93e1b2019-01-30 15:14:48 +0100882#define RBD_ALLOC_SIZE_DEFAULT (64 * 1024)
Dongsheng Yang34f55d02018-03-26 10:22:55 -0400883#define RBD_LOCK_TIMEOUT_DEFAULT 0 /* no timeout */
Alex Elder98571b52013-01-20 14:44:42 -0600884#define RBD_READ_ONLY_DEFAULT false
Ilya Dryomov80de1912016-09-20 14:23:17 +0200885#define RBD_LOCK_ON_READ_DEFAULT false
Ilya Dryomove010dd02017-04-13 12:17:39 +0200886#define RBD_EXCLUSIVE_DEFAULT false
Ilya Dryomovd9360542018-03-23 06:14:47 +0100887#define RBD_TRIM_DEFAULT true
Alex Elder98571b52013-01-20 14:44:42 -0600888
David Howells82995cc2019-03-25 16:38:32 +0000889struct rbd_parse_opts_ctx {
Ilya Dryomovc3001562018-07-03 15:28:43 +0200890 struct rbd_spec *spec;
David Howells82995cc2019-03-25 16:38:32 +0000891 struct ceph_options *copts;
Ilya Dryomovc3001562018-07-03 15:28:43 +0200892 struct rbd_options *opts;
893};
894
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800895static char* obj_op_name(enum obj_operation_type op_type)
896{
897 switch (op_type) {
898 case OBJ_OP_READ:
899 return "read";
900 case OBJ_OP_WRITE:
901 return "write";
Guangliang Zhao90e98c52014-04-01 22:22:16 +0800902 case OBJ_OP_DISCARD:
903 return "discard";
Ilya Dryomov6484cbe2019-01-29 12:46:25 +0100904 case OBJ_OP_ZEROOUT:
905 return "zeroout";
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800906 default:
907 return "???";
908 }
909}
910
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700911/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700912 * Destroy ceph client
Alex Elderd23a4b32012-01-29 13:57:43 -0600913 *
Alex Elder432b8582012-01-29 13:57:44 -0600914 * Caller must hold rbd_client_list_lock.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700915 */
916static void rbd_client_release(struct kref *kref)
917{
918 struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
919
Alex Elder37206ee2013-02-20 17:32:08 -0600920 dout("%s: rbdc %p\n", __func__, rbdc);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500921 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700922 list_del(&rbdc->node);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500923 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700924
925 ceph_destroy_client(rbdc->client);
926 kfree(rbdc);
927}
928
929/*
930 * Drop reference to ceph client node. If it's not referenced anymore, release
931 * it.
932 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500933static void rbd_put_client(struct rbd_client *rbdc)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700934{
Alex Elderc53d5892012-10-25 23:34:42 -0500935 if (rbdc)
936 kref_put(&rbdc->kref, rbd_client_release);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700937}
938
Ilya Dryomov5feb0d8d2018-02-22 13:19:04 +0100939/*
940 * Get a ceph client with specific addr and configuration, if one does
941 * not exist create it. Either way, ceph_opts is consumed by this
942 * function.
943 */
944static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts)
945{
946 struct rbd_client *rbdc;
Ilya Dryomovdd435852018-02-22 13:43:24 +0100947 int ret;
Ilya Dryomov5feb0d8d2018-02-22 13:19:04 +0100948
Ilya Dryomova32e4142019-05-02 15:56:00 +0200949 mutex_lock(&client_mutex);
Ilya Dryomov5feb0d8d2018-02-22 13:19:04 +0100950 rbdc = rbd_client_find(ceph_opts);
Ilya Dryomovdd435852018-02-22 13:43:24 +0100951 if (rbdc) {
Ilya Dryomov5feb0d8d2018-02-22 13:19:04 +0100952 ceph_destroy_options(ceph_opts);
Ilya Dryomovdd435852018-02-22 13:43:24 +0100953
954 /*
955 * Using an existing client. Make sure ->pg_pools is up to
956 * date before we look up the pool id in do_rbd_add().
957 */
Ilya Dryomov9d4a2272019-03-20 10:58:05 +0100958 ret = ceph_wait_for_latest_osdmap(rbdc->client,
959 rbdc->client->options->mount_timeout);
Ilya Dryomovdd435852018-02-22 13:43:24 +0100960 if (ret) {
961 rbd_warn(NULL, "failed to get latest osdmap: %d", ret);
962 rbd_put_client(rbdc);
963 rbdc = ERR_PTR(ret);
964 }
965 } else {
Ilya Dryomov5feb0d8d2018-02-22 13:19:04 +0100966 rbdc = rbd_client_create(ceph_opts);
Ilya Dryomovdd435852018-02-22 13:43:24 +0100967 }
Ilya Dryomov5feb0d8d2018-02-22 13:19:04 +0100968 mutex_unlock(&client_mutex);
969
970 return rbdc;
971}
972
Alex Eldera30b71b2012-07-10 20:30:11 -0500973static bool rbd_image_format_valid(u32 image_format)
974{
975 return image_format == 1 || image_format == 2;
976}
977
Alex Elder8e94af82012-07-25 09:32:40 -0500978static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
979{
Alex Elder103a1502012-08-02 11:29:45 -0500980 size_t size;
981 u32 snap_count;
982
983 /* The header has to start with the magic rbd header text */
984 if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT)))
985 return false;
986
Alex Elderdb2388b2012-10-20 22:17:27 -0500987 /* The bio layer requires at least sector-sized I/O */
988
989 if (ondisk->options.order < SECTOR_SHIFT)
990 return false;
991
992 /* If we use u64 in a few spots we may be able to loosen this */
993
994 if (ondisk->options.order > 8 * sizeof (int) - 1)
995 return false;
996
Alex Elder103a1502012-08-02 11:29:45 -0500997 /*
998 * The size of a snapshot header has to fit in a size_t, and
999 * that limits the number of snapshots.
1000 */
1001 snap_count = le32_to_cpu(ondisk->snap_count);
1002 size = SIZE_MAX - sizeof (struct ceph_snap_context);
1003 if (snap_count > size / sizeof (__le64))
1004 return false;
1005
1006 /*
1007 * Not only that, but the size of the entire the snapshot
1008 * header must also be representable in a size_t.
1009 */
1010 size -= snap_count * sizeof (__le64);
1011 if ((u64) size < le64_to_cpu(ondisk->snap_names_len))
1012 return false;
1013
1014 return true;
Alex Elder8e94af82012-07-25 09:32:40 -05001015}
1016
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001017/*
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +01001018 * returns the size of an object in the image
1019 */
1020static u32 rbd_obj_bytes(struct rbd_image_header *header)
1021{
1022 return 1U << header->obj_order;
1023}
1024
Ilya Dryomov263423f2017-01-25 18:16:22 +01001025static void rbd_init_layout(struct rbd_device *rbd_dev)
1026{
1027 if (rbd_dev->header.stripe_unit == 0 ||
1028 rbd_dev->header.stripe_count == 0) {
1029 rbd_dev->header.stripe_unit = rbd_obj_bytes(&rbd_dev->header);
1030 rbd_dev->header.stripe_count = 1;
1031 }
1032
1033 rbd_dev->layout.stripe_unit = rbd_dev->header.stripe_unit;
1034 rbd_dev->layout.stripe_count = rbd_dev->header.stripe_count;
1035 rbd_dev->layout.object_size = rbd_obj_bytes(&rbd_dev->header);
Ilya Dryomov7e973322017-01-25 18:16:22 +01001036 rbd_dev->layout.pool_id = rbd_dev->header.data_pool_id == CEPH_NOPOOL ?
1037 rbd_dev->spec->pool_id : rbd_dev->header.data_pool_id;
Ilya Dryomov263423f2017-01-25 18:16:22 +01001038 RCU_INIT_POINTER(rbd_dev->layout.pool_ns, NULL);
1039}
1040
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +01001041/*
Alex Elderbb23e372013-05-06 09:51:29 -05001042 * Fill an rbd image header with information from the given format 1
1043 * on-disk header.
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001044 */
Alex Elder662518b2013-05-06 09:51:29 -05001045static int rbd_header_from_disk(struct rbd_device *rbd_dev,
Alex Elder4156d992012-08-02 11:29:46 -05001046 struct rbd_image_header_ondisk *ondisk)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001047{
Alex Elder662518b2013-05-06 09:51:29 -05001048 struct rbd_image_header *header = &rbd_dev->header;
Alex Elderbb23e372013-05-06 09:51:29 -05001049 bool first_time = header->object_prefix == NULL;
1050 struct ceph_snap_context *snapc;
1051 char *object_prefix = NULL;
1052 char *snap_names = NULL;
1053 u64 *snap_sizes = NULL;
Alex Elderccece232012-07-10 20:30:10 -05001054 u32 snap_count;
Alex Elderbb23e372013-05-06 09:51:29 -05001055 int ret = -ENOMEM;
Alex Elder621901d2012-08-23 23:22:06 -05001056 u32 i;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001057
Alex Elderbb23e372013-05-06 09:51:29 -05001058 /* Allocate this now to avoid having to handle failure below */
1059
1060 if (first_time) {
Ilya Dryomov848d7962017-01-25 18:16:21 +01001061 object_prefix = kstrndup(ondisk->object_prefix,
1062 sizeof(ondisk->object_prefix),
1063 GFP_KERNEL);
Alex Elderbb23e372013-05-06 09:51:29 -05001064 if (!object_prefix)
1065 return -ENOMEM;
Alex Elderbb23e372013-05-06 09:51:29 -05001066 }
1067
1068 /* Allocate the snapshot context and fill it in */
Alex Elder6a523252012-07-19 17:12:59 -05001069
Alex Elder103a1502012-08-02 11:29:45 -05001070 snap_count = le32_to_cpu(ondisk->snap_count);
Alex Elderbb23e372013-05-06 09:51:29 -05001071 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
1072 if (!snapc)
1073 goto out_err;
1074 snapc->seq = le64_to_cpu(ondisk->snap_seq);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001075 if (snap_count) {
Alex Elderbb23e372013-05-06 09:51:29 -05001076 struct rbd_image_snap_ondisk *snaps;
Alex Elderf785cc12012-08-23 23:22:06 -05001077 u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len);
1078
Alex Elderbb23e372013-05-06 09:51:29 -05001079 /* We'll keep a copy of the snapshot names... */
Alex Elder621901d2012-08-23 23:22:06 -05001080
Alex Elderbb23e372013-05-06 09:51:29 -05001081 if (snap_names_len > (u64)SIZE_MAX)
1082 goto out_2big;
1083 snap_names = kmalloc(snap_names_len, GFP_KERNEL);
1084 if (!snap_names)
Alex Elder6a523252012-07-19 17:12:59 -05001085 goto out_err;
Alex Elderbb23e372013-05-06 09:51:29 -05001086
1087 /* ...as well as the array of their sizes. */
Markus Elfring88a25a52016-09-11 12:21:25 +02001088 snap_sizes = kmalloc_array(snap_count,
1089 sizeof(*header->snap_sizes),
1090 GFP_KERNEL);
Alex Elderbb23e372013-05-06 09:51:29 -05001091 if (!snap_sizes)
1092 goto out_err;
1093
Alex Elderf785cc12012-08-23 23:22:06 -05001094 /*
Alex Elderbb23e372013-05-06 09:51:29 -05001095 * Copy the names, and fill in each snapshot's id
1096 * and size.
1097 *
Alex Elder99a41eb2013-05-06 09:51:30 -05001098 * Note that rbd_dev_v1_header_info() guarantees the
Alex Elderbb23e372013-05-06 09:51:29 -05001099 * ondisk buffer we're working with has
Alex Elderf785cc12012-08-23 23:22:06 -05001100 * snap_names_len bytes beyond the end of the
1101 * snapshot id array, this memcpy() is safe.
1102 */
Alex Elderbb23e372013-05-06 09:51:29 -05001103 memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len);
1104 snaps = ondisk->snaps;
1105 for (i = 0; i < snap_count; i++) {
1106 snapc->snaps[i] = le64_to_cpu(snaps[i].id);
1107 snap_sizes[i] = le64_to_cpu(snaps[i].image_size);
1108 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001109 }
Alex Elder849b4262012-07-09 21:04:24 -05001110
Alex Elderbb23e372013-05-06 09:51:29 -05001111 /* We won't fail any more, fill in the header */
Alex Elder6a523252012-07-19 17:12:59 -05001112
Alex Elderbb23e372013-05-06 09:51:29 -05001113 if (first_time) {
1114 header->object_prefix = object_prefix;
1115 header->obj_order = ondisk->options.order;
Ilya Dryomov263423f2017-01-25 18:16:22 +01001116 rbd_init_layout(rbd_dev);
Alex Elder662518b2013-05-06 09:51:29 -05001117 } else {
1118 ceph_put_snap_context(header->snapc);
1119 kfree(header->snap_names);
1120 kfree(header->snap_sizes);
Alex Elderbb23e372013-05-06 09:51:29 -05001121 }
1122
1123 /* The remaining fields always get updated (when we refresh) */
Alex Elder621901d2012-08-23 23:22:06 -05001124
Alex Elderf84344f2012-08-31 17:29:51 -05001125 header->image_size = le64_to_cpu(ondisk->image_size);
Alex Elderbb23e372013-05-06 09:51:29 -05001126 header->snapc = snapc;
1127 header->snap_names = snap_names;
1128 header->snap_sizes = snap_sizes;
Alex Elder468521c2013-04-26 09:43:47 -05001129
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001130 return 0;
Alex Elderbb23e372013-05-06 09:51:29 -05001131out_2big:
1132 ret = -EIO;
Alex Elder6a523252012-07-19 17:12:59 -05001133out_err:
Alex Elderbb23e372013-05-06 09:51:29 -05001134 kfree(snap_sizes);
1135 kfree(snap_names);
1136 ceph_put_snap_context(snapc);
1137 kfree(object_prefix);
Alex Elderccece232012-07-10 20:30:10 -05001138
Alex Elderbb23e372013-05-06 09:51:29 -05001139 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001140}
1141
Alex Elder9682fc62013-04-30 00:44:33 -05001142static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which)
1143{
1144 const char *snap_name;
1145
1146 rbd_assert(which < rbd_dev->header.snapc->num_snaps);
1147
1148 /* Skip over names until we find the one we are looking for */
1149
1150 snap_name = rbd_dev->header.snap_names;
1151 while (which--)
1152 snap_name += strlen(snap_name) + 1;
1153
1154 return kstrdup(snap_name, GFP_KERNEL);
1155}
1156
Alex Elder30d1cff2013-05-01 12:43:03 -05001157/*
1158 * Snapshot id comparison function for use with qsort()/bsearch().
1159 * Note that result is for snapshots in *descending* order.
1160 */
1161static int snapid_compare_reverse(const void *s1, const void *s2)
1162{
1163 u64 snap_id1 = *(u64 *)s1;
1164 u64 snap_id2 = *(u64 *)s2;
1165
1166 if (snap_id1 < snap_id2)
1167 return 1;
1168 return snap_id1 == snap_id2 ? 0 : -1;
1169}
1170
1171/*
1172 * Search a snapshot context to see if the given snapshot id is
1173 * present.
1174 *
1175 * Returns the position of the snapshot id in the array if it's found,
1176 * or BAD_SNAP_INDEX otherwise.
1177 *
1178 * Note: The snapshot array is in kept sorted (by the osd) in
1179 * reverse order, highest snapshot id first.
1180 */
Alex Elder9682fc62013-04-30 00:44:33 -05001181static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id)
1182{
1183 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
Alex Elder30d1cff2013-05-01 12:43:03 -05001184 u64 *found;
Alex Elder9682fc62013-04-30 00:44:33 -05001185
Alex Elder30d1cff2013-05-01 12:43:03 -05001186 found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps,
1187 sizeof (snap_id), snapid_compare_reverse);
Alex Elder9682fc62013-04-30 00:44:33 -05001188
Alex Elder30d1cff2013-05-01 12:43:03 -05001189 return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX;
Alex Elder9682fc62013-04-30 00:44:33 -05001190}
1191
Alex Elder2ad3d712013-04-30 00:44:33 -05001192static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev,
1193 u64 snap_id)
Alex Elder54cac612013-04-30 00:44:33 -05001194{
1195 u32 which;
Josh Durginda6a6b62013-09-04 17:57:31 -07001196 const char *snap_name;
Alex Elder54cac612013-04-30 00:44:33 -05001197
1198 which = rbd_dev_snap_index(rbd_dev, snap_id);
1199 if (which == BAD_SNAP_INDEX)
Josh Durginda6a6b62013-09-04 17:57:31 -07001200 return ERR_PTR(-ENOENT);
Alex Elder54cac612013-04-30 00:44:33 -05001201
Josh Durginda6a6b62013-09-04 17:57:31 -07001202 snap_name = _rbd_dev_v1_snap_name(rbd_dev, which);
1203 return snap_name ? snap_name : ERR_PTR(-ENOMEM);
Alex Elder54cac612013-04-30 00:44:33 -05001204}
1205
Alex Elder9e15b772012-10-30 19:40:33 -05001206static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id)
1207{
Alex Elder9e15b772012-10-30 19:40:33 -05001208 if (snap_id == CEPH_NOSNAP)
1209 return RBD_SNAP_HEAD_NAME;
1210
Alex Elder54cac612013-04-30 00:44:33 -05001211 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1212 if (rbd_dev->image_format == 1)
1213 return rbd_dev_v1_snap_name(rbd_dev, snap_id);
Alex Elder9e15b772012-10-30 19:40:33 -05001214
Alex Elder54cac612013-04-30 00:44:33 -05001215 return rbd_dev_v2_snap_name(rbd_dev, snap_id);
Alex Elder9e15b772012-10-30 19:40:33 -05001216}
1217
Alex Elder2ad3d712013-04-30 00:44:33 -05001218static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
1219 u64 *snap_size)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001220{
Alex Elder2ad3d712013-04-30 00:44:33 -05001221 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1222 if (snap_id == CEPH_NOSNAP) {
1223 *snap_size = rbd_dev->header.image_size;
1224 } else if (rbd_dev->image_format == 1) {
1225 u32 which;
Alex Elder00f1f362012-02-07 12:03:36 -06001226
Alex Elder2ad3d712013-04-30 00:44:33 -05001227 which = rbd_dev_snap_index(rbd_dev, snap_id);
1228 if (which == BAD_SNAP_INDEX)
1229 return -ENOENT;
Alex Elder00f1f362012-02-07 12:03:36 -06001230
Alex Elder2ad3d712013-04-30 00:44:33 -05001231 *snap_size = rbd_dev->header.snap_sizes[which];
1232 } else {
1233 u64 size = 0;
1234 int ret;
1235
1236 ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size);
1237 if (ret)
1238 return ret;
1239
1240 *snap_size = size;
1241 }
1242 return 0;
1243}
1244
Alex Elderd1cf5782013-04-27 09:59:30 -05001245static int rbd_dev_mapping_set(struct rbd_device *rbd_dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001246{
Alex Elder8f4b7d92013-05-06 07:40:30 -05001247 u64 snap_id = rbd_dev->spec->snap_id;
Alex Elder2ad3d712013-04-30 00:44:33 -05001248 u64 size = 0;
Alex Elder2ad3d712013-04-30 00:44:33 -05001249 int ret;
Alex Elder8b0241f2013-04-25 23:15:08 -05001250
Alex Elder2ad3d712013-04-30 00:44:33 -05001251 ret = rbd_snap_size(rbd_dev, snap_id, &size);
1252 if (ret)
1253 return ret;
Alex Elder2ad3d712013-04-30 00:44:33 -05001254
1255 rbd_dev->mapping.size = size;
Alex Elder8b0241f2013-04-25 23:15:08 -05001256 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001257}
1258
Alex Elderd1cf5782013-04-27 09:59:30 -05001259static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev)
1260{
1261 rbd_dev->mapping.size = 0;
Alex Elder200a6a82013-04-28 23:32:34 -05001262}
1263
Ilya Dryomov5359a172018-01-20 10:30:10 +01001264static void zero_bvec(struct bio_vec *bv)
Alex Elder65ccfe22012-08-09 10:33:26 -07001265{
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001266 void *buf;
Ilya Dryomov5359a172018-01-20 10:30:10 +01001267 unsigned long flags;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001268
Ilya Dryomov5359a172018-01-20 10:30:10 +01001269 buf = bvec_kmap_irq(bv, &flags);
1270 memset(buf, 0, bv->bv_len);
1271 flush_dcache_page(bv->bv_page);
1272 bvec_kunmap_irq(buf, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001273}
1274
Ilya Dryomov5359a172018-01-20 10:30:10 +01001275static void zero_bios(struct ceph_bio_iter *bio_pos, u32 off, u32 bytes)
Alex Elderb9434c52013-04-19 15:34:50 -05001276{
Ilya Dryomov5359a172018-01-20 10:30:10 +01001277 struct ceph_bio_iter it = *bio_pos;
Alex Elderb9434c52013-04-19 15:34:50 -05001278
Ilya Dryomov5359a172018-01-20 10:30:10 +01001279 ceph_bio_iter_advance(&it, off);
1280 ceph_bio_iter_advance_step(&it, bytes, ({
1281 zero_bvec(&bv);
1282 }));
Alex Elderb9434c52013-04-19 15:34:50 -05001283}
1284
Ilya Dryomov7e07efb2018-01-20 10:30:11 +01001285static void zero_bvecs(struct ceph_bvec_iter *bvec_pos, u32 off, u32 bytes)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001286{
Ilya Dryomov7e07efb2018-01-20 10:30:11 +01001287 struct ceph_bvec_iter it = *bvec_pos;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001288
Ilya Dryomov7e07efb2018-01-20 10:30:11 +01001289 ceph_bvec_iter_advance(&it, off);
1290 ceph_bvec_iter_advance_step(&it, bytes, ({
1291 zero_bvec(&bv);
1292 }));
Alex Elderf7760da2012-10-20 22:17:27 -05001293}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001294
Alex Elderf7760da2012-10-20 22:17:27 -05001295/*
Ilya Dryomov3da691b2018-01-29 14:04:08 +01001296 * Zero a range in @obj_req data buffer defined by a bio (list) or
Ilya Dryomovafb97882018-02-06 19:26:35 +01001297 * (private) bio_vec array.
Alex Elderf7760da2012-10-20 22:17:27 -05001298 *
Ilya Dryomov3da691b2018-01-29 14:04:08 +01001299 * @off is relative to the start of the data buffer.
Alex Elderf7760da2012-10-20 22:17:27 -05001300 */
Ilya Dryomov3da691b2018-01-29 14:04:08 +01001301static void rbd_obj_zero_range(struct rbd_obj_request *obj_req, u32 off,
1302 u32 bytes)
Alex Elderf7760da2012-10-20 22:17:27 -05001303{
Ilya Dryomov54ab3b22019-05-11 16:21:49 +02001304 dout("%s %p data buf %u~%u\n", __func__, obj_req, off, bytes);
1305
Ilya Dryomovecc633c2018-02-01 11:50:47 +01001306 switch (obj_req->img_request->data_type) {
Ilya Dryomov3da691b2018-01-29 14:04:08 +01001307 case OBJ_REQUEST_BIO:
1308 zero_bios(&obj_req->bio_pos, off, bytes);
1309 break;
1310 case OBJ_REQUEST_BVECS:
Ilya Dryomovafb97882018-02-06 19:26:35 +01001311 case OBJ_REQUEST_OWN_BVECS:
Ilya Dryomov3da691b2018-01-29 14:04:08 +01001312 zero_bvecs(&obj_req->bvec_pos, off, bytes);
1313 break;
1314 default:
Arnd Bergmann16809372019-03-22 17:53:56 +01001315 BUG();
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001316 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06001317}
1318
1319static void rbd_obj_request_destroy(struct kref *kref);
1320static void rbd_obj_request_put(struct rbd_obj_request *obj_request)
1321{
1322 rbd_assert(obj_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001323 dout("%s: obj %p (was %d)\n", __func__, obj_request,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01001324 kref_read(&obj_request->kref));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001325 kref_put(&obj_request->kref, rbd_obj_request_destroy);
1326}
1327
Alex Elderbf0d5f502012-11-22 00:00:08 -06001328static void rbd_img_request_destroy(struct kref *kref);
1329static void rbd_img_request_put(struct rbd_img_request *img_request)
1330{
1331 rbd_assert(img_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001332 dout("%s: img %p (was %d)\n", __func__, img_request,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01001333 kref_read(&img_request->kref));
Ilya Dryomove93aca02018-02-06 19:26:35 +01001334 kref_put(&img_request->kref, rbd_img_request_destroy);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001335}
1336
1337static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request,
1338 struct rbd_obj_request *obj_request)
1339{
Alex Elder25dcf952013-01-25 17:08:55 -06001340 rbd_assert(obj_request->img_request == NULL);
1341
Alex Elderb155e862013-04-15 14:50:37 -05001342 /* Image request now owns object's original reference */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001343 obj_request->img_request = img_request;
Ilya Dryomov15961b42018-02-01 11:50:47 +01001344 dout("%s: img %p obj %p\n", __func__, img_request, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001345}
1346
1347static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request,
1348 struct rbd_obj_request *obj_request)
1349{
Ilya Dryomov15961b42018-02-01 11:50:47 +01001350 dout("%s: img %p obj %p\n", __func__, img_request, obj_request);
Ilya Dryomov43df3d32018-02-02 15:23:22 +01001351 list_del(&obj_request->ex.oe_item);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001352 rbd_assert(obj_request->img_request == img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001353 rbd_obj_request_put(obj_request);
1354}
1355
Ilya Dryomova086a1b2019-06-12 18:33:31 +02001356static void rbd_osd_submit(struct ceph_osd_request *osd_req)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001357{
Ilya Dryomova086a1b2019-06-12 18:33:31 +02001358 struct rbd_obj_request *obj_req = osd_req->r_priv;
Ilya Dryomov980917f2016-09-12 18:59:42 +02001359
Ilya Dryomova086a1b2019-06-12 18:33:31 +02001360 dout("%s osd_req %p for obj_req %p objno %llu %llu~%llu\n",
1361 __func__, osd_req, obj_req, obj_req->ex.oe_objno,
1362 obj_req->ex.oe_off, obj_req->ex.oe_len);
Ilya Dryomov980917f2016-09-12 18:59:42 +02001363 ceph_osdc_start_request(osd_req->r_osdc, osd_req, false);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001364}
1365
Alex Elder0c425242013-02-08 09:55:49 -06001366/*
1367 * The default/initial value for all image request flags is 0. Each
1368 * is conditionally set to 1 at image request initialization time
1369 * and currently never change thereafter.
1370 */
Alex Elderd0b2e942013-01-24 16:13:36 -06001371static void img_request_layered_set(struct rbd_img_request *img_request)
1372{
1373 set_bit(IMG_REQ_LAYERED, &img_request->flags);
1374 smp_mb();
1375}
1376
Alex Eldera2acd002013-05-08 22:50:04 -05001377static void img_request_layered_clear(struct rbd_img_request *img_request)
1378{
1379 clear_bit(IMG_REQ_LAYERED, &img_request->flags);
1380 smp_mb();
1381}
1382
Alex Elderd0b2e942013-01-24 16:13:36 -06001383static bool img_request_layered_test(struct rbd_img_request *img_request)
1384{
1385 smp_mb();
1386 return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0;
1387}
1388
Ilya Dryomov3da691b2018-01-29 14:04:08 +01001389static bool rbd_obj_is_entire(struct rbd_obj_request *obj_req)
Josh Durgin3b434a2a2014-04-04 17:32:15 -07001390{
Ilya Dryomov3da691b2018-01-29 14:04:08 +01001391 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
1392
Ilya Dryomov43df3d32018-02-02 15:23:22 +01001393 return !obj_req->ex.oe_off &&
1394 obj_req->ex.oe_len == rbd_dev->layout.object_size;
Josh Durgin3b434a2a2014-04-04 17:32:15 -07001395}
1396
Ilya Dryomov3da691b2018-01-29 14:04:08 +01001397static bool rbd_obj_is_tail(struct rbd_obj_request *obj_req)
Alex Elder6e2a4502013-03-27 09:16:30 -05001398{
Ilya Dryomov3da691b2018-01-29 14:04:08 +01001399 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
Alex Elderb9434c52013-04-19 15:34:50 -05001400
Ilya Dryomov43df3d32018-02-02 15:23:22 +01001401 return obj_req->ex.oe_off + obj_req->ex.oe_len ==
Ilya Dryomov3da691b2018-01-29 14:04:08 +01001402 rbd_dev->layout.object_size;
1403}
1404
Ilya Dryomov13488d52019-02-25 12:37:50 +01001405/*
1406 * Must be called after rbd_obj_calc_img_extents().
1407 */
1408static bool rbd_obj_copyup_enabled(struct rbd_obj_request *obj_req)
1409{
1410 if (!obj_req->num_img_extents ||
Ilya Dryomov9b17eb22019-02-28 15:51:39 +01001411 (rbd_obj_is_entire(obj_req) &&
1412 !obj_req->img_request->snapc->num_snaps))
Ilya Dryomov13488d52019-02-25 12:37:50 +01001413 return false;
1414
1415 return true;
1416}
1417
Ilya Dryomov86bd7992018-02-06 19:26:33 +01001418static u64 rbd_obj_img_extents_bytes(struct rbd_obj_request *obj_req)
1419{
1420 return ceph_file_extents_bytes(obj_req->img_extents,
1421 obj_req->num_img_extents);
1422}
1423
Ilya Dryomov3da691b2018-01-29 14:04:08 +01001424static bool rbd_img_is_write(struct rbd_img_request *img_req)
1425{
Ilya Dryomov9bb02482018-01-30 17:52:10 +01001426 switch (img_req->op_type) {
Ilya Dryomov3da691b2018-01-29 14:04:08 +01001427 case OBJ_OP_READ:
1428 return false;
1429 case OBJ_OP_WRITE:
1430 case OBJ_OP_DISCARD:
Ilya Dryomov6484cbe2019-01-29 12:46:25 +01001431 case OBJ_OP_ZEROOUT:
Ilya Dryomov3da691b2018-01-29 14:04:08 +01001432 return true;
1433 default:
Arnd Bergmannc6244b32018-04-04 14:53:39 +02001434 BUG();
Alex Elder6e2a4502013-03-27 09:16:30 -05001435 }
Alex Elder6e2a4502013-03-27 09:16:30 -05001436}
1437
Ilya Dryomov85e084f2016-04-28 16:07:24 +02001438static void rbd_osd_req_callback(struct ceph_osd_request *osd_req)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001439{
Ilya Dryomov3da691b2018-01-29 14:04:08 +01001440 struct rbd_obj_request *obj_req = osd_req->r_priv;
Ilya Dryomov54ab3b22019-05-11 16:21:49 +02001441 int result;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001442
Ilya Dryomov3da691b2018-01-29 14:04:08 +01001443 dout("%s osd_req %p result %d for obj_req %p\n", __func__, osd_req,
1444 osd_req->r_result, obj_req);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001445
Ilya Dryomov54ab3b22019-05-11 16:21:49 +02001446 /*
1447 * Writes aren't allowed to return a data payload. In some
1448 * guarded write cases (e.g. stat + zero on an empty object)
1449 * a stat response makes it through, but we don't care.
1450 */
1451 if (osd_req->r_result > 0 && rbd_img_is_write(obj_req->img_request))
1452 result = 0;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01001453 else
Ilya Dryomov54ab3b22019-05-11 16:21:49 +02001454 result = osd_req->r_result;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001455
Ilya Dryomov54ab3b22019-05-11 16:21:49 +02001456 rbd_obj_handle_request(obj_req, result);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001457}
1458
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02001459static void rbd_osd_format_read(struct ceph_osd_request *osd_req)
Alex Elder430c28c2013-04-03 21:32:51 -05001460{
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02001461 struct rbd_obj_request *obj_request = osd_req->r_priv;
Alex Elder430c28c2013-04-03 21:32:51 -05001462
Ilya Dryomova162b302018-01-30 17:52:10 +01001463 osd_req->r_flags = CEPH_OSD_FLAG_READ;
Ilya Dryomov7c848832016-09-15 17:56:39 +02001464 osd_req->r_snapid = obj_request->img_request->snap_id;
Alex Elder9d4df012013-04-19 15:34:50 -05001465}
1466
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02001467static void rbd_osd_format_write(struct ceph_osd_request *osd_req)
Alex Elder9d4df012013-04-19 15:34:50 -05001468{
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02001469 struct rbd_obj_request *obj_request = osd_req->r_priv;
Alex Elder9d4df012013-04-19 15:34:50 -05001470
Ilya Dryomova162b302018-01-30 17:52:10 +01001471 osd_req->r_flags = CEPH_OSD_FLAG_WRITE;
Arnd Bergmannfac02dd2018-07-13 22:18:37 +02001472 ktime_get_real_ts64(&osd_req->r_mtime);
Ilya Dryomov43df3d32018-02-02 15:23:22 +01001473 osd_req->r_data_offset = obj_request->ex.oe_off;
Alex Elder430c28c2013-04-03 21:32:51 -05001474}
1475
Ilya Dryomovbc812072017-01-25 18:16:23 +01001476static struct ceph_osd_request *
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02001477__rbd_obj_add_osd_request(struct rbd_obj_request *obj_req,
1478 struct ceph_snap_context *snapc, int num_ops)
Ilya Dryomovbc812072017-01-25 18:16:23 +01001479{
Ilya Dryomove28eded2019-02-25 11:42:26 +01001480 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
Ilya Dryomovbc812072017-01-25 18:16:23 +01001481 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
1482 struct ceph_osd_request *req;
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01001483 const char *name_format = rbd_dev->image_format == 1 ?
1484 RBD_V1_DATA_FORMAT : RBD_V2_DATA_FORMAT;
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02001485 int ret;
Ilya Dryomovbc812072017-01-25 18:16:23 +01001486
Ilya Dryomove28eded2019-02-25 11:42:26 +01001487 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, false, GFP_NOIO);
Ilya Dryomovbc812072017-01-25 18:16:23 +01001488 if (!req)
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02001489 return ERR_PTR(-ENOMEM);
Ilya Dryomovbc812072017-01-25 18:16:23 +01001490
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02001491 list_add_tail(&req->r_private_item, &obj_req->osd_reqs);
Ilya Dryomovbc812072017-01-25 18:16:23 +01001492 req->r_callback = rbd_osd_req_callback;
Ilya Dryomova162b302018-01-30 17:52:10 +01001493 req->r_priv = obj_req;
Ilya Dryomovbc812072017-01-25 18:16:23 +01001494
Ilya Dryomovb26c0472018-07-03 15:28:43 +02001495 /*
1496 * Data objects may be stored in a separate pool, but always in
1497 * the same namespace in that pool as the header in its pool.
1498 */
1499 ceph_oloc_copy(&req->r_base_oloc, &rbd_dev->header_oloc);
Ilya Dryomovbc812072017-01-25 18:16:23 +01001500 req->r_base_oloc.pool = rbd_dev->layout.pool_id;
Ilya Dryomovb26c0472018-07-03 15:28:43 +02001501
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02001502 ret = ceph_oid_aprintf(&req->r_base_oid, GFP_NOIO, name_format,
1503 rbd_dev->header.object_prefix,
1504 obj_req->ex.oe_objno);
1505 if (ret)
1506 return ERR_PTR(ret);
Ilya Dryomovbc812072017-01-25 18:16:23 +01001507
Ilya Dryomovbc812072017-01-25 18:16:23 +01001508 return req;
Ilya Dryomovbc812072017-01-25 18:16:23 +01001509}
1510
Ilya Dryomove28eded2019-02-25 11:42:26 +01001511static struct ceph_osd_request *
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02001512rbd_obj_add_osd_request(struct rbd_obj_request *obj_req, int num_ops)
Ilya Dryomove28eded2019-02-25 11:42:26 +01001513{
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02001514 return __rbd_obj_add_osd_request(obj_req, obj_req->img_request->snapc,
1515 num_ops);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001516}
1517
Ilya Dryomovecc633c2018-02-01 11:50:47 +01001518static struct rbd_obj_request *rbd_obj_request_create(void)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001519{
1520 struct rbd_obj_request *obj_request;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001521
Ilya Dryomov5a60e872015-06-24 17:24:33 +03001522 obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_NOIO);
Ilya Dryomov6c696d82017-01-25 18:16:23 +01001523 if (!obj_request)
Alex Elderf907ad52013-05-01 12:43:03 -05001524 return NULL;
Alex Elderf907ad52013-05-01 12:43:03 -05001525
Ilya Dryomov43df3d32018-02-02 15:23:22 +01001526 ceph_object_extent_init(&obj_request->ex);
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02001527 INIT_LIST_HEAD(&obj_request->osd_reqs);
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02001528 mutex_init(&obj_request->state_mutex);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001529 kref_init(&obj_request->kref);
1530
Ilya Dryomov67e2b652017-01-25 18:16:22 +01001531 dout("%s %p\n", __func__, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001532 return obj_request;
1533}
1534
1535static void rbd_obj_request_destroy(struct kref *kref)
1536{
1537 struct rbd_obj_request *obj_request;
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02001538 struct ceph_osd_request *osd_req;
Ilya Dryomov7e07efb2018-01-20 10:30:11 +01001539 u32 i;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001540
1541 obj_request = container_of(kref, struct rbd_obj_request, kref);
1542
Alex Elder37206ee2013-02-20 17:32:08 -06001543 dout("%s: obj %p\n", __func__, obj_request);
1544
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02001545 while (!list_empty(&obj_request->osd_reqs)) {
1546 osd_req = list_first_entry(&obj_request->osd_reqs,
1547 struct ceph_osd_request, r_private_item);
1548 list_del_init(&osd_req->r_private_item);
1549 ceph_osdc_put_request(osd_req);
1550 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06001551
Ilya Dryomovecc633c2018-02-01 11:50:47 +01001552 switch (obj_request->img_request->data_type) {
Alex Elder9969ebc2013-01-18 12:31:10 -06001553 case OBJ_REQUEST_NODATA:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001554 case OBJ_REQUEST_BIO:
Ilya Dryomov7e07efb2018-01-20 10:30:11 +01001555 case OBJ_REQUEST_BVECS:
Ilya Dryomov5359a172018-01-20 10:30:10 +01001556 break; /* Nothing to do */
Ilya Dryomovafb97882018-02-06 19:26:35 +01001557 case OBJ_REQUEST_OWN_BVECS:
1558 kfree(obj_request->bvec_pos.bvecs);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001559 break;
Ilya Dryomov7e07efb2018-01-20 10:30:11 +01001560 default:
Arnd Bergmann16809372019-03-22 17:53:56 +01001561 BUG();
Alex Elderbf0d5f502012-11-22 00:00:08 -06001562 }
1563
Ilya Dryomov86bd7992018-02-06 19:26:33 +01001564 kfree(obj_request->img_extents);
Ilya Dryomov7e07efb2018-01-20 10:30:11 +01001565 if (obj_request->copyup_bvecs) {
1566 for (i = 0; i < obj_request->copyup_bvec_count; i++) {
1567 if (obj_request->copyup_bvecs[i].bv_page)
1568 __free_page(obj_request->copyup_bvecs[i].bv_page);
1569 }
1570 kfree(obj_request->copyup_bvecs);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001571 }
1572
Alex Elder868311b2013-05-01 12:43:03 -05001573 kmem_cache_free(rbd_obj_request_cache, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001574}
1575
Alex Elderfb65d2282013-05-08 22:50:04 -05001576/* It's OK to call this for a device with no parent */
1577
1578static void rbd_spec_put(struct rbd_spec *spec);
1579static void rbd_dev_unparent(struct rbd_device *rbd_dev)
1580{
1581 rbd_dev_remove_parent(rbd_dev);
1582 rbd_spec_put(rbd_dev->parent_spec);
1583 rbd_dev->parent_spec = NULL;
1584 rbd_dev->parent_overlap = 0;
1585}
1586
Alex Elderbf0d5f502012-11-22 00:00:08 -06001587/*
Alex Eldera2acd002013-05-08 22:50:04 -05001588 * Parent image reference counting is used to determine when an
1589 * image's parent fields can be safely torn down--after there are no
1590 * more in-flight requests to the parent image. When the last
1591 * reference is dropped, cleaning them up is safe.
1592 */
1593static void rbd_dev_parent_put(struct rbd_device *rbd_dev)
1594{
1595 int counter;
1596
1597 if (!rbd_dev->parent_spec)
1598 return;
1599
1600 counter = atomic_dec_return_safe(&rbd_dev->parent_ref);
1601 if (counter > 0)
1602 return;
1603
1604 /* Last reference; clean up parent data structures */
1605
1606 if (!counter)
1607 rbd_dev_unparent(rbd_dev);
1608 else
Ilya Dryomov9584d502014-07-11 12:11:20 +04001609 rbd_warn(rbd_dev, "parent reference underflow");
Alex Eldera2acd002013-05-08 22:50:04 -05001610}
1611
1612/*
1613 * If an image has a non-zero parent overlap, get a reference to its
1614 * parent.
1615 *
1616 * Returns true if the rbd device has a parent with a non-zero
1617 * overlap and a reference for it was successfully taken, or
1618 * false otherwise.
1619 */
1620static bool rbd_dev_parent_get(struct rbd_device *rbd_dev)
1621{
Ilya Dryomovae43e9d2015-01-19 18:13:43 +03001622 int counter = 0;
Alex Eldera2acd002013-05-08 22:50:04 -05001623
1624 if (!rbd_dev->parent_spec)
1625 return false;
1626
Ilya Dryomovae43e9d2015-01-19 18:13:43 +03001627 down_read(&rbd_dev->header_rwsem);
1628 if (rbd_dev->parent_overlap)
1629 counter = atomic_inc_return_safe(&rbd_dev->parent_ref);
1630 up_read(&rbd_dev->header_rwsem);
Alex Eldera2acd002013-05-08 22:50:04 -05001631
1632 if (counter < 0)
Ilya Dryomov9584d502014-07-11 12:11:20 +04001633 rbd_warn(rbd_dev, "parent reference overflow");
Alex Eldera2acd002013-05-08 22:50:04 -05001634
Ilya Dryomovae43e9d2015-01-19 18:13:43 +03001635 return counter > 0;
Alex Eldera2acd002013-05-08 22:50:04 -05001636}
1637
Alex Elderbf0d5f502012-11-22 00:00:08 -06001638/*
1639 * Caller is responsible for filling in the list of object requests
1640 * that comprises the image request, and the Linux request pointer
1641 * (if there is one).
1642 */
Alex Eldercc344fa2013-02-19 12:25:56 -06001643static struct rbd_img_request *rbd_img_request_create(
1644 struct rbd_device *rbd_dev,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08001645 enum obj_operation_type op_type,
Josh Durgin4e752f02014-04-08 11:12:11 -07001646 struct ceph_snap_context *snapc)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001647{
1648 struct rbd_img_request *img_request;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001649
Ilya Dryomova0c58952018-01-22 16:03:06 +01001650 img_request = kmem_cache_zalloc(rbd_img_request_cache, GFP_NOIO);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001651 if (!img_request)
1652 return NULL;
1653
Alex Elderbf0d5f502012-11-22 00:00:08 -06001654 img_request->rbd_dev = rbd_dev;
Ilya Dryomov9bb02482018-01-30 17:52:10 +01001655 img_request->op_type = op_type;
Ilya Dryomov9bb02482018-01-30 17:52:10 +01001656 if (!rbd_img_is_write(img_request))
Alex Elderbf0d5f502012-11-22 00:00:08 -06001657 img_request->snap_id = rbd_dev->spec->snap_id;
Ilya Dryomov9bb02482018-01-30 17:52:10 +01001658 else
1659 img_request->snapc = snapc;
1660
Alex Eldera2acd002013-05-08 22:50:04 -05001661 if (rbd_dev_parent_get(rbd_dev))
Alex Elderd0b2e942013-01-24 16:13:36 -06001662 img_request_layered_set(img_request);
Ilya Dryomova0c58952018-01-22 16:03:06 +01001663
Ilya Dryomove1fddc82019-05-30 16:07:48 +02001664 INIT_LIST_HEAD(&img_request->lock_item);
Ilya Dryomov43df3d32018-02-02 15:23:22 +01001665 INIT_LIST_HEAD(&img_request->object_extents);
Ilya Dryomov0192ce22019-05-16 15:06:56 +02001666 mutex_init(&img_request->state_mutex);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001667 kref_init(&img_request->kref);
1668
Alex Elderbf0d5f502012-11-22 00:00:08 -06001669 return img_request;
1670}
1671
1672static void rbd_img_request_destroy(struct kref *kref)
1673{
1674 struct rbd_img_request *img_request;
1675 struct rbd_obj_request *obj_request;
1676 struct rbd_obj_request *next_obj_request;
1677
1678 img_request = container_of(kref, struct rbd_img_request, kref);
1679
Alex Elder37206ee2013-02-20 17:32:08 -06001680 dout("%s: img %p\n", __func__, img_request);
1681
Ilya Dryomove1fddc82019-05-30 16:07:48 +02001682 WARN_ON(!list_empty(&img_request->lock_item));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001683 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
1684 rbd_img_obj_request_del(img_request, obj_request);
1685
Alex Eldera2acd002013-05-08 22:50:04 -05001686 if (img_request_layered_test(img_request)) {
1687 img_request_layered_clear(img_request);
1688 rbd_dev_parent_put(img_request->rbd_dev);
1689 }
1690
Ilya Dryomov9bb02482018-01-30 17:52:10 +01001691 if (rbd_img_is_write(img_request))
Alex Elder812164f82013-04-30 00:44:32 -05001692 ceph_put_snap_context(img_request->snapc);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001693
Alex Elder1c2a9df2013-05-01 12:43:03 -05001694 kmem_cache_free(rbd_img_request_cache, img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001695}
1696
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02001697#define BITS_PER_OBJ 2
1698#define OBJS_PER_BYTE (BITS_PER_BYTE / BITS_PER_OBJ)
1699#define OBJ_MASK ((1 << BITS_PER_OBJ) - 1)
1700
1701static void __rbd_object_map_index(struct rbd_device *rbd_dev, u64 objno,
1702 u64 *index, u8 *shift)
1703{
1704 u32 off;
1705
1706 rbd_assert(objno < rbd_dev->object_map_size);
1707 *index = div_u64_rem(objno, OBJS_PER_BYTE, &off);
1708 *shift = (OBJS_PER_BYTE - off - 1) * BITS_PER_OBJ;
1709}
1710
1711static u8 __rbd_object_map_get(struct rbd_device *rbd_dev, u64 objno)
1712{
1713 u64 index;
1714 u8 shift;
1715
1716 lockdep_assert_held(&rbd_dev->object_map_lock);
1717 __rbd_object_map_index(rbd_dev, objno, &index, &shift);
1718 return (rbd_dev->object_map[index] >> shift) & OBJ_MASK;
1719}
1720
1721static void __rbd_object_map_set(struct rbd_device *rbd_dev, u64 objno, u8 val)
1722{
1723 u64 index;
1724 u8 shift;
1725 u8 *p;
1726
1727 lockdep_assert_held(&rbd_dev->object_map_lock);
1728 rbd_assert(!(val & ~OBJ_MASK));
1729
1730 __rbd_object_map_index(rbd_dev, objno, &index, &shift);
1731 p = &rbd_dev->object_map[index];
1732 *p = (*p & ~(OBJ_MASK << shift)) | (val << shift);
1733}
1734
1735static u8 rbd_object_map_get(struct rbd_device *rbd_dev, u64 objno)
1736{
1737 u8 state;
1738
1739 spin_lock(&rbd_dev->object_map_lock);
1740 state = __rbd_object_map_get(rbd_dev, objno);
1741 spin_unlock(&rbd_dev->object_map_lock);
1742 return state;
1743}
1744
1745static bool use_object_map(struct rbd_device *rbd_dev)
1746{
Ilya Dryomov3fe69922019-11-12 19:41:48 +01001747 /*
1748 * An image mapped read-only can't use the object map -- it isn't
1749 * loaded because the header lock isn't acquired. Someone else can
1750 * write to the image and update the object map behind our back.
1751 *
1752 * A snapshot can't be written to, so using the object map is always
1753 * safe.
1754 */
1755 if (!rbd_is_snap(rbd_dev) && rbd_is_ro(rbd_dev))
1756 return false;
1757
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02001758 return ((rbd_dev->header.features & RBD_FEATURE_OBJECT_MAP) &&
1759 !(rbd_dev->object_map_flags & RBD_FLAG_OBJECT_MAP_INVALID));
1760}
1761
1762static bool rbd_object_map_may_exist(struct rbd_device *rbd_dev, u64 objno)
1763{
1764 u8 state;
1765
1766 /* fall back to default logic if object map is disabled or invalid */
1767 if (!use_object_map(rbd_dev))
1768 return true;
1769
1770 state = rbd_object_map_get(rbd_dev, objno);
1771 return state != OBJECT_NONEXISTENT;
1772}
1773
1774static void rbd_object_map_name(struct rbd_device *rbd_dev, u64 snap_id,
1775 struct ceph_object_id *oid)
1776{
1777 if (snap_id == CEPH_NOSNAP)
1778 ceph_oid_printf(oid, "%s%s", RBD_OBJECT_MAP_PREFIX,
1779 rbd_dev->spec->image_id);
1780 else
1781 ceph_oid_printf(oid, "%s%s.%016llx", RBD_OBJECT_MAP_PREFIX,
1782 rbd_dev->spec->image_id, snap_id);
1783}
1784
1785static int rbd_object_map_lock(struct rbd_device *rbd_dev)
1786{
1787 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
1788 CEPH_DEFINE_OID_ONSTACK(oid);
1789 u8 lock_type;
1790 char *lock_tag;
1791 struct ceph_locker *lockers;
1792 u32 num_lockers;
1793 bool broke_lock = false;
1794 int ret;
1795
1796 rbd_object_map_name(rbd_dev, CEPH_NOSNAP, &oid);
1797
1798again:
1799 ret = ceph_cls_lock(osdc, &oid, &rbd_dev->header_oloc, RBD_LOCK_NAME,
1800 CEPH_CLS_LOCK_EXCLUSIVE, "", "", "", 0);
1801 if (ret != -EBUSY || broke_lock) {
1802 if (ret == -EEXIST)
1803 ret = 0; /* already locked by myself */
1804 if (ret)
1805 rbd_warn(rbd_dev, "failed to lock object map: %d", ret);
1806 return ret;
1807 }
1808
1809 ret = ceph_cls_lock_info(osdc, &oid, &rbd_dev->header_oloc,
1810 RBD_LOCK_NAME, &lock_type, &lock_tag,
1811 &lockers, &num_lockers);
1812 if (ret) {
1813 if (ret == -ENOENT)
1814 goto again;
1815
1816 rbd_warn(rbd_dev, "failed to get object map lockers: %d", ret);
1817 return ret;
1818 }
1819
1820 kfree(lock_tag);
1821 if (num_lockers == 0)
1822 goto again;
1823
1824 rbd_warn(rbd_dev, "breaking object map lock owned by %s%llu",
1825 ENTITY_NAME(lockers[0].id.name));
1826
1827 ret = ceph_cls_break_lock(osdc, &oid, &rbd_dev->header_oloc,
1828 RBD_LOCK_NAME, lockers[0].id.cookie,
1829 &lockers[0].id.name);
1830 ceph_free_lockers(lockers, num_lockers);
1831 if (ret) {
1832 if (ret == -ENOENT)
1833 goto again;
1834
1835 rbd_warn(rbd_dev, "failed to break object map lock: %d", ret);
1836 return ret;
1837 }
1838
1839 broke_lock = true;
1840 goto again;
1841}
1842
1843static void rbd_object_map_unlock(struct rbd_device *rbd_dev)
1844{
1845 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
1846 CEPH_DEFINE_OID_ONSTACK(oid);
1847 int ret;
1848
1849 rbd_object_map_name(rbd_dev, CEPH_NOSNAP, &oid);
1850
1851 ret = ceph_cls_unlock(osdc, &oid, &rbd_dev->header_oloc, RBD_LOCK_NAME,
1852 "");
1853 if (ret && ret != -ENOENT)
1854 rbd_warn(rbd_dev, "failed to unlock object map: %d", ret);
1855}
1856
1857static int decode_object_map_header(void **p, void *end, u64 *object_map_size)
1858{
1859 u8 struct_v;
1860 u32 struct_len;
1861 u32 header_len;
1862 void *header_end;
1863 int ret;
1864
1865 ceph_decode_32_safe(p, end, header_len, e_inval);
1866 header_end = *p + header_len;
1867
1868 ret = ceph_start_decoding(p, end, 1, "BitVector header", &struct_v,
1869 &struct_len);
1870 if (ret)
1871 return ret;
1872
1873 ceph_decode_64_safe(p, end, *object_map_size, e_inval);
1874
1875 *p = header_end;
1876 return 0;
1877
1878e_inval:
1879 return -EINVAL;
1880}
1881
1882static int __rbd_object_map_load(struct rbd_device *rbd_dev)
1883{
1884 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
1885 CEPH_DEFINE_OID_ONSTACK(oid);
1886 struct page **pages;
1887 void *p, *end;
1888 size_t reply_len;
1889 u64 num_objects;
1890 u64 object_map_bytes;
1891 u64 object_map_size;
1892 int num_pages;
1893 int ret;
1894
1895 rbd_assert(!rbd_dev->object_map && !rbd_dev->object_map_size);
1896
1897 num_objects = ceph_get_num_objects(&rbd_dev->layout,
1898 rbd_dev->mapping.size);
1899 object_map_bytes = DIV_ROUND_UP_ULL(num_objects * BITS_PER_OBJ,
1900 BITS_PER_BYTE);
1901 num_pages = calc_pages_for(0, object_map_bytes) + 1;
1902 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
1903 if (IS_ERR(pages))
1904 return PTR_ERR(pages);
1905
1906 reply_len = num_pages * PAGE_SIZE;
1907 rbd_object_map_name(rbd_dev, rbd_dev->spec->snap_id, &oid);
1908 ret = ceph_osdc_call(osdc, &oid, &rbd_dev->header_oloc,
1909 "rbd", "object_map_load", CEPH_OSD_FLAG_READ,
1910 NULL, 0, pages, &reply_len);
1911 if (ret)
1912 goto out;
1913
1914 p = page_address(pages[0]);
1915 end = p + min(reply_len, (size_t)PAGE_SIZE);
1916 ret = decode_object_map_header(&p, end, &object_map_size);
1917 if (ret)
1918 goto out;
1919
1920 if (object_map_size != num_objects) {
1921 rbd_warn(rbd_dev, "object map size mismatch: %llu vs %llu",
1922 object_map_size, num_objects);
1923 ret = -EINVAL;
1924 goto out;
1925 }
1926
1927 if (offset_in_page(p) + object_map_bytes > reply_len) {
1928 ret = -EINVAL;
1929 goto out;
1930 }
1931
1932 rbd_dev->object_map = kvmalloc(object_map_bytes, GFP_KERNEL);
1933 if (!rbd_dev->object_map) {
1934 ret = -ENOMEM;
1935 goto out;
1936 }
1937
1938 rbd_dev->object_map_size = object_map_size;
1939 ceph_copy_from_page_vector(pages, rbd_dev->object_map,
1940 offset_in_page(p), object_map_bytes);
1941
1942out:
1943 ceph_release_page_vector(pages, num_pages);
1944 return ret;
1945}
1946
1947static void rbd_object_map_free(struct rbd_device *rbd_dev)
1948{
1949 kvfree(rbd_dev->object_map);
1950 rbd_dev->object_map = NULL;
1951 rbd_dev->object_map_size = 0;
1952}
1953
1954static int rbd_object_map_load(struct rbd_device *rbd_dev)
1955{
1956 int ret;
1957
1958 ret = __rbd_object_map_load(rbd_dev);
1959 if (ret)
1960 return ret;
1961
1962 ret = rbd_dev_v2_get_flags(rbd_dev);
1963 if (ret) {
1964 rbd_object_map_free(rbd_dev);
1965 return ret;
1966 }
1967
1968 if (rbd_dev->object_map_flags & RBD_FLAG_OBJECT_MAP_INVALID)
1969 rbd_warn(rbd_dev, "object map is invalid");
1970
1971 return 0;
1972}
1973
1974static int rbd_object_map_open(struct rbd_device *rbd_dev)
1975{
1976 int ret;
1977
1978 ret = rbd_object_map_lock(rbd_dev);
1979 if (ret)
1980 return ret;
1981
1982 ret = rbd_object_map_load(rbd_dev);
1983 if (ret) {
1984 rbd_object_map_unlock(rbd_dev);
1985 return ret;
1986 }
1987
1988 return 0;
1989}
1990
1991static void rbd_object_map_close(struct rbd_device *rbd_dev)
1992{
1993 rbd_object_map_free(rbd_dev);
1994 rbd_object_map_unlock(rbd_dev);
1995}
1996
1997/*
1998 * This function needs snap_id (or more precisely just something to
1999 * distinguish between HEAD and snapshot object maps), new_state and
2000 * current_state that were passed to rbd_object_map_update().
2001 *
2002 * To avoid allocating and stashing a context we piggyback on the OSD
2003 * request. A HEAD update has two ops (assert_locked). For new_state
2004 * and current_state we decode our own object_map_update op, encoded in
2005 * rbd_cls_object_map_update().
2006 */
2007static int rbd_object_map_update_finish(struct rbd_obj_request *obj_req,
2008 struct ceph_osd_request *osd_req)
2009{
2010 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
2011 struct ceph_osd_data *osd_data;
2012 u64 objno;
Ilya Dryomov633739b2019-11-13 12:07:15 +01002013 u8 state, new_state, uninitialized_var(current_state);
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02002014 bool has_current_state;
2015 void *p;
2016
2017 if (osd_req->r_result)
2018 return osd_req->r_result;
2019
2020 /*
2021 * Nothing to do for a snapshot object map.
2022 */
2023 if (osd_req->r_num_ops == 1)
2024 return 0;
2025
2026 /*
2027 * Update in-memory HEAD object map.
2028 */
2029 rbd_assert(osd_req->r_num_ops == 2);
2030 osd_data = osd_req_op_data(osd_req, 1, cls, request_data);
2031 rbd_assert(osd_data->type == CEPH_OSD_DATA_TYPE_PAGES);
2032
2033 p = page_address(osd_data->pages[0]);
2034 objno = ceph_decode_64(&p);
2035 rbd_assert(objno == obj_req->ex.oe_objno);
2036 rbd_assert(ceph_decode_64(&p) == objno + 1);
2037 new_state = ceph_decode_8(&p);
2038 has_current_state = ceph_decode_8(&p);
2039 if (has_current_state)
2040 current_state = ceph_decode_8(&p);
2041
2042 spin_lock(&rbd_dev->object_map_lock);
2043 state = __rbd_object_map_get(rbd_dev, objno);
2044 if (!has_current_state || current_state == state ||
2045 (current_state == OBJECT_EXISTS && state == OBJECT_EXISTS_CLEAN))
2046 __rbd_object_map_set(rbd_dev, objno, new_state);
2047 spin_unlock(&rbd_dev->object_map_lock);
2048
2049 return 0;
2050}
2051
2052static void rbd_object_map_callback(struct ceph_osd_request *osd_req)
2053{
2054 struct rbd_obj_request *obj_req = osd_req->r_priv;
2055 int result;
2056
2057 dout("%s osd_req %p result %d for obj_req %p\n", __func__, osd_req,
2058 osd_req->r_result, obj_req);
2059
2060 result = rbd_object_map_update_finish(obj_req, osd_req);
2061 rbd_obj_handle_request(obj_req, result);
2062}
2063
2064static bool update_needed(struct rbd_device *rbd_dev, u64 objno, u8 new_state)
2065{
2066 u8 state = rbd_object_map_get(rbd_dev, objno);
2067
2068 if (state == new_state ||
2069 (new_state == OBJECT_PENDING && state == OBJECT_NONEXISTENT) ||
2070 (new_state == OBJECT_NONEXISTENT && state != OBJECT_PENDING))
2071 return false;
2072
2073 return true;
2074}
2075
2076static int rbd_cls_object_map_update(struct ceph_osd_request *req,
2077 int which, u64 objno, u8 new_state,
2078 const u8 *current_state)
2079{
2080 struct page **pages;
2081 void *p, *start;
2082 int ret;
2083
2084 ret = osd_req_op_cls_init(req, which, "rbd", "object_map_update");
2085 if (ret)
2086 return ret;
2087
2088 pages = ceph_alloc_page_vector(1, GFP_NOIO);
2089 if (IS_ERR(pages))
2090 return PTR_ERR(pages);
2091
2092 p = start = page_address(pages[0]);
2093 ceph_encode_64(&p, objno);
2094 ceph_encode_64(&p, objno + 1);
2095 ceph_encode_8(&p, new_state);
2096 if (current_state) {
2097 ceph_encode_8(&p, 1);
2098 ceph_encode_8(&p, *current_state);
2099 } else {
2100 ceph_encode_8(&p, 0);
2101 }
2102
2103 osd_req_op_cls_request_data_pages(req, which, pages, p - start, 0,
2104 false, true);
2105 return 0;
2106}
2107
2108/*
2109 * Return:
2110 * 0 - object map update sent
2111 * 1 - object map update isn't needed
2112 * <0 - error
2113 */
2114static int rbd_object_map_update(struct rbd_obj_request *obj_req, u64 snap_id,
2115 u8 new_state, const u8 *current_state)
2116{
2117 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
2118 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
2119 struct ceph_osd_request *req;
2120 int num_ops = 1;
2121 int which = 0;
2122 int ret;
2123
2124 if (snap_id == CEPH_NOSNAP) {
2125 if (!update_needed(rbd_dev, obj_req->ex.oe_objno, new_state))
2126 return 1;
2127
2128 num_ops++; /* assert_locked */
2129 }
2130
2131 req = ceph_osdc_alloc_request(osdc, NULL, num_ops, false, GFP_NOIO);
2132 if (!req)
2133 return -ENOMEM;
2134
2135 list_add_tail(&req->r_private_item, &obj_req->osd_reqs);
2136 req->r_callback = rbd_object_map_callback;
2137 req->r_priv = obj_req;
2138
2139 rbd_object_map_name(rbd_dev, snap_id, &req->r_base_oid);
2140 ceph_oloc_copy(&req->r_base_oloc, &rbd_dev->header_oloc);
2141 req->r_flags = CEPH_OSD_FLAG_WRITE;
2142 ktime_get_real_ts64(&req->r_mtime);
2143
2144 if (snap_id == CEPH_NOSNAP) {
2145 /*
2146 * Protect against possible race conditions during lock
2147 * ownership transitions.
2148 */
2149 ret = ceph_cls_assert_locked(req, which++, RBD_LOCK_NAME,
2150 CEPH_CLS_LOCK_EXCLUSIVE, "", "");
2151 if (ret)
2152 return ret;
2153 }
2154
2155 ret = rbd_cls_object_map_update(req, which, obj_req->ex.oe_objno,
2156 new_state, current_state);
2157 if (ret)
2158 return ret;
2159
2160 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
2161 if (ret)
2162 return ret;
2163
2164 ceph_osdc_start_request(osdc, req, false);
2165 return 0;
2166}
2167
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002168static void prune_extents(struct ceph_file_extent *img_extents,
2169 u32 *num_img_extents, u64 overlap)
Alex Eldere93f3152013-05-08 22:50:04 -05002170{
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002171 u32 cnt = *num_img_extents;
Alex Eldere93f3152013-05-08 22:50:04 -05002172
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002173 /* drop extents completely beyond the overlap */
2174 while (cnt && img_extents[cnt - 1].fe_off >= overlap)
2175 cnt--;
Alex Eldere93f3152013-05-08 22:50:04 -05002176
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002177 if (cnt) {
2178 struct ceph_file_extent *ex = &img_extents[cnt - 1];
Alex Eldere93f3152013-05-08 22:50:04 -05002179
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002180 /* trim final overlapping extent */
2181 if (ex->fe_off + ex->fe_len > overlap)
2182 ex->fe_len = overlap - ex->fe_off;
Alex Elder12178572013-02-08 09:55:49 -06002183 }
2184
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002185 *num_img_extents = cnt;
Alex Elder21692382013-04-05 01:27:12 -05002186}
2187
Alex Elderf1a47392013-04-19 15:34:50 -05002188/*
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002189 * Determine the byte range(s) covered by either just the object extent
2190 * or the entire object in the parent image.
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002191 */
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002192static int rbd_obj_calc_img_extents(struct rbd_obj_request *obj_req,
2193 bool entire)
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002194{
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002195 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002196 int ret;
2197
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002198 if (!rbd_dev->parent_overlap)
2199 return 0;
2200
2201 ret = ceph_extent_to_file(&rbd_dev->layout, obj_req->ex.oe_objno,
2202 entire ? 0 : obj_req->ex.oe_off,
2203 entire ? rbd_dev->layout.object_size :
2204 obj_req->ex.oe_len,
2205 &obj_req->img_extents,
2206 &obj_req->num_img_extents);
2207 if (ret)
2208 return ret;
2209
2210 prune_extents(obj_req->img_extents, &obj_req->num_img_extents,
2211 rbd_dev->parent_overlap);
2212 return 0;
2213}
2214
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02002215static void rbd_osd_setup_data(struct ceph_osd_request *osd_req, int which)
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002216{
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02002217 struct rbd_obj_request *obj_req = osd_req->r_priv;
2218
Ilya Dryomovecc633c2018-02-01 11:50:47 +01002219 switch (obj_req->img_request->data_type) {
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002220 case OBJ_REQUEST_BIO:
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02002221 osd_req_op_extent_osd_data_bio(osd_req, which,
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002222 &obj_req->bio_pos,
Ilya Dryomov43df3d32018-02-02 15:23:22 +01002223 obj_req->ex.oe_len);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002224 break;
2225 case OBJ_REQUEST_BVECS:
Ilya Dryomovafb97882018-02-06 19:26:35 +01002226 case OBJ_REQUEST_OWN_BVECS:
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002227 rbd_assert(obj_req->bvec_pos.iter.bi_size ==
Ilya Dryomov43df3d32018-02-02 15:23:22 +01002228 obj_req->ex.oe_len);
Ilya Dryomovafb97882018-02-06 19:26:35 +01002229 rbd_assert(obj_req->bvec_idx == obj_req->bvec_count);
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02002230 osd_req_op_extent_osd_data_bvec_pos(osd_req, which,
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002231 &obj_req->bvec_pos);
2232 break;
2233 default:
Arnd Bergmann16809372019-03-22 17:53:56 +01002234 BUG();
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002235 }
2236}
2237
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02002238static int rbd_osd_setup_stat(struct ceph_osd_request *osd_req, int which)
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002239{
2240 struct page **pages;
Ilya Dryomov710214e2016-09-15 17:53:32 +02002241
Alex Elderc5b5ef62013-02-11 12:33:24 -06002242 /*
2243 * The response data for a STAT call consists of:
2244 * le64 length;
2245 * struct {
2246 * le32 tv_sec;
2247 * le32 tv_nsec;
2248 * } mtime;
2249 */
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002250 pages = ceph_alloc_page_vector(1, GFP_NOIO);
2251 if (IS_ERR(pages))
2252 return PTR_ERR(pages);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002253
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02002254 osd_req_op_init(osd_req, which, CEPH_OSD_OP_STAT, 0);
2255 osd_req_op_raw_data_in_pages(osd_req, which, pages,
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002256 8 + sizeof(struct ceph_timespec),
2257 0, false, true);
Ilya Dryomov980917f2016-09-12 18:59:42 +02002258 return 0;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002259}
2260
Ilya Dryomovb5ae8cb2019-05-29 16:53:14 +02002261static int rbd_osd_setup_copyup(struct ceph_osd_request *osd_req, int which,
2262 u32 bytes)
Ilya Dryomov13488d52019-02-25 12:37:50 +01002263{
Ilya Dryomovb5ae8cb2019-05-29 16:53:14 +02002264 struct rbd_obj_request *obj_req = osd_req->r_priv;
2265 int ret;
2266
2267 ret = osd_req_op_cls_init(osd_req, which, "rbd", "copyup");
2268 if (ret)
2269 return ret;
2270
2271 osd_req_op_cls_request_data_bvecs(osd_req, which, obj_req->copyup_bvecs,
2272 obj_req->copyup_bvec_count, bytes);
2273 return 0;
Ilya Dryomov13488d52019-02-25 12:37:50 +01002274}
2275
Ilya Dryomovea9b7432019-05-31 15:11:26 +02002276static int rbd_obj_init_read(struct rbd_obj_request *obj_req)
Alex Elderb454e362013-04-19 15:34:50 -05002277{
Ilya Dryomovea9b7432019-05-31 15:11:26 +02002278 obj_req->read_state = RBD_OBJ_READ_START;
2279 return 0;
2280}
2281
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02002282static void __rbd_osd_setup_write_ops(struct ceph_osd_request *osd_req,
2283 int which)
Alex Elderb454e362013-04-19 15:34:50 -05002284{
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02002285 struct rbd_obj_request *obj_req = osd_req->r_priv;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002286 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
2287 u16 opcode;
Alex Elderb454e362013-04-19 15:34:50 -05002288
Ilya Dryomov8b5bec52019-06-19 15:45:27 +02002289 if (!use_object_map(rbd_dev) ||
2290 !(obj_req->flags & RBD_OBJ_FLAG_MAY_EXIST)) {
2291 osd_req_op_alloc_hint_init(osd_req, which++,
2292 rbd_dev->layout.object_size,
2293 rbd_dev->layout.object_size);
2294 }
Alex Elderb454e362013-04-19 15:34:50 -05002295
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002296 if (rbd_obj_is_entire(obj_req))
2297 opcode = CEPH_OSD_OP_WRITEFULL;
2298 else
2299 opcode = CEPH_OSD_OP_WRITE;
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002300
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02002301 osd_req_op_extent_init(osd_req, which, opcode,
Ilya Dryomov43df3d32018-02-02 15:23:22 +01002302 obj_req->ex.oe_off, obj_req->ex.oe_len, 0, 0);
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02002303 rbd_osd_setup_data(osd_req, which);
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002304}
2305
Ilya Dryomovea9b7432019-05-31 15:11:26 +02002306static int rbd_obj_init_write(struct rbd_obj_request *obj_req)
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002307{
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002308 int ret;
Ilya Dryomov058aa992016-09-12 14:44:45 +02002309
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002310 /* reverse map the entire object onto the parent */
2311 ret = rbd_obj_calc_img_extents(obj_req, true);
2312 if (ret)
2313 return ret;
2314
Ilya Dryomov0ad5d952019-05-14 20:45:38 +02002315 if (rbd_obj_copyup_enabled(obj_req))
2316 obj_req->flags |= RBD_OBJ_FLAG_COPYUP_ENABLED;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002317
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02002318 obj_req->write_state = RBD_OBJ_WRITE_START;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002319 return 0;
2320}
2321
Ilya Dryomov6484cbe2019-01-29 12:46:25 +01002322static u16 truncate_or_zero_opcode(struct rbd_obj_request *obj_req)
2323{
2324 return rbd_obj_is_tail(obj_req) ? CEPH_OSD_OP_TRUNCATE :
2325 CEPH_OSD_OP_ZERO;
2326}
2327
Ilya Dryomov27bbd912019-05-29 17:31:37 +02002328static void __rbd_osd_setup_discard_ops(struct ceph_osd_request *osd_req,
2329 int which)
2330{
2331 struct rbd_obj_request *obj_req = osd_req->r_priv;
2332
2333 if (rbd_obj_is_entire(obj_req) && !obj_req->num_img_extents) {
2334 rbd_assert(obj_req->flags & RBD_OBJ_FLAG_DELETION);
2335 osd_req_op_init(osd_req, which, CEPH_OSD_OP_DELETE, 0);
2336 } else {
2337 osd_req_op_extent_init(osd_req, which,
2338 truncate_or_zero_opcode(obj_req),
2339 obj_req->ex.oe_off, obj_req->ex.oe_len,
2340 0, 0);
2341 }
2342}
2343
Ilya Dryomovea9b7432019-05-31 15:11:26 +02002344static int rbd_obj_init_discard(struct rbd_obj_request *obj_req)
Ilya Dryomov6484cbe2019-01-29 12:46:25 +01002345{
Ilya Dryomov0c93e1b2019-01-30 15:14:48 +01002346 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
Ilya Dryomov27bbd912019-05-29 17:31:37 +02002347 u64 off, next_off;
Ilya Dryomov6484cbe2019-01-29 12:46:25 +01002348 int ret;
2349
Ilya Dryomov0c93e1b2019-01-30 15:14:48 +01002350 /*
2351 * Align the range to alloc_size boundary and punt on discards
2352 * that are too small to free up any space.
2353 *
2354 * alloc_size == object_size && is_tail() is a special case for
2355 * filestore with filestore_punch_hole = false, needed to allow
2356 * truncate (in addition to delete).
2357 */
2358 if (rbd_dev->opts->alloc_size != rbd_dev->layout.object_size ||
2359 !rbd_obj_is_tail(obj_req)) {
Ilya Dryomov27bbd912019-05-29 17:31:37 +02002360 off = round_up(obj_req->ex.oe_off, rbd_dev->opts->alloc_size);
2361 next_off = round_down(obj_req->ex.oe_off + obj_req->ex.oe_len,
2362 rbd_dev->opts->alloc_size);
Ilya Dryomov0c93e1b2019-01-30 15:14:48 +01002363 if (off >= next_off)
2364 return 1;
Ilya Dryomov27bbd912019-05-29 17:31:37 +02002365
2366 dout("%s %p %llu~%llu -> %llu~%llu\n", __func__,
2367 obj_req, obj_req->ex.oe_off, obj_req->ex.oe_len,
2368 off, next_off - off);
2369 obj_req->ex.oe_off = off;
2370 obj_req->ex.oe_len = next_off - off;
Ilya Dryomov0c93e1b2019-01-30 15:14:48 +01002371 }
2372
Ilya Dryomov6484cbe2019-01-29 12:46:25 +01002373 /* reverse map the entire object onto the parent */
2374 ret = rbd_obj_calc_img_extents(obj_req, true);
2375 if (ret)
2376 return ret;
2377
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02002378 obj_req->flags |= RBD_OBJ_FLAG_NOOP_FOR_NONEXISTENT;
Ilya Dryomov0ad5d952019-05-14 20:45:38 +02002379 if (rbd_obj_is_entire(obj_req) && !obj_req->num_img_extents)
2380 obj_req->flags |= RBD_OBJ_FLAG_DELETION;
Ilya Dryomov6484cbe2019-01-29 12:46:25 +01002381
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02002382 obj_req->write_state = RBD_OBJ_WRITE_START;
Ilya Dryomov6484cbe2019-01-29 12:46:25 +01002383 return 0;
2384}
2385
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02002386static void __rbd_osd_setup_zeroout_ops(struct ceph_osd_request *osd_req,
2387 int which)
Ilya Dryomov13488d52019-02-25 12:37:50 +01002388{
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02002389 struct rbd_obj_request *obj_req = osd_req->r_priv;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002390 u16 opcode;
2391
2392 if (rbd_obj_is_entire(obj_req)) {
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002393 if (obj_req->num_img_extents) {
Ilya Dryomov0ad5d952019-05-14 20:45:38 +02002394 if (!(obj_req->flags & RBD_OBJ_FLAG_COPYUP_ENABLED))
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02002395 osd_req_op_init(osd_req, which++,
Ilya Dryomov9b17eb22019-02-28 15:51:39 +01002396 CEPH_OSD_OP_CREATE, 0);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002397 opcode = CEPH_OSD_OP_TRUNCATE;
2398 } else {
Ilya Dryomov0ad5d952019-05-14 20:45:38 +02002399 rbd_assert(obj_req->flags & RBD_OBJ_FLAG_DELETION);
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02002400 osd_req_op_init(osd_req, which++,
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002401 CEPH_OSD_OP_DELETE, 0);
2402 opcode = 0;
2403 }
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002404 } else {
Ilya Dryomov6484cbe2019-01-29 12:46:25 +01002405 opcode = truncate_or_zero_opcode(obj_req);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002406 }
2407
2408 if (opcode)
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02002409 osd_req_op_extent_init(osd_req, which, opcode,
Ilya Dryomov43df3d32018-02-02 15:23:22 +01002410 obj_req->ex.oe_off, obj_req->ex.oe_len,
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002411 0, 0);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002412}
2413
Ilya Dryomovea9b7432019-05-31 15:11:26 +02002414static int rbd_obj_init_zeroout(struct rbd_obj_request *obj_req)
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002415{
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002416 int ret;
2417
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002418 /* reverse map the entire object onto the parent */
2419 ret = rbd_obj_calc_img_extents(obj_req, true);
2420 if (ret)
2421 return ret;
2422
Ilya Dryomov0ad5d952019-05-14 20:45:38 +02002423 if (rbd_obj_copyup_enabled(obj_req))
2424 obj_req->flags |= RBD_OBJ_FLAG_COPYUP_ENABLED;
2425 if (!obj_req->num_img_extents) {
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02002426 obj_req->flags |= RBD_OBJ_FLAG_NOOP_FOR_NONEXISTENT;
Ilya Dryomov0ad5d952019-05-14 20:45:38 +02002427 if (rbd_obj_is_entire(obj_req))
2428 obj_req->flags |= RBD_OBJ_FLAG_DELETION;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002429 }
2430
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02002431 obj_req->write_state = RBD_OBJ_WRITE_START;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002432 return 0;
2433}
2434
Ilya Dryomova086a1b2019-06-12 18:33:31 +02002435static int count_write_ops(struct rbd_obj_request *obj_req)
2436{
Ilya Dryomov8b5bec52019-06-19 15:45:27 +02002437 struct rbd_img_request *img_req = obj_req->img_request;
2438
2439 switch (img_req->op_type) {
Ilya Dryomova086a1b2019-06-12 18:33:31 +02002440 case OBJ_OP_WRITE:
Ilya Dryomov8b5bec52019-06-19 15:45:27 +02002441 if (!use_object_map(img_req->rbd_dev) ||
2442 !(obj_req->flags & RBD_OBJ_FLAG_MAY_EXIST))
2443 return 2; /* setallochint + write/writefull */
2444
2445 return 1; /* write/writefull */
Ilya Dryomova086a1b2019-06-12 18:33:31 +02002446 case OBJ_OP_DISCARD:
2447 return 1; /* delete/truncate/zero */
2448 case OBJ_OP_ZEROOUT:
2449 if (rbd_obj_is_entire(obj_req) && obj_req->num_img_extents &&
2450 !(obj_req->flags & RBD_OBJ_FLAG_COPYUP_ENABLED))
2451 return 2; /* create + truncate */
2452
2453 return 1; /* delete/truncate/zero */
2454 default:
2455 BUG();
2456 }
2457}
2458
2459static void rbd_osd_setup_write_ops(struct ceph_osd_request *osd_req,
2460 int which)
2461{
2462 struct rbd_obj_request *obj_req = osd_req->r_priv;
2463
2464 switch (obj_req->img_request->op_type) {
2465 case OBJ_OP_WRITE:
2466 __rbd_osd_setup_write_ops(osd_req, which);
2467 break;
2468 case OBJ_OP_DISCARD:
2469 __rbd_osd_setup_discard_ops(osd_req, which);
2470 break;
2471 case OBJ_OP_ZEROOUT:
2472 __rbd_osd_setup_zeroout_ops(osd_req, which);
2473 break;
2474 default:
2475 BUG();
2476 }
2477}
2478
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002479/*
Ilya Dryomova086a1b2019-06-12 18:33:31 +02002480 * Prune the list of object requests (adjust offset and/or length, drop
2481 * redundant requests). Prepare object request state machines and image
2482 * request state machine for execution.
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002483 */
2484static int __rbd_img_fill_request(struct rbd_img_request *img_req)
2485{
Ilya Dryomov0c93e1b2019-01-30 15:14:48 +01002486 struct rbd_obj_request *obj_req, *next_obj_req;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002487 int ret;
2488
Ilya Dryomov0c93e1b2019-01-30 15:14:48 +01002489 for_each_obj_request_safe(img_req, obj_req, next_obj_req) {
Ilya Dryomov9bb02482018-01-30 17:52:10 +01002490 switch (img_req->op_type) {
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002491 case OBJ_OP_READ:
Ilya Dryomovea9b7432019-05-31 15:11:26 +02002492 ret = rbd_obj_init_read(obj_req);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002493 break;
2494 case OBJ_OP_WRITE:
Ilya Dryomovea9b7432019-05-31 15:11:26 +02002495 ret = rbd_obj_init_write(obj_req);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002496 break;
2497 case OBJ_OP_DISCARD:
Ilya Dryomovea9b7432019-05-31 15:11:26 +02002498 ret = rbd_obj_init_discard(obj_req);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002499 break;
Ilya Dryomov6484cbe2019-01-29 12:46:25 +01002500 case OBJ_OP_ZEROOUT:
Ilya Dryomovea9b7432019-05-31 15:11:26 +02002501 ret = rbd_obj_init_zeroout(obj_req);
Ilya Dryomov6484cbe2019-01-29 12:46:25 +01002502 break;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002503 default:
Arnd Bergmann16809372019-03-22 17:53:56 +01002504 BUG();
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002505 }
Ilya Dryomov0c93e1b2019-01-30 15:14:48 +01002506 if (ret < 0)
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002507 return ret;
Ilya Dryomov0c93e1b2019-01-30 15:14:48 +01002508 if (ret > 0) {
Ilya Dryomov0c93e1b2019-01-30 15:14:48 +01002509 rbd_img_obj_request_del(img_req, obj_req);
2510 continue;
2511 }
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002512 }
2513
Ilya Dryomov0192ce22019-05-16 15:06:56 +02002514 img_req->state = RBD_IMG_START;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002515 return 0;
2516}
2517
Ilya Dryomov5a237812018-02-06 19:26:34 +01002518union rbd_img_fill_iter {
2519 struct ceph_bio_iter bio_iter;
2520 struct ceph_bvec_iter bvec_iter;
2521};
2522
2523struct rbd_img_fill_ctx {
2524 enum obj_request_type pos_type;
2525 union rbd_img_fill_iter *pos;
2526 union rbd_img_fill_iter iter;
2527 ceph_object_extent_fn_t set_pos_fn;
Ilya Dryomovafb97882018-02-06 19:26:35 +01002528 ceph_object_extent_fn_t count_fn;
2529 ceph_object_extent_fn_t copy_fn;
Ilya Dryomov5a237812018-02-06 19:26:34 +01002530};
2531
2532static struct ceph_object_extent *alloc_object_extent(void *arg)
2533{
2534 struct rbd_img_request *img_req = arg;
2535 struct rbd_obj_request *obj_req;
2536
2537 obj_req = rbd_obj_request_create();
2538 if (!obj_req)
2539 return NULL;
2540
2541 rbd_img_obj_request_add(img_req, obj_req);
2542 return &obj_req->ex;
2543}
2544
2545/*
Ilya Dryomovafb97882018-02-06 19:26:35 +01002546 * While su != os && sc == 1 is technically not fancy (it's the same
2547 * layout as su == os && sc == 1), we can't use the nocopy path for it
2548 * because ->set_pos_fn() should be called only once per object.
2549 * ceph_file_to_extents() invokes action_fn once per stripe unit, so
2550 * treat su != os && sc == 1 as fancy.
Ilya Dryomov5a237812018-02-06 19:26:34 +01002551 */
Ilya Dryomovafb97882018-02-06 19:26:35 +01002552static bool rbd_layout_is_fancy(struct ceph_file_layout *l)
2553{
2554 return l->stripe_unit != l->object_size;
2555}
2556
2557static int rbd_img_fill_request_nocopy(struct rbd_img_request *img_req,
2558 struct ceph_file_extent *img_extents,
2559 u32 num_img_extents,
2560 struct rbd_img_fill_ctx *fctx)
Ilya Dryomov5a237812018-02-06 19:26:34 +01002561{
2562 u32 i;
2563 int ret;
2564
2565 img_req->data_type = fctx->pos_type;
2566
2567 /*
2568 * Create object requests and set each object request's starting
2569 * position in the provided bio (list) or bio_vec array.
2570 */
2571 fctx->iter = *fctx->pos;
2572 for (i = 0; i < num_img_extents; i++) {
2573 ret = ceph_file_to_extents(&img_req->rbd_dev->layout,
2574 img_extents[i].fe_off,
2575 img_extents[i].fe_len,
2576 &img_req->object_extents,
2577 alloc_object_extent, img_req,
2578 fctx->set_pos_fn, &fctx->iter);
2579 if (ret)
2580 return ret;
2581 }
2582
2583 return __rbd_img_fill_request(img_req);
2584}
2585
Ilya Dryomovafb97882018-02-06 19:26:35 +01002586/*
2587 * Map a list of image extents to a list of object extents, create the
2588 * corresponding object requests (normally each to a different object,
2589 * but not always) and add them to @img_req. For each object request,
2590 * set up its data descriptor to point to the corresponding chunk(s) of
2591 * @fctx->pos data buffer.
2592 *
2593 * Because ceph_file_to_extents() will merge adjacent object extents
2594 * together, each object request's data descriptor may point to multiple
2595 * different chunks of @fctx->pos data buffer.
2596 *
2597 * @fctx->pos data buffer is assumed to be large enough.
2598 */
2599static int rbd_img_fill_request(struct rbd_img_request *img_req,
2600 struct ceph_file_extent *img_extents,
2601 u32 num_img_extents,
2602 struct rbd_img_fill_ctx *fctx)
2603{
2604 struct rbd_device *rbd_dev = img_req->rbd_dev;
2605 struct rbd_obj_request *obj_req;
2606 u32 i;
2607 int ret;
2608
2609 if (fctx->pos_type == OBJ_REQUEST_NODATA ||
2610 !rbd_layout_is_fancy(&rbd_dev->layout))
2611 return rbd_img_fill_request_nocopy(img_req, img_extents,
2612 num_img_extents, fctx);
2613
2614 img_req->data_type = OBJ_REQUEST_OWN_BVECS;
2615
2616 /*
2617 * Create object requests and determine ->bvec_count for each object
2618 * request. Note that ->bvec_count sum over all object requests may
2619 * be greater than the number of bio_vecs in the provided bio (list)
2620 * or bio_vec array because when mapped, those bio_vecs can straddle
2621 * stripe unit boundaries.
2622 */
2623 fctx->iter = *fctx->pos;
2624 for (i = 0; i < num_img_extents; i++) {
2625 ret = ceph_file_to_extents(&rbd_dev->layout,
2626 img_extents[i].fe_off,
2627 img_extents[i].fe_len,
2628 &img_req->object_extents,
2629 alloc_object_extent, img_req,
2630 fctx->count_fn, &fctx->iter);
2631 if (ret)
2632 return ret;
2633 }
2634
2635 for_each_obj_request(img_req, obj_req) {
2636 obj_req->bvec_pos.bvecs = kmalloc_array(obj_req->bvec_count,
2637 sizeof(*obj_req->bvec_pos.bvecs),
2638 GFP_NOIO);
2639 if (!obj_req->bvec_pos.bvecs)
2640 return -ENOMEM;
Alex Elderb454e362013-04-19 15:34:50 -05002641 }
2642
2643 /*
Ilya Dryomovafb97882018-02-06 19:26:35 +01002644 * Fill in each object request's private bio_vec array, splitting and
2645 * rearranging the provided bio_vecs in stripe unit chunks as needed.
Alex Elderb454e362013-04-19 15:34:50 -05002646 */
Ilya Dryomovafb97882018-02-06 19:26:35 +01002647 fctx->iter = *fctx->pos;
2648 for (i = 0; i < num_img_extents; i++) {
2649 ret = ceph_iterate_extents(&rbd_dev->layout,
2650 img_extents[i].fe_off,
2651 img_extents[i].fe_len,
2652 &img_req->object_extents,
2653 fctx->copy_fn, &fctx->iter);
2654 if (ret)
2655 return ret;
2656 }
Alex Elder3d7efd12013-04-19 15:34:50 -05002657
Ilya Dryomovafb97882018-02-06 19:26:35 +01002658 return __rbd_img_fill_request(img_req);
Alex Elderb454e362013-04-19 15:34:50 -05002659}
2660
Ilya Dryomov5a237812018-02-06 19:26:34 +01002661static int rbd_img_fill_nodata(struct rbd_img_request *img_req,
2662 u64 off, u64 len)
2663{
2664 struct ceph_file_extent ex = { off, len };
2665 union rbd_img_fill_iter dummy;
2666 struct rbd_img_fill_ctx fctx = {
2667 .pos_type = OBJ_REQUEST_NODATA,
2668 .pos = &dummy,
2669 };
2670
2671 return rbd_img_fill_request(img_req, &ex, 1, &fctx);
2672}
2673
2674static void set_bio_pos(struct ceph_object_extent *ex, u32 bytes, void *arg)
2675{
2676 struct rbd_obj_request *obj_req =
2677 container_of(ex, struct rbd_obj_request, ex);
2678 struct ceph_bio_iter *it = arg;
2679
2680 dout("%s objno %llu bytes %u\n", __func__, ex->oe_objno, bytes);
2681 obj_req->bio_pos = *it;
2682 ceph_bio_iter_advance(it, bytes);
2683}
2684
Ilya Dryomovafb97882018-02-06 19:26:35 +01002685static void count_bio_bvecs(struct ceph_object_extent *ex, u32 bytes, void *arg)
2686{
2687 struct rbd_obj_request *obj_req =
2688 container_of(ex, struct rbd_obj_request, ex);
2689 struct ceph_bio_iter *it = arg;
2690
2691 dout("%s objno %llu bytes %u\n", __func__, ex->oe_objno, bytes);
2692 ceph_bio_iter_advance_step(it, bytes, ({
2693 obj_req->bvec_count++;
2694 }));
2695
2696}
2697
2698static void copy_bio_bvecs(struct ceph_object_extent *ex, u32 bytes, void *arg)
2699{
2700 struct rbd_obj_request *obj_req =
2701 container_of(ex, struct rbd_obj_request, ex);
2702 struct ceph_bio_iter *it = arg;
2703
2704 dout("%s objno %llu bytes %u\n", __func__, ex->oe_objno, bytes);
2705 ceph_bio_iter_advance_step(it, bytes, ({
2706 obj_req->bvec_pos.bvecs[obj_req->bvec_idx++] = bv;
2707 obj_req->bvec_pos.iter.bi_size += bv.bv_len;
2708 }));
2709}
2710
Ilya Dryomov5a237812018-02-06 19:26:34 +01002711static int __rbd_img_fill_from_bio(struct rbd_img_request *img_req,
2712 struct ceph_file_extent *img_extents,
2713 u32 num_img_extents,
2714 struct ceph_bio_iter *bio_pos)
2715{
2716 struct rbd_img_fill_ctx fctx = {
2717 .pos_type = OBJ_REQUEST_BIO,
2718 .pos = (union rbd_img_fill_iter *)bio_pos,
2719 .set_pos_fn = set_bio_pos,
Ilya Dryomovafb97882018-02-06 19:26:35 +01002720 .count_fn = count_bio_bvecs,
2721 .copy_fn = copy_bio_bvecs,
Ilya Dryomov5a237812018-02-06 19:26:34 +01002722 };
2723
2724 return rbd_img_fill_request(img_req, img_extents, num_img_extents,
2725 &fctx);
2726}
2727
2728static int rbd_img_fill_from_bio(struct rbd_img_request *img_req,
2729 u64 off, u64 len, struct bio *bio)
2730{
2731 struct ceph_file_extent ex = { off, len };
2732 struct ceph_bio_iter it = { .bio = bio, .iter = bio->bi_iter };
2733
2734 return __rbd_img_fill_from_bio(img_req, &ex, 1, &it);
2735}
2736
2737static void set_bvec_pos(struct ceph_object_extent *ex, u32 bytes, void *arg)
2738{
2739 struct rbd_obj_request *obj_req =
2740 container_of(ex, struct rbd_obj_request, ex);
2741 struct ceph_bvec_iter *it = arg;
2742
2743 obj_req->bvec_pos = *it;
2744 ceph_bvec_iter_shorten(&obj_req->bvec_pos, bytes);
2745 ceph_bvec_iter_advance(it, bytes);
2746}
2747
Ilya Dryomovafb97882018-02-06 19:26:35 +01002748static void count_bvecs(struct ceph_object_extent *ex, u32 bytes, void *arg)
2749{
2750 struct rbd_obj_request *obj_req =
2751 container_of(ex, struct rbd_obj_request, ex);
2752 struct ceph_bvec_iter *it = arg;
2753
2754 ceph_bvec_iter_advance_step(it, bytes, ({
2755 obj_req->bvec_count++;
2756 }));
2757}
2758
2759static void copy_bvecs(struct ceph_object_extent *ex, u32 bytes, void *arg)
2760{
2761 struct rbd_obj_request *obj_req =
2762 container_of(ex, struct rbd_obj_request, ex);
2763 struct ceph_bvec_iter *it = arg;
2764
2765 ceph_bvec_iter_advance_step(it, bytes, ({
2766 obj_req->bvec_pos.bvecs[obj_req->bvec_idx++] = bv;
2767 obj_req->bvec_pos.iter.bi_size += bv.bv_len;
2768 }));
2769}
2770
Ilya Dryomov5a237812018-02-06 19:26:34 +01002771static int __rbd_img_fill_from_bvecs(struct rbd_img_request *img_req,
2772 struct ceph_file_extent *img_extents,
2773 u32 num_img_extents,
2774 struct ceph_bvec_iter *bvec_pos)
2775{
2776 struct rbd_img_fill_ctx fctx = {
2777 .pos_type = OBJ_REQUEST_BVECS,
2778 .pos = (union rbd_img_fill_iter *)bvec_pos,
2779 .set_pos_fn = set_bvec_pos,
Ilya Dryomovafb97882018-02-06 19:26:35 +01002780 .count_fn = count_bvecs,
2781 .copy_fn = copy_bvecs,
Ilya Dryomov5a237812018-02-06 19:26:34 +01002782 };
2783
2784 return rbd_img_fill_request(img_req, img_extents, num_img_extents,
2785 &fctx);
2786}
2787
2788static int rbd_img_fill_from_bvecs(struct rbd_img_request *img_req,
2789 struct ceph_file_extent *img_extents,
2790 u32 num_img_extents,
2791 struct bio_vec *bvecs)
2792{
2793 struct ceph_bvec_iter it = {
2794 .bvecs = bvecs,
2795 .iter = { .bi_size = ceph_file_extents_bytes(img_extents,
2796 num_img_extents) },
2797 };
2798
2799 return __rbd_img_fill_from_bvecs(img_req, img_extents, num_img_extents,
2800 &it);
2801}
2802
Ilya Dryomov0192ce22019-05-16 15:06:56 +02002803static void rbd_img_handle_request_work(struct work_struct *work)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002804{
Ilya Dryomov0192ce22019-05-16 15:06:56 +02002805 struct rbd_img_request *img_req =
2806 container_of(work, struct rbd_img_request, work);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002807
Ilya Dryomov0192ce22019-05-16 15:06:56 +02002808 rbd_img_handle_request(img_req, img_req->work_result);
2809}
Alex Elderbf0d5f502012-11-22 00:00:08 -06002810
Ilya Dryomov0192ce22019-05-16 15:06:56 +02002811static void rbd_img_schedule(struct rbd_img_request *img_req, int result)
2812{
2813 INIT_WORK(&img_req->work, rbd_img_handle_request_work);
2814 img_req->work_result = result;
2815 queue_work(rbd_wq, &img_req->work);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002816}
2817
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02002818static bool rbd_obj_may_exist(struct rbd_obj_request *obj_req)
2819{
2820 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
2821
2822 if (rbd_object_map_may_exist(rbd_dev, obj_req->ex.oe_objno)) {
2823 obj_req->flags |= RBD_OBJ_FLAG_MAY_EXIST;
2824 return true;
2825 }
2826
2827 dout("%s %p objno %llu assuming dne\n", __func__, obj_req,
2828 obj_req->ex.oe_objno);
2829 return false;
2830}
2831
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02002832static int rbd_obj_read_object(struct rbd_obj_request *obj_req)
2833{
Ilya Dryomova086a1b2019-06-12 18:33:31 +02002834 struct ceph_osd_request *osd_req;
2835 int ret;
2836
2837 osd_req = __rbd_obj_add_osd_request(obj_req, NULL, 1);
2838 if (IS_ERR(osd_req))
2839 return PTR_ERR(osd_req);
2840
2841 osd_req_op_extent_init(osd_req, 0, CEPH_OSD_OP_READ,
2842 obj_req->ex.oe_off, obj_req->ex.oe_len, 0, 0);
2843 rbd_osd_setup_data(osd_req, 0);
2844 rbd_osd_format_read(osd_req);
2845
2846 ret = ceph_osdc_alloc_messages(osd_req, GFP_NOIO);
2847 if (ret)
2848 return ret;
2849
2850 rbd_osd_submit(osd_req);
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02002851 return 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002852}
2853
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002854static int rbd_obj_read_from_parent(struct rbd_obj_request *obj_req)
Alex Elder8b3e1a52013-01-24 16:13:36 -06002855{
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002856 struct rbd_img_request *img_req = obj_req->img_request;
2857 struct rbd_img_request *child_img_req;
2858 int ret;
Alex Elder8b3e1a52013-01-24 16:13:36 -06002859
Ilya Dryomove93aca02018-02-06 19:26:35 +01002860 child_img_req = rbd_img_request_create(img_req->rbd_dev->parent,
2861 OBJ_OP_READ, NULL);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002862 if (!child_img_req)
2863 return -ENOMEM;
Alex Elder8b3e1a52013-01-24 16:13:36 -06002864
Ilya Dryomove93aca02018-02-06 19:26:35 +01002865 __set_bit(IMG_REQ_CHILD, &child_img_req->flags);
2866 child_img_req->obj_request = obj_req;
Alex Elder02c74fb2013-05-06 17:40:33 -05002867
Ilya Dryomov21ed05a2019-08-30 17:31:06 +02002868 dout("%s child_img_req %p for obj_req %p\n", __func__, child_img_req,
2869 obj_req);
2870
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002871 if (!rbd_img_is_write(img_req)) {
Ilya Dryomovecc633c2018-02-01 11:50:47 +01002872 switch (img_req->data_type) {
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002873 case OBJ_REQUEST_BIO:
Ilya Dryomov5a237812018-02-06 19:26:34 +01002874 ret = __rbd_img_fill_from_bio(child_img_req,
2875 obj_req->img_extents,
2876 obj_req->num_img_extents,
2877 &obj_req->bio_pos);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002878 break;
2879 case OBJ_REQUEST_BVECS:
Ilya Dryomovafb97882018-02-06 19:26:35 +01002880 case OBJ_REQUEST_OWN_BVECS:
Ilya Dryomov5a237812018-02-06 19:26:34 +01002881 ret = __rbd_img_fill_from_bvecs(child_img_req,
2882 obj_req->img_extents,
2883 obj_req->num_img_extents,
2884 &obj_req->bvec_pos);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002885 break;
2886 default:
Arnd Bergmannd342a152019-03-22 15:36:37 +01002887 BUG();
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002888 }
2889 } else {
Ilya Dryomov5a237812018-02-06 19:26:34 +01002890 ret = rbd_img_fill_from_bvecs(child_img_req,
2891 obj_req->img_extents,
2892 obj_req->num_img_extents,
2893 obj_req->copyup_bvecs);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002894 }
2895 if (ret) {
2896 rbd_img_request_put(child_img_req);
2897 return ret;
2898 }
2899
Ilya Dryomov0192ce22019-05-16 15:06:56 +02002900 /* avoid parent chain recursion */
2901 rbd_img_schedule(child_img_req, 0);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002902 return 0;
2903}
2904
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02002905static bool rbd_obj_advance_read(struct rbd_obj_request *obj_req, int *result)
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002906{
2907 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
2908 int ret;
2909
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02002910again:
Ilya Dryomova9b67e62019-05-08 13:35:57 +02002911 switch (obj_req->read_state) {
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02002912 case RBD_OBJ_READ_START:
2913 rbd_assert(!*result);
2914
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02002915 if (!rbd_obj_may_exist(obj_req)) {
2916 *result = -ENOENT;
2917 obj_req->read_state = RBD_OBJ_READ_OBJECT;
2918 goto again;
2919 }
2920
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02002921 ret = rbd_obj_read_object(obj_req);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002922 if (ret) {
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02002923 *result = ret;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002924 return true;
2925 }
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02002926 obj_req->read_state = RBD_OBJ_READ_OBJECT;
2927 return false;
Ilya Dryomova9b67e62019-05-08 13:35:57 +02002928 case RBD_OBJ_READ_OBJECT:
2929 if (*result == -ENOENT && rbd_dev->parent_overlap) {
2930 /* reverse map this object extent onto the parent */
2931 ret = rbd_obj_calc_img_extents(obj_req, false);
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002932 if (ret) {
Ilya Dryomov54ab3b22019-05-11 16:21:49 +02002933 *result = ret;
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002934 return true;
2935 }
Ilya Dryomova9b67e62019-05-08 13:35:57 +02002936 if (obj_req->num_img_extents) {
2937 ret = rbd_obj_read_from_parent(obj_req);
2938 if (ret) {
2939 *result = ret;
2940 return true;
2941 }
2942 obj_req->read_state = RBD_OBJ_READ_PARENT;
2943 return false;
2944 }
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002945 }
Alex Elder02c74fb2013-05-06 17:40:33 -05002946
Ilya Dryomova9b67e62019-05-08 13:35:57 +02002947 /*
2948 * -ENOENT means a hole in the image -- zero-fill the entire
2949 * length of the request. A short read also implies zero-fill
2950 * to the end of the request.
2951 */
2952 if (*result == -ENOENT) {
2953 rbd_obj_zero_range(obj_req, 0, obj_req->ex.oe_len);
2954 *result = 0;
2955 } else if (*result >= 0) {
2956 if (*result < obj_req->ex.oe_len)
2957 rbd_obj_zero_range(obj_req, *result,
2958 obj_req->ex.oe_len - *result);
2959 else
2960 rbd_assert(*result == obj_req->ex.oe_len);
2961 *result = 0;
2962 }
2963 return true;
2964 case RBD_OBJ_READ_PARENT:
Ilya Dryomovd435c9a2019-08-27 16:45:10 +02002965 /*
2966 * The parent image is read only up to the overlap -- zero-fill
2967 * from the overlap to the end of the request.
2968 */
2969 if (!*result) {
2970 u32 obj_overlap = rbd_obj_img_extents_bytes(obj_req);
2971
2972 if (obj_overlap < obj_req->ex.oe_len)
2973 rbd_obj_zero_range(obj_req, obj_overlap,
2974 obj_req->ex.oe_len - obj_overlap);
2975 }
Ilya Dryomova9b67e62019-05-08 13:35:57 +02002976 return true;
2977 default:
2978 BUG();
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002979 }
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002980}
2981
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02002982static bool rbd_obj_write_is_noop(struct rbd_obj_request *obj_req)
2983{
2984 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
2985
2986 if (rbd_object_map_may_exist(rbd_dev, obj_req->ex.oe_objno))
2987 obj_req->flags |= RBD_OBJ_FLAG_MAY_EXIST;
2988
2989 if (!(obj_req->flags & RBD_OBJ_FLAG_MAY_EXIST) &&
2990 (obj_req->flags & RBD_OBJ_FLAG_NOOP_FOR_NONEXISTENT)) {
2991 dout("%s %p noop for nonexistent\n", __func__, obj_req);
2992 return true;
Alex Elder02c74fb2013-05-06 17:40:33 -05002993 }
2994
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02002995 return false;
2996}
2997
2998/*
2999 * Return:
3000 * 0 - object map update sent
3001 * 1 - object map update isn't needed
3002 * <0 - error
3003 */
3004static int rbd_obj_write_pre_object_map(struct rbd_obj_request *obj_req)
3005{
3006 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
3007 u8 new_state;
3008
3009 if (!(rbd_dev->header.features & RBD_FEATURE_OBJECT_MAP))
3010 return 1;
3011
3012 if (obj_req->flags & RBD_OBJ_FLAG_DELETION)
3013 new_state = OBJECT_PENDING;
3014 else
3015 new_state = OBJECT_EXISTS;
3016
3017 return rbd_object_map_update(obj_req, CEPH_NOSNAP, new_state, NULL);
3018}
3019
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02003020static int rbd_obj_write_object(struct rbd_obj_request *obj_req)
3021{
Ilya Dryomova086a1b2019-06-12 18:33:31 +02003022 struct ceph_osd_request *osd_req;
3023 int num_ops = count_write_ops(obj_req);
3024 int which = 0;
3025 int ret;
3026
3027 if (obj_req->flags & RBD_OBJ_FLAG_COPYUP_ENABLED)
3028 num_ops++; /* stat */
3029
3030 osd_req = rbd_obj_add_osd_request(obj_req, num_ops);
3031 if (IS_ERR(osd_req))
3032 return PTR_ERR(osd_req);
3033
3034 if (obj_req->flags & RBD_OBJ_FLAG_COPYUP_ENABLED) {
3035 ret = rbd_osd_setup_stat(osd_req, which++);
3036 if (ret)
3037 return ret;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003038 }
3039
Ilya Dryomova086a1b2019-06-12 18:33:31 +02003040 rbd_osd_setup_write_ops(osd_req, which);
3041 rbd_osd_format_write(osd_req);
3042
3043 ret = ceph_osdc_alloc_messages(osd_req, GFP_NOIO);
3044 if (ret)
3045 return ret;
3046
3047 rbd_osd_submit(osd_req);
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02003048 return 0;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003049}
3050
3051/*
3052 * copyup_bvecs pages are never highmem pages
3053 */
3054static bool is_zero_bvecs(struct bio_vec *bvecs, u32 bytes)
3055{
3056 struct ceph_bvec_iter it = {
3057 .bvecs = bvecs,
3058 .iter = { .bi_size = bytes },
3059 };
3060
3061 ceph_bvec_iter_advance_step(&it, bytes, ({
3062 if (memchr_inv(page_address(bv.bv_page) + bv.bv_offset, 0,
3063 bv.bv_len))
3064 return false;
3065 }));
3066 return true;
3067}
3068
Ilya Dryomov3a482502019-02-28 10:49:12 +01003069#define MODS_ONLY U32_MAX
3070
Ilya Dryomov793333a302019-06-13 17:44:08 +02003071static int rbd_obj_copyup_empty_snapc(struct rbd_obj_request *obj_req,
3072 u32 bytes)
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003073{
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02003074 struct ceph_osd_request *osd_req;
Chengguang Xufe943d52018-04-12 12:04:55 +08003075 int ret;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003076
3077 dout("%s obj_req %p bytes %u\n", __func__, obj_req, bytes);
Ilya Dryomov89a59c12019-02-28 14:20:28 +01003078 rbd_assert(bytes > 0 && bytes != MODS_ONLY);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003079
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02003080 osd_req = __rbd_obj_add_osd_request(obj_req, &rbd_empty_snapc, 1);
3081 if (IS_ERR(osd_req))
3082 return PTR_ERR(osd_req);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003083
Ilya Dryomovb5ae8cb2019-05-29 16:53:14 +02003084 ret = rbd_osd_setup_copyup(osd_req, 0, bytes);
Chengguang Xufe943d52018-04-12 12:04:55 +08003085 if (ret)
3086 return ret;
3087
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02003088 rbd_osd_format_write(osd_req);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003089
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02003090 ret = ceph_osdc_alloc_messages(osd_req, GFP_NOIO);
Ilya Dryomov89a59c12019-02-28 14:20:28 +01003091 if (ret)
3092 return ret;
3093
Ilya Dryomova086a1b2019-06-12 18:33:31 +02003094 rbd_osd_submit(osd_req);
Ilya Dryomov89a59c12019-02-28 14:20:28 +01003095 return 0;
3096}
3097
Ilya Dryomov793333a302019-06-13 17:44:08 +02003098static int rbd_obj_copyup_current_snapc(struct rbd_obj_request *obj_req,
3099 u32 bytes)
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003100{
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02003101 struct ceph_osd_request *osd_req;
Ilya Dryomova086a1b2019-06-12 18:33:31 +02003102 int num_ops = count_write_ops(obj_req);
3103 int which = 0;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003104 int ret;
3105
3106 dout("%s obj_req %p bytes %u\n", __func__, obj_req, bytes);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003107
Ilya Dryomova086a1b2019-06-12 18:33:31 +02003108 if (bytes != MODS_ONLY)
3109 num_ops++; /* copyup */
Ilya Dryomov13488d52019-02-25 12:37:50 +01003110
Ilya Dryomova086a1b2019-06-12 18:33:31 +02003111 osd_req = rbd_obj_add_osd_request(obj_req, num_ops);
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02003112 if (IS_ERR(osd_req))
3113 return PTR_ERR(osd_req);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003114
Ilya Dryomov3a482502019-02-28 10:49:12 +01003115 if (bytes != MODS_ONLY) {
Ilya Dryomovb5ae8cb2019-05-29 16:53:14 +02003116 ret = rbd_osd_setup_copyup(osd_req, which++, bytes);
Ilya Dryomov3a482502019-02-28 10:49:12 +01003117 if (ret)
3118 return ret;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003119 }
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003120
Ilya Dryomova086a1b2019-06-12 18:33:31 +02003121 rbd_osd_setup_write_ops(osd_req, which);
3122 rbd_osd_format_write(osd_req);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003123
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02003124 ret = ceph_osdc_alloc_messages(osd_req, GFP_NOIO);
Ilya Dryomov26f887e2018-10-15 16:11:37 +02003125 if (ret)
3126 return ret;
3127
Ilya Dryomova086a1b2019-06-12 18:33:31 +02003128 rbd_osd_submit(osd_req);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003129 return 0;
3130}
3131
Ilya Dryomov7e07efb2018-01-20 10:30:11 +01003132static int setup_copyup_bvecs(struct rbd_obj_request *obj_req, u64 obj_overlap)
3133{
3134 u32 i;
3135
3136 rbd_assert(!obj_req->copyup_bvecs);
3137 obj_req->copyup_bvec_count = calc_pages_for(0, obj_overlap);
3138 obj_req->copyup_bvecs = kcalloc(obj_req->copyup_bvec_count,
3139 sizeof(*obj_req->copyup_bvecs),
3140 GFP_NOIO);
3141 if (!obj_req->copyup_bvecs)
3142 return -ENOMEM;
3143
3144 for (i = 0; i < obj_req->copyup_bvec_count; i++) {
3145 unsigned int len = min(obj_overlap, (u64)PAGE_SIZE);
3146
3147 obj_req->copyup_bvecs[i].bv_page = alloc_page(GFP_NOIO);
3148 if (!obj_req->copyup_bvecs[i].bv_page)
3149 return -ENOMEM;
3150
3151 obj_req->copyup_bvecs[i].bv_offset = 0;
3152 obj_req->copyup_bvecs[i].bv_len = len;
3153 obj_overlap -= len;
3154 }
3155
3156 rbd_assert(!obj_overlap);
3157 return 0;
3158}
3159
Ilya Dryomov0ad5d952019-05-14 20:45:38 +02003160/*
3161 * The target object doesn't exist. Read the data for the entire
3162 * target object up to the overlap point (if any) from the parent,
3163 * so we can use it for a copyup.
3164 */
Ilya Dryomov793333a302019-06-13 17:44:08 +02003165static int rbd_obj_copyup_read_parent(struct rbd_obj_request *obj_req)
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003166{
3167 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003168 int ret;
3169
Ilya Dryomov86bd7992018-02-06 19:26:33 +01003170 rbd_assert(obj_req->num_img_extents);
3171 prune_extents(obj_req->img_extents, &obj_req->num_img_extents,
3172 rbd_dev->parent_overlap);
3173 if (!obj_req->num_img_extents) {
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003174 /*
3175 * The overlap has become 0 (most likely because the
Ilya Dryomov3a482502019-02-28 10:49:12 +01003176 * image has been flattened). Re-submit the original write
3177 * request -- pass MODS_ONLY since the copyup isn't needed
3178 * anymore.
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003179 */
Ilya Dryomov793333a302019-06-13 17:44:08 +02003180 return rbd_obj_copyup_current_snapc(obj_req, MODS_ONLY);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003181 }
3182
Ilya Dryomov86bd7992018-02-06 19:26:33 +01003183 ret = setup_copyup_bvecs(obj_req, rbd_obj_img_extents_bytes(obj_req));
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003184 if (ret)
3185 return ret;
3186
Ilya Dryomov86bd7992018-02-06 19:26:33 +01003187 return rbd_obj_read_from_parent(obj_req);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003188}
3189
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02003190static void rbd_obj_copyup_object_maps(struct rbd_obj_request *obj_req)
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003191{
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02003192 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
3193 struct ceph_snap_context *snapc = obj_req->img_request->snapc;
3194 u8 new_state;
3195 u32 i;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003196 int ret;
3197
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02003198 rbd_assert(!obj_req->pending.result && !obj_req->pending.num_pending);
3199
3200 if (!(rbd_dev->header.features & RBD_FEATURE_OBJECT_MAP))
3201 return;
3202
3203 if (obj_req->flags & RBD_OBJ_FLAG_COPYUP_ZEROS)
3204 return;
3205
3206 for (i = 0; i < snapc->num_snaps; i++) {
3207 if ((rbd_dev->header.features & RBD_FEATURE_FAST_DIFF) &&
3208 i + 1 < snapc->num_snaps)
3209 new_state = OBJECT_EXISTS_CLEAN;
3210 else
3211 new_state = OBJECT_EXISTS;
3212
3213 ret = rbd_object_map_update(obj_req, snapc->snaps[i],
3214 new_state, NULL);
3215 if (ret < 0) {
3216 obj_req->pending.result = ret;
3217 return;
3218 }
3219
3220 rbd_assert(!ret);
3221 obj_req->pending.num_pending++;
3222 }
3223}
3224
Ilya Dryomov793333a302019-06-13 17:44:08 +02003225static void rbd_obj_copyup_write_object(struct rbd_obj_request *obj_req)
3226{
3227 u32 bytes = rbd_obj_img_extents_bytes(obj_req);
3228 int ret;
3229
3230 rbd_assert(!obj_req->pending.result && !obj_req->pending.num_pending);
3231
3232 /*
3233 * Only send non-zero copyup data to save some I/O and network
3234 * bandwidth -- zero copyup data is equivalent to the object not
3235 * existing.
3236 */
3237 if (obj_req->flags & RBD_OBJ_FLAG_COPYUP_ZEROS)
3238 bytes = 0;
3239
3240 if (obj_req->img_request->snapc->num_snaps && bytes > 0) {
3241 /*
3242 * Send a copyup request with an empty snapshot context to
3243 * deep-copyup the object through all existing snapshots.
3244 * A second request with the current snapshot context will be
3245 * sent for the actual modification.
3246 */
3247 ret = rbd_obj_copyup_empty_snapc(obj_req, bytes);
3248 if (ret) {
3249 obj_req->pending.result = ret;
3250 return;
3251 }
3252
3253 obj_req->pending.num_pending++;
3254 bytes = MODS_ONLY;
3255 }
3256
3257 ret = rbd_obj_copyup_current_snapc(obj_req, bytes);
3258 if (ret) {
3259 obj_req->pending.result = ret;
3260 return;
3261 }
3262
3263 obj_req->pending.num_pending++;
3264}
3265
3266static bool rbd_obj_advance_copyup(struct rbd_obj_request *obj_req, int *result)
3267{
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02003268 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
Ilya Dryomov793333a302019-06-13 17:44:08 +02003269 int ret;
3270
3271again:
3272 switch (obj_req->copyup_state) {
3273 case RBD_OBJ_COPYUP_START:
3274 rbd_assert(!*result);
3275
3276 ret = rbd_obj_copyup_read_parent(obj_req);
3277 if (ret) {
3278 *result = ret;
3279 return true;
3280 }
3281 if (obj_req->num_img_extents)
3282 obj_req->copyup_state = RBD_OBJ_COPYUP_READ_PARENT;
3283 else
3284 obj_req->copyup_state = RBD_OBJ_COPYUP_WRITE_OBJECT;
3285 return false;
3286 case RBD_OBJ_COPYUP_READ_PARENT:
3287 if (*result)
3288 return true;
3289
3290 if (is_zero_bvecs(obj_req->copyup_bvecs,
3291 rbd_obj_img_extents_bytes(obj_req))) {
3292 dout("%s %p detected zeros\n", __func__, obj_req);
3293 obj_req->flags |= RBD_OBJ_FLAG_COPYUP_ZEROS;
3294 }
3295
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02003296 rbd_obj_copyup_object_maps(obj_req);
3297 if (!obj_req->pending.num_pending) {
3298 *result = obj_req->pending.result;
3299 obj_req->copyup_state = RBD_OBJ_COPYUP_OBJECT_MAPS;
3300 goto again;
3301 }
3302 obj_req->copyup_state = __RBD_OBJ_COPYUP_OBJECT_MAPS;
3303 return false;
3304 case __RBD_OBJ_COPYUP_OBJECT_MAPS:
3305 if (!pending_result_dec(&obj_req->pending, result))
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003306 return false;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003307 /* fall through */
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02003308 case RBD_OBJ_COPYUP_OBJECT_MAPS:
3309 if (*result) {
3310 rbd_warn(rbd_dev, "snap object map update failed: %d",
3311 *result);
3312 return true;
3313 }
3314
Ilya Dryomov793333a302019-06-13 17:44:08 +02003315 rbd_obj_copyup_write_object(obj_req);
3316 if (!obj_req->pending.num_pending) {
3317 *result = obj_req->pending.result;
3318 obj_req->copyup_state = RBD_OBJ_COPYUP_WRITE_OBJECT;
3319 goto again;
3320 }
3321 obj_req->copyup_state = __RBD_OBJ_COPYUP_WRITE_OBJECT;
3322 return false;
3323 case __RBD_OBJ_COPYUP_WRITE_OBJECT:
3324 if (!pending_result_dec(&obj_req->pending, result))
3325 return false;
3326 /* fall through */
3327 case RBD_OBJ_COPYUP_WRITE_OBJECT:
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003328 return true;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003329 default:
Arnd Bergmannc6244b32018-04-04 14:53:39 +02003330 BUG();
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003331 }
3332}
3333
3334/*
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02003335 * Return:
3336 * 0 - object map update sent
3337 * 1 - object map update isn't needed
3338 * <0 - error
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003339 */
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02003340static int rbd_obj_write_post_object_map(struct rbd_obj_request *obj_req)
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003341{
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02003342 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
3343 u8 current_state = OBJECT_PENDING;
3344
3345 if (!(rbd_dev->header.features & RBD_FEATURE_OBJECT_MAP))
3346 return 1;
3347
3348 if (!(obj_req->flags & RBD_OBJ_FLAG_DELETION))
3349 return 1;
3350
3351 return rbd_object_map_update(obj_req, CEPH_NOSNAP, OBJECT_NONEXISTENT,
3352 &current_state);
3353}
3354
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02003355static bool rbd_obj_advance_write(struct rbd_obj_request *obj_req, int *result)
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003356{
Ilya Dryomov793333a302019-06-13 17:44:08 +02003357 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003358 int ret;
3359
Ilya Dryomov793333a302019-06-13 17:44:08 +02003360again:
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003361 switch (obj_req->write_state) {
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02003362 case RBD_OBJ_WRITE_START:
3363 rbd_assert(!*result);
3364
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02003365 if (rbd_obj_write_is_noop(obj_req))
3366 return true;
3367
3368 ret = rbd_obj_write_pre_object_map(obj_req);
3369 if (ret < 0) {
3370 *result = ret;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003371 return true;
3372 }
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02003373 obj_req->write_state = RBD_OBJ_WRITE_PRE_OBJECT_MAP;
3374 if (ret > 0)
3375 goto again;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003376 return false;
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02003377 case RBD_OBJ_WRITE_PRE_OBJECT_MAP:
3378 if (*result) {
3379 rbd_warn(rbd_dev, "pre object map update failed: %d",
3380 *result);
3381 return true;
3382 }
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02003383 ret = rbd_obj_write_object(obj_req);
3384 if (ret) {
3385 *result = ret;
3386 return true;
3387 }
3388 obj_req->write_state = RBD_OBJ_WRITE_OBJECT;
3389 return false;
Ilya Dryomov0ad5d952019-05-14 20:45:38 +02003390 case RBD_OBJ_WRITE_OBJECT:
Ilya Dryomov54ab3b22019-05-11 16:21:49 +02003391 if (*result == -ENOENT) {
Ilya Dryomov0ad5d952019-05-14 20:45:38 +02003392 if (obj_req->flags & RBD_OBJ_FLAG_COPYUP_ENABLED) {
Ilya Dryomov793333a302019-06-13 17:44:08 +02003393 *result = 0;
3394 obj_req->copyup_state = RBD_OBJ_COPYUP_START;
3395 obj_req->write_state = __RBD_OBJ_WRITE_COPYUP;
3396 goto again;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003397 }
Ilya Dryomov0ad5d952019-05-14 20:45:38 +02003398 /*
3399 * On a non-existent object:
3400 * delete - -ENOENT, truncate/zero - 0
3401 */
3402 if (obj_req->flags & RBD_OBJ_FLAG_DELETION)
3403 *result = 0;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003404 }
Ilya Dryomov793333a302019-06-13 17:44:08 +02003405 if (*result)
3406 return true;
3407
3408 obj_req->write_state = RBD_OBJ_WRITE_COPYUP;
3409 goto again;
3410 case __RBD_OBJ_WRITE_COPYUP:
3411 if (!rbd_obj_advance_copyup(obj_req, result))
3412 return false;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003413 /* fall through */
Ilya Dryomov793333a302019-06-13 17:44:08 +02003414 case RBD_OBJ_WRITE_COPYUP:
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02003415 if (*result) {
Ilya Dryomov793333a302019-06-13 17:44:08 +02003416 rbd_warn(rbd_dev, "copyup failed: %d", *result);
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02003417 return true;
3418 }
3419 ret = rbd_obj_write_post_object_map(obj_req);
3420 if (ret < 0) {
3421 *result = ret;
3422 return true;
3423 }
3424 obj_req->write_state = RBD_OBJ_WRITE_POST_OBJECT_MAP;
3425 if (ret > 0)
3426 goto again;
3427 return false;
3428 case RBD_OBJ_WRITE_POST_OBJECT_MAP:
3429 if (*result)
3430 rbd_warn(rbd_dev, "post object map update failed: %d",
3431 *result);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003432 return true;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003433 default:
Arnd Bergmannc6244b32018-04-04 14:53:39 +02003434 BUG();
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003435 }
3436}
3437
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003438/*
Ilya Dryomov0ad5d952019-05-14 20:45:38 +02003439 * Return true if @obj_req is completed.
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003440 */
Ilya Dryomov54ab3b22019-05-11 16:21:49 +02003441static bool __rbd_obj_handle_request(struct rbd_obj_request *obj_req,
3442 int *result)
Ilya Dryomov7114eda2018-02-01 11:50:47 +01003443{
3444 struct rbd_img_request *img_req = obj_req->img_request;
Ilya Dryomov0192ce22019-05-16 15:06:56 +02003445 struct rbd_device *rbd_dev = img_req->rbd_dev;
Ilya Dryomov0ad5d952019-05-14 20:45:38 +02003446 bool done;
Ilya Dryomov7114eda2018-02-01 11:50:47 +01003447
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02003448 mutex_lock(&obj_req->state_mutex);
Ilya Dryomov0ad5d952019-05-14 20:45:38 +02003449 if (!rbd_img_is_write(img_req))
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02003450 done = rbd_obj_advance_read(obj_req, result);
Ilya Dryomov0ad5d952019-05-14 20:45:38 +02003451 else
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02003452 done = rbd_obj_advance_write(obj_req, result);
3453 mutex_unlock(&obj_req->state_mutex);
Alex Elder02c74fb2013-05-06 17:40:33 -05003454
Ilya Dryomov0192ce22019-05-16 15:06:56 +02003455 if (done && *result) {
3456 rbd_assert(*result < 0);
3457 rbd_warn(rbd_dev, "%s at objno %llu %llu~%llu result %d",
3458 obj_op_name(img_req->op_type), obj_req->ex.oe_objno,
3459 obj_req->ex.oe_off, obj_req->ex.oe_len, *result);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05003460 }
Ilya Dryomov0ad5d952019-05-14 20:45:38 +02003461 return done;
Alex Elder8b3e1a52013-01-24 16:13:36 -06003462}
3463
Ilya Dryomov0192ce22019-05-16 15:06:56 +02003464/*
3465 * This is open-coded in rbd_img_handle_request() to avoid parent chain
3466 * recursion.
3467 */
Ilya Dryomov54ab3b22019-05-11 16:21:49 +02003468static void rbd_obj_handle_request(struct rbd_obj_request *obj_req, int result)
Alex Elder8b3e1a52013-01-24 16:13:36 -06003469{
Ilya Dryomov0192ce22019-05-16 15:06:56 +02003470 if (__rbd_obj_handle_request(obj_req, &result))
3471 rbd_img_handle_request(obj_req->img_request, result);
Ilya Dryomov7114eda2018-02-01 11:50:47 +01003472}
Alex Elder8b3e1a52013-01-24 16:13:36 -06003473
Ilya Dryomove1fddc82019-05-30 16:07:48 +02003474static bool need_exclusive_lock(struct rbd_img_request *img_req)
Ilya Dryomov7114eda2018-02-01 11:50:47 +01003475{
Ilya Dryomove1fddc82019-05-30 16:07:48 +02003476 struct rbd_device *rbd_dev = img_req->rbd_dev;
3477
3478 if (!(rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK))
3479 return false;
3480
Ilya Dryomov3fe69922019-11-12 19:41:48 +01003481 if (rbd_is_ro(rbd_dev))
Ilya Dryomove1fddc82019-05-30 16:07:48 +02003482 return false;
3483
Ilya Dryomov7114eda2018-02-01 11:50:47 +01003484 rbd_assert(!test_bit(IMG_REQ_CHILD, &img_req->flags));
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02003485 if (rbd_dev->opts->lock_on_read ||
3486 (rbd_dev->header.features & RBD_FEATURE_OBJECT_MAP))
Ilya Dryomove1fddc82019-05-30 16:07:48 +02003487 return true;
Alex Elder8b3e1a52013-01-24 16:13:36 -06003488
Ilya Dryomove1fddc82019-05-30 16:07:48 +02003489 return rbd_img_is_write(img_req);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003490}
Alex Elder8b3e1a52013-01-24 16:13:36 -06003491
Ilya Dryomov637cd062019-06-06 17:14:49 +02003492static bool rbd_lock_add_request(struct rbd_img_request *img_req)
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003493{
Ilya Dryomove1fddc82019-05-30 16:07:48 +02003494 struct rbd_device *rbd_dev = img_req->rbd_dev;
Ilya Dryomov637cd062019-06-06 17:14:49 +02003495 bool locked;
Ilya Dryomove1fddc82019-05-30 16:07:48 +02003496
3497 lockdep_assert_held(&rbd_dev->lock_rwsem);
Ilya Dryomov637cd062019-06-06 17:14:49 +02003498 locked = rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED;
Ilya Dryomove1fddc82019-05-30 16:07:48 +02003499 spin_lock(&rbd_dev->lock_lists_lock);
3500 rbd_assert(list_empty(&img_req->lock_item));
Ilya Dryomov637cd062019-06-06 17:14:49 +02003501 if (!locked)
3502 list_add_tail(&img_req->lock_item, &rbd_dev->acquiring_list);
3503 else
3504 list_add_tail(&img_req->lock_item, &rbd_dev->running_list);
Ilya Dryomove1fddc82019-05-30 16:07:48 +02003505 spin_unlock(&rbd_dev->lock_lists_lock);
Ilya Dryomov637cd062019-06-06 17:14:49 +02003506 return locked;
Ilya Dryomove1fddc82019-05-30 16:07:48 +02003507}
3508
3509static void rbd_lock_del_request(struct rbd_img_request *img_req)
3510{
3511 struct rbd_device *rbd_dev = img_req->rbd_dev;
3512 bool need_wakeup;
3513
3514 lockdep_assert_held(&rbd_dev->lock_rwsem);
3515 spin_lock(&rbd_dev->lock_lists_lock);
3516 rbd_assert(!list_empty(&img_req->lock_item));
3517 list_del_init(&img_req->lock_item);
3518 need_wakeup = (rbd_dev->lock_state == RBD_LOCK_STATE_RELEASING &&
3519 list_empty(&rbd_dev->running_list));
3520 spin_unlock(&rbd_dev->lock_lists_lock);
3521 if (need_wakeup)
3522 complete(&rbd_dev->releasing_wait);
3523}
3524
Ilya Dryomov637cd062019-06-06 17:14:49 +02003525static int rbd_img_exclusive_lock(struct rbd_img_request *img_req)
3526{
3527 struct rbd_device *rbd_dev = img_req->rbd_dev;
3528
3529 if (!need_exclusive_lock(img_req))
3530 return 1;
3531
3532 if (rbd_lock_add_request(img_req))
3533 return 1;
3534
3535 if (rbd_dev->opts->exclusive) {
3536 WARN_ON(1); /* lock got released? */
3537 return -EROFS;
3538 }
3539
3540 /*
3541 * Note the use of mod_delayed_work() in rbd_acquire_lock()
3542 * and cancel_delayed_work() in wake_lock_waiters().
3543 */
3544 dout("%s rbd_dev %p queueing lock_dwork\n", __func__, rbd_dev);
3545 queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0);
3546 return 0;
3547}
3548
Ilya Dryomov0192ce22019-05-16 15:06:56 +02003549static void rbd_img_object_requests(struct rbd_img_request *img_req)
3550{
3551 struct rbd_obj_request *obj_req;
3552
3553 rbd_assert(!img_req->pending.result && !img_req->pending.num_pending);
3554
3555 for_each_obj_request(img_req, obj_req) {
3556 int result = 0;
3557
3558 if (__rbd_obj_handle_request(obj_req, &result)) {
3559 if (result) {
3560 img_req->pending.result = result;
3561 return;
3562 }
3563 } else {
3564 img_req->pending.num_pending++;
3565 }
3566 }
3567}
3568
3569static bool rbd_img_advance(struct rbd_img_request *img_req, int *result)
3570{
Ilya Dryomov637cd062019-06-06 17:14:49 +02003571 struct rbd_device *rbd_dev = img_req->rbd_dev;
3572 int ret;
Ilya Dryomov7114eda2018-02-01 11:50:47 +01003573
3574again:
Ilya Dryomov0192ce22019-05-16 15:06:56 +02003575 switch (img_req->state) {
3576 case RBD_IMG_START:
3577 rbd_assert(!*result);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003578
Ilya Dryomov637cd062019-06-06 17:14:49 +02003579 ret = rbd_img_exclusive_lock(img_req);
3580 if (ret < 0) {
3581 *result = ret;
3582 return true;
3583 }
3584 img_req->state = RBD_IMG_EXCLUSIVE_LOCK;
3585 if (ret > 0)
3586 goto again;
3587 return false;
3588 case RBD_IMG_EXCLUSIVE_LOCK:
3589 if (*result)
3590 return true;
3591
3592 rbd_assert(!need_exclusive_lock(img_req) ||
3593 __rbd_is_lock_owner(rbd_dev));
3594
Ilya Dryomov0192ce22019-05-16 15:06:56 +02003595 rbd_img_object_requests(img_req);
3596 if (!img_req->pending.num_pending) {
3597 *result = img_req->pending.result;
3598 img_req->state = RBD_IMG_OBJECT_REQUESTS;
3599 goto again;
3600 }
3601 img_req->state = __RBD_IMG_OBJECT_REQUESTS;
3602 return false;
3603 case __RBD_IMG_OBJECT_REQUESTS:
3604 if (!pending_result_dec(&img_req->pending, result))
3605 return false;
3606 /* fall through */
3607 case RBD_IMG_OBJECT_REQUESTS:
3608 return true;
3609 default:
3610 BUG();
Ilya Dryomov7114eda2018-02-01 11:50:47 +01003611 }
Ilya Dryomov0192ce22019-05-16 15:06:56 +02003612}
Ilya Dryomov7114eda2018-02-01 11:50:47 +01003613
Ilya Dryomov0192ce22019-05-16 15:06:56 +02003614/*
3615 * Return true if @img_req is completed.
3616 */
3617static bool __rbd_img_handle_request(struct rbd_img_request *img_req,
3618 int *result)
3619{
3620 struct rbd_device *rbd_dev = img_req->rbd_dev;
3621 bool done;
3622
Ilya Dryomove1fddc82019-05-30 16:07:48 +02003623 if (need_exclusive_lock(img_req)) {
3624 down_read(&rbd_dev->lock_rwsem);
3625 mutex_lock(&img_req->state_mutex);
3626 done = rbd_img_advance(img_req, result);
3627 if (done)
3628 rbd_lock_del_request(img_req);
3629 mutex_unlock(&img_req->state_mutex);
3630 up_read(&rbd_dev->lock_rwsem);
3631 } else {
3632 mutex_lock(&img_req->state_mutex);
3633 done = rbd_img_advance(img_req, result);
3634 mutex_unlock(&img_req->state_mutex);
Ilya Dryomov7114eda2018-02-01 11:50:47 +01003635 }
3636
Ilya Dryomov0192ce22019-05-16 15:06:56 +02003637 if (done && *result) {
3638 rbd_assert(*result < 0);
3639 rbd_warn(rbd_dev, "%s%s result %d",
3640 test_bit(IMG_REQ_CHILD, &img_req->flags) ? "child " : "",
3641 obj_op_name(img_req->op_type), *result);
3642 }
3643 return done;
3644}
3645
3646static void rbd_img_handle_request(struct rbd_img_request *img_req, int result)
3647{
3648again:
3649 if (!__rbd_img_handle_request(img_req, &result))
3650 return;
3651
Ilya Dryomov7114eda2018-02-01 11:50:47 +01003652 if (test_bit(IMG_REQ_CHILD, &img_req->flags)) {
Ilya Dryomov0192ce22019-05-16 15:06:56 +02003653 struct rbd_obj_request *obj_req = img_req->obj_request;
3654
Ilya Dryomov54ab3b22019-05-11 16:21:49 +02003655 rbd_img_request_put(img_req);
Ilya Dryomov0192ce22019-05-16 15:06:56 +02003656 if (__rbd_obj_handle_request(obj_req, &result)) {
3657 img_req = obj_req->img_request;
3658 goto again;
3659 }
3660 } else {
3661 struct request *rq = img_req->rq;
3662
3663 rbd_img_request_put(img_req);
3664 blk_mq_end_request(rq, errno_to_blk_status(result));
Ilya Dryomov7114eda2018-02-01 11:50:47 +01003665 }
Alex Elder8b3e1a52013-01-24 16:13:36 -06003666}
3667
Ilya Dryomoved95b212016-08-12 16:40:02 +02003668static const struct rbd_client_id rbd_empty_cid;
3669
3670static bool rbd_cid_equal(const struct rbd_client_id *lhs,
3671 const struct rbd_client_id *rhs)
3672{
3673 return lhs->gid == rhs->gid && lhs->handle == rhs->handle;
3674}
3675
3676static struct rbd_client_id rbd_get_cid(struct rbd_device *rbd_dev)
3677{
3678 struct rbd_client_id cid;
3679
3680 mutex_lock(&rbd_dev->watch_mutex);
3681 cid.gid = ceph_client_gid(rbd_dev->rbd_client->client);
3682 cid.handle = rbd_dev->watch_cookie;
3683 mutex_unlock(&rbd_dev->watch_mutex);
3684 return cid;
3685}
3686
3687/*
3688 * lock_rwsem must be held for write
3689 */
3690static void rbd_set_owner_cid(struct rbd_device *rbd_dev,
3691 const struct rbd_client_id *cid)
3692{
3693 dout("%s rbd_dev %p %llu-%llu -> %llu-%llu\n", __func__, rbd_dev,
3694 rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle,
3695 cid->gid, cid->handle);
3696 rbd_dev->owner_cid = *cid; /* struct */
3697}
3698
3699static void format_lock_cookie(struct rbd_device *rbd_dev, char *buf)
3700{
3701 mutex_lock(&rbd_dev->watch_mutex);
3702 sprintf(buf, "%s %llu", RBD_LOCK_COOKIE_PREFIX, rbd_dev->watch_cookie);
3703 mutex_unlock(&rbd_dev->watch_mutex);
3704}
3705
Florian Margaineedd8ca82017-12-13 16:43:59 +01003706static void __rbd_lock(struct rbd_device *rbd_dev, const char *cookie)
3707{
3708 struct rbd_client_id cid = rbd_get_cid(rbd_dev);
3709
Ilya Dryomova2b1da02019-05-30 11:15:23 +02003710 rbd_dev->lock_state = RBD_LOCK_STATE_LOCKED;
Florian Margaineedd8ca82017-12-13 16:43:59 +01003711 strcpy(rbd_dev->lock_cookie, cookie);
3712 rbd_set_owner_cid(rbd_dev, &cid);
3713 queue_work(rbd_dev->task_wq, &rbd_dev->acquired_lock_work);
3714}
3715
Ilya Dryomoved95b212016-08-12 16:40:02 +02003716/*
3717 * lock_rwsem must be held for write
3718 */
3719static int rbd_lock(struct rbd_device *rbd_dev)
3720{
3721 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003722 char cookie[32];
3723 int ret;
3724
Ilya Dryomovcbbfb0f2017-04-13 12:17:38 +02003725 WARN_ON(__rbd_is_lock_owner(rbd_dev) ||
3726 rbd_dev->lock_cookie[0] != '\0');
Ilya Dryomoved95b212016-08-12 16:40:02 +02003727
3728 format_lock_cookie(rbd_dev, cookie);
3729 ret = ceph_cls_lock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc,
3730 RBD_LOCK_NAME, CEPH_CLS_LOCK_EXCLUSIVE, cookie,
3731 RBD_LOCK_TAG, "", 0);
3732 if (ret)
3733 return ret;
3734
Florian Margaineedd8ca82017-12-13 16:43:59 +01003735 __rbd_lock(rbd_dev, cookie);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003736 return 0;
3737}
3738
3739/*
3740 * lock_rwsem must be held for write
3741 */
Ilya Dryomovbbead742017-04-13 12:17:38 +02003742static void rbd_unlock(struct rbd_device *rbd_dev)
Ilya Dryomoved95b212016-08-12 16:40:02 +02003743{
3744 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003745 int ret;
3746
Ilya Dryomovcbbfb0f2017-04-13 12:17:38 +02003747 WARN_ON(!__rbd_is_lock_owner(rbd_dev) ||
3748 rbd_dev->lock_cookie[0] == '\0');
Ilya Dryomoved95b212016-08-12 16:40:02 +02003749
Ilya Dryomoved95b212016-08-12 16:40:02 +02003750 ret = ceph_cls_unlock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc,
Ilya Dryomovcbbfb0f2017-04-13 12:17:38 +02003751 RBD_LOCK_NAME, rbd_dev->lock_cookie);
Ilya Dryomovbbead742017-04-13 12:17:38 +02003752 if (ret && ret != -ENOENT)
Ilya Dryomov637cd062019-06-06 17:14:49 +02003753 rbd_warn(rbd_dev, "failed to unlock header: %d", ret);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003754
Ilya Dryomovbbead742017-04-13 12:17:38 +02003755 /* treat errors as the image is unlocked */
3756 rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED;
Ilya Dryomovcbbfb0f2017-04-13 12:17:38 +02003757 rbd_dev->lock_cookie[0] = '\0';
Ilya Dryomoved95b212016-08-12 16:40:02 +02003758 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
3759 queue_work(rbd_dev->task_wq, &rbd_dev->released_lock_work);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003760}
3761
3762static int __rbd_notify_op_lock(struct rbd_device *rbd_dev,
3763 enum rbd_notify_op notify_op,
3764 struct page ***preply_pages,
3765 size_t *preply_len)
3766{
3767 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3768 struct rbd_client_id cid = rbd_get_cid(rbd_dev);
Kyle Spiers08a79102018-03-17 09:44:01 -07003769 char buf[4 + 8 + 8 + CEPH_ENCODING_START_BLK_LEN];
3770 int buf_size = sizeof(buf);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003771 void *p = buf;
3772
3773 dout("%s rbd_dev %p notify_op %d\n", __func__, rbd_dev, notify_op);
3774
3775 /* encode *LockPayload NotifyMessage (op + ClientId) */
3776 ceph_start_encoding(&p, 2, 1, buf_size - CEPH_ENCODING_START_BLK_LEN);
3777 ceph_encode_32(&p, notify_op);
3778 ceph_encode_64(&p, cid.gid);
3779 ceph_encode_64(&p, cid.handle);
3780
3781 return ceph_osdc_notify(osdc, &rbd_dev->header_oid,
3782 &rbd_dev->header_oloc, buf, buf_size,
3783 RBD_NOTIFY_TIMEOUT, preply_pages, preply_len);
3784}
3785
3786static void rbd_notify_op_lock(struct rbd_device *rbd_dev,
3787 enum rbd_notify_op notify_op)
3788{
3789 struct page **reply_pages;
3790 size_t reply_len;
3791
3792 __rbd_notify_op_lock(rbd_dev, notify_op, &reply_pages, &reply_len);
3793 ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len));
3794}
3795
3796static void rbd_notify_acquired_lock(struct work_struct *work)
3797{
3798 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
3799 acquired_lock_work);
3800
3801 rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_ACQUIRED_LOCK);
3802}
3803
3804static void rbd_notify_released_lock(struct work_struct *work)
3805{
3806 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
3807 released_lock_work);
3808
3809 rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_RELEASED_LOCK);
3810}
3811
3812static int rbd_request_lock(struct rbd_device *rbd_dev)
3813{
3814 struct page **reply_pages;
3815 size_t reply_len;
3816 bool lock_owner_responded = false;
3817 int ret;
3818
3819 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3820
3821 ret = __rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_REQUEST_LOCK,
3822 &reply_pages, &reply_len);
3823 if (ret && ret != -ETIMEDOUT) {
3824 rbd_warn(rbd_dev, "failed to request lock: %d", ret);
3825 goto out;
3826 }
3827
3828 if (reply_len > 0 && reply_len <= PAGE_SIZE) {
3829 void *p = page_address(reply_pages[0]);
3830 void *const end = p + reply_len;
3831 u32 n;
3832
3833 ceph_decode_32_safe(&p, end, n, e_inval); /* num_acks */
3834 while (n--) {
3835 u8 struct_v;
3836 u32 len;
3837
3838 ceph_decode_need(&p, end, 8 + 8, e_inval);
3839 p += 8 + 8; /* skip gid and cookie */
3840
3841 ceph_decode_32_safe(&p, end, len, e_inval);
3842 if (!len)
3843 continue;
3844
3845 if (lock_owner_responded) {
3846 rbd_warn(rbd_dev,
3847 "duplicate lock owners detected");
3848 ret = -EIO;
3849 goto out;
3850 }
3851
3852 lock_owner_responded = true;
3853 ret = ceph_start_decoding(&p, end, 1, "ResponseMessage",
3854 &struct_v, &len);
3855 if (ret) {
3856 rbd_warn(rbd_dev,
3857 "failed to decode ResponseMessage: %d",
3858 ret);
3859 goto e_inval;
3860 }
3861
3862 ret = ceph_decode_32(&p);
3863 }
3864 }
3865
3866 if (!lock_owner_responded) {
3867 rbd_warn(rbd_dev, "no lock owners detected");
3868 ret = -ETIMEDOUT;
3869 }
3870
3871out:
3872 ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len));
3873 return ret;
3874
3875e_inval:
3876 ret = -EINVAL;
3877 goto out;
3878}
3879
Ilya Dryomov637cd062019-06-06 17:14:49 +02003880/*
3881 * Either image request state machine(s) or rbd_add_acquire_lock()
3882 * (i.e. "rbd map").
3883 */
3884static void wake_lock_waiters(struct rbd_device *rbd_dev, int result)
Ilya Dryomoved95b212016-08-12 16:40:02 +02003885{
Ilya Dryomov637cd062019-06-06 17:14:49 +02003886 struct rbd_img_request *img_req;
3887
3888 dout("%s rbd_dev %p result %d\n", __func__, rbd_dev, result);
Linus Torvaldsd9b9c892019-07-18 11:05:25 -07003889 lockdep_assert_held_write(&rbd_dev->lock_rwsem);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003890
3891 cancel_delayed_work(&rbd_dev->lock_dwork);
Ilya Dryomov637cd062019-06-06 17:14:49 +02003892 if (!completion_done(&rbd_dev->acquire_wait)) {
3893 rbd_assert(list_empty(&rbd_dev->acquiring_list) &&
3894 list_empty(&rbd_dev->running_list));
3895 rbd_dev->acquire_err = result;
3896 complete_all(&rbd_dev->acquire_wait);
3897 return;
3898 }
3899
3900 list_for_each_entry(img_req, &rbd_dev->acquiring_list, lock_item) {
3901 mutex_lock(&img_req->state_mutex);
3902 rbd_assert(img_req->state == RBD_IMG_EXCLUSIVE_LOCK);
3903 rbd_img_schedule(img_req, result);
3904 mutex_unlock(&img_req->state_mutex);
3905 }
3906
3907 list_splice_tail_init(&rbd_dev->acquiring_list, &rbd_dev->running_list);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003908}
3909
3910static int get_lock_owner_info(struct rbd_device *rbd_dev,
3911 struct ceph_locker **lockers, u32 *num_lockers)
3912{
3913 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3914 u8 lock_type;
3915 char *lock_tag;
3916 int ret;
3917
3918 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3919
3920 ret = ceph_cls_lock_info(osdc, &rbd_dev->header_oid,
3921 &rbd_dev->header_oloc, RBD_LOCK_NAME,
3922 &lock_type, &lock_tag, lockers, num_lockers);
3923 if (ret)
3924 return ret;
3925
3926 if (*num_lockers == 0) {
3927 dout("%s rbd_dev %p no lockers detected\n", __func__, rbd_dev);
3928 goto out;
3929 }
3930
3931 if (strcmp(lock_tag, RBD_LOCK_TAG)) {
3932 rbd_warn(rbd_dev, "locked by external mechanism, tag %s",
3933 lock_tag);
3934 ret = -EBUSY;
3935 goto out;
3936 }
3937
3938 if (lock_type == CEPH_CLS_LOCK_SHARED) {
3939 rbd_warn(rbd_dev, "shared lock type detected");
3940 ret = -EBUSY;
3941 goto out;
3942 }
3943
3944 if (strncmp((*lockers)[0].id.cookie, RBD_LOCK_COOKIE_PREFIX,
3945 strlen(RBD_LOCK_COOKIE_PREFIX))) {
3946 rbd_warn(rbd_dev, "locked by external mechanism, cookie %s",
3947 (*lockers)[0].id.cookie);
3948 ret = -EBUSY;
3949 goto out;
3950 }
3951
3952out:
3953 kfree(lock_tag);
3954 return ret;
3955}
3956
3957static int find_watcher(struct rbd_device *rbd_dev,
3958 const struct ceph_locker *locker)
3959{
3960 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3961 struct ceph_watch_item *watchers;
3962 u32 num_watchers;
3963 u64 cookie;
3964 int i;
3965 int ret;
3966
3967 ret = ceph_osdc_list_watchers(osdc, &rbd_dev->header_oid,
3968 &rbd_dev->header_oloc, &watchers,
3969 &num_watchers);
3970 if (ret)
3971 return ret;
3972
3973 sscanf(locker->id.cookie, RBD_LOCK_COOKIE_PREFIX " %llu", &cookie);
3974 for (i = 0; i < num_watchers; i++) {
3975 if (!memcmp(&watchers[i].addr, &locker->info.addr,
3976 sizeof(locker->info.addr)) &&
3977 watchers[i].cookie == cookie) {
3978 struct rbd_client_id cid = {
3979 .gid = le64_to_cpu(watchers[i].name.num),
3980 .handle = cookie,
3981 };
3982
3983 dout("%s rbd_dev %p found cid %llu-%llu\n", __func__,
3984 rbd_dev, cid.gid, cid.handle);
3985 rbd_set_owner_cid(rbd_dev, &cid);
3986 ret = 1;
3987 goto out;
3988 }
3989 }
3990
3991 dout("%s rbd_dev %p no watchers\n", __func__, rbd_dev);
3992 ret = 0;
3993out:
3994 kfree(watchers);
3995 return ret;
3996}
3997
3998/*
3999 * lock_rwsem must be held for write
4000 */
4001static int rbd_try_lock(struct rbd_device *rbd_dev)
4002{
4003 struct ceph_client *client = rbd_dev->rbd_client->client;
4004 struct ceph_locker *lockers;
4005 u32 num_lockers;
4006 int ret;
4007
4008 for (;;) {
4009 ret = rbd_lock(rbd_dev);
4010 if (ret != -EBUSY)
4011 return ret;
4012
4013 /* determine if the current lock holder is still alive */
4014 ret = get_lock_owner_info(rbd_dev, &lockers, &num_lockers);
4015 if (ret)
4016 return ret;
4017
4018 if (num_lockers == 0)
4019 goto again;
4020
4021 ret = find_watcher(rbd_dev, lockers);
Ilya Dryomov637cd062019-06-06 17:14:49 +02004022 if (ret)
4023 goto out; /* request lock or error */
Ilya Dryomoved95b212016-08-12 16:40:02 +02004024
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02004025 rbd_warn(rbd_dev, "breaking header lock owned by %s%llu",
Ilya Dryomoved95b212016-08-12 16:40:02 +02004026 ENTITY_NAME(lockers[0].id.name));
4027
4028 ret = ceph_monc_blacklist_add(&client->monc,
4029 &lockers[0].info.addr);
4030 if (ret) {
4031 rbd_warn(rbd_dev, "blacklist of %s%llu failed: %d",
4032 ENTITY_NAME(lockers[0].id.name), ret);
4033 goto out;
4034 }
4035
4036 ret = ceph_cls_break_lock(&client->osdc, &rbd_dev->header_oid,
4037 &rbd_dev->header_oloc, RBD_LOCK_NAME,
4038 lockers[0].id.cookie,
4039 &lockers[0].id.name);
4040 if (ret && ret != -ENOENT)
4041 goto out;
4042
4043again:
4044 ceph_free_lockers(lockers, num_lockers);
4045 }
4046
4047out:
4048 ceph_free_lockers(lockers, num_lockers);
4049 return ret;
4050}
4051
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02004052static int rbd_post_acquire_action(struct rbd_device *rbd_dev)
Ilya Dryomoved95b212016-08-12 16:40:02 +02004053{
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02004054 int ret;
4055
4056 if (rbd_dev->header.features & RBD_FEATURE_OBJECT_MAP) {
4057 ret = rbd_object_map_open(rbd_dev);
4058 if (ret)
4059 return ret;
4060 }
4061
4062 return 0;
4063}
4064
Ilya Dryomoved95b212016-08-12 16:40:02 +02004065/*
Ilya Dryomov637cd062019-06-06 17:14:49 +02004066 * Return:
4067 * 0 - lock acquired
4068 * 1 - caller should call rbd_request_lock()
4069 * <0 - error
Ilya Dryomoved95b212016-08-12 16:40:02 +02004070 */
Ilya Dryomov637cd062019-06-06 17:14:49 +02004071static int rbd_try_acquire_lock(struct rbd_device *rbd_dev)
Ilya Dryomoved95b212016-08-12 16:40:02 +02004072{
Ilya Dryomov637cd062019-06-06 17:14:49 +02004073 int ret;
Ilya Dryomoved95b212016-08-12 16:40:02 +02004074
4075 down_read(&rbd_dev->lock_rwsem);
4076 dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev,
4077 rbd_dev->lock_state);
4078 if (__rbd_is_lock_owner(rbd_dev)) {
Ilya Dryomoved95b212016-08-12 16:40:02 +02004079 up_read(&rbd_dev->lock_rwsem);
Ilya Dryomov637cd062019-06-06 17:14:49 +02004080 return 0;
Ilya Dryomoved95b212016-08-12 16:40:02 +02004081 }
4082
4083 up_read(&rbd_dev->lock_rwsem);
4084 down_write(&rbd_dev->lock_rwsem);
4085 dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev,
4086 rbd_dev->lock_state);
Ilya Dryomov637cd062019-06-06 17:14:49 +02004087 if (__rbd_is_lock_owner(rbd_dev)) {
4088 up_write(&rbd_dev->lock_rwsem);
4089 return 0;
Ilya Dryomoved95b212016-08-12 16:40:02 +02004090 }
4091
Ilya Dryomov637cd062019-06-06 17:14:49 +02004092 ret = rbd_try_lock(rbd_dev);
4093 if (ret < 0) {
4094 rbd_warn(rbd_dev, "failed to lock header: %d", ret);
4095 if (ret == -EBLACKLISTED)
4096 goto out;
4097
4098 ret = 1; /* request lock anyway */
4099 }
4100 if (ret > 0) {
4101 up_write(&rbd_dev->lock_rwsem);
4102 return ret;
4103 }
4104
4105 rbd_assert(rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED);
4106 rbd_assert(list_empty(&rbd_dev->running_list));
4107
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02004108 ret = rbd_post_acquire_action(rbd_dev);
4109 if (ret) {
4110 rbd_warn(rbd_dev, "post-acquire action failed: %d", ret);
4111 /*
4112 * Can't stay in RBD_LOCK_STATE_LOCKED because
4113 * rbd_lock_add_request() would let the request through,
4114 * assuming that e.g. object map is locked and loaded.
4115 */
4116 rbd_unlock(rbd_dev);
4117 }
4118
Ilya Dryomov637cd062019-06-06 17:14:49 +02004119out:
4120 wake_lock_waiters(rbd_dev, ret);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004121 up_write(&rbd_dev->lock_rwsem);
Ilya Dryomov637cd062019-06-06 17:14:49 +02004122 return ret;
Ilya Dryomoved95b212016-08-12 16:40:02 +02004123}
4124
4125static void rbd_acquire_lock(struct work_struct *work)
4126{
4127 struct rbd_device *rbd_dev = container_of(to_delayed_work(work),
4128 struct rbd_device, lock_dwork);
Ilya Dryomov637cd062019-06-06 17:14:49 +02004129 int ret;
Ilya Dryomoved95b212016-08-12 16:40:02 +02004130
4131 dout("%s rbd_dev %p\n", __func__, rbd_dev);
4132again:
Ilya Dryomov637cd062019-06-06 17:14:49 +02004133 ret = rbd_try_acquire_lock(rbd_dev);
4134 if (ret <= 0) {
4135 dout("%s rbd_dev %p ret %d - done\n", __func__, rbd_dev, ret);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004136 return;
4137 }
4138
4139 ret = rbd_request_lock(rbd_dev);
4140 if (ret == -ETIMEDOUT) {
4141 goto again; /* treat this as a dead client */
Ilya Dryomove010dd02017-04-13 12:17:39 +02004142 } else if (ret == -EROFS) {
4143 rbd_warn(rbd_dev, "peer will not release lock");
Ilya Dryomov637cd062019-06-06 17:14:49 +02004144 down_write(&rbd_dev->lock_rwsem);
4145 wake_lock_waiters(rbd_dev, ret);
4146 up_write(&rbd_dev->lock_rwsem);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004147 } else if (ret < 0) {
4148 rbd_warn(rbd_dev, "error requesting lock: %d", ret);
4149 mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork,
4150 RBD_RETRY_DELAY);
4151 } else {
4152 /*
4153 * lock owner acked, but resend if we don't see them
4154 * release the lock
4155 */
Colin Ian King6b0a8772019-11-07 22:36:46 +00004156 dout("%s rbd_dev %p requeuing lock_dwork\n", __func__,
Ilya Dryomoved95b212016-08-12 16:40:02 +02004157 rbd_dev);
4158 mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork,
4159 msecs_to_jiffies(2 * RBD_NOTIFY_TIMEOUT * MSEC_PER_SEC));
4160 }
4161}
4162
Ilya Dryomova2b1da02019-05-30 11:15:23 +02004163static bool rbd_quiesce_lock(struct rbd_device *rbd_dev)
Ilya Dryomoved95b212016-08-12 16:40:02 +02004164{
Ilya Dryomove1fddc82019-05-30 16:07:48 +02004165 bool need_wait;
4166
Ilya Dryomova2b1da02019-05-30 11:15:23 +02004167 dout("%s rbd_dev %p\n", __func__, rbd_dev);
Linus Torvaldsd9b9c892019-07-18 11:05:25 -07004168 lockdep_assert_held_write(&rbd_dev->lock_rwsem);
Ilya Dryomova2b1da02019-05-30 11:15:23 +02004169
Ilya Dryomoved95b212016-08-12 16:40:02 +02004170 if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED)
4171 return false;
4172
Ilya Dryomoved95b212016-08-12 16:40:02 +02004173 /*
4174 * Ensure that all in-flight IO is flushed.
Ilya Dryomoved95b212016-08-12 16:40:02 +02004175 */
Ilya Dryomove1fddc82019-05-30 16:07:48 +02004176 rbd_dev->lock_state = RBD_LOCK_STATE_RELEASING;
4177 rbd_assert(!completion_done(&rbd_dev->releasing_wait));
4178 need_wait = !list_empty(&rbd_dev->running_list);
4179 downgrade_write(&rbd_dev->lock_rwsem);
4180 if (need_wait)
4181 wait_for_completion(&rbd_dev->releasing_wait);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004182 up_read(&rbd_dev->lock_rwsem);
4183
4184 down_write(&rbd_dev->lock_rwsem);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004185 if (rbd_dev->lock_state != RBD_LOCK_STATE_RELEASING)
4186 return false;
4187
Ilya Dryomove1fddc82019-05-30 16:07:48 +02004188 rbd_assert(list_empty(&rbd_dev->running_list));
Ilya Dryomova2b1da02019-05-30 11:15:23 +02004189 return true;
4190}
4191
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02004192static void rbd_pre_release_action(struct rbd_device *rbd_dev)
4193{
4194 if (rbd_dev->header.features & RBD_FEATURE_OBJECT_MAP)
4195 rbd_object_map_close(rbd_dev);
4196}
4197
Ilya Dryomove1fddc82019-05-30 16:07:48 +02004198static void __rbd_release_lock(struct rbd_device *rbd_dev)
4199{
4200 rbd_assert(list_empty(&rbd_dev->running_list));
4201
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02004202 rbd_pre_release_action(rbd_dev);
Ilya Dryomovbbead742017-04-13 12:17:38 +02004203 rbd_unlock(rbd_dev);
Ilya Dryomove1fddc82019-05-30 16:07:48 +02004204}
4205
Ilya Dryomova2b1da02019-05-30 11:15:23 +02004206/*
4207 * lock_rwsem must be held for write
4208 */
4209static void rbd_release_lock(struct rbd_device *rbd_dev)
4210{
4211 if (!rbd_quiesce_lock(rbd_dev))
4212 return;
4213
Ilya Dryomove1fddc82019-05-30 16:07:48 +02004214 __rbd_release_lock(rbd_dev);
Ilya Dryomova2b1da02019-05-30 11:15:23 +02004215
Ilya Dryomovbbead742017-04-13 12:17:38 +02004216 /*
4217 * Give others a chance to grab the lock - we would re-acquire
Ilya Dryomov637cd062019-06-06 17:14:49 +02004218 * almost immediately if we got new IO while draining the running
4219 * list otherwise. We need to ack our own notifications, so this
4220 * lock_dwork will be requeued from rbd_handle_released_lock() by
4221 * way of maybe_kick_acquire().
Ilya Dryomovbbead742017-04-13 12:17:38 +02004222 */
4223 cancel_delayed_work(&rbd_dev->lock_dwork);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004224}
4225
4226static void rbd_release_lock_work(struct work_struct *work)
4227{
4228 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
4229 unlock_work);
4230
4231 down_write(&rbd_dev->lock_rwsem);
4232 rbd_release_lock(rbd_dev);
4233 up_write(&rbd_dev->lock_rwsem);
4234}
4235
Ilya Dryomov637cd062019-06-06 17:14:49 +02004236static void maybe_kick_acquire(struct rbd_device *rbd_dev)
4237{
4238 bool have_requests;
4239
4240 dout("%s rbd_dev %p\n", __func__, rbd_dev);
4241 if (__rbd_is_lock_owner(rbd_dev))
4242 return;
4243
4244 spin_lock(&rbd_dev->lock_lists_lock);
4245 have_requests = !list_empty(&rbd_dev->acquiring_list);
4246 spin_unlock(&rbd_dev->lock_lists_lock);
4247 if (have_requests || delayed_work_pending(&rbd_dev->lock_dwork)) {
4248 dout("%s rbd_dev %p kicking lock_dwork\n", __func__, rbd_dev);
4249 mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0);
4250 }
4251}
4252
Ilya Dryomoved95b212016-08-12 16:40:02 +02004253static void rbd_handle_acquired_lock(struct rbd_device *rbd_dev, u8 struct_v,
4254 void **p)
4255{
4256 struct rbd_client_id cid = { 0 };
4257
4258 if (struct_v >= 2) {
4259 cid.gid = ceph_decode_64(p);
4260 cid.handle = ceph_decode_64(p);
4261 }
4262
4263 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
4264 cid.handle);
4265 if (!rbd_cid_equal(&cid, &rbd_empty_cid)) {
4266 down_write(&rbd_dev->lock_rwsem);
4267 if (rbd_cid_equal(&cid, &rbd_dev->owner_cid)) {
4268 /*
4269 * we already know that the remote client is
4270 * the owner
4271 */
4272 up_write(&rbd_dev->lock_rwsem);
4273 return;
4274 }
4275
4276 rbd_set_owner_cid(rbd_dev, &cid);
4277 downgrade_write(&rbd_dev->lock_rwsem);
4278 } else {
4279 down_read(&rbd_dev->lock_rwsem);
4280 }
4281
Ilya Dryomov637cd062019-06-06 17:14:49 +02004282 maybe_kick_acquire(rbd_dev);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004283 up_read(&rbd_dev->lock_rwsem);
4284}
4285
4286static void rbd_handle_released_lock(struct rbd_device *rbd_dev, u8 struct_v,
4287 void **p)
4288{
4289 struct rbd_client_id cid = { 0 };
4290
4291 if (struct_v >= 2) {
4292 cid.gid = ceph_decode_64(p);
4293 cid.handle = ceph_decode_64(p);
4294 }
4295
4296 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
4297 cid.handle);
4298 if (!rbd_cid_equal(&cid, &rbd_empty_cid)) {
4299 down_write(&rbd_dev->lock_rwsem);
4300 if (!rbd_cid_equal(&cid, &rbd_dev->owner_cid)) {
4301 dout("%s rbd_dev %p unexpected owner, cid %llu-%llu != owner_cid %llu-%llu\n",
4302 __func__, rbd_dev, cid.gid, cid.handle,
4303 rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle);
4304 up_write(&rbd_dev->lock_rwsem);
4305 return;
4306 }
4307
4308 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
4309 downgrade_write(&rbd_dev->lock_rwsem);
4310 } else {
4311 down_read(&rbd_dev->lock_rwsem);
4312 }
4313
Ilya Dryomov637cd062019-06-06 17:14:49 +02004314 maybe_kick_acquire(rbd_dev);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004315 up_read(&rbd_dev->lock_rwsem);
4316}
4317
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02004318/*
4319 * Returns result for ResponseMessage to be encoded (<= 0), or 1 if no
4320 * ResponseMessage is needed.
4321 */
4322static int rbd_handle_request_lock(struct rbd_device *rbd_dev, u8 struct_v,
4323 void **p)
Ilya Dryomoved95b212016-08-12 16:40:02 +02004324{
4325 struct rbd_client_id my_cid = rbd_get_cid(rbd_dev);
4326 struct rbd_client_id cid = { 0 };
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02004327 int result = 1;
Ilya Dryomoved95b212016-08-12 16:40:02 +02004328
4329 if (struct_v >= 2) {
4330 cid.gid = ceph_decode_64(p);
4331 cid.handle = ceph_decode_64(p);
4332 }
4333
4334 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
4335 cid.handle);
4336 if (rbd_cid_equal(&cid, &my_cid))
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02004337 return result;
Ilya Dryomoved95b212016-08-12 16:40:02 +02004338
4339 down_read(&rbd_dev->lock_rwsem);
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02004340 if (__rbd_is_lock_owner(rbd_dev)) {
4341 if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED &&
4342 rbd_cid_equal(&rbd_dev->owner_cid, &rbd_empty_cid))
4343 goto out_unlock;
4344
4345 /*
4346 * encode ResponseMessage(0) so the peer can detect
4347 * a missing owner
4348 */
4349 result = 0;
4350
4351 if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) {
Ilya Dryomove010dd02017-04-13 12:17:39 +02004352 if (!rbd_dev->opts->exclusive) {
4353 dout("%s rbd_dev %p queueing unlock_work\n",
4354 __func__, rbd_dev);
4355 queue_work(rbd_dev->task_wq,
4356 &rbd_dev->unlock_work);
4357 } else {
4358 /* refuse to release the lock */
4359 result = -EROFS;
4360 }
Ilya Dryomoved95b212016-08-12 16:40:02 +02004361 }
4362 }
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02004363
4364out_unlock:
Ilya Dryomoved95b212016-08-12 16:40:02 +02004365 up_read(&rbd_dev->lock_rwsem);
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02004366 return result;
Ilya Dryomoved95b212016-08-12 16:40:02 +02004367}
4368
4369static void __rbd_acknowledge_notify(struct rbd_device *rbd_dev,
4370 u64 notify_id, u64 cookie, s32 *result)
4371{
4372 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Kyle Spiers08a79102018-03-17 09:44:01 -07004373 char buf[4 + CEPH_ENCODING_START_BLK_LEN];
4374 int buf_size = sizeof(buf);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004375 int ret;
4376
4377 if (result) {
4378 void *p = buf;
4379
4380 /* encode ResponseMessage */
4381 ceph_start_encoding(&p, 1, 1,
4382 buf_size - CEPH_ENCODING_START_BLK_LEN);
4383 ceph_encode_32(&p, *result);
4384 } else {
4385 buf_size = 0;
4386 }
4387
4388 ret = ceph_osdc_notify_ack(osdc, &rbd_dev->header_oid,
4389 &rbd_dev->header_oloc, notify_id, cookie,
4390 buf, buf_size);
4391 if (ret)
4392 rbd_warn(rbd_dev, "acknowledge_notify failed: %d", ret);
4393}
4394
4395static void rbd_acknowledge_notify(struct rbd_device *rbd_dev, u64 notify_id,
4396 u64 cookie)
4397{
4398 dout("%s rbd_dev %p\n", __func__, rbd_dev);
4399 __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, NULL);
4400}
4401
4402static void rbd_acknowledge_notify_result(struct rbd_device *rbd_dev,
4403 u64 notify_id, u64 cookie, s32 result)
4404{
4405 dout("%s rbd_dev %p result %d\n", __func__, rbd_dev, result);
4406 __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, &result);
4407}
Ilya Dryomov922dab62016-05-26 01:15:02 +02004408
4409static void rbd_watch_cb(void *arg, u64 notify_id, u64 cookie,
4410 u64 notifier_id, void *data, size_t data_len)
Alex Elderb8d70032012-11-30 17:53:04 -06004411{
Ilya Dryomov922dab62016-05-26 01:15:02 +02004412 struct rbd_device *rbd_dev = arg;
Ilya Dryomoved95b212016-08-12 16:40:02 +02004413 void *p = data;
4414 void *const end = p + data_len;
Ilya Dryomovd4c22692016-09-06 11:15:48 +02004415 u8 struct_v = 0;
Ilya Dryomoved95b212016-08-12 16:40:02 +02004416 u32 len;
4417 u32 notify_op;
Alex Elderb8d70032012-11-30 17:53:04 -06004418 int ret;
4419
Ilya Dryomoved95b212016-08-12 16:40:02 +02004420 dout("%s rbd_dev %p cookie %llu notify_id %llu data_len %zu\n",
4421 __func__, rbd_dev, cookie, notify_id, data_len);
4422 if (data_len) {
4423 ret = ceph_start_decoding(&p, end, 1, "NotifyMessage",
4424 &struct_v, &len);
4425 if (ret) {
4426 rbd_warn(rbd_dev, "failed to decode NotifyMessage: %d",
4427 ret);
4428 return;
4429 }
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04004430
Ilya Dryomoved95b212016-08-12 16:40:02 +02004431 notify_op = ceph_decode_32(&p);
4432 } else {
4433 /* legacy notification for header updates */
4434 notify_op = RBD_NOTIFY_OP_HEADER_UPDATE;
4435 len = 0;
4436 }
Alex Elderb8d70032012-11-30 17:53:04 -06004437
Ilya Dryomoved95b212016-08-12 16:40:02 +02004438 dout("%s rbd_dev %p notify_op %u\n", __func__, rbd_dev, notify_op);
4439 switch (notify_op) {
4440 case RBD_NOTIFY_OP_ACQUIRED_LOCK:
4441 rbd_handle_acquired_lock(rbd_dev, struct_v, &p);
4442 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
4443 break;
4444 case RBD_NOTIFY_OP_RELEASED_LOCK:
4445 rbd_handle_released_lock(rbd_dev, struct_v, &p);
4446 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
4447 break;
4448 case RBD_NOTIFY_OP_REQUEST_LOCK:
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02004449 ret = rbd_handle_request_lock(rbd_dev, struct_v, &p);
4450 if (ret <= 0)
Ilya Dryomoved95b212016-08-12 16:40:02 +02004451 rbd_acknowledge_notify_result(rbd_dev, notify_id,
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02004452 cookie, ret);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004453 else
4454 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
4455 break;
4456 case RBD_NOTIFY_OP_HEADER_UPDATE:
4457 ret = rbd_dev_refresh(rbd_dev);
4458 if (ret)
4459 rbd_warn(rbd_dev, "refresh failed: %d", ret);
4460
4461 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
4462 break;
4463 default:
4464 if (rbd_is_lock_owner(rbd_dev))
4465 rbd_acknowledge_notify_result(rbd_dev, notify_id,
4466 cookie, -EOPNOTSUPP);
4467 else
4468 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
4469 break;
4470 }
Alex Elderb8d70032012-11-30 17:53:04 -06004471}
4472
Ilya Dryomov99d16942016-08-12 16:11:41 +02004473static void __rbd_unregister_watch(struct rbd_device *rbd_dev);
4474
Ilya Dryomov922dab62016-05-26 01:15:02 +02004475static void rbd_watch_errcb(void *arg, u64 cookie, int err)
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04004476{
Ilya Dryomov922dab62016-05-26 01:15:02 +02004477 struct rbd_device *rbd_dev = arg;
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04004478
Ilya Dryomov922dab62016-05-26 01:15:02 +02004479 rbd_warn(rbd_dev, "encountered watch error: %d", err);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04004480
Ilya Dryomoved95b212016-08-12 16:40:02 +02004481 down_write(&rbd_dev->lock_rwsem);
4482 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
4483 up_write(&rbd_dev->lock_rwsem);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04004484
Ilya Dryomov99d16942016-08-12 16:11:41 +02004485 mutex_lock(&rbd_dev->watch_mutex);
4486 if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) {
4487 __rbd_unregister_watch(rbd_dev);
4488 rbd_dev->watch_state = RBD_WATCH_STATE_ERROR;
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04004489
Ilya Dryomov99d16942016-08-12 16:11:41 +02004490 queue_delayed_work(rbd_dev->task_wq, &rbd_dev->watch_dwork, 0);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04004491 }
Ilya Dryomov99d16942016-08-12 16:11:41 +02004492 mutex_unlock(&rbd_dev->watch_mutex);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04004493}
4494
4495/*
Ilya Dryomov99d16942016-08-12 16:11:41 +02004496 * watch_mutex must be locked
Alex Elder9969ebc2013-01-18 12:31:10 -06004497 */
Ilya Dryomov99d16942016-08-12 16:11:41 +02004498static int __rbd_register_watch(struct rbd_device *rbd_dev)
Alex Elder9969ebc2013-01-18 12:31:10 -06004499{
4500 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Ilya Dryomov922dab62016-05-26 01:15:02 +02004501 struct ceph_osd_linger_request *handle;
Alex Elder9969ebc2013-01-18 12:31:10 -06004502
Ilya Dryomov922dab62016-05-26 01:15:02 +02004503 rbd_assert(!rbd_dev->watch_handle);
Ilya Dryomov99d16942016-08-12 16:11:41 +02004504 dout("%s rbd_dev %p\n", __func__, rbd_dev);
Alex Elder9969ebc2013-01-18 12:31:10 -06004505
Ilya Dryomov922dab62016-05-26 01:15:02 +02004506 handle = ceph_osdc_watch(osdc, &rbd_dev->header_oid,
4507 &rbd_dev->header_oloc, rbd_watch_cb,
4508 rbd_watch_errcb, rbd_dev);
4509 if (IS_ERR(handle))
4510 return PTR_ERR(handle);
Alex Elder9969ebc2013-01-18 12:31:10 -06004511
Ilya Dryomov922dab62016-05-26 01:15:02 +02004512 rbd_dev->watch_handle = handle;
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04004513 return 0;
Alex Elder9969ebc2013-01-18 12:31:10 -06004514}
4515
Ilya Dryomov99d16942016-08-12 16:11:41 +02004516/*
4517 * watch_mutex must be locked
4518 */
4519static void __rbd_unregister_watch(struct rbd_device *rbd_dev)
Ilya Dryomovfca27062013-12-16 18:02:40 +02004520{
Ilya Dryomov922dab62016-05-26 01:15:02 +02004521 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
4522 int ret;
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04004523
Ilya Dryomov99d16942016-08-12 16:11:41 +02004524 rbd_assert(rbd_dev->watch_handle);
4525 dout("%s rbd_dev %p\n", __func__, rbd_dev);
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04004526
Ilya Dryomov922dab62016-05-26 01:15:02 +02004527 ret = ceph_osdc_unwatch(osdc, rbd_dev->watch_handle);
4528 if (ret)
4529 rbd_warn(rbd_dev, "failed to unwatch: %d", ret);
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04004530
Ilya Dryomov922dab62016-05-26 01:15:02 +02004531 rbd_dev->watch_handle = NULL;
Ilya Dryomovc525f032016-04-28 16:07:26 +02004532}
4533
Ilya Dryomov99d16942016-08-12 16:11:41 +02004534static int rbd_register_watch(struct rbd_device *rbd_dev)
Ilya Dryomovc525f032016-04-28 16:07:26 +02004535{
Ilya Dryomov99d16942016-08-12 16:11:41 +02004536 int ret;
Ilya Dryomov811c6682016-04-15 16:22:16 +02004537
Ilya Dryomov99d16942016-08-12 16:11:41 +02004538 mutex_lock(&rbd_dev->watch_mutex);
4539 rbd_assert(rbd_dev->watch_state == RBD_WATCH_STATE_UNREGISTERED);
4540 ret = __rbd_register_watch(rbd_dev);
4541 if (ret)
4542 goto out;
4543
4544 rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED;
4545 rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id;
4546
4547out:
4548 mutex_unlock(&rbd_dev->watch_mutex);
4549 return ret;
4550}
4551
4552static void cancel_tasks_sync(struct rbd_device *rbd_dev)
4553{
4554 dout("%s rbd_dev %p\n", __func__, rbd_dev);
4555
Ilya Dryomoved95b212016-08-12 16:40:02 +02004556 cancel_work_sync(&rbd_dev->acquired_lock_work);
4557 cancel_work_sync(&rbd_dev->released_lock_work);
4558 cancel_delayed_work_sync(&rbd_dev->lock_dwork);
4559 cancel_work_sync(&rbd_dev->unlock_work);
Ilya Dryomov99d16942016-08-12 16:11:41 +02004560}
4561
4562static void rbd_unregister_watch(struct rbd_device *rbd_dev)
4563{
4564 cancel_tasks_sync(rbd_dev);
4565
4566 mutex_lock(&rbd_dev->watch_mutex);
4567 if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED)
4568 __rbd_unregister_watch(rbd_dev);
4569 rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED;
4570 mutex_unlock(&rbd_dev->watch_mutex);
4571
Dongsheng Yang23edca82018-06-04 06:24:37 -04004572 cancel_delayed_work_sync(&rbd_dev->watch_dwork);
Ilya Dryomov811c6682016-04-15 16:22:16 +02004573 ceph_osdc_flush_notifies(&rbd_dev->rbd_client->client->osdc);
Ilya Dryomovfca27062013-12-16 18:02:40 +02004574}
4575
Ilya Dryomov14bb2112017-04-13 12:17:38 +02004576/*
4577 * lock_rwsem must be held for write
4578 */
4579static void rbd_reacquire_lock(struct rbd_device *rbd_dev)
4580{
4581 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
4582 char cookie[32];
4583 int ret;
4584
Ilya Dryomova2b1da02019-05-30 11:15:23 +02004585 if (!rbd_quiesce_lock(rbd_dev))
4586 return;
Ilya Dryomov14bb2112017-04-13 12:17:38 +02004587
4588 format_lock_cookie(rbd_dev, cookie);
4589 ret = ceph_cls_set_cookie(osdc, &rbd_dev->header_oid,
4590 &rbd_dev->header_oloc, RBD_LOCK_NAME,
4591 CEPH_CLS_LOCK_EXCLUSIVE, rbd_dev->lock_cookie,
4592 RBD_LOCK_TAG, cookie);
4593 if (ret) {
4594 if (ret != -EOPNOTSUPP)
4595 rbd_warn(rbd_dev, "failed to update lock cookie: %d",
4596 ret);
4597
4598 /*
4599 * Lock cookie cannot be updated on older OSDs, so do
4600 * a manual release and queue an acquire.
4601 */
Ilya Dryomove1fddc82019-05-30 16:07:48 +02004602 __rbd_release_lock(rbd_dev);
Ilya Dryomova2b1da02019-05-30 11:15:23 +02004603 queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0);
Ilya Dryomov14bb2112017-04-13 12:17:38 +02004604 } else {
Florian Margaineedd8ca82017-12-13 16:43:59 +01004605 __rbd_lock(rbd_dev, cookie);
Ilya Dryomov637cd062019-06-06 17:14:49 +02004606 wake_lock_waiters(rbd_dev, 0);
Ilya Dryomov14bb2112017-04-13 12:17:38 +02004607 }
4608}
4609
Ilya Dryomov99d16942016-08-12 16:11:41 +02004610static void rbd_reregister_watch(struct work_struct *work)
4611{
4612 struct rbd_device *rbd_dev = container_of(to_delayed_work(work),
4613 struct rbd_device, watch_dwork);
4614 int ret;
4615
4616 dout("%s rbd_dev %p\n", __func__, rbd_dev);
4617
4618 mutex_lock(&rbd_dev->watch_mutex);
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02004619 if (rbd_dev->watch_state != RBD_WATCH_STATE_ERROR) {
4620 mutex_unlock(&rbd_dev->watch_mutex);
Ilya Dryomov14bb2112017-04-13 12:17:38 +02004621 return;
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02004622 }
Ilya Dryomov99d16942016-08-12 16:11:41 +02004623
4624 ret = __rbd_register_watch(rbd_dev);
4625 if (ret) {
4626 rbd_warn(rbd_dev, "failed to reregister watch: %d", ret);
Ilya Dryomov637cd062019-06-06 17:14:49 +02004627 if (ret != -EBLACKLISTED && ret != -ENOENT) {
Ilya Dryomov99d16942016-08-12 16:11:41 +02004628 queue_delayed_work(rbd_dev->task_wq,
4629 &rbd_dev->watch_dwork,
4630 RBD_RETRY_DELAY);
Ilya Dryomov637cd062019-06-06 17:14:49 +02004631 mutex_unlock(&rbd_dev->watch_mutex);
4632 return;
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02004633 }
Ilya Dryomov637cd062019-06-06 17:14:49 +02004634
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02004635 mutex_unlock(&rbd_dev->watch_mutex);
Ilya Dryomov637cd062019-06-06 17:14:49 +02004636 down_write(&rbd_dev->lock_rwsem);
4637 wake_lock_waiters(rbd_dev, ret);
4638 up_write(&rbd_dev->lock_rwsem);
Ilya Dryomov14bb2112017-04-13 12:17:38 +02004639 return;
Ilya Dryomov99d16942016-08-12 16:11:41 +02004640 }
4641
4642 rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED;
4643 rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id;
4644 mutex_unlock(&rbd_dev->watch_mutex);
4645
Ilya Dryomov14bb2112017-04-13 12:17:38 +02004646 down_write(&rbd_dev->lock_rwsem);
4647 if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED)
4648 rbd_reacquire_lock(rbd_dev);
4649 up_write(&rbd_dev->lock_rwsem);
4650
Ilya Dryomov99d16942016-08-12 16:11:41 +02004651 ret = rbd_dev_refresh(rbd_dev);
4652 if (ret)
Colin Ian Kingf6870cc2018-03-19 13:33:10 +00004653 rbd_warn(rbd_dev, "reregistration refresh failed: %d", ret);
Ilya Dryomov99d16942016-08-12 16:11:41 +02004654}
4655
Alex Elder36be9a72013-01-19 00:30:28 -06004656/*
Alex Elderf40eb342013-04-25 15:09:42 -05004657 * Synchronous osd object method call. Returns the number of bytes
4658 * returned in the outbound buffer, or a negative error code.
Alex Elder36be9a72013-01-19 00:30:28 -06004659 */
4660static int rbd_obj_method_sync(struct rbd_device *rbd_dev,
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004661 struct ceph_object_id *oid,
4662 struct ceph_object_locator *oloc,
Alex Elder36be9a72013-01-19 00:30:28 -06004663 const char *method_name,
Alex Elder41579762013-04-21 12:14:45 -05004664 const void *outbound,
Alex Elder36be9a72013-01-19 00:30:28 -06004665 size_t outbound_size,
Alex Elder41579762013-04-21 12:14:45 -05004666 void *inbound,
Alex Eldere2a58ee2013-04-30 00:44:33 -05004667 size_t inbound_size)
Alex Elder36be9a72013-01-19 00:30:28 -06004668{
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004669 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
4670 struct page *req_page = NULL;
4671 struct page *reply_page;
Alex Elder36be9a72013-01-19 00:30:28 -06004672 int ret;
4673
4674 /*
Alex Elder6010a452013-04-05 01:27:11 -05004675 * Method calls are ultimately read operations. The result
4676 * should placed into the inbound buffer provided. They
4677 * also supply outbound data--parameters for the object
4678 * method. Currently if this is present it will be a
4679 * snapshot id.
Alex Elder36be9a72013-01-19 00:30:28 -06004680 */
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004681 if (outbound) {
4682 if (outbound_size > PAGE_SIZE)
4683 return -E2BIG;
Alex Elder36be9a72013-01-19 00:30:28 -06004684
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004685 req_page = alloc_page(GFP_KERNEL);
4686 if (!req_page)
4687 return -ENOMEM;
Alex Elder36be9a72013-01-19 00:30:28 -06004688
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004689 memcpy(page_address(req_page), outbound, outbound_size);
Alex Elder04017e22013-04-05 14:46:02 -05004690 }
Alex Elder430c28c2013-04-03 21:32:51 -05004691
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004692 reply_page = alloc_page(GFP_KERNEL);
4693 if (!reply_page) {
4694 if (req_page)
4695 __free_page(req_page);
4696 return -ENOMEM;
4697 }
Alex Elder36be9a72013-01-19 00:30:28 -06004698
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004699 ret = ceph_osdc_call(osdc, oid, oloc, RBD_DRV_NAME, method_name,
4700 CEPH_OSD_FLAG_READ, req_page, outbound_size,
Ilya Dryomov68ada912019-06-14 18:16:51 +02004701 &reply_page, &inbound_size);
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004702 if (!ret) {
4703 memcpy(inbound, page_address(reply_page), inbound_size);
4704 ret = inbound_size;
4705 }
Alex Elder57385b52013-04-21 12:14:45 -05004706
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004707 if (req_page)
4708 __free_page(req_page);
4709 __free_page(reply_page);
Alex Elder36be9a72013-01-19 00:30:28 -06004710 return ret;
4711}
4712
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004713static void rbd_queue_workfn(struct work_struct *work)
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004714{
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004715 struct request *rq = blk_mq_rq_from_pdu(work);
4716 struct rbd_device *rbd_dev = rq->q->queuedata;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004717 struct rbd_img_request *img_request;
Josh Durgin4e752f02014-04-08 11:12:11 -07004718 struct ceph_snap_context *snapc = NULL;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004719 u64 offset = (u64)blk_rq_pos(rq) << SECTOR_SHIFT;
4720 u64 length = blk_rq_bytes(rq);
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004721 enum obj_operation_type op_type;
Josh Durgin4e752f02014-04-08 11:12:11 -07004722 u64 mapping_size;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004723 int result;
4724
Christoph Hellwigaebf5262017-01-31 16:57:31 +01004725 switch (req_op(rq)) {
4726 case REQ_OP_DISCARD:
4727 op_type = OBJ_OP_DISCARD;
4728 break;
Ilya Dryomov6484cbe2019-01-29 12:46:25 +01004729 case REQ_OP_WRITE_ZEROES:
4730 op_type = OBJ_OP_ZEROOUT;
4731 break;
Christoph Hellwigaebf5262017-01-31 16:57:31 +01004732 case REQ_OP_WRITE:
4733 op_type = OBJ_OP_WRITE;
4734 break;
4735 case REQ_OP_READ:
4736 op_type = OBJ_OP_READ;
4737 break;
4738 default:
4739 dout("%s: non-fs request type %d\n", __func__, req_op(rq));
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004740 result = -EIO;
4741 goto err;
4742 }
4743
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004744 /* Ignore/skip any zero-length requests */
4745
4746 if (!length) {
4747 dout("%s: zero-length request\n", __func__);
4748 result = 0;
4749 goto err_rq;
4750 }
4751
Ilya Dryomovb948ad72019-11-08 15:26:51 +01004752 if (op_type != OBJ_OP_READ) {
4753 if (rbd_is_ro(rbd_dev)) {
4754 rbd_warn(rbd_dev, "%s on read-only mapping",
4755 obj_op_name(op_type));
4756 result = -EIO;
4757 goto err;
4758 }
4759 rbd_assert(!rbd_is_snap(rbd_dev));
Ilya Dryomovb91a7bd2019-05-03 17:27:03 +02004760 }
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004761
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004762 if (offset && length > U64_MAX - offset + 1) {
4763 rbd_warn(rbd_dev, "bad request range (%llu~%llu)", offset,
4764 length);
4765 result = -EINVAL;
4766 goto err_rq; /* Shouldn't happen */
4767 }
4768
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004769 blk_mq_start_request(rq);
4770
Josh Durgin4e752f02014-04-08 11:12:11 -07004771 down_read(&rbd_dev->header_rwsem);
4772 mapping_size = rbd_dev->mapping.size;
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004773 if (op_type != OBJ_OP_READ) {
Josh Durgin4e752f02014-04-08 11:12:11 -07004774 snapc = rbd_dev->header.snapc;
4775 ceph_get_snap_context(snapc);
4776 }
4777 up_read(&rbd_dev->header_rwsem);
4778
4779 if (offset + length > mapping_size) {
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004780 rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)", offset,
Josh Durgin4e752f02014-04-08 11:12:11 -07004781 length, mapping_size);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004782 result = -EIO;
4783 goto err_rq;
4784 }
4785
Ilya Dryomovdfd98752018-02-06 19:26:35 +01004786 img_request = rbd_img_request_create(rbd_dev, op_type, snapc);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004787 if (!img_request) {
4788 result = -ENOMEM;
Ilya Dryomov637cd062019-06-06 17:14:49 +02004789 goto err_rq;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004790 }
4791 img_request->rq = rq;
Ilya Dryomov70b16db2015-11-27 19:23:24 +01004792 snapc = NULL; /* img_request consumes a ref */
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004793
Ilya Dryomov21ed05a2019-08-30 17:31:06 +02004794 dout("%s rbd_dev %p img_req %p %s %llu~%llu\n", __func__, rbd_dev,
4795 img_request, obj_op_name(op_type), offset, length);
4796
Ilya Dryomov6484cbe2019-01-29 12:46:25 +01004797 if (op_type == OBJ_OP_DISCARD || op_type == OBJ_OP_ZEROOUT)
Ilya Dryomov5a237812018-02-06 19:26:34 +01004798 result = rbd_img_fill_nodata(img_request, offset, length);
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004799 else
Ilya Dryomov5a237812018-02-06 19:26:34 +01004800 result = rbd_img_fill_from_bio(img_request, offset, length,
4801 rq->bio);
Ilya Dryomov0192ce22019-05-16 15:06:56 +02004802 if (result)
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004803 goto err_img_request;
4804
Ilya Dryomove1fddc82019-05-30 16:07:48 +02004805 rbd_img_handle_request(img_request, 0);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004806 return;
4807
4808err_img_request:
4809 rbd_img_request_put(img_request);
4810err_rq:
4811 if (result)
4812 rbd_warn(rbd_dev, "%s %llx at %llx result %d",
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004813 obj_op_name(op_type), length, offset, result);
SF Markus Elfringe96a6502014-11-02 15:20:59 +01004814 ceph_put_snap_context(snapc);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004815err:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02004816 blk_mq_end_request(rq, errno_to_blk_status(result));
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004817}
4818
Christoph Hellwigfc17b652017-06-03 09:38:05 +02004819static blk_status_t rbd_queue_rq(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004820 const struct blk_mq_queue_data *bd)
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004821{
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004822 struct request *rq = bd->rq;
4823 struct work_struct *work = blk_mq_rq_to_pdu(rq);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004824
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004825 queue_work(rbd_wq, work);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02004826 return BLK_STS_OK;
Alex Elderbf0d5f502012-11-22 00:00:08 -06004827}
4828
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004829static void rbd_free_disk(struct rbd_device *rbd_dev)
4830{
Ilya Dryomov5769ed02017-04-13 12:17:38 +02004831 blk_cleanup_queue(rbd_dev->disk->queue);
4832 blk_mq_free_tag_set(&rbd_dev->tag_set);
4833 put_disk(rbd_dev->disk);
Alex Eldera0cab922013-04-25 23:15:08 -05004834 rbd_dev->disk = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004835}
4836
Alex Elder788e2df2013-01-17 12:25:27 -06004837static int rbd_obj_read_sync(struct rbd_device *rbd_dev,
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004838 struct ceph_object_id *oid,
4839 struct ceph_object_locator *oloc,
4840 void *buf, int buf_len)
Alex Elder788e2df2013-01-17 12:25:27 -06004841
4842{
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004843 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
4844 struct ceph_osd_request *req;
4845 struct page **pages;
4846 int num_pages = calc_pages_for(0, buf_len);
Alex Elder788e2df2013-01-17 12:25:27 -06004847 int ret;
4848
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004849 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL);
4850 if (!req)
4851 return -ENOMEM;
Alex Elder788e2df2013-01-17 12:25:27 -06004852
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004853 ceph_oid_copy(&req->r_base_oid, oid);
4854 ceph_oloc_copy(&req->r_base_oloc, oloc);
4855 req->r_flags = CEPH_OSD_FLAG_READ;
Alex Elder788e2df2013-01-17 12:25:27 -06004856
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004857 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
4858 if (IS_ERR(pages)) {
4859 ret = PTR_ERR(pages);
4860 goto out_req;
4861 }
Alex Elder1ceae7e2013-02-06 13:11:38 -06004862
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004863 osd_req_op_extent_init(req, 0, CEPH_OSD_OP_READ, 0, buf_len, 0, 0);
4864 osd_req_op_extent_osd_data_pages(req, 0, pages, buf_len, 0, false,
4865 true);
Alex Elder788e2df2013-01-17 12:25:27 -06004866
Ilya Dryomov26f887e2018-10-15 16:11:37 +02004867 ret = ceph_osdc_alloc_messages(req, GFP_KERNEL);
4868 if (ret)
4869 goto out_req;
4870
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004871 ceph_osdc_start_request(osdc, req, false);
4872 ret = ceph_osdc_wait_request(osdc, req);
4873 if (ret >= 0)
4874 ceph_copy_from_page_vector(pages, buf, 0, ret);
4875
4876out_req:
4877 ceph_osdc_put_request(req);
Alex Elder788e2df2013-01-17 12:25:27 -06004878 return ret;
4879}
4880
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004881/*
Alex Elder662518b2013-05-06 09:51:29 -05004882 * Read the complete header for the given rbd device. On successful
4883 * return, the rbd_dev->header field will contain up-to-date
4884 * information about the image.
Alex Elder4156d992012-08-02 11:29:46 -05004885 */
Alex Elder99a41eb2013-05-06 09:51:30 -05004886static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev)
Alex Elder4156d992012-08-02 11:29:46 -05004887{
4888 struct rbd_image_header_ondisk *ondisk = NULL;
4889 u32 snap_count = 0;
4890 u64 names_size = 0;
4891 u32 want_count;
4892 int ret;
4893
4894 /*
4895 * The complete header will include an array of its 64-bit
4896 * snapshot ids, followed by the names of those snapshots as
4897 * a contiguous block of NUL-terminated strings. Note that
4898 * the number of snapshots could change by the time we read
4899 * it in, in which case we re-read it.
4900 */
4901 do {
4902 size_t size;
4903
4904 kfree(ondisk);
4905
4906 size = sizeof (*ondisk);
4907 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
4908 size += names_size;
4909 ondisk = kmalloc(size, GFP_KERNEL);
4910 if (!ondisk)
Alex Elder662518b2013-05-06 09:51:29 -05004911 return -ENOMEM;
Alex Elder4156d992012-08-02 11:29:46 -05004912
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004913 ret = rbd_obj_read_sync(rbd_dev, &rbd_dev->header_oid,
4914 &rbd_dev->header_oloc, ondisk, size);
Alex Elder4156d992012-08-02 11:29:46 -05004915 if (ret < 0)
Alex Elder662518b2013-05-06 09:51:29 -05004916 goto out;
Alex Elderc0cd10db2013-04-26 09:43:47 -05004917 if ((size_t)ret < size) {
Alex Elder4156d992012-08-02 11:29:46 -05004918 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05004919 rbd_warn(rbd_dev, "short header read (want %zd got %d)",
4920 size, ret);
Alex Elder662518b2013-05-06 09:51:29 -05004921 goto out;
Alex Elder4156d992012-08-02 11:29:46 -05004922 }
4923 if (!rbd_dev_ondisk_valid(ondisk)) {
4924 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05004925 rbd_warn(rbd_dev, "invalid header");
Alex Elder662518b2013-05-06 09:51:29 -05004926 goto out;
Alex Elder4156d992012-08-02 11:29:46 -05004927 }
4928
4929 names_size = le64_to_cpu(ondisk->snap_names_len);
4930 want_count = snap_count;
4931 snap_count = le32_to_cpu(ondisk->snap_count);
4932 } while (snap_count != want_count);
4933
Alex Elder662518b2013-05-06 09:51:29 -05004934 ret = rbd_header_from_disk(rbd_dev, ondisk);
4935out:
Alex Elder4156d992012-08-02 11:29:46 -05004936 kfree(ondisk);
4937
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004938 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004939}
4940
Josh Durgin98752012013-08-29 17:26:31 -07004941static void rbd_dev_update_size(struct rbd_device *rbd_dev)
4942{
4943 sector_t size;
Josh Durgin98752012013-08-29 17:26:31 -07004944
4945 /*
Ilya Dryomov811c6682016-04-15 16:22:16 +02004946 * If EXISTS is not set, rbd_dev->disk may be NULL, so don't
4947 * try to update its size. If REMOVING is set, updating size
4948 * is just useless work since the device can't be opened.
Josh Durgin98752012013-08-29 17:26:31 -07004949 */
Ilya Dryomov811c6682016-04-15 16:22:16 +02004950 if (test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags) &&
4951 !test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) {
Josh Durgin98752012013-08-29 17:26:31 -07004952 size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE;
4953 dout("setting size to %llu sectors", (unsigned long long)size);
4954 set_capacity(rbd_dev->disk, size);
4955 revalidate_disk(rbd_dev->disk);
4956 }
4957}
4958
Alex Eldercc4a38bd2013-04-30 00:44:33 -05004959static int rbd_dev_refresh(struct rbd_device *rbd_dev)
Alex Elder1fe5e992012-07-25 09:32:41 -05004960{
Alex Eldere627db02013-05-06 07:40:30 -05004961 u64 mapping_size;
Alex Elder1fe5e992012-07-25 09:32:41 -05004962 int ret;
4963
Alex Eldercfbf6372013-05-31 17:40:45 -05004964 down_write(&rbd_dev->header_rwsem);
Alex Elder3b5cf2a2013-05-29 11:18:59 -05004965 mapping_size = rbd_dev->mapping.size;
Ilya Dryomova720ae02014-07-23 17:11:19 +04004966
4967 ret = rbd_dev_header_info(rbd_dev);
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04004968 if (ret)
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004969 goto out;
Alex Elder15228ed2013-05-01 12:43:03 -05004970
Ilya Dryomove8f59b52014-07-24 10:42:13 +04004971 /*
4972 * If there is a parent, see if it has disappeared due to the
4973 * mapped image getting flattened.
4974 */
4975 if (rbd_dev->parent) {
4976 ret = rbd_dev_v2_parent_info(rbd_dev);
4977 if (ret)
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004978 goto out;
Ilya Dryomove8f59b52014-07-24 10:42:13 +04004979 }
4980
Ilya Dryomov686238b2019-11-18 12:51:02 +01004981 rbd_assert(!rbd_is_snap(rbd_dev));
4982 rbd_dev->mapping.size = rbd_dev->header.image_size;
Alex Elder15228ed2013-05-01 12:43:03 -05004983
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004984out:
Alex Eldercfbf6372013-05-31 17:40:45 -05004985 up_write(&rbd_dev->header_rwsem);
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004986 if (!ret && mapping_size != rbd_dev->mapping.size)
Josh Durgin98752012013-08-29 17:26:31 -07004987 rbd_dev_update_size(rbd_dev);
Alex Elder1fe5e992012-07-25 09:32:41 -05004988
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004989 return ret;
Alex Elder1fe5e992012-07-25 09:32:41 -05004990}
4991
Christoph Hellwigd6296d392017-05-01 10:19:08 -06004992static int rbd_init_request(struct blk_mq_tag_set *set, struct request *rq,
4993 unsigned int hctx_idx, unsigned int numa_node)
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004994{
4995 struct work_struct *work = blk_mq_rq_to_pdu(rq);
4996
4997 INIT_WORK(work, rbd_queue_workfn);
4998 return 0;
4999}
5000
Eric Biggersf363b082017-03-30 13:39:16 -07005001static const struct blk_mq_ops rbd_mq_ops = {
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01005002 .queue_rq = rbd_queue_rq,
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01005003 .init_request = rbd_init_request,
5004};
5005
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005006static int rbd_init_disk(struct rbd_device *rbd_dev)
5007{
5008 struct gendisk *disk;
5009 struct request_queue *q;
Ilya Dryomov420efbd2018-04-16 09:32:18 +02005010 unsigned int objset_bytes =
5011 rbd_dev->layout.object_size * rbd_dev->layout.stripe_count;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01005012 int err;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005013
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005014 /* create gendisk info */
Ilya Dryomov7e513d42013-12-16 19:26:32 +02005015 disk = alloc_disk(single_major ?
5016 (1 << RBD_SINGLE_MAJOR_PART_SHIFT) :
5017 RBD_MINORS_PER_MAJOR);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005018 if (!disk)
Alex Elder1fcdb8a2012-08-29 17:11:06 -05005019 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005020
Alex Elderf0f8cef2012-01-29 13:57:44 -06005021 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
Alex Elderde71a292012-07-03 16:01:19 -05005022 rbd_dev->dev_id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005023 disk->major = rbd_dev->major;
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02005024 disk->first_minor = rbd_dev->minor;
Ilya Dryomov7e513d42013-12-16 19:26:32 +02005025 if (single_major)
5026 disk->flags |= GENHD_FL_EXT_DEVT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005027 disk->fops = &rbd_bd_ops;
5028 disk->private_data = rbd_dev;
5029
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01005030 memset(&rbd_dev->tag_set, 0, sizeof(rbd_dev->tag_set));
5031 rbd_dev->tag_set.ops = &rbd_mq_ops;
Ilya Dryomovb5584182015-06-23 16:21:19 +03005032 rbd_dev->tag_set.queue_depth = rbd_dev->opts->queue_depth;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01005033 rbd_dev->tag_set.numa_node = NUMA_NO_NODE;
Ming Lei56d18f62019-02-15 19:13:24 +08005034 rbd_dev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01005035 rbd_dev->tag_set.nr_hw_queues = 1;
5036 rbd_dev->tag_set.cmd_size = sizeof(struct work_struct);
5037
5038 err = blk_mq_alloc_tag_set(&rbd_dev->tag_set);
5039 if (err)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005040 goto out_disk;
Josh Durgin029bcbd2011-07-22 11:35:23 -07005041
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01005042 q = blk_mq_init_queue(&rbd_dev->tag_set);
5043 if (IS_ERR(q)) {
5044 err = PTR_ERR(q);
5045 goto out_tag_set;
5046 }
5047
Bart Van Assche8b904b52018-03-07 17:10:10 -08005048 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
Ilya Dryomovd8a2c892015-03-24 16:15:17 +03005049 /* QUEUE_FLAG_ADD_RANDOM is off by default for blk-mq */
Alex Elder593a9e72012-02-07 12:03:37 -06005050
Ilya Dryomov420efbd2018-04-16 09:32:18 +02005051 blk_queue_max_hw_sectors(q, objset_bytes >> SECTOR_SHIFT);
Ilya Dryomov0d9fde42015-10-07 16:09:35 +02005052 q->limits.max_sectors = queue_max_hw_sectors(q);
Ilya Dryomov21acdf42017-12-21 15:35:11 +01005053 blk_queue_max_segments(q, USHRT_MAX);
Ilya Dryomov24f1df62018-01-12 17:22:10 +01005054 blk_queue_max_segment_size(q, UINT_MAX);
Ilya Dryomov16d80c52019-03-15 14:50:04 +01005055 blk_queue_io_min(q, rbd_dev->opts->alloc_size);
5056 blk_queue_io_opt(q, rbd_dev->opts->alloc_size);
Josh Durgin029bcbd2011-07-22 11:35:23 -07005057
Ilya Dryomovd9360542018-03-23 06:14:47 +01005058 if (rbd_dev->opts->trim) {
5059 blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
Ilya Dryomov16d80c52019-03-15 14:50:04 +01005060 q->limits.discard_granularity = rbd_dev->opts->alloc_size;
Ilya Dryomovd9360542018-03-23 06:14:47 +01005061 blk_queue_max_discard_sectors(q, objset_bytes >> SECTOR_SHIFT);
5062 blk_queue_max_write_zeroes_sectors(q, objset_bytes >> SECTOR_SHIFT);
5063 }
Guangliang Zhao90e98c52014-04-01 22:22:16 +08005064
Ronny Hegewaldbae818e2015-10-15 18:50:46 +00005065 if (!ceph_test_opt(rbd_dev->rbd_client->client, NOCRC))
Jan Karadc3b17c2017-02-02 15:56:50 +01005066 q->backing_dev_info->capabilities |= BDI_CAP_STABLE_WRITES;
Ronny Hegewaldbae818e2015-10-15 18:50:46 +00005067
Ilya Dryomov5769ed02017-04-13 12:17:38 +02005068 /*
5069 * disk_release() expects a queue ref from add_disk() and will
5070 * put it. Hold an extra ref until add_disk() is called.
5071 */
5072 WARN_ON(!blk_get_queue(q));
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005073 disk->queue = q;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005074 q->queuedata = rbd_dev;
5075
5076 rbd_dev->disk = disk;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005077
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005078 return 0;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01005079out_tag_set:
5080 blk_mq_free_tag_set(&rbd_dev->tag_set);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005081out_disk:
5082 put_disk(disk);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01005083 return err;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005084}
5085
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005086/*
5087 sysfs
5088*/
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005089
Alex Elder593a9e72012-02-07 12:03:37 -06005090static struct rbd_device *dev_to_rbd_dev(struct device *dev)
5091{
5092 return container_of(dev, struct rbd_device, dev);
5093}
5094
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005095static ssize_t rbd_size_show(struct device *dev,
5096 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005097{
Alex Elder593a9e72012-02-07 12:03:37 -06005098 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005099
Alex Elderfc71d832013-04-26 15:44:36 -05005100 return sprintf(buf, "%llu\n",
5101 (unsigned long long)rbd_dev->mapping.size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005102}
5103
Alex Elder34b13182012-07-13 20:35:12 -05005104static ssize_t rbd_features_show(struct device *dev,
5105 struct device_attribute *attr, char *buf)
5106{
5107 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
5108
Ilya Dryomovfa58bca2019-11-05 13:16:52 +01005109 return sprintf(buf, "0x%016llx\n", rbd_dev->header.features);
Alex Elder34b13182012-07-13 20:35:12 -05005110}
5111
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005112static ssize_t rbd_major_show(struct device *dev,
5113 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005114{
Alex Elder593a9e72012-02-07 12:03:37 -06005115 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005116
Alex Elderfc71d832013-04-26 15:44:36 -05005117 if (rbd_dev->major)
5118 return sprintf(buf, "%d\n", rbd_dev->major);
5119
5120 return sprintf(buf, "(none)\n");
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02005121}
Alex Elderfc71d832013-04-26 15:44:36 -05005122
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02005123static ssize_t rbd_minor_show(struct device *dev,
5124 struct device_attribute *attr, char *buf)
5125{
5126 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
5127
5128 return sprintf(buf, "%d\n", rbd_dev->minor);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005129}
5130
Ilya Dryomov005a07bf2016-08-18 18:38:43 +02005131static ssize_t rbd_client_addr_show(struct device *dev,
5132 struct device_attribute *attr, char *buf)
5133{
5134 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
5135 struct ceph_entity_addr *client_addr =
5136 ceph_client_addr(rbd_dev->rbd_client->client);
5137
5138 return sprintf(buf, "%pISpc/%u\n", &client_addr->in_addr,
5139 le32_to_cpu(client_addr->nonce));
5140}
5141
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005142static ssize_t rbd_client_id_show(struct device *dev,
5143 struct device_attribute *attr, char *buf)
5144{
Alex Elder593a9e72012-02-07 12:03:37 -06005145 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005146
Alex Elder1dbb4392012-01-24 10:08:37 -06005147 return sprintf(buf, "client%lld\n",
Ilya Dryomov033268a2016-08-12 14:59:58 +02005148 ceph_client_gid(rbd_dev->rbd_client->client));
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005149}
5150
Mike Christie267fb902016-08-18 18:38:43 +02005151static ssize_t rbd_cluster_fsid_show(struct device *dev,
5152 struct device_attribute *attr, char *buf)
5153{
5154 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
5155
5156 return sprintf(buf, "%pU\n", &rbd_dev->rbd_client->client->fsid);
5157}
5158
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02005159static ssize_t rbd_config_info_show(struct device *dev,
5160 struct device_attribute *attr, char *buf)
5161{
5162 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
5163
5164 return sprintf(buf, "%s\n", rbd_dev->config_info);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005165}
5166
5167static ssize_t rbd_pool_show(struct device *dev,
5168 struct device_attribute *attr, char *buf)
5169{
Alex Elder593a9e72012-02-07 12:03:37 -06005170 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005171
Alex Elder0d7dbfc2012-10-25 23:34:41 -05005172 return sprintf(buf, "%s\n", rbd_dev->spec->pool_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005173}
5174
Alex Elder9bb2f332012-07-12 10:46:35 -05005175static ssize_t rbd_pool_id_show(struct device *dev,
5176 struct device_attribute *attr, char *buf)
5177{
5178 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
5179
Alex Elder0d7dbfc2012-10-25 23:34:41 -05005180 return sprintf(buf, "%llu\n",
Alex Elderfc71d832013-04-26 15:44:36 -05005181 (unsigned long long) rbd_dev->spec->pool_id);
Alex Elder9bb2f332012-07-12 10:46:35 -05005182}
5183
Ilya Dryomovb26c0472018-07-03 15:28:43 +02005184static ssize_t rbd_pool_ns_show(struct device *dev,
5185 struct device_attribute *attr, char *buf)
5186{
5187 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
5188
5189 return sprintf(buf, "%s\n", rbd_dev->spec->pool_ns ?: "");
5190}
5191
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005192static ssize_t rbd_name_show(struct device *dev,
5193 struct device_attribute *attr, char *buf)
5194{
Alex Elder593a9e72012-02-07 12:03:37 -06005195 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005196
Alex Eldera92ffdf2012-10-30 19:40:33 -05005197 if (rbd_dev->spec->image_name)
5198 return sprintf(buf, "%s\n", rbd_dev->spec->image_name);
5199
5200 return sprintf(buf, "(unknown)\n");
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005201}
5202
Alex Elder589d30e2012-07-10 20:30:11 -05005203static ssize_t rbd_image_id_show(struct device *dev,
5204 struct device_attribute *attr, char *buf)
5205{
5206 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
5207
Alex Elder0d7dbfc2012-10-25 23:34:41 -05005208 return sprintf(buf, "%s\n", rbd_dev->spec->image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05005209}
5210
Alex Elder34b13182012-07-13 20:35:12 -05005211/*
5212 * Shows the name of the currently-mapped snapshot (or
5213 * RBD_SNAP_HEAD_NAME for the base image).
5214 */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005215static ssize_t rbd_snap_show(struct device *dev,
5216 struct device_attribute *attr,
5217 char *buf)
5218{
Alex Elder593a9e72012-02-07 12:03:37 -06005219 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005220
Alex Elder0d7dbfc2012-10-25 23:34:41 -05005221 return sprintf(buf, "%s\n", rbd_dev->spec->snap_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005222}
5223
Mike Christie92a58672016-08-18 18:38:44 +02005224static ssize_t rbd_snap_id_show(struct device *dev,
5225 struct device_attribute *attr, char *buf)
5226{
5227 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
5228
5229 return sprintf(buf, "%llu\n", rbd_dev->spec->snap_id);
5230}
5231
Alex Elder86b00e02012-10-25 23:34:42 -05005232/*
Ilya Dryomovff961282014-07-22 21:53:07 +04005233 * For a v2 image, shows the chain of parent images, separated by empty
5234 * lines. For v1 images or if there is no parent, shows "(no parent
5235 * image)".
Alex Elder86b00e02012-10-25 23:34:42 -05005236 */
5237static ssize_t rbd_parent_show(struct device *dev,
Ilya Dryomovff961282014-07-22 21:53:07 +04005238 struct device_attribute *attr,
5239 char *buf)
Alex Elder86b00e02012-10-25 23:34:42 -05005240{
5241 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Ilya Dryomovff961282014-07-22 21:53:07 +04005242 ssize_t count = 0;
Alex Elder86b00e02012-10-25 23:34:42 -05005243
Ilya Dryomovff961282014-07-22 21:53:07 +04005244 if (!rbd_dev->parent)
Alex Elder86b00e02012-10-25 23:34:42 -05005245 return sprintf(buf, "(no parent image)\n");
5246
Ilya Dryomovff961282014-07-22 21:53:07 +04005247 for ( ; rbd_dev->parent; rbd_dev = rbd_dev->parent) {
5248 struct rbd_spec *spec = rbd_dev->parent_spec;
Alex Elder86b00e02012-10-25 23:34:42 -05005249
Ilya Dryomovff961282014-07-22 21:53:07 +04005250 count += sprintf(&buf[count], "%s"
5251 "pool_id %llu\npool_name %s\n"
Ilya Dryomove92c0ea2018-08-22 17:26:10 +02005252 "pool_ns %s\n"
Ilya Dryomovff961282014-07-22 21:53:07 +04005253 "image_id %s\nimage_name %s\n"
5254 "snap_id %llu\nsnap_name %s\n"
5255 "overlap %llu\n",
5256 !count ? "" : "\n", /* first? */
5257 spec->pool_id, spec->pool_name,
Ilya Dryomove92c0ea2018-08-22 17:26:10 +02005258 spec->pool_ns ?: "",
Ilya Dryomovff961282014-07-22 21:53:07 +04005259 spec->image_id, spec->image_name ?: "(unknown)",
5260 spec->snap_id, spec->snap_name,
5261 rbd_dev->parent_overlap);
5262 }
Alex Elder86b00e02012-10-25 23:34:42 -05005263
Ilya Dryomovff961282014-07-22 21:53:07 +04005264 return count;
Alex Elder86b00e02012-10-25 23:34:42 -05005265}
5266
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005267static ssize_t rbd_image_refresh(struct device *dev,
5268 struct device_attribute *attr,
5269 const char *buf,
5270 size_t size)
5271{
Alex Elder593a9e72012-02-07 12:03:37 -06005272 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Alex Elderb8136232012-07-25 09:32:41 -05005273 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005274
Alex Eldercc4a38bd2013-04-30 00:44:33 -05005275 ret = rbd_dev_refresh(rbd_dev);
Alex Eldere627db02013-05-06 07:40:30 -05005276 if (ret)
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04005277 return ret;
Alex Elderb8136232012-07-25 09:32:41 -05005278
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04005279 return size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005280}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005281
Joe Perches5657a812018-05-24 13:38:59 -06005282static DEVICE_ATTR(size, 0444, rbd_size_show, NULL);
5283static DEVICE_ATTR(features, 0444, rbd_features_show, NULL);
5284static DEVICE_ATTR(major, 0444, rbd_major_show, NULL);
5285static DEVICE_ATTR(minor, 0444, rbd_minor_show, NULL);
5286static DEVICE_ATTR(client_addr, 0444, rbd_client_addr_show, NULL);
5287static DEVICE_ATTR(client_id, 0444, rbd_client_id_show, NULL);
5288static DEVICE_ATTR(cluster_fsid, 0444, rbd_cluster_fsid_show, NULL);
5289static DEVICE_ATTR(config_info, 0400, rbd_config_info_show, NULL);
5290static DEVICE_ATTR(pool, 0444, rbd_pool_show, NULL);
5291static DEVICE_ATTR(pool_id, 0444, rbd_pool_id_show, NULL);
Ilya Dryomovb26c0472018-07-03 15:28:43 +02005292static DEVICE_ATTR(pool_ns, 0444, rbd_pool_ns_show, NULL);
Joe Perches5657a812018-05-24 13:38:59 -06005293static DEVICE_ATTR(name, 0444, rbd_name_show, NULL);
5294static DEVICE_ATTR(image_id, 0444, rbd_image_id_show, NULL);
5295static DEVICE_ATTR(refresh, 0200, NULL, rbd_image_refresh);
5296static DEVICE_ATTR(current_snap, 0444, rbd_snap_show, NULL);
5297static DEVICE_ATTR(snap_id, 0444, rbd_snap_id_show, NULL);
5298static DEVICE_ATTR(parent, 0444, rbd_parent_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005299
5300static struct attribute *rbd_attrs[] = {
5301 &dev_attr_size.attr,
Alex Elder34b13182012-07-13 20:35:12 -05005302 &dev_attr_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005303 &dev_attr_major.attr,
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02005304 &dev_attr_minor.attr,
Ilya Dryomov005a07bf2016-08-18 18:38:43 +02005305 &dev_attr_client_addr.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005306 &dev_attr_client_id.attr,
Mike Christie267fb902016-08-18 18:38:43 +02005307 &dev_attr_cluster_fsid.attr,
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02005308 &dev_attr_config_info.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005309 &dev_attr_pool.attr,
Alex Elder9bb2f332012-07-12 10:46:35 -05005310 &dev_attr_pool_id.attr,
Ilya Dryomovb26c0472018-07-03 15:28:43 +02005311 &dev_attr_pool_ns.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005312 &dev_attr_name.attr,
Alex Elder589d30e2012-07-10 20:30:11 -05005313 &dev_attr_image_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005314 &dev_attr_current_snap.attr,
Mike Christie92a58672016-08-18 18:38:44 +02005315 &dev_attr_snap_id.attr,
Alex Elder86b00e02012-10-25 23:34:42 -05005316 &dev_attr_parent.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005317 &dev_attr_refresh.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005318 NULL
5319};
5320
5321static struct attribute_group rbd_attr_group = {
5322 .attrs = rbd_attrs,
5323};
5324
5325static const struct attribute_group *rbd_attr_groups[] = {
5326 &rbd_attr_group,
5327 NULL
5328};
5329
Ilya Dryomov6cac4692015-10-16 20:11:25 +02005330static void rbd_dev_release(struct device *dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005331
Bhumika Goyalb9942bc2017-02-11 12:14:38 +05305332static const struct device_type rbd_device_type = {
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005333 .name = "rbd",
5334 .groups = rbd_attr_groups,
Ilya Dryomov6cac4692015-10-16 20:11:25 +02005335 .release = rbd_dev_release,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005336};
5337
Alex Elder8b8fb992012-10-26 17:25:24 -05005338static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)
5339{
5340 kref_get(&spec->kref);
5341
5342 return spec;
5343}
5344
5345static void rbd_spec_free(struct kref *kref);
5346static void rbd_spec_put(struct rbd_spec *spec)
5347{
5348 if (spec)
5349 kref_put(&spec->kref, rbd_spec_free);
5350}
5351
5352static struct rbd_spec *rbd_spec_alloc(void)
5353{
5354 struct rbd_spec *spec;
5355
5356 spec = kzalloc(sizeof (*spec), GFP_KERNEL);
5357 if (!spec)
5358 return NULL;
Ilya Dryomov04077592014-07-23 17:11:20 +04005359
5360 spec->pool_id = CEPH_NOPOOL;
5361 spec->snap_id = CEPH_NOSNAP;
Alex Elder8b8fb992012-10-26 17:25:24 -05005362 kref_init(&spec->kref);
5363
Alex Elder8b8fb992012-10-26 17:25:24 -05005364 return spec;
5365}
5366
5367static void rbd_spec_free(struct kref *kref)
5368{
5369 struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref);
5370
5371 kfree(spec->pool_name);
Ilya Dryomovb26c0472018-07-03 15:28:43 +02005372 kfree(spec->pool_ns);
Alex Elder8b8fb992012-10-26 17:25:24 -05005373 kfree(spec->image_id);
5374 kfree(spec->image_name);
5375 kfree(spec->snap_name);
5376 kfree(spec);
5377}
5378
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02005379static void rbd_dev_free(struct rbd_device *rbd_dev)
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02005380{
Ilya Dryomov99d16942016-08-12 16:11:41 +02005381 WARN_ON(rbd_dev->watch_state != RBD_WATCH_STATE_UNREGISTERED);
Ilya Dryomoved95b212016-08-12 16:40:02 +02005382 WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_UNLOCKED);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02005383
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005384 ceph_oid_destroy(&rbd_dev->header_oid);
Ilya Dryomov6b6dddb2016-08-05 16:15:38 +02005385 ceph_oloc_destroy(&rbd_dev->header_oloc);
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02005386 kfree(rbd_dev->config_info);
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005387
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02005388 rbd_put_client(rbd_dev->rbd_client);
5389 rbd_spec_put(rbd_dev->spec);
5390 kfree(rbd_dev->opts);
5391 kfree(rbd_dev);
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02005392}
5393
5394static void rbd_dev_release(struct device *dev)
5395{
5396 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
5397 bool need_put = !!rbd_dev->opts;
5398
5399 if (need_put) {
5400 destroy_workqueue(rbd_dev->task_wq);
5401 ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id);
5402 }
5403
5404 rbd_dev_free(rbd_dev);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02005405
5406 /*
5407 * This is racy, but way better than putting module outside of
5408 * the release callback. The race window is pretty small, so
5409 * doing something similar to dm (dm-builtin.c) is overkill.
5410 */
5411 if (need_put)
5412 module_put(THIS_MODULE);
5413}
5414
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02005415static struct rbd_device *__rbd_dev_create(struct rbd_client *rbdc,
5416 struct rbd_spec *spec)
Alex Elderc53d5892012-10-25 23:34:42 -05005417{
5418 struct rbd_device *rbd_dev;
5419
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02005420 rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL);
Alex Elderc53d5892012-10-25 23:34:42 -05005421 if (!rbd_dev)
5422 return NULL;
5423
5424 spin_lock_init(&rbd_dev->lock);
5425 INIT_LIST_HEAD(&rbd_dev->node);
Alex Elderc53d5892012-10-25 23:34:42 -05005426 init_rwsem(&rbd_dev->header_rwsem);
5427
Ilya Dryomov7e973322017-01-25 18:16:22 +01005428 rbd_dev->header.data_pool_id = CEPH_NOPOOL;
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005429 ceph_oid_init(&rbd_dev->header_oid);
Ilya Dryomov431a02c2017-01-25 18:16:21 +01005430 rbd_dev->header_oloc.pool = spec->pool_id;
Ilya Dryomovb26c0472018-07-03 15:28:43 +02005431 if (spec->pool_ns) {
5432 WARN_ON(!*spec->pool_ns);
5433 rbd_dev->header_oloc.pool_ns =
5434 ceph_find_or_create_string(spec->pool_ns,
5435 strlen(spec->pool_ns));
5436 }
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005437
Ilya Dryomov99d16942016-08-12 16:11:41 +02005438 mutex_init(&rbd_dev->watch_mutex);
5439 rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED;
5440 INIT_DELAYED_WORK(&rbd_dev->watch_dwork, rbd_reregister_watch);
5441
Ilya Dryomoved95b212016-08-12 16:40:02 +02005442 init_rwsem(&rbd_dev->lock_rwsem);
5443 rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED;
5444 INIT_WORK(&rbd_dev->acquired_lock_work, rbd_notify_acquired_lock);
5445 INIT_WORK(&rbd_dev->released_lock_work, rbd_notify_released_lock);
5446 INIT_DELAYED_WORK(&rbd_dev->lock_dwork, rbd_acquire_lock);
5447 INIT_WORK(&rbd_dev->unlock_work, rbd_release_lock_work);
Ilya Dryomove1fddc82019-05-30 16:07:48 +02005448 spin_lock_init(&rbd_dev->lock_lists_lock);
Ilya Dryomov637cd062019-06-06 17:14:49 +02005449 INIT_LIST_HEAD(&rbd_dev->acquiring_list);
Ilya Dryomove1fddc82019-05-30 16:07:48 +02005450 INIT_LIST_HEAD(&rbd_dev->running_list);
Ilya Dryomov637cd062019-06-06 17:14:49 +02005451 init_completion(&rbd_dev->acquire_wait);
Ilya Dryomove1fddc82019-05-30 16:07:48 +02005452 init_completion(&rbd_dev->releasing_wait);
Ilya Dryomoved95b212016-08-12 16:40:02 +02005453
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02005454 spin_lock_init(&rbd_dev->object_map_lock);
Alex Elderc53d5892012-10-25 23:34:42 -05005455
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02005456 rbd_dev->dev.bus = &rbd_bus_type;
5457 rbd_dev->dev.type = &rbd_device_type;
5458 rbd_dev->dev.parent = &rbd_root_dev;
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02005459 device_initialize(&rbd_dev->dev);
5460
Alex Elderc53d5892012-10-25 23:34:42 -05005461 rbd_dev->rbd_client = rbdc;
Ilya Dryomovd1475432015-06-22 13:24:48 +03005462 rbd_dev->spec = spec;
Alex Elder0903e872012-11-14 12:25:19 -06005463
Alex Elderc53d5892012-10-25 23:34:42 -05005464 return rbd_dev;
5465}
5466
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02005467/*
5468 * Create a mapping rbd_dev.
5469 */
5470static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,
5471 struct rbd_spec *spec,
5472 struct rbd_options *opts)
5473{
5474 struct rbd_device *rbd_dev;
5475
5476 rbd_dev = __rbd_dev_create(rbdc, spec);
5477 if (!rbd_dev)
5478 return NULL;
5479
5480 rbd_dev->opts = opts;
5481
5482 /* get an id and fill in device name */
5483 rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0,
5484 minor_to_rbd_dev_id(1 << MINORBITS),
5485 GFP_KERNEL);
5486 if (rbd_dev->dev_id < 0)
5487 goto fail_rbd_dev;
5488
5489 sprintf(rbd_dev->name, RBD_DRV_NAME "%d", rbd_dev->dev_id);
5490 rbd_dev->task_wq = alloc_ordered_workqueue("%s-tasks", WQ_MEM_RECLAIM,
5491 rbd_dev->name);
5492 if (!rbd_dev->task_wq)
5493 goto fail_dev_id;
5494
5495 /* we have a ref from do_rbd_add() */
5496 __module_get(THIS_MODULE);
5497
5498 dout("%s rbd_dev %p dev_id %d\n", __func__, rbd_dev, rbd_dev->dev_id);
5499 return rbd_dev;
5500
5501fail_dev_id:
5502 ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id);
5503fail_rbd_dev:
5504 rbd_dev_free(rbd_dev);
5505 return NULL;
5506}
5507
Alex Elderc53d5892012-10-25 23:34:42 -05005508static void rbd_dev_destroy(struct rbd_device *rbd_dev)
5509{
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02005510 if (rbd_dev)
5511 put_device(&rbd_dev->dev);
Alex Elderc53d5892012-10-25 23:34:42 -05005512}
5513
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005514/*
Alex Elder9d475de2012-07-03 16:01:19 -05005515 * Get the size and object order for an image snapshot, or if
5516 * snap_id is CEPH_NOSNAP, gets this information for the base
5517 * image.
5518 */
5519static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
5520 u8 *order, u64 *snap_size)
5521{
5522 __le64 snapid = cpu_to_le64(snap_id);
5523 int ret;
5524 struct {
5525 u8 order;
5526 __le64 size;
5527 } __attribute__ ((packed)) size_buf = { 0 };
5528
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005529 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5530 &rbd_dev->header_oloc, "get_size",
5531 &snapid, sizeof(snapid),
5532 &size_buf, sizeof(size_buf));
Alex Elder36be9a72013-01-19 00:30:28 -06005533 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder9d475de2012-07-03 16:01:19 -05005534 if (ret < 0)
5535 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05005536 if (ret < sizeof (size_buf))
5537 return -ERANGE;
Alex Elder9d475de2012-07-03 16:01:19 -05005538
Josh Durginc3545572013-08-28 17:08:10 -07005539 if (order) {
Alex Elderc86f86e2013-04-25 15:09:41 -05005540 *order = size_buf.order;
Josh Durginc3545572013-08-28 17:08:10 -07005541 dout(" order %u", (unsigned int)*order);
5542 }
Alex Elder9d475de2012-07-03 16:01:19 -05005543 *snap_size = le64_to_cpu(size_buf.size);
5544
Josh Durginc3545572013-08-28 17:08:10 -07005545 dout(" snap_id 0x%016llx snap_size = %llu\n",
5546 (unsigned long long)snap_id,
Alex Elder57385b52013-04-21 12:14:45 -05005547 (unsigned long long)*snap_size);
Alex Elder9d475de2012-07-03 16:01:19 -05005548
5549 return 0;
5550}
5551
5552static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
5553{
5554 return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
5555 &rbd_dev->header.obj_order,
5556 &rbd_dev->header.image_size);
5557}
5558
Alex Elder1e130192012-07-03 16:01:19 -05005559static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
5560{
Dongsheng Yang5435d2062019-08-09 07:05:27 +00005561 size_t size;
Alex Elder1e130192012-07-03 16:01:19 -05005562 void *reply_buf;
5563 int ret;
5564 void *p;
5565
Dongsheng Yang5435d2062019-08-09 07:05:27 +00005566 /* Response will be an encoded string, which includes a length */
5567 size = sizeof(__le32) + RBD_OBJ_PREFIX_LEN_MAX;
5568 reply_buf = kzalloc(size, GFP_KERNEL);
Alex Elder1e130192012-07-03 16:01:19 -05005569 if (!reply_buf)
5570 return -ENOMEM;
5571
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005572 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5573 &rbd_dev->header_oloc, "get_object_prefix",
Dongsheng Yang5435d2062019-08-09 07:05:27 +00005574 NULL, 0, reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06005575 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder1e130192012-07-03 16:01:19 -05005576 if (ret < 0)
5577 goto out;
5578
5579 p = reply_buf;
5580 rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p,
Alex Elder57385b52013-04-21 12:14:45 -05005581 p + ret, NULL, GFP_NOIO);
5582 ret = 0;
Alex Elder1e130192012-07-03 16:01:19 -05005583
5584 if (IS_ERR(rbd_dev->header.object_prefix)) {
5585 ret = PTR_ERR(rbd_dev->header.object_prefix);
5586 rbd_dev->header.object_prefix = NULL;
5587 } else {
5588 dout(" object_prefix = %s\n", rbd_dev->header.object_prefix);
5589 }
Alex Elder1e130192012-07-03 16:01:19 -05005590out:
5591 kfree(reply_buf);
5592
5593 return ret;
5594}
5595
Alex Elderb1b54022012-07-03 16:01:19 -05005596static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
Ilya Dryomov196e2d62019-11-05 15:38:46 +01005597 bool read_only, u64 *snap_features)
Alex Elderb1b54022012-07-03 16:01:19 -05005598{
Ilya Dryomov196e2d62019-11-05 15:38:46 +01005599 struct {
5600 __le64 snap_id;
5601 u8 read_only;
5602 } features_in;
Alex Elderb1b54022012-07-03 16:01:19 -05005603 struct {
5604 __le64 features;
5605 __le64 incompat;
Alex Elder41579762013-04-21 12:14:45 -05005606 } __attribute__ ((packed)) features_buf = { 0 };
Ilya Dryomovd3767f02016-04-13 14:15:50 +02005607 u64 unsup;
Alex Elderb1b54022012-07-03 16:01:19 -05005608 int ret;
5609
Ilya Dryomov196e2d62019-11-05 15:38:46 +01005610 features_in.snap_id = cpu_to_le64(snap_id);
5611 features_in.read_only = read_only;
5612
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005613 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5614 &rbd_dev->header_oloc, "get_features",
Ilya Dryomov196e2d62019-11-05 15:38:46 +01005615 &features_in, sizeof(features_in),
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005616 &features_buf, sizeof(features_buf));
Alex Elder36be9a72013-01-19 00:30:28 -06005617 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderb1b54022012-07-03 16:01:19 -05005618 if (ret < 0)
5619 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05005620 if (ret < sizeof (features_buf))
5621 return -ERANGE;
Alex Elderd8891402012-10-09 13:50:17 -07005622
Ilya Dryomovd3767f02016-04-13 14:15:50 +02005623 unsup = le64_to_cpu(features_buf.incompat) & ~RBD_FEATURES_SUPPORTED;
5624 if (unsup) {
5625 rbd_warn(rbd_dev, "image uses unsupported features: 0x%llx",
5626 unsup);
Alex Elderb8f5c6e2012-11-01 08:39:26 -05005627 return -ENXIO;
Ilya Dryomovd3767f02016-04-13 14:15:50 +02005628 }
Alex Elderd8891402012-10-09 13:50:17 -07005629
Alex Elderb1b54022012-07-03 16:01:19 -05005630 *snap_features = le64_to_cpu(features_buf.features);
5631
5632 dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
Alex Elder57385b52013-04-21 12:14:45 -05005633 (unsigned long long)snap_id,
5634 (unsigned long long)*snap_features,
5635 (unsigned long long)le64_to_cpu(features_buf.incompat));
Alex Elderb1b54022012-07-03 16:01:19 -05005636
5637 return 0;
5638}
5639
5640static int rbd_dev_v2_features(struct rbd_device *rbd_dev)
5641{
5642 return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP,
Ilya Dryomov196e2d62019-11-05 15:38:46 +01005643 rbd_is_ro(rbd_dev),
5644 &rbd_dev->header.features);
Alex Elderb1b54022012-07-03 16:01:19 -05005645}
5646
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02005647/*
5648 * These are generic image flags, but since they are used only for
5649 * object map, store them in rbd_dev->object_map_flags.
5650 *
5651 * For the same reason, this function is called only on object map
5652 * (re)load and not on header refresh.
5653 */
5654static int rbd_dev_v2_get_flags(struct rbd_device *rbd_dev)
5655{
5656 __le64 snapid = cpu_to_le64(rbd_dev->spec->snap_id);
5657 __le64 flags;
5658 int ret;
5659
5660 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5661 &rbd_dev->header_oloc, "get_flags",
5662 &snapid, sizeof(snapid),
5663 &flags, sizeof(flags));
5664 if (ret < 0)
5665 return ret;
5666 if (ret < sizeof(flags))
5667 return -EBADMSG;
5668
5669 rbd_dev->object_map_flags = le64_to_cpu(flags);
5670 return 0;
5671}
5672
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005673struct parent_image_info {
5674 u64 pool_id;
Ilya Dryomove92c0ea2018-08-22 17:26:10 +02005675 const char *pool_ns;
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005676 const char *image_id;
5677 u64 snap_id;
5678
Ilya Dryomove92c0ea2018-08-22 17:26:10 +02005679 bool has_overlap;
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005680 u64 overlap;
5681};
5682
5683/*
5684 * The caller is responsible for @pii.
5685 */
Ilya Dryomove92c0ea2018-08-22 17:26:10 +02005686static int decode_parent_image_spec(void **p, void *end,
5687 struct parent_image_info *pii)
5688{
5689 u8 struct_v;
5690 u32 struct_len;
5691 int ret;
5692
5693 ret = ceph_start_decoding(p, end, 1, "ParentImageSpec",
5694 &struct_v, &struct_len);
5695 if (ret)
5696 return ret;
5697
5698 ceph_decode_64_safe(p, end, pii->pool_id, e_inval);
5699 pii->pool_ns = ceph_extract_encoded_string(p, end, NULL, GFP_KERNEL);
5700 if (IS_ERR(pii->pool_ns)) {
5701 ret = PTR_ERR(pii->pool_ns);
5702 pii->pool_ns = NULL;
5703 return ret;
5704 }
5705 pii->image_id = ceph_extract_encoded_string(p, end, NULL, GFP_KERNEL);
5706 if (IS_ERR(pii->image_id)) {
5707 ret = PTR_ERR(pii->image_id);
5708 pii->image_id = NULL;
5709 return ret;
5710 }
5711 ceph_decode_64_safe(p, end, pii->snap_id, e_inval);
5712 return 0;
5713
5714e_inval:
5715 return -EINVAL;
5716}
5717
5718static int __get_parent_info(struct rbd_device *rbd_dev,
5719 struct page *req_page,
5720 struct page *reply_page,
5721 struct parent_image_info *pii)
5722{
5723 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
5724 size_t reply_len = PAGE_SIZE;
5725 void *p, *end;
5726 int ret;
5727
5728 ret = ceph_osdc_call(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc,
5729 "rbd", "parent_get", CEPH_OSD_FLAG_READ,
Ilya Dryomov68ada912019-06-14 18:16:51 +02005730 req_page, sizeof(u64), &reply_page, &reply_len);
Ilya Dryomove92c0ea2018-08-22 17:26:10 +02005731 if (ret)
5732 return ret == -EOPNOTSUPP ? 1 : ret;
5733
5734 p = page_address(reply_page);
5735 end = p + reply_len;
5736 ret = decode_parent_image_spec(&p, end, pii);
5737 if (ret)
5738 return ret;
5739
5740 ret = ceph_osdc_call(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc,
5741 "rbd", "parent_overlap_get", CEPH_OSD_FLAG_READ,
Ilya Dryomov68ada912019-06-14 18:16:51 +02005742 req_page, sizeof(u64), &reply_page, &reply_len);
Ilya Dryomove92c0ea2018-08-22 17:26:10 +02005743 if (ret)
5744 return ret;
5745
5746 p = page_address(reply_page);
5747 end = p + reply_len;
5748 ceph_decode_8_safe(&p, end, pii->has_overlap, e_inval);
5749 if (pii->has_overlap)
5750 ceph_decode_64_safe(&p, end, pii->overlap, e_inval);
5751
5752 return 0;
5753
5754e_inval:
5755 return -EINVAL;
5756}
5757
5758/*
5759 * The caller is responsible for @pii.
5760 */
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005761static int __get_parent_info_legacy(struct rbd_device *rbd_dev,
5762 struct page *req_page,
5763 struct page *reply_page,
5764 struct parent_image_info *pii)
5765{
5766 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
5767 size_t reply_len = PAGE_SIZE;
5768 void *p, *end;
5769 int ret;
5770
5771 ret = ceph_osdc_call(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc,
5772 "rbd", "get_parent", CEPH_OSD_FLAG_READ,
Ilya Dryomov68ada912019-06-14 18:16:51 +02005773 req_page, sizeof(u64), &reply_page, &reply_len);
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005774 if (ret)
5775 return ret;
5776
5777 p = page_address(reply_page);
5778 end = p + reply_len;
5779 ceph_decode_64_safe(&p, end, pii->pool_id, e_inval);
5780 pii->image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
5781 if (IS_ERR(pii->image_id)) {
5782 ret = PTR_ERR(pii->image_id);
5783 pii->image_id = NULL;
5784 return ret;
5785 }
5786 ceph_decode_64_safe(&p, end, pii->snap_id, e_inval);
Ilya Dryomove92c0ea2018-08-22 17:26:10 +02005787 pii->has_overlap = true;
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005788 ceph_decode_64_safe(&p, end, pii->overlap, e_inval);
5789
5790 return 0;
5791
5792e_inval:
5793 return -EINVAL;
5794}
5795
5796static int get_parent_info(struct rbd_device *rbd_dev,
5797 struct parent_image_info *pii)
5798{
5799 struct page *req_page, *reply_page;
5800 void *p;
5801 int ret;
5802
5803 req_page = alloc_page(GFP_KERNEL);
5804 if (!req_page)
5805 return -ENOMEM;
5806
5807 reply_page = alloc_page(GFP_KERNEL);
5808 if (!reply_page) {
5809 __free_page(req_page);
5810 return -ENOMEM;
5811 }
5812
5813 p = page_address(req_page);
5814 ceph_encode_64(&p, rbd_dev->spec->snap_id);
Ilya Dryomove92c0ea2018-08-22 17:26:10 +02005815 ret = __get_parent_info(rbd_dev, req_page, reply_page, pii);
5816 if (ret > 0)
5817 ret = __get_parent_info_legacy(rbd_dev, req_page, reply_page,
5818 pii);
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005819
5820 __free_page(req_page);
5821 __free_page(reply_page);
5822 return ret;
5823}
5824
Alex Elder86b00e02012-10-25 23:34:42 -05005825static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev)
5826{
5827 struct rbd_spec *parent_spec;
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005828 struct parent_image_info pii = { 0 };
Alex Elder86b00e02012-10-25 23:34:42 -05005829 int ret;
5830
5831 parent_spec = rbd_spec_alloc();
5832 if (!parent_spec)
5833 return -ENOMEM;
5834
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005835 ret = get_parent_info(rbd_dev, &pii);
5836 if (ret)
Alex Elder86b00e02012-10-25 23:34:42 -05005837 goto out_err;
5838
Ilya Dryomove92c0ea2018-08-22 17:26:10 +02005839 dout("%s pool_id %llu pool_ns %s image_id %s snap_id %llu has_overlap %d overlap %llu\n",
5840 __func__, pii.pool_id, pii.pool_ns, pii.image_id, pii.snap_id,
5841 pii.has_overlap, pii.overlap);
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005842
Ilya Dryomove92c0ea2018-08-22 17:26:10 +02005843 if (pii.pool_id == CEPH_NOPOOL || !pii.has_overlap) {
Alex Elder392a9da2013-05-06 17:40:33 -05005844 /*
5845 * Either the parent never existed, or we have
5846 * record of it but the image got flattened so it no
5847 * longer has a parent. When the parent of a
5848 * layered image disappears we immediately set the
5849 * overlap to 0. The effect of this is that all new
5850 * requests will be treated as if the image had no
5851 * parent.
Ilya Dryomove92c0ea2018-08-22 17:26:10 +02005852 *
5853 * If !pii.has_overlap, the parent image spec is not
5854 * applicable. It's there to avoid duplication in each
5855 * snapshot record.
Alex Elder392a9da2013-05-06 17:40:33 -05005856 */
5857 if (rbd_dev->parent_overlap) {
5858 rbd_dev->parent_overlap = 0;
Alex Elder392a9da2013-05-06 17:40:33 -05005859 rbd_dev_parent_put(rbd_dev);
5860 pr_info("%s: clone image has been flattened\n",
5861 rbd_dev->disk->disk_name);
5862 }
5863
Alex Elder86b00e02012-10-25 23:34:42 -05005864 goto out; /* No parent? No problem. */
Alex Elder392a9da2013-05-06 17:40:33 -05005865 }
Alex Elder86b00e02012-10-25 23:34:42 -05005866
Alex Elder0903e872012-11-14 12:25:19 -06005867 /* The ceph file layout needs to fit pool id in 32 bits */
5868
5869 ret = -EIO;
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005870 if (pii.pool_id > (u64)U32_MAX) {
Ilya Dryomov9584d502014-07-11 12:11:20 +04005871 rbd_warn(NULL, "parent pool id too large (%llu > %u)",
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005872 (unsigned long long)pii.pool_id, U32_MAX);
Alex Elder57385b52013-04-21 12:14:45 -05005873 goto out_err;
Alex Elderc0cd10db2013-04-26 09:43:47 -05005874 }
Alex Elder0903e872012-11-14 12:25:19 -06005875
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005876 /*
5877 * The parent won't change (except when the clone is
5878 * flattened, already handled that). So we only need to
5879 * record the parent spec we have not already done so.
5880 */
5881 if (!rbd_dev->parent_spec) {
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005882 parent_spec->pool_id = pii.pool_id;
Ilya Dryomove92c0ea2018-08-22 17:26:10 +02005883 if (pii.pool_ns && *pii.pool_ns) {
5884 parent_spec->pool_ns = pii.pool_ns;
5885 pii.pool_ns = NULL;
5886 }
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005887 parent_spec->image_id = pii.image_id;
5888 pii.image_id = NULL;
5889 parent_spec->snap_id = pii.snap_id;
Ilya Dryomovb26c0472018-07-03 15:28:43 +02005890
Alex Elder70cf49c2013-05-06 17:40:33 -05005891 rbd_dev->parent_spec = parent_spec;
5892 parent_spec = NULL; /* rbd_dev now owns this */
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005893 }
5894
5895 /*
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005896 * We always update the parent overlap. If it's zero we issue
5897 * a warning, as we will proceed as if there was no parent.
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005898 */
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005899 if (!pii.overlap) {
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005900 if (parent_spec) {
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005901 /* refresh, careful to warn just once */
5902 if (rbd_dev->parent_overlap)
5903 rbd_warn(rbd_dev,
5904 "clone now standalone (overlap became 0)");
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005905 } else {
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005906 /* initial probe */
5907 rbd_warn(rbd_dev, "clone is standalone (overlap 0)");
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005908 }
Alex Elder70cf49c2013-05-06 17:40:33 -05005909 }
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005910 rbd_dev->parent_overlap = pii.overlap;
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005911
Alex Elder86b00e02012-10-25 23:34:42 -05005912out:
5913 ret = 0;
5914out_err:
Ilya Dryomove92c0ea2018-08-22 17:26:10 +02005915 kfree(pii.pool_ns);
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005916 kfree(pii.image_id);
Alex Elder86b00e02012-10-25 23:34:42 -05005917 rbd_spec_put(parent_spec);
Alex Elder86b00e02012-10-25 23:34:42 -05005918 return ret;
5919}
5920
Alex Eldercc070d52013-04-21 12:14:45 -05005921static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev)
5922{
5923 struct {
5924 __le64 stripe_unit;
5925 __le64 stripe_count;
5926 } __attribute__ ((packed)) striping_info_buf = { 0 };
5927 size_t size = sizeof (striping_info_buf);
5928 void *p;
Alex Eldercc070d52013-04-21 12:14:45 -05005929 int ret;
5930
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005931 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5932 &rbd_dev->header_oloc, "get_stripe_unit_count",
5933 NULL, 0, &striping_info_buf, size);
Alex Eldercc070d52013-04-21 12:14:45 -05005934 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
5935 if (ret < 0)
5936 return ret;
5937 if (ret < size)
5938 return -ERANGE;
5939
Alex Eldercc070d52013-04-21 12:14:45 -05005940 p = &striping_info_buf;
Ilya Dryomovb1331852018-02-07 12:09:12 +01005941 rbd_dev->header.stripe_unit = ceph_decode_64(&p);
5942 rbd_dev->header.stripe_count = ceph_decode_64(&p);
Alex Eldercc070d52013-04-21 12:14:45 -05005943 return 0;
5944}
5945
Ilya Dryomov7e973322017-01-25 18:16:22 +01005946static int rbd_dev_v2_data_pool(struct rbd_device *rbd_dev)
5947{
5948 __le64 data_pool_id;
5949 int ret;
5950
5951 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5952 &rbd_dev->header_oloc, "get_data_pool",
5953 NULL, 0, &data_pool_id, sizeof(data_pool_id));
5954 if (ret < 0)
5955 return ret;
5956 if (ret < sizeof(data_pool_id))
5957 return -EBADMSG;
5958
5959 rbd_dev->header.data_pool_id = le64_to_cpu(data_pool_id);
5960 WARN_ON(rbd_dev->header.data_pool_id == CEPH_NOPOOL);
5961 return 0;
5962}
5963
Alex Elder9e15b772012-10-30 19:40:33 -05005964static char *rbd_dev_image_name(struct rbd_device *rbd_dev)
5965{
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005966 CEPH_DEFINE_OID_ONSTACK(oid);
Alex Elder9e15b772012-10-30 19:40:33 -05005967 size_t image_id_size;
5968 char *image_id;
5969 void *p;
5970 void *end;
5971 size_t size;
5972 void *reply_buf = NULL;
5973 size_t len = 0;
5974 char *image_name = NULL;
5975 int ret;
5976
5977 rbd_assert(!rbd_dev->spec->image_name);
5978
Alex Elder69e7a022012-11-01 08:39:26 -05005979 len = strlen(rbd_dev->spec->image_id);
5980 image_id_size = sizeof (__le32) + len;
Alex Elder9e15b772012-10-30 19:40:33 -05005981 image_id = kmalloc(image_id_size, GFP_KERNEL);
5982 if (!image_id)
5983 return NULL;
5984
5985 p = image_id;
Alex Elder41579762013-04-21 12:14:45 -05005986 end = image_id + image_id_size;
Alex Elder57385b52013-04-21 12:14:45 -05005987 ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len);
Alex Elder9e15b772012-10-30 19:40:33 -05005988
5989 size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX;
5990 reply_buf = kmalloc(size, GFP_KERNEL);
5991 if (!reply_buf)
5992 goto out;
5993
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005994 ceph_oid_printf(&oid, "%s", RBD_DIRECTORY);
5995 ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc,
5996 "dir_get_name", image_id, image_id_size,
5997 reply_buf, size);
Alex Elder9e15b772012-10-30 19:40:33 -05005998 if (ret < 0)
5999 goto out;
6000 p = reply_buf;
Alex Elderf40eb342013-04-25 15:09:42 -05006001 end = reply_buf + ret;
6002
Alex Elder9e15b772012-10-30 19:40:33 -05006003 image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL);
6004 if (IS_ERR(image_name))
6005 image_name = NULL;
6006 else
6007 dout("%s: name is %s len is %zd\n", __func__, image_name, len);
6008out:
6009 kfree(reply_buf);
6010 kfree(image_id);
6011
6012 return image_name;
6013}
6014
Alex Elder2ad3d712013-04-30 00:44:33 -05006015static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
6016{
6017 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
6018 const char *snap_name;
6019 u32 which = 0;
6020
6021 /* Skip over names until we find the one we are looking for */
6022
6023 snap_name = rbd_dev->header.snap_names;
6024 while (which < snapc->num_snaps) {
6025 if (!strcmp(name, snap_name))
6026 return snapc->snaps[which];
6027 snap_name += strlen(snap_name) + 1;
6028 which++;
6029 }
6030 return CEPH_NOSNAP;
6031}
6032
6033static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
6034{
6035 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
6036 u32 which;
6037 bool found = false;
6038 u64 snap_id;
6039
6040 for (which = 0; !found && which < snapc->num_snaps; which++) {
6041 const char *snap_name;
6042
6043 snap_id = snapc->snaps[which];
6044 snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id);
Josh Durginefadc982013-08-29 19:16:42 -07006045 if (IS_ERR(snap_name)) {
6046 /* ignore no-longer existing snapshots */
6047 if (PTR_ERR(snap_name) == -ENOENT)
6048 continue;
6049 else
6050 break;
6051 }
Alex Elder2ad3d712013-04-30 00:44:33 -05006052 found = !strcmp(name, snap_name);
6053 kfree(snap_name);
6054 }
6055 return found ? snap_id : CEPH_NOSNAP;
6056}
6057
6058/*
6059 * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if
6060 * no snapshot by that name is found, or if an error occurs.
6061 */
6062static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
6063{
6064 if (rbd_dev->image_format == 1)
6065 return rbd_v1_snap_id_by_name(rbd_dev, name);
6066
6067 return rbd_v2_snap_id_by_name(rbd_dev, name);
6068}
6069
Alex Elder9e15b772012-10-30 19:40:33 -05006070/*
Ilya Dryomov04077592014-07-23 17:11:20 +04006071 * An image being mapped will have everything but the snap id.
Alex Elder9e15b772012-10-30 19:40:33 -05006072 */
Ilya Dryomov04077592014-07-23 17:11:20 +04006073static int rbd_spec_fill_snap_id(struct rbd_device *rbd_dev)
6074{
6075 struct rbd_spec *spec = rbd_dev->spec;
6076
6077 rbd_assert(spec->pool_id != CEPH_NOPOOL && spec->pool_name);
6078 rbd_assert(spec->image_id && spec->image_name);
6079 rbd_assert(spec->snap_name);
6080
6081 if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) {
6082 u64 snap_id;
6083
6084 snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name);
6085 if (snap_id == CEPH_NOSNAP)
6086 return -ENOENT;
6087
6088 spec->snap_id = snap_id;
6089 } else {
6090 spec->snap_id = CEPH_NOSNAP;
6091 }
6092
6093 return 0;
6094}
6095
6096/*
6097 * A parent image will have all ids but none of the names.
6098 *
6099 * All names in an rbd spec are dynamically allocated. It's OK if we
6100 * can't figure out the name for an image id.
6101 */
6102static int rbd_spec_fill_names(struct rbd_device *rbd_dev)
Alex Elder9e15b772012-10-30 19:40:33 -05006103{
Alex Elder2e9f7f12013-04-26 09:43:48 -05006104 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
6105 struct rbd_spec *spec = rbd_dev->spec;
6106 const char *pool_name;
6107 const char *image_name;
6108 const char *snap_name;
Alex Elder9e15b772012-10-30 19:40:33 -05006109 int ret;
6110
Ilya Dryomov04077592014-07-23 17:11:20 +04006111 rbd_assert(spec->pool_id != CEPH_NOPOOL);
6112 rbd_assert(spec->image_id);
6113 rbd_assert(spec->snap_id != CEPH_NOSNAP);
Alex Elder9e15b772012-10-30 19:40:33 -05006114
Alex Elder2e9f7f12013-04-26 09:43:48 -05006115 /* Get the pool name; we have to make our own copy of this */
Alex Elder9e15b772012-10-30 19:40:33 -05006116
Alex Elder2e9f7f12013-04-26 09:43:48 -05006117 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id);
6118 if (!pool_name) {
6119 rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id);
Alex Elder935dc892012-11-01 10:17:15 -05006120 return -EIO;
6121 }
Alex Elder2e9f7f12013-04-26 09:43:48 -05006122 pool_name = kstrdup(pool_name, GFP_KERNEL);
6123 if (!pool_name)
Alex Elder9e15b772012-10-30 19:40:33 -05006124 return -ENOMEM;
6125
6126 /* Fetch the image name; tolerate failure here */
6127
Alex Elder2e9f7f12013-04-26 09:43:48 -05006128 image_name = rbd_dev_image_name(rbd_dev);
6129 if (!image_name)
Alex Elder06ecc6c2012-11-01 10:17:15 -05006130 rbd_warn(rbd_dev, "unable to get image name");
Alex Elder9e15b772012-10-30 19:40:33 -05006131
Ilya Dryomov04077592014-07-23 17:11:20 +04006132 /* Fetch the snapshot name */
Alex Elder9e15b772012-10-30 19:40:33 -05006133
Alex Elder2e9f7f12013-04-26 09:43:48 -05006134 snap_name = rbd_snap_name(rbd_dev, spec->snap_id);
Josh Durginda6a6b62013-09-04 17:57:31 -07006135 if (IS_ERR(snap_name)) {
6136 ret = PTR_ERR(snap_name);
Alex Elder9e15b772012-10-30 19:40:33 -05006137 goto out_err;
Alex Elder2e9f7f12013-04-26 09:43:48 -05006138 }
6139
6140 spec->pool_name = pool_name;
6141 spec->image_name = image_name;
6142 spec->snap_name = snap_name;
Alex Elder9e15b772012-10-30 19:40:33 -05006143
6144 return 0;
Ilya Dryomov04077592014-07-23 17:11:20 +04006145
Alex Elder9e15b772012-10-30 19:40:33 -05006146out_err:
Alex Elder2e9f7f12013-04-26 09:43:48 -05006147 kfree(image_name);
6148 kfree(pool_name);
Alex Elder9e15b772012-10-30 19:40:33 -05006149 return ret;
6150}
6151
Alex Eldercc4a38bd2013-04-30 00:44:33 -05006152static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev)
Alex Elder35d489f2012-07-03 16:01:19 -05006153{
6154 size_t size;
6155 int ret;
6156 void *reply_buf;
6157 void *p;
6158 void *end;
6159 u64 seq;
6160 u32 snap_count;
6161 struct ceph_snap_context *snapc;
6162 u32 i;
6163
6164 /*
6165 * We'll need room for the seq value (maximum snapshot id),
6166 * snapshot count, and array of that many snapshot ids.
6167 * For now we have a fixed upper limit on the number we're
6168 * prepared to receive.
6169 */
6170 size = sizeof (__le64) + sizeof (__le32) +
6171 RBD_MAX_SNAP_COUNT * sizeof (__le64);
6172 reply_buf = kzalloc(size, GFP_KERNEL);
6173 if (!reply_buf)
6174 return -ENOMEM;
6175
Ilya Dryomovecd4a682017-01-25 18:16:21 +01006176 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
6177 &rbd_dev->header_oloc, "get_snapcontext",
6178 NULL, 0, reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06006179 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder35d489f2012-07-03 16:01:19 -05006180 if (ret < 0)
6181 goto out;
6182
Alex Elder35d489f2012-07-03 16:01:19 -05006183 p = reply_buf;
Alex Elder57385b52013-04-21 12:14:45 -05006184 end = reply_buf + ret;
6185 ret = -ERANGE;
Alex Elder35d489f2012-07-03 16:01:19 -05006186 ceph_decode_64_safe(&p, end, seq, out);
6187 ceph_decode_32_safe(&p, end, snap_count, out);
6188
6189 /*
6190 * Make sure the reported number of snapshot ids wouldn't go
6191 * beyond the end of our buffer. But before checking that,
6192 * make sure the computed size of the snapshot context we
6193 * allocate is representable in a size_t.
6194 */
6195 if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
6196 / sizeof (u64)) {
6197 ret = -EINVAL;
6198 goto out;
6199 }
6200 if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
6201 goto out;
Alex Elder468521c2013-04-26 09:43:47 -05006202 ret = 0;
Alex Elder35d489f2012-07-03 16:01:19 -05006203
Alex Elder812164f82013-04-30 00:44:32 -05006204 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
Alex Elder35d489f2012-07-03 16:01:19 -05006205 if (!snapc) {
6206 ret = -ENOMEM;
6207 goto out;
6208 }
Alex Elder35d489f2012-07-03 16:01:19 -05006209 snapc->seq = seq;
Alex Elder35d489f2012-07-03 16:01:19 -05006210 for (i = 0; i < snap_count; i++)
6211 snapc->snaps[i] = ceph_decode_64(&p);
6212
Alex Elder49ece552013-05-06 08:37:00 -05006213 ceph_put_snap_context(rbd_dev->header.snapc);
Alex Elder35d489f2012-07-03 16:01:19 -05006214 rbd_dev->header.snapc = snapc;
6215
6216 dout(" snap context seq = %llu, snap_count = %u\n",
Alex Elder57385b52013-04-21 12:14:45 -05006217 (unsigned long long)seq, (unsigned int)snap_count);
Alex Elder35d489f2012-07-03 16:01:19 -05006218out:
6219 kfree(reply_buf);
6220
Alex Elder57385b52013-04-21 12:14:45 -05006221 return ret;
Alex Elder35d489f2012-07-03 16:01:19 -05006222}
6223
Alex Elder54cac612013-04-30 00:44:33 -05006224static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
6225 u64 snap_id)
Alex Elderb8b1e2d2012-07-03 16:01:19 -05006226{
6227 size_t size;
6228 void *reply_buf;
Alex Elder54cac612013-04-30 00:44:33 -05006229 __le64 snapid;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05006230 int ret;
6231 void *p;
6232 void *end;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05006233 char *snap_name;
6234
6235 size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN;
6236 reply_buf = kmalloc(size, GFP_KERNEL);
6237 if (!reply_buf)
6238 return ERR_PTR(-ENOMEM);
6239
Alex Elder54cac612013-04-30 00:44:33 -05006240 snapid = cpu_to_le64(snap_id);
Ilya Dryomovecd4a682017-01-25 18:16:21 +01006241 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
6242 &rbd_dev->header_oloc, "get_snapshot_name",
6243 &snapid, sizeof(snapid), reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06006244 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderf40eb342013-04-25 15:09:42 -05006245 if (ret < 0) {
6246 snap_name = ERR_PTR(ret);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05006247 goto out;
Alex Elderf40eb342013-04-25 15:09:42 -05006248 }
Alex Elderb8b1e2d2012-07-03 16:01:19 -05006249
6250 p = reply_buf;
Alex Elderf40eb342013-04-25 15:09:42 -05006251 end = reply_buf + ret;
Alex Eldere5c35532012-10-25 23:34:41 -05006252 snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elderf40eb342013-04-25 15:09:42 -05006253 if (IS_ERR(snap_name))
Alex Elderb8b1e2d2012-07-03 16:01:19 -05006254 goto out;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05006255
Alex Elderf40eb342013-04-25 15:09:42 -05006256 dout(" snap_id 0x%016llx snap_name = %s\n",
Alex Elder54cac612013-04-30 00:44:33 -05006257 (unsigned long long)snap_id, snap_name);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05006258out:
6259 kfree(reply_buf);
6260
Alex Elderf40eb342013-04-25 15:09:42 -05006261 return snap_name;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05006262}
6263
Alex Elder2df3fac2013-05-06 09:51:30 -05006264static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev)
Alex Elder117973f2012-08-31 17:29:55 -05006265{
Alex Elder2df3fac2013-05-06 09:51:30 -05006266 bool first_time = rbd_dev->header.object_prefix == NULL;
Alex Elder117973f2012-08-31 17:29:55 -05006267 int ret;
Alex Elder117973f2012-08-31 17:29:55 -05006268
Josh Durgin1617e402013-06-12 14:43:10 -07006269 ret = rbd_dev_v2_image_size(rbd_dev);
6270 if (ret)
Alex Eldercfbf6372013-05-31 17:40:45 -05006271 return ret;
Josh Durgin1617e402013-06-12 14:43:10 -07006272
Alex Elder2df3fac2013-05-06 09:51:30 -05006273 if (first_time) {
6274 ret = rbd_dev_v2_header_onetime(rbd_dev);
6275 if (ret)
Alex Eldercfbf6372013-05-31 17:40:45 -05006276 return ret;
Alex Elder2df3fac2013-05-06 09:51:30 -05006277 }
6278
Alex Eldercc4a38bd2013-04-30 00:44:33 -05006279 ret = rbd_dev_v2_snap_context(rbd_dev);
Ilya Dryomovd194cd12015-08-31 18:22:10 +03006280 if (ret && first_time) {
6281 kfree(rbd_dev->header.object_prefix);
6282 rbd_dev->header.object_prefix = NULL;
6283 }
Alex Elder117973f2012-08-31 17:29:55 -05006284
6285 return ret;
6286}
6287
Ilya Dryomova720ae02014-07-23 17:11:19 +04006288static int rbd_dev_header_info(struct rbd_device *rbd_dev)
6289{
6290 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
6291
6292 if (rbd_dev->image_format == 1)
6293 return rbd_dev_v1_header_info(rbd_dev);
6294
6295 return rbd_dev_v2_header_info(rbd_dev);
6296}
6297
Alex Elder1ddbe942012-01-29 13:57:44 -06006298/*
Alex Eldere28fff262012-02-02 08:13:30 -06006299 * Skips over white space at *buf, and updates *buf to point to the
6300 * first found non-space character (if any). Returns the length of
Alex Elder593a9e72012-02-07 12:03:37 -06006301 * the token (string of non-white space characters) found. Note
6302 * that *buf must be terminated with '\0'.
Alex Eldere28fff262012-02-02 08:13:30 -06006303 */
6304static inline size_t next_token(const char **buf)
6305{
6306 /*
6307 * These are the characters that produce nonzero for
6308 * isspace() in the "C" and "POSIX" locales.
6309 */
6310 const char *spaces = " \f\n\r\t\v";
6311
6312 *buf += strspn(*buf, spaces); /* Find start of token */
6313
6314 return strcspn(*buf, spaces); /* Return token length */
6315}
6316
6317/*
Alex Elderea3352f2012-07-09 21:04:23 -05006318 * Finds the next token in *buf, dynamically allocates a buffer big
6319 * enough to hold a copy of it, and copies the token into the new
6320 * buffer. The copy is guaranteed to be terminated with '\0'. Note
6321 * that a duplicate buffer is created even for a zero-length token.
6322 *
6323 * Returns a pointer to the newly-allocated duplicate, or a null
6324 * pointer if memory for the duplicate was not available. If
6325 * the lenp argument is a non-null pointer, the length of the token
6326 * (not including the '\0') is returned in *lenp.
6327 *
6328 * If successful, the *buf pointer will be updated to point beyond
6329 * the end of the found token.
6330 *
6331 * Note: uses GFP_KERNEL for allocation.
6332 */
6333static inline char *dup_token(const char **buf, size_t *lenp)
6334{
6335 char *dup;
6336 size_t len;
6337
6338 len = next_token(buf);
Alex Elder4caf35f2012-11-01 08:39:27 -05006339 dup = kmemdup(*buf, len + 1, GFP_KERNEL);
Alex Elderea3352f2012-07-09 21:04:23 -05006340 if (!dup)
6341 return NULL;
Alex Elderea3352f2012-07-09 21:04:23 -05006342 *(dup + len) = '\0';
6343 *buf += len;
6344
6345 if (lenp)
6346 *lenp = len;
6347
6348 return dup;
6349}
6350
David Howells82995cc2019-03-25 16:38:32 +00006351static int rbd_parse_param(struct fs_parameter *param,
6352 struct rbd_parse_opts_ctx *pctx)
6353{
6354 struct rbd_options *opt = pctx->opts;
6355 struct fs_parse_result result;
6356 int token, ret;
6357
6358 ret = ceph_parse_param(param, pctx->copts, NULL);
6359 if (ret != -ENOPARAM)
6360 return ret;
6361
6362 token = fs_parse(NULL, &rbd_parameters, param, &result);
6363 dout("%s fs_parse '%s' token %d\n", __func__, param->key, token);
6364 if (token < 0) {
6365 if (token == -ENOPARAM) {
6366 return invalf(NULL, "rbd: Unknown parameter '%s'",
6367 param->key);
6368 }
6369 return token;
6370 }
6371
6372 switch (token) {
6373 case Opt_queue_depth:
6374 if (result.uint_32 < 1)
6375 goto out_of_range;
6376 opt->queue_depth = result.uint_32;
6377 break;
6378 case Opt_alloc_size:
6379 if (result.uint_32 < SECTOR_SIZE)
6380 goto out_of_range;
6381 if (!is_power_of_2(result.uint_32)) {
6382 return invalf(NULL, "rbd: alloc_size must be a power of 2");
6383 }
6384 opt->alloc_size = result.uint_32;
6385 break;
6386 case Opt_lock_timeout:
6387 /* 0 is "wait forever" (i.e. infinite timeout) */
6388 if (result.uint_32 > INT_MAX / 1000)
6389 goto out_of_range;
6390 opt->lock_timeout = msecs_to_jiffies(result.uint_32 * 1000);
6391 break;
6392 case Opt_pool_ns:
6393 kfree(pctx->spec->pool_ns);
6394 pctx->spec->pool_ns = param->string;
6395 param->string = NULL;
6396 break;
6397 case Opt_read_only:
6398 opt->read_only = true;
6399 break;
6400 case Opt_read_write:
6401 opt->read_only = false;
6402 break;
6403 case Opt_lock_on_read:
6404 opt->lock_on_read = true;
6405 break;
6406 case Opt_exclusive:
6407 opt->exclusive = true;
6408 break;
6409 case Opt_notrim:
6410 opt->trim = false;
6411 break;
6412 default:
6413 BUG();
6414 }
6415
6416 return 0;
6417
6418out_of_range:
6419 return invalf(NULL, "rbd: %s out of range", param->key);
6420}
6421
6422/*
6423 * This duplicates most of generic_parse_monolithic(), untying it from
6424 * fs_context and skipping standard superblock and security options.
6425 */
6426static int rbd_parse_options(char *options, struct rbd_parse_opts_ctx *pctx)
6427{
6428 char *key;
6429 int ret = 0;
6430
6431 dout("%s '%s'\n", __func__, options);
6432 while ((key = strsep(&options, ",")) != NULL) {
6433 if (*key) {
6434 struct fs_parameter param = {
6435 .key = key,
6436 .type = fs_value_is_string,
6437 };
6438 char *value = strchr(key, '=');
6439 size_t v_len = 0;
6440
6441 if (value) {
6442 if (value == key)
6443 continue;
6444 *value++ = 0;
6445 v_len = strlen(value);
6446 }
6447
6448
6449 if (v_len > 0) {
6450 param.string = kmemdup_nul(value, v_len,
6451 GFP_KERNEL);
6452 if (!param.string)
6453 return -ENOMEM;
6454 }
6455 param.size = v_len;
6456
6457 ret = rbd_parse_param(&param, pctx);
6458 kfree(param.string);
6459 if (ret)
6460 break;
6461 }
6462 }
6463
6464 return ret;
6465}
6466
Alex Elderea3352f2012-07-09 21:04:23 -05006467/*
Alex Elder859c31d2012-10-25 23:34:42 -05006468 * Parse the options provided for an "rbd add" (i.e., rbd image
6469 * mapping) request. These arrive via a write to /sys/bus/rbd/add,
6470 * and the data written is passed here via a NUL-terminated buffer.
6471 * Returns 0 if successful or an error code otherwise.
Alex Elderd22f76e2012-07-12 10:46:35 -05006472 *
Alex Elder859c31d2012-10-25 23:34:42 -05006473 * The information extracted from these options is recorded in
6474 * the other parameters which return dynamically-allocated
6475 * structures:
6476 * ceph_opts
6477 * The address of a pointer that will refer to a ceph options
6478 * structure. Caller must release the returned pointer using
6479 * ceph_destroy_options() when it is no longer needed.
6480 * rbd_opts
6481 * Address of an rbd options pointer. Fully initialized by
6482 * this function; caller must release with kfree().
6483 * spec
6484 * Address of an rbd image specification pointer. Fully
6485 * initialized by this function based on parsed options.
6486 * Caller must release with rbd_spec_put().
6487 *
6488 * The options passed take this form:
6489 * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
6490 * where:
6491 * <mon_addrs>
6492 * A comma-separated list of one or more monitor addresses.
6493 * A monitor address is an ip address, optionally followed
6494 * by a port number (separated by a colon).
6495 * I.e.: ip1[:port1][,ip2[:port2]...]
6496 * <options>
6497 * A comma-separated list of ceph and/or rbd options.
6498 * <pool_name>
6499 * The name of the rados pool containing the rbd image.
6500 * <image_name>
6501 * The name of the image in that pool to map.
6502 * <snap_id>
6503 * An optional snapshot id. If provided, the mapping will
6504 * present data from the image at the time that snapshot was
6505 * created. The image head is used if no snapshot id is
6506 * provided. Snapshot mappings are always read-only.
Alex Eldera725f65e2012-02-02 08:13:30 -06006507 */
Alex Elder859c31d2012-10-25 23:34:42 -05006508static int rbd_add_parse_args(const char *buf,
Alex Elderdc79b112012-10-25 23:34:41 -05006509 struct ceph_options **ceph_opts,
Alex Elder859c31d2012-10-25 23:34:42 -05006510 struct rbd_options **opts,
6511 struct rbd_spec **rbd_spec)
Alex Eldera725f65e2012-02-02 08:13:30 -06006512{
Alex Elderd22f76e2012-07-12 10:46:35 -05006513 size_t len;
Alex Elder859c31d2012-10-25 23:34:42 -05006514 char *options;
Alex Elder0ddebc02012-10-25 23:34:41 -05006515 const char *mon_addrs;
Alex Elderecb4dc222013-04-26 09:43:47 -05006516 char *snap_name;
Alex Elder0ddebc02012-10-25 23:34:41 -05006517 size_t mon_addrs_size;
David Howells82995cc2019-03-25 16:38:32 +00006518 struct rbd_parse_opts_ctx pctx = { 0 };
Alex Elderdc79b112012-10-25 23:34:41 -05006519 int ret;
Alex Eldere28fff262012-02-02 08:13:30 -06006520
6521 /* The first four tokens are required */
6522
Alex Elder7ef32142012-02-02 08:13:30 -06006523 len = next_token(&buf);
Alex Elder4fb5d6712012-11-01 10:17:15 -05006524 if (!len) {
6525 rbd_warn(NULL, "no monitor address(es) provided");
6526 return -EINVAL;
6527 }
Alex Elder0ddebc02012-10-25 23:34:41 -05006528 mon_addrs = buf;
David Howells82995cc2019-03-25 16:38:32 +00006529 mon_addrs_size = len;
Alex Elder7ef32142012-02-02 08:13:30 -06006530 buf += len;
Alex Eldera725f65e2012-02-02 08:13:30 -06006531
Alex Elderdc79b112012-10-25 23:34:41 -05006532 ret = -EINVAL;
Alex Elderf28e5652012-10-25 23:34:41 -05006533 options = dup_token(&buf, NULL);
6534 if (!options)
Alex Elderdc79b112012-10-25 23:34:41 -05006535 return -ENOMEM;
Alex Elder4fb5d6712012-11-01 10:17:15 -05006536 if (!*options) {
6537 rbd_warn(NULL, "no options provided");
6538 goto out_err;
6539 }
Alex Eldera725f65e2012-02-02 08:13:30 -06006540
Ilya Dryomovc3001562018-07-03 15:28:43 +02006541 pctx.spec = rbd_spec_alloc();
6542 if (!pctx.spec)
Alex Elderf28e5652012-10-25 23:34:41 -05006543 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05006544
Ilya Dryomovc3001562018-07-03 15:28:43 +02006545 pctx.spec->pool_name = dup_token(&buf, NULL);
6546 if (!pctx.spec->pool_name)
Alex Elder859c31d2012-10-25 23:34:42 -05006547 goto out_mem;
Ilya Dryomovc3001562018-07-03 15:28:43 +02006548 if (!*pctx.spec->pool_name) {
Alex Elder4fb5d6712012-11-01 10:17:15 -05006549 rbd_warn(NULL, "no pool name provided");
6550 goto out_err;
6551 }
Alex Eldere28fff262012-02-02 08:13:30 -06006552
Ilya Dryomovc3001562018-07-03 15:28:43 +02006553 pctx.spec->image_name = dup_token(&buf, NULL);
6554 if (!pctx.spec->image_name)
Alex Elderf28e5652012-10-25 23:34:41 -05006555 goto out_mem;
Ilya Dryomovc3001562018-07-03 15:28:43 +02006556 if (!*pctx.spec->image_name) {
Alex Elder4fb5d6712012-11-01 10:17:15 -05006557 rbd_warn(NULL, "no image name provided");
6558 goto out_err;
6559 }
Alex Eldere28fff262012-02-02 08:13:30 -06006560
Alex Elderf28e5652012-10-25 23:34:41 -05006561 /*
6562 * Snapshot name is optional; default is to use "-"
6563 * (indicating the head/no snapshot).
6564 */
Alex Elder3feeb8942012-08-31 17:29:52 -05006565 len = next_token(&buf);
Alex Elder820a5f32012-07-09 21:04:24 -05006566 if (!len) {
Alex Elder3feeb8942012-08-31 17:29:52 -05006567 buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
6568 len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
Alex Elderf28e5652012-10-25 23:34:41 -05006569 } else if (len > RBD_MAX_SNAP_NAME_LEN) {
Alex Elderdc79b112012-10-25 23:34:41 -05006570 ret = -ENAMETOOLONG;
Alex Elderf28e5652012-10-25 23:34:41 -05006571 goto out_err;
Alex Elder849b4262012-07-09 21:04:24 -05006572 }
Alex Elderecb4dc222013-04-26 09:43:47 -05006573 snap_name = kmemdup(buf, len + 1, GFP_KERNEL);
6574 if (!snap_name)
Alex Elderf28e5652012-10-25 23:34:41 -05006575 goto out_mem;
Alex Elderecb4dc222013-04-26 09:43:47 -05006576 *(snap_name + len) = '\0';
Ilya Dryomovc3001562018-07-03 15:28:43 +02006577 pctx.spec->snap_name = snap_name;
Alex Eldere5c35532012-10-25 23:34:41 -05006578
David Howells82995cc2019-03-25 16:38:32 +00006579 pctx.copts = ceph_alloc_options();
6580 if (!pctx.copts)
6581 goto out_mem;
6582
Alex Elder0ddebc02012-10-25 23:34:41 -05006583 /* Initialize all rbd options to the defaults */
Alex Eldere28fff262012-02-02 08:13:30 -06006584
Ilya Dryomovc3001562018-07-03 15:28:43 +02006585 pctx.opts = kzalloc(sizeof(*pctx.opts), GFP_KERNEL);
6586 if (!pctx.opts)
Alex Elder4e9afeb2012-10-25 23:34:41 -05006587 goto out_mem;
6588
Ilya Dryomovc3001562018-07-03 15:28:43 +02006589 pctx.opts->read_only = RBD_READ_ONLY_DEFAULT;
6590 pctx.opts->queue_depth = RBD_QUEUE_DEPTH_DEFAULT;
Ilya Dryomov0c93e1b2019-01-30 15:14:48 +01006591 pctx.opts->alloc_size = RBD_ALLOC_SIZE_DEFAULT;
Ilya Dryomovc3001562018-07-03 15:28:43 +02006592 pctx.opts->lock_timeout = RBD_LOCK_TIMEOUT_DEFAULT;
6593 pctx.opts->lock_on_read = RBD_LOCK_ON_READ_DEFAULT;
6594 pctx.opts->exclusive = RBD_EXCLUSIVE_DEFAULT;
6595 pctx.opts->trim = RBD_TRIM_DEFAULT;
Alex Elderd22f76e2012-07-12 10:46:35 -05006596
David Howells82995cc2019-03-25 16:38:32 +00006597 ret = ceph_parse_mon_ips(mon_addrs, mon_addrs_size, pctx.copts, NULL);
6598 if (ret)
Alex Elderdc79b112012-10-25 23:34:41 -05006599 goto out_err;
Alex Elder859c31d2012-10-25 23:34:42 -05006600
David Howells82995cc2019-03-25 16:38:32 +00006601 ret = rbd_parse_options(options, &pctx);
6602 if (ret)
6603 goto out_err;
6604
6605 *ceph_opts = pctx.copts;
Ilya Dryomovc3001562018-07-03 15:28:43 +02006606 *opts = pctx.opts;
6607 *rbd_spec = pctx.spec;
David Howells82995cc2019-03-25 16:38:32 +00006608 kfree(options);
Alex Elderdc79b112012-10-25 23:34:41 -05006609 return 0;
David Howells82995cc2019-03-25 16:38:32 +00006610
Alex Elderf28e5652012-10-25 23:34:41 -05006611out_mem:
Alex Elderdc79b112012-10-25 23:34:41 -05006612 ret = -ENOMEM;
Alex Elderd22f76e2012-07-12 10:46:35 -05006613out_err:
Ilya Dryomovc3001562018-07-03 15:28:43 +02006614 kfree(pctx.opts);
David Howells82995cc2019-03-25 16:38:32 +00006615 ceph_destroy_options(pctx.copts);
Ilya Dryomovc3001562018-07-03 15:28:43 +02006616 rbd_spec_put(pctx.spec);
Alex Elderf28e5652012-10-25 23:34:41 -05006617 kfree(options);
Alex Elderdc79b112012-10-25 23:34:41 -05006618 return ret;
Alex Eldera725f65e2012-02-02 08:13:30 -06006619}
6620
Ilya Dryomove010dd02017-04-13 12:17:39 +02006621static void rbd_dev_image_unlock(struct rbd_device *rbd_dev)
6622{
6623 down_write(&rbd_dev->lock_rwsem);
6624 if (__rbd_is_lock_owner(rbd_dev))
Ilya Dryomove1fddc82019-05-30 16:07:48 +02006625 __rbd_release_lock(rbd_dev);
Ilya Dryomove010dd02017-04-13 12:17:39 +02006626 up_write(&rbd_dev->lock_rwsem);
6627}
6628
Ilya Dryomov637cd062019-06-06 17:14:49 +02006629/*
6630 * If the wait is interrupted, an error is returned even if the lock
6631 * was successfully acquired. rbd_dev_image_unlock() will release it
6632 * if needed.
6633 */
Ilya Dryomove010dd02017-04-13 12:17:39 +02006634static int rbd_add_acquire_lock(struct rbd_device *rbd_dev)
6635{
Ilya Dryomov637cd062019-06-06 17:14:49 +02006636 long ret;
Ilya Dryomov2f18d462018-04-04 10:15:38 +02006637
Ilya Dryomove010dd02017-04-13 12:17:39 +02006638 if (!(rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK)) {
Ilya Dryomov637cd062019-06-06 17:14:49 +02006639 if (!rbd_dev->opts->exclusive && !rbd_dev->opts->lock_on_read)
6640 return 0;
6641
Ilya Dryomove010dd02017-04-13 12:17:39 +02006642 rbd_warn(rbd_dev, "exclusive-lock feature is not enabled");
6643 return -EINVAL;
6644 }
6645
Ilya Dryomov3fe69922019-11-12 19:41:48 +01006646 if (rbd_is_ro(rbd_dev))
Ilya Dryomov637cd062019-06-06 17:14:49 +02006647 return 0;
6648
6649 rbd_assert(!rbd_is_lock_owner(rbd_dev));
6650 queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0);
6651 ret = wait_for_completion_killable_timeout(&rbd_dev->acquire_wait,
6652 ceph_timeout_jiffies(rbd_dev->opts->lock_timeout));
Dongsheng Yang25e6be22019-09-27 15:33:22 +00006653 if (ret > 0) {
Ilya Dryomov637cd062019-06-06 17:14:49 +02006654 ret = rbd_dev->acquire_err;
Dongsheng Yang25e6be22019-09-27 15:33:22 +00006655 } else {
6656 cancel_delayed_work_sync(&rbd_dev->lock_dwork);
6657 if (!ret)
6658 ret = -ETIMEDOUT;
6659 }
Ilya Dryomov637cd062019-06-06 17:14:49 +02006660
Ilya Dryomov2f18d462018-04-04 10:15:38 +02006661 if (ret) {
Ilya Dryomov637cd062019-06-06 17:14:49 +02006662 rbd_warn(rbd_dev, "failed to acquire exclusive lock: %ld", ret);
6663 return ret;
Ilya Dryomove010dd02017-04-13 12:17:39 +02006664 }
6665
Ilya Dryomov637cd062019-06-06 17:14:49 +02006666 /*
6667 * The lock may have been released by now, unless automatic lock
6668 * transitions are disabled.
6669 */
6670 rbd_assert(!rbd_dev->opts->exclusive || rbd_is_lock_owner(rbd_dev));
Ilya Dryomove010dd02017-04-13 12:17:39 +02006671 return 0;
6672}
6673
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04006674/*
Alex Elder589d30e2012-07-10 20:30:11 -05006675 * An rbd format 2 image has a unique identifier, distinct from the
6676 * name given to it by the user. Internally, that identifier is
6677 * what's used to specify the names of objects related to the image.
6678 *
6679 * A special "rbd id" object is used to map an rbd image name to its
6680 * id. If that object doesn't exist, then there is no v2 rbd image
6681 * with the supplied name.
6682 *
6683 * This function will record the given rbd_dev's image_id field if
6684 * it can be determined, and in that case will return 0. If any
6685 * errors occur a negative errno will be returned and the rbd_dev's
6686 * image_id field will be unchanged (and should be NULL).
6687 */
6688static int rbd_dev_image_id(struct rbd_device *rbd_dev)
6689{
6690 int ret;
6691 size_t size;
Ilya Dryomovecd4a682017-01-25 18:16:21 +01006692 CEPH_DEFINE_OID_ONSTACK(oid);
Alex Elder589d30e2012-07-10 20:30:11 -05006693 void *response;
Alex Elderc0fba362013-04-25 23:15:08 -05006694 char *image_id;
Alex Elder2f82ee52012-10-30 19:40:33 -05006695
Alex Elder589d30e2012-07-10 20:30:11 -05006696 /*
Alex Elder2c0d0a12012-10-30 19:40:33 -05006697 * When probing a parent image, the image id is already
6698 * known (and the image name likely is not). There's no
Alex Elderc0fba362013-04-25 23:15:08 -05006699 * need to fetch the image id again in this case. We
6700 * do still need to set the image format though.
Alex Elder2c0d0a12012-10-30 19:40:33 -05006701 */
Alex Elderc0fba362013-04-25 23:15:08 -05006702 if (rbd_dev->spec->image_id) {
6703 rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1;
6704
Alex Elder2c0d0a12012-10-30 19:40:33 -05006705 return 0;
Alex Elderc0fba362013-04-25 23:15:08 -05006706 }
Alex Elder2c0d0a12012-10-30 19:40:33 -05006707
6708 /*
Alex Elder589d30e2012-07-10 20:30:11 -05006709 * First, see if the format 2 image id file exists, and if
6710 * so, get the image's persistent id from it.
6711 */
Ilya Dryomovecd4a682017-01-25 18:16:21 +01006712 ret = ceph_oid_aprintf(&oid, GFP_KERNEL, "%s%s", RBD_ID_PREFIX,
6713 rbd_dev->spec->image_name);
6714 if (ret)
6715 return ret;
6716
6717 dout("rbd id object name is %s\n", oid.name);
Alex Elder589d30e2012-07-10 20:30:11 -05006718
6719 /* Response will be an encoded string, which includes a length */
Alex Elder589d30e2012-07-10 20:30:11 -05006720 size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
6721 response = kzalloc(size, GFP_NOIO);
6722 if (!response) {
6723 ret = -ENOMEM;
6724 goto out;
6725 }
6726
Alex Elderc0fba362013-04-25 23:15:08 -05006727 /* If it doesn't exist we'll assume it's a format 1 image */
6728
Ilya Dryomovecd4a682017-01-25 18:16:21 +01006729 ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc,
6730 "get_id", NULL, 0,
Dongsheng Yang5435d2062019-08-09 07:05:27 +00006731 response, size);
Alex Elder36be9a72013-01-19 00:30:28 -06006732 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderc0fba362013-04-25 23:15:08 -05006733 if (ret == -ENOENT) {
6734 image_id = kstrdup("", GFP_KERNEL);
6735 ret = image_id ? 0 : -ENOMEM;
6736 if (!ret)
6737 rbd_dev->image_format = 1;
Ilya Dryomov7dd440c2014-09-11 18:49:18 +04006738 } else if (ret >= 0) {
Alex Elderc0fba362013-04-25 23:15:08 -05006739 void *p = response;
Alex Elder589d30e2012-07-10 20:30:11 -05006740
Alex Elderc0fba362013-04-25 23:15:08 -05006741 image_id = ceph_extract_encoded_string(&p, p + ret,
Alex Elder979ed482012-11-01 08:39:26 -05006742 NULL, GFP_NOIO);
Duan Jiong461f7582014-04-11 16:38:12 +08006743 ret = PTR_ERR_OR_ZERO(image_id);
Alex Elderc0fba362013-04-25 23:15:08 -05006744 if (!ret)
6745 rbd_dev->image_format = 2;
Alex Elderc0fba362013-04-25 23:15:08 -05006746 }
6747
6748 if (!ret) {
6749 rbd_dev->spec->image_id = image_id;
6750 dout("image_id is %s\n", image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05006751 }
6752out:
6753 kfree(response);
Ilya Dryomovecd4a682017-01-25 18:16:21 +01006754 ceph_oid_destroy(&oid);
Alex Elder589d30e2012-07-10 20:30:11 -05006755 return ret;
6756}
6757
Alex Elder3abef3b2013-05-13 20:35:37 -05006758/*
6759 * Undo whatever state changes are made by v1 or v2 header info
6760 * call.
6761 */
Alex Elder6fd48b32013-04-28 23:32:34 -05006762static void rbd_dev_unprobe(struct rbd_device *rbd_dev)
6763{
6764 struct rbd_image_header *header;
6765
Ilya Dryomove69b8d42015-01-19 12:06:14 +03006766 rbd_dev_parent_put(rbd_dev);
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02006767 rbd_object_map_free(rbd_dev);
Ilya Dryomovda5ef6be2019-06-17 15:29:49 +02006768 rbd_dev_mapping_clear(rbd_dev);
Alex Elder6fd48b32013-04-28 23:32:34 -05006769
6770 /* Free dynamic fields from the header, then zero it out */
6771
6772 header = &rbd_dev->header;
Alex Elder812164f82013-04-30 00:44:32 -05006773 ceph_put_snap_context(header->snapc);
Alex Elder6fd48b32013-04-28 23:32:34 -05006774 kfree(header->snap_sizes);
6775 kfree(header->snap_names);
6776 kfree(header->object_prefix);
6777 memset(header, 0, sizeof (*header));
6778}
6779
Alex Elder2df3fac2013-05-06 09:51:30 -05006780static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev)
Alex Eldera30b71b2012-07-10 20:30:11 -05006781{
6782 int ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05006783
Alex Elder1e130192012-07-03 16:01:19 -05006784 ret = rbd_dev_v2_object_prefix(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05006785 if (ret)
Alex Elder1e130192012-07-03 16:01:19 -05006786 goto out_err;
Alex Elderb1b54022012-07-03 16:01:19 -05006787
Alex Elder2df3fac2013-05-06 09:51:30 -05006788 /*
6789 * Get the and check features for the image. Currently the
6790 * features are assumed to never change.
6791 */
Alex Elderb1b54022012-07-03 16:01:19 -05006792 ret = rbd_dev_v2_features(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05006793 if (ret)
Alex Elderb1b54022012-07-03 16:01:19 -05006794 goto out_err;
Alex Elder35d489f2012-07-03 16:01:19 -05006795
Alex Eldercc070d52013-04-21 12:14:45 -05006796 /* If the image supports fancy striping, get its parameters */
6797
6798 if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) {
6799 ret = rbd_dev_v2_striping_info(rbd_dev);
6800 if (ret < 0)
6801 goto out_err;
6802 }
Alex Eldera30b71b2012-07-10 20:30:11 -05006803
Ilya Dryomov7e973322017-01-25 18:16:22 +01006804 if (rbd_dev->header.features & RBD_FEATURE_DATA_POOL) {
6805 ret = rbd_dev_v2_data_pool(rbd_dev);
6806 if (ret)
6807 goto out_err;
6808 }
6809
Ilya Dryomov263423f2017-01-25 18:16:22 +01006810 rbd_init_layout(rbd_dev);
Alex Elder35152972012-08-31 17:29:55 -05006811 return 0;
Ilya Dryomov263423f2017-01-25 18:16:22 +01006812
Alex Elder9d475de2012-07-03 16:01:19 -05006813out_err:
Alex Elder642a2532013-05-06 17:40:33 -05006814 rbd_dev->header.features = 0;
Alex Elder1e130192012-07-03 16:01:19 -05006815 kfree(rbd_dev->header.object_prefix);
6816 rbd_dev->header.object_prefix = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05006817 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05006818}
6819
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006820/*
6821 * @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() ->
6822 * rbd_dev_image_probe() recursion depth, which means it's also the
6823 * length of the already discovered part of the parent chain.
6824 */
6825static int rbd_dev_probe_parent(struct rbd_device *rbd_dev, int depth)
Alex Elder83a06262012-10-30 15:47:17 -05006826{
Alex Elder2f82ee52012-10-30 19:40:33 -05006827 struct rbd_device *parent = NULL;
Alex Elder124afba2013-04-26 15:44:36 -05006828 int ret;
6829
6830 if (!rbd_dev->parent_spec)
6831 return 0;
Alex Elder124afba2013-04-26 15:44:36 -05006832
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006833 if (++depth > RBD_MAX_PARENT_CHAIN_LEN) {
6834 pr_info("parent chain is too long (%d)\n", depth);
6835 ret = -EINVAL;
6836 goto out_err;
6837 }
6838
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02006839 parent = __rbd_dev_create(rbd_dev->rbd_client, rbd_dev->parent_spec);
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02006840 if (!parent) {
6841 ret = -ENOMEM;
Alex Elder124afba2013-04-26 15:44:36 -05006842 goto out_err;
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02006843 }
6844
6845 /*
6846 * Images related by parent/child relationships always share
6847 * rbd_client and spec/parent_spec, so bump their refcounts.
6848 */
6849 __rbd_get_client(rbd_dev->rbd_client);
6850 rbd_spec_get(rbd_dev->parent_spec);
Alex Elder124afba2013-04-26 15:44:36 -05006851
Ilya Dryomov39258aa2019-11-07 17:16:23 +01006852 __set_bit(RBD_DEV_FLAG_READONLY, &parent->flags);
6853
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006854 ret = rbd_dev_image_probe(parent, depth);
Alex Elder124afba2013-04-26 15:44:36 -05006855 if (ret < 0)
6856 goto out_err;
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02006857
Alex Elder124afba2013-04-26 15:44:36 -05006858 rbd_dev->parent = parent;
Alex Eldera2acd002013-05-08 22:50:04 -05006859 atomic_set(&rbd_dev->parent_ref, 1);
Alex Elder124afba2013-04-26 15:44:36 -05006860 return 0;
Alex Elder124afba2013-04-26 15:44:36 -05006861
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02006862out_err:
6863 rbd_dev_unparent(rbd_dev);
Markus Elfring1761b222015-11-23 20:16:45 +01006864 rbd_dev_destroy(parent);
Alex Elder124afba2013-04-26 15:44:36 -05006865 return ret;
6866}
6867
Ilya Dryomov5769ed02017-04-13 12:17:38 +02006868static void rbd_dev_device_release(struct rbd_device *rbd_dev)
6869{
6870 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
Ilya Dryomov5769ed02017-04-13 12:17:38 +02006871 rbd_free_disk(rbd_dev);
6872 if (!single_major)
6873 unregister_blkdev(rbd_dev->major, rbd_dev->name);
6874}
6875
Ilya Dryomov811c6682016-04-15 16:22:16 +02006876/*
6877 * rbd_dev->header_rwsem must be locked for write and will be unlocked
6878 * upon return.
6879 */
Alex Elder200a6a82013-04-28 23:32:34 -05006880static int rbd_dev_device_setup(struct rbd_device *rbd_dev)
Alex Elder124afba2013-04-26 15:44:36 -05006881{
Alex Elder83a06262012-10-30 15:47:17 -05006882 int ret;
Alex Elder83a06262012-10-30 15:47:17 -05006883
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006884 /* Record our major and minor device numbers. */
Alex Elder83a06262012-10-30 15:47:17 -05006885
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006886 if (!single_major) {
6887 ret = register_blkdev(0, rbd_dev->name);
6888 if (ret < 0)
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02006889 goto err_out_unlock;
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006890
6891 rbd_dev->major = ret;
6892 rbd_dev->minor = 0;
6893 } else {
6894 rbd_dev->major = rbd_major;
6895 rbd_dev->minor = rbd_dev_id_to_minor(rbd_dev->dev_id);
6896 }
Alex Elder83a06262012-10-30 15:47:17 -05006897
6898 /* Set up the blkdev mapping. */
6899
6900 ret = rbd_init_disk(rbd_dev);
6901 if (ret)
6902 goto err_out_blkdev;
6903
Alex Elderf35a4de2013-05-06 09:51:29 -05006904 set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
Ilya Dryomov39258aa2019-11-07 17:16:23 +01006905 set_disk_ro(rbd_dev->disk, rbd_is_ro(rbd_dev));
Alex Elderf35a4de2013-05-06 09:51:29 -05006906
Ilya Dryomov5769ed02017-04-13 12:17:38 +02006907 ret = dev_set_name(&rbd_dev->dev, "%d", rbd_dev->dev_id);
Alex Elderf35a4de2013-05-06 09:51:29 -05006908 if (ret)
Ilya Dryomovda5ef6be2019-06-17 15:29:49 +02006909 goto err_out_disk;
Alex Elder83a06262012-10-30 15:47:17 -05006910
Alex Elder129b79d2013-04-26 15:44:36 -05006911 set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
Ilya Dryomov811c6682016-04-15 16:22:16 +02006912 up_write(&rbd_dev->header_rwsem);
Ilya Dryomov5769ed02017-04-13 12:17:38 +02006913 return 0;
Alex Elder2f82ee52012-10-30 19:40:33 -05006914
Alex Elder83a06262012-10-30 15:47:17 -05006915err_out_disk:
6916 rbd_free_disk(rbd_dev);
6917err_out_blkdev:
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006918 if (!single_major)
6919 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Ilya Dryomov811c6682016-04-15 16:22:16 +02006920err_out_unlock:
6921 up_write(&rbd_dev->header_rwsem);
Alex Elder83a06262012-10-30 15:47:17 -05006922 return ret;
6923}
6924
Alex Elder332bb122013-04-27 09:59:30 -05006925static int rbd_dev_header_name(struct rbd_device *rbd_dev)
6926{
6927 struct rbd_spec *spec = rbd_dev->spec;
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006928 int ret;
Alex Elder332bb122013-04-27 09:59:30 -05006929
6930 /* Record the header object name for this rbd image. */
6931
6932 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
Alex Elder332bb122013-04-27 09:59:30 -05006933 if (rbd_dev->image_format == 1)
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006934 ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s",
6935 spec->image_name, RBD_SUFFIX);
Alex Elder332bb122013-04-27 09:59:30 -05006936 else
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006937 ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s",
6938 RBD_HEADER_PREFIX, spec->image_id);
Alex Elder332bb122013-04-27 09:59:30 -05006939
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006940 return ret;
Alex Elder332bb122013-04-27 09:59:30 -05006941}
6942
Ilya Dryomovb9ef2b82019-11-12 20:20:04 +01006943static void rbd_print_dne(struct rbd_device *rbd_dev, bool is_snap)
6944{
6945 if (!is_snap) {
6946 pr_info("image %s/%s%s%s does not exist\n",
6947 rbd_dev->spec->pool_name,
6948 rbd_dev->spec->pool_ns ?: "",
6949 rbd_dev->spec->pool_ns ? "/" : "",
6950 rbd_dev->spec->image_name);
6951 } else {
6952 pr_info("snap %s/%s%s%s@%s does not exist\n",
6953 rbd_dev->spec->pool_name,
6954 rbd_dev->spec->pool_ns ?: "",
6955 rbd_dev->spec->pool_ns ? "/" : "",
6956 rbd_dev->spec->image_name,
6957 rbd_dev->spec->snap_name);
6958 }
6959}
6960
Alex Elder200a6a82013-04-28 23:32:34 -05006961static void rbd_dev_image_release(struct rbd_device *rbd_dev)
6962{
Alex Elder6fd48b32013-04-28 23:32:34 -05006963 rbd_dev_unprobe(rbd_dev);
Ilya Dryomovfd22aef2017-04-13 12:17:37 +02006964 if (rbd_dev->opts)
6965 rbd_unregister_watch(rbd_dev);
Alex Elder6fd48b32013-04-28 23:32:34 -05006966 rbd_dev->image_format = 0;
6967 kfree(rbd_dev->spec->image_id);
6968 rbd_dev->spec->image_id = NULL;
Alex Elder200a6a82013-04-28 23:32:34 -05006969}
6970
Alex Eldera30b71b2012-07-10 20:30:11 -05006971/*
6972 * Probe for the existence of the header object for the given rbd
Alex Elder1f3ef782013-05-06 17:40:33 -05006973 * device. If this image is the one being mapped (i.e., not a
6974 * parent), initiate a watch on its header object before using that
6975 * object to get detailed information about the rbd image.
Alex Eldera30b71b2012-07-10 20:30:11 -05006976 */
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006977static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth)
Alex Eldera30b71b2012-07-10 20:30:11 -05006978{
Ilya Dryomovb9ef2b82019-11-12 20:20:04 +01006979 bool need_watch = !rbd_is_ro(rbd_dev);
Alex Eldera30b71b2012-07-10 20:30:11 -05006980 int ret;
6981
6982 /*
Alex Elder3abef3b2013-05-13 20:35:37 -05006983 * Get the id from the image id object. Unless there's an
6984 * error, rbd_dev->spec->image_id will be filled in with
6985 * a dynamically-allocated string, and rbd_dev->image_format
6986 * will be set to either 1 or 2.
Alex Eldera30b71b2012-07-10 20:30:11 -05006987 */
6988 ret = rbd_dev_image_id(rbd_dev);
6989 if (ret)
Alex Elderc0fba362013-04-25 23:15:08 -05006990 return ret;
Alex Elderc0fba362013-04-25 23:15:08 -05006991
Alex Elder332bb122013-04-27 09:59:30 -05006992 ret = rbd_dev_header_name(rbd_dev);
6993 if (ret)
6994 goto err_out_format;
6995
Ilya Dryomovb9ef2b82019-11-12 20:20:04 +01006996 if (need_watch) {
Ilya Dryomov99d16942016-08-12 16:11:41 +02006997 ret = rbd_register_watch(rbd_dev);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006998 if (ret) {
6999 if (ret == -ENOENT)
Ilya Dryomovb9ef2b82019-11-12 20:20:04 +01007000 rbd_print_dne(rbd_dev, false);
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02007001 goto err_out_format;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03007002 }
Alex Elder1f3ef782013-05-06 17:40:33 -05007003 }
Alex Elderb644de22013-04-27 09:59:31 -05007004
Ilya Dryomova720ae02014-07-23 17:11:19 +04007005 ret = rbd_dev_header_info(rbd_dev);
Ilya Dryomovb9ef2b82019-11-12 20:20:04 +01007006 if (ret) {
7007 if (ret == -ENOENT && !need_watch)
7008 rbd_print_dne(rbd_dev, false);
Alex Elderb644de22013-04-27 09:59:31 -05007009 goto err_out_watch;
Ilya Dryomovb9ef2b82019-11-12 20:20:04 +01007010 }
Alex Elder83a06262012-10-30 15:47:17 -05007011
Ilya Dryomov04077592014-07-23 17:11:20 +04007012 /*
7013 * If this image is the one being mapped, we have pool name and
7014 * id, image name and id, and snap name - need to fill snap id.
7015 * Otherwise this is a parent image, identified by pool, image
7016 * and snap ids - need to fill in names for those ids.
7017 */
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02007018 if (!depth)
Ilya Dryomov04077592014-07-23 17:11:20 +04007019 ret = rbd_spec_fill_snap_id(rbd_dev);
7020 else
7021 ret = rbd_spec_fill_names(rbd_dev);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03007022 if (ret) {
7023 if (ret == -ENOENT)
Ilya Dryomovb9ef2b82019-11-12 20:20:04 +01007024 rbd_print_dne(rbd_dev, true);
Alex Elder33dca392013-04-30 00:44:33 -05007025 goto err_out_probe;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03007026 }
Alex Elder9bb81c92013-04-27 09:59:30 -05007027
Ilya Dryomovda5ef6be2019-06-17 15:29:49 +02007028 ret = rbd_dev_mapping_set(rbd_dev);
7029 if (ret)
7030 goto err_out_probe;
7031
Ilya Dryomovf3c0e452019-11-07 16:22:10 +01007032 if (rbd_is_snap(rbd_dev) &&
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02007033 (rbd_dev->header.features & RBD_FEATURE_OBJECT_MAP)) {
7034 ret = rbd_object_map_load(rbd_dev);
7035 if (ret)
7036 goto err_out_probe;
7037 }
7038
Ilya Dryomove8f59b52014-07-24 10:42:13 +04007039 if (rbd_dev->header.features & RBD_FEATURE_LAYERING) {
7040 ret = rbd_dev_v2_parent_info(rbd_dev);
7041 if (ret)
7042 goto err_out_probe;
Ilya Dryomove8f59b52014-07-24 10:42:13 +04007043 }
7044
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02007045 ret = rbd_dev_probe_parent(rbd_dev, depth);
Alex Elder30d60ba2013-05-06 09:51:30 -05007046 if (ret)
7047 goto err_out_probe;
Alex Elder83a06262012-10-30 15:47:17 -05007048
Alex Elder30d60ba2013-05-06 09:51:30 -05007049 dout("discovered format %u image, header name is %s\n",
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02007050 rbd_dev->image_format, rbd_dev->header_oid.name);
Alex Elder30d60ba2013-05-06 09:51:30 -05007051 return 0;
Ilya Dryomove8f59b52014-07-24 10:42:13 +04007052
Alex Elder6fd48b32013-04-28 23:32:34 -05007053err_out_probe:
7054 rbd_dev_unprobe(rbd_dev);
Alex Elderb644de22013-04-27 09:59:31 -05007055err_out_watch:
Ilya Dryomovb9ef2b82019-11-12 20:20:04 +01007056 if (need_watch)
Ilya Dryomov99d16942016-08-12 16:11:41 +02007057 rbd_unregister_watch(rbd_dev);
Alex Elder332bb122013-04-27 09:59:30 -05007058err_out_format:
7059 rbd_dev->image_format = 0;
Alex Elder5655c4d2013-04-25 23:15:08 -05007060 kfree(rbd_dev->spec->image_id);
7061 rbd_dev->spec->image_id = NULL;
Alex Elder5655c4d2013-04-25 23:15:08 -05007062 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05007063}
7064
Ilya Dryomov9b60e702013-12-13 15:28:57 +02007065static ssize_t do_rbd_add(struct bus_type *bus,
7066 const char *buf,
7067 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007068{
Alex Eldercb8627c2012-07-09 21:04:23 -05007069 struct rbd_device *rbd_dev = NULL;
Alex Elderdc79b112012-10-25 23:34:41 -05007070 struct ceph_options *ceph_opts = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05007071 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05007072 struct rbd_spec *spec = NULL;
Alex Elder9d3997f2012-10-25 23:34:42 -05007073 struct rbd_client *rbdc;
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02007074 int rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007075
7076 if (!try_module_get(THIS_MODULE))
7077 return -ENODEV;
7078
Alex Eldera725f65e2012-02-02 08:13:30 -06007079 /* parse add command */
Alex Elder859c31d2012-10-25 23:34:42 -05007080 rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec);
Alex Elderdc79b112012-10-25 23:34:41 -05007081 if (rc < 0)
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02007082 goto out;
Alex Eldera725f65e2012-02-02 08:13:30 -06007083
Alex Elder9d3997f2012-10-25 23:34:42 -05007084 rbdc = rbd_get_client(ceph_opts);
7085 if (IS_ERR(rbdc)) {
7086 rc = PTR_ERR(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05007087 goto err_out_args;
Alex Elder9d3997f2012-10-25 23:34:42 -05007088 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007089
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007090 /* pick the pool */
Ilya Dryomovdd435852018-02-22 13:43:24 +01007091 rc = ceph_pg_poolid_by_name(rbdc->client->osdc.osdmap, spec->pool_name);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03007092 if (rc < 0) {
7093 if (rc == -ENOENT)
7094 pr_info("pool %s does not exist\n", spec->pool_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007095 goto err_out_client;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03007096 }
Alex Elderc0cd10db2013-04-26 09:43:47 -05007097 spec->pool_id = (u64)rc;
Alex Elder859c31d2012-10-25 23:34:42 -05007098
Ilya Dryomovd1475432015-06-22 13:24:48 +03007099 rbd_dev = rbd_dev_create(rbdc, spec, rbd_opts);
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02007100 if (!rbd_dev) {
7101 rc = -ENOMEM;
Alex Elderbd4ba652012-10-25 23:34:42 -05007102 goto err_out_client;
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02007103 }
Alex Elderc53d5892012-10-25 23:34:42 -05007104 rbdc = NULL; /* rbd_dev now owns this */
7105 spec = NULL; /* rbd_dev now owns this */
Ilya Dryomovd1475432015-06-22 13:24:48 +03007106 rbd_opts = NULL; /* rbd_dev now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007107
Ilya Dryomov39258aa2019-11-07 17:16:23 +01007108 /* if we are mapping a snapshot it will be a read-only mapping */
7109 if (rbd_dev->opts->read_only ||
7110 strcmp(rbd_dev->spec->snap_name, RBD_SNAP_HEAD_NAME))
7111 __set_bit(RBD_DEV_FLAG_READONLY, &rbd_dev->flags);
7112
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02007113 rbd_dev->config_info = kstrdup(buf, GFP_KERNEL);
7114 if (!rbd_dev->config_info) {
7115 rc = -ENOMEM;
7116 goto err_out_rbd_dev;
7117 }
7118
Ilya Dryomov811c6682016-04-15 16:22:16 +02007119 down_write(&rbd_dev->header_rwsem);
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02007120 rc = rbd_dev_image_probe(rbd_dev, 0);
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02007121 if (rc < 0) {
7122 up_write(&rbd_dev->header_rwsem);
Alex Elderc53d5892012-10-25 23:34:42 -05007123 goto err_out_rbd_dev;
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02007124 }
Alex Elder05fd6f62012-08-29 17:11:07 -05007125
Ilya Dryomov0c93e1b2019-01-30 15:14:48 +01007126 if (rbd_dev->opts->alloc_size > rbd_dev->layout.object_size) {
7127 rbd_warn(rbd_dev, "alloc_size adjusted to %u",
7128 rbd_dev->layout.object_size);
7129 rbd_dev->opts->alloc_size = rbd_dev->layout.object_size;
7130 }
7131
Alex Elderb536f692013-04-28 23:32:34 -05007132 rc = rbd_dev_device_setup(rbd_dev);
Ilya Dryomovfd22aef2017-04-13 12:17:37 +02007133 if (rc)
Ilya Dryomov8b679ec2017-04-13 12:17:37 +02007134 goto err_out_image_probe;
Alex Elderb536f692013-04-28 23:32:34 -05007135
Ilya Dryomov637cd062019-06-06 17:14:49 +02007136 rc = rbd_add_acquire_lock(rbd_dev);
7137 if (rc)
7138 goto err_out_image_lock;
Alex Elderb536f692013-04-28 23:32:34 -05007139
Ilya Dryomov5769ed02017-04-13 12:17:38 +02007140 /* Everything's ready. Announce the disk to the world. */
7141
7142 rc = device_add(&rbd_dev->dev);
7143 if (rc)
Ilya Dryomove010dd02017-04-13 12:17:39 +02007144 goto err_out_image_lock;
Ilya Dryomov5769ed02017-04-13 12:17:38 +02007145
7146 add_disk(rbd_dev->disk);
7147 /* see rbd_init_disk() */
7148 blk_put_queue(rbd_dev->disk->queue);
7149
7150 spin_lock(&rbd_dev_list_lock);
7151 list_add_tail(&rbd_dev->node, &rbd_dev_list);
7152 spin_unlock(&rbd_dev_list_lock);
7153
7154 pr_info("%s: capacity %llu features 0x%llx\n", rbd_dev->disk->disk_name,
7155 (unsigned long long)get_capacity(rbd_dev->disk) << SECTOR_SHIFT,
7156 rbd_dev->header.features);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02007157 rc = count;
7158out:
7159 module_put(THIS_MODULE);
7160 return rc;
Alex Elder3abef3b2013-05-13 20:35:37 -05007161
Ilya Dryomove010dd02017-04-13 12:17:39 +02007162err_out_image_lock:
7163 rbd_dev_image_unlock(rbd_dev);
Ilya Dryomov5769ed02017-04-13 12:17:38 +02007164 rbd_dev_device_release(rbd_dev);
Ilya Dryomov8b679ec2017-04-13 12:17:37 +02007165err_out_image_probe:
7166 rbd_dev_image_release(rbd_dev);
Alex Elderc53d5892012-10-25 23:34:42 -05007167err_out_rbd_dev:
7168 rbd_dev_destroy(rbd_dev);
Alex Elderbd4ba652012-10-25 23:34:42 -05007169err_out_client:
Alex Elder9d3997f2012-10-25 23:34:42 -05007170 rbd_put_client(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05007171err_out_args:
Alex Elder859c31d2012-10-25 23:34:42 -05007172 rbd_spec_put(spec);
Ilya Dryomovd1475432015-06-22 13:24:48 +03007173 kfree(rbd_opts);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02007174 goto out;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007175}
7176
Greg Kroah-Hartman7e9586b2018-12-21 08:54:38 +01007177static ssize_t add_store(struct bus_type *bus, const char *buf, size_t count)
Ilya Dryomov9b60e702013-12-13 15:28:57 +02007178{
7179 if (single_major)
7180 return -EINVAL;
7181
7182 return do_rbd_add(bus, buf, count);
7183}
7184
Greg Kroah-Hartman7e9586b2018-12-21 08:54:38 +01007185static ssize_t add_single_major_store(struct bus_type *bus, const char *buf,
7186 size_t count)
Ilya Dryomov9b60e702013-12-13 15:28:57 +02007187{
7188 return do_rbd_add(bus, buf, count);
7189}
7190
Alex Elder05a46af2013-04-26 15:44:36 -05007191static void rbd_dev_remove_parent(struct rbd_device *rbd_dev)
7192{
Alex Elderad945fc2013-04-26 15:44:36 -05007193 while (rbd_dev->parent) {
Alex Elder05a46af2013-04-26 15:44:36 -05007194 struct rbd_device *first = rbd_dev;
7195 struct rbd_device *second = first->parent;
7196 struct rbd_device *third;
7197
7198 /*
7199 * Follow to the parent with no grandparent and
7200 * remove it.
7201 */
7202 while (second && (third = second->parent)) {
7203 first = second;
7204 second = third;
7205 }
Alex Elderad945fc2013-04-26 15:44:36 -05007206 rbd_assert(second);
Alex Elder8ad42cd2013-04-28 23:32:34 -05007207 rbd_dev_image_release(second);
Ilya Dryomov8b679ec2017-04-13 12:17:37 +02007208 rbd_dev_destroy(second);
Alex Elderad945fc2013-04-26 15:44:36 -05007209 first->parent = NULL;
7210 first->parent_overlap = 0;
7211
7212 rbd_assert(first->parent_spec);
Alex Elder05a46af2013-04-26 15:44:36 -05007213 rbd_spec_put(first->parent_spec);
7214 first->parent_spec = NULL;
Alex Elder05a46af2013-04-26 15:44:36 -05007215 }
7216}
7217
Ilya Dryomov9b60e702013-12-13 15:28:57 +02007218static ssize_t do_rbd_remove(struct bus_type *bus,
7219 const char *buf,
7220 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007221{
7222 struct rbd_device *rbd_dev = NULL;
Alex Elder751cc0e2013-05-31 15:17:01 -05007223 struct list_head *tmp;
7224 int dev_id;
Mike Christie0276dca2016-08-18 18:38:45 +02007225 char opt_buf[6];
Mike Christie0276dca2016-08-18 18:38:45 +02007226 bool force = false;
Alex Elder0d8189e2013-04-27 09:59:30 -05007227 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007228
Mike Christie0276dca2016-08-18 18:38:45 +02007229 dev_id = -1;
7230 opt_buf[0] = '\0';
7231 sscanf(buf, "%d %5s", &dev_id, opt_buf);
7232 if (dev_id < 0) {
7233 pr_err("dev_id out of range\n");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007234 return -EINVAL;
Mike Christie0276dca2016-08-18 18:38:45 +02007235 }
7236 if (opt_buf[0] != '\0') {
7237 if (!strcmp(opt_buf, "force")) {
7238 force = true;
7239 } else {
7240 pr_err("bad remove option at '%s'\n", opt_buf);
7241 return -EINVAL;
7242 }
7243 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007244
Alex Elder751cc0e2013-05-31 15:17:01 -05007245 ret = -ENOENT;
7246 spin_lock(&rbd_dev_list_lock);
7247 list_for_each(tmp, &rbd_dev_list) {
7248 rbd_dev = list_entry(tmp, struct rbd_device, node);
7249 if (rbd_dev->dev_id == dev_id) {
7250 ret = 0;
7251 break;
7252 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007253 }
Alex Elder751cc0e2013-05-31 15:17:01 -05007254 if (!ret) {
7255 spin_lock_irq(&rbd_dev->lock);
Mike Christie0276dca2016-08-18 18:38:45 +02007256 if (rbd_dev->open_count && !force)
Alex Elder751cc0e2013-05-31 15:17:01 -05007257 ret = -EBUSY;
Ilya Dryomov85f5a4d2019-01-08 19:47:38 +01007258 else if (test_and_set_bit(RBD_DEV_FLAG_REMOVING,
7259 &rbd_dev->flags))
7260 ret = -EINPROGRESS;
Alex Elder751cc0e2013-05-31 15:17:01 -05007261 spin_unlock_irq(&rbd_dev->lock);
7262 }
7263 spin_unlock(&rbd_dev_list_lock);
Ilya Dryomov85f5a4d2019-01-08 19:47:38 +01007264 if (ret)
Alex Elder1ba0f1e2013-05-31 15:17:01 -05007265 return ret;
Alex Elder751cc0e2013-05-31 15:17:01 -05007266
Mike Christie0276dca2016-08-18 18:38:45 +02007267 if (force) {
7268 /*
7269 * Prevent new IO from being queued and wait for existing
7270 * IO to complete/fail.
7271 */
7272 blk_mq_freeze_queue(rbd_dev->disk->queue);
7273 blk_set_queue_dying(rbd_dev->disk->queue);
7274 }
7275
Ilya Dryomov5769ed02017-04-13 12:17:38 +02007276 del_gendisk(rbd_dev->disk);
7277 spin_lock(&rbd_dev_list_lock);
7278 list_del_init(&rbd_dev->node);
7279 spin_unlock(&rbd_dev_list_lock);
7280 device_del(&rbd_dev->dev);
Ilya Dryomovfca27062013-12-16 18:02:40 +02007281
Ilya Dryomove010dd02017-04-13 12:17:39 +02007282 rbd_dev_image_unlock(rbd_dev);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02007283 rbd_dev_device_release(rbd_dev);
Alex Elder8ad42cd2013-04-28 23:32:34 -05007284 rbd_dev_image_release(rbd_dev);
Ilya Dryomov8b679ec2017-04-13 12:17:37 +02007285 rbd_dev_destroy(rbd_dev);
Alex Elder1ba0f1e2013-05-31 15:17:01 -05007286 return count;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007287}
7288
Greg Kroah-Hartman7e9586b2018-12-21 08:54:38 +01007289static ssize_t remove_store(struct bus_type *bus, const char *buf, size_t count)
Ilya Dryomov9b60e702013-12-13 15:28:57 +02007290{
7291 if (single_major)
7292 return -EINVAL;
7293
7294 return do_rbd_remove(bus, buf, count);
7295}
7296
Greg Kroah-Hartman7e9586b2018-12-21 08:54:38 +01007297static ssize_t remove_single_major_store(struct bus_type *bus, const char *buf,
7298 size_t count)
Ilya Dryomov9b60e702013-12-13 15:28:57 +02007299{
7300 return do_rbd_remove(bus, buf, count);
7301}
7302
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007303/*
7304 * create control files in sysfs
Yehuda Sadehdfc56062010-11-19 14:51:04 -08007305 * /sys/bus/rbd/...
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007306 */
Chengguang Xu7d8dc532018-08-12 23:06:54 +08007307static int __init rbd_sysfs_init(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007308{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08007309 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007310
Alex Elderfed4c142012-02-07 12:03:36 -06007311 ret = device_register(&rbd_root_dev);
Alex Elder21079782012-01-24 10:08:36 -06007312 if (ret < 0)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08007313 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007314
Alex Elderfed4c142012-02-07 12:03:36 -06007315 ret = bus_register(&rbd_bus_type);
7316 if (ret < 0)
7317 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007318
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007319 return ret;
7320}
7321
Chengguang Xu7d8dc532018-08-12 23:06:54 +08007322static void __exit rbd_sysfs_cleanup(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007323{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08007324 bus_unregister(&rbd_bus_type);
Alex Elderfed4c142012-02-07 12:03:36 -06007325 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007326}
7327
Chengguang Xu7d8dc532018-08-12 23:06:54 +08007328static int __init rbd_slab_init(void)
Alex Elder1c2a9df2013-05-01 12:43:03 -05007329{
7330 rbd_assert(!rbd_img_request_cache);
Geliang Tang03d94402016-03-13 15:17:32 +08007331 rbd_img_request_cache = KMEM_CACHE(rbd_img_request, 0);
Alex Elder868311b2013-05-01 12:43:03 -05007332 if (!rbd_img_request_cache)
7333 return -ENOMEM;
7334
7335 rbd_assert(!rbd_obj_request_cache);
Geliang Tang03d94402016-03-13 15:17:32 +08007336 rbd_obj_request_cache = KMEM_CACHE(rbd_obj_request, 0);
Alex Elder78c2a442013-05-01 12:43:04 -05007337 if (!rbd_obj_request_cache)
7338 goto out_err;
7339
Ilya Dryomov6c696d82017-01-25 18:16:23 +01007340 return 0;
Alex Elder1c2a9df2013-05-01 12:43:03 -05007341
Ilya Dryomov6c696d82017-01-25 18:16:23 +01007342out_err:
Alex Elder868311b2013-05-01 12:43:03 -05007343 kmem_cache_destroy(rbd_img_request_cache);
7344 rbd_img_request_cache = NULL;
Alex Elder1c2a9df2013-05-01 12:43:03 -05007345 return -ENOMEM;
7346}
7347
7348static void rbd_slab_exit(void)
7349{
Alex Elder868311b2013-05-01 12:43:03 -05007350 rbd_assert(rbd_obj_request_cache);
7351 kmem_cache_destroy(rbd_obj_request_cache);
7352 rbd_obj_request_cache = NULL;
7353
Alex Elder1c2a9df2013-05-01 12:43:03 -05007354 rbd_assert(rbd_img_request_cache);
7355 kmem_cache_destroy(rbd_img_request_cache);
7356 rbd_img_request_cache = NULL;
7357}
7358
Alex Eldercc344fa2013-02-19 12:25:56 -06007359static int __init rbd_init(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007360{
7361 int rc;
7362
Alex Elder1e32d342013-01-30 11:13:33 -06007363 if (!libceph_compatible(NULL)) {
7364 rbd_warn(NULL, "libceph incompatibility (quitting)");
Alex Elder1e32d342013-01-30 11:13:33 -06007365 return -EINVAL;
7366 }
Ilya Dryomove1b4d962013-12-13 15:28:57 +02007367
Alex Elder1c2a9df2013-05-01 12:43:03 -05007368 rc = rbd_slab_init();
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007369 if (rc)
7370 return rc;
Ilya Dryomove1b4d962013-12-13 15:28:57 +02007371
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04007372 /*
7373 * The number of active work items is limited by the number of
Ilya Dryomovf77303b2015-04-22 18:28:13 +03007374 * rbd devices * queue depth, so leave @max_active at default.
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04007375 */
7376 rbd_wq = alloc_workqueue(RBD_DRV_NAME, WQ_MEM_RECLAIM, 0);
7377 if (!rbd_wq) {
7378 rc = -ENOMEM;
7379 goto err_out_slab;
7380 }
7381
Ilya Dryomov9b60e702013-12-13 15:28:57 +02007382 if (single_major) {
7383 rbd_major = register_blkdev(0, RBD_DRV_NAME);
7384 if (rbd_major < 0) {
7385 rc = rbd_major;
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04007386 goto err_out_wq;
Ilya Dryomov9b60e702013-12-13 15:28:57 +02007387 }
7388 }
7389
Alex Elder1c2a9df2013-05-01 12:43:03 -05007390 rc = rbd_sysfs_init();
7391 if (rc)
Ilya Dryomov9b60e702013-12-13 15:28:57 +02007392 goto err_out_blkdev;
Alex Elder1c2a9df2013-05-01 12:43:03 -05007393
Ilya Dryomov9b60e702013-12-13 15:28:57 +02007394 if (single_major)
7395 pr_info("loaded (major %d)\n", rbd_major);
7396 else
7397 pr_info("loaded\n");
7398
Ilya Dryomove1b4d962013-12-13 15:28:57 +02007399 return 0;
7400
Ilya Dryomov9b60e702013-12-13 15:28:57 +02007401err_out_blkdev:
7402 if (single_major)
7403 unregister_blkdev(rbd_major, RBD_DRV_NAME);
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04007404err_out_wq:
7405 destroy_workqueue(rbd_wq);
Ilya Dryomove1b4d962013-12-13 15:28:57 +02007406err_out_slab:
7407 rbd_slab_exit();
Alex Elder1c2a9df2013-05-01 12:43:03 -05007408 return rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007409}
7410
Alex Eldercc344fa2013-02-19 12:25:56 -06007411static void __exit rbd_exit(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007412{
Ilya Dryomovffe312c2014-05-20 15:46:04 +04007413 ida_destroy(&rbd_dev_id_ida);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007414 rbd_sysfs_cleanup();
Ilya Dryomov9b60e702013-12-13 15:28:57 +02007415 if (single_major)
7416 unregister_blkdev(rbd_major, RBD_DRV_NAME);
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04007417 destroy_workqueue(rbd_wq);
Alex Elder1c2a9df2013-05-01 12:43:03 -05007418 rbd_slab_exit();
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007419}
7420
7421module_init(rbd_init);
7422module_exit(rbd_exit);
7423
Alex Elderd552c612013-05-31 20:13:09 -05007424MODULE_AUTHOR("Alex Elder <elder@inktank.com>");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007425MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
7426MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007427/* following authorship retained from original osdblk.c */
7428MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
7429
Ilya Dryomov90da2582013-12-13 15:28:56 +02007430MODULE_DESCRIPTION("RADOS Block Device (RBD) driver");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007431MODULE_LICENSE("GPL");