Commit | Line | Data |
---|---|---|
602adf40 YS |
1 | /* |
2 | rbd.c -- Export ceph rados objects as a Linux block device | |
3 | ||
4 | ||
5 | based on drivers/block/osdblk.c: | |
6 | ||
7 | Copyright 2009 Red Hat, Inc. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; see the file COPYING. If not, write to | |
20 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 | ||
22 | ||
23 | ||
dfc5606d | 24 | For usage instructions, please refer to: |
602adf40 | 25 | |
dfc5606d | 26 | Documentation/ABI/testing/sysfs-bus-rbd |
602adf40 YS |
27 | |
28 | */ | |
29 | ||
30 | #include <linux/ceph/libceph.h> | |
31 | #include <linux/ceph/osd_client.h> | |
32 | #include <linux/ceph/mon_client.h> | |
33 | #include <linux/ceph/decode.h> | |
59c2be1e | 34 | #include <linux/parser.h> |
602adf40 YS |
35 | |
36 | #include <linux/kernel.h> | |
37 | #include <linux/device.h> | |
38 | #include <linux/module.h> | |
39 | #include <linux/fs.h> | |
40 | #include <linux/blkdev.h> | |
41 | ||
42 | #include "rbd_types.h" | |
43 | ||
aafb230e AE |
44 | #define RBD_DEBUG /* Activate rbd_assert() calls */ |
45 | ||
593a9e7b AE |
46 | /* |
47 | * The basic unit of block I/O is a sector. It is interpreted in a | |
48 | * number of contexts in Linux (blk, bio, genhd), but the default is | |
49 | * universally 512 bytes. These symbols are just slightly more | |
50 | * meaningful than the bare numbers they represent. | |
51 | */ | |
52 | #define SECTOR_SHIFT 9 | |
53 | #define SECTOR_SIZE (1ULL << SECTOR_SHIFT) | |
54 | ||
df111be6 AE |
55 | /* It might be useful to have this defined elsewhere too */ |
56 | ||
57 | #define U64_MAX ((u64) (~0ULL)) | |
58 | ||
f0f8cef5 AE |
59 | #define RBD_DRV_NAME "rbd" |
60 | #define RBD_DRV_NAME_LONG "rbd (rados block device)" | |
602adf40 YS |
61 | |
62 | #define RBD_MINORS_PER_MAJOR 256 /* max minors per blkdev */ | |
63 | ||
602adf40 YS |
64 | #define RBD_MAX_SNAP_NAME_LEN 32 |
65 | #define RBD_MAX_OPT_LEN 1024 | |
66 | ||
67 | #define RBD_SNAP_HEAD_NAME "-" | |
68 | ||
589d30e0 AE |
69 | #define RBD_IMAGE_ID_LEN_MAX 64 |
70 | ||
81a89793 AE |
71 | /* |
72 | * An RBD device name will be "rbd#", where the "rbd" comes from | |
73 | * RBD_DRV_NAME above, and # is a unique integer identifier. | |
74 | * MAX_INT_FORMAT_WIDTH is used in ensuring DEV_NAME_LEN is big | |
75 | * enough to hold all possible device names. | |
76 | */ | |
602adf40 | 77 | #define DEV_NAME_LEN 32 |
81a89793 | 78 | #define MAX_INT_FORMAT_WIDTH ((5 * sizeof (int)) / 2 + 1) |
602adf40 | 79 | |
cc0538b6 | 80 | #define RBD_READ_ONLY_DEFAULT false |
59c2be1e | 81 | |
602adf40 YS |
82 | /* |
83 | * block device image metadata (in-memory version) | |
84 | */ | |
85 | struct rbd_image_header { | |
f84344f3 | 86 | /* These four fields never change for a given rbd image */ |
849b4260 | 87 | char *object_prefix; |
602adf40 YS |
88 | __u8 obj_order; |
89 | __u8 crypt_type; | |
90 | __u8 comp_type; | |
602adf40 | 91 | |
f84344f3 AE |
92 | /* The remaining fields need to be updated occasionally */ |
93 | u64 image_size; | |
94 | struct ceph_snap_context *snapc; | |
602adf40 YS |
95 | char *snap_names; |
96 | u64 *snap_sizes; | |
59c2be1e YS |
97 | |
98 | u64 obj_version; | |
99 | }; | |
100 | ||
101 | struct rbd_options { | |
cc0538b6 | 102 | bool read_only; |
602adf40 YS |
103 | }; |
104 | ||
105 | /* | |
f0f8cef5 | 106 | * an instance of the client. multiple devices may share an rbd client. |
602adf40 YS |
107 | */ |
108 | struct rbd_client { | |
109 | struct ceph_client *client; | |
110 | struct kref kref; | |
111 | struct list_head node; | |
112 | }; | |
113 | ||
114 | /* | |
f0f8cef5 | 115 | * a request completion status |
602adf40 | 116 | */ |
1fec7093 YS |
117 | struct rbd_req_status { |
118 | int done; | |
119 | int rc; | |
120 | u64 bytes; | |
121 | }; | |
122 | ||
123 | /* | |
124 | * a collection of requests | |
125 | */ | |
126 | struct rbd_req_coll { | |
127 | int total; | |
128 | int num_done; | |
129 | struct kref kref; | |
130 | struct rbd_req_status status[0]; | |
602adf40 YS |
131 | }; |
132 | ||
f0f8cef5 AE |
133 | /* |
134 | * a single io request | |
135 | */ | |
136 | struct rbd_request { | |
137 | struct request *rq; /* blk layer request */ | |
138 | struct bio *bio; /* cloned bio */ | |
139 | struct page **pages; /* list of used pages */ | |
140 | u64 len; | |
141 | int coll_index; | |
142 | struct rbd_req_coll *coll; | |
143 | }; | |
144 | ||
dfc5606d YS |
145 | struct rbd_snap { |
146 | struct device dev; | |
147 | const char *name; | |
3591538f | 148 | u64 size; |
dfc5606d YS |
149 | struct list_head node; |
150 | u64 id; | |
151 | }; | |
152 | ||
f84344f3 AE |
153 | struct rbd_mapping { |
154 | char *snap_name; | |
155 | u64 snap_id; | |
99c1f08f | 156 | u64 size; |
f84344f3 AE |
157 | bool snap_exists; |
158 | bool read_only; | |
159 | }; | |
160 | ||
602adf40 YS |
161 | /* |
162 | * a single device | |
163 | */ | |
164 | struct rbd_device { | |
de71a297 | 165 | int dev_id; /* blkdev unique id */ |
602adf40 YS |
166 | |
167 | int major; /* blkdev assigned major */ | |
168 | struct gendisk *disk; /* blkdev's gendisk and rq */ | |
602adf40 | 169 | |
f8c38929 | 170 | struct rbd_options rbd_opts; |
602adf40 YS |
171 | struct rbd_client *rbd_client; |
172 | ||
173 | char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */ | |
174 | ||
175 | spinlock_t lock; /* queue lock */ | |
176 | ||
177 | struct rbd_image_header header; | |
589d30e0 AE |
178 | char *image_id; |
179 | size_t image_id_len; | |
0bed54dc AE |
180 | char *image_name; |
181 | size_t image_name_len; | |
182 | char *header_name; | |
d22f76e7 | 183 | char *pool_name; |
9bb2f334 | 184 | int pool_id; |
602adf40 | 185 | |
59c2be1e YS |
186 | struct ceph_osd_event *watch_event; |
187 | struct ceph_osd_request *watch_request; | |
188 | ||
c666601a JD |
189 | /* protects updating the header */ |
190 | struct rw_semaphore header_rwsem; | |
f84344f3 AE |
191 | |
192 | struct rbd_mapping mapping; | |
602adf40 YS |
193 | |
194 | struct list_head node; | |
dfc5606d YS |
195 | |
196 | /* list of snapshots */ | |
197 | struct list_head snaps; | |
198 | ||
199 | /* sysfs related */ | |
200 | struct device dev; | |
201 | }; | |
202 | ||
602adf40 | 203 | static DEFINE_MUTEX(ctl_mutex); /* Serialize open/close/setup/teardown */ |
e124a82f | 204 | |
602adf40 | 205 | static LIST_HEAD(rbd_dev_list); /* devices */ |
e124a82f AE |
206 | static DEFINE_SPINLOCK(rbd_dev_list_lock); |
207 | ||
432b8587 AE |
208 | static LIST_HEAD(rbd_client_list); /* clients */ |
209 | static DEFINE_SPINLOCK(rbd_client_list_lock); | |
602adf40 | 210 | |
304f6808 AE |
211 | static int rbd_dev_snaps_update(struct rbd_device *rbd_dev); |
212 | static int rbd_dev_snaps_register(struct rbd_device *rbd_dev); | |
213 | ||
dfc5606d | 214 | static void rbd_dev_release(struct device *dev); |
14e7085d | 215 | static void __rbd_remove_snap_dev(struct rbd_snap *snap); |
dfc5606d | 216 | |
f0f8cef5 AE |
217 | static ssize_t rbd_add(struct bus_type *bus, const char *buf, |
218 | size_t count); | |
219 | static ssize_t rbd_remove(struct bus_type *bus, const char *buf, | |
220 | size_t count); | |
221 | ||
222 | static struct bus_attribute rbd_bus_attrs[] = { | |
223 | __ATTR(add, S_IWUSR, NULL, rbd_add), | |
224 | __ATTR(remove, S_IWUSR, NULL, rbd_remove), | |
225 | __ATTR_NULL | |
226 | }; | |
227 | ||
228 | static struct bus_type rbd_bus_type = { | |
229 | .name = "rbd", | |
230 | .bus_attrs = rbd_bus_attrs, | |
231 | }; | |
232 | ||
233 | static void rbd_root_dev_release(struct device *dev) | |
234 | { | |
235 | } | |
236 | ||
237 | static struct device rbd_root_dev = { | |
238 | .init_name = "rbd", | |
239 | .release = rbd_root_dev_release, | |
240 | }; | |
241 | ||
aafb230e AE |
242 | #ifdef RBD_DEBUG |
243 | #define rbd_assert(expr) \ | |
244 | if (unlikely(!(expr))) { \ | |
245 | printk(KERN_ERR "\nAssertion failure in %s() " \ | |
246 | "at line %d:\n\n" \ | |
247 | "\trbd_assert(%s);\n\n", \ | |
248 | __func__, __LINE__, #expr); \ | |
249 | BUG(); \ | |
250 | } | |
251 | #else /* !RBD_DEBUG */ | |
252 | # define rbd_assert(expr) ((void) 0) | |
253 | #endif /* !RBD_DEBUG */ | |
dfc5606d | 254 | |
dfc5606d YS |
255 | static struct device *rbd_get_dev(struct rbd_device *rbd_dev) |
256 | { | |
257 | return get_device(&rbd_dev->dev); | |
258 | } | |
259 | ||
260 | static void rbd_put_dev(struct rbd_device *rbd_dev) | |
261 | { | |
262 | put_device(&rbd_dev->dev); | |
263 | } | |
602adf40 | 264 | |
1fe5e993 | 265 | static int rbd_refresh_header(struct rbd_device *rbd_dev, u64 *hver); |
59c2be1e | 266 | |
602adf40 YS |
267 | static int rbd_open(struct block_device *bdev, fmode_t mode) |
268 | { | |
f0f8cef5 | 269 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
602adf40 | 270 | |
f84344f3 | 271 | if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only) |
602adf40 YS |
272 | return -EROFS; |
273 | ||
340c7a2b | 274 | rbd_get_dev(rbd_dev); |
f84344f3 | 275 | set_device_ro(bdev, rbd_dev->mapping.read_only); |
340c7a2b | 276 | |
602adf40 YS |
277 | return 0; |
278 | } | |
279 | ||
dfc5606d YS |
280 | static int rbd_release(struct gendisk *disk, fmode_t mode) |
281 | { | |
282 | struct rbd_device *rbd_dev = disk->private_data; | |
283 | ||
284 | rbd_put_dev(rbd_dev); | |
285 | ||
286 | return 0; | |
287 | } | |
288 | ||
602adf40 YS |
289 | static const struct block_device_operations rbd_bd_ops = { |
290 | .owner = THIS_MODULE, | |
291 | .open = rbd_open, | |
dfc5606d | 292 | .release = rbd_release, |
602adf40 YS |
293 | }; |
294 | ||
295 | /* | |
296 | * Initialize an rbd client instance. | |
43ae4701 | 297 | * We own *ceph_opts. |
602adf40 | 298 | */ |
f8c38929 | 299 | static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts) |
602adf40 YS |
300 | { |
301 | struct rbd_client *rbdc; | |
302 | int ret = -ENOMEM; | |
303 | ||
304 | dout("rbd_client_create\n"); | |
305 | rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL); | |
306 | if (!rbdc) | |
307 | goto out_opt; | |
308 | ||
309 | kref_init(&rbdc->kref); | |
310 | INIT_LIST_HEAD(&rbdc->node); | |
311 | ||
bc534d86 AE |
312 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); |
313 | ||
43ae4701 | 314 | rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0); |
602adf40 | 315 | if (IS_ERR(rbdc->client)) |
bc534d86 | 316 | goto out_mutex; |
43ae4701 | 317 | ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */ |
602adf40 YS |
318 | |
319 | ret = ceph_open_session(rbdc->client); | |
320 | if (ret < 0) | |
321 | goto out_err; | |
322 | ||
432b8587 | 323 | spin_lock(&rbd_client_list_lock); |
602adf40 | 324 | list_add_tail(&rbdc->node, &rbd_client_list); |
432b8587 | 325 | spin_unlock(&rbd_client_list_lock); |
602adf40 | 326 | |
bc534d86 AE |
327 | mutex_unlock(&ctl_mutex); |
328 | ||
602adf40 YS |
329 | dout("rbd_client_create created %p\n", rbdc); |
330 | return rbdc; | |
331 | ||
332 | out_err: | |
333 | ceph_destroy_client(rbdc->client); | |
bc534d86 AE |
334 | out_mutex: |
335 | mutex_unlock(&ctl_mutex); | |
602adf40 YS |
336 | kfree(rbdc); |
337 | out_opt: | |
43ae4701 AE |
338 | if (ceph_opts) |
339 | ceph_destroy_options(ceph_opts); | |
28f259b7 | 340 | return ERR_PTR(ret); |
602adf40 YS |
341 | } |
342 | ||
343 | /* | |
1f7ba331 AE |
344 | * Find a ceph client with specific addr and configuration. If |
345 | * found, bump its reference count. | |
602adf40 | 346 | */ |
1f7ba331 | 347 | static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts) |
602adf40 YS |
348 | { |
349 | struct rbd_client *client_node; | |
1f7ba331 | 350 | bool found = false; |
602adf40 | 351 | |
43ae4701 | 352 | if (ceph_opts->flags & CEPH_OPT_NOSHARE) |
602adf40 YS |
353 | return NULL; |
354 | ||
1f7ba331 AE |
355 | spin_lock(&rbd_client_list_lock); |
356 | list_for_each_entry(client_node, &rbd_client_list, node) { | |
357 | if (!ceph_compare_options(ceph_opts, client_node->client)) { | |
358 | kref_get(&client_node->kref); | |
359 | found = true; | |
360 | break; | |
361 | } | |
362 | } | |
363 | spin_unlock(&rbd_client_list_lock); | |
364 | ||
365 | return found ? client_node : NULL; | |
602adf40 YS |
366 | } |
367 | ||
59c2be1e YS |
368 | /* |
369 | * mount options | |
370 | */ | |
371 | enum { | |
59c2be1e YS |
372 | Opt_last_int, |
373 | /* int args above */ | |
374 | Opt_last_string, | |
375 | /* string args above */ | |
cc0538b6 AE |
376 | Opt_read_only, |
377 | Opt_read_write, | |
378 | /* Boolean args above */ | |
379 | Opt_last_bool, | |
59c2be1e YS |
380 | }; |
381 | ||
43ae4701 | 382 | static match_table_t rbd_opts_tokens = { |
59c2be1e YS |
383 | /* int args above */ |
384 | /* string args above */ | |
f84344f3 | 385 | {Opt_read_only, "mapping.read_only"}, |
cc0538b6 AE |
386 | {Opt_read_only, "ro"}, /* Alternate spelling */ |
387 | {Opt_read_write, "read_write"}, | |
388 | {Opt_read_write, "rw"}, /* Alternate spelling */ | |
389 | /* Boolean args above */ | |
59c2be1e YS |
390 | {-1, NULL} |
391 | }; | |
392 | ||
393 | static int parse_rbd_opts_token(char *c, void *private) | |
394 | { | |
43ae4701 | 395 | struct rbd_options *rbd_opts = private; |
59c2be1e YS |
396 | substring_t argstr[MAX_OPT_ARGS]; |
397 | int token, intval, ret; | |
398 | ||
43ae4701 | 399 | token = match_token(c, rbd_opts_tokens, argstr); |
59c2be1e YS |
400 | if (token < 0) |
401 | return -EINVAL; | |
402 | ||
403 | if (token < Opt_last_int) { | |
404 | ret = match_int(&argstr[0], &intval); | |
405 | if (ret < 0) { | |
406 | pr_err("bad mount option arg (not int) " | |
407 | "at '%s'\n", c); | |
408 | return ret; | |
409 | } | |
410 | dout("got int token %d val %d\n", token, intval); | |
411 | } else if (token > Opt_last_int && token < Opt_last_string) { | |
412 | dout("got string token %d val %s\n", token, | |
413 | argstr[0].from); | |
cc0538b6 AE |
414 | } else if (token > Opt_last_string && token < Opt_last_bool) { |
415 | dout("got Boolean token %d\n", token); | |
59c2be1e YS |
416 | } else { |
417 | dout("got token %d\n", token); | |
418 | } | |
419 | ||
420 | switch (token) { | |
cc0538b6 AE |
421 | case Opt_read_only: |
422 | rbd_opts->read_only = true; | |
423 | break; | |
424 | case Opt_read_write: | |
425 | rbd_opts->read_only = false; | |
426 | break; | |
59c2be1e | 427 | default: |
aafb230e AE |
428 | rbd_assert(false); |
429 | break; | |
59c2be1e YS |
430 | } |
431 | return 0; | |
432 | } | |
433 | ||
602adf40 YS |
434 | /* |
435 | * Get a ceph client with specific addr and configuration, if one does | |
436 | * not exist create it. | |
437 | */ | |
f8c38929 AE |
438 | static int rbd_get_client(struct rbd_device *rbd_dev, const char *mon_addr, |
439 | size_t mon_addr_len, char *options) | |
602adf40 | 440 | { |
f8c38929 | 441 | struct rbd_options *rbd_opts = &rbd_dev->rbd_opts; |
43ae4701 | 442 | struct ceph_options *ceph_opts; |
f8c38929 | 443 | struct rbd_client *rbdc; |
59c2be1e | 444 | |
cc0538b6 | 445 | rbd_opts->read_only = RBD_READ_ONLY_DEFAULT; |
602adf40 | 446 | |
43ae4701 AE |
447 | ceph_opts = ceph_parse_options(options, mon_addr, |
448 | mon_addr + mon_addr_len, | |
449 | parse_rbd_opts_token, rbd_opts); | |
f8c38929 AE |
450 | if (IS_ERR(ceph_opts)) |
451 | return PTR_ERR(ceph_opts); | |
602adf40 | 452 | |
1f7ba331 | 453 | rbdc = rbd_client_find(ceph_opts); |
602adf40 | 454 | if (rbdc) { |
602adf40 | 455 | /* using an existing client */ |
43ae4701 | 456 | ceph_destroy_options(ceph_opts); |
f8c38929 AE |
457 | } else { |
458 | rbdc = rbd_client_create(ceph_opts); | |
459 | if (IS_ERR(rbdc)) | |
460 | return PTR_ERR(rbdc); | |
602adf40 | 461 | } |
f8c38929 | 462 | rbd_dev->rbd_client = rbdc; |
602adf40 | 463 | |
f8c38929 | 464 | return 0; |
602adf40 YS |
465 | } |
466 | ||
467 | /* | |
468 | * Destroy ceph client | |
d23a4b3f | 469 | * |
432b8587 | 470 | * Caller must hold rbd_client_list_lock. |
602adf40 YS |
471 | */ |
472 | static void rbd_client_release(struct kref *kref) | |
473 | { | |
474 | struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref); | |
475 | ||
476 | dout("rbd_release_client %p\n", rbdc); | |
cd9d9f5d | 477 | spin_lock(&rbd_client_list_lock); |
602adf40 | 478 | list_del(&rbdc->node); |
cd9d9f5d | 479 | spin_unlock(&rbd_client_list_lock); |
602adf40 YS |
480 | |
481 | ceph_destroy_client(rbdc->client); | |
482 | kfree(rbdc); | |
483 | } | |
484 | ||
485 | /* | |
486 | * Drop reference to ceph client node. If it's not referenced anymore, release | |
487 | * it. | |
488 | */ | |
489 | static void rbd_put_client(struct rbd_device *rbd_dev) | |
490 | { | |
491 | kref_put(&rbd_dev->rbd_client->kref, rbd_client_release); | |
492 | rbd_dev->rbd_client = NULL; | |
602adf40 YS |
493 | } |
494 | ||
1fec7093 YS |
495 | /* |
496 | * Destroy requests collection | |
497 | */ | |
498 | static void rbd_coll_release(struct kref *kref) | |
499 | { | |
500 | struct rbd_req_coll *coll = | |
501 | container_of(kref, struct rbd_req_coll, kref); | |
502 | ||
503 | dout("rbd_coll_release %p\n", coll); | |
504 | kfree(coll); | |
505 | } | |
602adf40 | 506 | |
8e94af8e AE |
507 | static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk) |
508 | { | |
103a150f AE |
509 | size_t size; |
510 | u32 snap_count; | |
511 | ||
512 | /* The header has to start with the magic rbd header text */ | |
513 | if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT))) | |
514 | return false; | |
515 | ||
516 | /* | |
517 | * The size of a snapshot header has to fit in a size_t, and | |
518 | * that limits the number of snapshots. | |
519 | */ | |
520 | snap_count = le32_to_cpu(ondisk->snap_count); | |
521 | size = SIZE_MAX - sizeof (struct ceph_snap_context); | |
522 | if (snap_count > size / sizeof (__le64)) | |
523 | return false; | |
524 | ||
525 | /* | |
526 | * Not only that, but the size of the entire the snapshot | |
527 | * header must also be representable in a size_t. | |
528 | */ | |
529 | size -= snap_count * sizeof (__le64); | |
530 | if ((u64) size < le64_to_cpu(ondisk->snap_names_len)) | |
531 | return false; | |
532 | ||
533 | return true; | |
8e94af8e AE |
534 | } |
535 | ||
602adf40 YS |
536 | /* |
537 | * Create a new header structure, translate header format from the on-disk | |
538 | * header. | |
539 | */ | |
540 | static int rbd_header_from_disk(struct rbd_image_header *header, | |
4156d998 | 541 | struct rbd_image_header_ondisk *ondisk) |
602adf40 | 542 | { |
ccece235 | 543 | u32 snap_count; |
58c17b0e | 544 | size_t len; |
d2bb24e5 | 545 | size_t size; |
621901d6 | 546 | u32 i; |
602adf40 | 547 | |
6a52325f AE |
548 | memset(header, 0, sizeof (*header)); |
549 | ||
103a150f AE |
550 | snap_count = le32_to_cpu(ondisk->snap_count); |
551 | ||
58c17b0e AE |
552 | len = strnlen(ondisk->object_prefix, sizeof (ondisk->object_prefix)); |
553 | header->object_prefix = kmalloc(len + 1, GFP_KERNEL); | |
6a52325f | 554 | if (!header->object_prefix) |
602adf40 | 555 | return -ENOMEM; |
58c17b0e AE |
556 | memcpy(header->object_prefix, ondisk->object_prefix, len); |
557 | header->object_prefix[len] = '\0'; | |
00f1f36f | 558 | |
602adf40 | 559 | if (snap_count) { |
f785cc1d AE |
560 | u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len); |
561 | ||
621901d6 AE |
562 | /* Save a copy of the snapshot names */ |
563 | ||
f785cc1d AE |
564 | if (snap_names_len > (u64) SIZE_MAX) |
565 | return -EIO; | |
566 | header->snap_names = kmalloc(snap_names_len, GFP_KERNEL); | |
602adf40 | 567 | if (!header->snap_names) |
6a52325f | 568 | goto out_err; |
f785cc1d AE |
569 | /* |
570 | * Note that rbd_dev_v1_header_read() guarantees | |
571 | * the ondisk buffer we're working with has | |
572 | * snap_names_len bytes beyond the end of the | |
573 | * snapshot id array, this memcpy() is safe. | |
574 | */ | |
575 | memcpy(header->snap_names, &ondisk->snaps[snap_count], | |
576 | snap_names_len); | |
6a52325f | 577 | |
621901d6 AE |
578 | /* Record each snapshot's size */ |
579 | ||
d2bb24e5 AE |
580 | size = snap_count * sizeof (*header->snap_sizes); |
581 | header->snap_sizes = kmalloc(size, GFP_KERNEL); | |
602adf40 | 582 | if (!header->snap_sizes) |
6a52325f | 583 | goto out_err; |
621901d6 AE |
584 | for (i = 0; i < snap_count; i++) |
585 | header->snap_sizes[i] = | |
586 | le64_to_cpu(ondisk->snaps[i].image_size); | |
602adf40 | 587 | } else { |
ccece235 | 588 | WARN_ON(ondisk->snap_names_len); |
602adf40 YS |
589 | header->snap_names = NULL; |
590 | header->snap_sizes = NULL; | |
591 | } | |
849b4260 | 592 | |
602adf40 YS |
593 | header->obj_order = ondisk->options.order; |
594 | header->crypt_type = ondisk->options.crypt_type; | |
595 | header->comp_type = ondisk->options.comp_type; | |
6a52325f | 596 | |
621901d6 AE |
597 | /* Allocate and fill in the snapshot context */ |
598 | ||
f84344f3 | 599 | header->image_size = le64_to_cpu(ondisk->image_size); |
6a52325f AE |
600 | size = sizeof (struct ceph_snap_context); |
601 | size += snap_count * sizeof (header->snapc->snaps[0]); | |
602 | header->snapc = kzalloc(size, GFP_KERNEL); | |
603 | if (!header->snapc) | |
604 | goto out_err; | |
602adf40 YS |
605 | |
606 | atomic_set(&header->snapc->nref, 1); | |
505cbb9b | 607 | header->snapc->seq = le64_to_cpu(ondisk->snap_seq); |
602adf40 | 608 | header->snapc->num_snaps = snap_count; |
621901d6 AE |
609 | for (i = 0; i < snap_count; i++) |
610 | header->snapc->snaps[i] = | |
611 | le64_to_cpu(ondisk->snaps[i].id); | |
602adf40 YS |
612 | |
613 | return 0; | |
614 | ||
6a52325f | 615 | out_err: |
849b4260 | 616 | kfree(header->snap_sizes); |
ccece235 | 617 | header->snap_sizes = NULL; |
602adf40 | 618 | kfree(header->snap_names); |
ccece235 | 619 | header->snap_names = NULL; |
6a52325f AE |
620 | kfree(header->object_prefix); |
621 | header->object_prefix = NULL; | |
ccece235 | 622 | |
00f1f36f | 623 | return -ENOMEM; |
602adf40 YS |
624 | } |
625 | ||
8836b995 | 626 | static int snap_by_name(struct rbd_device *rbd_dev, const char *snap_name) |
602adf40 | 627 | { |
602adf40 | 628 | |
e86924a8 | 629 | struct rbd_snap *snap; |
602adf40 | 630 | |
e86924a8 AE |
631 | list_for_each_entry(snap, &rbd_dev->snaps, node) { |
632 | if (!strcmp(snap_name, snap->name)) { | |
633 | rbd_dev->mapping.snap_id = snap->id; | |
634 | rbd_dev->mapping.size = snap->size; | |
602adf40 | 635 | |
e86924a8 | 636 | return 0; |
00f1f36f | 637 | } |
00f1f36f | 638 | } |
e86924a8 | 639 | |
00f1f36f | 640 | return -ENOENT; |
602adf40 YS |
641 | } |
642 | ||
5ed16177 | 643 | static int rbd_dev_set_mapping(struct rbd_device *rbd_dev, char *snap_name) |
602adf40 | 644 | { |
78dc447d | 645 | int ret; |
602adf40 | 646 | |
4e1105a2 | 647 | if (!memcmp(snap_name, RBD_SNAP_HEAD_NAME, |
cc9d734c | 648 | sizeof (RBD_SNAP_HEAD_NAME))) { |
f84344f3 | 649 | rbd_dev->mapping.snap_id = CEPH_NOSNAP; |
99c1f08f | 650 | rbd_dev->mapping.size = rbd_dev->header.image_size; |
f84344f3 AE |
651 | rbd_dev->mapping.snap_exists = false; |
652 | rbd_dev->mapping.read_only = rbd_dev->rbd_opts.read_only; | |
e86924a8 | 653 | ret = 0; |
602adf40 | 654 | } else { |
8836b995 | 655 | ret = snap_by_name(rbd_dev, snap_name); |
602adf40 YS |
656 | if (ret < 0) |
657 | goto done; | |
f84344f3 AE |
658 | rbd_dev->mapping.snap_exists = true; |
659 | rbd_dev->mapping.read_only = true; | |
602adf40 | 660 | } |
4e1105a2 | 661 | rbd_dev->mapping.snap_name = snap_name; |
602adf40 | 662 | done: |
602adf40 YS |
663 | return ret; |
664 | } | |
665 | ||
666 | static void rbd_header_free(struct rbd_image_header *header) | |
667 | { | |
849b4260 | 668 | kfree(header->object_prefix); |
d78fd7ae | 669 | header->object_prefix = NULL; |
602adf40 | 670 | kfree(header->snap_sizes); |
d78fd7ae | 671 | header->snap_sizes = NULL; |
849b4260 | 672 | kfree(header->snap_names); |
d78fd7ae | 673 | header->snap_names = NULL; |
d1d25646 | 674 | ceph_put_snap_context(header->snapc); |
d78fd7ae | 675 | header->snapc = NULL; |
602adf40 YS |
676 | } |
677 | ||
65ccfe21 | 678 | static char *rbd_segment_name(struct rbd_device *rbd_dev, u64 offset) |
602adf40 | 679 | { |
65ccfe21 AE |
680 | char *name; |
681 | u64 segment; | |
682 | int ret; | |
602adf40 | 683 | |
65ccfe21 AE |
684 | name = kmalloc(RBD_MAX_SEG_NAME_LEN + 1, GFP_NOIO); |
685 | if (!name) | |
686 | return NULL; | |
687 | segment = offset >> rbd_dev->header.obj_order; | |
688 | ret = snprintf(name, RBD_MAX_SEG_NAME_LEN, "%s.%012llx", | |
689 | rbd_dev->header.object_prefix, segment); | |
690 | if (ret < 0 || ret >= RBD_MAX_SEG_NAME_LEN) { | |
691 | pr_err("error formatting segment name for #%llu (%d)\n", | |
692 | segment, ret); | |
693 | kfree(name); | |
694 | name = NULL; | |
695 | } | |
602adf40 | 696 | |
65ccfe21 AE |
697 | return name; |
698 | } | |
602adf40 | 699 | |
65ccfe21 AE |
700 | static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset) |
701 | { | |
702 | u64 segment_size = (u64) 1 << rbd_dev->header.obj_order; | |
602adf40 | 703 | |
65ccfe21 AE |
704 | return offset & (segment_size - 1); |
705 | } | |
706 | ||
707 | static u64 rbd_segment_length(struct rbd_device *rbd_dev, | |
708 | u64 offset, u64 length) | |
709 | { | |
710 | u64 segment_size = (u64) 1 << rbd_dev->header.obj_order; | |
711 | ||
712 | offset &= segment_size - 1; | |
713 | ||
aafb230e | 714 | rbd_assert(length <= U64_MAX - offset); |
65ccfe21 AE |
715 | if (offset + length > segment_size) |
716 | length = segment_size - offset; | |
717 | ||
718 | return length; | |
602adf40 YS |
719 | } |
720 | ||
1fec7093 YS |
721 | static int rbd_get_num_segments(struct rbd_image_header *header, |
722 | u64 ofs, u64 len) | |
723 | { | |
df111be6 AE |
724 | u64 start_seg; |
725 | u64 end_seg; | |
726 | ||
727 | if (!len) | |
728 | return 0; | |
729 | if (len - 1 > U64_MAX - ofs) | |
730 | return -ERANGE; | |
731 | ||
732 | start_seg = ofs >> header->obj_order; | |
733 | end_seg = (ofs + len - 1) >> header->obj_order; | |
734 | ||
1fec7093 YS |
735 | return end_seg - start_seg + 1; |
736 | } | |
737 | ||
029bcbd8 JD |
738 | /* |
739 | * returns the size of an object in the image | |
740 | */ | |
741 | static u64 rbd_obj_bytes(struct rbd_image_header *header) | |
742 | { | |
743 | return 1 << header->obj_order; | |
744 | } | |
745 | ||
602adf40 YS |
746 | /* |
747 | * bio helpers | |
748 | */ | |
749 | ||
750 | static void bio_chain_put(struct bio *chain) | |
751 | { | |
752 | struct bio *tmp; | |
753 | ||
754 | while (chain) { | |
755 | tmp = chain; | |
756 | chain = chain->bi_next; | |
757 | bio_put(tmp); | |
758 | } | |
759 | } | |
760 | ||
761 | /* | |
762 | * zeros a bio chain, starting at specific offset | |
763 | */ | |
764 | static void zero_bio_chain(struct bio *chain, int start_ofs) | |
765 | { | |
766 | struct bio_vec *bv; | |
767 | unsigned long flags; | |
768 | void *buf; | |
769 | int i; | |
770 | int pos = 0; | |
771 | ||
772 | while (chain) { | |
773 | bio_for_each_segment(bv, chain, i) { | |
774 | if (pos + bv->bv_len > start_ofs) { | |
775 | int remainder = max(start_ofs - pos, 0); | |
776 | buf = bvec_kmap_irq(bv, &flags); | |
777 | memset(buf + remainder, 0, | |
778 | bv->bv_len - remainder); | |
85b5aaa6 | 779 | bvec_kunmap_irq(buf, &flags); |
602adf40 YS |
780 | } |
781 | pos += bv->bv_len; | |
782 | } | |
783 | ||
784 | chain = chain->bi_next; | |
785 | } | |
786 | } | |
787 | ||
788 | /* | |
789 | * bio_chain_clone - clone a chain of bios up to a certain length. | |
790 | * might return a bio_pair that will need to be released. | |
791 | */ | |
792 | static struct bio *bio_chain_clone(struct bio **old, struct bio **next, | |
793 | struct bio_pair **bp, | |
794 | int len, gfp_t gfpmask) | |
795 | { | |
542582fc AE |
796 | struct bio *old_chain = *old; |
797 | struct bio *new_chain = NULL; | |
798 | struct bio *tail; | |
602adf40 YS |
799 | int total = 0; |
800 | ||
801 | if (*bp) { | |
802 | bio_pair_release(*bp); | |
803 | *bp = NULL; | |
804 | } | |
805 | ||
806 | while (old_chain && (total < len)) { | |
542582fc AE |
807 | struct bio *tmp; |
808 | ||
602adf40 YS |
809 | tmp = bio_kmalloc(gfpmask, old_chain->bi_max_vecs); |
810 | if (!tmp) | |
811 | goto err_out; | |
542582fc | 812 | gfpmask &= ~__GFP_WAIT; /* can't wait after the first */ |
602adf40 YS |
813 | |
814 | if (total + old_chain->bi_size > len) { | |
815 | struct bio_pair *bp; | |
816 | ||
817 | /* | |
818 | * this split can only happen with a single paged bio, | |
819 | * split_bio will BUG_ON if this is not the case | |
820 | */ | |
821 | dout("bio_chain_clone split! total=%d remaining=%d" | |
bd919d45 AE |
822 | "bi_size=%u\n", |
823 | total, len - total, old_chain->bi_size); | |
602adf40 YS |
824 | |
825 | /* split the bio. We'll release it either in the next | |
826 | call, or it will have to be released outside */ | |
593a9e7b | 827 | bp = bio_split(old_chain, (len - total) / SECTOR_SIZE); |
602adf40 YS |
828 | if (!bp) |
829 | goto err_out; | |
830 | ||
831 | __bio_clone(tmp, &bp->bio1); | |
832 | ||
833 | *next = &bp->bio2; | |
834 | } else { | |
835 | __bio_clone(tmp, old_chain); | |
836 | *next = old_chain->bi_next; | |
837 | } | |
838 | ||
839 | tmp->bi_bdev = NULL; | |
602adf40 | 840 | tmp->bi_next = NULL; |
542582fc | 841 | if (new_chain) |
602adf40 | 842 | tail->bi_next = tmp; |
542582fc AE |
843 | else |
844 | new_chain = tmp; | |
845 | tail = tmp; | |
602adf40 YS |
846 | old_chain = old_chain->bi_next; |
847 | ||
848 | total += tmp->bi_size; | |
849 | } | |
850 | ||
aafb230e | 851 | rbd_assert(total == len); |
602adf40 | 852 | |
602adf40 YS |
853 | *old = old_chain; |
854 | ||
855 | return new_chain; | |
856 | ||
857 | err_out: | |
858 | dout("bio_chain_clone with err\n"); | |
859 | bio_chain_put(new_chain); | |
860 | return NULL; | |
861 | } | |
862 | ||
863 | /* | |
864 | * helpers for osd request op vectors. | |
865 | */ | |
57cfc106 AE |
866 | static struct ceph_osd_req_op *rbd_create_rw_ops(int num_ops, |
867 | int opcode, u32 payload_len) | |
602adf40 | 868 | { |
57cfc106 AE |
869 | struct ceph_osd_req_op *ops; |
870 | ||
871 | ops = kzalloc(sizeof (*ops) * (num_ops + 1), GFP_NOIO); | |
872 | if (!ops) | |
873 | return NULL; | |
874 | ||
875 | ops[0].op = opcode; | |
876 | ||
602adf40 YS |
877 | /* |
878 | * op extent offset and length will be set later on | |
879 | * in calc_raw_layout() | |
880 | */ | |
57cfc106 AE |
881 | ops[0].payload_len = payload_len; |
882 | ||
883 | return ops; | |
602adf40 YS |
884 | } |
885 | ||
886 | static void rbd_destroy_ops(struct ceph_osd_req_op *ops) | |
887 | { | |
888 | kfree(ops); | |
889 | } | |
890 | ||
1fec7093 YS |
891 | static void rbd_coll_end_req_index(struct request *rq, |
892 | struct rbd_req_coll *coll, | |
893 | int index, | |
894 | int ret, u64 len) | |
895 | { | |
896 | struct request_queue *q; | |
897 | int min, max, i; | |
898 | ||
bd919d45 AE |
899 | dout("rbd_coll_end_req_index %p index %d ret %d len %llu\n", |
900 | coll, index, ret, (unsigned long long) len); | |
1fec7093 YS |
901 | |
902 | if (!rq) | |
903 | return; | |
904 | ||
905 | if (!coll) { | |
906 | blk_end_request(rq, ret, len); | |
907 | return; | |
908 | } | |
909 | ||
910 | q = rq->q; | |
911 | ||
912 | spin_lock_irq(q->queue_lock); | |
913 | coll->status[index].done = 1; | |
914 | coll->status[index].rc = ret; | |
915 | coll->status[index].bytes = len; | |
916 | max = min = coll->num_done; | |
917 | while (max < coll->total && coll->status[max].done) | |
918 | max++; | |
919 | ||
920 | for (i = min; i<max; i++) { | |
921 | __blk_end_request(rq, coll->status[i].rc, | |
922 | coll->status[i].bytes); | |
923 | coll->num_done++; | |
924 | kref_put(&coll->kref, rbd_coll_release); | |
925 | } | |
926 | spin_unlock_irq(q->queue_lock); | |
927 | } | |
928 | ||
929 | static void rbd_coll_end_req(struct rbd_request *req, | |
930 | int ret, u64 len) | |
931 | { | |
932 | rbd_coll_end_req_index(req->rq, req->coll, req->coll_index, ret, len); | |
933 | } | |
934 | ||
602adf40 YS |
935 | /* |
936 | * Send ceph osd request | |
937 | */ | |
938 | static int rbd_do_request(struct request *rq, | |
0ce1a794 | 939 | struct rbd_device *rbd_dev, |
602adf40 YS |
940 | struct ceph_snap_context *snapc, |
941 | u64 snapid, | |
aded07ea | 942 | const char *object_name, u64 ofs, u64 len, |
602adf40 YS |
943 | struct bio *bio, |
944 | struct page **pages, | |
945 | int num_pages, | |
946 | int flags, | |
947 | struct ceph_osd_req_op *ops, | |
1fec7093 YS |
948 | struct rbd_req_coll *coll, |
949 | int coll_index, | |
602adf40 | 950 | void (*rbd_cb)(struct ceph_osd_request *req, |
59c2be1e YS |
951 | struct ceph_msg *msg), |
952 | struct ceph_osd_request **linger_req, | |
953 | u64 *ver) | |
602adf40 YS |
954 | { |
955 | struct ceph_osd_request *req; | |
956 | struct ceph_file_layout *layout; | |
957 | int ret; | |
958 | u64 bno; | |
959 | struct timespec mtime = CURRENT_TIME; | |
960 | struct rbd_request *req_data; | |
961 | struct ceph_osd_request_head *reqhead; | |
1dbb4399 | 962 | struct ceph_osd_client *osdc; |
602adf40 | 963 | |
602adf40 | 964 | req_data = kzalloc(sizeof(*req_data), GFP_NOIO); |
1fec7093 YS |
965 | if (!req_data) { |
966 | if (coll) | |
967 | rbd_coll_end_req_index(rq, coll, coll_index, | |
968 | -ENOMEM, len); | |
969 | return -ENOMEM; | |
970 | } | |
971 | ||
972 | if (coll) { | |
973 | req_data->coll = coll; | |
974 | req_data->coll_index = coll_index; | |
975 | } | |
602adf40 | 976 | |
bd919d45 AE |
977 | dout("rbd_do_request object_name=%s ofs=%llu len=%llu\n", object_name, |
978 | (unsigned long long) ofs, (unsigned long long) len); | |
602adf40 | 979 | |
0ce1a794 | 980 | osdc = &rbd_dev->rbd_client->client->osdc; |
1dbb4399 AE |
981 | req = ceph_osdc_alloc_request(osdc, flags, snapc, ops, |
982 | false, GFP_NOIO, pages, bio); | |
4ad12621 | 983 | if (!req) { |
4ad12621 | 984 | ret = -ENOMEM; |
602adf40 YS |
985 | goto done_pages; |
986 | } | |
987 | ||
988 | req->r_callback = rbd_cb; | |
989 | ||
990 | req_data->rq = rq; | |
991 | req_data->bio = bio; | |
992 | req_data->pages = pages; | |
993 | req_data->len = len; | |
994 | ||
995 | req->r_priv = req_data; | |
996 | ||
997 | reqhead = req->r_request->front.iov_base; | |
998 | reqhead->snapid = cpu_to_le64(CEPH_NOSNAP); | |
999 | ||
aded07ea | 1000 | strncpy(req->r_oid, object_name, sizeof(req->r_oid)); |
602adf40 YS |
1001 | req->r_oid_len = strlen(req->r_oid); |
1002 | ||
1003 | layout = &req->r_file_layout; | |
1004 | memset(layout, 0, sizeof(*layout)); | |
1005 | layout->fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER); | |
1006 | layout->fl_stripe_count = cpu_to_le32(1); | |
1007 | layout->fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER); | |
0ce1a794 | 1008 | layout->fl_pg_pool = cpu_to_le32(rbd_dev->pool_id); |
1dbb4399 AE |
1009 | ceph_calc_raw_layout(osdc, layout, snapid, ofs, &len, &bno, |
1010 | req, ops); | |
602adf40 YS |
1011 | |
1012 | ceph_osdc_build_request(req, ofs, &len, | |
1013 | ops, | |
1014 | snapc, | |
1015 | &mtime, | |
1016 | req->r_oid, req->r_oid_len); | |
602adf40 | 1017 | |
59c2be1e | 1018 | if (linger_req) { |
1dbb4399 | 1019 | ceph_osdc_set_request_linger(osdc, req); |
59c2be1e YS |
1020 | *linger_req = req; |
1021 | } | |
1022 | ||
1dbb4399 | 1023 | ret = ceph_osdc_start_request(osdc, req, false); |
602adf40 YS |
1024 | if (ret < 0) |
1025 | goto done_err; | |
1026 | ||
1027 | if (!rbd_cb) { | |
1dbb4399 | 1028 | ret = ceph_osdc_wait_request(osdc, req); |
59c2be1e YS |
1029 | if (ver) |
1030 | *ver = le64_to_cpu(req->r_reassert_version.version); | |
bd919d45 AE |
1031 | dout("reassert_ver=%llu\n", |
1032 | (unsigned long long) | |
1033 | le64_to_cpu(req->r_reassert_version.version)); | |
602adf40 YS |
1034 | ceph_osdc_put_request(req); |
1035 | } | |
1036 | return ret; | |
1037 | ||
1038 | done_err: | |
1039 | bio_chain_put(req_data->bio); | |
1040 | ceph_osdc_put_request(req); | |
1041 | done_pages: | |
1fec7093 | 1042 | rbd_coll_end_req(req_data, ret, len); |
602adf40 | 1043 | kfree(req_data); |
602adf40 YS |
1044 | return ret; |
1045 | } | |
1046 | ||
1047 | /* | |
1048 | * Ceph osd op callback | |
1049 | */ | |
1050 | static void rbd_req_cb(struct ceph_osd_request *req, struct ceph_msg *msg) | |
1051 | { | |
1052 | struct rbd_request *req_data = req->r_priv; | |
1053 | struct ceph_osd_reply_head *replyhead; | |
1054 | struct ceph_osd_op *op; | |
1055 | __s32 rc; | |
1056 | u64 bytes; | |
1057 | int read_op; | |
1058 | ||
1059 | /* parse reply */ | |
1060 | replyhead = msg->front.iov_base; | |
1061 | WARN_ON(le32_to_cpu(replyhead->num_ops) == 0); | |
1062 | op = (void *)(replyhead + 1); | |
1063 | rc = le32_to_cpu(replyhead->result); | |
1064 | bytes = le64_to_cpu(op->extent.length); | |
895cfcc8 | 1065 | read_op = (le16_to_cpu(op->op) == CEPH_OSD_OP_READ); |
602adf40 | 1066 | |
bd919d45 AE |
1067 | dout("rbd_req_cb bytes=%llu readop=%d rc=%d\n", |
1068 | (unsigned long long) bytes, read_op, (int) rc); | |
602adf40 YS |
1069 | |
1070 | if (rc == -ENOENT && read_op) { | |
1071 | zero_bio_chain(req_data->bio, 0); | |
1072 | rc = 0; | |
1073 | } else if (rc == 0 && read_op && bytes < req_data->len) { | |
1074 | zero_bio_chain(req_data->bio, bytes); | |
1075 | bytes = req_data->len; | |
1076 | } | |
1077 | ||
1fec7093 | 1078 | rbd_coll_end_req(req_data, rc, bytes); |
602adf40 YS |
1079 | |
1080 | if (req_data->bio) | |
1081 | bio_chain_put(req_data->bio); | |
1082 | ||
1083 | ceph_osdc_put_request(req); | |
1084 | kfree(req_data); | |
1085 | } | |
1086 | ||
59c2be1e YS |
1087 | static void rbd_simple_req_cb(struct ceph_osd_request *req, struct ceph_msg *msg) |
1088 | { | |
1089 | ceph_osdc_put_request(req); | |
1090 | } | |
1091 | ||
602adf40 YS |
1092 | /* |
1093 | * Do a synchronous ceph osd operation | |
1094 | */ | |
0ce1a794 | 1095 | static int rbd_req_sync_op(struct rbd_device *rbd_dev, |
602adf40 YS |
1096 | struct ceph_snap_context *snapc, |
1097 | u64 snapid, | |
602adf40 | 1098 | int flags, |
913d2fdc | 1099 | struct ceph_osd_req_op *ops, |
aded07ea | 1100 | const char *object_name, |
f8d4de6e AE |
1101 | u64 ofs, u64 inbound_size, |
1102 | char *inbound, | |
59c2be1e YS |
1103 | struct ceph_osd_request **linger_req, |
1104 | u64 *ver) | |
602adf40 YS |
1105 | { |
1106 | int ret; | |
1107 | struct page **pages; | |
1108 | int num_pages; | |
913d2fdc | 1109 | |
aafb230e | 1110 | rbd_assert(ops != NULL); |
602adf40 | 1111 | |
f8d4de6e | 1112 | num_pages = calc_pages_for(ofs, inbound_size); |
602adf40 | 1113 | pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL); |
b8d0638a DC |
1114 | if (IS_ERR(pages)) |
1115 | return PTR_ERR(pages); | |
602adf40 | 1116 | |
0ce1a794 | 1117 | ret = rbd_do_request(NULL, rbd_dev, snapc, snapid, |
f8d4de6e | 1118 | object_name, ofs, inbound_size, NULL, |
602adf40 YS |
1119 | pages, num_pages, |
1120 | flags, | |
1121 | ops, | |
1fec7093 | 1122 | NULL, 0, |
59c2be1e YS |
1123 | NULL, |
1124 | linger_req, ver); | |
602adf40 | 1125 | if (ret < 0) |
913d2fdc | 1126 | goto done; |
602adf40 | 1127 | |
f8d4de6e AE |
1128 | if ((flags & CEPH_OSD_FLAG_READ) && inbound) |
1129 | ret = ceph_copy_from_page_vector(pages, inbound, ofs, ret); | |
602adf40 | 1130 | |
602adf40 YS |
1131 | done: |
1132 | ceph_release_page_vector(pages, num_pages); | |
1133 | return ret; | |
1134 | } | |
1135 | ||
1136 | /* | |
1137 | * Do an asynchronous ceph osd operation | |
1138 | */ | |
1139 | static int rbd_do_op(struct request *rq, | |
0ce1a794 | 1140 | struct rbd_device *rbd_dev, |
602adf40 YS |
1141 | struct ceph_snap_context *snapc, |
1142 | u64 snapid, | |
d1f57ea6 | 1143 | int opcode, int flags, |
602adf40 | 1144 | u64 ofs, u64 len, |
1fec7093 YS |
1145 | struct bio *bio, |
1146 | struct rbd_req_coll *coll, | |
1147 | int coll_index) | |
602adf40 YS |
1148 | { |
1149 | char *seg_name; | |
1150 | u64 seg_ofs; | |
1151 | u64 seg_len; | |
1152 | int ret; | |
1153 | struct ceph_osd_req_op *ops; | |
1154 | u32 payload_len; | |
1155 | ||
65ccfe21 | 1156 | seg_name = rbd_segment_name(rbd_dev, ofs); |
602adf40 YS |
1157 | if (!seg_name) |
1158 | return -ENOMEM; | |
65ccfe21 AE |
1159 | seg_len = rbd_segment_length(rbd_dev, ofs, len); |
1160 | seg_ofs = rbd_segment_offset(rbd_dev, ofs); | |
602adf40 YS |
1161 | |
1162 | payload_len = (flags & CEPH_OSD_FLAG_WRITE ? seg_len : 0); | |
1163 | ||
57cfc106 AE |
1164 | ret = -ENOMEM; |
1165 | ops = rbd_create_rw_ops(1, opcode, payload_len); | |
1166 | if (!ops) | |
602adf40 YS |
1167 | goto done; |
1168 | ||
1169 | /* we've taken care of segment sizes earlier when we | |
1170 | cloned the bios. We should never have a segment | |
1171 | truncated at this point */ | |
aafb230e | 1172 | rbd_assert(seg_len == len); |
602adf40 YS |
1173 | |
1174 | ret = rbd_do_request(rq, rbd_dev, snapc, snapid, | |
1175 | seg_name, seg_ofs, seg_len, | |
1176 | bio, | |
1177 | NULL, 0, | |
1178 | flags, | |
1179 | ops, | |
1fec7093 | 1180 | coll, coll_index, |
59c2be1e | 1181 | rbd_req_cb, 0, NULL); |
11f77002 SW |
1182 | |
1183 | rbd_destroy_ops(ops); | |
602adf40 YS |
1184 | done: |
1185 | kfree(seg_name); | |
1186 | return ret; | |
1187 | } | |
1188 | ||
1189 | /* | |
1190 | * Request async osd write | |
1191 | */ | |
1192 | static int rbd_req_write(struct request *rq, | |
1193 | struct rbd_device *rbd_dev, | |
1194 | struct ceph_snap_context *snapc, | |
1195 | u64 ofs, u64 len, | |
1fec7093 YS |
1196 | struct bio *bio, |
1197 | struct rbd_req_coll *coll, | |
1198 | int coll_index) | |
602adf40 YS |
1199 | { |
1200 | return rbd_do_op(rq, rbd_dev, snapc, CEPH_NOSNAP, | |
1201 | CEPH_OSD_OP_WRITE, | |
1202 | CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK, | |
1fec7093 | 1203 | ofs, len, bio, coll, coll_index); |
602adf40 YS |
1204 | } |
1205 | ||
1206 | /* | |
1207 | * Request async osd read | |
1208 | */ | |
1209 | static int rbd_req_read(struct request *rq, | |
1210 | struct rbd_device *rbd_dev, | |
1211 | u64 snapid, | |
1212 | u64 ofs, u64 len, | |
1fec7093 YS |
1213 | struct bio *bio, |
1214 | struct rbd_req_coll *coll, | |
1215 | int coll_index) | |
602adf40 YS |
1216 | { |
1217 | return rbd_do_op(rq, rbd_dev, NULL, | |
b06e6a6b | 1218 | snapid, |
602adf40 YS |
1219 | CEPH_OSD_OP_READ, |
1220 | CEPH_OSD_FLAG_READ, | |
1fec7093 | 1221 | ofs, len, bio, coll, coll_index); |
602adf40 YS |
1222 | } |
1223 | ||
1224 | /* | |
1225 | * Request sync osd read | |
1226 | */ | |
0ce1a794 | 1227 | static int rbd_req_sync_read(struct rbd_device *rbd_dev, |
602adf40 | 1228 | u64 snapid, |
aded07ea | 1229 | const char *object_name, |
602adf40 | 1230 | u64 ofs, u64 len, |
59c2be1e YS |
1231 | char *buf, |
1232 | u64 *ver) | |
602adf40 | 1233 | { |
913d2fdc AE |
1234 | struct ceph_osd_req_op *ops; |
1235 | int ret; | |
1236 | ||
1237 | ops = rbd_create_rw_ops(1, CEPH_OSD_OP_READ, 0); | |
1238 | if (!ops) | |
1239 | return -ENOMEM; | |
1240 | ||
1241 | ret = rbd_req_sync_op(rbd_dev, NULL, | |
b06e6a6b | 1242 | snapid, |
602adf40 | 1243 | CEPH_OSD_FLAG_READ, |
913d2fdc AE |
1244 | ops, object_name, ofs, len, buf, NULL, ver); |
1245 | rbd_destroy_ops(ops); | |
1246 | ||
1247 | return ret; | |
602adf40 YS |
1248 | } |
1249 | ||
1250 | /* | |
59c2be1e YS |
1251 | * Request sync osd watch |
1252 | */ | |
0ce1a794 | 1253 | static int rbd_req_sync_notify_ack(struct rbd_device *rbd_dev, |
59c2be1e | 1254 | u64 ver, |
7f0a24d8 | 1255 | u64 notify_id) |
59c2be1e YS |
1256 | { |
1257 | struct ceph_osd_req_op *ops; | |
11f77002 SW |
1258 | int ret; |
1259 | ||
57cfc106 AE |
1260 | ops = rbd_create_rw_ops(1, CEPH_OSD_OP_NOTIFY_ACK, 0); |
1261 | if (!ops) | |
1262 | return -ENOMEM; | |
59c2be1e | 1263 | |
a71b891b | 1264 | ops[0].watch.ver = cpu_to_le64(ver); |
59c2be1e YS |
1265 | ops[0].watch.cookie = notify_id; |
1266 | ops[0].watch.flag = 0; | |
1267 | ||
0ce1a794 | 1268 | ret = rbd_do_request(NULL, rbd_dev, NULL, CEPH_NOSNAP, |
7f0a24d8 | 1269 | rbd_dev->header_name, 0, 0, NULL, |
ad4f232f | 1270 | NULL, 0, |
59c2be1e YS |
1271 | CEPH_OSD_FLAG_READ, |
1272 | ops, | |
1fec7093 | 1273 | NULL, 0, |
59c2be1e YS |
1274 | rbd_simple_req_cb, 0, NULL); |
1275 | ||
1276 | rbd_destroy_ops(ops); | |
1277 | return ret; | |
1278 | } | |
1279 | ||
1280 | static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data) | |
1281 | { | |
0ce1a794 | 1282 | struct rbd_device *rbd_dev = (struct rbd_device *)data; |
a71b891b | 1283 | u64 hver; |
13143d2d SW |
1284 | int rc; |
1285 | ||
0ce1a794 | 1286 | if (!rbd_dev) |
59c2be1e YS |
1287 | return; |
1288 | ||
bd919d45 AE |
1289 | dout("rbd_watch_cb %s notify_id=%llu opcode=%u\n", |
1290 | rbd_dev->header_name, (unsigned long long) notify_id, | |
1291 | (unsigned int) opcode); | |
1fe5e993 | 1292 | rc = rbd_refresh_header(rbd_dev, &hver); |
13143d2d | 1293 | if (rc) |
f0f8cef5 | 1294 | pr_warning(RBD_DRV_NAME "%d got notification but failed to " |
0ce1a794 | 1295 | " update snaps: %d\n", rbd_dev->major, rc); |
59c2be1e | 1296 | |
7f0a24d8 | 1297 | rbd_req_sync_notify_ack(rbd_dev, hver, notify_id); |
59c2be1e YS |
1298 | } |
1299 | ||
1300 | /* | |
1301 | * Request sync osd watch | |
1302 | */ | |
0e6f322d | 1303 | static int rbd_req_sync_watch(struct rbd_device *rbd_dev) |
59c2be1e YS |
1304 | { |
1305 | struct ceph_osd_req_op *ops; | |
0ce1a794 | 1306 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
57cfc106 | 1307 | int ret; |
59c2be1e | 1308 | |
57cfc106 AE |
1309 | ops = rbd_create_rw_ops(1, CEPH_OSD_OP_WATCH, 0); |
1310 | if (!ops) | |
1311 | return -ENOMEM; | |
59c2be1e YS |
1312 | |
1313 | ret = ceph_osdc_create_event(osdc, rbd_watch_cb, 0, | |
0ce1a794 | 1314 | (void *)rbd_dev, &rbd_dev->watch_event); |
59c2be1e YS |
1315 | if (ret < 0) |
1316 | goto fail; | |
1317 | ||
0e6f322d | 1318 | ops[0].watch.ver = cpu_to_le64(rbd_dev->header.obj_version); |
0ce1a794 | 1319 | ops[0].watch.cookie = cpu_to_le64(rbd_dev->watch_event->cookie); |
59c2be1e YS |
1320 | ops[0].watch.flag = 1; |
1321 | ||
0ce1a794 | 1322 | ret = rbd_req_sync_op(rbd_dev, NULL, |
59c2be1e | 1323 | CEPH_NOSNAP, |
59c2be1e YS |
1324 | CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK, |
1325 | ops, | |
0e6f322d AE |
1326 | rbd_dev->header_name, |
1327 | 0, 0, NULL, | |
0ce1a794 | 1328 | &rbd_dev->watch_request, NULL); |
59c2be1e YS |
1329 | |
1330 | if (ret < 0) | |
1331 | goto fail_event; | |
1332 | ||
1333 | rbd_destroy_ops(ops); | |
1334 | return 0; | |
1335 | ||
1336 | fail_event: | |
0ce1a794 AE |
1337 | ceph_osdc_cancel_event(rbd_dev->watch_event); |
1338 | rbd_dev->watch_event = NULL; | |
59c2be1e YS |
1339 | fail: |
1340 | rbd_destroy_ops(ops); | |
1341 | return ret; | |
1342 | } | |
1343 | ||
79e3057c YS |
1344 | /* |
1345 | * Request sync osd unwatch | |
1346 | */ | |
070c633f | 1347 | static int rbd_req_sync_unwatch(struct rbd_device *rbd_dev) |
79e3057c YS |
1348 | { |
1349 | struct ceph_osd_req_op *ops; | |
57cfc106 | 1350 | int ret; |
79e3057c | 1351 | |
57cfc106 AE |
1352 | ops = rbd_create_rw_ops(1, CEPH_OSD_OP_WATCH, 0); |
1353 | if (!ops) | |
1354 | return -ENOMEM; | |
79e3057c YS |
1355 | |
1356 | ops[0].watch.ver = 0; | |
0ce1a794 | 1357 | ops[0].watch.cookie = cpu_to_le64(rbd_dev->watch_event->cookie); |
79e3057c YS |
1358 | ops[0].watch.flag = 0; |
1359 | ||
0ce1a794 | 1360 | ret = rbd_req_sync_op(rbd_dev, NULL, |
79e3057c | 1361 | CEPH_NOSNAP, |
79e3057c YS |
1362 | CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK, |
1363 | ops, | |
070c633f AE |
1364 | rbd_dev->header_name, |
1365 | 0, 0, NULL, NULL, NULL); | |
1366 | ||
79e3057c YS |
1367 | |
1368 | rbd_destroy_ops(ops); | |
0ce1a794 AE |
1369 | ceph_osdc_cancel_event(rbd_dev->watch_event); |
1370 | rbd_dev->watch_event = NULL; | |
79e3057c YS |
1371 | return ret; |
1372 | } | |
1373 | ||
602adf40 | 1374 | /* |
3cb4a687 | 1375 | * Synchronous osd object method call |
602adf40 | 1376 | */ |
0ce1a794 | 1377 | static int rbd_req_sync_exec(struct rbd_device *rbd_dev, |
aded07ea AE |
1378 | const char *object_name, |
1379 | const char *class_name, | |
1380 | const char *method_name, | |
3cb4a687 AE |
1381 | const char *outbound, |
1382 | size_t outbound_size, | |
f8d4de6e AE |
1383 | char *inbound, |
1384 | size_t inbound_size, | |
3cb4a687 | 1385 | int flags, |
59c2be1e | 1386 | u64 *ver) |
602adf40 YS |
1387 | { |
1388 | struct ceph_osd_req_op *ops; | |
aded07ea AE |
1389 | int class_name_len = strlen(class_name); |
1390 | int method_name_len = strlen(method_name); | |
3cb4a687 | 1391 | int payload_size; |
57cfc106 AE |
1392 | int ret; |
1393 | ||
3cb4a687 AE |
1394 | /* |
1395 | * Any input parameters required by the method we're calling | |
1396 | * will be sent along with the class and method names as | |
1397 | * part of the message payload. That data and its size are | |
1398 | * supplied via the indata and indata_len fields (named from | |
1399 | * the perspective of the server side) in the OSD request | |
1400 | * operation. | |
1401 | */ | |
1402 | payload_size = class_name_len + method_name_len + outbound_size; | |
1403 | ops = rbd_create_rw_ops(1, CEPH_OSD_OP_CALL, payload_size); | |
57cfc106 AE |
1404 | if (!ops) |
1405 | return -ENOMEM; | |
602adf40 | 1406 | |
aded07ea AE |
1407 | ops[0].cls.class_name = class_name; |
1408 | ops[0].cls.class_len = (__u8) class_name_len; | |
1409 | ops[0].cls.method_name = method_name; | |
1410 | ops[0].cls.method_len = (__u8) method_name_len; | |
602adf40 | 1411 | ops[0].cls.argc = 0; |
3cb4a687 AE |
1412 | ops[0].cls.indata = outbound; |
1413 | ops[0].cls.indata_len = outbound_size; | |
602adf40 | 1414 | |
0ce1a794 | 1415 | ret = rbd_req_sync_op(rbd_dev, NULL, |
602adf40 | 1416 | CEPH_NOSNAP, |
3cb4a687 | 1417 | flags, ops, |
f8d4de6e AE |
1418 | object_name, 0, inbound_size, inbound, |
1419 | NULL, ver); | |
602adf40 YS |
1420 | |
1421 | rbd_destroy_ops(ops); | |
1422 | ||
1423 | dout("cls_exec returned %d\n", ret); | |
1424 | return ret; | |
1425 | } | |
1426 | ||
1fec7093 YS |
1427 | static struct rbd_req_coll *rbd_alloc_coll(int num_reqs) |
1428 | { | |
1429 | struct rbd_req_coll *coll = | |
1430 | kzalloc(sizeof(struct rbd_req_coll) + | |
1431 | sizeof(struct rbd_req_status) * num_reqs, | |
1432 | GFP_ATOMIC); | |
1433 | ||
1434 | if (!coll) | |
1435 | return NULL; | |
1436 | coll->total = num_reqs; | |
1437 | kref_init(&coll->kref); | |
1438 | return coll; | |
1439 | } | |
1440 | ||
602adf40 YS |
1441 | /* |
1442 | * block device queue callback | |
1443 | */ | |
1444 | static void rbd_rq_fn(struct request_queue *q) | |
1445 | { | |
1446 | struct rbd_device *rbd_dev = q->queuedata; | |
1447 | struct request *rq; | |
1448 | struct bio_pair *bp = NULL; | |
1449 | ||
00f1f36f | 1450 | while ((rq = blk_fetch_request(q))) { |
602adf40 YS |
1451 | struct bio *bio; |
1452 | struct bio *rq_bio, *next_bio = NULL; | |
1453 | bool do_write; | |
bd919d45 AE |
1454 | unsigned int size; |
1455 | u64 op_size = 0; | |
602adf40 | 1456 | u64 ofs; |
1fec7093 YS |
1457 | int num_segs, cur_seg = 0; |
1458 | struct rbd_req_coll *coll; | |
d1d25646 | 1459 | struct ceph_snap_context *snapc; |
602adf40 | 1460 | |
602adf40 YS |
1461 | dout("fetched request\n"); |
1462 | ||
1463 | /* filter out block requests we don't understand */ | |
1464 | if ((rq->cmd_type != REQ_TYPE_FS)) { | |
1465 | __blk_end_request_all(rq, 0); | |
00f1f36f | 1466 | continue; |
602adf40 YS |
1467 | } |
1468 | ||
1469 | /* deduce our operation (read, write) */ | |
1470 | do_write = (rq_data_dir(rq) == WRITE); | |
1471 | ||
1472 | size = blk_rq_bytes(rq); | |
593a9e7b | 1473 | ofs = blk_rq_pos(rq) * SECTOR_SIZE; |
602adf40 | 1474 | rq_bio = rq->bio; |
f84344f3 | 1475 | if (do_write && rbd_dev->mapping.read_only) { |
602adf40 | 1476 | __blk_end_request_all(rq, -EROFS); |
00f1f36f | 1477 | continue; |
602adf40 YS |
1478 | } |
1479 | ||
1480 | spin_unlock_irq(q->queue_lock); | |
1481 | ||
d1d25646 | 1482 | down_read(&rbd_dev->header_rwsem); |
e88a36ec | 1483 | |
f84344f3 AE |
1484 | if (rbd_dev->mapping.snap_id != CEPH_NOSNAP && |
1485 | !rbd_dev->mapping.snap_exists) { | |
e88a36ec | 1486 | up_read(&rbd_dev->header_rwsem); |
d1d25646 JD |
1487 | dout("request for non-existent snapshot"); |
1488 | spin_lock_irq(q->queue_lock); | |
1489 | __blk_end_request_all(rq, -ENXIO); | |
1490 | continue; | |
e88a36ec JD |
1491 | } |
1492 | ||
d1d25646 JD |
1493 | snapc = ceph_get_snap_context(rbd_dev->header.snapc); |
1494 | ||
1495 | up_read(&rbd_dev->header_rwsem); | |
1496 | ||
602adf40 YS |
1497 | dout("%s 0x%x bytes at 0x%llx\n", |
1498 | do_write ? "write" : "read", | |
bd919d45 | 1499 | size, (unsigned long long) blk_rq_pos(rq) * SECTOR_SIZE); |
602adf40 | 1500 | |
1fec7093 | 1501 | num_segs = rbd_get_num_segments(&rbd_dev->header, ofs, size); |
df111be6 AE |
1502 | if (num_segs <= 0) { |
1503 | spin_lock_irq(q->queue_lock); | |
1504 | __blk_end_request_all(rq, num_segs); | |
1505 | ceph_put_snap_context(snapc); | |
1506 | continue; | |
1507 | } | |
1fec7093 YS |
1508 | coll = rbd_alloc_coll(num_segs); |
1509 | if (!coll) { | |
1510 | spin_lock_irq(q->queue_lock); | |
1511 | __blk_end_request_all(rq, -ENOMEM); | |
d1d25646 | 1512 | ceph_put_snap_context(snapc); |
00f1f36f | 1513 | continue; |
1fec7093 YS |
1514 | } |
1515 | ||
602adf40 YS |
1516 | do { |
1517 | /* a bio clone to be passed down to OSD req */ | |
bd919d45 | 1518 | dout("rq->bio->bi_vcnt=%hu\n", rq->bio->bi_vcnt); |
65ccfe21 | 1519 | op_size = rbd_segment_length(rbd_dev, ofs, size); |
1fec7093 | 1520 | kref_get(&coll->kref); |
602adf40 YS |
1521 | bio = bio_chain_clone(&rq_bio, &next_bio, &bp, |
1522 | op_size, GFP_ATOMIC); | |
1523 | if (!bio) { | |
1fec7093 YS |
1524 | rbd_coll_end_req_index(rq, coll, cur_seg, |
1525 | -ENOMEM, op_size); | |
1526 | goto next_seg; | |
602adf40 YS |
1527 | } |
1528 | ||
1fec7093 | 1529 | |
602adf40 YS |
1530 | /* init OSD command: write or read */ |
1531 | if (do_write) | |
1532 | rbd_req_write(rq, rbd_dev, | |
d1d25646 | 1533 | snapc, |
602adf40 | 1534 | ofs, |
1fec7093 YS |
1535 | op_size, bio, |
1536 | coll, cur_seg); | |
602adf40 YS |
1537 | else |
1538 | rbd_req_read(rq, rbd_dev, | |
f84344f3 | 1539 | rbd_dev->mapping.snap_id, |
602adf40 | 1540 | ofs, |
1fec7093 YS |
1541 | op_size, bio, |
1542 | coll, cur_seg); | |
602adf40 | 1543 | |
1fec7093 | 1544 | next_seg: |
602adf40 YS |
1545 | size -= op_size; |
1546 | ofs += op_size; | |
1547 | ||
1fec7093 | 1548 | cur_seg++; |
602adf40 YS |
1549 | rq_bio = next_bio; |
1550 | } while (size > 0); | |
1fec7093 | 1551 | kref_put(&coll->kref, rbd_coll_release); |
602adf40 YS |
1552 | |
1553 | if (bp) | |
1554 | bio_pair_release(bp); | |
602adf40 | 1555 | spin_lock_irq(q->queue_lock); |
d1d25646 JD |
1556 | |
1557 | ceph_put_snap_context(snapc); | |
602adf40 YS |
1558 | } |
1559 | } | |
1560 | ||
1561 | /* | |
1562 | * a queue callback. Makes sure that we don't create a bio that spans across | |
1563 | * multiple osd objects. One exception would be with a single page bios, | |
1564 | * which we handle later at bio_chain_clone | |
1565 | */ | |
1566 | static int rbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd, | |
1567 | struct bio_vec *bvec) | |
1568 | { | |
1569 | struct rbd_device *rbd_dev = q->queuedata; | |
593a9e7b AE |
1570 | unsigned int chunk_sectors; |
1571 | sector_t sector; | |
1572 | unsigned int bio_sectors; | |
602adf40 YS |
1573 | int max; |
1574 | ||
593a9e7b AE |
1575 | chunk_sectors = 1 << (rbd_dev->header.obj_order - SECTOR_SHIFT); |
1576 | sector = bmd->bi_sector + get_start_sect(bmd->bi_bdev); | |
1577 | bio_sectors = bmd->bi_size >> SECTOR_SHIFT; | |
1578 | ||
602adf40 | 1579 | max = (chunk_sectors - ((sector & (chunk_sectors - 1)) |
593a9e7b | 1580 | + bio_sectors)) << SECTOR_SHIFT; |
602adf40 YS |
1581 | if (max < 0) |
1582 | max = 0; /* bio_add cannot handle a negative return */ | |
1583 | if (max <= bvec->bv_len && bio_sectors == 0) | |
1584 | return bvec->bv_len; | |
1585 | return max; | |
1586 | } | |
1587 | ||
1588 | static void rbd_free_disk(struct rbd_device *rbd_dev) | |
1589 | { | |
1590 | struct gendisk *disk = rbd_dev->disk; | |
1591 | ||
1592 | if (!disk) | |
1593 | return; | |
1594 | ||
602adf40 YS |
1595 | if (disk->flags & GENHD_FL_UP) |
1596 | del_gendisk(disk); | |
1597 | if (disk->queue) | |
1598 | blk_cleanup_queue(disk->queue); | |
1599 | put_disk(disk); | |
1600 | } | |
1601 | ||
1602 | /* | |
4156d998 AE |
1603 | * Read the complete header for the given rbd device. |
1604 | * | |
1605 | * Returns a pointer to a dynamically-allocated buffer containing | |
1606 | * the complete and validated header. Caller can pass the address | |
1607 | * of a variable that will be filled in with the version of the | |
1608 | * header object at the time it was read. | |
1609 | * | |
1610 | * Returns a pointer-coded errno if a failure occurs. | |
602adf40 | 1611 | */ |
4156d998 AE |
1612 | static struct rbd_image_header_ondisk * |
1613 | rbd_dev_v1_header_read(struct rbd_device *rbd_dev, u64 *version) | |
602adf40 | 1614 | { |
4156d998 | 1615 | struct rbd_image_header_ondisk *ondisk = NULL; |
50f7c4c9 | 1616 | u32 snap_count = 0; |
4156d998 AE |
1617 | u64 names_size = 0; |
1618 | u32 want_count; | |
1619 | int ret; | |
602adf40 | 1620 | |
00f1f36f | 1621 | /* |
4156d998 AE |
1622 | * The complete header will include an array of its 64-bit |
1623 | * snapshot ids, followed by the names of those snapshots as | |
1624 | * a contiguous block of NUL-terminated strings. Note that | |
1625 | * the number of snapshots could change by the time we read | |
1626 | * it in, in which case we re-read it. | |
00f1f36f | 1627 | */ |
4156d998 AE |
1628 | do { |
1629 | size_t size; | |
1630 | ||
1631 | kfree(ondisk); | |
1632 | ||
1633 | size = sizeof (*ondisk); | |
1634 | size += snap_count * sizeof (struct rbd_image_snap_ondisk); | |
1635 | size += names_size; | |
1636 | ondisk = kmalloc(size, GFP_KERNEL); | |
1637 | if (!ondisk) | |
1638 | return ERR_PTR(-ENOMEM); | |
1639 | ||
1640 | ret = rbd_req_sync_read(rbd_dev, CEPH_NOSNAP, | |
0bed54dc | 1641 | rbd_dev->header_name, |
4156d998 AE |
1642 | 0, size, |
1643 | (char *) ondisk, version); | |
1644 | ||
1645 | if (ret < 0) | |
1646 | goto out_err; | |
1647 | if (WARN_ON((size_t) ret < size)) { | |
1648 | ret = -ENXIO; | |
1649 | pr_warning("short header read for image %s" | |
1650 | " (want %zd got %d)\n", | |
1651 | rbd_dev->image_name, size, ret); | |
1652 | goto out_err; | |
1653 | } | |
1654 | if (!rbd_dev_ondisk_valid(ondisk)) { | |
1655 | ret = -ENXIO; | |
1656 | pr_warning("invalid header for image %s\n", | |
1657 | rbd_dev->image_name); | |
1658 | goto out_err; | |
81e759fb | 1659 | } |
602adf40 | 1660 | |
4156d998 AE |
1661 | names_size = le64_to_cpu(ondisk->snap_names_len); |
1662 | want_count = snap_count; | |
1663 | snap_count = le32_to_cpu(ondisk->snap_count); | |
1664 | } while (snap_count != want_count); | |
00f1f36f | 1665 | |
4156d998 | 1666 | return ondisk; |
00f1f36f | 1667 | |
4156d998 AE |
1668 | out_err: |
1669 | kfree(ondisk); | |
1670 | ||
1671 | return ERR_PTR(ret); | |
1672 | } | |
1673 | ||
1674 | /* | |
1675 | * reload the ondisk the header | |
1676 | */ | |
1677 | static int rbd_read_header(struct rbd_device *rbd_dev, | |
1678 | struct rbd_image_header *header) | |
1679 | { | |
1680 | struct rbd_image_header_ondisk *ondisk; | |
1681 | u64 ver = 0; | |
1682 | int ret; | |
602adf40 | 1683 | |
4156d998 AE |
1684 | ondisk = rbd_dev_v1_header_read(rbd_dev, &ver); |
1685 | if (IS_ERR(ondisk)) | |
1686 | return PTR_ERR(ondisk); | |
1687 | ret = rbd_header_from_disk(header, ondisk); | |
1688 | if (ret >= 0) | |
1689 | header->obj_version = ver; | |
1690 | kfree(ondisk); | |
1691 | ||
1692 | return ret; | |
602adf40 YS |
1693 | } |
1694 | ||
dfc5606d YS |
1695 | static void __rbd_remove_all_snaps(struct rbd_device *rbd_dev) |
1696 | { | |
1697 | struct rbd_snap *snap; | |
a0593290 | 1698 | struct rbd_snap *next; |
dfc5606d | 1699 | |
a0593290 | 1700 | list_for_each_entry_safe(snap, next, &rbd_dev->snaps, node) |
14e7085d | 1701 | __rbd_remove_snap_dev(snap); |
dfc5606d YS |
1702 | } |
1703 | ||
602adf40 YS |
1704 | /* |
1705 | * only read the first part of the ondisk header, without the snaps info | |
1706 | */ | |
b813623a | 1707 | static int __rbd_refresh_header(struct rbd_device *rbd_dev, u64 *hver) |
602adf40 YS |
1708 | { |
1709 | int ret; | |
1710 | struct rbd_image_header h; | |
602adf40 YS |
1711 | |
1712 | ret = rbd_read_header(rbd_dev, &h); | |
1713 | if (ret < 0) | |
1714 | return ret; | |
1715 | ||
a51aa0c0 JD |
1716 | down_write(&rbd_dev->header_rwsem); |
1717 | ||
9db4b3e3 | 1718 | /* resized? */ |
f84344f3 | 1719 | if (rbd_dev->mapping.snap_id == CEPH_NOSNAP) { |
474ef7ce JD |
1720 | sector_t size = (sector_t) h.image_size / SECTOR_SIZE; |
1721 | ||
99c1f08f AE |
1722 | if (size != (sector_t) rbd_dev->mapping.size) { |
1723 | dout("setting size to %llu sectors", | |
1724 | (unsigned long long) size); | |
1725 | rbd_dev->mapping.size = (u64) size; | |
1726 | set_capacity(rbd_dev->disk, size); | |
1727 | } | |
474ef7ce | 1728 | } |
9db4b3e3 | 1729 | |
849b4260 | 1730 | /* rbd_dev->header.object_prefix shouldn't change */ |
602adf40 | 1731 | kfree(rbd_dev->header.snap_sizes); |
849b4260 | 1732 | kfree(rbd_dev->header.snap_names); |
d1d25646 JD |
1733 | /* osd requests may still refer to snapc */ |
1734 | ceph_put_snap_context(rbd_dev->header.snapc); | |
602adf40 | 1735 | |
b813623a AE |
1736 | if (hver) |
1737 | *hver = h.obj_version; | |
a71b891b | 1738 | rbd_dev->header.obj_version = h.obj_version; |
93a24e08 | 1739 | rbd_dev->header.image_size = h.image_size; |
602adf40 YS |
1740 | rbd_dev->header.snapc = h.snapc; |
1741 | rbd_dev->header.snap_names = h.snap_names; | |
1742 | rbd_dev->header.snap_sizes = h.snap_sizes; | |
849b4260 AE |
1743 | /* Free the extra copy of the object prefix */ |
1744 | WARN_ON(strcmp(rbd_dev->header.object_prefix, h.object_prefix)); | |
1745 | kfree(h.object_prefix); | |
1746 | ||
304f6808 AE |
1747 | ret = rbd_dev_snaps_update(rbd_dev); |
1748 | if (!ret) | |
1749 | ret = rbd_dev_snaps_register(rbd_dev); | |
dfc5606d | 1750 | |
c666601a | 1751 | up_write(&rbd_dev->header_rwsem); |
602adf40 | 1752 | |
dfc5606d | 1753 | return ret; |
602adf40 YS |
1754 | } |
1755 | ||
1fe5e993 AE |
1756 | static int rbd_refresh_header(struct rbd_device *rbd_dev, u64 *hver) |
1757 | { | |
1758 | int ret; | |
1759 | ||
1760 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); | |
1761 | ret = __rbd_refresh_header(rbd_dev, hver); | |
1762 | mutex_unlock(&ctl_mutex); | |
1763 | ||
1764 | return ret; | |
1765 | } | |
1766 | ||
602adf40 YS |
1767 | static int rbd_init_disk(struct rbd_device *rbd_dev) |
1768 | { | |
1769 | struct gendisk *disk; | |
1770 | struct request_queue *q; | |
593a9e7b | 1771 | u64 segment_size; |
602adf40 | 1772 | |
602adf40 | 1773 | /* create gendisk info */ |
602adf40 YS |
1774 | disk = alloc_disk(RBD_MINORS_PER_MAJOR); |
1775 | if (!disk) | |
1fcdb8aa | 1776 | return -ENOMEM; |
602adf40 | 1777 | |
f0f8cef5 | 1778 | snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d", |
de71a297 | 1779 | rbd_dev->dev_id); |
602adf40 YS |
1780 | disk->major = rbd_dev->major; |
1781 | disk->first_minor = 0; | |
1782 | disk->fops = &rbd_bd_ops; | |
1783 | disk->private_data = rbd_dev; | |
1784 | ||
1785 | /* init rq */ | |
602adf40 YS |
1786 | q = blk_init_queue(rbd_rq_fn, &rbd_dev->lock); |
1787 | if (!q) | |
1788 | goto out_disk; | |
029bcbd8 | 1789 | |
593a9e7b AE |
1790 | /* We use the default size, but let's be explicit about it. */ |
1791 | blk_queue_physical_block_size(q, SECTOR_SIZE); | |
1792 | ||
029bcbd8 | 1793 | /* set io sizes to object size */ |
593a9e7b AE |
1794 | segment_size = rbd_obj_bytes(&rbd_dev->header); |
1795 | blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE); | |
1796 | blk_queue_max_segment_size(q, segment_size); | |
1797 | blk_queue_io_min(q, segment_size); | |
1798 | blk_queue_io_opt(q, segment_size); | |
029bcbd8 | 1799 | |
602adf40 YS |
1800 | blk_queue_merge_bvec(q, rbd_merge_bvec); |
1801 | disk->queue = q; | |
1802 | ||
1803 | q->queuedata = rbd_dev; | |
1804 | ||
1805 | rbd_dev->disk = disk; | |
602adf40 | 1806 | |
12f02944 AE |
1807 | set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE); |
1808 | ||
602adf40 | 1809 | return 0; |
602adf40 YS |
1810 | out_disk: |
1811 | put_disk(disk); | |
1fcdb8aa AE |
1812 | |
1813 | return -ENOMEM; | |
602adf40 YS |
1814 | } |
1815 | ||
dfc5606d YS |
1816 | /* |
1817 | sysfs | |
1818 | */ | |
1819 | ||
593a9e7b AE |
1820 | static struct rbd_device *dev_to_rbd_dev(struct device *dev) |
1821 | { | |
1822 | return container_of(dev, struct rbd_device, dev); | |
1823 | } | |
1824 | ||
dfc5606d YS |
1825 | static ssize_t rbd_size_show(struct device *dev, |
1826 | struct device_attribute *attr, char *buf) | |
1827 | { | |
593a9e7b | 1828 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
a51aa0c0 JD |
1829 | sector_t size; |
1830 | ||
1831 | down_read(&rbd_dev->header_rwsem); | |
1832 | size = get_capacity(rbd_dev->disk); | |
1833 | up_read(&rbd_dev->header_rwsem); | |
dfc5606d | 1834 | |
a51aa0c0 | 1835 | return sprintf(buf, "%llu\n", (unsigned long long) size * SECTOR_SIZE); |
dfc5606d YS |
1836 | } |
1837 | ||
1838 | static ssize_t rbd_major_show(struct device *dev, | |
1839 | struct device_attribute *attr, char *buf) | |
1840 | { | |
593a9e7b | 1841 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
602adf40 | 1842 | |
dfc5606d YS |
1843 | return sprintf(buf, "%d\n", rbd_dev->major); |
1844 | } | |
1845 | ||
1846 | static ssize_t rbd_client_id_show(struct device *dev, | |
1847 | struct device_attribute *attr, char *buf) | |
602adf40 | 1848 | { |
593a9e7b | 1849 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
dfc5606d | 1850 | |
1dbb4399 AE |
1851 | return sprintf(buf, "client%lld\n", |
1852 | ceph_client_id(rbd_dev->rbd_client->client)); | |
602adf40 YS |
1853 | } |
1854 | ||
dfc5606d YS |
1855 | static ssize_t rbd_pool_show(struct device *dev, |
1856 | struct device_attribute *attr, char *buf) | |
602adf40 | 1857 | { |
593a9e7b | 1858 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
dfc5606d YS |
1859 | |
1860 | return sprintf(buf, "%s\n", rbd_dev->pool_name); | |
1861 | } | |
1862 | ||
9bb2f334 AE |
1863 | static ssize_t rbd_pool_id_show(struct device *dev, |
1864 | struct device_attribute *attr, char *buf) | |
1865 | { | |
1866 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); | |
1867 | ||
1868 | return sprintf(buf, "%d\n", rbd_dev->pool_id); | |
1869 | } | |
1870 | ||
dfc5606d YS |
1871 | static ssize_t rbd_name_show(struct device *dev, |
1872 | struct device_attribute *attr, char *buf) | |
1873 | { | |
593a9e7b | 1874 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
dfc5606d | 1875 | |
0bed54dc | 1876 | return sprintf(buf, "%s\n", rbd_dev->image_name); |
dfc5606d YS |
1877 | } |
1878 | ||
589d30e0 AE |
1879 | static ssize_t rbd_image_id_show(struct device *dev, |
1880 | struct device_attribute *attr, char *buf) | |
1881 | { | |
1882 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); | |
1883 | ||
1884 | return sprintf(buf, "%s\n", rbd_dev->image_id); | |
1885 | } | |
1886 | ||
dfc5606d YS |
1887 | static ssize_t rbd_snap_show(struct device *dev, |
1888 | struct device_attribute *attr, | |
1889 | char *buf) | |
1890 | { | |
593a9e7b | 1891 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
dfc5606d | 1892 | |
f84344f3 | 1893 | return sprintf(buf, "%s\n", rbd_dev->mapping.snap_name); |
dfc5606d YS |
1894 | } |
1895 | ||
1896 | static ssize_t rbd_image_refresh(struct device *dev, | |
1897 | struct device_attribute *attr, | |
1898 | const char *buf, | |
1899 | size_t size) | |
1900 | { | |
593a9e7b | 1901 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
b813623a | 1902 | int ret; |
602adf40 | 1903 | |
1fe5e993 | 1904 | ret = rbd_refresh_header(rbd_dev, NULL); |
b813623a AE |
1905 | |
1906 | return ret < 0 ? ret : size; | |
dfc5606d | 1907 | } |
602adf40 | 1908 | |
dfc5606d YS |
1909 | static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL); |
1910 | static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL); | |
1911 | static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL); | |
1912 | static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL); | |
9bb2f334 | 1913 | static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL); |
dfc5606d | 1914 | static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL); |
589d30e0 | 1915 | static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL); |
dfc5606d YS |
1916 | static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh); |
1917 | static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL); | |
dfc5606d YS |
1918 | |
1919 | static struct attribute *rbd_attrs[] = { | |
1920 | &dev_attr_size.attr, | |
1921 | &dev_attr_major.attr, | |
1922 | &dev_attr_client_id.attr, | |
1923 | &dev_attr_pool.attr, | |
9bb2f334 | 1924 | &dev_attr_pool_id.attr, |
dfc5606d | 1925 | &dev_attr_name.attr, |
589d30e0 | 1926 | &dev_attr_image_id.attr, |
dfc5606d YS |
1927 | &dev_attr_current_snap.attr, |
1928 | &dev_attr_refresh.attr, | |
dfc5606d YS |
1929 | NULL |
1930 | }; | |
1931 | ||
1932 | static struct attribute_group rbd_attr_group = { | |
1933 | .attrs = rbd_attrs, | |
1934 | }; | |
1935 | ||
1936 | static const struct attribute_group *rbd_attr_groups[] = { | |
1937 | &rbd_attr_group, | |
1938 | NULL | |
1939 | }; | |
1940 | ||
1941 | static void rbd_sysfs_dev_release(struct device *dev) | |
1942 | { | |
1943 | } | |
1944 | ||
1945 | static struct device_type rbd_device_type = { | |
1946 | .name = "rbd", | |
1947 | .groups = rbd_attr_groups, | |
1948 | .release = rbd_sysfs_dev_release, | |
1949 | }; | |
1950 | ||
1951 | ||
1952 | /* | |
1953 | sysfs - snapshots | |
1954 | */ | |
1955 | ||
1956 | static ssize_t rbd_snap_size_show(struct device *dev, | |
1957 | struct device_attribute *attr, | |
1958 | char *buf) | |
1959 | { | |
1960 | struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev); | |
1961 | ||
3591538f | 1962 | return sprintf(buf, "%llu\n", (unsigned long long)snap->size); |
dfc5606d YS |
1963 | } |
1964 | ||
1965 | static ssize_t rbd_snap_id_show(struct device *dev, | |
1966 | struct device_attribute *attr, | |
1967 | char *buf) | |
1968 | { | |
1969 | struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev); | |
1970 | ||
3591538f | 1971 | return sprintf(buf, "%llu\n", (unsigned long long)snap->id); |
dfc5606d YS |
1972 | } |
1973 | ||
1974 | static DEVICE_ATTR(snap_size, S_IRUGO, rbd_snap_size_show, NULL); | |
1975 | static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL); | |
1976 | ||
1977 | static struct attribute *rbd_snap_attrs[] = { | |
1978 | &dev_attr_snap_size.attr, | |
1979 | &dev_attr_snap_id.attr, | |
1980 | NULL, | |
1981 | }; | |
1982 | ||
1983 | static struct attribute_group rbd_snap_attr_group = { | |
1984 | .attrs = rbd_snap_attrs, | |
1985 | }; | |
1986 | ||
1987 | static void rbd_snap_dev_release(struct device *dev) | |
1988 | { | |
1989 | struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev); | |
1990 | kfree(snap->name); | |
1991 | kfree(snap); | |
1992 | } | |
1993 | ||
1994 | static const struct attribute_group *rbd_snap_attr_groups[] = { | |
1995 | &rbd_snap_attr_group, | |
1996 | NULL | |
1997 | }; | |
1998 | ||
1999 | static struct device_type rbd_snap_device_type = { | |
2000 | .groups = rbd_snap_attr_groups, | |
2001 | .release = rbd_snap_dev_release, | |
2002 | }; | |
2003 | ||
304f6808 AE |
2004 | static bool rbd_snap_registered(struct rbd_snap *snap) |
2005 | { | |
2006 | bool ret = snap->dev.type == &rbd_snap_device_type; | |
2007 | bool reg = device_is_registered(&snap->dev); | |
2008 | ||
2009 | rbd_assert(!ret ^ reg); | |
2010 | ||
2011 | return ret; | |
2012 | } | |
2013 | ||
14e7085d | 2014 | static void __rbd_remove_snap_dev(struct rbd_snap *snap) |
dfc5606d YS |
2015 | { |
2016 | list_del(&snap->node); | |
304f6808 AE |
2017 | if (device_is_registered(&snap->dev)) |
2018 | device_unregister(&snap->dev); | |
dfc5606d YS |
2019 | } |
2020 | ||
14e7085d | 2021 | static int rbd_register_snap_dev(struct rbd_snap *snap, |
dfc5606d YS |
2022 | struct device *parent) |
2023 | { | |
2024 | struct device *dev = &snap->dev; | |
2025 | int ret; | |
2026 | ||
2027 | dev->type = &rbd_snap_device_type; | |
2028 | dev->parent = parent; | |
2029 | dev->release = rbd_snap_dev_release; | |
2030 | dev_set_name(dev, "snap_%s", snap->name); | |
304f6808 AE |
2031 | dout("%s: registering device for snapshot %s\n", __func__, snap->name); |
2032 | ||
dfc5606d YS |
2033 | ret = device_register(dev); |
2034 | ||
2035 | return ret; | |
2036 | } | |
2037 | ||
4e891e0a | 2038 | static struct rbd_snap *__rbd_add_snap_dev(struct rbd_device *rbd_dev, |
c8d18425 AE |
2039 | const char *snap_name, |
2040 | u64 snap_id, u64 snap_size) | |
dfc5606d | 2041 | { |
4e891e0a | 2042 | struct rbd_snap *snap; |
dfc5606d | 2043 | int ret; |
4e891e0a AE |
2044 | |
2045 | snap = kzalloc(sizeof (*snap), GFP_KERNEL); | |
dfc5606d | 2046 | if (!snap) |
4e891e0a AE |
2047 | return ERR_PTR(-ENOMEM); |
2048 | ||
2049 | ret = -ENOMEM; | |
c8d18425 | 2050 | snap->name = kstrdup(snap_name, GFP_KERNEL); |
4e891e0a AE |
2051 | if (!snap->name) |
2052 | goto err; | |
2053 | ||
c8d18425 AE |
2054 | snap->id = snap_id; |
2055 | snap->size = snap_size; | |
4e891e0a AE |
2056 | |
2057 | return snap; | |
2058 | ||
dfc5606d YS |
2059 | err: |
2060 | kfree(snap->name); | |
2061 | kfree(snap); | |
4e891e0a AE |
2062 | |
2063 | return ERR_PTR(ret); | |
dfc5606d YS |
2064 | } |
2065 | ||
2066 | /* | |
35938150 AE |
2067 | * Scan the rbd device's current snapshot list and compare it to the |
2068 | * newly-received snapshot context. Remove any existing snapshots | |
2069 | * not present in the new snapshot context. Add a new snapshot for | |
2070 | * any snaphots in the snapshot context not in the current list. | |
2071 | * And verify there are no changes to snapshots we already know | |
2072 | * about. | |
2073 | * | |
2074 | * Assumes the snapshots in the snapshot context are sorted by | |
2075 | * snapshot id, highest id first. (Snapshots in the rbd_dev's list | |
2076 | * are also maintained in that order.) | |
dfc5606d | 2077 | */ |
304f6808 | 2078 | static int rbd_dev_snaps_update(struct rbd_device *rbd_dev) |
dfc5606d | 2079 | { |
35938150 AE |
2080 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
2081 | const u32 snap_count = snapc->num_snaps; | |
2082 | char *snap_name = rbd_dev->header.snap_names; | |
2083 | struct list_head *head = &rbd_dev->snaps; | |
2084 | struct list_head *links = head->next; | |
2085 | u32 index = 0; | |
dfc5606d | 2086 | |
9fcbb800 | 2087 | dout("%s: snap count is %u\n", __func__, (unsigned int) snap_count); |
35938150 AE |
2088 | while (index < snap_count || links != head) { |
2089 | u64 snap_id; | |
2090 | struct rbd_snap *snap; | |
dfc5606d | 2091 | |
35938150 AE |
2092 | snap_id = index < snap_count ? snapc->snaps[index] |
2093 | : CEPH_NOSNAP; | |
2094 | snap = links != head ? list_entry(links, struct rbd_snap, node) | |
2095 | : NULL; | |
aafb230e | 2096 | rbd_assert(!snap || snap->id != CEPH_NOSNAP); |
dfc5606d | 2097 | |
35938150 AE |
2098 | if (snap_id == CEPH_NOSNAP || (snap && snap->id > snap_id)) { |
2099 | struct list_head *next = links->next; | |
dfc5606d | 2100 | |
35938150 | 2101 | /* Existing snapshot not in the new snap context */ |
dfc5606d | 2102 | |
f84344f3 AE |
2103 | if (rbd_dev->mapping.snap_id == snap->id) |
2104 | rbd_dev->mapping.snap_exists = false; | |
35938150 | 2105 | __rbd_remove_snap_dev(snap); |
9fcbb800 | 2106 | dout("%ssnap id %llu has been removed\n", |
f84344f3 AE |
2107 | rbd_dev->mapping.snap_id == snap->id ? |
2108 | "mapped " : "", | |
9fcbb800 | 2109 | (unsigned long long) snap->id); |
35938150 AE |
2110 | |
2111 | /* Done with this list entry; advance */ | |
2112 | ||
2113 | links = next; | |
dfc5606d YS |
2114 | continue; |
2115 | } | |
35938150 | 2116 | |
9fcbb800 AE |
2117 | dout("entry %u: snap_id = %llu\n", (unsigned int) snap_count, |
2118 | (unsigned long long) snap_id); | |
35938150 | 2119 | if (!snap || (snap_id != CEPH_NOSNAP && snap->id < snap_id)) { |
c8d18425 | 2120 | struct rbd_image_header *header = &rbd_dev->header; |
35938150 AE |
2121 | struct rbd_snap *new_snap; |
2122 | ||
2123 | /* We haven't seen this snapshot before */ | |
2124 | ||
c8d18425 AE |
2125 | new_snap = __rbd_add_snap_dev(rbd_dev, snap_name, |
2126 | snap_id, header->snap_sizes[index]); | |
9fcbb800 AE |
2127 | if (IS_ERR(new_snap)) { |
2128 | int err = PTR_ERR(new_snap); | |
2129 | ||
2130 | dout(" failed to add dev, error %d\n", err); | |
2131 | ||
2132 | return err; | |
2133 | } | |
35938150 AE |
2134 | |
2135 | /* New goes before existing, or at end of list */ | |
2136 | ||
9fcbb800 | 2137 | dout(" added dev%s\n", snap ? "" : " at end\n"); |
35938150 AE |
2138 | if (snap) |
2139 | list_add_tail(&new_snap->node, &snap->node); | |
2140 | else | |
523f3258 | 2141 | list_add_tail(&new_snap->node, head); |
35938150 AE |
2142 | } else { |
2143 | /* Already have this one */ | |
2144 | ||
9fcbb800 AE |
2145 | dout(" already present\n"); |
2146 | ||
aafb230e AE |
2147 | rbd_assert(snap->size == |
2148 | rbd_dev->header.snap_sizes[index]); | |
2149 | rbd_assert(!strcmp(snap->name, snap_name)); | |
35938150 AE |
2150 | |
2151 | /* Done with this list entry; advance */ | |
2152 | ||
2153 | links = links->next; | |
dfc5606d | 2154 | } |
35938150 AE |
2155 | |
2156 | /* Advance to the next entry in the snapshot context */ | |
2157 | ||
2158 | index++; | |
2159 | snap_name += strlen(snap_name) + 1; | |
dfc5606d | 2160 | } |
9fcbb800 | 2161 | dout("%s: done\n", __func__); |
dfc5606d YS |
2162 | |
2163 | return 0; | |
2164 | } | |
2165 | ||
304f6808 AE |
2166 | /* |
2167 | * Scan the list of snapshots and register the devices for any that | |
2168 | * have not already been registered. | |
2169 | */ | |
2170 | static int rbd_dev_snaps_register(struct rbd_device *rbd_dev) | |
2171 | { | |
2172 | struct rbd_snap *snap; | |
2173 | int ret = 0; | |
2174 | ||
2175 | dout("%s called\n", __func__); | |
86ff77bb AE |
2176 | if (WARN_ON(!device_is_registered(&rbd_dev->dev))) |
2177 | return -EIO; | |
304f6808 AE |
2178 | |
2179 | list_for_each_entry(snap, &rbd_dev->snaps, node) { | |
2180 | if (!rbd_snap_registered(snap)) { | |
2181 | ret = rbd_register_snap_dev(snap, &rbd_dev->dev); | |
2182 | if (ret < 0) | |
2183 | break; | |
2184 | } | |
2185 | } | |
2186 | dout("%s: returning %d\n", __func__, ret); | |
2187 | ||
2188 | return ret; | |
2189 | } | |
2190 | ||
dfc5606d YS |
2191 | static int rbd_bus_add_dev(struct rbd_device *rbd_dev) |
2192 | { | |
dfc5606d | 2193 | struct device *dev; |
cd789ab9 | 2194 | int ret; |
dfc5606d YS |
2195 | |
2196 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); | |
dfc5606d | 2197 | |
cd789ab9 | 2198 | dev = &rbd_dev->dev; |
dfc5606d YS |
2199 | dev->bus = &rbd_bus_type; |
2200 | dev->type = &rbd_device_type; | |
2201 | dev->parent = &rbd_root_dev; | |
2202 | dev->release = rbd_dev_release; | |
de71a297 | 2203 | dev_set_name(dev, "%d", rbd_dev->dev_id); |
dfc5606d | 2204 | ret = device_register(dev); |
dfc5606d | 2205 | |
dfc5606d | 2206 | mutex_unlock(&ctl_mutex); |
cd789ab9 | 2207 | |
dfc5606d | 2208 | return ret; |
602adf40 YS |
2209 | } |
2210 | ||
dfc5606d YS |
2211 | static void rbd_bus_del_dev(struct rbd_device *rbd_dev) |
2212 | { | |
2213 | device_unregister(&rbd_dev->dev); | |
2214 | } | |
2215 | ||
59c2be1e YS |
2216 | static int rbd_init_watch_dev(struct rbd_device *rbd_dev) |
2217 | { | |
2218 | int ret, rc; | |
2219 | ||
2220 | do { | |
0e6f322d | 2221 | ret = rbd_req_sync_watch(rbd_dev); |
59c2be1e | 2222 | if (ret == -ERANGE) { |
1fe5e993 | 2223 | rc = rbd_refresh_header(rbd_dev, NULL); |
59c2be1e YS |
2224 | if (rc < 0) |
2225 | return rc; | |
2226 | } | |
2227 | } while (ret == -ERANGE); | |
2228 | ||
2229 | return ret; | |
2230 | } | |
2231 | ||
e2839308 | 2232 | static atomic64_t rbd_dev_id_max = ATOMIC64_INIT(0); |
1ddbe94e AE |
2233 | |
2234 | /* | |
499afd5b AE |
2235 | * Get a unique rbd identifier for the given new rbd_dev, and add |
2236 | * the rbd_dev to the global list. The minimum rbd id is 1. | |
1ddbe94e | 2237 | */ |
e2839308 | 2238 | static void rbd_dev_id_get(struct rbd_device *rbd_dev) |
b7f23c36 | 2239 | { |
e2839308 | 2240 | rbd_dev->dev_id = atomic64_inc_return(&rbd_dev_id_max); |
499afd5b AE |
2241 | |
2242 | spin_lock(&rbd_dev_list_lock); | |
2243 | list_add_tail(&rbd_dev->node, &rbd_dev_list); | |
2244 | spin_unlock(&rbd_dev_list_lock); | |
e2839308 AE |
2245 | dout("rbd_dev %p given dev id %llu\n", rbd_dev, |
2246 | (unsigned long long) rbd_dev->dev_id); | |
1ddbe94e | 2247 | } |
b7f23c36 | 2248 | |
1ddbe94e | 2249 | /* |
499afd5b AE |
2250 | * Remove an rbd_dev from the global list, and record that its |
2251 | * identifier is no longer in use. | |
1ddbe94e | 2252 | */ |
e2839308 | 2253 | static void rbd_dev_id_put(struct rbd_device *rbd_dev) |
1ddbe94e | 2254 | { |
d184f6bf | 2255 | struct list_head *tmp; |
de71a297 | 2256 | int rbd_id = rbd_dev->dev_id; |
d184f6bf AE |
2257 | int max_id; |
2258 | ||
aafb230e | 2259 | rbd_assert(rbd_id > 0); |
499afd5b | 2260 | |
e2839308 AE |
2261 | dout("rbd_dev %p released dev id %llu\n", rbd_dev, |
2262 | (unsigned long long) rbd_dev->dev_id); | |
499afd5b AE |
2263 | spin_lock(&rbd_dev_list_lock); |
2264 | list_del_init(&rbd_dev->node); | |
d184f6bf AE |
2265 | |
2266 | /* | |
2267 | * If the id being "put" is not the current maximum, there | |
2268 | * is nothing special we need to do. | |
2269 | */ | |
e2839308 | 2270 | if (rbd_id != atomic64_read(&rbd_dev_id_max)) { |
d184f6bf AE |
2271 | spin_unlock(&rbd_dev_list_lock); |
2272 | return; | |
2273 | } | |
2274 | ||
2275 | /* | |
2276 | * We need to update the current maximum id. Search the | |
2277 | * list to find out what it is. We're more likely to find | |
2278 | * the maximum at the end, so search the list backward. | |
2279 | */ | |
2280 | max_id = 0; | |
2281 | list_for_each_prev(tmp, &rbd_dev_list) { | |
2282 | struct rbd_device *rbd_dev; | |
2283 | ||
2284 | rbd_dev = list_entry(tmp, struct rbd_device, node); | |
2285 | if (rbd_id > max_id) | |
2286 | max_id = rbd_id; | |
2287 | } | |
499afd5b | 2288 | spin_unlock(&rbd_dev_list_lock); |
b7f23c36 | 2289 | |
1ddbe94e | 2290 | /* |
e2839308 | 2291 | * The max id could have been updated by rbd_dev_id_get(), in |
d184f6bf AE |
2292 | * which case it now accurately reflects the new maximum. |
2293 | * Be careful not to overwrite the maximum value in that | |
2294 | * case. | |
1ddbe94e | 2295 | */ |
e2839308 AE |
2296 | atomic64_cmpxchg(&rbd_dev_id_max, rbd_id, max_id); |
2297 | dout(" max dev id has been reset\n"); | |
b7f23c36 AE |
2298 | } |
2299 | ||
e28fff26 AE |
2300 | /* |
2301 | * Skips over white space at *buf, and updates *buf to point to the | |
2302 | * first found non-space character (if any). Returns the length of | |
593a9e7b AE |
2303 | * the token (string of non-white space characters) found. Note |
2304 | * that *buf must be terminated with '\0'. | |
e28fff26 AE |
2305 | */ |
2306 | static inline size_t next_token(const char **buf) | |
2307 | { | |
2308 | /* | |
2309 | * These are the characters that produce nonzero for | |
2310 | * isspace() in the "C" and "POSIX" locales. | |
2311 | */ | |
2312 | const char *spaces = " \f\n\r\t\v"; | |
2313 | ||
2314 | *buf += strspn(*buf, spaces); /* Find start of token */ | |
2315 | ||
2316 | return strcspn(*buf, spaces); /* Return token length */ | |
2317 | } | |
2318 | ||
2319 | /* | |
2320 | * Finds the next token in *buf, and if the provided token buffer is | |
2321 | * big enough, copies the found token into it. The result, if | |
593a9e7b AE |
2322 | * copied, is guaranteed to be terminated with '\0'. Note that *buf |
2323 | * must be terminated with '\0' on entry. | |
e28fff26 AE |
2324 | * |
2325 | * Returns the length of the token found (not including the '\0'). | |
2326 | * Return value will be 0 if no token is found, and it will be >= | |
2327 | * token_size if the token would not fit. | |
2328 | * | |
593a9e7b | 2329 | * The *buf pointer will be updated to point beyond the end of the |
e28fff26 AE |
2330 | * found token. Note that this occurs even if the token buffer is |
2331 | * too small to hold it. | |
2332 | */ | |
2333 | static inline size_t copy_token(const char **buf, | |
2334 | char *token, | |
2335 | size_t token_size) | |
2336 | { | |
2337 | size_t len; | |
2338 | ||
2339 | len = next_token(buf); | |
2340 | if (len < token_size) { | |
2341 | memcpy(token, *buf, len); | |
2342 | *(token + len) = '\0'; | |
2343 | } | |
2344 | *buf += len; | |
2345 | ||
2346 | return len; | |
2347 | } | |
2348 | ||
ea3352f4 AE |
2349 | /* |
2350 | * Finds the next token in *buf, dynamically allocates a buffer big | |
2351 | * enough to hold a copy of it, and copies the token into the new | |
2352 | * buffer. The copy is guaranteed to be terminated with '\0'. Note | |
2353 | * that a duplicate buffer is created even for a zero-length token. | |
2354 | * | |
2355 | * Returns a pointer to the newly-allocated duplicate, or a null | |
2356 | * pointer if memory for the duplicate was not available. If | |
2357 | * the lenp argument is a non-null pointer, the length of the token | |
2358 | * (not including the '\0') is returned in *lenp. | |
2359 | * | |
2360 | * If successful, the *buf pointer will be updated to point beyond | |
2361 | * the end of the found token. | |
2362 | * | |
2363 | * Note: uses GFP_KERNEL for allocation. | |
2364 | */ | |
2365 | static inline char *dup_token(const char **buf, size_t *lenp) | |
2366 | { | |
2367 | char *dup; | |
2368 | size_t len; | |
2369 | ||
2370 | len = next_token(buf); | |
2371 | dup = kmalloc(len + 1, GFP_KERNEL); | |
2372 | if (!dup) | |
2373 | return NULL; | |
2374 | ||
2375 | memcpy(dup, *buf, len); | |
2376 | *(dup + len) = '\0'; | |
2377 | *buf += len; | |
2378 | ||
2379 | if (lenp) | |
2380 | *lenp = len; | |
2381 | ||
2382 | return dup; | |
2383 | } | |
2384 | ||
a725f65e | 2385 | /* |
3feeb894 AE |
2386 | * This fills in the pool_name, image_name, image_name_len, rbd_dev, |
2387 | * rbd_md_name, and name fields of the given rbd_dev, based on the | |
2388 | * list of monitor addresses and other options provided via | |
2389 | * /sys/bus/rbd/add. Returns a pointer to a dynamically-allocated | |
2390 | * copy of the snapshot name to map if successful, or a | |
2391 | * pointer-coded error otherwise. | |
d22f76e7 AE |
2392 | * |
2393 | * Note: rbd_dev is assumed to have been initially zero-filled. | |
a725f65e | 2394 | */ |
3feeb894 AE |
2395 | static char *rbd_add_parse_args(struct rbd_device *rbd_dev, |
2396 | const char *buf, | |
2397 | const char **mon_addrs, | |
2398 | size_t *mon_addrs_size, | |
2399 | char *options, | |
2400 | size_t options_size) | |
e28fff26 | 2401 | { |
d22f76e7 | 2402 | size_t len; |
3feeb894 AE |
2403 | char *err_ptr = ERR_PTR(-EINVAL); |
2404 | char *snap_name; | |
e28fff26 AE |
2405 | |
2406 | /* The first four tokens are required */ | |
2407 | ||
7ef3214a AE |
2408 | len = next_token(&buf); |
2409 | if (!len) | |
3feeb894 | 2410 | return err_ptr; |
5214ecc4 | 2411 | *mon_addrs_size = len + 1; |
7ef3214a AE |
2412 | *mon_addrs = buf; |
2413 | ||
2414 | buf += len; | |
a725f65e | 2415 | |
e28fff26 AE |
2416 | len = copy_token(&buf, options, options_size); |
2417 | if (!len || len >= options_size) | |
3feeb894 | 2418 | return err_ptr; |
e28fff26 | 2419 | |
3feeb894 | 2420 | err_ptr = ERR_PTR(-ENOMEM); |
d22f76e7 AE |
2421 | rbd_dev->pool_name = dup_token(&buf, NULL); |
2422 | if (!rbd_dev->pool_name) | |
d22f76e7 | 2423 | goto out_err; |
e28fff26 | 2424 | |
0bed54dc AE |
2425 | rbd_dev->image_name = dup_token(&buf, &rbd_dev->image_name_len); |
2426 | if (!rbd_dev->image_name) | |
bf3e5ae1 | 2427 | goto out_err; |
a725f65e | 2428 | |
3feeb894 AE |
2429 | /* Snapshot name is optional */ |
2430 | len = next_token(&buf); | |
820a5f3e | 2431 | if (!len) { |
3feeb894 AE |
2432 | buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */ |
2433 | len = sizeof (RBD_SNAP_HEAD_NAME) - 1; | |
849b4260 | 2434 | } |
3feeb894 AE |
2435 | snap_name = kmalloc(len + 1, GFP_KERNEL); |
2436 | if (!snap_name) | |
2437 | goto out_err; | |
2438 | memcpy(snap_name, buf, len); | |
2439 | *(snap_name + len) = '\0'; | |
e28fff26 | 2440 | |
3feeb894 AE |
2441 | dout(" SNAP_NAME is <%s>, len is %zd\n", snap_name, len); |
2442 | ||
2443 | return snap_name; | |
d22f76e7 AE |
2444 | |
2445 | out_err: | |
0bed54dc | 2446 | kfree(rbd_dev->image_name); |
d78fd7ae AE |
2447 | rbd_dev->image_name = NULL; |
2448 | rbd_dev->image_name_len = 0; | |
d22f76e7 AE |
2449 | kfree(rbd_dev->pool_name); |
2450 | rbd_dev->pool_name = NULL; | |
2451 | ||
3feeb894 | 2452 | return err_ptr; |
a725f65e AE |
2453 | } |
2454 | ||
589d30e0 AE |
2455 | /* |
2456 | * An rbd format 2 image has a unique identifier, distinct from the | |
2457 | * name given to it by the user. Internally, that identifier is | |
2458 | * what's used to specify the names of objects related to the image. | |
2459 | * | |
2460 | * A special "rbd id" object is used to map an rbd image name to its | |
2461 | * id. If that object doesn't exist, then there is no v2 rbd image | |
2462 | * with the supplied name. | |
2463 | * | |
2464 | * This function will record the given rbd_dev's image_id field if | |
2465 | * it can be determined, and in that case will return 0. If any | |
2466 | * errors occur a negative errno will be returned and the rbd_dev's | |
2467 | * image_id field will be unchanged (and should be NULL). | |
2468 | */ | |
2469 | static int rbd_dev_image_id(struct rbd_device *rbd_dev) | |
2470 | { | |
2471 | int ret; | |
2472 | size_t size; | |
2473 | char *object_name; | |
2474 | void *response; | |
2475 | void *p; | |
2476 | ||
2477 | /* | |
2478 | * First, see if the format 2 image id file exists, and if | |
2479 | * so, get the image's persistent id from it. | |
2480 | */ | |
2481 | size = sizeof (RBD_ID_PREFIX) + rbd_dev->image_name_len; | |
2482 | object_name = kmalloc(size, GFP_NOIO); | |
2483 | if (!object_name) | |
2484 | return -ENOMEM; | |
2485 | sprintf(object_name, "%s%s", RBD_ID_PREFIX, rbd_dev->image_name); | |
2486 | dout("rbd id object name is %s\n", object_name); | |
2487 | ||
2488 | /* Response will be an encoded string, which includes a length */ | |
2489 | ||
2490 | size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX; | |
2491 | response = kzalloc(size, GFP_NOIO); | |
2492 | if (!response) { | |
2493 | ret = -ENOMEM; | |
2494 | goto out; | |
2495 | } | |
2496 | ||
2497 | ret = rbd_req_sync_exec(rbd_dev, object_name, | |
2498 | "rbd", "get_id", | |
2499 | NULL, 0, | |
2500 | response, RBD_IMAGE_ID_LEN_MAX, | |
2501 | CEPH_OSD_FLAG_READ, NULL); | |
2502 | dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret); | |
2503 | if (ret < 0) | |
2504 | goto out; | |
2505 | ||
2506 | p = response; | |
2507 | rbd_dev->image_id = ceph_extract_encoded_string(&p, | |
2508 | p + RBD_IMAGE_ID_LEN_MAX, | |
2509 | &rbd_dev->image_id_len, | |
2510 | GFP_NOIO); | |
2511 | if (IS_ERR(rbd_dev->image_id)) { | |
2512 | ret = PTR_ERR(rbd_dev->image_id); | |
2513 | rbd_dev->image_id = NULL; | |
2514 | } else { | |
2515 | dout("image_id is %s\n", rbd_dev->image_id); | |
2516 | } | |
2517 | out: | |
2518 | kfree(response); | |
2519 | kfree(object_name); | |
2520 | ||
2521 | return ret; | |
2522 | } | |
2523 | ||
59c2be1e YS |
2524 | static ssize_t rbd_add(struct bus_type *bus, |
2525 | const char *buf, | |
2526 | size_t count) | |
602adf40 | 2527 | { |
cb8627c7 AE |
2528 | char *options; |
2529 | struct rbd_device *rbd_dev = NULL; | |
7ef3214a AE |
2530 | const char *mon_addrs = NULL; |
2531 | size_t mon_addrs_size = 0; | |
27cc2594 AE |
2532 | struct ceph_osd_client *osdc; |
2533 | int rc = -ENOMEM; | |
3feeb894 | 2534 | char *snap_name; |
602adf40 YS |
2535 | |
2536 | if (!try_module_get(THIS_MODULE)) | |
2537 | return -ENODEV; | |
2538 | ||
60571c7d | 2539 | options = kmalloc(count, GFP_KERNEL); |
602adf40 | 2540 | if (!options) |
85ae8926 | 2541 | goto err_out_mem; |
cb8627c7 AE |
2542 | rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL); |
2543 | if (!rbd_dev) | |
85ae8926 | 2544 | goto err_out_mem; |
602adf40 YS |
2545 | |
2546 | /* static rbd_device initialization */ | |
2547 | spin_lock_init(&rbd_dev->lock); | |
2548 | INIT_LIST_HEAD(&rbd_dev->node); | |
dfc5606d | 2549 | INIT_LIST_HEAD(&rbd_dev->snaps); |
c666601a | 2550 | init_rwsem(&rbd_dev->header_rwsem); |
602adf40 | 2551 | |
602adf40 | 2552 | /* parse add command */ |
3feeb894 AE |
2553 | snap_name = rbd_add_parse_args(rbd_dev, buf, |
2554 | &mon_addrs, &mon_addrs_size, options, count); | |
2555 | if (IS_ERR(snap_name)) { | |
2556 | rc = PTR_ERR(snap_name); | |
85ae8926 | 2557 | goto err_out_mem; |
3feeb894 | 2558 | } |
e124a82f | 2559 | |
f8c38929 AE |
2560 | rc = rbd_get_client(rbd_dev, mon_addrs, mon_addrs_size - 1, options); |
2561 | if (rc < 0) | |
85ae8926 | 2562 | goto err_out_args; |
602adf40 | 2563 | |
602adf40 | 2564 | /* pick the pool */ |
1dbb4399 | 2565 | osdc = &rbd_dev->rbd_client->client->osdc; |
602adf40 YS |
2566 | rc = ceph_pg_poolid_by_name(osdc->osdmap, rbd_dev->pool_name); |
2567 | if (rc < 0) | |
2568 | goto err_out_client; | |
9bb2f334 | 2569 | rbd_dev->pool_id = rc; |
602adf40 | 2570 | |
589d30e0 AE |
2571 | rc = rbd_dev_image_id(rbd_dev); |
2572 | if (!rc) { | |
2573 | rc = -ENOTSUPP; /* Not actually supporting format 2 yet */ | |
2574 | goto err_out_client; | |
2575 | } | |
2576 | ||
2577 | /* Version 1 images have no id; empty string is used */ | |
2578 | ||
2579 | rbd_dev->image_id = kstrdup("", GFP_KERNEL); | |
2580 | if (!rbd_dev->image_id) { | |
2581 | rc = -ENOMEM; | |
2582 | goto err_out_client; | |
2583 | } | |
2584 | rbd_dev->image_id_len = 0; | |
2585 | ||
3fcf2581 AE |
2586 | /* Create the name of the header object */ |
2587 | ||
2588 | rbd_dev->header_name = kmalloc(rbd_dev->image_name_len | |
2589 | + sizeof (RBD_SUFFIX), | |
2590 | GFP_KERNEL); | |
2591 | if (!rbd_dev->header_name) | |
2592 | goto err_out_client; | |
2593 | sprintf(rbd_dev->header_name, "%s%s", rbd_dev->image_name, RBD_SUFFIX); | |
2594 | ||
05fd6f6f AE |
2595 | /* Get information about the image being mapped */ |
2596 | ||
2597 | rc = rbd_read_header(rbd_dev, &rbd_dev->header); | |
2598 | if (rc) | |
2599 | goto err_out_client; | |
2600 | ||
2601 | /* no need to lock here, as rbd_dev is not registered yet */ | |
2602 | rc = rbd_dev_snaps_update(rbd_dev); | |
2603 | if (rc) | |
2604 | goto err_out_header; | |
2605 | ||
2606 | rc = rbd_dev_set_mapping(rbd_dev, snap_name); | |
2607 | if (rc) | |
2608 | goto err_out_header; | |
2609 | ||
85ae8926 AE |
2610 | /* generate unique id: find highest unique id, add one */ |
2611 | rbd_dev_id_get(rbd_dev); | |
2612 | ||
2613 | /* Fill in the device name, now that we have its id. */ | |
2614 | BUILD_BUG_ON(DEV_NAME_LEN | |
2615 | < sizeof (RBD_DRV_NAME) + MAX_INT_FORMAT_WIDTH); | |
2616 | sprintf(rbd_dev->name, "%s%d", RBD_DRV_NAME, rbd_dev->dev_id); | |
2617 | ||
2618 | /* Get our block major device number. */ | |
2619 | ||
27cc2594 AE |
2620 | rc = register_blkdev(0, rbd_dev->name); |
2621 | if (rc < 0) | |
85ae8926 | 2622 | goto err_out_id; |
27cc2594 | 2623 | rbd_dev->major = rc; |
602adf40 | 2624 | |
0f308a31 AE |
2625 | /* Set up the blkdev mapping. */ |
2626 | ||
2627 | rc = rbd_init_disk(rbd_dev); | |
dfc5606d | 2628 | if (rc) |
766fc439 YS |
2629 | goto err_out_blkdev; |
2630 | ||
0f308a31 AE |
2631 | rc = rbd_bus_add_dev(rbd_dev); |
2632 | if (rc) | |
2633 | goto err_out_disk; | |
2634 | ||
32eec68d AE |
2635 | /* |
2636 | * At this point cleanup in the event of an error is the job | |
2637 | * of the sysfs code (initiated by rbd_bus_del_dev()). | |
32eec68d | 2638 | */ |
2ac4e75d | 2639 | |
4bb1f1ed | 2640 | down_write(&rbd_dev->header_rwsem); |
5ed16177 | 2641 | rc = rbd_dev_snaps_register(rbd_dev); |
4bb1f1ed | 2642 | up_write(&rbd_dev->header_rwsem); |
2ac4e75d AE |
2643 | if (rc) |
2644 | goto err_out_bus; | |
2645 | ||
3ee4001e AE |
2646 | rc = rbd_init_watch_dev(rbd_dev); |
2647 | if (rc) | |
2648 | goto err_out_bus; | |
2649 | ||
2ac4e75d AE |
2650 | /* Everything's ready. Announce the disk to the world. */ |
2651 | ||
2ac4e75d | 2652 | add_disk(rbd_dev->disk); |
3ee4001e | 2653 | |
2ac4e75d AE |
2654 | pr_info("%s: added with size 0x%llx\n", rbd_dev->disk->disk_name, |
2655 | (unsigned long long) rbd_dev->mapping.size); | |
2656 | ||
602adf40 YS |
2657 | return count; |
2658 | ||
766fc439 | 2659 | err_out_bus: |
766fc439 YS |
2660 | /* this will also clean up rest of rbd_dev stuff */ |
2661 | ||
2662 | rbd_bus_del_dev(rbd_dev); | |
2663 | kfree(options); | |
766fc439 YS |
2664 | return rc; |
2665 | ||
0f308a31 AE |
2666 | err_out_disk: |
2667 | rbd_free_disk(rbd_dev); | |
602adf40 YS |
2668 | err_out_blkdev: |
2669 | unregister_blkdev(rbd_dev->major, rbd_dev->name); | |
85ae8926 AE |
2670 | err_out_id: |
2671 | rbd_dev_id_put(rbd_dev); | |
05fd6f6f AE |
2672 | err_out_header: |
2673 | rbd_header_free(&rbd_dev->header); | |
602adf40 | 2674 | err_out_client: |
3fcf2581 | 2675 | kfree(rbd_dev->header_name); |
602adf40 | 2676 | rbd_put_client(rbd_dev); |
589d30e0 | 2677 | kfree(rbd_dev->image_id); |
85ae8926 AE |
2678 | err_out_args: |
2679 | kfree(rbd_dev->mapping.snap_name); | |
2680 | kfree(rbd_dev->image_name); | |
2681 | kfree(rbd_dev->pool_name); | |
2682 | err_out_mem: | |
27cc2594 | 2683 | kfree(rbd_dev); |
cb8627c7 | 2684 | kfree(options); |
27cc2594 | 2685 | |
602adf40 YS |
2686 | dout("Error adding device %s\n", buf); |
2687 | module_put(THIS_MODULE); | |
27cc2594 AE |
2688 | |
2689 | return (ssize_t) rc; | |
602adf40 YS |
2690 | } |
2691 | ||
de71a297 | 2692 | static struct rbd_device *__rbd_get_dev(unsigned long dev_id) |
602adf40 YS |
2693 | { |
2694 | struct list_head *tmp; | |
2695 | struct rbd_device *rbd_dev; | |
2696 | ||
e124a82f | 2697 | spin_lock(&rbd_dev_list_lock); |
602adf40 YS |
2698 | list_for_each(tmp, &rbd_dev_list) { |
2699 | rbd_dev = list_entry(tmp, struct rbd_device, node); | |
de71a297 | 2700 | if (rbd_dev->dev_id == dev_id) { |
e124a82f | 2701 | spin_unlock(&rbd_dev_list_lock); |
602adf40 | 2702 | return rbd_dev; |
e124a82f | 2703 | } |
602adf40 | 2704 | } |
e124a82f | 2705 | spin_unlock(&rbd_dev_list_lock); |
602adf40 YS |
2706 | return NULL; |
2707 | } | |
2708 | ||
dfc5606d | 2709 | static void rbd_dev_release(struct device *dev) |
602adf40 | 2710 | { |
593a9e7b | 2711 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
602adf40 | 2712 | |
1dbb4399 AE |
2713 | if (rbd_dev->watch_request) { |
2714 | struct ceph_client *client = rbd_dev->rbd_client->client; | |
2715 | ||
2716 | ceph_osdc_unregister_linger_request(&client->osdc, | |
59c2be1e | 2717 | rbd_dev->watch_request); |
1dbb4399 | 2718 | } |
59c2be1e | 2719 | if (rbd_dev->watch_event) |
070c633f | 2720 | rbd_req_sync_unwatch(rbd_dev); |
59c2be1e | 2721 | |
602adf40 YS |
2722 | rbd_put_client(rbd_dev); |
2723 | ||
2724 | /* clean up and free blkdev */ | |
2725 | rbd_free_disk(rbd_dev); | |
2726 | unregister_blkdev(rbd_dev->major, rbd_dev->name); | |
32eec68d | 2727 | |
2ac4e75d AE |
2728 | /* release allocated disk header fields */ |
2729 | rbd_header_free(&rbd_dev->header); | |
2730 | ||
32eec68d | 2731 | /* done with the id, and with the rbd_dev */ |
f84344f3 | 2732 | kfree(rbd_dev->mapping.snap_name); |
589d30e0 | 2733 | kfree(rbd_dev->image_id); |
0bed54dc | 2734 | kfree(rbd_dev->header_name); |
d22f76e7 | 2735 | kfree(rbd_dev->pool_name); |
0bed54dc | 2736 | kfree(rbd_dev->image_name); |
e2839308 | 2737 | rbd_dev_id_put(rbd_dev); |
602adf40 YS |
2738 | kfree(rbd_dev); |
2739 | ||
2740 | /* release module ref */ | |
2741 | module_put(THIS_MODULE); | |
602adf40 YS |
2742 | } |
2743 | ||
dfc5606d YS |
2744 | static ssize_t rbd_remove(struct bus_type *bus, |
2745 | const char *buf, | |
2746 | size_t count) | |
602adf40 YS |
2747 | { |
2748 | struct rbd_device *rbd_dev = NULL; | |
2749 | int target_id, rc; | |
2750 | unsigned long ul; | |
2751 | int ret = count; | |
2752 | ||
2753 | rc = strict_strtoul(buf, 10, &ul); | |
2754 | if (rc) | |
2755 | return rc; | |
2756 | ||
2757 | /* convert to int; abort if we lost anything in the conversion */ | |
2758 | target_id = (int) ul; | |
2759 | if (target_id != ul) | |
2760 | return -EINVAL; | |
2761 | ||
2762 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); | |
2763 | ||
2764 | rbd_dev = __rbd_get_dev(target_id); | |
2765 | if (!rbd_dev) { | |
2766 | ret = -ENOENT; | |
2767 | goto done; | |
2768 | } | |
2769 | ||
dfc5606d YS |
2770 | __rbd_remove_all_snaps(rbd_dev); |
2771 | rbd_bus_del_dev(rbd_dev); | |
602adf40 YS |
2772 | |
2773 | done: | |
2774 | mutex_unlock(&ctl_mutex); | |
aafb230e | 2775 | |
602adf40 YS |
2776 | return ret; |
2777 | } | |
2778 | ||
602adf40 YS |
2779 | /* |
2780 | * create control files in sysfs | |
dfc5606d | 2781 | * /sys/bus/rbd/... |
602adf40 YS |
2782 | */ |
2783 | static int rbd_sysfs_init(void) | |
2784 | { | |
dfc5606d | 2785 | int ret; |
602adf40 | 2786 | |
fed4c143 | 2787 | ret = device_register(&rbd_root_dev); |
21079786 | 2788 | if (ret < 0) |
dfc5606d | 2789 | return ret; |
602adf40 | 2790 | |
fed4c143 AE |
2791 | ret = bus_register(&rbd_bus_type); |
2792 | if (ret < 0) | |
2793 | device_unregister(&rbd_root_dev); | |
602adf40 | 2794 | |
602adf40 YS |
2795 | return ret; |
2796 | } | |
2797 | ||
2798 | static void rbd_sysfs_cleanup(void) | |
2799 | { | |
dfc5606d | 2800 | bus_unregister(&rbd_bus_type); |
fed4c143 | 2801 | device_unregister(&rbd_root_dev); |
602adf40 YS |
2802 | } |
2803 | ||
2804 | int __init rbd_init(void) | |
2805 | { | |
2806 | int rc; | |
2807 | ||
2808 | rc = rbd_sysfs_init(); | |
2809 | if (rc) | |
2810 | return rc; | |
f0f8cef5 | 2811 | pr_info("loaded " RBD_DRV_NAME_LONG "\n"); |
602adf40 YS |
2812 | return 0; |
2813 | } | |
2814 | ||
2815 | void __exit rbd_exit(void) | |
2816 | { | |
2817 | rbd_sysfs_cleanup(); | |
2818 | } | |
2819 | ||
2820 | module_init(rbd_init); | |
2821 | module_exit(rbd_exit); | |
2822 | ||
2823 | MODULE_AUTHOR("Sage Weil <sage@newdream.net>"); | |
2824 | MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>"); | |
2825 | MODULE_DESCRIPTION("rados block device"); | |
2826 | ||
2827 | /* following authorship retained from original osdblk.c */ | |
2828 | MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>"); | |
2829 | ||
2830 | MODULE_LICENSE("GPL"); |