]>
Commit | Line | Data |
---|---|---|
e2a58ee5 | 1 | |
602adf40 YS |
2 | /* |
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 | ||
dfc5606d | 25 | For usage instructions, please refer to: |
602adf40 | 26 | |
dfc5606d | 27 | Documentation/ABI/testing/sysfs-bus-rbd |
602adf40 YS |
28 | |
29 | */ | |
30 | ||
31 | #include <linux/ceph/libceph.h> | |
32 | #include <linux/ceph/osd_client.h> | |
33 | #include <linux/ceph/mon_client.h> | |
ed95b21a | 34 | #include <linux/ceph/cls_lock_client.h> |
602adf40 | 35 | #include <linux/ceph/decode.h> |
59c2be1e | 36 | #include <linux/parser.h> |
30d1cff8 | 37 | #include <linux/bsearch.h> |
602adf40 YS |
38 | |
39 | #include <linux/kernel.h> | |
40 | #include <linux/device.h> | |
41 | #include <linux/module.h> | |
7ad18afa | 42 | #include <linux/blk-mq.h> |
602adf40 YS |
43 | #include <linux/fs.h> |
44 | #include <linux/blkdev.h> | |
1c2a9dfe | 45 | #include <linux/slab.h> |
f8a22fc2 | 46 | #include <linux/idr.h> |
bc1ecc65 | 47 | #include <linux/workqueue.h> |
602adf40 YS |
48 | |
49 | #include "rbd_types.h" | |
50 | ||
aafb230e AE |
51 | #define RBD_DEBUG /* Activate rbd_assert() calls */ |
52 | ||
593a9e7b AE |
53 | /* |
54 | * The basic unit of block I/O is a sector. It is interpreted in a | |
55 | * number of contexts in Linux (blk, bio, genhd), but the default is | |
56 | * universally 512 bytes. These symbols are just slightly more | |
57 | * meaningful than the bare numbers they represent. | |
58 | */ | |
59 | #define SECTOR_SHIFT 9 | |
60 | #define SECTOR_SIZE (1ULL << SECTOR_SHIFT) | |
61 | ||
a2acd00e AE |
62 | /* |
63 | * Increment the given counter and return its updated value. | |
64 | * If the counter is already 0 it will not be incremented. | |
65 | * If the counter is already at its maximum value returns | |
66 | * -EINVAL without updating it. | |
67 | */ | |
68 | static int atomic_inc_return_safe(atomic_t *v) | |
69 | { | |
70 | unsigned int counter; | |
71 | ||
72 | counter = (unsigned int)__atomic_add_unless(v, 1, 0); | |
73 | if (counter <= (unsigned int)INT_MAX) | |
74 | return (int)counter; | |
75 | ||
76 | atomic_dec(v); | |
77 | ||
78 | return -EINVAL; | |
79 | } | |
80 | ||
81 | /* Decrement the counter. Return the resulting value, or -EINVAL */ | |
82 | static int atomic_dec_return_safe(atomic_t *v) | |
83 | { | |
84 | int counter; | |
85 | ||
86 | counter = atomic_dec_return(v); | |
87 | if (counter >= 0) | |
88 | return counter; | |
89 | ||
90 | atomic_inc(v); | |
91 | ||
92 | return -EINVAL; | |
93 | } | |
94 | ||
f0f8cef5 | 95 | #define RBD_DRV_NAME "rbd" |
602adf40 | 96 | |
7e513d43 ID |
97 | #define RBD_MINORS_PER_MAJOR 256 |
98 | #define RBD_SINGLE_MAJOR_PART_SHIFT 4 | |
602adf40 | 99 | |
6d69bb53 ID |
100 | #define RBD_MAX_PARENT_CHAIN_LEN 16 |
101 | ||
d4b125e9 AE |
102 | #define RBD_SNAP_DEV_NAME_PREFIX "snap_" |
103 | #define RBD_MAX_SNAP_NAME_LEN \ | |
104 | (NAME_MAX - (sizeof (RBD_SNAP_DEV_NAME_PREFIX) - 1)) | |
105 | ||
35d489f9 | 106 | #define RBD_MAX_SNAP_COUNT 510 /* allows max snapc to fit in 4KB */ |
602adf40 YS |
107 | |
108 | #define RBD_SNAP_HEAD_NAME "-" | |
109 | ||
9682fc6d AE |
110 | #define BAD_SNAP_INDEX U32_MAX /* invalid index into snap array */ |
111 | ||
9e15b77d AE |
112 | /* This allows a single page to hold an image name sent by OSD */ |
113 | #define RBD_IMAGE_NAME_LEN_MAX (PAGE_SIZE - sizeof (__le32) - 1) | |
1e130199 | 114 | #define RBD_IMAGE_ID_LEN_MAX 64 |
9e15b77d | 115 | |
1e130199 | 116 | #define RBD_OBJ_PREFIX_LEN_MAX 64 |
589d30e0 | 117 | |
ed95b21a | 118 | #define RBD_NOTIFY_TIMEOUT 5 /* seconds */ |
99d16943 ID |
119 | #define RBD_RETRY_DELAY msecs_to_jiffies(1000) |
120 | ||
d889140c AE |
121 | /* Feature bits */ |
122 | ||
5cbf6f12 AE |
123 | #define RBD_FEATURE_LAYERING (1<<0) |
124 | #define RBD_FEATURE_STRIPINGV2 (1<<1) | |
ed95b21a | 125 | #define RBD_FEATURE_EXCLUSIVE_LOCK (1<<2) |
7e97332e | 126 | #define RBD_FEATURE_DATA_POOL (1<<7) |
ed95b21a ID |
127 | #define RBD_FEATURES_ALL (RBD_FEATURE_LAYERING | \ |
128 | RBD_FEATURE_STRIPINGV2 | \ | |
7e97332e ID |
129 | RBD_FEATURE_EXCLUSIVE_LOCK | \ |
130 | RBD_FEATURE_DATA_POOL) | |
d889140c AE |
131 | |
132 | /* Features supported by this (client software) implementation. */ | |
133 | ||
770eba6e | 134 | #define RBD_FEATURES_SUPPORTED (RBD_FEATURES_ALL) |
d889140c | 135 | |
81a89793 AE |
136 | /* |
137 | * An RBD device name will be "rbd#", where the "rbd" comes from | |
138 | * RBD_DRV_NAME above, and # is a unique integer identifier. | |
81a89793 | 139 | */ |
602adf40 YS |
140 | #define DEV_NAME_LEN 32 |
141 | ||
142 | /* | |
143 | * block device image metadata (in-memory version) | |
144 | */ | |
145 | struct rbd_image_header { | |
f35a4dee | 146 | /* These six fields never change for a given rbd image */ |
849b4260 | 147 | char *object_prefix; |
602adf40 | 148 | __u8 obj_order; |
f35a4dee AE |
149 | u64 stripe_unit; |
150 | u64 stripe_count; | |
7e97332e | 151 | s64 data_pool_id; |
f35a4dee | 152 | u64 features; /* Might be changeable someday? */ |
602adf40 | 153 | |
f84344f3 AE |
154 | /* The remaining fields need to be updated occasionally */ |
155 | u64 image_size; | |
156 | struct ceph_snap_context *snapc; | |
f35a4dee AE |
157 | char *snap_names; /* format 1 only */ |
158 | u64 *snap_sizes; /* format 1 only */ | |
59c2be1e YS |
159 | }; |
160 | ||
0d7dbfce AE |
161 | /* |
162 | * An rbd image specification. | |
163 | * | |
164 | * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely | |
c66c6e0c AE |
165 | * identify an image. Each rbd_dev structure includes a pointer to |
166 | * an rbd_spec structure that encapsulates this identity. | |
167 | * | |
168 | * Each of the id's in an rbd_spec has an associated name. For a | |
169 | * user-mapped image, the names are supplied and the id's associated | |
170 | * with them are looked up. For a layered image, a parent image is | |
171 | * defined by the tuple, and the names are looked up. | |
172 | * | |
173 | * An rbd_dev structure contains a parent_spec pointer which is | |
174 | * non-null if the image it represents is a child in a layered | |
175 | * image. This pointer will refer to the rbd_spec structure used | |
176 | * by the parent rbd_dev for its own identity (i.e., the structure | |
177 | * is shared between the parent and child). | |
178 | * | |
179 | * Since these structures are populated once, during the discovery | |
180 | * phase of image construction, they are effectively immutable so | |
181 | * we make no effort to synchronize access to them. | |
182 | * | |
183 | * Note that code herein does not assume the image name is known (it | |
184 | * could be a null pointer). | |
0d7dbfce AE |
185 | */ |
186 | struct rbd_spec { | |
187 | u64 pool_id; | |
ecb4dc22 | 188 | const char *pool_name; |
0d7dbfce | 189 | |
ecb4dc22 AE |
190 | const char *image_id; |
191 | const char *image_name; | |
0d7dbfce AE |
192 | |
193 | u64 snap_id; | |
ecb4dc22 | 194 | const char *snap_name; |
0d7dbfce AE |
195 | |
196 | struct kref kref; | |
197 | }; | |
198 | ||
602adf40 | 199 | /* |
f0f8cef5 | 200 | * an instance of the client. multiple devices may share an rbd client. |
602adf40 YS |
201 | */ |
202 | struct rbd_client { | |
203 | struct ceph_client *client; | |
204 | struct kref kref; | |
205 | struct list_head node; | |
206 | }; | |
207 | ||
bf0d5f50 AE |
208 | struct rbd_img_request; |
209 | typedef void (*rbd_img_callback_t)(struct rbd_img_request *); | |
210 | ||
211 | #define BAD_WHICH U32_MAX /* Good which or bad which, which? */ | |
212 | ||
213 | struct rbd_obj_request; | |
214 | typedef void (*rbd_obj_callback_t)(struct rbd_obj_request *); | |
215 | ||
9969ebc5 AE |
216 | enum obj_request_type { |
217 | OBJ_REQUEST_NODATA, OBJ_REQUEST_BIO, OBJ_REQUEST_PAGES | |
218 | }; | |
bf0d5f50 | 219 | |
6d2940c8 GZ |
220 | enum obj_operation_type { |
221 | OBJ_OP_WRITE, | |
222 | OBJ_OP_READ, | |
90e98c52 | 223 | OBJ_OP_DISCARD, |
6d2940c8 GZ |
224 | }; |
225 | ||
926f9b3f AE |
226 | enum obj_req_flags { |
227 | OBJ_REQ_DONE, /* completion flag: not done = 0, done = 1 */ | |
6365d33a | 228 | OBJ_REQ_IMG_DATA, /* object usage: standalone = 0, image = 1 */ |
5679c59f AE |
229 | OBJ_REQ_KNOWN, /* EXISTS flag valid: no = 0, yes = 1 */ |
230 | OBJ_REQ_EXISTS, /* target exists: no = 0, yes = 1 */ | |
926f9b3f AE |
231 | }; |
232 | ||
bf0d5f50 | 233 | struct rbd_obj_request { |
a90bb0c1 | 234 | u64 object_no; |
bf0d5f50 AE |
235 | u64 offset; /* object start byte */ |
236 | u64 length; /* bytes from offset */ | |
926f9b3f | 237 | unsigned long flags; |
bf0d5f50 | 238 | |
c5b5ef6c AE |
239 | /* |
240 | * An object request associated with an image will have its | |
241 | * img_data flag set; a standalone object request will not. | |
242 | * | |
243 | * A standalone object request will have which == BAD_WHICH | |
244 | * and a null obj_request pointer. | |
245 | * | |
246 | * An object request initiated in support of a layered image | |
247 | * object (to check for its existence before a write) will | |
248 | * have which == BAD_WHICH and a non-null obj_request pointer. | |
249 | * | |
250 | * Finally, an object request for rbd image data will have | |
251 | * which != BAD_WHICH, and will have a non-null img_request | |
252 | * pointer. The value of which will be in the range | |
253 | * 0..(img_request->obj_request_count-1). | |
254 | */ | |
255 | union { | |
256 | struct rbd_obj_request *obj_request; /* STAT op */ | |
257 | struct { | |
258 | struct rbd_img_request *img_request; | |
259 | u64 img_offset; | |
260 | /* links for img_request->obj_requests list */ | |
261 | struct list_head links; | |
262 | }; | |
263 | }; | |
bf0d5f50 AE |
264 | u32 which; /* posn image request list */ |
265 | ||
266 | enum obj_request_type type; | |
788e2df3 AE |
267 | union { |
268 | struct bio *bio_list; | |
269 | struct { | |
270 | struct page **pages; | |
271 | u32 page_count; | |
272 | }; | |
273 | }; | |
0eefd470 | 274 | struct page **copyup_pages; |
ebda6408 | 275 | u32 copyup_page_count; |
bf0d5f50 AE |
276 | |
277 | struct ceph_osd_request *osd_req; | |
278 | ||
279 | u64 xferred; /* bytes transferred */ | |
1b83bef2 | 280 | int result; |
bf0d5f50 AE |
281 | |
282 | rbd_obj_callback_t callback; | |
788e2df3 | 283 | struct completion completion; |
bf0d5f50 AE |
284 | |
285 | struct kref kref; | |
286 | }; | |
287 | ||
0c425248 | 288 | enum img_req_flags { |
9849e986 AE |
289 | IMG_REQ_WRITE, /* I/O direction: read = 0, write = 1 */ |
290 | IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */ | |
d0b2e944 | 291 | IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */ |
90e98c52 | 292 | IMG_REQ_DISCARD, /* discard: normal = 0, discard request = 1 */ |
0c425248 AE |
293 | }; |
294 | ||
bf0d5f50 | 295 | struct rbd_img_request { |
bf0d5f50 AE |
296 | struct rbd_device *rbd_dev; |
297 | u64 offset; /* starting image byte offset */ | |
298 | u64 length; /* byte count from offset */ | |
0c425248 | 299 | unsigned long flags; |
bf0d5f50 | 300 | union { |
9849e986 | 301 | u64 snap_id; /* for reads */ |
bf0d5f50 | 302 | struct ceph_snap_context *snapc; /* for writes */ |
9849e986 AE |
303 | }; |
304 | union { | |
305 | struct request *rq; /* block request */ | |
306 | struct rbd_obj_request *obj_request; /* obj req initiator */ | |
bf0d5f50 | 307 | }; |
3d7efd18 | 308 | struct page **copyup_pages; |
ebda6408 | 309 | u32 copyup_page_count; |
bf0d5f50 AE |
310 | spinlock_t completion_lock;/* protects next_completion */ |
311 | u32 next_completion; | |
312 | rbd_img_callback_t callback; | |
55f27e09 | 313 | u64 xferred;/* aggregate bytes transferred */ |
a5a337d4 | 314 | int result; /* first nonzero obj_request result */ |
bf0d5f50 AE |
315 | |
316 | u32 obj_request_count; | |
317 | struct list_head obj_requests; /* rbd_obj_request structs */ | |
318 | ||
319 | struct kref kref; | |
320 | }; | |
321 | ||
322 | #define for_each_obj_request(ireq, oreq) \ | |
ef06f4d3 | 323 | list_for_each_entry(oreq, &(ireq)->obj_requests, links) |
bf0d5f50 | 324 | #define for_each_obj_request_from(ireq, oreq) \ |
ef06f4d3 | 325 | list_for_each_entry_from(oreq, &(ireq)->obj_requests, links) |
bf0d5f50 | 326 | #define for_each_obj_request_safe(ireq, oreq, n) \ |
ef06f4d3 | 327 | list_for_each_entry_safe_reverse(oreq, n, &(ireq)->obj_requests, links) |
bf0d5f50 | 328 | |
99d16943 ID |
329 | enum rbd_watch_state { |
330 | RBD_WATCH_STATE_UNREGISTERED, | |
331 | RBD_WATCH_STATE_REGISTERED, | |
332 | RBD_WATCH_STATE_ERROR, | |
333 | }; | |
334 | ||
ed95b21a ID |
335 | enum rbd_lock_state { |
336 | RBD_LOCK_STATE_UNLOCKED, | |
337 | RBD_LOCK_STATE_LOCKED, | |
338 | RBD_LOCK_STATE_RELEASING, | |
339 | }; | |
340 | ||
341 | /* WatchNotify::ClientId */ | |
342 | struct rbd_client_id { | |
343 | u64 gid; | |
344 | u64 handle; | |
345 | }; | |
346 | ||
f84344f3 | 347 | struct rbd_mapping { |
99c1f08f | 348 | u64 size; |
34b13184 | 349 | u64 features; |
f84344f3 AE |
350 | bool read_only; |
351 | }; | |
352 | ||
602adf40 YS |
353 | /* |
354 | * a single device | |
355 | */ | |
356 | struct rbd_device { | |
de71a297 | 357 | int dev_id; /* blkdev unique id */ |
602adf40 YS |
358 | |
359 | int major; /* blkdev assigned major */ | |
dd82fff1 | 360 | int minor; |
602adf40 | 361 | struct gendisk *disk; /* blkdev's gendisk and rq */ |
602adf40 | 362 | |
a30b71b9 | 363 | u32 image_format; /* Either 1 or 2 */ |
602adf40 YS |
364 | struct rbd_client *rbd_client; |
365 | ||
366 | char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */ | |
367 | ||
b82d167b | 368 | spinlock_t lock; /* queue, flags, open_count */ |
602adf40 YS |
369 | |
370 | struct rbd_image_header header; | |
b82d167b | 371 | unsigned long flags; /* possibly lock protected */ |
0d7dbfce | 372 | struct rbd_spec *spec; |
d147543d | 373 | struct rbd_options *opts; |
0d6d1e9c | 374 | char *config_info; /* add{,_single_major} string */ |
602adf40 | 375 | |
c41d13a3 | 376 | struct ceph_object_id header_oid; |
922dab61 | 377 | struct ceph_object_locator header_oloc; |
971f839a | 378 | |
1643dfa4 | 379 | struct ceph_file_layout layout; /* used for all rbd requests */ |
0903e875 | 380 | |
99d16943 ID |
381 | struct mutex watch_mutex; |
382 | enum rbd_watch_state watch_state; | |
922dab61 | 383 | struct ceph_osd_linger_request *watch_handle; |
99d16943 ID |
384 | u64 watch_cookie; |
385 | struct delayed_work watch_dwork; | |
59c2be1e | 386 | |
ed95b21a ID |
387 | struct rw_semaphore lock_rwsem; |
388 | enum rbd_lock_state lock_state; | |
389 | struct rbd_client_id owner_cid; | |
390 | struct work_struct acquired_lock_work; | |
391 | struct work_struct released_lock_work; | |
392 | struct delayed_work lock_dwork; | |
393 | struct work_struct unlock_work; | |
394 | wait_queue_head_t lock_waitq; | |
395 | ||
1643dfa4 | 396 | struct workqueue_struct *task_wq; |
59c2be1e | 397 | |
86b00e0d AE |
398 | struct rbd_spec *parent_spec; |
399 | u64 parent_overlap; | |
a2acd00e | 400 | atomic_t parent_ref; |
2f82ee54 | 401 | struct rbd_device *parent; |
86b00e0d | 402 | |
7ad18afa CH |
403 | /* Block layer tags. */ |
404 | struct blk_mq_tag_set tag_set; | |
405 | ||
c666601a JD |
406 | /* protects updating the header */ |
407 | struct rw_semaphore header_rwsem; | |
f84344f3 AE |
408 | |
409 | struct rbd_mapping mapping; | |
602adf40 YS |
410 | |
411 | struct list_head node; | |
dfc5606d | 412 | |
dfc5606d YS |
413 | /* sysfs related */ |
414 | struct device dev; | |
b82d167b | 415 | unsigned long open_count; /* protected by lock */ |
dfc5606d YS |
416 | }; |
417 | ||
b82d167b | 418 | /* |
87c0fded ID |
419 | * Flag bits for rbd_dev->flags: |
420 | * - REMOVING (which is coupled with rbd_dev->open_count) is protected | |
421 | * by rbd_dev->lock | |
422 | * - BLACKLISTED is protected by rbd_dev->lock_rwsem | |
b82d167b | 423 | */ |
6d292906 AE |
424 | enum rbd_dev_flags { |
425 | RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */ | |
b82d167b | 426 | RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */ |
87c0fded | 427 | RBD_DEV_FLAG_BLACKLISTED, /* our ceph_client is blacklisted */ |
6d292906 AE |
428 | }; |
429 | ||
cfbf6377 | 430 | static DEFINE_MUTEX(client_mutex); /* Serialize client creation */ |
e124a82f | 431 | |
602adf40 | 432 | static LIST_HEAD(rbd_dev_list); /* devices */ |
e124a82f AE |
433 | static DEFINE_SPINLOCK(rbd_dev_list_lock); |
434 | ||
432b8587 AE |
435 | static LIST_HEAD(rbd_client_list); /* clients */ |
436 | static DEFINE_SPINLOCK(rbd_client_list_lock); | |
602adf40 | 437 | |
78c2a44a AE |
438 | /* Slab caches for frequently-allocated structures */ |
439 | ||
1c2a9dfe | 440 | static struct kmem_cache *rbd_img_request_cache; |
868311b1 | 441 | static struct kmem_cache *rbd_obj_request_cache; |
1c2a9dfe | 442 | |
9b60e70b | 443 | static int rbd_major; |
f8a22fc2 ID |
444 | static DEFINE_IDA(rbd_dev_id_ida); |
445 | ||
f5ee37bd ID |
446 | static struct workqueue_struct *rbd_wq; |
447 | ||
9b60e70b ID |
448 | /* |
449 | * Default to false for now, as single-major requires >= 0.75 version of | |
450 | * userspace rbd utility. | |
451 | */ | |
452 | static bool single_major = false; | |
453 | module_param(single_major, bool, S_IRUGO); | |
454 | MODULE_PARM_DESC(single_major, "Use a single major number for all rbd devices (default: false)"); | |
455 | ||
3d7efd18 AE |
456 | static int rbd_img_request_submit(struct rbd_img_request *img_request); |
457 | ||
f0f8cef5 AE |
458 | static ssize_t rbd_add(struct bus_type *bus, const char *buf, |
459 | size_t count); | |
460 | static ssize_t rbd_remove(struct bus_type *bus, const char *buf, | |
461 | size_t count); | |
9b60e70b ID |
462 | static ssize_t rbd_add_single_major(struct bus_type *bus, const char *buf, |
463 | size_t count); | |
464 | static ssize_t rbd_remove_single_major(struct bus_type *bus, const char *buf, | |
465 | size_t count); | |
6d69bb53 | 466 | static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth); |
a2acd00e | 467 | static void rbd_spec_put(struct rbd_spec *spec); |
f0f8cef5 | 468 | |
9b60e70b ID |
469 | static int rbd_dev_id_to_minor(int dev_id) |
470 | { | |
7e513d43 | 471 | return dev_id << RBD_SINGLE_MAJOR_PART_SHIFT; |
9b60e70b ID |
472 | } |
473 | ||
474 | static int minor_to_rbd_dev_id(int minor) | |
475 | { | |
7e513d43 | 476 | return minor >> RBD_SINGLE_MAJOR_PART_SHIFT; |
9b60e70b ID |
477 | } |
478 | ||
ed95b21a ID |
479 | static bool rbd_is_lock_supported(struct rbd_device *rbd_dev) |
480 | { | |
481 | return (rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK) && | |
482 | rbd_dev->spec->snap_id == CEPH_NOSNAP && | |
483 | !rbd_dev->mapping.read_only; | |
484 | } | |
485 | ||
486 | static bool __rbd_is_lock_owner(struct rbd_device *rbd_dev) | |
487 | { | |
488 | return rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED || | |
489 | rbd_dev->lock_state == RBD_LOCK_STATE_RELEASING; | |
490 | } | |
491 | ||
492 | static bool rbd_is_lock_owner(struct rbd_device *rbd_dev) | |
493 | { | |
494 | bool is_lock_owner; | |
495 | ||
496 | down_read(&rbd_dev->lock_rwsem); | |
497 | is_lock_owner = __rbd_is_lock_owner(rbd_dev); | |
498 | up_read(&rbd_dev->lock_rwsem); | |
499 | return is_lock_owner; | |
500 | } | |
501 | ||
b15a21dd GKH |
502 | static BUS_ATTR(add, S_IWUSR, NULL, rbd_add); |
503 | static BUS_ATTR(remove, S_IWUSR, NULL, rbd_remove); | |
9b60e70b ID |
504 | static BUS_ATTR(add_single_major, S_IWUSR, NULL, rbd_add_single_major); |
505 | static BUS_ATTR(remove_single_major, S_IWUSR, NULL, rbd_remove_single_major); | |
b15a21dd GKH |
506 | |
507 | static struct attribute *rbd_bus_attrs[] = { | |
508 | &bus_attr_add.attr, | |
509 | &bus_attr_remove.attr, | |
9b60e70b ID |
510 | &bus_attr_add_single_major.attr, |
511 | &bus_attr_remove_single_major.attr, | |
b15a21dd | 512 | NULL, |
f0f8cef5 | 513 | }; |
92c76dc0 ID |
514 | |
515 | static umode_t rbd_bus_is_visible(struct kobject *kobj, | |
516 | struct attribute *attr, int index) | |
517 | { | |
9b60e70b ID |
518 | if (!single_major && |
519 | (attr == &bus_attr_add_single_major.attr || | |
520 | attr == &bus_attr_remove_single_major.attr)) | |
521 | return 0; | |
522 | ||
92c76dc0 ID |
523 | return attr->mode; |
524 | } | |
525 | ||
526 | static const struct attribute_group rbd_bus_group = { | |
527 | .attrs = rbd_bus_attrs, | |
528 | .is_visible = rbd_bus_is_visible, | |
529 | }; | |
530 | __ATTRIBUTE_GROUPS(rbd_bus); | |
f0f8cef5 AE |
531 | |
532 | static struct bus_type rbd_bus_type = { | |
533 | .name = "rbd", | |
b15a21dd | 534 | .bus_groups = rbd_bus_groups, |
f0f8cef5 AE |
535 | }; |
536 | ||
537 | static void rbd_root_dev_release(struct device *dev) | |
538 | { | |
539 | } | |
540 | ||
541 | static struct device rbd_root_dev = { | |
542 | .init_name = "rbd", | |
543 | .release = rbd_root_dev_release, | |
544 | }; | |
545 | ||
06ecc6cb AE |
546 | static __printf(2, 3) |
547 | void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...) | |
548 | { | |
549 | struct va_format vaf; | |
550 | va_list args; | |
551 | ||
552 | va_start(args, fmt); | |
553 | vaf.fmt = fmt; | |
554 | vaf.va = &args; | |
555 | ||
556 | if (!rbd_dev) | |
557 | printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf); | |
558 | else if (rbd_dev->disk) | |
559 | printk(KERN_WARNING "%s: %s: %pV\n", | |
560 | RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf); | |
561 | else if (rbd_dev->spec && rbd_dev->spec->image_name) | |
562 | printk(KERN_WARNING "%s: image %s: %pV\n", | |
563 | RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf); | |
564 | else if (rbd_dev->spec && rbd_dev->spec->image_id) | |
565 | printk(KERN_WARNING "%s: id %s: %pV\n", | |
566 | RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf); | |
567 | else /* punt */ | |
568 | printk(KERN_WARNING "%s: rbd_dev %p: %pV\n", | |
569 | RBD_DRV_NAME, rbd_dev, &vaf); | |
570 | va_end(args); | |
571 | } | |
572 | ||
aafb230e AE |
573 | #ifdef RBD_DEBUG |
574 | #define rbd_assert(expr) \ | |
575 | if (unlikely(!(expr))) { \ | |
576 | printk(KERN_ERR "\nAssertion failure in %s() " \ | |
577 | "at line %d:\n\n" \ | |
578 | "\trbd_assert(%s);\n\n", \ | |
579 | __func__, __LINE__, #expr); \ | |
580 | BUG(); \ | |
581 | } | |
582 | #else /* !RBD_DEBUG */ | |
583 | # define rbd_assert(expr) ((void) 0) | |
584 | #endif /* !RBD_DEBUG */ | |
dfc5606d | 585 | |
2761713d | 586 | static void rbd_osd_copyup_callback(struct rbd_obj_request *obj_request); |
b454e36d | 587 | static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request); |
05a46afd AE |
588 | static void rbd_img_parent_read(struct rbd_obj_request *obj_request); |
589 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev); | |
8b3e1a56 | 590 | |
cc4a38bd | 591 | static int rbd_dev_refresh(struct rbd_device *rbd_dev); |
2df3fac7 | 592 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev); |
a720ae09 | 593 | static int rbd_dev_header_info(struct rbd_device *rbd_dev); |
e8f59b59 | 594 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev); |
54cac61f AE |
595 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
596 | u64 snap_id); | |
2ad3d716 AE |
597 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
598 | u8 *order, u64 *snap_size); | |
599 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, | |
600 | u64 *snap_features); | |
59c2be1e | 601 | |
602adf40 YS |
602 | static int rbd_open(struct block_device *bdev, fmode_t mode) |
603 | { | |
f0f8cef5 | 604 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
b82d167b | 605 | bool removing = false; |
602adf40 | 606 | |
f84344f3 | 607 | if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only) |
602adf40 YS |
608 | return -EROFS; |
609 | ||
a14ea269 | 610 | spin_lock_irq(&rbd_dev->lock); |
b82d167b AE |
611 | if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) |
612 | removing = true; | |
613 | else | |
614 | rbd_dev->open_count++; | |
a14ea269 | 615 | spin_unlock_irq(&rbd_dev->lock); |
b82d167b AE |
616 | if (removing) |
617 | return -ENOENT; | |
618 | ||
c3e946ce | 619 | (void) get_device(&rbd_dev->dev); |
340c7a2b | 620 | |
602adf40 YS |
621 | return 0; |
622 | } | |
623 | ||
db2a144b | 624 | static void rbd_release(struct gendisk *disk, fmode_t mode) |
dfc5606d YS |
625 | { |
626 | struct rbd_device *rbd_dev = disk->private_data; | |
b82d167b AE |
627 | unsigned long open_count_before; |
628 | ||
a14ea269 | 629 | spin_lock_irq(&rbd_dev->lock); |
b82d167b | 630 | open_count_before = rbd_dev->open_count--; |
a14ea269 | 631 | spin_unlock_irq(&rbd_dev->lock); |
b82d167b | 632 | rbd_assert(open_count_before > 0); |
dfc5606d | 633 | |
c3e946ce | 634 | put_device(&rbd_dev->dev); |
dfc5606d YS |
635 | } |
636 | ||
131fd9f6 GZ |
637 | static int rbd_ioctl_set_ro(struct rbd_device *rbd_dev, unsigned long arg) |
638 | { | |
77f33c03 | 639 | int ret = 0; |
131fd9f6 GZ |
640 | int val; |
641 | bool ro; | |
77f33c03 | 642 | bool ro_changed = false; |
131fd9f6 | 643 | |
77f33c03 | 644 | /* get_user() may sleep, so call it before taking rbd_dev->lock */ |
131fd9f6 GZ |
645 | if (get_user(val, (int __user *)(arg))) |
646 | return -EFAULT; | |
647 | ||
648 | ro = val ? true : false; | |
649 | /* Snapshot doesn't allow to write*/ | |
650 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP && !ro) | |
651 | return -EROFS; | |
652 | ||
77f33c03 JD |
653 | spin_lock_irq(&rbd_dev->lock); |
654 | /* prevent others open this device */ | |
655 | if (rbd_dev->open_count > 1) { | |
656 | ret = -EBUSY; | |
657 | goto out; | |
658 | } | |
659 | ||
131fd9f6 GZ |
660 | if (rbd_dev->mapping.read_only != ro) { |
661 | rbd_dev->mapping.read_only = ro; | |
77f33c03 | 662 | ro_changed = true; |
131fd9f6 GZ |
663 | } |
664 | ||
77f33c03 JD |
665 | out: |
666 | spin_unlock_irq(&rbd_dev->lock); | |
667 | /* set_disk_ro() may sleep, so call it after releasing rbd_dev->lock */ | |
668 | if (ret == 0 && ro_changed) | |
669 | set_disk_ro(rbd_dev->disk, ro ? 1 : 0); | |
670 | ||
671 | return ret; | |
131fd9f6 GZ |
672 | } |
673 | ||
674 | static int rbd_ioctl(struct block_device *bdev, fmode_t mode, | |
675 | unsigned int cmd, unsigned long arg) | |
676 | { | |
677 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; | |
678 | int ret = 0; | |
679 | ||
131fd9f6 GZ |
680 | switch (cmd) { |
681 | case BLKROSET: | |
682 | ret = rbd_ioctl_set_ro(rbd_dev, arg); | |
683 | break; | |
684 | default: | |
685 | ret = -ENOTTY; | |
686 | } | |
687 | ||
131fd9f6 GZ |
688 | return ret; |
689 | } | |
690 | ||
691 | #ifdef CONFIG_COMPAT | |
692 | static int rbd_compat_ioctl(struct block_device *bdev, fmode_t mode, | |
693 | unsigned int cmd, unsigned long arg) | |
694 | { | |
695 | return rbd_ioctl(bdev, mode, cmd, arg); | |
696 | } | |
697 | #endif /* CONFIG_COMPAT */ | |
698 | ||
602adf40 YS |
699 | static const struct block_device_operations rbd_bd_ops = { |
700 | .owner = THIS_MODULE, | |
701 | .open = rbd_open, | |
dfc5606d | 702 | .release = rbd_release, |
131fd9f6 GZ |
703 | .ioctl = rbd_ioctl, |
704 | #ifdef CONFIG_COMPAT | |
705 | .compat_ioctl = rbd_compat_ioctl, | |
706 | #endif | |
602adf40 YS |
707 | }; |
708 | ||
709 | /* | |
7262cfca | 710 | * Initialize an rbd client instance. Success or not, this function |
cfbf6377 | 711 | * consumes ceph_opts. Caller holds client_mutex. |
602adf40 | 712 | */ |
f8c38929 | 713 | static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts) |
602adf40 YS |
714 | { |
715 | struct rbd_client *rbdc; | |
716 | int ret = -ENOMEM; | |
717 | ||
37206ee5 | 718 | dout("%s:\n", __func__); |
602adf40 YS |
719 | rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL); |
720 | if (!rbdc) | |
721 | goto out_opt; | |
722 | ||
723 | kref_init(&rbdc->kref); | |
724 | INIT_LIST_HEAD(&rbdc->node); | |
725 | ||
43ae4701 | 726 | rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0); |
602adf40 | 727 | if (IS_ERR(rbdc->client)) |
08f75463 | 728 | goto out_rbdc; |
43ae4701 | 729 | ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */ |
602adf40 YS |
730 | |
731 | ret = ceph_open_session(rbdc->client); | |
732 | if (ret < 0) | |
08f75463 | 733 | goto out_client; |
602adf40 | 734 | |
432b8587 | 735 | spin_lock(&rbd_client_list_lock); |
602adf40 | 736 | list_add_tail(&rbdc->node, &rbd_client_list); |
432b8587 | 737 | spin_unlock(&rbd_client_list_lock); |
602adf40 | 738 | |
37206ee5 | 739 | dout("%s: rbdc %p\n", __func__, rbdc); |
bc534d86 | 740 | |
602adf40 | 741 | return rbdc; |
08f75463 | 742 | out_client: |
602adf40 | 743 | ceph_destroy_client(rbdc->client); |
08f75463 | 744 | out_rbdc: |
602adf40 YS |
745 | kfree(rbdc); |
746 | out_opt: | |
43ae4701 AE |
747 | if (ceph_opts) |
748 | ceph_destroy_options(ceph_opts); | |
37206ee5 AE |
749 | dout("%s: error %d\n", __func__, ret); |
750 | ||
28f259b7 | 751 | return ERR_PTR(ret); |
602adf40 YS |
752 | } |
753 | ||
2f82ee54 AE |
754 | static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc) |
755 | { | |
756 | kref_get(&rbdc->kref); | |
757 | ||
758 | return rbdc; | |
759 | } | |
760 | ||
602adf40 | 761 | /* |
1f7ba331 AE |
762 | * Find a ceph client with specific addr and configuration. If |
763 | * found, bump its reference count. | |
602adf40 | 764 | */ |
1f7ba331 | 765 | static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts) |
602adf40 YS |
766 | { |
767 | struct rbd_client *client_node; | |
1f7ba331 | 768 | bool found = false; |
602adf40 | 769 | |
43ae4701 | 770 | if (ceph_opts->flags & CEPH_OPT_NOSHARE) |
602adf40 YS |
771 | return NULL; |
772 | ||
1f7ba331 AE |
773 | spin_lock(&rbd_client_list_lock); |
774 | list_for_each_entry(client_node, &rbd_client_list, node) { | |
775 | if (!ceph_compare_options(ceph_opts, client_node->client)) { | |
2f82ee54 AE |
776 | __rbd_get_client(client_node); |
777 | ||
1f7ba331 AE |
778 | found = true; |
779 | break; | |
780 | } | |
781 | } | |
782 | spin_unlock(&rbd_client_list_lock); | |
783 | ||
784 | return found ? client_node : NULL; | |
602adf40 YS |
785 | } |
786 | ||
59c2be1e | 787 | /* |
210c104c | 788 | * (Per device) rbd map options |
59c2be1e YS |
789 | */ |
790 | enum { | |
b5584180 | 791 | Opt_queue_depth, |
59c2be1e YS |
792 | Opt_last_int, |
793 | /* int args above */ | |
794 | Opt_last_string, | |
795 | /* string args above */ | |
cc0538b6 AE |
796 | Opt_read_only, |
797 | Opt_read_write, | |
80de1912 | 798 | Opt_lock_on_read, |
210c104c | 799 | Opt_err |
59c2be1e YS |
800 | }; |
801 | ||
43ae4701 | 802 | static match_table_t rbd_opts_tokens = { |
b5584180 | 803 | {Opt_queue_depth, "queue_depth=%d"}, |
59c2be1e YS |
804 | /* int args above */ |
805 | /* string args above */ | |
be466c1c | 806 | {Opt_read_only, "read_only"}, |
cc0538b6 AE |
807 | {Opt_read_only, "ro"}, /* Alternate spelling */ |
808 | {Opt_read_write, "read_write"}, | |
809 | {Opt_read_write, "rw"}, /* Alternate spelling */ | |
80de1912 | 810 | {Opt_lock_on_read, "lock_on_read"}, |
210c104c | 811 | {Opt_err, NULL} |
59c2be1e YS |
812 | }; |
813 | ||
98571b5a | 814 | struct rbd_options { |
b5584180 | 815 | int queue_depth; |
98571b5a | 816 | bool read_only; |
80de1912 | 817 | bool lock_on_read; |
98571b5a AE |
818 | }; |
819 | ||
b5584180 | 820 | #define RBD_QUEUE_DEPTH_DEFAULT BLKDEV_MAX_RQ |
98571b5a | 821 | #define RBD_READ_ONLY_DEFAULT false |
80de1912 | 822 | #define RBD_LOCK_ON_READ_DEFAULT false |
98571b5a | 823 | |
59c2be1e YS |
824 | static int parse_rbd_opts_token(char *c, void *private) |
825 | { | |
43ae4701 | 826 | struct rbd_options *rbd_opts = private; |
59c2be1e YS |
827 | substring_t argstr[MAX_OPT_ARGS]; |
828 | int token, intval, ret; | |
829 | ||
43ae4701 | 830 | token = match_token(c, rbd_opts_tokens, argstr); |
59c2be1e YS |
831 | if (token < Opt_last_int) { |
832 | ret = match_int(&argstr[0], &intval); | |
833 | if (ret < 0) { | |
210c104c | 834 | pr_err("bad mount option arg (not int) at '%s'\n", c); |
59c2be1e YS |
835 | return ret; |
836 | } | |
837 | dout("got int token %d val %d\n", token, intval); | |
838 | } else if (token > Opt_last_int && token < Opt_last_string) { | |
210c104c | 839 | dout("got string token %d val %s\n", token, argstr[0].from); |
59c2be1e YS |
840 | } else { |
841 | dout("got token %d\n", token); | |
842 | } | |
843 | ||
844 | switch (token) { | |
b5584180 ID |
845 | case Opt_queue_depth: |
846 | if (intval < 1) { | |
847 | pr_err("queue_depth out of range\n"); | |
848 | return -EINVAL; | |
849 | } | |
850 | rbd_opts->queue_depth = intval; | |
851 | break; | |
cc0538b6 AE |
852 | case Opt_read_only: |
853 | rbd_opts->read_only = true; | |
854 | break; | |
855 | case Opt_read_write: | |
856 | rbd_opts->read_only = false; | |
857 | break; | |
80de1912 ID |
858 | case Opt_lock_on_read: |
859 | rbd_opts->lock_on_read = true; | |
860 | break; | |
59c2be1e | 861 | default: |
210c104c ID |
862 | /* libceph prints "bad option" msg */ |
863 | return -EINVAL; | |
59c2be1e | 864 | } |
210c104c | 865 | |
59c2be1e YS |
866 | return 0; |
867 | } | |
868 | ||
6d2940c8 GZ |
869 | static char* obj_op_name(enum obj_operation_type op_type) |
870 | { | |
871 | switch (op_type) { | |
872 | case OBJ_OP_READ: | |
873 | return "read"; | |
874 | case OBJ_OP_WRITE: | |
875 | return "write"; | |
90e98c52 GZ |
876 | case OBJ_OP_DISCARD: |
877 | return "discard"; | |
6d2940c8 GZ |
878 | default: |
879 | return "???"; | |
880 | } | |
881 | } | |
882 | ||
602adf40 YS |
883 | /* |
884 | * Get a ceph client with specific addr and configuration, if one does | |
7262cfca AE |
885 | * not exist create it. Either way, ceph_opts is consumed by this |
886 | * function. | |
602adf40 | 887 | */ |
9d3997fd | 888 | static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts) |
602adf40 | 889 | { |
f8c38929 | 890 | struct rbd_client *rbdc; |
59c2be1e | 891 | |
cfbf6377 | 892 | mutex_lock_nested(&client_mutex, SINGLE_DEPTH_NESTING); |
1f7ba331 | 893 | rbdc = rbd_client_find(ceph_opts); |
9d3997fd | 894 | if (rbdc) /* using an existing client */ |
43ae4701 | 895 | ceph_destroy_options(ceph_opts); |
9d3997fd | 896 | else |
f8c38929 | 897 | rbdc = rbd_client_create(ceph_opts); |
cfbf6377 | 898 | mutex_unlock(&client_mutex); |
602adf40 | 899 | |
9d3997fd | 900 | return rbdc; |
602adf40 YS |
901 | } |
902 | ||
903 | /* | |
904 | * Destroy ceph client | |
d23a4b3f | 905 | * |
432b8587 | 906 | * Caller must hold rbd_client_list_lock. |
602adf40 YS |
907 | */ |
908 | static void rbd_client_release(struct kref *kref) | |
909 | { | |
910 | struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref); | |
911 | ||
37206ee5 | 912 | dout("%s: rbdc %p\n", __func__, rbdc); |
cd9d9f5d | 913 | spin_lock(&rbd_client_list_lock); |
602adf40 | 914 | list_del(&rbdc->node); |
cd9d9f5d | 915 | spin_unlock(&rbd_client_list_lock); |
602adf40 YS |
916 | |
917 | ceph_destroy_client(rbdc->client); | |
918 | kfree(rbdc); | |
919 | } | |
920 | ||
921 | /* | |
922 | * Drop reference to ceph client node. If it's not referenced anymore, release | |
923 | * it. | |
924 | */ | |
9d3997fd | 925 | static void rbd_put_client(struct rbd_client *rbdc) |
602adf40 | 926 | { |
c53d5893 AE |
927 | if (rbdc) |
928 | kref_put(&rbdc->kref, rbd_client_release); | |
602adf40 YS |
929 | } |
930 | ||
a30b71b9 AE |
931 | static bool rbd_image_format_valid(u32 image_format) |
932 | { | |
933 | return image_format == 1 || image_format == 2; | |
934 | } | |
935 | ||
8e94af8e AE |
936 | static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk) |
937 | { | |
103a150f AE |
938 | size_t size; |
939 | u32 snap_count; | |
940 | ||
941 | /* The header has to start with the magic rbd header text */ | |
942 | if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT))) | |
943 | return false; | |
944 | ||
db2388b6 AE |
945 | /* The bio layer requires at least sector-sized I/O */ |
946 | ||
947 | if (ondisk->options.order < SECTOR_SHIFT) | |
948 | return false; | |
949 | ||
950 | /* If we use u64 in a few spots we may be able to loosen this */ | |
951 | ||
952 | if (ondisk->options.order > 8 * sizeof (int) - 1) | |
953 | return false; | |
954 | ||
103a150f AE |
955 | /* |
956 | * The size of a snapshot header has to fit in a size_t, and | |
957 | * that limits the number of snapshots. | |
958 | */ | |
959 | snap_count = le32_to_cpu(ondisk->snap_count); | |
960 | size = SIZE_MAX - sizeof (struct ceph_snap_context); | |
961 | if (snap_count > size / sizeof (__le64)) | |
962 | return false; | |
963 | ||
964 | /* | |
965 | * Not only that, but the size of the entire the snapshot | |
966 | * header must also be representable in a size_t. | |
967 | */ | |
968 | size -= snap_count * sizeof (__le64); | |
969 | if ((u64) size < le64_to_cpu(ondisk->snap_names_len)) | |
970 | return false; | |
971 | ||
972 | return true; | |
8e94af8e AE |
973 | } |
974 | ||
5bc3fb17 ID |
975 | /* |
976 | * returns the size of an object in the image | |
977 | */ | |
978 | static u32 rbd_obj_bytes(struct rbd_image_header *header) | |
979 | { | |
980 | return 1U << header->obj_order; | |
981 | } | |
982 | ||
263423f8 ID |
983 | static void rbd_init_layout(struct rbd_device *rbd_dev) |
984 | { | |
985 | if (rbd_dev->header.stripe_unit == 0 || | |
986 | rbd_dev->header.stripe_count == 0) { | |
987 | rbd_dev->header.stripe_unit = rbd_obj_bytes(&rbd_dev->header); | |
988 | rbd_dev->header.stripe_count = 1; | |
989 | } | |
990 | ||
991 | rbd_dev->layout.stripe_unit = rbd_dev->header.stripe_unit; | |
992 | rbd_dev->layout.stripe_count = rbd_dev->header.stripe_count; | |
993 | rbd_dev->layout.object_size = rbd_obj_bytes(&rbd_dev->header); | |
7e97332e ID |
994 | rbd_dev->layout.pool_id = rbd_dev->header.data_pool_id == CEPH_NOPOOL ? |
995 | rbd_dev->spec->pool_id : rbd_dev->header.data_pool_id; | |
263423f8 ID |
996 | RCU_INIT_POINTER(rbd_dev->layout.pool_ns, NULL); |
997 | } | |
998 | ||
602adf40 | 999 | /* |
bb23e37a AE |
1000 | * Fill an rbd image header with information from the given format 1 |
1001 | * on-disk header. | |
602adf40 | 1002 | */ |
662518b1 | 1003 | static int rbd_header_from_disk(struct rbd_device *rbd_dev, |
4156d998 | 1004 | struct rbd_image_header_ondisk *ondisk) |
602adf40 | 1005 | { |
662518b1 | 1006 | struct rbd_image_header *header = &rbd_dev->header; |
bb23e37a AE |
1007 | bool first_time = header->object_prefix == NULL; |
1008 | struct ceph_snap_context *snapc; | |
1009 | char *object_prefix = NULL; | |
1010 | char *snap_names = NULL; | |
1011 | u64 *snap_sizes = NULL; | |
ccece235 | 1012 | u32 snap_count; |
bb23e37a | 1013 | int ret = -ENOMEM; |
621901d6 | 1014 | u32 i; |
602adf40 | 1015 | |
bb23e37a | 1016 | /* Allocate this now to avoid having to handle failure below */ |
6a52325f | 1017 | |
bb23e37a | 1018 | if (first_time) { |
848d796c ID |
1019 | object_prefix = kstrndup(ondisk->object_prefix, |
1020 | sizeof(ondisk->object_prefix), | |
1021 | GFP_KERNEL); | |
bb23e37a AE |
1022 | if (!object_prefix) |
1023 | return -ENOMEM; | |
bb23e37a | 1024 | } |
00f1f36f | 1025 | |
bb23e37a | 1026 | /* Allocate the snapshot context and fill it in */ |
00f1f36f | 1027 | |
bb23e37a AE |
1028 | snap_count = le32_to_cpu(ondisk->snap_count); |
1029 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); | |
1030 | if (!snapc) | |
1031 | goto out_err; | |
1032 | snapc->seq = le64_to_cpu(ondisk->snap_seq); | |
602adf40 | 1033 | if (snap_count) { |
bb23e37a | 1034 | struct rbd_image_snap_ondisk *snaps; |
f785cc1d AE |
1035 | u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len); |
1036 | ||
bb23e37a | 1037 | /* We'll keep a copy of the snapshot names... */ |
621901d6 | 1038 | |
bb23e37a AE |
1039 | if (snap_names_len > (u64)SIZE_MAX) |
1040 | goto out_2big; | |
1041 | snap_names = kmalloc(snap_names_len, GFP_KERNEL); | |
1042 | if (!snap_names) | |
6a52325f AE |
1043 | goto out_err; |
1044 | ||
bb23e37a | 1045 | /* ...as well as the array of their sizes. */ |
88a25a5f ME |
1046 | snap_sizes = kmalloc_array(snap_count, |
1047 | sizeof(*header->snap_sizes), | |
1048 | GFP_KERNEL); | |
bb23e37a | 1049 | if (!snap_sizes) |
6a52325f | 1050 | goto out_err; |
bb23e37a | 1051 | |
f785cc1d | 1052 | /* |
bb23e37a AE |
1053 | * Copy the names, and fill in each snapshot's id |
1054 | * and size. | |
1055 | * | |
99a41ebc | 1056 | * Note that rbd_dev_v1_header_info() guarantees the |
bb23e37a | 1057 | * ondisk buffer we're working with has |
f785cc1d AE |
1058 | * snap_names_len bytes beyond the end of the |
1059 | * snapshot id array, this memcpy() is safe. | |
1060 | */ | |
bb23e37a AE |
1061 | memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len); |
1062 | snaps = ondisk->snaps; | |
1063 | for (i = 0; i < snap_count; i++) { | |
1064 | snapc->snaps[i] = le64_to_cpu(snaps[i].id); | |
1065 | snap_sizes[i] = le64_to_cpu(snaps[i].image_size); | |
1066 | } | |
602adf40 | 1067 | } |
6a52325f | 1068 | |
bb23e37a | 1069 | /* We won't fail any more, fill in the header */ |
621901d6 | 1070 | |
bb23e37a AE |
1071 | if (first_time) { |
1072 | header->object_prefix = object_prefix; | |
1073 | header->obj_order = ondisk->options.order; | |
263423f8 | 1074 | rbd_init_layout(rbd_dev); |
602adf40 | 1075 | } else { |
662518b1 AE |
1076 | ceph_put_snap_context(header->snapc); |
1077 | kfree(header->snap_names); | |
1078 | kfree(header->snap_sizes); | |
602adf40 | 1079 | } |
849b4260 | 1080 | |
bb23e37a | 1081 | /* The remaining fields always get updated (when we refresh) */ |
621901d6 | 1082 | |
f84344f3 | 1083 | header->image_size = le64_to_cpu(ondisk->image_size); |
bb23e37a AE |
1084 | header->snapc = snapc; |
1085 | header->snap_names = snap_names; | |
1086 | header->snap_sizes = snap_sizes; | |
468521c1 | 1087 | |
602adf40 | 1088 | return 0; |
bb23e37a AE |
1089 | out_2big: |
1090 | ret = -EIO; | |
6a52325f | 1091 | out_err: |
bb23e37a AE |
1092 | kfree(snap_sizes); |
1093 | kfree(snap_names); | |
1094 | ceph_put_snap_context(snapc); | |
1095 | kfree(object_prefix); | |
ccece235 | 1096 | |
bb23e37a | 1097 | return ret; |
602adf40 YS |
1098 | } |
1099 | ||
9682fc6d AE |
1100 | static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which) |
1101 | { | |
1102 | const char *snap_name; | |
1103 | ||
1104 | rbd_assert(which < rbd_dev->header.snapc->num_snaps); | |
1105 | ||
1106 | /* Skip over names until we find the one we are looking for */ | |
1107 | ||
1108 | snap_name = rbd_dev->header.snap_names; | |
1109 | while (which--) | |
1110 | snap_name += strlen(snap_name) + 1; | |
1111 | ||
1112 | return kstrdup(snap_name, GFP_KERNEL); | |
1113 | } | |
1114 | ||
30d1cff8 AE |
1115 | /* |
1116 | * Snapshot id comparison function for use with qsort()/bsearch(). | |
1117 | * Note that result is for snapshots in *descending* order. | |
1118 | */ | |
1119 | static int snapid_compare_reverse(const void *s1, const void *s2) | |
1120 | { | |
1121 | u64 snap_id1 = *(u64 *)s1; | |
1122 | u64 snap_id2 = *(u64 *)s2; | |
1123 | ||
1124 | if (snap_id1 < snap_id2) | |
1125 | return 1; | |
1126 | return snap_id1 == snap_id2 ? 0 : -1; | |
1127 | } | |
1128 | ||
1129 | /* | |
1130 | * Search a snapshot context to see if the given snapshot id is | |
1131 | * present. | |
1132 | * | |
1133 | * Returns the position of the snapshot id in the array if it's found, | |
1134 | * or BAD_SNAP_INDEX otherwise. | |
1135 | * | |
1136 | * Note: The snapshot array is in kept sorted (by the osd) in | |
1137 | * reverse order, highest snapshot id first. | |
1138 | */ | |
9682fc6d AE |
1139 | static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id) |
1140 | { | |
1141 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; | |
30d1cff8 | 1142 | u64 *found; |
9682fc6d | 1143 | |
30d1cff8 AE |
1144 | found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps, |
1145 | sizeof (snap_id), snapid_compare_reverse); | |
9682fc6d | 1146 | |
30d1cff8 | 1147 | return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX; |
9682fc6d AE |
1148 | } |
1149 | ||
2ad3d716 AE |
1150 | static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, |
1151 | u64 snap_id) | |
9e15b77d | 1152 | { |
54cac61f | 1153 | u32 which; |
da6a6b63 | 1154 | const char *snap_name; |
9e15b77d | 1155 | |
54cac61f AE |
1156 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
1157 | if (which == BAD_SNAP_INDEX) | |
da6a6b63 | 1158 | return ERR_PTR(-ENOENT); |
54cac61f | 1159 | |
da6a6b63 JD |
1160 | snap_name = _rbd_dev_v1_snap_name(rbd_dev, which); |
1161 | return snap_name ? snap_name : ERR_PTR(-ENOMEM); | |
54cac61f AE |
1162 | } |
1163 | ||
1164 | static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id) | |
1165 | { | |
9e15b77d AE |
1166 | if (snap_id == CEPH_NOSNAP) |
1167 | return RBD_SNAP_HEAD_NAME; | |
1168 | ||
54cac61f AE |
1169 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
1170 | if (rbd_dev->image_format == 1) | |
1171 | return rbd_dev_v1_snap_name(rbd_dev, snap_id); | |
9e15b77d | 1172 | |
54cac61f | 1173 | return rbd_dev_v2_snap_name(rbd_dev, snap_id); |
9e15b77d AE |
1174 | } |
1175 | ||
2ad3d716 AE |
1176 | static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
1177 | u64 *snap_size) | |
602adf40 | 1178 | { |
2ad3d716 AE |
1179 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
1180 | if (snap_id == CEPH_NOSNAP) { | |
1181 | *snap_size = rbd_dev->header.image_size; | |
1182 | } else if (rbd_dev->image_format == 1) { | |
1183 | u32 which; | |
602adf40 | 1184 | |
2ad3d716 AE |
1185 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
1186 | if (which == BAD_SNAP_INDEX) | |
1187 | return -ENOENT; | |
e86924a8 | 1188 | |
2ad3d716 AE |
1189 | *snap_size = rbd_dev->header.snap_sizes[which]; |
1190 | } else { | |
1191 | u64 size = 0; | |
1192 | int ret; | |
1193 | ||
1194 | ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size); | |
1195 | if (ret) | |
1196 | return ret; | |
1197 | ||
1198 | *snap_size = size; | |
1199 | } | |
1200 | return 0; | |
602adf40 YS |
1201 | } |
1202 | ||
2ad3d716 AE |
1203 | static int rbd_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
1204 | u64 *snap_features) | |
602adf40 | 1205 | { |
2ad3d716 AE |
1206 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
1207 | if (snap_id == CEPH_NOSNAP) { | |
1208 | *snap_features = rbd_dev->header.features; | |
1209 | } else if (rbd_dev->image_format == 1) { | |
1210 | *snap_features = 0; /* No features for format 1 */ | |
602adf40 | 1211 | } else { |
2ad3d716 AE |
1212 | u64 features = 0; |
1213 | int ret; | |
8b0241f8 | 1214 | |
2ad3d716 AE |
1215 | ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, &features); |
1216 | if (ret) | |
1217 | return ret; | |
1218 | ||
1219 | *snap_features = features; | |
1220 | } | |
1221 | return 0; | |
1222 | } | |
1223 | ||
1224 | static int rbd_dev_mapping_set(struct rbd_device *rbd_dev) | |
1225 | { | |
8f4b7d98 | 1226 | u64 snap_id = rbd_dev->spec->snap_id; |
2ad3d716 AE |
1227 | u64 size = 0; |
1228 | u64 features = 0; | |
1229 | int ret; | |
1230 | ||
2ad3d716 AE |
1231 | ret = rbd_snap_size(rbd_dev, snap_id, &size); |
1232 | if (ret) | |
1233 | return ret; | |
1234 | ret = rbd_snap_features(rbd_dev, snap_id, &features); | |
1235 | if (ret) | |
1236 | return ret; | |
1237 | ||
1238 | rbd_dev->mapping.size = size; | |
1239 | rbd_dev->mapping.features = features; | |
1240 | ||
8b0241f8 | 1241 | return 0; |
602adf40 YS |
1242 | } |
1243 | ||
d1cf5788 AE |
1244 | static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev) |
1245 | { | |
1246 | rbd_dev->mapping.size = 0; | |
1247 | rbd_dev->mapping.features = 0; | |
200a6a8b AE |
1248 | } |
1249 | ||
65ccfe21 AE |
1250 | static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset) |
1251 | { | |
5bc3fb17 | 1252 | u64 segment_size = rbd_obj_bytes(&rbd_dev->header); |
602adf40 | 1253 | |
65ccfe21 AE |
1254 | return offset & (segment_size - 1); |
1255 | } | |
1256 | ||
1257 | static u64 rbd_segment_length(struct rbd_device *rbd_dev, | |
1258 | u64 offset, u64 length) | |
1259 | { | |
5bc3fb17 | 1260 | u64 segment_size = rbd_obj_bytes(&rbd_dev->header); |
65ccfe21 AE |
1261 | |
1262 | offset &= segment_size - 1; | |
1263 | ||
aafb230e | 1264 | rbd_assert(length <= U64_MAX - offset); |
65ccfe21 AE |
1265 | if (offset + length > segment_size) |
1266 | length = segment_size - offset; | |
1267 | ||
1268 | return length; | |
602adf40 YS |
1269 | } |
1270 | ||
1271 | /* | |
1272 | * bio helpers | |
1273 | */ | |
1274 | ||
1275 | static void bio_chain_put(struct bio *chain) | |
1276 | { | |
1277 | struct bio *tmp; | |
1278 | ||
1279 | while (chain) { | |
1280 | tmp = chain; | |
1281 | chain = chain->bi_next; | |
1282 | bio_put(tmp); | |
1283 | } | |
1284 | } | |
1285 | ||
1286 | /* | |
1287 | * zeros a bio chain, starting at specific offset | |
1288 | */ | |
1289 | static void zero_bio_chain(struct bio *chain, int start_ofs) | |
1290 | { | |
7988613b KO |
1291 | struct bio_vec bv; |
1292 | struct bvec_iter iter; | |
602adf40 YS |
1293 | unsigned long flags; |
1294 | void *buf; | |
602adf40 YS |
1295 | int pos = 0; |
1296 | ||
1297 | while (chain) { | |
7988613b KO |
1298 | bio_for_each_segment(bv, chain, iter) { |
1299 | if (pos + bv.bv_len > start_ofs) { | |
602adf40 | 1300 | int remainder = max(start_ofs - pos, 0); |
7988613b | 1301 | buf = bvec_kmap_irq(&bv, &flags); |
602adf40 | 1302 | memset(buf + remainder, 0, |
7988613b KO |
1303 | bv.bv_len - remainder); |
1304 | flush_dcache_page(bv.bv_page); | |
85b5aaa6 | 1305 | bvec_kunmap_irq(buf, &flags); |
602adf40 | 1306 | } |
7988613b | 1307 | pos += bv.bv_len; |
602adf40 YS |
1308 | } |
1309 | ||
1310 | chain = chain->bi_next; | |
1311 | } | |
1312 | } | |
1313 | ||
b9434c5b AE |
1314 | /* |
1315 | * similar to zero_bio_chain(), zeros data defined by a page array, | |
1316 | * starting at the given byte offset from the start of the array and | |
1317 | * continuing up to the given end offset. The pages array is | |
1318 | * assumed to be big enough to hold all bytes up to the end. | |
1319 | */ | |
1320 | static void zero_pages(struct page **pages, u64 offset, u64 end) | |
1321 | { | |
1322 | struct page **page = &pages[offset >> PAGE_SHIFT]; | |
1323 | ||
1324 | rbd_assert(end > offset); | |
1325 | rbd_assert(end - offset <= (u64)SIZE_MAX); | |
1326 | while (offset < end) { | |
1327 | size_t page_offset; | |
1328 | size_t length; | |
1329 | unsigned long flags; | |
1330 | void *kaddr; | |
1331 | ||
491205a8 GU |
1332 | page_offset = offset & ~PAGE_MASK; |
1333 | length = min_t(size_t, PAGE_SIZE - page_offset, end - offset); | |
b9434c5b AE |
1334 | local_irq_save(flags); |
1335 | kaddr = kmap_atomic(*page); | |
1336 | memset(kaddr + page_offset, 0, length); | |
e2156054 | 1337 | flush_dcache_page(*page); |
b9434c5b AE |
1338 | kunmap_atomic(kaddr); |
1339 | local_irq_restore(flags); | |
1340 | ||
1341 | offset += length; | |
1342 | page++; | |
1343 | } | |
1344 | } | |
1345 | ||
602adf40 | 1346 | /* |
f7760dad AE |
1347 | * Clone a portion of a bio, starting at the given byte offset |
1348 | * and continuing for the number of bytes indicated. | |
602adf40 | 1349 | */ |
f7760dad AE |
1350 | static struct bio *bio_clone_range(struct bio *bio_src, |
1351 | unsigned int offset, | |
1352 | unsigned int len, | |
1353 | gfp_t gfpmask) | |
602adf40 | 1354 | { |
f7760dad AE |
1355 | struct bio *bio; |
1356 | ||
5341a627 | 1357 | bio = bio_clone(bio_src, gfpmask); |
f7760dad AE |
1358 | if (!bio) |
1359 | return NULL; /* ENOMEM */ | |
602adf40 | 1360 | |
5341a627 | 1361 | bio_advance(bio, offset); |
4f024f37 | 1362 | bio->bi_iter.bi_size = len; |
f7760dad AE |
1363 | |
1364 | return bio; | |
1365 | } | |
1366 | ||
1367 | /* | |
1368 | * Clone a portion of a bio chain, starting at the given byte offset | |
1369 | * into the first bio in the source chain and continuing for the | |
1370 | * number of bytes indicated. The result is another bio chain of | |
1371 | * exactly the given length, or a null pointer on error. | |
1372 | * | |
1373 | * The bio_src and offset parameters are both in-out. On entry they | |
1374 | * refer to the first source bio and the offset into that bio where | |
1375 | * the start of data to be cloned is located. | |
1376 | * | |
1377 | * On return, bio_src is updated to refer to the bio in the source | |
1378 | * chain that contains first un-cloned byte, and *offset will | |
1379 | * contain the offset of that byte within that bio. | |
1380 | */ | |
1381 | static struct bio *bio_chain_clone_range(struct bio **bio_src, | |
1382 | unsigned int *offset, | |
1383 | unsigned int len, | |
1384 | gfp_t gfpmask) | |
1385 | { | |
1386 | struct bio *bi = *bio_src; | |
1387 | unsigned int off = *offset; | |
1388 | struct bio *chain = NULL; | |
1389 | struct bio **end; | |
1390 | ||
1391 | /* Build up a chain of clone bios up to the limit */ | |
1392 | ||
4f024f37 | 1393 | if (!bi || off >= bi->bi_iter.bi_size || !len) |
f7760dad | 1394 | return NULL; /* Nothing to clone */ |
602adf40 | 1395 | |
f7760dad AE |
1396 | end = &chain; |
1397 | while (len) { | |
1398 | unsigned int bi_size; | |
1399 | struct bio *bio; | |
1400 | ||
f5400b7a AE |
1401 | if (!bi) { |
1402 | rbd_warn(NULL, "bio_chain exhausted with %u left", len); | |
f7760dad | 1403 | goto out_err; /* EINVAL; ran out of bio's */ |
f5400b7a | 1404 | } |
4f024f37 | 1405 | bi_size = min_t(unsigned int, bi->bi_iter.bi_size - off, len); |
f7760dad AE |
1406 | bio = bio_clone_range(bi, off, bi_size, gfpmask); |
1407 | if (!bio) | |
1408 | goto out_err; /* ENOMEM */ | |
1409 | ||
1410 | *end = bio; | |
1411 | end = &bio->bi_next; | |
602adf40 | 1412 | |
f7760dad | 1413 | off += bi_size; |
4f024f37 | 1414 | if (off == bi->bi_iter.bi_size) { |
f7760dad AE |
1415 | bi = bi->bi_next; |
1416 | off = 0; | |
1417 | } | |
1418 | len -= bi_size; | |
1419 | } | |
1420 | *bio_src = bi; | |
1421 | *offset = off; | |
1422 | ||
1423 | return chain; | |
1424 | out_err: | |
1425 | bio_chain_put(chain); | |
602adf40 | 1426 | |
602adf40 YS |
1427 | return NULL; |
1428 | } | |
1429 | ||
926f9b3f AE |
1430 | /* |
1431 | * The default/initial value for all object request flags is 0. For | |
1432 | * each flag, once its value is set to 1 it is never reset to 0 | |
1433 | * again. | |
1434 | */ | |
57acbaa7 | 1435 | static void obj_request_img_data_set(struct rbd_obj_request *obj_request) |
926f9b3f | 1436 | { |
57acbaa7 | 1437 | if (test_and_set_bit(OBJ_REQ_IMG_DATA, &obj_request->flags)) { |
926f9b3f AE |
1438 | struct rbd_device *rbd_dev; |
1439 | ||
57acbaa7 | 1440 | rbd_dev = obj_request->img_request->rbd_dev; |
9584d508 | 1441 | rbd_warn(rbd_dev, "obj_request %p already marked img_data", |
926f9b3f AE |
1442 | obj_request); |
1443 | } | |
1444 | } | |
1445 | ||
57acbaa7 | 1446 | static bool obj_request_img_data_test(struct rbd_obj_request *obj_request) |
926f9b3f AE |
1447 | { |
1448 | smp_mb(); | |
57acbaa7 | 1449 | return test_bit(OBJ_REQ_IMG_DATA, &obj_request->flags) != 0; |
926f9b3f AE |
1450 | } |
1451 | ||
57acbaa7 | 1452 | static void obj_request_done_set(struct rbd_obj_request *obj_request) |
6365d33a | 1453 | { |
57acbaa7 AE |
1454 | if (test_and_set_bit(OBJ_REQ_DONE, &obj_request->flags)) { |
1455 | struct rbd_device *rbd_dev = NULL; | |
6365d33a | 1456 | |
57acbaa7 AE |
1457 | if (obj_request_img_data_test(obj_request)) |
1458 | rbd_dev = obj_request->img_request->rbd_dev; | |
9584d508 | 1459 | rbd_warn(rbd_dev, "obj_request %p already marked done", |
6365d33a AE |
1460 | obj_request); |
1461 | } | |
1462 | } | |
1463 | ||
57acbaa7 | 1464 | static bool obj_request_done_test(struct rbd_obj_request *obj_request) |
6365d33a AE |
1465 | { |
1466 | smp_mb(); | |
57acbaa7 | 1467 | return test_bit(OBJ_REQ_DONE, &obj_request->flags) != 0; |
6365d33a AE |
1468 | } |
1469 | ||
5679c59f AE |
1470 | /* |
1471 | * This sets the KNOWN flag after (possibly) setting the EXISTS | |
1472 | * flag. The latter is set based on the "exists" value provided. | |
1473 | * | |
1474 | * Note that for our purposes once an object exists it never goes | |
1475 | * away again. It's possible that the response from two existence | |
1476 | * checks are separated by the creation of the target object, and | |
1477 | * the first ("doesn't exist") response arrives *after* the second | |
1478 | * ("does exist"). In that case we ignore the second one. | |
1479 | */ | |
1480 | static void obj_request_existence_set(struct rbd_obj_request *obj_request, | |
1481 | bool exists) | |
1482 | { | |
1483 | if (exists) | |
1484 | set_bit(OBJ_REQ_EXISTS, &obj_request->flags); | |
1485 | set_bit(OBJ_REQ_KNOWN, &obj_request->flags); | |
1486 | smp_mb(); | |
1487 | } | |
1488 | ||
1489 | static bool obj_request_known_test(struct rbd_obj_request *obj_request) | |
1490 | { | |
1491 | smp_mb(); | |
1492 | return test_bit(OBJ_REQ_KNOWN, &obj_request->flags) != 0; | |
1493 | } | |
1494 | ||
1495 | static bool obj_request_exists_test(struct rbd_obj_request *obj_request) | |
1496 | { | |
1497 | smp_mb(); | |
1498 | return test_bit(OBJ_REQ_EXISTS, &obj_request->flags) != 0; | |
1499 | } | |
1500 | ||
9638556a ID |
1501 | static bool obj_request_overlaps_parent(struct rbd_obj_request *obj_request) |
1502 | { | |
1503 | struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev; | |
1504 | ||
1505 | return obj_request->img_offset < | |
1506 | round_up(rbd_dev->parent_overlap, rbd_obj_bytes(&rbd_dev->header)); | |
1507 | } | |
1508 | ||
bf0d5f50 AE |
1509 | static void rbd_obj_request_get(struct rbd_obj_request *obj_request) |
1510 | { | |
37206ee5 AE |
1511 | dout("%s: obj %p (was %d)\n", __func__, obj_request, |
1512 | atomic_read(&obj_request->kref.refcount)); | |
bf0d5f50 AE |
1513 | kref_get(&obj_request->kref); |
1514 | } | |
1515 | ||
1516 | static void rbd_obj_request_destroy(struct kref *kref); | |
1517 | static void rbd_obj_request_put(struct rbd_obj_request *obj_request) | |
1518 | { | |
1519 | rbd_assert(obj_request != NULL); | |
37206ee5 AE |
1520 | dout("%s: obj %p (was %d)\n", __func__, obj_request, |
1521 | atomic_read(&obj_request->kref.refcount)); | |
bf0d5f50 AE |
1522 | kref_put(&obj_request->kref, rbd_obj_request_destroy); |
1523 | } | |
1524 | ||
0f2d5be7 AE |
1525 | static void rbd_img_request_get(struct rbd_img_request *img_request) |
1526 | { | |
1527 | dout("%s: img %p (was %d)\n", __func__, img_request, | |
1528 | atomic_read(&img_request->kref.refcount)); | |
1529 | kref_get(&img_request->kref); | |
1530 | } | |
1531 | ||
e93f3152 AE |
1532 | static bool img_request_child_test(struct rbd_img_request *img_request); |
1533 | static void rbd_parent_request_destroy(struct kref *kref); | |
bf0d5f50 AE |
1534 | static void rbd_img_request_destroy(struct kref *kref); |
1535 | static void rbd_img_request_put(struct rbd_img_request *img_request) | |
1536 | { | |
1537 | rbd_assert(img_request != NULL); | |
37206ee5 AE |
1538 | dout("%s: img %p (was %d)\n", __func__, img_request, |
1539 | atomic_read(&img_request->kref.refcount)); | |
e93f3152 AE |
1540 | if (img_request_child_test(img_request)) |
1541 | kref_put(&img_request->kref, rbd_parent_request_destroy); | |
1542 | else | |
1543 | kref_put(&img_request->kref, rbd_img_request_destroy); | |
bf0d5f50 AE |
1544 | } |
1545 | ||
1546 | static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request, | |
1547 | struct rbd_obj_request *obj_request) | |
1548 | { | |
25dcf954 AE |
1549 | rbd_assert(obj_request->img_request == NULL); |
1550 | ||
b155e86c | 1551 | /* Image request now owns object's original reference */ |
bf0d5f50 | 1552 | obj_request->img_request = img_request; |
25dcf954 | 1553 | obj_request->which = img_request->obj_request_count; |
6365d33a AE |
1554 | rbd_assert(!obj_request_img_data_test(obj_request)); |
1555 | obj_request_img_data_set(obj_request); | |
bf0d5f50 | 1556 | rbd_assert(obj_request->which != BAD_WHICH); |
25dcf954 AE |
1557 | img_request->obj_request_count++; |
1558 | list_add_tail(&obj_request->links, &img_request->obj_requests); | |
37206ee5 AE |
1559 | dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request, |
1560 | obj_request->which); | |
bf0d5f50 AE |
1561 | } |
1562 | ||
1563 | static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request, | |
1564 | struct rbd_obj_request *obj_request) | |
1565 | { | |
1566 | rbd_assert(obj_request->which != BAD_WHICH); | |
25dcf954 | 1567 | |
37206ee5 AE |
1568 | dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request, |
1569 | obj_request->which); | |
bf0d5f50 | 1570 | list_del(&obj_request->links); |
25dcf954 AE |
1571 | rbd_assert(img_request->obj_request_count > 0); |
1572 | img_request->obj_request_count--; | |
1573 | rbd_assert(obj_request->which == img_request->obj_request_count); | |
1574 | obj_request->which = BAD_WHICH; | |
6365d33a | 1575 | rbd_assert(obj_request_img_data_test(obj_request)); |
bf0d5f50 | 1576 | rbd_assert(obj_request->img_request == img_request); |
bf0d5f50 | 1577 | obj_request->img_request = NULL; |
25dcf954 | 1578 | obj_request->callback = NULL; |
bf0d5f50 AE |
1579 | rbd_obj_request_put(obj_request); |
1580 | } | |
1581 | ||
1582 | static bool obj_request_type_valid(enum obj_request_type type) | |
1583 | { | |
1584 | switch (type) { | |
9969ebc5 | 1585 | case OBJ_REQUEST_NODATA: |
bf0d5f50 | 1586 | case OBJ_REQUEST_BIO: |
788e2df3 | 1587 | case OBJ_REQUEST_PAGES: |
bf0d5f50 AE |
1588 | return true; |
1589 | default: | |
1590 | return false; | |
1591 | } | |
1592 | } | |
1593 | ||
4a17dadc ID |
1594 | static void rbd_img_obj_callback(struct rbd_obj_request *obj_request); |
1595 | ||
980917fc | 1596 | static void rbd_obj_request_submit(struct rbd_obj_request *obj_request) |
bf0d5f50 | 1597 | { |
980917fc ID |
1598 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
1599 | ||
a90bb0c1 ID |
1600 | dout("%s %p object_no %016llx %llu~%llu osd_req %p\n", __func__, |
1601 | obj_request, obj_request->object_no, obj_request->offset, | |
67e2b652 | 1602 | obj_request->length, osd_req); |
4a17dadc ID |
1603 | if (obj_request_img_data_test(obj_request)) { |
1604 | WARN_ON(obj_request->callback != rbd_img_obj_callback); | |
1605 | rbd_img_request_get(obj_request->img_request); | |
1606 | } | |
980917fc | 1607 | ceph_osdc_start_request(osd_req->r_osdc, osd_req, false); |
bf0d5f50 AE |
1608 | } |
1609 | ||
1610 | static void rbd_img_request_complete(struct rbd_img_request *img_request) | |
1611 | { | |
55f27e09 | 1612 | |
37206ee5 | 1613 | dout("%s: img %p\n", __func__, img_request); |
55f27e09 AE |
1614 | |
1615 | /* | |
1616 | * If no error occurred, compute the aggregate transfer | |
1617 | * count for the image request. We could instead use | |
1618 | * atomic64_cmpxchg() to update it as each object request | |
1619 | * completes; not clear which way is better off hand. | |
1620 | */ | |
1621 | if (!img_request->result) { | |
1622 | struct rbd_obj_request *obj_request; | |
1623 | u64 xferred = 0; | |
1624 | ||
1625 | for_each_obj_request(img_request, obj_request) | |
1626 | xferred += obj_request->xferred; | |
1627 | img_request->xferred = xferred; | |
1628 | } | |
1629 | ||
bf0d5f50 AE |
1630 | if (img_request->callback) |
1631 | img_request->callback(img_request); | |
1632 | else | |
1633 | rbd_img_request_put(img_request); | |
1634 | } | |
1635 | ||
0c425248 AE |
1636 | /* |
1637 | * The default/initial value for all image request flags is 0. Each | |
1638 | * is conditionally set to 1 at image request initialization time | |
1639 | * and currently never change thereafter. | |
1640 | */ | |
1641 | static void img_request_write_set(struct rbd_img_request *img_request) | |
1642 | { | |
1643 | set_bit(IMG_REQ_WRITE, &img_request->flags); | |
1644 | smp_mb(); | |
1645 | } | |
1646 | ||
1647 | static bool img_request_write_test(struct rbd_img_request *img_request) | |
1648 | { | |
1649 | smp_mb(); | |
1650 | return test_bit(IMG_REQ_WRITE, &img_request->flags) != 0; | |
1651 | } | |
1652 | ||
90e98c52 GZ |
1653 | /* |
1654 | * Set the discard flag when the img_request is an discard request | |
1655 | */ | |
1656 | static void img_request_discard_set(struct rbd_img_request *img_request) | |
1657 | { | |
1658 | set_bit(IMG_REQ_DISCARD, &img_request->flags); | |
1659 | smp_mb(); | |
1660 | } | |
1661 | ||
1662 | static bool img_request_discard_test(struct rbd_img_request *img_request) | |
1663 | { | |
1664 | smp_mb(); | |
1665 | return test_bit(IMG_REQ_DISCARD, &img_request->flags) != 0; | |
1666 | } | |
1667 | ||
9849e986 AE |
1668 | static void img_request_child_set(struct rbd_img_request *img_request) |
1669 | { | |
1670 | set_bit(IMG_REQ_CHILD, &img_request->flags); | |
1671 | smp_mb(); | |
1672 | } | |
1673 | ||
e93f3152 AE |
1674 | static void img_request_child_clear(struct rbd_img_request *img_request) |
1675 | { | |
1676 | clear_bit(IMG_REQ_CHILD, &img_request->flags); | |
1677 | smp_mb(); | |
1678 | } | |
1679 | ||
9849e986 AE |
1680 | static bool img_request_child_test(struct rbd_img_request *img_request) |
1681 | { | |
1682 | smp_mb(); | |
1683 | return test_bit(IMG_REQ_CHILD, &img_request->flags) != 0; | |
1684 | } | |
1685 | ||
d0b2e944 AE |
1686 | static void img_request_layered_set(struct rbd_img_request *img_request) |
1687 | { | |
1688 | set_bit(IMG_REQ_LAYERED, &img_request->flags); | |
1689 | smp_mb(); | |
1690 | } | |
1691 | ||
a2acd00e AE |
1692 | static void img_request_layered_clear(struct rbd_img_request *img_request) |
1693 | { | |
1694 | clear_bit(IMG_REQ_LAYERED, &img_request->flags); | |
1695 | smp_mb(); | |
1696 | } | |
1697 | ||
d0b2e944 AE |
1698 | static bool img_request_layered_test(struct rbd_img_request *img_request) |
1699 | { | |
1700 | smp_mb(); | |
1701 | return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0; | |
1702 | } | |
1703 | ||
3b434a2a JD |
1704 | static enum obj_operation_type |
1705 | rbd_img_request_op_type(struct rbd_img_request *img_request) | |
1706 | { | |
1707 | if (img_request_write_test(img_request)) | |
1708 | return OBJ_OP_WRITE; | |
1709 | else if (img_request_discard_test(img_request)) | |
1710 | return OBJ_OP_DISCARD; | |
1711 | else | |
1712 | return OBJ_OP_READ; | |
1713 | } | |
1714 | ||
6e2a4505 AE |
1715 | static void |
1716 | rbd_img_obj_request_read_callback(struct rbd_obj_request *obj_request) | |
1717 | { | |
b9434c5b AE |
1718 | u64 xferred = obj_request->xferred; |
1719 | u64 length = obj_request->length; | |
1720 | ||
6e2a4505 AE |
1721 | dout("%s: obj %p img %p result %d %llu/%llu\n", __func__, |
1722 | obj_request, obj_request->img_request, obj_request->result, | |
b9434c5b | 1723 | xferred, length); |
6e2a4505 | 1724 | /* |
17c1cc1d JD |
1725 | * ENOENT means a hole in the image. We zero-fill the entire |
1726 | * length of the request. A short read also implies zero-fill | |
1727 | * to the end of the request. An error requires the whole | |
1728 | * length of the request to be reported finished with an error | |
1729 | * to the block layer. In each case we update the xferred | |
1730 | * count to indicate the whole request was satisfied. | |
6e2a4505 | 1731 | */ |
b9434c5b | 1732 | rbd_assert(obj_request->type != OBJ_REQUEST_NODATA); |
6e2a4505 | 1733 | if (obj_request->result == -ENOENT) { |
b9434c5b AE |
1734 | if (obj_request->type == OBJ_REQUEST_BIO) |
1735 | zero_bio_chain(obj_request->bio_list, 0); | |
1736 | else | |
1737 | zero_pages(obj_request->pages, 0, length); | |
6e2a4505 | 1738 | obj_request->result = 0; |
b9434c5b AE |
1739 | } else if (xferred < length && !obj_request->result) { |
1740 | if (obj_request->type == OBJ_REQUEST_BIO) | |
1741 | zero_bio_chain(obj_request->bio_list, xferred); | |
1742 | else | |
1743 | zero_pages(obj_request->pages, xferred, length); | |
6e2a4505 | 1744 | } |
17c1cc1d | 1745 | obj_request->xferred = length; |
6e2a4505 AE |
1746 | obj_request_done_set(obj_request); |
1747 | } | |
1748 | ||
bf0d5f50 AE |
1749 | static void rbd_obj_request_complete(struct rbd_obj_request *obj_request) |
1750 | { | |
37206ee5 AE |
1751 | dout("%s: obj %p cb %p\n", __func__, obj_request, |
1752 | obj_request->callback); | |
bf0d5f50 AE |
1753 | if (obj_request->callback) |
1754 | obj_request->callback(obj_request); | |
788e2df3 AE |
1755 | else |
1756 | complete_all(&obj_request->completion); | |
bf0d5f50 AE |
1757 | } |
1758 | ||
0dcc685e ID |
1759 | static void rbd_obj_request_error(struct rbd_obj_request *obj_request, int err) |
1760 | { | |
1761 | obj_request->result = err; | |
1762 | obj_request->xferred = 0; | |
1763 | /* | |
1764 | * kludge - mirror rbd_obj_request_submit() to match a put in | |
1765 | * rbd_img_obj_callback() | |
1766 | */ | |
1767 | if (obj_request_img_data_test(obj_request)) { | |
1768 | WARN_ON(obj_request->callback != rbd_img_obj_callback); | |
1769 | rbd_img_request_get(obj_request->img_request); | |
1770 | } | |
1771 | obj_request_done_set(obj_request); | |
1772 | rbd_obj_request_complete(obj_request); | |
1773 | } | |
1774 | ||
c47f9371 | 1775 | static void rbd_osd_read_callback(struct rbd_obj_request *obj_request) |
bf0d5f50 | 1776 | { |
57acbaa7 | 1777 | struct rbd_img_request *img_request = NULL; |
a9e8ba2c | 1778 | struct rbd_device *rbd_dev = NULL; |
57acbaa7 AE |
1779 | bool layered = false; |
1780 | ||
1781 | if (obj_request_img_data_test(obj_request)) { | |
1782 | img_request = obj_request->img_request; | |
1783 | layered = img_request && img_request_layered_test(img_request); | |
a9e8ba2c | 1784 | rbd_dev = img_request->rbd_dev; |
57acbaa7 | 1785 | } |
8b3e1a56 AE |
1786 | |
1787 | dout("%s: obj %p img %p result %d %llu/%llu\n", __func__, | |
1788 | obj_request, img_request, obj_request->result, | |
1789 | obj_request->xferred, obj_request->length); | |
a9e8ba2c AE |
1790 | if (layered && obj_request->result == -ENOENT && |
1791 | obj_request->img_offset < rbd_dev->parent_overlap) | |
8b3e1a56 AE |
1792 | rbd_img_parent_read(obj_request); |
1793 | else if (img_request) | |
6e2a4505 AE |
1794 | rbd_img_obj_request_read_callback(obj_request); |
1795 | else | |
1796 | obj_request_done_set(obj_request); | |
bf0d5f50 AE |
1797 | } |
1798 | ||
c47f9371 | 1799 | static void rbd_osd_write_callback(struct rbd_obj_request *obj_request) |
bf0d5f50 | 1800 | { |
1b83bef2 SW |
1801 | dout("%s: obj %p result %d %llu\n", __func__, obj_request, |
1802 | obj_request->result, obj_request->length); | |
1803 | /* | |
8b3e1a56 AE |
1804 | * There is no such thing as a successful short write. Set |
1805 | * it to our originally-requested length. | |
1b83bef2 SW |
1806 | */ |
1807 | obj_request->xferred = obj_request->length; | |
07741308 | 1808 | obj_request_done_set(obj_request); |
bf0d5f50 AE |
1809 | } |
1810 | ||
90e98c52 GZ |
1811 | static void rbd_osd_discard_callback(struct rbd_obj_request *obj_request) |
1812 | { | |
1813 | dout("%s: obj %p result %d %llu\n", __func__, obj_request, | |
1814 | obj_request->result, obj_request->length); | |
1815 | /* | |
1816 | * There is no such thing as a successful short discard. Set | |
1817 | * it to our originally-requested length. | |
1818 | */ | |
1819 | obj_request->xferred = obj_request->length; | |
d0265de7 JD |
1820 | /* discarding a non-existent object is not a problem */ |
1821 | if (obj_request->result == -ENOENT) | |
1822 | obj_request->result = 0; | |
90e98c52 GZ |
1823 | obj_request_done_set(obj_request); |
1824 | } | |
1825 | ||
fbfab539 AE |
1826 | /* |
1827 | * For a simple stat call there's nothing to do. We'll do more if | |
1828 | * this is part of a write sequence for a layered image. | |
1829 | */ | |
c47f9371 | 1830 | static void rbd_osd_stat_callback(struct rbd_obj_request *obj_request) |
fbfab539 | 1831 | { |
37206ee5 | 1832 | dout("%s: obj %p\n", __func__, obj_request); |
fbfab539 AE |
1833 | obj_request_done_set(obj_request); |
1834 | } | |
1835 | ||
2761713d ID |
1836 | static void rbd_osd_call_callback(struct rbd_obj_request *obj_request) |
1837 | { | |
1838 | dout("%s: obj %p\n", __func__, obj_request); | |
1839 | ||
1840 | if (obj_request_img_data_test(obj_request)) | |
1841 | rbd_osd_copyup_callback(obj_request); | |
1842 | else | |
1843 | obj_request_done_set(obj_request); | |
1844 | } | |
1845 | ||
85e084fe | 1846 | static void rbd_osd_req_callback(struct ceph_osd_request *osd_req) |
bf0d5f50 AE |
1847 | { |
1848 | struct rbd_obj_request *obj_request = osd_req->r_priv; | |
bf0d5f50 AE |
1849 | u16 opcode; |
1850 | ||
85e084fe | 1851 | dout("%s: osd_req %p\n", __func__, osd_req); |
bf0d5f50 | 1852 | rbd_assert(osd_req == obj_request->osd_req); |
57acbaa7 AE |
1853 | if (obj_request_img_data_test(obj_request)) { |
1854 | rbd_assert(obj_request->img_request); | |
1855 | rbd_assert(obj_request->which != BAD_WHICH); | |
1856 | } else { | |
1857 | rbd_assert(obj_request->which == BAD_WHICH); | |
1858 | } | |
bf0d5f50 | 1859 | |
1b83bef2 SW |
1860 | if (osd_req->r_result < 0) |
1861 | obj_request->result = osd_req->r_result; | |
bf0d5f50 | 1862 | |
c47f9371 AE |
1863 | /* |
1864 | * We support a 64-bit length, but ultimately it has to be | |
7ad18afa CH |
1865 | * passed to the block layer, which just supports a 32-bit |
1866 | * length field. | |
c47f9371 | 1867 | */ |
7665d85b | 1868 | obj_request->xferred = osd_req->r_ops[0].outdata_len; |
8b3e1a56 | 1869 | rbd_assert(obj_request->xferred < (u64)UINT_MAX); |
0ccd5926 | 1870 | |
79528734 | 1871 | opcode = osd_req->r_ops[0].op; |
bf0d5f50 AE |
1872 | switch (opcode) { |
1873 | case CEPH_OSD_OP_READ: | |
c47f9371 | 1874 | rbd_osd_read_callback(obj_request); |
bf0d5f50 | 1875 | break; |
0ccd5926 | 1876 | case CEPH_OSD_OP_SETALLOCHINT: |
e30b7577 ID |
1877 | rbd_assert(osd_req->r_ops[1].op == CEPH_OSD_OP_WRITE || |
1878 | osd_req->r_ops[1].op == CEPH_OSD_OP_WRITEFULL); | |
0ccd5926 | 1879 | /* fall through */ |
bf0d5f50 | 1880 | case CEPH_OSD_OP_WRITE: |
e30b7577 | 1881 | case CEPH_OSD_OP_WRITEFULL: |
c47f9371 | 1882 | rbd_osd_write_callback(obj_request); |
bf0d5f50 | 1883 | break; |
fbfab539 | 1884 | case CEPH_OSD_OP_STAT: |
c47f9371 | 1885 | rbd_osd_stat_callback(obj_request); |
fbfab539 | 1886 | break; |
90e98c52 GZ |
1887 | case CEPH_OSD_OP_DELETE: |
1888 | case CEPH_OSD_OP_TRUNCATE: | |
1889 | case CEPH_OSD_OP_ZERO: | |
1890 | rbd_osd_discard_callback(obj_request); | |
1891 | break; | |
36be9a76 | 1892 | case CEPH_OSD_OP_CALL: |
2761713d ID |
1893 | rbd_osd_call_callback(obj_request); |
1894 | break; | |
bf0d5f50 | 1895 | default: |
a90bb0c1 ID |
1896 | rbd_warn(NULL, "unexpected OSD op: object_no %016llx opcode %d", |
1897 | obj_request->object_no, opcode); | |
bf0d5f50 AE |
1898 | break; |
1899 | } | |
1900 | ||
07741308 | 1901 | if (obj_request_done_test(obj_request)) |
bf0d5f50 AE |
1902 | rbd_obj_request_complete(obj_request); |
1903 | } | |
1904 | ||
9d4df01f | 1905 | static void rbd_osd_req_format_read(struct rbd_obj_request *obj_request) |
430c28c3 | 1906 | { |
8c042b0d | 1907 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
430c28c3 | 1908 | |
7c84883a ID |
1909 | rbd_assert(obj_request_img_data_test(obj_request)); |
1910 | osd_req->r_snapid = obj_request->img_request->snap_id; | |
9d4df01f AE |
1911 | } |
1912 | ||
1913 | static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request) | |
1914 | { | |
9d4df01f | 1915 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
9d4df01f | 1916 | |
bb873b53 ID |
1917 | osd_req->r_mtime = CURRENT_TIME; |
1918 | osd_req->r_data_offset = obj_request->offset; | |
430c28c3 AE |
1919 | } |
1920 | ||
bc81207e ID |
1921 | static struct ceph_osd_request * |
1922 | __rbd_osd_req_create(struct rbd_device *rbd_dev, | |
1923 | struct ceph_snap_context *snapc, | |
1924 | int num_ops, unsigned int flags, | |
1925 | struct rbd_obj_request *obj_request) | |
1926 | { | |
1927 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; | |
1928 | struct ceph_osd_request *req; | |
a90bb0c1 ID |
1929 | const char *name_format = rbd_dev->image_format == 1 ? |
1930 | RBD_V1_DATA_FORMAT : RBD_V2_DATA_FORMAT; | |
bc81207e ID |
1931 | |
1932 | req = ceph_osdc_alloc_request(osdc, snapc, num_ops, false, GFP_NOIO); | |
1933 | if (!req) | |
1934 | return NULL; | |
1935 | ||
1936 | req->r_flags = flags; | |
1937 | req->r_callback = rbd_osd_req_callback; | |
1938 | req->r_priv = obj_request; | |
1939 | ||
1940 | req->r_base_oloc.pool = rbd_dev->layout.pool_id; | |
a90bb0c1 ID |
1941 | if (ceph_oid_aprintf(&req->r_base_oid, GFP_NOIO, name_format, |
1942 | rbd_dev->header.object_prefix, obj_request->object_no)) | |
bc81207e ID |
1943 | goto err_req; |
1944 | ||
1945 | if (ceph_osdc_alloc_messages(req, GFP_NOIO)) | |
1946 | goto err_req; | |
1947 | ||
1948 | return req; | |
1949 | ||
1950 | err_req: | |
1951 | ceph_osdc_put_request(req); | |
1952 | return NULL; | |
1953 | } | |
1954 | ||
0ccd5926 ID |
1955 | /* |
1956 | * Create an osd request. A read request has one osd op (read). | |
1957 | * A write request has either one (watch) or two (hint+write) osd ops. | |
1958 | * (All rbd data writes are prefixed with an allocation hint op, but | |
1959 | * technically osd watch is a write request, hence this distinction.) | |
1960 | */ | |
bf0d5f50 AE |
1961 | static struct ceph_osd_request *rbd_osd_req_create( |
1962 | struct rbd_device *rbd_dev, | |
6d2940c8 | 1963 | enum obj_operation_type op_type, |
deb236b3 | 1964 | unsigned int num_ops, |
430c28c3 | 1965 | struct rbd_obj_request *obj_request) |
bf0d5f50 | 1966 | { |
bf0d5f50 | 1967 | struct ceph_snap_context *snapc = NULL; |
bf0d5f50 | 1968 | |
90e98c52 GZ |
1969 | if (obj_request_img_data_test(obj_request) && |
1970 | (op_type == OBJ_OP_DISCARD || op_type == OBJ_OP_WRITE)) { | |
6365d33a | 1971 | struct rbd_img_request *img_request = obj_request->img_request; |
90e98c52 GZ |
1972 | if (op_type == OBJ_OP_WRITE) { |
1973 | rbd_assert(img_request_write_test(img_request)); | |
1974 | } else { | |
1975 | rbd_assert(img_request_discard_test(img_request)); | |
1976 | } | |
6d2940c8 | 1977 | snapc = img_request->snapc; |
bf0d5f50 AE |
1978 | } |
1979 | ||
6d2940c8 | 1980 | rbd_assert(num_ops == 1 || ((op_type == OBJ_OP_WRITE) && num_ops == 2)); |
deb236b3 | 1981 | |
bc81207e ID |
1982 | return __rbd_osd_req_create(rbd_dev, snapc, num_ops, |
1983 | (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD) ? | |
1984 | CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK : CEPH_OSD_FLAG_READ, | |
1985 | obj_request); | |
bf0d5f50 AE |
1986 | } |
1987 | ||
0eefd470 | 1988 | /* |
d3246fb0 JD |
1989 | * Create a copyup osd request based on the information in the object |
1990 | * request supplied. A copyup request has two or three osd ops, a | |
1991 | * copyup method call, potentially a hint op, and a write or truncate | |
1992 | * or zero op. | |
0eefd470 AE |
1993 | */ |
1994 | static struct ceph_osd_request * | |
1995 | rbd_osd_req_create_copyup(struct rbd_obj_request *obj_request) | |
1996 | { | |
1997 | struct rbd_img_request *img_request; | |
d3246fb0 | 1998 | int num_osd_ops = 3; |
0eefd470 AE |
1999 | |
2000 | rbd_assert(obj_request_img_data_test(obj_request)); | |
2001 | img_request = obj_request->img_request; | |
2002 | rbd_assert(img_request); | |
d3246fb0 JD |
2003 | rbd_assert(img_request_write_test(img_request) || |
2004 | img_request_discard_test(img_request)); | |
0eefd470 | 2005 | |
d3246fb0 JD |
2006 | if (img_request_discard_test(img_request)) |
2007 | num_osd_ops = 2; | |
2008 | ||
bc81207e ID |
2009 | return __rbd_osd_req_create(img_request->rbd_dev, |
2010 | img_request->snapc, num_osd_ops, | |
2011 | CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK, | |
2012 | obj_request); | |
0eefd470 AE |
2013 | } |
2014 | ||
bf0d5f50 AE |
2015 | static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req) |
2016 | { | |
2017 | ceph_osdc_put_request(osd_req); | |
2018 | } | |
2019 | ||
6c696d85 ID |
2020 | static struct rbd_obj_request * |
2021 | rbd_obj_request_create(enum obj_request_type type) | |
bf0d5f50 AE |
2022 | { |
2023 | struct rbd_obj_request *obj_request; | |
bf0d5f50 AE |
2024 | |
2025 | rbd_assert(obj_request_type_valid(type)); | |
2026 | ||
5a60e876 | 2027 | obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_NOIO); |
6c696d85 | 2028 | if (!obj_request) |
f907ad55 | 2029 | return NULL; |
f907ad55 | 2030 | |
bf0d5f50 AE |
2031 | obj_request->which = BAD_WHICH; |
2032 | obj_request->type = type; | |
2033 | INIT_LIST_HEAD(&obj_request->links); | |
788e2df3 | 2034 | init_completion(&obj_request->completion); |
bf0d5f50 AE |
2035 | kref_init(&obj_request->kref); |
2036 | ||
67e2b652 | 2037 | dout("%s %p\n", __func__, obj_request); |
bf0d5f50 AE |
2038 | return obj_request; |
2039 | } | |
2040 | ||
2041 | static void rbd_obj_request_destroy(struct kref *kref) | |
2042 | { | |
2043 | struct rbd_obj_request *obj_request; | |
2044 | ||
2045 | obj_request = container_of(kref, struct rbd_obj_request, kref); | |
2046 | ||
37206ee5 AE |
2047 | dout("%s: obj %p\n", __func__, obj_request); |
2048 | ||
bf0d5f50 AE |
2049 | rbd_assert(obj_request->img_request == NULL); |
2050 | rbd_assert(obj_request->which == BAD_WHICH); | |
2051 | ||
2052 | if (obj_request->osd_req) | |
2053 | rbd_osd_req_destroy(obj_request->osd_req); | |
2054 | ||
2055 | rbd_assert(obj_request_type_valid(obj_request->type)); | |
2056 | switch (obj_request->type) { | |
9969ebc5 AE |
2057 | case OBJ_REQUEST_NODATA: |
2058 | break; /* Nothing to do */ | |
bf0d5f50 AE |
2059 | case OBJ_REQUEST_BIO: |
2060 | if (obj_request->bio_list) | |
2061 | bio_chain_put(obj_request->bio_list); | |
2062 | break; | |
788e2df3 | 2063 | case OBJ_REQUEST_PAGES: |
04dc923c ID |
2064 | /* img_data requests don't own their page array */ |
2065 | if (obj_request->pages && | |
2066 | !obj_request_img_data_test(obj_request)) | |
788e2df3 AE |
2067 | ceph_release_page_vector(obj_request->pages, |
2068 | obj_request->page_count); | |
2069 | break; | |
bf0d5f50 AE |
2070 | } |
2071 | ||
868311b1 | 2072 | kmem_cache_free(rbd_obj_request_cache, obj_request); |
bf0d5f50 AE |
2073 | } |
2074 | ||
fb65d228 AE |
2075 | /* It's OK to call this for a device with no parent */ |
2076 | ||
2077 | static void rbd_spec_put(struct rbd_spec *spec); | |
2078 | static void rbd_dev_unparent(struct rbd_device *rbd_dev) | |
2079 | { | |
2080 | rbd_dev_remove_parent(rbd_dev); | |
2081 | rbd_spec_put(rbd_dev->parent_spec); | |
2082 | rbd_dev->parent_spec = NULL; | |
2083 | rbd_dev->parent_overlap = 0; | |
2084 | } | |
2085 | ||
a2acd00e AE |
2086 | /* |
2087 | * Parent image reference counting is used to determine when an | |
2088 | * image's parent fields can be safely torn down--after there are no | |
2089 | * more in-flight requests to the parent image. When the last | |
2090 | * reference is dropped, cleaning them up is safe. | |
2091 | */ | |
2092 | static void rbd_dev_parent_put(struct rbd_device *rbd_dev) | |
2093 | { | |
2094 | int counter; | |
2095 | ||
2096 | if (!rbd_dev->parent_spec) | |
2097 | return; | |
2098 | ||
2099 | counter = atomic_dec_return_safe(&rbd_dev->parent_ref); | |
2100 | if (counter > 0) | |
2101 | return; | |
2102 | ||
2103 | /* Last reference; clean up parent data structures */ | |
2104 | ||
2105 | if (!counter) | |
2106 | rbd_dev_unparent(rbd_dev); | |
2107 | else | |
9584d508 | 2108 | rbd_warn(rbd_dev, "parent reference underflow"); |
a2acd00e AE |
2109 | } |
2110 | ||
2111 | /* | |
2112 | * If an image has a non-zero parent overlap, get a reference to its | |
2113 | * parent. | |
2114 | * | |
2115 | * Returns true if the rbd device has a parent with a non-zero | |
2116 | * overlap and a reference for it was successfully taken, or | |
2117 | * false otherwise. | |
2118 | */ | |
2119 | static bool rbd_dev_parent_get(struct rbd_device *rbd_dev) | |
2120 | { | |
ae43e9d0 | 2121 | int counter = 0; |
a2acd00e AE |
2122 | |
2123 | if (!rbd_dev->parent_spec) | |
2124 | return false; | |
2125 | ||
ae43e9d0 ID |
2126 | down_read(&rbd_dev->header_rwsem); |
2127 | if (rbd_dev->parent_overlap) | |
2128 | counter = atomic_inc_return_safe(&rbd_dev->parent_ref); | |
2129 | up_read(&rbd_dev->header_rwsem); | |
a2acd00e AE |
2130 | |
2131 | if (counter < 0) | |
9584d508 | 2132 | rbd_warn(rbd_dev, "parent reference overflow"); |
a2acd00e | 2133 | |
ae43e9d0 | 2134 | return counter > 0; |
a2acd00e AE |
2135 | } |
2136 | ||
bf0d5f50 AE |
2137 | /* |
2138 | * Caller is responsible for filling in the list of object requests | |
2139 | * that comprises the image request, and the Linux request pointer | |
2140 | * (if there is one). | |
2141 | */ | |
cc344fa1 AE |
2142 | static struct rbd_img_request *rbd_img_request_create( |
2143 | struct rbd_device *rbd_dev, | |
bf0d5f50 | 2144 | u64 offset, u64 length, |
6d2940c8 | 2145 | enum obj_operation_type op_type, |
4e752f0a | 2146 | struct ceph_snap_context *snapc) |
bf0d5f50 AE |
2147 | { |
2148 | struct rbd_img_request *img_request; | |
bf0d5f50 | 2149 | |
7a716aac | 2150 | img_request = kmem_cache_alloc(rbd_img_request_cache, GFP_NOIO); |
bf0d5f50 AE |
2151 | if (!img_request) |
2152 | return NULL; | |
2153 | ||
bf0d5f50 AE |
2154 | img_request->rq = NULL; |
2155 | img_request->rbd_dev = rbd_dev; | |
2156 | img_request->offset = offset; | |
2157 | img_request->length = length; | |
0c425248 | 2158 | img_request->flags = 0; |
90e98c52 GZ |
2159 | if (op_type == OBJ_OP_DISCARD) { |
2160 | img_request_discard_set(img_request); | |
2161 | img_request->snapc = snapc; | |
2162 | } else if (op_type == OBJ_OP_WRITE) { | |
0c425248 | 2163 | img_request_write_set(img_request); |
4e752f0a | 2164 | img_request->snapc = snapc; |
0c425248 | 2165 | } else { |
bf0d5f50 | 2166 | img_request->snap_id = rbd_dev->spec->snap_id; |
0c425248 | 2167 | } |
a2acd00e | 2168 | if (rbd_dev_parent_get(rbd_dev)) |
d0b2e944 | 2169 | img_request_layered_set(img_request); |
bf0d5f50 AE |
2170 | spin_lock_init(&img_request->completion_lock); |
2171 | img_request->next_completion = 0; | |
2172 | img_request->callback = NULL; | |
a5a337d4 | 2173 | img_request->result = 0; |
bf0d5f50 AE |
2174 | img_request->obj_request_count = 0; |
2175 | INIT_LIST_HEAD(&img_request->obj_requests); | |
2176 | kref_init(&img_request->kref); | |
2177 | ||
37206ee5 | 2178 | dout("%s: rbd_dev %p %s %llu/%llu -> img %p\n", __func__, rbd_dev, |
6d2940c8 | 2179 | obj_op_name(op_type), offset, length, img_request); |
37206ee5 | 2180 | |
bf0d5f50 AE |
2181 | return img_request; |
2182 | } | |
2183 | ||
2184 | static void rbd_img_request_destroy(struct kref *kref) | |
2185 | { | |
2186 | struct rbd_img_request *img_request; | |
2187 | struct rbd_obj_request *obj_request; | |
2188 | struct rbd_obj_request *next_obj_request; | |
2189 | ||
2190 | img_request = container_of(kref, struct rbd_img_request, kref); | |
2191 | ||
37206ee5 AE |
2192 | dout("%s: img %p\n", __func__, img_request); |
2193 | ||
bf0d5f50 AE |
2194 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) |
2195 | rbd_img_obj_request_del(img_request, obj_request); | |
25dcf954 | 2196 | rbd_assert(img_request->obj_request_count == 0); |
bf0d5f50 | 2197 | |
a2acd00e AE |
2198 | if (img_request_layered_test(img_request)) { |
2199 | img_request_layered_clear(img_request); | |
2200 | rbd_dev_parent_put(img_request->rbd_dev); | |
2201 | } | |
2202 | ||
bef95455 JD |
2203 | if (img_request_write_test(img_request) || |
2204 | img_request_discard_test(img_request)) | |
812164f8 | 2205 | ceph_put_snap_context(img_request->snapc); |
bf0d5f50 | 2206 | |
1c2a9dfe | 2207 | kmem_cache_free(rbd_img_request_cache, img_request); |
bf0d5f50 AE |
2208 | } |
2209 | ||
e93f3152 AE |
2210 | static struct rbd_img_request *rbd_parent_request_create( |
2211 | struct rbd_obj_request *obj_request, | |
2212 | u64 img_offset, u64 length) | |
2213 | { | |
2214 | struct rbd_img_request *parent_request; | |
2215 | struct rbd_device *rbd_dev; | |
2216 | ||
2217 | rbd_assert(obj_request->img_request); | |
2218 | rbd_dev = obj_request->img_request->rbd_dev; | |
2219 | ||
4e752f0a | 2220 | parent_request = rbd_img_request_create(rbd_dev->parent, img_offset, |
6d2940c8 | 2221 | length, OBJ_OP_READ, NULL); |
e93f3152 AE |
2222 | if (!parent_request) |
2223 | return NULL; | |
2224 | ||
2225 | img_request_child_set(parent_request); | |
2226 | rbd_obj_request_get(obj_request); | |
2227 | parent_request->obj_request = obj_request; | |
2228 | ||
2229 | return parent_request; | |
2230 | } | |
2231 | ||
2232 | static void rbd_parent_request_destroy(struct kref *kref) | |
2233 | { | |
2234 | struct rbd_img_request *parent_request; | |
2235 | struct rbd_obj_request *orig_request; | |
2236 | ||
2237 | parent_request = container_of(kref, struct rbd_img_request, kref); | |
2238 | orig_request = parent_request->obj_request; | |
2239 | ||
2240 | parent_request->obj_request = NULL; | |
2241 | rbd_obj_request_put(orig_request); | |
2242 | img_request_child_clear(parent_request); | |
2243 | ||
2244 | rbd_img_request_destroy(kref); | |
2245 | } | |
2246 | ||
1217857f AE |
2247 | static bool rbd_img_obj_end_request(struct rbd_obj_request *obj_request) |
2248 | { | |
6365d33a | 2249 | struct rbd_img_request *img_request; |
1217857f AE |
2250 | unsigned int xferred; |
2251 | int result; | |
8b3e1a56 | 2252 | bool more; |
1217857f | 2253 | |
6365d33a AE |
2254 | rbd_assert(obj_request_img_data_test(obj_request)); |
2255 | img_request = obj_request->img_request; | |
2256 | ||
1217857f AE |
2257 | rbd_assert(obj_request->xferred <= (u64)UINT_MAX); |
2258 | xferred = (unsigned int)obj_request->xferred; | |
2259 | result = obj_request->result; | |
2260 | if (result) { | |
2261 | struct rbd_device *rbd_dev = img_request->rbd_dev; | |
6d2940c8 GZ |
2262 | enum obj_operation_type op_type; |
2263 | ||
90e98c52 GZ |
2264 | if (img_request_discard_test(img_request)) |
2265 | op_type = OBJ_OP_DISCARD; | |
2266 | else if (img_request_write_test(img_request)) | |
2267 | op_type = OBJ_OP_WRITE; | |
2268 | else | |
2269 | op_type = OBJ_OP_READ; | |
1217857f | 2270 | |
9584d508 | 2271 | rbd_warn(rbd_dev, "%s %llx at %llx (%llx)", |
6d2940c8 GZ |
2272 | obj_op_name(op_type), obj_request->length, |
2273 | obj_request->img_offset, obj_request->offset); | |
9584d508 | 2274 | rbd_warn(rbd_dev, " result %d xferred %x", |
1217857f AE |
2275 | result, xferred); |
2276 | if (!img_request->result) | |
2277 | img_request->result = result; | |
082a75da ID |
2278 | /* |
2279 | * Need to end I/O on the entire obj_request worth of | |
2280 | * bytes in case of error. | |
2281 | */ | |
2282 | xferred = obj_request->length; | |
1217857f AE |
2283 | } |
2284 | ||
8b3e1a56 AE |
2285 | if (img_request_child_test(img_request)) { |
2286 | rbd_assert(img_request->obj_request != NULL); | |
2287 | more = obj_request->which < img_request->obj_request_count - 1; | |
2288 | } else { | |
2289 | rbd_assert(img_request->rq != NULL); | |
7ad18afa CH |
2290 | |
2291 | more = blk_update_request(img_request->rq, result, xferred); | |
2292 | if (!more) | |
2293 | __blk_mq_end_request(img_request->rq, result); | |
8b3e1a56 AE |
2294 | } |
2295 | ||
2296 | return more; | |
1217857f AE |
2297 | } |
2298 | ||
2169238d AE |
2299 | static void rbd_img_obj_callback(struct rbd_obj_request *obj_request) |
2300 | { | |
2301 | struct rbd_img_request *img_request; | |
2302 | u32 which = obj_request->which; | |
2303 | bool more = true; | |
2304 | ||
6365d33a | 2305 | rbd_assert(obj_request_img_data_test(obj_request)); |
2169238d AE |
2306 | img_request = obj_request->img_request; |
2307 | ||
2308 | dout("%s: img %p obj %p\n", __func__, img_request, obj_request); | |
2309 | rbd_assert(img_request != NULL); | |
2169238d AE |
2310 | rbd_assert(img_request->obj_request_count > 0); |
2311 | rbd_assert(which != BAD_WHICH); | |
2312 | rbd_assert(which < img_request->obj_request_count); | |
2169238d AE |
2313 | |
2314 | spin_lock_irq(&img_request->completion_lock); | |
2315 | if (which != img_request->next_completion) | |
2316 | goto out; | |
2317 | ||
2318 | for_each_obj_request_from(img_request, obj_request) { | |
2169238d AE |
2319 | rbd_assert(more); |
2320 | rbd_assert(which < img_request->obj_request_count); | |
2321 | ||
2322 | if (!obj_request_done_test(obj_request)) | |
2323 | break; | |
1217857f | 2324 | more = rbd_img_obj_end_request(obj_request); |
2169238d AE |
2325 | which++; |
2326 | } | |
2327 | ||
2328 | rbd_assert(more ^ (which == img_request->obj_request_count)); | |
2329 | img_request->next_completion = which; | |
2330 | out: | |
2331 | spin_unlock_irq(&img_request->completion_lock); | |
0f2d5be7 | 2332 | rbd_img_request_put(img_request); |
2169238d AE |
2333 | |
2334 | if (!more) | |
2335 | rbd_img_request_complete(img_request); | |
2336 | } | |
2337 | ||
3b434a2a JD |
2338 | /* |
2339 | * Add individual osd ops to the given ceph_osd_request and prepare | |
2340 | * them for submission. num_ops is the current number of | |
2341 | * osd operations already to the object request. | |
2342 | */ | |
2343 | static void rbd_img_obj_request_fill(struct rbd_obj_request *obj_request, | |
2344 | struct ceph_osd_request *osd_request, | |
2345 | enum obj_operation_type op_type, | |
2346 | unsigned int num_ops) | |
2347 | { | |
2348 | struct rbd_img_request *img_request = obj_request->img_request; | |
2349 | struct rbd_device *rbd_dev = img_request->rbd_dev; | |
2350 | u64 object_size = rbd_obj_bytes(&rbd_dev->header); | |
2351 | u64 offset = obj_request->offset; | |
2352 | u64 length = obj_request->length; | |
2353 | u64 img_end; | |
2354 | u16 opcode; | |
2355 | ||
2356 | if (op_type == OBJ_OP_DISCARD) { | |
d3246fb0 JD |
2357 | if (!offset && length == object_size && |
2358 | (!img_request_layered_test(img_request) || | |
2359 | !obj_request_overlaps_parent(obj_request))) { | |
3b434a2a JD |
2360 | opcode = CEPH_OSD_OP_DELETE; |
2361 | } else if ((offset + length == object_size)) { | |
2362 | opcode = CEPH_OSD_OP_TRUNCATE; | |
2363 | } else { | |
2364 | down_read(&rbd_dev->header_rwsem); | |
2365 | img_end = rbd_dev->header.image_size; | |
2366 | up_read(&rbd_dev->header_rwsem); | |
2367 | ||
2368 | if (obj_request->img_offset + length == img_end) | |
2369 | opcode = CEPH_OSD_OP_TRUNCATE; | |
2370 | else | |
2371 | opcode = CEPH_OSD_OP_ZERO; | |
2372 | } | |
2373 | } else if (op_type == OBJ_OP_WRITE) { | |
e30b7577 ID |
2374 | if (!offset && length == object_size) |
2375 | opcode = CEPH_OSD_OP_WRITEFULL; | |
2376 | else | |
2377 | opcode = CEPH_OSD_OP_WRITE; | |
3b434a2a JD |
2378 | osd_req_op_alloc_hint_init(osd_request, num_ops, |
2379 | object_size, object_size); | |
2380 | num_ops++; | |
2381 | } else { | |
2382 | opcode = CEPH_OSD_OP_READ; | |
2383 | } | |
2384 | ||
7e868b6e | 2385 | if (opcode == CEPH_OSD_OP_DELETE) |
144cba14 | 2386 | osd_req_op_init(osd_request, num_ops, opcode, 0); |
7e868b6e ID |
2387 | else |
2388 | osd_req_op_extent_init(osd_request, num_ops, opcode, | |
2389 | offset, length, 0, 0); | |
2390 | ||
3b434a2a JD |
2391 | if (obj_request->type == OBJ_REQUEST_BIO) |
2392 | osd_req_op_extent_osd_data_bio(osd_request, num_ops, | |
2393 | obj_request->bio_list, length); | |
2394 | else if (obj_request->type == OBJ_REQUEST_PAGES) | |
2395 | osd_req_op_extent_osd_data_pages(osd_request, num_ops, | |
2396 | obj_request->pages, length, | |
2397 | offset & ~PAGE_MASK, false, false); | |
2398 | ||
2399 | /* Discards are also writes */ | |
2400 | if (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD) | |
2401 | rbd_osd_req_format_write(obj_request); | |
2402 | else | |
2403 | rbd_osd_req_format_read(obj_request); | |
2404 | } | |
2405 | ||
f1a4739f AE |
2406 | /* |
2407 | * Split up an image request into one or more object requests, each | |
2408 | * to a different object. The "type" parameter indicates whether | |
2409 | * "data_desc" is the pointer to the head of a list of bio | |
2410 | * structures, or the base of a page array. In either case this | |
2411 | * function assumes data_desc describes memory sufficient to hold | |
2412 | * all data described by the image request. | |
2413 | */ | |
2414 | static int rbd_img_request_fill(struct rbd_img_request *img_request, | |
2415 | enum obj_request_type type, | |
2416 | void *data_desc) | |
bf0d5f50 AE |
2417 | { |
2418 | struct rbd_device *rbd_dev = img_request->rbd_dev; | |
2419 | struct rbd_obj_request *obj_request = NULL; | |
2420 | struct rbd_obj_request *next_obj_request; | |
a158073c | 2421 | struct bio *bio_list = NULL; |
f1a4739f | 2422 | unsigned int bio_offset = 0; |
a158073c | 2423 | struct page **pages = NULL; |
6d2940c8 | 2424 | enum obj_operation_type op_type; |
7da22d29 | 2425 | u64 img_offset; |
bf0d5f50 | 2426 | u64 resid; |
bf0d5f50 | 2427 | |
f1a4739f AE |
2428 | dout("%s: img %p type %d data_desc %p\n", __func__, img_request, |
2429 | (int)type, data_desc); | |
37206ee5 | 2430 | |
7da22d29 | 2431 | img_offset = img_request->offset; |
bf0d5f50 | 2432 | resid = img_request->length; |
4dda41d3 | 2433 | rbd_assert(resid > 0); |
3b434a2a | 2434 | op_type = rbd_img_request_op_type(img_request); |
f1a4739f AE |
2435 | |
2436 | if (type == OBJ_REQUEST_BIO) { | |
2437 | bio_list = data_desc; | |
4f024f37 KO |
2438 | rbd_assert(img_offset == |
2439 | bio_list->bi_iter.bi_sector << SECTOR_SHIFT); | |
90e98c52 | 2440 | } else if (type == OBJ_REQUEST_PAGES) { |
f1a4739f AE |
2441 | pages = data_desc; |
2442 | } | |
2443 | ||
bf0d5f50 | 2444 | while (resid) { |
2fa12320 | 2445 | struct ceph_osd_request *osd_req; |
a90bb0c1 | 2446 | u64 object_no = img_offset >> rbd_dev->header.obj_order; |
67e2b652 ID |
2447 | u64 offset = rbd_segment_offset(rbd_dev, img_offset); |
2448 | u64 length = rbd_segment_length(rbd_dev, img_offset, resid); | |
bf0d5f50 | 2449 | |
6c696d85 | 2450 | obj_request = rbd_obj_request_create(type); |
bf0d5f50 AE |
2451 | if (!obj_request) |
2452 | goto out_unwind; | |
62054da6 | 2453 | |
a90bb0c1 | 2454 | obj_request->object_no = object_no; |
67e2b652 ID |
2455 | obj_request->offset = offset; |
2456 | obj_request->length = length; | |
2457 | ||
03507db6 JD |
2458 | /* |
2459 | * set obj_request->img_request before creating the | |
2460 | * osd_request so that it gets the right snapc | |
2461 | */ | |
2462 | rbd_img_obj_request_add(img_request, obj_request); | |
bf0d5f50 | 2463 | |
f1a4739f AE |
2464 | if (type == OBJ_REQUEST_BIO) { |
2465 | unsigned int clone_size; | |
2466 | ||
2467 | rbd_assert(length <= (u64)UINT_MAX); | |
2468 | clone_size = (unsigned int)length; | |
2469 | obj_request->bio_list = | |
2470 | bio_chain_clone_range(&bio_list, | |
2471 | &bio_offset, | |
2472 | clone_size, | |
2224d879 | 2473 | GFP_NOIO); |
f1a4739f | 2474 | if (!obj_request->bio_list) |
62054da6 | 2475 | goto out_unwind; |
90e98c52 | 2476 | } else if (type == OBJ_REQUEST_PAGES) { |
f1a4739f AE |
2477 | unsigned int page_count; |
2478 | ||
2479 | obj_request->pages = pages; | |
2480 | page_count = (u32)calc_pages_for(offset, length); | |
2481 | obj_request->page_count = page_count; | |
2482 | if ((offset + length) & ~PAGE_MASK) | |
2483 | page_count--; /* more on last page */ | |
2484 | pages += page_count; | |
2485 | } | |
bf0d5f50 | 2486 | |
6d2940c8 GZ |
2487 | osd_req = rbd_osd_req_create(rbd_dev, op_type, |
2488 | (op_type == OBJ_OP_WRITE) ? 2 : 1, | |
2489 | obj_request); | |
2fa12320 | 2490 | if (!osd_req) |
62054da6 | 2491 | goto out_unwind; |
3b434a2a | 2492 | |
2fa12320 | 2493 | obj_request->osd_req = osd_req; |
2169238d | 2494 | obj_request->callback = rbd_img_obj_callback; |
3b434a2a | 2495 | obj_request->img_offset = img_offset; |
9d4df01f | 2496 | |
3b434a2a | 2497 | rbd_img_obj_request_fill(obj_request, osd_req, op_type, 0); |
430c28c3 | 2498 | |
7da22d29 | 2499 | img_offset += length; |
bf0d5f50 AE |
2500 | resid -= length; |
2501 | } | |
2502 | ||
2503 | return 0; | |
2504 | ||
bf0d5f50 AE |
2505 | out_unwind: |
2506 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) | |
42dd037c | 2507 | rbd_img_obj_request_del(img_request, obj_request); |
bf0d5f50 AE |
2508 | |
2509 | return -ENOMEM; | |
2510 | } | |
2511 | ||
0eefd470 | 2512 | static void |
2761713d | 2513 | rbd_osd_copyup_callback(struct rbd_obj_request *obj_request) |
0eefd470 AE |
2514 | { |
2515 | struct rbd_img_request *img_request; | |
2516 | struct rbd_device *rbd_dev; | |
ebda6408 | 2517 | struct page **pages; |
0eefd470 AE |
2518 | u32 page_count; |
2519 | ||
2761713d ID |
2520 | dout("%s: obj %p\n", __func__, obj_request); |
2521 | ||
d3246fb0 JD |
2522 | rbd_assert(obj_request->type == OBJ_REQUEST_BIO || |
2523 | obj_request->type == OBJ_REQUEST_NODATA); | |
0eefd470 AE |
2524 | rbd_assert(obj_request_img_data_test(obj_request)); |
2525 | img_request = obj_request->img_request; | |
2526 | rbd_assert(img_request); | |
2527 | ||
2528 | rbd_dev = img_request->rbd_dev; | |
2529 | rbd_assert(rbd_dev); | |
0eefd470 | 2530 | |
ebda6408 AE |
2531 | pages = obj_request->copyup_pages; |
2532 | rbd_assert(pages != NULL); | |
0eefd470 | 2533 | obj_request->copyup_pages = NULL; |
ebda6408 AE |
2534 | page_count = obj_request->copyup_page_count; |
2535 | rbd_assert(page_count); | |
2536 | obj_request->copyup_page_count = 0; | |
2537 | ceph_release_page_vector(pages, page_count); | |
0eefd470 AE |
2538 | |
2539 | /* | |
2540 | * We want the transfer count to reflect the size of the | |
2541 | * original write request. There is no such thing as a | |
2542 | * successful short write, so if the request was successful | |
2543 | * we can just set it to the originally-requested length. | |
2544 | */ | |
2545 | if (!obj_request->result) | |
2546 | obj_request->xferred = obj_request->length; | |
2547 | ||
2761713d | 2548 | obj_request_done_set(obj_request); |
0eefd470 AE |
2549 | } |
2550 | ||
3d7efd18 AE |
2551 | static void |
2552 | rbd_img_obj_parent_read_full_callback(struct rbd_img_request *img_request) | |
2553 | { | |
2554 | struct rbd_obj_request *orig_request; | |
0eefd470 | 2555 | struct ceph_osd_request *osd_req; |
0eefd470 | 2556 | struct rbd_device *rbd_dev; |
3d7efd18 | 2557 | struct page **pages; |
d3246fb0 | 2558 | enum obj_operation_type op_type; |
ebda6408 | 2559 | u32 page_count; |
bbea1c1a | 2560 | int img_result; |
ebda6408 | 2561 | u64 parent_length; |
3d7efd18 AE |
2562 | |
2563 | rbd_assert(img_request_child_test(img_request)); | |
2564 | ||
2565 | /* First get what we need from the image request */ | |
2566 | ||
2567 | pages = img_request->copyup_pages; | |
2568 | rbd_assert(pages != NULL); | |
2569 | img_request->copyup_pages = NULL; | |
ebda6408 AE |
2570 | page_count = img_request->copyup_page_count; |
2571 | rbd_assert(page_count); | |
2572 | img_request->copyup_page_count = 0; | |
3d7efd18 AE |
2573 | |
2574 | orig_request = img_request->obj_request; | |
2575 | rbd_assert(orig_request != NULL); | |
b91f09f1 | 2576 | rbd_assert(obj_request_type_valid(orig_request->type)); |
bbea1c1a | 2577 | img_result = img_request->result; |
ebda6408 | 2578 | parent_length = img_request->length; |
fa355112 | 2579 | rbd_assert(img_result || parent_length == img_request->xferred); |
91c6febb | 2580 | rbd_img_request_put(img_request); |
3d7efd18 | 2581 | |
91c6febb AE |
2582 | rbd_assert(orig_request->img_request); |
2583 | rbd_dev = orig_request->img_request->rbd_dev; | |
0eefd470 | 2584 | rbd_assert(rbd_dev); |
0eefd470 | 2585 | |
bbea1c1a AE |
2586 | /* |
2587 | * If the overlap has become 0 (most likely because the | |
2588 | * image has been flattened) we need to free the pages | |
2589 | * and re-submit the original write request. | |
2590 | */ | |
2591 | if (!rbd_dev->parent_overlap) { | |
bbea1c1a | 2592 | ceph_release_page_vector(pages, page_count); |
980917fc ID |
2593 | rbd_obj_request_submit(orig_request); |
2594 | return; | |
bbea1c1a | 2595 | } |
0eefd470 | 2596 | |
bbea1c1a | 2597 | if (img_result) |
0eefd470 | 2598 | goto out_err; |
0eefd470 | 2599 | |
8785b1d4 AE |
2600 | /* |
2601 | * The original osd request is of no use to use any more. | |
0ccd5926 | 2602 | * We need a new one that can hold the three ops in a copyup |
8785b1d4 AE |
2603 | * request. Allocate the new copyup osd request for the |
2604 | * original request, and release the old one. | |
2605 | */ | |
bbea1c1a | 2606 | img_result = -ENOMEM; |
0eefd470 AE |
2607 | osd_req = rbd_osd_req_create_copyup(orig_request); |
2608 | if (!osd_req) | |
2609 | goto out_err; | |
8785b1d4 | 2610 | rbd_osd_req_destroy(orig_request->osd_req); |
0eefd470 AE |
2611 | orig_request->osd_req = osd_req; |
2612 | orig_request->copyup_pages = pages; | |
ebda6408 | 2613 | orig_request->copyup_page_count = page_count; |
3d7efd18 | 2614 | |
0eefd470 | 2615 | /* Initialize the copyup op */ |
3d7efd18 | 2616 | |
0eefd470 | 2617 | osd_req_op_cls_init(osd_req, 0, CEPH_OSD_OP_CALL, "rbd", "copyup"); |
ebda6408 | 2618 | osd_req_op_cls_request_data_pages(osd_req, 0, pages, parent_length, 0, |
0eefd470 | 2619 | false, false); |
3d7efd18 | 2620 | |
d3246fb0 | 2621 | /* Add the other op(s) */ |
0eefd470 | 2622 | |
d3246fb0 JD |
2623 | op_type = rbd_img_request_op_type(orig_request->img_request); |
2624 | rbd_img_obj_request_fill(orig_request, osd_req, op_type, 1); | |
0eefd470 AE |
2625 | |
2626 | /* All set, send it off. */ | |
2627 | ||
980917fc ID |
2628 | rbd_obj_request_submit(orig_request); |
2629 | return; | |
0eefd470 | 2630 | |
0eefd470 | 2631 | out_err: |
fa355112 | 2632 | ceph_release_page_vector(pages, page_count); |
0dcc685e | 2633 | rbd_obj_request_error(orig_request, img_result); |
3d7efd18 AE |
2634 | } |
2635 | ||
2636 | /* | |
2637 | * Read from the parent image the range of data that covers the | |
2638 | * entire target of the given object request. This is used for | |
2639 | * satisfying a layered image write request when the target of an | |
2640 | * object request from the image request does not exist. | |
2641 | * | |
2642 | * A page array big enough to hold the returned data is allocated | |
2643 | * and supplied to rbd_img_request_fill() as the "data descriptor." | |
2644 | * When the read completes, this page array will be transferred to | |
2645 | * the original object request for the copyup operation. | |
2646 | * | |
c2e82414 ID |
2647 | * If an error occurs, it is recorded as the result of the original |
2648 | * object request in rbd_img_obj_exists_callback(). | |
3d7efd18 AE |
2649 | */ |
2650 | static int rbd_img_obj_parent_read_full(struct rbd_obj_request *obj_request) | |
2651 | { | |
058aa991 | 2652 | struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev; |
3d7efd18 | 2653 | struct rbd_img_request *parent_request = NULL; |
3d7efd18 AE |
2654 | u64 img_offset; |
2655 | u64 length; | |
2656 | struct page **pages = NULL; | |
2657 | u32 page_count; | |
2658 | int result; | |
2659 | ||
3d7efd18 AE |
2660 | rbd_assert(rbd_dev->parent != NULL); |
2661 | ||
2662 | /* | |
2663 | * Determine the byte range covered by the object in the | |
2664 | * child image to which the original request was to be sent. | |
2665 | */ | |
2666 | img_offset = obj_request->img_offset - obj_request->offset; | |
5bc3fb17 | 2667 | length = rbd_obj_bytes(&rbd_dev->header); |
3d7efd18 | 2668 | |
a9e8ba2c AE |
2669 | /* |
2670 | * There is no defined parent data beyond the parent | |
2671 | * overlap, so limit what we read at that boundary if | |
2672 | * necessary. | |
2673 | */ | |
2674 | if (img_offset + length > rbd_dev->parent_overlap) { | |
2675 | rbd_assert(img_offset < rbd_dev->parent_overlap); | |
2676 | length = rbd_dev->parent_overlap - img_offset; | |
2677 | } | |
2678 | ||
3d7efd18 AE |
2679 | /* |
2680 | * Allocate a page array big enough to receive the data read | |
2681 | * from the parent. | |
2682 | */ | |
2683 | page_count = (u32)calc_pages_for(0, length); | |
2684 | pages = ceph_alloc_page_vector(page_count, GFP_KERNEL); | |
2685 | if (IS_ERR(pages)) { | |
2686 | result = PTR_ERR(pages); | |
2687 | pages = NULL; | |
2688 | goto out_err; | |
2689 | } | |
2690 | ||
2691 | result = -ENOMEM; | |
e93f3152 AE |
2692 | parent_request = rbd_parent_request_create(obj_request, |
2693 | img_offset, length); | |
3d7efd18 AE |
2694 | if (!parent_request) |
2695 | goto out_err; | |
3d7efd18 AE |
2696 | |
2697 | result = rbd_img_request_fill(parent_request, OBJ_REQUEST_PAGES, pages); | |
2698 | if (result) | |
2699 | goto out_err; | |
058aa991 | 2700 | |
3d7efd18 | 2701 | parent_request->copyup_pages = pages; |
ebda6408 | 2702 | parent_request->copyup_page_count = page_count; |
3d7efd18 | 2703 | parent_request->callback = rbd_img_obj_parent_read_full_callback; |
058aa991 | 2704 | |
3d7efd18 AE |
2705 | result = rbd_img_request_submit(parent_request); |
2706 | if (!result) | |
2707 | return 0; | |
2708 | ||
2709 | parent_request->copyup_pages = NULL; | |
ebda6408 | 2710 | parent_request->copyup_page_count = 0; |
3d7efd18 AE |
2711 | parent_request->obj_request = NULL; |
2712 | rbd_obj_request_put(obj_request); | |
2713 | out_err: | |
2714 | if (pages) | |
2715 | ceph_release_page_vector(pages, page_count); | |
2716 | if (parent_request) | |
2717 | rbd_img_request_put(parent_request); | |
3d7efd18 AE |
2718 | return result; |
2719 | } | |
2720 | ||
c5b5ef6c AE |
2721 | static void rbd_img_obj_exists_callback(struct rbd_obj_request *obj_request) |
2722 | { | |
c5b5ef6c | 2723 | struct rbd_obj_request *orig_request; |
638f5abe | 2724 | struct rbd_device *rbd_dev; |
c5b5ef6c AE |
2725 | int result; |
2726 | ||
2727 | rbd_assert(!obj_request_img_data_test(obj_request)); | |
2728 | ||
2729 | /* | |
2730 | * All we need from the object request is the original | |
2731 | * request and the result of the STAT op. Grab those, then | |
2732 | * we're done with the request. | |
2733 | */ | |
2734 | orig_request = obj_request->obj_request; | |
2735 | obj_request->obj_request = NULL; | |
912c317d | 2736 | rbd_obj_request_put(orig_request); |
c5b5ef6c AE |
2737 | rbd_assert(orig_request); |
2738 | rbd_assert(orig_request->img_request); | |
2739 | ||
2740 | result = obj_request->result; | |
2741 | obj_request->result = 0; | |
2742 | ||
2743 | dout("%s: obj %p for obj %p result %d %llu/%llu\n", __func__, | |
2744 | obj_request, orig_request, result, | |
2745 | obj_request->xferred, obj_request->length); | |
2746 | rbd_obj_request_put(obj_request); | |
2747 | ||
638f5abe AE |
2748 | /* |
2749 | * If the overlap has become 0 (most likely because the | |
980917fc ID |
2750 | * image has been flattened) we need to re-submit the |
2751 | * original request. | |
638f5abe AE |
2752 | */ |
2753 | rbd_dev = orig_request->img_request->rbd_dev; | |
2754 | if (!rbd_dev->parent_overlap) { | |
980917fc ID |
2755 | rbd_obj_request_submit(orig_request); |
2756 | return; | |
638f5abe | 2757 | } |
c5b5ef6c AE |
2758 | |
2759 | /* | |
2760 | * Our only purpose here is to determine whether the object | |
2761 | * exists, and we don't want to treat the non-existence as | |
2762 | * an error. If something else comes back, transfer the | |
2763 | * error to the original request and complete it now. | |
2764 | */ | |
2765 | if (!result) { | |
2766 | obj_request_existence_set(orig_request, true); | |
2767 | } else if (result == -ENOENT) { | |
2768 | obj_request_existence_set(orig_request, false); | |
c2e82414 ID |
2769 | } else { |
2770 | goto fail_orig_request; | |
c5b5ef6c AE |
2771 | } |
2772 | ||
2773 | /* | |
2774 | * Resubmit the original request now that we have recorded | |
2775 | * whether the target object exists. | |
2776 | */ | |
c2e82414 ID |
2777 | result = rbd_img_obj_request_submit(orig_request); |
2778 | if (result) | |
2779 | goto fail_orig_request; | |
2780 | ||
2781 | return; | |
2782 | ||
2783 | fail_orig_request: | |
0dcc685e | 2784 | rbd_obj_request_error(orig_request, result); |
c5b5ef6c AE |
2785 | } |
2786 | ||
2787 | static int rbd_img_obj_exists_submit(struct rbd_obj_request *obj_request) | |
2788 | { | |
058aa991 | 2789 | struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev; |
c5b5ef6c | 2790 | struct rbd_obj_request *stat_request; |
710214e3 | 2791 | struct page **pages; |
c5b5ef6c AE |
2792 | u32 page_count; |
2793 | size_t size; | |
2794 | int ret; | |
2795 | ||
6c696d85 | 2796 | stat_request = rbd_obj_request_create(OBJ_REQUEST_PAGES); |
710214e3 ID |
2797 | if (!stat_request) |
2798 | return -ENOMEM; | |
2799 | ||
a90bb0c1 ID |
2800 | stat_request->object_no = obj_request->object_no; |
2801 | ||
710214e3 ID |
2802 | stat_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_READ, 1, |
2803 | stat_request); | |
2804 | if (!stat_request->osd_req) { | |
2805 | ret = -ENOMEM; | |
2806 | goto fail_stat_request; | |
2807 | } | |
2808 | ||
c5b5ef6c AE |
2809 | /* |
2810 | * The response data for a STAT call consists of: | |
2811 | * le64 length; | |
2812 | * struct { | |
2813 | * le32 tv_sec; | |
2814 | * le32 tv_nsec; | |
2815 | * } mtime; | |
2816 | */ | |
2817 | size = sizeof (__le64) + sizeof (__le32) + sizeof (__le32); | |
2818 | page_count = (u32)calc_pages_for(0, size); | |
2819 | pages = ceph_alloc_page_vector(page_count, GFP_KERNEL); | |
710214e3 ID |
2820 | if (IS_ERR(pages)) { |
2821 | ret = PTR_ERR(pages); | |
2822 | goto fail_stat_request; | |
2823 | } | |
c5b5ef6c | 2824 | |
710214e3 ID |
2825 | osd_req_op_init(stat_request->osd_req, 0, CEPH_OSD_OP_STAT, 0); |
2826 | osd_req_op_raw_data_in_pages(stat_request->osd_req, 0, pages, size, 0, | |
2827 | false, false); | |
c5b5ef6c AE |
2828 | |
2829 | rbd_obj_request_get(obj_request); | |
2830 | stat_request->obj_request = obj_request; | |
2831 | stat_request->pages = pages; | |
2832 | stat_request->page_count = page_count; | |
c5b5ef6c AE |
2833 | stat_request->callback = rbd_img_obj_exists_callback; |
2834 | ||
980917fc ID |
2835 | rbd_obj_request_submit(stat_request); |
2836 | return 0; | |
c5b5ef6c | 2837 | |
710214e3 ID |
2838 | fail_stat_request: |
2839 | rbd_obj_request_put(stat_request); | |
c5b5ef6c AE |
2840 | return ret; |
2841 | } | |
2842 | ||
70d045f6 | 2843 | static bool img_obj_request_simple(struct rbd_obj_request *obj_request) |
b454e36d | 2844 | { |
058aa991 ID |
2845 | struct rbd_img_request *img_request = obj_request->img_request; |
2846 | struct rbd_device *rbd_dev = img_request->rbd_dev; | |
b454e36d | 2847 | |
70d045f6 | 2848 | /* Reads */ |
1c220881 JD |
2849 | if (!img_request_write_test(img_request) && |
2850 | !img_request_discard_test(img_request)) | |
70d045f6 ID |
2851 | return true; |
2852 | ||
2853 | /* Non-layered writes */ | |
2854 | if (!img_request_layered_test(img_request)) | |
2855 | return true; | |
2856 | ||
b454e36d | 2857 | /* |
70d045f6 ID |
2858 | * Layered writes outside of the parent overlap range don't |
2859 | * share any data with the parent. | |
b454e36d | 2860 | */ |
70d045f6 ID |
2861 | if (!obj_request_overlaps_parent(obj_request)) |
2862 | return true; | |
b454e36d | 2863 | |
c622d226 GZ |
2864 | /* |
2865 | * Entire-object layered writes - we will overwrite whatever | |
2866 | * parent data there is anyway. | |
2867 | */ | |
2868 | if (!obj_request->offset && | |
2869 | obj_request->length == rbd_obj_bytes(&rbd_dev->header)) | |
2870 | return true; | |
2871 | ||
70d045f6 ID |
2872 | /* |
2873 | * If the object is known to already exist, its parent data has | |
2874 | * already been copied. | |
2875 | */ | |
2876 | if (obj_request_known_test(obj_request) && | |
2877 | obj_request_exists_test(obj_request)) | |
2878 | return true; | |
2879 | ||
2880 | return false; | |
2881 | } | |
2882 | ||
2883 | static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request) | |
2884 | { | |
058aa991 ID |
2885 | rbd_assert(obj_request_img_data_test(obj_request)); |
2886 | rbd_assert(obj_request_type_valid(obj_request->type)); | |
2887 | rbd_assert(obj_request->img_request); | |
b454e36d | 2888 | |
70d045f6 | 2889 | if (img_obj_request_simple(obj_request)) { |
980917fc ID |
2890 | rbd_obj_request_submit(obj_request); |
2891 | return 0; | |
b454e36d AE |
2892 | } |
2893 | ||
2894 | /* | |
3d7efd18 AE |
2895 | * It's a layered write. The target object might exist but |
2896 | * we may not know that yet. If we know it doesn't exist, | |
2897 | * start by reading the data for the full target object from | |
2898 | * the parent so we can use it for a copyup to the target. | |
b454e36d | 2899 | */ |
70d045f6 | 2900 | if (obj_request_known_test(obj_request)) |
3d7efd18 AE |
2901 | return rbd_img_obj_parent_read_full(obj_request); |
2902 | ||
2903 | /* We don't know whether the target exists. Go find out. */ | |
b454e36d AE |
2904 | |
2905 | return rbd_img_obj_exists_submit(obj_request); | |
2906 | } | |
2907 | ||
bf0d5f50 AE |
2908 | static int rbd_img_request_submit(struct rbd_img_request *img_request) |
2909 | { | |
bf0d5f50 | 2910 | struct rbd_obj_request *obj_request; |
46faeed4 | 2911 | struct rbd_obj_request *next_obj_request; |
663ae2cc | 2912 | int ret = 0; |
bf0d5f50 | 2913 | |
37206ee5 | 2914 | dout("%s: img %p\n", __func__, img_request); |
bf0d5f50 | 2915 | |
663ae2cc ID |
2916 | rbd_img_request_get(img_request); |
2917 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) { | |
b454e36d | 2918 | ret = rbd_img_obj_request_submit(obj_request); |
bf0d5f50 | 2919 | if (ret) |
663ae2cc | 2920 | goto out_put_ireq; |
bf0d5f50 AE |
2921 | } |
2922 | ||
663ae2cc ID |
2923 | out_put_ireq: |
2924 | rbd_img_request_put(img_request); | |
2925 | return ret; | |
bf0d5f50 | 2926 | } |
8b3e1a56 AE |
2927 | |
2928 | static void rbd_img_parent_read_callback(struct rbd_img_request *img_request) | |
2929 | { | |
2930 | struct rbd_obj_request *obj_request; | |
a9e8ba2c AE |
2931 | struct rbd_device *rbd_dev; |
2932 | u64 obj_end; | |
02c74fba AE |
2933 | u64 img_xferred; |
2934 | int img_result; | |
8b3e1a56 AE |
2935 | |
2936 | rbd_assert(img_request_child_test(img_request)); | |
2937 | ||
02c74fba AE |
2938 | /* First get what we need from the image request and release it */ |
2939 | ||
8b3e1a56 | 2940 | obj_request = img_request->obj_request; |
02c74fba AE |
2941 | img_xferred = img_request->xferred; |
2942 | img_result = img_request->result; | |
2943 | rbd_img_request_put(img_request); | |
2944 | ||
2945 | /* | |
2946 | * If the overlap has become 0 (most likely because the | |
2947 | * image has been flattened) we need to re-submit the | |
2948 | * original request. | |
2949 | */ | |
a9e8ba2c AE |
2950 | rbd_assert(obj_request); |
2951 | rbd_assert(obj_request->img_request); | |
02c74fba AE |
2952 | rbd_dev = obj_request->img_request->rbd_dev; |
2953 | if (!rbd_dev->parent_overlap) { | |
980917fc ID |
2954 | rbd_obj_request_submit(obj_request); |
2955 | return; | |
02c74fba | 2956 | } |
a9e8ba2c | 2957 | |
02c74fba | 2958 | obj_request->result = img_result; |
a9e8ba2c AE |
2959 | if (obj_request->result) |
2960 | goto out; | |
2961 | ||
2962 | /* | |
2963 | * We need to zero anything beyond the parent overlap | |
2964 | * boundary. Since rbd_img_obj_request_read_callback() | |
2965 | * will zero anything beyond the end of a short read, an | |
2966 | * easy way to do this is to pretend the data from the | |
2967 | * parent came up short--ending at the overlap boundary. | |
2968 | */ | |
2969 | rbd_assert(obj_request->img_offset < U64_MAX - obj_request->length); | |
2970 | obj_end = obj_request->img_offset + obj_request->length; | |
a9e8ba2c AE |
2971 | if (obj_end > rbd_dev->parent_overlap) { |
2972 | u64 xferred = 0; | |
2973 | ||
2974 | if (obj_request->img_offset < rbd_dev->parent_overlap) | |
2975 | xferred = rbd_dev->parent_overlap - | |
2976 | obj_request->img_offset; | |
8b3e1a56 | 2977 | |
02c74fba | 2978 | obj_request->xferred = min(img_xferred, xferred); |
a9e8ba2c | 2979 | } else { |
02c74fba | 2980 | obj_request->xferred = img_xferred; |
a9e8ba2c AE |
2981 | } |
2982 | out: | |
8b3e1a56 AE |
2983 | rbd_img_obj_request_read_callback(obj_request); |
2984 | rbd_obj_request_complete(obj_request); | |
2985 | } | |
2986 | ||
2987 | static void rbd_img_parent_read(struct rbd_obj_request *obj_request) | |
2988 | { | |
8b3e1a56 AE |
2989 | struct rbd_img_request *img_request; |
2990 | int result; | |
2991 | ||
2992 | rbd_assert(obj_request_img_data_test(obj_request)); | |
2993 | rbd_assert(obj_request->img_request != NULL); | |
2994 | rbd_assert(obj_request->result == (s32) -ENOENT); | |
5b2ab72d | 2995 | rbd_assert(obj_request_type_valid(obj_request->type)); |
8b3e1a56 | 2996 | |
8b3e1a56 | 2997 | /* rbd_read_finish(obj_request, obj_request->length); */ |
e93f3152 | 2998 | img_request = rbd_parent_request_create(obj_request, |
8b3e1a56 | 2999 | obj_request->img_offset, |
e93f3152 | 3000 | obj_request->length); |
8b3e1a56 AE |
3001 | result = -ENOMEM; |
3002 | if (!img_request) | |
3003 | goto out_err; | |
3004 | ||
5b2ab72d AE |
3005 | if (obj_request->type == OBJ_REQUEST_BIO) |
3006 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO, | |
3007 | obj_request->bio_list); | |
3008 | else | |
3009 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_PAGES, | |
3010 | obj_request->pages); | |
8b3e1a56 AE |
3011 | if (result) |
3012 | goto out_err; | |
3013 | ||
3014 | img_request->callback = rbd_img_parent_read_callback; | |
3015 | result = rbd_img_request_submit(img_request); | |
3016 | if (result) | |
3017 | goto out_err; | |
3018 | ||
3019 | return; | |
3020 | out_err: | |
3021 | if (img_request) | |
3022 | rbd_img_request_put(img_request); | |
3023 | obj_request->result = result; | |
3024 | obj_request->xferred = 0; | |
3025 | obj_request_done_set(obj_request); | |
3026 | } | |
bf0d5f50 | 3027 | |
ed95b21a | 3028 | static const struct rbd_client_id rbd_empty_cid; |
b8d70035 | 3029 | |
ed95b21a ID |
3030 | static bool rbd_cid_equal(const struct rbd_client_id *lhs, |
3031 | const struct rbd_client_id *rhs) | |
3032 | { | |
3033 | return lhs->gid == rhs->gid && lhs->handle == rhs->handle; | |
3034 | } | |
3035 | ||
3036 | static struct rbd_client_id rbd_get_cid(struct rbd_device *rbd_dev) | |
3037 | { | |
3038 | struct rbd_client_id cid; | |
3039 | ||
3040 | mutex_lock(&rbd_dev->watch_mutex); | |
3041 | cid.gid = ceph_client_gid(rbd_dev->rbd_client->client); | |
3042 | cid.handle = rbd_dev->watch_cookie; | |
3043 | mutex_unlock(&rbd_dev->watch_mutex); | |
3044 | return cid; | |
3045 | } | |
3046 | ||
3047 | /* | |
3048 | * lock_rwsem must be held for write | |
3049 | */ | |
3050 | static void rbd_set_owner_cid(struct rbd_device *rbd_dev, | |
3051 | const struct rbd_client_id *cid) | |
3052 | { | |
3053 | dout("%s rbd_dev %p %llu-%llu -> %llu-%llu\n", __func__, rbd_dev, | |
3054 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle, | |
3055 | cid->gid, cid->handle); | |
3056 | rbd_dev->owner_cid = *cid; /* struct */ | |
3057 | } | |
3058 | ||
3059 | static void format_lock_cookie(struct rbd_device *rbd_dev, char *buf) | |
3060 | { | |
3061 | mutex_lock(&rbd_dev->watch_mutex); | |
3062 | sprintf(buf, "%s %llu", RBD_LOCK_COOKIE_PREFIX, rbd_dev->watch_cookie); | |
3063 | mutex_unlock(&rbd_dev->watch_mutex); | |
3064 | } | |
3065 | ||
3066 | /* | |
3067 | * lock_rwsem must be held for write | |
3068 | */ | |
3069 | static int rbd_lock(struct rbd_device *rbd_dev) | |
b8d70035 | 3070 | { |
922dab61 | 3071 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
ed95b21a ID |
3072 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
3073 | char cookie[32]; | |
e627db08 | 3074 | int ret; |
b8d70035 | 3075 | |
ed95b21a | 3076 | WARN_ON(__rbd_is_lock_owner(rbd_dev)); |
52bb1f9b | 3077 | |
ed95b21a ID |
3078 | format_lock_cookie(rbd_dev, cookie); |
3079 | ret = ceph_cls_lock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, | |
3080 | RBD_LOCK_NAME, CEPH_CLS_LOCK_EXCLUSIVE, cookie, | |
3081 | RBD_LOCK_TAG, "", 0); | |
e627db08 | 3082 | if (ret) |
ed95b21a | 3083 | return ret; |
b8d70035 | 3084 | |
ed95b21a ID |
3085 | rbd_dev->lock_state = RBD_LOCK_STATE_LOCKED; |
3086 | rbd_set_owner_cid(rbd_dev, &cid); | |
3087 | queue_work(rbd_dev->task_wq, &rbd_dev->acquired_lock_work); | |
3088 | return 0; | |
b8d70035 AE |
3089 | } |
3090 | ||
ed95b21a ID |
3091 | /* |
3092 | * lock_rwsem must be held for write | |
3093 | */ | |
3094 | static int rbd_unlock(struct rbd_device *rbd_dev) | |
bb040aa0 | 3095 | { |
922dab61 | 3096 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
ed95b21a | 3097 | char cookie[32]; |
bb040aa0 ID |
3098 | int ret; |
3099 | ||
ed95b21a | 3100 | WARN_ON(!__rbd_is_lock_owner(rbd_dev)); |
bb040aa0 | 3101 | |
ed95b21a | 3102 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; |
bb040aa0 | 3103 | |
ed95b21a ID |
3104 | format_lock_cookie(rbd_dev, cookie); |
3105 | ret = ceph_cls_unlock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, | |
3106 | RBD_LOCK_NAME, cookie); | |
3107 | if (ret && ret != -ENOENT) { | |
3108 | rbd_warn(rbd_dev, "cls_unlock failed: %d", ret); | |
3109 | return ret; | |
bb040aa0 ID |
3110 | } |
3111 | ||
ed95b21a ID |
3112 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
3113 | queue_work(rbd_dev->task_wq, &rbd_dev->released_lock_work); | |
3114 | return 0; | |
bb040aa0 ID |
3115 | } |
3116 | ||
ed95b21a ID |
3117 | static int __rbd_notify_op_lock(struct rbd_device *rbd_dev, |
3118 | enum rbd_notify_op notify_op, | |
3119 | struct page ***preply_pages, | |
3120 | size_t *preply_len) | |
9969ebc5 AE |
3121 | { |
3122 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; | |
ed95b21a ID |
3123 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
3124 | int buf_size = 4 + 8 + 8 + CEPH_ENCODING_START_BLK_LEN; | |
3125 | char buf[buf_size]; | |
3126 | void *p = buf; | |
9969ebc5 | 3127 | |
ed95b21a | 3128 | dout("%s rbd_dev %p notify_op %d\n", __func__, rbd_dev, notify_op); |
9969ebc5 | 3129 | |
ed95b21a ID |
3130 | /* encode *LockPayload NotifyMessage (op + ClientId) */ |
3131 | ceph_start_encoding(&p, 2, 1, buf_size - CEPH_ENCODING_START_BLK_LEN); | |
3132 | ceph_encode_32(&p, notify_op); | |
3133 | ceph_encode_64(&p, cid.gid); | |
3134 | ceph_encode_64(&p, cid.handle); | |
8eb87565 | 3135 | |
ed95b21a ID |
3136 | return ceph_osdc_notify(osdc, &rbd_dev->header_oid, |
3137 | &rbd_dev->header_oloc, buf, buf_size, | |
3138 | RBD_NOTIFY_TIMEOUT, preply_pages, preply_len); | |
b30a01f2 ID |
3139 | } |
3140 | ||
ed95b21a ID |
3141 | static void rbd_notify_op_lock(struct rbd_device *rbd_dev, |
3142 | enum rbd_notify_op notify_op) | |
b30a01f2 | 3143 | { |
ed95b21a ID |
3144 | struct page **reply_pages; |
3145 | size_t reply_len; | |
b30a01f2 | 3146 | |
ed95b21a ID |
3147 | __rbd_notify_op_lock(rbd_dev, notify_op, &reply_pages, &reply_len); |
3148 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); | |
3149 | } | |
b30a01f2 | 3150 | |
ed95b21a ID |
3151 | static void rbd_notify_acquired_lock(struct work_struct *work) |
3152 | { | |
3153 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, | |
3154 | acquired_lock_work); | |
76756a51 | 3155 | |
ed95b21a | 3156 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_ACQUIRED_LOCK); |
c525f036 ID |
3157 | } |
3158 | ||
ed95b21a | 3159 | static void rbd_notify_released_lock(struct work_struct *work) |
c525f036 | 3160 | { |
ed95b21a ID |
3161 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
3162 | released_lock_work); | |
811c6688 | 3163 | |
ed95b21a | 3164 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_RELEASED_LOCK); |
fca27065 ID |
3165 | } |
3166 | ||
ed95b21a | 3167 | static int rbd_request_lock(struct rbd_device *rbd_dev) |
36be9a76 | 3168 | { |
ed95b21a ID |
3169 | struct page **reply_pages; |
3170 | size_t reply_len; | |
3171 | bool lock_owner_responded = false; | |
36be9a76 AE |
3172 | int ret; |
3173 | ||
ed95b21a | 3174 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
36be9a76 | 3175 | |
ed95b21a ID |
3176 | ret = __rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_REQUEST_LOCK, |
3177 | &reply_pages, &reply_len); | |
3178 | if (ret && ret != -ETIMEDOUT) { | |
3179 | rbd_warn(rbd_dev, "failed to request lock: %d", ret); | |
36be9a76 | 3180 | goto out; |
ed95b21a | 3181 | } |
36be9a76 | 3182 | |
ed95b21a ID |
3183 | if (reply_len > 0 && reply_len <= PAGE_SIZE) { |
3184 | void *p = page_address(reply_pages[0]); | |
3185 | void *const end = p + reply_len; | |
3186 | u32 n; | |
36be9a76 | 3187 | |
ed95b21a ID |
3188 | ceph_decode_32_safe(&p, end, n, e_inval); /* num_acks */ |
3189 | while (n--) { | |
3190 | u8 struct_v; | |
3191 | u32 len; | |
36be9a76 | 3192 | |
ed95b21a ID |
3193 | ceph_decode_need(&p, end, 8 + 8, e_inval); |
3194 | p += 8 + 8; /* skip gid and cookie */ | |
04017e29 | 3195 | |
ed95b21a ID |
3196 | ceph_decode_32_safe(&p, end, len, e_inval); |
3197 | if (!len) | |
3198 | continue; | |
3199 | ||
3200 | if (lock_owner_responded) { | |
3201 | rbd_warn(rbd_dev, | |
3202 | "duplicate lock owners detected"); | |
3203 | ret = -EIO; | |
3204 | goto out; | |
3205 | } | |
3206 | ||
3207 | lock_owner_responded = true; | |
3208 | ret = ceph_start_decoding(&p, end, 1, "ResponseMessage", | |
3209 | &struct_v, &len); | |
3210 | if (ret) { | |
3211 | rbd_warn(rbd_dev, | |
3212 | "failed to decode ResponseMessage: %d", | |
3213 | ret); | |
3214 | goto e_inval; | |
3215 | } | |
3216 | ||
3217 | ret = ceph_decode_32(&p); | |
3218 | } | |
3219 | } | |
3220 | ||
3221 | if (!lock_owner_responded) { | |
3222 | rbd_warn(rbd_dev, "no lock owners detected"); | |
3223 | ret = -ETIMEDOUT; | |
3224 | } | |
3225 | ||
3226 | out: | |
3227 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); | |
3228 | return ret; | |
3229 | ||
3230 | e_inval: | |
3231 | ret = -EINVAL; | |
3232 | goto out; | |
3233 | } | |
3234 | ||
3235 | static void wake_requests(struct rbd_device *rbd_dev, bool wake_all) | |
3236 | { | |
3237 | dout("%s rbd_dev %p wake_all %d\n", __func__, rbd_dev, wake_all); | |
3238 | ||
3239 | cancel_delayed_work(&rbd_dev->lock_dwork); | |
3240 | if (wake_all) | |
3241 | wake_up_all(&rbd_dev->lock_waitq); | |
3242 | else | |
3243 | wake_up(&rbd_dev->lock_waitq); | |
3244 | } | |
3245 | ||
3246 | static int get_lock_owner_info(struct rbd_device *rbd_dev, | |
3247 | struct ceph_locker **lockers, u32 *num_lockers) | |
3248 | { | |
3249 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; | |
3250 | u8 lock_type; | |
3251 | char *lock_tag; | |
3252 | int ret; | |
3253 | ||
3254 | dout("%s rbd_dev %p\n", __func__, rbd_dev); | |
3255 | ||
3256 | ret = ceph_cls_lock_info(osdc, &rbd_dev->header_oid, | |
3257 | &rbd_dev->header_oloc, RBD_LOCK_NAME, | |
3258 | &lock_type, &lock_tag, lockers, num_lockers); | |
3259 | if (ret) | |
3260 | return ret; | |
3261 | ||
3262 | if (*num_lockers == 0) { | |
3263 | dout("%s rbd_dev %p no lockers detected\n", __func__, rbd_dev); | |
3264 | goto out; | |
3265 | } | |
3266 | ||
3267 | if (strcmp(lock_tag, RBD_LOCK_TAG)) { | |
3268 | rbd_warn(rbd_dev, "locked by external mechanism, tag %s", | |
3269 | lock_tag); | |
3270 | ret = -EBUSY; | |
3271 | goto out; | |
3272 | } | |
3273 | ||
3274 | if (lock_type == CEPH_CLS_LOCK_SHARED) { | |
3275 | rbd_warn(rbd_dev, "shared lock type detected"); | |
3276 | ret = -EBUSY; | |
3277 | goto out; | |
3278 | } | |
3279 | ||
3280 | if (strncmp((*lockers)[0].id.cookie, RBD_LOCK_COOKIE_PREFIX, | |
3281 | strlen(RBD_LOCK_COOKIE_PREFIX))) { | |
3282 | rbd_warn(rbd_dev, "locked by external mechanism, cookie %s", | |
3283 | (*lockers)[0].id.cookie); | |
3284 | ret = -EBUSY; | |
3285 | goto out; | |
3286 | } | |
3287 | ||
3288 | out: | |
3289 | kfree(lock_tag); | |
3290 | return ret; | |
3291 | } | |
3292 | ||
3293 | static int find_watcher(struct rbd_device *rbd_dev, | |
3294 | const struct ceph_locker *locker) | |
3295 | { | |
3296 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; | |
3297 | struct ceph_watch_item *watchers; | |
3298 | u32 num_watchers; | |
3299 | u64 cookie; | |
3300 | int i; | |
3301 | int ret; | |
3302 | ||
3303 | ret = ceph_osdc_list_watchers(osdc, &rbd_dev->header_oid, | |
3304 | &rbd_dev->header_oloc, &watchers, | |
3305 | &num_watchers); | |
3306 | if (ret) | |
3307 | return ret; | |
3308 | ||
3309 | sscanf(locker->id.cookie, RBD_LOCK_COOKIE_PREFIX " %llu", &cookie); | |
3310 | for (i = 0; i < num_watchers; i++) { | |
3311 | if (!memcmp(&watchers[i].addr, &locker->info.addr, | |
3312 | sizeof(locker->info.addr)) && | |
3313 | watchers[i].cookie == cookie) { | |
3314 | struct rbd_client_id cid = { | |
3315 | .gid = le64_to_cpu(watchers[i].name.num), | |
3316 | .handle = cookie, | |
3317 | }; | |
3318 | ||
3319 | dout("%s rbd_dev %p found cid %llu-%llu\n", __func__, | |
3320 | rbd_dev, cid.gid, cid.handle); | |
3321 | rbd_set_owner_cid(rbd_dev, &cid); | |
3322 | ret = 1; | |
3323 | goto out; | |
3324 | } | |
3325 | } | |
3326 | ||
3327 | dout("%s rbd_dev %p no watchers\n", __func__, rbd_dev); | |
3328 | ret = 0; | |
3329 | out: | |
3330 | kfree(watchers); | |
3331 | return ret; | |
3332 | } | |
3333 | ||
3334 | /* | |
3335 | * lock_rwsem must be held for write | |
3336 | */ | |
3337 | static int rbd_try_lock(struct rbd_device *rbd_dev) | |
3338 | { | |
3339 | struct ceph_client *client = rbd_dev->rbd_client->client; | |
3340 | struct ceph_locker *lockers; | |
3341 | u32 num_lockers; | |
3342 | int ret; | |
3343 | ||
3344 | for (;;) { | |
3345 | ret = rbd_lock(rbd_dev); | |
3346 | if (ret != -EBUSY) | |
3347 | return ret; | |
3348 | ||
3349 | /* determine if the current lock holder is still alive */ | |
3350 | ret = get_lock_owner_info(rbd_dev, &lockers, &num_lockers); | |
3351 | if (ret) | |
3352 | return ret; | |
3353 | ||
3354 | if (num_lockers == 0) | |
3355 | goto again; | |
3356 | ||
3357 | ret = find_watcher(rbd_dev, lockers); | |
3358 | if (ret) { | |
3359 | if (ret > 0) | |
3360 | ret = 0; /* have to request lock */ | |
3361 | goto out; | |
3362 | } | |
3363 | ||
3364 | rbd_warn(rbd_dev, "%s%llu seems dead, breaking lock", | |
3365 | ENTITY_NAME(lockers[0].id.name)); | |
3366 | ||
3367 | ret = ceph_monc_blacklist_add(&client->monc, | |
3368 | &lockers[0].info.addr); | |
3369 | if (ret) { | |
3370 | rbd_warn(rbd_dev, "blacklist of %s%llu failed: %d", | |
3371 | ENTITY_NAME(lockers[0].id.name), ret); | |
3372 | goto out; | |
3373 | } | |
3374 | ||
3375 | ret = ceph_cls_break_lock(&client->osdc, &rbd_dev->header_oid, | |
3376 | &rbd_dev->header_oloc, RBD_LOCK_NAME, | |
3377 | lockers[0].id.cookie, | |
3378 | &lockers[0].id.name); | |
3379 | if (ret && ret != -ENOENT) | |
3380 | goto out; | |
3381 | ||
3382 | again: | |
3383 | ceph_free_lockers(lockers, num_lockers); | |
3384 | } | |
3385 | ||
3386 | out: | |
3387 | ceph_free_lockers(lockers, num_lockers); | |
3388 | return ret; | |
3389 | } | |
3390 | ||
3391 | /* | |
3392 | * ret is set only if lock_state is RBD_LOCK_STATE_UNLOCKED | |
3393 | */ | |
3394 | static enum rbd_lock_state rbd_try_acquire_lock(struct rbd_device *rbd_dev, | |
3395 | int *pret) | |
3396 | { | |
3397 | enum rbd_lock_state lock_state; | |
3398 | ||
3399 | down_read(&rbd_dev->lock_rwsem); | |
3400 | dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, | |
3401 | rbd_dev->lock_state); | |
3402 | if (__rbd_is_lock_owner(rbd_dev)) { | |
3403 | lock_state = rbd_dev->lock_state; | |
3404 | up_read(&rbd_dev->lock_rwsem); | |
3405 | return lock_state; | |
3406 | } | |
3407 | ||
3408 | up_read(&rbd_dev->lock_rwsem); | |
3409 | down_write(&rbd_dev->lock_rwsem); | |
3410 | dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, | |
3411 | rbd_dev->lock_state); | |
3412 | if (!__rbd_is_lock_owner(rbd_dev)) { | |
3413 | *pret = rbd_try_lock(rbd_dev); | |
3414 | if (*pret) | |
3415 | rbd_warn(rbd_dev, "failed to acquire lock: %d", *pret); | |
3416 | } | |
3417 | ||
3418 | lock_state = rbd_dev->lock_state; | |
3419 | up_write(&rbd_dev->lock_rwsem); | |
3420 | return lock_state; | |
3421 | } | |
3422 | ||
3423 | static void rbd_acquire_lock(struct work_struct *work) | |
3424 | { | |
3425 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), | |
3426 | struct rbd_device, lock_dwork); | |
3427 | enum rbd_lock_state lock_state; | |
3428 | int ret; | |
3429 | ||
3430 | dout("%s rbd_dev %p\n", __func__, rbd_dev); | |
3431 | again: | |
3432 | lock_state = rbd_try_acquire_lock(rbd_dev, &ret); | |
3433 | if (lock_state != RBD_LOCK_STATE_UNLOCKED || ret == -EBLACKLISTED) { | |
3434 | if (lock_state == RBD_LOCK_STATE_LOCKED) | |
3435 | wake_requests(rbd_dev, true); | |
3436 | dout("%s rbd_dev %p lock_state %d ret %d - done\n", __func__, | |
3437 | rbd_dev, lock_state, ret); | |
3438 | return; | |
3439 | } | |
3440 | ||
3441 | ret = rbd_request_lock(rbd_dev); | |
3442 | if (ret == -ETIMEDOUT) { | |
3443 | goto again; /* treat this as a dead client */ | |
3444 | } else if (ret < 0) { | |
3445 | rbd_warn(rbd_dev, "error requesting lock: %d", ret); | |
3446 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, | |
3447 | RBD_RETRY_DELAY); | |
3448 | } else { | |
3449 | /* | |
3450 | * lock owner acked, but resend if we don't see them | |
3451 | * release the lock | |
3452 | */ | |
3453 | dout("%s rbd_dev %p requeueing lock_dwork\n", __func__, | |
3454 | rbd_dev); | |
3455 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, | |
3456 | msecs_to_jiffies(2 * RBD_NOTIFY_TIMEOUT * MSEC_PER_SEC)); | |
3457 | } | |
3458 | } | |
3459 | ||
3460 | /* | |
3461 | * lock_rwsem must be held for write | |
3462 | */ | |
3463 | static bool rbd_release_lock(struct rbd_device *rbd_dev) | |
3464 | { | |
3465 | dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, | |
3466 | rbd_dev->lock_state); | |
3467 | if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED) | |
3468 | return false; | |
3469 | ||
3470 | rbd_dev->lock_state = RBD_LOCK_STATE_RELEASING; | |
3471 | downgrade_write(&rbd_dev->lock_rwsem); | |
52bb1f9b | 3472 | /* |
ed95b21a | 3473 | * Ensure that all in-flight IO is flushed. |
52bb1f9b | 3474 | * |
ed95b21a ID |
3475 | * FIXME: ceph_osdc_sync() flushes the entire OSD client, which |
3476 | * may be shared with other devices. | |
52bb1f9b | 3477 | */ |
ed95b21a ID |
3478 | ceph_osdc_sync(&rbd_dev->rbd_client->client->osdc); |
3479 | up_read(&rbd_dev->lock_rwsem); | |
3480 | ||
3481 | down_write(&rbd_dev->lock_rwsem); | |
3482 | dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, | |
3483 | rbd_dev->lock_state); | |
3484 | if (rbd_dev->lock_state != RBD_LOCK_STATE_RELEASING) | |
3485 | return false; | |
3486 | ||
3487 | if (!rbd_unlock(rbd_dev)) | |
3488 | /* | |
3489 | * Give others a chance to grab the lock - we would re-acquire | |
3490 | * almost immediately if we got new IO during ceph_osdc_sync() | |
3491 | * otherwise. We need to ack our own notifications, so this | |
3492 | * lock_dwork will be requeued from rbd_wait_state_locked() | |
3493 | * after wake_requests() in rbd_handle_released_lock(). | |
3494 | */ | |
3495 | cancel_delayed_work(&rbd_dev->lock_dwork); | |
3496 | ||
3497 | return true; | |
3498 | } | |
3499 | ||
3500 | static void rbd_release_lock_work(struct work_struct *work) | |
3501 | { | |
3502 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, | |
3503 | unlock_work); | |
3504 | ||
3505 | down_write(&rbd_dev->lock_rwsem); | |
3506 | rbd_release_lock(rbd_dev); | |
3507 | up_write(&rbd_dev->lock_rwsem); | |
3508 | } | |
3509 | ||
3510 | static void rbd_handle_acquired_lock(struct rbd_device *rbd_dev, u8 struct_v, | |
3511 | void **p) | |
3512 | { | |
3513 | struct rbd_client_id cid = { 0 }; | |
3514 | ||
3515 | if (struct_v >= 2) { | |
3516 | cid.gid = ceph_decode_64(p); | |
3517 | cid.handle = ceph_decode_64(p); | |
3518 | } | |
3519 | ||
3520 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, | |
3521 | cid.handle); | |
3522 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { | |
3523 | down_write(&rbd_dev->lock_rwsem); | |
3524 | if (rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { | |
3525 | /* | |
3526 | * we already know that the remote client is | |
3527 | * the owner | |
3528 | */ | |
3529 | up_write(&rbd_dev->lock_rwsem); | |
3530 | return; | |
3531 | } | |
3532 | ||
3533 | rbd_set_owner_cid(rbd_dev, &cid); | |
3534 | downgrade_write(&rbd_dev->lock_rwsem); | |
3535 | } else { | |
3536 | down_read(&rbd_dev->lock_rwsem); | |
3537 | } | |
3538 | ||
3539 | if (!__rbd_is_lock_owner(rbd_dev)) | |
3540 | wake_requests(rbd_dev, false); | |
3541 | up_read(&rbd_dev->lock_rwsem); | |
3542 | } | |
3543 | ||
3544 | static void rbd_handle_released_lock(struct rbd_device *rbd_dev, u8 struct_v, | |
3545 | void **p) | |
3546 | { | |
3547 | struct rbd_client_id cid = { 0 }; | |
3548 | ||
3549 | if (struct_v >= 2) { | |
3550 | cid.gid = ceph_decode_64(p); | |
3551 | cid.handle = ceph_decode_64(p); | |
3552 | } | |
3553 | ||
3554 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, | |
3555 | cid.handle); | |
3556 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { | |
3557 | down_write(&rbd_dev->lock_rwsem); | |
3558 | if (!rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { | |
3559 | dout("%s rbd_dev %p unexpected owner, cid %llu-%llu != owner_cid %llu-%llu\n", | |
3560 | __func__, rbd_dev, cid.gid, cid.handle, | |
3561 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle); | |
3562 | up_write(&rbd_dev->lock_rwsem); | |
3563 | return; | |
3564 | } | |
3565 | ||
3566 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); | |
3567 | downgrade_write(&rbd_dev->lock_rwsem); | |
3568 | } else { | |
3569 | down_read(&rbd_dev->lock_rwsem); | |
3570 | } | |
3571 | ||
3572 | if (!__rbd_is_lock_owner(rbd_dev)) | |
3573 | wake_requests(rbd_dev, false); | |
3574 | up_read(&rbd_dev->lock_rwsem); | |
3575 | } | |
3576 | ||
3577 | static bool rbd_handle_request_lock(struct rbd_device *rbd_dev, u8 struct_v, | |
3578 | void **p) | |
3579 | { | |
3580 | struct rbd_client_id my_cid = rbd_get_cid(rbd_dev); | |
3581 | struct rbd_client_id cid = { 0 }; | |
3582 | bool need_to_send; | |
3583 | ||
3584 | if (struct_v >= 2) { | |
3585 | cid.gid = ceph_decode_64(p); | |
3586 | cid.handle = ceph_decode_64(p); | |
3587 | } | |
3588 | ||
3589 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, | |
3590 | cid.handle); | |
3591 | if (rbd_cid_equal(&cid, &my_cid)) | |
3592 | return false; | |
3593 | ||
3594 | down_read(&rbd_dev->lock_rwsem); | |
3595 | need_to_send = __rbd_is_lock_owner(rbd_dev); | |
3596 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) { | |
3597 | if (!rbd_cid_equal(&rbd_dev->owner_cid, &rbd_empty_cid)) { | |
3598 | dout("%s rbd_dev %p queueing unlock_work\n", __func__, | |
3599 | rbd_dev); | |
3600 | queue_work(rbd_dev->task_wq, &rbd_dev->unlock_work); | |
3601 | } | |
3602 | } | |
3603 | up_read(&rbd_dev->lock_rwsem); | |
3604 | return need_to_send; | |
3605 | } | |
3606 | ||
3607 | static void __rbd_acknowledge_notify(struct rbd_device *rbd_dev, | |
3608 | u64 notify_id, u64 cookie, s32 *result) | |
3609 | { | |
3610 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; | |
3611 | int buf_size = 4 + CEPH_ENCODING_START_BLK_LEN; | |
3612 | char buf[buf_size]; | |
3613 | int ret; | |
3614 | ||
3615 | if (result) { | |
3616 | void *p = buf; | |
3617 | ||
3618 | /* encode ResponseMessage */ | |
3619 | ceph_start_encoding(&p, 1, 1, | |
3620 | buf_size - CEPH_ENCODING_START_BLK_LEN); | |
3621 | ceph_encode_32(&p, *result); | |
3622 | } else { | |
3623 | buf_size = 0; | |
3624 | } | |
b8d70035 | 3625 | |
922dab61 ID |
3626 | ret = ceph_osdc_notify_ack(osdc, &rbd_dev->header_oid, |
3627 | &rbd_dev->header_oloc, notify_id, cookie, | |
ed95b21a | 3628 | buf, buf_size); |
52bb1f9b | 3629 | if (ret) |
ed95b21a ID |
3630 | rbd_warn(rbd_dev, "acknowledge_notify failed: %d", ret); |
3631 | } | |
3632 | ||
3633 | static void rbd_acknowledge_notify(struct rbd_device *rbd_dev, u64 notify_id, | |
3634 | u64 cookie) | |
3635 | { | |
3636 | dout("%s rbd_dev %p\n", __func__, rbd_dev); | |
3637 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, NULL); | |
3638 | } | |
3639 | ||
3640 | static void rbd_acknowledge_notify_result(struct rbd_device *rbd_dev, | |
3641 | u64 notify_id, u64 cookie, s32 result) | |
3642 | { | |
3643 | dout("%s rbd_dev %p result %d\n", __func__, rbd_dev, result); | |
3644 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, &result); | |
3645 | } | |
3646 | ||
3647 | static void rbd_watch_cb(void *arg, u64 notify_id, u64 cookie, | |
3648 | u64 notifier_id, void *data, size_t data_len) | |
3649 | { | |
3650 | struct rbd_device *rbd_dev = arg; | |
3651 | void *p = data; | |
3652 | void *const end = p + data_len; | |
d4c2269b | 3653 | u8 struct_v = 0; |
ed95b21a ID |
3654 | u32 len; |
3655 | u32 notify_op; | |
3656 | int ret; | |
3657 | ||
3658 | dout("%s rbd_dev %p cookie %llu notify_id %llu data_len %zu\n", | |
3659 | __func__, rbd_dev, cookie, notify_id, data_len); | |
3660 | if (data_len) { | |
3661 | ret = ceph_start_decoding(&p, end, 1, "NotifyMessage", | |
3662 | &struct_v, &len); | |
3663 | if (ret) { | |
3664 | rbd_warn(rbd_dev, "failed to decode NotifyMessage: %d", | |
3665 | ret); | |
3666 | return; | |
3667 | } | |
3668 | ||
3669 | notify_op = ceph_decode_32(&p); | |
3670 | } else { | |
3671 | /* legacy notification for header updates */ | |
3672 | notify_op = RBD_NOTIFY_OP_HEADER_UPDATE; | |
3673 | len = 0; | |
3674 | } | |
3675 | ||
3676 | dout("%s rbd_dev %p notify_op %u\n", __func__, rbd_dev, notify_op); | |
3677 | switch (notify_op) { | |
3678 | case RBD_NOTIFY_OP_ACQUIRED_LOCK: | |
3679 | rbd_handle_acquired_lock(rbd_dev, struct_v, &p); | |
3680 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); | |
3681 | break; | |
3682 | case RBD_NOTIFY_OP_RELEASED_LOCK: | |
3683 | rbd_handle_released_lock(rbd_dev, struct_v, &p); | |
3684 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); | |
3685 | break; | |
3686 | case RBD_NOTIFY_OP_REQUEST_LOCK: | |
3687 | if (rbd_handle_request_lock(rbd_dev, struct_v, &p)) | |
3688 | /* | |
3689 | * send ResponseMessage(0) back so the client | |
3690 | * can detect a missing owner | |
3691 | */ | |
3692 | rbd_acknowledge_notify_result(rbd_dev, notify_id, | |
3693 | cookie, 0); | |
3694 | else | |
3695 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); | |
3696 | break; | |
3697 | case RBD_NOTIFY_OP_HEADER_UPDATE: | |
3698 | ret = rbd_dev_refresh(rbd_dev); | |
3699 | if (ret) | |
3700 | rbd_warn(rbd_dev, "refresh failed: %d", ret); | |
3701 | ||
3702 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); | |
3703 | break; | |
3704 | default: | |
3705 | if (rbd_is_lock_owner(rbd_dev)) | |
3706 | rbd_acknowledge_notify_result(rbd_dev, notify_id, | |
3707 | cookie, -EOPNOTSUPP); | |
3708 | else | |
3709 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); | |
3710 | break; | |
3711 | } | |
b8d70035 AE |
3712 | } |
3713 | ||
99d16943 ID |
3714 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev); |
3715 | ||
922dab61 | 3716 | static void rbd_watch_errcb(void *arg, u64 cookie, int err) |
bb040aa0 | 3717 | { |
922dab61 | 3718 | struct rbd_device *rbd_dev = arg; |
bb040aa0 | 3719 | |
922dab61 | 3720 | rbd_warn(rbd_dev, "encountered watch error: %d", err); |
bb040aa0 | 3721 | |
ed95b21a ID |
3722 | down_write(&rbd_dev->lock_rwsem); |
3723 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); | |
3724 | up_write(&rbd_dev->lock_rwsem); | |
3725 | ||
99d16943 ID |
3726 | mutex_lock(&rbd_dev->watch_mutex); |
3727 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) { | |
3728 | __rbd_unregister_watch(rbd_dev); | |
3729 | rbd_dev->watch_state = RBD_WATCH_STATE_ERROR; | |
bb040aa0 | 3730 | |
99d16943 | 3731 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->watch_dwork, 0); |
bb040aa0 | 3732 | } |
99d16943 | 3733 | mutex_unlock(&rbd_dev->watch_mutex); |
bb040aa0 ID |
3734 | } |
3735 | ||
9969ebc5 | 3736 | /* |
99d16943 | 3737 | * watch_mutex must be locked |
9969ebc5 | 3738 | */ |
99d16943 | 3739 | static int __rbd_register_watch(struct rbd_device *rbd_dev) |
9969ebc5 AE |
3740 | { |
3741 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; | |
922dab61 | 3742 | struct ceph_osd_linger_request *handle; |
9969ebc5 | 3743 | |
922dab61 | 3744 | rbd_assert(!rbd_dev->watch_handle); |
99d16943 | 3745 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
9969ebc5 | 3746 | |
922dab61 ID |
3747 | handle = ceph_osdc_watch(osdc, &rbd_dev->header_oid, |
3748 | &rbd_dev->header_oloc, rbd_watch_cb, | |
3749 | rbd_watch_errcb, rbd_dev); | |
3750 | if (IS_ERR(handle)) | |
3751 | return PTR_ERR(handle); | |
8eb87565 | 3752 | |
922dab61 | 3753 | rbd_dev->watch_handle = handle; |
b30a01f2 | 3754 | return 0; |
b30a01f2 ID |
3755 | } |
3756 | ||
99d16943 ID |
3757 | /* |
3758 | * watch_mutex must be locked | |
3759 | */ | |
3760 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev) | |
b30a01f2 | 3761 | { |
922dab61 ID |
3762 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
3763 | int ret; | |
b30a01f2 | 3764 | |
99d16943 ID |
3765 | rbd_assert(rbd_dev->watch_handle); |
3766 | dout("%s rbd_dev %p\n", __func__, rbd_dev); | |
b30a01f2 | 3767 | |
922dab61 ID |
3768 | ret = ceph_osdc_unwatch(osdc, rbd_dev->watch_handle); |
3769 | if (ret) | |
3770 | rbd_warn(rbd_dev, "failed to unwatch: %d", ret); | |
76756a51 | 3771 | |
922dab61 | 3772 | rbd_dev->watch_handle = NULL; |
c525f036 ID |
3773 | } |
3774 | ||
99d16943 ID |
3775 | static int rbd_register_watch(struct rbd_device *rbd_dev) |
3776 | { | |
3777 | int ret; | |
3778 | ||
3779 | mutex_lock(&rbd_dev->watch_mutex); | |
3780 | rbd_assert(rbd_dev->watch_state == RBD_WATCH_STATE_UNREGISTERED); | |
3781 | ret = __rbd_register_watch(rbd_dev); | |
3782 | if (ret) | |
3783 | goto out; | |
3784 | ||
3785 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; | |
3786 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; | |
3787 | ||
3788 | out: | |
3789 | mutex_unlock(&rbd_dev->watch_mutex); | |
3790 | return ret; | |
3791 | } | |
3792 | ||
3793 | static void cancel_tasks_sync(struct rbd_device *rbd_dev) | |
c525f036 | 3794 | { |
99d16943 ID |
3795 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
3796 | ||
3797 | cancel_delayed_work_sync(&rbd_dev->watch_dwork); | |
ed95b21a ID |
3798 | cancel_work_sync(&rbd_dev->acquired_lock_work); |
3799 | cancel_work_sync(&rbd_dev->released_lock_work); | |
3800 | cancel_delayed_work_sync(&rbd_dev->lock_dwork); | |
3801 | cancel_work_sync(&rbd_dev->unlock_work); | |
99d16943 ID |
3802 | } |
3803 | ||
3804 | static void rbd_unregister_watch(struct rbd_device *rbd_dev) | |
3805 | { | |
ed95b21a | 3806 | WARN_ON(waitqueue_active(&rbd_dev->lock_waitq)); |
99d16943 ID |
3807 | cancel_tasks_sync(rbd_dev); |
3808 | ||
3809 | mutex_lock(&rbd_dev->watch_mutex); | |
3810 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) | |
3811 | __rbd_unregister_watch(rbd_dev); | |
3812 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; | |
3813 | mutex_unlock(&rbd_dev->watch_mutex); | |
811c6688 | 3814 | |
811c6688 | 3815 | ceph_osdc_flush_notifies(&rbd_dev->rbd_client->client->osdc); |
fca27065 ID |
3816 | } |
3817 | ||
99d16943 ID |
3818 | static void rbd_reregister_watch(struct work_struct *work) |
3819 | { | |
3820 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), | |
3821 | struct rbd_device, watch_dwork); | |
ed95b21a | 3822 | bool was_lock_owner = false; |
87c0fded | 3823 | bool need_to_wake = false; |
99d16943 ID |
3824 | int ret; |
3825 | ||
3826 | dout("%s rbd_dev %p\n", __func__, rbd_dev); | |
3827 | ||
ed95b21a ID |
3828 | down_write(&rbd_dev->lock_rwsem); |
3829 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) | |
3830 | was_lock_owner = rbd_release_lock(rbd_dev); | |
3831 | ||
99d16943 | 3832 | mutex_lock(&rbd_dev->watch_mutex); |
87c0fded ID |
3833 | if (rbd_dev->watch_state != RBD_WATCH_STATE_ERROR) { |
3834 | mutex_unlock(&rbd_dev->watch_mutex); | |
3835 | goto out; | |
3836 | } | |
99d16943 ID |
3837 | |
3838 | ret = __rbd_register_watch(rbd_dev); | |
3839 | if (ret) { | |
3840 | rbd_warn(rbd_dev, "failed to reregister watch: %d", ret); | |
4d73644b | 3841 | if (ret == -EBLACKLISTED || ret == -ENOENT) { |
87c0fded ID |
3842 | set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags); |
3843 | need_to_wake = true; | |
3844 | } else { | |
99d16943 ID |
3845 | queue_delayed_work(rbd_dev->task_wq, |
3846 | &rbd_dev->watch_dwork, | |
3847 | RBD_RETRY_DELAY); | |
87c0fded ID |
3848 | } |
3849 | mutex_unlock(&rbd_dev->watch_mutex); | |
3850 | goto out; | |
99d16943 ID |
3851 | } |
3852 | ||
87c0fded | 3853 | need_to_wake = true; |
99d16943 ID |
3854 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; |
3855 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; | |
3856 | mutex_unlock(&rbd_dev->watch_mutex); | |
3857 | ||
3858 | ret = rbd_dev_refresh(rbd_dev); | |
3859 | if (ret) | |
3860 | rbd_warn(rbd_dev, "reregisteration refresh failed: %d", ret); | |
3861 | ||
ed95b21a ID |
3862 | if (was_lock_owner) { |
3863 | ret = rbd_try_lock(rbd_dev); | |
3864 | if (ret) | |
3865 | rbd_warn(rbd_dev, "reregisteration lock failed: %d", | |
3866 | ret); | |
3867 | } | |
3868 | ||
87c0fded | 3869 | out: |
ed95b21a | 3870 | up_write(&rbd_dev->lock_rwsem); |
87c0fded ID |
3871 | if (need_to_wake) |
3872 | wake_requests(rbd_dev, true); | |
99d16943 ID |
3873 | } |
3874 | ||
36be9a76 | 3875 | /* |
f40eb349 AE |
3876 | * Synchronous osd object method call. Returns the number of bytes |
3877 | * returned in the outbound buffer, or a negative error code. | |
36be9a76 AE |
3878 | */ |
3879 | static int rbd_obj_method_sync(struct rbd_device *rbd_dev, | |
ecd4a68a ID |
3880 | struct ceph_object_id *oid, |
3881 | struct ceph_object_locator *oloc, | |
36be9a76 | 3882 | const char *method_name, |
4157976b | 3883 | const void *outbound, |
36be9a76 | 3884 | size_t outbound_size, |
4157976b | 3885 | void *inbound, |
e2a58ee5 | 3886 | size_t inbound_size) |
36be9a76 | 3887 | { |
ecd4a68a ID |
3888 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
3889 | struct page *req_page = NULL; | |
3890 | struct page *reply_page; | |
36be9a76 AE |
3891 | int ret; |
3892 | ||
3893 | /* | |
6010a451 AE |
3894 | * Method calls are ultimately read operations. The result |
3895 | * should placed into the inbound buffer provided. They | |
3896 | * also supply outbound data--parameters for the object | |
3897 | * method. Currently if this is present it will be a | |
3898 | * snapshot id. | |
36be9a76 | 3899 | */ |
ecd4a68a ID |
3900 | if (outbound) { |
3901 | if (outbound_size > PAGE_SIZE) | |
3902 | return -E2BIG; | |
36be9a76 | 3903 | |
ecd4a68a ID |
3904 | req_page = alloc_page(GFP_KERNEL); |
3905 | if (!req_page) | |
3906 | return -ENOMEM; | |
04017e29 | 3907 | |
ecd4a68a | 3908 | memcpy(page_address(req_page), outbound, outbound_size); |
04017e29 | 3909 | } |
430c28c3 | 3910 | |
ecd4a68a ID |
3911 | reply_page = alloc_page(GFP_KERNEL); |
3912 | if (!reply_page) { | |
3913 | if (req_page) | |
3914 | __free_page(req_page); | |
3915 | return -ENOMEM; | |
3916 | } | |
57385b51 | 3917 | |
ecd4a68a ID |
3918 | ret = ceph_osdc_call(osdc, oid, oloc, RBD_DRV_NAME, method_name, |
3919 | CEPH_OSD_FLAG_READ, req_page, outbound_size, | |
3920 | reply_page, &inbound_size); | |
3921 | if (!ret) { | |
3922 | memcpy(inbound, page_address(reply_page), inbound_size); | |
3923 | ret = inbound_size; | |
3924 | } | |
36be9a76 | 3925 | |
ecd4a68a ID |
3926 | if (req_page) |
3927 | __free_page(req_page); | |
3928 | __free_page(reply_page); | |
36be9a76 AE |
3929 | return ret; |
3930 | } | |
3931 | ||
ed95b21a ID |
3932 | /* |
3933 | * lock_rwsem must be held for read | |
3934 | */ | |
3935 | static void rbd_wait_state_locked(struct rbd_device *rbd_dev) | |
3936 | { | |
3937 | DEFINE_WAIT(wait); | |
3938 | ||
3939 | do { | |
3940 | /* | |
3941 | * Note the use of mod_delayed_work() in rbd_acquire_lock() | |
3942 | * and cancel_delayed_work() in wake_requests(). | |
3943 | */ | |
3944 | dout("%s rbd_dev %p queueing lock_dwork\n", __func__, rbd_dev); | |
3945 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0); | |
3946 | prepare_to_wait_exclusive(&rbd_dev->lock_waitq, &wait, | |
3947 | TASK_UNINTERRUPTIBLE); | |
3948 | up_read(&rbd_dev->lock_rwsem); | |
3949 | schedule(); | |
3950 | down_read(&rbd_dev->lock_rwsem); | |
87c0fded ID |
3951 | } while (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED && |
3952 | !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)); | |
3953 | ||
ed95b21a ID |
3954 | finish_wait(&rbd_dev->lock_waitq, &wait); |
3955 | } | |
3956 | ||
7ad18afa | 3957 | static void rbd_queue_workfn(struct work_struct *work) |
bf0d5f50 | 3958 | { |
7ad18afa CH |
3959 | struct request *rq = blk_mq_rq_from_pdu(work); |
3960 | struct rbd_device *rbd_dev = rq->q->queuedata; | |
bc1ecc65 | 3961 | struct rbd_img_request *img_request; |
4e752f0a | 3962 | struct ceph_snap_context *snapc = NULL; |
bc1ecc65 ID |
3963 | u64 offset = (u64)blk_rq_pos(rq) << SECTOR_SHIFT; |
3964 | u64 length = blk_rq_bytes(rq); | |
6d2940c8 | 3965 | enum obj_operation_type op_type; |
4e752f0a | 3966 | u64 mapping_size; |
80de1912 | 3967 | bool must_be_locked; |
bf0d5f50 AE |
3968 | int result; |
3969 | ||
7ad18afa CH |
3970 | if (rq->cmd_type != REQ_TYPE_FS) { |
3971 | dout("%s: non-fs request type %d\n", __func__, | |
3972 | (int) rq->cmd_type); | |
3973 | result = -EIO; | |
3974 | goto err; | |
3975 | } | |
3976 | ||
c2df40df | 3977 | if (req_op(rq) == REQ_OP_DISCARD) |
90e98c52 | 3978 | op_type = OBJ_OP_DISCARD; |
c2df40df | 3979 | else if (req_op(rq) == REQ_OP_WRITE) |
6d2940c8 GZ |
3980 | op_type = OBJ_OP_WRITE; |
3981 | else | |
3982 | op_type = OBJ_OP_READ; | |
3983 | ||
bc1ecc65 | 3984 | /* Ignore/skip any zero-length requests */ |
bf0d5f50 | 3985 | |
bc1ecc65 ID |
3986 | if (!length) { |
3987 | dout("%s: zero-length request\n", __func__); | |
3988 | result = 0; | |
3989 | goto err_rq; | |
3990 | } | |
bf0d5f50 | 3991 | |
6d2940c8 | 3992 | /* Only reads are allowed to a read-only device */ |
bc1ecc65 | 3993 | |
6d2940c8 | 3994 | if (op_type != OBJ_OP_READ) { |
bc1ecc65 ID |
3995 | if (rbd_dev->mapping.read_only) { |
3996 | result = -EROFS; | |
3997 | goto err_rq; | |
4dda41d3 | 3998 | } |
bc1ecc65 ID |
3999 | rbd_assert(rbd_dev->spec->snap_id == CEPH_NOSNAP); |
4000 | } | |
4dda41d3 | 4001 | |
bc1ecc65 ID |
4002 | /* |
4003 | * Quit early if the mapped snapshot no longer exists. It's | |
4004 | * still possible the snapshot will have disappeared by the | |
4005 | * time our request arrives at the osd, but there's no sense in | |
4006 | * sending it if we already know. | |
4007 | */ | |
4008 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) { | |
4009 | dout("request for non-existent snapshot"); | |
4010 | rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP); | |
4011 | result = -ENXIO; | |
4012 | goto err_rq; | |
4013 | } | |
4dda41d3 | 4014 | |
bc1ecc65 ID |
4015 | if (offset && length > U64_MAX - offset + 1) { |
4016 | rbd_warn(rbd_dev, "bad request range (%llu~%llu)", offset, | |
4017 | length); | |
4018 | result = -EINVAL; | |
4019 | goto err_rq; /* Shouldn't happen */ | |
4020 | } | |
4dda41d3 | 4021 | |
7ad18afa CH |
4022 | blk_mq_start_request(rq); |
4023 | ||
4e752f0a JD |
4024 | down_read(&rbd_dev->header_rwsem); |
4025 | mapping_size = rbd_dev->mapping.size; | |
6d2940c8 | 4026 | if (op_type != OBJ_OP_READ) { |
4e752f0a JD |
4027 | snapc = rbd_dev->header.snapc; |
4028 | ceph_get_snap_context(snapc); | |
ed95b21a | 4029 | must_be_locked = rbd_is_lock_supported(rbd_dev); |
80de1912 ID |
4030 | } else { |
4031 | must_be_locked = rbd_dev->opts->lock_on_read && | |
4032 | rbd_is_lock_supported(rbd_dev); | |
4e752f0a JD |
4033 | } |
4034 | up_read(&rbd_dev->header_rwsem); | |
4035 | ||
4036 | if (offset + length > mapping_size) { | |
bc1ecc65 | 4037 | rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)", offset, |
4e752f0a | 4038 | length, mapping_size); |
bc1ecc65 ID |
4039 | result = -EIO; |
4040 | goto err_rq; | |
4041 | } | |
bf0d5f50 | 4042 | |
ed95b21a ID |
4043 | if (must_be_locked) { |
4044 | down_read(&rbd_dev->lock_rwsem); | |
87c0fded ID |
4045 | if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED && |
4046 | !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) | |
ed95b21a | 4047 | rbd_wait_state_locked(rbd_dev); |
87c0fded ID |
4048 | |
4049 | WARN_ON((rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) ^ | |
4050 | !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)); | |
4051 | if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) { | |
4052 | result = -EBLACKLISTED; | |
4053 | goto err_unlock; | |
4054 | } | |
ed95b21a ID |
4055 | } |
4056 | ||
6d2940c8 | 4057 | img_request = rbd_img_request_create(rbd_dev, offset, length, op_type, |
4e752f0a | 4058 | snapc); |
bc1ecc65 ID |
4059 | if (!img_request) { |
4060 | result = -ENOMEM; | |
ed95b21a | 4061 | goto err_unlock; |
bc1ecc65 ID |
4062 | } |
4063 | img_request->rq = rq; | |
70b16db8 | 4064 | snapc = NULL; /* img_request consumes a ref */ |
bf0d5f50 | 4065 | |
90e98c52 GZ |
4066 | if (op_type == OBJ_OP_DISCARD) |
4067 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_NODATA, | |
4068 | NULL); | |
4069 | else | |
4070 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO, | |
4071 | rq->bio); | |
bc1ecc65 ID |
4072 | if (result) |
4073 | goto err_img_request; | |
bf0d5f50 | 4074 | |
bc1ecc65 ID |
4075 | result = rbd_img_request_submit(img_request); |
4076 | if (result) | |
4077 | goto err_img_request; | |
bf0d5f50 | 4078 | |
ed95b21a ID |
4079 | if (must_be_locked) |
4080 | up_read(&rbd_dev->lock_rwsem); | |
bc1ecc65 | 4081 | return; |
bf0d5f50 | 4082 | |
bc1ecc65 ID |
4083 | err_img_request: |
4084 | rbd_img_request_put(img_request); | |
ed95b21a ID |
4085 | err_unlock: |
4086 | if (must_be_locked) | |
4087 | up_read(&rbd_dev->lock_rwsem); | |
bc1ecc65 ID |
4088 | err_rq: |
4089 | if (result) | |
4090 | rbd_warn(rbd_dev, "%s %llx at %llx result %d", | |
6d2940c8 | 4091 | obj_op_name(op_type), length, offset, result); |
e96a650a | 4092 | ceph_put_snap_context(snapc); |
7ad18afa CH |
4093 | err: |
4094 | blk_mq_end_request(rq, result); | |
bc1ecc65 | 4095 | } |
bf0d5f50 | 4096 | |
7ad18afa CH |
4097 | static int rbd_queue_rq(struct blk_mq_hw_ctx *hctx, |
4098 | const struct blk_mq_queue_data *bd) | |
bc1ecc65 | 4099 | { |
7ad18afa CH |
4100 | struct request *rq = bd->rq; |
4101 | struct work_struct *work = blk_mq_rq_to_pdu(rq); | |
bf0d5f50 | 4102 | |
7ad18afa CH |
4103 | queue_work(rbd_wq, work); |
4104 | return BLK_MQ_RQ_QUEUE_OK; | |
bf0d5f50 AE |
4105 | } |
4106 | ||
602adf40 YS |
4107 | static void rbd_free_disk(struct rbd_device *rbd_dev) |
4108 | { | |
4109 | struct gendisk *disk = rbd_dev->disk; | |
4110 | ||
4111 | if (!disk) | |
4112 | return; | |
4113 | ||
a0cab924 AE |
4114 | rbd_dev->disk = NULL; |
4115 | if (disk->flags & GENHD_FL_UP) { | |
602adf40 | 4116 | del_gendisk(disk); |
a0cab924 AE |
4117 | if (disk->queue) |
4118 | blk_cleanup_queue(disk->queue); | |
7ad18afa | 4119 | blk_mq_free_tag_set(&rbd_dev->tag_set); |
a0cab924 | 4120 | } |
602adf40 YS |
4121 | put_disk(disk); |
4122 | } | |
4123 | ||
788e2df3 | 4124 | static int rbd_obj_read_sync(struct rbd_device *rbd_dev, |
fe5478e0 ID |
4125 | struct ceph_object_id *oid, |
4126 | struct ceph_object_locator *oloc, | |
4127 | void *buf, int buf_len) | |
788e2df3 AE |
4128 | |
4129 | { | |
fe5478e0 ID |
4130 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
4131 | struct ceph_osd_request *req; | |
4132 | struct page **pages; | |
4133 | int num_pages = calc_pages_for(0, buf_len); | |
788e2df3 AE |
4134 | int ret; |
4135 | ||
fe5478e0 ID |
4136 | req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL); |
4137 | if (!req) | |
4138 | return -ENOMEM; | |
788e2df3 | 4139 | |
fe5478e0 ID |
4140 | ceph_oid_copy(&req->r_base_oid, oid); |
4141 | ceph_oloc_copy(&req->r_base_oloc, oloc); | |
4142 | req->r_flags = CEPH_OSD_FLAG_READ; | |
430c28c3 | 4143 | |
fe5478e0 | 4144 | ret = ceph_osdc_alloc_messages(req, GFP_KERNEL); |
788e2df3 | 4145 | if (ret) |
fe5478e0 | 4146 | goto out_req; |
788e2df3 | 4147 | |
fe5478e0 ID |
4148 | pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL); |
4149 | if (IS_ERR(pages)) { | |
4150 | ret = PTR_ERR(pages); | |
4151 | goto out_req; | |
4152 | } | |
1ceae7ef | 4153 | |
fe5478e0 ID |
4154 | osd_req_op_extent_init(req, 0, CEPH_OSD_OP_READ, 0, buf_len, 0, 0); |
4155 | osd_req_op_extent_osd_data_pages(req, 0, pages, buf_len, 0, false, | |
4156 | true); | |
4157 | ||
4158 | ceph_osdc_start_request(osdc, req, false); | |
4159 | ret = ceph_osdc_wait_request(osdc, req); | |
4160 | if (ret >= 0) | |
4161 | ceph_copy_from_page_vector(pages, buf, 0, ret); | |
788e2df3 | 4162 | |
fe5478e0 ID |
4163 | out_req: |
4164 | ceph_osdc_put_request(req); | |
788e2df3 AE |
4165 | return ret; |
4166 | } | |
4167 | ||
602adf40 | 4168 | /* |
662518b1 AE |
4169 | * Read the complete header for the given rbd device. On successful |
4170 | * return, the rbd_dev->header field will contain up-to-date | |
4171 | * information about the image. | |
602adf40 | 4172 | */ |
99a41ebc | 4173 | static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev) |
602adf40 | 4174 | { |
4156d998 | 4175 | struct rbd_image_header_ondisk *ondisk = NULL; |
50f7c4c9 | 4176 | u32 snap_count = 0; |
4156d998 AE |
4177 | u64 names_size = 0; |
4178 | u32 want_count; | |
4179 | int ret; | |
602adf40 | 4180 | |
00f1f36f | 4181 | /* |
4156d998 AE |
4182 | * The complete header will include an array of its 64-bit |
4183 | * snapshot ids, followed by the names of those snapshots as | |
4184 | * a contiguous block of NUL-terminated strings. Note that | |
4185 | * the number of snapshots could change by the time we read | |
4186 | * it in, in which case we re-read it. | |
00f1f36f | 4187 | */ |
4156d998 AE |
4188 | do { |
4189 | size_t size; | |
4190 | ||
4191 | kfree(ondisk); | |
4192 | ||
4193 | size = sizeof (*ondisk); | |
4194 | size += snap_count * sizeof (struct rbd_image_snap_ondisk); | |
4195 | size += names_size; | |
4196 | ondisk = kmalloc(size, GFP_KERNEL); | |
4197 | if (!ondisk) | |
662518b1 | 4198 | return -ENOMEM; |
4156d998 | 4199 | |
fe5478e0 ID |
4200 | ret = rbd_obj_read_sync(rbd_dev, &rbd_dev->header_oid, |
4201 | &rbd_dev->header_oloc, ondisk, size); | |
4156d998 | 4202 | if (ret < 0) |
662518b1 | 4203 | goto out; |
c0cd10db | 4204 | if ((size_t)ret < size) { |
4156d998 | 4205 | ret = -ENXIO; |
06ecc6cb AE |
4206 | rbd_warn(rbd_dev, "short header read (want %zd got %d)", |
4207 | size, ret); | |
662518b1 | 4208 | goto out; |
4156d998 AE |
4209 | } |
4210 | if (!rbd_dev_ondisk_valid(ondisk)) { | |
4211 | ret = -ENXIO; | |
06ecc6cb | 4212 | rbd_warn(rbd_dev, "invalid header"); |
662518b1 | 4213 | goto out; |
81e759fb | 4214 | } |
602adf40 | 4215 | |
4156d998 AE |
4216 | names_size = le64_to_cpu(ondisk->snap_names_len); |
4217 | want_count = snap_count; | |
4218 | snap_count = le32_to_cpu(ondisk->snap_count); | |
4219 | } while (snap_count != want_count); | |
00f1f36f | 4220 | |
662518b1 AE |
4221 | ret = rbd_header_from_disk(rbd_dev, ondisk); |
4222 | out: | |
4156d998 AE |
4223 | kfree(ondisk); |
4224 | ||
4225 | return ret; | |
602adf40 YS |
4226 | } |
4227 | ||
15228ede AE |
4228 | /* |
4229 | * Clear the rbd device's EXISTS flag if the snapshot it's mapped to | |
4230 | * has disappeared from the (just updated) snapshot context. | |
4231 | */ | |
4232 | static void rbd_exists_validate(struct rbd_device *rbd_dev) | |
4233 | { | |
4234 | u64 snap_id; | |
4235 | ||
4236 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) | |
4237 | return; | |
4238 | ||
4239 | snap_id = rbd_dev->spec->snap_id; | |
4240 | if (snap_id == CEPH_NOSNAP) | |
4241 | return; | |
4242 | ||
4243 | if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX) | |
4244 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); | |
4245 | } | |
4246 | ||
9875201e JD |
4247 | static void rbd_dev_update_size(struct rbd_device *rbd_dev) |
4248 | { | |
4249 | sector_t size; | |
9875201e JD |
4250 | |
4251 | /* | |
811c6688 ID |
4252 | * If EXISTS is not set, rbd_dev->disk may be NULL, so don't |
4253 | * try to update its size. If REMOVING is set, updating size | |
4254 | * is just useless work since the device can't be opened. | |
9875201e | 4255 | */ |
811c6688 ID |
4256 | if (test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags) && |
4257 | !test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) { | |
9875201e JD |
4258 | size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE; |
4259 | dout("setting size to %llu sectors", (unsigned long long)size); | |
4260 | set_capacity(rbd_dev->disk, size); | |
4261 | revalidate_disk(rbd_dev->disk); | |
4262 | } | |
4263 | } | |
4264 | ||
cc4a38bd | 4265 | static int rbd_dev_refresh(struct rbd_device *rbd_dev) |
1fe5e993 | 4266 | { |
e627db08 | 4267 | u64 mapping_size; |
1fe5e993 AE |
4268 | int ret; |
4269 | ||
cfbf6377 | 4270 | down_write(&rbd_dev->header_rwsem); |
3b5cf2a2 | 4271 | mapping_size = rbd_dev->mapping.size; |
a720ae09 ID |
4272 | |
4273 | ret = rbd_dev_header_info(rbd_dev); | |
52bb1f9b | 4274 | if (ret) |
73e39e4d | 4275 | goto out; |
15228ede | 4276 | |
e8f59b59 ID |
4277 | /* |
4278 | * If there is a parent, see if it has disappeared due to the | |
4279 | * mapped image getting flattened. | |
4280 | */ | |
4281 | if (rbd_dev->parent) { | |
4282 | ret = rbd_dev_v2_parent_info(rbd_dev); | |
4283 | if (ret) | |
73e39e4d | 4284 | goto out; |
e8f59b59 ID |
4285 | } |
4286 | ||
5ff1108c | 4287 | if (rbd_dev->spec->snap_id == CEPH_NOSNAP) { |
73e39e4d | 4288 | rbd_dev->mapping.size = rbd_dev->header.image_size; |
5ff1108c ID |
4289 | } else { |
4290 | /* validate mapped snapshot's EXISTS flag */ | |
4291 | rbd_exists_validate(rbd_dev); | |
4292 | } | |
15228ede | 4293 | |
73e39e4d | 4294 | out: |
cfbf6377 | 4295 | up_write(&rbd_dev->header_rwsem); |
73e39e4d | 4296 | if (!ret && mapping_size != rbd_dev->mapping.size) |
9875201e | 4297 | rbd_dev_update_size(rbd_dev); |
1fe5e993 | 4298 | |
73e39e4d | 4299 | return ret; |
1fe5e993 AE |
4300 | } |
4301 | ||
7ad18afa CH |
4302 | static int rbd_init_request(void *data, struct request *rq, |
4303 | unsigned int hctx_idx, unsigned int request_idx, | |
4304 | unsigned int numa_node) | |
4305 | { | |
4306 | struct work_struct *work = blk_mq_rq_to_pdu(rq); | |
4307 | ||
4308 | INIT_WORK(work, rbd_queue_workfn); | |
4309 | return 0; | |
4310 | } | |
4311 | ||
4312 | static struct blk_mq_ops rbd_mq_ops = { | |
4313 | .queue_rq = rbd_queue_rq, | |
7ad18afa CH |
4314 | .init_request = rbd_init_request, |
4315 | }; | |
4316 | ||
602adf40 YS |
4317 | static int rbd_init_disk(struct rbd_device *rbd_dev) |
4318 | { | |
4319 | struct gendisk *disk; | |
4320 | struct request_queue *q; | |
593a9e7b | 4321 | u64 segment_size; |
7ad18afa | 4322 | int err; |
602adf40 | 4323 | |
602adf40 | 4324 | /* create gendisk info */ |
7e513d43 ID |
4325 | disk = alloc_disk(single_major ? |
4326 | (1 << RBD_SINGLE_MAJOR_PART_SHIFT) : | |
4327 | RBD_MINORS_PER_MAJOR); | |
602adf40 | 4328 | if (!disk) |
1fcdb8aa | 4329 | return -ENOMEM; |
602adf40 | 4330 | |
f0f8cef5 | 4331 | snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d", |
de71a297 | 4332 | rbd_dev->dev_id); |
602adf40 | 4333 | disk->major = rbd_dev->major; |
dd82fff1 | 4334 | disk->first_minor = rbd_dev->minor; |
7e513d43 ID |
4335 | if (single_major) |
4336 | disk->flags |= GENHD_FL_EXT_DEVT; | |
602adf40 YS |
4337 | disk->fops = &rbd_bd_ops; |
4338 | disk->private_data = rbd_dev; | |
4339 | ||
7ad18afa CH |
4340 | memset(&rbd_dev->tag_set, 0, sizeof(rbd_dev->tag_set)); |
4341 | rbd_dev->tag_set.ops = &rbd_mq_ops; | |
b5584180 | 4342 | rbd_dev->tag_set.queue_depth = rbd_dev->opts->queue_depth; |
7ad18afa | 4343 | rbd_dev->tag_set.numa_node = NUMA_NO_NODE; |
b5584180 | 4344 | rbd_dev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE; |
7ad18afa CH |
4345 | rbd_dev->tag_set.nr_hw_queues = 1; |
4346 | rbd_dev->tag_set.cmd_size = sizeof(struct work_struct); | |
4347 | ||
4348 | err = blk_mq_alloc_tag_set(&rbd_dev->tag_set); | |
4349 | if (err) | |
602adf40 | 4350 | goto out_disk; |
029bcbd8 | 4351 | |
7ad18afa CH |
4352 | q = blk_mq_init_queue(&rbd_dev->tag_set); |
4353 | if (IS_ERR(q)) { | |
4354 | err = PTR_ERR(q); | |
4355 | goto out_tag_set; | |
4356 | } | |
4357 | ||
d8a2c89c ID |
4358 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q); |
4359 | /* QUEUE_FLAG_ADD_RANDOM is off by default for blk-mq */ | |
593a9e7b | 4360 | |
029bcbd8 | 4361 | /* set io sizes to object size */ |
593a9e7b AE |
4362 | segment_size = rbd_obj_bytes(&rbd_dev->header); |
4363 | blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE); | |
0d9fde4f | 4364 | q->limits.max_sectors = queue_max_hw_sectors(q); |
d3834fef | 4365 | blk_queue_max_segments(q, segment_size / SECTOR_SIZE); |
593a9e7b AE |
4366 | blk_queue_max_segment_size(q, segment_size); |
4367 | blk_queue_io_min(q, segment_size); | |
4368 | blk_queue_io_opt(q, segment_size); | |
029bcbd8 | 4369 | |
90e98c52 GZ |
4370 | /* enable the discard support */ |
4371 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q); | |
4372 | q->limits.discard_granularity = segment_size; | |
4373 | q->limits.discard_alignment = segment_size; | |
2bb4cd5c | 4374 | blk_queue_max_discard_sectors(q, segment_size / SECTOR_SIZE); |
b76f8239 | 4375 | q->limits.discard_zeroes_data = 1; |
90e98c52 | 4376 | |
bae818ee RH |
4377 | if (!ceph_test_opt(rbd_dev->rbd_client->client, NOCRC)) |
4378 | q->backing_dev_info.capabilities |= BDI_CAP_STABLE_WRITES; | |
4379 | ||
602adf40 YS |
4380 | disk->queue = q; |
4381 | ||
4382 | q->queuedata = rbd_dev; | |
4383 | ||
4384 | rbd_dev->disk = disk; | |
602adf40 | 4385 | |
602adf40 | 4386 | return 0; |
7ad18afa CH |
4387 | out_tag_set: |
4388 | blk_mq_free_tag_set(&rbd_dev->tag_set); | |
602adf40 YS |
4389 | out_disk: |
4390 | put_disk(disk); | |
7ad18afa | 4391 | return err; |
602adf40 YS |
4392 | } |
4393 | ||
dfc5606d YS |
4394 | /* |
4395 | sysfs | |
4396 | */ | |
4397 | ||
593a9e7b AE |
4398 | static struct rbd_device *dev_to_rbd_dev(struct device *dev) |
4399 | { | |
4400 | return container_of(dev, struct rbd_device, dev); | |
4401 | } | |
4402 | ||
dfc5606d YS |
4403 | static ssize_t rbd_size_show(struct device *dev, |
4404 | struct device_attribute *attr, char *buf) | |
4405 | { | |
593a9e7b | 4406 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
a51aa0c0 | 4407 | |
fc71d833 AE |
4408 | return sprintf(buf, "%llu\n", |
4409 | (unsigned long long)rbd_dev->mapping.size); | |
dfc5606d YS |
4410 | } |
4411 | ||
34b13184 AE |
4412 | /* |
4413 | * Note this shows the features for whatever's mapped, which is not | |
4414 | * necessarily the base image. | |
4415 | */ | |
4416 | static ssize_t rbd_features_show(struct device *dev, | |
4417 | struct device_attribute *attr, char *buf) | |
4418 | { | |
4419 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); | |
4420 | ||
4421 | return sprintf(buf, "0x%016llx\n", | |
fc71d833 | 4422 | (unsigned long long)rbd_dev->mapping.features); |
34b13184 AE |
4423 | } |
4424 | ||
dfc5606d YS |
4425 | static ssize_t rbd_major_show(struct device *dev, |
4426 | struct device_attribute *attr, char *buf) | |
4427 | { | |
593a9e7b | 4428 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
602adf40 | 4429 | |
fc71d833 AE |
4430 | if (rbd_dev->major) |
4431 | return sprintf(buf, "%d\n", rbd_dev->major); | |
4432 | ||
4433 | return sprintf(buf, "(none)\n"); | |
dd82fff1 ID |
4434 | } |
4435 | ||
4436 | static ssize_t rbd_minor_show(struct device *dev, | |
4437 | struct device_attribute *attr, char *buf) | |
4438 | { | |
4439 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); | |
fc71d833 | 4440 | |
dd82fff1 | 4441 | return sprintf(buf, "%d\n", rbd_dev->minor); |
dfc5606d YS |
4442 | } |
4443 | ||
005a07bf ID |
4444 | static ssize_t rbd_client_addr_show(struct device *dev, |
4445 | struct device_attribute *attr, char *buf) | |
4446 | { | |
4447 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); | |
4448 | struct ceph_entity_addr *client_addr = | |
4449 | ceph_client_addr(rbd_dev->rbd_client->client); | |
4450 | ||
4451 | return sprintf(buf, "%pISpc/%u\n", &client_addr->in_addr, | |
4452 | le32_to_cpu(client_addr->nonce)); | |
4453 | } | |
4454 | ||
dfc5606d YS |
4455 | static ssize_t rbd_client_id_show(struct device *dev, |
4456 | struct device_attribute *attr, char *buf) | |
602adf40 | 4457 | { |
593a9e7b | 4458 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
dfc5606d | 4459 | |
1dbb4399 | 4460 | return sprintf(buf, "client%lld\n", |
033268a5 | 4461 | ceph_client_gid(rbd_dev->rbd_client->client)); |
602adf40 YS |
4462 | } |
4463 | ||
267fb90b MC |
4464 | static ssize_t rbd_cluster_fsid_show(struct device *dev, |
4465 | struct device_attribute *attr, char *buf) | |
4466 | { | |
4467 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); | |
4468 | ||
4469 | return sprintf(buf, "%pU\n", &rbd_dev->rbd_client->client->fsid); | |
4470 | } | |
4471 | ||
0d6d1e9c MC |
4472 | static ssize_t rbd_config_info_show(struct device *dev, |
4473 | struct device_attribute *attr, char *buf) | |
4474 | { | |
4475 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); | |
4476 | ||
4477 | return sprintf(buf, "%s\n", rbd_dev->config_info); | |
602adf40 YS |
4478 | } |
4479 | ||
dfc5606d YS |
4480 | static ssize_t rbd_pool_show(struct device *dev, |
4481 | struct device_attribute *attr, char *buf) | |
602adf40 | 4482 | { |
593a9e7b | 4483 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
dfc5606d | 4484 | |
0d7dbfce | 4485 | return sprintf(buf, "%s\n", rbd_dev->spec->pool_name); |
dfc5606d YS |
4486 | } |
4487 | ||
9bb2f334 AE |
4488 | static ssize_t rbd_pool_id_show(struct device *dev, |
4489 | struct device_attribute *attr, char *buf) | |
4490 | { | |
4491 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); | |
4492 | ||
0d7dbfce | 4493 | return sprintf(buf, "%llu\n", |
fc71d833 | 4494 | (unsigned long long) rbd_dev->spec->pool_id); |
9bb2f334 AE |
4495 | } |
4496 | ||
dfc5606d YS |
4497 | static ssize_t rbd_name_show(struct device *dev, |
4498 | struct device_attribute *attr, char *buf) | |
4499 | { | |
593a9e7b | 4500 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
dfc5606d | 4501 | |
a92ffdf8 AE |
4502 | if (rbd_dev->spec->image_name) |
4503 | return sprintf(buf, "%s\n", rbd_dev->spec->image_name); | |
4504 | ||
4505 | return sprintf(buf, "(unknown)\n"); | |
dfc5606d YS |
4506 | } |
4507 | ||
589d30e0 AE |
4508 | static ssize_t rbd_image_id_show(struct device *dev, |
4509 | struct device_attribute *attr, char *buf) | |
4510 | { | |
4511 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); | |
4512 | ||
0d7dbfce | 4513 | return sprintf(buf, "%s\n", rbd_dev->spec->image_id); |
589d30e0 AE |
4514 | } |
4515 | ||
34b13184 AE |
4516 | /* |
4517 | * Shows the name of the currently-mapped snapshot (or | |
4518 | * RBD_SNAP_HEAD_NAME for the base image). | |
4519 | */ | |
dfc5606d YS |
4520 | static ssize_t rbd_snap_show(struct device *dev, |
4521 | struct device_attribute *attr, | |
4522 | char *buf) | |
4523 | { | |
593a9e7b | 4524 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
dfc5606d | 4525 | |
0d7dbfce | 4526 | return sprintf(buf, "%s\n", rbd_dev->spec->snap_name); |
dfc5606d YS |
4527 | } |
4528 | ||
92a58671 MC |
4529 | static ssize_t rbd_snap_id_show(struct device *dev, |
4530 | struct device_attribute *attr, char *buf) | |
4531 | { | |
4532 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); | |
4533 | ||
4534 | return sprintf(buf, "%llu\n", rbd_dev->spec->snap_id); | |
4535 | } | |
4536 | ||
86b00e0d | 4537 | /* |
ff96128f ID |
4538 | * For a v2 image, shows the chain of parent images, separated by empty |
4539 | * lines. For v1 images or if there is no parent, shows "(no parent | |
4540 | * image)". | |
86b00e0d AE |
4541 | */ |
4542 | static ssize_t rbd_parent_show(struct device *dev, | |
ff96128f ID |
4543 | struct device_attribute *attr, |
4544 | char *buf) | |
86b00e0d AE |
4545 | { |
4546 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); | |
ff96128f | 4547 | ssize_t count = 0; |
86b00e0d | 4548 | |
ff96128f | 4549 | if (!rbd_dev->parent) |
86b00e0d AE |
4550 | return sprintf(buf, "(no parent image)\n"); |
4551 | ||
ff96128f ID |
4552 | for ( ; rbd_dev->parent; rbd_dev = rbd_dev->parent) { |
4553 | struct rbd_spec *spec = rbd_dev->parent_spec; | |
4554 | ||
4555 | count += sprintf(&buf[count], "%s" | |
4556 | "pool_id %llu\npool_name %s\n" | |
4557 | "image_id %s\nimage_name %s\n" | |
4558 | "snap_id %llu\nsnap_name %s\n" | |
4559 | "overlap %llu\n", | |
4560 | !count ? "" : "\n", /* first? */ | |
4561 | spec->pool_id, spec->pool_name, | |
4562 | spec->image_id, spec->image_name ?: "(unknown)", | |
4563 | spec->snap_id, spec->snap_name, | |
4564 | rbd_dev->parent_overlap); | |
4565 | } | |
4566 | ||
4567 | return count; | |
86b00e0d AE |
4568 | } |
4569 | ||
dfc5606d YS |
4570 | static ssize_t rbd_image_refresh(struct device *dev, |
4571 | struct device_attribute *attr, | |
4572 | const char *buf, | |
4573 | size_t size) | |
4574 | { | |
593a9e7b | 4575 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
b813623a | 4576 | int ret; |
602adf40 | 4577 | |
cc4a38bd | 4578 | ret = rbd_dev_refresh(rbd_dev); |
e627db08 | 4579 | if (ret) |
52bb1f9b | 4580 | return ret; |
b813623a | 4581 | |
52bb1f9b | 4582 | return size; |
dfc5606d | 4583 | } |
602adf40 | 4584 | |
dfc5606d | 4585 | static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL); |
34b13184 | 4586 | static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL); |
dfc5606d | 4587 | static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL); |
dd82fff1 | 4588 | static DEVICE_ATTR(minor, S_IRUGO, rbd_minor_show, NULL); |
005a07bf | 4589 | static DEVICE_ATTR(client_addr, S_IRUGO, rbd_client_addr_show, NULL); |
dfc5606d | 4590 | static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL); |
267fb90b | 4591 | static DEVICE_ATTR(cluster_fsid, S_IRUGO, rbd_cluster_fsid_show, NULL); |
0d6d1e9c | 4592 | static DEVICE_ATTR(config_info, S_IRUSR, rbd_config_info_show, NULL); |
dfc5606d | 4593 | static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL); |
9bb2f334 | 4594 | static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL); |
dfc5606d | 4595 | static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL); |
589d30e0 | 4596 | static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL); |
dfc5606d YS |
4597 | static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh); |
4598 | static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL); | |
92a58671 | 4599 | static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL); |
86b00e0d | 4600 | static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL); |
dfc5606d YS |
4601 | |
4602 | static struct attribute *rbd_attrs[] = { | |
4603 | &dev_attr_size.attr, | |
34b13184 | 4604 | &dev_attr_features.attr, |
dfc5606d | 4605 | &dev_attr_major.attr, |
dd82fff1 | 4606 | &dev_attr_minor.attr, |
005a07bf | 4607 | &dev_attr_client_addr.attr, |
dfc5606d | 4608 | &dev_attr_client_id.attr, |
267fb90b | 4609 | &dev_attr_cluster_fsid.attr, |
0d6d1e9c | 4610 | &dev_attr_config_info.attr, |
dfc5606d | 4611 | &dev_attr_pool.attr, |
9bb2f334 | 4612 | &dev_attr_pool_id.attr, |
dfc5606d | 4613 | &dev_attr_name.attr, |
589d30e0 | 4614 | &dev_attr_image_id.attr, |
dfc5606d | 4615 | &dev_attr_current_snap.attr, |
92a58671 | 4616 | &dev_attr_snap_id.attr, |
86b00e0d | 4617 | &dev_attr_parent.attr, |
dfc5606d | 4618 | &dev_attr_refresh.attr, |
dfc5606d YS |
4619 | NULL |
4620 | }; | |
4621 | ||
4622 | static struct attribute_group rbd_attr_group = { | |
4623 | .attrs = rbd_attrs, | |
4624 | }; | |
4625 | ||
4626 | static const struct attribute_group *rbd_attr_groups[] = { | |
4627 | &rbd_attr_group, | |
4628 | NULL | |
4629 | }; | |
4630 | ||
6cac4695 | 4631 | static void rbd_dev_release(struct device *dev); |
dfc5606d | 4632 | |
b9942bc9 | 4633 | static const struct device_type rbd_device_type = { |
dfc5606d YS |
4634 | .name = "rbd", |
4635 | .groups = rbd_attr_groups, | |
6cac4695 | 4636 | .release = rbd_dev_release, |
dfc5606d YS |
4637 | }; |
4638 | ||
8b8fb99c AE |
4639 | static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec) |
4640 | { | |
4641 | kref_get(&spec->kref); | |
4642 | ||
4643 | return spec; | |
4644 | } | |
4645 | ||
4646 | static void rbd_spec_free(struct kref *kref); | |
4647 | static void rbd_spec_put(struct rbd_spec *spec) | |
4648 | { | |
4649 | if (spec) | |
4650 | kref_put(&spec->kref, rbd_spec_free); | |
4651 | } | |
4652 | ||
4653 | static struct rbd_spec *rbd_spec_alloc(void) | |
4654 | { | |
4655 | struct rbd_spec *spec; | |
4656 | ||
4657 | spec = kzalloc(sizeof (*spec), GFP_KERNEL); | |
4658 | if (!spec) | |
4659 | return NULL; | |
04077599 ID |
4660 | |
4661 | spec->pool_id = CEPH_NOPOOL; | |
4662 | spec->snap_id = CEPH_NOSNAP; | |
8b8fb99c AE |
4663 | kref_init(&spec->kref); |
4664 | ||
8b8fb99c AE |
4665 | return spec; |
4666 | } | |
4667 | ||
4668 | static void rbd_spec_free(struct kref *kref) | |
4669 | { | |
4670 | struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref); | |
4671 | ||
4672 | kfree(spec->pool_name); | |
4673 | kfree(spec->image_id); | |
4674 | kfree(spec->image_name); | |
4675 | kfree(spec->snap_name); | |
4676 | kfree(spec); | |
4677 | } | |
4678 | ||
1643dfa4 | 4679 | static void rbd_dev_free(struct rbd_device *rbd_dev) |
dd5ac32d | 4680 | { |
99d16943 | 4681 | WARN_ON(rbd_dev->watch_state != RBD_WATCH_STATE_UNREGISTERED); |
ed95b21a | 4682 | WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_UNLOCKED); |
dd5ac32d | 4683 | |
c41d13a3 | 4684 | ceph_oid_destroy(&rbd_dev->header_oid); |
6b6dddbe | 4685 | ceph_oloc_destroy(&rbd_dev->header_oloc); |
0d6d1e9c | 4686 | kfree(rbd_dev->config_info); |
c41d13a3 | 4687 | |
dd5ac32d ID |
4688 | rbd_put_client(rbd_dev->rbd_client); |
4689 | rbd_spec_put(rbd_dev->spec); | |
4690 | kfree(rbd_dev->opts); | |
4691 | kfree(rbd_dev); | |
1643dfa4 ID |
4692 | } |
4693 | ||
4694 | static void rbd_dev_release(struct device *dev) | |
4695 | { | |
4696 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); | |
4697 | bool need_put = !!rbd_dev->opts; | |
4698 | ||
4699 | if (need_put) { | |
4700 | destroy_workqueue(rbd_dev->task_wq); | |
4701 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); | |
4702 | } | |
4703 | ||
4704 | rbd_dev_free(rbd_dev); | |
dd5ac32d ID |
4705 | |
4706 | /* | |
4707 | * This is racy, but way better than putting module outside of | |
4708 | * the release callback. The race window is pretty small, so | |
4709 | * doing something similar to dm (dm-builtin.c) is overkill. | |
4710 | */ | |
4711 | if (need_put) | |
4712 | module_put(THIS_MODULE); | |
4713 | } | |
4714 | ||
1643dfa4 ID |
4715 | static struct rbd_device *__rbd_dev_create(struct rbd_client *rbdc, |
4716 | struct rbd_spec *spec) | |
c53d5893 AE |
4717 | { |
4718 | struct rbd_device *rbd_dev; | |
4719 | ||
1643dfa4 | 4720 | rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL); |
c53d5893 AE |
4721 | if (!rbd_dev) |
4722 | return NULL; | |
4723 | ||
4724 | spin_lock_init(&rbd_dev->lock); | |
4725 | INIT_LIST_HEAD(&rbd_dev->node); | |
c53d5893 AE |
4726 | init_rwsem(&rbd_dev->header_rwsem); |
4727 | ||
7e97332e | 4728 | rbd_dev->header.data_pool_id = CEPH_NOPOOL; |
c41d13a3 | 4729 | ceph_oid_init(&rbd_dev->header_oid); |
431a02cd | 4730 | rbd_dev->header_oloc.pool = spec->pool_id; |
c41d13a3 | 4731 | |
99d16943 ID |
4732 | mutex_init(&rbd_dev->watch_mutex); |
4733 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; | |
4734 | INIT_DELAYED_WORK(&rbd_dev->watch_dwork, rbd_reregister_watch); | |
4735 | ||
ed95b21a ID |
4736 | init_rwsem(&rbd_dev->lock_rwsem); |
4737 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; | |
4738 | INIT_WORK(&rbd_dev->acquired_lock_work, rbd_notify_acquired_lock); | |
4739 | INIT_WORK(&rbd_dev->released_lock_work, rbd_notify_released_lock); | |
4740 | INIT_DELAYED_WORK(&rbd_dev->lock_dwork, rbd_acquire_lock); | |
4741 | INIT_WORK(&rbd_dev->unlock_work, rbd_release_lock_work); | |
4742 | init_waitqueue_head(&rbd_dev->lock_waitq); | |
4743 | ||
dd5ac32d ID |
4744 | rbd_dev->dev.bus = &rbd_bus_type; |
4745 | rbd_dev->dev.type = &rbd_device_type; | |
4746 | rbd_dev->dev.parent = &rbd_root_dev; | |
dd5ac32d ID |
4747 | device_initialize(&rbd_dev->dev); |
4748 | ||
c53d5893 | 4749 | rbd_dev->rbd_client = rbdc; |
d147543d | 4750 | rbd_dev->spec = spec; |
0903e875 | 4751 | |
1643dfa4 ID |
4752 | return rbd_dev; |
4753 | } | |
4754 | ||
4755 | /* | |
4756 | * Create a mapping rbd_dev. | |
4757 | */ | |
4758 | static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc, | |
4759 | struct rbd_spec *spec, | |
4760 | struct rbd_options *opts) | |
4761 | { | |
4762 | struct rbd_device *rbd_dev; | |
4763 | ||
4764 | rbd_dev = __rbd_dev_create(rbdc, spec); | |
4765 | if (!rbd_dev) | |
4766 | return NULL; | |
4767 | ||
4768 | rbd_dev->opts = opts; | |
4769 | ||
4770 | /* get an id and fill in device name */ | |
4771 | rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0, | |
4772 | minor_to_rbd_dev_id(1 << MINORBITS), | |
4773 | GFP_KERNEL); | |
4774 | if (rbd_dev->dev_id < 0) | |
4775 | goto fail_rbd_dev; | |
4776 | ||
4777 | sprintf(rbd_dev->name, RBD_DRV_NAME "%d", rbd_dev->dev_id); | |
4778 | rbd_dev->task_wq = alloc_ordered_workqueue("%s-tasks", WQ_MEM_RECLAIM, | |
4779 | rbd_dev->name); | |
4780 | if (!rbd_dev->task_wq) | |
4781 | goto fail_dev_id; | |
dd5ac32d | 4782 | |
1643dfa4 ID |
4783 | /* we have a ref from do_rbd_add() */ |
4784 | __module_get(THIS_MODULE); | |
dd5ac32d | 4785 | |
1643dfa4 | 4786 | dout("%s rbd_dev %p dev_id %d\n", __func__, rbd_dev, rbd_dev->dev_id); |
c53d5893 | 4787 | return rbd_dev; |
1643dfa4 ID |
4788 | |
4789 | fail_dev_id: | |
4790 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); | |
4791 | fail_rbd_dev: | |
4792 | rbd_dev_free(rbd_dev); | |
4793 | return NULL; | |
c53d5893 AE |
4794 | } |
4795 | ||
4796 | static void rbd_dev_destroy(struct rbd_device *rbd_dev) | |
4797 | { | |
dd5ac32d ID |
4798 | if (rbd_dev) |
4799 | put_device(&rbd_dev->dev); | |
c53d5893 AE |
4800 | } |
4801 | ||
9d475de5 AE |
4802 | /* |
4803 | * Get the size and object order for an image snapshot, or if | |
4804 | * snap_id is CEPH_NOSNAP, gets this information for the base | |
4805 | * image. | |
4806 | */ | |
4807 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, | |
4808 | u8 *order, u64 *snap_size) | |
4809 | { | |
4810 | __le64 snapid = cpu_to_le64(snap_id); | |
4811 | int ret; | |
4812 | struct { | |
4813 | u8 order; | |
4814 | __le64 size; | |
4815 | } __attribute__ ((packed)) size_buf = { 0 }; | |
4816 | ||
ecd4a68a ID |
4817 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
4818 | &rbd_dev->header_oloc, "get_size", | |
4819 | &snapid, sizeof(snapid), | |
4820 | &size_buf, sizeof(size_buf)); | |
36be9a76 | 4821 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
9d475de5 AE |
4822 | if (ret < 0) |
4823 | return ret; | |
57385b51 AE |
4824 | if (ret < sizeof (size_buf)) |
4825 | return -ERANGE; | |
9d475de5 | 4826 | |
c3545579 | 4827 | if (order) { |
c86f86e9 | 4828 | *order = size_buf.order; |
c3545579 JD |
4829 | dout(" order %u", (unsigned int)*order); |
4830 | } | |
9d475de5 AE |
4831 | *snap_size = le64_to_cpu(size_buf.size); |
4832 | ||
c3545579 JD |
4833 | dout(" snap_id 0x%016llx snap_size = %llu\n", |
4834 | (unsigned long long)snap_id, | |
57385b51 | 4835 | (unsigned long long)*snap_size); |
9d475de5 AE |
4836 | |
4837 | return 0; | |
4838 | } | |
4839 | ||
4840 | static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev) | |
4841 | { | |
4842 | return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP, | |
4843 | &rbd_dev->header.obj_order, | |
4844 | &rbd_dev->header.image_size); | |
4845 | } | |
4846 | ||
1e130199 AE |
4847 | static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev) |
4848 | { | |
4849 | void *reply_buf; | |
4850 | int ret; | |
4851 | void *p; | |
4852 | ||
4853 | reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL); | |
4854 | if (!reply_buf) | |
4855 | return -ENOMEM; | |
4856 | ||
ecd4a68a ID |
4857 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
4858 | &rbd_dev->header_oloc, "get_object_prefix", | |
4859 | NULL, 0, reply_buf, RBD_OBJ_PREFIX_LEN_MAX); | |
36be9a76 | 4860 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
1e130199 AE |
4861 | if (ret < 0) |
4862 | goto out; | |
4863 | ||
4864 | p = reply_buf; | |
4865 | rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p, | |
57385b51 AE |
4866 | p + ret, NULL, GFP_NOIO); |
4867 | ret = 0; | |
1e130199 AE |
4868 | |
4869 | if (IS_ERR(rbd_dev->header.object_prefix)) { | |
4870 | ret = PTR_ERR(rbd_dev->header.object_prefix); | |
4871 | rbd_dev->header.object_prefix = NULL; | |
4872 | } else { | |
4873 | dout(" object_prefix = %s\n", rbd_dev->header.object_prefix); | |
4874 | } | |
1e130199 AE |
4875 | out: |
4876 | kfree(reply_buf); | |
4877 | ||
4878 | return ret; | |
4879 | } | |
4880 | ||
b1b5402a AE |
4881 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
4882 | u64 *snap_features) | |
4883 | { | |
4884 | __le64 snapid = cpu_to_le64(snap_id); | |
4885 | struct { | |
4886 | __le64 features; | |
4887 | __le64 incompat; | |
4157976b | 4888 | } __attribute__ ((packed)) features_buf = { 0 }; |
d3767f0f | 4889 | u64 unsup; |
b1b5402a AE |
4890 | int ret; |
4891 | ||
ecd4a68a ID |
4892 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
4893 | &rbd_dev->header_oloc, "get_features", | |
4894 | &snapid, sizeof(snapid), | |
4895 | &features_buf, sizeof(features_buf)); | |
36be9a76 | 4896 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
b1b5402a AE |
4897 | if (ret < 0) |
4898 | return ret; | |
57385b51 AE |
4899 | if (ret < sizeof (features_buf)) |
4900 | return -ERANGE; | |
d889140c | 4901 | |
d3767f0f ID |
4902 | unsup = le64_to_cpu(features_buf.incompat) & ~RBD_FEATURES_SUPPORTED; |
4903 | if (unsup) { | |
4904 | rbd_warn(rbd_dev, "image uses unsupported features: 0x%llx", | |
4905 | unsup); | |
b8f5c6ed | 4906 | return -ENXIO; |
d3767f0f | 4907 | } |
d889140c | 4908 | |
b1b5402a AE |
4909 | *snap_features = le64_to_cpu(features_buf.features); |
4910 | ||
4911 | dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n", | |
57385b51 AE |
4912 | (unsigned long long)snap_id, |
4913 | (unsigned long long)*snap_features, | |
4914 | (unsigned long long)le64_to_cpu(features_buf.incompat)); | |
b1b5402a AE |
4915 | |
4916 | return 0; | |
4917 | } | |
4918 | ||
4919 | static int rbd_dev_v2_features(struct rbd_device *rbd_dev) | |
4920 | { | |
4921 | return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP, | |
4922 | &rbd_dev->header.features); | |
4923 | } | |
4924 | ||
86b00e0d AE |
4925 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev) |
4926 | { | |
4927 | struct rbd_spec *parent_spec; | |
4928 | size_t size; | |
4929 | void *reply_buf = NULL; | |
4930 | __le64 snapid; | |
4931 | void *p; | |
4932 | void *end; | |
642a2537 | 4933 | u64 pool_id; |
86b00e0d | 4934 | char *image_id; |
3b5cf2a2 | 4935 | u64 snap_id; |
86b00e0d | 4936 | u64 overlap; |
86b00e0d AE |
4937 | int ret; |
4938 | ||
4939 | parent_spec = rbd_spec_alloc(); | |
4940 | if (!parent_spec) | |
4941 | return -ENOMEM; | |
4942 | ||
4943 | size = sizeof (__le64) + /* pool_id */ | |
4944 | sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */ | |
4945 | sizeof (__le64) + /* snap_id */ | |
4946 | sizeof (__le64); /* overlap */ | |
4947 | reply_buf = kmalloc(size, GFP_KERNEL); | |
4948 | if (!reply_buf) { | |
4949 | ret = -ENOMEM; | |
4950 | goto out_err; | |
4951 | } | |
4952 | ||
4d9b67cd | 4953 | snapid = cpu_to_le64(rbd_dev->spec->snap_id); |
ecd4a68a ID |
4954 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
4955 | &rbd_dev->header_oloc, "get_parent", | |
4956 | &snapid, sizeof(snapid), reply_buf, size); | |
36be9a76 | 4957 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
86b00e0d AE |
4958 | if (ret < 0) |
4959 | goto out_err; | |
4960 | ||
86b00e0d | 4961 | p = reply_buf; |
57385b51 AE |
4962 | end = reply_buf + ret; |
4963 | ret = -ERANGE; | |
642a2537 | 4964 | ceph_decode_64_safe(&p, end, pool_id, out_err); |
392a9dad AE |
4965 | if (pool_id == CEPH_NOPOOL) { |
4966 | /* | |
4967 | * Either the parent never existed, or we have | |
4968 | * record of it but the image got flattened so it no | |
4969 | * longer has a parent. When the parent of a | |
4970 | * layered image disappears we immediately set the | |
4971 | * overlap to 0. The effect of this is that all new | |
4972 | * requests will be treated as if the image had no | |
4973 | * parent. | |
4974 | */ | |
4975 | if (rbd_dev->parent_overlap) { | |
4976 | rbd_dev->parent_overlap = 0; | |
392a9dad AE |
4977 | rbd_dev_parent_put(rbd_dev); |
4978 | pr_info("%s: clone image has been flattened\n", | |
4979 | rbd_dev->disk->disk_name); | |
4980 | } | |
4981 | ||
86b00e0d | 4982 | goto out; /* No parent? No problem. */ |
392a9dad | 4983 | } |
86b00e0d | 4984 | |
0903e875 AE |
4985 | /* The ceph file layout needs to fit pool id in 32 bits */ |
4986 | ||
4987 | ret = -EIO; | |
642a2537 | 4988 | if (pool_id > (u64)U32_MAX) { |
9584d508 | 4989 | rbd_warn(NULL, "parent pool id too large (%llu > %u)", |
642a2537 | 4990 | (unsigned long long)pool_id, U32_MAX); |
57385b51 | 4991 | goto out_err; |
c0cd10db | 4992 | } |
0903e875 | 4993 | |
979ed480 | 4994 | image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
86b00e0d AE |
4995 | if (IS_ERR(image_id)) { |
4996 | ret = PTR_ERR(image_id); | |
4997 | goto out_err; | |
4998 | } | |
3b5cf2a2 | 4999 | ceph_decode_64_safe(&p, end, snap_id, out_err); |
86b00e0d AE |
5000 | ceph_decode_64_safe(&p, end, overlap, out_err); |
5001 | ||
3b5cf2a2 AE |
5002 | /* |
5003 | * The parent won't change (except when the clone is | |
5004 | * flattened, already handled that). So we only need to | |
5005 | * record the parent spec we have not already done so. | |
5006 | */ | |
5007 | if (!rbd_dev->parent_spec) { | |
5008 | parent_spec->pool_id = pool_id; | |
5009 | parent_spec->image_id = image_id; | |
5010 | parent_spec->snap_id = snap_id; | |
70cf49cf AE |
5011 | rbd_dev->parent_spec = parent_spec; |
5012 | parent_spec = NULL; /* rbd_dev now owns this */ | |
fbba11b3 ID |
5013 | } else { |
5014 | kfree(image_id); | |
3b5cf2a2 AE |
5015 | } |
5016 | ||
5017 | /* | |
cf32bd9c ID |
5018 | * We always update the parent overlap. If it's zero we issue |
5019 | * a warning, as we will proceed as if there was no parent. | |
3b5cf2a2 | 5020 | */ |
3b5cf2a2 | 5021 | if (!overlap) { |
3b5cf2a2 | 5022 | if (parent_spec) { |
cf32bd9c ID |
5023 | /* refresh, careful to warn just once */ |
5024 | if (rbd_dev->parent_overlap) | |
5025 | rbd_warn(rbd_dev, | |
5026 | "clone now standalone (overlap became 0)"); | |
3b5cf2a2 | 5027 | } else { |
cf32bd9c ID |
5028 | /* initial probe */ |
5029 | rbd_warn(rbd_dev, "clone is standalone (overlap 0)"); | |
3b5cf2a2 | 5030 | } |
70cf49cf | 5031 | } |
cf32bd9c ID |
5032 | rbd_dev->parent_overlap = overlap; |
5033 | ||
86b00e0d AE |
5034 | out: |
5035 | ret = 0; | |
5036 | out_err: | |
5037 | kfree(reply_buf); | |
5038 | rbd_spec_put(parent_spec); | |
5039 | ||
5040 | return ret; | |
5041 | } | |
5042 | ||
cc070d59 AE |
5043 | static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev) |
5044 | { | |
5045 | struct { | |
5046 | __le64 stripe_unit; | |
5047 | __le64 stripe_count; | |
5048 | } __attribute__ ((packed)) striping_info_buf = { 0 }; | |
5049 | size_t size = sizeof (striping_info_buf); | |
5050 | void *p; | |
5051 | u64 obj_size; | |
5052 | u64 stripe_unit; | |
5053 | u64 stripe_count; | |
5054 | int ret; | |
5055 | ||
ecd4a68a ID |
5056 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
5057 | &rbd_dev->header_oloc, "get_stripe_unit_count", | |
5058 | NULL, 0, &striping_info_buf, size); | |
cc070d59 AE |
5059 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
5060 | if (ret < 0) | |
5061 | return ret; | |
5062 | if (ret < size) | |
5063 | return -ERANGE; | |
5064 | ||
5065 | /* | |
5066 | * We don't actually support the "fancy striping" feature | |
5067 | * (STRIPINGV2) yet, but if the striping sizes are the | |
5068 | * defaults the behavior is the same as before. So find | |
5069 | * out, and only fail if the image has non-default values. | |
5070 | */ | |
5071 | ret = -EINVAL; | |
5bc3fb17 | 5072 | obj_size = rbd_obj_bytes(&rbd_dev->header); |
cc070d59 AE |
5073 | p = &striping_info_buf; |
5074 | stripe_unit = ceph_decode_64(&p); | |
5075 | if (stripe_unit != obj_size) { | |
5076 | rbd_warn(rbd_dev, "unsupported stripe unit " | |
5077 | "(got %llu want %llu)", | |
5078 | stripe_unit, obj_size); | |
5079 | return -EINVAL; | |
5080 | } | |
5081 | stripe_count = ceph_decode_64(&p); | |
5082 | if (stripe_count != 1) { | |
5083 | rbd_warn(rbd_dev, "unsupported stripe count " | |
5084 | "(got %llu want 1)", stripe_count); | |
5085 | return -EINVAL; | |
5086 | } | |
500d0c0f AE |
5087 | rbd_dev->header.stripe_unit = stripe_unit; |
5088 | rbd_dev->header.stripe_count = stripe_count; | |
cc070d59 AE |
5089 | |
5090 | return 0; | |
5091 | } | |
5092 | ||
7e97332e ID |
5093 | static int rbd_dev_v2_data_pool(struct rbd_device *rbd_dev) |
5094 | { | |
5095 | __le64 data_pool_id; | |
5096 | int ret; | |
5097 | ||
5098 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, | |
5099 | &rbd_dev->header_oloc, "get_data_pool", | |
5100 | NULL, 0, &data_pool_id, sizeof(data_pool_id)); | |
5101 | if (ret < 0) | |
5102 | return ret; | |
5103 | if (ret < sizeof(data_pool_id)) | |
5104 | return -EBADMSG; | |
5105 | ||
5106 | rbd_dev->header.data_pool_id = le64_to_cpu(data_pool_id); | |
5107 | WARN_ON(rbd_dev->header.data_pool_id == CEPH_NOPOOL); | |
5108 | return 0; | |
5109 | } | |
5110 | ||
9e15b77d AE |
5111 | static char *rbd_dev_image_name(struct rbd_device *rbd_dev) |
5112 | { | |
ecd4a68a | 5113 | CEPH_DEFINE_OID_ONSTACK(oid); |
9e15b77d AE |
5114 | size_t image_id_size; |
5115 | char *image_id; | |
5116 | void *p; | |
5117 | void *end; | |
5118 | size_t size; | |
5119 | void *reply_buf = NULL; | |
5120 | size_t len = 0; | |
5121 | char *image_name = NULL; | |
5122 | int ret; | |
5123 | ||
5124 | rbd_assert(!rbd_dev->spec->image_name); | |
5125 | ||
69e7a02f AE |
5126 | len = strlen(rbd_dev->spec->image_id); |
5127 | image_id_size = sizeof (__le32) + len; | |
9e15b77d AE |
5128 | image_id = kmalloc(image_id_size, GFP_KERNEL); |
5129 | if (!image_id) | |
5130 | return NULL; | |
5131 | ||
5132 | p = image_id; | |
4157976b | 5133 | end = image_id + image_id_size; |
57385b51 | 5134 | ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len); |
9e15b77d AE |
5135 | |
5136 | size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX; | |
5137 | reply_buf = kmalloc(size, GFP_KERNEL); | |
5138 | if (!reply_buf) | |
5139 | goto out; | |
5140 | ||
ecd4a68a ID |
5141 | ceph_oid_printf(&oid, "%s", RBD_DIRECTORY); |
5142 | ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc, | |
5143 | "dir_get_name", image_id, image_id_size, | |
5144 | reply_buf, size); | |
9e15b77d AE |
5145 | if (ret < 0) |
5146 | goto out; | |
5147 | p = reply_buf; | |
f40eb349 AE |
5148 | end = reply_buf + ret; |
5149 | ||
9e15b77d AE |
5150 | image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL); |
5151 | if (IS_ERR(image_name)) | |
5152 | image_name = NULL; | |
5153 | else | |
5154 | dout("%s: name is %s len is %zd\n", __func__, image_name, len); | |
5155 | out: | |
5156 | kfree(reply_buf); | |
5157 | kfree(image_id); | |
5158 | ||
5159 | return image_name; | |
5160 | } | |
5161 | ||
2ad3d716 AE |
5162 | static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
5163 | { | |
5164 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; | |
5165 | const char *snap_name; | |
5166 | u32 which = 0; | |
5167 | ||
5168 | /* Skip over names until we find the one we are looking for */ | |
5169 | ||
5170 | snap_name = rbd_dev->header.snap_names; | |
5171 | while (which < snapc->num_snaps) { | |
5172 | if (!strcmp(name, snap_name)) | |
5173 | return snapc->snaps[which]; | |
5174 | snap_name += strlen(snap_name) + 1; | |
5175 | which++; | |
5176 | } | |
5177 | return CEPH_NOSNAP; | |
5178 | } | |
5179 | ||
5180 | static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) | |
5181 | { | |
5182 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; | |
5183 | u32 which; | |
5184 | bool found = false; | |
5185 | u64 snap_id; | |
5186 | ||
5187 | for (which = 0; !found && which < snapc->num_snaps; which++) { | |
5188 | const char *snap_name; | |
5189 | ||
5190 | snap_id = snapc->snaps[which]; | |
5191 | snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id); | |
efadc98a JD |
5192 | if (IS_ERR(snap_name)) { |
5193 | /* ignore no-longer existing snapshots */ | |
5194 | if (PTR_ERR(snap_name) == -ENOENT) | |
5195 | continue; | |
5196 | else | |
5197 | break; | |
5198 | } | |
2ad3d716 AE |
5199 | found = !strcmp(name, snap_name); |
5200 | kfree(snap_name); | |
5201 | } | |
5202 | return found ? snap_id : CEPH_NOSNAP; | |
5203 | } | |
5204 | ||
5205 | /* | |
5206 | * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if | |
5207 | * no snapshot by that name is found, or if an error occurs. | |
5208 | */ | |
5209 | static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) | |
5210 | { | |
5211 | if (rbd_dev->image_format == 1) | |
5212 | return rbd_v1_snap_id_by_name(rbd_dev, name); | |
5213 | ||
5214 | return rbd_v2_snap_id_by_name(rbd_dev, name); | |
5215 | } | |
5216 | ||
9e15b77d | 5217 | /* |
04077599 ID |
5218 | * An image being mapped will have everything but the snap id. |
5219 | */ | |
5220 | static int rbd_spec_fill_snap_id(struct rbd_device *rbd_dev) | |
5221 | { | |
5222 | struct rbd_spec *spec = rbd_dev->spec; | |
5223 | ||
5224 | rbd_assert(spec->pool_id != CEPH_NOPOOL && spec->pool_name); | |
5225 | rbd_assert(spec->image_id && spec->image_name); | |
5226 | rbd_assert(spec->snap_name); | |
5227 | ||
5228 | if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) { | |
5229 | u64 snap_id; | |
5230 | ||
5231 | snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name); | |
5232 | if (snap_id == CEPH_NOSNAP) | |
5233 | return -ENOENT; | |
5234 | ||
5235 | spec->snap_id = snap_id; | |
5236 | } else { | |
5237 | spec->snap_id = CEPH_NOSNAP; | |
5238 | } | |
5239 | ||
5240 | return 0; | |
5241 | } | |
5242 | ||
5243 | /* | |
5244 | * A parent image will have all ids but none of the names. | |
e1d4213f | 5245 | * |
04077599 ID |
5246 | * All names in an rbd spec are dynamically allocated. It's OK if we |
5247 | * can't figure out the name for an image id. | |
9e15b77d | 5248 | */ |
04077599 | 5249 | static int rbd_spec_fill_names(struct rbd_device *rbd_dev) |
9e15b77d | 5250 | { |
2e9f7f1c AE |
5251 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
5252 | struct rbd_spec *spec = rbd_dev->spec; | |
5253 | const char *pool_name; | |
5254 | const char *image_name; | |
5255 | const char *snap_name; | |
9e15b77d AE |
5256 | int ret; |
5257 | ||
04077599 ID |
5258 | rbd_assert(spec->pool_id != CEPH_NOPOOL); |
5259 | rbd_assert(spec->image_id); | |
5260 | rbd_assert(spec->snap_id != CEPH_NOSNAP); | |
9e15b77d | 5261 | |
2e9f7f1c | 5262 | /* Get the pool name; we have to make our own copy of this */ |
9e15b77d | 5263 | |
2e9f7f1c AE |
5264 | pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id); |
5265 | if (!pool_name) { | |
5266 | rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id); | |
935dc89f AE |
5267 | return -EIO; |
5268 | } | |
2e9f7f1c AE |
5269 | pool_name = kstrdup(pool_name, GFP_KERNEL); |
5270 | if (!pool_name) | |
9e15b77d AE |
5271 | return -ENOMEM; |
5272 | ||
5273 | /* Fetch the image name; tolerate failure here */ | |
5274 | ||
2e9f7f1c AE |
5275 | image_name = rbd_dev_image_name(rbd_dev); |
5276 | if (!image_name) | |
06ecc6cb | 5277 | rbd_warn(rbd_dev, "unable to get image name"); |
9e15b77d | 5278 | |
04077599 | 5279 | /* Fetch the snapshot name */ |
9e15b77d | 5280 | |
2e9f7f1c | 5281 | snap_name = rbd_snap_name(rbd_dev, spec->snap_id); |
da6a6b63 JD |
5282 | if (IS_ERR(snap_name)) { |
5283 | ret = PTR_ERR(snap_name); | |
9e15b77d | 5284 | goto out_err; |
2e9f7f1c AE |
5285 | } |
5286 | ||
5287 | spec->pool_name = pool_name; | |
5288 | spec->image_name = image_name; | |
5289 | spec->snap_name = snap_name; | |
9e15b77d AE |
5290 | |
5291 | return 0; | |
04077599 | 5292 | |
9e15b77d | 5293 | out_err: |
2e9f7f1c AE |
5294 | kfree(image_name); |
5295 | kfree(pool_name); | |
9e15b77d AE |
5296 | return ret; |
5297 | } | |
5298 | ||
cc4a38bd | 5299 | static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev) |
35d489f9 AE |
5300 | { |
5301 | size_t size; | |
5302 | int ret; | |
5303 | void *reply_buf; | |
5304 | void *p; | |
5305 | void *end; | |
5306 | u64 seq; | |
5307 | u32 snap_count; | |
5308 | struct ceph_snap_context *snapc; | |
5309 | u32 i; | |
5310 | ||
5311 | /* | |
5312 | * We'll need room for the seq value (maximum snapshot id), | |
5313 | * snapshot count, and array of that many snapshot ids. | |
5314 | * For now we have a fixed upper limit on the number we're | |
5315 | * prepared to receive. | |
5316 | */ | |
5317 | size = sizeof (__le64) + sizeof (__le32) + | |
5318 | RBD_MAX_SNAP_COUNT * sizeof (__le64); | |
5319 | reply_buf = kzalloc(size, GFP_KERNEL); | |
5320 | if (!reply_buf) | |
5321 | return -ENOMEM; | |
5322 | ||
ecd4a68a ID |
5323 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
5324 | &rbd_dev->header_oloc, "get_snapcontext", | |
5325 | NULL, 0, reply_buf, size); | |
36be9a76 | 5326 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
35d489f9 AE |
5327 | if (ret < 0) |
5328 | goto out; | |
5329 | ||
35d489f9 | 5330 | p = reply_buf; |
57385b51 AE |
5331 | end = reply_buf + ret; |
5332 | ret = -ERANGE; | |
35d489f9 AE |
5333 | ceph_decode_64_safe(&p, end, seq, out); |
5334 | ceph_decode_32_safe(&p, end, snap_count, out); | |
5335 | ||
5336 | /* | |
5337 | * Make sure the reported number of snapshot ids wouldn't go | |
5338 | * beyond the end of our buffer. But before checking that, | |
5339 | * make sure the computed size of the snapshot context we | |
5340 | * allocate is representable in a size_t. | |
5341 | */ | |
5342 | if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context)) | |
5343 | / sizeof (u64)) { | |
5344 | ret = -EINVAL; | |
5345 | goto out; | |
5346 | } | |
5347 | if (!ceph_has_room(&p, end, snap_count * sizeof (__le64))) | |
5348 | goto out; | |
468521c1 | 5349 | ret = 0; |
35d489f9 | 5350 | |
812164f8 | 5351 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); |
35d489f9 AE |
5352 | if (!snapc) { |
5353 | ret = -ENOMEM; | |
5354 | goto out; | |
5355 | } | |
35d489f9 | 5356 | snapc->seq = seq; |
35d489f9 AE |
5357 | for (i = 0; i < snap_count; i++) |
5358 | snapc->snaps[i] = ceph_decode_64(&p); | |
5359 | ||
49ece554 | 5360 | ceph_put_snap_context(rbd_dev->header.snapc); |
35d489f9 AE |
5361 | rbd_dev->header.snapc = snapc; |
5362 | ||
5363 | dout(" snap context seq = %llu, snap_count = %u\n", | |
57385b51 | 5364 | (unsigned long long)seq, (unsigned int)snap_count); |
35d489f9 AE |
5365 | out: |
5366 | kfree(reply_buf); | |
5367 | ||
57385b51 | 5368 | return ret; |
35d489f9 AE |
5369 | } |
5370 | ||
54cac61f AE |
5371 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
5372 | u64 snap_id) | |
b8b1e2db AE |
5373 | { |
5374 | size_t size; | |
5375 | void *reply_buf; | |
54cac61f | 5376 | __le64 snapid; |
b8b1e2db AE |
5377 | int ret; |
5378 | void *p; | |
5379 | void *end; | |
b8b1e2db AE |
5380 | char *snap_name; |
5381 | ||
5382 | size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN; | |
5383 | reply_buf = kmalloc(size, GFP_KERNEL); | |
5384 | if (!reply_buf) | |
5385 | return ERR_PTR(-ENOMEM); | |
5386 | ||
54cac61f | 5387 | snapid = cpu_to_le64(snap_id); |
ecd4a68a ID |
5388 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
5389 | &rbd_dev->header_oloc, "get_snapshot_name", | |
5390 | &snapid, sizeof(snapid), reply_buf, size); | |
36be9a76 | 5391 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
f40eb349 AE |
5392 | if (ret < 0) { |
5393 | snap_name = ERR_PTR(ret); | |
b8b1e2db | 5394 | goto out; |
f40eb349 | 5395 | } |
b8b1e2db AE |
5396 | |
5397 | p = reply_buf; | |
f40eb349 | 5398 | end = reply_buf + ret; |
e5c35534 | 5399 | snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
f40eb349 | 5400 | if (IS_ERR(snap_name)) |
b8b1e2db | 5401 | goto out; |
b8b1e2db | 5402 | |
f40eb349 | 5403 | dout(" snap_id 0x%016llx snap_name = %s\n", |
54cac61f | 5404 | (unsigned long long)snap_id, snap_name); |
b8b1e2db AE |
5405 | out: |
5406 | kfree(reply_buf); | |
5407 | ||
f40eb349 | 5408 | return snap_name; |
b8b1e2db AE |
5409 | } |
5410 | ||
2df3fac7 | 5411 | static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev) |
117973fb | 5412 | { |
2df3fac7 | 5413 | bool first_time = rbd_dev->header.object_prefix == NULL; |
117973fb | 5414 | int ret; |
117973fb | 5415 | |
1617e40c JD |
5416 | ret = rbd_dev_v2_image_size(rbd_dev); |
5417 | if (ret) | |
cfbf6377 | 5418 | return ret; |
1617e40c | 5419 | |
2df3fac7 AE |
5420 | if (first_time) { |
5421 | ret = rbd_dev_v2_header_onetime(rbd_dev); | |
5422 | if (ret) | |
cfbf6377 | 5423 | return ret; |
2df3fac7 AE |
5424 | } |
5425 | ||
cc4a38bd | 5426 | ret = rbd_dev_v2_snap_context(rbd_dev); |
d194cd1d ID |
5427 | if (ret && first_time) { |
5428 | kfree(rbd_dev->header.object_prefix); | |
5429 | rbd_dev->header.object_prefix = NULL; | |
5430 | } | |
117973fb AE |
5431 | |
5432 | return ret; | |
5433 | } | |
5434 | ||
a720ae09 ID |
5435 | static int rbd_dev_header_info(struct rbd_device *rbd_dev) |
5436 | { | |
5437 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); | |
5438 | ||
5439 | if (rbd_dev->image_format == 1) | |
5440 | return rbd_dev_v1_header_info(rbd_dev); | |
5441 | ||
5442 | return rbd_dev_v2_header_info(rbd_dev); | |
5443 | } | |
5444 | ||
e28fff26 AE |
5445 | /* |
5446 | * Skips over white space at *buf, and updates *buf to point to the | |
5447 | * first found non-space character (if any). Returns the length of | |
593a9e7b AE |
5448 | * the token (string of non-white space characters) found. Note |
5449 | * that *buf must be terminated with '\0'. | |
e28fff26 AE |
5450 | */ |
5451 | static inline size_t next_token(const char **buf) | |
5452 | { | |
5453 | /* | |
5454 | * These are the characters that produce nonzero for | |
5455 | * isspace() in the "C" and "POSIX" locales. | |
5456 | */ | |
5457 | const char *spaces = " \f\n\r\t\v"; | |
5458 | ||
5459 | *buf += strspn(*buf, spaces); /* Find start of token */ | |
5460 | ||
5461 | return strcspn(*buf, spaces); /* Return token length */ | |
5462 | } | |
5463 | ||
ea3352f4 AE |
5464 | /* |
5465 | * Finds the next token in *buf, dynamically allocates a buffer big | |
5466 | * enough to hold a copy of it, and copies the token into the new | |
5467 | * buffer. The copy is guaranteed to be terminated with '\0'. Note | |
5468 | * that a duplicate buffer is created even for a zero-length token. | |
5469 | * | |
5470 | * Returns a pointer to the newly-allocated duplicate, or a null | |
5471 | * pointer if memory for the duplicate was not available. If | |
5472 | * the lenp argument is a non-null pointer, the length of the token | |
5473 | * (not including the '\0') is returned in *lenp. | |
5474 | * | |
5475 | * If successful, the *buf pointer will be updated to point beyond | |
5476 | * the end of the found token. | |
5477 | * | |
5478 | * Note: uses GFP_KERNEL for allocation. | |
5479 | */ | |
5480 | static inline char *dup_token(const char **buf, size_t *lenp) | |
5481 | { | |
5482 | char *dup; | |
5483 | size_t len; | |
5484 | ||
5485 | len = next_token(buf); | |
4caf35f9 | 5486 | dup = kmemdup(*buf, len + 1, GFP_KERNEL); |
ea3352f4 AE |
5487 | if (!dup) |
5488 | return NULL; | |
ea3352f4 AE |
5489 | *(dup + len) = '\0'; |
5490 | *buf += len; | |
5491 | ||
5492 | if (lenp) | |
5493 | *lenp = len; | |
5494 | ||
5495 | return dup; | |
5496 | } | |
5497 | ||
a725f65e | 5498 | /* |
859c31df AE |
5499 | * Parse the options provided for an "rbd add" (i.e., rbd image |
5500 | * mapping) request. These arrive via a write to /sys/bus/rbd/add, | |
5501 | * and the data written is passed here via a NUL-terminated buffer. | |
5502 | * Returns 0 if successful or an error code otherwise. | |
d22f76e7 | 5503 | * |
859c31df AE |
5504 | * The information extracted from these options is recorded in |
5505 | * the other parameters which return dynamically-allocated | |
5506 | * structures: | |
5507 | * ceph_opts | |
5508 | * The address of a pointer that will refer to a ceph options | |
5509 | * structure. Caller must release the returned pointer using | |
5510 | * ceph_destroy_options() when it is no longer needed. | |
5511 | * rbd_opts | |
5512 | * Address of an rbd options pointer. Fully initialized by | |
5513 | * this function; caller must release with kfree(). | |
5514 | * spec | |
5515 | * Address of an rbd image specification pointer. Fully | |
5516 | * initialized by this function based on parsed options. | |
5517 | * Caller must release with rbd_spec_put(). | |
5518 | * | |
5519 | * The options passed take this form: | |
5520 | * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>] | |
5521 | * where: | |
5522 | * <mon_addrs> | |
5523 | * A comma-separated list of one or more monitor addresses. | |
5524 | * A monitor address is an ip address, optionally followed | |
5525 | * by a port number (separated by a colon). | |
5526 | * I.e.: ip1[:port1][,ip2[:port2]...] | |
5527 | * <options> | |
5528 | * A comma-separated list of ceph and/or rbd options. | |
5529 | * <pool_name> | |
5530 | * The name of the rados pool containing the rbd image. | |
5531 | * <image_name> | |
5532 | * The name of the image in that pool to map. | |
5533 | * <snap_id> | |
5534 | * An optional snapshot id. If provided, the mapping will | |
5535 | * present data from the image at the time that snapshot was | |
5536 | * created. The image head is used if no snapshot id is | |
5537 | * provided. Snapshot mappings are always read-only. | |
a725f65e | 5538 | */ |
859c31df | 5539 | static int rbd_add_parse_args(const char *buf, |
dc79b113 | 5540 | struct ceph_options **ceph_opts, |
859c31df AE |
5541 | struct rbd_options **opts, |
5542 | struct rbd_spec **rbd_spec) | |
e28fff26 | 5543 | { |
d22f76e7 | 5544 | size_t len; |
859c31df | 5545 | char *options; |
0ddebc0c | 5546 | const char *mon_addrs; |
ecb4dc22 | 5547 | char *snap_name; |
0ddebc0c | 5548 | size_t mon_addrs_size; |
859c31df | 5549 | struct rbd_spec *spec = NULL; |
4e9afeba | 5550 | struct rbd_options *rbd_opts = NULL; |
859c31df | 5551 | struct ceph_options *copts; |
dc79b113 | 5552 | int ret; |
e28fff26 AE |
5553 | |
5554 | /* The first four tokens are required */ | |
5555 | ||
7ef3214a | 5556 | len = next_token(&buf); |
4fb5d671 AE |
5557 | if (!len) { |
5558 | rbd_warn(NULL, "no monitor address(es) provided"); | |
5559 | return -EINVAL; | |
5560 | } | |
0ddebc0c | 5561 | mon_addrs = buf; |
f28e565a | 5562 | mon_addrs_size = len + 1; |
7ef3214a | 5563 | buf += len; |
a725f65e | 5564 | |
dc79b113 | 5565 | ret = -EINVAL; |
f28e565a AE |
5566 | options = dup_token(&buf, NULL); |
5567 | if (!options) | |
dc79b113 | 5568 | return -ENOMEM; |
4fb5d671 AE |
5569 | if (!*options) { |
5570 | rbd_warn(NULL, "no options provided"); | |
5571 | goto out_err; | |
5572 | } | |
e28fff26 | 5573 | |
859c31df AE |
5574 | spec = rbd_spec_alloc(); |
5575 | if (!spec) | |
f28e565a | 5576 | goto out_mem; |
859c31df AE |
5577 | |
5578 | spec->pool_name = dup_token(&buf, NULL); | |
5579 | if (!spec->pool_name) | |
5580 | goto out_mem; | |
4fb5d671 AE |
5581 | if (!*spec->pool_name) { |
5582 | rbd_warn(NULL, "no pool name provided"); | |
5583 | goto out_err; | |
5584 | } | |
e28fff26 | 5585 | |
69e7a02f | 5586 | spec->image_name = dup_token(&buf, NULL); |
859c31df | 5587 | if (!spec->image_name) |
f28e565a | 5588 | goto out_mem; |
4fb5d671 AE |
5589 | if (!*spec->image_name) { |
5590 | rbd_warn(NULL, "no image name provided"); | |
5591 | goto out_err; | |
5592 | } | |
d4b125e9 | 5593 | |
f28e565a AE |
5594 | /* |
5595 | * Snapshot name is optional; default is to use "-" | |
5596 | * (indicating the head/no snapshot). | |
5597 | */ | |
3feeb894 | 5598 | len = next_token(&buf); |
820a5f3e | 5599 | if (!len) { |
3feeb894 AE |
5600 | buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */ |
5601 | len = sizeof (RBD_SNAP_HEAD_NAME) - 1; | |
f28e565a | 5602 | } else if (len > RBD_MAX_SNAP_NAME_LEN) { |
dc79b113 | 5603 | ret = -ENAMETOOLONG; |
f28e565a | 5604 | goto out_err; |
849b4260 | 5605 | } |
ecb4dc22 AE |
5606 | snap_name = kmemdup(buf, len + 1, GFP_KERNEL); |
5607 | if (!snap_name) | |
f28e565a | 5608 | goto out_mem; |
ecb4dc22 AE |
5609 | *(snap_name + len) = '\0'; |
5610 | spec->snap_name = snap_name; | |
e5c35534 | 5611 | |
0ddebc0c | 5612 | /* Initialize all rbd options to the defaults */ |
e28fff26 | 5613 | |
4e9afeba AE |
5614 | rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL); |
5615 | if (!rbd_opts) | |
5616 | goto out_mem; | |
5617 | ||
5618 | rbd_opts->read_only = RBD_READ_ONLY_DEFAULT; | |
b5584180 | 5619 | rbd_opts->queue_depth = RBD_QUEUE_DEPTH_DEFAULT; |
80de1912 | 5620 | rbd_opts->lock_on_read = RBD_LOCK_ON_READ_DEFAULT; |
d22f76e7 | 5621 | |
859c31df | 5622 | copts = ceph_parse_options(options, mon_addrs, |
0ddebc0c | 5623 | mon_addrs + mon_addrs_size - 1, |
4e9afeba | 5624 | parse_rbd_opts_token, rbd_opts); |
859c31df AE |
5625 | if (IS_ERR(copts)) { |
5626 | ret = PTR_ERR(copts); | |
dc79b113 AE |
5627 | goto out_err; |
5628 | } | |
859c31df AE |
5629 | kfree(options); |
5630 | ||
5631 | *ceph_opts = copts; | |
4e9afeba | 5632 | *opts = rbd_opts; |
859c31df | 5633 | *rbd_spec = spec; |
0ddebc0c | 5634 | |
dc79b113 | 5635 | return 0; |
f28e565a | 5636 | out_mem: |
dc79b113 | 5637 | ret = -ENOMEM; |
d22f76e7 | 5638 | out_err: |
859c31df AE |
5639 | kfree(rbd_opts); |
5640 | rbd_spec_put(spec); | |
f28e565a | 5641 | kfree(options); |
d22f76e7 | 5642 | |
dc79b113 | 5643 | return ret; |
a725f65e AE |
5644 | } |
5645 | ||
30ba1f02 ID |
5646 | /* |
5647 | * Return pool id (>= 0) or a negative error code. | |
5648 | */ | |
5649 | static int rbd_add_get_pool_id(struct rbd_client *rbdc, const char *pool_name) | |
5650 | { | |
a319bf56 | 5651 | struct ceph_options *opts = rbdc->client->options; |
30ba1f02 | 5652 | u64 newest_epoch; |
30ba1f02 ID |
5653 | int tries = 0; |
5654 | int ret; | |
5655 | ||
5656 | again: | |
5657 | ret = ceph_pg_poolid_by_name(rbdc->client->osdc.osdmap, pool_name); | |
5658 | if (ret == -ENOENT && tries++ < 1) { | |
d0b19705 ID |
5659 | ret = ceph_monc_get_version(&rbdc->client->monc, "osdmap", |
5660 | &newest_epoch); | |
30ba1f02 ID |
5661 | if (ret < 0) |
5662 | return ret; | |
5663 | ||
5664 | if (rbdc->client->osdc.osdmap->epoch < newest_epoch) { | |
7cca78c9 | 5665 | ceph_osdc_maybe_request_map(&rbdc->client->osdc); |
30ba1f02 | 5666 | (void) ceph_monc_wait_osdmap(&rbdc->client->monc, |
a319bf56 ID |
5667 | newest_epoch, |
5668 | opts->mount_timeout); | |
30ba1f02 ID |
5669 | goto again; |
5670 | } else { | |
5671 | /* the osdmap we have is new enough */ | |
5672 | return -ENOENT; | |
5673 | } | |
5674 | } | |
5675 | ||
5676 | return ret; | |
5677 | } | |
5678 | ||
589d30e0 AE |
5679 | /* |
5680 | * An rbd format 2 image has a unique identifier, distinct from the | |
5681 | * name given to it by the user. Internally, that identifier is | |
5682 | * what's used to specify the names of objects related to the image. | |
5683 | * | |
5684 | * A special "rbd id" object is used to map an rbd image name to its | |
5685 | * id. If that object doesn't exist, then there is no v2 rbd image | |
5686 | * with the supplied name. | |
5687 | * | |
5688 | * This function will record the given rbd_dev's image_id field if | |
5689 | * it can be determined, and in that case will return 0. If any | |
5690 | * errors occur a negative errno will be returned and the rbd_dev's | |
5691 | * image_id field will be unchanged (and should be NULL). | |
5692 | */ | |
5693 | static int rbd_dev_image_id(struct rbd_device *rbd_dev) | |
5694 | { | |
5695 | int ret; | |
5696 | size_t size; | |
ecd4a68a | 5697 | CEPH_DEFINE_OID_ONSTACK(oid); |
589d30e0 | 5698 | void *response; |
c0fba368 | 5699 | char *image_id; |
2f82ee54 | 5700 | |
2c0d0a10 AE |
5701 | /* |
5702 | * When probing a parent image, the image id is already | |
5703 | * known (and the image name likely is not). There's no | |
c0fba368 AE |
5704 | * need to fetch the image id again in this case. We |
5705 | * do still need to set the image format though. | |
2c0d0a10 | 5706 | */ |
c0fba368 AE |
5707 | if (rbd_dev->spec->image_id) { |
5708 | rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1; | |
5709 | ||
2c0d0a10 | 5710 | return 0; |
c0fba368 | 5711 | } |
2c0d0a10 | 5712 | |
589d30e0 AE |
5713 | /* |
5714 | * First, see if the format 2 image id file exists, and if | |
5715 | * so, get the image's persistent id from it. | |
5716 | */ | |
ecd4a68a ID |
5717 | ret = ceph_oid_aprintf(&oid, GFP_KERNEL, "%s%s", RBD_ID_PREFIX, |
5718 | rbd_dev->spec->image_name); | |
5719 | if (ret) | |
5720 | return ret; | |
5721 | ||
5722 | dout("rbd id object name is %s\n", oid.name); | |
589d30e0 AE |
5723 | |
5724 | /* Response will be an encoded string, which includes a length */ | |
5725 | ||
5726 | size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX; | |
5727 | response = kzalloc(size, GFP_NOIO); | |
5728 | if (!response) { | |
5729 | ret = -ENOMEM; | |
5730 | goto out; | |
5731 | } | |
5732 | ||
c0fba368 AE |
5733 | /* If it doesn't exist we'll assume it's a format 1 image */ |
5734 | ||
ecd4a68a ID |
5735 | ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc, |
5736 | "get_id", NULL, 0, | |
5737 | response, RBD_IMAGE_ID_LEN_MAX); | |
36be9a76 | 5738 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
c0fba368 AE |
5739 | if (ret == -ENOENT) { |
5740 | image_id = kstrdup("", GFP_KERNEL); | |
5741 | ret = image_id ? 0 : -ENOMEM; | |
5742 | if (!ret) | |
5743 | rbd_dev->image_format = 1; | |
7dd440c9 | 5744 | } else if (ret >= 0) { |
c0fba368 AE |
5745 | void *p = response; |
5746 | ||
5747 | image_id = ceph_extract_encoded_string(&p, p + ret, | |
979ed480 | 5748 | NULL, GFP_NOIO); |
461f758a | 5749 | ret = PTR_ERR_OR_ZERO(image_id); |
c0fba368 AE |
5750 | if (!ret) |
5751 | rbd_dev->image_format = 2; | |
c0fba368 AE |
5752 | } |
5753 | ||
5754 | if (!ret) { | |
5755 | rbd_dev->spec->image_id = image_id; | |
5756 | dout("image_id is %s\n", image_id); | |
589d30e0 AE |
5757 | } |
5758 | out: | |
5759 | kfree(response); | |
ecd4a68a | 5760 | ceph_oid_destroy(&oid); |
589d30e0 AE |
5761 | return ret; |
5762 | } | |
5763 | ||
3abef3b3 AE |
5764 | /* |
5765 | * Undo whatever state changes are made by v1 or v2 header info | |
5766 | * call. | |
5767 | */ | |
6fd48b3b AE |
5768 | static void rbd_dev_unprobe(struct rbd_device *rbd_dev) |
5769 | { | |
5770 | struct rbd_image_header *header; | |
5771 | ||
e69b8d41 | 5772 | rbd_dev_parent_put(rbd_dev); |
6fd48b3b AE |
5773 | |
5774 | /* Free dynamic fields from the header, then zero it out */ | |
5775 | ||
5776 | header = &rbd_dev->header; | |
812164f8 | 5777 | ceph_put_snap_context(header->snapc); |
6fd48b3b AE |
5778 | kfree(header->snap_sizes); |
5779 | kfree(header->snap_names); | |
5780 | kfree(header->object_prefix); | |
5781 | memset(header, 0, sizeof (*header)); | |
5782 | } | |
5783 | ||
2df3fac7 | 5784 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev) |
a30b71b9 AE |
5785 | { |
5786 | int ret; | |
a30b71b9 | 5787 | |
1e130199 | 5788 | ret = rbd_dev_v2_object_prefix(rbd_dev); |
57385b51 | 5789 | if (ret) |
b1b5402a AE |
5790 | goto out_err; |
5791 | ||
2df3fac7 AE |
5792 | /* |
5793 | * Get the and check features for the image. Currently the | |
5794 | * features are assumed to never change. | |
5795 | */ | |
b1b5402a | 5796 | ret = rbd_dev_v2_features(rbd_dev); |
57385b51 | 5797 | if (ret) |
9d475de5 | 5798 | goto out_err; |
35d489f9 | 5799 | |
cc070d59 AE |
5800 | /* If the image supports fancy striping, get its parameters */ |
5801 | ||
5802 | if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) { | |
5803 | ret = rbd_dev_v2_striping_info(rbd_dev); | |
5804 | if (ret < 0) | |
5805 | goto out_err; | |
5806 | } | |
a30b71b9 | 5807 | |
7e97332e ID |
5808 | if (rbd_dev->header.features & RBD_FEATURE_DATA_POOL) { |
5809 | ret = rbd_dev_v2_data_pool(rbd_dev); | |
5810 | if (ret) | |
5811 | goto out_err; | |
5812 | } | |
5813 | ||
263423f8 | 5814 | rbd_init_layout(rbd_dev); |
35152979 | 5815 | return 0; |
263423f8 | 5816 | |
9d475de5 | 5817 | out_err: |
642a2537 | 5818 | rbd_dev->header.features = 0; |
1e130199 AE |
5819 | kfree(rbd_dev->header.object_prefix); |
5820 | rbd_dev->header.object_prefix = NULL; | |
9d475de5 | 5821 | return ret; |
a30b71b9 AE |
5822 | } |
5823 | ||
6d69bb53 ID |
5824 | /* |
5825 | * @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() -> | |
5826 | * rbd_dev_image_probe() recursion depth, which means it's also the | |
5827 | * length of the already discovered part of the parent chain. | |
5828 | */ | |
5829 | static int rbd_dev_probe_parent(struct rbd_device *rbd_dev, int depth) | |
83a06263 | 5830 | { |
2f82ee54 | 5831 | struct rbd_device *parent = NULL; |
124afba2 AE |
5832 | int ret; |
5833 | ||
5834 | if (!rbd_dev->parent_spec) | |
5835 | return 0; | |
124afba2 | 5836 | |
6d69bb53 ID |
5837 | if (++depth > RBD_MAX_PARENT_CHAIN_LEN) { |
5838 | pr_info("parent chain is too long (%d)\n", depth); | |
5839 | ret = -EINVAL; | |
5840 | goto out_err; | |
5841 | } | |
5842 | ||
1643dfa4 | 5843 | parent = __rbd_dev_create(rbd_dev->rbd_client, rbd_dev->parent_spec); |
1f2c6651 ID |
5844 | if (!parent) { |
5845 | ret = -ENOMEM; | |
124afba2 | 5846 | goto out_err; |
1f2c6651 ID |
5847 | } |
5848 | ||
5849 | /* | |
5850 | * Images related by parent/child relationships always share | |
5851 | * rbd_client and spec/parent_spec, so bump their refcounts. | |
5852 | */ | |
5853 | __rbd_get_client(rbd_dev->rbd_client); | |
5854 | rbd_spec_get(rbd_dev->parent_spec); | |
124afba2 | 5855 | |
6d69bb53 | 5856 | ret = rbd_dev_image_probe(parent, depth); |
124afba2 AE |
5857 | if (ret < 0) |
5858 | goto out_err; | |
1f2c6651 | 5859 | |
124afba2 | 5860 | rbd_dev->parent = parent; |
a2acd00e | 5861 | atomic_set(&rbd_dev->parent_ref, 1); |
124afba2 | 5862 | return 0; |
1f2c6651 | 5863 | |
124afba2 | 5864 | out_err: |
1f2c6651 | 5865 | rbd_dev_unparent(rbd_dev); |
1761b229 | 5866 | rbd_dev_destroy(parent); |
124afba2 AE |
5867 | return ret; |
5868 | } | |
5869 | ||
811c6688 ID |
5870 | /* |
5871 | * rbd_dev->header_rwsem must be locked for write and will be unlocked | |
5872 | * upon return. | |
5873 | */ | |
200a6a8b | 5874 | static int rbd_dev_device_setup(struct rbd_device *rbd_dev) |
124afba2 | 5875 | { |
83a06263 | 5876 | int ret; |
d1cf5788 | 5877 | |
9b60e70b | 5878 | /* Record our major and minor device numbers. */ |
83a06263 | 5879 | |
9b60e70b ID |
5880 | if (!single_major) { |
5881 | ret = register_blkdev(0, rbd_dev->name); | |
5882 | if (ret < 0) | |
1643dfa4 | 5883 | goto err_out_unlock; |
9b60e70b ID |
5884 | |
5885 | rbd_dev->major = ret; | |
5886 | rbd_dev->minor = 0; | |
5887 | } else { | |
5888 | rbd_dev->major = rbd_major; | |
5889 | rbd_dev->minor = rbd_dev_id_to_minor(rbd_dev->dev_id); | |
5890 | } | |
83a06263 AE |
5891 | |
5892 | /* Set up the blkdev mapping. */ | |
5893 | ||
5894 | ret = rbd_init_disk(rbd_dev); | |
5895 | if (ret) | |
5896 | goto err_out_blkdev; | |
5897 | ||
f35a4dee | 5898 | ret = rbd_dev_mapping_set(rbd_dev); |
83a06263 AE |
5899 | if (ret) |
5900 | goto err_out_disk; | |
bc1ecc65 | 5901 | |
f35a4dee | 5902 | set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE); |
22001f61 | 5903 | set_disk_ro(rbd_dev->disk, rbd_dev->mapping.read_only); |
f35a4dee | 5904 | |
dd5ac32d ID |
5905 | dev_set_name(&rbd_dev->dev, "%d", rbd_dev->dev_id); |
5906 | ret = device_add(&rbd_dev->dev); | |
f35a4dee | 5907 | if (ret) |
f5ee37bd | 5908 | goto err_out_mapping; |
83a06263 | 5909 | |
83a06263 AE |
5910 | /* Everything's ready. Announce the disk to the world. */ |
5911 | ||
129b79d4 | 5912 | set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
811c6688 | 5913 | up_write(&rbd_dev->header_rwsem); |
83a06263 | 5914 | |
1643dfa4 ID |
5915 | spin_lock(&rbd_dev_list_lock); |
5916 | list_add_tail(&rbd_dev->node, &rbd_dev_list); | |
5917 | spin_unlock(&rbd_dev_list_lock); | |
5918 | ||
811c6688 | 5919 | add_disk(rbd_dev->disk); |
ca7909e8 ID |
5920 | pr_info("%s: capacity %llu features 0x%llx\n", rbd_dev->disk->disk_name, |
5921 | (unsigned long long)get_capacity(rbd_dev->disk) << SECTOR_SHIFT, | |
5922 | rbd_dev->header.features); | |
83a06263 AE |
5923 | |
5924 | return ret; | |
2f82ee54 | 5925 | |
f35a4dee AE |
5926 | err_out_mapping: |
5927 | rbd_dev_mapping_clear(rbd_dev); | |
83a06263 AE |
5928 | err_out_disk: |
5929 | rbd_free_disk(rbd_dev); | |
5930 | err_out_blkdev: | |
9b60e70b ID |
5931 | if (!single_major) |
5932 | unregister_blkdev(rbd_dev->major, rbd_dev->name); | |
811c6688 ID |
5933 | err_out_unlock: |
5934 | up_write(&rbd_dev->header_rwsem); | |
83a06263 AE |
5935 | return ret; |
5936 | } | |
5937 | ||
332bb12d AE |
5938 | static int rbd_dev_header_name(struct rbd_device *rbd_dev) |
5939 | { | |
5940 | struct rbd_spec *spec = rbd_dev->spec; | |
c41d13a3 | 5941 | int ret; |
332bb12d AE |
5942 | |
5943 | /* Record the header object name for this rbd image. */ | |
5944 | ||
5945 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); | |
332bb12d | 5946 | if (rbd_dev->image_format == 1) |
c41d13a3 ID |
5947 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
5948 | spec->image_name, RBD_SUFFIX); | |
332bb12d | 5949 | else |
c41d13a3 ID |
5950 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
5951 | RBD_HEADER_PREFIX, spec->image_id); | |
332bb12d | 5952 | |
c41d13a3 | 5953 | return ret; |
332bb12d AE |
5954 | } |
5955 | ||
200a6a8b AE |
5956 | static void rbd_dev_image_release(struct rbd_device *rbd_dev) |
5957 | { | |
6fd48b3b | 5958 | rbd_dev_unprobe(rbd_dev); |
6fd48b3b AE |
5959 | rbd_dev->image_format = 0; |
5960 | kfree(rbd_dev->spec->image_id); | |
5961 | rbd_dev->spec->image_id = NULL; | |
5962 | ||
200a6a8b AE |
5963 | rbd_dev_destroy(rbd_dev); |
5964 | } | |
5965 | ||
a30b71b9 AE |
5966 | /* |
5967 | * Probe for the existence of the header object for the given rbd | |
1f3ef788 AE |
5968 | * device. If this image is the one being mapped (i.e., not a |
5969 | * parent), initiate a watch on its header object before using that | |
5970 | * object to get detailed information about the rbd image. | |
a30b71b9 | 5971 | */ |
6d69bb53 | 5972 | static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth) |
a30b71b9 AE |
5973 | { |
5974 | int ret; | |
5975 | ||
5976 | /* | |
3abef3b3 AE |
5977 | * Get the id from the image id object. Unless there's an |
5978 | * error, rbd_dev->spec->image_id will be filled in with | |
5979 | * a dynamically-allocated string, and rbd_dev->image_format | |
5980 | * will be set to either 1 or 2. | |
a30b71b9 AE |
5981 | */ |
5982 | ret = rbd_dev_image_id(rbd_dev); | |
5983 | if (ret) | |
c0fba368 | 5984 | return ret; |
c0fba368 | 5985 | |
332bb12d AE |
5986 | ret = rbd_dev_header_name(rbd_dev); |
5987 | if (ret) | |
5988 | goto err_out_format; | |
5989 | ||
6d69bb53 | 5990 | if (!depth) { |
99d16943 | 5991 | ret = rbd_register_watch(rbd_dev); |
1fe48023 ID |
5992 | if (ret) { |
5993 | if (ret == -ENOENT) | |
5994 | pr_info("image %s/%s does not exist\n", | |
5995 | rbd_dev->spec->pool_name, | |
5996 | rbd_dev->spec->image_name); | |
c41d13a3 | 5997 | goto err_out_format; |
1fe48023 | 5998 | } |
1f3ef788 | 5999 | } |
b644de2b | 6000 | |
a720ae09 | 6001 | ret = rbd_dev_header_info(rbd_dev); |
5655c4d9 | 6002 | if (ret) |
b644de2b | 6003 | goto err_out_watch; |
83a06263 | 6004 | |
04077599 ID |
6005 | /* |
6006 | * If this image is the one being mapped, we have pool name and | |
6007 | * id, image name and id, and snap name - need to fill snap id. | |
6008 | * Otherwise this is a parent image, identified by pool, image | |
6009 | * and snap ids - need to fill in names for those ids. | |
6010 | */ | |
6d69bb53 | 6011 | if (!depth) |
04077599 ID |
6012 | ret = rbd_spec_fill_snap_id(rbd_dev); |
6013 | else | |
6014 | ret = rbd_spec_fill_names(rbd_dev); | |
1fe48023 ID |
6015 | if (ret) { |
6016 | if (ret == -ENOENT) | |
6017 | pr_info("snap %s/%s@%s does not exist\n", | |
6018 | rbd_dev->spec->pool_name, | |
6019 | rbd_dev->spec->image_name, | |
6020 | rbd_dev->spec->snap_name); | |
33dca39f | 6021 | goto err_out_probe; |
1fe48023 | 6022 | } |
9bb81c9b | 6023 | |
e8f59b59 ID |
6024 | if (rbd_dev->header.features & RBD_FEATURE_LAYERING) { |
6025 | ret = rbd_dev_v2_parent_info(rbd_dev); | |
6026 | if (ret) | |
6027 | goto err_out_probe; | |
6028 | ||
6029 | /* | |
6030 | * Need to warn users if this image is the one being | |
6031 | * mapped and has a parent. | |
6032 | */ | |
6d69bb53 | 6033 | if (!depth && rbd_dev->parent_spec) |
e8f59b59 ID |
6034 | rbd_warn(rbd_dev, |
6035 | "WARNING: kernel layering is EXPERIMENTAL!"); | |
6036 | } | |
6037 | ||
6d69bb53 | 6038 | ret = rbd_dev_probe_parent(rbd_dev, depth); |
30d60ba2 AE |
6039 | if (ret) |
6040 | goto err_out_probe; | |
6041 | ||
6042 | dout("discovered format %u image, header name is %s\n", | |
c41d13a3 | 6043 | rbd_dev->image_format, rbd_dev->header_oid.name); |
30d60ba2 | 6044 | return 0; |
e8f59b59 | 6045 | |
6fd48b3b AE |
6046 | err_out_probe: |
6047 | rbd_dev_unprobe(rbd_dev); | |
b644de2b | 6048 | err_out_watch: |
6d69bb53 | 6049 | if (!depth) |
99d16943 | 6050 | rbd_unregister_watch(rbd_dev); |
332bb12d AE |
6051 | err_out_format: |
6052 | rbd_dev->image_format = 0; | |
5655c4d9 AE |
6053 | kfree(rbd_dev->spec->image_id); |
6054 | rbd_dev->spec->image_id = NULL; | |
a30b71b9 AE |
6055 | return ret; |
6056 | } | |
6057 | ||
9b60e70b ID |
6058 | static ssize_t do_rbd_add(struct bus_type *bus, |
6059 | const char *buf, | |
6060 | size_t count) | |
602adf40 | 6061 | { |
cb8627c7 | 6062 | struct rbd_device *rbd_dev = NULL; |
dc79b113 | 6063 | struct ceph_options *ceph_opts = NULL; |
4e9afeba | 6064 | struct rbd_options *rbd_opts = NULL; |
859c31df | 6065 | struct rbd_spec *spec = NULL; |
9d3997fd | 6066 | struct rbd_client *rbdc; |
51344a38 | 6067 | bool read_only; |
b51c83c2 | 6068 | int rc; |
602adf40 YS |
6069 | |
6070 | if (!try_module_get(THIS_MODULE)) | |
6071 | return -ENODEV; | |
6072 | ||
602adf40 | 6073 | /* parse add command */ |
859c31df | 6074 | rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec); |
dc79b113 | 6075 | if (rc < 0) |
dd5ac32d | 6076 | goto out; |
78cea76e | 6077 | |
9d3997fd AE |
6078 | rbdc = rbd_get_client(ceph_opts); |
6079 | if (IS_ERR(rbdc)) { | |
6080 | rc = PTR_ERR(rbdc); | |
0ddebc0c | 6081 | goto err_out_args; |
9d3997fd | 6082 | } |
602adf40 | 6083 | |
602adf40 | 6084 | /* pick the pool */ |
30ba1f02 | 6085 | rc = rbd_add_get_pool_id(rbdc, spec->pool_name); |
1fe48023 ID |
6086 | if (rc < 0) { |
6087 | if (rc == -ENOENT) | |
6088 | pr_info("pool %s does not exist\n", spec->pool_name); | |
602adf40 | 6089 | goto err_out_client; |
1fe48023 | 6090 | } |
c0cd10db | 6091 | spec->pool_id = (u64)rc; |
859c31df | 6092 | |
d147543d | 6093 | rbd_dev = rbd_dev_create(rbdc, spec, rbd_opts); |
b51c83c2 ID |
6094 | if (!rbd_dev) { |
6095 | rc = -ENOMEM; | |
bd4ba655 | 6096 | goto err_out_client; |
b51c83c2 | 6097 | } |
c53d5893 AE |
6098 | rbdc = NULL; /* rbd_dev now owns this */ |
6099 | spec = NULL; /* rbd_dev now owns this */ | |
d147543d | 6100 | rbd_opts = NULL; /* rbd_dev now owns this */ |
602adf40 | 6101 | |
0d6d1e9c MC |
6102 | rbd_dev->config_info = kstrdup(buf, GFP_KERNEL); |
6103 | if (!rbd_dev->config_info) { | |
6104 | rc = -ENOMEM; | |
6105 | goto err_out_rbd_dev; | |
6106 | } | |
6107 | ||
811c6688 | 6108 | down_write(&rbd_dev->header_rwsem); |
6d69bb53 | 6109 | rc = rbd_dev_image_probe(rbd_dev, 0); |
0d6d1e9c MC |
6110 | if (rc < 0) { |
6111 | up_write(&rbd_dev->header_rwsem); | |
c53d5893 | 6112 | goto err_out_rbd_dev; |
0d6d1e9c | 6113 | } |
05fd6f6f | 6114 | |
7ce4eef7 AE |
6115 | /* If we are mapping a snapshot it must be marked read-only */ |
6116 | ||
d147543d | 6117 | read_only = rbd_dev->opts->read_only; |
7ce4eef7 AE |
6118 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP) |
6119 | read_only = true; | |
6120 | rbd_dev->mapping.read_only = read_only; | |
6121 | ||
b536f69a | 6122 | rc = rbd_dev_device_setup(rbd_dev); |
3abef3b3 | 6123 | if (rc) { |
e37180c0 | 6124 | /* |
99d16943 | 6125 | * rbd_unregister_watch() can't be moved into |
e37180c0 ID |
6126 | * rbd_dev_image_release() without refactoring, see |
6127 | * commit 1f3ef78861ac. | |
6128 | */ | |
99d16943 | 6129 | rbd_unregister_watch(rbd_dev); |
3abef3b3 | 6130 | rbd_dev_image_release(rbd_dev); |
dd5ac32d | 6131 | goto out; |
3abef3b3 AE |
6132 | } |
6133 | ||
dd5ac32d ID |
6134 | rc = count; |
6135 | out: | |
6136 | module_put(THIS_MODULE); | |
6137 | return rc; | |
b536f69a | 6138 | |
c53d5893 AE |
6139 | err_out_rbd_dev: |
6140 | rbd_dev_destroy(rbd_dev); | |
bd4ba655 | 6141 | err_out_client: |
9d3997fd | 6142 | rbd_put_client(rbdc); |
0ddebc0c | 6143 | err_out_args: |
859c31df | 6144 | rbd_spec_put(spec); |
d147543d | 6145 | kfree(rbd_opts); |
dd5ac32d | 6146 | goto out; |
602adf40 YS |
6147 | } |
6148 | ||
9b60e70b ID |
6149 | static ssize_t rbd_add(struct bus_type *bus, |
6150 | const char *buf, | |
6151 | size_t count) | |
6152 | { | |
6153 | if (single_major) | |
6154 | return -EINVAL; | |
6155 | ||
6156 | return do_rbd_add(bus, buf, count); | |
6157 | } | |
6158 | ||
6159 | static ssize_t rbd_add_single_major(struct bus_type *bus, | |
6160 | const char *buf, | |
6161 | size_t count) | |
6162 | { | |
6163 | return do_rbd_add(bus, buf, count); | |
6164 | } | |
6165 | ||
dd5ac32d | 6166 | static void rbd_dev_device_release(struct rbd_device *rbd_dev) |
602adf40 | 6167 | { |
602adf40 | 6168 | rbd_free_disk(rbd_dev); |
1643dfa4 ID |
6169 | |
6170 | spin_lock(&rbd_dev_list_lock); | |
6171 | list_del_init(&rbd_dev->node); | |
6172 | spin_unlock(&rbd_dev_list_lock); | |
6173 | ||
200a6a8b | 6174 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
dd5ac32d | 6175 | device_del(&rbd_dev->dev); |
6d80b130 | 6176 | rbd_dev_mapping_clear(rbd_dev); |
9b60e70b ID |
6177 | if (!single_major) |
6178 | unregister_blkdev(rbd_dev->major, rbd_dev->name); | |
602adf40 YS |
6179 | } |
6180 | ||
05a46afd AE |
6181 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev) |
6182 | { | |
ad945fc1 | 6183 | while (rbd_dev->parent) { |
05a46afd AE |
6184 | struct rbd_device *first = rbd_dev; |
6185 | struct rbd_device *second = first->parent; | |
6186 | struct rbd_device *third; | |
6187 | ||
6188 | /* | |
6189 | * Follow to the parent with no grandparent and | |
6190 | * remove it. | |
6191 | */ | |
6192 | while (second && (third = second->parent)) { | |
6193 | first = second; | |
6194 | second = third; | |
6195 | } | |
ad945fc1 | 6196 | rbd_assert(second); |
8ad42cd0 | 6197 | rbd_dev_image_release(second); |
ad945fc1 AE |
6198 | first->parent = NULL; |
6199 | first->parent_overlap = 0; | |
6200 | ||
6201 | rbd_assert(first->parent_spec); | |
05a46afd AE |
6202 | rbd_spec_put(first->parent_spec); |
6203 | first->parent_spec = NULL; | |
05a46afd AE |
6204 | } |
6205 | } | |
6206 | ||
9b60e70b ID |
6207 | static ssize_t do_rbd_remove(struct bus_type *bus, |
6208 | const char *buf, | |
6209 | size_t count) | |
602adf40 YS |
6210 | { |
6211 | struct rbd_device *rbd_dev = NULL; | |
751cc0e3 AE |
6212 | struct list_head *tmp; |
6213 | int dev_id; | |
0276dca6 | 6214 | char opt_buf[6]; |
82a442d2 | 6215 | bool already = false; |
0276dca6 | 6216 | bool force = false; |
0d8189e1 | 6217 | int ret; |
602adf40 | 6218 | |
0276dca6 MC |
6219 | dev_id = -1; |
6220 | opt_buf[0] = '\0'; | |
6221 | sscanf(buf, "%d %5s", &dev_id, opt_buf); | |
6222 | if (dev_id < 0) { | |
6223 | pr_err("dev_id out of range\n"); | |
602adf40 | 6224 | return -EINVAL; |
0276dca6 MC |
6225 | } |
6226 | if (opt_buf[0] != '\0') { | |
6227 | if (!strcmp(opt_buf, "force")) { | |
6228 | force = true; | |
6229 | } else { | |
6230 | pr_err("bad remove option at '%s'\n", opt_buf); | |
6231 | return -EINVAL; | |
6232 | } | |
6233 | } | |
602adf40 | 6234 | |
751cc0e3 AE |
6235 | ret = -ENOENT; |
6236 | spin_lock(&rbd_dev_list_lock); | |
6237 | list_for_each(tmp, &rbd_dev_list) { | |
6238 | rbd_dev = list_entry(tmp, struct rbd_device, node); | |
6239 | if (rbd_dev->dev_id == dev_id) { | |
6240 | ret = 0; | |
6241 | break; | |
6242 | } | |
42382b70 | 6243 | } |
751cc0e3 AE |
6244 | if (!ret) { |
6245 | spin_lock_irq(&rbd_dev->lock); | |
0276dca6 | 6246 | if (rbd_dev->open_count && !force) |
751cc0e3 AE |
6247 | ret = -EBUSY; |
6248 | else | |
82a442d2 AE |
6249 | already = test_and_set_bit(RBD_DEV_FLAG_REMOVING, |
6250 | &rbd_dev->flags); | |
751cc0e3 AE |
6251 | spin_unlock_irq(&rbd_dev->lock); |
6252 | } | |
6253 | spin_unlock(&rbd_dev_list_lock); | |
82a442d2 | 6254 | if (ret < 0 || already) |
1ba0f1e7 | 6255 | return ret; |
751cc0e3 | 6256 | |
0276dca6 MC |
6257 | if (force) { |
6258 | /* | |
6259 | * Prevent new IO from being queued and wait for existing | |
6260 | * IO to complete/fail. | |
6261 | */ | |
6262 | blk_mq_freeze_queue(rbd_dev->disk->queue); | |
6263 | blk_set_queue_dying(rbd_dev->disk->queue); | |
6264 | } | |
6265 | ||
ed95b21a ID |
6266 | down_write(&rbd_dev->lock_rwsem); |
6267 | if (__rbd_is_lock_owner(rbd_dev)) | |
6268 | rbd_unlock(rbd_dev); | |
6269 | up_write(&rbd_dev->lock_rwsem); | |
99d16943 | 6270 | rbd_unregister_watch(rbd_dev); |
fca27065 | 6271 | |
9875201e JD |
6272 | /* |
6273 | * Don't free anything from rbd_dev->disk until after all | |
6274 | * notifies are completely processed. Otherwise | |
6275 | * rbd_bus_del_dev() will race with rbd_watch_cb(), resulting | |
6276 | * in a potential use after free of rbd_dev->disk or rbd_dev. | |
6277 | */ | |
dd5ac32d | 6278 | rbd_dev_device_release(rbd_dev); |
8ad42cd0 | 6279 | rbd_dev_image_release(rbd_dev); |
aafb230e | 6280 | |
1ba0f1e7 | 6281 | return count; |
602adf40 YS |
6282 | } |
6283 | ||
9b60e70b ID |
6284 | static ssize_t rbd_remove(struct bus_type *bus, |
6285 | const char *buf, | |
6286 | size_t count) | |
6287 | { | |
6288 | if (single_major) | |
6289 | return -EINVAL; | |
6290 | ||
6291 | return do_rbd_remove(bus, buf, count); | |
6292 | } | |
6293 | ||
6294 | static ssize_t rbd_remove_single_major(struct bus_type *bus, | |
6295 | const char *buf, | |
6296 | size_t count) | |
6297 | { | |
6298 | return do_rbd_remove(bus, buf, count); | |
6299 | } | |
6300 | ||
602adf40 YS |
6301 | /* |
6302 | * create control files in sysfs | |
dfc5606d | 6303 | * /sys/bus/rbd/... |
602adf40 YS |
6304 | */ |
6305 | static int rbd_sysfs_init(void) | |
6306 | { | |
dfc5606d | 6307 | int ret; |
602adf40 | 6308 | |
fed4c143 | 6309 | ret = device_register(&rbd_root_dev); |
21079786 | 6310 | if (ret < 0) |
dfc5606d | 6311 | return ret; |
602adf40 | 6312 | |
fed4c143 AE |
6313 | ret = bus_register(&rbd_bus_type); |
6314 | if (ret < 0) | |
6315 | device_unregister(&rbd_root_dev); | |
602adf40 | 6316 | |
602adf40 YS |
6317 | return ret; |
6318 | } | |
6319 | ||
6320 | static void rbd_sysfs_cleanup(void) | |
6321 | { | |
dfc5606d | 6322 | bus_unregister(&rbd_bus_type); |
fed4c143 | 6323 | device_unregister(&rbd_root_dev); |
602adf40 YS |
6324 | } |
6325 | ||
1c2a9dfe AE |
6326 | static int rbd_slab_init(void) |
6327 | { | |
6328 | rbd_assert(!rbd_img_request_cache); | |
03d94406 | 6329 | rbd_img_request_cache = KMEM_CACHE(rbd_img_request, 0); |
868311b1 AE |
6330 | if (!rbd_img_request_cache) |
6331 | return -ENOMEM; | |
6332 | ||
6333 | rbd_assert(!rbd_obj_request_cache); | |
03d94406 | 6334 | rbd_obj_request_cache = KMEM_CACHE(rbd_obj_request, 0); |
78c2a44a AE |
6335 | if (!rbd_obj_request_cache) |
6336 | goto out_err; | |
6337 | ||
6c696d85 | 6338 | return 0; |
1c2a9dfe | 6339 | |
6c696d85 | 6340 | out_err: |
868311b1 AE |
6341 | kmem_cache_destroy(rbd_img_request_cache); |
6342 | rbd_img_request_cache = NULL; | |
1c2a9dfe AE |
6343 | return -ENOMEM; |
6344 | } | |
6345 | ||
6346 | static void rbd_slab_exit(void) | |
6347 | { | |
868311b1 AE |
6348 | rbd_assert(rbd_obj_request_cache); |
6349 | kmem_cache_destroy(rbd_obj_request_cache); | |
6350 | rbd_obj_request_cache = NULL; | |
6351 | ||
1c2a9dfe AE |
6352 | rbd_assert(rbd_img_request_cache); |
6353 | kmem_cache_destroy(rbd_img_request_cache); | |
6354 | rbd_img_request_cache = NULL; | |
6355 | } | |
6356 | ||
cc344fa1 | 6357 | static int __init rbd_init(void) |
602adf40 YS |
6358 | { |
6359 | int rc; | |
6360 | ||
1e32d34c AE |
6361 | if (!libceph_compatible(NULL)) { |
6362 | rbd_warn(NULL, "libceph incompatibility (quitting)"); | |
1e32d34c AE |
6363 | return -EINVAL; |
6364 | } | |
e1b4d96d | 6365 | |
1c2a9dfe | 6366 | rc = rbd_slab_init(); |
602adf40 YS |
6367 | if (rc) |
6368 | return rc; | |
e1b4d96d | 6369 | |
f5ee37bd ID |
6370 | /* |
6371 | * The number of active work items is limited by the number of | |
f77303bd | 6372 | * rbd devices * queue depth, so leave @max_active at default. |
f5ee37bd ID |
6373 | */ |
6374 | rbd_wq = alloc_workqueue(RBD_DRV_NAME, WQ_MEM_RECLAIM, 0); | |
6375 | if (!rbd_wq) { | |
6376 | rc = -ENOMEM; | |
6377 | goto err_out_slab; | |
6378 | } | |
6379 | ||
9b60e70b ID |
6380 | if (single_major) { |
6381 | rbd_major = register_blkdev(0, RBD_DRV_NAME); | |
6382 | if (rbd_major < 0) { | |
6383 | rc = rbd_major; | |
f5ee37bd | 6384 | goto err_out_wq; |
9b60e70b ID |
6385 | } |
6386 | } | |
6387 | ||
1c2a9dfe AE |
6388 | rc = rbd_sysfs_init(); |
6389 | if (rc) | |
9b60e70b ID |
6390 | goto err_out_blkdev; |
6391 | ||
6392 | if (single_major) | |
6393 | pr_info("loaded (major %d)\n", rbd_major); | |
6394 | else | |
6395 | pr_info("loaded\n"); | |
1c2a9dfe | 6396 | |
e1b4d96d ID |
6397 | return 0; |
6398 | ||
9b60e70b ID |
6399 | err_out_blkdev: |
6400 | if (single_major) | |
6401 | unregister_blkdev(rbd_major, RBD_DRV_NAME); | |
f5ee37bd ID |
6402 | err_out_wq: |
6403 | destroy_workqueue(rbd_wq); | |
e1b4d96d ID |
6404 | err_out_slab: |
6405 | rbd_slab_exit(); | |
1c2a9dfe | 6406 | return rc; |
602adf40 YS |
6407 | } |
6408 | ||
cc344fa1 | 6409 | static void __exit rbd_exit(void) |
602adf40 | 6410 | { |
ffe312cf | 6411 | ida_destroy(&rbd_dev_id_ida); |
602adf40 | 6412 | rbd_sysfs_cleanup(); |
9b60e70b ID |
6413 | if (single_major) |
6414 | unregister_blkdev(rbd_major, RBD_DRV_NAME); | |
f5ee37bd | 6415 | destroy_workqueue(rbd_wq); |
1c2a9dfe | 6416 | rbd_slab_exit(); |
602adf40 YS |
6417 | } |
6418 | ||
6419 | module_init(rbd_init); | |
6420 | module_exit(rbd_exit); | |
6421 | ||
d552c619 | 6422 | MODULE_AUTHOR("Alex Elder <elder@inktank.com>"); |
602adf40 YS |
6423 | MODULE_AUTHOR("Sage Weil <sage@newdream.net>"); |
6424 | MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>"); | |
602adf40 YS |
6425 | /* following authorship retained from original osdblk.c */ |
6426 | MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>"); | |
6427 | ||
90da258b | 6428 | MODULE_DESCRIPTION("RADOS Block Device (RBD) driver"); |
602adf40 | 6429 | MODULE_LICENSE("GPL"); |