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