blob: 6343402c09e61d0166bfef6506937adb7ff35af8 [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
Al Virod7167b12019-09-07 07:23:15 -0400851static const struct fs_parameter_spec rbd_parameters[] = {
David Howells82995cc2019-03-25 16:38:32 +0000852 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
Alex Elder98571b52013-01-20 14:44:42 -0600866struct rbd_options {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300867 int queue_depth;
Ilya Dryomov0c93e1b2019-01-30 15:14:48 +0100868 int alloc_size;
Dongsheng Yang34f55d02018-03-26 10:22:55 -0400869 unsigned long lock_timeout;
Alex Elder98571b52013-01-20 14:44:42 -0600870 bool read_only;
Ilya Dryomov80de1912016-09-20 14:23:17 +0200871 bool lock_on_read;
Ilya Dryomove010dd02017-04-13 12:17:39 +0200872 bool exclusive;
Ilya Dryomovd9360542018-03-23 06:14:47 +0100873 bool trim;
Alex Elder98571b52013-01-20 14:44:42 -0600874};
875
Ilya Dryomovb5584182015-06-23 16:21:19 +0300876#define RBD_QUEUE_DEPTH_DEFAULT BLKDEV_MAX_RQ
Ilya Dryomov0c93e1b2019-01-30 15:14:48 +0100877#define RBD_ALLOC_SIZE_DEFAULT (64 * 1024)
Dongsheng Yang34f55d02018-03-26 10:22:55 -0400878#define RBD_LOCK_TIMEOUT_DEFAULT 0 /* no timeout */
Alex Elder98571b52013-01-20 14:44:42 -0600879#define RBD_READ_ONLY_DEFAULT false
Ilya Dryomov80de1912016-09-20 14:23:17 +0200880#define RBD_LOCK_ON_READ_DEFAULT false
Ilya Dryomove010dd02017-04-13 12:17:39 +0200881#define RBD_EXCLUSIVE_DEFAULT false
Ilya Dryomovd9360542018-03-23 06:14:47 +0100882#define RBD_TRIM_DEFAULT true
Alex Elder98571b52013-01-20 14:44:42 -0600883
David Howells82995cc2019-03-25 16:38:32 +0000884struct rbd_parse_opts_ctx {
Ilya Dryomovc3001562018-07-03 15:28:43 +0200885 struct rbd_spec *spec;
David Howells82995cc2019-03-25 16:38:32 +0000886 struct ceph_options *copts;
Ilya Dryomovc3001562018-07-03 15:28:43 +0200887 struct rbd_options *opts;
888};
889
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800890static char* obj_op_name(enum obj_operation_type op_type)
891{
892 switch (op_type) {
893 case OBJ_OP_READ:
894 return "read";
895 case OBJ_OP_WRITE:
896 return "write";
Guangliang Zhao90e98c52014-04-01 22:22:16 +0800897 case OBJ_OP_DISCARD:
898 return "discard";
Ilya Dryomov6484cbe2019-01-29 12:46:25 +0100899 case OBJ_OP_ZEROOUT:
900 return "zeroout";
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800901 default:
902 return "???";
903 }
904}
905
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700906/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700907 * Destroy ceph client
Alex Elderd23a4b32012-01-29 13:57:43 -0600908 *
Alex Elder432b8582012-01-29 13:57:44 -0600909 * Caller must hold rbd_client_list_lock.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700910 */
911static void rbd_client_release(struct kref *kref)
912{
913 struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
914
Alex Elder37206ee2013-02-20 17:32:08 -0600915 dout("%s: rbdc %p\n", __func__, rbdc);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500916 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700917 list_del(&rbdc->node);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500918 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700919
920 ceph_destroy_client(rbdc->client);
921 kfree(rbdc);
922}
923
924/*
925 * Drop reference to ceph client node. If it's not referenced anymore, release
926 * it.
927 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500928static void rbd_put_client(struct rbd_client *rbdc)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700929{
Alex Elderc53d5892012-10-25 23:34:42 -0500930 if (rbdc)
931 kref_put(&rbdc->kref, rbd_client_release);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700932}
933
Ilya Dryomov5feb0d8d2018-02-22 13:19:04 +0100934/*
935 * Get a ceph client with specific addr and configuration, if one does
936 * not exist create it. Either way, ceph_opts is consumed by this
937 * function.
938 */
939static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts)
940{
941 struct rbd_client *rbdc;
Ilya Dryomovdd435852018-02-22 13:43:24 +0100942 int ret;
Ilya Dryomov5feb0d8d2018-02-22 13:19:04 +0100943
Ilya Dryomova32e4142019-05-02 15:56:00 +0200944 mutex_lock(&client_mutex);
Ilya Dryomov5feb0d8d2018-02-22 13:19:04 +0100945 rbdc = rbd_client_find(ceph_opts);
Ilya Dryomovdd435852018-02-22 13:43:24 +0100946 if (rbdc) {
Ilya Dryomov5feb0d8d2018-02-22 13:19:04 +0100947 ceph_destroy_options(ceph_opts);
Ilya Dryomovdd435852018-02-22 13:43:24 +0100948
949 /*
950 * Using an existing client. Make sure ->pg_pools is up to
951 * date before we look up the pool id in do_rbd_add().
952 */
Ilya Dryomov9d4a2272019-03-20 10:58:05 +0100953 ret = ceph_wait_for_latest_osdmap(rbdc->client,
954 rbdc->client->options->mount_timeout);
Ilya Dryomovdd435852018-02-22 13:43:24 +0100955 if (ret) {
956 rbd_warn(NULL, "failed to get latest osdmap: %d", ret);
957 rbd_put_client(rbdc);
958 rbdc = ERR_PTR(ret);
959 }
960 } else {
Ilya Dryomov5feb0d8d2018-02-22 13:19:04 +0100961 rbdc = rbd_client_create(ceph_opts);
Ilya Dryomovdd435852018-02-22 13:43:24 +0100962 }
Ilya Dryomov5feb0d8d2018-02-22 13:19:04 +0100963 mutex_unlock(&client_mutex);
964
965 return rbdc;
966}
967
Alex Eldera30b71b2012-07-10 20:30:11 -0500968static bool rbd_image_format_valid(u32 image_format)
969{
970 return image_format == 1 || image_format == 2;
971}
972
Alex Elder8e94af82012-07-25 09:32:40 -0500973static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
974{
Alex Elder103a1502012-08-02 11:29:45 -0500975 size_t size;
976 u32 snap_count;
977
978 /* The header has to start with the magic rbd header text */
979 if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT)))
980 return false;
981
Alex Elderdb2388b2012-10-20 22:17:27 -0500982 /* The bio layer requires at least sector-sized I/O */
983
984 if (ondisk->options.order < SECTOR_SHIFT)
985 return false;
986
987 /* If we use u64 in a few spots we may be able to loosen this */
988
989 if (ondisk->options.order > 8 * sizeof (int) - 1)
990 return false;
991
Alex Elder103a1502012-08-02 11:29:45 -0500992 /*
993 * The size of a snapshot header has to fit in a size_t, and
994 * that limits the number of snapshots.
995 */
996 snap_count = le32_to_cpu(ondisk->snap_count);
997 size = SIZE_MAX - sizeof (struct ceph_snap_context);
998 if (snap_count > size / sizeof (__le64))
999 return false;
1000
1001 /*
1002 * Not only that, but the size of the entire the snapshot
1003 * header must also be representable in a size_t.
1004 */
1005 size -= snap_count * sizeof (__le64);
1006 if ((u64) size < le64_to_cpu(ondisk->snap_names_len))
1007 return false;
1008
1009 return true;
Alex Elder8e94af82012-07-25 09:32:40 -05001010}
1011
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001012/*
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +01001013 * returns the size of an object in the image
1014 */
1015static u32 rbd_obj_bytes(struct rbd_image_header *header)
1016{
1017 return 1U << header->obj_order;
1018}
1019
Ilya Dryomov263423f2017-01-25 18:16:22 +01001020static void rbd_init_layout(struct rbd_device *rbd_dev)
1021{
1022 if (rbd_dev->header.stripe_unit == 0 ||
1023 rbd_dev->header.stripe_count == 0) {
1024 rbd_dev->header.stripe_unit = rbd_obj_bytes(&rbd_dev->header);
1025 rbd_dev->header.stripe_count = 1;
1026 }
1027
1028 rbd_dev->layout.stripe_unit = rbd_dev->header.stripe_unit;
1029 rbd_dev->layout.stripe_count = rbd_dev->header.stripe_count;
1030 rbd_dev->layout.object_size = rbd_obj_bytes(&rbd_dev->header);
Ilya Dryomov7e973322017-01-25 18:16:22 +01001031 rbd_dev->layout.pool_id = rbd_dev->header.data_pool_id == CEPH_NOPOOL ?
1032 rbd_dev->spec->pool_id : rbd_dev->header.data_pool_id;
Ilya Dryomov263423f2017-01-25 18:16:22 +01001033 RCU_INIT_POINTER(rbd_dev->layout.pool_ns, NULL);
1034}
1035
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +01001036/*
Alex Elderbb23e372013-05-06 09:51:29 -05001037 * Fill an rbd image header with information from the given format 1
1038 * on-disk header.
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001039 */
Alex Elder662518b2013-05-06 09:51:29 -05001040static int rbd_header_from_disk(struct rbd_device *rbd_dev,
Alex Elder4156d992012-08-02 11:29:46 -05001041 struct rbd_image_header_ondisk *ondisk)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001042{
Alex Elder662518b2013-05-06 09:51:29 -05001043 struct rbd_image_header *header = &rbd_dev->header;
Alex Elderbb23e372013-05-06 09:51:29 -05001044 bool first_time = header->object_prefix == NULL;
1045 struct ceph_snap_context *snapc;
1046 char *object_prefix = NULL;
1047 char *snap_names = NULL;
1048 u64 *snap_sizes = NULL;
Alex Elderccece232012-07-10 20:30:10 -05001049 u32 snap_count;
Alex Elderbb23e372013-05-06 09:51:29 -05001050 int ret = -ENOMEM;
Alex Elder621901d2012-08-23 23:22:06 -05001051 u32 i;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001052
Alex Elderbb23e372013-05-06 09:51:29 -05001053 /* Allocate this now to avoid having to handle failure below */
1054
1055 if (first_time) {
Ilya Dryomov848d7962017-01-25 18:16:21 +01001056 object_prefix = kstrndup(ondisk->object_prefix,
1057 sizeof(ondisk->object_prefix),
1058 GFP_KERNEL);
Alex Elderbb23e372013-05-06 09:51:29 -05001059 if (!object_prefix)
1060 return -ENOMEM;
Alex Elderbb23e372013-05-06 09:51:29 -05001061 }
1062
1063 /* Allocate the snapshot context and fill it in */
Alex Elder6a523252012-07-19 17:12:59 -05001064
Alex Elder103a1502012-08-02 11:29:45 -05001065 snap_count = le32_to_cpu(ondisk->snap_count);
Alex Elderbb23e372013-05-06 09:51:29 -05001066 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
1067 if (!snapc)
1068 goto out_err;
1069 snapc->seq = le64_to_cpu(ondisk->snap_seq);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001070 if (snap_count) {
Alex Elderbb23e372013-05-06 09:51:29 -05001071 struct rbd_image_snap_ondisk *snaps;
Alex Elderf785cc12012-08-23 23:22:06 -05001072 u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len);
1073
Alex Elderbb23e372013-05-06 09:51:29 -05001074 /* We'll keep a copy of the snapshot names... */
Alex Elder621901d2012-08-23 23:22:06 -05001075
Alex Elderbb23e372013-05-06 09:51:29 -05001076 if (snap_names_len > (u64)SIZE_MAX)
1077 goto out_2big;
1078 snap_names = kmalloc(snap_names_len, GFP_KERNEL);
1079 if (!snap_names)
Alex Elder6a523252012-07-19 17:12:59 -05001080 goto out_err;
Alex Elderbb23e372013-05-06 09:51:29 -05001081
1082 /* ...as well as the array of their sizes. */
Markus Elfring88a25a52016-09-11 12:21:25 +02001083 snap_sizes = kmalloc_array(snap_count,
1084 sizeof(*header->snap_sizes),
1085 GFP_KERNEL);
Alex Elderbb23e372013-05-06 09:51:29 -05001086 if (!snap_sizes)
1087 goto out_err;
1088
Alex Elderf785cc12012-08-23 23:22:06 -05001089 /*
Alex Elderbb23e372013-05-06 09:51:29 -05001090 * Copy the names, and fill in each snapshot's id
1091 * and size.
1092 *
Alex Elder99a41eb2013-05-06 09:51:30 -05001093 * Note that rbd_dev_v1_header_info() guarantees the
Alex Elderbb23e372013-05-06 09:51:29 -05001094 * ondisk buffer we're working with has
Alex Elderf785cc12012-08-23 23:22:06 -05001095 * snap_names_len bytes beyond the end of the
1096 * snapshot id array, this memcpy() is safe.
1097 */
Alex Elderbb23e372013-05-06 09:51:29 -05001098 memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len);
1099 snaps = ondisk->snaps;
1100 for (i = 0; i < snap_count; i++) {
1101 snapc->snaps[i] = le64_to_cpu(snaps[i].id);
1102 snap_sizes[i] = le64_to_cpu(snaps[i].image_size);
1103 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001104 }
Alex Elder849b4262012-07-09 21:04:24 -05001105
Alex Elderbb23e372013-05-06 09:51:29 -05001106 /* We won't fail any more, fill in the header */
Alex Elder6a523252012-07-19 17:12:59 -05001107
Alex Elderbb23e372013-05-06 09:51:29 -05001108 if (first_time) {
1109 header->object_prefix = object_prefix;
1110 header->obj_order = ondisk->options.order;
Ilya Dryomov263423f2017-01-25 18:16:22 +01001111 rbd_init_layout(rbd_dev);
Alex Elder662518b2013-05-06 09:51:29 -05001112 } else {
1113 ceph_put_snap_context(header->snapc);
1114 kfree(header->snap_names);
1115 kfree(header->snap_sizes);
Alex Elderbb23e372013-05-06 09:51:29 -05001116 }
1117
1118 /* The remaining fields always get updated (when we refresh) */
Alex Elder621901d2012-08-23 23:22:06 -05001119
Alex Elderf84344f2012-08-31 17:29:51 -05001120 header->image_size = le64_to_cpu(ondisk->image_size);
Alex Elderbb23e372013-05-06 09:51:29 -05001121 header->snapc = snapc;
1122 header->snap_names = snap_names;
1123 header->snap_sizes = snap_sizes;
Alex Elder468521c2013-04-26 09:43:47 -05001124
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001125 return 0;
Alex Elderbb23e372013-05-06 09:51:29 -05001126out_2big:
1127 ret = -EIO;
Alex Elder6a523252012-07-19 17:12:59 -05001128out_err:
Alex Elderbb23e372013-05-06 09:51:29 -05001129 kfree(snap_sizes);
1130 kfree(snap_names);
1131 ceph_put_snap_context(snapc);
1132 kfree(object_prefix);
Alex Elderccece232012-07-10 20:30:10 -05001133
Alex Elderbb23e372013-05-06 09:51:29 -05001134 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001135}
1136
Alex Elder9682fc62013-04-30 00:44:33 -05001137static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which)
1138{
1139 const char *snap_name;
1140
1141 rbd_assert(which < rbd_dev->header.snapc->num_snaps);
1142
1143 /* Skip over names until we find the one we are looking for */
1144
1145 snap_name = rbd_dev->header.snap_names;
1146 while (which--)
1147 snap_name += strlen(snap_name) + 1;
1148
1149 return kstrdup(snap_name, GFP_KERNEL);
1150}
1151
Alex Elder30d1cff2013-05-01 12:43:03 -05001152/*
1153 * Snapshot id comparison function for use with qsort()/bsearch().
1154 * Note that result is for snapshots in *descending* order.
1155 */
1156static int snapid_compare_reverse(const void *s1, const void *s2)
1157{
1158 u64 snap_id1 = *(u64 *)s1;
1159 u64 snap_id2 = *(u64 *)s2;
1160
1161 if (snap_id1 < snap_id2)
1162 return 1;
1163 return snap_id1 == snap_id2 ? 0 : -1;
1164}
1165
1166/*
1167 * Search a snapshot context to see if the given snapshot id is
1168 * present.
1169 *
1170 * Returns the position of the snapshot id in the array if it's found,
1171 * or BAD_SNAP_INDEX otherwise.
1172 *
1173 * Note: The snapshot array is in kept sorted (by the osd) in
1174 * reverse order, highest snapshot id first.
1175 */
Alex Elder9682fc62013-04-30 00:44:33 -05001176static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id)
1177{
1178 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
Alex Elder30d1cff2013-05-01 12:43:03 -05001179 u64 *found;
Alex Elder9682fc62013-04-30 00:44:33 -05001180
Alex Elder30d1cff2013-05-01 12:43:03 -05001181 found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps,
1182 sizeof (snap_id), snapid_compare_reverse);
Alex Elder9682fc62013-04-30 00:44:33 -05001183
Alex Elder30d1cff2013-05-01 12:43:03 -05001184 return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX;
Alex Elder9682fc62013-04-30 00:44:33 -05001185}
1186
Alex Elder2ad3d712013-04-30 00:44:33 -05001187static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev,
1188 u64 snap_id)
Alex Elder54cac612013-04-30 00:44:33 -05001189{
1190 u32 which;
Josh Durginda6a6b62013-09-04 17:57:31 -07001191 const char *snap_name;
Alex Elder54cac612013-04-30 00:44:33 -05001192
1193 which = rbd_dev_snap_index(rbd_dev, snap_id);
1194 if (which == BAD_SNAP_INDEX)
Josh Durginda6a6b62013-09-04 17:57:31 -07001195 return ERR_PTR(-ENOENT);
Alex Elder54cac612013-04-30 00:44:33 -05001196
Josh Durginda6a6b62013-09-04 17:57:31 -07001197 snap_name = _rbd_dev_v1_snap_name(rbd_dev, which);
1198 return snap_name ? snap_name : ERR_PTR(-ENOMEM);
Alex Elder54cac612013-04-30 00:44:33 -05001199}
1200
Alex Elder9e15b772012-10-30 19:40:33 -05001201static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id)
1202{
Alex Elder9e15b772012-10-30 19:40:33 -05001203 if (snap_id == CEPH_NOSNAP)
1204 return RBD_SNAP_HEAD_NAME;
1205
Alex Elder54cac612013-04-30 00:44:33 -05001206 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1207 if (rbd_dev->image_format == 1)
1208 return rbd_dev_v1_snap_name(rbd_dev, snap_id);
Alex Elder9e15b772012-10-30 19:40:33 -05001209
Alex Elder54cac612013-04-30 00:44:33 -05001210 return rbd_dev_v2_snap_name(rbd_dev, snap_id);
Alex Elder9e15b772012-10-30 19:40:33 -05001211}
1212
Alex Elder2ad3d712013-04-30 00:44:33 -05001213static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
1214 u64 *snap_size)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001215{
Alex Elder2ad3d712013-04-30 00:44:33 -05001216 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1217 if (snap_id == CEPH_NOSNAP) {
1218 *snap_size = rbd_dev->header.image_size;
1219 } else if (rbd_dev->image_format == 1) {
1220 u32 which;
Alex Elder00f1f362012-02-07 12:03:36 -06001221
Alex Elder2ad3d712013-04-30 00:44:33 -05001222 which = rbd_dev_snap_index(rbd_dev, snap_id);
1223 if (which == BAD_SNAP_INDEX)
1224 return -ENOENT;
Alex Elder00f1f362012-02-07 12:03:36 -06001225
Alex Elder2ad3d712013-04-30 00:44:33 -05001226 *snap_size = rbd_dev->header.snap_sizes[which];
1227 } else {
1228 u64 size = 0;
1229 int ret;
1230
1231 ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size);
1232 if (ret)
1233 return ret;
1234
1235 *snap_size = size;
1236 }
1237 return 0;
1238}
1239
Alex Elderd1cf5782013-04-27 09:59:30 -05001240static int rbd_dev_mapping_set(struct rbd_device *rbd_dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001241{
Alex Elder8f4b7d92013-05-06 07:40:30 -05001242 u64 snap_id = rbd_dev->spec->snap_id;
Alex Elder2ad3d712013-04-30 00:44:33 -05001243 u64 size = 0;
Alex Elder2ad3d712013-04-30 00:44:33 -05001244 int ret;
Alex Elder8b0241f2013-04-25 23:15:08 -05001245
Alex Elder2ad3d712013-04-30 00:44:33 -05001246 ret = rbd_snap_size(rbd_dev, snap_id, &size);
1247 if (ret)
1248 return ret;
Alex Elder2ad3d712013-04-30 00:44:33 -05001249
1250 rbd_dev->mapping.size = size;
Alex Elder8b0241f2013-04-25 23:15:08 -05001251 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001252}
1253
Alex Elderd1cf5782013-04-27 09:59:30 -05001254static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev)
1255{
1256 rbd_dev->mapping.size = 0;
Alex Elder200a6a82013-04-28 23:32:34 -05001257}
1258
Ilya Dryomov5359a172018-01-20 10:30:10 +01001259static void zero_bvec(struct bio_vec *bv)
Alex Elder65ccfe22012-08-09 10:33:26 -07001260{
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001261 void *buf;
Ilya Dryomov5359a172018-01-20 10:30:10 +01001262 unsigned long flags;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001263
Ilya Dryomov5359a172018-01-20 10:30:10 +01001264 buf = bvec_kmap_irq(bv, &flags);
1265 memset(buf, 0, bv->bv_len);
1266 flush_dcache_page(bv->bv_page);
1267 bvec_kunmap_irq(buf, &flags);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001268}
1269
Ilya Dryomov5359a172018-01-20 10:30:10 +01001270static void zero_bios(struct ceph_bio_iter *bio_pos, u32 off, u32 bytes)
Alex Elderb9434c52013-04-19 15:34:50 -05001271{
Ilya Dryomov5359a172018-01-20 10:30:10 +01001272 struct ceph_bio_iter it = *bio_pos;
Alex Elderb9434c52013-04-19 15:34:50 -05001273
Ilya Dryomov5359a172018-01-20 10:30:10 +01001274 ceph_bio_iter_advance(&it, off);
1275 ceph_bio_iter_advance_step(&it, bytes, ({
1276 zero_bvec(&bv);
1277 }));
Alex Elderb9434c52013-04-19 15:34:50 -05001278}
1279
Ilya Dryomov7e07efb2018-01-20 10:30:11 +01001280static void zero_bvecs(struct ceph_bvec_iter *bvec_pos, u32 off, u32 bytes)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001281{
Ilya Dryomov7e07efb2018-01-20 10:30:11 +01001282 struct ceph_bvec_iter it = *bvec_pos;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001283
Ilya Dryomov7e07efb2018-01-20 10:30:11 +01001284 ceph_bvec_iter_advance(&it, off);
1285 ceph_bvec_iter_advance_step(&it, bytes, ({
1286 zero_bvec(&bv);
1287 }));
Alex Elderf7760da2012-10-20 22:17:27 -05001288}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001289
Alex Elderf7760da2012-10-20 22:17:27 -05001290/*
Ilya Dryomov3da691b2018-01-29 14:04:08 +01001291 * Zero a range in @obj_req data buffer defined by a bio (list) or
Ilya Dryomovafb97882018-02-06 19:26:35 +01001292 * (private) bio_vec array.
Alex Elderf7760da2012-10-20 22:17:27 -05001293 *
Ilya Dryomov3da691b2018-01-29 14:04:08 +01001294 * @off is relative to the start of the data buffer.
Alex Elderf7760da2012-10-20 22:17:27 -05001295 */
Ilya Dryomov3da691b2018-01-29 14:04:08 +01001296static void rbd_obj_zero_range(struct rbd_obj_request *obj_req, u32 off,
1297 u32 bytes)
Alex Elderf7760da2012-10-20 22:17:27 -05001298{
Ilya Dryomov54ab3b22019-05-11 16:21:49 +02001299 dout("%s %p data buf %u~%u\n", __func__, obj_req, off, bytes);
1300
Ilya Dryomovecc633c2018-02-01 11:50:47 +01001301 switch (obj_req->img_request->data_type) {
Ilya Dryomov3da691b2018-01-29 14:04:08 +01001302 case OBJ_REQUEST_BIO:
1303 zero_bios(&obj_req->bio_pos, off, bytes);
1304 break;
1305 case OBJ_REQUEST_BVECS:
Ilya Dryomovafb97882018-02-06 19:26:35 +01001306 case OBJ_REQUEST_OWN_BVECS:
Ilya Dryomov3da691b2018-01-29 14:04:08 +01001307 zero_bvecs(&obj_req->bvec_pos, off, bytes);
1308 break;
1309 default:
Arnd Bergmann16809372019-03-22 17:53:56 +01001310 BUG();
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001311 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06001312}
1313
1314static void rbd_obj_request_destroy(struct kref *kref);
1315static void rbd_obj_request_put(struct rbd_obj_request *obj_request)
1316{
1317 rbd_assert(obj_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001318 dout("%s: obj %p (was %d)\n", __func__, obj_request,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01001319 kref_read(&obj_request->kref));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001320 kref_put(&obj_request->kref, rbd_obj_request_destroy);
1321}
1322
Alex Elderbf0d5f502012-11-22 00:00:08 -06001323static void rbd_img_request_destroy(struct kref *kref);
1324static void rbd_img_request_put(struct rbd_img_request *img_request)
1325{
1326 rbd_assert(img_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001327 dout("%s: img %p (was %d)\n", __func__, img_request,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01001328 kref_read(&img_request->kref));
Ilya Dryomove93aca02018-02-06 19:26:35 +01001329 kref_put(&img_request->kref, rbd_img_request_destroy);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001330}
1331
1332static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request,
1333 struct rbd_obj_request *obj_request)
1334{
Alex Elder25dcf952013-01-25 17:08:55 -06001335 rbd_assert(obj_request->img_request == NULL);
1336
Alex Elderb155e862013-04-15 14:50:37 -05001337 /* Image request now owns object's original reference */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001338 obj_request->img_request = img_request;
Ilya Dryomov15961b42018-02-01 11:50:47 +01001339 dout("%s: img %p obj %p\n", __func__, img_request, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001340}
1341
1342static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request,
1343 struct rbd_obj_request *obj_request)
1344{
Ilya Dryomov15961b42018-02-01 11:50:47 +01001345 dout("%s: img %p obj %p\n", __func__, img_request, obj_request);
Ilya Dryomov43df3d32018-02-02 15:23:22 +01001346 list_del(&obj_request->ex.oe_item);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001347 rbd_assert(obj_request->img_request == img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001348 rbd_obj_request_put(obj_request);
1349}
1350
Ilya Dryomova086a1b2019-06-12 18:33:31 +02001351static void rbd_osd_submit(struct ceph_osd_request *osd_req)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001352{
Ilya Dryomova086a1b2019-06-12 18:33:31 +02001353 struct rbd_obj_request *obj_req = osd_req->r_priv;
Ilya Dryomov980917f2016-09-12 18:59:42 +02001354
Ilya Dryomova086a1b2019-06-12 18:33:31 +02001355 dout("%s osd_req %p for obj_req %p objno %llu %llu~%llu\n",
1356 __func__, osd_req, obj_req, obj_req->ex.oe_objno,
1357 obj_req->ex.oe_off, obj_req->ex.oe_len);
Ilya Dryomov980917f2016-09-12 18:59:42 +02001358 ceph_osdc_start_request(osd_req->r_osdc, osd_req, false);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001359}
1360
Alex Elder0c425242013-02-08 09:55:49 -06001361/*
1362 * The default/initial value for all image request flags is 0. Each
1363 * is conditionally set to 1 at image request initialization time
1364 * and currently never change thereafter.
1365 */
Alex Elderd0b2e942013-01-24 16:13:36 -06001366static void img_request_layered_set(struct rbd_img_request *img_request)
1367{
1368 set_bit(IMG_REQ_LAYERED, &img_request->flags);
1369 smp_mb();
1370}
1371
Alex Eldera2acd002013-05-08 22:50:04 -05001372static void img_request_layered_clear(struct rbd_img_request *img_request)
1373{
1374 clear_bit(IMG_REQ_LAYERED, &img_request->flags);
1375 smp_mb();
1376}
1377
Alex Elderd0b2e942013-01-24 16:13:36 -06001378static bool img_request_layered_test(struct rbd_img_request *img_request)
1379{
1380 smp_mb();
1381 return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0;
1382}
1383
Ilya Dryomov3da691b2018-01-29 14:04:08 +01001384static bool rbd_obj_is_entire(struct rbd_obj_request *obj_req)
Josh Durgin3b434a2a2014-04-04 17:32:15 -07001385{
Ilya Dryomov3da691b2018-01-29 14:04:08 +01001386 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
1387
Ilya Dryomov43df3d32018-02-02 15:23:22 +01001388 return !obj_req->ex.oe_off &&
1389 obj_req->ex.oe_len == rbd_dev->layout.object_size;
Josh Durgin3b434a2a2014-04-04 17:32:15 -07001390}
1391
Ilya Dryomov3da691b2018-01-29 14:04:08 +01001392static bool rbd_obj_is_tail(struct rbd_obj_request *obj_req)
Alex Elder6e2a4502013-03-27 09:16:30 -05001393{
Ilya Dryomov3da691b2018-01-29 14:04:08 +01001394 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
Alex Elderb9434c52013-04-19 15:34:50 -05001395
Ilya Dryomov43df3d32018-02-02 15:23:22 +01001396 return obj_req->ex.oe_off + obj_req->ex.oe_len ==
Ilya Dryomov3da691b2018-01-29 14:04:08 +01001397 rbd_dev->layout.object_size;
1398}
1399
Ilya Dryomov13488d52019-02-25 12:37:50 +01001400/*
1401 * Must be called after rbd_obj_calc_img_extents().
1402 */
1403static bool rbd_obj_copyup_enabled(struct rbd_obj_request *obj_req)
1404{
1405 if (!obj_req->num_img_extents ||
Ilya Dryomov9b17eb22019-02-28 15:51:39 +01001406 (rbd_obj_is_entire(obj_req) &&
1407 !obj_req->img_request->snapc->num_snaps))
Ilya Dryomov13488d52019-02-25 12:37:50 +01001408 return false;
1409
1410 return true;
1411}
1412
Ilya Dryomov86bd7992018-02-06 19:26:33 +01001413static u64 rbd_obj_img_extents_bytes(struct rbd_obj_request *obj_req)
1414{
1415 return ceph_file_extents_bytes(obj_req->img_extents,
1416 obj_req->num_img_extents);
1417}
1418
Ilya Dryomov3da691b2018-01-29 14:04:08 +01001419static bool rbd_img_is_write(struct rbd_img_request *img_req)
1420{
Ilya Dryomov9bb02482018-01-30 17:52:10 +01001421 switch (img_req->op_type) {
Ilya Dryomov3da691b2018-01-29 14:04:08 +01001422 case OBJ_OP_READ:
1423 return false;
1424 case OBJ_OP_WRITE:
1425 case OBJ_OP_DISCARD:
Ilya Dryomov6484cbe2019-01-29 12:46:25 +01001426 case OBJ_OP_ZEROOUT:
Ilya Dryomov3da691b2018-01-29 14:04:08 +01001427 return true;
1428 default:
Arnd Bergmannc6244b32018-04-04 14:53:39 +02001429 BUG();
Alex Elder6e2a4502013-03-27 09:16:30 -05001430 }
Alex Elder6e2a4502013-03-27 09:16:30 -05001431}
1432
Ilya Dryomov85e084f2016-04-28 16:07:24 +02001433static void rbd_osd_req_callback(struct ceph_osd_request *osd_req)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001434{
Ilya Dryomov3da691b2018-01-29 14:04:08 +01001435 struct rbd_obj_request *obj_req = osd_req->r_priv;
Ilya Dryomov54ab3b22019-05-11 16:21:49 +02001436 int result;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001437
Ilya Dryomov3da691b2018-01-29 14:04:08 +01001438 dout("%s osd_req %p result %d for obj_req %p\n", __func__, osd_req,
1439 osd_req->r_result, obj_req);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001440
Ilya Dryomov54ab3b22019-05-11 16:21:49 +02001441 /*
1442 * Writes aren't allowed to return a data payload. In some
1443 * guarded write cases (e.g. stat + zero on an empty object)
1444 * a stat response makes it through, but we don't care.
1445 */
1446 if (osd_req->r_result > 0 && rbd_img_is_write(obj_req->img_request))
1447 result = 0;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01001448 else
Ilya Dryomov54ab3b22019-05-11 16:21:49 +02001449 result = osd_req->r_result;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001450
Ilya Dryomov54ab3b22019-05-11 16:21:49 +02001451 rbd_obj_handle_request(obj_req, result);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001452}
1453
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02001454static void rbd_osd_format_read(struct ceph_osd_request *osd_req)
Alex Elder430c28c2013-04-03 21:32:51 -05001455{
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02001456 struct rbd_obj_request *obj_request = osd_req->r_priv;
Alex Elder430c28c2013-04-03 21:32:51 -05001457
Ilya Dryomova162b302018-01-30 17:52:10 +01001458 osd_req->r_flags = CEPH_OSD_FLAG_READ;
Ilya Dryomov7c848832016-09-15 17:56:39 +02001459 osd_req->r_snapid = obj_request->img_request->snap_id;
Alex Elder9d4df012013-04-19 15:34:50 -05001460}
1461
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02001462static void rbd_osd_format_write(struct ceph_osd_request *osd_req)
Alex Elder9d4df012013-04-19 15:34:50 -05001463{
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02001464 struct rbd_obj_request *obj_request = osd_req->r_priv;
Alex Elder9d4df012013-04-19 15:34:50 -05001465
Ilya Dryomova162b302018-01-30 17:52:10 +01001466 osd_req->r_flags = CEPH_OSD_FLAG_WRITE;
Arnd Bergmannfac02dd2018-07-13 22:18:37 +02001467 ktime_get_real_ts64(&osd_req->r_mtime);
Ilya Dryomov43df3d32018-02-02 15:23:22 +01001468 osd_req->r_data_offset = obj_request->ex.oe_off;
Alex Elder430c28c2013-04-03 21:32:51 -05001469}
1470
Ilya Dryomovbc812072017-01-25 18:16:23 +01001471static struct ceph_osd_request *
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02001472__rbd_obj_add_osd_request(struct rbd_obj_request *obj_req,
1473 struct ceph_snap_context *snapc, int num_ops)
Ilya Dryomovbc812072017-01-25 18:16:23 +01001474{
Ilya Dryomove28eded2019-02-25 11:42:26 +01001475 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
Ilya Dryomovbc812072017-01-25 18:16:23 +01001476 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
1477 struct ceph_osd_request *req;
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01001478 const char *name_format = rbd_dev->image_format == 1 ?
1479 RBD_V1_DATA_FORMAT : RBD_V2_DATA_FORMAT;
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02001480 int ret;
Ilya Dryomovbc812072017-01-25 18:16:23 +01001481
Ilya Dryomove28eded2019-02-25 11:42:26 +01001482 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, false, GFP_NOIO);
Ilya Dryomovbc812072017-01-25 18:16:23 +01001483 if (!req)
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02001484 return ERR_PTR(-ENOMEM);
Ilya Dryomovbc812072017-01-25 18:16:23 +01001485
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02001486 list_add_tail(&req->r_private_item, &obj_req->osd_reqs);
Ilya Dryomovbc812072017-01-25 18:16:23 +01001487 req->r_callback = rbd_osd_req_callback;
Ilya Dryomova162b302018-01-30 17:52:10 +01001488 req->r_priv = obj_req;
Ilya Dryomovbc812072017-01-25 18:16:23 +01001489
Ilya Dryomovb26c0472018-07-03 15:28:43 +02001490 /*
1491 * Data objects may be stored in a separate pool, but always in
1492 * the same namespace in that pool as the header in its pool.
1493 */
1494 ceph_oloc_copy(&req->r_base_oloc, &rbd_dev->header_oloc);
Ilya Dryomovbc812072017-01-25 18:16:23 +01001495 req->r_base_oloc.pool = rbd_dev->layout.pool_id;
Ilya Dryomovb26c0472018-07-03 15:28:43 +02001496
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02001497 ret = ceph_oid_aprintf(&req->r_base_oid, GFP_NOIO, name_format,
1498 rbd_dev->header.object_prefix,
1499 obj_req->ex.oe_objno);
1500 if (ret)
1501 return ERR_PTR(ret);
Ilya Dryomovbc812072017-01-25 18:16:23 +01001502
Ilya Dryomovbc812072017-01-25 18:16:23 +01001503 return req;
Ilya Dryomovbc812072017-01-25 18:16:23 +01001504}
1505
Ilya Dryomove28eded2019-02-25 11:42:26 +01001506static struct ceph_osd_request *
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02001507rbd_obj_add_osd_request(struct rbd_obj_request *obj_req, int num_ops)
Ilya Dryomove28eded2019-02-25 11:42:26 +01001508{
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02001509 return __rbd_obj_add_osd_request(obj_req, obj_req->img_request->snapc,
1510 num_ops);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001511}
1512
Ilya Dryomovecc633c2018-02-01 11:50:47 +01001513static struct rbd_obj_request *rbd_obj_request_create(void)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001514{
1515 struct rbd_obj_request *obj_request;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001516
Ilya Dryomov5a60e872015-06-24 17:24:33 +03001517 obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_NOIO);
Ilya Dryomov6c696d82017-01-25 18:16:23 +01001518 if (!obj_request)
Alex Elderf907ad52013-05-01 12:43:03 -05001519 return NULL;
Alex Elderf907ad52013-05-01 12:43:03 -05001520
Ilya Dryomov43df3d32018-02-02 15:23:22 +01001521 ceph_object_extent_init(&obj_request->ex);
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02001522 INIT_LIST_HEAD(&obj_request->osd_reqs);
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02001523 mutex_init(&obj_request->state_mutex);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001524 kref_init(&obj_request->kref);
1525
Ilya Dryomov67e2b652017-01-25 18:16:22 +01001526 dout("%s %p\n", __func__, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001527 return obj_request;
1528}
1529
1530static void rbd_obj_request_destroy(struct kref *kref)
1531{
1532 struct rbd_obj_request *obj_request;
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02001533 struct ceph_osd_request *osd_req;
Ilya Dryomov7e07efb2018-01-20 10:30:11 +01001534 u32 i;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001535
1536 obj_request = container_of(kref, struct rbd_obj_request, kref);
1537
Alex Elder37206ee2013-02-20 17:32:08 -06001538 dout("%s: obj %p\n", __func__, obj_request);
1539
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02001540 while (!list_empty(&obj_request->osd_reqs)) {
1541 osd_req = list_first_entry(&obj_request->osd_reqs,
1542 struct ceph_osd_request, r_private_item);
1543 list_del_init(&osd_req->r_private_item);
1544 ceph_osdc_put_request(osd_req);
1545 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06001546
Ilya Dryomovecc633c2018-02-01 11:50:47 +01001547 switch (obj_request->img_request->data_type) {
Alex Elder9969ebc2013-01-18 12:31:10 -06001548 case OBJ_REQUEST_NODATA:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001549 case OBJ_REQUEST_BIO:
Ilya Dryomov7e07efb2018-01-20 10:30:11 +01001550 case OBJ_REQUEST_BVECS:
Ilya Dryomov5359a172018-01-20 10:30:10 +01001551 break; /* Nothing to do */
Ilya Dryomovafb97882018-02-06 19:26:35 +01001552 case OBJ_REQUEST_OWN_BVECS:
1553 kfree(obj_request->bvec_pos.bvecs);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001554 break;
Ilya Dryomov7e07efb2018-01-20 10:30:11 +01001555 default:
Arnd Bergmann16809372019-03-22 17:53:56 +01001556 BUG();
Alex Elderbf0d5f502012-11-22 00:00:08 -06001557 }
1558
Ilya Dryomov86bd7992018-02-06 19:26:33 +01001559 kfree(obj_request->img_extents);
Ilya Dryomov7e07efb2018-01-20 10:30:11 +01001560 if (obj_request->copyup_bvecs) {
1561 for (i = 0; i < obj_request->copyup_bvec_count; i++) {
1562 if (obj_request->copyup_bvecs[i].bv_page)
1563 __free_page(obj_request->copyup_bvecs[i].bv_page);
1564 }
1565 kfree(obj_request->copyup_bvecs);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001566 }
1567
Alex Elder868311b2013-05-01 12:43:03 -05001568 kmem_cache_free(rbd_obj_request_cache, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001569}
1570
Alex Elderfb65d2282013-05-08 22:50:04 -05001571/* It's OK to call this for a device with no parent */
1572
1573static void rbd_spec_put(struct rbd_spec *spec);
1574static void rbd_dev_unparent(struct rbd_device *rbd_dev)
1575{
1576 rbd_dev_remove_parent(rbd_dev);
1577 rbd_spec_put(rbd_dev->parent_spec);
1578 rbd_dev->parent_spec = NULL;
1579 rbd_dev->parent_overlap = 0;
1580}
1581
Alex Elderbf0d5f502012-11-22 00:00:08 -06001582/*
Alex Eldera2acd002013-05-08 22:50:04 -05001583 * Parent image reference counting is used to determine when an
1584 * image's parent fields can be safely torn down--after there are no
1585 * more in-flight requests to the parent image. When the last
1586 * reference is dropped, cleaning them up is safe.
1587 */
1588static void rbd_dev_parent_put(struct rbd_device *rbd_dev)
1589{
1590 int counter;
1591
1592 if (!rbd_dev->parent_spec)
1593 return;
1594
1595 counter = atomic_dec_return_safe(&rbd_dev->parent_ref);
1596 if (counter > 0)
1597 return;
1598
1599 /* Last reference; clean up parent data structures */
1600
1601 if (!counter)
1602 rbd_dev_unparent(rbd_dev);
1603 else
Ilya Dryomov9584d502014-07-11 12:11:20 +04001604 rbd_warn(rbd_dev, "parent reference underflow");
Alex Eldera2acd002013-05-08 22:50:04 -05001605}
1606
1607/*
1608 * If an image has a non-zero parent overlap, get a reference to its
1609 * parent.
1610 *
1611 * Returns true if the rbd device has a parent with a non-zero
1612 * overlap and a reference for it was successfully taken, or
1613 * false otherwise.
1614 */
1615static bool rbd_dev_parent_get(struct rbd_device *rbd_dev)
1616{
Ilya Dryomovae43e9d2015-01-19 18:13:43 +03001617 int counter = 0;
Alex Eldera2acd002013-05-08 22:50:04 -05001618
1619 if (!rbd_dev->parent_spec)
1620 return false;
1621
Ilya Dryomovae43e9d2015-01-19 18:13:43 +03001622 down_read(&rbd_dev->header_rwsem);
1623 if (rbd_dev->parent_overlap)
1624 counter = atomic_inc_return_safe(&rbd_dev->parent_ref);
1625 up_read(&rbd_dev->header_rwsem);
Alex Eldera2acd002013-05-08 22:50:04 -05001626
1627 if (counter < 0)
Ilya Dryomov9584d502014-07-11 12:11:20 +04001628 rbd_warn(rbd_dev, "parent reference overflow");
Alex Eldera2acd002013-05-08 22:50:04 -05001629
Ilya Dryomovae43e9d2015-01-19 18:13:43 +03001630 return counter > 0;
Alex Eldera2acd002013-05-08 22:50:04 -05001631}
1632
Alex Elderbf0d5f502012-11-22 00:00:08 -06001633/*
1634 * Caller is responsible for filling in the list of object requests
1635 * that comprises the image request, and the Linux request pointer
1636 * (if there is one).
1637 */
Alex Eldercc344fa2013-02-19 12:25:56 -06001638static struct rbd_img_request *rbd_img_request_create(
1639 struct rbd_device *rbd_dev,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08001640 enum obj_operation_type op_type,
Josh Durgin4e752f02014-04-08 11:12:11 -07001641 struct ceph_snap_context *snapc)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001642{
1643 struct rbd_img_request *img_request;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001644
Ilya Dryomova0c58952018-01-22 16:03:06 +01001645 img_request = kmem_cache_zalloc(rbd_img_request_cache, GFP_NOIO);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001646 if (!img_request)
1647 return NULL;
1648
Alex Elderbf0d5f502012-11-22 00:00:08 -06001649 img_request->rbd_dev = rbd_dev;
Ilya Dryomov9bb02482018-01-30 17:52:10 +01001650 img_request->op_type = op_type;
Ilya Dryomov9bb02482018-01-30 17:52:10 +01001651 if (!rbd_img_is_write(img_request))
Alex Elderbf0d5f502012-11-22 00:00:08 -06001652 img_request->snap_id = rbd_dev->spec->snap_id;
Ilya Dryomov9bb02482018-01-30 17:52:10 +01001653 else
1654 img_request->snapc = snapc;
1655
Alex Eldera2acd002013-05-08 22:50:04 -05001656 if (rbd_dev_parent_get(rbd_dev))
Alex Elderd0b2e942013-01-24 16:13:36 -06001657 img_request_layered_set(img_request);
Ilya Dryomova0c58952018-01-22 16:03:06 +01001658
Ilya Dryomove1fddc82019-05-30 16:07:48 +02001659 INIT_LIST_HEAD(&img_request->lock_item);
Ilya Dryomov43df3d32018-02-02 15:23:22 +01001660 INIT_LIST_HEAD(&img_request->object_extents);
Ilya Dryomov0192ce22019-05-16 15:06:56 +02001661 mutex_init(&img_request->state_mutex);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001662 kref_init(&img_request->kref);
1663
Alex Elderbf0d5f502012-11-22 00:00:08 -06001664 return img_request;
1665}
1666
1667static void rbd_img_request_destroy(struct kref *kref)
1668{
1669 struct rbd_img_request *img_request;
1670 struct rbd_obj_request *obj_request;
1671 struct rbd_obj_request *next_obj_request;
1672
1673 img_request = container_of(kref, struct rbd_img_request, kref);
1674
Alex Elder37206ee2013-02-20 17:32:08 -06001675 dout("%s: img %p\n", __func__, img_request);
1676
Ilya Dryomove1fddc82019-05-30 16:07:48 +02001677 WARN_ON(!list_empty(&img_request->lock_item));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001678 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
1679 rbd_img_obj_request_del(img_request, obj_request);
1680
Alex Eldera2acd002013-05-08 22:50:04 -05001681 if (img_request_layered_test(img_request)) {
1682 img_request_layered_clear(img_request);
1683 rbd_dev_parent_put(img_request->rbd_dev);
1684 }
1685
Ilya Dryomov9bb02482018-01-30 17:52:10 +01001686 if (rbd_img_is_write(img_request))
Alex Elder812164f82013-04-30 00:44:32 -05001687 ceph_put_snap_context(img_request->snapc);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001688
Alex Elder1c2a9df2013-05-01 12:43:03 -05001689 kmem_cache_free(rbd_img_request_cache, img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001690}
1691
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02001692#define BITS_PER_OBJ 2
1693#define OBJS_PER_BYTE (BITS_PER_BYTE / BITS_PER_OBJ)
1694#define OBJ_MASK ((1 << BITS_PER_OBJ) - 1)
1695
1696static void __rbd_object_map_index(struct rbd_device *rbd_dev, u64 objno,
1697 u64 *index, u8 *shift)
1698{
1699 u32 off;
1700
1701 rbd_assert(objno < rbd_dev->object_map_size);
1702 *index = div_u64_rem(objno, OBJS_PER_BYTE, &off);
1703 *shift = (OBJS_PER_BYTE - off - 1) * BITS_PER_OBJ;
1704}
1705
1706static u8 __rbd_object_map_get(struct rbd_device *rbd_dev, u64 objno)
1707{
1708 u64 index;
1709 u8 shift;
1710
1711 lockdep_assert_held(&rbd_dev->object_map_lock);
1712 __rbd_object_map_index(rbd_dev, objno, &index, &shift);
1713 return (rbd_dev->object_map[index] >> shift) & OBJ_MASK;
1714}
1715
1716static void __rbd_object_map_set(struct rbd_device *rbd_dev, u64 objno, u8 val)
1717{
1718 u64 index;
1719 u8 shift;
1720 u8 *p;
1721
1722 lockdep_assert_held(&rbd_dev->object_map_lock);
1723 rbd_assert(!(val & ~OBJ_MASK));
1724
1725 __rbd_object_map_index(rbd_dev, objno, &index, &shift);
1726 p = &rbd_dev->object_map[index];
1727 *p = (*p & ~(OBJ_MASK << shift)) | (val << shift);
1728}
1729
1730static u8 rbd_object_map_get(struct rbd_device *rbd_dev, u64 objno)
1731{
1732 u8 state;
1733
1734 spin_lock(&rbd_dev->object_map_lock);
1735 state = __rbd_object_map_get(rbd_dev, objno);
1736 spin_unlock(&rbd_dev->object_map_lock);
1737 return state;
1738}
1739
1740static bool use_object_map(struct rbd_device *rbd_dev)
1741{
Ilya Dryomov3fe69922019-11-12 19:41:48 +01001742 /*
1743 * An image mapped read-only can't use the object map -- it isn't
1744 * loaded because the header lock isn't acquired. Someone else can
1745 * write to the image and update the object map behind our back.
1746 *
1747 * A snapshot can't be written to, so using the object map is always
1748 * safe.
1749 */
1750 if (!rbd_is_snap(rbd_dev) && rbd_is_ro(rbd_dev))
1751 return false;
1752
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02001753 return ((rbd_dev->header.features & RBD_FEATURE_OBJECT_MAP) &&
1754 !(rbd_dev->object_map_flags & RBD_FLAG_OBJECT_MAP_INVALID));
1755}
1756
1757static bool rbd_object_map_may_exist(struct rbd_device *rbd_dev, u64 objno)
1758{
1759 u8 state;
1760
1761 /* fall back to default logic if object map is disabled or invalid */
1762 if (!use_object_map(rbd_dev))
1763 return true;
1764
1765 state = rbd_object_map_get(rbd_dev, objno);
1766 return state != OBJECT_NONEXISTENT;
1767}
1768
1769static void rbd_object_map_name(struct rbd_device *rbd_dev, u64 snap_id,
1770 struct ceph_object_id *oid)
1771{
1772 if (snap_id == CEPH_NOSNAP)
1773 ceph_oid_printf(oid, "%s%s", RBD_OBJECT_MAP_PREFIX,
1774 rbd_dev->spec->image_id);
1775 else
1776 ceph_oid_printf(oid, "%s%s.%016llx", RBD_OBJECT_MAP_PREFIX,
1777 rbd_dev->spec->image_id, snap_id);
1778}
1779
1780static int rbd_object_map_lock(struct rbd_device *rbd_dev)
1781{
1782 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
1783 CEPH_DEFINE_OID_ONSTACK(oid);
1784 u8 lock_type;
1785 char *lock_tag;
1786 struct ceph_locker *lockers;
1787 u32 num_lockers;
1788 bool broke_lock = false;
1789 int ret;
1790
1791 rbd_object_map_name(rbd_dev, CEPH_NOSNAP, &oid);
1792
1793again:
1794 ret = ceph_cls_lock(osdc, &oid, &rbd_dev->header_oloc, RBD_LOCK_NAME,
1795 CEPH_CLS_LOCK_EXCLUSIVE, "", "", "", 0);
1796 if (ret != -EBUSY || broke_lock) {
1797 if (ret == -EEXIST)
1798 ret = 0; /* already locked by myself */
1799 if (ret)
1800 rbd_warn(rbd_dev, "failed to lock object map: %d", ret);
1801 return ret;
1802 }
1803
1804 ret = ceph_cls_lock_info(osdc, &oid, &rbd_dev->header_oloc,
1805 RBD_LOCK_NAME, &lock_type, &lock_tag,
1806 &lockers, &num_lockers);
1807 if (ret) {
1808 if (ret == -ENOENT)
1809 goto again;
1810
1811 rbd_warn(rbd_dev, "failed to get object map lockers: %d", ret);
1812 return ret;
1813 }
1814
1815 kfree(lock_tag);
1816 if (num_lockers == 0)
1817 goto again;
1818
1819 rbd_warn(rbd_dev, "breaking object map lock owned by %s%llu",
1820 ENTITY_NAME(lockers[0].id.name));
1821
1822 ret = ceph_cls_break_lock(osdc, &oid, &rbd_dev->header_oloc,
1823 RBD_LOCK_NAME, lockers[0].id.cookie,
1824 &lockers[0].id.name);
1825 ceph_free_lockers(lockers, num_lockers);
1826 if (ret) {
1827 if (ret == -ENOENT)
1828 goto again;
1829
1830 rbd_warn(rbd_dev, "failed to break object map lock: %d", ret);
1831 return ret;
1832 }
1833
1834 broke_lock = true;
1835 goto again;
1836}
1837
1838static void rbd_object_map_unlock(struct rbd_device *rbd_dev)
1839{
1840 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
1841 CEPH_DEFINE_OID_ONSTACK(oid);
1842 int ret;
1843
1844 rbd_object_map_name(rbd_dev, CEPH_NOSNAP, &oid);
1845
1846 ret = ceph_cls_unlock(osdc, &oid, &rbd_dev->header_oloc, RBD_LOCK_NAME,
1847 "");
1848 if (ret && ret != -ENOENT)
1849 rbd_warn(rbd_dev, "failed to unlock object map: %d", ret);
1850}
1851
1852static int decode_object_map_header(void **p, void *end, u64 *object_map_size)
1853{
1854 u8 struct_v;
1855 u32 struct_len;
1856 u32 header_len;
1857 void *header_end;
1858 int ret;
1859
1860 ceph_decode_32_safe(p, end, header_len, e_inval);
1861 header_end = *p + header_len;
1862
1863 ret = ceph_start_decoding(p, end, 1, "BitVector header", &struct_v,
1864 &struct_len);
1865 if (ret)
1866 return ret;
1867
1868 ceph_decode_64_safe(p, end, *object_map_size, e_inval);
1869
1870 *p = header_end;
1871 return 0;
1872
1873e_inval:
1874 return -EINVAL;
1875}
1876
1877static int __rbd_object_map_load(struct rbd_device *rbd_dev)
1878{
1879 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
1880 CEPH_DEFINE_OID_ONSTACK(oid);
1881 struct page **pages;
1882 void *p, *end;
1883 size_t reply_len;
1884 u64 num_objects;
1885 u64 object_map_bytes;
1886 u64 object_map_size;
1887 int num_pages;
1888 int ret;
1889
1890 rbd_assert(!rbd_dev->object_map && !rbd_dev->object_map_size);
1891
1892 num_objects = ceph_get_num_objects(&rbd_dev->layout,
1893 rbd_dev->mapping.size);
1894 object_map_bytes = DIV_ROUND_UP_ULL(num_objects * BITS_PER_OBJ,
1895 BITS_PER_BYTE);
1896 num_pages = calc_pages_for(0, object_map_bytes) + 1;
1897 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
1898 if (IS_ERR(pages))
1899 return PTR_ERR(pages);
1900
1901 reply_len = num_pages * PAGE_SIZE;
1902 rbd_object_map_name(rbd_dev, rbd_dev->spec->snap_id, &oid);
1903 ret = ceph_osdc_call(osdc, &oid, &rbd_dev->header_oloc,
1904 "rbd", "object_map_load", CEPH_OSD_FLAG_READ,
1905 NULL, 0, pages, &reply_len);
1906 if (ret)
1907 goto out;
1908
1909 p = page_address(pages[0]);
1910 end = p + min(reply_len, (size_t)PAGE_SIZE);
1911 ret = decode_object_map_header(&p, end, &object_map_size);
1912 if (ret)
1913 goto out;
1914
1915 if (object_map_size != num_objects) {
1916 rbd_warn(rbd_dev, "object map size mismatch: %llu vs %llu",
1917 object_map_size, num_objects);
1918 ret = -EINVAL;
1919 goto out;
1920 }
1921
1922 if (offset_in_page(p) + object_map_bytes > reply_len) {
1923 ret = -EINVAL;
1924 goto out;
1925 }
1926
1927 rbd_dev->object_map = kvmalloc(object_map_bytes, GFP_KERNEL);
1928 if (!rbd_dev->object_map) {
1929 ret = -ENOMEM;
1930 goto out;
1931 }
1932
1933 rbd_dev->object_map_size = object_map_size;
1934 ceph_copy_from_page_vector(pages, rbd_dev->object_map,
1935 offset_in_page(p), object_map_bytes);
1936
1937out:
1938 ceph_release_page_vector(pages, num_pages);
1939 return ret;
1940}
1941
1942static void rbd_object_map_free(struct rbd_device *rbd_dev)
1943{
1944 kvfree(rbd_dev->object_map);
1945 rbd_dev->object_map = NULL;
1946 rbd_dev->object_map_size = 0;
1947}
1948
1949static int rbd_object_map_load(struct rbd_device *rbd_dev)
1950{
1951 int ret;
1952
1953 ret = __rbd_object_map_load(rbd_dev);
1954 if (ret)
1955 return ret;
1956
1957 ret = rbd_dev_v2_get_flags(rbd_dev);
1958 if (ret) {
1959 rbd_object_map_free(rbd_dev);
1960 return ret;
1961 }
1962
1963 if (rbd_dev->object_map_flags & RBD_FLAG_OBJECT_MAP_INVALID)
1964 rbd_warn(rbd_dev, "object map is invalid");
1965
1966 return 0;
1967}
1968
1969static int rbd_object_map_open(struct rbd_device *rbd_dev)
1970{
1971 int ret;
1972
1973 ret = rbd_object_map_lock(rbd_dev);
1974 if (ret)
1975 return ret;
1976
1977 ret = rbd_object_map_load(rbd_dev);
1978 if (ret) {
1979 rbd_object_map_unlock(rbd_dev);
1980 return ret;
1981 }
1982
1983 return 0;
1984}
1985
1986static void rbd_object_map_close(struct rbd_device *rbd_dev)
1987{
1988 rbd_object_map_free(rbd_dev);
1989 rbd_object_map_unlock(rbd_dev);
1990}
1991
1992/*
1993 * This function needs snap_id (or more precisely just something to
1994 * distinguish between HEAD and snapshot object maps), new_state and
1995 * current_state that were passed to rbd_object_map_update().
1996 *
1997 * To avoid allocating and stashing a context we piggyback on the OSD
1998 * request. A HEAD update has two ops (assert_locked). For new_state
1999 * and current_state we decode our own object_map_update op, encoded in
2000 * rbd_cls_object_map_update().
2001 */
2002static int rbd_object_map_update_finish(struct rbd_obj_request *obj_req,
2003 struct ceph_osd_request *osd_req)
2004{
2005 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
2006 struct ceph_osd_data *osd_data;
2007 u64 objno;
Ilya Dryomov633739b2019-11-13 12:07:15 +01002008 u8 state, new_state, uninitialized_var(current_state);
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02002009 bool has_current_state;
2010 void *p;
2011
2012 if (osd_req->r_result)
2013 return osd_req->r_result;
2014
2015 /*
2016 * Nothing to do for a snapshot object map.
2017 */
2018 if (osd_req->r_num_ops == 1)
2019 return 0;
2020
2021 /*
2022 * Update in-memory HEAD object map.
2023 */
2024 rbd_assert(osd_req->r_num_ops == 2);
2025 osd_data = osd_req_op_data(osd_req, 1, cls, request_data);
2026 rbd_assert(osd_data->type == CEPH_OSD_DATA_TYPE_PAGES);
2027
2028 p = page_address(osd_data->pages[0]);
2029 objno = ceph_decode_64(&p);
2030 rbd_assert(objno == obj_req->ex.oe_objno);
2031 rbd_assert(ceph_decode_64(&p) == objno + 1);
2032 new_state = ceph_decode_8(&p);
2033 has_current_state = ceph_decode_8(&p);
2034 if (has_current_state)
2035 current_state = ceph_decode_8(&p);
2036
2037 spin_lock(&rbd_dev->object_map_lock);
2038 state = __rbd_object_map_get(rbd_dev, objno);
2039 if (!has_current_state || current_state == state ||
2040 (current_state == OBJECT_EXISTS && state == OBJECT_EXISTS_CLEAN))
2041 __rbd_object_map_set(rbd_dev, objno, new_state);
2042 spin_unlock(&rbd_dev->object_map_lock);
2043
2044 return 0;
2045}
2046
2047static void rbd_object_map_callback(struct ceph_osd_request *osd_req)
2048{
2049 struct rbd_obj_request *obj_req = osd_req->r_priv;
2050 int result;
2051
2052 dout("%s osd_req %p result %d for obj_req %p\n", __func__, osd_req,
2053 osd_req->r_result, obj_req);
2054
2055 result = rbd_object_map_update_finish(obj_req, osd_req);
2056 rbd_obj_handle_request(obj_req, result);
2057}
2058
2059static bool update_needed(struct rbd_device *rbd_dev, u64 objno, u8 new_state)
2060{
2061 u8 state = rbd_object_map_get(rbd_dev, objno);
2062
2063 if (state == new_state ||
2064 (new_state == OBJECT_PENDING && state == OBJECT_NONEXISTENT) ||
2065 (new_state == OBJECT_NONEXISTENT && state != OBJECT_PENDING))
2066 return false;
2067
2068 return true;
2069}
2070
2071static int rbd_cls_object_map_update(struct ceph_osd_request *req,
2072 int which, u64 objno, u8 new_state,
2073 const u8 *current_state)
2074{
2075 struct page **pages;
2076 void *p, *start;
2077 int ret;
2078
2079 ret = osd_req_op_cls_init(req, which, "rbd", "object_map_update");
2080 if (ret)
2081 return ret;
2082
2083 pages = ceph_alloc_page_vector(1, GFP_NOIO);
2084 if (IS_ERR(pages))
2085 return PTR_ERR(pages);
2086
2087 p = start = page_address(pages[0]);
2088 ceph_encode_64(&p, objno);
2089 ceph_encode_64(&p, objno + 1);
2090 ceph_encode_8(&p, new_state);
2091 if (current_state) {
2092 ceph_encode_8(&p, 1);
2093 ceph_encode_8(&p, *current_state);
2094 } else {
2095 ceph_encode_8(&p, 0);
2096 }
2097
2098 osd_req_op_cls_request_data_pages(req, which, pages, p - start, 0,
2099 false, true);
2100 return 0;
2101}
2102
2103/*
2104 * Return:
2105 * 0 - object map update sent
2106 * 1 - object map update isn't needed
2107 * <0 - error
2108 */
2109static int rbd_object_map_update(struct rbd_obj_request *obj_req, u64 snap_id,
2110 u8 new_state, const u8 *current_state)
2111{
2112 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
2113 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
2114 struct ceph_osd_request *req;
2115 int num_ops = 1;
2116 int which = 0;
2117 int ret;
2118
2119 if (snap_id == CEPH_NOSNAP) {
2120 if (!update_needed(rbd_dev, obj_req->ex.oe_objno, new_state))
2121 return 1;
2122
2123 num_ops++; /* assert_locked */
2124 }
2125
2126 req = ceph_osdc_alloc_request(osdc, NULL, num_ops, false, GFP_NOIO);
2127 if (!req)
2128 return -ENOMEM;
2129
2130 list_add_tail(&req->r_private_item, &obj_req->osd_reqs);
2131 req->r_callback = rbd_object_map_callback;
2132 req->r_priv = obj_req;
2133
2134 rbd_object_map_name(rbd_dev, snap_id, &req->r_base_oid);
2135 ceph_oloc_copy(&req->r_base_oloc, &rbd_dev->header_oloc);
2136 req->r_flags = CEPH_OSD_FLAG_WRITE;
2137 ktime_get_real_ts64(&req->r_mtime);
2138
2139 if (snap_id == CEPH_NOSNAP) {
2140 /*
2141 * Protect against possible race conditions during lock
2142 * ownership transitions.
2143 */
2144 ret = ceph_cls_assert_locked(req, which++, RBD_LOCK_NAME,
2145 CEPH_CLS_LOCK_EXCLUSIVE, "", "");
2146 if (ret)
2147 return ret;
2148 }
2149
2150 ret = rbd_cls_object_map_update(req, which, obj_req->ex.oe_objno,
2151 new_state, current_state);
2152 if (ret)
2153 return ret;
2154
2155 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
2156 if (ret)
2157 return ret;
2158
2159 ceph_osdc_start_request(osdc, req, false);
2160 return 0;
2161}
2162
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002163static void prune_extents(struct ceph_file_extent *img_extents,
2164 u32 *num_img_extents, u64 overlap)
Alex Eldere93f3152013-05-08 22:50:04 -05002165{
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002166 u32 cnt = *num_img_extents;
Alex Eldere93f3152013-05-08 22:50:04 -05002167
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002168 /* drop extents completely beyond the overlap */
2169 while (cnt && img_extents[cnt - 1].fe_off >= overlap)
2170 cnt--;
Alex Eldere93f3152013-05-08 22:50:04 -05002171
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002172 if (cnt) {
2173 struct ceph_file_extent *ex = &img_extents[cnt - 1];
Alex Eldere93f3152013-05-08 22:50:04 -05002174
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002175 /* trim final overlapping extent */
2176 if (ex->fe_off + ex->fe_len > overlap)
2177 ex->fe_len = overlap - ex->fe_off;
Alex Elder12178572013-02-08 09:55:49 -06002178 }
2179
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002180 *num_img_extents = cnt;
Alex Elder21692382013-04-05 01:27:12 -05002181}
2182
Alex Elderf1a47392013-04-19 15:34:50 -05002183/*
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002184 * Determine the byte range(s) covered by either just the object extent
2185 * or the entire object in the parent image.
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002186 */
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002187static int rbd_obj_calc_img_extents(struct rbd_obj_request *obj_req,
2188 bool entire)
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002189{
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002190 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002191 int ret;
2192
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002193 if (!rbd_dev->parent_overlap)
2194 return 0;
2195
2196 ret = ceph_extent_to_file(&rbd_dev->layout, obj_req->ex.oe_objno,
2197 entire ? 0 : obj_req->ex.oe_off,
2198 entire ? rbd_dev->layout.object_size :
2199 obj_req->ex.oe_len,
2200 &obj_req->img_extents,
2201 &obj_req->num_img_extents);
2202 if (ret)
2203 return ret;
2204
2205 prune_extents(obj_req->img_extents, &obj_req->num_img_extents,
2206 rbd_dev->parent_overlap);
2207 return 0;
2208}
2209
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02002210static void rbd_osd_setup_data(struct ceph_osd_request *osd_req, int which)
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002211{
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02002212 struct rbd_obj_request *obj_req = osd_req->r_priv;
2213
Ilya Dryomovecc633c2018-02-01 11:50:47 +01002214 switch (obj_req->img_request->data_type) {
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002215 case OBJ_REQUEST_BIO:
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02002216 osd_req_op_extent_osd_data_bio(osd_req, which,
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002217 &obj_req->bio_pos,
Ilya Dryomov43df3d32018-02-02 15:23:22 +01002218 obj_req->ex.oe_len);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002219 break;
2220 case OBJ_REQUEST_BVECS:
Ilya Dryomovafb97882018-02-06 19:26:35 +01002221 case OBJ_REQUEST_OWN_BVECS:
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002222 rbd_assert(obj_req->bvec_pos.iter.bi_size ==
Ilya Dryomov43df3d32018-02-02 15:23:22 +01002223 obj_req->ex.oe_len);
Ilya Dryomovafb97882018-02-06 19:26:35 +01002224 rbd_assert(obj_req->bvec_idx == obj_req->bvec_count);
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02002225 osd_req_op_extent_osd_data_bvec_pos(osd_req, which,
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002226 &obj_req->bvec_pos);
2227 break;
2228 default:
Arnd Bergmann16809372019-03-22 17:53:56 +01002229 BUG();
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002230 }
2231}
2232
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02002233static int rbd_osd_setup_stat(struct ceph_osd_request *osd_req, int which)
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002234{
2235 struct page **pages;
Ilya Dryomov710214e2016-09-15 17:53:32 +02002236
Alex Elderc5b5ef62013-02-11 12:33:24 -06002237 /*
2238 * The response data for a STAT call consists of:
2239 * le64 length;
2240 * struct {
2241 * le32 tv_sec;
2242 * le32 tv_nsec;
2243 * } mtime;
2244 */
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002245 pages = ceph_alloc_page_vector(1, GFP_NOIO);
2246 if (IS_ERR(pages))
2247 return PTR_ERR(pages);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002248
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02002249 osd_req_op_init(osd_req, which, CEPH_OSD_OP_STAT, 0);
2250 osd_req_op_raw_data_in_pages(osd_req, which, pages,
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002251 8 + sizeof(struct ceph_timespec),
2252 0, false, true);
Ilya Dryomov980917f2016-09-12 18:59:42 +02002253 return 0;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002254}
2255
Ilya Dryomovb5ae8cb2019-05-29 16:53:14 +02002256static int rbd_osd_setup_copyup(struct ceph_osd_request *osd_req, int which,
2257 u32 bytes)
Ilya Dryomov13488d52019-02-25 12:37:50 +01002258{
Ilya Dryomovb5ae8cb2019-05-29 16:53:14 +02002259 struct rbd_obj_request *obj_req = osd_req->r_priv;
2260 int ret;
2261
2262 ret = osd_req_op_cls_init(osd_req, which, "rbd", "copyup");
2263 if (ret)
2264 return ret;
2265
2266 osd_req_op_cls_request_data_bvecs(osd_req, which, obj_req->copyup_bvecs,
2267 obj_req->copyup_bvec_count, bytes);
2268 return 0;
Ilya Dryomov13488d52019-02-25 12:37:50 +01002269}
2270
Ilya Dryomovea9b7432019-05-31 15:11:26 +02002271static int rbd_obj_init_read(struct rbd_obj_request *obj_req)
Alex Elderb454e362013-04-19 15:34:50 -05002272{
Ilya Dryomovea9b7432019-05-31 15:11:26 +02002273 obj_req->read_state = RBD_OBJ_READ_START;
2274 return 0;
2275}
2276
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02002277static void __rbd_osd_setup_write_ops(struct ceph_osd_request *osd_req,
2278 int which)
Alex Elderb454e362013-04-19 15:34:50 -05002279{
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02002280 struct rbd_obj_request *obj_req = osd_req->r_priv;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002281 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
2282 u16 opcode;
Alex Elderb454e362013-04-19 15:34:50 -05002283
Ilya Dryomov8b5bec52019-06-19 15:45:27 +02002284 if (!use_object_map(rbd_dev) ||
2285 !(obj_req->flags & RBD_OBJ_FLAG_MAY_EXIST)) {
2286 osd_req_op_alloc_hint_init(osd_req, which++,
2287 rbd_dev->layout.object_size,
2288 rbd_dev->layout.object_size);
2289 }
Alex Elderb454e362013-04-19 15:34:50 -05002290
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002291 if (rbd_obj_is_entire(obj_req))
2292 opcode = CEPH_OSD_OP_WRITEFULL;
2293 else
2294 opcode = CEPH_OSD_OP_WRITE;
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002295
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02002296 osd_req_op_extent_init(osd_req, which, opcode,
Ilya Dryomov43df3d32018-02-02 15:23:22 +01002297 obj_req->ex.oe_off, obj_req->ex.oe_len, 0, 0);
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02002298 rbd_osd_setup_data(osd_req, which);
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002299}
2300
Ilya Dryomovea9b7432019-05-31 15:11:26 +02002301static int rbd_obj_init_write(struct rbd_obj_request *obj_req)
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002302{
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002303 int ret;
Ilya Dryomov058aa992016-09-12 14:44:45 +02002304
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002305 /* reverse map the entire object onto the parent */
2306 ret = rbd_obj_calc_img_extents(obj_req, true);
2307 if (ret)
2308 return ret;
2309
Ilya Dryomov0ad5d952019-05-14 20:45:38 +02002310 if (rbd_obj_copyup_enabled(obj_req))
2311 obj_req->flags |= RBD_OBJ_FLAG_COPYUP_ENABLED;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002312
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02002313 obj_req->write_state = RBD_OBJ_WRITE_START;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002314 return 0;
2315}
2316
Ilya Dryomov6484cbe2019-01-29 12:46:25 +01002317static u16 truncate_or_zero_opcode(struct rbd_obj_request *obj_req)
2318{
2319 return rbd_obj_is_tail(obj_req) ? CEPH_OSD_OP_TRUNCATE :
2320 CEPH_OSD_OP_ZERO;
2321}
2322
Ilya Dryomov27bbd912019-05-29 17:31:37 +02002323static void __rbd_osd_setup_discard_ops(struct ceph_osd_request *osd_req,
2324 int which)
2325{
2326 struct rbd_obj_request *obj_req = osd_req->r_priv;
2327
2328 if (rbd_obj_is_entire(obj_req) && !obj_req->num_img_extents) {
2329 rbd_assert(obj_req->flags & RBD_OBJ_FLAG_DELETION);
2330 osd_req_op_init(osd_req, which, CEPH_OSD_OP_DELETE, 0);
2331 } else {
2332 osd_req_op_extent_init(osd_req, which,
2333 truncate_or_zero_opcode(obj_req),
2334 obj_req->ex.oe_off, obj_req->ex.oe_len,
2335 0, 0);
2336 }
2337}
2338
Ilya Dryomovea9b7432019-05-31 15:11:26 +02002339static int rbd_obj_init_discard(struct rbd_obj_request *obj_req)
Ilya Dryomov6484cbe2019-01-29 12:46:25 +01002340{
Ilya Dryomov0c93e1b2019-01-30 15:14:48 +01002341 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
Ilya Dryomov27bbd912019-05-29 17:31:37 +02002342 u64 off, next_off;
Ilya Dryomov6484cbe2019-01-29 12:46:25 +01002343 int ret;
2344
Ilya Dryomov0c93e1b2019-01-30 15:14:48 +01002345 /*
2346 * Align the range to alloc_size boundary and punt on discards
2347 * that are too small to free up any space.
2348 *
2349 * alloc_size == object_size && is_tail() is a special case for
2350 * filestore with filestore_punch_hole = false, needed to allow
2351 * truncate (in addition to delete).
2352 */
2353 if (rbd_dev->opts->alloc_size != rbd_dev->layout.object_size ||
2354 !rbd_obj_is_tail(obj_req)) {
Ilya Dryomov27bbd912019-05-29 17:31:37 +02002355 off = round_up(obj_req->ex.oe_off, rbd_dev->opts->alloc_size);
2356 next_off = round_down(obj_req->ex.oe_off + obj_req->ex.oe_len,
2357 rbd_dev->opts->alloc_size);
Ilya Dryomov0c93e1b2019-01-30 15:14:48 +01002358 if (off >= next_off)
2359 return 1;
Ilya Dryomov27bbd912019-05-29 17:31:37 +02002360
2361 dout("%s %p %llu~%llu -> %llu~%llu\n", __func__,
2362 obj_req, obj_req->ex.oe_off, obj_req->ex.oe_len,
2363 off, next_off - off);
2364 obj_req->ex.oe_off = off;
2365 obj_req->ex.oe_len = next_off - off;
Ilya Dryomov0c93e1b2019-01-30 15:14:48 +01002366 }
2367
Ilya Dryomov6484cbe2019-01-29 12:46:25 +01002368 /* reverse map the entire object onto the parent */
2369 ret = rbd_obj_calc_img_extents(obj_req, true);
2370 if (ret)
2371 return ret;
2372
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02002373 obj_req->flags |= RBD_OBJ_FLAG_NOOP_FOR_NONEXISTENT;
Ilya Dryomov0ad5d952019-05-14 20:45:38 +02002374 if (rbd_obj_is_entire(obj_req) && !obj_req->num_img_extents)
2375 obj_req->flags |= RBD_OBJ_FLAG_DELETION;
Ilya Dryomov6484cbe2019-01-29 12:46:25 +01002376
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02002377 obj_req->write_state = RBD_OBJ_WRITE_START;
Ilya Dryomov6484cbe2019-01-29 12:46:25 +01002378 return 0;
2379}
2380
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02002381static void __rbd_osd_setup_zeroout_ops(struct ceph_osd_request *osd_req,
2382 int which)
Ilya Dryomov13488d52019-02-25 12:37:50 +01002383{
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02002384 struct rbd_obj_request *obj_req = osd_req->r_priv;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002385 u16 opcode;
2386
2387 if (rbd_obj_is_entire(obj_req)) {
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002388 if (obj_req->num_img_extents) {
Ilya Dryomov0ad5d952019-05-14 20:45:38 +02002389 if (!(obj_req->flags & RBD_OBJ_FLAG_COPYUP_ENABLED))
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02002390 osd_req_op_init(osd_req, which++,
Ilya Dryomov9b17eb22019-02-28 15:51:39 +01002391 CEPH_OSD_OP_CREATE, 0);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002392 opcode = CEPH_OSD_OP_TRUNCATE;
2393 } else {
Ilya Dryomov0ad5d952019-05-14 20:45:38 +02002394 rbd_assert(obj_req->flags & RBD_OBJ_FLAG_DELETION);
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02002395 osd_req_op_init(osd_req, which++,
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002396 CEPH_OSD_OP_DELETE, 0);
2397 opcode = 0;
2398 }
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002399 } else {
Ilya Dryomov6484cbe2019-01-29 12:46:25 +01002400 opcode = truncate_or_zero_opcode(obj_req);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002401 }
2402
2403 if (opcode)
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02002404 osd_req_op_extent_init(osd_req, which, opcode,
Ilya Dryomov43df3d32018-02-02 15:23:22 +01002405 obj_req->ex.oe_off, obj_req->ex.oe_len,
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002406 0, 0);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002407}
2408
Ilya Dryomovea9b7432019-05-31 15:11:26 +02002409static int rbd_obj_init_zeroout(struct rbd_obj_request *obj_req)
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002410{
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002411 int ret;
2412
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002413 /* reverse map the entire object onto the parent */
2414 ret = rbd_obj_calc_img_extents(obj_req, true);
2415 if (ret)
2416 return ret;
2417
Ilya Dryomov0ad5d952019-05-14 20:45:38 +02002418 if (rbd_obj_copyup_enabled(obj_req))
2419 obj_req->flags |= RBD_OBJ_FLAG_COPYUP_ENABLED;
2420 if (!obj_req->num_img_extents) {
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02002421 obj_req->flags |= RBD_OBJ_FLAG_NOOP_FOR_NONEXISTENT;
Ilya Dryomov0ad5d952019-05-14 20:45:38 +02002422 if (rbd_obj_is_entire(obj_req))
2423 obj_req->flags |= RBD_OBJ_FLAG_DELETION;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002424 }
2425
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02002426 obj_req->write_state = RBD_OBJ_WRITE_START;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002427 return 0;
2428}
2429
Ilya Dryomova086a1b2019-06-12 18:33:31 +02002430static int count_write_ops(struct rbd_obj_request *obj_req)
2431{
Ilya Dryomov8b5bec52019-06-19 15:45:27 +02002432 struct rbd_img_request *img_req = obj_req->img_request;
2433
2434 switch (img_req->op_type) {
Ilya Dryomova086a1b2019-06-12 18:33:31 +02002435 case OBJ_OP_WRITE:
Ilya Dryomov8b5bec52019-06-19 15:45:27 +02002436 if (!use_object_map(img_req->rbd_dev) ||
2437 !(obj_req->flags & RBD_OBJ_FLAG_MAY_EXIST))
2438 return 2; /* setallochint + write/writefull */
2439
2440 return 1; /* write/writefull */
Ilya Dryomova086a1b2019-06-12 18:33:31 +02002441 case OBJ_OP_DISCARD:
2442 return 1; /* delete/truncate/zero */
2443 case OBJ_OP_ZEROOUT:
2444 if (rbd_obj_is_entire(obj_req) && obj_req->num_img_extents &&
2445 !(obj_req->flags & RBD_OBJ_FLAG_COPYUP_ENABLED))
2446 return 2; /* create + truncate */
2447
2448 return 1; /* delete/truncate/zero */
2449 default:
2450 BUG();
2451 }
2452}
2453
2454static void rbd_osd_setup_write_ops(struct ceph_osd_request *osd_req,
2455 int which)
2456{
2457 struct rbd_obj_request *obj_req = osd_req->r_priv;
2458
2459 switch (obj_req->img_request->op_type) {
2460 case OBJ_OP_WRITE:
2461 __rbd_osd_setup_write_ops(osd_req, which);
2462 break;
2463 case OBJ_OP_DISCARD:
2464 __rbd_osd_setup_discard_ops(osd_req, which);
2465 break;
2466 case OBJ_OP_ZEROOUT:
2467 __rbd_osd_setup_zeroout_ops(osd_req, which);
2468 break;
2469 default:
2470 BUG();
2471 }
2472}
2473
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002474/*
Ilya Dryomova086a1b2019-06-12 18:33:31 +02002475 * Prune the list of object requests (adjust offset and/or length, drop
2476 * redundant requests). Prepare object request state machines and image
2477 * request state machine for execution.
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002478 */
2479static int __rbd_img_fill_request(struct rbd_img_request *img_req)
2480{
Ilya Dryomov0c93e1b2019-01-30 15:14:48 +01002481 struct rbd_obj_request *obj_req, *next_obj_req;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002482 int ret;
2483
Ilya Dryomov0c93e1b2019-01-30 15:14:48 +01002484 for_each_obj_request_safe(img_req, obj_req, next_obj_req) {
Ilya Dryomov9bb02482018-01-30 17:52:10 +01002485 switch (img_req->op_type) {
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002486 case OBJ_OP_READ:
Ilya Dryomovea9b7432019-05-31 15:11:26 +02002487 ret = rbd_obj_init_read(obj_req);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002488 break;
2489 case OBJ_OP_WRITE:
Ilya Dryomovea9b7432019-05-31 15:11:26 +02002490 ret = rbd_obj_init_write(obj_req);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002491 break;
2492 case OBJ_OP_DISCARD:
Ilya Dryomovea9b7432019-05-31 15:11:26 +02002493 ret = rbd_obj_init_discard(obj_req);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002494 break;
Ilya Dryomov6484cbe2019-01-29 12:46:25 +01002495 case OBJ_OP_ZEROOUT:
Ilya Dryomovea9b7432019-05-31 15:11:26 +02002496 ret = rbd_obj_init_zeroout(obj_req);
Ilya Dryomov6484cbe2019-01-29 12:46:25 +01002497 break;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002498 default:
Arnd Bergmann16809372019-03-22 17:53:56 +01002499 BUG();
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002500 }
Ilya Dryomov0c93e1b2019-01-30 15:14:48 +01002501 if (ret < 0)
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002502 return ret;
Ilya Dryomov0c93e1b2019-01-30 15:14:48 +01002503 if (ret > 0) {
Ilya Dryomov0c93e1b2019-01-30 15:14:48 +01002504 rbd_img_obj_request_del(img_req, obj_req);
2505 continue;
2506 }
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002507 }
2508
Ilya Dryomov0192ce22019-05-16 15:06:56 +02002509 img_req->state = RBD_IMG_START;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002510 return 0;
2511}
2512
Ilya Dryomov5a237812018-02-06 19:26:34 +01002513union rbd_img_fill_iter {
2514 struct ceph_bio_iter bio_iter;
2515 struct ceph_bvec_iter bvec_iter;
2516};
2517
2518struct rbd_img_fill_ctx {
2519 enum obj_request_type pos_type;
2520 union rbd_img_fill_iter *pos;
2521 union rbd_img_fill_iter iter;
2522 ceph_object_extent_fn_t set_pos_fn;
Ilya Dryomovafb97882018-02-06 19:26:35 +01002523 ceph_object_extent_fn_t count_fn;
2524 ceph_object_extent_fn_t copy_fn;
Ilya Dryomov5a237812018-02-06 19:26:34 +01002525};
2526
2527static struct ceph_object_extent *alloc_object_extent(void *arg)
2528{
2529 struct rbd_img_request *img_req = arg;
2530 struct rbd_obj_request *obj_req;
2531
2532 obj_req = rbd_obj_request_create();
2533 if (!obj_req)
2534 return NULL;
2535
2536 rbd_img_obj_request_add(img_req, obj_req);
2537 return &obj_req->ex;
2538}
2539
2540/*
Ilya Dryomovafb97882018-02-06 19:26:35 +01002541 * While su != os && sc == 1 is technically not fancy (it's the same
2542 * layout as su == os && sc == 1), we can't use the nocopy path for it
2543 * because ->set_pos_fn() should be called only once per object.
2544 * ceph_file_to_extents() invokes action_fn once per stripe unit, so
2545 * treat su != os && sc == 1 as fancy.
Ilya Dryomov5a237812018-02-06 19:26:34 +01002546 */
Ilya Dryomovafb97882018-02-06 19:26:35 +01002547static bool rbd_layout_is_fancy(struct ceph_file_layout *l)
2548{
2549 return l->stripe_unit != l->object_size;
2550}
2551
2552static int rbd_img_fill_request_nocopy(struct rbd_img_request *img_req,
2553 struct ceph_file_extent *img_extents,
2554 u32 num_img_extents,
2555 struct rbd_img_fill_ctx *fctx)
Ilya Dryomov5a237812018-02-06 19:26:34 +01002556{
2557 u32 i;
2558 int ret;
2559
2560 img_req->data_type = fctx->pos_type;
2561
2562 /*
2563 * Create object requests and set each object request's starting
2564 * position in the provided bio (list) or bio_vec array.
2565 */
2566 fctx->iter = *fctx->pos;
2567 for (i = 0; i < num_img_extents; i++) {
2568 ret = ceph_file_to_extents(&img_req->rbd_dev->layout,
2569 img_extents[i].fe_off,
2570 img_extents[i].fe_len,
2571 &img_req->object_extents,
2572 alloc_object_extent, img_req,
2573 fctx->set_pos_fn, &fctx->iter);
2574 if (ret)
2575 return ret;
2576 }
2577
2578 return __rbd_img_fill_request(img_req);
2579}
2580
Ilya Dryomovafb97882018-02-06 19:26:35 +01002581/*
2582 * Map a list of image extents to a list of object extents, create the
2583 * corresponding object requests (normally each to a different object,
2584 * but not always) and add them to @img_req. For each object request,
2585 * set up its data descriptor to point to the corresponding chunk(s) of
2586 * @fctx->pos data buffer.
2587 *
2588 * Because ceph_file_to_extents() will merge adjacent object extents
2589 * together, each object request's data descriptor may point to multiple
2590 * different chunks of @fctx->pos data buffer.
2591 *
2592 * @fctx->pos data buffer is assumed to be large enough.
2593 */
2594static int rbd_img_fill_request(struct rbd_img_request *img_req,
2595 struct ceph_file_extent *img_extents,
2596 u32 num_img_extents,
2597 struct rbd_img_fill_ctx *fctx)
2598{
2599 struct rbd_device *rbd_dev = img_req->rbd_dev;
2600 struct rbd_obj_request *obj_req;
2601 u32 i;
2602 int ret;
2603
2604 if (fctx->pos_type == OBJ_REQUEST_NODATA ||
2605 !rbd_layout_is_fancy(&rbd_dev->layout))
2606 return rbd_img_fill_request_nocopy(img_req, img_extents,
2607 num_img_extents, fctx);
2608
2609 img_req->data_type = OBJ_REQUEST_OWN_BVECS;
2610
2611 /*
2612 * Create object requests and determine ->bvec_count for each object
2613 * request. Note that ->bvec_count sum over all object requests may
2614 * be greater than the number of bio_vecs in the provided bio (list)
2615 * or bio_vec array because when mapped, those bio_vecs can straddle
2616 * stripe unit boundaries.
2617 */
2618 fctx->iter = *fctx->pos;
2619 for (i = 0; i < num_img_extents; i++) {
2620 ret = ceph_file_to_extents(&rbd_dev->layout,
2621 img_extents[i].fe_off,
2622 img_extents[i].fe_len,
2623 &img_req->object_extents,
2624 alloc_object_extent, img_req,
2625 fctx->count_fn, &fctx->iter);
2626 if (ret)
2627 return ret;
2628 }
2629
2630 for_each_obj_request(img_req, obj_req) {
2631 obj_req->bvec_pos.bvecs = kmalloc_array(obj_req->bvec_count,
2632 sizeof(*obj_req->bvec_pos.bvecs),
2633 GFP_NOIO);
2634 if (!obj_req->bvec_pos.bvecs)
2635 return -ENOMEM;
Alex Elderb454e362013-04-19 15:34:50 -05002636 }
2637
2638 /*
Ilya Dryomovafb97882018-02-06 19:26:35 +01002639 * Fill in each object request's private bio_vec array, splitting and
2640 * rearranging the provided bio_vecs in stripe unit chunks as needed.
Alex Elderb454e362013-04-19 15:34:50 -05002641 */
Ilya Dryomovafb97882018-02-06 19:26:35 +01002642 fctx->iter = *fctx->pos;
2643 for (i = 0; i < num_img_extents; i++) {
2644 ret = ceph_iterate_extents(&rbd_dev->layout,
2645 img_extents[i].fe_off,
2646 img_extents[i].fe_len,
2647 &img_req->object_extents,
2648 fctx->copy_fn, &fctx->iter);
2649 if (ret)
2650 return ret;
2651 }
Alex Elder3d7efd12013-04-19 15:34:50 -05002652
Ilya Dryomovafb97882018-02-06 19:26:35 +01002653 return __rbd_img_fill_request(img_req);
Alex Elderb454e362013-04-19 15:34:50 -05002654}
2655
Ilya Dryomov5a237812018-02-06 19:26:34 +01002656static int rbd_img_fill_nodata(struct rbd_img_request *img_req,
2657 u64 off, u64 len)
2658{
2659 struct ceph_file_extent ex = { off, len };
Arnd Bergmanna55e6012020-01-07 22:01:04 +01002660 union rbd_img_fill_iter dummy = {};
Ilya Dryomov5a237812018-02-06 19:26:34 +01002661 struct rbd_img_fill_ctx fctx = {
2662 .pos_type = OBJ_REQUEST_NODATA,
2663 .pos = &dummy,
2664 };
2665
2666 return rbd_img_fill_request(img_req, &ex, 1, &fctx);
2667}
2668
2669static void set_bio_pos(struct ceph_object_extent *ex, u32 bytes, void *arg)
2670{
2671 struct rbd_obj_request *obj_req =
2672 container_of(ex, struct rbd_obj_request, ex);
2673 struct ceph_bio_iter *it = arg;
2674
2675 dout("%s objno %llu bytes %u\n", __func__, ex->oe_objno, bytes);
2676 obj_req->bio_pos = *it;
2677 ceph_bio_iter_advance(it, bytes);
2678}
2679
Ilya Dryomovafb97882018-02-06 19:26:35 +01002680static void count_bio_bvecs(struct ceph_object_extent *ex, u32 bytes, void *arg)
2681{
2682 struct rbd_obj_request *obj_req =
2683 container_of(ex, struct rbd_obj_request, ex);
2684 struct ceph_bio_iter *it = arg;
2685
2686 dout("%s objno %llu bytes %u\n", __func__, ex->oe_objno, bytes);
2687 ceph_bio_iter_advance_step(it, bytes, ({
2688 obj_req->bvec_count++;
2689 }));
2690
2691}
2692
2693static void copy_bio_bvecs(struct ceph_object_extent *ex, u32 bytes, void *arg)
2694{
2695 struct rbd_obj_request *obj_req =
2696 container_of(ex, struct rbd_obj_request, ex);
2697 struct ceph_bio_iter *it = arg;
2698
2699 dout("%s objno %llu bytes %u\n", __func__, ex->oe_objno, bytes);
2700 ceph_bio_iter_advance_step(it, bytes, ({
2701 obj_req->bvec_pos.bvecs[obj_req->bvec_idx++] = bv;
2702 obj_req->bvec_pos.iter.bi_size += bv.bv_len;
2703 }));
2704}
2705
Ilya Dryomov5a237812018-02-06 19:26:34 +01002706static int __rbd_img_fill_from_bio(struct rbd_img_request *img_req,
2707 struct ceph_file_extent *img_extents,
2708 u32 num_img_extents,
2709 struct ceph_bio_iter *bio_pos)
2710{
2711 struct rbd_img_fill_ctx fctx = {
2712 .pos_type = OBJ_REQUEST_BIO,
2713 .pos = (union rbd_img_fill_iter *)bio_pos,
2714 .set_pos_fn = set_bio_pos,
Ilya Dryomovafb97882018-02-06 19:26:35 +01002715 .count_fn = count_bio_bvecs,
2716 .copy_fn = copy_bio_bvecs,
Ilya Dryomov5a237812018-02-06 19:26:34 +01002717 };
2718
2719 return rbd_img_fill_request(img_req, img_extents, num_img_extents,
2720 &fctx);
2721}
2722
2723static int rbd_img_fill_from_bio(struct rbd_img_request *img_req,
2724 u64 off, u64 len, struct bio *bio)
2725{
2726 struct ceph_file_extent ex = { off, len };
2727 struct ceph_bio_iter it = { .bio = bio, .iter = bio->bi_iter };
2728
2729 return __rbd_img_fill_from_bio(img_req, &ex, 1, &it);
2730}
2731
2732static void set_bvec_pos(struct ceph_object_extent *ex, u32 bytes, void *arg)
2733{
2734 struct rbd_obj_request *obj_req =
2735 container_of(ex, struct rbd_obj_request, ex);
2736 struct ceph_bvec_iter *it = arg;
2737
2738 obj_req->bvec_pos = *it;
2739 ceph_bvec_iter_shorten(&obj_req->bvec_pos, bytes);
2740 ceph_bvec_iter_advance(it, bytes);
2741}
2742
Ilya Dryomovafb97882018-02-06 19:26:35 +01002743static void count_bvecs(struct ceph_object_extent *ex, u32 bytes, void *arg)
2744{
2745 struct rbd_obj_request *obj_req =
2746 container_of(ex, struct rbd_obj_request, ex);
2747 struct ceph_bvec_iter *it = arg;
2748
2749 ceph_bvec_iter_advance_step(it, bytes, ({
2750 obj_req->bvec_count++;
2751 }));
2752}
2753
2754static void copy_bvecs(struct ceph_object_extent *ex, u32 bytes, void *arg)
2755{
2756 struct rbd_obj_request *obj_req =
2757 container_of(ex, struct rbd_obj_request, ex);
2758 struct ceph_bvec_iter *it = arg;
2759
2760 ceph_bvec_iter_advance_step(it, bytes, ({
2761 obj_req->bvec_pos.bvecs[obj_req->bvec_idx++] = bv;
2762 obj_req->bvec_pos.iter.bi_size += bv.bv_len;
2763 }));
2764}
2765
Ilya Dryomov5a237812018-02-06 19:26:34 +01002766static int __rbd_img_fill_from_bvecs(struct rbd_img_request *img_req,
2767 struct ceph_file_extent *img_extents,
2768 u32 num_img_extents,
2769 struct ceph_bvec_iter *bvec_pos)
2770{
2771 struct rbd_img_fill_ctx fctx = {
2772 .pos_type = OBJ_REQUEST_BVECS,
2773 .pos = (union rbd_img_fill_iter *)bvec_pos,
2774 .set_pos_fn = set_bvec_pos,
Ilya Dryomovafb97882018-02-06 19:26:35 +01002775 .count_fn = count_bvecs,
2776 .copy_fn = copy_bvecs,
Ilya Dryomov5a237812018-02-06 19:26:34 +01002777 };
2778
2779 return rbd_img_fill_request(img_req, img_extents, num_img_extents,
2780 &fctx);
2781}
2782
2783static int rbd_img_fill_from_bvecs(struct rbd_img_request *img_req,
2784 struct ceph_file_extent *img_extents,
2785 u32 num_img_extents,
2786 struct bio_vec *bvecs)
2787{
2788 struct ceph_bvec_iter it = {
2789 .bvecs = bvecs,
2790 .iter = { .bi_size = ceph_file_extents_bytes(img_extents,
2791 num_img_extents) },
2792 };
2793
2794 return __rbd_img_fill_from_bvecs(img_req, img_extents, num_img_extents,
2795 &it);
2796}
2797
Ilya Dryomov0192ce22019-05-16 15:06:56 +02002798static void rbd_img_handle_request_work(struct work_struct *work)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002799{
Ilya Dryomov0192ce22019-05-16 15:06:56 +02002800 struct rbd_img_request *img_req =
2801 container_of(work, struct rbd_img_request, work);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002802
Ilya Dryomov0192ce22019-05-16 15:06:56 +02002803 rbd_img_handle_request(img_req, img_req->work_result);
2804}
Alex Elderbf0d5f502012-11-22 00:00:08 -06002805
Ilya Dryomov0192ce22019-05-16 15:06:56 +02002806static void rbd_img_schedule(struct rbd_img_request *img_req, int result)
2807{
2808 INIT_WORK(&img_req->work, rbd_img_handle_request_work);
2809 img_req->work_result = result;
2810 queue_work(rbd_wq, &img_req->work);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002811}
2812
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02002813static bool rbd_obj_may_exist(struct rbd_obj_request *obj_req)
2814{
2815 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
2816
2817 if (rbd_object_map_may_exist(rbd_dev, obj_req->ex.oe_objno)) {
2818 obj_req->flags |= RBD_OBJ_FLAG_MAY_EXIST;
2819 return true;
2820 }
2821
2822 dout("%s %p objno %llu assuming dne\n", __func__, obj_req,
2823 obj_req->ex.oe_objno);
2824 return false;
2825}
2826
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02002827static int rbd_obj_read_object(struct rbd_obj_request *obj_req)
2828{
Ilya Dryomova086a1b2019-06-12 18:33:31 +02002829 struct ceph_osd_request *osd_req;
2830 int ret;
2831
2832 osd_req = __rbd_obj_add_osd_request(obj_req, NULL, 1);
2833 if (IS_ERR(osd_req))
2834 return PTR_ERR(osd_req);
2835
2836 osd_req_op_extent_init(osd_req, 0, CEPH_OSD_OP_READ,
2837 obj_req->ex.oe_off, obj_req->ex.oe_len, 0, 0);
2838 rbd_osd_setup_data(osd_req, 0);
2839 rbd_osd_format_read(osd_req);
2840
2841 ret = ceph_osdc_alloc_messages(osd_req, GFP_NOIO);
2842 if (ret)
2843 return ret;
2844
2845 rbd_osd_submit(osd_req);
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02002846 return 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002847}
2848
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002849static int rbd_obj_read_from_parent(struct rbd_obj_request *obj_req)
Alex Elder8b3e1a52013-01-24 16:13:36 -06002850{
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002851 struct rbd_img_request *img_req = obj_req->img_request;
2852 struct rbd_img_request *child_img_req;
2853 int ret;
Alex Elder8b3e1a52013-01-24 16:13:36 -06002854
Ilya Dryomove93aca02018-02-06 19:26:35 +01002855 child_img_req = rbd_img_request_create(img_req->rbd_dev->parent,
2856 OBJ_OP_READ, NULL);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002857 if (!child_img_req)
2858 return -ENOMEM;
Alex Elder8b3e1a52013-01-24 16:13:36 -06002859
Ilya Dryomove93aca02018-02-06 19:26:35 +01002860 __set_bit(IMG_REQ_CHILD, &child_img_req->flags);
2861 child_img_req->obj_request = obj_req;
Alex Elder02c74fb2013-05-06 17:40:33 -05002862
Ilya Dryomov21ed05a2019-08-30 17:31:06 +02002863 dout("%s child_img_req %p for obj_req %p\n", __func__, child_img_req,
2864 obj_req);
2865
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002866 if (!rbd_img_is_write(img_req)) {
Ilya Dryomovecc633c2018-02-01 11:50:47 +01002867 switch (img_req->data_type) {
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002868 case OBJ_REQUEST_BIO:
Ilya Dryomov5a237812018-02-06 19:26:34 +01002869 ret = __rbd_img_fill_from_bio(child_img_req,
2870 obj_req->img_extents,
2871 obj_req->num_img_extents,
2872 &obj_req->bio_pos);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002873 break;
2874 case OBJ_REQUEST_BVECS:
Ilya Dryomovafb97882018-02-06 19:26:35 +01002875 case OBJ_REQUEST_OWN_BVECS:
Ilya Dryomov5a237812018-02-06 19:26:34 +01002876 ret = __rbd_img_fill_from_bvecs(child_img_req,
2877 obj_req->img_extents,
2878 obj_req->num_img_extents,
2879 &obj_req->bvec_pos);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002880 break;
2881 default:
Arnd Bergmannd342a152019-03-22 15:36:37 +01002882 BUG();
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002883 }
2884 } else {
Ilya Dryomov5a237812018-02-06 19:26:34 +01002885 ret = rbd_img_fill_from_bvecs(child_img_req,
2886 obj_req->img_extents,
2887 obj_req->num_img_extents,
2888 obj_req->copyup_bvecs);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002889 }
2890 if (ret) {
2891 rbd_img_request_put(child_img_req);
2892 return ret;
2893 }
2894
Ilya Dryomov0192ce22019-05-16 15:06:56 +02002895 /* avoid parent chain recursion */
2896 rbd_img_schedule(child_img_req, 0);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002897 return 0;
2898}
2899
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02002900static bool rbd_obj_advance_read(struct rbd_obj_request *obj_req, int *result)
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002901{
2902 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
2903 int ret;
2904
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02002905again:
Ilya Dryomova9b67e62019-05-08 13:35:57 +02002906 switch (obj_req->read_state) {
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02002907 case RBD_OBJ_READ_START:
2908 rbd_assert(!*result);
2909
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02002910 if (!rbd_obj_may_exist(obj_req)) {
2911 *result = -ENOENT;
2912 obj_req->read_state = RBD_OBJ_READ_OBJECT;
2913 goto again;
2914 }
2915
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02002916 ret = rbd_obj_read_object(obj_req);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002917 if (ret) {
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02002918 *result = ret;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002919 return true;
2920 }
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02002921 obj_req->read_state = RBD_OBJ_READ_OBJECT;
2922 return false;
Ilya Dryomova9b67e62019-05-08 13:35:57 +02002923 case RBD_OBJ_READ_OBJECT:
2924 if (*result == -ENOENT && rbd_dev->parent_overlap) {
2925 /* reverse map this object extent onto the parent */
2926 ret = rbd_obj_calc_img_extents(obj_req, false);
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002927 if (ret) {
Ilya Dryomov54ab3b22019-05-11 16:21:49 +02002928 *result = ret;
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002929 return true;
2930 }
Ilya Dryomova9b67e62019-05-08 13:35:57 +02002931 if (obj_req->num_img_extents) {
2932 ret = rbd_obj_read_from_parent(obj_req);
2933 if (ret) {
2934 *result = ret;
2935 return true;
2936 }
2937 obj_req->read_state = RBD_OBJ_READ_PARENT;
2938 return false;
2939 }
Ilya Dryomov86bd7992018-02-06 19:26:33 +01002940 }
Alex Elder02c74fb2013-05-06 17:40:33 -05002941
Ilya Dryomova9b67e62019-05-08 13:35:57 +02002942 /*
2943 * -ENOENT means a hole in the image -- zero-fill the entire
2944 * length of the request. A short read also implies zero-fill
2945 * to the end of the request.
2946 */
2947 if (*result == -ENOENT) {
2948 rbd_obj_zero_range(obj_req, 0, obj_req->ex.oe_len);
2949 *result = 0;
2950 } else if (*result >= 0) {
2951 if (*result < obj_req->ex.oe_len)
2952 rbd_obj_zero_range(obj_req, *result,
2953 obj_req->ex.oe_len - *result);
2954 else
2955 rbd_assert(*result == obj_req->ex.oe_len);
2956 *result = 0;
2957 }
2958 return true;
2959 case RBD_OBJ_READ_PARENT:
Ilya Dryomovd435c9a2019-08-27 16:45:10 +02002960 /*
2961 * The parent image is read only up to the overlap -- zero-fill
2962 * from the overlap to the end of the request.
2963 */
2964 if (!*result) {
2965 u32 obj_overlap = rbd_obj_img_extents_bytes(obj_req);
2966
2967 if (obj_overlap < obj_req->ex.oe_len)
2968 rbd_obj_zero_range(obj_req, obj_overlap,
2969 obj_req->ex.oe_len - obj_overlap);
2970 }
Ilya Dryomova9b67e62019-05-08 13:35:57 +02002971 return true;
2972 default:
2973 BUG();
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002974 }
Ilya Dryomov3da691b2018-01-29 14:04:08 +01002975}
2976
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02002977static bool rbd_obj_write_is_noop(struct rbd_obj_request *obj_req)
2978{
2979 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
2980
2981 if (rbd_object_map_may_exist(rbd_dev, obj_req->ex.oe_objno))
2982 obj_req->flags |= RBD_OBJ_FLAG_MAY_EXIST;
2983
2984 if (!(obj_req->flags & RBD_OBJ_FLAG_MAY_EXIST) &&
2985 (obj_req->flags & RBD_OBJ_FLAG_NOOP_FOR_NONEXISTENT)) {
2986 dout("%s %p noop for nonexistent\n", __func__, obj_req);
2987 return true;
Alex Elder02c74fb2013-05-06 17:40:33 -05002988 }
2989
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02002990 return false;
2991}
2992
2993/*
2994 * Return:
2995 * 0 - object map update sent
2996 * 1 - object map update isn't needed
2997 * <0 - error
2998 */
2999static int rbd_obj_write_pre_object_map(struct rbd_obj_request *obj_req)
3000{
3001 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
3002 u8 new_state;
3003
3004 if (!(rbd_dev->header.features & RBD_FEATURE_OBJECT_MAP))
3005 return 1;
3006
3007 if (obj_req->flags & RBD_OBJ_FLAG_DELETION)
3008 new_state = OBJECT_PENDING;
3009 else
3010 new_state = OBJECT_EXISTS;
3011
3012 return rbd_object_map_update(obj_req, CEPH_NOSNAP, new_state, NULL);
3013}
3014
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02003015static int rbd_obj_write_object(struct rbd_obj_request *obj_req)
3016{
Ilya Dryomova086a1b2019-06-12 18:33:31 +02003017 struct ceph_osd_request *osd_req;
3018 int num_ops = count_write_ops(obj_req);
3019 int which = 0;
3020 int ret;
3021
3022 if (obj_req->flags & RBD_OBJ_FLAG_COPYUP_ENABLED)
3023 num_ops++; /* stat */
3024
3025 osd_req = rbd_obj_add_osd_request(obj_req, num_ops);
3026 if (IS_ERR(osd_req))
3027 return PTR_ERR(osd_req);
3028
3029 if (obj_req->flags & RBD_OBJ_FLAG_COPYUP_ENABLED) {
3030 ret = rbd_osd_setup_stat(osd_req, which++);
3031 if (ret)
3032 return ret;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003033 }
3034
Ilya Dryomova086a1b2019-06-12 18:33:31 +02003035 rbd_osd_setup_write_ops(osd_req, which);
3036 rbd_osd_format_write(osd_req);
3037
3038 ret = ceph_osdc_alloc_messages(osd_req, GFP_NOIO);
3039 if (ret)
3040 return ret;
3041
3042 rbd_osd_submit(osd_req);
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02003043 return 0;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003044}
3045
3046/*
3047 * copyup_bvecs pages are never highmem pages
3048 */
3049static bool is_zero_bvecs(struct bio_vec *bvecs, u32 bytes)
3050{
3051 struct ceph_bvec_iter it = {
3052 .bvecs = bvecs,
3053 .iter = { .bi_size = bytes },
3054 };
3055
3056 ceph_bvec_iter_advance_step(&it, bytes, ({
3057 if (memchr_inv(page_address(bv.bv_page) + bv.bv_offset, 0,
3058 bv.bv_len))
3059 return false;
3060 }));
3061 return true;
3062}
3063
Ilya Dryomov3a482502019-02-28 10:49:12 +01003064#define MODS_ONLY U32_MAX
3065
Ilya Dryomov793333a302019-06-13 17:44:08 +02003066static int rbd_obj_copyup_empty_snapc(struct rbd_obj_request *obj_req,
3067 u32 bytes)
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003068{
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02003069 struct ceph_osd_request *osd_req;
Chengguang Xufe943d52018-04-12 12:04:55 +08003070 int ret;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003071
3072 dout("%s obj_req %p bytes %u\n", __func__, obj_req, bytes);
Ilya Dryomov89a59c12019-02-28 14:20:28 +01003073 rbd_assert(bytes > 0 && bytes != MODS_ONLY);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003074
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02003075 osd_req = __rbd_obj_add_osd_request(obj_req, &rbd_empty_snapc, 1);
3076 if (IS_ERR(osd_req))
3077 return PTR_ERR(osd_req);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003078
Ilya Dryomovb5ae8cb2019-05-29 16:53:14 +02003079 ret = rbd_osd_setup_copyup(osd_req, 0, bytes);
Chengguang Xufe943d52018-04-12 12:04:55 +08003080 if (ret)
3081 return ret;
3082
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02003083 rbd_osd_format_write(osd_req);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003084
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02003085 ret = ceph_osdc_alloc_messages(osd_req, GFP_NOIO);
Ilya Dryomov89a59c12019-02-28 14:20:28 +01003086 if (ret)
3087 return ret;
3088
Ilya Dryomova086a1b2019-06-12 18:33:31 +02003089 rbd_osd_submit(osd_req);
Ilya Dryomov89a59c12019-02-28 14:20:28 +01003090 return 0;
3091}
3092
Ilya Dryomov793333a302019-06-13 17:44:08 +02003093static int rbd_obj_copyup_current_snapc(struct rbd_obj_request *obj_req,
3094 u32 bytes)
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003095{
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02003096 struct ceph_osd_request *osd_req;
Ilya Dryomova086a1b2019-06-12 18:33:31 +02003097 int num_ops = count_write_ops(obj_req);
3098 int which = 0;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003099 int ret;
3100
3101 dout("%s obj_req %p bytes %u\n", __func__, obj_req, bytes);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003102
Ilya Dryomova086a1b2019-06-12 18:33:31 +02003103 if (bytes != MODS_ONLY)
3104 num_ops++; /* copyup */
Ilya Dryomov13488d52019-02-25 12:37:50 +01003105
Ilya Dryomova086a1b2019-06-12 18:33:31 +02003106 osd_req = rbd_obj_add_osd_request(obj_req, num_ops);
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02003107 if (IS_ERR(osd_req))
3108 return PTR_ERR(osd_req);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003109
Ilya Dryomov3a482502019-02-28 10:49:12 +01003110 if (bytes != MODS_ONLY) {
Ilya Dryomovb5ae8cb2019-05-29 16:53:14 +02003111 ret = rbd_osd_setup_copyup(osd_req, which++, bytes);
Ilya Dryomov3a482502019-02-28 10:49:12 +01003112 if (ret)
3113 return ret;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003114 }
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003115
Ilya Dryomova086a1b2019-06-12 18:33:31 +02003116 rbd_osd_setup_write_ops(osd_req, which);
3117 rbd_osd_format_write(osd_req);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003118
Ilya Dryomovbcbab1d2019-05-27 11:41:36 +02003119 ret = ceph_osdc_alloc_messages(osd_req, GFP_NOIO);
Ilya Dryomov26f887e2018-10-15 16:11:37 +02003120 if (ret)
3121 return ret;
3122
Ilya Dryomova086a1b2019-06-12 18:33:31 +02003123 rbd_osd_submit(osd_req);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003124 return 0;
3125}
3126
Ilya Dryomov7e07efb2018-01-20 10:30:11 +01003127static int setup_copyup_bvecs(struct rbd_obj_request *obj_req, u64 obj_overlap)
3128{
3129 u32 i;
3130
3131 rbd_assert(!obj_req->copyup_bvecs);
3132 obj_req->copyup_bvec_count = calc_pages_for(0, obj_overlap);
3133 obj_req->copyup_bvecs = kcalloc(obj_req->copyup_bvec_count,
3134 sizeof(*obj_req->copyup_bvecs),
3135 GFP_NOIO);
3136 if (!obj_req->copyup_bvecs)
3137 return -ENOMEM;
3138
3139 for (i = 0; i < obj_req->copyup_bvec_count; i++) {
3140 unsigned int len = min(obj_overlap, (u64)PAGE_SIZE);
3141
3142 obj_req->copyup_bvecs[i].bv_page = alloc_page(GFP_NOIO);
3143 if (!obj_req->copyup_bvecs[i].bv_page)
3144 return -ENOMEM;
3145
3146 obj_req->copyup_bvecs[i].bv_offset = 0;
3147 obj_req->copyup_bvecs[i].bv_len = len;
3148 obj_overlap -= len;
3149 }
3150
3151 rbd_assert(!obj_overlap);
3152 return 0;
3153}
3154
Ilya Dryomov0ad5d952019-05-14 20:45:38 +02003155/*
3156 * The target object doesn't exist. Read the data for the entire
3157 * target object up to the overlap point (if any) from the parent,
3158 * so we can use it for a copyup.
3159 */
Ilya Dryomov793333a302019-06-13 17:44:08 +02003160static int rbd_obj_copyup_read_parent(struct rbd_obj_request *obj_req)
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003161{
3162 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003163 int ret;
3164
Ilya Dryomov86bd7992018-02-06 19:26:33 +01003165 rbd_assert(obj_req->num_img_extents);
3166 prune_extents(obj_req->img_extents, &obj_req->num_img_extents,
3167 rbd_dev->parent_overlap);
3168 if (!obj_req->num_img_extents) {
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003169 /*
3170 * The overlap has become 0 (most likely because the
Ilya Dryomov3a482502019-02-28 10:49:12 +01003171 * image has been flattened). Re-submit the original write
3172 * request -- pass MODS_ONLY since the copyup isn't needed
3173 * anymore.
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003174 */
Ilya Dryomov793333a302019-06-13 17:44:08 +02003175 return rbd_obj_copyup_current_snapc(obj_req, MODS_ONLY);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003176 }
3177
Ilya Dryomov86bd7992018-02-06 19:26:33 +01003178 ret = setup_copyup_bvecs(obj_req, rbd_obj_img_extents_bytes(obj_req));
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003179 if (ret)
3180 return ret;
3181
Ilya Dryomov86bd7992018-02-06 19:26:33 +01003182 return rbd_obj_read_from_parent(obj_req);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003183}
3184
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02003185static void rbd_obj_copyup_object_maps(struct rbd_obj_request *obj_req)
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003186{
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02003187 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
3188 struct ceph_snap_context *snapc = obj_req->img_request->snapc;
3189 u8 new_state;
3190 u32 i;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003191 int ret;
3192
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02003193 rbd_assert(!obj_req->pending.result && !obj_req->pending.num_pending);
3194
3195 if (!(rbd_dev->header.features & RBD_FEATURE_OBJECT_MAP))
3196 return;
3197
3198 if (obj_req->flags & RBD_OBJ_FLAG_COPYUP_ZEROS)
3199 return;
3200
3201 for (i = 0; i < snapc->num_snaps; i++) {
3202 if ((rbd_dev->header.features & RBD_FEATURE_FAST_DIFF) &&
3203 i + 1 < snapc->num_snaps)
3204 new_state = OBJECT_EXISTS_CLEAN;
3205 else
3206 new_state = OBJECT_EXISTS;
3207
3208 ret = rbd_object_map_update(obj_req, snapc->snaps[i],
3209 new_state, NULL);
3210 if (ret < 0) {
3211 obj_req->pending.result = ret;
3212 return;
3213 }
3214
3215 rbd_assert(!ret);
3216 obj_req->pending.num_pending++;
3217 }
3218}
3219
Ilya Dryomov793333a302019-06-13 17:44:08 +02003220static void rbd_obj_copyup_write_object(struct rbd_obj_request *obj_req)
3221{
3222 u32 bytes = rbd_obj_img_extents_bytes(obj_req);
3223 int ret;
3224
3225 rbd_assert(!obj_req->pending.result && !obj_req->pending.num_pending);
3226
3227 /*
3228 * Only send non-zero copyup data to save some I/O and network
3229 * bandwidth -- zero copyup data is equivalent to the object not
3230 * existing.
3231 */
3232 if (obj_req->flags & RBD_OBJ_FLAG_COPYUP_ZEROS)
3233 bytes = 0;
3234
3235 if (obj_req->img_request->snapc->num_snaps && bytes > 0) {
3236 /*
3237 * Send a copyup request with an empty snapshot context to
3238 * deep-copyup the object through all existing snapshots.
3239 * A second request with the current snapshot context will be
3240 * sent for the actual modification.
3241 */
3242 ret = rbd_obj_copyup_empty_snapc(obj_req, bytes);
3243 if (ret) {
3244 obj_req->pending.result = ret;
3245 return;
3246 }
3247
3248 obj_req->pending.num_pending++;
3249 bytes = MODS_ONLY;
3250 }
3251
3252 ret = rbd_obj_copyup_current_snapc(obj_req, bytes);
3253 if (ret) {
3254 obj_req->pending.result = ret;
3255 return;
3256 }
3257
3258 obj_req->pending.num_pending++;
3259}
3260
3261static bool rbd_obj_advance_copyup(struct rbd_obj_request *obj_req, int *result)
3262{
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02003263 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
Ilya Dryomov793333a302019-06-13 17:44:08 +02003264 int ret;
3265
3266again:
3267 switch (obj_req->copyup_state) {
3268 case RBD_OBJ_COPYUP_START:
3269 rbd_assert(!*result);
3270
3271 ret = rbd_obj_copyup_read_parent(obj_req);
3272 if (ret) {
3273 *result = ret;
3274 return true;
3275 }
3276 if (obj_req->num_img_extents)
3277 obj_req->copyup_state = RBD_OBJ_COPYUP_READ_PARENT;
3278 else
3279 obj_req->copyup_state = RBD_OBJ_COPYUP_WRITE_OBJECT;
3280 return false;
3281 case RBD_OBJ_COPYUP_READ_PARENT:
3282 if (*result)
3283 return true;
3284
3285 if (is_zero_bvecs(obj_req->copyup_bvecs,
3286 rbd_obj_img_extents_bytes(obj_req))) {
3287 dout("%s %p detected zeros\n", __func__, obj_req);
3288 obj_req->flags |= RBD_OBJ_FLAG_COPYUP_ZEROS;
3289 }
3290
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02003291 rbd_obj_copyup_object_maps(obj_req);
3292 if (!obj_req->pending.num_pending) {
3293 *result = obj_req->pending.result;
3294 obj_req->copyup_state = RBD_OBJ_COPYUP_OBJECT_MAPS;
3295 goto again;
3296 }
3297 obj_req->copyup_state = __RBD_OBJ_COPYUP_OBJECT_MAPS;
3298 return false;
3299 case __RBD_OBJ_COPYUP_OBJECT_MAPS:
3300 if (!pending_result_dec(&obj_req->pending, result))
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003301 return false;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003302 /* fall through */
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02003303 case RBD_OBJ_COPYUP_OBJECT_MAPS:
3304 if (*result) {
3305 rbd_warn(rbd_dev, "snap object map update failed: %d",
3306 *result);
3307 return true;
3308 }
3309
Ilya Dryomov793333a302019-06-13 17:44:08 +02003310 rbd_obj_copyup_write_object(obj_req);
3311 if (!obj_req->pending.num_pending) {
3312 *result = obj_req->pending.result;
3313 obj_req->copyup_state = RBD_OBJ_COPYUP_WRITE_OBJECT;
3314 goto again;
3315 }
3316 obj_req->copyup_state = __RBD_OBJ_COPYUP_WRITE_OBJECT;
3317 return false;
3318 case __RBD_OBJ_COPYUP_WRITE_OBJECT:
3319 if (!pending_result_dec(&obj_req->pending, result))
3320 return false;
3321 /* fall through */
3322 case RBD_OBJ_COPYUP_WRITE_OBJECT:
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003323 return true;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003324 default:
Arnd Bergmannc6244b32018-04-04 14:53:39 +02003325 BUG();
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003326 }
3327}
3328
3329/*
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02003330 * Return:
3331 * 0 - object map update sent
3332 * 1 - object map update isn't needed
3333 * <0 - error
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003334 */
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02003335static int rbd_obj_write_post_object_map(struct rbd_obj_request *obj_req)
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003336{
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02003337 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
3338 u8 current_state = OBJECT_PENDING;
3339
3340 if (!(rbd_dev->header.features & RBD_FEATURE_OBJECT_MAP))
3341 return 1;
3342
3343 if (!(obj_req->flags & RBD_OBJ_FLAG_DELETION))
3344 return 1;
3345
3346 return rbd_object_map_update(obj_req, CEPH_NOSNAP, OBJECT_NONEXISTENT,
3347 &current_state);
3348}
3349
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02003350static bool rbd_obj_advance_write(struct rbd_obj_request *obj_req, int *result)
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003351{
Ilya Dryomov793333a302019-06-13 17:44:08 +02003352 struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003353 int ret;
3354
Ilya Dryomov793333a302019-06-13 17:44:08 +02003355again:
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003356 switch (obj_req->write_state) {
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02003357 case RBD_OBJ_WRITE_START:
3358 rbd_assert(!*result);
3359
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02003360 if (rbd_obj_write_is_noop(obj_req))
3361 return true;
3362
3363 ret = rbd_obj_write_pre_object_map(obj_req);
3364 if (ret < 0) {
3365 *result = ret;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003366 return true;
3367 }
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02003368 obj_req->write_state = RBD_OBJ_WRITE_PRE_OBJECT_MAP;
3369 if (ret > 0)
3370 goto again;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003371 return false;
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02003372 case RBD_OBJ_WRITE_PRE_OBJECT_MAP:
3373 if (*result) {
3374 rbd_warn(rbd_dev, "pre object map update failed: %d",
3375 *result);
3376 return true;
3377 }
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02003378 ret = rbd_obj_write_object(obj_req);
3379 if (ret) {
3380 *result = ret;
3381 return true;
3382 }
3383 obj_req->write_state = RBD_OBJ_WRITE_OBJECT;
3384 return false;
Ilya Dryomov0ad5d952019-05-14 20:45:38 +02003385 case RBD_OBJ_WRITE_OBJECT:
Ilya Dryomov54ab3b22019-05-11 16:21:49 +02003386 if (*result == -ENOENT) {
Ilya Dryomov0ad5d952019-05-14 20:45:38 +02003387 if (obj_req->flags & RBD_OBJ_FLAG_COPYUP_ENABLED) {
Ilya Dryomov793333a302019-06-13 17:44:08 +02003388 *result = 0;
3389 obj_req->copyup_state = RBD_OBJ_COPYUP_START;
3390 obj_req->write_state = __RBD_OBJ_WRITE_COPYUP;
3391 goto again;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003392 }
Ilya Dryomov0ad5d952019-05-14 20:45:38 +02003393 /*
3394 * On a non-existent object:
3395 * delete - -ENOENT, truncate/zero - 0
3396 */
3397 if (obj_req->flags & RBD_OBJ_FLAG_DELETION)
3398 *result = 0;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003399 }
Ilya Dryomov793333a302019-06-13 17:44:08 +02003400 if (*result)
3401 return true;
3402
3403 obj_req->write_state = RBD_OBJ_WRITE_COPYUP;
3404 goto again;
3405 case __RBD_OBJ_WRITE_COPYUP:
3406 if (!rbd_obj_advance_copyup(obj_req, result))
3407 return false;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003408 /* fall through */
Ilya Dryomov793333a302019-06-13 17:44:08 +02003409 case RBD_OBJ_WRITE_COPYUP:
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02003410 if (*result) {
Ilya Dryomov793333a302019-06-13 17:44:08 +02003411 rbd_warn(rbd_dev, "copyup failed: %d", *result);
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02003412 return true;
3413 }
3414 ret = rbd_obj_write_post_object_map(obj_req);
3415 if (ret < 0) {
3416 *result = ret;
3417 return true;
3418 }
3419 obj_req->write_state = RBD_OBJ_WRITE_POST_OBJECT_MAP;
3420 if (ret > 0)
3421 goto again;
3422 return false;
3423 case RBD_OBJ_WRITE_POST_OBJECT_MAP:
3424 if (*result)
3425 rbd_warn(rbd_dev, "post object map update failed: %d",
3426 *result);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003427 return true;
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003428 default:
Arnd Bergmannc6244b32018-04-04 14:53:39 +02003429 BUG();
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003430 }
3431}
3432
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003433/*
Ilya Dryomov0ad5d952019-05-14 20:45:38 +02003434 * Return true if @obj_req is completed.
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003435 */
Ilya Dryomov54ab3b22019-05-11 16:21:49 +02003436static bool __rbd_obj_handle_request(struct rbd_obj_request *obj_req,
3437 int *result)
Ilya Dryomov7114eda2018-02-01 11:50:47 +01003438{
3439 struct rbd_img_request *img_req = obj_req->img_request;
Ilya Dryomov0192ce22019-05-16 15:06:56 +02003440 struct rbd_device *rbd_dev = img_req->rbd_dev;
Ilya Dryomov0ad5d952019-05-14 20:45:38 +02003441 bool done;
Ilya Dryomov7114eda2018-02-01 11:50:47 +01003442
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02003443 mutex_lock(&obj_req->state_mutex);
Ilya Dryomov0ad5d952019-05-14 20:45:38 +02003444 if (!rbd_img_is_write(img_req))
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02003445 done = rbd_obj_advance_read(obj_req, result);
Ilya Dryomov0ad5d952019-05-14 20:45:38 +02003446 else
Ilya Dryomov85b5e6d2019-05-14 21:06:07 +02003447 done = rbd_obj_advance_write(obj_req, result);
3448 mutex_unlock(&obj_req->state_mutex);
Alex Elder02c74fb2013-05-06 17:40:33 -05003449
Ilya Dryomov0192ce22019-05-16 15:06:56 +02003450 if (done && *result) {
3451 rbd_assert(*result < 0);
3452 rbd_warn(rbd_dev, "%s at objno %llu %llu~%llu result %d",
3453 obj_op_name(img_req->op_type), obj_req->ex.oe_objno,
3454 obj_req->ex.oe_off, obj_req->ex.oe_len, *result);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05003455 }
Ilya Dryomov0ad5d952019-05-14 20:45:38 +02003456 return done;
Alex Elder8b3e1a52013-01-24 16:13:36 -06003457}
3458
Ilya Dryomov0192ce22019-05-16 15:06:56 +02003459/*
3460 * This is open-coded in rbd_img_handle_request() to avoid parent chain
3461 * recursion.
3462 */
Ilya Dryomov54ab3b22019-05-11 16:21:49 +02003463static void rbd_obj_handle_request(struct rbd_obj_request *obj_req, int result)
Alex Elder8b3e1a52013-01-24 16:13:36 -06003464{
Ilya Dryomov0192ce22019-05-16 15:06:56 +02003465 if (__rbd_obj_handle_request(obj_req, &result))
3466 rbd_img_handle_request(obj_req->img_request, result);
Ilya Dryomov7114eda2018-02-01 11:50:47 +01003467}
Alex Elder8b3e1a52013-01-24 16:13:36 -06003468
Ilya Dryomove1fddc82019-05-30 16:07:48 +02003469static bool need_exclusive_lock(struct rbd_img_request *img_req)
Ilya Dryomov7114eda2018-02-01 11:50:47 +01003470{
Ilya Dryomove1fddc82019-05-30 16:07:48 +02003471 struct rbd_device *rbd_dev = img_req->rbd_dev;
3472
3473 if (!(rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK))
3474 return false;
3475
Ilya Dryomov3fe69922019-11-12 19:41:48 +01003476 if (rbd_is_ro(rbd_dev))
Ilya Dryomove1fddc82019-05-30 16:07:48 +02003477 return false;
3478
Ilya Dryomov7114eda2018-02-01 11:50:47 +01003479 rbd_assert(!test_bit(IMG_REQ_CHILD, &img_req->flags));
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02003480 if (rbd_dev->opts->lock_on_read ||
3481 (rbd_dev->header.features & RBD_FEATURE_OBJECT_MAP))
Ilya Dryomove1fddc82019-05-30 16:07:48 +02003482 return true;
Alex Elder8b3e1a52013-01-24 16:13:36 -06003483
Ilya Dryomove1fddc82019-05-30 16:07:48 +02003484 return rbd_img_is_write(img_req);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003485}
Alex Elder8b3e1a52013-01-24 16:13:36 -06003486
Ilya Dryomov637cd062019-06-06 17:14:49 +02003487static bool rbd_lock_add_request(struct rbd_img_request *img_req)
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003488{
Ilya Dryomove1fddc82019-05-30 16:07:48 +02003489 struct rbd_device *rbd_dev = img_req->rbd_dev;
Ilya Dryomov637cd062019-06-06 17:14:49 +02003490 bool locked;
Ilya Dryomove1fddc82019-05-30 16:07:48 +02003491
3492 lockdep_assert_held(&rbd_dev->lock_rwsem);
Ilya Dryomov637cd062019-06-06 17:14:49 +02003493 locked = rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED;
Ilya Dryomove1fddc82019-05-30 16:07:48 +02003494 spin_lock(&rbd_dev->lock_lists_lock);
3495 rbd_assert(list_empty(&img_req->lock_item));
Ilya Dryomov637cd062019-06-06 17:14:49 +02003496 if (!locked)
3497 list_add_tail(&img_req->lock_item, &rbd_dev->acquiring_list);
3498 else
3499 list_add_tail(&img_req->lock_item, &rbd_dev->running_list);
Ilya Dryomove1fddc82019-05-30 16:07:48 +02003500 spin_unlock(&rbd_dev->lock_lists_lock);
Ilya Dryomov637cd062019-06-06 17:14:49 +02003501 return locked;
Ilya Dryomove1fddc82019-05-30 16:07:48 +02003502}
3503
3504static void rbd_lock_del_request(struct rbd_img_request *img_req)
3505{
3506 struct rbd_device *rbd_dev = img_req->rbd_dev;
3507 bool need_wakeup;
3508
3509 lockdep_assert_held(&rbd_dev->lock_rwsem);
3510 spin_lock(&rbd_dev->lock_lists_lock);
3511 rbd_assert(!list_empty(&img_req->lock_item));
3512 list_del_init(&img_req->lock_item);
3513 need_wakeup = (rbd_dev->lock_state == RBD_LOCK_STATE_RELEASING &&
3514 list_empty(&rbd_dev->running_list));
3515 spin_unlock(&rbd_dev->lock_lists_lock);
3516 if (need_wakeup)
3517 complete(&rbd_dev->releasing_wait);
3518}
3519
Ilya Dryomov637cd062019-06-06 17:14:49 +02003520static int rbd_img_exclusive_lock(struct rbd_img_request *img_req)
3521{
3522 struct rbd_device *rbd_dev = img_req->rbd_dev;
3523
3524 if (!need_exclusive_lock(img_req))
3525 return 1;
3526
3527 if (rbd_lock_add_request(img_req))
3528 return 1;
3529
3530 if (rbd_dev->opts->exclusive) {
3531 WARN_ON(1); /* lock got released? */
3532 return -EROFS;
3533 }
3534
3535 /*
3536 * Note the use of mod_delayed_work() in rbd_acquire_lock()
3537 * and cancel_delayed_work() in wake_lock_waiters().
3538 */
3539 dout("%s rbd_dev %p queueing lock_dwork\n", __func__, rbd_dev);
3540 queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0);
3541 return 0;
3542}
3543
Ilya Dryomov0192ce22019-05-16 15:06:56 +02003544static void rbd_img_object_requests(struct rbd_img_request *img_req)
3545{
3546 struct rbd_obj_request *obj_req;
3547
3548 rbd_assert(!img_req->pending.result && !img_req->pending.num_pending);
3549
3550 for_each_obj_request(img_req, obj_req) {
3551 int result = 0;
3552
3553 if (__rbd_obj_handle_request(obj_req, &result)) {
3554 if (result) {
3555 img_req->pending.result = result;
3556 return;
3557 }
3558 } else {
3559 img_req->pending.num_pending++;
3560 }
3561 }
3562}
3563
3564static bool rbd_img_advance(struct rbd_img_request *img_req, int *result)
3565{
Ilya Dryomov637cd062019-06-06 17:14:49 +02003566 struct rbd_device *rbd_dev = img_req->rbd_dev;
3567 int ret;
Ilya Dryomov7114eda2018-02-01 11:50:47 +01003568
3569again:
Ilya Dryomov0192ce22019-05-16 15:06:56 +02003570 switch (img_req->state) {
3571 case RBD_IMG_START:
3572 rbd_assert(!*result);
Ilya Dryomov3da691b2018-01-29 14:04:08 +01003573
Ilya Dryomov637cd062019-06-06 17:14:49 +02003574 ret = rbd_img_exclusive_lock(img_req);
3575 if (ret < 0) {
3576 *result = ret;
3577 return true;
3578 }
3579 img_req->state = RBD_IMG_EXCLUSIVE_LOCK;
3580 if (ret > 0)
3581 goto again;
3582 return false;
3583 case RBD_IMG_EXCLUSIVE_LOCK:
3584 if (*result)
3585 return true;
3586
3587 rbd_assert(!need_exclusive_lock(img_req) ||
3588 __rbd_is_lock_owner(rbd_dev));
3589
Ilya Dryomov0192ce22019-05-16 15:06:56 +02003590 rbd_img_object_requests(img_req);
3591 if (!img_req->pending.num_pending) {
3592 *result = img_req->pending.result;
3593 img_req->state = RBD_IMG_OBJECT_REQUESTS;
3594 goto again;
3595 }
3596 img_req->state = __RBD_IMG_OBJECT_REQUESTS;
3597 return false;
3598 case __RBD_IMG_OBJECT_REQUESTS:
3599 if (!pending_result_dec(&img_req->pending, result))
3600 return false;
3601 /* fall through */
3602 case RBD_IMG_OBJECT_REQUESTS:
3603 return true;
3604 default:
3605 BUG();
Ilya Dryomov7114eda2018-02-01 11:50:47 +01003606 }
Ilya Dryomov0192ce22019-05-16 15:06:56 +02003607}
Ilya Dryomov7114eda2018-02-01 11:50:47 +01003608
Ilya Dryomov0192ce22019-05-16 15:06:56 +02003609/*
3610 * Return true if @img_req is completed.
3611 */
3612static bool __rbd_img_handle_request(struct rbd_img_request *img_req,
3613 int *result)
3614{
3615 struct rbd_device *rbd_dev = img_req->rbd_dev;
3616 bool done;
3617
Ilya Dryomove1fddc82019-05-30 16:07:48 +02003618 if (need_exclusive_lock(img_req)) {
3619 down_read(&rbd_dev->lock_rwsem);
3620 mutex_lock(&img_req->state_mutex);
3621 done = rbd_img_advance(img_req, result);
3622 if (done)
3623 rbd_lock_del_request(img_req);
3624 mutex_unlock(&img_req->state_mutex);
3625 up_read(&rbd_dev->lock_rwsem);
3626 } else {
3627 mutex_lock(&img_req->state_mutex);
3628 done = rbd_img_advance(img_req, result);
3629 mutex_unlock(&img_req->state_mutex);
Ilya Dryomov7114eda2018-02-01 11:50:47 +01003630 }
3631
Ilya Dryomov0192ce22019-05-16 15:06:56 +02003632 if (done && *result) {
3633 rbd_assert(*result < 0);
3634 rbd_warn(rbd_dev, "%s%s result %d",
3635 test_bit(IMG_REQ_CHILD, &img_req->flags) ? "child " : "",
3636 obj_op_name(img_req->op_type), *result);
3637 }
3638 return done;
3639}
3640
3641static void rbd_img_handle_request(struct rbd_img_request *img_req, int result)
3642{
3643again:
3644 if (!__rbd_img_handle_request(img_req, &result))
3645 return;
3646
Ilya Dryomov7114eda2018-02-01 11:50:47 +01003647 if (test_bit(IMG_REQ_CHILD, &img_req->flags)) {
Ilya Dryomov0192ce22019-05-16 15:06:56 +02003648 struct rbd_obj_request *obj_req = img_req->obj_request;
3649
Ilya Dryomov54ab3b22019-05-11 16:21:49 +02003650 rbd_img_request_put(img_req);
Ilya Dryomov0192ce22019-05-16 15:06:56 +02003651 if (__rbd_obj_handle_request(obj_req, &result)) {
3652 img_req = obj_req->img_request;
3653 goto again;
3654 }
3655 } else {
3656 struct request *rq = img_req->rq;
3657
3658 rbd_img_request_put(img_req);
3659 blk_mq_end_request(rq, errno_to_blk_status(result));
Ilya Dryomov7114eda2018-02-01 11:50:47 +01003660 }
Alex Elder8b3e1a52013-01-24 16:13:36 -06003661}
3662
Ilya Dryomoved95b212016-08-12 16:40:02 +02003663static const struct rbd_client_id rbd_empty_cid;
3664
3665static bool rbd_cid_equal(const struct rbd_client_id *lhs,
3666 const struct rbd_client_id *rhs)
3667{
3668 return lhs->gid == rhs->gid && lhs->handle == rhs->handle;
3669}
3670
3671static struct rbd_client_id rbd_get_cid(struct rbd_device *rbd_dev)
3672{
3673 struct rbd_client_id cid;
3674
3675 mutex_lock(&rbd_dev->watch_mutex);
3676 cid.gid = ceph_client_gid(rbd_dev->rbd_client->client);
3677 cid.handle = rbd_dev->watch_cookie;
3678 mutex_unlock(&rbd_dev->watch_mutex);
3679 return cid;
3680}
3681
3682/*
3683 * lock_rwsem must be held for write
3684 */
3685static void rbd_set_owner_cid(struct rbd_device *rbd_dev,
3686 const struct rbd_client_id *cid)
3687{
3688 dout("%s rbd_dev %p %llu-%llu -> %llu-%llu\n", __func__, rbd_dev,
3689 rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle,
3690 cid->gid, cid->handle);
3691 rbd_dev->owner_cid = *cid; /* struct */
3692}
3693
3694static void format_lock_cookie(struct rbd_device *rbd_dev, char *buf)
3695{
3696 mutex_lock(&rbd_dev->watch_mutex);
3697 sprintf(buf, "%s %llu", RBD_LOCK_COOKIE_PREFIX, rbd_dev->watch_cookie);
3698 mutex_unlock(&rbd_dev->watch_mutex);
3699}
3700
Florian Margaineedd8ca82017-12-13 16:43:59 +01003701static void __rbd_lock(struct rbd_device *rbd_dev, const char *cookie)
3702{
3703 struct rbd_client_id cid = rbd_get_cid(rbd_dev);
3704
Ilya Dryomova2b1da02019-05-30 11:15:23 +02003705 rbd_dev->lock_state = RBD_LOCK_STATE_LOCKED;
Florian Margaineedd8ca82017-12-13 16:43:59 +01003706 strcpy(rbd_dev->lock_cookie, cookie);
3707 rbd_set_owner_cid(rbd_dev, &cid);
3708 queue_work(rbd_dev->task_wq, &rbd_dev->acquired_lock_work);
3709}
3710
Ilya Dryomoved95b212016-08-12 16:40:02 +02003711/*
3712 * lock_rwsem must be held for write
3713 */
3714static int rbd_lock(struct rbd_device *rbd_dev)
3715{
3716 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003717 char cookie[32];
3718 int ret;
3719
Ilya Dryomovcbbfb0f2017-04-13 12:17:38 +02003720 WARN_ON(__rbd_is_lock_owner(rbd_dev) ||
3721 rbd_dev->lock_cookie[0] != '\0');
Ilya Dryomoved95b212016-08-12 16:40:02 +02003722
3723 format_lock_cookie(rbd_dev, cookie);
3724 ret = ceph_cls_lock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc,
3725 RBD_LOCK_NAME, CEPH_CLS_LOCK_EXCLUSIVE, cookie,
3726 RBD_LOCK_TAG, "", 0);
3727 if (ret)
3728 return ret;
3729
Florian Margaineedd8ca82017-12-13 16:43:59 +01003730 __rbd_lock(rbd_dev, cookie);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003731 return 0;
3732}
3733
3734/*
3735 * lock_rwsem must be held for write
3736 */
Ilya Dryomovbbead742017-04-13 12:17:38 +02003737static void rbd_unlock(struct rbd_device *rbd_dev)
Ilya Dryomoved95b212016-08-12 16:40:02 +02003738{
3739 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003740 int ret;
3741
Ilya Dryomovcbbfb0f2017-04-13 12:17:38 +02003742 WARN_ON(!__rbd_is_lock_owner(rbd_dev) ||
3743 rbd_dev->lock_cookie[0] == '\0');
Ilya Dryomoved95b212016-08-12 16:40:02 +02003744
Ilya Dryomoved95b212016-08-12 16:40:02 +02003745 ret = ceph_cls_unlock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc,
Ilya Dryomovcbbfb0f2017-04-13 12:17:38 +02003746 RBD_LOCK_NAME, rbd_dev->lock_cookie);
Ilya Dryomovbbead742017-04-13 12:17:38 +02003747 if (ret && ret != -ENOENT)
Ilya Dryomov637cd062019-06-06 17:14:49 +02003748 rbd_warn(rbd_dev, "failed to unlock header: %d", ret);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003749
Ilya Dryomovbbead742017-04-13 12:17:38 +02003750 /* treat errors as the image is unlocked */
3751 rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED;
Ilya Dryomovcbbfb0f2017-04-13 12:17:38 +02003752 rbd_dev->lock_cookie[0] = '\0';
Ilya Dryomoved95b212016-08-12 16:40:02 +02003753 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
3754 queue_work(rbd_dev->task_wq, &rbd_dev->released_lock_work);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003755}
3756
3757static int __rbd_notify_op_lock(struct rbd_device *rbd_dev,
3758 enum rbd_notify_op notify_op,
3759 struct page ***preply_pages,
3760 size_t *preply_len)
3761{
3762 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3763 struct rbd_client_id cid = rbd_get_cid(rbd_dev);
Kyle Spiers08a79102018-03-17 09:44:01 -07003764 char buf[4 + 8 + 8 + CEPH_ENCODING_START_BLK_LEN];
3765 int buf_size = sizeof(buf);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003766 void *p = buf;
3767
3768 dout("%s rbd_dev %p notify_op %d\n", __func__, rbd_dev, notify_op);
3769
3770 /* encode *LockPayload NotifyMessage (op + ClientId) */
3771 ceph_start_encoding(&p, 2, 1, buf_size - CEPH_ENCODING_START_BLK_LEN);
3772 ceph_encode_32(&p, notify_op);
3773 ceph_encode_64(&p, cid.gid);
3774 ceph_encode_64(&p, cid.handle);
3775
3776 return ceph_osdc_notify(osdc, &rbd_dev->header_oid,
3777 &rbd_dev->header_oloc, buf, buf_size,
3778 RBD_NOTIFY_TIMEOUT, preply_pages, preply_len);
3779}
3780
3781static void rbd_notify_op_lock(struct rbd_device *rbd_dev,
3782 enum rbd_notify_op notify_op)
3783{
3784 struct page **reply_pages;
3785 size_t reply_len;
3786
3787 __rbd_notify_op_lock(rbd_dev, notify_op, &reply_pages, &reply_len);
3788 ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len));
3789}
3790
3791static void rbd_notify_acquired_lock(struct work_struct *work)
3792{
3793 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
3794 acquired_lock_work);
3795
3796 rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_ACQUIRED_LOCK);
3797}
3798
3799static void rbd_notify_released_lock(struct work_struct *work)
3800{
3801 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
3802 released_lock_work);
3803
3804 rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_RELEASED_LOCK);
3805}
3806
3807static int rbd_request_lock(struct rbd_device *rbd_dev)
3808{
3809 struct page **reply_pages;
3810 size_t reply_len;
3811 bool lock_owner_responded = false;
3812 int ret;
3813
3814 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3815
3816 ret = __rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_REQUEST_LOCK,
3817 &reply_pages, &reply_len);
3818 if (ret && ret != -ETIMEDOUT) {
3819 rbd_warn(rbd_dev, "failed to request lock: %d", ret);
3820 goto out;
3821 }
3822
3823 if (reply_len > 0 && reply_len <= PAGE_SIZE) {
3824 void *p = page_address(reply_pages[0]);
3825 void *const end = p + reply_len;
3826 u32 n;
3827
3828 ceph_decode_32_safe(&p, end, n, e_inval); /* num_acks */
3829 while (n--) {
3830 u8 struct_v;
3831 u32 len;
3832
3833 ceph_decode_need(&p, end, 8 + 8, e_inval);
3834 p += 8 + 8; /* skip gid and cookie */
3835
3836 ceph_decode_32_safe(&p, end, len, e_inval);
3837 if (!len)
3838 continue;
3839
3840 if (lock_owner_responded) {
3841 rbd_warn(rbd_dev,
3842 "duplicate lock owners detected");
3843 ret = -EIO;
3844 goto out;
3845 }
3846
3847 lock_owner_responded = true;
3848 ret = ceph_start_decoding(&p, end, 1, "ResponseMessage",
3849 &struct_v, &len);
3850 if (ret) {
3851 rbd_warn(rbd_dev,
3852 "failed to decode ResponseMessage: %d",
3853 ret);
3854 goto e_inval;
3855 }
3856
3857 ret = ceph_decode_32(&p);
3858 }
3859 }
3860
3861 if (!lock_owner_responded) {
3862 rbd_warn(rbd_dev, "no lock owners detected");
3863 ret = -ETIMEDOUT;
3864 }
3865
3866out:
3867 ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len));
3868 return ret;
3869
3870e_inval:
3871 ret = -EINVAL;
3872 goto out;
3873}
3874
Ilya Dryomov637cd062019-06-06 17:14:49 +02003875/*
3876 * Either image request state machine(s) or rbd_add_acquire_lock()
3877 * (i.e. "rbd map").
3878 */
3879static void wake_lock_waiters(struct rbd_device *rbd_dev, int result)
Ilya Dryomoved95b212016-08-12 16:40:02 +02003880{
Ilya Dryomov637cd062019-06-06 17:14:49 +02003881 struct rbd_img_request *img_req;
3882
3883 dout("%s rbd_dev %p result %d\n", __func__, rbd_dev, result);
Linus Torvaldsd9b9c892019-07-18 11:05:25 -07003884 lockdep_assert_held_write(&rbd_dev->lock_rwsem);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003885
3886 cancel_delayed_work(&rbd_dev->lock_dwork);
Ilya Dryomov637cd062019-06-06 17:14:49 +02003887 if (!completion_done(&rbd_dev->acquire_wait)) {
3888 rbd_assert(list_empty(&rbd_dev->acquiring_list) &&
3889 list_empty(&rbd_dev->running_list));
3890 rbd_dev->acquire_err = result;
3891 complete_all(&rbd_dev->acquire_wait);
3892 return;
3893 }
3894
3895 list_for_each_entry(img_req, &rbd_dev->acquiring_list, lock_item) {
3896 mutex_lock(&img_req->state_mutex);
3897 rbd_assert(img_req->state == RBD_IMG_EXCLUSIVE_LOCK);
3898 rbd_img_schedule(img_req, result);
3899 mutex_unlock(&img_req->state_mutex);
3900 }
3901
3902 list_splice_tail_init(&rbd_dev->acquiring_list, &rbd_dev->running_list);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003903}
3904
3905static int get_lock_owner_info(struct rbd_device *rbd_dev,
3906 struct ceph_locker **lockers, u32 *num_lockers)
3907{
3908 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3909 u8 lock_type;
3910 char *lock_tag;
3911 int ret;
3912
3913 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3914
3915 ret = ceph_cls_lock_info(osdc, &rbd_dev->header_oid,
3916 &rbd_dev->header_oloc, RBD_LOCK_NAME,
3917 &lock_type, &lock_tag, lockers, num_lockers);
3918 if (ret)
3919 return ret;
3920
3921 if (*num_lockers == 0) {
3922 dout("%s rbd_dev %p no lockers detected\n", __func__, rbd_dev);
3923 goto out;
3924 }
3925
3926 if (strcmp(lock_tag, RBD_LOCK_TAG)) {
3927 rbd_warn(rbd_dev, "locked by external mechanism, tag %s",
3928 lock_tag);
3929 ret = -EBUSY;
3930 goto out;
3931 }
3932
3933 if (lock_type == CEPH_CLS_LOCK_SHARED) {
3934 rbd_warn(rbd_dev, "shared lock type detected");
3935 ret = -EBUSY;
3936 goto out;
3937 }
3938
3939 if (strncmp((*lockers)[0].id.cookie, RBD_LOCK_COOKIE_PREFIX,
3940 strlen(RBD_LOCK_COOKIE_PREFIX))) {
3941 rbd_warn(rbd_dev, "locked by external mechanism, cookie %s",
3942 (*lockers)[0].id.cookie);
3943 ret = -EBUSY;
3944 goto out;
3945 }
3946
3947out:
3948 kfree(lock_tag);
3949 return ret;
3950}
3951
3952static int find_watcher(struct rbd_device *rbd_dev,
3953 const struct ceph_locker *locker)
3954{
3955 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3956 struct ceph_watch_item *watchers;
3957 u32 num_watchers;
3958 u64 cookie;
3959 int i;
3960 int ret;
3961
3962 ret = ceph_osdc_list_watchers(osdc, &rbd_dev->header_oid,
3963 &rbd_dev->header_oloc, &watchers,
3964 &num_watchers);
3965 if (ret)
3966 return ret;
3967
3968 sscanf(locker->id.cookie, RBD_LOCK_COOKIE_PREFIX " %llu", &cookie);
3969 for (i = 0; i < num_watchers; i++) {
3970 if (!memcmp(&watchers[i].addr, &locker->info.addr,
3971 sizeof(locker->info.addr)) &&
3972 watchers[i].cookie == cookie) {
3973 struct rbd_client_id cid = {
3974 .gid = le64_to_cpu(watchers[i].name.num),
3975 .handle = cookie,
3976 };
3977
3978 dout("%s rbd_dev %p found cid %llu-%llu\n", __func__,
3979 rbd_dev, cid.gid, cid.handle);
3980 rbd_set_owner_cid(rbd_dev, &cid);
3981 ret = 1;
3982 goto out;
3983 }
3984 }
3985
3986 dout("%s rbd_dev %p no watchers\n", __func__, rbd_dev);
3987 ret = 0;
3988out:
3989 kfree(watchers);
3990 return ret;
3991}
3992
3993/*
3994 * lock_rwsem must be held for write
3995 */
3996static int rbd_try_lock(struct rbd_device *rbd_dev)
3997{
3998 struct ceph_client *client = rbd_dev->rbd_client->client;
3999 struct ceph_locker *lockers;
4000 u32 num_lockers;
4001 int ret;
4002
4003 for (;;) {
4004 ret = rbd_lock(rbd_dev);
4005 if (ret != -EBUSY)
4006 return ret;
4007
4008 /* determine if the current lock holder is still alive */
4009 ret = get_lock_owner_info(rbd_dev, &lockers, &num_lockers);
4010 if (ret)
4011 return ret;
4012
4013 if (num_lockers == 0)
4014 goto again;
4015
4016 ret = find_watcher(rbd_dev, lockers);
Ilya Dryomov637cd062019-06-06 17:14:49 +02004017 if (ret)
4018 goto out; /* request lock or error */
Ilya Dryomoved95b212016-08-12 16:40:02 +02004019
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02004020 rbd_warn(rbd_dev, "breaking header lock owned by %s%llu",
Ilya Dryomoved95b212016-08-12 16:40:02 +02004021 ENTITY_NAME(lockers[0].id.name));
4022
4023 ret = ceph_monc_blacklist_add(&client->monc,
4024 &lockers[0].info.addr);
4025 if (ret) {
4026 rbd_warn(rbd_dev, "blacklist of %s%llu failed: %d",
4027 ENTITY_NAME(lockers[0].id.name), ret);
4028 goto out;
4029 }
4030
4031 ret = ceph_cls_break_lock(&client->osdc, &rbd_dev->header_oid,
4032 &rbd_dev->header_oloc, RBD_LOCK_NAME,
4033 lockers[0].id.cookie,
4034 &lockers[0].id.name);
4035 if (ret && ret != -ENOENT)
4036 goto out;
4037
4038again:
4039 ceph_free_lockers(lockers, num_lockers);
4040 }
4041
4042out:
4043 ceph_free_lockers(lockers, num_lockers);
4044 return ret;
4045}
4046
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02004047static int rbd_post_acquire_action(struct rbd_device *rbd_dev)
Ilya Dryomoved95b212016-08-12 16:40:02 +02004048{
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02004049 int ret;
4050
4051 if (rbd_dev->header.features & RBD_FEATURE_OBJECT_MAP) {
4052 ret = rbd_object_map_open(rbd_dev);
4053 if (ret)
4054 return ret;
4055 }
4056
4057 return 0;
4058}
4059
Ilya Dryomoved95b212016-08-12 16:40:02 +02004060/*
Ilya Dryomov637cd062019-06-06 17:14:49 +02004061 * Return:
4062 * 0 - lock acquired
4063 * 1 - caller should call rbd_request_lock()
4064 * <0 - error
Ilya Dryomoved95b212016-08-12 16:40:02 +02004065 */
Ilya Dryomov637cd062019-06-06 17:14:49 +02004066static int rbd_try_acquire_lock(struct rbd_device *rbd_dev)
Ilya Dryomoved95b212016-08-12 16:40:02 +02004067{
Ilya Dryomov637cd062019-06-06 17:14:49 +02004068 int ret;
Ilya Dryomoved95b212016-08-12 16:40:02 +02004069
4070 down_read(&rbd_dev->lock_rwsem);
4071 dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev,
4072 rbd_dev->lock_state);
4073 if (__rbd_is_lock_owner(rbd_dev)) {
Ilya Dryomoved95b212016-08-12 16:40:02 +02004074 up_read(&rbd_dev->lock_rwsem);
Ilya Dryomov637cd062019-06-06 17:14:49 +02004075 return 0;
Ilya Dryomoved95b212016-08-12 16:40:02 +02004076 }
4077
4078 up_read(&rbd_dev->lock_rwsem);
4079 down_write(&rbd_dev->lock_rwsem);
4080 dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev,
4081 rbd_dev->lock_state);
Ilya Dryomov637cd062019-06-06 17:14:49 +02004082 if (__rbd_is_lock_owner(rbd_dev)) {
4083 up_write(&rbd_dev->lock_rwsem);
4084 return 0;
Ilya Dryomoved95b212016-08-12 16:40:02 +02004085 }
4086
Ilya Dryomov637cd062019-06-06 17:14:49 +02004087 ret = rbd_try_lock(rbd_dev);
4088 if (ret < 0) {
4089 rbd_warn(rbd_dev, "failed to lock header: %d", ret);
4090 if (ret == -EBLACKLISTED)
4091 goto out;
4092
4093 ret = 1; /* request lock anyway */
4094 }
4095 if (ret > 0) {
4096 up_write(&rbd_dev->lock_rwsem);
4097 return ret;
4098 }
4099
4100 rbd_assert(rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED);
4101 rbd_assert(list_empty(&rbd_dev->running_list));
4102
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02004103 ret = rbd_post_acquire_action(rbd_dev);
4104 if (ret) {
4105 rbd_warn(rbd_dev, "post-acquire action failed: %d", ret);
4106 /*
4107 * Can't stay in RBD_LOCK_STATE_LOCKED because
4108 * rbd_lock_add_request() would let the request through,
4109 * assuming that e.g. object map is locked and loaded.
4110 */
4111 rbd_unlock(rbd_dev);
4112 }
4113
Ilya Dryomov637cd062019-06-06 17:14:49 +02004114out:
4115 wake_lock_waiters(rbd_dev, ret);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004116 up_write(&rbd_dev->lock_rwsem);
Ilya Dryomov637cd062019-06-06 17:14:49 +02004117 return ret;
Ilya Dryomoved95b212016-08-12 16:40:02 +02004118}
4119
4120static void rbd_acquire_lock(struct work_struct *work)
4121{
4122 struct rbd_device *rbd_dev = container_of(to_delayed_work(work),
4123 struct rbd_device, lock_dwork);
Ilya Dryomov637cd062019-06-06 17:14:49 +02004124 int ret;
Ilya Dryomoved95b212016-08-12 16:40:02 +02004125
4126 dout("%s rbd_dev %p\n", __func__, rbd_dev);
4127again:
Ilya Dryomov637cd062019-06-06 17:14:49 +02004128 ret = rbd_try_acquire_lock(rbd_dev);
4129 if (ret <= 0) {
4130 dout("%s rbd_dev %p ret %d - done\n", __func__, rbd_dev, ret);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004131 return;
4132 }
4133
4134 ret = rbd_request_lock(rbd_dev);
4135 if (ret == -ETIMEDOUT) {
4136 goto again; /* treat this as a dead client */
Ilya Dryomove010dd02017-04-13 12:17:39 +02004137 } else if (ret == -EROFS) {
4138 rbd_warn(rbd_dev, "peer will not release lock");
Ilya Dryomov637cd062019-06-06 17:14:49 +02004139 down_write(&rbd_dev->lock_rwsem);
4140 wake_lock_waiters(rbd_dev, ret);
4141 up_write(&rbd_dev->lock_rwsem);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004142 } else if (ret < 0) {
4143 rbd_warn(rbd_dev, "error requesting lock: %d", ret);
4144 mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork,
4145 RBD_RETRY_DELAY);
4146 } else {
4147 /*
4148 * lock owner acked, but resend if we don't see them
4149 * release the lock
4150 */
Colin Ian King6b0a8772019-11-07 22:36:46 +00004151 dout("%s rbd_dev %p requeuing lock_dwork\n", __func__,
Ilya Dryomoved95b212016-08-12 16:40:02 +02004152 rbd_dev);
4153 mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork,
4154 msecs_to_jiffies(2 * RBD_NOTIFY_TIMEOUT * MSEC_PER_SEC));
4155 }
4156}
4157
Ilya Dryomova2b1da02019-05-30 11:15:23 +02004158static bool rbd_quiesce_lock(struct rbd_device *rbd_dev)
Ilya Dryomoved95b212016-08-12 16:40:02 +02004159{
Ilya Dryomove1fddc82019-05-30 16:07:48 +02004160 bool need_wait;
4161
Ilya Dryomova2b1da02019-05-30 11:15:23 +02004162 dout("%s rbd_dev %p\n", __func__, rbd_dev);
Linus Torvaldsd9b9c892019-07-18 11:05:25 -07004163 lockdep_assert_held_write(&rbd_dev->lock_rwsem);
Ilya Dryomova2b1da02019-05-30 11:15:23 +02004164
Ilya Dryomoved95b212016-08-12 16:40:02 +02004165 if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED)
4166 return false;
4167
Ilya Dryomoved95b212016-08-12 16:40:02 +02004168 /*
4169 * Ensure that all in-flight IO is flushed.
Ilya Dryomoved95b212016-08-12 16:40:02 +02004170 */
Ilya Dryomove1fddc82019-05-30 16:07:48 +02004171 rbd_dev->lock_state = RBD_LOCK_STATE_RELEASING;
4172 rbd_assert(!completion_done(&rbd_dev->releasing_wait));
4173 need_wait = !list_empty(&rbd_dev->running_list);
4174 downgrade_write(&rbd_dev->lock_rwsem);
4175 if (need_wait)
4176 wait_for_completion(&rbd_dev->releasing_wait);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004177 up_read(&rbd_dev->lock_rwsem);
4178
4179 down_write(&rbd_dev->lock_rwsem);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004180 if (rbd_dev->lock_state != RBD_LOCK_STATE_RELEASING)
4181 return false;
4182
Ilya Dryomove1fddc82019-05-30 16:07:48 +02004183 rbd_assert(list_empty(&rbd_dev->running_list));
Ilya Dryomova2b1da02019-05-30 11:15:23 +02004184 return true;
4185}
4186
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02004187static void rbd_pre_release_action(struct rbd_device *rbd_dev)
4188{
4189 if (rbd_dev->header.features & RBD_FEATURE_OBJECT_MAP)
4190 rbd_object_map_close(rbd_dev);
4191}
4192
Ilya Dryomove1fddc82019-05-30 16:07:48 +02004193static void __rbd_release_lock(struct rbd_device *rbd_dev)
4194{
4195 rbd_assert(list_empty(&rbd_dev->running_list));
4196
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02004197 rbd_pre_release_action(rbd_dev);
Ilya Dryomovbbead742017-04-13 12:17:38 +02004198 rbd_unlock(rbd_dev);
Ilya Dryomove1fddc82019-05-30 16:07:48 +02004199}
4200
Ilya Dryomova2b1da02019-05-30 11:15:23 +02004201/*
4202 * lock_rwsem must be held for write
4203 */
4204static void rbd_release_lock(struct rbd_device *rbd_dev)
4205{
4206 if (!rbd_quiesce_lock(rbd_dev))
4207 return;
4208
Ilya Dryomove1fddc82019-05-30 16:07:48 +02004209 __rbd_release_lock(rbd_dev);
Ilya Dryomova2b1da02019-05-30 11:15:23 +02004210
Ilya Dryomovbbead742017-04-13 12:17:38 +02004211 /*
4212 * Give others a chance to grab the lock - we would re-acquire
Ilya Dryomov637cd062019-06-06 17:14:49 +02004213 * almost immediately if we got new IO while draining the running
4214 * list otherwise. We need to ack our own notifications, so this
4215 * lock_dwork will be requeued from rbd_handle_released_lock() by
4216 * way of maybe_kick_acquire().
Ilya Dryomovbbead742017-04-13 12:17:38 +02004217 */
4218 cancel_delayed_work(&rbd_dev->lock_dwork);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004219}
4220
4221static void rbd_release_lock_work(struct work_struct *work)
4222{
4223 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
4224 unlock_work);
4225
4226 down_write(&rbd_dev->lock_rwsem);
4227 rbd_release_lock(rbd_dev);
4228 up_write(&rbd_dev->lock_rwsem);
4229}
4230
Ilya Dryomov637cd062019-06-06 17:14:49 +02004231static void maybe_kick_acquire(struct rbd_device *rbd_dev)
4232{
4233 bool have_requests;
4234
4235 dout("%s rbd_dev %p\n", __func__, rbd_dev);
4236 if (__rbd_is_lock_owner(rbd_dev))
4237 return;
4238
4239 spin_lock(&rbd_dev->lock_lists_lock);
4240 have_requests = !list_empty(&rbd_dev->acquiring_list);
4241 spin_unlock(&rbd_dev->lock_lists_lock);
4242 if (have_requests || delayed_work_pending(&rbd_dev->lock_dwork)) {
4243 dout("%s rbd_dev %p kicking lock_dwork\n", __func__, rbd_dev);
4244 mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0);
4245 }
4246}
4247
Ilya Dryomoved95b212016-08-12 16:40:02 +02004248static void rbd_handle_acquired_lock(struct rbd_device *rbd_dev, u8 struct_v,
4249 void **p)
4250{
4251 struct rbd_client_id cid = { 0 };
4252
4253 if (struct_v >= 2) {
4254 cid.gid = ceph_decode_64(p);
4255 cid.handle = ceph_decode_64(p);
4256 }
4257
4258 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
4259 cid.handle);
4260 if (!rbd_cid_equal(&cid, &rbd_empty_cid)) {
4261 down_write(&rbd_dev->lock_rwsem);
4262 if (rbd_cid_equal(&cid, &rbd_dev->owner_cid)) {
4263 /*
4264 * we already know that the remote client is
4265 * the owner
4266 */
4267 up_write(&rbd_dev->lock_rwsem);
4268 return;
4269 }
4270
4271 rbd_set_owner_cid(rbd_dev, &cid);
4272 downgrade_write(&rbd_dev->lock_rwsem);
4273 } else {
4274 down_read(&rbd_dev->lock_rwsem);
4275 }
4276
Ilya Dryomov637cd062019-06-06 17:14:49 +02004277 maybe_kick_acquire(rbd_dev);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004278 up_read(&rbd_dev->lock_rwsem);
4279}
4280
4281static void rbd_handle_released_lock(struct rbd_device *rbd_dev, u8 struct_v,
4282 void **p)
4283{
4284 struct rbd_client_id cid = { 0 };
4285
4286 if (struct_v >= 2) {
4287 cid.gid = ceph_decode_64(p);
4288 cid.handle = ceph_decode_64(p);
4289 }
4290
4291 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
4292 cid.handle);
4293 if (!rbd_cid_equal(&cid, &rbd_empty_cid)) {
4294 down_write(&rbd_dev->lock_rwsem);
4295 if (!rbd_cid_equal(&cid, &rbd_dev->owner_cid)) {
4296 dout("%s rbd_dev %p unexpected owner, cid %llu-%llu != owner_cid %llu-%llu\n",
4297 __func__, rbd_dev, cid.gid, cid.handle,
4298 rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle);
4299 up_write(&rbd_dev->lock_rwsem);
4300 return;
4301 }
4302
4303 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
4304 downgrade_write(&rbd_dev->lock_rwsem);
4305 } else {
4306 down_read(&rbd_dev->lock_rwsem);
4307 }
4308
Ilya Dryomov637cd062019-06-06 17:14:49 +02004309 maybe_kick_acquire(rbd_dev);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004310 up_read(&rbd_dev->lock_rwsem);
4311}
4312
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02004313/*
4314 * Returns result for ResponseMessage to be encoded (<= 0), or 1 if no
4315 * ResponseMessage is needed.
4316 */
4317static int rbd_handle_request_lock(struct rbd_device *rbd_dev, u8 struct_v,
4318 void **p)
Ilya Dryomoved95b212016-08-12 16:40:02 +02004319{
4320 struct rbd_client_id my_cid = rbd_get_cid(rbd_dev);
4321 struct rbd_client_id cid = { 0 };
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02004322 int result = 1;
Ilya Dryomoved95b212016-08-12 16:40:02 +02004323
4324 if (struct_v >= 2) {
4325 cid.gid = ceph_decode_64(p);
4326 cid.handle = ceph_decode_64(p);
4327 }
4328
4329 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
4330 cid.handle);
4331 if (rbd_cid_equal(&cid, &my_cid))
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02004332 return result;
Ilya Dryomoved95b212016-08-12 16:40:02 +02004333
4334 down_read(&rbd_dev->lock_rwsem);
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02004335 if (__rbd_is_lock_owner(rbd_dev)) {
4336 if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED &&
4337 rbd_cid_equal(&rbd_dev->owner_cid, &rbd_empty_cid))
4338 goto out_unlock;
4339
4340 /*
4341 * encode ResponseMessage(0) so the peer can detect
4342 * a missing owner
4343 */
4344 result = 0;
4345
4346 if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) {
Ilya Dryomove010dd02017-04-13 12:17:39 +02004347 if (!rbd_dev->opts->exclusive) {
4348 dout("%s rbd_dev %p queueing unlock_work\n",
4349 __func__, rbd_dev);
4350 queue_work(rbd_dev->task_wq,
4351 &rbd_dev->unlock_work);
4352 } else {
4353 /* refuse to release the lock */
4354 result = -EROFS;
4355 }
Ilya Dryomoved95b212016-08-12 16:40:02 +02004356 }
4357 }
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02004358
4359out_unlock:
Ilya Dryomoved95b212016-08-12 16:40:02 +02004360 up_read(&rbd_dev->lock_rwsem);
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02004361 return result;
Ilya Dryomoved95b212016-08-12 16:40:02 +02004362}
4363
4364static void __rbd_acknowledge_notify(struct rbd_device *rbd_dev,
4365 u64 notify_id, u64 cookie, s32 *result)
4366{
4367 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Kyle Spiers08a79102018-03-17 09:44:01 -07004368 char buf[4 + CEPH_ENCODING_START_BLK_LEN];
4369 int buf_size = sizeof(buf);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004370 int ret;
4371
4372 if (result) {
4373 void *p = buf;
4374
4375 /* encode ResponseMessage */
4376 ceph_start_encoding(&p, 1, 1,
4377 buf_size - CEPH_ENCODING_START_BLK_LEN);
4378 ceph_encode_32(&p, *result);
4379 } else {
4380 buf_size = 0;
4381 }
4382
4383 ret = ceph_osdc_notify_ack(osdc, &rbd_dev->header_oid,
4384 &rbd_dev->header_oloc, notify_id, cookie,
4385 buf, buf_size);
4386 if (ret)
4387 rbd_warn(rbd_dev, "acknowledge_notify failed: %d", ret);
4388}
4389
4390static void rbd_acknowledge_notify(struct rbd_device *rbd_dev, u64 notify_id,
4391 u64 cookie)
4392{
4393 dout("%s rbd_dev %p\n", __func__, rbd_dev);
4394 __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, NULL);
4395}
4396
4397static void rbd_acknowledge_notify_result(struct rbd_device *rbd_dev,
4398 u64 notify_id, u64 cookie, s32 result)
4399{
4400 dout("%s rbd_dev %p result %d\n", __func__, rbd_dev, result);
4401 __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, &result);
4402}
Ilya Dryomov922dab62016-05-26 01:15:02 +02004403
4404static void rbd_watch_cb(void *arg, u64 notify_id, u64 cookie,
4405 u64 notifier_id, void *data, size_t data_len)
Alex Elderb8d70032012-11-30 17:53:04 -06004406{
Ilya Dryomov922dab62016-05-26 01:15:02 +02004407 struct rbd_device *rbd_dev = arg;
Ilya Dryomoved95b212016-08-12 16:40:02 +02004408 void *p = data;
4409 void *const end = p + data_len;
Ilya Dryomovd4c22692016-09-06 11:15:48 +02004410 u8 struct_v = 0;
Ilya Dryomoved95b212016-08-12 16:40:02 +02004411 u32 len;
4412 u32 notify_op;
Alex Elderb8d70032012-11-30 17:53:04 -06004413 int ret;
4414
Ilya Dryomoved95b212016-08-12 16:40:02 +02004415 dout("%s rbd_dev %p cookie %llu notify_id %llu data_len %zu\n",
4416 __func__, rbd_dev, cookie, notify_id, data_len);
4417 if (data_len) {
4418 ret = ceph_start_decoding(&p, end, 1, "NotifyMessage",
4419 &struct_v, &len);
4420 if (ret) {
4421 rbd_warn(rbd_dev, "failed to decode NotifyMessage: %d",
4422 ret);
4423 return;
4424 }
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04004425
Ilya Dryomoved95b212016-08-12 16:40:02 +02004426 notify_op = ceph_decode_32(&p);
4427 } else {
4428 /* legacy notification for header updates */
4429 notify_op = RBD_NOTIFY_OP_HEADER_UPDATE;
4430 len = 0;
4431 }
Alex Elderb8d70032012-11-30 17:53:04 -06004432
Ilya Dryomoved95b212016-08-12 16:40:02 +02004433 dout("%s rbd_dev %p notify_op %u\n", __func__, rbd_dev, notify_op);
4434 switch (notify_op) {
4435 case RBD_NOTIFY_OP_ACQUIRED_LOCK:
4436 rbd_handle_acquired_lock(rbd_dev, struct_v, &p);
4437 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
4438 break;
4439 case RBD_NOTIFY_OP_RELEASED_LOCK:
4440 rbd_handle_released_lock(rbd_dev, struct_v, &p);
4441 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
4442 break;
4443 case RBD_NOTIFY_OP_REQUEST_LOCK:
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02004444 ret = rbd_handle_request_lock(rbd_dev, struct_v, &p);
4445 if (ret <= 0)
Ilya Dryomoved95b212016-08-12 16:40:02 +02004446 rbd_acknowledge_notify_result(rbd_dev, notify_id,
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02004447 cookie, ret);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004448 else
4449 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
4450 break;
4451 case RBD_NOTIFY_OP_HEADER_UPDATE:
4452 ret = rbd_dev_refresh(rbd_dev);
4453 if (ret)
4454 rbd_warn(rbd_dev, "refresh failed: %d", ret);
4455
4456 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
4457 break;
4458 default:
4459 if (rbd_is_lock_owner(rbd_dev))
4460 rbd_acknowledge_notify_result(rbd_dev, notify_id,
4461 cookie, -EOPNOTSUPP);
4462 else
4463 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
4464 break;
4465 }
Alex Elderb8d70032012-11-30 17:53:04 -06004466}
4467
Ilya Dryomov99d16942016-08-12 16:11:41 +02004468static void __rbd_unregister_watch(struct rbd_device *rbd_dev);
4469
Ilya Dryomov922dab62016-05-26 01:15:02 +02004470static void rbd_watch_errcb(void *arg, u64 cookie, int err)
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04004471{
Ilya Dryomov922dab62016-05-26 01:15:02 +02004472 struct rbd_device *rbd_dev = arg;
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04004473
Ilya Dryomov922dab62016-05-26 01:15:02 +02004474 rbd_warn(rbd_dev, "encountered watch error: %d", err);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04004475
Ilya Dryomoved95b212016-08-12 16:40:02 +02004476 down_write(&rbd_dev->lock_rwsem);
4477 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
4478 up_write(&rbd_dev->lock_rwsem);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04004479
Ilya Dryomov99d16942016-08-12 16:11:41 +02004480 mutex_lock(&rbd_dev->watch_mutex);
4481 if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) {
4482 __rbd_unregister_watch(rbd_dev);
4483 rbd_dev->watch_state = RBD_WATCH_STATE_ERROR;
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04004484
Ilya Dryomov99d16942016-08-12 16:11:41 +02004485 queue_delayed_work(rbd_dev->task_wq, &rbd_dev->watch_dwork, 0);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04004486 }
Ilya Dryomov99d16942016-08-12 16:11:41 +02004487 mutex_unlock(&rbd_dev->watch_mutex);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04004488}
4489
4490/*
Ilya Dryomov99d16942016-08-12 16:11:41 +02004491 * watch_mutex must be locked
Alex Elder9969ebc2013-01-18 12:31:10 -06004492 */
Ilya Dryomov99d16942016-08-12 16:11:41 +02004493static int __rbd_register_watch(struct rbd_device *rbd_dev)
Alex Elder9969ebc2013-01-18 12:31:10 -06004494{
4495 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Ilya Dryomov922dab62016-05-26 01:15:02 +02004496 struct ceph_osd_linger_request *handle;
Alex Elder9969ebc2013-01-18 12:31:10 -06004497
Ilya Dryomov922dab62016-05-26 01:15:02 +02004498 rbd_assert(!rbd_dev->watch_handle);
Ilya Dryomov99d16942016-08-12 16:11:41 +02004499 dout("%s rbd_dev %p\n", __func__, rbd_dev);
Alex Elder9969ebc2013-01-18 12:31:10 -06004500
Ilya Dryomov922dab62016-05-26 01:15:02 +02004501 handle = ceph_osdc_watch(osdc, &rbd_dev->header_oid,
4502 &rbd_dev->header_oloc, rbd_watch_cb,
4503 rbd_watch_errcb, rbd_dev);
4504 if (IS_ERR(handle))
4505 return PTR_ERR(handle);
Alex Elder9969ebc2013-01-18 12:31:10 -06004506
Ilya Dryomov922dab62016-05-26 01:15:02 +02004507 rbd_dev->watch_handle = handle;
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04004508 return 0;
Alex Elder9969ebc2013-01-18 12:31:10 -06004509}
4510
Ilya Dryomov99d16942016-08-12 16:11:41 +02004511/*
4512 * watch_mutex must be locked
4513 */
4514static void __rbd_unregister_watch(struct rbd_device *rbd_dev)
Ilya Dryomovfca27062013-12-16 18:02:40 +02004515{
Ilya Dryomov922dab62016-05-26 01:15:02 +02004516 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
4517 int ret;
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04004518
Ilya Dryomov99d16942016-08-12 16:11:41 +02004519 rbd_assert(rbd_dev->watch_handle);
4520 dout("%s rbd_dev %p\n", __func__, rbd_dev);
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04004521
Ilya Dryomov922dab62016-05-26 01:15:02 +02004522 ret = ceph_osdc_unwatch(osdc, rbd_dev->watch_handle);
4523 if (ret)
4524 rbd_warn(rbd_dev, "failed to unwatch: %d", ret);
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04004525
Ilya Dryomov922dab62016-05-26 01:15:02 +02004526 rbd_dev->watch_handle = NULL;
Ilya Dryomovc525f032016-04-28 16:07:26 +02004527}
4528
Ilya Dryomov99d16942016-08-12 16:11:41 +02004529static int rbd_register_watch(struct rbd_device *rbd_dev)
Ilya Dryomovc525f032016-04-28 16:07:26 +02004530{
Ilya Dryomov99d16942016-08-12 16:11:41 +02004531 int ret;
Ilya Dryomov811c6682016-04-15 16:22:16 +02004532
Ilya Dryomov99d16942016-08-12 16:11:41 +02004533 mutex_lock(&rbd_dev->watch_mutex);
4534 rbd_assert(rbd_dev->watch_state == RBD_WATCH_STATE_UNREGISTERED);
4535 ret = __rbd_register_watch(rbd_dev);
4536 if (ret)
4537 goto out;
4538
4539 rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED;
4540 rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id;
4541
4542out:
4543 mutex_unlock(&rbd_dev->watch_mutex);
4544 return ret;
4545}
4546
4547static void cancel_tasks_sync(struct rbd_device *rbd_dev)
4548{
4549 dout("%s rbd_dev %p\n", __func__, rbd_dev);
4550
Ilya Dryomoved95b212016-08-12 16:40:02 +02004551 cancel_work_sync(&rbd_dev->acquired_lock_work);
4552 cancel_work_sync(&rbd_dev->released_lock_work);
4553 cancel_delayed_work_sync(&rbd_dev->lock_dwork);
4554 cancel_work_sync(&rbd_dev->unlock_work);
Ilya Dryomov99d16942016-08-12 16:11:41 +02004555}
4556
4557static void rbd_unregister_watch(struct rbd_device *rbd_dev)
4558{
4559 cancel_tasks_sync(rbd_dev);
4560
4561 mutex_lock(&rbd_dev->watch_mutex);
4562 if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED)
4563 __rbd_unregister_watch(rbd_dev);
4564 rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED;
4565 mutex_unlock(&rbd_dev->watch_mutex);
4566
Dongsheng Yang23edca82018-06-04 06:24:37 -04004567 cancel_delayed_work_sync(&rbd_dev->watch_dwork);
Ilya Dryomov811c6682016-04-15 16:22:16 +02004568 ceph_osdc_flush_notifies(&rbd_dev->rbd_client->client->osdc);
Ilya Dryomovfca27062013-12-16 18:02:40 +02004569}
4570
Ilya Dryomov14bb2112017-04-13 12:17:38 +02004571/*
4572 * lock_rwsem must be held for write
4573 */
4574static void rbd_reacquire_lock(struct rbd_device *rbd_dev)
4575{
4576 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
4577 char cookie[32];
4578 int ret;
4579
Ilya Dryomova2b1da02019-05-30 11:15:23 +02004580 if (!rbd_quiesce_lock(rbd_dev))
4581 return;
Ilya Dryomov14bb2112017-04-13 12:17:38 +02004582
4583 format_lock_cookie(rbd_dev, cookie);
4584 ret = ceph_cls_set_cookie(osdc, &rbd_dev->header_oid,
4585 &rbd_dev->header_oloc, RBD_LOCK_NAME,
4586 CEPH_CLS_LOCK_EXCLUSIVE, rbd_dev->lock_cookie,
4587 RBD_LOCK_TAG, cookie);
4588 if (ret) {
4589 if (ret != -EOPNOTSUPP)
4590 rbd_warn(rbd_dev, "failed to update lock cookie: %d",
4591 ret);
4592
4593 /*
4594 * Lock cookie cannot be updated on older OSDs, so do
4595 * a manual release and queue an acquire.
4596 */
Ilya Dryomove1fddc82019-05-30 16:07:48 +02004597 __rbd_release_lock(rbd_dev);
Ilya Dryomova2b1da02019-05-30 11:15:23 +02004598 queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0);
Ilya Dryomov14bb2112017-04-13 12:17:38 +02004599 } else {
Florian Margaineedd8ca82017-12-13 16:43:59 +01004600 __rbd_lock(rbd_dev, cookie);
Ilya Dryomov637cd062019-06-06 17:14:49 +02004601 wake_lock_waiters(rbd_dev, 0);
Ilya Dryomov14bb2112017-04-13 12:17:38 +02004602 }
4603}
4604
Ilya Dryomov99d16942016-08-12 16:11:41 +02004605static void rbd_reregister_watch(struct work_struct *work)
4606{
4607 struct rbd_device *rbd_dev = container_of(to_delayed_work(work),
4608 struct rbd_device, watch_dwork);
4609 int ret;
4610
4611 dout("%s rbd_dev %p\n", __func__, rbd_dev);
4612
4613 mutex_lock(&rbd_dev->watch_mutex);
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02004614 if (rbd_dev->watch_state != RBD_WATCH_STATE_ERROR) {
4615 mutex_unlock(&rbd_dev->watch_mutex);
Ilya Dryomov14bb2112017-04-13 12:17:38 +02004616 return;
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02004617 }
Ilya Dryomov99d16942016-08-12 16:11:41 +02004618
4619 ret = __rbd_register_watch(rbd_dev);
4620 if (ret) {
4621 rbd_warn(rbd_dev, "failed to reregister watch: %d", ret);
Ilya Dryomov637cd062019-06-06 17:14:49 +02004622 if (ret != -EBLACKLISTED && ret != -ENOENT) {
Ilya Dryomov99d16942016-08-12 16:11:41 +02004623 queue_delayed_work(rbd_dev->task_wq,
4624 &rbd_dev->watch_dwork,
4625 RBD_RETRY_DELAY);
Ilya Dryomov637cd062019-06-06 17:14:49 +02004626 mutex_unlock(&rbd_dev->watch_mutex);
4627 return;
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02004628 }
Ilya Dryomov637cd062019-06-06 17:14:49 +02004629
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02004630 mutex_unlock(&rbd_dev->watch_mutex);
Ilya Dryomov637cd062019-06-06 17:14:49 +02004631 down_write(&rbd_dev->lock_rwsem);
4632 wake_lock_waiters(rbd_dev, ret);
4633 up_write(&rbd_dev->lock_rwsem);
Ilya Dryomov14bb2112017-04-13 12:17:38 +02004634 return;
Ilya Dryomov99d16942016-08-12 16:11:41 +02004635 }
4636
4637 rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED;
4638 rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id;
4639 mutex_unlock(&rbd_dev->watch_mutex);
4640
Ilya Dryomov14bb2112017-04-13 12:17:38 +02004641 down_write(&rbd_dev->lock_rwsem);
4642 if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED)
4643 rbd_reacquire_lock(rbd_dev);
4644 up_write(&rbd_dev->lock_rwsem);
4645
Ilya Dryomov99d16942016-08-12 16:11:41 +02004646 ret = rbd_dev_refresh(rbd_dev);
4647 if (ret)
Colin Ian Kingf6870cc2018-03-19 13:33:10 +00004648 rbd_warn(rbd_dev, "reregistration refresh failed: %d", ret);
Ilya Dryomov99d16942016-08-12 16:11:41 +02004649}
4650
Alex Elder36be9a72013-01-19 00:30:28 -06004651/*
Alex Elderf40eb342013-04-25 15:09:42 -05004652 * Synchronous osd object method call. Returns the number of bytes
4653 * returned in the outbound buffer, or a negative error code.
Alex Elder36be9a72013-01-19 00:30:28 -06004654 */
4655static int rbd_obj_method_sync(struct rbd_device *rbd_dev,
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004656 struct ceph_object_id *oid,
4657 struct ceph_object_locator *oloc,
Alex Elder36be9a72013-01-19 00:30:28 -06004658 const char *method_name,
Alex Elder41579762013-04-21 12:14:45 -05004659 const void *outbound,
Alex Elder36be9a72013-01-19 00:30:28 -06004660 size_t outbound_size,
Alex Elder41579762013-04-21 12:14:45 -05004661 void *inbound,
Alex Eldere2a58ee2013-04-30 00:44:33 -05004662 size_t inbound_size)
Alex Elder36be9a72013-01-19 00:30:28 -06004663{
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004664 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
4665 struct page *req_page = NULL;
4666 struct page *reply_page;
Alex Elder36be9a72013-01-19 00:30:28 -06004667 int ret;
4668
4669 /*
Alex Elder6010a452013-04-05 01:27:11 -05004670 * Method calls are ultimately read operations. The result
4671 * should placed into the inbound buffer provided. They
4672 * also supply outbound data--parameters for the object
4673 * method. Currently if this is present it will be a
4674 * snapshot id.
Alex Elder36be9a72013-01-19 00:30:28 -06004675 */
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004676 if (outbound) {
4677 if (outbound_size > PAGE_SIZE)
4678 return -E2BIG;
Alex Elder36be9a72013-01-19 00:30:28 -06004679
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004680 req_page = alloc_page(GFP_KERNEL);
4681 if (!req_page)
4682 return -ENOMEM;
Alex Elder36be9a72013-01-19 00:30:28 -06004683
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004684 memcpy(page_address(req_page), outbound, outbound_size);
Alex Elder04017e22013-04-05 14:46:02 -05004685 }
Alex Elder430c28c2013-04-03 21:32:51 -05004686
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004687 reply_page = alloc_page(GFP_KERNEL);
4688 if (!reply_page) {
4689 if (req_page)
4690 __free_page(req_page);
4691 return -ENOMEM;
4692 }
Alex Elder36be9a72013-01-19 00:30:28 -06004693
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004694 ret = ceph_osdc_call(osdc, oid, oloc, RBD_DRV_NAME, method_name,
4695 CEPH_OSD_FLAG_READ, req_page, outbound_size,
Ilya Dryomov68ada912019-06-14 18:16:51 +02004696 &reply_page, &inbound_size);
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004697 if (!ret) {
4698 memcpy(inbound, page_address(reply_page), inbound_size);
4699 ret = inbound_size;
4700 }
Alex Elder57385b52013-04-21 12:14:45 -05004701
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004702 if (req_page)
4703 __free_page(req_page);
4704 __free_page(reply_page);
Alex Elder36be9a72013-01-19 00:30:28 -06004705 return ret;
4706}
4707
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004708static void rbd_queue_workfn(struct work_struct *work)
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004709{
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004710 struct request *rq = blk_mq_rq_from_pdu(work);
4711 struct rbd_device *rbd_dev = rq->q->queuedata;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004712 struct rbd_img_request *img_request;
Josh Durgin4e752f02014-04-08 11:12:11 -07004713 struct ceph_snap_context *snapc = NULL;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004714 u64 offset = (u64)blk_rq_pos(rq) << SECTOR_SHIFT;
4715 u64 length = blk_rq_bytes(rq);
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004716 enum obj_operation_type op_type;
Josh Durgin4e752f02014-04-08 11:12:11 -07004717 u64 mapping_size;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004718 int result;
4719
Christoph Hellwigaebf5262017-01-31 16:57:31 +01004720 switch (req_op(rq)) {
4721 case REQ_OP_DISCARD:
4722 op_type = OBJ_OP_DISCARD;
4723 break;
Ilya Dryomov6484cbe2019-01-29 12:46:25 +01004724 case REQ_OP_WRITE_ZEROES:
4725 op_type = OBJ_OP_ZEROOUT;
4726 break;
Christoph Hellwigaebf5262017-01-31 16:57:31 +01004727 case REQ_OP_WRITE:
4728 op_type = OBJ_OP_WRITE;
4729 break;
4730 case REQ_OP_READ:
4731 op_type = OBJ_OP_READ;
4732 break;
4733 default:
4734 dout("%s: non-fs request type %d\n", __func__, req_op(rq));
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004735 result = -EIO;
4736 goto err;
4737 }
4738
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004739 /* Ignore/skip any zero-length requests */
4740
4741 if (!length) {
4742 dout("%s: zero-length request\n", __func__);
4743 result = 0;
4744 goto err_rq;
4745 }
4746
Ilya Dryomovb948ad72019-11-08 15:26:51 +01004747 if (op_type != OBJ_OP_READ) {
4748 if (rbd_is_ro(rbd_dev)) {
4749 rbd_warn(rbd_dev, "%s on read-only mapping",
4750 obj_op_name(op_type));
4751 result = -EIO;
4752 goto err;
4753 }
4754 rbd_assert(!rbd_is_snap(rbd_dev));
Ilya Dryomovb91a7bd2019-05-03 17:27:03 +02004755 }
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004756
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004757 if (offset && length > U64_MAX - offset + 1) {
4758 rbd_warn(rbd_dev, "bad request range (%llu~%llu)", offset,
4759 length);
4760 result = -EINVAL;
4761 goto err_rq; /* Shouldn't happen */
4762 }
4763
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004764 blk_mq_start_request(rq);
4765
Josh Durgin4e752f02014-04-08 11:12:11 -07004766 down_read(&rbd_dev->header_rwsem);
4767 mapping_size = rbd_dev->mapping.size;
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004768 if (op_type != OBJ_OP_READ) {
Josh Durgin4e752f02014-04-08 11:12:11 -07004769 snapc = rbd_dev->header.snapc;
4770 ceph_get_snap_context(snapc);
4771 }
4772 up_read(&rbd_dev->header_rwsem);
4773
4774 if (offset + length > mapping_size) {
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004775 rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)", offset,
Josh Durgin4e752f02014-04-08 11:12:11 -07004776 length, mapping_size);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004777 result = -EIO;
4778 goto err_rq;
4779 }
4780
Ilya Dryomovdfd98752018-02-06 19:26:35 +01004781 img_request = rbd_img_request_create(rbd_dev, op_type, snapc);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004782 if (!img_request) {
4783 result = -ENOMEM;
Ilya Dryomov637cd062019-06-06 17:14:49 +02004784 goto err_rq;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004785 }
4786 img_request->rq = rq;
Ilya Dryomov70b16db2015-11-27 19:23:24 +01004787 snapc = NULL; /* img_request consumes a ref */
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004788
Ilya Dryomov21ed05a2019-08-30 17:31:06 +02004789 dout("%s rbd_dev %p img_req %p %s %llu~%llu\n", __func__, rbd_dev,
4790 img_request, obj_op_name(op_type), offset, length);
4791
Ilya Dryomov6484cbe2019-01-29 12:46:25 +01004792 if (op_type == OBJ_OP_DISCARD || op_type == OBJ_OP_ZEROOUT)
Ilya Dryomov5a237812018-02-06 19:26:34 +01004793 result = rbd_img_fill_nodata(img_request, offset, length);
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004794 else
Ilya Dryomov5a237812018-02-06 19:26:34 +01004795 result = rbd_img_fill_from_bio(img_request, offset, length,
4796 rq->bio);
Ilya Dryomov0192ce22019-05-16 15:06:56 +02004797 if (result)
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004798 goto err_img_request;
4799
Ilya Dryomove1fddc82019-05-30 16:07:48 +02004800 rbd_img_handle_request(img_request, 0);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004801 return;
4802
4803err_img_request:
4804 rbd_img_request_put(img_request);
4805err_rq:
4806 if (result)
4807 rbd_warn(rbd_dev, "%s %llx at %llx result %d",
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08004808 obj_op_name(op_type), length, offset, result);
SF Markus Elfringe96a6502014-11-02 15:20:59 +01004809 ceph_put_snap_context(snapc);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004810err:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02004811 blk_mq_end_request(rq, errno_to_blk_status(result));
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004812}
4813
Christoph Hellwigfc17b652017-06-03 09:38:05 +02004814static blk_status_t rbd_queue_rq(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004815 const struct blk_mq_queue_data *bd)
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004816{
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004817 struct request *rq = bd->rq;
4818 struct work_struct *work = blk_mq_rq_to_pdu(rq);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04004819
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004820 queue_work(rbd_wq, work);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02004821 return BLK_STS_OK;
Alex Elderbf0d5f502012-11-22 00:00:08 -06004822}
4823
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004824static void rbd_free_disk(struct rbd_device *rbd_dev)
4825{
Ilya Dryomov5769ed02017-04-13 12:17:38 +02004826 blk_cleanup_queue(rbd_dev->disk->queue);
4827 blk_mq_free_tag_set(&rbd_dev->tag_set);
4828 put_disk(rbd_dev->disk);
Alex Eldera0cab922013-04-25 23:15:08 -05004829 rbd_dev->disk = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004830}
4831
Alex Elder788e2df2013-01-17 12:25:27 -06004832static int rbd_obj_read_sync(struct rbd_device *rbd_dev,
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004833 struct ceph_object_id *oid,
4834 struct ceph_object_locator *oloc,
4835 void *buf, int buf_len)
Alex Elder788e2df2013-01-17 12:25:27 -06004836
4837{
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004838 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
4839 struct ceph_osd_request *req;
4840 struct page **pages;
4841 int num_pages = calc_pages_for(0, buf_len);
Alex Elder788e2df2013-01-17 12:25:27 -06004842 int ret;
4843
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004844 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL);
4845 if (!req)
4846 return -ENOMEM;
Alex Elder788e2df2013-01-17 12:25:27 -06004847
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004848 ceph_oid_copy(&req->r_base_oid, oid);
4849 ceph_oloc_copy(&req->r_base_oloc, oloc);
4850 req->r_flags = CEPH_OSD_FLAG_READ;
Alex Elder788e2df2013-01-17 12:25:27 -06004851
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004852 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
4853 if (IS_ERR(pages)) {
4854 ret = PTR_ERR(pages);
4855 goto out_req;
4856 }
Alex Elder1ceae7e2013-02-06 13:11:38 -06004857
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004858 osd_req_op_extent_init(req, 0, CEPH_OSD_OP_READ, 0, buf_len, 0, 0);
4859 osd_req_op_extent_osd_data_pages(req, 0, pages, buf_len, 0, false,
4860 true);
Alex Elder788e2df2013-01-17 12:25:27 -06004861
Ilya Dryomov26f887e2018-10-15 16:11:37 +02004862 ret = ceph_osdc_alloc_messages(req, GFP_KERNEL);
4863 if (ret)
4864 goto out_req;
4865
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004866 ceph_osdc_start_request(osdc, req, false);
4867 ret = ceph_osdc_wait_request(osdc, req);
4868 if (ret >= 0)
4869 ceph_copy_from_page_vector(pages, buf, 0, ret);
4870
4871out_req:
4872 ceph_osdc_put_request(req);
Alex Elder788e2df2013-01-17 12:25:27 -06004873 return ret;
4874}
4875
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004876/*
Alex Elder662518b2013-05-06 09:51:29 -05004877 * Read the complete header for the given rbd device. On successful
4878 * return, the rbd_dev->header field will contain up-to-date
4879 * information about the image.
Alex Elder4156d992012-08-02 11:29:46 -05004880 */
Alex Elder99a41eb2013-05-06 09:51:30 -05004881static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev)
Alex Elder4156d992012-08-02 11:29:46 -05004882{
4883 struct rbd_image_header_ondisk *ondisk = NULL;
4884 u32 snap_count = 0;
4885 u64 names_size = 0;
4886 u32 want_count;
4887 int ret;
4888
4889 /*
4890 * The complete header will include an array of its 64-bit
4891 * snapshot ids, followed by the names of those snapshots as
4892 * a contiguous block of NUL-terminated strings. Note that
4893 * the number of snapshots could change by the time we read
4894 * it in, in which case we re-read it.
4895 */
4896 do {
4897 size_t size;
4898
4899 kfree(ondisk);
4900
4901 size = sizeof (*ondisk);
4902 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
4903 size += names_size;
4904 ondisk = kmalloc(size, GFP_KERNEL);
4905 if (!ondisk)
Alex Elder662518b2013-05-06 09:51:29 -05004906 return -ENOMEM;
Alex Elder4156d992012-08-02 11:29:46 -05004907
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004908 ret = rbd_obj_read_sync(rbd_dev, &rbd_dev->header_oid,
4909 &rbd_dev->header_oloc, ondisk, size);
Alex Elder4156d992012-08-02 11:29:46 -05004910 if (ret < 0)
Alex Elder662518b2013-05-06 09:51:29 -05004911 goto out;
Alex Elderc0cd10db2013-04-26 09:43:47 -05004912 if ((size_t)ret < size) {
Alex Elder4156d992012-08-02 11:29:46 -05004913 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05004914 rbd_warn(rbd_dev, "short header read (want %zd got %d)",
4915 size, ret);
Alex Elder662518b2013-05-06 09:51:29 -05004916 goto out;
Alex Elder4156d992012-08-02 11:29:46 -05004917 }
4918 if (!rbd_dev_ondisk_valid(ondisk)) {
4919 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05004920 rbd_warn(rbd_dev, "invalid header");
Alex Elder662518b2013-05-06 09:51:29 -05004921 goto out;
Alex Elder4156d992012-08-02 11:29:46 -05004922 }
4923
4924 names_size = le64_to_cpu(ondisk->snap_names_len);
4925 want_count = snap_count;
4926 snap_count = le32_to_cpu(ondisk->snap_count);
4927 } while (snap_count != want_count);
4928
Alex Elder662518b2013-05-06 09:51:29 -05004929 ret = rbd_header_from_disk(rbd_dev, ondisk);
4930out:
Alex Elder4156d992012-08-02 11:29:46 -05004931 kfree(ondisk);
4932
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004933 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004934}
4935
Josh Durgin98752012013-08-29 17:26:31 -07004936static void rbd_dev_update_size(struct rbd_device *rbd_dev)
4937{
4938 sector_t size;
Josh Durgin98752012013-08-29 17:26:31 -07004939
4940 /*
Ilya Dryomov811c6682016-04-15 16:22:16 +02004941 * If EXISTS is not set, rbd_dev->disk may be NULL, so don't
4942 * try to update its size. If REMOVING is set, updating size
4943 * is just useless work since the device can't be opened.
Josh Durgin98752012013-08-29 17:26:31 -07004944 */
Ilya Dryomov811c6682016-04-15 16:22:16 +02004945 if (test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags) &&
4946 !test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) {
Josh Durgin98752012013-08-29 17:26:31 -07004947 size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE;
4948 dout("setting size to %llu sectors", (unsigned long long)size);
4949 set_capacity(rbd_dev->disk, size);
4950 revalidate_disk(rbd_dev->disk);
4951 }
4952}
4953
Alex Eldercc4a38bd2013-04-30 00:44:33 -05004954static int rbd_dev_refresh(struct rbd_device *rbd_dev)
Alex Elder1fe5e992012-07-25 09:32:41 -05004955{
Alex Eldere627db02013-05-06 07:40:30 -05004956 u64 mapping_size;
Alex Elder1fe5e992012-07-25 09:32:41 -05004957 int ret;
4958
Alex Eldercfbf6372013-05-31 17:40:45 -05004959 down_write(&rbd_dev->header_rwsem);
Alex Elder3b5cf2a2013-05-29 11:18:59 -05004960 mapping_size = rbd_dev->mapping.size;
Ilya Dryomova720ae02014-07-23 17:11:19 +04004961
4962 ret = rbd_dev_header_info(rbd_dev);
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04004963 if (ret)
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004964 goto out;
Alex Elder15228ed2013-05-01 12:43:03 -05004965
Ilya Dryomove8f59b52014-07-24 10:42:13 +04004966 /*
4967 * If there is a parent, see if it has disappeared due to the
4968 * mapped image getting flattened.
4969 */
4970 if (rbd_dev->parent) {
4971 ret = rbd_dev_v2_parent_info(rbd_dev);
4972 if (ret)
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004973 goto out;
Ilya Dryomove8f59b52014-07-24 10:42:13 +04004974 }
4975
Ilya Dryomov686238b2019-11-18 12:51:02 +01004976 rbd_assert(!rbd_is_snap(rbd_dev));
4977 rbd_dev->mapping.size = rbd_dev->header.image_size;
Alex Elder15228ed2013-05-01 12:43:03 -05004978
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004979out:
Alex Eldercfbf6372013-05-31 17:40:45 -05004980 up_write(&rbd_dev->header_rwsem);
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004981 if (!ret && mapping_size != rbd_dev->mapping.size)
Josh Durgin98752012013-08-29 17:26:31 -07004982 rbd_dev_update_size(rbd_dev);
Alex Elder1fe5e992012-07-25 09:32:41 -05004983
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004984 return ret;
Alex Elder1fe5e992012-07-25 09:32:41 -05004985}
4986
Christoph Hellwigd6296d392017-05-01 10:19:08 -06004987static int rbd_init_request(struct blk_mq_tag_set *set, struct request *rq,
4988 unsigned int hctx_idx, unsigned int numa_node)
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004989{
4990 struct work_struct *work = blk_mq_rq_to_pdu(rq);
4991
4992 INIT_WORK(work, rbd_queue_workfn);
4993 return 0;
4994}
4995
Eric Biggersf363b082017-03-30 13:39:16 -07004996static const struct blk_mq_ops rbd_mq_ops = {
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004997 .queue_rq = rbd_queue_rq,
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004998 .init_request = rbd_init_request,
4999};
5000
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005001static int rbd_init_disk(struct rbd_device *rbd_dev)
5002{
5003 struct gendisk *disk;
5004 struct request_queue *q;
Ilya Dryomov420efbd2018-04-16 09:32:18 +02005005 unsigned int objset_bytes =
5006 rbd_dev->layout.object_size * rbd_dev->layout.stripe_count;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01005007 int err;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005008
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005009 /* create gendisk info */
Ilya Dryomov7e513d42013-12-16 19:26:32 +02005010 disk = alloc_disk(single_major ?
5011 (1 << RBD_SINGLE_MAJOR_PART_SHIFT) :
5012 RBD_MINORS_PER_MAJOR);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005013 if (!disk)
Alex Elder1fcdb8a2012-08-29 17:11:06 -05005014 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005015
Alex Elderf0f8cef2012-01-29 13:57:44 -06005016 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
Alex Elderde71a292012-07-03 16:01:19 -05005017 rbd_dev->dev_id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005018 disk->major = rbd_dev->major;
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02005019 disk->first_minor = rbd_dev->minor;
Ilya Dryomov7e513d42013-12-16 19:26:32 +02005020 if (single_major)
5021 disk->flags |= GENHD_FL_EXT_DEVT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005022 disk->fops = &rbd_bd_ops;
5023 disk->private_data = rbd_dev;
5024
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01005025 memset(&rbd_dev->tag_set, 0, sizeof(rbd_dev->tag_set));
5026 rbd_dev->tag_set.ops = &rbd_mq_ops;
Ilya Dryomovb5584182015-06-23 16:21:19 +03005027 rbd_dev->tag_set.queue_depth = rbd_dev->opts->queue_depth;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01005028 rbd_dev->tag_set.numa_node = NUMA_NO_NODE;
Ming Lei56d18f62019-02-15 19:13:24 +08005029 rbd_dev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01005030 rbd_dev->tag_set.nr_hw_queues = 1;
5031 rbd_dev->tag_set.cmd_size = sizeof(struct work_struct);
5032
5033 err = blk_mq_alloc_tag_set(&rbd_dev->tag_set);
5034 if (err)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005035 goto out_disk;
Josh Durgin029bcbd2011-07-22 11:35:23 -07005036
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01005037 q = blk_mq_init_queue(&rbd_dev->tag_set);
5038 if (IS_ERR(q)) {
5039 err = PTR_ERR(q);
5040 goto out_tag_set;
5041 }
5042
Bart Van Assche8b904b52018-03-07 17:10:10 -08005043 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
Ilya Dryomovd8a2c892015-03-24 16:15:17 +03005044 /* QUEUE_FLAG_ADD_RANDOM is off by default for blk-mq */
Alex Elder593a9e72012-02-07 12:03:37 -06005045
Ilya Dryomov420efbd2018-04-16 09:32:18 +02005046 blk_queue_max_hw_sectors(q, objset_bytes >> SECTOR_SHIFT);
Ilya Dryomov0d9fde42015-10-07 16:09:35 +02005047 q->limits.max_sectors = queue_max_hw_sectors(q);
Ilya Dryomov21acdf42017-12-21 15:35:11 +01005048 blk_queue_max_segments(q, USHRT_MAX);
Ilya Dryomov24f1df62018-01-12 17:22:10 +01005049 blk_queue_max_segment_size(q, UINT_MAX);
Ilya Dryomov16d80c52019-03-15 14:50:04 +01005050 blk_queue_io_min(q, rbd_dev->opts->alloc_size);
5051 blk_queue_io_opt(q, rbd_dev->opts->alloc_size);
Josh Durgin029bcbd2011-07-22 11:35:23 -07005052
Ilya Dryomovd9360542018-03-23 06:14:47 +01005053 if (rbd_dev->opts->trim) {
5054 blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
Ilya Dryomov16d80c52019-03-15 14:50:04 +01005055 q->limits.discard_granularity = rbd_dev->opts->alloc_size;
Ilya Dryomovd9360542018-03-23 06:14:47 +01005056 blk_queue_max_discard_sectors(q, objset_bytes >> SECTOR_SHIFT);
5057 blk_queue_max_write_zeroes_sectors(q, objset_bytes >> SECTOR_SHIFT);
5058 }
Guangliang Zhao90e98c52014-04-01 22:22:16 +08005059
Ronny Hegewaldbae818e2015-10-15 18:50:46 +00005060 if (!ceph_test_opt(rbd_dev->rbd_client->client, NOCRC))
Jan Karadc3b17c2017-02-02 15:56:50 +01005061 q->backing_dev_info->capabilities |= BDI_CAP_STABLE_WRITES;
Ronny Hegewaldbae818e2015-10-15 18:50:46 +00005062
Ilya Dryomov5769ed02017-04-13 12:17:38 +02005063 /*
5064 * disk_release() expects a queue ref from add_disk() and will
5065 * put it. Hold an extra ref until add_disk() is called.
5066 */
5067 WARN_ON(!blk_get_queue(q));
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005068 disk->queue = q;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005069 q->queuedata = rbd_dev;
5070
5071 rbd_dev->disk = disk;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005072
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005073 return 0;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01005074out_tag_set:
5075 blk_mq_free_tag_set(&rbd_dev->tag_set);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005076out_disk:
5077 put_disk(disk);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01005078 return err;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005079}
5080
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005081/*
5082 sysfs
5083*/
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005084
Alex Elder593a9e72012-02-07 12:03:37 -06005085static struct rbd_device *dev_to_rbd_dev(struct device *dev)
5086{
5087 return container_of(dev, struct rbd_device, dev);
5088}
5089
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005090static ssize_t rbd_size_show(struct device *dev,
5091 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005092{
Alex Elder593a9e72012-02-07 12:03:37 -06005093 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005094
Alex Elderfc71d832013-04-26 15:44:36 -05005095 return sprintf(buf, "%llu\n",
5096 (unsigned long long)rbd_dev->mapping.size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005097}
5098
Alex Elder34b13182012-07-13 20:35:12 -05005099static ssize_t rbd_features_show(struct device *dev,
5100 struct device_attribute *attr, char *buf)
5101{
5102 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
5103
Ilya Dryomovfa58bca2019-11-05 13:16:52 +01005104 return sprintf(buf, "0x%016llx\n", rbd_dev->header.features);
Alex Elder34b13182012-07-13 20:35:12 -05005105}
5106
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005107static ssize_t rbd_major_show(struct device *dev,
5108 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005109{
Alex Elder593a9e72012-02-07 12:03:37 -06005110 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005111
Alex Elderfc71d832013-04-26 15:44:36 -05005112 if (rbd_dev->major)
5113 return sprintf(buf, "%d\n", rbd_dev->major);
5114
5115 return sprintf(buf, "(none)\n");
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02005116}
Alex Elderfc71d832013-04-26 15:44:36 -05005117
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02005118static ssize_t rbd_minor_show(struct device *dev,
5119 struct device_attribute *attr, char *buf)
5120{
5121 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
5122
5123 return sprintf(buf, "%d\n", rbd_dev->minor);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005124}
5125
Ilya Dryomov005a07bf2016-08-18 18:38:43 +02005126static ssize_t rbd_client_addr_show(struct device *dev,
5127 struct device_attribute *attr, char *buf)
5128{
5129 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
5130 struct ceph_entity_addr *client_addr =
5131 ceph_client_addr(rbd_dev->rbd_client->client);
5132
5133 return sprintf(buf, "%pISpc/%u\n", &client_addr->in_addr,
5134 le32_to_cpu(client_addr->nonce));
5135}
5136
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005137static ssize_t rbd_client_id_show(struct device *dev,
5138 struct device_attribute *attr, char *buf)
5139{
Alex Elder593a9e72012-02-07 12:03:37 -06005140 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005141
Alex Elder1dbb4392012-01-24 10:08:37 -06005142 return sprintf(buf, "client%lld\n",
Ilya Dryomov033268a2016-08-12 14:59:58 +02005143 ceph_client_gid(rbd_dev->rbd_client->client));
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005144}
5145
Mike Christie267fb902016-08-18 18:38:43 +02005146static ssize_t rbd_cluster_fsid_show(struct device *dev,
5147 struct device_attribute *attr, char *buf)
5148{
5149 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
5150
5151 return sprintf(buf, "%pU\n", &rbd_dev->rbd_client->client->fsid);
5152}
5153
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02005154static ssize_t rbd_config_info_show(struct device *dev,
5155 struct device_attribute *attr, char *buf)
5156{
5157 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
5158
5159 return sprintf(buf, "%s\n", rbd_dev->config_info);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005160}
5161
5162static ssize_t rbd_pool_show(struct device *dev,
5163 struct device_attribute *attr, char *buf)
5164{
Alex Elder593a9e72012-02-07 12:03:37 -06005165 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005166
Alex Elder0d7dbfc2012-10-25 23:34:41 -05005167 return sprintf(buf, "%s\n", rbd_dev->spec->pool_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005168}
5169
Alex Elder9bb2f332012-07-12 10:46:35 -05005170static ssize_t rbd_pool_id_show(struct device *dev,
5171 struct device_attribute *attr, char *buf)
5172{
5173 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
5174
Alex Elder0d7dbfc2012-10-25 23:34:41 -05005175 return sprintf(buf, "%llu\n",
Alex Elderfc71d832013-04-26 15:44:36 -05005176 (unsigned long long) rbd_dev->spec->pool_id);
Alex Elder9bb2f332012-07-12 10:46:35 -05005177}
5178
Ilya Dryomovb26c0472018-07-03 15:28:43 +02005179static ssize_t rbd_pool_ns_show(struct device *dev,
5180 struct device_attribute *attr, char *buf)
5181{
5182 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
5183
5184 return sprintf(buf, "%s\n", rbd_dev->spec->pool_ns ?: "");
5185}
5186
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005187static ssize_t rbd_name_show(struct device *dev,
5188 struct device_attribute *attr, char *buf)
5189{
Alex Elder593a9e72012-02-07 12:03:37 -06005190 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005191
Alex Eldera92ffdf2012-10-30 19:40:33 -05005192 if (rbd_dev->spec->image_name)
5193 return sprintf(buf, "%s\n", rbd_dev->spec->image_name);
5194
5195 return sprintf(buf, "(unknown)\n");
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005196}
5197
Alex Elder589d30e2012-07-10 20:30:11 -05005198static ssize_t rbd_image_id_show(struct device *dev,
5199 struct device_attribute *attr, char *buf)
5200{
5201 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
5202
Alex Elder0d7dbfc2012-10-25 23:34:41 -05005203 return sprintf(buf, "%s\n", rbd_dev->spec->image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05005204}
5205
Alex Elder34b13182012-07-13 20:35:12 -05005206/*
5207 * Shows the name of the currently-mapped snapshot (or
5208 * RBD_SNAP_HEAD_NAME for the base image).
5209 */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005210static ssize_t rbd_snap_show(struct device *dev,
5211 struct device_attribute *attr,
5212 char *buf)
5213{
Alex Elder593a9e72012-02-07 12:03:37 -06005214 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005215
Alex Elder0d7dbfc2012-10-25 23:34:41 -05005216 return sprintf(buf, "%s\n", rbd_dev->spec->snap_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005217}
5218
Mike Christie92a58672016-08-18 18:38:44 +02005219static ssize_t rbd_snap_id_show(struct device *dev,
5220 struct device_attribute *attr, char *buf)
5221{
5222 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
5223
5224 return sprintf(buf, "%llu\n", rbd_dev->spec->snap_id);
5225}
5226
Alex Elder86b00e02012-10-25 23:34:42 -05005227/*
Ilya Dryomovff961282014-07-22 21:53:07 +04005228 * For a v2 image, shows the chain of parent images, separated by empty
5229 * lines. For v1 images or if there is no parent, shows "(no parent
5230 * image)".
Alex Elder86b00e02012-10-25 23:34:42 -05005231 */
5232static ssize_t rbd_parent_show(struct device *dev,
Ilya Dryomovff961282014-07-22 21:53:07 +04005233 struct device_attribute *attr,
5234 char *buf)
Alex Elder86b00e02012-10-25 23:34:42 -05005235{
5236 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Ilya Dryomovff961282014-07-22 21:53:07 +04005237 ssize_t count = 0;
Alex Elder86b00e02012-10-25 23:34:42 -05005238
Ilya Dryomovff961282014-07-22 21:53:07 +04005239 if (!rbd_dev->parent)
Alex Elder86b00e02012-10-25 23:34:42 -05005240 return sprintf(buf, "(no parent image)\n");
5241
Ilya Dryomovff961282014-07-22 21:53:07 +04005242 for ( ; rbd_dev->parent; rbd_dev = rbd_dev->parent) {
5243 struct rbd_spec *spec = rbd_dev->parent_spec;
Alex Elder86b00e02012-10-25 23:34:42 -05005244
Ilya Dryomovff961282014-07-22 21:53:07 +04005245 count += sprintf(&buf[count], "%s"
5246 "pool_id %llu\npool_name %s\n"
Ilya Dryomove92c0ea2018-08-22 17:26:10 +02005247 "pool_ns %s\n"
Ilya Dryomovff961282014-07-22 21:53:07 +04005248 "image_id %s\nimage_name %s\n"
5249 "snap_id %llu\nsnap_name %s\n"
5250 "overlap %llu\n",
5251 !count ? "" : "\n", /* first? */
5252 spec->pool_id, spec->pool_name,
Ilya Dryomove92c0ea2018-08-22 17:26:10 +02005253 spec->pool_ns ?: "",
Ilya Dryomovff961282014-07-22 21:53:07 +04005254 spec->image_id, spec->image_name ?: "(unknown)",
5255 spec->snap_id, spec->snap_name,
5256 rbd_dev->parent_overlap);
5257 }
Alex Elder86b00e02012-10-25 23:34:42 -05005258
Ilya Dryomovff961282014-07-22 21:53:07 +04005259 return count;
Alex Elder86b00e02012-10-25 23:34:42 -05005260}
5261
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005262static ssize_t rbd_image_refresh(struct device *dev,
5263 struct device_attribute *attr,
5264 const char *buf,
5265 size_t size)
5266{
Alex Elder593a9e72012-02-07 12:03:37 -06005267 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Alex Elderb8136232012-07-25 09:32:41 -05005268 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005269
Alex Eldercc4a38bd2013-04-30 00:44:33 -05005270 ret = rbd_dev_refresh(rbd_dev);
Alex Eldere627db02013-05-06 07:40:30 -05005271 if (ret)
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04005272 return ret;
Alex Elderb8136232012-07-25 09:32:41 -05005273
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04005274 return size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005275}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005276
Joe Perches5657a812018-05-24 13:38:59 -06005277static DEVICE_ATTR(size, 0444, rbd_size_show, NULL);
5278static DEVICE_ATTR(features, 0444, rbd_features_show, NULL);
5279static DEVICE_ATTR(major, 0444, rbd_major_show, NULL);
5280static DEVICE_ATTR(minor, 0444, rbd_minor_show, NULL);
5281static DEVICE_ATTR(client_addr, 0444, rbd_client_addr_show, NULL);
5282static DEVICE_ATTR(client_id, 0444, rbd_client_id_show, NULL);
5283static DEVICE_ATTR(cluster_fsid, 0444, rbd_cluster_fsid_show, NULL);
5284static DEVICE_ATTR(config_info, 0400, rbd_config_info_show, NULL);
5285static DEVICE_ATTR(pool, 0444, rbd_pool_show, NULL);
5286static DEVICE_ATTR(pool_id, 0444, rbd_pool_id_show, NULL);
Ilya Dryomovb26c0472018-07-03 15:28:43 +02005287static DEVICE_ATTR(pool_ns, 0444, rbd_pool_ns_show, NULL);
Joe Perches5657a812018-05-24 13:38:59 -06005288static DEVICE_ATTR(name, 0444, rbd_name_show, NULL);
5289static DEVICE_ATTR(image_id, 0444, rbd_image_id_show, NULL);
5290static DEVICE_ATTR(refresh, 0200, NULL, rbd_image_refresh);
5291static DEVICE_ATTR(current_snap, 0444, rbd_snap_show, NULL);
5292static DEVICE_ATTR(snap_id, 0444, rbd_snap_id_show, NULL);
5293static DEVICE_ATTR(parent, 0444, rbd_parent_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005294
5295static struct attribute *rbd_attrs[] = {
5296 &dev_attr_size.attr,
Alex Elder34b13182012-07-13 20:35:12 -05005297 &dev_attr_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005298 &dev_attr_major.attr,
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02005299 &dev_attr_minor.attr,
Ilya Dryomov005a07bf2016-08-18 18:38:43 +02005300 &dev_attr_client_addr.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005301 &dev_attr_client_id.attr,
Mike Christie267fb902016-08-18 18:38:43 +02005302 &dev_attr_cluster_fsid.attr,
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02005303 &dev_attr_config_info.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005304 &dev_attr_pool.attr,
Alex Elder9bb2f332012-07-12 10:46:35 -05005305 &dev_attr_pool_id.attr,
Ilya Dryomovb26c0472018-07-03 15:28:43 +02005306 &dev_attr_pool_ns.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005307 &dev_attr_name.attr,
Alex Elder589d30e2012-07-10 20:30:11 -05005308 &dev_attr_image_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005309 &dev_attr_current_snap.attr,
Mike Christie92a58672016-08-18 18:38:44 +02005310 &dev_attr_snap_id.attr,
Alex Elder86b00e02012-10-25 23:34:42 -05005311 &dev_attr_parent.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005312 &dev_attr_refresh.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005313 NULL
5314};
5315
5316static struct attribute_group rbd_attr_group = {
5317 .attrs = rbd_attrs,
5318};
5319
5320static const struct attribute_group *rbd_attr_groups[] = {
5321 &rbd_attr_group,
5322 NULL
5323};
5324
Ilya Dryomov6cac4692015-10-16 20:11:25 +02005325static void rbd_dev_release(struct device *dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005326
Bhumika Goyalb9942bc2017-02-11 12:14:38 +05305327static const struct device_type rbd_device_type = {
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005328 .name = "rbd",
5329 .groups = rbd_attr_groups,
Ilya Dryomov6cac4692015-10-16 20:11:25 +02005330 .release = rbd_dev_release,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005331};
5332
Alex Elder8b8fb992012-10-26 17:25:24 -05005333static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)
5334{
5335 kref_get(&spec->kref);
5336
5337 return spec;
5338}
5339
5340static void rbd_spec_free(struct kref *kref);
5341static void rbd_spec_put(struct rbd_spec *spec)
5342{
5343 if (spec)
5344 kref_put(&spec->kref, rbd_spec_free);
5345}
5346
5347static struct rbd_spec *rbd_spec_alloc(void)
5348{
5349 struct rbd_spec *spec;
5350
5351 spec = kzalloc(sizeof (*spec), GFP_KERNEL);
5352 if (!spec)
5353 return NULL;
Ilya Dryomov04077592014-07-23 17:11:20 +04005354
5355 spec->pool_id = CEPH_NOPOOL;
5356 spec->snap_id = CEPH_NOSNAP;
Alex Elder8b8fb992012-10-26 17:25:24 -05005357 kref_init(&spec->kref);
5358
Alex Elder8b8fb992012-10-26 17:25:24 -05005359 return spec;
5360}
5361
5362static void rbd_spec_free(struct kref *kref)
5363{
5364 struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref);
5365
5366 kfree(spec->pool_name);
Ilya Dryomovb26c0472018-07-03 15:28:43 +02005367 kfree(spec->pool_ns);
Alex Elder8b8fb992012-10-26 17:25:24 -05005368 kfree(spec->image_id);
5369 kfree(spec->image_name);
5370 kfree(spec->snap_name);
5371 kfree(spec);
5372}
5373
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02005374static void rbd_dev_free(struct rbd_device *rbd_dev)
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02005375{
Ilya Dryomov99d16942016-08-12 16:11:41 +02005376 WARN_ON(rbd_dev->watch_state != RBD_WATCH_STATE_UNREGISTERED);
Ilya Dryomoved95b212016-08-12 16:40:02 +02005377 WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_UNLOCKED);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02005378
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005379 ceph_oid_destroy(&rbd_dev->header_oid);
Ilya Dryomov6b6dddb2016-08-05 16:15:38 +02005380 ceph_oloc_destroy(&rbd_dev->header_oloc);
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02005381 kfree(rbd_dev->config_info);
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005382
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02005383 rbd_put_client(rbd_dev->rbd_client);
5384 rbd_spec_put(rbd_dev->spec);
5385 kfree(rbd_dev->opts);
5386 kfree(rbd_dev);
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02005387}
5388
5389static void rbd_dev_release(struct device *dev)
5390{
5391 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
5392 bool need_put = !!rbd_dev->opts;
5393
5394 if (need_put) {
5395 destroy_workqueue(rbd_dev->task_wq);
5396 ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id);
5397 }
5398
5399 rbd_dev_free(rbd_dev);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02005400
5401 /*
5402 * This is racy, but way better than putting module outside of
5403 * the release callback. The race window is pretty small, so
5404 * doing something similar to dm (dm-builtin.c) is overkill.
5405 */
5406 if (need_put)
5407 module_put(THIS_MODULE);
5408}
5409
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02005410static struct rbd_device *__rbd_dev_create(struct rbd_client *rbdc,
5411 struct rbd_spec *spec)
Alex Elderc53d5892012-10-25 23:34:42 -05005412{
5413 struct rbd_device *rbd_dev;
5414
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02005415 rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL);
Alex Elderc53d5892012-10-25 23:34:42 -05005416 if (!rbd_dev)
5417 return NULL;
5418
5419 spin_lock_init(&rbd_dev->lock);
5420 INIT_LIST_HEAD(&rbd_dev->node);
Alex Elderc53d5892012-10-25 23:34:42 -05005421 init_rwsem(&rbd_dev->header_rwsem);
5422
Ilya Dryomov7e973322017-01-25 18:16:22 +01005423 rbd_dev->header.data_pool_id = CEPH_NOPOOL;
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005424 ceph_oid_init(&rbd_dev->header_oid);
Ilya Dryomov431a02c2017-01-25 18:16:21 +01005425 rbd_dev->header_oloc.pool = spec->pool_id;
Ilya Dryomovb26c0472018-07-03 15:28:43 +02005426 if (spec->pool_ns) {
5427 WARN_ON(!*spec->pool_ns);
5428 rbd_dev->header_oloc.pool_ns =
5429 ceph_find_or_create_string(spec->pool_ns,
5430 strlen(spec->pool_ns));
5431 }
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005432
Ilya Dryomov99d16942016-08-12 16:11:41 +02005433 mutex_init(&rbd_dev->watch_mutex);
5434 rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED;
5435 INIT_DELAYED_WORK(&rbd_dev->watch_dwork, rbd_reregister_watch);
5436
Ilya Dryomoved95b212016-08-12 16:40:02 +02005437 init_rwsem(&rbd_dev->lock_rwsem);
5438 rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED;
5439 INIT_WORK(&rbd_dev->acquired_lock_work, rbd_notify_acquired_lock);
5440 INIT_WORK(&rbd_dev->released_lock_work, rbd_notify_released_lock);
5441 INIT_DELAYED_WORK(&rbd_dev->lock_dwork, rbd_acquire_lock);
5442 INIT_WORK(&rbd_dev->unlock_work, rbd_release_lock_work);
Ilya Dryomove1fddc82019-05-30 16:07:48 +02005443 spin_lock_init(&rbd_dev->lock_lists_lock);
Ilya Dryomov637cd062019-06-06 17:14:49 +02005444 INIT_LIST_HEAD(&rbd_dev->acquiring_list);
Ilya Dryomove1fddc82019-05-30 16:07:48 +02005445 INIT_LIST_HEAD(&rbd_dev->running_list);
Ilya Dryomov637cd062019-06-06 17:14:49 +02005446 init_completion(&rbd_dev->acquire_wait);
Ilya Dryomove1fddc82019-05-30 16:07:48 +02005447 init_completion(&rbd_dev->releasing_wait);
Ilya Dryomoved95b212016-08-12 16:40:02 +02005448
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02005449 spin_lock_init(&rbd_dev->object_map_lock);
Alex Elderc53d5892012-10-25 23:34:42 -05005450
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02005451 rbd_dev->dev.bus = &rbd_bus_type;
5452 rbd_dev->dev.type = &rbd_device_type;
5453 rbd_dev->dev.parent = &rbd_root_dev;
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02005454 device_initialize(&rbd_dev->dev);
5455
Alex Elderc53d5892012-10-25 23:34:42 -05005456 rbd_dev->rbd_client = rbdc;
Ilya Dryomovd1475432015-06-22 13:24:48 +03005457 rbd_dev->spec = spec;
Alex Elder0903e872012-11-14 12:25:19 -06005458
Alex Elderc53d5892012-10-25 23:34:42 -05005459 return rbd_dev;
5460}
5461
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02005462/*
5463 * Create a mapping rbd_dev.
5464 */
5465static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,
5466 struct rbd_spec *spec,
5467 struct rbd_options *opts)
5468{
5469 struct rbd_device *rbd_dev;
5470
5471 rbd_dev = __rbd_dev_create(rbdc, spec);
5472 if (!rbd_dev)
5473 return NULL;
5474
5475 rbd_dev->opts = opts;
5476
5477 /* get an id and fill in device name */
5478 rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0,
5479 minor_to_rbd_dev_id(1 << MINORBITS),
5480 GFP_KERNEL);
5481 if (rbd_dev->dev_id < 0)
5482 goto fail_rbd_dev;
5483
5484 sprintf(rbd_dev->name, RBD_DRV_NAME "%d", rbd_dev->dev_id);
5485 rbd_dev->task_wq = alloc_ordered_workqueue("%s-tasks", WQ_MEM_RECLAIM,
5486 rbd_dev->name);
5487 if (!rbd_dev->task_wq)
5488 goto fail_dev_id;
5489
5490 /* we have a ref from do_rbd_add() */
5491 __module_get(THIS_MODULE);
5492
5493 dout("%s rbd_dev %p dev_id %d\n", __func__, rbd_dev, rbd_dev->dev_id);
5494 return rbd_dev;
5495
5496fail_dev_id:
5497 ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id);
5498fail_rbd_dev:
5499 rbd_dev_free(rbd_dev);
5500 return NULL;
5501}
5502
Alex Elderc53d5892012-10-25 23:34:42 -05005503static void rbd_dev_destroy(struct rbd_device *rbd_dev)
5504{
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02005505 if (rbd_dev)
5506 put_device(&rbd_dev->dev);
Alex Elderc53d5892012-10-25 23:34:42 -05005507}
5508
Yehuda Sadehdfc56062010-11-19 14:51:04 -08005509/*
Alex Elder9d475de2012-07-03 16:01:19 -05005510 * Get the size and object order for an image snapshot, or if
5511 * snap_id is CEPH_NOSNAP, gets this information for the base
5512 * image.
5513 */
5514static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
5515 u8 *order, u64 *snap_size)
5516{
5517 __le64 snapid = cpu_to_le64(snap_id);
5518 int ret;
5519 struct {
5520 u8 order;
5521 __le64 size;
5522 } __attribute__ ((packed)) size_buf = { 0 };
5523
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005524 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5525 &rbd_dev->header_oloc, "get_size",
5526 &snapid, sizeof(snapid),
5527 &size_buf, sizeof(size_buf));
Alex Elder36be9a72013-01-19 00:30:28 -06005528 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder9d475de2012-07-03 16:01:19 -05005529 if (ret < 0)
5530 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05005531 if (ret < sizeof (size_buf))
5532 return -ERANGE;
Alex Elder9d475de2012-07-03 16:01:19 -05005533
Josh Durginc3545572013-08-28 17:08:10 -07005534 if (order) {
Alex Elderc86f86e2013-04-25 15:09:41 -05005535 *order = size_buf.order;
Josh Durginc3545572013-08-28 17:08:10 -07005536 dout(" order %u", (unsigned int)*order);
5537 }
Alex Elder9d475de2012-07-03 16:01:19 -05005538 *snap_size = le64_to_cpu(size_buf.size);
5539
Josh Durginc3545572013-08-28 17:08:10 -07005540 dout(" snap_id 0x%016llx snap_size = %llu\n",
5541 (unsigned long long)snap_id,
Alex Elder57385b52013-04-21 12:14:45 -05005542 (unsigned long long)*snap_size);
Alex Elder9d475de2012-07-03 16:01:19 -05005543
5544 return 0;
5545}
5546
5547static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
5548{
5549 return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
5550 &rbd_dev->header.obj_order,
5551 &rbd_dev->header.image_size);
5552}
5553
Alex Elder1e130192012-07-03 16:01:19 -05005554static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
5555{
Dongsheng Yang5435d2062019-08-09 07:05:27 +00005556 size_t size;
Alex Elder1e130192012-07-03 16:01:19 -05005557 void *reply_buf;
5558 int ret;
5559 void *p;
5560
Dongsheng Yang5435d2062019-08-09 07:05:27 +00005561 /* Response will be an encoded string, which includes a length */
5562 size = sizeof(__le32) + RBD_OBJ_PREFIX_LEN_MAX;
5563 reply_buf = kzalloc(size, GFP_KERNEL);
Alex Elder1e130192012-07-03 16:01:19 -05005564 if (!reply_buf)
5565 return -ENOMEM;
5566
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005567 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5568 &rbd_dev->header_oloc, "get_object_prefix",
Dongsheng Yang5435d2062019-08-09 07:05:27 +00005569 NULL, 0, reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06005570 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder1e130192012-07-03 16:01:19 -05005571 if (ret < 0)
5572 goto out;
5573
5574 p = reply_buf;
5575 rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p,
Alex Elder57385b52013-04-21 12:14:45 -05005576 p + ret, NULL, GFP_NOIO);
5577 ret = 0;
Alex Elder1e130192012-07-03 16:01:19 -05005578
5579 if (IS_ERR(rbd_dev->header.object_prefix)) {
5580 ret = PTR_ERR(rbd_dev->header.object_prefix);
5581 rbd_dev->header.object_prefix = NULL;
5582 } else {
5583 dout(" object_prefix = %s\n", rbd_dev->header.object_prefix);
5584 }
Alex Elder1e130192012-07-03 16:01:19 -05005585out:
5586 kfree(reply_buf);
5587
5588 return ret;
5589}
5590
Alex Elderb1b54022012-07-03 16:01:19 -05005591static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
Ilya Dryomov196e2d62019-11-05 15:38:46 +01005592 bool read_only, u64 *snap_features)
Alex Elderb1b54022012-07-03 16:01:19 -05005593{
Ilya Dryomov196e2d62019-11-05 15:38:46 +01005594 struct {
5595 __le64 snap_id;
5596 u8 read_only;
5597 } features_in;
Alex Elderb1b54022012-07-03 16:01:19 -05005598 struct {
5599 __le64 features;
5600 __le64 incompat;
Alex Elder41579762013-04-21 12:14:45 -05005601 } __attribute__ ((packed)) features_buf = { 0 };
Ilya Dryomovd3767f02016-04-13 14:15:50 +02005602 u64 unsup;
Alex Elderb1b54022012-07-03 16:01:19 -05005603 int ret;
5604
Ilya Dryomov196e2d62019-11-05 15:38:46 +01005605 features_in.snap_id = cpu_to_le64(snap_id);
5606 features_in.read_only = read_only;
5607
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005608 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5609 &rbd_dev->header_oloc, "get_features",
Ilya Dryomov196e2d62019-11-05 15:38:46 +01005610 &features_in, sizeof(features_in),
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005611 &features_buf, sizeof(features_buf));
Alex Elder36be9a72013-01-19 00:30:28 -06005612 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderb1b54022012-07-03 16:01:19 -05005613 if (ret < 0)
5614 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05005615 if (ret < sizeof (features_buf))
5616 return -ERANGE;
Alex Elderd8891402012-10-09 13:50:17 -07005617
Ilya Dryomovd3767f02016-04-13 14:15:50 +02005618 unsup = le64_to_cpu(features_buf.incompat) & ~RBD_FEATURES_SUPPORTED;
5619 if (unsup) {
5620 rbd_warn(rbd_dev, "image uses unsupported features: 0x%llx",
5621 unsup);
Alex Elderb8f5c6e2012-11-01 08:39:26 -05005622 return -ENXIO;
Ilya Dryomovd3767f02016-04-13 14:15:50 +02005623 }
Alex Elderd8891402012-10-09 13:50:17 -07005624
Alex Elderb1b54022012-07-03 16:01:19 -05005625 *snap_features = le64_to_cpu(features_buf.features);
5626
5627 dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
Alex Elder57385b52013-04-21 12:14:45 -05005628 (unsigned long long)snap_id,
5629 (unsigned long long)*snap_features,
5630 (unsigned long long)le64_to_cpu(features_buf.incompat));
Alex Elderb1b54022012-07-03 16:01:19 -05005631
5632 return 0;
5633}
5634
5635static int rbd_dev_v2_features(struct rbd_device *rbd_dev)
5636{
5637 return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP,
Ilya Dryomov196e2d62019-11-05 15:38:46 +01005638 rbd_is_ro(rbd_dev),
5639 &rbd_dev->header.features);
Alex Elderb1b54022012-07-03 16:01:19 -05005640}
5641
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02005642/*
5643 * These are generic image flags, but since they are used only for
5644 * object map, store them in rbd_dev->object_map_flags.
5645 *
5646 * For the same reason, this function is called only on object map
5647 * (re)load and not on header refresh.
5648 */
5649static int rbd_dev_v2_get_flags(struct rbd_device *rbd_dev)
5650{
5651 __le64 snapid = cpu_to_le64(rbd_dev->spec->snap_id);
5652 __le64 flags;
5653 int ret;
5654
5655 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5656 &rbd_dev->header_oloc, "get_flags",
5657 &snapid, sizeof(snapid),
5658 &flags, sizeof(flags));
5659 if (ret < 0)
5660 return ret;
5661 if (ret < sizeof(flags))
5662 return -EBADMSG;
5663
5664 rbd_dev->object_map_flags = le64_to_cpu(flags);
5665 return 0;
5666}
5667
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005668struct parent_image_info {
5669 u64 pool_id;
Ilya Dryomove92c0ea2018-08-22 17:26:10 +02005670 const char *pool_ns;
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005671 const char *image_id;
5672 u64 snap_id;
5673
Ilya Dryomove92c0ea2018-08-22 17:26:10 +02005674 bool has_overlap;
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005675 u64 overlap;
5676};
5677
5678/*
5679 * The caller is responsible for @pii.
5680 */
Ilya Dryomove92c0ea2018-08-22 17:26:10 +02005681static int decode_parent_image_spec(void **p, void *end,
5682 struct parent_image_info *pii)
5683{
5684 u8 struct_v;
5685 u32 struct_len;
5686 int ret;
5687
5688 ret = ceph_start_decoding(p, end, 1, "ParentImageSpec",
5689 &struct_v, &struct_len);
5690 if (ret)
5691 return ret;
5692
5693 ceph_decode_64_safe(p, end, pii->pool_id, e_inval);
5694 pii->pool_ns = ceph_extract_encoded_string(p, end, NULL, GFP_KERNEL);
5695 if (IS_ERR(pii->pool_ns)) {
5696 ret = PTR_ERR(pii->pool_ns);
5697 pii->pool_ns = NULL;
5698 return ret;
5699 }
5700 pii->image_id = ceph_extract_encoded_string(p, end, NULL, GFP_KERNEL);
5701 if (IS_ERR(pii->image_id)) {
5702 ret = PTR_ERR(pii->image_id);
5703 pii->image_id = NULL;
5704 return ret;
5705 }
5706 ceph_decode_64_safe(p, end, pii->snap_id, e_inval);
5707 return 0;
5708
5709e_inval:
5710 return -EINVAL;
5711}
5712
5713static int __get_parent_info(struct rbd_device *rbd_dev,
5714 struct page *req_page,
5715 struct page *reply_page,
5716 struct parent_image_info *pii)
5717{
5718 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
5719 size_t reply_len = PAGE_SIZE;
5720 void *p, *end;
5721 int ret;
5722
5723 ret = ceph_osdc_call(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc,
5724 "rbd", "parent_get", CEPH_OSD_FLAG_READ,
Ilya Dryomov68ada912019-06-14 18:16:51 +02005725 req_page, sizeof(u64), &reply_page, &reply_len);
Ilya Dryomove92c0ea2018-08-22 17:26:10 +02005726 if (ret)
5727 return ret == -EOPNOTSUPP ? 1 : ret;
5728
5729 p = page_address(reply_page);
5730 end = p + reply_len;
5731 ret = decode_parent_image_spec(&p, end, pii);
5732 if (ret)
5733 return ret;
5734
5735 ret = ceph_osdc_call(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc,
5736 "rbd", "parent_overlap_get", CEPH_OSD_FLAG_READ,
Ilya Dryomov68ada912019-06-14 18:16:51 +02005737 req_page, sizeof(u64), &reply_page, &reply_len);
Ilya Dryomove92c0ea2018-08-22 17:26:10 +02005738 if (ret)
5739 return ret;
5740
5741 p = page_address(reply_page);
5742 end = p + reply_len;
5743 ceph_decode_8_safe(&p, end, pii->has_overlap, e_inval);
5744 if (pii->has_overlap)
5745 ceph_decode_64_safe(&p, end, pii->overlap, e_inval);
5746
5747 return 0;
5748
5749e_inval:
5750 return -EINVAL;
5751}
5752
5753/*
5754 * The caller is responsible for @pii.
5755 */
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005756static int __get_parent_info_legacy(struct rbd_device *rbd_dev,
5757 struct page *req_page,
5758 struct page *reply_page,
5759 struct parent_image_info *pii)
5760{
5761 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
5762 size_t reply_len = PAGE_SIZE;
5763 void *p, *end;
5764 int ret;
5765
5766 ret = ceph_osdc_call(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc,
5767 "rbd", "get_parent", CEPH_OSD_FLAG_READ,
Ilya Dryomov68ada912019-06-14 18:16:51 +02005768 req_page, sizeof(u64), &reply_page, &reply_len);
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005769 if (ret)
5770 return ret;
5771
5772 p = page_address(reply_page);
5773 end = p + reply_len;
5774 ceph_decode_64_safe(&p, end, pii->pool_id, e_inval);
5775 pii->image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
5776 if (IS_ERR(pii->image_id)) {
5777 ret = PTR_ERR(pii->image_id);
5778 pii->image_id = NULL;
5779 return ret;
5780 }
5781 ceph_decode_64_safe(&p, end, pii->snap_id, e_inval);
Ilya Dryomove92c0ea2018-08-22 17:26:10 +02005782 pii->has_overlap = true;
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005783 ceph_decode_64_safe(&p, end, pii->overlap, e_inval);
5784
5785 return 0;
5786
5787e_inval:
5788 return -EINVAL;
5789}
5790
5791static int get_parent_info(struct rbd_device *rbd_dev,
5792 struct parent_image_info *pii)
5793{
5794 struct page *req_page, *reply_page;
5795 void *p;
5796 int ret;
5797
5798 req_page = alloc_page(GFP_KERNEL);
5799 if (!req_page)
5800 return -ENOMEM;
5801
5802 reply_page = alloc_page(GFP_KERNEL);
5803 if (!reply_page) {
5804 __free_page(req_page);
5805 return -ENOMEM;
5806 }
5807
5808 p = page_address(req_page);
5809 ceph_encode_64(&p, rbd_dev->spec->snap_id);
Ilya Dryomove92c0ea2018-08-22 17:26:10 +02005810 ret = __get_parent_info(rbd_dev, req_page, reply_page, pii);
5811 if (ret > 0)
5812 ret = __get_parent_info_legacy(rbd_dev, req_page, reply_page,
5813 pii);
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005814
5815 __free_page(req_page);
5816 __free_page(reply_page);
5817 return ret;
5818}
5819
Alex Elder86b00e02012-10-25 23:34:42 -05005820static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev)
5821{
5822 struct rbd_spec *parent_spec;
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005823 struct parent_image_info pii = { 0 };
Alex Elder86b00e02012-10-25 23:34:42 -05005824 int ret;
5825
5826 parent_spec = rbd_spec_alloc();
5827 if (!parent_spec)
5828 return -ENOMEM;
5829
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005830 ret = get_parent_info(rbd_dev, &pii);
5831 if (ret)
Alex Elder86b00e02012-10-25 23:34:42 -05005832 goto out_err;
5833
Ilya Dryomove92c0ea2018-08-22 17:26:10 +02005834 dout("%s pool_id %llu pool_ns %s image_id %s snap_id %llu has_overlap %d overlap %llu\n",
5835 __func__, pii.pool_id, pii.pool_ns, pii.image_id, pii.snap_id,
5836 pii.has_overlap, pii.overlap);
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005837
Ilya Dryomove92c0ea2018-08-22 17:26:10 +02005838 if (pii.pool_id == CEPH_NOPOOL || !pii.has_overlap) {
Alex Elder392a9da2013-05-06 17:40:33 -05005839 /*
5840 * Either the parent never existed, or we have
5841 * record of it but the image got flattened so it no
5842 * longer has a parent. When the parent of a
5843 * layered image disappears we immediately set the
5844 * overlap to 0. The effect of this is that all new
5845 * requests will be treated as if the image had no
5846 * parent.
Ilya Dryomove92c0ea2018-08-22 17:26:10 +02005847 *
5848 * If !pii.has_overlap, the parent image spec is not
5849 * applicable. It's there to avoid duplication in each
5850 * snapshot record.
Alex Elder392a9da2013-05-06 17:40:33 -05005851 */
5852 if (rbd_dev->parent_overlap) {
5853 rbd_dev->parent_overlap = 0;
Alex Elder392a9da2013-05-06 17:40:33 -05005854 rbd_dev_parent_put(rbd_dev);
5855 pr_info("%s: clone image has been flattened\n",
5856 rbd_dev->disk->disk_name);
5857 }
5858
Alex Elder86b00e02012-10-25 23:34:42 -05005859 goto out; /* No parent? No problem. */
Alex Elder392a9da2013-05-06 17:40:33 -05005860 }
Alex Elder86b00e02012-10-25 23:34:42 -05005861
Alex Elder0903e872012-11-14 12:25:19 -06005862 /* The ceph file layout needs to fit pool id in 32 bits */
5863
5864 ret = -EIO;
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005865 if (pii.pool_id > (u64)U32_MAX) {
Ilya Dryomov9584d502014-07-11 12:11:20 +04005866 rbd_warn(NULL, "parent pool id too large (%llu > %u)",
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005867 (unsigned long long)pii.pool_id, U32_MAX);
Alex Elder57385b52013-04-21 12:14:45 -05005868 goto out_err;
Alex Elderc0cd10db2013-04-26 09:43:47 -05005869 }
Alex Elder0903e872012-11-14 12:25:19 -06005870
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005871 /*
5872 * The parent won't change (except when the clone is
5873 * flattened, already handled that). So we only need to
5874 * record the parent spec we have not already done so.
5875 */
5876 if (!rbd_dev->parent_spec) {
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005877 parent_spec->pool_id = pii.pool_id;
Ilya Dryomove92c0ea2018-08-22 17:26:10 +02005878 if (pii.pool_ns && *pii.pool_ns) {
5879 parent_spec->pool_ns = pii.pool_ns;
5880 pii.pool_ns = NULL;
5881 }
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005882 parent_spec->image_id = pii.image_id;
5883 pii.image_id = NULL;
5884 parent_spec->snap_id = pii.snap_id;
Ilya Dryomovb26c0472018-07-03 15:28:43 +02005885
Alex Elder70cf49c2013-05-06 17:40:33 -05005886 rbd_dev->parent_spec = parent_spec;
5887 parent_spec = NULL; /* rbd_dev now owns this */
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005888 }
5889
5890 /*
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005891 * We always update the parent overlap. If it's zero we issue
5892 * a warning, as we will proceed as if there was no parent.
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005893 */
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005894 if (!pii.overlap) {
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005895 if (parent_spec) {
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005896 /* refresh, careful to warn just once */
5897 if (rbd_dev->parent_overlap)
5898 rbd_warn(rbd_dev,
5899 "clone now standalone (overlap became 0)");
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005900 } else {
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005901 /* initial probe */
5902 rbd_warn(rbd_dev, "clone is standalone (overlap 0)");
Alex Elder3b5cf2a2013-05-29 11:18:59 -05005903 }
Alex Elder70cf49c2013-05-06 17:40:33 -05005904 }
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005905 rbd_dev->parent_overlap = pii.overlap;
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03005906
Alex Elder86b00e02012-10-25 23:34:42 -05005907out:
5908 ret = 0;
5909out_err:
Ilya Dryomove92c0ea2018-08-22 17:26:10 +02005910 kfree(pii.pool_ns);
Ilya Dryomoveb3b2d62018-08-22 17:11:27 +02005911 kfree(pii.image_id);
Alex Elder86b00e02012-10-25 23:34:42 -05005912 rbd_spec_put(parent_spec);
Alex Elder86b00e02012-10-25 23:34:42 -05005913 return ret;
5914}
5915
Alex Eldercc070d52013-04-21 12:14:45 -05005916static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev)
5917{
5918 struct {
5919 __le64 stripe_unit;
5920 __le64 stripe_count;
5921 } __attribute__ ((packed)) striping_info_buf = { 0 };
5922 size_t size = sizeof (striping_info_buf);
5923 void *p;
Alex Eldercc070d52013-04-21 12:14:45 -05005924 int ret;
5925
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005926 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5927 &rbd_dev->header_oloc, "get_stripe_unit_count",
5928 NULL, 0, &striping_info_buf, size);
Alex Eldercc070d52013-04-21 12:14:45 -05005929 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
5930 if (ret < 0)
5931 return ret;
5932 if (ret < size)
5933 return -ERANGE;
5934
Alex Eldercc070d52013-04-21 12:14:45 -05005935 p = &striping_info_buf;
Ilya Dryomovb1331852018-02-07 12:09:12 +01005936 rbd_dev->header.stripe_unit = ceph_decode_64(&p);
5937 rbd_dev->header.stripe_count = ceph_decode_64(&p);
Alex Eldercc070d52013-04-21 12:14:45 -05005938 return 0;
5939}
5940
Ilya Dryomov7e973322017-01-25 18:16:22 +01005941static int rbd_dev_v2_data_pool(struct rbd_device *rbd_dev)
5942{
5943 __le64 data_pool_id;
5944 int ret;
5945
5946 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5947 &rbd_dev->header_oloc, "get_data_pool",
5948 NULL, 0, &data_pool_id, sizeof(data_pool_id));
5949 if (ret < 0)
5950 return ret;
5951 if (ret < sizeof(data_pool_id))
5952 return -EBADMSG;
5953
5954 rbd_dev->header.data_pool_id = le64_to_cpu(data_pool_id);
5955 WARN_ON(rbd_dev->header.data_pool_id == CEPH_NOPOOL);
5956 return 0;
5957}
5958
Alex Elder9e15b772012-10-30 19:40:33 -05005959static char *rbd_dev_image_name(struct rbd_device *rbd_dev)
5960{
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005961 CEPH_DEFINE_OID_ONSTACK(oid);
Alex Elder9e15b772012-10-30 19:40:33 -05005962 size_t image_id_size;
5963 char *image_id;
5964 void *p;
5965 void *end;
5966 size_t size;
5967 void *reply_buf = NULL;
5968 size_t len = 0;
5969 char *image_name = NULL;
5970 int ret;
5971
5972 rbd_assert(!rbd_dev->spec->image_name);
5973
Alex Elder69e7a022012-11-01 08:39:26 -05005974 len = strlen(rbd_dev->spec->image_id);
5975 image_id_size = sizeof (__le32) + len;
Alex Elder9e15b772012-10-30 19:40:33 -05005976 image_id = kmalloc(image_id_size, GFP_KERNEL);
5977 if (!image_id)
5978 return NULL;
5979
5980 p = image_id;
Alex Elder41579762013-04-21 12:14:45 -05005981 end = image_id + image_id_size;
Alex Elder57385b52013-04-21 12:14:45 -05005982 ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len);
Alex Elder9e15b772012-10-30 19:40:33 -05005983
5984 size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX;
5985 reply_buf = kmalloc(size, GFP_KERNEL);
5986 if (!reply_buf)
5987 goto out;
5988
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005989 ceph_oid_printf(&oid, "%s", RBD_DIRECTORY);
5990 ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc,
5991 "dir_get_name", image_id, image_id_size,
5992 reply_buf, size);
Alex Elder9e15b772012-10-30 19:40:33 -05005993 if (ret < 0)
5994 goto out;
5995 p = reply_buf;
Alex Elderf40eb342013-04-25 15:09:42 -05005996 end = reply_buf + ret;
5997
Alex Elder9e15b772012-10-30 19:40:33 -05005998 image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL);
5999 if (IS_ERR(image_name))
6000 image_name = NULL;
6001 else
6002 dout("%s: name is %s len is %zd\n", __func__, image_name, len);
6003out:
6004 kfree(reply_buf);
6005 kfree(image_id);
6006
6007 return image_name;
6008}
6009
Alex Elder2ad3d712013-04-30 00:44:33 -05006010static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
6011{
6012 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
6013 const char *snap_name;
6014 u32 which = 0;
6015
6016 /* Skip over names until we find the one we are looking for */
6017
6018 snap_name = rbd_dev->header.snap_names;
6019 while (which < snapc->num_snaps) {
6020 if (!strcmp(name, snap_name))
6021 return snapc->snaps[which];
6022 snap_name += strlen(snap_name) + 1;
6023 which++;
6024 }
6025 return CEPH_NOSNAP;
6026}
6027
6028static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
6029{
6030 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
6031 u32 which;
6032 bool found = false;
6033 u64 snap_id;
6034
6035 for (which = 0; !found && which < snapc->num_snaps; which++) {
6036 const char *snap_name;
6037
6038 snap_id = snapc->snaps[which];
6039 snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id);
Josh Durginefadc982013-08-29 19:16:42 -07006040 if (IS_ERR(snap_name)) {
6041 /* ignore no-longer existing snapshots */
6042 if (PTR_ERR(snap_name) == -ENOENT)
6043 continue;
6044 else
6045 break;
6046 }
Alex Elder2ad3d712013-04-30 00:44:33 -05006047 found = !strcmp(name, snap_name);
6048 kfree(snap_name);
6049 }
6050 return found ? snap_id : CEPH_NOSNAP;
6051}
6052
6053/*
6054 * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if
6055 * no snapshot by that name is found, or if an error occurs.
6056 */
6057static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
6058{
6059 if (rbd_dev->image_format == 1)
6060 return rbd_v1_snap_id_by_name(rbd_dev, name);
6061
6062 return rbd_v2_snap_id_by_name(rbd_dev, name);
6063}
6064
Alex Elder9e15b772012-10-30 19:40:33 -05006065/*
Ilya Dryomov04077592014-07-23 17:11:20 +04006066 * An image being mapped will have everything but the snap id.
Alex Elder9e15b772012-10-30 19:40:33 -05006067 */
Ilya Dryomov04077592014-07-23 17:11:20 +04006068static int rbd_spec_fill_snap_id(struct rbd_device *rbd_dev)
6069{
6070 struct rbd_spec *spec = rbd_dev->spec;
6071
6072 rbd_assert(spec->pool_id != CEPH_NOPOOL && spec->pool_name);
6073 rbd_assert(spec->image_id && spec->image_name);
6074 rbd_assert(spec->snap_name);
6075
6076 if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) {
6077 u64 snap_id;
6078
6079 snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name);
6080 if (snap_id == CEPH_NOSNAP)
6081 return -ENOENT;
6082
6083 spec->snap_id = snap_id;
6084 } else {
6085 spec->snap_id = CEPH_NOSNAP;
6086 }
6087
6088 return 0;
6089}
6090
6091/*
6092 * A parent image will have all ids but none of the names.
6093 *
6094 * All names in an rbd spec are dynamically allocated. It's OK if we
6095 * can't figure out the name for an image id.
6096 */
6097static int rbd_spec_fill_names(struct rbd_device *rbd_dev)
Alex Elder9e15b772012-10-30 19:40:33 -05006098{
Alex Elder2e9f7f12013-04-26 09:43:48 -05006099 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
6100 struct rbd_spec *spec = rbd_dev->spec;
6101 const char *pool_name;
6102 const char *image_name;
6103 const char *snap_name;
Alex Elder9e15b772012-10-30 19:40:33 -05006104 int ret;
6105
Ilya Dryomov04077592014-07-23 17:11:20 +04006106 rbd_assert(spec->pool_id != CEPH_NOPOOL);
6107 rbd_assert(spec->image_id);
6108 rbd_assert(spec->snap_id != CEPH_NOSNAP);
Alex Elder9e15b772012-10-30 19:40:33 -05006109
Alex Elder2e9f7f12013-04-26 09:43:48 -05006110 /* Get the pool name; we have to make our own copy of this */
Alex Elder9e15b772012-10-30 19:40:33 -05006111
Alex Elder2e9f7f12013-04-26 09:43:48 -05006112 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id);
6113 if (!pool_name) {
6114 rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id);
Alex Elder935dc892012-11-01 10:17:15 -05006115 return -EIO;
6116 }
Alex Elder2e9f7f12013-04-26 09:43:48 -05006117 pool_name = kstrdup(pool_name, GFP_KERNEL);
6118 if (!pool_name)
Alex Elder9e15b772012-10-30 19:40:33 -05006119 return -ENOMEM;
6120
6121 /* Fetch the image name; tolerate failure here */
6122
Alex Elder2e9f7f12013-04-26 09:43:48 -05006123 image_name = rbd_dev_image_name(rbd_dev);
6124 if (!image_name)
Alex Elder06ecc6c2012-11-01 10:17:15 -05006125 rbd_warn(rbd_dev, "unable to get image name");
Alex Elder9e15b772012-10-30 19:40:33 -05006126
Ilya Dryomov04077592014-07-23 17:11:20 +04006127 /* Fetch the snapshot name */
Alex Elder9e15b772012-10-30 19:40:33 -05006128
Alex Elder2e9f7f12013-04-26 09:43:48 -05006129 snap_name = rbd_snap_name(rbd_dev, spec->snap_id);
Josh Durginda6a6b62013-09-04 17:57:31 -07006130 if (IS_ERR(snap_name)) {
6131 ret = PTR_ERR(snap_name);
Alex Elder9e15b772012-10-30 19:40:33 -05006132 goto out_err;
Alex Elder2e9f7f12013-04-26 09:43:48 -05006133 }
6134
6135 spec->pool_name = pool_name;
6136 spec->image_name = image_name;
6137 spec->snap_name = snap_name;
Alex Elder9e15b772012-10-30 19:40:33 -05006138
6139 return 0;
Ilya Dryomov04077592014-07-23 17:11:20 +04006140
Alex Elder9e15b772012-10-30 19:40:33 -05006141out_err:
Alex Elder2e9f7f12013-04-26 09:43:48 -05006142 kfree(image_name);
6143 kfree(pool_name);
Alex Elder9e15b772012-10-30 19:40:33 -05006144 return ret;
6145}
6146
Alex Eldercc4a38bd2013-04-30 00:44:33 -05006147static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev)
Alex Elder35d489f2012-07-03 16:01:19 -05006148{
6149 size_t size;
6150 int ret;
6151 void *reply_buf;
6152 void *p;
6153 void *end;
6154 u64 seq;
6155 u32 snap_count;
6156 struct ceph_snap_context *snapc;
6157 u32 i;
6158
6159 /*
6160 * We'll need room for the seq value (maximum snapshot id),
6161 * snapshot count, and array of that many snapshot ids.
6162 * For now we have a fixed upper limit on the number we're
6163 * prepared to receive.
6164 */
6165 size = sizeof (__le64) + sizeof (__le32) +
6166 RBD_MAX_SNAP_COUNT * sizeof (__le64);
6167 reply_buf = kzalloc(size, GFP_KERNEL);
6168 if (!reply_buf)
6169 return -ENOMEM;
6170
Ilya Dryomovecd4a682017-01-25 18:16:21 +01006171 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
6172 &rbd_dev->header_oloc, "get_snapcontext",
6173 NULL, 0, reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06006174 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder35d489f2012-07-03 16:01:19 -05006175 if (ret < 0)
6176 goto out;
6177
Alex Elder35d489f2012-07-03 16:01:19 -05006178 p = reply_buf;
Alex Elder57385b52013-04-21 12:14:45 -05006179 end = reply_buf + ret;
6180 ret = -ERANGE;
Alex Elder35d489f2012-07-03 16:01:19 -05006181 ceph_decode_64_safe(&p, end, seq, out);
6182 ceph_decode_32_safe(&p, end, snap_count, out);
6183
6184 /*
6185 * Make sure the reported number of snapshot ids wouldn't go
6186 * beyond the end of our buffer. But before checking that,
6187 * make sure the computed size of the snapshot context we
6188 * allocate is representable in a size_t.
6189 */
6190 if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
6191 / sizeof (u64)) {
6192 ret = -EINVAL;
6193 goto out;
6194 }
6195 if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
6196 goto out;
Alex Elder468521c2013-04-26 09:43:47 -05006197 ret = 0;
Alex Elder35d489f2012-07-03 16:01:19 -05006198
Alex Elder812164f82013-04-30 00:44:32 -05006199 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
Alex Elder35d489f2012-07-03 16:01:19 -05006200 if (!snapc) {
6201 ret = -ENOMEM;
6202 goto out;
6203 }
Alex Elder35d489f2012-07-03 16:01:19 -05006204 snapc->seq = seq;
Alex Elder35d489f2012-07-03 16:01:19 -05006205 for (i = 0; i < snap_count; i++)
6206 snapc->snaps[i] = ceph_decode_64(&p);
6207
Alex Elder49ece552013-05-06 08:37:00 -05006208 ceph_put_snap_context(rbd_dev->header.snapc);
Alex Elder35d489f2012-07-03 16:01:19 -05006209 rbd_dev->header.snapc = snapc;
6210
6211 dout(" snap context seq = %llu, snap_count = %u\n",
Alex Elder57385b52013-04-21 12:14:45 -05006212 (unsigned long long)seq, (unsigned int)snap_count);
Alex Elder35d489f2012-07-03 16:01:19 -05006213out:
6214 kfree(reply_buf);
6215
Alex Elder57385b52013-04-21 12:14:45 -05006216 return ret;
Alex Elder35d489f2012-07-03 16:01:19 -05006217}
6218
Alex Elder54cac612013-04-30 00:44:33 -05006219static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
6220 u64 snap_id)
Alex Elderb8b1e2d2012-07-03 16:01:19 -05006221{
6222 size_t size;
6223 void *reply_buf;
Alex Elder54cac612013-04-30 00:44:33 -05006224 __le64 snapid;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05006225 int ret;
6226 void *p;
6227 void *end;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05006228 char *snap_name;
6229
6230 size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN;
6231 reply_buf = kmalloc(size, GFP_KERNEL);
6232 if (!reply_buf)
6233 return ERR_PTR(-ENOMEM);
6234
Alex Elder54cac612013-04-30 00:44:33 -05006235 snapid = cpu_to_le64(snap_id);
Ilya Dryomovecd4a682017-01-25 18:16:21 +01006236 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
6237 &rbd_dev->header_oloc, "get_snapshot_name",
6238 &snapid, sizeof(snapid), reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06006239 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderf40eb342013-04-25 15:09:42 -05006240 if (ret < 0) {
6241 snap_name = ERR_PTR(ret);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05006242 goto out;
Alex Elderf40eb342013-04-25 15:09:42 -05006243 }
Alex Elderb8b1e2d2012-07-03 16:01:19 -05006244
6245 p = reply_buf;
Alex Elderf40eb342013-04-25 15:09:42 -05006246 end = reply_buf + ret;
Alex Eldere5c35532012-10-25 23:34:41 -05006247 snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elderf40eb342013-04-25 15:09:42 -05006248 if (IS_ERR(snap_name))
Alex Elderb8b1e2d2012-07-03 16:01:19 -05006249 goto out;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05006250
Alex Elderf40eb342013-04-25 15:09:42 -05006251 dout(" snap_id 0x%016llx snap_name = %s\n",
Alex Elder54cac612013-04-30 00:44:33 -05006252 (unsigned long long)snap_id, snap_name);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05006253out:
6254 kfree(reply_buf);
6255
Alex Elderf40eb342013-04-25 15:09:42 -05006256 return snap_name;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05006257}
6258
Alex Elder2df3fac2013-05-06 09:51:30 -05006259static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev)
Alex Elder117973f2012-08-31 17:29:55 -05006260{
Alex Elder2df3fac2013-05-06 09:51:30 -05006261 bool first_time = rbd_dev->header.object_prefix == NULL;
Alex Elder117973f2012-08-31 17:29:55 -05006262 int ret;
Alex Elder117973f2012-08-31 17:29:55 -05006263
Josh Durgin1617e402013-06-12 14:43:10 -07006264 ret = rbd_dev_v2_image_size(rbd_dev);
6265 if (ret)
Alex Eldercfbf6372013-05-31 17:40:45 -05006266 return ret;
Josh Durgin1617e402013-06-12 14:43:10 -07006267
Alex Elder2df3fac2013-05-06 09:51:30 -05006268 if (first_time) {
6269 ret = rbd_dev_v2_header_onetime(rbd_dev);
6270 if (ret)
Alex Eldercfbf6372013-05-31 17:40:45 -05006271 return ret;
Alex Elder2df3fac2013-05-06 09:51:30 -05006272 }
6273
Alex Eldercc4a38bd2013-04-30 00:44:33 -05006274 ret = rbd_dev_v2_snap_context(rbd_dev);
Ilya Dryomovd194cd12015-08-31 18:22:10 +03006275 if (ret && first_time) {
6276 kfree(rbd_dev->header.object_prefix);
6277 rbd_dev->header.object_prefix = NULL;
6278 }
Alex Elder117973f2012-08-31 17:29:55 -05006279
6280 return ret;
6281}
6282
Ilya Dryomova720ae02014-07-23 17:11:19 +04006283static int rbd_dev_header_info(struct rbd_device *rbd_dev)
6284{
6285 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
6286
6287 if (rbd_dev->image_format == 1)
6288 return rbd_dev_v1_header_info(rbd_dev);
6289
6290 return rbd_dev_v2_header_info(rbd_dev);
6291}
6292
Alex Elder1ddbe942012-01-29 13:57:44 -06006293/*
Alex Eldere28fff262012-02-02 08:13:30 -06006294 * Skips over white space at *buf, and updates *buf to point to the
6295 * first found non-space character (if any). Returns the length of
Alex Elder593a9e72012-02-07 12:03:37 -06006296 * the token (string of non-white space characters) found. Note
6297 * that *buf must be terminated with '\0'.
Alex Eldere28fff262012-02-02 08:13:30 -06006298 */
6299static inline size_t next_token(const char **buf)
6300{
6301 /*
6302 * These are the characters that produce nonzero for
6303 * isspace() in the "C" and "POSIX" locales.
6304 */
6305 const char *spaces = " \f\n\r\t\v";
6306
6307 *buf += strspn(*buf, spaces); /* Find start of token */
6308
6309 return strcspn(*buf, spaces); /* Return token length */
6310}
6311
6312/*
Alex Elderea3352f2012-07-09 21:04:23 -05006313 * Finds the next token in *buf, dynamically allocates a buffer big
6314 * enough to hold a copy of it, and copies the token into the new
6315 * buffer. The copy is guaranteed to be terminated with '\0'. Note
6316 * that a duplicate buffer is created even for a zero-length token.
6317 *
6318 * Returns a pointer to the newly-allocated duplicate, or a null
6319 * pointer if memory for the duplicate was not available. If
6320 * the lenp argument is a non-null pointer, the length of the token
6321 * (not including the '\0') is returned in *lenp.
6322 *
6323 * If successful, the *buf pointer will be updated to point beyond
6324 * the end of the found token.
6325 *
6326 * Note: uses GFP_KERNEL for allocation.
6327 */
6328static inline char *dup_token(const char **buf, size_t *lenp)
6329{
6330 char *dup;
6331 size_t len;
6332
6333 len = next_token(buf);
Alex Elder4caf35f2012-11-01 08:39:27 -05006334 dup = kmemdup(*buf, len + 1, GFP_KERNEL);
Alex Elderea3352f2012-07-09 21:04:23 -05006335 if (!dup)
6336 return NULL;
Alex Elderea3352f2012-07-09 21:04:23 -05006337 *(dup + len) = '\0';
6338 *buf += len;
6339
6340 if (lenp)
6341 *lenp = len;
6342
6343 return dup;
6344}
6345
David Howells82995cc2019-03-25 16:38:32 +00006346static int rbd_parse_param(struct fs_parameter *param,
6347 struct rbd_parse_opts_ctx *pctx)
6348{
6349 struct rbd_options *opt = pctx->opts;
6350 struct fs_parse_result result;
Al Viro3fbb8d52019-12-20 23:43:32 -05006351 struct p_log log = {.prefix = "rbd"};
David Howells82995cc2019-03-25 16:38:32 +00006352 int token, ret;
6353
6354 ret = ceph_parse_param(param, pctx->copts, NULL);
6355 if (ret != -ENOPARAM)
6356 return ret;
6357
Al Virod7167b12019-09-07 07:23:15 -04006358 token = __fs_parse(&log, rbd_parameters, param, &result);
David Howells82995cc2019-03-25 16:38:32 +00006359 dout("%s fs_parse '%s' token %d\n", __func__, param->key, token);
6360 if (token < 0) {
Al Viro2c3f3dc2019-12-20 23:43:32 -05006361 if (token == -ENOPARAM)
6362 return inval_plog(&log, "Unknown parameter '%s'",
6363 param->key);
David Howells82995cc2019-03-25 16:38:32 +00006364 return token;
6365 }
6366
6367 switch (token) {
6368 case Opt_queue_depth:
6369 if (result.uint_32 < 1)
6370 goto out_of_range;
6371 opt->queue_depth = result.uint_32;
6372 break;
6373 case Opt_alloc_size:
6374 if (result.uint_32 < SECTOR_SIZE)
6375 goto out_of_range;
Al Viro2c3f3dc2019-12-20 23:43:32 -05006376 if (!is_power_of_2(result.uint_32))
6377 return inval_plog(&log, "alloc_size must be a power of 2");
David Howells82995cc2019-03-25 16:38:32 +00006378 opt->alloc_size = result.uint_32;
6379 break;
6380 case Opt_lock_timeout:
6381 /* 0 is "wait forever" (i.e. infinite timeout) */
6382 if (result.uint_32 > INT_MAX / 1000)
6383 goto out_of_range;
6384 opt->lock_timeout = msecs_to_jiffies(result.uint_32 * 1000);
6385 break;
6386 case Opt_pool_ns:
6387 kfree(pctx->spec->pool_ns);
6388 pctx->spec->pool_ns = param->string;
6389 param->string = NULL;
6390 break;
6391 case Opt_read_only:
6392 opt->read_only = true;
6393 break;
6394 case Opt_read_write:
6395 opt->read_only = false;
6396 break;
6397 case Opt_lock_on_read:
6398 opt->lock_on_read = true;
6399 break;
6400 case Opt_exclusive:
6401 opt->exclusive = true;
6402 break;
6403 case Opt_notrim:
6404 opt->trim = false;
6405 break;
6406 default:
6407 BUG();
6408 }
6409
6410 return 0;
6411
6412out_of_range:
Al Viro2c3f3dc2019-12-20 23:43:32 -05006413 return inval_plog(&log, "%s out of range", param->key);
David Howells82995cc2019-03-25 16:38:32 +00006414}
6415
6416/*
6417 * This duplicates most of generic_parse_monolithic(), untying it from
6418 * fs_context and skipping standard superblock and security options.
6419 */
6420static int rbd_parse_options(char *options, struct rbd_parse_opts_ctx *pctx)
6421{
6422 char *key;
6423 int ret = 0;
6424
6425 dout("%s '%s'\n", __func__, options);
6426 while ((key = strsep(&options, ",")) != NULL) {
6427 if (*key) {
6428 struct fs_parameter param = {
6429 .key = key,
Al Viro0f895892019-12-17 14:15:04 -05006430 .type = fs_value_is_flag,
David Howells82995cc2019-03-25 16:38:32 +00006431 };
6432 char *value = strchr(key, '=');
6433 size_t v_len = 0;
6434
6435 if (value) {
6436 if (value == key)
6437 continue;
6438 *value++ = 0;
6439 v_len = strlen(value);
David Howells82995cc2019-03-25 16:38:32 +00006440 param.string = kmemdup_nul(value, v_len,
6441 GFP_KERNEL);
6442 if (!param.string)
6443 return -ENOMEM;
Al Viro0f895892019-12-17 14:15:04 -05006444 param.type = fs_value_is_string;
David Howells82995cc2019-03-25 16:38:32 +00006445 }
6446 param.size = v_len;
6447
6448 ret = rbd_parse_param(&param, pctx);
6449 kfree(param.string);
6450 if (ret)
6451 break;
6452 }
6453 }
6454
6455 return ret;
6456}
6457
Alex Elderea3352f2012-07-09 21:04:23 -05006458/*
Alex Elder859c31d2012-10-25 23:34:42 -05006459 * Parse the options provided for an "rbd add" (i.e., rbd image
6460 * mapping) request. These arrive via a write to /sys/bus/rbd/add,
6461 * and the data written is passed here via a NUL-terminated buffer.
6462 * Returns 0 if successful or an error code otherwise.
Alex Elderd22f76e2012-07-12 10:46:35 -05006463 *
Alex Elder859c31d2012-10-25 23:34:42 -05006464 * The information extracted from these options is recorded in
6465 * the other parameters which return dynamically-allocated
6466 * structures:
6467 * ceph_opts
6468 * The address of a pointer that will refer to a ceph options
6469 * structure. Caller must release the returned pointer using
6470 * ceph_destroy_options() when it is no longer needed.
6471 * rbd_opts
6472 * Address of an rbd options pointer. Fully initialized by
6473 * this function; caller must release with kfree().
6474 * spec
6475 * Address of an rbd image specification pointer. Fully
6476 * initialized by this function based on parsed options.
6477 * Caller must release with rbd_spec_put().
6478 *
6479 * The options passed take this form:
6480 * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
6481 * where:
6482 * <mon_addrs>
6483 * A comma-separated list of one or more monitor addresses.
6484 * A monitor address is an ip address, optionally followed
6485 * by a port number (separated by a colon).
6486 * I.e.: ip1[:port1][,ip2[:port2]...]
6487 * <options>
6488 * A comma-separated list of ceph and/or rbd options.
6489 * <pool_name>
6490 * The name of the rados pool containing the rbd image.
6491 * <image_name>
6492 * The name of the image in that pool to map.
6493 * <snap_id>
6494 * An optional snapshot id. If provided, the mapping will
6495 * present data from the image at the time that snapshot was
6496 * created. The image head is used if no snapshot id is
6497 * provided. Snapshot mappings are always read-only.
Alex Eldera725f65e2012-02-02 08:13:30 -06006498 */
Alex Elder859c31d2012-10-25 23:34:42 -05006499static int rbd_add_parse_args(const char *buf,
Alex Elderdc79b112012-10-25 23:34:41 -05006500 struct ceph_options **ceph_opts,
Alex Elder859c31d2012-10-25 23:34:42 -05006501 struct rbd_options **opts,
6502 struct rbd_spec **rbd_spec)
Alex Eldera725f65e2012-02-02 08:13:30 -06006503{
Alex Elderd22f76e2012-07-12 10:46:35 -05006504 size_t len;
Alex Elder859c31d2012-10-25 23:34:42 -05006505 char *options;
Alex Elder0ddebc02012-10-25 23:34:41 -05006506 const char *mon_addrs;
Alex Elderecb4dc222013-04-26 09:43:47 -05006507 char *snap_name;
Alex Elder0ddebc02012-10-25 23:34:41 -05006508 size_t mon_addrs_size;
David Howells82995cc2019-03-25 16:38:32 +00006509 struct rbd_parse_opts_ctx pctx = { 0 };
Alex Elderdc79b112012-10-25 23:34:41 -05006510 int ret;
Alex Eldere28fff262012-02-02 08:13:30 -06006511
6512 /* The first four tokens are required */
6513
Alex Elder7ef32142012-02-02 08:13:30 -06006514 len = next_token(&buf);
Alex Elder4fb5d6712012-11-01 10:17:15 -05006515 if (!len) {
6516 rbd_warn(NULL, "no monitor address(es) provided");
6517 return -EINVAL;
6518 }
Alex Elder0ddebc02012-10-25 23:34:41 -05006519 mon_addrs = buf;
David Howells82995cc2019-03-25 16:38:32 +00006520 mon_addrs_size = len;
Alex Elder7ef32142012-02-02 08:13:30 -06006521 buf += len;
Alex Eldera725f65e2012-02-02 08:13:30 -06006522
Alex Elderdc79b112012-10-25 23:34:41 -05006523 ret = -EINVAL;
Alex Elderf28e5652012-10-25 23:34:41 -05006524 options = dup_token(&buf, NULL);
6525 if (!options)
Alex Elderdc79b112012-10-25 23:34:41 -05006526 return -ENOMEM;
Alex Elder4fb5d6712012-11-01 10:17:15 -05006527 if (!*options) {
6528 rbd_warn(NULL, "no options provided");
6529 goto out_err;
6530 }
Alex Eldera725f65e2012-02-02 08:13:30 -06006531
Ilya Dryomovc3001562018-07-03 15:28:43 +02006532 pctx.spec = rbd_spec_alloc();
6533 if (!pctx.spec)
Alex Elderf28e5652012-10-25 23:34:41 -05006534 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05006535
Ilya Dryomovc3001562018-07-03 15:28:43 +02006536 pctx.spec->pool_name = dup_token(&buf, NULL);
6537 if (!pctx.spec->pool_name)
Alex Elder859c31d2012-10-25 23:34:42 -05006538 goto out_mem;
Ilya Dryomovc3001562018-07-03 15:28:43 +02006539 if (!*pctx.spec->pool_name) {
Alex Elder4fb5d6712012-11-01 10:17:15 -05006540 rbd_warn(NULL, "no pool name provided");
6541 goto out_err;
6542 }
Alex Eldere28fff262012-02-02 08:13:30 -06006543
Ilya Dryomovc3001562018-07-03 15:28:43 +02006544 pctx.spec->image_name = dup_token(&buf, NULL);
6545 if (!pctx.spec->image_name)
Alex Elderf28e5652012-10-25 23:34:41 -05006546 goto out_mem;
Ilya Dryomovc3001562018-07-03 15:28:43 +02006547 if (!*pctx.spec->image_name) {
Alex Elder4fb5d6712012-11-01 10:17:15 -05006548 rbd_warn(NULL, "no image name provided");
6549 goto out_err;
6550 }
Alex Eldere28fff262012-02-02 08:13:30 -06006551
Alex Elderf28e5652012-10-25 23:34:41 -05006552 /*
6553 * Snapshot name is optional; default is to use "-"
6554 * (indicating the head/no snapshot).
6555 */
Alex Elder3feeb8942012-08-31 17:29:52 -05006556 len = next_token(&buf);
Alex Elder820a5f32012-07-09 21:04:24 -05006557 if (!len) {
Alex Elder3feeb8942012-08-31 17:29:52 -05006558 buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
6559 len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
Alex Elderf28e5652012-10-25 23:34:41 -05006560 } else if (len > RBD_MAX_SNAP_NAME_LEN) {
Alex Elderdc79b112012-10-25 23:34:41 -05006561 ret = -ENAMETOOLONG;
Alex Elderf28e5652012-10-25 23:34:41 -05006562 goto out_err;
Alex Elder849b4262012-07-09 21:04:24 -05006563 }
Alex Elderecb4dc222013-04-26 09:43:47 -05006564 snap_name = kmemdup(buf, len + 1, GFP_KERNEL);
6565 if (!snap_name)
Alex Elderf28e5652012-10-25 23:34:41 -05006566 goto out_mem;
Alex Elderecb4dc222013-04-26 09:43:47 -05006567 *(snap_name + len) = '\0';
Ilya Dryomovc3001562018-07-03 15:28:43 +02006568 pctx.spec->snap_name = snap_name;
Alex Eldere5c35532012-10-25 23:34:41 -05006569
David Howells82995cc2019-03-25 16:38:32 +00006570 pctx.copts = ceph_alloc_options();
6571 if (!pctx.copts)
6572 goto out_mem;
6573
Alex Elder0ddebc02012-10-25 23:34:41 -05006574 /* Initialize all rbd options to the defaults */
Alex Eldere28fff262012-02-02 08:13:30 -06006575
Ilya Dryomovc3001562018-07-03 15:28:43 +02006576 pctx.opts = kzalloc(sizeof(*pctx.opts), GFP_KERNEL);
6577 if (!pctx.opts)
Alex Elder4e9afeb2012-10-25 23:34:41 -05006578 goto out_mem;
6579
Ilya Dryomovc3001562018-07-03 15:28:43 +02006580 pctx.opts->read_only = RBD_READ_ONLY_DEFAULT;
6581 pctx.opts->queue_depth = RBD_QUEUE_DEPTH_DEFAULT;
Ilya Dryomov0c93e1b2019-01-30 15:14:48 +01006582 pctx.opts->alloc_size = RBD_ALLOC_SIZE_DEFAULT;
Ilya Dryomovc3001562018-07-03 15:28:43 +02006583 pctx.opts->lock_timeout = RBD_LOCK_TIMEOUT_DEFAULT;
6584 pctx.opts->lock_on_read = RBD_LOCK_ON_READ_DEFAULT;
6585 pctx.opts->exclusive = RBD_EXCLUSIVE_DEFAULT;
6586 pctx.opts->trim = RBD_TRIM_DEFAULT;
Alex Elderd22f76e2012-07-12 10:46:35 -05006587
David Howells82995cc2019-03-25 16:38:32 +00006588 ret = ceph_parse_mon_ips(mon_addrs, mon_addrs_size, pctx.copts, NULL);
6589 if (ret)
Alex Elderdc79b112012-10-25 23:34:41 -05006590 goto out_err;
Alex Elder859c31d2012-10-25 23:34:42 -05006591
David Howells82995cc2019-03-25 16:38:32 +00006592 ret = rbd_parse_options(options, &pctx);
6593 if (ret)
6594 goto out_err;
6595
6596 *ceph_opts = pctx.copts;
Ilya Dryomovc3001562018-07-03 15:28:43 +02006597 *opts = pctx.opts;
6598 *rbd_spec = pctx.spec;
David Howells82995cc2019-03-25 16:38:32 +00006599 kfree(options);
Alex Elderdc79b112012-10-25 23:34:41 -05006600 return 0;
David Howells82995cc2019-03-25 16:38:32 +00006601
Alex Elderf28e5652012-10-25 23:34:41 -05006602out_mem:
Alex Elderdc79b112012-10-25 23:34:41 -05006603 ret = -ENOMEM;
Alex Elderd22f76e2012-07-12 10:46:35 -05006604out_err:
Ilya Dryomovc3001562018-07-03 15:28:43 +02006605 kfree(pctx.opts);
David Howells82995cc2019-03-25 16:38:32 +00006606 ceph_destroy_options(pctx.copts);
Ilya Dryomovc3001562018-07-03 15:28:43 +02006607 rbd_spec_put(pctx.spec);
Alex Elderf28e5652012-10-25 23:34:41 -05006608 kfree(options);
Alex Elderdc79b112012-10-25 23:34:41 -05006609 return ret;
Alex Eldera725f65e2012-02-02 08:13:30 -06006610}
6611
Ilya Dryomove010dd02017-04-13 12:17:39 +02006612static void rbd_dev_image_unlock(struct rbd_device *rbd_dev)
6613{
6614 down_write(&rbd_dev->lock_rwsem);
6615 if (__rbd_is_lock_owner(rbd_dev))
Ilya Dryomove1fddc82019-05-30 16:07:48 +02006616 __rbd_release_lock(rbd_dev);
Ilya Dryomove010dd02017-04-13 12:17:39 +02006617 up_write(&rbd_dev->lock_rwsem);
6618}
6619
Ilya Dryomov637cd062019-06-06 17:14:49 +02006620/*
6621 * If the wait is interrupted, an error is returned even if the lock
6622 * was successfully acquired. rbd_dev_image_unlock() will release it
6623 * if needed.
6624 */
Ilya Dryomove010dd02017-04-13 12:17:39 +02006625static int rbd_add_acquire_lock(struct rbd_device *rbd_dev)
6626{
Ilya Dryomov637cd062019-06-06 17:14:49 +02006627 long ret;
Ilya Dryomov2f18d462018-04-04 10:15:38 +02006628
Ilya Dryomove010dd02017-04-13 12:17:39 +02006629 if (!(rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK)) {
Ilya Dryomov637cd062019-06-06 17:14:49 +02006630 if (!rbd_dev->opts->exclusive && !rbd_dev->opts->lock_on_read)
6631 return 0;
6632
Ilya Dryomove010dd02017-04-13 12:17:39 +02006633 rbd_warn(rbd_dev, "exclusive-lock feature is not enabled");
6634 return -EINVAL;
6635 }
6636
Ilya Dryomov3fe69922019-11-12 19:41:48 +01006637 if (rbd_is_ro(rbd_dev))
Ilya Dryomov637cd062019-06-06 17:14:49 +02006638 return 0;
6639
6640 rbd_assert(!rbd_is_lock_owner(rbd_dev));
6641 queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0);
6642 ret = wait_for_completion_killable_timeout(&rbd_dev->acquire_wait,
6643 ceph_timeout_jiffies(rbd_dev->opts->lock_timeout));
Dongsheng Yang25e6be22019-09-27 15:33:22 +00006644 if (ret > 0) {
Ilya Dryomov637cd062019-06-06 17:14:49 +02006645 ret = rbd_dev->acquire_err;
Dongsheng Yang25e6be22019-09-27 15:33:22 +00006646 } else {
6647 cancel_delayed_work_sync(&rbd_dev->lock_dwork);
6648 if (!ret)
6649 ret = -ETIMEDOUT;
6650 }
Ilya Dryomov637cd062019-06-06 17:14:49 +02006651
Ilya Dryomov2f18d462018-04-04 10:15:38 +02006652 if (ret) {
Ilya Dryomov637cd062019-06-06 17:14:49 +02006653 rbd_warn(rbd_dev, "failed to acquire exclusive lock: %ld", ret);
6654 return ret;
Ilya Dryomove010dd02017-04-13 12:17:39 +02006655 }
6656
Ilya Dryomov637cd062019-06-06 17:14:49 +02006657 /*
6658 * The lock may have been released by now, unless automatic lock
6659 * transitions are disabled.
6660 */
6661 rbd_assert(!rbd_dev->opts->exclusive || rbd_is_lock_owner(rbd_dev));
Ilya Dryomove010dd02017-04-13 12:17:39 +02006662 return 0;
6663}
6664
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04006665/*
Alex Elder589d30e2012-07-10 20:30:11 -05006666 * An rbd format 2 image has a unique identifier, distinct from the
6667 * name given to it by the user. Internally, that identifier is
6668 * what's used to specify the names of objects related to the image.
6669 *
6670 * A special "rbd id" object is used to map an rbd image name to its
6671 * id. If that object doesn't exist, then there is no v2 rbd image
6672 * with the supplied name.
6673 *
6674 * This function will record the given rbd_dev's image_id field if
6675 * it can be determined, and in that case will return 0. If any
6676 * errors occur a negative errno will be returned and the rbd_dev's
6677 * image_id field will be unchanged (and should be NULL).
6678 */
6679static int rbd_dev_image_id(struct rbd_device *rbd_dev)
6680{
6681 int ret;
6682 size_t size;
Ilya Dryomovecd4a682017-01-25 18:16:21 +01006683 CEPH_DEFINE_OID_ONSTACK(oid);
Alex Elder589d30e2012-07-10 20:30:11 -05006684 void *response;
Alex Elderc0fba362013-04-25 23:15:08 -05006685 char *image_id;
Alex Elder2f82ee52012-10-30 19:40:33 -05006686
Alex Elder589d30e2012-07-10 20:30:11 -05006687 /*
Alex Elder2c0d0a12012-10-30 19:40:33 -05006688 * When probing a parent image, the image id is already
6689 * known (and the image name likely is not). There's no
Alex Elderc0fba362013-04-25 23:15:08 -05006690 * need to fetch the image id again in this case. We
6691 * do still need to set the image format though.
Alex Elder2c0d0a12012-10-30 19:40:33 -05006692 */
Alex Elderc0fba362013-04-25 23:15:08 -05006693 if (rbd_dev->spec->image_id) {
6694 rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1;
6695
Alex Elder2c0d0a12012-10-30 19:40:33 -05006696 return 0;
Alex Elderc0fba362013-04-25 23:15:08 -05006697 }
Alex Elder2c0d0a12012-10-30 19:40:33 -05006698
6699 /*
Alex Elder589d30e2012-07-10 20:30:11 -05006700 * First, see if the format 2 image id file exists, and if
6701 * so, get the image's persistent id from it.
6702 */
Ilya Dryomovecd4a682017-01-25 18:16:21 +01006703 ret = ceph_oid_aprintf(&oid, GFP_KERNEL, "%s%s", RBD_ID_PREFIX,
6704 rbd_dev->spec->image_name);
6705 if (ret)
6706 return ret;
6707
6708 dout("rbd id object name is %s\n", oid.name);
Alex Elder589d30e2012-07-10 20:30:11 -05006709
6710 /* Response will be an encoded string, which includes a length */
Alex Elder589d30e2012-07-10 20:30:11 -05006711 size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
6712 response = kzalloc(size, GFP_NOIO);
6713 if (!response) {
6714 ret = -ENOMEM;
6715 goto out;
6716 }
6717
Alex Elderc0fba362013-04-25 23:15:08 -05006718 /* If it doesn't exist we'll assume it's a format 1 image */
6719
Ilya Dryomovecd4a682017-01-25 18:16:21 +01006720 ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc,
6721 "get_id", NULL, 0,
Dongsheng Yang5435d2062019-08-09 07:05:27 +00006722 response, size);
Alex Elder36be9a72013-01-19 00:30:28 -06006723 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderc0fba362013-04-25 23:15:08 -05006724 if (ret == -ENOENT) {
6725 image_id = kstrdup("", GFP_KERNEL);
6726 ret = image_id ? 0 : -ENOMEM;
6727 if (!ret)
6728 rbd_dev->image_format = 1;
Ilya Dryomov7dd440c2014-09-11 18:49:18 +04006729 } else if (ret >= 0) {
Alex Elderc0fba362013-04-25 23:15:08 -05006730 void *p = response;
Alex Elder589d30e2012-07-10 20:30:11 -05006731
Alex Elderc0fba362013-04-25 23:15:08 -05006732 image_id = ceph_extract_encoded_string(&p, p + ret,
Alex Elder979ed482012-11-01 08:39:26 -05006733 NULL, GFP_NOIO);
Duan Jiong461f7582014-04-11 16:38:12 +08006734 ret = PTR_ERR_OR_ZERO(image_id);
Alex Elderc0fba362013-04-25 23:15:08 -05006735 if (!ret)
6736 rbd_dev->image_format = 2;
Alex Elderc0fba362013-04-25 23:15:08 -05006737 }
6738
6739 if (!ret) {
6740 rbd_dev->spec->image_id = image_id;
6741 dout("image_id is %s\n", image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05006742 }
6743out:
6744 kfree(response);
Ilya Dryomovecd4a682017-01-25 18:16:21 +01006745 ceph_oid_destroy(&oid);
Alex Elder589d30e2012-07-10 20:30:11 -05006746 return ret;
6747}
6748
Alex Elder3abef3b2013-05-13 20:35:37 -05006749/*
6750 * Undo whatever state changes are made by v1 or v2 header info
6751 * call.
6752 */
Alex Elder6fd48b32013-04-28 23:32:34 -05006753static void rbd_dev_unprobe(struct rbd_device *rbd_dev)
6754{
6755 struct rbd_image_header *header;
6756
Ilya Dryomove69b8d42015-01-19 12:06:14 +03006757 rbd_dev_parent_put(rbd_dev);
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02006758 rbd_object_map_free(rbd_dev);
Ilya Dryomovda5ef6be2019-06-17 15:29:49 +02006759 rbd_dev_mapping_clear(rbd_dev);
Alex Elder6fd48b32013-04-28 23:32:34 -05006760
6761 /* Free dynamic fields from the header, then zero it out */
6762
6763 header = &rbd_dev->header;
Alex Elder812164f82013-04-30 00:44:32 -05006764 ceph_put_snap_context(header->snapc);
Alex Elder6fd48b32013-04-28 23:32:34 -05006765 kfree(header->snap_sizes);
6766 kfree(header->snap_names);
6767 kfree(header->object_prefix);
6768 memset(header, 0, sizeof (*header));
6769}
6770
Alex Elder2df3fac2013-05-06 09:51:30 -05006771static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev)
Alex Eldera30b71b2012-07-10 20:30:11 -05006772{
6773 int ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05006774
Alex Elder1e130192012-07-03 16:01:19 -05006775 ret = rbd_dev_v2_object_prefix(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05006776 if (ret)
Alex Elder1e130192012-07-03 16:01:19 -05006777 goto out_err;
Alex Elderb1b54022012-07-03 16:01:19 -05006778
Alex Elder2df3fac2013-05-06 09:51:30 -05006779 /*
6780 * Get the and check features for the image. Currently the
6781 * features are assumed to never change.
6782 */
Alex Elderb1b54022012-07-03 16:01:19 -05006783 ret = rbd_dev_v2_features(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05006784 if (ret)
Alex Elderb1b54022012-07-03 16:01:19 -05006785 goto out_err;
Alex Elder35d489f2012-07-03 16:01:19 -05006786
Alex Eldercc070d52013-04-21 12:14:45 -05006787 /* If the image supports fancy striping, get its parameters */
6788
6789 if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) {
6790 ret = rbd_dev_v2_striping_info(rbd_dev);
6791 if (ret < 0)
6792 goto out_err;
6793 }
Alex Eldera30b71b2012-07-10 20:30:11 -05006794
Ilya Dryomov7e973322017-01-25 18:16:22 +01006795 if (rbd_dev->header.features & RBD_FEATURE_DATA_POOL) {
6796 ret = rbd_dev_v2_data_pool(rbd_dev);
6797 if (ret)
6798 goto out_err;
6799 }
6800
Ilya Dryomov263423f2017-01-25 18:16:22 +01006801 rbd_init_layout(rbd_dev);
Alex Elder35152972012-08-31 17:29:55 -05006802 return 0;
Ilya Dryomov263423f2017-01-25 18:16:22 +01006803
Alex Elder9d475de2012-07-03 16:01:19 -05006804out_err:
Alex Elder642a2532013-05-06 17:40:33 -05006805 rbd_dev->header.features = 0;
Alex Elder1e130192012-07-03 16:01:19 -05006806 kfree(rbd_dev->header.object_prefix);
6807 rbd_dev->header.object_prefix = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05006808 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05006809}
6810
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006811/*
6812 * @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() ->
6813 * rbd_dev_image_probe() recursion depth, which means it's also the
6814 * length of the already discovered part of the parent chain.
6815 */
6816static int rbd_dev_probe_parent(struct rbd_device *rbd_dev, int depth)
Alex Elder83a06262012-10-30 15:47:17 -05006817{
Alex Elder2f82ee52012-10-30 19:40:33 -05006818 struct rbd_device *parent = NULL;
Alex Elder124afba2013-04-26 15:44:36 -05006819 int ret;
6820
6821 if (!rbd_dev->parent_spec)
6822 return 0;
Alex Elder124afba2013-04-26 15:44:36 -05006823
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006824 if (++depth > RBD_MAX_PARENT_CHAIN_LEN) {
6825 pr_info("parent chain is too long (%d)\n", depth);
6826 ret = -EINVAL;
6827 goto out_err;
6828 }
6829
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02006830 parent = __rbd_dev_create(rbd_dev->rbd_client, rbd_dev->parent_spec);
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02006831 if (!parent) {
6832 ret = -ENOMEM;
Alex Elder124afba2013-04-26 15:44:36 -05006833 goto out_err;
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02006834 }
6835
6836 /*
6837 * Images related by parent/child relationships always share
6838 * rbd_client and spec/parent_spec, so bump their refcounts.
6839 */
6840 __rbd_get_client(rbd_dev->rbd_client);
6841 rbd_spec_get(rbd_dev->parent_spec);
Alex Elder124afba2013-04-26 15:44:36 -05006842
Ilya Dryomov39258aa2019-11-07 17:16:23 +01006843 __set_bit(RBD_DEV_FLAG_READONLY, &parent->flags);
6844
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006845 ret = rbd_dev_image_probe(parent, depth);
Alex Elder124afba2013-04-26 15:44:36 -05006846 if (ret < 0)
6847 goto out_err;
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02006848
Alex Elder124afba2013-04-26 15:44:36 -05006849 rbd_dev->parent = parent;
Alex Eldera2acd002013-05-08 22:50:04 -05006850 atomic_set(&rbd_dev->parent_ref, 1);
Alex Elder124afba2013-04-26 15:44:36 -05006851 return 0;
Alex Elder124afba2013-04-26 15:44:36 -05006852
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02006853out_err:
6854 rbd_dev_unparent(rbd_dev);
Markus Elfring1761b222015-11-23 20:16:45 +01006855 rbd_dev_destroy(parent);
Alex Elder124afba2013-04-26 15:44:36 -05006856 return ret;
6857}
6858
Ilya Dryomov5769ed02017-04-13 12:17:38 +02006859static void rbd_dev_device_release(struct rbd_device *rbd_dev)
6860{
6861 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
Ilya Dryomov5769ed02017-04-13 12:17:38 +02006862 rbd_free_disk(rbd_dev);
6863 if (!single_major)
6864 unregister_blkdev(rbd_dev->major, rbd_dev->name);
6865}
6866
Ilya Dryomov811c6682016-04-15 16:22:16 +02006867/*
6868 * rbd_dev->header_rwsem must be locked for write and will be unlocked
6869 * upon return.
6870 */
Alex Elder200a6a82013-04-28 23:32:34 -05006871static int rbd_dev_device_setup(struct rbd_device *rbd_dev)
Alex Elder124afba2013-04-26 15:44:36 -05006872{
Alex Elder83a06262012-10-30 15:47:17 -05006873 int ret;
Alex Elder83a06262012-10-30 15:47:17 -05006874
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006875 /* Record our major and minor device numbers. */
Alex Elder83a06262012-10-30 15:47:17 -05006876
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006877 if (!single_major) {
6878 ret = register_blkdev(0, rbd_dev->name);
6879 if (ret < 0)
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02006880 goto err_out_unlock;
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006881
6882 rbd_dev->major = ret;
6883 rbd_dev->minor = 0;
6884 } else {
6885 rbd_dev->major = rbd_major;
6886 rbd_dev->minor = rbd_dev_id_to_minor(rbd_dev->dev_id);
6887 }
Alex Elder83a06262012-10-30 15:47:17 -05006888
6889 /* Set up the blkdev mapping. */
6890
6891 ret = rbd_init_disk(rbd_dev);
6892 if (ret)
6893 goto err_out_blkdev;
6894
Alex Elderf35a4de2013-05-06 09:51:29 -05006895 set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
Ilya Dryomov39258aa2019-11-07 17:16:23 +01006896 set_disk_ro(rbd_dev->disk, rbd_is_ro(rbd_dev));
Alex Elderf35a4de2013-05-06 09:51:29 -05006897
Ilya Dryomov5769ed02017-04-13 12:17:38 +02006898 ret = dev_set_name(&rbd_dev->dev, "%d", rbd_dev->dev_id);
Alex Elderf35a4de2013-05-06 09:51:29 -05006899 if (ret)
Ilya Dryomovda5ef6be2019-06-17 15:29:49 +02006900 goto err_out_disk;
Alex Elder83a06262012-10-30 15:47:17 -05006901
Alex Elder129b79d2013-04-26 15:44:36 -05006902 set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
Ilya Dryomov811c6682016-04-15 16:22:16 +02006903 up_write(&rbd_dev->header_rwsem);
Ilya Dryomov5769ed02017-04-13 12:17:38 +02006904 return 0;
Alex Elder2f82ee52012-10-30 19:40:33 -05006905
Alex Elder83a06262012-10-30 15:47:17 -05006906err_out_disk:
6907 rbd_free_disk(rbd_dev);
6908err_out_blkdev:
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006909 if (!single_major)
6910 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Ilya Dryomov811c6682016-04-15 16:22:16 +02006911err_out_unlock:
6912 up_write(&rbd_dev->header_rwsem);
Alex Elder83a06262012-10-30 15:47:17 -05006913 return ret;
6914}
6915
Alex Elder332bb122013-04-27 09:59:30 -05006916static int rbd_dev_header_name(struct rbd_device *rbd_dev)
6917{
6918 struct rbd_spec *spec = rbd_dev->spec;
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006919 int ret;
Alex Elder332bb122013-04-27 09:59:30 -05006920
6921 /* Record the header object name for this rbd image. */
6922
6923 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
Alex Elder332bb122013-04-27 09:59:30 -05006924 if (rbd_dev->image_format == 1)
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006925 ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s",
6926 spec->image_name, RBD_SUFFIX);
Alex Elder332bb122013-04-27 09:59:30 -05006927 else
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006928 ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s",
6929 RBD_HEADER_PREFIX, spec->image_id);
Alex Elder332bb122013-04-27 09:59:30 -05006930
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006931 return ret;
Alex Elder332bb122013-04-27 09:59:30 -05006932}
6933
Ilya Dryomovb9ef2b82019-11-12 20:20:04 +01006934static void rbd_print_dne(struct rbd_device *rbd_dev, bool is_snap)
6935{
6936 if (!is_snap) {
6937 pr_info("image %s/%s%s%s does not exist\n",
6938 rbd_dev->spec->pool_name,
6939 rbd_dev->spec->pool_ns ?: "",
6940 rbd_dev->spec->pool_ns ? "/" : "",
6941 rbd_dev->spec->image_name);
6942 } else {
6943 pr_info("snap %s/%s%s%s@%s does not exist\n",
6944 rbd_dev->spec->pool_name,
6945 rbd_dev->spec->pool_ns ?: "",
6946 rbd_dev->spec->pool_ns ? "/" : "",
6947 rbd_dev->spec->image_name,
6948 rbd_dev->spec->snap_name);
6949 }
6950}
6951
Alex Elder200a6a82013-04-28 23:32:34 -05006952static void rbd_dev_image_release(struct rbd_device *rbd_dev)
6953{
Alex Elder6fd48b32013-04-28 23:32:34 -05006954 rbd_dev_unprobe(rbd_dev);
Ilya Dryomovfd22aef2017-04-13 12:17:37 +02006955 if (rbd_dev->opts)
6956 rbd_unregister_watch(rbd_dev);
Alex Elder6fd48b32013-04-28 23:32:34 -05006957 rbd_dev->image_format = 0;
6958 kfree(rbd_dev->spec->image_id);
6959 rbd_dev->spec->image_id = NULL;
Alex Elder200a6a82013-04-28 23:32:34 -05006960}
6961
Alex Eldera30b71b2012-07-10 20:30:11 -05006962/*
6963 * Probe for the existence of the header object for the given rbd
Alex Elder1f3ef782013-05-06 17:40:33 -05006964 * device. If this image is the one being mapped (i.e., not a
6965 * parent), initiate a watch on its header object before using that
6966 * object to get detailed information about the rbd image.
Alex Eldera30b71b2012-07-10 20:30:11 -05006967 */
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006968static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth)
Alex Eldera30b71b2012-07-10 20:30:11 -05006969{
Ilya Dryomovb9ef2b82019-11-12 20:20:04 +01006970 bool need_watch = !rbd_is_ro(rbd_dev);
Alex Eldera30b71b2012-07-10 20:30:11 -05006971 int ret;
6972
6973 /*
Alex Elder3abef3b2013-05-13 20:35:37 -05006974 * Get the id from the image id object. Unless there's an
6975 * error, rbd_dev->spec->image_id will be filled in with
6976 * a dynamically-allocated string, and rbd_dev->image_format
6977 * will be set to either 1 or 2.
Alex Eldera30b71b2012-07-10 20:30:11 -05006978 */
6979 ret = rbd_dev_image_id(rbd_dev);
6980 if (ret)
Alex Elderc0fba362013-04-25 23:15:08 -05006981 return ret;
Alex Elderc0fba362013-04-25 23:15:08 -05006982
Alex Elder332bb122013-04-27 09:59:30 -05006983 ret = rbd_dev_header_name(rbd_dev);
6984 if (ret)
6985 goto err_out_format;
6986
Ilya Dryomovb9ef2b82019-11-12 20:20:04 +01006987 if (need_watch) {
Ilya Dryomov99d16942016-08-12 16:11:41 +02006988 ret = rbd_register_watch(rbd_dev);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006989 if (ret) {
6990 if (ret == -ENOENT)
Ilya Dryomovb9ef2b82019-11-12 20:20:04 +01006991 rbd_print_dne(rbd_dev, false);
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02006992 goto err_out_format;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03006993 }
Alex Elder1f3ef782013-05-06 17:40:33 -05006994 }
Alex Elderb644de22013-04-27 09:59:31 -05006995
Ilya Dryomova720ae02014-07-23 17:11:19 +04006996 ret = rbd_dev_header_info(rbd_dev);
Ilya Dryomovb9ef2b82019-11-12 20:20:04 +01006997 if (ret) {
6998 if (ret == -ENOENT && !need_watch)
6999 rbd_print_dne(rbd_dev, false);
Alex Elderb644de22013-04-27 09:59:31 -05007000 goto err_out_watch;
Ilya Dryomovb9ef2b82019-11-12 20:20:04 +01007001 }
Alex Elder83a06262012-10-30 15:47:17 -05007002
Ilya Dryomov04077592014-07-23 17:11:20 +04007003 /*
7004 * If this image is the one being mapped, we have pool name and
7005 * id, image name and id, and snap name - need to fill snap id.
7006 * Otherwise this is a parent image, identified by pool, image
7007 * and snap ids - need to fill in names for those ids.
7008 */
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02007009 if (!depth)
Ilya Dryomov04077592014-07-23 17:11:20 +04007010 ret = rbd_spec_fill_snap_id(rbd_dev);
7011 else
7012 ret = rbd_spec_fill_names(rbd_dev);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03007013 if (ret) {
7014 if (ret == -ENOENT)
Ilya Dryomovb9ef2b82019-11-12 20:20:04 +01007015 rbd_print_dne(rbd_dev, true);
Alex Elder33dca392013-04-30 00:44:33 -05007016 goto err_out_probe;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03007017 }
Alex Elder9bb81c92013-04-27 09:59:30 -05007018
Ilya Dryomovda5ef6be2019-06-17 15:29:49 +02007019 ret = rbd_dev_mapping_set(rbd_dev);
7020 if (ret)
7021 goto err_out_probe;
7022
Ilya Dryomovf3c0e452019-11-07 16:22:10 +01007023 if (rbd_is_snap(rbd_dev) &&
Ilya Dryomov22e8bd52019-06-05 19:25:11 +02007024 (rbd_dev->header.features & RBD_FEATURE_OBJECT_MAP)) {
7025 ret = rbd_object_map_load(rbd_dev);
7026 if (ret)
7027 goto err_out_probe;
7028 }
7029
Ilya Dryomove8f59b52014-07-24 10:42:13 +04007030 if (rbd_dev->header.features & RBD_FEATURE_LAYERING) {
7031 ret = rbd_dev_v2_parent_info(rbd_dev);
7032 if (ret)
7033 goto err_out_probe;
Ilya Dryomove8f59b52014-07-24 10:42:13 +04007034 }
7035
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02007036 ret = rbd_dev_probe_parent(rbd_dev, depth);
Alex Elder30d60ba2013-05-06 09:51:30 -05007037 if (ret)
7038 goto err_out_probe;
Alex Elder83a06262012-10-30 15:47:17 -05007039
Alex Elder30d60ba2013-05-06 09:51:30 -05007040 dout("discovered format %u image, header name is %s\n",
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02007041 rbd_dev->image_format, rbd_dev->header_oid.name);
Alex Elder30d60ba2013-05-06 09:51:30 -05007042 return 0;
Ilya Dryomove8f59b52014-07-24 10:42:13 +04007043
Alex Elder6fd48b32013-04-28 23:32:34 -05007044err_out_probe:
7045 rbd_dev_unprobe(rbd_dev);
Alex Elderb644de22013-04-27 09:59:31 -05007046err_out_watch:
Ilya Dryomovb9ef2b82019-11-12 20:20:04 +01007047 if (need_watch)
Ilya Dryomov99d16942016-08-12 16:11:41 +02007048 rbd_unregister_watch(rbd_dev);
Alex Elder332bb122013-04-27 09:59:30 -05007049err_out_format:
7050 rbd_dev->image_format = 0;
Alex Elder5655c4d2013-04-25 23:15:08 -05007051 kfree(rbd_dev->spec->image_id);
7052 rbd_dev->spec->image_id = NULL;
Alex Elder5655c4d2013-04-25 23:15:08 -05007053 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05007054}
7055
Ilya Dryomov9b60e702013-12-13 15:28:57 +02007056static ssize_t do_rbd_add(struct bus_type *bus,
7057 const char *buf,
7058 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007059{
Alex Eldercb8627c2012-07-09 21:04:23 -05007060 struct rbd_device *rbd_dev = NULL;
Alex Elderdc79b112012-10-25 23:34:41 -05007061 struct ceph_options *ceph_opts = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05007062 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05007063 struct rbd_spec *spec = NULL;
Alex Elder9d3997f2012-10-25 23:34:42 -05007064 struct rbd_client *rbdc;
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02007065 int rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007066
7067 if (!try_module_get(THIS_MODULE))
7068 return -ENODEV;
7069
Alex Eldera725f65e2012-02-02 08:13:30 -06007070 /* parse add command */
Alex Elder859c31d2012-10-25 23:34:42 -05007071 rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec);
Alex Elderdc79b112012-10-25 23:34:41 -05007072 if (rc < 0)
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02007073 goto out;
Alex Eldera725f65e2012-02-02 08:13:30 -06007074
Alex Elder9d3997f2012-10-25 23:34:42 -05007075 rbdc = rbd_get_client(ceph_opts);
7076 if (IS_ERR(rbdc)) {
7077 rc = PTR_ERR(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05007078 goto err_out_args;
Alex Elder9d3997f2012-10-25 23:34:42 -05007079 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007080
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007081 /* pick the pool */
Ilya Dryomovdd435852018-02-22 13:43:24 +01007082 rc = ceph_pg_poolid_by_name(rbdc->client->osdc.osdmap, spec->pool_name);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03007083 if (rc < 0) {
7084 if (rc == -ENOENT)
7085 pr_info("pool %s does not exist\n", spec->pool_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007086 goto err_out_client;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03007087 }
Alex Elderc0cd10db2013-04-26 09:43:47 -05007088 spec->pool_id = (u64)rc;
Alex Elder859c31d2012-10-25 23:34:42 -05007089
Ilya Dryomovd1475432015-06-22 13:24:48 +03007090 rbd_dev = rbd_dev_create(rbdc, spec, rbd_opts);
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02007091 if (!rbd_dev) {
7092 rc = -ENOMEM;
Alex Elderbd4ba652012-10-25 23:34:42 -05007093 goto err_out_client;
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02007094 }
Alex Elderc53d5892012-10-25 23:34:42 -05007095 rbdc = NULL; /* rbd_dev now owns this */
7096 spec = NULL; /* rbd_dev now owns this */
Ilya Dryomovd1475432015-06-22 13:24:48 +03007097 rbd_opts = NULL; /* rbd_dev now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007098
Ilya Dryomov39258aa2019-11-07 17:16:23 +01007099 /* if we are mapping a snapshot it will be a read-only mapping */
7100 if (rbd_dev->opts->read_only ||
7101 strcmp(rbd_dev->spec->snap_name, RBD_SNAP_HEAD_NAME))
7102 __set_bit(RBD_DEV_FLAG_READONLY, &rbd_dev->flags);
7103
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02007104 rbd_dev->config_info = kstrdup(buf, GFP_KERNEL);
7105 if (!rbd_dev->config_info) {
7106 rc = -ENOMEM;
7107 goto err_out_rbd_dev;
7108 }
7109
Ilya Dryomov811c6682016-04-15 16:22:16 +02007110 down_write(&rbd_dev->header_rwsem);
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02007111 rc = rbd_dev_image_probe(rbd_dev, 0);
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02007112 if (rc < 0) {
7113 up_write(&rbd_dev->header_rwsem);
Alex Elderc53d5892012-10-25 23:34:42 -05007114 goto err_out_rbd_dev;
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02007115 }
Alex Elder05fd6f62012-08-29 17:11:07 -05007116
Ilya Dryomov0c93e1b2019-01-30 15:14:48 +01007117 if (rbd_dev->opts->alloc_size > rbd_dev->layout.object_size) {
7118 rbd_warn(rbd_dev, "alloc_size adjusted to %u",
7119 rbd_dev->layout.object_size);
7120 rbd_dev->opts->alloc_size = rbd_dev->layout.object_size;
7121 }
7122
Alex Elderb536f692013-04-28 23:32:34 -05007123 rc = rbd_dev_device_setup(rbd_dev);
Ilya Dryomovfd22aef2017-04-13 12:17:37 +02007124 if (rc)
Ilya Dryomov8b679ec2017-04-13 12:17:37 +02007125 goto err_out_image_probe;
Alex Elderb536f692013-04-28 23:32:34 -05007126
Ilya Dryomov637cd062019-06-06 17:14:49 +02007127 rc = rbd_add_acquire_lock(rbd_dev);
7128 if (rc)
7129 goto err_out_image_lock;
Alex Elderb536f692013-04-28 23:32:34 -05007130
Ilya Dryomov5769ed02017-04-13 12:17:38 +02007131 /* Everything's ready. Announce the disk to the world. */
7132
7133 rc = device_add(&rbd_dev->dev);
7134 if (rc)
Ilya Dryomove010dd02017-04-13 12:17:39 +02007135 goto err_out_image_lock;
Ilya Dryomov5769ed02017-04-13 12:17:38 +02007136
Hannes Reinecke33253222020-01-23 13:44:33 +01007137 device_add_disk(&rbd_dev->dev, rbd_dev->disk, NULL);
Ilya Dryomov5769ed02017-04-13 12:17:38 +02007138 /* see rbd_init_disk() */
7139 blk_put_queue(rbd_dev->disk->queue);
7140
7141 spin_lock(&rbd_dev_list_lock);
7142 list_add_tail(&rbd_dev->node, &rbd_dev_list);
7143 spin_unlock(&rbd_dev_list_lock);
7144
7145 pr_info("%s: capacity %llu features 0x%llx\n", rbd_dev->disk->disk_name,
7146 (unsigned long long)get_capacity(rbd_dev->disk) << SECTOR_SHIFT,
7147 rbd_dev->header.features);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02007148 rc = count;
7149out:
7150 module_put(THIS_MODULE);
7151 return rc;
Alex Elder3abef3b2013-05-13 20:35:37 -05007152
Ilya Dryomove010dd02017-04-13 12:17:39 +02007153err_out_image_lock:
7154 rbd_dev_image_unlock(rbd_dev);
Ilya Dryomov5769ed02017-04-13 12:17:38 +02007155 rbd_dev_device_release(rbd_dev);
Ilya Dryomov8b679ec2017-04-13 12:17:37 +02007156err_out_image_probe:
7157 rbd_dev_image_release(rbd_dev);
Alex Elderc53d5892012-10-25 23:34:42 -05007158err_out_rbd_dev:
7159 rbd_dev_destroy(rbd_dev);
Alex Elderbd4ba652012-10-25 23:34:42 -05007160err_out_client:
Alex Elder9d3997f2012-10-25 23:34:42 -05007161 rbd_put_client(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05007162err_out_args:
Alex Elder859c31d2012-10-25 23:34:42 -05007163 rbd_spec_put(spec);
Ilya Dryomovd1475432015-06-22 13:24:48 +03007164 kfree(rbd_opts);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02007165 goto out;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007166}
7167
Greg Kroah-Hartman7e9586b2018-12-21 08:54:38 +01007168static ssize_t add_store(struct bus_type *bus, const char *buf, size_t count)
Ilya Dryomov9b60e702013-12-13 15:28:57 +02007169{
7170 if (single_major)
7171 return -EINVAL;
7172
7173 return do_rbd_add(bus, buf, count);
7174}
7175
Greg Kroah-Hartman7e9586b2018-12-21 08:54:38 +01007176static ssize_t add_single_major_store(struct bus_type *bus, const char *buf,
7177 size_t count)
Ilya Dryomov9b60e702013-12-13 15:28:57 +02007178{
7179 return do_rbd_add(bus, buf, count);
7180}
7181
Alex Elder05a46af2013-04-26 15:44:36 -05007182static void rbd_dev_remove_parent(struct rbd_device *rbd_dev)
7183{
Alex Elderad945fc2013-04-26 15:44:36 -05007184 while (rbd_dev->parent) {
Alex Elder05a46af2013-04-26 15:44:36 -05007185 struct rbd_device *first = rbd_dev;
7186 struct rbd_device *second = first->parent;
7187 struct rbd_device *third;
7188
7189 /*
7190 * Follow to the parent with no grandparent and
7191 * remove it.
7192 */
7193 while (second && (third = second->parent)) {
7194 first = second;
7195 second = third;
7196 }
Alex Elderad945fc2013-04-26 15:44:36 -05007197 rbd_assert(second);
Alex Elder8ad42cd2013-04-28 23:32:34 -05007198 rbd_dev_image_release(second);
Ilya Dryomov8b679ec2017-04-13 12:17:37 +02007199 rbd_dev_destroy(second);
Alex Elderad945fc2013-04-26 15:44:36 -05007200 first->parent = NULL;
7201 first->parent_overlap = 0;
7202
7203 rbd_assert(first->parent_spec);
Alex Elder05a46af2013-04-26 15:44:36 -05007204 rbd_spec_put(first->parent_spec);
7205 first->parent_spec = NULL;
Alex Elder05a46af2013-04-26 15:44:36 -05007206 }
7207}
7208
Ilya Dryomov9b60e702013-12-13 15:28:57 +02007209static ssize_t do_rbd_remove(struct bus_type *bus,
7210 const char *buf,
7211 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007212{
7213 struct rbd_device *rbd_dev = NULL;
Alex Elder751cc0e2013-05-31 15:17:01 -05007214 struct list_head *tmp;
7215 int dev_id;
Mike Christie0276dca2016-08-18 18:38:45 +02007216 char opt_buf[6];
Mike Christie0276dca2016-08-18 18:38:45 +02007217 bool force = false;
Alex Elder0d8189e2013-04-27 09:59:30 -05007218 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007219
Mike Christie0276dca2016-08-18 18:38:45 +02007220 dev_id = -1;
7221 opt_buf[0] = '\0';
7222 sscanf(buf, "%d %5s", &dev_id, opt_buf);
7223 if (dev_id < 0) {
7224 pr_err("dev_id out of range\n");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007225 return -EINVAL;
Mike Christie0276dca2016-08-18 18:38:45 +02007226 }
7227 if (opt_buf[0] != '\0') {
7228 if (!strcmp(opt_buf, "force")) {
7229 force = true;
7230 } else {
7231 pr_err("bad remove option at '%s'\n", opt_buf);
7232 return -EINVAL;
7233 }
7234 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007235
Alex Elder751cc0e2013-05-31 15:17:01 -05007236 ret = -ENOENT;
7237 spin_lock(&rbd_dev_list_lock);
7238 list_for_each(tmp, &rbd_dev_list) {
7239 rbd_dev = list_entry(tmp, struct rbd_device, node);
7240 if (rbd_dev->dev_id == dev_id) {
7241 ret = 0;
7242 break;
7243 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007244 }
Alex Elder751cc0e2013-05-31 15:17:01 -05007245 if (!ret) {
7246 spin_lock_irq(&rbd_dev->lock);
Mike Christie0276dca2016-08-18 18:38:45 +02007247 if (rbd_dev->open_count && !force)
Alex Elder751cc0e2013-05-31 15:17:01 -05007248 ret = -EBUSY;
Ilya Dryomov85f5a4d2019-01-08 19:47:38 +01007249 else if (test_and_set_bit(RBD_DEV_FLAG_REMOVING,
7250 &rbd_dev->flags))
7251 ret = -EINPROGRESS;
Alex Elder751cc0e2013-05-31 15:17:01 -05007252 spin_unlock_irq(&rbd_dev->lock);
7253 }
7254 spin_unlock(&rbd_dev_list_lock);
Ilya Dryomov85f5a4d2019-01-08 19:47:38 +01007255 if (ret)
Alex Elder1ba0f1e2013-05-31 15:17:01 -05007256 return ret;
Alex Elder751cc0e2013-05-31 15:17:01 -05007257
Mike Christie0276dca2016-08-18 18:38:45 +02007258 if (force) {
7259 /*
7260 * Prevent new IO from being queued and wait for existing
7261 * IO to complete/fail.
7262 */
7263 blk_mq_freeze_queue(rbd_dev->disk->queue);
7264 blk_set_queue_dying(rbd_dev->disk->queue);
7265 }
7266
Ilya Dryomov5769ed02017-04-13 12:17:38 +02007267 del_gendisk(rbd_dev->disk);
7268 spin_lock(&rbd_dev_list_lock);
7269 list_del_init(&rbd_dev->node);
7270 spin_unlock(&rbd_dev_list_lock);
7271 device_del(&rbd_dev->dev);
Ilya Dryomovfca27062013-12-16 18:02:40 +02007272
Ilya Dryomove010dd02017-04-13 12:17:39 +02007273 rbd_dev_image_unlock(rbd_dev);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02007274 rbd_dev_device_release(rbd_dev);
Alex Elder8ad42cd2013-04-28 23:32:34 -05007275 rbd_dev_image_release(rbd_dev);
Ilya Dryomov8b679ec2017-04-13 12:17:37 +02007276 rbd_dev_destroy(rbd_dev);
Alex Elder1ba0f1e2013-05-31 15:17:01 -05007277 return count;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007278}
7279
Greg Kroah-Hartman7e9586b2018-12-21 08:54:38 +01007280static ssize_t remove_store(struct bus_type *bus, const char *buf, size_t count)
Ilya Dryomov9b60e702013-12-13 15:28:57 +02007281{
7282 if (single_major)
7283 return -EINVAL;
7284
7285 return do_rbd_remove(bus, buf, count);
7286}
7287
Greg Kroah-Hartman7e9586b2018-12-21 08:54:38 +01007288static ssize_t remove_single_major_store(struct bus_type *bus, const char *buf,
7289 size_t count)
Ilya Dryomov9b60e702013-12-13 15:28:57 +02007290{
7291 return do_rbd_remove(bus, buf, count);
7292}
7293
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007294/*
7295 * create control files in sysfs
Yehuda Sadehdfc56062010-11-19 14:51:04 -08007296 * /sys/bus/rbd/...
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007297 */
Chengguang Xu7d8dc532018-08-12 23:06:54 +08007298static int __init rbd_sysfs_init(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007299{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08007300 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007301
Alex Elderfed4c142012-02-07 12:03:36 -06007302 ret = device_register(&rbd_root_dev);
Alex Elder21079782012-01-24 10:08:36 -06007303 if (ret < 0)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08007304 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007305
Alex Elderfed4c142012-02-07 12:03:36 -06007306 ret = bus_register(&rbd_bus_type);
7307 if (ret < 0)
7308 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007309
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007310 return ret;
7311}
7312
Chengguang Xu7d8dc532018-08-12 23:06:54 +08007313static void __exit rbd_sysfs_cleanup(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007314{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08007315 bus_unregister(&rbd_bus_type);
Alex Elderfed4c142012-02-07 12:03:36 -06007316 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007317}
7318
Chengguang Xu7d8dc532018-08-12 23:06:54 +08007319static int __init rbd_slab_init(void)
Alex Elder1c2a9df2013-05-01 12:43:03 -05007320{
7321 rbd_assert(!rbd_img_request_cache);
Geliang Tang03d94402016-03-13 15:17:32 +08007322 rbd_img_request_cache = KMEM_CACHE(rbd_img_request, 0);
Alex Elder868311b2013-05-01 12:43:03 -05007323 if (!rbd_img_request_cache)
7324 return -ENOMEM;
7325
7326 rbd_assert(!rbd_obj_request_cache);
Geliang Tang03d94402016-03-13 15:17:32 +08007327 rbd_obj_request_cache = KMEM_CACHE(rbd_obj_request, 0);
Alex Elder78c2a442013-05-01 12:43:04 -05007328 if (!rbd_obj_request_cache)
7329 goto out_err;
7330
Ilya Dryomov6c696d82017-01-25 18:16:23 +01007331 return 0;
Alex Elder1c2a9df2013-05-01 12:43:03 -05007332
Ilya Dryomov6c696d82017-01-25 18:16:23 +01007333out_err:
Alex Elder868311b2013-05-01 12:43:03 -05007334 kmem_cache_destroy(rbd_img_request_cache);
7335 rbd_img_request_cache = NULL;
Alex Elder1c2a9df2013-05-01 12:43:03 -05007336 return -ENOMEM;
7337}
7338
7339static void rbd_slab_exit(void)
7340{
Alex Elder868311b2013-05-01 12:43:03 -05007341 rbd_assert(rbd_obj_request_cache);
7342 kmem_cache_destroy(rbd_obj_request_cache);
7343 rbd_obj_request_cache = NULL;
7344
Alex Elder1c2a9df2013-05-01 12:43:03 -05007345 rbd_assert(rbd_img_request_cache);
7346 kmem_cache_destroy(rbd_img_request_cache);
7347 rbd_img_request_cache = NULL;
7348}
7349
Alex Eldercc344fa2013-02-19 12:25:56 -06007350static int __init rbd_init(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007351{
7352 int rc;
7353
Alex Elder1e32d342013-01-30 11:13:33 -06007354 if (!libceph_compatible(NULL)) {
7355 rbd_warn(NULL, "libceph incompatibility (quitting)");
Alex Elder1e32d342013-01-30 11:13:33 -06007356 return -EINVAL;
7357 }
Ilya Dryomove1b4d962013-12-13 15:28:57 +02007358
Alex Elder1c2a9df2013-05-01 12:43:03 -05007359 rc = rbd_slab_init();
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007360 if (rc)
7361 return rc;
Ilya Dryomove1b4d962013-12-13 15:28:57 +02007362
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04007363 /*
7364 * The number of active work items is limited by the number of
Ilya Dryomovf77303b2015-04-22 18:28:13 +03007365 * rbd devices * queue depth, so leave @max_active at default.
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04007366 */
7367 rbd_wq = alloc_workqueue(RBD_DRV_NAME, WQ_MEM_RECLAIM, 0);
7368 if (!rbd_wq) {
7369 rc = -ENOMEM;
7370 goto err_out_slab;
7371 }
7372
Ilya Dryomov9b60e702013-12-13 15:28:57 +02007373 if (single_major) {
7374 rbd_major = register_blkdev(0, RBD_DRV_NAME);
7375 if (rbd_major < 0) {
7376 rc = rbd_major;
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04007377 goto err_out_wq;
Ilya Dryomov9b60e702013-12-13 15:28:57 +02007378 }
7379 }
7380
Alex Elder1c2a9df2013-05-01 12:43:03 -05007381 rc = rbd_sysfs_init();
7382 if (rc)
Ilya Dryomov9b60e702013-12-13 15:28:57 +02007383 goto err_out_blkdev;
Alex Elder1c2a9df2013-05-01 12:43:03 -05007384
Ilya Dryomov9b60e702013-12-13 15:28:57 +02007385 if (single_major)
7386 pr_info("loaded (major %d)\n", rbd_major);
7387 else
7388 pr_info("loaded\n");
7389
Ilya Dryomove1b4d962013-12-13 15:28:57 +02007390 return 0;
7391
Ilya Dryomov9b60e702013-12-13 15:28:57 +02007392err_out_blkdev:
7393 if (single_major)
7394 unregister_blkdev(rbd_major, RBD_DRV_NAME);
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04007395err_out_wq:
7396 destroy_workqueue(rbd_wq);
Ilya Dryomove1b4d962013-12-13 15:28:57 +02007397err_out_slab:
7398 rbd_slab_exit();
Alex Elder1c2a9df2013-05-01 12:43:03 -05007399 return rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007400}
7401
Alex Eldercc344fa2013-02-19 12:25:56 -06007402static void __exit rbd_exit(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007403{
Ilya Dryomovffe312c2014-05-20 15:46:04 +04007404 ida_destroy(&rbd_dev_id_ida);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007405 rbd_sysfs_cleanup();
Ilya Dryomov9b60e702013-12-13 15:28:57 +02007406 if (single_major)
7407 unregister_blkdev(rbd_major, RBD_DRV_NAME);
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04007408 destroy_workqueue(rbd_wq);
Alex Elder1c2a9df2013-05-01 12:43:03 -05007409 rbd_slab_exit();
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007410}
7411
7412module_init(rbd_init);
7413module_exit(rbd_exit);
7414
Alex Elderd552c612013-05-31 20:13:09 -05007415MODULE_AUTHOR("Alex Elder <elder@inktank.com>");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007416MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
7417MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007418/* following authorship retained from original osdblk.c */
7419MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
7420
Ilya Dryomov90da2582013-12-13 15:28:56 +02007421MODULE_DESCRIPTION("RADOS Block Device (RBD) driver");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07007422MODULE_LICENSE("GPL");