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